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