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