s4:torture: Initialize param arrays
[Samba.git] / lib / util / tests / time.c
blobec27f567a7198d1f2ac57bf81c8057298e21cc71
1 /*
2 Unix SMB/CIFS implementation.
4 util time testing
6 Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2008
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.
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.
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/>.
22 #include "includes.h"
23 #include "torture/torture.h"
24 #include "torture/local/proto.h"
26 static bool test_null_time(struct torture_context *tctx)
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;
35 static bool test_null_nttime(struct torture_context *tctx)
37 torture_assert(tctx, null_nttime(0), "0");
38 torture_assert(tctx, !null_nttime(NTTIME_FREEZE), "-1");
39 torture_assert(tctx, !null_nttime(NTTIME_THAW), "-2");
40 torture_assert(tctx, !null_nttime(42), "42");
41 return true;
45 static bool test_http_timestring(struct torture_context *tctx)
47 const char *start = "Thu, 01 Jan 1970";
48 char *result;
50 * Correct test for negative UTC offset. Without the correction, the
51 * test fails when run on hosts with negative UTC offsets, as the date
52 * returned is back in 1969 (pre-epoch).
54 time_t now = time(NULL);
55 struct tm local = *localtime(&now);
56 struct tm gmt = *gmtime(&now);
57 time_t utc_offset = mktime(&local) - mktime(&gmt);
59 result = http_timestring(tctx, 42 - (utc_offset < 0 ? utc_offset : 0));
60 torture_assert(tctx, !strncmp(start, result,
61 strlen(start)), result);
62 torture_assert_str_equal(tctx, "never",
63 http_timestring(tctx, get_time_t_max()), "42");
64 return true;
67 static bool test_timestring(struct torture_context *tctx)
69 const char *start = "Thu Jan 1";
70 char *result;
72 * Correct test for negative UTC offset. Without the correction, the
73 * test fails when run on hosts with negative UTC offsets, as the date
74 * returned is back in 1969 (pre-epoch).
76 time_t now = time(NULL);
77 struct tm local = *localtime(&now);
78 struct tm gmt = *gmtime(&now);
79 time_t utc_offset = mktime(&local) - mktime(&gmt);
81 result = timestring(tctx, 42 - (utc_offset < 0 ? utc_offset : 0));
82 torture_assert(tctx, !strncmp(start, result, strlen(start)), result);
83 return true;
86 static bool test_normalize_timespec(struct torture_context *tctx)
88 const struct {
89 time_t in_s; long in_ns;
90 time_t out_s; long out_ns;
91 } data [] = {
92 { 0, 0, 0, 0 }
93 , { 1, 0, 1, 0 }
94 , { -1, 0, -1, 0 }
95 , { 0, 1000000000, 1, 0 }
96 , { 0, 2000000000, 2, 0 }
97 , { 0, 1000000001, 1, 1 }
98 , { 0, 2000000001, 2, 1 }
99 , { 0, -1000000000, -1, 0 }
100 , { 0, -2000000000, -2, 0 }
101 , { 0, -1000000001, -2, 999999999 }
102 , { 0, -2000000001, -3, 999999999 }
103 , { 0, -1, -1, 999999999 }
104 , { 1, -1, 0, 999999999 }
105 , { -1, -1, -2, 999999999 }
106 , { 0, 999999999, 0, 999999999 }
107 , { 0, 1999999999, 1, 999999999 }
108 , { 0, 2999999999, 2, 999999999 }
109 , { 0, -999999999, -1, 1 }
110 , { 0, -1999999999, -2, 1 }
111 , { 0, -2999999999, -3, 1 }
112 , { LONG_MAX, 1000000001, LONG_MAX, 999999999 } /* overflow */
113 , { LONG_MAX, 999999999, LONG_MAX, 999999999 } /* harmless */
114 , { LONG_MAX, -1, LONG_MAX-1, 999999999 } /* -1 */
115 , { LONG_MIN, -1000000001, LONG_MIN, 0 } /* overflow */
116 , { LONG_MIN, 0, LONG_MIN, 0 } /* harmless */
117 , { LONG_MIN, 1000000000, LONG_MIN+1, 0 } /* +1 */
119 int i;
121 for (i = 0; i < sizeof(data) / sizeof(data[0]); ++i) {
122 struct timespec ts = (struct timespec)
123 { .tv_sec = data[i].in_s
124 , .tv_nsec = data[i].in_ns };
126 normalize_timespec(&ts);
128 torture_assert_int_equal(tctx, ts.tv_sec, data[i].out_s,
129 "mismatch in tv_sec");
130 torture_assert_int_equal(tctx, ts.tv_nsec, data[i].out_ns,
131 "mismatch in tv_nsec");
134 return true;
137 struct torture_suite *torture_local_util_time(TALLOC_CTX *mem_ctx)
139 struct torture_suite *suite = torture_suite_create(mem_ctx, "time");
141 torture_suite_add_simple_test(suite, "null_time", test_null_time);
142 torture_suite_add_simple_test(suite, "null_nttime", test_null_nttime);
143 torture_suite_add_simple_test(suite, "http_timestring",
144 test_http_timestring);
145 torture_suite_add_simple_test(suite, "timestring",
146 test_timestring);
147 torture_suite_add_simple_test(suite, "normalize_timespec",
148 test_normalize_timespec);
150 return suite;