--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;
}
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;
}
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;
}
}
#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;
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;
}
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;
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 */
*mem_out = mem_cached;
return 0;
+ /* LCOV_EXCL_STOP */
}
#if SIZE_MAX != UINT64_MAX
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;
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)
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);
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),