1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2013 Lennart Poettering
8 #include "random-util.h"
9 #include "string-util.h"
11 #include "time-util.h"
13 static void test_parse_sec(void) {
16 log_info("/* %s */", __func__
);
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);
38 assert_se(parse_sec("23µs", &u
) >= 0);
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
);
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);
54 static void test_parse_sec_fix_0(void) {
57 log_info("/* %s */", __func__
);
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
);
69 static void test_parse_time(void) {
72 log_info("/* %s */", __func__
);
74 assert_se(parse_time("5", &u
, 1) >= 0);
77 assert_se(parse_time("5", &u
, USEC_PER_MSEC
) >= 0);
78 assert_se(u
== 5 * USEC_PER_MSEC
);
80 assert_se(parse_time("5", &u
, USEC_PER_SEC
) >= 0);
81 assert_se(u
== 5 * USEC_PER_SEC
);
83 assert_se(parse_time("5s", &u
, 1) >= 0);
84 assert_se(u
== 5 * USEC_PER_SEC
);
86 assert_se(parse_time("5s", &u
, USEC_PER_SEC
) >= 0);
87 assert_se(u
== 5 * USEC_PER_SEC
);
89 assert_se(parse_time("5s", &u
, USEC_PER_MSEC
) >= 0);
90 assert_se(u
== 5 * USEC_PER_SEC
);
93 static void test_parse_nsec(void) {
96 log_info("/* %s */", __func__
);
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);
114 assert_se(parse_nsec(".7", &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
);
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);
130 static void test_format_timespan_one(usec_t x
, usec_t accuracy
) {
131 char l
[FORMAT_TIMESPAN_MAX
];
135 log_info(USEC_FMT
" (at accuracy "USEC_FMT
")", x
, accuracy
);
137 assert_se(t
= format_timespan(l
, sizeof l
, x
, accuracy
));
138 log_info(" = <%s>", t
);
140 assert_se(parse_sec(t
, &y
) >= 0);
141 log_info(" = "USEC_FMT
, y
);
146 assert_se(x
/ accuracy
== y
/ accuracy
);
149 static void test_format_timespan(usec_t accuracy
) {
150 log_info("/* %s accuracy="USEC_FMT
" */", __func__
, accuracy
);
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
);
177 static void test_timezone_is_valid(void) {
178 log_info("/* %s */", __func__
);
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
));
185 static void test_get_timezones(void) {
186 _cleanup_strv_free_
char **zones
= NULL
;
190 log_info("/* %s */", __func__
);
192 r
= get_timezones(&zones
);
195 STRV_FOREACH(zone
, zones
)
196 assert_se(timezone_is_valid(*zone
, LOG_ERR
));
199 static void test_usec_add(void) {
200 log_info("/* %s */", __func__
);
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
);
212 static void test_usec_sub_unsigned(void) {
213 log_info("/* %s */", __func__
);
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
);
235 static void test_usec_sub_signed(void) {
236 log_info("/* %s */", __func__
);
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
);
249 static void test_format_timestamp(void) {
252 log_info("/* %s */", __func__
);
254 for (i
= 0; i
< 100; i
++) {
255 char buf
[MAX(FORMAT_TIMESTAMP_MAX
, FORMAT_TIMESPAN_MAX
)];
258 random_bytes(&x
, sizeof(x
));
259 x
= x
% (2147483600 * USEC_PER_SEC
) + 1;
261 assert_se(format_timestamp(buf
, sizeof(buf
), x
));
263 assert_se(parse_timestamp(buf
, &y
) >= 0);
264 assert_se(x
/ USEC_PER_SEC
== y
/ USEC_PER_SEC
);
266 assert_se(format_timestamp_utc(buf
, sizeof(buf
), x
));
268 assert_se(parse_timestamp(buf
, &y
) >= 0);
269 assert_se(x
/ USEC_PER_SEC
== y
/ USEC_PER_SEC
);
271 assert_se(format_timestamp_us(buf
, sizeof(buf
), x
));
273 assert_se(parse_timestamp(buf
, &y
) >= 0);
276 assert_se(format_timestamp_us_utc(buf
, sizeof(buf
), x
));
278 assert_se(parse_timestamp(buf
, &y
) >= 0);
281 assert_se(format_timestamp_relative(buf
, sizeof(buf
), x
));
283 assert_se(parse_timestamp(buf
, &y
) >= 0);
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
);
293 static void test_format_timestamp_utc_one(usec_t val
, const char *result
) {
294 char buf
[FORMAT_TIMESTAMP_MAX
];
297 t
= format_timestamp_utc(buf
, sizeof(buf
), val
);
298 assert_se(streq_ptr(t
, result
));
301 static void test_format_timestamp_utc(void) {
302 log_info("/* %s */", __func__
);
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");
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");
316 test_format_timestamp_utc_one(USEC_INFINITY
, NULL
);
319 static void test_dual_timestamp_deserialize(void) {
323 log_info("/* %s */", __func__
);
325 r
= dual_timestamp_deserialize("1234 5678", &t
);
327 assert_se(t
.realtime
== 1234);
328 assert_se(t
.monotonic
== 5678);
330 r
= dual_timestamp_deserialize("1234x 5678", &t
);
331 assert_se(r
== -EINVAL
);
333 r
= dual_timestamp_deserialize("1234 5678y", &t
);
334 assert_se(r
== -EINVAL
);
336 r
= dual_timestamp_deserialize("-1234 5678", &t
);
337 assert_se(r
== -EINVAL
);
339 r
= dual_timestamp_deserialize("1234 -5678", &t
);
340 assert_se(r
== -EINVAL
);
342 /* Check that output wasn't modified. */
343 assert_se(t
.realtime
== 1234);
344 assert_se(t
.monotonic
== 5678);
346 r
= dual_timestamp_deserialize("+123 567", &t
);
348 assert_se(t
.realtime
== 123);
349 assert_se(t
.monotonic
== 567);
351 /* Check that we get "infinity" on overflow. */
352 r
= dual_timestamp_deserialize("18446744073709551617 0", &t
);
354 assert_se(t
.realtime
== USEC_INFINITY
);
355 assert_se(t
.monotonic
== 0);
358 static void assert_similar(usec_t a
, usec_t b
) {
366 assert(d
< 10*USEC_PER_SEC
);
369 static void test_usec_shift_clock(void) {
372 log_info("/* %s */", __func__
);
374 rt
= now(CLOCK_REALTIME
);
375 mn
= now(CLOCK_MONOTONIC
);
376 bt
= now(clock_boottime_or_monotonic());
378 assert_se(usec_shift_clock(USEC_INFINITY
, CLOCK_REALTIME
, CLOCK_MONOTONIC
) == USEC_INFINITY
);
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
);
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
);
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
);
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
);
398 static void test_in_utc_timezone(void) {
399 log_info("/* %s */", __func__
);
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);
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"));
413 assert_se(unsetenv("TZ") >= 0);
416 int main(int argc
, char *argv
[]) {
419 log_info("realtime=" USEC_FMT
"\n"
420 "monotonic=" USEC_FMT
"\n"
421 "boottime=" USEC_FMT
"\n",
423 now(CLOCK_MONOTONIC
),
424 now(clock_boottime_or_monotonic()));
427 test_parse_sec_fix_0();
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();
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();
444 /* Ensure time_t is signed */
445 assert_cc((time_t) -1 < (time_t) 1);
447 /* Ensure TIME_T_MAX works correctly */
448 x
= (uintmax_t) TIME_T_MAX
;
450 assert((time_t) x
< 0);