test_mainloop.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. #include "test/test.h"
  8. #include "test/log_test_helpers.h"
  9. #include "core/or/or.h"
  10. #include "core/mainloop/mainloop.h"
  11. #include "core/mainloop/netstatus.h"
  12. static const uint64_t BILLION = 1000000000;
  13. static void
  14. test_mainloop_update_time_normal(void *arg)
  15. {
  16. (void)arg;
  17. monotime_enable_test_mocking();
  18. /* This is arbitrary */
  19. uint64_t mt_now = UINT64_C(7493289274986);
  20. /* This time is in the past as of when this test was written. */
  21. time_t now = 1525272090;
  22. monotime_coarse_set_mock_time_nsec(mt_now);
  23. reset_uptime();
  24. update_current_time(now);
  25. tt_int_op(approx_time(), OP_EQ, now);
  26. tt_int_op(get_uptime(), OP_EQ, 0);
  27. update_current_time(now); // Same time as before is a no-op.
  28. tt_int_op(get_uptime(), OP_EQ, 0);
  29. now += 1;
  30. mt_now += BILLION;
  31. monotime_coarse_set_mock_time_nsec(mt_now);
  32. update_current_time(now);
  33. tt_int_op(approx_time(), OP_EQ, now);
  34. tt_int_op(get_uptime(), OP_EQ, 1);
  35. now += 2; // two-second jump is unremarkable.
  36. mt_now += 2*BILLION;
  37. update_current_time(now);
  38. monotime_coarse_set_mock_time_nsec(mt_now);
  39. tt_int_op(approx_time(), OP_EQ, now);
  40. tt_int_op(get_uptime(), OP_EQ, 3);
  41. now -= 1; // a one-second hop backwards is also unremarkable.
  42. update_current_time(now);
  43. tt_int_op(approx_time(), OP_EQ, now); // it changes the approx time...
  44. tt_int_op(get_uptime(), OP_EQ, 3); // but it doesn't roll back our uptime
  45. done:
  46. monotime_disable_test_mocking();
  47. }
  48. static void
  49. test_mainloop_update_time_jumps(void *arg)
  50. {
  51. (void)arg;
  52. monotime_enable_test_mocking();
  53. /* This is arbitrary */
  54. uint64_t mt_now = UINT64_C(7493289274986);
  55. /* This time is in the past as of when this test was written. */
  56. time_t now = 220897152;
  57. monotime_coarse_set_mock_time_nsec(mt_now);
  58. reset_uptime();
  59. update_current_time(now);
  60. tt_int_op(approx_time(), OP_EQ, now);
  61. tt_int_op(get_uptime(), OP_EQ, 0);
  62. /* Put some uptime on the clock.. */
  63. now += 3;
  64. mt_now += 3*BILLION;
  65. monotime_coarse_set_mock_time_nsec(mt_now);
  66. update_current_time(now);
  67. tt_int_op(approx_time(), OP_EQ, now);
  68. tt_int_op(get_uptime(), OP_EQ, 3);
  69. /* Now try jumping forward and backward, without updating the monotonic
  70. * clock. */
  71. setup_capture_of_logs(LOG_NOTICE);
  72. now += 1800;
  73. update_current_time(now);
  74. expect_single_log_msg_containing(
  75. "Your system clock just jumped 1800 seconds forward");
  76. tt_int_op(approx_time(), OP_EQ, now);
  77. tt_int_op(get_uptime(), OP_EQ, 3); // no uptime change.
  78. mock_clean_saved_logs();
  79. now -= 600;
  80. update_current_time(now);
  81. expect_single_log_msg_containing(
  82. "Your system clock just jumped 600 seconds backward");
  83. tt_int_op(approx_time(), OP_EQ, now);
  84. tt_int_op(get_uptime(), OP_EQ, 3); // no uptime change.
  85. mock_clean_saved_logs();
  86. /* uptime tracking should go normally now if the clock moves sensibly. */
  87. now += 2;
  88. mt_now += 2*BILLION;
  89. update_current_time(now);
  90. tt_int_op(approx_time(), OP_EQ, now);
  91. tt_int_op(get_uptime(), OP_EQ, 5);
  92. /* If we skip forward by a few minutes but the monotonic clock agrees,
  93. * we've just been idle: that counts as not worth warning about. */
  94. now += 1800;
  95. mt_now += 1800*BILLION;
  96. monotime_coarse_set_mock_time_nsec(mt_now);
  97. update_current_time(now);
  98. expect_no_log_entry();
  99. tt_int_op(approx_time(), OP_EQ, now);
  100. tt_int_op(get_uptime(), OP_EQ, 5); // this doesn't count to uptime, though.
  101. /* If we skip forward by a long time, even if the clock agrees, it's
  102. * idnless that counts. */
  103. now += 4000;
  104. mt_now += 4000*BILLION;
  105. monotime_coarse_set_mock_time_nsec(mt_now);
  106. update_current_time(now);
  107. expect_single_log_msg_containing("Tor has been idle for 4000 seconds");
  108. tt_int_op(approx_time(), OP_EQ, now);
  109. tt_int_op(get_uptime(), OP_EQ, 5);
  110. done:
  111. teardown_capture_of_logs();
  112. monotime_disable_test_mocking();
  113. }
  114. static int schedule_rescan_called = 0;
  115. static void
  116. mock_schedule_rescan_periodic_events(void)
  117. {
  118. ++schedule_rescan_called;
  119. }
  120. static void
  121. test_mainloop_user_activity(void *arg)
  122. {
  123. (void)arg;
  124. const time_t start = 1542658829;
  125. update_approx_time(start);
  126. MOCK(schedule_rescan_periodic_events, mock_schedule_rescan_periodic_events);
  127. reset_user_activity(start);
  128. tt_i64_op(get_last_user_activity_time(), OP_EQ, start);
  129. set_network_participation(false);
  130. // reset can move backwards and forwards, but does not change network
  131. // participation.
  132. reset_user_activity(start-10);
  133. tt_i64_op(get_last_user_activity_time(), OP_EQ, start-10);
  134. reset_user_activity(start+10);
  135. tt_i64_op(get_last_user_activity_time(), OP_EQ, start+10);
  136. tt_int_op(schedule_rescan_called, OP_EQ, 0);
  137. tt_int_op(false, OP_EQ, is_participating_on_network());
  138. // "note" can only move forward. Calling it from a non-participating
  139. // state makes us rescan the periodic callbacks and set participation.
  140. note_user_activity(start+20);
  141. tt_i64_op(get_last_user_activity_time(), OP_EQ, start+20);
  142. tt_int_op(true, OP_EQ, is_participating_on_network());
  143. tt_int_op(schedule_rescan_called, OP_EQ, 1);
  144. // Calling it again will move us forward, but not call rescan again.
  145. note_user_activity(start+25);
  146. tt_i64_op(get_last_user_activity_time(), OP_EQ, start+25);
  147. tt_int_op(true, OP_EQ, is_participating_on_network());
  148. tt_int_op(schedule_rescan_called, OP_EQ, 1);
  149. // We won't move backwards.
  150. note_user_activity(start+20);
  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. done:
  155. UNMOCK(schedule_rescan_periodic_events);
  156. }
  157. #define MAINLOOP_TEST(name) \
  158. { #name, test_mainloop_## name , TT_FORK, NULL, NULL }
  159. struct testcase_t mainloop_tests[] = {
  160. MAINLOOP_TEST(update_time_normal),
  161. MAINLOOP_TEST(update_time_jumps),
  162. MAINLOOP_TEST(user_activity),
  163. END_OF_TESTCASES
  164. };