Debian Codegraph @

tests/isc/time_test.c

raw
1 /*
2 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3 *
4 * SPDX-License-Identifier: MPL-2.0
5 *
6 * This Source Code Form is subject to the terms of the Mozilla Public
7 * License, v. 2.0. If a copy of the MPL was not distributed with this
8 * file, you can obtain one at https://mozilla.org/MPL/2.0/.
9 *
10 * See the COPYRIGHT file distributed with this work for additional
11 * information regarding copyright ownership.
12 */
13
14 #include <inttypes.h>
15 #include <sched.h> /* IWYU pragma: keep */
16 #include <setjmp.h>
17 #include <stdarg.h>
18 #include <stddef.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22
23 #define UNIT_TESTING
24 #include <cmocka.h>
25
26 #include <isc/result.h>
27 #include <isc/time.h>
28 #include <isc/util.h>
29
30 #include "time.c"
31
32 #include <tests/isc.h>
33
34 #define MAX_NS (NS_PER_SEC - 1)
35
36 struct time_vectors {
37 isc_time_t a;
38 isc_interval_t b;
39 isc_time_t r;
40 isc_result_t result;
41 };
42
43 const struct time_vectors vectors_add[8] = {
44 { { 0, 0 }, { 0, 0 }, { 0, 0 }, ISC_R_SUCCESS },
45 { { 0, MAX_NS }, { 0, MAX_NS }, { 1, MAX_NS - 1 }, ISC_R_SUCCESS },
46 { { 0, NS_PER_SEC / 2 },
47 { 0, NS_PER_SEC / 2 },
48 { 1, 0 },
49 ISC_R_SUCCESS },
50 { { UINT_MAX, MAX_NS }, { 0, 0 }, { UINT_MAX, MAX_NS }, ISC_R_SUCCESS },
51 { { UINT_MAX, 0 }, { 0, MAX_NS }, { UINT_MAX, MAX_NS }, ISC_R_SUCCESS },
52 { { UINT_MAX, 0 }, { 1, 0 }, { 0, 0 }, ISC_R_RANGE },
53 { { UINT_MAX, MAX_NS }, { 0, 1 }, { 0, 0 }, ISC_R_RANGE },
54 { { UINT_MAX / 2 + 1, NS_PER_SEC / 2 },
55 { UINT_MAX / 2, NS_PER_SEC / 2 },
56 { 0, 0 },
57 ISC_R_RANGE },
58 };
59
60 const struct time_vectors vectors_sub[7] = {
61 { { 0, 0 }, { 0, 0 }, { 0, 0 }, ISC_R_SUCCESS },
62 { { 1, 0 }, { 0, MAX_NS }, { 0, 1 }, ISC_R_SUCCESS },
63 { { 1, NS_PER_SEC / 2 },
64 { 0, MAX_NS },
65 { 0, NS_PER_SEC / 2 + 1 },
66 ISC_R_SUCCESS },
67 { { UINT_MAX, MAX_NS }, { UINT_MAX, 0 }, { 0, MAX_NS }, ISC_R_SUCCESS },
68 { { 0, 0 }, { 1, 0 }, { 0, 0 }, ISC_R_RANGE },
69 { { 0, 0 }, { 0, MAX_NS }, { 0, 0 }, ISC_R_RANGE },
70 };
71
72 ISC_RUN_TEST_IMPL(isc_time_add_test) {
73 UNUSED(state);
74
75 for (size_t i = 0; i < ARRAY_SIZE(vectors_add); i++) {
76 isc_time_t r = { UINT_MAX, UINT_MAX };
77 isc_result_t result = isc_time_add(&(vectors_add[i].a),
78 &(vectors_add[i].b), &r);
79 assert_int_equal(result, vectors_add[i].result);
80 if (result != ISC_R_SUCCESS) {
81 continue;
82 }
83
84 assert_int_equal(r.seconds, vectors_add[i].r.seconds);
85 assert_int_equal(r.nanoseconds, vectors_add[i].r.nanoseconds);
86 }
87
88 expect_assert_failure((void)isc_time_add(&(isc_time_t){ 0, MAX_NS + 1 },
89 &(isc_interval_t){ 0, 0 },
90 &(isc_time_t){ 0, 0 }));
91 expect_assert_failure((void)isc_time_add(
92 &(isc_time_t){ 0, 0 }, &(isc_interval_t){ 0, MAX_NS + 1 },
93 &(isc_time_t){ 0, 0 }));
94
95 expect_assert_failure((void)isc_time_add((isc_time_t *)NULL,
96 &(isc_interval_t){ 0, 0 },
97 &(isc_time_t){ 0, 0 }));
98 expect_assert_failure((void)isc_time_add(&(isc_time_t){ 0, 0 },
99 (isc_interval_t *)NULL,
100 &(isc_time_t){ 0, 0 }));
101 expect_assert_failure((void)isc_time_add(
102 &(isc_time_t){ 0, 0 }, &(isc_interval_t){ 0, 0 }, NULL));
103 }
104
105 ISC_RUN_TEST_IMPL(isc_time_sub_test) {
106 UNUSED(state);
107
108 for (size_t i = 0; i < ARRAY_SIZE(vectors_sub); i++) {
109 isc_time_t r = { UINT_MAX, UINT_MAX };
110 isc_result_t result = isc_time_subtract(
111 &(vectors_sub[i].a), &(vectors_sub[i].b), &r);
112 assert_int_equal(result, vectors_sub[i].result);
113 if (result != ISC_R_SUCCESS) {
114 continue;
115 }
116 assert_int_equal(r.seconds, vectors_sub[i].r.seconds);
117 assert_int_equal(r.nanoseconds, vectors_sub[i].r.nanoseconds);
118 }
119
120 expect_assert_failure((void)isc_time_subtract(
121 &(isc_time_t){ 0, MAX_NS + 1 }, &(isc_interval_t){ 0, 0 },
122 &(isc_time_t){ 0, 0 }));
123 expect_assert_failure((void)isc_time_subtract(
124 &(isc_time_t){ 0, 0 }, &(isc_interval_t){ 0, MAX_NS + 1 },
125 &(isc_time_t){ 0, 0 }));
126
127 expect_assert_failure((void)isc_time_subtract((isc_time_t *)NULL,
128 &(isc_interval_t){ 0, 0 },
129 &(isc_time_t){ 0, 0 }));
130 expect_assert_failure((void)isc_time_subtract(&(isc_time_t){ 0, 0 },
131 (isc_interval_t *)NULL,
132 &(isc_time_t){ 0, 0 }));
133 expect_assert_failure((void)isc_time_subtract(
134 &(isc_time_t){ 0, 0 }, &(isc_interval_t){ 0, 0 }, NULL));
135 }
136
137 /* parse http time stamp */
138
139 ISC_RUN_TEST_IMPL(isc_time_parsehttptimestamp_test) {
140 isc_result_t result;
141 isc_time_t t, x;
142 char buf[ISC_FORMATHTTPTIMESTAMP_SIZE];
143
144 UNUSED(state);
145
146 setenv("TZ", "America/Los_Angeles", 1);
147 t = isc_time_now();
148
149 isc_time_formathttptimestamp(&t, buf, sizeof(buf));
150 result = isc_time_parsehttptimestamp(buf, &x);
151 assert_int_equal(result, ISC_R_SUCCESS);
152 assert_int_equal(isc_time_seconds(&t), isc_time_seconds(&x));
153 }
154
155 /* print UTC in ISO8601 */
156
157 ISC_RUN_TEST_IMPL(isc_time_formatISO8601_test) {
158 isc_time_t t;
159 char buf[64];
160
161 UNUSED(state);
162
163 setenv("TZ", "America/Los_Angeles", 1);
164 t = isc_time_now();
165
166 /* check formatting: yyyy-mm-ddThh:mm:ssZ */
167 memset(buf, 'X', sizeof(buf));
168 isc_time_formatISO8601(&t, buf, sizeof(buf));
169 assert_int_equal(strlen(buf), 20);
170 assert_int_equal(buf[4], '-');
171 assert_int_equal(buf[7], '-');
172 assert_int_equal(buf[10], 'T');
173 assert_int_equal(buf[13], ':');
174 assert_int_equal(buf[16], ':');
175 assert_int_equal(buf[19], 'Z');
176
177 /* check time conversion correctness */
178 memset(buf, 'X', sizeof(buf));
179 isc_time_settoepoch(&t);
180 isc_time_formatISO8601(&t, buf, sizeof(buf));
181 assert_string_equal(buf, "1970-01-01T00:00:00Z");
182
183 memset(buf, 'X', sizeof(buf));
184 isc_time_set(&t, 1450000000, 123000000);
185 isc_time_formatISO8601(&t, buf, sizeof(buf));
186 assert_string_equal(buf, "2015-12-13T09:46:40Z");
187 }
188
189 /* print UTC in ISO8601 with milliseconds */
190
191 ISC_RUN_TEST_IMPL(isc_time_formatISO8601ms_test) {
192 isc_time_t t;
193 char buf[64];
194
195 UNUSED(state);
196
197 setenv("TZ", "America/Los_Angeles", 1);
198 t = isc_time_now();
199
200 /* check formatting: yyyy-mm-ddThh:mm:ss.sssZ */
201 memset(buf, 'X', sizeof(buf));
202 isc_time_formatISO8601ms(&t, buf, sizeof(buf));
203 assert_int_equal(strlen(buf), 24);
204 assert_int_equal(buf[4], '-');
205 assert_int_equal(buf[7], '-');
206 assert_int_equal(buf[10], 'T');
207 assert_int_equal(buf[13], ':');
208 assert_int_equal(buf[16], ':');
209 assert_int_equal(buf[19], '.');
210 assert_int_equal(buf[23], 'Z');
211
212 /* check time conversion correctness */
213 memset(buf, 'X', sizeof(buf));
214 isc_time_settoepoch(&t);
215 isc_time_formatISO8601ms(&t, buf, sizeof(buf));
216 assert_string_equal(buf, "1970-01-01T00:00:00.000Z");
217
218 memset(buf, 'X', sizeof(buf));
219 isc_time_set(&t, 1450000000, 123000000);
220 isc_time_formatISO8601ms(&t, buf, sizeof(buf));
221 assert_string_equal(buf, "2015-12-13T09:46:40.123Z");
222 }
223
224 /* print UTC in ISO8601 with microseconds */
225
226 ISC_RUN_TEST_IMPL(isc_time_formatISO8601us_test) {
227 isc_time_t t;
228 char buf[64];
229
230 UNUSED(state);
231
232 setenv("TZ", "America/Los_Angeles", 1);
233 t = isc_time_now_hires();
234
235 /* check formatting: yyyy-mm-ddThh:mm:ss.ssssssZ */
236 memset(buf, 'X', sizeof(buf));
237 isc_time_formatISO8601us(&t, buf, sizeof(buf));
238 assert_int_equal(strlen(buf), 27);
239 assert_int_equal(buf[4], '-');
240 assert_int_equal(buf[7], '-');
241 assert_int_equal(buf[10], 'T');
242 assert_int_equal(buf[13], ':');
243 assert_int_equal(buf[16], ':');
244 assert_int_equal(buf[19], '.');
245 assert_int_equal(buf[26], 'Z');
246
247 /* check time conversion correctness */
248 memset(buf, 'X', sizeof(buf));
249 isc_time_settoepoch(&t);
250 isc_time_formatISO8601us(&t, buf, sizeof(buf));
251 assert_string_equal(buf, "1970-01-01T00:00:00.000000Z");
252
253 memset(buf, 'X', sizeof(buf));
254 isc_time_set(&t, 1450000000, 123456000);
255 isc_time_formatISO8601us(&t, buf, sizeof(buf));
256 assert_string_equal(buf, "2015-12-13T09:46:40.123456Z");
257 }
258
259 /* print local time in ISO8601 */
260
261 ISC_RUN_TEST_IMPL(isc_time_formatISO8601L_test) {
262 isc_time_t t;
263 char buf[64];
264
265 UNUSED(state);
266
267 setenv("TZ", "America/Los_Angeles", 1);
268 t = isc_time_now();
269
270 /* check formatting: yyyy-mm-ddThh:mm:ss */
271 memset(buf, 'X', sizeof(buf));
272 isc_time_formatISO8601L(&t, buf, sizeof(buf));
273 assert_int_equal(strlen(buf), 19);
274 assert_int_equal(buf[4], '-');
275 assert_int_equal(buf[7], '-');
276 assert_int_equal(buf[10], 'T');
277 assert_int_equal(buf[13], ':');
278 assert_int_equal(buf[16], ':');
279
280 /* check time conversion correctness */
281 memset(buf, 'X', sizeof(buf));
282 isc_time_settoepoch(&t);
283 isc_time_formatISO8601L(&t, buf, sizeof(buf));
284 assert_string_equal(buf, "1969-12-31T16:00:00");
285
286 memset(buf, 'X', sizeof(buf));
287 isc_time_set(&t, 1450000000, 123000000);
288 isc_time_formatISO8601L(&t, buf, sizeof(buf));
289 assert_string_equal(buf, "2015-12-13T01:46:40");
290 }
291
292 /* print local time in ISO8601 with milliseconds */
293
294 ISC_RUN_TEST_IMPL(isc_time_formatISO8601Lms_test) {
295 isc_time_t t;
296 char buf[64];
297
298 UNUSED(state);
299
300 setenv("TZ", "America/Los_Angeles", 1);
301 t = isc_time_now();
302
303 /* check formatting: yyyy-mm-ddThh:mm:ss.sss */
304 memset(buf, 'X', sizeof(buf));
305 isc_time_formatISO8601Lms(&t, buf, sizeof(buf));
306 assert_int_equal(strlen(buf), 23);
307 assert_int_equal(buf[4], '-');
308 assert_int_equal(buf[7], '-');
309 assert_int_equal(buf[10], 'T');
310 assert_int_equal(buf[13], ':');
311 assert_int_equal(buf[16], ':');
312 assert_int_equal(buf[19], '.');
313
314 /* check time conversion correctness */
315 memset(buf, 'X', sizeof(buf));
316 isc_time_settoepoch(&t);
317 isc_time_formatISO8601Lms(&t, buf, sizeof(buf));
318 assert_string_equal(buf, "1969-12-31T16:00:00.000");
319
320 memset(buf, 'X', sizeof(buf));
321 isc_time_set(&t, 1450000000, 123000000);
322 isc_time_formatISO8601Lms(&t, buf, sizeof(buf));
323 assert_string_equal(buf, "2015-12-13T01:46:40.123");
324 }
325
326 /* print local time in ISO8601 with microseconds */
327
328 ISC_RUN_TEST_IMPL(isc_time_formatISO8601Lus_test) {
329 isc_time_t t;
330 char buf[64];
331
332 UNUSED(state);
333
334 setenv("TZ", "America/Los_Angeles", 1);
335 t = isc_time_now_hires();
336
337 /* check formatting: yyyy-mm-ddThh:mm:ss.ssssss */
338 memset(buf, 'X', sizeof(buf));
339 isc_time_formatISO8601Lus(&t, buf, sizeof(buf));
340 assert_int_equal(strlen(buf), 26);
341 assert_int_equal(buf[4], '-');
342 assert_int_equal(buf[7], '-');
343 assert_int_equal(buf[10], 'T');
344 assert_int_equal(buf[13], ':');
345 assert_int_equal(buf[16], ':');
346 assert_int_equal(buf[19], '.');
347
348 /* check time conversion correctness */
349 memset(buf, 'X', sizeof(buf));
350 isc_time_settoepoch(&t);
351 isc_time_formatISO8601Lus(&t, buf, sizeof(buf));
352 assert_string_equal(buf, "1969-12-31T16:00:00.000000");
353
354 memset(buf, 'X', sizeof(buf));
355 isc_time_set(&t, 1450000000, 123456000);
356 isc_time_formatISO8601Lus(&t, buf, sizeof(buf));
357 assert_string_equal(buf, "2015-12-13T01:46:40.123456");
358 }
359
360 /* print UTC time as yyyymmddhhmmsssss */
361
362 ISC_RUN_TEST_IMPL(isc_time_formatshorttimestamp_test) {
363 isc_time_t t;
364 char buf[64];
365
366 UNUSED(state);
367
368 setenv("TZ", "America/Los_Angeles", 1);
369 t = isc_time_now();
370
371 /* check formatting: yyyymmddhhmmsssss */
372 memset(buf, 'X', sizeof(buf));
373 isc_time_formatshorttimestamp(&t, buf, sizeof(buf));
374 assert_int_equal(strlen(buf), 17);
375
376 /* check time conversion correctness */
377 memset(buf, 'X', sizeof(buf));
378 isc_time_settoepoch(&t);
379 isc_time_formatshorttimestamp(&t, buf, sizeof(buf));
380 assert_string_equal(buf, "19700101000000000");
381
382 memset(buf, 'X', sizeof(buf));
383 isc_time_set(&t, 1450000000, 123000000);
384 isc_time_formatshorttimestamp(&t, buf, sizeof(buf));
385 assert_string_equal(buf, "20151213094640123");
386 }
387
388 ISC_TEST_LIST_START
389
390 ISC_TEST_ENTRY(isc_time_add_test)
391 ISC_TEST_ENTRY(isc_time_sub_test)
392 ISC_TEST_ENTRY(isc_time_parsehttptimestamp_test)
393 ISC_TEST_ENTRY(isc_time_formatISO8601_test)
394 ISC_TEST_ENTRY(isc_time_formatISO8601ms_test)
395 ISC_TEST_ENTRY(isc_time_formatISO8601us_test)
396 ISC_TEST_ENTRY(isc_time_formatISO8601L_test)
397 ISC_TEST_ENTRY(isc_time_formatISO8601Lms_test)
398 ISC_TEST_ENTRY(isc_time_formatISO8601Lus_test)
399 ISC_TEST_ENTRY(isc_time_formatshorttimestamp_test)
400
401 ISC_TEST_LIST_END
402
403 ISC_TEST_MAIN