summaryrefslogtreecommitdiff
path: root/lib/audit_logging
diff options
context:
space:
mode:
authorJoseph Sutton <josephsutton@catalyst.net.nz>2021-05-11 10:03:34 +1200
committerAndreas Schneider <asn@cryptomilk.org>2021-05-11 07:03:35 +0000
commit556b114f11c9fbe806d960417e2777560a163793 (patch)
tree89f20e5190c677936e0f7a8bc128d0040bc369f7 /lib/audit_logging
parent21934c09bdcad04633f0e77ad8136b8f2c21b9e5 (diff)
downloadsamba-556b114f11c9fbe806d960417e2777560a163793.tar.gz
audit logging tests: Fix flapping test
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
Diffstat (limited to 'lib/audit_logging')
-rw-r--r--lib/audit_logging/tests/audit_logging_test.c30
1 files changed, 21 insertions, 9 deletions
diff --git a/lib/audit_logging/tests/audit_logging_test.c b/lib/audit_logging/tests/audit_logging_test.c
index 8c949e5f8fc..1f871c2e5f4 100644
--- a/lib/audit_logging/tests/audit_logging_test.c
+++ b/lib/audit_logging/tests/audit_logging_test.c
@@ -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);