2 * Copyright (C) 2011-2012 Data Differential, http://datadifferential.com/
3 * Copyright (C) 2006-2009 Brian Aker
6 * Use and distribution licensed under the BSD license. See
7 * the COPYING file in the parent directory for full text.
13 /* -*- Mode: C; tab-width: 2; c-basic-offset: 2; indent-tabs-mode: nil -*- */
16 #include <mem_config.h>
21 #include "poll/poll.h"
34 #include <sys/types.h>
37 #include <libmemcached-1.0/memcached.h>
39 #include "libmemcached/socket.hpp"
40 #include "libmemcachedprotocol-0.0/binary.h"
41 #include "libmemcached/byteorder.h"
42 #include "clients/utilities.h"
47 /* /usr/include/netinet/in.h defines macros from ntohs() to _bswap_nn to
48 * optimize the conversion functions, but the prototypes generate warnings
49 * from gcc. The conversion methods isn't the bottleneck for my app, so
50 * just remove the warnings by undef'ing the optimization ..
56 /* Should we generate coredumps when we enounter an error (-c) */
57 static bool do_core
= false;
58 /* connection to the server */
59 static memcached_socket_t sock
;
60 /* Should the output from test failures be verbose or quiet? */
61 static bool verbose
= false;
63 /* The number of seconds to wait for an IO-operation */
64 static int timeout
= 2;
67 * Instead of having to cast between the different datatypes we create
68 * a union of all of the different types of pacages we want to send.
69 * A lot of the different commands use the same packet layout, so I'll
70 * just define the different types I need. The typedefs only contain
71 * the header of the message, so we need some space for keys and body
72 * To avoid to have to do multiple writes, lets add a chunk of memory
73 * to use. 1k should be more than enough for header, key and body.
77 protocol_binary_request_no_extras plain
;
78 protocol_binary_request_flush flush
;
79 protocol_binary_request_incr incr
;
80 protocol_binary_request_set set
;
86 protocol_binary_response_no_extras plain
;
87 protocol_binary_response_incr incr
;
88 protocol_binary_response_decr decr
;
94 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
)
102 struct addrinfo
*ai
= 0;
103 struct addrinfo hints
;
104 memset(&hints
, 0, sizeof(struct addrinfo
));
105 hints
.ai_family
=AF_UNSPEC
;
106 hints
.ai_protocol
=IPPROTO_TCP
;
107 hints
.ai_socktype
=SOCK_STREAM
;
109 int error
= getaddrinfo(hostname
, port
, &hints
, &ai
);
112 if (error
!= EAI_SYSTEM
)
114 fprintf(stderr
, "getaddrinfo(): %s\n", gai_strerror(error
));
118 perror("getaddrinfo()");
126 * Set the socket in nonblocking mode
127 * @return -1 if failure, the socket otherwise
129 static memcached_socket_t
set_noblock(void)
133 if (ioctlsocket(sock
, FIONBIO
, &arg
) == SOCKET_ERROR
)
135 perror("Failed to set nonblocking io");
137 return INVALID_SOCKET
;
140 int flags
= fcntl(sock
, F_GETFL
, 0);
143 perror("Failed to get socket flags");
144 memcached_close_socket(sock
);
145 return INVALID_SOCKET
;
148 if ((flags
& O_NONBLOCK
) != O_NONBLOCK
)
150 if (fcntl(sock
, F_SETFL
, flags
| O_NONBLOCK
) == -1)
152 perror("Failed to set socket to nonblocking mode");
153 memcached_close_socket(sock
);
154 return INVALID_SOCKET
;
162 * Try to open a connection to the server
163 * @param hostname the name of the server to connect to
164 * @param port the port number (or service) to connect to
165 * @return positive integer if success, -1 otherwise
167 static memcached_socket_t
connect_server(const char *hostname
, const char *port
)
169 struct addrinfo
*ai
= lookuphost(hostname
, port
);
170 sock
= INVALID_SOCKET
;
173 if ((sock
= socket(ai
->ai_family
, ai
->ai_socktype
,
174 ai
->ai_protocol
)) != INVALID_SOCKET
)
176 if (connect(sock
, ai
->ai_addr
, ai
->ai_addrlen
) == SOCKET_ERROR
)
178 fprintf(stderr
, "Failed to connect socket: %s\n",
179 strerror(get_socket_errno()));
181 sock
= INVALID_SOCKET
;
190 fprintf(stderr
, "Failed to create socket: %s\n", strerror(get_socket_errno()));
199 static ssize_t
timeout_io_op(memcached_socket_t fd
, short direction
, void *buf
, size_t len
)
203 if (direction
== POLLOUT
)
205 ret
= send(fd
, buf
, len
, 0);
209 ret
= recv(fd
, buf
, len
, 0);
212 if (ret
== SOCKET_ERROR
&& get_socket_errno() == EWOULDBLOCK
)
215 memset(&fds
, 0, sizeof(struct pollfd
));
216 fds
.events
= direction
;
219 int err
= poll(&fds
, 1, timeout
* 1000);
222 if (direction
== POLLOUT
)
224 ret
= send(fd
, buf
, len
, 0);
228 ret
= recv(fd
, buf
, len
, 0);
237 perror("Failed to poll");
246 * Ensure that an expression is true. If it isn't print out a message similar
247 * to assert() and create a coredump if the user wants that. If not an error
248 * message is returned.
251 static enum test_return
ensure(bool val
, const char *expression
, const char *file
, int line
)
257 fprintf(stdout
, "\n%s:%d: %s", file
, line
, expression
);
271 #define verify(expression) do { if (ensure(expression, #expression, __FILE__, __LINE__) == TEST_FAIL) return TEST_FAIL; } while (0)
272 #define execute(expression) do { if (ensure(expression == TEST_PASS, #expression, __FILE__, __LINE__) == TEST_FAIL) return TEST_FAIL; } while (0)
275 * Send a chunk of memory over the socket (retry if the call is iterrupted
277 static enum test_return
retry_write(const void* buf
, size_t len
)
280 const char* ptr
= static_cast<const char *>(buf
);
284 size_t num_bytes
= len
- offset
;
285 ssize_t nw
= timeout_io_op(sock
, POLLOUT
, (void*)(ptr
+ offset
), num_bytes
);
288 verify(get_socket_errno() == EINTR
|| get_socket_errno() == EAGAIN
);
295 } while (offset
< len
);
301 * Resend a packet to the server (All fields in the command header should
302 * be in network byte order)
304 static enum test_return
resend_packet(command
*cmd
)
306 size_t length
= sizeof (protocol_binary_request_no_extras
) +
307 ntohl(cmd
->plain
.message
.header
.request
.bodylen
);
309 execute(retry_write(cmd
, length
));
314 * Send a command to the server. The command header needs to be updated
315 * to network byte order
317 static enum test_return
send_packet(command
*cmd
)
319 /* Fix the byteorder of the header */
320 cmd
->plain
.message
.header
.request
.keylen
=
321 ntohs(cmd
->plain
.message
.header
.request
.keylen
);
322 cmd
->plain
.message
.header
.request
.bodylen
=
323 ntohl(cmd
->plain
.message
.header
.request
.bodylen
);
324 cmd
->plain
.message
.header
.request
.cas
=
325 memcached_ntohll(cmd
->plain
.message
.header
.request
.cas
);
327 execute(resend_packet(cmd
));
332 * Read a fixed length chunk of data from the server
334 static enum test_return
retry_read(void *buf
, size_t len
)
339 ssize_t nr
= timeout_io_op(sock
, POLLIN
, ((char*) buf
) + offset
, len
- offset
);
342 fprintf(stderr
, "Errno: %d %s\n", get_socket_errno(), strerror(errno
));
343 verify(get_socket_errno() == EINTR
|| get_socket_errno() == EAGAIN
);
352 } while (offset
< len
);
358 * Receive a response from the server and conver the fields in the header
359 * to local byte order
361 static enum test_return
recv_packet(response
*rsp
)
363 execute(retry_read(rsp
, sizeof(protocol_binary_response_no_extras
)));
365 /* Fix the byte order in the packet header */
366 rsp
->plain
.message
.header
.response
.keylen
=
367 ntohs(rsp
->plain
.message
.header
.response
.keylen
);
368 rsp
->plain
.message
.header
.response
.status
=
369 ntohs(rsp
->plain
.message
.header
.response
.status
);
370 rsp
->plain
.message
.header
.response
.bodylen
=
371 ntohl(rsp
->plain
.message
.header
.response
.bodylen
);
372 rsp
->plain
.message
.header
.response
.cas
=
373 memcached_ntohll(rsp
->plain
.message
.header
.response
.cas
);
375 size_t bodysz
= rsp
->plain
.message
.header
.response
.bodylen
;
377 execute(retry_read(rsp
->bytes
+ sizeof (protocol_binary_response_no_extras
), bodysz
));
383 * Create a storage command (add, set, replace etc)
385 * @param cmd destination buffer
386 * @param cc the storage command to create
387 * @param key the key to store
388 * @param keylen the length of the key
389 * @param dta the data to store with the key
390 * @param dtalen the length of the data to store with the key
391 * @param flags the flags to store along with the key
392 * @param exptime the expiry time for the key
394 static void storage_command(command
*cmd
,
403 /* all of the storage commands use the same command layout */
404 protocol_binary_request_set
*request
= &cmd
->set
;
406 memset(request
, 0, sizeof (*request
));
407 request
->message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
408 request
->message
.header
.request
.opcode
= cc
;
409 request
->message
.header
.request
.keylen
= (uint16_t)keylen
;
410 request
->message
.header
.request
.extlen
= 8;
411 request
->message
.header
.request
.bodylen
= (uint32_t)(keylen
+ 8 + dtalen
);
412 request
->message
.header
.request
.opaque
= 0xdeadbeef;
413 request
->message
.body
.flags
= flags
;
414 request
->message
.body
.expiration
= exptime
;
416 off_t key_offset
= sizeof (protocol_binary_request_no_extras
) + 8;
417 memcpy(cmd
->bytes
+ key_offset
, key
, keylen
);
419 memcpy(cmd
->bytes
+ key_offset
+ keylen
, dta
, dtalen
);
423 * Create a basic command to send to the server
424 * @param cmd destination buffer
425 * @param cc the command to create
426 * @param key the key to store
427 * @param keylen the length of the key
428 * @param dta the data to store with the key
429 * @param dtalen the length of the data to store with the key
431 static void raw_command(command
*cmd
,
438 /* all of the storage commands use the same command layout */
439 memset(cmd
, 0, sizeof (*cmd
));
440 cmd
->plain
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
441 cmd
->plain
.message
.header
.request
.opcode
= cc
;
442 cmd
->plain
.message
.header
.request
.keylen
= (uint16_t)keylen
;
443 cmd
->plain
.message
.header
.request
.bodylen
= (uint32_t)(keylen
+ dtalen
);
444 cmd
->plain
.message
.header
.request
.opaque
= 0xdeadbeef;
446 off_t key_offset
= sizeof (protocol_binary_request_no_extras
);
449 memcpy(cmd
->bytes
+ key_offset
, key
, keylen
);
452 memcpy(cmd
->bytes
+ key_offset
+ keylen
, dta
, dtalen
);
456 * Create the flush command
457 * @param cmd destination buffer
458 * @param cc the command to create (FLUSH/FLUSHQ)
459 * @param exptime when to flush
460 * @param use_extra to force using of the extra field?
462 static void flush_command(command
*cmd
,
463 uint8_t cc
, uint32_t exptime
, bool use_extra
)
465 memset(cmd
, 0, sizeof (cmd
->flush
));
466 cmd
->flush
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
467 cmd
->flush
.message
.header
.request
.opcode
= cc
;
468 cmd
->flush
.message
.header
.request
.opaque
= 0xdeadbeef;
470 if (exptime
!= 0 || use_extra
)
472 cmd
->flush
.message
.header
.request
.extlen
= 4;
473 cmd
->flush
.message
.body
.expiration
= htonl(exptime
);
474 cmd
->flush
.message
.header
.request
.bodylen
= 4;
479 * Create a incr/decr command
480 * @param cc the cmd to create (FLUSH/FLUSHQ)
481 * @param key the key to operate on
482 * @param keylen the number of bytes in the key
483 * @param delta the number to add/subtract
484 * @param initial the initial value if the key doesn't exist
485 * @param exptime when the key should expire if it isn't set
487 static void arithmetic_command(command
*cmd
,
495 memset(cmd
, 0, sizeof (cmd
->incr
));
496 cmd
->incr
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
497 cmd
->incr
.message
.header
.request
.opcode
= cc
;
498 cmd
->incr
.message
.header
.request
.keylen
= (uint16_t)keylen
;
499 cmd
->incr
.message
.header
.request
.extlen
= 20;
500 cmd
->incr
.message
.header
.request
.bodylen
= (uint32_t)(keylen
+ 20);
501 cmd
->incr
.message
.header
.request
.opaque
= 0xdeadbeef;
502 cmd
->incr
.message
.body
.delta
= memcached_htonll(delta
);
503 cmd
->incr
.message
.body
.initial
= memcached_htonll(initial
);
504 cmd
->incr
.message
.body
.expiration
= htonl(exptime
);
506 off_t key_offset
= sizeof (protocol_binary_request_no_extras
) + 20;
507 memcpy(cmd
->bytes
+ key_offset
, key
, keylen
);
511 * Validate the response header from the server
512 * @param rsp the response to check
513 * @param cc the expected command
514 * @param status the expected status
516 static enum test_return
do_validate_response_header(response
*rsp
,
517 uint8_t cc
, uint16_t status
)
519 verify(rsp
->plain
.message
.header
.response
.magic
== PROTOCOL_BINARY_RES
);
520 verify(rsp
->plain
.message
.header
.response
.opcode
== cc
);
521 verify(rsp
->plain
.message
.header
.response
.datatype
== PROTOCOL_BINARY_RAW_BYTES
);
522 verify(rsp
->plain
.message
.header
.response
.status
== status
);
523 verify(rsp
->plain
.message
.header
.response
.opaque
== 0xdeadbeef);
525 if (status
== PROTOCOL_BINARY_RESPONSE_SUCCESS
)
528 case PROTOCOL_BINARY_CMD_ADDQ
:
529 case PROTOCOL_BINARY_CMD_APPENDQ
:
530 case PROTOCOL_BINARY_CMD_DECREMENTQ
:
531 case PROTOCOL_BINARY_CMD_DELETEQ
:
532 case PROTOCOL_BINARY_CMD_FLUSHQ
:
533 case PROTOCOL_BINARY_CMD_INCREMENTQ
:
534 case PROTOCOL_BINARY_CMD_PREPENDQ
:
535 case PROTOCOL_BINARY_CMD_QUITQ
:
536 case PROTOCOL_BINARY_CMD_REPLACEQ
:
537 case PROTOCOL_BINARY_CMD_SETQ
:
538 verify("Quiet command shouldn't return on success" == NULL
);
545 case PROTOCOL_BINARY_CMD_ADD
:
546 case PROTOCOL_BINARY_CMD_REPLACE
:
547 case PROTOCOL_BINARY_CMD_SET
:
548 case PROTOCOL_BINARY_CMD_APPEND
:
549 case PROTOCOL_BINARY_CMD_PREPEND
:
550 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
551 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
552 verify(rsp
->plain
.message
.header
.response
.bodylen
== 0);
553 verify(rsp
->plain
.message
.header
.response
.cas
!= 0);
555 case PROTOCOL_BINARY_CMD_FLUSH
:
556 case PROTOCOL_BINARY_CMD_NOOP
:
557 case PROTOCOL_BINARY_CMD_QUIT
:
558 case PROTOCOL_BINARY_CMD_DELETE
:
559 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
560 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
561 verify(rsp
->plain
.message
.header
.response
.bodylen
== 0);
562 verify(rsp
->plain
.message
.header
.response
.cas
== 0);
565 case PROTOCOL_BINARY_CMD_DECREMENT
:
566 case PROTOCOL_BINARY_CMD_INCREMENT
:
567 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
568 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
569 verify(rsp
->plain
.message
.header
.response
.bodylen
== 8);
570 verify(rsp
->plain
.message
.header
.response
.cas
!= 0);
573 case PROTOCOL_BINARY_CMD_STAT
:
574 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
575 /* key and value exists in all packets except in the terminating */
576 verify(rsp
->plain
.message
.header
.response
.cas
== 0);
579 case PROTOCOL_BINARY_CMD_VERSION
:
580 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
581 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
582 verify(rsp
->plain
.message
.header
.response
.bodylen
!= 0);
583 verify(rsp
->plain
.message
.header
.response
.cas
== 0);
586 case PROTOCOL_BINARY_CMD_GET
:
587 case PROTOCOL_BINARY_CMD_GETQ
:
588 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
589 verify(rsp
->plain
.message
.header
.response
.extlen
== 4);
590 verify(rsp
->plain
.message
.header
.response
.cas
!= 0);
593 case PROTOCOL_BINARY_CMD_GETK
:
594 case PROTOCOL_BINARY_CMD_GETKQ
:
595 verify(rsp
->plain
.message
.header
.response
.keylen
!= 0);
596 verify(rsp
->plain
.message
.header
.response
.extlen
== 4);
597 verify(rsp
->plain
.message
.header
.response
.cas
!= 0);
601 /* Undefined command code */
607 verify(rsp
->plain
.message
.header
.response
.cas
== 0);
608 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
609 if (cc
!= PROTOCOL_BINARY_CMD_GETK
)
611 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
618 /* We call verify(validate_response_header), but that macro
619 * expects a boolean expression, and the function returns
620 * an enum.... Let's just create a macro to avoid cluttering
621 * the code with all of the == TEST_PASS ;-)
623 #define validate_response_header(a,b,c) \
624 do_validate_response_header(a,b,c) == TEST_PASS
627 static enum test_return
send_binary_noop(void)
630 raw_command(&cmd
, PROTOCOL_BINARY_CMD_NOOP
, NULL
, 0, NULL
, 0);
631 execute(send_packet(&cmd
));
635 static enum test_return
receive_binary_noop(void)
638 execute(recv_packet(&rsp
));
639 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_NOOP
,
640 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
644 static enum test_return
test_binary_noop(void)
646 execute(send_binary_noop());
647 execute(receive_binary_noop());
651 static enum test_return
test_binary_quit_impl(uint8_t cc
)
655 raw_command(&cmd
, cc
, NULL
, 0, NULL
, 0);
657 execute(send_packet(&cmd
));
658 if (cc
== PROTOCOL_BINARY_CMD_QUIT
)
660 execute(recv_packet(&rsp
));
661 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_QUIT
,
662 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
665 /* Socket should be closed now, read should return EXIT_SUCCESS */
666 verify(timeout_io_op(sock
, POLLIN
, rsp
.bytes
, sizeof(rsp
.bytes
)) == 0);
668 return TEST_PASS_RECONNECT
;
671 static enum test_return
test_binary_quit(void)
673 return test_binary_quit_impl(PROTOCOL_BINARY_CMD_QUIT
);
676 static enum test_return
test_binary_quitq(void)
678 return test_binary_quit_impl(PROTOCOL_BINARY_CMD_QUITQ
);
681 static enum test_return
test_binary_set_impl(const char* key
, uint8_t cc
)
686 uint64_t value
= 0xdeadbeefdeadcafeULL
;
687 storage_command(&cmd
, cc
, key
, strlen(key
), &value
, sizeof (value
), 0, 0);
689 /* set should always work */
690 for (int ii
= 0; ii
< 10; ii
++)
694 execute(send_packet(&cmd
));
698 execute(resend_packet(&cmd
));
701 if (cc
== PROTOCOL_BINARY_CMD_SET
)
703 execute(recv_packet(&rsp
));
704 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
707 execute(test_binary_noop());
711 * We need to get the current CAS id, and at this time we haven't
712 * verified that we have a working get
714 if (cc
== PROTOCOL_BINARY_CMD_SETQ
)
716 cmd
.set
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_SET
;
717 execute(resend_packet(&cmd
));
718 execute(recv_packet(&rsp
));
719 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_SET
,
720 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
721 cmd
.set
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_SETQ
;
724 /* try to set with the correct CAS value */
725 cmd
.plain
.message
.header
.request
.cas
= memcached_htonll(rsp
.plain
.message
.header
.response
.cas
);
726 execute(resend_packet(&cmd
));
727 if (cc
== PROTOCOL_BINARY_CMD_SET
)
729 execute(recv_packet(&rsp
));
730 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
733 execute(test_binary_noop());
735 /* try to set with an incorrect CAS value */
736 cmd
.plain
.message
.header
.request
.cas
= memcached_htonll(rsp
.plain
.message
.header
.response
.cas
- 1);
737 execute(resend_packet(&cmd
));
738 execute(send_binary_noop());
739 execute(recv_packet(&rsp
));
740 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS
));
741 execute(receive_binary_noop());
746 static enum test_return
test_binary_set(void)
748 return test_binary_set_impl("test_binary_set", PROTOCOL_BINARY_CMD_SET
);
751 static enum test_return
test_binary_setq(void)
753 return test_binary_set_impl("test_binary_setq", PROTOCOL_BINARY_CMD_SETQ
);
756 static enum test_return
test_binary_add_impl(const char* key
, uint8_t cc
)
760 uint64_t value
= 0xdeadbeefdeadcafeULL
;
761 storage_command(&cmd
, cc
, key
, strlen(key
), &value
, sizeof (value
), 0, 0);
763 /* first add should work, rest of them should fail (even with cas
765 for (int ii
=0; ii
< 10; ii
++)
768 execute(send_packet(&cmd
));
770 execute(resend_packet(&cmd
));
772 if (cc
== PROTOCOL_BINARY_CMD_ADD
|| ii
> 0)
774 uint16_t expected_result
;
776 expected_result
= PROTOCOL_BINARY_RESPONSE_SUCCESS
;
778 expected_result
= PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS
;
780 execute(send_binary_noop());
781 execute(recv_packet(&rsp
));
782 execute(receive_binary_noop());
783 verify(validate_response_header(&rsp
, cc
, expected_result
));
786 execute(test_binary_noop());
792 static enum test_return
test_binary_add(void)
794 return test_binary_add_impl("test_binary_add", PROTOCOL_BINARY_CMD_ADD
);
797 static enum test_return
test_binary_addq(void)
799 return test_binary_add_impl("test_binary_addq", PROTOCOL_BINARY_CMD_ADDQ
);
802 static enum test_return
binary_set_item(const char *key
, const char *value
)
806 storage_command(&cmd
, PROTOCOL_BINARY_CMD_SET
, key
, strlen(key
),
807 value
, strlen(value
), 0, 0);
808 execute(send_packet(&cmd
));
809 execute(recv_packet(&rsp
));
810 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_SET
,
811 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
815 static enum test_return
test_binary_replace_impl(const char* key
, uint8_t cc
)
819 uint64_t value
= 0xdeadbeefdeadcafeULL
;
820 storage_command(&cmd
, cc
, key
, strlen(key
), &value
, sizeof (value
), 0, 0);
822 /* first replace should fail, successive should succeed (when the
824 for (int ii
= 0; ii
< 10; ii
++)
828 execute(send_packet(&cmd
));
832 execute(resend_packet(&cmd
));
835 if (cc
== PROTOCOL_BINARY_CMD_REPLACE
|| ii
== 0)
837 uint16_t expected_result
;
840 expected_result
=PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
;
844 expected_result
=PROTOCOL_BINARY_RESPONSE_SUCCESS
;
847 execute(send_binary_noop());
848 execute(recv_packet(&rsp
));
849 execute(receive_binary_noop());
850 verify(validate_response_header(&rsp
, cc
, expected_result
));
853 execute(binary_set_item(key
, key
));
857 execute(test_binary_noop());
861 /* verify that replace with CAS value works! */
862 cmd
.plain
.message
.header
.request
.cas
= memcached_htonll(rsp
.plain
.message
.header
.response
.cas
);
863 execute(resend_packet(&cmd
));
865 if (cc
== PROTOCOL_BINARY_CMD_REPLACE
)
867 execute(recv_packet(&rsp
));
868 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
871 execute(test_binary_noop());
873 /* try to set with an incorrect CAS value */
874 cmd
.plain
.message
.header
.request
.cas
= memcached_htonll(rsp
.plain
.message
.header
.response
.cas
- 1);
875 execute(resend_packet(&cmd
));
876 execute(send_binary_noop());
877 execute(recv_packet(&rsp
));
878 execute(receive_binary_noop());
879 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS
));
884 static enum test_return
test_binary_replace(void)
886 return test_binary_replace_impl("test_binary_replace", PROTOCOL_BINARY_CMD_REPLACE
);
889 static enum test_return
test_binary_replaceq(void)
891 return test_binary_replace_impl("test_binary_replaceq", PROTOCOL_BINARY_CMD_REPLACEQ
);
894 static enum test_return
test_binary_delete_impl(const char *key
, uint8_t cc
)
898 raw_command(&cmd
, cc
, key
, strlen(key
), NULL
, 0);
900 /* The delete shouldn't work the first time, because the item isn't there */
901 execute(send_packet(&cmd
));
902 execute(send_binary_noop());
903 execute(recv_packet(&rsp
));
904 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
));
905 execute(receive_binary_noop());
906 execute(binary_set_item(key
, key
));
908 /* The item should be present now, resend*/
909 execute(resend_packet(&cmd
));
910 if (cc
== PROTOCOL_BINARY_CMD_DELETE
)
912 execute(recv_packet(&rsp
));
913 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
916 execute(test_binary_noop());
921 static enum test_return
test_binary_delete(void)
923 return test_binary_delete_impl("test_binary_delete", PROTOCOL_BINARY_CMD_DELETE
);
926 static enum test_return
test_binary_deleteq(void)
928 return test_binary_delete_impl("test_binary_deleteq", PROTOCOL_BINARY_CMD_DELETEQ
);
931 static enum test_return
test_binary_get_impl(const char *key
, uint8_t cc
)
936 raw_command(&cmd
, cc
, key
, strlen(key
), NULL
, 0);
937 execute(send_packet(&cmd
));
938 execute(send_binary_noop());
940 if (cc
== PROTOCOL_BINARY_CMD_GET
|| cc
== PROTOCOL_BINARY_CMD_GETK
)
942 execute(recv_packet(&rsp
));
943 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
));
946 execute(receive_binary_noop());
948 execute(binary_set_item(key
, key
));
949 execute(resend_packet(&cmd
));
950 execute(send_binary_noop());
952 execute(recv_packet(&rsp
));
953 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
954 execute(receive_binary_noop());
959 static enum test_return
test_binary_get(void)
961 return test_binary_get_impl("test_binary_get", PROTOCOL_BINARY_CMD_GET
);
964 static enum test_return
test_binary_getk(void)
966 return test_binary_get_impl("test_binary_getk", PROTOCOL_BINARY_CMD_GETK
);
969 static enum test_return
test_binary_getq(void)
971 return test_binary_get_impl("test_binary_getq", PROTOCOL_BINARY_CMD_GETQ
);
974 static enum test_return
test_binary_getkq(void)
976 return test_binary_get_impl("test_binary_getkq", PROTOCOL_BINARY_CMD_GETKQ
);
979 static enum test_return
test_binary_incr_impl(const char* key
, uint8_t cc
)
983 arithmetic_command(&cmd
, cc
, key
, strlen(key
), 1, 0, 0);
986 for (ii
= 0; ii
< 10; ++ii
)
989 execute(send_packet(&cmd
));
991 execute(resend_packet(&cmd
));
993 if (cc
== PROTOCOL_BINARY_CMD_INCREMENT
)
995 execute(recv_packet(&rsp
));
996 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
997 verify(memcached_ntohll(rsp
.incr
.message
.body
.value
) == ii
);
1000 execute(test_binary_noop());
1003 /* @todo add incorrect CAS */
1007 static enum test_return
test_binary_incr(void)
1009 return test_binary_incr_impl("test_binary_incr", PROTOCOL_BINARY_CMD_INCREMENT
);
1012 static enum test_return
test_binary_incrq(void)
1014 return test_binary_incr_impl("test_binary_incrq", PROTOCOL_BINARY_CMD_INCREMENTQ
);
1017 static enum test_return
test_binary_decr_impl(const char* key
, uint8_t cc
)
1021 arithmetic_command(&cmd
, cc
, key
, strlen(key
), 1, 9, 0);
1024 for (ii
= 9; ii
> -1; --ii
)
1027 execute(send_packet(&cmd
));
1029 execute(resend_packet(&cmd
));
1031 if (cc
== PROTOCOL_BINARY_CMD_DECREMENT
)
1033 execute(recv_packet(&rsp
));
1034 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1035 verify(memcached_ntohll(rsp
.decr
.message
.body
.value
) == (uint64_t)ii
);
1038 execute(test_binary_noop());
1041 /* decr 0 should not wrap */
1042 execute(resend_packet(&cmd
));
1043 if (cc
== PROTOCOL_BINARY_CMD_DECREMENT
)
1045 execute(recv_packet(&rsp
));
1046 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1047 verify(memcached_ntohll(rsp
.decr
.message
.body
.value
) == 0);
1051 /* @todo get the value and verify! */
1055 /* @todo add incorrect cas */
1056 execute(test_binary_noop());
1060 static enum test_return
test_binary_decr(void)
1062 return test_binary_decr_impl("test_binary_decr",
1063 PROTOCOL_BINARY_CMD_DECREMENT
);
1066 static enum test_return
test_binary_decrq(void)
1068 return test_binary_decr_impl("test_binary_decrq",
1069 PROTOCOL_BINARY_CMD_DECREMENTQ
);
1072 static enum test_return
test_binary_version(void)
1076 raw_command(&cmd
, PROTOCOL_BINARY_CMD_VERSION
, NULL
, 0, NULL
, 0);
1078 execute(send_packet(&cmd
));
1079 execute(recv_packet(&rsp
));
1080 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_VERSION
,
1081 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1086 static enum test_return
test_binary_flush_impl(const char *key
, uint8_t cc
)
1091 for (int ii
= 0; ii
< 2; ++ii
)
1093 execute(binary_set_item(key
, key
));
1094 flush_command(&cmd
, cc
, 0, ii
== 0);
1095 execute(send_packet(&cmd
));
1097 if (cc
== PROTOCOL_BINARY_CMD_FLUSH
)
1099 execute(recv_packet(&rsp
));
1100 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1103 execute(test_binary_noop());
1105 raw_command(&cmd
, PROTOCOL_BINARY_CMD_GET
, key
, strlen(key
), NULL
, 0);
1106 execute(send_packet(&cmd
));
1107 execute(recv_packet(&rsp
));
1108 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_GET
,
1109 PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
));
1115 static enum test_return
test_binary_flush(void)
1117 return test_binary_flush_impl("test_binary_flush", PROTOCOL_BINARY_CMD_FLUSH
);
1120 static enum test_return
test_binary_flushq(void)
1122 return test_binary_flush_impl("test_binary_flushq", PROTOCOL_BINARY_CMD_FLUSHQ
);
1125 static enum test_return
test_binary_concat_impl(const char *key
, uint8_t cc
)
1131 if (cc
== PROTOCOL_BINARY_CMD_APPEND
|| cc
== PROTOCOL_BINARY_CMD_APPENDQ
)
1140 execute(binary_set_item(key
, value
));
1142 if (cc
== PROTOCOL_BINARY_CMD_APPEND
|| cc
== PROTOCOL_BINARY_CMD_APPENDQ
)
1151 raw_command(&cmd
, cc
, key
, strlen(key
), value
, strlen(value
));
1152 execute(send_packet(&cmd
));
1153 if (cc
== PROTOCOL_BINARY_CMD_APPEND
|| cc
== PROTOCOL_BINARY_CMD_PREPEND
)
1155 execute(recv_packet(&rsp
));
1156 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1160 execute(test_binary_noop());
1163 raw_command(&cmd
, PROTOCOL_BINARY_CMD_GET
, key
, strlen(key
), NULL
, 0);
1164 execute(send_packet(&cmd
));
1165 execute(recv_packet(&rsp
));
1166 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_GET
,
1167 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1168 verify(rsp
.plain
.message
.header
.response
.bodylen
- 4 == 11);
1169 verify(memcmp(rsp
.bytes
+ 28, "hello world", 11) == 0);
1174 static enum test_return
test_binary_append(void)
1176 return test_binary_concat_impl("test_binary_append", PROTOCOL_BINARY_CMD_APPEND
);
1179 static enum test_return
test_binary_prepend(void)
1181 return test_binary_concat_impl("test_binary_prepend", PROTOCOL_BINARY_CMD_PREPEND
);
1184 static enum test_return
test_binary_appendq(void)
1186 return test_binary_concat_impl("test_binary_appendq", PROTOCOL_BINARY_CMD_APPENDQ
);
1189 static enum test_return
test_binary_prependq(void)
1191 return test_binary_concat_impl("test_binary_prependq", PROTOCOL_BINARY_CMD_PREPENDQ
);
1194 static enum test_return
test_binary_stat(void)
1199 raw_command(&cmd
, PROTOCOL_BINARY_CMD_STAT
, NULL
, 0, NULL
, 0);
1200 execute(send_packet(&cmd
));
1204 execute(recv_packet(&rsp
));
1205 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_STAT
,
1206 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1207 } while (rsp
.plain
.message
.header
.response
.keylen
!= 0);
1212 static enum test_return
send_string(const char *cmd
)
1214 execute(retry_write(cmd
, strlen(cmd
)));
1218 static enum test_return
receive_line(char *buffer
, size_t size
)
1221 while (offset
< size
)
1223 execute(retry_read(buffer
+ offset
, 1));
1224 if (buffer
[offset
] == '\n')
1226 if (offset
+ 1 < size
)
1228 buffer
[offset
+ 1]= '\0';
1240 static enum test_return
receive_response(const char *msg
) {
1242 execute(receive_line(buffer
, sizeof(buffer
)));
1243 if (strcmp(msg
, buffer
) != 0) {
1244 fprintf(stderr
, "[%s]\n", buffer
);
1246 verify(strcmp(msg
, buffer
) == 0);
1250 static enum test_return
receive_error_response(void)
1253 execute(receive_line(buffer
, sizeof(buffer
)));
1254 verify(strncmp(buffer
, "ERROR", 5) == 0 ||
1255 strncmp(buffer
, "CLIENT_ERROR", 12) == 0 ||
1256 strncmp(buffer
, "SERVER_ERROR", 12) == 0);
1260 static enum test_return
test_ascii_quit(void)
1262 /* Verify that quit handles unknown options */
1263 execute(send_string("quit foo bar\r\n"));
1264 execute(receive_error_response());
1266 /* quit doesn't support noreply */
1267 execute(send_string("quit noreply\r\n"));
1268 execute(receive_error_response());
1270 /* Verify that quit works */
1271 execute(send_string("quit\r\n"));
1273 /* Socket should be closed now, read should return EXIT_SUCCESS */
1275 verify(timeout_io_op(sock
, POLLIN
, buffer
, sizeof(buffer
)) == 0);
1276 return TEST_PASS_RECONNECT
;
1280 static enum test_return
test_ascii_version(void)
1282 /* Verify that version command handles unknown options */
1283 execute(send_string("version foo bar\r\n"));
1284 execute(receive_error_response());
1286 /* version doesn't support noreply */
1287 execute(send_string("version noreply\r\n"));
1288 execute(receive_error_response());
1290 /* Verify that verify works */
1291 execute(send_string("version\r\n"));
1293 execute(receive_line(buffer
, sizeof(buffer
)));
1294 verify(strncmp(buffer
, "VERSION ", 8) == 0);
1299 static enum test_return
test_ascii_verbosity(void)
1301 /* This command does not adhere to the spec! */
1302 execute(send_string("verbosity foo bar my\r\n"));
1303 execute(receive_error_response());
1305 execute(send_string("verbosity noreply\r\n"));
1306 execute(test_ascii_version());
1308 execute(send_string("verbosity 0 noreply\r\n"));
1309 execute(test_ascii_version());
1311 execute(send_string("verbosity\r\n"));
1312 execute(receive_error_response());
1314 execute(send_string("verbosity 1\r\n"));
1315 execute(receive_response("OK\r\n"));
1317 execute(send_string("verbosity 0\r\n"));
1318 execute(receive_response("OK\r\n"));
1325 static enum test_return
test_ascii_set_impl(const char* key
, bool noreply
)
1327 /* @todo add tests for bogus format! */
1329 snprintf(buffer
, sizeof(buffer
), "set %s 0 0 5%s\r\nvalue\r\n", key
, noreply
? " noreply" : "");
1330 execute(send_string(buffer
));
1334 execute(receive_response("STORED\r\n"));
1337 return test_ascii_version();
1340 static enum test_return
test_ascii_set(void)
1342 return test_ascii_set_impl("test_ascii_set", false);
1345 static enum test_return
test_ascii_set_noreply(void)
1347 return test_ascii_set_impl("test_ascii_set_noreply", true);
1350 static enum test_return
test_ascii_add_impl(const char* key
, bool noreply
)
1352 /* @todo add tests for bogus format! */
1354 snprintf(buffer
, sizeof(buffer
), "add %s 0 0 5%s\r\nvalue\r\n", key
, noreply
? " noreply" : "");
1355 execute(send_string(buffer
));
1359 execute(receive_response("STORED\r\n"));
1362 execute(send_string(buffer
));
1366 execute(receive_response("NOT_STORED\r\n"));
1369 return test_ascii_version();
1372 static enum test_return
test_ascii_add(void)
1374 return test_ascii_add_impl("test_ascii_add", false);
1377 static enum test_return
test_ascii_add_noreply(void)
1379 return test_ascii_add_impl("test_ascii_add_noreply", true);
1382 static enum test_return
ascii_get_unknown_value(char **key
, char **value
, ssize_t
*ndata
)
1386 execute(receive_line(buffer
, sizeof(buffer
)));
1387 verify(strncmp(buffer
, "VALUE ", 6) == 0);
1388 char *end
= strchr(buffer
+ 6, ' ');
1389 verify(end
!= NULL
);
1394 *key
= strdup(buffer
+ 6);
1395 verify(*key
!= NULL
);
1399 unsigned long val
= strtoul(ptr
, &end
, 10); /* flags */
1403 verify(end
!= NULL
);
1405 *ndata
= (ssize_t
)strtoul(end
, &end
, 10); /* size */
1408 verify(end
!= NULL
);
1409 while (end
and *end
!= '\n' and isspace(*end
))
1411 verify(end
and *end
== '\n');
1413 *value
= static_cast<char*>(malloc((size_t)*ndata
));
1414 verify(*value
!= NULL
);
1416 execute(retry_read(*value
, (size_t)*ndata
));
1418 execute(retry_read(buffer
, 2));
1419 verify(memcmp(buffer
, "\r\n", 2) == 0);
1424 static enum test_return
ascii_get_value(const char *key
, const char *value
)
1428 size_t datasize
= strlen(value
);
1430 verify(datasize
< sizeof(buffer
));
1431 execute(receive_line(buffer
, sizeof(buffer
)));
1432 verify(strncmp(buffer
, "VALUE ", 6) == 0);
1433 verify(strncmp(buffer
+ 6, key
, strlen(key
)) == 0);
1434 char *ptr
= buffer
+ 6 + strlen(key
) + 1;
1438 unsigned long val
= strtoul(ptr
, &end
, 10); /* flags */
1442 verify(end
!= NULL
);
1445 val
= strtoul(end
, &end
, 10); /* size */
1448 verify(val
== datasize
);
1449 verify(end
!= NULL
);
1450 while (end
and *end
!= '\n' and isspace(*end
))
1454 verify(end
and *end
== '\n');
1456 execute(retry_read(buffer
, datasize
));
1457 verify(memcmp(buffer
, value
, datasize
) == 0);
1459 execute(retry_read(buffer
, 2));
1460 verify(memcmp(buffer
, "\r\n", 2) == 0);
1465 static enum test_return
ascii_get_item(const char *key
, const char *value
,
1472 datasize
= strlen(value
);
1475 verify(datasize
< sizeof(buffer
));
1476 snprintf(buffer
, sizeof(buffer
), "get %s\r\n", key
);
1477 execute(send_string(buffer
));
1481 execute(ascii_get_value(key
, value
));
1484 execute(retry_read(buffer
, 5));
1485 verify(memcmp(buffer
, "END\r\n", 5) == 0);
1490 static enum test_return
ascii_gets_value(const char *key
, const char *value
,
1495 size_t datasize
= strlen(value
);
1497 verify(datasize
< sizeof(buffer
));
1498 execute(receive_line(buffer
, sizeof(buffer
)));
1499 verify(strncmp(buffer
, "VALUE ", 6) == 0);
1500 verify(strncmp(buffer
+ 6, key
, strlen(key
)) == 0);
1501 char *ptr
= buffer
+ 6 + strlen(key
) + 1;
1505 unsigned long val
= strtoul(ptr
, &end
, 10); /* flags */
1509 verify(end
!= NULL
);
1512 val
= strtoul(end
, &end
, 10); /* size */
1515 verify(val
== datasize
);
1516 verify(end
!= NULL
);
1519 *cas
= strtoul(end
, &end
, 10); /* cas */
1522 verify(val
== datasize
);
1523 verify(end
!= NULL
);
1525 while (end
and *end
!= '\n' and isspace(*end
))
1529 verify(end
and *end
== '\n');
1531 execute(retry_read(buffer
, datasize
));
1532 verify(memcmp(buffer
, value
, datasize
) == 0);
1534 execute(retry_read(buffer
, 2));
1535 verify(memcmp(buffer
, "\r\n", 2) == 0);
1540 static enum test_return
ascii_gets_item(const char *key
, const char *value
,
1541 bool exist
, unsigned long *cas
)
1547 datasize
= strlen(value
);
1550 verify(datasize
< sizeof(buffer
));
1551 snprintf(buffer
, sizeof(buffer
), "gets %s\r\n", key
);
1552 execute(send_string(buffer
));
1555 execute(ascii_gets_value(key
, value
, cas
));
1557 execute(retry_read(buffer
, 5));
1558 verify(memcmp(buffer
, "END\r\n", 5) == 0);
1563 static enum test_return
ascii_set_item(const char *key
, const char *value
)
1566 size_t len
= strlen(value
);
1567 snprintf(buffer
, sizeof(buffer
), "set %s 0 0 %u\r\n", key
, (unsigned int)len
);
1568 execute(send_string(buffer
));
1569 execute(retry_write(value
, len
));
1570 execute(send_string("\r\n"));
1571 execute(receive_response("STORED\r\n"));
1575 static enum test_return
test_ascii_replace_impl(const char* key
, bool noreply
)
1578 snprintf(buffer
, sizeof(buffer
), "replace %s 0 0 5%s\r\nvalue\r\n", key
, noreply
? " noreply" : "");
1579 execute(send_string(buffer
));
1583 execute(test_ascii_version());
1587 execute(receive_response("NOT_STORED\r\n"));
1590 execute(ascii_set_item(key
, "value"));
1591 execute(ascii_get_item(key
, "value", true));
1594 execute(send_string(buffer
));
1597 execute(test_ascii_version());
1599 execute(receive_response("STORED\r\n"));
1601 return test_ascii_version();
1604 static enum test_return
test_ascii_replace(void)
1606 return test_ascii_replace_impl("test_ascii_replace", false);
1609 static enum test_return
test_ascii_replace_noreply(void)
1611 return test_ascii_replace_impl("test_ascii_replace_noreply", true);
1614 static enum test_return
test_ascii_cas_impl(const char* key
, bool noreply
)
1619 execute(ascii_set_item(key
, "value"));
1620 execute(ascii_gets_item(key
, "value", true, &cas
));
1622 snprintf(buffer
, sizeof(buffer
), "cas %s 0 0 6 %lu%s\r\nvalue2\r\n", key
, cas
, noreply
? " noreply" : "");
1623 execute(send_string(buffer
));
1627 execute(test_ascii_version());
1631 execute(receive_response("STORED\r\n"));
1634 /* reexecute the same command should fail due to illegal cas */
1635 execute(send_string(buffer
));
1639 execute(test_ascii_version());
1643 execute(receive_response("EXISTS\r\n"));
1646 return test_ascii_version();
1649 static enum test_return
test_ascii_cas(void)
1651 return test_ascii_cas_impl("test_ascii_cas", false);
1654 static enum test_return
test_ascii_cas_noreply(void)
1656 return test_ascii_cas_impl("test_ascii_cas_noreply", true);
1659 static enum test_return
test_ascii_delete_impl(const char *key
, bool noreply
)
1661 execute(ascii_set_item(key
, "value"));
1663 execute(send_string("delete\r\n"));
1664 execute(receive_error_response());
1665 /* BUG: the server accepts delete a b */
1666 execute(send_string("delete a b c d e\r\n"));
1667 execute(receive_error_response());
1670 snprintf(buffer
, sizeof(buffer
), "delete %s%s\r\n", key
, noreply
? " noreply" : "");
1671 execute(send_string(buffer
));
1674 execute(test_ascii_version());
1676 execute(receive_response("DELETED\r\n"));
1678 execute(ascii_get_item(key
, "value", false));
1679 execute(send_string(buffer
));
1681 execute(test_ascii_version());
1683 execute(receive_response("NOT_FOUND\r\n"));
1688 static enum test_return
test_ascii_delete(void)
1690 return test_ascii_delete_impl("test_ascii_delete", false);
1693 static enum test_return
test_ascii_delete_noreply(void)
1695 return test_ascii_delete_impl("test_ascii_delete_noreply", true);
1698 static enum test_return
test_ascii_get(void)
1700 execute(ascii_set_item("test_ascii_get", "value"));
1702 execute(send_string("get\r\n"));
1703 execute(receive_error_response());
1704 execute(ascii_get_item("test_ascii_get", "value", true));
1705 execute(ascii_get_item("test_ascii_get_notfound", "value", false));
1710 static enum test_return
test_ascii_gets(void)
1712 execute(ascii_set_item("test_ascii_gets", "value"));
1714 execute(send_string("gets\r\n"));
1715 execute(receive_error_response());
1717 execute(ascii_gets_item("test_ascii_gets", "value", true, &cas
));
1718 execute(ascii_gets_item("test_ascii_gets_notfound", "value", false, &cas
));
1723 static enum test_return
test_ascii_mget(void)
1725 const uint32_t nkeys
= 5;
1726 const char * const keys
[]= {
1729 /* test_ascii_mget_3 does not exist :) */
1735 for (uint32_t x
= 0; x
< nkeys
; ++x
)
1737 execute(ascii_set_item(keys
[x
], "value"));
1740 /* Ask for a key that doesn't exist as well */
1741 execute(send_string("get test_ascii_mget1 test_ascii_mget2 test_ascii_mget3 "
1742 "test_ascii_mget4 test_ascii_mget5 "
1743 "test_ascii_mget6\r\n"));
1745 std::vector
<char *> returned
;
1746 returned
.resize(nkeys
);
1748 for (uint32_t x
= 0; x
< nkeys
; ++x
)
1752 execute(ascii_get_unknown_value(&returned
[x
], &v
, &nbytes
));
1753 verify(nbytes
== 5);
1754 verify(memcmp(v
, "value", 5) == 0);
1759 execute(retry_read(buffer
, 5));
1760 verify(memcmp(buffer
, "END\r\n", 5) == 0);
1762 /* verify that we got all the keys we expected */
1763 for (uint32_t x
= 0; x
< nkeys
; ++x
)
1766 for (uint32_t y
= 0; y
< nkeys
; ++y
)
1768 if (strcmp(keys
[x
], returned
[y
]) == 0)
1777 for (uint32_t x
= 0; x
< nkeys
; ++x
)
1785 static enum test_return
test_ascii_incr_impl(const char* key
, bool noreply
)
1788 snprintf(cmd
, sizeof(cmd
), "incr %s 1%s\r\n", key
, noreply
? " noreply" : "");
1790 execute(ascii_set_item(key
, "0"));
1791 for (int x
= 1; x
< 11; ++x
)
1793 execute(send_string(cmd
));
1796 execute(test_ascii_version());
1800 execute(receive_line(buffer
, sizeof(buffer
)));
1801 int val
= atoi(buffer
);
1806 execute(ascii_get_item(key
, "10", true));
1811 static enum test_return
test_ascii_incr(void)
1813 return test_ascii_incr_impl("test_ascii_incr", false);
1816 static enum test_return
test_ascii_incr_noreply(void)
1818 return test_ascii_incr_impl("test_ascii_incr_noreply", true);
1821 static enum test_return
test_ascii_decr_impl(const char* key
, bool noreply
)
1824 snprintf(cmd
, sizeof(cmd
), "decr %s 1%s\r\n", key
, noreply
? " noreply" : "");
1826 execute(ascii_set_item(key
, "9"));
1827 for (int x
= 8; x
> -1; --x
)
1829 execute(send_string(cmd
));
1833 execute(test_ascii_version());
1838 execute(receive_line(buffer
, sizeof(buffer
)));
1839 int val
= atoi(buffer
);
1844 execute(ascii_get_item(key
, "0", true));
1846 /* verify that it doesn't wrap */
1847 execute(send_string(cmd
));
1850 execute(test_ascii_version());
1855 execute(receive_line(buffer
, sizeof(buffer
)));
1857 execute(ascii_get_item(key
, "0", true));
1862 static enum test_return
test_ascii_decr(void)
1864 return test_ascii_decr_impl("test_ascii_decr", false);
1867 static enum test_return
test_ascii_decr_noreply(void)
1869 return test_ascii_decr_impl("test_ascii_decr_noreply", true);
1873 static enum test_return
test_ascii_flush_impl(const char *key
, bool noreply
)
1876 /* Verify that the flush_all command handles unknown options */
1877 /* Bug in the current memcached server! */
1878 execute(send_string("flush_all foo bar\r\n"));
1879 execute(receive_error_response());
1882 execute(ascii_set_item(key
, key
));
1883 execute(ascii_get_item(key
, key
, true));
1887 execute(send_string("flush_all noreply\r\n"));
1888 execute(test_ascii_version());
1892 execute(send_string("flush_all\r\n"));
1893 execute(receive_response("OK\r\n"));
1896 execute(ascii_get_item(key
, key
, false));
1901 static enum test_return
test_ascii_flush(void)
1903 return test_ascii_flush_impl("test_ascii_flush", false);
1906 static enum test_return
test_ascii_flush_noreply(void)
1908 return test_ascii_flush_impl("test_ascii_flush_noreply", true);
1911 static enum test_return
test_ascii_concat_impl(const char *key
,
1922 execute(ascii_set_item(key
, value
));
1934 snprintf(cmd
, sizeof(cmd
), "%s %s 0 0 %u%s\r\n%s\r\n",
1935 append
? "append" : "prepend",
1936 key
, (unsigned int)strlen(value
), noreply
? " noreply" : "",
1938 execute(send_string(cmd
));
1942 execute(test_ascii_version());
1946 execute(receive_response("STORED\r\n"));
1949 execute(ascii_get_item(key
, "hello world", true));
1951 snprintf(cmd
, sizeof(cmd
), "%s %s_notfound 0 0 %u%s\r\n%s\r\n",
1952 append
? "append" : "prepend",
1953 key
, (unsigned int)strlen(value
), noreply
? " noreply" : "",
1955 execute(send_string(cmd
));
1959 execute(test_ascii_version());
1963 execute(receive_response("NOT_STORED\r\n"));
1969 static enum test_return
test_ascii_append(void)
1971 return test_ascii_concat_impl("test_ascii_append", true, false);
1974 static enum test_return
test_ascii_prepend(void)
1976 return test_ascii_concat_impl("test_ascii_prepend", false, false);
1979 static enum test_return
test_ascii_append_noreply(void)
1981 return test_ascii_concat_impl("test_ascii_append_noreply", true, true);
1984 static enum test_return
test_ascii_prepend_noreply(void)
1986 return test_ascii_concat_impl("test_ascii_prepend_noreply", false, true);
1989 static enum test_return
test_ascii_stat(void)
1991 execute(send_string("stats noreply\r\n"));
1992 execute(receive_error_response());
1993 execute(send_string("stats\r\n"));
1996 execute(receive_line(buffer
, sizeof(buffer
)));
1997 } while (strcmp(buffer
, "END\r\n") != 0);
1999 return TEST_PASS_RECONNECT
;
2002 typedef enum test_return(*TEST_FUNC
)(void);
2006 const char *description
;
2010 struct testcase testcases
[]= {
2011 { "ascii quit", test_ascii_quit
},
2012 { "ascii version", test_ascii_version
},
2013 { "ascii verbosity", test_ascii_verbosity
},
2014 { "ascii set", test_ascii_set
},
2015 { "ascii set noreply", test_ascii_set_noreply
},
2016 { "ascii get", test_ascii_get
},
2017 { "ascii gets", test_ascii_gets
},
2018 { "ascii mget", test_ascii_mget
},
2019 { "ascii flush", test_ascii_flush
},
2020 { "ascii flush noreply", test_ascii_flush_noreply
},
2021 { "ascii add", test_ascii_add
},
2022 { "ascii add noreply", test_ascii_add_noreply
},
2023 { "ascii replace", test_ascii_replace
},
2024 { "ascii replace noreply", test_ascii_replace_noreply
},
2025 { "ascii cas", test_ascii_cas
},
2026 { "ascii cas noreply", test_ascii_cas_noreply
},
2027 { "ascii delete", test_ascii_delete
},
2028 { "ascii delete noreply", test_ascii_delete_noreply
},
2029 { "ascii incr", test_ascii_incr
},
2030 { "ascii incr noreply", test_ascii_incr_noreply
},
2031 { "ascii decr", test_ascii_decr
},
2032 { "ascii decr noreply", test_ascii_decr_noreply
},
2033 { "ascii append", test_ascii_append
},
2034 { "ascii append noreply", test_ascii_append_noreply
},
2035 { "ascii prepend", test_ascii_prepend
},
2036 { "ascii prepend noreply", test_ascii_prepend_noreply
},
2037 { "ascii stat", test_ascii_stat
},
2038 { "binary noop", test_binary_noop
},
2039 { "binary quit", test_binary_quit
},
2040 { "binary quitq", test_binary_quitq
},
2041 { "binary set", test_binary_set
},
2042 { "binary setq", test_binary_setq
},
2043 { "binary flush", test_binary_flush
},
2044 { "binary flushq", test_binary_flushq
},
2045 { "binary add", test_binary_add
},
2046 { "binary addq", test_binary_addq
},
2047 { "binary replace", test_binary_replace
},
2048 { "binary replaceq", test_binary_replaceq
},
2049 { "binary delete", test_binary_delete
},
2050 { "binary deleteq", test_binary_deleteq
},
2051 { "binary get", test_binary_get
},
2052 { "binary getq", test_binary_getq
},
2053 { "binary getk", test_binary_getk
},
2054 { "binary getkq", test_binary_getkq
},
2055 { "binary incr", test_binary_incr
},
2056 { "binary incrq", test_binary_incrq
},
2057 { "binary decr", test_binary_decr
},
2058 { "binary decrq", test_binary_decrq
},
2059 { "binary version", test_binary_version
},
2060 { "binary append", test_binary_append
},
2061 { "binary appendq", test_binary_appendq
},
2062 { "binary prepend", test_binary_prepend
},
2063 { "binary prependq", test_binary_prependq
},
2064 { "binary stat", test_binary_stat
},
2068 const int ascii_tests
= 1;
2069 const int binary_tests
= 2;
2077 int main(int argc
, char **argv
)
2079 static const char * const status_msg
[]= {"[skip]", "[pass]", "[pass]", "[FAIL]"};
2080 struct test_type_st tests
= { true, true };
2083 const char *hostname
= NULL
;
2084 const char *port
= MEMCACHED_DEFAULT_PORT_STRING
;
2087 const char *testname
= NULL
;
2091 while ((cmd
= getopt(argc
, argv
, "qt:vch:p:PT:?ab")) != EOF
)
2096 tests
.binary
= false;
2105 timeout
= atoi(optarg
);
2108 fprintf(stderr
, "Invalid timeout. Please specify a number for -t\n");
2109 return EXIT_FAILURE
;
2113 case 'v': verbose
= true;
2116 case 'c': do_core
= true;
2119 case 'h': hostname
= optarg
;
2122 case 'p': port
= optarg
;
2129 case 'P': prompt
= true;
2132 case 'T': testname
= optarg
;
2136 fprintf(stderr
, "Usage: %s [-h hostname] [-p port] [-c] [-v] [-t n] [-P] [-T testname]'\n"
2137 "\t-c\tGenerate coredump if a test fails\n"
2138 "\t-v\tVerbose test output (print out the assertion)\n"
2139 "\t-t n\tSet the timeout for io-operations to n seconds\n"
2140 "\t-P\tPrompt the user before starting a test.\n"
2141 "\t\t\t\"skip\" will skip the test\n"
2142 "\t\t\t\"quit\" will terminate memcapable\n"
2143 "\t\t\tEverything else will start the test\n"
2144 "\t-T n\tJust run the test named n\n"
2145 "\t-a\tOnly test the ascii protocol\n"
2146 "\t-b\tOnly test the binary protocol\n",
2148 return EXIT_SUCCESS
;
2154 fprintf(stderr
, "No hostname was provided.\n");
2155 return EXIT_FAILURE
;
2158 initialize_sockets();
2159 sock
= connect_server(hostname
, port
);
2160 if (sock
== INVALID_SOCKET
)
2162 fprintf(stderr
, "Failed to connect to <%s:%s>: %s\n",
2163 hostname
?:"(null)", port
?:"(null)", strerror(get_socket_errno()));
2164 return EXIT_FAILURE
;
2167 for (int ii
= 0; testcases
[ii
].description
!= NULL
; ++ii
)
2169 if (testname
!= NULL
&& strcmp(testcases
[ii
].description
, testname
) != 0)
2174 if ((testcases
[ii
].description
[0] == 'a' && (tests
.ascii
) == 0) ||
2175 (testcases
[ii
].description
[0] == 'b' && (tests
.binary
) == 0))
2180 fprintf(stdout
, "%-40s", testcases
[ii
].description
);
2185 fprintf(stdout
, "\nPress <return> when you are ready? ");
2186 char buffer
[80] = {0};
2187 if (fgets(buffer
, sizeof(buffer
), stdin
) != NULL
) {
2188 if (strncmp(buffer
, "skip", 4) == 0)
2190 fprintf(stdout
, "%-40s%s\n", testcases
[ii
].description
,
2191 status_msg
[TEST_SKIP
]);
2195 if (strncmp(buffer
, "quit", 4) == 0)
2201 fprintf(stdout
, "%-40s", testcases
[ii
].description
);
2205 bool reconnect
= false;
2206 enum test_return ret
= testcases
[ii
].function();
2207 if (ret
== TEST_FAIL
)
2213 fprintf(stderr
, "\n");
2216 else if (ret
== TEST_PASS_RECONNECT
)
2221 if (ret
== TEST_FAIL
)
2223 fprintf(stderr
, "%s\n", status_msg
[ret
]);
2227 fprintf(stdout
, "%s\n", status_msg
[ret
]);
2233 if ((sock
= connect_server(hostname
, port
)) == INVALID_SOCKET
)
2235 fprintf(stderr
, "Failed to connect to <%s:%s>: %s\n", hostname
?:"(null)", port
?:"(null)", strerror(get_socket_errno()));
2236 fprintf(stderr
, "%d of %d tests failed\n", failed
, total
);
2237 return EXIT_FAILURE
;
2245 fprintf(stdout
, "All tests passed\n");
2249 fprintf(stderr
, "%d of %d tests failed\n", failed
, total
);
2252 return (failed
== 0) ? EXIT_SUCCESS
: EXIT_FAILURE
;