1 /* -*- Mode: C; tab-width: 2; c-basic-offset: 2; indent-tabs-mode: nil -*- */
6 #include <sys/socket.h>
9 #include <netinet/in.h>
10 #include <netinet/tcp.h>
23 #include <libmemcached/memcached/protocol_binary.h>
24 #include <libmemcached/byteorder.h>
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 ..
36 /* Should we generate coredumps when we enounter an error (-c) */
37 static bool do_core
= false;
38 /* connection to the server */
40 /* Should the output from test failures be verbose or quiet? */
41 static bool verbose
= false;
43 /* The number of seconds to wait for an IO-operation */
44 static int timeout
= 2;
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.
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
;
66 protocol_binary_response_no_extras plain
;
67 protocol_binary_response_incr incr
;
68 protocol_binary_response_decr decr
;
74 TEST_SKIP
, TEST_PASS
, TEST_PASS_RECONNECT
, TEST_FAIL
78 * Try to get an addrinfo struct for a given port on a given host
80 static struct addrinfo
*lookuphost(const char *hostname
, const char *port
)
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
);
90 if (error
!= EAI_SYSTEM
)
91 fprintf(stderr
, "getaddrinfo(): %s\n", gai_strerror(error
));
93 perror("getaddrinfo()");
100 * Set the socket in nonblocking mode
101 * @return -1 if failure, the socket otherwise
103 static int set_noblock(void)
105 int flags
= fcntl(sock
, F_GETFL
, 0);
108 perror("Failed to get socket flags");
113 if ((flags
& O_NONBLOCK
) != O_NONBLOCK
)
115 if (fcntl(sock
, F_SETFL
, flags
| O_NONBLOCK
) == -1)
117 perror("Failed to set socket to nonblocking mode");
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
132 static int connect_server(const char *hostname
, const char *port
)
134 struct addrinfo
*ai
= lookuphost(hostname
, port
);
138 if ((sock
=socket(ai
->ai_family
, ai
->ai_socktype
,
139 ai
->ai_protocol
)) != -1)
141 if (connect(sock
, ai
->ai_addr
, ai
->ai_addrlen
) == -1)
143 fprintf(stderr
, "Failed to connect socket: %s\n",
153 fprintf(stderr
, "Failed to create socket: %s\n", strerror(errno
));
161 static ssize_t
timeout_io_op(int fd
, short direction
, void *buf
, size_t len
)
165 if (direction
== POLLOUT
)
166 ret
= write(fd
, buf
, len
);
168 ret
= read(fd
, buf
, len
);
170 if (ret
== -1 && errno
== EWOULDBLOCK
) {
171 struct pollfd fds
= {
175 int err
= poll(&fds
, 1, timeout
* 1000);
179 if (direction
== POLLOUT
)
180 ret
= write(fd
, buf
, len
);
182 ret
= read(fd
, buf
, len
);
190 perror("Failed to poll");
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.
204 static enum test_return
ensure(bool val
, const char *expression
, const char *file
, int line
)
209 fprintf(stderr
, "%s:%u: %s\n", file
, line
, expression
);
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)
224 * Send a chunk of memory over the socket (retry if the call is iterrupted
226 static enum test_return
retry_write(const void* buf
, size_t len
)
229 const char* ptr
= buf
;
233 size_t num_bytes
= len
- offset
;
234 ssize_t nw
= timeout_io_op(sock
, POLLOUT
, (void*)(ptr
+ offset
), num_bytes
);
236 verify(errno
== EINTR
|| errno
== EAGAIN
);
239 } while (offset
< len
);
245 * Resend a packet to the server (All fields in the command header should
246 * be in network byte order)
248 static enum test_return
resend_packet(command
*cmd
)
250 size_t length
= sizeof (protocol_binary_request_no_extras
) +
251 ntohl(cmd
->plain
.message
.header
.request
.bodylen
);
253 execute(retry_write(cmd
, length
));
258 * Send a command to the server. The command header needs to be updated
259 * to network byte order
261 static enum test_return
send_packet(command
*cmd
)
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
);
271 execute(resend_packet(cmd
));
276 * Read a fixed length chunk of data from the server
278 static enum test_return
retry_read(void *buf
, size_t len
)
283 ssize_t nr
= timeout_io_op(sock
, POLLIN
, ((char*) buf
) + offset
, len
- offset
);
286 verify(errno
== EINTR
|| errno
== EAGAIN
);
293 } while (offset
< len
);
299 * Receive a response from the server and conver the fields in the header
300 * to local byte order
302 static enum test_return
recv_packet(response
*rsp
)
304 execute(retry_read(rsp
, sizeof (protocol_binary_response_no_extras
)));
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
);
316 size_t bodysz
= rsp
->plain
.message
.header
.response
.bodylen
;
318 execute(retry_read(rsp
->bytes
+ sizeof (protocol_binary_response_no_extras
), bodysz
));
324 * Create a storage command (add, set, replace etc)
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
335 static void storage_command(command
*cmd
,
344 /* all of the storage commands use the same command layout */
345 protocol_binary_request_set
*request
= &cmd
->set
;
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
;
357 off_t key_offset
= sizeof (protocol_binary_request_no_extras
) + 8;
358 memcpy(cmd
->bytes
+ key_offset
, key
, keylen
);
360 memcpy(cmd
->bytes
+ key_offset
+ keylen
, dta
, dtalen
);
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
372 static void raw_command(command
*cmd
,
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;
387 off_t key_offset
= sizeof (protocol_binary_request_no_extras
);
390 memcpy(cmd
->bytes
+ key_offset
, key
, keylen
);
393 memcpy(cmd
->bytes
+ key_offset
+ keylen
, dta
, dtalen
);
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?
403 static void flush_command(command
*cmd
,
404 uint8_t cc
, uint32_t exptime
, bool use_extra
)
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;
411 if (exptime
!= 0 || use_extra
)
413 cmd
->flush
.message
.header
.request
.extlen
= 4;
414 cmd
->flush
.message
.body
.expiration
= htonl(exptime
);
415 cmd
->flush
.message
.header
.request
.bodylen
= 4;
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
428 static void arithmetic_command(command
*cmd
,
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
);
447 off_t key_offset
= sizeof (protocol_binary_request_no_extras
) + 20;
448 memcpy(cmd
->bytes
+ key_offset
, key
, keylen
);
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
457 static enum test_return
validate_response_header(response
*rsp
,
458 uint8_t cc
, uint16_t status
)
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);
466 if (status
== PROTOCOL_BINARY_RESPONSE_SUCCESS
)
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
);
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);
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);
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);
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);
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);
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);
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);
541 /* Undefined command code */
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
)
551 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
558 static enum test_return
test_binary_noop(void)
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
));
570 static enum test_return
test_binary_quit_impl(uint8_t cc
)
574 raw_command(&cmd
, cc
, NULL
, 0, NULL
, 0);
576 execute(send_packet(&cmd
));
577 if (cc
== PROTOCOL_BINARY_CMD_QUIT
)
579 execute(recv_packet(&rsp
));
580 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_QUIT
,
581 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
584 /* Socket should be closed now, read should return 0 */
585 verify(timeout_io_op(sock
, POLLIN
, rsp
.bytes
, sizeof(rsp
.bytes
)) == 0);
587 return TEST_PASS_RECONNECT
;
590 static enum test_return
test_binary_quit(void)
592 return test_binary_quit_impl(PROTOCOL_BINARY_CMD_QUIT
);
595 static enum test_return
test_binary_quitq(void)
597 return test_binary_quit_impl(PROTOCOL_BINARY_CMD_QUITQ
);
600 static enum test_return
test_binary_set_impl(const char* key
, uint8_t cc
)
605 uint64_t value
= 0xdeadbeefdeadcafe;
606 storage_command(&cmd
, cc
, key
, strlen(key
), &value
, sizeof (value
), 0, 0);
608 /* set should always work */
609 for (int ii
= 0; ii
< 10; ii
++)
612 execute(send_packet(&cmd
));
614 execute(resend_packet(&cmd
));
616 if (cc
== PROTOCOL_BINARY_CMD_SET
)
618 execute(recv_packet(&rsp
));
619 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
622 execute(test_binary_noop());
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
)
631 execute(recv_packet(&rsp
));
632 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
635 execute(test_binary_noop());
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
));
644 return test_binary_noop();
647 static enum test_return
test_binary_set(void)
649 return test_binary_set_impl("test_binary_set", PROTOCOL_BINARY_CMD_SET
);
652 static enum test_return
test_binary_setq(void)
654 return test_binary_set_impl("test_binary_setq", PROTOCOL_BINARY_CMD_SETQ
);
657 static enum test_return
test_binary_add_impl(const char* key
, uint8_t cc
)
661 uint64_t value
= 0xdeadbeefdeadcafe;
662 storage_command(&cmd
, cc
, key
, strlen(key
), &value
, sizeof (value
), 0, 0);
664 /* first add should work, rest of them should fail (even with cas
666 for (int ii
=0; ii
< 10; ii
++)
669 execute(send_packet(&cmd
));
671 execute(resend_packet(&cmd
));
673 if (cc
== PROTOCOL_BINARY_CMD_ADD
|| ii
> 0)
675 uint16_t expected_result
;
677 expected_result
= PROTOCOL_BINARY_RESPONSE_SUCCESS
;
679 expected_result
= PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS
;
681 execute(recv_packet(&rsp
));
682 verify(validate_response_header(&rsp
, cc
, expected_result
));
685 execute(test_binary_noop());
691 static enum test_return
test_binary_add(void)
693 return test_binary_add_impl("test_binary_add", PROTOCOL_BINARY_CMD_ADD
);
696 static enum test_return
test_binary_addq(void)
698 return test_binary_add_impl("test_binary_addq", PROTOCOL_BINARY_CMD_ADDQ
);
701 static enum test_return
set_item(const char *key
, const char *value
)
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
));
714 static enum test_return
test_binary_replace_impl(const char* key
, uint8_t cc
)
718 uint64_t value
= 0xdeadbeefdeadcafe;
719 storage_command(&cmd
, cc
, key
, strlen(key
), &value
, sizeof (value
), 0, 0);
721 /* first replace should fail, successive should succeed (when the
723 for (int ii
= 0; ii
< 10; ii
++)
726 execute(send_packet(&cmd
));
728 execute(resend_packet(&cmd
));
730 if (cc
== PROTOCOL_BINARY_CMD_REPLACE
|| ii
== 0)
732 uint16_t expected_result
;
734 expected_result
=PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
;
736 expected_result
=PROTOCOL_BINARY_RESPONSE_SUCCESS
;
738 execute(recv_packet(&rsp
));
739 verify(validate_response_header(&rsp
, cc
, expected_result
));
742 execute(set_item(key
, key
));
745 execute(test_binary_noop());
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
));
753 if (cc
== PROTOCOL_BINARY_CMD_REPLACE
)
755 execute(recv_packet(&rsp
));
756 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
759 execute(test_binary_noop());
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
));
771 static enum test_return
test_binary_replace(void)
773 return test_binary_replace_impl("test_binary_replace", PROTOCOL_BINARY_CMD_REPLACE
);
776 static enum test_return
test_binary_replaceq(void)
778 return test_binary_replace_impl("test_binary_replaceq", PROTOCOL_BINARY_CMD_REPLACEQ
);
781 static enum test_return
test_binary_delete_impl(const char *key
, uint8_t cc
)
785 raw_command(&cmd
, cc
, key
, strlen(key
), NULL
, 0);
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
));
793 /* The item should be present now, resend*/
794 execute(resend_packet(&cmd
));
795 if (cc
== PROTOCOL_BINARY_CMD_DELETE
)
797 execute(recv_packet(&rsp
));
798 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
801 execute(test_binary_noop());
806 static enum test_return
test_binary_delete(void)
808 return test_binary_delete_impl("test_binary_delete", PROTOCOL_BINARY_CMD_DELETE
);
811 static enum test_return
test_binary_deleteq(void)
813 return test_binary_delete_impl("test_binary_deleteq", PROTOCOL_BINARY_CMD_DELETEQ
);
816 static enum test_return
test_binary_get_impl(const char *key
, uint8_t cc
)
821 raw_command(&cmd
, cc
, key
, strlen(key
), NULL
, 0);
822 execute(send_packet(&cmd
));
824 if (cc
== PROTOCOL_BINARY_CMD_GET
|| cc
== PROTOCOL_BINARY_CMD_GETK
)
826 execute(recv_packet(&rsp
));
827 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
));
830 execute(test_binary_noop());
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
));
840 static enum test_return
test_binary_get(void)
842 return test_binary_get_impl("test_binary_get", PROTOCOL_BINARY_CMD_GET
);
845 static enum test_return
test_binary_getk(void)
847 return test_binary_get_impl("test_binary_getk", PROTOCOL_BINARY_CMD_GETK
);
850 static enum test_return
test_binary_getq(void)
852 return test_binary_get_impl("test_binary_getq", PROTOCOL_BINARY_CMD_GETQ
);
855 static enum test_return
test_binary_getkq(void)
857 return test_binary_get_impl("test_binary_getkq", PROTOCOL_BINARY_CMD_GETKQ
);
860 static enum test_return
test_binary_incr_impl(const char* key
, uint8_t cc
)
864 arithmetic_command(&cmd
, cc
, key
, strlen(key
), 1, 0, 0);
867 for (ii
= 0; ii
< 10; ++ii
)
870 execute(send_packet(&cmd
));
872 execute(resend_packet(&cmd
));
874 if (cc
== PROTOCOL_BINARY_CMD_INCREMENT
)
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
);
881 execute(test_binary_noop());
884 /* @todo add incorrect CAS */
888 static enum test_return
test_binary_incr(void)
890 return test_binary_incr_impl("test_binary_incr", PROTOCOL_BINARY_CMD_INCREMENT
);
893 static enum test_return
test_binary_incrq(void)
895 return test_binary_incr_impl("test_binary_incrq", PROTOCOL_BINARY_CMD_INCREMENTQ
);
898 static enum test_return
test_binary_decr_impl(const char* key
, uint8_t cc
)
902 arithmetic_command(&cmd
, cc
, key
, strlen(key
), 1, 9, 0);
905 for (ii
= 9; ii
> -1; --ii
)
908 execute(send_packet(&cmd
));
910 execute(resend_packet(&cmd
));
912 if (cc
== PROTOCOL_BINARY_CMD_DECREMENT
)
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
);
919 execute(test_binary_noop());
922 /* decr 0 should not wrap */
923 execute(resend_packet(&cmd
));
924 if (cc
== PROTOCOL_BINARY_CMD_DECREMENT
)
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);
932 /* @todo get the value and verify! */
936 /* @todo add incorrect cas */
937 execute(test_binary_noop());
941 static enum test_return
test_binary_decr(void)
943 return test_binary_decr_impl("test_binary_decr",
944 PROTOCOL_BINARY_CMD_DECREMENT
);
947 static enum test_return
test_binary_decrq(void)
949 return test_binary_decr_impl("test_binary_decrq",
950 PROTOCOL_BINARY_CMD_DECREMENTQ
);
953 static enum test_return
test_binary_version(void)
957 raw_command(&cmd
, PROTOCOL_BINARY_CMD_VERSION
, NULL
, 0, NULL
, 0);
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
));
967 static enum test_return
test_binary_flush_impl(const char *key
, uint8_t cc
)
972 for (int ii
= 0; ii
< 2; ++ii
)
974 execute(set_item(key
, key
));
975 flush_command(&cmd
, cc
, 0, ii
== 0);
976 execute(send_packet(&cmd
));
978 if (cc
== PROTOCOL_BINARY_CMD_FLUSH
)
980 execute(recv_packet(&rsp
));
981 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
984 execute(test_binary_noop());
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
));
996 static enum test_return
test_binary_flush(void)
998 return test_binary_flush_impl("test_binary_flush", PROTOCOL_BINARY_CMD_FLUSH
);
1001 static enum test_return
test_binary_flushq(void)
1003 return test_binary_flush_impl("test_binary_flushq", PROTOCOL_BINARY_CMD_FLUSHQ
);
1006 static enum test_return
test_binary_concat_impl(const char *key
, uint8_t cc
)
1012 if (cc
== PROTOCOL_BINARY_CMD_APPEND
|| cc
== PROTOCOL_BINARY_CMD_APPENDQ
)
1017 execute(set_item(key
, value
));
1019 if (cc
== PROTOCOL_BINARY_CMD_APPEND
|| cc
== PROTOCOL_BINARY_CMD_APPENDQ
)
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
)
1028 execute(recv_packet(&rsp
));
1029 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1032 execute(test_binary_noop());
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);
1045 static enum test_return
test_binary_append(void)
1047 return test_binary_concat_impl("test_binary_append", PROTOCOL_BINARY_CMD_APPEND
);
1050 static enum test_return
test_binary_prepend(void)
1052 return test_binary_concat_impl("test_binary_prepend", PROTOCOL_BINARY_CMD_PREPEND
);
1055 static enum test_return
test_binary_appendq(void)
1057 return test_binary_concat_impl("test_binary_appendq", PROTOCOL_BINARY_CMD_APPENDQ
);
1060 static enum test_return
test_binary_prependq(void)
1062 return test_binary_concat_impl("test_binary_prependq", PROTOCOL_BINARY_CMD_PREPENDQ
);
1065 static enum test_return
test_binary_stat(void)
1070 raw_command(&cmd
, PROTOCOL_BINARY_CMD_STAT
, NULL
, 0, NULL
, 0);
1071 execute(send_packet(&cmd
));
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);
1083 static enum test_return
test_binary_illegal(void)
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
));
1101 typedef enum test_return(*TEST_FUNC
)(void);
1105 const char *description
;
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
},
1141 int main(int argc
, char **argv
)
1143 static const char * const status_msg
[]= {"[skip]", "[pass]", "[pass]", "[FAIL]"};
1146 const char *hostname
= "localhost";
1147 const char *port
= "11211";
1150 while ((cmd
= getopt(argc
, argv
, "t:vch:p:?")) != EOF
)
1154 timeout
= atoi(optarg
);
1157 fprintf(stderr
, "Invalid timeout. Please specify a number for -t\n");
1161 case 'v': verbose
= true;
1163 case 'c': do_core
= true;
1165 case 'h': hostname
= optarg
;
1167 case 'p': port
= optarg
;
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",
1179 sock
= connect_server(hostname
, port
);
1182 fprintf(stderr
, "Failed to connect to <%s:%s>: %s\n",
1183 hostname
, port
, strerror(errno
));
1187 for (int ii
= 0; testcases
[ii
].description
!= NULL
; ++ii
)
1190 fprintf(stdout
, "%s\t\t", testcases
[ii
].description
);
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
)
1201 else if (ret
== TEST_PASS_RECONNECT
)
1207 if ((sock
=connect_server(hostname
, port
)) == -1)
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
);
1219 fprintf(stdout
, "All tests passed\n");
1221 fprintf(stderr
, "%d of %d tests failed\n", failed
, total
);
1223 return (failed
== 0) ? 0 : 1;