2 This file is part of systemd.
4 Copyright 2013 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 #include "random-util.h"
21 #include "string-util.h"
23 #include "time-util.h"
25 static void test_parse_sec(void) {
28 assert_se(parse_sec("5s", &u
) >= 0);
29 assert_se(u
== 5 * USEC_PER_SEC
);
30 assert_se(parse_sec("5s500ms", &u
) >= 0);
31 assert_se(u
== 5 * USEC_PER_SEC
+ 500 * USEC_PER_MSEC
);
32 assert_se(parse_sec(" 5s 500ms ", &u
) >= 0);
33 assert_se(u
== 5 * USEC_PER_SEC
+ 500 * USEC_PER_MSEC
);
34 assert_se(parse_sec(" 5.5s ", &u
) >= 0);
35 assert_se(u
== 5 * USEC_PER_SEC
+ 500 * USEC_PER_MSEC
);
36 assert_se(parse_sec(" 5.5s 0.5ms ", &u
) >= 0);
37 assert_se(u
== 5 * USEC_PER_SEC
+ 500 * USEC_PER_MSEC
+ 500);
38 assert_se(parse_sec(" .22s ", &u
) >= 0);
39 assert_se(u
== 220 * USEC_PER_MSEC
);
40 assert_se(parse_sec(" .50y ", &u
) >= 0);
41 assert_se(u
== USEC_PER_YEAR
/ 2);
42 assert_se(parse_sec("2.5", &u
) >= 0);
43 assert_se(u
== 2500 * USEC_PER_MSEC
);
44 assert_se(parse_sec(".7", &u
) >= 0);
45 assert_se(u
== 700 * USEC_PER_MSEC
);
46 assert_se(parse_sec("23us", &u
) >= 0);
48 assert_se(parse_sec("23µs", &u
) >= 0);
50 assert_se(parse_sec("infinity", &u
) >= 0);
51 assert_se(u
== USEC_INFINITY
);
52 assert_se(parse_sec(" infinity ", &u
) >= 0);
53 assert_se(u
== USEC_INFINITY
);
55 assert_se(parse_sec(" xyz ", &u
) < 0);
56 assert_se(parse_sec("", &u
) < 0);
57 assert_se(parse_sec(" . ", &u
) < 0);
58 assert_se(parse_sec(" 5. ", &u
) < 0);
59 assert_se(parse_sec(".s ", &u
) < 0);
60 assert_se(parse_sec(" infinity .7", &u
) < 0);
61 assert_se(parse_sec(".3 infinity", &u
) < 0);
64 static void test_parse_sec_fix_0(void) {
67 assert_se(parse_sec_fix_0("5s", &u
) >= 0);
68 assert_se(u
== 5 * USEC_PER_SEC
);
69 assert_se(parse_sec_fix_0("0s", &u
) >= 0);
70 assert_se(u
== 0 * USEC_PER_SEC
);
71 assert_se(parse_sec_fix_0("0", &u
) >= 0);
72 assert_se(u
== USEC_INFINITY
);
73 assert_se(parse_sec_fix_0(" 0", &u
) >= 0);
74 assert_se(u
== USEC_INFINITY
);
77 static void test_parse_time(void) {
80 assert_se(parse_time("5", &u
, 1) >= 0);
83 assert_se(parse_time("5", &u
, USEC_PER_MSEC
) >= 0);
84 assert_se(u
== 5 * USEC_PER_MSEC
);
86 assert_se(parse_time("5", &u
, USEC_PER_SEC
) >= 0);
87 assert_se(u
== 5 * USEC_PER_SEC
);
89 assert_se(parse_time("5s", &u
, 1) >= 0);
90 assert_se(u
== 5 * USEC_PER_SEC
);
92 assert_se(parse_time("5s", &u
, USEC_PER_SEC
) >= 0);
93 assert_se(u
== 5 * USEC_PER_SEC
);
95 assert_se(parse_time("5s", &u
, USEC_PER_MSEC
) >= 0);
96 assert_se(u
== 5 * USEC_PER_SEC
);
99 static void test_parse_nsec(void) {
102 assert_se(parse_nsec("5s", &u
) >= 0);
103 assert_se(u
== 5 * NSEC_PER_SEC
);
104 assert_se(parse_nsec("5s500ms", &u
) >= 0);
105 assert_se(u
== 5 * NSEC_PER_SEC
+ 500 * NSEC_PER_MSEC
);
106 assert_se(parse_nsec(" 5s 500ms ", &u
) >= 0);
107 assert_se(u
== 5 * NSEC_PER_SEC
+ 500 * NSEC_PER_MSEC
);
108 assert_se(parse_nsec(" 5.5s ", &u
) >= 0);
109 assert_se(u
== 5 * NSEC_PER_SEC
+ 500 * NSEC_PER_MSEC
);
110 assert_se(parse_nsec(" 5.5s 0.5ms ", &u
) >= 0);
111 assert_se(u
== 5 * NSEC_PER_SEC
+ 500 * NSEC_PER_MSEC
+ 500 * NSEC_PER_USEC
);
112 assert_se(parse_nsec(" .22s ", &u
) >= 0);
113 assert_se(u
== 220 * NSEC_PER_MSEC
);
114 assert_se(parse_nsec(" .50y ", &u
) >= 0);
115 assert_se(u
== NSEC_PER_YEAR
/ 2);
116 assert_se(parse_nsec("2.5", &u
) >= 0);
118 assert_se(parse_nsec(".7", &u
) >= 0);
120 assert_se(parse_nsec("infinity", &u
) >= 0);
121 assert_se(u
== NSEC_INFINITY
);
122 assert_se(parse_nsec(" infinity ", &u
) >= 0);
123 assert_se(u
== NSEC_INFINITY
);
125 assert_se(parse_nsec(" xyz ", &u
) < 0);
126 assert_se(parse_nsec("", &u
) < 0);
127 assert_se(parse_nsec(" . ", &u
) < 0);
128 assert_se(parse_nsec(" 5. ", &u
) < 0);
129 assert_se(parse_nsec(".s ", &u
) < 0);
130 assert_se(parse_nsec(" infinity .7", &u
) < 0);
131 assert_se(parse_nsec(".3 infinity", &u
) < 0);
134 static void test_format_timespan_one(usec_t x
, usec_t accuracy
) {
136 char l
[FORMAT_TIMESPAN_MAX
];
139 log_info(USEC_FMT
" (at accuracy "USEC_FMT
")", x
, accuracy
);
141 r
= format_timespan(l
, sizeof(l
), x
, accuracy
);
144 log_info(" = <%s>", l
);
146 assert_se(parse_sec(l
, &y
) >= 0);
148 log_info(" = "USEC_FMT
, y
);
153 assert_se(x
/ accuracy
== y
/ accuracy
);
156 static void test_format_timespan(usec_t accuracy
) {
157 test_format_timespan_one(0, accuracy
);
158 test_format_timespan_one(1, accuracy
);
159 test_format_timespan_one(1*USEC_PER_SEC
, accuracy
);
160 test_format_timespan_one(999*USEC_PER_MSEC
, accuracy
);
161 test_format_timespan_one(1234567, accuracy
);
162 test_format_timespan_one(12, accuracy
);
163 test_format_timespan_one(123, accuracy
);
164 test_format_timespan_one(1234, accuracy
);
165 test_format_timespan_one(12345, accuracy
);
166 test_format_timespan_one(123456, accuracy
);
167 test_format_timespan_one(1234567, accuracy
);
168 test_format_timespan_one(12345678, accuracy
);
169 test_format_timespan_one(1200000, accuracy
);
170 test_format_timespan_one(1230000, accuracy
);
171 test_format_timespan_one(1230000, accuracy
);
172 test_format_timespan_one(1234000, accuracy
);
173 test_format_timespan_one(1234500, accuracy
);
174 test_format_timespan_one(1234560, accuracy
);
175 test_format_timespan_one(1234567, accuracy
);
176 test_format_timespan_one(986087, accuracy
);
177 test_format_timespan_one(500 * USEC_PER_MSEC
, accuracy
);
178 test_format_timespan_one(9*USEC_PER_YEAR
/5 - 23, accuracy
);
179 test_format_timespan_one(USEC_INFINITY
, accuracy
);
182 static void test_timezone_is_valid(void) {
183 assert_se(timezone_is_valid("Europe/Berlin"));
184 assert_se(timezone_is_valid("Australia/Sydney"));
185 assert_se(!timezone_is_valid("Europe/Do not exist"));
188 static void test_get_timezones(void) {
189 _cleanup_strv_free_
char **zones
= NULL
;
193 r
= get_timezones(&zones
);
196 STRV_FOREACH(zone
, zones
)
197 assert_se(timezone_is_valid(*zone
));
200 static void test_usec_add(void) {
201 assert_se(usec_add(0, 0) == 0);
202 assert_se(usec_add(1, 4) == 5);
203 assert_se(usec_add(USEC_INFINITY
, 5) == USEC_INFINITY
);
204 assert_se(usec_add(5, USEC_INFINITY
) == USEC_INFINITY
);
205 assert_se(usec_add(USEC_INFINITY
-5, 2) == USEC_INFINITY
-3);
206 assert_se(usec_add(USEC_INFINITY
-2, 2) == USEC_INFINITY
);
207 assert_se(usec_add(USEC_INFINITY
-1, 2) == USEC_INFINITY
);
208 assert_se(usec_add(USEC_INFINITY
, 2) == USEC_INFINITY
);
211 static void test_usec_sub_unsigned(void) {
212 assert_se(usec_sub_unsigned(0, 0) == 0);
213 assert_se(usec_sub_unsigned(0, 2) == 0);
214 assert_se(usec_sub_unsigned(0, USEC_INFINITY
) == 0);
215 assert_se(usec_sub_unsigned(1, 0) == 1);
216 assert_se(usec_sub_unsigned(1, 1) == 0);
217 assert_se(usec_sub_unsigned(1, 2) == 0);
218 assert_se(usec_sub_unsigned(1, 3) == 0);
219 assert_se(usec_sub_unsigned(1, USEC_INFINITY
) == 0);
220 assert_se(usec_sub_unsigned(USEC_INFINITY
-1, 0) == USEC_INFINITY
-1);
221 assert_se(usec_sub_unsigned(USEC_INFINITY
-1, 1) == USEC_INFINITY
-2);
222 assert_se(usec_sub_unsigned(USEC_INFINITY
-1, 2) == USEC_INFINITY
-3);
223 assert_se(usec_sub_unsigned(USEC_INFINITY
-1, USEC_INFINITY
-2) == 1);
224 assert_se(usec_sub_unsigned(USEC_INFINITY
-1, USEC_INFINITY
-1) == 0);
225 assert_se(usec_sub_unsigned(USEC_INFINITY
-1, USEC_INFINITY
) == 0);
226 assert_se(usec_sub_unsigned(USEC_INFINITY
, 0) == USEC_INFINITY
);
227 assert_se(usec_sub_unsigned(USEC_INFINITY
, 1) == USEC_INFINITY
);
228 assert_se(usec_sub_unsigned(USEC_INFINITY
, 2) == USEC_INFINITY
);
229 assert_se(usec_sub_unsigned(USEC_INFINITY
, USEC_INFINITY
) == USEC_INFINITY
);
232 static void test_usec_sub_signed(void) {
233 assert_se(usec_sub_signed(0, 0) == 0);
234 assert_se(usec_sub_signed(4, 1) == 3);
235 assert_se(usec_sub_signed(4, 4) == 0);
236 assert_se(usec_sub_signed(4, 5) == 0);
237 assert_se(usec_sub_signed(USEC_INFINITY
-3, -3) == USEC_INFINITY
);
238 assert_se(usec_sub_signed(USEC_INFINITY
-3, -3) == USEC_INFINITY
);
239 assert_se(usec_sub_signed(USEC_INFINITY
-3, -4) == USEC_INFINITY
);
240 assert_se(usec_sub_signed(USEC_INFINITY
-3, -5) == USEC_INFINITY
);
241 assert_se(usec_sub_signed(USEC_INFINITY
, 5) == USEC_INFINITY
);
244 static void test_format_timestamp(void) {
247 for (i
= 0; i
< 100; i
++) {
248 char buf
[MAX(FORMAT_TIMESTAMP_MAX
, FORMAT_TIMESPAN_MAX
)];
251 random_bytes(&x
, sizeof(x
));
252 x
= x
% (2147483600 * USEC_PER_SEC
) + 1;
254 assert_se(format_timestamp(buf
, sizeof(buf
), x
));
256 assert_se(parse_timestamp(buf
, &y
) >= 0);
257 assert_se(x
/ USEC_PER_SEC
== y
/ USEC_PER_SEC
);
259 assert_se(format_timestamp_utc(buf
, sizeof(buf
), x
));
261 assert_se(parse_timestamp(buf
, &y
) >= 0);
262 assert_se(x
/ USEC_PER_SEC
== y
/ USEC_PER_SEC
);
264 assert_se(format_timestamp_us(buf
, sizeof(buf
), x
));
266 assert_se(parse_timestamp(buf
, &y
) >= 0);
269 assert_se(format_timestamp_us_utc(buf
, sizeof(buf
), x
));
271 assert_se(parse_timestamp(buf
, &y
) >= 0);
274 assert_se(format_timestamp_relative(buf
, sizeof(buf
), x
));
276 assert_se(parse_timestamp(buf
, &y
) >= 0);
278 /* The two calls above will run with a slightly different local time. Make sure we are in the same
279 * range however, but give enough leeway that this is unlikely to explode. And of course,
280 * format_timestamp_relative() scales the accuracy with the distance from the current time up to one
281 * month, cover for that too. */
282 assert_se(y
> x
? y
- x
: x
- y
<= USEC_PER_MONTH
+ USEC_PER_DAY
);
286 static void test_format_timestamp_utc_one(usec_t t
, const char *result
) {
287 char buf
[FORMAT_TIMESTAMP_MAX
];
289 assert_se(!format_timestamp_utc(buf
, sizeof(buf
), t
) == !result
);
292 assert_se(streq(result
, buf
));
295 static void test_format_timestamp_utc(void) {
296 test_format_timestamp_utc_one(0, NULL
);
297 test_format_timestamp_utc_one(1, "Thu 1970-01-01 00:00:00 UTC");
298 test_format_timestamp_utc_one(USEC_PER_SEC
, "Thu 1970-01-01 00:00:01 UTC");
300 #if SIZEOF_TIME_T == 8
301 test_format_timestamp_utc_one(USEC_TIMESTAMP_FORMATTABLE_MAX
, "Thu 9999-12-30 23:59:59 UTC");
302 #elif SIZEOF_TIME_T == 4
303 test_format_timestamp_utc_one(USEC_TIMESTAMP_FORMATTABLE_MAX
, "Tue 2038-01-19 03:14:07 UTC");
306 test_format_timestamp_utc_one(USEC_TIMESTAMP_FORMATTABLE_MAX
+1, NULL
);
307 test_format_timestamp_utc_one(USEC_INFINITY
, NULL
);
310 static void test_dual_timestamp_deserialize(void) {
314 r
= dual_timestamp_deserialize("1234 5678", &t
);
316 assert_se(t
.realtime
== 1234);
317 assert_se(t
.monotonic
== 5678);
319 r
= dual_timestamp_deserialize("1234x 5678", &t
);
320 assert_se(r
== -EINVAL
);
322 r
= dual_timestamp_deserialize("1234 5678y", &t
);
323 assert_se(r
== -EINVAL
);
325 r
= dual_timestamp_deserialize("-1234 5678", &t
);
326 assert_se(r
== -EINVAL
);
328 r
= dual_timestamp_deserialize("1234 -5678", &t
);
329 assert_se(r
== -EINVAL
);
331 /* Check that output wasn't modified. */
332 assert_se(t
.realtime
== 1234);
333 assert_se(t
.monotonic
== 5678);
335 r
= dual_timestamp_deserialize("+123 567", &t
);
337 assert_se(t
.realtime
== 123);
338 assert_se(t
.monotonic
== 567);
340 /* Check that we get "infinity" on overflow. */
341 r
= dual_timestamp_deserialize("18446744073709551617 0", &t
);
343 assert_se(t
.realtime
== USEC_INFINITY
);
344 assert_se(t
.monotonic
== 0);
347 static void assert_similar(usec_t a
, usec_t b
) {
355 assert(d
< 10*USEC_PER_SEC
);
358 static void test_usec_shift_clock(void) {
361 rt
= now(CLOCK_REALTIME
);
362 mn
= now(CLOCK_MONOTONIC
);
363 bt
= now(clock_boottime_or_monotonic());
365 assert_se(usec_shift_clock(USEC_INFINITY
, CLOCK_REALTIME
, CLOCK_MONOTONIC
) == USEC_INFINITY
);
367 assert_similar(usec_shift_clock(rt
+ USEC_PER_HOUR
, CLOCK_REALTIME
, CLOCK_MONOTONIC
), mn
+ USEC_PER_HOUR
);
368 assert_similar(usec_shift_clock(rt
+ 2*USEC_PER_HOUR
, CLOCK_REALTIME
, clock_boottime_or_monotonic()), bt
+ 2*USEC_PER_HOUR
);
369 assert_se(usec_shift_clock(rt
+ 3*USEC_PER_HOUR
, CLOCK_REALTIME
, CLOCK_REALTIME_ALARM
) == rt
+ 3*USEC_PER_HOUR
);
371 assert_similar(usec_shift_clock(mn
+ 4*USEC_PER_HOUR
, CLOCK_MONOTONIC
, CLOCK_REALTIME_ALARM
), rt
+ 4*USEC_PER_HOUR
);
372 assert_similar(usec_shift_clock(mn
+ 5*USEC_PER_HOUR
, CLOCK_MONOTONIC
, clock_boottime_or_monotonic()), bt
+ 5*USEC_PER_HOUR
);
373 assert_se(usec_shift_clock(mn
+ 6*USEC_PER_HOUR
, CLOCK_MONOTONIC
, CLOCK_MONOTONIC
) == mn
+ 6*USEC_PER_HOUR
);
375 assert_similar(usec_shift_clock(bt
+ 7*USEC_PER_HOUR
, clock_boottime_or_monotonic(), CLOCK_MONOTONIC
), mn
+ 7*USEC_PER_HOUR
);
376 assert_similar(usec_shift_clock(bt
+ 8*USEC_PER_HOUR
, clock_boottime_or_monotonic(), CLOCK_REALTIME_ALARM
), rt
+ 8*USEC_PER_HOUR
);
377 assert_se(usec_shift_clock(bt
+ 9*USEC_PER_HOUR
, clock_boottime_or_monotonic(), clock_boottime_or_monotonic()) == bt
+ 9*USEC_PER_HOUR
);
379 if (mn
> USEC_PER_MINUTE
) {
380 assert_similar(usec_shift_clock(rt
- 30 * USEC_PER_SEC
, CLOCK_REALTIME_ALARM
, CLOCK_MONOTONIC
), mn
- 30 * USEC_PER_SEC
);
381 assert_similar(usec_shift_clock(rt
- 50 * USEC_PER_SEC
, CLOCK_REALTIME
, clock_boottime_or_monotonic()), bt
- 50 * USEC_PER_SEC
);
385 int main(int argc
, char *argv
[]) {
388 log_info("realtime=" USEC_FMT
"\n"
389 "monotonic=" USEC_FMT
"\n"
390 "boottime=" USEC_FMT
"\n",
392 now(CLOCK_MONOTONIC
),
393 now(clock_boottime_or_monotonic()));
396 test_parse_sec_fix_0();
399 test_format_timespan(1);
400 test_format_timespan(USEC_PER_MSEC
);
401 test_format_timespan(USEC_PER_SEC
);
402 test_timezone_is_valid();
403 test_get_timezones();
405 test_usec_sub_signed();
406 test_usec_sub_unsigned();
407 test_format_timestamp();
408 test_format_timestamp_utc();
409 test_dual_timestamp_deserialize();
410 test_usec_shift_clock();
412 /* Ensure time_t is signed */
413 assert_cc((time_t) -1 < (time_t) 1);
415 /* Ensure TIME_T_MAX works correctly */
416 x
= (uintmax_t) TIME_T_MAX
;
418 assert((time_t) x
< 0);