|
@@ -0,0 +1,102 @@
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ * \file test_mainloop.c
|
|
|
+ * \brief Tests for functions closely related to the Tor main loop
|
|
|
+ */
|
|
|
+
|
|
|
+#include "test.h"
|
|
|
+#include "log_test_helpers.h"
|
|
|
+
|
|
|
+#include "or.h"
|
|
|
+#include "main.h"
|
|
|
+
|
|
|
+static void
|
|
|
+test_mainloop_update_time_normal(void *arg)
|
|
|
+{
|
|
|
+ (void)arg;
|
|
|
+
|
|
|
+
|
|
|
+ time_t now = 1525272090;
|
|
|
+ reset_uptime();
|
|
|
+ update_current_time(now);
|
|
|
+ tt_int_op(approx_time(), OP_EQ, now);
|
|
|
+ tt_int_op(get_uptime(), OP_EQ, 0);
|
|
|
+
|
|
|
+ update_current_time(now);
|
|
|
+ tt_int_op(get_uptime(), OP_EQ, 0);
|
|
|
+
|
|
|
+ now += 1;
|
|
|
+ update_current_time(now);
|
|
|
+ tt_int_op(approx_time(), OP_EQ, now);
|
|
|
+ tt_int_op(get_uptime(), OP_EQ, 1);
|
|
|
+
|
|
|
+ now += 2;
|
|
|
+ update_current_time(now);
|
|
|
+ tt_int_op(approx_time(), OP_EQ, now);
|
|
|
+ tt_int_op(get_uptime(), OP_EQ, 3);
|
|
|
+
|
|
|
+ now -= 1;
|
|
|
+ update_current_time(now);
|
|
|
+ tt_int_op(approx_time(), OP_EQ, now);
|
|
|
+ tt_int_op(get_uptime(), OP_EQ, 3);
|
|
|
+
|
|
|
+ done:
|
|
|
+ ;
|
|
|
+}
|
|
|
+
|
|
|
+static void
|
|
|
+test_mainloop_update_time_jumps(void *arg)
|
|
|
+{
|
|
|
+ (void)arg;
|
|
|
+
|
|
|
+
|
|
|
+ time_t now = 220897152;
|
|
|
+ reset_uptime();
|
|
|
+ update_current_time(now);
|
|
|
+ tt_int_op(approx_time(), OP_EQ, now);
|
|
|
+ tt_int_op(get_uptime(), OP_EQ, 0);
|
|
|
+
|
|
|
+
|
|
|
+ now += 3;
|
|
|
+ update_current_time(now);
|
|
|
+ tt_int_op(approx_time(), OP_EQ, now);
|
|
|
+ tt_int_op(get_uptime(), OP_EQ, 3);
|
|
|
+
|
|
|
+
|
|
|
+ setup_capture_of_logs(LOG_NOTICE);
|
|
|
+ now += 3600;
|
|
|
+ update_current_time(now);
|
|
|
+ expect_single_log_msg_containing(
|
|
|
+ "Your system clock just jumped 3600 seconds forward");
|
|
|
+ tt_int_op(approx_time(), OP_EQ, now);
|
|
|
+ tt_int_op(get_uptime(), OP_EQ, 3);
|
|
|
+ mock_clean_saved_logs();
|
|
|
+
|
|
|
+ now -= 600;
|
|
|
+ update_current_time(now);
|
|
|
+ expect_single_log_msg_containing(
|
|
|
+ "Your system clock just jumped 600 seconds backward");
|
|
|
+ tt_int_op(approx_time(), OP_EQ, now);
|
|
|
+ tt_int_op(get_uptime(), OP_EQ, 3);
|
|
|
+
|
|
|
+
|
|
|
+ now += 2;
|
|
|
+ update_current_time(now);
|
|
|
+ tt_int_op(approx_time(), OP_EQ, now);
|
|
|
+ tt_int_op(get_uptime(), OP_EQ, 5);
|
|
|
+
|
|
|
+ done:
|
|
|
+ teardown_capture_of_logs();
|
|
|
+}
|
|
|
+
|
|
|
+#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),
|
|
|
+ END_OF_TESTCASES
|
|
|
+};
|
|
|
+
|