Fixes a typo/oops that shows up on openbsd.
[m6w6/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 exptime 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 exptime)
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= exptime;
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 exptime 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 exptime)
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(exptime);
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
579 static enum test_return send_binary_noop(void)
580 {
581 command cmd;
582 raw_command(&cmd, PROTOCOL_BINARY_CMD_NOOP, NULL, 0, NULL, 0);
583 execute(send_packet(&cmd));
584 return TEST_PASS;
585 }
586
587 static enum test_return receive_binary_noop(void)
588 {
589 response rsp;
590 execute(recv_packet(&rsp));
591 verify(validate_response_header(&rsp, PROTOCOL_BINARY_CMD_NOOP,
592 PROTOCOL_BINARY_RESPONSE_SUCCESS));
593 return TEST_PASS;
594 }
595
596 static enum test_return test_binary_noop(void)
597 {
598 execute(send_binary_noop());
599 execute(receive_binary_noop());
600 return TEST_PASS;
601 }
602
603 static enum test_return test_binary_quit_impl(uint8_t cc)
604 {
605 command cmd;
606 response rsp;
607 raw_command(&cmd, cc, NULL, 0, NULL, 0);
608
609 execute(send_packet(&cmd));
610 if (cc == PROTOCOL_BINARY_CMD_QUIT)
611 {
612 execute(recv_packet(&rsp));
613 verify(validate_response_header(&rsp, PROTOCOL_BINARY_CMD_QUIT,
614 PROTOCOL_BINARY_RESPONSE_SUCCESS));
615 }
616
617 /* Socket should be closed now, read should return 0 */
618 verify(timeout_io_op(sock, POLLIN, rsp.bytes, sizeof(rsp.bytes)) == 0);
619
620 return TEST_PASS_RECONNECT;
621 }
622
623 static enum test_return test_binary_quit(void)
624 {
625 return test_binary_quit_impl(PROTOCOL_BINARY_CMD_QUIT);
626 }
627
628 static enum test_return test_binary_quitq(void)
629 {
630 return test_binary_quit_impl(PROTOCOL_BINARY_CMD_QUITQ);
631 }
632
633 static enum test_return test_binary_set_impl(const char* key, uint8_t cc)
634 {
635 command cmd;
636 response rsp;
637
638 uint64_t value= 0xdeadbeefdeadcafe;
639 storage_command(&cmd, cc, key, strlen(key), &value, sizeof (value), 0, 0);
640
641 /* set should always work */
642 for (int ii= 0; ii < 10; ii++)
643 {
644 if (ii == 0)
645 execute(send_packet(&cmd));
646 else
647 execute(resend_packet(&cmd));
648
649 if (cc == PROTOCOL_BINARY_CMD_SET)
650 {
651 execute(recv_packet(&rsp));
652 verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_SUCCESS));
653 }
654 else
655 execute(test_binary_noop());
656 }
657
658 /*
659 * We need to get the current CAS id, and at this time we haven't
660 * verified that we have a working get
661 */
662 if (cc == PROTOCOL_BINARY_CMD_SETQ)
663 {
664 cmd.set.message.header.request.opcode= PROTOCOL_BINARY_CMD_SET;
665 execute(resend_packet(&cmd));
666 execute(recv_packet(&rsp));
667 verify(validate_response_header(&rsp, PROTOCOL_BINARY_CMD_SET,
668 PROTOCOL_BINARY_RESPONSE_SUCCESS));
669 cmd.set.message.header.request.opcode= PROTOCOL_BINARY_CMD_SETQ;
670 }
671
672 /* try to set with the correct CAS value */
673 cmd.plain.message.header.request.cas=
674 htonll(rsp.plain.message.header.response.cas);
675 execute(resend_packet(&cmd));
676 if (cc == PROTOCOL_BINARY_CMD_SET)
677 {
678 execute(recv_packet(&rsp));
679 verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_SUCCESS));
680 }
681 else
682 execute(test_binary_noop());
683
684 /* try to set with an incorrect CAS value */
685 cmd.plain.message.header.request.cas=
686 htonll(rsp.plain.message.header.response.cas - 1);
687 execute(resend_packet(&cmd));
688 execute(recv_packet(&rsp));
689 verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS));
690
691 return test_binary_noop();
692 }
693
694 static enum test_return test_binary_set(void)
695 {
696 return test_binary_set_impl("test_binary_set", PROTOCOL_BINARY_CMD_SET);
697 }
698
699 static enum test_return test_binary_setq(void)
700 {
701 return test_binary_set_impl("test_binary_setq", PROTOCOL_BINARY_CMD_SETQ);
702 }
703
704 static enum test_return test_binary_add_impl(const char* key, uint8_t cc)
705 {
706 command cmd;
707 response rsp;
708 uint64_t value= 0xdeadbeefdeadcafe;
709 storage_command(&cmd, cc, key, strlen(key), &value, sizeof (value), 0, 0);
710
711 /* first add should work, rest of them should fail (even with cas
712 as wildcard */
713 for (int ii=0; ii < 10; ii++)
714 {
715 if (ii == 0)
716 execute(send_packet(&cmd));
717 else
718 execute(resend_packet(&cmd));
719
720 if (cc == PROTOCOL_BINARY_CMD_ADD || ii > 0)
721 {
722 uint16_t expected_result;
723 if (ii == 0)
724 expected_result= PROTOCOL_BINARY_RESPONSE_SUCCESS;
725 else
726 expected_result= PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS;
727
728 execute(recv_packet(&rsp));
729 verify(validate_response_header(&rsp, cc, expected_result));
730 }
731 else
732 execute(test_binary_noop());
733 }
734
735 return TEST_PASS;
736 }
737
738 static enum test_return test_binary_add(void)
739 {
740 return test_binary_add_impl("test_binary_add", PROTOCOL_BINARY_CMD_ADD);
741 }
742
743 static enum test_return test_binary_addq(void)
744 {
745 return test_binary_add_impl("test_binary_addq", PROTOCOL_BINARY_CMD_ADDQ);
746 }
747
748 static enum test_return binary_set_item(const char *key, const char *value)
749 {
750 command cmd;
751 response rsp;
752 storage_command(&cmd, PROTOCOL_BINARY_CMD_SET, key, strlen(key),
753 value, strlen(value), 0, 0);
754 execute(send_packet(&cmd));
755 execute(recv_packet(&rsp));
756 verify(validate_response_header(&rsp, PROTOCOL_BINARY_CMD_SET,
757 PROTOCOL_BINARY_RESPONSE_SUCCESS));
758 return TEST_PASS;
759 }
760
761 static enum test_return test_binary_replace_impl(const char* key, uint8_t cc)
762 {
763 command cmd;
764 response rsp;
765 uint64_t value= 0xdeadbeefdeadcafe;
766 storage_command(&cmd, cc, key, strlen(key), &value, sizeof (value), 0, 0);
767
768 /* first replace should fail, successive should succeed (when the
769 item is added! */
770 for (int ii= 0; ii < 10; ii++)
771 {
772 if (ii == 0)
773 execute(send_packet(&cmd));
774 else
775 execute(resend_packet(&cmd));
776
777 if (cc == PROTOCOL_BINARY_CMD_REPLACE || ii == 0)
778 {
779 uint16_t expected_result;
780 if (ii == 0)
781 expected_result=PROTOCOL_BINARY_RESPONSE_KEY_ENOENT;
782 else
783 expected_result=PROTOCOL_BINARY_RESPONSE_SUCCESS;
784
785 execute(recv_packet(&rsp));
786 verify(validate_response_header(&rsp, cc, expected_result));
787
788 if (ii == 0)
789 execute(binary_set_item(key, key));
790 }
791 else
792 execute(test_binary_noop());
793 }
794
795 /* verify that replace with CAS value works! */
796 cmd.plain.message.header.request.cas=
797 htonll(rsp.plain.message.header.response.cas);
798 execute(resend_packet(&cmd));
799
800 if (cc == PROTOCOL_BINARY_CMD_REPLACE)
801 {
802 execute(recv_packet(&rsp));
803 verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_SUCCESS));
804 }
805 else
806 execute(test_binary_noop());
807
808 /* try to set with an incorrect CAS value */
809 cmd.plain.message.header.request.cas=
810 htonll(rsp.plain.message.header.response.cas - 1);
811 execute(resend_packet(&cmd));
812 execute(recv_packet(&rsp));
813 verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS));
814
815 return TEST_PASS;
816 }
817
818 static enum test_return test_binary_replace(void)
819 {
820 return test_binary_replace_impl("test_binary_replace", PROTOCOL_BINARY_CMD_REPLACE);
821 }
822
823 static enum test_return test_binary_replaceq(void)
824 {
825 return test_binary_replace_impl("test_binary_replaceq", PROTOCOL_BINARY_CMD_REPLACEQ);
826 }
827
828 static enum test_return test_binary_delete_impl(const char *key, uint8_t cc)
829 {
830 command cmd;
831 response rsp;
832 raw_command(&cmd, cc, key, strlen(key), NULL, 0);
833
834 /* The delete shouldn't work the first time, because the item isn't there */
835 execute(send_packet(&cmd));
836 execute(recv_packet(&rsp));
837 verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_KEY_ENOENT));
838 execute(binary_set_item(key, key));
839
840 /* The item should be present now, resend*/
841 execute(resend_packet(&cmd));
842 if (cc == PROTOCOL_BINARY_CMD_DELETE)
843 {
844 execute(recv_packet(&rsp));
845 verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_SUCCESS));
846 }
847
848 execute(test_binary_noop());
849
850 return TEST_PASS;
851 }
852
853 static enum test_return test_binary_delete(void)
854 {
855 return test_binary_delete_impl("test_binary_delete", PROTOCOL_BINARY_CMD_DELETE);
856 }
857
858 static enum test_return test_binary_deleteq(void)
859 {
860 return test_binary_delete_impl("test_binary_deleteq", PROTOCOL_BINARY_CMD_DELETEQ);
861 }
862
863 static enum test_return test_binary_get_impl(const char *key, uint8_t cc)
864 {
865 command cmd;
866 response rsp;
867
868 raw_command(&cmd, cc, key, strlen(key), NULL, 0);
869 execute(send_packet(&cmd));
870 execute(send_binary_noop());
871
872 if (cc == PROTOCOL_BINARY_CMD_GET || cc == PROTOCOL_BINARY_CMD_GETK)
873 {
874 execute(recv_packet(&rsp));
875 verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_KEY_ENOENT));
876 }
877
878 execute(receive_binary_noop());
879
880 execute(binary_set_item(key, key));
881 execute(resend_packet(&cmd));
882 execute(send_binary_noop());
883
884 execute(recv_packet(&rsp));
885 verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_SUCCESS));
886 execute(receive_binary_noop());
887
888 return TEST_PASS;
889 }
890
891 static enum test_return test_binary_get(void)
892 {
893 return test_binary_get_impl("test_binary_get", PROTOCOL_BINARY_CMD_GET);
894 }
895
896 static enum test_return test_binary_getk(void)
897 {
898 return test_binary_get_impl("test_binary_getk", PROTOCOL_BINARY_CMD_GETK);
899 }
900
901 static enum test_return test_binary_getq(void)
902 {
903 return test_binary_get_impl("test_binary_getq", PROTOCOL_BINARY_CMD_GETQ);
904 }
905
906 static enum test_return test_binary_getkq(void)
907 {
908 return test_binary_get_impl("test_binary_getkq", PROTOCOL_BINARY_CMD_GETKQ);
909 }
910
911 static enum test_return test_binary_incr_impl(const char* key, uint8_t cc)
912 {
913 command cmd;
914 response rsp;
915 arithmetic_command(&cmd, cc, key, strlen(key), 1, 0, 0);
916
917 uint64_t ii;
918 for (ii= 0; ii < 10; ++ii)
919 {
920 if (ii == 0)
921 execute(send_packet(&cmd));
922 else
923 execute(resend_packet(&cmd));
924
925 if (cc == PROTOCOL_BINARY_CMD_INCREMENT)
926 {
927 execute(recv_packet(&rsp));
928 verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_SUCCESS));
929 verify(ntohll(rsp.incr.message.body.value) == ii);
930 }
931 else
932 execute(test_binary_noop());
933 }
934
935 /* @todo add incorrect CAS */
936 return TEST_PASS;
937 }
938
939 static enum test_return test_binary_incr(void)
940 {
941 return test_binary_incr_impl("test_binary_incr", PROTOCOL_BINARY_CMD_INCREMENT);
942 }
943
944 static enum test_return test_binary_incrq(void)
945 {
946 return test_binary_incr_impl("test_binary_incrq", PROTOCOL_BINARY_CMD_INCREMENTQ);
947 }
948
949 static enum test_return test_binary_decr_impl(const char* key, uint8_t cc)
950 {
951 command cmd;
952 response rsp;
953 arithmetic_command(&cmd, cc, key, strlen(key), 1, 9, 0);
954
955 int ii;
956 for (ii= 9; ii > -1; --ii)
957 {
958 if (ii == 9)
959 execute(send_packet(&cmd));
960 else
961 execute(resend_packet(&cmd));
962
963 if (cc == PROTOCOL_BINARY_CMD_DECREMENT)
964 {
965 execute(recv_packet(&rsp));
966 verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_SUCCESS));
967 verify(ntohll(rsp.decr.message.body.value) == (uint64_t)ii);
968 }
969 else
970 execute(test_binary_noop());
971 }
972
973 /* decr 0 should not wrap */
974 execute(resend_packet(&cmd));
975 if (cc == PROTOCOL_BINARY_CMD_DECREMENT)
976 {
977 execute(recv_packet(&rsp));
978 verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_SUCCESS));
979 verify(ntohll(rsp.decr.message.body.value) == 0);
980 }
981 else
982 {
983 /* @todo get the value and verify! */
984
985 }
986
987 /* @todo add incorrect cas */
988 execute(test_binary_noop());
989 return TEST_PASS;
990 }
991
992 static enum test_return test_binary_decr(void)
993 {
994 return test_binary_decr_impl("test_binary_decr",
995 PROTOCOL_BINARY_CMD_DECREMENT);
996 }
997
998 static enum test_return test_binary_decrq(void)
999 {
1000 return test_binary_decr_impl("test_binary_decrq",
1001 PROTOCOL_BINARY_CMD_DECREMENTQ);
1002 }
1003
1004 static enum test_return test_binary_version(void)
1005 {
1006 command cmd;
1007 response rsp;
1008 raw_command(&cmd, PROTOCOL_BINARY_CMD_VERSION, NULL, 0, NULL, 0);
1009
1010 execute(send_packet(&cmd));
1011 execute(recv_packet(&rsp));
1012 verify(validate_response_header(&rsp, PROTOCOL_BINARY_CMD_VERSION,
1013 PROTOCOL_BINARY_RESPONSE_SUCCESS));
1014
1015 return TEST_PASS;
1016 }
1017
1018 static enum test_return test_binary_flush_impl(const char *key, uint8_t cc)
1019 {
1020 command cmd;
1021 response rsp;
1022
1023 for (int ii= 0; ii < 2; ++ii)
1024 {
1025 execute(binary_set_item(key, key));
1026 flush_command(&cmd, cc, 0, ii == 0);
1027 execute(send_packet(&cmd));
1028
1029 if (cc == PROTOCOL_BINARY_CMD_FLUSH)
1030 {
1031 execute(recv_packet(&rsp));
1032 verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_SUCCESS));
1033 }
1034 else
1035 execute(test_binary_noop());
1036
1037 raw_command(&cmd, PROTOCOL_BINARY_CMD_GET, key, strlen(key), NULL, 0);
1038 execute(send_packet(&cmd));
1039 execute(recv_packet(&rsp));
1040 verify(validate_response_header(&rsp, PROTOCOL_BINARY_CMD_GET,
1041 PROTOCOL_BINARY_RESPONSE_KEY_ENOENT));
1042 }
1043
1044 return TEST_PASS;
1045 }
1046
1047 static enum test_return test_binary_flush(void)
1048 {
1049 return test_binary_flush_impl("test_binary_flush", PROTOCOL_BINARY_CMD_FLUSH);
1050 }
1051
1052 static enum test_return test_binary_flushq(void)
1053 {
1054 return test_binary_flush_impl("test_binary_flushq", PROTOCOL_BINARY_CMD_FLUSHQ);
1055 }
1056
1057 static enum test_return test_binary_concat_impl(const char *key, uint8_t cc)
1058 {
1059 command cmd;
1060 response rsp;
1061 const char *value;
1062
1063 if (cc == PROTOCOL_BINARY_CMD_APPEND || cc == PROTOCOL_BINARY_CMD_APPENDQ)
1064 value="hello";
1065 else
1066 value=" world";
1067
1068 execute(binary_set_item(key, value));
1069
1070 if (cc == PROTOCOL_BINARY_CMD_APPEND || cc == PROTOCOL_BINARY_CMD_APPENDQ)
1071 value=" world";
1072 else
1073 value="hello";
1074
1075 raw_command(&cmd, cc, key, strlen(key), value, strlen(value));
1076 execute(send_packet(&cmd));
1077 if (cc == PROTOCOL_BINARY_CMD_APPEND || cc == PROTOCOL_BINARY_CMD_PREPEND)
1078 {
1079 execute(recv_packet(&rsp));
1080 verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_SUCCESS));
1081 }
1082 else
1083 execute(test_binary_noop());
1084
1085 raw_command(&cmd, PROTOCOL_BINARY_CMD_GET, key, strlen(key), NULL, 0);
1086 execute(send_packet(&cmd));
1087 execute(recv_packet(&rsp));
1088 verify(validate_response_header(&rsp, PROTOCOL_BINARY_CMD_GET,
1089 PROTOCOL_BINARY_RESPONSE_SUCCESS));
1090 verify(rsp.plain.message.header.response.bodylen - 4 == 11);
1091 verify(memcmp(rsp.bytes + 28, "hello world", 11) == 0);
1092
1093 return TEST_PASS;
1094 }
1095
1096 static enum test_return test_binary_append(void)
1097 {
1098 return test_binary_concat_impl("test_binary_append", PROTOCOL_BINARY_CMD_APPEND);
1099 }
1100
1101 static enum test_return test_binary_prepend(void)
1102 {
1103 return test_binary_concat_impl("test_binary_prepend", PROTOCOL_BINARY_CMD_PREPEND);
1104 }
1105
1106 static enum test_return test_binary_appendq(void)
1107 {
1108 return test_binary_concat_impl("test_binary_appendq", PROTOCOL_BINARY_CMD_APPENDQ);
1109 }
1110
1111 static enum test_return test_binary_prependq(void)
1112 {
1113 return test_binary_concat_impl("test_binary_prependq", PROTOCOL_BINARY_CMD_PREPENDQ);
1114 }
1115
1116 static enum test_return test_binary_stat(void)
1117 {
1118 command cmd;
1119 response rsp;
1120
1121 raw_command(&cmd, PROTOCOL_BINARY_CMD_STAT, NULL, 0, NULL, 0);
1122 execute(send_packet(&cmd));
1123
1124 do
1125 {
1126 execute(recv_packet(&rsp));
1127 verify(validate_response_header(&rsp, PROTOCOL_BINARY_CMD_STAT,
1128 PROTOCOL_BINARY_RESPONSE_SUCCESS));
1129 } while (rsp.plain.message.header.response.keylen != 0);
1130
1131 return TEST_PASS;
1132 }
1133
1134 static enum test_return send_string(const char *cmd)
1135 {
1136 execute(retry_write(cmd, strlen(cmd)));
1137 return TEST_PASS;
1138 }
1139
1140 static enum test_return receive_line(char *buffer, size_t size)
1141 {
1142 size_t offset= 0;
1143 while (offset < size)
1144 {
1145 execute(retry_read(buffer + offset, 1));
1146 if (buffer[offset] == '\n')
1147 {
1148 if (offset + 1 < size)
1149 {
1150 buffer[offset + 1]= '\0';
1151 return TEST_PASS;
1152 }
1153 else
1154 return TEST_FAIL;
1155 }
1156 ++offset;
1157 }
1158
1159 return TEST_FAIL;
1160 }
1161
1162 static enum test_return receive_response(const char *msg) {
1163 char buffer[80];
1164 execute(receive_line(buffer, sizeof(buffer)));
1165 if (strcmp(msg, buffer) != 0) {
1166 fprintf(stderr, "[%s]\n", buffer);
1167 }
1168 verify(strcmp(msg, buffer) == 0);
1169 return TEST_PASS;
1170 }
1171
1172 static enum test_return receive_error_response(void)
1173 {
1174 char buffer[80];
1175 execute(receive_line(buffer, sizeof(buffer)));
1176 verify(strncmp(buffer, "ERROR", 5) == 0 ||
1177 strncmp(buffer, "CLIENT_ERROR", 12) == 0 ||
1178 strncmp(buffer, "SERVER_ERROR", 12) == 0);
1179 return TEST_PASS;
1180 }
1181
1182 static enum test_return test_ascii_quit(void)
1183 {
1184 /* Verify that quit handles unknown options */
1185 execute(send_string("quit foo bar\r\n"));
1186 execute(receive_error_response());
1187
1188 /* quit doesn't support noreply */
1189 execute(send_string("quit noreply\r\n"));
1190 execute(receive_error_response());
1191
1192 /* Verify that quit works */
1193 execute(send_string("quit\r\n"));
1194
1195 /* Socket should be closed now, read should return 0 */
1196 char buffer[80];
1197 verify(timeout_io_op(sock, POLLIN, buffer, sizeof(buffer)) == 0);
1198 return TEST_PASS_RECONNECT;
1199
1200 }
1201
1202 static enum test_return test_ascii_version(void)
1203 {
1204 /* Verify that version command handles unknown options */
1205 execute(send_string("version foo bar\r\n"));
1206 execute(receive_error_response());
1207
1208 /* version doesn't support noreply */
1209 execute(send_string("version noreply\r\n"));
1210 execute(receive_error_response());
1211
1212 /* Verify that verify works */
1213 execute(send_string("version\r\n"));
1214 char buffer[256];
1215 execute(receive_line(buffer, sizeof(buffer)));
1216 verify(strncmp(buffer, "VERSION ", 8) == 0);
1217
1218 return TEST_PASS;
1219 }
1220
1221 static enum test_return test_ascii_verbosity(void)
1222 {
1223 /* This command does not adhere to the spec! */
1224 execute(send_string("verbosity foo bar my\r\n"));
1225 execute(receive_error_response());
1226
1227 execute(send_string("verbosity noreply\r\n"));
1228 execute(receive_error_response());
1229
1230 execute(send_string("verbosity 0 noreply\r\n"));
1231 execute(test_ascii_version());
1232
1233 execute(send_string("verbosity\r\n"));
1234 execute(receive_error_response());
1235
1236 execute(send_string("verbosity 1\r\n"));
1237 execute(receive_response("OK\r\n"));
1238
1239 execute(send_string("verbosity 0\r\n"));
1240 execute(receive_response("OK\r\n"));
1241
1242 return TEST_PASS;
1243 }
1244
1245
1246
1247 static enum test_return test_ascii_set_impl(const char* key, bool noreply)
1248 {
1249 /* @todo add tests for bogus format! */
1250 char buffer[1024];
1251 sprintf(buffer, "set %s 0 0 5%s\r\nvalue\r\n", key,
1252 noreply ? " noreply" : "");
1253 execute(send_string(buffer));
1254
1255 if (!noreply)
1256 execute(receive_response("STORED\r\n"));
1257
1258 return test_ascii_version();
1259 }
1260
1261 static enum test_return test_ascii_set(void)
1262 {
1263 return test_ascii_set_impl("test_ascii_set", false);
1264 }
1265
1266 static enum test_return test_ascii_set_noreply(void)
1267 {
1268 return test_ascii_set_impl("test_ascii_set_noreply", true);
1269 }
1270
1271 static enum test_return test_ascii_add_impl(const char* key, bool noreply)
1272 {
1273 /* @todo add tests for bogus format! */
1274 char buffer[1024];
1275 sprintf(buffer, "add %s 0 0 5%s\r\nvalue\r\n", key,
1276 noreply ? " noreply" : "");
1277 execute(send_string(buffer));
1278
1279 if (!noreply)
1280 execute(receive_response("STORED\r\n"));
1281
1282 execute(send_string(buffer));
1283
1284 if (!noreply)
1285 execute(receive_response("NOT_STORED\r\n"));
1286
1287 return test_ascii_version();
1288 }
1289
1290 static enum test_return test_ascii_add(void)
1291 {
1292 return test_ascii_add_impl("test_ascii_add", false);
1293 }
1294
1295 static enum test_return test_ascii_add_noreply(void)
1296 {
1297 return test_ascii_add_impl("test_ascii_add_noreply", true);
1298 }
1299
1300 static enum test_return ascii_get_value(const char *key, const char *value)
1301 {
1302
1303 char buffer[1024];
1304 size_t datasize= strlen(value);
1305
1306 verify(datasize < sizeof(buffer));
1307 execute(receive_line(buffer, sizeof(buffer)));
1308 verify(strncmp(buffer, "VALUE ", 6) == 0);
1309 verify(strncmp(buffer + 6, key, strlen(key)) == 0);
1310 char *ptr= buffer + 6 + strlen(key) + 1;
1311 char *end;
1312
1313 unsigned long val= strtoul(ptr, &end, 10); /* flags */
1314 verify(ptr != end);
1315 verify(val == 0);
1316 verify(end != NULL);
1317 val= strtoul(end, &end, 10); /* size */
1318 verify(ptr != end);
1319 verify(val == datasize);
1320 verify(end != NULL);
1321 while (*end != '\n' && isspace(*end))
1322 ++end;
1323 verify(*end == '\n');
1324
1325 execute(retry_read(buffer, datasize));
1326 verify(memcmp(buffer, value, datasize) == 0);
1327
1328 execute(retry_read(buffer, 2));
1329 verify(memcmp(buffer, "\r\n", 2) == 0);
1330
1331 return TEST_PASS;
1332 }
1333
1334 static enum test_return ascii_get_item(const char *key, const char *value,
1335 bool exist)
1336 {
1337 char buffer[1024];
1338 size_t datasize= 0;
1339 if (value != NULL)
1340 datasize= strlen(value);
1341
1342 verify(datasize < sizeof(buffer));
1343 sprintf(buffer, "get %s\r\n", key);
1344 execute(send_string(buffer));
1345
1346 if (exist)
1347 execute(ascii_get_value(key, value));
1348
1349 execute(retry_read(buffer, 5));
1350 verify(memcmp(buffer, "END\r\n", 5) == 0);
1351
1352 return TEST_PASS;
1353 }
1354
1355 static enum test_return ascii_gets_value(const char *key, const char *value,
1356 unsigned long *cas)
1357 {
1358
1359 char buffer[1024];
1360 size_t datasize= strlen(value);
1361
1362 verify(datasize < sizeof(buffer));
1363 execute(receive_line(buffer, sizeof(buffer)));
1364 verify(strncmp(buffer, "VALUE ", 6) == 0);
1365 verify(strncmp(buffer + 6, key, strlen(key)) == 0);
1366 char *ptr= buffer + 6 + strlen(key) + 1;
1367 char *end;
1368
1369 unsigned long val= strtoul(ptr, &end, 10); /* flags */
1370 verify(ptr != end);
1371 verify(val == 0);
1372 verify(end != NULL);
1373 val= strtoul(end, &end, 10); /* size */
1374 verify(ptr != end);
1375 verify(val == datasize);
1376 verify(end != NULL);
1377 *cas= strtoul(end, &end, 10); /* cas */
1378 verify(ptr != end);
1379 verify(val == datasize);
1380 verify(end != NULL);
1381
1382 while (*end != '\n' && isspace(*end))
1383 ++end;
1384 verify(*end == '\n');
1385
1386 execute(retry_read(buffer, datasize));
1387 verify(memcmp(buffer, value, datasize) == 0);
1388
1389 execute(retry_read(buffer, 2));
1390 verify(memcmp(buffer, "\r\n", 2) == 0);
1391
1392 return TEST_PASS;
1393 }
1394
1395 static enum test_return ascii_gets_item(const char *key, const char *value,
1396 bool exist, unsigned long *cas)
1397 {
1398 char buffer[1024];
1399 size_t datasize= 0;
1400 if (value != NULL)
1401 datasize= strlen(value);
1402
1403 verify(datasize < sizeof(buffer));
1404 sprintf(buffer, "gets %s\r\n", key);
1405 execute(send_string(buffer));
1406
1407 if (exist)
1408 execute(ascii_gets_value(key, value, cas));
1409
1410 execute(retry_read(buffer, 5));
1411 verify(memcmp(buffer, "END\r\n", 5) == 0);
1412
1413 return TEST_PASS;
1414 }
1415
1416 static enum test_return ascii_set_item(const char *key, const char *value)
1417 {
1418 char buffer[300];
1419 size_t len= strlen(value);
1420 sprintf(buffer, "set %s 0 0 %u\r\n", key, (unsigned int)len);
1421 execute(send_string(buffer));
1422 execute(retry_write(value, len));
1423 execute(send_string("\r\n"));
1424 execute(receive_response("STORED\r\n"));
1425 return TEST_PASS;
1426 }
1427
1428 static enum test_return test_ascii_replace_impl(const char* key, bool noreply)
1429 {
1430 char buffer[1024];
1431 sprintf(buffer, "replace %s 0 0 5%s\r\nvalue\r\n", key,
1432 noreply ? " noreply" : "");
1433 execute(send_string(buffer));
1434
1435 if (noreply)
1436 execute(test_ascii_version());
1437 else
1438 execute(receive_response("NOT_STORED\r\n"));
1439
1440 execute(ascii_set_item(key, "value"));
1441 execute(ascii_get_item(key, "value", true));
1442
1443
1444 execute(send_string(buffer));
1445
1446 if (noreply)
1447 execute(test_ascii_version());
1448 else
1449 execute(receive_response("STORED\r\n"));
1450
1451 return test_ascii_version();
1452 }
1453
1454 static enum test_return test_ascii_replace(void)
1455 {
1456 return test_ascii_replace_impl("test_ascii_replace", false);
1457 }
1458
1459 static enum test_return test_ascii_replace_noreply(void)
1460 {
1461 return test_ascii_replace_impl("test_ascii_replace_noreply", true);
1462 }
1463
1464 static enum test_return test_ascii_cas_impl(const char* key, bool noreply)
1465 {
1466 char buffer[1024];
1467 unsigned long cas;
1468
1469 execute(ascii_set_item(key, "value"));
1470 execute(ascii_gets_item(key, "value", true, &cas));
1471
1472 sprintf(buffer, "cas %s 0 0 6 %lu%s\r\nvalue2\r\n", key, cas,
1473 noreply ? " noreply" : "");
1474 execute(send_string(buffer));
1475
1476 if (noreply)
1477 execute(test_ascii_version());
1478 else
1479 execute(receive_response("STORED\r\n"));
1480
1481 /* reexecute the same command should fail due to illegal cas */
1482 execute(send_string(buffer));
1483
1484 if (noreply)
1485 execute(test_ascii_version());
1486 else
1487 execute(receive_response("EXISTS\r\n"));
1488
1489 return test_ascii_version();
1490 }
1491
1492 static enum test_return test_ascii_cas(void)
1493 {
1494 return test_ascii_cas_impl("test_ascii_cas", false);
1495 }
1496
1497 static enum test_return test_ascii_cas_noreply(void)
1498 {
1499 return test_ascii_cas_impl("test_ascii_cas_noreply", true);
1500 }
1501
1502 static enum test_return test_ascii_delete_impl(const char *key, bool noreply)
1503 {
1504 execute(ascii_set_item(key, "value"));
1505
1506 execute(send_string("delete\r\n"));
1507 execute(receive_error_response());
1508 /* BUG: the server accepts delete a b */
1509 execute(send_string("delete a b c d e\r\n"));
1510 execute(receive_error_response());
1511
1512 char buffer[1024];
1513 sprintf(buffer, "delete %s%s\r\n", key, noreply ? " noreply" : "");
1514 execute(send_string(buffer));
1515
1516 if (noreply)
1517 execute(test_ascii_version());
1518 else
1519 execute(receive_response("DELETED\r\n"));
1520
1521 execute(ascii_get_item(key, "value", false));
1522 execute(send_string(buffer));
1523 if (noreply)
1524 execute(test_ascii_version());
1525 else
1526 execute(receive_response("NOT_FOUND\r\n"));
1527
1528 return TEST_PASS;
1529 }
1530
1531 static enum test_return test_ascii_delete(void)
1532 {
1533 return test_ascii_delete_impl("test_ascii_delete", false);
1534 }
1535
1536 static enum test_return test_ascii_delete_noreply(void)
1537 {
1538 return test_ascii_delete_impl("test_ascii_delete_noreply", true);
1539 }
1540
1541 static enum test_return test_ascii_get(void)
1542 {
1543 execute(ascii_set_item("test_ascii_get", "value"));
1544
1545 execute(send_string("get\r\n"));
1546 execute(receive_error_response());
1547 execute(ascii_get_item("test_ascii_get", "value", true));
1548 execute(ascii_get_item("test_ascii_get_notfound", "value", false));
1549
1550 return TEST_PASS;
1551 }
1552
1553 static enum test_return test_ascii_gets(void)
1554 {
1555 execute(ascii_set_item("test_ascii_gets", "value"));
1556
1557 execute(send_string("gets\r\n"));
1558 execute(receive_error_response());
1559 unsigned long cas;
1560 execute(ascii_gets_item("test_ascii_gets", "value", true, &cas));
1561 execute(ascii_gets_item("test_ascii_gets_notfound", "value", false, &cas));
1562
1563 return TEST_PASS;
1564 }
1565
1566 static enum test_return test_ascii_mget(void)
1567 {
1568 execute(ascii_set_item("test_ascii_mget1", "value"));
1569 execute(ascii_set_item("test_ascii_mget2", "value"));
1570 execute(ascii_set_item("test_ascii_mget3", "value"));
1571 execute(ascii_set_item("test_ascii_mget4", "value"));
1572 execute(ascii_set_item("test_ascii_mget5", "value"));
1573
1574 execute(send_string("get test_ascii_mget1 test_ascii_mget2 test_ascii_mget3 "
1575 "test_ascii_mget4 test_ascii_mget5 "
1576 "test_ascii_mget6\r\n"));
1577 execute(ascii_get_value("test_ascii_mget1", "value"));
1578 execute(ascii_get_value("test_ascii_mget2", "value"));
1579 execute(ascii_get_value("test_ascii_mget3", "value"));
1580 execute(ascii_get_value("test_ascii_mget4", "value"));
1581 execute(ascii_get_value("test_ascii_mget5", "value"));
1582
1583 char buffer[5];
1584 execute(retry_read(buffer, 5));
1585 verify(memcmp(buffer, "END\r\n", 5) == 0);
1586 return TEST_PASS;
1587 }
1588
1589 static enum test_return test_ascii_incr_impl(const char* key, bool noreply)
1590 {
1591 char cmd[300];
1592 sprintf(cmd, "incr %s 1%s\r\n", key, noreply ? " noreply" : "");
1593
1594 execute(ascii_set_item(key, "0"));
1595 for (int x= 1; x < 11; ++x)
1596 {
1597 execute(send_string(cmd));
1598
1599 if (noreply)
1600 execute(test_ascii_version());
1601 else
1602 {
1603 char buffer[80];
1604 execute(receive_line(buffer, sizeof(buffer)));
1605 int val= atoi(buffer);
1606 verify(val == x);
1607 }
1608 }
1609
1610 execute(ascii_get_item(key, "10", true));
1611
1612 return TEST_PASS;
1613 }
1614
1615 static enum test_return test_ascii_incr(void)
1616 {
1617 return test_ascii_incr_impl("test_ascii_incr", false);
1618 }
1619
1620 static enum test_return test_ascii_incr_noreply(void)
1621 {
1622 return test_ascii_incr_impl("test_ascii_incr_noreply", true);
1623 }
1624
1625 static enum test_return test_ascii_decr_impl(const char* key, bool noreply)
1626 {
1627 char cmd[300];
1628 sprintf(cmd, "decr %s 1%s\r\n", key, noreply ? " noreply" : "");
1629
1630 execute(ascii_set_item(key, "9"));
1631 for (int x= 8; x > -1; --x)
1632 {
1633 execute(send_string(cmd));
1634
1635 if (noreply)
1636 execute(test_ascii_version());
1637 else
1638 {
1639 char buffer[80];
1640 execute(receive_line(buffer, sizeof(buffer)));
1641 int val= atoi(buffer);
1642 verify(val == x);
1643 }
1644 }
1645
1646 execute(ascii_get_item(key, "0", true));
1647
1648 /* verify that it doesn't wrap */
1649 execute(send_string(cmd));
1650 if (noreply)
1651 execute(test_ascii_version());
1652 else
1653 {
1654 char buffer[80];
1655 execute(receive_line(buffer, sizeof(buffer)));
1656 }
1657 execute(ascii_get_item(key, "0", true));
1658
1659 return TEST_PASS;
1660 }
1661
1662 static enum test_return test_ascii_decr(void)
1663 {
1664 return test_ascii_decr_impl("test_ascii_decr", false);
1665 }
1666
1667 static enum test_return test_ascii_decr_noreply(void)
1668 {
1669 return test_ascii_decr_impl("test_ascii_decr_noreply", true);
1670 }
1671
1672
1673 static enum test_return test_ascii_flush_impl(const char *key, bool noreply)
1674 {
1675 #if 0
1676 /* Verify that the flush_all command handles unknown options */
1677 /* Bug in the current memcached server! */
1678 execute(send_string("flush_all foo bar\r\n"));
1679 execute(receive_error_response());
1680 #endif
1681
1682 execute(ascii_set_item(key, key));
1683 execute(ascii_get_item(key, key, true));
1684
1685 if (noreply)
1686 {
1687 execute(send_string("flush_all noreply\r\n"));
1688 execute(test_ascii_version());
1689 }
1690 else
1691 {
1692 execute(send_string("flush_all\r\n"));
1693 execute(receive_response("OK\r\n"));
1694 }
1695
1696 execute(ascii_get_item(key, key, false));
1697
1698 return TEST_PASS;
1699 }
1700
1701 static enum test_return test_ascii_flush(void)
1702 {
1703 return test_ascii_flush_impl("test_ascii_flush", false);
1704 }
1705
1706 static enum test_return test_ascii_flush_noreply(void)
1707 {
1708 return test_ascii_flush_impl("test_ascii_flush_noreply", true);
1709 }
1710
1711 static enum test_return test_ascii_concat_impl(const char *key,
1712 bool append,
1713 bool noreply)
1714 {
1715 const char *value;
1716
1717 if (append)
1718 value="hello";
1719 else
1720 value=" world";
1721
1722 execute(ascii_set_item(key, value));
1723
1724 if (append)
1725 value=" world";
1726 else
1727 value="hello";
1728
1729 char cmd[400];
1730 sprintf(cmd, "%s %s 0 0 %u%s\r\n%s\r\n",
1731 append ? "append" : "prepend",
1732 key, (unsigned int)strlen(value), noreply ? " noreply" : "",
1733 value);
1734 execute(send_string(cmd));
1735
1736 if (noreply)
1737 execute(test_ascii_version());
1738 else
1739 execute(receive_response("STORED\r\n"));
1740
1741 execute(ascii_get_item(key, "hello world", true));
1742
1743 sprintf(cmd, "%s %s_notfound 0 0 %u%s\r\n%s\r\n",
1744 append ? "append" : "prepend",
1745 key, (unsigned int)strlen(value), noreply ? " noreply" : "",
1746 value);
1747 execute(send_string(cmd));
1748
1749 if (noreply)
1750 execute(test_ascii_version());
1751 else
1752 execute(receive_response("NOT_STORED\r\n"));
1753
1754 return TEST_PASS;
1755 }
1756
1757 static enum test_return test_ascii_append(void)
1758 {
1759 return test_ascii_concat_impl("test_ascii_append", true, false);
1760 }
1761
1762 static enum test_return test_ascii_prepend(void)
1763 {
1764 return test_ascii_concat_impl("test_ascii_prepend", false, false);
1765 }
1766
1767 static enum test_return test_ascii_append_noreply(void)
1768 {
1769 return test_ascii_concat_impl("test_ascii_append_noreply", true, true);
1770 }
1771
1772 static enum test_return test_ascii_prepend_noreply(void)
1773 {
1774 return test_ascii_concat_impl("test_ascii_prepend_noreply", false, true);
1775 }
1776
1777 static enum test_return test_ascii_stat(void)
1778 {
1779 execute(send_string("stats noreply\r\n"));
1780 execute(receive_error_response());
1781 execute(send_string("stats\r\n"));
1782 char buffer[1024];
1783 do {
1784 execute(receive_line(buffer, sizeof(buffer)));
1785 } while (strcmp(buffer, "END\r\n") != 0);
1786
1787 return TEST_PASS_RECONNECT;
1788 }
1789
1790 typedef enum test_return(*TEST_FUNC)(void);
1791
1792 struct testcase
1793 {
1794 const char *description;
1795 TEST_FUNC function;
1796 };
1797
1798 struct testcase testcases[]= {
1799 { "ascii quit", test_ascii_quit },
1800 { "ascii version", test_ascii_version },
1801 { "ascii verbosity", test_ascii_verbosity },
1802 { "ascii set", test_ascii_set },
1803 { "ascii set noreply", test_ascii_set_noreply },
1804 { "ascii get", test_ascii_get },
1805 { "ascii gets", test_ascii_gets },
1806 { "ascii mget", test_ascii_mget },
1807 { "ascii flush", test_ascii_flush },
1808 { "ascii flush noreply", test_ascii_flush_noreply },
1809 { "ascii add", test_ascii_add },
1810 { "ascii add noreply", test_ascii_add_noreply },
1811 { "ascii replace", test_ascii_replace },
1812 { "ascii replace noreply", test_ascii_replace_noreply },
1813 { "ascii cas", test_ascii_cas },
1814 { "ascii cas noreply", test_ascii_cas_noreply },
1815 { "ascii delete", test_ascii_delete },
1816 { "ascii delete noreply", test_ascii_delete_noreply },
1817 { "ascii incr", test_ascii_incr },
1818 { "ascii incr noreply", test_ascii_incr_noreply },
1819 { "ascii decr", test_ascii_decr },
1820 { "ascii decr noreply", test_ascii_decr_noreply },
1821 { "ascii append", test_ascii_append },
1822 { "ascii append noreply", test_ascii_append_noreply },
1823 { "ascii prepend", test_ascii_prepend },
1824 { "ascii prepend noreply", test_ascii_prepend_noreply },
1825 { "ascii stat", test_ascii_stat },
1826 { "binary noop", test_binary_noop },
1827 { "binary quit", test_binary_quit },
1828 { "binary quitq", test_binary_quitq },
1829 { "binary set", test_binary_set },
1830 { "binary setq", test_binary_setq },
1831 { "binary flush", test_binary_flush },
1832 { "binary flushq", test_binary_flushq },
1833 { "binary add", test_binary_add },
1834 { "binary addq", test_binary_addq },
1835 { "binary replace", test_binary_replace },
1836 { "binary replaceq", test_binary_replaceq },
1837 { "binary delete", test_binary_delete },
1838 { "binary deleteq", test_binary_deleteq },
1839 { "binary get", test_binary_get },
1840 { "binary getq", test_binary_getq },
1841 { "binary getk", test_binary_getk },
1842 { "binary getkq", test_binary_getkq },
1843 { "binary incr", test_binary_incr },
1844 { "binary incrq", test_binary_incrq },
1845 { "binary decr", test_binary_decr },
1846 { "binary decrq", test_binary_decrq },
1847 { "binary version", test_binary_version },
1848 { "binary append", test_binary_append },
1849 { "binary appendq", test_binary_appendq },
1850 { "binary prepend", test_binary_prepend },
1851 { "binary prependq", test_binary_prependq },
1852 { "binary stat", test_binary_stat },
1853 { NULL, NULL}
1854 };
1855
1856 int main(int argc, char **argv)
1857 {
1858 static const char * const status_msg[]= {"[skip]", "[pass]", "[pass]", "[FAIL]"};
1859 int total= 0;
1860 int failed= 0;
1861 const char *hostname= "localhost";
1862 const char *port= "11211";
1863 int cmd;
1864
1865 while ((cmd= getopt(argc, argv, "t:vch:p:?")) != EOF)
1866 {
1867 switch (cmd) {
1868 case 't':
1869 timeout= atoi(optarg);
1870 if (timeout == 0)
1871 {
1872 fprintf(stderr, "Invalid timeout. Please specify a number for -t\n");
1873 return 1;
1874 }
1875 break;
1876 case 'v': verbose= true;
1877 break;
1878 case 'c': do_core= true;
1879 break;
1880 case 'h': hostname= optarg;
1881 break;
1882 case 'p': port= optarg;
1883 break;
1884 default:
1885 fprintf(stderr, "Usage: %s [-h hostname] [-p port] [-c] [-v] [-t n]\n"
1886 "\t-c\tGenerate coredump if a test fails\n"
1887 "\t-v\tVerbose test output (print out the assertion)\n"
1888 "\t-t n\tSet the timeout for io-operations to n seconds\n",
1889 argv[0]);
1890 return 1;
1891 }
1892 }
1893
1894 sock= connect_server(hostname, port);
1895 if (sock == -1)
1896 {
1897 fprintf(stderr, "Failed to connect to <%s:%s>: %s\n",
1898 hostname, port, strerror(errno));
1899 return 1;
1900 }
1901
1902 for (int ii= 0; testcases[ii].description != NULL; ++ii)
1903 {
1904 ++total;
1905 fprintf(stdout, "%-40s", testcases[ii].description);
1906 fflush(stdout);
1907
1908 bool reconnect= false;
1909 enum test_return ret= testcases[ii].function();
1910 if (ret == TEST_FAIL)
1911 {
1912 reconnect= true;
1913 ++failed;
1914 if (verbose)
1915 fprintf(stderr, "\n");
1916 }
1917 else if (ret == TEST_PASS_RECONNECT)
1918 reconnect= true;
1919
1920 fprintf(stderr, "%s\n", status_msg[ret]);
1921 if (reconnect)
1922 {
1923 (void) close(sock);
1924 if ((sock=connect_server(hostname, port)) == -1)
1925 {
1926 fprintf(stderr, "Failed to connect to <%s:%s>: %s\n",
1927 hostname, port, strerror(errno));
1928 fprintf(stderr, "%d of %d tests failed\n", failed, total);
1929 return 1;
1930 }
1931 }
1932 }
1933
1934 (void) close(sock);
1935 if (failed == 0)
1936 fprintf(stdout, "All tests passed\n");
1937 else
1938 fprintf(stderr, "%d of %d tests failed\n", failed, total);
1939
1940 return (failed == 0) ? 0 : 1;
1941 }