From: Joseph Sutton Date: Mon, 10 May 2021 22:03:34 +0000 (+1200) Subject: audit logging tests: Fix flapping test X-Git-Tag: tevent-0.11.0~931 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=556b114f11c9fbe806d960417e2777560a163793;p=thirdparty%2Fsamba.git 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 Reviewed-by: Andrew Bartlett Reviewed-by: Gary Lockyer Reviewed-by: Andreas Schneider Autobuild-User(master): Andreas Schneider Autobuild-Date(master): Tue May 11 07:03:35 UTC 2021 on sn-devel-184 --- 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);