2 +--------------------------------------------------------------------+
3 | libmemcached-awesome - C/C++ Client Library for memcached |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted under the terms of the BSD license. |
7 | You should have received a copy of the license in a bundled file |
8 | named LICENSE; in case you did not receive a copy you can review |
9 | the terms online at: https://opensource.org/licenses/BSD-3-Clause |
10 +--------------------------------------------------------------------+
11 | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ |
12 | Copyright (c) 2020-2021 Michael Wallner https://awesome.co/ |
13 +--------------------------------------------------------------------+
18 #include "mem_config.h"
30 #include <sys/types.h>
35 #include "p9y/getopt.hpp"
36 #include "p9y/socket.hpp"
37 #include "p9y/poll.hpp"
39 #include "libmemcached-1.0/memcached.h"
40 #include "libmemcachedprotocol-0.0/binary.h"
41 #include "libmemcached/byteorder.h"
46 /* /usr/include/netinet/in.h defines macros from ntohs() to _bswap_nn to
47 * optimize the conversion functions, but the prototypes generate warnings
48 * from gcc. The conversion methods isn't the bottleneck for my app, so
49 * just remove the warnings by undef'ing the optimization ..
55 /* Should we generate coredumps when we enounter an error (-c) */
56 static bool do_core
= false;
57 /* connection to the server */
58 static memcached_socket_t sock
;
59 /* Should the output from test failures be verbose or quiet? */
60 static bool verbose
= false;
62 /* The number of seconds to wait for an IO-operation */
63 static int timeout
= 2;
65 /* v1.6.x is more permissible */
66 static bool v16x_or_greater
= false;
69 * Instead of having to cast between the different datatypes we create
70 * a union of all of the different types of pacages we want to send.
71 * A lot of the different commands use the same packet layout, so I'll
72 * just define the different types I need. The typedefs only contain
73 * the header of the message, so we need some space for keys and body
74 * To avoid to have to do multiple writes, lets add a chunk of memory
75 * to use. 1k should be more than enough for header, key and body.
78 protocol_binary_request_no_extras plain
;
79 protocol_binary_request_flush flush
;
80 protocol_binary_request_incr incr
;
81 protocol_binary_request_set set
;
86 protocol_binary_response_no_extras plain
;
87 protocol_binary_response_incr incr
;
88 protocol_binary_response_decr decr
;
92 enum test_return
{ TEST_SKIP
, TEST_PASS
, TEST_PASS_RECONNECT
, TEST_FAIL
};
95 * Try to get an addrinfo struct for a given port on a given host
97 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
);
107 if (error
!= EAI_SYSTEM
) {
108 fprintf(stderr
, "getaddrinfo(): %s\n", gai_strerror(error
));
110 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) {
124 if (ioctlsocket(sock
, FIONBIO
, &arg
) == SOCKET_ERROR
) {
125 perror("Failed to set nonblocking io");
127 return INVALID_SOCKET
;
130 int flags
= fcntl(sock
, F_GETFL
, 0);
132 perror("Failed to get socket flags");
134 return INVALID_SOCKET
;
137 if ((flags
& O_NONBLOCK
) != O_NONBLOCK
) {
138 if (fcntl(sock
, F_SETFL
, flags
| O_NONBLOCK
) == -1) {
139 perror("Failed to set socket to nonblocking mode");
141 return INVALID_SOCKET
;
149 * Try to open a connection to the server
150 * @param hostname the name of the server to connect to
151 * @param port the port number (or service) to connect to
152 * @return positive integer if success, -1 otherwise
154 static memcached_socket_t
connect_server(const char *hostname
, const char *port
) {
155 struct addrinfo
*ai
= lookuphost(hostname
, port
);
156 sock
= INVALID_SOCKET
;
158 if ((sock
= socket(ai
->ai_family
, ai
->ai_socktype
, ai
->ai_protocol
)) != INVALID_SOCKET
) {
159 if (connect(sock
, ai
->ai_addr
, ai
->ai_addrlen
) == SOCKET_ERROR
) {
160 fprintf(stderr
, "Failed to connect socket: %s\n", strerror(get_socket_errno()));
162 sock
= INVALID_SOCKET
;
164 sock
= set_noblock();
167 fprintf(stderr
, "Failed to create socket: %s\n", strerror(get_socket_errno()));
176 static ssize_t
timeout_io_op(memcached_socket_t fd
, short direction
, const char *buf
, size_t len
) {
179 if (direction
== POLLOUT
) {
180 ret
= send(fd
, buf
, len
, 0);
182 ret
= recv(fd
, const_cast<char *>(buf
), len
, 0);
184 int local_errno
= get_socket_errno();
185 if (ret
== SOCKET_ERROR
&& local_errno
== EWOULDBLOCK
|| (EAGAIN
!= EWOULDBLOCK
&& local_errno
== EAGAIN
)) {
187 memset(&fds
, 0, sizeof(struct pollfd
));
188 fds
.events
= direction
;
191 int err
= poll(&fds
, 1, timeout
* 1000);
193 if (direction
== POLLOUT
) {
194 ret
= send(fd
, buf
, len
, 0);
196 ret
= recv(fd
, const_cast<char *>(buf
), len
, 0);
198 } else if (err
== 0) {
201 perror("Failed to poll");
210 * Ensure that an expression is true. If it isn't print out a message similar
211 * to assert() and create a coredump if the user wants that. If not an error
212 * message is returned.
215 static enum test_return
ensure(bool val
, const char *expression
, const char *file
, int line
) {
218 fprintf(stdout
, "\n%s:%d: %s", file
, line
, expression
);
231 #define verify(expression) \
233 if (ensure(expression, #expression, __FILE__, __LINE__) == TEST_FAIL) \
236 #define execute(expression) \
238 if (ensure(expression == TEST_PASS, #expression, __FILE__, __LINE__) == TEST_FAIL) \
243 * Send a chunk of memory over the socket (retry if the call is iterrupted
245 static enum test_return
retry_write(const void *buf
, size_t len
) {
247 const char *ptr
= static_cast<const char *>(buf
);
250 size_t num_bytes
= len
- offset
;
251 ssize_t nw
= timeout_io_op(sock
, POLLOUT
, (ptr
+ offset
), num_bytes
);
253 verify(get_socket_errno() == EINTR
|| get_socket_errno() == EAGAIN
);
255 offset
+= (size_t) nw
;
258 } while (offset
< len
);
264 * Resend a packet to the server (All fields in the command header should
265 * be in network byte order)
267 static enum test_return
resend_packet(command
*cmd
) {
269 sizeof(protocol_binary_request_no_extras
) + ntohl(cmd
->plain
.message
.header
.request
.bodylen
);
271 execute(retry_write(cmd
, length
));
276 * Send a command to the server. The command header needs to be updated
277 * to network byte order
279 static enum test_return
send_packet(command
*cmd
) {
280 /* Fix the byteorder of the header */
281 cmd
->plain
.message
.header
.request
.keylen
= ntohs(cmd
->plain
.message
.header
.request
.keylen
);
282 cmd
->plain
.message
.header
.request
.bodylen
= ntohl(cmd
->plain
.message
.header
.request
.bodylen
);
283 cmd
->plain
.message
.header
.request
.cas
= memcached_ntohll(cmd
->plain
.message
.header
.request
.cas
);
285 execute(resend_packet(cmd
));
290 * Read a fixed length chunk of data from the server
292 static enum test_return
retry_read(void *buf
, size_t len
) {
295 ssize_t nr
= timeout_io_op(sock
, POLLIN
, ((char *) buf
) + offset
, len
- offset
);
298 fprintf(stderr
, "Errno: %d %s\n", get_socket_errno(), strerror(errno
));
299 verify(get_socket_errno() == EINTR
|| get_socket_errno() == EAGAIN
);
306 offset
+= (size_t) nr
;
308 } while (offset
< len
);
314 * Receive a response from the server and conver the fields in the header
315 * to local byte order
317 static enum test_return
recv_packet(response
*rsp
) {
318 execute(retry_read(rsp
, sizeof(protocol_binary_response_no_extras
)));
320 /* Fix the byte order in the packet header */
321 rsp
->plain
.message
.header
.response
.keylen
= ntohs(rsp
->plain
.message
.header
.response
.keylen
);
322 rsp
->plain
.message
.header
.response
.status
= ntohs(rsp
->plain
.message
.header
.response
.status
);
323 rsp
->plain
.message
.header
.response
.bodylen
= ntohl(rsp
->plain
.message
.header
.response
.bodylen
);
324 rsp
->plain
.message
.header
.response
.cas
= memcached_ntohll(rsp
->plain
.message
.header
.response
.cas
);
326 size_t bodysz
= rsp
->plain
.message
.header
.response
.bodylen
;
328 execute(retry_read(rsp
->bytes
+ sizeof(protocol_binary_response_no_extras
), bodysz
));
334 * Create a storage command (add, set, replace etc)
336 * @param cmd destination buffer
337 * @param cc the storage command to create
338 * @param key the key to store
339 * @param keylen the length of the key
340 * @param dta the data to store with the key
341 * @param dtalen the length of the data to store with the key
342 * @param flags the flags to store along with the key
343 * @param exptime the expiry time for the key
345 static void storage_command(command
*cmd
, uint8_t cc
, const void *key
, size_t keylen
,
346 const void *dta
, size_t dtalen
, uint32_t flags
, uint32_t exptime
) {
347 /* all of the storage commands use the same command layout */
348 protocol_binary_request_set
*request
= &cmd
->set
;
350 memset(request
, 0, sizeof(*request
));
351 request
->message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
352 request
->message
.header
.request
.opcode
= cc
;
353 request
->message
.header
.request
.keylen
= (uint16_t) keylen
;
354 request
->message
.header
.request
.extlen
= 8;
355 request
->message
.header
.request
.bodylen
= (uint32_t)(keylen
+ 8 + dtalen
);
356 request
->message
.header
.request
.opaque
= 0xdeadbeef;
357 request
->message
.body
.flags
= flags
;
358 request
->message
.body
.expiration
= exptime
;
360 off_t key_offset
= sizeof(protocol_binary_request_no_extras
) + 8;
361 memcpy(cmd
->bytes
+ key_offset
, key
, keylen
);
363 memcpy(cmd
->bytes
+ key_offset
+ keylen
, dta
, dtalen
);
367 * Create a basic command to send to the server
368 * @param cmd destination buffer
369 * @param cc the command to create
370 * @param key the key to store
371 * @param keylen the length of the key
372 * @param dta the data to store with the key
373 * @param dtalen the length of the data to store with the key
375 static void raw_command(command
*cmd
, uint8_t cc
, const void *key
, size_t keylen
, const void *dta
,
377 /* all of the storage commands use the same command layout */
378 memset(cmd
, 0, sizeof(*cmd
));
379 cmd
->plain
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
380 cmd
->plain
.message
.header
.request
.opcode
= cc
;
381 cmd
->plain
.message
.header
.request
.keylen
= (uint16_t) keylen
;
382 cmd
->plain
.message
.header
.request
.bodylen
= (uint32_t)(keylen
+ dtalen
);
383 cmd
->plain
.message
.header
.request
.opaque
= 0xdeadbeef;
385 off_t key_offset
= sizeof(protocol_binary_request_no_extras
);
388 memcpy(cmd
->bytes
+ key_offset
, key
, keylen
);
391 memcpy(cmd
->bytes
+ key_offset
+ keylen
, dta
, dtalen
);
395 * Create the flush command
396 * @param cmd destination buffer
397 * @param cc the command to create (FLUSH/FLUSHQ)
398 * @param exptime when to flush
399 * @param use_extra to force using of the extra field?
401 static void flush_command(command
*cmd
, uint8_t cc
, uint32_t exptime
, bool use_extra
) {
402 memset(cmd
, 0, sizeof(cmd
->flush
));
403 cmd
->flush
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
404 cmd
->flush
.message
.header
.request
.opcode
= cc
;
405 cmd
->flush
.message
.header
.request
.opaque
= 0xdeadbeef;
407 if (exptime
|| use_extra
) {
408 cmd
->flush
.message
.header
.request
.extlen
= 4;
409 cmd
->flush
.message
.body
.expiration
= htonl(exptime
);
410 cmd
->flush
.message
.header
.request
.bodylen
= 4;
415 * Create a incr/decr command
416 * @param cc the cmd to create (FLUSH/FLUSHQ)
417 * @param key the key to operate on
418 * @param keylen the number of bytes in the key
419 * @param delta the number to add/subtract
420 * @param initial the initial value if the key doesn't exist
421 * @param exptime when the key should expire if it isn't set
423 static void arithmetic_command(command
*cmd
, uint8_t cc
, const void *key
, size_t keylen
,
424 uint64_t delta
, uint64_t initial
, uint32_t exptime
) {
425 memset(cmd
, 0, sizeof(cmd
->incr
));
426 cmd
->incr
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
427 cmd
->incr
.message
.header
.request
.opcode
= cc
;
428 cmd
->incr
.message
.header
.request
.keylen
= (uint16_t) keylen
;
429 cmd
->incr
.message
.header
.request
.extlen
= 20;
430 cmd
->incr
.message
.header
.request
.bodylen
= (uint32_t)(keylen
+ 20);
431 cmd
->incr
.message
.header
.request
.opaque
= 0xdeadbeef;
432 cmd
->incr
.message
.body
.delta
= memcached_htonll(delta
);
433 cmd
->incr
.message
.body
.initial
= memcached_htonll(initial
);
434 cmd
->incr
.message
.body
.expiration
= htonl(exptime
);
436 off_t key_offset
= sizeof(protocol_binary_request_no_extras
) + 20;
437 memcpy(cmd
->bytes
+ key_offset
, key
, keylen
);
441 * Validate the response header from the server
442 * @param rsp the response to check
443 * @param cc the expected command
444 * @param status the expected status
446 static enum test_return
do_validate_response_header(response
*rsp
, uint8_t cc
, uint16_t status
) {
447 verify(rsp
->plain
.message
.header
.response
.magic
== PROTOCOL_BINARY_RES
);
448 verify(rsp
->plain
.message
.header
.response
.opcode
== cc
);
449 verify(rsp
->plain
.message
.header
.response
.datatype
== PROTOCOL_BINARY_RAW_BYTES
);
450 verify(rsp
->plain
.message
.header
.response
.status
== status
);
451 verify(rsp
->plain
.message
.header
.response
.opaque
== 0xdeadbeef);
453 if (status
== PROTOCOL_BINARY_RESPONSE_SUCCESS
) {
455 case PROTOCOL_BINARY_CMD_ADDQ
:
456 case PROTOCOL_BINARY_CMD_APPENDQ
:
457 case PROTOCOL_BINARY_CMD_DECREMENTQ
:
458 case PROTOCOL_BINARY_CMD_DELETEQ
:
459 case PROTOCOL_BINARY_CMD_FLUSHQ
:
460 case PROTOCOL_BINARY_CMD_INCREMENTQ
:
461 case PROTOCOL_BINARY_CMD_PREPENDQ
:
462 case PROTOCOL_BINARY_CMD_QUITQ
:
463 case PROTOCOL_BINARY_CMD_REPLACEQ
:
464 case PROTOCOL_BINARY_CMD_SETQ
:
465 verify("Quiet command shouldn't return on success" == NULL
);
472 case PROTOCOL_BINARY_CMD_ADD
:
473 case PROTOCOL_BINARY_CMD_REPLACE
:
474 case PROTOCOL_BINARY_CMD_SET
:
475 case PROTOCOL_BINARY_CMD_APPEND
:
476 case PROTOCOL_BINARY_CMD_PREPEND
:
477 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
478 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
479 verify(rsp
->plain
.message
.header
.response
.bodylen
== 0);
480 verify(rsp
->plain
.message
.header
.response
.cas
);
482 case PROTOCOL_BINARY_CMD_FLUSH
:
483 case PROTOCOL_BINARY_CMD_NOOP
:
484 case PROTOCOL_BINARY_CMD_QUIT
:
485 case PROTOCOL_BINARY_CMD_DELETE
:
486 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
487 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
488 verify(rsp
->plain
.message
.header
.response
.bodylen
== 0);
489 verify(rsp
->plain
.message
.header
.response
.cas
== 0);
492 case PROTOCOL_BINARY_CMD_DECREMENT
:
493 case PROTOCOL_BINARY_CMD_INCREMENT
:
494 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
495 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
496 verify(rsp
->plain
.message
.header
.response
.bodylen
== 8);
497 verify(rsp
->plain
.message
.header
.response
.cas
);
500 case PROTOCOL_BINARY_CMD_STAT
:
501 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
502 /* key and value exists in all packets except in the terminating */
503 verify(rsp
->plain
.message
.header
.response
.cas
== 0);
506 case PROTOCOL_BINARY_CMD_VERSION
:
507 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
508 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
509 verify(rsp
->plain
.message
.header
.response
.bodylen
);
510 verify(rsp
->plain
.message
.header
.response
.cas
== 0);
513 case PROTOCOL_BINARY_CMD_GET
:
514 case PROTOCOL_BINARY_CMD_GETQ
:
515 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
516 verify(rsp
->plain
.message
.header
.response
.extlen
== 4);
517 verify(rsp
->plain
.message
.header
.response
.cas
);
520 case PROTOCOL_BINARY_CMD_GETK
:
521 case PROTOCOL_BINARY_CMD_GETKQ
:
522 verify(rsp
->plain
.message
.header
.response
.keylen
);
523 verify(rsp
->plain
.message
.header
.response
.extlen
== 4);
524 verify(rsp
->plain
.message
.header
.response
.cas
);
528 /* Undefined command code */
532 verify(rsp
->plain
.message
.header
.response
.cas
== 0);
533 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
534 if (cc
!= PROTOCOL_BINARY_CMD_GETK
) {
535 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
542 /* We call verify(validate_response_header), but that macro
543 * expects a boolean expression, and the function returns
544 * an enum.... Let's just create a macro to avoid cluttering
545 * the code with all of the == TEST_PASS ;-)
547 #define validate_response_header(a, b, c) do_validate_response_header(a, b, c) == TEST_PASS
549 static enum test_return
send_binary_noop(void) {
551 raw_command(&cmd
, PROTOCOL_BINARY_CMD_NOOP
, NULL
, 0, NULL
, 0);
552 execute(send_packet(&cmd
));
556 static enum test_return
receive_binary_noop(void) {
558 execute(recv_packet(&rsp
));
560 validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_NOOP
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
564 static enum test_return
test_binary_noop(void) {
565 execute(send_binary_noop());
566 execute(receive_binary_noop());
570 static enum test_return
test_binary_quit_impl(uint8_t cc
) {
573 raw_command(&cmd
, cc
, NULL
, 0, NULL
, 0);
575 execute(send_packet(&cmd
));
576 if (cc
== PROTOCOL_BINARY_CMD_QUIT
) {
577 execute(recv_packet(&rsp
));
579 validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_QUIT
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
582 /* Socket should be closed now, read should return EXIT_SUCCESS */
583 verify(timeout_io_op(sock
, POLLIN
, rsp
.bytes
, sizeof(rsp
.bytes
)) == 0);
585 return TEST_PASS_RECONNECT
;
588 static enum test_return
test_binary_quit(void) {
589 return test_binary_quit_impl(PROTOCOL_BINARY_CMD_QUIT
);
592 static enum test_return
test_binary_quitq(void) {
593 return test_binary_quit_impl(PROTOCOL_BINARY_CMD_QUITQ
);
596 static enum test_return
test_binary_set_impl(const char *key
, uint8_t cc
) {
600 uint64_t value
= 0xdeadbeefdeadcafeULL
;
601 storage_command(&cmd
, cc
, key
, strlen(key
), &value
, sizeof(value
), 0, 0);
603 /* set should always work */
604 for (int ii
= 0; ii
< 10; ii
++) {
606 execute(send_packet(&cmd
));
608 execute(resend_packet(&cmd
));
611 if (cc
== PROTOCOL_BINARY_CMD_SET
) {
612 execute(recv_packet(&rsp
));
613 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
615 execute(test_binary_noop());
619 * We need to get the current CAS id, and at this time we haven't
620 * verified that we have a working get
622 if (cc
== PROTOCOL_BINARY_CMD_SETQ
) {
623 cmd
.set
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_SET
;
624 execute(resend_packet(&cmd
));
625 execute(recv_packet(&rsp
));
627 validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_SET
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
628 cmd
.set
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_SETQ
;
631 /* try to set with the correct CAS value */
632 cmd
.plain
.message
.header
.request
.cas
= memcached_htonll(rsp
.plain
.message
.header
.response
.cas
);
633 execute(resend_packet(&cmd
));
634 if (cc
== PROTOCOL_BINARY_CMD_SET
) {
635 execute(recv_packet(&rsp
));
636 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
638 execute(test_binary_noop());
640 /* try to set with an incorrect CAS value */
641 cmd
.plain
.message
.header
.request
.cas
=
642 memcached_htonll(rsp
.plain
.message
.header
.response
.cas
- 1);
643 execute(resend_packet(&cmd
));
644 execute(send_binary_noop());
645 execute(recv_packet(&rsp
));
646 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS
));
647 execute(receive_binary_noop());
652 static enum test_return
test_binary_set(void) {
653 return test_binary_set_impl("test_binary_set", PROTOCOL_BINARY_CMD_SET
);
656 static enum test_return
test_binary_setq(void) {
657 return test_binary_set_impl("test_binary_setq", PROTOCOL_BINARY_CMD_SETQ
);
660 static enum test_return
test_binary_add_impl(const char *key
, uint8_t cc
) {
663 uint64_t value
= 0xdeadbeefdeadcafeULL
;
664 storage_command(&cmd
, cc
, key
, strlen(key
), &value
, sizeof(value
), 0, 0);
666 /* first add should work, rest of them should fail (even with cas
668 for (int ii
= 0; ii
< 10; ii
++) {
670 execute(send_packet(&cmd
));
672 execute(resend_packet(&cmd
));
674 if (cc
== PROTOCOL_BINARY_CMD_ADD
|| ii
> 0) {
675 uint16_t expected_result
;
677 expected_result
= PROTOCOL_BINARY_RESPONSE_SUCCESS
;
679 expected_result
= PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS
;
681 execute(send_binary_noop());
682 execute(recv_packet(&rsp
));
683 execute(receive_binary_noop());
684 verify(validate_response_header(&rsp
, cc
, expected_result
));
686 execute(test_binary_noop());
692 static enum test_return
test_binary_add(void) {
693 return test_binary_add_impl("test_binary_add", PROTOCOL_BINARY_CMD_ADD
);
696 static enum test_return
test_binary_addq(void) {
697 return test_binary_add_impl("test_binary_addq", PROTOCOL_BINARY_CMD_ADDQ
);
700 static enum test_return
binary_set_item(const char *key
, const char *value
) {
703 storage_command(&cmd
, PROTOCOL_BINARY_CMD_SET
, key
, strlen(key
), value
, strlen(value
), 0, 0);
704 execute(send_packet(&cmd
));
705 execute(recv_packet(&rsp
));
706 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_SET
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
710 static enum test_return
test_binary_replace_impl(const char *key
, uint8_t cc
) {
713 uint64_t value
= 0xdeadbeefdeadcafeULL
;
714 storage_command(&cmd
, cc
, key
, strlen(key
), &value
, sizeof(value
), 0, 0);
716 /* first replace should fail, successive should succeed (when the
718 for (int ii
= 0; ii
< 10; ii
++) {
720 execute(send_packet(&cmd
));
722 execute(resend_packet(&cmd
));
725 if (cc
== PROTOCOL_BINARY_CMD_REPLACE
|| ii
== 0) {
726 uint16_t expected_result
;
728 expected_result
= PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
;
730 expected_result
= PROTOCOL_BINARY_RESPONSE_SUCCESS
;
733 execute(send_binary_noop());
734 execute(recv_packet(&rsp
));
735 execute(receive_binary_noop());
736 verify(validate_response_header(&rsp
, cc
, expected_result
));
739 execute(binary_set_item(key
, key
));
741 execute(test_binary_noop());
745 /* verify that replace with CAS value works! */
746 cmd
.plain
.message
.header
.request
.cas
= memcached_htonll(rsp
.plain
.message
.header
.response
.cas
);
747 execute(resend_packet(&cmd
));
749 if (cc
== PROTOCOL_BINARY_CMD_REPLACE
) {
750 execute(recv_packet(&rsp
));
751 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
753 execute(test_binary_noop());
755 /* try to set with an incorrect CAS value */
756 cmd
.plain
.message
.header
.request
.cas
=
757 memcached_htonll(rsp
.plain
.message
.header
.response
.cas
- 1);
758 execute(resend_packet(&cmd
));
759 execute(send_binary_noop());
760 execute(recv_packet(&rsp
));
761 execute(receive_binary_noop());
762 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS
));
767 static enum test_return
test_binary_replace(void) {
768 return test_binary_replace_impl("test_binary_replace", PROTOCOL_BINARY_CMD_REPLACE
);
771 static enum test_return
test_binary_replaceq(void) {
772 return test_binary_replace_impl("test_binary_replaceq", PROTOCOL_BINARY_CMD_REPLACEQ
);
775 static enum test_return
test_binary_delete_impl(const char *key
, uint8_t cc
) {
778 raw_command(&cmd
, cc
, key
, strlen(key
), NULL
, 0);
780 /* The delete shouldn't work the first time, because the item isn't there */
781 execute(send_packet(&cmd
));
782 execute(send_binary_noop());
783 execute(recv_packet(&rsp
));
784 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
));
785 execute(receive_binary_noop());
786 execute(binary_set_item(key
, key
));
788 /* The item should be present now, resend*/
789 execute(resend_packet(&cmd
));
790 if (cc
== PROTOCOL_BINARY_CMD_DELETE
) {
791 execute(recv_packet(&rsp
));
792 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
795 execute(test_binary_noop());
800 static enum test_return
test_binary_delete(void) {
801 return test_binary_delete_impl("test_binary_delete", PROTOCOL_BINARY_CMD_DELETE
);
804 static enum test_return
test_binary_deleteq(void) {
805 return test_binary_delete_impl("test_binary_deleteq", PROTOCOL_BINARY_CMD_DELETEQ
);
808 static enum test_return
test_binary_get_impl(const char *key
, uint8_t cc
) {
812 raw_command(&cmd
, cc
, key
, strlen(key
), NULL
, 0);
813 execute(send_packet(&cmd
));
814 execute(send_binary_noop());
816 if (cc
== PROTOCOL_BINARY_CMD_GET
|| cc
== PROTOCOL_BINARY_CMD_GETK
) {
817 execute(recv_packet(&rsp
));
818 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
));
821 execute(receive_binary_noop());
823 execute(binary_set_item(key
, key
));
824 execute(resend_packet(&cmd
));
825 execute(send_binary_noop());
827 execute(recv_packet(&rsp
));
828 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
829 execute(receive_binary_noop());
834 static enum test_return
test_binary_get(void) {
835 return test_binary_get_impl("test_binary_get", PROTOCOL_BINARY_CMD_GET
);
838 static enum test_return
test_binary_getk(void) {
839 return test_binary_get_impl("test_binary_getk", PROTOCOL_BINARY_CMD_GETK
);
842 static enum test_return
test_binary_getq(void) {
843 return test_binary_get_impl("test_binary_getq", PROTOCOL_BINARY_CMD_GETQ
);
846 static enum test_return
test_binary_getkq(void) {
847 return test_binary_get_impl("test_binary_getkq", PROTOCOL_BINARY_CMD_GETKQ
);
850 static enum test_return
test_binary_incr_impl(const char *key
, uint8_t cc
) {
853 arithmetic_command(&cmd
, cc
, key
, strlen(key
), 1, 0, 0);
856 for (ii
= 0; ii
< 10; ++ii
) {
858 execute(send_packet(&cmd
));
860 execute(resend_packet(&cmd
));
862 if (cc
== PROTOCOL_BINARY_CMD_INCREMENT
) {
863 execute(recv_packet(&rsp
));
864 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
865 verify(memcached_ntohll(rsp
.incr
.message
.body
.value
) == ii
);
867 execute(test_binary_noop());
870 /* @todo add incorrect CAS */
874 static enum test_return
test_binary_incr(void) {
875 return test_binary_incr_impl("test_binary_incr", PROTOCOL_BINARY_CMD_INCREMENT
);
878 static enum test_return
test_binary_incrq(void) {
879 return test_binary_incr_impl("test_binary_incrq", PROTOCOL_BINARY_CMD_INCREMENTQ
);
882 static enum test_return
test_binary_decr_impl(const char *key
, uint8_t cc
) {
885 arithmetic_command(&cmd
, cc
, key
, strlen(key
), 1, 9, 0);
888 for (ii
= 9; ii
> -1; --ii
) {
890 execute(send_packet(&cmd
));
892 execute(resend_packet(&cmd
));
894 if (cc
== PROTOCOL_BINARY_CMD_DECREMENT
) {
895 execute(recv_packet(&rsp
));
896 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
897 verify(memcached_ntohll(rsp
.decr
.message
.body
.value
) == (uint64_t) ii
);
899 execute(test_binary_noop());
902 /* decr 0 should not wrap */
903 execute(resend_packet(&cmd
));
904 if (cc
== PROTOCOL_BINARY_CMD_DECREMENT
) {
905 execute(recv_packet(&rsp
));
906 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
907 verify(memcached_ntohll(rsp
.decr
.message
.body
.value
) == 0);
909 /* @todo get the value and verify! */
912 /* @todo add incorrect cas */
913 execute(test_binary_noop());
917 static enum test_return
test_binary_decr(void) {
918 return test_binary_decr_impl("test_binary_decr", PROTOCOL_BINARY_CMD_DECREMENT
);
921 static enum test_return
test_binary_decrq(void) {
922 return test_binary_decr_impl("test_binary_decrq", PROTOCOL_BINARY_CMD_DECREMENTQ
);
925 static enum test_return
test_binary_version(void) {
928 raw_command(&cmd
, PROTOCOL_BINARY_CMD_VERSION
, NULL
, 0, NULL
, 0);
930 execute(send_packet(&cmd
));
931 execute(recv_packet(&rsp
));
932 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_VERSION
,
933 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
938 static enum test_return
test_binary_flush_impl(const char *key
, uint8_t cc
) {
942 for (int ii
= 0; ii
< 2; ++ii
) {
943 execute(binary_set_item(key
, key
));
944 flush_command(&cmd
, cc
, 0, ii
== 0);
945 execute(send_packet(&cmd
));
947 if (cc
== PROTOCOL_BINARY_CMD_FLUSH
) {
948 execute(recv_packet(&rsp
));
949 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
951 execute(test_binary_noop());
953 raw_command(&cmd
, PROTOCOL_BINARY_CMD_GET
, key
, strlen(key
), NULL
, 0);
954 execute(send_packet(&cmd
));
955 execute(recv_packet(&rsp
));
956 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_GET
,
957 PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
));
963 static enum test_return
test_binary_flush(void) {
964 return test_binary_flush_impl("test_binary_flush", PROTOCOL_BINARY_CMD_FLUSH
);
967 static enum test_return
test_binary_flushq(void) {
968 return test_binary_flush_impl("test_binary_flushq", PROTOCOL_BINARY_CMD_FLUSHQ
);
971 static enum test_return
test_binary_concat_impl(const char *key
, uint8_t cc
) {
976 if (cc
== PROTOCOL_BINARY_CMD_APPEND
|| cc
== PROTOCOL_BINARY_CMD_APPENDQ
) {
982 execute(binary_set_item(key
, value
));
984 if (cc
== PROTOCOL_BINARY_CMD_APPEND
|| cc
== PROTOCOL_BINARY_CMD_APPENDQ
) {
990 raw_command(&cmd
, cc
, key
, strlen(key
), value
, strlen(value
));
991 execute(send_packet(&cmd
));
992 if (cc
== PROTOCOL_BINARY_CMD_APPEND
|| cc
== PROTOCOL_BINARY_CMD_PREPEND
) {
993 execute(recv_packet(&rsp
));
994 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
996 execute(test_binary_noop());
999 raw_command(&cmd
, PROTOCOL_BINARY_CMD_GET
, key
, strlen(key
), NULL
, 0);
1000 execute(send_packet(&cmd
));
1001 execute(recv_packet(&rsp
));
1002 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_GET
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1003 verify(rsp
.plain
.message
.header
.response
.bodylen
- 4 == 11);
1004 verify(memcmp(rsp
.bytes
+ 28, "hello world", 11) == 0);
1009 static enum test_return
test_binary_append(void) {
1010 return test_binary_concat_impl("test_binary_append", PROTOCOL_BINARY_CMD_APPEND
);
1013 static enum test_return
test_binary_prepend(void) {
1014 return test_binary_concat_impl("test_binary_prepend", PROTOCOL_BINARY_CMD_PREPEND
);
1017 static enum test_return
test_binary_appendq(void) {
1018 return test_binary_concat_impl("test_binary_appendq", PROTOCOL_BINARY_CMD_APPENDQ
);
1021 static enum test_return
test_binary_prependq(void) {
1022 return test_binary_concat_impl("test_binary_prependq", PROTOCOL_BINARY_CMD_PREPENDQ
);
1025 static enum test_return
test_binary_stat(void) {
1029 raw_command(&cmd
, PROTOCOL_BINARY_CMD_STAT
, NULL
, 0, NULL
, 0);
1030 execute(send_packet(&cmd
));
1033 execute(recv_packet(&rsp
));
1035 validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_STAT
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1036 } while (rsp
.plain
.message
.header
.response
.keylen
);
1041 static enum test_return
send_string(const char *cmd
) {
1042 execute(retry_write(cmd
, strlen(cmd
)));
1046 static enum test_return
receive_line(char *buffer
, size_t size
) {
1048 while (offset
< size
) {
1049 execute(retry_read(buffer
+ offset
, 1));
1050 if (buffer
[offset
] == '\n') {
1051 if (offset
+ 1 < size
) {
1052 buffer
[offset
+ 1] = '\0';
1063 static enum test_return
receive_response(const char *msg
) {
1065 execute(receive_line(buffer
, sizeof(buffer
)));
1066 if (strcmp(msg
, buffer
)) {
1067 fprintf(stderr
, "[%s]\n", buffer
);
1069 verify(strcmp(msg
, buffer
) == 0);
1073 static enum test_return
receive_error_response(void) {
1075 execute(receive_line(buffer
, sizeof(buffer
)));
1076 verify(strncmp(buffer
, "ERROR", 5) == 0 || strncmp(buffer
, "CLIENT_ERROR", 12) == 0
1077 || strncmp(buffer
, "SERVER_ERROR", 12) == 0);
1081 static enum test_return
test_ascii_quit(void) {
1082 if (!v16x_or_greater
) {
1083 /* Verify that quit handles unknown options */
1084 execute(send_string("quit foo bar\r\n"));
1085 execute(receive_error_response());
1087 /* quit doesn't support noreply */
1088 execute(send_string("quit noreply\r\n"));
1089 execute(receive_error_response());
1092 /* Verify that quit works */
1093 execute(send_string("quit\r\n"));
1095 /* Socket should be closed now, read should return EXIT_SUCCESS */
1097 verify(timeout_io_op(sock
, POLLIN
, buffer
, sizeof(buffer
)) == 0);
1098 return TEST_PASS_RECONNECT
;
1101 static enum test_return
test_ascii_version(void) {
1102 /* Verify that version works */
1103 execute(send_string("version\r\n"));
1105 execute(receive_line(buffer
, sizeof(buffer
)));
1106 verify(strncmp(buffer
, "VERSION ", 8) == 0);
1108 char *version
= &buffer
[sizeof("VERSION")];
1109 if (version
[0] > '1' || (version
[0] == '1' && version
[2] >= '6')) {
1110 v16x_or_greater
= true;
1113 /* Verify that version command handles unknown options */
1114 execute(send_string("version foo bar\r\n"));
1115 if (v16x_or_greater
) {
1116 execute(receive_line(buffer
, sizeof(buffer
)));
1117 verify(strncmp(buffer
, "VERSION ", 8) == 0);
1119 execute(receive_error_response());
1121 /* version doesn't support noreply */
1122 execute(send_string("version noreply\r\n"));
1123 if (v16x_or_greater
) {
1124 execute(receive_line(buffer
, sizeof(buffer
)));
1125 verify(strncmp(buffer
, "VERSION ", 8) == 0);
1127 execute(receive_error_response());
1132 static enum test_return
test_ascii_verbosity(void) {
1133 /* This command does not adhere to the spec! */
1134 execute(send_string("verbosity foo bar my\r\n"));
1135 execute(receive_error_response());
1137 execute(send_string("verbosity noreply\r\n"));
1138 execute(test_ascii_version());
1140 execute(send_string("verbosity 0 noreply\r\n"));
1141 execute(test_ascii_version());
1143 execute(send_string("verbosity\r\n"));
1144 execute(receive_error_response());
1146 execute(send_string("verbosity 1\r\n"));
1147 execute(receive_response("OK\r\n"));
1149 execute(send_string("verbosity 0\r\n"));
1150 execute(receive_response("OK\r\n"));
1155 static enum test_return
test_ascii_set_impl(const char *key
, bool noreply
) {
1156 /* @todo add tests for bogus format! */
1158 snprintf(buffer
, sizeof(buffer
), "set %s 0 0 5%s\r\nvalue\r\n", key
, noreply
? " noreply" : "");
1159 execute(send_string(buffer
));
1162 execute(receive_response("STORED\r\n"));
1165 return test_ascii_version();
1168 static enum test_return
test_ascii_set(void) {
1169 return test_ascii_set_impl("test_ascii_set", false);
1172 static enum test_return
test_ascii_set_noreply(void) {
1173 return test_ascii_set_impl("test_ascii_set_noreply", true);
1176 static enum test_return
test_ascii_add_impl(const char *key
, bool noreply
) {
1177 /* @todo add tests for bogus format! */
1179 snprintf(buffer
, sizeof(buffer
), "add %s 0 0 5%s\r\nvalue\r\n", key
, noreply
? " noreply" : "");
1180 execute(send_string(buffer
));
1183 execute(receive_response("STORED\r\n"));
1186 execute(send_string(buffer
));
1189 execute(receive_response("NOT_STORED\r\n"));
1192 return test_ascii_version();
1195 static enum test_return
test_ascii_add(void) {
1196 return test_ascii_add_impl("test_ascii_add", false);
1199 static enum test_return
test_ascii_add_noreply(void) {
1200 return test_ascii_add_impl("test_ascii_add_noreply", true);
1203 static enum test_return
ascii_get_unknown_value(char **key
, char **value
, ssize_t
*ndata
) {
1206 execute(receive_line(buffer
, sizeof(buffer
)));
1207 verify(strncmp(buffer
, "VALUE ", 6) == 0);
1208 char *end
= strchr(buffer
+ 6, ' ');
1213 *key
= strdup(buffer
+ 6);
1215 char *ptr
= end
+ 1;
1218 unsigned long val
= strtoul(ptr
, &end
, 10); /* flags */
1224 *ndata
= (ssize_t
) strtoul(end
, &end
, 10); /* size */
1228 while (end
and *end
!= '\n' and isspace(*end
)) ++end
;
1229 verify(end
and *end
== '\n');
1231 *value
= static_cast<char *>(malloc((size_t) *ndata
));
1234 execute(retry_read(*value
, (size_t) *ndata
));
1236 execute(retry_read(buffer
, 2));
1237 verify(memcmp(buffer
, "\r\n", 2) == 0);
1242 static enum test_return
ascii_get_value(const char *key
, const char *value
) {
1244 size_t datasize
= strlen(value
);
1246 verify(datasize
< sizeof(buffer
));
1247 execute(receive_line(buffer
, sizeof(buffer
)));
1248 verify(strncmp(buffer
, "VALUE ", 6) == 0);
1249 verify(strncmp(buffer
+ 6, key
, strlen(key
)) == 0);
1250 char *ptr
= buffer
+ 6 + strlen(key
) + 1;
1254 unsigned long val
= strtoul(ptr
, &end
, 10); /* flags */
1261 val
= strtoul(end
, &end
, 10); /* size */
1264 verify(val
== datasize
);
1266 while (end
and *end
!= '\n' and isspace(*end
)) {
1269 verify(end
and *end
== '\n');
1271 execute(retry_read(buffer
, datasize
));
1272 verify(memcmp(buffer
, value
, datasize
) == 0);
1274 execute(retry_read(buffer
, 2));
1275 verify(memcmp(buffer
, "\r\n", 2) == 0);
1280 static enum test_return
ascii_get_item(const char *key
, const char *value
, bool exist
) {
1282 size_t datasize
= 0;
1284 datasize
= strlen(value
);
1287 verify(datasize
< sizeof(buffer
));
1288 snprintf(buffer
, sizeof(buffer
), "get %s\r\n", key
);
1289 execute(send_string(buffer
));
1292 execute(ascii_get_value(key
, value
));
1295 execute(retry_read(buffer
, 5));
1296 verify(memcmp(buffer
, "END\r\n", 5) == 0);
1301 static enum test_return
ascii_gets_value(const char *key
, const char *value
, unsigned long *cas
) {
1303 size_t datasize
= strlen(value
);
1305 verify(datasize
< sizeof(buffer
));
1306 execute(receive_line(buffer
, sizeof(buffer
)));
1307 verify(strncmp(buffer
, "VALUE ", 6) == 0);
1308 verify(strncmp(buffer
+ 6, key
, strlen(key
)) == 0);
1309 char *ptr
= buffer
+ 6 + strlen(key
) + 1;
1313 unsigned long val
= strtoul(ptr
, &end
, 10); /* flags */
1320 val
= strtoul(end
, &end
, 10); /* size */
1323 verify(val
== datasize
);
1327 *cas
= strtoul(end
, &end
, 10); /* cas */
1330 verify(val
== datasize
);
1333 while (end
and *end
!= '\n' and isspace(*end
)) {
1336 verify(end
and *end
== '\n');
1338 execute(retry_read(buffer
, datasize
));
1339 verify(memcmp(buffer
, value
, datasize
) == 0);
1341 execute(retry_read(buffer
, 2));
1342 verify(memcmp(buffer
, "\r\n", 2) == 0);
1347 static enum test_return
ascii_gets_item(const char *key
, const char *value
, bool exist
,
1348 unsigned long *cas
) {
1350 size_t datasize
= 0;
1352 datasize
= strlen(value
);
1355 verify(datasize
< sizeof(buffer
));
1356 snprintf(buffer
, sizeof(buffer
), "gets %s\r\n", key
);
1357 execute(send_string(buffer
));
1360 execute(ascii_gets_value(key
, value
, cas
));
1362 execute(retry_read(buffer
, 5));
1363 verify(memcmp(buffer
, "END\r\n", 5) == 0);
1368 static enum test_return
ascii_set_item(const char *key
, const char *value
) {
1370 size_t len
= strlen(value
);
1371 snprintf(buffer
, sizeof(buffer
), "set %s 0 0 %u\r\n", key
, (unsigned int) len
);
1372 execute(send_string(buffer
));
1373 execute(retry_write(value
, len
));
1374 execute(send_string("\r\n"));
1375 execute(receive_response("STORED\r\n"));
1379 static enum test_return
test_ascii_replace_impl(const char *key
, bool noreply
) {
1381 snprintf(buffer
, sizeof(buffer
), "replace %s 0 0 5%s\r\nvalue\r\n", key
,
1382 noreply
? " noreply" : "");
1383 execute(send_string(buffer
));
1386 execute(test_ascii_version());
1388 execute(receive_response("NOT_STORED\r\n"));
1391 execute(ascii_set_item(key
, "value"));
1392 execute(ascii_get_item(key
, "value", true));
1394 execute(send_string(buffer
));
1397 execute(test_ascii_version());
1399 execute(receive_response("STORED\r\n"));
1401 return test_ascii_version();
1404 static enum test_return
test_ascii_replace(void) {
1405 return test_ascii_replace_impl("test_ascii_replace", false);
1408 static enum test_return
test_ascii_replace_noreply(void) {
1409 return test_ascii_replace_impl("test_ascii_replace_noreply", true);
1412 static enum test_return
test_ascii_cas_impl(const char *key
, bool noreply
) {
1416 execute(ascii_set_item(key
, "value"));
1417 execute(ascii_gets_item(key
, "value", true, &cas
));
1419 snprintf(buffer
, sizeof(buffer
), "cas %s 0 0 6 %lu%s\r\nvalue2\r\n", key
, cas
,
1420 noreply
? " noreply" : "");
1421 execute(send_string(buffer
));
1424 execute(test_ascii_version());
1426 execute(receive_response("STORED\r\n"));
1429 /* reexecute the same command should fail due to illegal cas */
1430 execute(send_string(buffer
));
1433 execute(test_ascii_version());
1435 execute(receive_response("EXISTS\r\n"));
1438 return test_ascii_version();
1441 static enum test_return
test_ascii_cas(void) {
1442 return test_ascii_cas_impl("test_ascii_cas", false);
1445 static enum test_return
test_ascii_cas_noreply(void) {
1446 return test_ascii_cas_impl("test_ascii_cas_noreply", true);
1449 static enum test_return
test_ascii_delete_impl(const char *key
, bool noreply
) {
1450 execute(ascii_set_item(key
, "value"));
1452 execute(send_string("delete\r\n"));
1453 execute(receive_error_response());
1454 /* BUG: the server accepts delete a b */
1455 execute(send_string("delete a b c d e\r\n"));
1456 execute(receive_error_response());
1459 snprintf(buffer
, sizeof(buffer
), "delete %s%s\r\n", key
, noreply
? " noreply" : "");
1460 execute(send_string(buffer
));
1463 execute(test_ascii_version());
1465 execute(receive_response("DELETED\r\n"));
1467 execute(ascii_get_item(key
, "value", false));
1468 execute(send_string(buffer
));
1470 execute(test_ascii_version());
1472 execute(receive_response("NOT_FOUND\r\n"));
1477 static enum test_return
test_ascii_delete(void) {
1478 return test_ascii_delete_impl("test_ascii_delete", false);
1481 static enum test_return
test_ascii_delete_noreply(void) {
1482 return test_ascii_delete_impl("test_ascii_delete_noreply", true);
1485 static enum test_return
test_ascii_get(void) {
1486 execute(ascii_set_item("test_ascii_get", "value"));
1488 execute(send_string("get\r\n"));
1489 execute(receive_error_response());
1490 execute(ascii_get_item("test_ascii_get", "value", true));
1491 execute(ascii_get_item("test_ascii_get_notfound", "value", false));
1496 static enum test_return
test_ascii_gets(void) {
1497 execute(ascii_set_item("test_ascii_gets", "value"));
1499 execute(send_string("gets\r\n"));
1500 execute(receive_error_response());
1502 execute(ascii_gets_item("test_ascii_gets", "value", true, &cas
));
1503 execute(ascii_gets_item("test_ascii_gets_notfound", "value", false, &cas
));
1508 static enum test_return
test_ascii_mget(void) {
1509 const uint32_t nkeys
= 5;
1510 const char *const keys
[] = {"test_ascii_mget1", "test_ascii_mget2",
1511 /* test_ascii_mget_3 does not exist :) */
1512 "test_ascii_mget4", "test_ascii_mget5", "test_ascii_mget6"};
1514 for (uint32_t x
= 0; x
< nkeys
; ++x
) {
1515 execute(ascii_set_item(keys
[x
], "value"));
1518 /* Ask for a key that doesn't exist as well */
1519 execute(send_string("get test_ascii_mget1 test_ascii_mget2 test_ascii_mget3 "
1520 "test_ascii_mget4 test_ascii_mget5 "
1521 "test_ascii_mget6\r\n"));
1523 std::vector
<char *> returned
;
1524 returned
.resize(nkeys
);
1526 for (uint32_t x
= 0; x
< nkeys
; ++x
) {
1529 execute(ascii_get_unknown_value(&returned
[x
], &v
, &nbytes
));
1530 verify(nbytes
== 5);
1531 verify(memcmp(v
, "value", 5) == 0);
1536 execute(retry_read(buffer
, 5));
1537 verify(memcmp(buffer
, "END\r\n", 5) == 0);
1539 /* verify that we got all the keys we expected */
1540 for (uint32_t x
= 0; x
< nkeys
; ++x
) {
1542 for (uint32_t y
= 0; y
< nkeys
; ++y
) {
1543 if (strcmp(keys
[x
], returned
[y
]) == 0) {
1551 for (uint32_t x
= 0; x
< nkeys
; ++x
) {
1558 static enum test_return
test_ascii_incr_impl(const char *key
, bool noreply
) {
1560 snprintf(cmd
, sizeof(cmd
), "incr %s 1%s\r\n", key
, noreply
? " noreply" : "");
1562 execute(ascii_set_item(key
, "0"));
1563 for (int x
= 1; x
< 11; ++x
) {
1564 execute(send_string(cmd
));
1567 execute(test_ascii_version());
1570 execute(receive_line(buffer
, sizeof(buffer
)));
1571 int val
= atoi(buffer
);
1576 execute(ascii_get_item(key
, "10", true));
1581 static enum test_return
test_ascii_incr(void) {
1582 return test_ascii_incr_impl("test_ascii_incr", false);
1585 static enum test_return
test_ascii_incr_noreply(void) {
1586 return test_ascii_incr_impl("test_ascii_incr_noreply", true);
1589 static enum test_return
test_ascii_decr_impl(const char *key
, bool noreply
) {
1591 snprintf(cmd
, sizeof(cmd
), "decr %s 1%s\r\n", key
, noreply
? " noreply" : "");
1593 execute(ascii_set_item(key
, "9"));
1594 for (int x
= 8; x
> -1; --x
) {
1595 execute(send_string(cmd
));
1598 execute(test_ascii_version());
1601 execute(receive_line(buffer
, sizeof(buffer
)));
1602 int val
= atoi(buffer
);
1607 execute(ascii_get_item(key
, "0", true));
1609 /* verify that it doesn't wrap */
1610 execute(send_string(cmd
));
1612 execute(test_ascii_version());
1615 execute(receive_line(buffer
, sizeof(buffer
)));
1617 execute(ascii_get_item(key
, "0", true));
1622 static enum test_return
test_ascii_decr(void) {
1623 return test_ascii_decr_impl("test_ascii_decr", false);
1626 static enum test_return
test_ascii_decr_noreply(void) {
1627 return test_ascii_decr_impl("test_ascii_decr_noreply", true);
1630 static enum test_return
test_ascii_flush_impl(const char *key
, bool noreply
) {
1632 /* Verify that the flush_all command handles unknown options */
1633 /* Bug in the current memcached server! */
1634 execute(send_string("flush_all foo bar\r\n"));
1635 execute(receive_error_response());
1638 execute(ascii_set_item(key
, key
));
1639 execute(ascii_get_item(key
, key
, true));
1642 execute(send_string("flush_all noreply\r\n"));
1643 execute(test_ascii_version());
1645 execute(send_string("flush_all\r\n"));
1646 execute(receive_response("OK\r\n"));
1649 execute(ascii_get_item(key
, key
, false));
1654 static enum test_return
test_ascii_flush(void) {
1655 return test_ascii_flush_impl("test_ascii_flush", false);
1658 static enum test_return
test_ascii_flush_noreply(void) {
1659 return test_ascii_flush_impl("test_ascii_flush_noreply", true);
1662 static enum test_return
test_ascii_concat_impl(const char *key
, bool append
, bool noreply
) {
1670 execute(ascii_set_item(key
, value
));
1679 snprintf(cmd
, sizeof(cmd
), "%s %s 0 0 %u%s\r\n%s\r\n", append
? "append" : "prepend", key
,
1680 (unsigned int) strlen(value
), noreply
? " noreply" : "", value
);
1681 execute(send_string(cmd
));
1684 execute(test_ascii_version());
1686 execute(receive_response("STORED\r\n"));
1689 execute(ascii_get_item(key
, "hello world", true));
1691 snprintf(cmd
, sizeof(cmd
), "%s %s_notfound 0 0 %u%s\r\n%s\r\n", append
? "append" : "prepend",
1692 key
, (unsigned int) strlen(value
), noreply
? " noreply" : "", value
);
1693 execute(send_string(cmd
));
1696 execute(test_ascii_version());
1698 execute(receive_response("NOT_STORED\r\n"));
1704 static enum test_return
test_ascii_append(void) {
1705 return test_ascii_concat_impl("test_ascii_append", true, false);
1708 static enum test_return
test_ascii_prepend(void) {
1709 return test_ascii_concat_impl("test_ascii_prepend", false, false);
1712 static enum test_return
test_ascii_append_noreply(void) {
1713 return test_ascii_concat_impl("test_ascii_append_noreply", true, true);
1716 static enum test_return
test_ascii_prepend_noreply(void) {
1717 return test_ascii_concat_impl("test_ascii_prepend_noreply", false, true);
1720 static enum test_return
test_ascii_stat(void) {
1721 execute(send_string("stats noreply\r\n"));
1722 execute(receive_error_response());
1723 execute(send_string("stats\r\n"));
1726 execute(receive_line(buffer
, sizeof(buffer
)));
1727 } while (strcmp(buffer
, "END\r\n"));
1729 return TEST_PASS_RECONNECT
;
1732 typedef enum test_return (*TEST_FUNC
)(void);
1735 const char *description
;
1739 struct testcase testcases
[] = {{"ascii version", test_ascii_version
},
1740 {"ascii quit", test_ascii_quit
},
1741 {"ascii verbosity", test_ascii_verbosity
},
1742 {"ascii set", test_ascii_set
},
1743 {"ascii set noreply", test_ascii_set_noreply
},
1744 {"ascii get", test_ascii_get
},
1745 {"ascii gets", test_ascii_gets
},
1746 {"ascii mget", test_ascii_mget
},
1747 {"ascii flush", test_ascii_flush
},
1748 {"ascii flush noreply", test_ascii_flush_noreply
},
1749 {"ascii add", test_ascii_add
},
1750 {"ascii add noreply", test_ascii_add_noreply
},
1751 {"ascii replace", test_ascii_replace
},
1752 {"ascii replace noreply", test_ascii_replace_noreply
},
1753 {"ascii cas", test_ascii_cas
},
1754 {"ascii cas noreply", test_ascii_cas_noreply
},
1755 {"ascii delete", test_ascii_delete
},
1756 {"ascii delete noreply", test_ascii_delete_noreply
},
1757 {"ascii incr", test_ascii_incr
},
1758 {"ascii incr noreply", test_ascii_incr_noreply
},
1759 {"ascii decr", test_ascii_decr
},
1760 {"ascii decr noreply", test_ascii_decr_noreply
},
1761 {"ascii append", test_ascii_append
},
1762 {"ascii append noreply", test_ascii_append_noreply
},
1763 {"ascii prepend", test_ascii_prepend
},
1764 {"ascii prepend noreply", test_ascii_prepend_noreply
},
1765 {"ascii stat", test_ascii_stat
},
1766 {"binary noop", test_binary_noop
},
1767 {"binary quit", test_binary_quit
},
1768 {"binary quitq", test_binary_quitq
},
1769 {"binary set", test_binary_set
},
1770 {"binary setq", test_binary_setq
},
1771 {"binary flush", test_binary_flush
},
1772 {"binary flushq", test_binary_flushq
},
1773 {"binary add", test_binary_add
},
1774 {"binary addq", test_binary_addq
},
1775 {"binary replace", test_binary_replace
},
1776 {"binary replaceq", test_binary_replaceq
},
1777 {"binary delete", test_binary_delete
},
1778 {"binary deleteq", test_binary_deleteq
},
1779 {"binary get", test_binary_get
},
1780 {"binary getq", test_binary_getq
},
1781 {"binary getk", test_binary_getk
},
1782 {"binary getkq", test_binary_getkq
},
1783 {"binary incr", test_binary_incr
},
1784 {"binary incrq", test_binary_incrq
},
1785 {"binary decr", test_binary_decr
},
1786 {"binary decrq", test_binary_decrq
},
1787 {"binary version", test_binary_version
},
1788 {"binary append", test_binary_append
},
1789 {"binary appendq", test_binary_appendq
},
1790 {"binary prepend", test_binary_prepend
},
1791 {"binary prependq", test_binary_prependq
},
1792 {"binary stat", test_binary_stat
},
1795 struct test_type_st
{
1800 int main(int argc
, char **argv
) {
1801 static const char *const status_msg
[] = {"[skip]", "[pass]", "[pass]", "[FAIL]"};
1802 struct test_type_st tests
= {true, true};
1805 const char *hostname
= NULL
;
1806 const char *port
= MEMCACHED_DEFAULT_PORT_STRING
;
1808 bool prompt
= false;
1809 const char *testname
= NULL
;
1811 while ((cmd
= getopt(argc
, argv
, "qt:vch:p:PT:?ab")) != EOF
) {
1815 tests
.binary
= false;
1819 tests
.ascii
= false;
1820 tests
.binary
= true;
1824 timeout
= atoi(optarg
);
1826 fprintf(stderr
, "Invalid timeout. Please specify a number for -t\n");
1827 return EXIT_FAILURE
;
1861 "Usage: %s [-h hostname] [-p port] [-c] [-v] [-t n] [-P] [-T testname]'\n"
1862 "\t-c\tGenerate coredump if a test fails\n"
1863 "\t-v\tVerbose test output (print out the assertion)\n"
1864 "\t-t n\tSet the timeout for io-operations to n seconds\n"
1865 "\t-P\tPrompt the user before starting a test.\n"
1866 "\t\t\t\"skip\" will skip the test\n"
1867 "\t\t\t\"quit\" will terminate memcapable\n"
1868 "\t\t\tEverything else will start the test\n"
1869 "\t-T n\tJust run the test named n\n"
1870 "\t-a\tOnly test the ascii protocol\n"
1871 "\t-b\tOnly test the binary protocol\n\n"
1872 "WARNING: This will FLUSH your server!\n\n",
1874 return EXIT_SUCCESS
;
1879 fprintf(stderr
, "No hostname was provided.\n");
1880 return EXIT_FAILURE
;
1885 if (WSAStartup(MAKEWORD(2, 2), &wsaData
)) {
1886 fprintf(stderr
, "Socket Initialization Error.\n");
1887 return EXIT_FAILURE
;
1891 sock
= connect_server(hostname
, port
);
1892 if (sock
== INVALID_SOCKET
) {
1893 fprintf(stderr
, "Failed to connect to <%s:%s>: %s\n", hostname
, port
,
1894 strerror(get_socket_errno()));
1895 return EXIT_FAILURE
;
1898 for (int ii
= 0; testcases
[ii
].description
; ++ii
) {
1899 if (testname
&& strcmp(testcases
[ii
].description
, testname
)) {
1903 if ((testcases
[ii
].description
[0] == 'a' && (tests
.ascii
) == 0)
1904 || (testcases
[ii
].description
[0] == 'b' && (tests
.binary
) == 0))
1909 fprintf(stdout
, "%-40s", testcases
[ii
].description
);
1913 fprintf(stdout
, "\nPress <return> when you are ready? ");
1914 char buffer
[80] = {0};
1915 if (fgets(buffer
, sizeof(buffer
), stdin
)) {
1916 if (strncmp(buffer
, "skip", 4) == 0) {
1917 fprintf(stdout
, "%-40s%s\n", testcases
[ii
].description
, status_msg
[TEST_SKIP
]);
1921 if (strncmp(buffer
, "quit", 4) == 0) {
1926 fprintf(stdout
, "%-40s", testcases
[ii
].description
);
1930 bool reconnect
= false;
1931 enum test_return ret
= testcases
[ii
].function();
1932 if (ret
== TEST_FAIL
) {
1936 fprintf(stderr
, "\n");
1938 } else if (ret
== TEST_PASS_RECONNECT
) {
1942 if (ret
== TEST_FAIL
) {
1943 fprintf(stderr
, "%s\n", status_msg
[ret
]);
1945 fprintf(stdout
, "%s\n", status_msg
[ret
]);
1950 if ((sock
= connect_server(hostname
, port
)) == INVALID_SOCKET
) {
1951 fprintf(stderr
, "Failed to connect to <%s:%s>: %s\n", hostname
,
1952 port
, strerror(get_socket_errno()));
1953 fprintf(stderr
, "%d of %d tests failed\n", failed
, total
);
1954 return EXIT_FAILURE
;
1961 fprintf(stdout
, "All tests passed\n");
1963 fprintf(stderr
, "%d of %d tests failed\n", failed
, total
);
1970 return (failed
== 0) ? EXIT_SUCCESS
: EXIT_FAILURE
;