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