Merge branch 'maint-0.4.5' into release-0.4.5
[tor.git] / src / test / log_test_helpers.h
blobc2d71c6bcd70b56c0d2df1d7bfc8900bd83636f2
1 /* Copyright (c) 2014-2020, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
4 #include "core/or/or.h"
6 #ifndef TOR_LOG_TEST_HELPERS_H
7 #define TOR_LOG_TEST_HELPERS_H
9 /** An element of mock_saved_logs(); records the log element that we
10 * received. */
11 typedef struct mock_saved_log_entry_t {
12 int severity;
13 const char *funcname;
14 const char *suffix;
15 const char *format;
16 char *generated_msg;
17 } mock_saved_log_entry_t;
19 void mock_clean_saved_logs(void);
20 const smartlist_t *mock_saved_logs(void);
21 void setup_capture_of_logs(int new_level);
22 void setup_full_capture_of_logs(int new_level);
23 void teardown_capture_of_logs(void);
25 int mock_saved_log_has_message(const char *msg);
26 int mock_saved_log_has_message_containing(const char *msg);
27 int mock_saved_log_has_message_not_containing(const char *msg);
28 int mock_saved_log_has_severity(int severity);
29 int mock_saved_log_has_entry(void);
30 int mock_saved_log_n_entries(void);
31 void mock_dump_saved_logs(void);
33 #define assert_log_predicate(predicate, failure_msg) \
34 do { \
35 if (!(predicate)) { \
36 TT_FAIL(failure_msg); \
37 mock_dump_saved_logs(); \
38 TT_EXIT_TEST_FUNCTION; \
39 } \
40 } while (0)
42 #define expect_log_msg(str) \
43 assert_log_predicate(mock_saved_log_has_message(str), \
44 ("expected log to contain \"%s\"", str));
46 #define expect_log_msg_containing(str) \
47 assert_log_predicate(mock_saved_log_has_message_containing(str), \
48 ("expected log to contain \"%s\"", str));
50 #define expect_log_msg_not_containing(str) \
51 assert_log_predicate(mock_saved_log_has_message_not_containing(str), \
52 ("expected log to not contain \"%s\"", str));
54 #define expect_log_msg_containing_either(str1, str2) \
55 assert_log_predicate(mock_saved_log_has_message_containing(str1) || \
56 mock_saved_log_has_message_containing(str2), \
57 ("expected log to contain \"%s\" or \"%s\"", str1, str2));
59 #define expect_log_msg_containing_either3(str1, str2, str3) \
60 assert_log_predicate(mock_saved_log_has_message_containing(str1) || \
61 mock_saved_log_has_message_containing(str2) || \
62 mock_saved_log_has_message_containing(str3), \
63 ("expected log to contain \"%s\" or \"%s\" or \"%s\"", \
64 str1, str2, str3))
66 #define expect_log_msg_containing_either4(str1, str2, str3, str4) \
67 assert_log_predicate(mock_saved_log_has_message_containing(str1) || \
68 mock_saved_log_has_message_containing(str2) || \
69 mock_saved_log_has_message_containing(str3) || \
70 mock_saved_log_has_message_containing(str4), \
71 ("expected log to contain \"%s\" or \"%s\" or \"%s\" or \"%s\"", \
72 str1, str2, str3, str4))
74 #define expect_single_log_msg(str) \
75 do { \
77 assert_log_predicate(mock_saved_log_has_message_containing(str) && \
78 mock_saved_log_n_entries() == 1, \
79 ("expected log to contain exactly 1 message \"%s\"", \
80 str)); \
81 } while (0)
83 #define expect_single_log_msg_containing(str) \
84 do { \
85 assert_log_predicate(mock_saved_log_has_message_containing(str)&& \
86 mock_saved_log_n_entries() == 1 , \
87 ("expected log to contain 1 message, containing \"%s\"",\
88 str)); \
89 } while (0)
91 #define expect_no_log_msg(str) \
92 assert_log_predicate(!mock_saved_log_has_message(str), \
93 ("expected log to not contain \"%s\"",str))
95 #define expect_no_log_msg_containing(str) \
96 assert_log_predicate(!mock_saved_log_has_message_containing(str), \
97 ("expected log to not contain \"%s\"", str))
99 #define expect_log_severity(severity) \
100 assert_log_predicate(mock_saved_log_has_severity(severity), \
101 ("expected log to contain severity " # severity))
103 #define expect_no_log_severity(severity) \
104 assert_log_predicate(!mock_saved_log_has_severity(severity), \
105 ("expected log to not contain severity " # severity))
107 #define expect_log_entry() \
108 assert_log_predicate(mock_saved_log_has_entry(), \
109 ("expected log to contain entries"))
111 #define expect_no_log_entry() \
112 assert_log_predicate(!mock_saved_log_has_entry(), \
113 ("expected log to not contain entries"))
115 #endif /* !defined(TOR_LOG_TEST_HELPERS_H) */