1 /* Copyright (c) 2010-2019, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
4 #define COMPAT_LIBEVENT_PRIVATE
6 #include "core/or/or.h"
10 #include "lib/evloop/compat_libevent.h"
12 #include <event2/event.h>
14 #include "test/log_test_helpers.h"
16 #define NS_MODULE compat_libevent
19 test_compat_libevent_logging_callback(void *ignored
)
22 setup_full_capture_of_logs(LOG_DEBUG
);
24 libevent_logging_callback(_EVENT_LOG_DEBUG
, "hello world");
25 expect_log_msg("Message from libevent: hello world\n");
26 expect_log_severity(LOG_DEBUG
);
27 tt_int_op(smartlist_len(mock_saved_logs()), OP_EQ
, 1);
29 mock_clean_saved_logs();
30 libevent_logging_callback(_EVENT_LOG_MSG
, "hello world another time");
31 expect_log_msg("Message from libevent: hello world another time\n");
32 expect_log_severity(LOG_INFO
);
33 tt_int_op(smartlist_len(mock_saved_logs()), OP_EQ
, 1);
35 mock_clean_saved_logs();
36 libevent_logging_callback(_EVENT_LOG_WARN
, "hello world a third time");
37 expect_log_msg("Warning from libevent: hello world a third time\n");
38 expect_log_severity(LOG_WARN
);
39 tt_int_op(smartlist_len(mock_saved_logs()), OP_EQ
, 1);
41 mock_clean_saved_logs();
42 libevent_logging_callback(_EVENT_LOG_ERR
, "hello world a fourth time");
43 expect_log_msg("Error from libevent: hello world a fourth time\n");
44 expect_log_severity(LOG_ERR
);
45 tt_int_op(smartlist_len(mock_saved_logs()), OP_EQ
, 1);
47 mock_clean_saved_logs();
48 libevent_logging_callback(42, "hello world a fifth time");
49 expect_log_msg("Message [42] from libevent: hello world a fifth time\n");
50 expect_log_severity(LOG_WARN
);
51 tt_int_op(smartlist_len(mock_saved_logs()), OP_EQ
, 1);
53 mock_clean_saved_logs();
54 libevent_logging_callback(_EVENT_LOG_DEBUG
,
55 "012345678901234567890123456789"
56 "012345678901234567890123456789"
57 "012345678901234567890123456789"
58 "012345678901234567890123456789"
59 "012345678901234567890123456789"
60 "012345678901234567890123456789"
61 "012345678901234567890123456789"
62 "012345678901234567890123456789"
63 "012345678901234567890123456789"
64 "012345678901234567890123456789"
65 "012345678901234567890123456789"
66 "012345678901234567890123456789"
68 expect_log_msg("Message from libevent: "
69 "012345678901234567890123456789"
70 "012345678901234567890123456789"
71 "012345678901234567890123456789"
72 "012345678901234567890123456789"
73 "012345678901234567890123456789"
74 "012345678901234567890123456789"
75 "012345678901234567890123456789"
76 "012345678901234567890123456789"
77 "012345678901234567890123456789"
78 "012345678901234567890123456789"
79 "012345678901234567890123456789"
80 "012345678901234567890123456789\n");
81 expect_log_severity(LOG_DEBUG
);
82 tt_int_op(smartlist_len(mock_saved_logs()), OP_EQ
, 1);
84 mock_clean_saved_logs();
85 libevent_logging_callback(42, "xxx\n");
86 expect_log_msg("Message [42] from libevent: xxx\n");
87 expect_log_severity(LOG_WARN
);
88 tt_int_op(smartlist_len(mock_saved_logs()), OP_EQ
, 1);
90 suppress_libevent_log_msg("something");
91 mock_clean_saved_logs();
92 libevent_logging_callback(_EVENT_LOG_MSG
, "hello there");
93 expect_log_msg("Message from libevent: hello there\n");
94 expect_log_severity(LOG_INFO
);
95 tt_int_op(smartlist_len(mock_saved_logs()), OP_EQ
, 1);
97 mock_clean_saved_logs();
98 libevent_logging_callback(_EVENT_LOG_MSG
, "hello there something else");
99 expect_no_log_msg("hello there something else");
100 if (mock_saved_logs())
101 tt_int_op(smartlist_len(mock_saved_logs()), OP_EQ
, 0);
103 // No way of verifying the result of this, it seems =/
104 configure_libevent_logging();
107 suppress_libevent_log_msg(NULL
);
108 teardown_capture_of_logs();
112 test_compat_libevent_header_version(void *ignored
)
117 res
= tor_libevent_get_header_version_str();
118 tt_str_op(res
, OP_EQ
, LIBEVENT_VERSION
);
124 /* Test for postloop events */
126 /* Event callback to increment a counter. */
128 increment_int_counter_cb(periodic_timer_t
*timer
, void *arg
)
135 static int activated_counter
= 0;
137 /* Mainloop event callback to activate another mainloop event */
139 activate_event_cb(mainloop_event_t
*ev
, void *arg
)
142 mainloop_event_t
**other_event
= arg
;
143 mainloop_event_activate(*other_event
);
148 test_compat_libevent_postloop_events(void *arg
)
151 mainloop_event_t
*a
= NULL
, *b
= NULL
;
152 periodic_timer_t
*timed
= NULL
;
154 tor_libevent_postfork();
156 /* If postloop events don't work, then these events will activate one
157 * another ad infinitum and, and the periodic event will never occur. */
158 b
= mainloop_event_postloop_new(activate_event_cb
, &a
);
159 a
= mainloop_event_postloop_new(activate_event_cb
, &b
);
162 struct timeval fifty_ms
= { 0, 10 * 1000 };
163 timed
= periodic_timer_new(tor_libevent_get_base(), &fifty_ms
,
164 increment_int_counter_cb
, &counter
);
166 mainloop_event_activate(a
);
169 r
= tor_libevent_run_event_loop(tor_libevent_get_base(), 0);
172 } while (counter
< 5);
174 tt_int_op(activated_counter
, OP_GE
, 2);
177 mainloop_event_free(a
);
178 mainloop_event_free(b
);
179 periodic_timer_free(timed
);
182 struct testcase_t compat_libevent_tests
[] = {
183 { "logging_callback", test_compat_libevent_logging_callback
,
184 TT_FORK
, NULL
, NULL
},
185 { "header_version", test_compat_libevent_header_version
, 0, NULL
, NULL
},
186 { "postloop_events", test_compat_libevent_postloop_events
,
187 TT_FORK
, NULL
, NULL
},