source: branches/samba-3.5.x/lib/util/tests/time.c

Last change on this file was 414, checked in by Herwig Bauernfeind, 15 years ago

Samba 3.5.0: Initial import

File size: 3.6 KB
Line 
1/*
2 Unix SMB/CIFS implementation.
3
4 util time testing
5
6 Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2008
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
20*/
21
22#include "includes.h"
23#include "torture/torture.h"
24
25static bool test_null_time(struct torture_context *tctx)
26{
27 torture_assert(tctx, null_time(0), "0");
28 torture_assert(tctx, null_time(0xFFFFFFFF), "0xFFFFFFFF");
29 torture_assert(tctx, null_time(-1), "-1");
30 torture_assert(tctx, !null_time(42), "42");
31 return true;
32}
33
34static bool test_null_nttime(struct torture_context *tctx)
35{
36 torture_assert(tctx, null_nttime(-1), "-1");
37 torture_assert(tctx, null_nttime(-1), "-1");
38 torture_assert(tctx, !null_nttime(42), "42");
39 return true;
40}
41
42
43static bool test_http_timestring(struct torture_context *tctx)
44{
45 const char *start = "Thu, 01 Jan 1970";
46 char *result;
47 /*
48 * Correct test for negative UTC offset. Without the correction, the
49 * test fails when run on hosts with negative UTC offsets, as the date
50 * returned is back in 1969 (pre-epoch).
51 */
52 time_t now = time(NULL);
53 struct tm local = *localtime(&now);
54 struct tm gmt = *gmtime(&now);
55 time_t utc_offset = mktime(&local) - mktime(&gmt);
56
57 result = http_timestring(tctx, 42 - (utc_offset < 0 ? utc_offset : 0));
58 torture_assert(tctx, !strncmp(start, result,
59 strlen(start)), result);
60 torture_assert_str_equal(tctx, "never",
61 http_timestring(tctx, get_time_t_max()), "42");
62 return true;
63}
64
65static bool test_timestring(struct torture_context *tctx)
66{
67 const char *start = "Thu Jan 1";
68 char *result;
69 /*
70 * Correct test for negative UTC offset. Without the correction, the
71 * test fails when run on hosts with negative UTC offsets, as the date
72 * returned is back in 1969 (pre-epoch).
73 */
74 time_t now = time(NULL);
75 struct tm local = *localtime(&now);
76 struct tm gmt = *gmtime(&now);
77 time_t utc_offset = mktime(&local) - mktime(&gmt);
78
79 result = timestring(tctx, 42 - (utc_offset < 0 ? utc_offset : 0));
80 torture_assert(tctx, !strncmp(start, result, strlen(start)),
81 result);
82 return true;
83}
84
85static bool test_get_time_zone(struct torture_context *tctx)
86{
87 time_t t = time(NULL);
88 int old_extra_time_offset = extra_time_offset;
89 int old_offset, new_offset;
90 /* test that extra_time_offset works */
91
92 old_offset = get_time_zone(t);
93 extra_time_offset = 42;
94 new_offset = get_time_zone(t);
95 extra_time_offset = old_extra_time_offset;
96 torture_assert_int_equal(tctx, old_offset+60*42, new_offset,
97 "time offset not used");
98 return true;
99}
100
101
102struct torture_suite *torture_local_util_time(TALLOC_CTX *mem_ctx)
103{
104 struct torture_suite *suite = torture_suite_create(mem_ctx, "TIME");
105
106 torture_suite_add_simple_test(suite, "null_time", test_null_time);
107 torture_suite_add_simple_test(suite, "get_time_zone", test_get_time_zone);
108 torture_suite_add_simple_test(suite, "null_nttime", test_null_nttime);
109 torture_suite_add_simple_test(suite, "http_timestring",
110 test_http_timestring);
111 torture_suite_add_simple_test(suite, "timestring",
112 test_timestring);
113
114 return suite;
115}
Note: See TracBrowser for help on using the repository browser.