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 if (!v16x_or_greater
) {
1266 /* Verify that quit handles unknown options */
1267 execute(send_string("quit foo bar\r\n"));
1268 execute(receive_error_response());
1270 /* quit doesn't support noreply */
1271 execute(send_string("quit noreply\r\n"));
1272 execute(receive_error_response());
1275 /* Verify that quit works */
1276 execute(send_string("quit\r\n"));
1278 /* Socket should be closed now, read should return EXIT_SUCCESS */
1280 verify(timeout_io_op(sock
, POLLIN
, buffer
, sizeof(buffer
)) == 0);
1281 return TEST_PASS_RECONNECT
;
1285 static enum test_return
test_ascii_version(void)
1287 /* Verify that version works */
1288 execute(send_string("version\r\n"));
1290 execute(receive_line(buffer
, sizeof(buffer
)));
1291 verify(strncmp(buffer
, "VERSION ", 8) == 0);
1293 char *version
= &buffer
[sizeof("VERSION") + 2];
1294 if (version
[0] >= '1' || (version
[0] == '1' && version
[2] >= '6')) {
1295 v16x_or_greater
= true;
1298 /* Verify that version command handles unknown options */
1299 execute(send_string("version foo bar\r\n"));
1300 if (v16x_or_greater
) {
1301 execute(receive_line(buffer
, sizeof(buffer
)));
1302 verify(strncmp(buffer
, "VERSION ", 8) == 0);
1304 execute(receive_error_response());
1306 /* version doesn't support noreply */
1307 execute(send_string("version noreply\r\n"));
1308 if (v16x_or_greater
) {
1309 execute(receive_line(buffer
, sizeof(buffer
)));
1310 verify(strncmp(buffer
, "VERSION ", 8) == 0);
1312 execute(receive_error_response());
1317 static enum test_return
test_ascii_verbosity(void)
1319 /* This command does not adhere to the spec! */
1320 execute(send_string("verbosity foo bar my\r\n"));
1321 execute(receive_error_response());
1323 execute(send_string("verbosity noreply\r\n"));
1324 execute(test_ascii_version());
1326 execute(send_string("verbosity 0 noreply\r\n"));
1327 execute(test_ascii_version());
1329 execute(send_string("verbosity\r\n"));
1330 execute(receive_error_response());
1332 execute(send_string("verbosity 1\r\n"));
1333 execute(receive_response("OK\r\n"));
1335 execute(send_string("verbosity 0\r\n"));
1336 execute(receive_response("OK\r\n"));
1343 static enum test_return
test_ascii_set_impl(const char* key
, bool noreply
)
1345 /* @todo add tests for bogus format! */
1347 snprintf(buffer
, sizeof(buffer
), "set %s 0 0 5%s\r\nvalue\r\n", key
, noreply
? " noreply" : "");
1348 execute(send_string(buffer
));
1352 execute(receive_response("STORED\r\n"));
1355 return test_ascii_version();
1358 static enum test_return
test_ascii_set(void)
1360 return test_ascii_set_impl("test_ascii_set", false);
1363 static enum test_return
test_ascii_set_noreply(void)
1365 return test_ascii_set_impl("test_ascii_set_noreply", true);
1368 static enum test_return
test_ascii_add_impl(const char* key
, bool noreply
)
1370 /* @todo add tests for bogus format! */
1372 snprintf(buffer
, sizeof(buffer
), "add %s 0 0 5%s\r\nvalue\r\n", key
, noreply
? " noreply" : "");
1373 execute(send_string(buffer
));
1377 execute(receive_response("STORED\r\n"));
1380 execute(send_string(buffer
));
1384 execute(receive_response("NOT_STORED\r\n"));
1387 return test_ascii_version();
1390 static enum test_return
test_ascii_add(void)
1392 return test_ascii_add_impl("test_ascii_add", false);
1395 static enum test_return
test_ascii_add_noreply(void)
1397 return test_ascii_add_impl("test_ascii_add_noreply", true);
1400 static enum test_return
ascii_get_unknown_value(char **key
, char **value
, ssize_t
*ndata
)
1404 execute(receive_line(buffer
, sizeof(buffer
)));
1405 verify(strncmp(buffer
, "VALUE ", 6) == 0);
1406 char *end
= strchr(buffer
+ 6, ' ');
1407 verify(end
!= NULL
);
1412 *key
= strdup(buffer
+ 6);
1413 verify(*key
!= NULL
);
1417 unsigned long val
= strtoul(ptr
, &end
, 10); /* flags */
1421 verify(end
!= NULL
);
1423 *ndata
= (ssize_t
)strtoul(end
, &end
, 10); /* size */
1426 verify(end
!= NULL
);
1427 while (end
and *end
!= '\n' and isspace(*end
))
1429 verify(end
and *end
== '\n');
1431 *value
= static_cast<char*>(malloc((size_t)*ndata
));
1432 verify(*value
!= NULL
);
1434 execute(retry_read(*value
, (size_t)*ndata
));
1436 execute(retry_read(buffer
, 2));
1437 verify(memcmp(buffer
, "\r\n", 2) == 0);
1442 static enum test_return
ascii_get_value(const char *key
, const char *value
)
1446 size_t datasize
= strlen(value
);
1448 verify(datasize
< sizeof(buffer
));
1449 execute(receive_line(buffer
, sizeof(buffer
)));
1450 verify(strncmp(buffer
, "VALUE ", 6) == 0);
1451 verify(strncmp(buffer
+ 6, key
, strlen(key
)) == 0);
1452 char *ptr
= buffer
+ 6 + strlen(key
) + 1;
1456 unsigned long val
= strtoul(ptr
, &end
, 10); /* flags */
1460 verify(end
!= NULL
);
1463 val
= strtoul(end
, &end
, 10); /* size */
1466 verify(val
== datasize
);
1467 verify(end
!= NULL
);
1468 while (end
and *end
!= '\n' and isspace(*end
))
1472 verify(end
and *end
== '\n');
1474 execute(retry_read(buffer
, datasize
));
1475 verify(memcmp(buffer
, value
, datasize
) == 0);
1477 execute(retry_read(buffer
, 2));
1478 verify(memcmp(buffer
, "\r\n", 2) == 0);
1483 static enum test_return
ascii_get_item(const char *key
, const char *value
,
1490 datasize
= strlen(value
);
1493 verify(datasize
< sizeof(buffer
));
1494 snprintf(buffer
, sizeof(buffer
), "get %s\r\n", key
);
1495 execute(send_string(buffer
));
1499 execute(ascii_get_value(key
, value
));
1502 execute(retry_read(buffer
, 5));
1503 verify(memcmp(buffer
, "END\r\n", 5) == 0);
1508 static enum test_return
ascii_gets_value(const char *key
, const char *value
,
1513 size_t datasize
= strlen(value
);
1515 verify(datasize
< sizeof(buffer
));
1516 execute(receive_line(buffer
, sizeof(buffer
)));
1517 verify(strncmp(buffer
, "VALUE ", 6) == 0);
1518 verify(strncmp(buffer
+ 6, key
, strlen(key
)) == 0);
1519 char *ptr
= buffer
+ 6 + strlen(key
) + 1;
1523 unsigned long val
= strtoul(ptr
, &end
, 10); /* flags */
1527 verify(end
!= NULL
);
1530 val
= strtoul(end
, &end
, 10); /* size */
1533 verify(val
== datasize
);
1534 verify(end
!= NULL
);
1537 *cas
= strtoul(end
, &end
, 10); /* cas */
1540 verify(val
== datasize
);
1541 verify(end
!= NULL
);
1543 while (end
and *end
!= '\n' and isspace(*end
))
1547 verify(end
and *end
== '\n');
1549 execute(retry_read(buffer
, datasize
));
1550 verify(memcmp(buffer
, value
, datasize
) == 0);
1552 execute(retry_read(buffer
, 2));
1553 verify(memcmp(buffer
, "\r\n", 2) == 0);
1558 static enum test_return
ascii_gets_item(const char *key
, const char *value
,
1559 bool exist
, unsigned long *cas
)
1565 datasize
= strlen(value
);
1568 verify(datasize
< sizeof(buffer
));
1569 snprintf(buffer
, sizeof(buffer
), "gets %s\r\n", key
);
1570 execute(send_string(buffer
));
1573 execute(ascii_gets_value(key
, value
, cas
));
1575 execute(retry_read(buffer
, 5));
1576 verify(memcmp(buffer
, "END\r\n", 5) == 0);
1581 static enum test_return
ascii_set_item(const char *key
, const char *value
)
1584 size_t len
= strlen(value
);
1585 snprintf(buffer
, sizeof(buffer
), "set %s 0 0 %u\r\n", key
, (unsigned int)len
);
1586 execute(send_string(buffer
));
1587 execute(retry_write(value
, len
));
1588 execute(send_string("\r\n"));
1589 execute(receive_response("STORED\r\n"));
1593 static enum test_return
test_ascii_replace_impl(const char* key
, bool noreply
)
1596 snprintf(buffer
, sizeof(buffer
), "replace %s 0 0 5%s\r\nvalue\r\n", key
, noreply
? " noreply" : "");
1597 execute(send_string(buffer
));
1601 execute(test_ascii_version());
1605 execute(receive_response("NOT_STORED\r\n"));
1608 execute(ascii_set_item(key
, "value"));
1609 execute(ascii_get_item(key
, "value", true));
1612 execute(send_string(buffer
));
1615 execute(test_ascii_version());
1617 execute(receive_response("STORED\r\n"));
1619 return test_ascii_version();
1622 static enum test_return
test_ascii_replace(void)
1624 return test_ascii_replace_impl("test_ascii_replace", false);
1627 static enum test_return
test_ascii_replace_noreply(void)
1629 return test_ascii_replace_impl("test_ascii_replace_noreply", true);
1632 static enum test_return
test_ascii_cas_impl(const char* key
, bool noreply
)
1637 execute(ascii_set_item(key
, "value"));
1638 execute(ascii_gets_item(key
, "value", true, &cas
));
1640 snprintf(buffer
, sizeof(buffer
), "cas %s 0 0 6 %lu%s\r\nvalue2\r\n", key
, cas
, noreply
? " noreply" : "");
1641 execute(send_string(buffer
));
1645 execute(test_ascii_version());
1649 execute(receive_response("STORED\r\n"));
1652 /* reexecute the same command should fail due to illegal cas */
1653 execute(send_string(buffer
));
1657 execute(test_ascii_version());
1661 execute(receive_response("EXISTS\r\n"));
1664 return test_ascii_version();
1667 static enum test_return
test_ascii_cas(void)
1669 return test_ascii_cas_impl("test_ascii_cas", false);
1672 static enum test_return
test_ascii_cas_noreply(void)
1674 return test_ascii_cas_impl("test_ascii_cas_noreply", true);
1677 static enum test_return
test_ascii_delete_impl(const char *key
, bool noreply
)
1679 execute(ascii_set_item(key
, "value"));
1681 execute(send_string("delete\r\n"));
1682 execute(receive_error_response());
1683 /* BUG: the server accepts delete a b */
1684 execute(send_string("delete a b c d e\r\n"));
1685 execute(receive_error_response());
1688 snprintf(buffer
, sizeof(buffer
), "delete %s%s\r\n", key
, noreply
? " noreply" : "");
1689 execute(send_string(buffer
));
1692 execute(test_ascii_version());
1694 execute(receive_response("DELETED\r\n"));
1696 execute(ascii_get_item(key
, "value", false));
1697 execute(send_string(buffer
));
1699 execute(test_ascii_version());
1701 execute(receive_response("NOT_FOUND\r\n"));
1706 static enum test_return
test_ascii_delete(void)
1708 return test_ascii_delete_impl("test_ascii_delete", false);
1711 static enum test_return
test_ascii_delete_noreply(void)
1713 return test_ascii_delete_impl("test_ascii_delete_noreply", true);
1716 static enum test_return
test_ascii_get(void)
1718 execute(ascii_set_item("test_ascii_get", "value"));
1720 execute(send_string("get\r\n"));
1721 execute(receive_error_response());
1722 execute(ascii_get_item("test_ascii_get", "value", true));
1723 execute(ascii_get_item("test_ascii_get_notfound", "value", false));
1728 static enum test_return
test_ascii_gets(void)
1730 execute(ascii_set_item("test_ascii_gets", "value"));
1732 execute(send_string("gets\r\n"));
1733 execute(receive_error_response());
1735 execute(ascii_gets_item("test_ascii_gets", "value", true, &cas
));
1736 execute(ascii_gets_item("test_ascii_gets_notfound", "value", false, &cas
));
1741 static enum test_return
test_ascii_mget(void)
1743 const uint32_t nkeys
= 5;
1744 const char * const keys
[]= {
1747 /* test_ascii_mget_3 does not exist :) */
1753 for (uint32_t x
= 0; x
< nkeys
; ++x
)
1755 execute(ascii_set_item(keys
[x
], "value"));
1758 /* Ask for a key that doesn't exist as well */
1759 execute(send_string("get test_ascii_mget1 test_ascii_mget2 test_ascii_mget3 "
1760 "test_ascii_mget4 test_ascii_mget5 "
1761 "test_ascii_mget6\r\n"));
1763 std::vector
<char *> returned
;
1764 returned
.resize(nkeys
);
1766 for (uint32_t x
= 0; x
< nkeys
; ++x
)
1770 execute(ascii_get_unknown_value(&returned
[x
], &v
, &nbytes
));
1771 verify(nbytes
== 5);
1772 verify(memcmp(v
, "value", 5) == 0);
1777 execute(retry_read(buffer
, 5));
1778 verify(memcmp(buffer
, "END\r\n", 5) == 0);
1780 /* verify that we got all the keys we expected */
1781 for (uint32_t x
= 0; x
< nkeys
; ++x
)
1784 for (uint32_t y
= 0; y
< nkeys
; ++y
)
1786 if (strcmp(keys
[x
], returned
[y
]) == 0)
1795 for (uint32_t x
= 0; x
< nkeys
; ++x
)
1803 static enum test_return
test_ascii_incr_impl(const char* key
, bool noreply
)
1806 snprintf(cmd
, sizeof(cmd
), "incr %s 1%s\r\n", key
, noreply
? " noreply" : "");
1808 execute(ascii_set_item(key
, "0"));
1809 for (int x
= 1; x
< 11; ++x
)
1811 execute(send_string(cmd
));
1814 execute(test_ascii_version());
1818 execute(receive_line(buffer
, sizeof(buffer
)));
1819 int val
= atoi(buffer
);
1824 execute(ascii_get_item(key
, "10", true));
1829 static enum test_return
test_ascii_incr(void)
1831 return test_ascii_incr_impl("test_ascii_incr", false);
1834 static enum test_return
test_ascii_incr_noreply(void)
1836 return test_ascii_incr_impl("test_ascii_incr_noreply", true);
1839 static enum test_return
test_ascii_decr_impl(const char* key
, bool noreply
)
1842 snprintf(cmd
, sizeof(cmd
), "decr %s 1%s\r\n", key
, noreply
? " noreply" : "");
1844 execute(ascii_set_item(key
, "9"));
1845 for (int x
= 8; x
> -1; --x
)
1847 execute(send_string(cmd
));
1851 execute(test_ascii_version());
1856 execute(receive_line(buffer
, sizeof(buffer
)));
1857 int val
= atoi(buffer
);
1862 execute(ascii_get_item(key
, "0", true));
1864 /* verify that it doesn't wrap */
1865 execute(send_string(cmd
));
1868 execute(test_ascii_version());
1873 execute(receive_line(buffer
, sizeof(buffer
)));
1875 execute(ascii_get_item(key
, "0", true));
1880 static enum test_return
test_ascii_decr(void)
1882 return test_ascii_decr_impl("test_ascii_decr", false);
1885 static enum test_return
test_ascii_decr_noreply(void)
1887 return test_ascii_decr_impl("test_ascii_decr_noreply", true);
1891 static enum test_return
test_ascii_flush_impl(const char *key
, bool noreply
)
1894 /* Verify that the flush_all command handles unknown options */
1895 /* Bug in the current memcached server! */
1896 execute(send_string("flush_all foo bar\r\n"));
1897 execute(receive_error_response());
1900 execute(ascii_set_item(key
, key
));
1901 execute(ascii_get_item(key
, key
, true));
1905 execute(send_string("flush_all noreply\r\n"));
1906 execute(test_ascii_version());
1910 execute(send_string("flush_all\r\n"));
1911 execute(receive_response("OK\r\n"));
1914 execute(ascii_get_item(key
, key
, false));
1919 static enum test_return
test_ascii_flush(void)
1921 return test_ascii_flush_impl("test_ascii_flush", false);
1924 static enum test_return
test_ascii_flush_noreply(void)
1926 return test_ascii_flush_impl("test_ascii_flush_noreply", true);
1929 static enum test_return
test_ascii_concat_impl(const char *key
,
1940 execute(ascii_set_item(key
, value
));
1952 snprintf(cmd
, sizeof(cmd
), "%s %s 0 0 %u%s\r\n%s\r\n",
1953 append
? "append" : "prepend",
1954 key
, (unsigned int)strlen(value
), noreply
? " noreply" : "",
1956 execute(send_string(cmd
));
1960 execute(test_ascii_version());
1964 execute(receive_response("STORED\r\n"));
1967 execute(ascii_get_item(key
, "hello world", true));
1969 snprintf(cmd
, sizeof(cmd
), "%s %s_notfound 0 0 %u%s\r\n%s\r\n",
1970 append
? "append" : "prepend",
1971 key
, (unsigned int)strlen(value
), noreply
? " noreply" : "",
1973 execute(send_string(cmd
));
1977 execute(test_ascii_version());
1981 execute(receive_response("NOT_STORED\r\n"));
1987 static enum test_return
test_ascii_append(void)
1989 return test_ascii_concat_impl("test_ascii_append", true, false);
1992 static enum test_return
test_ascii_prepend(void)
1994 return test_ascii_concat_impl("test_ascii_prepend", false, false);
1997 static enum test_return
test_ascii_append_noreply(void)
1999 return test_ascii_concat_impl("test_ascii_append_noreply", true, true);
2002 static enum test_return
test_ascii_prepend_noreply(void)
2004 return test_ascii_concat_impl("test_ascii_prepend_noreply", false, true);
2007 static enum test_return
test_ascii_stat(void)
2009 execute(send_string("stats noreply\r\n"));
2010 execute(receive_error_response());
2011 execute(send_string("stats\r\n"));
2014 execute(receive_line(buffer
, sizeof(buffer
)));
2015 } while (strcmp(buffer
, "END\r\n") != 0);
2017 return TEST_PASS_RECONNECT
;
2020 typedef enum test_return(*TEST_FUNC
)(void);
2024 const char *description
;
2028 struct testcase testcases
[]= {
2029 { "ascii version", test_ascii_version
},
2030 { "ascii quit", test_ascii_quit
},
2031 { "ascii verbosity", test_ascii_verbosity
},
2032 { "ascii set", test_ascii_set
},
2033 { "ascii set noreply", test_ascii_set_noreply
},
2034 { "ascii get", test_ascii_get
},
2035 { "ascii gets", test_ascii_gets
},
2036 { "ascii mget", test_ascii_mget
},
2037 { "ascii flush", test_ascii_flush
},
2038 { "ascii flush noreply", test_ascii_flush_noreply
},
2039 { "ascii add", test_ascii_add
},
2040 { "ascii add noreply", test_ascii_add_noreply
},
2041 { "ascii replace", test_ascii_replace
},
2042 { "ascii replace noreply", test_ascii_replace_noreply
},
2043 { "ascii cas", test_ascii_cas
},
2044 { "ascii cas noreply", test_ascii_cas_noreply
},
2045 { "ascii delete", test_ascii_delete
},
2046 { "ascii delete noreply", test_ascii_delete_noreply
},
2047 { "ascii incr", test_ascii_incr
},
2048 { "ascii incr noreply", test_ascii_incr_noreply
},
2049 { "ascii decr", test_ascii_decr
},
2050 { "ascii decr noreply", test_ascii_decr_noreply
},
2051 { "ascii append", test_ascii_append
},
2052 { "ascii append noreply", test_ascii_append_noreply
},
2053 { "ascii prepend", test_ascii_prepend
},
2054 { "ascii prepend noreply", test_ascii_prepend_noreply
},
2055 { "ascii stat", test_ascii_stat
},
2056 { "binary noop", test_binary_noop
},
2057 { "binary quit", test_binary_quit
},
2058 { "binary quitq", test_binary_quitq
},
2059 { "binary set", test_binary_set
},
2060 { "binary setq", test_binary_setq
},
2061 { "binary flush", test_binary_flush
},
2062 { "binary flushq", test_binary_flushq
},
2063 { "binary add", test_binary_add
},
2064 { "binary addq", test_binary_addq
},
2065 { "binary replace", test_binary_replace
},
2066 { "binary replaceq", test_binary_replaceq
},
2067 { "binary delete", test_binary_delete
},
2068 { "binary deleteq", test_binary_deleteq
},
2069 { "binary get", test_binary_get
},
2070 { "binary getq", test_binary_getq
},
2071 { "binary getk", test_binary_getk
},
2072 { "binary getkq", test_binary_getkq
},
2073 { "binary incr", test_binary_incr
},
2074 { "binary incrq", test_binary_incrq
},
2075 { "binary decr", test_binary_decr
},
2076 { "binary decrq", test_binary_decrq
},
2077 { "binary version", test_binary_version
},
2078 { "binary append", test_binary_append
},
2079 { "binary appendq", test_binary_appendq
},
2080 { "binary prepend", test_binary_prepend
},
2081 { "binary prependq", test_binary_prependq
},
2082 { "binary stat", test_binary_stat
},
2086 const int ascii_tests
= 1;
2087 const int binary_tests
= 2;
2095 int main(int argc
, char **argv
)
2097 static const char * const status_msg
[]= {"[skip]", "[pass]", "[pass]", "[FAIL]"};
2098 struct test_type_st tests
= { true, true };
2101 const char *hostname
= NULL
;
2102 const char *port
= MEMCACHED_DEFAULT_PORT_STRING
;
2105 const char *testname
= NULL
;
2109 while ((cmd
= getopt(argc
, argv
, "qt:vch:p:PT:?ab")) != EOF
)
2114 tests
.binary
= false;
2123 timeout
= atoi(optarg
);
2126 fprintf(stderr
, "Invalid timeout. Please specify a number for -t\n");
2127 return EXIT_FAILURE
;
2131 case 'v': verbose
= true;
2134 case 'c': do_core
= true;
2137 case 'h': hostname
= optarg
;
2140 case 'p': port
= optarg
;
2147 case 'P': prompt
= true;
2150 case 'T': testname
= optarg
;
2154 fprintf(stderr
, "Usage: %s [-h hostname] [-p port] [-c] [-v] [-t n] [-P] [-T testname]'\n"
2155 "\t-c\tGenerate coredump if a test fails\n"
2156 "\t-v\tVerbose test output (print out the assertion)\n"
2157 "\t-t n\tSet the timeout for io-operations to n seconds\n"
2158 "\t-P\tPrompt the user before starting a test.\n"
2159 "\t\t\t\"skip\" will skip the test\n"
2160 "\t\t\t\"quit\" will terminate memcapable\n"
2161 "\t\t\tEverything else will start the test\n"
2162 "\t-T n\tJust run the test named n\n"
2163 "\t-a\tOnly test the ascii protocol\n"
2164 "\t-b\tOnly test the binary protocol\n",
2166 return EXIT_SUCCESS
;
2172 fprintf(stderr
, "No hostname was provided.\n");
2173 return EXIT_FAILURE
;
2176 initialize_sockets();
2177 sock
= connect_server(hostname
, port
);
2178 if (sock
== INVALID_SOCKET
)
2180 fprintf(stderr
, "Failed to connect to <%s:%s>: %s\n",
2181 hostname
?:"(null)", port
?:"(null)", strerror(get_socket_errno()));
2182 return EXIT_FAILURE
;
2185 for (int ii
= 0; testcases
[ii
].description
!= NULL
; ++ii
)
2187 if (testname
!= NULL
&& strcmp(testcases
[ii
].description
, testname
) != 0)
2192 if ((testcases
[ii
].description
[0] == 'a' && (tests
.ascii
) == 0) ||
2193 (testcases
[ii
].description
[0] == 'b' && (tests
.binary
) == 0))
2198 fprintf(stdout
, "%-40s", testcases
[ii
].description
);
2203 fprintf(stdout
, "\nPress <return> when you are ready? ");
2204 char buffer
[80] = {0};
2205 if (fgets(buffer
, sizeof(buffer
), stdin
) != NULL
) {
2206 if (strncmp(buffer
, "skip", 4) == 0)
2208 fprintf(stdout
, "%-40s%s\n", testcases
[ii
].description
,
2209 status_msg
[TEST_SKIP
]);
2213 if (strncmp(buffer
, "quit", 4) == 0)
2219 fprintf(stdout
, "%-40s", testcases
[ii
].description
);
2223 bool reconnect
= false;
2224 enum test_return ret
= testcases
[ii
].function();
2225 if (ret
== TEST_FAIL
)
2231 fprintf(stderr
, "\n");
2234 else if (ret
== TEST_PASS_RECONNECT
)
2239 if (ret
== TEST_FAIL
)
2241 fprintf(stderr
, "%s\n", status_msg
[ret
]);
2245 fprintf(stdout
, "%s\n", status_msg
[ret
]);
2251 if ((sock
= connect_server(hostname
, port
)) == INVALID_SOCKET
)
2253 fprintf(stderr
, "Failed to connect to <%s:%s>: %s\n", hostname
?:"(null)", port
?:"(null)", strerror(get_socket_errno()));
2254 fprintf(stderr
, "%d of %d tests failed\n", failed
, total
);
2255 return EXIT_FAILURE
;
2263 fprintf(stdout
, "All tests passed\n");
2267 fprintf(stderr
, "%d of %d tests failed\n", failed
, total
);
2270 return (failed
== 0) ? EXIT_SUCCESS
: EXIT_FAILURE
;