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