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
do_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 /* We call verify(validate_response_header), but that macro
559 * expects a boolean expression, and the function returns
560 * an enum.... Let's just create a macro to avoid cluttering
561 * the code with all of the == TEST_PASS ;-)
563 #define validate_response_header(a,b,c) \
564 do_validate_response_header(a,b,c) == TEST_PASS
566 static enum test_return
test_binary_noop(void)
570 raw_command(&cmd
, PROTOCOL_BINARY_CMD_NOOP
, NULL
, 0, NULL
, 0);
571 execute(send_packet(&cmd
));
572 execute(recv_packet(&rsp
));
573 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_NOOP
,
574 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
578 static enum test_return
test_binary_quit_impl(uint8_t cc
)
582 raw_command(&cmd
, cc
, NULL
, 0, NULL
, 0);
584 execute(send_packet(&cmd
));
585 if (cc
== PROTOCOL_BINARY_CMD_QUIT
)
587 execute(recv_packet(&rsp
));
588 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_QUIT
,
589 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
592 /* Socket should be closed now, read should return 0 */
593 verify(timeout_io_op(sock
, POLLIN
, rsp
.bytes
, sizeof(rsp
.bytes
)) == 0);
595 return TEST_PASS_RECONNECT
;
598 static enum test_return
test_binary_quit(void)
600 return test_binary_quit_impl(PROTOCOL_BINARY_CMD_QUIT
);
603 static enum test_return
test_binary_quitq(void)
605 return test_binary_quit_impl(PROTOCOL_BINARY_CMD_QUITQ
);
608 static enum test_return
test_binary_set_impl(const char* key
, uint8_t cc
)
613 uint64_t value
= 0xdeadbeefdeadcafe;
614 storage_command(&cmd
, cc
, key
, strlen(key
), &value
, sizeof (value
), 0, 0);
616 /* set should always work */
617 for (int ii
= 0; ii
< 10; ii
++)
620 execute(send_packet(&cmd
));
622 execute(resend_packet(&cmd
));
624 if (cc
== PROTOCOL_BINARY_CMD_SET
)
626 execute(recv_packet(&rsp
));
627 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
630 execute(test_binary_noop());
634 * We need to get the current CAS id, and at this time we haven't
635 * verified that we have a working get
637 if (cc
== PROTOCOL_BINARY_CMD_SETQ
)
639 cmd
.set
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_SET
;
640 execute(resend_packet(&cmd
));
641 execute(recv_packet(&rsp
));
642 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_SET
,
643 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
644 cmd
.set
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_SETQ
;
647 /* try to set with the correct CAS value */
648 cmd
.plain
.message
.header
.request
.cas
=
649 htonll(rsp
.plain
.message
.header
.response
.cas
);
650 execute(resend_packet(&cmd
));
651 if (cc
== PROTOCOL_BINARY_CMD_SET
)
653 execute(recv_packet(&rsp
));
654 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
657 execute(test_binary_noop());
659 /* try to set with an incorrect CAS value */
660 cmd
.plain
.message
.header
.request
.cas
=
661 htonll(rsp
.plain
.message
.header
.response
.cas
- 1);
662 execute(resend_packet(&cmd
));
663 execute(recv_packet(&rsp
));
664 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS
));
666 return test_binary_noop();
669 static enum test_return
test_binary_set(void)
671 return test_binary_set_impl("test_binary_set", PROTOCOL_BINARY_CMD_SET
);
674 static enum test_return
test_binary_setq(void)
676 return test_binary_set_impl("test_binary_setq", PROTOCOL_BINARY_CMD_SETQ
);
679 static enum test_return
test_binary_add_impl(const char* key
, uint8_t cc
)
683 uint64_t value
= 0xdeadbeefdeadcafe;
684 storage_command(&cmd
, cc
, key
, strlen(key
), &value
, sizeof (value
), 0, 0);
686 /* first add should work, rest of them should fail (even with cas
688 for (int ii
=0; ii
< 10; ii
++)
691 execute(send_packet(&cmd
));
693 execute(resend_packet(&cmd
));
695 if (cc
== PROTOCOL_BINARY_CMD_ADD
|| ii
> 0)
697 uint16_t expected_result
;
699 expected_result
= PROTOCOL_BINARY_RESPONSE_SUCCESS
;
701 expected_result
= PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS
;
703 execute(recv_packet(&rsp
));
704 verify(validate_response_header(&rsp
, cc
, expected_result
));
707 execute(test_binary_noop());
713 static enum test_return
test_binary_add(void)
715 return test_binary_add_impl("test_binary_add", PROTOCOL_BINARY_CMD_ADD
);
718 static enum test_return
test_binary_addq(void)
720 return test_binary_add_impl("test_binary_addq", PROTOCOL_BINARY_CMD_ADDQ
);
723 static enum test_return
set_item(const char *key
, const char *value
)
727 storage_command(&cmd
, PROTOCOL_BINARY_CMD_SET
, key
, strlen(key
),
728 value
, strlen(value
), 0, 0);
729 execute(send_packet(&cmd
));
730 execute(recv_packet(&rsp
));
731 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_SET
,
732 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
736 static enum test_return
test_binary_replace_impl(const char* key
, uint8_t cc
)
740 uint64_t value
= 0xdeadbeefdeadcafe;
741 storage_command(&cmd
, cc
, key
, strlen(key
), &value
, sizeof (value
), 0, 0);
743 /* first replace should fail, successive should succeed (when the
745 for (int ii
= 0; ii
< 10; ii
++)
748 execute(send_packet(&cmd
));
750 execute(resend_packet(&cmd
));
752 if (cc
== PROTOCOL_BINARY_CMD_REPLACE
|| ii
== 0)
754 uint16_t expected_result
;
756 expected_result
=PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
;
758 expected_result
=PROTOCOL_BINARY_RESPONSE_SUCCESS
;
760 execute(recv_packet(&rsp
));
761 verify(validate_response_header(&rsp
, cc
, expected_result
));
764 execute(set_item(key
, key
));
767 execute(test_binary_noop());
770 /* verify that replace with CAS value works! */
771 cmd
.plain
.message
.header
.request
.cas
=
772 htonll(rsp
.plain
.message
.header
.response
.cas
);
773 execute(resend_packet(&cmd
));
775 if (cc
== PROTOCOL_BINARY_CMD_REPLACE
)
777 execute(recv_packet(&rsp
));
778 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
781 execute(test_binary_noop());
783 /* try to set with an incorrect CAS value */
784 cmd
.plain
.message
.header
.request
.cas
=
785 htonll(rsp
.plain
.message
.header
.response
.cas
- 1);
786 execute(resend_packet(&cmd
));
787 execute(recv_packet(&rsp
));
788 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS
));
793 static enum test_return
test_binary_replace(void)
795 return test_binary_replace_impl("test_binary_replace", PROTOCOL_BINARY_CMD_REPLACE
);
798 static enum test_return
test_binary_replaceq(void)
800 return test_binary_replace_impl("test_binary_replaceq", PROTOCOL_BINARY_CMD_REPLACEQ
);
803 static enum test_return
test_binary_delete_impl(const char *key
, uint8_t cc
)
807 raw_command(&cmd
, cc
, key
, strlen(key
), NULL
, 0);
809 /* The delete shouldn't work the first time, because the item isn't there */
810 execute(send_packet(&cmd
));
811 execute(recv_packet(&rsp
));
812 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
));
813 execute(set_item(key
, key
));
815 /* The item should be present now, resend*/
816 execute(resend_packet(&cmd
));
817 if (cc
== PROTOCOL_BINARY_CMD_DELETE
)
819 execute(recv_packet(&rsp
));
820 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
823 execute(test_binary_noop());
828 static enum test_return
test_binary_delete(void)
830 return test_binary_delete_impl("test_binary_delete", PROTOCOL_BINARY_CMD_DELETE
);
833 static enum test_return
test_binary_deleteq(void)
835 return test_binary_delete_impl("test_binary_deleteq", PROTOCOL_BINARY_CMD_DELETEQ
);
838 static enum test_return
test_binary_get_impl(const char *key
, uint8_t cc
)
843 raw_command(&cmd
, cc
, key
, strlen(key
), NULL
, 0);
844 execute(send_packet(&cmd
));
846 if (cc
== PROTOCOL_BINARY_CMD_GET
|| cc
== PROTOCOL_BINARY_CMD_GETK
)
848 execute(recv_packet(&rsp
));
849 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
));
852 execute(test_binary_noop());
854 execute(set_item(key
, key
));
855 execute(resend_packet(&cmd
));
856 execute(recv_packet(&rsp
));
857 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
862 static enum test_return
test_binary_get(void)
864 return test_binary_get_impl("test_binary_get", PROTOCOL_BINARY_CMD_GET
);
867 static enum test_return
test_binary_getk(void)
869 return test_binary_get_impl("test_binary_getk", PROTOCOL_BINARY_CMD_GETK
);
872 static enum test_return
test_binary_getq(void)
874 return test_binary_get_impl("test_binary_getq", PROTOCOL_BINARY_CMD_GETQ
);
877 static enum test_return
test_binary_getkq(void)
879 return test_binary_get_impl("test_binary_getkq", PROTOCOL_BINARY_CMD_GETKQ
);
882 static enum test_return
test_binary_incr_impl(const char* key
, uint8_t cc
)
886 arithmetic_command(&cmd
, cc
, key
, strlen(key
), 1, 0, 0);
889 for (ii
= 0; ii
< 10; ++ii
)
892 execute(send_packet(&cmd
));
894 execute(resend_packet(&cmd
));
896 if (cc
== PROTOCOL_BINARY_CMD_INCREMENT
)
898 execute(recv_packet(&rsp
));
899 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
900 verify(ntohll(rsp
.incr
.message
.body
.value
) == ii
);
903 execute(test_binary_noop());
906 /* @todo add incorrect CAS */
910 static enum test_return
test_binary_incr(void)
912 return test_binary_incr_impl("test_binary_incr", PROTOCOL_BINARY_CMD_INCREMENT
);
915 static enum test_return
test_binary_incrq(void)
917 return test_binary_incr_impl("test_binary_incrq", PROTOCOL_BINARY_CMD_INCREMENTQ
);
920 static enum test_return
test_binary_decr_impl(const char* key
, uint8_t cc
)
924 arithmetic_command(&cmd
, cc
, key
, strlen(key
), 1, 9, 0);
927 for (ii
= 9; ii
> -1; --ii
)
930 execute(send_packet(&cmd
));
932 execute(resend_packet(&cmd
));
934 if (cc
== PROTOCOL_BINARY_CMD_DECREMENT
)
936 execute(recv_packet(&rsp
));
937 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
938 verify(ntohll(rsp
.decr
.message
.body
.value
) == (uint64_t)ii
);
941 execute(test_binary_noop());
944 /* decr 0 should not wrap */
945 execute(resend_packet(&cmd
));
946 if (cc
== PROTOCOL_BINARY_CMD_DECREMENT
)
948 execute(recv_packet(&rsp
));
949 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
950 verify(ntohll(rsp
.decr
.message
.body
.value
) == 0);
954 /* @todo get the value and verify! */
958 /* @todo add incorrect cas */
959 execute(test_binary_noop());
963 static enum test_return
test_binary_decr(void)
965 return test_binary_decr_impl("test_binary_decr",
966 PROTOCOL_BINARY_CMD_DECREMENT
);
969 static enum test_return
test_binary_decrq(void)
971 return test_binary_decr_impl("test_binary_decrq",
972 PROTOCOL_BINARY_CMD_DECREMENTQ
);
975 static enum test_return
test_binary_version(void)
979 raw_command(&cmd
, PROTOCOL_BINARY_CMD_VERSION
, NULL
, 0, NULL
, 0);
981 execute(send_packet(&cmd
));
982 execute(recv_packet(&rsp
));
983 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_VERSION
,
984 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
989 static enum test_return
test_binary_flush_impl(const char *key
, uint8_t cc
)
994 for (int ii
= 0; ii
< 2; ++ii
)
996 execute(set_item(key
, key
));
997 flush_command(&cmd
, cc
, 0, ii
== 0);
998 execute(send_packet(&cmd
));
1000 if (cc
== PROTOCOL_BINARY_CMD_FLUSH
)
1002 execute(recv_packet(&rsp
));
1003 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1006 execute(test_binary_noop());
1008 raw_command(&cmd
, PROTOCOL_BINARY_CMD_GET
, key
, strlen(key
), NULL
, 0);
1009 execute(send_packet(&cmd
));
1010 execute(recv_packet(&rsp
));
1011 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_GET
,
1012 PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
));
1018 static enum test_return
test_binary_flush(void)
1020 return test_binary_flush_impl("test_binary_flush", PROTOCOL_BINARY_CMD_FLUSH
);
1023 static enum test_return
test_binary_flushq(void)
1025 return test_binary_flush_impl("test_binary_flushq", PROTOCOL_BINARY_CMD_FLUSHQ
);
1028 static enum test_return
test_binary_concat_impl(const char *key
, uint8_t cc
)
1034 if (cc
== PROTOCOL_BINARY_CMD_APPEND
|| cc
== PROTOCOL_BINARY_CMD_APPENDQ
)
1039 execute(set_item(key
, value
));
1041 if (cc
== PROTOCOL_BINARY_CMD_APPEND
|| cc
== PROTOCOL_BINARY_CMD_APPENDQ
)
1046 raw_command(&cmd
, cc
, key
, strlen(key
), value
, strlen(value
));
1047 execute(send_packet(&cmd
));
1048 if (cc
== PROTOCOL_BINARY_CMD_APPEND
|| cc
== PROTOCOL_BINARY_CMD_PREPEND
)
1050 execute(recv_packet(&rsp
));
1051 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1054 execute(test_binary_noop());
1056 raw_command(&cmd
, PROTOCOL_BINARY_CMD_GET
, key
, strlen(key
), NULL
, 0);
1057 execute(send_packet(&cmd
));
1058 execute(recv_packet(&rsp
));
1059 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_GET
,
1060 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1061 verify(rsp
.plain
.message
.header
.response
.bodylen
- 4 == 11);
1062 verify(memcmp(rsp
.bytes
+ 28, "hello world", 11) == 0);
1067 static enum test_return
test_binary_append(void)
1069 return test_binary_concat_impl("test_binary_append", PROTOCOL_BINARY_CMD_APPEND
);
1072 static enum test_return
test_binary_prepend(void)
1074 return test_binary_concat_impl("test_binary_prepend", PROTOCOL_BINARY_CMD_PREPEND
);
1077 static enum test_return
test_binary_appendq(void)
1079 return test_binary_concat_impl("test_binary_appendq", PROTOCOL_BINARY_CMD_APPENDQ
);
1082 static enum test_return
test_binary_prependq(void)
1084 return test_binary_concat_impl("test_binary_prependq", PROTOCOL_BINARY_CMD_PREPENDQ
);
1087 static enum test_return
test_binary_stat(void)
1092 raw_command(&cmd
, PROTOCOL_BINARY_CMD_STAT
, NULL
, 0, NULL
, 0);
1093 execute(send_packet(&cmd
));
1097 execute(recv_packet(&rsp
));
1098 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_STAT
,
1099 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1100 } while (rsp
.plain
.message
.header
.response
.keylen
!= 0);
1105 static enum test_return
test_binary_illegal(void)
1113 raw_command(&cmd
, cc
, NULL
, 0, NULL
, 0);
1114 execute(send_packet(&cmd
));
1115 execute(recv_packet(&rsp
));
1116 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_UNKNOWN_COMMAND
));
1123 typedef enum test_return(*TEST_FUNC
)(void);
1127 const char *description
;
1131 struct testcase testcases
[]= {
1132 { "noop", test_binary_noop
},
1133 { "quit", test_binary_quit
},
1134 { "quitq", test_binary_quitq
},
1135 { "set", test_binary_set
},
1136 { "setq", test_binary_setq
},
1137 { "flush", test_binary_flush
},
1138 { "flushq", test_binary_flushq
},
1139 { "add", test_binary_add
},
1140 { "addq", test_binary_addq
},
1141 { "replace", test_binary_replace
},
1142 { "replaceq", test_binary_replaceq
},
1143 { "delete", test_binary_delete
},
1144 { "deleteq", test_binary_deleteq
},
1145 { "get", test_binary_get
},
1146 { "getq", test_binary_getq
},
1147 { "getk", test_binary_getk
},
1148 { "getkq", test_binary_getkq
},
1149 { "incr", test_binary_incr
},
1150 { "incrq", test_binary_incrq
},
1151 { "decr", test_binary_decr
},
1152 { "decrq", test_binary_decrq
},
1153 { "version", test_binary_version
},
1154 { "append", test_binary_append
},
1155 { "appendq", test_binary_appendq
},
1156 { "prepend", test_binary_prepend
},
1157 { "prependq", test_binary_prependq
},
1158 { "stat", test_binary_stat
},
1159 { "illegal", test_binary_illegal
},
1163 int main(int argc
, char **argv
)
1165 static const char * const status_msg
[]= {"[skip]", "[pass]", "[pass]", "[FAIL]"};
1168 const char *hostname
= "localhost";
1169 const char *port
= "11211";
1172 while ((cmd
= getopt(argc
, argv
, "t:vch:p:?")) != EOF
)
1176 timeout
= atoi(optarg
);
1179 fprintf(stderr
, "Invalid timeout. Please specify a number for -t\n");
1183 case 'v': verbose
= true;
1185 case 'c': do_core
= true;
1187 case 'h': hostname
= optarg
;
1189 case 'p': port
= optarg
;
1192 fprintf(stderr
, "Usage: %s [-h hostname] [-p port] [-c] [-v] [-t n]\n"
1193 "\t-c\tGenerate coredump if a test fails\n"
1194 "\t-v\tVerbose test output (print out the assertion)\n"
1195 "\t-t n\tSet the timeout for io-operations to n seconds\n",
1201 sock
= connect_server(hostname
, port
);
1204 fprintf(stderr
, "Failed to connect to <%s:%s>: %s\n",
1205 hostname
, port
, strerror(errno
));
1209 for (int ii
= 0; testcases
[ii
].description
!= NULL
; ++ii
)
1212 fprintf(stdout
, "%s\t\t", testcases
[ii
].description
);
1215 bool reconnect
= false;
1216 enum test_return ret
= testcases
[ii
].function();
1217 fprintf(stderr
, "%s\n", status_msg
[ret
]);
1218 if (ret
== TEST_FAIL
)
1223 else if (ret
== TEST_PASS_RECONNECT
)
1229 if ((sock
=connect_server(hostname
, port
)) == -1)
1231 fprintf(stderr
, "Failed to connect to <%s:%s>: %s\n",
1232 hostname
, port
, strerror(errno
));
1233 fprintf(stderr
, "%d of %d tests failed\n", failed
, total
);
1241 fprintf(stdout
, "All tests passed\n");
1243 fprintf(stderr
, "%d of %d tests failed\n", failed
, total
);
1245 return (failed
== 0) ? 0 : 1;