]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
cb0dac05 | 2 | |
1bb4b028 | 3 | #include "random-util.h" |
d68c645b | 4 | #include "serialize.h" |
1bb4b028 | 5 | #include "string-util.h" |
6accc7a2 | 6 | #include "strv.h" |
cf0fbc49 | 7 | #include "time-util.h" |
cb0dac05 LP |
8 | |
9 | static void test_parse_sec(void) { | |
10 | usec_t u; | |
11 | ||
f1880a4b ZJS |
12 | log_info("/* %s */", __func__); |
13 | ||
cb0dac05 LP |
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); | |
5efdbf11 LP |
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); | |
b1d6dcf5 ZJS |
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); | |
279f52a1 FB |
40 | assert_se(parse_sec("+3.1s", &u) >= 0); |
41 | assert_se(u == 3100 * USEC_PER_MSEC); | |
cb0dac05 LP |
42 | |
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); | |
5a9fb358 LP |
48 | assert_se(parse_sec("-5s ", &u) < 0); |
49 | assert_se(parse_sec("-0.3s ", &u) < 0); | |
50 | assert_se(parse_sec("-0.0s ", &u) < 0); | |
51 | assert_se(parse_sec("-0.-0s ", &u) < 0); | |
52 | assert_se(parse_sec("0.-0s ", &u) < 0); | |
53 | assert_se(parse_sec("3.-0s ", &u) < 0); | |
b1d6dcf5 ZJS |
54 | assert_se(parse_sec(" infinity .7", &u) < 0); |
55 | assert_se(parse_sec(".3 infinity", &u) < 0); | |
279f52a1 FB |
56 | assert_se(parse_sec("3.+1s", &u) < 0); |
57 | assert_se(parse_sec("3. 1s", &u) < 0); | |
58 | assert_se(parse_sec("3.s", &u) < 0); | |
cb0dac05 LP |
59 | } |
60 | ||
0004f698 ZJS |
61 | static void test_parse_sec_fix_0(void) { |
62 | usec_t u; | |
63 | ||
f1880a4b ZJS |
64 | log_info("/* %s */", __func__); |
65 | ||
0004f698 ZJS |
66 | assert_se(parse_sec_fix_0("5s", &u) >= 0); |
67 | assert_se(u == 5 * USEC_PER_SEC); | |
68 | assert_se(parse_sec_fix_0("0s", &u) >= 0); | |
def34f63 | 69 | assert_se(u == USEC_INFINITY); |
0004f698 ZJS |
70 | assert_se(parse_sec_fix_0("0", &u) >= 0); |
71 | assert_se(u == USEC_INFINITY); | |
72 | assert_se(parse_sec_fix_0(" 0", &u) >= 0); | |
73 | assert_se(u == USEC_INFINITY); | |
74 | } | |
75 | ||
519cffec LP |
76 | static void test_parse_time(void) { |
77 | usec_t u; | |
78 | ||
f1880a4b ZJS |
79 | log_info("/* %s */", __func__); |
80 | ||
519cffec LP |
81 | assert_se(parse_time("5", &u, 1) >= 0); |
82 | assert_se(u == 5); | |
83 | ||
84 | assert_se(parse_time("5", &u, USEC_PER_MSEC) >= 0); | |
85 | assert_se(u == 5 * USEC_PER_MSEC); | |
86 | ||
87 | assert_se(parse_time("5", &u, USEC_PER_SEC) >= 0); | |
88 | assert_se(u == 5 * USEC_PER_SEC); | |
89 | ||
90 | assert_se(parse_time("5s", &u, 1) >= 0); | |
91 | assert_se(u == 5 * USEC_PER_SEC); | |
92 | ||
93 | assert_se(parse_time("5s", &u, USEC_PER_SEC) >= 0); | |
94 | assert_se(u == 5 * USEC_PER_SEC); | |
95 | ||
96 | assert_se(parse_time("5s", &u, USEC_PER_MSEC) >= 0); | |
97 | assert_se(u == 5 * USEC_PER_SEC); | |
db4e6107 YW |
98 | |
99 | assert_se(parse_time("11111111111111y", &u, 1) == -ERANGE); | |
100 | assert_se(parse_time("1.1111111111111y", &u, 1) == -ERANGE); | |
519cffec LP |
101 | } |
102 | ||
cb0dac05 LP |
103 | static void test_parse_nsec(void) { |
104 | nsec_t u; | |
105 | ||
f1880a4b ZJS |
106 | log_info("/* %s */", __func__); |
107 | ||
cb0dac05 LP |
108 | assert_se(parse_nsec("5s", &u) >= 0); |
109 | assert_se(u == 5 * NSEC_PER_SEC); | |
110 | assert_se(parse_nsec("5s500ms", &u) >= 0); | |
111 | assert_se(u == 5 * NSEC_PER_SEC + 500 * NSEC_PER_MSEC); | |
112 | assert_se(parse_nsec(" 5s 500ms ", &u) >= 0); | |
113 | assert_se(u == 5 * NSEC_PER_SEC + 500 * NSEC_PER_MSEC); | |
114 | assert_se(parse_nsec(" 5.5s ", &u) >= 0); | |
115 | assert_se(u == 5 * NSEC_PER_SEC + 500 * NSEC_PER_MSEC); | |
116 | assert_se(parse_nsec(" 5.5s 0.5ms ", &u) >= 0); | |
117 | assert_se(u == 5 * NSEC_PER_SEC + 500 * NSEC_PER_MSEC + 500 * NSEC_PER_USEC); | |
118 | assert_se(parse_nsec(" .22s ", &u) >= 0); | |
119 | assert_se(u == 220 * NSEC_PER_MSEC); | |
120 | assert_se(parse_nsec(" .50y ", &u) >= 0); | |
121 | assert_se(u == NSEC_PER_YEAR / 2); | |
122 | assert_se(parse_nsec("2.5", &u) >= 0); | |
123 | assert_se(u == 2); | |
124 | assert_se(parse_nsec(".7", &u) >= 0); | |
125 | assert_se(u == 0); | |
fdd30a15 DM |
126 | assert_se(parse_nsec("infinity", &u) >= 0); |
127 | assert_se(u == NSEC_INFINITY); | |
128 | assert_se(parse_nsec(" infinity ", &u) >= 0); | |
129 | assert_se(u == NSEC_INFINITY); | |
279f52a1 FB |
130 | assert_se(parse_nsec("+3.1s", &u) >= 0); |
131 | assert_se(u == 3100 * NSEC_PER_MSEC); | |
cb0dac05 LP |
132 | |
133 | assert_se(parse_nsec(" xyz ", &u) < 0); | |
134 | assert_se(parse_nsec("", &u) < 0); | |
135 | assert_se(parse_nsec(" . ", &u) < 0); | |
136 | assert_se(parse_nsec(" 5. ", &u) < 0); | |
137 | assert_se(parse_nsec(".s ", &u) < 0); | |
fdd30a15 DM |
138 | assert_se(parse_nsec(" infinity .7", &u) < 0); |
139 | assert_se(parse_nsec(".3 infinity", &u) < 0); | |
279f52a1 FB |
140 | assert_se(parse_nsec("-5s ", &u) < 0); |
141 | assert_se(parse_nsec("-0.3s ", &u) < 0); | |
142 | assert_se(parse_nsec("-0.0s ", &u) < 0); | |
143 | assert_se(parse_nsec("-0.-0s ", &u) < 0); | |
144 | assert_se(parse_nsec("0.-0s ", &u) < 0); | |
145 | assert_se(parse_nsec("3.-0s ", &u) < 0); | |
146 | assert_se(parse_nsec(" infinity .7", &u) < 0); | |
147 | assert_se(parse_nsec(".3 infinity", &u) < 0); | |
148 | assert_se(parse_nsec("3.+1s", &u) < 0); | |
149 | assert_se(parse_nsec("3. 1s", &u) < 0); | |
150 | assert_se(parse_nsec("3.s", &u) < 0); | |
db4e6107 YW |
151 | assert_se(parse_nsec("1111111111111y", &u) == -ERANGE); |
152 | assert_se(parse_nsec("1.111111111111y", &u) == -ERANGE); | |
cb0dac05 LP |
153 | } |
154 | ||
2fa4092c | 155 | static void test_format_timespan_one(usec_t x, usec_t accuracy) { |
2fa4092c | 156 | char l[FORMAT_TIMESPAN_MAX]; |
4d9685be | 157 | const char *t; |
2fa4092c LP |
158 | usec_t y; |
159 | ||
de0671ee | 160 | log_info(USEC_FMT" (at accuracy "USEC_FMT")", x, accuracy); |
2fa4092c | 161 | |
4d9685be ZJS |
162 | assert_se(t = format_timespan(l, sizeof l, x, accuracy)); |
163 | log_info(" = <%s>", t); | |
2fa4092c | 164 | |
4d9685be | 165 | assert_se(parse_sec(t, &y) >= 0); |
de0671ee | 166 | log_info(" = "USEC_FMT, y); |
2fa4092c LP |
167 | |
168 | if (accuracy <= 0) | |
169 | accuracy = 1; | |
170 | ||
171 | assert_se(x / accuracy == y / accuracy); | |
172 | } | |
173 | ||
174 | static void test_format_timespan(usec_t accuracy) { | |
f1880a4b ZJS |
175 | log_info("/* %s accuracy="USEC_FMT" */", __func__, accuracy); |
176 | ||
2fa4092c LP |
177 | test_format_timespan_one(0, accuracy); |
178 | test_format_timespan_one(1, accuracy); | |
179 | test_format_timespan_one(1*USEC_PER_SEC, accuracy); | |
180 | test_format_timespan_one(999*USEC_PER_MSEC, accuracy); | |
181 | test_format_timespan_one(1234567, accuracy); | |
182 | test_format_timespan_one(12, accuracy); | |
183 | test_format_timespan_one(123, accuracy); | |
184 | test_format_timespan_one(1234, accuracy); | |
185 | test_format_timespan_one(12345, accuracy); | |
186 | test_format_timespan_one(123456, accuracy); | |
187 | test_format_timespan_one(1234567, accuracy); | |
188 | test_format_timespan_one(12345678, accuracy); | |
189 | test_format_timespan_one(1200000, accuracy); | |
190 | test_format_timespan_one(1230000, accuracy); | |
191 | test_format_timespan_one(1230000, accuracy); | |
192 | test_format_timespan_one(1234000, accuracy); | |
193 | test_format_timespan_one(1234500, accuracy); | |
194 | test_format_timespan_one(1234560, accuracy); | |
195 | test_format_timespan_one(1234567, accuracy); | |
196 | test_format_timespan_one(986087, accuracy); | |
197 | test_format_timespan_one(500 * USEC_PER_MSEC, accuracy); | |
198 | test_format_timespan_one(9*USEC_PER_YEAR/5 - 23, accuracy); | |
b1d6dcf5 | 199 | test_format_timespan_one(USEC_INFINITY, accuracy); |
2fa4092c LP |
200 | } |
201 | ||
6accc7a2 | 202 | static void test_timezone_is_valid(void) { |
f1880a4b ZJS |
203 | log_info("/* %s */", __func__); |
204 | ||
089fb865 MG |
205 | assert_se(timezone_is_valid("Europe/Berlin", LOG_ERR)); |
206 | assert_se(timezone_is_valid("Australia/Sydney", LOG_ERR)); | |
207 | assert_se(!timezone_is_valid("Europe/Do not exist", LOG_ERR)); | |
6accc7a2 RC |
208 | } |
209 | ||
210 | static void test_get_timezones(void) { | |
211 | _cleanup_strv_free_ char **zones = NULL; | |
212 | int r; | |
213 | char **zone; | |
214 | ||
f1880a4b ZJS |
215 | log_info("/* %s */", __func__); |
216 | ||
6accc7a2 RC |
217 | r = get_timezones(&zones); |
218 | assert_se(r == 0); | |
219 | ||
53f555b6 | 220 | STRV_FOREACH(zone, zones) |
089fb865 | 221 | assert_se(timezone_is_valid(*zone, LOG_ERR)); |
53f555b6 LP |
222 | } |
223 | ||
224 | static void test_usec_add(void) { | |
f1880a4b ZJS |
225 | log_info("/* %s */", __func__); |
226 | ||
53f555b6 LP |
227 | assert_se(usec_add(0, 0) == 0); |
228 | assert_se(usec_add(1, 4) == 5); | |
229 | assert_se(usec_add(USEC_INFINITY, 5) == USEC_INFINITY); | |
230 | assert_se(usec_add(5, USEC_INFINITY) == USEC_INFINITY); | |
231 | assert_se(usec_add(USEC_INFINITY-5, 2) == USEC_INFINITY-3); | |
232 | assert_se(usec_add(USEC_INFINITY-2, 2) == USEC_INFINITY); | |
233 | assert_se(usec_add(USEC_INFINITY-1, 2) == USEC_INFINITY); | |
234 | assert_se(usec_add(USEC_INFINITY, 2) == USEC_INFINITY); | |
6accc7a2 RC |
235 | } |
236 | ||
54d8ef14 | 237 | static void test_usec_sub_unsigned(void) { |
f1880a4b ZJS |
238 | log_info("/* %s */", __func__); |
239 | ||
54d8ef14 LP |
240 | assert_se(usec_sub_unsigned(0, 0) == 0); |
241 | assert_se(usec_sub_unsigned(0, 2) == 0); | |
242 | assert_se(usec_sub_unsigned(0, USEC_INFINITY) == 0); | |
243 | assert_se(usec_sub_unsigned(1, 0) == 1); | |
244 | assert_se(usec_sub_unsigned(1, 1) == 0); | |
245 | assert_se(usec_sub_unsigned(1, 2) == 0); | |
246 | assert_se(usec_sub_unsigned(1, 3) == 0); | |
247 | assert_se(usec_sub_unsigned(1, USEC_INFINITY) == 0); | |
248 | assert_se(usec_sub_unsigned(USEC_INFINITY-1, 0) == USEC_INFINITY-1); | |
249 | assert_se(usec_sub_unsigned(USEC_INFINITY-1, 1) == USEC_INFINITY-2); | |
250 | assert_se(usec_sub_unsigned(USEC_INFINITY-1, 2) == USEC_INFINITY-3); | |
251 | assert_se(usec_sub_unsigned(USEC_INFINITY-1, USEC_INFINITY-2) == 1); | |
252 | assert_se(usec_sub_unsigned(USEC_INFINITY-1, USEC_INFINITY-1) == 0); | |
253 | assert_se(usec_sub_unsigned(USEC_INFINITY-1, USEC_INFINITY) == 0); | |
254 | assert_se(usec_sub_unsigned(USEC_INFINITY, 0) == USEC_INFINITY); | |
255 | assert_se(usec_sub_unsigned(USEC_INFINITY, 1) == USEC_INFINITY); | |
256 | assert_se(usec_sub_unsigned(USEC_INFINITY, 2) == USEC_INFINITY); | |
257 | assert_se(usec_sub_unsigned(USEC_INFINITY, USEC_INFINITY) == USEC_INFINITY); | |
258 | } | |
259 | ||
260 | static void test_usec_sub_signed(void) { | |
f1880a4b ZJS |
261 | log_info("/* %s */", __func__); |
262 | ||
54d8ef14 LP |
263 | assert_se(usec_sub_signed(0, 0) == 0); |
264 | assert_se(usec_sub_signed(4, 1) == 3); | |
265 | assert_se(usec_sub_signed(4, 4) == 0); | |
266 | assert_se(usec_sub_signed(4, 5) == 0); | |
267 | assert_se(usec_sub_signed(USEC_INFINITY-3, -3) == USEC_INFINITY); | |
268 | assert_se(usec_sub_signed(USEC_INFINITY-3, -3) == USEC_INFINITY); | |
269 | assert_se(usec_sub_signed(USEC_INFINITY-3, -4) == USEC_INFINITY); | |
270 | assert_se(usec_sub_signed(USEC_INFINITY-3, -5) == USEC_INFINITY); | |
271 | assert_se(usec_sub_signed(USEC_INFINITY, 5) == USEC_INFINITY); | |
04a1d84c LP |
272 | } |
273 | ||
21b3a0fc LP |
274 | static void test_format_timestamp(void) { |
275 | unsigned i; | |
276 | ||
f1880a4b ZJS |
277 | log_info("/* %s */", __func__); |
278 | ||
21b3a0fc LP |
279 | for (i = 0; i < 100; i++) { |
280 | char buf[MAX(FORMAT_TIMESTAMP_MAX, FORMAT_TIMESPAN_MAX)]; | |
281 | usec_t x, y; | |
282 | ||
283 | random_bytes(&x, sizeof(x)); | |
284 | x = x % (2147483600 * USEC_PER_SEC) + 1; | |
285 | ||
286 | assert_se(format_timestamp(buf, sizeof(buf), x)); | |
287 | log_info("%s", buf); | |
288 | assert_se(parse_timestamp(buf, &y) >= 0); | |
289 | assert_se(x / USEC_PER_SEC == y / USEC_PER_SEC); | |
290 | ||
291 | assert_se(format_timestamp_utc(buf, sizeof(buf), x)); | |
292 | log_info("%s", buf); | |
293 | assert_se(parse_timestamp(buf, &y) >= 0); | |
294 | assert_se(x / USEC_PER_SEC == y / USEC_PER_SEC); | |
295 | ||
296 | assert_se(format_timestamp_us(buf, sizeof(buf), x)); | |
297 | log_info("%s", buf); | |
298 | assert_se(parse_timestamp(buf, &y) >= 0); | |
299 | assert_se(x == y); | |
300 | ||
301 | assert_se(format_timestamp_us_utc(buf, sizeof(buf), x)); | |
302 | log_info("%s", buf); | |
303 | assert_se(parse_timestamp(buf, &y) >= 0); | |
304 | assert_se(x == y); | |
305 | ||
306 | assert_se(format_timestamp_relative(buf, sizeof(buf), x)); | |
307 | log_info("%s", buf); | |
308 | assert_se(parse_timestamp(buf, &y) >= 0); | |
309 | ||
310 | /* The two calls above will run with a slightly different local time. Make sure we are in the same | |
311 | * range however, but give enough leeway that this is unlikely to explode. And of course, | |
312 | * format_timestamp_relative() scales the accuracy with the distance from the current time up to one | |
313 | * month, cover for that too. */ | |
314 | assert_se(y > x ? y - x : x - y <= USEC_PER_MONTH + USEC_PER_DAY); | |
315 | } | |
316 | } | |
317 | ||
4d9685be | 318 | static void test_format_timestamp_utc_one(usec_t val, const char *result) { |
1bb4b028 | 319 | char buf[FORMAT_TIMESTAMP_MAX]; |
4d9685be | 320 | const char *t; |
1bb4b028 | 321 | |
4d9685be ZJS |
322 | t = format_timestamp_utc(buf, sizeof(buf), val); |
323 | assert_se(streq_ptr(t, result)); | |
1bb4b028 LP |
324 | } |
325 | ||
326 | static void test_format_timestamp_utc(void) { | |
f1880a4b ZJS |
327 | log_info("/* %s */", __func__); |
328 | ||
1bb4b028 LP |
329 | test_format_timestamp_utc_one(0, NULL); |
330 | test_format_timestamp_utc_one(1, "Thu 1970-01-01 00:00:00 UTC"); | |
331 | test_format_timestamp_utc_one(USEC_PER_SEC, "Thu 1970-01-01 00:00:01 UTC"); | |
332 | ||
333 | #if SIZEOF_TIME_T == 8 | |
334 | test_format_timestamp_utc_one(USEC_TIMESTAMP_FORMATTABLE_MAX, "Thu 9999-12-30 23:59:59 UTC"); | |
d3d28024 | 335 | test_format_timestamp_utc_one(USEC_TIMESTAMP_FORMATTABLE_MAX + 1, "--- XXXX-XX-XX XX:XX:XX"); |
1bb4b028 LP |
336 | #elif SIZEOF_TIME_T == 4 |
337 | test_format_timestamp_utc_one(USEC_TIMESTAMP_FORMATTABLE_MAX, "Tue 2038-01-19 03:14:07 UTC"); | |
d3d28024 | 338 | test_format_timestamp_utc_one(USEC_TIMESTAMP_FORMATTABLE_MAX + 1, "--- XXXX-XX-XX XX:XX:XX"); |
1bb4b028 LP |
339 | #endif |
340 | ||
1bb4b028 LP |
341 | test_format_timestamp_utc_one(USEC_INFINITY, NULL); |
342 | } | |
343 | ||
d68c645b | 344 | static void test_deserialize_dual_timestamp(void) { |
9c0565b2 ZJS |
345 | int r; |
346 | dual_timestamp t; | |
347 | ||
f1880a4b ZJS |
348 | log_info("/* %s */", __func__); |
349 | ||
d68c645b | 350 | r = deserialize_dual_timestamp("1234 5678", &t); |
9c0565b2 ZJS |
351 | assert_se(r == 0); |
352 | assert_se(t.realtime == 1234); | |
353 | assert_se(t.monotonic == 5678); | |
354 | ||
d68c645b | 355 | r = deserialize_dual_timestamp("1234x 5678", &t); |
9c0565b2 ZJS |
356 | assert_se(r == -EINVAL); |
357 | ||
d68c645b | 358 | r = deserialize_dual_timestamp("1234 5678y", &t); |
9c0565b2 ZJS |
359 | assert_se(r == -EINVAL); |
360 | ||
d68c645b | 361 | r = deserialize_dual_timestamp("-1234 5678", &t); |
9c0565b2 ZJS |
362 | assert_se(r == -EINVAL); |
363 | ||
d68c645b | 364 | r = deserialize_dual_timestamp("1234 -5678", &t); |
9c0565b2 ZJS |
365 | assert_se(r == -EINVAL); |
366 | ||
367 | /* Check that output wasn't modified. */ | |
368 | assert_se(t.realtime == 1234); | |
369 | assert_se(t.monotonic == 5678); | |
370 | ||
d68c645b | 371 | r = deserialize_dual_timestamp("+123 567", &t); |
9c0565b2 ZJS |
372 | assert_se(r == 0); |
373 | assert_se(t.realtime == 123); | |
374 | assert_se(t.monotonic == 567); | |
375 | ||
376 | /* Check that we get "infinity" on overflow. */ | |
d68c645b | 377 | r = deserialize_dual_timestamp("18446744073709551617 0", &t); |
9c0565b2 ZJS |
378 | assert_se(r == 0); |
379 | assert_se(t.realtime == USEC_INFINITY); | |
380 | assert_se(t.monotonic == 0); | |
381 | } | |
382 | ||
1007ec60 LP |
383 | static void assert_similar(usec_t a, usec_t b) { |
384 | usec_t d; | |
385 | ||
386 | if (a > b) | |
387 | d = a - b; | |
388 | else | |
389 | d = b - a; | |
390 | ||
391 | assert(d < 10*USEC_PER_SEC); | |
392 | } | |
393 | ||
394 | static void test_usec_shift_clock(void) { | |
395 | usec_t rt, mn, bt; | |
396 | ||
f1880a4b ZJS |
397 | log_info("/* %s */", __func__); |
398 | ||
1007ec60 LP |
399 | rt = now(CLOCK_REALTIME); |
400 | mn = now(CLOCK_MONOTONIC); | |
401 | bt = now(clock_boottime_or_monotonic()); | |
402 | ||
403 | assert_se(usec_shift_clock(USEC_INFINITY, CLOCK_REALTIME, CLOCK_MONOTONIC) == USEC_INFINITY); | |
404 | ||
405 | assert_similar(usec_shift_clock(rt + USEC_PER_HOUR, CLOCK_REALTIME, CLOCK_MONOTONIC), mn + USEC_PER_HOUR); | |
406 | assert_similar(usec_shift_clock(rt + 2*USEC_PER_HOUR, CLOCK_REALTIME, clock_boottime_or_monotonic()), bt + 2*USEC_PER_HOUR); | |
407 | assert_se(usec_shift_clock(rt + 3*USEC_PER_HOUR, CLOCK_REALTIME, CLOCK_REALTIME_ALARM) == rt + 3*USEC_PER_HOUR); | |
408 | ||
409 | assert_similar(usec_shift_clock(mn + 4*USEC_PER_HOUR, CLOCK_MONOTONIC, CLOCK_REALTIME_ALARM), rt + 4*USEC_PER_HOUR); | |
410 | assert_similar(usec_shift_clock(mn + 5*USEC_PER_HOUR, CLOCK_MONOTONIC, clock_boottime_or_monotonic()), bt + 5*USEC_PER_HOUR); | |
411 | assert_se(usec_shift_clock(mn + 6*USEC_PER_HOUR, CLOCK_MONOTONIC, CLOCK_MONOTONIC) == mn + 6*USEC_PER_HOUR); | |
412 | ||
413 | assert_similar(usec_shift_clock(bt + 7*USEC_PER_HOUR, clock_boottime_or_monotonic(), CLOCK_MONOTONIC), mn + 7*USEC_PER_HOUR); | |
414 | assert_similar(usec_shift_clock(bt + 8*USEC_PER_HOUR, clock_boottime_or_monotonic(), CLOCK_REALTIME_ALARM), rt + 8*USEC_PER_HOUR); | |
415 | assert_se(usec_shift_clock(bt + 9*USEC_PER_HOUR, clock_boottime_or_monotonic(), clock_boottime_or_monotonic()) == bt + 9*USEC_PER_HOUR); | |
416 | ||
417 | if (mn > USEC_PER_MINUTE) { | |
418 | assert_similar(usec_shift_clock(rt - 30 * USEC_PER_SEC, CLOCK_REALTIME_ALARM, CLOCK_MONOTONIC), mn - 30 * USEC_PER_SEC); | |
419 | assert_similar(usec_shift_clock(rt - 50 * USEC_PER_SEC, CLOCK_REALTIME, clock_boottime_or_monotonic()), bt - 50 * USEC_PER_SEC); | |
420 | } | |
421 | } | |
422 | ||
9a9a4f10 | 423 | static void test_in_utc_timezone(void) { |
f1880a4b ZJS |
424 | log_info("/* %s */", __func__); |
425 | ||
9a9a4f10 LP |
426 | assert_se(setenv("TZ", ":UTC", 1) >= 0); |
427 | assert_se(in_utc_timezone()); | |
428 | assert_se(streq(tzname[0], "UTC")); | |
429 | assert_se(streq(tzname[1], "UTC")); | |
430 | assert_se(timezone == 0); | |
431 | assert_se(daylight == 0); | |
432 | ||
433 | assert_se(setenv("TZ", "Europe/Berlin", 1) >= 0); | |
434 | assert_se(!in_utc_timezone()); | |
435 | assert_se(streq(tzname[0], "CET")); | |
436 | assert_se(streq(tzname[1], "CEST")); | |
437 | ||
438 | assert_se(unsetenv("TZ") >= 0); | |
439 | } | |
440 | ||
cb0dac05 | 441 | int main(int argc, char *argv[]) { |
2d60169d LP |
442 | uintmax_t x; |
443 | ||
c4834ffa LP |
444 | log_info("realtime=" USEC_FMT "\n" |
445 | "monotonic=" USEC_FMT "\n" | |
446 | "boottime=" USEC_FMT "\n", | |
447 | now(CLOCK_REALTIME), | |
448 | now(CLOCK_MONOTONIC), | |
449 | now(clock_boottime_or_monotonic())); | |
450 | ||
cb0dac05 | 451 | test_parse_sec(); |
0004f698 | 452 | test_parse_sec_fix_0(); |
519cffec | 453 | test_parse_time(); |
cb0dac05 | 454 | test_parse_nsec(); |
2fa4092c LP |
455 | test_format_timespan(1); |
456 | test_format_timespan(USEC_PER_MSEC); | |
457 | test_format_timespan(USEC_PER_SEC); | |
6accc7a2 RC |
458 | test_timezone_is_valid(); |
459 | test_get_timezones(); | |
53f555b6 | 460 | test_usec_add(); |
54d8ef14 LP |
461 | test_usec_sub_signed(); |
462 | test_usec_sub_unsigned(); | |
21b3a0fc | 463 | test_format_timestamp(); |
1bb4b028 | 464 | test_format_timestamp_utc(); |
d68c645b | 465 | test_deserialize_dual_timestamp(); |
1007ec60 | 466 | test_usec_shift_clock(); |
9a9a4f10 | 467 | test_in_utc_timezone(); |
6accc7a2 | 468 | |
2d60169d LP |
469 | /* Ensure time_t is signed */ |
470 | assert_cc((time_t) -1 < (time_t) 1); | |
471 | ||
472 | /* Ensure TIME_T_MAX works correctly */ | |
473 | x = (uintmax_t) TIME_T_MAX; | |
313cefa1 | 474 | x++; |
2d60169d LP |
475 | assert((time_t) x < 0); |
476 | ||
cb0dac05 LP |
477 | return 0; |
478 | } |