test_mainloop.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /* Copyright (c) 2018-2019, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file test_mainloop.c
  5. * \brief Tests for functions closely related to the Tor main loop
  6. */
  7. #define CONFIG_PRIVATE
  8. #define MAINLOOP_PRIVATE
  9. #define STATEFILE_PRIVATE
  10. #include "test/test.h"
  11. #include "test/log_test_helpers.h"
  12. #include "core/or/or.h"
  13. #include "core/mainloop/connection.h"
  14. #include "core/mainloop/mainloop.h"
  15. #include "core/mainloop/netstatus.h"
  16. #include "feature/hs/hs_service.h"
  17. #include "app/config/config.h"
  18. #include "app/config/statefile.h"
  19. #include "app/config/or_state_st.h"
  20. static const uint64_t BILLION = 1000000000;
  21. static void
  22. test_mainloop_update_time_normal(void *arg)
  23. {
  24. (void)arg;
  25. monotime_enable_test_mocking();
  26. /* This is arbitrary */
  27. uint64_t mt_now = UINT64_C(7493289274986);
  28. /* This time is in the past as of when this test was written. */
  29. time_t now = 1525272090;
  30. monotime_coarse_set_mock_time_nsec(mt_now);
  31. reset_uptime();
  32. update_current_time(now);
  33. tt_int_op(approx_time(), OP_EQ, now);
  34. tt_int_op(get_uptime(), OP_EQ, 0);
  35. update_current_time(now); // Same time as before is a no-op.
  36. tt_int_op(get_uptime(), OP_EQ, 0);
  37. now += 1;
  38. mt_now += BILLION;
  39. monotime_coarse_set_mock_time_nsec(mt_now);
  40. update_current_time(now);
  41. tt_int_op(approx_time(), OP_EQ, now);
  42. tt_int_op(get_uptime(), OP_EQ, 1);
  43. now += 2; // two-second jump is unremarkable.
  44. mt_now += 2*BILLION;
  45. update_current_time(now);
  46. monotime_coarse_set_mock_time_nsec(mt_now);
  47. tt_int_op(approx_time(), OP_EQ, now);
  48. tt_int_op(get_uptime(), OP_EQ, 3);
  49. now -= 1; // a one-second hop backwards is also unremarkable.
  50. update_current_time(now);
  51. tt_int_op(approx_time(), OP_EQ, now); // it changes the approx time...
  52. tt_int_op(get_uptime(), OP_EQ, 3); // but it doesn't roll back our uptime
  53. done:
  54. monotime_disable_test_mocking();
  55. }
  56. static void
  57. test_mainloop_update_time_jumps(void *arg)
  58. {
  59. (void)arg;
  60. monotime_enable_test_mocking();
  61. /* This is arbitrary */
  62. uint64_t mt_now = UINT64_C(7493289274986);
  63. /* This time is in the past as of when this test was written. */
  64. time_t now = 220897152;
  65. monotime_coarse_set_mock_time_nsec(mt_now);
  66. reset_uptime();
  67. update_current_time(now);
  68. tt_int_op(approx_time(), OP_EQ, now);
  69. tt_int_op(get_uptime(), OP_EQ, 0);
  70. /* Put some uptime on the clock.. */
  71. now += 3;
  72. mt_now += 3*BILLION;
  73. monotime_coarse_set_mock_time_nsec(mt_now);
  74. update_current_time(now);
  75. tt_int_op(approx_time(), OP_EQ, now);
  76. tt_int_op(get_uptime(), OP_EQ, 3);
  77. /* Now try jumping forward and backward, without updating the monotonic
  78. * clock. */
  79. setup_capture_of_logs(LOG_NOTICE);
  80. now += 1800;
  81. update_current_time(now);
  82. expect_single_log_msg_containing(
  83. "Your system clock just jumped 1800 seconds forward");
  84. tt_int_op(approx_time(), OP_EQ, now);
  85. tt_int_op(get_uptime(), OP_EQ, 3); // no uptime change.
  86. mock_clean_saved_logs();
  87. now -= 600;
  88. update_current_time(now);
  89. expect_single_log_msg_containing(
  90. "Your system clock just jumped 600 seconds backward");
  91. tt_int_op(approx_time(), OP_EQ, now);
  92. tt_int_op(get_uptime(), OP_EQ, 3); // no uptime change.
  93. mock_clean_saved_logs();
  94. /* uptime tracking should go normally now if the clock moves sensibly. */
  95. now += 2;
  96. mt_now += 2*BILLION;
  97. update_current_time(now);
  98. tt_int_op(approx_time(), OP_EQ, now);
  99. tt_int_op(get_uptime(), OP_EQ, 5);
  100. /* If we skip forward by a few minutes but the monotonic clock agrees,
  101. * we've just been idle: that counts as not worth warning about. */
  102. now += 1800;
  103. mt_now += 1800*BILLION;
  104. monotime_coarse_set_mock_time_nsec(mt_now);
  105. update_current_time(now);
  106. expect_no_log_entry();
  107. tt_int_op(approx_time(), OP_EQ, now);
  108. tt_int_op(get_uptime(), OP_EQ, 5); // this doesn't count to uptime, though.
  109. /* If we skip forward by a long time, even if the clock agrees, it's
  110. * idnless that counts. */
  111. now += 4000;
  112. mt_now += 4000*BILLION;
  113. monotime_coarse_set_mock_time_nsec(mt_now);
  114. update_current_time(now);
  115. expect_single_log_msg_containing("Tor has been idle for 4000 seconds");
  116. tt_int_op(approx_time(), OP_EQ, now);
  117. tt_int_op(get_uptime(), OP_EQ, 5);
  118. done:
  119. teardown_capture_of_logs();
  120. monotime_disable_test_mocking();
  121. }
  122. static int schedule_rescan_called = 0;
  123. static void
  124. mock_schedule_rescan_periodic_events(void)
  125. {
  126. ++schedule_rescan_called;
  127. }
  128. static void
  129. test_mainloop_user_activity(void *arg)
  130. {
  131. (void)arg;
  132. const time_t start = 1542658829;
  133. update_approx_time(start);
  134. MOCK(schedule_rescan_periodic_events, mock_schedule_rescan_periodic_events);
  135. reset_user_activity(start);
  136. tt_i64_op(get_last_user_activity_time(), OP_EQ, start);
  137. set_network_participation(false);
  138. // reset can move backwards and forwards, but does not change network
  139. // participation.
  140. reset_user_activity(start-10);
  141. tt_i64_op(get_last_user_activity_time(), OP_EQ, start-10);
  142. reset_user_activity(start+10);
  143. tt_i64_op(get_last_user_activity_time(), OP_EQ, start+10);
  144. tt_int_op(schedule_rescan_called, OP_EQ, 0);
  145. tt_int_op(false, OP_EQ, is_participating_on_network());
  146. // "note" can only move forward. Calling it from a non-participating
  147. // state makes us rescan the periodic callbacks and set participation.
  148. note_user_activity(start+20);
  149. tt_i64_op(get_last_user_activity_time(), OP_EQ, start+20);
  150. tt_int_op(true, OP_EQ, is_participating_on_network());
  151. tt_int_op(schedule_rescan_called, OP_EQ, 1);
  152. // Calling it again will move us forward, but not call rescan again.
  153. note_user_activity(start+25);
  154. tt_i64_op(get_last_user_activity_time(), OP_EQ, start+25);
  155. tt_int_op(true, OP_EQ, is_participating_on_network());
  156. tt_int_op(schedule_rescan_called, OP_EQ, 1);
  157. // We won't move backwards.
  158. note_user_activity(start+20);
  159. tt_i64_op(get_last_user_activity_time(), OP_EQ, start+25);
  160. tt_int_op(true, OP_EQ, is_participating_on_network());
  161. tt_int_op(schedule_rescan_called, OP_EQ, 1);
  162. // We _will_ adjust if the clock jumps though.
  163. netstatus_note_clock_jumped(500);
  164. tt_i64_op(get_last_user_activity_time(), OP_EQ, start+525);
  165. netstatus_note_clock_jumped(-400);
  166. tt_i64_op(get_last_user_activity_time(), OP_EQ, start+125);
  167. done:
  168. UNMOCK(schedule_rescan_periodic_events);
  169. }
  170. static unsigned int
  171. mock_get_num_services(void)
  172. {
  173. return 1;
  174. }
  175. static connection_t *
  176. mock_connection_gbtu(int type)
  177. {
  178. (void) type;
  179. return (void *)"hello fellow connections";
  180. }
  181. static void
  182. test_mainloop_check_participation(void *arg)
  183. {
  184. (void)arg;
  185. or_options_t *options = options_new();
  186. const time_t start = 1542658829;
  187. const time_t ONE_DAY = 24*60*60;
  188. // Suppose we've been idle for a day or two
  189. reset_user_activity(start - 2*ONE_DAY);
  190. set_network_participation(true);
  191. check_network_participation_callback(start, options);
  192. tt_int_op(is_participating_on_network(), OP_EQ, false);
  193. tt_i64_op(get_last_user_activity_time(), OP_EQ, start-2*ONE_DAY);
  194. // suppose we've been idle for 2 days... but we are a server.
  195. reset_user_activity(start - 2*ONE_DAY);
  196. options->ORPort_set = 1;
  197. set_network_participation(true);
  198. check_network_participation_callback(start+2, options);
  199. tt_int_op(is_participating_on_network(), OP_EQ, true);
  200. tt_i64_op(get_last_user_activity_time(), OP_EQ, start+2);
  201. options->ORPort_set = 0;
  202. // idle for 2 days, but we have a hidden service.
  203. reset_user_activity(start - 2*ONE_DAY);
  204. set_network_participation(true);
  205. MOCK(hs_service_get_num_services, mock_get_num_services);
  206. check_network_participation_callback(start+3, options);
  207. tt_int_op(is_participating_on_network(), OP_EQ, true);
  208. tt_i64_op(get_last_user_activity_time(), OP_EQ, start+3);
  209. UNMOCK(hs_service_get_num_services);
  210. // idle for 2 days but we have at least one user connection
  211. MOCK(connection_get_by_type_nonlinked, mock_connection_gbtu);
  212. reset_user_activity(start - 2*ONE_DAY);
  213. set_network_participation(true);
  214. options->DormantTimeoutDisabledByIdleStreams = 1;
  215. check_network_participation_callback(start+10, options);
  216. tt_int_op(is_participating_on_network(), OP_EQ, true);
  217. tt_i64_op(get_last_user_activity_time(), OP_EQ, start+10);
  218. // as above, but DormantTimeoutDisabledByIdleStreams is not set
  219. reset_user_activity(start - 2*ONE_DAY);
  220. set_network_participation(true);
  221. options->DormantTimeoutDisabledByIdleStreams = 0;
  222. check_network_participation_callback(start+13, options);
  223. tt_int_op(is_participating_on_network(), OP_EQ, false);
  224. tt_i64_op(get_last_user_activity_time(), OP_EQ, start-2*ONE_DAY);
  225. UNMOCK(connection_get_by_type_nonlinked);
  226. options->DormantTimeoutDisabledByIdleStreams = 1;
  227. // idle for 2 days but DormantClientTimeout is 3 days
  228. reset_user_activity(start - 2*ONE_DAY);
  229. set_network_participation(true);
  230. options->DormantClientTimeout = ONE_DAY * 3;
  231. check_network_participation_callback(start+30, options);
  232. tt_int_op(is_participating_on_network(), OP_EQ, true);
  233. tt_i64_op(get_last_user_activity_time(), OP_EQ, start-2*ONE_DAY);
  234. done:
  235. or_options_free(options);
  236. UNMOCK(hs_service_get_num_services);
  237. UNMOCK(connection_get_by_type_nonlinked);
  238. }
  239. static void
  240. test_mainloop_dormant_load_state(void *arg)
  241. {
  242. (void)arg;
  243. or_state_t *state = or_state_new();
  244. const time_t start = 1543956575;
  245. reset_user_activity(0);
  246. set_network_participation(false);
  247. // When we construct a new state, it starts out in "auto" mode.
  248. tt_int_op(state->Dormant, OP_EQ, -1);
  249. // Initializing from "auto" makes us start out (by default) non-Dormant,
  250. // with activity right now.
  251. netstatus_load_from_state(state, start);
  252. tt_assert(is_participating_on_network());
  253. tt_i64_op(get_last_user_activity_time(), OP_EQ, start);
  254. // Initializing from dormant clears the last user activity time, and
  255. // makes us dormant.
  256. state->Dormant = 1;
  257. netstatus_load_from_state(state, start);
  258. tt_assert(! is_participating_on_network());
  259. tt_i64_op(get_last_user_activity_time(), OP_EQ, 0);
  260. // Initializing from non-dormant sets the last user activity time, and
  261. // makes us non-dormant.
  262. state->Dormant = 0;
  263. state->MinutesSinceUserActivity = 123;
  264. netstatus_load_from_state(state, start);
  265. tt_assert(is_participating_on_network());
  266. tt_i64_op(get_last_user_activity_time(), OP_EQ, start - 123*60);
  267. // If we would start dormant, but DormantCanceledByStartup is set, then
  268. // we start up non-dormant.
  269. state->Dormant = 1;
  270. get_options_mutable()->DormantCanceledByStartup = 1;
  271. netstatus_load_from_state(state, start);
  272. tt_assert(is_participating_on_network());
  273. tt_i64_op(get_last_user_activity_time(), OP_EQ, start);
  274. done:
  275. or_state_free(state);
  276. }
  277. static void
  278. test_mainloop_dormant_save_state(void *arg)
  279. {
  280. (void)arg;
  281. or_state_t *state = or_state_new();
  282. const time_t start = 1543956575;
  283. // Can we save a non-dormant state correctly?
  284. reset_user_activity(start - 1000);
  285. set_network_participation(true);
  286. netstatus_flush_to_state(state, start);
  287. tt_int_op(state->Dormant, OP_EQ, 0);
  288. tt_int_op(state->MinutesSinceUserActivity, OP_EQ, 1000 / 60);
  289. // Can we save a dormant state correctly?
  290. set_network_participation(false);
  291. netstatus_flush_to_state(state, start);
  292. tt_int_op(state->Dormant, OP_EQ, 1);
  293. tt_int_op(state->MinutesSinceUserActivity, OP_EQ, 0);
  294. done:
  295. or_state_free(state);
  296. }
  297. #define MAINLOOP_TEST(name) \
  298. { #name, test_mainloop_## name , TT_FORK, NULL, NULL }
  299. struct testcase_t mainloop_tests[] = {
  300. MAINLOOP_TEST(update_time_normal),
  301. MAINLOOP_TEST(update_time_jumps),
  302. MAINLOOP_TEST(user_activity),
  303. MAINLOOP_TEST(check_participation),
  304. MAINLOOP_TEST(dormant_load_state),
  305. MAINLOOP_TEST(dormant_save_state),
  306. END_OF_TESTCASES
  307. };