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 "libmemcached/memcached/protocol_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
);
544 case PROTOCOL_BINARY_CMD_ADD
:
545 case PROTOCOL_BINARY_CMD_REPLACE
:
546 case PROTOCOL_BINARY_CMD_SET
:
547 case PROTOCOL_BINARY_CMD_APPEND
:
548 case PROTOCOL_BINARY_CMD_PREPEND
:
549 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
550 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
551 verify(rsp
->plain
.message
.header
.response
.bodylen
== 0);
552 verify(rsp
->plain
.message
.header
.response
.cas
!= 0);
554 case PROTOCOL_BINARY_CMD_FLUSH
:
555 case PROTOCOL_BINARY_CMD_NOOP
:
556 case PROTOCOL_BINARY_CMD_QUIT
:
557 case PROTOCOL_BINARY_CMD_DELETE
:
558 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
559 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
560 verify(rsp
->plain
.message
.header
.response
.bodylen
== 0);
561 verify(rsp
->plain
.message
.header
.response
.cas
== 0);
564 case PROTOCOL_BINARY_CMD_DECREMENT
:
565 case PROTOCOL_BINARY_CMD_INCREMENT
:
566 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
567 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
568 verify(rsp
->plain
.message
.header
.response
.bodylen
== 8);
569 verify(rsp
->plain
.message
.header
.response
.cas
!= 0);
572 case PROTOCOL_BINARY_CMD_STAT
:
573 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
574 /* key and value exists in all packets except in the terminating */
575 verify(rsp
->plain
.message
.header
.response
.cas
== 0);
578 case PROTOCOL_BINARY_CMD_VERSION
:
579 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
580 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
581 verify(rsp
->plain
.message
.header
.response
.bodylen
!= 0);
582 verify(rsp
->plain
.message
.header
.response
.cas
== 0);
585 case PROTOCOL_BINARY_CMD_GET
:
586 case PROTOCOL_BINARY_CMD_GETQ
:
587 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
588 verify(rsp
->plain
.message
.header
.response
.extlen
== 4);
589 verify(rsp
->plain
.message
.header
.response
.cas
!= 0);
592 case PROTOCOL_BINARY_CMD_GETK
:
593 case PROTOCOL_BINARY_CMD_GETKQ
:
594 verify(rsp
->plain
.message
.header
.response
.keylen
!= 0);
595 verify(rsp
->plain
.message
.header
.response
.extlen
== 4);
596 verify(rsp
->plain
.message
.header
.response
.cas
!= 0);
600 /* Undefined command code */
606 verify(rsp
->plain
.message
.header
.response
.cas
== 0);
607 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
608 if (cc
!= PROTOCOL_BINARY_CMD_GETK
)
610 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
617 /* We call verify(validate_response_header), but that macro
618 * expects a boolean expression, and the function returns
619 * an enum.... Let's just create a macro to avoid cluttering
620 * the code with all of the == TEST_PASS ;-)
622 #define validate_response_header(a,b,c) \
623 do_validate_response_header(a,b,c) == TEST_PASS
626 static enum test_return
send_binary_noop(void)
629 raw_command(&cmd
, PROTOCOL_BINARY_CMD_NOOP
, NULL
, 0, NULL
, 0);
630 execute(send_packet(&cmd
));
634 static enum test_return
receive_binary_noop(void)
637 execute(recv_packet(&rsp
));
638 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_NOOP
,
639 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
643 static enum test_return
test_binary_noop(void)
645 execute(send_binary_noop());
646 execute(receive_binary_noop());
650 static enum test_return
test_binary_quit_impl(uint8_t cc
)
654 raw_command(&cmd
, cc
, NULL
, 0, NULL
, 0);
656 execute(send_packet(&cmd
));
657 if (cc
== PROTOCOL_BINARY_CMD_QUIT
)
659 execute(recv_packet(&rsp
));
660 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_QUIT
,
661 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
664 /* Socket should be closed now, read should return EXIT_SUCCESS */
665 verify(timeout_io_op(sock
, POLLIN
, rsp
.bytes
, sizeof(rsp
.bytes
)) == 0);
667 return TEST_PASS_RECONNECT
;
670 static enum test_return
test_binary_quit(void)
672 return test_binary_quit_impl(PROTOCOL_BINARY_CMD_QUIT
);
675 static enum test_return
test_binary_quitq(void)
677 return test_binary_quit_impl(PROTOCOL_BINARY_CMD_QUITQ
);
680 static enum test_return
test_binary_set_impl(const char* key
, uint8_t cc
)
685 uint64_t value
= 0xdeadbeefdeadcafeULL
;
686 storage_command(&cmd
, cc
, key
, strlen(key
), &value
, sizeof (value
), 0, 0);
688 /* set should always work */
689 for (int ii
= 0; ii
< 10; ii
++)
693 execute(send_packet(&cmd
));
697 execute(resend_packet(&cmd
));
700 if (cc
== PROTOCOL_BINARY_CMD_SET
)
702 execute(recv_packet(&rsp
));
703 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
706 execute(test_binary_noop());
710 * We need to get the current CAS id, and at this time we haven't
711 * verified that we have a working get
713 if (cc
== PROTOCOL_BINARY_CMD_SETQ
)
715 cmd
.set
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_SET
;
716 execute(resend_packet(&cmd
));
717 execute(recv_packet(&rsp
));
718 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_SET
,
719 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
720 cmd
.set
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_SETQ
;
723 /* try to set with the correct CAS value */
724 cmd
.plain
.message
.header
.request
.cas
= memcached_htonll(rsp
.plain
.message
.header
.response
.cas
);
725 execute(resend_packet(&cmd
));
726 if (cc
== PROTOCOL_BINARY_CMD_SET
)
728 execute(recv_packet(&rsp
));
729 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
732 execute(test_binary_noop());
734 /* try to set with an incorrect CAS value */
735 cmd
.plain
.message
.header
.request
.cas
= memcached_htonll(rsp
.plain
.message
.header
.response
.cas
- 1);
736 execute(resend_packet(&cmd
));
737 execute(send_binary_noop());
738 execute(recv_packet(&rsp
));
739 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS
));
740 execute(receive_binary_noop());
745 static enum test_return
test_binary_set(void)
747 return test_binary_set_impl("test_binary_set", PROTOCOL_BINARY_CMD_SET
);
750 static enum test_return
test_binary_setq(void)
752 return test_binary_set_impl("test_binary_setq", PROTOCOL_BINARY_CMD_SETQ
);
755 static enum test_return
test_binary_add_impl(const char* key
, uint8_t cc
)
759 uint64_t value
= 0xdeadbeefdeadcafeULL
;
760 storage_command(&cmd
, cc
, key
, strlen(key
), &value
, sizeof (value
), 0, 0);
762 /* first add should work, rest of them should fail (even with cas
764 for (int ii
=0; ii
< 10; ii
++)
767 execute(send_packet(&cmd
));
769 execute(resend_packet(&cmd
));
771 if (cc
== PROTOCOL_BINARY_CMD_ADD
|| ii
> 0)
773 uint16_t expected_result
;
775 expected_result
= PROTOCOL_BINARY_RESPONSE_SUCCESS
;
777 expected_result
= PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS
;
779 execute(send_binary_noop());
780 execute(recv_packet(&rsp
));
781 execute(receive_binary_noop());
782 verify(validate_response_header(&rsp
, cc
, expected_result
));
785 execute(test_binary_noop());
791 static enum test_return
test_binary_add(void)
793 return test_binary_add_impl("test_binary_add", PROTOCOL_BINARY_CMD_ADD
);
796 static enum test_return
test_binary_addq(void)
798 return test_binary_add_impl("test_binary_addq", PROTOCOL_BINARY_CMD_ADDQ
);
801 static enum test_return
binary_set_item(const char *key
, const char *value
)
805 storage_command(&cmd
, PROTOCOL_BINARY_CMD_SET
, key
, strlen(key
),
806 value
, strlen(value
), 0, 0);
807 execute(send_packet(&cmd
));
808 execute(recv_packet(&rsp
));
809 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_SET
,
810 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
814 static enum test_return
test_binary_replace_impl(const char* key
, uint8_t cc
)
818 uint64_t value
= 0xdeadbeefdeadcafeULL
;
819 storage_command(&cmd
, cc
, key
, strlen(key
), &value
, sizeof (value
), 0, 0);
821 /* first replace should fail, successive should succeed (when the
823 for (int ii
= 0; ii
< 10; ii
++)
827 execute(send_packet(&cmd
));
831 execute(resend_packet(&cmd
));
834 if (cc
== PROTOCOL_BINARY_CMD_REPLACE
|| ii
== 0)
836 uint16_t expected_result
;
839 expected_result
=PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
;
843 expected_result
=PROTOCOL_BINARY_RESPONSE_SUCCESS
;
846 execute(send_binary_noop());
847 execute(recv_packet(&rsp
));
848 execute(receive_binary_noop());
849 verify(validate_response_header(&rsp
, cc
, expected_result
));
852 execute(binary_set_item(key
, key
));
856 execute(test_binary_noop());
860 /* verify that replace with CAS value works! */
861 cmd
.plain
.message
.header
.request
.cas
= memcached_htonll(rsp
.plain
.message
.header
.response
.cas
);
862 execute(resend_packet(&cmd
));
864 if (cc
== PROTOCOL_BINARY_CMD_REPLACE
)
866 execute(recv_packet(&rsp
));
867 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
870 execute(test_binary_noop());
872 /* try to set with an incorrect CAS value */
873 cmd
.plain
.message
.header
.request
.cas
= memcached_htonll(rsp
.plain
.message
.header
.response
.cas
- 1);
874 execute(resend_packet(&cmd
));
875 execute(send_binary_noop());
876 execute(recv_packet(&rsp
));
877 execute(receive_binary_noop());
878 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS
));
883 static enum test_return
test_binary_replace(void)
885 return test_binary_replace_impl("test_binary_replace", PROTOCOL_BINARY_CMD_REPLACE
);
888 static enum test_return
test_binary_replaceq(void)
890 return test_binary_replace_impl("test_binary_replaceq", PROTOCOL_BINARY_CMD_REPLACEQ
);
893 static enum test_return
test_binary_delete_impl(const char *key
, uint8_t cc
)
897 raw_command(&cmd
, cc
, key
, strlen(key
), NULL
, 0);
899 /* The delete shouldn't work the first time, because the item isn't there */
900 execute(send_packet(&cmd
));
901 execute(send_binary_noop());
902 execute(recv_packet(&rsp
));
903 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
));
904 execute(receive_binary_noop());
905 execute(binary_set_item(key
, key
));
907 /* The item should be present now, resend*/
908 execute(resend_packet(&cmd
));
909 if (cc
== PROTOCOL_BINARY_CMD_DELETE
)
911 execute(recv_packet(&rsp
));
912 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
915 execute(test_binary_noop());
920 static enum test_return
test_binary_delete(void)
922 return test_binary_delete_impl("test_binary_delete", PROTOCOL_BINARY_CMD_DELETE
);
925 static enum test_return
test_binary_deleteq(void)
927 return test_binary_delete_impl("test_binary_deleteq", PROTOCOL_BINARY_CMD_DELETEQ
);
930 static enum test_return
test_binary_get_impl(const char *key
, uint8_t cc
)
935 raw_command(&cmd
, cc
, key
, strlen(key
), NULL
, 0);
936 execute(send_packet(&cmd
));
937 execute(send_binary_noop());
939 if (cc
== PROTOCOL_BINARY_CMD_GET
|| cc
== PROTOCOL_BINARY_CMD_GETK
)
941 execute(recv_packet(&rsp
));
942 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
));
945 execute(receive_binary_noop());
947 execute(binary_set_item(key
, key
));
948 execute(resend_packet(&cmd
));
949 execute(send_binary_noop());
951 execute(recv_packet(&rsp
));
952 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
953 execute(receive_binary_noop());
958 static enum test_return
test_binary_get(void)
960 return test_binary_get_impl("test_binary_get", PROTOCOL_BINARY_CMD_GET
);
963 static enum test_return
test_binary_getk(void)
965 return test_binary_get_impl("test_binary_getk", PROTOCOL_BINARY_CMD_GETK
);
968 static enum test_return
test_binary_getq(void)
970 return test_binary_get_impl("test_binary_getq", PROTOCOL_BINARY_CMD_GETQ
);
973 static enum test_return
test_binary_getkq(void)
975 return test_binary_get_impl("test_binary_getkq", PROTOCOL_BINARY_CMD_GETKQ
);
978 static enum test_return
test_binary_incr_impl(const char* key
, uint8_t cc
)
982 arithmetic_command(&cmd
, cc
, key
, strlen(key
), 1, 0, 0);
985 for (ii
= 0; ii
< 10; ++ii
)
988 execute(send_packet(&cmd
));
990 execute(resend_packet(&cmd
));
992 if (cc
== PROTOCOL_BINARY_CMD_INCREMENT
)
994 execute(recv_packet(&rsp
));
995 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
996 verify(memcached_ntohll(rsp
.incr
.message
.body
.value
) == ii
);
999 execute(test_binary_noop());
1002 /* @todo add incorrect CAS */
1006 static enum test_return
test_binary_incr(void)
1008 return test_binary_incr_impl("test_binary_incr", PROTOCOL_BINARY_CMD_INCREMENT
);
1011 static enum test_return
test_binary_incrq(void)
1013 return test_binary_incr_impl("test_binary_incrq", PROTOCOL_BINARY_CMD_INCREMENTQ
);
1016 static enum test_return
test_binary_decr_impl(const char* key
, uint8_t cc
)
1020 arithmetic_command(&cmd
, cc
, key
, strlen(key
), 1, 9, 0);
1023 for (ii
= 9; ii
> -1; --ii
)
1026 execute(send_packet(&cmd
));
1028 execute(resend_packet(&cmd
));
1030 if (cc
== PROTOCOL_BINARY_CMD_DECREMENT
)
1032 execute(recv_packet(&rsp
));
1033 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1034 verify(memcached_ntohll(rsp
.decr
.message
.body
.value
) == (uint64_t)ii
);
1037 execute(test_binary_noop());
1040 /* decr 0 should not wrap */
1041 execute(resend_packet(&cmd
));
1042 if (cc
== PROTOCOL_BINARY_CMD_DECREMENT
)
1044 execute(recv_packet(&rsp
));
1045 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1046 verify(memcached_ntohll(rsp
.decr
.message
.body
.value
) == 0);
1050 /* @todo get the value and verify! */
1054 /* @todo add incorrect cas */
1055 execute(test_binary_noop());
1059 static enum test_return
test_binary_decr(void)
1061 return test_binary_decr_impl("test_binary_decr",
1062 PROTOCOL_BINARY_CMD_DECREMENT
);
1065 static enum test_return
test_binary_decrq(void)
1067 return test_binary_decr_impl("test_binary_decrq",
1068 PROTOCOL_BINARY_CMD_DECREMENTQ
);
1071 static enum test_return
test_binary_version(void)
1075 raw_command(&cmd
, PROTOCOL_BINARY_CMD_VERSION
, NULL
, 0, NULL
, 0);
1077 execute(send_packet(&cmd
));
1078 execute(recv_packet(&rsp
));
1079 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_VERSION
,
1080 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1085 static enum test_return
test_binary_flush_impl(const char *key
, uint8_t cc
)
1090 for (int ii
= 0; ii
< 2; ++ii
)
1092 execute(binary_set_item(key
, key
));
1093 flush_command(&cmd
, cc
, 0, ii
== 0);
1094 execute(send_packet(&cmd
));
1096 if (cc
== PROTOCOL_BINARY_CMD_FLUSH
)
1098 execute(recv_packet(&rsp
));
1099 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1102 execute(test_binary_noop());
1104 raw_command(&cmd
, PROTOCOL_BINARY_CMD_GET
, key
, strlen(key
), NULL
, 0);
1105 execute(send_packet(&cmd
));
1106 execute(recv_packet(&rsp
));
1107 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_GET
,
1108 PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
));
1114 static enum test_return
test_binary_flush(void)
1116 return test_binary_flush_impl("test_binary_flush", PROTOCOL_BINARY_CMD_FLUSH
);
1119 static enum test_return
test_binary_flushq(void)
1121 return test_binary_flush_impl("test_binary_flushq", PROTOCOL_BINARY_CMD_FLUSHQ
);
1124 static enum test_return
test_binary_concat_impl(const char *key
, uint8_t cc
)
1130 if (cc
== PROTOCOL_BINARY_CMD_APPEND
|| cc
== PROTOCOL_BINARY_CMD_APPENDQ
)
1139 execute(binary_set_item(key
, value
));
1141 if (cc
== PROTOCOL_BINARY_CMD_APPEND
|| cc
== PROTOCOL_BINARY_CMD_APPENDQ
)
1150 raw_command(&cmd
, cc
, key
, strlen(key
), value
, strlen(value
));
1151 execute(send_packet(&cmd
));
1152 if (cc
== PROTOCOL_BINARY_CMD_APPEND
|| cc
== PROTOCOL_BINARY_CMD_PREPEND
)
1154 execute(recv_packet(&rsp
));
1155 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1159 execute(test_binary_noop());
1162 raw_command(&cmd
, PROTOCOL_BINARY_CMD_GET
, key
, strlen(key
), NULL
, 0);
1163 execute(send_packet(&cmd
));
1164 execute(recv_packet(&rsp
));
1165 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_GET
,
1166 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1167 verify(rsp
.plain
.message
.header
.response
.bodylen
- 4 == 11);
1168 verify(memcmp(rsp
.bytes
+ 28, "hello world", 11) == 0);
1173 static enum test_return
test_binary_append(void)
1175 return test_binary_concat_impl("test_binary_append", PROTOCOL_BINARY_CMD_APPEND
);
1178 static enum test_return
test_binary_prepend(void)
1180 return test_binary_concat_impl("test_binary_prepend", PROTOCOL_BINARY_CMD_PREPEND
);
1183 static enum test_return
test_binary_appendq(void)
1185 return test_binary_concat_impl("test_binary_appendq", PROTOCOL_BINARY_CMD_APPENDQ
);
1188 static enum test_return
test_binary_prependq(void)
1190 return test_binary_concat_impl("test_binary_prependq", PROTOCOL_BINARY_CMD_PREPENDQ
);
1193 static enum test_return
test_binary_stat(void)
1198 raw_command(&cmd
, PROTOCOL_BINARY_CMD_STAT
, NULL
, 0, NULL
, 0);
1199 execute(send_packet(&cmd
));
1203 execute(recv_packet(&rsp
));
1204 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_STAT
,
1205 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1206 } while (rsp
.plain
.message
.header
.response
.keylen
!= 0);
1211 static enum test_return
send_string(const char *cmd
)
1213 execute(retry_write(cmd
, strlen(cmd
)));
1217 static enum test_return
receive_line(char *buffer
, size_t size
)
1220 while (offset
< size
)
1222 execute(retry_read(buffer
+ offset
, 1));
1223 if (buffer
[offset
] == '\n')
1225 if (offset
+ 1 < size
)
1227 buffer
[offset
+ 1]= '\0';
1239 static enum test_return
receive_response(const char *msg
) {
1241 execute(receive_line(buffer
, sizeof(buffer
)));
1242 if (strcmp(msg
, buffer
) != 0) {
1243 fprintf(stderr
, "[%s]\n", buffer
);
1245 verify(strcmp(msg
, buffer
) == 0);
1249 static enum test_return
receive_error_response(void)
1252 execute(receive_line(buffer
, sizeof(buffer
)));
1253 verify(strncmp(buffer
, "ERROR", 5) == 0 ||
1254 strncmp(buffer
, "CLIENT_ERROR", 12) == 0 ||
1255 strncmp(buffer
, "SERVER_ERROR", 12) == 0);
1259 static enum test_return
test_ascii_quit(void)
1261 /* Verify that quit handles unknown options */
1262 execute(send_string("quit foo bar\r\n"));
1263 execute(receive_error_response());
1265 /* quit doesn't support noreply */
1266 execute(send_string("quit noreply\r\n"));
1267 execute(receive_error_response());
1269 /* Verify that quit works */
1270 execute(send_string("quit\r\n"));
1272 /* Socket should be closed now, read should return EXIT_SUCCESS */
1274 verify(timeout_io_op(sock
, POLLIN
, buffer
, sizeof(buffer
)) == 0);
1275 return TEST_PASS_RECONNECT
;
1279 static enum test_return
test_ascii_version(void)
1281 /* Verify that version command handles unknown options */
1282 execute(send_string("version foo bar\r\n"));
1283 execute(receive_error_response());
1285 /* version doesn't support noreply */
1286 execute(send_string("version noreply\r\n"));
1287 execute(receive_error_response());
1289 /* Verify that verify works */
1290 execute(send_string("version\r\n"));
1292 execute(receive_line(buffer
, sizeof(buffer
)));
1293 verify(strncmp(buffer
, "VERSION ", 8) == 0);
1298 static enum test_return
test_ascii_verbosity(void)
1300 /* This command does not adhere to the spec! */
1301 execute(send_string("verbosity foo bar my\r\n"));
1302 execute(receive_error_response());
1304 execute(send_string("verbosity noreply\r\n"));
1305 execute(receive_error_response());
1307 execute(send_string("verbosity 0 noreply\r\n"));
1308 execute(test_ascii_version());
1310 execute(send_string("verbosity\r\n"));
1311 execute(receive_error_response());
1313 execute(send_string("verbosity 1\r\n"));
1314 execute(receive_response("OK\r\n"));
1316 execute(send_string("verbosity 0\r\n"));
1317 execute(receive_response("OK\r\n"));
1324 static enum test_return
test_ascii_set_impl(const char* key
, bool noreply
)
1326 /* @todo add tests for bogus format! */
1328 snprintf(buffer
, sizeof(buffer
), "set %s 0 0 5%s\r\nvalue\r\n", key
, noreply
? " noreply" : "");
1329 execute(send_string(buffer
));
1333 execute(receive_response("STORED\r\n"));
1336 return test_ascii_version();
1339 static enum test_return
test_ascii_set(void)
1341 return test_ascii_set_impl("test_ascii_set", false);
1344 static enum test_return
test_ascii_set_noreply(void)
1346 return test_ascii_set_impl("test_ascii_set_noreply", true);
1349 static enum test_return
test_ascii_add_impl(const char* key
, bool noreply
)
1351 /* @todo add tests for bogus format! */
1353 snprintf(buffer
, sizeof(buffer
), "add %s 0 0 5%s\r\nvalue\r\n", key
, noreply
? " noreply" : "");
1354 execute(send_string(buffer
));
1358 execute(receive_response("STORED\r\n"));
1361 execute(send_string(buffer
));
1365 execute(receive_response("NOT_STORED\r\n"));
1368 return test_ascii_version();
1371 static enum test_return
test_ascii_add(void)
1373 return test_ascii_add_impl("test_ascii_add", false);
1376 static enum test_return
test_ascii_add_noreply(void)
1378 return test_ascii_add_impl("test_ascii_add_noreply", true);
1381 static enum test_return
ascii_get_unknown_value(char **key
, char **value
, ssize_t
*ndata
)
1385 execute(receive_line(buffer
, sizeof(buffer
)));
1386 verify(strncmp(buffer
, "VALUE ", 6) == 0);
1387 char *end
= strchr(buffer
+ 6, ' ');
1388 verify(end
!= NULL
);
1393 *key
= strdup(buffer
+ 6);
1394 verify(*key
!= NULL
);
1398 unsigned long val
= strtoul(ptr
, &end
, 10); /* flags */
1402 verify(end
!= NULL
);
1404 *ndata
= (ssize_t
)strtoul(end
, &end
, 10); /* size */
1407 verify(end
!= NULL
);
1408 while (end
and *end
!= '\n' and isspace(*end
))
1410 verify(end
and *end
== '\n');
1412 *value
= static_cast<char*>(malloc((size_t)*ndata
));
1413 verify(*value
!= NULL
);
1415 execute(retry_read(*value
, (size_t)*ndata
));
1417 execute(retry_read(buffer
, 2));
1418 verify(memcmp(buffer
, "\r\n", 2) == 0);
1423 static enum test_return
ascii_get_value(const char *key
, const char *value
)
1427 size_t datasize
= strlen(value
);
1429 verify(datasize
< sizeof(buffer
));
1430 execute(receive_line(buffer
, sizeof(buffer
)));
1431 verify(strncmp(buffer
, "VALUE ", 6) == 0);
1432 verify(strncmp(buffer
+ 6, key
, strlen(key
)) == 0);
1433 char *ptr
= buffer
+ 6 + strlen(key
) + 1;
1437 unsigned long val
= strtoul(ptr
, &end
, 10); /* flags */
1441 verify(end
!= NULL
);
1444 val
= strtoul(end
, &end
, 10); /* size */
1447 verify(val
== datasize
);
1448 verify(end
!= NULL
);
1449 while (end
and *end
!= '\n' and isspace(*end
))
1453 verify(end
and *end
== '\n');
1455 execute(retry_read(buffer
, datasize
));
1456 verify(memcmp(buffer
, value
, datasize
) == 0);
1458 execute(retry_read(buffer
, 2));
1459 verify(memcmp(buffer
, "\r\n", 2) == 0);
1464 static enum test_return
ascii_get_item(const char *key
, const char *value
,
1471 datasize
= strlen(value
);
1474 verify(datasize
< sizeof(buffer
));
1475 snprintf(buffer
, sizeof(buffer
), "get %s\r\n", key
);
1476 execute(send_string(buffer
));
1480 execute(ascii_get_value(key
, value
));
1483 execute(retry_read(buffer
, 5));
1484 verify(memcmp(buffer
, "END\r\n", 5) == 0);
1489 static enum test_return
ascii_gets_value(const char *key
, const char *value
,
1494 size_t datasize
= strlen(value
);
1496 verify(datasize
< sizeof(buffer
));
1497 execute(receive_line(buffer
, sizeof(buffer
)));
1498 verify(strncmp(buffer
, "VALUE ", 6) == 0);
1499 verify(strncmp(buffer
+ 6, key
, strlen(key
)) == 0);
1500 char *ptr
= buffer
+ 6 + strlen(key
) + 1;
1504 unsigned long val
= strtoul(ptr
, &end
, 10); /* flags */
1508 verify(end
!= NULL
);
1511 val
= strtoul(end
, &end
, 10); /* size */
1514 verify(val
== datasize
);
1515 verify(end
!= NULL
);
1518 *cas
= strtoul(end
, &end
, 10); /* cas */
1521 verify(val
== datasize
);
1522 verify(end
!= NULL
);
1524 while (end
and *end
!= '\n' and isspace(*end
))
1528 verify(end
and *end
== '\n');
1530 execute(retry_read(buffer
, datasize
));
1531 verify(memcmp(buffer
, value
, datasize
) == 0);
1533 execute(retry_read(buffer
, 2));
1534 verify(memcmp(buffer
, "\r\n", 2) == 0);
1539 static enum test_return
ascii_gets_item(const char *key
, const char *value
,
1540 bool exist
, unsigned long *cas
)
1546 datasize
= strlen(value
);
1549 verify(datasize
< sizeof(buffer
));
1550 snprintf(buffer
, sizeof(buffer
), "gets %s\r\n", key
);
1551 execute(send_string(buffer
));
1554 execute(ascii_gets_value(key
, value
, cas
));
1556 execute(retry_read(buffer
, 5));
1557 verify(memcmp(buffer
, "END\r\n", 5) == 0);
1562 static enum test_return
ascii_set_item(const char *key
, const char *value
)
1565 size_t len
= strlen(value
);
1566 snprintf(buffer
, sizeof(buffer
), "set %s 0 0 %u\r\n", key
, (unsigned int)len
);
1567 execute(send_string(buffer
));
1568 execute(retry_write(value
, len
));
1569 execute(send_string("\r\n"));
1570 execute(receive_response("STORED\r\n"));
1574 static enum test_return
test_ascii_replace_impl(const char* key
, bool noreply
)
1577 snprintf(buffer
, sizeof(buffer
), "replace %s 0 0 5%s\r\nvalue\r\n", key
, noreply
? " noreply" : "");
1578 execute(send_string(buffer
));
1582 execute(test_ascii_version());
1586 execute(receive_response("NOT_STORED\r\n"));
1589 execute(ascii_set_item(key
, "value"));
1590 execute(ascii_get_item(key
, "value", true));
1593 execute(send_string(buffer
));
1596 execute(test_ascii_version());
1598 execute(receive_response("STORED\r\n"));
1600 return test_ascii_version();
1603 static enum test_return
test_ascii_replace(void)
1605 return test_ascii_replace_impl("test_ascii_replace", false);
1608 static enum test_return
test_ascii_replace_noreply(void)
1610 return test_ascii_replace_impl("test_ascii_replace_noreply", true);
1613 static enum test_return
test_ascii_cas_impl(const char* key
, bool noreply
)
1618 execute(ascii_set_item(key
, "value"));
1619 execute(ascii_gets_item(key
, "value", true, &cas
));
1621 snprintf(buffer
, sizeof(buffer
), "cas %s 0 0 6 %lu%s\r\nvalue2\r\n", key
, cas
, noreply
? " noreply" : "");
1622 execute(send_string(buffer
));
1626 execute(test_ascii_version());
1630 execute(receive_response("STORED\r\n"));
1633 /* reexecute the same command should fail due to illegal cas */
1634 execute(send_string(buffer
));
1638 execute(test_ascii_version());
1642 execute(receive_response("EXISTS\r\n"));
1645 return test_ascii_version();
1648 static enum test_return
test_ascii_cas(void)
1650 return test_ascii_cas_impl("test_ascii_cas", false);
1653 static enum test_return
test_ascii_cas_noreply(void)
1655 return test_ascii_cas_impl("test_ascii_cas_noreply", true);
1658 static enum test_return
test_ascii_delete_impl(const char *key
, bool noreply
)
1660 execute(ascii_set_item(key
, "value"));
1662 execute(send_string("delete\r\n"));
1663 execute(receive_error_response());
1664 /* BUG: the server accepts delete a b */
1665 execute(send_string("delete a b c d e\r\n"));
1666 execute(receive_error_response());
1669 snprintf(buffer
, sizeof(buffer
), "delete %s%s\r\n", key
, noreply
? " noreply" : "");
1670 execute(send_string(buffer
));
1673 execute(test_ascii_version());
1675 execute(receive_response("DELETED\r\n"));
1677 execute(ascii_get_item(key
, "value", false));
1678 execute(send_string(buffer
));
1680 execute(test_ascii_version());
1682 execute(receive_response("NOT_FOUND\r\n"));
1687 static enum test_return
test_ascii_delete(void)
1689 return test_ascii_delete_impl("test_ascii_delete", false);
1692 static enum test_return
test_ascii_delete_noreply(void)
1694 return test_ascii_delete_impl("test_ascii_delete_noreply", true);
1697 static enum test_return
test_ascii_get(void)
1699 execute(ascii_set_item("test_ascii_get", "value"));
1701 execute(send_string("get\r\n"));
1702 execute(receive_error_response());
1703 execute(ascii_get_item("test_ascii_get", "value", true));
1704 execute(ascii_get_item("test_ascii_get_notfound", "value", false));
1709 static enum test_return
test_ascii_gets(void)
1711 execute(ascii_set_item("test_ascii_gets", "value"));
1713 execute(send_string("gets\r\n"));
1714 execute(receive_error_response());
1716 execute(ascii_gets_item("test_ascii_gets", "value", true, &cas
));
1717 execute(ascii_gets_item("test_ascii_gets_notfound", "value", false, &cas
));
1722 static enum test_return
test_ascii_mget(void)
1724 const uint32_t nkeys
= 5;
1725 const char * const keys
[]= {
1728 /* test_ascii_mget_3 does not exist :) */
1734 for (uint32_t x
= 0; x
< nkeys
; ++x
)
1736 execute(ascii_set_item(keys
[x
], "value"));
1739 /* Ask for a key that doesn't exist as well */
1740 execute(send_string("get test_ascii_mget1 test_ascii_mget2 test_ascii_mget3 "
1741 "test_ascii_mget4 test_ascii_mget5 "
1742 "test_ascii_mget6\r\n"));
1744 std::vector
<char *> returned
;
1745 returned
.resize(nkeys
);
1747 for (uint32_t x
= 0; x
< nkeys
; ++x
)
1751 execute(ascii_get_unknown_value(&returned
[x
], &v
, &nbytes
));
1752 verify(nbytes
== 5);
1753 verify(memcmp(v
, "value", 5) == 0);
1758 execute(retry_read(buffer
, 5));
1759 verify(memcmp(buffer
, "END\r\n", 5) == 0);
1761 /* verify that we got all the keys we expected */
1762 for (uint32_t x
= 0; x
< nkeys
; ++x
)
1765 for (uint32_t y
= 0; y
< nkeys
; ++y
)
1767 if (strcmp(keys
[x
], returned
[y
]) == 0)
1776 for (uint32_t x
= 0; x
< nkeys
; ++x
)
1784 static enum test_return
test_ascii_incr_impl(const char* key
, bool noreply
)
1787 snprintf(cmd
, sizeof(cmd
), "incr %s 1%s\r\n", key
, noreply
? " noreply" : "");
1789 execute(ascii_set_item(key
, "0"));
1790 for (int x
= 1; x
< 11; ++x
)
1792 execute(send_string(cmd
));
1795 execute(test_ascii_version());
1799 execute(receive_line(buffer
, sizeof(buffer
)));
1800 int val
= atoi(buffer
);
1805 execute(ascii_get_item(key
, "10", true));
1810 static enum test_return
test_ascii_incr(void)
1812 return test_ascii_incr_impl("test_ascii_incr", false);
1815 static enum test_return
test_ascii_incr_noreply(void)
1817 return test_ascii_incr_impl("test_ascii_incr_noreply", true);
1820 static enum test_return
test_ascii_decr_impl(const char* key
, bool noreply
)
1823 snprintf(cmd
, sizeof(cmd
), "decr %s 1%s\r\n", key
, noreply
? " noreply" : "");
1825 execute(ascii_set_item(key
, "9"));
1826 for (int x
= 8; x
> -1; --x
)
1828 execute(send_string(cmd
));
1832 execute(test_ascii_version());
1837 execute(receive_line(buffer
, sizeof(buffer
)));
1838 int val
= atoi(buffer
);
1843 execute(ascii_get_item(key
, "0", true));
1845 /* verify that it doesn't wrap */
1846 execute(send_string(cmd
));
1849 execute(test_ascii_version());
1854 execute(receive_line(buffer
, sizeof(buffer
)));
1856 execute(ascii_get_item(key
, "0", true));
1861 static enum test_return
test_ascii_decr(void)
1863 return test_ascii_decr_impl("test_ascii_decr", false);
1866 static enum test_return
test_ascii_decr_noreply(void)
1868 return test_ascii_decr_impl("test_ascii_decr_noreply", true);
1872 static enum test_return
test_ascii_flush_impl(const char *key
, bool noreply
)
1875 /* Verify that the flush_all command handles unknown options */
1876 /* Bug in the current memcached server! */
1877 execute(send_string("flush_all foo bar\r\n"));
1878 execute(receive_error_response());
1881 execute(ascii_set_item(key
, key
));
1882 execute(ascii_get_item(key
, key
, true));
1886 execute(send_string("flush_all noreply\r\n"));
1887 execute(test_ascii_version());
1891 execute(send_string("flush_all\r\n"));
1892 execute(receive_response("OK\r\n"));
1895 execute(ascii_get_item(key
, key
, false));
1900 static enum test_return
test_ascii_flush(void)
1902 return test_ascii_flush_impl("test_ascii_flush", false);
1905 static enum test_return
test_ascii_flush_noreply(void)
1907 return test_ascii_flush_impl("test_ascii_flush_noreply", true);
1910 static enum test_return
test_ascii_concat_impl(const char *key
,
1921 execute(ascii_set_item(key
, value
));
1933 snprintf(cmd
, sizeof(cmd
), "%s %s 0 0 %u%s\r\n%s\r\n",
1934 append
? "append" : "prepend",
1935 key
, (unsigned int)strlen(value
), noreply
? " noreply" : "",
1937 execute(send_string(cmd
));
1941 execute(test_ascii_version());
1945 execute(receive_response("STORED\r\n"));
1948 execute(ascii_get_item(key
, "hello world", true));
1950 snprintf(cmd
, sizeof(cmd
), "%s %s_notfound 0 0 %u%s\r\n%s\r\n",
1951 append
? "append" : "prepend",
1952 key
, (unsigned int)strlen(value
), noreply
? " noreply" : "",
1954 execute(send_string(cmd
));
1958 execute(test_ascii_version());
1962 execute(receive_response("NOT_STORED\r\n"));
1968 static enum test_return
test_ascii_append(void)
1970 return test_ascii_concat_impl("test_ascii_append", true, false);
1973 static enum test_return
test_ascii_prepend(void)
1975 return test_ascii_concat_impl("test_ascii_prepend", false, false);
1978 static enum test_return
test_ascii_append_noreply(void)
1980 return test_ascii_concat_impl("test_ascii_append_noreply", true, true);
1983 static enum test_return
test_ascii_prepend_noreply(void)
1985 return test_ascii_concat_impl("test_ascii_prepend_noreply", false, true);
1988 static enum test_return
test_ascii_stat(void)
1990 execute(send_string("stats noreply\r\n"));
1991 execute(receive_error_response());
1992 execute(send_string("stats\r\n"));
1995 execute(receive_line(buffer
, sizeof(buffer
)));
1996 } while (strcmp(buffer
, "END\r\n") != 0);
1998 return TEST_PASS_RECONNECT
;
2001 typedef enum test_return(*TEST_FUNC
)(void);
2005 const char *description
;
2009 struct testcase testcases
[]= {
2010 { "ascii quit", test_ascii_quit
},
2011 { "ascii version", test_ascii_version
},
2012 { "ascii verbosity", test_ascii_verbosity
},
2013 { "ascii set", test_ascii_set
},
2014 { "ascii set noreply", test_ascii_set_noreply
},
2015 { "ascii get", test_ascii_get
},
2016 { "ascii gets", test_ascii_gets
},
2017 { "ascii mget", test_ascii_mget
},
2018 { "ascii flush", test_ascii_flush
},
2019 { "ascii flush noreply", test_ascii_flush_noreply
},
2020 { "ascii add", test_ascii_add
},
2021 { "ascii add noreply", test_ascii_add_noreply
},
2022 { "ascii replace", test_ascii_replace
},
2023 { "ascii replace noreply", test_ascii_replace_noreply
},
2024 { "ascii cas", test_ascii_cas
},
2025 { "ascii cas noreply", test_ascii_cas_noreply
},
2026 { "ascii delete", test_ascii_delete
},
2027 { "ascii delete noreply", test_ascii_delete_noreply
},
2028 { "ascii incr", test_ascii_incr
},
2029 { "ascii incr noreply", test_ascii_incr_noreply
},
2030 { "ascii decr", test_ascii_decr
},
2031 { "ascii decr noreply", test_ascii_decr_noreply
},
2032 { "ascii append", test_ascii_append
},
2033 { "ascii append noreply", test_ascii_append_noreply
},
2034 { "ascii prepend", test_ascii_prepend
},
2035 { "ascii prepend noreply", test_ascii_prepend_noreply
},
2036 { "ascii stat", test_ascii_stat
},
2037 { "binary noop", test_binary_noop
},
2038 { "binary quit", test_binary_quit
},
2039 { "binary quitq", test_binary_quitq
},
2040 { "binary set", test_binary_set
},
2041 { "binary setq", test_binary_setq
},
2042 { "binary flush", test_binary_flush
},
2043 { "binary flushq", test_binary_flushq
},
2044 { "binary add", test_binary_add
},
2045 { "binary addq", test_binary_addq
},
2046 { "binary replace", test_binary_replace
},
2047 { "binary replaceq", test_binary_replaceq
},
2048 { "binary delete", test_binary_delete
},
2049 { "binary deleteq", test_binary_deleteq
},
2050 { "binary get", test_binary_get
},
2051 { "binary getq", test_binary_getq
},
2052 { "binary getk", test_binary_getk
},
2053 { "binary getkq", test_binary_getkq
},
2054 { "binary incr", test_binary_incr
},
2055 { "binary incrq", test_binary_incrq
},
2056 { "binary decr", test_binary_decr
},
2057 { "binary decrq", test_binary_decrq
},
2058 { "binary version", test_binary_version
},
2059 { "binary append", test_binary_append
},
2060 { "binary appendq", test_binary_appendq
},
2061 { "binary prepend", test_binary_prepend
},
2062 { "binary prependq", test_binary_prependq
},
2063 { "binary stat", test_binary_stat
},
2067 const int ascii_tests
= 1;
2068 const int binary_tests
= 2;
2076 int main(int argc
, char **argv
)
2078 static const char * const status_msg
[]= {"[skip]", "[pass]", "[pass]", "[FAIL]"};
2079 struct test_type_st tests
= { true, true };
2082 const char *hostname
= NULL
;
2083 const char *port
= MEMCACHED_DEFAULT_PORT_STRING
;
2086 const char *testname
= NULL
;
2090 while ((cmd
= getopt(argc
, argv
, "qt:vch:p:PT:?ab")) != EOF
)
2095 tests
.binary
= false;
2104 timeout
= atoi(optarg
);
2107 fprintf(stderr
, "Invalid timeout. Please specify a number for -t\n");
2108 return EXIT_FAILURE
;
2112 case 'v': verbose
= true;
2115 case 'c': do_core
= true;
2118 case 'h': hostname
= optarg
;
2121 case 'p': port
= optarg
;
2128 case 'P': prompt
= true;
2131 case 'T': testname
= optarg
;
2135 fprintf(stderr
, "Usage: %s [-h hostname] [-p port] [-c] [-v] [-t n] [-P] [-T testname]'\n"
2136 "\t-c\tGenerate coredump if a test fails\n"
2137 "\t-v\tVerbose test output (print out the assertion)\n"
2138 "\t-t n\tSet the timeout for io-operations to n seconds\n"
2139 "\t-P\tPrompt the user before starting a test.\n"
2140 "\t\t\t\"skip\" will skip the test\n"
2141 "\t\t\t\"quit\" will terminate memcapable\n"
2142 "\t\t\tEverything else will start the test\n"
2143 "\t-T n\tJust run the test named n\n"
2144 "\t-a\tOnly test the ascii protocol\n"
2145 "\t-b\tOnly test the binary protocol\n",
2147 return EXIT_SUCCESS
;
2153 fprintf(stderr
, "No hostname was provided.\n");
2154 return EXIT_FAILURE
;
2157 initialize_sockets();
2158 sock
= connect_server(hostname
, port
);
2159 if (sock
== INVALID_SOCKET
)
2161 fprintf(stderr
, "Failed to connect to <%s:%s>: %s\n",
2162 hostname
, port
, strerror(get_socket_errno()));
2163 return EXIT_FAILURE
;
2166 for (int ii
= 0; testcases
[ii
].description
!= NULL
; ++ii
)
2168 if (testname
!= NULL
&& strcmp(testcases
[ii
].description
, testname
) != 0)
2173 if ((testcases
[ii
].description
[0] == 'a' && (tests
.ascii
) == 0) ||
2174 (testcases
[ii
].description
[0] == 'b' && (tests
.binary
) == 0))
2179 fprintf(stdout
, "%-40s", testcases
[ii
].description
);
2184 fprintf(stdout
, "\nPress <return> when you are ready? ");
2185 char buffer
[80] = {0};
2186 if (fgets(buffer
, sizeof(buffer
), stdin
) != NULL
) {
2187 if (strncmp(buffer
, "skip", 4) == 0)
2189 fprintf(stdout
, "%-40s%s\n", testcases
[ii
].description
,
2190 status_msg
[TEST_SKIP
]);
2194 if (strncmp(buffer
, "quit", 4) == 0)
2200 fprintf(stdout
, "%-40s", testcases
[ii
].description
);
2204 bool reconnect
= false;
2205 enum test_return ret
= testcases
[ii
].function();
2206 if (ret
== TEST_FAIL
)
2212 fprintf(stderr
, "\n");
2215 else if (ret
== TEST_PASS_RECONNECT
)
2220 if (ret
== TEST_FAIL
)
2222 fprintf(stderr
, "%s\n", status_msg
[ret
]);
2226 fprintf(stdout
, "%s\n", status_msg
[ret
]);
2232 if ((sock
= connect_server(hostname
, port
)) == INVALID_SOCKET
)
2234 fprintf(stderr
, "Failed to connect to <%s:%s>: %s\n", hostname
, port
, strerror(get_socket_errno()));
2235 fprintf(stderr
, "%d of %d tests failed\n", failed
, total
);
2236 return EXIT_FAILURE
;
2244 fprintf(stdout
, "All tests passed\n");
2248 fprintf(stderr
, "%d of %d tests failed\n", failed
, total
);
2251 return (failed
== 0) ? EXIT_SUCCESS
: EXIT_FAILURE
;