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
);
309 offset
+= (size_t) nr
;
311 } while (offset
< len
);
317 * Receive a response from the server and conver the fields in the header
318 * to local byte order
320 static enum test_return
recv_packet(response
*rsp
) {
321 execute(retry_read(rsp
, sizeof(protocol_binary_response_no_extras
)));
323 /* Fix the byte order in the packet header */
324 rsp
->plain
.message
.header
.response
.keylen
= ntohs(rsp
->plain
.message
.header
.response
.keylen
);
325 rsp
->plain
.message
.header
.response
.status
= ntohs(rsp
->plain
.message
.header
.response
.status
);
326 rsp
->plain
.message
.header
.response
.bodylen
= ntohl(rsp
->plain
.message
.header
.response
.bodylen
);
327 rsp
->plain
.message
.header
.response
.cas
= memcached_ntohll(rsp
->plain
.message
.header
.response
.cas
);
329 size_t bodysz
= rsp
->plain
.message
.header
.response
.bodylen
;
331 execute(retry_read(rsp
->bytes
+ sizeof(protocol_binary_response_no_extras
), bodysz
));
337 * Create a storage command (add, set, replace etc)
339 * @param cmd destination buffer
340 * @param cc the storage command to create
341 * @param key the key to store
342 * @param keylen the length of the key
343 * @param dta the data to store with the key
344 * @param dtalen the length of the data to store with the key
345 * @param flags the flags to store along with the key
346 * @param exptime the expiry time for the key
348 static void storage_command(command
*cmd
, uint8_t cc
, const void *key
, size_t keylen
,
349 const void *dta
, size_t dtalen
, uint32_t flags
, uint32_t exptime
) {
350 /* all of the storage commands use the same command layout */
351 protocol_binary_request_set
*request
= &cmd
->set
;
353 memset(request
, 0, sizeof(*request
));
354 request
->message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
355 request
->message
.header
.request
.opcode
= cc
;
356 request
->message
.header
.request
.keylen
= (uint16_t) keylen
;
357 request
->message
.header
.request
.extlen
= 8;
358 request
->message
.header
.request
.bodylen
= (uint32_t)(keylen
+ 8 + dtalen
);
359 request
->message
.header
.request
.opaque
= 0xdeadbeef;
360 request
->message
.body
.flags
= flags
;
361 request
->message
.body
.expiration
= exptime
;
363 off_t key_offset
= sizeof(protocol_binary_request_no_extras
) + 8;
364 memcpy(cmd
->bytes
+ key_offset
, key
, keylen
);
366 memcpy(cmd
->bytes
+ key_offset
+ keylen
, dta
, dtalen
);
370 * Create a basic command to send to the server
371 * @param cmd destination buffer
372 * @param cc the command to create
373 * @param key the key to store
374 * @param keylen the length of the key
375 * @param dta the data to store with the key
376 * @param dtalen the length of the data to store with the key
378 static void raw_command(command
*cmd
, uint8_t cc
, const void *key
, size_t keylen
, const void *dta
,
380 /* all of the storage commands use the same command layout */
381 memset(cmd
, 0, sizeof(*cmd
));
382 cmd
->plain
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
383 cmd
->plain
.message
.header
.request
.opcode
= cc
;
384 cmd
->plain
.message
.header
.request
.keylen
= (uint16_t) keylen
;
385 cmd
->plain
.message
.header
.request
.bodylen
= (uint32_t)(keylen
+ dtalen
);
386 cmd
->plain
.message
.header
.request
.opaque
= 0xdeadbeef;
388 off_t key_offset
= sizeof(protocol_binary_request_no_extras
);
391 memcpy(cmd
->bytes
+ key_offset
, key
, keylen
);
394 memcpy(cmd
->bytes
+ key_offset
+ keylen
, dta
, dtalen
);
398 * Create the flush command
399 * @param cmd destination buffer
400 * @param cc the command to create (FLUSH/FLUSHQ)
401 * @param exptime when to flush
402 * @param use_extra to force using of the extra field?
404 static void flush_command(command
*cmd
, uint8_t cc
, uint32_t exptime
, bool use_extra
) {
405 memset(cmd
, 0, sizeof(cmd
->flush
));
406 cmd
->flush
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
407 cmd
->flush
.message
.header
.request
.opcode
= cc
;
408 cmd
->flush
.message
.header
.request
.opaque
= 0xdeadbeef;
410 if (exptime
|| use_extra
) {
411 cmd
->flush
.message
.header
.request
.extlen
= 4;
412 cmd
->flush
.message
.body
.expiration
= htonl(exptime
);
413 cmd
->flush
.message
.header
.request
.bodylen
= 4;
418 * Create a incr/decr command
419 * @param cc the cmd to create (FLUSH/FLUSHQ)
420 * @param key the key to operate on
421 * @param keylen the number of bytes in the key
422 * @param delta the number to add/subtract
423 * @param initial the initial value if the key doesn't exist
424 * @param exptime when the key should expire if it isn't set
426 static void arithmetic_command(command
*cmd
, uint8_t cc
, const void *key
, size_t keylen
,
427 uint64_t delta
, uint64_t initial
, uint32_t exptime
) {
428 memset(cmd
, 0, sizeof(cmd
->incr
));
429 cmd
->incr
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
430 cmd
->incr
.message
.header
.request
.opcode
= cc
;
431 cmd
->incr
.message
.header
.request
.keylen
= (uint16_t) keylen
;
432 cmd
->incr
.message
.header
.request
.extlen
= 20;
433 cmd
->incr
.message
.header
.request
.bodylen
= (uint32_t)(keylen
+ 20);
434 cmd
->incr
.message
.header
.request
.opaque
= 0xdeadbeef;
435 cmd
->incr
.message
.body
.delta
= memcached_htonll(delta
);
436 cmd
->incr
.message
.body
.initial
= memcached_htonll(initial
);
437 cmd
->incr
.message
.body
.expiration
= htonl(exptime
);
439 off_t key_offset
= sizeof(protocol_binary_request_no_extras
) + 20;
440 memcpy(cmd
->bytes
+ key_offset
, key
, keylen
);
444 * Validate the response header from the server
445 * @param rsp the response to check
446 * @param cc the expected command
447 * @param status the expected status
449 static enum test_return
do_validate_response_header(response
*rsp
, uint8_t cc
, uint16_t status
) {
450 verify(rsp
->plain
.message
.header
.response
.magic
== PROTOCOL_BINARY_RES
);
451 verify(rsp
->plain
.message
.header
.response
.opcode
== cc
);
452 verify(rsp
->plain
.message
.header
.response
.datatype
== PROTOCOL_BINARY_RAW_BYTES
);
453 verify(rsp
->plain
.message
.header
.response
.status
== status
);
454 verify(rsp
->plain
.message
.header
.response
.opaque
== 0xdeadbeef);
456 if (status
== PROTOCOL_BINARY_RESPONSE_SUCCESS
) {
458 case PROTOCOL_BINARY_CMD_ADDQ
:
459 case PROTOCOL_BINARY_CMD_APPENDQ
:
460 case PROTOCOL_BINARY_CMD_DECREMENTQ
:
461 case PROTOCOL_BINARY_CMD_DELETEQ
:
462 case PROTOCOL_BINARY_CMD_FLUSHQ
:
463 case PROTOCOL_BINARY_CMD_INCREMENTQ
:
464 case PROTOCOL_BINARY_CMD_PREPENDQ
:
465 case PROTOCOL_BINARY_CMD_QUITQ
:
466 case PROTOCOL_BINARY_CMD_REPLACEQ
:
467 case PROTOCOL_BINARY_CMD_SETQ
:
468 verify("Quiet command shouldn't return on success" == NULL
);
475 case PROTOCOL_BINARY_CMD_ADD
:
476 case PROTOCOL_BINARY_CMD_REPLACE
:
477 case PROTOCOL_BINARY_CMD_SET
:
478 case PROTOCOL_BINARY_CMD_APPEND
:
479 case PROTOCOL_BINARY_CMD_PREPEND
:
480 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
481 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
482 verify(rsp
->plain
.message
.header
.response
.bodylen
== 0);
483 verify(rsp
->plain
.message
.header
.response
.cas
);
485 case PROTOCOL_BINARY_CMD_FLUSH
:
486 case PROTOCOL_BINARY_CMD_NOOP
:
487 case PROTOCOL_BINARY_CMD_QUIT
:
488 case PROTOCOL_BINARY_CMD_DELETE
:
489 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
490 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
491 verify(rsp
->plain
.message
.header
.response
.bodylen
== 0);
492 verify(rsp
->plain
.message
.header
.response
.cas
== 0);
495 case PROTOCOL_BINARY_CMD_DECREMENT
:
496 case PROTOCOL_BINARY_CMD_INCREMENT
:
497 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
498 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
499 verify(rsp
->plain
.message
.header
.response
.bodylen
== 8);
500 verify(rsp
->plain
.message
.header
.response
.cas
);
503 case PROTOCOL_BINARY_CMD_STAT
:
504 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
505 /* key and value exists in all packets except in the terminating */
506 verify(rsp
->plain
.message
.header
.response
.cas
== 0);
509 case PROTOCOL_BINARY_CMD_VERSION
:
510 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
511 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
512 verify(rsp
->plain
.message
.header
.response
.bodylen
);
513 verify(rsp
->plain
.message
.header
.response
.cas
== 0);
516 case PROTOCOL_BINARY_CMD_GET
:
517 case PROTOCOL_BINARY_CMD_GETQ
:
518 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
519 verify(rsp
->plain
.message
.header
.response
.extlen
== 4);
520 verify(rsp
->plain
.message
.header
.response
.cas
);
523 case PROTOCOL_BINARY_CMD_GETK
:
524 case PROTOCOL_BINARY_CMD_GETKQ
:
525 verify(rsp
->plain
.message
.header
.response
.keylen
);
526 verify(rsp
->plain
.message
.header
.response
.extlen
== 4);
527 verify(rsp
->plain
.message
.header
.response
.cas
);
531 /* Undefined command code */
535 verify(rsp
->plain
.message
.header
.response
.cas
== 0);
536 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
537 if (cc
!= PROTOCOL_BINARY_CMD_GETK
) {
538 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
545 /* We call verify(validate_response_header), but that macro
546 * expects a boolean expression, and the function returns
547 * an enum.... Let's just create a macro to avoid cluttering
548 * the code with all of the == TEST_PASS ;-)
550 #define validate_response_header(a, b, c) do_validate_response_header(a, b, c) == TEST_PASS
552 static enum test_return
send_binary_noop(void) {
554 raw_command(&cmd
, PROTOCOL_BINARY_CMD_NOOP
, NULL
, 0, NULL
, 0);
555 execute(send_packet(&cmd
));
559 static enum test_return
receive_binary_noop(void) {
561 execute(recv_packet(&rsp
));
563 validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_NOOP
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
567 static enum test_return
test_binary_noop(void) {
568 execute(send_binary_noop());
569 execute(receive_binary_noop());
573 static enum test_return
test_binary_quit_impl(uint8_t cc
) {
576 raw_command(&cmd
, cc
, NULL
, 0, NULL
, 0);
578 execute(send_packet(&cmd
));
579 if (cc
== PROTOCOL_BINARY_CMD_QUIT
) {
580 execute(recv_packet(&rsp
));
582 validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_QUIT
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
585 /* Socket should be closed now, read should return EXIT_SUCCESS */
586 verify(timeout_io_op(sock
, POLLIN
, rsp
.bytes
, sizeof(rsp
.bytes
)) == 0);
588 return TEST_PASS_RECONNECT
;
591 static enum test_return
test_binary_quit(void) {
592 return test_binary_quit_impl(PROTOCOL_BINARY_CMD_QUIT
);
595 static enum test_return
test_binary_quitq(void) {
596 return test_binary_quit_impl(PROTOCOL_BINARY_CMD_QUITQ
);
599 static enum test_return
test_binary_set_impl(const char *key
, uint8_t cc
) {
603 uint64_t value
= 0xdeadbeefdeadcafeULL
;
604 storage_command(&cmd
, cc
, key
, strlen(key
), &value
, sizeof(value
), 0, 0);
606 /* set should always work */
607 for (int ii
= 0; ii
< 10; ii
++) {
609 execute(send_packet(&cmd
));
611 execute(resend_packet(&cmd
));
614 if (cc
== PROTOCOL_BINARY_CMD_SET
) {
615 execute(recv_packet(&rsp
));
616 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
618 execute(test_binary_noop());
622 * We need to get the current CAS id, and at this time we haven't
623 * verified that we have a working get
625 if (cc
== PROTOCOL_BINARY_CMD_SETQ
) {
626 cmd
.set
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_SET
;
627 execute(resend_packet(&cmd
));
628 execute(recv_packet(&rsp
));
630 validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_SET
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
631 cmd
.set
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_SETQ
;
634 /* try to set with the correct CAS value */
635 cmd
.plain
.message
.header
.request
.cas
= memcached_htonll(rsp
.plain
.message
.header
.response
.cas
);
636 execute(resend_packet(&cmd
));
637 if (cc
== PROTOCOL_BINARY_CMD_SET
) {
638 execute(recv_packet(&rsp
));
639 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
641 execute(test_binary_noop());
643 /* try to set with an incorrect CAS value */
644 cmd
.plain
.message
.header
.request
.cas
=
645 memcached_htonll(rsp
.plain
.message
.header
.response
.cas
- 1);
646 execute(resend_packet(&cmd
));
647 execute(send_binary_noop());
648 execute(recv_packet(&rsp
));
649 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS
));
650 execute(receive_binary_noop());
655 static enum test_return
test_binary_set(void) {
656 return test_binary_set_impl("test_binary_set", PROTOCOL_BINARY_CMD_SET
);
659 static enum test_return
test_binary_setq(void) {
660 return test_binary_set_impl("test_binary_setq", PROTOCOL_BINARY_CMD_SETQ
);
663 static enum test_return
test_binary_add_impl(const char *key
, uint8_t cc
) {
666 uint64_t value
= 0xdeadbeefdeadcafeULL
;
667 storage_command(&cmd
, cc
, key
, strlen(key
), &value
, sizeof(value
), 0, 0);
669 /* first add should work, rest of them should fail (even with cas
671 for (int ii
= 0; ii
< 10; ii
++) {
673 execute(send_packet(&cmd
));
675 execute(resend_packet(&cmd
));
677 if (cc
== PROTOCOL_BINARY_CMD_ADD
|| ii
> 0) {
678 uint16_t expected_result
;
680 expected_result
= PROTOCOL_BINARY_RESPONSE_SUCCESS
;
682 expected_result
= PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS
;
684 execute(send_binary_noop());
685 execute(recv_packet(&rsp
));
686 execute(receive_binary_noop());
687 verify(validate_response_header(&rsp
, cc
, expected_result
));
689 execute(test_binary_noop());
695 static enum test_return
test_binary_add(void) {
696 return test_binary_add_impl("test_binary_add", PROTOCOL_BINARY_CMD_ADD
);
699 static enum test_return
test_binary_addq(void) {
700 return test_binary_add_impl("test_binary_addq", PROTOCOL_BINARY_CMD_ADDQ
);
703 static enum test_return
binary_set_item(const char *key
, const char *value
) {
706 storage_command(&cmd
, PROTOCOL_BINARY_CMD_SET
, key
, strlen(key
), value
, strlen(value
), 0, 0);
707 execute(send_packet(&cmd
));
708 execute(recv_packet(&rsp
));
709 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_SET
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
713 static enum test_return
test_binary_replace_impl(const char *key
, uint8_t cc
) {
716 uint64_t value
= 0xdeadbeefdeadcafeULL
;
717 storage_command(&cmd
, cc
, key
, strlen(key
), &value
, sizeof(value
), 0, 0);
719 /* first replace should fail, successive should succeed (when the
721 for (int ii
= 0; ii
< 10; ii
++) {
723 execute(send_packet(&cmd
));
725 execute(resend_packet(&cmd
));
728 if (cc
== PROTOCOL_BINARY_CMD_REPLACE
|| ii
== 0) {
729 uint16_t expected_result
;
731 expected_result
= PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
;
733 expected_result
= PROTOCOL_BINARY_RESPONSE_SUCCESS
;
736 execute(send_binary_noop());
737 execute(recv_packet(&rsp
));
738 execute(receive_binary_noop());
739 verify(validate_response_header(&rsp
, cc
, expected_result
));
742 execute(binary_set_item(key
, key
));
744 execute(test_binary_noop());
748 /* verify that replace with CAS value works! */
749 cmd
.plain
.message
.header
.request
.cas
= memcached_htonll(rsp
.plain
.message
.header
.response
.cas
);
750 execute(resend_packet(&cmd
));
752 if (cc
== PROTOCOL_BINARY_CMD_REPLACE
) {
753 execute(recv_packet(&rsp
));
754 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
756 execute(test_binary_noop());
758 /* try to set with an incorrect CAS value */
759 cmd
.plain
.message
.header
.request
.cas
=
760 memcached_htonll(rsp
.plain
.message
.header
.response
.cas
- 1);
761 execute(resend_packet(&cmd
));
762 execute(send_binary_noop());
763 execute(recv_packet(&rsp
));
764 execute(receive_binary_noop());
765 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS
));
770 static enum test_return
test_binary_replace(void) {
771 return test_binary_replace_impl("test_binary_replace", PROTOCOL_BINARY_CMD_REPLACE
);
774 static enum test_return
test_binary_replaceq(void) {
775 return test_binary_replace_impl("test_binary_replaceq", PROTOCOL_BINARY_CMD_REPLACEQ
);
778 static enum test_return
test_binary_delete_impl(const char *key
, uint8_t cc
) {
781 raw_command(&cmd
, cc
, key
, strlen(key
), NULL
, 0);
783 /* The delete shouldn't work the first time, because the item isn't there */
784 execute(send_packet(&cmd
));
785 execute(send_binary_noop());
786 execute(recv_packet(&rsp
));
787 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
));
788 execute(receive_binary_noop());
789 execute(binary_set_item(key
, key
));
791 /* The item should be present now, resend*/
792 execute(resend_packet(&cmd
));
793 if (cc
== PROTOCOL_BINARY_CMD_DELETE
) {
794 execute(recv_packet(&rsp
));
795 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
798 execute(test_binary_noop());
803 static enum test_return
test_binary_delete(void) {
804 return test_binary_delete_impl("test_binary_delete", PROTOCOL_BINARY_CMD_DELETE
);
807 static enum test_return
test_binary_deleteq(void) {
808 return test_binary_delete_impl("test_binary_deleteq", PROTOCOL_BINARY_CMD_DELETEQ
);
811 static enum test_return
test_binary_get_impl(const char *key
, uint8_t cc
) {
815 raw_command(&cmd
, cc
, key
, strlen(key
), NULL
, 0);
816 execute(send_packet(&cmd
));
817 execute(send_binary_noop());
819 if (cc
== PROTOCOL_BINARY_CMD_GET
|| cc
== PROTOCOL_BINARY_CMD_GETK
) {
820 execute(recv_packet(&rsp
));
821 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
));
824 execute(receive_binary_noop());
826 execute(binary_set_item(key
, key
));
827 execute(resend_packet(&cmd
));
828 execute(send_binary_noop());
830 execute(recv_packet(&rsp
));
831 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
832 execute(receive_binary_noop());
837 static enum test_return
test_binary_get(void) {
838 return test_binary_get_impl("test_binary_get", PROTOCOL_BINARY_CMD_GET
);
841 static enum test_return
test_binary_getk(void) {
842 return test_binary_get_impl("test_binary_getk", PROTOCOL_BINARY_CMD_GETK
);
845 static enum test_return
test_binary_getq(void) {
846 return test_binary_get_impl("test_binary_getq", PROTOCOL_BINARY_CMD_GETQ
);
849 static enum test_return
test_binary_getkq(void) {
850 return test_binary_get_impl("test_binary_getkq", PROTOCOL_BINARY_CMD_GETKQ
);
853 static enum test_return
test_binary_incr_impl(const char *key
, uint8_t cc
) {
856 arithmetic_command(&cmd
, cc
, key
, strlen(key
), 1, 0, 0);
859 for (ii
= 0; ii
< 10; ++ii
) {
861 execute(send_packet(&cmd
));
863 execute(resend_packet(&cmd
));
865 if (cc
== PROTOCOL_BINARY_CMD_INCREMENT
) {
866 execute(recv_packet(&rsp
));
867 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
868 verify(memcached_ntohll(rsp
.incr
.message
.body
.value
) == ii
);
870 execute(test_binary_noop());
873 /* @todo add incorrect CAS */
877 static enum test_return
test_binary_incr(void) {
878 return test_binary_incr_impl("test_binary_incr", PROTOCOL_BINARY_CMD_INCREMENT
);
881 static enum test_return
test_binary_incrq(void) {
882 return test_binary_incr_impl("test_binary_incrq", PROTOCOL_BINARY_CMD_INCREMENTQ
);
885 static enum test_return
test_binary_decr_impl(const char *key
, uint8_t cc
) {
888 arithmetic_command(&cmd
, cc
, key
, strlen(key
), 1, 9, 0);
891 for (ii
= 9; ii
> -1; --ii
) {
893 execute(send_packet(&cmd
));
895 execute(resend_packet(&cmd
));
897 if (cc
== PROTOCOL_BINARY_CMD_DECREMENT
) {
898 execute(recv_packet(&rsp
));
899 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
900 verify(memcached_ntohll(rsp
.decr
.message
.body
.value
) == (uint64_t) ii
);
902 execute(test_binary_noop());
905 /* decr 0 should not wrap */
906 execute(resend_packet(&cmd
));
907 if (cc
== PROTOCOL_BINARY_CMD_DECREMENT
) {
908 execute(recv_packet(&rsp
));
909 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
910 verify(memcached_ntohll(rsp
.decr
.message
.body
.value
) == 0);
912 /* @todo get the value and verify! */
915 /* @todo add incorrect cas */
916 execute(test_binary_noop());
920 static enum test_return
test_binary_decr(void) {
921 return test_binary_decr_impl("test_binary_decr", PROTOCOL_BINARY_CMD_DECREMENT
);
924 static enum test_return
test_binary_decrq(void) {
925 return test_binary_decr_impl("test_binary_decrq", PROTOCOL_BINARY_CMD_DECREMENTQ
);
928 static enum test_return
test_binary_version(void) {
931 raw_command(&cmd
, PROTOCOL_BINARY_CMD_VERSION
, NULL
, 0, NULL
, 0);
933 execute(send_packet(&cmd
));
934 execute(recv_packet(&rsp
));
935 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_VERSION
,
936 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
941 static enum test_return
test_binary_flush_impl(const char *key
, uint8_t cc
) {
945 for (int ii
= 0; ii
< 2; ++ii
) {
946 execute(binary_set_item(key
, key
));
947 flush_command(&cmd
, cc
, 0, ii
== 0);
948 execute(send_packet(&cmd
));
950 if (cc
== PROTOCOL_BINARY_CMD_FLUSH
) {
951 execute(recv_packet(&rsp
));
952 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
954 execute(test_binary_noop());
956 raw_command(&cmd
, PROTOCOL_BINARY_CMD_GET
, key
, strlen(key
), NULL
, 0);
957 execute(send_packet(&cmd
));
958 execute(recv_packet(&rsp
));
959 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_GET
,
960 PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
));
966 static enum test_return
test_binary_flush(void) {
967 return test_binary_flush_impl("test_binary_flush", PROTOCOL_BINARY_CMD_FLUSH
);
970 static enum test_return
test_binary_flushq(void) {
971 return test_binary_flush_impl("test_binary_flushq", PROTOCOL_BINARY_CMD_FLUSHQ
);
974 static enum test_return
test_binary_concat_impl(const char *key
, uint8_t cc
) {
979 if (cc
== PROTOCOL_BINARY_CMD_APPEND
|| cc
== PROTOCOL_BINARY_CMD_APPENDQ
) {
985 execute(binary_set_item(key
, value
));
987 if (cc
== PROTOCOL_BINARY_CMD_APPEND
|| cc
== PROTOCOL_BINARY_CMD_APPENDQ
) {
993 raw_command(&cmd
, cc
, key
, strlen(key
), value
, strlen(value
));
994 execute(send_packet(&cmd
));
995 if (cc
== PROTOCOL_BINARY_CMD_APPEND
|| cc
== PROTOCOL_BINARY_CMD_PREPEND
) {
996 execute(recv_packet(&rsp
));
997 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
999 execute(test_binary_noop());
1002 raw_command(&cmd
, PROTOCOL_BINARY_CMD_GET
, key
, strlen(key
), NULL
, 0);
1003 execute(send_packet(&cmd
));
1004 execute(recv_packet(&rsp
));
1005 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_GET
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1006 verify(rsp
.plain
.message
.header
.response
.bodylen
- 4 == 11);
1007 verify(memcmp(rsp
.bytes
+ 28, "hello world", 11) == 0);
1012 static enum test_return
test_binary_append(void) {
1013 return test_binary_concat_impl("test_binary_append", PROTOCOL_BINARY_CMD_APPEND
);
1016 static enum test_return
test_binary_prepend(void) {
1017 return test_binary_concat_impl("test_binary_prepend", PROTOCOL_BINARY_CMD_PREPEND
);
1020 static enum test_return
test_binary_appendq(void) {
1021 return test_binary_concat_impl("test_binary_appendq", PROTOCOL_BINARY_CMD_APPENDQ
);
1024 static enum test_return
test_binary_prependq(void) {
1025 return test_binary_concat_impl("test_binary_prependq", PROTOCOL_BINARY_CMD_PREPENDQ
);
1028 static enum test_return
test_binary_stat(void) {
1032 raw_command(&cmd
, PROTOCOL_BINARY_CMD_STAT
, NULL
, 0, NULL
, 0);
1033 execute(send_packet(&cmd
));
1036 execute(recv_packet(&rsp
));
1038 validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_STAT
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1039 } while (rsp
.plain
.message
.header
.response
.keylen
);
1044 static enum test_return
send_string(const char *cmd
) {
1045 execute(retry_write(cmd
, strlen(cmd
)));
1049 static enum test_return
receive_line(char *buffer
, size_t size
) {
1051 while (offset
< size
) {
1052 execute(retry_read(buffer
+ offset
, 1));
1053 if (buffer
[offset
] == '\n') {
1054 if (offset
+ 1 < size
) {
1055 buffer
[offset
+ 1] = '\0';
1066 static enum test_return
receive_response(const char *msg
) {
1068 execute(receive_line(buffer
, sizeof(buffer
)));
1069 if (strcmp(msg
, buffer
)) {
1070 fprintf(stderr
, "[%s]\n", buffer
);
1072 verify(strcmp(msg
, buffer
) == 0);
1076 static enum test_return
receive_error_response(void) {
1078 execute(receive_line(buffer
, sizeof(buffer
)));
1079 verify(strncmp(buffer
, "ERROR", 5) == 0 || strncmp(buffer
, "CLIENT_ERROR", 12) == 0
1080 || strncmp(buffer
, "SERVER_ERROR", 12) == 0);
1084 static enum test_return
test_ascii_quit(void) {
1085 if (!v16x_or_greater
) {
1086 /* Verify that quit handles unknown options */
1087 execute(send_string("quit foo bar\r\n"));
1088 execute(receive_error_response());
1090 /* quit doesn't support noreply */
1091 execute(send_string("quit noreply\r\n"));
1092 execute(receive_error_response());
1095 /* Verify that quit works */
1096 execute(send_string("quit\r\n"));
1098 /* Socket should be closed now, read should return EXIT_SUCCESS */
1100 verify(timeout_io_op(sock
, POLLIN
, buffer
, sizeof(buffer
)) == 0);
1101 return TEST_PASS_RECONNECT
;
1104 static enum test_return
test_ascii_version(void) {
1105 /* Verify that version works */
1106 execute(send_string("version\r\n"));
1108 execute(receive_line(buffer
, sizeof(buffer
)));
1109 verify(strncmp(buffer
, "VERSION ", 8) == 0);
1111 char *version
= &buffer
[sizeof("VERSION")];
1112 if (version
[0] > '1' || (version
[0] == '1' && version
[2] >= '6')) {
1113 v16x_or_greater
= true;
1116 /* Verify that version command handles unknown options */
1117 execute(send_string("version foo bar\r\n"));
1118 if (v16x_or_greater
) {
1119 execute(receive_line(buffer
, sizeof(buffer
)));
1120 verify(strncmp(buffer
, "VERSION ", 8) == 0);
1122 execute(receive_error_response());
1124 /* version doesn't support noreply */
1125 execute(send_string("version noreply\r\n"));
1126 if (v16x_or_greater
) {
1127 execute(receive_line(buffer
, sizeof(buffer
)));
1128 verify(strncmp(buffer
, "VERSION ", 8) == 0);
1130 execute(receive_error_response());
1135 static enum test_return
test_ascii_verbosity(void) {
1136 /* This command does not adhere to the spec! */
1137 execute(send_string("verbosity foo bar my\r\n"));
1138 execute(receive_error_response());
1140 execute(send_string("verbosity noreply\r\n"));
1141 execute(test_ascii_version());
1143 execute(send_string("verbosity 0 noreply\r\n"));
1144 execute(test_ascii_version());
1146 execute(send_string("verbosity\r\n"));
1147 execute(receive_error_response());
1149 execute(send_string("verbosity 1\r\n"));
1150 execute(receive_response("OK\r\n"));
1152 execute(send_string("verbosity 0\r\n"));
1153 execute(receive_response("OK\r\n"));
1158 static enum test_return
test_ascii_set_impl(const char *key
, bool noreply
) {
1159 /* @todo add tests for bogus format! */
1161 snprintf(buffer
, sizeof(buffer
), "set %s 0 0 5%s\r\nvalue\r\n", key
, noreply
? " noreply" : "");
1162 execute(send_string(buffer
));
1165 execute(receive_response("STORED\r\n"));
1168 return test_ascii_version();
1171 static enum test_return
test_ascii_set(void) {
1172 return test_ascii_set_impl("test_ascii_set", false);
1175 static enum test_return
test_ascii_set_noreply(void) {
1176 return test_ascii_set_impl("test_ascii_set_noreply", true);
1179 static enum test_return
test_ascii_add_impl(const char *key
, bool noreply
) {
1180 /* @todo add tests for bogus format! */
1182 snprintf(buffer
, sizeof(buffer
), "add %s 0 0 5%s\r\nvalue\r\n", key
, noreply
? " noreply" : "");
1183 execute(send_string(buffer
));
1186 execute(receive_response("STORED\r\n"));
1189 execute(send_string(buffer
));
1192 execute(receive_response("NOT_STORED\r\n"));
1195 return test_ascii_version();
1198 static enum test_return
test_ascii_add(void) {
1199 return test_ascii_add_impl("test_ascii_add", false);
1202 static enum test_return
test_ascii_add_noreply(void) {
1203 return test_ascii_add_impl("test_ascii_add_noreply", true);
1206 static enum test_return
ascii_get_unknown_value(char **key
, char **value
, ssize_t
*ndata
) {
1209 execute(receive_line(buffer
, sizeof(buffer
)));
1210 verify(strncmp(buffer
, "VALUE ", 6) == 0);
1211 char *end
= strchr(buffer
+ 6, ' ');
1216 *key
= strdup(buffer
+ 6);
1218 char *ptr
= end
+ 1;
1221 unsigned long val
= strtoul(ptr
, &end
, 10); /* flags */
1227 *ndata
= (ssize_t
) strtoul(end
, &end
, 10); /* size */
1231 while (end
and *end
!= '\n' and isspace(*end
)) ++end
;
1232 verify(end
and *end
== '\n');
1234 *value
= static_cast<char *>(malloc((size_t) *ndata
));
1237 execute(retry_read(*value
, (size_t) *ndata
));
1239 execute(retry_read(buffer
, 2));
1240 verify(memcmp(buffer
, "\r\n", 2) == 0);
1245 static enum test_return
ascii_get_value(const char *key
, const char *value
) {
1247 size_t datasize
= strlen(value
);
1249 verify(datasize
< sizeof(buffer
));
1250 execute(receive_line(buffer
, sizeof(buffer
)));
1251 verify(strncmp(buffer
, "VALUE ", 6) == 0);
1252 verify(strncmp(buffer
+ 6, key
, strlen(key
)) == 0);
1253 char *ptr
= buffer
+ 6 + strlen(key
) + 1;
1257 unsigned long val
= strtoul(ptr
, &end
, 10); /* flags */
1264 val
= strtoul(end
, &end
, 10); /* size */
1267 verify(val
== datasize
);
1269 while (end
and *end
!= '\n' and isspace(*end
)) {
1272 verify(end
and *end
== '\n');
1274 execute(retry_read(buffer
, datasize
));
1275 verify(memcmp(buffer
, value
, datasize
) == 0);
1277 execute(retry_read(buffer
, 2));
1278 verify(memcmp(buffer
, "\r\n", 2) == 0);
1283 static enum test_return
ascii_get_item(const char *key
, const char *value
, bool exist
) {
1285 size_t datasize
= 0;
1287 datasize
= strlen(value
);
1290 verify(datasize
< sizeof(buffer
));
1291 snprintf(buffer
, sizeof(buffer
), "get %s\r\n", key
);
1292 execute(send_string(buffer
));
1295 execute(ascii_get_value(key
, value
));
1298 execute(retry_read(buffer
, 5));
1299 verify(memcmp(buffer
, "END\r\n", 5) == 0);
1304 static enum test_return
ascii_gets_value(const char *key
, const char *value
, unsigned long *cas
) {
1306 size_t datasize
= strlen(value
);
1308 verify(datasize
< sizeof(buffer
));
1309 execute(receive_line(buffer
, sizeof(buffer
)));
1310 verify(strncmp(buffer
, "VALUE ", 6) == 0);
1311 verify(strncmp(buffer
+ 6, key
, strlen(key
)) == 0);
1312 char *ptr
= buffer
+ 6 + strlen(key
) + 1;
1316 unsigned long val
= strtoul(ptr
, &end
, 10); /* flags */
1323 val
= strtoul(end
, &end
, 10); /* size */
1326 verify(val
== datasize
);
1330 *cas
= strtoul(end
, &end
, 10); /* cas */
1333 verify(val
== datasize
);
1336 while (end
and *end
!= '\n' and isspace(*end
)) {
1339 verify(end
and *end
== '\n');
1341 execute(retry_read(buffer
, datasize
));
1342 verify(memcmp(buffer
, value
, datasize
) == 0);
1344 execute(retry_read(buffer
, 2));
1345 verify(memcmp(buffer
, "\r\n", 2) == 0);
1350 static enum test_return
ascii_gets_item(const char *key
, const char *value
, bool exist
,
1351 unsigned long *cas
) {
1353 size_t datasize
= 0;
1355 datasize
= strlen(value
);
1358 verify(datasize
< sizeof(buffer
));
1359 snprintf(buffer
, sizeof(buffer
), "gets %s\r\n", key
);
1360 execute(send_string(buffer
));
1363 execute(ascii_gets_value(key
, value
, cas
));
1365 execute(retry_read(buffer
, 5));
1366 verify(memcmp(buffer
, "END\r\n", 5) == 0);
1371 static enum test_return
ascii_set_item(const char *key
, const char *value
) {
1373 size_t len
= strlen(value
);
1374 snprintf(buffer
, sizeof(buffer
), "set %s 0 0 %u\r\n", key
, (unsigned int) len
);
1375 execute(send_string(buffer
));
1376 execute(retry_write(value
, len
));
1377 execute(send_string("\r\n"));
1378 execute(receive_response("STORED\r\n"));
1382 static enum test_return
test_ascii_replace_impl(const char *key
, bool noreply
) {
1384 snprintf(buffer
, sizeof(buffer
), "replace %s 0 0 5%s\r\nvalue\r\n", key
,
1385 noreply
? " noreply" : "");
1386 execute(send_string(buffer
));
1389 execute(test_ascii_version());
1391 execute(receive_response("NOT_STORED\r\n"));
1394 execute(ascii_set_item(key
, "value"));
1395 execute(ascii_get_item(key
, "value", true));
1397 execute(send_string(buffer
));
1400 execute(test_ascii_version());
1402 execute(receive_response("STORED\r\n"));
1404 return test_ascii_version();
1407 static enum test_return
test_ascii_replace(void) {
1408 return test_ascii_replace_impl("test_ascii_replace", false);
1411 static enum test_return
test_ascii_replace_noreply(void) {
1412 return test_ascii_replace_impl("test_ascii_replace_noreply", true);
1415 static enum test_return
test_ascii_cas_impl(const char *key
, bool noreply
) {
1419 execute(ascii_set_item(key
, "value"));
1420 execute(ascii_gets_item(key
, "value", true, &cas
));
1422 snprintf(buffer
, sizeof(buffer
), "cas %s 0 0 6 %lu%s\r\nvalue2\r\n", key
, cas
,
1423 noreply
? " noreply" : "");
1424 execute(send_string(buffer
));
1427 execute(test_ascii_version());
1429 execute(receive_response("STORED\r\n"));
1432 /* reexecute the same command should fail due to illegal cas */
1433 execute(send_string(buffer
));
1436 execute(test_ascii_version());
1438 execute(receive_response("EXISTS\r\n"));
1441 return test_ascii_version();
1444 static enum test_return
test_ascii_cas(void) {
1445 return test_ascii_cas_impl("test_ascii_cas", false);
1448 static enum test_return
test_ascii_cas_noreply(void) {
1449 return test_ascii_cas_impl("test_ascii_cas_noreply", true);
1452 static enum test_return
test_ascii_delete_impl(const char *key
, bool noreply
) {
1453 execute(ascii_set_item(key
, "value"));
1455 execute(send_string("delete\r\n"));
1456 execute(receive_error_response());
1457 /* BUG: the server accepts delete a b */
1458 execute(send_string("delete a b c d e\r\n"));
1459 execute(receive_error_response());
1462 snprintf(buffer
, sizeof(buffer
), "delete %s%s\r\n", key
, noreply
? " noreply" : "");
1463 execute(send_string(buffer
));
1466 execute(test_ascii_version());
1468 execute(receive_response("DELETED\r\n"));
1470 execute(ascii_get_item(key
, "value", false));
1471 execute(send_string(buffer
));
1473 execute(test_ascii_version());
1475 execute(receive_response("NOT_FOUND\r\n"));
1480 static enum test_return
test_ascii_delete(void) {
1481 return test_ascii_delete_impl("test_ascii_delete", false);
1484 static enum test_return
test_ascii_delete_noreply(void) {
1485 return test_ascii_delete_impl("test_ascii_delete_noreply", true);
1488 static enum test_return
test_ascii_get(void) {
1489 execute(ascii_set_item("test_ascii_get", "value"));
1491 execute(send_string("get\r\n"));
1492 execute(receive_error_response());
1493 execute(ascii_get_item("test_ascii_get", "value", true));
1494 execute(ascii_get_item("test_ascii_get_notfound", "value", false));
1499 static enum test_return
test_ascii_gets(void) {
1500 execute(ascii_set_item("test_ascii_gets", "value"));
1502 execute(send_string("gets\r\n"));
1503 execute(receive_error_response());
1505 execute(ascii_gets_item("test_ascii_gets", "value", true, &cas
));
1506 execute(ascii_gets_item("test_ascii_gets_notfound", "value", false, &cas
));
1511 static enum test_return
test_ascii_mget(void) {
1512 const uint32_t nkeys
= 5;
1513 const char *const keys
[] = {"test_ascii_mget1", "test_ascii_mget2",
1514 /* test_ascii_mget_3 does not exist :) */
1515 "test_ascii_mget4", "test_ascii_mget5", "test_ascii_mget6"};
1517 for (uint32_t x
= 0; x
< nkeys
; ++x
) {
1518 execute(ascii_set_item(keys
[x
], "value"));
1521 /* Ask for a key that doesn't exist as well */
1522 execute(send_string("get test_ascii_mget1 test_ascii_mget2 test_ascii_mget3 "
1523 "test_ascii_mget4 test_ascii_mget5 "
1524 "test_ascii_mget6\r\n"));
1526 std::vector
<char *> returned
;
1527 returned
.resize(nkeys
);
1529 for (uint32_t x
= 0; x
< nkeys
; ++x
) {
1532 execute(ascii_get_unknown_value(&returned
[x
], &v
, &nbytes
));
1533 verify(nbytes
== 5);
1534 verify(memcmp(v
, "value", 5) == 0);
1539 execute(retry_read(buffer
, 5));
1540 verify(memcmp(buffer
, "END\r\n", 5) == 0);
1542 /* verify that we got all the keys we expected */
1543 for (uint32_t x
= 0; x
< nkeys
; ++x
) {
1545 for (uint32_t y
= 0; y
< nkeys
; ++y
) {
1546 if (strcmp(keys
[x
], returned
[y
]) == 0) {
1554 for (uint32_t x
= 0; x
< nkeys
; ++x
) {
1561 static enum test_return
test_ascii_incr_impl(const char *key
, bool noreply
) {
1563 snprintf(cmd
, sizeof(cmd
), "incr %s 1%s\r\n", key
, noreply
? " noreply" : "");
1565 execute(ascii_set_item(key
, "0"));
1566 for (int x
= 1; x
< 11; ++x
) {
1567 execute(send_string(cmd
));
1570 execute(test_ascii_version());
1573 execute(receive_line(buffer
, sizeof(buffer
)));
1574 int val
= atoi(buffer
);
1579 execute(ascii_get_item(key
, "10", true));
1584 static enum test_return
test_ascii_incr(void) {
1585 return test_ascii_incr_impl("test_ascii_incr", false);
1588 static enum test_return
test_ascii_incr_noreply(void) {
1589 return test_ascii_incr_impl("test_ascii_incr_noreply", true);
1592 static enum test_return
test_ascii_decr_impl(const char *key
, bool noreply
) {
1594 snprintf(cmd
, sizeof(cmd
), "decr %s 1%s\r\n", key
, noreply
? " noreply" : "");
1596 execute(ascii_set_item(key
, "9"));
1597 for (int x
= 8; x
> -1; --x
) {
1598 execute(send_string(cmd
));
1601 execute(test_ascii_version());
1604 execute(receive_line(buffer
, sizeof(buffer
)));
1605 int val
= atoi(buffer
);
1610 execute(ascii_get_item(key
, "0", true));
1612 /* verify that it doesn't wrap */
1613 execute(send_string(cmd
));
1615 execute(test_ascii_version());
1618 execute(receive_line(buffer
, sizeof(buffer
)));
1620 execute(ascii_get_item(key
, "0", true));
1625 static enum test_return
test_ascii_decr(void) {
1626 return test_ascii_decr_impl("test_ascii_decr", false);
1629 static enum test_return
test_ascii_decr_noreply(void) {
1630 return test_ascii_decr_impl("test_ascii_decr_noreply", true);
1633 static enum test_return
test_ascii_flush_impl(const char *key
, bool noreply
) {
1635 /* Verify that the flush_all command handles unknown options */
1636 /* Bug in the current memcached server! */
1637 execute(send_string("flush_all foo bar\r\n"));
1638 execute(receive_error_response());
1641 execute(ascii_set_item(key
, key
));
1642 execute(ascii_get_item(key
, key
, true));
1645 execute(send_string("flush_all noreply\r\n"));
1646 execute(test_ascii_version());
1648 execute(send_string("flush_all\r\n"));
1649 execute(receive_response("OK\r\n"));
1652 execute(ascii_get_item(key
, key
, false));
1657 static enum test_return
test_ascii_flush(void) {
1658 return test_ascii_flush_impl("test_ascii_flush", false);
1661 static enum test_return
test_ascii_flush_noreply(void) {
1662 return test_ascii_flush_impl("test_ascii_flush_noreply", true);
1665 static enum test_return
test_ascii_concat_impl(const char *key
, bool append
, bool noreply
) {
1673 execute(ascii_set_item(key
, value
));
1682 snprintf(cmd
, sizeof(cmd
), "%s %s 0 0 %u%s\r\n%s\r\n", append
? "append" : "prepend", key
,
1683 (unsigned int) strlen(value
), noreply
? " noreply" : "", value
);
1684 execute(send_string(cmd
));
1687 execute(test_ascii_version());
1689 execute(receive_response("STORED\r\n"));
1692 execute(ascii_get_item(key
, "hello world", true));
1694 snprintf(cmd
, sizeof(cmd
), "%s %s_notfound 0 0 %u%s\r\n%s\r\n", append
? "append" : "prepend",
1695 key
, (unsigned int) strlen(value
), noreply
? " noreply" : "", value
);
1696 execute(send_string(cmd
));
1699 execute(test_ascii_version());
1701 execute(receive_response("NOT_STORED\r\n"));
1707 static enum test_return
test_ascii_append(void) {
1708 return test_ascii_concat_impl("test_ascii_append", true, false);
1711 static enum test_return
test_ascii_prepend(void) {
1712 return test_ascii_concat_impl("test_ascii_prepend", false, false);
1715 static enum test_return
test_ascii_append_noreply(void) {
1716 return test_ascii_concat_impl("test_ascii_append_noreply", true, true);
1719 static enum test_return
test_ascii_prepend_noreply(void) {
1720 return test_ascii_concat_impl("test_ascii_prepend_noreply", false, true);
1723 static enum test_return
test_ascii_stat(void) {
1724 execute(send_string("stats noreply\r\n"));
1725 execute(receive_error_response());
1726 execute(send_string("stats\r\n"));
1729 execute(receive_line(buffer
, sizeof(buffer
)));
1730 } while (strcmp(buffer
, "END\r\n"));
1732 return TEST_PASS_RECONNECT
;
1735 typedef enum test_return (*TEST_FUNC
)(void);
1738 const char *description
;
1742 struct testcase testcases
[] = {{"ascii version", test_ascii_version
},
1743 {"ascii quit", test_ascii_quit
},
1744 {"ascii verbosity", test_ascii_verbosity
},
1745 {"ascii set", test_ascii_set
},
1746 {"ascii set noreply", test_ascii_set_noreply
},
1747 {"ascii get", test_ascii_get
},
1748 {"ascii gets", test_ascii_gets
},
1749 {"ascii mget", test_ascii_mget
},
1750 {"ascii flush", test_ascii_flush
},
1751 {"ascii flush noreply", test_ascii_flush_noreply
},
1752 {"ascii add", test_ascii_add
},
1753 {"ascii add noreply", test_ascii_add_noreply
},
1754 {"ascii replace", test_ascii_replace
},
1755 {"ascii replace noreply", test_ascii_replace_noreply
},
1756 {"ascii cas", test_ascii_cas
},
1757 {"ascii cas noreply", test_ascii_cas_noreply
},
1758 {"ascii delete", test_ascii_delete
},
1759 {"ascii delete noreply", test_ascii_delete_noreply
},
1760 {"ascii incr", test_ascii_incr
},
1761 {"ascii incr noreply", test_ascii_incr_noreply
},
1762 {"ascii decr", test_ascii_decr
},
1763 {"ascii decr noreply", test_ascii_decr_noreply
},
1764 {"ascii append", test_ascii_append
},
1765 {"ascii append noreply", test_ascii_append_noreply
},
1766 {"ascii prepend", test_ascii_prepend
},
1767 {"ascii prepend noreply", test_ascii_prepend_noreply
},
1768 {"ascii stat", test_ascii_stat
},
1769 {"binary noop", test_binary_noop
},
1770 {"binary quit", test_binary_quit
},
1771 {"binary quitq", test_binary_quitq
},
1772 {"binary set", test_binary_set
},
1773 {"binary setq", test_binary_setq
},
1774 {"binary flush", test_binary_flush
},
1775 {"binary flushq", test_binary_flushq
},
1776 {"binary add", test_binary_add
},
1777 {"binary addq", test_binary_addq
},
1778 {"binary replace", test_binary_replace
},
1779 {"binary replaceq", test_binary_replaceq
},
1780 {"binary delete", test_binary_delete
},
1781 {"binary deleteq", test_binary_deleteq
},
1782 {"binary get", test_binary_get
},
1783 {"binary getq", test_binary_getq
},
1784 {"binary getk", test_binary_getk
},
1785 {"binary getkq", test_binary_getkq
},
1786 {"binary incr", test_binary_incr
},
1787 {"binary incrq", test_binary_incrq
},
1788 {"binary decr", test_binary_decr
},
1789 {"binary decrq", test_binary_decrq
},
1790 {"binary version", test_binary_version
},
1791 {"binary append", test_binary_append
},
1792 {"binary appendq", test_binary_appendq
},
1793 {"binary prepend", test_binary_prepend
},
1794 {"binary prependq", test_binary_prependq
},
1795 {"binary stat", test_binary_stat
},
1798 struct test_type_st
{
1803 int main(int argc
, char **argv
) {
1804 static const char *const status_msg
[] = {"[skip]", "[pass]", "[pass]", "[FAIL]"};
1805 struct test_type_st tests
= {true, true};
1808 const char *hostname
= NULL
;
1809 const char *port
= MEMCACHED_DEFAULT_PORT_STRING
;
1811 bool prompt
= false;
1812 const char *testname
= NULL
;
1814 while ((cmd
= getopt(argc
, argv
, "qt:vch:p:PT:?ab")) != EOF
) {
1818 tests
.binary
= false;
1822 tests
.ascii
= false;
1823 tests
.binary
= true;
1827 timeout
= atoi(optarg
);
1829 fprintf(stderr
, "Invalid timeout. Please specify a number for -t\n");
1830 return EXIT_FAILURE
;
1864 "Usage: %s [-h hostname] [-p port] [-c] [-v] [-t n] [-P] [-T testname]'\n"
1865 "\t-c\tGenerate coredump if a test fails\n"
1866 "\t-v\tVerbose test output (print out the assertion)\n"
1867 "\t-t n\tSet the timeout for io-operations to n seconds\n"
1868 "\t-P\tPrompt the user before starting a test.\n"
1869 "\t\t\t\"skip\" will skip the test\n"
1870 "\t\t\t\"quit\" will terminate memcapable\n"
1871 "\t\t\tEverything else will start the test\n"
1872 "\t-T n\tJust run the test named n\n"
1873 "\t-a\tOnly test the ascii protocol\n"
1874 "\t-b\tOnly test the binary protocol\n",
1876 return EXIT_SUCCESS
;
1881 fprintf(stderr
, "No hostname was provided.\n");
1882 return EXIT_FAILURE
;
1885 initialize_sockets();
1886 sock
= connect_server(hostname
, port
);
1887 if (sock
== INVALID_SOCKET
) {
1888 fprintf(stderr
, "Failed to connect to <%s:%s>: %s\n", hostname
?: "(null)", port
?: "(null)",
1889 strerror(get_socket_errno()));
1890 return EXIT_FAILURE
;
1893 for (int ii
= 0; testcases
[ii
].description
; ++ii
) {
1894 if (testname
&& strcmp(testcases
[ii
].description
, testname
)) {
1898 if ((testcases
[ii
].description
[0] == 'a' && (tests
.ascii
) == 0)
1899 || (testcases
[ii
].description
[0] == 'b' && (tests
.binary
) == 0))
1904 fprintf(stdout
, "%-40s", testcases
[ii
].description
);
1908 fprintf(stdout
, "\nPress <return> when you are ready? ");
1909 char buffer
[80] = {0};
1910 if (fgets(buffer
, sizeof(buffer
), stdin
)) {
1911 if (strncmp(buffer
, "skip", 4) == 0) {
1912 fprintf(stdout
, "%-40s%s\n", testcases
[ii
].description
, status_msg
[TEST_SKIP
]);
1916 if (strncmp(buffer
, "quit", 4) == 0) {
1921 fprintf(stdout
, "%-40s", testcases
[ii
].description
);
1925 bool reconnect
= false;
1926 enum test_return ret
= testcases
[ii
].function();
1927 if (ret
== TEST_FAIL
) {
1931 fprintf(stderr
, "\n");
1933 } else if (ret
== TEST_PASS_RECONNECT
) {
1937 if (ret
== TEST_FAIL
) {
1938 fprintf(stderr
, "%s\n", status_msg
[ret
]);
1940 fprintf(stdout
, "%s\n", status_msg
[ret
]);
1945 if ((sock
= connect_server(hostname
, port
)) == INVALID_SOCKET
) {
1946 fprintf(stderr
, "Failed to connect to <%s:%s>: %s\n", hostname
?: "(null)",
1947 port
?: "(null)", strerror(get_socket_errno()));
1948 fprintf(stderr
, "%d of %d tests failed\n", failed
, total
);
1949 return EXIT_FAILURE
;
1956 fprintf(stdout
, "All tests passed\n");
1958 fprintf(stderr
, "%d of %d tests failed\n", failed
, total
);
1961 return (failed
== 0) ? EXIT_SUCCESS
: EXIT_FAILURE
;