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