X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=clients%2Fmemcapable.cc;h=31c2fc54c04203ffec5ef13123123d09661c9037;hb=d61d9f95bb372c0691ece9632382c62c7b3c1b46;hp=bc3b551f412810d73fb9d7410d15bd5dcdfee7ad;hpb=751e342e158ae65052ce098ccd64aa54e39312db;p=awesomized%2Flibmemcached diff --git a/clients/memcapable.cc b/clients/memcapable.cc index bc3b551f..31c2fc54 100644 --- a/clients/memcapable.cc +++ b/clients/memcapable.cc @@ -1,4 +1,5 @@ /* LibMemcached + * Copyright (C) 2011-2012 Data Differential, http://datadifferential.com/ * Copyright (C) 2006-2009 Brian Aker * All rights reserved. * @@ -12,7 +13,7 @@ /* -*- Mode: C; tab-width: 2; c-basic-offset: 2; indent-tabs-mode: nil -*- */ #undef NDEBUG -#include +#include #ifdef HAVE_POLL_H #include @@ -34,10 +35,13 @@ #include #include -#include -#include -#include -#include + +#include "libmemcached/socket.hpp" +#include "libmemcached/memcached/protocol_binary.h" +#include "libmemcached/byteorder.h" +#include "clients/utilities.h" + +#include #ifdef linux /* /usr/include/netinet/in.h defines macros from ntohs() to _bswap_nn to @@ -106,9 +110,13 @@ static struct addrinfo *lookuphost(const char *hostname, const char *port) if (error != 0) { if (error != EAI_SYSTEM) + { fprintf(stderr, "getaddrinfo(): %s\n", gai_strerror(error)); + } else + { perror("getaddrinfo()"); + } } return ai; @@ -120,7 +128,7 @@ static struct addrinfo *lookuphost(const char *hostname, const char *port) */ static memcached_socket_t set_noblock(void) { -#ifdef WIN32 +#if defined(_WIN32) u_long arg = 1; if (ioctlsocket(sock, FIONBIO, &arg) == SOCKET_ERROR) { @@ -178,8 +186,9 @@ static memcached_socket_t connect_server(const char *hostname, const char *port) } } else - fprintf(stderr, "Failed to create socket: %s\n", - strerror(get_socket_errno())); + { + fprintf(stderr, "Failed to create socket: %s\n", strerror(get_socket_errno())); + } freeaddrinfo(ai); } @@ -245,7 +254,7 @@ static enum test_return ensure(bool val, const char *expression, const char *fil { if (verbose) { - fprintf(stderr, "\n%s:%d: %s", file, line, expression); + fprintf(stdout, "\n%s:%d: %s", file, line, expression); } if (do_core) @@ -330,7 +339,7 @@ static enum test_return retry_read(void *buf, size_t len) ssize_t nr= timeout_io_op(sock, POLLIN, ((char*) buf) + offset, len - offset); switch (nr) { case -1 : - fprintf(stderr, "Errno: %d %s\n", get_socket_errno(), strerror(errno)); + fprintf(stderr, "Errno: %d %s\n", get_socket_errno(), strerror(errno)); verify(get_socket_errno() == EINTR || get_socket_errno() == EAGAIN); break; @@ -1293,7 +1302,7 @@ static enum test_return test_ascii_verbosity(void) execute(receive_error_response()); execute(send_string("verbosity noreply\r\n")); - execute(receive_error_response()); + execute(test_ascii_version()); execute(send_string("verbosity 0 noreply\r\n")); execute(test_ascii_version()); @@ -1385,11 +1394,15 @@ static enum test_return ascii_get_unknown_value(char **key, char **value, ssize_ verify(*key != NULL); char *ptr= end + 1; + errno= 0; unsigned long val= strtoul(ptr, &end, 10); /* flags */ + verify(errno == 0); verify(ptr != end); verify(val == 0); verify(end != NULL); + errno= 0; *ndata = (ssize_t)strtoul(end, &end, 10); /* size */ + verify(errno == 0); verify(ptr != end); verify(end != NULL); while (end and *end != '\n' and isspace(*end)) @@ -1420,11 +1433,16 @@ static enum test_return ascii_get_value(const char *key, const char *value) char *ptr= buffer + 6 + strlen(key) + 1; char *end; + errno= 0; unsigned long val= strtoul(ptr, &end, 10); /* flags */ + verify(errno == 0); verify(ptr != end); verify(val == 0); verify(end != NULL); + + errno= 0; val= strtoul(end, &end, 10); /* size */ + verify(errno == 0); verify(ptr != end); verify(val == datasize); verify(end != NULL); @@ -1482,15 +1500,23 @@ static enum test_return ascii_gets_value(const char *key, const char *value, char *ptr= buffer + 6 + strlen(key) + 1; char *end; + errno= 0; unsigned long val= strtoul(ptr, &end, 10); /* flags */ + verify(errno == 0); verify(ptr != end); verify(val == 0); verify(end != NULL); + + errno= 0; val= strtoul(end, &end, 10); /* size */ + verify(errno == 0); verify(ptr != end); verify(val == datasize); verify(end != NULL); + + errno= 0; *cas= strtoul(end, &end, 10); /* cas */ + verify(errno == 0); verify(ptr != end); verify(val == datasize); verify(end != NULL); @@ -1715,7 +1741,8 @@ static enum test_return test_ascii_mget(void) "test_ascii_mget4 test_ascii_mget5 " "test_ascii_mget6\r\n")); - char *returned[nkeys]; + std::vector returned; + returned.resize(nkeys); for (uint32_t x= 0; x < nkeys; ++x) { @@ -2052,8 +2079,8 @@ int main(int argc, char **argv) struct test_type_st tests= { true, true }; int total= 0; int failed= 0; - const char *hostname= "localhost"; - const char *port= "11211"; + const char *hostname= NULL; + const char *port= MEMCACHED_DEFAULT_PORT_STRING; int cmd; bool prompt= false; const char *testname= NULL; @@ -2121,12 +2148,18 @@ int main(int argc, char **argv) } } + if (!hostname) + { + fprintf(stderr, "No hostname was provided.\n"); + return EXIT_FAILURE; + } + initialize_sockets(); sock= connect_server(hostname, port); if (sock == INVALID_SOCKET) { fprintf(stderr, "Failed to connect to <%s:%s>: %s\n", - hostname, port, strerror(get_socket_errno())); + hostname?:"(null)", port?:"(null)", strerror(get_socket_errno())); return EXIT_FAILURE; } @@ -2175,18 +2208,30 @@ int main(int argc, char **argv) reconnect= true; ++failed; if (verbose) + { fprintf(stderr, "\n"); + } } else if (ret == TEST_PASS_RECONNECT) + { reconnect= true; + } + + if (ret == TEST_FAIL) + { + fprintf(stderr, "%s\n", status_msg[ret]); + } + else + { + fprintf(stdout, "%s\n", status_msg[ret]); + } - fprintf(stderr, "%s\n", status_msg[ret]); if (reconnect) { closesocket(sock); if ((sock= connect_server(hostname, port)) == INVALID_SOCKET) { - fprintf(stderr, "Failed to connect to <%s:%s>: %s\n", hostname, port, strerror(get_socket_errno())); + fprintf(stderr, "Failed to connect to <%s:%s>: %s\n", hostname?:"(null)", port?:"(null)", strerror(get_socket_errno())); fprintf(stderr, "%d of %d tests failed\n", failed, total); return EXIT_FAILURE; }