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