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