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