aboutsummaryrefslogtreecommitdiff
path: root/src/test/test_voting_schedule.c
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2018-05-06 20:42:18 -0400
committerNick Mathewson <nickm@torproject.org>2018-05-06 20:42:18 -0400
commitd14c245a0f3733fa6b33d3c7c44fc7fe0bfc302e (patch)
tree22ec019ac0043d6e004404af728037d033dbe7d5 /src/test/test_voting_schedule.c
parentf36656cada48a2d9f51c857d8477a8060cb89b9d (diff)
downloadtor-d14c245a0f3733fa6b33d3c7c44fc7fe0bfc302e.tar.gz
tor-d14c245a0f3733fa6b33d3c7c44fc7fe0bfc302e.zip
Add unit test for ..get_start_of_next_voting_interval().
This functionality was covered only accidentally by our voting-test code, and as such wasn't actually tested at all. The tests that called it made its coverage nondeterministic, depending on what time of day you ran the tests. Closes ticket 26014.
Diffstat (limited to 'src/test/test_voting_schedule.c')
-rw-r--r--src/test/test_voting_schedule.c64
1 files changed, 64 insertions, 0 deletions
diff --git a/src/test/test_voting_schedule.c b/src/test/test_voting_schedule.c
new file mode 100644
index 0000000000..df6058b74f
--- /dev/null
+++ b/src/test/test_voting_schedule.c
@@ -0,0 +1,64 @@
+/* Copyright (c) 2018, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+#include "orconfig.h"
+
+#include "or.h"
+#include "voting_schedule.h"
+
+#include "test.h"
+
+static void
+test_voting_schedule_interval_start(void *arg)
+{
+#define next_interval voting_schedule_get_start_of_next_interval
+ (void)arg;
+ char buf[ISO_TIME_LEN+1];
+
+ // Midnight UTC tonight (as I am writing this test)
+ const time_t midnight = 1525651200;
+ format_iso_time(buf, midnight);
+ tt_str_op(buf, OP_EQ, "2018-05-07 00:00:00");
+
+ /* Some simple tests with a 50-minute voting interval */
+
+ tt_i64_op(next_interval(midnight, 3000, 0), OP_EQ,
+ midnight+3000);
+
+ tt_i64_op(next_interval(midnight+100, 3000, 0), OP_EQ,
+ midnight+3000);
+
+ tt_i64_op(next_interval(midnight+3000, 3000, 0), OP_EQ,
+ midnight+6000);
+
+ tt_i64_op(next_interval(midnight+3001, 3000, 0), OP_EQ,
+ midnight+6000);
+
+ /* Make sure that we roll around properly at midnight */
+ tt_i64_op(next_interval(midnight+83000, 3000, 0), OP_EQ,
+ midnight+84000);
+
+ /* We start fresh at midnight UTC, even if there are leftover seconds. */
+ tt_i64_op(next_interval(midnight+84005, 3000, 0), OP_EQ,
+ midnight+86400);
+
+ /* Now try with offsets. (These are only used for test networks.) */
+ tt_i64_op(next_interval(midnight, 3000, 99), OP_EQ,
+ midnight+99);
+
+ tt_i64_op(next_interval(midnight+100, 3000, 99), OP_EQ,
+ midnight+3099);
+
+ done:
+ ;
+#undef next_interval
+}
+
+#define VS(name,flags) \
+ { #name, test_voting_schedule_##name, (flags), NULL, NULL }
+
+struct testcase_t voting_schedule_tests[] = {
+ VS(interval_start, 0),
+ END_OF_TESTCASES
+};
+