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