[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [tor/master] Small coverage improvements on compat.c



commit 603cb712ef756dd700a52e837bcd643a96311ad6
Author: Nick Mathewson <nickm@xxxxxxxxxxxxxx>
Date:   Mon Jun 20 11:03:13 2016 -0400

    Small coverage improvements on compat.c
---
 src/common/compat.c  | 21 ++++++++++++++-------
 src/test/test_util.c | 46 +++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 59 insertions(+), 8 deletions(-)

diff --git a/src/common/compat.c b/src/common/compat.c
index 24543cd..bae76f4 100644
--- a/src/common/compat.c
+++ b/src/common/compat.c
@@ -1145,14 +1145,12 @@ tor_close_socket(tor_socket_t s)
       --n_sockets_open;
 #else
     if (r != EBADF)
-      --n_sockets_open;
+      --n_sockets_open; // LCOV_EXCL_LINE -- EIO and EINTR too hard to force.
 #endif
     r = -1;
   }
 
-  if (n_sockets_open < 0)
-    log_warn(LD_BUG, "Our socket count is below zero: %d. Please submit a "
-             "bug report.", n_sockets_open);
+  tor_assert_nonfatal(n_sockets_open >= 0);
   socket_accounting_unlock();
   return r;
 }
@@ -1932,7 +1930,7 @@ tor_getpwnam(const char *username)
     return NULL;
 
   if (! strcmp(username, passwd_cached->pw_name))
-    return passwd_cached;
+    return passwd_cached; // LCOV_EXCL_LINE - would need to make getpwnam flaky
 
   return NULL;
 }
@@ -1958,7 +1956,7 @@ tor_getpwuid(uid_t uid)
     return NULL;
 
   if (uid == passwd_cached->pw_uid)
-    return passwd_cached;
+    return passwd_cached; // LCOV_EXCL_LINE - would need to make getpwnam flaky
 
   return NULL;
 }
@@ -2750,7 +2748,9 @@ MOCK_IMPL(const char *, get_uname, (void))
       }
 #endif
 #else
+        /* LCOV_EXCL_START -- can't provoke uname failure */
         strlcpy(uname_result, "Unknown platform", sizeof(uname_result));
+        /* LCOV_EXCL_STOP */
 #endif
       }
     uname_result_is_set = 1;
@@ -2824,11 +2824,14 @@ compute_num_cpus(void)
   if (num_cpus == -2) {
     num_cpus = compute_num_cpus_impl();
     tor_assert(num_cpus != -2);
-    if (num_cpus > MAX_DETECTABLE_CPUS)
+    if (num_cpus > MAX_DETECTABLE_CPUS) {
+      /* LCOV_EXCL_START */
       log_notice(LD_GENERAL, "Wow!  I detected that you have %d CPUs. I "
                  "will not autodetect any more than %d, though.  If you "
                  "want to configure more, set NumCPUs in your torrc",
                  num_cpus, MAX_DETECTABLE_CPUS);
+      /* LCOV_EXCL_STOP */
+    }
   }
   return num_cpus;
 }
@@ -3358,9 +3361,11 @@ get_total_system_memory_impl(void)
   return result * 1024;
 
  err:
+  /* LCOV_EXCL_START Can't reach this unless proc is broken. */
   tor_free(s);
   close(fd);
   return 0;
+  /* LCOV_EXCL_STOP */
 #elif defined (_WIN32)
   /* Windows has MEMORYSTATUSEX; pretty straightforward. */
   MEMORYSTATUSEX ms;
@@ -3409,6 +3414,7 @@ get_total_system_memory(size_t *mem_out)
   static size_t mem_cached=0;
   uint64_t m = get_total_system_memory_impl();
   if (0 == m) {
+    /* LCOV_EXCL_START -- can't make this happen without mocking. */
     /* We couldn't find our memory total */
     if (0 == mem_cached) {
       /* We have no cached value either */
@@ -3418,6 +3424,7 @@ get_total_system_memory(size_t *mem_out)
 
     *mem_out = mem_cached;
     return 0;
+    /* LCOV_EXCL_STOP */
   }
 
 #if SIZE_MAX != UINT64_MAX
diff --git a/src/test/test_util.c b/src/test/test_util.c
index 9086437..56c71e8 100644
--- a/src/test/test_util.c
+++ b/src/test/test_util.c
@@ -1951,7 +1951,6 @@ test_util_gzip_compression_bomb(void *arg)
                                            compression_bomb, 1039,
                                            ZLIB_METHOD, 0, LOG_WARN));
 
-
   /* Now try streaming that. */
   state = tor_zlib_new(0, ZLIB_METHOD, HIGH_COMPRESSION);
   tor_zlib_output_t r;
@@ -3400,6 +3399,21 @@ test_util_ftruncate(void *ptr)
   tor_free(buf);
 }
 
+static void
+test_util_num_cpus(void *arg)
+{
+  (void)arg;
+  int num = compute_num_cpus();
+  if (num < 0)
+    tt_skip();
+
+  tt_int_op(num, OP_GE, 1);
+  tt_int_op(num, OP_LE, 16);
+
+ done:
+  ;
+}
+
 #ifdef _WIN32
 static void
 test_util_load_win_lib(void *ptr)
@@ -4921,6 +4935,35 @@ test_util_pwdb(void *arg)
   dir = get_user_homedir(name);
   tt_assert(dir != NULL);
 
+  /* Try failing cases.  First find a user that doesn't exist by name */
+  char rand[4];
+  char badname[9];
+  int i, found=0;
+  for (i = 0; i < 100; ++i) {
+    crypto_rand(rand, sizeof(rand));
+    base16_encode(badname, sizeof(badname), rand, sizeof(rand));
+    if (tor_getpwnam(badname) == NULL) {
+      found = 1;
+      break;
+    }
+  }
+  tt_assert(found);
+  tor_free(dir);
+  dir = get_user_homedir(badname);
+  tt_assert(dir == NULL);
+
+  /* Now try to find a user that doesn't exist by ID. */
+  found = 0;
+  for (i = 0; i < 1000; ++i) {
+    uid_t u;
+    crypto_rand((char*)&u, sizeof(u));
+    if (tor_getpwuid(u) == NULL) {
+      found = 1;
+      break;
+    }
+  }
+  tt_assert(found);
+
  done:
   tor_free(name);
   tor_free(dir);
@@ -5006,6 +5049,7 @@ struct testcase_t util_tests[] = {
   UTIL_TEST(listdir, 0),
   UTIL_TEST(parent_dir, 0),
   UTIL_TEST(ftruncate, 0),
+  UTIL_TEST(num_cpus, 0),
   UTIL_TEST_WIN_ONLY(load_win_lib, 0),
   UTIL_TEST_NO_WIN(exit_status, 0),
   UTIL_TEST_NO_WIN(fgets_eagain, 0),

_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits