]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-time-util.c
Merge pull request #9500 from zsol/append
[thirdparty/systemd.git] / src / test / test-time-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include "random-util.h"
4 #include "string-util.h"
5 #include "strv.h"
6 #include "time-util.h"
7
8 static void test_parse_sec(void) {
9 usec_t u;
10
11 log_info("/* %s */", __func__);
12
13 assert_se(parse_sec("5s", &u) >= 0);
14 assert_se(u == 5 * USEC_PER_SEC);
15 assert_se(parse_sec("5s500ms", &u) >= 0);
16 assert_se(u == 5 * USEC_PER_SEC + 500 * USEC_PER_MSEC);
17 assert_se(parse_sec(" 5s 500ms ", &u) >= 0);
18 assert_se(u == 5 * USEC_PER_SEC + 500 * USEC_PER_MSEC);
19 assert_se(parse_sec(" 5.5s ", &u) >= 0);
20 assert_se(u == 5 * USEC_PER_SEC + 500 * USEC_PER_MSEC);
21 assert_se(parse_sec(" 5.5s 0.5ms ", &u) >= 0);
22 assert_se(u == 5 * USEC_PER_SEC + 500 * USEC_PER_MSEC + 500);
23 assert_se(parse_sec(" .22s ", &u) >= 0);
24 assert_se(u == 220 * USEC_PER_MSEC);
25 assert_se(parse_sec(" .50y ", &u) >= 0);
26 assert_se(u == USEC_PER_YEAR / 2);
27 assert_se(parse_sec("2.5", &u) >= 0);
28 assert_se(u == 2500 * USEC_PER_MSEC);
29 assert_se(parse_sec(".7", &u) >= 0);
30 assert_se(u == 700 * USEC_PER_MSEC);
31 assert_se(parse_sec("23us", &u) >= 0);
32 assert_se(u == 23);
33 assert_se(parse_sec("23µs", &u) >= 0);
34 assert_se(u == 23);
35 assert_se(parse_sec("infinity", &u) >= 0);
36 assert_se(u == USEC_INFINITY);
37 assert_se(parse_sec(" infinity ", &u) >= 0);
38 assert_se(u == USEC_INFINITY);
39 assert_se(parse_sec("+3.1s", &u) >= 0);
40 assert_se(u == 3100 * USEC_PER_MSEC);
41
42 assert_se(parse_sec(" xyz ", &u) < 0);
43 assert_se(parse_sec("", &u) < 0);
44 assert_se(parse_sec(" . ", &u) < 0);
45 assert_se(parse_sec(" 5. ", &u) < 0);
46 assert_se(parse_sec(".s ", &u) < 0);
47 assert_se(parse_sec("-5s ", &u) < 0);
48 assert_se(parse_sec("-0.3s ", &u) < 0);
49 assert_se(parse_sec("-0.0s ", &u) < 0);
50 assert_se(parse_sec("-0.-0s ", &u) < 0);
51 assert_se(parse_sec("0.-0s ", &u) < 0);
52 assert_se(parse_sec("3.-0s ", &u) < 0);
53 assert_se(parse_sec(" infinity .7", &u) < 0);
54 assert_se(parse_sec(".3 infinity", &u) < 0);
55 assert_se(parse_sec("3.+1s", &u) < 0);
56 assert_se(parse_sec("3. 1s", &u) < 0);
57 assert_se(parse_sec("3.s", &u) < 0);
58 }
59
60 static void test_parse_sec_fix_0(void) {
61 usec_t u;
62
63 log_info("/* %s */", __func__);
64
65 assert_se(parse_sec_fix_0("5s", &u) >= 0);
66 assert_se(u == 5 * USEC_PER_SEC);
67 assert_se(parse_sec_fix_0("0s", &u) >= 0);
68 assert_se(u == 0 * USEC_PER_SEC);
69 assert_se(parse_sec_fix_0("0", &u) >= 0);
70 assert_se(u == USEC_INFINITY);
71 assert_se(parse_sec_fix_0(" 0", &u) >= 0);
72 assert_se(u == USEC_INFINITY);
73 }
74
75 static void test_parse_time(void) {
76 usec_t u;
77
78 log_info("/* %s */", __func__);
79
80 assert_se(parse_time("5", &u, 1) >= 0);
81 assert_se(u == 5);
82
83 assert_se(parse_time("5", &u, USEC_PER_MSEC) >= 0);
84 assert_se(u == 5 * USEC_PER_MSEC);
85
86 assert_se(parse_time("5", &u, USEC_PER_SEC) >= 0);
87 assert_se(u == 5 * USEC_PER_SEC);
88
89 assert_se(parse_time("5s", &u, 1) >= 0);
90 assert_se(u == 5 * USEC_PER_SEC);
91
92 assert_se(parse_time("5s", &u, USEC_PER_SEC) >= 0);
93 assert_se(u == 5 * USEC_PER_SEC);
94
95 assert_se(parse_time("5s", &u, USEC_PER_MSEC) >= 0);
96 assert_se(u == 5 * USEC_PER_SEC);
97 }
98
99 static void test_parse_nsec(void) {
100 nsec_t u;
101
102 log_info("/* %s */", __func__);
103
104 assert_se(parse_nsec("5s", &u) >= 0);
105 assert_se(u == 5 * NSEC_PER_SEC);
106 assert_se(parse_nsec("5s500ms", &u) >= 0);
107 assert_se(u == 5 * NSEC_PER_SEC + 500 * NSEC_PER_MSEC);
108 assert_se(parse_nsec(" 5s 500ms ", &u) >= 0);
109 assert_se(u == 5 * NSEC_PER_SEC + 500 * NSEC_PER_MSEC);
110 assert_se(parse_nsec(" 5.5s ", &u) >= 0);
111 assert_se(u == 5 * NSEC_PER_SEC + 500 * NSEC_PER_MSEC);
112 assert_se(parse_nsec(" 5.5s 0.5ms ", &u) >= 0);
113 assert_se(u == 5 * NSEC_PER_SEC + 500 * NSEC_PER_MSEC + 500 * NSEC_PER_USEC);
114 assert_se(parse_nsec(" .22s ", &u) >= 0);
115 assert_se(u == 220 * NSEC_PER_MSEC);
116 assert_se(parse_nsec(" .50y ", &u) >= 0);
117 assert_se(u == NSEC_PER_YEAR / 2);
118 assert_se(parse_nsec("2.5", &u) >= 0);
119 assert_se(u == 2);
120 assert_se(parse_nsec(".7", &u) >= 0);
121 assert_se(u == 0);
122 assert_se(parse_nsec("infinity", &u) >= 0);
123 assert_se(u == NSEC_INFINITY);
124 assert_se(parse_nsec(" infinity ", &u) >= 0);
125 assert_se(u == NSEC_INFINITY);
126 assert_se(parse_nsec("+3.1s", &u) >= 0);
127 assert_se(u == 3100 * NSEC_PER_MSEC);
128
129 assert_se(parse_nsec(" xyz ", &u) < 0);
130 assert_se(parse_nsec("", &u) < 0);
131 assert_se(parse_nsec(" . ", &u) < 0);
132 assert_se(parse_nsec(" 5. ", &u) < 0);
133 assert_se(parse_nsec(".s ", &u) < 0);
134 assert_se(parse_nsec(" infinity .7", &u) < 0);
135 assert_se(parse_nsec(".3 infinity", &u) < 0);
136 assert_se(parse_nsec("-5s ", &u) < 0);
137 assert_se(parse_nsec("-0.3s ", &u) < 0);
138 assert_se(parse_nsec("-0.0s ", &u) < 0);
139 assert_se(parse_nsec("-0.-0s ", &u) < 0);
140 assert_se(parse_nsec("0.-0s ", &u) < 0);
141 assert_se(parse_nsec("3.-0s ", &u) < 0);
142 assert_se(parse_nsec(" infinity .7", &u) < 0);
143 assert_se(parse_nsec(".3 infinity", &u) < 0);
144 assert_se(parse_nsec("3.+1s", &u) < 0);
145 assert_se(parse_nsec("3. 1s", &u) < 0);
146 assert_se(parse_nsec("3.s", &u) < 0);
147 }
148
149 static void test_format_timespan_one(usec_t x, usec_t accuracy) {
150 char l[FORMAT_TIMESPAN_MAX];
151 const char *t;
152 usec_t y;
153
154 log_info(USEC_FMT" (at accuracy "USEC_FMT")", x, accuracy);
155
156 assert_se(t = format_timespan(l, sizeof l, x, accuracy));
157 log_info(" = <%s>", t);
158
159 assert_se(parse_sec(t, &y) >= 0);
160 log_info(" = "USEC_FMT, y);
161
162 if (accuracy <= 0)
163 accuracy = 1;
164
165 assert_se(x / accuracy == y / accuracy);
166 }
167
168 static void test_format_timespan(usec_t accuracy) {
169 log_info("/* %s accuracy="USEC_FMT" */", __func__, accuracy);
170
171 test_format_timespan_one(0, accuracy);
172 test_format_timespan_one(1, accuracy);
173 test_format_timespan_one(1*USEC_PER_SEC, accuracy);
174 test_format_timespan_one(999*USEC_PER_MSEC, accuracy);
175 test_format_timespan_one(1234567, accuracy);
176 test_format_timespan_one(12, accuracy);
177 test_format_timespan_one(123, accuracy);
178 test_format_timespan_one(1234, accuracy);
179 test_format_timespan_one(12345, accuracy);
180 test_format_timespan_one(123456, accuracy);
181 test_format_timespan_one(1234567, accuracy);
182 test_format_timespan_one(12345678, accuracy);
183 test_format_timespan_one(1200000, accuracy);
184 test_format_timespan_one(1230000, accuracy);
185 test_format_timespan_one(1230000, accuracy);
186 test_format_timespan_one(1234000, accuracy);
187 test_format_timespan_one(1234500, accuracy);
188 test_format_timespan_one(1234560, accuracy);
189 test_format_timespan_one(1234567, accuracy);
190 test_format_timespan_one(986087, accuracy);
191 test_format_timespan_one(500 * USEC_PER_MSEC, accuracy);
192 test_format_timespan_one(9*USEC_PER_YEAR/5 - 23, accuracy);
193 test_format_timespan_one(USEC_INFINITY, accuracy);
194 }
195
196 static void test_timezone_is_valid(void) {
197 log_info("/* %s */", __func__);
198
199 assert_se(timezone_is_valid("Europe/Berlin", LOG_ERR));
200 assert_se(timezone_is_valid("Australia/Sydney", LOG_ERR));
201 assert_se(!timezone_is_valid("Europe/Do not exist", LOG_ERR));
202 }
203
204 static void test_get_timezones(void) {
205 _cleanup_strv_free_ char **zones = NULL;
206 int r;
207 char **zone;
208
209 log_info("/* %s */", __func__);
210
211 r = get_timezones(&zones);
212 assert_se(r == 0);
213
214 STRV_FOREACH(zone, zones)
215 assert_se(timezone_is_valid(*zone, LOG_ERR));
216 }
217
218 static void test_usec_add(void) {
219 log_info("/* %s */", __func__);
220
221 assert_se(usec_add(0, 0) == 0);
222 assert_se(usec_add(1, 4) == 5);
223 assert_se(usec_add(USEC_INFINITY, 5) == USEC_INFINITY);
224 assert_se(usec_add(5, USEC_INFINITY) == USEC_INFINITY);
225 assert_se(usec_add(USEC_INFINITY-5, 2) == USEC_INFINITY-3);
226 assert_se(usec_add(USEC_INFINITY-2, 2) == USEC_INFINITY);
227 assert_se(usec_add(USEC_INFINITY-1, 2) == USEC_INFINITY);
228 assert_se(usec_add(USEC_INFINITY, 2) == USEC_INFINITY);
229 }
230
231 static void test_usec_sub_unsigned(void) {
232 log_info("/* %s */", __func__);
233
234 assert_se(usec_sub_unsigned(0, 0) == 0);
235 assert_se(usec_sub_unsigned(0, 2) == 0);
236 assert_se(usec_sub_unsigned(0, USEC_INFINITY) == 0);
237 assert_se(usec_sub_unsigned(1, 0) == 1);
238 assert_se(usec_sub_unsigned(1, 1) == 0);
239 assert_se(usec_sub_unsigned(1, 2) == 0);
240 assert_se(usec_sub_unsigned(1, 3) == 0);
241 assert_se(usec_sub_unsigned(1, USEC_INFINITY) == 0);
242 assert_se(usec_sub_unsigned(USEC_INFINITY-1, 0) == USEC_INFINITY-1);
243 assert_se(usec_sub_unsigned(USEC_INFINITY-1, 1) == USEC_INFINITY-2);
244 assert_se(usec_sub_unsigned(USEC_INFINITY-1, 2) == USEC_INFINITY-3);
245 assert_se(usec_sub_unsigned(USEC_INFINITY-1, USEC_INFINITY-2) == 1);
246 assert_se(usec_sub_unsigned(USEC_INFINITY-1, USEC_INFINITY-1) == 0);
247 assert_se(usec_sub_unsigned(USEC_INFINITY-1, USEC_INFINITY) == 0);
248 assert_se(usec_sub_unsigned(USEC_INFINITY, 0) == USEC_INFINITY);
249 assert_se(usec_sub_unsigned(USEC_INFINITY, 1) == USEC_INFINITY);
250 assert_se(usec_sub_unsigned(USEC_INFINITY, 2) == USEC_INFINITY);
251 assert_se(usec_sub_unsigned(USEC_INFINITY, USEC_INFINITY) == USEC_INFINITY);
252 }
253
254 static void test_usec_sub_signed(void) {
255 log_info("/* %s */", __func__);
256
257 assert_se(usec_sub_signed(0, 0) == 0);
258 assert_se(usec_sub_signed(4, 1) == 3);
259 assert_se(usec_sub_signed(4, 4) == 0);
260 assert_se(usec_sub_signed(4, 5) == 0);
261 assert_se(usec_sub_signed(USEC_INFINITY-3, -3) == USEC_INFINITY);
262 assert_se(usec_sub_signed(USEC_INFINITY-3, -3) == USEC_INFINITY);
263 assert_se(usec_sub_signed(USEC_INFINITY-3, -4) == USEC_INFINITY);
264 assert_se(usec_sub_signed(USEC_INFINITY-3, -5) == USEC_INFINITY);
265 assert_se(usec_sub_signed(USEC_INFINITY, 5) == USEC_INFINITY);
266 }
267
268 static void test_format_timestamp(void) {
269 unsigned i;
270
271 log_info("/* %s */", __func__);
272
273 for (i = 0; i < 100; i++) {
274 char buf[MAX(FORMAT_TIMESTAMP_MAX, FORMAT_TIMESPAN_MAX)];
275 usec_t x, y;
276
277 random_bytes(&x, sizeof(x));
278 x = x % (2147483600 * USEC_PER_SEC) + 1;
279
280 assert_se(format_timestamp(buf, sizeof(buf), x));
281 log_info("%s", buf);
282 assert_se(parse_timestamp(buf, &y) >= 0);
283 assert_se(x / USEC_PER_SEC == y / USEC_PER_SEC);
284
285 assert_se(format_timestamp_utc(buf, sizeof(buf), x));
286 log_info("%s", buf);
287 assert_se(parse_timestamp(buf, &y) >= 0);
288 assert_se(x / USEC_PER_SEC == y / USEC_PER_SEC);
289
290 assert_se(format_timestamp_us(buf, sizeof(buf), x));
291 log_info("%s", buf);
292 assert_se(parse_timestamp(buf, &y) >= 0);
293 assert_se(x == y);
294
295 assert_se(format_timestamp_us_utc(buf, sizeof(buf), x));
296 log_info("%s", buf);
297 assert_se(parse_timestamp(buf, &y) >= 0);
298 assert_se(x == y);
299
300 assert_se(format_timestamp_relative(buf, sizeof(buf), x));
301 log_info("%s", buf);
302 assert_se(parse_timestamp(buf, &y) >= 0);
303
304 /* The two calls above will run with a slightly different local time. Make sure we are in the same
305 * range however, but give enough leeway that this is unlikely to explode. And of course,
306 * format_timestamp_relative() scales the accuracy with the distance from the current time up to one
307 * month, cover for that too. */
308 assert_se(y > x ? y - x : x - y <= USEC_PER_MONTH + USEC_PER_DAY);
309 }
310 }
311
312 static void test_format_timestamp_utc_one(usec_t val, const char *result) {
313 char buf[FORMAT_TIMESTAMP_MAX];
314 const char *t;
315
316 t = format_timestamp_utc(buf, sizeof(buf), val);
317 assert_se(streq_ptr(t, result));
318 }
319
320 static void test_format_timestamp_utc(void) {
321 log_info("/* %s */", __func__);
322
323 test_format_timestamp_utc_one(0, NULL);
324 test_format_timestamp_utc_one(1, "Thu 1970-01-01 00:00:00 UTC");
325 test_format_timestamp_utc_one(USEC_PER_SEC, "Thu 1970-01-01 00:00:01 UTC");
326
327 #if SIZEOF_TIME_T == 8
328 test_format_timestamp_utc_one(USEC_TIMESTAMP_FORMATTABLE_MAX, "Thu 9999-12-30 23:59:59 UTC");
329 test_format_timestamp_utc_one(USEC_TIMESTAMP_FORMATTABLE_MAX + 1, "--- XXXX-XX-XX XX:XX:XX");
330 #elif SIZEOF_TIME_T == 4
331 test_format_timestamp_utc_one(USEC_TIMESTAMP_FORMATTABLE_MAX, "Tue 2038-01-19 03:14:07 UTC");
332 test_format_timestamp_utc_one(USEC_TIMESTAMP_FORMATTABLE_MAX + 1, "--- XXXX-XX-XX XX:XX:XX");
333 #endif
334
335 test_format_timestamp_utc_one(USEC_INFINITY, NULL);
336 }
337
338 static void test_dual_timestamp_deserialize(void) {
339 int r;
340 dual_timestamp t;
341
342 log_info("/* %s */", __func__);
343
344 r = dual_timestamp_deserialize("1234 5678", &t);
345 assert_se(r == 0);
346 assert_se(t.realtime == 1234);
347 assert_se(t.monotonic == 5678);
348
349 r = dual_timestamp_deserialize("1234x 5678", &t);
350 assert_se(r == -EINVAL);
351
352 r = dual_timestamp_deserialize("1234 5678y", &t);
353 assert_se(r == -EINVAL);
354
355 r = dual_timestamp_deserialize("-1234 5678", &t);
356 assert_se(r == -EINVAL);
357
358 r = dual_timestamp_deserialize("1234 -5678", &t);
359 assert_se(r == -EINVAL);
360
361 /* Check that output wasn't modified. */
362 assert_se(t.realtime == 1234);
363 assert_se(t.monotonic == 5678);
364
365 r = dual_timestamp_deserialize("+123 567", &t);
366 assert_se(r == 0);
367 assert_se(t.realtime == 123);
368 assert_se(t.monotonic == 567);
369
370 /* Check that we get "infinity" on overflow. */
371 r = dual_timestamp_deserialize("18446744073709551617 0", &t);
372 assert_se(r == 0);
373 assert_se(t.realtime == USEC_INFINITY);
374 assert_se(t.monotonic == 0);
375 }
376
377 static void assert_similar(usec_t a, usec_t b) {
378 usec_t d;
379
380 if (a > b)
381 d = a - b;
382 else
383 d = b - a;
384
385 assert(d < 10*USEC_PER_SEC);
386 }
387
388 static void test_usec_shift_clock(void) {
389 usec_t rt, mn, bt;
390
391 log_info("/* %s */", __func__);
392
393 rt = now(CLOCK_REALTIME);
394 mn = now(CLOCK_MONOTONIC);
395 bt = now(clock_boottime_or_monotonic());
396
397 assert_se(usec_shift_clock(USEC_INFINITY, CLOCK_REALTIME, CLOCK_MONOTONIC) == USEC_INFINITY);
398
399 assert_similar(usec_shift_clock(rt + USEC_PER_HOUR, CLOCK_REALTIME, CLOCK_MONOTONIC), mn + USEC_PER_HOUR);
400 assert_similar(usec_shift_clock(rt + 2*USEC_PER_HOUR, CLOCK_REALTIME, clock_boottime_or_monotonic()), bt + 2*USEC_PER_HOUR);
401 assert_se(usec_shift_clock(rt + 3*USEC_PER_HOUR, CLOCK_REALTIME, CLOCK_REALTIME_ALARM) == rt + 3*USEC_PER_HOUR);
402
403 assert_similar(usec_shift_clock(mn + 4*USEC_PER_HOUR, CLOCK_MONOTONIC, CLOCK_REALTIME_ALARM), rt + 4*USEC_PER_HOUR);
404 assert_similar(usec_shift_clock(mn + 5*USEC_PER_HOUR, CLOCK_MONOTONIC, clock_boottime_or_monotonic()), bt + 5*USEC_PER_HOUR);
405 assert_se(usec_shift_clock(mn + 6*USEC_PER_HOUR, CLOCK_MONOTONIC, CLOCK_MONOTONIC) == mn + 6*USEC_PER_HOUR);
406
407 assert_similar(usec_shift_clock(bt + 7*USEC_PER_HOUR, clock_boottime_or_monotonic(), CLOCK_MONOTONIC), mn + 7*USEC_PER_HOUR);
408 assert_similar(usec_shift_clock(bt + 8*USEC_PER_HOUR, clock_boottime_or_monotonic(), CLOCK_REALTIME_ALARM), rt + 8*USEC_PER_HOUR);
409 assert_se(usec_shift_clock(bt + 9*USEC_PER_HOUR, clock_boottime_or_monotonic(), clock_boottime_or_monotonic()) == bt + 9*USEC_PER_HOUR);
410
411 if (mn > USEC_PER_MINUTE) {
412 assert_similar(usec_shift_clock(rt - 30 * USEC_PER_SEC, CLOCK_REALTIME_ALARM, CLOCK_MONOTONIC), mn - 30 * USEC_PER_SEC);
413 assert_similar(usec_shift_clock(rt - 50 * USEC_PER_SEC, CLOCK_REALTIME, clock_boottime_or_monotonic()), bt - 50 * USEC_PER_SEC);
414 }
415 }
416
417 static void test_in_utc_timezone(void) {
418 log_info("/* %s */", __func__);
419
420 assert_se(setenv("TZ", ":UTC", 1) >= 0);
421 assert_se(in_utc_timezone());
422 assert_se(streq(tzname[0], "UTC"));
423 assert_se(streq(tzname[1], "UTC"));
424 assert_se(timezone == 0);
425 assert_se(daylight == 0);
426
427 assert_se(setenv("TZ", "Europe/Berlin", 1) >= 0);
428 assert_se(!in_utc_timezone());
429 assert_se(streq(tzname[0], "CET"));
430 assert_se(streq(tzname[1], "CEST"));
431
432 assert_se(unsetenv("TZ") >= 0);
433 }
434
435 int main(int argc, char *argv[]) {
436 uintmax_t x;
437
438 log_info("realtime=" USEC_FMT "\n"
439 "monotonic=" USEC_FMT "\n"
440 "boottime=" USEC_FMT "\n",
441 now(CLOCK_REALTIME),
442 now(CLOCK_MONOTONIC),
443 now(clock_boottime_or_monotonic()));
444
445 test_parse_sec();
446 test_parse_sec_fix_0();
447 test_parse_time();
448 test_parse_nsec();
449 test_format_timespan(1);
450 test_format_timespan(USEC_PER_MSEC);
451 test_format_timespan(USEC_PER_SEC);
452 test_timezone_is_valid();
453 test_get_timezones();
454 test_usec_add();
455 test_usec_sub_signed();
456 test_usec_sub_unsigned();
457 test_format_timestamp();
458 test_format_timestamp_utc();
459 test_dual_timestamp_deserialize();
460 test_usec_shift_clock();
461 test_in_utc_timezone();
462
463 /* Ensure time_t is signed */
464 assert_cc((time_t) -1 < (time_t) 1);
465
466 /* Ensure TIME_T_MAX works correctly */
467 x = (uintmax_t) TIME_T_MAX;
468 x++;
469 assert((time_t) x < 0);
470
471 return 0;
472 }