]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
dsdb audit tests: Fix flapping test
authorJoseph Sutton <josephsutton@catalyst.net.nz>
Tue, 28 Sep 2021 07:42:36 +0000 (20:42 +1300)
committerAndrew Bartlett <abartlet@samba.org>
Thu, 17 Mar 2022 00:41:34 +0000 (00:41 +0000)
Use gettimeofday() to obtain the current time for comparison, to be
consistent with audit_logging.c. On Linux, time() may occasionally
return a smaller value than gettimeofday(), despite being called later.

Signed-off-by: Joseph Sutton <josephsutton@catalyst.net.nz>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
source4/dsdb/samdb/ldb_modules/tests/test_audit_log.c
source4/dsdb/samdb/ldb_modules/tests/test_group_audit.c

index 2fba2406b64b9e24410da6a99ecdd7b4996568c0..2885b7fe744d518303a6d206e2b57752552d2e35 100644 (file)
@@ -41,10 +41,12 @@ static void check_timestamp(time_t before, const char* timestamp)
        struct tm tm;
        time_t after;
        time_t actual;
-       const double lower = -1;
+       struct timeval tv;
 
 
-       after = time(NULL);
+       rc = gettimeofday(&tv, NULL);
+       assert_return_code(rc, errno);
+       after = tv.tv_sec;
 
        /*
         * Convert the ISO 8601 timestamp into a time_t
@@ -71,12 +73,9 @@ static void check_timestamp(time_t before, const char* timestamp)
 
        /*
         * The timestamp should be before <= actual <= after
-        * Note: as the microsecond portion of the time is truncated we use
-        *       a -1 as the lower bound for the time difference instead of
-        *       zero
         */
-       assert_true(difftime(actual, before) >= lower);
-       assert_true(difftime(after, actual) >= lower);
+       assert_true(difftime(actual, before) >= 0);
+       assert_true(difftime(after, actual) >= 0);
 }
 
 static void test_has_password_changed(void **state)
@@ -295,6 +294,8 @@ static void test_operation_json_empty(void **state)
        json_t *v = NULL;
        json_t *o = NULL;
        time_t before;
+       struct timeval tv;
+       int rc;
 
 
        TALLOC_CTX *ctx = talloc_new(NULL);
@@ -310,7 +311,9 @@ static void test_operation_json_empty(void **state)
        reply = talloc_zero(ctx, struct ldb_reply);
        reply->error = LDB_SUCCESS;
 
-       before = time(NULL);
+       rc = gettimeofday(&tv, NULL);
+       assert_return_code(rc, errno);
+       before = tv.tv_sec;
        json = operation_json(module, req, reply);
        assert_int_equal(3, json_object_size(json.root));
 
@@ -426,6 +429,8 @@ static void test_operation_json(void **state)
        json_t *f = NULL;
        json_t *g = NULL;
        time_t before;
+       struct timeval tv;
+       int rc;
 
 
        TALLOC_CTX *ctx = talloc_new(NULL);
@@ -465,7 +470,9 @@ static void test_operation_json(void **state)
        reply = talloc_zero(ctx, struct ldb_reply);
        reply->error = LDB_ERR_OPERATIONS_ERROR;
 
-       before = time(NULL);
+       rc = gettimeofday(&tv, NULL);
+       assert_return_code(rc, errno);
+       before = tv.tv_sec;
        json = operation_json(module, req, reply);
        assert_int_equal(3, json_object_size(json.root));
 
@@ -621,6 +628,8 @@ static void test_as_system_operation_json(void **state)
        json_t *f = NULL;
        json_t *g = NULL;
        time_t before;
+       struct timeval tv;
+       int rc;
 
 
        TALLOC_CTX *ctx = talloc_new(NULL);
@@ -669,7 +678,9 @@ static void test_as_system_operation_json(void **state)
        reply = talloc_zero(ctx, struct ldb_reply);
        reply->error = LDB_ERR_OPERATIONS_ERROR;
 
-       before = time(NULL);
+       rc = gettimeofday(&tv, NULL);
+       assert_return_code(rc, errno);
+       before = tv.tv_sec;
        json = operation_json(module, req, reply);
        assert_int_equal(3, json_object_size(json.root));
 
@@ -796,6 +807,8 @@ static void test_password_change_json_empty(void **state)
        json_t *v = NULL;
        json_t *o = NULL;
        time_t before;
+       struct timeval tv;
+       int rc;
 
 
        TALLOC_CTX *ctx = talloc_new(NULL);
@@ -811,7 +824,9 @@ static void test_password_change_json_empty(void **state)
        reply = talloc_zero(ctx, struct ldb_reply);
        reply->error = LDB_SUCCESS;
 
-       before = time(NULL);
+       rc = gettimeofday(&tv, NULL);
+       assert_return_code(rc, errno);
+       before = tv.tv_sec;
        json = password_change_json(module, req, reply);
        assert_int_equal(3, json_object_size(json.root));
 
@@ -899,6 +914,8 @@ static void test_password_change_json(void **state)
        json_t *v = NULL;
        json_t *o = NULL;
        time_t before;
+       struct timeval tv;
+       int rc;
 
        TALLOC_CTX *ctx = talloc_new(NULL);
 
@@ -936,7 +953,9 @@ static void test_password_change_json(void **state)
        reply = talloc_zero(ctx, struct ldb_reply);
        reply->error = LDB_SUCCESS;
 
-       before = time(NULL);
+       rc = gettimeofday(&tv, NULL);
+       assert_return_code(rc, errno);
+       before = tv.tv_sec;
        json = password_change_json(module, req, reply);
        assert_int_equal(3, json_object_size(json.root));
 
@@ -1025,10 +1044,14 @@ static void test_transaction_json(void **state)
        json_t *v = NULL;
        json_t *o = NULL;
        time_t before;
+       struct timeval tv;
+       int rc;
 
        GUID_from_string(GUID, &guid);
 
-       before = time(NULL);
+       rc = gettimeofday(&tv, NULL);
+       assert_return_code(rc, errno);
+       before = tv.tv_sec;
        json = transaction_json("delete", &guid, 10000099);
 
        assert_int_equal(3, json_object_size(json.root));
@@ -1086,10 +1109,14 @@ static void test_commit_failure_json(void **state)
        json_t *v = NULL;
        json_t *o = NULL;
        time_t before;
+       struct timeval tv;
+       int rc;
 
        GUID_from_string(GUID, &guid);
 
-       before = time(NULL);
+       rc = gettimeofday(&tv, NULL);
+       assert_return_code(rc, errno);
+       before = tv.tv_sec;
        json = commit_failure_json(
                "prepare",
                987876,
@@ -1173,6 +1200,8 @@ static void test_replicated_update_json_empty(void **state)
        json_t *v = NULL;
        json_t *o = NULL;
        time_t before;
+       struct timeval tv;
+       int rc;
 
 
        TALLOC_CTX *ctx = talloc_new(NULL);
@@ -1193,7 +1222,9 @@ static void test_replicated_update_json_empty(void **state)
        reply = talloc_zero(ctx, struct ldb_reply);
        reply->error = LDB_SUCCESS;
 
-       before = time(NULL);
+       rc = gettimeofday(&tv, NULL);
+       assert_return_code(rc, errno);
+       before = tv.tv_sec;
        json = replicated_update_json(module, req, reply);
        assert_int_equal(3, json_object_size(json.root));
 
@@ -1309,6 +1340,8 @@ static void test_replicated_update_json(void **state)
        json_t *v = NULL;
        json_t *o = NULL;
        time_t before;
+       struct timeval tv;
+       int rc;
 
 
        TALLOC_CTX *ctx = talloc_new(NULL);
@@ -1345,7 +1378,9 @@ static void test_replicated_update_json(void **state)
        reply = talloc_zero(ctx, struct ldb_reply);
        reply->error = LDB_ERR_NO_SUCH_OBJECT;
 
-       before = time(NULL);
+       rc = gettimeofday(&tv, NULL);
+       assert_return_code(rc, errno);
+       before = tv.tv_sec;
        json = replicated_update_json(module, req, reply);
        assert_int_equal(3, json_object_size(json.root));
 
index 0bbde9f3e3be91e7a0c3126ed0b3bb2b54d55804..f7075f3485e21b1f93c5612c9c360f777ad7a9bb 100644 (file)
@@ -268,9 +268,12 @@ static void _check_timestamp(
        struct tm tm;
        time_t after;
        time_t actual;
+       struct timeval tv;
 
 
-       after = time(NULL);
+       rc = gettimeofday(&tv, NULL);
+       assert_return_code(rc, errno);
+       after = tv.tv_sec;
 
        /*
         * Convert the ISO 8601 timestamp into a time_t
@@ -806,6 +809,8 @@ static void test_audit_group_json(void **state)
        json_t *v = NULL;
        json_t *o = NULL;
        time_t before;
+       struct timeval tv;
+       int rc;
 
 
        TALLOC_CTX *ctx = talloc_new(NULL);
@@ -826,7 +831,9 @@ static void test_audit_group_json(void **state)
        req->operation =  LDB_ADD;
        add_transaction_id(req, TRANSACTION);
 
-       before = time(NULL);
+       rc = gettimeofday(&tv, NULL);
+       assert_return_code(rc, errno);
+       before = tv.tv_sec;
        json = audit_group_json(module,
                                req,
                                "the-action",
@@ -910,6 +917,8 @@ static void test_audit_group_json_error(void **state)
        json_t *v = NULL;
        json_t *o = NULL;
        time_t before;
+       struct timeval tv;
+       int rc;
 
 
        TALLOC_CTX *ctx = talloc_new(NULL);
@@ -930,7 +939,9 @@ static void test_audit_group_json_error(void **state)
        req->operation =  LDB_ADD;
        add_transaction_id(req, TRANSACTION);
 
-       before = time(NULL);
+       rc = gettimeofday(&tv, NULL);
+       assert_return_code(rc, errno);
+       before = tv.tv_sec;
        json = audit_group_json(module,
                                req,
                                "the-action",
@@ -1015,6 +1026,8 @@ static void test_audit_group_json_no_event(void **state)
        json_t *v = NULL;
        json_t *o = NULL;
        time_t before;
+       struct timeval tv;
+       int rc;
 
 
        TALLOC_CTX *ctx = talloc_new(NULL);
@@ -1035,7 +1048,9 @@ static void test_audit_group_json_no_event(void **state)
        req->operation =  LDB_ADD;
        add_transaction_id(req, TRANSACTION);
 
-       before = time(NULL);
+       rc = gettimeofday(&tv, NULL);
+       assert_return_code(rc, errno);
+       before = tv.tv_sec;
        json = audit_group_json(module,
                                req,
                                "the-action",