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/close_socket.hpp>
38 #include <libmemcached/memcached/protocol_binary.h>
39 #include <libmemcached/byteorder.h>
40 #include <clients/utilities.h>
43 /* /usr/include/netinet/in.h defines macros from ntohs() to _bswap_nn to
44 * optimize the conversion functions, but the prototypes generate warnings
45 * from gcc. The conversion methods isn't the bottleneck for my app, so
46 * just remove the warnings by undef'ing the optimization ..
52 /* Should we generate coredumps when we enounter an error (-c) */
53 static bool do_core
= false;
54 /* connection to the server */
55 static memcached_socket_t sock
;
56 /* Should the output from test failures be verbose or quiet? */
57 static bool verbose
= false;
59 /* The number of seconds to wait for an IO-operation */
60 static int timeout
= 2;
63 * Instead of having to cast between the different datatypes we create
64 * a union of all of the different types of pacages we want to send.
65 * A lot of the different commands use the same packet layout, so I'll
66 * just define the different types I need. The typedefs only contain
67 * the header of the message, so we need some space for keys and body
68 * To avoid to have to do multiple writes, lets add a chunk of memory
69 * to use. 1k should be more than enough for header, key and body.
73 protocol_binary_request_no_extras plain
;
74 protocol_binary_request_flush flush
;
75 protocol_binary_request_incr incr
;
76 protocol_binary_request_set set
;
82 protocol_binary_response_no_extras plain
;
83 protocol_binary_response_incr incr
;
84 protocol_binary_response_decr decr
;
90 TEST_SKIP
, TEST_PASS
, TEST_PASS_RECONNECT
, TEST_FAIL
94 * Try to get an addrinfo struct for a given port on a given host
96 static struct addrinfo
*lookuphost(const char *hostname
, const char *port
)
98 struct addrinfo
*ai
= 0;
99 struct addrinfo hints
;
100 memset(&hints
, 0, sizeof(struct addrinfo
));
101 hints
.ai_family
=AF_UNSPEC
;
102 hints
.ai_protocol
=IPPROTO_TCP
;
103 hints
.ai_socktype
=SOCK_STREAM
;
105 int error
= getaddrinfo(hostname
, port
, &hints
, &ai
);
108 if (error
!= EAI_SYSTEM
)
109 fprintf(stderr
, "getaddrinfo(): %s\n", gai_strerror(error
));
111 perror("getaddrinfo()");
118 * Set the socket in nonblocking mode
119 * @return -1 if failure, the socket otherwise
121 static memcached_socket_t
set_noblock(void)
125 if (ioctlsocket(sock
, FIONBIO
, &arg
) == SOCKET_ERROR
)
127 perror("Failed to set nonblocking io");
129 return INVALID_SOCKET
;
132 int flags
= fcntl(sock
, F_GETFL
, 0);
135 perror("Failed to get socket flags");
136 memcached_close_socket(sock
);
137 return INVALID_SOCKET
;
140 if ((flags
& O_NONBLOCK
) != O_NONBLOCK
)
142 if (fcntl(sock
, F_SETFL
, flags
| O_NONBLOCK
) == -1)
144 perror("Failed to set socket to nonblocking mode");
145 memcached_close_socket(sock
);
146 return INVALID_SOCKET
;
154 * Try to open a connection to the server
155 * @param hostname the name of the server to connect to
156 * @param port the port number (or service) to connect to
157 * @return positive integer if success, -1 otherwise
159 static memcached_socket_t
connect_server(const char *hostname
, const char *port
)
161 struct addrinfo
*ai
= lookuphost(hostname
, port
);
162 sock
= INVALID_SOCKET
;
165 if ((sock
= socket(ai
->ai_family
, ai
->ai_socktype
,
166 ai
->ai_protocol
)) != INVALID_SOCKET
)
168 if (connect(sock
, ai
->ai_addr
, ai
->ai_addrlen
) == SOCKET_ERROR
)
170 fprintf(stderr
, "Failed to connect socket: %s\n",
171 strerror(get_socket_errno()));
173 sock
= INVALID_SOCKET
;
181 fprintf(stderr
, "Failed to create socket: %s\n",
182 strerror(get_socket_errno()));
190 static ssize_t
timeout_io_op(memcached_socket_t fd
, short direction
, void *buf
, size_t len
)
194 if (direction
== POLLOUT
)
196 ret
= send(fd
, buf
, len
, 0);
200 ret
= recv(fd
, buf
, len
, 0);
203 if (ret
== SOCKET_ERROR
&& get_socket_errno() == EWOULDBLOCK
)
206 memset(&fds
, 0, sizeof(struct pollfd
));
207 fds
.events
= direction
;
210 int err
= poll(&fds
, 1, timeout
* 1000);
213 if (direction
== POLLOUT
)
215 ret
= send(fd
, buf
, len
, 0);
219 ret
= recv(fd
, buf
, len
, 0);
228 perror("Failed to poll");
237 * Ensure that an expression is true. If it isn't print out a message similar
238 * to assert() and create a coredump if the user wants that. If not an error
239 * message is returned.
242 static enum test_return
ensure(bool val
, const char *expression
, const char *file
, int line
)
247 fprintf(stderr
, "\n%s:%d: %s", file
, line
, expression
);
258 #define verify(expression) do { if (ensure(expression, #expression, __FILE__, __LINE__) == TEST_FAIL) return TEST_FAIL; } while (0)
259 #define execute(expression) do { if (ensure(expression == TEST_PASS, #expression, __FILE__, __LINE__) == TEST_FAIL) return TEST_FAIL; } while (0)
262 * Send a chunk of memory over the socket (retry if the call is iterrupted
264 static enum test_return
retry_write(const void* buf
, size_t len
)
267 const char* ptr
= static_cast<const char *>(buf
);
271 size_t num_bytes
= len
- offset
;
272 ssize_t nw
= timeout_io_op(sock
, POLLOUT
, (void*)(ptr
+ offset
), num_bytes
);
274 verify(get_socket_errno() == EINTR
|| get_socket_errno() == EAGAIN
);
277 } while (offset
< len
);
283 * Resend a packet to the server (All fields in the command header should
284 * be in network byte order)
286 static enum test_return
resend_packet(command
*cmd
)
288 size_t length
= sizeof (protocol_binary_request_no_extras
) +
289 ntohl(cmd
->plain
.message
.header
.request
.bodylen
);
291 execute(retry_write(cmd
, length
));
296 * Send a command to the server. The command header needs to be updated
297 * to network byte order
299 static enum test_return
send_packet(command
*cmd
)
301 /* Fix the byteorder of the header */
302 cmd
->plain
.message
.header
.request
.keylen
=
303 ntohs(cmd
->plain
.message
.header
.request
.keylen
);
304 cmd
->plain
.message
.header
.request
.bodylen
=
305 ntohl(cmd
->plain
.message
.header
.request
.bodylen
);
306 cmd
->plain
.message
.header
.request
.cas
=
307 memcached_ntohll(cmd
->plain
.message
.header
.request
.cas
);
309 execute(resend_packet(cmd
));
314 * Read a fixed length chunk of data from the server
316 static enum test_return
retry_read(void *buf
, size_t len
)
321 ssize_t nr
= timeout_io_op(sock
, POLLIN
, ((char*) buf
) + offset
, len
- offset
);
324 fprintf(stderr
, "Errno: %d %s\n", get_socket_errno(), strerror(errno
));
325 verify(get_socket_errno() == EINTR
|| get_socket_errno() == EAGAIN
);
332 } while (offset
< len
);
338 * Receive a response from the server and conver the fields in the header
339 * to local byte order
341 static enum test_return
recv_packet(response
*rsp
)
343 execute(retry_read(rsp
, sizeof(protocol_binary_response_no_extras
)));
345 /* Fix the byte order in the packet header */
346 rsp
->plain
.message
.header
.response
.keylen
=
347 ntohs(rsp
->plain
.message
.header
.response
.keylen
);
348 rsp
->plain
.message
.header
.response
.status
=
349 ntohs(rsp
->plain
.message
.header
.response
.status
);
350 rsp
->plain
.message
.header
.response
.bodylen
=
351 ntohl(rsp
->plain
.message
.header
.response
.bodylen
);
352 rsp
->plain
.message
.header
.response
.cas
=
353 memcached_ntohll(rsp
->plain
.message
.header
.response
.cas
);
355 size_t bodysz
= rsp
->plain
.message
.header
.response
.bodylen
;
357 execute(retry_read(rsp
->bytes
+ sizeof (protocol_binary_response_no_extras
), bodysz
));
363 * Create a storage command (add, set, replace etc)
365 * @param cmd destination buffer
366 * @param cc the storage command to create
367 * @param key the key to store
368 * @param keylen the length of the key
369 * @param dta the data to store with the key
370 * @param dtalen the length of the data to store with the key
371 * @param flags the flags to store along with the key
372 * @param exptime the expiry time for the key
374 static void storage_command(command
*cmd
,
383 /* all of the storage commands use the same command layout */
384 protocol_binary_request_set
*request
= &cmd
->set
;
386 memset(request
, 0, sizeof (*request
));
387 request
->message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
388 request
->message
.header
.request
.opcode
= cc
;
389 request
->message
.header
.request
.keylen
= (uint16_t)keylen
;
390 request
->message
.header
.request
.extlen
= 8;
391 request
->message
.header
.request
.bodylen
= (uint32_t)(keylen
+ 8 + dtalen
);
392 request
->message
.header
.request
.opaque
= 0xdeadbeef;
393 request
->message
.body
.flags
= flags
;
394 request
->message
.body
.expiration
= exptime
;
396 off_t key_offset
= sizeof (protocol_binary_request_no_extras
) + 8;
397 memcpy(cmd
->bytes
+ key_offset
, key
, keylen
);
399 memcpy(cmd
->bytes
+ key_offset
+ keylen
, dta
, dtalen
);
403 * Create a basic command to send to the server
404 * @param cmd destination buffer
405 * @param cc the command to create
406 * @param key the key to store
407 * @param keylen the length of the key
408 * @param dta the data to store with the key
409 * @param dtalen the length of the data to store with the key
411 static void raw_command(command
*cmd
,
418 /* all of the storage commands use the same command layout */
419 memset(cmd
, 0, sizeof (*cmd
));
420 cmd
->plain
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
421 cmd
->plain
.message
.header
.request
.opcode
= cc
;
422 cmd
->plain
.message
.header
.request
.keylen
= (uint16_t)keylen
;
423 cmd
->plain
.message
.header
.request
.bodylen
= (uint32_t)(keylen
+ dtalen
);
424 cmd
->plain
.message
.header
.request
.opaque
= 0xdeadbeef;
426 off_t key_offset
= sizeof (protocol_binary_request_no_extras
);
429 memcpy(cmd
->bytes
+ key_offset
, key
, keylen
);
432 memcpy(cmd
->bytes
+ key_offset
+ keylen
, dta
, dtalen
);
436 * Create the flush command
437 * @param cmd destination buffer
438 * @param cc the command to create (FLUSH/FLUSHQ)
439 * @param exptime when to flush
440 * @param use_extra to force using of the extra field?
442 static void flush_command(command
*cmd
,
443 uint8_t cc
, uint32_t exptime
, bool use_extra
)
445 memset(cmd
, 0, sizeof (cmd
->flush
));
446 cmd
->flush
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
447 cmd
->flush
.message
.header
.request
.opcode
= cc
;
448 cmd
->flush
.message
.header
.request
.opaque
= 0xdeadbeef;
450 if (exptime
!= 0 || use_extra
)
452 cmd
->flush
.message
.header
.request
.extlen
= 4;
453 cmd
->flush
.message
.body
.expiration
= htonl(exptime
);
454 cmd
->flush
.message
.header
.request
.bodylen
= 4;
459 * Create a incr/decr command
460 * @param cc the cmd to create (FLUSH/FLUSHQ)
461 * @param key the key to operate on
462 * @param keylen the number of bytes in the key
463 * @param delta the number to add/subtract
464 * @param initial the initial value if the key doesn't exist
465 * @param exptime when the key should expire if it isn't set
467 static void arithmetic_command(command
*cmd
,
475 memset(cmd
, 0, sizeof (cmd
->incr
));
476 cmd
->incr
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
477 cmd
->incr
.message
.header
.request
.opcode
= cc
;
478 cmd
->incr
.message
.header
.request
.keylen
= (uint16_t)keylen
;
479 cmd
->incr
.message
.header
.request
.extlen
= 20;
480 cmd
->incr
.message
.header
.request
.bodylen
= (uint32_t)(keylen
+ 20);
481 cmd
->incr
.message
.header
.request
.opaque
= 0xdeadbeef;
482 cmd
->incr
.message
.body
.delta
= memcached_htonll(delta
);
483 cmd
->incr
.message
.body
.initial
= memcached_htonll(initial
);
484 cmd
->incr
.message
.body
.expiration
= htonl(exptime
);
486 off_t key_offset
= sizeof (protocol_binary_request_no_extras
) + 20;
487 memcpy(cmd
->bytes
+ key_offset
, key
, keylen
);
491 * Validate the response header from the server
492 * @param rsp the response to check
493 * @param cc the expected command
494 * @param status the expected status
496 static enum test_return
do_validate_response_header(response
*rsp
,
497 uint8_t cc
, uint16_t status
)
499 verify(rsp
->plain
.message
.header
.response
.magic
== PROTOCOL_BINARY_RES
);
500 verify(rsp
->plain
.message
.header
.response
.opcode
== cc
);
501 verify(rsp
->plain
.message
.header
.response
.datatype
== PROTOCOL_BINARY_RAW_BYTES
);
502 verify(rsp
->plain
.message
.header
.response
.status
== status
);
503 verify(rsp
->plain
.message
.header
.response
.opaque
== 0xdeadbeef);
505 if (status
== PROTOCOL_BINARY_RESPONSE_SUCCESS
)
508 case PROTOCOL_BINARY_CMD_ADDQ
:
509 case PROTOCOL_BINARY_CMD_APPENDQ
:
510 case PROTOCOL_BINARY_CMD_DECREMENTQ
:
511 case PROTOCOL_BINARY_CMD_DELETEQ
:
512 case PROTOCOL_BINARY_CMD_FLUSHQ
:
513 case PROTOCOL_BINARY_CMD_INCREMENTQ
:
514 case PROTOCOL_BINARY_CMD_PREPENDQ
:
515 case PROTOCOL_BINARY_CMD_QUITQ
:
516 case PROTOCOL_BINARY_CMD_REPLACEQ
:
517 case PROTOCOL_BINARY_CMD_SETQ
:
518 verify("Quiet command shouldn't return on success" == NULL
);
524 case PROTOCOL_BINARY_CMD_ADD
:
525 case PROTOCOL_BINARY_CMD_REPLACE
:
526 case PROTOCOL_BINARY_CMD_SET
:
527 case PROTOCOL_BINARY_CMD_APPEND
:
528 case PROTOCOL_BINARY_CMD_PREPEND
:
529 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
530 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
531 verify(rsp
->plain
.message
.header
.response
.bodylen
== 0);
532 verify(rsp
->plain
.message
.header
.response
.cas
!= 0);
534 case PROTOCOL_BINARY_CMD_FLUSH
:
535 case PROTOCOL_BINARY_CMD_NOOP
:
536 case PROTOCOL_BINARY_CMD_QUIT
:
537 case PROTOCOL_BINARY_CMD_DELETE
:
538 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
539 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
540 verify(rsp
->plain
.message
.header
.response
.bodylen
== 0);
541 verify(rsp
->plain
.message
.header
.response
.cas
== 0);
544 case PROTOCOL_BINARY_CMD_DECREMENT
:
545 case PROTOCOL_BINARY_CMD_INCREMENT
:
546 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
547 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
548 verify(rsp
->plain
.message
.header
.response
.bodylen
== 8);
549 verify(rsp
->plain
.message
.header
.response
.cas
!= 0);
552 case PROTOCOL_BINARY_CMD_STAT
:
553 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
554 /* key and value exists in all packets except in the terminating */
555 verify(rsp
->plain
.message
.header
.response
.cas
== 0);
558 case PROTOCOL_BINARY_CMD_VERSION
:
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
!= 0);
562 verify(rsp
->plain
.message
.header
.response
.cas
== 0);
565 case PROTOCOL_BINARY_CMD_GET
:
566 case PROTOCOL_BINARY_CMD_GETQ
:
567 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
568 verify(rsp
->plain
.message
.header
.response
.extlen
== 4);
569 verify(rsp
->plain
.message
.header
.response
.cas
!= 0);
572 case PROTOCOL_BINARY_CMD_GETK
:
573 case PROTOCOL_BINARY_CMD_GETKQ
:
574 verify(rsp
->plain
.message
.header
.response
.keylen
!= 0);
575 verify(rsp
->plain
.message
.header
.response
.extlen
== 4);
576 verify(rsp
->plain
.message
.header
.response
.cas
!= 0);
580 /* Undefined command code */
586 verify(rsp
->plain
.message
.header
.response
.cas
== 0);
587 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
588 if (cc
!= PROTOCOL_BINARY_CMD_GETK
)
590 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
597 /* We call verify(validate_response_header), but that macro
598 * expects a boolean expression, and the function returns
599 * an enum.... Let's just create a macro to avoid cluttering
600 * the code with all of the == TEST_PASS ;-)
602 #define validate_response_header(a,b,c) \
603 do_validate_response_header(a,b,c) == TEST_PASS
606 static enum test_return
send_binary_noop(void)
609 raw_command(&cmd
, PROTOCOL_BINARY_CMD_NOOP
, NULL
, 0, NULL
, 0);
610 execute(send_packet(&cmd
));
614 static enum test_return
receive_binary_noop(void)
617 execute(recv_packet(&rsp
));
618 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_NOOP
,
619 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
623 static enum test_return
test_binary_noop(void)
625 execute(send_binary_noop());
626 execute(receive_binary_noop());
630 static enum test_return
test_binary_quit_impl(uint8_t cc
)
634 raw_command(&cmd
, cc
, NULL
, 0, NULL
, 0);
636 execute(send_packet(&cmd
));
637 if (cc
== PROTOCOL_BINARY_CMD_QUIT
)
639 execute(recv_packet(&rsp
));
640 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_QUIT
,
641 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
644 /* Socket should be closed now, read should return EXIT_SUCCESS */
645 verify(timeout_io_op(sock
, POLLIN
, rsp
.bytes
, sizeof(rsp
.bytes
)) == 0);
647 return TEST_PASS_RECONNECT
;
650 static enum test_return
test_binary_quit(void)
652 return test_binary_quit_impl(PROTOCOL_BINARY_CMD_QUIT
);
655 static enum test_return
test_binary_quitq(void)
657 return test_binary_quit_impl(PROTOCOL_BINARY_CMD_QUITQ
);
660 static enum test_return
test_binary_set_impl(const char* key
, uint8_t cc
)
665 uint64_t value
= 0xdeadbeefdeadcafeULL
;
666 storage_command(&cmd
, cc
, key
, strlen(key
), &value
, sizeof (value
), 0, 0);
668 /* set should always work */
669 for (int ii
= 0; ii
< 10; ii
++)
672 execute(send_packet(&cmd
));
674 execute(resend_packet(&cmd
));
676 if (cc
== PROTOCOL_BINARY_CMD_SET
)
678 execute(recv_packet(&rsp
));
679 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
682 execute(test_binary_noop());
686 * We need to get the current CAS id, and at this time we haven't
687 * verified that we have a working get
689 if (cc
== PROTOCOL_BINARY_CMD_SETQ
)
691 cmd
.set
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_SET
;
692 execute(resend_packet(&cmd
));
693 execute(recv_packet(&rsp
));
694 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_SET
,
695 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
696 cmd
.set
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_SETQ
;
699 /* try to set with the correct CAS value */
700 cmd
.plain
.message
.header
.request
.cas
= memcached_htonll(rsp
.plain
.message
.header
.response
.cas
);
701 execute(resend_packet(&cmd
));
702 if (cc
== PROTOCOL_BINARY_CMD_SET
)
704 execute(recv_packet(&rsp
));
705 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
708 execute(test_binary_noop());
710 /* try to set with an incorrect CAS value */
711 cmd
.plain
.message
.header
.request
.cas
= memcached_htonll(rsp
.plain
.message
.header
.response
.cas
- 1);
712 execute(resend_packet(&cmd
));
713 execute(send_binary_noop());
714 execute(recv_packet(&rsp
));
715 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS
));
716 execute(receive_binary_noop());
721 static enum test_return
test_binary_set(void)
723 return test_binary_set_impl("test_binary_set", PROTOCOL_BINARY_CMD_SET
);
726 static enum test_return
test_binary_setq(void)
728 return test_binary_set_impl("test_binary_setq", PROTOCOL_BINARY_CMD_SETQ
);
731 static enum test_return
test_binary_add_impl(const char* key
, uint8_t cc
)
735 uint64_t value
= 0xdeadbeefdeadcafeULL
;
736 storage_command(&cmd
, cc
, key
, strlen(key
), &value
, sizeof (value
), 0, 0);
738 /* first add should work, rest of them should fail (even with cas
740 for (int ii
=0; ii
< 10; ii
++)
743 execute(send_packet(&cmd
));
745 execute(resend_packet(&cmd
));
747 if (cc
== PROTOCOL_BINARY_CMD_ADD
|| ii
> 0)
749 uint16_t expected_result
;
751 expected_result
= PROTOCOL_BINARY_RESPONSE_SUCCESS
;
753 expected_result
= PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS
;
755 execute(send_binary_noop());
756 execute(recv_packet(&rsp
));
757 execute(receive_binary_noop());
758 verify(validate_response_header(&rsp
, cc
, expected_result
));
761 execute(test_binary_noop());
767 static enum test_return
test_binary_add(void)
769 return test_binary_add_impl("test_binary_add", PROTOCOL_BINARY_CMD_ADD
);
772 static enum test_return
test_binary_addq(void)
774 return test_binary_add_impl("test_binary_addq", PROTOCOL_BINARY_CMD_ADDQ
);
777 static enum test_return
binary_set_item(const char *key
, const char *value
)
781 storage_command(&cmd
, PROTOCOL_BINARY_CMD_SET
, key
, strlen(key
),
782 value
, strlen(value
), 0, 0);
783 execute(send_packet(&cmd
));
784 execute(recv_packet(&rsp
));
785 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_SET
,
786 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
790 static enum test_return
test_binary_replace_impl(const char* key
, uint8_t cc
)
794 uint64_t value
= 0xdeadbeefdeadcafeULL
;
795 storage_command(&cmd
, cc
, key
, strlen(key
), &value
, sizeof (value
), 0, 0);
797 /* first replace should fail, successive should succeed (when the
799 for (int ii
= 0; ii
< 10; ii
++)
802 execute(send_packet(&cmd
));
804 execute(resend_packet(&cmd
));
806 if (cc
== PROTOCOL_BINARY_CMD_REPLACE
|| ii
== 0)
808 uint16_t expected_result
;
810 expected_result
=PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
;
812 expected_result
=PROTOCOL_BINARY_RESPONSE_SUCCESS
;
814 execute(send_binary_noop());
815 execute(recv_packet(&rsp
));
816 execute(receive_binary_noop());
817 verify(validate_response_header(&rsp
, cc
, expected_result
));
820 execute(binary_set_item(key
, key
));
823 execute(test_binary_noop());
826 /* verify that replace with CAS value works! */
827 cmd
.plain
.message
.header
.request
.cas
= memcached_htonll(rsp
.plain
.message
.header
.response
.cas
);
828 execute(resend_packet(&cmd
));
830 if (cc
== PROTOCOL_BINARY_CMD_REPLACE
)
832 execute(recv_packet(&rsp
));
833 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
836 execute(test_binary_noop());
838 /* try to set with an incorrect CAS value */
839 cmd
.plain
.message
.header
.request
.cas
= memcached_htonll(rsp
.plain
.message
.header
.response
.cas
- 1);
840 execute(resend_packet(&cmd
));
841 execute(send_binary_noop());
842 execute(recv_packet(&rsp
));
843 execute(receive_binary_noop());
844 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS
));
849 static enum test_return
test_binary_replace(void)
851 return test_binary_replace_impl("test_binary_replace", PROTOCOL_BINARY_CMD_REPLACE
);
854 static enum test_return
test_binary_replaceq(void)
856 return test_binary_replace_impl("test_binary_replaceq", PROTOCOL_BINARY_CMD_REPLACEQ
);
859 static enum test_return
test_binary_delete_impl(const char *key
, uint8_t cc
)
863 raw_command(&cmd
, cc
, key
, strlen(key
), NULL
, 0);
865 /* The delete shouldn't work the first time, because the item isn't there */
866 execute(send_packet(&cmd
));
867 execute(send_binary_noop());
868 execute(recv_packet(&rsp
));
869 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
));
870 execute(receive_binary_noop());
871 execute(binary_set_item(key
, key
));
873 /* The item should be present now, resend*/
874 execute(resend_packet(&cmd
));
875 if (cc
== PROTOCOL_BINARY_CMD_DELETE
)
877 execute(recv_packet(&rsp
));
878 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
881 execute(test_binary_noop());
886 static enum test_return
test_binary_delete(void)
888 return test_binary_delete_impl("test_binary_delete", PROTOCOL_BINARY_CMD_DELETE
);
891 static enum test_return
test_binary_deleteq(void)
893 return test_binary_delete_impl("test_binary_deleteq", PROTOCOL_BINARY_CMD_DELETEQ
);
896 static enum test_return
test_binary_get_impl(const char *key
, uint8_t cc
)
901 raw_command(&cmd
, cc
, key
, strlen(key
), NULL
, 0);
902 execute(send_packet(&cmd
));
903 execute(send_binary_noop());
905 if (cc
== PROTOCOL_BINARY_CMD_GET
|| cc
== PROTOCOL_BINARY_CMD_GETK
)
907 execute(recv_packet(&rsp
));
908 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
));
911 execute(receive_binary_noop());
913 execute(binary_set_item(key
, key
));
914 execute(resend_packet(&cmd
));
915 execute(send_binary_noop());
917 execute(recv_packet(&rsp
));
918 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
919 execute(receive_binary_noop());
924 static enum test_return
test_binary_get(void)
926 return test_binary_get_impl("test_binary_get", PROTOCOL_BINARY_CMD_GET
);
929 static enum test_return
test_binary_getk(void)
931 return test_binary_get_impl("test_binary_getk", PROTOCOL_BINARY_CMD_GETK
);
934 static enum test_return
test_binary_getq(void)
936 return test_binary_get_impl("test_binary_getq", PROTOCOL_BINARY_CMD_GETQ
);
939 static enum test_return
test_binary_getkq(void)
941 return test_binary_get_impl("test_binary_getkq", PROTOCOL_BINARY_CMD_GETKQ
);
944 static enum test_return
test_binary_incr_impl(const char* key
, uint8_t cc
)
948 arithmetic_command(&cmd
, cc
, key
, strlen(key
), 1, 0, 0);
951 for (ii
= 0; ii
< 10; ++ii
)
954 execute(send_packet(&cmd
));
956 execute(resend_packet(&cmd
));
958 if (cc
== PROTOCOL_BINARY_CMD_INCREMENT
)
960 execute(recv_packet(&rsp
));
961 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
962 verify(memcached_ntohll(rsp
.incr
.message
.body
.value
) == ii
);
965 execute(test_binary_noop());
968 /* @todo add incorrect CAS */
972 static enum test_return
test_binary_incr(void)
974 return test_binary_incr_impl("test_binary_incr", PROTOCOL_BINARY_CMD_INCREMENT
);
977 static enum test_return
test_binary_incrq(void)
979 return test_binary_incr_impl("test_binary_incrq", PROTOCOL_BINARY_CMD_INCREMENTQ
);
982 static enum test_return
test_binary_decr_impl(const char* key
, uint8_t cc
)
986 arithmetic_command(&cmd
, cc
, key
, strlen(key
), 1, 9, 0);
989 for (ii
= 9; ii
> -1; --ii
)
992 execute(send_packet(&cmd
));
994 execute(resend_packet(&cmd
));
996 if (cc
== PROTOCOL_BINARY_CMD_DECREMENT
)
998 execute(recv_packet(&rsp
));
999 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1000 verify(memcached_ntohll(rsp
.decr
.message
.body
.value
) == (uint64_t)ii
);
1003 execute(test_binary_noop());
1006 /* decr 0 should not wrap */
1007 execute(resend_packet(&cmd
));
1008 if (cc
== PROTOCOL_BINARY_CMD_DECREMENT
)
1010 execute(recv_packet(&rsp
));
1011 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1012 verify(memcached_ntohll(rsp
.decr
.message
.body
.value
) == 0);
1016 /* @todo get the value and verify! */
1020 /* @todo add incorrect cas */
1021 execute(test_binary_noop());
1025 static enum test_return
test_binary_decr(void)
1027 return test_binary_decr_impl("test_binary_decr",
1028 PROTOCOL_BINARY_CMD_DECREMENT
);
1031 static enum test_return
test_binary_decrq(void)
1033 return test_binary_decr_impl("test_binary_decrq",
1034 PROTOCOL_BINARY_CMD_DECREMENTQ
);
1037 static enum test_return
test_binary_version(void)
1041 raw_command(&cmd
, PROTOCOL_BINARY_CMD_VERSION
, NULL
, 0, NULL
, 0);
1043 execute(send_packet(&cmd
));
1044 execute(recv_packet(&rsp
));
1045 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_VERSION
,
1046 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1051 static enum test_return
test_binary_flush_impl(const char *key
, uint8_t cc
)
1056 for (int ii
= 0; ii
< 2; ++ii
)
1058 execute(binary_set_item(key
, key
));
1059 flush_command(&cmd
, cc
, 0, ii
== 0);
1060 execute(send_packet(&cmd
));
1062 if (cc
== PROTOCOL_BINARY_CMD_FLUSH
)
1064 execute(recv_packet(&rsp
));
1065 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1068 execute(test_binary_noop());
1070 raw_command(&cmd
, PROTOCOL_BINARY_CMD_GET
, key
, strlen(key
), NULL
, 0);
1071 execute(send_packet(&cmd
));
1072 execute(recv_packet(&rsp
));
1073 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_GET
,
1074 PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
));
1080 static enum test_return
test_binary_flush(void)
1082 return test_binary_flush_impl("test_binary_flush", PROTOCOL_BINARY_CMD_FLUSH
);
1085 static enum test_return
test_binary_flushq(void)
1087 return test_binary_flush_impl("test_binary_flushq", PROTOCOL_BINARY_CMD_FLUSHQ
);
1090 static enum test_return
test_binary_concat_impl(const char *key
, uint8_t cc
)
1096 if (cc
== PROTOCOL_BINARY_CMD_APPEND
|| cc
== PROTOCOL_BINARY_CMD_APPENDQ
)
1101 execute(binary_set_item(key
, value
));
1103 if (cc
== PROTOCOL_BINARY_CMD_APPEND
|| cc
== PROTOCOL_BINARY_CMD_APPENDQ
)
1108 raw_command(&cmd
, cc
, key
, strlen(key
), value
, strlen(value
));
1109 execute(send_packet(&cmd
));
1110 if (cc
== PROTOCOL_BINARY_CMD_APPEND
|| cc
== PROTOCOL_BINARY_CMD_PREPEND
)
1112 execute(recv_packet(&rsp
));
1113 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1116 execute(test_binary_noop());
1118 raw_command(&cmd
, PROTOCOL_BINARY_CMD_GET
, key
, strlen(key
), NULL
, 0);
1119 execute(send_packet(&cmd
));
1120 execute(recv_packet(&rsp
));
1121 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_GET
,
1122 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1123 verify(rsp
.plain
.message
.header
.response
.bodylen
- 4 == 11);
1124 verify(memcmp(rsp
.bytes
+ 28, "hello world", 11) == 0);
1129 static enum test_return
test_binary_append(void)
1131 return test_binary_concat_impl("test_binary_append", PROTOCOL_BINARY_CMD_APPEND
);
1134 static enum test_return
test_binary_prepend(void)
1136 return test_binary_concat_impl("test_binary_prepend", PROTOCOL_BINARY_CMD_PREPEND
);
1139 static enum test_return
test_binary_appendq(void)
1141 return test_binary_concat_impl("test_binary_appendq", PROTOCOL_BINARY_CMD_APPENDQ
);
1144 static enum test_return
test_binary_prependq(void)
1146 return test_binary_concat_impl("test_binary_prependq", PROTOCOL_BINARY_CMD_PREPENDQ
);
1149 static enum test_return
test_binary_stat(void)
1154 raw_command(&cmd
, PROTOCOL_BINARY_CMD_STAT
, NULL
, 0, NULL
, 0);
1155 execute(send_packet(&cmd
));
1159 execute(recv_packet(&rsp
));
1160 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_STAT
,
1161 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1162 } while (rsp
.plain
.message
.header
.response
.keylen
!= 0);
1167 static enum test_return
send_string(const char *cmd
)
1169 execute(retry_write(cmd
, strlen(cmd
)));
1173 static enum test_return
receive_line(char *buffer
, size_t size
)
1176 while (offset
< size
)
1178 execute(retry_read(buffer
+ offset
, 1));
1179 if (buffer
[offset
] == '\n')
1181 if (offset
+ 1 < size
)
1183 buffer
[offset
+ 1]= '\0';
1195 static enum test_return
receive_response(const char *msg
) {
1197 execute(receive_line(buffer
, sizeof(buffer
)));
1198 if (strcmp(msg
, buffer
) != 0) {
1199 fprintf(stderr
, "[%s]\n", buffer
);
1201 verify(strcmp(msg
, buffer
) == 0);
1205 static enum test_return
receive_error_response(void)
1208 execute(receive_line(buffer
, sizeof(buffer
)));
1209 verify(strncmp(buffer
, "ERROR", 5) == 0 ||
1210 strncmp(buffer
, "CLIENT_ERROR", 12) == 0 ||
1211 strncmp(buffer
, "SERVER_ERROR", 12) == 0);
1215 static enum test_return
test_ascii_quit(void)
1217 /* Verify that quit handles unknown options */
1218 execute(send_string("quit foo bar\r\n"));
1219 execute(receive_error_response());
1221 /* quit doesn't support noreply */
1222 execute(send_string("quit noreply\r\n"));
1223 execute(receive_error_response());
1225 /* Verify that quit works */
1226 execute(send_string("quit\r\n"));
1228 /* Socket should be closed now, read should return EXIT_SUCCESS */
1230 verify(timeout_io_op(sock
, POLLIN
, buffer
, sizeof(buffer
)) == 0);
1231 return TEST_PASS_RECONNECT
;
1235 static enum test_return
test_ascii_version(void)
1237 /* Verify that version command handles unknown options */
1238 execute(send_string("version foo bar\r\n"));
1239 execute(receive_error_response());
1241 /* version doesn't support noreply */
1242 execute(send_string("version noreply\r\n"));
1243 execute(receive_error_response());
1245 /* Verify that verify works */
1246 execute(send_string("version\r\n"));
1248 execute(receive_line(buffer
, sizeof(buffer
)));
1249 verify(strncmp(buffer
, "VERSION ", 8) == 0);
1254 static enum test_return
test_ascii_verbosity(void)
1256 /* This command does not adhere to the spec! */
1257 execute(send_string("verbosity foo bar my\r\n"));
1258 execute(receive_error_response());
1260 execute(send_string("verbosity noreply\r\n"));
1261 execute(receive_error_response());
1263 execute(send_string("verbosity 0 noreply\r\n"));
1264 execute(test_ascii_version());
1266 execute(send_string("verbosity\r\n"));
1267 execute(receive_error_response());
1269 execute(send_string("verbosity 1\r\n"));
1270 execute(receive_response("OK\r\n"));
1272 execute(send_string("verbosity 0\r\n"));
1273 execute(receive_response("OK\r\n"));
1280 static enum test_return
test_ascii_set_impl(const char* key
, bool noreply
)
1282 /* @todo add tests for bogus format! */
1284 snprintf(buffer
, sizeof(buffer
), "set %s 0 0 5%s\r\nvalue\r\n", key
, noreply
? " noreply" : "");
1285 execute(send_string(buffer
));
1288 execute(receive_response("STORED\r\n"));
1290 return test_ascii_version();
1293 static enum test_return
test_ascii_set(void)
1295 return test_ascii_set_impl("test_ascii_set", false);
1298 static enum test_return
test_ascii_set_noreply(void)
1300 return test_ascii_set_impl("test_ascii_set_noreply", true);
1303 static enum test_return
test_ascii_add_impl(const char* key
, bool noreply
)
1305 /* @todo add tests for bogus format! */
1307 snprintf(buffer
, sizeof(buffer
), "add %s 0 0 5%s\r\nvalue\r\n", key
, noreply
? " noreply" : "");
1308 execute(send_string(buffer
));
1311 execute(receive_response("STORED\r\n"));
1313 execute(send_string(buffer
));
1316 execute(receive_response("NOT_STORED\r\n"));
1318 return test_ascii_version();
1321 static enum test_return
test_ascii_add(void)
1323 return test_ascii_add_impl("test_ascii_add", false);
1326 static enum test_return
test_ascii_add_noreply(void)
1328 return test_ascii_add_impl("test_ascii_add_noreply", true);
1331 static enum test_return
ascii_get_unknown_value(char **key
, char **value
, ssize_t
*ndata
)
1335 execute(receive_line(buffer
, sizeof(buffer
)));
1336 verify(strncmp(buffer
, "VALUE ", 6) == 0);
1337 char *end
= strchr(buffer
+ 6, ' ');
1338 verify(end
!= NULL
);
1343 *key
= strdup(buffer
+ 6);
1344 verify(*key
!= NULL
);
1347 unsigned long val
= strtoul(ptr
, &end
, 10); /* flags */
1350 verify(end
!= NULL
);
1351 *ndata
= (ssize_t
)strtoul(end
, &end
, 10); /* size */
1353 verify(end
!= NULL
);
1354 while (end
and *end
!= '\n' and isspace(*end
))
1356 verify(end
and *end
== '\n');
1358 *value
= static_cast<char*>(malloc((size_t)*ndata
));
1359 verify(*value
!= NULL
);
1361 execute(retry_read(*value
, (size_t)*ndata
));
1363 execute(retry_read(buffer
, 2));
1364 verify(memcmp(buffer
, "\r\n", 2) == 0);
1369 static enum test_return
ascii_get_value(const char *key
, const char *value
)
1373 size_t datasize
= strlen(value
);
1375 verify(datasize
< sizeof(buffer
));
1376 execute(receive_line(buffer
, sizeof(buffer
)));
1377 verify(strncmp(buffer
, "VALUE ", 6) == 0);
1378 verify(strncmp(buffer
+ 6, key
, strlen(key
)) == 0);
1379 char *ptr
= buffer
+ 6 + strlen(key
) + 1;
1382 unsigned long val
= strtoul(ptr
, &end
, 10); /* flags */
1385 verify(end
!= NULL
);
1386 val
= strtoul(end
, &end
, 10); /* size */
1388 verify(val
== datasize
);
1389 verify(end
!= NULL
);
1390 while (end
and *end
!= '\n' and isspace(*end
))
1394 verify(end
and *end
== '\n');
1396 execute(retry_read(buffer
, datasize
));
1397 verify(memcmp(buffer
, value
, datasize
) == 0);
1399 execute(retry_read(buffer
, 2));
1400 verify(memcmp(buffer
, "\r\n", 2) == 0);
1405 static enum test_return
ascii_get_item(const char *key
, const char *value
,
1411 datasize
= strlen(value
);
1413 verify(datasize
< sizeof(buffer
));
1414 snprintf(buffer
, sizeof(buffer
), "get %s\r\n", key
);
1415 execute(send_string(buffer
));
1418 execute(ascii_get_value(key
, value
));
1420 execute(retry_read(buffer
, 5));
1421 verify(memcmp(buffer
, "END\r\n", 5) == 0);
1426 static enum test_return
ascii_gets_value(const char *key
, const char *value
,
1431 size_t datasize
= strlen(value
);
1433 verify(datasize
< sizeof(buffer
));
1434 execute(receive_line(buffer
, sizeof(buffer
)));
1435 verify(strncmp(buffer
, "VALUE ", 6) == 0);
1436 verify(strncmp(buffer
+ 6, key
, strlen(key
)) == 0);
1437 char *ptr
= buffer
+ 6 + strlen(key
) + 1;
1440 unsigned long val
= strtoul(ptr
, &end
, 10); /* flags */
1443 verify(end
!= NULL
);
1444 val
= strtoul(end
, &end
, 10); /* size */
1446 verify(val
== datasize
);
1447 verify(end
!= NULL
);
1448 *cas
= strtoul(end
, &end
, 10); /* cas */
1450 verify(val
== datasize
);
1451 verify(end
!= NULL
);
1453 while (end
and *end
!= '\n' and isspace(*end
))
1457 verify(end
and *end
== '\n');
1459 execute(retry_read(buffer
, datasize
));
1460 verify(memcmp(buffer
, value
, datasize
) == 0);
1462 execute(retry_read(buffer
, 2));
1463 verify(memcmp(buffer
, "\r\n", 2) == 0);
1468 static enum test_return
ascii_gets_item(const char *key
, const char *value
,
1469 bool exist
, unsigned long *cas
)
1474 datasize
= strlen(value
);
1476 verify(datasize
< sizeof(buffer
));
1477 snprintf(buffer
, sizeof(buffer
), "gets %s\r\n", key
);
1478 execute(send_string(buffer
));
1481 execute(ascii_gets_value(key
, value
, cas
));
1483 execute(retry_read(buffer
, 5));
1484 verify(memcmp(buffer
, "END\r\n", 5) == 0);
1489 static enum test_return
ascii_set_item(const char *key
, const char *value
)
1492 size_t len
= strlen(value
);
1493 snprintf(buffer
, sizeof(buffer
), "set %s 0 0 %u\r\n", key
, (unsigned int)len
);
1494 execute(send_string(buffer
));
1495 execute(retry_write(value
, len
));
1496 execute(send_string("\r\n"));
1497 execute(receive_response("STORED\r\n"));
1501 static enum test_return
test_ascii_replace_impl(const char* key
, bool noreply
)
1504 snprintf(buffer
, sizeof(buffer
), "replace %s 0 0 5%s\r\nvalue\r\n", key
, noreply
? " noreply" : "");
1505 execute(send_string(buffer
));
1508 execute(test_ascii_version());
1510 execute(receive_response("NOT_STORED\r\n"));
1512 execute(ascii_set_item(key
, "value"));
1513 execute(ascii_get_item(key
, "value", true));
1516 execute(send_string(buffer
));
1519 execute(test_ascii_version());
1521 execute(receive_response("STORED\r\n"));
1523 return test_ascii_version();
1526 static enum test_return
test_ascii_replace(void)
1528 return test_ascii_replace_impl("test_ascii_replace", false);
1531 static enum test_return
test_ascii_replace_noreply(void)
1533 return test_ascii_replace_impl("test_ascii_replace_noreply", true);
1536 static enum test_return
test_ascii_cas_impl(const char* key
, bool noreply
)
1541 execute(ascii_set_item(key
, "value"));
1542 execute(ascii_gets_item(key
, "value", true, &cas
));
1544 snprintf(buffer
, sizeof(buffer
), "cas %s 0 0 6 %lu%s\r\nvalue2\r\n", key
, cas
, noreply
? " noreply" : "");
1545 execute(send_string(buffer
));
1548 execute(test_ascii_version());
1550 execute(receive_response("STORED\r\n"));
1552 /* reexecute the same command should fail due to illegal cas */
1553 execute(send_string(buffer
));
1556 execute(test_ascii_version());
1558 execute(receive_response("EXISTS\r\n"));
1560 return test_ascii_version();
1563 static enum test_return
test_ascii_cas(void)
1565 return test_ascii_cas_impl("test_ascii_cas", false);
1568 static enum test_return
test_ascii_cas_noreply(void)
1570 return test_ascii_cas_impl("test_ascii_cas_noreply", true);
1573 static enum test_return
test_ascii_delete_impl(const char *key
, bool noreply
)
1575 execute(ascii_set_item(key
, "value"));
1577 execute(send_string("delete\r\n"));
1578 execute(receive_error_response());
1579 /* BUG: the server accepts delete a b */
1580 execute(send_string("delete a b c d e\r\n"));
1581 execute(receive_error_response());
1584 snprintf(buffer
, sizeof(buffer
), "delete %s%s\r\n", key
, noreply
? " noreply" : "");
1585 execute(send_string(buffer
));
1588 execute(test_ascii_version());
1590 execute(receive_response("DELETED\r\n"));
1592 execute(ascii_get_item(key
, "value", false));
1593 execute(send_string(buffer
));
1595 execute(test_ascii_version());
1597 execute(receive_response("NOT_FOUND\r\n"));
1602 static enum test_return
test_ascii_delete(void)
1604 return test_ascii_delete_impl("test_ascii_delete", false);
1607 static enum test_return
test_ascii_delete_noreply(void)
1609 return test_ascii_delete_impl("test_ascii_delete_noreply", true);
1612 static enum test_return
test_ascii_get(void)
1614 execute(ascii_set_item("test_ascii_get", "value"));
1616 execute(send_string("get\r\n"));
1617 execute(receive_error_response());
1618 execute(ascii_get_item("test_ascii_get", "value", true));
1619 execute(ascii_get_item("test_ascii_get_notfound", "value", false));
1624 static enum test_return
test_ascii_gets(void)
1626 execute(ascii_set_item("test_ascii_gets", "value"));
1628 execute(send_string("gets\r\n"));
1629 execute(receive_error_response());
1631 execute(ascii_gets_item("test_ascii_gets", "value", true, &cas
));
1632 execute(ascii_gets_item("test_ascii_gets_notfound", "value", false, &cas
));
1637 static enum test_return
test_ascii_mget(void)
1639 const uint32_t nkeys
= 5;
1640 const char * const keys
[]= {
1643 /* test_ascii_mget_3 does not exist :) */
1649 for (uint32_t x
= 0; x
< nkeys
; ++x
)
1650 execute(ascii_set_item(keys
[x
], "value"));
1652 /* Ask for a key that doesn't exist as well */
1653 execute(send_string("get test_ascii_mget1 test_ascii_mget2 test_ascii_mget3 "
1654 "test_ascii_mget4 test_ascii_mget5 "
1655 "test_ascii_mget6\r\n"));
1657 char *returned
[nkeys
];
1659 for (uint32_t x
= 0; x
< nkeys
; ++x
)
1663 execute(ascii_get_unknown_value(&returned
[x
], &v
, &nbytes
));
1664 verify(nbytes
== 5);
1665 verify(memcmp(v
, "value", 5) == 0);
1670 execute(retry_read(buffer
, 5));
1671 verify(memcmp(buffer
, "END\r\n", 5) == 0);
1673 /* verify that we got all the keys we expected */
1674 for (uint32_t x
= 0; x
< nkeys
; ++x
)
1677 for (uint32_t y
= 0; y
< nkeys
; ++y
)
1679 if (strcmp(keys
[x
], returned
[y
]) == 0)
1688 for (uint32_t x
= 0; x
< nkeys
; ++x
)
1694 static enum test_return
test_ascii_incr_impl(const char* key
, bool noreply
)
1697 snprintf(cmd
, sizeof(cmd
), "incr %s 1%s\r\n", key
, noreply
? " noreply" : "");
1699 execute(ascii_set_item(key
, "0"));
1700 for (int x
= 1; x
< 11; ++x
)
1702 execute(send_string(cmd
));
1705 execute(test_ascii_version());
1709 execute(receive_line(buffer
, sizeof(buffer
)));
1710 int val
= atoi(buffer
);
1715 execute(ascii_get_item(key
, "10", true));
1720 static enum test_return
test_ascii_incr(void)
1722 return test_ascii_incr_impl("test_ascii_incr", false);
1725 static enum test_return
test_ascii_incr_noreply(void)
1727 return test_ascii_incr_impl("test_ascii_incr_noreply", true);
1730 static enum test_return
test_ascii_decr_impl(const char* key
, bool noreply
)
1733 snprintf(cmd
, sizeof(cmd
), "decr %s 1%s\r\n", key
, noreply
? " noreply" : "");
1735 execute(ascii_set_item(key
, "9"));
1736 for (int x
= 8; x
> -1; --x
)
1738 execute(send_string(cmd
));
1741 execute(test_ascii_version());
1745 execute(receive_line(buffer
, sizeof(buffer
)));
1746 int val
= atoi(buffer
);
1751 execute(ascii_get_item(key
, "0", true));
1753 /* verify that it doesn't wrap */
1754 execute(send_string(cmd
));
1756 execute(test_ascii_version());
1760 execute(receive_line(buffer
, sizeof(buffer
)));
1762 execute(ascii_get_item(key
, "0", true));
1767 static enum test_return
test_ascii_decr(void)
1769 return test_ascii_decr_impl("test_ascii_decr", false);
1772 static enum test_return
test_ascii_decr_noreply(void)
1774 return test_ascii_decr_impl("test_ascii_decr_noreply", true);
1778 static enum test_return
test_ascii_flush_impl(const char *key
, bool noreply
)
1781 /* Verify that the flush_all command handles unknown options */
1782 /* Bug in the current memcached server! */
1783 execute(send_string("flush_all foo bar\r\n"));
1784 execute(receive_error_response());
1787 execute(ascii_set_item(key
, key
));
1788 execute(ascii_get_item(key
, key
, true));
1792 execute(send_string("flush_all noreply\r\n"));
1793 execute(test_ascii_version());
1797 execute(send_string("flush_all\r\n"));
1798 execute(receive_response("OK\r\n"));
1801 execute(ascii_get_item(key
, key
, false));
1806 static enum test_return
test_ascii_flush(void)
1808 return test_ascii_flush_impl("test_ascii_flush", false);
1811 static enum test_return
test_ascii_flush_noreply(void)
1813 return test_ascii_flush_impl("test_ascii_flush_noreply", true);
1816 static enum test_return
test_ascii_concat_impl(const char *key
,
1827 execute(ascii_set_item(key
, value
));
1835 snprintf(cmd
, sizeof(cmd
), "%s %s 0 0 %u%s\r\n%s\r\n",
1836 append
? "append" : "prepend",
1837 key
, (unsigned int)strlen(value
), noreply
? " noreply" : "",
1839 execute(send_string(cmd
));
1842 execute(test_ascii_version());
1844 execute(receive_response("STORED\r\n"));
1846 execute(ascii_get_item(key
, "hello world", true));
1848 snprintf(cmd
, sizeof(cmd
), "%s %s_notfound 0 0 %u%s\r\n%s\r\n",
1849 append
? "append" : "prepend",
1850 key
, (unsigned int)strlen(value
), noreply
? " noreply" : "",
1852 execute(send_string(cmd
));
1855 execute(test_ascii_version());
1857 execute(receive_response("NOT_STORED\r\n"));
1862 static enum test_return
test_ascii_append(void)
1864 return test_ascii_concat_impl("test_ascii_append", true, false);
1867 static enum test_return
test_ascii_prepend(void)
1869 return test_ascii_concat_impl("test_ascii_prepend", false, false);
1872 static enum test_return
test_ascii_append_noreply(void)
1874 return test_ascii_concat_impl("test_ascii_append_noreply", true, true);
1877 static enum test_return
test_ascii_prepend_noreply(void)
1879 return test_ascii_concat_impl("test_ascii_prepend_noreply", false, true);
1882 static enum test_return
test_ascii_stat(void)
1884 execute(send_string("stats noreply\r\n"));
1885 execute(receive_error_response());
1886 execute(send_string("stats\r\n"));
1889 execute(receive_line(buffer
, sizeof(buffer
)));
1890 } while (strcmp(buffer
, "END\r\n") != 0);
1892 return TEST_PASS_RECONNECT
;
1895 typedef enum test_return(*TEST_FUNC
)(void);
1899 const char *description
;
1903 struct testcase testcases
[]= {
1904 { "ascii quit", test_ascii_quit
},
1905 { "ascii version", test_ascii_version
},
1906 { "ascii verbosity", test_ascii_verbosity
},
1907 { "ascii set", test_ascii_set
},
1908 { "ascii set noreply", test_ascii_set_noreply
},
1909 { "ascii get", test_ascii_get
},
1910 { "ascii gets", test_ascii_gets
},
1911 { "ascii mget", test_ascii_mget
},
1912 { "ascii flush", test_ascii_flush
},
1913 { "ascii flush noreply", test_ascii_flush_noreply
},
1914 { "ascii add", test_ascii_add
},
1915 { "ascii add noreply", test_ascii_add_noreply
},
1916 { "ascii replace", test_ascii_replace
},
1917 { "ascii replace noreply", test_ascii_replace_noreply
},
1918 { "ascii cas", test_ascii_cas
},
1919 { "ascii cas noreply", test_ascii_cas_noreply
},
1920 { "ascii delete", test_ascii_delete
},
1921 { "ascii delete noreply", test_ascii_delete_noreply
},
1922 { "ascii incr", test_ascii_incr
},
1923 { "ascii incr noreply", test_ascii_incr_noreply
},
1924 { "ascii decr", test_ascii_decr
},
1925 { "ascii decr noreply", test_ascii_decr_noreply
},
1926 { "ascii append", test_ascii_append
},
1927 { "ascii append noreply", test_ascii_append_noreply
},
1928 { "ascii prepend", test_ascii_prepend
},
1929 { "ascii prepend noreply", test_ascii_prepend_noreply
},
1930 { "ascii stat", test_ascii_stat
},
1931 { "binary noop", test_binary_noop
},
1932 { "binary quit", test_binary_quit
},
1933 { "binary quitq", test_binary_quitq
},
1934 { "binary set", test_binary_set
},
1935 { "binary setq", test_binary_setq
},
1936 { "binary flush", test_binary_flush
},
1937 { "binary flushq", test_binary_flushq
},
1938 { "binary add", test_binary_add
},
1939 { "binary addq", test_binary_addq
},
1940 { "binary replace", test_binary_replace
},
1941 { "binary replaceq", test_binary_replaceq
},
1942 { "binary delete", test_binary_delete
},
1943 { "binary deleteq", test_binary_deleteq
},
1944 { "binary get", test_binary_get
},
1945 { "binary getq", test_binary_getq
},
1946 { "binary getk", test_binary_getk
},
1947 { "binary getkq", test_binary_getkq
},
1948 { "binary incr", test_binary_incr
},
1949 { "binary incrq", test_binary_incrq
},
1950 { "binary decr", test_binary_decr
},
1951 { "binary decrq", test_binary_decrq
},
1952 { "binary version", test_binary_version
},
1953 { "binary append", test_binary_append
},
1954 { "binary appendq", test_binary_appendq
},
1955 { "binary prepend", test_binary_prepend
},
1956 { "binary prependq", test_binary_prependq
},
1957 { "binary stat", test_binary_stat
},
1961 const int ascii_tests
= 1;
1962 const int binary_tests
= 2;
1970 int main(int argc
, char **argv
)
1972 static const char * const status_msg
[]= {"[skip]", "[pass]", "[pass]", "[FAIL]"};
1973 struct test_type_st tests
= { true, true };
1976 const char *hostname
= "localhost";
1977 const char *port
= "11211";
1980 const char *testname
= NULL
;
1984 while ((cmd
= getopt(argc
, argv
, "qt:vch:p:PT:?ab")) != EOF
)
1989 tests
.binary
= false;
1998 timeout
= atoi(optarg
);
2001 fprintf(stderr
, "Invalid timeout. Please specify a number for -t\n");
2002 return EXIT_FAILURE
;
2006 case 'v': verbose
= true;
2009 case 'c': do_core
= true;
2012 case 'h': hostname
= optarg
;
2015 case 'p': port
= optarg
;
2022 case 'P': prompt
= true;
2025 case 'T': testname
= optarg
;
2029 fprintf(stderr
, "Usage: %s [-h hostname] [-p port] [-c] [-v] [-t n] [-P] [-T testname]'\n"
2030 "\t-c\tGenerate coredump if a test fails\n"
2031 "\t-v\tVerbose test output (print out the assertion)\n"
2032 "\t-t n\tSet the timeout for io-operations to n seconds\n"
2033 "\t-P\tPrompt the user before starting a test.\n"
2034 "\t\t\t\"skip\" will skip the test\n"
2035 "\t\t\t\"quit\" will terminate memcapable\n"
2036 "\t\t\tEverything else will start the test\n"
2037 "\t-T n\tJust run the test named n\n"
2038 "\t-a\tOnly test the ascii protocol\n"
2039 "\t-b\tOnly test the binary protocol\n",
2041 return EXIT_FAILURE
;
2045 initialize_sockets();
2046 sock
= connect_server(hostname
, port
);
2047 if (sock
== INVALID_SOCKET
)
2049 fprintf(stderr
, "Failed to connect to <%s:%s>: %s\n",
2050 hostname
, port
, strerror(get_socket_errno()));
2051 return EXIT_FAILURE
;
2054 for (int ii
= 0; testcases
[ii
].description
!= NULL
; ++ii
)
2056 if (testname
!= NULL
&& strcmp(testcases
[ii
].description
, testname
) != 0)
2059 if ((testcases
[ii
].description
[0] == 'a' && (tests
.ascii
) == 0) ||
2060 (testcases
[ii
].description
[0] == 'b' && (tests
.binary
) == 0))
2065 fprintf(stdout
, "%-40s", testcases
[ii
].description
);
2070 fprintf(stdout
, "\nPress <return> when you are ready? ");
2071 char buffer
[80] = {0};
2072 if (fgets(buffer
, sizeof(buffer
), stdin
) != NULL
) {
2073 if (strncmp(buffer
, "skip", 4) == 0)
2075 fprintf(stdout
, "%-40s%s\n", testcases
[ii
].description
,
2076 status_msg
[TEST_SKIP
]);
2080 if (strncmp(buffer
, "quit", 4) == 0)
2084 fprintf(stdout
, "%-40s", testcases
[ii
].description
);
2088 bool reconnect
= false;
2089 enum test_return ret
= testcases
[ii
].function();
2090 if (ret
== TEST_FAIL
)
2095 fprintf(stderr
, "\n");
2097 else if (ret
== TEST_PASS_RECONNECT
)
2100 fprintf(stderr
, "%s\n", status_msg
[ret
]);
2104 if ((sock
= connect_server(hostname
, port
)) == INVALID_SOCKET
)
2106 fprintf(stderr
, "Failed to connect to <%s:%s>: %s\n",
2107 hostname
, port
, strerror(get_socket_errno()));
2108 fprintf(stderr
, "%d of %d tests failed\n", failed
, total
);
2109 return EXIT_FAILURE
;
2116 fprintf(stdout
, "All tests passed\n");
2118 fprintf(stderr
, "%d of %d tests failed\n", failed
, total
);
2120 return (failed
== 0) ? 0 : 1;