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 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);
36 assert_se(parse_sec("23µs", &u
) >= 0);
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
);
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);
52 static void test_parse_sec_fix_0(void) {
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
);
65 static void test_parse_time(void) {
68 assert_se(parse_time("5", &u
, 1) >= 0);
71 assert_se(parse_time("5", &u
, USEC_PER_MSEC
) >= 0);
72 assert_se(u
== 5 * USEC_PER_MSEC
);
74 assert_se(parse_time("5", &u
, USEC_PER_SEC
) >= 0);
75 assert_se(u
== 5 * USEC_PER_SEC
);
77 assert_se(parse_time("5s", &u
, 1) >= 0);
78 assert_se(u
== 5 * USEC_PER_SEC
);
80 assert_se(parse_time("5s", &u
, USEC_PER_SEC
) >= 0);
81 assert_se(u
== 5 * USEC_PER_SEC
);
83 assert_se(parse_time("5s", &u
, USEC_PER_MSEC
) >= 0);
84 assert_se(u
== 5 * USEC_PER_SEC
);
87 static void test_parse_nsec(void) {
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);
106 assert_se(parse_nsec(".7", &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
);
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);
122 static void test_format_timespan_one(usec_t x
, usec_t accuracy
) {
124 char l
[FORMAT_TIMESPAN_MAX
];
127 log_info(USEC_FMT
" (at accuracy "USEC_FMT
")", x
, accuracy
);
129 r
= format_timespan(l
, sizeof(l
), x
, accuracy
);
132 log_info(" = <%s>", l
);
134 assert_se(parse_sec(l
, &y
) >= 0);
136 log_info(" = "USEC_FMT
, y
);
141 assert_se(x
/ accuracy
== y
/ accuracy
);
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
);
170 static void test_timezone_is_valid(void) {
171 assert_se(timezone_is_valid("Europe/Berlin"));
172 assert_se(timezone_is_valid("Australia/Sydney"));
173 assert_se(!timezone_is_valid("Europe/Do not exist"));
176 static void test_get_timezones(void) {
177 _cleanup_strv_free_
char **zones
= NULL
;
181 r
= get_timezones(&zones
);
184 STRV_FOREACH(zone
, zones
)
185 assert_se(timezone_is_valid(*zone
));
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
);
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
);
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
);
232 static void test_format_timestamp(void) {
235 for (i
= 0; i
< 100; i
++) {
236 char buf
[MAX(FORMAT_TIMESTAMP_MAX
, FORMAT_TIMESPAN_MAX
)];
239 random_bytes(&x
, sizeof(x
));
240 x
= x
% (2147483600 * USEC_PER_SEC
) + 1;
242 assert_se(format_timestamp(buf
, sizeof(buf
), x
));
244 assert_se(parse_timestamp(buf
, &y
) >= 0);
245 assert_se(x
/ USEC_PER_SEC
== y
/ USEC_PER_SEC
);
247 assert_se(format_timestamp_utc(buf
, sizeof(buf
), x
));
249 assert_se(parse_timestamp(buf
, &y
) >= 0);
250 assert_se(x
/ USEC_PER_SEC
== y
/ USEC_PER_SEC
);
252 assert_se(format_timestamp_us(buf
, sizeof(buf
), x
));
254 assert_se(parse_timestamp(buf
, &y
) >= 0);
257 assert_se(format_timestamp_us_utc(buf
, sizeof(buf
), x
));
259 assert_se(parse_timestamp(buf
, &y
) >= 0);
262 assert_se(format_timestamp_relative(buf
, sizeof(buf
), x
));
264 assert_se(parse_timestamp(buf
, &y
) >= 0);
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
);
274 static void test_format_timestamp_utc_one(usec_t t
, const char *result
) {
275 char buf
[FORMAT_TIMESTAMP_MAX
];
277 assert_se(!format_timestamp_utc(buf
, sizeof(buf
), t
) == !result
);
280 assert_se(streq(result
, buf
));
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");
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 #elif SIZEOF_TIME_T == 4
291 test_format_timestamp_utc_one(USEC_TIMESTAMP_FORMATTABLE_MAX
, "Tue 2038-01-19 03:14:07 UTC");
294 test_format_timestamp_utc_one(USEC_TIMESTAMP_FORMATTABLE_MAX
+1, NULL
);
295 test_format_timestamp_utc_one(USEC_INFINITY
, NULL
);
298 static void test_dual_timestamp_deserialize(void) {
302 r
= dual_timestamp_deserialize("1234 5678", &t
);
304 assert_se(t
.realtime
== 1234);
305 assert_se(t
.monotonic
== 5678);
307 r
= dual_timestamp_deserialize("1234x 5678", &t
);
308 assert_se(r
== -EINVAL
);
310 r
= dual_timestamp_deserialize("1234 5678y", &t
);
311 assert_se(r
== -EINVAL
);
313 r
= dual_timestamp_deserialize("-1234 5678", &t
);
314 assert_se(r
== -EINVAL
);
316 r
= dual_timestamp_deserialize("1234 -5678", &t
);
317 assert_se(r
== -EINVAL
);
319 /* Check that output wasn't modified. */
320 assert_se(t
.realtime
== 1234);
321 assert_se(t
.monotonic
== 5678);
323 r
= dual_timestamp_deserialize("+123 567", &t
);
325 assert_se(t
.realtime
== 123);
326 assert_se(t
.monotonic
== 567);
328 /* Check that we get "infinity" on overflow. */
329 r
= dual_timestamp_deserialize("18446744073709551617 0", &t
);
331 assert_se(t
.realtime
== USEC_INFINITY
);
332 assert_se(t
.monotonic
== 0);
335 static void assert_similar(usec_t a
, usec_t b
) {
343 assert(d
< 10*USEC_PER_SEC
);
346 static void test_usec_shift_clock(void) {
349 rt
= now(CLOCK_REALTIME
);
350 mn
= now(CLOCK_MONOTONIC
);
351 bt
= now(clock_boottime_or_monotonic());
353 assert_se(usec_shift_clock(USEC_INFINITY
, CLOCK_REALTIME
, CLOCK_MONOTONIC
) == USEC_INFINITY
);
355 assert_similar(usec_shift_clock(rt
+ USEC_PER_HOUR
, CLOCK_REALTIME
, CLOCK_MONOTONIC
), mn
+ USEC_PER_HOUR
);
356 assert_similar(usec_shift_clock(rt
+ 2*USEC_PER_HOUR
, CLOCK_REALTIME
, clock_boottime_or_monotonic()), bt
+ 2*USEC_PER_HOUR
);
357 assert_se(usec_shift_clock(rt
+ 3*USEC_PER_HOUR
, CLOCK_REALTIME
, CLOCK_REALTIME_ALARM
) == rt
+ 3*USEC_PER_HOUR
);
359 assert_similar(usec_shift_clock(mn
+ 4*USEC_PER_HOUR
, CLOCK_MONOTONIC
, CLOCK_REALTIME_ALARM
), rt
+ 4*USEC_PER_HOUR
);
360 assert_similar(usec_shift_clock(mn
+ 5*USEC_PER_HOUR
, CLOCK_MONOTONIC
, clock_boottime_or_monotonic()), bt
+ 5*USEC_PER_HOUR
);
361 assert_se(usec_shift_clock(mn
+ 6*USEC_PER_HOUR
, CLOCK_MONOTONIC
, CLOCK_MONOTONIC
) == mn
+ 6*USEC_PER_HOUR
);
363 assert_similar(usec_shift_clock(bt
+ 7*USEC_PER_HOUR
, clock_boottime_or_monotonic(), CLOCK_MONOTONIC
), mn
+ 7*USEC_PER_HOUR
);
364 assert_similar(usec_shift_clock(bt
+ 8*USEC_PER_HOUR
, clock_boottime_or_monotonic(), CLOCK_REALTIME_ALARM
), rt
+ 8*USEC_PER_HOUR
);
365 assert_se(usec_shift_clock(bt
+ 9*USEC_PER_HOUR
, clock_boottime_or_monotonic(), clock_boottime_or_monotonic()) == bt
+ 9*USEC_PER_HOUR
);
367 if (mn
> USEC_PER_MINUTE
) {
368 assert_similar(usec_shift_clock(rt
- 30 * USEC_PER_SEC
, CLOCK_REALTIME_ALARM
, CLOCK_MONOTONIC
), mn
- 30 * USEC_PER_SEC
);
369 assert_similar(usec_shift_clock(rt
- 50 * USEC_PER_SEC
, CLOCK_REALTIME
, clock_boottime_or_monotonic()), bt
- 50 * USEC_PER_SEC
);
373 static void test_in_utc_timezone(void) {
374 assert_se(setenv("TZ", ":UTC", 1) >= 0);
375 assert_se(in_utc_timezone());
376 assert_se(streq(tzname
[0], "UTC"));
377 assert_se(streq(tzname
[1], "UTC"));
378 assert_se(timezone
== 0);
379 assert_se(daylight
== 0);
381 assert_se(setenv("TZ", "Europe/Berlin", 1) >= 0);
382 assert_se(!in_utc_timezone());
383 assert_se(streq(tzname
[0], "CET"));
384 assert_se(streq(tzname
[1], "CEST"));
386 assert_se(unsetenv("TZ") >= 0);
389 int main(int argc
, char *argv
[]) {
392 log_info("realtime=" USEC_FMT
"\n"
393 "monotonic=" USEC_FMT
"\n"
394 "boottime=" USEC_FMT
"\n",
396 now(CLOCK_MONOTONIC
),
397 now(clock_boottime_or_monotonic()));
400 test_parse_sec_fix_0();
403 test_format_timespan(1);
404 test_format_timespan(USEC_PER_MSEC
);
405 test_format_timespan(USEC_PER_SEC
);
406 test_timezone_is_valid();
407 test_get_timezones();
409 test_usec_sub_signed();
410 test_usec_sub_unsigned();
411 test_format_timestamp();
412 test_format_timestamp_utc();
413 test_dual_timestamp_deserialize();
414 test_usec_shift_clock();
415 test_in_utc_timezone();
417 /* Ensure time_t is signed */
418 assert_cc((time_t) -1 < (time_t) 1);
420 /* Ensure TIME_T_MAX works correctly */
421 x
= (uintmax_t) TIME_T_MAX
;
423 assert((time_t) x
< 0);