1 /* -*- Mode: C; tab-width: 2; c-basic-offset: 2; indent-tabs-mode: nil -*- */
5 #include <sys/socket.h>
8 #include <netinet/in.h>
9 #include <netinet/tcp.h>
22 #include <libmemcached/memcached/protocol_binary.h>
23 #include <libmemcached/byteorder.h>
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 ..
35 /* Should we generate coredumps when we enounter an error (-c) */
36 static bool do_core
= false;
37 /* connection to the server */
39 /* Should the output from test failures be verbose or quiet? */
40 static bool verbose
= false;
42 /* The number of seconds to wait for an IO-operation */
43 static int timeout
= 2;
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.
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
;
65 protocol_binary_response_no_extras plain
;
66 protocol_binary_response_incr incr
;
67 protocol_binary_response_decr decr
;
73 TEST_SKIP
, TEST_PASS
, TEST_PASS_RECONNECT
, TEST_FAIL
77 * Try to get an addrinfo struct for a given port on a given host
79 static struct addrinfo
*lookuphost(const char *hostname
, const char *port
)
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
);
89 if (error
!= EAI_SYSTEM
)
90 fprintf(stderr
, "getaddrinfo(): %s\n", gai_strerror(error
));
92 perror("getaddrinfo()");
99 * Set the socket in nonblocking mode
100 * @return -1 if failure, the socket otherwise
102 static int set_noblock(void)
104 int flags
= fcntl(sock
, F_GETFL
, 0);
107 perror("Failed to get socket flags");
112 if ((flags
& O_NONBLOCK
) != O_NONBLOCK
)
114 if (fcntl(sock
, F_SETFL
, flags
| O_NONBLOCK
) == -1)
116 perror("Failed to set socket to nonblocking mode");
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
131 static int connect_server(const char *hostname
, const char *port
)
133 struct addrinfo
*ai
= lookuphost(hostname
, port
);
137 if ((sock
=socket(ai
->ai_family
, ai
->ai_socktype
,
138 ai
->ai_protocol
)) != -1)
140 if (connect(sock
, ai
->ai_addr
, ai
->ai_addrlen
) == -1)
142 fprintf(stderr
, "Failed to connect socket: %s\n",
152 fprintf(stderr
, "Failed to create socket: %s\n", strerror(errno
));
160 static ssize_t
timeout_io_op(int fd
, short direction
, void *buf
, size_t len
)
164 if (direction
== POLLOUT
)
165 ret
= write(fd
, buf
, len
);
167 ret
= read(fd
, buf
, len
);
169 if (ret
== -1 && errno
== EWOULDBLOCK
) {
170 struct pollfd fds
= {
174 int err
= poll(&fds
, 1, timeout
* 1000);
178 if (direction
== POLLOUT
)
179 ret
= write(fd
, buf
, len
);
181 ret
= read(fd
, buf
, len
);
189 perror("Failed to poll");
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.
203 static enum test_return
ensure(bool val
, const char *expression
, const char *file
, int line
)
208 fprintf(stderr
, "%s:%u: %s\n", file
, line
, expression
);
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)
223 * Send a chunk of memory over the socket (retry if the call is iterrupted
225 static enum test_return
retry_write(const void* buf
, size_t len
)
228 const char* ptr
= buf
;
232 size_t num_bytes
= len
- offset
;
233 ssize_t nw
= timeout_io_op(sock
, POLLOUT
, (void*)(ptr
+ offset
), num_bytes
);
235 verify(errno
== EINTR
|| errno
== EAGAIN
);
238 } while (offset
< len
);
244 * Resend a packet to the server (All fields in the command header should
245 * be in network byte order)
247 static enum test_return
resend_packet(command
*cmd
)
249 size_t length
= sizeof (protocol_binary_request_no_extras
) +
250 ntohl(cmd
->plain
.message
.header
.request
.bodylen
);
252 execute(retry_write(cmd
, length
));
257 * Send a command to the server. The command header needs to be updated
258 * to network byte order
260 static enum test_return
send_packet(command
*cmd
)
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
);
270 execute(resend_packet(cmd
));
275 * Read a fixed length chunk of data from the server
277 static enum test_return
retry_read(void *buf
, size_t len
)
282 ssize_t nr
= timeout_io_op(sock
, POLLIN
, ((char*) buf
) + offset
, len
- offset
);
285 verify(errno
== EINTR
|| errno
== EAGAIN
);
292 } while (offset
< len
);
298 * Receive a response from the server and conver the fields in the header
299 * to local byte order
301 static enum test_return
recv_packet(response
*rsp
)
303 execute(retry_read(rsp
, sizeof (protocol_binary_response_no_extras
)));
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
);
315 size_t bodysz
= rsp
->plain
.message
.header
.response
.bodylen
;
317 execute(retry_read(rsp
->bytes
+ sizeof (protocol_binary_response_no_extras
), bodysz
));
323 * Create a storage command (add, set, replace etc)
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
334 static void storage_command(command
*cmd
,
343 /* all of the storage commands use the same command layout */
344 protocol_binary_request_set
*request
= &cmd
->set
;
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
;
356 off_t key_offset
= sizeof (protocol_binary_request_no_extras
) + 8;
357 memcpy(cmd
->bytes
+ key_offset
, key
, keylen
);
359 memcpy(cmd
->bytes
+ key_offset
+ keylen
, dta
, dtalen
);
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
371 static void raw_command(command
*cmd
,
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;
386 off_t key_offset
= sizeof (protocol_binary_request_no_extras
);
389 memcpy(cmd
->bytes
+ key_offset
, key
, keylen
);
392 memcpy(cmd
->bytes
+ key_offset
+ keylen
, dta
, dtalen
);
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?
402 static void flush_command(command
*cmd
,
403 uint8_t cc
, uint32_t exptime
, bool use_extra
)
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;
410 if (exptime
!= 0 || use_extra
)
412 cmd
->flush
.message
.header
.request
.extlen
= 4;
413 cmd
->flush
.message
.body
.expiration
= htonl(exptime
);
414 cmd
->flush
.message
.header
.request
.bodylen
= 4;
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
427 static void arithmetic_command(command
*cmd
,
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
);
446 off_t key_offset
= sizeof (protocol_binary_request_no_extras
) + 20;
447 memcpy(cmd
->bytes
+ key_offset
, key
, keylen
);
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
456 static enum test_return
validate_response_header(response
*rsp
,
457 uint8_t cc
, uint16_t status
)
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);
465 if (status
== PROTOCOL_BINARY_RESPONSE_SUCCESS
)
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
);
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);
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);
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);
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);
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);
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);
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);
540 /* Undefined command code */
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
)
550 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
557 static enum test_return
test_binary_noop(void)
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
));
569 static enum test_return
test_binary_quit_impl(uint8_t cc
)
573 raw_command(&cmd
, cc
, NULL
, 0, NULL
, 0);
575 execute(send_packet(&cmd
));
576 if (cc
== PROTOCOL_BINARY_CMD_QUIT
)
578 execute(recv_packet(&rsp
));
579 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_QUIT
,
580 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
583 /* Socket should be closed now, read should return 0 */
584 verify(timeout_io_op(sock
, POLLIN
, rsp
.bytes
, sizeof(rsp
.bytes
)) == 0);
586 return TEST_PASS_RECONNECT
;
589 static enum test_return
test_binary_quit(void)
591 return test_binary_quit_impl(PROTOCOL_BINARY_CMD_QUIT
);
594 static enum test_return
test_binary_quitq(void)
596 return test_binary_quit_impl(PROTOCOL_BINARY_CMD_QUITQ
);
599 static enum test_return
test_binary_set_impl(const char* key
, uint8_t cc
)
604 uint64_t value
= 0xdeadbeefdeadcafe;
605 storage_command(&cmd
, cc
, key
, strlen(key
), &value
, sizeof (value
), 0, 0);
607 /* set should always work */
608 for (int ii
= 0; ii
< 10; ii
++)
611 execute(send_packet(&cmd
));
613 execute(resend_packet(&cmd
));
615 if (cc
== PROTOCOL_BINARY_CMD_SET
)
617 execute(recv_packet(&rsp
));
618 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
621 execute(test_binary_noop());
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
)
630 execute(recv_packet(&rsp
));
631 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
634 execute(test_binary_noop());
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
));
643 return test_binary_noop();
646 static enum test_return
test_binary_set(void)
648 return test_binary_set_impl("test_binary_set", PROTOCOL_BINARY_CMD_SET
);
651 static enum test_return
test_binary_setq(void)
653 return test_binary_set_impl("test_binary_setq", PROTOCOL_BINARY_CMD_SETQ
);
656 static enum test_return
test_binary_add_impl(const char* key
, uint8_t cc
)
660 uint64_t value
= 0xdeadbeefdeadcafe;
661 storage_command(&cmd
, cc
, key
, strlen(key
), &value
, sizeof (value
), 0, 0);
663 /* first add should work, rest of them should fail (even with cas
665 for (int ii
=0; ii
< 10; ii
++)
668 execute(send_packet(&cmd
));
670 execute(resend_packet(&cmd
));
672 if (cc
== PROTOCOL_BINARY_CMD_ADD
|| ii
> 0)
674 uint16_t expected_result
;
676 expected_result
= PROTOCOL_BINARY_RESPONSE_SUCCESS
;
678 expected_result
= PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS
;
680 execute(recv_packet(&rsp
));
681 verify(validate_response_header(&rsp
, cc
, expected_result
));
684 execute(test_binary_noop());
690 static enum test_return
test_binary_add(void)
692 return test_binary_add_impl("test_binary_add", PROTOCOL_BINARY_CMD_ADD
);
695 static enum test_return
test_binary_addq(void)
697 return test_binary_add_impl("test_binary_addq", PROTOCOL_BINARY_CMD_ADDQ
);
700 static enum test_return
set_item(const char *key
, const char *value
)
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
));
713 static enum test_return
test_binary_replace_impl(const char* key
, uint8_t cc
)
717 uint64_t value
= 0xdeadbeefdeadcafe;
718 storage_command(&cmd
, cc
, key
, strlen(key
), &value
, sizeof (value
), 0, 0);
720 /* first replace should fail, successive should succeed (when the
722 for (int ii
= 0; ii
< 10; ii
++)
725 execute(send_packet(&cmd
));
727 execute(resend_packet(&cmd
));
729 if (cc
== PROTOCOL_BINARY_CMD_REPLACE
|| ii
== 0)
731 uint16_t expected_result
;
733 expected_result
=PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
;
735 expected_result
=PROTOCOL_BINARY_RESPONSE_SUCCESS
;
737 execute(recv_packet(&rsp
));
738 verify(validate_response_header(&rsp
, cc
, expected_result
));
741 execute(set_item(key
, key
));
744 execute(test_binary_noop());
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
));
752 if (cc
== PROTOCOL_BINARY_CMD_REPLACE
)
754 execute(recv_packet(&rsp
));
755 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
758 execute(test_binary_noop());
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
));
770 static enum test_return
test_binary_replace(void)
772 return test_binary_replace_impl("test_binary_replace", PROTOCOL_BINARY_CMD_REPLACE
);
775 static enum test_return
test_binary_replaceq(void)
777 return test_binary_replace_impl("test_binary_replaceq", PROTOCOL_BINARY_CMD_REPLACEQ
);
780 static enum test_return
test_binary_delete_impl(const char *key
, uint8_t cc
)
784 raw_command(&cmd
, cc
, key
, strlen(key
), NULL
, 0);
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
));
792 /* The item should be present now, resend*/
793 execute(resend_packet(&cmd
));
794 if (cc
== PROTOCOL_BINARY_CMD_DELETE
)
796 execute(recv_packet(&rsp
));
797 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
800 execute(test_binary_noop());
805 static enum test_return
test_binary_delete(void)
807 return test_binary_delete_impl("test_binary_delete", PROTOCOL_BINARY_CMD_DELETE
);
810 static enum test_return
test_binary_deleteq(void)
812 return test_binary_delete_impl("test_binary_deleteq", PROTOCOL_BINARY_CMD_DELETEQ
);
815 static enum test_return
test_binary_get_impl(const char *key
, uint8_t cc
)
820 raw_command(&cmd
, cc
, key
, strlen(key
), NULL
, 0);
821 execute(send_packet(&cmd
));
823 if (cc
== PROTOCOL_BINARY_CMD_GET
|| cc
== PROTOCOL_BINARY_CMD_GETK
)
825 execute(recv_packet(&rsp
));
826 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
));
829 execute(test_binary_noop());
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
));
839 static enum test_return
test_binary_get(void)
841 return test_binary_get_impl("test_binary_get", PROTOCOL_BINARY_CMD_GET
);
844 static enum test_return
test_binary_getk(void)
846 return test_binary_get_impl("test_binary_getk", PROTOCOL_BINARY_CMD_GETK
);
849 static enum test_return
test_binary_getq(void)
851 return test_binary_get_impl("test_binary_getq", PROTOCOL_BINARY_CMD_GETQ
);
854 static enum test_return
test_binary_getkq(void)
856 return test_binary_get_impl("test_binary_getkq", PROTOCOL_BINARY_CMD_GETKQ
);
859 static enum test_return
test_binary_incr_impl(const char* key
, uint8_t cc
)
863 arithmetic_command(&cmd
, cc
, key
, strlen(key
), 1, 0, 0);
866 for (ii
= 0; ii
< 10; ++ii
)
869 execute(send_packet(&cmd
));
871 execute(resend_packet(&cmd
));
873 if (cc
== PROTOCOL_BINARY_CMD_INCREMENT
)
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
);
880 execute(test_binary_noop());
883 /* @todo add incorrect CAS */
887 static enum test_return
test_binary_incr(void)
889 return test_binary_incr_impl("test_binary_incr", PROTOCOL_BINARY_CMD_INCREMENT
);
892 static enum test_return
test_binary_incrq(void)
894 return test_binary_incr_impl("test_binary_incrq", PROTOCOL_BINARY_CMD_INCREMENTQ
);
897 static enum test_return
test_binary_decr_impl(const char* key
, uint8_t cc
)
901 arithmetic_command(&cmd
, cc
, key
, strlen(key
), 1, 9, 0);
904 for (ii
= 9; ii
> -1; --ii
)
907 execute(send_packet(&cmd
));
909 execute(resend_packet(&cmd
));
911 if (cc
== PROTOCOL_BINARY_CMD_DECREMENT
)
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
);
918 execute(test_binary_noop());
921 /* decr 0 should not wrap */
922 execute(resend_packet(&cmd
));
923 if (cc
== PROTOCOL_BINARY_CMD_DECREMENT
)
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);
931 /* @todo get the value and verify! */
935 /* @todo add incorrect cas */
936 execute(test_binary_noop());
940 static enum test_return
test_binary_decr(void)
942 return test_binary_decr_impl("test_binary_decr",
943 PROTOCOL_BINARY_CMD_DECREMENT
);
946 static enum test_return
test_binary_decrq(void)
948 return test_binary_decr_impl("test_binary_decrq",
949 PROTOCOL_BINARY_CMD_DECREMENTQ
);
952 static enum test_return
test_binary_version(void)
956 raw_command(&cmd
, PROTOCOL_BINARY_CMD_VERSION
, NULL
, 0, NULL
, 0);
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
));
966 static enum test_return
test_binary_flush_impl(const char *key
, uint8_t cc
)
971 for (int ii
= 0; ii
< 2; ++ii
)
973 execute(set_item(key
, key
));
974 flush_command(&cmd
, cc
, 0, ii
== 0);
975 execute(send_packet(&cmd
));
977 if (cc
== PROTOCOL_BINARY_CMD_FLUSH
)
979 execute(recv_packet(&rsp
));
980 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
983 execute(test_binary_noop());
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
));
995 static enum test_return
test_binary_flush(void)
997 return test_binary_flush_impl("test_binary_flush", PROTOCOL_BINARY_CMD_FLUSH
);
1000 static enum test_return
test_binary_flushq(void)
1002 return test_binary_flush_impl("test_binary_flushq", PROTOCOL_BINARY_CMD_FLUSHQ
);
1005 static enum test_return
test_binary_concat_impl(const char *key
, uint8_t cc
)
1011 if (cc
== PROTOCOL_BINARY_CMD_APPEND
|| cc
== PROTOCOL_BINARY_CMD_APPENDQ
)
1016 execute(set_item(key
, value
));
1018 if (cc
== PROTOCOL_BINARY_CMD_APPEND
|| cc
== PROTOCOL_BINARY_CMD_APPENDQ
)
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
)
1027 execute(recv_packet(&rsp
));
1028 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1031 execute(test_binary_noop());
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);
1044 static enum test_return
test_binary_append(void)
1046 return test_binary_concat_impl("test_binary_append", PROTOCOL_BINARY_CMD_APPEND
);
1049 static enum test_return
test_binary_prepend(void)
1051 return test_binary_concat_impl("test_binary_prepend", PROTOCOL_BINARY_CMD_PREPEND
);
1054 static enum test_return
test_binary_appendq(void)
1056 return test_binary_concat_impl("test_binary_appendq", PROTOCOL_BINARY_CMD_APPENDQ
);
1059 static enum test_return
test_binary_prependq(void)
1061 return test_binary_concat_impl("test_binary_prependq", PROTOCOL_BINARY_CMD_PREPENDQ
);
1064 static enum test_return
test_binary_stat(void)
1069 raw_command(&cmd
, PROTOCOL_BINARY_CMD_STAT
, NULL
, 0, NULL
, 0);
1070 execute(send_packet(&cmd
));
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);
1082 static enum test_return
test_binary_illegal(void)
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
));
1100 typedef enum test_return(*TEST_FUNC
)(void);
1104 const char *description
;
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
},
1140 int main(int argc
, char **argv
)
1142 static const char * const status_msg
[]= {"[skip]", "[pass]", "[pass]", "[FAIL]"};
1145 const char *hostname
= "localhost";
1146 const char *port
= "11211";
1149 while ((cmd
= getopt(argc
, argv
, "t:vch:p:?")) != EOF
)
1153 timeout
= atoi(optarg
);
1156 fprintf(stderr
, "Invalid timeout. Please specify a number for -t\n");
1160 case 'v': verbose
= true;
1162 case 'c': do_core
= true;
1164 case 'h': hostname
= optarg
;
1166 case 'p': port
= optarg
;
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",
1178 sock
= connect_server(hostname
, port
);
1181 fprintf(stderr
, "Failed to connect to <%s:%s>: %s\n",
1182 hostname
, port
, strerror(errno
));
1186 for (int ii
= 0; testcases
[ii
].description
!= NULL
; ++ii
)
1189 fprintf(stdout
, "%s\t\t", testcases
[ii
].description
);
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
)
1200 else if (ret
== TEST_PASS_RECONNECT
)
1206 if ((sock
=connect_server(hostname
, port
)) == -1)
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
);
1218 fprintf(stdout
, "All tests passed\n");
1220 fprintf(stderr
, "%d of %d tests failed\n", failed
, total
);
1222 return (failed
== 0) ? 0 : 1;