Merge in code for C++ compiling of libmemcached.
[awesomized/libmemcached] / clients / memcapable.cc
1 /* LibMemcached
2 * Copyright (C) 2006-2009 Brian Aker
3 * All rights reserved.
4 *
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
7 *
8 * Summary:
9 *
10 */
11
12 /* -*- Mode: C; tab-width: 2; c-basic-offset: 2; indent-tabs-mode: nil -*- */
13 #undef NDEBUG
14
15 #include "config.h"
16 #include <pthread.h>
17 #include <sys/types.h>
18 #include <fcntl.h>
19 #include <signal.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <errno.h>
23 #include <assert.h>
24 #include <string.h>
25 #include <inttypes.h>
26 #include <stdbool.h>
27 #include <unistd.h>
28 #include <ctype.h>
29
30 #include <libmemcached/memcached.h>
31 #include <libmemcached/memcached/protocol_binary.h>
32 #include <libmemcached/byteorder.h>
33 #include "utilities.h"
34
35 #ifdef linux
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 ..
40 */
41 #undef ntohs
42 #undef ntohl
43 #endif
44
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;
51
52 /* The number of seconds to wait for an IO-operation */
53 static int timeout= 2;
54
55 /*
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.
63 */
64 typedef union
65 {
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;
70 char bytes[1024];
71 } command;
72
73 typedef union
74 {
75 protocol_binary_response_no_extras plain;
76 protocol_binary_response_incr incr;
77 protocol_binary_response_decr decr;
78 char bytes[1024];
79 } response;
80
81 enum test_return
82 {
83 TEST_SKIP, TEST_PASS, TEST_PASS_RECONNECT, TEST_FAIL
84 };
85
86 /**
87 * Try to get an addrinfo struct for a given port on a given host
88 */
89 static struct addrinfo *lookuphost(const char *hostname, const char *port)
90 {
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;
97
98 int error= getaddrinfo(hostname, port, &hints, &ai);
99 if (error != 0)
100 {
101 if (error != EAI_SYSTEM)
102 fprintf(stderr, "getaddrinfo(): %s\n", gai_strerror(error));
103 else
104 perror("getaddrinfo()");
105 }
106
107 return ai;
108 }
109
110 /**
111 * Set the socket in nonblocking mode
112 * @return -1 if failure, the socket otherwise
113 */
114 static memcached_socket_t set_noblock(void)
115 {
116 #ifdef WIN32
117 u_long arg = 1;
118 if (ioctlsocket(sock, FIONBIO, &arg) == SOCKET_ERROR)
119 {
120 perror("Failed to set nonblocking io");
121 closesocket(sock);
122 return INVALID_SOCKET;
123 }
124 #else
125 int flags= fcntl(sock, F_GETFL, 0);
126 if (flags == -1)
127 {
128 perror("Failed to get socket flags");
129 closesocket(sock);
130 return INVALID_SOCKET;
131 }
132
133 if ((flags & O_NONBLOCK) != O_NONBLOCK)
134 {
135 if (fcntl(sock, F_SETFL, flags | O_NONBLOCK) == -1)
136 {
137 perror("Failed to set socket to nonblocking mode");
138 closesocket(sock);
139 return INVALID_SOCKET;
140 }
141 }
142 #endif
143 return sock;
144 }
145
146 /**
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
151 */
152 static memcached_socket_t connect_server(const char *hostname, const char *port)
153 {
154 struct addrinfo *ai= lookuphost(hostname, port);
155 sock= INVALID_SOCKET;
156 if (ai != NULL)
157 {
158 if ((sock= socket(ai->ai_family, ai->ai_socktype,
159 ai->ai_protocol)) != INVALID_SOCKET)
160 {
161 if (connect(sock, ai->ai_addr, ai->ai_addrlen) == SOCKET_ERROR)
162 {
163 fprintf(stderr, "Failed to connect socket: %s\n",
164 strerror(get_socket_errno()));
165 closesocket(sock);
166 sock= INVALID_SOCKET;
167 }
168 else
169 {
170 sock= set_noblock();
171 }
172 }
173 else
174 fprintf(stderr, "Failed to create socket: %s\n",
175 strerror(get_socket_errno()));
176
177 freeaddrinfo(ai);
178 }
179
180 return sock;
181 }
182
183 static ssize_t timeout_io_op(memcached_socket_t fd, short direction, void *buf, size_t len)
184 {
185 ssize_t ret;
186
187 if (direction == POLLOUT)
188 {
189 ret= send(fd, buf, len, 0);
190 }
191 else
192 {
193 ret= recv(fd, buf, len, 0);
194 }
195
196 if (ret == SOCKET_ERROR && get_socket_errno() == EWOULDBLOCK)
197 {
198 struct pollfd fds;
199 memset(&fds, 0, sizeof(struct pollfd));
200 fds.events= direction;
201 fds.fd= fd;
202
203 int err= poll(&fds, 1, timeout * 1000);
204 if (err == 1)
205 {
206 if (direction == POLLOUT)
207 {
208 ret= send(fd, buf, len, 0);
209 }
210 else
211 {
212 ret= recv(fd, buf, len, 0);
213 }
214 }
215 else if (err == 0)
216 {
217 errno= ETIMEDOUT;
218 }
219 else
220 {
221 perror("Failed to poll");
222 return -1;
223 }
224 }
225
226 return ret;
227 }
228
229 /**
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.
233 *
234 */
235 static enum test_return ensure(bool val, const char *expression, const char *file, int line)
236 {
237 if (!val)
238 {
239 if (verbose)
240 fprintf(stderr, "\n%s:%d: %s", file, line, expression);
241
242 if (do_core)
243 abort();
244
245 return TEST_FAIL;
246 }
247
248 return TEST_PASS;
249 }
250
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)
253
254 /**
255 * Send a chunk of memory over the socket (retry if the call is iterrupted
256 */
257 static enum test_return retry_write(const void* buf, size_t len)
258 {
259 size_t offset= 0;
260 const char* ptr= static_cast<const char *>(buf);
261
262 do
263 {
264 size_t num_bytes= len - offset;
265 ssize_t nw= timeout_io_op(sock, POLLOUT, (void*)(ptr + offset), num_bytes);
266 if (nw == -1)
267 verify(get_socket_errno() == EINTR || get_socket_errno() == EAGAIN);
268 else
269 offset+= (size_t)nw;
270 } while (offset < len);
271
272 return TEST_PASS;
273 }
274
275 /**
276 * Resend a packet to the server (All fields in the command header should
277 * be in network byte order)
278 */
279 static enum test_return resend_packet(command *cmd)
280 {
281 size_t length= sizeof (protocol_binary_request_no_extras) +
282 ntohl(cmd->plain.message.header.request.bodylen);
283
284 execute(retry_write(cmd, length));
285 return TEST_PASS;
286 }
287
288 /**
289 * Send a command to the server. The command header needs to be updated
290 * to network byte order
291 */
292 static enum test_return send_packet(command *cmd)
293 {
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);
301
302 execute(resend_packet(cmd));
303 return TEST_PASS;
304 }
305
306 /**
307 * Read a fixed length chunk of data from the server
308 */
309 static enum test_return retry_read(void *buf, size_t len)
310 {
311 size_t offset= 0;
312 do
313 {
314 ssize_t nr= timeout_io_op(sock, POLLIN, ((char*) buf) + offset, len - offset);
315 switch (nr) {
316 case -1 :
317 fprintf(stderr, "Errno: %d %s\n", get_socket_errno(), strerror(errno));
318 verify(get_socket_errno() == EINTR || get_socket_errno() == EAGAIN);
319 break;
320 case 0:
321 return TEST_FAIL;
322 default:
323 offset+= (size_t)nr;
324 }
325 } while (offset < len);
326
327 return TEST_PASS;
328 }
329
330 /**
331 * Receive a response from the server and conver the fields in the header
332 * to local byte order
333 */
334 static enum test_return recv_packet(response *rsp)
335 {
336 execute(retry_read(rsp, sizeof(protocol_binary_response_no_extras)));
337
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);
347
348 size_t bodysz= rsp->plain.message.header.response.bodylen;
349 if (bodysz > 0)
350 execute(retry_read(rsp->bytes + sizeof (protocol_binary_response_no_extras), bodysz));
351
352 return TEST_PASS;
353 }
354
355 /**
356 * Create a storage command (add, set, replace etc)
357 *
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
366 */
367 static void storage_command(command *cmd,
368 uint8_t cc,
369 const void* key,
370 size_t keylen,
371 const void* dta,
372 size_t dtalen,
373 uint32_t flags,
374 uint32_t exptime)
375 {
376 /* all of the storage commands use the same command layout */
377 protocol_binary_request_set *request= &cmd->set;
378
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;
388
389 off_t key_offset= sizeof (protocol_binary_request_no_extras) + 8;
390 memcpy(cmd->bytes + key_offset, key, keylen);
391 if (dta != NULL)
392 memcpy(cmd->bytes + key_offset + keylen, dta, dtalen);
393 }
394
395 /**
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
403 */
404 static void raw_command(command *cmd,
405 uint8_t cc,
406 const void* key,
407 size_t keylen,
408 const void* dta,
409 size_t dtalen)
410 {
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;
418
419 off_t key_offset= sizeof (protocol_binary_request_no_extras);
420
421 if (key != NULL)
422 memcpy(cmd->bytes + key_offset, key, keylen);
423
424 if (dta != NULL)
425 memcpy(cmd->bytes + key_offset + keylen, dta, dtalen);
426 }
427
428 /**
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?
434 */
435 static void flush_command(command *cmd,
436 uint8_t cc, uint32_t exptime, bool use_extra)
437 {
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;
442
443 if (exptime != 0 || use_extra)
444 {
445 cmd->flush.message.header.request.extlen= 4;
446 cmd->flush.message.body.expiration= htonl(exptime);
447 cmd->flush.message.header.request.bodylen= 4;
448 }
449 }
450
451 /**
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
459 */
460 static void arithmetic_command(command *cmd,
461 uint8_t cc,
462 const void* key,
463 size_t keylen,
464 uint64_t delta,
465 uint64_t initial,
466 uint32_t exptime)
467 {
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);
478
479 off_t key_offset= sizeof (protocol_binary_request_no_extras) + 20;
480 memcpy(cmd->bytes + key_offset, key, keylen);
481 }
482
483 /**
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
488 */
489 static enum test_return do_validate_response_header(response *rsp,
490 uint8_t cc, uint16_t status)
491 {
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);
497
498 if (status == PROTOCOL_BINARY_RESPONSE_SUCCESS)
499 {
500 switch (cc) {
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);
512 default:
513 break;
514 }
515
516 switch (cc) {
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);
526 break;
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);
535 break;
536
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);
543 break;
544
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);
549 break;
550
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);
556 break;
557
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);
563 break;
564
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);
570 break;
571
572 default:
573 /* Undefined command code */
574 break;
575 }
576 }
577 else
578 {
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)
582 {
583 verify(rsp->plain.message.header.response.keylen == 0);
584 }
585 }
586
587 return TEST_PASS;
588 }
589
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 ;-)
594 */
595 #define validate_response_header(a,b,c) \
596 do_validate_response_header(a,b,c) == TEST_PASS
597
598
599 static enum test_return send_binary_noop(void)
600 {
601 command cmd;
602 raw_command(&cmd, PROTOCOL_BINARY_CMD_NOOP, NULL, 0, NULL, 0);
603 execute(send_packet(&cmd));
604 return TEST_PASS;
605 }
606
607 static enum test_return receive_binary_noop(void)
608 {
609 response rsp;
610 execute(recv_packet(&rsp));
611 verify(validate_response_header(&rsp, PROTOCOL_BINARY_CMD_NOOP,
612 PROTOCOL_BINARY_RESPONSE_SUCCESS));
613 return TEST_PASS;
614 }
615
616 static enum test_return test_binary_noop(void)
617 {
618 execute(send_binary_noop());
619 execute(receive_binary_noop());
620 return TEST_PASS;
621 }
622
623 static enum test_return test_binary_quit_impl(uint8_t cc)
624 {
625 command cmd;
626 response rsp;
627 raw_command(&cmd, cc, NULL, 0, NULL, 0);
628
629 execute(send_packet(&cmd));
630 if (cc == PROTOCOL_BINARY_CMD_QUIT)
631 {
632 execute(recv_packet(&rsp));
633 verify(validate_response_header(&rsp, PROTOCOL_BINARY_CMD_QUIT,
634 PROTOCOL_BINARY_RESPONSE_SUCCESS));
635 }
636
637 /* Socket should be closed now, read should return EXIT_SUCCESS */
638 verify(timeout_io_op(sock, POLLIN, rsp.bytes, sizeof(rsp.bytes)) == 0);
639
640 return TEST_PASS_RECONNECT;
641 }
642
643 static enum test_return test_binary_quit(void)
644 {
645 return test_binary_quit_impl(PROTOCOL_BINARY_CMD_QUIT);
646 }
647
648 static enum test_return test_binary_quitq(void)
649 {
650 return test_binary_quit_impl(PROTOCOL_BINARY_CMD_QUITQ);
651 }
652
653 static enum test_return test_binary_set_impl(const char* key, uint8_t cc)
654 {
655 command cmd;
656 response rsp;
657
658 uint64_t value= 0xdeadbeefdeadcafe;
659 storage_command(&cmd, cc, key, strlen(key), &value, sizeof (value), 0, 0);
660
661 /* set should always work */
662 for (int ii= 0; ii < 10; ii++)
663 {
664 if (ii == 0)
665 execute(send_packet(&cmd));
666 else
667 execute(resend_packet(&cmd));
668
669 if (cc == PROTOCOL_BINARY_CMD_SET)
670 {
671 execute(recv_packet(&rsp));
672 verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_SUCCESS));
673 }
674 else
675 execute(test_binary_noop());
676 }
677
678 /*
679 * We need to get the current CAS id, and at this time we haven't
680 * verified that we have a working get
681 */
682 if (cc == PROTOCOL_BINARY_CMD_SETQ)
683 {
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;
690 }
691
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)
697 {
698 execute(recv_packet(&rsp));
699 verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_SUCCESS));
700 }
701 else
702 execute(test_binary_noop());
703
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());
712
713 return TEST_PASS;
714 }
715
716 static enum test_return test_binary_set(void)
717 {
718 return test_binary_set_impl("test_binary_set", PROTOCOL_BINARY_CMD_SET);
719 }
720
721 static enum test_return test_binary_setq(void)
722 {
723 return test_binary_set_impl("test_binary_setq", PROTOCOL_BINARY_CMD_SETQ);
724 }
725
726 static enum test_return test_binary_add_impl(const char* key, uint8_t cc)
727 {
728 command cmd;
729 response rsp;
730 uint64_t value= 0xdeadbeefdeadcafe;
731 storage_command(&cmd, cc, key, strlen(key), &value, sizeof (value), 0, 0);
732
733 /* first add should work, rest of them should fail (even with cas
734 as wildcard */
735 for (int ii=0; ii < 10; ii++)
736 {
737 if (ii == 0)
738 execute(send_packet(&cmd));
739 else
740 execute(resend_packet(&cmd));
741
742 if (cc == PROTOCOL_BINARY_CMD_ADD || ii > 0)
743 {
744 uint16_t expected_result;
745 if (ii == 0)
746 expected_result= PROTOCOL_BINARY_RESPONSE_SUCCESS;
747 else
748 expected_result= PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS;
749
750 execute(send_binary_noop());
751 execute(recv_packet(&rsp));
752 execute(receive_binary_noop());
753 verify(validate_response_header(&rsp, cc, expected_result));
754 }
755 else
756 execute(test_binary_noop());
757 }
758
759 return TEST_PASS;
760 }
761
762 static enum test_return test_binary_add(void)
763 {
764 return test_binary_add_impl("test_binary_add", PROTOCOL_BINARY_CMD_ADD);
765 }
766
767 static enum test_return test_binary_addq(void)
768 {
769 return test_binary_add_impl("test_binary_addq", PROTOCOL_BINARY_CMD_ADDQ);
770 }
771
772 static enum test_return binary_set_item(const char *key, const char *value)
773 {
774 command cmd;
775 response rsp;
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));
782 return TEST_PASS;
783 }
784
785 static enum test_return test_binary_replace_impl(const char* key, uint8_t cc)
786 {
787 command cmd;
788 response rsp;
789 uint64_t value= 0xdeadbeefdeadcafe;
790 storage_command(&cmd, cc, key, strlen(key), &value, sizeof (value), 0, 0);
791
792 /* first replace should fail, successive should succeed (when the
793 item is added! */
794 for (int ii= 0; ii < 10; ii++)
795 {
796 if (ii == 0)
797 execute(send_packet(&cmd));
798 else
799 execute(resend_packet(&cmd));
800
801 if (cc == PROTOCOL_BINARY_CMD_REPLACE || ii == 0)
802 {
803 uint16_t expected_result;
804 if (ii == 0)
805 expected_result=PROTOCOL_BINARY_RESPONSE_KEY_ENOENT;
806 else
807 expected_result=PROTOCOL_BINARY_RESPONSE_SUCCESS;
808
809 execute(send_binary_noop());
810 execute(recv_packet(&rsp));
811 execute(receive_binary_noop());
812 verify(validate_response_header(&rsp, cc, expected_result));
813
814 if (ii == 0)
815 execute(binary_set_item(key, key));
816 }
817 else
818 execute(test_binary_noop());
819 }
820
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));
825
826 if (cc == PROTOCOL_BINARY_CMD_REPLACE)
827 {
828 execute(recv_packet(&rsp));
829 verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_SUCCESS));
830 }
831 else
832 execute(test_binary_noop());
833
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));
842
843 return TEST_PASS;
844 }
845
846 static enum test_return test_binary_replace(void)
847 {
848 return test_binary_replace_impl("test_binary_replace", PROTOCOL_BINARY_CMD_REPLACE);
849 }
850
851 static enum test_return test_binary_replaceq(void)
852 {
853 return test_binary_replace_impl("test_binary_replaceq", PROTOCOL_BINARY_CMD_REPLACEQ);
854 }
855
856 static enum test_return test_binary_delete_impl(const char *key, uint8_t cc)
857 {
858 command cmd;
859 response rsp;
860 raw_command(&cmd, cc, key, strlen(key), NULL, 0);
861
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));
869
870 /* The item should be present now, resend*/
871 execute(resend_packet(&cmd));
872 if (cc == PROTOCOL_BINARY_CMD_DELETE)
873 {
874 execute(recv_packet(&rsp));
875 verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_SUCCESS));
876 }
877
878 execute(test_binary_noop());
879
880 return TEST_PASS;
881 }
882
883 static enum test_return test_binary_delete(void)
884 {
885 return test_binary_delete_impl("test_binary_delete", PROTOCOL_BINARY_CMD_DELETE);
886 }
887
888 static enum test_return test_binary_deleteq(void)
889 {
890 return test_binary_delete_impl("test_binary_deleteq", PROTOCOL_BINARY_CMD_DELETEQ);
891 }
892
893 static enum test_return test_binary_get_impl(const char *key, uint8_t cc)
894 {
895 command cmd;
896 response rsp;
897
898 raw_command(&cmd, cc, key, strlen(key), NULL, 0);
899 execute(send_packet(&cmd));
900 execute(send_binary_noop());
901
902 if (cc == PROTOCOL_BINARY_CMD_GET || cc == PROTOCOL_BINARY_CMD_GETK)
903 {
904 execute(recv_packet(&rsp));
905 verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_KEY_ENOENT));
906 }
907
908 execute(receive_binary_noop());
909
910 execute(binary_set_item(key, key));
911 execute(resend_packet(&cmd));
912 execute(send_binary_noop());
913
914 execute(recv_packet(&rsp));
915 verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_SUCCESS));
916 execute(receive_binary_noop());
917
918 return TEST_PASS;
919 }
920
921 static enum test_return test_binary_get(void)
922 {
923 return test_binary_get_impl("test_binary_get", PROTOCOL_BINARY_CMD_GET);
924 }
925
926 static enum test_return test_binary_getk(void)
927 {
928 return test_binary_get_impl("test_binary_getk", PROTOCOL_BINARY_CMD_GETK);
929 }
930
931 static enum test_return test_binary_getq(void)
932 {
933 return test_binary_get_impl("test_binary_getq", PROTOCOL_BINARY_CMD_GETQ);
934 }
935
936 static enum test_return test_binary_getkq(void)
937 {
938 return test_binary_get_impl("test_binary_getkq", PROTOCOL_BINARY_CMD_GETKQ);
939 }
940
941 static enum test_return test_binary_incr_impl(const char* key, uint8_t cc)
942 {
943 command cmd;
944 response rsp;
945 arithmetic_command(&cmd, cc, key, strlen(key), 1, 0, 0);
946
947 uint64_t ii;
948 for (ii= 0; ii < 10; ++ii)
949 {
950 if (ii == 0)
951 execute(send_packet(&cmd));
952 else
953 execute(resend_packet(&cmd));
954
955 if (cc == PROTOCOL_BINARY_CMD_INCREMENT)
956 {
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);
960 }
961 else
962 execute(test_binary_noop());
963 }
964
965 /* @todo add incorrect CAS */
966 return TEST_PASS;
967 }
968
969 static enum test_return test_binary_incr(void)
970 {
971 return test_binary_incr_impl("test_binary_incr", PROTOCOL_BINARY_CMD_INCREMENT);
972 }
973
974 static enum test_return test_binary_incrq(void)
975 {
976 return test_binary_incr_impl("test_binary_incrq", PROTOCOL_BINARY_CMD_INCREMENTQ);
977 }
978
979 static enum test_return test_binary_decr_impl(const char* key, uint8_t cc)
980 {
981 command cmd;
982 response rsp;
983 arithmetic_command(&cmd, cc, key, strlen(key), 1, 9, 0);
984
985 int ii;
986 for (ii= 9; ii > -1; --ii)
987 {
988 if (ii == 9)
989 execute(send_packet(&cmd));
990 else
991 execute(resend_packet(&cmd));
992
993 if (cc == PROTOCOL_BINARY_CMD_DECREMENT)
994 {
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);
998 }
999 else
1000 execute(test_binary_noop());
1001 }
1002
1003 /* decr 0 should not wrap */
1004 execute(resend_packet(&cmd));
1005 if (cc == PROTOCOL_BINARY_CMD_DECREMENT)
1006 {
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);
1010 }
1011 else
1012 {
1013 /* @todo get the value and verify! */
1014
1015 }
1016
1017 /* @todo add incorrect cas */
1018 execute(test_binary_noop());
1019 return TEST_PASS;
1020 }
1021
1022 static enum test_return test_binary_decr(void)
1023 {
1024 return test_binary_decr_impl("test_binary_decr",
1025 PROTOCOL_BINARY_CMD_DECREMENT);
1026 }
1027
1028 static enum test_return test_binary_decrq(void)
1029 {
1030 return test_binary_decr_impl("test_binary_decrq",
1031 PROTOCOL_BINARY_CMD_DECREMENTQ);
1032 }
1033
1034 static enum test_return test_binary_version(void)
1035 {
1036 command cmd;
1037 response rsp;
1038 raw_command(&cmd, PROTOCOL_BINARY_CMD_VERSION, NULL, 0, NULL, 0);
1039
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));
1044
1045 return TEST_PASS;
1046 }
1047
1048 static enum test_return test_binary_flush_impl(const char *key, uint8_t cc)
1049 {
1050 command cmd;
1051 response rsp;
1052
1053 for (int ii= 0; ii < 2; ++ii)
1054 {
1055 execute(binary_set_item(key, key));
1056 flush_command(&cmd, cc, 0, ii == 0);
1057 execute(send_packet(&cmd));
1058
1059 if (cc == PROTOCOL_BINARY_CMD_FLUSH)
1060 {
1061 execute(recv_packet(&rsp));
1062 verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_SUCCESS));
1063 }
1064 else
1065 execute(test_binary_noop());
1066
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));
1072 }
1073
1074 return TEST_PASS;
1075 }
1076
1077 static enum test_return test_binary_flush(void)
1078 {
1079 return test_binary_flush_impl("test_binary_flush", PROTOCOL_BINARY_CMD_FLUSH);
1080 }
1081
1082 static enum test_return test_binary_flushq(void)
1083 {
1084 return test_binary_flush_impl("test_binary_flushq", PROTOCOL_BINARY_CMD_FLUSHQ);
1085 }
1086
1087 static enum test_return test_binary_concat_impl(const char *key, uint8_t cc)
1088 {
1089 command cmd;
1090 response rsp;
1091 const char *value;
1092
1093 if (cc == PROTOCOL_BINARY_CMD_APPEND || cc == PROTOCOL_BINARY_CMD_APPENDQ)
1094 value="hello";
1095 else
1096 value=" world";
1097
1098 execute(binary_set_item(key, value));
1099
1100 if (cc == PROTOCOL_BINARY_CMD_APPEND || cc == PROTOCOL_BINARY_CMD_APPENDQ)
1101 value=" world";
1102 else
1103 value="hello";
1104
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)
1108 {
1109 execute(recv_packet(&rsp));
1110 verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_SUCCESS));
1111 }
1112 else
1113 execute(test_binary_noop());
1114
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);
1122
1123 return TEST_PASS;
1124 }
1125
1126 static enum test_return test_binary_append(void)
1127 {
1128 return test_binary_concat_impl("test_binary_append", PROTOCOL_BINARY_CMD_APPEND);
1129 }
1130
1131 static enum test_return test_binary_prepend(void)
1132 {
1133 return test_binary_concat_impl("test_binary_prepend", PROTOCOL_BINARY_CMD_PREPEND);
1134 }
1135
1136 static enum test_return test_binary_appendq(void)
1137 {
1138 return test_binary_concat_impl("test_binary_appendq", PROTOCOL_BINARY_CMD_APPENDQ);
1139 }
1140
1141 static enum test_return test_binary_prependq(void)
1142 {
1143 return test_binary_concat_impl("test_binary_prependq", PROTOCOL_BINARY_CMD_PREPENDQ);
1144 }
1145
1146 static enum test_return test_binary_stat(void)
1147 {
1148 command cmd;
1149 response rsp;
1150
1151 raw_command(&cmd, PROTOCOL_BINARY_CMD_STAT, NULL, 0, NULL, 0);
1152 execute(send_packet(&cmd));
1153
1154 do
1155 {
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);
1160
1161 return TEST_PASS;
1162 }
1163
1164 static enum test_return send_string(const char *cmd)
1165 {
1166 execute(retry_write(cmd, strlen(cmd)));
1167 return TEST_PASS;
1168 }
1169
1170 static enum test_return receive_line(char *buffer, size_t size)
1171 {
1172 size_t offset= 0;
1173 while (offset < size)
1174 {
1175 execute(retry_read(buffer + offset, 1));
1176 if (buffer[offset] == '\n')
1177 {
1178 if (offset + 1 < size)
1179 {
1180 buffer[offset + 1]= '\0';
1181 return TEST_PASS;
1182 }
1183 else
1184 return TEST_FAIL;
1185 }
1186 ++offset;
1187 }
1188
1189 return TEST_FAIL;
1190 }
1191
1192 static enum test_return receive_response(const char *msg) {
1193 char buffer[80];
1194 execute(receive_line(buffer, sizeof(buffer)));
1195 if (strcmp(msg, buffer) != 0) {
1196 fprintf(stderr, "[%s]\n", buffer);
1197 }
1198 verify(strcmp(msg, buffer) == 0);
1199 return TEST_PASS;
1200 }
1201
1202 static enum test_return receive_error_response(void)
1203 {
1204 char buffer[80];
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);
1209 return TEST_PASS;
1210 }
1211
1212 static enum test_return test_ascii_quit(void)
1213 {
1214 /* Verify that quit handles unknown options */
1215 execute(send_string("quit foo bar\r\n"));
1216 execute(receive_error_response());
1217
1218 /* quit doesn't support noreply */
1219 execute(send_string("quit noreply\r\n"));
1220 execute(receive_error_response());
1221
1222 /* Verify that quit works */
1223 execute(send_string("quit\r\n"));
1224
1225 /* Socket should be closed now, read should return EXIT_SUCCESS */
1226 char buffer[80];
1227 verify(timeout_io_op(sock, POLLIN, buffer, sizeof(buffer)) == 0);
1228 return TEST_PASS_RECONNECT;
1229
1230 }
1231
1232 static enum test_return test_ascii_version(void)
1233 {
1234 /* Verify that version command handles unknown options */
1235 execute(send_string("version foo bar\r\n"));
1236 execute(receive_error_response());
1237
1238 /* version doesn't support noreply */
1239 execute(send_string("version noreply\r\n"));
1240 execute(receive_error_response());
1241
1242 /* Verify that verify works */
1243 execute(send_string("version\r\n"));
1244 char buffer[256];
1245 execute(receive_line(buffer, sizeof(buffer)));
1246 verify(strncmp(buffer, "VERSION ", 8) == 0);
1247
1248 return TEST_PASS;
1249 }
1250
1251 static enum test_return test_ascii_verbosity(void)
1252 {
1253 /* This command does not adhere to the spec! */
1254 execute(send_string("verbosity foo bar my\r\n"));
1255 execute(receive_error_response());
1256
1257 execute(send_string("verbosity noreply\r\n"));
1258 execute(receive_error_response());
1259
1260 execute(send_string("verbosity 0 noreply\r\n"));
1261 execute(test_ascii_version());
1262
1263 execute(send_string("verbosity\r\n"));
1264 execute(receive_error_response());
1265
1266 execute(send_string("verbosity 1\r\n"));
1267 execute(receive_response("OK\r\n"));
1268
1269 execute(send_string("verbosity 0\r\n"));
1270 execute(receive_response("OK\r\n"));
1271
1272 return TEST_PASS;
1273 }
1274
1275
1276
1277 static enum test_return test_ascii_set_impl(const char* key, bool noreply)
1278 {
1279 /* @todo add tests for bogus format! */
1280 char buffer[1024];
1281 snprintf(buffer, sizeof(buffer), "set %s 0 0 5%s\r\nvalue\r\n", key, noreply ? " noreply" : "");
1282 execute(send_string(buffer));
1283
1284 if (!noreply)
1285 execute(receive_response("STORED\r\n"));
1286
1287 return test_ascii_version();
1288 }
1289
1290 static enum test_return test_ascii_set(void)
1291 {
1292 return test_ascii_set_impl("test_ascii_set", false);
1293 }
1294
1295 static enum test_return test_ascii_set_noreply(void)
1296 {
1297 return test_ascii_set_impl("test_ascii_set_noreply", true);
1298 }
1299
1300 static enum test_return test_ascii_add_impl(const char* key, bool noreply)
1301 {
1302 /* @todo add tests for bogus format! */
1303 char buffer[1024];
1304 snprintf(buffer, sizeof(buffer), "add %s 0 0 5%s\r\nvalue\r\n", key, noreply ? " noreply" : "");
1305 execute(send_string(buffer));
1306
1307 if (!noreply)
1308 execute(receive_response("STORED\r\n"));
1309
1310 execute(send_string(buffer));
1311
1312 if (!noreply)
1313 execute(receive_response("NOT_STORED\r\n"));
1314
1315 return test_ascii_version();
1316 }
1317
1318 static enum test_return test_ascii_add(void)
1319 {
1320 return test_ascii_add_impl("test_ascii_add", false);
1321 }
1322
1323 static enum test_return test_ascii_add_noreply(void)
1324 {
1325 return test_ascii_add_impl("test_ascii_add_noreply", true);
1326 }
1327
1328 static enum test_return ascii_get_unknown_value(char **key, char **value, ssize_t *ndata)
1329 {
1330 char buffer[1024];
1331
1332 execute(receive_line(buffer, sizeof(buffer)));
1333 verify(strncmp(buffer, "VALUE ", 6) == 0);
1334 char *end= strchr(buffer + 6, ' ');
1335 verify(end != NULL);
1336 *end= '\0';
1337 *key= strdup(buffer + 6);
1338 verify(*key != NULL);
1339 char *ptr= end + 1;
1340
1341 unsigned long val= strtoul(ptr, &end, 10); /* flags */
1342 verify(ptr != end);
1343 verify(val == 0);
1344 verify(end != NULL);
1345 *ndata = (ssize_t)strtoul(end, &end, 10); /* size */
1346 verify(ptr != end);
1347 verify(end != NULL);
1348 while (*end != '\n' && isspace(*end))
1349 ++end;
1350 verify(*end == '\n');
1351
1352 *value= static_cast<char*>(malloc((size_t)*ndata));
1353 verify(*value != NULL);
1354
1355 execute(retry_read(*value, (size_t)*ndata));
1356
1357 execute(retry_read(buffer, 2));
1358 verify(memcmp(buffer, "\r\n", 2) == 0);
1359
1360 return TEST_PASS;
1361 }
1362
1363 static enum test_return ascii_get_value(const char *key, const char *value)
1364 {
1365
1366 char buffer[1024];
1367 size_t datasize= strlen(value);
1368
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;
1374 char *end;
1375
1376 unsigned long val= strtoul(ptr, &end, 10); /* flags */
1377 verify(ptr != end);
1378 verify(val == 0);
1379 verify(end != NULL);
1380 val= strtoul(end, &end, 10); /* size */
1381 verify(ptr != end);
1382 verify(val == datasize);
1383 verify(end != NULL);
1384 while (*end != '\n' && isspace(*end))
1385 ++end;
1386 verify(*end == '\n');
1387
1388 execute(retry_read(buffer, datasize));
1389 verify(memcmp(buffer, value, datasize) == 0);
1390
1391 execute(retry_read(buffer, 2));
1392 verify(memcmp(buffer, "\r\n", 2) == 0);
1393
1394 return TEST_PASS;
1395 }
1396
1397 static enum test_return ascii_get_item(const char *key, const char *value,
1398 bool exist)
1399 {
1400 char buffer[1024];
1401 size_t datasize= 0;
1402 if (value != NULL)
1403 datasize= strlen(value);
1404
1405 verify(datasize < sizeof(buffer));
1406 snprintf(buffer, sizeof(buffer), "get %s\r\n", key);
1407 execute(send_string(buffer));
1408
1409 if (exist)
1410 execute(ascii_get_value(key, value));
1411
1412 execute(retry_read(buffer, 5));
1413 verify(memcmp(buffer, "END\r\n", 5) == 0);
1414
1415 return TEST_PASS;
1416 }
1417
1418 static enum test_return ascii_gets_value(const char *key, const char *value,
1419 unsigned long *cas)
1420 {
1421
1422 char buffer[1024];
1423 size_t datasize= strlen(value);
1424
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;
1430 char *end;
1431
1432 unsigned long val= strtoul(ptr, &end, 10); /* flags */
1433 verify(ptr != end);
1434 verify(val == 0);
1435 verify(end != NULL);
1436 val= strtoul(end, &end, 10); /* size */
1437 verify(ptr != end);
1438 verify(val == datasize);
1439 verify(end != NULL);
1440 *cas= strtoul(end, &end, 10); /* cas */
1441 verify(ptr != end);
1442 verify(val == datasize);
1443 verify(end != NULL);
1444
1445 while (*end != '\n' && isspace(*end))
1446 ++end;
1447 verify(*end == '\n');
1448
1449 execute(retry_read(buffer, datasize));
1450 verify(memcmp(buffer, value, datasize) == 0);
1451
1452 execute(retry_read(buffer, 2));
1453 verify(memcmp(buffer, "\r\n", 2) == 0);
1454
1455 return TEST_PASS;
1456 }
1457
1458 static enum test_return ascii_gets_item(const char *key, const char *value,
1459 bool exist, unsigned long *cas)
1460 {
1461 char buffer[1024];
1462 size_t datasize= 0;
1463 if (value != NULL)
1464 datasize= strlen(value);
1465
1466 verify(datasize < sizeof(buffer));
1467 snprintf(buffer, sizeof(buffer), "gets %s\r\n", key);
1468 execute(send_string(buffer));
1469
1470 if (exist)
1471 execute(ascii_gets_value(key, value, cas));
1472
1473 execute(retry_read(buffer, 5));
1474 verify(memcmp(buffer, "END\r\n", 5) == 0);
1475
1476 return TEST_PASS;
1477 }
1478
1479 static enum test_return ascii_set_item(const char *key, const char *value)
1480 {
1481 char buffer[300];
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"));
1488 return TEST_PASS;
1489 }
1490
1491 static enum test_return test_ascii_replace_impl(const char* key, bool noreply)
1492 {
1493 char buffer[1024];
1494 snprintf(buffer, sizeof(buffer), "replace %s 0 0 5%s\r\nvalue\r\n", key, noreply ? " noreply" : "");
1495 execute(send_string(buffer));
1496
1497 if (noreply)
1498 execute(test_ascii_version());
1499 else
1500 execute(receive_response("NOT_STORED\r\n"));
1501
1502 execute(ascii_set_item(key, "value"));
1503 execute(ascii_get_item(key, "value", true));
1504
1505
1506 execute(send_string(buffer));
1507
1508 if (noreply)
1509 execute(test_ascii_version());
1510 else
1511 execute(receive_response("STORED\r\n"));
1512
1513 return test_ascii_version();
1514 }
1515
1516 static enum test_return test_ascii_replace(void)
1517 {
1518 return test_ascii_replace_impl("test_ascii_replace", false);
1519 }
1520
1521 static enum test_return test_ascii_replace_noreply(void)
1522 {
1523 return test_ascii_replace_impl("test_ascii_replace_noreply", true);
1524 }
1525
1526 static enum test_return test_ascii_cas_impl(const char* key, bool noreply)
1527 {
1528 char buffer[1024];
1529 unsigned long cas;
1530
1531 execute(ascii_set_item(key, "value"));
1532 execute(ascii_gets_item(key, "value", true, &cas));
1533
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));
1536
1537 if (noreply)
1538 execute(test_ascii_version());
1539 else
1540 execute(receive_response("STORED\r\n"));
1541
1542 /* reexecute the same command should fail due to illegal cas */
1543 execute(send_string(buffer));
1544
1545 if (noreply)
1546 execute(test_ascii_version());
1547 else
1548 execute(receive_response("EXISTS\r\n"));
1549
1550 return test_ascii_version();
1551 }
1552
1553 static enum test_return test_ascii_cas(void)
1554 {
1555 return test_ascii_cas_impl("test_ascii_cas", false);
1556 }
1557
1558 static enum test_return test_ascii_cas_noreply(void)
1559 {
1560 return test_ascii_cas_impl("test_ascii_cas_noreply", true);
1561 }
1562
1563 static enum test_return test_ascii_delete_impl(const char *key, bool noreply)
1564 {
1565 execute(ascii_set_item(key, "value"));
1566
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());
1572
1573 char buffer[1024];
1574 snprintf(buffer, sizeof(buffer), "delete %s%s\r\n", key, noreply ? " noreply" : "");
1575 execute(send_string(buffer));
1576
1577 if (noreply)
1578 execute(test_ascii_version());
1579 else
1580 execute(receive_response("DELETED\r\n"));
1581
1582 execute(ascii_get_item(key, "value", false));
1583 execute(send_string(buffer));
1584 if (noreply)
1585 execute(test_ascii_version());
1586 else
1587 execute(receive_response("NOT_FOUND\r\n"));
1588
1589 return TEST_PASS;
1590 }
1591
1592 static enum test_return test_ascii_delete(void)
1593 {
1594 return test_ascii_delete_impl("test_ascii_delete", false);
1595 }
1596
1597 static enum test_return test_ascii_delete_noreply(void)
1598 {
1599 return test_ascii_delete_impl("test_ascii_delete_noreply", true);
1600 }
1601
1602 static enum test_return test_ascii_get(void)
1603 {
1604 execute(ascii_set_item("test_ascii_get", "value"));
1605
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));
1610
1611 return TEST_PASS;
1612 }
1613
1614 static enum test_return test_ascii_gets(void)
1615 {
1616 execute(ascii_set_item("test_ascii_gets", "value"));
1617
1618 execute(send_string("gets\r\n"));
1619 execute(receive_error_response());
1620 unsigned long cas;
1621 execute(ascii_gets_item("test_ascii_gets", "value", true, &cas));
1622 execute(ascii_gets_item("test_ascii_gets_notfound", "value", false, &cas));
1623
1624 return TEST_PASS;
1625 }
1626
1627 static enum test_return test_ascii_mget(void)
1628 {
1629 const uint32_t nkeys= 5;
1630 const char * const keys[]= {
1631 "test_ascii_mget1",
1632 "test_ascii_mget2",
1633 /* test_ascii_mget_3 does not exist :) */
1634 "test_ascii_mget4",
1635 "test_ascii_mget5",
1636 "test_ascii_mget6"
1637 };
1638
1639 for (uint32_t x= 0; x < nkeys; ++x)
1640 execute(ascii_set_item(keys[x], "value"));
1641
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"));
1646
1647 char *returned[nkeys];
1648
1649 for (uint32_t x= 0; x < nkeys; ++x)
1650 {
1651 ssize_t nbytes = 0;
1652 char *v= NULL;
1653 execute(ascii_get_unknown_value(&returned[x], &v, &nbytes));
1654 verify(nbytes == 5);
1655 verify(memcmp(v, "value", 5) == 0);
1656 free(v);
1657 }
1658
1659 char buffer[5];
1660 execute(retry_read(buffer, 5));
1661 verify(memcmp(buffer, "END\r\n", 5) == 0);
1662
1663 /* verify that we got all the keys we expected */
1664 for (uint32_t x= 0; x < nkeys; ++x)
1665 {
1666 bool found= false;
1667 for (uint32_t y= 0; y < nkeys; ++y)
1668 {
1669 if (strcmp(keys[x], returned[y]) == 0)
1670 {
1671 found = true;
1672 break;
1673 }
1674 }
1675 verify(found);
1676 }
1677
1678 for (uint32_t x= 0; x < nkeys; ++x)
1679 free(returned[x]);
1680
1681 return TEST_PASS;
1682 }
1683
1684 static enum test_return test_ascii_incr_impl(const char* key, bool noreply)
1685 {
1686 char cmd[300];
1687 snprintf(cmd, sizeof(cmd), "incr %s 1%s\r\n", key, noreply ? " noreply" : "");
1688
1689 execute(ascii_set_item(key, "0"));
1690 for (int x= 1; x < 11; ++x)
1691 {
1692 execute(send_string(cmd));
1693
1694 if (noreply)
1695 execute(test_ascii_version());
1696 else
1697 {
1698 char buffer[80];
1699 execute(receive_line(buffer, sizeof(buffer)));
1700 int val= atoi(buffer);
1701 verify(val == x);
1702 }
1703 }
1704
1705 execute(ascii_get_item(key, "10", true));
1706
1707 return TEST_PASS;
1708 }
1709
1710 static enum test_return test_ascii_incr(void)
1711 {
1712 return test_ascii_incr_impl("test_ascii_incr", false);
1713 }
1714
1715 static enum test_return test_ascii_incr_noreply(void)
1716 {
1717 return test_ascii_incr_impl("test_ascii_incr_noreply", true);
1718 }
1719
1720 static enum test_return test_ascii_decr_impl(const char* key, bool noreply)
1721 {
1722 char cmd[300];
1723 snprintf(cmd, sizeof(cmd), "decr %s 1%s\r\n", key, noreply ? " noreply" : "");
1724
1725 execute(ascii_set_item(key, "9"));
1726 for (int x= 8; x > -1; --x)
1727 {
1728 execute(send_string(cmd));
1729
1730 if (noreply)
1731 execute(test_ascii_version());
1732 else
1733 {
1734 char buffer[80];
1735 execute(receive_line(buffer, sizeof(buffer)));
1736 int val= atoi(buffer);
1737 verify(val == x);
1738 }
1739 }
1740
1741 execute(ascii_get_item(key, "0", true));
1742
1743 /* verify that it doesn't wrap */
1744 execute(send_string(cmd));
1745 if (noreply)
1746 execute(test_ascii_version());
1747 else
1748 {
1749 char buffer[80];
1750 execute(receive_line(buffer, sizeof(buffer)));
1751 }
1752 execute(ascii_get_item(key, "0", true));
1753
1754 return TEST_PASS;
1755 }
1756
1757 static enum test_return test_ascii_decr(void)
1758 {
1759 return test_ascii_decr_impl("test_ascii_decr", false);
1760 }
1761
1762 static enum test_return test_ascii_decr_noreply(void)
1763 {
1764 return test_ascii_decr_impl("test_ascii_decr_noreply", true);
1765 }
1766
1767
1768 static enum test_return test_ascii_flush_impl(const char *key, bool noreply)
1769 {
1770 #if 0
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());
1775 #endif
1776
1777 execute(ascii_set_item(key, key));
1778 execute(ascii_get_item(key, key, true));
1779
1780 if (noreply)
1781 {
1782 execute(send_string("flush_all noreply\r\n"));
1783 execute(test_ascii_version());
1784 }
1785 else
1786 {
1787 execute(send_string("flush_all\r\n"));
1788 execute(receive_response("OK\r\n"));
1789 }
1790
1791 execute(ascii_get_item(key, key, false));
1792
1793 return TEST_PASS;
1794 }
1795
1796 static enum test_return test_ascii_flush(void)
1797 {
1798 return test_ascii_flush_impl("test_ascii_flush", false);
1799 }
1800
1801 static enum test_return test_ascii_flush_noreply(void)
1802 {
1803 return test_ascii_flush_impl("test_ascii_flush_noreply", true);
1804 }
1805
1806 static enum test_return test_ascii_concat_impl(const char *key,
1807 bool append,
1808 bool noreply)
1809 {
1810 const char *value;
1811
1812 if (append)
1813 value="hello";
1814 else
1815 value=" world";
1816
1817 execute(ascii_set_item(key, value));
1818
1819 if (append)
1820 value=" world";
1821 else
1822 value="hello";
1823
1824 char cmd[400];
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" : "",
1828 value);
1829 execute(send_string(cmd));
1830
1831 if (noreply)
1832 execute(test_ascii_version());
1833 else
1834 execute(receive_response("STORED\r\n"));
1835
1836 execute(ascii_get_item(key, "hello world", true));
1837
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" : "",
1841 value);
1842 execute(send_string(cmd));
1843
1844 if (noreply)
1845 execute(test_ascii_version());
1846 else
1847 execute(receive_response("NOT_STORED\r\n"));
1848
1849 return TEST_PASS;
1850 }
1851
1852 static enum test_return test_ascii_append(void)
1853 {
1854 return test_ascii_concat_impl("test_ascii_append", true, false);
1855 }
1856
1857 static enum test_return test_ascii_prepend(void)
1858 {
1859 return test_ascii_concat_impl("test_ascii_prepend", false, false);
1860 }
1861
1862 static enum test_return test_ascii_append_noreply(void)
1863 {
1864 return test_ascii_concat_impl("test_ascii_append_noreply", true, true);
1865 }
1866
1867 static enum test_return test_ascii_prepend_noreply(void)
1868 {
1869 return test_ascii_concat_impl("test_ascii_prepend_noreply", false, true);
1870 }
1871
1872 static enum test_return test_ascii_stat(void)
1873 {
1874 execute(send_string("stats noreply\r\n"));
1875 execute(receive_error_response());
1876 execute(send_string("stats\r\n"));
1877 char buffer[1024];
1878 do {
1879 execute(receive_line(buffer, sizeof(buffer)));
1880 } while (strcmp(buffer, "END\r\n") != 0);
1881
1882 return TEST_PASS_RECONNECT;
1883 }
1884
1885 typedef enum test_return(*TEST_FUNC)(void);
1886
1887 struct testcase
1888 {
1889 const char *description;
1890 TEST_FUNC function;
1891 };
1892
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 },
1948 { NULL, NULL}
1949 };
1950
1951 const int ascii_tests = 1;
1952 const int binary_tests = 2;
1953
1954 struct test_type_st
1955 {
1956 bool ascii;
1957 bool binary;
1958 };
1959
1960 int main(int argc, char **argv)
1961 {
1962 static const char * const status_msg[]= {"[skip]", "[pass]", "[pass]", "[FAIL]"};
1963 struct test_type_st tests= { true, true };
1964 int total= 0;
1965 int failed= 0;
1966 const char *hostname= "localhost";
1967 const char *port= "11211";
1968 int cmd;
1969 bool prompt= false;
1970 const char *testname= NULL;
1971
1972
1973
1974 while ((cmd= getopt(argc, argv, "t:vch:p:PT:?ab")) != EOF)
1975 {
1976 switch (cmd) {
1977 case 'a':
1978 tests.ascii= true;
1979 tests.binary= false;
1980 break;
1981 case 'b':
1982 tests.ascii= false;
1983 tests.binary= true;
1984 break;
1985 case 't':
1986 timeout= atoi(optarg);
1987 if (timeout == 0)
1988 {
1989 fprintf(stderr, "Invalid timeout. Please specify a number for -t\n");
1990 return EXIT_FAILURE;
1991 }
1992 break;
1993 case 'v': verbose= true;
1994 break;
1995 case 'c': do_core= true;
1996 break;
1997 case 'h': hostname= optarg;
1998 break;
1999 case 'p': port= optarg;
2000 break;
2001 case 'P': prompt= true;
2002 break;
2003 case 'T': testname= optarg;
2004 break;
2005 default:
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",
2017 argv[0]);
2018 return EXIT_FAILURE;
2019 }
2020 }
2021
2022 initialize_sockets();
2023 sock= connect_server(hostname, port);
2024 if (sock == INVALID_SOCKET)
2025 {
2026 fprintf(stderr, "Failed to connect to <%s:%s>: %s\n",
2027 hostname, port, strerror(get_socket_errno()));
2028 return EXIT_FAILURE;
2029 }
2030
2031 for (int ii= 0; testcases[ii].description != NULL; ++ii)
2032 {
2033 if (testname != NULL && strcmp(testcases[ii].description, testname) != 0)
2034 continue;
2035
2036 if ((testcases[ii].description[0] == 'a' && (tests.ascii) == 0) ||
2037 (testcases[ii].description[0] == 'b' && (tests.binary) == 0))
2038 {
2039 continue;
2040 }
2041 ++total;
2042 fprintf(stdout, "%-40s", testcases[ii].description);
2043 fflush(stdout);
2044
2045 if (prompt)
2046 {
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)
2051 {
2052 fprintf(stdout, "%-40s%s\n", testcases[ii].description,
2053 status_msg[TEST_SKIP]);
2054 fflush(stdout);
2055 continue;
2056 }
2057 if (strncmp(buffer, "quit", 4) == 0)
2058 exit(0);
2059 }
2060
2061 fprintf(stdout, "%-40s", testcases[ii].description);
2062 fflush(stdout);
2063 }
2064
2065 bool reconnect= false;
2066 enum test_return ret= testcases[ii].function();
2067 if (ret == TEST_FAIL)
2068 {
2069 reconnect= true;
2070 ++failed;
2071 if (verbose)
2072 fprintf(stderr, "\n");
2073 }
2074 else if (ret == TEST_PASS_RECONNECT)
2075 reconnect= true;
2076
2077 fprintf(stderr, "%s\n", status_msg[ret]);
2078 if (reconnect)
2079 {
2080 closesocket(sock);
2081 if ((sock= connect_server(hostname, port)) == INVALID_SOCKET)
2082 {
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;
2087 }
2088 }
2089 }
2090
2091 closesocket(sock);
2092 if (failed == 0)
2093 fprintf(stdout, "All tests passed\n");
2094 else
2095 fprintf(stderr, "%d of %d tests failed\n", failed, total);
2096
2097 return (failed == 0) ? 0 : 1;
2098 }