2 * Copyright (C) 2006-2009 Brian Aker
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
12 /* -*- Mode: C; tab-width: 2; c-basic-offset: 2; indent-tabs-mode: nil -*- */
16 #include <sys/types.h>
29 #include <libmemcached/memcached.h>
30 #include <libmemcached/memcached/protocol_binary.h>
31 #include <libmemcached/byteorder.h>
32 #include "utilities.h"
35 /* /usr/include/netinet/in.h defines macros from ntohs() to _bswap_nn to
36 * optimize the conversion functions, but the prototypes generate warnings
37 * from gcc. The conversion methods isn't the bottleneck for my app, so
38 * just remove the warnings by undef'ing the optimization ..
44 /* Should we generate coredumps when we enounter an error (-c) */
45 static bool do_core
= false;
46 /* connection to the server */
47 static memcached_socket_t sock
;
48 /* Should the output from test failures be verbose or quiet? */
49 static bool verbose
= false;
51 /* The number of seconds to wait for an IO-operation */
52 static int timeout
= 2;
55 * Instead of having to cast between the different datatypes we create
56 * a union of all of the different types of pacages we want to send.
57 * A lot of the different commands use the same packet layout, so I'll
58 * just define the different types I need. The typedefs only contain
59 * the header of the message, so we need some space for keys and body
60 * To avoid to have to do multiple writes, lets add a chunk of memory
61 * to use. 1k should be more than enough for header, key and body.
65 protocol_binary_request_no_extras plain
;
66 protocol_binary_request_flush flush
;
67 protocol_binary_request_incr incr
;
68 protocol_binary_request_set set
;
74 protocol_binary_response_no_extras plain
;
75 protocol_binary_response_incr incr
;
76 protocol_binary_response_decr decr
;
82 TEST_SKIP
, TEST_PASS
, TEST_PASS_RECONNECT
, TEST_FAIL
86 * Try to get an addrinfo struct for a given port on a given host
88 static struct addrinfo
*lookuphost(const char *hostname
, const char *port
)
90 struct addrinfo
*ai
= 0;
91 struct addrinfo hints
= {.ai_family
=AF_UNSPEC
,
92 .ai_protocol
=IPPROTO_TCP
,
93 .ai_socktype
=SOCK_STREAM
};
94 int error
= getaddrinfo(hostname
, port
, &hints
, &ai
);
98 if (error
!= EAI_SYSTEM
)
99 fprintf(stderr
, "getaddrinfo(): %s\n", gai_strerror(error
));
101 perror("getaddrinfo()");
108 * Set the socket in nonblocking mode
109 * @return -1 if failure, the socket otherwise
111 static memcached_socket_t
set_noblock(void)
115 if (ioctlsocket(sock
, FIONBIO
, &arg
) == SOCKET_ERROR
)
117 perror("Failed to set nonblocking io");
119 return INVALID_SOCKET
;
122 int flags
= fcntl(sock
, F_GETFL
, 0);
125 perror("Failed to get socket flags");
127 return INVALID_SOCKET
;
130 if ((flags
& O_NONBLOCK
) != O_NONBLOCK
)
132 if (fcntl(sock
, F_SETFL
, flags
| O_NONBLOCK
) == -1)
134 perror("Failed to set socket to nonblocking mode");
136 return INVALID_SOCKET
;
144 * Try to open a connection to the server
145 * @param hostname the name of the server to connect to
146 * @param port the port number (or service) to connect to
147 * @return positive integer if success, -1 otherwise
149 static memcached_socket_t
connect_server(const char *hostname
, const char *port
)
151 struct addrinfo
*ai
= lookuphost(hostname
, port
);
152 sock
= INVALID_SOCKET
;
155 if ((sock
= socket(ai
->ai_family
, ai
->ai_socktype
,
156 ai
->ai_protocol
)) != INVALID_SOCKET
)
158 if (connect(sock
, ai
->ai_addr
, ai
->ai_addrlen
) == SOCKET_ERROR
)
160 fprintf(stderr
, "Failed to connect socket: %s\n",
161 strerror(get_socket_errno()));
163 sock
= INVALID_SOCKET
;
171 fprintf(stderr
, "Failed to create socket: %s\n",
172 strerror(get_socket_errno()));
180 static ssize_t
timeout_io_op(memcached_socket_t fd
, short direction
, void *buf
, size_t len
)
184 if (direction
== POLLOUT
)
185 ret
= send(fd
, buf
, len
, 0);
187 ret
= recv(fd
, buf
, len
, 0);
189 if (ret
== SOCKET_ERROR
&& get_socket_errno() == EWOULDBLOCK
) {
195 int err
= poll(&fds
, 1, timeout
* 1000);
199 if (direction
== POLLOUT
)
200 ret
= send(fd
, buf
, len
, 0);
202 ret
= recv(fd
, buf
, len
, 0);
210 perror("Failed to poll");
219 * Ensure that an expression is true. If it isn't print out a message similar
220 * to assert() and create a coredump if the user wants that. If not an error
221 * message is returned.
224 static enum test_return
ensure(bool val
, const char *expression
, const char *file
, int line
)
229 fprintf(stderr
, "\n%s:%d: %s", file
, line
, expression
);
240 #define verify(expression) do { if (ensure(expression, #expression, __FILE__, __LINE__) == TEST_FAIL) return TEST_FAIL; } while (0)
241 #define execute(expression) do { if (ensure(expression == TEST_PASS, #expression, __FILE__, __LINE__) == TEST_FAIL) return TEST_FAIL; } while (0)
244 * Send a chunk of memory over the socket (retry if the call is iterrupted
246 static enum test_return
retry_write(const void* buf
, size_t len
)
249 const char* ptr
= buf
;
253 size_t num_bytes
= len
- offset
;
254 ssize_t nw
= timeout_io_op(sock
, POLLOUT
, (void*)(ptr
+ offset
), num_bytes
);
256 verify(get_socket_errno() == EINTR
|| get_socket_errno() == EAGAIN
);
259 } while (offset
< len
);
265 * Resend a packet to the server (All fields in the command header should
266 * be in network byte order)
268 static enum test_return
resend_packet(command
*cmd
)
270 size_t length
= sizeof (protocol_binary_request_no_extras
) +
271 ntohl(cmd
->plain
.message
.header
.request
.bodylen
);
273 execute(retry_write(cmd
, length
));
278 * Send a command to the server. The command header needs to be updated
279 * to network byte order
281 static enum test_return
send_packet(command
*cmd
)
283 /* Fix the byteorder of the header */
284 cmd
->plain
.message
.header
.request
.keylen
=
285 ntohs(cmd
->plain
.message
.header
.request
.keylen
);
286 cmd
->plain
.message
.header
.request
.bodylen
=
287 ntohl(cmd
->plain
.message
.header
.request
.bodylen
);
288 cmd
->plain
.message
.header
.request
.cas
=
289 ntohll(cmd
->plain
.message
.header
.request
.cas
);
291 execute(resend_packet(cmd
));
296 * Read a fixed length chunk of data from the server
298 static enum test_return
retry_read(void *buf
, size_t len
)
303 ssize_t nr
= timeout_io_op(sock
, POLLIN
, ((char*) buf
) + offset
, len
- offset
);
306 verify(get_socket_errno() == EINTR
|| get_socket_errno() == EAGAIN
);
313 } while (offset
< len
);
319 * Receive a response from the server and conver the fields in the header
320 * to local byte order
322 static enum test_return
recv_packet(response
*rsp
)
324 execute(retry_read(rsp
, sizeof (protocol_binary_response_no_extras
)));
326 /* Fix the byte order in the packet header */
327 rsp
->plain
.message
.header
.response
.keylen
=
328 ntohs(rsp
->plain
.message
.header
.response
.keylen
);
329 rsp
->plain
.message
.header
.response
.status
=
330 ntohs(rsp
->plain
.message
.header
.response
.status
);
331 rsp
->plain
.message
.header
.response
.bodylen
=
332 ntohl(rsp
->plain
.message
.header
.response
.bodylen
);
333 rsp
->plain
.message
.header
.response
.cas
=
334 ntohll(rsp
->plain
.message
.header
.response
.cas
);
336 size_t bodysz
= rsp
->plain
.message
.header
.response
.bodylen
;
338 execute(retry_read(rsp
->bytes
+ sizeof (protocol_binary_response_no_extras
), bodysz
));
344 * Create a storage command (add, set, replace etc)
346 * @param cmd destination buffer
347 * @param cc the storage command to create
348 * @param key the key to store
349 * @param keylen the length of the key
350 * @param dta the data to store with the key
351 * @param dtalen the length of the data to store with the key
352 * @param flags the flags to store along with the key
353 * @param exptime the expiry time for the key
355 static void storage_command(command
*cmd
,
364 /* all of the storage commands use the same command layout */
365 protocol_binary_request_set
*request
= &cmd
->set
;
367 memset(request
, 0, sizeof (*request
));
368 request
->message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
369 request
->message
.header
.request
.opcode
= cc
;
370 request
->message
.header
.request
.keylen
= (uint16_t)keylen
;
371 request
->message
.header
.request
.extlen
= 8;
372 request
->message
.header
.request
.bodylen
= (uint32_t)(keylen
+ 8 + dtalen
);
373 request
->message
.header
.request
.opaque
= 0xdeadbeef;
374 request
->message
.body
.flags
= flags
;
375 request
->message
.body
.expiration
= exptime
;
377 off_t key_offset
= sizeof (protocol_binary_request_no_extras
) + 8;
378 memcpy(cmd
->bytes
+ key_offset
, key
, keylen
);
380 memcpy(cmd
->bytes
+ key_offset
+ keylen
, dta
, dtalen
);
384 * Create a basic command to send to the server
385 * @param cmd destination buffer
386 * @param cc the command to create
387 * @param key the key to store
388 * @param keylen the length of the key
389 * @param dta the data to store with the key
390 * @param dtalen the length of the data to store with the key
392 static void raw_command(command
*cmd
,
399 /* all of the storage commands use the same command layout */
400 memset(cmd
, 0, sizeof (*cmd
));
401 cmd
->plain
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
402 cmd
->plain
.message
.header
.request
.opcode
= cc
;
403 cmd
->plain
.message
.header
.request
.keylen
= (uint16_t)keylen
;
404 cmd
->plain
.message
.header
.request
.bodylen
= (uint32_t)(keylen
+ dtalen
);
405 cmd
->plain
.message
.header
.request
.opaque
= 0xdeadbeef;
407 off_t key_offset
= sizeof (protocol_binary_request_no_extras
);
410 memcpy(cmd
->bytes
+ key_offset
, key
, keylen
);
413 memcpy(cmd
->bytes
+ key_offset
+ keylen
, dta
, dtalen
);
417 * Create the flush command
418 * @param cmd destination buffer
419 * @param cc the command to create (FLUSH/FLUSHQ)
420 * @param exptime when to flush
421 * @param use_extra to force using of the extra field?
423 static void flush_command(command
*cmd
,
424 uint8_t cc
, uint32_t exptime
, bool use_extra
)
426 memset(cmd
, 0, sizeof (cmd
->flush
));
427 cmd
->flush
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
428 cmd
->flush
.message
.header
.request
.opcode
= cc
;
429 cmd
->flush
.message
.header
.request
.opaque
= 0xdeadbeef;
431 if (exptime
!= 0 || use_extra
)
433 cmd
->flush
.message
.header
.request
.extlen
= 4;
434 cmd
->flush
.message
.body
.expiration
= htonl(exptime
);
435 cmd
->flush
.message
.header
.request
.bodylen
= 4;
440 * Create a incr/decr command
441 * @param cc the cmd to create (FLUSH/FLUSHQ)
442 * @param key the key to operate on
443 * @param keylen the number of bytes in the key
444 * @param delta the number to add/subtract
445 * @param initial the initial value if the key doesn't exist
446 * @param exptime when the key should expire if it isn't set
448 static void arithmetic_command(command
*cmd
,
456 memset(cmd
, 0, sizeof (cmd
->incr
));
457 cmd
->incr
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
458 cmd
->incr
.message
.header
.request
.opcode
= cc
;
459 cmd
->incr
.message
.header
.request
.keylen
= (uint16_t)keylen
;
460 cmd
->incr
.message
.header
.request
.extlen
= 20;
461 cmd
->incr
.message
.header
.request
.bodylen
= (uint32_t)(keylen
+ 20);
462 cmd
->incr
.message
.header
.request
.opaque
= 0xdeadbeef;
463 cmd
->incr
.message
.body
.delta
= htonll(delta
);
464 cmd
->incr
.message
.body
.initial
= htonll(initial
);
465 cmd
->incr
.message
.body
.expiration
= htonl(exptime
);
467 off_t key_offset
= sizeof (protocol_binary_request_no_extras
) + 20;
468 memcpy(cmd
->bytes
+ key_offset
, key
, keylen
);
472 * Validate the response header from the server
473 * @param rsp the response to check
474 * @param cc the expected command
475 * @param status the expected status
477 static enum test_return
do_validate_response_header(response
*rsp
,
478 uint8_t cc
, uint16_t status
)
480 verify(rsp
->plain
.message
.header
.response
.magic
== PROTOCOL_BINARY_RES
);
481 verify(rsp
->plain
.message
.header
.response
.opcode
== cc
);
482 verify(rsp
->plain
.message
.header
.response
.datatype
== PROTOCOL_BINARY_RAW_BYTES
);
483 verify(rsp
->plain
.message
.header
.response
.status
== status
);
484 verify(rsp
->plain
.message
.header
.response
.opaque
== 0xdeadbeef);
486 if (status
== PROTOCOL_BINARY_RESPONSE_SUCCESS
)
489 case PROTOCOL_BINARY_CMD_ADDQ
:
490 case PROTOCOL_BINARY_CMD_APPENDQ
:
491 case PROTOCOL_BINARY_CMD_DECREMENTQ
:
492 case PROTOCOL_BINARY_CMD_DELETEQ
:
493 case PROTOCOL_BINARY_CMD_FLUSHQ
:
494 case PROTOCOL_BINARY_CMD_INCREMENTQ
:
495 case PROTOCOL_BINARY_CMD_PREPENDQ
:
496 case PROTOCOL_BINARY_CMD_QUITQ
:
497 case PROTOCOL_BINARY_CMD_REPLACEQ
:
498 case PROTOCOL_BINARY_CMD_SETQ
:
499 verify("Quiet command shouldn't return on success" == NULL
);
505 case PROTOCOL_BINARY_CMD_ADD
:
506 case PROTOCOL_BINARY_CMD_REPLACE
:
507 case PROTOCOL_BINARY_CMD_SET
:
508 case PROTOCOL_BINARY_CMD_APPEND
:
509 case PROTOCOL_BINARY_CMD_PREPEND
:
510 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
511 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
512 verify(rsp
->plain
.message
.header
.response
.bodylen
== 0);
513 verify(rsp
->plain
.message
.header
.response
.cas
!= 0);
515 case PROTOCOL_BINARY_CMD_FLUSH
:
516 case PROTOCOL_BINARY_CMD_NOOP
:
517 case PROTOCOL_BINARY_CMD_QUIT
:
518 case PROTOCOL_BINARY_CMD_DELETE
:
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_DECREMENT
:
526 case PROTOCOL_BINARY_CMD_INCREMENT
:
527 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
528 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
529 verify(rsp
->plain
.message
.header
.response
.bodylen
== 8);
530 verify(rsp
->plain
.message
.header
.response
.cas
!= 0);
533 case PROTOCOL_BINARY_CMD_STAT
:
534 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
535 /* key and value exists in all packets except in the terminating */
536 verify(rsp
->plain
.message
.header
.response
.cas
== 0);
539 case PROTOCOL_BINARY_CMD_VERSION
:
540 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
541 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
542 verify(rsp
->plain
.message
.header
.response
.bodylen
!= 0);
543 verify(rsp
->plain
.message
.header
.response
.cas
== 0);
546 case PROTOCOL_BINARY_CMD_GET
:
547 case PROTOCOL_BINARY_CMD_GETQ
:
548 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
549 verify(rsp
->plain
.message
.header
.response
.extlen
== 4);
550 verify(rsp
->plain
.message
.header
.response
.cas
!= 0);
553 case PROTOCOL_BINARY_CMD_GETK
:
554 case PROTOCOL_BINARY_CMD_GETKQ
:
555 verify(rsp
->plain
.message
.header
.response
.keylen
!= 0);
556 verify(rsp
->plain
.message
.header
.response
.extlen
== 4);
557 verify(rsp
->plain
.message
.header
.response
.cas
!= 0);
561 /* Undefined command code */
567 verify(rsp
->plain
.message
.header
.response
.cas
== 0);
568 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
569 if (cc
!= PROTOCOL_BINARY_CMD_GETK
)
571 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
578 /* We call verify(validate_response_header), but that macro
579 * expects a boolean expression, and the function returns
580 * an enum.... Let's just create a macro to avoid cluttering
581 * the code with all of the == TEST_PASS ;-)
583 #define validate_response_header(a,b,c) \
584 do_validate_response_header(a,b,c) == TEST_PASS
587 static enum test_return
send_binary_noop(void)
590 raw_command(&cmd
, PROTOCOL_BINARY_CMD_NOOP
, NULL
, 0, NULL
, 0);
591 execute(send_packet(&cmd
));
595 static enum test_return
receive_binary_noop(void)
598 execute(recv_packet(&rsp
));
599 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_NOOP
,
600 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
604 static enum test_return
test_binary_noop(void)
606 execute(send_binary_noop());
607 execute(receive_binary_noop());
611 static enum test_return
test_binary_quit_impl(uint8_t cc
)
615 raw_command(&cmd
, cc
, NULL
, 0, NULL
, 0);
617 execute(send_packet(&cmd
));
618 if (cc
== PROTOCOL_BINARY_CMD_QUIT
)
620 execute(recv_packet(&rsp
));
621 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_QUIT
,
622 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
625 /* Socket should be closed now, read should return 0 */
626 verify(timeout_io_op(sock
, POLLIN
, rsp
.bytes
, sizeof(rsp
.bytes
)) == 0);
628 return TEST_PASS_RECONNECT
;
631 static enum test_return
test_binary_quit(void)
633 return test_binary_quit_impl(PROTOCOL_BINARY_CMD_QUIT
);
636 static enum test_return
test_binary_quitq(void)
638 return test_binary_quit_impl(PROTOCOL_BINARY_CMD_QUITQ
);
641 static enum test_return
test_binary_set_impl(const char* key
, uint8_t cc
)
646 uint64_t value
= 0xdeadbeefdeadcafe;
647 storage_command(&cmd
, cc
, key
, strlen(key
), &value
, sizeof (value
), 0, 0);
649 /* set should always work */
650 for (int ii
= 0; ii
< 10; ii
++)
653 execute(send_packet(&cmd
));
655 execute(resend_packet(&cmd
));
657 if (cc
== PROTOCOL_BINARY_CMD_SET
)
659 execute(recv_packet(&rsp
));
660 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
663 execute(test_binary_noop());
667 * We need to get the current CAS id, and at this time we haven't
668 * verified that we have a working get
670 if (cc
== PROTOCOL_BINARY_CMD_SETQ
)
672 cmd
.set
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_SET
;
673 execute(resend_packet(&cmd
));
674 execute(recv_packet(&rsp
));
675 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_SET
,
676 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
677 cmd
.set
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_SETQ
;
680 /* try to set with the correct CAS value */
681 cmd
.plain
.message
.header
.request
.cas
=
682 htonll(rsp
.plain
.message
.header
.response
.cas
);
683 execute(resend_packet(&cmd
));
684 if (cc
== PROTOCOL_BINARY_CMD_SET
)
686 execute(recv_packet(&rsp
));
687 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
690 execute(test_binary_noop());
692 /* try to set with an incorrect CAS value */
693 cmd
.plain
.message
.header
.request
.cas
=
694 htonll(rsp
.plain
.message
.header
.response
.cas
- 1);
695 execute(resend_packet(&cmd
));
696 execute(recv_packet(&rsp
));
697 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS
));
699 return test_binary_noop();
702 static enum test_return
test_binary_set(void)
704 return test_binary_set_impl("test_binary_set", PROTOCOL_BINARY_CMD_SET
);
707 static enum test_return
test_binary_setq(void)
709 return test_binary_set_impl("test_binary_setq", PROTOCOL_BINARY_CMD_SETQ
);
712 static enum test_return
test_binary_add_impl(const char* key
, uint8_t cc
)
716 uint64_t value
= 0xdeadbeefdeadcafe;
717 storage_command(&cmd
, cc
, key
, strlen(key
), &value
, sizeof (value
), 0, 0);
719 /* first add should work, rest of them should fail (even with cas
721 for (int ii
=0; ii
< 10; ii
++)
724 execute(send_packet(&cmd
));
726 execute(resend_packet(&cmd
));
728 if (cc
== PROTOCOL_BINARY_CMD_ADD
|| ii
> 0)
730 uint16_t expected_result
;
732 expected_result
= PROTOCOL_BINARY_RESPONSE_SUCCESS
;
734 expected_result
= PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS
;
736 execute(recv_packet(&rsp
));
737 verify(validate_response_header(&rsp
, cc
, expected_result
));
740 execute(test_binary_noop());
746 static enum test_return
test_binary_add(void)
748 return test_binary_add_impl("test_binary_add", PROTOCOL_BINARY_CMD_ADD
);
751 static enum test_return
test_binary_addq(void)
753 return test_binary_add_impl("test_binary_addq", PROTOCOL_BINARY_CMD_ADDQ
);
756 static enum test_return
binary_set_item(const char *key
, const char *value
)
760 storage_command(&cmd
, PROTOCOL_BINARY_CMD_SET
, key
, strlen(key
),
761 value
, strlen(value
), 0, 0);
762 execute(send_packet(&cmd
));
763 execute(recv_packet(&rsp
));
764 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_SET
,
765 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
769 static enum test_return
test_binary_replace_impl(const char* key
, uint8_t cc
)
773 uint64_t value
= 0xdeadbeefdeadcafe;
774 storage_command(&cmd
, cc
, key
, strlen(key
), &value
, sizeof (value
), 0, 0);
776 /* first replace should fail, successive should succeed (when the
778 for (int ii
= 0; ii
< 10; ii
++)
781 execute(send_packet(&cmd
));
783 execute(resend_packet(&cmd
));
785 if (cc
== PROTOCOL_BINARY_CMD_REPLACE
|| ii
== 0)
787 uint16_t expected_result
;
789 expected_result
=PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
;
791 expected_result
=PROTOCOL_BINARY_RESPONSE_SUCCESS
;
793 execute(recv_packet(&rsp
));
794 verify(validate_response_header(&rsp
, cc
, expected_result
));
797 execute(binary_set_item(key
, key
));
800 execute(test_binary_noop());
803 /* verify that replace with CAS value works! */
804 cmd
.plain
.message
.header
.request
.cas
=
805 htonll(rsp
.plain
.message
.header
.response
.cas
);
806 execute(resend_packet(&cmd
));
808 if (cc
== PROTOCOL_BINARY_CMD_REPLACE
)
810 execute(recv_packet(&rsp
));
811 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
814 execute(test_binary_noop());
816 /* try to set with an incorrect CAS value */
817 cmd
.plain
.message
.header
.request
.cas
=
818 htonll(rsp
.plain
.message
.header
.response
.cas
- 1);
819 execute(resend_packet(&cmd
));
820 execute(recv_packet(&rsp
));
821 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS
));
826 static enum test_return
test_binary_replace(void)
828 return test_binary_replace_impl("test_binary_replace", PROTOCOL_BINARY_CMD_REPLACE
);
831 static enum test_return
test_binary_replaceq(void)
833 return test_binary_replace_impl("test_binary_replaceq", PROTOCOL_BINARY_CMD_REPLACEQ
);
836 static enum test_return
test_binary_delete_impl(const char *key
, uint8_t cc
)
840 raw_command(&cmd
, cc
, key
, strlen(key
), NULL
, 0);
842 /* The delete shouldn't work the first time, because the item isn't there */
843 execute(send_packet(&cmd
));
844 execute(recv_packet(&rsp
));
845 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
));
846 execute(binary_set_item(key
, key
));
848 /* The item should be present now, resend*/
849 execute(resend_packet(&cmd
));
850 if (cc
== PROTOCOL_BINARY_CMD_DELETE
)
852 execute(recv_packet(&rsp
));
853 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
856 execute(test_binary_noop());
861 static enum test_return
test_binary_delete(void)
863 return test_binary_delete_impl("test_binary_delete", PROTOCOL_BINARY_CMD_DELETE
);
866 static enum test_return
test_binary_deleteq(void)
868 return test_binary_delete_impl("test_binary_deleteq", PROTOCOL_BINARY_CMD_DELETEQ
);
871 static enum test_return
test_binary_get_impl(const char *key
, uint8_t cc
)
876 raw_command(&cmd
, cc
, key
, strlen(key
), NULL
, 0);
877 execute(send_packet(&cmd
));
878 execute(send_binary_noop());
880 if (cc
== PROTOCOL_BINARY_CMD_GET
|| cc
== PROTOCOL_BINARY_CMD_GETK
)
882 execute(recv_packet(&rsp
));
883 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
));
886 execute(receive_binary_noop());
888 execute(binary_set_item(key
, key
));
889 execute(resend_packet(&cmd
));
890 execute(send_binary_noop());
892 execute(recv_packet(&rsp
));
893 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
894 execute(receive_binary_noop());
899 static enum test_return
test_binary_get(void)
901 return test_binary_get_impl("test_binary_get", PROTOCOL_BINARY_CMD_GET
);
904 static enum test_return
test_binary_getk(void)
906 return test_binary_get_impl("test_binary_getk", PROTOCOL_BINARY_CMD_GETK
);
909 static enum test_return
test_binary_getq(void)
911 return test_binary_get_impl("test_binary_getq", PROTOCOL_BINARY_CMD_GETQ
);
914 static enum test_return
test_binary_getkq(void)
916 return test_binary_get_impl("test_binary_getkq", PROTOCOL_BINARY_CMD_GETKQ
);
919 static enum test_return
test_binary_incr_impl(const char* key
, uint8_t cc
)
923 arithmetic_command(&cmd
, cc
, key
, strlen(key
), 1, 0, 0);
926 for (ii
= 0; ii
< 10; ++ii
)
929 execute(send_packet(&cmd
));
931 execute(resend_packet(&cmd
));
933 if (cc
== PROTOCOL_BINARY_CMD_INCREMENT
)
935 execute(recv_packet(&rsp
));
936 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
937 verify(ntohll(rsp
.incr
.message
.body
.value
) == ii
);
940 execute(test_binary_noop());
943 /* @todo add incorrect CAS */
947 static enum test_return
test_binary_incr(void)
949 return test_binary_incr_impl("test_binary_incr", PROTOCOL_BINARY_CMD_INCREMENT
);
952 static enum test_return
test_binary_incrq(void)
954 return test_binary_incr_impl("test_binary_incrq", PROTOCOL_BINARY_CMD_INCREMENTQ
);
957 static enum test_return
test_binary_decr_impl(const char* key
, uint8_t cc
)
961 arithmetic_command(&cmd
, cc
, key
, strlen(key
), 1, 9, 0);
964 for (ii
= 9; ii
> -1; --ii
)
967 execute(send_packet(&cmd
));
969 execute(resend_packet(&cmd
));
971 if (cc
== PROTOCOL_BINARY_CMD_DECREMENT
)
973 execute(recv_packet(&rsp
));
974 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
975 verify(ntohll(rsp
.decr
.message
.body
.value
) == (uint64_t)ii
);
978 execute(test_binary_noop());
981 /* decr 0 should not wrap */
982 execute(resend_packet(&cmd
));
983 if (cc
== PROTOCOL_BINARY_CMD_DECREMENT
)
985 execute(recv_packet(&rsp
));
986 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
987 verify(ntohll(rsp
.decr
.message
.body
.value
) == 0);
991 /* @todo get the value and verify! */
995 /* @todo add incorrect cas */
996 execute(test_binary_noop());
1000 static enum test_return
test_binary_decr(void)
1002 return test_binary_decr_impl("test_binary_decr",
1003 PROTOCOL_BINARY_CMD_DECREMENT
);
1006 static enum test_return
test_binary_decrq(void)
1008 return test_binary_decr_impl("test_binary_decrq",
1009 PROTOCOL_BINARY_CMD_DECREMENTQ
);
1012 static enum test_return
test_binary_version(void)
1016 raw_command(&cmd
, PROTOCOL_BINARY_CMD_VERSION
, NULL
, 0, NULL
, 0);
1018 execute(send_packet(&cmd
));
1019 execute(recv_packet(&rsp
));
1020 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_VERSION
,
1021 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1026 static enum test_return
test_binary_flush_impl(const char *key
, uint8_t cc
)
1031 for (int ii
= 0; ii
< 2; ++ii
)
1033 execute(binary_set_item(key
, key
));
1034 flush_command(&cmd
, cc
, 0, ii
== 0);
1035 execute(send_packet(&cmd
));
1037 if (cc
== PROTOCOL_BINARY_CMD_FLUSH
)
1039 execute(recv_packet(&rsp
));
1040 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1043 execute(test_binary_noop());
1045 raw_command(&cmd
, PROTOCOL_BINARY_CMD_GET
, key
, strlen(key
), NULL
, 0);
1046 execute(send_packet(&cmd
));
1047 execute(recv_packet(&rsp
));
1048 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_GET
,
1049 PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
));
1055 static enum test_return
test_binary_flush(void)
1057 return test_binary_flush_impl("test_binary_flush", PROTOCOL_BINARY_CMD_FLUSH
);
1060 static enum test_return
test_binary_flushq(void)
1062 return test_binary_flush_impl("test_binary_flushq", PROTOCOL_BINARY_CMD_FLUSHQ
);
1065 static enum test_return
test_binary_concat_impl(const char *key
, uint8_t cc
)
1071 if (cc
== PROTOCOL_BINARY_CMD_APPEND
|| cc
== PROTOCOL_BINARY_CMD_APPENDQ
)
1076 execute(binary_set_item(key
, value
));
1078 if (cc
== PROTOCOL_BINARY_CMD_APPEND
|| cc
== PROTOCOL_BINARY_CMD_APPENDQ
)
1083 raw_command(&cmd
, cc
, key
, strlen(key
), value
, strlen(value
));
1084 execute(send_packet(&cmd
));
1085 if (cc
== PROTOCOL_BINARY_CMD_APPEND
|| cc
== PROTOCOL_BINARY_CMD_PREPEND
)
1087 execute(recv_packet(&rsp
));
1088 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1091 execute(test_binary_noop());
1093 raw_command(&cmd
, PROTOCOL_BINARY_CMD_GET
, key
, strlen(key
), NULL
, 0);
1094 execute(send_packet(&cmd
));
1095 execute(recv_packet(&rsp
));
1096 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_GET
,
1097 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1098 verify(rsp
.plain
.message
.header
.response
.bodylen
- 4 == 11);
1099 verify(memcmp(rsp
.bytes
+ 28, "hello world", 11) == 0);
1104 static enum test_return
test_binary_append(void)
1106 return test_binary_concat_impl("test_binary_append", PROTOCOL_BINARY_CMD_APPEND
);
1109 static enum test_return
test_binary_prepend(void)
1111 return test_binary_concat_impl("test_binary_prepend", PROTOCOL_BINARY_CMD_PREPEND
);
1114 static enum test_return
test_binary_appendq(void)
1116 return test_binary_concat_impl("test_binary_appendq", PROTOCOL_BINARY_CMD_APPENDQ
);
1119 static enum test_return
test_binary_prependq(void)
1121 return test_binary_concat_impl("test_binary_prependq", PROTOCOL_BINARY_CMD_PREPENDQ
);
1124 static enum test_return
test_binary_stat(void)
1129 raw_command(&cmd
, PROTOCOL_BINARY_CMD_STAT
, NULL
, 0, NULL
, 0);
1130 execute(send_packet(&cmd
));
1134 execute(recv_packet(&rsp
));
1135 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_STAT
,
1136 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1137 } while (rsp
.plain
.message
.header
.response
.keylen
!= 0);
1142 static enum test_return
send_string(const char *cmd
)
1144 execute(retry_write(cmd
, strlen(cmd
)));
1148 static enum test_return
receive_line(char *buffer
, size_t size
)
1151 while (offset
< size
)
1153 execute(retry_read(buffer
+ offset
, 1));
1154 if (buffer
[offset
] == '\n')
1156 if (offset
+ 1 < size
)
1158 buffer
[offset
+ 1]= '\0';
1170 static enum test_return
receive_response(const char *msg
) {
1172 execute(receive_line(buffer
, sizeof(buffer
)));
1173 if (strcmp(msg
, buffer
) != 0) {
1174 fprintf(stderr
, "[%s]\n", buffer
);
1176 verify(strcmp(msg
, buffer
) == 0);
1180 static enum test_return
receive_error_response(void)
1183 execute(receive_line(buffer
, sizeof(buffer
)));
1184 verify(strncmp(buffer
, "ERROR", 5) == 0 ||
1185 strncmp(buffer
, "CLIENT_ERROR", 12) == 0 ||
1186 strncmp(buffer
, "SERVER_ERROR", 12) == 0);
1190 static enum test_return
test_ascii_quit(void)
1192 /* Verify that quit handles unknown options */
1193 execute(send_string("quit foo bar\r\n"));
1194 execute(receive_error_response());
1196 /* quit doesn't support noreply */
1197 execute(send_string("quit noreply\r\n"));
1198 execute(receive_error_response());
1200 /* Verify that quit works */
1201 execute(send_string("quit\r\n"));
1203 /* Socket should be closed now, read should return 0 */
1205 verify(timeout_io_op(sock
, POLLIN
, buffer
, sizeof(buffer
)) == 0);
1206 return TEST_PASS_RECONNECT
;
1210 static enum test_return
test_ascii_version(void)
1212 /* Verify that version command handles unknown options */
1213 execute(send_string("version foo bar\r\n"));
1214 execute(receive_error_response());
1216 /* version doesn't support noreply */
1217 execute(send_string("version noreply\r\n"));
1218 execute(receive_error_response());
1220 /* Verify that verify works */
1221 execute(send_string("version\r\n"));
1223 execute(receive_line(buffer
, sizeof(buffer
)));
1224 verify(strncmp(buffer
, "VERSION ", 8) == 0);
1229 static enum test_return
test_ascii_verbosity(void)
1231 /* This command does not adhere to the spec! */
1232 execute(send_string("verbosity foo bar my\r\n"));
1233 execute(receive_error_response());
1235 execute(send_string("verbosity noreply\r\n"));
1236 execute(receive_error_response());
1238 execute(send_string("verbosity 0 noreply\r\n"));
1239 execute(test_ascii_version());
1241 execute(send_string("verbosity\r\n"));
1242 execute(receive_error_response());
1244 execute(send_string("verbosity 1\r\n"));
1245 execute(receive_response("OK\r\n"));
1247 execute(send_string("verbosity 0\r\n"));
1248 execute(receive_response("OK\r\n"));
1255 static enum test_return
test_ascii_set_impl(const char* key
, bool noreply
)
1257 /* @todo add tests for bogus format! */
1259 sprintf(buffer
, "set %s 0 0 5%s\r\nvalue\r\n", key
,
1260 noreply
? " noreply" : "");
1261 execute(send_string(buffer
));
1264 execute(receive_response("STORED\r\n"));
1266 return test_ascii_version();
1269 static enum test_return
test_ascii_set(void)
1271 return test_ascii_set_impl("test_ascii_set", false);
1274 static enum test_return
test_ascii_set_noreply(void)
1276 return test_ascii_set_impl("test_ascii_set_noreply", true);
1279 static enum test_return
test_ascii_add_impl(const char* key
, bool noreply
)
1281 /* @todo add tests for bogus format! */
1283 sprintf(buffer
, "add %s 0 0 5%s\r\nvalue\r\n", key
,
1284 noreply
? " noreply" : "");
1285 execute(send_string(buffer
));
1288 execute(receive_response("STORED\r\n"));
1290 execute(send_string(buffer
));
1293 execute(receive_response("NOT_STORED\r\n"));
1295 return test_ascii_version();
1298 static enum test_return
test_ascii_add(void)
1300 return test_ascii_add_impl("test_ascii_add", false);
1303 static enum test_return
test_ascii_add_noreply(void)
1305 return test_ascii_add_impl("test_ascii_add_noreply", true);
1308 static enum test_return
ascii_get_value(const char *key
, const char *value
)
1312 size_t datasize
= strlen(value
);
1314 verify(datasize
< sizeof(buffer
));
1315 execute(receive_line(buffer
, sizeof(buffer
)));
1316 verify(strncmp(buffer
, "VALUE ", 6) == 0);
1317 verify(strncmp(buffer
+ 6, key
, strlen(key
)) == 0);
1318 char *ptr
= buffer
+ 6 + strlen(key
) + 1;
1321 unsigned long val
= strtoul(ptr
, &end
, 10); /* flags */
1324 verify(end
!= NULL
);
1325 val
= strtoul(end
, &end
, 10); /* size */
1327 verify(val
== datasize
);
1328 verify(end
!= NULL
);
1329 while (*end
!= '\n' && isspace(*end
))
1331 verify(*end
== '\n');
1333 execute(retry_read(buffer
, datasize
));
1334 verify(memcmp(buffer
, value
, datasize
) == 0);
1336 execute(retry_read(buffer
, 2));
1337 verify(memcmp(buffer
, "\r\n", 2) == 0);
1342 static enum test_return
ascii_get_item(const char *key
, const char *value
,
1348 datasize
= strlen(value
);
1350 verify(datasize
< sizeof(buffer
));
1351 sprintf(buffer
, "get %s\r\n", key
);
1352 execute(send_string(buffer
));
1355 execute(ascii_get_value(key
, value
));
1357 execute(retry_read(buffer
, 5));
1358 verify(memcmp(buffer
, "END\r\n", 5) == 0);
1363 static enum test_return
ascii_gets_value(const char *key
, const char *value
,
1368 size_t datasize
= strlen(value
);
1370 verify(datasize
< sizeof(buffer
));
1371 execute(receive_line(buffer
, sizeof(buffer
)));
1372 verify(strncmp(buffer
, "VALUE ", 6) == 0);
1373 verify(strncmp(buffer
+ 6, key
, strlen(key
)) == 0);
1374 char *ptr
= buffer
+ 6 + strlen(key
) + 1;
1377 unsigned long val
= strtoul(ptr
, &end
, 10); /* flags */
1380 verify(end
!= NULL
);
1381 val
= strtoul(end
, &end
, 10); /* size */
1383 verify(val
== datasize
);
1384 verify(end
!= NULL
);
1385 *cas
= strtoul(end
, &end
, 10); /* cas */
1387 verify(val
== datasize
);
1388 verify(end
!= NULL
);
1390 while (*end
!= '\n' && isspace(*end
))
1392 verify(*end
== '\n');
1394 execute(retry_read(buffer
, datasize
));
1395 verify(memcmp(buffer
, value
, datasize
) == 0);
1397 execute(retry_read(buffer
, 2));
1398 verify(memcmp(buffer
, "\r\n", 2) == 0);
1403 static enum test_return
ascii_gets_item(const char *key
, const char *value
,
1404 bool exist
, unsigned long *cas
)
1409 datasize
= strlen(value
);
1411 verify(datasize
< sizeof(buffer
));
1412 sprintf(buffer
, "gets %s\r\n", key
);
1413 execute(send_string(buffer
));
1416 execute(ascii_gets_value(key
, value
, cas
));
1418 execute(retry_read(buffer
, 5));
1419 verify(memcmp(buffer
, "END\r\n", 5) == 0);
1424 static enum test_return
ascii_set_item(const char *key
, const char *value
)
1427 size_t len
= strlen(value
);
1428 sprintf(buffer
, "set %s 0 0 %u\r\n", key
, (unsigned int)len
);
1429 execute(send_string(buffer
));
1430 execute(retry_write(value
, len
));
1431 execute(send_string("\r\n"));
1432 execute(receive_response("STORED\r\n"));
1436 static enum test_return
test_ascii_replace_impl(const char* key
, bool noreply
)
1439 sprintf(buffer
, "replace %s 0 0 5%s\r\nvalue\r\n", key
,
1440 noreply
? " noreply" : "");
1441 execute(send_string(buffer
));
1444 execute(test_ascii_version());
1446 execute(receive_response("NOT_STORED\r\n"));
1448 execute(ascii_set_item(key
, "value"));
1449 execute(ascii_get_item(key
, "value", true));
1452 execute(send_string(buffer
));
1455 execute(test_ascii_version());
1457 execute(receive_response("STORED\r\n"));
1459 return test_ascii_version();
1462 static enum test_return
test_ascii_replace(void)
1464 return test_ascii_replace_impl("test_ascii_replace", false);
1467 static enum test_return
test_ascii_replace_noreply(void)
1469 return test_ascii_replace_impl("test_ascii_replace_noreply", true);
1472 static enum test_return
test_ascii_cas_impl(const char* key
, bool noreply
)
1477 execute(ascii_set_item(key
, "value"));
1478 execute(ascii_gets_item(key
, "value", true, &cas
));
1480 sprintf(buffer
, "cas %s 0 0 6 %lu%s\r\nvalue2\r\n", key
, cas
,
1481 noreply
? " noreply" : "");
1482 execute(send_string(buffer
));
1485 execute(test_ascii_version());
1487 execute(receive_response("STORED\r\n"));
1489 /* reexecute the same command should fail due to illegal cas */
1490 execute(send_string(buffer
));
1493 execute(test_ascii_version());
1495 execute(receive_response("EXISTS\r\n"));
1497 return test_ascii_version();
1500 static enum test_return
test_ascii_cas(void)
1502 return test_ascii_cas_impl("test_ascii_cas", false);
1505 static enum test_return
test_ascii_cas_noreply(void)
1507 return test_ascii_cas_impl("test_ascii_cas_noreply", true);
1510 static enum test_return
test_ascii_delete_impl(const char *key
, bool noreply
)
1512 execute(ascii_set_item(key
, "value"));
1514 execute(send_string("delete\r\n"));
1515 execute(receive_error_response());
1516 /* BUG: the server accepts delete a b */
1517 execute(send_string("delete a b c d e\r\n"));
1518 execute(receive_error_response());
1521 sprintf(buffer
, "delete %s%s\r\n", key
, noreply
? " noreply" : "");
1522 execute(send_string(buffer
));
1525 execute(test_ascii_version());
1527 execute(receive_response("DELETED\r\n"));
1529 execute(ascii_get_item(key
, "value", false));
1530 execute(send_string(buffer
));
1532 execute(test_ascii_version());
1534 execute(receive_response("NOT_FOUND\r\n"));
1539 static enum test_return
test_ascii_delete(void)
1541 return test_ascii_delete_impl("test_ascii_delete", false);
1544 static enum test_return
test_ascii_delete_noreply(void)
1546 return test_ascii_delete_impl("test_ascii_delete_noreply", true);
1549 static enum test_return
test_ascii_get(void)
1551 execute(ascii_set_item("test_ascii_get", "value"));
1553 execute(send_string("get\r\n"));
1554 execute(receive_error_response());
1555 execute(ascii_get_item("test_ascii_get", "value", true));
1556 execute(ascii_get_item("test_ascii_get_notfound", "value", false));
1561 static enum test_return
test_ascii_gets(void)
1563 execute(ascii_set_item("test_ascii_gets", "value"));
1565 execute(send_string("gets\r\n"));
1566 execute(receive_error_response());
1568 execute(ascii_gets_item("test_ascii_gets", "value", true, &cas
));
1569 execute(ascii_gets_item("test_ascii_gets_notfound", "value", false, &cas
));
1574 static enum test_return
test_ascii_mget(void)
1576 execute(ascii_set_item("test_ascii_mget1", "value"));
1577 execute(ascii_set_item("test_ascii_mget2", "value"));
1578 execute(ascii_set_item("test_ascii_mget3", "value"));
1579 execute(ascii_set_item("test_ascii_mget4", "value"));
1580 execute(ascii_set_item("test_ascii_mget5", "value"));
1582 execute(send_string("get test_ascii_mget1 test_ascii_mget2 test_ascii_mget3 "
1583 "test_ascii_mget4 test_ascii_mget5 "
1584 "test_ascii_mget6\r\n"));
1585 execute(ascii_get_value("test_ascii_mget1", "value"));
1586 execute(ascii_get_value("test_ascii_mget2", "value"));
1587 execute(ascii_get_value("test_ascii_mget3", "value"));
1588 execute(ascii_get_value("test_ascii_mget4", "value"));
1589 execute(ascii_get_value("test_ascii_mget5", "value"));
1592 execute(retry_read(buffer
, 5));
1593 verify(memcmp(buffer
, "END\r\n", 5) == 0);
1597 static enum test_return
test_ascii_incr_impl(const char* key
, bool noreply
)
1600 sprintf(cmd
, "incr %s 1%s\r\n", key
, noreply
? " noreply" : "");
1602 execute(ascii_set_item(key
, "0"));
1603 for (int x
= 1; x
< 11; ++x
)
1605 execute(send_string(cmd
));
1608 execute(test_ascii_version());
1612 execute(receive_line(buffer
, sizeof(buffer
)));
1613 int val
= atoi(buffer
);
1618 execute(ascii_get_item(key
, "10", true));
1623 static enum test_return
test_ascii_incr(void)
1625 return test_ascii_incr_impl("test_ascii_incr", false);
1628 static enum test_return
test_ascii_incr_noreply(void)
1630 return test_ascii_incr_impl("test_ascii_incr_noreply", true);
1633 static enum test_return
test_ascii_decr_impl(const char* key
, bool noreply
)
1636 sprintf(cmd
, "decr %s 1%s\r\n", key
, noreply
? " noreply" : "");
1638 execute(ascii_set_item(key
, "9"));
1639 for (int x
= 8; x
> -1; --x
)
1641 execute(send_string(cmd
));
1644 execute(test_ascii_version());
1648 execute(receive_line(buffer
, sizeof(buffer
)));
1649 int val
= atoi(buffer
);
1654 execute(ascii_get_item(key
, "0", true));
1656 /* verify that it doesn't wrap */
1657 execute(send_string(cmd
));
1659 execute(test_ascii_version());
1663 execute(receive_line(buffer
, sizeof(buffer
)));
1665 execute(ascii_get_item(key
, "0", true));
1670 static enum test_return
test_ascii_decr(void)
1672 return test_ascii_decr_impl("test_ascii_decr", false);
1675 static enum test_return
test_ascii_decr_noreply(void)
1677 return test_ascii_decr_impl("test_ascii_decr_noreply", true);
1681 static enum test_return
test_ascii_flush_impl(const char *key
, bool noreply
)
1684 /* Verify that the flush_all command handles unknown options */
1685 /* Bug in the current memcached server! */
1686 execute(send_string("flush_all foo bar\r\n"));
1687 execute(receive_error_response());
1690 execute(ascii_set_item(key
, key
));
1691 execute(ascii_get_item(key
, key
, true));
1695 execute(send_string("flush_all noreply\r\n"));
1696 execute(test_ascii_version());
1700 execute(send_string("flush_all\r\n"));
1701 execute(receive_response("OK\r\n"));
1704 execute(ascii_get_item(key
, key
, false));
1709 static enum test_return
test_ascii_flush(void)
1711 return test_ascii_flush_impl("test_ascii_flush", false);
1714 static enum test_return
test_ascii_flush_noreply(void)
1716 return test_ascii_flush_impl("test_ascii_flush_noreply", true);
1719 static enum test_return
test_ascii_concat_impl(const char *key
,
1730 execute(ascii_set_item(key
, value
));
1738 sprintf(cmd
, "%s %s 0 0 %u%s\r\n%s\r\n",
1739 append
? "append" : "prepend",
1740 key
, (unsigned int)strlen(value
), noreply
? " noreply" : "",
1742 execute(send_string(cmd
));
1745 execute(test_ascii_version());
1747 execute(receive_response("STORED\r\n"));
1749 execute(ascii_get_item(key
, "hello world", true));
1751 sprintf(cmd
, "%s %s_notfound 0 0 %u%s\r\n%s\r\n",
1752 append
? "append" : "prepend",
1753 key
, (unsigned int)strlen(value
), noreply
? " noreply" : "",
1755 execute(send_string(cmd
));
1758 execute(test_ascii_version());
1760 execute(receive_response("NOT_STORED\r\n"));
1765 static enum test_return
test_ascii_append(void)
1767 return test_ascii_concat_impl("test_ascii_append", true, false);
1770 static enum test_return
test_ascii_prepend(void)
1772 return test_ascii_concat_impl("test_ascii_prepend", false, false);
1775 static enum test_return
test_ascii_append_noreply(void)
1777 return test_ascii_concat_impl("test_ascii_append_noreply", true, true);
1780 static enum test_return
test_ascii_prepend_noreply(void)
1782 return test_ascii_concat_impl("test_ascii_prepend_noreply", false, true);
1785 static enum test_return
test_ascii_stat(void)
1787 execute(send_string("stats noreply\r\n"));
1788 execute(receive_error_response());
1789 execute(send_string("stats\r\n"));
1792 execute(receive_line(buffer
, sizeof(buffer
)));
1793 } while (strcmp(buffer
, "END\r\n") != 0);
1795 return TEST_PASS_RECONNECT
;
1798 typedef enum test_return(*TEST_FUNC
)(void);
1802 const char *description
;
1806 struct testcase testcases
[]= {
1807 { "ascii quit", test_ascii_quit
},
1808 { "ascii version", test_ascii_version
},
1809 { "ascii verbosity", test_ascii_verbosity
},
1810 { "ascii set", test_ascii_set
},
1811 { "ascii set noreply", test_ascii_set_noreply
},
1812 { "ascii get", test_ascii_get
},
1813 { "ascii gets", test_ascii_gets
},
1814 { "ascii mget", test_ascii_mget
},
1815 { "ascii flush", test_ascii_flush
},
1816 { "ascii flush noreply", test_ascii_flush_noreply
},
1817 { "ascii add", test_ascii_add
},
1818 { "ascii add noreply", test_ascii_add_noreply
},
1819 { "ascii replace", test_ascii_replace
},
1820 { "ascii replace noreply", test_ascii_replace_noreply
},
1821 { "ascii cas", test_ascii_cas
},
1822 { "ascii cas noreply", test_ascii_cas_noreply
},
1823 { "ascii delete", test_ascii_delete
},
1824 { "ascii delete noreply", test_ascii_delete_noreply
},
1825 { "ascii incr", test_ascii_incr
},
1826 { "ascii incr noreply", test_ascii_incr_noreply
},
1827 { "ascii decr", test_ascii_decr
},
1828 { "ascii decr noreply", test_ascii_decr_noreply
},
1829 { "ascii append", test_ascii_append
},
1830 { "ascii append noreply", test_ascii_append_noreply
},
1831 { "ascii prepend", test_ascii_prepend
},
1832 { "ascii prepend noreply", test_ascii_prepend_noreply
},
1833 { "ascii stat", test_ascii_stat
},
1834 { "binary noop", test_binary_noop
},
1835 { "binary quit", test_binary_quit
},
1836 { "binary quitq", test_binary_quitq
},
1837 { "binary set", test_binary_set
},
1838 { "binary setq", test_binary_setq
},
1839 { "binary flush", test_binary_flush
},
1840 { "binary flushq", test_binary_flushq
},
1841 { "binary add", test_binary_add
},
1842 { "binary addq", test_binary_addq
},
1843 { "binary replace", test_binary_replace
},
1844 { "binary replaceq", test_binary_replaceq
},
1845 { "binary delete", test_binary_delete
},
1846 { "binary deleteq", test_binary_deleteq
},
1847 { "binary get", test_binary_get
},
1848 { "binary getq", test_binary_getq
},
1849 { "binary getk", test_binary_getk
},
1850 { "binary getkq", test_binary_getkq
},
1851 { "binary incr", test_binary_incr
},
1852 { "binary incrq", test_binary_incrq
},
1853 { "binary decr", test_binary_decr
},
1854 { "binary decrq", test_binary_decrq
},
1855 { "binary version", test_binary_version
},
1856 { "binary append", test_binary_append
},
1857 { "binary appendq", test_binary_appendq
},
1858 { "binary prepend", test_binary_prepend
},
1859 { "binary prependq", test_binary_prependq
},
1860 { "binary stat", test_binary_stat
},
1864 int main(int argc
, char **argv
)
1866 static const char * const status_msg
[]= {"[skip]", "[pass]", "[pass]", "[FAIL]"};
1869 const char *hostname
= "localhost";
1870 const char *port
= "11211";
1873 while ((cmd
= getopt(argc
, argv
, "t:vch:p:?")) != EOF
)
1877 timeout
= atoi(optarg
);
1880 fprintf(stderr
, "Invalid timeout. Please specify a number for -t\n");
1884 case 'v': verbose
= true;
1886 case 'c': do_core
= true;
1888 case 'h': hostname
= optarg
;
1890 case 'p': port
= optarg
;
1893 fprintf(stderr
, "Usage: %s [-h hostname] [-p port] [-c] [-v] [-t n]\n"
1894 "\t-c\tGenerate coredump if a test fails\n"
1895 "\t-v\tVerbose test output (print out the assertion)\n"
1896 "\t-t n\tSet the timeout for io-operations to n seconds\n",
1902 initialize_sockets();
1903 sock
= connect_server(hostname
, port
);
1904 if (sock
== INVALID_SOCKET
)
1906 fprintf(stderr
, "Failed to connect to <%s:%s>: %s\n",
1907 hostname
, port
, strerror(get_socket_errno()));
1911 for (int ii
= 0; testcases
[ii
].description
!= NULL
; ++ii
)
1914 fprintf(stdout
, "%-40s", testcases
[ii
].description
);
1917 bool reconnect
= false;
1918 enum test_return ret
= testcases
[ii
].function();
1919 if (ret
== TEST_FAIL
)
1924 fprintf(stderr
, "\n");
1926 else if (ret
== TEST_PASS_RECONNECT
)
1929 fprintf(stderr
, "%s\n", status_msg
[ret
]);
1933 if ((sock
= connect_server(hostname
, port
)) == INVALID_SOCKET
)
1935 fprintf(stderr
, "Failed to connect to <%s:%s>: %s\n",
1936 hostname
, port
, strerror(get_socket_errno()));
1937 fprintf(stderr
, "%d of %d tests failed\n", failed
, total
);
1945 fprintf(stdout
, "All tests passed\n");
1947 fprintf(stderr
, "%d of %d tests failed\n", failed
, total
);
1949 return (failed
== 0) ? 0 : 1;