]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-time.c
basic/time-util: make parsing of dual_timestamp more strict
[thirdparty/systemd.git] / src / test / test-time.c
CommitLineData
cb0dac05
LP
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
1bb4b028
LP
20#include "random-util.h"
21#include "string-util.h"
6accc7a2 22#include "strv.h"
cf0fbc49 23#include "time-util.h"
cb0dac05
LP
24
25static 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);
5efdbf11
LP
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);
b1d6dcf5
ZJS
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);
cb0dac05
LP
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);
b1d6dcf5
ZJS
60 assert_se(parse_sec(" infinity .7", &u) < 0);
61 assert_se(parse_sec(".3 infinity", &u) < 0);
cb0dac05
LP
62}
63
519cffec
LP
64static void test_parse_time(void) {
65 usec_t u;
66
67 assert_se(parse_time("5", &u, 1) >= 0);
68 assert_se(u == 5);
69
70 assert_se(parse_time("5", &u, USEC_PER_MSEC) >= 0);
71 assert_se(u == 5 * USEC_PER_MSEC);
72
73 assert_se(parse_time("5", &u, USEC_PER_SEC) >= 0);
74 assert_se(u == 5 * USEC_PER_SEC);
75
76 assert_se(parse_time("5s", &u, 1) >= 0);
77 assert_se(u == 5 * USEC_PER_SEC);
78
79 assert_se(parse_time("5s", &u, USEC_PER_SEC) >= 0);
80 assert_se(u == 5 * USEC_PER_SEC);
81
82 assert_se(parse_time("5s", &u, USEC_PER_MSEC) >= 0);
83 assert_se(u == 5 * USEC_PER_SEC);
84}
85
cb0dac05
LP
86static void test_parse_nsec(void) {
87 nsec_t u;
88
89 assert_se(parse_nsec("5s", &u) >= 0);
90 assert_se(u == 5 * NSEC_PER_SEC);
91 assert_se(parse_nsec("5s500ms", &u) >= 0);
92 assert_se(u == 5 * NSEC_PER_SEC + 500 * NSEC_PER_MSEC);
93 assert_se(parse_nsec(" 5s 500ms ", &u) >= 0);
94 assert_se(u == 5 * NSEC_PER_SEC + 500 * NSEC_PER_MSEC);
95 assert_se(parse_nsec(" 5.5s ", &u) >= 0);
96 assert_se(u == 5 * NSEC_PER_SEC + 500 * NSEC_PER_MSEC);
97 assert_se(parse_nsec(" 5.5s 0.5ms ", &u) >= 0);
98 assert_se(u == 5 * NSEC_PER_SEC + 500 * NSEC_PER_MSEC + 500 * NSEC_PER_USEC);
99 assert_se(parse_nsec(" .22s ", &u) >= 0);
100 assert_se(u == 220 * NSEC_PER_MSEC);
101 assert_se(parse_nsec(" .50y ", &u) >= 0);
102 assert_se(u == NSEC_PER_YEAR / 2);
103 assert_se(parse_nsec("2.5", &u) >= 0);
104 assert_se(u == 2);
105 assert_se(parse_nsec(".7", &u) >= 0);
106 assert_se(u == 0);
fdd30a15
DM
107 assert_se(parse_nsec("infinity", &u) >= 0);
108 assert_se(u == NSEC_INFINITY);
109 assert_se(parse_nsec(" infinity ", &u) >= 0);
110 assert_se(u == NSEC_INFINITY);
cb0dac05
LP
111
112 assert_se(parse_nsec(" xyz ", &u) < 0);
113 assert_se(parse_nsec("", &u) < 0);
114 assert_se(parse_nsec(" . ", &u) < 0);
115 assert_se(parse_nsec(" 5. ", &u) < 0);
116 assert_se(parse_nsec(".s ", &u) < 0);
fdd30a15
DM
117 assert_se(parse_nsec(" infinity .7", &u) < 0);
118 assert_se(parse_nsec(".3 infinity", &u) < 0);
cb0dac05
LP
119}
120
2fa4092c
LP
121static void test_format_timespan_one(usec_t x, usec_t accuracy) {
122 char *r;
123 char l[FORMAT_TIMESPAN_MAX];
124 usec_t y;
125
de0671ee 126 log_info(USEC_FMT" (at accuracy "USEC_FMT")", x, accuracy);
2fa4092c
LP
127
128 r = format_timespan(l, sizeof(l), x, accuracy);
129 assert_se(r);
130
131 log_info(" = <%s>", l);
132
133 assert_se(parse_sec(l, &y) >= 0);
134
de0671ee 135 log_info(" = "USEC_FMT, y);
2fa4092c
LP
136
137 if (accuracy <= 0)
138 accuracy = 1;
139
140 assert_se(x / accuracy == y / accuracy);
141}
142
143static void test_format_timespan(usec_t accuracy) {
144 test_format_timespan_one(0, accuracy);
145 test_format_timespan_one(1, accuracy);
146 test_format_timespan_one(1*USEC_PER_SEC, accuracy);
147 test_format_timespan_one(999*USEC_PER_MSEC, accuracy);
148 test_format_timespan_one(1234567, accuracy);
149 test_format_timespan_one(12, accuracy);
150 test_format_timespan_one(123, accuracy);
151 test_format_timespan_one(1234, accuracy);
152 test_format_timespan_one(12345, accuracy);
153 test_format_timespan_one(123456, accuracy);
154 test_format_timespan_one(1234567, accuracy);
155 test_format_timespan_one(12345678, accuracy);
156 test_format_timespan_one(1200000, accuracy);
157 test_format_timespan_one(1230000, accuracy);
158 test_format_timespan_one(1230000, accuracy);
159 test_format_timespan_one(1234000, accuracy);
160 test_format_timespan_one(1234500, accuracy);
161 test_format_timespan_one(1234560, accuracy);
162 test_format_timespan_one(1234567, accuracy);
163 test_format_timespan_one(986087, accuracy);
164 test_format_timespan_one(500 * USEC_PER_MSEC, accuracy);
165 test_format_timespan_one(9*USEC_PER_YEAR/5 - 23, accuracy);
b1d6dcf5 166 test_format_timespan_one(USEC_INFINITY, accuracy);
2fa4092c
LP
167}
168
6accc7a2
RC
169static void test_timezone_is_valid(void) {
170 assert_se(timezone_is_valid("Europe/Berlin"));
171 assert_se(timezone_is_valid("Australia/Sydney"));
172 assert_se(!timezone_is_valid("Europe/Do not exist"));
173}
174
175static void test_get_timezones(void) {
176 _cleanup_strv_free_ char **zones = NULL;
177 int r;
178 char **zone;
179
180 r = get_timezones(&zones);
181 assert_se(r == 0);
182
53f555b6 183 STRV_FOREACH(zone, zones)
6accc7a2 184 assert_se(timezone_is_valid(*zone));
53f555b6
LP
185}
186
187static void test_usec_add(void) {
188 assert_se(usec_add(0, 0) == 0);
189 assert_se(usec_add(1, 4) == 5);
190 assert_se(usec_add(USEC_INFINITY, 5) == USEC_INFINITY);
191 assert_se(usec_add(5, USEC_INFINITY) == USEC_INFINITY);
192 assert_se(usec_add(USEC_INFINITY-5, 2) == USEC_INFINITY-3);
193 assert_se(usec_add(USEC_INFINITY-2, 2) == USEC_INFINITY);
194 assert_se(usec_add(USEC_INFINITY-1, 2) == USEC_INFINITY);
195 assert_se(usec_add(USEC_INFINITY, 2) == USEC_INFINITY);
6accc7a2
RC
196}
197
04a1d84c
LP
198static void test_usec_sub(void) {
199 assert_se(usec_sub(0, 0) == 0);
200 assert_se(usec_sub(4, 1) == 3);
201 assert_se(usec_sub(4, 4) == 0);
202 assert_se(usec_sub(4, 5) == 0);
203 assert_se(usec_sub(USEC_INFINITY-3, -3) == USEC_INFINITY);
204 assert_se(usec_sub(USEC_INFINITY-3, -3) == USEC_INFINITY);
205 assert_se(usec_sub(USEC_INFINITY-3, -4) == USEC_INFINITY);
206 assert_se(usec_sub(USEC_INFINITY-3, -5) == USEC_INFINITY);
207 assert_se(usec_sub(USEC_INFINITY, 5) == USEC_INFINITY);
208}
209
21b3a0fc
LP
210static void test_format_timestamp(void) {
211 unsigned i;
212
213 for (i = 0; i < 100; i++) {
214 char buf[MAX(FORMAT_TIMESTAMP_MAX, FORMAT_TIMESPAN_MAX)];
215 usec_t x, y;
216
217 random_bytes(&x, sizeof(x));
218 x = x % (2147483600 * USEC_PER_SEC) + 1;
219
220 assert_se(format_timestamp(buf, sizeof(buf), x));
221 log_info("%s", buf);
222 assert_se(parse_timestamp(buf, &y) >= 0);
223 assert_se(x / USEC_PER_SEC == y / USEC_PER_SEC);
224
225 assert_se(format_timestamp_utc(buf, sizeof(buf), x));
226 log_info("%s", buf);
227 assert_se(parse_timestamp(buf, &y) >= 0);
228 assert_se(x / USEC_PER_SEC == y / USEC_PER_SEC);
229
230 assert_se(format_timestamp_us(buf, sizeof(buf), x));
231 log_info("%s", buf);
232 assert_se(parse_timestamp(buf, &y) >= 0);
233 assert_se(x == y);
234
235 assert_se(format_timestamp_us_utc(buf, sizeof(buf), x));
236 log_info("%s", buf);
237 assert_se(parse_timestamp(buf, &y) >= 0);
238 assert_se(x == y);
239
240 assert_se(format_timestamp_relative(buf, sizeof(buf), x));
241 log_info("%s", buf);
242 assert_se(parse_timestamp(buf, &y) >= 0);
243
244 /* The two calls above will run with a slightly different local time. Make sure we are in the same
245 * range however, but give enough leeway that this is unlikely to explode. And of course,
246 * format_timestamp_relative() scales the accuracy with the distance from the current time up to one
247 * month, cover for that too. */
248 assert_se(y > x ? y - x : x - y <= USEC_PER_MONTH + USEC_PER_DAY);
249 }
250}
251
1bb4b028
LP
252static void test_format_timestamp_utc_one(usec_t t, const char *result) {
253 char buf[FORMAT_TIMESTAMP_MAX];
254
255 assert_se(!format_timestamp_utc(buf, sizeof(buf), t) == !result);
256
257 if (result)
258 assert_se(streq(result, buf));
259}
260
261static void test_format_timestamp_utc(void) {
262 test_format_timestamp_utc_one(0, NULL);
263 test_format_timestamp_utc_one(1, "Thu 1970-01-01 00:00:00 UTC");
264 test_format_timestamp_utc_one(USEC_PER_SEC, "Thu 1970-01-01 00:00:01 UTC");
265
266#if SIZEOF_TIME_T == 8
267 test_format_timestamp_utc_one(USEC_TIMESTAMP_FORMATTABLE_MAX, "Thu 9999-12-30 23:59:59 UTC");
268#elif SIZEOF_TIME_T == 4
269 test_format_timestamp_utc_one(USEC_TIMESTAMP_FORMATTABLE_MAX, "Tue 2038-01-19 03:14:07 UTC");
270#endif
271
272 test_format_timestamp_utc_one(USEC_TIMESTAMP_FORMATTABLE_MAX+1, NULL);
273 test_format_timestamp_utc_one(USEC_INFINITY, NULL);
274}
275
9c0565b2
ZJS
276static void test_dual_timestamp_deserialize(void) {
277 int r;
278 dual_timestamp t;
279
280 r = dual_timestamp_deserialize("1234 5678", &t);
281 assert_se(r == 0);
282 assert_se(t.realtime == 1234);
283 assert_se(t.monotonic == 5678);
284
285 r = dual_timestamp_deserialize("1234x 5678", &t);
286 assert_se(r == -EINVAL);
287
288 r = dual_timestamp_deserialize("1234 5678y", &t);
289 assert_se(r == -EINVAL);
290
291 r = dual_timestamp_deserialize("-1234 5678", &t);
292 assert_se(r == -EINVAL);
293
294 r = dual_timestamp_deserialize("1234 -5678", &t);
295 assert_se(r == -EINVAL);
296
297 /* Check that output wasn't modified. */
298 assert_se(t.realtime == 1234);
299 assert_se(t.monotonic == 5678);
300
301 r = dual_timestamp_deserialize("+123 567", &t);
302 assert_se(r == 0);
303 assert_se(t.realtime == 123);
304 assert_se(t.monotonic == 567);
305
306 /* Check that we get "infinity" on overflow. */
307 r = dual_timestamp_deserialize("18446744073709551617 0", &t);
308 assert_se(r == 0);
309 assert_se(t.realtime == USEC_INFINITY);
310 assert_se(t.monotonic == 0);
311}
312
cb0dac05 313int main(int argc, char *argv[]) {
2d60169d
LP
314 uintmax_t x;
315
cb0dac05 316 test_parse_sec();
519cffec 317 test_parse_time();
cb0dac05 318 test_parse_nsec();
2fa4092c
LP
319 test_format_timespan(1);
320 test_format_timespan(USEC_PER_MSEC);
321 test_format_timespan(USEC_PER_SEC);
6accc7a2
RC
322 test_timezone_is_valid();
323 test_get_timezones();
53f555b6 324 test_usec_add();
04a1d84c 325 test_usec_sub();
21b3a0fc 326 test_format_timestamp();
1bb4b028 327 test_format_timestamp_utc();
9c0565b2 328 test_dual_timestamp_deserialize();
6accc7a2 329
2d60169d
LP
330 /* Ensure time_t is signed */
331 assert_cc((time_t) -1 < (time_t) 1);
332
333 /* Ensure TIME_T_MAX works correctly */
334 x = (uintmax_t) TIME_T_MAX;
313cefa1 335 x++;
2d60169d
LP
336 assert((time_t) x < 0);
337
cb0dac05
LP
338 return 0;
339}