diff options
author | Ola Bini <ola@olabini.se> | 2015-09-15 17:20:44 +0200 |
---|---|---|
committer | Ola Bini <ola@olabini.se> | 2015-09-15 17:20:44 +0200 |
commit | 9985a62a67a16f8985dc0002ce0480a7cae070e3 (patch) | |
tree | 532e8bfe0844deb3c6e8b8128d283132a45f97d9 /src/test/log_test_helpers.c | |
parent | a444b11323799536b4cd7902e29f711b0806293a (diff) | |
download | tor-9985a62a67a16f8985dc0002ce0480a7cae070e3.tar.gz tor-9985a62a67a16f8985dc0002ce0480a7cae070e3.zip |
Add tests for compat_libevent
Diffstat (limited to 'src/test/log_test_helpers.c')
-rw-r--r-- | src/test/log_test_helpers.c | 95 |
1 files changed, 95 insertions, 0 deletions
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); +} |