test_mainloop.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /* Copyright (c) 2018, 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. #include "test/test.h"
  10. #include "test/log_test_helpers.h"
  11. #include "core/or/or.h"
  12. #include "core/mainloop/connection.h"
  13. #include "core/mainloop/mainloop.h"
  14. #include "core/mainloop/netstatus.h"
  15. #include "feature/hs/hs_service.h"
  16. #include "app/config/config.h"
  17. static const uint64_t BILLION = 1000000000;
  18. static void
  19. test_mainloop_update_time_normal(void *arg)
  20. {
  21. (void)arg;
  22. monotime_enable_test_mocking();
  23. /* This is arbitrary */
  24. uint64_t mt_now = UINT64_C(7493289274986);
  25. /* This time is in the past as of when this test was written. */
  26. time_t now = 1525272090;
  27. monotime_coarse_set_mock_time_nsec(mt_now);
  28. reset_uptime();
  29. update_current_time(now);
  30. tt_int_op(approx_time(), OP_EQ, now);
  31. tt_int_op(get_uptime(), OP_EQ, 0);
  32. update_current_time(now); // Same time as before is a no-op.
  33. tt_int_op(get_uptime(), OP_EQ, 0);
  34. now += 1;
  35. mt_now += BILLION;
  36. monotime_coarse_set_mock_time_nsec(mt_now);
  37. update_current_time(now);
  38. tt_int_op(approx_time(), OP_EQ, now);
  39. tt_int_op(get_uptime(), OP_EQ, 1);
  40. now += 2; // two-second jump is unremarkable.
  41. mt_now += 2*BILLION;
  42. update_current_time(now);
  43. monotime_coarse_set_mock_time_nsec(mt_now);
  44. tt_int_op(approx_time(), OP_EQ, now);
  45. tt_int_op(get_uptime(), OP_EQ, 3);
  46. now -= 1; // a one-second hop backwards is also unremarkable.
  47. update_current_time(now);
  48. tt_int_op(approx_time(), OP_EQ, now); // it changes the approx time...
  49. tt_int_op(get_uptime(), OP_EQ, 3); // but it doesn't roll back our uptime
  50. done:
  51. monotime_disable_test_mocking();
  52. }
  53. static void
  54. test_mainloop_update_time_jumps(void *arg)
  55. {
  56. (void)arg;
  57. monotime_enable_test_mocking();
  58. /* This is arbitrary */
  59. uint64_t mt_now = UINT64_C(7493289274986);
  60. /* This time is in the past as of when this test was written. */
  61. time_t now = 220897152;
  62. monotime_coarse_set_mock_time_nsec(mt_now);
  63. reset_uptime();
  64. update_current_time(now);
  65. tt_int_op(approx_time(), OP_EQ, now);
  66. tt_int_op(get_uptime(), OP_EQ, 0);
  67. /* Put some uptime on the clock.. */
  68. now += 3;
  69. mt_now += 3*BILLION;
  70. monotime_coarse_set_mock_time_nsec(mt_now);
  71. update_current_time(now);
  72. tt_int_op(approx_time(), OP_EQ, now);
  73. tt_int_op(get_uptime(), OP_EQ, 3);
  74. /* Now try jumping forward and backward, without updating the monotonic
  75. * clock. */
  76. setup_capture_of_logs(LOG_NOTICE);
  77. now += 1800;
  78. update_current_time(now);
  79. expect_single_log_msg_containing(
  80. "Your system clock just jumped 1800 seconds forward");
  81. tt_int_op(approx_time(), OP_EQ, now);
  82. tt_int_op(get_uptime(), OP_EQ, 3); // no uptime change.
  83. mock_clean_saved_logs();
  84. now -= 600;
  85. update_current_time(now);
  86. expect_single_log_msg_containing(
  87. "Your system clock just jumped 600 seconds backward");
  88. tt_int_op(approx_time(), OP_EQ, now);
  89. tt_int_op(get_uptime(), OP_EQ, 3); // no uptime change.
  90. mock_clean_saved_logs();
  91. /* uptime tracking should go normally now if the clock moves sensibly. */
  92. now += 2;
  93. mt_now += 2*BILLION;
  94. update_current_time(now);
  95. tt_int_op(approx_time(), OP_EQ, now);
  96. tt_int_op(get_uptime(), OP_EQ, 5);
  97. /* If we skip forward by a few minutes but the monotonic clock agrees,
  98. * we've just been idle: that counts as not worth warning about. */
  99. now += 1800;
  100. mt_now += 1800*BILLION;
  101. monotime_coarse_set_mock_time_nsec(mt_now);
  102. update_current_time(now);
  103. expect_no_log_entry();
  104. tt_int_op(approx_time(), OP_EQ, now);
  105. tt_int_op(get_uptime(), OP_EQ, 5); // this doesn't count to uptime, though.
  106. /* If we skip forward by a long time, even if the clock agrees, it's
  107. * idnless that counts. */
  108. now += 4000;
  109. mt_now += 4000*BILLION;
  110. monotime_coarse_set_mock_time_nsec(mt_now);
  111. update_current_time(now);
  112. expect_single_log_msg_containing("Tor has been idle for 4000 seconds");
  113. tt_int_op(approx_time(), OP_EQ, now);
  114. tt_int_op(get_uptime(), OP_EQ, 5);
  115. done:
  116. teardown_capture_of_logs();
  117. monotime_disable_test_mocking();
  118. }
  119. static int schedule_rescan_called = 0;
  120. static void
  121. mock_schedule_rescan_periodic_events(void)
  122. {
  123. ++schedule_rescan_called;
  124. }
  125. static void
  126. test_mainloop_user_activity(void *arg)
  127. {
  128. (void)arg;
  129. const time_t start = 1542658829;
  130. update_approx_time(start);
  131. MOCK(schedule_rescan_periodic_events, mock_schedule_rescan_periodic_events);
  132. reset_user_activity(start);
  133. tt_i64_op(get_last_user_activity_time(), OP_EQ, start);
  134. set_network_participation(false);
  135. // reset can move backwards and forwards, but does not change network
  136. // participation.
  137. reset_user_activity(start-10);
  138. tt_i64_op(get_last_user_activity_time(), OP_EQ, start-10);
  139. reset_user_activity(start+10);
  140. tt_i64_op(get_last_user_activity_time(), OP_EQ, start+10);
  141. tt_int_op(schedule_rescan_called, OP_EQ, 0);
  142. tt_int_op(false, OP_EQ, is_participating_on_network());
  143. // "note" can only move forward. Calling it from a non-participating
  144. // state makes us rescan the periodic callbacks and set participation.
  145. note_user_activity(start+20);
  146. tt_i64_op(get_last_user_activity_time(), OP_EQ, start+20);
  147. tt_int_op(true, OP_EQ, is_participating_on_network());
  148. tt_int_op(schedule_rescan_called, OP_EQ, 1);
  149. // Calling it again will move us forward, but not call rescan again.
  150. note_user_activity(start+25);
  151. tt_i64_op(get_last_user_activity_time(), OP_EQ, start+25);
  152. tt_int_op(true, OP_EQ, is_participating_on_network());
  153. tt_int_op(schedule_rescan_called, OP_EQ, 1);
  154. // We won't move backwards.
  155. note_user_activity(start+20);
  156. tt_i64_op(get_last_user_activity_time(), OP_EQ, start+25);
  157. tt_int_op(true, OP_EQ, is_participating_on_network());
  158. tt_int_op(schedule_rescan_called, OP_EQ, 1);
  159. done:
  160. UNMOCK(schedule_rescan_periodic_events);
  161. }
  162. static unsigned int
  163. mock_get_num_services(void)
  164. {
  165. return 1;
  166. }
  167. static connection_t *
  168. mock_connection_gbtu(int type)
  169. {
  170. (void) type;
  171. return (void *)"hello fellow connections";
  172. }
  173. static void
  174. test_mainloop_check_participation(void *arg)
  175. {
  176. (void)arg;
  177. or_options_t *options = options_new();
  178. const time_t start = 1542658829;
  179. const time_t ONE_DAY = 24*60*60;
  180. // Suppose we've been idle for a day or two
  181. reset_user_activity(start - 2*ONE_DAY);
  182. set_network_participation(true);
  183. check_network_participation_callback(start, options);
  184. tt_int_op(is_participating_on_network(), OP_EQ, false);
  185. tt_i64_op(get_last_user_activity_time(), OP_EQ, start-2*ONE_DAY);
  186. // suppose we've been idle for 2 days... but we are a server.
  187. reset_user_activity(start - 2*ONE_DAY);
  188. options->ORPort_set = 1;
  189. set_network_participation(true);
  190. check_network_participation_callback(start+2, options);
  191. tt_int_op(is_participating_on_network(), OP_EQ, true);
  192. tt_i64_op(get_last_user_activity_time(), OP_EQ, start+2);
  193. options->ORPort_set = 0;
  194. // idle for 2 days, but we have a hidden service.
  195. reset_user_activity(start - 2*ONE_DAY);
  196. set_network_participation(true);
  197. MOCK(hs_service_get_num_services, mock_get_num_services);
  198. check_network_participation_callback(start+3, options);
  199. tt_int_op(is_participating_on_network(), OP_EQ, true);
  200. tt_i64_op(get_last_user_activity_time(), OP_EQ, start+3);
  201. UNMOCK(hs_service_get_num_services);
  202. // idle for 2 days but we have at least one user connection
  203. MOCK(connection_get_by_type_nonlinked, mock_connection_gbtu);
  204. reset_user_activity(start - 2*ONE_DAY);
  205. set_network_participation(true);
  206. options->DormantTimeoutDisabledByIdleStreams = 1;
  207. check_network_participation_callback(start+10, options);
  208. tt_int_op(is_participating_on_network(), OP_EQ, true);
  209. tt_i64_op(get_last_user_activity_time(), OP_EQ, start+10);
  210. // as above, but DormantTimeoutDisabledByIdleStreams is not set
  211. reset_user_activity(start - 2*ONE_DAY);
  212. set_network_participation(true);
  213. options->DormantTimeoutDisabledByIdleStreams = 0;
  214. check_network_participation_callback(start+13, options);
  215. tt_int_op(is_participating_on_network(), OP_EQ, false);
  216. tt_i64_op(get_last_user_activity_time(), OP_EQ, start-2*ONE_DAY);
  217. UNMOCK(connection_get_by_type_nonlinked);
  218. options->DormantTimeoutDisabledByIdleStreams = 1;
  219. // idle for 2 days but DormantClientTimeout is 3 days
  220. reset_user_activity(start - 2*ONE_DAY);
  221. set_network_participation(true);
  222. options->DormantClientTimeout = ONE_DAY * 3;
  223. check_network_participation_callback(start+30, options);
  224. tt_int_op(is_participating_on_network(), OP_EQ, true);
  225. tt_i64_op(get_last_user_activity_time(), OP_EQ, start-2*ONE_DAY);
  226. done:
  227. or_options_free(options);
  228. UNMOCK(hs_service_get_num_services);
  229. UNMOCK(connection_get_by_type_nonlinked);
  230. }
  231. #define MAINLOOP_TEST(name) \
  232. { #name, test_mainloop_## name , TT_FORK, NULL, NULL }
  233. struct testcase_t mainloop_tests[] = {
  234. MAINLOOP_TEST(update_time_normal),
  235. MAINLOOP_TEST(update_time_jumps),
  236. MAINLOOP_TEST(user_activity),
  237. MAINLOOP_TEST(check_participation),
  238. END_OF_TESTCASES
  239. };