diff options
author | Nick Mathewson <nickm@torproject.org> | 2018-08-06 17:15:10 -0400 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2018-08-21 12:14:41 -0400 |
commit | fe00a481fc4a6a514c46978b9476684398933336 (patch) | |
tree | c1d84aca78bb7d3018003a95526425b336b284bc /src/test/test_util.c | |
parent | f124037cdeac666e7e76a47f8aef2108b40672b4 (diff) | |
download | tor-fe00a481fc4a6a514c46978b9476684398933336.tar.gz tor-fe00a481fc4a6a514c46978b9476684398933336.zip |
Add a unit test for tor_log_mallinfo()
Diffstat (limited to 'src/test/test_util.c')
-rw-r--r-- | src/test/test_util.c | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/src/test/test_util.c b/src/test/test_util.c index c3c0e6df3b..b395110cdf 100644 --- a/src/test/test_util.c +++ b/src/test/test_util.c @@ -6262,6 +6262,57 @@ test_util_get_unquoted_path(void *arg) tor_free(r); } +static void +test_util_log_mallinfo(void *arg) +{ + (void)arg; + char *log1 = NULL, *log2 = NULL, *mem = NULL; +#ifdef HAVE_MALLINFO + setup_capture_of_logs(LOG_INFO); + tor_log_mallinfo(LOG_INFO); + expect_single_log_msg_containing("mallinfo() said: "); + mock_saved_log_entry_t *lg = smartlist_get(mock_saved_logs(), 0); + log1 = tor_strdup(lg->generated_msg); + + mock_clean_saved_logs(); + mem = tor_malloc(8192); + tor_log_mallinfo(LOG_INFO); + expect_single_log_msg_containing("mallinfo() said: "); + lg = smartlist_get(mock_saved_logs(), 0); + log2 = tor_strdup(lg->generated_msg); + + /* Make sure that the amount of used memory increased. */ + const char *used1 = strstr(log1, "uordblks="); + const char *used2 = strstr(log2, "uordblks="); + tt_assert(used1); + tt_assert(used2); + used1 += strlen("uordblks="); + used2 += strlen("uordblks="); + + int ok1, ok2; + char *next1 = NULL, *next2 = NULL; + uint64_t mem1 = tor_parse_uint64(used1, 10, 0, UINT64_MAX, &ok1, &next1); + uint64_t mem2 = tor_parse_uint64(used2, 10, 0, UINT64_MAX, &ok2, &next2); + tt_assert(ok1); + tt_assert(ok2); + tt_assert(next1); + tt_assert(next2); + if (mem2 == 0) { + /* This is a fake mallinfo that doesn't actually fill in its outputs. */ + tt_int_op(mem1, OP_EQ, 0); + } else { + tt_u64_op(mem1, OP_LT, mem2); + } +#else + tt_skip(); +#endif + done: + teardown_capture_of_logs(); + tor_free(log1); + tor_free(log2); + tor_free(mem); +} + #define UTIL_LEGACY(name) \ { #name, test_util_ ## name , 0, NULL, NULL } @@ -6399,5 +6450,6 @@ struct testcase_t util_tests[] = { UTIL_TEST(monotonic_time_add_msec, 0), UTIL_TEST(htonll, 0), UTIL_TEST(get_unquoted_path, 0), + UTIL_TEST(log_mallinfo, 0), END_OF_TESTCASES }; |