]>
Commit | Line | Data |
---|---|---|
539ad707 TA |
1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ |
2 | ||
3 | /*** | |
4 | This file is part of systemd. | |
5 | ||
6 | Copyright 2010 Lennart Poettering | |
7 | Copyright 2013 Thomas H.P. Andersen | |
8 | ||
9 | systemd is free software; you can redistribute it and/or modify it | |
10 | under the terms of the GNU Lesser General Public License as published by | |
11 | the Free Software Foundation; either version 2.1 of the License, or | |
12 | (at your option) any later version. | |
13 | ||
14 | systemd is distributed in the hope that it will be useful, but | |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
17 | Lesser General Public License for more details. | |
18 | ||
19 | You should have received a copy of the GNU Lesser General Public License | |
20 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
21 | ***/ | |
22 | ||
23 | #include <string.h> | |
dbd73f9e TA |
24 | #include <unistd.h> |
25 | #include <fcntl.h> | |
d6dd604b | 26 | #include <locale.h> |
2a371001 | 27 | #include <errno.h> |
927be00c | 28 | #include <signal.h> |
8e211000 | 29 | #include <math.h> |
8852362b | 30 | #include <sys/wait.h> |
539ad707 TA |
31 | |
32 | #include "util.h" | |
927be00c | 33 | #include "mkdir.h" |
893fa014 | 34 | #include "strv.h" |
65b3903f ZJS |
35 | #include "def.h" |
36 | #include "fileio.h" | |
9480794b | 37 | #include "conf-parser.h" |
539ad707 TA |
38 | |
39 | static void test_streq_ptr(void) { | |
8354c34e TA |
40 | assert_se(streq_ptr(NULL, NULL)); |
41 | assert_se(!streq_ptr("abc", "cdef")); | |
539ad707 TA |
42 | } |
43 | ||
625e870b DH |
44 | static void test_align_power2(void) { |
45 | unsigned long i, p2; | |
46 | ||
47 | assert_se(ALIGN_POWER2(0) == 0); | |
48 | assert_se(ALIGN_POWER2(1) == 1); | |
49 | assert_se(ALIGN_POWER2(2) == 2); | |
50 | assert_se(ALIGN_POWER2(3) == 4); | |
51 | assert_se(ALIGN_POWER2(12) == 16); | |
52 | ||
53 | assert_se(ALIGN_POWER2(ULONG_MAX) == 0); | |
54 | assert_se(ALIGN_POWER2(ULONG_MAX - 1) == 0); | |
55 | assert_se(ALIGN_POWER2(ULONG_MAX - 1024) == 0); | |
56 | assert_se(ALIGN_POWER2(ULONG_MAX / 2) == ULONG_MAX / 2 + 1); | |
57 | assert_se(ALIGN_POWER2(ULONG_MAX + 1) == 0); | |
58 | ||
59 | for (i = 1; i < 131071; ++i) { | |
60 | for (p2 = 1; p2 < i; p2 <<= 1) | |
61 | /* empty */ ; | |
62 | ||
63 | assert_se(ALIGN_POWER2(i) == p2); | |
64 | } | |
65 | ||
66 | for (i = ULONG_MAX - 1024; i < ULONG_MAX; ++i) { | |
67 | for (p2 = 1; p2 && p2 < i; p2 <<= 1) | |
68 | /* empty */ ; | |
69 | ||
70 | assert_se(ALIGN_POWER2(i) == p2); | |
71 | } | |
72 | } | |
73 | ||
7242d742 DH |
74 | static void test_max(void) { |
75 | static const struct { | |
76 | int a; | |
77 | int b[CONST_MAX(10, 100)]; | |
78 | } val1 = { | |
79 | .a = CONST_MAX(10, 100), | |
80 | }; | |
81 | int d = 0; | |
82 | ||
83 | assert_cc(sizeof(val1.b) == sizeof(int) * 100); | |
84 | ||
85 | /* CONST_MAX returns (void) instead of a value if the passed arguments | |
86 | * are not of the same type or not constant expressions. */ | |
87 | assert_cc(__builtin_types_compatible_p(typeof(CONST_MAX(1, 10)), int)); | |
7242d742 DH |
88 | assert_cc(__builtin_types_compatible_p(typeof(CONST_MAX(1, 1U)), void)); |
89 | ||
90 | assert_se(val1.a == 100); | |
91 | assert_se(MAX(++d, 0) == 1); | |
92 | assert_se(d == 1); | |
40a1eebd DH |
93 | |
94 | assert_cc(MAXSIZE(char[3], uint16_t) == 3); | |
95 | assert_cc(MAXSIZE(char[3], uint32_t) == 4); | |
96 | assert_cc(MAXSIZE(char, long) == sizeof(long)); | |
667a0377 DH |
97 | |
98 | assert_se(MAX(-5, 5) == 5); | |
99 | assert_se(MAX(5, 5) == 5); | |
100 | assert_se(MAX(MAX(1, MAX(2, MAX(3, 4))), 5) == 5); | |
101 | assert_se(MAX(MAX(1, MAX(2, MAX(3, 2))), 1) == 3); | |
102 | assert_se(MAX(MIN(1, MIN(2, MIN(3, 4))), 5) == 5); | |
103 | assert_se(MAX(MAX(1, MIN(2, MIN(3, 2))), 1) == 2); | |
104 | assert_se(LESS_BY(8, 4) == 4); | |
105 | assert_se(LESS_BY(8, 8) == 0); | |
106 | assert_se(LESS_BY(4, 8) == 0); | |
107 | assert_se(LESS_BY(16, LESS_BY(8, 4)) == 12); | |
108 | assert_se(LESS_BY(4, LESS_BY(8, 4)) == 0); | |
109 | assert_se(CLAMP(-5, 0, 1) == 0); | |
110 | assert_se(CLAMP(5, 0, 1) == 1); | |
111 | assert_se(CLAMP(5, -10, 1) == 1); | |
112 | assert_se(CLAMP(5, -10, 10) == 5); | |
113 | assert_se(CLAMP(CLAMP(0, -10, 10), CLAMP(-5, 10, 20), CLAMP(100, -5, 20)) == 10); | |
7242d742 DH |
114 | } |
115 | ||
fb835651 DH |
116 | static void test_container_of(void) { |
117 | struct mytype { | |
118 | uint8_t pad1[3]; | |
119 | uint64_t v1; | |
120 | uint8_t pad2[2]; | |
121 | uint32_t v2; | |
122 | } _packed_ myval = { }; | |
123 | ||
124 | assert_cc(sizeof(myval) == 17); | |
125 | assert_se(container_of(&myval.v1, struct mytype, v1) == &myval); | |
126 | assert_se(container_of(&myval.v2, struct mytype, v2) == &myval); | |
127 | assert_se(container_of(&container_of(&myval.v2, | |
128 | struct mytype, | |
129 | v2)->v1, | |
130 | struct mytype, | |
131 | v1) == &myval); | |
132 | } | |
133 | ||
95d78c7e DH |
134 | static void test_alloca(void) { |
135 | static const uint8_t zero[997] = { }; | |
136 | char *t; | |
137 | ||
138 | t = alloca_align(17, 512); | |
139 | assert_se(!((uintptr_t)t & 0xff)); | |
140 | memzero(t, 17); | |
141 | ||
142 | t = alloca0_align(997, 1024); | |
143 | assert_se(!((uintptr_t)t & 0x1ff)); | |
144 | assert_se(!memcmp(t, zero, 997)); | |
145 | } | |
146 | ||
539ad707 | 147 | static void test_first_word(void) { |
8354c34e TA |
148 | assert_se(first_word("Hello", "")); |
149 | assert_se(first_word("Hello", "Hello")); | |
150 | assert_se(first_word("Hello world", "Hello")); | |
151 | assert_se(first_word("Hello\tworld", "Hello")); | |
152 | assert_se(first_word("Hello\nworld", "Hello")); | |
153 | assert_se(first_word("Hello\rworld", "Hello")); | |
154 | assert_se(first_word("Hello ", "Hello")); | |
155 | ||
156 | assert_se(!first_word("Hello", "Hellooo")); | |
157 | assert_se(!first_word("Hello", "xxxxx")); | |
158 | assert_se(!first_word("Hellooo", "Hello")); | |
159 | } | |
160 | ||
dbd73f9e TA |
161 | static void test_close_many(void) { |
162 | int fds[3]; | |
163 | char name0[] = "/tmp/test-close-many.XXXXXX"; | |
164 | char name1[] = "/tmp/test-close-many.XXXXXX"; | |
165 | char name2[] = "/tmp/test-close-many.XXXXXX"; | |
166 | ||
2d5bdf5b LP |
167 | fds[0] = mkostemp_safe(name0, O_RDWR|O_CLOEXEC); |
168 | fds[1] = mkostemp_safe(name1, O_RDWR|O_CLOEXEC); | |
169 | fds[2] = mkostemp_safe(name2, O_RDWR|O_CLOEXEC); | |
dbd73f9e TA |
170 | |
171 | close_many(fds, 2); | |
172 | ||
173 | assert_se(fcntl(fds[0], F_GETFD) == -1); | |
174 | assert_se(fcntl(fds[1], F_GETFD) == -1); | |
175 | assert_se(fcntl(fds[2], F_GETFD) >= 0); | |
176 | ||
03e334a1 | 177 | safe_close(fds[2]); |
dbd73f9e TA |
178 | |
179 | unlink(name0); | |
180 | unlink(name1); | |
181 | unlink(name2); | |
182 | } | |
183 | ||
8354c34e TA |
184 | static void test_parse_boolean(void) { |
185 | assert_se(parse_boolean("1") == 1); | |
186 | assert_se(parse_boolean("y") == 1); | |
187 | assert_se(parse_boolean("Y") == 1); | |
188 | assert_se(parse_boolean("yes") == 1); | |
189 | assert_se(parse_boolean("YES") == 1); | |
190 | assert_se(parse_boolean("true") == 1); | |
191 | assert_se(parse_boolean("TRUE") == 1); | |
192 | assert_se(parse_boolean("on") == 1); | |
193 | assert_se(parse_boolean("ON") == 1); | |
194 | ||
195 | assert_se(parse_boolean("0") == 0); | |
196 | assert_se(parse_boolean("n") == 0); | |
197 | assert_se(parse_boolean("N") == 0); | |
198 | assert_se(parse_boolean("no") == 0); | |
199 | assert_se(parse_boolean("NO") == 0); | |
200 | assert_se(parse_boolean("false") == 0); | |
201 | assert_se(parse_boolean("FALSE") == 0); | |
202 | assert_se(parse_boolean("off") == 0); | |
203 | assert_se(parse_boolean("OFF") == 0); | |
204 | ||
205 | assert_se(parse_boolean("garbage") < 0); | |
206 | assert_se(parse_boolean("") < 0); | |
0f625d0b | 207 | assert_se(parse_boolean("full") < 0); |
539ad707 TA |
208 | } |
209 | ||
8d99e5f5 TA |
210 | static void test_parse_pid(void) { |
211 | int r; | |
212 | pid_t pid; | |
213 | ||
214 | r = parse_pid("100", &pid); | |
215 | assert_se(r == 0); | |
216 | assert_se(pid == 100); | |
217 | ||
218 | r = parse_pid("0x7FFFFFFF", &pid); | |
219 | assert_se(r == 0); | |
220 | assert_se(pid == 2147483647); | |
221 | ||
222 | pid = 65; /* pid is left unchanged on ERANGE. Set to known arbitrary value. */ | |
223 | r = parse_pid("0", &pid); | |
224 | assert_se(r == -ERANGE); | |
225 | assert_se(pid == 65); | |
226 | ||
227 | pid = 65; /* pid is left unchanged on ERANGE. Set to known arbitrary value. */ | |
228 | r = parse_pid("-100", &pid); | |
229 | assert_se(r == -ERANGE); | |
230 | assert_se(pid == 65); | |
231 | ||
232 | pid = 65; /* pid is left unchanged on ERANGE. Set to known arbitrary value. */ | |
233 | r = parse_pid("0xFFFFFFFFFFFFFFFFF", &pid); | |
bdf7026e | 234 | assert_se(r == -ERANGE); |
8d99e5f5 TA |
235 | assert_se(pid == 65); |
236 | } | |
237 | ||
238 | static void test_parse_uid(void) { | |
239 | int r; | |
240 | uid_t uid; | |
241 | ||
242 | r = parse_uid("100", &uid); | |
243 | assert_se(r == 0); | |
244 | assert_se(uid == 100); | |
245 | } | |
246 | ||
247 | static void test_safe_atolli(void) { | |
248 | int r; | |
249 | long long l; | |
250 | ||
251 | r = safe_atolli("12345", &l); | |
252 | assert_se(r == 0); | |
253 | assert_se(l == 12345); | |
254 | ||
255 | r = safe_atolli("junk", &l); | |
256 | assert_se(r == -EINVAL); | |
257 | } | |
258 | ||
259 | static void test_safe_atod(void) { | |
260 | int r; | |
261 | double d; | |
d6dd604b LP |
262 | char *e; |
263 | ||
264 | r = safe_atod("junk", &d); | |
265 | assert_se(r == -EINVAL); | |
8d99e5f5 TA |
266 | |
267 | r = safe_atod("0.2244", &d); | |
268 | assert_se(r == 0); | |
8e211000 | 269 | assert_se(fabs(d - 0.2244) < 0.000001); |
8d99e5f5 | 270 | |
d6dd604b | 271 | r = safe_atod("0,5", &d); |
8d99e5f5 | 272 | assert_se(r == -EINVAL); |
d6dd604b LP |
273 | |
274 | errno = 0; | |
275 | strtod("0,5", &e); | |
276 | assert_se(*e == ','); | |
277 | ||
278 | /* Check if this really is locale independent */ | |
926446f4 | 279 | if (setlocale(LC_NUMERIC, "de_DE.utf8")) { |
d6dd604b | 280 | |
926446f4 DH |
281 | r = safe_atod("0.2244", &d); |
282 | assert_se(r == 0); | |
283 | assert_se(fabs(d - 0.2244) < 0.000001); | |
d6dd604b | 284 | |
926446f4 DH |
285 | r = safe_atod("0,5", &d); |
286 | assert_se(r == -EINVAL); | |
d6dd604b | 287 | |
926446f4 DH |
288 | errno = 0; |
289 | assert_se(fabs(strtod("0,5", &e) - 0.5) < 0.00001); | |
290 | } | |
d6dd604b LP |
291 | |
292 | /* And check again, reset */ | |
926446f4 | 293 | assert_se(setlocale(LC_NUMERIC, "C")); |
d6dd604b LP |
294 | |
295 | r = safe_atod("0.2244", &d); | |
296 | assert_se(r == 0); | |
8e211000 | 297 | assert_se(fabs(d - 0.2244) < 0.000001); |
d6dd604b LP |
298 | |
299 | r = safe_atod("0,5", &d); | |
300 | assert_se(r == -EINVAL); | |
301 | ||
302 | errno = 0; | |
303 | strtod("0,5", &e); | |
304 | assert_se(*e == ','); | |
8d99e5f5 TA |
305 | } |
306 | ||
dbd73f9e | 307 | static void test_strappend(void) { |
998b087f | 308 | _cleanup_free_ char *t1, *t2, *t3, *t4; |
dbd73f9e | 309 | |
998b087f TA |
310 | t1 = strappend(NULL, NULL); |
311 | assert_se(streq(t1, "")); | |
dbd73f9e | 312 | |
998b087f TA |
313 | t2 = strappend(NULL, "suf"); |
314 | assert_se(streq(t2, "suf")); | |
dbd73f9e | 315 | |
998b087f TA |
316 | t3 = strappend("pre", NULL); |
317 | assert_se(streq(t3, "pre")); | |
dbd73f9e | 318 | |
998b087f TA |
319 | t4 = strappend("pre", "suf"); |
320 | assert_se(streq(t4, "presuf")); | |
dbd73f9e TA |
321 | } |
322 | ||
1ef04f0b | 323 | static void test_strstrip(void) { |
998b087f TA |
324 | char *r; |
325 | char input[] = " hello, waldo. "; | |
1ef04f0b | 326 | |
998b087f TA |
327 | r = strstrip(input); |
328 | assert_se(streq(r, "hello, waldo.")); | |
1ef04f0b TA |
329 | } |
330 | ||
331 | static void test_delete_chars(void) { | |
998b087f TA |
332 | char *r; |
333 | char input[] = " hello, waldo. abc"; | |
1ef04f0b | 334 | |
998b087f TA |
335 | r = delete_chars(input, WHITESPACE); |
336 | assert_se(streq(r, "hello,waldo.abc")); | |
1ef04f0b TA |
337 | } |
338 | ||
339 | static void test_in_charset(void) { | |
998b087f TA |
340 | assert_se(in_charset("dddaaabbbcccc", "abcd")); |
341 | assert_se(!in_charset("dddaaabbbcccc", "abc f")); | |
1ef04f0b TA |
342 | } |
343 | ||
44f4c86c DB |
344 | static void test_hexchar(void) { |
345 | assert_se(hexchar(0xa) == 'a'); | |
346 | assert_se(hexchar(0x0) == '0'); | |
347 | } | |
348 | ||
349 | static void test_unhexchar(void) { | |
350 | assert_se(unhexchar('a') == 0xA); | |
351 | assert_se(unhexchar('A') == 0xA); | |
352 | assert_se(unhexchar('0') == 0x0); | |
353 | } | |
354 | ||
355 | static void test_octchar(void) { | |
356 | assert_se(octchar(00) == '0'); | |
357 | assert_se(octchar(07) == '7'); | |
358 | } | |
359 | ||
360 | static void test_unoctchar(void) { | |
361 | assert_se(unoctchar('0') == 00); | |
362 | assert_se(unoctchar('7') == 07); | |
363 | } | |
364 | ||
365 | static void test_decchar(void) { | |
366 | assert_se(decchar(0) == '0'); | |
367 | assert_se(decchar(9) == '9'); | |
368 | } | |
369 | ||
370 | static void test_undecchar(void) { | |
371 | assert_se(undecchar('0') == 0); | |
372 | assert_se(undecchar('9') == 9); | |
373 | } | |
374 | ||
b4ecc959 TA |
375 | static void test_cescape(void) { |
376 | _cleanup_free_ char *escaped; | |
e0a33e7b LP |
377 | |
378 | assert_se(escaped = cescape("abc\\\"\b\f\n\r\t\v\a\003\177\234\313")); | |
927be00c | 379 | assert_se(streq(escaped, "abc\\\\\\\"\\b\\f\\n\\r\\t\\v\\a\\003\\177\\234\\313")); |
b4ecc959 TA |
380 | } |
381 | ||
382 | static void test_cunescape(void) { | |
383 | _cleanup_free_ char *unescaped; | |
e0a33e7b LP |
384 | |
385 | assert_se(unescaped = cunescape("abc\\\\\\\"\\b\\f\\a\\n\\r\\t\\v\\003\\177\\234\\313\\000\\x00")); | |
386 | assert_se(streq(unescaped, "abc\\\"\b\f\a\n\r\t\v\003\177\234\313\\000\\x00")); | |
b4ecc959 TA |
387 | } |
388 | ||
1ef04f0b | 389 | static void test_foreach_word(void) { |
a2a5291b | 390 | const char *word, *state; |
1ef04f0b TA |
391 | size_t l; |
392 | int i = 0; | |
393 | const char test[] = "test abc d\te f "; | |
394 | const char * const expected[] = { | |
395 | "test", | |
396 | "abc", | |
397 | "d", | |
398 | "e", | |
399 | "f", | |
400 | "", | |
401 | NULL | |
402 | }; | |
403 | ||
a2a5291b ZJS |
404 | FOREACH_WORD(word, l, test, state) |
405 | assert_se(strneq(expected[i++], word, l)); | |
1ef04f0b TA |
406 | } |
407 | ||
539ad707 | 408 | static void test_foreach_word_quoted(void) { |
a2a5291b | 409 | const char *word, *state; |
539ad707 | 410 | size_t l; |
1ef04f0b TA |
411 | int i = 0; |
412 | const char test[] = "test a b c 'd' e '' '' hhh '' '' \"a b c\""; | |
413 | const char * const expected[] = { | |
414 | "test", | |
415 | "a", | |
416 | "b", | |
417 | "c", | |
418 | "d", | |
419 | "e", | |
420 | "", | |
421 | "", | |
422 | "hhh", | |
423 | "", | |
424 | "", | |
425 | "a b c", | |
426 | NULL | |
427 | }; | |
428 | ||
539ad707 | 429 | printf("<%s>\n", test); |
a2a5291b | 430 | FOREACH_WORD_QUOTED(word, l, test, state) { |
1ef04f0b | 431 | _cleanup_free_ char *t = NULL; |
539ad707 | 432 | |
a2a5291b ZJS |
433 | assert_se(t = strndup(word, l)); |
434 | assert_se(strneq(expected[i++], word, l)); | |
539ad707 | 435 | printf("<%s>\n", t); |
539ad707 | 436 | } |
bdf7026e | 437 | assert_se(isempty(state)); |
539ad707 TA |
438 | } |
439 | ||
440 | static void test_default_term_for_tty(void) { | |
441 | puts(default_term_for_tty("/dev/tty23")); | |
442 | puts(default_term_for_tty("/dev/ttyS23")); | |
443 | puts(default_term_for_tty("/dev/tty0")); | |
444 | puts(default_term_for_tty("/dev/pty0")); | |
445 | puts(default_term_for_tty("/dev/pts/0")); | |
446 | puts(default_term_for_tty("/dev/console")); | |
447 | puts(default_term_for_tty("tty23")); | |
448 | puts(default_term_for_tty("ttyS23")); | |
449 | puts(default_term_for_tty("tty0")); | |
450 | puts(default_term_for_tty("pty0")); | |
451 | puts(default_term_for_tty("pts/0")); | |
452 | puts(default_term_for_tty("console")); | |
453 | } | |
454 | ||
0d585d82 TA |
455 | static void test_memdup_multiply(void) { |
456 | int org[] = {1, 2, 3}; | |
457 | int *dup; | |
458 | ||
459 | dup = (int*)memdup_multiply(org, sizeof(int), 3); | |
460 | ||
461 | assert_se(dup); | |
462 | assert_se(dup[0] == 1); | |
463 | assert_se(dup[1] == 2); | |
464 | assert_se(dup[2] == 3); | |
465 | free(dup); | |
466 | } | |
467 | ||
aa3c5cf8 | 468 | static void test_hostname_is_valid(void) { |
bdf7026e TA |
469 | assert_se(hostname_is_valid("foobar")); |
470 | assert_se(hostname_is_valid("foobar.com")); | |
471 | assert_se(!hostname_is_valid("fööbar")); | |
472 | assert_se(!hostname_is_valid("")); | |
473 | assert_se(!hostname_is_valid(".")); | |
474 | assert_se(!hostname_is_valid("..")); | |
475 | assert_se(!hostname_is_valid("foobar.")); | |
476 | assert_se(!hostname_is_valid(".foobar")); | |
477 | assert_se(!hostname_is_valid("foo..bar")); | |
478 | assert_se(!hostname_is_valid("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")); | |
aa3c5cf8 LP |
479 | } |
480 | ||
144e51ec | 481 | static void test_u64log2(void) { |
bdf7026e TA |
482 | assert_se(u64log2(0) == 0); |
483 | assert_se(u64log2(8) == 3); | |
484 | assert_se(u64log2(9) == 3); | |
485 | assert_se(u64log2(15) == 3); | |
486 | assert_se(u64log2(16) == 4); | |
487 | assert_se(u64log2(1024*1024) == 20); | |
488 | assert_se(u64log2(1024*1024+5) == 20); | |
144e51ec CR |
489 | } |
490 | ||
49aa47c7 | 491 | static void test_get_process_comm(void) { |
143bfdaf | 492 | struct stat st; |
49aa47c7 LP |
493 | _cleanup_free_ char *a = NULL, *c = NULL, *d = NULL, *f = NULL, *i = NULL; |
494 | unsigned long long b; | |
495 | pid_t e; | |
496 | uid_t u; | |
497 | gid_t g; | |
498 | dev_t h; | |
499 | int r; | |
500 | ||
143bfdaf HHPF |
501 | if (stat("/proc/1/comm", &st) == 0) { |
502 | assert_se(get_process_comm(1, &a) >= 0); | |
503 | log_info("pid1 comm: '%s'", a); | |
504 | } else { | |
505 | log_warning("/proc/1/comm does not exist."); | |
506 | } | |
49aa47c7 LP |
507 | |
508 | assert_se(get_starttime_of_pid(1, &b) >= 0); | |
509 | log_info("pid1 starttime: '%llu'", b); | |
510 | ||
511 | assert_se(get_process_cmdline(1, 0, true, &c) >= 0); | |
512 | log_info("pid1 cmdline: '%s'", c); | |
513 | ||
514 | assert_se(get_process_cmdline(1, 8, false, &d) >= 0); | |
515 | log_info("pid1 cmdline truncated: '%s'", d); | |
516 | ||
517 | assert_se(get_parent_of_pid(1, &e) >= 0); | |
de0671ee | 518 | log_info("pid1 ppid: "PID_FMT, e); |
49aa47c7 LP |
519 | assert_se(e == 0); |
520 | ||
521 | assert_se(is_kernel_thread(1) == 0); | |
522 | ||
523 | r = get_process_exe(1, &f); | |
524 | assert_se(r >= 0 || r == -EACCES); | |
525 | log_info("pid1 exe: '%s'", strna(f)); | |
526 | ||
527 | assert_se(get_process_uid(1, &u) == 0); | |
de0671ee | 528 | log_info("pid1 uid: "UID_FMT, u); |
49aa47c7 LP |
529 | assert_se(u == 0); |
530 | ||
531 | assert_se(get_process_gid(1, &g) == 0); | |
de0671ee | 532 | log_info("pid1 gid: "GID_FMT, g); |
49aa47c7 LP |
533 | assert_se(g == 0); |
534 | ||
bdf7026e | 535 | assert_se(get_ctty_devnr(1, &h) == -ENOENT); |
49aa47c7 LP |
536 | |
537 | getenv_for_pid(1, "PATH", &i); | |
538 | log_info("pid1 $PATH: '%s'", strna(i)); | |
539 | } | |
540 | ||
2a371001 ZJS |
541 | static void test_protect_errno(void) { |
542 | errno = 12; | |
543 | { | |
544 | PROTECT_ERRNO; | |
545 | errno = 11; | |
546 | } | |
bdf7026e | 547 | assert_se(errno == 12); |
2a371001 ZJS |
548 | } |
549 | ||
5556b5fe | 550 | static void test_parse_size(void) { |
b32ff512 ZJS |
551 | off_t bytes; |
552 | ||
5556b5fe | 553 | assert_se(parse_size("111", 1024, &bytes) == 0); |
b32ff512 ZJS |
554 | assert_se(bytes == 111); |
555 | ||
9480794b ZJS |
556 | assert_se(parse_size("111.4", 1024, &bytes) == 0); |
557 | assert_se(bytes == 111); | |
558 | ||
5556b5fe | 559 | assert_se(parse_size(" 112 B", 1024, &bytes) == 0); |
b32ff512 ZJS |
560 | assert_se(bytes == 112); |
561 | ||
9480794b ZJS |
562 | assert_se(parse_size(" 112.6 B", 1024, &bytes) == 0); |
563 | assert_se(bytes == 112); | |
564 | ||
565 | assert_se(parse_size("3.5 K", 1024, &bytes) == 0); | |
566 | assert_se(bytes == 3*1024 + 512); | |
567 | ||
568 | assert_se(parse_size("3. K", 1024, &bytes) == 0); | |
569 | assert_se(bytes == 3*1024); | |
570 | ||
571 | assert_se(parse_size("3.0 K", 1024, &bytes) == 0); | |
b32ff512 ZJS |
572 | assert_se(bytes == 3*1024); |
573 | ||
840292be | 574 | assert_se(parse_size("3. 0 K", 1024, &bytes) == -EINVAL); |
b32ff512 | 575 | |
9480794b ZJS |
576 | assert_se(parse_size(" 4 M 11.5K", 1024, &bytes) == 0); |
577 | assert_se(bytes == 4*1024*1024 + 11 * 1024 + 512); | |
578 | ||
840292be ZJS |
579 | assert_se(parse_size("3B3.5G", 1024, &bytes) == -EINVAL); |
580 | ||
581 | assert_se(parse_size("3.5G3B", 1024, &bytes) == 0); | |
9480794b | 582 | assert_se(bytes == 3ULL*1024*1024*1024 + 512*1024*1024 + 3); |
b32ff512 | 583 | |
840292be ZJS |
584 | assert_se(parse_size("3.5G 4B", 1024, &bytes) == 0); |
585 | assert_se(bytes == 3ULL*1024*1024*1024 + 512*1024*1024 + 4); | |
586 | ||
587 | assert_se(parse_size("3B3G4T", 1024, &bytes) == -EINVAL); | |
588 | ||
589 | assert_se(parse_size("4T3G3B", 1024, &bytes) == 0); | |
590 | assert_se(bytes == (4ULL*1024 + 3)*1024*1024*1024 + 3); | |
591 | ||
592 | assert_se(parse_size(" 4 T 3 G 3 B", 1024, &bytes) == 0); | |
b32ff512 ZJS |
593 | assert_se(bytes == (4ULL*1024 + 3)*1024*1024*1024 + 3); |
594 | ||
5556b5fe | 595 | assert_se(parse_size("12P", 1024, &bytes) == 0); |
b32ff512 ZJS |
596 | assert_se(bytes == 12ULL * 1024*1024*1024*1024*1024); |
597 | ||
840292be ZJS |
598 | assert_se(parse_size("12P12P", 1024, &bytes) == -EINVAL); |
599 | ||
5556b5fe | 600 | assert_se(parse_size("3E 2P", 1024, &bytes) == 0); |
b32ff512 ZJS |
601 | assert_se(bytes == (3 * 1024 + 2ULL) * 1024*1024*1024*1024*1024); |
602 | ||
5556b5fe | 603 | assert_se(parse_size("12X", 1024, &bytes) == -EINVAL); |
b32ff512 | 604 | |
9480794b ZJS |
605 | assert_se(parse_size("12.5X", 1024, &bytes) == -EINVAL); |
606 | ||
607 | assert_se(parse_size("12.5e3", 1024, &bytes) == -EINVAL); | |
608 | ||
5556b5fe LP |
609 | assert_se(parse_size("1024E", 1024, &bytes) == -ERANGE); |
610 | assert_se(parse_size("-1", 1024, &bytes) == -ERANGE); | |
611 | assert_se(parse_size("-1024E", 1024, &bytes) == -ERANGE); | |
b32ff512 | 612 | |
5556b5fe | 613 | assert_se(parse_size("-1024P", 1024, &bytes) == -ERANGE); |
b32ff512 | 614 | |
5556b5fe | 615 | assert_se(parse_size("-10B 20K", 1024, &bytes) == -ERANGE); |
b32ff512 ZJS |
616 | } |
617 | ||
9480794b ZJS |
618 | static void test_config_parse_iec_off(void) { |
619 | off_t offset = 0; | |
620 | assert_se(config_parse_iec_off(NULL, "/this/file", 11, "Section", 22, "Size", 0, "4M", &offset, NULL) == 0); | |
621 | assert_se(offset == 4 * 1024 * 1024); | |
622 | ||
623 | assert_se(config_parse_iec_off(NULL, "/this/file", 11, "Section", 22, "Size", 0, "4.5M", &offset, NULL) == 0); | |
624 | } | |
625 | ||
b4ecc959 TA |
626 | static void test_strextend(void) { |
627 | _cleanup_free_ char *str = strdup("0123"); | |
628 | strextend(&str, "456", "78", "9", NULL); | |
629 | assert_se(streq(str, "0123456789")); | |
630 | } | |
631 | ||
632 | static void test_strrep(void) { | |
633 | _cleanup_free_ char *one, *three, *zero; | |
634 | one = strrep("waldo", 1); | |
635 | three = strrep("waldo", 3); | |
636 | zero = strrep("waldo", 0); | |
637 | ||
638 | assert_se(streq(one, "waldo")); | |
639 | assert_se(streq(three, "waldowaldowaldo")); | |
640 | assert_se(streq(zero, "")); | |
641 | } | |
642 | ||
d4ac85c6 LP |
643 | static void test_split_pair(void) { |
644 | _cleanup_free_ char *a = NULL, *b = NULL; | |
645 | ||
646 | assert_se(split_pair("", "", &a, &b) == -EINVAL); | |
647 | assert_se(split_pair("foo=bar", "", &a, &b) == -EINVAL); | |
648 | assert_se(split_pair("", "=", &a, &b) == -EINVAL); | |
649 | assert_se(split_pair("foo=bar", "=", &a, &b) >= 0); | |
650 | assert_se(streq(a, "foo")); | |
651 | assert_se(streq(b, "bar")); | |
652 | free(a); | |
653 | free(b); | |
654 | assert_se(split_pair("==", "==", &a, &b) >= 0); | |
655 | assert_se(streq(a, "")); | |
656 | assert_se(streq(b, "")); | |
657 | free(a); | |
658 | free(b); | |
659 | ||
660 | assert_se(split_pair("===", "==", &a, &b) >= 0); | |
661 | assert_se(streq(a, "")); | |
662 | assert_se(streq(b, "=")); | |
663 | } | |
664 | ||
22f5f628 DR |
665 | static void test_fstab_node_to_udev_node(void) { |
666 | char *n; | |
667 | ||
668 | n = fstab_node_to_udev_node("LABEL=applé/jack"); | |
669 | puts(n); | |
670 | assert_se(streq(n, "/dev/disk/by-label/applé\\x2fjack")); | |
671 | free(n); | |
672 | ||
673 | n = fstab_node_to_udev_node("PARTLABEL=pinkié pie"); | |
674 | puts(n); | |
675 | assert_se(streq(n, "/dev/disk/by-partlabel/pinkié\\x20pie")); | |
676 | free(n); | |
677 | ||
678 | n = fstab_node_to_udev_node("UUID=037b9d94-148e-4ee4-8d38-67bfe15bb535"); | |
679 | puts(n); | |
680 | assert_se(streq(n, "/dev/disk/by-uuid/037b9d94-148e-4ee4-8d38-67bfe15bb535")); | |
681 | free(n); | |
682 | ||
683 | n = fstab_node_to_udev_node("PARTUUID=037b9d94-148e-4ee4-8d38-67bfe15bb535"); | |
684 | puts(n); | |
685 | assert_se(streq(n, "/dev/disk/by-partuuid/037b9d94-148e-4ee4-8d38-67bfe15bb535")); | |
686 | free(n); | |
687 | ||
22f5f628 DR |
688 | n = fstab_node_to_udev_node("PONIES=awesome"); |
689 | puts(n); | |
690 | assert_se(streq(n, "PONIES=awesome")); | |
691 | free(n); | |
692 | ||
693 | n = fstab_node_to_udev_node("/dev/xda1"); | |
694 | puts(n); | |
695 | assert_se(streq(n, "/dev/xda1")); | |
696 | free(n); | |
697 | } | |
698 | ||
893fa014 ZJS |
699 | static void test_get_files_in_directory(void) { |
700 | _cleanup_strv_free_ char **l = NULL, **t = NULL; | |
701 | ||
702 | assert_se(get_files_in_directory("/tmp", &l) >= 0); | |
510b857f | 703 | assert_se(get_files_in_directory(".", &t) >= 0); |
893fa014 ZJS |
704 | assert_se(get_files_in_directory(".", NULL) >= 0); |
705 | } | |
706 | ||
cabb7806 LP |
707 | static void test_in_set(void) { |
708 | assert_se(IN_SET(1, 1)); | |
709 | assert_se(IN_SET(1, 1, 2, 3, 4)); | |
710 | assert_se(IN_SET(2, 1, 2, 3, 4)); | |
711 | assert_se(IN_SET(3, 1, 2, 3, 4)); | |
712 | assert_se(IN_SET(4, 1, 2, 3, 4)); | |
713 | assert_se(!IN_SET(0, 1)); | |
714 | assert_se(!IN_SET(0, 1, 2, 3, 4)); | |
715 | } | |
716 | ||
87b02843 ZJS |
717 | static void test_writing_tmpfile(void) { |
718 | char name[] = "/tmp/test-systemd_writing_tmpfile.XXXXXX"; | |
39883f62 | 719 | _cleanup_free_ char *contents = NULL; |
65b3903f ZJS |
720 | size_t size; |
721 | int fd, r; | |
65b3903f | 722 | struct iovec iov[3]; |
39883f62 | 723 | |
65b3903f ZJS |
724 | IOVEC_SET_STRING(iov[0], "abc\n"); |
725 | IOVEC_SET_STRING(iov[1], ALPHANUMERICAL "\n"); | |
726 | IOVEC_SET_STRING(iov[2], ""); | |
727 | ||
2d5bdf5b | 728 | fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC); |
87b02843 | 729 | printf("tmpfile: %s", name); |
65b3903f | 730 | |
87b02843 | 731 | r = writev(fd, iov, 3); |
bdf7026e | 732 | assert_se(r >= 0); |
65b3903f ZJS |
733 | |
734 | r = read_full_file(name, &contents, &size); | |
bdf7026e | 735 | assert_se(r == 0); |
65b3903f | 736 | printf("contents: %s", contents); |
bdf7026e | 737 | assert_se(streq(contents, "abc\n" ALPHANUMERICAL "\n")); |
40edd236 RC |
738 | |
739 | unlink(name); | |
65b3903f ZJS |
740 | } |
741 | ||
29bfbcd6 LP |
742 | static void test_hexdump(void) { |
743 | uint8_t data[146]; | |
744 | unsigned i; | |
745 | ||
746 | hexdump(stdout, NULL, 0); | |
747 | hexdump(stdout, "", 0); | |
748 | hexdump(stdout, "", 1); | |
749 | hexdump(stdout, "x", 1); | |
750 | hexdump(stdout, "x", 2); | |
751 | hexdump(stdout, "foobar", 7); | |
752 | hexdump(stdout, "f\nobar", 7); | |
753 | hexdump(stdout, "xxxxxxxxxxxxxxxxxxxxyz", 23); | |
754 | ||
755 | for (i = 0; i < ELEMENTSOF(data); i++) | |
756 | data[i] = i*2; | |
757 | ||
758 | hexdump(stdout, data, sizeof(data)); | |
759 | } | |
760 | ||
8fe90522 ZJS |
761 | static void test_log2i(void) { |
762 | assert_se(log2i(1) == 0); | |
763 | assert_se(log2i(2) == 1); | |
764 | assert_se(log2i(3) == 1); | |
765 | assert_se(log2i(4) == 2); | |
766 | assert_se(log2i(32) == 5); | |
767 | assert_se(log2i(33) == 5); | |
768 | assert_se(log2i(63) == 5); | |
769 | assert_se(log2i(INT_MAX) == sizeof(int)*8-2); | |
770 | } | |
771 | ||
c4a7b2c5 LP |
772 | static void test_foreach_string(void) { |
773 | const char * const t[] = { | |
774 | "foo", | |
775 | "bar", | |
776 | "waldo", | |
777 | NULL | |
778 | }; | |
779 | const char *x; | |
780 | unsigned i = 0; | |
781 | ||
782 | FOREACH_STRING(x, "foo", "bar", "waldo") | |
783 | assert_se(streq_ptr(t[i++], x)); | |
784 | ||
785 | assert_se(i == 3); | |
786 | ||
787 | FOREACH_STRING(x, "zzz") | |
788 | assert_se(streq(x, "zzz")); | |
789 | } | |
790 | ||
927be00c RC |
791 | static void test_filename_is_safe(void) { |
792 | char foo[FILENAME_MAX+2]; | |
793 | int i; | |
794 | ||
795 | assert_se(!filename_is_safe("")); | |
796 | assert_se(!filename_is_safe("/bar/foo")); | |
797 | assert_se(!filename_is_safe("/")); | |
798 | assert_se(!filename_is_safe(".")); | |
799 | assert_se(!filename_is_safe("..")); | |
800 | ||
801 | for (i=0; i<FILENAME_MAX+1; i++) | |
802 | foo[i] = 'a'; | |
803 | foo[FILENAME_MAX+1] = '\0'; | |
804 | ||
805 | assert_se(!filename_is_safe(foo)); | |
806 | ||
807 | assert_se(filename_is_safe("foo_bar-333")); | |
808 | assert_se(filename_is_safe("o.o")); | |
809 | } | |
810 | ||
1cb1767a ZJS |
811 | static void test_string_has_cc(void) { |
812 | assert_se(string_has_cc("abc\1", NULL)); | |
813 | assert_se(string_has_cc("abc\x7f", NULL)); | |
814 | assert_se(string_has_cc("abc\x7f", NULL)); | |
815 | assert_se(string_has_cc("abc\t\x7f", "\t")); | |
816 | assert_se(string_has_cc("abc\t\x7f", "\t")); | |
817 | assert_se(string_has_cc("\x7f", "\t")); | |
818 | assert_se(string_has_cc("\x7f", "\t\a")); | |
819 | ||
820 | assert_se(!string_has_cc("abc\t\t", "\t")); | |
821 | assert_se(!string_has_cc("abc\t\t\a", "\t\a")); | |
822 | assert_se(!string_has_cc("a\ab\tc", "\t\a")); | |
823 | } | |
824 | ||
927be00c RC |
825 | static void test_ascii_strlower(void) { |
826 | char a[] = "AabBcC Jk Ii Od LKJJJ kkd LK"; | |
827 | assert_se(streq(ascii_strlower(a), "aabbcc jk ii od lkjjj kkd lk")); | |
828 | } | |
829 | ||
830 | static void test_files_same(void) { | |
831 | _cleanup_close_ int fd = -1; | |
832 | char name[] = "/tmp/test-files_same.XXXXXX"; | |
833 | char name_alias[] = "/tmp/test-files_same.alias"; | |
834 | ||
835 | fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC); | |
836 | assert_se(fd >= 0); | |
837 | assert_se(symlink(name, name_alias) >= 0); | |
838 | ||
839 | assert_se(files_same(name, name)); | |
840 | assert_se(files_same(name, name_alias)); | |
841 | ||
842 | unlink(name); | |
843 | unlink(name_alias); | |
844 | } | |
845 | ||
846 | static void test_is_valid_documentation_url(void) { | |
847 | assert_se(is_valid_documentation_url("http://www.freedesktop.org/wiki/Software/systemd")); | |
848 | assert_se(is_valid_documentation_url("https://www.kernel.org/doc/Documentation/binfmt_misc.txt")); | |
849 | assert_se(is_valid_documentation_url("file:foo")); | |
850 | assert_se(is_valid_documentation_url("man:systemd.special(7)")); | |
851 | assert_se(is_valid_documentation_url("info:bar")); | |
852 | ||
853 | assert_se(!is_valid_documentation_url("foo:")); | |
854 | assert_se(!is_valid_documentation_url("info:")); | |
855 | assert_se(!is_valid_documentation_url("")); | |
856 | } | |
857 | ||
858 | static void test_file_in_same_dir(void) { | |
eee84633 DH |
859 | char *t; |
860 | ||
861 | t = file_in_same_dir("/", "a"); | |
862 | assert_se(streq(t, "/a")); | |
863 | free(t); | |
864 | ||
865 | t = file_in_same_dir("/", "/a"); | |
866 | assert_se(streq(t, "/a")); | |
867 | free(t); | |
868 | ||
869 | t = file_in_same_dir("", "a"); | |
870 | assert_se(streq(t, "a")); | |
871 | free(t); | |
872 | ||
873 | t = file_in_same_dir("a/", "a"); | |
874 | assert_se(streq(t, "a/a")); | |
875 | free(t); | |
876 | ||
877 | t = file_in_same_dir("bar/foo", "bar"); | |
878 | assert_se(streq(t, "bar/bar")); | |
879 | free(t); | |
927be00c RC |
880 | } |
881 | ||
882 | static void test_endswith(void) { | |
883 | assert_se(endswith("foobar", "bar")); | |
884 | assert_se(endswith("foobar", "")); | |
885 | assert_se(endswith("foobar", "foobar")); | |
886 | assert_se(endswith("", "")); | |
887 | ||
888 | assert_se(!endswith("foobar", "foo")); | |
889 | assert_se(!endswith("foobar", "foobarfoofoo")); | |
890 | } | |
891 | ||
892 | static void test_close_nointr(void) { | |
893 | char name[] = "/tmp/test-test-close_nointr.XXXXXX"; | |
894 | int fd; | |
895 | ||
896 | fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC); | |
897 | assert_se(fd >= 0); | |
898 | assert_se(close_nointr(fd) >= 0); | |
899 | assert_se(close_nointr(fd) < 0); | |
900 | ||
901 | unlink(name); | |
902 | } | |
903 | ||
904 | ||
905 | static void test_unlink_noerrno(void) { | |
906 | char name[] = "/tmp/test-close_nointr.XXXXXX"; | |
907 | int fd; | |
908 | ||
909 | fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC); | |
910 | assert_se(fd >= 0); | |
911 | assert_se(close_nointr(fd) >= 0); | |
912 | ||
913 | { | |
914 | PROTECT_ERRNO; | |
915 | errno = -42; | |
916 | assert_se(unlink_noerrno(name) >= 0); | |
917 | assert_se(errno == -42); | |
918 | assert_se(unlink_noerrno(name) < 0); | |
919 | assert_se(errno == -42); | |
920 | } | |
921 | } | |
922 | ||
923 | static void test_readlink_and_make_absolute(void) { | |
924 | char tempdir[] = "/tmp/test-readlink_and_make_absolute"; | |
925 | char name[] = "/tmp/test-readlink_and_make_absolute/original"; | |
926 | char name2[] = "test-readlink_and_make_absolute/original"; | |
927 | char name_alias[] = "/tmp/test-readlink_and_make_absolute-alias"; | |
928 | char *r = NULL; | |
929 | ||
684fc892 | 930 | assert_se(mkdir_safe(tempdir, 0755, getuid(), getgid()) >= 0); |
927be00c RC |
931 | assert_se(touch(name) >= 0); |
932 | ||
933 | assert_se(symlink(name, name_alias) >= 0); | |
934 | assert_se(readlink_and_make_absolute(name_alias, &r) >= 0); | |
935 | assert_se(streq(r, name)); | |
936 | free(r); | |
937 | assert_se(unlink(name_alias) >= 0); | |
938 | ||
939 | assert_se(chdir(tempdir) >= 0); | |
940 | assert_se(symlink(name2, name_alias) >= 0); | |
941 | assert_se(readlink_and_make_absolute(name_alias, &r) >= 0); | |
942 | assert_se(streq(r, name)); | |
943 | free(r); | |
944 | assert_se(unlink(name_alias) >= 0); | |
945 | ||
946 | assert_se(rm_rf_dangerous(tempdir, false, true, false) >= 0); | |
947 | } | |
948 | ||
949 | static void test_read_one_char(void) { | |
3f2e132a | 950 | _cleanup_fclose_ FILE *file = NULL; |
927be00c RC |
951 | char r; |
952 | bool need_nl; | |
953 | char name[] = "/tmp/test-read_one_char.XXXXXX"; | |
3f2e132a | 954 | int fd; |
927be00c RC |
955 | |
956 | fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC); | |
957 | assert_se(fd >= 0); | |
958 | file = fdopen(fd, "r+"); | |
959 | assert_se(file); | |
960 | assert_se(fputs("c\n", file) >= 0); | |
961 | rewind(file); | |
962 | ||
963 | assert_se(read_one_char(file, &r, 1000000, &need_nl) >= 0); | |
964 | assert_se(!need_nl); | |
965 | assert_se(r == 'c'); | |
966 | assert_se(read_one_char(file, &r, 1000000, &need_nl) < 0); | |
967 | ||
968 | rewind(file); | |
969 | assert_se(fputs("foobar\n", file) >= 0); | |
970 | rewind(file); | |
971 | assert_se(read_one_char(file, &r, 1000000, &need_nl) < 0); | |
972 | ||
973 | rewind(file); | |
974 | assert_se(fputs("\n", file) >= 0); | |
975 | rewind(file); | |
976 | assert_se(read_one_char(file, &r, 1000000, &need_nl) < 0); | |
977 | ||
978 | unlink(name); | |
979 | } | |
980 | ||
981 | static void test_ignore_signals(void) { | |
982 | assert_se(ignore_signals(SIGINT, -1) >= 0); | |
983 | assert_se(kill(getpid(), SIGINT) >= 0); | |
984 | assert_se(ignore_signals(SIGUSR1, SIGUSR2, SIGTERM, SIGPIPE, -1) >= 0); | |
985 | assert_se(kill(getpid(), SIGUSR1) >= 0); | |
986 | assert_se(kill(getpid(), SIGUSR2) >= 0); | |
987 | assert_se(kill(getpid(), SIGTERM) >= 0); | |
988 | assert_se(kill(getpid(), SIGPIPE) >= 0); | |
989 | assert_se(default_signals(SIGINT, SIGUSR1, SIGUSR2, SIGTERM, SIGPIPE, -1) >= 0); | |
990 | } | |
991 | ||
992 | static void test_strshorten(void) { | |
993 | char s[] = "foobar"; | |
994 | ||
995 | assert_se(strlen(strshorten(s, 6)) == 6); | |
996 | assert_se(strlen(strshorten(s, 12)) == 6); | |
997 | assert_se(strlen(strshorten(s, 2)) == 2); | |
998 | assert_se(strlen(strshorten(s, 0)) == 0); | |
999 | } | |
1000 | ||
8085f163 DR |
1001 | static void test_strappenda(void) { |
1002 | char *actual; | |
1003 | ||
1004 | actual = strappenda("", "foo", "bar"); | |
1005 | assert_se(streq(actual, "foobar")); | |
1006 | ||
1007 | actual = strappenda("foo", "bar", "baz"); | |
1008 | assert_se(streq(actual, "foobarbaz")); | |
1009 | ||
1010 | actual = strappenda("foo", "", "bar", "baz"); | |
1011 | assert_se(streq(actual, "foobarbaz")); | |
1012 | } | |
1013 | ||
8852362b RC |
1014 | static void test_is_symlink(void) { |
1015 | char name[] = "/tmp/test-is_symlink.XXXXXX"; | |
1016 | char name_link[] = "/tmp/test-is_symlink.link"; | |
1017 | _cleanup_close_ int fd = -1; | |
1018 | ||
1019 | fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC); | |
1020 | assert_se(fd >= 0); | |
1021 | assert_se(symlink(name, name_link) >= 0); | |
1022 | ||
1023 | assert_se(is_symlink(name) == 0); | |
1024 | assert_se(is_symlink(name_link) == 1); | |
1025 | assert_se(is_symlink("/a/file/which/does/not/exist/i/guess") < 0); | |
1026 | ||
1027 | ||
1028 | unlink(name); | |
1029 | unlink(name_link); | |
1030 | } | |
1031 | ||
1032 | static void test_pid_is_unwaited(void) { | |
1033 | pid_t pid; | |
1034 | ||
1035 | pid = fork(); | |
1036 | assert_se(pid >= 0); | |
1037 | if (pid == 0) { | |
1038 | _exit(EXIT_SUCCESS); | |
1039 | } else { | |
1040 | int status; | |
1041 | ||
1042 | waitpid(pid, &status, 0); | |
1043 | assert_se(!pid_is_unwaited(pid)); | |
1044 | } | |
1045 | assert_se(pid_is_unwaited(getpid())); | |
1046 | assert_se(!pid_is_unwaited(-1)); | |
1047 | } | |
1048 | ||
1049 | static void test_pid_is_alive(void) { | |
1050 | pid_t pid; | |
1051 | ||
1052 | pid = fork(); | |
1053 | assert_se(pid >= 0); | |
1054 | if (pid == 0) { | |
1055 | _exit(EXIT_SUCCESS); | |
1056 | } else { | |
1057 | int status; | |
1058 | ||
1059 | waitpid(pid, &status, 0); | |
1060 | assert_se(!pid_is_alive(pid)); | |
1061 | } | |
1062 | assert_se(pid_is_alive(getpid())); | |
1063 | assert_se(!pid_is_alive(-1)); | |
1064 | } | |
1065 | ||
1066 | static void test_search_and_fopen(void) { | |
1067 | const char *dirs[] = {"/tmp/foo/bar", "/tmp", NULL}; | |
1068 | char name[] = "/tmp/test-search_and_fopen.XXXXXX"; | |
1069 | int fd = -1; | |
1070 | int r; | |
1071 | FILE *f; | |
1072 | ||
1073 | fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC); | |
1074 | assert_se(fd >= 0); | |
1075 | close(fd); | |
1076 | ||
1077 | r = search_and_fopen(basename(name), "r", NULL, dirs, &f); | |
1078 | assert_se(r >= 0); | |
1079 | fclose(f); | |
1080 | ||
1081 | r = search_and_fopen(name, "r", NULL, dirs, &f); | |
1082 | assert_se(r >= 0); | |
1083 | fclose(f); | |
1084 | ||
1085 | r = search_and_fopen(basename(name), "r", "/", dirs, &f); | |
1086 | assert_se(r >= 0); | |
1087 | fclose(f); | |
1088 | ||
1089 | r = search_and_fopen("/a/file/which/does/not/exist/i/guess", "r", NULL, dirs, &f); | |
1090 | assert_se(r < 0); | |
1091 | r = search_and_fopen("afilewhichdoesnotexistiguess", "r", NULL, dirs, &f); | |
1092 | assert_se(r < 0); | |
1093 | ||
1094 | r = unlink(name); | |
1095 | assert_se(r == 0); | |
1096 | ||
1097 | r = search_and_fopen(basename(name), "r", NULL, dirs, &f); | |
1098 | assert_se(r < 0); | |
1099 | } | |
1100 | ||
1101 | ||
1102 | static void test_search_and_fopen_nulstr(void) { | |
1103 | const char dirs[] = "/tmp/foo/bar\0/tmp\0"; | |
1104 | char name[] = "/tmp/test-search_and_fopen.XXXXXX"; | |
1105 | int fd = -1; | |
1106 | int r; | |
1107 | FILE *f; | |
1108 | ||
1109 | fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC); | |
1110 | assert_se(fd >= 0); | |
1111 | close(fd); | |
1112 | ||
1113 | r = search_and_fopen_nulstr(basename(name), "r", NULL, dirs, &f); | |
1114 | assert_se(r >= 0); | |
1115 | fclose(f); | |
1116 | ||
1117 | r = search_and_fopen_nulstr(name, "r", NULL, dirs, &f); | |
1118 | assert_se(r >= 0); | |
1119 | fclose(f); | |
1120 | ||
1121 | r = search_and_fopen_nulstr("/a/file/which/does/not/exist/i/guess", "r", NULL, dirs, &f); | |
1122 | assert_se(r < 0); | |
1123 | r = search_and_fopen_nulstr("afilewhichdoesnotexistiguess", "r", NULL, dirs, &f); | |
1124 | assert_se(r < 0); | |
1125 | ||
1126 | r = unlink(name); | |
1127 | assert_se(r == 0); | |
1128 | ||
1129 | r = search_and_fopen_nulstr(basename(name), "r", NULL, dirs, &f); | |
1130 | assert_se(r < 0); | |
1131 | } | |
1132 | ||
1133 | static void test_glob_exists(void) { | |
1134 | char name[] = "/tmp/test-glob_exists.XXXXXX"; | |
1135 | int fd = -1; | |
1136 | int r; | |
1137 | ||
1138 | fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC); | |
1139 | assert_se(fd >= 0); | |
1140 | close(fd); | |
1141 | ||
1142 | r = glob_exists("/tmp/test-glob_exists*"); | |
1143 | assert_se(r == 1); | |
1144 | ||
1145 | r = unlink(name); | |
1146 | assert_se(r == 0); | |
1147 | r = glob_exists("/tmp/test-glob_exists*"); | |
1148 | assert_se(r == 0); | |
1149 | } | |
1150 | ||
1151 | static void test_execute_directory(void) { | |
1152 | char name[] = "/tmp/test-execute_directory/script1"; | |
1153 | char name2[] = "/tmp/test-execute_directory/script2"; | |
1154 | char name3[] = "/tmp/test-execute_directory/useless"; | |
1155 | char tempdir[] = "/tmp/test-execute_directory/"; | |
1156 | ||
1157 | assert_se(mkdir_safe(tempdir, 0755, getuid(), getgid()) >= 0); | |
1158 | assert_se(write_string_file(name, "#!/bin/sh\necho 'Executing '$0\ntouch /tmp/test-execute_directory/it_works") == 0); | |
1159 | assert_se(write_string_file(name2, "#!/bin/sh\necho 'Executing '$0\ntouch /tmp/test-execute_directory/it_works2") == 0); | |
1160 | assert_se(chmod(name, 0755) == 0); | |
1161 | assert_se(chmod(name2, 0755) == 0); | |
1162 | assert_se(touch(name3) >= 0); | |
1163 | ||
1164 | execute_directory(tempdir, NULL, DEFAULT_TIMEOUT_USEC, NULL); | |
1165 | assert_se(access("/tmp/test-execute_directory/it_works", F_OK) >= 0); | |
1166 | assert_se(access("/tmp/test-execute_directory/it_works2", F_OK) >= 0); | |
1167 | ||
1168 | rm_rf_dangerous(tempdir, false, true, false); | |
1169 | } | |
1170 | ||
7629889c LP |
1171 | static void test_unquote_first_word(void) { |
1172 | const char *p, *original; | |
1173 | char *t; | |
1174 | ||
1175 | p = original = "foobar waldo"; | |
f32d2db1 | 1176 | assert_se(unquote_first_word(&p, &t, false) > 0); |
7629889c LP |
1177 | assert_se(streq(t, "foobar")); |
1178 | free(t); | |
1179 | assert_se(p == original + 7); | |
1180 | ||
f32d2db1 | 1181 | assert_se(unquote_first_word(&p, &t, false) > 0); |
7629889c LP |
1182 | assert_se(streq(t, "waldo")); |
1183 | free(t); | |
1184 | assert_se(p == original + 12); | |
1185 | ||
f32d2db1 | 1186 | assert_se(unquote_first_word(&p, &t, false) == 0); |
7629889c LP |
1187 | assert_se(!t); |
1188 | assert_se(p == original + 12); | |
1189 | ||
1190 | p = original = "\"foobar\" \'waldo\'"; | |
f32d2db1 | 1191 | assert_se(unquote_first_word(&p, &t, false) > 0); |
7629889c LP |
1192 | assert_se(streq(t, "foobar")); |
1193 | free(t); | |
1194 | assert_se(p == original + 9); | |
1195 | ||
f32d2db1 | 1196 | assert_se(unquote_first_word(&p, &t, false) > 0); |
7629889c LP |
1197 | assert_se(streq(t, "waldo")); |
1198 | free(t); | |
1199 | assert_se(p == original + 16); | |
1200 | ||
f32d2db1 | 1201 | assert_se(unquote_first_word(&p, &t, false) == 0); |
7629889c LP |
1202 | assert_se(!t); |
1203 | assert_se(p == original + 16); | |
1204 | ||
1205 | p = original = "\""; | |
f32d2db1 | 1206 | assert_se(unquote_first_word(&p, &t, false) == -EINVAL); |
7629889c LP |
1207 | assert_se(p == original + 1); |
1208 | ||
1209 | p = original = "\'"; | |
f32d2db1 | 1210 | assert_se(unquote_first_word(&p, &t, false) == -EINVAL); |
7629889c LP |
1211 | assert_se(p == original + 1); |
1212 | ||
f32d2db1 LP |
1213 | p = original = "\'fooo"; |
1214 | assert_se(unquote_first_word(&p, &t, false) == -EINVAL); | |
1215 | assert_se(p == original + 5); | |
1216 | ||
1217 | p = original = "\'fooo"; | |
1218 | assert_se(unquote_first_word(&p, &t, true) > 0); | |
1219 | assert_se(streq(t, "fooo")); | |
e1ba963f | 1220 | free(t); |
f32d2db1 LP |
1221 | assert_se(p == original + 5); |
1222 | ||
7629889c | 1223 | p = original = "yay\'foo\'bar"; |
f32d2db1 | 1224 | assert_se(unquote_first_word(&p, &t, false) > 0); |
7629889c LP |
1225 | assert_se(streq(t, "yayfoobar")); |
1226 | free(t); | |
1227 | assert_se(p == original + 11); | |
1228 | ||
1229 | p = original = " foobar "; | |
f32d2db1 | 1230 | assert_se(unquote_first_word(&p, &t, false) > 0); |
7629889c LP |
1231 | assert_se(streq(t, "foobar")); |
1232 | free(t); | |
1233 | assert_se(p == original + 12); | |
1234 | } | |
1235 | ||
1236 | static void test_unquote_many_words(void) { | |
1237 | const char *p, *original; | |
1238 | char *a, *b, *c; | |
1239 | ||
1240 | p = original = "foobar waldi piep"; | |
1241 | assert_se(unquote_many_words(&p, &a, &b, &c, NULL) == 3); | |
1242 | assert_se(p == original + 17); | |
1243 | assert_se(streq_ptr(a, "foobar")); | |
1244 | assert_se(streq_ptr(b, "waldi")); | |
1245 | assert_se(streq_ptr(c, "piep")); | |
1246 | free(a); | |
1247 | free(b); | |
1248 | free(c); | |
1249 | ||
1250 | p = original = "'foobar' wa\"ld\"i "; | |
1251 | assert_se(unquote_many_words(&p, &a, &b, &c, NULL) == 2); | |
1252 | assert_se(p == original + 19); | |
1253 | assert_se(streq_ptr(a, "foobar")); | |
1254 | assert_se(streq_ptr(b, "waldi")); | |
1255 | assert_se(streq_ptr(c, NULL)); | |
1256 | free(a); | |
1257 | free(b); | |
1258 | ||
1259 | p = original = ""; | |
1260 | assert_se(unquote_many_words(&p, &a, &b, &c, NULL) == 0); | |
1261 | assert_se(p == original); | |
1262 | assert_se(streq_ptr(a, NULL)); | |
1263 | assert_se(streq_ptr(b, NULL)); | |
1264 | assert_se(streq_ptr(c, NULL)); | |
1265 | ||
1266 | p = original = " "; | |
1267 | assert_se(unquote_many_words(&p, &a, &b, &c, NULL) == 0); | |
1268 | assert_se(p == original+2); | |
1269 | assert_se(streq_ptr(a, NULL)); | |
1270 | assert_se(streq_ptr(b, NULL)); | |
1271 | assert_se(streq_ptr(c, NULL)); | |
1272 | ||
1273 | p = original = "foobar"; | |
1274 | assert_se(unquote_many_words(&p, NULL) == 0); | |
1275 | assert_se(p == original); | |
1276 | ||
1277 | p = original = "foobar waldi"; | |
1278 | assert_se(unquote_many_words(&p, &a, NULL) == 1); | |
1279 | assert_se(p == original+7); | |
1280 | assert_se(streq_ptr(a, "foobar")); | |
eee84633 | 1281 | free(a); |
7629889c LP |
1282 | |
1283 | p = original = " foobar "; | |
1284 | assert_se(unquote_many_words(&p, &a, NULL) == 1); | |
1285 | assert_se(p == original+15); | |
1286 | assert_se(streq_ptr(a, "foobar")); | |
eee84633 | 1287 | free(a); |
7629889c LP |
1288 | } |
1289 | ||
f32d2db1 LP |
1290 | static int parse_item(const char *key, const char *value) { |
1291 | assert_se(key); | |
1292 | ||
1293 | log_info("kernel cmdline option <%s> = <%s>", key, strna(value)); | |
1294 | return 0; | |
1295 | } | |
1296 | ||
1297 | static void test_parse_proc_cmdline(void) { | |
1298 | assert_se(parse_proc_cmdline(parse_item) >= 0); | |
1299 | } | |
1300 | ||
539ad707 | 1301 | int main(int argc, char *argv[]) { |
9480794b ZJS |
1302 | log_parse_environment(); |
1303 | log_open(); | |
1304 | ||
539ad707 | 1305 | test_streq_ptr(); |
625e870b | 1306 | test_align_power2(); |
7242d742 | 1307 | test_max(); |
fb835651 | 1308 | test_container_of(); |
95d78c7e | 1309 | test_alloca(); |
539ad707 | 1310 | test_first_word(); |
dbd73f9e | 1311 | test_close_many(); |
8354c34e | 1312 | test_parse_boolean(); |
8d99e5f5 TA |
1313 | test_parse_pid(); |
1314 | test_parse_uid(); | |
1315 | test_safe_atolli(); | |
1316 | test_safe_atod(); | |
dbd73f9e | 1317 | test_strappend(); |
1ef04f0b TA |
1318 | test_strstrip(); |
1319 | test_delete_chars(); | |
1320 | test_in_charset(); | |
44f4c86c DB |
1321 | test_hexchar(); |
1322 | test_unhexchar(); | |
1323 | test_octchar(); | |
1324 | test_unoctchar(); | |
1325 | test_decchar(); | |
1326 | test_undecchar(); | |
b4ecc959 TA |
1327 | test_cescape(); |
1328 | test_cunescape(); | |
1ef04f0b | 1329 | test_foreach_word(); |
539ad707 | 1330 | test_foreach_word_quoted(); |
1ef04f0b | 1331 | test_default_term_for_tty(); |
0d585d82 | 1332 | test_memdup_multiply(); |
aa3c5cf8 | 1333 | test_hostname_is_valid(); |
144e51ec | 1334 | test_u64log2(); |
49aa47c7 | 1335 | test_get_process_comm(); |
2a371001 | 1336 | test_protect_errno(); |
5556b5fe | 1337 | test_parse_size(); |
9480794b | 1338 | test_config_parse_iec_off(); |
b4ecc959 TA |
1339 | test_strextend(); |
1340 | test_strrep(); | |
d4ac85c6 | 1341 | test_split_pair(); |
22f5f628 | 1342 | test_fstab_node_to_udev_node(); |
893fa014 | 1343 | test_get_files_in_directory(); |
cabb7806 | 1344 | test_in_set(); |
87b02843 | 1345 | test_writing_tmpfile(); |
29bfbcd6 | 1346 | test_hexdump(); |
8fe90522 | 1347 | test_log2i(); |
c4a7b2c5 | 1348 | test_foreach_string(); |
927be00c | 1349 | test_filename_is_safe(); |
1cb1767a | 1350 | test_string_has_cc(); |
927be00c RC |
1351 | test_ascii_strlower(); |
1352 | test_files_same(); | |
1353 | test_is_valid_documentation_url(); | |
1354 | test_file_in_same_dir(); | |
1355 | test_endswith(); | |
1356 | test_close_nointr(); | |
1357 | test_unlink_noerrno(); | |
1358 | test_readlink_and_make_absolute(); | |
1359 | test_read_one_char(); | |
1360 | test_ignore_signals(); | |
1361 | test_strshorten(); | |
8085f163 | 1362 | test_strappenda(); |
8852362b RC |
1363 | test_is_symlink(); |
1364 | test_pid_is_unwaited(); | |
1365 | test_pid_is_alive(); | |
1366 | test_search_and_fopen(); | |
1367 | test_search_and_fopen_nulstr(); | |
1368 | test_glob_exists(); | |
1369 | test_execute_directory(); | |
7629889c LP |
1370 | test_unquote_first_word(); |
1371 | test_unquote_many_words(); | |
f32d2db1 | 1372 | test_parse_proc_cmdline(); |
539ad707 TA |
1373 | |
1374 | return 0; | |
1375 | } |