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 "libmemcached/memcached/protocol_binary.h"
41 #include "libmemcached/byteorder.h"
42 #include "clients/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;
67 * Instead of having to cast between the different datatypes we create
68 * a union of all of the different types of pacages we want to send.
69 * A lot of the different commands use the same packet layout, so I'll
70 * just define the different types I need. The typedefs only contain
71 * the header of the message, so we need some space for keys and body
72 * To avoid to have to do multiple writes, lets add a chunk of memory
73 * to use. 1k should be more than enough for header, key and body.
77 protocol_binary_request_no_extras plain
;
78 protocol_binary_request_flush flush
;
79 protocol_binary_request_incr incr
;
80 protocol_binary_request_set set
;
86 protocol_binary_response_no_extras plain
;
87 protocol_binary_response_incr incr
;
88 protocol_binary_response_decr decr
;
94 TEST_SKIP
, TEST_PASS
, TEST_PASS_RECONNECT
, TEST_FAIL
98 * Try to get an addrinfo struct for a given port on a given host
100 static struct addrinfo
*lookuphost(const char *hostname
, const char *port
)
102 struct addrinfo
*ai
= 0;
103 struct addrinfo hints
;
104 memset(&hints
, 0, sizeof(struct addrinfo
));
105 hints
.ai_family
=AF_UNSPEC
;
106 hints
.ai_protocol
=IPPROTO_TCP
;
107 hints
.ai_socktype
=SOCK_STREAM
;
109 int error
= getaddrinfo(hostname
, port
, &hints
, &ai
);
112 if (error
!= EAI_SYSTEM
)
114 fprintf(stderr
, "getaddrinfo(): %s\n", gai_strerror(error
));
118 perror("getaddrinfo()");
126 * Set the socket in nonblocking mode
127 * @return -1 if failure, the socket otherwise
129 static memcached_socket_t
set_noblock(void)
133 if (ioctlsocket(sock
, FIONBIO
, &arg
) == SOCKET_ERROR
)
135 perror("Failed to set nonblocking io");
137 return INVALID_SOCKET
;
140 int flags
= fcntl(sock
, F_GETFL
, 0);
143 perror("Failed to get socket flags");
144 memcached_close_socket(sock
);
145 return INVALID_SOCKET
;
148 if ((flags
& O_NONBLOCK
) != O_NONBLOCK
)
150 if (fcntl(sock
, F_SETFL
, flags
| O_NONBLOCK
) == -1)
152 perror("Failed to set socket to nonblocking mode");
153 memcached_close_socket(sock
);
154 return INVALID_SOCKET
;
162 * Try to open a connection to the server
163 * @param hostname the name of the server to connect to
164 * @param port the port number (or service) to connect to
165 * @return positive integer if success, -1 otherwise
167 static memcached_socket_t
connect_server(const char *hostname
, const char *port
)
169 struct addrinfo
*ai
= lookuphost(hostname
, port
);
170 sock
= INVALID_SOCKET
;
173 if ((sock
= socket(ai
->ai_family
, ai
->ai_socktype
,
174 ai
->ai_protocol
)) != INVALID_SOCKET
)
176 if (connect(sock
, ai
->ai_addr
, ai
->ai_addrlen
) == SOCKET_ERROR
)
178 fprintf(stderr
, "Failed to connect socket: %s\n",
179 strerror(get_socket_errno()));
181 sock
= INVALID_SOCKET
;
189 fprintf(stderr
, "Failed to create socket: %s\n",
190 strerror(get_socket_errno()));
198 static ssize_t
timeout_io_op(memcached_socket_t fd
, short direction
, void *buf
, size_t len
)
202 if (direction
== POLLOUT
)
204 ret
= send(fd
, buf
, len
, 0);
208 ret
= recv(fd
, buf
, len
, 0);
211 if (ret
== SOCKET_ERROR
&& get_socket_errno() == EWOULDBLOCK
)
214 memset(&fds
, 0, sizeof(struct pollfd
));
215 fds
.events
= direction
;
218 int err
= poll(&fds
, 1, timeout
* 1000);
221 if (direction
== POLLOUT
)
223 ret
= send(fd
, buf
, len
, 0);
227 ret
= recv(fd
, buf
, len
, 0);
236 perror("Failed to poll");
245 * Ensure that an expression is true. If it isn't print out a message similar
246 * to assert() and create a coredump if the user wants that. If not an error
247 * message is returned.
250 static enum test_return
ensure(bool val
, const char *expression
, const char *file
, int line
)
256 fprintf(stdout
, "\n%s:%d: %s", file
, line
, expression
);
270 #define verify(expression) do { if (ensure(expression, #expression, __FILE__, __LINE__) == TEST_FAIL) return TEST_FAIL; } while (0)
271 #define execute(expression) do { if (ensure(expression == TEST_PASS, #expression, __FILE__, __LINE__) == TEST_FAIL) return TEST_FAIL; } while (0)
274 * Send a chunk of memory over the socket (retry if the call is iterrupted
276 static enum test_return
retry_write(const void* buf
, size_t len
)
279 const char* ptr
= static_cast<const char *>(buf
);
283 size_t num_bytes
= len
- offset
;
284 ssize_t nw
= timeout_io_op(sock
, POLLOUT
, (void*)(ptr
+ offset
), num_bytes
);
287 verify(get_socket_errno() == EINTR
|| get_socket_errno() == EAGAIN
);
294 } while (offset
< len
);
300 * Resend a packet to the server (All fields in the command header should
301 * be in network byte order)
303 static enum test_return
resend_packet(command
*cmd
)
305 size_t length
= sizeof (protocol_binary_request_no_extras
) +
306 ntohl(cmd
->plain
.message
.header
.request
.bodylen
);
308 execute(retry_write(cmd
, length
));
313 * Send a command to the server. The command header needs to be updated
314 * to network byte order
316 static enum test_return
send_packet(command
*cmd
)
318 /* Fix the byteorder of the header */
319 cmd
->plain
.message
.header
.request
.keylen
=
320 ntohs(cmd
->plain
.message
.header
.request
.keylen
);
321 cmd
->plain
.message
.header
.request
.bodylen
=
322 ntohl(cmd
->plain
.message
.header
.request
.bodylen
);
323 cmd
->plain
.message
.header
.request
.cas
=
324 memcached_ntohll(cmd
->plain
.message
.header
.request
.cas
);
326 execute(resend_packet(cmd
));
331 * Read a fixed length chunk of data from the server
333 static enum test_return
retry_read(void *buf
, size_t len
)
338 ssize_t nr
= timeout_io_op(sock
, POLLIN
, ((char*) buf
) + offset
, len
- offset
);
341 fprintf(stderr
, "Errno: %d %s\n", get_socket_errno(), strerror(errno
));
342 verify(get_socket_errno() == EINTR
|| get_socket_errno() == EAGAIN
);
351 } while (offset
< len
);
357 * Receive a response from the server and conver the fields in the header
358 * to local byte order
360 static enum test_return
recv_packet(response
*rsp
)
362 execute(retry_read(rsp
, sizeof(protocol_binary_response_no_extras
)));
364 /* Fix the byte order in the packet header */
365 rsp
->plain
.message
.header
.response
.keylen
=
366 ntohs(rsp
->plain
.message
.header
.response
.keylen
);
367 rsp
->plain
.message
.header
.response
.status
=
368 ntohs(rsp
->plain
.message
.header
.response
.status
);
369 rsp
->plain
.message
.header
.response
.bodylen
=
370 ntohl(rsp
->plain
.message
.header
.response
.bodylen
);
371 rsp
->plain
.message
.header
.response
.cas
=
372 memcached_ntohll(rsp
->plain
.message
.header
.response
.cas
);
374 size_t bodysz
= rsp
->plain
.message
.header
.response
.bodylen
;
376 execute(retry_read(rsp
->bytes
+ sizeof (protocol_binary_response_no_extras
), bodysz
));
382 * Create a storage command (add, set, replace etc)
384 * @param cmd destination buffer
385 * @param cc the storage command to create
386 * @param key the key to store
387 * @param keylen the length of the key
388 * @param dta the data to store with the key
389 * @param dtalen the length of the data to store with the key
390 * @param flags the flags to store along with the key
391 * @param exptime the expiry time for the key
393 static void storage_command(command
*cmd
,
402 /* all of the storage commands use the same command layout */
403 protocol_binary_request_set
*request
= &cmd
->set
;
405 memset(request
, 0, sizeof (*request
));
406 request
->message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
407 request
->message
.header
.request
.opcode
= cc
;
408 request
->message
.header
.request
.keylen
= (uint16_t)keylen
;
409 request
->message
.header
.request
.extlen
= 8;
410 request
->message
.header
.request
.bodylen
= (uint32_t)(keylen
+ 8 + dtalen
);
411 request
->message
.header
.request
.opaque
= 0xdeadbeef;
412 request
->message
.body
.flags
= flags
;
413 request
->message
.body
.expiration
= exptime
;
415 off_t key_offset
= sizeof (protocol_binary_request_no_extras
) + 8;
416 memcpy(cmd
->bytes
+ key_offset
, key
, keylen
);
418 memcpy(cmd
->bytes
+ key_offset
+ keylen
, dta
, dtalen
);
422 * Create a basic command to send to the server
423 * @param cmd destination buffer
424 * @param cc the command to create
425 * @param key the key to store
426 * @param keylen the length of the key
427 * @param dta the data to store with the key
428 * @param dtalen the length of the data to store with the key
430 static void raw_command(command
*cmd
,
437 /* all of the storage commands use the same command layout */
438 memset(cmd
, 0, sizeof (*cmd
));
439 cmd
->plain
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
440 cmd
->plain
.message
.header
.request
.opcode
= cc
;
441 cmd
->plain
.message
.header
.request
.keylen
= (uint16_t)keylen
;
442 cmd
->plain
.message
.header
.request
.bodylen
= (uint32_t)(keylen
+ dtalen
);
443 cmd
->plain
.message
.header
.request
.opaque
= 0xdeadbeef;
445 off_t key_offset
= sizeof (protocol_binary_request_no_extras
);
448 memcpy(cmd
->bytes
+ key_offset
, key
, keylen
);
451 memcpy(cmd
->bytes
+ key_offset
+ keylen
, dta
, dtalen
);
455 * Create the flush command
456 * @param cmd destination buffer
457 * @param cc the command to create (FLUSH/FLUSHQ)
458 * @param exptime when to flush
459 * @param use_extra to force using of the extra field?
461 static void flush_command(command
*cmd
,
462 uint8_t cc
, uint32_t exptime
, bool use_extra
)
464 memset(cmd
, 0, sizeof (cmd
->flush
));
465 cmd
->flush
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
466 cmd
->flush
.message
.header
.request
.opcode
= cc
;
467 cmd
->flush
.message
.header
.request
.opaque
= 0xdeadbeef;
469 if (exptime
!= 0 || use_extra
)
471 cmd
->flush
.message
.header
.request
.extlen
= 4;
472 cmd
->flush
.message
.body
.expiration
= htonl(exptime
);
473 cmd
->flush
.message
.header
.request
.bodylen
= 4;
478 * Create a incr/decr command
479 * @param cc the cmd to create (FLUSH/FLUSHQ)
480 * @param key the key to operate on
481 * @param keylen the number of bytes in the key
482 * @param delta the number to add/subtract
483 * @param initial the initial value if the key doesn't exist
484 * @param exptime when the key should expire if it isn't set
486 static void arithmetic_command(command
*cmd
,
494 memset(cmd
, 0, sizeof (cmd
->incr
));
495 cmd
->incr
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
496 cmd
->incr
.message
.header
.request
.opcode
= cc
;
497 cmd
->incr
.message
.header
.request
.keylen
= (uint16_t)keylen
;
498 cmd
->incr
.message
.header
.request
.extlen
= 20;
499 cmd
->incr
.message
.header
.request
.bodylen
= (uint32_t)(keylen
+ 20);
500 cmd
->incr
.message
.header
.request
.opaque
= 0xdeadbeef;
501 cmd
->incr
.message
.body
.delta
= memcached_htonll(delta
);
502 cmd
->incr
.message
.body
.initial
= memcached_htonll(initial
);
503 cmd
->incr
.message
.body
.expiration
= htonl(exptime
);
505 off_t key_offset
= sizeof (protocol_binary_request_no_extras
) + 20;
506 memcpy(cmd
->bytes
+ key_offset
, key
, keylen
);
510 * Validate the response header from the server
511 * @param rsp the response to check
512 * @param cc the expected command
513 * @param status the expected status
515 static enum test_return
do_validate_response_header(response
*rsp
,
516 uint8_t cc
, uint16_t status
)
518 verify(rsp
->plain
.message
.header
.response
.magic
== PROTOCOL_BINARY_RES
);
519 verify(rsp
->plain
.message
.header
.response
.opcode
== cc
);
520 verify(rsp
->plain
.message
.header
.response
.datatype
== PROTOCOL_BINARY_RAW_BYTES
);
521 verify(rsp
->plain
.message
.header
.response
.status
== status
);
522 verify(rsp
->plain
.message
.header
.response
.opaque
== 0xdeadbeef);
524 if (status
== PROTOCOL_BINARY_RESPONSE_SUCCESS
)
527 case PROTOCOL_BINARY_CMD_ADDQ
:
528 case PROTOCOL_BINARY_CMD_APPENDQ
:
529 case PROTOCOL_BINARY_CMD_DECREMENTQ
:
530 case PROTOCOL_BINARY_CMD_DELETEQ
:
531 case PROTOCOL_BINARY_CMD_FLUSHQ
:
532 case PROTOCOL_BINARY_CMD_INCREMENTQ
:
533 case PROTOCOL_BINARY_CMD_PREPENDQ
:
534 case PROTOCOL_BINARY_CMD_QUITQ
:
535 case PROTOCOL_BINARY_CMD_REPLACEQ
:
536 case PROTOCOL_BINARY_CMD_SETQ
:
537 verify("Quiet command shouldn't return on success" == NULL
);
543 case PROTOCOL_BINARY_CMD_ADD
:
544 case PROTOCOL_BINARY_CMD_REPLACE
:
545 case PROTOCOL_BINARY_CMD_SET
:
546 case PROTOCOL_BINARY_CMD_APPEND
:
547 case PROTOCOL_BINARY_CMD_PREPEND
:
548 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
549 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
550 verify(rsp
->plain
.message
.header
.response
.bodylen
== 0);
551 verify(rsp
->plain
.message
.header
.response
.cas
!= 0);
553 case PROTOCOL_BINARY_CMD_FLUSH
:
554 case PROTOCOL_BINARY_CMD_NOOP
:
555 case PROTOCOL_BINARY_CMD_QUIT
:
556 case PROTOCOL_BINARY_CMD_DELETE
:
557 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
558 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
559 verify(rsp
->plain
.message
.header
.response
.bodylen
== 0);
560 verify(rsp
->plain
.message
.header
.response
.cas
== 0);
563 case PROTOCOL_BINARY_CMD_DECREMENT
:
564 case PROTOCOL_BINARY_CMD_INCREMENT
:
565 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
566 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
567 verify(rsp
->plain
.message
.header
.response
.bodylen
== 8);
568 verify(rsp
->plain
.message
.header
.response
.cas
!= 0);
571 case PROTOCOL_BINARY_CMD_STAT
:
572 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
573 /* key and value exists in all packets except in the terminating */
574 verify(rsp
->plain
.message
.header
.response
.cas
== 0);
577 case PROTOCOL_BINARY_CMD_VERSION
:
578 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
579 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
580 verify(rsp
->plain
.message
.header
.response
.bodylen
!= 0);
581 verify(rsp
->plain
.message
.header
.response
.cas
== 0);
584 case PROTOCOL_BINARY_CMD_GET
:
585 case PROTOCOL_BINARY_CMD_GETQ
:
586 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
587 verify(rsp
->plain
.message
.header
.response
.extlen
== 4);
588 verify(rsp
->plain
.message
.header
.response
.cas
!= 0);
591 case PROTOCOL_BINARY_CMD_GETK
:
592 case PROTOCOL_BINARY_CMD_GETKQ
:
593 verify(rsp
->plain
.message
.header
.response
.keylen
!= 0);
594 verify(rsp
->plain
.message
.header
.response
.extlen
== 4);
595 verify(rsp
->plain
.message
.header
.response
.cas
!= 0);
599 /* Undefined command code */
605 verify(rsp
->plain
.message
.header
.response
.cas
== 0);
606 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
607 if (cc
!= PROTOCOL_BINARY_CMD_GETK
)
609 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
616 /* We call verify(validate_response_header), but that macro
617 * expects a boolean expression, and the function returns
618 * an enum.... Let's just create a macro to avoid cluttering
619 * the code with all of the == TEST_PASS ;-)
621 #define validate_response_header(a,b,c) \
622 do_validate_response_header(a,b,c) == TEST_PASS
625 static enum test_return
send_binary_noop(void)
628 raw_command(&cmd
, PROTOCOL_BINARY_CMD_NOOP
, NULL
, 0, NULL
, 0);
629 execute(send_packet(&cmd
));
633 static enum test_return
receive_binary_noop(void)
636 execute(recv_packet(&rsp
));
637 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_NOOP
,
638 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
642 static enum test_return
test_binary_noop(void)
644 execute(send_binary_noop());
645 execute(receive_binary_noop());
649 static enum test_return
test_binary_quit_impl(uint8_t cc
)
653 raw_command(&cmd
, cc
, NULL
, 0, NULL
, 0);
655 execute(send_packet(&cmd
));
656 if (cc
== PROTOCOL_BINARY_CMD_QUIT
)
658 execute(recv_packet(&rsp
));
659 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_QUIT
,
660 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
663 /* Socket should be closed now, read should return EXIT_SUCCESS */
664 verify(timeout_io_op(sock
, POLLIN
, rsp
.bytes
, sizeof(rsp
.bytes
)) == 0);
666 return TEST_PASS_RECONNECT
;
669 static enum test_return
test_binary_quit(void)
671 return test_binary_quit_impl(PROTOCOL_BINARY_CMD_QUIT
);
674 static enum test_return
test_binary_quitq(void)
676 return test_binary_quit_impl(PROTOCOL_BINARY_CMD_QUITQ
);
679 static enum test_return
test_binary_set_impl(const char* key
, uint8_t cc
)
684 uint64_t value
= 0xdeadbeefdeadcafeULL
;
685 storage_command(&cmd
, cc
, key
, strlen(key
), &value
, sizeof (value
), 0, 0);
687 /* set should always work */
688 for (int ii
= 0; ii
< 10; ii
++)
692 execute(send_packet(&cmd
));
696 execute(resend_packet(&cmd
));
699 if (cc
== PROTOCOL_BINARY_CMD_SET
)
701 execute(recv_packet(&rsp
));
702 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
705 execute(test_binary_noop());
709 * We need to get the current CAS id, and at this time we haven't
710 * verified that we have a working get
712 if (cc
== PROTOCOL_BINARY_CMD_SETQ
)
714 cmd
.set
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_SET
;
715 execute(resend_packet(&cmd
));
716 execute(recv_packet(&rsp
));
717 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_SET
,
718 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
719 cmd
.set
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_SETQ
;
722 /* try to set with the correct CAS value */
723 cmd
.plain
.message
.header
.request
.cas
= memcached_htonll(rsp
.plain
.message
.header
.response
.cas
);
724 execute(resend_packet(&cmd
));
725 if (cc
== PROTOCOL_BINARY_CMD_SET
)
727 execute(recv_packet(&rsp
));
728 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
731 execute(test_binary_noop());
733 /* try to set with an incorrect CAS value */
734 cmd
.plain
.message
.header
.request
.cas
= memcached_htonll(rsp
.plain
.message
.header
.response
.cas
- 1);
735 execute(resend_packet(&cmd
));
736 execute(send_binary_noop());
737 execute(recv_packet(&rsp
));
738 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS
));
739 execute(receive_binary_noop());
744 static enum test_return
test_binary_set(void)
746 return test_binary_set_impl("test_binary_set", PROTOCOL_BINARY_CMD_SET
);
749 static enum test_return
test_binary_setq(void)
751 return test_binary_set_impl("test_binary_setq", PROTOCOL_BINARY_CMD_SETQ
);
754 static enum test_return
test_binary_add_impl(const char* key
, uint8_t cc
)
758 uint64_t value
= 0xdeadbeefdeadcafeULL
;
759 storage_command(&cmd
, cc
, key
, strlen(key
), &value
, sizeof (value
), 0, 0);
761 /* first add should work, rest of them should fail (even with cas
763 for (int ii
=0; ii
< 10; ii
++)
766 execute(send_packet(&cmd
));
768 execute(resend_packet(&cmd
));
770 if (cc
== PROTOCOL_BINARY_CMD_ADD
|| ii
> 0)
772 uint16_t expected_result
;
774 expected_result
= PROTOCOL_BINARY_RESPONSE_SUCCESS
;
776 expected_result
= PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS
;
778 execute(send_binary_noop());
779 execute(recv_packet(&rsp
));
780 execute(receive_binary_noop());
781 verify(validate_response_header(&rsp
, cc
, expected_result
));
784 execute(test_binary_noop());
790 static enum test_return
test_binary_add(void)
792 return test_binary_add_impl("test_binary_add", PROTOCOL_BINARY_CMD_ADD
);
795 static enum test_return
test_binary_addq(void)
797 return test_binary_add_impl("test_binary_addq", PROTOCOL_BINARY_CMD_ADDQ
);
800 static enum test_return
binary_set_item(const char *key
, const char *value
)
804 storage_command(&cmd
, PROTOCOL_BINARY_CMD_SET
, key
, strlen(key
),
805 value
, strlen(value
), 0, 0);
806 execute(send_packet(&cmd
));
807 execute(recv_packet(&rsp
));
808 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_SET
,
809 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
813 static enum test_return
test_binary_replace_impl(const char* key
, uint8_t cc
)
817 uint64_t value
= 0xdeadbeefdeadcafeULL
;
818 storage_command(&cmd
, cc
, key
, strlen(key
), &value
, sizeof (value
), 0, 0);
820 /* first replace should fail, successive should succeed (when the
822 for (int ii
= 0; ii
< 10; ii
++)
826 execute(send_packet(&cmd
));
830 execute(resend_packet(&cmd
));
833 if (cc
== PROTOCOL_BINARY_CMD_REPLACE
|| ii
== 0)
835 uint16_t expected_result
;
838 expected_result
=PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
;
842 expected_result
=PROTOCOL_BINARY_RESPONSE_SUCCESS
;
845 execute(send_binary_noop());
846 execute(recv_packet(&rsp
));
847 execute(receive_binary_noop());
848 verify(validate_response_header(&rsp
, cc
, expected_result
));
851 execute(binary_set_item(key
, key
));
855 execute(test_binary_noop());
859 /* verify that replace with CAS value works! */
860 cmd
.plain
.message
.header
.request
.cas
= memcached_htonll(rsp
.plain
.message
.header
.response
.cas
);
861 execute(resend_packet(&cmd
));
863 if (cc
== PROTOCOL_BINARY_CMD_REPLACE
)
865 execute(recv_packet(&rsp
));
866 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
869 execute(test_binary_noop());
871 /* try to set with an incorrect CAS value */
872 cmd
.plain
.message
.header
.request
.cas
= memcached_htonll(rsp
.plain
.message
.header
.response
.cas
- 1);
873 execute(resend_packet(&cmd
));
874 execute(send_binary_noop());
875 execute(recv_packet(&rsp
));
876 execute(receive_binary_noop());
877 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS
));
882 static enum test_return
test_binary_replace(void)
884 return test_binary_replace_impl("test_binary_replace", PROTOCOL_BINARY_CMD_REPLACE
);
887 static enum test_return
test_binary_replaceq(void)
889 return test_binary_replace_impl("test_binary_replaceq", PROTOCOL_BINARY_CMD_REPLACEQ
);
892 static enum test_return
test_binary_delete_impl(const char *key
, uint8_t cc
)
896 raw_command(&cmd
, cc
, key
, strlen(key
), NULL
, 0);
898 /* The delete shouldn't work the first time, because the item isn't there */
899 execute(send_packet(&cmd
));
900 execute(send_binary_noop());
901 execute(recv_packet(&rsp
));
902 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
));
903 execute(receive_binary_noop());
904 execute(binary_set_item(key
, key
));
906 /* The item should be present now, resend*/
907 execute(resend_packet(&cmd
));
908 if (cc
== PROTOCOL_BINARY_CMD_DELETE
)
910 execute(recv_packet(&rsp
));
911 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
914 execute(test_binary_noop());
919 static enum test_return
test_binary_delete(void)
921 return test_binary_delete_impl("test_binary_delete", PROTOCOL_BINARY_CMD_DELETE
);
924 static enum test_return
test_binary_deleteq(void)
926 return test_binary_delete_impl("test_binary_deleteq", PROTOCOL_BINARY_CMD_DELETEQ
);
929 static enum test_return
test_binary_get_impl(const char *key
, uint8_t cc
)
934 raw_command(&cmd
, cc
, key
, strlen(key
), NULL
, 0);
935 execute(send_packet(&cmd
));
936 execute(send_binary_noop());
938 if (cc
== PROTOCOL_BINARY_CMD_GET
|| cc
== PROTOCOL_BINARY_CMD_GETK
)
940 execute(recv_packet(&rsp
));
941 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
));
944 execute(receive_binary_noop());
946 execute(binary_set_item(key
, key
));
947 execute(resend_packet(&cmd
));
948 execute(send_binary_noop());
950 execute(recv_packet(&rsp
));
951 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
952 execute(receive_binary_noop());
957 static enum test_return
test_binary_get(void)
959 return test_binary_get_impl("test_binary_get", PROTOCOL_BINARY_CMD_GET
);
962 static enum test_return
test_binary_getk(void)
964 return test_binary_get_impl("test_binary_getk", PROTOCOL_BINARY_CMD_GETK
);
967 static enum test_return
test_binary_getq(void)
969 return test_binary_get_impl("test_binary_getq", PROTOCOL_BINARY_CMD_GETQ
);
972 static enum test_return
test_binary_getkq(void)
974 return test_binary_get_impl("test_binary_getkq", PROTOCOL_BINARY_CMD_GETKQ
);
977 static enum test_return
test_binary_incr_impl(const char* key
, uint8_t cc
)
981 arithmetic_command(&cmd
, cc
, key
, strlen(key
), 1, 0, 0);
984 for (ii
= 0; ii
< 10; ++ii
)
987 execute(send_packet(&cmd
));
989 execute(resend_packet(&cmd
));
991 if (cc
== PROTOCOL_BINARY_CMD_INCREMENT
)
993 execute(recv_packet(&rsp
));
994 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
995 verify(memcached_ntohll(rsp
.incr
.message
.body
.value
) == ii
);
998 execute(test_binary_noop());
1001 /* @todo add incorrect CAS */
1005 static enum test_return
test_binary_incr(void)
1007 return test_binary_incr_impl("test_binary_incr", PROTOCOL_BINARY_CMD_INCREMENT
);
1010 static enum test_return
test_binary_incrq(void)
1012 return test_binary_incr_impl("test_binary_incrq", PROTOCOL_BINARY_CMD_INCREMENTQ
);
1015 static enum test_return
test_binary_decr_impl(const char* key
, uint8_t cc
)
1019 arithmetic_command(&cmd
, cc
, key
, strlen(key
), 1, 9, 0);
1022 for (ii
= 9; ii
> -1; --ii
)
1025 execute(send_packet(&cmd
));
1027 execute(resend_packet(&cmd
));
1029 if (cc
== PROTOCOL_BINARY_CMD_DECREMENT
)
1031 execute(recv_packet(&rsp
));
1032 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1033 verify(memcached_ntohll(rsp
.decr
.message
.body
.value
) == (uint64_t)ii
);
1036 execute(test_binary_noop());
1039 /* decr 0 should not wrap */
1040 execute(resend_packet(&cmd
));
1041 if (cc
== PROTOCOL_BINARY_CMD_DECREMENT
)
1043 execute(recv_packet(&rsp
));
1044 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1045 verify(memcached_ntohll(rsp
.decr
.message
.body
.value
) == 0);
1049 /* @todo get the value and verify! */
1053 /* @todo add incorrect cas */
1054 execute(test_binary_noop());
1058 static enum test_return
test_binary_decr(void)
1060 return test_binary_decr_impl("test_binary_decr",
1061 PROTOCOL_BINARY_CMD_DECREMENT
);
1064 static enum test_return
test_binary_decrq(void)
1066 return test_binary_decr_impl("test_binary_decrq",
1067 PROTOCOL_BINARY_CMD_DECREMENTQ
);
1070 static enum test_return
test_binary_version(void)
1074 raw_command(&cmd
, PROTOCOL_BINARY_CMD_VERSION
, NULL
, 0, NULL
, 0);
1076 execute(send_packet(&cmd
));
1077 execute(recv_packet(&rsp
));
1078 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_VERSION
,
1079 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1084 static enum test_return
test_binary_flush_impl(const char *key
, uint8_t cc
)
1089 for (int ii
= 0; ii
< 2; ++ii
)
1091 execute(binary_set_item(key
, key
));
1092 flush_command(&cmd
, cc
, 0, ii
== 0);
1093 execute(send_packet(&cmd
));
1095 if (cc
== PROTOCOL_BINARY_CMD_FLUSH
)
1097 execute(recv_packet(&rsp
));
1098 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1101 execute(test_binary_noop());
1103 raw_command(&cmd
, PROTOCOL_BINARY_CMD_GET
, key
, strlen(key
), NULL
, 0);
1104 execute(send_packet(&cmd
));
1105 execute(recv_packet(&rsp
));
1106 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_GET
,
1107 PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
));
1113 static enum test_return
test_binary_flush(void)
1115 return test_binary_flush_impl("test_binary_flush", PROTOCOL_BINARY_CMD_FLUSH
);
1118 static enum test_return
test_binary_flushq(void)
1120 return test_binary_flush_impl("test_binary_flushq", PROTOCOL_BINARY_CMD_FLUSHQ
);
1123 static enum test_return
test_binary_concat_impl(const char *key
, uint8_t cc
)
1129 if (cc
== PROTOCOL_BINARY_CMD_APPEND
|| cc
== PROTOCOL_BINARY_CMD_APPENDQ
)
1138 execute(binary_set_item(key
, value
));
1140 if (cc
== PROTOCOL_BINARY_CMD_APPEND
|| cc
== PROTOCOL_BINARY_CMD_APPENDQ
)
1149 raw_command(&cmd
, cc
, key
, strlen(key
), value
, strlen(value
));
1150 execute(send_packet(&cmd
));
1151 if (cc
== PROTOCOL_BINARY_CMD_APPEND
|| cc
== PROTOCOL_BINARY_CMD_PREPEND
)
1153 execute(recv_packet(&rsp
));
1154 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1158 execute(test_binary_noop());
1161 raw_command(&cmd
, PROTOCOL_BINARY_CMD_GET
, key
, strlen(key
), NULL
, 0);
1162 execute(send_packet(&cmd
));
1163 execute(recv_packet(&rsp
));
1164 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_GET
,
1165 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1166 verify(rsp
.plain
.message
.header
.response
.bodylen
- 4 == 11);
1167 verify(memcmp(rsp
.bytes
+ 28, "hello world", 11) == 0);
1172 static enum test_return
test_binary_append(void)
1174 return test_binary_concat_impl("test_binary_append", PROTOCOL_BINARY_CMD_APPEND
);
1177 static enum test_return
test_binary_prepend(void)
1179 return test_binary_concat_impl("test_binary_prepend", PROTOCOL_BINARY_CMD_PREPEND
);
1182 static enum test_return
test_binary_appendq(void)
1184 return test_binary_concat_impl("test_binary_appendq", PROTOCOL_BINARY_CMD_APPENDQ
);
1187 static enum test_return
test_binary_prependq(void)
1189 return test_binary_concat_impl("test_binary_prependq", PROTOCOL_BINARY_CMD_PREPENDQ
);
1192 static enum test_return
test_binary_stat(void)
1197 raw_command(&cmd
, PROTOCOL_BINARY_CMD_STAT
, NULL
, 0, NULL
, 0);
1198 execute(send_packet(&cmd
));
1202 execute(recv_packet(&rsp
));
1203 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_STAT
,
1204 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1205 } while (rsp
.plain
.message
.header
.response
.keylen
!= 0);
1210 static enum test_return
send_string(const char *cmd
)
1212 execute(retry_write(cmd
, strlen(cmd
)));
1216 static enum test_return
receive_line(char *buffer
, size_t size
)
1219 while (offset
< size
)
1221 execute(retry_read(buffer
+ offset
, 1));
1222 if (buffer
[offset
] == '\n')
1224 if (offset
+ 1 < size
)
1226 buffer
[offset
+ 1]= '\0';
1238 static enum test_return
receive_response(const char *msg
) {
1240 execute(receive_line(buffer
, sizeof(buffer
)));
1241 if (strcmp(msg
, buffer
) != 0) {
1242 fprintf(stderr
, "[%s]\n", buffer
);
1244 verify(strcmp(msg
, buffer
) == 0);
1248 static enum test_return
receive_error_response(void)
1251 execute(receive_line(buffer
, sizeof(buffer
)));
1252 verify(strncmp(buffer
, "ERROR", 5) == 0 ||
1253 strncmp(buffer
, "CLIENT_ERROR", 12) == 0 ||
1254 strncmp(buffer
, "SERVER_ERROR", 12) == 0);
1258 static enum test_return
test_ascii_quit(void)
1260 /* Verify that quit handles unknown options */
1261 execute(send_string("quit foo bar\r\n"));
1262 execute(receive_error_response());
1264 /* quit doesn't support noreply */
1265 execute(send_string("quit noreply\r\n"));
1266 execute(receive_error_response());
1268 /* Verify that quit works */
1269 execute(send_string("quit\r\n"));
1271 /* Socket should be closed now, read should return EXIT_SUCCESS */
1273 verify(timeout_io_op(sock
, POLLIN
, buffer
, sizeof(buffer
)) == 0);
1274 return TEST_PASS_RECONNECT
;
1278 static enum test_return
test_ascii_version(void)
1280 /* Verify that version command handles unknown options */
1281 execute(send_string("version foo bar\r\n"));
1282 execute(receive_error_response());
1284 /* version doesn't support noreply */
1285 execute(send_string("version noreply\r\n"));
1286 execute(receive_error_response());
1288 /* Verify that verify works */
1289 execute(send_string("version\r\n"));
1291 execute(receive_line(buffer
, sizeof(buffer
)));
1292 verify(strncmp(buffer
, "VERSION ", 8) == 0);
1297 static enum test_return
test_ascii_verbosity(void)
1299 /* This command does not adhere to the spec! */
1300 execute(send_string("verbosity foo bar my\r\n"));
1301 execute(receive_error_response());
1303 execute(send_string("verbosity noreply\r\n"));
1304 execute(receive_error_response());
1306 execute(send_string("verbosity 0 noreply\r\n"));
1307 execute(test_ascii_version());
1309 execute(send_string("verbosity\r\n"));
1310 execute(receive_error_response());
1312 execute(send_string("verbosity 1\r\n"));
1313 execute(receive_response("OK\r\n"));
1315 execute(send_string("verbosity 0\r\n"));
1316 execute(receive_response("OK\r\n"));
1323 static enum test_return
test_ascii_set_impl(const char* key
, bool noreply
)
1325 /* @todo add tests for bogus format! */
1327 snprintf(buffer
, sizeof(buffer
), "set %s 0 0 5%s\r\nvalue\r\n", key
, noreply
? " noreply" : "");
1328 execute(send_string(buffer
));
1332 execute(receive_response("STORED\r\n"));
1335 return test_ascii_version();
1338 static enum test_return
test_ascii_set(void)
1340 return test_ascii_set_impl("test_ascii_set", false);
1343 static enum test_return
test_ascii_set_noreply(void)
1345 return test_ascii_set_impl("test_ascii_set_noreply", true);
1348 static enum test_return
test_ascii_add_impl(const char* key
, bool noreply
)
1350 /* @todo add tests for bogus format! */
1352 snprintf(buffer
, sizeof(buffer
), "add %s 0 0 5%s\r\nvalue\r\n", key
, noreply
? " noreply" : "");
1353 execute(send_string(buffer
));
1357 execute(receive_response("STORED\r\n"));
1360 execute(send_string(buffer
));
1364 execute(receive_response("NOT_STORED\r\n"));
1367 return test_ascii_version();
1370 static enum test_return
test_ascii_add(void)
1372 return test_ascii_add_impl("test_ascii_add", false);
1375 static enum test_return
test_ascii_add_noreply(void)
1377 return test_ascii_add_impl("test_ascii_add_noreply", true);
1380 static enum test_return
ascii_get_unknown_value(char **key
, char **value
, ssize_t
*ndata
)
1384 execute(receive_line(buffer
, sizeof(buffer
)));
1385 verify(strncmp(buffer
, "VALUE ", 6) == 0);
1386 char *end
= strchr(buffer
+ 6, ' ');
1387 verify(end
!= NULL
);
1392 *key
= strdup(buffer
+ 6);
1393 verify(*key
!= NULL
);
1397 unsigned long val
= strtoul(ptr
, &end
, 10); /* flags */
1401 verify(end
!= NULL
);
1403 *ndata
= (ssize_t
)strtoul(end
, &end
, 10); /* size */
1406 verify(end
!= NULL
);
1407 while (end
and *end
!= '\n' and isspace(*end
))
1409 verify(end
and *end
== '\n');
1411 *value
= static_cast<char*>(malloc((size_t)*ndata
));
1412 verify(*value
!= NULL
);
1414 execute(retry_read(*value
, (size_t)*ndata
));
1416 execute(retry_read(buffer
, 2));
1417 verify(memcmp(buffer
, "\r\n", 2) == 0);
1422 static enum test_return
ascii_get_value(const char *key
, const char *value
)
1426 size_t datasize
= strlen(value
);
1428 verify(datasize
< sizeof(buffer
));
1429 execute(receive_line(buffer
, sizeof(buffer
)));
1430 verify(strncmp(buffer
, "VALUE ", 6) == 0);
1431 verify(strncmp(buffer
+ 6, key
, strlen(key
)) == 0);
1432 char *ptr
= buffer
+ 6 + strlen(key
) + 1;
1436 unsigned long val
= strtoul(ptr
, &end
, 10); /* flags */
1440 verify(end
!= NULL
);
1443 val
= strtoul(end
, &end
, 10); /* size */
1446 verify(val
== datasize
);
1447 verify(end
!= NULL
);
1448 while (end
and *end
!= '\n' and isspace(*end
))
1452 verify(end
and *end
== '\n');
1454 execute(retry_read(buffer
, datasize
));
1455 verify(memcmp(buffer
, value
, datasize
) == 0);
1457 execute(retry_read(buffer
, 2));
1458 verify(memcmp(buffer
, "\r\n", 2) == 0);
1463 static enum test_return
ascii_get_item(const char *key
, const char *value
,
1470 datasize
= strlen(value
);
1473 verify(datasize
< sizeof(buffer
));
1474 snprintf(buffer
, sizeof(buffer
), "get %s\r\n", key
);
1475 execute(send_string(buffer
));
1479 execute(ascii_get_value(key
, value
));
1482 execute(retry_read(buffer
, 5));
1483 verify(memcmp(buffer
, "END\r\n", 5) == 0);
1488 static enum test_return
ascii_gets_value(const char *key
, const char *value
,
1493 size_t datasize
= strlen(value
);
1495 verify(datasize
< sizeof(buffer
));
1496 execute(receive_line(buffer
, sizeof(buffer
)));
1497 verify(strncmp(buffer
, "VALUE ", 6) == 0);
1498 verify(strncmp(buffer
+ 6, key
, strlen(key
)) == 0);
1499 char *ptr
= buffer
+ 6 + strlen(key
) + 1;
1503 unsigned long val
= strtoul(ptr
, &end
, 10); /* flags */
1507 verify(end
!= NULL
);
1510 val
= strtoul(end
, &end
, 10); /* size */
1513 verify(val
== datasize
);
1514 verify(end
!= NULL
);
1517 *cas
= strtoul(end
, &end
, 10); /* cas */
1520 verify(val
== datasize
);
1521 verify(end
!= NULL
);
1523 while (end
and *end
!= '\n' and isspace(*end
))
1527 verify(end
and *end
== '\n');
1529 execute(retry_read(buffer
, datasize
));
1530 verify(memcmp(buffer
, value
, datasize
) == 0);
1532 execute(retry_read(buffer
, 2));
1533 verify(memcmp(buffer
, "\r\n", 2) == 0);
1538 static enum test_return
ascii_gets_item(const char *key
, const char *value
,
1539 bool exist
, unsigned long *cas
)
1545 datasize
= strlen(value
);
1548 verify(datasize
< sizeof(buffer
));
1549 snprintf(buffer
, sizeof(buffer
), "gets %s\r\n", key
);
1550 execute(send_string(buffer
));
1553 execute(ascii_gets_value(key
, value
, cas
));
1555 execute(retry_read(buffer
, 5));
1556 verify(memcmp(buffer
, "END\r\n", 5) == 0);
1561 static enum test_return
ascii_set_item(const char *key
, const char *value
)
1564 size_t len
= strlen(value
);
1565 snprintf(buffer
, sizeof(buffer
), "set %s 0 0 %u\r\n", key
, (unsigned int)len
);
1566 execute(send_string(buffer
));
1567 execute(retry_write(value
, len
));
1568 execute(send_string("\r\n"));
1569 execute(receive_response("STORED\r\n"));
1573 static enum test_return
test_ascii_replace_impl(const char* key
, bool noreply
)
1576 snprintf(buffer
, sizeof(buffer
), "replace %s 0 0 5%s\r\nvalue\r\n", key
, noreply
? " noreply" : "");
1577 execute(send_string(buffer
));
1581 execute(test_ascii_version());
1585 execute(receive_response("NOT_STORED\r\n"));
1588 execute(ascii_set_item(key
, "value"));
1589 execute(ascii_get_item(key
, "value", true));
1592 execute(send_string(buffer
));
1595 execute(test_ascii_version());
1597 execute(receive_response("STORED\r\n"));
1599 return test_ascii_version();
1602 static enum test_return
test_ascii_replace(void)
1604 return test_ascii_replace_impl("test_ascii_replace", false);
1607 static enum test_return
test_ascii_replace_noreply(void)
1609 return test_ascii_replace_impl("test_ascii_replace_noreply", true);
1612 static enum test_return
test_ascii_cas_impl(const char* key
, bool noreply
)
1617 execute(ascii_set_item(key
, "value"));
1618 execute(ascii_gets_item(key
, "value", true, &cas
));
1620 snprintf(buffer
, sizeof(buffer
), "cas %s 0 0 6 %lu%s\r\nvalue2\r\n", key
, cas
, noreply
? " noreply" : "");
1621 execute(send_string(buffer
));
1625 execute(test_ascii_version());
1629 execute(receive_response("STORED\r\n"));
1632 /* reexecute the same command should fail due to illegal cas */
1633 execute(send_string(buffer
));
1637 execute(test_ascii_version());
1641 execute(receive_response("EXISTS\r\n"));
1644 return test_ascii_version();
1647 static enum test_return
test_ascii_cas(void)
1649 return test_ascii_cas_impl("test_ascii_cas", false);
1652 static enum test_return
test_ascii_cas_noreply(void)
1654 return test_ascii_cas_impl("test_ascii_cas_noreply", true);
1657 static enum test_return
test_ascii_delete_impl(const char *key
, bool noreply
)
1659 execute(ascii_set_item(key
, "value"));
1661 execute(send_string("delete\r\n"));
1662 execute(receive_error_response());
1663 /* BUG: the server accepts delete a b */
1664 execute(send_string("delete a b c d e\r\n"));
1665 execute(receive_error_response());
1668 snprintf(buffer
, sizeof(buffer
), "delete %s%s\r\n", key
, noreply
? " noreply" : "");
1669 execute(send_string(buffer
));
1672 execute(test_ascii_version());
1674 execute(receive_response("DELETED\r\n"));
1676 execute(ascii_get_item(key
, "value", false));
1677 execute(send_string(buffer
));
1679 execute(test_ascii_version());
1681 execute(receive_response("NOT_FOUND\r\n"));
1686 static enum test_return
test_ascii_delete(void)
1688 return test_ascii_delete_impl("test_ascii_delete", false);
1691 static enum test_return
test_ascii_delete_noreply(void)
1693 return test_ascii_delete_impl("test_ascii_delete_noreply", true);
1696 static enum test_return
test_ascii_get(void)
1698 execute(ascii_set_item("test_ascii_get", "value"));
1700 execute(send_string("get\r\n"));
1701 execute(receive_error_response());
1702 execute(ascii_get_item("test_ascii_get", "value", true));
1703 execute(ascii_get_item("test_ascii_get_notfound", "value", false));
1708 static enum test_return
test_ascii_gets(void)
1710 execute(ascii_set_item("test_ascii_gets", "value"));
1712 execute(send_string("gets\r\n"));
1713 execute(receive_error_response());
1715 execute(ascii_gets_item("test_ascii_gets", "value", true, &cas
));
1716 execute(ascii_gets_item("test_ascii_gets_notfound", "value", false, &cas
));
1721 static enum test_return
test_ascii_mget(void)
1723 const uint32_t nkeys
= 5;
1724 const char * const keys
[]= {
1727 /* test_ascii_mget_3 does not exist :) */
1733 for (uint32_t x
= 0; x
< nkeys
; ++x
)
1735 execute(ascii_set_item(keys
[x
], "value"));
1738 /* Ask for a key that doesn't exist as well */
1739 execute(send_string("get test_ascii_mget1 test_ascii_mget2 test_ascii_mget3 "
1740 "test_ascii_mget4 test_ascii_mget5 "
1741 "test_ascii_mget6\r\n"));
1743 std::vector
<char *> returned
;
1744 returned
.resize(nkeys
);
1746 for (uint32_t x
= 0; x
< nkeys
; ++x
)
1750 execute(ascii_get_unknown_value(&returned
[x
], &v
, &nbytes
));
1751 verify(nbytes
== 5);
1752 verify(memcmp(v
, "value", 5) == 0);
1757 execute(retry_read(buffer
, 5));
1758 verify(memcmp(buffer
, "END\r\n", 5) == 0);
1760 /* verify that we got all the keys we expected */
1761 for (uint32_t x
= 0; x
< nkeys
; ++x
)
1764 for (uint32_t y
= 0; y
< nkeys
; ++y
)
1766 if (strcmp(keys
[x
], returned
[y
]) == 0)
1775 for (uint32_t x
= 0; x
< nkeys
; ++x
)
1783 static enum test_return
test_ascii_incr_impl(const char* key
, bool noreply
)
1786 snprintf(cmd
, sizeof(cmd
), "incr %s 1%s\r\n", key
, noreply
? " noreply" : "");
1788 execute(ascii_set_item(key
, "0"));
1789 for (int x
= 1; x
< 11; ++x
)
1791 execute(send_string(cmd
));
1794 execute(test_ascii_version());
1798 execute(receive_line(buffer
, sizeof(buffer
)));
1799 int val
= atoi(buffer
);
1804 execute(ascii_get_item(key
, "10", true));
1809 static enum test_return
test_ascii_incr(void)
1811 return test_ascii_incr_impl("test_ascii_incr", false);
1814 static enum test_return
test_ascii_incr_noreply(void)
1816 return test_ascii_incr_impl("test_ascii_incr_noreply", true);
1819 static enum test_return
test_ascii_decr_impl(const char* key
, bool noreply
)
1822 snprintf(cmd
, sizeof(cmd
), "decr %s 1%s\r\n", key
, noreply
? " noreply" : "");
1824 execute(ascii_set_item(key
, "9"));
1825 for (int x
= 8; x
> -1; --x
)
1827 execute(send_string(cmd
));
1831 execute(test_ascii_version());
1836 execute(receive_line(buffer
, sizeof(buffer
)));
1837 int val
= atoi(buffer
);
1842 execute(ascii_get_item(key
, "0", true));
1844 /* verify that it doesn't wrap */
1845 execute(send_string(cmd
));
1848 execute(test_ascii_version());
1853 execute(receive_line(buffer
, sizeof(buffer
)));
1855 execute(ascii_get_item(key
, "0", true));
1860 static enum test_return
test_ascii_decr(void)
1862 return test_ascii_decr_impl("test_ascii_decr", false);
1865 static enum test_return
test_ascii_decr_noreply(void)
1867 return test_ascii_decr_impl("test_ascii_decr_noreply", true);
1871 static enum test_return
test_ascii_flush_impl(const char *key
, bool noreply
)
1874 /* Verify that the flush_all command handles unknown options */
1875 /* Bug in the current memcached server! */
1876 execute(send_string("flush_all foo bar\r\n"));
1877 execute(receive_error_response());
1880 execute(ascii_set_item(key
, key
));
1881 execute(ascii_get_item(key
, key
, true));
1885 execute(send_string("flush_all noreply\r\n"));
1886 execute(test_ascii_version());
1890 execute(send_string("flush_all\r\n"));
1891 execute(receive_response("OK\r\n"));
1894 execute(ascii_get_item(key
, key
, false));
1899 static enum test_return
test_ascii_flush(void)
1901 return test_ascii_flush_impl("test_ascii_flush", false);
1904 static enum test_return
test_ascii_flush_noreply(void)
1906 return test_ascii_flush_impl("test_ascii_flush_noreply", true);
1909 static enum test_return
test_ascii_concat_impl(const char *key
,
1920 execute(ascii_set_item(key
, value
));
1932 snprintf(cmd
, sizeof(cmd
), "%s %s 0 0 %u%s\r\n%s\r\n",
1933 append
? "append" : "prepend",
1934 key
, (unsigned int)strlen(value
), noreply
? " noreply" : "",
1936 execute(send_string(cmd
));
1940 execute(test_ascii_version());
1944 execute(receive_response("STORED\r\n"));
1947 execute(ascii_get_item(key
, "hello world", true));
1949 snprintf(cmd
, sizeof(cmd
), "%s %s_notfound 0 0 %u%s\r\n%s\r\n",
1950 append
? "append" : "prepend",
1951 key
, (unsigned int)strlen(value
), noreply
? " noreply" : "",
1953 execute(send_string(cmd
));
1957 execute(test_ascii_version());
1961 execute(receive_response("NOT_STORED\r\n"));
1967 static enum test_return
test_ascii_append(void)
1969 return test_ascii_concat_impl("test_ascii_append", true, false);
1972 static enum test_return
test_ascii_prepend(void)
1974 return test_ascii_concat_impl("test_ascii_prepend", false, false);
1977 static enum test_return
test_ascii_append_noreply(void)
1979 return test_ascii_concat_impl("test_ascii_append_noreply", true, true);
1982 static enum test_return
test_ascii_prepend_noreply(void)
1984 return test_ascii_concat_impl("test_ascii_prepend_noreply", false, true);
1987 static enum test_return
test_ascii_stat(void)
1989 execute(send_string("stats noreply\r\n"));
1990 execute(receive_error_response());
1991 execute(send_string("stats\r\n"));
1994 execute(receive_line(buffer
, sizeof(buffer
)));
1995 } while (strcmp(buffer
, "END\r\n") != 0);
1997 return TEST_PASS_RECONNECT
;
2000 typedef enum test_return(*TEST_FUNC
)(void);
2004 const char *description
;
2008 struct testcase testcases
[]= {
2009 { "ascii quit", test_ascii_quit
},
2010 { "ascii version", test_ascii_version
},
2011 { "ascii verbosity", test_ascii_verbosity
},
2012 { "ascii set", test_ascii_set
},
2013 { "ascii set noreply", test_ascii_set_noreply
},
2014 { "ascii get", test_ascii_get
},
2015 { "ascii gets", test_ascii_gets
},
2016 { "ascii mget", test_ascii_mget
},
2017 { "ascii flush", test_ascii_flush
},
2018 { "ascii flush noreply", test_ascii_flush_noreply
},
2019 { "ascii add", test_ascii_add
},
2020 { "ascii add noreply", test_ascii_add_noreply
},
2021 { "ascii replace", test_ascii_replace
},
2022 { "ascii replace noreply", test_ascii_replace_noreply
},
2023 { "ascii cas", test_ascii_cas
},
2024 { "ascii cas noreply", test_ascii_cas_noreply
},
2025 { "ascii delete", test_ascii_delete
},
2026 { "ascii delete noreply", test_ascii_delete_noreply
},
2027 { "ascii incr", test_ascii_incr
},
2028 { "ascii incr noreply", test_ascii_incr_noreply
},
2029 { "ascii decr", test_ascii_decr
},
2030 { "ascii decr noreply", test_ascii_decr_noreply
},
2031 { "ascii append", test_ascii_append
},
2032 { "ascii append noreply", test_ascii_append_noreply
},
2033 { "ascii prepend", test_ascii_prepend
},
2034 { "ascii prepend noreply", test_ascii_prepend_noreply
},
2035 { "ascii stat", test_ascii_stat
},
2036 { "binary noop", test_binary_noop
},
2037 { "binary quit", test_binary_quit
},
2038 { "binary quitq", test_binary_quitq
},
2039 { "binary set", test_binary_set
},
2040 { "binary setq", test_binary_setq
},
2041 { "binary flush", test_binary_flush
},
2042 { "binary flushq", test_binary_flushq
},
2043 { "binary add", test_binary_add
},
2044 { "binary addq", test_binary_addq
},
2045 { "binary replace", test_binary_replace
},
2046 { "binary replaceq", test_binary_replaceq
},
2047 { "binary delete", test_binary_delete
},
2048 { "binary deleteq", test_binary_deleteq
},
2049 { "binary get", test_binary_get
},
2050 { "binary getq", test_binary_getq
},
2051 { "binary getk", test_binary_getk
},
2052 { "binary getkq", test_binary_getkq
},
2053 { "binary incr", test_binary_incr
},
2054 { "binary incrq", test_binary_incrq
},
2055 { "binary decr", test_binary_decr
},
2056 { "binary decrq", test_binary_decrq
},
2057 { "binary version", test_binary_version
},
2058 { "binary append", test_binary_append
},
2059 { "binary appendq", test_binary_appendq
},
2060 { "binary prepend", test_binary_prepend
},
2061 { "binary prependq", test_binary_prependq
},
2062 { "binary stat", test_binary_stat
},
2066 const int ascii_tests
= 1;
2067 const int binary_tests
= 2;
2075 int main(int argc
, char **argv
)
2077 static const char * const status_msg
[]= {"[skip]", "[pass]", "[pass]", "[FAIL]"};
2078 struct test_type_st tests
= { true, true };
2081 const char *hostname
= "localhost";
2082 const char *port
= "11211";
2085 const char *testname
= NULL
;
2089 while ((cmd
= getopt(argc
, argv
, "qt:vch:p:PT:?ab")) != EOF
)
2094 tests
.binary
= false;
2103 timeout
= atoi(optarg
);
2106 fprintf(stderr
, "Invalid timeout. Please specify a number for -t\n");
2107 return EXIT_FAILURE
;
2111 case 'v': verbose
= true;
2114 case 'c': do_core
= true;
2117 case 'h': hostname
= optarg
;
2120 case 'p': port
= optarg
;
2127 case 'P': prompt
= true;
2130 case 'T': testname
= optarg
;
2134 fprintf(stderr
, "Usage: %s [-h hostname] [-p port] [-c] [-v] [-t n] [-P] [-T testname]'\n"
2135 "\t-c\tGenerate coredump if a test fails\n"
2136 "\t-v\tVerbose test output (print out the assertion)\n"
2137 "\t-t n\tSet the timeout for io-operations to n seconds\n"
2138 "\t-P\tPrompt the user before starting a test.\n"
2139 "\t\t\t\"skip\" will skip the test\n"
2140 "\t\t\t\"quit\" will terminate memcapable\n"
2141 "\t\t\tEverything else will start the test\n"
2142 "\t-T n\tJust run the test named n\n"
2143 "\t-a\tOnly test the ascii protocol\n"
2144 "\t-b\tOnly test the binary protocol\n",
2146 return EXIT_SUCCESS
;
2150 initialize_sockets();
2151 sock
= connect_server(hostname
, port
);
2152 if (sock
== INVALID_SOCKET
)
2154 fprintf(stderr
, "Failed to connect to <%s:%s>: %s\n",
2155 hostname
, port
, strerror(get_socket_errno()));
2156 return EXIT_FAILURE
;
2159 for (int ii
= 0; testcases
[ii
].description
!= NULL
; ++ii
)
2161 if (testname
!= NULL
&& strcmp(testcases
[ii
].description
, testname
) != 0)
2166 if ((testcases
[ii
].description
[0] == 'a' && (tests
.ascii
) == 0) ||
2167 (testcases
[ii
].description
[0] == 'b' && (tests
.binary
) == 0))
2172 fprintf(stdout
, "%-40s", testcases
[ii
].description
);
2177 fprintf(stdout
, "\nPress <return> when you are ready? ");
2178 char buffer
[80] = {0};
2179 if (fgets(buffer
, sizeof(buffer
), stdin
) != NULL
) {
2180 if (strncmp(buffer
, "skip", 4) == 0)
2182 fprintf(stdout
, "%-40s%s\n", testcases
[ii
].description
,
2183 status_msg
[TEST_SKIP
]);
2187 if (strncmp(buffer
, "quit", 4) == 0)
2193 fprintf(stdout
, "%-40s", testcases
[ii
].description
);
2197 bool reconnect
= false;
2198 enum test_return ret
= testcases
[ii
].function();
2199 if (ret
== TEST_FAIL
)
2205 fprintf(stderr
, "\n");
2208 else if (ret
== TEST_PASS_RECONNECT
)
2213 if (ret
== TEST_FAIL
)
2215 fprintf(stderr
, "%s\n", status_msg
[ret
]);
2219 fprintf(stdout
, "%s\n", status_msg
[ret
]);
2225 if ((sock
= connect_server(hostname
, port
)) == INVALID_SOCKET
)
2227 fprintf(stderr
, "Failed to connect to <%s:%s>: %s\n", hostname
, port
, strerror(get_socket_errno()));
2228 fprintf(stderr
, "%d of %d tests failed\n", failed
, total
);
2229 return EXIT_FAILURE
;
2237 fprintf(stdout
, "All tests passed\n");
2241 fprintf(stderr
, "%d of %d tests failed\n", failed
, total
);
2244 return (failed
== 0) ? EXIT_SUCCESS
: EXIT_FAILURE
;