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