aboutsummaryrefslogtreecommitdiff
path: root/src/test/test_controller.c
diff options
context:
space:
mode:
authorNeel Chauhan <neel@neelc.org>2018-04-16 20:35:45 -0400
committerNeel Chauhan <neel@neelc.org>2018-04-16 20:37:50 -0400
commit3a6e37f57f599fe4c94a17422c8e41a7bbe06616 (patch)
tree3c34cff8f3e3a2c4995976c17ea7c97f2168ca95 /src/test/test_controller.c
parentce84de39efc09142d36d5b94963b1aeb2c0a1ae9 (diff)
downloadtor-3a6e37f57f599fe4c94a17422c8e41a7bbe06616.tar.gz
tor-3a6e37f57f599fe4c94a17422c8e41a7bbe06616.zip
Add GETINFO current-time/{local,utc} regression test
Diffstat (limited to 'src/test/test_controller.c')
-rw-r--r--src/test/test_controller.c56
1 files changed, 56 insertions, 0 deletions
diff --git a/src/test/test_controller.c b/src/test/test_controller.c
index 1c285bb3a2..3b58a78e1d 100644
--- a/src/test/test_controller.c
+++ b/src/test/test_controller.c
@@ -1470,6 +1470,61 @@ test_download_status_bridge(void *arg)
return;
}
+/** Set timeval to a mock date and time. This is neccessary
+ * to make tor_gettimeofday() mockable. */
+static void
+mock_tor_gettimeofday(struct timeval *timeval)
+{
+ timeval->tv_sec = 1523405073;
+ timeval->tv_usec = 271645;
+}
+
+static void
+test_current_time(void *arg)
+{
+ /* We just need one of these to pass, it doesn't matter what's in it */
+ control_connection_t dummy;
+ /* Get results out */
+ char *answer = NULL;
+ const char *errmsg = NULL;
+
+ (void)arg;
+
+ /* We need these for storing the (mock) time. */
+ MOCK(tor_gettimeofday, mock_tor_gettimeofday);
+ struct timeval now;
+ tor_gettimeofday(&now);
+ char timebuf[ISO_TIME_LEN+1];
+
+ /* Case 1 - local time */
+ format_local_iso_time_nospace(timebuf, (time_t)now.tv_sec);
+ getinfo_helper_current_time(&dummy,
+ "current-time/local",
+ &answer, &errmsg);
+ tt_ptr_op(answer, OP_NE, NULL);
+ tt_ptr_op(errmsg, OP_EQ, NULL);
+ tt_str_op(answer, OP_EQ, timebuf);
+ tor_free(answer);
+ errmsg = NULL;
+
+ /* Case 2 - UTC time */
+ format_iso_time_nospace(timebuf, (time_t)now.tv_sec);
+ getinfo_helper_current_time(&dummy,
+ "current-time/utc",
+ &answer, &errmsg);
+ tt_ptr_op(answer, OP_NE, NULL);
+ tt_ptr_op(errmsg, OP_EQ, NULL);
+ tt_str_op(answer, OP_EQ, timebuf);
+ tor_free(answer);
+ errmsg = NULL;
+
+ done:
+ UNMOCK(tor_gettimeofday);
+ tor_free(answer);
+
+ return;
+}
+
struct testcase_t controller_tests[] = {
{ "add_onion_helper_keyarg_v2", test_add_onion_helper_keyarg_v2, 0,
NULL, NULL },
@@ -1486,6 +1541,7 @@ struct testcase_t controller_tests[] = {
NULL },
{ "download_status_desc", test_download_status_desc, 0, NULL, NULL },
{ "download_status_bridge", test_download_status_bridge, 0, NULL, NULL },
+ { "current_time", test_current_time, 0, NULL, NULL },
END_OF_TESTCASES
};