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