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 | #include "torture/local/proto.h"
|
---|
25 |
|
---|
26 | static bool test_null_time(struct torture_context *tctx)
|
---|
27 | {
|
---|
28 | torture_assert(tctx, null_time(0), "0");
|
---|
29 | torture_assert(tctx, null_time(0xFFFFFFFF), "0xFFFFFFFF");
|
---|
30 | torture_assert(tctx, null_time(-1), "-1");
|
---|
31 | torture_assert(tctx, !null_time(42), "42");
|
---|
32 | return true;
|
---|
33 | }
|
---|
34 |
|
---|
35 | static bool test_null_nttime(struct torture_context *tctx)
|
---|
36 | {
|
---|
37 | torture_assert(tctx, null_nttime(-1), "-1");
|
---|
38 | torture_assert(tctx, null_nttime(-1), "-1");
|
---|
39 | torture_assert(tctx, !null_nttime(42), "42");
|
---|
40 | return true;
|
---|
41 | }
|
---|
42 |
|
---|
43 |
|
---|
44 | static bool test_http_timestring(struct torture_context *tctx)
|
---|
45 | {
|
---|
46 | const char *start = "Thu, 01 Jan 1970";
|
---|
47 | char *result;
|
---|
48 | /*
|
---|
49 | * Correct test for negative UTC offset. Without the correction, the
|
---|
50 | * test fails when run on hosts with negative UTC offsets, as the date
|
---|
51 | * returned is back in 1969 (pre-epoch).
|
---|
52 | */
|
---|
53 | time_t now = time(NULL);
|
---|
54 | struct tm local = *localtime(&now);
|
---|
55 | struct tm gmt = *gmtime(&now);
|
---|
56 | time_t utc_offset = mktime(&local) - mktime(&gmt);
|
---|
57 |
|
---|
58 | result = http_timestring(tctx, 42 - (utc_offset < 0 ? utc_offset : 0));
|
---|
59 | torture_assert(tctx, !strncmp(start, result,
|
---|
60 | strlen(start)), result);
|
---|
61 | torture_assert_str_equal(tctx, "never",
|
---|
62 | http_timestring(tctx, get_time_t_max()), "42");
|
---|
63 | return true;
|
---|
64 | }
|
---|
65 |
|
---|
66 | static bool test_timestring(struct torture_context *tctx)
|
---|
67 | {
|
---|
68 | const char *start = "Thu Jan 1";
|
---|
69 | char *result;
|
---|
70 | /*
|
---|
71 | * Correct test for negative UTC offset. Without the correction, the
|
---|
72 | * test fails when run on hosts with negative UTC offsets, as the date
|
---|
73 | * returned is back in 1969 (pre-epoch).
|
---|
74 | */
|
---|
75 | time_t now = time(NULL);
|
---|
76 | struct tm local = *localtime(&now);
|
---|
77 | struct tm gmt = *gmtime(&now);
|
---|
78 | time_t utc_offset = mktime(&local) - mktime(&gmt);
|
---|
79 |
|
---|
80 | result = timestring(tctx, 42 - (utc_offset < 0 ? utc_offset : 0));
|
---|
81 | torture_assert(tctx, !strncmp(start, result, strlen(start)), result);
|
---|
82 | return true;
|
---|
83 | }
|
---|
84 |
|
---|
85 | struct torture_suite *torture_local_util_time(TALLOC_CTX *mem_ctx)
|
---|
86 | {
|
---|
87 | struct torture_suite *suite = torture_suite_create(mem_ctx, "time");
|
---|
88 |
|
---|
89 | torture_suite_add_simple_test(suite, "null_time", test_null_time);
|
---|
90 | torture_suite_add_simple_test(suite, "null_nttime", test_null_nttime);
|
---|
91 | torture_suite_add_simple_test(suite, "http_timestring",
|
---|
92 | test_http_timestring);
|
---|
93 | torture_suite_add_simple_test(suite, "timestring",
|
---|
94 | test_timestring);
|
---|
95 |
|
---|
96 | return suite;
|
---|
97 | }
|
---|