diff options
author | Ola Bini <ola@olabini.se> | 2015-09-15 17:56:56 +0200 |
---|---|---|
committer | Ola Bini <ola@olabini.se> | 2015-09-15 17:56:56 +0200 |
commit | b4950c9334d476049b8b273ad1d8cb15a86f6074 (patch) | |
tree | 8297da8a35881f263c945c32e2b5545bfe64f842 /src | |
parent | a444b11323799536b4cd7902e29f711b0806293a (diff) | |
download | tor-b4950c9334d476049b8b273ad1d8cb15a86f6074.tar.gz tor-b4950c9334d476049b8b273ad1d8cb15a86f6074.zip |
Add tests for procmon. These currently fail. Investigation should happen before submitting
Diffstat (limited to 'src')
-rw-r--r-- | src/test/include.am | 2 | ||||
-rw-r--r-- | src/test/log_test_helpers.c | 95 | ||||
-rw-r--r-- | src/test/log_test_helpers.h | 27 | ||||
-rw-r--r-- | src/test/test.c | 3 | ||||
-rw-r--r-- | src/test/test_procmon.c | 51 |
5 files changed, 177 insertions, 1 deletions
diff --git a/src/test/include.am b/src/test/include.am index f7c0204832..b99adb9f88 100644 --- a/src/test/include.am +++ b/src/test/include.am @@ -51,6 +51,7 @@ src_test_AM_CPPFLAGS = -DSHARE_DATADIR="\"$(datadir)\"" \ # matters a lot there, and is quite hard to debug if you forget to do it. src_test_test_SOURCES = \ + src/test/log_test_helpers.c \ src/test/test.c \ src/test/test_accounting.c \ src/test/test_addr.c \ @@ -84,6 +85,7 @@ src_test_test_SOURCES = \ src/test/test_oom.c \ src/test/test_options.c \ src/test/test_policy.c \ + src/test/test_procmon.c \ src/test/test_pt.c \ src/test/test_relay.c \ src/test/test_relaycell.c \ diff --git a/src/test/log_test_helpers.c b/src/test/log_test_helpers.c new file mode 100644 index 0000000000..42778d4642 --- /dev/null +++ b/src/test/log_test_helpers.c @@ -0,0 +1,95 @@ +#define LOG_PRIVATE +#include "torlog.h" +#include "log_test_helpers.h" + +static smartlist_t *saved_logs = NULL; + +int +setup_capture_of_logs(int new_level) +{ + int previous_log = log_global_min_severity_; + log_global_min_severity_ = new_level; + mock_clean_saved_logs(); + MOCK(logv, mock_saving_logv); + return previous_log; +} + +void +teardown_capture_of_logs(int prev) +{ + UNMOCK(logv); + log_global_min_severity_ = prev; + mock_clean_saved_logs(); +} + +void +mock_clean_saved_logs(void) +{ + if (!saved_logs) + return; + SMARTLIST_FOREACH(saved_logs, mock_saved_log_entry_t *, m, + { tor_free(m->generated_msg); tor_free(m); }); + smartlist_free(saved_logs); + saved_logs = NULL; +} + +char * +mock_saved_log_at(int ix) +{ + int saved_log_count = mock_saved_log_number(); + if(ix < 0) { + ix = saved_log_count + ix; + } + + if (saved_log_count <= ix) + return ""; + return ((mock_saved_log_entry_t *)smartlist_get(saved_logs, ix))->generated_msg; +} + +int +mock_saved_severity_at(int ix) +{ + int saved_log_count = mock_saved_log_number(); + if(ix < 0) { + ix = saved_log_count + ix; + } + + if (saved_log_count <= ix) + return -1; + return ((mock_saved_log_entry_t *)smartlist_get(saved_logs, ix))->severity; +} + +int +mock_saved_log_number(void) +{ + if (!saved_logs) + return 0; + return smartlist_len(saved_logs); +} + +const smartlist_t * +mock_saved_logs(void) +{ + return saved_logs; +} + +void +mock_saving_logv(int severity, log_domain_mask_t domain, const char *funcname, const char *suffix, const char *format, va_list ap) +{ + char *buf = tor_malloc_zero(10240); + int n; + n = tor_vsnprintf(buf,10240,format,ap); + buf[n]='\n'; + buf[n+1]='\0'; + + mock_saved_log_entry_t *e = tor_malloc_zero(sizeof(mock_saved_log_entry_t)); + e->severity = severity; + e->funcname = funcname; + e->suffix = suffix; + e->format = format; + e->generated_msg = buf; + + if (!saved_logs) + saved_logs = smartlist_new(); + smartlist_add(saved_logs, e); +} diff --git a/src/test/log_test_helpers.h b/src/test/log_test_helpers.h new file mode 100644 index 0000000000..789bfe4143 --- /dev/null +++ b/src/test/log_test_helpers.h @@ -0,0 +1,27 @@ +/* Copyright (c) 2014-2015, The Tor Project, Inc. */ +/* See LICENSE for licensing information */ + +#include "or.h" + +#ifndef TOR_LOG_TEST_HELPERS_H +#define TOR_LOG_TEST_HELPERS_H + +typedef struct mock_saved_log_entry_t { + int severity; + const char *funcname; + const char *suffix; + const char *format; + char *generated_msg; + struct mock_saved_log_entry_t *next; +} mock_saved_log_entry_t; + +void mock_saving_logv(int severity, log_domain_mask_t domain, const char *funcname, const char *suffix, const char *format, va_list ap); +void mock_clean_saved_logs(void); +const smartlist_t *mock_saved_logs(void); +int setup_capture_of_logs(int new_level); +void teardown_capture_of_logs(int prev); +char *mock_saved_log_at(int ix); +int mock_saved_severity_at(int ix); +int mock_saved_log_number(void); + +#endif diff --git a/src/test/test.c b/src/test/test.c index e10e260266..23e41b7fba 100644 --- a/src/test/test.c +++ b/src/test/test.c @@ -1145,6 +1145,7 @@ extern struct testcase_t nodelist_tests[]; extern struct testcase_t oom_tests[]; extern struct testcase_t options_tests[]; extern struct testcase_t policy_tests[]; +extern struct testcase_t procmon_tests[]; extern struct testcase_t pt_tests[]; extern struct testcase_t relay_tests[]; extern struct testcase_t relaycell_tests[]; @@ -1192,6 +1193,7 @@ struct testgroup_t testgroups[] = { { "oom/", oom_tests }, { "options/", options_tests }, { "policy/" , policy_tests }, + { "procmon/", procmon_tests }, { "pt/", pt_tests }, { "relay/" , relay_tests }, { "relaycell/", relaycell_tests }, @@ -1208,4 +1210,3 @@ struct testgroup_t testgroups[] = { { "dns/", dns_tests }, END_OF_GROUPS }; - diff --git a/src/test/test_procmon.c b/src/test/test_procmon.c new file mode 100644 index 0000000000..ac70a0f3db --- /dev/null +++ b/src/test/test_procmon.c @@ -0,0 +1,51 @@ +/* Copyright (c) 2010-2015, The Tor Project, Inc. */ +/* See LICENSE for licensing information */ + +#define PROCMON_PRIVATE +#include "orconfig.h" +#include "or.h" +#include "test.h" + +#include "procmon.h" + +#include "log_test_helpers.h" + +#define NS_MODULE procmon + +struct event_base; + +static void +test_procmon_tor_process_monitor_new(void *ignored) +{ + (void)ignored; + tor_process_monitor_t *res; + const char *msg; + + res = tor_process_monitor_new(NULL, "probably invalid", 0, NULL, NULL, &msg); + tt_assert(!res); + tt_str_op(msg, OP_EQ, "invalid PID"); + + res = tor_process_monitor_new(NULL, "243443535345454", 0, NULL, NULL, &msg); + tt_assert(!res); + tt_str_op(msg, OP_EQ, "invalid PID"); + + res = tor_process_monitor_new(tor_libevent_get_base(), "43", 0, NULL, NULL, &msg); + tt_assert(res); + tt_assert(!msg); + + res = tor_process_monitor_new(tor_libevent_get_base(), "44 hello", 0, NULL, NULL, &msg); + tt_assert(res); + tt_assert(!msg); + + res = tor_process_monitor_new(tor_libevent_get_base(), "45:hello", 0, NULL, NULL, &msg); + tt_assert(res); + tt_assert(!msg); + + done: + (void)0; +} + +struct testcase_t procmon_tests[] = { + { "tor_process_monitor_new", test_procmon_tor_process_monitor_new, TT_FORK, NULL, NULL }, + END_OF_TESTCASES +}; |