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 fprintf(stderr
, "Errno: %d %s\n", get_socket_errno(), strerror(errno
));
307 verify(get_socket_errno() == EINTR
|| get_socket_errno() == EAGAIN
);
314 } while (offset
< len
);
320 * Receive a response from the server and conver the fields in the header
321 * to local byte order
323 static enum test_return
recv_packet(response
*rsp
)
325 execute(retry_read(rsp
, sizeof(protocol_binary_response_no_extras
)));
327 /* Fix the byte order in the packet header */
328 rsp
->plain
.message
.header
.response
.keylen
=
329 ntohs(rsp
->plain
.message
.header
.response
.keylen
);
330 rsp
->plain
.message
.header
.response
.status
=
331 ntohs(rsp
->plain
.message
.header
.response
.status
);
332 rsp
->plain
.message
.header
.response
.bodylen
=
333 ntohl(rsp
->plain
.message
.header
.response
.bodylen
);
334 rsp
->plain
.message
.header
.response
.cas
=
335 ntohll(rsp
->plain
.message
.header
.response
.cas
);
337 size_t bodysz
= rsp
->plain
.message
.header
.response
.bodylen
;
339 execute(retry_read(rsp
->bytes
+ sizeof (protocol_binary_response_no_extras
), bodysz
));
345 * Create a storage command (add, set, replace etc)
347 * @param cmd destination buffer
348 * @param cc the storage command to create
349 * @param key the key to store
350 * @param keylen the length of the key
351 * @param dta the data to store with the key
352 * @param dtalen the length of the data to store with the key
353 * @param flags the flags to store along with the key
354 * @param exptime the expiry time for the key
356 static void storage_command(command
*cmd
,
365 /* all of the storage commands use the same command layout */
366 protocol_binary_request_set
*request
= &cmd
->set
;
368 memset(request
, 0, sizeof (*request
));
369 request
->message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
370 request
->message
.header
.request
.opcode
= cc
;
371 request
->message
.header
.request
.keylen
= (uint16_t)keylen
;
372 request
->message
.header
.request
.extlen
= 8;
373 request
->message
.header
.request
.bodylen
= (uint32_t)(keylen
+ 8 + dtalen
);
374 request
->message
.header
.request
.opaque
= 0xdeadbeef;
375 request
->message
.body
.flags
= flags
;
376 request
->message
.body
.expiration
= exptime
;
378 off_t key_offset
= sizeof (protocol_binary_request_no_extras
) + 8;
379 memcpy(cmd
->bytes
+ key_offset
, key
, keylen
);
381 memcpy(cmd
->bytes
+ key_offset
+ keylen
, dta
, dtalen
);
385 * Create a basic command to send to the server
386 * @param cmd destination buffer
387 * @param cc the command to create
388 * @param key the key to store
389 * @param keylen the length of the key
390 * @param dta the data to store with the key
391 * @param dtalen the length of the data to store with the key
393 static void raw_command(command
*cmd
,
400 /* all of the storage commands use the same command layout */
401 memset(cmd
, 0, sizeof (*cmd
));
402 cmd
->plain
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
403 cmd
->plain
.message
.header
.request
.opcode
= cc
;
404 cmd
->plain
.message
.header
.request
.keylen
= (uint16_t)keylen
;
405 cmd
->plain
.message
.header
.request
.bodylen
= (uint32_t)(keylen
+ dtalen
);
406 cmd
->plain
.message
.header
.request
.opaque
= 0xdeadbeef;
408 off_t key_offset
= sizeof (protocol_binary_request_no_extras
);
411 memcpy(cmd
->bytes
+ key_offset
, key
, keylen
);
414 memcpy(cmd
->bytes
+ key_offset
+ keylen
, dta
, dtalen
);
418 * Create the flush command
419 * @param cmd destination buffer
420 * @param cc the command to create (FLUSH/FLUSHQ)
421 * @param exptime when to flush
422 * @param use_extra to force using of the extra field?
424 static void flush_command(command
*cmd
,
425 uint8_t cc
, uint32_t exptime
, bool use_extra
)
427 memset(cmd
, 0, sizeof (cmd
->flush
));
428 cmd
->flush
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
429 cmd
->flush
.message
.header
.request
.opcode
= cc
;
430 cmd
->flush
.message
.header
.request
.opaque
= 0xdeadbeef;
432 if (exptime
!= 0 || use_extra
)
434 cmd
->flush
.message
.header
.request
.extlen
= 4;
435 cmd
->flush
.message
.body
.expiration
= htonl(exptime
);
436 cmd
->flush
.message
.header
.request
.bodylen
= 4;
441 * Create a incr/decr command
442 * @param cc the cmd to create (FLUSH/FLUSHQ)
443 * @param key the key to operate on
444 * @param keylen the number of bytes in the key
445 * @param delta the number to add/subtract
446 * @param initial the initial value if the key doesn't exist
447 * @param exptime when the key should expire if it isn't set
449 static void arithmetic_command(command
*cmd
,
457 memset(cmd
, 0, sizeof (cmd
->incr
));
458 cmd
->incr
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
459 cmd
->incr
.message
.header
.request
.opcode
= cc
;
460 cmd
->incr
.message
.header
.request
.keylen
= (uint16_t)keylen
;
461 cmd
->incr
.message
.header
.request
.extlen
= 20;
462 cmd
->incr
.message
.header
.request
.bodylen
= (uint32_t)(keylen
+ 20);
463 cmd
->incr
.message
.header
.request
.opaque
= 0xdeadbeef;
464 cmd
->incr
.message
.body
.delta
= htonll(delta
);
465 cmd
->incr
.message
.body
.initial
= htonll(initial
);
466 cmd
->incr
.message
.body
.expiration
= htonl(exptime
);
468 off_t key_offset
= sizeof (protocol_binary_request_no_extras
) + 20;
469 memcpy(cmd
->bytes
+ key_offset
, key
, keylen
);
473 * Validate the response header from the server
474 * @param rsp the response to check
475 * @param cc the expected command
476 * @param status the expected status
478 static enum test_return
do_validate_response_header(response
*rsp
,
479 uint8_t cc
, uint16_t status
)
481 verify(rsp
->plain
.message
.header
.response
.magic
== PROTOCOL_BINARY_RES
);
482 verify(rsp
->plain
.message
.header
.response
.opcode
== cc
);
483 verify(rsp
->plain
.message
.header
.response
.datatype
== PROTOCOL_BINARY_RAW_BYTES
);
484 verify(rsp
->plain
.message
.header
.response
.status
== status
);
485 verify(rsp
->plain
.message
.header
.response
.opaque
== 0xdeadbeef);
487 if (status
== PROTOCOL_BINARY_RESPONSE_SUCCESS
)
490 case PROTOCOL_BINARY_CMD_ADDQ
:
491 case PROTOCOL_BINARY_CMD_APPENDQ
:
492 case PROTOCOL_BINARY_CMD_DECREMENTQ
:
493 case PROTOCOL_BINARY_CMD_DELETEQ
:
494 case PROTOCOL_BINARY_CMD_FLUSHQ
:
495 case PROTOCOL_BINARY_CMD_INCREMENTQ
:
496 case PROTOCOL_BINARY_CMD_PREPENDQ
:
497 case PROTOCOL_BINARY_CMD_QUITQ
:
498 case PROTOCOL_BINARY_CMD_REPLACEQ
:
499 case PROTOCOL_BINARY_CMD_SETQ
:
500 verify("Quiet command shouldn't return on success" == NULL
);
506 case PROTOCOL_BINARY_CMD_ADD
:
507 case PROTOCOL_BINARY_CMD_REPLACE
:
508 case PROTOCOL_BINARY_CMD_SET
:
509 case PROTOCOL_BINARY_CMD_APPEND
:
510 case PROTOCOL_BINARY_CMD_PREPEND
:
511 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
512 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
513 verify(rsp
->plain
.message
.header
.response
.bodylen
== 0);
514 verify(rsp
->plain
.message
.header
.response
.cas
!= 0);
516 case PROTOCOL_BINARY_CMD_FLUSH
:
517 case PROTOCOL_BINARY_CMD_NOOP
:
518 case PROTOCOL_BINARY_CMD_QUIT
:
519 case PROTOCOL_BINARY_CMD_DELETE
:
520 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
521 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
522 verify(rsp
->plain
.message
.header
.response
.bodylen
== 0);
523 verify(rsp
->plain
.message
.header
.response
.cas
== 0);
526 case PROTOCOL_BINARY_CMD_DECREMENT
:
527 case PROTOCOL_BINARY_CMD_INCREMENT
:
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
== 8);
531 verify(rsp
->plain
.message
.header
.response
.cas
!= 0);
534 case PROTOCOL_BINARY_CMD_STAT
:
535 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
536 /* key and value exists in all packets except in the terminating */
537 verify(rsp
->plain
.message
.header
.response
.cas
== 0);
540 case PROTOCOL_BINARY_CMD_VERSION
:
541 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
542 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
543 verify(rsp
->plain
.message
.header
.response
.bodylen
!= 0);
544 verify(rsp
->plain
.message
.header
.response
.cas
== 0);
547 case PROTOCOL_BINARY_CMD_GET
:
548 case PROTOCOL_BINARY_CMD_GETQ
:
549 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
550 verify(rsp
->plain
.message
.header
.response
.extlen
== 4);
551 verify(rsp
->plain
.message
.header
.response
.cas
!= 0);
554 case PROTOCOL_BINARY_CMD_GETK
:
555 case PROTOCOL_BINARY_CMD_GETKQ
:
556 verify(rsp
->plain
.message
.header
.response
.keylen
!= 0);
557 verify(rsp
->plain
.message
.header
.response
.extlen
== 4);
558 verify(rsp
->plain
.message
.header
.response
.cas
!= 0);
562 /* Undefined command code */
568 verify(rsp
->plain
.message
.header
.response
.cas
== 0);
569 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
570 if (cc
!= PROTOCOL_BINARY_CMD_GETK
)
572 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
579 /* We call verify(validate_response_header), but that macro
580 * expects a boolean expression, and the function returns
581 * an enum.... Let's just create a macro to avoid cluttering
582 * the code with all of the == TEST_PASS ;-)
584 #define validate_response_header(a,b,c) \
585 do_validate_response_header(a,b,c) == TEST_PASS
588 static enum test_return
send_binary_noop(void)
591 raw_command(&cmd
, PROTOCOL_BINARY_CMD_NOOP
, NULL
, 0, NULL
, 0);
592 execute(send_packet(&cmd
));
596 static enum test_return
receive_binary_noop(void)
599 execute(recv_packet(&rsp
));
600 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_NOOP
,
601 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
605 static enum test_return
test_binary_noop(void)
607 execute(send_binary_noop());
608 execute(receive_binary_noop());
612 static enum test_return
test_binary_quit_impl(uint8_t cc
)
616 raw_command(&cmd
, cc
, NULL
, 0, NULL
, 0);
618 execute(send_packet(&cmd
));
619 if (cc
== PROTOCOL_BINARY_CMD_QUIT
)
621 execute(recv_packet(&rsp
));
622 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_QUIT
,
623 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
626 /* Socket should be closed now, read should return 0 */
627 verify(timeout_io_op(sock
, POLLIN
, rsp
.bytes
, sizeof(rsp
.bytes
)) == 0);
629 return TEST_PASS_RECONNECT
;
632 static enum test_return
test_binary_quit(void)
634 return test_binary_quit_impl(PROTOCOL_BINARY_CMD_QUIT
);
637 static enum test_return
test_binary_quitq(void)
639 return test_binary_quit_impl(PROTOCOL_BINARY_CMD_QUITQ
);
642 static enum test_return
test_binary_set_impl(const char* key
, uint8_t cc
)
647 uint64_t value
= 0xdeadbeefdeadcafe;
648 storage_command(&cmd
, cc
, key
, strlen(key
), &value
, sizeof (value
), 0, 0);
650 /* set should always work */
651 for (int ii
= 0; ii
< 10; ii
++)
654 execute(send_packet(&cmd
));
656 execute(resend_packet(&cmd
));
658 if (cc
== PROTOCOL_BINARY_CMD_SET
)
660 execute(recv_packet(&rsp
));
661 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
664 execute(test_binary_noop());
668 * We need to get the current CAS id, and at this time we haven't
669 * verified that we have a working get
671 if (cc
== PROTOCOL_BINARY_CMD_SETQ
)
673 cmd
.set
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_SET
;
674 execute(resend_packet(&cmd
));
675 execute(recv_packet(&rsp
));
676 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_SET
,
677 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
678 cmd
.set
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_SETQ
;
681 /* try to set with the correct CAS value */
682 cmd
.plain
.message
.header
.request
.cas
=
683 htonll(rsp
.plain
.message
.header
.response
.cas
);
684 execute(resend_packet(&cmd
));
685 if (cc
== PROTOCOL_BINARY_CMD_SET
)
687 execute(recv_packet(&rsp
));
688 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
691 execute(test_binary_noop());
693 /* try to set with an incorrect CAS value */
694 cmd
.plain
.message
.header
.request
.cas
=
695 htonll(rsp
.plain
.message
.header
.response
.cas
- 1);
696 execute(resend_packet(&cmd
));
697 execute(send_binary_noop());
698 execute(recv_packet(&rsp
));
699 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS
));
700 execute(receive_binary_noop());
705 static enum test_return
test_binary_set(void)
707 return test_binary_set_impl("test_binary_set", PROTOCOL_BINARY_CMD_SET
);
710 static enum test_return
test_binary_setq(void)
712 return test_binary_set_impl("test_binary_setq", PROTOCOL_BINARY_CMD_SETQ
);
715 static enum test_return
test_binary_add_impl(const char* key
, uint8_t cc
)
719 uint64_t value
= 0xdeadbeefdeadcafe;
720 storage_command(&cmd
, cc
, key
, strlen(key
), &value
, sizeof (value
), 0, 0);
722 /* first add should work, rest of them should fail (even with cas
724 for (int ii
=0; ii
< 10; ii
++)
727 execute(send_packet(&cmd
));
729 execute(resend_packet(&cmd
));
731 if (cc
== PROTOCOL_BINARY_CMD_ADD
|| ii
> 0)
733 uint16_t expected_result
;
735 expected_result
= PROTOCOL_BINARY_RESPONSE_SUCCESS
;
737 expected_result
= PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS
;
739 execute(send_binary_noop());
740 execute(recv_packet(&rsp
));
741 execute(receive_binary_noop());
742 verify(validate_response_header(&rsp
, cc
, expected_result
));
745 execute(test_binary_noop());
751 static enum test_return
test_binary_add(void)
753 return test_binary_add_impl("test_binary_add", PROTOCOL_BINARY_CMD_ADD
);
756 static enum test_return
test_binary_addq(void)
758 return test_binary_add_impl("test_binary_addq", PROTOCOL_BINARY_CMD_ADDQ
);
761 static enum test_return
binary_set_item(const char *key
, const char *value
)
765 storage_command(&cmd
, PROTOCOL_BINARY_CMD_SET
, key
, strlen(key
),
766 value
, strlen(value
), 0, 0);
767 execute(send_packet(&cmd
));
768 execute(recv_packet(&rsp
));
769 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_SET
,
770 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
774 static enum test_return
test_binary_replace_impl(const char* key
, uint8_t cc
)
778 uint64_t value
= 0xdeadbeefdeadcafe;
779 storage_command(&cmd
, cc
, key
, strlen(key
), &value
, sizeof (value
), 0, 0);
781 /* first replace should fail, successive should succeed (when the
783 for (int ii
= 0; ii
< 10; ii
++)
786 execute(send_packet(&cmd
));
788 execute(resend_packet(&cmd
));
790 if (cc
== PROTOCOL_BINARY_CMD_REPLACE
|| ii
== 0)
792 uint16_t expected_result
;
794 expected_result
=PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
;
796 expected_result
=PROTOCOL_BINARY_RESPONSE_SUCCESS
;
798 execute(send_binary_noop());
799 execute(recv_packet(&rsp
));
800 execute(receive_binary_noop());
801 verify(validate_response_header(&rsp
, cc
, expected_result
));
804 execute(binary_set_item(key
, key
));
807 execute(test_binary_noop());
810 /* verify that replace with CAS value works! */
811 cmd
.plain
.message
.header
.request
.cas
=
812 htonll(rsp
.plain
.message
.header
.response
.cas
);
813 execute(resend_packet(&cmd
));
815 if (cc
== PROTOCOL_BINARY_CMD_REPLACE
)
817 execute(recv_packet(&rsp
));
818 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
821 execute(test_binary_noop());
823 /* try to set with an incorrect CAS value */
824 cmd
.plain
.message
.header
.request
.cas
=
825 htonll(rsp
.plain
.message
.header
.response
.cas
- 1);
826 execute(resend_packet(&cmd
));
827 execute(send_binary_noop());
828 execute(recv_packet(&rsp
));
829 execute(receive_binary_noop());
830 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS
));
835 static enum test_return
test_binary_replace(void)
837 return test_binary_replace_impl("test_binary_replace", PROTOCOL_BINARY_CMD_REPLACE
);
840 static enum test_return
test_binary_replaceq(void)
842 return test_binary_replace_impl("test_binary_replaceq", PROTOCOL_BINARY_CMD_REPLACEQ
);
845 static enum test_return
test_binary_delete_impl(const char *key
, uint8_t cc
)
849 raw_command(&cmd
, cc
, key
, strlen(key
), NULL
, 0);
851 /* The delete shouldn't work the first time, because the item isn't there */
852 execute(send_packet(&cmd
));
853 execute(send_binary_noop());
854 execute(recv_packet(&rsp
));
855 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
));
856 execute(receive_binary_noop());
857 execute(binary_set_item(key
, key
));
859 /* The item should be present now, resend*/
860 execute(resend_packet(&cmd
));
861 if (cc
== PROTOCOL_BINARY_CMD_DELETE
)
863 execute(recv_packet(&rsp
));
864 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
867 execute(test_binary_noop());
872 static enum test_return
test_binary_delete(void)
874 return test_binary_delete_impl("test_binary_delete", PROTOCOL_BINARY_CMD_DELETE
);
877 static enum test_return
test_binary_deleteq(void)
879 return test_binary_delete_impl("test_binary_deleteq", PROTOCOL_BINARY_CMD_DELETEQ
);
882 static enum test_return
test_binary_get_impl(const char *key
, uint8_t cc
)
887 raw_command(&cmd
, cc
, key
, strlen(key
), NULL
, 0);
888 execute(send_packet(&cmd
));
889 execute(send_binary_noop());
891 if (cc
== PROTOCOL_BINARY_CMD_GET
|| cc
== PROTOCOL_BINARY_CMD_GETK
)
893 execute(recv_packet(&rsp
));
894 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
));
897 execute(receive_binary_noop());
899 execute(binary_set_item(key
, key
));
900 execute(resend_packet(&cmd
));
901 execute(send_binary_noop());
903 execute(recv_packet(&rsp
));
904 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
905 execute(receive_binary_noop());
910 static enum test_return
test_binary_get(void)
912 return test_binary_get_impl("test_binary_get", PROTOCOL_BINARY_CMD_GET
);
915 static enum test_return
test_binary_getk(void)
917 return test_binary_get_impl("test_binary_getk", PROTOCOL_BINARY_CMD_GETK
);
920 static enum test_return
test_binary_getq(void)
922 return test_binary_get_impl("test_binary_getq", PROTOCOL_BINARY_CMD_GETQ
);
925 static enum test_return
test_binary_getkq(void)
927 return test_binary_get_impl("test_binary_getkq", PROTOCOL_BINARY_CMD_GETKQ
);
930 static enum test_return
test_binary_incr_impl(const char* key
, uint8_t cc
)
934 arithmetic_command(&cmd
, cc
, key
, strlen(key
), 1, 0, 0);
937 for (ii
= 0; ii
< 10; ++ii
)
940 execute(send_packet(&cmd
));
942 execute(resend_packet(&cmd
));
944 if (cc
== PROTOCOL_BINARY_CMD_INCREMENT
)
946 execute(recv_packet(&rsp
));
947 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
948 verify(ntohll(rsp
.incr
.message
.body
.value
) == ii
);
951 execute(test_binary_noop());
954 /* @todo add incorrect CAS */
958 static enum test_return
test_binary_incr(void)
960 return test_binary_incr_impl("test_binary_incr", PROTOCOL_BINARY_CMD_INCREMENT
);
963 static enum test_return
test_binary_incrq(void)
965 return test_binary_incr_impl("test_binary_incrq", PROTOCOL_BINARY_CMD_INCREMENTQ
);
968 static enum test_return
test_binary_decr_impl(const char* key
, uint8_t cc
)
972 arithmetic_command(&cmd
, cc
, key
, strlen(key
), 1, 9, 0);
975 for (ii
= 9; ii
> -1; --ii
)
978 execute(send_packet(&cmd
));
980 execute(resend_packet(&cmd
));
982 if (cc
== PROTOCOL_BINARY_CMD_DECREMENT
)
984 execute(recv_packet(&rsp
));
985 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
986 verify(ntohll(rsp
.decr
.message
.body
.value
) == (uint64_t)ii
);
989 execute(test_binary_noop());
992 /* decr 0 should not wrap */
993 execute(resend_packet(&cmd
));
994 if (cc
== PROTOCOL_BINARY_CMD_DECREMENT
)
996 execute(recv_packet(&rsp
));
997 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
998 verify(ntohll(rsp
.decr
.message
.body
.value
) == 0);
1002 /* @todo get the value and verify! */
1006 /* @todo add incorrect cas */
1007 execute(test_binary_noop());
1011 static enum test_return
test_binary_decr(void)
1013 return test_binary_decr_impl("test_binary_decr",
1014 PROTOCOL_BINARY_CMD_DECREMENT
);
1017 static enum test_return
test_binary_decrq(void)
1019 return test_binary_decr_impl("test_binary_decrq",
1020 PROTOCOL_BINARY_CMD_DECREMENTQ
);
1023 static enum test_return
test_binary_version(void)
1027 raw_command(&cmd
, PROTOCOL_BINARY_CMD_VERSION
, NULL
, 0, NULL
, 0);
1029 execute(send_packet(&cmd
));
1030 execute(recv_packet(&rsp
));
1031 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_VERSION
,
1032 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1037 static enum test_return
test_binary_flush_impl(const char *key
, uint8_t cc
)
1042 for (int ii
= 0; ii
< 2; ++ii
)
1044 execute(binary_set_item(key
, key
));
1045 flush_command(&cmd
, cc
, 0, ii
== 0);
1046 execute(send_packet(&cmd
));
1048 if (cc
== PROTOCOL_BINARY_CMD_FLUSH
)
1050 execute(recv_packet(&rsp
));
1051 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1054 execute(test_binary_noop());
1056 raw_command(&cmd
, PROTOCOL_BINARY_CMD_GET
, key
, strlen(key
), NULL
, 0);
1057 execute(send_packet(&cmd
));
1058 execute(recv_packet(&rsp
));
1059 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_GET
,
1060 PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
));
1066 static enum test_return
test_binary_flush(void)
1068 return test_binary_flush_impl("test_binary_flush", PROTOCOL_BINARY_CMD_FLUSH
);
1071 static enum test_return
test_binary_flushq(void)
1073 return test_binary_flush_impl("test_binary_flushq", PROTOCOL_BINARY_CMD_FLUSHQ
);
1076 static enum test_return
test_binary_concat_impl(const char *key
, uint8_t cc
)
1082 if (cc
== PROTOCOL_BINARY_CMD_APPEND
|| cc
== PROTOCOL_BINARY_CMD_APPENDQ
)
1087 execute(binary_set_item(key
, value
));
1089 if (cc
== PROTOCOL_BINARY_CMD_APPEND
|| cc
== PROTOCOL_BINARY_CMD_APPENDQ
)
1094 raw_command(&cmd
, cc
, key
, strlen(key
), value
, strlen(value
));
1095 execute(send_packet(&cmd
));
1096 if (cc
== PROTOCOL_BINARY_CMD_APPEND
|| cc
== PROTOCOL_BINARY_CMD_PREPEND
)
1098 execute(recv_packet(&rsp
));
1099 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1102 execute(test_binary_noop());
1104 raw_command(&cmd
, PROTOCOL_BINARY_CMD_GET
, key
, strlen(key
), NULL
, 0);
1105 execute(send_packet(&cmd
));
1106 execute(recv_packet(&rsp
));
1107 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_GET
,
1108 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1109 verify(rsp
.plain
.message
.header
.response
.bodylen
- 4 == 11);
1110 verify(memcmp(rsp
.bytes
+ 28, "hello world", 11) == 0);
1115 static enum test_return
test_binary_append(void)
1117 return test_binary_concat_impl("test_binary_append", PROTOCOL_BINARY_CMD_APPEND
);
1120 static enum test_return
test_binary_prepend(void)
1122 return test_binary_concat_impl("test_binary_prepend", PROTOCOL_BINARY_CMD_PREPEND
);
1125 static enum test_return
test_binary_appendq(void)
1127 return test_binary_concat_impl("test_binary_appendq", PROTOCOL_BINARY_CMD_APPENDQ
);
1130 static enum test_return
test_binary_prependq(void)
1132 return test_binary_concat_impl("test_binary_prependq", PROTOCOL_BINARY_CMD_PREPENDQ
);
1135 static enum test_return
test_binary_stat(void)
1140 raw_command(&cmd
, PROTOCOL_BINARY_CMD_STAT
, NULL
, 0, NULL
, 0);
1141 execute(send_packet(&cmd
));
1145 execute(recv_packet(&rsp
));
1146 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_STAT
,
1147 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1148 } while (rsp
.plain
.message
.header
.response
.keylen
!= 0);
1153 static enum test_return
send_string(const char *cmd
)
1155 execute(retry_write(cmd
, strlen(cmd
)));
1159 static enum test_return
receive_line(char *buffer
, size_t size
)
1162 while (offset
< size
)
1164 execute(retry_read(buffer
+ offset
, 1));
1165 if (buffer
[offset
] == '\n')
1167 if (offset
+ 1 < size
)
1169 buffer
[offset
+ 1]= '\0';
1181 static enum test_return
receive_response(const char *msg
) {
1183 execute(receive_line(buffer
, sizeof(buffer
)));
1184 if (strcmp(msg
, buffer
) != 0) {
1185 fprintf(stderr
, "[%s]\n", buffer
);
1187 verify(strcmp(msg
, buffer
) == 0);
1191 static enum test_return
receive_error_response(void)
1194 execute(receive_line(buffer
, sizeof(buffer
)));
1195 verify(strncmp(buffer
, "ERROR", 5) == 0 ||
1196 strncmp(buffer
, "CLIENT_ERROR", 12) == 0 ||
1197 strncmp(buffer
, "SERVER_ERROR", 12) == 0);
1201 static enum test_return
test_ascii_quit(void)
1203 /* Verify that quit handles unknown options */
1204 execute(send_string("quit foo bar\r\n"));
1205 execute(receive_error_response());
1207 /* quit doesn't support noreply */
1208 execute(send_string("quit noreply\r\n"));
1209 execute(receive_error_response());
1211 /* Verify that quit works */
1212 execute(send_string("quit\r\n"));
1214 /* Socket should be closed now, read should return 0 */
1216 verify(timeout_io_op(sock
, POLLIN
, buffer
, sizeof(buffer
)) == 0);
1217 return TEST_PASS_RECONNECT
;
1221 static enum test_return
test_ascii_version(void)
1223 /* Verify that version command handles unknown options */
1224 execute(send_string("version foo bar\r\n"));
1225 execute(receive_error_response());
1227 /* version doesn't support noreply */
1228 execute(send_string("version noreply\r\n"));
1229 execute(receive_error_response());
1231 /* Verify that verify works */
1232 execute(send_string("version\r\n"));
1234 execute(receive_line(buffer
, sizeof(buffer
)));
1235 verify(strncmp(buffer
, "VERSION ", 8) == 0);
1240 static enum test_return
test_ascii_verbosity(void)
1242 /* This command does not adhere to the spec! */
1243 execute(send_string("verbosity foo bar my\r\n"));
1244 execute(receive_error_response());
1246 execute(send_string("verbosity noreply\r\n"));
1247 execute(receive_error_response());
1249 execute(send_string("verbosity 0 noreply\r\n"));
1250 execute(test_ascii_version());
1252 execute(send_string("verbosity\r\n"));
1253 execute(receive_error_response());
1255 execute(send_string("verbosity 1\r\n"));
1256 execute(receive_response("OK\r\n"));
1258 execute(send_string("verbosity 0\r\n"));
1259 execute(receive_response("OK\r\n"));
1266 static enum test_return
test_ascii_set_impl(const char* key
, bool noreply
)
1268 /* @todo add tests for bogus format! */
1270 snprintf(buffer
, sizeof(buffer
), "set %s 0 0 5%s\r\nvalue\r\n", key
, noreply
? " noreply" : "");
1271 execute(send_string(buffer
));
1274 execute(receive_response("STORED\r\n"));
1276 return test_ascii_version();
1279 static enum test_return
test_ascii_set(void)
1281 return test_ascii_set_impl("test_ascii_set", false);
1284 static enum test_return
test_ascii_set_noreply(void)
1286 return test_ascii_set_impl("test_ascii_set_noreply", true);
1289 static enum test_return
test_ascii_add_impl(const char* key
, bool noreply
)
1291 /* @todo add tests for bogus format! */
1293 snprintf(buffer
, sizeof(buffer
), "add %s 0 0 5%s\r\nvalue\r\n", key
, noreply
? " noreply" : "");
1294 execute(send_string(buffer
));
1297 execute(receive_response("STORED\r\n"));
1299 execute(send_string(buffer
));
1302 execute(receive_response("NOT_STORED\r\n"));
1304 return test_ascii_version();
1307 static enum test_return
test_ascii_add(void)
1309 return test_ascii_add_impl("test_ascii_add", false);
1312 static enum test_return
test_ascii_add_noreply(void)
1314 return test_ascii_add_impl("test_ascii_add_noreply", true);
1317 static enum test_return
ascii_get_unknown_value(char **key
, char **value
, ssize_t
*ndata
)
1321 execute(receive_line(buffer
, sizeof(buffer
)));
1322 verify(strncmp(buffer
, "VALUE ", 6) == 0);
1323 char *end
= strchr(buffer
+ 6, ' ');
1324 verify(end
!= NULL
);
1326 *key
= strdup(buffer
+ 6);
1327 verify(*key
!= NULL
);
1330 unsigned long val
= strtoul(ptr
, &end
, 10); /* flags */
1333 verify(end
!= NULL
);
1334 *ndata
= (ssize_t
)strtoul(end
, &end
, 10); /* size */
1336 verify(end
!= NULL
);
1337 while (*end
!= '\n' && isspace(*end
))
1339 verify(*end
== '\n');
1341 *value
= malloc((size_t)*ndata
);
1342 verify(*value
!= NULL
);
1344 execute(retry_read(*value
, (size_t)*ndata
));
1346 execute(retry_read(buffer
, 2));
1347 verify(memcmp(buffer
, "\r\n", 2) == 0);
1352 static enum test_return
ascii_get_value(const char *key
, const char *value
)
1356 size_t datasize
= strlen(value
);
1358 verify(datasize
< sizeof(buffer
));
1359 execute(receive_line(buffer
, sizeof(buffer
)));
1360 verify(strncmp(buffer
, "VALUE ", 6) == 0);
1361 verify(strncmp(buffer
+ 6, key
, strlen(key
)) == 0);
1362 char *ptr
= buffer
+ 6 + strlen(key
) + 1;
1365 unsigned long val
= strtoul(ptr
, &end
, 10); /* flags */
1368 verify(end
!= NULL
);
1369 val
= strtoul(end
, &end
, 10); /* size */
1371 verify(val
== datasize
);
1372 verify(end
!= NULL
);
1373 while (*end
!= '\n' && isspace(*end
))
1375 verify(*end
== '\n');
1377 execute(retry_read(buffer
, datasize
));
1378 verify(memcmp(buffer
, value
, datasize
) == 0);
1380 execute(retry_read(buffer
, 2));
1381 verify(memcmp(buffer
, "\r\n", 2) == 0);
1386 static enum test_return
ascii_get_item(const char *key
, const char *value
,
1392 datasize
= strlen(value
);
1394 verify(datasize
< sizeof(buffer
));
1395 snprintf(buffer
, sizeof(buffer
), "get %s\r\n", key
);
1396 execute(send_string(buffer
));
1399 execute(ascii_get_value(key
, value
));
1401 execute(retry_read(buffer
, 5));
1402 verify(memcmp(buffer
, "END\r\n", 5) == 0);
1407 static enum test_return
ascii_gets_value(const char *key
, const char *value
,
1412 size_t datasize
= strlen(value
);
1414 verify(datasize
< sizeof(buffer
));
1415 execute(receive_line(buffer
, sizeof(buffer
)));
1416 verify(strncmp(buffer
, "VALUE ", 6) == 0);
1417 verify(strncmp(buffer
+ 6, key
, strlen(key
)) == 0);
1418 char *ptr
= buffer
+ 6 + strlen(key
) + 1;
1421 unsigned long val
= strtoul(ptr
, &end
, 10); /* flags */
1424 verify(end
!= NULL
);
1425 val
= strtoul(end
, &end
, 10); /* size */
1427 verify(val
== datasize
);
1428 verify(end
!= NULL
);
1429 *cas
= strtoul(end
, &end
, 10); /* cas */
1431 verify(val
== datasize
);
1432 verify(end
!= NULL
);
1434 while (*end
!= '\n' && isspace(*end
))
1436 verify(*end
== '\n');
1438 execute(retry_read(buffer
, datasize
));
1439 verify(memcmp(buffer
, value
, datasize
) == 0);
1441 execute(retry_read(buffer
, 2));
1442 verify(memcmp(buffer
, "\r\n", 2) == 0);
1447 static enum test_return
ascii_gets_item(const char *key
, const char *value
,
1448 bool exist
, unsigned long *cas
)
1453 datasize
= strlen(value
);
1455 verify(datasize
< sizeof(buffer
));
1456 snprintf(buffer
, sizeof(buffer
), "gets %s\r\n", key
);
1457 execute(send_string(buffer
));
1460 execute(ascii_gets_value(key
, value
, cas
));
1462 execute(retry_read(buffer
, 5));
1463 verify(memcmp(buffer
, "END\r\n", 5) == 0);
1468 static enum test_return
ascii_set_item(const char *key
, const char *value
)
1471 size_t len
= strlen(value
);
1472 snprintf(buffer
, sizeof(buffer
), "set %s 0 0 %u\r\n", key
, (unsigned int)len
);
1473 execute(send_string(buffer
));
1474 execute(retry_write(value
, len
));
1475 execute(send_string("\r\n"));
1476 execute(receive_response("STORED\r\n"));
1480 static enum test_return
test_ascii_replace_impl(const char* key
, bool noreply
)
1483 snprintf(buffer
, sizeof(buffer
), "replace %s 0 0 5%s\r\nvalue\r\n", key
, noreply
? " noreply" : "");
1484 execute(send_string(buffer
));
1487 execute(test_ascii_version());
1489 execute(receive_response("NOT_STORED\r\n"));
1491 execute(ascii_set_item(key
, "value"));
1492 execute(ascii_get_item(key
, "value", true));
1495 execute(send_string(buffer
));
1498 execute(test_ascii_version());
1500 execute(receive_response("STORED\r\n"));
1502 return test_ascii_version();
1505 static enum test_return
test_ascii_replace(void)
1507 return test_ascii_replace_impl("test_ascii_replace", false);
1510 static enum test_return
test_ascii_replace_noreply(void)
1512 return test_ascii_replace_impl("test_ascii_replace_noreply", true);
1515 static enum test_return
test_ascii_cas_impl(const char* key
, bool noreply
)
1520 execute(ascii_set_item(key
, "value"));
1521 execute(ascii_gets_item(key
, "value", true, &cas
));
1523 snprintf(buffer
, sizeof(buffer
), "cas %s 0 0 6 %lu%s\r\nvalue2\r\n", key
, cas
, noreply
? " noreply" : "");
1524 execute(send_string(buffer
));
1527 execute(test_ascii_version());
1529 execute(receive_response("STORED\r\n"));
1531 /* reexecute the same command should fail due to illegal cas */
1532 execute(send_string(buffer
));
1535 execute(test_ascii_version());
1537 execute(receive_response("EXISTS\r\n"));
1539 return test_ascii_version();
1542 static enum test_return
test_ascii_cas(void)
1544 return test_ascii_cas_impl("test_ascii_cas", false);
1547 static enum test_return
test_ascii_cas_noreply(void)
1549 return test_ascii_cas_impl("test_ascii_cas_noreply", true);
1552 static enum test_return
test_ascii_delete_impl(const char *key
, bool noreply
)
1554 execute(ascii_set_item(key
, "value"));
1556 execute(send_string("delete\r\n"));
1557 execute(receive_error_response());
1558 /* BUG: the server accepts delete a b */
1559 execute(send_string("delete a b c d e\r\n"));
1560 execute(receive_error_response());
1563 snprintf(buffer
, sizeof(buffer
), "delete %s%s\r\n", key
, noreply
? " noreply" : "");
1564 execute(send_string(buffer
));
1567 execute(test_ascii_version());
1569 execute(receive_response("DELETED\r\n"));
1571 execute(ascii_get_item(key
, "value", false));
1572 execute(send_string(buffer
));
1574 execute(test_ascii_version());
1576 execute(receive_response("NOT_FOUND\r\n"));
1581 static enum test_return
test_ascii_delete(void)
1583 return test_ascii_delete_impl("test_ascii_delete", false);
1586 static enum test_return
test_ascii_delete_noreply(void)
1588 return test_ascii_delete_impl("test_ascii_delete_noreply", true);
1591 static enum test_return
test_ascii_get(void)
1593 execute(ascii_set_item("test_ascii_get", "value"));
1595 execute(send_string("get\r\n"));
1596 execute(receive_error_response());
1597 execute(ascii_get_item("test_ascii_get", "value", true));
1598 execute(ascii_get_item("test_ascii_get_notfound", "value", false));
1603 static enum test_return
test_ascii_gets(void)
1605 execute(ascii_set_item("test_ascii_gets", "value"));
1607 execute(send_string("gets\r\n"));
1608 execute(receive_error_response());
1610 execute(ascii_gets_item("test_ascii_gets", "value", true, &cas
));
1611 execute(ascii_gets_item("test_ascii_gets_notfound", "value", false, &cas
));
1616 static enum test_return
test_ascii_mget(void)
1618 const uint32_t nkeys
= 5;
1619 const char * const keys
[]= {
1622 /* test_ascii_mget_3 does not exist :) */
1628 for (uint32_t x
= 0; x
< nkeys
; ++x
)
1629 execute(ascii_set_item(keys
[x
], "value"));
1631 /* Ask for a key that doesn't exist as well */
1632 execute(send_string("get test_ascii_mget1 test_ascii_mget2 test_ascii_mget3 "
1633 "test_ascii_mget4 test_ascii_mget5 "
1634 "test_ascii_mget6\r\n"));
1636 char *returned
[nkeys
];
1638 for (uint32_t x
= 0; x
< nkeys
; ++x
)
1642 execute(ascii_get_unknown_value(&returned
[x
], &v
, &nbytes
));
1643 verify(nbytes
== 5);
1644 verify(memcmp(v
, "value", 5) == 0);
1649 execute(retry_read(buffer
, 5));
1650 verify(memcmp(buffer
, "END\r\n", 5) == 0);
1652 /* verify that we got all the keys we expected */
1653 for (uint32_t x
= 0; x
< nkeys
; ++x
)
1656 for (uint32_t y
= 0; y
< nkeys
; ++y
)
1658 if (strcmp(keys
[x
], returned
[y
]) == 0)
1667 for (uint32_t x
= 0; x
< nkeys
; ++x
)
1673 static enum test_return
test_ascii_incr_impl(const char* key
, bool noreply
)
1676 snprintf(cmd
, sizeof(cmd
), "incr %s 1%s\r\n", key
, noreply
? " noreply" : "");
1678 execute(ascii_set_item(key
, "0"));
1679 for (int x
= 1; x
< 11; ++x
)
1681 execute(send_string(cmd
));
1684 execute(test_ascii_version());
1688 execute(receive_line(buffer
, sizeof(buffer
)));
1689 int val
= atoi(buffer
);
1694 execute(ascii_get_item(key
, "10", true));
1699 static enum test_return
test_ascii_incr(void)
1701 return test_ascii_incr_impl("test_ascii_incr", false);
1704 static enum test_return
test_ascii_incr_noreply(void)
1706 return test_ascii_incr_impl("test_ascii_incr_noreply", true);
1709 static enum test_return
test_ascii_decr_impl(const char* key
, bool noreply
)
1712 snprintf(cmd
, sizeof(cmd
), "decr %s 1%s\r\n", key
, noreply
? " noreply" : "");
1714 execute(ascii_set_item(key
, "9"));
1715 for (int x
= 8; x
> -1; --x
)
1717 execute(send_string(cmd
));
1720 execute(test_ascii_version());
1724 execute(receive_line(buffer
, sizeof(buffer
)));
1725 int val
= atoi(buffer
);
1730 execute(ascii_get_item(key
, "0", true));
1732 /* verify that it doesn't wrap */
1733 execute(send_string(cmd
));
1735 execute(test_ascii_version());
1739 execute(receive_line(buffer
, sizeof(buffer
)));
1741 execute(ascii_get_item(key
, "0", true));
1746 static enum test_return
test_ascii_decr(void)
1748 return test_ascii_decr_impl("test_ascii_decr", false);
1751 static enum test_return
test_ascii_decr_noreply(void)
1753 return test_ascii_decr_impl("test_ascii_decr_noreply", true);
1757 static enum test_return
test_ascii_flush_impl(const char *key
, bool noreply
)
1760 /* Verify that the flush_all command handles unknown options */
1761 /* Bug in the current memcached server! */
1762 execute(send_string("flush_all foo bar\r\n"));
1763 execute(receive_error_response());
1766 execute(ascii_set_item(key
, key
));
1767 execute(ascii_get_item(key
, key
, true));
1771 execute(send_string("flush_all noreply\r\n"));
1772 execute(test_ascii_version());
1776 execute(send_string("flush_all\r\n"));
1777 execute(receive_response("OK\r\n"));
1780 execute(ascii_get_item(key
, key
, false));
1785 static enum test_return
test_ascii_flush(void)
1787 return test_ascii_flush_impl("test_ascii_flush", false);
1790 static enum test_return
test_ascii_flush_noreply(void)
1792 return test_ascii_flush_impl("test_ascii_flush_noreply", true);
1795 static enum test_return
test_ascii_concat_impl(const char *key
,
1806 execute(ascii_set_item(key
, value
));
1814 snprintf(cmd
, sizeof(cmd
), "%s %s 0 0 %u%s\r\n%s\r\n",
1815 append
? "append" : "prepend",
1816 key
, (unsigned int)strlen(value
), noreply
? " noreply" : "",
1818 execute(send_string(cmd
));
1821 execute(test_ascii_version());
1823 execute(receive_response("STORED\r\n"));
1825 execute(ascii_get_item(key
, "hello world", true));
1827 snprintf(cmd
, sizeof(cmd
), "%s %s_notfound 0 0 %u%s\r\n%s\r\n",
1828 append
? "append" : "prepend",
1829 key
, (unsigned int)strlen(value
), noreply
? " noreply" : "",
1831 execute(send_string(cmd
));
1834 execute(test_ascii_version());
1836 execute(receive_response("NOT_STORED\r\n"));
1841 static enum test_return
test_ascii_append(void)
1843 return test_ascii_concat_impl("test_ascii_append", true, false);
1846 static enum test_return
test_ascii_prepend(void)
1848 return test_ascii_concat_impl("test_ascii_prepend", false, false);
1851 static enum test_return
test_ascii_append_noreply(void)
1853 return test_ascii_concat_impl("test_ascii_append_noreply", true, true);
1856 static enum test_return
test_ascii_prepend_noreply(void)
1858 return test_ascii_concat_impl("test_ascii_prepend_noreply", false, true);
1861 static enum test_return
test_ascii_stat(void)
1863 execute(send_string("stats noreply\r\n"));
1864 execute(receive_error_response());
1865 execute(send_string("stats\r\n"));
1868 execute(receive_line(buffer
, sizeof(buffer
)));
1869 } while (strcmp(buffer
, "END\r\n") != 0);
1871 return TEST_PASS_RECONNECT
;
1874 typedef enum test_return(*TEST_FUNC
)(void);
1878 const char *description
;
1882 struct testcase testcases
[]= {
1883 { "ascii quit", test_ascii_quit
},
1884 { "ascii version", test_ascii_version
},
1885 { "ascii verbosity", test_ascii_verbosity
},
1886 { "ascii set", test_ascii_set
},
1887 { "ascii set noreply", test_ascii_set_noreply
},
1888 { "ascii get", test_ascii_get
},
1889 { "ascii gets", test_ascii_gets
},
1890 { "ascii mget", test_ascii_mget
},
1891 { "ascii flush", test_ascii_flush
},
1892 { "ascii flush noreply", test_ascii_flush_noreply
},
1893 { "ascii add", test_ascii_add
},
1894 { "ascii add noreply", test_ascii_add_noreply
},
1895 { "ascii replace", test_ascii_replace
},
1896 { "ascii replace noreply", test_ascii_replace_noreply
},
1897 { "ascii cas", test_ascii_cas
},
1898 { "ascii cas noreply", test_ascii_cas_noreply
},
1899 { "ascii delete", test_ascii_delete
},
1900 { "ascii delete noreply", test_ascii_delete_noreply
},
1901 { "ascii incr", test_ascii_incr
},
1902 { "ascii incr noreply", test_ascii_incr_noreply
},
1903 { "ascii decr", test_ascii_decr
},
1904 { "ascii decr noreply", test_ascii_decr_noreply
},
1905 { "ascii append", test_ascii_append
},
1906 { "ascii append noreply", test_ascii_append_noreply
},
1907 { "ascii prepend", test_ascii_prepend
},
1908 { "ascii prepend noreply", test_ascii_prepend_noreply
},
1909 { "ascii stat", test_ascii_stat
},
1910 { "binary noop", test_binary_noop
},
1911 { "binary quit", test_binary_quit
},
1912 { "binary quitq", test_binary_quitq
},
1913 { "binary set", test_binary_set
},
1914 { "binary setq", test_binary_setq
},
1915 { "binary flush", test_binary_flush
},
1916 { "binary flushq", test_binary_flushq
},
1917 { "binary add", test_binary_add
},
1918 { "binary addq", test_binary_addq
},
1919 { "binary replace", test_binary_replace
},
1920 { "binary replaceq", test_binary_replaceq
},
1921 { "binary delete", test_binary_delete
},
1922 { "binary deleteq", test_binary_deleteq
},
1923 { "binary get", test_binary_get
},
1924 { "binary getq", test_binary_getq
},
1925 { "binary getk", test_binary_getk
},
1926 { "binary getkq", test_binary_getkq
},
1927 { "binary incr", test_binary_incr
},
1928 { "binary incrq", test_binary_incrq
},
1929 { "binary decr", test_binary_decr
},
1930 { "binary decrq", test_binary_decrq
},
1931 { "binary version", test_binary_version
},
1932 { "binary append", test_binary_append
},
1933 { "binary appendq", test_binary_appendq
},
1934 { "binary prepend", test_binary_prepend
},
1935 { "binary prependq", test_binary_prependq
},
1936 { "binary stat", test_binary_stat
},
1940 int main(int argc
, char **argv
)
1942 static const char * const status_msg
[]= {"[skip]", "[pass]", "[pass]", "[FAIL]"};
1945 const char *hostname
= "localhost";
1946 const char *port
= "11211";
1949 const char *testname
= NULL
;
1951 while ((cmd
= getopt(argc
, argv
, "t:vch:p:PT:?")) != EOF
)
1955 timeout
= atoi(optarg
);
1958 fprintf(stderr
, "Invalid timeout. Please specify a number for -t\n");
1962 case 'v': verbose
= true;
1964 case 'c': do_core
= true;
1966 case 'h': hostname
= optarg
;
1968 case 'p': port
= optarg
;
1970 case 'P': prompt
= true;
1972 case 'T': testname
= optarg
;
1975 fprintf(stderr
, "Usage: %s [-h hostname] [-p port] [-c] [-v] [-t n]"
1976 " [-P] [-T testname]'\n"
1977 "\t-c\tGenerate coredump if a test fails\n"
1978 "\t-v\tVerbose test output (print out the assertion)\n"
1979 "\t-t n\tSet the timeout for io-operations to n seconds\n"
1980 "\t-P\tPrompt the user before starting a test.\n"
1981 "\t\t\t\"skip\" will skip the test\n"
1982 "\t\t\t\"quit\" will terminate memcapable\n"
1983 "\t\t\tEverything else will start the test\n"
1984 "\t-T n\tJust run the test named n\n",
1990 initialize_sockets();
1991 sock
= connect_server(hostname
, port
);
1992 if (sock
== INVALID_SOCKET
)
1994 fprintf(stderr
, "Failed to connect to <%s:%s>: %s\n",
1995 hostname
, port
, strerror(get_socket_errno()));
1999 for (int ii
= 0; testcases
[ii
].description
!= NULL
; ++ii
)
2001 if (testname
!= NULL
&& strcmp(testcases
[ii
].description
, testname
) != 0)
2005 fprintf(stdout
, "%-40s", testcases
[ii
].description
);
2010 fprintf(stdout
, "\nPress <return> when you are ready? ");
2011 char buffer
[80] = {0};
2012 if (fgets(buffer
, sizeof(buffer
), stdin
) != NULL
) {
2013 if (strncmp(buffer
, "skip", 4) == 0)
2015 fprintf(stdout
, "%-40s%s\n", testcases
[ii
].description
,
2016 status_msg
[TEST_SKIP
]);
2020 if (strncmp(buffer
, "quit", 4) == 0)
2024 fprintf(stdout
, "%-40s", testcases
[ii
].description
);
2028 bool reconnect
= false;
2029 enum test_return ret
= testcases
[ii
].function();
2030 if (ret
== TEST_FAIL
)
2035 fprintf(stderr
, "\n");
2037 else if (ret
== TEST_PASS_RECONNECT
)
2040 fprintf(stderr
, "%s\n", status_msg
[ret
]);
2044 if ((sock
= connect_server(hostname
, port
)) == INVALID_SOCKET
)
2046 fprintf(stderr
, "Failed to connect to <%s:%s>: %s\n",
2047 hostname
, port
, strerror(get_socket_errno()));
2048 fprintf(stderr
, "%d of %d tests failed\n", failed
, total
);
2056 fprintf(stdout
, "All tests passed\n");
2058 fprintf(stderr
, "%d of %d tests failed\n", failed
, total
);
2060 return (failed
== 0) ? 0 : 1;