Fix all include location, and drop versions of the library that were never shipped.
[awesomized/libmemcached] / clients / memcapable.cc
index baac28b2484f9c9983334a2b3cb06500d484855e..2026aceec9a4139ecd4a7190c256fa101597c543 100644 (file)
@@ -1,4 +1,5 @@
 /* LibMemcached
+ * Copyright (C) 2011-2012 Data Differential, http://datadifferential.com/
  * Copyright (C) 2006-2009 Brian Aker
  * All rights reserved.
  *
 /* -*- 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/memcached/protocol_binary.h>
-#include <libmemcached/byteorder.h>
-#include "utilities.h"
+#include <libmemcached-1.0/memcached.h>
+
+#include "libmemcached/socket.hpp"
+#include "libmemcached/memcached/protocol_binary.h"
+#include "libmemcached/byteorder.h"
+#include "clients/utilities.h"
+
+#include <vector>
 
 #ifdef linux
 /* /usr/include/netinet/in.h defines macros from ntohs() to _bswap_nn to
@@ -126,7 +137,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 +146,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;
     }
   }
@@ -237,10 +248,14 @@ static enum test_return ensure(bool val, const char *expression, const char *fil
   if (!val)
   {
     if (verbose)
+    {
       fprintf(stderr, "\n%s:%d: %s", file, line, expression);
+    }
 
     if (do_core)
+    {
       abort();
+    }
 
     return TEST_FAIL;
   }
@@ -264,9 +279,14 @@ static enum test_return retry_write(const void* buf, size_t len)
     size_t num_bytes= len - offset;
     ssize_t nw= timeout_io_op(sock, POLLOUT, (void*)(ptr + offset), num_bytes);
     if (nw == -1)
+    {
       verify(get_socket_errno() == EINTR || get_socket_errno() == EAGAIN);
+    }
     else
+    {
       offset+= (size_t)nw;
+    }
+
   } while (offset < len);
 
   return TEST_PASS;
@@ -279,7 +299,7 @@ static enum test_return retry_write(const void* buf, size_t len)
 static enum test_return resend_packet(command *cmd)
 {
   size_t length= sizeof (protocol_binary_request_no_extras) +
-          ntohl(cmd->plain.message.header.request.bodylen);
+    ntohl(cmd->plain.message.header.request.bodylen);
 
   execute(retry_write(cmd, length));
   return TEST_PASS;
@@ -297,7 +317,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;
@@ -317,8 +337,10 @@ static enum test_return retry_read(void *buf, size_t len)
        fprintf(stderr, "Errno: %d %s\n", get_socket_errno(), strerror(errno));
       verify(get_socket_errno() == EINTR || get_socket_errno() == EAGAIN);
       break;
+
     case 0:
       return TEST_FAIL;
+
     default:
       offset+= (size_t)nr;
     }
@@ -343,7 +365,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 +494,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,16 +677,20 @@ 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 */
   for (int ii= 0; ii < 10; ii++)
   {
     if (ii == 0)
+    {
       execute(send_packet(&cmd));
+    }
     else
+    {
       execute(resend_packet(&cmd));
+    }
 
     if (cc == PROTOCOL_BINARY_CMD_SET)
     {
@@ -690,8 +716,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 +727,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 +751,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 +810,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
@@ -794,17 +818,25 @@ static enum test_return test_binary_replace_impl(const char* key, uint8_t cc)
   for (int ii= 0; ii < 10; ii++)
   {
     if (ii == 0)
+    {
       execute(send_packet(&cmd));
+    }
     else
+    {
       execute(resend_packet(&cmd));
+    }
 
     if (cc == PROTOCOL_BINARY_CMD_REPLACE || ii == 0)
     {
       uint16_t expected_result;
       if (ii == 0)
+      {
         expected_result=PROTOCOL_BINARY_RESPONSE_KEY_ENOENT;
+      }
       else
+      {
         expected_result=PROTOCOL_BINARY_RESPONSE_SUCCESS;
+      }
 
       execute(send_binary_noop());
       execute(recv_packet(&rsp));
@@ -815,12 +847,13 @@ static enum test_return test_binary_replace_impl(const char* key, uint8_t cc)
         execute(binary_set_item(key, key));
     }
     else
+    {
       execute(test_binary_noop());
+    }
   }
 
   /* 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 +865,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 +988,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 +1026,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 +1038,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
   {
@@ -1091,16 +1123,24 @@ static enum test_return test_binary_concat_impl(const char *key, uint8_t cc)
   const char *value;
 
   if (cc == PROTOCOL_BINARY_CMD_APPEND || cc == PROTOCOL_BINARY_CMD_APPENDQ)
+  {
     value="hello";
+  }
   else
+  {
     value=" world";
+  }
 
   execute(binary_set_item(key, value));
 
   if (cc == PROTOCOL_BINARY_CMD_APPEND || cc == PROTOCOL_BINARY_CMD_APPENDQ)
+  {
     value=" world";
+  }
   else
+  {
     value="hello";
+  }
 
   raw_command(&cmd, cc, key, strlen(key), value, strlen(value));
   execute(send_packet(&cmd));
@@ -1110,7 +1150,9 @@ static enum test_return test_binary_concat_impl(const char *key, uint8_t cc)
     verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_SUCCESS));
   }
   else
+  {
     execute(test_binary_noop());
+  }
 
   raw_command(&cmd, PROTOCOL_BINARY_CMD_GET, key, strlen(key), NULL, 0);
   execute(send_packet(&cmd));
@@ -1282,7 +1324,9 @@ static enum test_return test_ascii_set_impl(const char* key, bool noreply)
   execute(send_string(buffer));
 
   if (!noreply)
+  {
     execute(receive_response("STORED\r\n"));
+  }
 
   return test_ascii_version();
 }
@@ -1305,12 +1349,16 @@ static enum test_return test_ascii_add_impl(const char* key, bool noreply)
   execute(send_string(buffer));
 
   if (!noreply)
+  {
     execute(receive_response("STORED\r\n"));
+  }
 
   execute(send_string(buffer));
 
   if (!noreply)
+  {
     execute(receive_response("NOT_STORED\r\n"));
+  }
 
   return test_ascii_version();
 }
@@ -1333,7 +1381,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 +1396,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 +1432,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);
@@ -1400,14 +1453,18 @@ static enum test_return ascii_get_item(const char *key, const char *value,
   char buffer[1024];
   size_t datasize= 0;
   if (value != NULL)
+  {
     datasize= strlen(value);
+  }
 
   verify(datasize < sizeof(buffer));
   snprintf(buffer, sizeof(buffer), "get %s\r\n", key);
   execute(send_string(buffer));
 
   if (exist)
+  {
     execute(ascii_get_value(key, value));
+  }
 
   execute(retry_read(buffer, 5));
   verify(memcmp(buffer, "END\r\n", 5) == 0);
@@ -1442,9 +1499,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);
@@ -1461,7 +1520,9 @@ static enum test_return ascii_gets_item(const char *key, const char *value,
   char buffer[1024];
   size_t datasize= 0;
   if (value != NULL)
+  {
     datasize= strlen(value);
+  }
 
   verify(datasize < sizeof(buffer));
   snprintf(buffer, sizeof(buffer), "gets %s\r\n", key);
@@ -1495,9 +1556,13 @@ static enum test_return test_ascii_replace_impl(const char* key, bool noreply)
   execute(send_string(buffer));
 
   if (noreply)
+  {
     execute(test_ascii_version());
+  }
   else
+  {
     execute(receive_response("NOT_STORED\r\n"));
+  }
 
   execute(ascii_set_item(key, "value"));
   execute(ascii_get_item(key, "value", true));
@@ -1535,17 +1600,25 @@ static enum test_return test_ascii_cas_impl(const char* key, bool noreply)
   execute(send_string(buffer));
 
   if (noreply)
+  {
     execute(test_ascii_version());
+  }
   else
+  {
     execute(receive_response("STORED\r\n"));
+  }
 
   /* reexecute the same command should fail due to illegal cas */
   execute(send_string(buffer));
 
   if (noreply)
+  {
     execute(test_ascii_version());
+  }
   else
+  {
     execute(receive_response("EXISTS\r\n"));
+  }
 
   return test_ascii_version();
 }
@@ -1637,14 +1710,17 @@ static enum test_return test_ascii_mget(void)
   };
 
   for (uint32_t x= 0; x < nkeys; ++x)
+  {
     execute(ascii_set_item(keys[x], "value"));
+  }
 
   /* Ask for a key that doesn't exist as well */
   execute(send_string("get test_ascii_mget1 test_ascii_mget2 test_ascii_mget3 "
                       "test_ascii_mget4 test_ascii_mget5 "
                       "test_ascii_mget6\r\n"));
 
-  char *returned[nkeys];
+  std::vector<char *> returned;
+  returned.resize(nkeys);
 
   for (uint32_t x= 0; x < nkeys; ++x)
   {
@@ -1676,7 +1752,9 @@ static enum test_return test_ascii_mget(void)
   }
 
   for (uint32_t x= 0; x < nkeys; ++x)
+  {
     free(returned[x]);
+  }
 
   return TEST_PASS;
 }
@@ -1728,7 +1806,9 @@ static enum test_return test_ascii_decr_impl(const char* key, bool noreply)
     execute(send_string(cmd));
 
     if (noreply)
+    {
       execute(test_ascii_version());
+    }
     else
     {
       char buffer[80];
@@ -1743,7 +1823,9 @@ static enum test_return test_ascii_decr_impl(const char* key, bool noreply)
   /* verify that it doesn't wrap */
   execute(send_string(cmd));
   if (noreply)
+  {
     execute(test_ascii_version());
+  }
   else
   {
     char buffer[80];
@@ -1817,9 +1899,13 @@ static enum test_return test_ascii_concat_impl(const char *key,
   execute(ascii_set_item(key, value));
 
   if (append)
+  {
     value=" world";
+  }
   else
+  {
     value="hello";
+  }
 
   char cmd[400];
   snprintf(cmd, sizeof(cmd), "%s %s 0 0 %u%s\r\n%s\r\n",
@@ -1829,9 +1915,13 @@ static enum test_return test_ascii_concat_impl(const char *key,
   execute(send_string(cmd));
 
   if (noreply)
+  {
     execute(test_ascii_version());
+  }
   else
+  {
     execute(receive_response("STORED\r\n"));
+  }
 
   execute(ascii_get_item(key, "hello world", true));
 
@@ -1842,9 +1932,13 @@ static enum test_return test_ascii_concat_impl(const char *key,
   execute(send_string(cmd));
 
   if (noreply)
+  {
     execute(test_ascii_version());
+  }
   else
+  {
     execute(receive_response("NOT_STORED\r\n"));
+  }
 
   return TEST_PASS;
 }
@@ -1971,17 +2065,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 +2086,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"
@@ -2015,7 +2122,7 @@ int main(int argc, char **argv)
               "\t-a\tOnly test the ascii protocol\n"
               "\t-b\tOnly test the binary protocol\n",
               argv[0]);
-      return EXIT_FAILURE;
+      return EXIT_SUCCESS;
     }
   }
 
@@ -2031,7 +2138,9 @@ int main(int argc, char **argv)
   for (int ii= 0; testcases[ii].description != NULL; ++ii)
   {
     if (testname != NULL && strcmp(testcases[ii].description, testname) != 0)
-       continue;
+    {
+      continue;
+    }
 
     if ((testcases[ii].description[0] == 'a' && (tests.ascii) == 0) ||
         (testcases[ii].description[0] == 'b' && (tests.binary) == 0))
@@ -2055,7 +2164,9 @@ int main(int argc, char **argv)
           continue;
         }
         if (strncmp(buffer, "quit", 4) == 0)
-          exit(0);
+        {
+          exit(EXIT_SUCCESS);
+        }
       }
 
       fprintf(stdout, "%-40s", testcases[ii].description);
@@ -2080,8 +2191,7 @@ int main(int argc, char **argv)
       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, port, strerror(get_socket_errno()));
         fprintf(stderr, "%d of %d tests failed\n", failed, total);
         return EXIT_FAILURE;
       }
@@ -2090,9 +2200,13 @@ int main(int argc, char **argv)
 
   closesocket(sock);
   if (failed == 0)
+  {
     fprintf(stdout, "All tests passed\n");
+  }
   else
+  {
     fprintf(stderr, "%d of %d tests failed\n", failed, total);
+  }
 
-  return (failed == 0) ? 0 : 1;
+  return (failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
 }