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 exptime 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
= exptime
;
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 exptime 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(exptime
);
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
579 static enum test_return
send_binary_noop(void)
582 raw_command(&cmd
, PROTOCOL_BINARY_CMD_NOOP
, NULL
, 0, NULL
, 0);
583 execute(send_packet(&cmd
));
587 static enum test_return
receive_binary_noop(void)
590 execute(recv_packet(&rsp
));
591 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_NOOP
,
592 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
596 static enum test_return
test_binary_noop(void)
598 execute(send_binary_noop());
599 execute(receive_binary_noop());
603 static enum test_return
test_binary_quit_impl(uint8_t cc
)
607 raw_command(&cmd
, cc
, NULL
, 0, NULL
, 0);
609 execute(send_packet(&cmd
));
610 if (cc
== PROTOCOL_BINARY_CMD_QUIT
)
612 execute(recv_packet(&rsp
));
613 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_QUIT
,
614 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
617 /* Socket should be closed now, read should return 0 */
618 verify(timeout_io_op(sock
, POLLIN
, rsp
.bytes
, sizeof(rsp
.bytes
)) == 0);
620 return TEST_PASS_RECONNECT
;
623 static enum test_return
test_binary_quit(void)
625 return test_binary_quit_impl(PROTOCOL_BINARY_CMD_QUIT
);
628 static enum test_return
test_binary_quitq(void)
630 return test_binary_quit_impl(PROTOCOL_BINARY_CMD_QUITQ
);
633 static enum test_return
test_binary_set_impl(const char* key
, uint8_t cc
)
638 uint64_t value
= 0xdeadbeefdeadcafe;
639 storage_command(&cmd
, cc
, key
, strlen(key
), &value
, sizeof (value
), 0, 0);
641 /* set should always work */
642 for (int ii
= 0; ii
< 10; ii
++)
645 execute(send_packet(&cmd
));
647 execute(resend_packet(&cmd
));
649 if (cc
== PROTOCOL_BINARY_CMD_SET
)
651 execute(recv_packet(&rsp
));
652 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
655 execute(test_binary_noop());
659 * We need to get the current CAS id, and at this time we haven't
660 * verified that we have a working get
662 if (cc
== PROTOCOL_BINARY_CMD_SETQ
)
664 cmd
.set
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_SET
;
665 execute(resend_packet(&cmd
));
666 execute(recv_packet(&rsp
));
667 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_SET
,
668 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
669 cmd
.set
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_SETQ
;
672 /* try to set with the correct CAS value */
673 cmd
.plain
.message
.header
.request
.cas
=
674 htonll(rsp
.plain
.message
.header
.response
.cas
);
675 execute(resend_packet(&cmd
));
676 if (cc
== PROTOCOL_BINARY_CMD_SET
)
678 execute(recv_packet(&rsp
));
679 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
682 execute(test_binary_noop());
684 /* try to set with an incorrect CAS value */
685 cmd
.plain
.message
.header
.request
.cas
=
686 htonll(rsp
.plain
.message
.header
.response
.cas
- 1);
687 execute(resend_packet(&cmd
));
688 execute(recv_packet(&rsp
));
689 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS
));
691 return test_binary_noop();
694 static enum test_return
test_binary_set(void)
696 return test_binary_set_impl("test_binary_set", PROTOCOL_BINARY_CMD_SET
);
699 static enum test_return
test_binary_setq(void)
701 return test_binary_set_impl("test_binary_setq", PROTOCOL_BINARY_CMD_SETQ
);
704 static enum test_return
test_binary_add_impl(const char* key
, uint8_t cc
)
708 uint64_t value
= 0xdeadbeefdeadcafe;
709 storage_command(&cmd
, cc
, key
, strlen(key
), &value
, sizeof (value
), 0, 0);
711 /* first add should work, rest of them should fail (even with cas
713 for (int ii
=0; ii
< 10; ii
++)
716 execute(send_packet(&cmd
));
718 execute(resend_packet(&cmd
));
720 if (cc
== PROTOCOL_BINARY_CMD_ADD
|| ii
> 0)
722 uint16_t expected_result
;
724 expected_result
= PROTOCOL_BINARY_RESPONSE_SUCCESS
;
726 expected_result
= PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS
;
728 execute(recv_packet(&rsp
));
729 verify(validate_response_header(&rsp
, cc
, expected_result
));
732 execute(test_binary_noop());
738 static enum test_return
test_binary_add(void)
740 return test_binary_add_impl("test_binary_add", PROTOCOL_BINARY_CMD_ADD
);
743 static enum test_return
test_binary_addq(void)
745 return test_binary_add_impl("test_binary_addq", PROTOCOL_BINARY_CMD_ADDQ
);
748 static enum test_return
binary_set_item(const char *key
, const char *value
)
752 storage_command(&cmd
, PROTOCOL_BINARY_CMD_SET
, key
, strlen(key
),
753 value
, strlen(value
), 0, 0);
754 execute(send_packet(&cmd
));
755 execute(recv_packet(&rsp
));
756 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_SET
,
757 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
761 static enum test_return
test_binary_replace_impl(const char* key
, uint8_t cc
)
765 uint64_t value
= 0xdeadbeefdeadcafe;
766 storage_command(&cmd
, cc
, key
, strlen(key
), &value
, sizeof (value
), 0, 0);
768 /* first replace should fail, successive should succeed (when the
770 for (int ii
= 0; ii
< 10; ii
++)
773 execute(send_packet(&cmd
));
775 execute(resend_packet(&cmd
));
777 if (cc
== PROTOCOL_BINARY_CMD_REPLACE
|| ii
== 0)
779 uint16_t expected_result
;
781 expected_result
=PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
;
783 expected_result
=PROTOCOL_BINARY_RESPONSE_SUCCESS
;
785 execute(recv_packet(&rsp
));
786 verify(validate_response_header(&rsp
, cc
, expected_result
));
789 execute(binary_set_item(key
, key
));
792 execute(test_binary_noop());
795 /* verify that replace with CAS value works! */
796 cmd
.plain
.message
.header
.request
.cas
=
797 htonll(rsp
.plain
.message
.header
.response
.cas
);
798 execute(resend_packet(&cmd
));
800 if (cc
== PROTOCOL_BINARY_CMD_REPLACE
)
802 execute(recv_packet(&rsp
));
803 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
806 execute(test_binary_noop());
808 /* try to set with an incorrect CAS value */
809 cmd
.plain
.message
.header
.request
.cas
=
810 htonll(rsp
.plain
.message
.header
.response
.cas
- 1);
811 execute(resend_packet(&cmd
));
812 execute(recv_packet(&rsp
));
813 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS
));
818 static enum test_return
test_binary_replace(void)
820 return test_binary_replace_impl("test_binary_replace", PROTOCOL_BINARY_CMD_REPLACE
);
823 static enum test_return
test_binary_replaceq(void)
825 return test_binary_replace_impl("test_binary_replaceq", PROTOCOL_BINARY_CMD_REPLACEQ
);
828 static enum test_return
test_binary_delete_impl(const char *key
, uint8_t cc
)
832 raw_command(&cmd
, cc
, key
, strlen(key
), NULL
, 0);
834 /* The delete shouldn't work the first time, because the item isn't there */
835 execute(send_packet(&cmd
));
836 execute(recv_packet(&rsp
));
837 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
));
838 execute(binary_set_item(key
, key
));
840 /* The item should be present now, resend*/
841 execute(resend_packet(&cmd
));
842 if (cc
== PROTOCOL_BINARY_CMD_DELETE
)
844 execute(recv_packet(&rsp
));
845 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
848 execute(test_binary_noop());
853 static enum test_return
test_binary_delete(void)
855 return test_binary_delete_impl("test_binary_delete", PROTOCOL_BINARY_CMD_DELETE
);
858 static enum test_return
test_binary_deleteq(void)
860 return test_binary_delete_impl("test_binary_deleteq", PROTOCOL_BINARY_CMD_DELETEQ
);
863 static enum test_return
test_binary_get_impl(const char *key
, uint8_t cc
)
868 raw_command(&cmd
, cc
, key
, strlen(key
), NULL
, 0);
869 execute(send_packet(&cmd
));
870 execute(send_binary_noop());
872 if (cc
== PROTOCOL_BINARY_CMD_GET
|| cc
== PROTOCOL_BINARY_CMD_GETK
)
874 execute(recv_packet(&rsp
));
875 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
));
878 execute(receive_binary_noop());
880 execute(binary_set_item(key
, key
));
881 execute(resend_packet(&cmd
));
882 execute(send_binary_noop());
884 execute(recv_packet(&rsp
));
885 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
886 execute(receive_binary_noop());
891 static enum test_return
test_binary_get(void)
893 return test_binary_get_impl("test_binary_get", PROTOCOL_BINARY_CMD_GET
);
896 static enum test_return
test_binary_getk(void)
898 return test_binary_get_impl("test_binary_getk", PROTOCOL_BINARY_CMD_GETK
);
901 static enum test_return
test_binary_getq(void)
903 return test_binary_get_impl("test_binary_getq", PROTOCOL_BINARY_CMD_GETQ
);
906 static enum test_return
test_binary_getkq(void)
908 return test_binary_get_impl("test_binary_getkq", PROTOCOL_BINARY_CMD_GETKQ
);
911 static enum test_return
test_binary_incr_impl(const char* key
, uint8_t cc
)
915 arithmetic_command(&cmd
, cc
, key
, strlen(key
), 1, 0, 0);
918 for (ii
= 0; ii
< 10; ++ii
)
921 execute(send_packet(&cmd
));
923 execute(resend_packet(&cmd
));
925 if (cc
== PROTOCOL_BINARY_CMD_INCREMENT
)
927 execute(recv_packet(&rsp
));
928 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
929 verify(ntohll(rsp
.incr
.message
.body
.value
) == ii
);
932 execute(test_binary_noop());
935 /* @todo add incorrect CAS */
939 static enum test_return
test_binary_incr(void)
941 return test_binary_incr_impl("test_binary_incr", PROTOCOL_BINARY_CMD_INCREMENT
);
944 static enum test_return
test_binary_incrq(void)
946 return test_binary_incr_impl("test_binary_incrq", PROTOCOL_BINARY_CMD_INCREMENTQ
);
949 static enum test_return
test_binary_decr_impl(const char* key
, uint8_t cc
)
953 arithmetic_command(&cmd
, cc
, key
, strlen(key
), 1, 9, 0);
956 for (ii
= 9; ii
> -1; --ii
)
959 execute(send_packet(&cmd
));
961 execute(resend_packet(&cmd
));
963 if (cc
== PROTOCOL_BINARY_CMD_DECREMENT
)
965 execute(recv_packet(&rsp
));
966 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
967 verify(ntohll(rsp
.decr
.message
.body
.value
) == (uint64_t)ii
);
970 execute(test_binary_noop());
973 /* decr 0 should not wrap */
974 execute(resend_packet(&cmd
));
975 if (cc
== PROTOCOL_BINARY_CMD_DECREMENT
)
977 execute(recv_packet(&rsp
));
978 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
979 verify(ntohll(rsp
.decr
.message
.body
.value
) == 0);
983 /* @todo get the value and verify! */
987 /* @todo add incorrect cas */
988 execute(test_binary_noop());
992 static enum test_return
test_binary_decr(void)
994 return test_binary_decr_impl("test_binary_decr",
995 PROTOCOL_BINARY_CMD_DECREMENT
);
998 static enum test_return
test_binary_decrq(void)
1000 return test_binary_decr_impl("test_binary_decrq",
1001 PROTOCOL_BINARY_CMD_DECREMENTQ
);
1004 static enum test_return
test_binary_version(void)
1008 raw_command(&cmd
, PROTOCOL_BINARY_CMD_VERSION
, NULL
, 0, NULL
, 0);
1010 execute(send_packet(&cmd
));
1011 execute(recv_packet(&rsp
));
1012 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_VERSION
,
1013 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1018 static enum test_return
test_binary_flush_impl(const char *key
, uint8_t cc
)
1023 for (int ii
= 0; ii
< 2; ++ii
)
1025 execute(binary_set_item(key
, key
));
1026 flush_command(&cmd
, cc
, 0, ii
== 0);
1027 execute(send_packet(&cmd
));
1029 if (cc
== PROTOCOL_BINARY_CMD_FLUSH
)
1031 execute(recv_packet(&rsp
));
1032 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1035 execute(test_binary_noop());
1037 raw_command(&cmd
, PROTOCOL_BINARY_CMD_GET
, key
, strlen(key
), NULL
, 0);
1038 execute(send_packet(&cmd
));
1039 execute(recv_packet(&rsp
));
1040 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_GET
,
1041 PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
));
1047 static enum test_return
test_binary_flush(void)
1049 return test_binary_flush_impl("test_binary_flush", PROTOCOL_BINARY_CMD_FLUSH
);
1052 static enum test_return
test_binary_flushq(void)
1054 return test_binary_flush_impl("test_binary_flushq", PROTOCOL_BINARY_CMD_FLUSHQ
);
1057 static enum test_return
test_binary_concat_impl(const char *key
, uint8_t cc
)
1063 if (cc
== PROTOCOL_BINARY_CMD_APPEND
|| cc
== PROTOCOL_BINARY_CMD_APPENDQ
)
1068 execute(binary_set_item(key
, value
));
1070 if (cc
== PROTOCOL_BINARY_CMD_APPEND
|| cc
== PROTOCOL_BINARY_CMD_APPENDQ
)
1075 raw_command(&cmd
, cc
, key
, strlen(key
), value
, strlen(value
));
1076 execute(send_packet(&cmd
));
1077 if (cc
== PROTOCOL_BINARY_CMD_APPEND
|| cc
== PROTOCOL_BINARY_CMD_PREPEND
)
1079 execute(recv_packet(&rsp
));
1080 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1083 execute(test_binary_noop());
1085 raw_command(&cmd
, PROTOCOL_BINARY_CMD_GET
, key
, strlen(key
), NULL
, 0);
1086 execute(send_packet(&cmd
));
1087 execute(recv_packet(&rsp
));
1088 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_GET
,
1089 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1090 verify(rsp
.plain
.message
.header
.response
.bodylen
- 4 == 11);
1091 verify(memcmp(rsp
.bytes
+ 28, "hello world", 11) == 0);
1096 static enum test_return
test_binary_append(void)
1098 return test_binary_concat_impl("test_binary_append", PROTOCOL_BINARY_CMD_APPEND
);
1101 static enum test_return
test_binary_prepend(void)
1103 return test_binary_concat_impl("test_binary_prepend", PROTOCOL_BINARY_CMD_PREPEND
);
1106 static enum test_return
test_binary_appendq(void)
1108 return test_binary_concat_impl("test_binary_appendq", PROTOCOL_BINARY_CMD_APPENDQ
);
1111 static enum test_return
test_binary_prependq(void)
1113 return test_binary_concat_impl("test_binary_prependq", PROTOCOL_BINARY_CMD_PREPENDQ
);
1116 static enum test_return
test_binary_stat(void)
1121 raw_command(&cmd
, PROTOCOL_BINARY_CMD_STAT
, NULL
, 0, NULL
, 0);
1122 execute(send_packet(&cmd
));
1126 execute(recv_packet(&rsp
));
1127 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_STAT
,
1128 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1129 } while (rsp
.plain
.message
.header
.response
.keylen
!= 0);
1134 static enum test_return
send_string(const char *cmd
)
1136 execute(retry_write(cmd
, strlen(cmd
)));
1140 static enum test_return
receive_line(char *buffer
, size_t size
)
1143 while (offset
< size
)
1145 execute(retry_read(buffer
+ offset
, 1));
1146 if (buffer
[offset
] == '\n')
1148 if (offset
+ 1 < size
)
1150 buffer
[offset
+ 1]= '\0';
1162 static enum test_return
receive_response(const char *msg
) {
1164 execute(receive_line(buffer
, sizeof(buffer
)));
1165 if (strcmp(msg
, buffer
) != 0) {
1166 fprintf(stderr
, "[%s]\n", buffer
);
1168 verify(strcmp(msg
, buffer
) == 0);
1172 static enum test_return
receive_error_response(void)
1175 execute(receive_line(buffer
, sizeof(buffer
)));
1176 verify(strncmp(buffer
, "ERROR\r\n", 7) == 0 ||
1177 strncmp(buffer
, "CLIENT_ERROR:", 13) == 0 ||
1178 strncmp(buffer
, "SERVER_ERROR:", 13) == 0);
1182 static enum test_return
test_ascii_quit(void)
1184 /* Verify that quit handles unknown options */
1185 execute(send_string("quit foo bar\r\n"));
1186 execute(receive_error_response());
1188 /* quit doesn't support noreply */
1189 execute(send_string("quit noreply\r\n"));
1190 execute(receive_error_response());
1192 /* Verify that quit works */
1193 execute(send_string("quit\r\n"));
1195 /* Socket should be closed now, read should return 0 */
1197 verify(timeout_io_op(sock
, POLLIN
, buffer
, sizeof(buffer
)) == 0);
1198 return TEST_PASS_RECONNECT
;
1202 static enum test_return
test_ascii_version(void)
1204 /* Verify that version command handles unknown options */
1205 execute(send_string("version foo bar\r\n"));
1206 execute(receive_error_response());
1208 /* version doesn't support noreply */
1209 execute(send_string("version noreply\r\n"));
1210 execute(receive_error_response());
1212 /* Verify that verify works */
1213 execute(send_string("version\r\n"));
1215 execute(receive_line(buffer
, sizeof(buffer
)));
1216 verify(strncmp(buffer
, "VERSION ", 8) == 0);
1221 static enum test_return
test_ascii_verbosity(void)
1223 /* This command does not adhere to the spec! */
1224 execute(send_string("verbosity foo bar my\r\n"));
1225 execute(receive_error_response());
1227 execute(send_string("verbosity noreply\r\n"));
1228 execute(receive_error_response());
1230 execute(send_string("verbosity 0 noreply\r\n"));
1231 execute(test_ascii_version());
1233 execute(send_string("verbosity\r\n"));
1234 execute(receive_error_response());
1236 execute(send_string("verbosity 1\r\n"));
1237 execute(receive_response("OK\r\n"));
1239 execute(send_string("verbosity 0\r\n"));
1240 execute(receive_response("OK\r\n"));
1247 static enum test_return
test_ascii_set_impl(const char* key
, bool noreply
)
1249 /* @todo add tests for bogus format! */
1251 sprintf(buffer
, "set %s 0 0 5%s\r\nvalue\r\n", key
,
1252 noreply
? " noreply" : "");
1253 execute(send_string(buffer
));
1256 execute(receive_response("STORED\r\n"));
1258 return test_ascii_version();
1261 static enum test_return
test_ascii_set(void)
1263 return test_ascii_set_impl("test_ascii_set", false);
1266 static enum test_return
test_ascii_set_noreply(void)
1268 return test_ascii_set_impl("test_ascii_set_noreply", true);
1271 static enum test_return
test_ascii_add_impl(const char* key
, bool noreply
)
1273 /* @todo add tests for bogus format! */
1275 sprintf(buffer
, "add %s 0 0 5%s\r\nvalue\r\n", key
,
1276 noreply
? " noreply" : "");
1277 execute(send_string(buffer
));
1280 execute(receive_response("STORED\r\n"));
1282 execute(send_string(buffer
));
1285 execute(receive_response("NOT_STORED\r\n"));
1287 return test_ascii_version();
1290 static enum test_return
test_ascii_add(void)
1292 return test_ascii_add_impl("test_ascii_add", false);
1295 static enum test_return
test_ascii_add_noreply(void)
1297 return test_ascii_add_impl("test_ascii_add_noreply", true);
1300 static enum test_return
ascii_get_value(const char *key
, const char *value
)
1304 size_t datasize
= strlen(value
);
1306 verify(datasize
< sizeof(buffer
));
1307 execute(receive_line(buffer
, sizeof(buffer
)));
1308 verify(strncmp(buffer
, "VALUE ", 6) == 0);
1309 verify(strncmp(buffer
+ 6, key
, strlen(key
)) == 0);
1310 char *ptr
= buffer
+ 6 + strlen(key
) + 1;
1313 unsigned long val
= strtoul(ptr
, &end
, 10); /* flags */
1316 verify(end
!= NULL
);
1317 val
= strtoul(end
, &end
, 10); /* size */
1319 verify(val
== datasize
);
1320 verify(end
!= NULL
);
1321 while (*end
!= '\n' && isspace(*end
))
1323 verify(*end
== '\n');
1325 execute(retry_read(buffer
, datasize
));
1326 verify(memcmp(buffer
, value
, datasize
) == 0);
1328 execute(retry_read(buffer
, 2));
1329 verify(memcmp(buffer
, "\r\n", 2) == 0);
1334 static enum test_return
ascii_get_item(const char *key
, const char *value
,
1340 datasize
= strlen(value
);
1342 verify(datasize
< sizeof(buffer
));
1343 sprintf(buffer
, "get %s\r\n", key
);
1344 execute(send_string(buffer
));
1347 execute(ascii_get_value(key
, value
));
1349 execute(retry_read(buffer
, 5));
1350 verify(memcmp(buffer
, "END\r\n", 5) == 0);
1355 static enum test_return
ascii_gets_value(const char *key
, const char *value
,
1360 size_t datasize
= strlen(value
);
1362 verify(datasize
< sizeof(buffer
));
1363 execute(receive_line(buffer
, sizeof(buffer
)));
1364 verify(strncmp(buffer
, "VALUE ", 6) == 0);
1365 verify(strncmp(buffer
+ 6, key
, strlen(key
)) == 0);
1366 char *ptr
= buffer
+ 6 + strlen(key
) + 1;
1369 unsigned long val
= strtoul(ptr
, &end
, 10); /* flags */
1372 verify(end
!= NULL
);
1373 val
= strtoul(end
, &end
, 10); /* size */
1375 verify(val
== datasize
);
1376 verify(end
!= NULL
);
1377 *cas
= strtoul(end
, &end
, 10); /* cas */
1379 verify(val
== datasize
);
1380 verify(end
!= NULL
);
1382 while (*end
!= '\n' && isspace(*end
))
1384 verify(*end
== '\n');
1386 execute(retry_read(buffer
, datasize
));
1387 verify(memcmp(buffer
, value
, datasize
) == 0);
1389 execute(retry_read(buffer
, 2));
1390 verify(memcmp(buffer
, "\r\n", 2) == 0);
1395 static enum test_return
ascii_gets_item(const char *key
, const char *value
,
1396 bool exist
, unsigned long *cas
)
1401 datasize
= strlen(value
);
1403 verify(datasize
< sizeof(buffer
));
1404 sprintf(buffer
, "gets %s\r\n", key
);
1405 execute(send_string(buffer
));
1408 execute(ascii_gets_value(key
, value
, cas
));
1410 execute(retry_read(buffer
, 5));
1411 verify(memcmp(buffer
, "END\r\n", 5) == 0);
1416 static enum test_return
ascii_set_item(const char *key
, const char *value
)
1419 size_t len
= strlen(value
);
1420 sprintf(buffer
, "set %s 0 0 %u\r\n", key
, (unsigned int)len
);
1421 execute(send_string(buffer
));
1422 execute(retry_write(value
, len
));
1423 execute(send_string("\r\n"));
1424 execute(receive_response("STORED\r\n"));
1428 static enum test_return
test_ascii_replace_impl(const char* key
, bool noreply
)
1431 sprintf(buffer
, "replace %s 0 0 5%s\r\nvalue\r\n", key
,
1432 noreply
? " noreply" : "");
1433 execute(send_string(buffer
));
1436 execute(test_ascii_version());
1438 execute(receive_response("NOT_STORED\r\n"));
1440 execute(ascii_set_item(key
, "value"));
1441 execute(ascii_get_item(key
, "value", true));
1444 execute(send_string(buffer
));
1447 execute(test_ascii_version());
1449 execute(receive_response("STORED\r\n"));
1451 return test_ascii_version();
1454 static enum test_return
test_ascii_replace(void)
1456 return test_ascii_replace_impl("test_ascii_replace", false);
1459 static enum test_return
test_ascii_replace_noreply(void)
1461 return test_ascii_replace_impl("test_ascii_replace_noreply", true);
1464 static enum test_return
test_ascii_cas_impl(const char* key
, bool noreply
)
1469 execute(ascii_set_item(key
, "value"));
1470 execute(ascii_gets_item(key
, "value", true, &cas
));
1472 sprintf(buffer
, "cas %s 0 0 6 %lu%s\r\nvalue2\r\n", key
, cas
,
1473 noreply
? " noreply" : "");
1474 execute(send_string(buffer
));
1477 execute(test_ascii_version());
1479 execute(receive_response("STORED\r\n"));
1481 /* reexecute the same command should fail due to illegal cas */
1482 execute(send_string(buffer
));
1485 execute(test_ascii_version());
1487 execute(receive_response("EXISTS\r\n"));
1489 return test_ascii_version();
1492 static enum test_return
test_ascii_cas(void)
1494 return test_ascii_cas_impl("test_ascii_cas", false);
1497 static enum test_return
test_ascii_cas_noreply(void)
1499 return test_ascii_cas_impl("test_ascii_cas_noreply", true);
1502 static enum test_return
test_ascii_delete_impl(const char *key
, bool noreply
)
1504 execute(ascii_set_item(key
, "value"));
1506 execute(send_string("delete\r\n"));
1507 execute(receive_error_response());
1508 /* BUG: the server accepts delete a b */
1509 execute(send_string("delete a b c d e\r\n"));
1510 execute(receive_error_response());
1513 sprintf(buffer
, "delete %s%s\r\n", key
, noreply
? " noreply" : "");
1514 execute(send_string(buffer
));
1517 execute(test_ascii_version());
1519 execute(receive_response("DELETED\r\n"));
1521 execute(ascii_get_item(key
, "value", false));
1522 execute(send_string(buffer
));
1524 execute(test_ascii_version());
1526 execute(receive_response("NOT_FOUND\r\n"));
1531 static enum test_return
test_ascii_delete(void)
1533 return test_ascii_delete_impl("test_ascii_delete", false);
1536 static enum test_return
test_ascii_delete_noreply(void)
1538 return test_ascii_delete_impl("test_ascii_delete_noreply", true);
1541 static enum test_return
test_ascii_get(void)
1543 execute(ascii_set_item("test_ascii_get", "value"));
1545 execute(send_string("get\r\n"));
1546 execute(receive_error_response());
1547 execute(ascii_get_item("test_ascii_get", "value", true));
1548 execute(ascii_get_item("test_ascii_get_notfound", "value", false));
1553 static enum test_return
test_ascii_gets(void)
1555 execute(ascii_set_item("test_ascii_gets", "value"));
1557 execute(send_string("gets\r\n"));
1558 execute(receive_error_response());
1560 execute(ascii_gets_item("test_ascii_gets", "value", true, &cas
));
1561 execute(ascii_gets_item("test_ascii_gets_notfound", "value", false, &cas
));
1566 static enum test_return
test_ascii_mget(void)
1568 execute(ascii_set_item("test_ascii_mget1", "value"));
1569 execute(ascii_set_item("test_ascii_mget2", "value"));
1570 execute(ascii_set_item("test_ascii_mget3", "value"));
1571 execute(ascii_set_item("test_ascii_mget4", "value"));
1572 execute(ascii_set_item("test_ascii_mget5", "value"));
1574 execute(send_string("get test_ascii_mget1 test_ascii_mget2 test_ascii_mget3 "
1575 "test_ascii_mget4 test_ascii_mget5 "
1576 "test_ascii_mget6\r\n"));
1577 execute(ascii_get_value("test_ascii_mget1", "value"));
1578 execute(ascii_get_value("test_ascii_mget2", "value"));
1579 execute(ascii_get_value("test_ascii_mget3", "value"));
1580 execute(ascii_get_value("test_ascii_mget4", "value"));
1581 execute(ascii_get_value("test_ascii_mget5", "value"));
1584 execute(retry_read(buffer
, 5));
1585 verify(memcmp(buffer
, "END\r\n", 5) == 0);
1589 static enum test_return
test_ascii_incr_impl(const char* key
, bool noreply
)
1592 sprintf(cmd
, "incr %s 1%s\r\n", key
, noreply
? " noreply" : "");
1594 execute(ascii_set_item(key
, "0"));
1595 for (int x
= 1; x
< 11; ++x
)
1597 execute(send_string(cmd
));
1600 execute(test_ascii_version());
1604 execute(receive_line(buffer
, sizeof(buffer
)));
1605 int val
= atoi(buffer
);
1610 execute(ascii_get_item(key
, "10", true));
1615 static enum test_return
test_ascii_incr(void)
1617 return test_ascii_incr_impl("test_ascii_incr", false);
1620 static enum test_return
test_ascii_incr_noreply(void)
1622 return test_ascii_incr_impl("test_ascii_incr_noreply", true);
1625 static enum test_return
test_ascii_decr_impl(const char* key
, bool noreply
)
1628 sprintf(cmd
, "decr %s 1%s\r\n", key
, noreply
? " noreply" : "");
1630 execute(ascii_set_item(key
, "9"));
1631 for (int x
= 8; x
> -1; --x
)
1633 execute(send_string(cmd
));
1636 execute(test_ascii_version());
1640 execute(receive_line(buffer
, sizeof(buffer
)));
1641 int val
= atoi(buffer
);
1646 execute(ascii_get_item(key
, "0", true));
1648 /* verify that it doesn't wrap */
1649 execute(send_string(cmd
));
1651 execute(test_ascii_version());
1655 execute(receive_line(buffer
, sizeof(buffer
)));
1657 execute(ascii_get_item(key
, "0", true));
1662 static enum test_return
test_ascii_decr(void)
1664 return test_ascii_decr_impl("test_ascii_decr", false);
1667 static enum test_return
test_ascii_decr_noreply(void)
1669 return test_ascii_decr_impl("test_ascii_decr_noreply", true);
1673 static enum test_return
test_ascii_flush_impl(const char *key
, bool noreply
)
1676 /* Verify that the flush_all command handles unknown options */
1677 /* Bug in the current memcached server! */
1678 execute(send_string("flush_all foo bar\r\n"));
1679 execute(receive_error_response());
1682 execute(ascii_set_item(key
, key
));
1683 execute(ascii_get_item(key
, key
, true));
1687 execute(send_string("flush_all noreply\r\n"));
1688 execute(test_ascii_version());
1692 execute(send_string("flush_all\r\n"));
1693 execute(receive_response("OK\r\n"));
1696 execute(ascii_get_item(key
, key
, false));
1701 static enum test_return
test_ascii_flush(void)
1703 return test_ascii_flush_impl("test_ascii_flush", false);
1706 static enum test_return
test_ascii_flush_noreply(void)
1708 return test_ascii_flush_impl("test_ascii_flush_noreply", true);
1711 static enum test_return
test_ascii_concat_impl(const char *key
,
1722 execute(ascii_set_item(key
, value
));
1730 sprintf(cmd
, "%s %s 0 0 %u%s\r\n%s\r\n",
1731 append
? "append" : "prepend",
1732 key
, (unsigned int)strlen(value
), noreply
? " noreply" : "",
1734 execute(send_string(cmd
));
1737 execute(test_ascii_version());
1739 execute(receive_response("STORED\r\n"));
1741 execute(ascii_get_item(key
, "hello world", true));
1743 sprintf(cmd
, "%s %s_notfound 0 0 %u%s\r\n%s\r\n",
1744 append
? "append" : "prepend",
1745 key
, (unsigned int)strlen(value
), noreply
? " noreply" : "",
1747 execute(send_string(cmd
));
1750 execute(test_ascii_version());
1752 execute(receive_response("NOT_STORED\r\n"));
1757 static enum test_return
test_ascii_append(void)
1759 return test_ascii_concat_impl("test_ascii_append", true, false);
1762 static enum test_return
test_ascii_prepend(void)
1764 return test_ascii_concat_impl("test_ascii_prepend", false, false);
1767 static enum test_return
test_ascii_append_noreply(void)
1769 return test_ascii_concat_impl("test_ascii_append_noreply", true, true);
1772 static enum test_return
test_ascii_prepend_noreply(void)
1774 return test_ascii_concat_impl("test_ascii_prepend_noreply", false, true);
1777 static enum test_return
test_ascii_stat(void)
1779 execute(send_string("stats noreply\r\n"));
1780 execute(receive_error_response());
1781 execute(send_string("stats\r\n"));
1784 execute(receive_line(buffer
, sizeof(buffer
)));
1785 } while (strcmp(buffer
, "END\r\n") != 0);
1787 return TEST_PASS_RECONNECT
;
1790 typedef enum test_return(*TEST_FUNC
)(void);
1794 const char *description
;
1798 struct testcase testcases
[]= {
1799 { "ascii quit", test_ascii_quit
},
1800 { "ascii version", test_ascii_version
},
1801 { "ascii verbosity", test_ascii_verbosity
},
1802 { "ascii set", test_ascii_set
},
1803 { "ascii set noreply", test_ascii_set_noreply
},
1804 { "ascii get", test_ascii_get
},
1805 { "ascii gets", test_ascii_gets
},
1806 { "ascii mget", test_ascii_mget
},
1807 { "ascii flush", test_ascii_flush
},
1808 { "ascii flush noreply", test_ascii_flush_noreply
},
1809 { "ascii add", test_ascii_add
},
1810 { "ascii add noreply", test_ascii_add_noreply
},
1811 { "ascii replace", test_ascii_replace
},
1812 { "ascii replace noreply", test_ascii_replace_noreply
},
1813 { "ascii cas", test_ascii_cas
},
1814 { "ascii cas noreply", test_ascii_cas_noreply
},
1815 { "ascii delete", test_ascii_delete
},
1816 { "ascii delete noreply", test_ascii_delete_noreply
},
1817 { "ascii incr", test_ascii_incr
},
1818 { "ascii incr noreply", test_ascii_incr_noreply
},
1819 { "ascii decr", test_ascii_decr
},
1820 { "ascii decr noreply", test_ascii_decr_noreply
},
1821 { "ascii append", test_ascii_append
},
1822 { "ascii append noreply", test_ascii_append_noreply
},
1823 { "ascii prepend", test_ascii_prepend
},
1824 { "ascii prepend noreply", test_ascii_prepend_noreply
},
1825 { "ascii stat", test_ascii_stat
},
1826 { "binary noop", test_binary_noop
},
1827 { "binary quit", test_binary_quit
},
1828 { "binary quitq", test_binary_quitq
},
1829 { "binary set", test_binary_set
},
1830 { "binary setq", test_binary_setq
},
1831 { "binary flush", test_binary_flush
},
1832 { "binary flushq", test_binary_flushq
},
1833 { "binary add", test_binary_add
},
1834 { "binary addq", test_binary_addq
},
1835 { "binary replace", test_binary_replace
},
1836 { "binary replaceq", test_binary_replaceq
},
1837 { "binary delete", test_binary_delete
},
1838 { "binary deleteq", test_binary_deleteq
},
1839 { "binary get", test_binary_get
},
1840 { "binary getq", test_binary_getq
},
1841 { "binary getk", test_binary_getk
},
1842 { "binary getkq", test_binary_getkq
},
1843 { "binary incr", test_binary_incr
},
1844 { "binary incrq", test_binary_incrq
},
1845 { "binary decr", test_binary_decr
},
1846 { "binary decrq", test_binary_decrq
},
1847 { "binary version", test_binary_version
},
1848 { "binary append", test_binary_append
},
1849 { "binary appendq", test_binary_appendq
},
1850 { "binary prepend", test_binary_prepend
},
1851 { "binary prependq", test_binary_prependq
},
1852 { "binary stat", test_binary_stat
},
1856 int main(int argc
, char **argv
)
1858 static const char * const status_msg
[]= {"[skip]", "[pass]", "[pass]", "[FAIL]"};
1861 const char *hostname
= "localhost";
1862 const char *port
= "11211";
1865 while ((cmd
= getopt(argc
, argv
, "t:vch:p:?")) != EOF
)
1869 timeout
= atoi(optarg
);
1872 fprintf(stderr
, "Invalid timeout. Please specify a number for -t\n");
1876 case 'v': verbose
= true;
1878 case 'c': do_core
= true;
1880 case 'h': hostname
= optarg
;
1882 case 'p': port
= optarg
;
1885 fprintf(stderr
, "Usage: %s [-h hostname] [-p port] [-c] [-v] [-t n]\n"
1886 "\t-c\tGenerate coredump if a test fails\n"
1887 "\t-v\tVerbose test output (print out the assertion)\n"
1888 "\t-t n\tSet the timeout for io-operations to n seconds\n",
1894 sock
= connect_server(hostname
, port
);
1897 fprintf(stderr
, "Failed to connect to <%s:%s>: %s\n",
1898 hostname
, port
, strerror(errno
));
1902 for (int ii
= 0; testcases
[ii
].description
!= NULL
; ++ii
)
1905 fprintf(stdout
, "%-40s", testcases
[ii
].description
);
1908 bool reconnect
= false;
1909 enum test_return ret
= testcases
[ii
].function();
1910 if (ret
== TEST_FAIL
)
1915 fprintf(stderr
, "\n");
1917 else if (ret
== TEST_PASS_RECONNECT
)
1920 fprintf(stderr
, "%s\n", status_msg
[ret
]);
1924 if ((sock
=connect_server(hostname
, port
)) == -1)
1926 fprintf(stderr
, "Failed to connect to <%s:%s>: %s\n",
1927 hostname
, port
, strerror(errno
));
1928 fprintf(stderr
, "%d of %d tests failed\n", failed
, total
);
1936 fprintf(stdout
, "All tests passed\n");
1938 fprintf(stderr
, "%d of %d tests failed\n", failed
, total
);
1940 return (failed
== 0) ? 0 : 1;