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