Merge in fixes for SASL.
[awesomized/libmemcached] / libtest / unittest.cc
index 1e3241ed82bc165a1cb0a05dfea4225a3b2cccc8..d2c965bac504641e3b042abab78041f3219855ef 100644 (file)
@@ -1,48 +1,58 @@
 /*  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  * 
- *  uTest self unit test.
+ *  libtest
  *
  *  Copyright (C) 2011 Data Differential, http://datadifferential.com/
- *  All rights reserved.
  *
- *  Redistribution and use in source and binary forms, with or without
- *  modification, are permitted provided that the following conditions are
- *  met:
+ *  This library is free software; you can redistribute it and/or
+ *  modify it under the terms of the GNU Lesser General Public
+ *  License as published by the Free Software Foundation; either
+ *  version 3 of the License, or (at your option) any later version.
  *
- *      * Redistributions of source code must retain the above copyright
- *  notice, this list of conditions and the following disclaimer.
- *
- *      * Redistributions in binary form must reproduce the above
- *  copyright notice, this list of conditions and the following disclaimer
- *  in the documentation and/or other materials provided with the
- *  distribution.
- *
- *      * The names of its contributors may not be used to endorse or
- *  promote products derived from this software without specific prior
- *  written permission.
- *
- *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *  This library is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ *  Lesser General Public License for more details.
  *
+ *  You should have received a copy of the GNU Lesser General Public
+ *  License along with this library; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  */
 
+
 #include <config.h>
 
 #include <libtest/test.hpp>
 
 #include <cstdlib>
+#include <unistd.h>
 
 using namespace libtest;
 
+static test_return_t LIBTOOL_COMMAND_test(void *)
+{
+  test_true(getenv("LIBTOOL_COMMAND"));
+  return TEST_SUCCESS;
+}
+
+static test_return_t VALGRIND_COMMAND_test(void *)
+{
+  test_true(getenv("VALGRIND_COMMAND"));
+  return TEST_SUCCESS;
+}
+
+static test_return_t HELGRIND_COMMAND_test(void *)
+{
+  test_true(getenv("HELGRIND_COMMAND"));
+  return TEST_SUCCESS;
+}
+
+static test_return_t GDB_COMMAND_test(void *)
+{
+  test_true(getenv("GDB_COMMAND"));
+  return TEST_SUCCESS;
+}
+
 static test_return_t test_success_test(void *)
 {
   return TEST_SUCCESS;
@@ -103,28 +113,88 @@ static test_return_t local_not_test(void *)
   return TEST_SUCCESS;
 }
 
-#if 0
-static test_return_t pause_test(void *)
+static test_return_t var_exists_test(void *)
 {
-  (void)getchar();
+  test_compare(0, access("var", R_OK | W_OK | X_OK));
+  return TEST_SUCCESS;
+}
+
+static test_return_t var_tmp_exists_test(void *)
+{
+  test_compare(0, access("var/tmp", R_OK | W_OK | X_OK));
+  return TEST_SUCCESS;
+}
+
+static test_return_t var_run_exists_test(void *)
+{
+  test_compare(0, access("var/run", R_OK | W_OK | X_OK));
+  return TEST_SUCCESS;
+}
+
+static test_return_t var_log_exists_test(void *)
+{
+  test_compare(0, access("var/log", R_OK | W_OK | X_OK));
+  return TEST_SUCCESS;
+}
+
+static test_return_t var_tmp_test(void *)
+{
+  FILE *file= fopen("var/tmp/junk", "w+");
+  char buffer[1024];
+  const char *dir= getcwd(buffer, sizeof(buffer));
+  test_true_got(file, dir);
+  fclose(file);
   return TEST_SUCCESS;
 }
-#endif
 
+static test_return_t var_run_test(void *)
+{
+  FILE *file= fopen("var/run/junk", "w+");
+  test_true(file);
+  fclose(file);
+  return TEST_SUCCESS;
+}
+
+static test_return_t var_log_test(void *)
+{
+  FILE *file= fopen("var/log/junk", "w+");
+  test_true(file);
+  fclose(file);
+  return TEST_SUCCESS;
+}
+
+static test_return_t var_tmp_rm_test(void *)
+{
+  test_true(unlink("var/tmp/junk") == 0);
+  return TEST_SUCCESS;
+}
+
+static test_return_t var_run_rm_test(void *)
+{
+  test_true(unlink("var/run/junk") == 0);
+  return TEST_SUCCESS;
+}
+
+static test_return_t var_log_rm_test(void *)
+{
+  test_true(unlink("var/log/junk") == 0);
+  return TEST_SUCCESS;
+}
 
 static test_return_t gearmand_cycle_test(void *object)
 {
   server_startup_st *servers= (server_startup_st*)object;
   test_true(servers);
 
-#ifndef HAVE_LIBGEARMAN
-  return TEST_SKIPPED;
-#endif
+  if (HAVE_LIBGEARMAN)
+  {
+    const char *argv[1]= { "cycle_gearmand" };
+    test_true(server_startup(*servers, "gearmand", 9999, 1, argv));
 
-  const char *argv[1]= { "cycle_gearmand" };
-  test_true(server_startup(*servers, "gearmand", 9999, 1, argv));
+    return TEST_SUCCESS;
+  }
 
-  return TEST_SUCCESS;
+  return TEST_SKIPPED;
 }
 
 static test_return_t memcached_cycle_test(void *object)
@@ -132,14 +202,15 @@ static test_return_t memcached_cycle_test(void *object)
   server_startup_st *servers= (server_startup_st*)object;
   test_true(servers);
 
-#if !defined(MEMCACHED_BINARY) && !defined(HAVE_LIBMEMCACHED)
-  return TEST_SKIPPED;
-#endif
+  if (MEMCACHED_BINARY and HAVE_LIBMEMCACHED) 
+  {
+    const char *argv[1]= { "cycle_memcached" };
+    test_true(server_startup(*servers, "memcached", 9998, 1, argv));
 
-  const char *argv[1]= { "cycle_memcached" };
-  test_true(server_startup(*servers, "memcached", 9998, 1, argv));
+    return TEST_SUCCESS;
+  }
 
-  return TEST_SUCCESS;
+  return TEST_SKIPPED;
 }
 
 static test_return_t memcached_socket_cycle_test(void *object)
@@ -147,14 +218,15 @@ static test_return_t memcached_socket_cycle_test(void *object)
   server_startup_st *servers= (server_startup_st*)object;
   test_true(servers);
 
-#if !defined(MEMCACHED_BINARY) && !defined(HAVE_LIBMEMCACHED)
-  return TEST_SKIPPED;
-#endif
+  if (MEMCACHED_BINARY and HAVE_LIBMEMCACHED)
+  {
+    const char *argv[1]= { "cycle_memcached" };
+    test_true(servers->start_socket_server("memcached", 9997, 1, argv));
 
-  const char *argv[1]= { "cycle_memcached" };
-  test_true(servers->start_socket_server("memcached", 9997, 1, argv));
+    return TEST_SUCCESS;
+  }
 
-  return TEST_SUCCESS;
+  return TEST_SKIPPED;
 }
 
 test_st gearmand_tests[] ={
@@ -171,6 +243,14 @@ test_st memcached_tests[] ={
   {0, 0, 0}
 };
 
+test_st environment_tests[] ={
+  {"LIBTOOL_COMMAND", 0, LIBTOOL_COMMAND_test },
+  {"VALGRIND_COMMAND", 0, VALGRIND_COMMAND_test },
+  {"HELGRIND_COMMAND", 0, HELGRIND_COMMAND_test },
+  {"GDB_COMMAND", 0, GDB_COMMAND_test },
+  {0, 0, 0}
+};
+
 test_st tests_log[] ={
   {"TEST_SUCCESS", 0, test_success_test },
   {"TEST_FAILURE", 0, test_failure_test },
@@ -183,9 +263,25 @@ test_st local_log[] ={
   {0, 0, 0}
 };
 
+test_st directories_tests[] ={
+  {"var exists", 0, var_exists_test },
+  {"var/tmp exists", 0, var_tmp_exists_test },
+  {"var/run exists", 0, var_run_exists_test },
+  {"var/log exists", 0, var_log_exists_test },
+  {"var/tmp", 0, var_tmp_test },
+  {"var/run", 0, var_run_test },
+  {"var/log", 0, var_log_test },
+  {"var/tmp rm", 0, var_tmp_rm_test },
+  {"var/run rm", 0, var_run_rm_test },
+  {"var/log rm", 0, var_log_rm_test },
+  {0, 0, 0}
+};
+
 collection_st collection[] ={
+  {"environment", 0, 0, environment_tests},
   {"return values", 0, 0, tests_log},
   {"local", 0, 0, local_log},
+  {"directories", 0, 0, directories_tests},
   {"gearmand", 0, 0, gearmand_tests},
   {"memcached", 0, 0, memcached_tests},
   {0, 0, 0, 0}