Fix one of the valgrind issues in mem_function.cc test.
[awesomized/libmemcached] / clients / memcapable.cc
index baac28b2484f9c9983334a2b3cb06500d484855e..8bce153a555e44ffa9136d5c8e30f7a6194025bb 100644 (file)
 /* -*- Mode: C; tab-width: 2; c-basic-offset: 2; indent-tabs-mode: nil -*- */
 #undef NDEBUG
 
-#include "config.h"
-#include <pthread.h>
-#include <sys/types.h>
+#include <config.h>
+
+#ifdef HAVE_POLL_H
+#include <poll.h>
+#else
+#include "poll/poll.h"
+#endif
+
+#include <cassert>
+#include <cerrno>
+#include <cstdio>
+#include <cstdlib>
+#include <cstring>
+#include <ctype.h>
 #include <fcntl.h>
-#include <signal.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <errno.h>
-#include <assert.h>
-#include <string.h>
 #include <inttypes.h>
-#include <stdbool.h>
+#include <pthread.h>
+#include <signal.h>
+#include <sys/types.h>
 #include <unistd.h>
-#include <ctype.h>
 
-#include <libmemcached/memcached.h>
+#include <libmemcached-1.0/memcached.h>
+#include <libmemcached/close_socket.hpp>
 #include <libmemcached/memcached/protocol_binary.h>
 #include <libmemcached/byteorder.h>
-#include "utilities.h"
+#include <clients/utilities.h>
 
 #ifdef linux
 /* /usr/include/netinet/in.h defines macros from ntohs() to _bswap_nn to
@@ -126,7 +133,7 @@ static memcached_socket_t set_noblock(void)
   if (flags == -1)
   {
     perror("Failed to get socket flags");
-    closesocket(sock);
+    memcached_close_socket(sock);
     return INVALID_SOCKET;
   }
 
@@ -135,7 +142,7 @@ static memcached_socket_t set_noblock(void)
     if (fcntl(sock, F_SETFL, flags | O_NONBLOCK) == -1)
     {
       perror("Failed to set socket to nonblocking mode");
-      closesocket(sock);
+      memcached_close_socket(sock);
       return INVALID_SOCKET;
     }
   }
@@ -297,7 +304,7 @@ static enum test_return send_packet(command *cmd)
   cmd->plain.message.header.request.bodylen=
           ntohl(cmd->plain.message.header.request.bodylen);
   cmd->plain.message.header.request.cas=
-          ntohll(cmd->plain.message.header.request.cas);
+          memcached_ntohll(cmd->plain.message.header.request.cas);
 
   execute(resend_packet(cmd));
   return TEST_PASS;
@@ -343,7 +350,7 @@ static enum test_return recv_packet(response *rsp)
   rsp->plain.message.header.response.bodylen=
           ntohl(rsp->plain.message.header.response.bodylen);
   rsp->plain.message.header.response.cas=
-          ntohll(rsp->plain.message.header.response.cas);
+          memcached_ntohll(rsp->plain.message.header.response.cas);
 
   size_t bodysz= rsp->plain.message.header.response.bodylen;
   if (bodysz > 0)
@@ -472,8 +479,8 @@ static void arithmetic_command(command *cmd,
   cmd->incr.message.header.request.extlen= 20;
   cmd->incr.message.header.request.bodylen= (uint32_t)(keylen + 20);
   cmd->incr.message.header.request.opaque= 0xdeadbeef;
-  cmd->incr.message.body.delta= htonll(delta);
-  cmd->incr.message.body.initial= htonll(initial);
+  cmd->incr.message.body.delta= memcached_htonll(delta);
+  cmd->incr.message.body.initial= memcached_htonll(initial);
   cmd->incr.message.body.expiration= htonl(exptime);
 
   off_t key_offset= sizeof (protocol_binary_request_no_extras) + 20;
@@ -655,7 +662,7 @@ static enum test_return test_binary_set_impl(const char* key, uint8_t cc)
   command cmd;
   response rsp;
 
-  uint64_t value= 0xdeadbeefdeadcafe;
+  uint64_t value= 0xdeadbeefdeadcafeULL;
   storage_command(&cmd, cc, key, strlen(key), &value, sizeof (value), 0, 0);
 
   /* set should always work */
@@ -690,8 +697,7 @@ static enum test_return test_binary_set_impl(const char* key, uint8_t cc)
   }
 
   /* try to set with the correct CAS value */
-  cmd.plain.message.header.request.cas=
-          htonll(rsp.plain.message.header.response.cas);
+  cmd.plain.message.header.request.cas= memcached_htonll(rsp.plain.message.header.response.cas);
   execute(resend_packet(&cmd));
   if (cc == PROTOCOL_BINARY_CMD_SET)
   {
@@ -702,8 +708,7 @@ static enum test_return test_binary_set_impl(const char* key, uint8_t cc)
     execute(test_binary_noop());
 
   /* try to set with an incorrect CAS value */
-  cmd.plain.message.header.request.cas=
-          htonll(rsp.plain.message.header.response.cas - 1);
+  cmd.plain.message.header.request.cas= memcached_htonll(rsp.plain.message.header.response.cas - 1);
   execute(resend_packet(&cmd));
   execute(send_binary_noop());
   execute(recv_packet(&rsp));
@@ -727,7 +732,7 @@ static enum test_return test_binary_add_impl(const char* key, uint8_t cc)
 {
   command cmd;
   response rsp;
-  uint64_t value= 0xdeadbeefdeadcafe;
+  uint64_t value= 0xdeadbeefdeadcafeULL;
   storage_command(&cmd, cc, key, strlen(key), &value, sizeof (value), 0, 0);
 
   /* first add should work, rest of them should fail (even with cas
@@ -786,7 +791,7 @@ static enum test_return test_binary_replace_impl(const char* key, uint8_t cc)
 {
   command cmd;
   response rsp;
-  uint64_t value= 0xdeadbeefdeadcafe;
+  uint64_t value= 0xdeadbeefdeadcafeULL;
   storage_command(&cmd, cc, key, strlen(key), &value, sizeof (value), 0, 0);
 
   /* first replace should fail, successive should succeed (when the
@@ -819,8 +824,7 @@ static enum test_return test_binary_replace_impl(const char* key, uint8_t cc)
   }
 
   /* verify that replace with CAS value works! */
-  cmd.plain.message.header.request.cas=
-          htonll(rsp.plain.message.header.response.cas);
+  cmd.plain.message.header.request.cas= memcached_htonll(rsp.plain.message.header.response.cas);
   execute(resend_packet(&cmd));
 
   if (cc == PROTOCOL_BINARY_CMD_REPLACE)
@@ -832,8 +836,7 @@ static enum test_return test_binary_replace_impl(const char* key, uint8_t cc)
     execute(test_binary_noop());
 
   /* try to set with an incorrect CAS value */
-  cmd.plain.message.header.request.cas=
-          htonll(rsp.plain.message.header.response.cas - 1);
+  cmd.plain.message.header.request.cas= memcached_htonll(rsp.plain.message.header.response.cas - 1);
   execute(resend_packet(&cmd));
   execute(send_binary_noop());
   execute(recv_packet(&rsp));
@@ -956,7 +959,7 @@ static enum test_return test_binary_incr_impl(const char* key, uint8_t cc)
     {
       execute(recv_packet(&rsp));
       verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_SUCCESS));
-      verify(ntohll(rsp.incr.message.body.value) == ii);
+      verify(memcached_ntohll(rsp.incr.message.body.value) == ii);
     }
     else
       execute(test_binary_noop());
@@ -994,7 +997,7 @@ static enum test_return test_binary_decr_impl(const char* key, uint8_t cc)
     {
       execute(recv_packet(&rsp));
       verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_SUCCESS));
-      verify(ntohll(rsp.decr.message.body.value) == (uint64_t)ii);
+      verify(memcached_ntohll(rsp.decr.message.body.value) == (uint64_t)ii);
     }
     else
       execute(test_binary_noop());
@@ -1006,7 +1009,7 @@ static enum test_return test_binary_decr_impl(const char* key, uint8_t cc)
   {
     execute(recv_packet(&rsp));
     verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_SUCCESS));
-    verify(ntohll(rsp.decr.message.body.value) == 0);
+    verify(memcached_ntohll(rsp.decr.message.body.value) == 0);
   }
   else
   {
@@ -1333,7 +1336,10 @@ static enum test_return ascii_get_unknown_value(char **key, char **value, ssize_
   verify(strncmp(buffer, "VALUE ", 6) == 0);
   char *end= strchr(buffer + 6, ' ');
   verify(end != NULL);
-  *end= '\0';
+  if (end)
+  {
+    *end= '\0';
+  }
   *key= strdup(buffer + 6);
   verify(*key != NULL);
   char *ptr= end + 1;
@@ -1345,9 +1351,9 @@ static enum test_return ascii_get_unknown_value(char **key, char **value, ssize_
   *ndata = (ssize_t)strtoul(end, &end, 10); /* size */
   verify(ptr != end);
   verify(end != NULL);
-  while (*end != '\n' && isspace(*end))
+  while (end and *end != '\n' and isspace(*end))
     ++end;
-  verify(*end == '\n');
+  verify(end and *end == '\n');
 
   *value= static_cast<char*>(malloc((size_t)*ndata));
   verify(*value != NULL);
@@ -1381,9 +1387,11 @@ static enum test_return ascii_get_value(const char *key, const char *value)
   verify(ptr != end);
   verify(val == datasize);
   verify(end != NULL);
-  while (*end != '\n' && isspace(*end))
+  while (end and *end != '\n' and isspace(*end))
+  {
     ++end;
-  verify(*end == '\n');
+  }
+  verify(end and *end == '\n');
 
   execute(retry_read(buffer, datasize));
   verify(memcmp(buffer, value, datasize) == 0);
@@ -1442,9 +1450,11 @@ static enum test_return ascii_gets_value(const char *key, const char *value,
   verify(val == datasize);
   verify(end != NULL);
 
-  while (*end != '\n' && isspace(*end))
+  while (end and *end != '\n' and isspace(*end))
+  {
     ++end;
-  verify(*end == '\n');
+  }
+  verify(end and *end == '\n');
 
   execute(retry_read(buffer, datasize));
   verify(memcmp(buffer, value, datasize) == 0);
@@ -1971,17 +1981,19 @@ int main(int argc, char **argv)
 
 
 
-  while ((cmd= getopt(argc, argv, "t:vch:p:PT:?ab")) != EOF)
+  while ((cmd= getopt(argc, argv, "qt:vch:p:PT:?ab")) != EOF)
   {
     switch (cmd) {
     case 'a':
       tests.ascii= true;
       tests.binary= false;
       break;
+
     case 'b':
       tests.ascii= false;
       tests.binary= true;
       break;
+
     case 't':
       timeout= atoi(optarg);
       if (timeout == 0)
@@ -1990,18 +2002,29 @@ int main(int argc, char **argv)
         return EXIT_FAILURE;
       }
       break;
+
     case 'v': verbose= true;
       break;
+
     case 'c': do_core= true;
       break;
+
     case 'h': hostname= optarg;
       break;
+
     case 'p': port= optarg;
       break;
+
+    case 'q':
+      close_stdio();
+      break;
+
     case 'P': prompt= true;
       break;
+
     case 'T': testname= optarg;
        break;
+
     default:
       fprintf(stderr, "Usage: %s [-h hostname] [-p port] [-c] [-v] [-t n] [-P] [-T testname]'\n"
               "\t-c\tGenerate coredump if a test fails\n"