b38cf311001cbae26ebe2f12a272fe507828181f
[awesomized/libmemcached] / clients / memcapable.cc
1 /* LibMemcached
2 * Copyright (C) 2011-2012 Data Differential, http://datadifferential.com/
3 * Copyright (C) 2006-2009 Brian Aker
4 * All rights reserved.
5 *
6 * Use and distribution licensed under the BSD license. See
7 * the COPYING file in the parent directory for full text.
8 *
9 * Summary:
10 *
11 */
12
13 /* -*- Mode: C; tab-width: 2; c-basic-offset: 2; indent-tabs-mode: nil -*- */
14 #undef NDEBUG
15
16 #include <mem_config.h>
17
18 #ifdef HAVE_POLL_H
19 #include <poll.h>
20 #else
21 #include "poll/poll.h"
22 #endif
23
24 #include <cassert>
25 #include <cerrno>
26 #include <cstdio>
27 #include <cstdlib>
28 #include <cstring>
29 #include <ctype.h>
30 #include <fcntl.h>
31 #include <inttypes.h>
32 #include <pthread.h>
33 #include <signal.h>
34 #include <sys/types.h>
35 #include <unistd.h>
36
37 #include <libmemcached-1.0/memcached.h>
38
39 #include "libmemcached/socket.hpp"
40 #include "libmemcached/memcached/protocol_binary.h"
41 #include "libmemcached/byteorder.h"
42 #include "clients/utilities.h"
43
44 #include <vector>
45
46 #ifdef linux
47 /* /usr/include/netinet/in.h defines macros from ntohs() to _bswap_nn to
48 * optimize the conversion functions, but the prototypes generate warnings
49 * from gcc. The conversion methods isn't the bottleneck for my app, so
50 * just remove the warnings by undef'ing the optimization ..
51 */
52 #undef ntohs
53 #undef ntohl
54 #endif
55
56 /* Should we generate coredumps when we enounter an error (-c) */
57 static bool do_core= false;
58 /* connection to the server */
59 static memcached_socket_t sock;
60 /* Should the output from test failures be verbose or quiet? */
61 static bool verbose= false;
62
63 /* The number of seconds to wait for an IO-operation */
64 static int timeout= 2;
65
66 /*
67 * Instead of having to cast between the different datatypes we create
68 * a union of all of the different types of pacages we want to send.
69 * A lot of the different commands use the same packet layout, so I'll
70 * just define the different types I need. The typedefs only contain
71 * the header of the message, so we need some space for keys and body
72 * To avoid to have to do multiple writes, lets add a chunk of memory
73 * to use. 1k should be more than enough for header, key and body.
74 */
75 typedef union
76 {
77 protocol_binary_request_no_extras plain;
78 protocol_binary_request_flush flush;
79 protocol_binary_request_incr incr;
80 protocol_binary_request_set set;
81 char bytes[1024];
82 } command;
83
84 typedef union
85 {
86 protocol_binary_response_no_extras plain;
87 protocol_binary_response_incr incr;
88 protocol_binary_response_decr decr;
89 char bytes[1024];
90 } response;
91
92 enum test_return
93 {
94 TEST_SKIP, TEST_PASS, TEST_PASS_RECONNECT, TEST_FAIL
95 };
96
97 /**
98 * Try to get an addrinfo struct for a given port on a given host
99 */
100 static struct addrinfo *lookuphost(const char *hostname, const char *port)
101 {
102 struct addrinfo *ai= 0;
103 struct addrinfo hints;
104 memset(&hints, 0, sizeof(struct addrinfo));
105 hints.ai_family=AF_UNSPEC;
106 hints.ai_protocol=IPPROTO_TCP;
107 hints.ai_socktype=SOCK_STREAM;
108
109 int error= getaddrinfo(hostname, port, &hints, &ai);
110 if (error != 0)
111 {
112 if (error != EAI_SYSTEM)
113 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 #if defined(_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 errno= 0;
1393 unsigned long val= strtoul(ptr, &end, 10); /* flags */
1394 verify(errno == 0);
1395 verify(ptr != end);
1396 verify(val == 0);
1397 verify(end != NULL);
1398 errno= 0;
1399 *ndata = (ssize_t)strtoul(end, &end, 10); /* size */
1400 verify(errno == 0);
1401 verify(ptr != end);
1402 verify(end != NULL);
1403 while (end and *end != '\n' and isspace(*end))
1404 ++end;
1405 verify(end and *end == '\n');
1406
1407 *value= static_cast<char*>(malloc((size_t)*ndata));
1408 verify(*value != NULL);
1409
1410 execute(retry_read(*value, (size_t)*ndata));
1411
1412 execute(retry_read(buffer, 2));
1413 verify(memcmp(buffer, "\r\n", 2) == 0);
1414
1415 return TEST_PASS;
1416 }
1417
1418 static enum test_return ascii_get_value(const char *key, const char *value)
1419 {
1420
1421 char buffer[1024];
1422 size_t datasize= strlen(value);
1423
1424 verify(datasize < sizeof(buffer));
1425 execute(receive_line(buffer, sizeof(buffer)));
1426 verify(strncmp(buffer, "VALUE ", 6) == 0);
1427 verify(strncmp(buffer + 6, key, strlen(key)) == 0);
1428 char *ptr= buffer + 6 + strlen(key) + 1;
1429 char *end;
1430
1431 errno= 0;
1432 unsigned long val= strtoul(ptr, &end, 10); /* flags */
1433 verify(errno == 0);
1434 verify(ptr != end);
1435 verify(val == 0);
1436 verify(end != NULL);
1437
1438 errno= 0;
1439 val= strtoul(end, &end, 10); /* size */
1440 verify(errno == 0);
1441 verify(ptr != end);
1442 verify(val == datasize);
1443 verify(end != NULL);
1444 while (end and *end != '\n' and isspace(*end))
1445 {
1446 ++end;
1447 }
1448 verify(end and *end == '\n');
1449
1450 execute(retry_read(buffer, datasize));
1451 verify(memcmp(buffer, value, datasize) == 0);
1452
1453 execute(retry_read(buffer, 2));
1454 verify(memcmp(buffer, "\r\n", 2) == 0);
1455
1456 return TEST_PASS;
1457 }
1458
1459 static enum test_return ascii_get_item(const char *key, const char *value,
1460 bool exist)
1461 {
1462 char buffer[1024];
1463 size_t datasize= 0;
1464 if (value != NULL)
1465 {
1466 datasize= strlen(value);
1467 }
1468
1469 verify(datasize < sizeof(buffer));
1470 snprintf(buffer, sizeof(buffer), "get %s\r\n", key);
1471 execute(send_string(buffer));
1472
1473 if (exist)
1474 {
1475 execute(ascii_get_value(key, value));
1476 }
1477
1478 execute(retry_read(buffer, 5));
1479 verify(memcmp(buffer, "END\r\n", 5) == 0);
1480
1481 return TEST_PASS;
1482 }
1483
1484 static enum test_return ascii_gets_value(const char *key, const char *value,
1485 unsigned long *cas)
1486 {
1487
1488 char buffer[1024];
1489 size_t datasize= strlen(value);
1490
1491 verify(datasize < sizeof(buffer));
1492 execute(receive_line(buffer, sizeof(buffer)));
1493 verify(strncmp(buffer, "VALUE ", 6) == 0);
1494 verify(strncmp(buffer + 6, key, strlen(key)) == 0);
1495 char *ptr= buffer + 6 + strlen(key) + 1;
1496 char *end;
1497
1498 errno= 0;
1499 unsigned long val= strtoul(ptr, &end, 10); /* flags */
1500 verify(errno == 0);
1501 verify(ptr != end);
1502 verify(val == 0);
1503 verify(end != NULL);
1504
1505 errno= 0;
1506 val= strtoul(end, &end, 10); /* size */
1507 verify(errno == 0);
1508 verify(ptr != end);
1509 verify(val == datasize);
1510 verify(end != NULL);
1511
1512 errno= 0;
1513 *cas= strtoul(end, &end, 10); /* cas */
1514 verify(errno == 0);
1515 verify(ptr != end);
1516 verify(val == datasize);
1517 verify(end != NULL);
1518
1519 while (end and *end != '\n' and isspace(*end))
1520 {
1521 ++end;
1522 }
1523 verify(end and *end == '\n');
1524
1525 execute(retry_read(buffer, datasize));
1526 verify(memcmp(buffer, value, datasize) == 0);
1527
1528 execute(retry_read(buffer, 2));
1529 verify(memcmp(buffer, "\r\n", 2) == 0);
1530
1531 return TEST_PASS;
1532 }
1533
1534 static enum test_return ascii_gets_item(const char *key, const char *value,
1535 bool exist, unsigned long *cas)
1536 {
1537 char buffer[1024];
1538 size_t datasize= 0;
1539 if (value != NULL)
1540 {
1541 datasize= strlen(value);
1542 }
1543
1544 verify(datasize < sizeof(buffer));
1545 snprintf(buffer, sizeof(buffer), "gets %s\r\n", key);
1546 execute(send_string(buffer));
1547
1548 if (exist)
1549 execute(ascii_gets_value(key, value, cas));
1550
1551 execute(retry_read(buffer, 5));
1552 verify(memcmp(buffer, "END\r\n", 5) == 0);
1553
1554 return TEST_PASS;
1555 }
1556
1557 static enum test_return ascii_set_item(const char *key, const char *value)
1558 {
1559 char buffer[300];
1560 size_t len= strlen(value);
1561 snprintf(buffer, sizeof(buffer), "set %s 0 0 %u\r\n", key, (unsigned int)len);
1562 execute(send_string(buffer));
1563 execute(retry_write(value, len));
1564 execute(send_string("\r\n"));
1565 execute(receive_response("STORED\r\n"));
1566 return TEST_PASS;
1567 }
1568
1569 static enum test_return test_ascii_replace_impl(const char* key, bool noreply)
1570 {
1571 char buffer[1024];
1572 snprintf(buffer, sizeof(buffer), "replace %s 0 0 5%s\r\nvalue\r\n", key, noreply ? " noreply" : "");
1573 execute(send_string(buffer));
1574
1575 if (noreply)
1576 {
1577 execute(test_ascii_version());
1578 }
1579 else
1580 {
1581 execute(receive_response("NOT_STORED\r\n"));
1582 }
1583
1584 execute(ascii_set_item(key, "value"));
1585 execute(ascii_get_item(key, "value", true));
1586
1587
1588 execute(send_string(buffer));
1589
1590 if (noreply)
1591 execute(test_ascii_version());
1592 else
1593 execute(receive_response("STORED\r\n"));
1594
1595 return test_ascii_version();
1596 }
1597
1598 static enum test_return test_ascii_replace(void)
1599 {
1600 return test_ascii_replace_impl("test_ascii_replace", false);
1601 }
1602
1603 static enum test_return test_ascii_replace_noreply(void)
1604 {
1605 return test_ascii_replace_impl("test_ascii_replace_noreply", true);
1606 }
1607
1608 static enum test_return test_ascii_cas_impl(const char* key, bool noreply)
1609 {
1610 char buffer[1024];
1611 unsigned long cas;
1612
1613 execute(ascii_set_item(key, "value"));
1614 execute(ascii_gets_item(key, "value", true, &cas));
1615
1616 snprintf(buffer, sizeof(buffer), "cas %s 0 0 6 %lu%s\r\nvalue2\r\n", key, cas, noreply ? " noreply" : "");
1617 execute(send_string(buffer));
1618
1619 if (noreply)
1620 {
1621 execute(test_ascii_version());
1622 }
1623 else
1624 {
1625 execute(receive_response("STORED\r\n"));
1626 }
1627
1628 /* reexecute the same command should fail due to illegal cas */
1629 execute(send_string(buffer));
1630
1631 if (noreply)
1632 {
1633 execute(test_ascii_version());
1634 }
1635 else
1636 {
1637 execute(receive_response("EXISTS\r\n"));
1638 }
1639
1640 return test_ascii_version();
1641 }
1642
1643 static enum test_return test_ascii_cas(void)
1644 {
1645 return test_ascii_cas_impl("test_ascii_cas", false);
1646 }
1647
1648 static enum test_return test_ascii_cas_noreply(void)
1649 {
1650 return test_ascii_cas_impl("test_ascii_cas_noreply", true);
1651 }
1652
1653 static enum test_return test_ascii_delete_impl(const char *key, bool noreply)
1654 {
1655 execute(ascii_set_item(key, "value"));
1656
1657 execute(send_string("delete\r\n"));
1658 execute(receive_error_response());
1659 /* BUG: the server accepts delete a b */
1660 execute(send_string("delete a b c d e\r\n"));
1661 execute(receive_error_response());
1662
1663 char buffer[1024];
1664 snprintf(buffer, sizeof(buffer), "delete %s%s\r\n", key, noreply ? " noreply" : "");
1665 execute(send_string(buffer));
1666
1667 if (noreply)
1668 execute(test_ascii_version());
1669 else
1670 execute(receive_response("DELETED\r\n"));
1671
1672 execute(ascii_get_item(key, "value", false));
1673 execute(send_string(buffer));
1674 if (noreply)
1675 execute(test_ascii_version());
1676 else
1677 execute(receive_response("NOT_FOUND\r\n"));
1678
1679 return TEST_PASS;
1680 }
1681
1682 static enum test_return test_ascii_delete(void)
1683 {
1684 return test_ascii_delete_impl("test_ascii_delete", false);
1685 }
1686
1687 static enum test_return test_ascii_delete_noreply(void)
1688 {
1689 return test_ascii_delete_impl("test_ascii_delete_noreply", true);
1690 }
1691
1692 static enum test_return test_ascii_get(void)
1693 {
1694 execute(ascii_set_item("test_ascii_get", "value"));
1695
1696 execute(send_string("get\r\n"));
1697 execute(receive_error_response());
1698 execute(ascii_get_item("test_ascii_get", "value", true));
1699 execute(ascii_get_item("test_ascii_get_notfound", "value", false));
1700
1701 return TEST_PASS;
1702 }
1703
1704 static enum test_return test_ascii_gets(void)
1705 {
1706 execute(ascii_set_item("test_ascii_gets", "value"));
1707
1708 execute(send_string("gets\r\n"));
1709 execute(receive_error_response());
1710 unsigned long cas;
1711 execute(ascii_gets_item("test_ascii_gets", "value", true, &cas));
1712 execute(ascii_gets_item("test_ascii_gets_notfound", "value", false, &cas));
1713
1714 return TEST_PASS;
1715 }
1716
1717 static enum test_return test_ascii_mget(void)
1718 {
1719 const uint32_t nkeys= 5;
1720 const char * const keys[]= {
1721 "test_ascii_mget1",
1722 "test_ascii_mget2",
1723 /* test_ascii_mget_3 does not exist :) */
1724 "test_ascii_mget4",
1725 "test_ascii_mget5",
1726 "test_ascii_mget6"
1727 };
1728
1729 for (uint32_t x= 0; x < nkeys; ++x)
1730 {
1731 execute(ascii_set_item(keys[x], "value"));
1732 }
1733
1734 /* Ask for a key that doesn't exist as well */
1735 execute(send_string("get test_ascii_mget1 test_ascii_mget2 test_ascii_mget3 "
1736 "test_ascii_mget4 test_ascii_mget5 "
1737 "test_ascii_mget6\r\n"));
1738
1739 std::vector<char *> returned;
1740 returned.resize(nkeys);
1741
1742 for (uint32_t x= 0; x < nkeys; ++x)
1743 {
1744 ssize_t nbytes = 0;
1745 char *v= NULL;
1746 execute(ascii_get_unknown_value(&returned[x], &v, &nbytes));
1747 verify(nbytes == 5);
1748 verify(memcmp(v, "value", 5) == 0);
1749 free(v);
1750 }
1751
1752 char buffer[5];
1753 execute(retry_read(buffer, 5));
1754 verify(memcmp(buffer, "END\r\n", 5) == 0);
1755
1756 /* verify that we got all the keys we expected */
1757 for (uint32_t x= 0; x < nkeys; ++x)
1758 {
1759 bool found= false;
1760 for (uint32_t y= 0; y < nkeys; ++y)
1761 {
1762 if (strcmp(keys[x], returned[y]) == 0)
1763 {
1764 found = true;
1765 break;
1766 }
1767 }
1768 verify(found);
1769 }
1770
1771 for (uint32_t x= 0; x < nkeys; ++x)
1772 {
1773 free(returned[x]);
1774 }
1775
1776 return TEST_PASS;
1777 }
1778
1779 static enum test_return test_ascii_incr_impl(const char* key, bool noreply)
1780 {
1781 char cmd[300];
1782 snprintf(cmd, sizeof(cmd), "incr %s 1%s\r\n", key, noreply ? " noreply" : "");
1783
1784 execute(ascii_set_item(key, "0"));
1785 for (int x= 1; x < 11; ++x)
1786 {
1787 execute(send_string(cmd));
1788
1789 if (noreply)
1790 execute(test_ascii_version());
1791 else
1792 {
1793 char buffer[80];
1794 execute(receive_line(buffer, sizeof(buffer)));
1795 int val= atoi(buffer);
1796 verify(val == x);
1797 }
1798 }
1799
1800 execute(ascii_get_item(key, "10", true));
1801
1802 return TEST_PASS;
1803 }
1804
1805 static enum test_return test_ascii_incr(void)
1806 {
1807 return test_ascii_incr_impl("test_ascii_incr", false);
1808 }
1809
1810 static enum test_return test_ascii_incr_noreply(void)
1811 {
1812 return test_ascii_incr_impl("test_ascii_incr_noreply", true);
1813 }
1814
1815 static enum test_return test_ascii_decr_impl(const char* key, bool noreply)
1816 {
1817 char cmd[300];
1818 snprintf(cmd, sizeof(cmd), "decr %s 1%s\r\n", key, noreply ? " noreply" : "");
1819
1820 execute(ascii_set_item(key, "9"));
1821 for (int x= 8; x > -1; --x)
1822 {
1823 execute(send_string(cmd));
1824
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 int val= atoi(buffer);
1834 verify(val == x);
1835 }
1836 }
1837
1838 execute(ascii_get_item(key, "0", true));
1839
1840 /* verify that it doesn't wrap */
1841 execute(send_string(cmd));
1842 if (noreply)
1843 {
1844 execute(test_ascii_version());
1845 }
1846 else
1847 {
1848 char buffer[80];
1849 execute(receive_line(buffer, sizeof(buffer)));
1850 }
1851 execute(ascii_get_item(key, "0", true));
1852
1853 return TEST_PASS;
1854 }
1855
1856 static enum test_return test_ascii_decr(void)
1857 {
1858 return test_ascii_decr_impl("test_ascii_decr", false);
1859 }
1860
1861 static enum test_return test_ascii_decr_noreply(void)
1862 {
1863 return test_ascii_decr_impl("test_ascii_decr_noreply", true);
1864 }
1865
1866
1867 static enum test_return test_ascii_flush_impl(const char *key, bool noreply)
1868 {
1869 #if 0
1870 /* Verify that the flush_all command handles unknown options */
1871 /* Bug in the current memcached server! */
1872 execute(send_string("flush_all foo bar\r\n"));
1873 execute(receive_error_response());
1874 #endif
1875
1876 execute(ascii_set_item(key, key));
1877 execute(ascii_get_item(key, key, true));
1878
1879 if (noreply)
1880 {
1881 execute(send_string("flush_all noreply\r\n"));
1882 execute(test_ascii_version());
1883 }
1884 else
1885 {
1886 execute(send_string("flush_all\r\n"));
1887 execute(receive_response("OK\r\n"));
1888 }
1889
1890 execute(ascii_get_item(key, key, false));
1891
1892 return TEST_PASS;
1893 }
1894
1895 static enum test_return test_ascii_flush(void)
1896 {
1897 return test_ascii_flush_impl("test_ascii_flush", false);
1898 }
1899
1900 static enum test_return test_ascii_flush_noreply(void)
1901 {
1902 return test_ascii_flush_impl("test_ascii_flush_noreply", true);
1903 }
1904
1905 static enum test_return test_ascii_concat_impl(const char *key,
1906 bool append,
1907 bool noreply)
1908 {
1909 const char *value;
1910
1911 if (append)
1912 value="hello";
1913 else
1914 value=" world";
1915
1916 execute(ascii_set_item(key, value));
1917
1918 if (append)
1919 {
1920 value=" world";
1921 }
1922 else
1923 {
1924 value="hello";
1925 }
1926
1927 char cmd[400];
1928 snprintf(cmd, sizeof(cmd), "%s %s 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("STORED\r\n"));
1941 }
1942
1943 execute(ascii_get_item(key, "hello world", true));
1944
1945 snprintf(cmd, sizeof(cmd), "%s %s_notfound 0 0 %u%s\r\n%s\r\n",
1946 append ? "append" : "prepend",
1947 key, (unsigned int)strlen(value), noreply ? " noreply" : "",
1948 value);
1949 execute(send_string(cmd));
1950
1951 if (noreply)
1952 {
1953 execute(test_ascii_version());
1954 }
1955 else
1956 {
1957 execute(receive_response("NOT_STORED\r\n"));
1958 }
1959
1960 return TEST_PASS;
1961 }
1962
1963 static enum test_return test_ascii_append(void)
1964 {
1965 return test_ascii_concat_impl("test_ascii_append", true, false);
1966 }
1967
1968 static enum test_return test_ascii_prepend(void)
1969 {
1970 return test_ascii_concat_impl("test_ascii_prepend", false, false);
1971 }
1972
1973 static enum test_return test_ascii_append_noreply(void)
1974 {
1975 return test_ascii_concat_impl("test_ascii_append_noreply", true, true);
1976 }
1977
1978 static enum test_return test_ascii_prepend_noreply(void)
1979 {
1980 return test_ascii_concat_impl("test_ascii_prepend_noreply", false, true);
1981 }
1982
1983 static enum test_return test_ascii_stat(void)
1984 {
1985 execute(send_string("stats noreply\r\n"));
1986 execute(receive_error_response());
1987 execute(send_string("stats\r\n"));
1988 char buffer[1024];
1989 do {
1990 execute(receive_line(buffer, sizeof(buffer)));
1991 } while (strcmp(buffer, "END\r\n") != 0);
1992
1993 return TEST_PASS_RECONNECT;
1994 }
1995
1996 typedef enum test_return(*TEST_FUNC)(void);
1997
1998 struct testcase
1999 {
2000 const char *description;
2001 TEST_FUNC function;
2002 };
2003
2004 struct testcase testcases[]= {
2005 { "ascii quit", test_ascii_quit },
2006 { "ascii version", test_ascii_version },
2007 { "ascii verbosity", test_ascii_verbosity },
2008 { "ascii set", test_ascii_set },
2009 { "ascii set noreply", test_ascii_set_noreply },
2010 { "ascii get", test_ascii_get },
2011 { "ascii gets", test_ascii_gets },
2012 { "ascii mget", test_ascii_mget },
2013 { "ascii flush", test_ascii_flush },
2014 { "ascii flush noreply", test_ascii_flush_noreply },
2015 { "ascii add", test_ascii_add },
2016 { "ascii add noreply", test_ascii_add_noreply },
2017 { "ascii replace", test_ascii_replace },
2018 { "ascii replace noreply", test_ascii_replace_noreply },
2019 { "ascii cas", test_ascii_cas },
2020 { "ascii cas noreply", test_ascii_cas_noreply },
2021 { "ascii delete", test_ascii_delete },
2022 { "ascii delete noreply", test_ascii_delete_noreply },
2023 { "ascii incr", test_ascii_incr },
2024 { "ascii incr noreply", test_ascii_incr_noreply },
2025 { "ascii decr", test_ascii_decr },
2026 { "ascii decr noreply", test_ascii_decr_noreply },
2027 { "ascii append", test_ascii_append },
2028 { "ascii append noreply", test_ascii_append_noreply },
2029 { "ascii prepend", test_ascii_prepend },
2030 { "ascii prepend noreply", test_ascii_prepend_noreply },
2031 { "ascii stat", test_ascii_stat },
2032 { "binary noop", test_binary_noop },
2033 { "binary quit", test_binary_quit },
2034 { "binary quitq", test_binary_quitq },
2035 { "binary set", test_binary_set },
2036 { "binary setq", test_binary_setq },
2037 { "binary flush", test_binary_flush },
2038 { "binary flushq", test_binary_flushq },
2039 { "binary add", test_binary_add },
2040 { "binary addq", test_binary_addq },
2041 { "binary replace", test_binary_replace },
2042 { "binary replaceq", test_binary_replaceq },
2043 { "binary delete", test_binary_delete },
2044 { "binary deleteq", test_binary_deleteq },
2045 { "binary get", test_binary_get },
2046 { "binary getq", test_binary_getq },
2047 { "binary getk", test_binary_getk },
2048 { "binary getkq", test_binary_getkq },
2049 { "binary incr", test_binary_incr },
2050 { "binary incrq", test_binary_incrq },
2051 { "binary decr", test_binary_decr },
2052 { "binary decrq", test_binary_decrq },
2053 { "binary version", test_binary_version },
2054 { "binary append", test_binary_append },
2055 { "binary appendq", test_binary_appendq },
2056 { "binary prepend", test_binary_prepend },
2057 { "binary prependq", test_binary_prependq },
2058 { "binary stat", test_binary_stat },
2059 { NULL, NULL}
2060 };
2061
2062 const int ascii_tests = 1;
2063 const int binary_tests = 2;
2064
2065 struct test_type_st
2066 {
2067 bool ascii;
2068 bool binary;
2069 };
2070
2071 int main(int argc, char **argv)
2072 {
2073 static const char * const status_msg[]= {"[skip]", "[pass]", "[pass]", "[FAIL]"};
2074 struct test_type_st tests= { true, true };
2075 int total= 0;
2076 int failed= 0;
2077 const char *hostname= "localhost";
2078 const char *port= "11211";
2079 int cmd;
2080 bool prompt= false;
2081 const char *testname= NULL;
2082
2083
2084
2085 while ((cmd= getopt(argc, argv, "qt:vch:p:PT:?ab")) != EOF)
2086 {
2087 switch (cmd) {
2088 case 'a':
2089 tests.ascii= true;
2090 tests.binary= false;
2091 break;
2092
2093 case 'b':
2094 tests.ascii= false;
2095 tests.binary= true;
2096 break;
2097
2098 case 't':
2099 timeout= atoi(optarg);
2100 if (timeout == 0)
2101 {
2102 fprintf(stderr, "Invalid timeout. Please specify a number for -t\n");
2103 return EXIT_FAILURE;
2104 }
2105 break;
2106
2107 case 'v': verbose= true;
2108 break;
2109
2110 case 'c': do_core= true;
2111 break;
2112
2113 case 'h': hostname= optarg;
2114 break;
2115
2116 case 'p': port= optarg;
2117 break;
2118
2119 case 'q':
2120 close_stdio();
2121 break;
2122
2123 case 'P': prompt= true;
2124 break;
2125
2126 case 'T': testname= optarg;
2127 break;
2128
2129 default:
2130 fprintf(stderr, "Usage: %s [-h hostname] [-p port] [-c] [-v] [-t n] [-P] [-T testname]'\n"
2131 "\t-c\tGenerate coredump if a test fails\n"
2132 "\t-v\tVerbose test output (print out the assertion)\n"
2133 "\t-t n\tSet the timeout for io-operations to n seconds\n"
2134 "\t-P\tPrompt the user before starting a test.\n"
2135 "\t\t\t\"skip\" will skip the test\n"
2136 "\t\t\t\"quit\" will terminate memcapable\n"
2137 "\t\t\tEverything else will start the test\n"
2138 "\t-T n\tJust run the test named n\n"
2139 "\t-a\tOnly test the ascii protocol\n"
2140 "\t-b\tOnly test the binary protocol\n",
2141 argv[0]);
2142 return EXIT_SUCCESS;
2143 }
2144 }
2145
2146 initialize_sockets();
2147 sock= connect_server(hostname, port);
2148 if (sock == INVALID_SOCKET)
2149 {
2150 fprintf(stderr, "Failed to connect to <%s:%s>: %s\n",
2151 hostname, port, strerror(get_socket_errno()));
2152 return EXIT_FAILURE;
2153 }
2154
2155 for (int ii= 0; testcases[ii].description != NULL; ++ii)
2156 {
2157 if (testname != NULL && strcmp(testcases[ii].description, testname) != 0)
2158 {
2159 continue;
2160 }
2161
2162 if ((testcases[ii].description[0] == 'a' && (tests.ascii) == 0) ||
2163 (testcases[ii].description[0] == 'b' && (tests.binary) == 0))
2164 {
2165 continue;
2166 }
2167 ++total;
2168 fprintf(stdout, "%-40s", testcases[ii].description);
2169 fflush(stdout);
2170
2171 if (prompt)
2172 {
2173 fprintf(stdout, "\nPress <return> when you are ready? ");
2174 char buffer[80] = {0};
2175 if (fgets(buffer, sizeof(buffer), stdin) != NULL) {
2176 if (strncmp(buffer, "skip", 4) == 0)
2177 {
2178 fprintf(stdout, "%-40s%s\n", testcases[ii].description,
2179 status_msg[TEST_SKIP]);
2180 fflush(stdout);
2181 continue;
2182 }
2183 if (strncmp(buffer, "quit", 4) == 0)
2184 {
2185 exit(EXIT_SUCCESS);
2186 }
2187 }
2188
2189 fprintf(stdout, "%-40s", testcases[ii].description);
2190 fflush(stdout);
2191 }
2192
2193 bool reconnect= false;
2194 enum test_return ret= testcases[ii].function();
2195 if (ret == TEST_FAIL)
2196 {
2197 reconnect= true;
2198 ++failed;
2199 if (verbose)
2200 fprintf(stderr, "\n");
2201 }
2202 else if (ret == TEST_PASS_RECONNECT)
2203 reconnect= true;
2204
2205 fprintf(stderr, "%s\n", status_msg[ret]);
2206 if (reconnect)
2207 {
2208 closesocket(sock);
2209 if ((sock= connect_server(hostname, port)) == INVALID_SOCKET)
2210 {
2211 fprintf(stderr, "Failed to connect to <%s:%s>: %s\n", hostname, port, strerror(get_socket_errno()));
2212 fprintf(stderr, "%d of %d tests failed\n", failed, total);
2213 return EXIT_FAILURE;
2214 }
2215 }
2216 }
2217
2218 closesocket(sock);
2219 if (failed == 0)
2220 {
2221 fprintf(stdout, "All tests passed\n");
2222 }
2223 else
2224 {
2225 fprintf(stderr, "%d of %d tests failed\n", failed, total);
2226 }
2227
2228 return (failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
2229 }