2 * Copyright (C) 2006-2009 Brian Aker
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
12 /* -*- Mode: C; tab-width: 2; c-basic-offset: 2; indent-tabs-mode: nil -*- */
20 #include "poll/poll.h"
33 #include <sys/types.h>
36 #include <libmemcached-1.0/memcached.h>
37 #include <libmemcached/socket.hpp>
38 #include <libmemcached/memcached/protocol_binary.h>
39 #include <libmemcached/byteorder.h>
40 #include <clients/utilities.h>
45 /* /usr/include/netinet/in.h defines macros from ntohs() to _bswap_nn to
46 * optimize the conversion functions, but the prototypes generate warnings
47 * from gcc. The conversion methods isn't the bottleneck for my app, so
48 * just remove the warnings by undef'ing the optimization ..
54 /* Should we generate coredumps when we enounter an error (-c) */
55 static bool do_core
= false;
56 /* connection to the server */
57 static memcached_socket_t sock
;
58 /* Should the output from test failures be verbose or quiet? */
59 static bool verbose
= false;
61 /* The number of seconds to wait for an IO-operation */
62 static int timeout
= 2;
65 * Instead of having to cast between the different datatypes we create
66 * a union of all of the different types of pacages we want to send.
67 * A lot of the different commands use the same packet layout, so I'll
68 * just define the different types I need. The typedefs only contain
69 * the header of the message, so we need some space for keys and body
70 * To avoid to have to do multiple writes, lets add a chunk of memory
71 * to use. 1k should be more than enough for header, key and body.
75 protocol_binary_request_no_extras plain
;
76 protocol_binary_request_flush flush
;
77 protocol_binary_request_incr incr
;
78 protocol_binary_request_set set
;
84 protocol_binary_response_no_extras plain
;
85 protocol_binary_response_incr incr
;
86 protocol_binary_response_decr decr
;
92 TEST_SKIP
, TEST_PASS
, TEST_PASS_RECONNECT
, TEST_FAIL
96 * Try to get an addrinfo struct for a given port on a given host
98 static struct addrinfo
*lookuphost(const char *hostname
, const char *port
)
100 struct addrinfo
*ai
= 0;
101 struct addrinfo hints
;
102 memset(&hints
, 0, sizeof(struct addrinfo
));
103 hints
.ai_family
=AF_UNSPEC
;
104 hints
.ai_protocol
=IPPROTO_TCP
;
105 hints
.ai_socktype
=SOCK_STREAM
;
107 int error
= getaddrinfo(hostname
, port
, &hints
, &ai
);
110 if (error
!= EAI_SYSTEM
)
111 fprintf(stderr
, "getaddrinfo(): %s\n", gai_strerror(error
));
113 perror("getaddrinfo()");
120 * Set the socket in nonblocking mode
121 * @return -1 if failure, the socket otherwise
123 static memcached_socket_t
set_noblock(void)
127 if (ioctlsocket(sock
, FIONBIO
, &arg
) == SOCKET_ERROR
)
129 perror("Failed to set nonblocking io");
131 return INVALID_SOCKET
;
134 int flags
= fcntl(sock
, F_GETFL
, 0);
137 perror("Failed to get socket flags");
138 memcached_close_socket(sock
);
139 return INVALID_SOCKET
;
142 if ((flags
& O_NONBLOCK
) != O_NONBLOCK
)
144 if (fcntl(sock
, F_SETFL
, flags
| O_NONBLOCK
) == -1)
146 perror("Failed to set socket to nonblocking mode");
147 memcached_close_socket(sock
);
148 return INVALID_SOCKET
;
156 * Try to open a connection to the server
157 * @param hostname the name of the server to connect to
158 * @param port the port number (or service) to connect to
159 * @return positive integer if success, -1 otherwise
161 static memcached_socket_t
connect_server(const char *hostname
, const char *port
)
163 struct addrinfo
*ai
= lookuphost(hostname
, port
);
164 sock
= INVALID_SOCKET
;
167 if ((sock
= socket(ai
->ai_family
, ai
->ai_socktype
,
168 ai
->ai_protocol
)) != INVALID_SOCKET
)
170 if (connect(sock
, ai
->ai_addr
, ai
->ai_addrlen
) == SOCKET_ERROR
)
172 fprintf(stderr
, "Failed to connect socket: %s\n",
173 strerror(get_socket_errno()));
175 sock
= INVALID_SOCKET
;
183 fprintf(stderr
, "Failed to create socket: %s\n",
184 strerror(get_socket_errno()));
192 static ssize_t
timeout_io_op(memcached_socket_t fd
, short direction
, void *buf
, size_t len
)
196 if (direction
== POLLOUT
)
198 ret
= send(fd
, buf
, len
, 0);
202 ret
= recv(fd
, buf
, len
, 0);
205 if (ret
== SOCKET_ERROR
&& get_socket_errno() == EWOULDBLOCK
)
208 memset(&fds
, 0, sizeof(struct pollfd
));
209 fds
.events
= direction
;
212 int err
= poll(&fds
, 1, timeout
* 1000);
215 if (direction
== POLLOUT
)
217 ret
= send(fd
, buf
, len
, 0);
221 ret
= recv(fd
, buf
, len
, 0);
230 perror("Failed to poll");
239 * Ensure that an expression is true. If it isn't print out a message similar
240 * to assert() and create a coredump if the user wants that. If not an error
241 * message is returned.
244 static enum test_return
ensure(bool val
, const char *expression
, const char *file
, int line
)
250 fprintf(stderr
, "\n%s:%d: %s", file
, line
, expression
);
264 #define verify(expression) do { if (ensure(expression, #expression, __FILE__, __LINE__) == TEST_FAIL) return TEST_FAIL; } while (0)
265 #define execute(expression) do { if (ensure(expression == TEST_PASS, #expression, __FILE__, __LINE__) == TEST_FAIL) return TEST_FAIL; } while (0)
268 * Send a chunk of memory over the socket (retry if the call is iterrupted
270 static enum test_return
retry_write(const void* buf
, size_t len
)
273 const char* ptr
= static_cast<const char *>(buf
);
277 size_t num_bytes
= len
- offset
;
278 ssize_t nw
= timeout_io_op(sock
, POLLOUT
, (void*)(ptr
+ offset
), num_bytes
);
281 verify(get_socket_errno() == EINTR
|| get_socket_errno() == EAGAIN
);
288 } while (offset
< len
);
294 * Resend a packet to the server (All fields in the command header should
295 * be in network byte order)
297 static enum test_return
resend_packet(command
*cmd
)
299 size_t length
= sizeof (protocol_binary_request_no_extras
) +
300 ntohl(cmd
->plain
.message
.header
.request
.bodylen
);
302 execute(retry_write(cmd
, length
));
307 * Send a command to the server. The command header needs to be updated
308 * to network byte order
310 static enum test_return
send_packet(command
*cmd
)
312 /* Fix the byteorder of the header */
313 cmd
->plain
.message
.header
.request
.keylen
=
314 ntohs(cmd
->plain
.message
.header
.request
.keylen
);
315 cmd
->plain
.message
.header
.request
.bodylen
=
316 ntohl(cmd
->plain
.message
.header
.request
.bodylen
);
317 cmd
->plain
.message
.header
.request
.cas
=
318 memcached_ntohll(cmd
->plain
.message
.header
.request
.cas
);
320 execute(resend_packet(cmd
));
325 * Read a fixed length chunk of data from the server
327 static enum test_return
retry_read(void *buf
, size_t len
)
332 ssize_t nr
= timeout_io_op(sock
, POLLIN
, ((char*) buf
) + offset
, len
- offset
);
335 fprintf(stderr
, "Errno: %d %s\n", get_socket_errno(), strerror(errno
));
336 verify(get_socket_errno() == EINTR
|| get_socket_errno() == EAGAIN
);
345 } while (offset
< len
);
351 * Receive a response from the server and conver the fields in the header
352 * to local byte order
354 static enum test_return
recv_packet(response
*rsp
)
356 execute(retry_read(rsp
, sizeof(protocol_binary_response_no_extras
)));
358 /* Fix the byte order in the packet header */
359 rsp
->plain
.message
.header
.response
.keylen
=
360 ntohs(rsp
->plain
.message
.header
.response
.keylen
);
361 rsp
->plain
.message
.header
.response
.status
=
362 ntohs(rsp
->plain
.message
.header
.response
.status
);
363 rsp
->plain
.message
.header
.response
.bodylen
=
364 ntohl(rsp
->plain
.message
.header
.response
.bodylen
);
365 rsp
->plain
.message
.header
.response
.cas
=
366 memcached_ntohll(rsp
->plain
.message
.header
.response
.cas
);
368 size_t bodysz
= rsp
->plain
.message
.header
.response
.bodylen
;
370 execute(retry_read(rsp
->bytes
+ sizeof (protocol_binary_response_no_extras
), bodysz
));
376 * Create a storage command (add, set, replace etc)
378 * @param cmd destination buffer
379 * @param cc the storage command to create
380 * @param key the key to store
381 * @param keylen the length of the key
382 * @param dta the data to store with the key
383 * @param dtalen the length of the data to store with the key
384 * @param flags the flags to store along with the key
385 * @param exptime the expiry time for the key
387 static void storage_command(command
*cmd
,
396 /* all of the storage commands use the same command layout */
397 protocol_binary_request_set
*request
= &cmd
->set
;
399 memset(request
, 0, sizeof (*request
));
400 request
->message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
401 request
->message
.header
.request
.opcode
= cc
;
402 request
->message
.header
.request
.keylen
= (uint16_t)keylen
;
403 request
->message
.header
.request
.extlen
= 8;
404 request
->message
.header
.request
.bodylen
= (uint32_t)(keylen
+ 8 + dtalen
);
405 request
->message
.header
.request
.opaque
= 0xdeadbeef;
406 request
->message
.body
.flags
= flags
;
407 request
->message
.body
.expiration
= exptime
;
409 off_t key_offset
= sizeof (protocol_binary_request_no_extras
) + 8;
410 memcpy(cmd
->bytes
+ key_offset
, key
, keylen
);
412 memcpy(cmd
->bytes
+ key_offset
+ keylen
, dta
, dtalen
);
416 * Create a basic command to send to the server
417 * @param cmd destination buffer
418 * @param cc the command to create
419 * @param key the key to store
420 * @param keylen the length of the key
421 * @param dta the data to store with the key
422 * @param dtalen the length of the data to store with the key
424 static void raw_command(command
*cmd
,
431 /* all of the storage commands use the same command layout */
432 memset(cmd
, 0, sizeof (*cmd
));
433 cmd
->plain
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
434 cmd
->plain
.message
.header
.request
.opcode
= cc
;
435 cmd
->plain
.message
.header
.request
.keylen
= (uint16_t)keylen
;
436 cmd
->plain
.message
.header
.request
.bodylen
= (uint32_t)(keylen
+ dtalen
);
437 cmd
->plain
.message
.header
.request
.opaque
= 0xdeadbeef;
439 off_t key_offset
= sizeof (protocol_binary_request_no_extras
);
442 memcpy(cmd
->bytes
+ key_offset
, key
, keylen
);
445 memcpy(cmd
->bytes
+ key_offset
+ keylen
, dta
, dtalen
);
449 * Create the flush command
450 * @param cmd destination buffer
451 * @param cc the command to create (FLUSH/FLUSHQ)
452 * @param exptime when to flush
453 * @param use_extra to force using of the extra field?
455 static void flush_command(command
*cmd
,
456 uint8_t cc
, uint32_t exptime
, bool use_extra
)
458 memset(cmd
, 0, sizeof (cmd
->flush
));
459 cmd
->flush
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
460 cmd
->flush
.message
.header
.request
.opcode
= cc
;
461 cmd
->flush
.message
.header
.request
.opaque
= 0xdeadbeef;
463 if (exptime
!= 0 || use_extra
)
465 cmd
->flush
.message
.header
.request
.extlen
= 4;
466 cmd
->flush
.message
.body
.expiration
= htonl(exptime
);
467 cmd
->flush
.message
.header
.request
.bodylen
= 4;
472 * Create a incr/decr command
473 * @param cc the cmd to create (FLUSH/FLUSHQ)
474 * @param key the key to operate on
475 * @param keylen the number of bytes in the key
476 * @param delta the number to add/subtract
477 * @param initial the initial value if the key doesn't exist
478 * @param exptime when the key should expire if it isn't set
480 static void arithmetic_command(command
*cmd
,
488 memset(cmd
, 0, sizeof (cmd
->incr
));
489 cmd
->incr
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
490 cmd
->incr
.message
.header
.request
.opcode
= cc
;
491 cmd
->incr
.message
.header
.request
.keylen
= (uint16_t)keylen
;
492 cmd
->incr
.message
.header
.request
.extlen
= 20;
493 cmd
->incr
.message
.header
.request
.bodylen
= (uint32_t)(keylen
+ 20);
494 cmd
->incr
.message
.header
.request
.opaque
= 0xdeadbeef;
495 cmd
->incr
.message
.body
.delta
= memcached_htonll(delta
);
496 cmd
->incr
.message
.body
.initial
= memcached_htonll(initial
);
497 cmd
->incr
.message
.body
.expiration
= htonl(exptime
);
499 off_t key_offset
= sizeof (protocol_binary_request_no_extras
) + 20;
500 memcpy(cmd
->bytes
+ key_offset
, key
, keylen
);
504 * Validate the response header from the server
505 * @param rsp the response to check
506 * @param cc the expected command
507 * @param status the expected status
509 static enum test_return
do_validate_response_header(response
*rsp
,
510 uint8_t cc
, uint16_t status
)
512 verify(rsp
->plain
.message
.header
.response
.magic
== PROTOCOL_BINARY_RES
);
513 verify(rsp
->plain
.message
.header
.response
.opcode
== cc
);
514 verify(rsp
->plain
.message
.header
.response
.datatype
== PROTOCOL_BINARY_RAW_BYTES
);
515 verify(rsp
->plain
.message
.header
.response
.status
== status
);
516 verify(rsp
->plain
.message
.header
.response
.opaque
== 0xdeadbeef);
518 if (status
== PROTOCOL_BINARY_RESPONSE_SUCCESS
)
521 case PROTOCOL_BINARY_CMD_ADDQ
:
522 case PROTOCOL_BINARY_CMD_APPENDQ
:
523 case PROTOCOL_BINARY_CMD_DECREMENTQ
:
524 case PROTOCOL_BINARY_CMD_DELETEQ
:
525 case PROTOCOL_BINARY_CMD_FLUSHQ
:
526 case PROTOCOL_BINARY_CMD_INCREMENTQ
:
527 case PROTOCOL_BINARY_CMD_PREPENDQ
:
528 case PROTOCOL_BINARY_CMD_QUITQ
:
529 case PROTOCOL_BINARY_CMD_REPLACEQ
:
530 case PROTOCOL_BINARY_CMD_SETQ
:
531 verify("Quiet command shouldn't return on success" == NULL
);
537 case PROTOCOL_BINARY_CMD_ADD
:
538 case PROTOCOL_BINARY_CMD_REPLACE
:
539 case PROTOCOL_BINARY_CMD_SET
:
540 case PROTOCOL_BINARY_CMD_APPEND
:
541 case PROTOCOL_BINARY_CMD_PREPEND
:
542 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
543 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
544 verify(rsp
->plain
.message
.header
.response
.bodylen
== 0);
545 verify(rsp
->plain
.message
.header
.response
.cas
!= 0);
547 case PROTOCOL_BINARY_CMD_FLUSH
:
548 case PROTOCOL_BINARY_CMD_NOOP
:
549 case PROTOCOL_BINARY_CMD_QUIT
:
550 case PROTOCOL_BINARY_CMD_DELETE
:
551 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
552 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
553 verify(rsp
->plain
.message
.header
.response
.bodylen
== 0);
554 verify(rsp
->plain
.message
.header
.response
.cas
== 0);
557 case PROTOCOL_BINARY_CMD_DECREMENT
:
558 case PROTOCOL_BINARY_CMD_INCREMENT
:
559 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
560 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
561 verify(rsp
->plain
.message
.header
.response
.bodylen
== 8);
562 verify(rsp
->plain
.message
.header
.response
.cas
!= 0);
565 case PROTOCOL_BINARY_CMD_STAT
:
566 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
567 /* key and value exists in all packets except in the terminating */
568 verify(rsp
->plain
.message
.header
.response
.cas
== 0);
571 case PROTOCOL_BINARY_CMD_VERSION
:
572 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
573 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
574 verify(rsp
->plain
.message
.header
.response
.bodylen
!= 0);
575 verify(rsp
->plain
.message
.header
.response
.cas
== 0);
578 case PROTOCOL_BINARY_CMD_GET
:
579 case PROTOCOL_BINARY_CMD_GETQ
:
580 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
581 verify(rsp
->plain
.message
.header
.response
.extlen
== 4);
582 verify(rsp
->plain
.message
.header
.response
.cas
!= 0);
585 case PROTOCOL_BINARY_CMD_GETK
:
586 case PROTOCOL_BINARY_CMD_GETKQ
:
587 verify(rsp
->plain
.message
.header
.response
.keylen
!= 0);
588 verify(rsp
->plain
.message
.header
.response
.extlen
== 4);
589 verify(rsp
->plain
.message
.header
.response
.cas
!= 0);
593 /* Undefined command code */
599 verify(rsp
->plain
.message
.header
.response
.cas
== 0);
600 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
601 if (cc
!= PROTOCOL_BINARY_CMD_GETK
)
603 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
610 /* We call verify(validate_response_header), but that macro
611 * expects a boolean expression, and the function returns
612 * an enum.... Let's just create a macro to avoid cluttering
613 * the code with all of the == TEST_PASS ;-)
615 #define validate_response_header(a,b,c) \
616 do_validate_response_header(a,b,c) == TEST_PASS
619 static enum test_return
send_binary_noop(void)
622 raw_command(&cmd
, PROTOCOL_BINARY_CMD_NOOP
, NULL
, 0, NULL
, 0);
623 execute(send_packet(&cmd
));
627 static enum test_return
receive_binary_noop(void)
630 execute(recv_packet(&rsp
));
631 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_NOOP
,
632 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
636 static enum test_return
test_binary_noop(void)
638 execute(send_binary_noop());
639 execute(receive_binary_noop());
643 static enum test_return
test_binary_quit_impl(uint8_t cc
)
647 raw_command(&cmd
, cc
, NULL
, 0, NULL
, 0);
649 execute(send_packet(&cmd
));
650 if (cc
== PROTOCOL_BINARY_CMD_QUIT
)
652 execute(recv_packet(&rsp
));
653 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_QUIT
,
654 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
657 /* Socket should be closed now, read should return EXIT_SUCCESS */
658 verify(timeout_io_op(sock
, POLLIN
, rsp
.bytes
, sizeof(rsp
.bytes
)) == 0);
660 return TEST_PASS_RECONNECT
;
663 static enum test_return
test_binary_quit(void)
665 return test_binary_quit_impl(PROTOCOL_BINARY_CMD_QUIT
);
668 static enum test_return
test_binary_quitq(void)
670 return test_binary_quit_impl(PROTOCOL_BINARY_CMD_QUITQ
);
673 static enum test_return
test_binary_set_impl(const char* key
, uint8_t cc
)
678 uint64_t value
= 0xdeadbeefdeadcafeULL
;
679 storage_command(&cmd
, cc
, key
, strlen(key
), &value
, sizeof (value
), 0, 0);
681 /* set should always work */
682 for (int ii
= 0; ii
< 10; ii
++)
686 execute(send_packet(&cmd
));
690 execute(resend_packet(&cmd
));
693 if (cc
== PROTOCOL_BINARY_CMD_SET
)
695 execute(recv_packet(&rsp
));
696 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
699 execute(test_binary_noop());
703 * We need to get the current CAS id, and at this time we haven't
704 * verified that we have a working get
706 if (cc
== PROTOCOL_BINARY_CMD_SETQ
)
708 cmd
.set
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_SET
;
709 execute(resend_packet(&cmd
));
710 execute(recv_packet(&rsp
));
711 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_SET
,
712 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
713 cmd
.set
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_SETQ
;
716 /* try to set with the correct CAS value */
717 cmd
.plain
.message
.header
.request
.cas
= memcached_htonll(rsp
.plain
.message
.header
.response
.cas
);
718 execute(resend_packet(&cmd
));
719 if (cc
== PROTOCOL_BINARY_CMD_SET
)
721 execute(recv_packet(&rsp
));
722 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
725 execute(test_binary_noop());
727 /* try to set with an incorrect CAS value */
728 cmd
.plain
.message
.header
.request
.cas
= memcached_htonll(rsp
.plain
.message
.header
.response
.cas
- 1);
729 execute(resend_packet(&cmd
));
730 execute(send_binary_noop());
731 execute(recv_packet(&rsp
));
732 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS
));
733 execute(receive_binary_noop());
738 static enum test_return
test_binary_set(void)
740 return test_binary_set_impl("test_binary_set", PROTOCOL_BINARY_CMD_SET
);
743 static enum test_return
test_binary_setq(void)
745 return test_binary_set_impl("test_binary_setq", PROTOCOL_BINARY_CMD_SETQ
);
748 static enum test_return
test_binary_add_impl(const char* key
, uint8_t cc
)
752 uint64_t value
= 0xdeadbeefdeadcafeULL
;
753 storage_command(&cmd
, cc
, key
, strlen(key
), &value
, sizeof (value
), 0, 0);
755 /* first add should work, rest of them should fail (even with cas
757 for (int ii
=0; ii
< 10; ii
++)
760 execute(send_packet(&cmd
));
762 execute(resend_packet(&cmd
));
764 if (cc
== PROTOCOL_BINARY_CMD_ADD
|| ii
> 0)
766 uint16_t expected_result
;
768 expected_result
= PROTOCOL_BINARY_RESPONSE_SUCCESS
;
770 expected_result
= PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS
;
772 execute(send_binary_noop());
773 execute(recv_packet(&rsp
));
774 execute(receive_binary_noop());
775 verify(validate_response_header(&rsp
, cc
, expected_result
));
778 execute(test_binary_noop());
784 static enum test_return
test_binary_add(void)
786 return test_binary_add_impl("test_binary_add", PROTOCOL_BINARY_CMD_ADD
);
789 static enum test_return
test_binary_addq(void)
791 return test_binary_add_impl("test_binary_addq", PROTOCOL_BINARY_CMD_ADDQ
);
794 static enum test_return
binary_set_item(const char *key
, const char *value
)
798 storage_command(&cmd
, PROTOCOL_BINARY_CMD_SET
, key
, strlen(key
),
799 value
, strlen(value
), 0, 0);
800 execute(send_packet(&cmd
));
801 execute(recv_packet(&rsp
));
802 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_SET
,
803 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
807 static enum test_return
test_binary_replace_impl(const char* key
, uint8_t cc
)
811 uint64_t value
= 0xdeadbeefdeadcafeULL
;
812 storage_command(&cmd
, cc
, key
, strlen(key
), &value
, sizeof (value
), 0, 0);
814 /* first replace should fail, successive should succeed (when the
816 for (int ii
= 0; ii
< 10; ii
++)
820 execute(send_packet(&cmd
));
824 execute(resend_packet(&cmd
));
827 if (cc
== PROTOCOL_BINARY_CMD_REPLACE
|| ii
== 0)
829 uint16_t expected_result
;
832 expected_result
=PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
;
836 expected_result
=PROTOCOL_BINARY_RESPONSE_SUCCESS
;
839 execute(send_binary_noop());
840 execute(recv_packet(&rsp
));
841 execute(receive_binary_noop());
842 verify(validate_response_header(&rsp
, cc
, expected_result
));
845 execute(binary_set_item(key
, key
));
849 execute(test_binary_noop());
853 /* verify that replace with CAS value works! */
854 cmd
.plain
.message
.header
.request
.cas
= memcached_htonll(rsp
.plain
.message
.header
.response
.cas
);
855 execute(resend_packet(&cmd
));
857 if (cc
== PROTOCOL_BINARY_CMD_REPLACE
)
859 execute(recv_packet(&rsp
));
860 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
863 execute(test_binary_noop());
865 /* try to set with an incorrect CAS value */
866 cmd
.plain
.message
.header
.request
.cas
= memcached_htonll(rsp
.plain
.message
.header
.response
.cas
- 1);
867 execute(resend_packet(&cmd
));
868 execute(send_binary_noop());
869 execute(recv_packet(&rsp
));
870 execute(receive_binary_noop());
871 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS
));
876 static enum test_return
test_binary_replace(void)
878 return test_binary_replace_impl("test_binary_replace", PROTOCOL_BINARY_CMD_REPLACE
);
881 static enum test_return
test_binary_replaceq(void)
883 return test_binary_replace_impl("test_binary_replaceq", PROTOCOL_BINARY_CMD_REPLACEQ
);
886 static enum test_return
test_binary_delete_impl(const char *key
, uint8_t cc
)
890 raw_command(&cmd
, cc
, key
, strlen(key
), NULL
, 0);
892 /* The delete shouldn't work the first time, because the item isn't there */
893 execute(send_packet(&cmd
));
894 execute(send_binary_noop());
895 execute(recv_packet(&rsp
));
896 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
));
897 execute(receive_binary_noop());
898 execute(binary_set_item(key
, key
));
900 /* The item should be present now, resend*/
901 execute(resend_packet(&cmd
));
902 if (cc
== PROTOCOL_BINARY_CMD_DELETE
)
904 execute(recv_packet(&rsp
));
905 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
908 execute(test_binary_noop());
913 static enum test_return
test_binary_delete(void)
915 return test_binary_delete_impl("test_binary_delete", PROTOCOL_BINARY_CMD_DELETE
);
918 static enum test_return
test_binary_deleteq(void)
920 return test_binary_delete_impl("test_binary_deleteq", PROTOCOL_BINARY_CMD_DELETEQ
);
923 static enum test_return
test_binary_get_impl(const char *key
, uint8_t cc
)
928 raw_command(&cmd
, cc
, key
, strlen(key
), NULL
, 0);
929 execute(send_packet(&cmd
));
930 execute(send_binary_noop());
932 if (cc
== PROTOCOL_BINARY_CMD_GET
|| cc
== PROTOCOL_BINARY_CMD_GETK
)
934 execute(recv_packet(&rsp
));
935 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
));
938 execute(receive_binary_noop());
940 execute(binary_set_item(key
, key
));
941 execute(resend_packet(&cmd
));
942 execute(send_binary_noop());
944 execute(recv_packet(&rsp
));
945 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
946 execute(receive_binary_noop());
951 static enum test_return
test_binary_get(void)
953 return test_binary_get_impl("test_binary_get", PROTOCOL_BINARY_CMD_GET
);
956 static enum test_return
test_binary_getk(void)
958 return test_binary_get_impl("test_binary_getk", PROTOCOL_BINARY_CMD_GETK
);
961 static enum test_return
test_binary_getq(void)
963 return test_binary_get_impl("test_binary_getq", PROTOCOL_BINARY_CMD_GETQ
);
966 static enum test_return
test_binary_getkq(void)
968 return test_binary_get_impl("test_binary_getkq", PROTOCOL_BINARY_CMD_GETKQ
);
971 static enum test_return
test_binary_incr_impl(const char* key
, uint8_t cc
)
975 arithmetic_command(&cmd
, cc
, key
, strlen(key
), 1, 0, 0);
978 for (ii
= 0; ii
< 10; ++ii
)
981 execute(send_packet(&cmd
));
983 execute(resend_packet(&cmd
));
985 if (cc
== PROTOCOL_BINARY_CMD_INCREMENT
)
987 execute(recv_packet(&rsp
));
988 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
989 verify(memcached_ntohll(rsp
.incr
.message
.body
.value
) == ii
);
992 execute(test_binary_noop());
995 /* @todo add incorrect CAS */
999 static enum test_return
test_binary_incr(void)
1001 return test_binary_incr_impl("test_binary_incr", PROTOCOL_BINARY_CMD_INCREMENT
);
1004 static enum test_return
test_binary_incrq(void)
1006 return test_binary_incr_impl("test_binary_incrq", PROTOCOL_BINARY_CMD_INCREMENTQ
);
1009 static enum test_return
test_binary_decr_impl(const char* key
, uint8_t cc
)
1013 arithmetic_command(&cmd
, cc
, key
, strlen(key
), 1, 9, 0);
1016 for (ii
= 9; ii
> -1; --ii
)
1019 execute(send_packet(&cmd
));
1021 execute(resend_packet(&cmd
));
1023 if (cc
== PROTOCOL_BINARY_CMD_DECREMENT
)
1025 execute(recv_packet(&rsp
));
1026 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1027 verify(memcached_ntohll(rsp
.decr
.message
.body
.value
) == (uint64_t)ii
);
1030 execute(test_binary_noop());
1033 /* decr 0 should not wrap */
1034 execute(resend_packet(&cmd
));
1035 if (cc
== PROTOCOL_BINARY_CMD_DECREMENT
)
1037 execute(recv_packet(&rsp
));
1038 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1039 verify(memcached_ntohll(rsp
.decr
.message
.body
.value
) == 0);
1043 /* @todo get the value and verify! */
1047 /* @todo add incorrect cas */
1048 execute(test_binary_noop());
1052 static enum test_return
test_binary_decr(void)
1054 return test_binary_decr_impl("test_binary_decr",
1055 PROTOCOL_BINARY_CMD_DECREMENT
);
1058 static enum test_return
test_binary_decrq(void)
1060 return test_binary_decr_impl("test_binary_decrq",
1061 PROTOCOL_BINARY_CMD_DECREMENTQ
);
1064 static enum test_return
test_binary_version(void)
1068 raw_command(&cmd
, PROTOCOL_BINARY_CMD_VERSION
, NULL
, 0, NULL
, 0);
1070 execute(send_packet(&cmd
));
1071 execute(recv_packet(&rsp
));
1072 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_VERSION
,
1073 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1078 static enum test_return
test_binary_flush_impl(const char *key
, uint8_t cc
)
1083 for (int ii
= 0; ii
< 2; ++ii
)
1085 execute(binary_set_item(key
, key
));
1086 flush_command(&cmd
, cc
, 0, ii
== 0);
1087 execute(send_packet(&cmd
));
1089 if (cc
== PROTOCOL_BINARY_CMD_FLUSH
)
1091 execute(recv_packet(&rsp
));
1092 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1095 execute(test_binary_noop());
1097 raw_command(&cmd
, PROTOCOL_BINARY_CMD_GET
, key
, strlen(key
), NULL
, 0);
1098 execute(send_packet(&cmd
));
1099 execute(recv_packet(&rsp
));
1100 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_GET
,
1101 PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
));
1107 static enum test_return
test_binary_flush(void)
1109 return test_binary_flush_impl("test_binary_flush", PROTOCOL_BINARY_CMD_FLUSH
);
1112 static enum test_return
test_binary_flushq(void)
1114 return test_binary_flush_impl("test_binary_flushq", PROTOCOL_BINARY_CMD_FLUSHQ
);
1117 static enum test_return
test_binary_concat_impl(const char *key
, uint8_t cc
)
1123 if (cc
== PROTOCOL_BINARY_CMD_APPEND
|| cc
== PROTOCOL_BINARY_CMD_APPENDQ
)
1132 execute(binary_set_item(key
, value
));
1134 if (cc
== PROTOCOL_BINARY_CMD_APPEND
|| cc
== PROTOCOL_BINARY_CMD_APPENDQ
)
1143 raw_command(&cmd
, cc
, key
, strlen(key
), value
, strlen(value
));
1144 execute(send_packet(&cmd
));
1145 if (cc
== PROTOCOL_BINARY_CMD_APPEND
|| cc
== PROTOCOL_BINARY_CMD_PREPEND
)
1147 execute(recv_packet(&rsp
));
1148 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1152 execute(test_binary_noop());
1155 raw_command(&cmd
, PROTOCOL_BINARY_CMD_GET
, key
, strlen(key
), NULL
, 0);
1156 execute(send_packet(&cmd
));
1157 execute(recv_packet(&rsp
));
1158 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_GET
,
1159 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1160 verify(rsp
.plain
.message
.header
.response
.bodylen
- 4 == 11);
1161 verify(memcmp(rsp
.bytes
+ 28, "hello world", 11) == 0);
1166 static enum test_return
test_binary_append(void)
1168 return test_binary_concat_impl("test_binary_append", PROTOCOL_BINARY_CMD_APPEND
);
1171 static enum test_return
test_binary_prepend(void)
1173 return test_binary_concat_impl("test_binary_prepend", PROTOCOL_BINARY_CMD_PREPEND
);
1176 static enum test_return
test_binary_appendq(void)
1178 return test_binary_concat_impl("test_binary_appendq", PROTOCOL_BINARY_CMD_APPENDQ
);
1181 static enum test_return
test_binary_prependq(void)
1183 return test_binary_concat_impl("test_binary_prependq", PROTOCOL_BINARY_CMD_PREPENDQ
);
1186 static enum test_return
test_binary_stat(void)
1191 raw_command(&cmd
, PROTOCOL_BINARY_CMD_STAT
, NULL
, 0, NULL
, 0);
1192 execute(send_packet(&cmd
));
1196 execute(recv_packet(&rsp
));
1197 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_STAT
,
1198 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1199 } while (rsp
.plain
.message
.header
.response
.keylen
!= 0);
1204 static enum test_return
send_string(const char *cmd
)
1206 execute(retry_write(cmd
, strlen(cmd
)));
1210 static enum test_return
receive_line(char *buffer
, size_t size
)
1213 while (offset
< size
)
1215 execute(retry_read(buffer
+ offset
, 1));
1216 if (buffer
[offset
] == '\n')
1218 if (offset
+ 1 < size
)
1220 buffer
[offset
+ 1]= '\0';
1232 static enum test_return
receive_response(const char *msg
) {
1234 execute(receive_line(buffer
, sizeof(buffer
)));
1235 if (strcmp(msg
, buffer
) != 0) {
1236 fprintf(stderr
, "[%s]\n", buffer
);
1238 verify(strcmp(msg
, buffer
) == 0);
1242 static enum test_return
receive_error_response(void)
1245 execute(receive_line(buffer
, sizeof(buffer
)));
1246 verify(strncmp(buffer
, "ERROR", 5) == 0 ||
1247 strncmp(buffer
, "CLIENT_ERROR", 12) == 0 ||
1248 strncmp(buffer
, "SERVER_ERROR", 12) == 0);
1252 static enum test_return
test_ascii_quit(void)
1254 /* Verify that quit handles unknown options */
1255 execute(send_string("quit foo bar\r\n"));
1256 execute(receive_error_response());
1258 /* quit doesn't support noreply */
1259 execute(send_string("quit noreply\r\n"));
1260 execute(receive_error_response());
1262 /* Verify that quit works */
1263 execute(send_string("quit\r\n"));
1265 /* Socket should be closed now, read should return EXIT_SUCCESS */
1267 verify(timeout_io_op(sock
, POLLIN
, buffer
, sizeof(buffer
)) == 0);
1268 return TEST_PASS_RECONNECT
;
1272 static enum test_return
test_ascii_version(void)
1274 /* Verify that version command handles unknown options */
1275 execute(send_string("version foo bar\r\n"));
1276 execute(receive_error_response());
1278 /* version doesn't support noreply */
1279 execute(send_string("version noreply\r\n"));
1280 execute(receive_error_response());
1282 /* Verify that verify works */
1283 execute(send_string("version\r\n"));
1285 execute(receive_line(buffer
, sizeof(buffer
)));
1286 verify(strncmp(buffer
, "VERSION ", 8) == 0);
1291 static enum test_return
test_ascii_verbosity(void)
1293 /* This command does not adhere to the spec! */
1294 execute(send_string("verbosity foo bar my\r\n"));
1295 execute(receive_error_response());
1297 execute(send_string("verbosity noreply\r\n"));
1298 execute(receive_error_response());
1300 execute(send_string("verbosity 0 noreply\r\n"));
1301 execute(test_ascii_version());
1303 execute(send_string("verbosity\r\n"));
1304 execute(receive_error_response());
1306 execute(send_string("verbosity 1\r\n"));
1307 execute(receive_response("OK\r\n"));
1309 execute(send_string("verbosity 0\r\n"));
1310 execute(receive_response("OK\r\n"));
1317 static enum test_return
test_ascii_set_impl(const char* key
, bool noreply
)
1319 /* @todo add tests for bogus format! */
1321 snprintf(buffer
, sizeof(buffer
), "set %s 0 0 5%s\r\nvalue\r\n", key
, noreply
? " noreply" : "");
1322 execute(send_string(buffer
));
1326 execute(receive_response("STORED\r\n"));
1329 return test_ascii_version();
1332 static enum test_return
test_ascii_set(void)
1334 return test_ascii_set_impl("test_ascii_set", false);
1337 static enum test_return
test_ascii_set_noreply(void)
1339 return test_ascii_set_impl("test_ascii_set_noreply", true);
1342 static enum test_return
test_ascii_add_impl(const char* key
, bool noreply
)
1344 /* @todo add tests for bogus format! */
1346 snprintf(buffer
, sizeof(buffer
), "add %s 0 0 5%s\r\nvalue\r\n", key
, noreply
? " noreply" : "");
1347 execute(send_string(buffer
));
1351 execute(receive_response("STORED\r\n"));
1354 execute(send_string(buffer
));
1358 execute(receive_response("NOT_STORED\r\n"));
1361 return test_ascii_version();
1364 static enum test_return
test_ascii_add(void)
1366 return test_ascii_add_impl("test_ascii_add", false);
1369 static enum test_return
test_ascii_add_noreply(void)
1371 return test_ascii_add_impl("test_ascii_add_noreply", true);
1374 static enum test_return
ascii_get_unknown_value(char **key
, char **value
, ssize_t
*ndata
)
1378 execute(receive_line(buffer
, sizeof(buffer
)));
1379 verify(strncmp(buffer
, "VALUE ", 6) == 0);
1380 char *end
= strchr(buffer
+ 6, ' ');
1381 verify(end
!= NULL
);
1386 *key
= strdup(buffer
+ 6);
1387 verify(*key
!= NULL
);
1390 unsigned long val
= strtoul(ptr
, &end
, 10); /* flags */
1393 verify(end
!= NULL
);
1394 *ndata
= (ssize_t
)strtoul(end
, &end
, 10); /* size */
1396 verify(end
!= NULL
);
1397 while (end
and *end
!= '\n' and isspace(*end
))
1399 verify(end
and *end
== '\n');
1401 *value
= static_cast<char*>(malloc((size_t)*ndata
));
1402 verify(*value
!= NULL
);
1404 execute(retry_read(*value
, (size_t)*ndata
));
1406 execute(retry_read(buffer
, 2));
1407 verify(memcmp(buffer
, "\r\n", 2) == 0);
1412 static enum test_return
ascii_get_value(const char *key
, const char *value
)
1416 size_t datasize
= strlen(value
);
1418 verify(datasize
< sizeof(buffer
));
1419 execute(receive_line(buffer
, sizeof(buffer
)));
1420 verify(strncmp(buffer
, "VALUE ", 6) == 0);
1421 verify(strncmp(buffer
+ 6, key
, strlen(key
)) == 0);
1422 char *ptr
= buffer
+ 6 + strlen(key
) + 1;
1425 unsigned long val
= strtoul(ptr
, &end
, 10); /* flags */
1428 verify(end
!= NULL
);
1429 val
= strtoul(end
, &end
, 10); /* size */
1431 verify(val
== datasize
);
1432 verify(end
!= NULL
);
1433 while (end
and *end
!= '\n' and isspace(*end
))
1437 verify(end
and *end
== '\n');
1439 execute(retry_read(buffer
, datasize
));
1440 verify(memcmp(buffer
, value
, datasize
) == 0);
1442 execute(retry_read(buffer
, 2));
1443 verify(memcmp(buffer
, "\r\n", 2) == 0);
1448 static enum test_return
ascii_get_item(const char *key
, const char *value
,
1455 datasize
= strlen(value
);
1458 verify(datasize
< sizeof(buffer
));
1459 snprintf(buffer
, sizeof(buffer
), "get %s\r\n", key
);
1460 execute(send_string(buffer
));
1464 execute(ascii_get_value(key
, value
));
1467 execute(retry_read(buffer
, 5));
1468 verify(memcmp(buffer
, "END\r\n", 5) == 0);
1473 static enum test_return
ascii_gets_value(const char *key
, const char *value
,
1478 size_t datasize
= strlen(value
);
1480 verify(datasize
< sizeof(buffer
));
1481 execute(receive_line(buffer
, sizeof(buffer
)));
1482 verify(strncmp(buffer
, "VALUE ", 6) == 0);
1483 verify(strncmp(buffer
+ 6, key
, strlen(key
)) == 0);
1484 char *ptr
= buffer
+ 6 + strlen(key
) + 1;
1487 unsigned long val
= strtoul(ptr
, &end
, 10); /* flags */
1490 verify(end
!= NULL
);
1491 val
= strtoul(end
, &end
, 10); /* size */
1493 verify(val
== datasize
);
1494 verify(end
!= NULL
);
1495 *cas
= strtoul(end
, &end
, 10); /* cas */
1497 verify(val
== datasize
);
1498 verify(end
!= NULL
);
1500 while (end
and *end
!= '\n' and isspace(*end
))
1504 verify(end
and *end
== '\n');
1506 execute(retry_read(buffer
, datasize
));
1507 verify(memcmp(buffer
, value
, datasize
) == 0);
1509 execute(retry_read(buffer
, 2));
1510 verify(memcmp(buffer
, "\r\n", 2) == 0);
1515 static enum test_return
ascii_gets_item(const char *key
, const char *value
,
1516 bool exist
, unsigned long *cas
)
1522 datasize
= strlen(value
);
1525 verify(datasize
< sizeof(buffer
));
1526 snprintf(buffer
, sizeof(buffer
), "gets %s\r\n", key
);
1527 execute(send_string(buffer
));
1530 execute(ascii_gets_value(key
, value
, cas
));
1532 execute(retry_read(buffer
, 5));
1533 verify(memcmp(buffer
, "END\r\n", 5) == 0);
1538 static enum test_return
ascii_set_item(const char *key
, const char *value
)
1541 size_t len
= strlen(value
);
1542 snprintf(buffer
, sizeof(buffer
), "set %s 0 0 %u\r\n", key
, (unsigned int)len
);
1543 execute(send_string(buffer
));
1544 execute(retry_write(value
, len
));
1545 execute(send_string("\r\n"));
1546 execute(receive_response("STORED\r\n"));
1550 static enum test_return
test_ascii_replace_impl(const char* key
, bool noreply
)
1553 snprintf(buffer
, sizeof(buffer
), "replace %s 0 0 5%s\r\nvalue\r\n", key
, noreply
? " noreply" : "");
1554 execute(send_string(buffer
));
1558 execute(test_ascii_version());
1562 execute(receive_response("NOT_STORED\r\n"));
1565 execute(ascii_set_item(key
, "value"));
1566 execute(ascii_get_item(key
, "value", true));
1569 execute(send_string(buffer
));
1572 execute(test_ascii_version());
1574 execute(receive_response("STORED\r\n"));
1576 return test_ascii_version();
1579 static enum test_return
test_ascii_replace(void)
1581 return test_ascii_replace_impl("test_ascii_replace", false);
1584 static enum test_return
test_ascii_replace_noreply(void)
1586 return test_ascii_replace_impl("test_ascii_replace_noreply", true);
1589 static enum test_return
test_ascii_cas_impl(const char* key
, bool noreply
)
1594 execute(ascii_set_item(key
, "value"));
1595 execute(ascii_gets_item(key
, "value", true, &cas
));
1597 snprintf(buffer
, sizeof(buffer
), "cas %s 0 0 6 %lu%s\r\nvalue2\r\n", key
, cas
, noreply
? " noreply" : "");
1598 execute(send_string(buffer
));
1602 execute(test_ascii_version());
1606 execute(receive_response("STORED\r\n"));
1609 /* reexecute the same command should fail due to illegal cas */
1610 execute(send_string(buffer
));
1614 execute(test_ascii_version());
1618 execute(receive_response("EXISTS\r\n"));
1621 return test_ascii_version();
1624 static enum test_return
test_ascii_cas(void)
1626 return test_ascii_cas_impl("test_ascii_cas", false);
1629 static enum test_return
test_ascii_cas_noreply(void)
1631 return test_ascii_cas_impl("test_ascii_cas_noreply", true);
1634 static enum test_return
test_ascii_delete_impl(const char *key
, bool noreply
)
1636 execute(ascii_set_item(key
, "value"));
1638 execute(send_string("delete\r\n"));
1639 execute(receive_error_response());
1640 /* BUG: the server accepts delete a b */
1641 execute(send_string("delete a b c d e\r\n"));
1642 execute(receive_error_response());
1645 snprintf(buffer
, sizeof(buffer
), "delete %s%s\r\n", key
, noreply
? " noreply" : "");
1646 execute(send_string(buffer
));
1649 execute(test_ascii_version());
1651 execute(receive_response("DELETED\r\n"));
1653 execute(ascii_get_item(key
, "value", false));
1654 execute(send_string(buffer
));
1656 execute(test_ascii_version());
1658 execute(receive_response("NOT_FOUND\r\n"));
1663 static enum test_return
test_ascii_delete(void)
1665 return test_ascii_delete_impl("test_ascii_delete", false);
1668 static enum test_return
test_ascii_delete_noreply(void)
1670 return test_ascii_delete_impl("test_ascii_delete_noreply", true);
1673 static enum test_return
test_ascii_get(void)
1675 execute(ascii_set_item("test_ascii_get", "value"));
1677 execute(send_string("get\r\n"));
1678 execute(receive_error_response());
1679 execute(ascii_get_item("test_ascii_get", "value", true));
1680 execute(ascii_get_item("test_ascii_get_notfound", "value", false));
1685 static enum test_return
test_ascii_gets(void)
1687 execute(ascii_set_item("test_ascii_gets", "value"));
1689 execute(send_string("gets\r\n"));
1690 execute(receive_error_response());
1692 execute(ascii_gets_item("test_ascii_gets", "value", true, &cas
));
1693 execute(ascii_gets_item("test_ascii_gets_notfound", "value", false, &cas
));
1698 static enum test_return
test_ascii_mget(void)
1700 const uint32_t nkeys
= 5;
1701 const char * const keys
[]= {
1704 /* test_ascii_mget_3 does not exist :) */
1710 for (uint32_t x
= 0; x
< nkeys
; ++x
)
1712 execute(ascii_set_item(keys
[x
], "value"));
1715 /* Ask for a key that doesn't exist as well */
1716 execute(send_string("get test_ascii_mget1 test_ascii_mget2 test_ascii_mget3 "
1717 "test_ascii_mget4 test_ascii_mget5 "
1718 "test_ascii_mget6\r\n"));
1720 std::vector
<char *> returned
;
1721 returned
.resize(nkeys
);
1723 for (uint32_t x
= 0; x
< nkeys
; ++x
)
1727 execute(ascii_get_unknown_value(&returned
[x
], &v
, &nbytes
));
1728 verify(nbytes
== 5);
1729 verify(memcmp(v
, "value", 5) == 0);
1734 execute(retry_read(buffer
, 5));
1735 verify(memcmp(buffer
, "END\r\n", 5) == 0);
1737 /* verify that we got all the keys we expected */
1738 for (uint32_t x
= 0; x
< nkeys
; ++x
)
1741 for (uint32_t y
= 0; y
< nkeys
; ++y
)
1743 if (strcmp(keys
[x
], returned
[y
]) == 0)
1752 for (uint32_t x
= 0; x
< nkeys
; ++x
)
1760 static enum test_return
test_ascii_incr_impl(const char* key
, bool noreply
)
1763 snprintf(cmd
, sizeof(cmd
), "incr %s 1%s\r\n", key
, noreply
? " noreply" : "");
1765 execute(ascii_set_item(key
, "0"));
1766 for (int x
= 1; x
< 11; ++x
)
1768 execute(send_string(cmd
));
1771 execute(test_ascii_version());
1775 execute(receive_line(buffer
, sizeof(buffer
)));
1776 int val
= atoi(buffer
);
1781 execute(ascii_get_item(key
, "10", true));
1786 static enum test_return
test_ascii_incr(void)
1788 return test_ascii_incr_impl("test_ascii_incr", false);
1791 static enum test_return
test_ascii_incr_noreply(void)
1793 return test_ascii_incr_impl("test_ascii_incr_noreply", true);
1796 static enum test_return
test_ascii_decr_impl(const char* key
, bool noreply
)
1799 snprintf(cmd
, sizeof(cmd
), "decr %s 1%s\r\n", key
, noreply
? " noreply" : "");
1801 execute(ascii_set_item(key
, "9"));
1802 for (int x
= 8; x
> -1; --x
)
1804 execute(send_string(cmd
));
1808 execute(test_ascii_version());
1813 execute(receive_line(buffer
, sizeof(buffer
)));
1814 int val
= atoi(buffer
);
1819 execute(ascii_get_item(key
, "0", true));
1821 /* verify that it doesn't wrap */
1822 execute(send_string(cmd
));
1825 execute(test_ascii_version());
1830 execute(receive_line(buffer
, sizeof(buffer
)));
1832 execute(ascii_get_item(key
, "0", true));
1837 static enum test_return
test_ascii_decr(void)
1839 return test_ascii_decr_impl("test_ascii_decr", false);
1842 static enum test_return
test_ascii_decr_noreply(void)
1844 return test_ascii_decr_impl("test_ascii_decr_noreply", true);
1848 static enum test_return
test_ascii_flush_impl(const char *key
, bool noreply
)
1851 /* Verify that the flush_all command handles unknown options */
1852 /* Bug in the current memcached server! */
1853 execute(send_string("flush_all foo bar\r\n"));
1854 execute(receive_error_response());
1857 execute(ascii_set_item(key
, key
));
1858 execute(ascii_get_item(key
, key
, true));
1862 execute(send_string("flush_all noreply\r\n"));
1863 execute(test_ascii_version());
1867 execute(send_string("flush_all\r\n"));
1868 execute(receive_response("OK\r\n"));
1871 execute(ascii_get_item(key
, key
, false));
1876 static enum test_return
test_ascii_flush(void)
1878 return test_ascii_flush_impl("test_ascii_flush", false);
1881 static enum test_return
test_ascii_flush_noreply(void)
1883 return test_ascii_flush_impl("test_ascii_flush_noreply", true);
1886 static enum test_return
test_ascii_concat_impl(const char *key
,
1897 execute(ascii_set_item(key
, value
));
1909 snprintf(cmd
, sizeof(cmd
), "%s %s 0 0 %u%s\r\n%s\r\n",
1910 append
? "append" : "prepend",
1911 key
, (unsigned int)strlen(value
), noreply
? " noreply" : "",
1913 execute(send_string(cmd
));
1917 execute(test_ascii_version());
1921 execute(receive_response("STORED\r\n"));
1924 execute(ascii_get_item(key
, "hello world", true));
1926 snprintf(cmd
, sizeof(cmd
), "%s %s_notfound 0 0 %u%s\r\n%s\r\n",
1927 append
? "append" : "prepend",
1928 key
, (unsigned int)strlen(value
), noreply
? " noreply" : "",
1930 execute(send_string(cmd
));
1934 execute(test_ascii_version());
1938 execute(receive_response("NOT_STORED\r\n"));
1944 static enum test_return
test_ascii_append(void)
1946 return test_ascii_concat_impl("test_ascii_append", true, false);
1949 static enum test_return
test_ascii_prepend(void)
1951 return test_ascii_concat_impl("test_ascii_prepend", false, false);
1954 static enum test_return
test_ascii_append_noreply(void)
1956 return test_ascii_concat_impl("test_ascii_append_noreply", true, true);
1959 static enum test_return
test_ascii_prepend_noreply(void)
1961 return test_ascii_concat_impl("test_ascii_prepend_noreply", false, true);
1964 static enum test_return
test_ascii_stat(void)
1966 execute(send_string("stats noreply\r\n"));
1967 execute(receive_error_response());
1968 execute(send_string("stats\r\n"));
1971 execute(receive_line(buffer
, sizeof(buffer
)));
1972 } while (strcmp(buffer
, "END\r\n") != 0);
1974 return TEST_PASS_RECONNECT
;
1977 typedef enum test_return(*TEST_FUNC
)(void);
1981 const char *description
;
1985 struct testcase testcases
[]= {
1986 { "ascii quit", test_ascii_quit
},
1987 { "ascii version", test_ascii_version
},
1988 { "ascii verbosity", test_ascii_verbosity
},
1989 { "ascii set", test_ascii_set
},
1990 { "ascii set noreply", test_ascii_set_noreply
},
1991 { "ascii get", test_ascii_get
},
1992 { "ascii gets", test_ascii_gets
},
1993 { "ascii mget", test_ascii_mget
},
1994 { "ascii flush", test_ascii_flush
},
1995 { "ascii flush noreply", test_ascii_flush_noreply
},
1996 { "ascii add", test_ascii_add
},
1997 { "ascii add noreply", test_ascii_add_noreply
},
1998 { "ascii replace", test_ascii_replace
},
1999 { "ascii replace noreply", test_ascii_replace_noreply
},
2000 { "ascii cas", test_ascii_cas
},
2001 { "ascii cas noreply", test_ascii_cas_noreply
},
2002 { "ascii delete", test_ascii_delete
},
2003 { "ascii delete noreply", test_ascii_delete_noreply
},
2004 { "ascii incr", test_ascii_incr
},
2005 { "ascii incr noreply", test_ascii_incr_noreply
},
2006 { "ascii decr", test_ascii_decr
},
2007 { "ascii decr noreply", test_ascii_decr_noreply
},
2008 { "ascii append", test_ascii_append
},
2009 { "ascii append noreply", test_ascii_append_noreply
},
2010 { "ascii prepend", test_ascii_prepend
},
2011 { "ascii prepend noreply", test_ascii_prepend_noreply
},
2012 { "ascii stat", test_ascii_stat
},
2013 { "binary noop", test_binary_noop
},
2014 { "binary quit", test_binary_quit
},
2015 { "binary quitq", test_binary_quitq
},
2016 { "binary set", test_binary_set
},
2017 { "binary setq", test_binary_setq
},
2018 { "binary flush", test_binary_flush
},
2019 { "binary flushq", test_binary_flushq
},
2020 { "binary add", test_binary_add
},
2021 { "binary addq", test_binary_addq
},
2022 { "binary replace", test_binary_replace
},
2023 { "binary replaceq", test_binary_replaceq
},
2024 { "binary delete", test_binary_delete
},
2025 { "binary deleteq", test_binary_deleteq
},
2026 { "binary get", test_binary_get
},
2027 { "binary getq", test_binary_getq
},
2028 { "binary getk", test_binary_getk
},
2029 { "binary getkq", test_binary_getkq
},
2030 { "binary incr", test_binary_incr
},
2031 { "binary incrq", test_binary_incrq
},
2032 { "binary decr", test_binary_decr
},
2033 { "binary decrq", test_binary_decrq
},
2034 { "binary version", test_binary_version
},
2035 { "binary append", test_binary_append
},
2036 { "binary appendq", test_binary_appendq
},
2037 { "binary prepend", test_binary_prepend
},
2038 { "binary prependq", test_binary_prependq
},
2039 { "binary stat", test_binary_stat
},
2043 const int ascii_tests
= 1;
2044 const int binary_tests
= 2;
2052 int main(int argc
, char **argv
)
2054 static const char * const status_msg
[]= {"[skip]", "[pass]", "[pass]", "[FAIL]"};
2055 struct test_type_st tests
= { true, true };
2058 const char *hostname
= "localhost";
2059 const char *port
= "11211";
2062 const char *testname
= NULL
;
2066 while ((cmd
= getopt(argc
, argv
, "qt:vch:p:PT:?ab")) != EOF
)
2071 tests
.binary
= false;
2080 timeout
= atoi(optarg
);
2083 fprintf(stderr
, "Invalid timeout. Please specify a number for -t\n");
2084 return EXIT_FAILURE
;
2088 case 'v': verbose
= true;
2091 case 'c': do_core
= true;
2094 case 'h': hostname
= optarg
;
2097 case 'p': port
= optarg
;
2104 case 'P': prompt
= true;
2107 case 'T': testname
= optarg
;
2111 fprintf(stderr
, "Usage: %s [-h hostname] [-p port] [-c] [-v] [-t n] [-P] [-T testname]'\n"
2112 "\t-c\tGenerate coredump if a test fails\n"
2113 "\t-v\tVerbose test output (print out the assertion)\n"
2114 "\t-t n\tSet the timeout for io-operations to n seconds\n"
2115 "\t-P\tPrompt the user before starting a test.\n"
2116 "\t\t\t\"skip\" will skip the test\n"
2117 "\t\t\t\"quit\" will terminate memcapable\n"
2118 "\t\t\tEverything else will start the test\n"
2119 "\t-T n\tJust run the test named n\n"
2120 "\t-a\tOnly test the ascii protocol\n"
2121 "\t-b\tOnly test the binary protocol\n",
2123 return EXIT_SUCCESS
;
2127 initialize_sockets();
2128 sock
= connect_server(hostname
, port
);
2129 if (sock
== INVALID_SOCKET
)
2131 fprintf(stderr
, "Failed to connect to <%s:%s>: %s\n",
2132 hostname
, port
, strerror(get_socket_errno()));
2133 return EXIT_FAILURE
;
2136 for (int ii
= 0; testcases
[ii
].description
!= NULL
; ++ii
)
2138 if (testname
!= NULL
&& strcmp(testcases
[ii
].description
, testname
) != 0)
2143 if ((testcases
[ii
].description
[0] == 'a' && (tests
.ascii
) == 0) ||
2144 (testcases
[ii
].description
[0] == 'b' && (tests
.binary
) == 0))
2149 fprintf(stdout
, "%-40s", testcases
[ii
].description
);
2154 fprintf(stdout
, "\nPress <return> when you are ready? ");
2155 char buffer
[80] = {0};
2156 if (fgets(buffer
, sizeof(buffer
), stdin
) != NULL
) {
2157 if (strncmp(buffer
, "skip", 4) == 0)
2159 fprintf(stdout
, "%-40s%s\n", testcases
[ii
].description
,
2160 status_msg
[TEST_SKIP
]);
2164 if (strncmp(buffer
, "quit", 4) == 0)
2170 fprintf(stdout
, "%-40s", testcases
[ii
].description
);
2174 bool reconnect
= false;
2175 enum test_return ret
= testcases
[ii
].function();
2176 if (ret
== TEST_FAIL
)
2181 fprintf(stderr
, "\n");
2183 else if (ret
== TEST_PASS_RECONNECT
)
2186 fprintf(stderr
, "%s\n", status_msg
[ret
]);
2190 if ((sock
= connect_server(hostname
, port
)) == INVALID_SOCKET
)
2192 fprintf(stderr
, "Failed to connect to <%s:%s>: %s\n", hostname
, port
, strerror(get_socket_errno()));
2193 fprintf(stderr
, "%d of %d tests failed\n", failed
, total
);
2194 return EXIT_FAILURE
;
2202 fprintf(stdout
, "All tests passed\n");
2206 fprintf(stderr
, "%d of %d tests failed\n", failed
, total
);
2209 return (failed
== 0) ? EXIT_SUCCESS
: EXIT_FAILURE
;