1
0
mirror of https://github.com/samba-team/samba.git synced 2025-02-04 17:47:26 +03:00
Andrew Kroeger a32f4dd3cf util:tests: Correct time tests for negative UTC offsets.
All:

Please find attached a patch to fix the timestring and http_timestring
tests on hosts that have a negative UTC offset (west of the Prime Meridian).

Sincerely,
Andrew Kroeger

>From 8a8ca35edccf64aa98f2f3ae1469c4c27db8215e Mon Sep 17 00:00:00 2001
From: Andrew Kroeger <andrew@id10ts.net>
Date: Fri, 4 Sep 2009 01:31:50 -0500
Subject: [PATCH] util:tests: Correct time tests for negative UTC offsets.

The timestring and http_timestring tests were failing on hosts with negative
offsets from UTC.  Due to the timezone offset, the returned values were back in
the year 1969 (before the epoch) and did not match the test patterns.

The correction computes the offset from UTC, and if it is negative that offset
is added onto the value given to the timestring() and http_timestring() calls so
that the returned values fall on 01-Jan-1970 and match the test pattern.
2009-09-05 10:06:29 +10:00

116 lines
3.6 KiB
C

/*
Unix SMB/CIFS implementation.
util time testing
Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2008
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "includes.h"
#include "torture/torture.h"
static bool test_null_time(struct torture_context *tctx)
{
torture_assert(tctx, null_time(0), "0");
torture_assert(tctx, null_time(0xFFFFFFFF), "0xFFFFFFFF");
torture_assert(tctx, null_time(-1), "-1");
torture_assert(tctx, !null_time(42), "42");
return true;
}
static bool test_null_nttime(struct torture_context *tctx)
{
torture_assert(tctx, null_nttime(-1), "-1");
torture_assert(tctx, null_nttime(-1), "-1");
torture_assert(tctx, !null_nttime(42), "42");
return true;
}
static bool test_http_timestring(struct torture_context *tctx)
{
const char *start = "Thu, 01 Jan 1970";
char *result;
/*
* Correct test for negative UTC offset. Without the correction, the
* test fails when run on hosts with negative UTC offsets, as the date
* returned is back in 1969 (pre-epoch).
*/
time_t now = time(NULL);
struct tm local = *localtime(&now);
struct tm gmt = *gmtime(&now);
time_t utc_offset = mktime(&local) - mktime(&gmt);
result = http_timestring(tctx, 42 - (utc_offset < 0 ? utc_offset : 0));
torture_assert(tctx, !strncmp(start, result,
strlen(start)), result);
torture_assert_str_equal(tctx, "never",
http_timestring(tctx, get_time_t_max()), "42");
return true;
}
static bool test_timestring(struct torture_context *tctx)
{
const char *start = "Thu Jan 1";
char *result;
/*
* Correct test for negative UTC offset. Without the correction, the
* test fails when run on hosts with negative UTC offsets, as the date
* returned is back in 1969 (pre-epoch).
*/
time_t now = time(NULL);
struct tm local = *localtime(&now);
struct tm gmt = *gmtime(&now);
time_t utc_offset = mktime(&local) - mktime(&gmt);
result = timestring(tctx, 42 - (utc_offset < 0 ? utc_offset : 0));
torture_assert(tctx, !strncmp(start, result, strlen(start)),
result);
return true;
}
static bool test_get_time_zone(struct torture_context *tctx)
{
time_t t = time(NULL);
int old_extra_time_offset = extra_time_offset;
int old_offset, new_offset;
/* test that extra_time_offset works */
old_offset = get_time_zone(t);
extra_time_offset = 42;
new_offset = get_time_zone(t);
extra_time_offset = old_extra_time_offset;
torture_assert_int_equal(tctx, old_offset+60*42, new_offset,
"time offset not used");
return true;
}
struct torture_suite *torture_local_util_time(TALLOC_CTX *mem_ctx)
{
struct torture_suite *suite = torture_suite_create(mem_ctx, "TIME");
torture_suite_add_simple_test(suite, "null_time", test_null_time);
torture_suite_add_simple_test(suite, "get_time_zone", test_get_time_zone);
torture_suite_add_simple_test(suite, "null_nttime", test_null_nttime);
torture_suite_add_simple_test(suite, "http_timestring",
test_http_timestring);
torture_suite_add_simple_test(suite, "timestring",
test_timestring);
return suite;
}