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 -*- */
16 #include <sys/types.h>
17 #include <sys/socket.h>
19 #include <arpa/inet.h>
20 #include <netinet/in.h>
21 #include <netinet/tcp.h>
35 #include <libmemcached/memcached/protocol_binary.h>
36 #include <libmemcached/byteorder.h>
39 /* /usr/include/netinet/in.h defines macros from ntohs() to _bswap_nn to
40 * optimize the conversion functions, but the prototypes generate warnings
41 * from gcc. The conversion methods isn't the bottleneck for my app, so
42 * just remove the warnings by undef'ing the optimization ..
48 /* Should we generate coredumps when we enounter an error (-c) */
49 static bool do_core
= false;
50 /* connection to the server */
52 /* Should the output from test failures be verbose or quiet? */
53 static bool verbose
= false;
55 /* The number of seconds to wait for an IO-operation */
56 static int timeout
= 2;
59 * Instead of having to cast between the different datatypes we create
60 * a union of all of the different types of pacages we want to send.
61 * A lot of the different commands use the same packet layout, so I'll
62 * just define the different types I need. The typedefs only contain
63 * the header of the message, so we need some space for keys and body
64 * To avoid to have to do multiple writes, lets add a chunk of memory
65 * to use. 1k should be more than enough for header, key and body.
69 protocol_binary_request_no_extras plain
;
70 protocol_binary_request_flush flush
;
71 protocol_binary_request_incr incr
;
72 protocol_binary_request_set set
;
78 protocol_binary_response_no_extras plain
;
79 protocol_binary_response_incr incr
;
80 protocol_binary_response_decr decr
;
86 TEST_SKIP
, TEST_PASS
, TEST_PASS_RECONNECT
, TEST_FAIL
90 * Try to get an addrinfo struct for a given port on a given host
92 static struct addrinfo
*lookuphost(const char *hostname
, const char *port
)
94 struct addrinfo
*ai
= 0;
95 struct addrinfo hints
= {.ai_family
=AF_UNSPEC
,
96 .ai_protocol
=IPPROTO_TCP
,
97 .ai_socktype
=SOCK_STREAM
};
98 int error
= getaddrinfo(hostname
, port
, &hints
, &ai
);
102 if (error
!= EAI_SYSTEM
)
103 fprintf(stderr
, "getaddrinfo(): %s\n", gai_strerror(error
));
105 perror("getaddrinfo()");
112 * Set the socket in nonblocking mode
113 * @return -1 if failure, the socket otherwise
115 static int set_noblock(void)
117 int flags
= fcntl(sock
, F_GETFL
, 0);
120 perror("Failed to get socket flags");
125 if ((flags
& O_NONBLOCK
) != O_NONBLOCK
)
127 if (fcntl(sock
, F_SETFL
, flags
| O_NONBLOCK
) == -1)
129 perror("Failed to set socket to nonblocking mode");
139 * Try to open a connection to the server
140 * @param hostname the name of the server to connect to
141 * @param port the port number (or service) to connect to
142 * @return positive integer if success, -1 otherwise
144 static int connect_server(const char *hostname
, const char *port
)
146 struct addrinfo
*ai
= lookuphost(hostname
, port
);
150 if ((sock
=socket(ai
->ai_family
, ai
->ai_socktype
,
151 ai
->ai_protocol
)) != -1)
153 if (connect(sock
, ai
->ai_addr
, ai
->ai_addrlen
) == -1)
155 fprintf(stderr
, "Failed to connect socket: %s\n",
165 fprintf(stderr
, "Failed to create socket: %s\n", strerror(errno
));
173 static ssize_t
timeout_io_op(int fd
, short direction
, void *buf
, size_t len
)
177 if (direction
== POLLOUT
)
178 ret
= write(fd
, buf
, len
);
180 ret
= read(fd
, buf
, len
);
182 if (ret
== -1 && errno
== EWOULDBLOCK
) {
187 int err
= poll(&fds
, 1, timeout
* 1000);
191 if (direction
== POLLOUT
)
192 ret
= write(fd
, buf
, len
);
194 ret
= read(fd
, buf
, len
);
202 perror("Failed to poll");
211 * Ensure that an expression is true. If it isn't print out a message similar
212 * to assert() and create a coredump if the user wants that. If not an error
213 * message is returned.
216 static enum test_return
ensure(bool val
, const char *expression
, const char *file
, int line
)
221 fprintf(stderr
, "\n%s:%d: %s", file
, line
, expression
);
232 #define verify(expression) do { if (ensure(expression, #expression, __FILE__, __LINE__) == TEST_FAIL) return TEST_FAIL; } while (0)
233 #define execute(expression) do { if (ensure(expression == TEST_PASS, #expression, __FILE__, __LINE__) == TEST_FAIL) return TEST_FAIL; } while (0)
236 * Send a chunk of memory over the socket (retry if the call is iterrupted
238 static enum test_return
retry_write(const void* buf
, size_t len
)
241 const char* ptr
= buf
;
245 size_t num_bytes
= len
- offset
;
246 ssize_t nw
= timeout_io_op(sock
, POLLOUT
, (void*)(ptr
+ offset
), num_bytes
);
248 verify(errno
== EINTR
|| errno
== EAGAIN
);
251 } while (offset
< len
);
257 * Resend a packet to the server (All fields in the command header should
258 * be in network byte order)
260 static enum test_return
resend_packet(command
*cmd
)
262 size_t length
= sizeof (protocol_binary_request_no_extras
) +
263 ntohl(cmd
->plain
.message
.header
.request
.bodylen
);
265 execute(retry_write(cmd
, length
));
270 * Send a command to the server. The command header needs to be updated
271 * to network byte order
273 static enum test_return
send_packet(command
*cmd
)
275 /* Fix the byteorder of the header */
276 cmd
->plain
.message
.header
.request
.keylen
=
277 ntohs(cmd
->plain
.message
.header
.request
.keylen
);
278 cmd
->plain
.message
.header
.request
.bodylen
=
279 ntohl(cmd
->plain
.message
.header
.request
.bodylen
);
280 cmd
->plain
.message
.header
.request
.cas
=
281 ntohll(cmd
->plain
.message
.header
.request
.cas
);
283 execute(resend_packet(cmd
));
288 * Read a fixed length chunk of data from the server
290 static enum test_return
retry_read(void *buf
, size_t len
)
295 ssize_t nr
= timeout_io_op(sock
, POLLIN
, ((char*) buf
) + offset
, len
- offset
);
298 verify(errno
== EINTR
|| errno
== EAGAIN
);
305 } while (offset
< len
);
311 * Receive a response from the server and conver the fields in the header
312 * to local byte order
314 static enum test_return
recv_packet(response
*rsp
)
316 execute(retry_read(rsp
, sizeof (protocol_binary_response_no_extras
)));
318 /* Fix the byte order in the packet header */
319 rsp
->plain
.message
.header
.response
.keylen
=
320 ntohs(rsp
->plain
.message
.header
.response
.keylen
);
321 rsp
->plain
.message
.header
.response
.status
=
322 ntohs(rsp
->plain
.message
.header
.response
.status
);
323 rsp
->plain
.message
.header
.response
.bodylen
=
324 ntohl(rsp
->plain
.message
.header
.response
.bodylen
);
325 rsp
->plain
.message
.header
.response
.cas
=
326 ntohll(rsp
->plain
.message
.header
.response
.cas
);
328 size_t bodysz
= rsp
->plain
.message
.header
.response
.bodylen
;
330 execute(retry_read(rsp
->bytes
+ sizeof (protocol_binary_response_no_extras
), bodysz
));
336 * Create a storage command (add, set, replace etc)
338 * @param cmd destination buffer
339 * @param cc the storage command to create
340 * @param key the key to store
341 * @param keylen the length of the key
342 * @param dta the data to store with the key
343 * @param dtalen the length of the data to store with the key
344 * @param flags the flags to store along with the key
345 * @param exp the expiry time for the key
347 static void storage_command(command
*cmd
,
356 /* all of the storage commands use the same command layout */
357 protocol_binary_request_set
*request
= &cmd
->set
;
359 memset(request
, 0, sizeof (*request
));
360 request
->message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
361 request
->message
.header
.request
.opcode
= cc
;
362 request
->message
.header
.request
.keylen
= (uint16_t)keylen
;
363 request
->message
.header
.request
.extlen
= 8;
364 request
->message
.header
.request
.bodylen
= (uint32_t)(keylen
+ 8 + dtalen
);
365 request
->message
.header
.request
.opaque
= 0xdeadbeef;
366 request
->message
.body
.flags
= flags
;
367 request
->message
.body
.expiration
= exp
;
369 off_t key_offset
= sizeof (protocol_binary_request_no_extras
) + 8;
370 memcpy(cmd
->bytes
+ key_offset
, key
, keylen
);
372 memcpy(cmd
->bytes
+ key_offset
+ keylen
, dta
, dtalen
);
376 * Create a basic command to send to the server
377 * @param cmd destination buffer
378 * @param cc the command to create
379 * @param key the key to store
380 * @param keylen the length of the key
381 * @param dta the data to store with the key
382 * @param dtalen the length of the data to store with the key
384 static void raw_command(command
*cmd
,
391 /* all of the storage commands use the same command layout */
392 memset(cmd
, 0, sizeof (*cmd
));
393 cmd
->plain
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
394 cmd
->plain
.message
.header
.request
.opcode
= cc
;
395 cmd
->plain
.message
.header
.request
.keylen
= (uint16_t)keylen
;
396 cmd
->plain
.message
.header
.request
.bodylen
= (uint32_t)(keylen
+ dtalen
);
397 cmd
->plain
.message
.header
.request
.opaque
= 0xdeadbeef;
399 off_t key_offset
= sizeof (protocol_binary_request_no_extras
);
402 memcpy(cmd
->bytes
+ key_offset
, key
, keylen
);
405 memcpy(cmd
->bytes
+ key_offset
+ keylen
, dta
, dtalen
);
409 * Create the flush command
410 * @param cmd destination buffer
411 * @param cc the command to create (FLUSH/FLUSHQ)
412 * @param exptime when to flush
413 * @param use_extra to force using of the extra field?
415 static void flush_command(command
*cmd
,
416 uint8_t cc
, uint32_t exptime
, bool use_extra
)
418 memset(cmd
, 0, sizeof (cmd
->flush
));
419 cmd
->flush
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
420 cmd
->flush
.message
.header
.request
.opcode
= cc
;
421 cmd
->flush
.message
.header
.request
.opaque
= 0xdeadbeef;
423 if (exptime
!= 0 || use_extra
)
425 cmd
->flush
.message
.header
.request
.extlen
= 4;
426 cmd
->flush
.message
.body
.expiration
= htonl(exptime
);
427 cmd
->flush
.message
.header
.request
.bodylen
= 4;
432 * Create a incr/decr command
433 * @param cc the cmd to create (FLUSH/FLUSHQ)
434 * @param key the key to operate on
435 * @param keylen the number of bytes in the key
436 * @param delta the number to add/subtract
437 * @param initial the initial value if the key doesn't exist
438 * @param exp when the key should expire if it isn't set
440 static void arithmetic_command(command
*cmd
,
448 memset(cmd
, 0, sizeof (cmd
->incr
));
449 cmd
->incr
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
450 cmd
->incr
.message
.header
.request
.opcode
= cc
;
451 cmd
->incr
.message
.header
.request
.keylen
= (uint16_t)keylen
;
452 cmd
->incr
.message
.header
.request
.extlen
= 20;
453 cmd
->incr
.message
.header
.request
.bodylen
= (uint32_t)(keylen
+ 20);
454 cmd
->incr
.message
.header
.request
.opaque
= 0xdeadbeef;
455 cmd
->incr
.message
.body
.delta
= htonll(delta
);
456 cmd
->incr
.message
.body
.initial
= htonll(initial
);
457 cmd
->incr
.message
.body
.expiration
= htonl(exp
);
459 off_t key_offset
= sizeof (protocol_binary_request_no_extras
) + 20;
460 memcpy(cmd
->bytes
+ key_offset
, key
, keylen
);
464 * Validate the response header from the server
465 * @param rsp the response to check
466 * @param cc the expected command
467 * @param status the expected status
469 static enum test_return
do_validate_response_header(response
*rsp
,
470 uint8_t cc
, uint16_t status
)
472 verify(rsp
->plain
.message
.header
.response
.magic
== PROTOCOL_BINARY_RES
);
473 verify(rsp
->plain
.message
.header
.response
.opcode
== cc
);
474 verify(rsp
->plain
.message
.header
.response
.datatype
== PROTOCOL_BINARY_RAW_BYTES
);
475 verify(rsp
->plain
.message
.header
.response
.status
== status
);
476 verify(rsp
->plain
.message
.header
.response
.opaque
== 0xdeadbeef);
478 if (status
== PROTOCOL_BINARY_RESPONSE_SUCCESS
)
481 case PROTOCOL_BINARY_CMD_ADDQ
:
482 case PROTOCOL_BINARY_CMD_APPENDQ
:
483 case PROTOCOL_BINARY_CMD_DECREMENTQ
:
484 case PROTOCOL_BINARY_CMD_DELETEQ
:
485 case PROTOCOL_BINARY_CMD_FLUSHQ
:
486 case PROTOCOL_BINARY_CMD_INCREMENTQ
:
487 case PROTOCOL_BINARY_CMD_PREPENDQ
:
488 case PROTOCOL_BINARY_CMD_QUITQ
:
489 case PROTOCOL_BINARY_CMD_REPLACEQ
:
490 case PROTOCOL_BINARY_CMD_SETQ
:
491 verify("Quiet command shouldn't return on success" == NULL
);
497 case PROTOCOL_BINARY_CMD_ADD
:
498 case PROTOCOL_BINARY_CMD_REPLACE
:
499 case PROTOCOL_BINARY_CMD_SET
:
500 case PROTOCOL_BINARY_CMD_APPEND
:
501 case PROTOCOL_BINARY_CMD_PREPEND
:
502 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
503 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
504 verify(rsp
->plain
.message
.header
.response
.bodylen
== 0);
505 verify(rsp
->plain
.message
.header
.response
.cas
!= 0);
507 case PROTOCOL_BINARY_CMD_FLUSH
:
508 case PROTOCOL_BINARY_CMD_NOOP
:
509 case PROTOCOL_BINARY_CMD_QUIT
:
510 case PROTOCOL_BINARY_CMD_DELETE
:
511 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
512 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
513 verify(rsp
->plain
.message
.header
.response
.bodylen
== 0);
514 verify(rsp
->plain
.message
.header
.response
.cas
== 0);
517 case PROTOCOL_BINARY_CMD_DECREMENT
:
518 case PROTOCOL_BINARY_CMD_INCREMENT
:
519 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
520 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
521 verify(rsp
->plain
.message
.header
.response
.bodylen
== 8);
522 verify(rsp
->plain
.message
.header
.response
.cas
!= 0);
525 case PROTOCOL_BINARY_CMD_STAT
:
526 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
527 /* key and value exists in all packets except in the terminating */
528 verify(rsp
->plain
.message
.header
.response
.cas
== 0);
531 case PROTOCOL_BINARY_CMD_VERSION
:
532 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
533 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
534 verify(rsp
->plain
.message
.header
.response
.bodylen
!= 0);
535 verify(rsp
->plain
.message
.header
.response
.cas
== 0);
538 case PROTOCOL_BINARY_CMD_GET
:
539 case PROTOCOL_BINARY_CMD_GETQ
:
540 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
541 verify(rsp
->plain
.message
.header
.response
.extlen
== 4);
542 verify(rsp
->plain
.message
.header
.response
.cas
!= 0);
545 case PROTOCOL_BINARY_CMD_GETK
:
546 case PROTOCOL_BINARY_CMD_GETKQ
:
547 verify(rsp
->plain
.message
.header
.response
.keylen
!= 0);
548 verify(rsp
->plain
.message
.header
.response
.extlen
== 4);
549 verify(rsp
->plain
.message
.header
.response
.cas
!= 0);
553 /* Undefined command code */
559 verify(rsp
->plain
.message
.header
.response
.cas
== 0);
560 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
561 if (cc
!= PROTOCOL_BINARY_CMD_GETK
)
563 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
570 /* We call verify(validate_response_header), but that macro
571 * expects a boolean expression, and the function returns
572 * an enum.... Let's just create a macro to avoid cluttering
573 * the code with all of the == TEST_PASS ;-)
575 #define validate_response_header(a,b,c) \
576 do_validate_response_header(a,b,c) == TEST_PASS
578 static enum test_return
test_binary_noop(void)
582 raw_command(&cmd
, PROTOCOL_BINARY_CMD_NOOP
, NULL
, 0, NULL
, 0);
583 execute(send_packet(&cmd
));
584 execute(recv_packet(&rsp
));
585 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_NOOP
,
586 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
590 static enum test_return
test_binary_quit_impl(uint8_t cc
)
594 raw_command(&cmd
, cc
, NULL
, 0, NULL
, 0);
596 execute(send_packet(&cmd
));
597 if (cc
== PROTOCOL_BINARY_CMD_QUIT
)
599 execute(recv_packet(&rsp
));
600 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_QUIT
,
601 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
604 /* Socket should be closed now, read should return 0 */
605 verify(timeout_io_op(sock
, POLLIN
, rsp
.bytes
, sizeof(rsp
.bytes
)) == 0);
607 return TEST_PASS_RECONNECT
;
610 static enum test_return
test_binary_quit(void)
612 return test_binary_quit_impl(PROTOCOL_BINARY_CMD_QUIT
);
615 static enum test_return
test_binary_quitq(void)
617 return test_binary_quit_impl(PROTOCOL_BINARY_CMD_QUITQ
);
620 static enum test_return
test_binary_set_impl(const char* key
, uint8_t cc
)
625 uint64_t value
= 0xdeadbeefdeadcafe;
626 storage_command(&cmd
, cc
, key
, strlen(key
), &value
, sizeof (value
), 0, 0);
628 /* set should always work */
629 for (int ii
= 0; ii
< 10; ii
++)
632 execute(send_packet(&cmd
));
634 execute(resend_packet(&cmd
));
636 if (cc
== PROTOCOL_BINARY_CMD_SET
)
638 execute(recv_packet(&rsp
));
639 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
642 execute(test_binary_noop());
646 * We need to get the current CAS id, and at this time we haven't
647 * verified that we have a working get
649 if (cc
== PROTOCOL_BINARY_CMD_SETQ
)
651 cmd
.set
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_SET
;
652 execute(resend_packet(&cmd
));
653 execute(recv_packet(&rsp
));
654 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_SET
,
655 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
656 cmd
.set
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_SETQ
;
659 /* try to set with the correct CAS value */
660 cmd
.plain
.message
.header
.request
.cas
=
661 htonll(rsp
.plain
.message
.header
.response
.cas
);
662 execute(resend_packet(&cmd
));
663 if (cc
== PROTOCOL_BINARY_CMD_SET
)
665 execute(recv_packet(&rsp
));
666 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
669 execute(test_binary_noop());
671 /* try to set with an incorrect CAS value */
672 cmd
.plain
.message
.header
.request
.cas
=
673 htonll(rsp
.plain
.message
.header
.response
.cas
- 1);
674 execute(resend_packet(&cmd
));
675 execute(recv_packet(&rsp
));
676 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS
));
678 return test_binary_noop();
681 static enum test_return
test_binary_set(void)
683 return test_binary_set_impl("test_binary_set", PROTOCOL_BINARY_CMD_SET
);
686 static enum test_return
test_binary_setq(void)
688 return test_binary_set_impl("test_binary_setq", PROTOCOL_BINARY_CMD_SETQ
);
691 static enum test_return
test_binary_add_impl(const char* key
, uint8_t cc
)
695 uint64_t value
= 0xdeadbeefdeadcafe;
696 storage_command(&cmd
, cc
, key
, strlen(key
), &value
, sizeof (value
), 0, 0);
698 /* first add should work, rest of them should fail (even with cas
700 for (int ii
=0; ii
< 10; ii
++)
703 execute(send_packet(&cmd
));
705 execute(resend_packet(&cmd
));
707 if (cc
== PROTOCOL_BINARY_CMD_ADD
|| ii
> 0)
709 uint16_t expected_result
;
711 expected_result
= PROTOCOL_BINARY_RESPONSE_SUCCESS
;
713 expected_result
= PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS
;
715 execute(recv_packet(&rsp
));
716 verify(validate_response_header(&rsp
, cc
, expected_result
));
719 execute(test_binary_noop());
725 static enum test_return
test_binary_add(void)
727 return test_binary_add_impl("test_binary_add", PROTOCOL_BINARY_CMD_ADD
);
730 static enum test_return
test_binary_addq(void)
732 return test_binary_add_impl("test_binary_addq", PROTOCOL_BINARY_CMD_ADDQ
);
735 static enum test_return
binary_set_item(const char *key
, const char *value
)
739 storage_command(&cmd
, PROTOCOL_BINARY_CMD_SET
, key
, strlen(key
),
740 value
, strlen(value
), 0, 0);
741 execute(send_packet(&cmd
));
742 execute(recv_packet(&rsp
));
743 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_SET
,
744 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
748 static enum test_return
test_binary_replace_impl(const char* key
, uint8_t cc
)
752 uint64_t value
= 0xdeadbeefdeadcafe;
753 storage_command(&cmd
, cc
, key
, strlen(key
), &value
, sizeof (value
), 0, 0);
755 /* first replace should fail, successive should succeed (when the
757 for (int ii
= 0; ii
< 10; ii
++)
760 execute(send_packet(&cmd
));
762 execute(resend_packet(&cmd
));
764 if (cc
== PROTOCOL_BINARY_CMD_REPLACE
|| ii
== 0)
766 uint16_t expected_result
;
768 expected_result
=PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
;
770 expected_result
=PROTOCOL_BINARY_RESPONSE_SUCCESS
;
772 execute(recv_packet(&rsp
));
773 verify(validate_response_header(&rsp
, cc
, expected_result
));
776 execute(binary_set_item(key
, key
));
779 execute(test_binary_noop());
782 /* verify that replace with CAS value works! */
783 cmd
.plain
.message
.header
.request
.cas
=
784 htonll(rsp
.plain
.message
.header
.response
.cas
);
785 execute(resend_packet(&cmd
));
787 if (cc
== PROTOCOL_BINARY_CMD_REPLACE
)
789 execute(recv_packet(&rsp
));
790 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
793 execute(test_binary_noop());
795 /* try to set with an incorrect CAS value */
796 cmd
.plain
.message
.header
.request
.cas
=
797 htonll(rsp
.plain
.message
.header
.response
.cas
- 1);
798 execute(resend_packet(&cmd
));
799 execute(recv_packet(&rsp
));
800 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS
));
805 static enum test_return
test_binary_replace(void)
807 return test_binary_replace_impl("test_binary_replace", PROTOCOL_BINARY_CMD_REPLACE
);
810 static enum test_return
test_binary_replaceq(void)
812 return test_binary_replace_impl("test_binary_replaceq", PROTOCOL_BINARY_CMD_REPLACEQ
);
815 static enum test_return
test_binary_delete_impl(const char *key
, uint8_t cc
)
819 raw_command(&cmd
, cc
, key
, strlen(key
), NULL
, 0);
821 /* The delete shouldn't work the first time, because the item isn't there */
822 execute(send_packet(&cmd
));
823 execute(recv_packet(&rsp
));
824 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
));
825 execute(binary_set_item(key
, key
));
827 /* The item should be present now, resend*/
828 execute(resend_packet(&cmd
));
829 if (cc
== PROTOCOL_BINARY_CMD_DELETE
)
831 execute(recv_packet(&rsp
));
832 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
835 execute(test_binary_noop());
840 static enum test_return
test_binary_delete(void)
842 return test_binary_delete_impl("test_binary_delete", PROTOCOL_BINARY_CMD_DELETE
);
845 static enum test_return
test_binary_deleteq(void)
847 return test_binary_delete_impl("test_binary_deleteq", PROTOCOL_BINARY_CMD_DELETEQ
);
850 static enum test_return
test_binary_get_impl(const char *key
, uint8_t cc
)
855 raw_command(&cmd
, cc
, key
, strlen(key
), NULL
, 0);
856 execute(send_packet(&cmd
));
858 if (cc
== PROTOCOL_BINARY_CMD_GET
|| cc
== PROTOCOL_BINARY_CMD_GETK
)
860 execute(recv_packet(&rsp
));
861 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
));
864 execute(test_binary_noop());
866 execute(binary_set_item(key
, key
));
867 execute(resend_packet(&cmd
));
868 execute(recv_packet(&rsp
));
869 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
874 static enum test_return
test_binary_get(void)
876 return test_binary_get_impl("test_binary_get", PROTOCOL_BINARY_CMD_GET
);
879 static enum test_return
test_binary_getk(void)
881 return test_binary_get_impl("test_binary_getk", PROTOCOL_BINARY_CMD_GETK
);
884 static enum test_return
test_binary_getq(void)
886 return test_binary_get_impl("test_binary_getq", PROTOCOL_BINARY_CMD_GETQ
);
889 static enum test_return
test_binary_getkq(void)
891 return test_binary_get_impl("test_binary_getkq", PROTOCOL_BINARY_CMD_GETKQ
);
894 static enum test_return
test_binary_incr_impl(const char* key
, uint8_t cc
)
898 arithmetic_command(&cmd
, cc
, key
, strlen(key
), 1, 0, 0);
901 for (ii
= 0; ii
< 10; ++ii
)
904 execute(send_packet(&cmd
));
906 execute(resend_packet(&cmd
));
908 if (cc
== PROTOCOL_BINARY_CMD_INCREMENT
)
910 execute(recv_packet(&rsp
));
911 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
912 verify(ntohll(rsp
.incr
.message
.body
.value
) == ii
);
915 execute(test_binary_noop());
918 /* @todo add incorrect CAS */
922 static enum test_return
test_binary_incr(void)
924 return test_binary_incr_impl("test_binary_incr", PROTOCOL_BINARY_CMD_INCREMENT
);
927 static enum test_return
test_binary_incrq(void)
929 return test_binary_incr_impl("test_binary_incrq", PROTOCOL_BINARY_CMD_INCREMENTQ
);
932 static enum test_return
test_binary_decr_impl(const char* key
, uint8_t cc
)
936 arithmetic_command(&cmd
, cc
, key
, strlen(key
), 1, 9, 0);
939 for (ii
= 9; ii
> -1; --ii
)
942 execute(send_packet(&cmd
));
944 execute(resend_packet(&cmd
));
946 if (cc
== PROTOCOL_BINARY_CMD_DECREMENT
)
948 execute(recv_packet(&rsp
));
949 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
950 verify(ntohll(rsp
.decr
.message
.body
.value
) == (uint64_t)ii
);
953 execute(test_binary_noop());
956 /* decr 0 should not wrap */
957 execute(resend_packet(&cmd
));
958 if (cc
== PROTOCOL_BINARY_CMD_DECREMENT
)
960 execute(recv_packet(&rsp
));
961 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
962 verify(ntohll(rsp
.decr
.message
.body
.value
) == 0);
966 /* @todo get the value and verify! */
970 /* @todo add incorrect cas */
971 execute(test_binary_noop());
975 static enum test_return
test_binary_decr(void)
977 return test_binary_decr_impl("test_binary_decr",
978 PROTOCOL_BINARY_CMD_DECREMENT
);
981 static enum test_return
test_binary_decrq(void)
983 return test_binary_decr_impl("test_binary_decrq",
984 PROTOCOL_BINARY_CMD_DECREMENTQ
);
987 static enum test_return
test_binary_version(void)
991 raw_command(&cmd
, PROTOCOL_BINARY_CMD_VERSION
, NULL
, 0, NULL
, 0);
993 execute(send_packet(&cmd
));
994 execute(recv_packet(&rsp
));
995 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_VERSION
,
996 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1001 static enum test_return
test_binary_flush_impl(const char *key
, uint8_t cc
)
1006 for (int ii
= 0; ii
< 2; ++ii
)
1008 execute(binary_set_item(key
, key
));
1009 flush_command(&cmd
, cc
, 0, ii
== 0);
1010 execute(send_packet(&cmd
));
1012 if (cc
== PROTOCOL_BINARY_CMD_FLUSH
)
1014 execute(recv_packet(&rsp
));
1015 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1018 execute(test_binary_noop());
1020 raw_command(&cmd
, PROTOCOL_BINARY_CMD_GET
, key
, strlen(key
), NULL
, 0);
1021 execute(send_packet(&cmd
));
1022 execute(recv_packet(&rsp
));
1023 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_GET
,
1024 PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
));
1030 static enum test_return
test_binary_flush(void)
1032 return test_binary_flush_impl("test_binary_flush", PROTOCOL_BINARY_CMD_FLUSH
);
1035 static enum test_return
test_binary_flushq(void)
1037 return test_binary_flush_impl("test_binary_flushq", PROTOCOL_BINARY_CMD_FLUSHQ
);
1040 static enum test_return
test_binary_concat_impl(const char *key
, uint8_t cc
)
1046 if (cc
== PROTOCOL_BINARY_CMD_APPEND
|| cc
== PROTOCOL_BINARY_CMD_APPENDQ
)
1051 execute(binary_set_item(key
, value
));
1053 if (cc
== PROTOCOL_BINARY_CMD_APPEND
|| cc
== PROTOCOL_BINARY_CMD_APPENDQ
)
1058 raw_command(&cmd
, cc
, key
, strlen(key
), value
, strlen(value
));
1059 execute(send_packet(&cmd
));
1060 if (cc
== PROTOCOL_BINARY_CMD_APPEND
|| cc
== PROTOCOL_BINARY_CMD_PREPEND
)
1062 execute(recv_packet(&rsp
));
1063 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1066 execute(test_binary_noop());
1068 raw_command(&cmd
, PROTOCOL_BINARY_CMD_GET
, key
, strlen(key
), NULL
, 0);
1069 execute(send_packet(&cmd
));
1070 execute(recv_packet(&rsp
));
1071 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_GET
,
1072 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1073 verify(rsp
.plain
.message
.header
.response
.bodylen
- 4 == 11);
1074 verify(memcmp(rsp
.bytes
+ 28, "hello world", 11) == 0);
1079 static enum test_return
test_binary_append(void)
1081 return test_binary_concat_impl("test_binary_append", PROTOCOL_BINARY_CMD_APPEND
);
1084 static enum test_return
test_binary_prepend(void)
1086 return test_binary_concat_impl("test_binary_prepend", PROTOCOL_BINARY_CMD_PREPEND
);
1089 static enum test_return
test_binary_appendq(void)
1091 return test_binary_concat_impl("test_binary_appendq", PROTOCOL_BINARY_CMD_APPENDQ
);
1094 static enum test_return
test_binary_prependq(void)
1096 return test_binary_concat_impl("test_binary_prependq", PROTOCOL_BINARY_CMD_PREPENDQ
);
1099 static enum test_return
test_binary_stat(void)
1104 raw_command(&cmd
, PROTOCOL_BINARY_CMD_STAT
, NULL
, 0, NULL
, 0);
1105 execute(send_packet(&cmd
));
1109 execute(recv_packet(&rsp
));
1110 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_STAT
,
1111 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1112 } while (rsp
.plain
.message
.header
.response
.keylen
!= 0);
1117 static enum test_return
test_binary_illegal(void)
1125 raw_command(&cmd
, cc
, NULL
, 0, NULL
, 0);
1126 execute(send_packet(&cmd
));
1127 execute(recv_packet(&rsp
));
1128 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_UNKNOWN_COMMAND
));
1132 return TEST_PASS_RECONNECT
;
1135 static enum test_return
send_string(const char *cmd
)
1137 execute(retry_write(cmd
, strlen(cmd
)));
1141 static enum test_return
receive_line(char *buffer
, size_t size
)
1144 while (offset
< size
)
1146 execute(retry_read(buffer
+ offset
, 1));
1147 if (buffer
[offset
] == '\n')
1149 if (offset
+ 1 < size
)
1151 buffer
[offset
+ 1]= '\0';
1163 static enum test_return
receive_response(const char *msg
) {
1165 execute(receive_line(buffer
, sizeof(buffer
)));
1166 verify(strcmp(msg
, buffer
) == 0);
1170 static enum test_return
test_ascii_quit(void)
1172 /* Verify that quit handles unknown options */
1173 execute(send_string("quit foo bar\r\n"));
1174 execute(receive_response("ERROR\r\n"));
1176 /* quit doesn't support noreply */
1177 execute(send_string("quit noreply\r\n"));
1178 execute(receive_response("ERROR\r\n"));
1180 /* Verify that quit works */
1181 execute(send_string("quit\r\n"));
1183 /* Socket should be closed now, read should return 0 */
1185 verify(timeout_io_op(sock
, POLLIN
, buffer
, sizeof(buffer
)) == 0);
1186 return TEST_PASS_RECONNECT
;
1190 static enum test_return
test_ascii_version(void)
1192 /* Verify that version command handles unknown options */
1193 execute(send_string("version foo bar\r\n"));
1194 execute(receive_response("ERROR\r\n"));
1196 /* version doesn't support noreply */
1197 execute(send_string("version noreply\r\n"));
1198 execute(receive_response("ERROR\r\n"));
1200 /* Verify that verify works */
1201 execute(send_string("version\r\n"));
1203 execute(receive_line(buffer
, sizeof(buffer
)));
1204 verify(strncmp(buffer
, "VERSION ", 8) == 0);
1209 static enum test_return
test_ascii_verbosity(void)
1211 /* This command does not adhere to the spec! */
1212 execute(send_string("verbosity foo bar my\r\n"));
1213 execute(receive_response("ERROR\r\n"));
1215 execute(send_string("verbosity noreply\r\n"));
1216 execute(test_ascii_version());
1218 execute(send_string("verbosity 0 noreply\r\n"));
1219 execute(test_ascii_version());
1221 execute(send_string("verbosity\r\n"));
1222 execute(receive_response("ERROR\r\n"));
1224 execute(send_string("verbosity 1\r\n"));
1225 execute(receive_response("OK\r\n"));
1227 execute(send_string("verbosity 0\r\n"));
1228 execute(receive_response("OK\r\n"));
1235 static enum test_return
test_ascii_set_impl(const char* key
, bool noreply
)
1237 /* @todo add tests for bogus format! */
1239 sprintf(buffer
, "set %s 0 0 5%s\r\nvalue\r\n", key
,
1240 noreply
? " noreply" : "");
1241 execute(send_string(buffer
));
1244 execute(receive_response("STORED\r\n"));
1246 return test_ascii_version();
1249 static enum test_return
test_ascii_set(void)
1251 return test_ascii_set_impl("test_ascii_set", false);
1254 static enum test_return
test_ascii_set_noreply(void)
1256 return test_ascii_set_impl("test_ascii_set_noreply", true);
1259 static enum test_return
test_ascii_add_impl(const char* key
, bool noreply
)
1261 /* @todo add tests for bogus format! */
1263 sprintf(buffer
, "add %s 0 0 5%s\r\nvalue\r\n", key
,
1264 noreply
? " noreply" : "");
1265 execute(send_string(buffer
));
1268 execute(receive_response("STORED\r\n"));
1270 execute(send_string(buffer
));
1273 execute(receive_response("NOT_STORED\r\n"));
1275 return test_ascii_version();
1278 static enum test_return
test_ascii_add(void)
1280 return test_ascii_add_impl("test_ascii_add", false);
1283 static enum test_return
test_ascii_add_noreply(void)
1285 return test_ascii_add_impl("test_ascii_add_noreply", true);
1288 static enum test_return
ascii_get_value(const char *key
, const char *value
)
1292 size_t datasize
= strlen(value
);
1294 verify(datasize
< sizeof(buffer
));
1295 execute(receive_line(buffer
, sizeof(buffer
)));
1296 verify(strncmp(buffer
, "VALUE ", 6) == 0);
1297 verify(strncmp(buffer
+ 6, key
, strlen(key
)) == 0);
1298 char *ptr
= buffer
+ 6 + strlen(key
) + 1;
1301 unsigned long val
= strtoul(ptr
, &end
, 10); /* flags */
1304 verify(end
!= NULL
);
1305 val
= strtoul(end
, &end
, 10); /* size */
1307 verify(val
== datasize
);
1308 verify(end
!= NULL
);
1309 while (*end
!= '\n' && isspace(*end
))
1311 verify(*end
== '\n');
1313 execute(retry_read(buffer
, datasize
));
1314 verify(memcmp(buffer
, value
, datasize
) == 0);
1316 execute(retry_read(buffer
, 2));
1317 verify(memcmp(buffer
, "\r\n", 2) == 0);
1322 static enum test_return
ascii_get_item(const char *key
, const char *value
,
1328 datasize
= strlen(value
);
1330 verify(datasize
< sizeof(buffer
));
1331 sprintf(buffer
, "get %s\r\n", key
);
1332 execute(send_string(buffer
));
1335 execute(ascii_get_value(key
, value
));
1337 execute(retry_read(buffer
, 5));
1338 verify(memcmp(buffer
, "END\r\n", 5) == 0);
1343 static enum test_return
ascii_gets_value(const char *key
, const char *value
,
1348 size_t datasize
= strlen(value
);
1350 verify(datasize
< sizeof(buffer
));
1351 execute(receive_line(buffer
, sizeof(buffer
)));
1352 verify(strncmp(buffer
, "VALUE ", 6) == 0);
1353 verify(strncmp(buffer
+ 6, key
, strlen(key
)) == 0);
1354 char *ptr
= buffer
+ 6 + strlen(key
) + 1;
1357 unsigned long val
= strtoul(ptr
, &end
, 10); /* flags */
1360 verify(end
!= NULL
);
1361 val
= strtoul(end
, &end
, 10); /* size */
1363 verify(val
== datasize
);
1364 verify(end
!= NULL
);
1365 *cas
= strtoul(end
, &end
, 10); /* cas */
1367 verify(val
== datasize
);
1368 verify(end
!= NULL
);
1370 while (*end
!= '\n' && isspace(*end
))
1372 verify(*end
== '\n');
1374 execute(retry_read(buffer
, datasize
));
1375 verify(memcmp(buffer
, value
, datasize
) == 0);
1377 execute(retry_read(buffer
, 2));
1378 verify(memcmp(buffer
, "\r\n", 2) == 0);
1383 static enum test_return
ascii_gets_item(const char *key
, const char *value
,
1384 bool exist
, unsigned long *cas
)
1389 datasize
= strlen(value
);
1391 verify(datasize
< sizeof(buffer
));
1392 sprintf(buffer
, "gets %s\r\n", key
);
1393 execute(send_string(buffer
));
1396 execute(ascii_gets_value(key
, value
, cas
));
1398 execute(retry_read(buffer
, 5));
1399 verify(memcmp(buffer
, "END\r\n", 5) == 0);
1404 static enum test_return
ascii_set_item(const char *key
, const char *value
)
1407 size_t len
= strlen(value
);
1408 sprintf(buffer
, "set %s 0 0 %u\r\n", key
, (unsigned int)len
);
1409 execute(send_string(buffer
));
1410 execute(retry_write(value
, len
));
1411 execute(send_string("\r\n"));
1412 execute(receive_response("STORED\r\n"));
1416 static enum test_return
test_ascii_replace_impl(const char* key
, bool noreply
)
1419 sprintf(buffer
, "replace %s 0 0 5%s\r\nvalue\r\n", key
,
1420 noreply
? " noreply" : "");
1421 execute(send_string(buffer
));
1424 execute(test_ascii_version());
1426 execute(receive_response("NOT_STORED\r\n"));
1428 execute(ascii_set_item(key
, "value"));
1429 execute(ascii_get_item(key
, "value", true));
1432 execute(send_string(buffer
));
1435 execute(test_ascii_version());
1437 execute(receive_response("STORED\r\n"));
1439 return test_ascii_version();
1442 static enum test_return
test_ascii_replace(void)
1444 return test_ascii_replace_impl("test_ascii_replace", false);
1447 static enum test_return
test_ascii_replace_noreply(void)
1449 return test_ascii_replace_impl("test_ascii_replace_noreply", true);
1452 static enum test_return
test_ascii_cas_impl(const char* key
, bool noreply
)
1457 execute(ascii_set_item(key
, "value"));
1458 execute(ascii_gets_item(key
, "value", true, &cas
));
1460 sprintf(buffer
, "cas %s 0 0 6 %lu%s\r\nvalue2\r\n", key
, cas
,
1461 noreply
? " noreply" : "");
1462 execute(send_string(buffer
));
1465 execute(test_ascii_version());
1467 execute(receive_response("STORED\r\n"));
1469 /* reexecute the same command should fail due to illegal cas */
1470 execute(send_string(buffer
));
1473 execute(test_ascii_version());
1475 execute(receive_response("EXISTS\r\n"));
1477 return test_ascii_version();
1480 static enum test_return
test_ascii_cas(void)
1482 return test_ascii_cas_impl("test_ascii_cas", false);
1485 static enum test_return
test_ascii_cas_noreply(void)
1487 return test_ascii_cas_impl("test_ascii_cas_noreply", true);
1490 static enum test_return
test_ascii_delete_impl(const char *key
, bool noreply
)
1492 execute(ascii_set_item(key
, "value"));
1494 execute(send_string("delete\r\n"));
1495 execute(receive_response("ERROR\r\n"));
1496 /* BUG: the server accepts delete a b */
1497 execute(send_string("delete a b c d e\r\n"));
1498 execute(receive_response("ERROR\r\n"));
1501 sprintf(buffer
, "delete %s%s\r\n", key
, noreply
? " noreply" : "");
1502 execute(send_string(buffer
));
1505 execute(test_ascii_version());
1507 execute(receive_response("DELETED\r\n"));
1509 execute(ascii_get_item(key
, "value", false));
1510 execute(send_string(buffer
));
1512 execute(test_ascii_version());
1514 execute(receive_response("NOT_FOUND\r\n"));
1519 static enum test_return
test_ascii_delete(void)
1521 return test_ascii_delete_impl("test_ascii_delete", false);
1524 static enum test_return
test_ascii_delete_noreply(void)
1526 return test_ascii_delete_impl("test_ascii_delete_noreply", true);
1529 static enum test_return
test_ascii_get(void)
1531 execute(ascii_set_item("test_ascii_get", "value"));
1533 execute(send_string("get\r\n"));
1534 execute(receive_response("ERROR\r\n"));
1535 execute(ascii_get_item("test_ascii_get", "value", true));
1536 execute(ascii_get_item("test_ascii_get_notfound", "value", false));
1541 static enum test_return
test_ascii_gets(void)
1543 execute(ascii_set_item("test_ascii_gets", "value"));
1545 execute(send_string("gets\r\n"));
1546 execute(receive_response("ERROR\r\n"));
1548 execute(ascii_gets_item("test_ascii_gets", "value", true, &cas
));
1549 execute(ascii_gets_item("test_ascii_gets_notfound", "value", false, &cas
));
1554 static enum test_return
test_ascii_mget(void)
1556 execute(ascii_set_item("test_ascii_mget1", "value"));
1557 execute(ascii_set_item("test_ascii_mget2", "value"));
1558 execute(ascii_set_item("test_ascii_mget3", "value"));
1559 execute(ascii_set_item("test_ascii_mget4", "value"));
1560 execute(ascii_set_item("test_ascii_mget5", "value"));
1562 execute(send_string("get test_ascii_mget1 test_ascii_mget2 test_ascii_mget3 "
1563 "test_ascii_mget4 test_ascii_mget5 "
1564 "test_ascii_mget6\r\n"));
1565 execute(ascii_get_value("test_ascii_mget1", "value"));
1566 execute(ascii_get_value("test_ascii_mget2", "value"));
1567 execute(ascii_get_value("test_ascii_mget3", "value"));
1568 execute(ascii_get_value("test_ascii_mget4", "value"));
1569 execute(ascii_get_value("test_ascii_mget5", "value"));
1572 execute(retry_read(buffer
, 5));
1573 verify(memcmp(buffer
, "END\r\n", 5) == 0);
1577 static enum test_return
test_ascii_incr_impl(const char* key
, bool noreply
)
1580 sprintf(cmd
, "incr %s 1%s\r\n", key
, noreply
? " noreply" : "");
1582 execute(ascii_set_item(key
, "0"));
1583 for (int x
= 1; x
< 11; ++x
)
1585 execute(send_string(cmd
));
1588 execute(test_ascii_version());
1592 execute(receive_line(buffer
, sizeof(buffer
)));
1593 int val
= atoi(buffer
);
1598 execute(ascii_get_item(key
, "10", true));
1603 static enum test_return
test_ascii_incr(void)
1605 return test_ascii_incr_impl("test_ascii_incr", false);
1608 static enum test_return
test_ascii_incr_noreply(void)
1610 return test_ascii_incr_impl("test_ascii_incr_noreply", true);
1613 static enum test_return
test_ascii_decr_impl(const char* key
, bool noreply
)
1616 sprintf(cmd
, "decr %s 1%s\r\n", key
, noreply
? " noreply" : "");
1618 execute(ascii_set_item(key
, "9"));
1619 for (int x
= 8; x
> -1; --x
)
1621 execute(send_string(cmd
));
1624 execute(test_ascii_version());
1628 execute(receive_line(buffer
, sizeof(buffer
)));
1629 int val
= atoi(buffer
);
1634 execute(ascii_get_item(key
, "0", true));
1636 /* verify that it doesn't wrap */
1637 execute(send_string(cmd
));
1639 execute(test_ascii_version());
1643 execute(receive_line(buffer
, sizeof(buffer
)));
1645 execute(ascii_get_item(key
, "0", true));
1650 static enum test_return
test_ascii_decr(void)
1652 return test_ascii_decr_impl("test_ascii_decr", false);
1655 static enum test_return
test_ascii_decr_noreply(void)
1657 return test_ascii_decr_impl("test_ascii_decr_noreply", true);
1661 static enum test_return
test_ascii_flush_impl(const char *key
, bool noreply
)
1664 /* Verify that the flush_all command handles unknown options */
1665 /* Bug in the current memcached server! */
1666 execute(send_string("flush_all foo bar\r\n"));
1667 execute(receive_response("ERROR\r\n"));
1670 execute(ascii_set_item(key
, key
));
1671 execute(ascii_get_item(key
, key
, true));
1675 execute(send_string("flush_all noreply\r\n"));
1676 execute(test_ascii_version());
1680 execute(send_string("flush_all\r\n"));
1681 execute(receive_response("OK\r\n"));
1684 execute(ascii_get_item(key
, key
, false));
1689 static enum test_return
test_ascii_flush(void)
1691 return test_ascii_flush_impl("test_ascii_flush", false);
1694 static enum test_return
test_ascii_flush_noreply(void)
1696 return test_ascii_flush_impl("test_ascii_flush_noreply", true);
1699 static enum test_return
test_ascii_concat_impl(const char *key
,
1710 execute(ascii_set_item(key
, value
));
1718 sprintf(cmd
, "%s %s 0 0 %u%s\r\n%s\r\n",
1719 append
? "append" : "prepend",
1720 key
, (unsigned int)strlen(value
), noreply
? " noreply" : "",
1722 execute(send_string(cmd
));
1725 execute(test_ascii_version());
1727 execute(receive_response("STORED\r\n"));
1729 execute(ascii_get_item(key
, "hello world", true));
1731 sprintf(cmd
, "%s %s_notfound 0 0 %u%s\r\n%s\r\n",
1732 append
? "append" : "prepend",
1733 key
, (unsigned int)strlen(value
), noreply
? " noreply" : "",
1735 execute(send_string(cmd
));
1738 execute(test_ascii_version());
1740 execute(receive_response("NOT_STORED\r\n"));
1745 static enum test_return
test_ascii_append(void)
1747 return test_ascii_concat_impl("test_ascii_append", true, false);
1750 static enum test_return
test_ascii_prepend(void)
1752 return test_ascii_concat_impl("test_ascii_prepend", false, false);
1755 static enum test_return
test_ascii_append_noreply(void)
1757 return test_ascii_concat_impl("test_ascii_append_noreply", true, true);
1760 static enum test_return
test_ascii_prepend_noreply(void)
1762 return test_ascii_concat_impl("test_ascii_prepend_noreply", false, true);
1765 static enum test_return
test_ascii_stat(void)
1767 execute(send_string("stats noreply\r\n"));
1768 execute(receive_response("ERROR\r\n"));
1769 execute(send_string("stats\r\n"));
1772 execute(receive_line(buffer
, sizeof(buffer
)));
1773 } while (strcmp(buffer
, "END\r\n") != 0);
1775 return TEST_PASS_RECONNECT
;
1778 typedef enum test_return(*TEST_FUNC
)(void);
1782 const char *description
;
1786 struct testcase testcases
[]= {
1787 { "ascii quit", test_ascii_quit
},
1788 { "ascii version", test_ascii_version
},
1789 { "ascii verbosity", test_ascii_verbosity
},
1790 { "ascii set", test_ascii_set
},
1791 { "ascii set noreply", test_ascii_set_noreply
},
1792 { "ascii get", test_ascii_get
},
1793 { "ascii gets", test_ascii_gets
},
1794 { "ascii mget", test_ascii_mget
},
1795 { "ascii flush", test_ascii_flush
},
1796 { "ascii flush noreply", test_ascii_flush_noreply
},
1797 { "ascii add", test_ascii_add
},
1798 { "ascii add noreply", test_ascii_add_noreply
},
1799 { "ascii replace", test_ascii_replace
},
1800 { "ascii replace noreply", test_ascii_replace_noreply
},
1801 { "ascii cas", test_ascii_cas
},
1802 { "ascii cas noreply", test_ascii_cas_noreply
},
1803 { "ascii delete", test_ascii_delete
},
1804 { "ascii delete noreply", test_ascii_delete_noreply
},
1805 { "ascii incr", test_ascii_incr
},
1806 { "ascii incr noreply", test_ascii_incr_noreply
},
1807 { "ascii decr", test_ascii_decr
},
1808 { "ascii decr noreply", test_ascii_decr_noreply
},
1809 { "ascii append", test_ascii_append
},
1810 { "ascii append noreply", test_ascii_append_noreply
},
1811 { "ascii prepend", test_ascii_prepend
},
1812 { "ascii prepend noreply", test_ascii_prepend_noreply
},
1813 { "ascii stat", test_ascii_stat
},
1814 { "binary noop", test_binary_noop
},
1815 { "binary quit", test_binary_quit
},
1816 { "binary quitq", test_binary_quitq
},
1817 { "binary set", test_binary_set
},
1818 { "binary setq", test_binary_setq
},
1819 { "binary flush", test_binary_flush
},
1820 { "binary flushq", test_binary_flushq
},
1821 { "binary add", test_binary_add
},
1822 { "binary addq", test_binary_addq
},
1823 { "binary replace", test_binary_replace
},
1824 { "binary replaceq", test_binary_replaceq
},
1825 { "binary delete", test_binary_delete
},
1826 { "binary deleteq", test_binary_deleteq
},
1827 { "binary get", test_binary_get
},
1828 { "binary getq", test_binary_getq
},
1829 { "binary getk", test_binary_getk
},
1830 { "binary getkq", test_binary_getkq
},
1831 { "binary incr", test_binary_incr
},
1832 { "binary incrq", test_binary_incrq
},
1833 { "binary decr", test_binary_decr
},
1834 { "binary decrq", test_binary_decrq
},
1835 { "binary version", test_binary_version
},
1836 { "binary append", test_binary_append
},
1837 { "binary appendq", test_binary_appendq
},
1838 { "binary prepend", test_binary_prepend
},
1839 { "binary prependq", test_binary_prependq
},
1840 { "binary stat", test_binary_stat
},
1841 { "binary illegal", test_binary_illegal
},
1845 int main(int argc
, char **argv
)
1847 static const char * const status_msg
[]= {"[skip]", "[pass]", "[pass]", "[FAIL]"};
1850 const char *hostname
= "localhost";
1851 const char *port
= "11211";
1854 while ((cmd
= getopt(argc
, argv
, "t:vch:p:?")) != EOF
)
1858 timeout
= atoi(optarg
);
1861 fprintf(stderr
, "Invalid timeout. Please specify a number for -t\n");
1865 case 'v': verbose
= true;
1867 case 'c': do_core
= true;
1869 case 'h': hostname
= optarg
;
1871 case 'p': port
= optarg
;
1874 fprintf(stderr
, "Usage: %s [-h hostname] [-p port] [-c] [-v] [-t n]\n"
1875 "\t-c\tGenerate coredump if a test fails\n"
1876 "\t-v\tVerbose test output (print out the assertion)\n"
1877 "\t-t n\tSet the timeout for io-operations to n seconds\n",
1883 sock
= connect_server(hostname
, port
);
1886 fprintf(stderr
, "Failed to connect to <%s:%s>: %s\n",
1887 hostname
, port
, strerror(errno
));
1891 for (int ii
= 0; testcases
[ii
].description
!= NULL
; ++ii
)
1894 fprintf(stdout
, "%-40s", testcases
[ii
].description
);
1897 bool reconnect
= false;
1898 enum test_return ret
= testcases
[ii
].function();
1899 if (ret
== TEST_FAIL
)
1904 fprintf(stderr
, "\n");
1906 else if (ret
== TEST_PASS_RECONNECT
)
1909 fprintf(stderr
, "%s\n", status_msg
[ret
]);
1913 if ((sock
=connect_server(hostname
, port
)) == -1)
1915 fprintf(stderr
, "Failed to connect to <%s:%s>: %s\n",
1916 hostname
, port
, strerror(errno
));
1917 fprintf(stderr
, "%d of %d tests failed\n", failed
, total
);
1925 fprintf(stdout
, "All tests passed\n");
1927 fprintf(stderr
, "%d of %d tests failed\n", failed
, total
);
1929 return (failed
== 0) ? 0 : 1;