]>
Commit | Line | Data |
---|---|---|
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 | ||
6accc7a2 | 20 | #include "strv.h" |
cf0fbc49 | 21 | #include "time-util.h" |
21b3a0fc | 22 | #include "random-util.h" |
cb0dac05 LP |
23 | |
24 | static void test_parse_sec(void) { | |
25 | usec_t u; | |
26 | ||
27 | assert_se(parse_sec("5s", &u) >= 0); | |
28 | assert_se(u == 5 * USEC_PER_SEC); | |
29 | assert_se(parse_sec("5s500ms", &u) >= 0); | |
30 | assert_se(u == 5 * USEC_PER_SEC + 500 * USEC_PER_MSEC); | |
31 | assert_se(parse_sec(" 5s 500ms ", &u) >= 0); | |
32 | assert_se(u == 5 * USEC_PER_SEC + 500 * USEC_PER_MSEC); | |
33 | assert_se(parse_sec(" 5.5s ", &u) >= 0); | |
34 | assert_se(u == 5 * USEC_PER_SEC + 500 * USEC_PER_MSEC); | |
35 | assert_se(parse_sec(" 5.5s 0.5ms ", &u) >= 0); | |
36 | assert_se(u == 5 * USEC_PER_SEC + 500 * USEC_PER_MSEC + 500); | |
37 | assert_se(parse_sec(" .22s ", &u) >= 0); | |
38 | assert_se(u == 220 * USEC_PER_MSEC); | |
39 | assert_se(parse_sec(" .50y ", &u) >= 0); | |
40 | assert_se(u == USEC_PER_YEAR / 2); | |
41 | assert_se(parse_sec("2.5", &u) >= 0); | |
42 | assert_se(u == 2500 * USEC_PER_MSEC); | |
43 | assert_se(parse_sec(".7", &u) >= 0); | |
44 | assert_se(u == 700 * USEC_PER_MSEC); | |
b1d6dcf5 ZJS |
45 | assert_se(parse_sec("infinity", &u) >= 0); |
46 | assert_se(u == USEC_INFINITY); | |
47 | assert_se(parse_sec(" infinity ", &u) >= 0); | |
48 | assert_se(u == USEC_INFINITY); | |
cb0dac05 LP |
49 | |
50 | assert_se(parse_sec(" xyz ", &u) < 0); | |
51 | assert_se(parse_sec("", &u) < 0); | |
52 | assert_se(parse_sec(" . ", &u) < 0); | |
53 | assert_se(parse_sec(" 5. ", &u) < 0); | |
54 | assert_se(parse_sec(".s ", &u) < 0); | |
b1d6dcf5 ZJS |
55 | assert_se(parse_sec(" infinity .7", &u) < 0); |
56 | assert_se(parse_sec(".3 infinity", &u) < 0); | |
cb0dac05 LP |
57 | } |
58 | ||
519cffec LP |
59 | static void test_parse_time(void) { |
60 | usec_t u; | |
61 | ||
62 | assert_se(parse_time("5", &u, 1) >= 0); | |
63 | assert_se(u == 5); | |
64 | ||
65 | assert_se(parse_time("5", &u, USEC_PER_MSEC) >= 0); | |
66 | assert_se(u == 5 * USEC_PER_MSEC); | |
67 | ||
68 | assert_se(parse_time("5", &u, USEC_PER_SEC) >= 0); | |
69 | assert_se(u == 5 * USEC_PER_SEC); | |
70 | ||
71 | assert_se(parse_time("5s", &u, 1) >= 0); | |
72 | assert_se(u == 5 * USEC_PER_SEC); | |
73 | ||
74 | assert_se(parse_time("5s", &u, USEC_PER_SEC) >= 0); | |
75 | assert_se(u == 5 * USEC_PER_SEC); | |
76 | ||
77 | assert_se(parse_time("5s", &u, USEC_PER_MSEC) >= 0); | |
78 | assert_se(u == 5 * USEC_PER_SEC); | |
79 | } | |
80 | ||
cb0dac05 LP |
81 | static void test_parse_nsec(void) { |
82 | nsec_t u; | |
83 | ||
84 | assert_se(parse_nsec("5s", &u) >= 0); | |
85 | assert_se(u == 5 * NSEC_PER_SEC); | |
86 | assert_se(parse_nsec("5s500ms", &u) >= 0); | |
87 | assert_se(u == 5 * NSEC_PER_SEC + 500 * NSEC_PER_MSEC); | |
88 | assert_se(parse_nsec(" 5s 500ms ", &u) >= 0); | |
89 | assert_se(u == 5 * NSEC_PER_SEC + 500 * NSEC_PER_MSEC); | |
90 | assert_se(parse_nsec(" 5.5s ", &u) >= 0); | |
91 | assert_se(u == 5 * NSEC_PER_SEC + 500 * NSEC_PER_MSEC); | |
92 | assert_se(parse_nsec(" 5.5s 0.5ms ", &u) >= 0); | |
93 | assert_se(u == 5 * NSEC_PER_SEC + 500 * NSEC_PER_MSEC + 500 * NSEC_PER_USEC); | |
94 | assert_se(parse_nsec(" .22s ", &u) >= 0); | |
95 | assert_se(u == 220 * NSEC_PER_MSEC); | |
96 | assert_se(parse_nsec(" .50y ", &u) >= 0); | |
97 | assert_se(u == NSEC_PER_YEAR / 2); | |
98 | assert_se(parse_nsec("2.5", &u) >= 0); | |
99 | assert_se(u == 2); | |
100 | assert_se(parse_nsec(".7", &u) >= 0); | |
101 | assert_se(u == 0); | |
fdd30a15 DM |
102 | assert_se(parse_nsec("infinity", &u) >= 0); |
103 | assert_se(u == NSEC_INFINITY); | |
104 | assert_se(parse_nsec(" infinity ", &u) >= 0); | |
105 | assert_se(u == NSEC_INFINITY); | |
cb0dac05 LP |
106 | |
107 | assert_se(parse_nsec(" xyz ", &u) < 0); | |
108 | assert_se(parse_nsec("", &u) < 0); | |
109 | assert_se(parse_nsec(" . ", &u) < 0); | |
110 | assert_se(parse_nsec(" 5. ", &u) < 0); | |
111 | assert_se(parse_nsec(".s ", &u) < 0); | |
fdd30a15 DM |
112 | assert_se(parse_nsec(" infinity .7", &u) < 0); |
113 | assert_se(parse_nsec(".3 infinity", &u) < 0); | |
cb0dac05 LP |
114 | } |
115 | ||
2fa4092c LP |
116 | static void test_format_timespan_one(usec_t x, usec_t accuracy) { |
117 | char *r; | |
118 | char l[FORMAT_TIMESPAN_MAX]; | |
119 | usec_t y; | |
120 | ||
de0671ee | 121 | log_info(USEC_FMT" (at accuracy "USEC_FMT")", x, accuracy); |
2fa4092c LP |
122 | |
123 | r = format_timespan(l, sizeof(l), x, accuracy); | |
124 | assert_se(r); | |
125 | ||
126 | log_info(" = <%s>", l); | |
127 | ||
128 | assert_se(parse_sec(l, &y) >= 0); | |
129 | ||
de0671ee | 130 | log_info(" = "USEC_FMT, y); |
2fa4092c LP |
131 | |
132 | if (accuracy <= 0) | |
133 | accuracy = 1; | |
134 | ||
135 | assert_se(x / accuracy == y / accuracy); | |
136 | } | |
137 | ||
138 | static void test_format_timespan(usec_t accuracy) { | |
139 | test_format_timespan_one(0, accuracy); | |
140 | test_format_timespan_one(1, accuracy); | |
141 | test_format_timespan_one(1*USEC_PER_SEC, accuracy); | |
142 | test_format_timespan_one(999*USEC_PER_MSEC, accuracy); | |
143 | test_format_timespan_one(1234567, accuracy); | |
144 | test_format_timespan_one(12, accuracy); | |
145 | test_format_timespan_one(123, accuracy); | |
146 | test_format_timespan_one(1234, accuracy); | |
147 | test_format_timespan_one(12345, accuracy); | |
148 | test_format_timespan_one(123456, accuracy); | |
149 | test_format_timespan_one(1234567, accuracy); | |
150 | test_format_timespan_one(12345678, accuracy); | |
151 | test_format_timespan_one(1200000, accuracy); | |
152 | test_format_timespan_one(1230000, accuracy); | |
153 | test_format_timespan_one(1230000, accuracy); | |
154 | test_format_timespan_one(1234000, accuracy); | |
155 | test_format_timespan_one(1234500, accuracy); | |
156 | test_format_timespan_one(1234560, accuracy); | |
157 | test_format_timespan_one(1234567, accuracy); | |
158 | test_format_timespan_one(986087, accuracy); | |
159 | test_format_timespan_one(500 * USEC_PER_MSEC, accuracy); | |
160 | test_format_timespan_one(9*USEC_PER_YEAR/5 - 23, accuracy); | |
b1d6dcf5 | 161 | test_format_timespan_one(USEC_INFINITY, accuracy); |
2fa4092c LP |
162 | } |
163 | ||
6accc7a2 RC |
164 | static void test_timezone_is_valid(void) { |
165 | assert_se(timezone_is_valid("Europe/Berlin")); | |
166 | assert_se(timezone_is_valid("Australia/Sydney")); | |
167 | assert_se(!timezone_is_valid("Europe/Do not exist")); | |
168 | } | |
169 | ||
170 | static void test_get_timezones(void) { | |
171 | _cleanup_strv_free_ char **zones = NULL; | |
172 | int r; | |
173 | char **zone; | |
174 | ||
175 | r = get_timezones(&zones); | |
176 | assert_se(r == 0); | |
177 | ||
53f555b6 | 178 | STRV_FOREACH(zone, zones) |
6accc7a2 | 179 | assert_se(timezone_is_valid(*zone)); |
53f555b6 LP |
180 | } |
181 | ||
182 | static void test_usec_add(void) { | |
183 | assert_se(usec_add(0, 0) == 0); | |
184 | assert_se(usec_add(1, 4) == 5); | |
185 | assert_se(usec_add(USEC_INFINITY, 5) == USEC_INFINITY); | |
186 | assert_se(usec_add(5, USEC_INFINITY) == USEC_INFINITY); | |
187 | assert_se(usec_add(USEC_INFINITY-5, 2) == USEC_INFINITY-3); | |
188 | assert_se(usec_add(USEC_INFINITY-2, 2) == USEC_INFINITY); | |
189 | assert_se(usec_add(USEC_INFINITY-1, 2) == USEC_INFINITY); | |
190 | assert_se(usec_add(USEC_INFINITY, 2) == USEC_INFINITY); | |
6accc7a2 RC |
191 | } |
192 | ||
04a1d84c LP |
193 | static void test_usec_sub(void) { |
194 | assert_se(usec_sub(0, 0) == 0); | |
195 | assert_se(usec_sub(4, 1) == 3); | |
196 | assert_se(usec_sub(4, 4) == 0); | |
197 | assert_se(usec_sub(4, 5) == 0); | |
198 | assert_se(usec_sub(USEC_INFINITY-3, -3) == USEC_INFINITY); | |
199 | assert_se(usec_sub(USEC_INFINITY-3, -3) == USEC_INFINITY); | |
200 | assert_se(usec_sub(USEC_INFINITY-3, -4) == USEC_INFINITY); | |
201 | assert_se(usec_sub(USEC_INFINITY-3, -5) == USEC_INFINITY); | |
202 | assert_se(usec_sub(USEC_INFINITY, 5) == USEC_INFINITY); | |
203 | } | |
204 | ||
21b3a0fc LP |
205 | static void test_format_timestamp(void) { |
206 | unsigned i; | |
207 | ||
208 | for (i = 0; i < 100; i++) { | |
209 | char buf[MAX(FORMAT_TIMESTAMP_MAX, FORMAT_TIMESPAN_MAX)]; | |
210 | usec_t x, y; | |
211 | ||
212 | random_bytes(&x, sizeof(x)); | |
213 | x = x % (2147483600 * USEC_PER_SEC) + 1; | |
214 | ||
215 | assert_se(format_timestamp(buf, sizeof(buf), x)); | |
216 | log_info("%s", buf); | |
217 | assert_se(parse_timestamp(buf, &y) >= 0); | |
218 | assert_se(x / USEC_PER_SEC == y / USEC_PER_SEC); | |
219 | ||
220 | assert_se(format_timestamp_utc(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_us(buf, sizeof(buf), x)); | |
226 | log_info("%s", buf); | |
227 | assert_se(parse_timestamp(buf, &y) >= 0); | |
228 | assert_se(x == y); | |
229 | ||
230 | assert_se(format_timestamp_us_utc(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_relative(buf, sizeof(buf), x)); | |
236 | log_info("%s", buf); | |
237 | assert_se(parse_timestamp(buf, &y) >= 0); | |
238 | ||
239 | /* The two calls above will run with a slightly different local time. Make sure we are in the same | |
240 | * range however, but give enough leeway that this is unlikely to explode. And of course, | |
241 | * format_timestamp_relative() scales the accuracy with the distance from the current time up to one | |
242 | * month, cover for that too. */ | |
243 | assert_se(y > x ? y - x : x - y <= USEC_PER_MONTH + USEC_PER_DAY); | |
244 | } | |
245 | } | |
246 | ||
cb0dac05 | 247 | int main(int argc, char *argv[]) { |
2d60169d LP |
248 | uintmax_t x; |
249 | ||
cb0dac05 | 250 | test_parse_sec(); |
519cffec | 251 | test_parse_time(); |
cb0dac05 | 252 | test_parse_nsec(); |
2fa4092c LP |
253 | test_format_timespan(1); |
254 | test_format_timespan(USEC_PER_MSEC); | |
255 | test_format_timespan(USEC_PER_SEC); | |
6accc7a2 RC |
256 | test_timezone_is_valid(); |
257 | test_get_timezones(); | |
53f555b6 | 258 | test_usec_add(); |
04a1d84c | 259 | test_usec_sub(); |
21b3a0fc | 260 | test_format_timestamp(); |
6accc7a2 | 261 | |
2d60169d LP |
262 | /* Ensure time_t is signed */ |
263 | assert_cc((time_t) -1 < (time_t) 1); | |
264 | ||
265 | /* Ensure TIME_T_MAX works correctly */ | |
266 | x = (uintmax_t) TIME_T_MAX; | |
313cefa1 | 267 | x++; |
2d60169d LP |
268 | assert((time_t) x < 0); |
269 | ||
cb0dac05 LP |
270 | return 0; |
271 | } |