]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-time-util.c
sd-boot+bootctl: invert order of entries w/o sort-key
[thirdparty/systemd.git] / src / test / test-time-util.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "env-util.h"
4 #include "random-util.h"
5 #include "serialize.h"
6 #include "string-util.h"
7 #include "strv.h"
8 #include "tests.h"
9 #include "time-util.h"
10
11 TEST(parse_sec) {
12 usec_t u;
13
14 assert_se(parse_sec("5s", &u) >= 0);
15 assert_se(u == 5 * USEC_PER_SEC);
16 assert_se(parse_sec("5s500ms", &u) >= 0);
17 assert_se(u == 5 * USEC_PER_SEC + 500 * USEC_PER_MSEC);
18 assert_se(parse_sec(" 5s 500ms ", &u) >= 0);
19 assert_se(u == 5 * USEC_PER_SEC + 500 * USEC_PER_MSEC);
20 assert_se(parse_sec(" 5.5s ", &u) >= 0);
21 assert_se(u == 5 * USEC_PER_SEC + 500 * USEC_PER_MSEC);
22 assert_se(parse_sec(" 5.5s 0.5ms ", &u) >= 0);
23 assert_se(u == 5 * USEC_PER_SEC + 500 * USEC_PER_MSEC + 500);
24 assert_se(parse_sec(" .22s ", &u) >= 0);
25 assert_se(u == 220 * USEC_PER_MSEC);
26 assert_se(parse_sec(" .50y ", &u) >= 0);
27 assert_se(u == USEC_PER_YEAR / 2);
28 assert_se(parse_sec("2.5", &u) >= 0);
29 assert_se(u == 2500 * USEC_PER_MSEC);
30 assert_se(parse_sec(".7", &u) >= 0);
31 assert_se(u == 700 * USEC_PER_MSEC);
32 assert_se(parse_sec("23us", &u) >= 0);
33 assert_se(u == 23);
34 assert_se(parse_sec("23µs", &u) >= 0);
35 assert_se(u == 23);
36 assert_se(parse_sec("infinity", &u) >= 0);
37 assert_se(u == USEC_INFINITY);
38 assert_se(parse_sec(" infinity ", &u) >= 0);
39 assert_se(u == USEC_INFINITY);
40 assert_se(parse_sec("+3.1s", &u) >= 0);
41 assert_se(u == 3100 * USEC_PER_MSEC);
42 assert_se(parse_sec("3.1s.2", &u) >= 0);
43 assert_se(u == 3300 * USEC_PER_MSEC);
44 assert_se(parse_sec("3.1 .2", &u) >= 0);
45 assert_se(u == 3300 * USEC_PER_MSEC);
46 assert_se(parse_sec("3.1 sec .2 sec", &u) >= 0);
47 assert_se(u == 3300 * USEC_PER_MSEC);
48 assert_se(parse_sec("3.1 sec 1.2 sec", &u) >= 0);
49 assert_se(u == 4300 * USEC_PER_MSEC);
50
51 assert_se(parse_sec(" xyz ", &u) < 0);
52 assert_se(parse_sec("", &u) < 0);
53 assert_se(parse_sec(" . ", &u) < 0);
54 assert_se(parse_sec(" 5. ", &u) < 0);
55 assert_se(parse_sec(".s ", &u) < 0);
56 assert_se(parse_sec("-5s ", &u) < 0);
57 assert_se(parse_sec("-0.3s ", &u) < 0);
58 assert_se(parse_sec("-0.0s ", &u) < 0);
59 assert_se(parse_sec("-0.-0s ", &u) < 0);
60 assert_se(parse_sec("0.-0s ", &u) < 0);
61 assert_se(parse_sec("3.-0s ", &u) < 0);
62 assert_se(parse_sec(" infinity .7", &u) < 0);
63 assert_se(parse_sec(".3 infinity", &u) < 0);
64 assert_se(parse_sec("3.+1s", &u) < 0);
65 assert_se(parse_sec("3. 1s", &u) < 0);
66 assert_se(parse_sec("3.s", &u) < 0);
67 assert_se(parse_sec("12.34.56", &u) < 0);
68 assert_se(parse_sec("12..34", &u) < 0);
69 assert_se(parse_sec("..1234", &u) < 0);
70 assert_se(parse_sec("1234..", &u) < 0);
71 }
72
73 TEST(parse_sec_fix_0) {
74 usec_t u;
75
76 assert_se(parse_sec_fix_0("5s", &u) >= 0);
77 assert_se(u == 5 * USEC_PER_SEC);
78 assert_se(parse_sec_fix_0("0s", &u) >= 0);
79 assert_se(u == USEC_INFINITY);
80 assert_se(parse_sec_fix_0("0", &u) >= 0);
81 assert_se(u == USEC_INFINITY);
82 assert_se(parse_sec_fix_0(" 0", &u) >= 0);
83 assert_se(u == USEC_INFINITY);
84 }
85
86 TEST(parse_sec_def_infinity) {
87 usec_t u;
88
89 assert_se(parse_sec_def_infinity("5s", &u) >= 0);
90 assert_se(u == 5 * USEC_PER_SEC);
91 assert_se(parse_sec_def_infinity("", &u) >= 0);
92 assert_se(u == USEC_INFINITY);
93 assert_se(parse_sec_def_infinity(" ", &u) >= 0);
94 assert_se(u == USEC_INFINITY);
95 assert_se(parse_sec_def_infinity("0s", &u) >= 0);
96 assert_se(u == 0);
97 assert_se(parse_sec_def_infinity("0", &u) >= 0);
98 assert_se(u == 0);
99 assert_se(parse_sec_def_infinity(" 0", &u) >= 0);
100 assert_se(u == 0);
101 assert_se(parse_sec_def_infinity("-5s", &u) < 0);
102 }
103
104 TEST(parse_time) {
105 usec_t u;
106
107 assert_se(parse_time("5", &u, 1) >= 0);
108 assert_se(u == 5);
109
110 assert_se(parse_time("5", &u, USEC_PER_MSEC) >= 0);
111 assert_se(u == 5 * USEC_PER_MSEC);
112
113 assert_se(parse_time("5", &u, USEC_PER_SEC) >= 0);
114 assert_se(u == 5 * USEC_PER_SEC);
115
116 assert_se(parse_time("5s", &u, 1) >= 0);
117 assert_se(u == 5 * USEC_PER_SEC);
118
119 assert_se(parse_time("5s", &u, USEC_PER_SEC) >= 0);
120 assert_se(u == 5 * USEC_PER_SEC);
121
122 assert_se(parse_time("5s", &u, USEC_PER_MSEC) >= 0);
123 assert_se(u == 5 * USEC_PER_SEC);
124
125 assert_se(parse_time("11111111111111y", &u, 1) == -ERANGE);
126 assert_se(parse_time("1.1111111111111y", &u, 1) >= 0);
127 }
128
129 TEST(parse_nsec) {
130 nsec_t u;
131
132 assert_se(parse_nsec("5s", &u) >= 0);
133 assert_se(u == 5 * NSEC_PER_SEC);
134 assert_se(parse_nsec("5s500ms", &u) >= 0);
135 assert_se(u == 5 * NSEC_PER_SEC + 500 * NSEC_PER_MSEC);
136 assert_se(parse_nsec(" 5s 500ms ", &u) >= 0);
137 assert_se(u == 5 * NSEC_PER_SEC + 500 * NSEC_PER_MSEC);
138 assert_se(parse_nsec(" 5.5s ", &u) >= 0);
139 assert_se(u == 5 * NSEC_PER_SEC + 500 * NSEC_PER_MSEC);
140 assert_se(parse_nsec(" 5.5s 0.5ms ", &u) >= 0);
141 assert_se(u == 5 * NSEC_PER_SEC + 500 * NSEC_PER_MSEC + 500 * NSEC_PER_USEC);
142 assert_se(parse_nsec(" .22s ", &u) >= 0);
143 assert_se(u == 220 * NSEC_PER_MSEC);
144 assert_se(parse_nsec(" .50y ", &u) >= 0);
145 assert_se(u == NSEC_PER_YEAR / 2);
146 assert_se(parse_nsec("2.5", &u) >= 0);
147 assert_se(u == 2);
148 assert_se(parse_nsec(".7", &u) >= 0);
149 assert_se(u == 0);
150 assert_se(parse_nsec("infinity", &u) >= 0);
151 assert_se(u == NSEC_INFINITY);
152 assert_se(parse_nsec(" infinity ", &u) >= 0);
153 assert_se(u == NSEC_INFINITY);
154 assert_se(parse_nsec("+3.1s", &u) >= 0);
155 assert_se(u == 3100 * NSEC_PER_MSEC);
156 assert_se(parse_nsec("3.1s.2", &u) >= 0);
157 assert_se(u == 3100 * NSEC_PER_MSEC);
158 assert_se(parse_nsec("3.1 .2s", &u) >= 0);
159 assert_se(u == 200 * NSEC_PER_MSEC + 3);
160 assert_se(parse_nsec("3.1 sec .2 sec", &u) >= 0);
161 assert_se(u == 3300 * NSEC_PER_MSEC);
162 assert_se(parse_nsec("3.1 sec 1.2 sec", &u) >= 0);
163 assert_se(u == 4300 * NSEC_PER_MSEC);
164
165 assert_se(parse_nsec(" xyz ", &u) < 0);
166 assert_se(parse_nsec("", &u) < 0);
167 assert_se(parse_nsec(" . ", &u) < 0);
168 assert_se(parse_nsec(" 5. ", &u) < 0);
169 assert_se(parse_nsec(".s ", &u) < 0);
170 assert_se(parse_nsec(" infinity .7", &u) < 0);
171 assert_se(parse_nsec(".3 infinity", &u) < 0);
172 assert_se(parse_nsec("-5s ", &u) < 0);
173 assert_se(parse_nsec("-0.3s ", &u) < 0);
174 assert_se(parse_nsec("-0.0s ", &u) < 0);
175 assert_se(parse_nsec("-0.-0s ", &u) < 0);
176 assert_se(parse_nsec("0.-0s ", &u) < 0);
177 assert_se(parse_nsec("3.-0s ", &u) < 0);
178 assert_se(parse_nsec(" infinity .7", &u) < 0);
179 assert_se(parse_nsec(".3 infinity", &u) < 0);
180 assert_se(parse_nsec("3.+1s", &u) < 0);
181 assert_se(parse_nsec("3. 1s", &u) < 0);
182 assert_se(parse_nsec("3.s", &u) < 0);
183 assert_se(parse_nsec("12.34.56", &u) < 0);
184 assert_se(parse_nsec("12..34", &u) < 0);
185 assert_se(parse_nsec("..1234", &u) < 0);
186 assert_se(parse_nsec("1234..", &u) < 0);
187 assert_se(parse_nsec("1111111111111y", &u) == -ERANGE);
188 assert_se(parse_nsec("1.111111111111y", &u) >= 0);
189 }
190
191 static void test_format_timespan_one(usec_t x, usec_t accuracy) {
192 char l[FORMAT_TIMESPAN_MAX];
193 const char *t;
194 usec_t y;
195
196 log_debug(USEC_FMT" (at accuracy "USEC_FMT")", x, accuracy);
197
198 assert_se(t = format_timespan(l, sizeof l, x, accuracy));
199 log_debug(" = <%s>", t);
200
201 assert_se(parse_sec(t, &y) >= 0);
202 log_debug(" = "USEC_FMT, y);
203
204 if (accuracy <= 0)
205 accuracy = 1;
206
207 assert_se(x / accuracy == y / accuracy);
208 }
209
210 static void test_format_timespan_accuracy(usec_t accuracy) {
211 log_info("/* %s accuracy="USEC_FMT" */", __func__, accuracy);
212
213 test_format_timespan_one(0, accuracy);
214 test_format_timespan_one(1, accuracy);
215 test_format_timespan_one(1*USEC_PER_SEC, accuracy);
216 test_format_timespan_one(999*USEC_PER_MSEC, accuracy);
217 test_format_timespan_one(1234567, accuracy);
218 test_format_timespan_one(12, accuracy);
219 test_format_timespan_one(123, accuracy);
220 test_format_timespan_one(1234, accuracy);
221 test_format_timespan_one(12345, accuracy);
222 test_format_timespan_one(123456, accuracy);
223 test_format_timespan_one(1234567, accuracy);
224 test_format_timespan_one(12345678, accuracy);
225 test_format_timespan_one(1200000, accuracy);
226 test_format_timespan_one(1230000, accuracy);
227 test_format_timespan_one(1234000, accuracy);
228 test_format_timespan_one(1234500, accuracy);
229 test_format_timespan_one(1234560, accuracy);
230 test_format_timespan_one(1234567, accuracy);
231 test_format_timespan_one(986087, accuracy);
232 test_format_timespan_one(500 * USEC_PER_MSEC, accuracy);
233 test_format_timespan_one(9*USEC_PER_YEAR/5 - 23, accuracy);
234 test_format_timespan_one(USEC_INFINITY, accuracy);
235 }
236
237 TEST(format_timespan) {
238 test_format_timespan_accuracy(1);
239 test_format_timespan_accuracy(USEC_PER_MSEC);
240 test_format_timespan_accuracy(USEC_PER_SEC);
241 }
242
243 TEST(verify_timezone) {
244 assert_se(verify_timezone("Europe/Berlin", LOG_DEBUG) == 0);
245 assert_se(verify_timezone("Australia/Sydney", LOG_DEBUG) == 0);
246 assert_se(verify_timezone("Europe/Do not exist", LOG_DEBUG) == -EINVAL);
247 assert_se(verify_timezone("Europe/DoNotExist", LOG_DEBUG) == -ENOENT);
248 assert_se(verify_timezone("/DoNotExist", LOG_DEBUG) == -EINVAL);
249 assert_se(verify_timezone("DoNotExist/", LOG_DEBUG) == -EINVAL);
250 }
251
252 TEST(timezone_is_valid) {
253 assert_se(timezone_is_valid("Europe/Berlin", LOG_ERR));
254 assert_se(timezone_is_valid("Australia/Sydney", LOG_ERR));
255 assert_se(!timezone_is_valid("Europe/Do not exist", LOG_ERR));
256 }
257
258 TEST(get_timezones) {
259 _cleanup_strv_free_ char **zones = NULL;
260 int r;
261 char **zone;
262
263 r = get_timezones(&zones);
264 assert_se(r == 0);
265
266 STRV_FOREACH(zone, zones) {
267 r = verify_timezone(*zone, LOG_ERR);
268 log_debug_errno(r, "verify_timezone(\"%s\"): %m", *zone);
269 assert_se(r >= 0 || r == -ENOENT);
270 }
271 }
272
273 TEST(usec_add) {
274 assert_se(usec_add(0, 0) == 0);
275 assert_se(usec_add(1, 4) == 5);
276 assert_se(usec_add(USEC_INFINITY, 5) == USEC_INFINITY);
277 assert_se(usec_add(5, USEC_INFINITY) == USEC_INFINITY);
278 assert_se(usec_add(USEC_INFINITY-5, 2) == USEC_INFINITY-3);
279 assert_se(usec_add(USEC_INFINITY-2, 2) == USEC_INFINITY);
280 assert_se(usec_add(USEC_INFINITY-1, 2) == USEC_INFINITY);
281 assert_se(usec_add(USEC_INFINITY, 2) == USEC_INFINITY);
282 }
283
284 TEST(usec_sub_unsigned) {
285 assert_se(usec_sub_unsigned(0, 0) == 0);
286 assert_se(usec_sub_unsigned(0, 2) == 0);
287 assert_se(usec_sub_unsigned(0, USEC_INFINITY) == 0);
288 assert_se(usec_sub_unsigned(1, 0) == 1);
289 assert_se(usec_sub_unsigned(1, 1) == 0);
290 assert_se(usec_sub_unsigned(1, 2) == 0);
291 assert_se(usec_sub_unsigned(1, 3) == 0);
292 assert_se(usec_sub_unsigned(1, USEC_INFINITY) == 0);
293 assert_se(usec_sub_unsigned(USEC_INFINITY-1, 0) == USEC_INFINITY-1);
294 assert_se(usec_sub_unsigned(USEC_INFINITY-1, 1) == USEC_INFINITY-2);
295 assert_se(usec_sub_unsigned(USEC_INFINITY-1, 2) == USEC_INFINITY-3);
296 assert_se(usec_sub_unsigned(USEC_INFINITY-1, USEC_INFINITY-2) == 1);
297 assert_se(usec_sub_unsigned(USEC_INFINITY-1, USEC_INFINITY-1) == 0);
298 assert_se(usec_sub_unsigned(USEC_INFINITY-1, USEC_INFINITY) == 0);
299 assert_se(usec_sub_unsigned(USEC_INFINITY, 0) == USEC_INFINITY);
300 assert_se(usec_sub_unsigned(USEC_INFINITY, 1) == USEC_INFINITY);
301 assert_se(usec_sub_unsigned(USEC_INFINITY, 2) == USEC_INFINITY);
302 assert_se(usec_sub_unsigned(USEC_INFINITY, USEC_INFINITY) == USEC_INFINITY);
303 }
304
305 TEST(usec_sub_signed) {
306 assert_se(usec_sub_signed(0, 0) == 0);
307 assert_se(usec_sub_signed(4, 1) == 3);
308 assert_se(usec_sub_signed(4, 4) == 0);
309 assert_se(usec_sub_signed(4, 5) == 0);
310 assert_se(usec_sub_signed(USEC_INFINITY-3, -3) == USEC_INFINITY);
311 assert_se(usec_sub_signed(USEC_INFINITY-3, -4) == USEC_INFINITY);
312 assert_se(usec_sub_signed(USEC_INFINITY-3, -5) == USEC_INFINITY);
313 assert_se(usec_sub_signed(USEC_INFINITY, 5) == USEC_INFINITY);
314 }
315
316 TEST(format_timestamp) {
317 for (unsigned i = 0; i < 100; i++) {
318 char buf[MAX(FORMAT_TIMESTAMP_MAX, FORMAT_TIMESPAN_MAX)];
319 usec_t x, y;
320
321 x = random_u64_range(2147483600 * USEC_PER_SEC) + 1;
322
323 assert_se(format_timestamp(buf, sizeof(buf), x));
324 log_debug("%s", buf);
325 assert_se(parse_timestamp(buf, &y) >= 0);
326 assert_se(x / USEC_PER_SEC == y / USEC_PER_SEC);
327
328 assert_se(format_timestamp_style(buf, sizeof(buf), x, TIMESTAMP_UNIX));
329 log_debug("%s", buf);
330 assert_se(parse_timestamp(buf, &y) >= 0);
331 assert_se(x / USEC_PER_SEC == y / USEC_PER_SEC);
332
333 assert_se(format_timestamp_style(buf, sizeof(buf), x, TIMESTAMP_UTC));
334 log_debug("%s", buf);
335 assert_se(parse_timestamp(buf, &y) >= 0);
336 assert_se(x / USEC_PER_SEC == y / USEC_PER_SEC);
337
338 assert_se(format_timestamp_style(buf, sizeof(buf), x, TIMESTAMP_US));
339 log_debug("%s", buf);
340 assert_se(parse_timestamp(buf, &y) >= 0);
341 assert_se(x == y);
342
343 assert_se(format_timestamp_style(buf, sizeof(buf), x, TIMESTAMP_US_UTC));
344 log_debug("%s", buf);
345 assert_se(parse_timestamp(buf, &y) >= 0);
346 assert_se(x == y);
347
348 assert_se(format_timestamp_relative(buf, sizeof(buf), x));
349 log_debug("%s", buf);
350 assert_se(parse_timestamp(buf, &y) >= 0);
351
352 /* The two calls above will run with a slightly different local time. Make sure we are in the same
353 * range however, but give enough leeway that this is unlikely to explode. And of course,
354 * format_timestamp_relative() scales the accuracy with the distance from the current time up to one
355 * month, cover for that too. */
356 assert_se(y > x ? y - x : x - y <= USEC_PER_MONTH + USEC_PER_DAY);
357 }
358 }
359
360 TEST(FORMAT_TIMESTAMP) {
361 for (unsigned i = 0; i < 100; i++) {
362 _cleanup_free_ char *buf;
363 usec_t x, y;
364
365 x = random_u64_range(2147483600 * USEC_PER_SEC) + 1;
366
367 /* strbuf() is to test the macro in an argument to a function call. */
368 assert_se(buf = strdup(FORMAT_TIMESTAMP(x)));
369 log_debug("%s", buf);
370 assert_se(parse_timestamp(buf, &y) >= 0);
371 assert_se(x / USEC_PER_SEC == y / USEC_PER_SEC);
372
373 assert_se(streq(FORMAT_TIMESTAMP(x), buf));
374 }
375 }
376
377 TEST(format_timestamp_relative) {
378 char buf[MAX(FORMAT_TIMESTAMP_MAX, FORMAT_TIMESPAN_MAX)];
379 usec_t x;
380
381 /* Only testing timestamps in the past so we don't need to add some delta to account for time passing
382 * by while we are running the tests (unless we're running on potatoes and 24 hours somehow passes
383 * between our call to now() and format_timestamp_relative's call to now()). */
384
385 /* Years and months */
386 x = now(CLOCK_REALTIME) - (1*USEC_PER_YEAR + 1*USEC_PER_MONTH);
387 assert_se(format_timestamp_relative(buf, sizeof(buf), x));
388 log_debug("%s", buf);
389 assert_se(streq(buf, "1 year 1 month ago"));
390
391 x = now(CLOCK_REALTIME) - (1*USEC_PER_YEAR + 2*USEC_PER_MONTH);
392 assert_se(format_timestamp_relative(buf, sizeof(buf), x));
393 log_debug("%s", buf);
394 assert_se(streq(buf, "1 year 2 months ago"));
395
396 x = now(CLOCK_REALTIME) - (2*USEC_PER_YEAR + 1*USEC_PER_MONTH);
397 assert_se(format_timestamp_relative(buf, sizeof(buf), x));
398 log_debug("%s", buf);
399 assert_se(streq(buf, "2 years 1 month ago"));
400
401 x = now(CLOCK_REALTIME) - (2*USEC_PER_YEAR + 2*USEC_PER_MONTH);
402 assert_se(format_timestamp_relative(buf, sizeof(buf), x));
403 log_debug("%s", buf);
404 assert_se(streq(buf, "2 years 2 months ago"));
405
406 /* Months and days */
407 x = now(CLOCK_REALTIME) - (1*USEC_PER_MONTH + 1*USEC_PER_DAY);
408 assert_se(format_timestamp_relative(buf, sizeof(buf), x));
409 log_debug("%s", buf);
410 assert_se(streq(buf, "1 month 1 day ago"));
411
412 x = now(CLOCK_REALTIME) - (1*USEC_PER_MONTH + 2*USEC_PER_DAY);
413 assert_se(format_timestamp_relative(buf, sizeof(buf), x));
414 log_debug("%s", buf);
415 assert_se(streq(buf, "1 month 2 days ago"));
416
417 x = now(CLOCK_REALTIME) - (2*USEC_PER_MONTH + 1*USEC_PER_DAY);
418 assert_se(format_timestamp_relative(buf, sizeof(buf), x));
419 log_debug("%s", buf);
420 assert_se(streq(buf, "2 months 1 day ago"));
421
422 x = now(CLOCK_REALTIME) - (2*USEC_PER_MONTH + 2*USEC_PER_DAY);
423 assert_se(format_timestamp_relative(buf, sizeof(buf), x));
424 log_debug("%s", buf);
425 assert_se(streq(buf, "2 months 2 days ago"));
426
427 /* Weeks and days */
428 x = now(CLOCK_REALTIME) - (1*USEC_PER_WEEK + 1*USEC_PER_DAY);
429 assert_se(format_timestamp_relative(buf, sizeof(buf), x));
430 log_debug("%s", buf);
431 assert_se(streq(buf, "1 week 1 day ago"));
432
433 x = now(CLOCK_REALTIME) - (1*USEC_PER_WEEK + 2*USEC_PER_DAY);
434 assert_se(format_timestamp_relative(buf, sizeof(buf), x));
435 log_debug("%s", buf);
436 assert_se(streq(buf, "1 week 2 days ago"));
437
438 x = now(CLOCK_REALTIME) - (2*USEC_PER_WEEK + 1*USEC_PER_DAY);
439 assert_se(format_timestamp_relative(buf, sizeof(buf), x));
440 log_debug("%s", buf);
441 assert_se(streq(buf, "2 weeks 1 day ago"));
442
443 x = now(CLOCK_REALTIME) - (2*USEC_PER_WEEK + 2*USEC_PER_DAY);
444 assert_se(format_timestamp_relative(buf, sizeof(buf), x));
445 log_debug("%s", buf);
446 assert_se(streq(buf, "2 weeks 2 days ago"));
447 }
448
449 static void test_format_timestamp_utc_one(usec_t val, const char *result) {
450 char buf[FORMAT_TIMESTAMP_MAX];
451 const char *t;
452
453 t = format_timestamp_style(buf, sizeof(buf), val, TIMESTAMP_UTC);
454 assert_se(streq_ptr(t, result));
455 }
456
457 TEST(format_timestamp_utc) {
458 test_format_timestamp_utc_one(0, NULL);
459 test_format_timestamp_utc_one(1, "Thu 1970-01-01 00:00:00 UTC");
460 test_format_timestamp_utc_one(USEC_PER_SEC, "Thu 1970-01-01 00:00:01 UTC");
461
462 #if SIZEOF_TIME_T == 8
463 test_format_timestamp_utc_one(USEC_TIMESTAMP_FORMATTABLE_MAX, "Thu 9999-12-30 23:59:59 UTC");
464 test_format_timestamp_utc_one(USEC_TIMESTAMP_FORMATTABLE_MAX + 1, "--- XXXX-XX-XX XX:XX:XX");
465 #elif SIZEOF_TIME_T == 4
466 test_format_timestamp_utc_one(USEC_TIMESTAMP_FORMATTABLE_MAX, "Tue 2038-01-19 03:14:07 UTC");
467 test_format_timestamp_utc_one(USEC_TIMESTAMP_FORMATTABLE_MAX + 1, "--- XXXX-XX-XX XX:XX:XX");
468 #endif
469
470 test_format_timestamp_utc_one(USEC_INFINITY, NULL);
471 }
472
473 TEST(deserialize_dual_timestamp) {
474 int r;
475 dual_timestamp t;
476
477 r = deserialize_dual_timestamp("1234 5678", &t);
478 assert_se(r == 0);
479 assert_se(t.realtime == 1234);
480 assert_se(t.monotonic == 5678);
481
482 r = deserialize_dual_timestamp("1234x 5678", &t);
483 assert_se(r == -EINVAL);
484
485 r = deserialize_dual_timestamp("1234 5678y", &t);
486 assert_se(r == -EINVAL);
487
488 r = deserialize_dual_timestamp("-1234 5678", &t);
489 assert_se(r == -EINVAL);
490
491 r = deserialize_dual_timestamp("1234 -5678", &t);
492 assert_se(r == -EINVAL);
493
494 /* Check that output wasn't modified. */
495 assert_se(t.realtime == 1234);
496 assert_se(t.monotonic == 5678);
497
498 r = deserialize_dual_timestamp("+123 567", &t);
499 assert_se(r == 0);
500 assert_se(t.realtime == 123);
501 assert_se(t.monotonic == 567);
502
503 /* Check that we get "infinity" on overflow. */
504 r = deserialize_dual_timestamp("18446744073709551617 0", &t);
505 assert_se(r == 0);
506 assert_se(t.realtime == USEC_INFINITY);
507 assert_se(t.monotonic == 0);
508 }
509
510 static void assert_similar(usec_t a, usec_t b) {
511 usec_t d;
512
513 if (a > b)
514 d = a - b;
515 else
516 d = b - a;
517
518 assert_se(d < 10*USEC_PER_SEC);
519 }
520
521 TEST(usec_shift_clock) {
522 usec_t rt, mn, bt;
523
524 rt = now(CLOCK_REALTIME);
525 mn = now(CLOCK_MONOTONIC);
526 bt = now(clock_boottime_or_monotonic());
527
528 assert_se(usec_shift_clock(USEC_INFINITY, CLOCK_REALTIME, CLOCK_MONOTONIC) == USEC_INFINITY);
529
530 assert_similar(usec_shift_clock(rt + USEC_PER_HOUR, CLOCK_REALTIME, CLOCK_MONOTONIC), mn + USEC_PER_HOUR);
531 assert_similar(usec_shift_clock(rt + 2*USEC_PER_HOUR, CLOCK_REALTIME, clock_boottime_or_monotonic()), bt + 2*USEC_PER_HOUR);
532 assert_se(usec_shift_clock(rt + 3*USEC_PER_HOUR, CLOCK_REALTIME, CLOCK_REALTIME_ALARM) == rt + 3*USEC_PER_HOUR);
533
534 assert_similar(usec_shift_clock(mn + 4*USEC_PER_HOUR, CLOCK_MONOTONIC, CLOCK_REALTIME_ALARM), rt + 4*USEC_PER_HOUR);
535 assert_similar(usec_shift_clock(mn + 5*USEC_PER_HOUR, CLOCK_MONOTONIC, clock_boottime_or_monotonic()), bt + 5*USEC_PER_HOUR);
536 assert_se(usec_shift_clock(mn + 6*USEC_PER_HOUR, CLOCK_MONOTONIC, CLOCK_MONOTONIC) == mn + 6*USEC_PER_HOUR);
537
538 assert_similar(usec_shift_clock(bt + 7*USEC_PER_HOUR, clock_boottime_or_monotonic(), CLOCK_MONOTONIC), mn + 7*USEC_PER_HOUR);
539 assert_similar(usec_shift_clock(bt + 8*USEC_PER_HOUR, clock_boottime_or_monotonic(), CLOCK_REALTIME_ALARM), rt + 8*USEC_PER_HOUR);
540 assert_se(usec_shift_clock(bt + 9*USEC_PER_HOUR, clock_boottime_or_monotonic(), clock_boottime_or_monotonic()) == bt + 9*USEC_PER_HOUR);
541
542 if (mn > USEC_PER_MINUTE) {
543 assert_similar(usec_shift_clock(rt - 30 * USEC_PER_SEC, CLOCK_REALTIME_ALARM, CLOCK_MONOTONIC), mn - 30 * USEC_PER_SEC);
544 assert_similar(usec_shift_clock(rt - 50 * USEC_PER_SEC, CLOCK_REALTIME, clock_boottime_or_monotonic()), bt - 50 * USEC_PER_SEC);
545 }
546 }
547
548 TEST(in_utc_timezone) {
549 const char *tz = getenv("TZ");
550
551 assert_se(setenv("TZ", ":UTC", 1) >= 0);
552 assert_se(in_utc_timezone());
553 assert_se(streq(tzname[0], "UTC"));
554 assert_se(streq(tzname[1], "UTC"));
555 assert_se(timezone == 0);
556 assert_se(daylight == 0);
557
558 assert_se(setenv("TZ", ":Europe/Berlin", 1) >= 0);
559 assert_se(!in_utc_timezone());
560 assert_se(streq(tzname[0], "CET"));
561 assert_se(streq(tzname[1], "CEST"));
562
563 assert_se(set_unset_env("TZ", tz, true) == 0);
564 tzset();
565 }
566
567 TEST(map_clock_usec) {
568 usec_t nowr, x, y, z;
569
570 x = nowr = now(CLOCK_REALTIME); /* right now */
571 y = map_clock_usec(x, CLOCK_REALTIME, CLOCK_MONOTONIC);
572 z = map_clock_usec(y, CLOCK_MONOTONIC, CLOCK_REALTIME);
573 /* Converting forth and back will introduce inaccuracies, since we cannot query both clocks atomically, but it should be small. Even on the slowest CI smaller than 1h */
574
575 assert_se((z > x ? z - x : x - z) < USEC_PER_HOUR);
576
577 assert_se(nowr < USEC_INFINITY - USEC_PER_DAY*7); /* overflow check */
578 x = nowr + USEC_PER_DAY*7; /* 1 week from now */
579 y = map_clock_usec(x, CLOCK_REALTIME, CLOCK_MONOTONIC);
580 assert_se(y > 0 && y < USEC_INFINITY);
581 z = map_clock_usec(y, CLOCK_MONOTONIC, CLOCK_REALTIME);
582 assert_se(z > 0 && z < USEC_INFINITY);
583 assert_se((z > x ? z - x : x - z) < USEC_PER_HOUR);
584
585 assert_se(nowr > USEC_PER_DAY * 7); /* underflow check */
586 x = nowr - USEC_PER_DAY*7; /* 1 week ago */
587 y = map_clock_usec(x, CLOCK_REALTIME, CLOCK_MONOTONIC);
588 if (y != 0) { /* might underflow if machine is not up long enough for the monotonic clock to be beyond 1w */
589 assert_se(y < USEC_INFINITY);
590 z = map_clock_usec(y, CLOCK_MONOTONIC, CLOCK_REALTIME);
591 assert_se(z > 0 && z < USEC_INFINITY);
592 assert_se((z > x ? z - x : x - z) < USEC_PER_HOUR);
593 }
594 }
595
596 static int intro(void) {
597 log_info("realtime=" USEC_FMT "\n"
598 "monotonic=" USEC_FMT "\n"
599 "boottime=" USEC_FMT "\n",
600 now(CLOCK_REALTIME),
601 now(CLOCK_MONOTONIC),
602 now(clock_boottime_or_monotonic()));
603
604 /* Ensure time_t is signed */
605 assert_cc((time_t) -1 < (time_t) 1);
606
607 /* Ensure TIME_T_MAX works correctly */
608 uintmax_t x = TIME_T_MAX;
609 x++;
610 assert_se((time_t) x < 0);
611
612 return EXIT_SUCCESS;
613 }
614
615 DEFINE_TEST_MAIN_WITH_INTRO(LOG_INFO, intro);