Add memcapable, a tool to test memcached capabilities
[awesomized/libmemcached] / clients / memcapable.c
1 /* -*- Mode: C; tab-width: 2; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 #undef NDEBUG
3 #include <pthread.h>
4 #include <sys/types.h>
5 #include <sys/socket.h>
6 #include <netdb.h>
7 #include <arpa/inet.h>
8 #include <netinet/in.h>
9 #include <netinet/tcp.h>
10 #include <fcntl.h>
11 #include <signal.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <errno.h>
15 #include <assert.h>
16 #include <string.h>
17 #include <inttypes.h>
18 #include <stdbool.h>
19 #include <unistd.h>
20 #include <poll.h>
21
22 #include <libmemcached/memcached/protocol_binary.h>
23 #include <libmemcached/byteorder.h>
24
25 #ifdef linux
26 /* /usr/include/netinet/in.h defines macros from ntohs() to _bswap_nn to
27 * optimize the conversion functions, but the prototypes generate warnings
28 * from gcc. The conversion methods isn't the bottleneck for my app, so
29 * just remove the warnings by undef'ing the optimization ..
30 */
31 #undef ntohs
32 #undef ntohl
33 #endif
34
35 /* Should we generate coredumps when we enounter an error (-c) */
36 static bool do_core= false;
37 /* connection to the server */
38 static int sock;
39 /* Should the output from test failures be verbose or quiet? */
40 static bool verbose= false;
41
42 /* The number of seconds to wait for an IO-operation */
43 static int timeout= 2;
44
45 /*
46 * Instead of having to cast between the different datatypes we create
47 * a union of all of the different types of pacages we want to send.
48 * A lot of the different commands use the same packet layout, so I'll
49 * just define the different types I need. The typedefs only contain
50 * the header of the message, so we need some space for keys and body
51 * To avoid to have to do multiple writes, lets add a chunk of memory
52 * to use. 1k should be more than enough for header, key and body.
53 */
54 typedef union
55 {
56 protocol_binary_request_no_extras plain;
57 protocol_binary_request_flush flush;
58 protocol_binary_request_incr incr;
59 protocol_binary_request_set set;
60 char bytes[1024];
61 } command;
62
63 typedef union
64 {
65 protocol_binary_response_no_extras plain;
66 protocol_binary_response_incr incr;
67 protocol_binary_response_decr decr;
68 char bytes[1024];
69 } response;
70
71 enum test_return
72 {
73 TEST_SKIP, TEST_PASS, TEST_PASS_RECONNECT, TEST_FAIL
74 };
75
76 /**
77 * Try to get an addrinfo struct for a given port on a given host
78 */
79 static struct addrinfo *lookuphost(const char *hostname, const char *port)
80 {
81 struct addrinfo *ai= 0;
82 struct addrinfo hints= {.ai_family=AF_UNSPEC,
83 .ai_protocol=IPPROTO_TCP,
84 .ai_socktype=SOCK_STREAM};
85 int error= getaddrinfo(hostname, port, &hints, &ai);
86
87 if (error != 0)
88 {
89 if (error != EAI_SYSTEM)
90 fprintf(stderr, "getaddrinfo(): %s\n", gai_strerror(error));
91 else
92 perror("getaddrinfo()");
93 }
94
95 return ai;
96 }
97
98 /**
99 * Set the socket in nonblocking mode
100 * @return -1 if failure, the socket otherwise
101 */
102 static int set_noblock(void)
103 {
104 int flags= fcntl(sock, F_GETFL, 0);
105 if (flags == -1)
106 {
107 perror("Failed to get socket flags");
108 close(sock);
109 return -1;
110 }
111
112 if ((flags & O_NONBLOCK) != O_NONBLOCK)
113 {
114 if (fcntl(sock, F_SETFL, flags | O_NONBLOCK) == -1)
115 {
116 perror("Failed to set socket to nonblocking mode");
117 close(sock);
118 return -1;
119 }
120 }
121
122 return sock;
123 }
124
125 /**
126 * Try to open a connection to the server
127 * @param hostname the name of the server to connect to
128 * @param port the port number (or service) to connect to
129 * @return positive integer if success, -1 otherwise
130 */
131 static int connect_server(const char *hostname, const char *port)
132 {
133 struct addrinfo *ai= lookuphost(hostname, port);
134 sock= -1;
135 if (ai != NULL)
136 {
137 if ((sock=socket(ai->ai_family, ai->ai_socktype,
138 ai->ai_protocol)) != -1)
139 {
140 if (connect(sock, ai->ai_addr, ai->ai_addrlen) == -1)
141 {
142 fprintf(stderr, "Failed to connect socket: %s\n",
143 strerror(errno));
144 close(sock);
145 sock= -1;
146 }
147 else
148 {
149 sock= set_noblock();
150 }
151 } else
152 fprintf(stderr, "Failed to create socket: %s\n", strerror(errno));
153
154 freeaddrinfo(ai);
155 }
156
157 return sock;
158 }
159
160 static ssize_t timeout_io_op(int fd, short direction, void *buf, size_t len)
161 {
162 ssize_t ret;
163
164 if (direction == POLLOUT)
165 ret= write(fd, buf, len);
166 else
167 ret= read(fd, buf, len);
168
169 if (ret == -1 && errno == EWOULDBLOCK) {
170 struct pollfd fds = {
171 .events= direction,
172 .fd= fd
173 };
174 int err= poll(&fds, 1, timeout * 1000);
175
176 if (err == 1)
177 {
178 if (direction == POLLOUT)
179 ret= write(fd, buf, len);
180 else
181 ret= read(fd, buf, len);
182 }
183 else if (err == 0)
184 {
185 errno = ETIMEDOUT;
186 }
187 else
188 {
189 perror("Failed to poll");
190 return -1;
191 }
192 }
193
194 return ret;
195 }
196
197 /**
198 * Ensure that an expression is true. If it isn't print out a message similar
199 * to assert() and create a coredump if the user wants that. If not an error
200 * message is returned.
201 *
202 */
203 static enum test_return ensure(bool val, const char *expression, const char *file, int line)
204 {
205 if (!val)
206 {
207 if (verbose)
208 fprintf(stderr, "%s:%u: %s\n", file, line, expression);
209
210 if (do_core)
211 abort();
212
213 return TEST_FAIL;
214 }
215
216 return TEST_PASS;
217 }
218
219 #define verify(expression) do { if (ensure(expression, #expression, __FILE__, __LINE__) == TEST_FAIL) return TEST_FAIL; } while (0)
220 #define execute(expression) do { if (ensure(expression == TEST_PASS, #expression, __FILE__, __LINE__) == TEST_FAIL) return TEST_FAIL; } while (0)
221
222 /**
223 * Send a chunk of memory over the socket (retry if the call is iterrupted
224 */
225 static enum test_return retry_write(const void* buf, size_t len)
226 {
227 size_t offset= 0;
228 const char* ptr= buf;
229
230 do
231 {
232 size_t num_bytes= len - offset;
233 ssize_t nw= timeout_io_op(sock, POLLOUT, (void*)(ptr + offset), num_bytes);
234 if (nw == -1)
235 verify(errno == EINTR || errno == EAGAIN);
236 else
237 offset+= (size_t)nw;
238 } while (offset < len);
239
240 return TEST_PASS;
241 }
242
243 /**
244 * Resend a packet to the server (All fields in the command header should
245 * be in network byte order)
246 */
247 static enum test_return resend_packet(command *cmd)
248 {
249 size_t length= sizeof (protocol_binary_request_no_extras) +
250 ntohl(cmd->plain.message.header.request.bodylen);
251
252 execute(retry_write(cmd, length));
253 return TEST_PASS;
254 }
255
256 /**
257 * Send a command to the server. The command header needs to be updated
258 * to network byte order
259 */
260 static enum test_return send_packet(command *cmd)
261 {
262 /* Fix the byteorder of the header */
263 cmd->plain.message.header.request.keylen=
264 ntohs(cmd->plain.message.header.request.keylen);
265 cmd->plain.message.header.request.bodylen=
266 ntohl(cmd->plain.message.header.request.bodylen);
267 cmd->plain.message.header.request.cas=
268 ntohll(cmd->plain.message.header.request.cas);
269
270 execute(resend_packet(cmd));
271 return TEST_PASS;
272 }
273
274 /**
275 * Read a fixed length chunk of data from the server
276 */
277 static enum test_return retry_read(void *buf, size_t len)
278 {
279 size_t offset= 0;
280 do
281 {
282 ssize_t nr= timeout_io_op(sock, POLLIN, ((char*) buf) + offset, len - offset);
283 switch (nr) {
284 case -1 :
285 verify(errno == EINTR || errno == EAGAIN);
286 break;
287 case 0:
288 return TEST_FAIL;
289 default:
290 offset+= (size_t)nr;
291 }
292 } while (offset < len);
293
294 return TEST_PASS;
295 }
296
297 /**
298 * Receive a response from the server and conver the fields in the header
299 * to local byte order
300 */
301 static enum test_return recv_packet(response *rsp)
302 {
303 execute(retry_read(rsp, sizeof (protocol_binary_response_no_extras)));
304
305 /* Fix the byte order in the packet header */
306 rsp->plain.message.header.response.keylen=
307 ntohs(rsp->plain.message.header.response.keylen);
308 rsp->plain.message.header.response.status=
309 ntohs(rsp->plain.message.header.response.status);
310 rsp->plain.message.header.response.bodylen=
311 ntohl(rsp->plain.message.header.response.bodylen);
312 rsp->plain.message.header.response.cas=
313 ntohll(rsp->plain.message.header.response.cas);
314
315 size_t bodysz= rsp->plain.message.header.response.bodylen;
316 if (bodysz > 0)
317 execute(retry_read(rsp->bytes + sizeof (protocol_binary_response_no_extras), bodysz));
318
319 return TEST_PASS;
320 }
321
322 /**
323 * Create a storage command (add, set, replace etc)
324 *
325 * @param cmd destination buffer
326 * @param cc the storage command to create
327 * @param key the key to store
328 * @param keylen the length of the key
329 * @param dta the data to store with the key
330 * @param dtalen the length of the data to store with the key
331 * @param flags the flags to store along with the key
332 * @param exp the expiry time for the key
333 */
334 static void storage_command(command *cmd,
335 uint8_t cc,
336 const void* key,
337 size_t keylen,
338 const void* dta,
339 size_t dtalen,
340 uint32_t flags,
341 uint32_t exp)
342 {
343 /* all of the storage commands use the same command layout */
344 protocol_binary_request_set *request= &cmd->set;
345
346 memset(request, 0, sizeof (*request));
347 request->message.header.request.magic= PROTOCOL_BINARY_REQ;
348 request->message.header.request.opcode= cc;
349 request->message.header.request.keylen= (uint16_t)keylen;
350 request->message.header.request.extlen= 8;
351 request->message.header.request.bodylen= (uint32_t)(keylen + 8 + dtalen);
352 request->message.header.request.opaque= 0xdeadbeef;
353 request->message.body.flags= flags;
354 request->message.body.expiration= exp;
355
356 off_t key_offset= sizeof (protocol_binary_request_no_extras) + 8;
357 memcpy(cmd->bytes + key_offset, key, keylen);
358 if (dta != NULL)
359 memcpy(cmd->bytes + key_offset + keylen, dta, dtalen);
360 }
361
362 /**
363 * Create a basic command to send to the server
364 * @param cmd destination buffer
365 * @param cc the command to create
366 * @param key the key to store
367 * @param keylen the length of the key
368 * @param dta the data to store with the key
369 * @param dtalen the length of the data to store with the key
370 */
371 static void raw_command(command *cmd,
372 uint8_t cc,
373 const void* key,
374 size_t keylen,
375 const void* dta,
376 size_t dtalen)
377 {
378 /* all of the storage commands use the same command layout */
379 memset(cmd, 0, sizeof (*cmd));
380 cmd->plain.message.header.request.magic= PROTOCOL_BINARY_REQ;
381 cmd->plain.message.header.request.opcode= cc;
382 cmd->plain.message.header.request.keylen= (uint16_t)keylen;
383 cmd->plain.message.header.request.bodylen= (uint32_t)(keylen + dtalen);
384 cmd->plain.message.header.request.opaque= 0xdeadbeef;
385
386 off_t key_offset= sizeof (protocol_binary_request_no_extras);
387
388 if (key != NULL)
389 memcpy(cmd->bytes + key_offset, key, keylen);
390
391 if (dta != NULL)
392 memcpy(cmd->bytes + key_offset + keylen, dta, dtalen);
393 }
394
395 /**
396 * Create the flush command
397 * @param cmd destination buffer
398 * @param cc the command to create (FLUSH/FLUSHQ)
399 * @param exptime when to flush
400 * @param use_extra to force using of the extra field?
401 */
402 static void flush_command(command *cmd,
403 uint8_t cc, uint32_t exptime, bool use_extra)
404 {
405 memset(cmd, 0, sizeof (cmd->flush));
406 cmd->flush.message.header.request.magic= PROTOCOL_BINARY_REQ;
407 cmd->flush.message.header.request.opcode= cc;
408 cmd->flush.message.header.request.opaque= 0xdeadbeef;
409
410 if (exptime != 0 || use_extra)
411 {
412 cmd->flush.message.header.request.extlen= 4;
413 cmd->flush.message.body.expiration= htonl(exptime);
414 cmd->flush.message.header.request.bodylen= 4;
415 }
416 }
417
418 /**
419 * Create a incr/decr command
420 * @param cc the cmd to create (FLUSH/FLUSHQ)
421 * @param key the key to operate on
422 * @param keylen the number of bytes in the key
423 * @param delta the number to add/subtract
424 * @param initial the initial value if the key doesn't exist
425 * @param exp when the key should expire if it isn't set
426 */
427 static void arithmetic_command(command *cmd,
428 uint8_t cc,
429 const void* key,
430 size_t keylen,
431 uint64_t delta,
432 uint64_t initial,
433 uint32_t exp)
434 {
435 memset(cmd, 0, sizeof (cmd->incr));
436 cmd->incr.message.header.request.magic= PROTOCOL_BINARY_REQ;
437 cmd->incr.message.header.request.opcode= cc;
438 cmd->incr.message.header.request.keylen= (uint16_t)keylen;
439 cmd->incr.message.header.request.extlen= 20;
440 cmd->incr.message.header.request.bodylen= (uint32_t)(keylen + 20);
441 cmd->incr.message.header.request.opaque= 0xdeadbeef;
442 cmd->incr.message.body.delta= htonll(delta);
443 cmd->incr.message.body.initial= htonll(initial);
444 cmd->incr.message.body.expiration= htonl(exp);
445
446 off_t key_offset= sizeof (protocol_binary_request_no_extras) + 20;
447 memcpy(cmd->bytes + key_offset, key, keylen);
448 }
449
450 /**
451 * Validate the response header from the server
452 * @param rsp the response to check
453 * @param cc the expected command
454 * @param status the expected status
455 */
456 static enum test_return validate_response_header(response *rsp,
457 uint8_t cc, uint16_t status)
458 {
459 verify(rsp->plain.message.header.response.magic == PROTOCOL_BINARY_RES);
460 verify(rsp->plain.message.header.response.opcode == cc);
461 verify(rsp->plain.message.header.response.datatype == PROTOCOL_BINARY_RAW_BYTES);
462 verify(rsp->plain.message.header.response.status == status);
463 verify(rsp->plain.message.header.response.opaque == 0xdeadbeef);
464
465 if (status == PROTOCOL_BINARY_RESPONSE_SUCCESS)
466 {
467 switch (cc) {
468 case PROTOCOL_BINARY_CMD_ADDQ:
469 case PROTOCOL_BINARY_CMD_APPENDQ:
470 case PROTOCOL_BINARY_CMD_DECREMENTQ:
471 case PROTOCOL_BINARY_CMD_DELETEQ:
472 case PROTOCOL_BINARY_CMD_FLUSHQ:
473 case PROTOCOL_BINARY_CMD_INCREMENTQ:
474 case PROTOCOL_BINARY_CMD_PREPENDQ:
475 case PROTOCOL_BINARY_CMD_QUITQ:
476 case PROTOCOL_BINARY_CMD_REPLACEQ:
477 case PROTOCOL_BINARY_CMD_SETQ:
478 verify("Quiet command shouldn't return on success" == NULL);
479 default:
480 break;
481 }
482
483 switch (cc) {
484 case PROTOCOL_BINARY_CMD_ADD:
485 case PROTOCOL_BINARY_CMD_REPLACE:
486 case PROTOCOL_BINARY_CMD_SET:
487 case PROTOCOL_BINARY_CMD_APPEND:
488 case PROTOCOL_BINARY_CMD_PREPEND:
489 verify(rsp->plain.message.header.response.keylen == 0);
490 verify(rsp->plain.message.header.response.extlen == 0);
491 verify(rsp->plain.message.header.response.bodylen == 0);
492 verify(rsp->plain.message.header.response.cas != 0);
493 break;
494 case PROTOCOL_BINARY_CMD_FLUSH:
495 case PROTOCOL_BINARY_CMD_NOOP:
496 case PROTOCOL_BINARY_CMD_QUIT:
497 case PROTOCOL_BINARY_CMD_DELETE:
498 verify(rsp->plain.message.header.response.keylen == 0);
499 verify(rsp->plain.message.header.response.extlen == 0);
500 verify(rsp->plain.message.header.response.bodylen == 0);
501 verify(rsp->plain.message.header.response.cas == 0);
502 break;
503
504 case PROTOCOL_BINARY_CMD_DECREMENT:
505 case PROTOCOL_BINARY_CMD_INCREMENT:
506 verify(rsp->plain.message.header.response.keylen == 0);
507 verify(rsp->plain.message.header.response.extlen == 0);
508 verify(rsp->plain.message.header.response.bodylen == 8);
509 verify(rsp->plain.message.header.response.cas != 0);
510 break;
511
512 case PROTOCOL_BINARY_CMD_STAT:
513 verify(rsp->plain.message.header.response.extlen == 0);
514 /* key and value exists in all packets except in the terminating */
515 verify(rsp->plain.message.header.response.cas == 0);
516 break;
517
518 case PROTOCOL_BINARY_CMD_VERSION:
519 verify(rsp->plain.message.header.response.keylen == 0);
520 verify(rsp->plain.message.header.response.extlen == 0);
521 verify(rsp->plain.message.header.response.bodylen != 0);
522 verify(rsp->plain.message.header.response.cas == 0);
523 break;
524
525 case PROTOCOL_BINARY_CMD_GET:
526 case PROTOCOL_BINARY_CMD_GETQ:
527 verify(rsp->plain.message.header.response.keylen == 0);
528 verify(rsp->plain.message.header.response.extlen == 4);
529 verify(rsp->plain.message.header.response.cas != 0);
530 break;
531
532 case PROTOCOL_BINARY_CMD_GETK:
533 case PROTOCOL_BINARY_CMD_GETKQ:
534 verify(rsp->plain.message.header.response.keylen != 0);
535 verify(rsp->plain.message.header.response.extlen == 4);
536 verify(rsp->plain.message.header.response.cas != 0);
537 break;
538
539 default:
540 /* Undefined command code */
541 break;
542 }
543 }
544 else
545 {
546 verify(rsp->plain.message.header.response.cas == 0);
547 verify(rsp->plain.message.header.response.extlen == 0);
548 if (cc != PROTOCOL_BINARY_CMD_GETK)
549 {
550 verify(rsp->plain.message.header.response.keylen == 0);
551 }
552 }
553
554 return TEST_PASS;
555 }
556
557 static enum test_return test_binary_noop(void)
558 {
559 command cmd;
560 response rsp;
561 raw_command(&cmd, PROTOCOL_BINARY_CMD_NOOP, NULL, 0, NULL, 0);
562 execute(send_packet(&cmd));
563 execute(recv_packet(&rsp));
564 verify(validate_response_header(&rsp, PROTOCOL_BINARY_CMD_NOOP,
565 PROTOCOL_BINARY_RESPONSE_SUCCESS));
566 return TEST_PASS;
567 }
568
569 static enum test_return test_binary_quit_impl(uint8_t cc)
570 {
571 command cmd;
572 response rsp;
573 raw_command(&cmd, cc, NULL, 0, NULL, 0);
574
575 execute(send_packet(&cmd));
576 if (cc == PROTOCOL_BINARY_CMD_QUIT)
577 {
578 execute(recv_packet(&rsp));
579 verify(validate_response_header(&rsp, PROTOCOL_BINARY_CMD_QUIT,
580 PROTOCOL_BINARY_RESPONSE_SUCCESS));
581 }
582
583 /* Socket should be closed now, read should return 0 */
584 verify(timeout_io_op(sock, POLLIN, rsp.bytes, sizeof(rsp.bytes)) == 0);
585
586 return TEST_PASS_RECONNECT;
587 }
588
589 static enum test_return test_binary_quit(void)
590 {
591 return test_binary_quit_impl(PROTOCOL_BINARY_CMD_QUIT);
592 }
593
594 static enum test_return test_binary_quitq(void)
595 {
596 return test_binary_quit_impl(PROTOCOL_BINARY_CMD_QUITQ);
597 }
598
599 static enum test_return test_binary_set_impl(const char* key, uint8_t cc)
600 {
601 command cmd;
602 response rsp;
603
604 uint64_t value= 0xdeadbeefdeadcafe;
605 storage_command(&cmd, cc, key, strlen(key), &value, sizeof (value), 0, 0);
606
607 /* set should always work */
608 for (int ii= 0; ii < 10; ii++)
609 {
610 if (ii == 0)
611 execute(send_packet(&cmd));
612 else
613 execute(resend_packet(&cmd));
614
615 if (cc == PROTOCOL_BINARY_CMD_SET)
616 {
617 execute(recv_packet(&rsp));
618 verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_SUCCESS));
619 }
620 else
621 execute(test_binary_noop());
622 }
623
624 /* try to set with the correct CAS value */
625 cmd.plain.message.header.request.cas=
626 htonll(rsp.plain.message.header.response.cas);
627 execute(resend_packet(&cmd));
628 if (cc == PROTOCOL_BINARY_CMD_SET)
629 {
630 execute(recv_packet(&rsp));
631 verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_SUCCESS));
632 }
633 else
634 execute(test_binary_noop());
635
636 /* try to set with an incorrect CAS value */
637 cmd.plain.message.header.request.cas=
638 htonll(rsp.plain.message.header.response.cas - 1);
639 execute(resend_packet(&cmd));
640 execute(recv_packet(&rsp));
641 verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS));
642
643 return test_binary_noop();
644 }
645
646 static enum test_return test_binary_set(void)
647 {
648 return test_binary_set_impl("test_binary_set", PROTOCOL_BINARY_CMD_SET);
649 }
650
651 static enum test_return test_binary_setq(void)
652 {
653 return test_binary_set_impl("test_binary_setq", PROTOCOL_BINARY_CMD_SETQ);
654 }
655
656 static enum test_return test_binary_add_impl(const char* key, uint8_t cc)
657 {
658 command cmd;
659 response rsp;
660 uint64_t value= 0xdeadbeefdeadcafe;
661 storage_command(&cmd, cc, key, strlen(key), &value, sizeof (value), 0, 0);
662
663 /* first add should work, rest of them should fail (even with cas
664 as wildcard */
665 for (int ii=0; ii < 10; ii++)
666 {
667 if (ii == 0)
668 execute(send_packet(&cmd));
669 else
670 execute(resend_packet(&cmd));
671
672 if (cc == PROTOCOL_BINARY_CMD_ADD || ii > 0)
673 {
674 uint16_t expected_result;
675 if (ii == 0)
676 expected_result= PROTOCOL_BINARY_RESPONSE_SUCCESS;
677 else
678 expected_result= PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS;
679
680 execute(recv_packet(&rsp));
681 verify(validate_response_header(&rsp, cc, expected_result));
682 }
683 else
684 execute(test_binary_noop());
685 }
686
687 return TEST_PASS;
688 }
689
690 static enum test_return test_binary_add(void)
691 {
692 return test_binary_add_impl("test_binary_add", PROTOCOL_BINARY_CMD_ADD);
693 }
694
695 static enum test_return test_binary_addq(void)
696 {
697 return test_binary_add_impl("test_binary_addq", PROTOCOL_BINARY_CMD_ADDQ);
698 }
699
700 static enum test_return set_item(const char *key, const char *value)
701 {
702 command cmd;
703 response rsp;
704 storage_command(&cmd, PROTOCOL_BINARY_CMD_SET, key, strlen(key),
705 value, strlen(value), 0, 0);
706 execute(send_packet(&cmd));
707 execute(recv_packet(&rsp));
708 verify(validate_response_header(&rsp, PROTOCOL_BINARY_CMD_SET,
709 PROTOCOL_BINARY_RESPONSE_SUCCESS));
710 return TEST_PASS;
711 }
712
713 static enum test_return test_binary_replace_impl(const char* key, uint8_t cc)
714 {
715 command cmd;
716 response rsp;
717 uint64_t value= 0xdeadbeefdeadcafe;
718 storage_command(&cmd, cc, key, strlen(key), &value, sizeof (value), 0, 0);
719
720 /* first replace should fail, successive should succeed (when the
721 item is added! */
722 for (int ii= 0; ii < 10; ii++)
723 {
724 if (ii == 0)
725 execute(send_packet(&cmd));
726 else
727 execute(resend_packet(&cmd));
728
729 if (cc == PROTOCOL_BINARY_CMD_REPLACE || ii == 0)
730 {
731 uint16_t expected_result;
732 if (ii == 0)
733 expected_result=PROTOCOL_BINARY_RESPONSE_KEY_ENOENT;
734 else
735 expected_result=PROTOCOL_BINARY_RESPONSE_SUCCESS;
736
737 execute(recv_packet(&rsp));
738 verify(validate_response_header(&rsp, cc, expected_result));
739
740 if (ii == 0)
741 execute(set_item(key, key));
742 }
743 else
744 execute(test_binary_noop());
745 }
746
747 /* verify that replace with CAS value works! */
748 cmd.plain.message.header.request.cas=
749 htonll(rsp.plain.message.header.response.cas);
750 execute(resend_packet(&cmd));
751
752 if (cc == PROTOCOL_BINARY_CMD_REPLACE)
753 {
754 execute(recv_packet(&rsp));
755 verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_SUCCESS));
756 }
757 else
758 execute(test_binary_noop());
759
760 /* try to set with an incorrect CAS value */
761 cmd.plain.message.header.request.cas=
762 htonll(rsp.plain.message.header.response.cas - 1);
763 execute(resend_packet(&cmd));
764 execute(recv_packet(&rsp));
765 verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS));
766
767 return TEST_PASS;
768 }
769
770 static enum test_return test_binary_replace(void)
771 {
772 return test_binary_replace_impl("test_binary_replace", PROTOCOL_BINARY_CMD_REPLACE);
773 }
774
775 static enum test_return test_binary_replaceq(void)
776 {
777 return test_binary_replace_impl("test_binary_replaceq", PROTOCOL_BINARY_CMD_REPLACEQ);
778 }
779
780 static enum test_return test_binary_delete_impl(const char *key, uint8_t cc)
781 {
782 command cmd;
783 response rsp;
784 raw_command(&cmd, cc, key, strlen(key), NULL, 0);
785
786 /* The delete shouldn't work the first time, because the item isn't there */
787 execute(send_packet(&cmd));
788 execute(recv_packet(&rsp));
789 verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_KEY_ENOENT));
790 execute(set_item(key, key));
791
792 /* The item should be present now, resend*/
793 execute(resend_packet(&cmd));
794 if (cc == PROTOCOL_BINARY_CMD_DELETE)
795 {
796 execute(recv_packet(&rsp));
797 verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_SUCCESS));
798 }
799
800 execute(test_binary_noop());
801
802 return TEST_PASS;
803 }
804
805 static enum test_return test_binary_delete(void)
806 {
807 return test_binary_delete_impl("test_binary_delete", PROTOCOL_BINARY_CMD_DELETE);
808 }
809
810 static enum test_return test_binary_deleteq(void)
811 {
812 return test_binary_delete_impl("test_binary_deleteq", PROTOCOL_BINARY_CMD_DELETEQ);
813 }
814
815 static enum test_return test_binary_get_impl(const char *key, uint8_t cc)
816 {
817 command cmd;
818 response rsp;
819
820 raw_command(&cmd, cc, key, strlen(key), NULL, 0);
821 execute(send_packet(&cmd));
822
823 if (cc == PROTOCOL_BINARY_CMD_GET || cc == PROTOCOL_BINARY_CMD_GETK)
824 {
825 execute(recv_packet(&rsp));
826 verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_KEY_ENOENT));
827 }
828 else
829 execute(test_binary_noop());
830
831 execute(set_item(key, key));
832 execute(resend_packet(&cmd));
833 execute(recv_packet(&rsp));
834 verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_SUCCESS));
835
836 return TEST_PASS;
837 }
838
839 static enum test_return test_binary_get(void)
840 {
841 return test_binary_get_impl("test_binary_get", PROTOCOL_BINARY_CMD_GET);
842 }
843
844 static enum test_return test_binary_getk(void)
845 {
846 return test_binary_get_impl("test_binary_getk", PROTOCOL_BINARY_CMD_GETK);
847 }
848
849 static enum test_return test_binary_getq(void)
850 {
851 return test_binary_get_impl("test_binary_getq", PROTOCOL_BINARY_CMD_GETQ);
852 }
853
854 static enum test_return test_binary_getkq(void)
855 {
856 return test_binary_get_impl("test_binary_getkq", PROTOCOL_BINARY_CMD_GETKQ);
857 }
858
859 static enum test_return test_binary_incr_impl(const char* key, uint8_t cc)
860 {
861 command cmd;
862 response rsp;
863 arithmetic_command(&cmd, cc, key, strlen(key), 1, 0, 0);
864
865 uint64_t ii;
866 for (ii= 0; ii < 10; ++ii)
867 {
868 if (ii == 0)
869 execute(send_packet(&cmd));
870 else
871 execute(resend_packet(&cmd));
872
873 if (cc == PROTOCOL_BINARY_CMD_INCREMENT)
874 {
875 execute(recv_packet(&rsp));
876 verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_SUCCESS));
877 verify(ntohll(rsp.incr.message.body.value) == ii);
878 }
879 else
880 execute(test_binary_noop());
881 }
882
883 /* @todo add incorrect CAS */
884 return TEST_PASS;
885 }
886
887 static enum test_return test_binary_incr(void)
888 {
889 return test_binary_incr_impl("test_binary_incr", PROTOCOL_BINARY_CMD_INCREMENT);
890 }
891
892 static enum test_return test_binary_incrq(void)
893 {
894 return test_binary_incr_impl("test_binary_incrq", PROTOCOL_BINARY_CMD_INCREMENTQ);
895 }
896
897 static enum test_return test_binary_decr_impl(const char* key, uint8_t cc)
898 {
899 command cmd;
900 response rsp;
901 arithmetic_command(&cmd, cc, key, strlen(key), 1, 9, 0);
902
903 int ii;
904 for (ii= 9; ii > -1; --ii)
905 {
906 if (ii == 9)
907 execute(send_packet(&cmd));
908 else
909 execute(resend_packet(&cmd));
910
911 if (cc == PROTOCOL_BINARY_CMD_DECREMENT)
912 {
913 execute(recv_packet(&rsp));
914 verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_SUCCESS));
915 verify(ntohll(rsp.decr.message.body.value) == (uint64_t)ii);
916 }
917 else
918 execute(test_binary_noop());
919 }
920
921 /* decr 0 should not wrap */
922 execute(resend_packet(&cmd));
923 if (cc == PROTOCOL_BINARY_CMD_DECREMENT)
924 {
925 execute(recv_packet(&rsp));
926 verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_SUCCESS));
927 verify(ntohll(rsp.decr.message.body.value) == 0);
928 }
929 else
930 {
931 /* @todo get the value and verify! */
932
933 }
934
935 /* @todo add incorrect cas */
936 execute(test_binary_noop());
937 return TEST_PASS;
938 }
939
940 static enum test_return test_binary_decr(void)
941 {
942 return test_binary_decr_impl("test_binary_decr",
943 PROTOCOL_BINARY_CMD_DECREMENT);
944 }
945
946 static enum test_return test_binary_decrq(void)
947 {
948 return test_binary_decr_impl("test_binary_decrq",
949 PROTOCOL_BINARY_CMD_DECREMENTQ);
950 }
951
952 static enum test_return test_binary_version(void)
953 {
954 command cmd;
955 response rsp;
956 raw_command(&cmd, PROTOCOL_BINARY_CMD_VERSION, NULL, 0, NULL, 0);
957
958 execute(send_packet(&cmd));
959 execute(recv_packet(&rsp));
960 verify(validate_response_header(&rsp, PROTOCOL_BINARY_CMD_VERSION,
961 PROTOCOL_BINARY_RESPONSE_SUCCESS));
962
963 return TEST_PASS;
964 }
965
966 static enum test_return test_binary_flush_impl(const char *key, uint8_t cc)
967 {
968 command cmd;
969 response rsp;
970
971 for (int ii= 0; ii < 2; ++ii)
972 {
973 execute(set_item(key, key));
974 flush_command(&cmd, cc, 0, ii == 0);
975 execute(send_packet(&cmd));
976
977 if (cc == PROTOCOL_BINARY_CMD_FLUSH)
978 {
979 execute(recv_packet(&rsp));
980 verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_SUCCESS));
981 }
982 else
983 execute(test_binary_noop());
984
985 raw_command(&cmd, PROTOCOL_BINARY_CMD_GET, key, strlen(key), NULL, 0);
986 execute(send_packet(&cmd));
987 execute(recv_packet(&rsp));
988 verify(validate_response_header(&rsp, PROTOCOL_BINARY_CMD_GET,
989 PROTOCOL_BINARY_RESPONSE_KEY_ENOENT));
990 }
991
992 return TEST_PASS;
993 }
994
995 static enum test_return test_binary_flush(void)
996 {
997 return test_binary_flush_impl("test_binary_flush", PROTOCOL_BINARY_CMD_FLUSH);
998 }
999
1000 static enum test_return test_binary_flushq(void)
1001 {
1002 return test_binary_flush_impl("test_binary_flushq", PROTOCOL_BINARY_CMD_FLUSHQ);
1003 }
1004
1005 static enum test_return test_binary_concat_impl(const char *key, uint8_t cc)
1006 {
1007 command cmd;
1008 response rsp;
1009 const char *value;
1010
1011 if (cc == PROTOCOL_BINARY_CMD_APPEND || cc == PROTOCOL_BINARY_CMD_APPENDQ)
1012 value="hello";
1013 else
1014 value=" world";
1015
1016 execute(set_item(key, value));
1017
1018 if (cc == PROTOCOL_BINARY_CMD_APPEND || cc == PROTOCOL_BINARY_CMD_APPENDQ)
1019 value=" world";
1020 else
1021 value="hello";
1022
1023 raw_command(&cmd, cc, key, strlen(key), value, strlen(value));
1024 execute(send_packet(&cmd));
1025 if (cc == PROTOCOL_BINARY_CMD_APPEND || cc == PROTOCOL_BINARY_CMD_PREPEND)
1026 {
1027 execute(recv_packet(&rsp));
1028 verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_SUCCESS));
1029 }
1030 else
1031 execute(test_binary_noop());
1032
1033 raw_command(&cmd, PROTOCOL_BINARY_CMD_GET, key, strlen(key), NULL, 0);
1034 execute(send_packet(&cmd));
1035 execute(recv_packet(&rsp));
1036 verify(validate_response_header(&rsp, PROTOCOL_BINARY_CMD_GET,
1037 PROTOCOL_BINARY_RESPONSE_SUCCESS));
1038 verify(rsp.plain.message.header.response.bodylen - 4 == 11);
1039 verify(memcmp(rsp.bytes + 28, "hello world", 11) == 0);
1040
1041 return TEST_PASS;
1042 }
1043
1044 static enum test_return test_binary_append(void)
1045 {
1046 return test_binary_concat_impl("test_binary_append", PROTOCOL_BINARY_CMD_APPEND);
1047 }
1048
1049 static enum test_return test_binary_prepend(void)
1050 {
1051 return test_binary_concat_impl("test_binary_prepend", PROTOCOL_BINARY_CMD_PREPEND);
1052 }
1053
1054 static enum test_return test_binary_appendq(void)
1055 {
1056 return test_binary_concat_impl("test_binary_appendq", PROTOCOL_BINARY_CMD_APPENDQ);
1057 }
1058
1059 static enum test_return test_binary_prependq(void)
1060 {
1061 return test_binary_concat_impl("test_binary_prependq", PROTOCOL_BINARY_CMD_PREPENDQ);
1062 }
1063
1064 static enum test_return test_binary_stat(void)
1065 {
1066 command cmd;
1067 response rsp;
1068
1069 raw_command(&cmd, PROTOCOL_BINARY_CMD_STAT, NULL, 0, NULL, 0);
1070 execute(send_packet(&cmd));
1071
1072 do
1073 {
1074 execute(recv_packet(&rsp));
1075 verify(validate_response_header(&rsp, PROTOCOL_BINARY_CMD_STAT,
1076 PROTOCOL_BINARY_RESPONSE_SUCCESS));
1077 } while (rsp.plain.message.header.response.keylen != 0);
1078
1079 return TEST_PASS;
1080 }
1081
1082 static enum test_return test_binary_illegal(void)
1083 {
1084 command cmd;
1085 response rsp;
1086 uint8_t cc= 0x1b;
1087
1088 while (cc != 0x00)
1089 {
1090 raw_command(&cmd, cc, NULL, 0, NULL, 0);
1091 execute(send_packet(&cmd));
1092 execute(recv_packet(&rsp));
1093 verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_UNKNOWN_COMMAND));
1094 ++cc;
1095 }
1096
1097 return TEST_PASS;
1098 }
1099
1100 typedef enum test_return(*TEST_FUNC)(void);
1101
1102 struct testcase
1103 {
1104 const char *description;
1105 TEST_FUNC function;
1106 };
1107
1108 struct testcase testcases[]= {
1109 { "noop", test_binary_noop},
1110 { "quit", test_binary_quit},
1111 { "quitq", test_binary_quitq},
1112 { "set", test_binary_set},
1113 { "setq", test_binary_setq},
1114 { "flush", test_binary_flush},
1115 { "flushq", test_binary_flushq},
1116 { "add", test_binary_add},
1117 { "addq", test_binary_addq},
1118 { "replace", test_binary_replace},
1119 { "replaceq", test_binary_replaceq},
1120 { "delete", test_binary_delete},
1121 { "deleteq", test_binary_deleteq},
1122 { "get", test_binary_get},
1123 { "getq", test_binary_getq},
1124 { "getk", test_binary_getk},
1125 { "getkq", test_binary_getkq},
1126 { "incr", test_binary_incr},
1127 { "incrq", test_binary_incrq},
1128 { "decr", test_binary_decr},
1129 { "decrq", test_binary_decrq},
1130 { "version", test_binary_version},
1131 { "append", test_binary_append},
1132 { "appendq", test_binary_appendq},
1133 { "prepend", test_binary_prepend},
1134 { "prependq", test_binary_prependq},
1135 { "stat", test_binary_stat},
1136 { "illegal", test_binary_illegal},
1137 { NULL, NULL}
1138 };
1139
1140 int main(int argc, char **argv)
1141 {
1142 static const char * const status_msg[]= {"[skip]", "[pass]", "[pass]", "[FAIL]"};
1143 int total= 0;
1144 int failed= 0;
1145 const char *hostname= "localhost";
1146 const char *port= "11211";
1147 int cmd;
1148
1149 while ((cmd= getopt(argc, argv, "t:vch:p:?")) != EOF)
1150 {
1151 switch (cmd) {
1152 case 't':
1153 timeout= atoi(optarg);
1154 if (timeout == 0)
1155 {
1156 fprintf(stderr, "Invalid timeout. Please specify a number for -t\n");
1157 return 1;
1158 }
1159 break;
1160 case 'v': verbose= true;
1161 break;
1162 case 'c': do_core= true;
1163 break;
1164 case 'h': hostname= optarg;
1165 break;
1166 case 'p': port= optarg;
1167 break;
1168 default:
1169 fprintf(stderr, "Usage: %s [-h hostname] [-p port] [-c] [-v] [-t n]\n"
1170 "\t-c\tGenerate coredump if a test fails\n"
1171 "\t-v\tVerbose test output (print out the assertion)\n"
1172 "\t-c n\tSet the timeout for io-operations to n seconds\n",
1173 argv[0]);
1174 return 1;
1175 }
1176 }
1177
1178 sock= connect_server(hostname, port);
1179 if (sock == -1)
1180 {
1181 fprintf(stderr, "Failed to connect to <%s:%s>: %s\n",
1182 hostname, port, strerror(errno));
1183 return 1;
1184 }
1185
1186 for (int ii= 0; testcases[ii].description != NULL; ++ii)
1187 {
1188 ++total;
1189 fprintf(stdout, "%s\t\t", testcases[ii].description);
1190 fflush(stdout);
1191
1192 bool reconnect= false;
1193 enum test_return ret= testcases[ii].function();
1194 fprintf(stderr, "%s\n", status_msg[ret]);
1195 if (ret == TEST_FAIL)
1196 {
1197 reconnect= true;
1198 ++failed;
1199 }
1200 else if (ret == TEST_PASS_RECONNECT)
1201 reconnect= true;
1202
1203 if (reconnect)
1204 {
1205 (void) close(sock);
1206 if ((sock=connect_server(hostname, port)) == -1)
1207 {
1208 fprintf(stderr, "Failed to connect to <%s:%s>: %s\n",
1209 hostname, port, strerror(errno));
1210 fprintf(stderr, "%d of %d tests failed\n", failed, total);
1211 return 1;
1212 }
1213 }
1214 }
1215
1216 (void) close(sock);
1217 if (failed == 0)
1218 fprintf(stdout, "All tests passed\n");
1219 else
1220 fprintf(stderr, "%d of %d tests failed\n", failed, total);
1221
1222 return (failed == 0) ? 0 : 1;
1223 }