]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
audit logging tests: Fix flapping test
authorJoseph Sutton <josephsutton@catalyst.net.nz>
Mon, 10 May 2021 22:03:34 +0000 (10:03 +1200)
committerAndreas Schneider <asn@cryptomilk.org>
Tue, 11 May 2021 07:03:35 +0000 (07:03 +0000)
On Linux, gettimeofday() uses the clock's microsecond field to adjust
the returned time in seconds, while time() only takes the seconds field
into account. As a result, time() would occasionally return a smaller
value than gettimeofday(), despite being called later.

Changing the time() calls to gettimeofday() as used in audit_logging.c
makes the time values consistent.

https://stackoverflow.com/questions/22917318/time-and-gettimeofday-return-different-seconds

Signed-off-by: Joseph Sutton <josephsutton@catalyst.net.nz>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
Reviewed-by: Gary Lockyer <gary@catalyst.net.nz>
Reviewed-by: Andreas Schneider <asn@samba.org>
Autobuild-User(master): Andreas Schneider <asn@cryptomilk.org>
Autobuild-Date(master): Tue May 11 07:03:35 UTC 2021 on sn-devel-184

lib/audit_logging/tests/audit_logging_test.c

index 8c949e5f8fcb4a2b5251f7438775110d440f7697..1f871c2e5f4901f9c058177f7385fab396bebc79 100644 (file)
@@ -283,14 +283,21 @@ static void test_json_add_timestamp(_UNUSED_ void **state)
        time_t before;
        time_t after;
        time_t actual;
-       const int adjustment = 1;
-
+       struct timeval tv;
+       int ret;
 
        object = json_new_object();
-       before = time(NULL);
+
+       ret = gettimeofday(&tv, NULL);
+       assert_int_equal(0, ret);
+       before = tv.tv_sec;
+
        rc = json_add_timestamp(&object);
        assert_int_equal(0, rc);
-       after = time(NULL);
+
+       ret = gettimeofday(&tv, NULL);
+       assert_int_equal(0, ret);
+       after = tv.tv_sec;
 
        ts = json_object_get(object.root, "timestamp");
        assert_true(json_is_string(ts));
@@ -321,10 +328,7 @@ static void test_json_add_timestamp(_UNUSED_ void **state)
 
        /*
         * The timestamp should be before <= actual <= after
-        * but we adjust the times to cater for any precision issues.
         */
-       before -= adjustment;
-       after += adjustment;
        assert_true(difftime(actual, before) >= 0);
        assert_true(difftime(after, actual) >= 0);
 
@@ -796,6 +800,8 @@ static void test_audit_get_timestamp(_UNUSED_ void **state)
        time_t before;
        time_t after;
        time_t actual;
+       struct timeval tv;
+       int ret;
        char *env_tz = NULL;
        char *orig_tz = NULL;
 
@@ -810,9 +816,15 @@ static void test_audit_get_timestamp(_UNUSED_ void **state)
        }
        setenv("TZ", "UTC", 1);
 
-       before = time(NULL);
+       ret = gettimeofday(&tv, NULL);
+       assert_int_equal(0, ret);
+       before = tv.tv_sec;
+
        t = audit_get_timestamp(ctx);
-       after = time(NULL);
+
+       ret = gettimeofday(&tv, NULL);
+       assert_int_equal(0, ret);
+       after = tv.tv_sec;
 
        c = strptime(t, "%a, %d %b %Y %H:%M:%S", &tm);