audit_logging tests: Fix timezone validation
authorGary Lockyer <gary@catalyst.net.nz>
Mon, 2 Mar 2020 21:44:47 +0000 (10:44 +1300)
committerAndrew Bartlett <abartlet@samba.org>
Sat, 7 Mar 2020 06:37:09 +0000 (06:37 +0000)
test_audit_get_timestamp used the "%Z" format specifier in strptime,
this is non-portable.  Updated tests now explicitly set the time zone to
"UTC".

Signed-off-by: Gary Lockyer <gary@catalyst.net.nz>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
Autobuild-User(master): Andrew Bartlett <abartlet@samba.org>
Autobuild-Date(master): Sat Mar  7 06:37:09 UTC 2020 on sn-devel-184

lib/audit_logging/tests/audit_logging_test.c

index 1efb03b0b516b33f23c3647eedbcaed8d781ba92..8c949e5f8fcb4a2b5251f7438775110d440f7697 100644 (file)
@@ -796,15 +796,35 @@ static void test_audit_get_timestamp(_UNUSED_ void **state)
        time_t before;
        time_t after;
        time_t actual;
+       char *env_tz = NULL;
+       char *orig_tz = NULL;
 
        TALLOC_CTX *ctx = talloc_new(NULL);
 
+       /*
+        * Explicitly set the time zone to UTC to make the test easier
+        */
+       env_tz = getenv("TZ");
+       if (env_tz != NULL) {
+               orig_tz = talloc_strdup(ctx, env_tz);
+       }
+       setenv("TZ", "UTC", 1);
+
        before = time(NULL);
        t = audit_get_timestamp(ctx);
        after = time(NULL);
 
-
        c = strptime(t, "%a, %d %b %Y %H:%M:%S", &tm);
+
+       /*
+        * Restore the time zone if we changed it
+        */
+       if (orig_tz != NULL) {
+               setenv("TZ", orig_tz, 1);
+               TALLOC_FREE(orig_tz);
+       }
+
+       assert_non_null(c);
        tm.tm_isdst = -1;
        if (c != NULL && *c == '.') {
                char *e;
@@ -812,10 +832,9 @@ static void test_audit_get_timestamp(_UNUSED_ void **state)
                c = e;
        }
        if (c != NULL && *c == ' ') {
-               struct tm tz;
-               c = strptime(c, " %Z", &tz);
+               assert_string_equal(" UTC", c);
+               c += 4;
        }
-       assert_non_null(c);
        assert_int_equal(0, strlen(c));
 
        actual = mktime(&tm);