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 -*- */
20 #include "poll/poll.h"
33 #include <sys/types.h>
36 #include <libmemcached/memcached.h>
37 #include <libmemcached/memcached/protocol_binary.h>
38 #include <libmemcached/byteorder.h>
39 #include <clients/utilities.h>
42 /* /usr/include/netinet/in.h defines macros from ntohs() to _bswap_nn to
43 * optimize the conversion functions, but the prototypes generate warnings
44 * from gcc. The conversion methods isn't the bottleneck for my app, so
45 * just remove the warnings by undef'ing the optimization ..
51 /* Should we generate coredumps when we enounter an error (-c) */
52 static bool do_core
= false;
53 /* connection to the server */
54 static memcached_socket_t sock
;
55 /* Should the output from test failures be verbose or quiet? */
56 static bool verbose
= false;
58 /* The number of seconds to wait for an IO-operation */
59 static int timeout
= 2;
62 * Instead of having to cast between the different datatypes we create
63 * a union of all of the different types of pacages we want to send.
64 * A lot of the different commands use the same packet layout, so I'll
65 * just define the different types I need. The typedefs only contain
66 * the header of the message, so we need some space for keys and body
67 * To avoid to have to do multiple writes, lets add a chunk of memory
68 * to use. 1k should be more than enough for header, key and body.
72 protocol_binary_request_no_extras plain
;
73 protocol_binary_request_flush flush
;
74 protocol_binary_request_incr incr
;
75 protocol_binary_request_set set
;
81 protocol_binary_response_no_extras plain
;
82 protocol_binary_response_incr incr
;
83 protocol_binary_response_decr decr
;
89 TEST_SKIP
, TEST_PASS
, TEST_PASS_RECONNECT
, TEST_FAIL
93 * Try to get an addrinfo struct for a given port on a given host
95 static struct addrinfo
*lookuphost(const char *hostname
, const char *port
)
97 struct addrinfo
*ai
= 0;
98 struct addrinfo hints
;
99 memset(&hints
, 0, sizeof(struct addrinfo
));
100 hints
.ai_family
=AF_UNSPEC
;
101 hints
.ai_protocol
=IPPROTO_TCP
;
102 hints
.ai_socktype
=SOCK_STREAM
;
104 int error
= getaddrinfo(hostname
, port
, &hints
, &ai
);
107 if (error
!= EAI_SYSTEM
)
108 fprintf(stderr
, "getaddrinfo(): %s\n", gai_strerror(error
));
110 perror("getaddrinfo()");
117 * Set the socket in nonblocking mode
118 * @return -1 if failure, the socket otherwise
120 static memcached_socket_t
set_noblock(void)
124 if (ioctlsocket(sock
, FIONBIO
, &arg
) == SOCKET_ERROR
)
126 perror("Failed to set nonblocking io");
128 return INVALID_SOCKET
;
131 int flags
= fcntl(sock
, F_GETFL
, 0);
134 perror("Failed to get socket flags");
136 return INVALID_SOCKET
;
139 if ((flags
& O_NONBLOCK
) != O_NONBLOCK
)
141 if (fcntl(sock
, F_SETFL
, flags
| O_NONBLOCK
) == -1)
143 perror("Failed to set socket to nonblocking mode");
145 return INVALID_SOCKET
;
153 * Try to open a connection to the server
154 * @param hostname the name of the server to connect to
155 * @param port the port number (or service) to connect to
156 * @return positive integer if success, -1 otherwise
158 static memcached_socket_t
connect_server(const char *hostname
, const char *port
)
160 struct addrinfo
*ai
= lookuphost(hostname
, port
);
161 sock
= INVALID_SOCKET
;
164 if ((sock
= socket(ai
->ai_family
, ai
->ai_socktype
,
165 ai
->ai_protocol
)) != INVALID_SOCKET
)
167 if (connect(sock
, ai
->ai_addr
, ai
->ai_addrlen
) == SOCKET_ERROR
)
169 fprintf(stderr
, "Failed to connect socket: %s\n",
170 strerror(get_socket_errno()));
172 sock
= INVALID_SOCKET
;
180 fprintf(stderr
, "Failed to create socket: %s\n",
181 strerror(get_socket_errno()));
189 static ssize_t
timeout_io_op(memcached_socket_t fd
, short direction
, void *buf
, size_t len
)
193 if (direction
== POLLOUT
)
195 ret
= send(fd
, buf
, len
, 0);
199 ret
= recv(fd
, buf
, len
, 0);
202 if (ret
== SOCKET_ERROR
&& get_socket_errno() == EWOULDBLOCK
)
205 memset(&fds
, 0, sizeof(struct pollfd
));
206 fds
.events
= direction
;
209 int err
= poll(&fds
, 1, timeout
* 1000);
212 if (direction
== POLLOUT
)
214 ret
= send(fd
, buf
, len
, 0);
218 ret
= recv(fd
, buf
, len
, 0);
227 perror("Failed to poll");
236 * Ensure that an expression is true. If it isn't print out a message similar
237 * to assert() and create a coredump if the user wants that. If not an error
238 * message is returned.
241 static enum test_return
ensure(bool val
, const char *expression
, const char *file
, int line
)
246 fprintf(stderr
, "\n%s:%d: %s", file
, line
, expression
);
257 #define verify(expression) do { if (ensure(expression, #expression, __FILE__, __LINE__) == TEST_FAIL) return TEST_FAIL; } while (0)
258 #define execute(expression) do { if (ensure(expression == TEST_PASS, #expression, __FILE__, __LINE__) == TEST_FAIL) return TEST_FAIL; } while (0)
261 * Send a chunk of memory over the socket (retry if the call is iterrupted
263 static enum test_return
retry_write(const void* buf
, size_t len
)
266 const char* ptr
= static_cast<const char *>(buf
);
270 size_t num_bytes
= len
- offset
;
271 ssize_t nw
= timeout_io_op(sock
, POLLOUT
, (void*)(ptr
+ offset
), num_bytes
);
273 verify(get_socket_errno() == EINTR
|| get_socket_errno() == EAGAIN
);
276 } while (offset
< len
);
282 * Resend a packet to the server (All fields in the command header should
283 * be in network byte order)
285 static enum test_return
resend_packet(command
*cmd
)
287 size_t length
= sizeof (protocol_binary_request_no_extras
) +
288 ntohl(cmd
->plain
.message
.header
.request
.bodylen
);
290 execute(retry_write(cmd
, length
));
295 * Send a command to the server. The command header needs to be updated
296 * to network byte order
298 static enum test_return
send_packet(command
*cmd
)
300 /* Fix the byteorder of the header */
301 cmd
->plain
.message
.header
.request
.keylen
=
302 ntohs(cmd
->plain
.message
.header
.request
.keylen
);
303 cmd
->plain
.message
.header
.request
.bodylen
=
304 ntohl(cmd
->plain
.message
.header
.request
.bodylen
);
305 cmd
->plain
.message
.header
.request
.cas
=
306 memcached_ntohll(cmd
->plain
.message
.header
.request
.cas
);
308 execute(resend_packet(cmd
));
313 * Read a fixed length chunk of data from the server
315 static enum test_return
retry_read(void *buf
, size_t len
)
320 ssize_t nr
= timeout_io_op(sock
, POLLIN
, ((char*) buf
) + offset
, len
- offset
);
323 fprintf(stderr
, "Errno: %d %s\n", get_socket_errno(), strerror(errno
));
324 verify(get_socket_errno() == EINTR
|| get_socket_errno() == EAGAIN
);
331 } while (offset
< len
);
337 * Receive a response from the server and conver the fields in the header
338 * to local byte order
340 static enum test_return
recv_packet(response
*rsp
)
342 execute(retry_read(rsp
, sizeof(protocol_binary_response_no_extras
)));
344 /* Fix the byte order in the packet header */
345 rsp
->plain
.message
.header
.response
.keylen
=
346 ntohs(rsp
->plain
.message
.header
.response
.keylen
);
347 rsp
->plain
.message
.header
.response
.status
=
348 ntohs(rsp
->plain
.message
.header
.response
.status
);
349 rsp
->plain
.message
.header
.response
.bodylen
=
350 ntohl(rsp
->plain
.message
.header
.response
.bodylen
);
351 rsp
->plain
.message
.header
.response
.cas
=
352 memcached_ntohll(rsp
->plain
.message
.header
.response
.cas
);
354 size_t bodysz
= rsp
->plain
.message
.header
.response
.bodylen
;
356 execute(retry_read(rsp
->bytes
+ sizeof (protocol_binary_response_no_extras
), bodysz
));
362 * Create a storage command (add, set, replace etc)
364 * @param cmd destination buffer
365 * @param cc the storage command to create
366 * @param key the key to store
367 * @param keylen the length of the key
368 * @param dta the data to store with the key
369 * @param dtalen the length of the data to store with the key
370 * @param flags the flags to store along with the key
371 * @param exptime the expiry time for the key
373 static void storage_command(command
*cmd
,
382 /* all of the storage commands use the same command layout */
383 protocol_binary_request_set
*request
= &cmd
->set
;
385 memset(request
, 0, sizeof (*request
));
386 request
->message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
387 request
->message
.header
.request
.opcode
= cc
;
388 request
->message
.header
.request
.keylen
= (uint16_t)keylen
;
389 request
->message
.header
.request
.extlen
= 8;
390 request
->message
.header
.request
.bodylen
= (uint32_t)(keylen
+ 8 + dtalen
);
391 request
->message
.header
.request
.opaque
= 0xdeadbeef;
392 request
->message
.body
.flags
= flags
;
393 request
->message
.body
.expiration
= exptime
;
395 off_t key_offset
= sizeof (protocol_binary_request_no_extras
) + 8;
396 memcpy(cmd
->bytes
+ key_offset
, key
, keylen
);
398 memcpy(cmd
->bytes
+ key_offset
+ keylen
, dta
, dtalen
);
402 * Create a basic command to send to the server
403 * @param cmd destination buffer
404 * @param cc the command to create
405 * @param key the key to store
406 * @param keylen the length of the key
407 * @param dta the data to store with the key
408 * @param dtalen the length of the data to store with the key
410 static void raw_command(command
*cmd
,
417 /* all of the storage commands use the same command layout */
418 memset(cmd
, 0, sizeof (*cmd
));
419 cmd
->plain
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
420 cmd
->plain
.message
.header
.request
.opcode
= cc
;
421 cmd
->plain
.message
.header
.request
.keylen
= (uint16_t)keylen
;
422 cmd
->plain
.message
.header
.request
.bodylen
= (uint32_t)(keylen
+ dtalen
);
423 cmd
->plain
.message
.header
.request
.opaque
= 0xdeadbeef;
425 off_t key_offset
= sizeof (protocol_binary_request_no_extras
);
428 memcpy(cmd
->bytes
+ key_offset
, key
, keylen
);
431 memcpy(cmd
->bytes
+ key_offset
+ keylen
, dta
, dtalen
);
435 * Create the flush command
436 * @param cmd destination buffer
437 * @param cc the command to create (FLUSH/FLUSHQ)
438 * @param exptime when to flush
439 * @param use_extra to force using of the extra field?
441 static void flush_command(command
*cmd
,
442 uint8_t cc
, uint32_t exptime
, bool use_extra
)
444 memset(cmd
, 0, sizeof (cmd
->flush
));
445 cmd
->flush
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
446 cmd
->flush
.message
.header
.request
.opcode
= cc
;
447 cmd
->flush
.message
.header
.request
.opaque
= 0xdeadbeef;
449 if (exptime
!= 0 || use_extra
)
451 cmd
->flush
.message
.header
.request
.extlen
= 4;
452 cmd
->flush
.message
.body
.expiration
= htonl(exptime
);
453 cmd
->flush
.message
.header
.request
.bodylen
= 4;
458 * Create a incr/decr command
459 * @param cc the cmd to create (FLUSH/FLUSHQ)
460 * @param key the key to operate on
461 * @param keylen the number of bytes in the key
462 * @param delta the number to add/subtract
463 * @param initial the initial value if the key doesn't exist
464 * @param exptime when the key should expire if it isn't set
466 static void arithmetic_command(command
*cmd
,
474 memset(cmd
, 0, sizeof (cmd
->incr
));
475 cmd
->incr
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
476 cmd
->incr
.message
.header
.request
.opcode
= cc
;
477 cmd
->incr
.message
.header
.request
.keylen
= (uint16_t)keylen
;
478 cmd
->incr
.message
.header
.request
.extlen
= 20;
479 cmd
->incr
.message
.header
.request
.bodylen
= (uint32_t)(keylen
+ 20);
480 cmd
->incr
.message
.header
.request
.opaque
= 0xdeadbeef;
481 cmd
->incr
.message
.body
.delta
= memcached_htonll(delta
);
482 cmd
->incr
.message
.body
.initial
= memcached_htonll(initial
);
483 cmd
->incr
.message
.body
.expiration
= htonl(exptime
);
485 off_t key_offset
= sizeof (protocol_binary_request_no_extras
) + 20;
486 memcpy(cmd
->bytes
+ key_offset
, key
, keylen
);
490 * Validate the response header from the server
491 * @param rsp the response to check
492 * @param cc the expected command
493 * @param status the expected status
495 static enum test_return
do_validate_response_header(response
*rsp
,
496 uint8_t cc
, uint16_t status
)
498 verify(rsp
->plain
.message
.header
.response
.magic
== PROTOCOL_BINARY_RES
);
499 verify(rsp
->plain
.message
.header
.response
.opcode
== cc
);
500 verify(rsp
->plain
.message
.header
.response
.datatype
== PROTOCOL_BINARY_RAW_BYTES
);
501 verify(rsp
->plain
.message
.header
.response
.status
== status
);
502 verify(rsp
->plain
.message
.header
.response
.opaque
== 0xdeadbeef);
504 if (status
== PROTOCOL_BINARY_RESPONSE_SUCCESS
)
507 case PROTOCOL_BINARY_CMD_ADDQ
:
508 case PROTOCOL_BINARY_CMD_APPENDQ
:
509 case PROTOCOL_BINARY_CMD_DECREMENTQ
:
510 case PROTOCOL_BINARY_CMD_DELETEQ
:
511 case PROTOCOL_BINARY_CMD_FLUSHQ
:
512 case PROTOCOL_BINARY_CMD_INCREMENTQ
:
513 case PROTOCOL_BINARY_CMD_PREPENDQ
:
514 case PROTOCOL_BINARY_CMD_QUITQ
:
515 case PROTOCOL_BINARY_CMD_REPLACEQ
:
516 case PROTOCOL_BINARY_CMD_SETQ
:
517 verify("Quiet command shouldn't return on success" == NULL
);
523 case PROTOCOL_BINARY_CMD_ADD
:
524 case PROTOCOL_BINARY_CMD_REPLACE
:
525 case PROTOCOL_BINARY_CMD_SET
:
526 case PROTOCOL_BINARY_CMD_APPEND
:
527 case PROTOCOL_BINARY_CMD_PREPEND
:
528 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
529 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
530 verify(rsp
->plain
.message
.header
.response
.bodylen
== 0);
531 verify(rsp
->plain
.message
.header
.response
.cas
!= 0);
533 case PROTOCOL_BINARY_CMD_FLUSH
:
534 case PROTOCOL_BINARY_CMD_NOOP
:
535 case PROTOCOL_BINARY_CMD_QUIT
:
536 case PROTOCOL_BINARY_CMD_DELETE
:
537 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
538 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
539 verify(rsp
->plain
.message
.header
.response
.bodylen
== 0);
540 verify(rsp
->plain
.message
.header
.response
.cas
== 0);
543 case PROTOCOL_BINARY_CMD_DECREMENT
:
544 case PROTOCOL_BINARY_CMD_INCREMENT
:
545 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
546 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
547 verify(rsp
->plain
.message
.header
.response
.bodylen
== 8);
548 verify(rsp
->plain
.message
.header
.response
.cas
!= 0);
551 case PROTOCOL_BINARY_CMD_STAT
:
552 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
553 /* key and value exists in all packets except in the terminating */
554 verify(rsp
->plain
.message
.header
.response
.cas
== 0);
557 case PROTOCOL_BINARY_CMD_VERSION
:
558 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
559 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
560 verify(rsp
->plain
.message
.header
.response
.bodylen
!= 0);
561 verify(rsp
->plain
.message
.header
.response
.cas
== 0);
564 case PROTOCOL_BINARY_CMD_GET
:
565 case PROTOCOL_BINARY_CMD_GETQ
:
566 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
567 verify(rsp
->plain
.message
.header
.response
.extlen
== 4);
568 verify(rsp
->plain
.message
.header
.response
.cas
!= 0);
571 case PROTOCOL_BINARY_CMD_GETK
:
572 case PROTOCOL_BINARY_CMD_GETKQ
:
573 verify(rsp
->plain
.message
.header
.response
.keylen
!= 0);
574 verify(rsp
->plain
.message
.header
.response
.extlen
== 4);
575 verify(rsp
->plain
.message
.header
.response
.cas
!= 0);
579 /* Undefined command code */
585 verify(rsp
->plain
.message
.header
.response
.cas
== 0);
586 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
587 if (cc
!= PROTOCOL_BINARY_CMD_GETK
)
589 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
596 /* We call verify(validate_response_header), but that macro
597 * expects a boolean expression, and the function returns
598 * an enum.... Let's just create a macro to avoid cluttering
599 * the code with all of the == TEST_PASS ;-)
601 #define validate_response_header(a,b,c) \
602 do_validate_response_header(a,b,c) == TEST_PASS
605 static enum test_return
send_binary_noop(void)
608 raw_command(&cmd
, PROTOCOL_BINARY_CMD_NOOP
, NULL
, 0, NULL
, 0);
609 execute(send_packet(&cmd
));
613 static enum test_return
receive_binary_noop(void)
616 execute(recv_packet(&rsp
));
617 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_NOOP
,
618 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
622 static enum test_return
test_binary_noop(void)
624 execute(send_binary_noop());
625 execute(receive_binary_noop());
629 static enum test_return
test_binary_quit_impl(uint8_t cc
)
633 raw_command(&cmd
, cc
, NULL
, 0, NULL
, 0);
635 execute(send_packet(&cmd
));
636 if (cc
== PROTOCOL_BINARY_CMD_QUIT
)
638 execute(recv_packet(&rsp
));
639 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_QUIT
,
640 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
643 /* Socket should be closed now, read should return EXIT_SUCCESS */
644 verify(timeout_io_op(sock
, POLLIN
, rsp
.bytes
, sizeof(rsp
.bytes
)) == 0);
646 return TEST_PASS_RECONNECT
;
649 static enum test_return
test_binary_quit(void)
651 return test_binary_quit_impl(PROTOCOL_BINARY_CMD_QUIT
);
654 static enum test_return
test_binary_quitq(void)
656 return test_binary_quit_impl(PROTOCOL_BINARY_CMD_QUITQ
);
659 static enum test_return
test_binary_set_impl(const char* key
, uint8_t cc
)
664 uint64_t value
= 0xdeadbeefdeadcafeULL
;
665 storage_command(&cmd
, cc
, key
, strlen(key
), &value
, sizeof (value
), 0, 0);
667 /* set should always work */
668 for (int ii
= 0; ii
< 10; ii
++)
671 execute(send_packet(&cmd
));
673 execute(resend_packet(&cmd
));
675 if (cc
== PROTOCOL_BINARY_CMD_SET
)
677 execute(recv_packet(&rsp
));
678 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
681 execute(test_binary_noop());
685 * We need to get the current CAS id, and at this time we haven't
686 * verified that we have a working get
688 if (cc
== PROTOCOL_BINARY_CMD_SETQ
)
690 cmd
.set
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_SET
;
691 execute(resend_packet(&cmd
));
692 execute(recv_packet(&rsp
));
693 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_SET
,
694 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
695 cmd
.set
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_SETQ
;
698 /* try to set with the correct CAS value */
699 cmd
.plain
.message
.header
.request
.cas
= memcached_htonll(rsp
.plain
.message
.header
.response
.cas
);
700 execute(resend_packet(&cmd
));
701 if (cc
== PROTOCOL_BINARY_CMD_SET
)
703 execute(recv_packet(&rsp
));
704 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
707 execute(test_binary_noop());
709 /* try to set with an incorrect CAS value */
710 cmd
.plain
.message
.header
.request
.cas
= memcached_htonll(rsp
.plain
.message
.header
.response
.cas
- 1);
711 execute(resend_packet(&cmd
));
712 execute(send_binary_noop());
713 execute(recv_packet(&rsp
));
714 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS
));
715 execute(receive_binary_noop());
720 static enum test_return
test_binary_set(void)
722 return test_binary_set_impl("test_binary_set", PROTOCOL_BINARY_CMD_SET
);
725 static enum test_return
test_binary_setq(void)
727 return test_binary_set_impl("test_binary_setq", PROTOCOL_BINARY_CMD_SETQ
);
730 static enum test_return
test_binary_add_impl(const char* key
, uint8_t cc
)
734 uint64_t value
= 0xdeadbeefdeadcafeULL
;
735 storage_command(&cmd
, cc
, key
, strlen(key
), &value
, sizeof (value
), 0, 0);
737 /* first add should work, rest of them should fail (even with cas
739 for (int ii
=0; ii
< 10; ii
++)
742 execute(send_packet(&cmd
));
744 execute(resend_packet(&cmd
));
746 if (cc
== PROTOCOL_BINARY_CMD_ADD
|| ii
> 0)
748 uint16_t expected_result
;
750 expected_result
= PROTOCOL_BINARY_RESPONSE_SUCCESS
;
752 expected_result
= PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS
;
754 execute(send_binary_noop());
755 execute(recv_packet(&rsp
));
756 execute(receive_binary_noop());
757 verify(validate_response_header(&rsp
, cc
, expected_result
));
760 execute(test_binary_noop());
766 static enum test_return
test_binary_add(void)
768 return test_binary_add_impl("test_binary_add", PROTOCOL_BINARY_CMD_ADD
);
771 static enum test_return
test_binary_addq(void)
773 return test_binary_add_impl("test_binary_addq", PROTOCOL_BINARY_CMD_ADDQ
);
776 static enum test_return
binary_set_item(const char *key
, const char *value
)
780 storage_command(&cmd
, PROTOCOL_BINARY_CMD_SET
, key
, strlen(key
),
781 value
, strlen(value
), 0, 0);
782 execute(send_packet(&cmd
));
783 execute(recv_packet(&rsp
));
784 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_SET
,
785 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
789 static enum test_return
test_binary_replace_impl(const char* key
, uint8_t cc
)
793 uint64_t value
= 0xdeadbeefdeadcafeULL
;
794 storage_command(&cmd
, cc
, key
, strlen(key
), &value
, sizeof (value
), 0, 0);
796 /* first replace should fail, successive should succeed (when the
798 for (int ii
= 0; ii
< 10; ii
++)
801 execute(send_packet(&cmd
));
803 execute(resend_packet(&cmd
));
805 if (cc
== PROTOCOL_BINARY_CMD_REPLACE
|| ii
== 0)
807 uint16_t expected_result
;
809 expected_result
=PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
;
811 expected_result
=PROTOCOL_BINARY_RESPONSE_SUCCESS
;
813 execute(send_binary_noop());
814 execute(recv_packet(&rsp
));
815 execute(receive_binary_noop());
816 verify(validate_response_header(&rsp
, cc
, expected_result
));
819 execute(binary_set_item(key
, key
));
822 execute(test_binary_noop());
825 /* verify that replace with CAS value works! */
826 cmd
.plain
.message
.header
.request
.cas
= memcached_htonll(rsp
.plain
.message
.header
.response
.cas
);
827 execute(resend_packet(&cmd
));
829 if (cc
== PROTOCOL_BINARY_CMD_REPLACE
)
831 execute(recv_packet(&rsp
));
832 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
835 execute(test_binary_noop());
837 /* try to set with an incorrect CAS value */
838 cmd
.plain
.message
.header
.request
.cas
= memcached_htonll(rsp
.plain
.message
.header
.response
.cas
- 1);
839 execute(resend_packet(&cmd
));
840 execute(send_binary_noop());
841 execute(recv_packet(&rsp
));
842 execute(receive_binary_noop());
843 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS
));
848 static enum test_return
test_binary_replace(void)
850 return test_binary_replace_impl("test_binary_replace", PROTOCOL_BINARY_CMD_REPLACE
);
853 static enum test_return
test_binary_replaceq(void)
855 return test_binary_replace_impl("test_binary_replaceq", PROTOCOL_BINARY_CMD_REPLACEQ
);
858 static enum test_return
test_binary_delete_impl(const char *key
, uint8_t cc
)
862 raw_command(&cmd
, cc
, key
, strlen(key
), NULL
, 0);
864 /* The delete shouldn't work the first time, because the item isn't there */
865 execute(send_packet(&cmd
));
866 execute(send_binary_noop());
867 execute(recv_packet(&rsp
));
868 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
));
869 execute(receive_binary_noop());
870 execute(binary_set_item(key
, key
));
872 /* The item should be present now, resend*/
873 execute(resend_packet(&cmd
));
874 if (cc
== PROTOCOL_BINARY_CMD_DELETE
)
876 execute(recv_packet(&rsp
));
877 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
880 execute(test_binary_noop());
885 static enum test_return
test_binary_delete(void)
887 return test_binary_delete_impl("test_binary_delete", PROTOCOL_BINARY_CMD_DELETE
);
890 static enum test_return
test_binary_deleteq(void)
892 return test_binary_delete_impl("test_binary_deleteq", PROTOCOL_BINARY_CMD_DELETEQ
);
895 static enum test_return
test_binary_get_impl(const char *key
, uint8_t cc
)
900 raw_command(&cmd
, cc
, key
, strlen(key
), NULL
, 0);
901 execute(send_packet(&cmd
));
902 execute(send_binary_noop());
904 if (cc
== PROTOCOL_BINARY_CMD_GET
|| cc
== PROTOCOL_BINARY_CMD_GETK
)
906 execute(recv_packet(&rsp
));
907 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
));
910 execute(receive_binary_noop());
912 execute(binary_set_item(key
, key
));
913 execute(resend_packet(&cmd
));
914 execute(send_binary_noop());
916 execute(recv_packet(&rsp
));
917 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
918 execute(receive_binary_noop());
923 static enum test_return
test_binary_get(void)
925 return test_binary_get_impl("test_binary_get", PROTOCOL_BINARY_CMD_GET
);
928 static enum test_return
test_binary_getk(void)
930 return test_binary_get_impl("test_binary_getk", PROTOCOL_BINARY_CMD_GETK
);
933 static enum test_return
test_binary_getq(void)
935 return test_binary_get_impl("test_binary_getq", PROTOCOL_BINARY_CMD_GETQ
);
938 static enum test_return
test_binary_getkq(void)
940 return test_binary_get_impl("test_binary_getkq", PROTOCOL_BINARY_CMD_GETKQ
);
943 static enum test_return
test_binary_incr_impl(const char* key
, uint8_t cc
)
947 arithmetic_command(&cmd
, cc
, key
, strlen(key
), 1, 0, 0);
950 for (ii
= 0; ii
< 10; ++ii
)
953 execute(send_packet(&cmd
));
955 execute(resend_packet(&cmd
));
957 if (cc
== PROTOCOL_BINARY_CMD_INCREMENT
)
959 execute(recv_packet(&rsp
));
960 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
961 verify(memcached_ntohll(rsp
.incr
.message
.body
.value
) == ii
);
964 execute(test_binary_noop());
967 /* @todo add incorrect CAS */
971 static enum test_return
test_binary_incr(void)
973 return test_binary_incr_impl("test_binary_incr", PROTOCOL_BINARY_CMD_INCREMENT
);
976 static enum test_return
test_binary_incrq(void)
978 return test_binary_incr_impl("test_binary_incrq", PROTOCOL_BINARY_CMD_INCREMENTQ
);
981 static enum test_return
test_binary_decr_impl(const char* key
, uint8_t cc
)
985 arithmetic_command(&cmd
, cc
, key
, strlen(key
), 1, 9, 0);
988 for (ii
= 9; ii
> -1; --ii
)
991 execute(send_packet(&cmd
));
993 execute(resend_packet(&cmd
));
995 if (cc
== PROTOCOL_BINARY_CMD_DECREMENT
)
997 execute(recv_packet(&rsp
));
998 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
999 verify(memcached_ntohll(rsp
.decr
.message
.body
.value
) == (uint64_t)ii
);
1002 execute(test_binary_noop());
1005 /* decr 0 should not wrap */
1006 execute(resend_packet(&cmd
));
1007 if (cc
== PROTOCOL_BINARY_CMD_DECREMENT
)
1009 execute(recv_packet(&rsp
));
1010 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1011 verify(memcached_ntohll(rsp
.decr
.message
.body
.value
) == 0);
1015 /* @todo get the value and verify! */
1019 /* @todo add incorrect cas */
1020 execute(test_binary_noop());
1024 static enum test_return
test_binary_decr(void)
1026 return test_binary_decr_impl("test_binary_decr",
1027 PROTOCOL_BINARY_CMD_DECREMENT
);
1030 static enum test_return
test_binary_decrq(void)
1032 return test_binary_decr_impl("test_binary_decrq",
1033 PROTOCOL_BINARY_CMD_DECREMENTQ
);
1036 static enum test_return
test_binary_version(void)
1040 raw_command(&cmd
, PROTOCOL_BINARY_CMD_VERSION
, NULL
, 0, NULL
, 0);
1042 execute(send_packet(&cmd
));
1043 execute(recv_packet(&rsp
));
1044 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_VERSION
,
1045 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1050 static enum test_return
test_binary_flush_impl(const char *key
, uint8_t cc
)
1055 for (int ii
= 0; ii
< 2; ++ii
)
1057 execute(binary_set_item(key
, key
));
1058 flush_command(&cmd
, cc
, 0, ii
== 0);
1059 execute(send_packet(&cmd
));
1061 if (cc
== PROTOCOL_BINARY_CMD_FLUSH
)
1063 execute(recv_packet(&rsp
));
1064 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1067 execute(test_binary_noop());
1069 raw_command(&cmd
, PROTOCOL_BINARY_CMD_GET
, key
, strlen(key
), NULL
, 0);
1070 execute(send_packet(&cmd
));
1071 execute(recv_packet(&rsp
));
1072 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_GET
,
1073 PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
));
1079 static enum test_return
test_binary_flush(void)
1081 return test_binary_flush_impl("test_binary_flush", PROTOCOL_BINARY_CMD_FLUSH
);
1084 static enum test_return
test_binary_flushq(void)
1086 return test_binary_flush_impl("test_binary_flushq", PROTOCOL_BINARY_CMD_FLUSHQ
);
1089 static enum test_return
test_binary_concat_impl(const char *key
, uint8_t cc
)
1095 if (cc
== PROTOCOL_BINARY_CMD_APPEND
|| cc
== PROTOCOL_BINARY_CMD_APPENDQ
)
1100 execute(binary_set_item(key
, value
));
1102 if (cc
== PROTOCOL_BINARY_CMD_APPEND
|| cc
== PROTOCOL_BINARY_CMD_APPENDQ
)
1107 raw_command(&cmd
, cc
, key
, strlen(key
), value
, strlen(value
));
1108 execute(send_packet(&cmd
));
1109 if (cc
== PROTOCOL_BINARY_CMD_APPEND
|| cc
== PROTOCOL_BINARY_CMD_PREPEND
)
1111 execute(recv_packet(&rsp
));
1112 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1115 execute(test_binary_noop());
1117 raw_command(&cmd
, PROTOCOL_BINARY_CMD_GET
, key
, strlen(key
), NULL
, 0);
1118 execute(send_packet(&cmd
));
1119 execute(recv_packet(&rsp
));
1120 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_GET
,
1121 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1122 verify(rsp
.plain
.message
.header
.response
.bodylen
- 4 == 11);
1123 verify(memcmp(rsp
.bytes
+ 28, "hello world", 11) == 0);
1128 static enum test_return
test_binary_append(void)
1130 return test_binary_concat_impl("test_binary_append", PROTOCOL_BINARY_CMD_APPEND
);
1133 static enum test_return
test_binary_prepend(void)
1135 return test_binary_concat_impl("test_binary_prepend", PROTOCOL_BINARY_CMD_PREPEND
);
1138 static enum test_return
test_binary_appendq(void)
1140 return test_binary_concat_impl("test_binary_appendq", PROTOCOL_BINARY_CMD_APPENDQ
);
1143 static enum test_return
test_binary_prependq(void)
1145 return test_binary_concat_impl("test_binary_prependq", PROTOCOL_BINARY_CMD_PREPENDQ
);
1148 static enum test_return
test_binary_stat(void)
1153 raw_command(&cmd
, PROTOCOL_BINARY_CMD_STAT
, NULL
, 0, NULL
, 0);
1154 execute(send_packet(&cmd
));
1158 execute(recv_packet(&rsp
));
1159 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_STAT
,
1160 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1161 } while (rsp
.plain
.message
.header
.response
.keylen
!= 0);
1166 static enum test_return
send_string(const char *cmd
)
1168 execute(retry_write(cmd
, strlen(cmd
)));
1172 static enum test_return
receive_line(char *buffer
, size_t size
)
1175 while (offset
< size
)
1177 execute(retry_read(buffer
+ offset
, 1));
1178 if (buffer
[offset
] == '\n')
1180 if (offset
+ 1 < size
)
1182 buffer
[offset
+ 1]= '\0';
1194 static enum test_return
receive_response(const char *msg
) {
1196 execute(receive_line(buffer
, sizeof(buffer
)));
1197 if (strcmp(msg
, buffer
) != 0) {
1198 fprintf(stderr
, "[%s]\n", buffer
);
1200 verify(strcmp(msg
, buffer
) == 0);
1204 static enum test_return
receive_error_response(void)
1207 execute(receive_line(buffer
, sizeof(buffer
)));
1208 verify(strncmp(buffer
, "ERROR", 5) == 0 ||
1209 strncmp(buffer
, "CLIENT_ERROR", 12) == 0 ||
1210 strncmp(buffer
, "SERVER_ERROR", 12) == 0);
1214 static enum test_return
test_ascii_quit(void)
1216 /* Verify that quit handles unknown options */
1217 execute(send_string("quit foo bar\r\n"));
1218 execute(receive_error_response());
1220 /* quit doesn't support noreply */
1221 execute(send_string("quit noreply\r\n"));
1222 execute(receive_error_response());
1224 /* Verify that quit works */
1225 execute(send_string("quit\r\n"));
1227 /* Socket should be closed now, read should return EXIT_SUCCESS */
1229 verify(timeout_io_op(sock
, POLLIN
, buffer
, sizeof(buffer
)) == 0);
1230 return TEST_PASS_RECONNECT
;
1234 static enum test_return
test_ascii_version(void)
1236 /* Verify that version command handles unknown options */
1237 execute(send_string("version foo bar\r\n"));
1238 execute(receive_error_response());
1240 /* version doesn't support noreply */
1241 execute(send_string("version noreply\r\n"));
1242 execute(receive_error_response());
1244 /* Verify that verify works */
1245 execute(send_string("version\r\n"));
1247 execute(receive_line(buffer
, sizeof(buffer
)));
1248 verify(strncmp(buffer
, "VERSION ", 8) == 0);
1253 static enum test_return
test_ascii_verbosity(void)
1255 /* This command does not adhere to the spec! */
1256 execute(send_string("verbosity foo bar my\r\n"));
1257 execute(receive_error_response());
1259 execute(send_string("verbosity noreply\r\n"));
1260 execute(receive_error_response());
1262 execute(send_string("verbosity 0 noreply\r\n"));
1263 execute(test_ascii_version());
1265 execute(send_string("verbosity\r\n"));
1266 execute(receive_error_response());
1268 execute(send_string("verbosity 1\r\n"));
1269 execute(receive_response("OK\r\n"));
1271 execute(send_string("verbosity 0\r\n"));
1272 execute(receive_response("OK\r\n"));
1279 static enum test_return
test_ascii_set_impl(const char* key
, bool noreply
)
1281 /* @todo add tests for bogus format! */
1283 snprintf(buffer
, sizeof(buffer
), "set %s 0 0 5%s\r\nvalue\r\n", key
, noreply
? " noreply" : "");
1284 execute(send_string(buffer
));
1287 execute(receive_response("STORED\r\n"));
1289 return test_ascii_version();
1292 static enum test_return
test_ascii_set(void)
1294 return test_ascii_set_impl("test_ascii_set", false);
1297 static enum test_return
test_ascii_set_noreply(void)
1299 return test_ascii_set_impl("test_ascii_set_noreply", true);
1302 static enum test_return
test_ascii_add_impl(const char* key
, bool noreply
)
1304 /* @todo add tests for bogus format! */
1306 snprintf(buffer
, sizeof(buffer
), "add %s 0 0 5%s\r\nvalue\r\n", key
, noreply
? " noreply" : "");
1307 execute(send_string(buffer
));
1310 execute(receive_response("STORED\r\n"));
1312 execute(send_string(buffer
));
1315 execute(receive_response("NOT_STORED\r\n"));
1317 return test_ascii_version();
1320 static enum test_return
test_ascii_add(void)
1322 return test_ascii_add_impl("test_ascii_add", false);
1325 static enum test_return
test_ascii_add_noreply(void)
1327 return test_ascii_add_impl("test_ascii_add_noreply", true);
1330 static enum test_return
ascii_get_unknown_value(char **key
, char **value
, ssize_t
*ndata
)
1334 execute(receive_line(buffer
, sizeof(buffer
)));
1335 verify(strncmp(buffer
, "VALUE ", 6) == 0);
1336 char *end
= strchr(buffer
+ 6, ' ');
1337 verify(end
!= NULL
);
1342 *key
= strdup(buffer
+ 6);
1343 verify(*key
!= NULL
);
1346 unsigned long val
= strtoul(ptr
, &end
, 10); /* flags */
1349 verify(end
!= NULL
);
1350 *ndata
= (ssize_t
)strtoul(end
, &end
, 10); /* size */
1352 verify(end
!= NULL
);
1353 while (end
and *end
!= '\n' and isspace(*end
))
1355 verify(end
and *end
== '\n');
1357 *value
= static_cast<char*>(malloc((size_t)*ndata
));
1358 verify(*value
!= NULL
);
1360 execute(retry_read(*value
, (size_t)*ndata
));
1362 execute(retry_read(buffer
, 2));
1363 verify(memcmp(buffer
, "\r\n", 2) == 0);
1368 static enum test_return
ascii_get_value(const char *key
, const char *value
)
1372 size_t datasize
= strlen(value
);
1374 verify(datasize
< sizeof(buffer
));
1375 execute(receive_line(buffer
, sizeof(buffer
)));
1376 verify(strncmp(buffer
, "VALUE ", 6) == 0);
1377 verify(strncmp(buffer
+ 6, key
, strlen(key
)) == 0);
1378 char *ptr
= buffer
+ 6 + strlen(key
) + 1;
1381 unsigned long val
= strtoul(ptr
, &end
, 10); /* flags */
1384 verify(end
!= NULL
);
1385 val
= strtoul(end
, &end
, 10); /* size */
1387 verify(val
== datasize
);
1388 verify(end
!= NULL
);
1389 while (end
and *end
!= '\n' and isspace(*end
))
1393 verify(end
and *end
== '\n');
1395 execute(retry_read(buffer
, datasize
));
1396 verify(memcmp(buffer
, value
, datasize
) == 0);
1398 execute(retry_read(buffer
, 2));
1399 verify(memcmp(buffer
, "\r\n", 2) == 0);
1404 static enum test_return
ascii_get_item(const char *key
, const char *value
,
1410 datasize
= strlen(value
);
1412 verify(datasize
< sizeof(buffer
));
1413 snprintf(buffer
, sizeof(buffer
), "get %s\r\n", key
);
1414 execute(send_string(buffer
));
1417 execute(ascii_get_value(key
, value
));
1419 execute(retry_read(buffer
, 5));
1420 verify(memcmp(buffer
, "END\r\n", 5) == 0);
1425 static enum test_return
ascii_gets_value(const char *key
, const char *value
,
1430 size_t datasize
= strlen(value
);
1432 verify(datasize
< sizeof(buffer
));
1433 execute(receive_line(buffer
, sizeof(buffer
)));
1434 verify(strncmp(buffer
, "VALUE ", 6) == 0);
1435 verify(strncmp(buffer
+ 6, key
, strlen(key
)) == 0);
1436 char *ptr
= buffer
+ 6 + strlen(key
) + 1;
1439 unsigned long val
= strtoul(ptr
, &end
, 10); /* flags */
1442 verify(end
!= NULL
);
1443 val
= strtoul(end
, &end
, 10); /* size */
1445 verify(val
== datasize
);
1446 verify(end
!= NULL
);
1447 *cas
= strtoul(end
, &end
, 10); /* cas */
1449 verify(val
== datasize
);
1450 verify(end
!= NULL
);
1452 while (end
and *end
!= '\n' and isspace(*end
))
1456 verify(end
and *end
== '\n');
1458 execute(retry_read(buffer
, datasize
));
1459 verify(memcmp(buffer
, value
, datasize
) == 0);
1461 execute(retry_read(buffer
, 2));
1462 verify(memcmp(buffer
, "\r\n", 2) == 0);
1467 static enum test_return
ascii_gets_item(const char *key
, const char *value
,
1468 bool exist
, unsigned long *cas
)
1473 datasize
= strlen(value
);
1475 verify(datasize
< sizeof(buffer
));
1476 snprintf(buffer
, sizeof(buffer
), "gets %s\r\n", key
);
1477 execute(send_string(buffer
));
1480 execute(ascii_gets_value(key
, value
, cas
));
1482 execute(retry_read(buffer
, 5));
1483 verify(memcmp(buffer
, "END\r\n", 5) == 0);
1488 static enum test_return
ascii_set_item(const char *key
, const char *value
)
1491 size_t len
= strlen(value
);
1492 snprintf(buffer
, sizeof(buffer
), "set %s 0 0 %u\r\n", key
, (unsigned int)len
);
1493 execute(send_string(buffer
));
1494 execute(retry_write(value
, len
));
1495 execute(send_string("\r\n"));
1496 execute(receive_response("STORED\r\n"));
1500 static enum test_return
test_ascii_replace_impl(const char* key
, bool noreply
)
1503 snprintf(buffer
, sizeof(buffer
), "replace %s 0 0 5%s\r\nvalue\r\n", key
, noreply
? " noreply" : "");
1504 execute(send_string(buffer
));
1507 execute(test_ascii_version());
1509 execute(receive_response("NOT_STORED\r\n"));
1511 execute(ascii_set_item(key
, "value"));
1512 execute(ascii_get_item(key
, "value", true));
1515 execute(send_string(buffer
));
1518 execute(test_ascii_version());
1520 execute(receive_response("STORED\r\n"));
1522 return test_ascii_version();
1525 static enum test_return
test_ascii_replace(void)
1527 return test_ascii_replace_impl("test_ascii_replace", false);
1530 static enum test_return
test_ascii_replace_noreply(void)
1532 return test_ascii_replace_impl("test_ascii_replace_noreply", true);
1535 static enum test_return
test_ascii_cas_impl(const char* key
, bool noreply
)
1540 execute(ascii_set_item(key
, "value"));
1541 execute(ascii_gets_item(key
, "value", true, &cas
));
1543 snprintf(buffer
, sizeof(buffer
), "cas %s 0 0 6 %lu%s\r\nvalue2\r\n", key
, cas
, noreply
? " noreply" : "");
1544 execute(send_string(buffer
));
1547 execute(test_ascii_version());
1549 execute(receive_response("STORED\r\n"));
1551 /* reexecute the same command should fail due to illegal cas */
1552 execute(send_string(buffer
));
1555 execute(test_ascii_version());
1557 execute(receive_response("EXISTS\r\n"));
1559 return test_ascii_version();
1562 static enum test_return
test_ascii_cas(void)
1564 return test_ascii_cas_impl("test_ascii_cas", false);
1567 static enum test_return
test_ascii_cas_noreply(void)
1569 return test_ascii_cas_impl("test_ascii_cas_noreply", true);
1572 static enum test_return
test_ascii_delete_impl(const char *key
, bool noreply
)
1574 execute(ascii_set_item(key
, "value"));
1576 execute(send_string("delete\r\n"));
1577 execute(receive_error_response());
1578 /* BUG: the server accepts delete a b */
1579 execute(send_string("delete a b c d e\r\n"));
1580 execute(receive_error_response());
1583 snprintf(buffer
, sizeof(buffer
), "delete %s%s\r\n", key
, noreply
? " noreply" : "");
1584 execute(send_string(buffer
));
1587 execute(test_ascii_version());
1589 execute(receive_response("DELETED\r\n"));
1591 execute(ascii_get_item(key
, "value", false));
1592 execute(send_string(buffer
));
1594 execute(test_ascii_version());
1596 execute(receive_response("NOT_FOUND\r\n"));
1601 static enum test_return
test_ascii_delete(void)
1603 return test_ascii_delete_impl("test_ascii_delete", false);
1606 static enum test_return
test_ascii_delete_noreply(void)
1608 return test_ascii_delete_impl("test_ascii_delete_noreply", true);
1611 static enum test_return
test_ascii_get(void)
1613 execute(ascii_set_item("test_ascii_get", "value"));
1615 execute(send_string("get\r\n"));
1616 execute(receive_error_response());
1617 execute(ascii_get_item("test_ascii_get", "value", true));
1618 execute(ascii_get_item("test_ascii_get_notfound", "value", false));
1623 static enum test_return
test_ascii_gets(void)
1625 execute(ascii_set_item("test_ascii_gets", "value"));
1627 execute(send_string("gets\r\n"));
1628 execute(receive_error_response());
1630 execute(ascii_gets_item("test_ascii_gets", "value", true, &cas
));
1631 execute(ascii_gets_item("test_ascii_gets_notfound", "value", false, &cas
));
1636 static enum test_return
test_ascii_mget(void)
1638 const uint32_t nkeys
= 5;
1639 const char * const keys
[]= {
1642 /* test_ascii_mget_3 does not exist :) */
1648 for (uint32_t x
= 0; x
< nkeys
; ++x
)
1649 execute(ascii_set_item(keys
[x
], "value"));
1651 /* Ask for a key that doesn't exist as well */
1652 execute(send_string("get test_ascii_mget1 test_ascii_mget2 test_ascii_mget3 "
1653 "test_ascii_mget4 test_ascii_mget5 "
1654 "test_ascii_mget6\r\n"));
1656 char *returned
[nkeys
];
1658 for (uint32_t x
= 0; x
< nkeys
; ++x
)
1662 execute(ascii_get_unknown_value(&returned
[x
], &v
, &nbytes
));
1663 verify(nbytes
== 5);
1664 verify(memcmp(v
, "value", 5) == 0);
1669 execute(retry_read(buffer
, 5));
1670 verify(memcmp(buffer
, "END\r\n", 5) == 0);
1672 /* verify that we got all the keys we expected */
1673 for (uint32_t x
= 0; x
< nkeys
; ++x
)
1676 for (uint32_t y
= 0; y
< nkeys
; ++y
)
1678 if (strcmp(keys
[x
], returned
[y
]) == 0)
1687 for (uint32_t x
= 0; x
< nkeys
; ++x
)
1693 static enum test_return
test_ascii_incr_impl(const char* key
, bool noreply
)
1696 snprintf(cmd
, sizeof(cmd
), "incr %s 1%s\r\n", key
, noreply
? " noreply" : "");
1698 execute(ascii_set_item(key
, "0"));
1699 for (int x
= 1; x
< 11; ++x
)
1701 execute(send_string(cmd
));
1704 execute(test_ascii_version());
1708 execute(receive_line(buffer
, sizeof(buffer
)));
1709 int val
= atoi(buffer
);
1714 execute(ascii_get_item(key
, "10", true));
1719 static enum test_return
test_ascii_incr(void)
1721 return test_ascii_incr_impl("test_ascii_incr", false);
1724 static enum test_return
test_ascii_incr_noreply(void)
1726 return test_ascii_incr_impl("test_ascii_incr_noreply", true);
1729 static enum test_return
test_ascii_decr_impl(const char* key
, bool noreply
)
1732 snprintf(cmd
, sizeof(cmd
), "decr %s 1%s\r\n", key
, noreply
? " noreply" : "");
1734 execute(ascii_set_item(key
, "9"));
1735 for (int x
= 8; x
> -1; --x
)
1737 execute(send_string(cmd
));
1740 execute(test_ascii_version());
1744 execute(receive_line(buffer
, sizeof(buffer
)));
1745 int val
= atoi(buffer
);
1750 execute(ascii_get_item(key
, "0", true));
1752 /* verify that it doesn't wrap */
1753 execute(send_string(cmd
));
1755 execute(test_ascii_version());
1759 execute(receive_line(buffer
, sizeof(buffer
)));
1761 execute(ascii_get_item(key
, "0", true));
1766 static enum test_return
test_ascii_decr(void)
1768 return test_ascii_decr_impl("test_ascii_decr", false);
1771 static enum test_return
test_ascii_decr_noreply(void)
1773 return test_ascii_decr_impl("test_ascii_decr_noreply", true);
1777 static enum test_return
test_ascii_flush_impl(const char *key
, bool noreply
)
1780 /* Verify that the flush_all command handles unknown options */
1781 /* Bug in the current memcached server! */
1782 execute(send_string("flush_all foo bar\r\n"));
1783 execute(receive_error_response());
1786 execute(ascii_set_item(key
, key
));
1787 execute(ascii_get_item(key
, key
, true));
1791 execute(send_string("flush_all noreply\r\n"));
1792 execute(test_ascii_version());
1796 execute(send_string("flush_all\r\n"));
1797 execute(receive_response("OK\r\n"));
1800 execute(ascii_get_item(key
, key
, false));
1805 static enum test_return
test_ascii_flush(void)
1807 return test_ascii_flush_impl("test_ascii_flush", false);
1810 static enum test_return
test_ascii_flush_noreply(void)
1812 return test_ascii_flush_impl("test_ascii_flush_noreply", true);
1815 static enum test_return
test_ascii_concat_impl(const char *key
,
1826 execute(ascii_set_item(key
, value
));
1834 snprintf(cmd
, sizeof(cmd
), "%s %s 0 0 %u%s\r\n%s\r\n",
1835 append
? "append" : "prepend",
1836 key
, (unsigned int)strlen(value
), noreply
? " noreply" : "",
1838 execute(send_string(cmd
));
1841 execute(test_ascii_version());
1843 execute(receive_response("STORED\r\n"));
1845 execute(ascii_get_item(key
, "hello world", true));
1847 snprintf(cmd
, sizeof(cmd
), "%s %s_notfound 0 0 %u%s\r\n%s\r\n",
1848 append
? "append" : "prepend",
1849 key
, (unsigned int)strlen(value
), noreply
? " noreply" : "",
1851 execute(send_string(cmd
));
1854 execute(test_ascii_version());
1856 execute(receive_response("NOT_STORED\r\n"));
1861 static enum test_return
test_ascii_append(void)
1863 return test_ascii_concat_impl("test_ascii_append", true, false);
1866 static enum test_return
test_ascii_prepend(void)
1868 return test_ascii_concat_impl("test_ascii_prepend", false, false);
1871 static enum test_return
test_ascii_append_noreply(void)
1873 return test_ascii_concat_impl("test_ascii_append_noreply", true, true);
1876 static enum test_return
test_ascii_prepend_noreply(void)
1878 return test_ascii_concat_impl("test_ascii_prepend_noreply", false, true);
1881 static enum test_return
test_ascii_stat(void)
1883 execute(send_string("stats noreply\r\n"));
1884 execute(receive_error_response());
1885 execute(send_string("stats\r\n"));
1888 execute(receive_line(buffer
, sizeof(buffer
)));
1889 } while (strcmp(buffer
, "END\r\n") != 0);
1891 return TEST_PASS_RECONNECT
;
1894 typedef enum test_return(*TEST_FUNC
)(void);
1898 const char *description
;
1902 struct testcase testcases
[]= {
1903 { "ascii quit", test_ascii_quit
},
1904 { "ascii version", test_ascii_version
},
1905 { "ascii verbosity", test_ascii_verbosity
},
1906 { "ascii set", test_ascii_set
},
1907 { "ascii set noreply", test_ascii_set_noreply
},
1908 { "ascii get", test_ascii_get
},
1909 { "ascii gets", test_ascii_gets
},
1910 { "ascii mget", test_ascii_mget
},
1911 { "ascii flush", test_ascii_flush
},
1912 { "ascii flush noreply", test_ascii_flush_noreply
},
1913 { "ascii add", test_ascii_add
},
1914 { "ascii add noreply", test_ascii_add_noreply
},
1915 { "ascii replace", test_ascii_replace
},
1916 { "ascii replace noreply", test_ascii_replace_noreply
},
1917 { "ascii cas", test_ascii_cas
},
1918 { "ascii cas noreply", test_ascii_cas_noreply
},
1919 { "ascii delete", test_ascii_delete
},
1920 { "ascii delete noreply", test_ascii_delete_noreply
},
1921 { "ascii incr", test_ascii_incr
},
1922 { "ascii incr noreply", test_ascii_incr_noreply
},
1923 { "ascii decr", test_ascii_decr
},
1924 { "ascii decr noreply", test_ascii_decr_noreply
},
1925 { "ascii append", test_ascii_append
},
1926 { "ascii append noreply", test_ascii_append_noreply
},
1927 { "ascii prepend", test_ascii_prepend
},
1928 { "ascii prepend noreply", test_ascii_prepend_noreply
},
1929 { "ascii stat", test_ascii_stat
},
1930 { "binary noop", test_binary_noop
},
1931 { "binary quit", test_binary_quit
},
1932 { "binary quitq", test_binary_quitq
},
1933 { "binary set", test_binary_set
},
1934 { "binary setq", test_binary_setq
},
1935 { "binary flush", test_binary_flush
},
1936 { "binary flushq", test_binary_flushq
},
1937 { "binary add", test_binary_add
},
1938 { "binary addq", test_binary_addq
},
1939 { "binary replace", test_binary_replace
},
1940 { "binary replaceq", test_binary_replaceq
},
1941 { "binary delete", test_binary_delete
},
1942 { "binary deleteq", test_binary_deleteq
},
1943 { "binary get", test_binary_get
},
1944 { "binary getq", test_binary_getq
},
1945 { "binary getk", test_binary_getk
},
1946 { "binary getkq", test_binary_getkq
},
1947 { "binary incr", test_binary_incr
},
1948 { "binary incrq", test_binary_incrq
},
1949 { "binary decr", test_binary_decr
},
1950 { "binary decrq", test_binary_decrq
},
1951 { "binary version", test_binary_version
},
1952 { "binary append", test_binary_append
},
1953 { "binary appendq", test_binary_appendq
},
1954 { "binary prepend", test_binary_prepend
},
1955 { "binary prependq", test_binary_prependq
},
1956 { "binary stat", test_binary_stat
},
1960 const int ascii_tests
= 1;
1961 const int binary_tests
= 2;
1969 int main(int argc
, char **argv
)
1971 static const char * const status_msg
[]= {"[skip]", "[pass]", "[pass]", "[FAIL]"};
1972 struct test_type_st tests
= { true, true };
1975 const char *hostname
= "localhost";
1976 const char *port
= "11211";
1979 const char *testname
= NULL
;
1983 while ((cmd
= getopt(argc
, argv
, "qt:vch:p:PT:?ab")) != EOF
)
1988 tests
.binary
= false;
1997 timeout
= atoi(optarg
);
2000 fprintf(stderr
, "Invalid timeout. Please specify a number for -t\n");
2001 return EXIT_FAILURE
;
2005 case 'v': verbose
= true;
2008 case 'c': do_core
= true;
2011 case 'h': hostname
= optarg
;
2014 case 'p': port
= optarg
;
2021 case 'P': prompt
= true;
2024 case 'T': testname
= optarg
;
2028 fprintf(stderr
, "Usage: %s [-h hostname] [-p port] [-c] [-v] [-t n] [-P] [-T testname]'\n"
2029 "\t-c\tGenerate coredump if a test fails\n"
2030 "\t-v\tVerbose test output (print out the assertion)\n"
2031 "\t-t n\tSet the timeout for io-operations to n seconds\n"
2032 "\t-P\tPrompt the user before starting a test.\n"
2033 "\t\t\t\"skip\" will skip the test\n"
2034 "\t\t\t\"quit\" will terminate memcapable\n"
2035 "\t\t\tEverything else will start the test\n"
2036 "\t-T n\tJust run the test named n\n"
2037 "\t-a\tOnly test the ascii protocol\n"
2038 "\t-b\tOnly test the binary protocol\n",
2040 return EXIT_FAILURE
;
2044 initialize_sockets();
2045 sock
= connect_server(hostname
, port
);
2046 if (sock
== INVALID_SOCKET
)
2048 fprintf(stderr
, "Failed to connect to <%s:%s>: %s\n",
2049 hostname
, port
, strerror(get_socket_errno()));
2050 return EXIT_FAILURE
;
2053 for (int ii
= 0; testcases
[ii
].description
!= NULL
; ++ii
)
2055 if (testname
!= NULL
&& strcmp(testcases
[ii
].description
, testname
) != 0)
2058 if ((testcases
[ii
].description
[0] == 'a' && (tests
.ascii
) == 0) ||
2059 (testcases
[ii
].description
[0] == 'b' && (tests
.binary
) == 0))
2064 fprintf(stdout
, "%-40s", testcases
[ii
].description
);
2069 fprintf(stdout
, "\nPress <return> when you are ready? ");
2070 char buffer
[80] = {0};
2071 if (fgets(buffer
, sizeof(buffer
), stdin
) != NULL
) {
2072 if (strncmp(buffer
, "skip", 4) == 0)
2074 fprintf(stdout
, "%-40s%s\n", testcases
[ii
].description
,
2075 status_msg
[TEST_SKIP
]);
2079 if (strncmp(buffer
, "quit", 4) == 0)
2083 fprintf(stdout
, "%-40s", testcases
[ii
].description
);
2087 bool reconnect
= false;
2088 enum test_return ret
= testcases
[ii
].function();
2089 if (ret
== TEST_FAIL
)
2094 fprintf(stderr
, "\n");
2096 else if (ret
== TEST_PASS_RECONNECT
)
2099 fprintf(stderr
, "%s\n", status_msg
[ret
]);
2103 if ((sock
= connect_server(hostname
, port
)) == INVALID_SOCKET
)
2105 fprintf(stderr
, "Failed to connect to <%s:%s>: %s\n",
2106 hostname
, port
, strerror(get_socket_errno()));
2107 fprintf(stderr
, "%d of %d tests failed\n", failed
, total
);
2108 return EXIT_FAILURE
;
2115 fprintf(stdout
, "All tests passed\n");
2117 fprintf(stderr
, "%d of %d tests failed\n", failed
, total
);
2119 return (failed
== 0) ? 0 : 1;