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
)
113 fprintf(stderr
, "getaddrinfo(): %s\n", gai_strerror(error
));
115 perror("getaddrinfo()");
122 * Set the socket in nonblocking mode
123 * @return -1 if failure, the socket otherwise
125 static memcached_socket_t
set_noblock(void)
129 if (ioctlsocket(sock
, FIONBIO
, &arg
) == SOCKET_ERROR
)
131 perror("Failed to set nonblocking io");
133 return INVALID_SOCKET
;
136 int flags
= fcntl(sock
, F_GETFL
, 0);
139 perror("Failed to get socket flags");
140 memcached_close_socket(sock
);
141 return INVALID_SOCKET
;
144 if ((flags
& O_NONBLOCK
) != O_NONBLOCK
)
146 if (fcntl(sock
, F_SETFL
, flags
| O_NONBLOCK
) == -1)
148 perror("Failed to set socket to nonblocking mode");
149 memcached_close_socket(sock
);
150 return INVALID_SOCKET
;
158 * Try to open a connection to the server
159 * @param hostname the name of the server to connect to
160 * @param port the port number (or service) to connect to
161 * @return positive integer if success, -1 otherwise
163 static memcached_socket_t
connect_server(const char *hostname
, const char *port
)
165 struct addrinfo
*ai
= lookuphost(hostname
, port
);
166 sock
= INVALID_SOCKET
;
169 if ((sock
= socket(ai
->ai_family
, ai
->ai_socktype
,
170 ai
->ai_protocol
)) != INVALID_SOCKET
)
172 if (connect(sock
, ai
->ai_addr
, ai
->ai_addrlen
) == SOCKET_ERROR
)
174 fprintf(stderr
, "Failed to connect socket: %s\n",
175 strerror(get_socket_errno()));
177 sock
= INVALID_SOCKET
;
185 fprintf(stderr
, "Failed to create socket: %s\n",
186 strerror(get_socket_errno()));
194 static ssize_t
timeout_io_op(memcached_socket_t fd
, short direction
, void *buf
, size_t len
)
198 if (direction
== POLLOUT
)
200 ret
= send(fd
, buf
, len
, 0);
204 ret
= recv(fd
, buf
, len
, 0);
207 if (ret
== SOCKET_ERROR
&& get_socket_errno() == EWOULDBLOCK
)
210 memset(&fds
, 0, sizeof(struct pollfd
));
211 fds
.events
= direction
;
214 int err
= poll(&fds
, 1, timeout
* 1000);
217 if (direction
== POLLOUT
)
219 ret
= send(fd
, buf
, len
, 0);
223 ret
= recv(fd
, buf
, len
, 0);
232 perror("Failed to poll");
241 * Ensure that an expression is true. If it isn't print out a message similar
242 * to assert() and create a coredump if the user wants that. If not an error
243 * message is returned.
246 static enum test_return
ensure(bool val
, const char *expression
, const char *file
, int line
)
252 fprintf(stderr
, "\n%s:%d: %s", file
, line
, expression
);
266 #define verify(expression) do { if (ensure(expression, #expression, __FILE__, __LINE__) == TEST_FAIL) return TEST_FAIL; } while (0)
267 #define execute(expression) do { if (ensure(expression == TEST_PASS, #expression, __FILE__, __LINE__) == TEST_FAIL) return TEST_FAIL; } while (0)
270 * Send a chunk of memory over the socket (retry if the call is iterrupted
272 static enum test_return
retry_write(const void* buf
, size_t len
)
275 const char* ptr
= static_cast<const char *>(buf
);
279 size_t num_bytes
= len
- offset
;
280 ssize_t nw
= timeout_io_op(sock
, POLLOUT
, (void*)(ptr
+ offset
), num_bytes
);
283 verify(get_socket_errno() == EINTR
|| get_socket_errno() == EAGAIN
);
290 } while (offset
< len
);
296 * Resend a packet to the server (All fields in the command header should
297 * be in network byte order)
299 static enum test_return
resend_packet(command
*cmd
)
301 size_t length
= sizeof (protocol_binary_request_no_extras
) +
302 ntohl(cmd
->plain
.message
.header
.request
.bodylen
);
304 execute(retry_write(cmd
, length
));
309 * Send a command to the server. The command header needs to be updated
310 * to network byte order
312 static enum test_return
send_packet(command
*cmd
)
314 /* Fix the byteorder of the header */
315 cmd
->plain
.message
.header
.request
.keylen
=
316 ntohs(cmd
->plain
.message
.header
.request
.keylen
);
317 cmd
->plain
.message
.header
.request
.bodylen
=
318 ntohl(cmd
->plain
.message
.header
.request
.bodylen
);
319 cmd
->plain
.message
.header
.request
.cas
=
320 memcached_ntohll(cmd
->plain
.message
.header
.request
.cas
);
322 execute(resend_packet(cmd
));
327 * Read a fixed length chunk of data from the server
329 static enum test_return
retry_read(void *buf
, size_t len
)
334 ssize_t nr
= timeout_io_op(sock
, POLLIN
, ((char*) buf
) + offset
, len
- offset
);
337 fprintf(stderr
, "Errno: %d %s\n", get_socket_errno(), strerror(errno
));
338 verify(get_socket_errno() == EINTR
|| get_socket_errno() == EAGAIN
);
347 } while (offset
< len
);
353 * Receive a response from the server and conver the fields in the header
354 * to local byte order
356 static enum test_return
recv_packet(response
*rsp
)
358 execute(retry_read(rsp
, sizeof(protocol_binary_response_no_extras
)));
360 /* Fix the byte order in the packet header */
361 rsp
->plain
.message
.header
.response
.keylen
=
362 ntohs(rsp
->plain
.message
.header
.response
.keylen
);
363 rsp
->plain
.message
.header
.response
.status
=
364 ntohs(rsp
->plain
.message
.header
.response
.status
);
365 rsp
->plain
.message
.header
.response
.bodylen
=
366 ntohl(rsp
->plain
.message
.header
.response
.bodylen
);
367 rsp
->plain
.message
.header
.response
.cas
=
368 memcached_ntohll(rsp
->plain
.message
.header
.response
.cas
);
370 size_t bodysz
= rsp
->plain
.message
.header
.response
.bodylen
;
372 execute(retry_read(rsp
->bytes
+ sizeof (protocol_binary_response_no_extras
), bodysz
));
378 * Create a storage command (add, set, replace etc)
380 * @param cmd destination buffer
381 * @param cc the storage command to create
382 * @param key the key to store
383 * @param keylen the length of the key
384 * @param dta the data to store with the key
385 * @param dtalen the length of the data to store with the key
386 * @param flags the flags to store along with the key
387 * @param exptime the expiry time for the key
389 static void storage_command(command
*cmd
,
398 /* all of the storage commands use the same command layout */
399 protocol_binary_request_set
*request
= &cmd
->set
;
401 memset(request
, 0, sizeof (*request
));
402 request
->message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
403 request
->message
.header
.request
.opcode
= cc
;
404 request
->message
.header
.request
.keylen
= (uint16_t)keylen
;
405 request
->message
.header
.request
.extlen
= 8;
406 request
->message
.header
.request
.bodylen
= (uint32_t)(keylen
+ 8 + dtalen
);
407 request
->message
.header
.request
.opaque
= 0xdeadbeef;
408 request
->message
.body
.flags
= flags
;
409 request
->message
.body
.expiration
= exptime
;
411 off_t key_offset
= sizeof (protocol_binary_request_no_extras
) + 8;
412 memcpy(cmd
->bytes
+ key_offset
, key
, keylen
);
414 memcpy(cmd
->bytes
+ key_offset
+ keylen
, dta
, dtalen
);
418 * Create a basic command to send to the server
419 * @param cmd destination buffer
420 * @param cc the command to create
421 * @param key the key to store
422 * @param keylen the length of the key
423 * @param dta the data to store with the key
424 * @param dtalen the length of the data to store with the key
426 static void raw_command(command
*cmd
,
433 /* all of the storage commands use the same command layout */
434 memset(cmd
, 0, sizeof (*cmd
));
435 cmd
->plain
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
436 cmd
->plain
.message
.header
.request
.opcode
= cc
;
437 cmd
->plain
.message
.header
.request
.keylen
= (uint16_t)keylen
;
438 cmd
->plain
.message
.header
.request
.bodylen
= (uint32_t)(keylen
+ dtalen
);
439 cmd
->plain
.message
.header
.request
.opaque
= 0xdeadbeef;
441 off_t key_offset
= sizeof (protocol_binary_request_no_extras
);
444 memcpy(cmd
->bytes
+ key_offset
, key
, keylen
);
447 memcpy(cmd
->bytes
+ key_offset
+ keylen
, dta
, dtalen
);
451 * Create the flush command
452 * @param cmd destination buffer
453 * @param cc the command to create (FLUSH/FLUSHQ)
454 * @param exptime when to flush
455 * @param use_extra to force using of the extra field?
457 static void flush_command(command
*cmd
,
458 uint8_t cc
, uint32_t exptime
, bool use_extra
)
460 memset(cmd
, 0, sizeof (cmd
->flush
));
461 cmd
->flush
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
462 cmd
->flush
.message
.header
.request
.opcode
= cc
;
463 cmd
->flush
.message
.header
.request
.opaque
= 0xdeadbeef;
465 if (exptime
!= 0 || use_extra
)
467 cmd
->flush
.message
.header
.request
.extlen
= 4;
468 cmd
->flush
.message
.body
.expiration
= htonl(exptime
);
469 cmd
->flush
.message
.header
.request
.bodylen
= 4;
474 * Create a incr/decr command
475 * @param cc the cmd to create (FLUSH/FLUSHQ)
476 * @param key the key to operate on
477 * @param keylen the number of bytes in the key
478 * @param delta the number to add/subtract
479 * @param initial the initial value if the key doesn't exist
480 * @param exptime when the key should expire if it isn't set
482 static void arithmetic_command(command
*cmd
,
490 memset(cmd
, 0, sizeof (cmd
->incr
));
491 cmd
->incr
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
492 cmd
->incr
.message
.header
.request
.opcode
= cc
;
493 cmd
->incr
.message
.header
.request
.keylen
= (uint16_t)keylen
;
494 cmd
->incr
.message
.header
.request
.extlen
= 20;
495 cmd
->incr
.message
.header
.request
.bodylen
= (uint32_t)(keylen
+ 20);
496 cmd
->incr
.message
.header
.request
.opaque
= 0xdeadbeef;
497 cmd
->incr
.message
.body
.delta
= memcached_htonll(delta
);
498 cmd
->incr
.message
.body
.initial
= memcached_htonll(initial
);
499 cmd
->incr
.message
.body
.expiration
= htonl(exptime
);
501 off_t key_offset
= sizeof (protocol_binary_request_no_extras
) + 20;
502 memcpy(cmd
->bytes
+ key_offset
, key
, keylen
);
506 * Validate the response header from the server
507 * @param rsp the response to check
508 * @param cc the expected command
509 * @param status the expected status
511 static enum test_return
do_validate_response_header(response
*rsp
,
512 uint8_t cc
, uint16_t status
)
514 verify(rsp
->plain
.message
.header
.response
.magic
== PROTOCOL_BINARY_RES
);
515 verify(rsp
->plain
.message
.header
.response
.opcode
== cc
);
516 verify(rsp
->plain
.message
.header
.response
.datatype
== PROTOCOL_BINARY_RAW_BYTES
);
517 verify(rsp
->plain
.message
.header
.response
.status
== status
);
518 verify(rsp
->plain
.message
.header
.response
.opaque
== 0xdeadbeef);
520 if (status
== PROTOCOL_BINARY_RESPONSE_SUCCESS
)
523 case PROTOCOL_BINARY_CMD_ADDQ
:
524 case PROTOCOL_BINARY_CMD_APPENDQ
:
525 case PROTOCOL_BINARY_CMD_DECREMENTQ
:
526 case PROTOCOL_BINARY_CMD_DELETEQ
:
527 case PROTOCOL_BINARY_CMD_FLUSHQ
:
528 case PROTOCOL_BINARY_CMD_INCREMENTQ
:
529 case PROTOCOL_BINARY_CMD_PREPENDQ
:
530 case PROTOCOL_BINARY_CMD_QUITQ
:
531 case PROTOCOL_BINARY_CMD_REPLACEQ
:
532 case PROTOCOL_BINARY_CMD_SETQ
:
533 verify("Quiet command shouldn't return on success" == NULL
);
539 case PROTOCOL_BINARY_CMD_ADD
:
540 case PROTOCOL_BINARY_CMD_REPLACE
:
541 case PROTOCOL_BINARY_CMD_SET
:
542 case PROTOCOL_BINARY_CMD_APPEND
:
543 case PROTOCOL_BINARY_CMD_PREPEND
:
544 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
545 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
546 verify(rsp
->plain
.message
.header
.response
.bodylen
== 0);
547 verify(rsp
->plain
.message
.header
.response
.cas
!= 0);
549 case PROTOCOL_BINARY_CMD_FLUSH
:
550 case PROTOCOL_BINARY_CMD_NOOP
:
551 case PROTOCOL_BINARY_CMD_QUIT
:
552 case PROTOCOL_BINARY_CMD_DELETE
:
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);
559 case PROTOCOL_BINARY_CMD_DECREMENT
:
560 case PROTOCOL_BINARY_CMD_INCREMENT
:
561 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
562 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
563 verify(rsp
->plain
.message
.header
.response
.bodylen
== 8);
564 verify(rsp
->plain
.message
.header
.response
.cas
!= 0);
567 case PROTOCOL_BINARY_CMD_STAT
:
568 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
569 /* key and value exists in all packets except in the terminating */
570 verify(rsp
->plain
.message
.header
.response
.cas
== 0);
573 case PROTOCOL_BINARY_CMD_VERSION
:
574 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
575 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
576 verify(rsp
->plain
.message
.header
.response
.bodylen
!= 0);
577 verify(rsp
->plain
.message
.header
.response
.cas
== 0);
580 case PROTOCOL_BINARY_CMD_GET
:
581 case PROTOCOL_BINARY_CMD_GETQ
:
582 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
583 verify(rsp
->plain
.message
.header
.response
.extlen
== 4);
584 verify(rsp
->plain
.message
.header
.response
.cas
!= 0);
587 case PROTOCOL_BINARY_CMD_GETK
:
588 case PROTOCOL_BINARY_CMD_GETKQ
:
589 verify(rsp
->plain
.message
.header
.response
.keylen
!= 0);
590 verify(rsp
->plain
.message
.header
.response
.extlen
== 4);
591 verify(rsp
->plain
.message
.header
.response
.cas
!= 0);
595 /* Undefined command code */
601 verify(rsp
->plain
.message
.header
.response
.cas
== 0);
602 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
603 if (cc
!= PROTOCOL_BINARY_CMD_GETK
)
605 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
612 /* We call verify(validate_response_header), but that macro
613 * expects a boolean expression, and the function returns
614 * an enum.... Let's just create a macro to avoid cluttering
615 * the code with all of the == TEST_PASS ;-)
617 #define validate_response_header(a,b,c) \
618 do_validate_response_header(a,b,c) == TEST_PASS
621 static enum test_return
send_binary_noop(void)
624 raw_command(&cmd
, PROTOCOL_BINARY_CMD_NOOP
, NULL
, 0, NULL
, 0);
625 execute(send_packet(&cmd
));
629 static enum test_return
receive_binary_noop(void)
632 execute(recv_packet(&rsp
));
633 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_NOOP
,
634 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
638 static enum test_return
test_binary_noop(void)
640 execute(send_binary_noop());
641 execute(receive_binary_noop());
645 static enum test_return
test_binary_quit_impl(uint8_t cc
)
649 raw_command(&cmd
, cc
, NULL
, 0, NULL
, 0);
651 execute(send_packet(&cmd
));
652 if (cc
== PROTOCOL_BINARY_CMD_QUIT
)
654 execute(recv_packet(&rsp
));
655 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_QUIT
,
656 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
659 /* Socket should be closed now, read should return EXIT_SUCCESS */
660 verify(timeout_io_op(sock
, POLLIN
, rsp
.bytes
, sizeof(rsp
.bytes
)) == 0);
662 return TEST_PASS_RECONNECT
;
665 static enum test_return
test_binary_quit(void)
667 return test_binary_quit_impl(PROTOCOL_BINARY_CMD_QUIT
);
670 static enum test_return
test_binary_quitq(void)
672 return test_binary_quit_impl(PROTOCOL_BINARY_CMD_QUITQ
);
675 static enum test_return
test_binary_set_impl(const char* key
, uint8_t cc
)
680 uint64_t value
= 0xdeadbeefdeadcafeULL
;
681 storage_command(&cmd
, cc
, key
, strlen(key
), &value
, sizeof (value
), 0, 0);
683 /* set should always work */
684 for (int ii
= 0; ii
< 10; ii
++)
688 execute(send_packet(&cmd
));
692 execute(resend_packet(&cmd
));
695 if (cc
== PROTOCOL_BINARY_CMD_SET
)
697 execute(recv_packet(&rsp
));
698 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
701 execute(test_binary_noop());
705 * We need to get the current CAS id, and at this time we haven't
706 * verified that we have a working get
708 if (cc
== PROTOCOL_BINARY_CMD_SETQ
)
710 cmd
.set
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_SET
;
711 execute(resend_packet(&cmd
));
712 execute(recv_packet(&rsp
));
713 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_SET
,
714 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
715 cmd
.set
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_SETQ
;
718 /* try to set with the correct CAS value */
719 cmd
.plain
.message
.header
.request
.cas
= memcached_htonll(rsp
.plain
.message
.header
.response
.cas
);
720 execute(resend_packet(&cmd
));
721 if (cc
== PROTOCOL_BINARY_CMD_SET
)
723 execute(recv_packet(&rsp
));
724 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
727 execute(test_binary_noop());
729 /* try to set with an incorrect CAS value */
730 cmd
.plain
.message
.header
.request
.cas
= memcached_htonll(rsp
.plain
.message
.header
.response
.cas
- 1);
731 execute(resend_packet(&cmd
));
732 execute(send_binary_noop());
733 execute(recv_packet(&rsp
));
734 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS
));
735 execute(receive_binary_noop());
740 static enum test_return
test_binary_set(void)
742 return test_binary_set_impl("test_binary_set", PROTOCOL_BINARY_CMD_SET
);
745 static enum test_return
test_binary_setq(void)
747 return test_binary_set_impl("test_binary_setq", PROTOCOL_BINARY_CMD_SETQ
);
750 static enum test_return
test_binary_add_impl(const char* key
, uint8_t cc
)
754 uint64_t value
= 0xdeadbeefdeadcafeULL
;
755 storage_command(&cmd
, cc
, key
, strlen(key
), &value
, sizeof (value
), 0, 0);
757 /* first add should work, rest of them should fail (even with cas
759 for (int ii
=0; ii
< 10; ii
++)
762 execute(send_packet(&cmd
));
764 execute(resend_packet(&cmd
));
766 if (cc
== PROTOCOL_BINARY_CMD_ADD
|| ii
> 0)
768 uint16_t expected_result
;
770 expected_result
= PROTOCOL_BINARY_RESPONSE_SUCCESS
;
772 expected_result
= PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS
;
774 execute(send_binary_noop());
775 execute(recv_packet(&rsp
));
776 execute(receive_binary_noop());
777 verify(validate_response_header(&rsp
, cc
, expected_result
));
780 execute(test_binary_noop());
786 static enum test_return
test_binary_add(void)
788 return test_binary_add_impl("test_binary_add", PROTOCOL_BINARY_CMD_ADD
);
791 static enum test_return
test_binary_addq(void)
793 return test_binary_add_impl("test_binary_addq", PROTOCOL_BINARY_CMD_ADDQ
);
796 static enum test_return
binary_set_item(const char *key
, const char *value
)
800 storage_command(&cmd
, PROTOCOL_BINARY_CMD_SET
, key
, strlen(key
),
801 value
, strlen(value
), 0, 0);
802 execute(send_packet(&cmd
));
803 execute(recv_packet(&rsp
));
804 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_SET
,
805 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
809 static enum test_return
test_binary_replace_impl(const char* key
, uint8_t cc
)
813 uint64_t value
= 0xdeadbeefdeadcafeULL
;
814 storage_command(&cmd
, cc
, key
, strlen(key
), &value
, sizeof (value
), 0, 0);
816 /* first replace should fail, successive should succeed (when the
818 for (int ii
= 0; ii
< 10; ii
++)
822 execute(send_packet(&cmd
));
826 execute(resend_packet(&cmd
));
829 if (cc
== PROTOCOL_BINARY_CMD_REPLACE
|| ii
== 0)
831 uint16_t expected_result
;
834 expected_result
=PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
;
838 expected_result
=PROTOCOL_BINARY_RESPONSE_SUCCESS
;
841 execute(send_binary_noop());
842 execute(recv_packet(&rsp
));
843 execute(receive_binary_noop());
844 verify(validate_response_header(&rsp
, cc
, expected_result
));
847 execute(binary_set_item(key
, key
));
851 execute(test_binary_noop());
855 /* verify that replace with CAS value works! */
856 cmd
.plain
.message
.header
.request
.cas
= memcached_htonll(rsp
.plain
.message
.header
.response
.cas
);
857 execute(resend_packet(&cmd
));
859 if (cc
== PROTOCOL_BINARY_CMD_REPLACE
)
861 execute(recv_packet(&rsp
));
862 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
865 execute(test_binary_noop());
867 /* try to set with an incorrect CAS value */
868 cmd
.plain
.message
.header
.request
.cas
= memcached_htonll(rsp
.plain
.message
.header
.response
.cas
- 1);
869 execute(resend_packet(&cmd
));
870 execute(send_binary_noop());
871 execute(recv_packet(&rsp
));
872 execute(receive_binary_noop());
873 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS
));
878 static enum test_return
test_binary_replace(void)
880 return test_binary_replace_impl("test_binary_replace", PROTOCOL_BINARY_CMD_REPLACE
);
883 static enum test_return
test_binary_replaceq(void)
885 return test_binary_replace_impl("test_binary_replaceq", PROTOCOL_BINARY_CMD_REPLACEQ
);
888 static enum test_return
test_binary_delete_impl(const char *key
, uint8_t cc
)
892 raw_command(&cmd
, cc
, key
, strlen(key
), NULL
, 0);
894 /* The delete shouldn't work the first time, because the item isn't there */
895 execute(send_packet(&cmd
));
896 execute(send_binary_noop());
897 execute(recv_packet(&rsp
));
898 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
));
899 execute(receive_binary_noop());
900 execute(binary_set_item(key
, key
));
902 /* The item should be present now, resend*/
903 execute(resend_packet(&cmd
));
904 if (cc
== PROTOCOL_BINARY_CMD_DELETE
)
906 execute(recv_packet(&rsp
));
907 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
910 execute(test_binary_noop());
915 static enum test_return
test_binary_delete(void)
917 return test_binary_delete_impl("test_binary_delete", PROTOCOL_BINARY_CMD_DELETE
);
920 static enum test_return
test_binary_deleteq(void)
922 return test_binary_delete_impl("test_binary_deleteq", PROTOCOL_BINARY_CMD_DELETEQ
);
925 static enum test_return
test_binary_get_impl(const char *key
, uint8_t cc
)
930 raw_command(&cmd
, cc
, key
, strlen(key
), NULL
, 0);
931 execute(send_packet(&cmd
));
932 execute(send_binary_noop());
934 if (cc
== PROTOCOL_BINARY_CMD_GET
|| cc
== PROTOCOL_BINARY_CMD_GETK
)
936 execute(recv_packet(&rsp
));
937 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
));
940 execute(receive_binary_noop());
942 execute(binary_set_item(key
, key
));
943 execute(resend_packet(&cmd
));
944 execute(send_binary_noop());
946 execute(recv_packet(&rsp
));
947 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
948 execute(receive_binary_noop());
953 static enum test_return
test_binary_get(void)
955 return test_binary_get_impl("test_binary_get", PROTOCOL_BINARY_CMD_GET
);
958 static enum test_return
test_binary_getk(void)
960 return test_binary_get_impl("test_binary_getk", PROTOCOL_BINARY_CMD_GETK
);
963 static enum test_return
test_binary_getq(void)
965 return test_binary_get_impl("test_binary_getq", PROTOCOL_BINARY_CMD_GETQ
);
968 static enum test_return
test_binary_getkq(void)
970 return test_binary_get_impl("test_binary_getkq", PROTOCOL_BINARY_CMD_GETKQ
);
973 static enum test_return
test_binary_incr_impl(const char* key
, uint8_t cc
)
977 arithmetic_command(&cmd
, cc
, key
, strlen(key
), 1, 0, 0);
980 for (ii
= 0; ii
< 10; ++ii
)
983 execute(send_packet(&cmd
));
985 execute(resend_packet(&cmd
));
987 if (cc
== PROTOCOL_BINARY_CMD_INCREMENT
)
989 execute(recv_packet(&rsp
));
990 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
991 verify(memcached_ntohll(rsp
.incr
.message
.body
.value
) == ii
);
994 execute(test_binary_noop());
997 /* @todo add incorrect CAS */
1001 static enum test_return
test_binary_incr(void)
1003 return test_binary_incr_impl("test_binary_incr", PROTOCOL_BINARY_CMD_INCREMENT
);
1006 static enum test_return
test_binary_incrq(void)
1008 return test_binary_incr_impl("test_binary_incrq", PROTOCOL_BINARY_CMD_INCREMENTQ
);
1011 static enum test_return
test_binary_decr_impl(const char* key
, uint8_t cc
)
1015 arithmetic_command(&cmd
, cc
, key
, strlen(key
), 1, 9, 0);
1018 for (ii
= 9; ii
> -1; --ii
)
1021 execute(send_packet(&cmd
));
1023 execute(resend_packet(&cmd
));
1025 if (cc
== PROTOCOL_BINARY_CMD_DECREMENT
)
1027 execute(recv_packet(&rsp
));
1028 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1029 verify(memcached_ntohll(rsp
.decr
.message
.body
.value
) == (uint64_t)ii
);
1032 execute(test_binary_noop());
1035 /* decr 0 should not wrap */
1036 execute(resend_packet(&cmd
));
1037 if (cc
== PROTOCOL_BINARY_CMD_DECREMENT
)
1039 execute(recv_packet(&rsp
));
1040 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1041 verify(memcached_ntohll(rsp
.decr
.message
.body
.value
) == 0);
1045 /* @todo get the value and verify! */
1049 /* @todo add incorrect cas */
1050 execute(test_binary_noop());
1054 static enum test_return
test_binary_decr(void)
1056 return test_binary_decr_impl("test_binary_decr",
1057 PROTOCOL_BINARY_CMD_DECREMENT
);
1060 static enum test_return
test_binary_decrq(void)
1062 return test_binary_decr_impl("test_binary_decrq",
1063 PROTOCOL_BINARY_CMD_DECREMENTQ
);
1066 static enum test_return
test_binary_version(void)
1070 raw_command(&cmd
, PROTOCOL_BINARY_CMD_VERSION
, NULL
, 0, NULL
, 0);
1072 execute(send_packet(&cmd
));
1073 execute(recv_packet(&rsp
));
1074 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_VERSION
,
1075 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1080 static enum test_return
test_binary_flush_impl(const char *key
, uint8_t cc
)
1085 for (int ii
= 0; ii
< 2; ++ii
)
1087 execute(binary_set_item(key
, key
));
1088 flush_command(&cmd
, cc
, 0, ii
== 0);
1089 execute(send_packet(&cmd
));
1091 if (cc
== PROTOCOL_BINARY_CMD_FLUSH
)
1093 execute(recv_packet(&rsp
));
1094 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1097 execute(test_binary_noop());
1099 raw_command(&cmd
, PROTOCOL_BINARY_CMD_GET
, key
, strlen(key
), NULL
, 0);
1100 execute(send_packet(&cmd
));
1101 execute(recv_packet(&rsp
));
1102 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_GET
,
1103 PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
));
1109 static enum test_return
test_binary_flush(void)
1111 return test_binary_flush_impl("test_binary_flush", PROTOCOL_BINARY_CMD_FLUSH
);
1114 static enum test_return
test_binary_flushq(void)
1116 return test_binary_flush_impl("test_binary_flushq", PROTOCOL_BINARY_CMD_FLUSHQ
);
1119 static enum test_return
test_binary_concat_impl(const char *key
, uint8_t cc
)
1125 if (cc
== PROTOCOL_BINARY_CMD_APPEND
|| cc
== PROTOCOL_BINARY_CMD_APPENDQ
)
1134 execute(binary_set_item(key
, value
));
1136 if (cc
== PROTOCOL_BINARY_CMD_APPEND
|| cc
== PROTOCOL_BINARY_CMD_APPENDQ
)
1145 raw_command(&cmd
, cc
, key
, strlen(key
), value
, strlen(value
));
1146 execute(send_packet(&cmd
));
1147 if (cc
== PROTOCOL_BINARY_CMD_APPEND
|| cc
== PROTOCOL_BINARY_CMD_PREPEND
)
1149 execute(recv_packet(&rsp
));
1150 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1154 execute(test_binary_noop());
1157 raw_command(&cmd
, PROTOCOL_BINARY_CMD_GET
, key
, strlen(key
), NULL
, 0);
1158 execute(send_packet(&cmd
));
1159 execute(recv_packet(&rsp
));
1160 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_GET
,
1161 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1162 verify(rsp
.plain
.message
.header
.response
.bodylen
- 4 == 11);
1163 verify(memcmp(rsp
.bytes
+ 28, "hello world", 11) == 0);
1168 static enum test_return
test_binary_append(void)
1170 return test_binary_concat_impl("test_binary_append", PROTOCOL_BINARY_CMD_APPEND
);
1173 static enum test_return
test_binary_prepend(void)
1175 return test_binary_concat_impl("test_binary_prepend", PROTOCOL_BINARY_CMD_PREPEND
);
1178 static enum test_return
test_binary_appendq(void)
1180 return test_binary_concat_impl("test_binary_appendq", PROTOCOL_BINARY_CMD_APPENDQ
);
1183 static enum test_return
test_binary_prependq(void)
1185 return test_binary_concat_impl("test_binary_prependq", PROTOCOL_BINARY_CMD_PREPENDQ
);
1188 static enum test_return
test_binary_stat(void)
1193 raw_command(&cmd
, PROTOCOL_BINARY_CMD_STAT
, NULL
, 0, NULL
, 0);
1194 execute(send_packet(&cmd
));
1198 execute(recv_packet(&rsp
));
1199 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_STAT
,
1200 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1201 } while (rsp
.plain
.message
.header
.response
.keylen
!= 0);
1206 static enum test_return
send_string(const char *cmd
)
1208 execute(retry_write(cmd
, strlen(cmd
)));
1212 static enum test_return
receive_line(char *buffer
, size_t size
)
1215 while (offset
< size
)
1217 execute(retry_read(buffer
+ offset
, 1));
1218 if (buffer
[offset
] == '\n')
1220 if (offset
+ 1 < size
)
1222 buffer
[offset
+ 1]= '\0';
1234 static enum test_return
receive_response(const char *msg
) {
1236 execute(receive_line(buffer
, sizeof(buffer
)));
1237 if (strcmp(msg
, buffer
) != 0) {
1238 fprintf(stderr
, "[%s]\n", buffer
);
1240 verify(strcmp(msg
, buffer
) == 0);
1244 static enum test_return
receive_error_response(void)
1247 execute(receive_line(buffer
, sizeof(buffer
)));
1248 verify(strncmp(buffer
, "ERROR", 5) == 0 ||
1249 strncmp(buffer
, "CLIENT_ERROR", 12) == 0 ||
1250 strncmp(buffer
, "SERVER_ERROR", 12) == 0);
1254 static enum test_return
test_ascii_quit(void)
1256 /* Verify that quit handles unknown options */
1257 execute(send_string("quit foo bar\r\n"));
1258 execute(receive_error_response());
1260 /* quit doesn't support noreply */
1261 execute(send_string("quit noreply\r\n"));
1262 execute(receive_error_response());
1264 /* Verify that quit works */
1265 execute(send_string("quit\r\n"));
1267 /* Socket should be closed now, read should return EXIT_SUCCESS */
1269 verify(timeout_io_op(sock
, POLLIN
, buffer
, sizeof(buffer
)) == 0);
1270 return TEST_PASS_RECONNECT
;
1274 static enum test_return
test_ascii_version(void)
1276 /* Verify that version command handles unknown options */
1277 execute(send_string("version foo bar\r\n"));
1278 execute(receive_error_response());
1280 /* version doesn't support noreply */
1281 execute(send_string("version noreply\r\n"));
1282 execute(receive_error_response());
1284 /* Verify that verify works */
1285 execute(send_string("version\r\n"));
1287 execute(receive_line(buffer
, sizeof(buffer
)));
1288 verify(strncmp(buffer
, "VERSION ", 8) == 0);
1293 static enum test_return
test_ascii_verbosity(void)
1295 /* This command does not adhere to the spec! */
1296 execute(send_string("verbosity foo bar my\r\n"));
1297 execute(receive_error_response());
1299 execute(send_string("verbosity noreply\r\n"));
1300 execute(receive_error_response());
1302 execute(send_string("verbosity 0 noreply\r\n"));
1303 execute(test_ascii_version());
1305 execute(send_string("verbosity\r\n"));
1306 execute(receive_error_response());
1308 execute(send_string("verbosity 1\r\n"));
1309 execute(receive_response("OK\r\n"));
1311 execute(send_string("verbosity 0\r\n"));
1312 execute(receive_response("OK\r\n"));
1319 static enum test_return
test_ascii_set_impl(const char* key
, bool noreply
)
1321 /* @todo add tests for bogus format! */
1323 snprintf(buffer
, sizeof(buffer
), "set %s 0 0 5%s\r\nvalue\r\n", key
, noreply
? " noreply" : "");
1324 execute(send_string(buffer
));
1328 execute(receive_response("STORED\r\n"));
1331 return test_ascii_version();
1334 static enum test_return
test_ascii_set(void)
1336 return test_ascii_set_impl("test_ascii_set", false);
1339 static enum test_return
test_ascii_set_noreply(void)
1341 return test_ascii_set_impl("test_ascii_set_noreply", true);
1344 static enum test_return
test_ascii_add_impl(const char* key
, bool noreply
)
1346 /* @todo add tests for bogus format! */
1348 snprintf(buffer
, sizeof(buffer
), "add %s 0 0 5%s\r\nvalue\r\n", key
, noreply
? " noreply" : "");
1349 execute(send_string(buffer
));
1353 execute(receive_response("STORED\r\n"));
1356 execute(send_string(buffer
));
1360 execute(receive_response("NOT_STORED\r\n"));
1363 return test_ascii_version();
1366 static enum test_return
test_ascii_add(void)
1368 return test_ascii_add_impl("test_ascii_add", false);
1371 static enum test_return
test_ascii_add_noreply(void)
1373 return test_ascii_add_impl("test_ascii_add_noreply", true);
1376 static enum test_return
ascii_get_unknown_value(char **key
, char **value
, ssize_t
*ndata
)
1380 execute(receive_line(buffer
, sizeof(buffer
)));
1381 verify(strncmp(buffer
, "VALUE ", 6) == 0);
1382 char *end
= strchr(buffer
+ 6, ' ');
1383 verify(end
!= NULL
);
1388 *key
= strdup(buffer
+ 6);
1389 verify(*key
!= NULL
);
1392 unsigned long val
= strtoul(ptr
, &end
, 10); /* flags */
1395 verify(end
!= NULL
);
1396 *ndata
= (ssize_t
)strtoul(end
, &end
, 10); /* size */
1398 verify(end
!= NULL
);
1399 while (end
and *end
!= '\n' and isspace(*end
))
1401 verify(end
and *end
== '\n');
1403 *value
= static_cast<char*>(malloc((size_t)*ndata
));
1404 verify(*value
!= NULL
);
1406 execute(retry_read(*value
, (size_t)*ndata
));
1408 execute(retry_read(buffer
, 2));
1409 verify(memcmp(buffer
, "\r\n", 2) == 0);
1414 static enum test_return
ascii_get_value(const char *key
, const char *value
)
1418 size_t datasize
= strlen(value
);
1420 verify(datasize
< sizeof(buffer
));
1421 execute(receive_line(buffer
, sizeof(buffer
)));
1422 verify(strncmp(buffer
, "VALUE ", 6) == 0);
1423 verify(strncmp(buffer
+ 6, key
, strlen(key
)) == 0);
1424 char *ptr
= buffer
+ 6 + strlen(key
) + 1;
1427 unsigned long val
= strtoul(ptr
, &end
, 10); /* flags */
1430 verify(end
!= NULL
);
1431 val
= strtoul(end
, &end
, 10); /* size */
1433 verify(val
== datasize
);
1434 verify(end
!= NULL
);
1435 while (end
and *end
!= '\n' and isspace(*end
))
1439 verify(end
and *end
== '\n');
1441 execute(retry_read(buffer
, datasize
));
1442 verify(memcmp(buffer
, value
, datasize
) == 0);
1444 execute(retry_read(buffer
, 2));
1445 verify(memcmp(buffer
, "\r\n", 2) == 0);
1450 static enum test_return
ascii_get_item(const char *key
, const char *value
,
1457 datasize
= strlen(value
);
1460 verify(datasize
< sizeof(buffer
));
1461 snprintf(buffer
, sizeof(buffer
), "get %s\r\n", key
);
1462 execute(send_string(buffer
));
1466 execute(ascii_get_value(key
, value
));
1469 execute(retry_read(buffer
, 5));
1470 verify(memcmp(buffer
, "END\r\n", 5) == 0);
1475 static enum test_return
ascii_gets_value(const char *key
, const char *value
,
1480 size_t datasize
= strlen(value
);
1482 verify(datasize
< sizeof(buffer
));
1483 execute(receive_line(buffer
, sizeof(buffer
)));
1484 verify(strncmp(buffer
, "VALUE ", 6) == 0);
1485 verify(strncmp(buffer
+ 6, key
, strlen(key
)) == 0);
1486 char *ptr
= buffer
+ 6 + strlen(key
) + 1;
1489 unsigned long val
= strtoul(ptr
, &end
, 10); /* flags */
1492 verify(end
!= NULL
);
1493 val
= strtoul(end
, &end
, 10); /* size */
1495 verify(val
== datasize
);
1496 verify(end
!= NULL
);
1497 *cas
= strtoul(end
, &end
, 10); /* cas */
1499 verify(val
== datasize
);
1500 verify(end
!= NULL
);
1502 while (end
and *end
!= '\n' and isspace(*end
))
1506 verify(end
and *end
== '\n');
1508 execute(retry_read(buffer
, datasize
));
1509 verify(memcmp(buffer
, value
, datasize
) == 0);
1511 execute(retry_read(buffer
, 2));
1512 verify(memcmp(buffer
, "\r\n", 2) == 0);
1517 static enum test_return
ascii_gets_item(const char *key
, const char *value
,
1518 bool exist
, unsigned long *cas
)
1524 datasize
= strlen(value
);
1527 verify(datasize
< sizeof(buffer
));
1528 snprintf(buffer
, sizeof(buffer
), "gets %s\r\n", key
);
1529 execute(send_string(buffer
));
1532 execute(ascii_gets_value(key
, value
, cas
));
1534 execute(retry_read(buffer
, 5));
1535 verify(memcmp(buffer
, "END\r\n", 5) == 0);
1540 static enum test_return
ascii_set_item(const char *key
, const char *value
)
1543 size_t len
= strlen(value
);
1544 snprintf(buffer
, sizeof(buffer
), "set %s 0 0 %u\r\n", key
, (unsigned int)len
);
1545 execute(send_string(buffer
));
1546 execute(retry_write(value
, len
));
1547 execute(send_string("\r\n"));
1548 execute(receive_response("STORED\r\n"));
1552 static enum test_return
test_ascii_replace_impl(const char* key
, bool noreply
)
1555 snprintf(buffer
, sizeof(buffer
), "replace %s 0 0 5%s\r\nvalue\r\n", key
, noreply
? " noreply" : "");
1556 execute(send_string(buffer
));
1560 execute(test_ascii_version());
1564 execute(receive_response("NOT_STORED\r\n"));
1567 execute(ascii_set_item(key
, "value"));
1568 execute(ascii_get_item(key
, "value", true));
1571 execute(send_string(buffer
));
1574 execute(test_ascii_version());
1576 execute(receive_response("STORED\r\n"));
1578 return test_ascii_version();
1581 static enum test_return
test_ascii_replace(void)
1583 return test_ascii_replace_impl("test_ascii_replace", false);
1586 static enum test_return
test_ascii_replace_noreply(void)
1588 return test_ascii_replace_impl("test_ascii_replace_noreply", true);
1591 static enum test_return
test_ascii_cas_impl(const char* key
, bool noreply
)
1596 execute(ascii_set_item(key
, "value"));
1597 execute(ascii_gets_item(key
, "value", true, &cas
));
1599 snprintf(buffer
, sizeof(buffer
), "cas %s 0 0 6 %lu%s\r\nvalue2\r\n", key
, cas
, noreply
? " noreply" : "");
1600 execute(send_string(buffer
));
1604 execute(test_ascii_version());
1608 execute(receive_response("STORED\r\n"));
1611 /* reexecute the same command should fail due to illegal cas */
1612 execute(send_string(buffer
));
1616 execute(test_ascii_version());
1620 execute(receive_response("EXISTS\r\n"));
1623 return test_ascii_version();
1626 static enum test_return
test_ascii_cas(void)
1628 return test_ascii_cas_impl("test_ascii_cas", false);
1631 static enum test_return
test_ascii_cas_noreply(void)
1633 return test_ascii_cas_impl("test_ascii_cas_noreply", true);
1636 static enum test_return
test_ascii_delete_impl(const char *key
, bool noreply
)
1638 execute(ascii_set_item(key
, "value"));
1640 execute(send_string("delete\r\n"));
1641 execute(receive_error_response());
1642 /* BUG: the server accepts delete a b */
1643 execute(send_string("delete a b c d e\r\n"));
1644 execute(receive_error_response());
1647 snprintf(buffer
, sizeof(buffer
), "delete %s%s\r\n", key
, noreply
? " noreply" : "");
1648 execute(send_string(buffer
));
1651 execute(test_ascii_version());
1653 execute(receive_response("DELETED\r\n"));
1655 execute(ascii_get_item(key
, "value", false));
1656 execute(send_string(buffer
));
1658 execute(test_ascii_version());
1660 execute(receive_response("NOT_FOUND\r\n"));
1665 static enum test_return
test_ascii_delete(void)
1667 return test_ascii_delete_impl("test_ascii_delete", false);
1670 static enum test_return
test_ascii_delete_noreply(void)
1672 return test_ascii_delete_impl("test_ascii_delete_noreply", true);
1675 static enum test_return
test_ascii_get(void)
1677 execute(ascii_set_item("test_ascii_get", "value"));
1679 execute(send_string("get\r\n"));
1680 execute(receive_error_response());
1681 execute(ascii_get_item("test_ascii_get", "value", true));
1682 execute(ascii_get_item("test_ascii_get_notfound", "value", false));
1687 static enum test_return
test_ascii_gets(void)
1689 execute(ascii_set_item("test_ascii_gets", "value"));
1691 execute(send_string("gets\r\n"));
1692 execute(receive_error_response());
1694 execute(ascii_gets_item("test_ascii_gets", "value", true, &cas
));
1695 execute(ascii_gets_item("test_ascii_gets_notfound", "value", false, &cas
));
1700 static enum test_return
test_ascii_mget(void)
1702 const uint32_t nkeys
= 5;
1703 const char * const keys
[]= {
1706 /* test_ascii_mget_3 does not exist :) */
1712 for (uint32_t x
= 0; x
< nkeys
; ++x
)
1714 execute(ascii_set_item(keys
[x
], "value"));
1717 /* Ask for a key that doesn't exist as well */
1718 execute(send_string("get test_ascii_mget1 test_ascii_mget2 test_ascii_mget3 "
1719 "test_ascii_mget4 test_ascii_mget5 "
1720 "test_ascii_mget6\r\n"));
1722 std::vector
<char *> returned
;
1723 returned
.resize(nkeys
);
1725 for (uint32_t x
= 0; x
< nkeys
; ++x
)
1729 execute(ascii_get_unknown_value(&returned
[x
], &v
, &nbytes
));
1730 verify(nbytes
== 5);
1731 verify(memcmp(v
, "value", 5) == 0);
1736 execute(retry_read(buffer
, 5));
1737 verify(memcmp(buffer
, "END\r\n", 5) == 0);
1739 /* verify that we got all the keys we expected */
1740 for (uint32_t x
= 0; x
< nkeys
; ++x
)
1743 for (uint32_t y
= 0; y
< nkeys
; ++y
)
1745 if (strcmp(keys
[x
], returned
[y
]) == 0)
1754 for (uint32_t x
= 0; x
< nkeys
; ++x
)
1762 static enum test_return
test_ascii_incr_impl(const char* key
, bool noreply
)
1765 snprintf(cmd
, sizeof(cmd
), "incr %s 1%s\r\n", key
, noreply
? " noreply" : "");
1767 execute(ascii_set_item(key
, "0"));
1768 for (int x
= 1; x
< 11; ++x
)
1770 execute(send_string(cmd
));
1773 execute(test_ascii_version());
1777 execute(receive_line(buffer
, sizeof(buffer
)));
1778 int val
= atoi(buffer
);
1783 execute(ascii_get_item(key
, "10", true));
1788 static enum test_return
test_ascii_incr(void)
1790 return test_ascii_incr_impl("test_ascii_incr", false);
1793 static enum test_return
test_ascii_incr_noreply(void)
1795 return test_ascii_incr_impl("test_ascii_incr_noreply", true);
1798 static enum test_return
test_ascii_decr_impl(const char* key
, bool noreply
)
1801 snprintf(cmd
, sizeof(cmd
), "decr %s 1%s\r\n", key
, noreply
? " noreply" : "");
1803 execute(ascii_set_item(key
, "9"));
1804 for (int x
= 8; x
> -1; --x
)
1806 execute(send_string(cmd
));
1810 execute(test_ascii_version());
1815 execute(receive_line(buffer
, sizeof(buffer
)));
1816 int val
= atoi(buffer
);
1821 execute(ascii_get_item(key
, "0", true));
1823 /* verify that it doesn't wrap */
1824 execute(send_string(cmd
));
1827 execute(test_ascii_version());
1832 execute(receive_line(buffer
, sizeof(buffer
)));
1834 execute(ascii_get_item(key
, "0", true));
1839 static enum test_return
test_ascii_decr(void)
1841 return test_ascii_decr_impl("test_ascii_decr", false);
1844 static enum test_return
test_ascii_decr_noreply(void)
1846 return test_ascii_decr_impl("test_ascii_decr_noreply", true);
1850 static enum test_return
test_ascii_flush_impl(const char *key
, bool noreply
)
1853 /* Verify that the flush_all command handles unknown options */
1854 /* Bug in the current memcached server! */
1855 execute(send_string("flush_all foo bar\r\n"));
1856 execute(receive_error_response());
1859 execute(ascii_set_item(key
, key
));
1860 execute(ascii_get_item(key
, key
, true));
1864 execute(send_string("flush_all noreply\r\n"));
1865 execute(test_ascii_version());
1869 execute(send_string("flush_all\r\n"));
1870 execute(receive_response("OK\r\n"));
1873 execute(ascii_get_item(key
, key
, false));
1878 static enum test_return
test_ascii_flush(void)
1880 return test_ascii_flush_impl("test_ascii_flush", false);
1883 static enum test_return
test_ascii_flush_noreply(void)
1885 return test_ascii_flush_impl("test_ascii_flush_noreply", true);
1888 static enum test_return
test_ascii_concat_impl(const char *key
,
1899 execute(ascii_set_item(key
, value
));
1911 snprintf(cmd
, sizeof(cmd
), "%s %s 0 0 %u%s\r\n%s\r\n",
1912 append
? "append" : "prepend",
1913 key
, (unsigned int)strlen(value
), noreply
? " noreply" : "",
1915 execute(send_string(cmd
));
1919 execute(test_ascii_version());
1923 execute(receive_response("STORED\r\n"));
1926 execute(ascii_get_item(key
, "hello world", true));
1928 snprintf(cmd
, sizeof(cmd
), "%s %s_notfound 0 0 %u%s\r\n%s\r\n",
1929 append
? "append" : "prepend",
1930 key
, (unsigned int)strlen(value
), noreply
? " noreply" : "",
1932 execute(send_string(cmd
));
1936 execute(test_ascii_version());
1940 execute(receive_response("NOT_STORED\r\n"));
1946 static enum test_return
test_ascii_append(void)
1948 return test_ascii_concat_impl("test_ascii_append", true, false);
1951 static enum test_return
test_ascii_prepend(void)
1953 return test_ascii_concat_impl("test_ascii_prepend", false, false);
1956 static enum test_return
test_ascii_append_noreply(void)
1958 return test_ascii_concat_impl("test_ascii_append_noreply", true, true);
1961 static enum test_return
test_ascii_prepend_noreply(void)
1963 return test_ascii_concat_impl("test_ascii_prepend_noreply", false, true);
1966 static enum test_return
test_ascii_stat(void)
1968 execute(send_string("stats noreply\r\n"));
1969 execute(receive_error_response());
1970 execute(send_string("stats\r\n"));
1973 execute(receive_line(buffer
, sizeof(buffer
)));
1974 } while (strcmp(buffer
, "END\r\n") != 0);
1976 return TEST_PASS_RECONNECT
;
1979 typedef enum test_return(*TEST_FUNC
)(void);
1983 const char *description
;
1987 struct testcase testcases
[]= {
1988 { "ascii quit", test_ascii_quit
},
1989 { "ascii version", test_ascii_version
},
1990 { "ascii verbosity", test_ascii_verbosity
},
1991 { "ascii set", test_ascii_set
},
1992 { "ascii set noreply", test_ascii_set_noreply
},
1993 { "ascii get", test_ascii_get
},
1994 { "ascii gets", test_ascii_gets
},
1995 { "ascii mget", test_ascii_mget
},
1996 { "ascii flush", test_ascii_flush
},
1997 { "ascii flush noreply", test_ascii_flush_noreply
},
1998 { "ascii add", test_ascii_add
},
1999 { "ascii add noreply", test_ascii_add_noreply
},
2000 { "ascii replace", test_ascii_replace
},
2001 { "ascii replace noreply", test_ascii_replace_noreply
},
2002 { "ascii cas", test_ascii_cas
},
2003 { "ascii cas noreply", test_ascii_cas_noreply
},
2004 { "ascii delete", test_ascii_delete
},
2005 { "ascii delete noreply", test_ascii_delete_noreply
},
2006 { "ascii incr", test_ascii_incr
},
2007 { "ascii incr noreply", test_ascii_incr_noreply
},
2008 { "ascii decr", test_ascii_decr
},
2009 { "ascii decr noreply", test_ascii_decr_noreply
},
2010 { "ascii append", test_ascii_append
},
2011 { "ascii append noreply", test_ascii_append_noreply
},
2012 { "ascii prepend", test_ascii_prepend
},
2013 { "ascii prepend noreply", test_ascii_prepend_noreply
},
2014 { "ascii stat", test_ascii_stat
},
2015 { "binary noop", test_binary_noop
},
2016 { "binary quit", test_binary_quit
},
2017 { "binary quitq", test_binary_quitq
},
2018 { "binary set", test_binary_set
},
2019 { "binary setq", test_binary_setq
},
2020 { "binary flush", test_binary_flush
},
2021 { "binary flushq", test_binary_flushq
},
2022 { "binary add", test_binary_add
},
2023 { "binary addq", test_binary_addq
},
2024 { "binary replace", test_binary_replace
},
2025 { "binary replaceq", test_binary_replaceq
},
2026 { "binary delete", test_binary_delete
},
2027 { "binary deleteq", test_binary_deleteq
},
2028 { "binary get", test_binary_get
},
2029 { "binary getq", test_binary_getq
},
2030 { "binary getk", test_binary_getk
},
2031 { "binary getkq", test_binary_getkq
},
2032 { "binary incr", test_binary_incr
},
2033 { "binary incrq", test_binary_incrq
},
2034 { "binary decr", test_binary_decr
},
2035 { "binary decrq", test_binary_decrq
},
2036 { "binary version", test_binary_version
},
2037 { "binary append", test_binary_append
},
2038 { "binary appendq", test_binary_appendq
},
2039 { "binary prepend", test_binary_prepend
},
2040 { "binary prependq", test_binary_prependq
},
2041 { "binary stat", test_binary_stat
},
2045 const int ascii_tests
= 1;
2046 const int binary_tests
= 2;
2054 int main(int argc
, char **argv
)
2056 static const char * const status_msg
[]= {"[skip]", "[pass]", "[pass]", "[FAIL]"};
2057 struct test_type_st tests
= { true, true };
2060 const char *hostname
= "localhost";
2061 const char *port
= "11211";
2064 const char *testname
= NULL
;
2068 while ((cmd
= getopt(argc
, argv
, "qt:vch:p:PT:?ab")) != EOF
)
2073 tests
.binary
= false;
2082 timeout
= atoi(optarg
);
2085 fprintf(stderr
, "Invalid timeout. Please specify a number for -t\n");
2086 return EXIT_FAILURE
;
2090 case 'v': verbose
= true;
2093 case 'c': do_core
= true;
2096 case 'h': hostname
= optarg
;
2099 case 'p': port
= optarg
;
2106 case 'P': prompt
= true;
2109 case 'T': testname
= optarg
;
2113 fprintf(stderr
, "Usage: %s [-h hostname] [-p port] [-c] [-v] [-t n] [-P] [-T testname]'\n"
2114 "\t-c\tGenerate coredump if a test fails\n"
2115 "\t-v\tVerbose test output (print out the assertion)\n"
2116 "\t-t n\tSet the timeout for io-operations to n seconds\n"
2117 "\t-P\tPrompt the user before starting a test.\n"
2118 "\t\t\t\"skip\" will skip the test\n"
2119 "\t\t\t\"quit\" will terminate memcapable\n"
2120 "\t\t\tEverything else will start the test\n"
2121 "\t-T n\tJust run the test named n\n"
2122 "\t-a\tOnly test the ascii protocol\n"
2123 "\t-b\tOnly test the binary protocol\n",
2125 return EXIT_SUCCESS
;
2129 initialize_sockets();
2130 sock
= connect_server(hostname
, port
);
2131 if (sock
== INVALID_SOCKET
)
2133 fprintf(stderr
, "Failed to connect to <%s:%s>: %s\n",
2134 hostname
, port
, strerror(get_socket_errno()));
2135 return EXIT_FAILURE
;
2138 for (int ii
= 0; testcases
[ii
].description
!= NULL
; ++ii
)
2140 if (testname
!= NULL
&& strcmp(testcases
[ii
].description
, testname
) != 0)
2145 if ((testcases
[ii
].description
[0] == 'a' && (tests
.ascii
) == 0) ||
2146 (testcases
[ii
].description
[0] == 'b' && (tests
.binary
) == 0))
2151 fprintf(stdout
, "%-40s", testcases
[ii
].description
);
2156 fprintf(stdout
, "\nPress <return> when you are ready? ");
2157 char buffer
[80] = {0};
2158 if (fgets(buffer
, sizeof(buffer
), stdin
) != NULL
) {
2159 if (strncmp(buffer
, "skip", 4) == 0)
2161 fprintf(stdout
, "%-40s%s\n", testcases
[ii
].description
,
2162 status_msg
[TEST_SKIP
]);
2166 if (strncmp(buffer
, "quit", 4) == 0)
2172 fprintf(stdout
, "%-40s", testcases
[ii
].description
);
2176 bool reconnect
= false;
2177 enum test_return ret
= testcases
[ii
].function();
2178 if (ret
== TEST_FAIL
)
2183 fprintf(stderr
, "\n");
2185 else if (ret
== TEST_PASS_RECONNECT
)
2188 fprintf(stderr
, "%s\n", status_msg
[ret
]);
2192 if ((sock
= connect_server(hostname
, port
)) == INVALID_SOCKET
)
2194 fprintf(stderr
, "Failed to connect to <%s:%s>: %s\n", hostname
, port
, strerror(get_socket_errno()));
2195 fprintf(stderr
, "%d of %d tests failed\n", failed
, total
);
2196 return EXIT_FAILURE
;
2204 fprintf(stdout
, "All tests passed\n");
2208 fprintf(stderr
, "%d of %d tests failed\n", failed
, total
);
2211 return (failed
== 0) ? EXIT_SUCCESS
: EXIT_FAILURE
;