Update build bits.
[awesomized/libmemcached] / clients / memcapable.cc
1 /* LibMemcached
2 * Copyright (C) 2006-2009 Brian Aker
3 * All rights reserved.
4 *
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
7 *
8 * Summary:
9 *
10 */
11
12 /* -*- Mode: C; tab-width: 2; c-basic-offset: 2; indent-tabs-mode: nil -*- */
13 #undef NDEBUG
14
15 #include <config.h>
16
17 #ifdef HAVE_POLL_H
18 #include <poll.h>
19 #else
20 #include "poll/poll.h"
21 #endif
22
23 #include <assert.h>
24 #include <ctype.h>
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <inttypes.h>
28 #include <pthread.h>
29 #include <signal.h>
30 #include <stdbool.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <sys/types.h>
35 #include <unistd.h>
36
37 #include <libmemcached/memcached.h>
38 #include <libmemcached/memcached/protocol_binary.h>
39 #include <libmemcached/byteorder.h>
40 #include <clients/utilities.h>
41
42 #ifdef linux
43 /* /usr/include/netinet/in.h defines macros from ntohs() to _bswap_nn to
44 * optimize the conversion functions, but the prototypes generate warnings
45 * from gcc. The conversion methods isn't the bottleneck for my app, so
46 * just remove the warnings by undef'ing the optimization ..
47 */
48 #undef ntohs
49 #undef ntohl
50 #endif
51
52 /* Should we generate coredumps when we enounter an error (-c) */
53 static bool do_core= false;
54 /* connection to the server */
55 static memcached_socket_t sock;
56 /* Should the output from test failures be verbose or quiet? */
57 static bool verbose= false;
58
59 /* The number of seconds to wait for an IO-operation */
60 static int timeout= 2;
61
62 /*
63 * Instead of having to cast between the different datatypes we create
64 * a union of all of the different types of pacages we want to send.
65 * A lot of the different commands use the same packet layout, so I'll
66 * just define the different types I need. The typedefs only contain
67 * the header of the message, so we need some space for keys and body
68 * To avoid to have to do multiple writes, lets add a chunk of memory
69 * to use. 1k should be more than enough for header, key and body.
70 */
71 typedef union
72 {
73 protocol_binary_request_no_extras plain;
74 protocol_binary_request_flush flush;
75 protocol_binary_request_incr incr;
76 protocol_binary_request_set set;
77 char bytes[1024];
78 } command;
79
80 typedef union
81 {
82 protocol_binary_response_no_extras plain;
83 protocol_binary_response_incr incr;
84 protocol_binary_response_decr decr;
85 char bytes[1024];
86 } response;
87
88 enum test_return
89 {
90 TEST_SKIP, TEST_PASS, TEST_PASS_RECONNECT, TEST_FAIL
91 };
92
93 /**
94 * Try to get an addrinfo struct for a given port on a given host
95 */
96 static struct addrinfo *lookuphost(const char *hostname, const char *port)
97 {
98 struct addrinfo *ai= 0;
99 struct addrinfo hints;
100 memset(&hints, 0, sizeof(struct addrinfo));
101 hints.ai_family=AF_UNSPEC;
102 hints.ai_protocol=IPPROTO_TCP;
103 hints.ai_socktype=SOCK_STREAM;
104
105 int error= getaddrinfo(hostname, port, &hints, &ai);
106 if (error != 0)
107 {
108 if (error != EAI_SYSTEM)
109 fprintf(stderr, "getaddrinfo(): %s\n", gai_strerror(error));
110 else
111 perror("getaddrinfo()");
112 }
113
114 return ai;
115 }
116
117 /**
118 * Set the socket in nonblocking mode
119 * @return -1 if failure, the socket otherwise
120 */
121 static memcached_socket_t set_noblock(void)
122 {
123 #ifdef WIN32
124 u_long arg = 1;
125 if (ioctlsocket(sock, FIONBIO, &arg) == SOCKET_ERROR)
126 {
127 perror("Failed to set nonblocking io");
128 closesocket(sock);
129 return INVALID_SOCKET;
130 }
131 #else
132 int flags= fcntl(sock, F_GETFL, 0);
133 if (flags == -1)
134 {
135 perror("Failed to get socket flags");
136 closesocket(sock);
137 return INVALID_SOCKET;
138 }
139
140 if ((flags & O_NONBLOCK) != O_NONBLOCK)
141 {
142 if (fcntl(sock, F_SETFL, flags | O_NONBLOCK) == -1)
143 {
144 perror("Failed to set socket to nonblocking mode");
145 closesocket(sock);
146 return INVALID_SOCKET;
147 }
148 }
149 #endif
150 return sock;
151 }
152
153 /**
154 * Try to open a connection to the server
155 * @param hostname the name of the server to connect to
156 * @param port the port number (or service) to connect to
157 * @return positive integer if success, -1 otherwise
158 */
159 static memcached_socket_t connect_server(const char *hostname, const char *port)
160 {
161 struct addrinfo *ai= lookuphost(hostname, port);
162 sock= INVALID_SOCKET;
163 if (ai != NULL)
164 {
165 if ((sock= socket(ai->ai_family, ai->ai_socktype,
166 ai->ai_protocol)) != INVALID_SOCKET)
167 {
168 if (connect(sock, ai->ai_addr, ai->ai_addrlen) == SOCKET_ERROR)
169 {
170 fprintf(stderr, "Failed to connect socket: %s\n",
171 strerror(get_socket_errno()));
172 closesocket(sock);
173 sock= INVALID_SOCKET;
174 }
175 else
176 {
177 sock= set_noblock();
178 }
179 }
180 else
181 fprintf(stderr, "Failed to create socket: %s\n",
182 strerror(get_socket_errno()));
183
184 freeaddrinfo(ai);
185 }
186
187 return sock;
188 }
189
190 static ssize_t timeout_io_op(memcached_socket_t fd, short direction, void *buf, size_t len)
191 {
192 ssize_t ret;
193
194 if (direction == POLLOUT)
195 {
196 ret= send(fd, buf, len, 0);
197 }
198 else
199 {
200 ret= recv(fd, buf, len, 0);
201 }
202
203 if (ret == SOCKET_ERROR && get_socket_errno() == EWOULDBLOCK)
204 {
205 struct pollfd fds;
206 memset(&fds, 0, sizeof(struct pollfd));
207 fds.events= direction;
208 fds.fd= fd;
209
210 int err= poll(&fds, 1, timeout * 1000);
211 if (err == 1)
212 {
213 if (direction == POLLOUT)
214 {
215 ret= send(fd, buf, len, 0);
216 }
217 else
218 {
219 ret= recv(fd, buf, len, 0);
220 }
221 }
222 else if (err == 0)
223 {
224 errno= ETIMEDOUT;
225 }
226 else
227 {
228 perror("Failed to poll");
229 return -1;
230 }
231 }
232
233 return ret;
234 }
235
236 /**
237 * Ensure that an expression is true. If it isn't print out a message similar
238 * to assert() and create a coredump if the user wants that. If not an error
239 * message is returned.
240 *
241 */
242 static enum test_return ensure(bool val, const char *expression, const char *file, int line)
243 {
244 if (!val)
245 {
246 if (verbose)
247 fprintf(stderr, "\n%s:%d: %s", file, line, expression);
248
249 if (do_core)
250 abort();
251
252 return TEST_FAIL;
253 }
254
255 return TEST_PASS;
256 }
257
258 #define verify(expression) do { if (ensure(expression, #expression, __FILE__, __LINE__) == TEST_FAIL) return TEST_FAIL; } while (0)
259 #define execute(expression) do { if (ensure(expression == TEST_PASS, #expression, __FILE__, __LINE__) == TEST_FAIL) return TEST_FAIL; } while (0)
260
261 /**
262 * Send a chunk of memory over the socket (retry if the call is iterrupted
263 */
264 static enum test_return retry_write(const void* buf, size_t len)
265 {
266 size_t offset= 0;
267 const char* ptr= static_cast<const char *>(buf);
268
269 do
270 {
271 size_t num_bytes= len - offset;
272 ssize_t nw= timeout_io_op(sock, POLLOUT, (void*)(ptr + offset), num_bytes);
273 if (nw == -1)
274 verify(get_socket_errno() == EINTR || get_socket_errno() == EAGAIN);
275 else
276 offset+= (size_t)nw;
277 } while (offset < len);
278
279 return TEST_PASS;
280 }
281
282 /**
283 * Resend a packet to the server (All fields in the command header should
284 * be in network byte order)
285 */
286 static enum test_return resend_packet(command *cmd)
287 {
288 size_t length= sizeof (protocol_binary_request_no_extras) +
289 ntohl(cmd->plain.message.header.request.bodylen);
290
291 execute(retry_write(cmd, length));
292 return TEST_PASS;
293 }
294
295 /**
296 * Send a command to the server. The command header needs to be updated
297 * to network byte order
298 */
299 static enum test_return send_packet(command *cmd)
300 {
301 /* Fix the byteorder of the header */
302 cmd->plain.message.header.request.keylen=
303 ntohs(cmd->plain.message.header.request.keylen);
304 cmd->plain.message.header.request.bodylen=
305 ntohl(cmd->plain.message.header.request.bodylen);
306 cmd->plain.message.header.request.cas=
307 memcached_ntohll(cmd->plain.message.header.request.cas);
308
309 execute(resend_packet(cmd));
310 return TEST_PASS;
311 }
312
313 /**
314 * Read a fixed length chunk of data from the server
315 */
316 static enum test_return retry_read(void *buf, size_t len)
317 {
318 size_t offset= 0;
319 do
320 {
321 ssize_t nr= timeout_io_op(sock, POLLIN, ((char*) buf) + offset, len - offset);
322 switch (nr) {
323 case -1 :
324 fprintf(stderr, "Errno: %d %s\n", get_socket_errno(), strerror(errno));
325 verify(get_socket_errno() == EINTR || get_socket_errno() == EAGAIN);
326 break;
327 case 0:
328 return TEST_FAIL;
329 default:
330 offset+= (size_t)nr;
331 }
332 } while (offset < len);
333
334 return TEST_PASS;
335 }
336
337 /**
338 * Receive a response from the server and conver the fields in the header
339 * to local byte order
340 */
341 static enum test_return recv_packet(response *rsp)
342 {
343 execute(retry_read(rsp, sizeof(protocol_binary_response_no_extras)));
344
345 /* Fix the byte order in the packet header */
346 rsp->plain.message.header.response.keylen=
347 ntohs(rsp->plain.message.header.response.keylen);
348 rsp->plain.message.header.response.status=
349 ntohs(rsp->plain.message.header.response.status);
350 rsp->plain.message.header.response.bodylen=
351 ntohl(rsp->plain.message.header.response.bodylen);
352 rsp->plain.message.header.response.cas=
353 memcached_ntohll(rsp->plain.message.header.response.cas);
354
355 size_t bodysz= rsp->plain.message.header.response.bodylen;
356 if (bodysz > 0)
357 execute(retry_read(rsp->bytes + sizeof (protocol_binary_response_no_extras), bodysz));
358
359 return TEST_PASS;
360 }
361
362 /**
363 * Create a storage command (add, set, replace etc)
364 *
365 * @param cmd destination buffer
366 * @param cc the storage command to create
367 * @param key the key to store
368 * @param keylen the length of the key
369 * @param dta the data to store with the key
370 * @param dtalen the length of the data to store with the key
371 * @param flags the flags to store along with the key
372 * @param exptime the expiry time for the key
373 */
374 static void storage_command(command *cmd,
375 uint8_t cc,
376 const void* key,
377 size_t keylen,
378 const void* dta,
379 size_t dtalen,
380 uint32_t flags,
381 uint32_t exptime)
382 {
383 /* all of the storage commands use the same command layout */
384 protocol_binary_request_set *request= &cmd->set;
385
386 memset(request, 0, sizeof (*request));
387 request->message.header.request.magic= PROTOCOL_BINARY_REQ;
388 request->message.header.request.opcode= cc;
389 request->message.header.request.keylen= (uint16_t)keylen;
390 request->message.header.request.extlen= 8;
391 request->message.header.request.bodylen= (uint32_t)(keylen + 8 + dtalen);
392 request->message.header.request.opaque= 0xdeadbeef;
393 request->message.body.flags= flags;
394 request->message.body.expiration= exptime;
395
396 off_t key_offset= sizeof (protocol_binary_request_no_extras) + 8;
397 memcpy(cmd->bytes + key_offset, key, keylen);
398 if (dta != NULL)
399 memcpy(cmd->bytes + key_offset + keylen, dta, dtalen);
400 }
401
402 /**
403 * Create a basic command to send to the server
404 * @param cmd destination buffer
405 * @param cc the command to create
406 * @param key the key to store
407 * @param keylen the length of the key
408 * @param dta the data to store with the key
409 * @param dtalen the length of the data to store with the key
410 */
411 static void raw_command(command *cmd,
412 uint8_t cc,
413 const void* key,
414 size_t keylen,
415 const void* dta,
416 size_t dtalen)
417 {
418 /* all of the storage commands use the same command layout */
419 memset(cmd, 0, sizeof (*cmd));
420 cmd->plain.message.header.request.magic= PROTOCOL_BINARY_REQ;
421 cmd->plain.message.header.request.opcode= cc;
422 cmd->plain.message.header.request.keylen= (uint16_t)keylen;
423 cmd->plain.message.header.request.bodylen= (uint32_t)(keylen + dtalen);
424 cmd->plain.message.header.request.opaque= 0xdeadbeef;
425
426 off_t key_offset= sizeof (protocol_binary_request_no_extras);
427
428 if (key != NULL)
429 memcpy(cmd->bytes + key_offset, key, keylen);
430
431 if (dta != NULL)
432 memcpy(cmd->bytes + key_offset + keylen, dta, dtalen);
433 }
434
435 /**
436 * Create the flush command
437 * @param cmd destination buffer
438 * @param cc the command to create (FLUSH/FLUSHQ)
439 * @param exptime when to flush
440 * @param use_extra to force using of the extra field?
441 */
442 static void flush_command(command *cmd,
443 uint8_t cc, uint32_t exptime, bool use_extra)
444 {
445 memset(cmd, 0, sizeof (cmd->flush));
446 cmd->flush.message.header.request.magic= PROTOCOL_BINARY_REQ;
447 cmd->flush.message.header.request.opcode= cc;
448 cmd->flush.message.header.request.opaque= 0xdeadbeef;
449
450 if (exptime != 0 || use_extra)
451 {
452 cmd->flush.message.header.request.extlen= 4;
453 cmd->flush.message.body.expiration= htonl(exptime);
454 cmd->flush.message.header.request.bodylen= 4;
455 }
456 }
457
458 /**
459 * Create a incr/decr command
460 * @param cc the cmd to create (FLUSH/FLUSHQ)
461 * @param key the key to operate on
462 * @param keylen the number of bytes in the key
463 * @param delta the number to add/subtract
464 * @param initial the initial value if the key doesn't exist
465 * @param exptime when the key should expire if it isn't set
466 */
467 static void arithmetic_command(command *cmd,
468 uint8_t cc,
469 const void* key,
470 size_t keylen,
471 uint64_t delta,
472 uint64_t initial,
473 uint32_t exptime)
474 {
475 memset(cmd, 0, sizeof (cmd->incr));
476 cmd->incr.message.header.request.magic= PROTOCOL_BINARY_REQ;
477 cmd->incr.message.header.request.opcode= cc;
478 cmd->incr.message.header.request.keylen= (uint16_t)keylen;
479 cmd->incr.message.header.request.extlen= 20;
480 cmd->incr.message.header.request.bodylen= (uint32_t)(keylen + 20);
481 cmd->incr.message.header.request.opaque= 0xdeadbeef;
482 cmd->incr.message.body.delta= memcached_htonll(delta);
483 cmd->incr.message.body.initial= memcached_htonll(initial);
484 cmd->incr.message.body.expiration= htonl(exptime);
485
486 off_t key_offset= sizeof (protocol_binary_request_no_extras) + 20;
487 memcpy(cmd->bytes + key_offset, key, keylen);
488 }
489
490 /**
491 * Validate the response header from the server
492 * @param rsp the response to check
493 * @param cc the expected command
494 * @param status the expected status
495 */
496 static enum test_return do_validate_response_header(response *rsp,
497 uint8_t cc, uint16_t status)
498 {
499 verify(rsp->plain.message.header.response.magic == PROTOCOL_BINARY_RES);
500 verify(rsp->plain.message.header.response.opcode == cc);
501 verify(rsp->plain.message.header.response.datatype == PROTOCOL_BINARY_RAW_BYTES);
502 verify(rsp->plain.message.header.response.status == status);
503 verify(rsp->plain.message.header.response.opaque == 0xdeadbeef);
504
505 if (status == PROTOCOL_BINARY_RESPONSE_SUCCESS)
506 {
507 switch (cc) {
508 case PROTOCOL_BINARY_CMD_ADDQ:
509 case PROTOCOL_BINARY_CMD_APPENDQ:
510 case PROTOCOL_BINARY_CMD_DECREMENTQ:
511 case PROTOCOL_BINARY_CMD_DELETEQ:
512 case PROTOCOL_BINARY_CMD_FLUSHQ:
513 case PROTOCOL_BINARY_CMD_INCREMENTQ:
514 case PROTOCOL_BINARY_CMD_PREPENDQ:
515 case PROTOCOL_BINARY_CMD_QUITQ:
516 case PROTOCOL_BINARY_CMD_REPLACEQ:
517 case PROTOCOL_BINARY_CMD_SETQ:
518 verify("Quiet command shouldn't return on success" == NULL);
519 default:
520 break;
521 }
522
523 switch (cc) {
524 case PROTOCOL_BINARY_CMD_ADD:
525 case PROTOCOL_BINARY_CMD_REPLACE:
526 case PROTOCOL_BINARY_CMD_SET:
527 case PROTOCOL_BINARY_CMD_APPEND:
528 case PROTOCOL_BINARY_CMD_PREPEND:
529 verify(rsp->plain.message.header.response.keylen == 0);
530 verify(rsp->plain.message.header.response.extlen == 0);
531 verify(rsp->plain.message.header.response.bodylen == 0);
532 verify(rsp->plain.message.header.response.cas != 0);
533 break;
534 case PROTOCOL_BINARY_CMD_FLUSH:
535 case PROTOCOL_BINARY_CMD_NOOP:
536 case PROTOCOL_BINARY_CMD_QUIT:
537 case PROTOCOL_BINARY_CMD_DELETE:
538 verify(rsp->plain.message.header.response.keylen == 0);
539 verify(rsp->plain.message.header.response.extlen == 0);
540 verify(rsp->plain.message.header.response.bodylen == 0);
541 verify(rsp->plain.message.header.response.cas == 0);
542 break;
543
544 case PROTOCOL_BINARY_CMD_DECREMENT:
545 case PROTOCOL_BINARY_CMD_INCREMENT:
546 verify(rsp->plain.message.header.response.keylen == 0);
547 verify(rsp->plain.message.header.response.extlen == 0);
548 verify(rsp->plain.message.header.response.bodylen == 8);
549 verify(rsp->plain.message.header.response.cas != 0);
550 break;
551
552 case PROTOCOL_BINARY_CMD_STAT:
553 verify(rsp->plain.message.header.response.extlen == 0);
554 /* key and value exists in all packets except in the terminating */
555 verify(rsp->plain.message.header.response.cas == 0);
556 break;
557
558 case PROTOCOL_BINARY_CMD_VERSION:
559 verify(rsp->plain.message.header.response.keylen == 0);
560 verify(rsp->plain.message.header.response.extlen == 0);
561 verify(rsp->plain.message.header.response.bodylen != 0);
562 verify(rsp->plain.message.header.response.cas == 0);
563 break;
564
565 case PROTOCOL_BINARY_CMD_GET:
566 case PROTOCOL_BINARY_CMD_GETQ:
567 verify(rsp->plain.message.header.response.keylen == 0);
568 verify(rsp->plain.message.header.response.extlen == 4);
569 verify(rsp->plain.message.header.response.cas != 0);
570 break;
571
572 case PROTOCOL_BINARY_CMD_GETK:
573 case PROTOCOL_BINARY_CMD_GETKQ:
574 verify(rsp->plain.message.header.response.keylen != 0);
575 verify(rsp->plain.message.header.response.extlen == 4);
576 verify(rsp->plain.message.header.response.cas != 0);
577 break;
578
579 default:
580 /* Undefined command code */
581 break;
582 }
583 }
584 else
585 {
586 verify(rsp->plain.message.header.response.cas == 0);
587 verify(rsp->plain.message.header.response.extlen == 0);
588 if (cc != PROTOCOL_BINARY_CMD_GETK)
589 {
590 verify(rsp->plain.message.header.response.keylen == 0);
591 }
592 }
593
594 return TEST_PASS;
595 }
596
597 /* We call verify(validate_response_header), but that macro
598 * expects a boolean expression, and the function returns
599 * an enum.... Let's just create a macro to avoid cluttering
600 * the code with all of the == TEST_PASS ;-)
601 */
602 #define validate_response_header(a,b,c) \
603 do_validate_response_header(a,b,c) == TEST_PASS
604
605
606 static enum test_return send_binary_noop(void)
607 {
608 command cmd;
609 raw_command(&cmd, PROTOCOL_BINARY_CMD_NOOP, NULL, 0, NULL, 0);
610 execute(send_packet(&cmd));
611 return TEST_PASS;
612 }
613
614 static enum test_return receive_binary_noop(void)
615 {
616 response rsp;
617 execute(recv_packet(&rsp));
618 verify(validate_response_header(&rsp, PROTOCOL_BINARY_CMD_NOOP,
619 PROTOCOL_BINARY_RESPONSE_SUCCESS));
620 return TEST_PASS;
621 }
622
623 static enum test_return test_binary_noop(void)
624 {
625 execute(send_binary_noop());
626 execute(receive_binary_noop());
627 return TEST_PASS;
628 }
629
630 static enum test_return test_binary_quit_impl(uint8_t cc)
631 {
632 command cmd;
633 response rsp;
634 raw_command(&cmd, cc, NULL, 0, NULL, 0);
635
636 execute(send_packet(&cmd));
637 if (cc == PROTOCOL_BINARY_CMD_QUIT)
638 {
639 execute(recv_packet(&rsp));
640 verify(validate_response_header(&rsp, PROTOCOL_BINARY_CMD_QUIT,
641 PROTOCOL_BINARY_RESPONSE_SUCCESS));
642 }
643
644 /* Socket should be closed now, read should return EXIT_SUCCESS */
645 verify(timeout_io_op(sock, POLLIN, rsp.bytes, sizeof(rsp.bytes)) == 0);
646
647 return TEST_PASS_RECONNECT;
648 }
649
650 static enum test_return test_binary_quit(void)
651 {
652 return test_binary_quit_impl(PROTOCOL_BINARY_CMD_QUIT);
653 }
654
655 static enum test_return test_binary_quitq(void)
656 {
657 return test_binary_quit_impl(PROTOCOL_BINARY_CMD_QUITQ);
658 }
659
660 static enum test_return test_binary_set_impl(const char* key, uint8_t cc)
661 {
662 command cmd;
663 response rsp;
664
665 uint64_t value= 0xdeadbeefdeadcafeULL;
666 storage_command(&cmd, cc, key, strlen(key), &value, sizeof (value), 0, 0);
667
668 /* set should always work */
669 for (int ii= 0; ii < 10; ii++)
670 {
671 if (ii == 0)
672 execute(send_packet(&cmd));
673 else
674 execute(resend_packet(&cmd));
675
676 if (cc == PROTOCOL_BINARY_CMD_SET)
677 {
678 execute(recv_packet(&rsp));
679 verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_SUCCESS));
680 }
681 else
682 execute(test_binary_noop());
683 }
684
685 /*
686 * We need to get the current CAS id, and at this time we haven't
687 * verified that we have a working get
688 */
689 if (cc == PROTOCOL_BINARY_CMD_SETQ)
690 {
691 cmd.set.message.header.request.opcode= PROTOCOL_BINARY_CMD_SET;
692 execute(resend_packet(&cmd));
693 execute(recv_packet(&rsp));
694 verify(validate_response_header(&rsp, PROTOCOL_BINARY_CMD_SET,
695 PROTOCOL_BINARY_RESPONSE_SUCCESS));
696 cmd.set.message.header.request.opcode= PROTOCOL_BINARY_CMD_SETQ;
697 }
698
699 /* try to set with the correct CAS value */
700 cmd.plain.message.header.request.cas= memcached_htonll(rsp.plain.message.header.response.cas);
701 execute(resend_packet(&cmd));
702 if (cc == PROTOCOL_BINARY_CMD_SET)
703 {
704 execute(recv_packet(&rsp));
705 verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_SUCCESS));
706 }
707 else
708 execute(test_binary_noop());
709
710 /* try to set with an incorrect CAS value */
711 cmd.plain.message.header.request.cas= memcached_htonll(rsp.plain.message.header.response.cas - 1);
712 execute(resend_packet(&cmd));
713 execute(send_binary_noop());
714 execute(recv_packet(&rsp));
715 verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS));
716 execute(receive_binary_noop());
717
718 return TEST_PASS;
719 }
720
721 static enum test_return test_binary_set(void)
722 {
723 return test_binary_set_impl("test_binary_set", PROTOCOL_BINARY_CMD_SET);
724 }
725
726 static enum test_return test_binary_setq(void)
727 {
728 return test_binary_set_impl("test_binary_setq", PROTOCOL_BINARY_CMD_SETQ);
729 }
730
731 static enum test_return test_binary_add_impl(const char* key, uint8_t cc)
732 {
733 command cmd;
734 response rsp;
735 uint64_t value= 0xdeadbeefdeadcafeULL;
736 storage_command(&cmd, cc, key, strlen(key), &value, sizeof (value), 0, 0);
737
738 /* first add should work, rest of them should fail (even with cas
739 as wildcard */
740 for (int ii=0; ii < 10; ii++)
741 {
742 if (ii == 0)
743 execute(send_packet(&cmd));
744 else
745 execute(resend_packet(&cmd));
746
747 if (cc == PROTOCOL_BINARY_CMD_ADD || ii > 0)
748 {
749 uint16_t expected_result;
750 if (ii == 0)
751 expected_result= PROTOCOL_BINARY_RESPONSE_SUCCESS;
752 else
753 expected_result= PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS;
754
755 execute(send_binary_noop());
756 execute(recv_packet(&rsp));
757 execute(receive_binary_noop());
758 verify(validate_response_header(&rsp, cc, expected_result));
759 }
760 else
761 execute(test_binary_noop());
762 }
763
764 return TEST_PASS;
765 }
766
767 static enum test_return test_binary_add(void)
768 {
769 return test_binary_add_impl("test_binary_add", PROTOCOL_BINARY_CMD_ADD);
770 }
771
772 static enum test_return test_binary_addq(void)
773 {
774 return test_binary_add_impl("test_binary_addq", PROTOCOL_BINARY_CMD_ADDQ);
775 }
776
777 static enum test_return binary_set_item(const char *key, const char *value)
778 {
779 command cmd;
780 response rsp;
781 storage_command(&cmd, PROTOCOL_BINARY_CMD_SET, key, strlen(key),
782 value, strlen(value), 0, 0);
783 execute(send_packet(&cmd));
784 execute(recv_packet(&rsp));
785 verify(validate_response_header(&rsp, PROTOCOL_BINARY_CMD_SET,
786 PROTOCOL_BINARY_RESPONSE_SUCCESS));
787 return TEST_PASS;
788 }
789
790 static enum test_return test_binary_replace_impl(const char* key, uint8_t cc)
791 {
792 command cmd;
793 response rsp;
794 uint64_t value= 0xdeadbeefdeadcafeULL;
795 storage_command(&cmd, cc, key, strlen(key), &value, sizeof (value), 0, 0);
796
797 /* first replace should fail, successive should succeed (when the
798 item is added! */
799 for (int ii= 0; ii < 10; ii++)
800 {
801 if (ii == 0)
802 execute(send_packet(&cmd));
803 else
804 execute(resend_packet(&cmd));
805
806 if (cc == PROTOCOL_BINARY_CMD_REPLACE || ii == 0)
807 {
808 uint16_t expected_result;
809 if (ii == 0)
810 expected_result=PROTOCOL_BINARY_RESPONSE_KEY_ENOENT;
811 else
812 expected_result=PROTOCOL_BINARY_RESPONSE_SUCCESS;
813
814 execute(send_binary_noop());
815 execute(recv_packet(&rsp));
816 execute(receive_binary_noop());
817 verify(validate_response_header(&rsp, cc, expected_result));
818
819 if (ii == 0)
820 execute(binary_set_item(key, key));
821 }
822 else
823 execute(test_binary_noop());
824 }
825
826 /* verify that replace with CAS value works! */
827 cmd.plain.message.header.request.cas= memcached_htonll(rsp.plain.message.header.response.cas);
828 execute(resend_packet(&cmd));
829
830 if (cc == PROTOCOL_BINARY_CMD_REPLACE)
831 {
832 execute(recv_packet(&rsp));
833 verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_SUCCESS));
834 }
835 else
836 execute(test_binary_noop());
837
838 /* try to set with an incorrect CAS value */
839 cmd.plain.message.header.request.cas= memcached_htonll(rsp.plain.message.header.response.cas - 1);
840 execute(resend_packet(&cmd));
841 execute(send_binary_noop());
842 execute(recv_packet(&rsp));
843 execute(receive_binary_noop());
844 verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS));
845
846 return TEST_PASS;
847 }
848
849 static enum test_return test_binary_replace(void)
850 {
851 return test_binary_replace_impl("test_binary_replace", PROTOCOL_BINARY_CMD_REPLACE);
852 }
853
854 static enum test_return test_binary_replaceq(void)
855 {
856 return test_binary_replace_impl("test_binary_replaceq", PROTOCOL_BINARY_CMD_REPLACEQ);
857 }
858
859 static enum test_return test_binary_delete_impl(const char *key, uint8_t cc)
860 {
861 command cmd;
862 response rsp;
863 raw_command(&cmd, cc, key, strlen(key), NULL, 0);
864
865 /* The delete shouldn't work the first time, because the item isn't there */
866 execute(send_packet(&cmd));
867 execute(send_binary_noop());
868 execute(recv_packet(&rsp));
869 verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_KEY_ENOENT));
870 execute(receive_binary_noop());
871 execute(binary_set_item(key, key));
872
873 /* The item should be present now, resend*/
874 execute(resend_packet(&cmd));
875 if (cc == PROTOCOL_BINARY_CMD_DELETE)
876 {
877 execute(recv_packet(&rsp));
878 verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_SUCCESS));
879 }
880
881 execute(test_binary_noop());
882
883 return TEST_PASS;
884 }
885
886 static enum test_return test_binary_delete(void)
887 {
888 return test_binary_delete_impl("test_binary_delete", PROTOCOL_BINARY_CMD_DELETE);
889 }
890
891 static enum test_return test_binary_deleteq(void)
892 {
893 return test_binary_delete_impl("test_binary_deleteq", PROTOCOL_BINARY_CMD_DELETEQ);
894 }
895
896 static enum test_return test_binary_get_impl(const char *key, uint8_t cc)
897 {
898 command cmd;
899 response rsp;
900
901 raw_command(&cmd, cc, key, strlen(key), NULL, 0);
902 execute(send_packet(&cmd));
903 execute(send_binary_noop());
904
905 if (cc == PROTOCOL_BINARY_CMD_GET || cc == PROTOCOL_BINARY_CMD_GETK)
906 {
907 execute(recv_packet(&rsp));
908 verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_KEY_ENOENT));
909 }
910
911 execute(receive_binary_noop());
912
913 execute(binary_set_item(key, key));
914 execute(resend_packet(&cmd));
915 execute(send_binary_noop());
916
917 execute(recv_packet(&rsp));
918 verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_SUCCESS));
919 execute(receive_binary_noop());
920
921 return TEST_PASS;
922 }
923
924 static enum test_return test_binary_get(void)
925 {
926 return test_binary_get_impl("test_binary_get", PROTOCOL_BINARY_CMD_GET);
927 }
928
929 static enum test_return test_binary_getk(void)
930 {
931 return test_binary_get_impl("test_binary_getk", PROTOCOL_BINARY_CMD_GETK);
932 }
933
934 static enum test_return test_binary_getq(void)
935 {
936 return test_binary_get_impl("test_binary_getq", PROTOCOL_BINARY_CMD_GETQ);
937 }
938
939 static enum test_return test_binary_getkq(void)
940 {
941 return test_binary_get_impl("test_binary_getkq", PROTOCOL_BINARY_CMD_GETKQ);
942 }
943
944 static enum test_return test_binary_incr_impl(const char* key, uint8_t cc)
945 {
946 command cmd;
947 response rsp;
948 arithmetic_command(&cmd, cc, key, strlen(key), 1, 0, 0);
949
950 uint64_t ii;
951 for (ii= 0; ii < 10; ++ii)
952 {
953 if (ii == 0)
954 execute(send_packet(&cmd));
955 else
956 execute(resend_packet(&cmd));
957
958 if (cc == PROTOCOL_BINARY_CMD_INCREMENT)
959 {
960 execute(recv_packet(&rsp));
961 verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_SUCCESS));
962 verify(memcached_ntohll(rsp.incr.message.body.value) == ii);
963 }
964 else
965 execute(test_binary_noop());
966 }
967
968 /* @todo add incorrect CAS */
969 return TEST_PASS;
970 }
971
972 static enum test_return test_binary_incr(void)
973 {
974 return test_binary_incr_impl("test_binary_incr", PROTOCOL_BINARY_CMD_INCREMENT);
975 }
976
977 static enum test_return test_binary_incrq(void)
978 {
979 return test_binary_incr_impl("test_binary_incrq", PROTOCOL_BINARY_CMD_INCREMENTQ);
980 }
981
982 static enum test_return test_binary_decr_impl(const char* key, uint8_t cc)
983 {
984 command cmd;
985 response rsp;
986 arithmetic_command(&cmd, cc, key, strlen(key), 1, 9, 0);
987
988 int ii;
989 for (ii= 9; ii > -1; --ii)
990 {
991 if (ii == 9)
992 execute(send_packet(&cmd));
993 else
994 execute(resend_packet(&cmd));
995
996 if (cc == PROTOCOL_BINARY_CMD_DECREMENT)
997 {
998 execute(recv_packet(&rsp));
999 verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_SUCCESS));
1000 verify(memcached_ntohll(rsp.decr.message.body.value) == (uint64_t)ii);
1001 }
1002 else
1003 execute(test_binary_noop());
1004 }
1005
1006 /* decr 0 should not wrap */
1007 execute(resend_packet(&cmd));
1008 if (cc == PROTOCOL_BINARY_CMD_DECREMENT)
1009 {
1010 execute(recv_packet(&rsp));
1011 verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_SUCCESS));
1012 verify(memcached_ntohll(rsp.decr.message.body.value) == 0);
1013 }
1014 else
1015 {
1016 /* @todo get the value and verify! */
1017
1018 }
1019
1020 /* @todo add incorrect cas */
1021 execute(test_binary_noop());
1022 return TEST_PASS;
1023 }
1024
1025 static enum test_return test_binary_decr(void)
1026 {
1027 return test_binary_decr_impl("test_binary_decr",
1028 PROTOCOL_BINARY_CMD_DECREMENT);
1029 }
1030
1031 static enum test_return test_binary_decrq(void)
1032 {
1033 return test_binary_decr_impl("test_binary_decrq",
1034 PROTOCOL_BINARY_CMD_DECREMENTQ);
1035 }
1036
1037 static enum test_return test_binary_version(void)
1038 {
1039 command cmd;
1040 response rsp;
1041 raw_command(&cmd, PROTOCOL_BINARY_CMD_VERSION, NULL, 0, NULL, 0);
1042
1043 execute(send_packet(&cmd));
1044 execute(recv_packet(&rsp));
1045 verify(validate_response_header(&rsp, PROTOCOL_BINARY_CMD_VERSION,
1046 PROTOCOL_BINARY_RESPONSE_SUCCESS));
1047
1048 return TEST_PASS;
1049 }
1050
1051 static enum test_return test_binary_flush_impl(const char *key, uint8_t cc)
1052 {
1053 command cmd;
1054 response rsp;
1055
1056 for (int ii= 0; ii < 2; ++ii)
1057 {
1058 execute(binary_set_item(key, key));
1059 flush_command(&cmd, cc, 0, ii == 0);
1060 execute(send_packet(&cmd));
1061
1062 if (cc == PROTOCOL_BINARY_CMD_FLUSH)
1063 {
1064 execute(recv_packet(&rsp));
1065 verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_SUCCESS));
1066 }
1067 else
1068 execute(test_binary_noop());
1069
1070 raw_command(&cmd, PROTOCOL_BINARY_CMD_GET, key, strlen(key), NULL, 0);
1071 execute(send_packet(&cmd));
1072 execute(recv_packet(&rsp));
1073 verify(validate_response_header(&rsp, PROTOCOL_BINARY_CMD_GET,
1074 PROTOCOL_BINARY_RESPONSE_KEY_ENOENT));
1075 }
1076
1077 return TEST_PASS;
1078 }
1079
1080 static enum test_return test_binary_flush(void)
1081 {
1082 return test_binary_flush_impl("test_binary_flush", PROTOCOL_BINARY_CMD_FLUSH);
1083 }
1084
1085 static enum test_return test_binary_flushq(void)
1086 {
1087 return test_binary_flush_impl("test_binary_flushq", PROTOCOL_BINARY_CMD_FLUSHQ);
1088 }
1089
1090 static enum test_return test_binary_concat_impl(const char *key, uint8_t cc)
1091 {
1092 command cmd;
1093 response rsp;
1094 const char *value;
1095
1096 if (cc == PROTOCOL_BINARY_CMD_APPEND || cc == PROTOCOL_BINARY_CMD_APPENDQ)
1097 value="hello";
1098 else
1099 value=" world";
1100
1101 execute(binary_set_item(key, value));
1102
1103 if (cc == PROTOCOL_BINARY_CMD_APPEND || cc == PROTOCOL_BINARY_CMD_APPENDQ)
1104 value=" world";
1105 else
1106 value="hello";
1107
1108 raw_command(&cmd, cc, key, strlen(key), value, strlen(value));
1109 execute(send_packet(&cmd));
1110 if (cc == PROTOCOL_BINARY_CMD_APPEND || cc == PROTOCOL_BINARY_CMD_PREPEND)
1111 {
1112 execute(recv_packet(&rsp));
1113 verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_SUCCESS));
1114 }
1115 else
1116 execute(test_binary_noop());
1117
1118 raw_command(&cmd, PROTOCOL_BINARY_CMD_GET, key, strlen(key), NULL, 0);
1119 execute(send_packet(&cmd));
1120 execute(recv_packet(&rsp));
1121 verify(validate_response_header(&rsp, PROTOCOL_BINARY_CMD_GET,
1122 PROTOCOL_BINARY_RESPONSE_SUCCESS));
1123 verify(rsp.plain.message.header.response.bodylen - 4 == 11);
1124 verify(memcmp(rsp.bytes + 28, "hello world", 11) == 0);
1125
1126 return TEST_PASS;
1127 }
1128
1129 static enum test_return test_binary_append(void)
1130 {
1131 return test_binary_concat_impl("test_binary_append", PROTOCOL_BINARY_CMD_APPEND);
1132 }
1133
1134 static enum test_return test_binary_prepend(void)
1135 {
1136 return test_binary_concat_impl("test_binary_prepend", PROTOCOL_BINARY_CMD_PREPEND);
1137 }
1138
1139 static enum test_return test_binary_appendq(void)
1140 {
1141 return test_binary_concat_impl("test_binary_appendq", PROTOCOL_BINARY_CMD_APPENDQ);
1142 }
1143
1144 static enum test_return test_binary_prependq(void)
1145 {
1146 return test_binary_concat_impl("test_binary_prependq", PROTOCOL_BINARY_CMD_PREPENDQ);
1147 }
1148
1149 static enum test_return test_binary_stat(void)
1150 {
1151 command cmd;
1152 response rsp;
1153
1154 raw_command(&cmd, PROTOCOL_BINARY_CMD_STAT, NULL, 0, NULL, 0);
1155 execute(send_packet(&cmd));
1156
1157 do
1158 {
1159 execute(recv_packet(&rsp));
1160 verify(validate_response_header(&rsp, PROTOCOL_BINARY_CMD_STAT,
1161 PROTOCOL_BINARY_RESPONSE_SUCCESS));
1162 } while (rsp.plain.message.header.response.keylen != 0);
1163
1164 return TEST_PASS;
1165 }
1166
1167 static enum test_return send_string(const char *cmd)
1168 {
1169 execute(retry_write(cmd, strlen(cmd)));
1170 return TEST_PASS;
1171 }
1172
1173 static enum test_return receive_line(char *buffer, size_t size)
1174 {
1175 size_t offset= 0;
1176 while (offset < size)
1177 {
1178 execute(retry_read(buffer + offset, 1));
1179 if (buffer[offset] == '\n')
1180 {
1181 if (offset + 1 < size)
1182 {
1183 buffer[offset + 1]= '\0';
1184 return TEST_PASS;
1185 }
1186 else
1187 return TEST_FAIL;
1188 }
1189 ++offset;
1190 }
1191
1192 return TEST_FAIL;
1193 }
1194
1195 static enum test_return receive_response(const char *msg) {
1196 char buffer[80];
1197 execute(receive_line(buffer, sizeof(buffer)));
1198 if (strcmp(msg, buffer) != 0) {
1199 fprintf(stderr, "[%s]\n", buffer);
1200 }
1201 verify(strcmp(msg, buffer) == 0);
1202 return TEST_PASS;
1203 }
1204
1205 static enum test_return receive_error_response(void)
1206 {
1207 char buffer[80];
1208 execute(receive_line(buffer, sizeof(buffer)));
1209 verify(strncmp(buffer, "ERROR", 5) == 0 ||
1210 strncmp(buffer, "CLIENT_ERROR", 12) == 0 ||
1211 strncmp(buffer, "SERVER_ERROR", 12) == 0);
1212 return TEST_PASS;
1213 }
1214
1215 static enum test_return test_ascii_quit(void)
1216 {
1217 /* Verify that quit handles unknown options */
1218 execute(send_string("quit foo bar\r\n"));
1219 execute(receive_error_response());
1220
1221 /* quit doesn't support noreply */
1222 execute(send_string("quit noreply\r\n"));
1223 execute(receive_error_response());
1224
1225 /* Verify that quit works */
1226 execute(send_string("quit\r\n"));
1227
1228 /* Socket should be closed now, read should return EXIT_SUCCESS */
1229 char buffer[80];
1230 verify(timeout_io_op(sock, POLLIN, buffer, sizeof(buffer)) == 0);
1231 return TEST_PASS_RECONNECT;
1232
1233 }
1234
1235 static enum test_return test_ascii_version(void)
1236 {
1237 /* Verify that version command handles unknown options */
1238 execute(send_string("version foo bar\r\n"));
1239 execute(receive_error_response());
1240
1241 /* version doesn't support noreply */
1242 execute(send_string("version noreply\r\n"));
1243 execute(receive_error_response());
1244
1245 /* Verify that verify works */
1246 execute(send_string("version\r\n"));
1247 char buffer[256];
1248 execute(receive_line(buffer, sizeof(buffer)));
1249 verify(strncmp(buffer, "VERSION ", 8) == 0);
1250
1251 return TEST_PASS;
1252 }
1253
1254 static enum test_return test_ascii_verbosity(void)
1255 {
1256 /* This command does not adhere to the spec! */
1257 execute(send_string("verbosity foo bar my\r\n"));
1258 execute(receive_error_response());
1259
1260 execute(send_string("verbosity noreply\r\n"));
1261 execute(receive_error_response());
1262
1263 execute(send_string("verbosity 0 noreply\r\n"));
1264 execute(test_ascii_version());
1265
1266 execute(send_string("verbosity\r\n"));
1267 execute(receive_error_response());
1268
1269 execute(send_string("verbosity 1\r\n"));
1270 execute(receive_response("OK\r\n"));
1271
1272 execute(send_string("verbosity 0\r\n"));
1273 execute(receive_response("OK\r\n"));
1274
1275 return TEST_PASS;
1276 }
1277
1278
1279
1280 static enum test_return test_ascii_set_impl(const char* key, bool noreply)
1281 {
1282 /* @todo add tests for bogus format! */
1283 char buffer[1024];
1284 snprintf(buffer, sizeof(buffer), "set %s 0 0 5%s\r\nvalue\r\n", key, noreply ? " noreply" : "");
1285 execute(send_string(buffer));
1286
1287 if (!noreply)
1288 execute(receive_response("STORED\r\n"));
1289
1290 return test_ascii_version();
1291 }
1292
1293 static enum test_return test_ascii_set(void)
1294 {
1295 return test_ascii_set_impl("test_ascii_set", false);
1296 }
1297
1298 static enum test_return test_ascii_set_noreply(void)
1299 {
1300 return test_ascii_set_impl("test_ascii_set_noreply", true);
1301 }
1302
1303 static enum test_return test_ascii_add_impl(const char* key, bool noreply)
1304 {
1305 /* @todo add tests for bogus format! */
1306 char buffer[1024];
1307 snprintf(buffer, sizeof(buffer), "add %s 0 0 5%s\r\nvalue\r\n", key, noreply ? " noreply" : "");
1308 execute(send_string(buffer));
1309
1310 if (!noreply)
1311 execute(receive_response("STORED\r\n"));
1312
1313 execute(send_string(buffer));
1314
1315 if (!noreply)
1316 execute(receive_response("NOT_STORED\r\n"));
1317
1318 return test_ascii_version();
1319 }
1320
1321 static enum test_return test_ascii_add(void)
1322 {
1323 return test_ascii_add_impl("test_ascii_add", false);
1324 }
1325
1326 static enum test_return test_ascii_add_noreply(void)
1327 {
1328 return test_ascii_add_impl("test_ascii_add_noreply", true);
1329 }
1330
1331 static enum test_return ascii_get_unknown_value(char **key, char **value, ssize_t *ndata)
1332 {
1333 char buffer[1024];
1334
1335 execute(receive_line(buffer, sizeof(buffer)));
1336 verify(strncmp(buffer, "VALUE ", 6) == 0);
1337 char *end= strchr(buffer + 6, ' ');
1338 verify(end != NULL);
1339 *end= '\0';
1340 *key= strdup(buffer + 6);
1341 verify(*key != NULL);
1342 char *ptr= end + 1;
1343
1344 unsigned long val= strtoul(ptr, &end, 10); /* flags */
1345 verify(ptr != end);
1346 verify(val == 0);
1347 verify(end != NULL);
1348 *ndata = (ssize_t)strtoul(end, &end, 10); /* size */
1349 verify(ptr != end);
1350 verify(end != NULL);
1351 while (*end != '\n' && isspace(*end))
1352 ++end;
1353 verify(*end == '\n');
1354
1355 *value= static_cast<char*>(malloc((size_t)*ndata));
1356 verify(*value != NULL);
1357
1358 execute(retry_read(*value, (size_t)*ndata));
1359
1360 execute(retry_read(buffer, 2));
1361 verify(memcmp(buffer, "\r\n", 2) == 0);
1362
1363 return TEST_PASS;
1364 }
1365
1366 static enum test_return ascii_get_value(const char *key, const char *value)
1367 {
1368
1369 char buffer[1024];
1370 size_t datasize= strlen(value);
1371
1372 verify(datasize < sizeof(buffer));
1373 execute(receive_line(buffer, sizeof(buffer)));
1374 verify(strncmp(buffer, "VALUE ", 6) == 0);
1375 verify(strncmp(buffer + 6, key, strlen(key)) == 0);
1376 char *ptr= buffer + 6 + strlen(key) + 1;
1377 char *end;
1378
1379 unsigned long val= strtoul(ptr, &end, 10); /* flags */
1380 verify(ptr != end);
1381 verify(val == 0);
1382 verify(end != NULL);
1383 val= strtoul(end, &end, 10); /* size */
1384 verify(ptr != end);
1385 verify(val == datasize);
1386 verify(end != NULL);
1387 while (*end != '\n' && isspace(*end))
1388 ++end;
1389 verify(*end == '\n');
1390
1391 execute(retry_read(buffer, datasize));
1392 verify(memcmp(buffer, value, datasize) == 0);
1393
1394 execute(retry_read(buffer, 2));
1395 verify(memcmp(buffer, "\r\n", 2) == 0);
1396
1397 return TEST_PASS;
1398 }
1399
1400 static enum test_return ascii_get_item(const char *key, const char *value,
1401 bool exist)
1402 {
1403 char buffer[1024];
1404 size_t datasize= 0;
1405 if (value != NULL)
1406 datasize= strlen(value);
1407
1408 verify(datasize < sizeof(buffer));
1409 snprintf(buffer, sizeof(buffer), "get %s\r\n", key);
1410 execute(send_string(buffer));
1411
1412 if (exist)
1413 execute(ascii_get_value(key, value));
1414
1415 execute(retry_read(buffer, 5));
1416 verify(memcmp(buffer, "END\r\n", 5) == 0);
1417
1418 return TEST_PASS;
1419 }
1420
1421 static enum test_return ascii_gets_value(const char *key, const char *value,
1422 unsigned long *cas)
1423 {
1424
1425 char buffer[1024];
1426 size_t datasize= strlen(value);
1427
1428 verify(datasize < sizeof(buffer));
1429 execute(receive_line(buffer, sizeof(buffer)));
1430 verify(strncmp(buffer, "VALUE ", 6) == 0);
1431 verify(strncmp(buffer + 6, key, strlen(key)) == 0);
1432 char *ptr= buffer + 6 + strlen(key) + 1;
1433 char *end;
1434
1435 unsigned long val= strtoul(ptr, &end, 10); /* flags */
1436 verify(ptr != end);
1437 verify(val == 0);
1438 verify(end != NULL);
1439 val= strtoul(end, &end, 10); /* size */
1440 verify(ptr != end);
1441 verify(val == datasize);
1442 verify(end != NULL);
1443 *cas= strtoul(end, &end, 10); /* cas */
1444 verify(ptr != end);
1445 verify(val == datasize);
1446 verify(end != NULL);
1447
1448 while (*end != '\n' && isspace(*end))
1449 ++end;
1450 verify(*end == '\n');
1451
1452 execute(retry_read(buffer, datasize));
1453 verify(memcmp(buffer, value, datasize) == 0);
1454
1455 execute(retry_read(buffer, 2));
1456 verify(memcmp(buffer, "\r\n", 2) == 0);
1457
1458 return TEST_PASS;
1459 }
1460
1461 static enum test_return ascii_gets_item(const char *key, const char *value,
1462 bool exist, unsigned long *cas)
1463 {
1464 char buffer[1024];
1465 size_t datasize= 0;
1466 if (value != NULL)
1467 datasize= strlen(value);
1468
1469 verify(datasize < sizeof(buffer));
1470 snprintf(buffer, sizeof(buffer), "gets %s\r\n", key);
1471 execute(send_string(buffer));
1472
1473 if (exist)
1474 execute(ascii_gets_value(key, value, cas));
1475
1476 execute(retry_read(buffer, 5));
1477 verify(memcmp(buffer, "END\r\n", 5) == 0);
1478
1479 return TEST_PASS;
1480 }
1481
1482 static enum test_return ascii_set_item(const char *key, const char *value)
1483 {
1484 char buffer[300];
1485 size_t len= strlen(value);
1486 snprintf(buffer, sizeof(buffer), "set %s 0 0 %u\r\n", key, (unsigned int)len);
1487 execute(send_string(buffer));
1488 execute(retry_write(value, len));
1489 execute(send_string("\r\n"));
1490 execute(receive_response("STORED\r\n"));
1491 return TEST_PASS;
1492 }
1493
1494 static enum test_return test_ascii_replace_impl(const char* key, bool noreply)
1495 {
1496 char buffer[1024];
1497 snprintf(buffer, sizeof(buffer), "replace %s 0 0 5%s\r\nvalue\r\n", key, noreply ? " noreply" : "");
1498 execute(send_string(buffer));
1499
1500 if (noreply)
1501 execute(test_ascii_version());
1502 else
1503 execute(receive_response("NOT_STORED\r\n"));
1504
1505 execute(ascii_set_item(key, "value"));
1506 execute(ascii_get_item(key, "value", true));
1507
1508
1509 execute(send_string(buffer));
1510
1511 if (noreply)
1512 execute(test_ascii_version());
1513 else
1514 execute(receive_response("STORED\r\n"));
1515
1516 return test_ascii_version();
1517 }
1518
1519 static enum test_return test_ascii_replace(void)
1520 {
1521 return test_ascii_replace_impl("test_ascii_replace", false);
1522 }
1523
1524 static enum test_return test_ascii_replace_noreply(void)
1525 {
1526 return test_ascii_replace_impl("test_ascii_replace_noreply", true);
1527 }
1528
1529 static enum test_return test_ascii_cas_impl(const char* key, bool noreply)
1530 {
1531 char buffer[1024];
1532 unsigned long cas;
1533
1534 execute(ascii_set_item(key, "value"));
1535 execute(ascii_gets_item(key, "value", true, &cas));
1536
1537 snprintf(buffer, sizeof(buffer), "cas %s 0 0 6 %lu%s\r\nvalue2\r\n", key, cas, noreply ? " noreply" : "");
1538 execute(send_string(buffer));
1539
1540 if (noreply)
1541 execute(test_ascii_version());
1542 else
1543 execute(receive_response("STORED\r\n"));
1544
1545 /* reexecute the same command should fail due to illegal cas */
1546 execute(send_string(buffer));
1547
1548 if (noreply)
1549 execute(test_ascii_version());
1550 else
1551 execute(receive_response("EXISTS\r\n"));
1552
1553 return test_ascii_version();
1554 }
1555
1556 static enum test_return test_ascii_cas(void)
1557 {
1558 return test_ascii_cas_impl("test_ascii_cas", false);
1559 }
1560
1561 static enum test_return test_ascii_cas_noreply(void)
1562 {
1563 return test_ascii_cas_impl("test_ascii_cas_noreply", true);
1564 }
1565
1566 static enum test_return test_ascii_delete_impl(const char *key, bool noreply)
1567 {
1568 execute(ascii_set_item(key, "value"));
1569
1570 execute(send_string("delete\r\n"));
1571 execute(receive_error_response());
1572 /* BUG: the server accepts delete a b */
1573 execute(send_string("delete a b c d e\r\n"));
1574 execute(receive_error_response());
1575
1576 char buffer[1024];
1577 snprintf(buffer, sizeof(buffer), "delete %s%s\r\n", key, noreply ? " noreply" : "");
1578 execute(send_string(buffer));
1579
1580 if (noreply)
1581 execute(test_ascii_version());
1582 else
1583 execute(receive_response("DELETED\r\n"));
1584
1585 execute(ascii_get_item(key, "value", false));
1586 execute(send_string(buffer));
1587 if (noreply)
1588 execute(test_ascii_version());
1589 else
1590 execute(receive_response("NOT_FOUND\r\n"));
1591
1592 return TEST_PASS;
1593 }
1594
1595 static enum test_return test_ascii_delete(void)
1596 {
1597 return test_ascii_delete_impl("test_ascii_delete", false);
1598 }
1599
1600 static enum test_return test_ascii_delete_noreply(void)
1601 {
1602 return test_ascii_delete_impl("test_ascii_delete_noreply", true);
1603 }
1604
1605 static enum test_return test_ascii_get(void)
1606 {
1607 execute(ascii_set_item("test_ascii_get", "value"));
1608
1609 execute(send_string("get\r\n"));
1610 execute(receive_error_response());
1611 execute(ascii_get_item("test_ascii_get", "value", true));
1612 execute(ascii_get_item("test_ascii_get_notfound", "value", false));
1613
1614 return TEST_PASS;
1615 }
1616
1617 static enum test_return test_ascii_gets(void)
1618 {
1619 execute(ascii_set_item("test_ascii_gets", "value"));
1620
1621 execute(send_string("gets\r\n"));
1622 execute(receive_error_response());
1623 unsigned long cas;
1624 execute(ascii_gets_item("test_ascii_gets", "value", true, &cas));
1625 execute(ascii_gets_item("test_ascii_gets_notfound", "value", false, &cas));
1626
1627 return TEST_PASS;
1628 }
1629
1630 static enum test_return test_ascii_mget(void)
1631 {
1632 const uint32_t nkeys= 5;
1633 const char * const keys[]= {
1634 "test_ascii_mget1",
1635 "test_ascii_mget2",
1636 /* test_ascii_mget_3 does not exist :) */
1637 "test_ascii_mget4",
1638 "test_ascii_mget5",
1639 "test_ascii_mget6"
1640 };
1641
1642 for (uint32_t x= 0; x < nkeys; ++x)
1643 execute(ascii_set_item(keys[x], "value"));
1644
1645 /* Ask for a key that doesn't exist as well */
1646 execute(send_string("get test_ascii_mget1 test_ascii_mget2 test_ascii_mget3 "
1647 "test_ascii_mget4 test_ascii_mget5 "
1648 "test_ascii_mget6\r\n"));
1649
1650 char *returned[nkeys];
1651
1652 for (uint32_t x= 0; x < nkeys; ++x)
1653 {
1654 ssize_t nbytes = 0;
1655 char *v= NULL;
1656 execute(ascii_get_unknown_value(&returned[x], &v, &nbytes));
1657 verify(nbytes == 5);
1658 verify(memcmp(v, "value", 5) == 0);
1659 free(v);
1660 }
1661
1662 char buffer[5];
1663 execute(retry_read(buffer, 5));
1664 verify(memcmp(buffer, "END\r\n", 5) == 0);
1665
1666 /* verify that we got all the keys we expected */
1667 for (uint32_t x= 0; x < nkeys; ++x)
1668 {
1669 bool found= false;
1670 for (uint32_t y= 0; y < nkeys; ++y)
1671 {
1672 if (strcmp(keys[x], returned[y]) == 0)
1673 {
1674 found = true;
1675 break;
1676 }
1677 }
1678 verify(found);
1679 }
1680
1681 for (uint32_t x= 0; x < nkeys; ++x)
1682 free(returned[x]);
1683
1684 return TEST_PASS;
1685 }
1686
1687 static enum test_return test_ascii_incr_impl(const char* key, bool noreply)
1688 {
1689 char cmd[300];
1690 snprintf(cmd, sizeof(cmd), "incr %s 1%s\r\n", key, noreply ? " noreply" : "");
1691
1692 execute(ascii_set_item(key, "0"));
1693 for (int x= 1; x < 11; ++x)
1694 {
1695 execute(send_string(cmd));
1696
1697 if (noreply)
1698 execute(test_ascii_version());
1699 else
1700 {
1701 char buffer[80];
1702 execute(receive_line(buffer, sizeof(buffer)));
1703 int val= atoi(buffer);
1704 verify(val == x);
1705 }
1706 }
1707
1708 execute(ascii_get_item(key, "10", true));
1709
1710 return TEST_PASS;
1711 }
1712
1713 static enum test_return test_ascii_incr(void)
1714 {
1715 return test_ascii_incr_impl("test_ascii_incr", false);
1716 }
1717
1718 static enum test_return test_ascii_incr_noreply(void)
1719 {
1720 return test_ascii_incr_impl("test_ascii_incr_noreply", true);
1721 }
1722
1723 static enum test_return test_ascii_decr_impl(const char* key, bool noreply)
1724 {
1725 char cmd[300];
1726 snprintf(cmd, sizeof(cmd), "decr %s 1%s\r\n", key, noreply ? " noreply" : "");
1727
1728 execute(ascii_set_item(key, "9"));
1729 for (int x= 8; x > -1; --x)
1730 {
1731 execute(send_string(cmd));
1732
1733 if (noreply)
1734 execute(test_ascii_version());
1735 else
1736 {
1737 char buffer[80];
1738 execute(receive_line(buffer, sizeof(buffer)));
1739 int val= atoi(buffer);
1740 verify(val == x);
1741 }
1742 }
1743
1744 execute(ascii_get_item(key, "0", true));
1745
1746 /* verify that it doesn't wrap */
1747 execute(send_string(cmd));
1748 if (noreply)
1749 execute(test_ascii_version());
1750 else
1751 {
1752 char buffer[80];
1753 execute(receive_line(buffer, sizeof(buffer)));
1754 }
1755 execute(ascii_get_item(key, "0", true));
1756
1757 return TEST_PASS;
1758 }
1759
1760 static enum test_return test_ascii_decr(void)
1761 {
1762 return test_ascii_decr_impl("test_ascii_decr", false);
1763 }
1764
1765 static enum test_return test_ascii_decr_noreply(void)
1766 {
1767 return test_ascii_decr_impl("test_ascii_decr_noreply", true);
1768 }
1769
1770
1771 static enum test_return test_ascii_flush_impl(const char *key, bool noreply)
1772 {
1773 #if 0
1774 /* Verify that the flush_all command handles unknown options */
1775 /* Bug in the current memcached server! */
1776 execute(send_string("flush_all foo bar\r\n"));
1777 execute(receive_error_response());
1778 #endif
1779
1780 execute(ascii_set_item(key, key));
1781 execute(ascii_get_item(key, key, true));
1782
1783 if (noreply)
1784 {
1785 execute(send_string("flush_all noreply\r\n"));
1786 execute(test_ascii_version());
1787 }
1788 else
1789 {
1790 execute(send_string("flush_all\r\n"));
1791 execute(receive_response("OK\r\n"));
1792 }
1793
1794 execute(ascii_get_item(key, key, false));
1795
1796 return TEST_PASS;
1797 }
1798
1799 static enum test_return test_ascii_flush(void)
1800 {
1801 return test_ascii_flush_impl("test_ascii_flush", false);
1802 }
1803
1804 static enum test_return test_ascii_flush_noreply(void)
1805 {
1806 return test_ascii_flush_impl("test_ascii_flush_noreply", true);
1807 }
1808
1809 static enum test_return test_ascii_concat_impl(const char *key,
1810 bool append,
1811 bool noreply)
1812 {
1813 const char *value;
1814
1815 if (append)
1816 value="hello";
1817 else
1818 value=" world";
1819
1820 execute(ascii_set_item(key, value));
1821
1822 if (append)
1823 value=" world";
1824 else
1825 value="hello";
1826
1827 char cmd[400];
1828 snprintf(cmd, sizeof(cmd), "%s %s 0 0 %u%s\r\n%s\r\n",
1829 append ? "append" : "prepend",
1830 key, (unsigned int)strlen(value), noreply ? " noreply" : "",
1831 value);
1832 execute(send_string(cmd));
1833
1834 if (noreply)
1835 execute(test_ascii_version());
1836 else
1837 execute(receive_response("STORED\r\n"));
1838
1839 execute(ascii_get_item(key, "hello world", true));
1840
1841 snprintf(cmd, sizeof(cmd), "%s %s_notfound 0 0 %u%s\r\n%s\r\n",
1842 append ? "append" : "prepend",
1843 key, (unsigned int)strlen(value), noreply ? " noreply" : "",
1844 value);
1845 execute(send_string(cmd));
1846
1847 if (noreply)
1848 execute(test_ascii_version());
1849 else
1850 execute(receive_response("NOT_STORED\r\n"));
1851
1852 return TEST_PASS;
1853 }
1854
1855 static enum test_return test_ascii_append(void)
1856 {
1857 return test_ascii_concat_impl("test_ascii_append", true, false);
1858 }
1859
1860 static enum test_return test_ascii_prepend(void)
1861 {
1862 return test_ascii_concat_impl("test_ascii_prepend", false, false);
1863 }
1864
1865 static enum test_return test_ascii_append_noreply(void)
1866 {
1867 return test_ascii_concat_impl("test_ascii_append_noreply", true, true);
1868 }
1869
1870 static enum test_return test_ascii_prepend_noreply(void)
1871 {
1872 return test_ascii_concat_impl("test_ascii_prepend_noreply", false, true);
1873 }
1874
1875 static enum test_return test_ascii_stat(void)
1876 {
1877 execute(send_string("stats noreply\r\n"));
1878 execute(receive_error_response());
1879 execute(send_string("stats\r\n"));
1880 char buffer[1024];
1881 do {
1882 execute(receive_line(buffer, sizeof(buffer)));
1883 } while (strcmp(buffer, "END\r\n") != 0);
1884
1885 return TEST_PASS_RECONNECT;
1886 }
1887
1888 typedef enum test_return(*TEST_FUNC)(void);
1889
1890 struct testcase
1891 {
1892 const char *description;
1893 TEST_FUNC function;
1894 };
1895
1896 struct testcase testcases[]= {
1897 { "ascii quit", test_ascii_quit },
1898 { "ascii version", test_ascii_version },
1899 { "ascii verbosity", test_ascii_verbosity },
1900 { "ascii set", test_ascii_set },
1901 { "ascii set noreply", test_ascii_set_noreply },
1902 { "ascii get", test_ascii_get },
1903 { "ascii gets", test_ascii_gets },
1904 { "ascii mget", test_ascii_mget },
1905 { "ascii flush", test_ascii_flush },
1906 { "ascii flush noreply", test_ascii_flush_noreply },
1907 { "ascii add", test_ascii_add },
1908 { "ascii add noreply", test_ascii_add_noreply },
1909 { "ascii replace", test_ascii_replace },
1910 { "ascii replace noreply", test_ascii_replace_noreply },
1911 { "ascii cas", test_ascii_cas },
1912 { "ascii cas noreply", test_ascii_cas_noreply },
1913 { "ascii delete", test_ascii_delete },
1914 { "ascii delete noreply", test_ascii_delete_noreply },
1915 { "ascii incr", test_ascii_incr },
1916 { "ascii incr noreply", test_ascii_incr_noreply },
1917 { "ascii decr", test_ascii_decr },
1918 { "ascii decr noreply", test_ascii_decr_noreply },
1919 { "ascii append", test_ascii_append },
1920 { "ascii append noreply", test_ascii_append_noreply },
1921 { "ascii prepend", test_ascii_prepend },
1922 { "ascii prepend noreply", test_ascii_prepend_noreply },
1923 { "ascii stat", test_ascii_stat },
1924 { "binary noop", test_binary_noop },
1925 { "binary quit", test_binary_quit },
1926 { "binary quitq", test_binary_quitq },
1927 { "binary set", test_binary_set },
1928 { "binary setq", test_binary_setq },
1929 { "binary flush", test_binary_flush },
1930 { "binary flushq", test_binary_flushq },
1931 { "binary add", test_binary_add },
1932 { "binary addq", test_binary_addq },
1933 { "binary replace", test_binary_replace },
1934 { "binary replaceq", test_binary_replaceq },
1935 { "binary delete", test_binary_delete },
1936 { "binary deleteq", test_binary_deleteq },
1937 { "binary get", test_binary_get },
1938 { "binary getq", test_binary_getq },
1939 { "binary getk", test_binary_getk },
1940 { "binary getkq", test_binary_getkq },
1941 { "binary incr", test_binary_incr },
1942 { "binary incrq", test_binary_incrq },
1943 { "binary decr", test_binary_decr },
1944 { "binary decrq", test_binary_decrq },
1945 { "binary version", test_binary_version },
1946 { "binary append", test_binary_append },
1947 { "binary appendq", test_binary_appendq },
1948 { "binary prepend", test_binary_prepend },
1949 { "binary prependq", test_binary_prependq },
1950 { "binary stat", test_binary_stat },
1951 { NULL, NULL}
1952 };
1953
1954 const int ascii_tests = 1;
1955 const int binary_tests = 2;
1956
1957 struct test_type_st
1958 {
1959 bool ascii;
1960 bool binary;
1961 };
1962
1963 int main(int argc, char **argv)
1964 {
1965 static const char * const status_msg[]= {"[skip]", "[pass]", "[pass]", "[FAIL]"};
1966 struct test_type_st tests= { true, true };
1967 int total= 0;
1968 int failed= 0;
1969 const char *hostname= "localhost";
1970 const char *port= "11211";
1971 int cmd;
1972 bool prompt= false;
1973 const char *testname= NULL;
1974
1975
1976
1977 while ((cmd= getopt(argc, argv, "t:vch:p:PT:?ab")) != EOF)
1978 {
1979 switch (cmd) {
1980 case 'a':
1981 tests.ascii= true;
1982 tests.binary= false;
1983 break;
1984 case 'b':
1985 tests.ascii= false;
1986 tests.binary= true;
1987 break;
1988 case 't':
1989 timeout= atoi(optarg);
1990 if (timeout == 0)
1991 {
1992 fprintf(stderr, "Invalid timeout. Please specify a number for -t\n");
1993 return EXIT_FAILURE;
1994 }
1995 break;
1996 case 'v': verbose= true;
1997 break;
1998 case 'c': do_core= true;
1999 break;
2000 case 'h': hostname= optarg;
2001 break;
2002 case 'p': port= optarg;
2003 break;
2004 case 'P': prompt= true;
2005 break;
2006 case 'T': testname= optarg;
2007 break;
2008 default:
2009 fprintf(stderr, "Usage: %s [-h hostname] [-p port] [-c] [-v] [-t n] [-P] [-T testname]'\n"
2010 "\t-c\tGenerate coredump if a test fails\n"
2011 "\t-v\tVerbose test output (print out the assertion)\n"
2012 "\t-t n\tSet the timeout for io-operations to n seconds\n"
2013 "\t-P\tPrompt the user before starting a test.\n"
2014 "\t\t\t\"skip\" will skip the test\n"
2015 "\t\t\t\"quit\" will terminate memcapable\n"
2016 "\t\t\tEverything else will start the test\n"
2017 "\t-T n\tJust run the test named n\n"
2018 "\t-a\tOnly test the ascii protocol\n"
2019 "\t-b\tOnly test the binary protocol\n",
2020 argv[0]);
2021 return EXIT_FAILURE;
2022 }
2023 }
2024
2025 initialize_sockets();
2026 sock= connect_server(hostname, port);
2027 if (sock == INVALID_SOCKET)
2028 {
2029 fprintf(stderr, "Failed to connect to <%s:%s>: %s\n",
2030 hostname, port, strerror(get_socket_errno()));
2031 return EXIT_FAILURE;
2032 }
2033
2034 for (int ii= 0; testcases[ii].description != NULL; ++ii)
2035 {
2036 if (testname != NULL && strcmp(testcases[ii].description, testname) != 0)
2037 continue;
2038
2039 if ((testcases[ii].description[0] == 'a' && (tests.ascii) == 0) ||
2040 (testcases[ii].description[0] == 'b' && (tests.binary) == 0))
2041 {
2042 continue;
2043 }
2044 ++total;
2045 fprintf(stdout, "%-40s", testcases[ii].description);
2046 fflush(stdout);
2047
2048 if (prompt)
2049 {
2050 fprintf(stdout, "\nPress <return> when you are ready? ");
2051 char buffer[80] = {0};
2052 if (fgets(buffer, sizeof(buffer), stdin) != NULL) {
2053 if (strncmp(buffer, "skip", 4) == 0)
2054 {
2055 fprintf(stdout, "%-40s%s\n", testcases[ii].description,
2056 status_msg[TEST_SKIP]);
2057 fflush(stdout);
2058 continue;
2059 }
2060 if (strncmp(buffer, "quit", 4) == 0)
2061 exit(0);
2062 }
2063
2064 fprintf(stdout, "%-40s", testcases[ii].description);
2065 fflush(stdout);
2066 }
2067
2068 bool reconnect= false;
2069 enum test_return ret= testcases[ii].function();
2070 if (ret == TEST_FAIL)
2071 {
2072 reconnect= true;
2073 ++failed;
2074 if (verbose)
2075 fprintf(stderr, "\n");
2076 }
2077 else if (ret == TEST_PASS_RECONNECT)
2078 reconnect= true;
2079
2080 fprintf(stderr, "%s\n", status_msg[ret]);
2081 if (reconnect)
2082 {
2083 closesocket(sock);
2084 if ((sock= connect_server(hostname, port)) == INVALID_SOCKET)
2085 {
2086 fprintf(stderr, "Failed to connect to <%s:%s>: %s\n",
2087 hostname, port, strerror(get_socket_errno()));
2088 fprintf(stderr, "%d of %d tests failed\n", failed, total);
2089 return EXIT_FAILURE;
2090 }
2091 }
2092 }
2093
2094 closesocket(sock);
2095 if (failed == 0)
2096 fprintf(stdout, "All tests passed\n");
2097 else
2098 fprintf(stderr, "%d of %d tests failed\n", failed, total);
2099
2100 return (failed == 0) ? 0 : 1;
2101 }