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
);
1393 unsigned long val
= strtoul(ptr
, &end
, 10); /* flags */
1397 verify(end
!= NULL
);
1399 *ndata
= (ssize_t
)strtoul(end
, &end
, 10); /* size */
1402 verify(end
!= NULL
);
1403 while (end
and *end
!= '\n' and isspace(*end
))
1405 verify(end
and *end
== '\n');
1407 *value
= static_cast<char*>(malloc((size_t)*ndata
));
1408 verify(*value
!= NULL
);
1410 execute(retry_read(*value
, (size_t)*ndata
));
1412 execute(retry_read(buffer
, 2));
1413 verify(memcmp(buffer
, "\r\n", 2) == 0);
1418 static enum test_return
ascii_get_value(const char *key
, const char *value
)
1422 size_t datasize
= strlen(value
);
1424 verify(datasize
< sizeof(buffer
));
1425 execute(receive_line(buffer
, sizeof(buffer
)));
1426 verify(strncmp(buffer
, "VALUE ", 6) == 0);
1427 verify(strncmp(buffer
+ 6, key
, strlen(key
)) == 0);
1428 char *ptr
= buffer
+ 6 + strlen(key
) + 1;
1432 unsigned long val
= strtoul(ptr
, &end
, 10); /* flags */
1436 verify(end
!= NULL
);
1439 val
= strtoul(end
, &end
, 10); /* size */
1442 verify(val
== datasize
);
1443 verify(end
!= NULL
);
1444 while (end
and *end
!= '\n' and isspace(*end
))
1448 verify(end
and *end
== '\n');
1450 execute(retry_read(buffer
, datasize
));
1451 verify(memcmp(buffer
, value
, datasize
) == 0);
1453 execute(retry_read(buffer
, 2));
1454 verify(memcmp(buffer
, "\r\n", 2) == 0);
1459 static enum test_return
ascii_get_item(const char *key
, const char *value
,
1466 datasize
= strlen(value
);
1469 verify(datasize
< sizeof(buffer
));
1470 snprintf(buffer
, sizeof(buffer
), "get %s\r\n", key
);
1471 execute(send_string(buffer
));
1475 execute(ascii_get_value(key
, value
));
1478 execute(retry_read(buffer
, 5));
1479 verify(memcmp(buffer
, "END\r\n", 5) == 0);
1484 static enum test_return
ascii_gets_value(const char *key
, const char *value
,
1489 size_t datasize
= strlen(value
);
1491 verify(datasize
< sizeof(buffer
));
1492 execute(receive_line(buffer
, sizeof(buffer
)));
1493 verify(strncmp(buffer
, "VALUE ", 6) == 0);
1494 verify(strncmp(buffer
+ 6, key
, strlen(key
)) == 0);
1495 char *ptr
= buffer
+ 6 + strlen(key
) + 1;
1499 unsigned long val
= strtoul(ptr
, &end
, 10); /* flags */
1503 verify(end
!= NULL
);
1506 val
= strtoul(end
, &end
, 10); /* size */
1509 verify(val
== datasize
);
1510 verify(end
!= NULL
);
1513 *cas
= strtoul(end
, &end
, 10); /* cas */
1516 verify(val
== datasize
);
1517 verify(end
!= NULL
);
1519 while (end
and *end
!= '\n' and isspace(*end
))
1523 verify(end
and *end
== '\n');
1525 execute(retry_read(buffer
, datasize
));
1526 verify(memcmp(buffer
, value
, datasize
) == 0);
1528 execute(retry_read(buffer
, 2));
1529 verify(memcmp(buffer
, "\r\n", 2) == 0);
1534 static enum test_return
ascii_gets_item(const char *key
, const char *value
,
1535 bool exist
, unsigned long *cas
)
1541 datasize
= strlen(value
);
1544 verify(datasize
< sizeof(buffer
));
1545 snprintf(buffer
, sizeof(buffer
), "gets %s\r\n", key
);
1546 execute(send_string(buffer
));
1549 execute(ascii_gets_value(key
, value
, cas
));
1551 execute(retry_read(buffer
, 5));
1552 verify(memcmp(buffer
, "END\r\n", 5) == 0);
1557 static enum test_return
ascii_set_item(const char *key
, const char *value
)
1560 size_t len
= strlen(value
);
1561 snprintf(buffer
, sizeof(buffer
), "set %s 0 0 %u\r\n", key
, (unsigned int)len
);
1562 execute(send_string(buffer
));
1563 execute(retry_write(value
, len
));
1564 execute(send_string("\r\n"));
1565 execute(receive_response("STORED\r\n"));
1569 static enum test_return
test_ascii_replace_impl(const char* key
, bool noreply
)
1572 snprintf(buffer
, sizeof(buffer
), "replace %s 0 0 5%s\r\nvalue\r\n", key
, noreply
? " noreply" : "");
1573 execute(send_string(buffer
));
1577 execute(test_ascii_version());
1581 execute(receive_response("NOT_STORED\r\n"));
1584 execute(ascii_set_item(key
, "value"));
1585 execute(ascii_get_item(key
, "value", true));
1588 execute(send_string(buffer
));
1591 execute(test_ascii_version());
1593 execute(receive_response("STORED\r\n"));
1595 return test_ascii_version();
1598 static enum test_return
test_ascii_replace(void)
1600 return test_ascii_replace_impl("test_ascii_replace", false);
1603 static enum test_return
test_ascii_replace_noreply(void)
1605 return test_ascii_replace_impl("test_ascii_replace_noreply", true);
1608 static enum test_return
test_ascii_cas_impl(const char* key
, bool noreply
)
1613 execute(ascii_set_item(key
, "value"));
1614 execute(ascii_gets_item(key
, "value", true, &cas
));
1616 snprintf(buffer
, sizeof(buffer
), "cas %s 0 0 6 %lu%s\r\nvalue2\r\n", key
, cas
, noreply
? " noreply" : "");
1617 execute(send_string(buffer
));
1621 execute(test_ascii_version());
1625 execute(receive_response("STORED\r\n"));
1628 /* reexecute the same command should fail due to illegal cas */
1629 execute(send_string(buffer
));
1633 execute(test_ascii_version());
1637 execute(receive_response("EXISTS\r\n"));
1640 return test_ascii_version();
1643 static enum test_return
test_ascii_cas(void)
1645 return test_ascii_cas_impl("test_ascii_cas", false);
1648 static enum test_return
test_ascii_cas_noreply(void)
1650 return test_ascii_cas_impl("test_ascii_cas_noreply", true);
1653 static enum test_return
test_ascii_delete_impl(const char *key
, bool noreply
)
1655 execute(ascii_set_item(key
, "value"));
1657 execute(send_string("delete\r\n"));
1658 execute(receive_error_response());
1659 /* BUG: the server accepts delete a b */
1660 execute(send_string("delete a b c d e\r\n"));
1661 execute(receive_error_response());
1664 snprintf(buffer
, sizeof(buffer
), "delete %s%s\r\n", key
, noreply
? " noreply" : "");
1665 execute(send_string(buffer
));
1668 execute(test_ascii_version());
1670 execute(receive_response("DELETED\r\n"));
1672 execute(ascii_get_item(key
, "value", false));
1673 execute(send_string(buffer
));
1675 execute(test_ascii_version());
1677 execute(receive_response("NOT_FOUND\r\n"));
1682 static enum test_return
test_ascii_delete(void)
1684 return test_ascii_delete_impl("test_ascii_delete", false);
1687 static enum test_return
test_ascii_delete_noreply(void)
1689 return test_ascii_delete_impl("test_ascii_delete_noreply", true);
1692 static enum test_return
test_ascii_get(void)
1694 execute(ascii_set_item("test_ascii_get", "value"));
1696 execute(send_string("get\r\n"));
1697 execute(receive_error_response());
1698 execute(ascii_get_item("test_ascii_get", "value", true));
1699 execute(ascii_get_item("test_ascii_get_notfound", "value", false));
1704 static enum test_return
test_ascii_gets(void)
1706 execute(ascii_set_item("test_ascii_gets", "value"));
1708 execute(send_string("gets\r\n"));
1709 execute(receive_error_response());
1711 execute(ascii_gets_item("test_ascii_gets", "value", true, &cas
));
1712 execute(ascii_gets_item("test_ascii_gets_notfound", "value", false, &cas
));
1717 static enum test_return
test_ascii_mget(void)
1719 const uint32_t nkeys
= 5;
1720 const char * const keys
[]= {
1723 /* test_ascii_mget_3 does not exist :) */
1729 for (uint32_t x
= 0; x
< nkeys
; ++x
)
1731 execute(ascii_set_item(keys
[x
], "value"));
1734 /* Ask for a key that doesn't exist as well */
1735 execute(send_string("get test_ascii_mget1 test_ascii_mget2 test_ascii_mget3 "
1736 "test_ascii_mget4 test_ascii_mget5 "
1737 "test_ascii_mget6\r\n"));
1739 std::vector
<char *> returned
;
1740 returned
.resize(nkeys
);
1742 for (uint32_t x
= 0; x
< nkeys
; ++x
)
1746 execute(ascii_get_unknown_value(&returned
[x
], &v
, &nbytes
));
1747 verify(nbytes
== 5);
1748 verify(memcmp(v
, "value", 5) == 0);
1753 execute(retry_read(buffer
, 5));
1754 verify(memcmp(buffer
, "END\r\n", 5) == 0);
1756 /* verify that we got all the keys we expected */
1757 for (uint32_t x
= 0; x
< nkeys
; ++x
)
1760 for (uint32_t y
= 0; y
< nkeys
; ++y
)
1762 if (strcmp(keys
[x
], returned
[y
]) == 0)
1771 for (uint32_t x
= 0; x
< nkeys
; ++x
)
1779 static enum test_return
test_ascii_incr_impl(const char* key
, bool noreply
)
1782 snprintf(cmd
, sizeof(cmd
), "incr %s 1%s\r\n", key
, noreply
? " noreply" : "");
1784 execute(ascii_set_item(key
, "0"));
1785 for (int x
= 1; x
< 11; ++x
)
1787 execute(send_string(cmd
));
1790 execute(test_ascii_version());
1794 execute(receive_line(buffer
, sizeof(buffer
)));
1795 int val
= atoi(buffer
);
1800 execute(ascii_get_item(key
, "10", true));
1805 static enum test_return
test_ascii_incr(void)
1807 return test_ascii_incr_impl("test_ascii_incr", false);
1810 static enum test_return
test_ascii_incr_noreply(void)
1812 return test_ascii_incr_impl("test_ascii_incr_noreply", true);
1815 static enum test_return
test_ascii_decr_impl(const char* key
, bool noreply
)
1818 snprintf(cmd
, sizeof(cmd
), "decr %s 1%s\r\n", key
, noreply
? " noreply" : "");
1820 execute(ascii_set_item(key
, "9"));
1821 for (int x
= 8; x
> -1; --x
)
1823 execute(send_string(cmd
));
1827 execute(test_ascii_version());
1832 execute(receive_line(buffer
, sizeof(buffer
)));
1833 int val
= atoi(buffer
);
1838 execute(ascii_get_item(key
, "0", true));
1840 /* verify that it doesn't wrap */
1841 execute(send_string(cmd
));
1844 execute(test_ascii_version());
1849 execute(receive_line(buffer
, sizeof(buffer
)));
1851 execute(ascii_get_item(key
, "0", true));
1856 static enum test_return
test_ascii_decr(void)
1858 return test_ascii_decr_impl("test_ascii_decr", false);
1861 static enum test_return
test_ascii_decr_noreply(void)
1863 return test_ascii_decr_impl("test_ascii_decr_noreply", true);
1867 static enum test_return
test_ascii_flush_impl(const char *key
, bool noreply
)
1870 /* Verify that the flush_all command handles unknown options */
1871 /* Bug in the current memcached server! */
1872 execute(send_string("flush_all foo bar\r\n"));
1873 execute(receive_error_response());
1876 execute(ascii_set_item(key
, key
));
1877 execute(ascii_get_item(key
, key
, true));
1881 execute(send_string("flush_all noreply\r\n"));
1882 execute(test_ascii_version());
1886 execute(send_string("flush_all\r\n"));
1887 execute(receive_response("OK\r\n"));
1890 execute(ascii_get_item(key
, key
, false));
1895 static enum test_return
test_ascii_flush(void)
1897 return test_ascii_flush_impl("test_ascii_flush", false);
1900 static enum test_return
test_ascii_flush_noreply(void)
1902 return test_ascii_flush_impl("test_ascii_flush_noreply", true);
1905 static enum test_return
test_ascii_concat_impl(const char *key
,
1916 execute(ascii_set_item(key
, value
));
1928 snprintf(cmd
, sizeof(cmd
), "%s %s 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("STORED\r\n"));
1943 execute(ascii_get_item(key
, "hello world", true));
1945 snprintf(cmd
, sizeof(cmd
), "%s %s_notfound 0 0 %u%s\r\n%s\r\n",
1946 append
? "append" : "prepend",
1947 key
, (unsigned int)strlen(value
), noreply
? " noreply" : "",
1949 execute(send_string(cmd
));
1953 execute(test_ascii_version());
1957 execute(receive_response("NOT_STORED\r\n"));
1963 static enum test_return
test_ascii_append(void)
1965 return test_ascii_concat_impl("test_ascii_append", true, false);
1968 static enum test_return
test_ascii_prepend(void)
1970 return test_ascii_concat_impl("test_ascii_prepend", false, false);
1973 static enum test_return
test_ascii_append_noreply(void)
1975 return test_ascii_concat_impl("test_ascii_append_noreply", true, true);
1978 static enum test_return
test_ascii_prepend_noreply(void)
1980 return test_ascii_concat_impl("test_ascii_prepend_noreply", false, true);
1983 static enum test_return
test_ascii_stat(void)
1985 execute(send_string("stats noreply\r\n"));
1986 execute(receive_error_response());
1987 execute(send_string("stats\r\n"));
1990 execute(receive_line(buffer
, sizeof(buffer
)));
1991 } while (strcmp(buffer
, "END\r\n") != 0);
1993 return TEST_PASS_RECONNECT
;
1996 typedef enum test_return(*TEST_FUNC
)(void);
2000 const char *description
;
2004 struct testcase testcases
[]= {
2005 { "ascii quit", test_ascii_quit
},
2006 { "ascii version", test_ascii_version
},
2007 { "ascii verbosity", test_ascii_verbosity
},
2008 { "ascii set", test_ascii_set
},
2009 { "ascii set noreply", test_ascii_set_noreply
},
2010 { "ascii get", test_ascii_get
},
2011 { "ascii gets", test_ascii_gets
},
2012 { "ascii mget", test_ascii_mget
},
2013 { "ascii flush", test_ascii_flush
},
2014 { "ascii flush noreply", test_ascii_flush_noreply
},
2015 { "ascii add", test_ascii_add
},
2016 { "ascii add noreply", test_ascii_add_noreply
},
2017 { "ascii replace", test_ascii_replace
},
2018 { "ascii replace noreply", test_ascii_replace_noreply
},
2019 { "ascii cas", test_ascii_cas
},
2020 { "ascii cas noreply", test_ascii_cas_noreply
},
2021 { "ascii delete", test_ascii_delete
},
2022 { "ascii delete noreply", test_ascii_delete_noreply
},
2023 { "ascii incr", test_ascii_incr
},
2024 { "ascii incr noreply", test_ascii_incr_noreply
},
2025 { "ascii decr", test_ascii_decr
},
2026 { "ascii decr noreply", test_ascii_decr_noreply
},
2027 { "ascii append", test_ascii_append
},
2028 { "ascii append noreply", test_ascii_append_noreply
},
2029 { "ascii prepend", test_ascii_prepend
},
2030 { "ascii prepend noreply", test_ascii_prepend_noreply
},
2031 { "ascii stat", test_ascii_stat
},
2032 { "binary noop", test_binary_noop
},
2033 { "binary quit", test_binary_quit
},
2034 { "binary quitq", test_binary_quitq
},
2035 { "binary set", test_binary_set
},
2036 { "binary setq", test_binary_setq
},
2037 { "binary flush", test_binary_flush
},
2038 { "binary flushq", test_binary_flushq
},
2039 { "binary add", test_binary_add
},
2040 { "binary addq", test_binary_addq
},
2041 { "binary replace", test_binary_replace
},
2042 { "binary replaceq", test_binary_replaceq
},
2043 { "binary delete", test_binary_delete
},
2044 { "binary deleteq", test_binary_deleteq
},
2045 { "binary get", test_binary_get
},
2046 { "binary getq", test_binary_getq
},
2047 { "binary getk", test_binary_getk
},
2048 { "binary getkq", test_binary_getkq
},
2049 { "binary incr", test_binary_incr
},
2050 { "binary incrq", test_binary_incrq
},
2051 { "binary decr", test_binary_decr
},
2052 { "binary decrq", test_binary_decrq
},
2053 { "binary version", test_binary_version
},
2054 { "binary append", test_binary_append
},
2055 { "binary appendq", test_binary_appendq
},
2056 { "binary prepend", test_binary_prepend
},
2057 { "binary prependq", test_binary_prependq
},
2058 { "binary stat", test_binary_stat
},
2062 const int ascii_tests
= 1;
2063 const int binary_tests
= 2;
2071 int main(int argc
, char **argv
)
2073 static const char * const status_msg
[]= {"[skip]", "[pass]", "[pass]", "[FAIL]"};
2074 struct test_type_st tests
= { true, true };
2077 const char *hostname
= "localhost";
2078 const char *port
= "11211";
2081 const char *testname
= NULL
;
2085 while ((cmd
= getopt(argc
, argv
, "qt:vch:p:PT:?ab")) != EOF
)
2090 tests
.binary
= false;
2099 timeout
= atoi(optarg
);
2102 fprintf(stderr
, "Invalid timeout. Please specify a number for -t\n");
2103 return EXIT_FAILURE
;
2107 case 'v': verbose
= true;
2110 case 'c': do_core
= true;
2113 case 'h': hostname
= optarg
;
2116 case 'p': port
= optarg
;
2123 case 'P': prompt
= true;
2126 case 'T': testname
= optarg
;
2130 fprintf(stderr
, "Usage: %s [-h hostname] [-p port] [-c] [-v] [-t n] [-P] [-T testname]'\n"
2131 "\t-c\tGenerate coredump if a test fails\n"
2132 "\t-v\tVerbose test output (print out the assertion)\n"
2133 "\t-t n\tSet the timeout for io-operations to n seconds\n"
2134 "\t-P\tPrompt the user before starting a test.\n"
2135 "\t\t\t\"skip\" will skip the test\n"
2136 "\t\t\t\"quit\" will terminate memcapable\n"
2137 "\t\t\tEverything else will start the test\n"
2138 "\t-T n\tJust run the test named n\n"
2139 "\t-a\tOnly test the ascii protocol\n"
2140 "\t-b\tOnly test the binary protocol\n",
2142 return EXIT_SUCCESS
;
2146 initialize_sockets();
2147 sock
= connect_server(hostname
, port
);
2148 if (sock
== INVALID_SOCKET
)
2150 fprintf(stderr
, "Failed to connect to <%s:%s>: %s\n",
2151 hostname
, port
, strerror(get_socket_errno()));
2152 return EXIT_FAILURE
;
2155 for (int ii
= 0; testcases
[ii
].description
!= NULL
; ++ii
)
2157 if (testname
!= NULL
&& strcmp(testcases
[ii
].description
, testname
) != 0)
2162 if ((testcases
[ii
].description
[0] == 'a' && (tests
.ascii
) == 0) ||
2163 (testcases
[ii
].description
[0] == 'b' && (tests
.binary
) == 0))
2168 fprintf(stdout
, "%-40s", testcases
[ii
].description
);
2173 fprintf(stdout
, "\nPress <return> when you are ready? ");
2174 char buffer
[80] = {0};
2175 if (fgets(buffer
, sizeof(buffer
), stdin
) != NULL
) {
2176 if (strncmp(buffer
, "skip", 4) == 0)
2178 fprintf(stdout
, "%-40s%s\n", testcases
[ii
].description
,
2179 status_msg
[TEST_SKIP
]);
2183 if (strncmp(buffer
, "quit", 4) == 0)
2189 fprintf(stdout
, "%-40s", testcases
[ii
].description
);
2193 bool reconnect
= false;
2194 enum test_return ret
= testcases
[ii
].function();
2195 if (ret
== TEST_FAIL
)
2200 fprintf(stderr
, "\n");
2202 else if (ret
== TEST_PASS_RECONNECT
)
2205 fprintf(stderr
, "%s\n", status_msg
[ret
]);
2209 if ((sock
= connect_server(hostname
, port
)) == INVALID_SOCKET
)
2211 fprintf(stderr
, "Failed to connect to <%s:%s>: %s\n", hostname
, port
, strerror(get_socket_errno()));
2212 fprintf(stderr
, "%d of %d tests failed\n", failed
, total
);
2213 return EXIT_FAILURE
;
2221 fprintf(stdout
, "All tests passed\n");
2225 fprintf(stderr
, "%d of %d tests failed\n", failed
, total
);
2228 return (failed
== 0) ? EXIT_SUCCESS
: EXIT_FAILURE
;