1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
7 Copyright 2013 Thomas H.P. Andersen
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.
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.
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/>.
29 #include <sys/types.h>
31 #include <sys/xattr.h>
34 #include "alloc-util.h"
35 #include "conf-parser.h"
36 #include "cpu-set-util.h"
42 #include "fstab-util.h"
43 #include "glob-util.h"
44 #include "hexdecoct.h"
47 #include "parse-util.h"
48 #include "path-util.h"
49 #include "proc-cmdline.h"
50 #include "process-util.h"
52 #include "signal-util.h"
53 #include "stat-util.h"
54 #include "string-util.h"
56 #include "user-util.h"
60 #include "xattr-util.h"
62 static void test_streq_ptr(void) {
63 assert_se(streq_ptr(NULL, NULL));
64 assert_se(!streq_ptr("abc", "cdef"));
67 static void test_align_power2(void) {
70 assert_se(ALIGN_POWER2(0) == 0);
71 assert_se(ALIGN_POWER2(1) == 1);
72 assert_se(ALIGN_POWER2(2) == 2);
73 assert_se(ALIGN_POWER2(3) == 4);
74 assert_se(ALIGN_POWER2(12) == 16);
76 assert_se(ALIGN_POWER2(ULONG_MAX) == 0);
77 assert_se(ALIGN_POWER2(ULONG_MAX - 1) == 0);
78 assert_se(ALIGN_POWER2(ULONG_MAX - 1024) == 0);
79 assert_se(ALIGN_POWER2(ULONG_MAX / 2) == ULONG_MAX / 2 + 1);
80 assert_se(ALIGN_POWER2(ULONG_MAX + 1) == 0);
82 for (i = 1; i < 131071; ++i) {
83 for (p2 = 1; p2 < i; p2 <<= 1)
86 assert_se(ALIGN_POWER2(i) == p2);
89 for (i = ULONG_MAX - 1024; i < ULONG_MAX; ++i) {
90 for (p2 = 1; p2 && p2 < i; p2 <<= 1)
93 assert_se(ALIGN_POWER2(i) == p2);
97 static void test_max(void) {
100 int b[CONST_MAX(10, 100)];
102 .a = CONST_MAX(10, 100),
106 assert_cc(sizeof(val1.b) == sizeof(int) * 100);
108 /* CONST_MAX returns (void) instead of a value if the passed arguments
109 * are not of the same type or not constant expressions. */
110 assert_cc(__builtin_types_compatible_p(typeof(CONST_MAX(1, 10)), int));
111 assert_cc(__builtin_types_compatible_p(typeof(CONST_MAX(1, 1U)), void));
113 assert_se(val1.a == 100);
114 assert_se(MAX(++d, 0) == 1);
117 assert_cc(MAXSIZE(char[3], uint16_t) == 3);
118 assert_cc(MAXSIZE(char[3], uint32_t) == 4);
119 assert_cc(MAXSIZE(char, long) == sizeof(long));
121 assert_se(MAX(-5, 5) == 5);
122 assert_se(MAX(5, 5) == 5);
123 assert_se(MAX(MAX(1, MAX(2, MAX(3, 4))), 5) == 5);
124 assert_se(MAX(MAX(1, MAX(2, MAX(3, 2))), 1) == 3);
125 assert_se(MAX(MIN(1, MIN(2, MIN(3, 4))), 5) == 5);
126 assert_se(MAX(MAX(1, MIN(2, MIN(3, 2))), 1) == 2);
127 assert_se(LESS_BY(8, 4) == 4);
128 assert_se(LESS_BY(8, 8) == 0);
129 assert_se(LESS_BY(4, 8) == 0);
130 assert_se(LESS_BY(16, LESS_BY(8, 4)) == 12);
131 assert_se(LESS_BY(4, LESS_BY(8, 4)) == 0);
132 assert_se(CLAMP(-5, 0, 1) == 0);
133 assert_se(CLAMP(5, 0, 1) == 1);
134 assert_se(CLAMP(5, -10, 1) == 1);
135 assert_se(CLAMP(5, -10, 10) == 5);
136 assert_se(CLAMP(CLAMP(0, -10, 10), CLAMP(-5, 10, 20), CLAMP(100, -5, 20)) == 10);
139 static void test_container_of(void) {
145 } _packed_ myval = { };
147 assert_cc(sizeof(myval) == 17);
148 assert_se(container_of(&myval.v1, struct mytype, v1) == &myval);
149 assert_se(container_of(&myval.v2, struct mytype, v2) == &myval);
150 assert_se(container_of(&container_of(&myval.v2,
157 static void test_alloca(void) {
158 static const uint8_t zero[997] = { };
161 t = alloca_align(17, 512);
162 assert_se(!((uintptr_t)t & 0xff));
165 t = alloca0_align(997, 1024);
166 assert_se(!((uintptr_t)t & 0x1ff));
167 assert_se(!memcmp(t, zero, 997));
170 static void test_div_round_up(void) {
174 assert_se(DIV_ROUND_UP(0, 8) == 0);
175 assert_se(DIV_ROUND_UP(1, 8) == 1);
176 assert_se(DIV_ROUND_UP(8, 8) == 1);
177 assert_se(DIV_ROUND_UP(12, 8) == 2);
178 assert_se(DIV_ROUND_UP(16, 8) == 2);
180 /* test multiple evaluation */
182 assert_se(DIV_ROUND_UP(div++, 8) == 0 && div == 1);
183 assert_se(DIV_ROUND_UP(++div, 8) == 1 && div == 2);
184 assert_se(DIV_ROUND_UP(8, div++) == 4 && div == 3);
185 assert_se(DIV_ROUND_UP(8, ++div) == 2 && div == 4);
187 /* overflow test with exact division */
188 assert_se(sizeof(0U) == 4);
189 assert_se(0xfffffffaU % 10U == 0U);
190 assert_se(0xfffffffaU / 10U == 429496729U);
191 assert_se(DIV_ROUND_UP(0xfffffffaU, 10U) == 429496729U);
192 assert_se((0xfffffffaU + 10U - 1U) / 10U == 0U);
193 assert_se(0xfffffffaU / 10U + !!(0xfffffffaU % 10U) == 429496729U);
195 /* overflow test with rounded division */
196 assert_se(0xfffffffdU % 10U == 3U);
197 assert_se(0xfffffffdU / 10U == 429496729U);
198 assert_se(DIV_ROUND_UP(0xfffffffdU, 10U) == 429496730U);
199 assert_se((0xfffffffdU + 10U - 1U) / 10U == 0U);
200 assert_se(0xfffffffdU / 10U + !!(0xfffffffdU % 10U) == 429496730U);
203 static void test_first_word(void) {
204 assert_se(first_word("Hello", ""));
205 assert_se(first_word("Hello", "Hello"));
206 assert_se(first_word("Hello world", "Hello"));
207 assert_se(first_word("Hello\tworld", "Hello"));
208 assert_se(first_word("Hello\nworld", "Hello"));
209 assert_se(first_word("Hello\rworld", "Hello"));
210 assert_se(first_word("Hello ", "Hello"));
212 assert_se(!first_word("Hello", "Hellooo"));
213 assert_se(!first_word("Hello", "xxxxx"));
214 assert_se(!first_word("Hellooo", "Hello"));
217 static void test_close_many(void) {
219 char name0[] = "/tmp/test-close-many.XXXXXX";
220 char name1[] = "/tmp/test-close-many.XXXXXX";
221 char name2[] = "/tmp/test-close-many.XXXXXX";
223 fds[0] = mkostemp_safe(name0, O_RDWR|O_CLOEXEC);
224 fds[1] = mkostemp_safe(name1, O_RDWR|O_CLOEXEC);
225 fds[2] = mkostemp_safe(name2, O_RDWR|O_CLOEXEC);
229 assert_se(fcntl(fds[0], F_GETFD) == -1);
230 assert_se(fcntl(fds[1], F_GETFD) == -1);
231 assert_se(fcntl(fds[2], F_GETFD) >= 0);
240 static void test_parse_boolean(void) {
241 assert_se(parse_boolean("1") == 1);
242 assert_se(parse_boolean("y") == 1);
243 assert_se(parse_boolean("Y") == 1);
244 assert_se(parse_boolean("yes") == 1);
245 assert_se(parse_boolean("YES") == 1);
246 assert_se(parse_boolean("true") == 1);
247 assert_se(parse_boolean("TRUE") == 1);
248 assert_se(parse_boolean("on") == 1);
249 assert_se(parse_boolean("ON") == 1);
251 assert_se(parse_boolean("0") == 0);
252 assert_se(parse_boolean("n") == 0);
253 assert_se(parse_boolean("N") == 0);
254 assert_se(parse_boolean("no") == 0);
255 assert_se(parse_boolean("NO") == 0);
256 assert_se(parse_boolean("false") == 0);
257 assert_se(parse_boolean("FALSE") == 0);
258 assert_se(parse_boolean("off") == 0);
259 assert_se(parse_boolean("OFF") == 0);
261 assert_se(parse_boolean("garbage") < 0);
262 assert_se(parse_boolean("") < 0);
263 assert_se(parse_boolean("full") < 0);
266 static void test_parse_pid(void) {
270 r = parse_pid("100", &pid);
272 assert_se(pid == 100);
274 r = parse_pid("0x7FFFFFFF", &pid);
276 assert_se(pid == 2147483647);
278 pid = 65; /* pid is left unchanged on ERANGE. Set to known arbitrary value. */
279 r = parse_pid("0", &pid);
280 assert_se(r == -ERANGE);
281 assert_se(pid == 65);
283 pid = 65; /* pid is left unchanged on ERANGE. Set to known arbitrary value. */
284 r = parse_pid("-100", &pid);
285 assert_se(r == -ERANGE);
286 assert_se(pid == 65);
288 pid = 65; /* pid is left unchanged on ERANGE. Set to known arbitrary value. */
289 r = parse_pid("0xFFFFFFFFFFFFFFFFF", &pid);
290 assert_se(r == -ERANGE);
291 assert_se(pid == 65);
293 r = parse_pid("junk", &pid);
294 assert_se(r == -EINVAL);
297 static void test_parse_uid(void) {
301 r = parse_uid("100", &uid);
303 assert_se(uid == 100);
305 r = parse_uid("65535", &uid);
306 assert_se(r == -ENXIO);
308 r = parse_uid("asdsdas", &uid);
309 assert_se(r == -EINVAL);
312 static void test_safe_atou16(void) {
316 r = safe_atou16("12345", &l);
318 assert_se(l == 12345);
320 r = safe_atou16("123456", &l);
321 assert_se(r == -ERANGE);
323 r = safe_atou16("junk", &l);
324 assert_se(r == -EINVAL);
327 static void test_safe_atoi16(void) {
331 r = safe_atoi16("-12345", &l);
333 assert_se(l == -12345);
335 r = safe_atoi16("36536", &l);
336 assert_se(r == -ERANGE);
338 r = safe_atoi16("junk", &l);
339 assert_se(r == -EINVAL);
342 static void test_safe_atolli(void) {
346 r = safe_atolli("12345", &l);
348 assert_se(l == 12345);
350 r = safe_atolli("junk", &l);
351 assert_se(r == -EINVAL);
354 static void test_safe_atod(void) {
359 r = safe_atod("junk", &d);
360 assert_se(r == -EINVAL);
362 r = safe_atod("0.2244", &d);
364 assert_se(fabs(d - 0.2244) < 0.000001);
366 r = safe_atod("0,5", &d);
367 assert_se(r == -EINVAL);
371 assert_se(*e == ',');
373 /* Check if this really is locale independent */
374 if (setlocale(LC_NUMERIC, "de_DE.utf8")) {
376 r = safe_atod("0.2244", &d);
378 assert_se(fabs(d - 0.2244) < 0.000001);
380 r = safe_atod("0,5", &d);
381 assert_se(r == -EINVAL);
384 assert_se(fabs(strtod("0,5", &e) - 0.5) < 0.00001);
387 /* And check again, reset */
388 assert_se(setlocale(LC_NUMERIC, "C"));
390 r = safe_atod("0.2244", &d);
392 assert_se(fabs(d - 0.2244) < 0.000001);
394 r = safe_atod("0,5", &d);
395 assert_se(r == -EINVAL);
399 assert_se(*e == ',');
402 static void test_strappend(void) {
403 _cleanup_free_ char *t1, *t2, *t3, *t4;
405 t1 = strappend(NULL, NULL);
406 assert_se(streq(t1, ""));
408 t2 = strappend(NULL, "suf");
409 assert_se(streq(t2, "suf"));
411 t3 = strappend("pre", NULL);
412 assert_se(streq(t3, "pre"));
414 t4 = strappend("pre", "suf");
415 assert_se(streq(t4, "presuf"));
418 static void test_strstrip(void) {
420 char input[] = " hello, waldo. ";
423 assert_se(streq(r, "hello, waldo."));
426 static void test_delete_chars(void) {
428 char input[] = " hello, waldo. abc";
430 r = delete_chars(input, WHITESPACE);
431 assert_se(streq(r, "hello,waldo.abc"));
434 static void test_in_charset(void) {
435 assert_se(in_charset("dddaaabbbcccc", "abcd"));
436 assert_se(!in_charset("dddaaabbbcccc", "abc f"));
439 static void test_hexchar(void) {
440 assert_se(hexchar(0xa) == 'a');
441 assert_se(hexchar(0x0) == '0');
444 static void test_unhexchar(void) {
445 assert_se(unhexchar('a') == 0xA);
446 assert_se(unhexchar('A') == 0xA);
447 assert_se(unhexchar('0') == 0x0);
450 static void test_base32hexchar(void) {
451 assert_se(base32hexchar(0) == '0');
452 assert_se(base32hexchar(9) == '9');
453 assert_se(base32hexchar(10) == 'A');
454 assert_se(base32hexchar(31) == 'V');
457 static void test_unbase32hexchar(void) {
458 assert_se(unbase32hexchar('0') == 0);
459 assert_se(unbase32hexchar('9') == 9);
460 assert_se(unbase32hexchar('A') == 10);
461 assert_se(unbase32hexchar('V') == 31);
462 assert_se(unbase32hexchar('=') == -EINVAL);
465 static void test_base64char(void) {
466 assert_se(base64char(0) == 'A');
467 assert_se(base64char(26) == 'a');
468 assert_se(base64char(63) == '/');
471 static void test_unbase64char(void) {
472 assert_se(unbase64char('A') == 0);
473 assert_se(unbase64char('Z') == 25);
474 assert_se(unbase64char('a') == 26);
475 assert_se(unbase64char('z') == 51);
476 assert_se(unbase64char('0') == 52);
477 assert_se(unbase64char('9') == 61);
478 assert_se(unbase64char('+') == 62);
479 assert_se(unbase64char('/') == 63);
480 assert_se(unbase64char('=') == -EINVAL);
483 static void test_octchar(void) {
484 assert_se(octchar(00) == '0');
485 assert_se(octchar(07) == '7');
488 static void test_unoctchar(void) {
489 assert_se(unoctchar('0') == 00);
490 assert_se(unoctchar('7') == 07);
493 static void test_decchar(void) {
494 assert_se(decchar(0) == '0');
495 assert_se(decchar(9) == '9');
498 static void test_undecchar(void) {
499 assert_se(undecchar('0') == 0);
500 assert_se(undecchar('9') == 9);
503 static void test_unhexmem(void) {
504 const char *hex = "efa214921";
505 const char *hex_invalid = "efa214921o";
506 _cleanup_free_ char *hex2 = NULL;
507 _cleanup_free_ void *mem = NULL;
510 assert_se(unhexmem(hex, strlen(hex), &mem, &len) == 0);
511 assert_se(unhexmem(hex, strlen(hex) + 1, &mem, &len) == -EINVAL);
512 assert_se(unhexmem(hex_invalid, strlen(hex_invalid), &mem, &len) == -EINVAL);
514 assert_se((hex2 = hexmem(mem, len)));
518 assert_se(memcmp(hex, hex2, strlen(hex)) == 0);
522 assert_se(unhexmem(hex, strlen(hex) - 1, &mem, &len) == 0);
523 assert_se((hex2 = hexmem(mem, len)));
524 assert_se(memcmp(hex, hex2, strlen(hex) - 1) == 0);
527 /* https://tools.ietf.org/html/rfc4648#section-10 */
528 static void test_base32hexmem(void) {
531 b32 = base32hexmem("", strlen(""), true);
533 assert_se(streq(b32, ""));
536 b32 = base32hexmem("f", strlen("f"), true);
538 assert_se(streq(b32, "CO======"));
541 b32 = base32hexmem("fo", strlen("fo"), true);
543 assert_se(streq(b32, "CPNG===="));
546 b32 = base32hexmem("foo", strlen("foo"), true);
548 assert_se(streq(b32, "CPNMU==="));
551 b32 = base32hexmem("foob", strlen("foob"), true);
553 assert_se(streq(b32, "CPNMUOG="));
556 b32 = base32hexmem("fooba", strlen("fooba"), true);
558 assert_se(streq(b32, "CPNMUOJ1"));
561 b32 = base32hexmem("foobar", strlen("foobar"), true);
563 assert_se(streq(b32, "CPNMUOJ1E8======"));
566 b32 = base32hexmem("", strlen(""), false);
568 assert_se(streq(b32, ""));
571 b32 = base32hexmem("f", strlen("f"), false);
573 assert_se(streq(b32, "CO"));
576 b32 = base32hexmem("fo", strlen("fo"), false);
578 assert_se(streq(b32, "CPNG"));
581 b32 = base32hexmem("foo", strlen("foo"), false);
583 assert_se(streq(b32, "CPNMU"));
586 b32 = base32hexmem("foob", strlen("foob"), false);
588 assert_se(streq(b32, "CPNMUOG"));
591 b32 = base32hexmem("fooba", strlen("fooba"), false);
593 assert_se(streq(b32, "CPNMUOJ1"));
596 b32 = base32hexmem("foobar", strlen("foobar"), false);
598 assert_se(streq(b32, "CPNMUOJ1E8"));
602 static void test_unbase32hexmem(void) {
606 assert_se(unbase32hexmem("", strlen(""), true, &mem, &len) == 0);
607 assert_se(streq(strndupa(mem, len), ""));
610 assert_se(unbase32hexmem("CO======", strlen("CO======"), true, &mem, &len) == 0);
611 assert_se(streq(strndupa(mem, len), "f"));
614 assert_se(unbase32hexmem("CPNG====", strlen("CPNG===="), true, &mem, &len) == 0);
615 assert_se(streq(strndupa(mem, len), "fo"));
618 assert_se(unbase32hexmem("CPNMU===", strlen("CPNMU==="), true, &mem, &len) == 0);
619 assert_se(streq(strndupa(mem, len), "foo"));
622 assert_se(unbase32hexmem("CPNMUOG=", strlen("CPNMUOG="), true, &mem, &len) == 0);
623 assert_se(streq(strndupa(mem, len), "foob"));
626 assert_se(unbase32hexmem("CPNMUOJ1", strlen("CPNMUOJ1"), true, &mem, &len) == 0);
627 assert_se(streq(strndupa(mem, len), "fooba"));
630 assert_se(unbase32hexmem("CPNMUOJ1E8======", strlen("CPNMUOJ1E8======"), true, &mem, &len) == 0);
631 assert_se(streq(strndupa(mem, len), "foobar"));
634 assert_se(unbase32hexmem("A", strlen("A"), true, &mem, &len) == -EINVAL);
635 assert_se(unbase32hexmem("A=======", strlen("A======="), true, &mem, &len) == -EINVAL);
636 assert_se(unbase32hexmem("AAA=====", strlen("AAA====="), true, &mem, &len) == -EINVAL);
637 assert_se(unbase32hexmem("AAAAAA==", strlen("AAAAAA=="), true, &mem, &len) == -EINVAL);
638 assert_se(unbase32hexmem("AB======", strlen("AB======"), true, &mem, &len) == -EINVAL);
639 assert_se(unbase32hexmem("AAAB====", strlen("AAAB===="), true, &mem, &len) == -EINVAL);
640 assert_se(unbase32hexmem("AAAAB===", strlen("AAAAB==="), true, &mem, &len) == -EINVAL);
641 assert_se(unbase32hexmem("AAAAAAB=", strlen("AAAAAAB="), true, &mem, &len) == -EINVAL);
643 assert_se(unbase32hexmem("XPNMUOJ1", strlen("CPNMUOJ1"), true, &mem, &len) == -EINVAL);
644 assert_se(unbase32hexmem("CXNMUOJ1", strlen("CPNMUOJ1"), true, &mem, &len) == -EINVAL);
645 assert_se(unbase32hexmem("CPXMUOJ1", strlen("CPNMUOJ1"), true, &mem, &len) == -EINVAL);
646 assert_se(unbase32hexmem("CPNXUOJ1", strlen("CPNMUOJ1"), true, &mem, &len) == -EINVAL);
647 assert_se(unbase32hexmem("CPNMXOJ1", strlen("CPNMUOJ1"), true, &mem, &len) == -EINVAL);
648 assert_se(unbase32hexmem("CPNMUXJ1", strlen("CPNMUOJ1"), true, &mem, &len) == -EINVAL);
649 assert_se(unbase32hexmem("CPNMUOX1", strlen("CPNMUOJ1"), true, &mem, &len) == -EINVAL);
650 assert_se(unbase32hexmem("CPNMUOJX", strlen("CPNMUOJ1"), true, &mem, &len) == -EINVAL);
652 assert_se(unbase32hexmem("", strlen(""), false, &mem, &len) == 0);
653 assert_se(streq(strndupa(mem, len), ""));
656 assert_se(unbase32hexmem("CO", strlen("CO"), false, &mem, &len) == 0);
657 assert_se(streq(strndupa(mem, len), "f"));
660 assert_se(unbase32hexmem("CPNG", strlen("CPNG"), false, &mem, &len) == 0);
661 assert_se(streq(strndupa(mem, len), "fo"));
664 assert_se(unbase32hexmem("CPNMU", strlen("CPNMU"), false, &mem, &len) == 0);
665 assert_se(streq(strndupa(mem, len), "foo"));
668 assert_se(unbase32hexmem("CPNMUOG", strlen("CPNMUOG"), false, &mem, &len) == 0);
669 assert_se(streq(strndupa(mem, len), "foob"));
672 assert_se(unbase32hexmem("CPNMUOJ1", strlen("CPNMUOJ1"), false, &mem, &len) == 0);
673 assert_se(streq(strndupa(mem, len), "fooba"));
676 assert_se(unbase32hexmem("CPNMUOJ1E8", strlen("CPNMUOJ1E8"), false, &mem, &len) == 0);
677 assert_se(streq(strndupa(mem, len), "foobar"));
680 assert_se(unbase32hexmem("CPNMUOG=", strlen("CPNMUOG="), false, &mem, &len) == -EINVAL);
681 assert_se(unbase32hexmem("CPNMUOJ1E8======", strlen("CPNMUOJ1E8======"), false, &mem, &len) == -EINVAL);
682 assert_se(unbase32hexmem("A", strlen("A"), false, &mem, &len) == -EINVAL);
683 assert_se(unbase32hexmem("A", strlen("A"), false, &mem, &len) == -EINVAL);
684 assert_se(unbase32hexmem("AAA", strlen("AAA"), false, &mem, &len) == -EINVAL);
685 assert_se(unbase32hexmem("AAAAAA", strlen("AAAAAA"), false, &mem, &len) == -EINVAL);
686 assert_se(unbase32hexmem("AB", strlen("AB"), false, &mem, &len) == -EINVAL);
687 assert_se(unbase32hexmem("AAAB", strlen("AAAB"), false, &mem, &len) == -EINVAL);
688 assert_se(unbase32hexmem("AAAAB", strlen("AAAAB"), false, &mem, &len) == -EINVAL);
689 assert_se(unbase32hexmem("AAAAAAB", strlen("AAAAAAB"), false, &mem, &len) == -EINVAL);
692 /* https://tools.ietf.org/html/rfc4648#section-10 */
693 static void test_base64mem(void) {
696 b64 = base64mem("", strlen(""));
698 assert_se(streq(b64, ""));
701 b64 = base64mem("f", strlen("f"));
703 assert_se(streq(b64, "Zg=="));
706 b64 = base64mem("fo", strlen("fo"));
708 assert_se(streq(b64, "Zm8="));
711 b64 = base64mem("foo", strlen("foo"));
713 assert_se(streq(b64, "Zm9v"));
716 b64 = base64mem("foob", strlen("foob"));
718 assert_se(streq(b64, "Zm9vYg=="));
721 b64 = base64mem("fooba", strlen("fooba"));
723 assert_se(streq(b64, "Zm9vYmE="));
726 b64 = base64mem("foobar", strlen("foobar"));
728 assert_se(streq(b64, "Zm9vYmFy"));
732 static void test_unbase64mem(void) {
736 assert_se(unbase64mem("", strlen(""), &mem, &len) == 0);
737 assert_se(streq(strndupa(mem, len), ""));
740 assert_se(unbase64mem("Zg==", strlen("Zg=="), &mem, &len) == 0);
741 assert_se(streq(strndupa(mem, len), "f"));
744 assert_se(unbase64mem("Zm8=", strlen("Zm8="), &mem, &len) == 0);
745 assert_se(streq(strndupa(mem, len), "fo"));
748 assert_se(unbase64mem("Zm9v", strlen("Zm9v"), &mem, &len) == 0);
749 assert_se(streq(strndupa(mem, len), "foo"));
752 assert_se(unbase64mem("Zm9vYg==", strlen("Zm9vYg=="), &mem, &len) == 0);
753 assert_se(streq(strndupa(mem, len), "foob"));
756 assert_se(unbase64mem("Zm9vYmE=", strlen("Zm9vYmE="), &mem, &len) == 0);
757 assert_se(streq(strndupa(mem, len), "fooba"));
760 assert_se(unbase64mem("Zm9vYmFy", strlen("Zm9vYmFy"), &mem, &len) == 0);
761 assert_se(streq(strndupa(mem, len), "foobar"));
764 assert_se(unbase64mem("A", strlen("A"), &mem, &len) == -EINVAL);
765 assert_se(unbase64mem("A====", strlen("A===="), &mem, &len) == -EINVAL);
766 assert_se(unbase64mem("AAB==", strlen("AAB=="), &mem, &len) == -EINVAL);
767 assert_se(unbase64mem("AAAB=", strlen("AAAB="), &mem, &len) == -EINVAL);
770 static void test_cescape(void) {
771 _cleanup_free_ char *escaped;
773 assert_se(escaped = cescape("abc\\\"\b\f\n\r\t\v\a\003\177\234\313"));
774 assert_se(streq(escaped, "abc\\\\\\\"\\b\\f\\n\\r\\t\\v\\a\\003\\177\\234\\313"));
777 static void test_cunescape(void) {
778 _cleanup_free_ char *unescaped;
780 assert_se(cunescape("abc\\\\\\\"\\b\\f\\a\\n\\r\\t\\v\\003\\177\\234\\313\\000\\x00", 0, &unescaped) < 0);
781 assert_se(cunescape("abc\\\\\\\"\\b\\f\\a\\n\\r\\t\\v\\003\\177\\234\\313\\000\\x00", UNESCAPE_RELAX, &unescaped) >= 0);
782 assert_se(streq_ptr(unescaped, "abc\\\"\b\f\a\n\r\t\v\003\177\234\313\\000\\x00"));
783 unescaped = mfree(unescaped);
785 /* incomplete sequences */
786 assert_se(cunescape("\\x0", 0, &unescaped) < 0);
787 assert_se(cunescape("\\x0", UNESCAPE_RELAX, &unescaped) >= 0);
788 assert_se(streq_ptr(unescaped, "\\x0"));
789 unescaped = mfree(unescaped);
791 assert_se(cunescape("\\x", 0, &unescaped) < 0);
792 assert_se(cunescape("\\x", UNESCAPE_RELAX, &unescaped) >= 0);
793 assert_se(streq_ptr(unescaped, "\\x"));
794 unescaped = mfree(unescaped);
796 assert_se(cunescape("\\", 0, &unescaped) < 0);
797 assert_se(cunescape("\\", UNESCAPE_RELAX, &unescaped) >= 0);
798 assert_se(streq_ptr(unescaped, "\\"));
799 unescaped = mfree(unescaped);
801 assert_se(cunescape("\\11", 0, &unescaped) < 0);
802 assert_se(cunescape("\\11", UNESCAPE_RELAX, &unescaped) >= 0);
803 assert_se(streq_ptr(unescaped, "\\11"));
804 unescaped = mfree(unescaped);
806 assert_se(cunescape("\\1", 0, &unescaped) < 0);
807 assert_se(cunescape("\\1", UNESCAPE_RELAX, &unescaped) >= 0);
808 assert_se(streq_ptr(unescaped, "\\1"));
809 unescaped = mfree(unescaped);
811 assert_se(cunescape("\\u0000", 0, &unescaped) < 0);
812 assert_se(cunescape("\\u00DF\\U000000df\\u03a0\\U00000041", UNESCAPE_RELAX, &unescaped) >= 0);
813 assert_se(streq_ptr(unescaped, "ßßΠA"));
814 unescaped = mfree(unescaped);
816 assert_se(cunescape("\\073", 0, &unescaped) >= 0);
817 assert_se(streq_ptr(unescaped, ";"));
820 static void test_foreach_word(void) {
821 const char *word, *state;
824 const char test[] = "test abc d\te f ";
825 const char * const expected[] = {
835 FOREACH_WORD(word, l, test, state)
836 assert_se(strneq(expected[i++], word, l));
839 static void check(const char *test, char** expected, bool trailing) {
840 const char *word, *state;
844 printf("<<<%s>>>\n", test);
845 FOREACH_WORD_QUOTED(word, l, test, state) {
846 _cleanup_free_ char *t = NULL;
848 assert_se(t = strndup(word, l));
849 assert_se(strneq(expected[i++], word, l));
852 printf("<<<%s>>>\n", state);
853 assert_se(expected[i] == NULL);
854 assert_se(isempty(state) == !trailing);
857 static void test_foreach_word_quoted(void) {
858 check("test a b c 'd' e '' '' hhh '' '' \"a b c\"",
882 static void test_memdup_multiply(void) {
883 int org[] = {1, 2, 3};
886 dup = (int*)memdup_multiply(org, sizeof(int), 3);
889 assert_se(dup[0] == 1);
890 assert_se(dup[1] == 2);
891 assert_se(dup[2] == 3);
895 static void test_u64log2(void) {
896 assert_se(u64log2(0) == 0);
897 assert_se(u64log2(8) == 3);
898 assert_se(u64log2(9) == 3);
899 assert_se(u64log2(15) == 3);
900 assert_se(u64log2(16) == 4);
901 assert_se(u64log2(1024*1024) == 20);
902 assert_se(u64log2(1024*1024+5) == 20);
905 static void test_protect_errno(void) {
911 assert_se(errno == 12);
914 static void test_parse_size(void) {
917 assert_se(parse_size("111", 1024, &bytes) == 0);
918 assert_se(bytes == 111);
920 assert_se(parse_size("111.4", 1024, &bytes) == 0);
921 assert_se(bytes == 111);
923 assert_se(parse_size(" 112 B", 1024, &bytes) == 0);
924 assert_se(bytes == 112);
926 assert_se(parse_size(" 112.6 B", 1024, &bytes) == 0);
927 assert_se(bytes == 112);
929 assert_se(parse_size("3.5 K", 1024, &bytes) == 0);
930 assert_se(bytes == 3*1024 + 512);
932 assert_se(parse_size("3. K", 1024, &bytes) == 0);
933 assert_se(bytes == 3*1024);
935 assert_se(parse_size("3.0 K", 1024, &bytes) == 0);
936 assert_se(bytes == 3*1024);
938 assert_se(parse_size("3. 0 K", 1024, &bytes) == -EINVAL);
940 assert_se(parse_size(" 4 M 11.5K", 1024, &bytes) == 0);
941 assert_se(bytes == 4*1024*1024 + 11 * 1024 + 512);
943 assert_se(parse_size("3B3.5G", 1024, &bytes) == -EINVAL);
945 assert_se(parse_size("3.5G3B", 1024, &bytes) == 0);
946 assert_se(bytes == 3ULL*1024*1024*1024 + 512*1024*1024 + 3);
948 assert_se(parse_size("3.5G 4B", 1024, &bytes) == 0);
949 assert_se(bytes == 3ULL*1024*1024*1024 + 512*1024*1024 + 4);
951 assert_se(parse_size("3B3G4T", 1024, &bytes) == -EINVAL);
953 assert_se(parse_size("4T3G3B", 1024, &bytes) == 0);
954 assert_se(bytes == (4ULL*1024 + 3)*1024*1024*1024 + 3);
956 assert_se(parse_size(" 4 T 3 G 3 B", 1024, &bytes) == 0);
957 assert_se(bytes == (4ULL*1024 + 3)*1024*1024*1024 + 3);
959 assert_se(parse_size("12P", 1024, &bytes) == 0);
960 assert_se(bytes == 12ULL * 1024*1024*1024*1024*1024);
962 assert_se(parse_size("12P12P", 1024, &bytes) == -EINVAL);
964 assert_se(parse_size("3E 2P", 1024, &bytes) == 0);
965 assert_se(bytes == (3 * 1024 + 2ULL) * 1024*1024*1024*1024*1024);
967 assert_se(parse_size("12X", 1024, &bytes) == -EINVAL);
969 assert_se(parse_size("12.5X", 1024, &bytes) == -EINVAL);
971 assert_se(parse_size("12.5e3", 1024, &bytes) == -EINVAL);
973 assert_se(parse_size("1024E", 1024, &bytes) == -ERANGE);
974 assert_se(parse_size("-1", 1024, &bytes) == -ERANGE);
975 assert_se(parse_size("-1024E", 1024, &bytes) == -ERANGE);
977 assert_se(parse_size("-1024P", 1024, &bytes) == -ERANGE);
979 assert_se(parse_size("-10B 20K", 1024, &bytes) == -ERANGE);
982 static void test_parse_range(void) {
983 unsigned lower, upper;
985 /* Successful cases */
986 assert_se(parse_range("111", &lower, &upper) == 0);
987 assert_se(lower == 111);
988 assert_se(upper == 111);
990 assert_se(parse_range("111-123", &lower, &upper) == 0);
991 assert_se(lower == 111);
992 assert_se(upper == 123);
994 assert_se(parse_range("123-111", &lower, &upper) == 0);
995 assert_se(lower == 123);
996 assert_se(upper == 111);
998 assert_se(parse_range("123-123", &lower, &upper) == 0);
999 assert_se(lower == 123);
1000 assert_se(upper == 123);
1002 assert_se(parse_range("0", &lower, &upper) == 0);
1003 assert_se(lower == 0);
1004 assert_se(upper == 0);
1006 assert_se(parse_range("0-15", &lower, &upper) == 0);
1007 assert_se(lower == 0);
1008 assert_se(upper == 15);
1010 assert_se(parse_range("15-0", &lower, &upper) == 0);
1011 assert_se(lower == 15);
1012 assert_se(upper == 0);
1014 assert_se(parse_range("128-65535", &lower, &upper) == 0);
1015 assert_se(lower == 128);
1016 assert_se(upper == 65535);
1018 assert_se(parse_range("1024-4294967295", &lower, &upper) == 0);
1019 assert_se(lower == 1024);
1020 assert_se(upper == 4294967295);
1022 /* Leading whitespace is acceptable */
1023 assert_se(parse_range(" 111", &lower, &upper) == 0);
1024 assert_se(lower == 111);
1025 assert_se(upper == 111);
1027 assert_se(parse_range(" 111-123", &lower, &upper) == 0);
1028 assert_se(lower == 111);
1029 assert_se(upper == 123);
1031 assert_se(parse_range("111- 123", &lower, &upper) == 0);
1032 assert_se(lower == 111);
1033 assert_se(upper == 123);
1035 assert_se(parse_range("\t111-\t123", &lower, &upper) == 0);
1036 assert_se(lower == 111);
1037 assert_se(upper == 123);
1039 assert_se(parse_range(" \t 111- \t 123", &lower, &upper) == 0);
1040 assert_se(lower == 111);
1041 assert_se(upper == 123);
1043 /* Error cases, make sure they fail as expected */
1044 lower = upper = 9999;
1045 assert_se(parse_range("111garbage", &lower, &upper) == -EINVAL);
1046 assert_se(lower == 9999);
1047 assert_se(upper == 9999);
1049 assert_se(parse_range("garbage111", &lower, &upper) == -EINVAL);
1050 assert_se(lower == 9999);
1051 assert_se(upper == 9999);
1053 assert_se(parse_range("garbage", &lower, &upper) == -EINVAL);
1054 assert_se(lower == 9999);
1055 assert_se(upper == 9999);
1057 assert_se(parse_range("111-123garbage", &lower, &upper) == -EINVAL);
1058 assert_se(lower == 9999);
1059 assert_se(upper == 9999);
1061 assert_se(parse_range("111garbage-123", &lower, &upper) == -EINVAL);
1062 assert_se(lower == 9999);
1063 assert_se(upper == 9999);
1066 lower = upper = 9999;
1067 assert_se(parse_range("", &lower, &upper) == -EINVAL);
1068 assert_se(lower == 9999);
1069 assert_se(upper == 9999);
1071 /* 111--123 will pass -123 to safe_atou which returns -ERANGE for negative */
1072 assert_se(parse_range("111--123", &lower, &upper) == -ERANGE);
1073 assert_se(lower == 9999);
1074 assert_se(upper == 9999);
1076 assert_se(parse_range("-111-123", &lower, &upper) == -EINVAL);
1077 assert_se(lower == 9999);
1078 assert_se(upper == 9999);
1080 assert_se(parse_range("111-123-", &lower, &upper) == -EINVAL);
1081 assert_se(lower == 9999);
1082 assert_se(upper == 9999);
1084 assert_se(parse_range("111.4-123", &lower, &upper) == -EINVAL);
1085 assert_se(lower == 9999);
1086 assert_se(upper == 9999);
1088 assert_se(parse_range("111-123.4", &lower, &upper) == -EINVAL);
1089 assert_se(lower == 9999);
1090 assert_se(upper == 9999);
1092 assert_se(parse_range("111,4-123", &lower, &upper) == -EINVAL);
1093 assert_se(lower == 9999);
1094 assert_se(upper == 9999);
1096 assert_se(parse_range("111-123,4", &lower, &upper) == -EINVAL);
1097 assert_se(lower == 9999);
1098 assert_se(upper == 9999);
1100 /* Error on trailing dash */
1101 assert_se(parse_range("111-", &lower, &upper) == -EINVAL);
1102 assert_se(lower == 9999);
1103 assert_se(upper == 9999);
1105 assert_se(parse_range("111-123-", &lower, &upper) == -EINVAL);
1106 assert_se(lower == 9999);
1107 assert_se(upper == 9999);
1109 assert_se(parse_range("111--", &lower, &upper) == -EINVAL);
1110 assert_se(lower == 9999);
1111 assert_se(upper == 9999);
1113 assert_se(parse_range("111- ", &lower, &upper) == -EINVAL);
1114 assert_se(lower == 9999);
1115 assert_se(upper == 9999);
1117 /* Whitespace is not a separator */
1118 assert_se(parse_range("111 123", &lower, &upper) == -EINVAL);
1119 assert_se(lower == 9999);
1120 assert_se(upper == 9999);
1122 assert_se(parse_range("111\t123", &lower, &upper) == -EINVAL);
1123 assert_se(lower == 9999);
1124 assert_se(upper == 9999);
1126 assert_se(parse_range("111 \t 123", &lower, &upper) == -EINVAL);
1127 assert_se(lower == 9999);
1128 assert_se(upper == 9999);
1130 /* Trailing whitespace is invalid (from safe_atou) */
1131 assert_se(parse_range("111 ", &lower, &upper) == -EINVAL);
1132 assert_se(lower == 9999);
1133 assert_se(upper == 9999);
1135 assert_se(parse_range("111-123 ", &lower, &upper) == -EINVAL);
1136 assert_se(lower == 9999);
1137 assert_se(upper == 9999);
1139 assert_se(parse_range("111 -123", &lower, &upper) == -EINVAL);
1140 assert_se(lower == 9999);
1141 assert_se(upper == 9999);
1143 assert_se(parse_range("111 -123 ", &lower, &upper) == -EINVAL);
1144 assert_se(lower == 9999);
1145 assert_se(upper == 9999);
1147 assert_se(parse_range("111\t-123\t", &lower, &upper) == -EINVAL);
1148 assert_se(lower == 9999);
1149 assert_se(upper == 9999);
1151 assert_se(parse_range("111 \t -123 \t ", &lower, &upper) == -EINVAL);
1152 assert_se(lower == 9999);
1153 assert_se(upper == 9999);
1155 /* Out of the "unsigned" range, this is 1<<64 */
1156 assert_se(parse_range("0-18446744073709551616", &lower, &upper) == -ERANGE);
1157 assert_se(lower == 9999);
1158 assert_se(upper == 9999);
1161 static void test_parse_cpu_set(void) {
1162 cpu_set_t *c = NULL;
1166 /* Simple range (from CPUAffinity example) */
1167 ncpus = parse_cpu_set_and_warn("1 2", &c, NULL, "fake", 1, "CPUAffinity");
1168 assert_se(ncpus >= 1024);
1169 assert_se(CPU_ISSET_S(1, CPU_ALLOC_SIZE(ncpus), c));
1170 assert_se(CPU_ISSET_S(2, CPU_ALLOC_SIZE(ncpus), c));
1171 assert_se(CPU_COUNT_S(CPU_ALLOC_SIZE(ncpus), c) == 2);
1174 /* A more interesting range */
1175 ncpus = parse_cpu_set_and_warn("0 1 2 3 8 9 10 11", &c, NULL, "fake", 1, "CPUAffinity");
1176 assert_se(ncpus >= 1024);
1177 assert_se(CPU_COUNT_S(CPU_ALLOC_SIZE(ncpus), c) == 8);
1178 for (cpu = 0; cpu < 4; cpu++)
1179 assert_se(CPU_ISSET_S(cpu, CPU_ALLOC_SIZE(ncpus), c));
1180 for (cpu = 8; cpu < 12; cpu++)
1181 assert_se(CPU_ISSET_S(cpu, CPU_ALLOC_SIZE(ncpus), c));
1184 /* Quoted strings */
1185 ncpus = parse_cpu_set_and_warn("8 '9' 10 \"11\"", &c, NULL, "fake", 1, "CPUAffinity");
1186 assert_se(ncpus >= 1024);
1187 assert_se(CPU_COUNT_S(CPU_ALLOC_SIZE(ncpus), c) == 4);
1188 for (cpu = 8; cpu < 12; cpu++)
1189 assert_se(CPU_ISSET_S(cpu, CPU_ALLOC_SIZE(ncpus), c));
1192 /* Use commas as separators */
1193 ncpus = parse_cpu_set_and_warn("0,1,2,3 8,9,10,11", &c, NULL, "fake", 1, "CPUAffinity");
1194 assert_se(ncpus >= 1024);
1195 assert_se(CPU_COUNT_S(CPU_ALLOC_SIZE(ncpus), c) == 8);
1196 for (cpu = 0; cpu < 4; cpu++)
1197 assert_se(CPU_ISSET_S(cpu, CPU_ALLOC_SIZE(ncpus), c));
1198 for (cpu = 8; cpu < 12; cpu++)
1199 assert_se(CPU_ISSET_S(cpu, CPU_ALLOC_SIZE(ncpus), c));
1202 /* Commas with spaces (and trailing comma, space) */
1203 ncpus = parse_cpu_set_and_warn("0, 1, 2, 3, 4, 5, 6, 7, ", &c, NULL, "fake", 1, "CPUAffinity");
1204 assert_se(ncpus >= 1024);
1205 assert_se(CPU_COUNT_S(CPU_ALLOC_SIZE(ncpus), c) == 8);
1206 for (cpu = 0; cpu < 8; cpu++)
1207 assert_se(CPU_ISSET_S(cpu, CPU_ALLOC_SIZE(ncpus), c));
1211 ncpus = parse_cpu_set_and_warn("0-3,8-11", &c, NULL, "fake", 1, "CPUAffinity");
1212 assert_se(ncpus >= 1024);
1213 assert_se(CPU_COUNT_S(CPU_ALLOC_SIZE(ncpus), c) == 8);
1214 for (cpu = 0; cpu < 4; cpu++)
1215 assert_se(CPU_ISSET_S(cpu, CPU_ALLOC_SIZE(ncpus), c));
1216 for (cpu = 8; cpu < 12; cpu++)
1217 assert_se(CPU_ISSET_S(cpu, CPU_ALLOC_SIZE(ncpus), c));
1220 /* Ranges with trailing comma, space */
1221 ncpus = parse_cpu_set_and_warn("0-3 8-11, ", &c, NULL, "fake", 1, "CPUAffinity");
1222 assert_se(ncpus >= 1024);
1223 assert_se(CPU_COUNT_S(CPU_ALLOC_SIZE(ncpus), c) == 8);
1224 for (cpu = 0; cpu < 4; cpu++)
1225 assert_se(CPU_ISSET_S(cpu, CPU_ALLOC_SIZE(ncpus), c));
1226 for (cpu = 8; cpu < 12; cpu++)
1227 assert_se(CPU_ISSET_S(cpu, CPU_ALLOC_SIZE(ncpus), c));
1230 /* Negative range (returns empty cpu_set) */
1231 ncpus = parse_cpu_set_and_warn("3-0", &c, NULL, "fake", 1, "CPUAffinity");
1232 assert_se(ncpus >= 1024);
1233 assert_se(CPU_COUNT_S(CPU_ALLOC_SIZE(ncpus), c) == 0);
1236 /* Overlapping ranges */
1237 ncpus = parse_cpu_set_and_warn("0-7 4-11", &c, NULL, "fake", 1, "CPUAffinity");
1238 assert_se(ncpus >= 1024);
1239 assert_se(CPU_COUNT_S(CPU_ALLOC_SIZE(ncpus), c) == 12);
1240 for (cpu = 0; cpu < 12; cpu++)
1241 assert_se(CPU_ISSET_S(cpu, CPU_ALLOC_SIZE(ncpus), c));
1244 /* Mix ranges and individual CPUs */
1245 ncpus = parse_cpu_set_and_warn("0,1 4-11", &c, NULL, "fake", 1, "CPUAffinity");
1246 assert_se(ncpus >= 1024);
1247 assert_se(CPU_COUNT_S(CPU_ALLOC_SIZE(ncpus), c) == 10);
1248 assert_se(CPU_ISSET_S(0, CPU_ALLOC_SIZE(ncpus), c));
1249 assert_se(CPU_ISSET_S(1, CPU_ALLOC_SIZE(ncpus), c));
1250 for (cpu = 4; cpu < 12; cpu++)
1251 assert_se(CPU_ISSET_S(cpu, CPU_ALLOC_SIZE(ncpus), c));
1255 ncpus = parse_cpu_set_and_warn("0 1 2 3 garbage", &c, NULL, "fake", 1, "CPUAffinity");
1256 assert_se(ncpus < 0);
1259 /* Range with garbage */
1260 ncpus = parse_cpu_set_and_warn("0-3 8-garbage", &c, NULL, "fake", 1, "CPUAffinity");
1261 assert_se(ncpus < 0);
1266 ncpus = parse_cpu_set_and_warn("", &c, NULL, "fake", 1, "CPUAffinity");
1267 assert_se(ncpus == 0); /* empty string returns 0 */
1270 /* Runnaway quoted string */
1271 ncpus = parse_cpu_set_and_warn("0 1 2 3 \"4 5 6 7 ", &c, NULL, "fake", 1, "CPUAffinity");
1272 assert_se(ncpus < 0);
1276 static void test_config_parse_iec_uint64(void) {
1277 uint64_t offset = 0;
1278 assert_se(config_parse_iec_uint64(NULL, "/this/file", 11, "Section", 22, "Size", 0, "4M", &offset, NULL) == 0);
1279 assert_se(offset == 4 * 1024 * 1024);
1281 assert_se(config_parse_iec_uint64(NULL, "/this/file", 11, "Section", 22, "Size", 0, "4.5M", &offset, NULL) == 0);
1284 static void test_strextend(void) {
1285 _cleanup_free_ char *str = strdup("0123");
1286 strextend(&str, "456", "78", "9", NULL);
1287 assert_se(streq(str, "0123456789"));
1290 static void test_strrep(void) {
1291 _cleanup_free_ char *one, *three, *zero;
1292 one = strrep("waldo", 1);
1293 three = strrep("waldo", 3);
1294 zero = strrep("waldo", 0);
1296 assert_se(streq(one, "waldo"));
1297 assert_se(streq(three, "waldowaldowaldo"));
1298 assert_se(streq(zero, ""));
1301 static void test_split_pair(void) {
1302 _cleanup_free_ char *a = NULL, *b = NULL;
1304 assert_se(split_pair("", "", &a, &b) == -EINVAL);
1305 assert_se(split_pair("foo=bar", "", &a, &b) == -EINVAL);
1306 assert_se(split_pair("", "=", &a, &b) == -EINVAL);
1307 assert_se(split_pair("foo=bar", "=", &a, &b) >= 0);
1308 assert_se(streq(a, "foo"));
1309 assert_se(streq(b, "bar"));
1312 assert_se(split_pair("==", "==", &a, &b) >= 0);
1313 assert_se(streq(a, ""));
1314 assert_se(streq(b, ""));
1318 assert_se(split_pair("===", "==", &a, &b) >= 0);
1319 assert_se(streq(a, ""));
1320 assert_se(streq(b, "="));
1323 static void test_fstab_node_to_udev_node(void) {
1326 n = fstab_node_to_udev_node("LABEL=applé/jack");
1328 assert_se(streq(n, "/dev/disk/by-label/applé\\x2fjack"));
1331 n = fstab_node_to_udev_node("PARTLABEL=pinkié pie");
1333 assert_se(streq(n, "/dev/disk/by-partlabel/pinkié\\x20pie"));
1336 n = fstab_node_to_udev_node("UUID=037b9d94-148e-4ee4-8d38-67bfe15bb535");
1338 assert_se(streq(n, "/dev/disk/by-uuid/037b9d94-148e-4ee4-8d38-67bfe15bb535"));
1341 n = fstab_node_to_udev_node("PARTUUID=037b9d94-148e-4ee4-8d38-67bfe15bb535");
1343 assert_se(streq(n, "/dev/disk/by-partuuid/037b9d94-148e-4ee4-8d38-67bfe15bb535"));
1346 n = fstab_node_to_udev_node("PONIES=awesome");
1348 assert_se(streq(n, "PONIES=awesome"));
1351 n = fstab_node_to_udev_node("/dev/xda1");
1353 assert_se(streq(n, "/dev/xda1"));
1357 static void test_get_files_in_directory(void) {
1358 _cleanup_strv_free_ char **l = NULL, **t = NULL;
1360 assert_se(get_files_in_directory("/tmp", &l) >= 0);
1361 assert_se(get_files_in_directory(".", &t) >= 0);
1362 assert_se(get_files_in_directory(".", NULL) >= 0);
1365 static void test_in_set(void) {
1366 assert_se(IN_SET(1, 1));
1367 assert_se(IN_SET(1, 1, 2, 3, 4));
1368 assert_se(IN_SET(2, 1, 2, 3, 4));
1369 assert_se(IN_SET(3, 1, 2, 3, 4));
1370 assert_se(IN_SET(4, 1, 2, 3, 4));
1371 assert_se(!IN_SET(0, 1));
1372 assert_se(!IN_SET(0, 1, 2, 3, 4));
1375 static void test_writing_tmpfile(void) {
1376 char name[] = "/tmp/test-systemd_writing_tmpfile.XXXXXX";
1377 _cleanup_free_ char *contents = NULL;
1380 struct iovec iov[3];
1382 IOVEC_SET_STRING(iov[0], "abc\n");
1383 IOVEC_SET_STRING(iov[1], ALPHANUMERICAL "\n");
1384 IOVEC_SET_STRING(iov[2], "");
1386 fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
1387 printf("tmpfile: %s", name);
1389 r = writev(fd, iov, 3);
1392 r = read_full_file(name, &contents, &size);
1394 printf("contents: %s", contents);
1395 assert_se(streq(contents, "abc\n" ALPHANUMERICAL "\n"));
1400 static void test_hexdump(void) {
1404 hexdump(stdout, NULL, 0);
1405 hexdump(stdout, "", 0);
1406 hexdump(stdout, "", 1);
1407 hexdump(stdout, "x", 1);
1408 hexdump(stdout, "x", 2);
1409 hexdump(stdout, "foobar", 7);
1410 hexdump(stdout, "f\nobar", 7);
1411 hexdump(stdout, "xxxxxxxxxxxxxxxxxxxxyz", 23);
1413 for (i = 0; i < ELEMENTSOF(data); i++)
1416 hexdump(stdout, data, sizeof(data));
1419 static void test_log2i(void) {
1420 assert_se(log2i(1) == 0);
1421 assert_se(log2i(2) == 1);
1422 assert_se(log2i(3) == 1);
1423 assert_se(log2i(4) == 2);
1424 assert_se(log2i(32) == 5);
1425 assert_se(log2i(33) == 5);
1426 assert_se(log2i(63) == 5);
1427 assert_se(log2i(INT_MAX) == sizeof(int)*8-2);
1430 static void test_foreach_string(void) {
1431 const char * const t[] = {
1440 FOREACH_STRING(x, "foo", "bar", "waldo")
1441 assert_se(streq_ptr(t[i++], x));
1445 FOREACH_STRING(x, "zzz")
1446 assert_se(streq(x, "zzz"));
1449 static void test_filename_is_valid(void) {
1450 char foo[FILENAME_MAX+2];
1453 assert_se(!filename_is_valid(""));
1454 assert_se(!filename_is_valid("/bar/foo"));
1455 assert_se(!filename_is_valid("/"));
1456 assert_se(!filename_is_valid("."));
1457 assert_se(!filename_is_valid(".."));
1459 for (i=0; i<FILENAME_MAX+1; i++)
1461 foo[FILENAME_MAX+1] = '\0';
1463 assert_se(!filename_is_valid(foo));
1465 assert_se(filename_is_valid("foo_bar-333"));
1466 assert_se(filename_is_valid("o.o"));
1469 static void test_string_has_cc(void) {
1470 assert_se(string_has_cc("abc\1", NULL));
1471 assert_se(string_has_cc("abc\x7f", NULL));
1472 assert_se(string_has_cc("abc\x7f", NULL));
1473 assert_se(string_has_cc("abc\t\x7f", "\t"));
1474 assert_se(string_has_cc("abc\t\x7f", "\t"));
1475 assert_se(string_has_cc("\x7f", "\t"));
1476 assert_se(string_has_cc("\x7f", "\t\a"));
1478 assert_se(!string_has_cc("abc\t\t", "\t"));
1479 assert_se(!string_has_cc("abc\t\t\a", "\t\a"));
1480 assert_se(!string_has_cc("a\ab\tc", "\t\a"));
1483 static void test_ascii_strlower(void) {
1484 char a[] = "AabBcC Jk Ii Od LKJJJ kkd LK";
1485 assert_se(streq(ascii_strlower(a), "aabbcc jk ii od lkjjj kkd lk"));
1488 static void test_files_same(void) {
1489 _cleanup_close_ int fd = -1;
1490 char name[] = "/tmp/test-files_same.XXXXXX";
1491 char name_alias[] = "/tmp/test-files_same.alias";
1493 fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
1495 assert_se(symlink(name, name_alias) >= 0);
1497 assert_se(files_same(name, name));
1498 assert_se(files_same(name, name_alias));
1504 static void test_is_valid_documentation_url(void) {
1505 assert_se(documentation_url_is_valid("http://www.freedesktop.org/wiki/Software/systemd"));
1506 assert_se(documentation_url_is_valid("https://www.kernel.org/doc/Documentation/binfmt_misc.txt"));
1507 assert_se(documentation_url_is_valid("file:/foo/foo"));
1508 assert_se(documentation_url_is_valid("man:systemd.special(7)"));
1509 assert_se(documentation_url_is_valid("info:bar"));
1511 assert_se(!documentation_url_is_valid("foo:"));
1512 assert_se(!documentation_url_is_valid("info:"));
1513 assert_se(!documentation_url_is_valid(""));
1516 static void test_file_in_same_dir(void) {
1519 t = file_in_same_dir("/", "a");
1520 assert_se(streq(t, "/a"));
1523 t = file_in_same_dir("/", "/a");
1524 assert_se(streq(t, "/a"));
1527 t = file_in_same_dir("", "a");
1528 assert_se(streq(t, "a"));
1531 t = file_in_same_dir("a/", "a");
1532 assert_se(streq(t, "a/a"));
1535 t = file_in_same_dir("bar/foo", "bar");
1536 assert_se(streq(t, "bar/bar"));
1540 static void test_endswith(void) {
1541 assert_se(endswith("foobar", "bar"));
1542 assert_se(endswith("foobar", ""));
1543 assert_se(endswith("foobar", "foobar"));
1544 assert_se(endswith("", ""));
1546 assert_se(!endswith("foobar", "foo"));
1547 assert_se(!endswith("foobar", "foobarfoofoo"));
1550 static void test_endswith_no_case(void) {
1551 assert_se(endswith_no_case("fooBAR", "bar"));
1552 assert_se(endswith_no_case("foobar", ""));
1553 assert_se(endswith_no_case("foobar", "FOOBAR"));
1554 assert_se(endswith_no_case("", ""));
1556 assert_se(!endswith_no_case("foobar", "FOO"));
1557 assert_se(!endswith_no_case("foobar", "FOOBARFOOFOO"));
1560 static void test_close_nointr(void) {
1561 char name[] = "/tmp/test-test-close_nointr.XXXXXX";
1564 fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
1566 assert_se(close_nointr(fd) >= 0);
1567 assert_se(close_nointr(fd) < 0);
1573 static void test_unlink_noerrno(void) {
1574 char name[] = "/tmp/test-close_nointr.XXXXXX";
1577 fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
1579 assert_se(close_nointr(fd) >= 0);
1584 assert_se(unlink_noerrno(name) >= 0);
1585 assert_se(errno == -42);
1586 assert_se(unlink_noerrno(name) < 0);
1587 assert_se(errno == -42);
1591 static void test_readlink_and_make_absolute(void) {
1592 char tempdir[] = "/tmp/test-readlink_and_make_absolute";
1593 char name[] = "/tmp/test-readlink_and_make_absolute/original";
1594 char name2[] = "test-readlink_and_make_absolute/original";
1595 char name_alias[] = "/tmp/test-readlink_and_make_absolute-alias";
1598 assert_se(mkdir_safe(tempdir, 0755, getuid(), getgid()) >= 0);
1599 assert_se(touch(name) >= 0);
1601 assert_se(symlink(name, name_alias) >= 0);
1602 assert_se(readlink_and_make_absolute(name_alias, &r) >= 0);
1603 assert_se(streq(r, name));
1605 assert_se(unlink(name_alias) >= 0);
1607 assert_se(chdir(tempdir) >= 0);
1608 assert_se(symlink(name2, name_alias) >= 0);
1609 assert_se(readlink_and_make_absolute(name_alias, &r) >= 0);
1610 assert_se(streq(r, name));
1612 assert_se(unlink(name_alias) >= 0);
1614 assert_se(rm_rf(tempdir, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0);
1617 static void test_ignore_signals(void) {
1618 assert_se(ignore_signals(SIGINT, -1) >= 0);
1619 assert_se(kill(getpid(), SIGINT) >= 0);
1620 assert_se(ignore_signals(SIGUSR1, SIGUSR2, SIGTERM, SIGPIPE, -1) >= 0);
1621 assert_se(kill(getpid(), SIGUSR1) >= 0);
1622 assert_se(kill(getpid(), SIGUSR2) >= 0);
1623 assert_se(kill(getpid(), SIGTERM) >= 0);
1624 assert_se(kill(getpid(), SIGPIPE) >= 0);
1625 assert_se(default_signals(SIGINT, SIGUSR1, SIGUSR2, SIGTERM, SIGPIPE, -1) >= 0);
1628 static void test_strshorten(void) {
1629 char s[] = "foobar";
1631 assert_se(strlen(strshorten(s, 6)) == 6);
1632 assert_se(strlen(strshorten(s, 12)) == 6);
1633 assert_se(strlen(strshorten(s, 2)) == 2);
1634 assert_se(strlen(strshorten(s, 0)) == 0);
1637 static void test_strjoina(void) {
1640 actual = strjoina("", "foo", "bar");
1641 assert_se(streq(actual, "foobar"));
1643 actual = strjoina("foo", "bar", "baz");
1644 assert_se(streq(actual, "foobarbaz"));
1646 actual = strjoina("foo", "", "bar", "baz");
1647 assert_se(streq(actual, "foobarbaz"));
1649 actual = strjoina("foo");
1650 assert_se(streq(actual, "foo"));
1652 actual = strjoina(NULL);
1653 assert_se(streq(actual, ""));
1655 actual = strjoina(NULL, "foo");
1656 assert_se(streq(actual, ""));
1658 actual = strjoina("foo", NULL, "bar");
1659 assert_se(streq(actual, "foo"));
1662 static void test_is_symlink(void) {
1663 char name[] = "/tmp/test-is_symlink.XXXXXX";
1664 char name_link[] = "/tmp/test-is_symlink.link";
1665 _cleanup_close_ int fd = -1;
1667 fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
1669 assert_se(symlink(name, name_link) >= 0);
1671 assert_se(is_symlink(name) == 0);
1672 assert_se(is_symlink(name_link) == 1);
1673 assert_se(is_symlink("/a/file/which/does/not/exist/i/guess") < 0);
1680 static void test_search_and_fopen(void) {
1681 const char *dirs[] = {"/tmp/foo/bar", "/tmp", NULL};
1682 char name[] = "/tmp/test-search_and_fopen.XXXXXX";
1687 fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
1691 r = search_and_fopen(basename(name), "r", NULL, dirs, &f);
1695 r = search_and_fopen(name, "r", NULL, dirs, &f);
1699 r = search_and_fopen(basename(name), "r", "/", dirs, &f);
1703 r = search_and_fopen("/a/file/which/does/not/exist/i/guess", "r", NULL, dirs, &f);
1705 r = search_and_fopen("afilewhichdoesnotexistiguess", "r", NULL, dirs, &f);
1711 r = search_and_fopen(basename(name), "r", NULL, dirs, &f);
1716 static void test_search_and_fopen_nulstr(void) {
1717 const char dirs[] = "/tmp/foo/bar\0/tmp\0";
1718 char name[] = "/tmp/test-search_and_fopen.XXXXXX";
1723 fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
1727 r = search_and_fopen_nulstr(basename(name), "r", NULL, dirs, &f);
1731 r = search_and_fopen_nulstr(name, "r", NULL, dirs, &f);
1735 r = search_and_fopen_nulstr("/a/file/which/does/not/exist/i/guess", "r", NULL, dirs, &f);
1737 r = search_and_fopen_nulstr("afilewhichdoesnotexistiguess", "r", NULL, dirs, &f);
1743 r = search_and_fopen_nulstr(basename(name), "r", NULL, dirs, &f);
1747 static void test_glob_exists(void) {
1748 char name[] = "/tmp/test-glob_exists.XXXXXX";
1752 fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
1756 r = glob_exists("/tmp/test-glob_exists*");
1761 r = glob_exists("/tmp/test-glob_exists*");
1765 static void test_execute_directory(void) {
1766 char template_lo[] = "/tmp/test-readlink_and_make_absolute-lo.XXXXXXX";
1767 char template_hi[] = "/tmp/test-readlink_and_make_absolute-hi.XXXXXXX";
1768 const char * dirs[] = {template_hi, template_lo, NULL};
1769 const char *name, *name2, *name3, *overridden, *override, *masked, *mask;
1771 assert_se(mkdtemp(template_lo));
1772 assert_se(mkdtemp(template_hi));
1774 name = strjoina(template_lo, "/script");
1775 name2 = strjoina(template_hi, "/script2");
1776 name3 = strjoina(template_lo, "/useless");
1777 overridden = strjoina(template_lo, "/overridden");
1778 override = strjoina(template_hi, "/overridden");
1779 masked = strjoina(template_lo, "/masked");
1780 mask = strjoina(template_hi, "/masked");
1782 assert_se(write_string_file(name, "#!/bin/sh\necho 'Executing '$0\ntouch $(dirname $0)/it_works", WRITE_STRING_FILE_CREATE) == 0);
1783 assert_se(write_string_file(name2, "#!/bin/sh\necho 'Executing '$0\ntouch $(dirname $0)/it_works2", WRITE_STRING_FILE_CREATE) == 0);
1784 assert_se(write_string_file(overridden, "#!/bin/sh\necho 'Executing '$0\ntouch $(dirname $0)/failed", WRITE_STRING_FILE_CREATE) == 0);
1785 assert_se(write_string_file(override, "#!/bin/sh\necho 'Executing '$0", WRITE_STRING_FILE_CREATE) == 0);
1786 assert_se(write_string_file(masked, "#!/bin/sh\necho 'Executing '$0\ntouch $(dirname $0)/failed", WRITE_STRING_FILE_CREATE) == 0);
1787 assert_se(symlink("/dev/null", mask) == 0);
1788 assert_se(chmod(name, 0755) == 0);
1789 assert_se(chmod(name2, 0755) == 0);
1790 assert_se(chmod(overridden, 0755) == 0);
1791 assert_se(chmod(override, 0755) == 0);
1792 assert_se(chmod(masked, 0755) == 0);
1793 assert_se(touch(name3) >= 0);
1795 execute_directories(dirs, DEFAULT_TIMEOUT_USEC, NULL);
1797 assert_se(chdir(template_lo) == 0);
1798 assert_se(access("it_works", F_OK) >= 0);
1799 assert_se(access("failed", F_OK) < 0);
1801 assert_se(chdir(template_hi) == 0);
1802 assert_se(access("it_works2", F_OK) >= 0);
1803 assert_se(access("failed", F_OK) < 0);
1805 (void) rm_rf(template_lo, REMOVE_ROOT|REMOVE_PHYSICAL);
1806 (void) rm_rf(template_hi, REMOVE_ROOT|REMOVE_PHYSICAL);
1809 static void test_extract_first_word(void) {
1810 const char *p, *original;
1813 p = original = "foobar waldo";
1814 assert_se(extract_first_word(&p, &t, NULL, 0) > 0);
1815 assert_se(streq(t, "foobar"));
1817 assert_se(p == original + 7);
1819 assert_se(extract_first_word(&p, &t, NULL, 0) > 0);
1820 assert_se(streq(t, "waldo"));
1822 assert_se(isempty(p));
1824 assert_se(extract_first_word(&p, &t, NULL, 0) == 0);
1826 assert_se(isempty(p));
1828 p = original = "\"foobar\" \'waldo\'";
1829 assert_se(extract_first_word(&p, &t, NULL, 0) > 0);
1830 assert_se(streq(t, "\"foobar\""));
1832 assert_se(p == original + 9);
1834 assert_se(extract_first_word(&p, &t, NULL, 0) > 0);
1835 assert_se(streq(t, "\'waldo\'"));
1837 assert_se(isempty(p));
1839 assert_se(extract_first_word(&p, &t, NULL, 0) == 0);
1841 assert_se(isempty(p));
1843 p = original = "\"foobar\" \'waldo\'";
1844 assert_se(extract_first_word(&p, &t, NULL, EXTRACT_QUOTES) > 0);
1845 assert_se(streq(t, "foobar"));
1847 assert_se(p == original + 9);
1849 assert_se(extract_first_word(&p, &t, NULL, EXTRACT_QUOTES) > 0);
1850 assert_se(streq(t, "waldo"));
1852 assert_se(isempty(p));
1854 assert_se(extract_first_word(&p, &t, NULL, 0) == 0);
1856 assert_se(isempty(p));
1858 p = original = "\"";
1859 assert_se(extract_first_word(&p, &t, NULL, 0) == 1);
1860 assert_se(streq(t, "\""));
1862 assert_se(isempty(p));
1864 p = original = "\"";
1865 assert_se(extract_first_word(&p, &t, NULL, EXTRACT_QUOTES) == -EINVAL);
1866 assert_se(p == original + 1);
1868 p = original = "\'";
1869 assert_se(extract_first_word(&p, &t, NULL, 0) == 1);
1870 assert_se(streq(t, "\'"));
1872 assert_se(isempty(p));
1874 p = original = "\'";
1875 assert_se(extract_first_word(&p, &t, NULL, EXTRACT_QUOTES) == -EINVAL);
1876 assert_se(p == original + 1);
1878 p = original = "\'fooo";
1879 assert_se(extract_first_word(&p, &t, NULL, 0) == 1);
1880 assert_se(streq(t, "\'fooo"));
1882 assert_se(isempty(p));
1884 p = original = "\'fooo";
1885 assert_se(extract_first_word(&p, &t, NULL, EXTRACT_QUOTES) == -EINVAL);
1886 assert_se(p == original + 5);
1888 p = original = "\'fooo";
1889 assert_se(extract_first_word(&p, &t, NULL, EXTRACT_QUOTES|EXTRACT_RELAX) > 0);
1890 assert_se(streq(t, "fooo"));
1892 assert_se(isempty(p));
1894 p = original = "\"fooo";
1895 assert_se(extract_first_word(&p, &t, NULL, EXTRACT_QUOTES|EXTRACT_RELAX) > 0);
1896 assert_se(streq(t, "fooo"));
1898 assert_se(isempty(p));
1900 p = original = "yay\'foo\'bar";
1901 assert_se(extract_first_word(&p, &t, NULL, 0) > 0);
1902 assert_se(streq(t, "yay\'foo\'bar"));
1904 assert_se(isempty(p));
1906 p = original = "yay\'foo\'bar";
1907 assert_se(extract_first_word(&p, &t, NULL, EXTRACT_QUOTES) > 0);
1908 assert_se(streq(t, "yayfoobar"));
1910 assert_se(isempty(p));
1912 p = original = " foobar ";
1913 assert_se(extract_first_word(&p, &t, NULL, 0) > 0);
1914 assert_se(streq(t, "foobar"));
1916 assert_se(isempty(p));
1918 p = original = " foo\\ba\\x6ar ";
1919 assert_se(extract_first_word(&p, &t, NULL, EXTRACT_CUNESCAPE) > 0);
1920 assert_se(streq(t, "foo\ba\x6ar"));
1922 assert_se(isempty(p));
1924 p = original = " foo\\ba\\x6ar ";
1925 assert_se(extract_first_word(&p, &t, NULL, 0) > 0);
1926 assert_se(streq(t, "foobax6ar"));
1928 assert_se(isempty(p));
1930 p = original = " f\\u00f6o \"pi\\U0001F4A9le\" ";
1931 assert_se(extract_first_word(&p, &t, NULL, EXTRACT_CUNESCAPE) > 0);
1932 assert_se(streq(t, "föo"));
1934 assert_se(p == original + 13);
1936 assert_se(extract_first_word(&p, &t, NULL, EXTRACT_QUOTES|EXTRACT_CUNESCAPE) > 0);
1937 assert_se(streq(t, "pi\360\237\222\251le"));
1939 assert_se(isempty(p));
1941 p = original = "fooo\\";
1942 assert_se(extract_first_word(&p, &t, NULL, EXTRACT_RELAX) > 0);
1943 assert_se(streq(t, "fooo"));
1945 assert_se(isempty(p));
1947 p = original = "fooo\\";
1948 assert_se(extract_first_word(&p, &t, NULL, EXTRACT_CUNESCAPE_RELAX) > 0);
1949 assert_se(streq(t, "fooo\\"));
1951 assert_se(isempty(p));
1953 p = original = "fooo\\";
1954 assert_se(extract_first_word(&p, &t, NULL, EXTRACT_CUNESCAPE_RELAX|EXTRACT_RELAX) > 0);
1955 assert_se(streq(t, "fooo\\"));
1957 assert_se(isempty(p));
1959 p = original = "fooo\\";
1960 assert_se(extract_first_word(&p, &t, NULL, EXTRACT_CUNESCAPE|EXTRACT_CUNESCAPE_RELAX) > 0);
1961 assert_se(streq(t, "fooo\\"));
1963 assert_se(isempty(p));
1965 p = original = "\"foo\\";
1966 assert_se(extract_first_word(&p, &t, NULL, 0) == -EINVAL);
1967 assert_se(p == original + 5);
1969 p = original = "\"foo\\";
1970 assert_se(extract_first_word(&p, &t, NULL, EXTRACT_QUOTES|EXTRACT_RELAX) > 0);
1971 assert_se(streq(t, "foo"));
1973 assert_se(isempty(p));
1975 p = original = "foo::bar";
1976 assert_se(extract_first_word(&p, &t, ":", 0) == 1);
1977 assert_se(streq(t, "foo"));
1979 assert_se(p == original + 5);
1981 assert_se(extract_first_word(&p, &t, ":", 0) == 1);
1982 assert_se(streq(t, "bar"));
1984 assert_se(isempty(p));
1986 assert_se(extract_first_word(&p, &t, ":", 0) == 0);
1988 assert_se(isempty(p));
1990 p = original = "foo\\:bar::waldo";
1991 assert_se(extract_first_word(&p, &t, ":", 0) == 1);
1992 assert_se(streq(t, "foo:bar"));
1994 assert_se(p == original + 10);
1996 assert_se(extract_first_word(&p, &t, ":", 0) == 1);
1997 assert_se(streq(t, "waldo"));
1999 assert_se(isempty(p));
2001 assert_se(extract_first_word(&p, &t, ":", 0) == 0);
2003 assert_se(isempty(p));
2005 p = original = "\"foo\\";
2006 assert_se(extract_first_word(&p, &t, NULL, EXTRACT_QUOTES|EXTRACT_CUNESCAPE_RELAX) == -EINVAL);
2007 assert_se(p == original + 5);
2009 p = original = "\"foo\\";
2010 assert_se(extract_first_word(&p, &t, NULL, EXTRACT_QUOTES|EXTRACT_CUNESCAPE_RELAX|EXTRACT_RELAX) > 0);
2011 assert_se(streq(t, "foo\\"));
2013 assert_se(isempty(p));
2015 p = original = "\"foo\\";
2016 assert_se(extract_first_word(&p, &t, NULL, EXTRACT_QUOTES|EXTRACT_CUNESCAPE|EXTRACT_CUNESCAPE_RELAX|EXTRACT_RELAX) > 0);
2017 assert_se(streq(t, "foo\\"));
2019 assert_se(isempty(p));
2021 p = original = "fooo\\ bar quux";
2022 assert_se(extract_first_word(&p, &t, NULL, EXTRACT_RELAX) > 0);
2023 assert_se(streq(t, "fooo bar"));
2025 assert_se(p == original + 10);
2027 p = original = "fooo\\ bar quux";
2028 assert_se(extract_first_word(&p, &t, NULL, EXTRACT_CUNESCAPE_RELAX) > 0);
2029 assert_se(streq(t, "fooo bar"));
2031 assert_se(p == original + 10);
2033 p = original = "fooo\\ bar quux";
2034 assert_se(extract_first_word(&p, &t, NULL, EXTRACT_CUNESCAPE_RELAX|EXTRACT_RELAX) > 0);
2035 assert_se(streq(t, "fooo bar"));
2037 assert_se(p == original + 10);
2039 p = original = "fooo\\ bar quux";
2040 assert_se(extract_first_word(&p, &t, NULL, EXTRACT_CUNESCAPE) == -EINVAL);
2041 assert_se(p == original + 5);
2043 p = original = "fooo\\ bar quux";
2044 assert_se(extract_first_word(&p, &t, NULL, EXTRACT_CUNESCAPE|EXTRACT_CUNESCAPE_RELAX) > 0);
2045 assert_se(streq(t, "fooo\\ bar"));
2047 assert_se(p == original + 10);
2049 p = original = "\\w+@\\K[\\d.]+";
2050 assert_se(extract_first_word(&p, &t, NULL, EXTRACT_CUNESCAPE) == -EINVAL);
2051 assert_se(p == original + 1);
2053 p = original = "\\w+@\\K[\\d.]+";
2054 assert_se(extract_first_word(&p, &t, NULL, EXTRACT_CUNESCAPE|EXTRACT_CUNESCAPE_RELAX) > 0);
2055 assert_se(streq(t, "\\w+@\\K[\\d.]+"));
2057 assert_se(isempty(p));
2059 p = original = "\\w+\\b";
2060 assert_se(extract_first_word(&p, &t, NULL, EXTRACT_CUNESCAPE|EXTRACT_CUNESCAPE_RELAX) > 0);
2061 assert_se(streq(t, "\\w+\b"));
2063 assert_se(isempty(p));
2065 p = original = "-N ''";
2066 assert_se(extract_first_word(&p, &t, NULL, EXTRACT_QUOTES) > 0);
2067 assert_se(streq(t, "-N"));
2069 assert_se(p == original + 3);
2071 assert_se(extract_first_word(&p, &t, NULL, EXTRACT_QUOTES) > 0);
2072 assert_se(streq(t, ""));
2074 assert_se(isempty(p));
2076 p = original = ":foo\\:bar::waldo:";
2077 assert_se(extract_first_word(&p, &t, ":", EXTRACT_DONT_COALESCE_SEPARATORS) == 1);
2079 assert_se(streq(t, ""));
2081 assert_se(p == original + 1);
2083 assert_se(extract_first_word(&p, &t, ":", EXTRACT_DONT_COALESCE_SEPARATORS) == 1);
2084 assert_se(streq(t, "foo:bar"));
2086 assert_se(p == original + 10);
2088 assert_se(extract_first_word(&p, &t, ":", EXTRACT_DONT_COALESCE_SEPARATORS) == 1);
2090 assert_se(streq(t, ""));
2092 assert_se(p == original + 11);
2094 assert_se(extract_first_word(&p, &t, ":", EXTRACT_DONT_COALESCE_SEPARATORS) == 1);
2095 assert_se(streq(t, "waldo"));
2097 assert_se(p == original + 17);
2099 assert_se(extract_first_word(&p, &t, ":", EXTRACT_DONT_COALESCE_SEPARATORS) == 1);
2100 assert_se(streq(t, ""));
2102 assert_se(p == NULL);
2104 assert_se(extract_first_word(&p, &t, ":", EXTRACT_DONT_COALESCE_SEPARATORS) == 0);
2109 static void test_extract_first_word_and_warn(void) {
2110 const char *p, *original;
2113 p = original = "foobar waldo";
2114 assert_se(extract_first_word_and_warn(&p, &t, NULL, 0, NULL, "fake", 1, original) > 0);
2115 assert_se(streq(t, "foobar"));
2117 assert_se(p == original + 7);
2119 assert_se(extract_first_word_and_warn(&p, &t, NULL, 0, NULL, "fake", 1, original) > 0);
2120 assert_se(streq(t, "waldo"));
2122 assert_se(isempty(p));
2124 assert_se(extract_first_word_and_warn(&p, &t, NULL, 0, NULL, "fake", 1, original) == 0);
2126 assert_se(isempty(p));
2128 p = original = "\"foobar\" \'waldo\'";
2129 assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_QUOTES, NULL, "fake", 1, original) > 0);
2130 assert_se(streq(t, "foobar"));
2132 assert_se(p == original + 9);
2134 assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_QUOTES, NULL, "fake", 1, original) > 0);
2135 assert_se(streq(t, "waldo"));
2137 assert_se(isempty(p));
2139 assert_se(extract_first_word_and_warn(&p, &t, NULL, 0, NULL, "fake", 1, original) == 0);
2141 assert_se(isempty(p));
2143 p = original = "\"";
2144 assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_QUOTES, NULL, "fake", 1, original) == -EINVAL);
2145 assert_se(p == original + 1);
2147 p = original = "\'";
2148 assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_QUOTES, NULL, "fake", 1, original) == -EINVAL);
2149 assert_se(p == original + 1);
2151 p = original = "\'fooo";
2152 assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_QUOTES, NULL, "fake", 1, original) == -EINVAL);
2153 assert_se(p == original + 5);
2155 p = original = "\'fooo";
2156 assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_QUOTES|EXTRACT_RELAX, NULL, "fake", 1, original) > 0);
2157 assert_se(streq(t, "fooo"));
2159 assert_se(isempty(p));
2161 p = original = " foo\\ba\\x6ar ";
2162 assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_CUNESCAPE, NULL, "fake", 1, original) > 0);
2163 assert_se(streq(t, "foo\ba\x6ar"));
2165 assert_se(isempty(p));
2167 p = original = " foo\\ba\\x6ar ";
2168 assert_se(extract_first_word_and_warn(&p, &t, NULL, 0, NULL, "fake", 1, original) > 0);
2169 assert_se(streq(t, "foobax6ar"));
2171 assert_se(isempty(p));
2173 p = original = " f\\u00f6o \"pi\\U0001F4A9le\" ";
2174 assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_CUNESCAPE, NULL, "fake", 1, original) > 0);
2175 assert_se(streq(t, "föo"));
2177 assert_se(p == original + 13);
2179 assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_QUOTES|EXTRACT_CUNESCAPE, NULL, "fake", 1, original) > 0);
2180 assert_se(streq(t, "pi\360\237\222\251le"));
2182 assert_se(isempty(p));
2184 p = original = "fooo\\";
2185 assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_RELAX, NULL, "fake", 1, original) > 0);
2186 assert_se(streq(t, "fooo"));
2188 assert_se(isempty(p));
2190 p = original = "fooo\\";
2191 assert_se(extract_first_word_and_warn(&p, &t, NULL, 0, NULL, "fake", 1, original) > 0);
2192 assert_se(streq(t, "fooo\\"));
2194 assert_se(isempty(p));
2196 p = original = "fooo\\";
2197 assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_CUNESCAPE, NULL, "fake", 1, original) > 0);
2198 assert_se(streq(t, "fooo\\"));
2200 assert_se(isempty(p));
2202 p = original = "\"foo\\";
2203 assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_QUOTES, NULL, "fake", 1, original) == -EINVAL);
2204 assert_se(p == original + 5);
2206 p = original = "\"foo\\";
2207 assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_QUOTES|EXTRACT_RELAX, NULL, "fake", 1, original) > 0);
2208 assert_se(streq(t, "foo"));
2210 assert_se(isempty(p));
2212 p = original = "\"foo\\";
2213 assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_QUOTES|EXTRACT_CUNESCAPE, NULL, "fake", 1, original) == -EINVAL);
2214 assert_se(p == original + 5);
2216 p = original = "\"foo\\";
2217 assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_QUOTES|EXTRACT_CUNESCAPE|EXTRACT_RELAX, NULL, "fake", 1, original) > 0);
2218 assert_se(streq(t, "foo"));
2220 assert_se(isempty(p));
2222 p = original = "fooo\\ bar quux";
2223 assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_RELAX, NULL, "fake", 1, original) > 0);
2224 assert_se(streq(t, "fooo bar"));
2226 assert_se(p == original + 10);
2228 p = original = "fooo\\ bar quux";
2229 assert_se(extract_first_word_and_warn(&p, &t, NULL, 0, NULL, "fake", 1, original) > 0);
2230 assert_se(streq(t, "fooo bar"));
2232 assert_se(p == original + 10);
2234 p = original = "fooo\\ bar quux";
2235 assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_CUNESCAPE, NULL, "fake", 1, original) > 0);
2236 assert_se(streq(t, "fooo\\ bar"));
2238 assert_se(p == original + 10);
2240 p = original = "\\w+@\\K[\\d.]+";
2241 assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_CUNESCAPE, NULL, "fake", 1, original) > 0);
2242 assert_se(streq(t, "\\w+@\\K[\\d.]+"));
2244 assert_se(isempty(p));
2246 p = original = "\\w+\\b";
2247 assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_CUNESCAPE, NULL, "fake", 1, original) > 0);
2248 assert_se(streq(t, "\\w+\b"));
2250 assert_se(isempty(p));
2253 static void test_extract_many_words(void) {
2254 const char *p, *original;
2257 p = original = "foobar waldi piep";
2258 assert_se(extract_many_words(&p, NULL, 0, &a, &b, &c, NULL) == 3);
2259 assert_se(isempty(p));
2260 assert_se(streq_ptr(a, "foobar"));
2261 assert_se(streq_ptr(b, "waldi"));
2262 assert_se(streq_ptr(c, "piep"));
2267 p = original = "'foobar' wa\"ld\"i ";
2268 assert_se(extract_many_words(&p, NULL, 0, &a, &b, &c, NULL) == 2);
2269 assert_se(isempty(p));
2270 assert_se(streq_ptr(a, "'foobar'"));
2271 assert_se(streq_ptr(b, "wa\"ld\"i"));
2272 assert_se(streq_ptr(c, NULL));
2276 p = original = "'foobar' wa\"ld\"i ";
2277 assert_se(extract_many_words(&p, NULL, EXTRACT_QUOTES, &a, &b, &c, NULL) == 2);
2278 assert_se(isempty(p));
2279 assert_se(streq_ptr(a, "foobar"));
2280 assert_se(streq_ptr(b, "waldi"));
2281 assert_se(streq_ptr(c, NULL));
2286 assert_se(extract_many_words(&p, NULL, 0, &a, &b, &c, NULL) == 0);
2287 assert_se(isempty(p));
2288 assert_se(streq_ptr(a, NULL));
2289 assert_se(streq_ptr(b, NULL));
2290 assert_se(streq_ptr(c, NULL));
2293 assert_se(extract_many_words(&p, NULL, 0, &a, &b, &c, NULL) == 0);
2294 assert_se(isempty(p));
2295 assert_se(streq_ptr(a, NULL));
2296 assert_se(streq_ptr(b, NULL));
2297 assert_se(streq_ptr(c, NULL));
2299 p = original = "foobar";
2300 assert_se(extract_many_words(&p, NULL, 0, NULL) == 0);
2301 assert_se(p == original);
2303 p = original = "foobar waldi";
2304 assert_se(extract_many_words(&p, NULL, 0, &a, NULL) == 1);
2305 assert_se(p == original+7);
2306 assert_se(streq_ptr(a, "foobar"));
2309 p = original = " foobar ";
2310 assert_se(extract_many_words(&p, NULL, 0, &a, NULL) == 1);
2311 assert_se(isempty(p));
2312 assert_se(streq_ptr(a, "foobar"));
2316 static int parse_item(const char *key, const char *value) {
2319 log_info("kernel cmdline option <%s> = <%s>", key, strna(value));
2323 static void test_parse_proc_cmdline(void) {
2324 assert_se(parse_proc_cmdline(parse_item) >= 0);
2327 static void test_raw_clone(void) {
2328 pid_t parent, pid, pid2;
2331 log_info("before clone: getpid()→"PID_FMT, parent);
2332 assert_se(raw_getpid() == parent);
2334 pid = raw_clone(0, NULL);
2335 assert_se(pid >= 0);
2337 pid2 = raw_getpid();
2338 log_info("raw_clone: "PID_FMT" getpid()→"PID_FMT" raw_getpid()→"PID_FMT,
2339 pid, getpid(), pid2);
2341 assert_se(pid2 != parent);
2342 _exit(EXIT_SUCCESS);
2346 assert_se(pid2 == parent);
2347 waitpid(pid, &status, __WCLONE);
2348 assert_se(WIFEXITED(status) && WEXITSTATUS(status) == EXIT_SUCCESS);
2352 static void test_same_fd(void) {
2353 _cleanup_close_pair_ int p[2] = { -1, -1 };
2354 _cleanup_close_ int a = -1, b = -1, c = -1;
2356 assert_se(pipe2(p, O_CLOEXEC) >= 0);
2357 assert_se((a = dup(p[0])) >= 0);
2358 assert_se((b = open("/dev/null", O_RDONLY|O_CLOEXEC)) >= 0);
2359 assert_se((c = dup(a)) >= 0);
2361 assert_se(same_fd(p[0], p[0]) > 0);
2362 assert_se(same_fd(p[1], p[1]) > 0);
2363 assert_se(same_fd(a, a) > 0);
2364 assert_se(same_fd(b, b) > 0);
2366 assert_se(same_fd(a, p[0]) > 0);
2367 assert_se(same_fd(p[0], a) > 0);
2368 assert_se(same_fd(c, p[0]) > 0);
2369 assert_se(same_fd(p[0], c) > 0);
2370 assert_se(same_fd(a, c) > 0);
2371 assert_se(same_fd(c, a) > 0);
2373 assert_se(same_fd(p[0], p[1]) == 0);
2374 assert_se(same_fd(p[1], p[0]) == 0);
2375 assert_se(same_fd(p[0], b) == 0);
2376 assert_se(same_fd(b, p[0]) == 0);
2377 assert_se(same_fd(p[1], a) == 0);
2378 assert_se(same_fd(a, p[1]) == 0);
2379 assert_se(same_fd(p[1], b) == 0);
2380 assert_se(same_fd(b, p[1]) == 0);
2382 assert_se(same_fd(a, b) == 0);
2383 assert_se(same_fd(b, a) == 0);
2386 static void test_uid_ptr(void) {
2388 assert_se(UID_TO_PTR(0) != NULL);
2389 assert_se(UID_TO_PTR(1000) != NULL);
2391 assert_se(PTR_TO_UID(UID_TO_PTR(0)) == 0);
2392 assert_se(PTR_TO_UID(UID_TO_PTR(1000)) == 1000);
2395 static void test_sparse_write_one(int fd, const char *buffer, size_t n) {
2398 assert_se(lseek(fd, 0, SEEK_SET) == 0);
2399 assert_se(ftruncate(fd, 0) >= 0);
2400 assert_se(sparse_write(fd, buffer, n, 4) == (ssize_t) n);
2402 assert_se(lseek(fd, 0, SEEK_CUR) == (off_t) n);
2403 assert_se(ftruncate(fd, n) >= 0);
2405 assert_se(lseek(fd, 0, SEEK_SET) == 0);
2406 assert_se(read(fd, check, n) == (ssize_t) n);
2408 assert_se(memcmp(buffer, check, n) == 0);
2411 static void test_sparse_write(void) {
2412 const char test_a[] = "test";
2413 const char test_b[] = "\0\0\0\0test\0\0\0\0";
2414 const char test_c[] = "\0\0test\0\0\0\0";
2415 const char test_d[] = "\0\0test\0\0\0test\0\0\0\0test\0\0\0\0\0test\0\0\0test\0\0\0\0test\0\0\0\0\0\0\0\0";
2416 const char test_e[] = "test\0\0\0\0test";
2417 _cleanup_close_ int fd = -1;
2418 char fn[] = "/tmp/sparseXXXXXX";
2420 fd = mkostemp(fn, O_CLOEXEC);
2424 test_sparse_write_one(fd, test_a, sizeof(test_a));
2425 test_sparse_write_one(fd, test_b, sizeof(test_b));
2426 test_sparse_write_one(fd, test_c, sizeof(test_c));
2427 test_sparse_write_one(fd, test_d, sizeof(test_d));
2428 test_sparse_write_one(fd, test_e, sizeof(test_e));
2431 static void test_shell_escape_one(const char *s, const char *bad, const char *expected) {
2432 _cleanup_free_ char *r;
2434 assert_se(r = shell_escape(s, bad));
2435 assert_se(streq_ptr(r, expected));
2438 static void test_shell_escape(void) {
2439 test_shell_escape_one("", "", "");
2440 test_shell_escape_one("\\", "", "\\\\");
2441 test_shell_escape_one("foobar", "", "foobar");
2442 test_shell_escape_one("foobar", "o", "f\\o\\obar");
2443 test_shell_escape_one("foo:bar,baz", ",:", "foo\\:bar\\,baz");
2446 static void test_shell_maybe_quote_one(const char *s, const char *expected) {
2447 _cleanup_free_ char *r;
2449 assert_se(r = shell_maybe_quote(s));
2450 assert_se(streq(r, expected));
2453 static void test_shell_maybe_quote(void) {
2455 test_shell_maybe_quote_one("", "");
2456 test_shell_maybe_quote_one("\\", "\"\\\\\"");
2457 test_shell_maybe_quote_one("\"", "\"\\\"\"");
2458 test_shell_maybe_quote_one("foobar", "foobar");
2459 test_shell_maybe_quote_one("foo bar", "\"foo bar\"");
2460 test_shell_maybe_quote_one("foo \"bar\" waldo", "\"foo \\\"bar\\\" waldo\"");
2461 test_shell_maybe_quote_one("foo$bar", "\"foo\\$bar\"");
2464 static void test_parse_mode(void) {
2467 assert_se(parse_mode("-1", &m) < 0);
2468 assert_se(parse_mode("", &m) < 0);
2469 assert_se(parse_mode("888", &m) < 0);
2470 assert_se(parse_mode("77777", &m) < 0);
2472 assert_se(parse_mode("544", &m) >= 0 && m == 0544);
2473 assert_se(parse_mode("777", &m) >= 0 && m == 0777);
2474 assert_se(parse_mode("7777", &m) >= 0 && m == 07777);
2475 assert_se(parse_mode("0", &m) >= 0 && m == 0);
2478 static void test_tempfn(void) {
2479 char *ret = NULL, *p;
2481 assert_se(tempfn_xxxxxx("/foo/bar/waldo", NULL, &ret) >= 0);
2482 assert_se(streq_ptr(ret, "/foo/bar/.#waldoXXXXXX"));
2485 assert_se(tempfn_xxxxxx("/foo/bar/waldo", "[miau]", &ret) >= 0);
2486 assert_se(streq_ptr(ret, "/foo/bar/.#[miau]waldoXXXXXX"));
2489 assert_se(tempfn_random("/foo/bar/waldo", NULL, &ret) >= 0);
2490 assert_se(p = startswith(ret, "/foo/bar/.#waldo"));
2491 assert_se(strlen(p) == 16);
2492 assert_se(in_charset(p, "0123456789abcdef"));
2495 assert_se(tempfn_random("/foo/bar/waldo", "[wuff]", &ret) >= 0);
2496 assert_se(p = startswith(ret, "/foo/bar/.#[wuff]waldo"));
2497 assert_se(strlen(p) == 16);
2498 assert_se(in_charset(p, "0123456789abcdef"));
2501 assert_se(tempfn_random_child("/foo/bar/waldo", NULL, &ret) >= 0);
2502 assert_se(p = startswith(ret, "/foo/bar/waldo/.#"));
2503 assert_se(strlen(p) == 16);
2504 assert_se(in_charset(p, "0123456789abcdef"));
2507 assert_se(tempfn_random_child("/foo/bar/waldo", "[kikiriki]", &ret) >= 0);
2508 assert_se(p = startswith(ret, "/foo/bar/waldo/.#[kikiriki]"));
2509 assert_se(strlen(p) == 16);
2510 assert_se(in_charset(p, "0123456789abcdef"));
2514 static void test_strcmp_ptr(void) {
2515 assert_se(strcmp_ptr(NULL, NULL) == 0);
2516 assert_se(strcmp_ptr("", NULL) > 0);
2517 assert_se(strcmp_ptr("foo", NULL) > 0);
2518 assert_se(strcmp_ptr(NULL, "") < 0);
2519 assert_se(strcmp_ptr(NULL, "bar") < 0);
2520 assert_se(strcmp_ptr("foo", "bar") > 0);
2521 assert_se(strcmp_ptr("bar", "baz") < 0);
2522 assert_se(strcmp_ptr("foo", "foo") == 0);
2523 assert_se(strcmp_ptr("", "") == 0);
2526 static void test_fgetxattrat_fake(void) {
2527 char t[] = "/var/tmp/xattrtestXXXXXX";
2528 _cleanup_close_ int fd = -1;
2533 assert_se(mkdtemp(t));
2534 x = strjoina(t, "/test");
2535 assert_se(touch(x) >= 0);
2537 r = setxattr(x, "user.foo", "bar", 3, 0);
2538 if (r < 0 && errno == EOPNOTSUPP) /* no xattrs supported on /var/tmp... */
2542 fd = open(t, O_RDONLY|O_DIRECTORY|O_CLOEXEC|O_NOCTTY);
2545 assert_se(fgetxattrat_fake(fd, "test", "user.foo", v, 3, 0) >= 0);
2546 assert_se(memcmp(v, "bar", 3) == 0);
2549 fd = open("/", O_RDONLY|O_DIRECTORY|O_CLOEXEC|O_NOCTTY);
2551 assert_se(fgetxattrat_fake(fd, "usr", "user.idontexist", v, 3, 0) == -ENODATA);
2554 assert_se(unlink(x) >= 0);
2555 assert_se(rmdir(t) >= 0);
2558 int main(int argc, char *argv[]) {
2559 log_parse_environment();
2563 test_align_power2();
2565 test_container_of();
2567 test_div_round_up();
2570 test_parse_boolean();
2579 test_delete_chars();
2583 test_base32hexchar();
2584 test_unbase32hexchar();
2586 test_unbase64char();
2592 test_base32hexmem();
2593 test_unbase32hexmem();
2598 test_foreach_word();
2599 test_foreach_word_quoted();
2600 test_memdup_multiply();
2602 test_protect_errno();
2605 test_parse_cpu_set();
2606 test_config_parse_iec_uint64();
2610 test_fstab_node_to_udev_node();
2611 test_get_files_in_directory();
2613 test_writing_tmpfile();
2616 test_foreach_string();
2617 test_filename_is_valid();
2618 test_string_has_cc();
2619 test_ascii_strlower();
2621 test_is_valid_documentation_url();
2622 test_file_in_same_dir();
2624 test_endswith_no_case();
2625 test_close_nointr();
2626 test_unlink_noerrno();
2627 test_readlink_and_make_absolute();
2628 test_ignore_signals();
2632 test_search_and_fopen();
2633 test_search_and_fopen_nulstr();
2635 test_execute_directory();
2636 test_extract_first_word();
2637 test_extract_first_word_and_warn();
2638 test_extract_many_words();
2639 test_parse_proc_cmdline();
2643 test_sparse_write();
2644 test_shell_escape();
2645 test_shell_maybe_quote();
2649 test_fgetxattrat_fake();