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