2 +--------------------------------------------------------------------+
3 | libmemcached - 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 Michael Wallner <mike@php.net> |
13 +--------------------------------------------------------------------+
18 #include "mem_config.h"
23 # include "poll/poll.h"
36 #include <sys/types.h>
39 #include "libmemcached-1.0/memcached.h"
41 #include "libmemcached/socket.hpp"
42 #include "libmemcachedprotocol-0.0/binary.h"
43 #include "libmemcached/byteorder.h"
44 #include "utilities.h"
49 /* /usr/include/netinet/in.h defines macros from ntohs() to _bswap_nn to
50 * optimize the conversion functions, but the prototypes generate warnings
51 * from gcc. The conversion methods isn't the bottleneck for my app, so
52 * just remove the warnings by undef'ing the optimization ..
58 /* Should we generate coredumps when we enounter an error (-c) */
59 static bool do_core
= false;
60 /* connection to the server */
61 static memcached_socket_t sock
;
62 /* Should the output from test failures be verbose or quiet? */
63 static bool verbose
= false;
65 /* The number of seconds to wait for an IO-operation */
66 static int timeout
= 2;
68 /* v1.6.x is more permissible */
69 static bool v16x_or_greater
= false;
72 * Instead of having to cast between the different datatypes we create
73 * a union of all of the different types of pacages we want to send.
74 * A lot of the different commands use the same packet layout, so I'll
75 * just define the different types I need. The typedefs only contain
76 * the header of the message, so we need some space for keys and body
77 * To avoid to have to do multiple writes, lets add a chunk of memory
78 * to use. 1k should be more than enough for header, key and body.
81 protocol_binary_request_no_extras plain
;
82 protocol_binary_request_flush flush
;
83 protocol_binary_request_incr incr
;
84 protocol_binary_request_set set
;
89 protocol_binary_response_no_extras plain
;
90 protocol_binary_response_incr incr
;
91 protocol_binary_response_decr decr
;
95 enum test_return
{ TEST_SKIP
, TEST_PASS
, TEST_PASS_RECONNECT
, TEST_FAIL
};
98 * Try to get an addrinfo struct for a given port on a given host
100 static struct addrinfo
*lookuphost(const char *hostname
, const char *port
) {
101 struct addrinfo
*ai
= 0;
102 struct addrinfo hints
;
103 memset(&hints
, 0, sizeof(struct addrinfo
));
104 hints
.ai_family
= AF_UNSPEC
;
105 hints
.ai_protocol
= IPPROTO_TCP
;
106 hints
.ai_socktype
= SOCK_STREAM
;
108 int error
= getaddrinfo(hostname
, port
, &hints
, &ai
);
110 if (error
!= EAI_SYSTEM
) {
111 fprintf(stderr
, "getaddrinfo(): %s\n", gai_strerror(error
));
113 perror("getaddrinfo()");
121 * Set the socket in nonblocking mode
122 * @return -1 if failure, the socket otherwise
124 static memcached_socket_t
set_noblock(void) {
127 if (ioctlsocket(sock
, FIONBIO
, &arg
) == SOCKET_ERROR
) {
128 perror("Failed to set nonblocking io");
130 return INVALID_SOCKET
;
133 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
) {
141 if (fcntl(sock
, F_SETFL
, flags
| O_NONBLOCK
) == -1) {
142 perror("Failed to set socket to nonblocking mode");
143 memcached_close_socket(sock
);
144 return INVALID_SOCKET
;
152 * Try to open a connection to the server
153 * @param hostname the name of the server to connect to
154 * @param port the port number (or service) to connect to
155 * @return positive integer if success, -1 otherwise
157 static memcached_socket_t
connect_server(const char *hostname
, const char *port
) {
158 struct addrinfo
*ai
= lookuphost(hostname
, port
);
159 sock
= INVALID_SOCKET
;
161 if ((sock
= socket(ai
->ai_family
, ai
->ai_socktype
, ai
->ai_protocol
)) != INVALID_SOCKET
) {
162 if (connect(sock
, ai
->ai_addr
, ai
->ai_addrlen
) == SOCKET_ERROR
) {
163 fprintf(stderr
, "Failed to connect socket: %s\n", strerror(get_socket_errno()));
165 sock
= INVALID_SOCKET
;
167 sock
= set_noblock();
170 fprintf(stderr
, "Failed to create socket: %s\n", strerror(get_socket_errno()));
179 static ssize_t
timeout_io_op(memcached_socket_t fd
, short direction
, void *buf
, size_t len
) {
182 if (direction
== POLLOUT
) {
183 ret
= send(fd
, buf
, len
, 0);
185 ret
= recv(fd
, buf
, len
, 0);
188 if (ret
== SOCKET_ERROR
&& get_socket_errno() == EWOULDBLOCK
) {
190 memset(&fds
, 0, sizeof(struct pollfd
));
191 fds
.events
= direction
;
194 int err
= poll(&fds
, 1, timeout
* 1000);
196 if (direction
== POLLOUT
) {
197 ret
= send(fd
, buf
, len
, 0);
199 ret
= recv(fd
, buf
, len
, 0);
201 } else if (err
== 0) {
204 perror("Failed to poll");
213 * Ensure that an expression is true. If it isn't print out a message similar
214 * to assert() and create a coredump if the user wants that. If not an error
215 * message is returned.
218 static enum test_return
ensure(bool val
, const char *expression
, const char *file
, int line
) {
221 fprintf(stdout
, "\n%s:%d: %s", file
, line
, expression
);
234 #define verify(expression) \
236 if (ensure(expression, #expression, __FILE__, __LINE__) == TEST_FAIL) \
239 #define execute(expression) \
241 if (ensure(expression == TEST_PASS, #expression, __FILE__, __LINE__) == TEST_FAIL) \
246 * Send a chunk of memory over the socket (retry if the call is iterrupted
248 static enum test_return
retry_write(const void *buf
, size_t len
) {
250 const char *ptr
= static_cast<const char *>(buf
);
253 size_t num_bytes
= len
- offset
;
254 ssize_t nw
= timeout_io_op(sock
, POLLOUT
, (void *) (ptr
+ offset
), num_bytes
);
256 verify(get_socket_errno() == EINTR
|| get_socket_errno() == EAGAIN
);
258 offset
+= (size_t) nw
;
261 } while (offset
< len
);
267 * Resend a packet to the server (All fields in the command header should
268 * be in network byte order)
270 static enum test_return
resend_packet(command
*cmd
) {
272 sizeof(protocol_binary_request_no_extras
) + ntohl(cmd
->plain
.message
.header
.request
.bodylen
);
274 execute(retry_write(cmd
, length
));
279 * Send a command to the server. The command header needs to be updated
280 * to network byte order
282 static enum test_return
send_packet(command
*cmd
) {
283 /* Fix the byteorder of the header */
284 cmd
->plain
.message
.header
.request
.keylen
= ntohs(cmd
->plain
.message
.header
.request
.keylen
);
285 cmd
->plain
.message
.header
.request
.bodylen
= ntohl(cmd
->plain
.message
.header
.request
.bodylen
);
286 cmd
->plain
.message
.header
.request
.cas
= memcached_ntohll(cmd
->plain
.message
.header
.request
.cas
);
288 execute(resend_packet(cmd
));
293 * Read a fixed length chunk of data from the server
295 static enum test_return
retry_read(void *buf
, size_t len
) {
298 ssize_t nr
= timeout_io_op(sock
, POLLIN
, ((char *) buf
) + offset
, len
- offset
);
301 fprintf(stderr
, "Errno: %d %s\n", get_socket_errno(), strerror(errno
));
302 verify(get_socket_errno() == EINTR
|| get_socket_errno() == EAGAIN
);
305 case 0: return TEST_FAIL
;
307 default: offset
+= (size_t) nr
;
309 } while (offset
< len
);
315 * Receive a response from the server and conver the fields in the header
316 * to local byte order
318 static enum test_return
recv_packet(response
*rsp
) {
319 execute(retry_read(rsp
, sizeof(protocol_binary_response_no_extras
)));
321 /* Fix the byte order in the packet header */
322 rsp
->plain
.message
.header
.response
.keylen
= ntohs(rsp
->plain
.message
.header
.response
.keylen
);
323 rsp
->plain
.message
.header
.response
.status
= ntohs(rsp
->plain
.message
.header
.response
.status
);
324 rsp
->plain
.message
.header
.response
.bodylen
= ntohl(rsp
->plain
.message
.header
.response
.bodylen
);
325 rsp
->plain
.message
.header
.response
.cas
= memcached_ntohll(rsp
->plain
.message
.header
.response
.cas
);
327 size_t bodysz
= rsp
->plain
.message
.header
.response
.bodylen
;
329 execute(retry_read(rsp
->bytes
+ sizeof(protocol_binary_response_no_extras
), bodysz
));
335 * Create a storage command (add, set, replace etc)
337 * @param cmd destination buffer
338 * @param cc the storage command to create
339 * @param key the key to store
340 * @param keylen the length of the key
341 * @param dta the data to store with the key
342 * @param dtalen the length of the data to store with the key
343 * @param flags the flags to store along with the key
344 * @param exptime the expiry time for the key
346 static void storage_command(command
*cmd
, uint8_t cc
, const void *key
, size_t keylen
,
347 const void *dta
, size_t dtalen
, uint32_t flags
, uint32_t exptime
) {
348 /* all of the storage commands use the same command layout */
349 protocol_binary_request_set
*request
= &cmd
->set
;
351 memset(request
, 0, sizeof(*request
));
352 request
->message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
353 request
->message
.header
.request
.opcode
= cc
;
354 request
->message
.header
.request
.keylen
= (uint16_t) keylen
;
355 request
->message
.header
.request
.extlen
= 8;
356 request
->message
.header
.request
.bodylen
= (uint32_t)(keylen
+ 8 + dtalen
);
357 request
->message
.header
.request
.opaque
= 0xdeadbeef;
358 request
->message
.body
.flags
= flags
;
359 request
->message
.body
.expiration
= exptime
;
361 off_t key_offset
= sizeof(protocol_binary_request_no_extras
) + 8;
362 memcpy(cmd
->bytes
+ key_offset
, key
, keylen
);
364 memcpy(cmd
->bytes
+ key_offset
+ keylen
, dta
, dtalen
);
368 * Create a basic command to send to the server
369 * @param cmd destination buffer
370 * @param cc the command to create
371 * @param key the key to store
372 * @param keylen the length of the key
373 * @param dta the data to store with the key
374 * @param dtalen the length of the data to store with the key
376 static void raw_command(command
*cmd
, uint8_t cc
, const void *key
, size_t keylen
, const void *dta
,
378 /* all of the storage commands use the same command layout */
379 memset(cmd
, 0, sizeof(*cmd
));
380 cmd
->plain
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
381 cmd
->plain
.message
.header
.request
.opcode
= cc
;
382 cmd
->plain
.message
.header
.request
.keylen
= (uint16_t) keylen
;
383 cmd
->plain
.message
.header
.request
.bodylen
= (uint32_t)(keylen
+ dtalen
);
384 cmd
->plain
.message
.header
.request
.opaque
= 0xdeadbeef;
386 off_t key_offset
= sizeof(protocol_binary_request_no_extras
);
389 memcpy(cmd
->bytes
+ key_offset
, key
, keylen
);
392 memcpy(cmd
->bytes
+ key_offset
+ keylen
, dta
, dtalen
);
396 * Create the flush command
397 * @param cmd destination buffer
398 * @param cc the command to create (FLUSH/FLUSHQ)
399 * @param exptime when to flush
400 * @param use_extra to force using of the extra field?
402 static void flush_command(command
*cmd
, uint8_t cc
, uint32_t exptime
, bool use_extra
) {
403 memset(cmd
, 0, sizeof(cmd
->flush
));
404 cmd
->flush
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
405 cmd
->flush
.message
.header
.request
.opcode
= cc
;
406 cmd
->flush
.message
.header
.request
.opaque
= 0xdeadbeef;
408 if (exptime
!= 0 || use_extra
) {
409 cmd
->flush
.message
.header
.request
.extlen
= 4;
410 cmd
->flush
.message
.body
.expiration
= htonl(exptime
);
411 cmd
->flush
.message
.header
.request
.bodylen
= 4;
416 * Create a incr/decr command
417 * @param cc the cmd to create (FLUSH/FLUSHQ)
418 * @param key the key to operate on
419 * @param keylen the number of bytes in the key
420 * @param delta the number to add/subtract
421 * @param initial the initial value if the key doesn't exist
422 * @param exptime when the key should expire if it isn't set
424 static void arithmetic_command(command
*cmd
, uint8_t cc
, const void *key
, size_t keylen
,
425 uint64_t delta
, uint64_t initial
, uint32_t exptime
) {
426 memset(cmd
, 0, sizeof(cmd
->incr
));
427 cmd
->incr
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
428 cmd
->incr
.message
.header
.request
.opcode
= cc
;
429 cmd
->incr
.message
.header
.request
.keylen
= (uint16_t) keylen
;
430 cmd
->incr
.message
.header
.request
.extlen
= 20;
431 cmd
->incr
.message
.header
.request
.bodylen
= (uint32_t)(keylen
+ 20);
432 cmd
->incr
.message
.header
.request
.opaque
= 0xdeadbeef;
433 cmd
->incr
.message
.body
.delta
= memcached_htonll(delta
);
434 cmd
->incr
.message
.body
.initial
= memcached_htonll(initial
);
435 cmd
->incr
.message
.body
.expiration
= htonl(exptime
);
437 off_t key_offset
= sizeof(protocol_binary_request_no_extras
) + 20;
438 memcpy(cmd
->bytes
+ key_offset
, key
, keylen
);
442 * Validate the response header from the server
443 * @param rsp the response to check
444 * @param cc the expected command
445 * @param status the expected status
447 static enum test_return
do_validate_response_header(response
*rsp
, uint8_t cc
, uint16_t status
) {
448 verify(rsp
->plain
.message
.header
.response
.magic
== PROTOCOL_BINARY_RES
);
449 verify(rsp
->plain
.message
.header
.response
.opcode
== cc
);
450 verify(rsp
->plain
.message
.header
.response
.datatype
== PROTOCOL_BINARY_RAW_BYTES
);
451 verify(rsp
->plain
.message
.header
.response
.status
== status
);
452 verify(rsp
->plain
.message
.header
.response
.opaque
== 0xdeadbeef);
454 if (status
== PROTOCOL_BINARY_RESPONSE_SUCCESS
) {
456 case PROTOCOL_BINARY_CMD_ADDQ
:
457 case PROTOCOL_BINARY_CMD_APPENDQ
:
458 case PROTOCOL_BINARY_CMD_DECREMENTQ
:
459 case PROTOCOL_BINARY_CMD_DELETEQ
:
460 case PROTOCOL_BINARY_CMD_FLUSHQ
:
461 case PROTOCOL_BINARY_CMD_INCREMENTQ
:
462 case PROTOCOL_BINARY_CMD_PREPENDQ
:
463 case PROTOCOL_BINARY_CMD_QUITQ
:
464 case PROTOCOL_BINARY_CMD_REPLACEQ
:
465 case PROTOCOL_BINARY_CMD_SETQ
:
466 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
!= 0);
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
!= 0);
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
!= 0);
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
!= 0);
520 case PROTOCOL_BINARY_CMD_GETK
:
521 case PROTOCOL_BINARY_CMD_GETKQ
:
522 verify(rsp
->plain
.message
.header
.response
.keylen
!= 0);
523 verify(rsp
->plain
.message
.header
.response
.extlen
== 4);
524 verify(rsp
->plain
.message
.header
.response
.cas
!= 0);
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
!= 0);
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
) != 0) {
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, ' ');
1209 verify(end
!= NULL
);
1213 *key
= strdup(buffer
+ 6);
1214 verify(*key
!= NULL
);
1215 char *ptr
= end
+ 1;
1218 unsigned long val
= strtoul(ptr
, &end
, 10); /* flags */
1222 verify(end
!= NULL
);
1224 *ndata
= (ssize_t
) strtoul(end
, &end
, 10); /* size */
1227 verify(end
!= NULL
);
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
));
1232 verify(*value
!= NULL
);
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 */
1258 verify(end
!= NULL
);
1261 val
= strtoul(end
, &end
, 10); /* size */
1264 verify(val
== datasize
);
1265 verify(end
!= NULL
);
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;
1283 if (value
!= NULL
) {
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 */
1317 verify(end
!= NULL
);
1320 val
= strtoul(end
, &end
, 10); /* size */
1323 verify(val
== datasize
);
1324 verify(end
!= NULL
);
1327 *cas
= strtoul(end
, &end
, 10); /* cas */
1330 verify(val
== datasize
);
1331 verify(end
!= NULL
);
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;
1351 if (value
!= NULL
) {
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") != 0);
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
;
1831 case 'v': verbose
= true; break;
1833 case 'c': do_core
= true; break;
1835 case 'h': hostname
= optarg
; break;
1837 case 'p': port
= optarg
; break;
1839 case 'q': close_stdio(); break;
1841 case 'P': prompt
= true; break;
1843 case 'T': testname
= optarg
; break;
1847 "Usage: %s [-h hostname] [-p port] [-c] [-v] [-t n] [-P] [-T testname]'\n"
1848 "\t-c\tGenerate coredump if a test fails\n"
1849 "\t-v\tVerbose test output (print out the assertion)\n"
1850 "\t-t n\tSet the timeout for io-operations to n seconds\n"
1851 "\t-P\tPrompt the user before starting a test.\n"
1852 "\t\t\t\"skip\" will skip the test\n"
1853 "\t\t\t\"quit\" will terminate memcapable\n"
1854 "\t\t\tEverything else will start the test\n"
1855 "\t-T n\tJust run the test named n\n"
1856 "\t-a\tOnly test the ascii protocol\n"
1857 "\t-b\tOnly test the binary protocol\n",
1859 return EXIT_SUCCESS
;
1864 fprintf(stderr
, "No hostname was provided.\n");
1865 return EXIT_FAILURE
;
1868 initialize_sockets();
1869 sock
= connect_server(hostname
, port
);
1870 if (sock
== INVALID_SOCKET
) {
1871 fprintf(stderr
, "Failed to connect to <%s:%s>: %s\n", hostname
?: "(null)", port
?: "(null)",
1872 strerror(get_socket_errno()));
1873 return EXIT_FAILURE
;
1876 for (int ii
= 0; testcases
[ii
].description
!= NULL
; ++ii
) {
1877 if (testname
!= NULL
&& strcmp(testcases
[ii
].description
, testname
) != 0) {
1881 if ((testcases
[ii
].description
[0] == 'a' && (tests
.ascii
) == 0)
1882 || (testcases
[ii
].description
[0] == 'b' && (tests
.binary
) == 0))
1887 fprintf(stdout
, "%-40s", testcases
[ii
].description
);
1891 fprintf(stdout
, "\nPress <return> when you are ready? ");
1892 char buffer
[80] = {0};
1893 if (fgets(buffer
, sizeof(buffer
), stdin
) != NULL
) {
1894 if (strncmp(buffer
, "skip", 4) == 0) {
1895 fprintf(stdout
, "%-40s%s\n", testcases
[ii
].description
, status_msg
[TEST_SKIP
]);
1899 if (strncmp(buffer
, "quit", 4) == 0) {
1904 fprintf(stdout
, "%-40s", testcases
[ii
].description
);
1908 bool reconnect
= false;
1909 enum test_return ret
= testcases
[ii
].function();
1910 if (ret
== TEST_FAIL
) {
1914 fprintf(stderr
, "\n");
1916 } else if (ret
== TEST_PASS_RECONNECT
) {
1920 if (ret
== TEST_FAIL
) {
1921 fprintf(stderr
, "%s\n", status_msg
[ret
]);
1923 fprintf(stdout
, "%s\n", status_msg
[ret
]);
1928 if ((sock
= connect_server(hostname
, port
)) == INVALID_SOCKET
) {
1929 fprintf(stderr
, "Failed to connect to <%s:%s>: %s\n", hostname
?: "(null)",
1930 port
?: "(null)", strerror(get_socket_errno()));
1931 fprintf(stderr
, "%d of %d tests failed\n", failed
, total
);
1932 return EXIT_FAILURE
;
1939 fprintf(stdout
, "All tests passed\n");
1941 fprintf(stderr
, "%d of %d tests failed\n", failed
, total
);
1944 return (failed
== 0) ? EXIT_SUCCESS
: EXIT_FAILURE
;