diff options
author | Nick Mathewson <nickm@torproject.org> | 2016-09-06 21:01:17 -0400 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2016-09-06 21:01:17 -0400 |
commit | d09723ad19b1f292d31a3f52d8ef8052c8ece309 (patch) | |
tree | e4857ecb09af74466d0002db5ff98becebd8b156 /src/common | |
parent | 2b39c927c7f2c13f17d9883371a12131b6c6df40 (diff) | |
download | tor-d09723ad19b1f292d31a3f52d8ef8052c8ece309.tar.gz tor-d09723ad19b1f292d31a3f52d8ef8052c8ece309.zip |
Add facility to suppress/capture tor_bug_occurred_() messages in unit tests.
Diffstat (limited to 'src/common')
-rw-r--r-- | src/common/util_bug.c | 46 | ||||
-rw-r--r-- | src/common/util_bug.h | 6 |
2 files changed, 52 insertions, 0 deletions
diff --git a/src/common/util_bug.c b/src/common/util_bug.c index e3e1d6df90..f1cd33642e 100644 --- a/src/common/util_bug.c +++ b/src/common/util_bug.c @@ -11,6 +11,44 @@ #include "util_bug.h" #include "torlog.h" #include "backtrace.h" +#include "container.h" + +#ifdef TOR_UNIT_TESTS +static int n_bugs_to_capture = 0; +static smartlist_t *bug_messages = NULL; +#define capturing_bugs() (bug_messages != NULL && n_bugs_to_capture) +void +tor_capture_bugs_(int n) +{ + tor_end_capture_bugs_(); + bug_messages = smartlist_new(); + n_bugs_to_capture = n; +} +void +tor_end_capture_bugs_(void) +{ + n_bugs_to_capture = 0; + if (!bug_messages) + return; + SMARTLIST_FOREACH(bug_messages, char *, cp, tor_free(cp)); + smartlist_free(bug_messages); + bug_messages = NULL; +} +const smartlist_t * +tor_get_captured_bug_log_(void) +{ + return bug_messages; +} +static void +add_captured_bug(const char *s) +{ + --n_bugs_to_capture; + smartlist_add(bug_messages, tor_strdup(s)); +} +#else +#define capturing_bugs() (0) +#define add_captured_bug(s) do { } while (0) +#endif /** Helper for tor_assert: report the assertion failure. */ void @@ -36,12 +74,20 @@ tor_bug_occurred_(const char *fname, unsigned int line, const char *once_str = once ? " (Future instances of this warning will be silenced.)": ""; if (! expr) { + if (capturing_bugs()) { + add_captured_bug("This line should not have been reached."); + return; + } log_warn(LD_BUG, "%s:%u: %s: This line should not have been reached.%s", fname, line, func, once_str); tor_snprintf(buf, sizeof(buf), "Line unexpectedly reached at %s at %s:%u", func, fname, line); } else { + if (capturing_bugs()) { + add_captured_bug(expr); + return; + } log_warn(LD_BUG, "%s:%u: %s: Non-fatal assertion %s failed.%s", fname, line, func, expr, once_str); tor_snprintf(buf, sizeof(buf), diff --git a/src/common/util_bug.h b/src/common/util_bug.h index 8b85d472b6..049ca1a6ef 100644 --- a/src/common/util_bug.h +++ b/src/common/util_bug.h @@ -149,5 +149,11 @@ void tor_bug_occurred_(const char *fname, unsigned int line, const char *func, const char *expr, int once); +#ifdef TOR_UNIT_TESTS +void tor_capture_bugs_(int n); +void tor_end_capture_bugs_(void); +const struct smartlist_t *tor_get_captured_bug_log_(void); +#endif + #endif |