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