commit 55512ef022de39770e0787e9dfc2e29e762652ae Author: Nick Mathewson nickm@torproject.org Date: Mon Nov 19 15:35:55 2018 -0500
Test netstatus.c tracking of user participation status --- src/core/mainloop/mainloop.c | 4 ++-- src/core/mainloop/mainloop.h | 2 +- src/test/test_mainloop.c | 57 +++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 59 insertions(+), 4 deletions(-)
diff --git a/src/core/mainloop/mainloop.c b/src/core/mainloop/mainloop.c index 1bd186d85..331f7021a 100644 --- a/src/core/mainloop/mainloop.c +++ b/src/core/mainloop/mainloop.c @@ -1608,8 +1608,8 @@ rescan_periodic_events_cb(mainloop_event_t *event, void *arg) /** * Schedule an event that will rescan which periodic events should run. **/ -void -schedule_rescan_periodic_events(void) +MOCK_IMPL(void, +schedule_rescan_periodic_events,(void)) { if (!rescan_periodic_events_ev) { rescan_periodic_events_ev = diff --git a/src/core/mainloop/mainloop.h b/src/core/mainloop/mainloop.h index 7f27ef9a5..e5e730fc8 100644 --- a/src/core/mainloop/mainloop.h +++ b/src/core/mainloop/mainloop.h @@ -65,7 +65,7 @@ void reschedule_or_state_save(void); void reschedule_dirvote(const or_options_t *options); void mainloop_schedule_postloop_cleanup(void); void rescan_periodic_events(const or_options_t *options); -void schedule_rescan_periodic_events(void); +MOCK_DECL(void, schedule_rescan_periodic_events,(void));
void update_current_time(time_t now);
diff --git a/src/test/test_mainloop.c b/src/test/test_mainloop.c index 92ce2e991..94b684fb3 100644 --- a/src/test/test_mainloop.c +++ b/src/test/test_mainloop.c @@ -11,6 +11,7 @@
#include "core/or/or.h" #include "core/mainloop/mainloop.h" +#include "core/mainloop/netstatus.h"
static const uint64_t BILLION = 1000000000;
@@ -131,12 +132,66 @@ test_mainloop_update_time_jumps(void *arg) monotime_disable_test_mocking(); }
+static int schedule_rescan_called = 0; +static void +mock_schedule_rescan_periodic_events(void) +{ + ++schedule_rescan_called; +} + +static void +test_mainloop_user_activity(void *arg) +{ + (void)arg; + const time_t start = 1542658829; + update_approx_time(start); + + MOCK(schedule_rescan_periodic_events, mock_schedule_rescan_periodic_events); + + reset_user_activity(start); + tt_i64_op(get_last_user_activity_time(), OP_EQ, start); + + set_network_participation(false); + + // reset can move backwards and forwards, but does not change network + // participation. + reset_user_activity(start-10); + tt_i64_op(get_last_user_activity_time(), OP_EQ, start-10); + reset_user_activity(start+10); + tt_i64_op(get_last_user_activity_time(), OP_EQ, start+10); + + tt_int_op(schedule_rescan_called, OP_EQ, 0); + tt_int_op(false, OP_EQ, is_participating_on_network()); + + // "note" can only move forward. Calling it from a non-participating + // state makes us rescan the periodic callbacks and set participation. + note_user_activity(start+20); + tt_i64_op(get_last_user_activity_time(), OP_EQ, start+20); + tt_int_op(true, OP_EQ, is_participating_on_network()); + tt_int_op(schedule_rescan_called, OP_EQ, 1); + + // Calling it again will move us forward, but not call rescan again. + note_user_activity(start+25); + tt_i64_op(get_last_user_activity_time(), OP_EQ, start+25); + tt_int_op(true, OP_EQ, is_participating_on_network()); + tt_int_op(schedule_rescan_called, OP_EQ, 1); + + // We won't move backwards. + note_user_activity(start+20); + tt_i64_op(get_last_user_activity_time(), OP_EQ, start+25); + tt_int_op(true, OP_EQ, is_participating_on_network()); + tt_int_op(schedule_rescan_called, OP_EQ, 1); + + done: + UNMOCK(schedule_rescan_periodic_events); +} + #define MAINLOOP_TEST(name) \ { #name, test_mainloop_## name , TT_FORK, NULL, NULL }
struct testcase_t mainloop_tests[] = { MAINLOOP_TEST(update_time_normal), MAINLOOP_TEST(update_time_jumps), + MAINLOOP_TEST(user_activity), END_OF_TESTCASES }; -
tor-commits@lists.torproject.org