2 * Copyright (C) 2006-2009 Brian Aker
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
12 /* -*- Mode: C; tab-width: 2; c-basic-offset: 2; indent-tabs-mode: nil -*- */
17 #include <sys/types.h>
30 #include <libmemcached/memcached.h>
31 #include <libmemcached/memcached/protocol_binary.h>
32 #include <libmemcached/byteorder.h>
33 #include "utilities.h"
36 /* /usr/include/netinet/in.h defines macros from ntohs() to _bswap_nn to
37 * optimize the conversion functions, but the prototypes generate warnings
38 * from gcc. The conversion methods isn't the bottleneck for my app, so
39 * just remove the warnings by undef'ing the optimization ..
45 /* Should we generate coredumps when we enounter an error (-c) */
46 static bool do_core
= false;
47 /* connection to the server */
48 static memcached_socket_t sock
;
49 /* Should the output from test failures be verbose or quiet? */
50 static bool verbose
= false;
52 /* The number of seconds to wait for an IO-operation */
53 static int timeout
= 2;
56 * Instead of having to cast between the different datatypes we create
57 * a union of all of the different types of pacages we want to send.
58 * A lot of the different commands use the same packet layout, so I'll
59 * just define the different types I need. The typedefs only contain
60 * the header of the message, so we need some space for keys and body
61 * To avoid to have to do multiple writes, lets add a chunk of memory
62 * to use. 1k should be more than enough for header, key and body.
66 protocol_binary_request_no_extras plain
;
67 protocol_binary_request_flush flush
;
68 protocol_binary_request_incr incr
;
69 protocol_binary_request_set set
;
75 protocol_binary_response_no_extras plain
;
76 protocol_binary_response_incr incr
;
77 protocol_binary_response_decr decr
;
83 TEST_SKIP
, TEST_PASS
, TEST_PASS_RECONNECT
, TEST_FAIL
87 * Try to get an addrinfo struct for a given port on a given host
89 static struct addrinfo
*lookuphost(const char *hostname
, const char *port
)
91 struct addrinfo
*ai
= 0;
92 struct addrinfo hints
;
93 memset(&hints
, 0, sizeof(struct addrinfo
));
94 hints
.ai_family
=AF_UNSPEC
;
95 hints
.ai_protocol
=IPPROTO_TCP
;
96 hints
.ai_socktype
=SOCK_STREAM
;
98 int error
= getaddrinfo(hostname
, port
, &hints
, &ai
);
101 if (error
!= EAI_SYSTEM
)
102 fprintf(stderr
, "getaddrinfo(): %s\n", gai_strerror(error
));
104 perror("getaddrinfo()");
111 * Set the socket in nonblocking mode
112 * @return -1 if failure, the socket otherwise
114 static memcached_socket_t
set_noblock(void)
118 if (ioctlsocket(sock
, FIONBIO
, &arg
) == SOCKET_ERROR
)
120 perror("Failed to set nonblocking io");
122 return INVALID_SOCKET
;
125 int flags
= fcntl(sock
, F_GETFL
, 0);
128 perror("Failed to get socket flags");
130 return INVALID_SOCKET
;
133 if ((flags
& O_NONBLOCK
) != O_NONBLOCK
)
135 if (fcntl(sock
, F_SETFL
, flags
| O_NONBLOCK
) == -1)
137 perror("Failed to set socket to nonblocking mode");
139 return INVALID_SOCKET
;
147 * Try to open a connection to the server
148 * @param hostname the name of the server to connect to
149 * @param port the port number (or service) to connect to
150 * @return positive integer if success, -1 otherwise
152 static memcached_socket_t
connect_server(const char *hostname
, const char *port
)
154 struct addrinfo
*ai
= lookuphost(hostname
, port
);
155 sock
= INVALID_SOCKET
;
158 if ((sock
= socket(ai
->ai_family
, ai
->ai_socktype
,
159 ai
->ai_protocol
)) != INVALID_SOCKET
)
161 if (connect(sock
, ai
->ai_addr
, ai
->ai_addrlen
) == SOCKET_ERROR
)
163 fprintf(stderr
, "Failed to connect socket: %s\n",
164 strerror(get_socket_errno()));
166 sock
= INVALID_SOCKET
;
174 fprintf(stderr
, "Failed to create socket: %s\n",
175 strerror(get_socket_errno()));
183 static ssize_t
timeout_io_op(memcached_socket_t fd
, short direction
, void *buf
, size_t len
)
187 if (direction
== POLLOUT
)
189 ret
= send(fd
, buf
, len
, 0);
193 ret
= recv(fd
, buf
, len
, 0);
196 if (ret
== SOCKET_ERROR
&& get_socket_errno() == EWOULDBLOCK
)
199 memset(&fds
, 0, sizeof(struct pollfd
));
200 fds
.events
= direction
;
203 int err
= poll(&fds
, 1, timeout
* 1000);
206 if (direction
== POLLOUT
)
208 ret
= send(fd
, buf
, len
, 0);
212 ret
= recv(fd
, buf
, len
, 0);
221 perror("Failed to poll");
230 * Ensure that an expression is true. If it isn't print out a message similar
231 * to assert() and create a coredump if the user wants that. If not an error
232 * message is returned.
235 static enum test_return
ensure(bool val
, const char *expression
, const char *file
, int line
)
240 fprintf(stderr
, "\n%s:%d: %s", file
, line
, expression
);
251 #define verify(expression) do { if (ensure(expression, #expression, __FILE__, __LINE__) == TEST_FAIL) return TEST_FAIL; } while (0)
252 #define execute(expression) do { if (ensure(expression == TEST_PASS, #expression, __FILE__, __LINE__) == TEST_FAIL) return TEST_FAIL; } while (0)
255 * Send a chunk of memory over the socket (retry if the call is iterrupted
257 static enum test_return
retry_write(const void* buf
, size_t len
)
260 const char* ptr
= static_cast<const char *>(buf
);
264 size_t num_bytes
= len
- offset
;
265 ssize_t nw
= timeout_io_op(sock
, POLLOUT
, (void*)(ptr
+ offset
), num_bytes
);
267 verify(get_socket_errno() == EINTR
|| get_socket_errno() == EAGAIN
);
270 } while (offset
< len
);
276 * Resend a packet to the server (All fields in the command header should
277 * be in network byte order)
279 static enum test_return
resend_packet(command
*cmd
)
281 size_t length
= sizeof (protocol_binary_request_no_extras
) +
282 ntohl(cmd
->plain
.message
.header
.request
.bodylen
);
284 execute(retry_write(cmd
, length
));
289 * Send a command to the server. The command header needs to be updated
290 * to network byte order
292 static enum test_return
send_packet(command
*cmd
)
294 /* Fix the byteorder of the header */
295 cmd
->plain
.message
.header
.request
.keylen
=
296 ntohs(cmd
->plain
.message
.header
.request
.keylen
);
297 cmd
->plain
.message
.header
.request
.bodylen
=
298 ntohl(cmd
->plain
.message
.header
.request
.bodylen
);
299 cmd
->plain
.message
.header
.request
.cas
=
300 ntohll(cmd
->plain
.message
.header
.request
.cas
);
302 execute(resend_packet(cmd
));
307 * Read a fixed length chunk of data from the server
309 static enum test_return
retry_read(void *buf
, size_t len
)
314 ssize_t nr
= timeout_io_op(sock
, POLLIN
, ((char*) buf
) + offset
, len
- offset
);
317 fprintf(stderr
, "Errno: %d %s\n", get_socket_errno(), strerror(errno
));
318 verify(get_socket_errno() == EINTR
|| get_socket_errno() == EAGAIN
);
325 } while (offset
< len
);
331 * Receive a response from the server and conver the fields in the header
332 * to local byte order
334 static enum test_return
recv_packet(response
*rsp
)
336 execute(retry_read(rsp
, sizeof(protocol_binary_response_no_extras
)));
338 /* Fix the byte order in the packet header */
339 rsp
->plain
.message
.header
.response
.keylen
=
340 ntohs(rsp
->plain
.message
.header
.response
.keylen
);
341 rsp
->plain
.message
.header
.response
.status
=
342 ntohs(rsp
->plain
.message
.header
.response
.status
);
343 rsp
->plain
.message
.header
.response
.bodylen
=
344 ntohl(rsp
->plain
.message
.header
.response
.bodylen
);
345 rsp
->plain
.message
.header
.response
.cas
=
346 ntohll(rsp
->plain
.message
.header
.response
.cas
);
348 size_t bodysz
= rsp
->plain
.message
.header
.response
.bodylen
;
350 execute(retry_read(rsp
->bytes
+ sizeof (protocol_binary_response_no_extras
), bodysz
));
356 * Create a storage command (add, set, replace etc)
358 * @param cmd destination buffer
359 * @param cc the storage command to create
360 * @param key the key to store
361 * @param keylen the length of the key
362 * @param dta the data to store with the key
363 * @param dtalen the length of the data to store with the key
364 * @param flags the flags to store along with the key
365 * @param exptime the expiry time for the key
367 static void storage_command(command
*cmd
,
376 /* all of the storage commands use the same command layout */
377 protocol_binary_request_set
*request
= &cmd
->set
;
379 memset(request
, 0, sizeof (*request
));
380 request
->message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
381 request
->message
.header
.request
.opcode
= cc
;
382 request
->message
.header
.request
.keylen
= (uint16_t)keylen
;
383 request
->message
.header
.request
.extlen
= 8;
384 request
->message
.header
.request
.bodylen
= (uint32_t)(keylen
+ 8 + dtalen
);
385 request
->message
.header
.request
.opaque
= 0xdeadbeef;
386 request
->message
.body
.flags
= flags
;
387 request
->message
.body
.expiration
= exptime
;
389 off_t key_offset
= sizeof (protocol_binary_request_no_extras
) + 8;
390 memcpy(cmd
->bytes
+ key_offset
, key
, keylen
);
392 memcpy(cmd
->bytes
+ key_offset
+ keylen
, dta
, dtalen
);
396 * Create a basic command to send to the server
397 * @param cmd destination buffer
398 * @param cc the command to create
399 * @param key the key to store
400 * @param keylen the length of the key
401 * @param dta the data to store with the key
402 * @param dtalen the length of the data to store with the key
404 static void raw_command(command
*cmd
,
411 /* all of the storage commands use the same command layout */
412 memset(cmd
, 0, sizeof (*cmd
));
413 cmd
->plain
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
414 cmd
->plain
.message
.header
.request
.opcode
= cc
;
415 cmd
->plain
.message
.header
.request
.keylen
= (uint16_t)keylen
;
416 cmd
->plain
.message
.header
.request
.bodylen
= (uint32_t)(keylen
+ dtalen
);
417 cmd
->plain
.message
.header
.request
.opaque
= 0xdeadbeef;
419 off_t key_offset
= sizeof (protocol_binary_request_no_extras
);
422 memcpy(cmd
->bytes
+ key_offset
, key
, keylen
);
425 memcpy(cmd
->bytes
+ key_offset
+ keylen
, dta
, dtalen
);
429 * Create the flush command
430 * @param cmd destination buffer
431 * @param cc the command to create (FLUSH/FLUSHQ)
432 * @param exptime when to flush
433 * @param use_extra to force using of the extra field?
435 static void flush_command(command
*cmd
,
436 uint8_t cc
, uint32_t exptime
, bool use_extra
)
438 memset(cmd
, 0, sizeof (cmd
->flush
));
439 cmd
->flush
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
440 cmd
->flush
.message
.header
.request
.opcode
= cc
;
441 cmd
->flush
.message
.header
.request
.opaque
= 0xdeadbeef;
443 if (exptime
!= 0 || use_extra
)
445 cmd
->flush
.message
.header
.request
.extlen
= 4;
446 cmd
->flush
.message
.body
.expiration
= htonl(exptime
);
447 cmd
->flush
.message
.header
.request
.bodylen
= 4;
452 * Create a incr/decr command
453 * @param cc the cmd to create (FLUSH/FLUSHQ)
454 * @param key the key to operate on
455 * @param keylen the number of bytes in the key
456 * @param delta the number to add/subtract
457 * @param initial the initial value if the key doesn't exist
458 * @param exptime when the key should expire if it isn't set
460 static void arithmetic_command(command
*cmd
,
468 memset(cmd
, 0, sizeof (cmd
->incr
));
469 cmd
->incr
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
470 cmd
->incr
.message
.header
.request
.opcode
= cc
;
471 cmd
->incr
.message
.header
.request
.keylen
= (uint16_t)keylen
;
472 cmd
->incr
.message
.header
.request
.extlen
= 20;
473 cmd
->incr
.message
.header
.request
.bodylen
= (uint32_t)(keylen
+ 20);
474 cmd
->incr
.message
.header
.request
.opaque
= 0xdeadbeef;
475 cmd
->incr
.message
.body
.delta
= htonll(delta
);
476 cmd
->incr
.message
.body
.initial
= htonll(initial
);
477 cmd
->incr
.message
.body
.expiration
= htonl(exptime
);
479 off_t key_offset
= sizeof (protocol_binary_request_no_extras
) + 20;
480 memcpy(cmd
->bytes
+ key_offset
, key
, keylen
);
484 * Validate the response header from the server
485 * @param rsp the response to check
486 * @param cc the expected command
487 * @param status the expected status
489 static enum test_return
do_validate_response_header(response
*rsp
,
490 uint8_t cc
, uint16_t status
)
492 verify(rsp
->plain
.message
.header
.response
.magic
== PROTOCOL_BINARY_RES
);
493 verify(rsp
->plain
.message
.header
.response
.opcode
== cc
);
494 verify(rsp
->plain
.message
.header
.response
.datatype
== PROTOCOL_BINARY_RAW_BYTES
);
495 verify(rsp
->plain
.message
.header
.response
.status
== status
);
496 verify(rsp
->plain
.message
.header
.response
.opaque
== 0xdeadbeef);
498 if (status
== PROTOCOL_BINARY_RESPONSE_SUCCESS
)
501 case PROTOCOL_BINARY_CMD_ADDQ
:
502 case PROTOCOL_BINARY_CMD_APPENDQ
:
503 case PROTOCOL_BINARY_CMD_DECREMENTQ
:
504 case PROTOCOL_BINARY_CMD_DELETEQ
:
505 case PROTOCOL_BINARY_CMD_FLUSHQ
:
506 case PROTOCOL_BINARY_CMD_INCREMENTQ
:
507 case PROTOCOL_BINARY_CMD_PREPENDQ
:
508 case PROTOCOL_BINARY_CMD_QUITQ
:
509 case PROTOCOL_BINARY_CMD_REPLACEQ
:
510 case PROTOCOL_BINARY_CMD_SETQ
:
511 verify("Quiet command shouldn't return on success" == NULL
);
517 case PROTOCOL_BINARY_CMD_ADD
:
518 case PROTOCOL_BINARY_CMD_REPLACE
:
519 case PROTOCOL_BINARY_CMD_SET
:
520 case PROTOCOL_BINARY_CMD_APPEND
:
521 case PROTOCOL_BINARY_CMD_PREPEND
:
522 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
523 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
524 verify(rsp
->plain
.message
.header
.response
.bodylen
== 0);
525 verify(rsp
->plain
.message
.header
.response
.cas
!= 0);
527 case PROTOCOL_BINARY_CMD_FLUSH
:
528 case PROTOCOL_BINARY_CMD_NOOP
:
529 case PROTOCOL_BINARY_CMD_QUIT
:
530 case PROTOCOL_BINARY_CMD_DELETE
:
531 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
532 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
533 verify(rsp
->plain
.message
.header
.response
.bodylen
== 0);
534 verify(rsp
->plain
.message
.header
.response
.cas
== 0);
537 case PROTOCOL_BINARY_CMD_DECREMENT
:
538 case PROTOCOL_BINARY_CMD_INCREMENT
:
539 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
540 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
541 verify(rsp
->plain
.message
.header
.response
.bodylen
== 8);
542 verify(rsp
->plain
.message
.header
.response
.cas
!= 0);
545 case PROTOCOL_BINARY_CMD_STAT
:
546 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
547 /* key and value exists in all packets except in the terminating */
548 verify(rsp
->plain
.message
.header
.response
.cas
== 0);
551 case PROTOCOL_BINARY_CMD_VERSION
:
552 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
553 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
554 verify(rsp
->plain
.message
.header
.response
.bodylen
!= 0);
555 verify(rsp
->plain
.message
.header
.response
.cas
== 0);
558 case PROTOCOL_BINARY_CMD_GET
:
559 case PROTOCOL_BINARY_CMD_GETQ
:
560 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
561 verify(rsp
->plain
.message
.header
.response
.extlen
== 4);
562 verify(rsp
->plain
.message
.header
.response
.cas
!= 0);
565 case PROTOCOL_BINARY_CMD_GETK
:
566 case PROTOCOL_BINARY_CMD_GETKQ
:
567 verify(rsp
->plain
.message
.header
.response
.keylen
!= 0);
568 verify(rsp
->plain
.message
.header
.response
.extlen
== 4);
569 verify(rsp
->plain
.message
.header
.response
.cas
!= 0);
573 /* Undefined command code */
579 verify(rsp
->plain
.message
.header
.response
.cas
== 0);
580 verify(rsp
->plain
.message
.header
.response
.extlen
== 0);
581 if (cc
!= PROTOCOL_BINARY_CMD_GETK
)
583 verify(rsp
->plain
.message
.header
.response
.keylen
== 0);
590 /* We call verify(validate_response_header), but that macro
591 * expects a boolean expression, and the function returns
592 * an enum.... Let's just create a macro to avoid cluttering
593 * the code with all of the == TEST_PASS ;-)
595 #define validate_response_header(a,b,c) \
596 do_validate_response_header(a,b,c) == TEST_PASS
599 static enum test_return
send_binary_noop(void)
602 raw_command(&cmd
, PROTOCOL_BINARY_CMD_NOOP
, NULL
, 0, NULL
, 0);
603 execute(send_packet(&cmd
));
607 static enum test_return
receive_binary_noop(void)
610 execute(recv_packet(&rsp
));
611 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_NOOP
,
612 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
616 static enum test_return
test_binary_noop(void)
618 execute(send_binary_noop());
619 execute(receive_binary_noop());
623 static enum test_return
test_binary_quit_impl(uint8_t cc
)
627 raw_command(&cmd
, cc
, NULL
, 0, NULL
, 0);
629 execute(send_packet(&cmd
));
630 if (cc
== PROTOCOL_BINARY_CMD_QUIT
)
632 execute(recv_packet(&rsp
));
633 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_QUIT
,
634 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
637 /* Socket should be closed now, read should return EXIT_SUCCESS */
638 verify(timeout_io_op(sock
, POLLIN
, rsp
.bytes
, sizeof(rsp
.bytes
)) == 0);
640 return TEST_PASS_RECONNECT
;
643 static enum test_return
test_binary_quit(void)
645 return test_binary_quit_impl(PROTOCOL_BINARY_CMD_QUIT
);
648 static enum test_return
test_binary_quitq(void)
650 return test_binary_quit_impl(PROTOCOL_BINARY_CMD_QUITQ
);
653 static enum test_return
test_binary_set_impl(const char* key
, uint8_t cc
)
658 uint64_t value
= 0xdeadbeefdeadcafe;
659 storage_command(&cmd
, cc
, key
, strlen(key
), &value
, sizeof (value
), 0, 0);
661 /* set should always work */
662 for (int ii
= 0; ii
< 10; ii
++)
665 execute(send_packet(&cmd
));
667 execute(resend_packet(&cmd
));
669 if (cc
== PROTOCOL_BINARY_CMD_SET
)
671 execute(recv_packet(&rsp
));
672 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
675 execute(test_binary_noop());
679 * We need to get the current CAS id, and at this time we haven't
680 * verified that we have a working get
682 if (cc
== PROTOCOL_BINARY_CMD_SETQ
)
684 cmd
.set
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_SET
;
685 execute(resend_packet(&cmd
));
686 execute(recv_packet(&rsp
));
687 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_SET
,
688 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
689 cmd
.set
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_SETQ
;
692 /* try to set with the correct CAS value */
693 cmd
.plain
.message
.header
.request
.cas
=
694 htonll(rsp
.plain
.message
.header
.response
.cas
);
695 execute(resend_packet(&cmd
));
696 if (cc
== PROTOCOL_BINARY_CMD_SET
)
698 execute(recv_packet(&rsp
));
699 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
702 execute(test_binary_noop());
704 /* try to set with an incorrect CAS value */
705 cmd
.plain
.message
.header
.request
.cas
=
706 htonll(rsp
.plain
.message
.header
.response
.cas
- 1);
707 execute(resend_packet(&cmd
));
708 execute(send_binary_noop());
709 execute(recv_packet(&rsp
));
710 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS
));
711 execute(receive_binary_noop());
716 static enum test_return
test_binary_set(void)
718 return test_binary_set_impl("test_binary_set", PROTOCOL_BINARY_CMD_SET
);
721 static enum test_return
test_binary_setq(void)
723 return test_binary_set_impl("test_binary_setq", PROTOCOL_BINARY_CMD_SETQ
);
726 static enum test_return
test_binary_add_impl(const char* key
, uint8_t cc
)
730 uint64_t value
= 0xdeadbeefdeadcafe;
731 storage_command(&cmd
, cc
, key
, strlen(key
), &value
, sizeof (value
), 0, 0);
733 /* first add should work, rest of them should fail (even with cas
735 for (int ii
=0; ii
< 10; ii
++)
738 execute(send_packet(&cmd
));
740 execute(resend_packet(&cmd
));
742 if (cc
== PROTOCOL_BINARY_CMD_ADD
|| ii
> 0)
744 uint16_t expected_result
;
746 expected_result
= PROTOCOL_BINARY_RESPONSE_SUCCESS
;
748 expected_result
= PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS
;
750 execute(send_binary_noop());
751 execute(recv_packet(&rsp
));
752 execute(receive_binary_noop());
753 verify(validate_response_header(&rsp
, cc
, expected_result
));
756 execute(test_binary_noop());
762 static enum test_return
test_binary_add(void)
764 return test_binary_add_impl("test_binary_add", PROTOCOL_BINARY_CMD_ADD
);
767 static enum test_return
test_binary_addq(void)
769 return test_binary_add_impl("test_binary_addq", PROTOCOL_BINARY_CMD_ADDQ
);
772 static enum test_return
binary_set_item(const char *key
, const char *value
)
776 storage_command(&cmd
, PROTOCOL_BINARY_CMD_SET
, key
, strlen(key
),
777 value
, strlen(value
), 0, 0);
778 execute(send_packet(&cmd
));
779 execute(recv_packet(&rsp
));
780 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_SET
,
781 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
785 static enum test_return
test_binary_replace_impl(const char* key
, uint8_t cc
)
789 uint64_t value
= 0xdeadbeefdeadcafe;
790 storage_command(&cmd
, cc
, key
, strlen(key
), &value
, sizeof (value
), 0, 0);
792 /* first replace should fail, successive should succeed (when the
794 for (int ii
= 0; ii
< 10; ii
++)
797 execute(send_packet(&cmd
));
799 execute(resend_packet(&cmd
));
801 if (cc
== PROTOCOL_BINARY_CMD_REPLACE
|| ii
== 0)
803 uint16_t expected_result
;
805 expected_result
=PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
;
807 expected_result
=PROTOCOL_BINARY_RESPONSE_SUCCESS
;
809 execute(send_binary_noop());
810 execute(recv_packet(&rsp
));
811 execute(receive_binary_noop());
812 verify(validate_response_header(&rsp
, cc
, expected_result
));
815 execute(binary_set_item(key
, key
));
818 execute(test_binary_noop());
821 /* verify that replace with CAS value works! */
822 cmd
.plain
.message
.header
.request
.cas
=
823 htonll(rsp
.plain
.message
.header
.response
.cas
);
824 execute(resend_packet(&cmd
));
826 if (cc
== PROTOCOL_BINARY_CMD_REPLACE
)
828 execute(recv_packet(&rsp
));
829 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
832 execute(test_binary_noop());
834 /* try to set with an incorrect CAS value */
835 cmd
.plain
.message
.header
.request
.cas
=
836 htonll(rsp
.plain
.message
.header
.response
.cas
- 1);
837 execute(resend_packet(&cmd
));
838 execute(send_binary_noop());
839 execute(recv_packet(&rsp
));
840 execute(receive_binary_noop());
841 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS
));
846 static enum test_return
test_binary_replace(void)
848 return test_binary_replace_impl("test_binary_replace", PROTOCOL_BINARY_CMD_REPLACE
);
851 static enum test_return
test_binary_replaceq(void)
853 return test_binary_replace_impl("test_binary_replaceq", PROTOCOL_BINARY_CMD_REPLACEQ
);
856 static enum test_return
test_binary_delete_impl(const char *key
, uint8_t cc
)
860 raw_command(&cmd
, cc
, key
, strlen(key
), NULL
, 0);
862 /* The delete shouldn't work the first time, because the item isn't there */
863 execute(send_packet(&cmd
));
864 execute(send_binary_noop());
865 execute(recv_packet(&rsp
));
866 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
));
867 execute(receive_binary_noop());
868 execute(binary_set_item(key
, key
));
870 /* The item should be present now, resend*/
871 execute(resend_packet(&cmd
));
872 if (cc
== PROTOCOL_BINARY_CMD_DELETE
)
874 execute(recv_packet(&rsp
));
875 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
878 execute(test_binary_noop());
883 static enum test_return
test_binary_delete(void)
885 return test_binary_delete_impl("test_binary_delete", PROTOCOL_BINARY_CMD_DELETE
);
888 static enum test_return
test_binary_deleteq(void)
890 return test_binary_delete_impl("test_binary_deleteq", PROTOCOL_BINARY_CMD_DELETEQ
);
893 static enum test_return
test_binary_get_impl(const char *key
, uint8_t cc
)
898 raw_command(&cmd
, cc
, key
, strlen(key
), NULL
, 0);
899 execute(send_packet(&cmd
));
900 execute(send_binary_noop());
902 if (cc
== PROTOCOL_BINARY_CMD_GET
|| cc
== PROTOCOL_BINARY_CMD_GETK
)
904 execute(recv_packet(&rsp
));
905 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
));
908 execute(receive_binary_noop());
910 execute(binary_set_item(key
, key
));
911 execute(resend_packet(&cmd
));
912 execute(send_binary_noop());
914 execute(recv_packet(&rsp
));
915 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
916 execute(receive_binary_noop());
921 static enum test_return
test_binary_get(void)
923 return test_binary_get_impl("test_binary_get", PROTOCOL_BINARY_CMD_GET
);
926 static enum test_return
test_binary_getk(void)
928 return test_binary_get_impl("test_binary_getk", PROTOCOL_BINARY_CMD_GETK
);
931 static enum test_return
test_binary_getq(void)
933 return test_binary_get_impl("test_binary_getq", PROTOCOL_BINARY_CMD_GETQ
);
936 static enum test_return
test_binary_getkq(void)
938 return test_binary_get_impl("test_binary_getkq", PROTOCOL_BINARY_CMD_GETKQ
);
941 static enum test_return
test_binary_incr_impl(const char* key
, uint8_t cc
)
945 arithmetic_command(&cmd
, cc
, key
, strlen(key
), 1, 0, 0);
948 for (ii
= 0; ii
< 10; ++ii
)
951 execute(send_packet(&cmd
));
953 execute(resend_packet(&cmd
));
955 if (cc
== PROTOCOL_BINARY_CMD_INCREMENT
)
957 execute(recv_packet(&rsp
));
958 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
959 verify(ntohll(rsp
.incr
.message
.body
.value
) == ii
);
962 execute(test_binary_noop());
965 /* @todo add incorrect CAS */
969 static enum test_return
test_binary_incr(void)
971 return test_binary_incr_impl("test_binary_incr", PROTOCOL_BINARY_CMD_INCREMENT
);
974 static enum test_return
test_binary_incrq(void)
976 return test_binary_incr_impl("test_binary_incrq", PROTOCOL_BINARY_CMD_INCREMENTQ
);
979 static enum test_return
test_binary_decr_impl(const char* key
, uint8_t cc
)
983 arithmetic_command(&cmd
, cc
, key
, strlen(key
), 1, 9, 0);
986 for (ii
= 9; ii
> -1; --ii
)
989 execute(send_packet(&cmd
));
991 execute(resend_packet(&cmd
));
993 if (cc
== PROTOCOL_BINARY_CMD_DECREMENT
)
995 execute(recv_packet(&rsp
));
996 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
997 verify(ntohll(rsp
.decr
.message
.body
.value
) == (uint64_t)ii
);
1000 execute(test_binary_noop());
1003 /* decr 0 should not wrap */
1004 execute(resend_packet(&cmd
));
1005 if (cc
== PROTOCOL_BINARY_CMD_DECREMENT
)
1007 execute(recv_packet(&rsp
));
1008 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1009 verify(ntohll(rsp
.decr
.message
.body
.value
) == 0);
1013 /* @todo get the value and verify! */
1017 /* @todo add incorrect cas */
1018 execute(test_binary_noop());
1022 static enum test_return
test_binary_decr(void)
1024 return test_binary_decr_impl("test_binary_decr",
1025 PROTOCOL_BINARY_CMD_DECREMENT
);
1028 static enum test_return
test_binary_decrq(void)
1030 return test_binary_decr_impl("test_binary_decrq",
1031 PROTOCOL_BINARY_CMD_DECREMENTQ
);
1034 static enum test_return
test_binary_version(void)
1038 raw_command(&cmd
, PROTOCOL_BINARY_CMD_VERSION
, NULL
, 0, NULL
, 0);
1040 execute(send_packet(&cmd
));
1041 execute(recv_packet(&rsp
));
1042 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_VERSION
,
1043 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1048 static enum test_return
test_binary_flush_impl(const char *key
, uint8_t cc
)
1053 for (int ii
= 0; ii
< 2; ++ii
)
1055 execute(binary_set_item(key
, key
));
1056 flush_command(&cmd
, cc
, 0, ii
== 0);
1057 execute(send_packet(&cmd
));
1059 if (cc
== PROTOCOL_BINARY_CMD_FLUSH
)
1061 execute(recv_packet(&rsp
));
1062 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1065 execute(test_binary_noop());
1067 raw_command(&cmd
, PROTOCOL_BINARY_CMD_GET
, key
, strlen(key
), NULL
, 0);
1068 execute(send_packet(&cmd
));
1069 execute(recv_packet(&rsp
));
1070 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_GET
,
1071 PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
));
1077 static enum test_return
test_binary_flush(void)
1079 return test_binary_flush_impl("test_binary_flush", PROTOCOL_BINARY_CMD_FLUSH
);
1082 static enum test_return
test_binary_flushq(void)
1084 return test_binary_flush_impl("test_binary_flushq", PROTOCOL_BINARY_CMD_FLUSHQ
);
1087 static enum test_return
test_binary_concat_impl(const char *key
, uint8_t cc
)
1093 if (cc
== PROTOCOL_BINARY_CMD_APPEND
|| cc
== PROTOCOL_BINARY_CMD_APPENDQ
)
1098 execute(binary_set_item(key
, value
));
1100 if (cc
== PROTOCOL_BINARY_CMD_APPEND
|| cc
== PROTOCOL_BINARY_CMD_APPENDQ
)
1105 raw_command(&cmd
, cc
, key
, strlen(key
), value
, strlen(value
));
1106 execute(send_packet(&cmd
));
1107 if (cc
== PROTOCOL_BINARY_CMD_APPEND
|| cc
== PROTOCOL_BINARY_CMD_PREPEND
)
1109 execute(recv_packet(&rsp
));
1110 verify(validate_response_header(&rsp
, cc
, PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1113 execute(test_binary_noop());
1115 raw_command(&cmd
, PROTOCOL_BINARY_CMD_GET
, key
, strlen(key
), NULL
, 0);
1116 execute(send_packet(&cmd
));
1117 execute(recv_packet(&rsp
));
1118 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_GET
,
1119 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1120 verify(rsp
.plain
.message
.header
.response
.bodylen
- 4 == 11);
1121 verify(memcmp(rsp
.bytes
+ 28, "hello world", 11) == 0);
1126 static enum test_return
test_binary_append(void)
1128 return test_binary_concat_impl("test_binary_append", PROTOCOL_BINARY_CMD_APPEND
);
1131 static enum test_return
test_binary_prepend(void)
1133 return test_binary_concat_impl("test_binary_prepend", PROTOCOL_BINARY_CMD_PREPEND
);
1136 static enum test_return
test_binary_appendq(void)
1138 return test_binary_concat_impl("test_binary_appendq", PROTOCOL_BINARY_CMD_APPENDQ
);
1141 static enum test_return
test_binary_prependq(void)
1143 return test_binary_concat_impl("test_binary_prependq", PROTOCOL_BINARY_CMD_PREPENDQ
);
1146 static enum test_return
test_binary_stat(void)
1151 raw_command(&cmd
, PROTOCOL_BINARY_CMD_STAT
, NULL
, 0, NULL
, 0);
1152 execute(send_packet(&cmd
));
1156 execute(recv_packet(&rsp
));
1157 verify(validate_response_header(&rsp
, PROTOCOL_BINARY_CMD_STAT
,
1158 PROTOCOL_BINARY_RESPONSE_SUCCESS
));
1159 } while (rsp
.plain
.message
.header
.response
.keylen
!= 0);
1164 static enum test_return
send_string(const char *cmd
)
1166 execute(retry_write(cmd
, strlen(cmd
)));
1170 static enum test_return
receive_line(char *buffer
, size_t size
)
1173 while (offset
< size
)
1175 execute(retry_read(buffer
+ offset
, 1));
1176 if (buffer
[offset
] == '\n')
1178 if (offset
+ 1 < size
)
1180 buffer
[offset
+ 1]= '\0';
1192 static enum test_return
receive_response(const char *msg
) {
1194 execute(receive_line(buffer
, sizeof(buffer
)));
1195 if (strcmp(msg
, buffer
) != 0) {
1196 fprintf(stderr
, "[%s]\n", buffer
);
1198 verify(strcmp(msg
, buffer
) == 0);
1202 static enum test_return
receive_error_response(void)
1205 execute(receive_line(buffer
, sizeof(buffer
)));
1206 verify(strncmp(buffer
, "ERROR", 5) == 0 ||
1207 strncmp(buffer
, "CLIENT_ERROR", 12) == 0 ||
1208 strncmp(buffer
, "SERVER_ERROR", 12) == 0);
1212 static enum test_return
test_ascii_quit(void)
1214 /* Verify that quit handles unknown options */
1215 execute(send_string("quit foo bar\r\n"));
1216 execute(receive_error_response());
1218 /* quit doesn't support noreply */
1219 execute(send_string("quit noreply\r\n"));
1220 execute(receive_error_response());
1222 /* Verify that quit works */
1223 execute(send_string("quit\r\n"));
1225 /* Socket should be closed now, read should return EXIT_SUCCESS */
1227 verify(timeout_io_op(sock
, POLLIN
, buffer
, sizeof(buffer
)) == 0);
1228 return TEST_PASS_RECONNECT
;
1232 static enum test_return
test_ascii_version(void)
1234 /* Verify that version command handles unknown options */
1235 execute(send_string("version foo bar\r\n"));
1236 execute(receive_error_response());
1238 /* version doesn't support noreply */
1239 execute(send_string("version noreply\r\n"));
1240 execute(receive_error_response());
1242 /* Verify that verify works */
1243 execute(send_string("version\r\n"));
1245 execute(receive_line(buffer
, sizeof(buffer
)));
1246 verify(strncmp(buffer
, "VERSION ", 8) == 0);
1251 static enum test_return
test_ascii_verbosity(void)
1253 /* This command does not adhere to the spec! */
1254 execute(send_string("verbosity foo bar my\r\n"));
1255 execute(receive_error_response());
1257 execute(send_string("verbosity noreply\r\n"));
1258 execute(receive_error_response());
1260 execute(send_string("verbosity 0 noreply\r\n"));
1261 execute(test_ascii_version());
1263 execute(send_string("verbosity\r\n"));
1264 execute(receive_error_response());
1266 execute(send_string("verbosity 1\r\n"));
1267 execute(receive_response("OK\r\n"));
1269 execute(send_string("verbosity 0\r\n"));
1270 execute(receive_response("OK\r\n"));
1277 static enum test_return
test_ascii_set_impl(const char* key
, bool noreply
)
1279 /* @todo add tests for bogus format! */
1281 snprintf(buffer
, sizeof(buffer
), "set %s 0 0 5%s\r\nvalue\r\n", key
, noreply
? " noreply" : "");
1282 execute(send_string(buffer
));
1285 execute(receive_response("STORED\r\n"));
1287 return test_ascii_version();
1290 static enum test_return
test_ascii_set(void)
1292 return test_ascii_set_impl("test_ascii_set", false);
1295 static enum test_return
test_ascii_set_noreply(void)
1297 return test_ascii_set_impl("test_ascii_set_noreply", true);
1300 static enum test_return
test_ascii_add_impl(const char* key
, bool noreply
)
1302 /* @todo add tests for bogus format! */
1304 snprintf(buffer
, sizeof(buffer
), "add %s 0 0 5%s\r\nvalue\r\n", key
, noreply
? " noreply" : "");
1305 execute(send_string(buffer
));
1308 execute(receive_response("STORED\r\n"));
1310 execute(send_string(buffer
));
1313 execute(receive_response("NOT_STORED\r\n"));
1315 return test_ascii_version();
1318 static enum test_return
test_ascii_add(void)
1320 return test_ascii_add_impl("test_ascii_add", false);
1323 static enum test_return
test_ascii_add_noreply(void)
1325 return test_ascii_add_impl("test_ascii_add_noreply", true);
1328 static enum test_return
ascii_get_unknown_value(char **key
, char **value
, ssize_t
*ndata
)
1332 execute(receive_line(buffer
, sizeof(buffer
)));
1333 verify(strncmp(buffer
, "VALUE ", 6) == 0);
1334 char *end
= strchr(buffer
+ 6, ' ');
1335 verify(end
!= NULL
);
1337 *key
= strdup(buffer
+ 6);
1338 verify(*key
!= NULL
);
1341 unsigned long val
= strtoul(ptr
, &end
, 10); /* flags */
1344 verify(end
!= NULL
);
1345 *ndata
= (ssize_t
)strtoul(end
, &end
, 10); /* size */
1347 verify(end
!= NULL
);
1348 while (*end
!= '\n' && isspace(*end
))
1350 verify(*end
== '\n');
1352 *value
= static_cast<char*>(malloc((size_t)*ndata
));
1353 verify(*value
!= NULL
);
1355 execute(retry_read(*value
, (size_t)*ndata
));
1357 execute(retry_read(buffer
, 2));
1358 verify(memcmp(buffer
, "\r\n", 2) == 0);
1363 static enum test_return
ascii_get_value(const char *key
, const char *value
)
1367 size_t datasize
= strlen(value
);
1369 verify(datasize
< sizeof(buffer
));
1370 execute(receive_line(buffer
, sizeof(buffer
)));
1371 verify(strncmp(buffer
, "VALUE ", 6) == 0);
1372 verify(strncmp(buffer
+ 6, key
, strlen(key
)) == 0);
1373 char *ptr
= buffer
+ 6 + strlen(key
) + 1;
1376 unsigned long val
= strtoul(ptr
, &end
, 10); /* flags */
1379 verify(end
!= NULL
);
1380 val
= strtoul(end
, &end
, 10); /* size */
1382 verify(val
== datasize
);
1383 verify(end
!= NULL
);
1384 while (*end
!= '\n' && isspace(*end
))
1386 verify(*end
== '\n');
1388 execute(retry_read(buffer
, datasize
));
1389 verify(memcmp(buffer
, value
, datasize
) == 0);
1391 execute(retry_read(buffer
, 2));
1392 verify(memcmp(buffer
, "\r\n", 2) == 0);
1397 static enum test_return
ascii_get_item(const char *key
, const char *value
,
1403 datasize
= strlen(value
);
1405 verify(datasize
< sizeof(buffer
));
1406 snprintf(buffer
, sizeof(buffer
), "get %s\r\n", key
);
1407 execute(send_string(buffer
));
1410 execute(ascii_get_value(key
, value
));
1412 execute(retry_read(buffer
, 5));
1413 verify(memcmp(buffer
, "END\r\n", 5) == 0);
1418 static enum test_return
ascii_gets_value(const char *key
, const char *value
,
1423 size_t datasize
= strlen(value
);
1425 verify(datasize
< sizeof(buffer
));
1426 execute(receive_line(buffer
, sizeof(buffer
)));
1427 verify(strncmp(buffer
, "VALUE ", 6) == 0);
1428 verify(strncmp(buffer
+ 6, key
, strlen(key
)) == 0);
1429 char *ptr
= buffer
+ 6 + strlen(key
) + 1;
1432 unsigned long val
= strtoul(ptr
, &end
, 10); /* flags */
1435 verify(end
!= NULL
);
1436 val
= strtoul(end
, &end
, 10); /* size */
1438 verify(val
== datasize
);
1439 verify(end
!= NULL
);
1440 *cas
= strtoul(end
, &end
, 10); /* cas */
1442 verify(val
== datasize
);
1443 verify(end
!= NULL
);
1445 while (*end
!= '\n' && isspace(*end
))
1447 verify(*end
== '\n');
1449 execute(retry_read(buffer
, datasize
));
1450 verify(memcmp(buffer
, value
, datasize
) == 0);
1452 execute(retry_read(buffer
, 2));
1453 verify(memcmp(buffer
, "\r\n", 2) == 0);
1458 static enum test_return
ascii_gets_item(const char *key
, const char *value
,
1459 bool exist
, unsigned long *cas
)
1464 datasize
= strlen(value
);
1466 verify(datasize
< sizeof(buffer
));
1467 snprintf(buffer
, sizeof(buffer
), "gets %s\r\n", key
);
1468 execute(send_string(buffer
));
1471 execute(ascii_gets_value(key
, value
, cas
));
1473 execute(retry_read(buffer
, 5));
1474 verify(memcmp(buffer
, "END\r\n", 5) == 0);
1479 static enum test_return
ascii_set_item(const char *key
, const char *value
)
1482 size_t len
= strlen(value
);
1483 snprintf(buffer
, sizeof(buffer
), "set %s 0 0 %u\r\n", key
, (unsigned int)len
);
1484 execute(send_string(buffer
));
1485 execute(retry_write(value
, len
));
1486 execute(send_string("\r\n"));
1487 execute(receive_response("STORED\r\n"));
1491 static enum test_return
test_ascii_replace_impl(const char* key
, bool noreply
)
1494 snprintf(buffer
, sizeof(buffer
), "replace %s 0 0 5%s\r\nvalue\r\n", key
, noreply
? " noreply" : "");
1495 execute(send_string(buffer
));
1498 execute(test_ascii_version());
1500 execute(receive_response("NOT_STORED\r\n"));
1502 execute(ascii_set_item(key
, "value"));
1503 execute(ascii_get_item(key
, "value", true));
1506 execute(send_string(buffer
));
1509 execute(test_ascii_version());
1511 execute(receive_response("STORED\r\n"));
1513 return test_ascii_version();
1516 static enum test_return
test_ascii_replace(void)
1518 return test_ascii_replace_impl("test_ascii_replace", false);
1521 static enum test_return
test_ascii_replace_noreply(void)
1523 return test_ascii_replace_impl("test_ascii_replace_noreply", true);
1526 static enum test_return
test_ascii_cas_impl(const char* key
, bool noreply
)
1531 execute(ascii_set_item(key
, "value"));
1532 execute(ascii_gets_item(key
, "value", true, &cas
));
1534 snprintf(buffer
, sizeof(buffer
), "cas %s 0 0 6 %lu%s\r\nvalue2\r\n", key
, cas
, noreply
? " noreply" : "");
1535 execute(send_string(buffer
));
1538 execute(test_ascii_version());
1540 execute(receive_response("STORED\r\n"));
1542 /* reexecute the same command should fail due to illegal cas */
1543 execute(send_string(buffer
));
1546 execute(test_ascii_version());
1548 execute(receive_response("EXISTS\r\n"));
1550 return test_ascii_version();
1553 static enum test_return
test_ascii_cas(void)
1555 return test_ascii_cas_impl("test_ascii_cas", false);
1558 static enum test_return
test_ascii_cas_noreply(void)
1560 return test_ascii_cas_impl("test_ascii_cas_noreply", true);
1563 static enum test_return
test_ascii_delete_impl(const char *key
, bool noreply
)
1565 execute(ascii_set_item(key
, "value"));
1567 execute(send_string("delete\r\n"));
1568 execute(receive_error_response());
1569 /* BUG: the server accepts delete a b */
1570 execute(send_string("delete a b c d e\r\n"));
1571 execute(receive_error_response());
1574 snprintf(buffer
, sizeof(buffer
), "delete %s%s\r\n", key
, noreply
? " noreply" : "");
1575 execute(send_string(buffer
));
1578 execute(test_ascii_version());
1580 execute(receive_response("DELETED\r\n"));
1582 execute(ascii_get_item(key
, "value", false));
1583 execute(send_string(buffer
));
1585 execute(test_ascii_version());
1587 execute(receive_response("NOT_FOUND\r\n"));
1592 static enum test_return
test_ascii_delete(void)
1594 return test_ascii_delete_impl("test_ascii_delete", false);
1597 static enum test_return
test_ascii_delete_noreply(void)
1599 return test_ascii_delete_impl("test_ascii_delete_noreply", true);
1602 static enum test_return
test_ascii_get(void)
1604 execute(ascii_set_item("test_ascii_get", "value"));
1606 execute(send_string("get\r\n"));
1607 execute(receive_error_response());
1608 execute(ascii_get_item("test_ascii_get", "value", true));
1609 execute(ascii_get_item("test_ascii_get_notfound", "value", false));
1614 static enum test_return
test_ascii_gets(void)
1616 execute(ascii_set_item("test_ascii_gets", "value"));
1618 execute(send_string("gets\r\n"));
1619 execute(receive_error_response());
1621 execute(ascii_gets_item("test_ascii_gets", "value", true, &cas
));
1622 execute(ascii_gets_item("test_ascii_gets_notfound", "value", false, &cas
));
1627 static enum test_return
test_ascii_mget(void)
1629 const uint32_t nkeys
= 5;
1630 const char * const keys
[]= {
1633 /* test_ascii_mget_3 does not exist :) */
1639 for (uint32_t x
= 0; x
< nkeys
; ++x
)
1640 execute(ascii_set_item(keys
[x
], "value"));
1642 /* Ask for a key that doesn't exist as well */
1643 execute(send_string("get test_ascii_mget1 test_ascii_mget2 test_ascii_mget3 "
1644 "test_ascii_mget4 test_ascii_mget5 "
1645 "test_ascii_mget6\r\n"));
1647 char *returned
[nkeys
];
1649 for (uint32_t x
= 0; x
< nkeys
; ++x
)
1653 execute(ascii_get_unknown_value(&returned
[x
], &v
, &nbytes
));
1654 verify(nbytes
== 5);
1655 verify(memcmp(v
, "value", 5) == 0);
1660 execute(retry_read(buffer
, 5));
1661 verify(memcmp(buffer
, "END\r\n", 5) == 0);
1663 /* verify that we got all the keys we expected */
1664 for (uint32_t x
= 0; x
< nkeys
; ++x
)
1667 for (uint32_t y
= 0; y
< nkeys
; ++y
)
1669 if (strcmp(keys
[x
], returned
[y
]) == 0)
1678 for (uint32_t x
= 0; x
< nkeys
; ++x
)
1684 static enum test_return
test_ascii_incr_impl(const char* key
, bool noreply
)
1687 snprintf(cmd
, sizeof(cmd
), "incr %s 1%s\r\n", key
, noreply
? " noreply" : "");
1689 execute(ascii_set_item(key
, "0"));
1690 for (int x
= 1; x
< 11; ++x
)
1692 execute(send_string(cmd
));
1695 execute(test_ascii_version());
1699 execute(receive_line(buffer
, sizeof(buffer
)));
1700 int val
= atoi(buffer
);
1705 execute(ascii_get_item(key
, "10", true));
1710 static enum test_return
test_ascii_incr(void)
1712 return test_ascii_incr_impl("test_ascii_incr", false);
1715 static enum test_return
test_ascii_incr_noreply(void)
1717 return test_ascii_incr_impl("test_ascii_incr_noreply", true);
1720 static enum test_return
test_ascii_decr_impl(const char* key
, bool noreply
)
1723 snprintf(cmd
, sizeof(cmd
), "decr %s 1%s\r\n", key
, noreply
? " noreply" : "");
1725 execute(ascii_set_item(key
, "9"));
1726 for (int x
= 8; x
> -1; --x
)
1728 execute(send_string(cmd
));
1731 execute(test_ascii_version());
1735 execute(receive_line(buffer
, sizeof(buffer
)));
1736 int val
= atoi(buffer
);
1741 execute(ascii_get_item(key
, "0", true));
1743 /* verify that it doesn't wrap */
1744 execute(send_string(cmd
));
1746 execute(test_ascii_version());
1750 execute(receive_line(buffer
, sizeof(buffer
)));
1752 execute(ascii_get_item(key
, "0", true));
1757 static enum test_return
test_ascii_decr(void)
1759 return test_ascii_decr_impl("test_ascii_decr", false);
1762 static enum test_return
test_ascii_decr_noreply(void)
1764 return test_ascii_decr_impl("test_ascii_decr_noreply", true);
1768 static enum test_return
test_ascii_flush_impl(const char *key
, bool noreply
)
1771 /* Verify that the flush_all command handles unknown options */
1772 /* Bug in the current memcached server! */
1773 execute(send_string("flush_all foo bar\r\n"));
1774 execute(receive_error_response());
1777 execute(ascii_set_item(key
, key
));
1778 execute(ascii_get_item(key
, key
, true));
1782 execute(send_string("flush_all noreply\r\n"));
1783 execute(test_ascii_version());
1787 execute(send_string("flush_all\r\n"));
1788 execute(receive_response("OK\r\n"));
1791 execute(ascii_get_item(key
, key
, false));
1796 static enum test_return
test_ascii_flush(void)
1798 return test_ascii_flush_impl("test_ascii_flush", false);
1801 static enum test_return
test_ascii_flush_noreply(void)
1803 return test_ascii_flush_impl("test_ascii_flush_noreply", true);
1806 static enum test_return
test_ascii_concat_impl(const char *key
,
1817 execute(ascii_set_item(key
, value
));
1825 snprintf(cmd
, sizeof(cmd
), "%s %s 0 0 %u%s\r\n%s\r\n",
1826 append
? "append" : "prepend",
1827 key
, (unsigned int)strlen(value
), noreply
? " noreply" : "",
1829 execute(send_string(cmd
));
1832 execute(test_ascii_version());
1834 execute(receive_response("STORED\r\n"));
1836 execute(ascii_get_item(key
, "hello world", true));
1838 snprintf(cmd
, sizeof(cmd
), "%s %s_notfound 0 0 %u%s\r\n%s\r\n",
1839 append
? "append" : "prepend",
1840 key
, (unsigned int)strlen(value
), noreply
? " noreply" : "",
1842 execute(send_string(cmd
));
1845 execute(test_ascii_version());
1847 execute(receive_response("NOT_STORED\r\n"));
1852 static enum test_return
test_ascii_append(void)
1854 return test_ascii_concat_impl("test_ascii_append", true, false);
1857 static enum test_return
test_ascii_prepend(void)
1859 return test_ascii_concat_impl("test_ascii_prepend", false, false);
1862 static enum test_return
test_ascii_append_noreply(void)
1864 return test_ascii_concat_impl("test_ascii_append_noreply", true, true);
1867 static enum test_return
test_ascii_prepend_noreply(void)
1869 return test_ascii_concat_impl("test_ascii_prepend_noreply", false, true);
1872 static enum test_return
test_ascii_stat(void)
1874 execute(send_string("stats noreply\r\n"));
1875 execute(receive_error_response());
1876 execute(send_string("stats\r\n"));
1879 execute(receive_line(buffer
, sizeof(buffer
)));
1880 } while (strcmp(buffer
, "END\r\n") != 0);
1882 return TEST_PASS_RECONNECT
;
1885 typedef enum test_return(*TEST_FUNC
)(void);
1889 const char *description
;
1893 struct testcase testcases
[]= {
1894 { "ascii quit", test_ascii_quit
},
1895 { "ascii version", test_ascii_version
},
1896 { "ascii verbosity", test_ascii_verbosity
},
1897 { "ascii set", test_ascii_set
},
1898 { "ascii set noreply", test_ascii_set_noreply
},
1899 { "ascii get", test_ascii_get
},
1900 { "ascii gets", test_ascii_gets
},
1901 { "ascii mget", test_ascii_mget
},
1902 { "ascii flush", test_ascii_flush
},
1903 { "ascii flush noreply", test_ascii_flush_noreply
},
1904 { "ascii add", test_ascii_add
},
1905 { "ascii add noreply", test_ascii_add_noreply
},
1906 { "ascii replace", test_ascii_replace
},
1907 { "ascii replace noreply", test_ascii_replace_noreply
},
1908 { "ascii cas", test_ascii_cas
},
1909 { "ascii cas noreply", test_ascii_cas_noreply
},
1910 { "ascii delete", test_ascii_delete
},
1911 { "ascii delete noreply", test_ascii_delete_noreply
},
1912 { "ascii incr", test_ascii_incr
},
1913 { "ascii incr noreply", test_ascii_incr_noreply
},
1914 { "ascii decr", test_ascii_decr
},
1915 { "ascii decr noreply", test_ascii_decr_noreply
},
1916 { "ascii append", test_ascii_append
},
1917 { "ascii append noreply", test_ascii_append_noreply
},
1918 { "ascii prepend", test_ascii_prepend
},
1919 { "ascii prepend noreply", test_ascii_prepend_noreply
},
1920 { "ascii stat", test_ascii_stat
},
1921 { "binary noop", test_binary_noop
},
1922 { "binary quit", test_binary_quit
},
1923 { "binary quitq", test_binary_quitq
},
1924 { "binary set", test_binary_set
},
1925 { "binary setq", test_binary_setq
},
1926 { "binary flush", test_binary_flush
},
1927 { "binary flushq", test_binary_flushq
},
1928 { "binary add", test_binary_add
},
1929 { "binary addq", test_binary_addq
},
1930 { "binary replace", test_binary_replace
},
1931 { "binary replaceq", test_binary_replaceq
},
1932 { "binary delete", test_binary_delete
},
1933 { "binary deleteq", test_binary_deleteq
},
1934 { "binary get", test_binary_get
},
1935 { "binary getq", test_binary_getq
},
1936 { "binary getk", test_binary_getk
},
1937 { "binary getkq", test_binary_getkq
},
1938 { "binary incr", test_binary_incr
},
1939 { "binary incrq", test_binary_incrq
},
1940 { "binary decr", test_binary_decr
},
1941 { "binary decrq", test_binary_decrq
},
1942 { "binary version", test_binary_version
},
1943 { "binary append", test_binary_append
},
1944 { "binary appendq", test_binary_appendq
},
1945 { "binary prepend", test_binary_prepend
},
1946 { "binary prependq", test_binary_prependq
},
1947 { "binary stat", test_binary_stat
},
1951 const int ascii_tests
= 1;
1952 const int binary_tests
= 2;
1960 int main(int argc
, char **argv
)
1962 static const char * const status_msg
[]= {"[skip]", "[pass]", "[pass]", "[FAIL]"};
1963 struct test_type_st tests
= { true, true };
1966 const char *hostname
= "localhost";
1967 const char *port
= "11211";
1970 const char *testname
= NULL
;
1974 while ((cmd
= getopt(argc
, argv
, "t:vch:p:PT:?ab")) != EOF
)
1979 tests
.binary
= false;
1986 timeout
= atoi(optarg
);
1989 fprintf(stderr
, "Invalid timeout. Please specify a number for -t\n");
1990 return EXIT_FAILURE
;
1993 case 'v': verbose
= true;
1995 case 'c': do_core
= true;
1997 case 'h': hostname
= optarg
;
1999 case 'p': port
= optarg
;
2001 case 'P': prompt
= true;
2003 case 'T': testname
= optarg
;
2006 fprintf(stderr
, "Usage: %s [-h hostname] [-p port] [-c] [-v] [-t n] [-P] [-T testname]'\n"
2007 "\t-c\tGenerate coredump if a test fails\n"
2008 "\t-v\tVerbose test output (print out the assertion)\n"
2009 "\t-t n\tSet the timeout for io-operations to n seconds\n"
2010 "\t-P\tPrompt the user before starting a test.\n"
2011 "\t\t\t\"skip\" will skip the test\n"
2012 "\t\t\t\"quit\" will terminate memcapable\n"
2013 "\t\t\tEverything else will start the test\n"
2014 "\t-T n\tJust run the test named n\n"
2015 "\t-a\tOnly test the ascii protocol\n"
2016 "\t-b\tOnly test the binary protocol\n",
2018 return EXIT_FAILURE
;
2022 initialize_sockets();
2023 sock
= connect_server(hostname
, port
);
2024 if (sock
== INVALID_SOCKET
)
2026 fprintf(stderr
, "Failed to connect to <%s:%s>: %s\n",
2027 hostname
, port
, strerror(get_socket_errno()));
2028 return EXIT_FAILURE
;
2031 for (int ii
= 0; testcases
[ii
].description
!= NULL
; ++ii
)
2033 if (testname
!= NULL
&& strcmp(testcases
[ii
].description
, testname
) != 0)
2036 if ((testcases
[ii
].description
[0] == 'a' && (tests
.ascii
) == 0) ||
2037 (testcases
[ii
].description
[0] == 'b' && (tests
.binary
) == 0))
2042 fprintf(stdout
, "%-40s", testcases
[ii
].description
);
2047 fprintf(stdout
, "\nPress <return> when you are ready? ");
2048 char buffer
[80] = {0};
2049 if (fgets(buffer
, sizeof(buffer
), stdin
) != NULL
) {
2050 if (strncmp(buffer
, "skip", 4) == 0)
2052 fprintf(stdout
, "%-40s%s\n", testcases
[ii
].description
,
2053 status_msg
[TEST_SKIP
]);
2057 if (strncmp(buffer
, "quit", 4) == 0)
2061 fprintf(stdout
, "%-40s", testcases
[ii
].description
);
2065 bool reconnect
= false;
2066 enum test_return ret
= testcases
[ii
].function();
2067 if (ret
== TEST_FAIL
)
2072 fprintf(stderr
, "\n");
2074 else if (ret
== TEST_PASS_RECONNECT
)
2077 fprintf(stderr
, "%s\n", status_msg
[ret
]);
2081 if ((sock
= connect_server(hostname
, port
)) == INVALID_SOCKET
)
2083 fprintf(stderr
, "Failed to connect to <%s:%s>: %s\n",
2084 hostname
, port
, strerror(get_socket_errno()));
2085 fprintf(stderr
, "%d of %d tests failed\n", failed
, total
);
2086 return EXIT_FAILURE
;
2093 fprintf(stdout
, "All tests passed\n");
2095 fprintf(stderr
, "%d of %d tests failed\n", failed
, total
);
2097 return (failed
== 0) ? 0 : 1;