]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-time-util.c
tests: rename test-time to test-time-util
[thirdparty/systemd.git] / src / test / test-time-util.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2013 Lennart Poettering
5
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.
10
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.
15
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/>.
18 ***/
19
20 #include "random-util.h"
21 #include "string-util.h"
22 #include "strv.h"
23 #include "time-util.h"
24
25 static void test_parse_sec(void) {
26 usec_t u;
27
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);
47 assert_se(u == 23);
48 assert_se(parse_sec("23µs", &u) >= 0);
49 assert_se(u == 23);
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);
54
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);
62 }
63
64 static void test_parse_sec_fix_0(void) {
65 usec_t u;
66
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);
75 }
76
77 static void test_parse_time(void) {
78 usec_t u;
79
80 assert_se(parse_time("5", &u, 1) >= 0);
81 assert_se(u == 5);
82
83 assert_se(parse_time("5", &u, USEC_PER_MSEC) >= 0);
84 assert_se(u == 5 * USEC_PER_MSEC);
85
86 assert_se(parse_time("5", &u, USEC_PER_SEC) >= 0);
87 assert_se(u == 5 * USEC_PER_SEC);
88
89 assert_se(parse_time("5s", &u, 1) >= 0);
90 assert_se(u == 5 * USEC_PER_SEC);
91
92 assert_se(parse_time("5s", &u, USEC_PER_SEC) >= 0);
93 assert_se(u == 5 * USEC_PER_SEC);
94
95 assert_se(parse_time("5s", &u, USEC_PER_MSEC) >= 0);
96 assert_se(u == 5 * USEC_PER_SEC);
97 }
98
99 static void test_parse_nsec(void) {
100 nsec_t u;
101
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);
117 assert_se(u == 2);
118 assert_se(parse_nsec(".7", &u) >= 0);
119 assert_se(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);
124
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);
132 }
133
134 static void test_format_timespan_one(usec_t x, usec_t accuracy) {
135 char *r;
136 char l[FORMAT_TIMESPAN_MAX];
137 usec_t y;
138
139 log_info(USEC_FMT" (at accuracy "USEC_FMT")", x, accuracy);
140
141 r = format_timespan(l, sizeof(l), x, accuracy);
142 assert_se(r);
143
144 log_info(" = <%s>", l);
145
146 assert_se(parse_sec(l, &y) >= 0);
147
148 log_info(" = "USEC_FMT, y);
149
150 if (accuracy <= 0)
151 accuracy = 1;
152
153 assert_se(x / accuracy == y / accuracy);
154 }
155
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);
180 }
181
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"));
186 }
187
188 static void test_get_timezones(void) {
189 _cleanup_strv_free_ char **zones = NULL;
190 int r;
191 char **zone;
192
193 r = get_timezones(&zones);
194 assert_se(r == 0);
195
196 STRV_FOREACH(zone, zones)
197 assert_se(timezone_is_valid(*zone));
198 }
199
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);
209 }
210
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);
230 }
231
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);
242 }
243
244 static void test_format_timestamp(void) {
245 unsigned i;
246
247 for (i = 0; i < 100; i++) {
248 char buf[MAX(FORMAT_TIMESTAMP_MAX, FORMAT_TIMESPAN_MAX)];
249 usec_t x, y;
250
251 random_bytes(&x, sizeof(x));
252 x = x % (2147483600 * USEC_PER_SEC) + 1;
253
254 assert_se(format_timestamp(buf, sizeof(buf), x));
255 log_info("%s", buf);
256 assert_se(parse_timestamp(buf, &y) >= 0);
257 assert_se(x / USEC_PER_SEC == y / USEC_PER_SEC);
258
259 assert_se(format_timestamp_utc(buf, sizeof(buf), x));
260 log_info("%s", buf);
261 assert_se(parse_timestamp(buf, &y) >= 0);
262 assert_se(x / USEC_PER_SEC == y / USEC_PER_SEC);
263
264 assert_se(format_timestamp_us(buf, sizeof(buf), x));
265 log_info("%s", buf);
266 assert_se(parse_timestamp(buf, &y) >= 0);
267 assert_se(x == y);
268
269 assert_se(format_timestamp_us_utc(buf, sizeof(buf), x));
270 log_info("%s", buf);
271 assert_se(parse_timestamp(buf, &y) >= 0);
272 assert_se(x == y);
273
274 assert_se(format_timestamp_relative(buf, sizeof(buf), x));
275 log_info("%s", buf);
276 assert_se(parse_timestamp(buf, &y) >= 0);
277
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);
283 }
284 }
285
286 static void test_format_timestamp_utc_one(usec_t t, const char *result) {
287 char buf[FORMAT_TIMESTAMP_MAX];
288
289 assert_se(!format_timestamp_utc(buf, sizeof(buf), t) == !result);
290
291 if (result)
292 assert_se(streq(result, buf));
293 }
294
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");
299
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");
304 #endif
305
306 test_format_timestamp_utc_one(USEC_TIMESTAMP_FORMATTABLE_MAX+1, NULL);
307 test_format_timestamp_utc_one(USEC_INFINITY, NULL);
308 }
309
310 static void test_dual_timestamp_deserialize(void) {
311 int r;
312 dual_timestamp t;
313
314 r = dual_timestamp_deserialize("1234 5678", &t);
315 assert_se(r == 0);
316 assert_se(t.realtime == 1234);
317 assert_se(t.monotonic == 5678);
318
319 r = dual_timestamp_deserialize("1234x 5678", &t);
320 assert_se(r == -EINVAL);
321
322 r = dual_timestamp_deserialize("1234 5678y", &t);
323 assert_se(r == -EINVAL);
324
325 r = dual_timestamp_deserialize("-1234 5678", &t);
326 assert_se(r == -EINVAL);
327
328 r = dual_timestamp_deserialize("1234 -5678", &t);
329 assert_se(r == -EINVAL);
330
331 /* Check that output wasn't modified. */
332 assert_se(t.realtime == 1234);
333 assert_se(t.monotonic == 5678);
334
335 r = dual_timestamp_deserialize("+123 567", &t);
336 assert_se(r == 0);
337 assert_se(t.realtime == 123);
338 assert_se(t.monotonic == 567);
339
340 /* Check that we get "infinity" on overflow. */
341 r = dual_timestamp_deserialize("18446744073709551617 0", &t);
342 assert_se(r == 0);
343 assert_se(t.realtime == USEC_INFINITY);
344 assert_se(t.monotonic == 0);
345 }
346
347 static void assert_similar(usec_t a, usec_t b) {
348 usec_t d;
349
350 if (a > b)
351 d = a - b;
352 else
353 d = b - a;
354
355 assert(d < 10*USEC_PER_SEC);
356 }
357
358 static void test_usec_shift_clock(void) {
359 usec_t rt, mn, bt;
360
361 rt = now(CLOCK_REALTIME);
362 mn = now(CLOCK_MONOTONIC);
363 bt = now(clock_boottime_or_monotonic());
364
365 assert_se(usec_shift_clock(USEC_INFINITY, CLOCK_REALTIME, CLOCK_MONOTONIC) == USEC_INFINITY);
366
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);
370
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);
374
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);
378
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);
382 }
383 }
384
385 int main(int argc, char *argv[]) {
386 uintmax_t x;
387
388 log_info("realtime=" USEC_FMT "\n"
389 "monotonic=" USEC_FMT "\n"
390 "boottime=" USEC_FMT "\n",
391 now(CLOCK_REALTIME),
392 now(CLOCK_MONOTONIC),
393 now(clock_boottime_or_monotonic()));
394
395 test_parse_sec();
396 test_parse_sec_fix_0();
397 test_parse_time();
398 test_parse_nsec();
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();
404 test_usec_add();
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();
411
412 /* Ensure time_t is signed */
413 assert_cc((time_t) -1 < (time_t) 1);
414
415 /* Ensure TIME_T_MAX works correctly */
416 x = (uintmax_t) TIME_T_MAX;
417 x++;
418 assert((time_t) x < 0);
419
420 return 0;
421 }