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