memcapable: fix quit test with v1.6+
[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 if (!v16x_or_greater) {
1266 /* Verify that quit handles unknown options */
1267 execute(send_string("quit foo bar\r\n"));
1268 execute(receive_error_response());
1269
1270 /* quit doesn't support noreply */
1271 execute(send_string("quit noreply\r\n"));
1272 execute(receive_error_response());
1273 }
1274
1275 /* Verify that quit works */
1276 execute(send_string("quit\r\n"));
1277
1278 /* Socket should be closed now, read should return EXIT_SUCCESS */
1279 char buffer[80];
1280 verify(timeout_io_op(sock, POLLIN, buffer, sizeof(buffer)) == 0);
1281 return TEST_PASS_RECONNECT;
1282
1283 }
1284
1285 static enum test_return test_ascii_version(void)
1286 {
1287 /* Verify that version works */
1288 execute(send_string("version\r\n"));
1289 char buffer[256];
1290 execute(receive_line(buffer, sizeof(buffer)));
1291 verify(strncmp(buffer, "VERSION ", 8) == 0);
1292
1293 char *version = &buffer[sizeof("VERSION") + 2];
1294 if (version[0] >= '1' || (version[0] == '1' && version[2] >= '6')) {
1295 v16x_or_greater = true;
1296 }
1297
1298 /* Verify that version command handles unknown options */
1299 execute(send_string("version foo bar\r\n"));
1300 if (v16x_or_greater) {
1301 execute(receive_line(buffer, sizeof(buffer)));
1302 verify(strncmp(buffer, "VERSION ", 8) == 0);
1303 } else {
1304 execute(receive_error_response());
1305 }
1306 /* version doesn't support noreply */
1307 execute(send_string("version noreply\r\n"));
1308 if (v16x_or_greater) {
1309 execute(receive_line(buffer, sizeof(buffer)));
1310 verify(strncmp(buffer, "VERSION ", 8) == 0);
1311 } else {
1312 execute(receive_error_response());
1313 }
1314 return TEST_PASS;
1315 }
1316
1317 static enum test_return test_ascii_verbosity(void)
1318 {
1319 /* This command does not adhere to the spec! */
1320 execute(send_string("verbosity foo bar my\r\n"));
1321 execute(receive_error_response());
1322
1323 execute(send_string("verbosity noreply\r\n"));
1324 execute(test_ascii_version());
1325
1326 execute(send_string("verbosity 0 noreply\r\n"));
1327 execute(test_ascii_version());
1328
1329 execute(send_string("verbosity\r\n"));
1330 execute(receive_error_response());
1331
1332 execute(send_string("verbosity 1\r\n"));
1333 execute(receive_response("OK\r\n"));
1334
1335 execute(send_string("verbosity 0\r\n"));
1336 execute(receive_response("OK\r\n"));
1337
1338 return TEST_PASS;
1339 }
1340
1341
1342
1343 static enum test_return test_ascii_set_impl(const char* key, bool noreply)
1344 {
1345 /* @todo add tests for bogus format! */
1346 char buffer[1024];
1347 snprintf(buffer, sizeof(buffer), "set %s 0 0 5%s\r\nvalue\r\n", key, noreply ? " noreply" : "");
1348 execute(send_string(buffer));
1349
1350 if (!noreply)
1351 {
1352 execute(receive_response("STORED\r\n"));
1353 }
1354
1355 return test_ascii_version();
1356 }
1357
1358 static enum test_return test_ascii_set(void)
1359 {
1360 return test_ascii_set_impl("test_ascii_set", false);
1361 }
1362
1363 static enum test_return test_ascii_set_noreply(void)
1364 {
1365 return test_ascii_set_impl("test_ascii_set_noreply", true);
1366 }
1367
1368 static enum test_return test_ascii_add_impl(const char* key, bool noreply)
1369 {
1370 /* @todo add tests for bogus format! */
1371 char buffer[1024];
1372 snprintf(buffer, sizeof(buffer), "add %s 0 0 5%s\r\nvalue\r\n", key, noreply ? " noreply" : "");
1373 execute(send_string(buffer));
1374
1375 if (!noreply)
1376 {
1377 execute(receive_response("STORED\r\n"));
1378 }
1379
1380 execute(send_string(buffer));
1381
1382 if (!noreply)
1383 {
1384 execute(receive_response("NOT_STORED\r\n"));
1385 }
1386
1387 return test_ascii_version();
1388 }
1389
1390 static enum test_return test_ascii_add(void)
1391 {
1392 return test_ascii_add_impl("test_ascii_add", false);
1393 }
1394
1395 static enum test_return test_ascii_add_noreply(void)
1396 {
1397 return test_ascii_add_impl("test_ascii_add_noreply", true);
1398 }
1399
1400 static enum test_return ascii_get_unknown_value(char **key, char **value, ssize_t *ndata)
1401 {
1402 char buffer[1024];
1403
1404 execute(receive_line(buffer, sizeof(buffer)));
1405 verify(strncmp(buffer, "VALUE ", 6) == 0);
1406 char *end= strchr(buffer + 6, ' ');
1407 verify(end != NULL);
1408 if (end)
1409 {
1410 *end= '\0';
1411 }
1412 *key= strdup(buffer + 6);
1413 verify(*key != NULL);
1414 char *ptr= end + 1;
1415
1416 errno= 0;
1417 unsigned long val= strtoul(ptr, &end, 10); /* flags */
1418 verify(errno == 0);
1419 verify(ptr != end);
1420 verify(val == 0);
1421 verify(end != NULL);
1422 errno= 0;
1423 *ndata = (ssize_t)strtoul(end, &end, 10); /* size */
1424 verify(errno == 0);
1425 verify(ptr != end);
1426 verify(end != NULL);
1427 while (end and *end != '\n' and isspace(*end))
1428 ++end;
1429 verify(end and *end == '\n');
1430
1431 *value= static_cast<char*>(malloc((size_t)*ndata));
1432 verify(*value != NULL);
1433
1434 execute(retry_read(*value, (size_t)*ndata));
1435
1436 execute(retry_read(buffer, 2));
1437 verify(memcmp(buffer, "\r\n", 2) == 0);
1438
1439 return TEST_PASS;
1440 }
1441
1442 static enum test_return ascii_get_value(const char *key, const char *value)
1443 {
1444
1445 char buffer[1024];
1446 size_t datasize= strlen(value);
1447
1448 verify(datasize < sizeof(buffer));
1449 execute(receive_line(buffer, sizeof(buffer)));
1450 verify(strncmp(buffer, "VALUE ", 6) == 0);
1451 verify(strncmp(buffer + 6, key, strlen(key)) == 0);
1452 char *ptr= buffer + 6 + strlen(key) + 1;
1453 char *end;
1454
1455 errno= 0;
1456 unsigned long val= strtoul(ptr, &end, 10); /* flags */
1457 verify(errno == 0);
1458 verify(ptr != end);
1459 verify(val == 0);
1460 verify(end != NULL);
1461
1462 errno= 0;
1463 val= strtoul(end, &end, 10); /* size */
1464 verify(errno == 0);
1465 verify(ptr != end);
1466 verify(val == datasize);
1467 verify(end != NULL);
1468 while (end and *end != '\n' and isspace(*end))
1469 {
1470 ++end;
1471 }
1472 verify(end and *end == '\n');
1473
1474 execute(retry_read(buffer, datasize));
1475 verify(memcmp(buffer, value, datasize) == 0);
1476
1477 execute(retry_read(buffer, 2));
1478 verify(memcmp(buffer, "\r\n", 2) == 0);
1479
1480 return TEST_PASS;
1481 }
1482
1483 static enum test_return ascii_get_item(const char *key, const char *value,
1484 bool exist)
1485 {
1486 char buffer[1024];
1487 size_t datasize= 0;
1488 if (value != NULL)
1489 {
1490 datasize= strlen(value);
1491 }
1492
1493 verify(datasize < sizeof(buffer));
1494 snprintf(buffer, sizeof(buffer), "get %s\r\n", key);
1495 execute(send_string(buffer));
1496
1497 if (exist)
1498 {
1499 execute(ascii_get_value(key, value));
1500 }
1501
1502 execute(retry_read(buffer, 5));
1503 verify(memcmp(buffer, "END\r\n", 5) == 0);
1504
1505 return TEST_PASS;
1506 }
1507
1508 static enum test_return ascii_gets_value(const char *key, const char *value,
1509 unsigned long *cas)
1510 {
1511
1512 char buffer[1024];
1513 size_t datasize= strlen(value);
1514
1515 verify(datasize < sizeof(buffer));
1516 execute(receive_line(buffer, sizeof(buffer)));
1517 verify(strncmp(buffer, "VALUE ", 6) == 0);
1518 verify(strncmp(buffer + 6, key, strlen(key)) == 0);
1519 char *ptr= buffer + 6 + strlen(key) + 1;
1520 char *end;
1521
1522 errno= 0;
1523 unsigned long val= strtoul(ptr, &end, 10); /* flags */
1524 verify(errno == 0);
1525 verify(ptr != end);
1526 verify(val == 0);
1527 verify(end != NULL);
1528
1529 errno= 0;
1530 val= strtoul(end, &end, 10); /* size */
1531 verify(errno == 0);
1532 verify(ptr != end);
1533 verify(val == datasize);
1534 verify(end != NULL);
1535
1536 errno= 0;
1537 *cas= strtoul(end, &end, 10); /* cas */
1538 verify(errno == 0);
1539 verify(ptr != end);
1540 verify(val == datasize);
1541 verify(end != NULL);
1542
1543 while (end and *end != '\n' and isspace(*end))
1544 {
1545 ++end;
1546 }
1547 verify(end and *end == '\n');
1548
1549 execute(retry_read(buffer, datasize));
1550 verify(memcmp(buffer, value, datasize) == 0);
1551
1552 execute(retry_read(buffer, 2));
1553 verify(memcmp(buffer, "\r\n", 2) == 0);
1554
1555 return TEST_PASS;
1556 }
1557
1558 static enum test_return ascii_gets_item(const char *key, const char *value,
1559 bool exist, unsigned long *cas)
1560 {
1561 char buffer[1024];
1562 size_t datasize= 0;
1563 if (value != NULL)
1564 {
1565 datasize= strlen(value);
1566 }
1567
1568 verify(datasize < sizeof(buffer));
1569 snprintf(buffer, sizeof(buffer), "gets %s\r\n", key);
1570 execute(send_string(buffer));
1571
1572 if (exist)
1573 execute(ascii_gets_value(key, value, cas));
1574
1575 execute(retry_read(buffer, 5));
1576 verify(memcmp(buffer, "END\r\n", 5) == 0);
1577
1578 return TEST_PASS;
1579 }
1580
1581 static enum test_return ascii_set_item(const char *key, const char *value)
1582 {
1583 char buffer[300];
1584 size_t len= strlen(value);
1585 snprintf(buffer, sizeof(buffer), "set %s 0 0 %u\r\n", key, (unsigned int)len);
1586 execute(send_string(buffer));
1587 execute(retry_write(value, len));
1588 execute(send_string("\r\n"));
1589 execute(receive_response("STORED\r\n"));
1590 return TEST_PASS;
1591 }
1592
1593 static enum test_return test_ascii_replace_impl(const char* key, bool noreply)
1594 {
1595 char buffer[1024];
1596 snprintf(buffer, sizeof(buffer), "replace %s 0 0 5%s\r\nvalue\r\n", key, noreply ? " noreply" : "");
1597 execute(send_string(buffer));
1598
1599 if (noreply)
1600 {
1601 execute(test_ascii_version());
1602 }
1603 else
1604 {
1605 execute(receive_response("NOT_STORED\r\n"));
1606 }
1607
1608 execute(ascii_set_item(key, "value"));
1609 execute(ascii_get_item(key, "value", true));
1610
1611
1612 execute(send_string(buffer));
1613
1614 if (noreply)
1615 execute(test_ascii_version());
1616 else
1617 execute(receive_response("STORED\r\n"));
1618
1619 return test_ascii_version();
1620 }
1621
1622 static enum test_return test_ascii_replace(void)
1623 {
1624 return test_ascii_replace_impl("test_ascii_replace", false);
1625 }
1626
1627 static enum test_return test_ascii_replace_noreply(void)
1628 {
1629 return test_ascii_replace_impl("test_ascii_replace_noreply", true);
1630 }
1631
1632 static enum test_return test_ascii_cas_impl(const char* key, bool noreply)
1633 {
1634 char buffer[1024];
1635 unsigned long cas;
1636
1637 execute(ascii_set_item(key, "value"));
1638 execute(ascii_gets_item(key, "value", true, &cas));
1639
1640 snprintf(buffer, sizeof(buffer), "cas %s 0 0 6 %lu%s\r\nvalue2\r\n", key, cas, noreply ? " noreply" : "");
1641 execute(send_string(buffer));
1642
1643 if (noreply)
1644 {
1645 execute(test_ascii_version());
1646 }
1647 else
1648 {
1649 execute(receive_response("STORED\r\n"));
1650 }
1651
1652 /* reexecute the same command should fail due to illegal cas */
1653 execute(send_string(buffer));
1654
1655 if (noreply)
1656 {
1657 execute(test_ascii_version());
1658 }
1659 else
1660 {
1661 execute(receive_response("EXISTS\r\n"));
1662 }
1663
1664 return test_ascii_version();
1665 }
1666
1667 static enum test_return test_ascii_cas(void)
1668 {
1669 return test_ascii_cas_impl("test_ascii_cas", false);
1670 }
1671
1672 static enum test_return test_ascii_cas_noreply(void)
1673 {
1674 return test_ascii_cas_impl("test_ascii_cas_noreply", true);
1675 }
1676
1677 static enum test_return test_ascii_delete_impl(const char *key, bool noreply)
1678 {
1679 execute(ascii_set_item(key, "value"));
1680
1681 execute(send_string("delete\r\n"));
1682 execute(receive_error_response());
1683 /* BUG: the server accepts delete a b */
1684 execute(send_string("delete a b c d e\r\n"));
1685 execute(receive_error_response());
1686
1687 char buffer[1024];
1688 snprintf(buffer, sizeof(buffer), "delete %s%s\r\n", key, noreply ? " noreply" : "");
1689 execute(send_string(buffer));
1690
1691 if (noreply)
1692 execute(test_ascii_version());
1693 else
1694 execute(receive_response("DELETED\r\n"));
1695
1696 execute(ascii_get_item(key, "value", false));
1697 execute(send_string(buffer));
1698 if (noreply)
1699 execute(test_ascii_version());
1700 else
1701 execute(receive_response("NOT_FOUND\r\n"));
1702
1703 return TEST_PASS;
1704 }
1705
1706 static enum test_return test_ascii_delete(void)
1707 {
1708 return test_ascii_delete_impl("test_ascii_delete", false);
1709 }
1710
1711 static enum test_return test_ascii_delete_noreply(void)
1712 {
1713 return test_ascii_delete_impl("test_ascii_delete_noreply", true);
1714 }
1715
1716 static enum test_return test_ascii_get(void)
1717 {
1718 execute(ascii_set_item("test_ascii_get", "value"));
1719
1720 execute(send_string("get\r\n"));
1721 execute(receive_error_response());
1722 execute(ascii_get_item("test_ascii_get", "value", true));
1723 execute(ascii_get_item("test_ascii_get_notfound", "value", false));
1724
1725 return TEST_PASS;
1726 }
1727
1728 static enum test_return test_ascii_gets(void)
1729 {
1730 execute(ascii_set_item("test_ascii_gets", "value"));
1731
1732 execute(send_string("gets\r\n"));
1733 execute(receive_error_response());
1734 unsigned long cas;
1735 execute(ascii_gets_item("test_ascii_gets", "value", true, &cas));
1736 execute(ascii_gets_item("test_ascii_gets_notfound", "value", false, &cas));
1737
1738 return TEST_PASS;
1739 }
1740
1741 static enum test_return test_ascii_mget(void)
1742 {
1743 const uint32_t nkeys= 5;
1744 const char * const keys[]= {
1745 "test_ascii_mget1",
1746 "test_ascii_mget2",
1747 /* test_ascii_mget_3 does not exist :) */
1748 "test_ascii_mget4",
1749 "test_ascii_mget5",
1750 "test_ascii_mget6"
1751 };
1752
1753 for (uint32_t x= 0; x < nkeys; ++x)
1754 {
1755 execute(ascii_set_item(keys[x], "value"));
1756 }
1757
1758 /* Ask for a key that doesn't exist as well */
1759 execute(send_string("get test_ascii_mget1 test_ascii_mget2 test_ascii_mget3 "
1760 "test_ascii_mget4 test_ascii_mget5 "
1761 "test_ascii_mget6\r\n"));
1762
1763 std::vector<char *> returned;
1764 returned.resize(nkeys);
1765
1766 for (uint32_t x= 0; x < nkeys; ++x)
1767 {
1768 ssize_t nbytes = 0;
1769 char *v= NULL;
1770 execute(ascii_get_unknown_value(&returned[x], &v, &nbytes));
1771 verify(nbytes == 5);
1772 verify(memcmp(v, "value", 5) == 0);
1773 free(v);
1774 }
1775
1776 char buffer[5];
1777 execute(retry_read(buffer, 5));
1778 verify(memcmp(buffer, "END\r\n", 5) == 0);
1779
1780 /* verify that we got all the keys we expected */
1781 for (uint32_t x= 0; x < nkeys; ++x)
1782 {
1783 bool found= false;
1784 for (uint32_t y= 0; y < nkeys; ++y)
1785 {
1786 if (strcmp(keys[x], returned[y]) == 0)
1787 {
1788 found = true;
1789 break;
1790 }
1791 }
1792 verify(found);
1793 }
1794
1795 for (uint32_t x= 0; x < nkeys; ++x)
1796 {
1797 free(returned[x]);
1798 }
1799
1800 return TEST_PASS;
1801 }
1802
1803 static enum test_return test_ascii_incr_impl(const char* key, bool noreply)
1804 {
1805 char cmd[300];
1806 snprintf(cmd, sizeof(cmd), "incr %s 1%s\r\n", key, noreply ? " noreply" : "");
1807
1808 execute(ascii_set_item(key, "0"));
1809 for (int x= 1; x < 11; ++x)
1810 {
1811 execute(send_string(cmd));
1812
1813 if (noreply)
1814 execute(test_ascii_version());
1815 else
1816 {
1817 char buffer[80];
1818 execute(receive_line(buffer, sizeof(buffer)));
1819 int val= atoi(buffer);
1820 verify(val == x);
1821 }
1822 }
1823
1824 execute(ascii_get_item(key, "10", true));
1825
1826 return TEST_PASS;
1827 }
1828
1829 static enum test_return test_ascii_incr(void)
1830 {
1831 return test_ascii_incr_impl("test_ascii_incr", false);
1832 }
1833
1834 static enum test_return test_ascii_incr_noreply(void)
1835 {
1836 return test_ascii_incr_impl("test_ascii_incr_noreply", true);
1837 }
1838
1839 static enum test_return test_ascii_decr_impl(const char* key, bool noreply)
1840 {
1841 char cmd[300];
1842 snprintf(cmd, sizeof(cmd), "decr %s 1%s\r\n", key, noreply ? " noreply" : "");
1843
1844 execute(ascii_set_item(key, "9"));
1845 for (int x= 8; x > -1; --x)
1846 {
1847 execute(send_string(cmd));
1848
1849 if (noreply)
1850 {
1851 execute(test_ascii_version());
1852 }
1853 else
1854 {
1855 char buffer[80];
1856 execute(receive_line(buffer, sizeof(buffer)));
1857 int val= atoi(buffer);
1858 verify(val == x);
1859 }
1860 }
1861
1862 execute(ascii_get_item(key, "0", true));
1863
1864 /* verify that it doesn't wrap */
1865 execute(send_string(cmd));
1866 if (noreply)
1867 {
1868 execute(test_ascii_version());
1869 }
1870 else
1871 {
1872 char buffer[80];
1873 execute(receive_line(buffer, sizeof(buffer)));
1874 }
1875 execute(ascii_get_item(key, "0", true));
1876
1877 return TEST_PASS;
1878 }
1879
1880 static enum test_return test_ascii_decr(void)
1881 {
1882 return test_ascii_decr_impl("test_ascii_decr", false);
1883 }
1884
1885 static enum test_return test_ascii_decr_noreply(void)
1886 {
1887 return test_ascii_decr_impl("test_ascii_decr_noreply", true);
1888 }
1889
1890
1891 static enum test_return test_ascii_flush_impl(const char *key, bool noreply)
1892 {
1893 #if 0
1894 /* Verify that the flush_all command handles unknown options */
1895 /* Bug in the current memcached server! */
1896 execute(send_string("flush_all foo bar\r\n"));
1897 execute(receive_error_response());
1898 #endif
1899
1900 execute(ascii_set_item(key, key));
1901 execute(ascii_get_item(key, key, true));
1902
1903 if (noreply)
1904 {
1905 execute(send_string("flush_all noreply\r\n"));
1906 execute(test_ascii_version());
1907 }
1908 else
1909 {
1910 execute(send_string("flush_all\r\n"));
1911 execute(receive_response("OK\r\n"));
1912 }
1913
1914 execute(ascii_get_item(key, key, false));
1915
1916 return TEST_PASS;
1917 }
1918
1919 static enum test_return test_ascii_flush(void)
1920 {
1921 return test_ascii_flush_impl("test_ascii_flush", false);
1922 }
1923
1924 static enum test_return test_ascii_flush_noreply(void)
1925 {
1926 return test_ascii_flush_impl("test_ascii_flush_noreply", true);
1927 }
1928
1929 static enum test_return test_ascii_concat_impl(const char *key,
1930 bool append,
1931 bool noreply)
1932 {
1933 const char *value;
1934
1935 if (append)
1936 value="hello";
1937 else
1938 value=" world";
1939
1940 execute(ascii_set_item(key, value));
1941
1942 if (append)
1943 {
1944 value=" world";
1945 }
1946 else
1947 {
1948 value="hello";
1949 }
1950
1951 char cmd[400];
1952 snprintf(cmd, sizeof(cmd), "%s %s 0 0 %u%s\r\n%s\r\n",
1953 append ? "append" : "prepend",
1954 key, (unsigned int)strlen(value), noreply ? " noreply" : "",
1955 value);
1956 execute(send_string(cmd));
1957
1958 if (noreply)
1959 {
1960 execute(test_ascii_version());
1961 }
1962 else
1963 {
1964 execute(receive_response("STORED\r\n"));
1965 }
1966
1967 execute(ascii_get_item(key, "hello world", true));
1968
1969 snprintf(cmd, sizeof(cmd), "%s %s_notfound 0 0 %u%s\r\n%s\r\n",
1970 append ? "append" : "prepend",
1971 key, (unsigned int)strlen(value), noreply ? " noreply" : "",
1972 value);
1973 execute(send_string(cmd));
1974
1975 if (noreply)
1976 {
1977 execute(test_ascii_version());
1978 }
1979 else
1980 {
1981 execute(receive_response("NOT_STORED\r\n"));
1982 }
1983
1984 return TEST_PASS;
1985 }
1986
1987 static enum test_return test_ascii_append(void)
1988 {
1989 return test_ascii_concat_impl("test_ascii_append", true, false);
1990 }
1991
1992 static enum test_return test_ascii_prepend(void)
1993 {
1994 return test_ascii_concat_impl("test_ascii_prepend", false, false);
1995 }
1996
1997 static enum test_return test_ascii_append_noreply(void)
1998 {
1999 return test_ascii_concat_impl("test_ascii_append_noreply", true, true);
2000 }
2001
2002 static enum test_return test_ascii_prepend_noreply(void)
2003 {
2004 return test_ascii_concat_impl("test_ascii_prepend_noreply", false, true);
2005 }
2006
2007 static enum test_return test_ascii_stat(void)
2008 {
2009 execute(send_string("stats noreply\r\n"));
2010 execute(receive_error_response());
2011 execute(send_string("stats\r\n"));
2012 char buffer[1024];
2013 do {
2014 execute(receive_line(buffer, sizeof(buffer)));
2015 } while (strcmp(buffer, "END\r\n") != 0);
2016
2017 return TEST_PASS_RECONNECT;
2018 }
2019
2020 typedef enum test_return(*TEST_FUNC)(void);
2021
2022 struct testcase
2023 {
2024 const char *description;
2025 TEST_FUNC function;
2026 };
2027
2028 struct testcase testcases[]= {
2029 { "ascii version", test_ascii_version },
2030 { "ascii quit", test_ascii_quit },
2031 { "ascii verbosity", test_ascii_verbosity },
2032 { "ascii set", test_ascii_set },
2033 { "ascii set noreply", test_ascii_set_noreply },
2034 { "ascii get", test_ascii_get },
2035 { "ascii gets", test_ascii_gets },
2036 { "ascii mget", test_ascii_mget },
2037 { "ascii flush", test_ascii_flush },
2038 { "ascii flush noreply", test_ascii_flush_noreply },
2039 { "ascii add", test_ascii_add },
2040 { "ascii add noreply", test_ascii_add_noreply },
2041 { "ascii replace", test_ascii_replace },
2042 { "ascii replace noreply", test_ascii_replace_noreply },
2043 { "ascii cas", test_ascii_cas },
2044 { "ascii cas noreply", test_ascii_cas_noreply },
2045 { "ascii delete", test_ascii_delete },
2046 { "ascii delete noreply", test_ascii_delete_noreply },
2047 { "ascii incr", test_ascii_incr },
2048 { "ascii incr noreply", test_ascii_incr_noreply },
2049 { "ascii decr", test_ascii_decr },
2050 { "ascii decr noreply", test_ascii_decr_noreply },
2051 { "ascii append", test_ascii_append },
2052 { "ascii append noreply", test_ascii_append_noreply },
2053 { "ascii prepend", test_ascii_prepend },
2054 { "ascii prepend noreply", test_ascii_prepend_noreply },
2055 { "ascii stat", test_ascii_stat },
2056 { "binary noop", test_binary_noop },
2057 { "binary quit", test_binary_quit },
2058 { "binary quitq", test_binary_quitq },
2059 { "binary set", test_binary_set },
2060 { "binary setq", test_binary_setq },
2061 { "binary flush", test_binary_flush },
2062 { "binary flushq", test_binary_flushq },
2063 { "binary add", test_binary_add },
2064 { "binary addq", test_binary_addq },
2065 { "binary replace", test_binary_replace },
2066 { "binary replaceq", test_binary_replaceq },
2067 { "binary delete", test_binary_delete },
2068 { "binary deleteq", test_binary_deleteq },
2069 { "binary get", test_binary_get },
2070 { "binary getq", test_binary_getq },
2071 { "binary getk", test_binary_getk },
2072 { "binary getkq", test_binary_getkq },
2073 { "binary incr", test_binary_incr },
2074 { "binary incrq", test_binary_incrq },
2075 { "binary decr", test_binary_decr },
2076 { "binary decrq", test_binary_decrq },
2077 { "binary version", test_binary_version },
2078 { "binary append", test_binary_append },
2079 { "binary appendq", test_binary_appendq },
2080 { "binary prepend", test_binary_prepend },
2081 { "binary prependq", test_binary_prependq },
2082 { "binary stat", test_binary_stat },
2083 { NULL, NULL}
2084 };
2085
2086 const int ascii_tests = 1;
2087 const int binary_tests = 2;
2088
2089 struct test_type_st
2090 {
2091 bool ascii;
2092 bool binary;
2093 };
2094
2095 int main(int argc, char **argv)
2096 {
2097 static const char * const status_msg[]= {"[skip]", "[pass]", "[pass]", "[FAIL]"};
2098 struct test_type_st tests= { true, true };
2099 int total= 0;
2100 int failed= 0;
2101 const char *hostname= NULL;
2102 const char *port= MEMCACHED_DEFAULT_PORT_STRING;
2103 int cmd;
2104 bool prompt= false;
2105 const char *testname= NULL;
2106
2107
2108
2109 while ((cmd= getopt(argc, argv, "qt:vch:p:PT:?ab")) != EOF)
2110 {
2111 switch (cmd) {
2112 case 'a':
2113 tests.ascii= true;
2114 tests.binary= false;
2115 break;
2116
2117 case 'b':
2118 tests.ascii= false;
2119 tests.binary= true;
2120 break;
2121
2122 case 't':
2123 timeout= atoi(optarg);
2124 if (timeout == 0)
2125 {
2126 fprintf(stderr, "Invalid timeout. Please specify a number for -t\n");
2127 return EXIT_FAILURE;
2128 }
2129 break;
2130
2131 case 'v': verbose= true;
2132 break;
2133
2134 case 'c': do_core= true;
2135 break;
2136
2137 case 'h': hostname= optarg;
2138 break;
2139
2140 case 'p': port= optarg;
2141 break;
2142
2143 case 'q':
2144 close_stdio();
2145 break;
2146
2147 case 'P': prompt= true;
2148 break;
2149
2150 case 'T': testname= optarg;
2151 break;
2152
2153 default:
2154 fprintf(stderr, "Usage: %s [-h hostname] [-p port] [-c] [-v] [-t n] [-P] [-T testname]'\n"
2155 "\t-c\tGenerate coredump if a test fails\n"
2156 "\t-v\tVerbose test output (print out the assertion)\n"
2157 "\t-t n\tSet the timeout for io-operations to n seconds\n"
2158 "\t-P\tPrompt the user before starting a test.\n"
2159 "\t\t\t\"skip\" will skip the test\n"
2160 "\t\t\t\"quit\" will terminate memcapable\n"
2161 "\t\t\tEverything else will start the test\n"
2162 "\t-T n\tJust run the test named n\n"
2163 "\t-a\tOnly test the ascii protocol\n"
2164 "\t-b\tOnly test the binary protocol\n",
2165 argv[0]);
2166 return EXIT_SUCCESS;
2167 }
2168 }
2169
2170 if (!hostname)
2171 {
2172 fprintf(stderr, "No hostname was provided.\n");
2173 return EXIT_FAILURE;
2174 }
2175
2176 initialize_sockets();
2177 sock= connect_server(hostname, port);
2178 if (sock == INVALID_SOCKET)
2179 {
2180 fprintf(stderr, "Failed to connect to <%s:%s>: %s\n",
2181 hostname?:"(null)", port?:"(null)", strerror(get_socket_errno()));
2182 return EXIT_FAILURE;
2183 }
2184
2185 for (int ii= 0; testcases[ii].description != NULL; ++ii)
2186 {
2187 if (testname != NULL && strcmp(testcases[ii].description, testname) != 0)
2188 {
2189 continue;
2190 }
2191
2192 if ((testcases[ii].description[0] == 'a' && (tests.ascii) == 0) ||
2193 (testcases[ii].description[0] == 'b' && (tests.binary) == 0))
2194 {
2195 continue;
2196 }
2197 ++total;
2198 fprintf(stdout, "%-40s", testcases[ii].description);
2199 fflush(stdout);
2200
2201 if (prompt)
2202 {
2203 fprintf(stdout, "\nPress <return> when you are ready? ");
2204 char buffer[80] = {0};
2205 if (fgets(buffer, sizeof(buffer), stdin) != NULL) {
2206 if (strncmp(buffer, "skip", 4) == 0)
2207 {
2208 fprintf(stdout, "%-40s%s\n", testcases[ii].description,
2209 status_msg[TEST_SKIP]);
2210 fflush(stdout);
2211 continue;
2212 }
2213 if (strncmp(buffer, "quit", 4) == 0)
2214 {
2215 exit(EXIT_SUCCESS);
2216 }
2217 }
2218
2219 fprintf(stdout, "%-40s", testcases[ii].description);
2220 fflush(stdout);
2221 }
2222
2223 bool reconnect= false;
2224 enum test_return ret= testcases[ii].function();
2225 if (ret == TEST_FAIL)
2226 {
2227 reconnect= true;
2228 ++failed;
2229 if (verbose)
2230 {
2231 fprintf(stderr, "\n");
2232 }
2233 }
2234 else if (ret == TEST_PASS_RECONNECT)
2235 {
2236 reconnect= true;
2237 }
2238
2239 if (ret == TEST_FAIL)
2240 {
2241 fprintf(stderr, "%s\n", status_msg[ret]);
2242 }
2243 else
2244 {
2245 fprintf(stdout, "%s\n", status_msg[ret]);
2246 }
2247
2248 if (reconnect)
2249 {
2250 closesocket(sock);
2251 if ((sock= connect_server(hostname, port)) == INVALID_SOCKET)
2252 {
2253 fprintf(stderr, "Failed to connect to <%s:%s>: %s\n", hostname?:"(null)", port?:"(null)", strerror(get_socket_errno()));
2254 fprintf(stderr, "%d of %d tests failed\n", failed, total);
2255 return EXIT_FAILURE;
2256 }
2257 }
2258 }
2259
2260 closesocket(sock);
2261 if (failed == 0)
2262 {
2263 fprintf(stdout, "All tests passed\n");
2264 }
2265 else
2266 {
2267 fprintf(stderr, "%d of %d tests failed\n", failed, total);
2268 }
2269
2270 return (failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
2271 }