2 * Copyright (C) 2011-2012 Data Differential, http://datadifferential.com/
3 * Copyright (C) 2006-2009 Brian Aker
6 * Use and distribution licensed under the BSD license. See
7 * the COPYING file in the parent directory for full text.
13 /* -*- Mode: C; tab-width: 2; c-basic-offset: 2; indent-tabs-mode: nil -*- */
16 #include "mem_config.h"
21 #include "poll/poll.h"
34 #include <sys/types.h>
37 #include "libmemcached-1.0/memcached.h"
39 #include "libmemcached/socket.hpp"
40 #include "libmemcachedprotocol-0.0/binary.h"
41 #include "libmemcached/byteorder.h"
42 #include "utilities.h"
47 /* /usr/include/netinet/in.h defines macros from ntohs() to _bswap_nn to
48 * optimize the conversion functions, but the prototypes generate warnings
49 * from gcc. The conversion methods isn't the bottleneck for my app, so
50 * just remove the warnings by undef'ing the optimization ..
56 /* Should we generate coredumps when we enounter an error (-c) */
57 static bool do_core
= false;
58 /* connection to the server */
59 static memcached_socket_t sock
;
60 /* Should the output from test failures be verbose or quiet? */
61 static bool verbose
= false;
63 /* The number of seconds to wait for an IO-operation */
64 static int timeout
= 2;
66 /* v1.6.x is more permissible */
67 static bool v16x_or_greater
= false;
70 * Instead of having to cast between the different datatypes we create
71 * a union of all of the different types of pacages we want to send.
72 * A lot of the different commands use the same packet layout, so I'll
73 * just define the different types I need. The typedefs only contain
74 * the header of the message, so we need some space for keys and body
75 * To avoid to have to do multiple writes, lets add a chunk of memory
76 * to use. 1k should be more than enough for header, key and body.
80 protocol_binary_request_no_extras plain
;
81 protocol_binary_request_flush flush
;
82 protocol_binary_request_incr incr
;
83 protocol_binary_request_set set
;
89 protocol_binary_response_no_extras plain
;
90 protocol_binary_response_incr incr
;
91 protocol_binary_response_decr decr
;
97 TEST_SKIP
, TEST_PASS
, TEST_PASS_RECONNECT
, TEST_FAIL
101 * Try to get an addrinfo struct for a given port on a given host
103 static struct addrinfo
*lookuphost(const char *hostname
, const char *port
)
105 struct addrinfo
*ai
= 0;
106 struct addrinfo hints
;
107 memset(&hints
, 0, sizeof(struct addrinfo
));
108 hints
.ai_family
=AF_UNSPEC
;
109 hints
.ai_protocol
=IPPROTO_TCP
;
110 hints
.ai_socktype
=SOCK_STREAM
;
112 int error
= getaddrinfo(hostname
, port
, &hints
, &ai
);
115 if (error
!= EAI_SYSTEM
)
117 fprintf(stderr
, "getaddrinfo(): %s\n", gai_strerror(error
));
121 perror("getaddrinfo()");
129 * Set the socket in nonblocking mode
130 * @return -1 if failure, the socket otherwise
132 static memcached_socket_t
set_noblock(void)
136 if (ioctlsocket(sock
, FIONBIO
, &arg
) == SOCKET_ERROR
)
138 perror("Failed to set nonblocking io");
140 return INVALID_SOCKET
;
143 int flags
= fcntl(sock
, F_GETFL
, 0);
146 perror("Failed to get socket flags");
147 memcached_close_socket(sock
);
148 return INVALID_SOCKET
;
151 if ((flags
& O_NONBLOCK
) != O_NONBLOCK
)
153 if (fcntl(sock
, F_SETFL
, flags
| O_NONBLOCK
) == -1)
155 perror("Failed to set socket to nonblocking mode");
156 memcached_close_socket(sock
);
157 return INVALID_SOCKET
;
165 * Try to open a connection to the server
166 * @param hostname the name of the server to connect to
167 * @param port the port number (or service) to connect to
168 * @return positive integer if success, -1 otherwise
170 static memcached_socket_t
connect_server(const char *hostname
, const char *port
)
172 struct addrinfo
*ai
= lookuphost(hostname
, port
);
173 sock
= INVALID_SOCKET
;
176 if ((sock
= socket(ai
->ai_family
, ai
->ai_socktype
,
177 ai
->ai_protocol
)) != INVALID_SOCKET
)
179 if (connect(sock
, ai
->ai_addr
, ai
->ai_addrlen
) == SOCKET_ERROR
)
181 fprintf(stderr
, "Failed to connect socket: %s\n",
182 strerror(get_socket_errno()));
184 sock
= INVALID_SOCKET
;
193 fprintf(stderr
, "Failed to create socket: %s\n", strerror(get_socket_errno()));
202 static ssize_t
timeout_io_op(memcached_socket_t fd
, short direction
, void *buf
, size_t len
)
206 if (direction
== POLLOUT
)
208 ret
= send(fd
, buf
, len
, 0);
212 ret
= recv(fd
, buf
, len
, 0);
215 if (ret
== SOCKET_ERROR
&& get_socket_errno() == EWOULDBLOCK
)
218 memset(&fds
, 0, sizeof(struct pollfd
));
219 fds
.events
= direction
;
222 int err
= poll(&fds
, 1, timeout
* 1000);
225 if (direction
== POLLOUT
)
227 ret
= send(fd
, buf
, len
, 0);
231 ret
= recv(fd
, buf
, len
, 0);
240 perror("Failed to poll");
249 * Ensure that an expression is true. If it isn't print out a message similar
250 * to assert() and create a coredump if the user wants that. If not an error
251 * message is returned.
254 static enum test_return
ensure(bool val
, const char *expression
, const char *file
, int line
)
260 fprintf(stdout
, "\n%s:%d: %s", file
, line
, expression
);
274 #define verify(expression) do { if (ensure(expression, #expression, __FILE__, __LINE__) == TEST_FAIL) return TEST_FAIL; } while (0)
275 #define execute(expression) do { if (ensure(expression == TEST_PASS, #expression, __FILE__, __LINE__) == TEST_FAIL) return TEST_FAIL; } while (0)
278 * Send a chunk of memory over the socket (retry if the call is iterrupted
280 static enum test_return
retry_write(const void* buf
, size_t len
)
283 const char* ptr
= static_cast<const char *>(buf
);
287 size_t num_bytes
= len
- offset
;
288 ssize_t nw
= timeout_io_op(sock
, POLLOUT
, (void*)(ptr
+ offset
), num_bytes
);
291 verify(get_socket_errno() == EINTR
|| get_socket_errno() == EAGAIN
);
298 } while (offset
< len
);
304 * Resend a packet to the server (All fields in the command header should
305 * be in network byte order)
307 static enum test_return
resend_packet(command
*cmd
)
309 size_t length
= sizeof (protocol_binary_request_no_extras
) +
310 ntohl(cmd
->plain
.message
.header
.request
.bodylen
);
312 execute(retry_write(cmd
, length
));
317 * Send a command to the server. The command header needs to be updated
318 * to network byte order
320 static enum test_return
send_packet(command
*cmd
)
322 /* Fix the byteorder of the header */
323 cmd
->plain
.message
.header
.request
.keylen
=
324 ntohs(cmd
->plain
.message
.header
.request
.keylen
);
325 cmd
->plain
.message
.header
.request
.bodylen
=
326 ntohl(cmd
->plain
.message
.header
.request
.bodylen
);
327 cmd
->plain
.message
.header
.request
.cas
=
328 memcached_ntohll(cmd
->plain
.message
.header
.request
.cas
);
330 execute(resend_packet(cmd
));
335 * Read a fixed length chunk of data from the server
337 static enum test_return
retry_read(void *buf
, size_t len
)
342 ssize_t nr
= timeout_io_op(sock
, POLLIN
, ((char*) buf
) + offset
, len
- offset
);
345 fprintf(stderr
, "Errno: %d %s\n", get_socket_errno(), strerror(errno
));
346 verify(get_socket_errno() == EINTR
|| get_socket_errno() == EAGAIN
);
355 } while (offset
< len
);
361 * Receive a response from the server and conver the fields in the header
362 * to local byte order
364 static enum test_return
recv_packet(response
*rsp
)
366 execute(retry_read(rsp
, sizeof(protocol_binary_response_no_extras
)));
368 /* Fix the byte order in the packet header */
369 rsp
->plain
.message
.header
.response
.keylen
=
370 ntohs(rsp
->plain
.message
.header
.response
.keylen
);
371 rsp
->plain
.message
.header
.response
.status
=
372 ntohs(rsp
->plain
.message
.header
.response
.status
);
373 rsp
->plain
.message
.header
.response
.bodylen
=
374 ntohl(rsp
->plain
.message
.header
.response
.bodylen
);
375 rsp
->plain
.message
.header
.response
.cas
=
376 memcached_ntohll(rsp
->plain
.message
.header
.response
.cas
);
378 size_t bodysz
= rsp
->plain
.message
.header
.response
.bodylen
;
380 execute(retry_read(rsp
->bytes
+ sizeof (protocol_binary_response_no_extras
), bodysz
));
386 * Create a storage command (add, set, replace etc)
388 * @param cmd destination buffer
389 * @param cc the storage command to create
390 * @param key the key to store
391 * @param keylen the length of the key
392 * @param dta the data to store with the key
393 * @param dtalen the length of the data to store with the key
394 * @param flags the flags to store along with the key
395 * @param exptime the expiry time for the key
397 static void storage_command(command
*cmd
,
406 /* all of the storage commands use the same command layout */
407 protocol_binary_request_set
*request
= &cmd
->set
;
409 memset(request
, 0, sizeof (*request
));
410 request
->message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
411 request
->message
.header
.request
.opcode
= cc
;
412 request
->message
.header
.request
.keylen
= (uint16_t)keylen
;
413 request
->message
.header
.request
.extlen
= 8;
414 request
->message
.header
.request
.bodylen
= (uint32_t)(keylen
+ 8 + dtalen
);
415 request
->message
.header
.request
.opaque
= 0xdeadbeef;
416 request
->message
.body
.flags
= flags
;
417 request
->message
.body
.expiration
= exptime
;
419 off_t key_offset
= sizeof (protocol_binary_request_no_extras
) + 8;
420 memcpy(cmd
->bytes
+ key_offset
, key
, keylen
);
422 memcpy(cmd
->bytes
+ key_offset
+ keylen
, dta
, dtalen
);
426 * Create a basic command to send to the server
427 * @param cmd destination buffer
428 * @param cc the command to create
429 * @param key the key to store
430 * @param keylen the length of the key
431 * @param dta the data to store with the key
432 * @param dtalen the length of the data to store with the key
434 static void raw_command(command
*cmd
,
441 /* all of the storage commands use the same command layout */
442 memset(cmd
, 0, sizeof (*cmd
));
443 cmd
->plain
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
444 cmd
->plain
.message
.header
.request
.opcode
= cc
;
445 cmd
->plain
.message
.header
.request
.keylen
= (uint16_t)keylen
;
446 cmd
->plain
.message
.header
.request
.bodylen
= (uint32_t)(keylen
+ dtalen
);
447 cmd
->plain
.message
.header
.request
.opaque
= 0xdeadbeef;
449 off_t key_offset
= sizeof (protocol_binary_request_no_extras
);
452 memcpy(cmd
->bytes
+ key_offset
, key
, keylen
);
455 memcpy(cmd
->bytes
+ key_offset
+ keylen
, dta
, dtalen
);
459 * Create the flush command
460 * @param cmd destination buffer
461 * @param cc the command to create (FLUSH/FLUSHQ)
462 * @param exptime when to flush
463 * @param use_extra to force using of the extra field?
465 static void flush_command(command
*cmd
,
466 uint8_t cc
, uint32_t exptime
, bool use_extra
)
468 memset(cmd
, 0, sizeof (cmd
->flush
));
469 cmd
->flush
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
470 cmd
->flush
.message
.header
.request
.opcode
= cc
;
471 cmd
->flush
.message
.header
.request
.opaque
= 0xdeadbeef;
473 if (exptime
!= 0 || use_extra
)
475 cmd
->flush
.message
.header
.request
.extlen
= 4;
476 cmd
->flush
.message
.body
.expiration
= htonl(exptime
);
477 cmd
->flush
.message
.header
.request
.bodylen
= 4;
482 * Create a incr/decr command
483 * @param cc the cmd to create (FLUSH/FLUSHQ)
484 * @param key the key to operate on
485 * @param keylen the number of bytes in the key
486 * @param delta the number to add/subtract
487 * @param initial the initial value if the key doesn't exist
488 * @param exptime when the key should expire if it isn't set
490 static void arithmetic_command(command
*cmd
,
498 memset(cmd
, 0, sizeof (cmd
->incr
));
499 cmd
->incr
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
500 cmd
->incr
.message
.header
.request
.opcode
= cc
;
501 cmd
->incr
.message
.header
.request
.keylen
= (uint16_t)keylen
;
502 cmd
->incr
.message
.header
.request
.extlen
= 20;
503 cmd
->incr
.message
.header
.request
.bodylen
= (uint32_t)(keylen
+ 20);
504 cmd
->incr
.message
.header
.request
.opaque
= 0xdeadbeef;
505 cmd
->incr
.message
.body
.delta
= memcached_htonll(delta
);
506 cmd
->incr
.message
.body
.initial
= memcached_htonll(initial
);
507 cmd
->incr
.message
.body
.expiration
= htonl(exptime
);
509 off_t key_offset
= sizeof (protocol_binary_request_no_extras
) + 20;
510 memcpy(cmd
->bytes
+ key_offset
, key
, keylen
);
514 * Validate the response header from the server
515 * @param rsp the response to check
516 * @param cc the expected command
517 * @param status the expected status
519 static enum test_return
do_validate_response_header(response
*rsp
,
520 uint8_t cc
, uint16_t status
)
522 verify(rsp
->plain
.message
.header
.response
.magic
== PROTOCOL_BINARY_RES
);
523 verify(rsp
->plain
.message
.header
.response
.opcode
== cc
);
524 verify(rsp
->plain
.message
.header
.response
.datatype
== PROTOCOL_BINARY_RAW_BYTES
);
525 verify(rsp
->plain
.message
.header
.response
.status
== status
);
526 verify(rsp
->plain
.message
.header
.response
.opaque
== 0xdeadbeef);
528 if (status
== PROTOCOL_BINARY_RESPONSE_SUCCESS
)
531 case PROTOCOL_BINARY_CMD_ADDQ
:
532 case PROTOCOL_BINARY_CMD_APPENDQ
:
533 case PROTOCOL_BINARY_CMD_DECREMENTQ
:
534 case PROTOCOL_BINARY_CMD_DELETEQ
:
535 case PROTOCOL_BINARY_CMD_FLUSHQ
:
536 case PROTOCOL_BINARY_CMD_INCREMENTQ
:
537 case PROTOCOL_BINARY_CMD_PREPENDQ
:
538 case PROTOCOL_BINARY_CMD_QUITQ
:
539 case PROTOCOL_BINARY_CMD_REPLACEQ
:
540 case PROTOCOL_BINARY_CMD_SETQ
:
541 verify("Quiet command shouldn't return on success" == NULL
);
548 case PROTOCOL_BINARY_CMD_ADD
:
549 case PROTOCOL_BINARY_CMD_REPLACE
:
550 case PROTOCOL_BINARY_CMD_SET
:
551 case PROTOCOL_BINARY_CMD_APPEND
:
552 case PROTOCOL_BINARY_CMD_PREPEND
:
553 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
554 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
555 verify(rsp
->plain
.message
.header
.response
.bodylen
== 0);
556 verify(rsp
->plain
.message
.header
.response
.cas
!= 0);
558 case PROTOCOL_BINARY_CMD_FLUSH
:
559 case PROTOCOL_BINARY_CMD_NOOP
:
560 case PROTOCOL_BINARY_CMD_QUIT
:
561 case PROTOCOL_BINARY_CMD_DELETE
:
562 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
563 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
564 verify(rsp
->plain
.message
.header
.response
.bodylen
== 0);
565 verify(rsp
->plain
.message
.header
.response
.cas
== 0);
568 case PROTOCOL_BINARY_CMD_DECREMENT
:
569 case PROTOCOL_BINARY_CMD_INCREMENT
:
570 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
571 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
572 verify(rsp
->plain
.message
.header
.response
.bodylen
== 8);
573 verify(rsp
->plain
.message
.header
.response
.cas
!= 0);
576 case PROTOCOL_BINARY_CMD_STAT
:
577 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
578 /* key and value exists in all packets except in the terminating */
579 verify(rsp
->plain
.message
.header
.response
.cas
== 0);
582 case PROTOCOL_BINARY_CMD_VERSION
:
583 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
584 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
585 verify(rsp
->plain
.message
.header
.response
.bodylen
!= 0);
586 verify(rsp
->plain
.message
.header
.response
.cas
== 0);
589 case PROTOCOL_BINARY_CMD_GET
:
590 case PROTOCOL_BINARY_CMD_GETQ
:
591 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
592 verify(rsp
->plain
.message
.header
.response
.extlen
== 4);
593 verify(rsp
->plain
.message
.header
.response
.cas
!= 0);
596 case PROTOCOL_BINARY_CMD_GETK
:
597 case PROTOCOL_BINARY_CMD_GETKQ
:
598 verify(rsp
->plain
.message
.header
.response
.keylen
!= 0);
599 verify(rsp
->plain
.message
.header
.response
.extlen
== 4);
600 verify(rsp
->plain
.message
.header
.response
.cas
!= 0);
604 /* Undefined command code */
610 verify(rsp
->plain
.message
.header
.response
.cas
== 0);
611 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
612 if (cc
!= PROTOCOL_BINARY_CMD_GETK
)
614 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
621 /* We call verify(validate_response_header), but that macro
622 * expects a boolean expression, and the function returns
623 * an enum.... Let's just create a macro to avoid cluttering
624 * the code with all of the == TEST_PASS ;-)
626 #define validate_response_header(a,b,c) \
627 do_validate_response_header(a,b,c) == TEST_PASS
630 static enum test_return
send_binary_noop(void)
633 raw_command(&cmd
, PROTOCOL_BINARY_CMD_NOOP
, NULL
, 0, NULL
, 0);
634 execute(send_packet(&cmd
));
638 static enum test_return
receive_binary_noop(void)
641 execute(recv_packet(&rsp
));
642 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_NOOP
,
643 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
647 static enum test_return
test_binary_noop(void)
649 execute(send_binary_noop());
650 execute(receive_binary_noop());
654 static enum test_return
test_binary_quit_impl(uint8_t cc
)
658 raw_command(&cmd
, cc
, NULL
, 0, NULL
, 0);
660 execute(send_packet(&cmd
));
661 if (cc
== PROTOCOL_BINARY_CMD_QUIT
)
663 execute(recv_packet(&rsp
));
664 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_QUIT
,
665 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
668 /* Socket should be closed now, read should return EXIT_SUCCESS */
669 verify(timeout_io_op(sock
, POLLIN
, rsp
.bytes
, sizeof(rsp
.bytes
)) == 0);
671 return TEST_PASS_RECONNECT
;
674 static enum test_return
test_binary_quit(void)
676 return test_binary_quit_impl(PROTOCOL_BINARY_CMD_QUIT
);
679 static enum test_return
test_binary_quitq(void)
681 return test_binary_quit_impl(PROTOCOL_BINARY_CMD_QUITQ
);
684 static enum test_return
test_binary_set_impl(const char* key
, uint8_t cc
)
689 uint64_t value
= 0xdeadbeefdeadcafeULL
;
690 storage_command(&cmd
, cc
, key
, strlen(key
), &value
, sizeof (value
), 0, 0);
692 /* set should always work */
693 for (int ii
= 0; ii
< 10; ii
++)
697 execute(send_packet(&cmd
));
701 execute(resend_packet(&cmd
));
704 if (cc
== PROTOCOL_BINARY_CMD_SET
)
706 execute(recv_packet(&rsp
));
707 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
710 execute(test_binary_noop());
714 * We need to get the current CAS id, and at this time we haven't
715 * verified that we have a working get
717 if (cc
== PROTOCOL_BINARY_CMD_SETQ
)
719 cmd
.set
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_SET
;
720 execute(resend_packet(&cmd
));
721 execute(recv_packet(&rsp
));
722 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_SET
,
723 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
724 cmd
.set
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_SETQ
;
727 /* try to set with the correct CAS value */
728 cmd
.plain
.message
.header
.request
.cas
= memcached_htonll(rsp
.plain
.message
.header
.response
.cas
);
729 execute(resend_packet(&cmd
));
730 if (cc
== PROTOCOL_BINARY_CMD_SET
)
732 execute(recv_packet(&rsp
));
733 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
736 execute(test_binary_noop());
738 /* try to set with an incorrect CAS value */
739 cmd
.plain
.message
.header
.request
.cas
= memcached_htonll(rsp
.plain
.message
.header
.response
.cas
- 1);
740 execute(resend_packet(&cmd
));
741 execute(send_binary_noop());
742 execute(recv_packet(&rsp
));
743 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS
));
744 execute(receive_binary_noop());
749 static enum test_return
test_binary_set(void)
751 return test_binary_set_impl("test_binary_set", PROTOCOL_BINARY_CMD_SET
);
754 static enum test_return
test_binary_setq(void)
756 return test_binary_set_impl("test_binary_setq", PROTOCOL_BINARY_CMD_SETQ
);
759 static enum test_return
test_binary_add_impl(const char* key
, uint8_t cc
)
763 uint64_t value
= 0xdeadbeefdeadcafeULL
;
764 storage_command(&cmd
, cc
, key
, strlen(key
), &value
, sizeof (value
), 0, 0);
766 /* first add should work, rest of them should fail (even with cas
768 for (int ii
=0; ii
< 10; ii
++)
771 execute(send_packet(&cmd
));
773 execute(resend_packet(&cmd
));
775 if (cc
== PROTOCOL_BINARY_CMD_ADD
|| ii
> 0)
777 uint16_t expected_result
;
779 expected_result
= PROTOCOL_BINARY_RESPONSE_SUCCESS
;
781 expected_result
= PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS
;
783 execute(send_binary_noop());
784 execute(recv_packet(&rsp
));
785 execute(receive_binary_noop());
786 verify(validate_response_header(&rsp
, cc
, expected_result
));
789 execute(test_binary_noop());
795 static enum test_return
test_binary_add(void)
797 return test_binary_add_impl("test_binary_add", PROTOCOL_BINARY_CMD_ADD
);
800 static enum test_return
test_binary_addq(void)
802 return test_binary_add_impl("test_binary_addq", PROTOCOL_BINARY_CMD_ADDQ
);
805 static enum test_return
binary_set_item(const char *key
, const char *value
)
809 storage_command(&cmd
, PROTOCOL_BINARY_CMD_SET
, key
, strlen(key
),
810 value
, strlen(value
), 0, 0);
811 execute(send_packet(&cmd
));
812 execute(recv_packet(&rsp
));
813 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_SET
,
814 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
818 static enum test_return
test_binary_replace_impl(const char* key
, uint8_t cc
)
822 uint64_t value
= 0xdeadbeefdeadcafeULL
;
823 storage_command(&cmd
, cc
, key
, strlen(key
), &value
, sizeof (value
), 0, 0);
825 /* first replace should fail, successive should succeed (when the
827 for (int ii
= 0; ii
< 10; ii
++)
831 execute(send_packet(&cmd
));
835 execute(resend_packet(&cmd
));
838 if (cc
== PROTOCOL_BINARY_CMD_REPLACE
|| ii
== 0)
840 uint16_t expected_result
;
843 expected_result
=PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
;
847 expected_result
=PROTOCOL_BINARY_RESPONSE_SUCCESS
;
850 execute(send_binary_noop());
851 execute(recv_packet(&rsp
));
852 execute(receive_binary_noop());
853 verify(validate_response_header(&rsp
, cc
, expected_result
));
856 execute(binary_set_item(key
, key
));
860 execute(test_binary_noop());
864 /* verify that replace with CAS value works! */
865 cmd
.plain
.message
.header
.request
.cas
= memcached_htonll(rsp
.plain
.message
.header
.response
.cas
);
866 execute(resend_packet(&cmd
));
868 if (cc
== PROTOCOL_BINARY_CMD_REPLACE
)
870 execute(recv_packet(&rsp
));
871 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
874 execute(test_binary_noop());
876 /* try to set with an incorrect CAS value */
877 cmd
.plain
.message
.header
.request
.cas
= memcached_htonll(rsp
.plain
.message
.header
.response
.cas
- 1);
878 execute(resend_packet(&cmd
));
879 execute(send_binary_noop());
880 execute(recv_packet(&rsp
));
881 execute(receive_binary_noop());
882 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS
));
887 static enum test_return
test_binary_replace(void)
889 return test_binary_replace_impl("test_binary_replace", PROTOCOL_BINARY_CMD_REPLACE
);
892 static enum test_return
test_binary_replaceq(void)
894 return test_binary_replace_impl("test_binary_replaceq", PROTOCOL_BINARY_CMD_REPLACEQ
);
897 static enum test_return
test_binary_delete_impl(const char *key
, uint8_t cc
)
901 raw_command(&cmd
, cc
, key
, strlen(key
), NULL
, 0);
903 /* The delete shouldn't work the first time, because the item isn't there */
904 execute(send_packet(&cmd
));
905 execute(send_binary_noop());
906 execute(recv_packet(&rsp
));
907 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
));
908 execute(receive_binary_noop());
909 execute(binary_set_item(key
, key
));
911 /* The item should be present now, resend*/
912 execute(resend_packet(&cmd
));
913 if (cc
== PROTOCOL_BINARY_CMD_DELETE
)
915 execute(recv_packet(&rsp
));
916 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
919 execute(test_binary_noop());
924 static enum test_return
test_binary_delete(void)
926 return test_binary_delete_impl("test_binary_delete", PROTOCOL_BINARY_CMD_DELETE
);
929 static enum test_return
test_binary_deleteq(void)
931 return test_binary_delete_impl("test_binary_deleteq", PROTOCOL_BINARY_CMD_DELETEQ
);
934 static enum test_return
test_binary_get_impl(const char *key
, uint8_t cc
)
939 raw_command(&cmd
, cc
, key
, strlen(key
), NULL
, 0);
940 execute(send_packet(&cmd
));
941 execute(send_binary_noop());
943 if (cc
== PROTOCOL_BINARY_CMD_GET
|| cc
== PROTOCOL_BINARY_CMD_GETK
)
945 execute(recv_packet(&rsp
));
946 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
));
949 execute(receive_binary_noop());
951 execute(binary_set_item(key
, key
));
952 execute(resend_packet(&cmd
));
953 execute(send_binary_noop());
955 execute(recv_packet(&rsp
));
956 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
957 execute(receive_binary_noop());
962 static enum test_return
test_binary_get(void)
964 return test_binary_get_impl("test_binary_get", PROTOCOL_BINARY_CMD_GET
);
967 static enum test_return
test_binary_getk(void)
969 return test_binary_get_impl("test_binary_getk", PROTOCOL_BINARY_CMD_GETK
);
972 static enum test_return
test_binary_getq(void)
974 return test_binary_get_impl("test_binary_getq", PROTOCOL_BINARY_CMD_GETQ
);
977 static enum test_return
test_binary_getkq(void)
979 return test_binary_get_impl("test_binary_getkq", PROTOCOL_BINARY_CMD_GETKQ
);
982 static enum test_return
test_binary_incr_impl(const char* key
, uint8_t cc
)
986 arithmetic_command(&cmd
, cc
, key
, strlen(key
), 1, 0, 0);
989 for (ii
= 0; ii
< 10; ++ii
)
992 execute(send_packet(&cmd
));
994 execute(resend_packet(&cmd
));
996 if (cc
== PROTOCOL_BINARY_CMD_INCREMENT
)
998 execute(recv_packet(&rsp
));
999 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1000 verify(memcached_ntohll(rsp
.incr
.message
.body
.value
) == ii
);
1003 execute(test_binary_noop());
1006 /* @todo add incorrect CAS */
1010 static enum test_return
test_binary_incr(void)
1012 return test_binary_incr_impl("test_binary_incr", PROTOCOL_BINARY_CMD_INCREMENT
);
1015 static enum test_return
test_binary_incrq(void)
1017 return test_binary_incr_impl("test_binary_incrq", PROTOCOL_BINARY_CMD_INCREMENTQ
);
1020 static enum test_return
test_binary_decr_impl(const char* key
, uint8_t cc
)
1024 arithmetic_command(&cmd
, cc
, key
, strlen(key
), 1, 9, 0);
1027 for (ii
= 9; ii
> -1; --ii
)
1030 execute(send_packet(&cmd
));
1032 execute(resend_packet(&cmd
));
1034 if (cc
== PROTOCOL_BINARY_CMD_DECREMENT
)
1036 execute(recv_packet(&rsp
));
1037 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1038 verify(memcached_ntohll(rsp
.decr
.message
.body
.value
) == (uint64_t)ii
);
1041 execute(test_binary_noop());
1044 /* decr 0 should not wrap */
1045 execute(resend_packet(&cmd
));
1046 if (cc
== PROTOCOL_BINARY_CMD_DECREMENT
)
1048 execute(recv_packet(&rsp
));
1049 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1050 verify(memcached_ntohll(rsp
.decr
.message
.body
.value
) == 0);
1054 /* @todo get the value and verify! */
1058 /* @todo add incorrect cas */
1059 execute(test_binary_noop());
1063 static enum test_return
test_binary_decr(void)
1065 return test_binary_decr_impl("test_binary_decr",
1066 PROTOCOL_BINARY_CMD_DECREMENT
);
1069 static enum test_return
test_binary_decrq(void)
1071 return test_binary_decr_impl("test_binary_decrq",
1072 PROTOCOL_BINARY_CMD_DECREMENTQ
);
1075 static enum test_return
test_binary_version(void)
1079 raw_command(&cmd
, PROTOCOL_BINARY_CMD_VERSION
, NULL
, 0, NULL
, 0);
1081 execute(send_packet(&cmd
));
1082 execute(recv_packet(&rsp
));
1083 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_VERSION
,
1084 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1089 static enum test_return
test_binary_flush_impl(const char *key
, uint8_t cc
)
1094 for (int ii
= 0; ii
< 2; ++ii
)
1096 execute(binary_set_item(key
, key
));
1097 flush_command(&cmd
, cc
, 0, ii
== 0);
1098 execute(send_packet(&cmd
));
1100 if (cc
== PROTOCOL_BINARY_CMD_FLUSH
)
1102 execute(recv_packet(&rsp
));
1103 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1106 execute(test_binary_noop());
1108 raw_command(&cmd
, PROTOCOL_BINARY_CMD_GET
, key
, strlen(key
), NULL
, 0);
1109 execute(send_packet(&cmd
));
1110 execute(recv_packet(&rsp
));
1111 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_GET
,
1112 PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
));
1118 static enum test_return
test_binary_flush(void)
1120 return test_binary_flush_impl("test_binary_flush", PROTOCOL_BINARY_CMD_FLUSH
);
1123 static enum test_return
test_binary_flushq(void)
1125 return test_binary_flush_impl("test_binary_flushq", PROTOCOL_BINARY_CMD_FLUSHQ
);
1128 static enum test_return
test_binary_concat_impl(const char *key
, uint8_t cc
)
1134 if (cc
== PROTOCOL_BINARY_CMD_APPEND
|| cc
== PROTOCOL_BINARY_CMD_APPENDQ
)
1143 execute(binary_set_item(key
, value
));
1145 if (cc
== PROTOCOL_BINARY_CMD_APPEND
|| cc
== PROTOCOL_BINARY_CMD_APPENDQ
)
1154 raw_command(&cmd
, cc
, key
, strlen(key
), value
, strlen(value
));
1155 execute(send_packet(&cmd
));
1156 if (cc
== PROTOCOL_BINARY_CMD_APPEND
|| cc
== PROTOCOL_BINARY_CMD_PREPEND
)
1158 execute(recv_packet(&rsp
));
1159 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1163 execute(test_binary_noop());
1166 raw_command(&cmd
, PROTOCOL_BINARY_CMD_GET
, key
, strlen(key
), NULL
, 0);
1167 execute(send_packet(&cmd
));
1168 execute(recv_packet(&rsp
));
1169 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_GET
,
1170 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1171 verify(rsp
.plain
.message
.header
.response
.bodylen
- 4 == 11);
1172 verify(memcmp(rsp
.bytes
+ 28, "hello world", 11) == 0);
1177 static enum test_return
test_binary_append(void)
1179 return test_binary_concat_impl("test_binary_append", PROTOCOL_BINARY_CMD_APPEND
);
1182 static enum test_return
test_binary_prepend(void)
1184 return test_binary_concat_impl("test_binary_prepend", PROTOCOL_BINARY_CMD_PREPEND
);
1187 static enum test_return
test_binary_appendq(void)
1189 return test_binary_concat_impl("test_binary_appendq", PROTOCOL_BINARY_CMD_APPENDQ
);
1192 static enum test_return
test_binary_prependq(void)
1194 return test_binary_concat_impl("test_binary_prependq", PROTOCOL_BINARY_CMD_PREPENDQ
);
1197 static enum test_return
test_binary_stat(void)
1202 raw_command(&cmd
, PROTOCOL_BINARY_CMD_STAT
, NULL
, 0, NULL
, 0);
1203 execute(send_packet(&cmd
));
1207 execute(recv_packet(&rsp
));
1208 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_STAT
,
1209 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1210 } while (rsp
.plain
.message
.header
.response
.keylen
!= 0);
1215 static enum test_return
send_string(const char *cmd
)
1217 execute(retry_write(cmd
, strlen(cmd
)));
1221 static enum test_return
receive_line(char *buffer
, size_t size
)
1224 while (offset
< size
)
1226 execute(retry_read(buffer
+ offset
, 1));
1227 if (buffer
[offset
] == '\n')
1229 if (offset
+ 1 < size
)
1231 buffer
[offset
+ 1]= '\0';
1243 static enum test_return
receive_response(const char *msg
) {
1245 execute(receive_line(buffer
, sizeof(buffer
)));
1246 if (strcmp(msg
, buffer
) != 0) {
1247 fprintf(stderr
, "[%s]\n", buffer
);
1249 verify(strcmp(msg
, buffer
) == 0);
1253 static enum test_return
receive_error_response(void)
1256 execute(receive_line(buffer
, sizeof(buffer
)));
1257 verify(strncmp(buffer
, "ERROR", 5) == 0 ||
1258 strncmp(buffer
, "CLIENT_ERROR", 12) == 0 ||
1259 strncmp(buffer
, "SERVER_ERROR", 12) == 0);
1263 static enum test_return
test_ascii_quit(void)
1265 /* Verify that quit handles unknown options */
1266 execute(send_string("quit foo bar\r\n"));
1267 execute(receive_error_response());
1269 /* quit doesn't support noreply */
1270 execute(send_string("quit noreply\r\n"));
1271 execute(receive_error_response());
1273 /* Verify that quit works */
1274 execute(send_string("quit\r\n"));
1276 /* Socket should be closed now, read should return EXIT_SUCCESS */
1278 verify(timeout_io_op(sock
, POLLIN
, buffer
, sizeof(buffer
)) == 0);
1279 return TEST_PASS_RECONNECT
;
1283 static enum test_return
test_ascii_version(void)
1285 /* Verify that version works */
1286 execute(send_string("version\r\n"));
1288 execute(receive_line(buffer
, sizeof(buffer
)));
1289 verify(strncmp(buffer
, "VERSION ", 8) == 0);
1291 char *version
= &buffer
[sizeof("VERSION") + 2];
1292 if (version
[0] >= '1' || (version
[0] == '1' && version
[2] >= '6')) {
1293 v16x_or_greater
= true;
1296 /* Verify that version command handles unknown options */
1297 execute(send_string("version foo bar\r\n"));
1298 if (v16x_or_greater
) {
1299 execute(receive_line(buffer
, sizeof(buffer
)));
1300 verify(strncmp(buffer
, "VERSION ", 8) == 0);
1302 execute(receive_error_response());
1304 /* version doesn't support noreply */
1305 execute(send_string("version noreply\r\n"));
1306 if (v16x_or_greater
) {
1307 execute(receive_line(buffer
, sizeof(buffer
)));
1308 verify(strncmp(buffer
, "VERSION ", 8) == 0);
1310 execute(receive_error_response());
1315 static enum test_return
test_ascii_verbosity(void)
1317 /* This command does not adhere to the spec! */
1318 execute(send_string("verbosity foo bar my\r\n"));
1319 execute(receive_error_response());
1321 execute(send_string("verbosity noreply\r\n"));
1322 execute(test_ascii_version());
1324 execute(send_string("verbosity 0 noreply\r\n"));
1325 execute(test_ascii_version());
1327 execute(send_string("verbosity\r\n"));
1328 execute(receive_error_response());
1330 execute(send_string("verbosity 1\r\n"));
1331 execute(receive_response("OK\r\n"));
1333 execute(send_string("verbosity 0\r\n"));
1334 execute(receive_response("OK\r\n"));
1341 static enum test_return
test_ascii_set_impl(const char* key
, bool noreply
)
1343 /* @todo add tests for bogus format! */
1345 snprintf(buffer
, sizeof(buffer
), "set %s 0 0 5%s\r\nvalue\r\n", key
, noreply
? " noreply" : "");
1346 execute(send_string(buffer
));
1350 execute(receive_response("STORED\r\n"));
1353 return test_ascii_version();
1356 static enum test_return
test_ascii_set(void)
1358 return test_ascii_set_impl("test_ascii_set", false);
1361 static enum test_return
test_ascii_set_noreply(void)
1363 return test_ascii_set_impl("test_ascii_set_noreply", true);
1366 static enum test_return
test_ascii_add_impl(const char* key
, bool noreply
)
1368 /* @todo add tests for bogus format! */
1370 snprintf(buffer
, sizeof(buffer
), "add %s 0 0 5%s\r\nvalue\r\n", key
, noreply
? " noreply" : "");
1371 execute(send_string(buffer
));
1375 execute(receive_response("STORED\r\n"));
1378 execute(send_string(buffer
));
1382 execute(receive_response("NOT_STORED\r\n"));
1385 return test_ascii_version();
1388 static enum test_return
test_ascii_add(void)
1390 return test_ascii_add_impl("test_ascii_add", false);
1393 static enum test_return
test_ascii_add_noreply(void)
1395 return test_ascii_add_impl("test_ascii_add_noreply", true);
1398 static enum test_return
ascii_get_unknown_value(char **key
, char **value
, ssize_t
*ndata
)
1402 execute(receive_line(buffer
, sizeof(buffer
)));
1403 verify(strncmp(buffer
, "VALUE ", 6) == 0);
1404 char *end
= strchr(buffer
+ 6, ' ');
1405 verify(end
!= NULL
);
1410 *key
= strdup(buffer
+ 6);
1411 verify(*key
!= NULL
);
1415 unsigned long val
= strtoul(ptr
, &end
, 10); /* flags */
1419 verify(end
!= NULL
);
1421 *ndata
= (ssize_t
)strtoul(end
, &end
, 10); /* size */
1424 verify(end
!= NULL
);
1425 while (end
and *end
!= '\n' and isspace(*end
))
1427 verify(end
and *end
== '\n');
1429 *value
= static_cast<char*>(malloc((size_t)*ndata
));
1430 verify(*value
!= NULL
);
1432 execute(retry_read(*value
, (size_t)*ndata
));
1434 execute(retry_read(buffer
, 2));
1435 verify(memcmp(buffer
, "\r\n", 2) == 0);
1440 static enum test_return
ascii_get_value(const char *key
, const char *value
)
1444 size_t datasize
= strlen(value
);
1446 verify(datasize
< sizeof(buffer
));
1447 execute(receive_line(buffer
, sizeof(buffer
)));
1448 verify(strncmp(buffer
, "VALUE ", 6) == 0);
1449 verify(strncmp(buffer
+ 6, key
, strlen(key
)) == 0);
1450 char *ptr
= buffer
+ 6 + strlen(key
) + 1;
1454 unsigned long val
= strtoul(ptr
, &end
, 10); /* flags */
1458 verify(end
!= NULL
);
1461 val
= strtoul(end
, &end
, 10); /* size */
1464 verify(val
== datasize
);
1465 verify(end
!= NULL
);
1466 while (end
and *end
!= '\n' and isspace(*end
))
1470 verify(end
and *end
== '\n');
1472 execute(retry_read(buffer
, datasize
));
1473 verify(memcmp(buffer
, value
, datasize
) == 0);
1475 execute(retry_read(buffer
, 2));
1476 verify(memcmp(buffer
, "\r\n", 2) == 0);
1481 static enum test_return
ascii_get_item(const char *key
, const char *value
,
1488 datasize
= strlen(value
);
1491 verify(datasize
< sizeof(buffer
));
1492 snprintf(buffer
, sizeof(buffer
), "get %s\r\n", key
);
1493 execute(send_string(buffer
));
1497 execute(ascii_get_value(key
, value
));
1500 execute(retry_read(buffer
, 5));
1501 verify(memcmp(buffer
, "END\r\n", 5) == 0);
1506 static enum test_return
ascii_gets_value(const char *key
, const char *value
,
1511 size_t datasize
= strlen(value
);
1513 verify(datasize
< sizeof(buffer
));
1514 execute(receive_line(buffer
, sizeof(buffer
)));
1515 verify(strncmp(buffer
, "VALUE ", 6) == 0);
1516 verify(strncmp(buffer
+ 6, key
, strlen(key
)) == 0);
1517 char *ptr
= buffer
+ 6 + strlen(key
) + 1;
1521 unsigned long val
= strtoul(ptr
, &end
, 10); /* flags */
1525 verify(end
!= NULL
);
1528 val
= strtoul(end
, &end
, 10); /* size */
1531 verify(val
== datasize
);
1532 verify(end
!= NULL
);
1535 *cas
= strtoul(end
, &end
, 10); /* cas */
1538 verify(val
== datasize
);
1539 verify(end
!= NULL
);
1541 while (end
and *end
!= '\n' and isspace(*end
))
1545 verify(end
and *end
== '\n');
1547 execute(retry_read(buffer
, datasize
));
1548 verify(memcmp(buffer
, value
, datasize
) == 0);
1550 execute(retry_read(buffer
, 2));
1551 verify(memcmp(buffer
, "\r\n", 2) == 0);
1556 static enum test_return
ascii_gets_item(const char *key
, const char *value
,
1557 bool exist
, unsigned long *cas
)
1563 datasize
= strlen(value
);
1566 verify(datasize
< sizeof(buffer
));
1567 snprintf(buffer
, sizeof(buffer
), "gets %s\r\n", key
);
1568 execute(send_string(buffer
));
1571 execute(ascii_gets_value(key
, value
, cas
));
1573 execute(retry_read(buffer
, 5));
1574 verify(memcmp(buffer
, "END\r\n", 5) == 0);
1579 static enum test_return
ascii_set_item(const char *key
, const char *value
)
1582 size_t len
= strlen(value
);
1583 snprintf(buffer
, sizeof(buffer
), "set %s 0 0 %u\r\n", key
, (unsigned int)len
);
1584 execute(send_string(buffer
));
1585 execute(retry_write(value
, len
));
1586 execute(send_string("\r\n"));
1587 execute(receive_response("STORED\r\n"));
1591 static enum test_return
test_ascii_replace_impl(const char* key
, bool noreply
)
1594 snprintf(buffer
, sizeof(buffer
), "replace %s 0 0 5%s\r\nvalue\r\n", key
, noreply
? " noreply" : "");
1595 execute(send_string(buffer
));
1599 execute(test_ascii_version());
1603 execute(receive_response("NOT_STORED\r\n"));
1606 execute(ascii_set_item(key
, "value"));
1607 execute(ascii_get_item(key
, "value", true));
1610 execute(send_string(buffer
));
1613 execute(test_ascii_version());
1615 execute(receive_response("STORED\r\n"));
1617 return test_ascii_version();
1620 static enum test_return
test_ascii_replace(void)
1622 return test_ascii_replace_impl("test_ascii_replace", false);
1625 static enum test_return
test_ascii_replace_noreply(void)
1627 return test_ascii_replace_impl("test_ascii_replace_noreply", true);
1630 static enum test_return
test_ascii_cas_impl(const char* key
, bool noreply
)
1635 execute(ascii_set_item(key
, "value"));
1636 execute(ascii_gets_item(key
, "value", true, &cas
));
1638 snprintf(buffer
, sizeof(buffer
), "cas %s 0 0 6 %lu%s\r\nvalue2\r\n", key
, cas
, noreply
? " noreply" : "");
1639 execute(send_string(buffer
));
1643 execute(test_ascii_version());
1647 execute(receive_response("STORED\r\n"));
1650 /* reexecute the same command should fail due to illegal cas */
1651 execute(send_string(buffer
));
1655 execute(test_ascii_version());
1659 execute(receive_response("EXISTS\r\n"));
1662 return test_ascii_version();
1665 static enum test_return
test_ascii_cas(void)
1667 return test_ascii_cas_impl("test_ascii_cas", false);
1670 static enum test_return
test_ascii_cas_noreply(void)
1672 return test_ascii_cas_impl("test_ascii_cas_noreply", true);
1675 static enum test_return
test_ascii_delete_impl(const char *key
, bool noreply
)
1677 execute(ascii_set_item(key
, "value"));
1679 execute(send_string("delete\r\n"));
1680 execute(receive_error_response());
1681 /* BUG: the server accepts delete a b */
1682 execute(send_string("delete a b c d e\r\n"));
1683 execute(receive_error_response());
1686 snprintf(buffer
, sizeof(buffer
), "delete %s%s\r\n", key
, noreply
? " noreply" : "");
1687 execute(send_string(buffer
));
1690 execute(test_ascii_version());
1692 execute(receive_response("DELETED\r\n"));
1694 execute(ascii_get_item(key
, "value", false));
1695 execute(send_string(buffer
));
1697 execute(test_ascii_version());
1699 execute(receive_response("NOT_FOUND\r\n"));
1704 static enum test_return
test_ascii_delete(void)
1706 return test_ascii_delete_impl("test_ascii_delete", false);
1709 static enum test_return
test_ascii_delete_noreply(void)
1711 return test_ascii_delete_impl("test_ascii_delete_noreply", true);
1714 static enum test_return
test_ascii_get(void)
1716 execute(ascii_set_item("test_ascii_get", "value"));
1718 execute(send_string("get\r\n"));
1719 execute(receive_error_response());
1720 execute(ascii_get_item("test_ascii_get", "value", true));
1721 execute(ascii_get_item("test_ascii_get_notfound", "value", false));
1726 static enum test_return
test_ascii_gets(void)
1728 execute(ascii_set_item("test_ascii_gets", "value"));
1730 execute(send_string("gets\r\n"));
1731 execute(receive_error_response());
1733 execute(ascii_gets_item("test_ascii_gets", "value", true, &cas
));
1734 execute(ascii_gets_item("test_ascii_gets_notfound", "value", false, &cas
));
1739 static enum test_return
test_ascii_mget(void)
1741 const uint32_t nkeys
= 5;
1742 const char * const keys
[]= {
1745 /* test_ascii_mget_3 does not exist :) */
1751 for (uint32_t x
= 0; x
< nkeys
; ++x
)
1753 execute(ascii_set_item(keys
[x
], "value"));
1756 /* Ask for a key that doesn't exist as well */
1757 execute(send_string("get test_ascii_mget1 test_ascii_mget2 test_ascii_mget3 "
1758 "test_ascii_mget4 test_ascii_mget5 "
1759 "test_ascii_mget6\r\n"));
1761 std::vector
<char *> returned
;
1762 returned
.resize(nkeys
);
1764 for (uint32_t x
= 0; x
< nkeys
; ++x
)
1768 execute(ascii_get_unknown_value(&returned
[x
], &v
, &nbytes
));
1769 verify(nbytes
== 5);
1770 verify(memcmp(v
, "value", 5) == 0);
1775 execute(retry_read(buffer
, 5));
1776 verify(memcmp(buffer
, "END\r\n", 5) == 0);
1778 /* verify that we got all the keys we expected */
1779 for (uint32_t x
= 0; x
< nkeys
; ++x
)
1782 for (uint32_t y
= 0; y
< nkeys
; ++y
)
1784 if (strcmp(keys
[x
], returned
[y
]) == 0)
1793 for (uint32_t x
= 0; x
< nkeys
; ++x
)
1801 static enum test_return
test_ascii_incr_impl(const char* key
, bool noreply
)
1804 snprintf(cmd
, sizeof(cmd
), "incr %s 1%s\r\n", key
, noreply
? " noreply" : "");
1806 execute(ascii_set_item(key
, "0"));
1807 for (int x
= 1; x
< 11; ++x
)
1809 execute(send_string(cmd
));
1812 execute(test_ascii_version());
1816 execute(receive_line(buffer
, sizeof(buffer
)));
1817 int val
= atoi(buffer
);
1822 execute(ascii_get_item(key
, "10", true));
1827 static enum test_return
test_ascii_incr(void)
1829 return test_ascii_incr_impl("test_ascii_incr", false);
1832 static enum test_return
test_ascii_incr_noreply(void)
1834 return test_ascii_incr_impl("test_ascii_incr_noreply", true);
1837 static enum test_return
test_ascii_decr_impl(const char* key
, bool noreply
)
1840 snprintf(cmd
, sizeof(cmd
), "decr %s 1%s\r\n", key
, noreply
? " noreply" : "");
1842 execute(ascii_set_item(key
, "9"));
1843 for (int x
= 8; x
> -1; --x
)
1845 execute(send_string(cmd
));
1849 execute(test_ascii_version());
1854 execute(receive_line(buffer
, sizeof(buffer
)));
1855 int val
= atoi(buffer
);
1860 execute(ascii_get_item(key
, "0", true));
1862 /* verify that it doesn't wrap */
1863 execute(send_string(cmd
));
1866 execute(test_ascii_version());
1871 execute(receive_line(buffer
, sizeof(buffer
)));
1873 execute(ascii_get_item(key
, "0", true));
1878 static enum test_return
test_ascii_decr(void)
1880 return test_ascii_decr_impl("test_ascii_decr", false);
1883 static enum test_return
test_ascii_decr_noreply(void)
1885 return test_ascii_decr_impl("test_ascii_decr_noreply", true);
1889 static enum test_return
test_ascii_flush_impl(const char *key
, bool noreply
)
1892 /* Verify that the flush_all command handles unknown options */
1893 /* Bug in the current memcached server! */
1894 execute(send_string("flush_all foo bar\r\n"));
1895 execute(receive_error_response());
1898 execute(ascii_set_item(key
, key
));
1899 execute(ascii_get_item(key
, key
, true));
1903 execute(send_string("flush_all noreply\r\n"));
1904 execute(test_ascii_version());
1908 execute(send_string("flush_all\r\n"));
1909 execute(receive_response("OK\r\n"));
1912 execute(ascii_get_item(key
, key
, false));
1917 static enum test_return
test_ascii_flush(void)
1919 return test_ascii_flush_impl("test_ascii_flush", false);
1922 static enum test_return
test_ascii_flush_noreply(void)
1924 return test_ascii_flush_impl("test_ascii_flush_noreply", true);
1927 static enum test_return
test_ascii_concat_impl(const char *key
,
1938 execute(ascii_set_item(key
, value
));
1950 snprintf(cmd
, sizeof(cmd
), "%s %s 0 0 %u%s\r\n%s\r\n",
1951 append
? "append" : "prepend",
1952 key
, (unsigned int)strlen(value
), noreply
? " noreply" : "",
1954 execute(send_string(cmd
));
1958 execute(test_ascii_version());
1962 execute(receive_response("STORED\r\n"));
1965 execute(ascii_get_item(key
, "hello world", true));
1967 snprintf(cmd
, sizeof(cmd
), "%s %s_notfound 0 0 %u%s\r\n%s\r\n",
1968 append
? "append" : "prepend",
1969 key
, (unsigned int)strlen(value
), noreply
? " noreply" : "",
1971 execute(send_string(cmd
));
1975 execute(test_ascii_version());
1979 execute(receive_response("NOT_STORED\r\n"));
1985 static enum test_return
test_ascii_append(void)
1987 return test_ascii_concat_impl("test_ascii_append", true, false);
1990 static enum test_return
test_ascii_prepend(void)
1992 return test_ascii_concat_impl("test_ascii_prepend", false, false);
1995 static enum test_return
test_ascii_append_noreply(void)
1997 return test_ascii_concat_impl("test_ascii_append_noreply", true, true);
2000 static enum test_return
test_ascii_prepend_noreply(void)
2002 return test_ascii_concat_impl("test_ascii_prepend_noreply", false, true);
2005 static enum test_return
test_ascii_stat(void)
2007 execute(send_string("stats noreply\r\n"));
2008 execute(receive_error_response());
2009 execute(send_string("stats\r\n"));
2012 execute(receive_line(buffer
, sizeof(buffer
)));
2013 } while (strcmp(buffer
, "END\r\n") != 0);
2015 return TEST_PASS_RECONNECT
;
2018 typedef enum test_return(*TEST_FUNC
)(void);
2022 const char *description
;
2026 struct testcase testcases
[]= {
2027 { "ascii quit", test_ascii_quit
},
2028 { "ascii version", test_ascii_version
},
2029 { "ascii verbosity", test_ascii_verbosity
},
2030 { "ascii set", test_ascii_set
},
2031 { "ascii set noreply", test_ascii_set_noreply
},
2032 { "ascii get", test_ascii_get
},
2033 { "ascii gets", test_ascii_gets
},
2034 { "ascii mget", test_ascii_mget
},
2035 { "ascii flush", test_ascii_flush
},
2036 { "ascii flush noreply", test_ascii_flush_noreply
},
2037 { "ascii add", test_ascii_add
},
2038 { "ascii add noreply", test_ascii_add_noreply
},
2039 { "ascii replace", test_ascii_replace
},
2040 { "ascii replace noreply", test_ascii_replace_noreply
},
2041 { "ascii cas", test_ascii_cas
},
2042 { "ascii cas noreply", test_ascii_cas_noreply
},
2043 { "ascii delete", test_ascii_delete
},
2044 { "ascii delete noreply", test_ascii_delete_noreply
},
2045 { "ascii incr", test_ascii_incr
},
2046 { "ascii incr noreply", test_ascii_incr_noreply
},
2047 { "ascii decr", test_ascii_decr
},
2048 { "ascii decr noreply", test_ascii_decr_noreply
},
2049 { "ascii append", test_ascii_append
},
2050 { "ascii append noreply", test_ascii_append_noreply
},
2051 { "ascii prepend", test_ascii_prepend
},
2052 { "ascii prepend noreply", test_ascii_prepend_noreply
},
2053 { "ascii stat", test_ascii_stat
},
2054 { "binary noop", test_binary_noop
},
2055 { "binary quit", test_binary_quit
},
2056 { "binary quitq", test_binary_quitq
},
2057 { "binary set", test_binary_set
},
2058 { "binary setq", test_binary_setq
},
2059 { "binary flush", test_binary_flush
},
2060 { "binary flushq", test_binary_flushq
},
2061 { "binary add", test_binary_add
},
2062 { "binary addq", test_binary_addq
},
2063 { "binary replace", test_binary_replace
},
2064 { "binary replaceq", test_binary_replaceq
},
2065 { "binary delete", test_binary_delete
},
2066 { "binary deleteq", test_binary_deleteq
},
2067 { "binary get", test_binary_get
},
2068 { "binary getq", test_binary_getq
},
2069 { "binary getk", test_binary_getk
},
2070 { "binary getkq", test_binary_getkq
},
2071 { "binary incr", test_binary_incr
},
2072 { "binary incrq", test_binary_incrq
},
2073 { "binary decr", test_binary_decr
},
2074 { "binary decrq", test_binary_decrq
},
2075 { "binary version", test_binary_version
},
2076 { "binary append", test_binary_append
},
2077 { "binary appendq", test_binary_appendq
},
2078 { "binary prepend", test_binary_prepend
},
2079 { "binary prependq", test_binary_prependq
},
2080 { "binary stat", test_binary_stat
},
2084 const int ascii_tests
= 1;
2085 const int binary_tests
= 2;
2093 int main(int argc
, char **argv
)
2095 static const char * const status_msg
[]= {"[skip]", "[pass]", "[pass]", "[FAIL]"};
2096 struct test_type_st tests
= { true, true };
2099 const char *hostname
= NULL
;
2100 const char *port
= MEMCACHED_DEFAULT_PORT_STRING
;
2103 const char *testname
= NULL
;
2107 while ((cmd
= getopt(argc
, argv
, "qt:vch:p:PT:?ab")) != EOF
)
2112 tests
.binary
= false;
2121 timeout
= atoi(optarg
);
2124 fprintf(stderr
, "Invalid timeout. Please specify a number for -t\n");
2125 return EXIT_FAILURE
;
2129 case 'v': verbose
= true;
2132 case 'c': do_core
= true;
2135 case 'h': hostname
= optarg
;
2138 case 'p': port
= optarg
;
2145 case 'P': prompt
= true;
2148 case 'T': testname
= optarg
;
2152 fprintf(stderr
, "Usage: %s [-h hostname] [-p port] [-c] [-v] [-t n] [-P] [-T testname]'\n"
2153 "\t-c\tGenerate coredump if a test fails\n"
2154 "\t-v\tVerbose test output (print out the assertion)\n"
2155 "\t-t n\tSet the timeout for io-operations to n seconds\n"
2156 "\t-P\tPrompt the user before starting a test.\n"
2157 "\t\t\t\"skip\" will skip the test\n"
2158 "\t\t\t\"quit\" will terminate memcapable\n"
2159 "\t\t\tEverything else will start the test\n"
2160 "\t-T n\tJust run the test named n\n"
2161 "\t-a\tOnly test the ascii protocol\n"
2162 "\t-b\tOnly test the binary protocol\n",
2164 return EXIT_SUCCESS
;
2170 fprintf(stderr
, "No hostname was provided.\n");
2171 return EXIT_FAILURE
;
2174 initialize_sockets();
2175 sock
= connect_server(hostname
, port
);
2176 if (sock
== INVALID_SOCKET
)
2178 fprintf(stderr
, "Failed to connect to <%s:%s>: %s\n",
2179 hostname
?:"(null)", port
?:"(null)", strerror(get_socket_errno()));
2180 return EXIT_FAILURE
;
2183 for (int ii
= 0; testcases
[ii
].description
!= NULL
; ++ii
)
2185 if (testname
!= NULL
&& strcmp(testcases
[ii
].description
, testname
) != 0)
2190 if ((testcases
[ii
].description
[0] == 'a' && (tests
.ascii
) == 0) ||
2191 (testcases
[ii
].description
[0] == 'b' && (tests
.binary
) == 0))
2196 fprintf(stdout
, "%-40s", testcases
[ii
].description
);
2201 fprintf(stdout
, "\nPress <return> when you are ready? ");
2202 char buffer
[80] = {0};
2203 if (fgets(buffer
, sizeof(buffer
), stdin
) != NULL
) {
2204 if (strncmp(buffer
, "skip", 4) == 0)
2206 fprintf(stdout
, "%-40s%s\n", testcases
[ii
].description
,
2207 status_msg
[TEST_SKIP
]);
2211 if (strncmp(buffer
, "quit", 4) == 0)
2217 fprintf(stdout
, "%-40s", testcases
[ii
].description
);
2221 bool reconnect
= false;
2222 enum test_return ret
= testcases
[ii
].function();
2223 if (ret
== TEST_FAIL
)
2229 fprintf(stderr
, "\n");
2232 else if (ret
== TEST_PASS_RECONNECT
)
2237 if (ret
== TEST_FAIL
)
2239 fprintf(stderr
, "%s\n", status_msg
[ret
]);
2243 fprintf(stdout
, "%s\n", status_msg
[ret
]);
2249 if ((sock
= connect_server(hostname
, port
)) == INVALID_SOCKET
)
2251 fprintf(stderr
, "Failed to connect to <%s:%s>: %s\n", hostname
?:"(null)", port
?:"(null)", strerror(get_socket_errno()));
2252 fprintf(stderr
, "%d of %d tests failed\n", failed
, total
);
2253 return EXIT_FAILURE
;
2261 fprintf(stdout
, "All tests passed\n");
2265 fprintf(stderr
, "%d of %d tests failed\n", failed
, total
);
2268 return (failed
== 0) ? EXIT_SUCCESS
: EXIT_FAILURE
;