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