2 * Copyright (C) 2006-2009 Brian Aker
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
12 /* -*- Mode: C; tab-width: 2; c-basic-offset: 2; indent-tabs-mode: nil -*- */
17 #include <sys/types.h>
30 #include <libmemcached/memcached.h>
31 #include <libmemcached/memcached/protocol_binary.h>
32 #include <libmemcached/byteorder.h>
33 #include "utilities.h"
36 /* /usr/include/netinet/in.h defines macros from ntohs() to _bswap_nn to
37 * optimize the conversion functions, but the prototypes generate warnings
38 * from gcc. The conversion methods isn't the bottleneck for my app, so
39 * just remove the warnings by undef'ing the optimization ..
45 /* Should we generate coredumps when we enounter an error (-c) */
46 static bool do_core
= false;
47 /* connection to the server */
48 static memcached_socket_t sock
;
49 /* Should the output from test failures be verbose or quiet? */
50 static bool verbose
= false;
52 /* The number of seconds to wait for an IO-operation */
53 static int timeout
= 2;
56 * Instead of having to cast between the different datatypes we create
57 * a union of all of the different types of pacages we want to send.
58 * A lot of the different commands use the same packet layout, so I'll
59 * just define the different types I need. The typedefs only contain
60 * the header of the message, so we need some space for keys and body
61 * To avoid to have to do multiple writes, lets add a chunk of memory
62 * to use. 1k should be more than enough for header, key and body.
66 protocol_binary_request_no_extras plain
;
67 protocol_binary_request_flush flush
;
68 protocol_binary_request_incr incr
;
69 protocol_binary_request_set set
;
75 protocol_binary_response_no_extras plain
;
76 protocol_binary_response_incr incr
;
77 protocol_binary_response_decr decr
;
83 TEST_SKIP
, TEST_PASS
, TEST_PASS_RECONNECT
, TEST_FAIL
87 * Try to get an addrinfo struct for a given port on a given host
89 static struct addrinfo
*lookuphost(const char *hostname
, const char *port
)
91 struct addrinfo
*ai
= 0;
92 struct addrinfo hints
;
93 memset(&hints
, 0, sizeof(struct addrinfo
));
94 hints
.ai_family
=AF_UNSPEC
;
95 hints
.ai_protocol
=IPPROTO_TCP
;
96 hints
.ai_socktype
=SOCK_STREAM
;
98 int error
= getaddrinfo(hostname
, port
, &hints
, &ai
);
101 if (error
!= EAI_SYSTEM
)
102 fprintf(stderr
, "getaddrinfo(): %s\n", gai_strerror(error
));
104 perror("getaddrinfo()");
111 * Set the socket in nonblocking mode
112 * @return -1 if failure, the socket otherwise
114 static memcached_socket_t
set_noblock(void)
118 if (ioctlsocket(sock
, FIONBIO
, &arg
) == SOCKET_ERROR
)
120 perror("Failed to set nonblocking io");
122 return INVALID_SOCKET
;
125 int flags
= fcntl(sock
, F_GETFL
, 0);
128 perror("Failed to get socket flags");
130 return INVALID_SOCKET
;
133 if ((flags
& O_NONBLOCK
) != O_NONBLOCK
)
135 if (fcntl(sock
, F_SETFL
, flags
| O_NONBLOCK
) == -1)
137 perror("Failed to set socket to nonblocking mode");
139 return INVALID_SOCKET
;
147 * Try to open a connection to the server
148 * @param hostname the name of the server to connect to
149 * @param port the port number (or service) to connect to
150 * @return positive integer if success, -1 otherwise
152 static memcached_socket_t
connect_server(const char *hostname
, const char *port
)
154 struct addrinfo
*ai
= lookuphost(hostname
, port
);
155 sock
= INVALID_SOCKET
;
158 if ((sock
= socket(ai
->ai_family
, ai
->ai_socktype
,
159 ai
->ai_protocol
)) != INVALID_SOCKET
)
161 if (connect(sock
, ai
->ai_addr
, ai
->ai_addrlen
) == SOCKET_ERROR
)
163 fprintf(stderr
, "Failed to connect socket: %s\n",
164 strerror(get_socket_errno()));
166 sock
= INVALID_SOCKET
;
174 fprintf(stderr
, "Failed to create socket: %s\n",
175 strerror(get_socket_errno()));
183 static ssize_t
timeout_io_op(memcached_socket_t fd
, short direction
, void *buf
, size_t len
)
187 if (direction
== POLLOUT
)
189 ret
= send(fd
, buf
, len
, 0);
193 ret
= recv(fd
, buf
, len
, 0);
196 if (ret
== SOCKET_ERROR
&& get_socket_errno() == EWOULDBLOCK
)
199 memset(&fds
, 0, sizeof(struct pollfd
));
200 fds
.events
= direction
;
203 int err
= poll(&fds
, 1, timeout
* 1000);
206 if (direction
== POLLOUT
)
208 ret
= send(fd
, buf
, len
, 0);
212 ret
= recv(fd
, buf
, len
, 0);
221 perror("Failed to poll");
230 * Ensure that an expression is true. If it isn't print out a message similar
231 * to assert() and create a coredump if the user wants that. If not an error
232 * message is returned.
235 static enum test_return
ensure(bool val
, const char *expression
, const char *file
, int line
)
240 fprintf(stderr
, "\n%s:%d: %s", file
, line
, expression
);
251 #define verify(expression) do { if (ensure(expression, #expression, __FILE__, __LINE__) == TEST_FAIL) return TEST_FAIL; } while (0)
252 #define execute(expression) do { if (ensure(expression == TEST_PASS, #expression, __FILE__, __LINE__) == TEST_FAIL) return TEST_FAIL; } while (0)
255 * Send a chunk of memory over the socket (retry if the call is iterrupted
257 static enum test_return
retry_write(const void* buf
, size_t len
)
260 const char* ptr
= static_cast<const char *>(buf
);
264 size_t num_bytes
= len
- offset
;
265 ssize_t nw
= timeout_io_op(sock
, POLLOUT
, (void*)(ptr
+ offset
), num_bytes
);
267 verify(get_socket_errno() == EINTR
|| get_socket_errno() == EAGAIN
);
270 } while (offset
< len
);
276 * Resend a packet to the server (All fields in the command header should
277 * be in network byte order)
279 static enum test_return
resend_packet(command
*cmd
)
281 size_t length
= sizeof (protocol_binary_request_no_extras
) +
282 ntohl(cmd
->plain
.message
.header
.request
.bodylen
);
284 execute(retry_write(cmd
, length
));
289 * Send a command to the server. The command header needs to be updated
290 * to network byte order
292 static enum test_return
send_packet(command
*cmd
)
294 /* Fix the byteorder of the header */
295 cmd
->plain
.message
.header
.request
.keylen
=
296 ntohs(cmd
->plain
.message
.header
.request
.keylen
);
297 cmd
->plain
.message
.header
.request
.bodylen
=
298 ntohl(cmd
->plain
.message
.header
.request
.bodylen
);
299 cmd
->plain
.message
.header
.request
.cas
=
300 memcached_ntohll(cmd
->plain
.message
.header
.request
.cas
);
302 execute(resend_packet(cmd
));
307 * Read a fixed length chunk of data from the server
309 static enum test_return
retry_read(void *buf
, size_t len
)
314 ssize_t nr
= timeout_io_op(sock
, POLLIN
, ((char*) buf
) + offset
, len
- offset
);
317 fprintf(stderr
, "Errno: %d %s\n", get_socket_errno(), strerror(errno
));
318 verify(get_socket_errno() == EINTR
|| get_socket_errno() == EAGAIN
);
325 } while (offset
< len
);
331 * Receive a response from the server and conver the fields in the header
332 * to local byte order
334 static enum test_return
recv_packet(response
*rsp
)
336 execute(retry_read(rsp
, sizeof(protocol_binary_response_no_extras
)));
338 /* Fix the byte order in the packet header */
339 rsp
->plain
.message
.header
.response
.keylen
=
340 ntohs(rsp
->plain
.message
.header
.response
.keylen
);
341 rsp
->plain
.message
.header
.response
.status
=
342 ntohs(rsp
->plain
.message
.header
.response
.status
);
343 rsp
->plain
.message
.header
.response
.bodylen
=
344 ntohl(rsp
->plain
.message
.header
.response
.bodylen
);
345 rsp
->plain
.message
.header
.response
.cas
=
346 memcached_ntohll(rsp
->plain
.message
.header
.response
.cas
);
348 size_t bodysz
= rsp
->plain
.message
.header
.response
.bodylen
;
350 execute(retry_read(rsp
->bytes
+ sizeof (protocol_binary_response_no_extras
), bodysz
));
356 * Create a storage command (add, set, replace etc)
358 * @param cmd destination buffer
359 * @param cc the storage command to create
360 * @param key the key to store
361 * @param keylen the length of the key
362 * @param dta the data to store with the key
363 * @param dtalen the length of the data to store with the key
364 * @param flags the flags to store along with the key
365 * @param exptime the expiry time for the key
367 static void storage_command(command
*cmd
,
376 /* all of the storage commands use the same command layout */
377 protocol_binary_request_set
*request
= &cmd
->set
;
379 memset(request
, 0, sizeof (*request
));
380 request
->message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
381 request
->message
.header
.request
.opcode
= cc
;
382 request
->message
.header
.request
.keylen
= (uint16_t)keylen
;
383 request
->message
.header
.request
.extlen
= 8;
384 request
->message
.header
.request
.bodylen
= (uint32_t)(keylen
+ 8 + dtalen
);
385 request
->message
.header
.request
.opaque
= 0xdeadbeef;
386 request
->message
.body
.flags
= flags
;
387 request
->message
.body
.expiration
= exptime
;
389 off_t key_offset
= sizeof (protocol_binary_request_no_extras
) + 8;
390 memcpy(cmd
->bytes
+ key_offset
, key
, keylen
);
392 memcpy(cmd
->bytes
+ key_offset
+ keylen
, dta
, dtalen
);
396 * Create a basic command to send to the server
397 * @param cmd destination buffer
398 * @param cc the command to create
399 * @param key the key to store
400 * @param keylen the length of the key
401 * @param dta the data to store with the key
402 * @param dtalen the length of the data to store with the key
404 static void raw_command(command
*cmd
,
411 /* all of the storage commands use the same command layout */
412 memset(cmd
, 0, sizeof (*cmd
));
413 cmd
->plain
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
414 cmd
->plain
.message
.header
.request
.opcode
= cc
;
415 cmd
->plain
.message
.header
.request
.keylen
= (uint16_t)keylen
;
416 cmd
->plain
.message
.header
.request
.bodylen
= (uint32_t)(keylen
+ dtalen
);
417 cmd
->plain
.message
.header
.request
.opaque
= 0xdeadbeef;
419 off_t key_offset
= sizeof (protocol_binary_request_no_extras
);
422 memcpy(cmd
->bytes
+ key_offset
, key
, keylen
);
425 memcpy(cmd
->bytes
+ key_offset
+ keylen
, dta
, dtalen
);
429 * Create the flush command
430 * @param cmd destination buffer
431 * @param cc the command to create (FLUSH/FLUSHQ)
432 * @param exptime when to flush
433 * @param use_extra to force using of the extra field?
435 static void flush_command(command
*cmd
,
436 uint8_t cc
, uint32_t exptime
, bool use_extra
)
438 memset(cmd
, 0, sizeof (cmd
->flush
));
439 cmd
->flush
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
440 cmd
->flush
.message
.header
.request
.opcode
= cc
;
441 cmd
->flush
.message
.header
.request
.opaque
= 0xdeadbeef;
443 if (exptime
!= 0 || use_extra
)
445 cmd
->flush
.message
.header
.request
.extlen
= 4;
446 cmd
->flush
.message
.body
.expiration
= htonl(exptime
);
447 cmd
->flush
.message
.header
.request
.bodylen
= 4;
452 * Create a incr/decr command
453 * @param cc the cmd to create (FLUSH/FLUSHQ)
454 * @param key the key to operate on
455 * @param keylen the number of bytes in the key
456 * @param delta the number to add/subtract
457 * @param initial the initial value if the key doesn't exist
458 * @param exptime when the key should expire if it isn't set
460 static void arithmetic_command(command
*cmd
,
468 memset(cmd
, 0, sizeof (cmd
->incr
));
469 cmd
->incr
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
470 cmd
->incr
.message
.header
.request
.opcode
= cc
;
471 cmd
->incr
.message
.header
.request
.keylen
= (uint16_t)keylen
;
472 cmd
->incr
.message
.header
.request
.extlen
= 20;
473 cmd
->incr
.message
.header
.request
.bodylen
= (uint32_t)(keylen
+ 20);
474 cmd
->incr
.message
.header
.request
.opaque
= 0xdeadbeef;
475 cmd
->incr
.message
.body
.delta
= memcached_htonll(delta
);
476 cmd
->incr
.message
.body
.initial
= memcached_htonll(initial
);
477 cmd
->incr
.message
.body
.expiration
= htonl(exptime
);
479 off_t key_offset
= sizeof (protocol_binary_request_no_extras
) + 20;
480 memcpy(cmd
->bytes
+ key_offset
, key
, keylen
);
484 * Validate the response header from the server
485 * @param rsp the response to check
486 * @param cc the expected command
487 * @param status the expected status
489 static enum test_return
do_validate_response_header(response
*rsp
,
490 uint8_t cc
, uint16_t status
)
492 verify(rsp
->plain
.message
.header
.response
.magic
== PROTOCOL_BINARY_RES
);
493 verify(rsp
->plain
.message
.header
.response
.opcode
== cc
);
494 verify(rsp
->plain
.message
.header
.response
.datatype
== PROTOCOL_BINARY_RAW_BYTES
);
495 verify(rsp
->plain
.message
.header
.response
.status
== status
);
496 verify(rsp
->plain
.message
.header
.response
.opaque
== 0xdeadbeef);
498 if (status
== PROTOCOL_BINARY_RESPONSE_SUCCESS
)
501 case PROTOCOL_BINARY_CMD_ADDQ
:
502 case PROTOCOL_BINARY_CMD_APPENDQ
:
503 case PROTOCOL_BINARY_CMD_DECREMENTQ
:
504 case PROTOCOL_BINARY_CMD_DELETEQ
:
505 case PROTOCOL_BINARY_CMD_FLUSHQ
:
506 case PROTOCOL_BINARY_CMD_INCREMENTQ
:
507 case PROTOCOL_BINARY_CMD_PREPENDQ
:
508 case PROTOCOL_BINARY_CMD_QUITQ
:
509 case PROTOCOL_BINARY_CMD_REPLACEQ
:
510 case PROTOCOL_BINARY_CMD_SETQ
:
511 verify("Quiet command shouldn't return on success" == NULL
);
517 case PROTOCOL_BINARY_CMD_ADD
:
518 case PROTOCOL_BINARY_CMD_REPLACE
:
519 case PROTOCOL_BINARY_CMD_SET
:
520 case PROTOCOL_BINARY_CMD_APPEND
:
521 case PROTOCOL_BINARY_CMD_PREPEND
:
522 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
523 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
524 verify(rsp
->plain
.message
.header
.response
.bodylen
== 0);
525 verify(rsp
->plain
.message
.header
.response
.cas
!= 0);
527 case PROTOCOL_BINARY_CMD_FLUSH
:
528 case PROTOCOL_BINARY_CMD_NOOP
:
529 case PROTOCOL_BINARY_CMD_QUIT
:
530 case PROTOCOL_BINARY_CMD_DELETE
:
531 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
532 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
533 verify(rsp
->plain
.message
.header
.response
.bodylen
== 0);
534 verify(rsp
->plain
.message
.header
.response
.cas
== 0);
537 case PROTOCOL_BINARY_CMD_DECREMENT
:
538 case PROTOCOL_BINARY_CMD_INCREMENT
:
539 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
540 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
541 verify(rsp
->plain
.message
.header
.response
.bodylen
== 8);
542 verify(rsp
->plain
.message
.header
.response
.cas
!= 0);
545 case PROTOCOL_BINARY_CMD_STAT
:
546 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
547 /* key and value exists in all packets except in the terminating */
548 verify(rsp
->plain
.message
.header
.response
.cas
== 0);
551 case PROTOCOL_BINARY_CMD_VERSION
:
552 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
553 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
554 verify(rsp
->plain
.message
.header
.response
.bodylen
!= 0);
555 verify(rsp
->plain
.message
.header
.response
.cas
== 0);
558 case PROTOCOL_BINARY_CMD_GET
:
559 case PROTOCOL_BINARY_CMD_GETQ
:
560 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
561 verify(rsp
->plain
.message
.header
.response
.extlen
== 4);
562 verify(rsp
->plain
.message
.header
.response
.cas
!= 0);
565 case PROTOCOL_BINARY_CMD_GETK
:
566 case PROTOCOL_BINARY_CMD_GETKQ
:
567 verify(rsp
->plain
.message
.header
.response
.keylen
!= 0);
568 verify(rsp
->plain
.message
.header
.response
.extlen
== 4);
569 verify(rsp
->plain
.message
.header
.response
.cas
!= 0);
573 /* Undefined command code */
579 verify(rsp
->plain
.message
.header
.response
.cas
== 0);
580 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
581 if (cc
!= PROTOCOL_BINARY_CMD_GETK
)
583 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
590 /* We call verify(validate_response_header), but that macro
591 * expects a boolean expression, and the function returns
592 * an enum.... Let's just create a macro to avoid cluttering
593 * the code with all of the == TEST_PASS ;-)
595 #define validate_response_header(a,b,c) \
596 do_validate_response_header(a,b,c) == TEST_PASS
599 static enum test_return
send_binary_noop(void)
602 raw_command(&cmd
, PROTOCOL_BINARY_CMD_NOOP
, NULL
, 0, NULL
, 0);
603 execute(send_packet(&cmd
));
607 static enum test_return
receive_binary_noop(void)
610 execute(recv_packet(&rsp
));
611 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_NOOP
,
612 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
616 static enum test_return
test_binary_noop(void)
618 execute(send_binary_noop());
619 execute(receive_binary_noop());
623 static enum test_return
test_binary_quit_impl(uint8_t cc
)
627 raw_command(&cmd
, cc
, NULL
, 0, NULL
, 0);
629 execute(send_packet(&cmd
));
630 if (cc
== PROTOCOL_BINARY_CMD_QUIT
)
632 execute(recv_packet(&rsp
));
633 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_QUIT
,
634 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
637 /* Socket should be closed now, read should return EXIT_SUCCESS */
638 verify(timeout_io_op(sock
, POLLIN
, rsp
.bytes
, sizeof(rsp
.bytes
)) == 0);
640 return TEST_PASS_RECONNECT
;
643 static enum test_return
test_binary_quit(void)
645 return test_binary_quit_impl(PROTOCOL_BINARY_CMD_QUIT
);
648 static enum test_return
test_binary_quitq(void)
650 return test_binary_quit_impl(PROTOCOL_BINARY_CMD_QUITQ
);
653 static enum test_return
test_binary_set_impl(const char* key
, uint8_t cc
)
658 uint64_t value
= 0xdeadbeefdeadcafeULL
;
659 storage_command(&cmd
, cc
, key
, strlen(key
), &value
, sizeof (value
), 0, 0);
661 /* set should always work */
662 for (int ii
= 0; ii
< 10; ii
++)
665 execute(send_packet(&cmd
));
667 execute(resend_packet(&cmd
));
669 if (cc
== PROTOCOL_BINARY_CMD_SET
)
671 execute(recv_packet(&rsp
));
672 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
675 execute(test_binary_noop());
679 * We need to get the current CAS id, and at this time we haven't
680 * verified that we have a working get
682 if (cc
== PROTOCOL_BINARY_CMD_SETQ
)
684 cmd
.set
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_SET
;
685 execute(resend_packet(&cmd
));
686 execute(recv_packet(&rsp
));
687 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_SET
,
688 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
689 cmd
.set
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_SETQ
;
692 /* try to set with the correct CAS value */
693 cmd
.plain
.message
.header
.request
.cas
= memcached_htonll(rsp
.plain
.message
.header
.response
.cas
);
694 execute(resend_packet(&cmd
));
695 if (cc
== PROTOCOL_BINARY_CMD_SET
)
697 execute(recv_packet(&rsp
));
698 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
701 execute(test_binary_noop());
703 /* try to set with an incorrect CAS value */
704 cmd
.plain
.message
.header
.request
.cas
= memcached_htonll(rsp
.plain
.message
.header
.response
.cas
- 1);
705 execute(resend_packet(&cmd
));
706 execute(send_binary_noop());
707 execute(recv_packet(&rsp
));
708 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS
));
709 execute(receive_binary_noop());
714 static enum test_return
test_binary_set(void)
716 return test_binary_set_impl("test_binary_set", PROTOCOL_BINARY_CMD_SET
);
719 static enum test_return
test_binary_setq(void)
721 return test_binary_set_impl("test_binary_setq", PROTOCOL_BINARY_CMD_SETQ
);
724 static enum test_return
test_binary_add_impl(const char* key
, uint8_t cc
)
728 uint64_t value
= 0xdeadbeefdeadcafeULL
;
729 storage_command(&cmd
, cc
, key
, strlen(key
), &value
, sizeof (value
), 0, 0);
731 /* first add should work, rest of them should fail (even with cas
733 for (int ii
=0; ii
< 10; ii
++)
736 execute(send_packet(&cmd
));
738 execute(resend_packet(&cmd
));
740 if (cc
== PROTOCOL_BINARY_CMD_ADD
|| ii
> 0)
742 uint16_t expected_result
;
744 expected_result
= PROTOCOL_BINARY_RESPONSE_SUCCESS
;
746 expected_result
= PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS
;
748 execute(send_binary_noop());
749 execute(recv_packet(&rsp
));
750 execute(receive_binary_noop());
751 verify(validate_response_header(&rsp
, cc
, expected_result
));
754 execute(test_binary_noop());
760 static enum test_return
test_binary_add(void)
762 return test_binary_add_impl("test_binary_add", PROTOCOL_BINARY_CMD_ADD
);
765 static enum test_return
test_binary_addq(void)
767 return test_binary_add_impl("test_binary_addq", PROTOCOL_BINARY_CMD_ADDQ
);
770 static enum test_return
binary_set_item(const char *key
, const char *value
)
774 storage_command(&cmd
, PROTOCOL_BINARY_CMD_SET
, key
, strlen(key
),
775 value
, strlen(value
), 0, 0);
776 execute(send_packet(&cmd
));
777 execute(recv_packet(&rsp
));
778 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_SET
,
779 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
783 static enum test_return
test_binary_replace_impl(const char* key
, uint8_t cc
)
787 uint64_t value
= 0xdeadbeefdeadcafeULL
;
788 storage_command(&cmd
, cc
, key
, strlen(key
), &value
, sizeof (value
), 0, 0);
790 /* first replace should fail, successive should succeed (when the
792 for (int ii
= 0; ii
< 10; ii
++)
795 execute(send_packet(&cmd
));
797 execute(resend_packet(&cmd
));
799 if (cc
== PROTOCOL_BINARY_CMD_REPLACE
|| ii
== 0)
801 uint16_t expected_result
;
803 expected_result
=PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
;
805 expected_result
=PROTOCOL_BINARY_RESPONSE_SUCCESS
;
807 execute(send_binary_noop());
808 execute(recv_packet(&rsp
));
809 execute(receive_binary_noop());
810 verify(validate_response_header(&rsp
, cc
, expected_result
));
813 execute(binary_set_item(key
, key
));
816 execute(test_binary_noop());
819 /* verify that replace with CAS value works! */
820 cmd
.plain
.message
.header
.request
.cas
= memcached_htonll(rsp
.plain
.message
.header
.response
.cas
);
821 execute(resend_packet(&cmd
));
823 if (cc
== PROTOCOL_BINARY_CMD_REPLACE
)
825 execute(recv_packet(&rsp
));
826 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
829 execute(test_binary_noop());
831 /* try to set with an incorrect CAS value */
832 cmd
.plain
.message
.header
.request
.cas
= memcached_htonll(rsp
.plain
.message
.header
.response
.cas
- 1);
833 execute(resend_packet(&cmd
));
834 execute(send_binary_noop());
835 execute(recv_packet(&rsp
));
836 execute(receive_binary_noop());
837 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS
));
842 static enum test_return
test_binary_replace(void)
844 return test_binary_replace_impl("test_binary_replace", PROTOCOL_BINARY_CMD_REPLACE
);
847 static enum test_return
test_binary_replaceq(void)
849 return test_binary_replace_impl("test_binary_replaceq", PROTOCOL_BINARY_CMD_REPLACEQ
);
852 static enum test_return
test_binary_delete_impl(const char *key
, uint8_t cc
)
856 raw_command(&cmd
, cc
, key
, strlen(key
), NULL
, 0);
858 /* The delete shouldn't work the first time, because the item isn't there */
859 execute(send_packet(&cmd
));
860 execute(send_binary_noop());
861 execute(recv_packet(&rsp
));
862 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
));
863 execute(receive_binary_noop());
864 execute(binary_set_item(key
, key
));
866 /* The item should be present now, resend*/
867 execute(resend_packet(&cmd
));
868 if (cc
== PROTOCOL_BINARY_CMD_DELETE
)
870 execute(recv_packet(&rsp
));
871 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
874 execute(test_binary_noop());
879 static enum test_return
test_binary_delete(void)
881 return test_binary_delete_impl("test_binary_delete", PROTOCOL_BINARY_CMD_DELETE
);
884 static enum test_return
test_binary_deleteq(void)
886 return test_binary_delete_impl("test_binary_deleteq", PROTOCOL_BINARY_CMD_DELETEQ
);
889 static enum test_return
test_binary_get_impl(const char *key
, uint8_t cc
)
894 raw_command(&cmd
, cc
, key
, strlen(key
), NULL
, 0);
895 execute(send_packet(&cmd
));
896 execute(send_binary_noop());
898 if (cc
== PROTOCOL_BINARY_CMD_GET
|| cc
== PROTOCOL_BINARY_CMD_GETK
)
900 execute(recv_packet(&rsp
));
901 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
));
904 execute(receive_binary_noop());
906 execute(binary_set_item(key
, key
));
907 execute(resend_packet(&cmd
));
908 execute(send_binary_noop());
910 execute(recv_packet(&rsp
));
911 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
912 execute(receive_binary_noop());
917 static enum test_return
test_binary_get(void)
919 return test_binary_get_impl("test_binary_get", PROTOCOL_BINARY_CMD_GET
);
922 static enum test_return
test_binary_getk(void)
924 return test_binary_get_impl("test_binary_getk", PROTOCOL_BINARY_CMD_GETK
);
927 static enum test_return
test_binary_getq(void)
929 return test_binary_get_impl("test_binary_getq", PROTOCOL_BINARY_CMD_GETQ
);
932 static enum test_return
test_binary_getkq(void)
934 return test_binary_get_impl("test_binary_getkq", PROTOCOL_BINARY_CMD_GETKQ
);
937 static enum test_return
test_binary_incr_impl(const char* key
, uint8_t cc
)
941 arithmetic_command(&cmd
, cc
, key
, strlen(key
), 1, 0, 0);
944 for (ii
= 0; ii
< 10; ++ii
)
947 execute(send_packet(&cmd
));
949 execute(resend_packet(&cmd
));
951 if (cc
== PROTOCOL_BINARY_CMD_INCREMENT
)
953 execute(recv_packet(&rsp
));
954 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
955 verify(memcached_ntohll(rsp
.incr
.message
.body
.value
) == ii
);
958 execute(test_binary_noop());
961 /* @todo add incorrect CAS */
965 static enum test_return
test_binary_incr(void)
967 return test_binary_incr_impl("test_binary_incr", PROTOCOL_BINARY_CMD_INCREMENT
);
970 static enum test_return
test_binary_incrq(void)
972 return test_binary_incr_impl("test_binary_incrq", PROTOCOL_BINARY_CMD_INCREMENTQ
);
975 static enum test_return
test_binary_decr_impl(const char* key
, uint8_t cc
)
979 arithmetic_command(&cmd
, cc
, key
, strlen(key
), 1, 9, 0);
982 for (ii
= 9; ii
> -1; --ii
)
985 execute(send_packet(&cmd
));
987 execute(resend_packet(&cmd
));
989 if (cc
== PROTOCOL_BINARY_CMD_DECREMENT
)
991 execute(recv_packet(&rsp
));
992 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
993 verify(memcached_ntohll(rsp
.decr
.message
.body
.value
) == (uint64_t)ii
);
996 execute(test_binary_noop());
999 /* decr 0 should not wrap */
1000 execute(resend_packet(&cmd
));
1001 if (cc
== PROTOCOL_BINARY_CMD_DECREMENT
)
1003 execute(recv_packet(&rsp
));
1004 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1005 verify(memcached_ntohll(rsp
.decr
.message
.body
.value
) == 0);
1009 /* @todo get the value and verify! */
1013 /* @todo add incorrect cas */
1014 execute(test_binary_noop());
1018 static enum test_return
test_binary_decr(void)
1020 return test_binary_decr_impl("test_binary_decr",
1021 PROTOCOL_BINARY_CMD_DECREMENT
);
1024 static enum test_return
test_binary_decrq(void)
1026 return test_binary_decr_impl("test_binary_decrq",
1027 PROTOCOL_BINARY_CMD_DECREMENTQ
);
1030 static enum test_return
test_binary_version(void)
1034 raw_command(&cmd
, PROTOCOL_BINARY_CMD_VERSION
, NULL
, 0, NULL
, 0);
1036 execute(send_packet(&cmd
));
1037 execute(recv_packet(&rsp
));
1038 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_VERSION
,
1039 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1044 static enum test_return
test_binary_flush_impl(const char *key
, uint8_t cc
)
1049 for (int ii
= 0; ii
< 2; ++ii
)
1051 execute(binary_set_item(key
, key
));
1052 flush_command(&cmd
, cc
, 0, ii
== 0);
1053 execute(send_packet(&cmd
));
1055 if (cc
== PROTOCOL_BINARY_CMD_FLUSH
)
1057 execute(recv_packet(&rsp
));
1058 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1061 execute(test_binary_noop());
1063 raw_command(&cmd
, PROTOCOL_BINARY_CMD_GET
, key
, strlen(key
), NULL
, 0);
1064 execute(send_packet(&cmd
));
1065 execute(recv_packet(&rsp
));
1066 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_GET
,
1067 PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
));
1073 static enum test_return
test_binary_flush(void)
1075 return test_binary_flush_impl("test_binary_flush", PROTOCOL_BINARY_CMD_FLUSH
);
1078 static enum test_return
test_binary_flushq(void)
1080 return test_binary_flush_impl("test_binary_flushq", PROTOCOL_BINARY_CMD_FLUSHQ
);
1083 static enum test_return
test_binary_concat_impl(const char *key
, uint8_t cc
)
1089 if (cc
== PROTOCOL_BINARY_CMD_APPEND
|| cc
== PROTOCOL_BINARY_CMD_APPENDQ
)
1094 execute(binary_set_item(key
, value
));
1096 if (cc
== PROTOCOL_BINARY_CMD_APPEND
|| cc
== PROTOCOL_BINARY_CMD_APPENDQ
)
1101 raw_command(&cmd
, cc
, key
, strlen(key
), value
, strlen(value
));
1102 execute(send_packet(&cmd
));
1103 if (cc
== PROTOCOL_BINARY_CMD_APPEND
|| cc
== PROTOCOL_BINARY_CMD_PREPEND
)
1105 execute(recv_packet(&rsp
));
1106 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1109 execute(test_binary_noop());
1111 raw_command(&cmd
, PROTOCOL_BINARY_CMD_GET
, key
, strlen(key
), NULL
, 0);
1112 execute(send_packet(&cmd
));
1113 execute(recv_packet(&rsp
));
1114 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_GET
,
1115 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1116 verify(rsp
.plain
.message
.header
.response
.bodylen
- 4 == 11);
1117 verify(memcmp(rsp
.bytes
+ 28, "hello world", 11) == 0);
1122 static enum test_return
test_binary_append(void)
1124 return test_binary_concat_impl("test_binary_append", PROTOCOL_BINARY_CMD_APPEND
);
1127 static enum test_return
test_binary_prepend(void)
1129 return test_binary_concat_impl("test_binary_prepend", PROTOCOL_BINARY_CMD_PREPEND
);
1132 static enum test_return
test_binary_appendq(void)
1134 return test_binary_concat_impl("test_binary_appendq", PROTOCOL_BINARY_CMD_APPENDQ
);
1137 static enum test_return
test_binary_prependq(void)
1139 return test_binary_concat_impl("test_binary_prependq", PROTOCOL_BINARY_CMD_PREPENDQ
);
1142 static enum test_return
test_binary_stat(void)
1147 raw_command(&cmd
, PROTOCOL_BINARY_CMD_STAT
, NULL
, 0, NULL
, 0);
1148 execute(send_packet(&cmd
));
1152 execute(recv_packet(&rsp
));
1153 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_STAT
,
1154 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1155 } while (rsp
.plain
.message
.header
.response
.keylen
!= 0);
1160 static enum test_return
send_string(const char *cmd
)
1162 execute(retry_write(cmd
, strlen(cmd
)));
1166 static enum test_return
receive_line(char *buffer
, size_t size
)
1169 while (offset
< size
)
1171 execute(retry_read(buffer
+ offset
, 1));
1172 if (buffer
[offset
] == '\n')
1174 if (offset
+ 1 < size
)
1176 buffer
[offset
+ 1]= '\0';
1188 static enum test_return
receive_response(const char *msg
) {
1190 execute(receive_line(buffer
, sizeof(buffer
)));
1191 if (strcmp(msg
, buffer
) != 0) {
1192 fprintf(stderr
, "[%s]\n", buffer
);
1194 verify(strcmp(msg
, buffer
) == 0);
1198 static enum test_return
receive_error_response(void)
1201 execute(receive_line(buffer
, sizeof(buffer
)));
1202 verify(strncmp(buffer
, "ERROR", 5) == 0 ||
1203 strncmp(buffer
, "CLIENT_ERROR", 12) == 0 ||
1204 strncmp(buffer
, "SERVER_ERROR", 12) == 0);
1208 static enum test_return
test_ascii_quit(void)
1210 /* Verify that quit handles unknown options */
1211 execute(send_string("quit foo bar\r\n"));
1212 execute(receive_error_response());
1214 /* quit doesn't support noreply */
1215 execute(send_string("quit noreply\r\n"));
1216 execute(receive_error_response());
1218 /* Verify that quit works */
1219 execute(send_string("quit\r\n"));
1221 /* Socket should be closed now, read should return EXIT_SUCCESS */
1223 verify(timeout_io_op(sock
, POLLIN
, buffer
, sizeof(buffer
)) == 0);
1224 return TEST_PASS_RECONNECT
;
1228 static enum test_return
test_ascii_version(void)
1230 /* Verify that version command handles unknown options */
1231 execute(send_string("version foo bar\r\n"));
1232 execute(receive_error_response());
1234 /* version doesn't support noreply */
1235 execute(send_string("version noreply\r\n"));
1236 execute(receive_error_response());
1238 /* Verify that verify works */
1239 execute(send_string("version\r\n"));
1241 execute(receive_line(buffer
, sizeof(buffer
)));
1242 verify(strncmp(buffer
, "VERSION ", 8) == 0);
1247 static enum test_return
test_ascii_verbosity(void)
1249 /* This command does not adhere to the spec! */
1250 execute(send_string("verbosity foo bar my\r\n"));
1251 execute(receive_error_response());
1253 execute(send_string("verbosity noreply\r\n"));
1254 execute(receive_error_response());
1256 execute(send_string("verbosity 0 noreply\r\n"));
1257 execute(test_ascii_version());
1259 execute(send_string("verbosity\r\n"));
1260 execute(receive_error_response());
1262 execute(send_string("verbosity 1\r\n"));
1263 execute(receive_response("OK\r\n"));
1265 execute(send_string("verbosity 0\r\n"));
1266 execute(receive_response("OK\r\n"));
1273 static enum test_return
test_ascii_set_impl(const char* key
, bool noreply
)
1275 /* @todo add tests for bogus format! */
1277 snprintf(buffer
, sizeof(buffer
), "set %s 0 0 5%s\r\nvalue\r\n", key
, noreply
? " noreply" : "");
1278 execute(send_string(buffer
));
1281 execute(receive_response("STORED\r\n"));
1283 return test_ascii_version();
1286 static enum test_return
test_ascii_set(void)
1288 return test_ascii_set_impl("test_ascii_set", false);
1291 static enum test_return
test_ascii_set_noreply(void)
1293 return test_ascii_set_impl("test_ascii_set_noreply", true);
1296 static enum test_return
test_ascii_add_impl(const char* key
, bool noreply
)
1298 /* @todo add tests for bogus format! */
1300 snprintf(buffer
, sizeof(buffer
), "add %s 0 0 5%s\r\nvalue\r\n", key
, noreply
? " noreply" : "");
1301 execute(send_string(buffer
));
1304 execute(receive_response("STORED\r\n"));
1306 execute(send_string(buffer
));
1309 execute(receive_response("NOT_STORED\r\n"));
1311 return test_ascii_version();
1314 static enum test_return
test_ascii_add(void)
1316 return test_ascii_add_impl("test_ascii_add", false);
1319 static enum test_return
test_ascii_add_noreply(void)
1321 return test_ascii_add_impl("test_ascii_add_noreply", true);
1324 static enum test_return
ascii_get_unknown_value(char **key
, char **value
, ssize_t
*ndata
)
1328 execute(receive_line(buffer
, sizeof(buffer
)));
1329 verify(strncmp(buffer
, "VALUE ", 6) == 0);
1330 char *end
= strchr(buffer
+ 6, ' ');
1331 verify(end
!= NULL
);
1333 *key
= strdup(buffer
+ 6);
1334 verify(*key
!= NULL
);
1337 unsigned long val
= strtoul(ptr
, &end
, 10); /* flags */
1340 verify(end
!= NULL
);
1341 *ndata
= (ssize_t
)strtoul(end
, &end
, 10); /* size */
1343 verify(end
!= NULL
);
1344 while (*end
!= '\n' && isspace(*end
))
1346 verify(*end
== '\n');
1348 *value
= static_cast<char*>(malloc((size_t)*ndata
));
1349 verify(*value
!= NULL
);
1351 execute(retry_read(*value
, (size_t)*ndata
));
1353 execute(retry_read(buffer
, 2));
1354 verify(memcmp(buffer
, "\r\n", 2) == 0);
1359 static enum test_return
ascii_get_value(const char *key
, const char *value
)
1363 size_t datasize
= strlen(value
);
1365 verify(datasize
< sizeof(buffer
));
1366 execute(receive_line(buffer
, sizeof(buffer
)));
1367 verify(strncmp(buffer
, "VALUE ", 6) == 0);
1368 verify(strncmp(buffer
+ 6, key
, strlen(key
)) == 0);
1369 char *ptr
= buffer
+ 6 + strlen(key
) + 1;
1372 unsigned long val
= strtoul(ptr
, &end
, 10); /* flags */
1375 verify(end
!= NULL
);
1376 val
= strtoul(end
, &end
, 10); /* size */
1378 verify(val
== datasize
);
1379 verify(end
!= NULL
);
1380 while (*end
!= '\n' && isspace(*end
))
1382 verify(*end
== '\n');
1384 execute(retry_read(buffer
, datasize
));
1385 verify(memcmp(buffer
, value
, datasize
) == 0);
1387 execute(retry_read(buffer
, 2));
1388 verify(memcmp(buffer
, "\r\n", 2) == 0);
1393 static enum test_return
ascii_get_item(const char *key
, const char *value
,
1399 datasize
= strlen(value
);
1401 verify(datasize
< sizeof(buffer
));
1402 snprintf(buffer
, sizeof(buffer
), "get %s\r\n", key
);
1403 execute(send_string(buffer
));
1406 execute(ascii_get_value(key
, value
));
1408 execute(retry_read(buffer
, 5));
1409 verify(memcmp(buffer
, "END\r\n", 5) == 0);
1414 static enum test_return
ascii_gets_value(const char *key
, const char *value
,
1419 size_t datasize
= strlen(value
);
1421 verify(datasize
< sizeof(buffer
));
1422 execute(receive_line(buffer
, sizeof(buffer
)));
1423 verify(strncmp(buffer
, "VALUE ", 6) == 0);
1424 verify(strncmp(buffer
+ 6, key
, strlen(key
)) == 0);
1425 char *ptr
= buffer
+ 6 + strlen(key
) + 1;
1428 unsigned long val
= strtoul(ptr
, &end
, 10); /* flags */
1431 verify(end
!= NULL
);
1432 val
= strtoul(end
, &end
, 10); /* size */
1434 verify(val
== datasize
);
1435 verify(end
!= NULL
);
1436 *cas
= strtoul(end
, &end
, 10); /* cas */
1438 verify(val
== datasize
);
1439 verify(end
!= NULL
);
1441 while (*end
!= '\n' && isspace(*end
))
1443 verify(*end
== '\n');
1445 execute(retry_read(buffer
, datasize
));
1446 verify(memcmp(buffer
, value
, datasize
) == 0);
1448 execute(retry_read(buffer
, 2));
1449 verify(memcmp(buffer
, "\r\n", 2) == 0);
1454 static enum test_return
ascii_gets_item(const char *key
, const char *value
,
1455 bool exist
, unsigned long *cas
)
1460 datasize
= strlen(value
);
1462 verify(datasize
< sizeof(buffer
));
1463 snprintf(buffer
, sizeof(buffer
), "gets %s\r\n", key
);
1464 execute(send_string(buffer
));
1467 execute(ascii_gets_value(key
, value
, cas
));
1469 execute(retry_read(buffer
, 5));
1470 verify(memcmp(buffer
, "END\r\n", 5) == 0);
1475 static enum test_return
ascii_set_item(const char *key
, const char *value
)
1478 size_t len
= strlen(value
);
1479 snprintf(buffer
, sizeof(buffer
), "set %s 0 0 %u\r\n", key
, (unsigned int)len
);
1480 execute(send_string(buffer
));
1481 execute(retry_write(value
, len
));
1482 execute(send_string("\r\n"));
1483 execute(receive_response("STORED\r\n"));
1487 static enum test_return
test_ascii_replace_impl(const char* key
, bool noreply
)
1490 snprintf(buffer
, sizeof(buffer
), "replace %s 0 0 5%s\r\nvalue\r\n", key
, noreply
? " noreply" : "");
1491 execute(send_string(buffer
));
1494 execute(test_ascii_version());
1496 execute(receive_response("NOT_STORED\r\n"));
1498 execute(ascii_set_item(key
, "value"));
1499 execute(ascii_get_item(key
, "value", true));
1502 execute(send_string(buffer
));
1505 execute(test_ascii_version());
1507 execute(receive_response("STORED\r\n"));
1509 return test_ascii_version();
1512 static enum test_return
test_ascii_replace(void)
1514 return test_ascii_replace_impl("test_ascii_replace", false);
1517 static enum test_return
test_ascii_replace_noreply(void)
1519 return test_ascii_replace_impl("test_ascii_replace_noreply", true);
1522 static enum test_return
test_ascii_cas_impl(const char* key
, bool noreply
)
1527 execute(ascii_set_item(key
, "value"));
1528 execute(ascii_gets_item(key
, "value", true, &cas
));
1530 snprintf(buffer
, sizeof(buffer
), "cas %s 0 0 6 %lu%s\r\nvalue2\r\n", key
, cas
, noreply
? " noreply" : "");
1531 execute(send_string(buffer
));
1534 execute(test_ascii_version());
1536 execute(receive_response("STORED\r\n"));
1538 /* reexecute the same command should fail due to illegal cas */
1539 execute(send_string(buffer
));
1542 execute(test_ascii_version());
1544 execute(receive_response("EXISTS\r\n"));
1546 return test_ascii_version();
1549 static enum test_return
test_ascii_cas(void)
1551 return test_ascii_cas_impl("test_ascii_cas", false);
1554 static enum test_return
test_ascii_cas_noreply(void)
1556 return test_ascii_cas_impl("test_ascii_cas_noreply", true);
1559 static enum test_return
test_ascii_delete_impl(const char *key
, bool noreply
)
1561 execute(ascii_set_item(key
, "value"));
1563 execute(send_string("delete\r\n"));
1564 execute(receive_error_response());
1565 /* BUG: the server accepts delete a b */
1566 execute(send_string("delete a b c d e\r\n"));
1567 execute(receive_error_response());
1570 snprintf(buffer
, sizeof(buffer
), "delete %s%s\r\n", key
, noreply
? " noreply" : "");
1571 execute(send_string(buffer
));
1574 execute(test_ascii_version());
1576 execute(receive_response("DELETED\r\n"));
1578 execute(ascii_get_item(key
, "value", false));
1579 execute(send_string(buffer
));
1581 execute(test_ascii_version());
1583 execute(receive_response("NOT_FOUND\r\n"));
1588 static enum test_return
test_ascii_delete(void)
1590 return test_ascii_delete_impl("test_ascii_delete", false);
1593 static enum test_return
test_ascii_delete_noreply(void)
1595 return test_ascii_delete_impl("test_ascii_delete_noreply", true);
1598 static enum test_return
test_ascii_get(void)
1600 execute(ascii_set_item("test_ascii_get", "value"));
1602 execute(send_string("get\r\n"));
1603 execute(receive_error_response());
1604 execute(ascii_get_item("test_ascii_get", "value", true));
1605 execute(ascii_get_item("test_ascii_get_notfound", "value", false));
1610 static enum test_return
test_ascii_gets(void)
1612 execute(ascii_set_item("test_ascii_gets", "value"));
1614 execute(send_string("gets\r\n"));
1615 execute(receive_error_response());
1617 execute(ascii_gets_item("test_ascii_gets", "value", true, &cas
));
1618 execute(ascii_gets_item("test_ascii_gets_notfound", "value", false, &cas
));
1623 static enum test_return
test_ascii_mget(void)
1625 const uint32_t nkeys
= 5;
1626 const char * const keys
[]= {
1629 /* test_ascii_mget_3 does not exist :) */
1635 for (uint32_t x
= 0; x
< nkeys
; ++x
)
1636 execute(ascii_set_item(keys
[x
], "value"));
1638 /* Ask for a key that doesn't exist as well */
1639 execute(send_string("get test_ascii_mget1 test_ascii_mget2 test_ascii_mget3 "
1640 "test_ascii_mget4 test_ascii_mget5 "
1641 "test_ascii_mget6\r\n"));
1643 char *returned
[nkeys
];
1645 for (uint32_t x
= 0; x
< nkeys
; ++x
)
1649 execute(ascii_get_unknown_value(&returned
[x
], &v
, &nbytes
));
1650 verify(nbytes
== 5);
1651 verify(memcmp(v
, "value", 5) == 0);
1656 execute(retry_read(buffer
, 5));
1657 verify(memcmp(buffer
, "END\r\n", 5) == 0);
1659 /* verify that we got all the keys we expected */
1660 for (uint32_t x
= 0; x
< nkeys
; ++x
)
1663 for (uint32_t y
= 0; y
< nkeys
; ++y
)
1665 if (strcmp(keys
[x
], returned
[y
]) == 0)
1674 for (uint32_t x
= 0; x
< nkeys
; ++x
)
1680 static enum test_return
test_ascii_incr_impl(const char* key
, bool noreply
)
1683 snprintf(cmd
, sizeof(cmd
), "incr %s 1%s\r\n", key
, noreply
? " noreply" : "");
1685 execute(ascii_set_item(key
, "0"));
1686 for (int x
= 1; x
< 11; ++x
)
1688 execute(send_string(cmd
));
1691 execute(test_ascii_version());
1695 execute(receive_line(buffer
, sizeof(buffer
)));
1696 int val
= atoi(buffer
);
1701 execute(ascii_get_item(key
, "10", true));
1706 static enum test_return
test_ascii_incr(void)
1708 return test_ascii_incr_impl("test_ascii_incr", false);
1711 static enum test_return
test_ascii_incr_noreply(void)
1713 return test_ascii_incr_impl("test_ascii_incr_noreply", true);
1716 static enum test_return
test_ascii_decr_impl(const char* key
, bool noreply
)
1719 snprintf(cmd
, sizeof(cmd
), "decr %s 1%s\r\n", key
, noreply
? " noreply" : "");
1721 execute(ascii_set_item(key
, "9"));
1722 for (int x
= 8; x
> -1; --x
)
1724 execute(send_string(cmd
));
1727 execute(test_ascii_version());
1731 execute(receive_line(buffer
, sizeof(buffer
)));
1732 int val
= atoi(buffer
);
1737 execute(ascii_get_item(key
, "0", true));
1739 /* verify that it doesn't wrap */
1740 execute(send_string(cmd
));
1742 execute(test_ascii_version());
1746 execute(receive_line(buffer
, sizeof(buffer
)));
1748 execute(ascii_get_item(key
, "0", true));
1753 static enum test_return
test_ascii_decr(void)
1755 return test_ascii_decr_impl("test_ascii_decr", false);
1758 static enum test_return
test_ascii_decr_noreply(void)
1760 return test_ascii_decr_impl("test_ascii_decr_noreply", true);
1764 static enum test_return
test_ascii_flush_impl(const char *key
, bool noreply
)
1767 /* Verify that the flush_all command handles unknown options */
1768 /* Bug in the current memcached server! */
1769 execute(send_string("flush_all foo bar\r\n"));
1770 execute(receive_error_response());
1773 execute(ascii_set_item(key
, key
));
1774 execute(ascii_get_item(key
, key
, true));
1778 execute(send_string("flush_all noreply\r\n"));
1779 execute(test_ascii_version());
1783 execute(send_string("flush_all\r\n"));
1784 execute(receive_response("OK\r\n"));
1787 execute(ascii_get_item(key
, key
, false));
1792 static enum test_return
test_ascii_flush(void)
1794 return test_ascii_flush_impl("test_ascii_flush", false);
1797 static enum test_return
test_ascii_flush_noreply(void)
1799 return test_ascii_flush_impl("test_ascii_flush_noreply", true);
1802 static enum test_return
test_ascii_concat_impl(const char *key
,
1813 execute(ascii_set_item(key
, value
));
1821 snprintf(cmd
, sizeof(cmd
), "%s %s 0 0 %u%s\r\n%s\r\n",
1822 append
? "append" : "prepend",
1823 key
, (unsigned int)strlen(value
), noreply
? " noreply" : "",
1825 execute(send_string(cmd
));
1828 execute(test_ascii_version());
1830 execute(receive_response("STORED\r\n"));
1832 execute(ascii_get_item(key
, "hello world", true));
1834 snprintf(cmd
, sizeof(cmd
), "%s %s_notfound 0 0 %u%s\r\n%s\r\n",
1835 append
? "append" : "prepend",
1836 key
, (unsigned int)strlen(value
), noreply
? " noreply" : "",
1838 execute(send_string(cmd
));
1841 execute(test_ascii_version());
1843 execute(receive_response("NOT_STORED\r\n"));
1848 static enum test_return
test_ascii_append(void)
1850 return test_ascii_concat_impl("test_ascii_append", true, false);
1853 static enum test_return
test_ascii_prepend(void)
1855 return test_ascii_concat_impl("test_ascii_prepend", false, false);
1858 static enum test_return
test_ascii_append_noreply(void)
1860 return test_ascii_concat_impl("test_ascii_append_noreply", true, true);
1863 static enum test_return
test_ascii_prepend_noreply(void)
1865 return test_ascii_concat_impl("test_ascii_prepend_noreply", false, true);
1868 static enum test_return
test_ascii_stat(void)
1870 execute(send_string("stats noreply\r\n"));
1871 execute(receive_error_response());
1872 execute(send_string("stats\r\n"));
1875 execute(receive_line(buffer
, sizeof(buffer
)));
1876 } while (strcmp(buffer
, "END\r\n") != 0);
1878 return TEST_PASS_RECONNECT
;
1881 typedef enum test_return(*TEST_FUNC
)(void);
1885 const char *description
;
1889 struct testcase testcases
[]= {
1890 { "ascii quit", test_ascii_quit
},
1891 { "ascii version", test_ascii_version
},
1892 { "ascii verbosity", test_ascii_verbosity
},
1893 { "ascii set", test_ascii_set
},
1894 { "ascii set noreply", test_ascii_set_noreply
},
1895 { "ascii get", test_ascii_get
},
1896 { "ascii gets", test_ascii_gets
},
1897 { "ascii mget", test_ascii_mget
},
1898 { "ascii flush", test_ascii_flush
},
1899 { "ascii flush noreply", test_ascii_flush_noreply
},
1900 { "ascii add", test_ascii_add
},
1901 { "ascii add noreply", test_ascii_add_noreply
},
1902 { "ascii replace", test_ascii_replace
},
1903 { "ascii replace noreply", test_ascii_replace_noreply
},
1904 { "ascii cas", test_ascii_cas
},
1905 { "ascii cas noreply", test_ascii_cas_noreply
},
1906 { "ascii delete", test_ascii_delete
},
1907 { "ascii delete noreply", test_ascii_delete_noreply
},
1908 { "ascii incr", test_ascii_incr
},
1909 { "ascii incr noreply", test_ascii_incr_noreply
},
1910 { "ascii decr", test_ascii_decr
},
1911 { "ascii decr noreply", test_ascii_decr_noreply
},
1912 { "ascii append", test_ascii_append
},
1913 { "ascii append noreply", test_ascii_append_noreply
},
1914 { "ascii prepend", test_ascii_prepend
},
1915 { "ascii prepend noreply", test_ascii_prepend_noreply
},
1916 { "ascii stat", test_ascii_stat
},
1917 { "binary noop", test_binary_noop
},
1918 { "binary quit", test_binary_quit
},
1919 { "binary quitq", test_binary_quitq
},
1920 { "binary set", test_binary_set
},
1921 { "binary setq", test_binary_setq
},
1922 { "binary flush", test_binary_flush
},
1923 { "binary flushq", test_binary_flushq
},
1924 { "binary add", test_binary_add
},
1925 { "binary addq", test_binary_addq
},
1926 { "binary replace", test_binary_replace
},
1927 { "binary replaceq", test_binary_replaceq
},
1928 { "binary delete", test_binary_delete
},
1929 { "binary deleteq", test_binary_deleteq
},
1930 { "binary get", test_binary_get
},
1931 { "binary getq", test_binary_getq
},
1932 { "binary getk", test_binary_getk
},
1933 { "binary getkq", test_binary_getkq
},
1934 { "binary incr", test_binary_incr
},
1935 { "binary incrq", test_binary_incrq
},
1936 { "binary decr", test_binary_decr
},
1937 { "binary decrq", test_binary_decrq
},
1938 { "binary version", test_binary_version
},
1939 { "binary append", test_binary_append
},
1940 { "binary appendq", test_binary_appendq
},
1941 { "binary prepend", test_binary_prepend
},
1942 { "binary prependq", test_binary_prependq
},
1943 { "binary stat", test_binary_stat
},
1947 const int ascii_tests
= 1;
1948 const int binary_tests
= 2;
1956 int main(int argc
, char **argv
)
1958 static const char * const status_msg
[]= {"[skip]", "[pass]", "[pass]", "[FAIL]"};
1959 struct test_type_st tests
= { true, true };
1962 const char *hostname
= "localhost";
1963 const char *port
= "11211";
1966 const char *testname
= NULL
;
1970 while ((cmd
= getopt(argc
, argv
, "t:vch:p:PT:?ab")) != EOF
)
1975 tests
.binary
= false;
1982 timeout
= atoi(optarg
);
1985 fprintf(stderr
, "Invalid timeout. Please specify a number for -t\n");
1986 return EXIT_FAILURE
;
1989 case 'v': verbose
= true;
1991 case 'c': do_core
= true;
1993 case 'h': hostname
= optarg
;
1995 case 'p': port
= optarg
;
1997 case 'P': prompt
= true;
1999 case 'T': testname
= optarg
;
2002 fprintf(stderr
, "Usage: %s [-h hostname] [-p port] [-c] [-v] [-t n] [-P] [-T testname]'\n"
2003 "\t-c\tGenerate coredump if a test fails\n"
2004 "\t-v\tVerbose test output (print out the assertion)\n"
2005 "\t-t n\tSet the timeout for io-operations to n seconds\n"
2006 "\t-P\tPrompt the user before starting a test.\n"
2007 "\t\t\t\"skip\" will skip the test\n"
2008 "\t\t\t\"quit\" will terminate memcapable\n"
2009 "\t\t\tEverything else will start the test\n"
2010 "\t-T n\tJust run the test named n\n"
2011 "\t-a\tOnly test the ascii protocol\n"
2012 "\t-b\tOnly test the binary protocol\n",
2014 return EXIT_FAILURE
;
2018 initialize_sockets();
2019 sock
= connect_server(hostname
, port
);
2020 if (sock
== INVALID_SOCKET
)
2022 fprintf(stderr
, "Failed to connect to <%s:%s>: %s\n",
2023 hostname
, port
, strerror(get_socket_errno()));
2024 return EXIT_FAILURE
;
2027 for (int ii
= 0; testcases
[ii
].description
!= NULL
; ++ii
)
2029 if (testname
!= NULL
&& strcmp(testcases
[ii
].description
, testname
) != 0)
2032 if ((testcases
[ii
].description
[0] == 'a' && (tests
.ascii
) == 0) ||
2033 (testcases
[ii
].description
[0] == 'b' && (tests
.binary
) == 0))
2038 fprintf(stdout
, "%-40s", testcases
[ii
].description
);
2043 fprintf(stdout
, "\nPress <return> when you are ready? ");
2044 char buffer
[80] = {0};
2045 if (fgets(buffer
, sizeof(buffer
), stdin
) != NULL
) {
2046 if (strncmp(buffer
, "skip", 4) == 0)
2048 fprintf(stdout
, "%-40s%s\n", testcases
[ii
].description
,
2049 status_msg
[TEST_SKIP
]);
2053 if (strncmp(buffer
, "quit", 4) == 0)
2057 fprintf(stdout
, "%-40s", testcases
[ii
].description
);
2061 bool reconnect
= false;
2062 enum test_return ret
= testcases
[ii
].function();
2063 if (ret
== TEST_FAIL
)
2068 fprintf(stderr
, "\n");
2070 else if (ret
== TEST_PASS_RECONNECT
)
2073 fprintf(stderr
, "%s\n", status_msg
[ret
]);
2077 if ((sock
= connect_server(hostname
, port
)) == INVALID_SOCKET
)
2079 fprintf(stderr
, "Failed to connect to <%s:%s>: %s\n",
2080 hostname
, port
, strerror(get_socket_errno()));
2081 fprintf(stderr
, "%d of %d tests failed\n", failed
, total
);
2082 return EXIT_FAILURE
;
2089 fprintf(stdout
, "All tests passed\n");
2091 fprintf(stderr
, "%d of %d tests failed\n", failed
, total
);
2093 return (failed
== 0) ? 0 : 1;