aboutsummaryrefslogtreecommitdiff
path: root/src/test/test_util.c
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2021-06-01 16:18:23 -0400
committerGeorge Kadianakis <desnacked@riseup.net>2021-07-06 13:33:05 +0300
commitc1d96358d49a4583b8aa9bdb1e8d73c70f9d8d06 (patch)
tree47d79f59872c97624310adca4f3a981cb2159d8a /src/test/test_util.c
parent8b6e919086ad6dde681516fe52d744afa3ffcc89 (diff)
downloadtor-c1d96358d49a4583b8aa9bdb1e8d73c70f9d8d06.tar.gz
tor-c1d96358d49a4583b8aa9bdb1e8d73c70f9d8d06.zip
Use native timegm when available.
Continue having a tor_gmtime_impl() unit test so that we can detect any problems in our replacement function; add a new test function to make sure that gmtime<->timegm are a round-trip on now-ish times. This is a fix for bug #40383, wherein we ran into trouble because tor_timegm() does not believe that time_t should include a count of leap seconds, but FreeBSD's gmtime believes that it should. This disagreement meant that for a certain amount of time each day, instead of calculating the most recent midnight, our voting-schedule functions would calculate the second-most-recent midnight, and lead to an assertion failure. I am calling this a bugfix on 0.2.0.3-alpha when we first started calculating our voting schedule in this way.
Diffstat (limited to 'src/test/test_util.c')
-rw-r--r--src/test/test_util.c26
1 files changed, 25 insertions, 1 deletions
diff --git a/src/test/test_util.c b/src/test/test_util.c
index d43bf781f2..ab63344806 100644
--- a/src/test/test_util.c
+++ b/src/test/test_util.c
@@ -7,6 +7,7 @@
#define COMPAT_TIME_PRIVATE
#define UTIL_MALLOC_PRIVATE
#define PROCESS_WIN32_PRIVATE
+#define TIME_FMT_PRIVATE
#include "lib/testsupport/testsupport.h"
#include "core/or/or.h"
#include "lib/buf/buffers.h"
@@ -111,7 +112,7 @@ static time_t
tor_timegm_wrapper(const struct tm *tm)
{
time_t t;
- if (tor_timegm(tm, &t) < 0)
+ if (tor_timegm_impl(tm, &t) < 0)
return -1;
return t;
}
@@ -1502,6 +1503,28 @@ test_util_parse_http_time(void *arg)
}
static void
+test_util_timegm_real(void *arg)
+{
+ (void)arg;
+ /* Get the real timegm again! We're not testing our impl; we want the
+ * one that will actually get called. */
+#undef tor_timegm
+
+ /* Now check: is timegm the real inverse of gmtime? */
+ time_t now = time(NULL), time2=0;
+ struct tm tm, *p;
+ p = tor_gmtime_r(&now, &tm);
+ tt_ptr_op(p, OP_NE, NULL);
+
+ int r = tor_timegm(&tm, &time2);
+ tt_int_op(r, OP_EQ, 0);
+ tt_i64_op((int64_t) now, OP_EQ, (int64_t) time2);
+
+ done:
+ ;
+}
+
+static void
test_util_config_line(void *arg)
{
char buf[1024];
@@ -7043,6 +7066,7 @@ struct testcase_t util_tests[] = {
UTIL_TEST(monotonic_time_ratchet, TT_FORK),
UTIL_TEST(monotonic_time_zero, 0),
UTIL_TEST(monotonic_time_add_msec, 0),
+ UTIL_TEST(timegm_real, 0),
UTIL_TEST(htonll, 0),
UTIL_TEST(get_unquoted_path, 0),
UTIL_TEST(map_anon, 0),