1 /* -*- Mode: C; tab-width: 2; c-basic-offset: 2; indent-tabs-mode: nil -*- */
6 #include <sys/socket.h>
9 #include <netinet/in.h>
10 #include <netinet/tcp.h>
24 #include <libmemcached/memcached/protocol_binary.h>
25 #include <libmemcached/byteorder.h>
28 /* /usr/include/netinet/in.h defines macros from ntohs() to _bswap_nn to
29 * optimize the conversion functions, but the prototypes generate warnings
30 * from gcc. The conversion methods isn't the bottleneck for my app, so
31 * just remove the warnings by undef'ing the optimization ..
37 /* Should we generate coredumps when we enounter an error (-c) */
38 static bool do_core
= false;
39 /* connection to the server */
41 /* Should the output from test failures be verbose or quiet? */
42 static bool verbose
= false;
44 /* The number of seconds to wait for an IO-operation */
45 static int timeout
= 2;
48 * Instead of having to cast between the different datatypes we create
49 * a union of all of the different types of pacages we want to send.
50 * A lot of the different commands use the same packet layout, so I'll
51 * just define the different types I need. The typedefs only contain
52 * the header of the message, so we need some space for keys and body
53 * To avoid to have to do multiple writes, lets add a chunk of memory
54 * to use. 1k should be more than enough for header, key and body.
58 protocol_binary_request_no_extras plain
;
59 protocol_binary_request_flush flush
;
60 protocol_binary_request_incr incr
;
61 protocol_binary_request_set set
;
67 protocol_binary_response_no_extras plain
;
68 protocol_binary_response_incr incr
;
69 protocol_binary_response_decr decr
;
75 TEST_SKIP
, TEST_PASS
, TEST_PASS_RECONNECT
, TEST_FAIL
79 * Try to get an addrinfo struct for a given port on a given host
81 static struct addrinfo
*lookuphost(const char *hostname
, const char *port
)
83 struct addrinfo
*ai
= 0;
84 struct addrinfo hints
= {.ai_family
=AF_UNSPEC
,
85 .ai_protocol
=IPPROTO_TCP
,
86 .ai_socktype
=SOCK_STREAM
};
87 int error
= getaddrinfo(hostname
, port
, &hints
, &ai
);
91 if (error
!= EAI_SYSTEM
)
92 fprintf(stderr
, "getaddrinfo(): %s\n", gai_strerror(error
));
94 perror("getaddrinfo()");
101 * Set the socket in nonblocking mode
102 * @return -1 if failure, the socket otherwise
104 static int set_noblock(void)
106 int flags
= fcntl(sock
, F_GETFL
, 0);
109 perror("Failed to get socket flags");
114 if ((flags
& O_NONBLOCK
) != O_NONBLOCK
)
116 if (fcntl(sock
, F_SETFL
, flags
| O_NONBLOCK
) == -1)
118 perror("Failed to set socket to nonblocking mode");
128 * Try to open a connection to the server
129 * @param hostname the name of the server to connect to
130 * @param port the port number (or service) to connect to
131 * @return positive integer if success, -1 otherwise
133 static int connect_server(const char *hostname
, const char *port
)
135 struct addrinfo
*ai
= lookuphost(hostname
, port
);
139 if ((sock
=socket(ai
->ai_family
, ai
->ai_socktype
,
140 ai
->ai_protocol
)) != -1)
142 if (connect(sock
, ai
->ai_addr
, ai
->ai_addrlen
) == -1)
144 fprintf(stderr
, "Failed to connect socket: %s\n",
154 fprintf(stderr
, "Failed to create socket: %s\n", strerror(errno
));
162 static ssize_t
timeout_io_op(int fd
, short direction
, void *buf
, size_t len
)
166 if (direction
== POLLOUT
)
167 ret
= write(fd
, buf
, len
);
169 ret
= read(fd
, buf
, len
);
171 if (ret
== -1 && errno
== EWOULDBLOCK
) {
176 int err
= poll(&fds
, 1, timeout
* 1000);
180 if (direction
== POLLOUT
)
181 ret
= write(fd
, buf
, len
);
183 ret
= read(fd
, buf
, len
);
191 perror("Failed to poll");
200 * Ensure that an expression is true. If it isn't print out a message similar
201 * to assert() and create a coredump if the user wants that. If not an error
202 * message is returned.
205 static enum test_return
ensure(bool val
, const char *expression
, const char *file
, int line
)
210 fprintf(stderr
, "\n%s:%u: %s", file
, line
, expression
);
221 #define verify(expression) do { if (ensure(expression, #expression, __FILE__, __LINE__) == TEST_FAIL) return TEST_FAIL; } while (0)
222 #define execute(expression) do { if (ensure(expression == TEST_PASS, #expression, __FILE__, __LINE__) == TEST_FAIL) return TEST_FAIL; } while (0)
225 * Send a chunk of memory over the socket (retry if the call is iterrupted
227 static enum test_return
retry_write(const void* buf
, size_t len
)
230 const char* ptr
= buf
;
234 size_t num_bytes
= len
- offset
;
235 ssize_t nw
= timeout_io_op(sock
, POLLOUT
, (void*)(ptr
+ offset
), num_bytes
);
237 verify(errno
== EINTR
|| errno
== EAGAIN
);
240 } while (offset
< len
);
246 * Resend a packet to the server (All fields in the command header should
247 * be in network byte order)
249 static enum test_return
resend_packet(command
*cmd
)
251 size_t length
= sizeof (protocol_binary_request_no_extras
) +
252 ntohl(cmd
->plain
.message
.header
.request
.bodylen
);
254 execute(retry_write(cmd
, length
));
259 * Send a command to the server. The command header needs to be updated
260 * to network byte order
262 static enum test_return
send_packet(command
*cmd
)
264 /* Fix the byteorder of the header */
265 cmd
->plain
.message
.header
.request
.keylen
=
266 ntohs(cmd
->plain
.message
.header
.request
.keylen
);
267 cmd
->plain
.message
.header
.request
.bodylen
=
268 ntohl(cmd
->plain
.message
.header
.request
.bodylen
);
269 cmd
->plain
.message
.header
.request
.cas
=
270 ntohll(cmd
->plain
.message
.header
.request
.cas
);
272 execute(resend_packet(cmd
));
277 * Read a fixed length chunk of data from the server
279 static enum test_return
retry_read(void *buf
, size_t len
)
284 ssize_t nr
= timeout_io_op(sock
, POLLIN
, ((char*) buf
) + offset
, len
- offset
);
287 verify(errno
== EINTR
|| errno
== EAGAIN
);
294 } while (offset
< len
);
300 * Receive a response from the server and conver the fields in the header
301 * to local byte order
303 static enum test_return
recv_packet(response
*rsp
)
305 execute(retry_read(rsp
, sizeof (protocol_binary_response_no_extras
)));
307 /* Fix the byte order in the packet header */
308 rsp
->plain
.message
.header
.response
.keylen
=
309 ntohs(rsp
->plain
.message
.header
.response
.keylen
);
310 rsp
->plain
.message
.header
.response
.status
=
311 ntohs(rsp
->plain
.message
.header
.response
.status
);
312 rsp
->plain
.message
.header
.response
.bodylen
=
313 ntohl(rsp
->plain
.message
.header
.response
.bodylen
);
314 rsp
->plain
.message
.header
.response
.cas
=
315 ntohll(rsp
->plain
.message
.header
.response
.cas
);
317 size_t bodysz
= rsp
->plain
.message
.header
.response
.bodylen
;
319 execute(retry_read(rsp
->bytes
+ sizeof (protocol_binary_response_no_extras
), bodysz
));
325 * Create a storage command (add, set, replace etc)
327 * @param cmd destination buffer
328 * @param cc the storage command to create
329 * @param key the key to store
330 * @param keylen the length of the key
331 * @param dta the data to store with the key
332 * @param dtalen the length of the data to store with the key
333 * @param flags the flags to store along with the key
334 * @param exp the expiry time for the key
336 static void storage_command(command
*cmd
,
345 /* all of the storage commands use the same command layout */
346 protocol_binary_request_set
*request
= &cmd
->set
;
348 memset(request
, 0, sizeof (*request
));
349 request
->message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
350 request
->message
.header
.request
.opcode
= cc
;
351 request
->message
.header
.request
.keylen
= (uint16_t)keylen
;
352 request
->message
.header
.request
.extlen
= 8;
353 request
->message
.header
.request
.bodylen
= (uint32_t)(keylen
+ 8 + dtalen
);
354 request
->message
.header
.request
.opaque
= 0xdeadbeef;
355 request
->message
.body
.flags
= flags
;
356 request
->message
.body
.expiration
= exp
;
358 off_t key_offset
= sizeof (protocol_binary_request_no_extras
) + 8;
359 memcpy(cmd
->bytes
+ key_offset
, key
, keylen
);
361 memcpy(cmd
->bytes
+ key_offset
+ keylen
, dta
, dtalen
);
365 * Create a basic command to send to the server
366 * @param cmd destination buffer
367 * @param cc the command to create
368 * @param key the key to store
369 * @param keylen the length of the key
370 * @param dta the data to store with the key
371 * @param dtalen the length of the data to store with the key
373 static void raw_command(command
*cmd
,
380 /* all of the storage commands use the same command layout */
381 memset(cmd
, 0, sizeof (*cmd
));
382 cmd
->plain
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
383 cmd
->plain
.message
.header
.request
.opcode
= cc
;
384 cmd
->plain
.message
.header
.request
.keylen
= (uint16_t)keylen
;
385 cmd
->plain
.message
.header
.request
.bodylen
= (uint32_t)(keylen
+ dtalen
);
386 cmd
->plain
.message
.header
.request
.opaque
= 0xdeadbeef;
388 off_t key_offset
= sizeof (protocol_binary_request_no_extras
);
391 memcpy(cmd
->bytes
+ key_offset
, key
, keylen
);
394 memcpy(cmd
->bytes
+ key_offset
+ keylen
, dta
, dtalen
);
398 * Create the flush command
399 * @param cmd destination buffer
400 * @param cc the command to create (FLUSH/FLUSHQ)
401 * @param exptime when to flush
402 * @param use_extra to force using of the extra field?
404 static void flush_command(command
*cmd
,
405 uint8_t cc
, uint32_t exptime
, bool use_extra
)
407 memset(cmd
, 0, sizeof (cmd
->flush
));
408 cmd
->flush
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
409 cmd
->flush
.message
.header
.request
.opcode
= cc
;
410 cmd
->flush
.message
.header
.request
.opaque
= 0xdeadbeef;
412 if (exptime
!= 0 || use_extra
)
414 cmd
->flush
.message
.header
.request
.extlen
= 4;
415 cmd
->flush
.message
.body
.expiration
= htonl(exptime
);
416 cmd
->flush
.message
.header
.request
.bodylen
= 4;
421 * Create a incr/decr command
422 * @param cc the cmd to create (FLUSH/FLUSHQ)
423 * @param key the key to operate on
424 * @param keylen the number of bytes in the key
425 * @param delta the number to add/subtract
426 * @param initial the initial value if the key doesn't exist
427 * @param exp when the key should expire if it isn't set
429 static void arithmetic_command(command
*cmd
,
437 memset(cmd
, 0, sizeof (cmd
->incr
));
438 cmd
->incr
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
439 cmd
->incr
.message
.header
.request
.opcode
= cc
;
440 cmd
->incr
.message
.header
.request
.keylen
= (uint16_t)keylen
;
441 cmd
->incr
.message
.header
.request
.extlen
= 20;
442 cmd
->incr
.message
.header
.request
.bodylen
= (uint32_t)(keylen
+ 20);
443 cmd
->incr
.message
.header
.request
.opaque
= 0xdeadbeef;
444 cmd
->incr
.message
.body
.delta
= htonll(delta
);
445 cmd
->incr
.message
.body
.initial
= htonll(initial
);
446 cmd
->incr
.message
.body
.expiration
= htonl(exp
);
448 off_t key_offset
= sizeof (protocol_binary_request_no_extras
) + 20;
449 memcpy(cmd
->bytes
+ key_offset
, key
, keylen
);
453 * Validate the response header from the server
454 * @param rsp the response to check
455 * @param cc the expected command
456 * @param status the expected status
458 static enum test_return
do_validate_response_header(response
*rsp
,
459 uint8_t cc
, uint16_t status
)
461 verify(rsp
->plain
.message
.header
.response
.magic
== PROTOCOL_BINARY_RES
);
462 verify(rsp
->plain
.message
.header
.response
.opcode
== cc
);
463 verify(rsp
->plain
.message
.header
.response
.datatype
== PROTOCOL_BINARY_RAW_BYTES
);
464 verify(rsp
->plain
.message
.header
.response
.status
== status
);
465 verify(rsp
->plain
.message
.header
.response
.opaque
== 0xdeadbeef);
467 if (status
== PROTOCOL_BINARY_RESPONSE_SUCCESS
)
470 case PROTOCOL_BINARY_CMD_ADDQ
:
471 case PROTOCOL_BINARY_CMD_APPENDQ
:
472 case PROTOCOL_BINARY_CMD_DECREMENTQ
:
473 case PROTOCOL_BINARY_CMD_DELETEQ
:
474 case PROTOCOL_BINARY_CMD_FLUSHQ
:
475 case PROTOCOL_BINARY_CMD_INCREMENTQ
:
476 case PROTOCOL_BINARY_CMD_PREPENDQ
:
477 case PROTOCOL_BINARY_CMD_QUITQ
:
478 case PROTOCOL_BINARY_CMD_REPLACEQ
:
479 case PROTOCOL_BINARY_CMD_SETQ
:
480 verify("Quiet command shouldn't return on success" == NULL
);
486 case PROTOCOL_BINARY_CMD_ADD
:
487 case PROTOCOL_BINARY_CMD_REPLACE
:
488 case PROTOCOL_BINARY_CMD_SET
:
489 case PROTOCOL_BINARY_CMD_APPEND
:
490 case PROTOCOL_BINARY_CMD_PREPEND
:
491 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
492 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
493 verify(rsp
->plain
.message
.header
.response
.bodylen
== 0);
494 verify(rsp
->plain
.message
.header
.response
.cas
!= 0);
496 case PROTOCOL_BINARY_CMD_FLUSH
:
497 case PROTOCOL_BINARY_CMD_NOOP
:
498 case PROTOCOL_BINARY_CMD_QUIT
:
499 case PROTOCOL_BINARY_CMD_DELETE
:
500 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
501 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
502 verify(rsp
->plain
.message
.header
.response
.bodylen
== 0);
503 verify(rsp
->plain
.message
.header
.response
.cas
== 0);
506 case PROTOCOL_BINARY_CMD_DECREMENT
:
507 case PROTOCOL_BINARY_CMD_INCREMENT
:
508 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
509 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
510 verify(rsp
->plain
.message
.header
.response
.bodylen
== 8);
511 verify(rsp
->plain
.message
.header
.response
.cas
!= 0);
514 case PROTOCOL_BINARY_CMD_STAT
:
515 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
516 /* key and value exists in all packets except in the terminating */
517 verify(rsp
->plain
.message
.header
.response
.cas
== 0);
520 case PROTOCOL_BINARY_CMD_VERSION
:
521 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
522 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
523 verify(rsp
->plain
.message
.header
.response
.bodylen
!= 0);
524 verify(rsp
->plain
.message
.header
.response
.cas
== 0);
527 case PROTOCOL_BINARY_CMD_GET
:
528 case PROTOCOL_BINARY_CMD_GETQ
:
529 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
530 verify(rsp
->plain
.message
.header
.response
.extlen
== 4);
531 verify(rsp
->plain
.message
.header
.response
.cas
!= 0);
534 case PROTOCOL_BINARY_CMD_GETK
:
535 case PROTOCOL_BINARY_CMD_GETKQ
:
536 verify(rsp
->plain
.message
.header
.response
.keylen
!= 0);
537 verify(rsp
->plain
.message
.header
.response
.extlen
== 4);
538 verify(rsp
->plain
.message
.header
.response
.cas
!= 0);
542 /* Undefined command code */
548 verify(rsp
->plain
.message
.header
.response
.cas
== 0);
549 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
550 if (cc
!= PROTOCOL_BINARY_CMD_GETK
)
552 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
559 /* We call verify(validate_response_header), but that macro
560 * expects a boolean expression, and the function returns
561 * an enum.... Let's just create a macro to avoid cluttering
562 * the code with all of the == TEST_PASS ;-)
564 #define validate_response_header(a,b,c) \
565 do_validate_response_header(a,b,c) == TEST_PASS
567 static enum test_return
test_binary_noop(void)
571 raw_command(&cmd
, PROTOCOL_BINARY_CMD_NOOP
, NULL
, 0, NULL
, 0);
572 execute(send_packet(&cmd
));
573 execute(recv_packet(&rsp
));
574 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_NOOP
,
575 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
579 static enum test_return
test_binary_quit_impl(uint8_t cc
)
583 raw_command(&cmd
, cc
, NULL
, 0, NULL
, 0);
585 execute(send_packet(&cmd
));
586 if (cc
== PROTOCOL_BINARY_CMD_QUIT
)
588 execute(recv_packet(&rsp
));
589 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_QUIT
,
590 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
593 /* Socket should be closed now, read should return 0 */
594 verify(timeout_io_op(sock
, POLLIN
, rsp
.bytes
, sizeof(rsp
.bytes
)) == 0);
596 return TEST_PASS_RECONNECT
;
599 static enum test_return
test_binary_quit(void)
601 return test_binary_quit_impl(PROTOCOL_BINARY_CMD_QUIT
);
604 static enum test_return
test_binary_quitq(void)
606 return test_binary_quit_impl(PROTOCOL_BINARY_CMD_QUITQ
);
609 static enum test_return
test_binary_set_impl(const char* key
, uint8_t cc
)
614 uint64_t value
= 0xdeadbeefdeadcafe;
615 storage_command(&cmd
, cc
, key
, strlen(key
), &value
, sizeof (value
), 0, 0);
617 /* set should always work */
618 for (int ii
= 0; ii
< 10; ii
++)
621 execute(send_packet(&cmd
));
623 execute(resend_packet(&cmd
));
625 if (cc
== PROTOCOL_BINARY_CMD_SET
)
627 execute(recv_packet(&rsp
));
628 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
631 execute(test_binary_noop());
635 * We need to get the current CAS id, and at this time we haven't
636 * verified that we have a working get
638 if (cc
== PROTOCOL_BINARY_CMD_SETQ
)
640 cmd
.set
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_SET
;
641 execute(resend_packet(&cmd
));
642 execute(recv_packet(&rsp
));
643 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_SET
,
644 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
645 cmd
.set
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_SETQ
;
648 /* try to set with the correct CAS value */
649 cmd
.plain
.message
.header
.request
.cas
=
650 htonll(rsp
.plain
.message
.header
.response
.cas
);
651 execute(resend_packet(&cmd
));
652 if (cc
== PROTOCOL_BINARY_CMD_SET
)
654 execute(recv_packet(&rsp
));
655 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
658 execute(test_binary_noop());
660 /* try to set with an incorrect CAS value */
661 cmd
.plain
.message
.header
.request
.cas
=
662 htonll(rsp
.plain
.message
.header
.response
.cas
- 1);
663 execute(resend_packet(&cmd
));
664 execute(recv_packet(&rsp
));
665 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS
));
667 return test_binary_noop();
670 static enum test_return
test_binary_set(void)
672 return test_binary_set_impl("test_binary_set", PROTOCOL_BINARY_CMD_SET
);
675 static enum test_return
test_binary_setq(void)
677 return test_binary_set_impl("test_binary_setq", PROTOCOL_BINARY_CMD_SETQ
);
680 static enum test_return
test_binary_add_impl(const char* key
, uint8_t cc
)
684 uint64_t value
= 0xdeadbeefdeadcafe;
685 storage_command(&cmd
, cc
, key
, strlen(key
), &value
, sizeof (value
), 0, 0);
687 /* first add should work, rest of them should fail (even with cas
689 for (int ii
=0; ii
< 10; ii
++)
692 execute(send_packet(&cmd
));
694 execute(resend_packet(&cmd
));
696 if (cc
== PROTOCOL_BINARY_CMD_ADD
|| ii
> 0)
698 uint16_t expected_result
;
700 expected_result
= PROTOCOL_BINARY_RESPONSE_SUCCESS
;
702 expected_result
= PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS
;
704 execute(recv_packet(&rsp
));
705 verify(validate_response_header(&rsp
, cc
, expected_result
));
708 execute(test_binary_noop());
714 static enum test_return
test_binary_add(void)
716 return test_binary_add_impl("test_binary_add", PROTOCOL_BINARY_CMD_ADD
);
719 static enum test_return
test_binary_addq(void)
721 return test_binary_add_impl("test_binary_addq", PROTOCOL_BINARY_CMD_ADDQ
);
724 static enum test_return
binary_set_item(const char *key
, const char *value
)
728 storage_command(&cmd
, PROTOCOL_BINARY_CMD_SET
, key
, strlen(key
),
729 value
, strlen(value
), 0, 0);
730 execute(send_packet(&cmd
));
731 execute(recv_packet(&rsp
));
732 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_SET
,
733 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
737 static enum test_return
test_binary_replace_impl(const char* key
, uint8_t cc
)
741 uint64_t value
= 0xdeadbeefdeadcafe;
742 storage_command(&cmd
, cc
, key
, strlen(key
), &value
, sizeof (value
), 0, 0);
744 /* first replace should fail, successive should succeed (when the
746 for (int ii
= 0; ii
< 10; ii
++)
749 execute(send_packet(&cmd
));
751 execute(resend_packet(&cmd
));
753 if (cc
== PROTOCOL_BINARY_CMD_REPLACE
|| ii
== 0)
755 uint16_t expected_result
;
757 expected_result
=PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
;
759 expected_result
=PROTOCOL_BINARY_RESPONSE_SUCCESS
;
761 execute(recv_packet(&rsp
));
762 verify(validate_response_header(&rsp
, cc
, expected_result
));
765 execute(binary_set_item(key
, key
));
768 execute(test_binary_noop());
771 /* verify that replace with CAS value works! */
772 cmd
.plain
.message
.header
.request
.cas
=
773 htonll(rsp
.plain
.message
.header
.response
.cas
);
774 execute(resend_packet(&cmd
));
776 if (cc
== PROTOCOL_BINARY_CMD_REPLACE
)
778 execute(recv_packet(&rsp
));
779 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
782 execute(test_binary_noop());
784 /* try to set with an incorrect CAS value */
785 cmd
.plain
.message
.header
.request
.cas
=
786 htonll(rsp
.plain
.message
.header
.response
.cas
- 1);
787 execute(resend_packet(&cmd
));
788 execute(recv_packet(&rsp
));
789 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS
));
794 static enum test_return
test_binary_replace(void)
796 return test_binary_replace_impl("test_binary_replace", PROTOCOL_BINARY_CMD_REPLACE
);
799 static enum test_return
test_binary_replaceq(void)
801 return test_binary_replace_impl("test_binary_replaceq", PROTOCOL_BINARY_CMD_REPLACEQ
);
804 static enum test_return
test_binary_delete_impl(const char *key
, uint8_t cc
)
808 raw_command(&cmd
, cc
, key
, strlen(key
), NULL
, 0);
810 /* The delete shouldn't work the first time, because the item isn't there */
811 execute(send_packet(&cmd
));
812 execute(recv_packet(&rsp
));
813 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
));
814 execute(binary_set_item(key
, key
));
816 /* The item should be present now, resend*/
817 execute(resend_packet(&cmd
));
818 if (cc
== PROTOCOL_BINARY_CMD_DELETE
)
820 execute(recv_packet(&rsp
));
821 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
824 execute(test_binary_noop());
829 static enum test_return
test_binary_delete(void)
831 return test_binary_delete_impl("test_binary_delete", PROTOCOL_BINARY_CMD_DELETE
);
834 static enum test_return
test_binary_deleteq(void)
836 return test_binary_delete_impl("test_binary_deleteq", PROTOCOL_BINARY_CMD_DELETEQ
);
839 static enum test_return
test_binary_get_impl(const char *key
, uint8_t cc
)
844 raw_command(&cmd
, cc
, key
, strlen(key
), NULL
, 0);
845 execute(send_packet(&cmd
));
847 if (cc
== PROTOCOL_BINARY_CMD_GET
|| cc
== PROTOCOL_BINARY_CMD_GETK
)
849 execute(recv_packet(&rsp
));
850 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
));
853 execute(test_binary_noop());
855 execute(binary_set_item(key
, key
));
856 execute(resend_packet(&cmd
));
857 execute(recv_packet(&rsp
));
858 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
863 static enum test_return
test_binary_get(void)
865 return test_binary_get_impl("test_binary_get", PROTOCOL_BINARY_CMD_GET
);
868 static enum test_return
test_binary_getk(void)
870 return test_binary_get_impl("test_binary_getk", PROTOCOL_BINARY_CMD_GETK
);
873 static enum test_return
test_binary_getq(void)
875 return test_binary_get_impl("test_binary_getq", PROTOCOL_BINARY_CMD_GETQ
);
878 static enum test_return
test_binary_getkq(void)
880 return test_binary_get_impl("test_binary_getkq", PROTOCOL_BINARY_CMD_GETKQ
);
883 static enum test_return
test_binary_incr_impl(const char* key
, uint8_t cc
)
887 arithmetic_command(&cmd
, cc
, key
, strlen(key
), 1, 0, 0);
890 for (ii
= 0; ii
< 10; ++ii
)
893 execute(send_packet(&cmd
));
895 execute(resend_packet(&cmd
));
897 if (cc
== PROTOCOL_BINARY_CMD_INCREMENT
)
899 execute(recv_packet(&rsp
));
900 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
901 verify(ntohll(rsp
.incr
.message
.body
.value
) == ii
);
904 execute(test_binary_noop());
907 /* @todo add incorrect CAS */
911 static enum test_return
test_binary_incr(void)
913 return test_binary_incr_impl("test_binary_incr", PROTOCOL_BINARY_CMD_INCREMENT
);
916 static enum test_return
test_binary_incrq(void)
918 return test_binary_incr_impl("test_binary_incrq", PROTOCOL_BINARY_CMD_INCREMENTQ
);
921 static enum test_return
test_binary_decr_impl(const char* key
, uint8_t cc
)
925 arithmetic_command(&cmd
, cc
, key
, strlen(key
), 1, 9, 0);
928 for (ii
= 9; ii
> -1; --ii
)
931 execute(send_packet(&cmd
));
933 execute(resend_packet(&cmd
));
935 if (cc
== PROTOCOL_BINARY_CMD_DECREMENT
)
937 execute(recv_packet(&rsp
));
938 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
939 verify(ntohll(rsp
.decr
.message
.body
.value
) == (uint64_t)ii
);
942 execute(test_binary_noop());
945 /* decr 0 should not wrap */
946 execute(resend_packet(&cmd
));
947 if (cc
== PROTOCOL_BINARY_CMD_DECREMENT
)
949 execute(recv_packet(&rsp
));
950 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
951 verify(ntohll(rsp
.decr
.message
.body
.value
) == 0);
955 /* @todo get the value and verify! */
959 /* @todo add incorrect cas */
960 execute(test_binary_noop());
964 static enum test_return
test_binary_decr(void)
966 return test_binary_decr_impl("test_binary_decr",
967 PROTOCOL_BINARY_CMD_DECREMENT
);
970 static enum test_return
test_binary_decrq(void)
972 return test_binary_decr_impl("test_binary_decrq",
973 PROTOCOL_BINARY_CMD_DECREMENTQ
);
976 static enum test_return
test_binary_version(void)
980 raw_command(&cmd
, PROTOCOL_BINARY_CMD_VERSION
, NULL
, 0, NULL
, 0);
982 execute(send_packet(&cmd
));
983 execute(recv_packet(&rsp
));
984 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_VERSION
,
985 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
990 static enum test_return
test_binary_flush_impl(const char *key
, uint8_t cc
)
995 for (int ii
= 0; ii
< 2; ++ii
)
997 execute(binary_set_item(key
, key
));
998 flush_command(&cmd
, cc
, 0, ii
== 0);
999 execute(send_packet(&cmd
));
1001 if (cc
== PROTOCOL_BINARY_CMD_FLUSH
)
1003 execute(recv_packet(&rsp
));
1004 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1007 execute(test_binary_noop());
1009 raw_command(&cmd
, PROTOCOL_BINARY_CMD_GET
, key
, strlen(key
), NULL
, 0);
1010 execute(send_packet(&cmd
));
1011 execute(recv_packet(&rsp
));
1012 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_GET
,
1013 PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
));
1019 static enum test_return
test_binary_flush(void)
1021 return test_binary_flush_impl("test_binary_flush", PROTOCOL_BINARY_CMD_FLUSH
);
1024 static enum test_return
test_binary_flushq(void)
1026 return test_binary_flush_impl("test_binary_flushq", PROTOCOL_BINARY_CMD_FLUSHQ
);
1029 static enum test_return
test_binary_concat_impl(const char *key
, uint8_t cc
)
1035 if (cc
== PROTOCOL_BINARY_CMD_APPEND
|| cc
== PROTOCOL_BINARY_CMD_APPENDQ
)
1040 execute(binary_set_item(key
, value
));
1042 if (cc
== PROTOCOL_BINARY_CMD_APPEND
|| cc
== PROTOCOL_BINARY_CMD_APPENDQ
)
1047 raw_command(&cmd
, cc
, key
, strlen(key
), value
, strlen(value
));
1048 execute(send_packet(&cmd
));
1049 if (cc
== PROTOCOL_BINARY_CMD_APPEND
|| cc
== PROTOCOL_BINARY_CMD_PREPEND
)
1051 execute(recv_packet(&rsp
));
1052 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1055 execute(test_binary_noop());
1057 raw_command(&cmd
, PROTOCOL_BINARY_CMD_GET
, key
, strlen(key
), NULL
, 0);
1058 execute(send_packet(&cmd
));
1059 execute(recv_packet(&rsp
));
1060 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_GET
,
1061 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1062 verify(rsp
.plain
.message
.header
.response
.bodylen
- 4 == 11);
1063 verify(memcmp(rsp
.bytes
+ 28, "hello world", 11) == 0);
1068 static enum test_return
test_binary_append(void)
1070 return test_binary_concat_impl("test_binary_append", PROTOCOL_BINARY_CMD_APPEND
);
1073 static enum test_return
test_binary_prepend(void)
1075 return test_binary_concat_impl("test_binary_prepend", PROTOCOL_BINARY_CMD_PREPEND
);
1078 static enum test_return
test_binary_appendq(void)
1080 return test_binary_concat_impl("test_binary_appendq", PROTOCOL_BINARY_CMD_APPENDQ
);
1083 static enum test_return
test_binary_prependq(void)
1085 return test_binary_concat_impl("test_binary_prependq", PROTOCOL_BINARY_CMD_PREPENDQ
);
1088 static enum test_return
test_binary_stat(void)
1093 raw_command(&cmd
, PROTOCOL_BINARY_CMD_STAT
, NULL
, 0, NULL
, 0);
1094 execute(send_packet(&cmd
));
1098 execute(recv_packet(&rsp
));
1099 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_STAT
,
1100 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1101 } while (rsp
.plain
.message
.header
.response
.keylen
!= 0);
1106 static enum test_return
test_binary_illegal(void)
1114 raw_command(&cmd
, cc
, NULL
, 0, NULL
, 0);
1115 execute(send_packet(&cmd
));
1116 execute(recv_packet(&rsp
));
1117 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_UNKNOWN_COMMAND
));
1121 return TEST_PASS_RECONNECT
;
1124 static enum test_return
send_string(const char *cmd
)
1126 execute(retry_write(cmd
, strlen(cmd
)));
1130 static enum test_return
receive_line(char *buffer
, size_t size
)
1133 while (offset
< size
)
1135 execute(retry_read(buffer
+ offset
, 1));
1136 if (buffer
[offset
] == '\n')
1138 if (offset
+ 1 < size
)
1140 buffer
[offset
+ 1]= '\0';
1152 static enum test_return
receive_response(const char *msg
) {
1154 execute(receive_line(buffer
, sizeof(buffer
)));
1155 verify(strcmp(msg
, buffer
) == 0);
1159 static enum test_return
test_ascii_quit(void)
1161 /* Verify that quit handles unknown options */
1162 execute(send_string("quit foo bar\r\n"));
1163 execute(receive_response("ERROR\r\n"));
1165 /* quit doesn't support noreply */
1166 execute(send_string("quit noreply\r\n"));
1167 execute(receive_response("ERROR\r\n"));
1169 /* Verify that quit works */
1170 execute(send_string("quit\r\n"));
1172 /* Socket should be closed now, read should return 0 */
1174 verify(timeout_io_op(sock
, POLLIN
, buffer
, sizeof(buffer
)) == 0);
1175 return TEST_PASS_RECONNECT
;
1179 static enum test_return
test_ascii_version(void)
1181 /* Verify that version command handles unknown options */
1182 execute(send_string("version foo bar\r\n"));
1183 execute(receive_response("ERROR\r\n"));
1185 /* version doesn't support noreply */
1186 execute(send_string("version noreply\r\n"));
1187 execute(receive_response("ERROR\r\n"));
1189 /* Verify that verify works */
1190 execute(send_string("version\r\n"));
1192 execute(receive_line(buffer
, sizeof(buffer
)));
1193 verify(strncmp(buffer
, "VERSION ", 8) == 0);
1198 static enum test_return
test_ascii_verbosity(void)
1200 /* This command does not adhere to the spec! */
1201 execute(send_string("verbosity foo bar my\r\n"));
1202 execute(receive_response("ERROR\r\n"));
1204 execute(send_string("verbosity noreply\r\n"));
1205 execute(test_ascii_version());
1207 execute(send_string("verbosity 0 noreply\r\n"));
1208 execute(test_ascii_version());
1210 execute(send_string("verbosity\r\n"));
1211 execute(receive_response("ERROR\r\n"));
1213 execute(send_string("verbosity 1\r\n"));
1214 execute(receive_response("OK\r\n"));
1216 execute(send_string("verbosity 0\r\n"));
1217 execute(receive_response("OK\r\n"));
1224 static enum test_return
test_ascii_set_impl(const char* key
, bool noreply
)
1226 /* @todo add tests for bogus format! */
1228 sprintf(buffer
, "set %s 0 0 5%s\r\nvalue\r\n", key
,
1229 noreply
? " noreply" : "");
1230 execute(send_string(buffer
));
1233 execute(receive_response("STORED\r\n"));
1235 return test_ascii_version();
1238 static enum test_return
test_ascii_set(void)
1240 return test_ascii_set_impl("test_ascii_set", false);
1243 static enum test_return
test_ascii_set_noreply(void)
1245 return test_ascii_set_impl("test_ascii_set_noreply", true);
1248 static enum test_return
test_ascii_add_impl(const char* key
, bool noreply
)
1250 /* @todo add tests for bogus format! */
1252 sprintf(buffer
, "add %s 0 0 5%s\r\nvalue\r\n", key
,
1253 noreply
? " noreply" : "");
1254 execute(send_string(buffer
));
1257 execute(receive_response("STORED\r\n"));
1259 execute(send_string(buffer
));
1262 execute(receive_response("NOT_STORED\r\n"));
1264 return test_ascii_version();
1267 static enum test_return
test_ascii_add(void)
1269 return test_ascii_add_impl("test_ascii_add", false);
1272 static enum test_return
test_ascii_add_noreply(void)
1274 return test_ascii_add_impl("test_ascii_add_noreply", true);
1277 static enum test_return
ascii_get_value(const char *key
, const char *value
)
1281 size_t datasize
= strlen(value
);
1283 verify(datasize
< sizeof(buffer
));
1284 execute(receive_line(buffer
, sizeof(buffer
)));
1285 verify(strncmp(buffer
, "VALUE ", 6) == 0);
1286 verify(strncmp(buffer
+ 6, key
, strlen(key
)) == 0);
1287 char *ptr
= buffer
+ 6 + strlen(key
) + 1;
1290 unsigned long val
= strtoul(ptr
, &end
, 10); /* flags */
1293 verify(end
!= NULL
);
1294 val
= strtoul(end
, &end
, 10); /* size */
1296 verify(val
== datasize
);
1297 verify(end
!= NULL
);
1298 while (*end
!= '\n' && isspace(*end
))
1300 verify(*end
== '\n');
1302 execute(retry_read(buffer
, datasize
));
1303 verify(memcmp(buffer
, value
, datasize
) == 0);
1305 execute(retry_read(buffer
, 2));
1306 verify(memcmp(buffer
, "\r\n", 2) == 0);
1311 static enum test_return
ascii_get_item(const char *key
, const char *value
,
1317 datasize
= strlen(value
);
1319 verify(datasize
< sizeof(buffer
));
1320 sprintf(buffer
, "get %s\r\n", key
);
1321 execute(send_string(buffer
));
1324 execute(ascii_get_value(key
, value
));
1326 execute(retry_read(buffer
, 5));
1327 verify(memcmp(buffer
, "END\r\n", 5) == 0);
1332 static enum test_return
ascii_gets_value(const char *key
, const char *value
,
1337 size_t datasize
= strlen(value
);
1339 verify(datasize
< sizeof(buffer
));
1340 execute(receive_line(buffer
, sizeof(buffer
)));
1341 verify(strncmp(buffer
, "VALUE ", 6) == 0);
1342 verify(strncmp(buffer
+ 6, key
, strlen(key
)) == 0);
1343 char *ptr
= buffer
+ 6 + strlen(key
) + 1;
1346 unsigned long val
= strtoul(ptr
, &end
, 10); /* flags */
1349 verify(end
!= NULL
);
1350 val
= strtoul(end
, &end
, 10); /* size */
1352 verify(val
== datasize
);
1353 verify(end
!= NULL
);
1354 *cas
= strtoul(end
, &end
, 10); /* cas */
1356 verify(val
== datasize
);
1357 verify(end
!= NULL
);
1359 while (*end
!= '\n' && isspace(*end
))
1361 verify(*end
== '\n');
1363 execute(retry_read(buffer
, datasize
));
1364 verify(memcmp(buffer
, value
, datasize
) == 0);
1366 execute(retry_read(buffer
, 2));
1367 verify(memcmp(buffer
, "\r\n", 2) == 0);
1372 static enum test_return
ascii_gets_item(const char *key
, const char *value
,
1373 bool exist
, unsigned long *cas
)
1378 datasize
= strlen(value
);
1380 verify(datasize
< sizeof(buffer
));
1381 sprintf(buffer
, "gets %s\r\n", key
);
1382 execute(send_string(buffer
));
1385 execute(ascii_gets_value(key
, value
, cas
));
1387 execute(retry_read(buffer
, 5));
1388 verify(memcmp(buffer
, "END\r\n", 5) == 0);
1393 static enum test_return
ascii_set_item(const char *key
, const char *value
)
1396 size_t len
= strlen(value
);
1397 sprintf(buffer
, "set %s 0 0 %u\r\n", key
, (unsigned int)len
);
1398 execute(send_string(buffer
));
1399 execute(retry_write(value
, len
));
1400 execute(send_string("\r\n"));
1401 execute(receive_response("STORED\r\n"));
1405 static enum test_return
test_ascii_replace_impl(const char* key
, bool noreply
)
1408 sprintf(buffer
, "replace %s 0 0 5%s\r\nvalue\r\n", key
,
1409 noreply
? " noreply" : "");
1410 execute(send_string(buffer
));
1413 execute(test_ascii_version());
1415 execute(receive_response("NOT_STORED\r\n"));
1417 execute(ascii_set_item(key
, "value"));
1418 execute(ascii_get_item(key
, "value", true));
1421 execute(send_string(buffer
));
1424 execute(test_ascii_version());
1426 execute(receive_response("STORED\r\n"));
1428 return test_ascii_version();
1431 static enum test_return
test_ascii_replace(void)
1433 return test_ascii_replace_impl("test_ascii_replace", false);
1436 static enum test_return
test_ascii_replace_noreply(void)
1438 return test_ascii_replace_impl("test_ascii_replace_noreply", true);
1441 static enum test_return
test_ascii_cas_impl(const char* key
, bool noreply
)
1446 execute(ascii_set_item(key
, "value"));
1447 execute(ascii_gets_item(key
, "value", true, &cas
));
1449 sprintf(buffer
, "cas %s 0 0 6 %lu%s\r\nvalue2\r\n", key
, cas
,
1450 noreply
? " noreply" : "");
1451 execute(send_string(buffer
));
1454 execute(test_ascii_version());
1456 execute(receive_response("STORED\r\n"));
1458 /* reexecute the same command should fail due to illegal cas */
1459 execute(send_string(buffer
));
1462 execute(test_ascii_version());
1464 execute(receive_response("EXISTS\r\n"));
1466 return test_ascii_version();
1469 static enum test_return
test_ascii_cas(void)
1471 return test_ascii_cas_impl("test_ascii_cas", false);
1474 static enum test_return
test_ascii_cas_noreply(void)
1476 return test_ascii_cas_impl("test_ascii_cas_noreply", true);
1479 static enum test_return
test_ascii_delete_impl(const char *key
, bool noreply
)
1481 execute(ascii_set_item(key
, "value"));
1483 execute(send_string("delete\r\n"));
1484 execute(receive_response("ERROR\r\n"));
1485 /* BUG: the server accepts delete a b */
1486 execute(send_string("delete a b c d e\r\n"));
1487 execute(receive_response("ERROR\r\n"));
1490 sprintf(buffer
, "delete %s%s\r\n", key
, noreply
? " noreply" : "");
1491 execute(send_string(buffer
));
1494 execute(test_ascii_version());
1496 execute(receive_response("DELETED\r\n"));
1498 execute(ascii_get_item(key
, "value", false));
1499 execute(send_string(buffer
));
1501 execute(test_ascii_version());
1503 execute(receive_response("NOT_FOUND\r\n"));
1508 static enum test_return
test_ascii_delete(void)
1510 return test_ascii_delete_impl("test_ascii_delete", false);
1513 static enum test_return
test_ascii_delete_noreply(void)
1515 return test_ascii_delete_impl("test_ascii_delete_noreply", true);
1518 static enum test_return
test_ascii_get(void)
1520 execute(ascii_set_item("test_ascii_get", "value"));
1522 execute(send_string("get\r\n"));
1523 execute(receive_response("ERROR\r\n"));
1524 execute(ascii_get_item("test_ascii_get", "value", true));
1525 execute(ascii_get_item("test_ascii_get_notfound", "value", false));
1530 static enum test_return
test_ascii_gets(void)
1532 execute(ascii_set_item("test_ascii_gets", "value"));
1534 execute(send_string("gets\r\n"));
1535 execute(receive_response("ERROR\r\n"));
1537 execute(ascii_gets_item("test_ascii_gets", "value", true, &cas
));
1538 execute(ascii_gets_item("test_ascii_gets_notfound", "value", false, &cas
));
1543 static enum test_return
test_ascii_mget(void)
1545 execute(ascii_set_item("test_ascii_mget1", "value"));
1546 execute(ascii_set_item("test_ascii_mget2", "value"));
1547 execute(ascii_set_item("test_ascii_mget3", "value"));
1548 execute(ascii_set_item("test_ascii_mget4", "value"));
1549 execute(ascii_set_item("test_ascii_mget5", "value"));
1551 execute(send_string("get test_ascii_mget1 test_ascii_mget2 test_ascii_mget3 "
1552 "test_ascii_mget4 test_ascii_mget5 "
1553 "test_ascii_mget6\r\n"));
1554 execute(ascii_get_value("test_ascii_mget1", "value"));
1555 execute(ascii_get_value("test_ascii_mget2", "value"));
1556 execute(ascii_get_value("test_ascii_mget3", "value"));
1557 execute(ascii_get_value("test_ascii_mget4", "value"));
1558 execute(ascii_get_value("test_ascii_mget5", "value"));
1561 execute(retry_read(buffer
, 5));
1562 verify(memcmp(buffer
, "END\r\n", 5) == 0);
1566 static enum test_return
test_ascii_incr_impl(const char* key
, bool noreply
)
1569 sprintf(cmd
, "incr %s 1%s\r\n", key
, noreply
? " noreply" : "");
1571 execute(ascii_set_item(key
, "0"));
1572 for (int x
= 1; x
< 11; ++x
)
1574 execute(send_string(cmd
));
1577 execute(test_ascii_version());
1581 execute(receive_line(buffer
, sizeof(buffer
)));
1582 int val
= atoi(buffer
);
1587 execute(ascii_get_item(key
, "10", true));
1592 static enum test_return
test_ascii_incr(void)
1594 return test_ascii_incr_impl("test_ascii_incr", false);
1597 static enum test_return
test_ascii_incr_noreply(void)
1599 return test_ascii_incr_impl("test_ascii_incr_noreply", true);
1602 static enum test_return
test_ascii_decr_impl(const char* key
, bool noreply
)
1605 sprintf(cmd
, "decr %s 1%s\r\n", key
, noreply
? " noreply" : "");
1607 execute(ascii_set_item(key
, "9"));
1608 for (int x
= 8; x
> -1; --x
)
1610 execute(send_string(cmd
));
1613 execute(test_ascii_version());
1617 execute(receive_line(buffer
, sizeof(buffer
)));
1618 int val
= atoi(buffer
);
1623 execute(ascii_get_item(key
, "0", true));
1625 /* verify that it doesn't wrap */
1626 execute(send_string(cmd
));
1628 execute(test_ascii_version());
1632 execute(receive_line(buffer
, sizeof(buffer
)));
1634 execute(ascii_get_item(key
, "0", true));
1639 static enum test_return
test_ascii_decr(void)
1641 return test_ascii_decr_impl("test_ascii_decr", false);
1644 static enum test_return
test_ascii_decr_noreply(void)
1646 return test_ascii_decr_impl("test_ascii_decr_noreply", true);
1650 static enum test_return
test_ascii_flush_impl(const char *key
, bool noreply
)
1653 /* Verify that the flush_all command handles unknown options */
1654 /* Bug in the current memcached server! */
1655 execute(send_string("flush_all foo bar\r\n"));
1656 execute(receive_response("ERROR\r\n"));
1659 execute(ascii_set_item(key
, key
));
1660 execute(ascii_get_item(key
, key
, true));
1664 execute(send_string("flush_all noreply\r\n"));
1665 execute(test_ascii_version());
1669 execute(send_string("flush_all\r\n"));
1670 execute(receive_response("OK\r\n"));
1673 execute(ascii_get_item(key
, key
, false));
1678 static enum test_return
test_ascii_flush(void)
1680 return test_ascii_flush_impl("test_ascii_flush", false);
1683 static enum test_return
test_ascii_flush_noreply(void)
1685 return test_ascii_flush_impl("test_ascii_flush_noreply", true);
1688 static enum test_return
test_ascii_concat_impl(const char *key
,
1699 execute(ascii_set_item(key
, value
));
1707 sprintf(cmd
, "%s %s 0 0 %u%s\r\n%s\r\n",
1708 append
? "append" : "prepend",
1709 key
, (unsigned int)strlen(value
), noreply
? " noreply" : "",
1711 execute(send_string(cmd
));
1714 execute(test_ascii_version());
1716 execute(receive_response("STORED\r\n"));
1718 execute(ascii_get_item(key
, "hello world", true));
1720 sprintf(cmd
, "%s %s_notfound 0 0 %u%s\r\n%s\r\n",
1721 append
? "append" : "prepend",
1722 key
, (unsigned int)strlen(value
), noreply
? " noreply" : "",
1724 execute(send_string(cmd
));
1727 execute(test_ascii_version());
1729 execute(receive_response("NOT_STORED\r\n"));
1734 static enum test_return
test_ascii_append(void)
1736 return test_ascii_concat_impl("test_ascii_append", true, false);
1739 static enum test_return
test_ascii_prepend(void)
1741 return test_ascii_concat_impl("test_ascii_prepend", false, false);
1744 static enum test_return
test_ascii_append_noreply(void)
1746 return test_ascii_concat_impl("test_ascii_append_noreply", true, true);
1749 static enum test_return
test_ascii_prepend_noreply(void)
1751 return test_ascii_concat_impl("test_ascii_prepend_noreply", false, true);
1754 static enum test_return
test_ascii_stat(void)
1756 execute(send_string("stats noreply\r\n"));
1757 execute(receive_response("ERROR\r\n"));
1758 execute(send_string("stats\r\n"));
1761 execute(receive_line(buffer
, sizeof(buffer
)));
1762 } while (strcmp(buffer
, "END\r\n") != 0);
1764 return TEST_PASS_RECONNECT
;
1767 typedef enum test_return(*TEST_FUNC
)(void);
1771 const char *description
;
1775 struct testcase testcases
[]= {
1776 { "ascii quit", test_ascii_quit
},
1777 { "ascii version", test_ascii_version
},
1778 { "ascii verbosity", test_ascii_verbosity
},
1779 { "ascii set", test_ascii_set
},
1780 { "ascii set noreply", test_ascii_set_noreply
},
1781 { "ascii get", test_ascii_get
},
1782 { "ascii gets", test_ascii_gets
},
1783 { "ascii mget", test_ascii_mget
},
1784 { "ascii flush", test_ascii_flush
},
1785 { "ascii flush noreply", test_ascii_flush_noreply
},
1786 { "ascii add", test_ascii_add
},
1787 { "ascii add noreply", test_ascii_add_noreply
},
1788 { "ascii replace", test_ascii_replace
},
1789 { "ascii replace noreply", test_ascii_replace_noreply
},
1790 { "ascii cas", test_ascii_cas
},
1791 { "ascii cas noreply", test_ascii_cas_noreply
},
1792 { "ascii delete", test_ascii_delete
},
1793 { "ascii delete noreply", test_ascii_delete_noreply
},
1794 { "ascii incr", test_ascii_incr
},
1795 { "ascii incr noreply", test_ascii_incr_noreply
},
1796 { "ascii decr", test_ascii_decr
},
1797 { "ascii decr noreply", test_ascii_decr_noreply
},
1798 { "ascii append", test_ascii_append
},
1799 { "ascii append noreply", test_ascii_append_noreply
},
1800 { "ascii prepend", test_ascii_prepend
},
1801 { "ascii prepend noreply", test_ascii_prepend_noreply
},
1802 { "ascii stat", test_ascii_stat
},
1803 { "binary noop", test_binary_noop
},
1804 { "binary quit", test_binary_quit
},
1805 { "binary quitq", test_binary_quitq
},
1806 { "binary set", test_binary_set
},
1807 { "binary setq", test_binary_setq
},
1808 { "binary flush", test_binary_flush
},
1809 { "binary flushq", test_binary_flushq
},
1810 { "binary add", test_binary_add
},
1811 { "binary addq", test_binary_addq
},
1812 { "binary replace", test_binary_replace
},
1813 { "binary replaceq", test_binary_replaceq
},
1814 { "binary delete", test_binary_delete
},
1815 { "binary deleteq", test_binary_deleteq
},
1816 { "binary get", test_binary_get
},
1817 { "binary getq", test_binary_getq
},
1818 { "binary getk", test_binary_getk
},
1819 { "binary getkq", test_binary_getkq
},
1820 { "binary incr", test_binary_incr
},
1821 { "binary incrq", test_binary_incrq
},
1822 { "binary decr", test_binary_decr
},
1823 { "binary decrq", test_binary_decrq
},
1824 { "binary version", test_binary_version
},
1825 { "binary append", test_binary_append
},
1826 { "binary appendq", test_binary_appendq
},
1827 { "binary prepend", test_binary_prepend
},
1828 { "binary prependq", test_binary_prependq
},
1829 { "binary stat", test_binary_stat
},
1830 { "binary illegal", test_binary_illegal
},
1834 int main(int argc
, char **argv
)
1836 static const char * const status_msg
[]= {"[skip]", "[pass]", "[pass]", "[FAIL]"};
1839 const char *hostname
= "localhost";
1840 const char *port
= "11211";
1843 while ((cmd
= getopt(argc
, argv
, "t:vch:p:?")) != EOF
)
1847 timeout
= atoi(optarg
);
1850 fprintf(stderr
, "Invalid timeout. Please specify a number for -t\n");
1854 case 'v': verbose
= true;
1856 case 'c': do_core
= true;
1858 case 'h': hostname
= optarg
;
1860 case 'p': port
= optarg
;
1863 fprintf(stderr
, "Usage: %s [-h hostname] [-p port] [-c] [-v] [-t n]\n"
1864 "\t-c\tGenerate coredump if a test fails\n"
1865 "\t-v\tVerbose test output (print out the assertion)\n"
1866 "\t-t n\tSet the timeout for io-operations to n seconds\n",
1872 sock
= connect_server(hostname
, port
);
1875 fprintf(stderr
, "Failed to connect to <%s:%s>: %s\n",
1876 hostname
, port
, strerror(errno
));
1880 for (int ii
= 0; testcases
[ii
].description
!= NULL
; ++ii
)
1883 fprintf(stdout
, "%-40s", testcases
[ii
].description
);
1886 bool reconnect
= false;
1887 enum test_return ret
= testcases
[ii
].function();
1888 if (ret
== TEST_FAIL
)
1893 fprintf(stderr
, "\n");
1895 else if (ret
== TEST_PASS_RECONNECT
)
1898 fprintf(stderr
, "%s\n", status_msg
[ret
]);
1902 if ((sock
=connect_server(hostname
, port
)) == -1)
1904 fprintf(stderr
, "Failed to connect to <%s:%s>: %s\n",
1905 hostname
, port
, strerror(errno
));
1906 fprintf(stderr
, "%d of %d tests failed\n", failed
, total
);
1914 fprintf(stdout
, "All tests passed\n");
1916 fprintf(stderr
, "%d of %d tests failed\n", failed
, total
);
1918 return (failed
== 0) ? 0 : 1;