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