1 /* Copyright (c) 2010-2021, 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"
17 test_compat_libevent_logging_callback(void *ignored
)
20 setup_full_capture_of_logs(LOG_DEBUG
);
22 libevent_logging_callback(_EVENT_LOG_DEBUG
, "hello world");
23 expect_log_msg("Message from libevent: hello world\n");
24 expect_log_severity(LOG_DEBUG
);
25 tt_int_op(smartlist_len(mock_saved_logs()), OP_EQ
, 1);
27 mock_clean_saved_logs();
28 libevent_logging_callback(_EVENT_LOG_MSG
, "hello world another time");
29 expect_log_msg("Message from libevent: hello world another time\n");
30 expect_log_severity(LOG_INFO
);
31 tt_int_op(smartlist_len(mock_saved_logs()), OP_EQ
, 1);
33 mock_clean_saved_logs();
34 libevent_logging_callback(_EVENT_LOG_WARN
, "hello world a third time");
35 expect_log_msg("Warning from libevent: hello world a third time\n");
36 expect_log_severity(LOG_WARN
);
37 tt_int_op(smartlist_len(mock_saved_logs()), OP_EQ
, 1);
39 mock_clean_saved_logs();
40 libevent_logging_callback(_EVENT_LOG_ERR
, "hello world a fourth time");
41 expect_log_msg("Error from libevent: hello world a fourth time\n");
42 expect_log_severity(LOG_ERR
);
43 tt_int_op(smartlist_len(mock_saved_logs()), OP_EQ
, 1);
45 mock_clean_saved_logs();
46 libevent_logging_callback(42, "hello world a fifth time");
47 expect_log_msg("Message [42] from libevent: hello world a fifth time\n");
48 expect_log_severity(LOG_WARN
);
49 tt_int_op(smartlist_len(mock_saved_logs()), OP_EQ
, 1);
51 mock_clean_saved_logs();
52 libevent_logging_callback(_EVENT_LOG_DEBUG
,
53 "012345678901234567890123456789"
54 "012345678901234567890123456789"
55 "012345678901234567890123456789"
56 "012345678901234567890123456789"
57 "012345678901234567890123456789"
58 "012345678901234567890123456789"
59 "012345678901234567890123456789"
60 "012345678901234567890123456789"
61 "012345678901234567890123456789"
62 "012345678901234567890123456789"
63 "012345678901234567890123456789"
64 "012345678901234567890123456789"
66 expect_log_msg("Message from libevent: "
67 "012345678901234567890123456789"
68 "012345678901234567890123456789"
69 "012345678901234567890123456789"
70 "012345678901234567890123456789"
71 "012345678901234567890123456789"
72 "012345678901234567890123456789"
73 "012345678901234567890123456789"
74 "012345678901234567890123456789"
75 "012345678901234567890123456789"
76 "012345678901234567890123456789"
77 "012345678901234567890123456789"
78 "012345678901234567890123456789\n");
79 expect_log_severity(LOG_DEBUG
);
80 tt_int_op(smartlist_len(mock_saved_logs()), OP_EQ
, 1);
82 mock_clean_saved_logs();
83 libevent_logging_callback(42, "xxx\n");
84 expect_log_msg("Message [42] from libevent: xxx\n");
85 expect_log_severity(LOG_WARN
);
86 tt_int_op(smartlist_len(mock_saved_logs()), OP_EQ
, 1);
88 suppress_libevent_log_msg("something");
89 mock_clean_saved_logs();
90 libevent_logging_callback(_EVENT_LOG_MSG
, "hello there");
91 expect_log_msg("Message from libevent: hello there\n");
92 expect_log_severity(LOG_INFO
);
93 tt_int_op(smartlist_len(mock_saved_logs()), OP_EQ
, 1);
95 mock_clean_saved_logs();
96 libevent_logging_callback(_EVENT_LOG_MSG
, "hello there something else");
97 expect_no_log_msg("hello there something else");
98 if (mock_saved_logs())
99 tt_int_op(smartlist_len(mock_saved_logs()), OP_EQ
, 0);
101 // No way of verifying the result of this, it seems =/
102 configure_libevent_logging();
105 suppress_libevent_log_msg(NULL
);
106 teardown_capture_of_logs();
110 test_compat_libevent_header_version(void *ignored
)
115 res
= tor_libevent_get_header_version_str();
116 tt_str_op(res
, OP_EQ
, LIBEVENT_VERSION
);
122 /* Test for postloop events */
124 /* Event callback to increment a counter. */
126 increment_int_counter_cb(periodic_timer_t
*timer
, void *arg
)
133 static int activated_counter
= 0;
135 /* Mainloop event callback to activate another mainloop event */
137 activate_event_cb(mainloop_event_t
*ev
, void *arg
)
140 mainloop_event_t
**other_event
= arg
;
141 mainloop_event_activate(*other_event
);
146 test_compat_libevent_postloop_events(void *arg
)
149 mainloop_event_t
*a
= NULL
, *b
= NULL
;
150 periodic_timer_t
*timed
= NULL
;
152 /* If postloop events don't work, then these events will activate one
153 * another ad infinitum and, and the periodic event will never occur. */
154 b
= mainloop_event_postloop_new(activate_event_cb
, &a
);
155 a
= mainloop_event_postloop_new(activate_event_cb
, &b
);
158 struct timeval fifty_ms
= { 0, 10 * 1000 };
159 timed
= periodic_timer_new(tor_libevent_get_base(), &fifty_ms
,
160 increment_int_counter_cb
, &counter
);
162 mainloop_event_activate(a
);
165 r
= tor_libevent_run_event_loop(tor_libevent_get_base(), 0);
168 } while (counter
< 5);
170 tt_int_op(activated_counter
, OP_GE
, 2);
173 mainloop_event_free(a
);
174 mainloop_event_free(b
);
175 periodic_timer_free(timed
);
178 struct testcase_t compat_libevent_tests
[] = {
179 { "logging_callback", test_compat_libevent_logging_callback
,
180 TT_FORK
, NULL
, NULL
},
181 { "header_version", test_compat_libevent_header_version
, 0, NULL
, NULL
},
182 { "postloop_events", test_compat_libevent_postloop_events
,
183 TT_FORK
, NULL
, NULL
},