test_compat_libevent.c (6203B)
1 /* Copyright (c) 2010-2021, The Tor Project, Inc. */ 2 /* See LICENSE for licensing information */ 3 4 #define COMPAT_LIBEVENT_PRIVATE 5 #include "orconfig.h" 6 #include "core/or/or.h" 7 8 #include "test/test.h" 9 10 #include "lib/evloop/compat_libevent.h" 11 12 #include <event2/event.h> 13 14 #include "test/log_test_helpers.h" 15 16 static void 17 test_compat_libevent_logging_callback(void *ignored) 18 { 19 (void)ignored; 20 setup_full_capture_of_logs(LOG_DEBUG); 21 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); 26 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); 32 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); 38 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); 44 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); 50 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" 65 ); 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); 81 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); 87 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); 94 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); 100 101 // No way of verifying the result of this, it seems =/ 102 configure_libevent_logging(); 103 104 done: 105 suppress_libevent_log_msg(NULL); 106 teardown_capture_of_logs(); 107 } 108 109 static void 110 test_compat_libevent_header_version(void *ignored) 111 { 112 (void)ignored; 113 const char *res; 114 115 res = tor_libevent_get_header_version_str(); 116 tt_str_op(res, OP_EQ, LIBEVENT_VERSION); 117 118 done: 119 (void)0; 120 } 121 122 /* Test for postloop events */ 123 124 /* Event callback to increment a counter. */ 125 static void 126 increment_int_counter_cb(periodic_timer_t *timer, void *arg) 127 { 128 (void)timer; 129 int *ctr = arg; 130 ++*ctr; 131 } 132 133 static int activated_counter = 0; 134 135 /* Mainloop event callback to activate another mainloop event */ 136 static void 137 activate_event_cb(mainloop_event_t *ev, void *arg) 138 { 139 (void)ev; 140 mainloop_event_t **other_event = arg; 141 mainloop_event_activate(*other_event); 142 ++activated_counter; 143 } 144 145 static void 146 test_compat_libevent_postloop_events(void *arg) 147 { 148 (void)arg; 149 mainloop_event_t *a = NULL, *b = NULL; 150 periodic_timer_t *timed = NULL; 151 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); 156 157 int counter = 0; 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); 161 162 mainloop_event_activate(a); 163 int r; 164 do { 165 r = tor_libevent_run_event_loop(tor_libevent_get_base(), 0); 166 if (r == -1) 167 break; 168 } while (counter < 5); 169 170 tt_int_op(activated_counter, OP_GE, 2); 171 172 done: 173 mainloop_event_free(a); 174 mainloop_event_free(b); 175 periodic_timer_free(timed); 176 } 177 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 }, 184 END_OF_TESTCASES 185 };