7a2b514c27d401ddae78c82cfd09a50b9b1e06d4
[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 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 static enum test_return test_binary_noop(void)
559 {
560 command cmd;
561 response rsp;
562 raw_command(&cmd, PROTOCOL_BINARY_CMD_NOOP, NULL, 0, NULL, 0);
563 execute(send_packet(&cmd));
564 execute(recv_packet(&rsp));
565 verify(validate_response_header(&rsp, PROTOCOL_BINARY_CMD_NOOP,
566 PROTOCOL_BINARY_RESPONSE_SUCCESS));
567 return TEST_PASS;
568 }
569
570 static enum test_return test_binary_quit_impl(uint8_t cc)
571 {
572 command cmd;
573 response rsp;
574 raw_command(&cmd, cc, NULL, 0, NULL, 0);
575
576 execute(send_packet(&cmd));
577 if (cc == PROTOCOL_BINARY_CMD_QUIT)
578 {
579 execute(recv_packet(&rsp));
580 verify(validate_response_header(&rsp, PROTOCOL_BINARY_CMD_QUIT,
581 PROTOCOL_BINARY_RESPONSE_SUCCESS));
582 }
583
584 /* Socket should be closed now, read should return 0 */
585 verify(timeout_io_op(sock, POLLIN, rsp.bytes, sizeof(rsp.bytes)) == 0);
586
587 return TEST_PASS_RECONNECT;
588 }
589
590 static enum test_return test_binary_quit(void)
591 {
592 return test_binary_quit_impl(PROTOCOL_BINARY_CMD_QUIT);
593 }
594
595 static enum test_return test_binary_quitq(void)
596 {
597 return test_binary_quit_impl(PROTOCOL_BINARY_CMD_QUITQ);
598 }
599
600 static enum test_return test_binary_set_impl(const char* key, uint8_t cc)
601 {
602 command cmd;
603 response rsp;
604
605 uint64_t value= 0xdeadbeefdeadcafe;
606 storage_command(&cmd, cc, key, strlen(key), &value, sizeof (value), 0, 0);
607
608 /* set should always work */
609 for (int ii= 0; ii < 10; ii++)
610 {
611 if (ii == 0)
612 execute(send_packet(&cmd));
613 else
614 execute(resend_packet(&cmd));
615
616 if (cc == PROTOCOL_BINARY_CMD_SET)
617 {
618 execute(recv_packet(&rsp));
619 verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_SUCCESS));
620 }
621 else
622 execute(test_binary_noop());
623 }
624
625 /* try to set with the correct CAS value */
626 cmd.plain.message.header.request.cas=
627 htonll(rsp.plain.message.header.response.cas);
628 execute(resend_packet(&cmd));
629 if (cc == PROTOCOL_BINARY_CMD_SET)
630 {
631 execute(recv_packet(&rsp));
632 verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_SUCCESS));
633 }
634 else
635 execute(test_binary_noop());
636
637 /* try to set with an incorrect CAS value */
638 cmd.plain.message.header.request.cas=
639 htonll(rsp.plain.message.header.response.cas - 1);
640 execute(resend_packet(&cmd));
641 execute(recv_packet(&rsp));
642 verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS));
643
644 return test_binary_noop();
645 }
646
647 static enum test_return test_binary_set(void)
648 {
649 return test_binary_set_impl("test_binary_set", PROTOCOL_BINARY_CMD_SET);
650 }
651
652 static enum test_return test_binary_setq(void)
653 {
654 return test_binary_set_impl("test_binary_setq", PROTOCOL_BINARY_CMD_SETQ);
655 }
656
657 static enum test_return test_binary_add_impl(const char* key, uint8_t cc)
658 {
659 command cmd;
660 response rsp;
661 uint64_t value= 0xdeadbeefdeadcafe;
662 storage_command(&cmd, cc, key, strlen(key), &value, sizeof (value), 0, 0);
663
664 /* first add should work, rest of them should fail (even with cas
665 as wildcard */
666 for (int ii=0; ii < 10; ii++)
667 {
668 if (ii == 0)
669 execute(send_packet(&cmd));
670 else
671 execute(resend_packet(&cmd));
672
673 if (cc == PROTOCOL_BINARY_CMD_ADD || ii > 0)
674 {
675 uint16_t expected_result;
676 if (ii == 0)
677 expected_result= PROTOCOL_BINARY_RESPONSE_SUCCESS;
678 else
679 expected_result= PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS;
680
681 execute(recv_packet(&rsp));
682 verify(validate_response_header(&rsp, cc, expected_result));
683 }
684 else
685 execute(test_binary_noop());
686 }
687
688 return TEST_PASS;
689 }
690
691 static enum test_return test_binary_add(void)
692 {
693 return test_binary_add_impl("test_binary_add", PROTOCOL_BINARY_CMD_ADD);
694 }
695
696 static enum test_return test_binary_addq(void)
697 {
698 return test_binary_add_impl("test_binary_addq", PROTOCOL_BINARY_CMD_ADDQ);
699 }
700
701 static enum test_return set_item(const char *key, const char *value)
702 {
703 command cmd;
704 response rsp;
705 storage_command(&cmd, PROTOCOL_BINARY_CMD_SET, key, strlen(key),
706 value, strlen(value), 0, 0);
707 execute(send_packet(&cmd));
708 execute(recv_packet(&rsp));
709 verify(validate_response_header(&rsp, PROTOCOL_BINARY_CMD_SET,
710 PROTOCOL_BINARY_RESPONSE_SUCCESS));
711 return TEST_PASS;
712 }
713
714 static enum test_return test_binary_replace_impl(const char* key, uint8_t cc)
715 {
716 command cmd;
717 response rsp;
718 uint64_t value= 0xdeadbeefdeadcafe;
719 storage_command(&cmd, cc, key, strlen(key), &value, sizeof (value), 0, 0);
720
721 /* first replace should fail, successive should succeed (when the
722 item is added! */
723 for (int ii= 0; ii < 10; ii++)
724 {
725 if (ii == 0)
726 execute(send_packet(&cmd));
727 else
728 execute(resend_packet(&cmd));
729
730 if (cc == PROTOCOL_BINARY_CMD_REPLACE || ii == 0)
731 {
732 uint16_t expected_result;
733 if (ii == 0)
734 expected_result=PROTOCOL_BINARY_RESPONSE_KEY_ENOENT;
735 else
736 expected_result=PROTOCOL_BINARY_RESPONSE_SUCCESS;
737
738 execute(recv_packet(&rsp));
739 verify(validate_response_header(&rsp, cc, expected_result));
740
741 if (ii == 0)
742 execute(set_item(key, key));
743 }
744 else
745 execute(test_binary_noop());
746 }
747
748 /* verify that replace with CAS value works! */
749 cmd.plain.message.header.request.cas=
750 htonll(rsp.plain.message.header.response.cas);
751 execute(resend_packet(&cmd));
752
753 if (cc == PROTOCOL_BINARY_CMD_REPLACE)
754 {
755 execute(recv_packet(&rsp));
756 verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_SUCCESS));
757 }
758 else
759 execute(test_binary_noop());
760
761 /* try to set with an incorrect CAS value */
762 cmd.plain.message.header.request.cas=
763 htonll(rsp.plain.message.header.response.cas - 1);
764 execute(resend_packet(&cmd));
765 execute(recv_packet(&rsp));
766 verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS));
767
768 return TEST_PASS;
769 }
770
771 static enum test_return test_binary_replace(void)
772 {
773 return test_binary_replace_impl("test_binary_replace", PROTOCOL_BINARY_CMD_REPLACE);
774 }
775
776 static enum test_return test_binary_replaceq(void)
777 {
778 return test_binary_replace_impl("test_binary_replaceq", PROTOCOL_BINARY_CMD_REPLACEQ);
779 }
780
781 static enum test_return test_binary_delete_impl(const char *key, uint8_t cc)
782 {
783 command cmd;
784 response rsp;
785 raw_command(&cmd, cc, key, strlen(key), NULL, 0);
786
787 /* The delete shouldn't work the first time, because the item isn't there */
788 execute(send_packet(&cmd));
789 execute(recv_packet(&rsp));
790 verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_KEY_ENOENT));
791 execute(set_item(key, key));
792
793 /* The item should be present now, resend*/
794 execute(resend_packet(&cmd));
795 if (cc == PROTOCOL_BINARY_CMD_DELETE)
796 {
797 execute(recv_packet(&rsp));
798 verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_SUCCESS));
799 }
800
801 execute(test_binary_noop());
802
803 return TEST_PASS;
804 }
805
806 static enum test_return test_binary_delete(void)
807 {
808 return test_binary_delete_impl("test_binary_delete", PROTOCOL_BINARY_CMD_DELETE);
809 }
810
811 static enum test_return test_binary_deleteq(void)
812 {
813 return test_binary_delete_impl("test_binary_deleteq", PROTOCOL_BINARY_CMD_DELETEQ);
814 }
815
816 static enum test_return test_binary_get_impl(const char *key, uint8_t cc)
817 {
818 command cmd;
819 response rsp;
820
821 raw_command(&cmd, cc, key, strlen(key), NULL, 0);
822 execute(send_packet(&cmd));
823
824 if (cc == PROTOCOL_BINARY_CMD_GET || cc == PROTOCOL_BINARY_CMD_GETK)
825 {
826 execute(recv_packet(&rsp));
827 verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_KEY_ENOENT));
828 }
829 else
830 execute(test_binary_noop());
831
832 execute(set_item(key, key));
833 execute(resend_packet(&cmd));
834 execute(recv_packet(&rsp));
835 verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_SUCCESS));
836
837 return TEST_PASS;
838 }
839
840 static enum test_return test_binary_get(void)
841 {
842 return test_binary_get_impl("test_binary_get", PROTOCOL_BINARY_CMD_GET);
843 }
844
845 static enum test_return test_binary_getk(void)
846 {
847 return test_binary_get_impl("test_binary_getk", PROTOCOL_BINARY_CMD_GETK);
848 }
849
850 static enum test_return test_binary_getq(void)
851 {
852 return test_binary_get_impl("test_binary_getq", PROTOCOL_BINARY_CMD_GETQ);
853 }
854
855 static enum test_return test_binary_getkq(void)
856 {
857 return test_binary_get_impl("test_binary_getkq", PROTOCOL_BINARY_CMD_GETKQ);
858 }
859
860 static enum test_return test_binary_incr_impl(const char* key, uint8_t cc)
861 {
862 command cmd;
863 response rsp;
864 arithmetic_command(&cmd, cc, key, strlen(key), 1, 0, 0);
865
866 uint64_t ii;
867 for (ii= 0; ii < 10; ++ii)
868 {
869 if (ii == 0)
870 execute(send_packet(&cmd));
871 else
872 execute(resend_packet(&cmd));
873
874 if (cc == PROTOCOL_BINARY_CMD_INCREMENT)
875 {
876 execute(recv_packet(&rsp));
877 verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_SUCCESS));
878 verify(ntohll(rsp.incr.message.body.value) == ii);
879 }
880 else
881 execute(test_binary_noop());
882 }
883
884 /* @todo add incorrect CAS */
885 return TEST_PASS;
886 }
887
888 static enum test_return test_binary_incr(void)
889 {
890 return test_binary_incr_impl("test_binary_incr", PROTOCOL_BINARY_CMD_INCREMENT);
891 }
892
893 static enum test_return test_binary_incrq(void)
894 {
895 return test_binary_incr_impl("test_binary_incrq", PROTOCOL_BINARY_CMD_INCREMENTQ);
896 }
897
898 static enum test_return test_binary_decr_impl(const char* key, uint8_t cc)
899 {
900 command cmd;
901 response rsp;
902 arithmetic_command(&cmd, cc, key, strlen(key), 1, 9, 0);
903
904 int ii;
905 for (ii= 9; ii > -1; --ii)
906 {
907 if (ii == 9)
908 execute(send_packet(&cmd));
909 else
910 execute(resend_packet(&cmd));
911
912 if (cc == PROTOCOL_BINARY_CMD_DECREMENT)
913 {
914 execute(recv_packet(&rsp));
915 verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_SUCCESS));
916 verify(ntohll(rsp.decr.message.body.value) == (uint64_t)ii);
917 }
918 else
919 execute(test_binary_noop());
920 }
921
922 /* decr 0 should not wrap */
923 execute(resend_packet(&cmd));
924 if (cc == PROTOCOL_BINARY_CMD_DECREMENT)
925 {
926 execute(recv_packet(&rsp));
927 verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_SUCCESS));
928 verify(ntohll(rsp.decr.message.body.value) == 0);
929 }
930 else
931 {
932 /* @todo get the value and verify! */
933
934 }
935
936 /* @todo add incorrect cas */
937 execute(test_binary_noop());
938 return TEST_PASS;
939 }
940
941 static enum test_return test_binary_decr(void)
942 {
943 return test_binary_decr_impl("test_binary_decr",
944 PROTOCOL_BINARY_CMD_DECREMENT);
945 }
946
947 static enum test_return test_binary_decrq(void)
948 {
949 return test_binary_decr_impl("test_binary_decrq",
950 PROTOCOL_BINARY_CMD_DECREMENTQ);
951 }
952
953 static enum test_return test_binary_version(void)
954 {
955 command cmd;
956 response rsp;
957 raw_command(&cmd, PROTOCOL_BINARY_CMD_VERSION, NULL, 0, NULL, 0);
958
959 execute(send_packet(&cmd));
960 execute(recv_packet(&rsp));
961 verify(validate_response_header(&rsp, PROTOCOL_BINARY_CMD_VERSION,
962 PROTOCOL_BINARY_RESPONSE_SUCCESS));
963
964 return TEST_PASS;
965 }
966
967 static enum test_return test_binary_flush_impl(const char *key, uint8_t cc)
968 {
969 command cmd;
970 response rsp;
971
972 for (int ii= 0; ii < 2; ++ii)
973 {
974 execute(set_item(key, key));
975 flush_command(&cmd, cc, 0, ii == 0);
976 execute(send_packet(&cmd));
977
978 if (cc == PROTOCOL_BINARY_CMD_FLUSH)
979 {
980 execute(recv_packet(&rsp));
981 verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_SUCCESS));
982 }
983 else
984 execute(test_binary_noop());
985
986 raw_command(&cmd, PROTOCOL_BINARY_CMD_GET, key, strlen(key), NULL, 0);
987 execute(send_packet(&cmd));
988 execute(recv_packet(&rsp));
989 verify(validate_response_header(&rsp, PROTOCOL_BINARY_CMD_GET,
990 PROTOCOL_BINARY_RESPONSE_KEY_ENOENT));
991 }
992
993 return TEST_PASS;
994 }
995
996 static enum test_return test_binary_flush(void)
997 {
998 return test_binary_flush_impl("test_binary_flush", PROTOCOL_BINARY_CMD_FLUSH);
999 }
1000
1001 static enum test_return test_binary_flushq(void)
1002 {
1003 return test_binary_flush_impl("test_binary_flushq", PROTOCOL_BINARY_CMD_FLUSHQ);
1004 }
1005
1006 static enum test_return test_binary_concat_impl(const char *key, uint8_t cc)
1007 {
1008 command cmd;
1009 response rsp;
1010 const char *value;
1011
1012 if (cc == PROTOCOL_BINARY_CMD_APPEND || cc == PROTOCOL_BINARY_CMD_APPENDQ)
1013 value="hello";
1014 else
1015 value=" world";
1016
1017 execute(set_item(key, value));
1018
1019 if (cc == PROTOCOL_BINARY_CMD_APPEND || cc == PROTOCOL_BINARY_CMD_APPENDQ)
1020 value=" world";
1021 else
1022 value="hello";
1023
1024 raw_command(&cmd, cc, key, strlen(key), value, strlen(value));
1025 execute(send_packet(&cmd));
1026 if (cc == PROTOCOL_BINARY_CMD_APPEND || cc == PROTOCOL_BINARY_CMD_PREPEND)
1027 {
1028 execute(recv_packet(&rsp));
1029 verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_SUCCESS));
1030 }
1031 else
1032 execute(test_binary_noop());
1033
1034 raw_command(&cmd, PROTOCOL_BINARY_CMD_GET, key, strlen(key), NULL, 0);
1035 execute(send_packet(&cmd));
1036 execute(recv_packet(&rsp));
1037 verify(validate_response_header(&rsp, PROTOCOL_BINARY_CMD_GET,
1038 PROTOCOL_BINARY_RESPONSE_SUCCESS));
1039 verify(rsp.plain.message.header.response.bodylen - 4 == 11);
1040 verify(memcmp(rsp.bytes + 28, "hello world", 11) == 0);
1041
1042 return TEST_PASS;
1043 }
1044
1045 static enum test_return test_binary_append(void)
1046 {
1047 return test_binary_concat_impl("test_binary_append", PROTOCOL_BINARY_CMD_APPEND);
1048 }
1049
1050 static enum test_return test_binary_prepend(void)
1051 {
1052 return test_binary_concat_impl("test_binary_prepend", PROTOCOL_BINARY_CMD_PREPEND);
1053 }
1054
1055 static enum test_return test_binary_appendq(void)
1056 {
1057 return test_binary_concat_impl("test_binary_appendq", PROTOCOL_BINARY_CMD_APPENDQ);
1058 }
1059
1060 static enum test_return test_binary_prependq(void)
1061 {
1062 return test_binary_concat_impl("test_binary_prependq", PROTOCOL_BINARY_CMD_PREPENDQ);
1063 }
1064
1065 static enum test_return test_binary_stat(void)
1066 {
1067 command cmd;
1068 response rsp;
1069
1070 raw_command(&cmd, PROTOCOL_BINARY_CMD_STAT, NULL, 0, NULL, 0);
1071 execute(send_packet(&cmd));
1072
1073 do
1074 {
1075 execute(recv_packet(&rsp));
1076 verify(validate_response_header(&rsp, PROTOCOL_BINARY_CMD_STAT,
1077 PROTOCOL_BINARY_RESPONSE_SUCCESS));
1078 } while (rsp.plain.message.header.response.keylen != 0);
1079
1080 return TEST_PASS;
1081 }
1082
1083 static enum test_return test_binary_illegal(void)
1084 {
1085 command cmd;
1086 response rsp;
1087 uint8_t cc= 0x1b;
1088
1089 while (cc != 0x00)
1090 {
1091 raw_command(&cmd, cc, NULL, 0, NULL, 0);
1092 execute(send_packet(&cmd));
1093 execute(recv_packet(&rsp));
1094 verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_UNKNOWN_COMMAND));
1095 ++cc;
1096 }
1097
1098 return TEST_PASS;
1099 }
1100
1101 typedef enum test_return(*TEST_FUNC)(void);
1102
1103 struct testcase
1104 {
1105 const char *description;
1106 TEST_FUNC function;
1107 };
1108
1109 struct testcase testcases[]= {
1110 { "noop", test_binary_noop},
1111 { "quit", test_binary_quit},
1112 { "quitq", test_binary_quitq},
1113 { "set", test_binary_set},
1114 { "setq", test_binary_setq},
1115 { "flush", test_binary_flush},
1116 { "flushq", test_binary_flushq},
1117 { "add", test_binary_add},
1118 { "addq", test_binary_addq},
1119 { "replace", test_binary_replace},
1120 { "replaceq", test_binary_replaceq},
1121 { "delete", test_binary_delete},
1122 { "deleteq", test_binary_deleteq},
1123 { "get", test_binary_get},
1124 { "getq", test_binary_getq},
1125 { "getk", test_binary_getk},
1126 { "getkq", test_binary_getkq},
1127 { "incr", test_binary_incr},
1128 { "incrq", test_binary_incrq},
1129 { "decr", test_binary_decr},
1130 { "decrq", test_binary_decrq},
1131 { "version", test_binary_version},
1132 { "append", test_binary_append},
1133 { "appendq", test_binary_appendq},
1134 { "prepend", test_binary_prepend},
1135 { "prependq", test_binary_prependq},
1136 { "stat", test_binary_stat},
1137 { "illegal", test_binary_illegal},
1138 { NULL, NULL}
1139 };
1140
1141 int main(int argc, char **argv)
1142 {
1143 static const char * const status_msg[]= {"[skip]", "[pass]", "[pass]", "[FAIL]"};
1144 int total= 0;
1145 int failed= 0;
1146 const char *hostname= "localhost";
1147 const char *port= "11211";
1148 int cmd;
1149
1150 while ((cmd= getopt(argc, argv, "t:vch:p:?")) != EOF)
1151 {
1152 switch (cmd) {
1153 case 't':
1154 timeout= atoi(optarg);
1155 if (timeout == 0)
1156 {
1157 fprintf(stderr, "Invalid timeout. Please specify a number for -t\n");
1158 return 1;
1159 }
1160 break;
1161 case 'v': verbose= true;
1162 break;
1163 case 'c': do_core= true;
1164 break;
1165 case 'h': hostname= optarg;
1166 break;
1167 case 'p': port= optarg;
1168 break;
1169 default:
1170 fprintf(stderr, "Usage: %s [-h hostname] [-p port] [-c] [-v] [-t n]\n"
1171 "\t-c\tGenerate coredump if a test fails\n"
1172 "\t-v\tVerbose test output (print out the assertion)\n"
1173 "\t-c n\tSet the timeout for io-operations to n seconds\n",
1174 argv[0]);
1175 return 1;
1176 }
1177 }
1178
1179 sock= connect_server(hostname, port);
1180 if (sock == -1)
1181 {
1182 fprintf(stderr, "Failed to connect to <%s:%s>: %s\n",
1183 hostname, port, strerror(errno));
1184 return 1;
1185 }
1186
1187 for (int ii= 0; testcases[ii].description != NULL; ++ii)
1188 {
1189 ++total;
1190 fprintf(stdout, "%s\t\t", testcases[ii].description);
1191 fflush(stdout);
1192
1193 bool reconnect= false;
1194 enum test_return ret= testcases[ii].function();
1195 fprintf(stderr, "%s\n", status_msg[ret]);
1196 if (ret == TEST_FAIL)
1197 {
1198 reconnect= true;
1199 ++failed;
1200 }
1201 else if (ret == TEST_PASS_RECONNECT)
1202 reconnect= true;
1203
1204 if (reconnect)
1205 {
1206 (void) close(sock);
1207 if ((sock=connect_server(hostname, port)) == -1)
1208 {
1209 fprintf(stderr, "Failed to connect to <%s:%s>: %s\n",
1210 hostname, port, strerror(errno));
1211 fprintf(stderr, "%d of %d tests failed\n", failed, total);
1212 return 1;
1213 }
1214 }
1215 }
1216
1217 (void) close(sock);
1218 if (failed == 0)
1219 fprintf(stdout, "All tests passed\n");
1220 else
1221 fprintf(stderr, "%d of %d tests failed\n", failed, total);
1222
1223 return (failed == 0) ? 0 : 1;
1224 }