aboutsummaryrefslogtreecommitdiff
path: root/src/test/test_mainloop.c
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2018-12-04 17:52:06 -0500
committerNick Mathewson <nickm@torproject.org>2018-12-13 08:25:54 -0500
commite3b7fd2a8129f0d2a7879976e57496b6fd4a7d00 (patch)
tree578a05fd350d3d8f4c4dee28219590b04d9a1444 /src/test/test_mainloop.c
parentb5c04173c88369d0c4cdaf8a34c2474dc25c79fa (diff)
downloadtor-e3b7fd2a8129f0d2a7879976e57496b6fd4a7d00.tar.gz
tor-e3b7fd2a8129f0d2a7879976e57496b6fd4a7d00.zip
Unit tests for back-end functions for persistent dormant state
Diffstat (limited to 'src/test/test_mainloop.c')
-rw-r--r--src/test/test_mainloop.c76
1 files changed, 76 insertions, 0 deletions
diff --git a/src/test/test_mainloop.c b/src/test/test_mainloop.c
index 8dfd5f619a..d797417912 100644
--- a/src/test/test_mainloop.c
+++ b/src/test/test_mainloop.c
@@ -8,6 +8,7 @@
#define CONFIG_PRIVATE
#define MAINLOOP_PRIVATE
+#define STATEFILE_PRIVATE
#include "test/test.h"
#include "test/log_test_helpers.h"
@@ -20,6 +21,8 @@
#include "feature/hs/hs_service.h"
#include "app/config/config.h"
+#include "app/config/statefile.h"
+#include "app/config/or_state_st.h"
static const uint64_t BILLION = 1000000000;
@@ -190,6 +193,13 @@ test_mainloop_user_activity(void *arg)
tt_int_op(true, OP_EQ, is_participating_on_network());
tt_int_op(schedule_rescan_called, OP_EQ, 1);
+ // We _will_ adjust if the clock jumps though.
+ netstatus_note_clock_jumped(500);
+ tt_i64_op(get_last_user_activity_time(), OP_EQ, start+525);
+
+ netstatus_note_clock_jumped(-400);
+ tt_i64_op(get_last_user_activity_time(), OP_EQ, start+125);
+
done:
UNMOCK(schedule_rescan_periodic_events);
}
@@ -273,6 +283,70 @@ test_mainloop_check_participation(void *arg)
UNMOCK(connection_get_by_type_nonlinked);
}
+static void
+test_mainloop_dormant_load_state(void *arg)
+{
+ (void)arg;
+ or_state_t *state = or_state_new();
+ const time_t start = 1543956575;
+
+ reset_user_activity(0);
+ set_network_participation(false);
+
+ // When we construct a new state, it starts out in "auto" mode.
+ tt_int_op(state->Dormant, OP_EQ, -1);
+
+ // Initializing from "auto" makes us start out (by default) non-Dormant,
+ // with activity right now.
+ netstatus_load_from_state(state, start);
+ tt_assert(is_participating_on_network());
+ tt_i64_op(get_last_user_activity_time(), OP_EQ, start);
+
+ // Initializing from dormant clears the last user activity time, and
+ // makes us dormant.
+ state->Dormant = 1;
+ netstatus_load_from_state(state, start);
+ tt_assert(! is_participating_on_network());
+ tt_i64_op(get_last_user_activity_time(), OP_EQ, 0);
+
+ // Initializing from non-dormant sets the last user activity time, and
+ // makes us non-dormant.
+ state->Dormant = 0;
+ state->MinutesSinceUserActivity = 123;
+ netstatus_load_from_state(state, start);
+ tt_assert(is_participating_on_network());
+ tt_i64_op(get_last_user_activity_time(), OP_EQ, start - 123*60);
+
+ done:
+ or_state_free(state);
+}
+
+static void
+test_mainloop_dormant_save_state(void *arg)
+{
+ (void)arg;
+ or_state_t *state = or_state_new();
+ const time_t start = 1543956575;
+
+ // Can we save a non-dormant state correctly?
+ reset_user_activity(start - 1000);
+ set_network_participation(true);
+ netstatus_flush_to_state(state, start);
+
+ tt_int_op(state->Dormant, OP_EQ, 0);
+ tt_int_op(state->MinutesSinceUserActivity, OP_EQ, 1000 / 60);
+
+ // Can we save a dormant state correctly?
+ set_network_participation(false);
+ netstatus_flush_to_state(state, start);
+
+ tt_int_op(state->Dormant, OP_EQ, 1);
+ tt_int_op(state->MinutesSinceUserActivity, OP_EQ, 0);
+
+ done:
+ or_state_free(state);
+}
+
#define MAINLOOP_TEST(name) \
{ #name, test_mainloop_## name , TT_FORK, NULL, NULL }
@@ -281,5 +355,7 @@ struct testcase_t mainloop_tests[] = {
MAINLOOP_TEST(update_time_jumps),
MAINLOOP_TEST(user_activity),
MAINLOOP_TEST(check_participation),
+ MAINLOOP_TEST(dormant_load_state),
+ MAINLOOP_TEST(dormant_save_state),
END_OF_TESTCASES
};