Merge
[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
17 #ifdef HAVE_POLL_H
18 #include <poll.h>
19 #else
20 #include "poll/poll.h"
21 #endif
22
23 #include <cassert>
24 #include <cerrno>
25 #include <cstdio>
26 #include <cstdlib>
27 #include <cstring>
28 #include <ctype.h>
29 #include <fcntl.h>
30 #include <inttypes.h>
31 #include <pthread.h>
32 #include <signal.h>
33 #include <sys/types.h>
34 #include <unistd.h>
35
36 #include <libmemcached-1.0/memcached.h>
37 #include <libmemcached/close_socket.hpp>
38 #include <libmemcached/memcached/protocol_binary.h>
39 #include <libmemcached/byteorder.h>
40 #include <clients/utilities.h>
41
42 #ifdef linux
43 /* /usr/include/netinet/in.h defines macros from ntohs() to _bswap_nn to
44 * optimize the conversion functions, but the prototypes generate warnings
45 * from gcc. The conversion methods isn't the bottleneck for my app, so
46 * just remove the warnings by undef'ing the optimization ..
47 */
48 #undef ntohs
49 #undef ntohl
50 #endif
51
52 /* Should we generate coredumps when we enounter an error (-c) */
53 static bool do_core= false;
54 /* connection to the server */
55 static memcached_socket_t sock;
56 /* Should the output from test failures be verbose or quiet? */
57 static bool verbose= false;
58
59 /* The number of seconds to wait for an IO-operation */
60 static int timeout= 2;
61
62 /*
63 * Instead of having to cast between the different datatypes we create
64 * a union of all of the different types of pacages we want to send.
65 * A lot of the different commands use the same packet layout, so I'll
66 * just define the different types I need. The typedefs only contain
67 * the header of the message, so we need some space for keys and body
68 * To avoid to have to do multiple writes, lets add a chunk of memory
69 * to use. 1k should be more than enough for header, key and body.
70 */
71 typedef union
72 {
73 protocol_binary_request_no_extras plain;
74 protocol_binary_request_flush flush;
75 protocol_binary_request_incr incr;
76 protocol_binary_request_set set;
77 char bytes[1024];
78 } command;
79
80 typedef union
81 {
82 protocol_binary_response_no_extras plain;
83 protocol_binary_response_incr incr;
84 protocol_binary_response_decr decr;
85 char bytes[1024];
86 } response;
87
88 enum test_return
89 {
90 TEST_SKIP, TEST_PASS, TEST_PASS_RECONNECT, TEST_FAIL
91 };
92
93 /**
94 * Try to get an addrinfo struct for a given port on a given host
95 */
96 static struct addrinfo *lookuphost(const char *hostname, const char *port)
97 {
98 struct addrinfo *ai= 0;
99 struct addrinfo hints;
100 memset(&hints, 0, sizeof(struct addrinfo));
101 hints.ai_family=AF_UNSPEC;
102 hints.ai_protocol=IPPROTO_TCP;
103 hints.ai_socktype=SOCK_STREAM;
104
105 int error= getaddrinfo(hostname, port, &hints, &ai);
106 if (error != 0)
107 {
108 if (error != EAI_SYSTEM)
109 fprintf(stderr, "getaddrinfo(): %s\n", gai_strerror(error));
110 else
111 perror("getaddrinfo()");
112 }
113
114 return ai;
115 }
116
117 /**
118 * Set the socket in nonblocking mode
119 * @return -1 if failure, the socket otherwise
120 */
121 static memcached_socket_t set_noblock(void)
122 {
123 #ifdef WIN32
124 u_long arg = 1;
125 if (ioctlsocket(sock, FIONBIO, &arg) == SOCKET_ERROR)
126 {
127 perror("Failed to set nonblocking io");
128 closesocket(sock);
129 return INVALID_SOCKET;
130 }
131 #else
132 int flags= fcntl(sock, F_GETFL, 0);
133 if (flags == -1)
134 {
135 perror("Failed to get socket flags");
136 memcached_close_socket(sock);
137 return INVALID_SOCKET;
138 }
139
140 if ((flags & O_NONBLOCK) != O_NONBLOCK)
141 {
142 if (fcntl(sock, F_SETFL, flags | O_NONBLOCK) == -1)
143 {
144 perror("Failed to set socket to nonblocking mode");
145 memcached_close_socket(sock);
146 return INVALID_SOCKET;
147 }
148 }
149 #endif
150 return sock;
151 }
152
153 /**
154 * Try to open a connection to the server
155 * @param hostname the name of the server to connect to
156 * @param port the port number (or service) to connect to
157 * @return positive integer if success, -1 otherwise
158 */
159 static memcached_socket_t connect_server(const char *hostname, const char *port)
160 {
161 struct addrinfo *ai= lookuphost(hostname, port);
162 sock= INVALID_SOCKET;
163 if (ai != NULL)
164 {
165 if ((sock= socket(ai->ai_family, ai->ai_socktype,
166 ai->ai_protocol)) != INVALID_SOCKET)
167 {
168 if (connect(sock, ai->ai_addr, ai->ai_addrlen) == SOCKET_ERROR)
169 {
170 fprintf(stderr, "Failed to connect socket: %s\n",
171 strerror(get_socket_errno()));
172 closesocket(sock);
173 sock= INVALID_SOCKET;
174 }
175 else
176 {
177 sock= set_noblock();
178 }
179 }
180 else
181 fprintf(stderr, "Failed to create socket: %s\n",
182 strerror(get_socket_errno()));
183
184 freeaddrinfo(ai);
185 }
186
187 return sock;
188 }
189
190 static ssize_t timeout_io_op(memcached_socket_t fd, short direction, void *buf, size_t len)
191 {
192 ssize_t ret;
193
194 if (direction == POLLOUT)
195 {
196 ret= send(fd, buf, len, 0);
197 }
198 else
199 {
200 ret= recv(fd, buf, len, 0);
201 }
202
203 if (ret == SOCKET_ERROR && get_socket_errno() == EWOULDBLOCK)
204 {
205 struct pollfd fds;
206 memset(&fds, 0, sizeof(struct pollfd));
207 fds.events= direction;
208 fds.fd= fd;
209
210 int err= poll(&fds, 1, timeout * 1000);
211 if (err == 1)
212 {
213 if (direction == POLLOUT)
214 {
215 ret= send(fd, buf, len, 0);
216 }
217 else
218 {
219 ret= recv(fd, buf, len, 0);
220 }
221 }
222 else if (err == 0)
223 {
224 errno= ETIMEDOUT;
225 }
226 else
227 {
228 perror("Failed to poll");
229 return -1;
230 }
231 }
232
233 return ret;
234 }
235
236 /**
237 * Ensure that an expression is true. If it isn't print out a message similar
238 * to assert() and create a coredump if the user wants that. If not an error
239 * message is returned.
240 *
241 */
242 static enum test_return ensure(bool val, const char *expression, const char *file, int line)
243 {
244 if (!val)
245 {
246 if (verbose)
247 fprintf(stderr, "\n%s:%d: %s", file, line, expression);
248
249 if (do_core)
250 abort();
251
252 return TEST_FAIL;
253 }
254
255 return TEST_PASS;
256 }
257
258 #define verify(expression) do { if (ensure(expression, #expression, __FILE__, __LINE__) == TEST_FAIL) return TEST_FAIL; } while (0)
259 #define execute(expression) do { if (ensure(expression == TEST_PASS, #expression, __FILE__, __LINE__) == TEST_FAIL) return TEST_FAIL; } while (0)
260
261 /**
262 * Send a chunk of memory over the socket (retry if the call is iterrupted
263 */
264 static enum test_return retry_write(const void* buf, size_t len)
265 {
266 size_t offset= 0;
267 const char* ptr= static_cast<const char *>(buf);
268
269 do
270 {
271 size_t num_bytes= len - offset;
272 ssize_t nw= timeout_io_op(sock, POLLOUT, (void*)(ptr + offset), num_bytes);
273 if (nw == -1)
274 verify(get_socket_errno() == EINTR || get_socket_errno() == EAGAIN);
275 else
276 offset+= (size_t)nw;
277 } while (offset < len);
278
279 return TEST_PASS;
280 }
281
282 /**
283 * Resend a packet to the server (All fields in the command header should
284 * be in network byte order)
285 */
286 static enum test_return resend_packet(command *cmd)
287 {
288 size_t length= sizeof (protocol_binary_request_no_extras) +
289 ntohl(cmd->plain.message.header.request.bodylen);
290
291 execute(retry_write(cmd, length));
292 return TEST_PASS;
293 }
294
295 /**
296 * Send a command to the server. The command header needs to be updated
297 * to network byte order
298 */
299 static enum test_return send_packet(command *cmd)
300 {
301 /* Fix the byteorder of the header */
302 cmd->plain.message.header.request.keylen=
303 ntohs(cmd->plain.message.header.request.keylen);
304 cmd->plain.message.header.request.bodylen=
305 ntohl(cmd->plain.message.header.request.bodylen);
306 cmd->plain.message.header.request.cas=
307 memcached_ntohll(cmd->plain.message.header.request.cas);
308
309 execute(resend_packet(cmd));
310 return TEST_PASS;
311 }
312
313 /**
314 * Read a fixed length chunk of data from the server
315 */
316 static enum test_return retry_read(void *buf, size_t len)
317 {
318 size_t offset= 0;
319 do
320 {
321 ssize_t nr= timeout_io_op(sock, POLLIN, ((char*) buf) + offset, len - offset);
322 switch (nr) {
323 case -1 :
324 fprintf(stderr, "Errno: %d %s\n", get_socket_errno(), strerror(errno));
325 verify(get_socket_errno() == EINTR || get_socket_errno() == EAGAIN);
326 break;
327 case 0:
328 return TEST_FAIL;
329 default:
330 offset+= (size_t)nr;
331 }
332 } while (offset < len);
333
334 return TEST_PASS;
335 }
336
337 /**
338 * Receive a response from the server and conver the fields in the header
339 * to local byte order
340 */
341 static enum test_return recv_packet(response *rsp)
342 {
343 execute(retry_read(rsp, sizeof(protocol_binary_response_no_extras)));
344
345 /* Fix the byte order in the packet header */
346 rsp->plain.message.header.response.keylen=
347 ntohs(rsp->plain.message.header.response.keylen);
348 rsp->plain.message.header.response.status=
349 ntohs(rsp->plain.message.header.response.status);
350 rsp->plain.message.header.response.bodylen=
351 ntohl(rsp->plain.message.header.response.bodylen);
352 rsp->plain.message.header.response.cas=
353 memcached_ntohll(rsp->plain.message.header.response.cas);
354
355 size_t bodysz= rsp->plain.message.header.response.bodylen;
356 if (bodysz > 0)
357 execute(retry_read(rsp->bytes + sizeof (protocol_binary_response_no_extras), bodysz));
358
359 return TEST_PASS;
360 }
361
362 /**
363 * Create a storage command (add, set, replace etc)
364 *
365 * @param cmd destination buffer
366 * @param cc the storage command to create
367 * @param key the key to store
368 * @param keylen the length of the key
369 * @param dta the data to store with the key
370 * @param dtalen the length of the data to store with the key
371 * @param flags the flags to store along with the key
372 * @param exptime the expiry time for the key
373 */
374 static void storage_command(command *cmd,
375 uint8_t cc,
376 const void* key,
377 size_t keylen,
378 const void* dta,
379 size_t dtalen,
380 uint32_t flags,
381 uint32_t exptime)
382 {
383 /* all of the storage commands use the same command layout */
384 protocol_binary_request_set *request= &cmd->set;
385
386 memset(request, 0, sizeof (*request));
387 request->message.header.request.magic= PROTOCOL_BINARY_REQ;
388 request->message.header.request.opcode= cc;
389 request->message.header.request.keylen= (uint16_t)keylen;
390 request->message.header.request.extlen= 8;
391 request->message.header.request.bodylen= (uint32_t)(keylen + 8 + dtalen);
392 request->message.header.request.opaque= 0xdeadbeef;
393 request->message.body.flags= flags;
394 request->message.body.expiration= exptime;
395
396 off_t key_offset= sizeof (protocol_binary_request_no_extras) + 8;
397 memcpy(cmd->bytes + key_offset, key, keylen);
398 if (dta != NULL)
399 memcpy(cmd->bytes + key_offset + keylen, dta, dtalen);
400 }
401
402 /**
403 * Create a basic command to send to the server
404 * @param cmd destination buffer
405 * @param cc the command to create
406 * @param key the key to store
407 * @param keylen the length of the key
408 * @param dta the data to store with the key
409 * @param dtalen the length of the data to store with the key
410 */
411 static void raw_command(command *cmd,
412 uint8_t cc,
413 const void* key,
414 size_t keylen,
415 const void* dta,
416 size_t dtalen)
417 {
418 /* all of the storage commands use the same command layout */
419 memset(cmd, 0, sizeof (*cmd));
420 cmd->plain.message.header.request.magic= PROTOCOL_BINARY_REQ;
421 cmd->plain.message.header.request.opcode= cc;
422 cmd->plain.message.header.request.keylen= (uint16_t)keylen;
423 cmd->plain.message.header.request.bodylen= (uint32_t)(keylen + dtalen);
424 cmd->plain.message.header.request.opaque= 0xdeadbeef;
425
426 off_t key_offset= sizeof (protocol_binary_request_no_extras);
427
428 if (key != NULL)
429 memcpy(cmd->bytes + key_offset, key, keylen);
430
431 if (dta != NULL)
432 memcpy(cmd->bytes + key_offset + keylen, dta, dtalen);
433 }
434
435 /**
436 * Create the flush command
437 * @param cmd destination buffer
438 * @param cc the command to create (FLUSH/FLUSHQ)
439 * @param exptime when to flush
440 * @param use_extra to force using of the extra field?
441 */
442 static void flush_command(command *cmd,
443 uint8_t cc, uint32_t exptime, bool use_extra)
444 {
445 memset(cmd, 0, sizeof (cmd->flush));
446 cmd->flush.message.header.request.magic= PROTOCOL_BINARY_REQ;
447 cmd->flush.message.header.request.opcode= cc;
448 cmd->flush.message.header.request.opaque= 0xdeadbeef;
449
450 if (exptime != 0 || use_extra)
451 {
452 cmd->flush.message.header.request.extlen= 4;
453 cmd->flush.message.body.expiration= htonl(exptime);
454 cmd->flush.message.header.request.bodylen= 4;
455 }
456 }
457
458 /**
459 * Create a incr/decr command
460 * @param cc the cmd to create (FLUSH/FLUSHQ)
461 * @param key the key to operate on
462 * @param keylen the number of bytes in the key
463 * @param delta the number to add/subtract
464 * @param initial the initial value if the key doesn't exist
465 * @param exptime when the key should expire if it isn't set
466 */
467 static void arithmetic_command(command *cmd,
468 uint8_t cc,
469 const void* key,
470 size_t keylen,
471 uint64_t delta,
472 uint64_t initial,
473 uint32_t exptime)
474 {
475 memset(cmd, 0, sizeof (cmd->incr));
476 cmd->incr.message.header.request.magic= PROTOCOL_BINARY_REQ;
477 cmd->incr.message.header.request.opcode= cc;
478 cmd->incr.message.header.request.keylen= (uint16_t)keylen;
479 cmd->incr.message.header.request.extlen= 20;
480 cmd->incr.message.header.request.bodylen= (uint32_t)(keylen + 20);
481 cmd->incr.message.header.request.opaque= 0xdeadbeef;
482 cmd->incr.message.body.delta= memcached_htonll(delta);
483 cmd->incr.message.body.initial= memcached_htonll(initial);
484 cmd->incr.message.body.expiration= htonl(exptime);
485
486 off_t key_offset= sizeof (protocol_binary_request_no_extras) + 20;
487 memcpy(cmd->bytes + key_offset, key, keylen);
488 }
489
490 /**
491 * Validate the response header from the server
492 * @param rsp the response to check
493 * @param cc the expected command
494 * @param status the expected status
495 */
496 static enum test_return do_validate_response_header(response *rsp,
497 uint8_t cc, uint16_t status)
498 {
499 verify(rsp->plain.message.header.response.magic == PROTOCOL_BINARY_RES);
500 verify(rsp->plain.message.header.response.opcode == cc);
501 verify(rsp->plain.message.header.response.datatype == PROTOCOL_BINARY_RAW_BYTES);
502 verify(rsp->plain.message.header.response.status == status);
503 verify(rsp->plain.message.header.response.opaque == 0xdeadbeef);
504
505 if (status == PROTOCOL_BINARY_RESPONSE_SUCCESS)
506 {
507 switch (cc) {
508 case PROTOCOL_BINARY_CMD_ADDQ:
509 case PROTOCOL_BINARY_CMD_APPENDQ:
510 case PROTOCOL_BINARY_CMD_DECREMENTQ:
511 case PROTOCOL_BINARY_CMD_DELETEQ:
512 case PROTOCOL_BINARY_CMD_FLUSHQ:
513 case PROTOCOL_BINARY_CMD_INCREMENTQ:
514 case PROTOCOL_BINARY_CMD_PREPENDQ:
515 case PROTOCOL_BINARY_CMD_QUITQ:
516 case PROTOCOL_BINARY_CMD_REPLACEQ:
517 case PROTOCOL_BINARY_CMD_SETQ:
518 verify("Quiet command shouldn't return on success" == NULL);
519 default:
520 break;
521 }
522
523 switch (cc) {
524 case PROTOCOL_BINARY_CMD_ADD:
525 case PROTOCOL_BINARY_CMD_REPLACE:
526 case PROTOCOL_BINARY_CMD_SET:
527 case PROTOCOL_BINARY_CMD_APPEND:
528 case PROTOCOL_BINARY_CMD_PREPEND:
529 verify(rsp->plain.message.header.response.keylen == 0);
530 verify(rsp->plain.message.header.response.extlen == 0);
531 verify(rsp->plain.message.header.response.bodylen == 0);
532 verify(rsp->plain.message.header.response.cas != 0);
533 break;
534 case PROTOCOL_BINARY_CMD_FLUSH:
535 case PROTOCOL_BINARY_CMD_NOOP:
536 case PROTOCOL_BINARY_CMD_QUIT:
537 case PROTOCOL_BINARY_CMD_DELETE:
538 verify(rsp->plain.message.header.response.keylen == 0);
539 verify(rsp->plain.message.header.response.extlen == 0);
540 verify(rsp->plain.message.header.response.bodylen == 0);
541 verify(rsp->plain.message.header.response.cas == 0);
542 break;
543
544 case PROTOCOL_BINARY_CMD_DECREMENT:
545 case PROTOCOL_BINARY_CMD_INCREMENT:
546 verify(rsp->plain.message.header.response.keylen == 0);
547 verify(rsp->plain.message.header.response.extlen == 0);
548 verify(rsp->plain.message.header.response.bodylen == 8);
549 verify(rsp->plain.message.header.response.cas != 0);
550 break;
551
552 case PROTOCOL_BINARY_CMD_STAT:
553 verify(rsp->plain.message.header.response.extlen == 0);
554 /* key and value exists in all packets except in the terminating */
555 verify(rsp->plain.message.header.response.cas == 0);
556 break;
557
558 case PROTOCOL_BINARY_CMD_VERSION:
559 verify(rsp->plain.message.header.response.keylen == 0);
560 verify(rsp->plain.message.header.response.extlen == 0);
561 verify(rsp->plain.message.header.response.bodylen != 0);
562 verify(rsp->plain.message.header.response.cas == 0);
563 break;
564
565 case PROTOCOL_BINARY_CMD_GET:
566 case PROTOCOL_BINARY_CMD_GETQ:
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 case PROTOCOL_BINARY_CMD_GETK:
573 case PROTOCOL_BINARY_CMD_GETKQ:
574 verify(rsp->plain.message.header.response.keylen != 0);
575 verify(rsp->plain.message.header.response.extlen == 4);
576 verify(rsp->plain.message.header.response.cas != 0);
577 break;
578
579 default:
580 /* Undefined command code */
581 break;
582 }
583 }
584 else
585 {
586 verify(rsp->plain.message.header.response.cas == 0);
587 verify(rsp->plain.message.header.response.extlen == 0);
588 if (cc != PROTOCOL_BINARY_CMD_GETK)
589 {
590 verify(rsp->plain.message.header.response.keylen == 0);
591 }
592 }
593
594 return TEST_PASS;
595 }
596
597 /* We call verify(validate_response_header), but that macro
598 * expects a boolean expression, and the function returns
599 * an enum.... Let's just create a macro to avoid cluttering
600 * the code with all of the == TEST_PASS ;-)
601 */
602 #define validate_response_header(a,b,c) \
603 do_validate_response_header(a,b,c) == TEST_PASS
604
605
606 static enum test_return send_binary_noop(void)
607 {
608 command cmd;
609 raw_command(&cmd, PROTOCOL_BINARY_CMD_NOOP, NULL, 0, NULL, 0);
610 execute(send_packet(&cmd));
611 return TEST_PASS;
612 }
613
614 static enum test_return receive_binary_noop(void)
615 {
616 response rsp;
617 execute(recv_packet(&rsp));
618 verify(validate_response_header(&rsp, PROTOCOL_BINARY_CMD_NOOP,
619 PROTOCOL_BINARY_RESPONSE_SUCCESS));
620 return TEST_PASS;
621 }
622
623 static enum test_return test_binary_noop(void)
624 {
625 execute(send_binary_noop());
626 execute(receive_binary_noop());
627 return TEST_PASS;
628 }
629
630 static enum test_return test_binary_quit_impl(uint8_t cc)
631 {
632 command cmd;
633 response rsp;
634 raw_command(&cmd, cc, NULL, 0, NULL, 0);
635
636 execute(send_packet(&cmd));
637 if (cc == PROTOCOL_BINARY_CMD_QUIT)
638 {
639 execute(recv_packet(&rsp));
640 verify(validate_response_header(&rsp, PROTOCOL_BINARY_CMD_QUIT,
641 PROTOCOL_BINARY_RESPONSE_SUCCESS));
642 }
643
644 /* Socket should be closed now, read should return EXIT_SUCCESS */
645 verify(timeout_io_op(sock, POLLIN, rsp.bytes, sizeof(rsp.bytes)) == 0);
646
647 return TEST_PASS_RECONNECT;
648 }
649
650 static enum test_return test_binary_quit(void)
651 {
652 return test_binary_quit_impl(PROTOCOL_BINARY_CMD_QUIT);
653 }
654
655 static enum test_return test_binary_quitq(void)
656 {
657 return test_binary_quit_impl(PROTOCOL_BINARY_CMD_QUITQ);
658 }
659
660 static enum test_return test_binary_set_impl(const char* key, uint8_t cc)
661 {
662 command cmd;
663 response rsp;
664
665 uint64_t value= 0xdeadbeefdeadcafeULL;
666 storage_command(&cmd, cc, key, strlen(key), &value, sizeof (value), 0, 0);
667
668 /* set should always work */
669 for (int ii= 0; ii < 10; ii++)
670 {
671 if (ii == 0)
672 execute(send_packet(&cmd));
673 else
674 execute(resend_packet(&cmd));
675
676 if (cc == PROTOCOL_BINARY_CMD_SET)
677 {
678 execute(recv_packet(&rsp));
679 verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_SUCCESS));
680 }
681 else
682 execute(test_binary_noop());
683 }
684
685 /*
686 * We need to get the current CAS id, and at this time we haven't
687 * verified that we have a working get
688 */
689 if (cc == PROTOCOL_BINARY_CMD_SETQ)
690 {
691 cmd.set.message.header.request.opcode= PROTOCOL_BINARY_CMD_SET;
692 execute(resend_packet(&cmd));
693 execute(recv_packet(&rsp));
694 verify(validate_response_header(&rsp, PROTOCOL_BINARY_CMD_SET,
695 PROTOCOL_BINARY_RESPONSE_SUCCESS));
696 cmd.set.message.header.request.opcode= PROTOCOL_BINARY_CMD_SETQ;
697 }
698
699 /* try to set with the correct CAS value */
700 cmd.plain.message.header.request.cas= memcached_htonll(rsp.plain.message.header.response.cas);
701 execute(resend_packet(&cmd));
702 if (cc == PROTOCOL_BINARY_CMD_SET)
703 {
704 execute(recv_packet(&rsp));
705 verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_SUCCESS));
706 }
707 else
708 execute(test_binary_noop());
709
710 /* try to set with an incorrect CAS value */
711 cmd.plain.message.header.request.cas= memcached_htonll(rsp.plain.message.header.response.cas - 1);
712 execute(resend_packet(&cmd));
713 execute(send_binary_noop());
714 execute(recv_packet(&rsp));
715 verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS));
716 execute(receive_binary_noop());
717
718 return TEST_PASS;
719 }
720
721 static enum test_return test_binary_set(void)
722 {
723 return test_binary_set_impl("test_binary_set", PROTOCOL_BINARY_CMD_SET);
724 }
725
726 static enum test_return test_binary_setq(void)
727 {
728 return test_binary_set_impl("test_binary_setq", PROTOCOL_BINARY_CMD_SETQ);
729 }
730
731 static enum test_return test_binary_add_impl(const char* key, uint8_t cc)
732 {
733 command cmd;
734 response rsp;
735 uint64_t value= 0xdeadbeefdeadcafeULL;
736 storage_command(&cmd, cc, key, strlen(key), &value, sizeof (value), 0, 0);
737
738 /* first add should work, rest of them should fail (even with cas
739 as wildcard */
740 for (int ii=0; ii < 10; ii++)
741 {
742 if (ii == 0)
743 execute(send_packet(&cmd));
744 else
745 execute(resend_packet(&cmd));
746
747 if (cc == PROTOCOL_BINARY_CMD_ADD || ii > 0)
748 {
749 uint16_t expected_result;
750 if (ii == 0)
751 expected_result= PROTOCOL_BINARY_RESPONSE_SUCCESS;
752 else
753 expected_result= PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS;
754
755 execute(send_binary_noop());
756 execute(recv_packet(&rsp));
757 execute(receive_binary_noop());
758 verify(validate_response_header(&rsp, cc, expected_result));
759 }
760 else
761 execute(test_binary_noop());
762 }
763
764 return TEST_PASS;
765 }
766
767 static enum test_return test_binary_add(void)
768 {
769 return test_binary_add_impl("test_binary_add", PROTOCOL_BINARY_CMD_ADD);
770 }
771
772 static enum test_return test_binary_addq(void)
773 {
774 return test_binary_add_impl("test_binary_addq", PROTOCOL_BINARY_CMD_ADDQ);
775 }
776
777 static enum test_return binary_set_item(const char *key, const char *value)
778 {
779 command cmd;
780 response rsp;
781 storage_command(&cmd, PROTOCOL_BINARY_CMD_SET, key, strlen(key),
782 value, strlen(value), 0, 0);
783 execute(send_packet(&cmd));
784 execute(recv_packet(&rsp));
785 verify(validate_response_header(&rsp, PROTOCOL_BINARY_CMD_SET,
786 PROTOCOL_BINARY_RESPONSE_SUCCESS));
787 return TEST_PASS;
788 }
789
790 static enum test_return test_binary_replace_impl(const char* key, uint8_t cc)
791 {
792 command cmd;
793 response rsp;
794 uint64_t value= 0xdeadbeefdeadcafeULL;
795 storage_command(&cmd, cc, key, strlen(key), &value, sizeof (value), 0, 0);
796
797 /* first replace should fail, successive should succeed (when the
798 item is added! */
799 for (int ii= 0; ii < 10; ii++)
800 {
801 if (ii == 0)
802 execute(send_packet(&cmd));
803 else
804 execute(resend_packet(&cmd));
805
806 if (cc == PROTOCOL_BINARY_CMD_REPLACE || ii == 0)
807 {
808 uint16_t expected_result;
809 if (ii == 0)
810 expected_result=PROTOCOL_BINARY_RESPONSE_KEY_ENOENT;
811 else
812 expected_result=PROTOCOL_BINARY_RESPONSE_SUCCESS;
813
814 execute(send_binary_noop());
815 execute(recv_packet(&rsp));
816 execute(receive_binary_noop());
817 verify(validate_response_header(&rsp, cc, expected_result));
818
819 if (ii == 0)
820 execute(binary_set_item(key, key));
821 }
822 else
823 execute(test_binary_noop());
824 }
825
826 /* verify that replace with CAS value works! */
827 cmd.plain.message.header.request.cas= memcached_htonll(rsp.plain.message.header.response.cas);
828 execute(resend_packet(&cmd));
829
830 if (cc == PROTOCOL_BINARY_CMD_REPLACE)
831 {
832 execute(recv_packet(&rsp));
833 verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_SUCCESS));
834 }
835 else
836 execute(test_binary_noop());
837
838 /* try to set with an incorrect CAS value */
839 cmd.plain.message.header.request.cas= memcached_htonll(rsp.plain.message.header.response.cas - 1);
840 execute(resend_packet(&cmd));
841 execute(send_binary_noop());
842 execute(recv_packet(&rsp));
843 execute(receive_binary_noop());
844 verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS));
845
846 return TEST_PASS;
847 }
848
849 static enum test_return test_binary_replace(void)
850 {
851 return test_binary_replace_impl("test_binary_replace", PROTOCOL_BINARY_CMD_REPLACE);
852 }
853
854 static enum test_return test_binary_replaceq(void)
855 {
856 return test_binary_replace_impl("test_binary_replaceq", PROTOCOL_BINARY_CMD_REPLACEQ);
857 }
858
859 static enum test_return test_binary_delete_impl(const char *key, uint8_t cc)
860 {
861 command cmd;
862 response rsp;
863 raw_command(&cmd, cc, key, strlen(key), NULL, 0);
864
865 /* The delete shouldn't work the first time, because the item isn't there */
866 execute(send_packet(&cmd));
867 execute(send_binary_noop());
868 execute(recv_packet(&rsp));
869 verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_KEY_ENOENT));
870 execute(receive_binary_noop());
871 execute(binary_set_item(key, key));
872
873 /* The item should be present now, resend*/
874 execute(resend_packet(&cmd));
875 if (cc == PROTOCOL_BINARY_CMD_DELETE)
876 {
877 execute(recv_packet(&rsp));
878 verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_SUCCESS));
879 }
880
881 execute(test_binary_noop());
882
883 return TEST_PASS;
884 }
885
886 static enum test_return test_binary_delete(void)
887 {
888 return test_binary_delete_impl("test_binary_delete", PROTOCOL_BINARY_CMD_DELETE);
889 }
890
891 static enum test_return test_binary_deleteq(void)
892 {
893 return test_binary_delete_impl("test_binary_deleteq", PROTOCOL_BINARY_CMD_DELETEQ);
894 }
895
896 static enum test_return test_binary_get_impl(const char *key, uint8_t cc)
897 {
898 command cmd;
899 response rsp;
900
901 raw_command(&cmd, cc, key, strlen(key), NULL, 0);
902 execute(send_packet(&cmd));
903 execute(send_binary_noop());
904
905 if (cc == PROTOCOL_BINARY_CMD_GET || cc == PROTOCOL_BINARY_CMD_GETK)
906 {
907 execute(recv_packet(&rsp));
908 verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_KEY_ENOENT));
909 }
910
911 execute(receive_binary_noop());
912
913 execute(binary_set_item(key, key));
914 execute(resend_packet(&cmd));
915 execute(send_binary_noop());
916
917 execute(recv_packet(&rsp));
918 verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_SUCCESS));
919 execute(receive_binary_noop());
920
921 return TEST_PASS;
922 }
923
924 static enum test_return test_binary_get(void)
925 {
926 return test_binary_get_impl("test_binary_get", PROTOCOL_BINARY_CMD_GET);
927 }
928
929 static enum test_return test_binary_getk(void)
930 {
931 return test_binary_get_impl("test_binary_getk", PROTOCOL_BINARY_CMD_GETK);
932 }
933
934 static enum test_return test_binary_getq(void)
935 {
936 return test_binary_get_impl("test_binary_getq", PROTOCOL_BINARY_CMD_GETQ);
937 }
938
939 static enum test_return test_binary_getkq(void)
940 {
941 return test_binary_get_impl("test_binary_getkq", PROTOCOL_BINARY_CMD_GETKQ);
942 }
943
944 static enum test_return test_binary_incr_impl(const char* key, uint8_t cc)
945 {
946 command cmd;
947 response rsp;
948 arithmetic_command(&cmd, cc, key, strlen(key), 1, 0, 0);
949
950 uint64_t ii;
951 for (ii= 0; ii < 10; ++ii)
952 {
953 if (ii == 0)
954 execute(send_packet(&cmd));
955 else
956 execute(resend_packet(&cmd));
957
958 if (cc == PROTOCOL_BINARY_CMD_INCREMENT)
959 {
960 execute(recv_packet(&rsp));
961 verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_SUCCESS));
962 verify(memcached_ntohll(rsp.incr.message.body.value) == ii);
963 }
964 else
965 execute(test_binary_noop());
966 }
967
968 /* @todo add incorrect CAS */
969 return TEST_PASS;
970 }
971
972 static enum test_return test_binary_incr(void)
973 {
974 return test_binary_incr_impl("test_binary_incr", PROTOCOL_BINARY_CMD_INCREMENT);
975 }
976
977 static enum test_return test_binary_incrq(void)
978 {
979 return test_binary_incr_impl("test_binary_incrq", PROTOCOL_BINARY_CMD_INCREMENTQ);
980 }
981
982 static enum test_return test_binary_decr_impl(const char* key, uint8_t cc)
983 {
984 command cmd;
985 response rsp;
986 arithmetic_command(&cmd, cc, key, strlen(key), 1, 9, 0);
987
988 int ii;
989 for (ii= 9; ii > -1; --ii)
990 {
991 if (ii == 9)
992 execute(send_packet(&cmd));
993 else
994 execute(resend_packet(&cmd));
995
996 if (cc == PROTOCOL_BINARY_CMD_DECREMENT)
997 {
998 execute(recv_packet(&rsp));
999 verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_SUCCESS));
1000 verify(memcached_ntohll(rsp.decr.message.body.value) == (uint64_t)ii);
1001 }
1002 else
1003 execute(test_binary_noop());
1004 }
1005
1006 /* decr 0 should not wrap */
1007 execute(resend_packet(&cmd));
1008 if (cc == PROTOCOL_BINARY_CMD_DECREMENT)
1009 {
1010 execute(recv_packet(&rsp));
1011 verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_SUCCESS));
1012 verify(memcached_ntohll(rsp.decr.message.body.value) == 0);
1013 }
1014 else
1015 {
1016 /* @todo get the value and verify! */
1017
1018 }
1019
1020 /* @todo add incorrect cas */
1021 execute(test_binary_noop());
1022 return TEST_PASS;
1023 }
1024
1025 static enum test_return test_binary_decr(void)
1026 {
1027 return test_binary_decr_impl("test_binary_decr",
1028 PROTOCOL_BINARY_CMD_DECREMENT);
1029 }
1030
1031 static enum test_return test_binary_decrq(void)
1032 {
1033 return test_binary_decr_impl("test_binary_decrq",
1034 PROTOCOL_BINARY_CMD_DECREMENTQ);
1035 }
1036
1037 static enum test_return test_binary_version(void)
1038 {
1039 command cmd;
1040 response rsp;
1041 raw_command(&cmd, PROTOCOL_BINARY_CMD_VERSION, NULL, 0, NULL, 0);
1042
1043 execute(send_packet(&cmd));
1044 execute(recv_packet(&rsp));
1045 verify(validate_response_header(&rsp, PROTOCOL_BINARY_CMD_VERSION,
1046 PROTOCOL_BINARY_RESPONSE_SUCCESS));
1047
1048 return TEST_PASS;
1049 }
1050
1051 static enum test_return test_binary_flush_impl(const char *key, uint8_t cc)
1052 {
1053 command cmd;
1054 response rsp;
1055
1056 for (int ii= 0; ii < 2; ++ii)
1057 {
1058 execute(binary_set_item(key, key));
1059 flush_command(&cmd, cc, 0, ii == 0);
1060 execute(send_packet(&cmd));
1061
1062 if (cc == PROTOCOL_BINARY_CMD_FLUSH)
1063 {
1064 execute(recv_packet(&rsp));
1065 verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_SUCCESS));
1066 }
1067 else
1068 execute(test_binary_noop());
1069
1070 raw_command(&cmd, PROTOCOL_BINARY_CMD_GET, key, strlen(key), NULL, 0);
1071 execute(send_packet(&cmd));
1072 execute(recv_packet(&rsp));
1073 verify(validate_response_header(&rsp, PROTOCOL_BINARY_CMD_GET,
1074 PROTOCOL_BINARY_RESPONSE_KEY_ENOENT));
1075 }
1076
1077 return TEST_PASS;
1078 }
1079
1080 static enum test_return test_binary_flush(void)
1081 {
1082 return test_binary_flush_impl("test_binary_flush", PROTOCOL_BINARY_CMD_FLUSH);
1083 }
1084
1085 static enum test_return test_binary_flushq(void)
1086 {
1087 return test_binary_flush_impl("test_binary_flushq", PROTOCOL_BINARY_CMD_FLUSHQ);
1088 }
1089
1090 static enum test_return test_binary_concat_impl(const char *key, uint8_t cc)
1091 {
1092 command cmd;
1093 response rsp;
1094 const char *value;
1095
1096 if (cc == PROTOCOL_BINARY_CMD_APPEND || cc == PROTOCOL_BINARY_CMD_APPENDQ)
1097 value="hello";
1098 else
1099 value=" world";
1100
1101 execute(binary_set_item(key, value));
1102
1103 if (cc == PROTOCOL_BINARY_CMD_APPEND || cc == PROTOCOL_BINARY_CMD_APPENDQ)
1104 value=" world";
1105 else
1106 value="hello";
1107
1108 raw_command(&cmd, cc, key, strlen(key), value, strlen(value));
1109 execute(send_packet(&cmd));
1110 if (cc == PROTOCOL_BINARY_CMD_APPEND || cc == PROTOCOL_BINARY_CMD_PREPEND)
1111 {
1112 execute(recv_packet(&rsp));
1113 verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_SUCCESS));
1114 }
1115 else
1116 execute(test_binary_noop());
1117
1118 raw_command(&cmd, PROTOCOL_BINARY_CMD_GET, key, strlen(key), NULL, 0);
1119 execute(send_packet(&cmd));
1120 execute(recv_packet(&rsp));
1121 verify(validate_response_header(&rsp, PROTOCOL_BINARY_CMD_GET,
1122 PROTOCOL_BINARY_RESPONSE_SUCCESS));
1123 verify(rsp.plain.message.header.response.bodylen - 4 == 11);
1124 verify(memcmp(rsp.bytes + 28, "hello world", 11) == 0);
1125
1126 return TEST_PASS;
1127 }
1128
1129 static enum test_return test_binary_append(void)
1130 {
1131 return test_binary_concat_impl("test_binary_append", PROTOCOL_BINARY_CMD_APPEND);
1132 }
1133
1134 static enum test_return test_binary_prepend(void)
1135 {
1136 return test_binary_concat_impl("test_binary_prepend", PROTOCOL_BINARY_CMD_PREPEND);
1137 }
1138
1139 static enum test_return test_binary_appendq(void)
1140 {
1141 return test_binary_concat_impl("test_binary_appendq", PROTOCOL_BINARY_CMD_APPENDQ);
1142 }
1143
1144 static enum test_return test_binary_prependq(void)
1145 {
1146 return test_binary_concat_impl("test_binary_prependq", PROTOCOL_BINARY_CMD_PREPENDQ);
1147 }
1148
1149 static enum test_return test_binary_stat(void)
1150 {
1151 command cmd;
1152 response rsp;
1153
1154 raw_command(&cmd, PROTOCOL_BINARY_CMD_STAT, NULL, 0, NULL, 0);
1155 execute(send_packet(&cmd));
1156
1157 do
1158 {
1159 execute(recv_packet(&rsp));
1160 verify(validate_response_header(&rsp, PROTOCOL_BINARY_CMD_STAT,
1161 PROTOCOL_BINARY_RESPONSE_SUCCESS));
1162 } while (rsp.plain.message.header.response.keylen != 0);
1163
1164 return TEST_PASS;
1165 }
1166
1167 static enum test_return send_string(const char *cmd)
1168 {
1169 execute(retry_write(cmd, strlen(cmd)));
1170 return TEST_PASS;
1171 }
1172
1173 static enum test_return receive_line(char *buffer, size_t size)
1174 {
1175 size_t offset= 0;
1176 while (offset < size)
1177 {
1178 execute(retry_read(buffer + offset, 1));
1179 if (buffer[offset] == '\n')
1180 {
1181 if (offset + 1 < size)
1182 {
1183 buffer[offset + 1]= '\0';
1184 return TEST_PASS;
1185 }
1186 else
1187 return TEST_FAIL;
1188 }
1189 ++offset;
1190 }
1191
1192 return TEST_FAIL;
1193 }
1194
1195 static enum test_return receive_response(const char *msg) {
1196 char buffer[80];
1197 execute(receive_line(buffer, sizeof(buffer)));
1198 if (strcmp(msg, buffer) != 0) {
1199 fprintf(stderr, "[%s]\n", buffer);
1200 }
1201 verify(strcmp(msg, buffer) == 0);
1202 return TEST_PASS;
1203 }
1204
1205 static enum test_return receive_error_response(void)
1206 {
1207 char buffer[80];
1208 execute(receive_line(buffer, sizeof(buffer)));
1209 verify(strncmp(buffer, "ERROR", 5) == 0 ||
1210 strncmp(buffer, "CLIENT_ERROR", 12) == 0 ||
1211 strncmp(buffer, "SERVER_ERROR", 12) == 0);
1212 return TEST_PASS;
1213 }
1214
1215 static enum test_return test_ascii_quit(void)
1216 {
1217 /* Verify that quit handles unknown options */
1218 execute(send_string("quit foo bar\r\n"));
1219 execute(receive_error_response());
1220
1221 /* quit doesn't support noreply */
1222 execute(send_string("quit noreply\r\n"));
1223 execute(receive_error_response());
1224
1225 /* Verify that quit works */
1226 execute(send_string("quit\r\n"));
1227
1228 /* Socket should be closed now, read should return EXIT_SUCCESS */
1229 char buffer[80];
1230 verify(timeout_io_op(sock, POLLIN, buffer, sizeof(buffer)) == 0);
1231 return TEST_PASS_RECONNECT;
1232
1233 }
1234
1235 static enum test_return test_ascii_version(void)
1236 {
1237 /* Verify that version command handles unknown options */
1238 execute(send_string("version foo bar\r\n"));
1239 execute(receive_error_response());
1240
1241 /* version doesn't support noreply */
1242 execute(send_string("version noreply\r\n"));
1243 execute(receive_error_response());
1244
1245 /* Verify that verify works */
1246 execute(send_string("version\r\n"));
1247 char buffer[256];
1248 execute(receive_line(buffer, sizeof(buffer)));
1249 verify(strncmp(buffer, "VERSION ", 8) == 0);
1250
1251 return TEST_PASS;
1252 }
1253
1254 static enum test_return test_ascii_verbosity(void)
1255 {
1256 /* This command does not adhere to the spec! */
1257 execute(send_string("verbosity foo bar my\r\n"));
1258 execute(receive_error_response());
1259
1260 execute(send_string("verbosity noreply\r\n"));
1261 execute(receive_error_response());
1262
1263 execute(send_string("verbosity 0 noreply\r\n"));
1264 execute(test_ascii_version());
1265
1266 execute(send_string("verbosity\r\n"));
1267 execute(receive_error_response());
1268
1269 execute(send_string("verbosity 1\r\n"));
1270 execute(receive_response("OK\r\n"));
1271
1272 execute(send_string("verbosity 0\r\n"));
1273 execute(receive_response("OK\r\n"));
1274
1275 return TEST_PASS;
1276 }
1277
1278
1279
1280 static enum test_return test_ascii_set_impl(const char* key, bool noreply)
1281 {
1282 /* @todo add tests for bogus format! */
1283 char buffer[1024];
1284 snprintf(buffer, sizeof(buffer), "set %s 0 0 5%s\r\nvalue\r\n", key, noreply ? " noreply" : "");
1285 execute(send_string(buffer));
1286
1287 if (!noreply)
1288 execute(receive_response("STORED\r\n"));
1289
1290 return test_ascii_version();
1291 }
1292
1293 static enum test_return test_ascii_set(void)
1294 {
1295 return test_ascii_set_impl("test_ascii_set", false);
1296 }
1297
1298 static enum test_return test_ascii_set_noreply(void)
1299 {
1300 return test_ascii_set_impl("test_ascii_set_noreply", true);
1301 }
1302
1303 static enum test_return test_ascii_add_impl(const char* key, bool noreply)
1304 {
1305 /* @todo add tests for bogus format! */
1306 char buffer[1024];
1307 snprintf(buffer, sizeof(buffer), "add %s 0 0 5%s\r\nvalue\r\n", key, noreply ? " noreply" : "");
1308 execute(send_string(buffer));
1309
1310 if (!noreply)
1311 execute(receive_response("STORED\r\n"));
1312
1313 execute(send_string(buffer));
1314
1315 if (!noreply)
1316 execute(receive_response("NOT_STORED\r\n"));
1317
1318 return test_ascii_version();
1319 }
1320
1321 static enum test_return test_ascii_add(void)
1322 {
1323 return test_ascii_add_impl("test_ascii_add", false);
1324 }
1325
1326 static enum test_return test_ascii_add_noreply(void)
1327 {
1328 return test_ascii_add_impl("test_ascii_add_noreply", true);
1329 }
1330
1331 static enum test_return ascii_get_unknown_value(char **key, char **value, ssize_t *ndata)
1332 {
1333 char buffer[1024];
1334
1335 execute(receive_line(buffer, sizeof(buffer)));
1336 verify(strncmp(buffer, "VALUE ", 6) == 0);
1337 char *end= strchr(buffer + 6, ' ');
1338 verify(end != NULL);
1339 if (end)
1340 {
1341 *end= '\0';
1342 }
1343 *key= strdup(buffer + 6);
1344 verify(*key != NULL);
1345 char *ptr= end + 1;
1346
1347 unsigned long val= strtoul(ptr, &end, 10); /* flags */
1348 verify(ptr != end);
1349 verify(val == 0);
1350 verify(end != NULL);
1351 *ndata = (ssize_t)strtoul(end, &end, 10); /* size */
1352 verify(ptr != end);
1353 verify(end != NULL);
1354 while (end and *end != '\n' and isspace(*end))
1355 ++end;
1356 verify(end and *end == '\n');
1357
1358 *value= static_cast<char*>(malloc((size_t)*ndata));
1359 verify(*value != NULL);
1360
1361 execute(retry_read(*value, (size_t)*ndata));
1362
1363 execute(retry_read(buffer, 2));
1364 verify(memcmp(buffer, "\r\n", 2) == 0);
1365
1366 return TEST_PASS;
1367 }
1368
1369 static enum test_return ascii_get_value(const char *key, const char *value)
1370 {
1371
1372 char buffer[1024];
1373 size_t datasize= strlen(value);
1374
1375 verify(datasize < sizeof(buffer));
1376 execute(receive_line(buffer, sizeof(buffer)));
1377 verify(strncmp(buffer, "VALUE ", 6) == 0);
1378 verify(strncmp(buffer + 6, key, strlen(key)) == 0);
1379 char *ptr= buffer + 6 + strlen(key) + 1;
1380 char *end;
1381
1382 unsigned long val= strtoul(ptr, &end, 10); /* flags */
1383 verify(ptr != end);
1384 verify(val == 0);
1385 verify(end != NULL);
1386 val= strtoul(end, &end, 10); /* size */
1387 verify(ptr != end);
1388 verify(val == datasize);
1389 verify(end != NULL);
1390 while (end and *end != '\n' and isspace(*end))
1391 {
1392 ++end;
1393 }
1394 verify(end and *end == '\n');
1395
1396 execute(retry_read(buffer, datasize));
1397 verify(memcmp(buffer, value, datasize) == 0);
1398
1399 execute(retry_read(buffer, 2));
1400 verify(memcmp(buffer, "\r\n", 2) == 0);
1401
1402 return TEST_PASS;
1403 }
1404
1405 static enum test_return ascii_get_item(const char *key, const char *value,
1406 bool exist)
1407 {
1408 char buffer[1024];
1409 size_t datasize= 0;
1410 if (value != NULL)
1411 datasize= strlen(value);
1412
1413 verify(datasize < sizeof(buffer));
1414 snprintf(buffer, sizeof(buffer), "get %s\r\n", key);
1415 execute(send_string(buffer));
1416
1417 if (exist)
1418 execute(ascii_get_value(key, value));
1419
1420 execute(retry_read(buffer, 5));
1421 verify(memcmp(buffer, "END\r\n", 5) == 0);
1422
1423 return TEST_PASS;
1424 }
1425
1426 static enum test_return ascii_gets_value(const char *key, const char *value,
1427 unsigned long *cas)
1428 {
1429
1430 char buffer[1024];
1431 size_t datasize= strlen(value);
1432
1433 verify(datasize < sizeof(buffer));
1434 execute(receive_line(buffer, sizeof(buffer)));
1435 verify(strncmp(buffer, "VALUE ", 6) == 0);
1436 verify(strncmp(buffer + 6, key, strlen(key)) == 0);
1437 char *ptr= buffer + 6 + strlen(key) + 1;
1438 char *end;
1439
1440 unsigned long val= strtoul(ptr, &end, 10); /* flags */
1441 verify(ptr != end);
1442 verify(val == 0);
1443 verify(end != NULL);
1444 val= strtoul(end, &end, 10); /* size */
1445 verify(ptr != end);
1446 verify(val == datasize);
1447 verify(end != NULL);
1448 *cas= strtoul(end, &end, 10); /* cas */
1449 verify(ptr != end);
1450 verify(val == datasize);
1451 verify(end != NULL);
1452
1453 while (end and *end != '\n' and isspace(*end))
1454 {
1455 ++end;
1456 }
1457 verify(end and *end == '\n');
1458
1459 execute(retry_read(buffer, datasize));
1460 verify(memcmp(buffer, value, datasize) == 0);
1461
1462 execute(retry_read(buffer, 2));
1463 verify(memcmp(buffer, "\r\n", 2) == 0);
1464
1465 return TEST_PASS;
1466 }
1467
1468 static enum test_return ascii_gets_item(const char *key, const char *value,
1469 bool exist, unsigned long *cas)
1470 {
1471 char buffer[1024];
1472 size_t datasize= 0;
1473 if (value != NULL)
1474 datasize= strlen(value);
1475
1476 verify(datasize < sizeof(buffer));
1477 snprintf(buffer, sizeof(buffer), "gets %s\r\n", key);
1478 execute(send_string(buffer));
1479
1480 if (exist)
1481 execute(ascii_gets_value(key, value, cas));
1482
1483 execute(retry_read(buffer, 5));
1484 verify(memcmp(buffer, "END\r\n", 5) == 0);
1485
1486 return TEST_PASS;
1487 }
1488
1489 static enum test_return ascii_set_item(const char *key, const char *value)
1490 {
1491 char buffer[300];
1492 size_t len= strlen(value);
1493 snprintf(buffer, sizeof(buffer), "set %s 0 0 %u\r\n", key, (unsigned int)len);
1494 execute(send_string(buffer));
1495 execute(retry_write(value, len));
1496 execute(send_string("\r\n"));
1497 execute(receive_response("STORED\r\n"));
1498 return TEST_PASS;
1499 }
1500
1501 static enum test_return test_ascii_replace_impl(const char* key, bool noreply)
1502 {
1503 char buffer[1024];
1504 snprintf(buffer, sizeof(buffer), "replace %s 0 0 5%s\r\nvalue\r\n", key, noreply ? " noreply" : "");
1505 execute(send_string(buffer));
1506
1507 if (noreply)
1508 execute(test_ascii_version());
1509 else
1510 execute(receive_response("NOT_STORED\r\n"));
1511
1512 execute(ascii_set_item(key, "value"));
1513 execute(ascii_get_item(key, "value", true));
1514
1515
1516 execute(send_string(buffer));
1517
1518 if (noreply)
1519 execute(test_ascii_version());
1520 else
1521 execute(receive_response("STORED\r\n"));
1522
1523 return test_ascii_version();
1524 }
1525
1526 static enum test_return test_ascii_replace(void)
1527 {
1528 return test_ascii_replace_impl("test_ascii_replace", false);
1529 }
1530
1531 static enum test_return test_ascii_replace_noreply(void)
1532 {
1533 return test_ascii_replace_impl("test_ascii_replace_noreply", true);
1534 }
1535
1536 static enum test_return test_ascii_cas_impl(const char* key, bool noreply)
1537 {
1538 char buffer[1024];
1539 unsigned long cas;
1540
1541 execute(ascii_set_item(key, "value"));
1542 execute(ascii_gets_item(key, "value", true, &cas));
1543
1544 snprintf(buffer, sizeof(buffer), "cas %s 0 0 6 %lu%s\r\nvalue2\r\n", key, cas, noreply ? " noreply" : "");
1545 execute(send_string(buffer));
1546
1547 if (noreply)
1548 execute(test_ascii_version());
1549 else
1550 execute(receive_response("STORED\r\n"));
1551
1552 /* reexecute the same command should fail due to illegal cas */
1553 execute(send_string(buffer));
1554
1555 if (noreply)
1556 execute(test_ascii_version());
1557 else
1558 execute(receive_response("EXISTS\r\n"));
1559
1560 return test_ascii_version();
1561 }
1562
1563 static enum test_return test_ascii_cas(void)
1564 {
1565 return test_ascii_cas_impl("test_ascii_cas", false);
1566 }
1567
1568 static enum test_return test_ascii_cas_noreply(void)
1569 {
1570 return test_ascii_cas_impl("test_ascii_cas_noreply", true);
1571 }
1572
1573 static enum test_return test_ascii_delete_impl(const char *key, bool noreply)
1574 {
1575 execute(ascii_set_item(key, "value"));
1576
1577 execute(send_string("delete\r\n"));
1578 execute(receive_error_response());
1579 /* BUG: the server accepts delete a b */
1580 execute(send_string("delete a b c d e\r\n"));
1581 execute(receive_error_response());
1582
1583 char buffer[1024];
1584 snprintf(buffer, sizeof(buffer), "delete %s%s\r\n", key, noreply ? " noreply" : "");
1585 execute(send_string(buffer));
1586
1587 if (noreply)
1588 execute(test_ascii_version());
1589 else
1590 execute(receive_response("DELETED\r\n"));
1591
1592 execute(ascii_get_item(key, "value", false));
1593 execute(send_string(buffer));
1594 if (noreply)
1595 execute(test_ascii_version());
1596 else
1597 execute(receive_response("NOT_FOUND\r\n"));
1598
1599 return TEST_PASS;
1600 }
1601
1602 static enum test_return test_ascii_delete(void)
1603 {
1604 return test_ascii_delete_impl("test_ascii_delete", false);
1605 }
1606
1607 static enum test_return test_ascii_delete_noreply(void)
1608 {
1609 return test_ascii_delete_impl("test_ascii_delete_noreply", true);
1610 }
1611
1612 static enum test_return test_ascii_get(void)
1613 {
1614 execute(ascii_set_item("test_ascii_get", "value"));
1615
1616 execute(send_string("get\r\n"));
1617 execute(receive_error_response());
1618 execute(ascii_get_item("test_ascii_get", "value", true));
1619 execute(ascii_get_item("test_ascii_get_notfound", "value", false));
1620
1621 return TEST_PASS;
1622 }
1623
1624 static enum test_return test_ascii_gets(void)
1625 {
1626 execute(ascii_set_item("test_ascii_gets", "value"));
1627
1628 execute(send_string("gets\r\n"));
1629 execute(receive_error_response());
1630 unsigned long cas;
1631 execute(ascii_gets_item("test_ascii_gets", "value", true, &cas));
1632 execute(ascii_gets_item("test_ascii_gets_notfound", "value", false, &cas));
1633
1634 return TEST_PASS;
1635 }
1636
1637 static enum test_return test_ascii_mget(void)
1638 {
1639 const uint32_t nkeys= 5;
1640 const char * const keys[]= {
1641 "test_ascii_mget1",
1642 "test_ascii_mget2",
1643 /* test_ascii_mget_3 does not exist :) */
1644 "test_ascii_mget4",
1645 "test_ascii_mget5",
1646 "test_ascii_mget6"
1647 };
1648
1649 for (uint32_t x= 0; x < nkeys; ++x)
1650 execute(ascii_set_item(keys[x], "value"));
1651
1652 /* Ask for a key that doesn't exist as well */
1653 execute(send_string("get test_ascii_mget1 test_ascii_mget2 test_ascii_mget3 "
1654 "test_ascii_mget4 test_ascii_mget5 "
1655 "test_ascii_mget6\r\n"));
1656
1657 char *returned[nkeys];
1658
1659 for (uint32_t x= 0; x < nkeys; ++x)
1660 {
1661 ssize_t nbytes = 0;
1662 char *v= NULL;
1663 execute(ascii_get_unknown_value(&returned[x], &v, &nbytes));
1664 verify(nbytes == 5);
1665 verify(memcmp(v, "value", 5) == 0);
1666 free(v);
1667 }
1668
1669 char buffer[5];
1670 execute(retry_read(buffer, 5));
1671 verify(memcmp(buffer, "END\r\n", 5) == 0);
1672
1673 /* verify that we got all the keys we expected */
1674 for (uint32_t x= 0; x < nkeys; ++x)
1675 {
1676 bool found= false;
1677 for (uint32_t y= 0; y < nkeys; ++y)
1678 {
1679 if (strcmp(keys[x], returned[y]) == 0)
1680 {
1681 found = true;
1682 break;
1683 }
1684 }
1685 verify(found);
1686 }
1687
1688 for (uint32_t x= 0; x < nkeys; ++x)
1689 free(returned[x]);
1690
1691 return TEST_PASS;
1692 }
1693
1694 static enum test_return test_ascii_incr_impl(const char* key, bool noreply)
1695 {
1696 char cmd[300];
1697 snprintf(cmd, sizeof(cmd), "incr %s 1%s\r\n", key, noreply ? " noreply" : "");
1698
1699 execute(ascii_set_item(key, "0"));
1700 for (int x= 1; x < 11; ++x)
1701 {
1702 execute(send_string(cmd));
1703
1704 if (noreply)
1705 execute(test_ascii_version());
1706 else
1707 {
1708 char buffer[80];
1709 execute(receive_line(buffer, sizeof(buffer)));
1710 int val= atoi(buffer);
1711 verify(val == x);
1712 }
1713 }
1714
1715 execute(ascii_get_item(key, "10", true));
1716
1717 return TEST_PASS;
1718 }
1719
1720 static enum test_return test_ascii_incr(void)
1721 {
1722 return test_ascii_incr_impl("test_ascii_incr", false);
1723 }
1724
1725 static enum test_return test_ascii_incr_noreply(void)
1726 {
1727 return test_ascii_incr_impl("test_ascii_incr_noreply", true);
1728 }
1729
1730 static enum test_return test_ascii_decr_impl(const char* key, bool noreply)
1731 {
1732 char cmd[300];
1733 snprintf(cmd, sizeof(cmd), "decr %s 1%s\r\n", key, noreply ? " noreply" : "");
1734
1735 execute(ascii_set_item(key, "9"));
1736 for (int x= 8; x > -1; --x)
1737 {
1738 execute(send_string(cmd));
1739
1740 if (noreply)
1741 execute(test_ascii_version());
1742 else
1743 {
1744 char buffer[80];
1745 execute(receive_line(buffer, sizeof(buffer)));
1746 int val= atoi(buffer);
1747 verify(val == x);
1748 }
1749 }
1750
1751 execute(ascii_get_item(key, "0", true));
1752
1753 /* verify that it doesn't wrap */
1754 execute(send_string(cmd));
1755 if (noreply)
1756 execute(test_ascii_version());
1757 else
1758 {
1759 char buffer[80];
1760 execute(receive_line(buffer, sizeof(buffer)));
1761 }
1762 execute(ascii_get_item(key, "0", true));
1763
1764 return TEST_PASS;
1765 }
1766
1767 static enum test_return test_ascii_decr(void)
1768 {
1769 return test_ascii_decr_impl("test_ascii_decr", false);
1770 }
1771
1772 static enum test_return test_ascii_decr_noreply(void)
1773 {
1774 return test_ascii_decr_impl("test_ascii_decr_noreply", true);
1775 }
1776
1777
1778 static enum test_return test_ascii_flush_impl(const char *key, bool noreply)
1779 {
1780 #if 0
1781 /* Verify that the flush_all command handles unknown options */
1782 /* Bug in the current memcached server! */
1783 execute(send_string("flush_all foo bar\r\n"));
1784 execute(receive_error_response());
1785 #endif
1786
1787 execute(ascii_set_item(key, key));
1788 execute(ascii_get_item(key, key, true));
1789
1790 if (noreply)
1791 {
1792 execute(send_string("flush_all noreply\r\n"));
1793 execute(test_ascii_version());
1794 }
1795 else
1796 {
1797 execute(send_string("flush_all\r\n"));
1798 execute(receive_response("OK\r\n"));
1799 }
1800
1801 execute(ascii_get_item(key, key, false));
1802
1803 return TEST_PASS;
1804 }
1805
1806 static enum test_return test_ascii_flush(void)
1807 {
1808 return test_ascii_flush_impl("test_ascii_flush", false);
1809 }
1810
1811 static enum test_return test_ascii_flush_noreply(void)
1812 {
1813 return test_ascii_flush_impl("test_ascii_flush_noreply", true);
1814 }
1815
1816 static enum test_return test_ascii_concat_impl(const char *key,
1817 bool append,
1818 bool noreply)
1819 {
1820 const char *value;
1821
1822 if (append)
1823 value="hello";
1824 else
1825 value=" world";
1826
1827 execute(ascii_set_item(key, value));
1828
1829 if (append)
1830 value=" world";
1831 else
1832 value="hello";
1833
1834 char cmd[400];
1835 snprintf(cmd, sizeof(cmd), "%s %s 0 0 %u%s\r\n%s\r\n",
1836 append ? "append" : "prepend",
1837 key, (unsigned int)strlen(value), noreply ? " noreply" : "",
1838 value);
1839 execute(send_string(cmd));
1840
1841 if (noreply)
1842 execute(test_ascii_version());
1843 else
1844 execute(receive_response("STORED\r\n"));
1845
1846 execute(ascii_get_item(key, "hello world", true));
1847
1848 snprintf(cmd, sizeof(cmd), "%s %s_notfound 0 0 %u%s\r\n%s\r\n",
1849 append ? "append" : "prepend",
1850 key, (unsigned int)strlen(value), noreply ? " noreply" : "",
1851 value);
1852 execute(send_string(cmd));
1853
1854 if (noreply)
1855 execute(test_ascii_version());
1856 else
1857 execute(receive_response("NOT_STORED\r\n"));
1858
1859 return TEST_PASS;
1860 }
1861
1862 static enum test_return test_ascii_append(void)
1863 {
1864 return test_ascii_concat_impl("test_ascii_append", true, false);
1865 }
1866
1867 static enum test_return test_ascii_prepend(void)
1868 {
1869 return test_ascii_concat_impl("test_ascii_prepend", false, false);
1870 }
1871
1872 static enum test_return test_ascii_append_noreply(void)
1873 {
1874 return test_ascii_concat_impl("test_ascii_append_noreply", true, true);
1875 }
1876
1877 static enum test_return test_ascii_prepend_noreply(void)
1878 {
1879 return test_ascii_concat_impl("test_ascii_prepend_noreply", false, true);
1880 }
1881
1882 static enum test_return test_ascii_stat(void)
1883 {
1884 execute(send_string("stats noreply\r\n"));
1885 execute(receive_error_response());
1886 execute(send_string("stats\r\n"));
1887 char buffer[1024];
1888 do {
1889 execute(receive_line(buffer, sizeof(buffer)));
1890 } while (strcmp(buffer, "END\r\n") != 0);
1891
1892 return TEST_PASS_RECONNECT;
1893 }
1894
1895 typedef enum test_return(*TEST_FUNC)(void);
1896
1897 struct testcase
1898 {
1899 const char *description;
1900 TEST_FUNC function;
1901 };
1902
1903 struct testcase testcases[]= {
1904 { "ascii quit", test_ascii_quit },
1905 { "ascii version", test_ascii_version },
1906 { "ascii verbosity", test_ascii_verbosity },
1907 { "ascii set", test_ascii_set },
1908 { "ascii set noreply", test_ascii_set_noreply },
1909 { "ascii get", test_ascii_get },
1910 { "ascii gets", test_ascii_gets },
1911 { "ascii mget", test_ascii_mget },
1912 { "ascii flush", test_ascii_flush },
1913 { "ascii flush noreply", test_ascii_flush_noreply },
1914 { "ascii add", test_ascii_add },
1915 { "ascii add noreply", test_ascii_add_noreply },
1916 { "ascii replace", test_ascii_replace },
1917 { "ascii replace noreply", test_ascii_replace_noreply },
1918 { "ascii cas", test_ascii_cas },
1919 { "ascii cas noreply", test_ascii_cas_noreply },
1920 { "ascii delete", test_ascii_delete },
1921 { "ascii delete noreply", test_ascii_delete_noreply },
1922 { "ascii incr", test_ascii_incr },
1923 { "ascii incr noreply", test_ascii_incr_noreply },
1924 { "ascii decr", test_ascii_decr },
1925 { "ascii decr noreply", test_ascii_decr_noreply },
1926 { "ascii append", test_ascii_append },
1927 { "ascii append noreply", test_ascii_append_noreply },
1928 { "ascii prepend", test_ascii_prepend },
1929 { "ascii prepend noreply", test_ascii_prepend_noreply },
1930 { "ascii stat", test_ascii_stat },
1931 { "binary noop", test_binary_noop },
1932 { "binary quit", test_binary_quit },
1933 { "binary quitq", test_binary_quitq },
1934 { "binary set", test_binary_set },
1935 { "binary setq", test_binary_setq },
1936 { "binary flush", test_binary_flush },
1937 { "binary flushq", test_binary_flushq },
1938 { "binary add", test_binary_add },
1939 { "binary addq", test_binary_addq },
1940 { "binary replace", test_binary_replace },
1941 { "binary replaceq", test_binary_replaceq },
1942 { "binary delete", test_binary_delete },
1943 { "binary deleteq", test_binary_deleteq },
1944 { "binary get", test_binary_get },
1945 { "binary getq", test_binary_getq },
1946 { "binary getk", test_binary_getk },
1947 { "binary getkq", test_binary_getkq },
1948 { "binary incr", test_binary_incr },
1949 { "binary incrq", test_binary_incrq },
1950 { "binary decr", test_binary_decr },
1951 { "binary decrq", test_binary_decrq },
1952 { "binary version", test_binary_version },
1953 { "binary append", test_binary_append },
1954 { "binary appendq", test_binary_appendq },
1955 { "binary prepend", test_binary_prepend },
1956 { "binary prependq", test_binary_prependq },
1957 { "binary stat", test_binary_stat },
1958 { NULL, NULL}
1959 };
1960
1961 const int ascii_tests = 1;
1962 const int binary_tests = 2;
1963
1964 struct test_type_st
1965 {
1966 bool ascii;
1967 bool binary;
1968 };
1969
1970 int main(int argc, char **argv)
1971 {
1972 static const char * const status_msg[]= {"[skip]", "[pass]", "[pass]", "[FAIL]"};
1973 struct test_type_st tests= { true, true };
1974 int total= 0;
1975 int failed= 0;
1976 const char *hostname= "localhost";
1977 const char *port= "11211";
1978 int cmd;
1979 bool prompt= false;
1980 const char *testname= NULL;
1981
1982
1983
1984 while ((cmd= getopt(argc, argv, "qt:vch:p:PT:?ab")) != EOF)
1985 {
1986 switch (cmd) {
1987 case 'a':
1988 tests.ascii= true;
1989 tests.binary= false;
1990 break;
1991
1992 case 'b':
1993 tests.ascii= false;
1994 tests.binary= true;
1995 break;
1996
1997 case 't':
1998 timeout= atoi(optarg);
1999 if (timeout == 0)
2000 {
2001 fprintf(stderr, "Invalid timeout. Please specify a number for -t\n");
2002 return EXIT_FAILURE;
2003 }
2004 break;
2005
2006 case 'v': verbose= true;
2007 break;
2008
2009 case 'c': do_core= true;
2010 break;
2011
2012 case 'h': hostname= optarg;
2013 break;
2014
2015 case 'p': port= optarg;
2016 break;
2017
2018 case 'q':
2019 close_stdio();
2020 break;
2021
2022 case 'P': prompt= true;
2023 break;
2024
2025 case 'T': testname= optarg;
2026 break;
2027
2028 default:
2029 fprintf(stderr, "Usage: %s [-h hostname] [-p port] [-c] [-v] [-t n] [-P] [-T testname]'\n"
2030 "\t-c\tGenerate coredump if a test fails\n"
2031 "\t-v\tVerbose test output (print out the assertion)\n"
2032 "\t-t n\tSet the timeout for io-operations to n seconds\n"
2033 "\t-P\tPrompt the user before starting a test.\n"
2034 "\t\t\t\"skip\" will skip the test\n"
2035 "\t\t\t\"quit\" will terminate memcapable\n"
2036 "\t\t\tEverything else will start the test\n"
2037 "\t-T n\tJust run the test named n\n"
2038 "\t-a\tOnly test the ascii protocol\n"
2039 "\t-b\tOnly test the binary protocol\n",
2040 argv[0]);
2041 return EXIT_FAILURE;
2042 }
2043 }
2044
2045 initialize_sockets();
2046 sock= connect_server(hostname, port);
2047 if (sock == INVALID_SOCKET)
2048 {
2049 fprintf(stderr, "Failed to connect to <%s:%s>: %s\n",
2050 hostname, port, strerror(get_socket_errno()));
2051 return EXIT_FAILURE;
2052 }
2053
2054 for (int ii= 0; testcases[ii].description != NULL; ++ii)
2055 {
2056 if (testname != NULL && strcmp(testcases[ii].description, testname) != 0)
2057 continue;
2058
2059 if ((testcases[ii].description[0] == 'a' && (tests.ascii) == 0) ||
2060 (testcases[ii].description[0] == 'b' && (tests.binary) == 0))
2061 {
2062 continue;
2063 }
2064 ++total;
2065 fprintf(stdout, "%-40s", testcases[ii].description);
2066 fflush(stdout);
2067
2068 if (prompt)
2069 {
2070 fprintf(stdout, "\nPress <return> when you are ready? ");
2071 char buffer[80] = {0};
2072 if (fgets(buffer, sizeof(buffer), stdin) != NULL) {
2073 if (strncmp(buffer, "skip", 4) == 0)
2074 {
2075 fprintf(stdout, "%-40s%s\n", testcases[ii].description,
2076 status_msg[TEST_SKIP]);
2077 fflush(stdout);
2078 continue;
2079 }
2080 if (strncmp(buffer, "quit", 4) == 0)
2081 exit(0);
2082 }
2083
2084 fprintf(stdout, "%-40s", testcases[ii].description);
2085 fflush(stdout);
2086 }
2087
2088 bool reconnect= false;
2089 enum test_return ret= testcases[ii].function();
2090 if (ret == TEST_FAIL)
2091 {
2092 reconnect= true;
2093 ++failed;
2094 if (verbose)
2095 fprintf(stderr, "\n");
2096 }
2097 else if (ret == TEST_PASS_RECONNECT)
2098 reconnect= true;
2099
2100 fprintf(stderr, "%s\n", status_msg[ret]);
2101 if (reconnect)
2102 {
2103 closesocket(sock);
2104 if ((sock= connect_server(hostname, port)) == INVALID_SOCKET)
2105 {
2106 fprintf(stderr, "Failed to connect to <%s:%s>: %s\n",
2107 hostname, port, strerror(get_socket_errno()));
2108 fprintf(stderr, "%d of %d tests failed\n", failed, total);
2109 return EXIT_FAILURE;
2110 }
2111 }
2112 }
2113
2114 closesocket(sock);
2115 if (failed == 0)
2116 fprintf(stdout, "All tests passed\n");
2117 else
2118 fprintf(stderr, "%d of %d tests failed\n", failed, total);
2119
2120 return (failed == 0) ? 0 : 1;
2121 }