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"
30 #include <sys/types.h>
36 # include "../../win32/getopt.h"
39 #include "libmemcached-1.0/memcached.h"
40 #include "libmemcached/poll.h"
41 #include "libmemcached/socket.hpp"
42 #include "libmemcachedprotocol-0.0/binary.h"
43 #include "libmemcached/byteorder.h"
48 /* /usr/include/netinet/in.h defines macros from ntohs() to _bswap_nn to
49 * optimize the conversion functions, but the prototypes generate warnings
50 * from gcc. The conversion methods isn't the bottleneck for my app, so
51 * just remove the warnings by undef'ing the optimization ..
57 /* Should we generate coredumps when we enounter an error (-c) */
58 static bool do_core
= false;
59 /* connection to the server */
60 static memcached_socket_t sock
;
61 /* Should the output from test failures be verbose or quiet? */
62 static bool verbose
= false;
64 /* The number of seconds to wait for an IO-operation */
65 static int timeout
= 2;
67 /* v1.6.x is more permissible */
68 static bool v16x_or_greater
= false;
71 * Instead of having to cast between the different datatypes we create
72 * a union of all of the different types of pacages we want to send.
73 * A lot of the different commands use the same packet layout, so I'll
74 * just define the different types I need. The typedefs only contain
75 * the header of the message, so we need some space for keys and body
76 * To avoid to have to do multiple writes, lets add a chunk of memory
77 * to use. 1k should be more than enough for header, key and body.
80 protocol_binary_request_no_extras plain
;
81 protocol_binary_request_flush flush
;
82 protocol_binary_request_incr incr
;
83 protocol_binary_request_set set
;
88 protocol_binary_response_no_extras plain
;
89 protocol_binary_response_incr incr
;
90 protocol_binary_response_decr decr
;
94 enum test_return
{ TEST_SKIP
, TEST_PASS
, TEST_PASS_RECONNECT
, TEST_FAIL
};
97 * Try to get an addrinfo struct for a given port on a given host
99 static struct addrinfo
*lookuphost(const char *hostname
, const char *port
) {
100 struct addrinfo
*ai
= 0;
101 struct addrinfo hints
;
102 memset(&hints
, 0, sizeof(struct addrinfo
));
103 hints
.ai_family
= AF_UNSPEC
;
104 hints
.ai_protocol
= IPPROTO_TCP
;
105 hints
.ai_socktype
= SOCK_STREAM
;
107 int error
= getaddrinfo(hostname
, port
, &hints
, &ai
);
109 if (error
!= EAI_SYSTEM
) {
110 fprintf(stderr
, "getaddrinfo(): %s\n", gai_strerror(error
));
112 perror("getaddrinfo()");
120 * Set the socket in nonblocking mode
121 * @return -1 if failure, the socket otherwise
123 static memcached_socket_t
set_noblock(void) {
126 if (ioctlsocket(sock
, FIONBIO
, &arg
) == SOCKET_ERROR
) {
127 perror("Failed to set nonblocking io");
129 return INVALID_SOCKET
;
132 int flags
= fcntl(sock
, F_GETFL
, 0);
134 perror("Failed to get socket flags");
135 memcached_close_socket(sock
);
136 return INVALID_SOCKET
;
139 if ((flags
& O_NONBLOCK
) != O_NONBLOCK
) {
140 if (fcntl(sock
, F_SETFL
, flags
| O_NONBLOCK
) == -1) {
141 perror("Failed to set socket to nonblocking mode");
142 memcached_close_socket(sock
);
143 return INVALID_SOCKET
;
151 * Try to open a connection to the server
152 * @param hostname the name of the server to connect to
153 * @param port the port number (or service) to connect to
154 * @return positive integer if success, -1 otherwise
156 static memcached_socket_t
connect_server(const char *hostname
, const char *port
) {
157 struct addrinfo
*ai
= lookuphost(hostname
, port
);
158 sock
= INVALID_SOCKET
;
160 if ((sock
= socket(ai
->ai_family
, ai
->ai_socktype
, ai
->ai_protocol
)) != INVALID_SOCKET
) {
161 if (connect(sock
, ai
->ai_addr
, ai
->ai_addrlen
) == SOCKET_ERROR
) {
162 fprintf(stderr
, "Failed to connect socket: %s\n", strerror(get_socket_errno()));
164 sock
= INVALID_SOCKET
;
166 sock
= set_noblock();
169 fprintf(stderr
, "Failed to create socket: %s\n", strerror(get_socket_errno()));
178 static ssize_t
timeout_io_op(memcached_socket_t fd
, short direction
, const char *buf
, size_t len
) {
181 if (direction
== POLLOUT
) {
182 ret
= send(fd
, buf
, len
, 0);
184 ret
= recv(fd
, const_cast<char *>(buf
), len
, 0);
187 if (ret
== SOCKET_ERROR
&& get_socket_errno() == EWOULDBLOCK
) {
189 memset(&fds
, 0, sizeof(struct pollfd
));
190 fds
.events
= direction
;
193 int err
= poll(&fds
, 1, timeout
* 1000);
195 if (direction
== POLLOUT
) {
196 ret
= send(fd
, buf
, len
, 0);
198 ret
= recv(fd
, const_cast<char *>(buf
), len
, 0);
200 } else if (err
== 0) {
203 perror("Failed to poll");
212 * Ensure that an expression is true. If it isn't print out a message similar
213 * to assert() and create a coredump if the user wants that. If not an error
214 * message is returned.
217 static enum test_return
ensure(bool val
, const char *expression
, const char *file
, int line
) {
220 fprintf(stdout
, "\n%s:%d: %s", file
, line
, expression
);
233 #define verify(expression) \
235 if (ensure(expression, #expression, __FILE__, __LINE__) == TEST_FAIL) \
238 #define execute(expression) \
240 if (ensure(expression == TEST_PASS, #expression, __FILE__, __LINE__) == TEST_FAIL) \
245 * Send a chunk of memory over the socket (retry if the call is iterrupted
247 static enum test_return
retry_write(const void *buf
, size_t len
) {
249 const char *ptr
= static_cast<const char *>(buf
);
252 size_t num_bytes
= len
- offset
;
253 ssize_t nw
= timeout_io_op(sock
, POLLOUT
, (ptr
+ offset
), num_bytes
);
255 verify(get_socket_errno() == EINTR
|| get_socket_errno() == EAGAIN
);
257 offset
+= (size_t) nw
;
260 } while (offset
< len
);
266 * Resend a packet to the server (All fields in the command header should
267 * be in network byte order)
269 static enum test_return
resend_packet(command
*cmd
) {
271 sizeof(protocol_binary_request_no_extras
) + ntohl(cmd
->plain
.message
.header
.request
.bodylen
);
273 execute(retry_write(cmd
, length
));
278 * Send a command to the server. The command header needs to be updated
279 * to network byte order
281 static enum test_return
send_packet(command
*cmd
) {
282 /* Fix the byteorder of the header */
283 cmd
->plain
.message
.header
.request
.keylen
= ntohs(cmd
->plain
.message
.header
.request
.keylen
);
284 cmd
->plain
.message
.header
.request
.bodylen
= ntohl(cmd
->plain
.message
.header
.request
.bodylen
);
285 cmd
->plain
.message
.header
.request
.cas
= memcached_ntohll(cmd
->plain
.message
.header
.request
.cas
);
287 execute(resend_packet(cmd
));
292 * Read a fixed length chunk of data from the server
294 static enum test_return
retry_read(void *buf
, size_t len
) {
297 ssize_t nr
= timeout_io_op(sock
, POLLIN
, ((char *) buf
) + offset
, len
- offset
);
300 fprintf(stderr
, "Errno: %d %s\n", get_socket_errno(), strerror(errno
));
301 verify(get_socket_errno() == EINTR
|| get_socket_errno() == EAGAIN
);
308 offset
+= (size_t) nr
;
310 } while (offset
< len
);
316 * Receive a response from the server and conver the fields in the header
317 * to local byte order
319 static enum test_return
recv_packet(response
*rsp
) {
320 execute(retry_read(rsp
, sizeof(protocol_binary_response_no_extras
)));
322 /* Fix the byte order in the packet header */
323 rsp
->plain
.message
.header
.response
.keylen
= ntohs(rsp
->plain
.message
.header
.response
.keylen
);
324 rsp
->plain
.message
.header
.response
.status
= ntohs(rsp
->plain
.message
.header
.response
.status
);
325 rsp
->plain
.message
.header
.response
.bodylen
= ntohl(rsp
->plain
.message
.header
.response
.bodylen
);
326 rsp
->plain
.message
.header
.response
.cas
= memcached_ntohll(rsp
->plain
.message
.header
.response
.cas
);
328 size_t bodysz
= rsp
->plain
.message
.header
.response
.bodylen
;
330 execute(retry_read(rsp
->bytes
+ sizeof(protocol_binary_response_no_extras
), bodysz
));
336 * Create a storage command (add, set, replace etc)
338 * @param cmd destination buffer
339 * @param cc the storage command to create
340 * @param key the key to store
341 * @param keylen the length of the key
342 * @param dta the data to store with the key
343 * @param dtalen the length of the data to store with the key
344 * @param flags the flags to store along with the key
345 * @param exptime the expiry time for the key
347 static void storage_command(command
*cmd
, uint8_t cc
, const void *key
, size_t keylen
,
348 const void *dta
, size_t dtalen
, uint32_t flags
, uint32_t exptime
) {
349 /* all of the storage commands use the same command layout */
350 protocol_binary_request_set
*request
= &cmd
->set
;
352 memset(request
, 0, sizeof(*request
));
353 request
->message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
354 request
->message
.header
.request
.opcode
= cc
;
355 request
->message
.header
.request
.keylen
= (uint16_t) keylen
;
356 request
->message
.header
.request
.extlen
= 8;
357 request
->message
.header
.request
.bodylen
= (uint32_t)(keylen
+ 8 + dtalen
);
358 request
->message
.header
.request
.opaque
= 0xdeadbeef;
359 request
->message
.body
.flags
= flags
;
360 request
->message
.body
.expiration
= exptime
;
362 off_t key_offset
= sizeof(protocol_binary_request_no_extras
) + 8;
363 memcpy(cmd
->bytes
+ key_offset
, key
, keylen
);
365 memcpy(cmd
->bytes
+ key_offset
+ keylen
, dta
, dtalen
);
369 * Create a basic command to send to the server
370 * @param cmd destination buffer
371 * @param cc the command to create
372 * @param key the key to store
373 * @param keylen the length of the key
374 * @param dta the data to store with the key
375 * @param dtalen the length of the data to store with the key
377 static void raw_command(command
*cmd
, uint8_t cc
, const void *key
, size_t keylen
, const void *dta
,
379 /* all of the storage commands use the same command layout */
380 memset(cmd
, 0, sizeof(*cmd
));
381 cmd
->plain
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
382 cmd
->plain
.message
.header
.request
.opcode
= cc
;
383 cmd
->plain
.message
.header
.request
.keylen
= (uint16_t) keylen
;
384 cmd
->plain
.message
.header
.request
.bodylen
= (uint32_t)(keylen
+ dtalen
);
385 cmd
->plain
.message
.header
.request
.opaque
= 0xdeadbeef;
387 off_t key_offset
= sizeof(protocol_binary_request_no_extras
);
390 memcpy(cmd
->bytes
+ key_offset
, key
, keylen
);
393 memcpy(cmd
->bytes
+ key_offset
+ keylen
, dta
, dtalen
);
397 * Create the flush command
398 * @param cmd destination buffer
399 * @param cc the command to create (FLUSH/FLUSHQ)
400 * @param exptime when to flush
401 * @param use_extra to force using of the extra field?
403 static void flush_command(command
*cmd
, uint8_t cc
, uint32_t exptime
, bool use_extra
) {
404 memset(cmd
, 0, sizeof(cmd
->flush
));
405 cmd
->flush
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
406 cmd
->flush
.message
.header
.request
.opcode
= cc
;
407 cmd
->flush
.message
.header
.request
.opaque
= 0xdeadbeef;
409 if (exptime
|| use_extra
) {
410 cmd
->flush
.message
.header
.request
.extlen
= 4;
411 cmd
->flush
.message
.body
.expiration
= htonl(exptime
);
412 cmd
->flush
.message
.header
.request
.bodylen
= 4;
417 * Create a incr/decr command
418 * @param cc the cmd to create (FLUSH/FLUSHQ)
419 * @param key the key to operate on
420 * @param keylen the number of bytes in the key
421 * @param delta the number to add/subtract
422 * @param initial the initial value if the key doesn't exist
423 * @param exptime when the key should expire if it isn't set
425 static void arithmetic_command(command
*cmd
, uint8_t cc
, const void *key
, size_t keylen
,
426 uint64_t delta
, uint64_t initial
, uint32_t exptime
) {
427 memset(cmd
, 0, sizeof(cmd
->incr
));
428 cmd
->incr
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
429 cmd
->incr
.message
.header
.request
.opcode
= cc
;
430 cmd
->incr
.message
.header
.request
.keylen
= (uint16_t) keylen
;
431 cmd
->incr
.message
.header
.request
.extlen
= 20;
432 cmd
->incr
.message
.header
.request
.bodylen
= (uint32_t)(keylen
+ 20);
433 cmd
->incr
.message
.header
.request
.opaque
= 0xdeadbeef;
434 cmd
->incr
.message
.body
.delta
= memcached_htonll(delta
);
435 cmd
->incr
.message
.body
.initial
= memcached_htonll(initial
);
436 cmd
->incr
.message
.body
.expiration
= htonl(exptime
);
438 off_t key_offset
= sizeof(protocol_binary_request_no_extras
) + 20;
439 memcpy(cmd
->bytes
+ key_offset
, key
, keylen
);
443 * Validate the response header from the server
444 * @param rsp the response to check
445 * @param cc the expected command
446 * @param status the expected status
448 static enum test_return
do_validate_response_header(response
*rsp
, uint8_t cc
, uint16_t status
) {
449 verify(rsp
->plain
.message
.header
.response
.magic
== PROTOCOL_BINARY_RES
);
450 verify(rsp
->plain
.message
.header
.response
.opcode
== cc
);
451 verify(rsp
->plain
.message
.header
.response
.datatype
== PROTOCOL_BINARY_RAW_BYTES
);
452 verify(rsp
->plain
.message
.header
.response
.status
== status
);
453 verify(rsp
->plain
.message
.header
.response
.opaque
== 0xdeadbeef);
455 if (status
== PROTOCOL_BINARY_RESPONSE_SUCCESS
) {
457 case PROTOCOL_BINARY_CMD_ADDQ
:
458 case PROTOCOL_BINARY_CMD_APPENDQ
:
459 case PROTOCOL_BINARY_CMD_DECREMENTQ
:
460 case PROTOCOL_BINARY_CMD_DELETEQ
:
461 case PROTOCOL_BINARY_CMD_FLUSHQ
:
462 case PROTOCOL_BINARY_CMD_INCREMENTQ
:
463 case PROTOCOL_BINARY_CMD_PREPENDQ
:
464 case PROTOCOL_BINARY_CMD_QUITQ
:
465 case PROTOCOL_BINARY_CMD_REPLACEQ
:
466 case PROTOCOL_BINARY_CMD_SETQ
:
467 verify("Quiet command shouldn't return on success" == NULL
);
474 case PROTOCOL_BINARY_CMD_ADD
:
475 case PROTOCOL_BINARY_CMD_REPLACE
:
476 case PROTOCOL_BINARY_CMD_SET
:
477 case PROTOCOL_BINARY_CMD_APPEND
:
478 case PROTOCOL_BINARY_CMD_PREPEND
:
479 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
480 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
481 verify(rsp
->plain
.message
.header
.response
.bodylen
== 0);
482 verify(rsp
->plain
.message
.header
.response
.cas
);
484 case PROTOCOL_BINARY_CMD_FLUSH
:
485 case PROTOCOL_BINARY_CMD_NOOP
:
486 case PROTOCOL_BINARY_CMD_QUIT
:
487 case PROTOCOL_BINARY_CMD_DELETE
:
488 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
489 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
490 verify(rsp
->plain
.message
.header
.response
.bodylen
== 0);
491 verify(rsp
->plain
.message
.header
.response
.cas
== 0);
494 case PROTOCOL_BINARY_CMD_DECREMENT
:
495 case PROTOCOL_BINARY_CMD_INCREMENT
:
496 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
497 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
498 verify(rsp
->plain
.message
.header
.response
.bodylen
== 8);
499 verify(rsp
->plain
.message
.header
.response
.cas
);
502 case PROTOCOL_BINARY_CMD_STAT
:
503 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
504 /* key and value exists in all packets except in the terminating */
505 verify(rsp
->plain
.message
.header
.response
.cas
== 0);
508 case PROTOCOL_BINARY_CMD_VERSION
:
509 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
510 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
511 verify(rsp
->plain
.message
.header
.response
.bodylen
);
512 verify(rsp
->plain
.message
.header
.response
.cas
== 0);
515 case PROTOCOL_BINARY_CMD_GET
:
516 case PROTOCOL_BINARY_CMD_GETQ
:
517 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
518 verify(rsp
->plain
.message
.header
.response
.extlen
== 4);
519 verify(rsp
->plain
.message
.header
.response
.cas
);
522 case PROTOCOL_BINARY_CMD_GETK
:
523 case PROTOCOL_BINARY_CMD_GETKQ
:
524 verify(rsp
->plain
.message
.header
.response
.keylen
);
525 verify(rsp
->plain
.message
.header
.response
.extlen
== 4);
526 verify(rsp
->plain
.message
.header
.response
.cas
);
530 /* Undefined command code */
534 verify(rsp
->plain
.message
.header
.response
.cas
== 0);
535 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
536 if (cc
!= PROTOCOL_BINARY_CMD_GETK
) {
537 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
544 /* We call verify(validate_response_header), but that macro
545 * expects a boolean expression, and the function returns
546 * an enum.... Let's just create a macro to avoid cluttering
547 * the code with all of the == TEST_PASS ;-)
549 #define validate_response_header(a, b, c) do_validate_response_header(a, b, c) == TEST_PASS
551 static enum test_return
send_binary_noop(void) {
553 raw_command(&cmd
, PROTOCOL_BINARY_CMD_NOOP
, NULL
, 0, NULL
, 0);
554 execute(send_packet(&cmd
));
558 static enum test_return
receive_binary_noop(void) {
560 execute(recv_packet(&rsp
));
562 validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_NOOP
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
566 static enum test_return
test_binary_noop(void) {
567 execute(send_binary_noop());
568 execute(receive_binary_noop());
572 static enum test_return
test_binary_quit_impl(uint8_t cc
) {
575 raw_command(&cmd
, cc
, NULL
, 0, NULL
, 0);
577 execute(send_packet(&cmd
));
578 if (cc
== PROTOCOL_BINARY_CMD_QUIT
) {
579 execute(recv_packet(&rsp
));
581 validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_QUIT
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
584 /* Socket should be closed now, read should return EXIT_SUCCESS */
585 verify(timeout_io_op(sock
, POLLIN
, rsp
.bytes
, sizeof(rsp
.bytes
)) == 0);
587 return TEST_PASS_RECONNECT
;
590 static enum test_return
test_binary_quit(void) {
591 return test_binary_quit_impl(PROTOCOL_BINARY_CMD_QUIT
);
594 static enum test_return
test_binary_quitq(void) {
595 return test_binary_quit_impl(PROTOCOL_BINARY_CMD_QUITQ
);
598 static enum test_return
test_binary_set_impl(const char *key
, uint8_t cc
) {
602 uint64_t value
= 0xdeadbeefdeadcafeULL
;
603 storage_command(&cmd
, cc
, key
, strlen(key
), &value
, sizeof(value
), 0, 0);
605 /* set should always work */
606 for (int ii
= 0; ii
< 10; ii
++) {
608 execute(send_packet(&cmd
));
610 execute(resend_packet(&cmd
));
613 if (cc
== PROTOCOL_BINARY_CMD_SET
) {
614 execute(recv_packet(&rsp
));
615 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
617 execute(test_binary_noop());
621 * We need to get the current CAS id, and at this time we haven't
622 * verified that we have a working get
624 if (cc
== PROTOCOL_BINARY_CMD_SETQ
) {
625 cmd
.set
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_SET
;
626 execute(resend_packet(&cmd
));
627 execute(recv_packet(&rsp
));
629 validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_SET
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
630 cmd
.set
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_SETQ
;
633 /* try to set with the correct CAS value */
634 cmd
.plain
.message
.header
.request
.cas
= memcached_htonll(rsp
.plain
.message
.header
.response
.cas
);
635 execute(resend_packet(&cmd
));
636 if (cc
== PROTOCOL_BINARY_CMD_SET
) {
637 execute(recv_packet(&rsp
));
638 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
640 execute(test_binary_noop());
642 /* try to set with an incorrect CAS value */
643 cmd
.plain
.message
.header
.request
.cas
=
644 memcached_htonll(rsp
.plain
.message
.header
.response
.cas
- 1);
645 execute(resend_packet(&cmd
));
646 execute(send_binary_noop());
647 execute(recv_packet(&rsp
));
648 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS
));
649 execute(receive_binary_noop());
654 static enum test_return
test_binary_set(void) {
655 return test_binary_set_impl("test_binary_set", PROTOCOL_BINARY_CMD_SET
);
658 static enum test_return
test_binary_setq(void) {
659 return test_binary_set_impl("test_binary_setq", PROTOCOL_BINARY_CMD_SETQ
);
662 static enum test_return
test_binary_add_impl(const char *key
, uint8_t cc
) {
665 uint64_t value
= 0xdeadbeefdeadcafeULL
;
666 storage_command(&cmd
, cc
, key
, strlen(key
), &value
, sizeof(value
), 0, 0);
668 /* first add should work, rest of them should fail (even with cas
670 for (int ii
= 0; ii
< 10; ii
++) {
672 execute(send_packet(&cmd
));
674 execute(resend_packet(&cmd
));
676 if (cc
== PROTOCOL_BINARY_CMD_ADD
|| ii
> 0) {
677 uint16_t expected_result
;
679 expected_result
= PROTOCOL_BINARY_RESPONSE_SUCCESS
;
681 expected_result
= PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS
;
683 execute(send_binary_noop());
684 execute(recv_packet(&rsp
));
685 execute(receive_binary_noop());
686 verify(validate_response_header(&rsp
, cc
, expected_result
));
688 execute(test_binary_noop());
694 static enum test_return
test_binary_add(void) {
695 return test_binary_add_impl("test_binary_add", PROTOCOL_BINARY_CMD_ADD
);
698 static enum test_return
test_binary_addq(void) {
699 return test_binary_add_impl("test_binary_addq", PROTOCOL_BINARY_CMD_ADDQ
);
702 static enum test_return
binary_set_item(const char *key
, const char *value
) {
705 storage_command(&cmd
, PROTOCOL_BINARY_CMD_SET
, key
, strlen(key
), value
, strlen(value
), 0, 0);
706 execute(send_packet(&cmd
));
707 execute(recv_packet(&rsp
));
708 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_SET
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
712 static enum test_return
test_binary_replace_impl(const char *key
, uint8_t cc
) {
715 uint64_t value
= 0xdeadbeefdeadcafeULL
;
716 storage_command(&cmd
, cc
, key
, strlen(key
), &value
, sizeof(value
), 0, 0);
718 /* first replace should fail, successive should succeed (when the
720 for (int ii
= 0; ii
< 10; ii
++) {
722 execute(send_packet(&cmd
));
724 execute(resend_packet(&cmd
));
727 if (cc
== PROTOCOL_BINARY_CMD_REPLACE
|| ii
== 0) {
728 uint16_t expected_result
;
730 expected_result
= PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
;
732 expected_result
= PROTOCOL_BINARY_RESPONSE_SUCCESS
;
735 execute(send_binary_noop());
736 execute(recv_packet(&rsp
));
737 execute(receive_binary_noop());
738 verify(validate_response_header(&rsp
, cc
, expected_result
));
741 execute(binary_set_item(key
, key
));
743 execute(test_binary_noop());
747 /* verify that replace with CAS value works! */
748 cmd
.plain
.message
.header
.request
.cas
= memcached_htonll(rsp
.plain
.message
.header
.response
.cas
);
749 execute(resend_packet(&cmd
));
751 if (cc
== PROTOCOL_BINARY_CMD_REPLACE
) {
752 execute(recv_packet(&rsp
));
753 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
755 execute(test_binary_noop());
757 /* try to set with an incorrect CAS value */
758 cmd
.plain
.message
.header
.request
.cas
=
759 memcached_htonll(rsp
.plain
.message
.header
.response
.cas
- 1);
760 execute(resend_packet(&cmd
));
761 execute(send_binary_noop());
762 execute(recv_packet(&rsp
));
763 execute(receive_binary_noop());
764 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS
));
769 static enum test_return
test_binary_replace(void) {
770 return test_binary_replace_impl("test_binary_replace", PROTOCOL_BINARY_CMD_REPLACE
);
773 static enum test_return
test_binary_replaceq(void) {
774 return test_binary_replace_impl("test_binary_replaceq", PROTOCOL_BINARY_CMD_REPLACEQ
);
777 static enum test_return
test_binary_delete_impl(const char *key
, uint8_t cc
) {
780 raw_command(&cmd
, cc
, key
, strlen(key
), NULL
, 0);
782 /* The delete shouldn't work the first time, because the item isn't there */
783 execute(send_packet(&cmd
));
784 execute(send_binary_noop());
785 execute(recv_packet(&rsp
));
786 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
));
787 execute(receive_binary_noop());
788 execute(binary_set_item(key
, key
));
790 /* The item should be present now, resend*/
791 execute(resend_packet(&cmd
));
792 if (cc
== PROTOCOL_BINARY_CMD_DELETE
) {
793 execute(recv_packet(&rsp
));
794 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
797 execute(test_binary_noop());
802 static enum test_return
test_binary_delete(void) {
803 return test_binary_delete_impl("test_binary_delete", PROTOCOL_BINARY_CMD_DELETE
);
806 static enum test_return
test_binary_deleteq(void) {
807 return test_binary_delete_impl("test_binary_deleteq", PROTOCOL_BINARY_CMD_DELETEQ
);
810 static enum test_return
test_binary_get_impl(const char *key
, uint8_t cc
) {
814 raw_command(&cmd
, cc
, key
, strlen(key
), NULL
, 0);
815 execute(send_packet(&cmd
));
816 execute(send_binary_noop());
818 if (cc
== PROTOCOL_BINARY_CMD_GET
|| cc
== PROTOCOL_BINARY_CMD_GETK
) {
819 execute(recv_packet(&rsp
));
820 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
));
823 execute(receive_binary_noop());
825 execute(binary_set_item(key
, key
));
826 execute(resend_packet(&cmd
));
827 execute(send_binary_noop());
829 execute(recv_packet(&rsp
));
830 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
831 execute(receive_binary_noop());
836 static enum test_return
test_binary_get(void) {
837 return test_binary_get_impl("test_binary_get", PROTOCOL_BINARY_CMD_GET
);
840 static enum test_return
test_binary_getk(void) {
841 return test_binary_get_impl("test_binary_getk", PROTOCOL_BINARY_CMD_GETK
);
844 static enum test_return
test_binary_getq(void) {
845 return test_binary_get_impl("test_binary_getq", PROTOCOL_BINARY_CMD_GETQ
);
848 static enum test_return
test_binary_getkq(void) {
849 return test_binary_get_impl("test_binary_getkq", PROTOCOL_BINARY_CMD_GETKQ
);
852 static enum test_return
test_binary_incr_impl(const char *key
, uint8_t cc
) {
855 arithmetic_command(&cmd
, cc
, key
, strlen(key
), 1, 0, 0);
858 for (ii
= 0; ii
< 10; ++ii
) {
860 execute(send_packet(&cmd
));
862 execute(resend_packet(&cmd
));
864 if (cc
== PROTOCOL_BINARY_CMD_INCREMENT
) {
865 execute(recv_packet(&rsp
));
866 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
867 verify(memcached_ntohll(rsp
.incr
.message
.body
.value
) == ii
);
869 execute(test_binary_noop());
872 /* @todo add incorrect CAS */
876 static enum test_return
test_binary_incr(void) {
877 return test_binary_incr_impl("test_binary_incr", PROTOCOL_BINARY_CMD_INCREMENT
);
880 static enum test_return
test_binary_incrq(void) {
881 return test_binary_incr_impl("test_binary_incrq", PROTOCOL_BINARY_CMD_INCREMENTQ
);
884 static enum test_return
test_binary_decr_impl(const char *key
, uint8_t cc
) {
887 arithmetic_command(&cmd
, cc
, key
, strlen(key
), 1, 9, 0);
890 for (ii
= 9; ii
> -1; --ii
) {
892 execute(send_packet(&cmd
));
894 execute(resend_packet(&cmd
));
896 if (cc
== PROTOCOL_BINARY_CMD_DECREMENT
) {
897 execute(recv_packet(&rsp
));
898 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
899 verify(memcached_ntohll(rsp
.decr
.message
.body
.value
) == (uint64_t) ii
);
901 execute(test_binary_noop());
904 /* decr 0 should not wrap */
905 execute(resend_packet(&cmd
));
906 if (cc
== PROTOCOL_BINARY_CMD_DECREMENT
) {
907 execute(recv_packet(&rsp
));
908 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
909 verify(memcached_ntohll(rsp
.decr
.message
.body
.value
) == 0);
911 /* @todo get the value and verify! */
914 /* @todo add incorrect cas */
915 execute(test_binary_noop());
919 static enum test_return
test_binary_decr(void) {
920 return test_binary_decr_impl("test_binary_decr", PROTOCOL_BINARY_CMD_DECREMENT
);
923 static enum test_return
test_binary_decrq(void) {
924 return test_binary_decr_impl("test_binary_decrq", PROTOCOL_BINARY_CMD_DECREMENTQ
);
927 static enum test_return
test_binary_version(void) {
930 raw_command(&cmd
, PROTOCOL_BINARY_CMD_VERSION
, NULL
, 0, NULL
, 0);
932 execute(send_packet(&cmd
));
933 execute(recv_packet(&rsp
));
934 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_VERSION
,
935 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
940 static enum test_return
test_binary_flush_impl(const char *key
, uint8_t cc
) {
944 for (int ii
= 0; ii
< 2; ++ii
) {
945 execute(binary_set_item(key
, key
));
946 flush_command(&cmd
, cc
, 0, ii
== 0);
947 execute(send_packet(&cmd
));
949 if (cc
== PROTOCOL_BINARY_CMD_FLUSH
) {
950 execute(recv_packet(&rsp
));
951 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
953 execute(test_binary_noop());
955 raw_command(&cmd
, PROTOCOL_BINARY_CMD_GET
, key
, strlen(key
), NULL
, 0);
956 execute(send_packet(&cmd
));
957 execute(recv_packet(&rsp
));
958 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_GET
,
959 PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
));
965 static enum test_return
test_binary_flush(void) {
966 return test_binary_flush_impl("test_binary_flush", PROTOCOL_BINARY_CMD_FLUSH
);
969 static enum test_return
test_binary_flushq(void) {
970 return test_binary_flush_impl("test_binary_flushq", PROTOCOL_BINARY_CMD_FLUSHQ
);
973 static enum test_return
test_binary_concat_impl(const char *key
, uint8_t cc
) {
978 if (cc
== PROTOCOL_BINARY_CMD_APPEND
|| cc
== PROTOCOL_BINARY_CMD_APPENDQ
) {
984 execute(binary_set_item(key
, value
));
986 if (cc
== PROTOCOL_BINARY_CMD_APPEND
|| cc
== PROTOCOL_BINARY_CMD_APPENDQ
) {
992 raw_command(&cmd
, cc
, key
, strlen(key
), value
, strlen(value
));
993 execute(send_packet(&cmd
));
994 if (cc
== PROTOCOL_BINARY_CMD_APPEND
|| cc
== PROTOCOL_BINARY_CMD_PREPEND
) {
995 execute(recv_packet(&rsp
));
996 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
998 execute(test_binary_noop());
1001 raw_command(&cmd
, PROTOCOL_BINARY_CMD_GET
, key
, strlen(key
), NULL
, 0);
1002 execute(send_packet(&cmd
));
1003 execute(recv_packet(&rsp
));
1004 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_GET
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1005 verify(rsp
.plain
.message
.header
.response
.bodylen
- 4 == 11);
1006 verify(memcmp(rsp
.bytes
+ 28, "hello world", 11) == 0);
1011 static enum test_return
test_binary_append(void) {
1012 return test_binary_concat_impl("test_binary_append", PROTOCOL_BINARY_CMD_APPEND
);
1015 static enum test_return
test_binary_prepend(void) {
1016 return test_binary_concat_impl("test_binary_prepend", PROTOCOL_BINARY_CMD_PREPEND
);
1019 static enum test_return
test_binary_appendq(void) {
1020 return test_binary_concat_impl("test_binary_appendq", PROTOCOL_BINARY_CMD_APPENDQ
);
1023 static enum test_return
test_binary_prependq(void) {
1024 return test_binary_concat_impl("test_binary_prependq", PROTOCOL_BINARY_CMD_PREPENDQ
);
1027 static enum test_return
test_binary_stat(void) {
1031 raw_command(&cmd
, PROTOCOL_BINARY_CMD_STAT
, NULL
, 0, NULL
, 0);
1032 execute(send_packet(&cmd
));
1035 execute(recv_packet(&rsp
));
1037 validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_STAT
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1038 } while (rsp
.plain
.message
.header
.response
.keylen
);
1043 static enum test_return
send_string(const char *cmd
) {
1044 execute(retry_write(cmd
, strlen(cmd
)));
1048 static enum test_return
receive_line(char *buffer
, size_t size
) {
1050 while (offset
< size
) {
1051 execute(retry_read(buffer
+ offset
, 1));
1052 if (buffer
[offset
] == '\n') {
1053 if (offset
+ 1 < size
) {
1054 buffer
[offset
+ 1] = '\0';
1065 static enum test_return
receive_response(const char *msg
) {
1067 execute(receive_line(buffer
, sizeof(buffer
)));
1068 if (strcmp(msg
, buffer
)) {
1069 fprintf(stderr
, "[%s]\n", buffer
);
1071 verify(strcmp(msg
, buffer
) == 0);
1075 static enum test_return
receive_error_response(void) {
1077 execute(receive_line(buffer
, sizeof(buffer
)));
1078 verify(strncmp(buffer
, "ERROR", 5) == 0 || strncmp(buffer
, "CLIENT_ERROR", 12) == 0
1079 || strncmp(buffer
, "SERVER_ERROR", 12) == 0);
1083 static enum test_return
test_ascii_quit(void) {
1084 if (!v16x_or_greater
) {
1085 /* Verify that quit handles unknown options */
1086 execute(send_string("quit foo bar\r\n"));
1087 execute(receive_error_response());
1089 /* quit doesn't support noreply */
1090 execute(send_string("quit noreply\r\n"));
1091 execute(receive_error_response());
1094 /* Verify that quit works */
1095 execute(send_string("quit\r\n"));
1097 /* Socket should be closed now, read should return EXIT_SUCCESS */
1099 verify(timeout_io_op(sock
, POLLIN
, buffer
, sizeof(buffer
)) == 0);
1100 return TEST_PASS_RECONNECT
;
1103 static enum test_return
test_ascii_version(void) {
1104 /* Verify that version works */
1105 execute(send_string("version\r\n"));
1107 execute(receive_line(buffer
, sizeof(buffer
)));
1108 verify(strncmp(buffer
, "VERSION ", 8) == 0);
1110 char *version
= &buffer
[sizeof("VERSION")];
1111 if (version
[0] > '1' || (version
[0] == '1' && version
[2] >= '6')) {
1112 v16x_or_greater
= true;
1115 /* Verify that version command handles unknown options */
1116 execute(send_string("version foo bar\r\n"));
1117 if (v16x_or_greater
) {
1118 execute(receive_line(buffer
, sizeof(buffer
)));
1119 verify(strncmp(buffer
, "VERSION ", 8) == 0);
1121 execute(receive_error_response());
1123 /* version doesn't support noreply */
1124 execute(send_string("version noreply\r\n"));
1125 if (v16x_or_greater
) {
1126 execute(receive_line(buffer
, sizeof(buffer
)));
1127 verify(strncmp(buffer
, "VERSION ", 8) == 0);
1129 execute(receive_error_response());
1134 static enum test_return
test_ascii_verbosity(void) {
1135 /* This command does not adhere to the spec! */
1136 execute(send_string("verbosity foo bar my\r\n"));
1137 execute(receive_error_response());
1139 execute(send_string("verbosity noreply\r\n"));
1140 execute(test_ascii_version());
1142 execute(send_string("verbosity 0 noreply\r\n"));
1143 execute(test_ascii_version());
1145 execute(send_string("verbosity\r\n"));
1146 execute(receive_error_response());
1148 execute(send_string("verbosity 1\r\n"));
1149 execute(receive_response("OK\r\n"));
1151 execute(send_string("verbosity 0\r\n"));
1152 execute(receive_response("OK\r\n"));
1157 static enum test_return
test_ascii_set_impl(const char *key
, bool noreply
) {
1158 /* @todo add tests for bogus format! */
1160 snprintf(buffer
, sizeof(buffer
), "set %s 0 0 5%s\r\nvalue\r\n", key
, noreply
? " noreply" : "");
1161 execute(send_string(buffer
));
1164 execute(receive_response("STORED\r\n"));
1167 return test_ascii_version();
1170 static enum test_return
test_ascii_set(void) {
1171 return test_ascii_set_impl("test_ascii_set", false);
1174 static enum test_return
test_ascii_set_noreply(void) {
1175 return test_ascii_set_impl("test_ascii_set_noreply", true);
1178 static enum test_return
test_ascii_add_impl(const char *key
, bool noreply
) {
1179 /* @todo add tests for bogus format! */
1181 snprintf(buffer
, sizeof(buffer
), "add %s 0 0 5%s\r\nvalue\r\n", key
, noreply
? " noreply" : "");
1182 execute(send_string(buffer
));
1185 execute(receive_response("STORED\r\n"));
1188 execute(send_string(buffer
));
1191 execute(receive_response("NOT_STORED\r\n"));
1194 return test_ascii_version();
1197 static enum test_return
test_ascii_add(void) {
1198 return test_ascii_add_impl("test_ascii_add", false);
1201 static enum test_return
test_ascii_add_noreply(void) {
1202 return test_ascii_add_impl("test_ascii_add_noreply", true);
1205 static enum test_return
ascii_get_unknown_value(char **key
, char **value
, ssize_t
*ndata
) {
1208 execute(receive_line(buffer
, sizeof(buffer
)));
1209 verify(strncmp(buffer
, "VALUE ", 6) == 0);
1210 char *end
= strchr(buffer
+ 6, ' ');
1215 *key
= strdup(buffer
+ 6);
1217 char *ptr
= end
+ 1;
1220 unsigned long val
= strtoul(ptr
, &end
, 10); /* flags */
1226 *ndata
= (ssize_t
) strtoul(end
, &end
, 10); /* size */
1230 while (end
and *end
!= '\n' and isspace(*end
)) ++end
;
1231 verify(end
and *end
== '\n');
1233 *value
= static_cast<char *>(malloc((size_t) *ndata
));
1236 execute(retry_read(*value
, (size_t) *ndata
));
1238 execute(retry_read(buffer
, 2));
1239 verify(memcmp(buffer
, "\r\n", 2) == 0);
1244 static enum test_return
ascii_get_value(const char *key
, const char *value
) {
1246 size_t datasize
= strlen(value
);
1248 verify(datasize
< sizeof(buffer
));
1249 execute(receive_line(buffer
, sizeof(buffer
)));
1250 verify(strncmp(buffer
, "VALUE ", 6) == 0);
1251 verify(strncmp(buffer
+ 6, key
, strlen(key
)) == 0);
1252 char *ptr
= buffer
+ 6 + strlen(key
) + 1;
1256 unsigned long val
= strtoul(ptr
, &end
, 10); /* flags */
1263 val
= strtoul(end
, &end
, 10); /* size */
1266 verify(val
== datasize
);
1268 while (end
and *end
!= '\n' and isspace(*end
)) {
1271 verify(end
and *end
== '\n');
1273 execute(retry_read(buffer
, datasize
));
1274 verify(memcmp(buffer
, value
, datasize
) == 0);
1276 execute(retry_read(buffer
, 2));
1277 verify(memcmp(buffer
, "\r\n", 2) == 0);
1282 static enum test_return
ascii_get_item(const char *key
, const char *value
, bool exist
) {
1284 size_t datasize
= 0;
1286 datasize
= strlen(value
);
1289 verify(datasize
< sizeof(buffer
));
1290 snprintf(buffer
, sizeof(buffer
), "get %s\r\n", key
);
1291 execute(send_string(buffer
));
1294 execute(ascii_get_value(key
, value
));
1297 execute(retry_read(buffer
, 5));
1298 verify(memcmp(buffer
, "END\r\n", 5) == 0);
1303 static enum test_return
ascii_gets_value(const char *key
, const char *value
, unsigned long *cas
) {
1305 size_t datasize
= strlen(value
);
1307 verify(datasize
< sizeof(buffer
));
1308 execute(receive_line(buffer
, sizeof(buffer
)));
1309 verify(strncmp(buffer
, "VALUE ", 6) == 0);
1310 verify(strncmp(buffer
+ 6, key
, strlen(key
)) == 0);
1311 char *ptr
= buffer
+ 6 + strlen(key
) + 1;
1315 unsigned long val
= strtoul(ptr
, &end
, 10); /* flags */
1322 val
= strtoul(end
, &end
, 10); /* size */
1325 verify(val
== datasize
);
1329 *cas
= strtoul(end
, &end
, 10); /* cas */
1332 verify(val
== datasize
);
1335 while (end
and *end
!= '\n' and isspace(*end
)) {
1338 verify(end
and *end
== '\n');
1340 execute(retry_read(buffer
, datasize
));
1341 verify(memcmp(buffer
, value
, datasize
) == 0);
1343 execute(retry_read(buffer
, 2));
1344 verify(memcmp(buffer
, "\r\n", 2) == 0);
1349 static enum test_return
ascii_gets_item(const char *key
, const char *value
, bool exist
,
1350 unsigned long *cas
) {
1352 size_t datasize
= 0;
1354 datasize
= strlen(value
);
1357 verify(datasize
< sizeof(buffer
));
1358 snprintf(buffer
, sizeof(buffer
), "gets %s\r\n", key
);
1359 execute(send_string(buffer
));
1362 execute(ascii_gets_value(key
, value
, cas
));
1364 execute(retry_read(buffer
, 5));
1365 verify(memcmp(buffer
, "END\r\n", 5) == 0);
1370 static enum test_return
ascii_set_item(const char *key
, const char *value
) {
1372 size_t len
= strlen(value
);
1373 snprintf(buffer
, sizeof(buffer
), "set %s 0 0 %u\r\n", key
, (unsigned int) len
);
1374 execute(send_string(buffer
));
1375 execute(retry_write(value
, len
));
1376 execute(send_string("\r\n"));
1377 execute(receive_response("STORED\r\n"));
1381 static enum test_return
test_ascii_replace_impl(const char *key
, bool noreply
) {
1383 snprintf(buffer
, sizeof(buffer
), "replace %s 0 0 5%s\r\nvalue\r\n", key
,
1384 noreply
? " noreply" : "");
1385 execute(send_string(buffer
));
1388 execute(test_ascii_version());
1390 execute(receive_response("NOT_STORED\r\n"));
1393 execute(ascii_set_item(key
, "value"));
1394 execute(ascii_get_item(key
, "value", true));
1396 execute(send_string(buffer
));
1399 execute(test_ascii_version());
1401 execute(receive_response("STORED\r\n"));
1403 return test_ascii_version();
1406 static enum test_return
test_ascii_replace(void) {
1407 return test_ascii_replace_impl("test_ascii_replace", false);
1410 static enum test_return
test_ascii_replace_noreply(void) {
1411 return test_ascii_replace_impl("test_ascii_replace_noreply", true);
1414 static enum test_return
test_ascii_cas_impl(const char *key
, bool noreply
) {
1418 execute(ascii_set_item(key
, "value"));
1419 execute(ascii_gets_item(key
, "value", true, &cas
));
1421 snprintf(buffer
, sizeof(buffer
), "cas %s 0 0 6 %lu%s\r\nvalue2\r\n", key
, cas
,
1422 noreply
? " noreply" : "");
1423 execute(send_string(buffer
));
1426 execute(test_ascii_version());
1428 execute(receive_response("STORED\r\n"));
1431 /* reexecute the same command should fail due to illegal cas */
1432 execute(send_string(buffer
));
1435 execute(test_ascii_version());
1437 execute(receive_response("EXISTS\r\n"));
1440 return test_ascii_version();
1443 static enum test_return
test_ascii_cas(void) {
1444 return test_ascii_cas_impl("test_ascii_cas", false);
1447 static enum test_return
test_ascii_cas_noreply(void) {
1448 return test_ascii_cas_impl("test_ascii_cas_noreply", true);
1451 static enum test_return
test_ascii_delete_impl(const char *key
, bool noreply
) {
1452 execute(ascii_set_item(key
, "value"));
1454 execute(send_string("delete\r\n"));
1455 execute(receive_error_response());
1456 /* BUG: the server accepts delete a b */
1457 execute(send_string("delete a b c d e\r\n"));
1458 execute(receive_error_response());
1461 snprintf(buffer
, sizeof(buffer
), "delete %s%s\r\n", key
, noreply
? " noreply" : "");
1462 execute(send_string(buffer
));
1465 execute(test_ascii_version());
1467 execute(receive_response("DELETED\r\n"));
1469 execute(ascii_get_item(key
, "value", false));
1470 execute(send_string(buffer
));
1472 execute(test_ascii_version());
1474 execute(receive_response("NOT_FOUND\r\n"));
1479 static enum test_return
test_ascii_delete(void) {
1480 return test_ascii_delete_impl("test_ascii_delete", false);
1483 static enum test_return
test_ascii_delete_noreply(void) {
1484 return test_ascii_delete_impl("test_ascii_delete_noreply", true);
1487 static enum test_return
test_ascii_get(void) {
1488 execute(ascii_set_item("test_ascii_get", "value"));
1490 execute(send_string("get\r\n"));
1491 execute(receive_error_response());
1492 execute(ascii_get_item("test_ascii_get", "value", true));
1493 execute(ascii_get_item("test_ascii_get_notfound", "value", false));
1498 static enum test_return
test_ascii_gets(void) {
1499 execute(ascii_set_item("test_ascii_gets", "value"));
1501 execute(send_string("gets\r\n"));
1502 execute(receive_error_response());
1504 execute(ascii_gets_item("test_ascii_gets", "value", true, &cas
));
1505 execute(ascii_gets_item("test_ascii_gets_notfound", "value", false, &cas
));
1510 static enum test_return
test_ascii_mget(void) {
1511 const uint32_t nkeys
= 5;
1512 const char *const keys
[] = {"test_ascii_mget1", "test_ascii_mget2",
1513 /* test_ascii_mget_3 does not exist :) */
1514 "test_ascii_mget4", "test_ascii_mget5", "test_ascii_mget6"};
1516 for (uint32_t x
= 0; x
< nkeys
; ++x
) {
1517 execute(ascii_set_item(keys
[x
], "value"));
1520 /* Ask for a key that doesn't exist as well */
1521 execute(send_string("get test_ascii_mget1 test_ascii_mget2 test_ascii_mget3 "
1522 "test_ascii_mget4 test_ascii_mget5 "
1523 "test_ascii_mget6\r\n"));
1525 std::vector
<char *> returned
;
1526 returned
.resize(nkeys
);
1528 for (uint32_t x
= 0; x
< nkeys
; ++x
) {
1531 execute(ascii_get_unknown_value(&returned
[x
], &v
, &nbytes
));
1532 verify(nbytes
== 5);
1533 verify(memcmp(v
, "value", 5) == 0);
1538 execute(retry_read(buffer
, 5));
1539 verify(memcmp(buffer
, "END\r\n", 5) == 0);
1541 /* verify that we got all the keys we expected */
1542 for (uint32_t x
= 0; x
< nkeys
; ++x
) {
1544 for (uint32_t y
= 0; y
< nkeys
; ++y
) {
1545 if (strcmp(keys
[x
], returned
[y
]) == 0) {
1553 for (uint32_t x
= 0; x
< nkeys
; ++x
) {
1560 static enum test_return
test_ascii_incr_impl(const char *key
, bool noreply
) {
1562 snprintf(cmd
, sizeof(cmd
), "incr %s 1%s\r\n", key
, noreply
? " noreply" : "");
1564 execute(ascii_set_item(key
, "0"));
1565 for (int x
= 1; x
< 11; ++x
) {
1566 execute(send_string(cmd
));
1569 execute(test_ascii_version());
1572 execute(receive_line(buffer
, sizeof(buffer
)));
1573 int val
= atoi(buffer
);
1578 execute(ascii_get_item(key
, "10", true));
1583 static enum test_return
test_ascii_incr(void) {
1584 return test_ascii_incr_impl("test_ascii_incr", false);
1587 static enum test_return
test_ascii_incr_noreply(void) {
1588 return test_ascii_incr_impl("test_ascii_incr_noreply", true);
1591 static enum test_return
test_ascii_decr_impl(const char *key
, bool noreply
) {
1593 snprintf(cmd
, sizeof(cmd
), "decr %s 1%s\r\n", key
, noreply
? " noreply" : "");
1595 execute(ascii_set_item(key
, "9"));
1596 for (int x
= 8; x
> -1; --x
) {
1597 execute(send_string(cmd
));
1600 execute(test_ascii_version());
1603 execute(receive_line(buffer
, sizeof(buffer
)));
1604 int val
= atoi(buffer
);
1609 execute(ascii_get_item(key
, "0", true));
1611 /* verify that it doesn't wrap */
1612 execute(send_string(cmd
));
1614 execute(test_ascii_version());
1617 execute(receive_line(buffer
, sizeof(buffer
)));
1619 execute(ascii_get_item(key
, "0", true));
1624 static enum test_return
test_ascii_decr(void) {
1625 return test_ascii_decr_impl("test_ascii_decr", false);
1628 static enum test_return
test_ascii_decr_noreply(void) {
1629 return test_ascii_decr_impl("test_ascii_decr_noreply", true);
1632 static enum test_return
test_ascii_flush_impl(const char *key
, bool noreply
) {
1634 /* Verify that the flush_all command handles unknown options */
1635 /* Bug in the current memcached server! */
1636 execute(send_string("flush_all foo bar\r\n"));
1637 execute(receive_error_response());
1640 execute(ascii_set_item(key
, key
));
1641 execute(ascii_get_item(key
, key
, true));
1644 execute(send_string("flush_all noreply\r\n"));
1645 execute(test_ascii_version());
1647 execute(send_string("flush_all\r\n"));
1648 execute(receive_response("OK\r\n"));
1651 execute(ascii_get_item(key
, key
, false));
1656 static enum test_return
test_ascii_flush(void) {
1657 return test_ascii_flush_impl("test_ascii_flush", false);
1660 static enum test_return
test_ascii_flush_noreply(void) {
1661 return test_ascii_flush_impl("test_ascii_flush_noreply", true);
1664 static enum test_return
test_ascii_concat_impl(const char *key
, bool append
, bool noreply
) {
1672 execute(ascii_set_item(key
, value
));
1681 snprintf(cmd
, sizeof(cmd
), "%s %s 0 0 %u%s\r\n%s\r\n", append
? "append" : "prepend", key
,
1682 (unsigned int) strlen(value
), noreply
? " noreply" : "", value
);
1683 execute(send_string(cmd
));
1686 execute(test_ascii_version());
1688 execute(receive_response("STORED\r\n"));
1691 execute(ascii_get_item(key
, "hello world", true));
1693 snprintf(cmd
, sizeof(cmd
), "%s %s_notfound 0 0 %u%s\r\n%s\r\n", append
? "append" : "prepend",
1694 key
, (unsigned int) strlen(value
), noreply
? " noreply" : "", value
);
1695 execute(send_string(cmd
));
1698 execute(test_ascii_version());
1700 execute(receive_response("NOT_STORED\r\n"));
1706 static enum test_return
test_ascii_append(void) {
1707 return test_ascii_concat_impl("test_ascii_append", true, false);
1710 static enum test_return
test_ascii_prepend(void) {
1711 return test_ascii_concat_impl("test_ascii_prepend", false, false);
1714 static enum test_return
test_ascii_append_noreply(void) {
1715 return test_ascii_concat_impl("test_ascii_append_noreply", true, true);
1718 static enum test_return
test_ascii_prepend_noreply(void) {
1719 return test_ascii_concat_impl("test_ascii_prepend_noreply", false, true);
1722 static enum test_return
test_ascii_stat(void) {
1723 execute(send_string("stats noreply\r\n"));
1724 execute(receive_error_response());
1725 execute(send_string("stats\r\n"));
1728 execute(receive_line(buffer
, sizeof(buffer
)));
1729 } while (strcmp(buffer
, "END\r\n"));
1731 return TEST_PASS_RECONNECT
;
1734 typedef enum test_return (*TEST_FUNC
)(void);
1737 const char *description
;
1741 struct testcase testcases
[] = {{"ascii version", test_ascii_version
},
1742 {"ascii quit", test_ascii_quit
},
1743 {"ascii verbosity", test_ascii_verbosity
},
1744 {"ascii set", test_ascii_set
},
1745 {"ascii set noreply", test_ascii_set_noreply
},
1746 {"ascii get", test_ascii_get
},
1747 {"ascii gets", test_ascii_gets
},
1748 {"ascii mget", test_ascii_mget
},
1749 {"ascii flush", test_ascii_flush
},
1750 {"ascii flush noreply", test_ascii_flush_noreply
},
1751 {"ascii add", test_ascii_add
},
1752 {"ascii add noreply", test_ascii_add_noreply
},
1753 {"ascii replace", test_ascii_replace
},
1754 {"ascii replace noreply", test_ascii_replace_noreply
},
1755 {"ascii cas", test_ascii_cas
},
1756 {"ascii cas noreply", test_ascii_cas_noreply
},
1757 {"ascii delete", test_ascii_delete
},
1758 {"ascii delete noreply", test_ascii_delete_noreply
},
1759 {"ascii incr", test_ascii_incr
},
1760 {"ascii incr noreply", test_ascii_incr_noreply
},
1761 {"ascii decr", test_ascii_decr
},
1762 {"ascii decr noreply", test_ascii_decr_noreply
},
1763 {"ascii append", test_ascii_append
},
1764 {"ascii append noreply", test_ascii_append_noreply
},
1765 {"ascii prepend", test_ascii_prepend
},
1766 {"ascii prepend noreply", test_ascii_prepend_noreply
},
1767 {"ascii stat", test_ascii_stat
},
1768 {"binary noop", test_binary_noop
},
1769 {"binary quit", test_binary_quit
},
1770 {"binary quitq", test_binary_quitq
},
1771 {"binary set", test_binary_set
},
1772 {"binary setq", test_binary_setq
},
1773 {"binary flush", test_binary_flush
},
1774 {"binary flushq", test_binary_flushq
},
1775 {"binary add", test_binary_add
},
1776 {"binary addq", test_binary_addq
},
1777 {"binary replace", test_binary_replace
},
1778 {"binary replaceq", test_binary_replaceq
},
1779 {"binary delete", test_binary_delete
},
1780 {"binary deleteq", test_binary_deleteq
},
1781 {"binary get", test_binary_get
},
1782 {"binary getq", test_binary_getq
},
1783 {"binary getk", test_binary_getk
},
1784 {"binary getkq", test_binary_getkq
},
1785 {"binary incr", test_binary_incr
},
1786 {"binary incrq", test_binary_incrq
},
1787 {"binary decr", test_binary_decr
},
1788 {"binary decrq", test_binary_decrq
},
1789 {"binary version", test_binary_version
},
1790 {"binary append", test_binary_append
},
1791 {"binary appendq", test_binary_appendq
},
1792 {"binary prepend", test_binary_prepend
},
1793 {"binary prependq", test_binary_prependq
},
1794 {"binary stat", test_binary_stat
},
1797 struct test_type_st
{
1802 int main(int argc
, char **argv
) {
1803 static const char *const status_msg
[] = {"[skip]", "[pass]", "[pass]", "[FAIL]"};
1804 struct test_type_st tests
= {true, true};
1807 const char *hostname
= NULL
;
1808 const char *port
= MEMCACHED_DEFAULT_PORT_STRING
;
1810 bool prompt
= false;
1811 const char *testname
= NULL
;
1813 while ((cmd
= getopt(argc
, argv
, "qt:vch:p:PT:?ab")) != EOF
) {
1817 tests
.binary
= false;
1821 tests
.ascii
= false;
1822 tests
.binary
= true;
1826 timeout
= atoi(optarg
);
1828 fprintf(stderr
, "Invalid timeout. Please specify a number for -t\n");
1829 return EXIT_FAILURE
;
1863 "Usage: %s [-h hostname] [-p port] [-c] [-v] [-t n] [-P] [-T testname]'\n"
1864 "\t-c\tGenerate coredump if a test fails\n"
1865 "\t-v\tVerbose test output (print out the assertion)\n"
1866 "\t-t n\tSet the timeout for io-operations to n seconds\n"
1867 "\t-P\tPrompt the user before starting a test.\n"
1868 "\t\t\t\"skip\" will skip the test\n"
1869 "\t\t\t\"quit\" will terminate memcapable\n"
1870 "\t\t\tEverything else will start the test\n"
1871 "\t-T n\tJust run the test named n\n"
1872 "\t-a\tOnly test the ascii protocol\n"
1873 "\t-b\tOnly test the binary protocol\n",
1875 return EXIT_SUCCESS
;
1880 fprintf(stderr
, "No hostname was provided.\n");
1881 return EXIT_FAILURE
;
1886 if (WSAStartup(MAKEWORD(2, 2), &wsaData
)) {
1887 fprintf(stderr
, "Socket Initialization Error.\n");
1888 return EXIT_FAILURE
;
1892 sock
= connect_server(hostname
, port
);
1893 if (sock
== INVALID_SOCKET
) {
1894 fprintf(stderr
, "Failed to connect to <%s:%s>: %s\n", hostname
, port
,
1895 strerror(get_socket_errno()));
1896 return EXIT_FAILURE
;
1899 for (int ii
= 0; testcases
[ii
].description
; ++ii
) {
1900 if (testname
&& strcmp(testcases
[ii
].description
, testname
)) {
1904 if ((testcases
[ii
].description
[0] == 'a' && (tests
.ascii
) == 0)
1905 || (testcases
[ii
].description
[0] == 'b' && (tests
.binary
) == 0))
1910 fprintf(stdout
, "%-40s", testcases
[ii
].description
);
1914 fprintf(stdout
, "\nPress <return> when you are ready? ");
1915 char buffer
[80] = {0};
1916 if (fgets(buffer
, sizeof(buffer
), stdin
)) {
1917 if (strncmp(buffer
, "skip", 4) == 0) {
1918 fprintf(stdout
, "%-40s%s\n", testcases
[ii
].description
, status_msg
[TEST_SKIP
]);
1922 if (strncmp(buffer
, "quit", 4) == 0) {
1927 fprintf(stdout
, "%-40s", testcases
[ii
].description
);
1931 bool reconnect
= false;
1932 enum test_return ret
= testcases
[ii
].function();
1933 if (ret
== TEST_FAIL
) {
1937 fprintf(stderr
, "\n");
1939 } else if (ret
== TEST_PASS_RECONNECT
) {
1943 if (ret
== TEST_FAIL
) {
1944 fprintf(stderr
, "%s\n", status_msg
[ret
]);
1946 fprintf(stdout
, "%s\n", status_msg
[ret
]);
1951 if ((sock
= connect_server(hostname
, port
)) == INVALID_SOCKET
) {
1952 fprintf(stderr
, "Failed to connect to <%s:%s>: %s\n", hostname
,
1953 port
, strerror(get_socket_errno()));
1954 fprintf(stderr
, "%d of %d tests failed\n", failed
, total
);
1955 return EXIT_FAILURE
;
1962 fprintf(stdout
, "All tests passed\n");
1964 fprintf(stderr
, "%d of %d tests failed\n", failed
, total
);
1971 return (failed
== 0) ? EXIT_SUCCESS
: EXIT_FAILURE
;