mirror of
https://github.com/samba-team/samba.git
synced 2024-12-22 13:34:15 +03:00
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
This commit is contained in:
parent
21934c09bd
commit
556b114f11
@ -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);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user