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