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/>.
32 #include "conf-parser.h"
33 #include "cpu-set-util.h"
37 #include "process-util.h"
39 #include "signal-util.h"
44 static void test_streq_ptr(void) {
45 assert_se(streq_ptr(NULL, NULL));
46 assert_se(!streq_ptr("abc", "cdef"));
49 static void test_align_power2(void) {
52 assert_se(ALIGN_POWER2(0) == 0);
53 assert_se(ALIGN_POWER2(1) == 1);
54 assert_se(ALIGN_POWER2(2) == 2);
55 assert_se(ALIGN_POWER2(3) == 4);
56 assert_se(ALIGN_POWER2(12) == 16);
58 assert_se(ALIGN_POWER2(ULONG_MAX) == 0);
59 assert_se(ALIGN_POWER2(ULONG_MAX - 1) == 0);
60 assert_se(ALIGN_POWER2(ULONG_MAX - 1024) == 0);
61 assert_se(ALIGN_POWER2(ULONG_MAX / 2) == ULONG_MAX / 2 + 1);
62 assert_se(ALIGN_POWER2(ULONG_MAX + 1) == 0);
64 for (i = 1; i < 131071; ++i) {
65 for (p2 = 1; p2 < i; p2 <<= 1)
68 assert_se(ALIGN_POWER2(i) == p2);
71 for (i = ULONG_MAX - 1024; i < ULONG_MAX; ++i) {
72 for (p2 = 1; p2 && p2 < i; p2 <<= 1)
75 assert_se(ALIGN_POWER2(i) == p2);
79 static void test_max(void) {
82 int b[CONST_MAX(10, 100)];
84 .a = CONST_MAX(10, 100),
88 assert_cc(sizeof(val1.b) == sizeof(int) * 100);
90 /* CONST_MAX returns (void) instead of a value if the passed arguments
91 * are not of the same type or not constant expressions. */
92 assert_cc(__builtin_types_compatible_p(typeof(CONST_MAX(1, 10)), int));
93 assert_cc(__builtin_types_compatible_p(typeof(CONST_MAX(1, 1U)), void));
95 assert_se(val1.a == 100);
96 assert_se(MAX(++d, 0) == 1);
99 assert_cc(MAXSIZE(char[3], uint16_t) == 3);
100 assert_cc(MAXSIZE(char[3], uint32_t) == 4);
101 assert_cc(MAXSIZE(char, long) == sizeof(long));
103 assert_se(MAX(-5, 5) == 5);
104 assert_se(MAX(5, 5) == 5);
105 assert_se(MAX(MAX(1, MAX(2, MAX(3, 4))), 5) == 5);
106 assert_se(MAX(MAX(1, MAX(2, MAX(3, 2))), 1) == 3);
107 assert_se(MAX(MIN(1, MIN(2, MIN(3, 4))), 5) == 5);
108 assert_se(MAX(MAX(1, MIN(2, MIN(3, 2))), 1) == 2);
109 assert_se(LESS_BY(8, 4) == 4);
110 assert_se(LESS_BY(8, 8) == 0);
111 assert_se(LESS_BY(4, 8) == 0);
112 assert_se(LESS_BY(16, LESS_BY(8, 4)) == 12);
113 assert_se(LESS_BY(4, LESS_BY(8, 4)) == 0);
114 assert_se(CLAMP(-5, 0, 1) == 0);
115 assert_se(CLAMP(5, 0, 1) == 1);
116 assert_se(CLAMP(5, -10, 1) == 1);
117 assert_se(CLAMP(5, -10, 10) == 5);
118 assert_se(CLAMP(CLAMP(0, -10, 10), CLAMP(-5, 10, 20), CLAMP(100, -5, 20)) == 10);
121 static void test_container_of(void) {
127 } _packed_ myval = { };
129 assert_cc(sizeof(myval) == 17);
130 assert_se(container_of(&myval.v1, struct mytype, v1) == &myval);
131 assert_se(container_of(&myval.v2, struct mytype, v2) == &myval);
132 assert_se(container_of(&container_of(&myval.v2,
139 static void test_alloca(void) {
140 static const uint8_t zero[997] = { };
143 t = alloca_align(17, 512);
144 assert_se(!((uintptr_t)t & 0xff));
147 t = alloca0_align(997, 1024);
148 assert_se(!((uintptr_t)t & 0x1ff));
149 assert_se(!memcmp(t, zero, 997));
152 static void test_div_round_up(void) {
156 assert_se(DIV_ROUND_UP(0, 8) == 0);
157 assert_se(DIV_ROUND_UP(1, 8) == 1);
158 assert_se(DIV_ROUND_UP(8, 8) == 1);
159 assert_se(DIV_ROUND_UP(12, 8) == 2);
160 assert_se(DIV_ROUND_UP(16, 8) == 2);
162 /* test multiple evaluation */
164 assert_se(DIV_ROUND_UP(div++, 8) == 0 && div == 1);
165 assert_se(DIV_ROUND_UP(++div, 8) == 1 && div == 2);
166 assert_se(DIV_ROUND_UP(8, div++) == 4 && div == 3);
167 assert_se(DIV_ROUND_UP(8, ++div) == 2 && div == 4);
169 /* overflow test with exact division */
170 assert_se(sizeof(0U) == 4);
171 assert_se(0xfffffffaU % 10U == 0U);
172 assert_se(0xfffffffaU / 10U == 429496729U);
173 assert_se(DIV_ROUND_UP(0xfffffffaU, 10U) == 429496729U);
174 assert_se((0xfffffffaU + 10U - 1U) / 10U == 0U);
175 assert_se(0xfffffffaU / 10U + !!(0xfffffffaU % 10U) == 429496729U);
177 /* overflow test with rounded division */
178 assert_se(0xfffffffdU % 10U == 3U);
179 assert_se(0xfffffffdU / 10U == 429496729U);
180 assert_se(DIV_ROUND_UP(0xfffffffdU, 10U) == 429496730U);
181 assert_se((0xfffffffdU + 10U - 1U) / 10U == 0U);
182 assert_se(0xfffffffdU / 10U + !!(0xfffffffdU % 10U) == 429496730U);
185 static void test_first_word(void) {
186 assert_se(first_word("Hello", ""));
187 assert_se(first_word("Hello", "Hello"));
188 assert_se(first_word("Hello world", "Hello"));
189 assert_se(first_word("Hello\tworld", "Hello"));
190 assert_se(first_word("Hello\nworld", "Hello"));
191 assert_se(first_word("Hello\rworld", "Hello"));
192 assert_se(first_word("Hello ", "Hello"));
194 assert_se(!first_word("Hello", "Hellooo"));
195 assert_se(!first_word("Hello", "xxxxx"));
196 assert_se(!first_word("Hellooo", "Hello"));
199 static void test_close_many(void) {
201 char name0[] = "/tmp/test-close-many.XXXXXX";
202 char name1[] = "/tmp/test-close-many.XXXXXX";
203 char name2[] = "/tmp/test-close-many.XXXXXX";
205 fds[0] = mkostemp_safe(name0, O_RDWR|O_CLOEXEC);
206 fds[1] = mkostemp_safe(name1, O_RDWR|O_CLOEXEC);
207 fds[2] = mkostemp_safe(name2, O_RDWR|O_CLOEXEC);
211 assert_se(fcntl(fds[0], F_GETFD) == -1);
212 assert_se(fcntl(fds[1], F_GETFD) == -1);
213 assert_se(fcntl(fds[2], F_GETFD) >= 0);
222 static void test_parse_boolean(void) {
223 assert_se(parse_boolean("1") == 1);
224 assert_se(parse_boolean("y") == 1);
225 assert_se(parse_boolean("Y") == 1);
226 assert_se(parse_boolean("yes") == 1);
227 assert_se(parse_boolean("YES") == 1);
228 assert_se(parse_boolean("true") == 1);
229 assert_se(parse_boolean("TRUE") == 1);
230 assert_se(parse_boolean("on") == 1);
231 assert_se(parse_boolean("ON") == 1);
233 assert_se(parse_boolean("0") == 0);
234 assert_se(parse_boolean("n") == 0);
235 assert_se(parse_boolean("N") == 0);
236 assert_se(parse_boolean("no") == 0);
237 assert_se(parse_boolean("NO") == 0);
238 assert_se(parse_boolean("false") == 0);
239 assert_se(parse_boolean("FALSE") == 0);
240 assert_se(parse_boolean("off") == 0);
241 assert_se(parse_boolean("OFF") == 0);
243 assert_se(parse_boolean("garbage") < 0);
244 assert_se(parse_boolean("") < 0);
245 assert_se(parse_boolean("full") < 0);
248 static void test_parse_pid(void) {
252 r = parse_pid("100", &pid);
254 assert_se(pid == 100);
256 r = parse_pid("0x7FFFFFFF", &pid);
258 assert_se(pid == 2147483647);
260 pid = 65; /* pid is left unchanged on ERANGE. Set to known arbitrary value. */
261 r = parse_pid("0", &pid);
262 assert_se(r == -ERANGE);
263 assert_se(pid == 65);
265 pid = 65; /* pid is left unchanged on ERANGE. Set to known arbitrary value. */
266 r = parse_pid("-100", &pid);
267 assert_se(r == -ERANGE);
268 assert_se(pid == 65);
270 pid = 65; /* pid is left unchanged on ERANGE. Set to known arbitrary value. */
271 r = parse_pid("0xFFFFFFFFFFFFFFFFF", &pid);
272 assert_se(r == -ERANGE);
273 assert_se(pid == 65);
275 r = parse_pid("junk", &pid);
276 assert_se(r == -EINVAL);
279 static void test_parse_uid(void) {
283 r = parse_uid("100", &uid);
285 assert_se(uid == 100);
287 r = parse_uid("65535", &uid);
288 assert_se(r == -ENXIO);
290 r = parse_uid("asdsdas", &uid);
291 assert_se(r == -EINVAL);
294 static void test_safe_atou16(void) {
298 r = safe_atou16("12345", &l);
300 assert_se(l == 12345);
302 r = safe_atou16("123456", &l);
303 assert_se(r == -ERANGE);
305 r = safe_atou16("junk", &l);
306 assert_se(r == -EINVAL);
309 static void test_safe_atoi16(void) {
313 r = safe_atoi16("-12345", &l);
315 assert_se(l == -12345);
317 r = safe_atoi16("36536", &l);
318 assert_se(r == -ERANGE);
320 r = safe_atoi16("junk", &l);
321 assert_se(r == -EINVAL);
324 static void test_safe_atolli(void) {
328 r = safe_atolli("12345", &l);
330 assert_se(l == 12345);
332 r = safe_atolli("junk", &l);
333 assert_se(r == -EINVAL);
336 static void test_safe_atod(void) {
341 r = safe_atod("junk", &d);
342 assert_se(r == -EINVAL);
344 r = safe_atod("0.2244", &d);
346 assert_se(fabs(d - 0.2244) < 0.000001);
348 r = safe_atod("0,5", &d);
349 assert_se(r == -EINVAL);
353 assert_se(*e == ',');
355 /* Check if this really is locale independent */
356 if (setlocale(LC_NUMERIC, "de_DE.utf8")) {
358 r = safe_atod("0.2244", &d);
360 assert_se(fabs(d - 0.2244) < 0.000001);
362 r = safe_atod("0,5", &d);
363 assert_se(r == -EINVAL);
366 assert_se(fabs(strtod("0,5", &e) - 0.5) < 0.00001);
369 /* And check again, reset */
370 assert_se(setlocale(LC_NUMERIC, "C"));
372 r = safe_atod("0.2244", &d);
374 assert_se(fabs(d - 0.2244) < 0.000001);
376 r = safe_atod("0,5", &d);
377 assert_se(r == -EINVAL);
381 assert_se(*e == ',');
384 static void test_strappend(void) {
385 _cleanup_free_ char *t1, *t2, *t3, *t4;
387 t1 = strappend(NULL, NULL);
388 assert_se(streq(t1, ""));
390 t2 = strappend(NULL, "suf");
391 assert_se(streq(t2, "suf"));
393 t3 = strappend("pre", NULL);
394 assert_se(streq(t3, "pre"));
396 t4 = strappend("pre", "suf");
397 assert_se(streq(t4, "presuf"));
400 static void test_strstrip(void) {
402 char input[] = " hello, waldo. ";
405 assert_se(streq(r, "hello, waldo."));
408 static void test_delete_chars(void) {
410 char input[] = " hello, waldo. abc";
412 r = delete_chars(input, WHITESPACE);
413 assert_se(streq(r, "hello,waldo.abc"));
416 static void test_in_charset(void) {
417 assert_se(in_charset("dddaaabbbcccc", "abcd"));
418 assert_se(!in_charset("dddaaabbbcccc", "abc f"));
421 static void test_hexchar(void) {
422 assert_se(hexchar(0xa) == 'a');
423 assert_se(hexchar(0x0) == '0');
426 static void test_unhexchar(void) {
427 assert_se(unhexchar('a') == 0xA);
428 assert_se(unhexchar('A') == 0xA);
429 assert_se(unhexchar('0') == 0x0);
432 static void test_base32hexchar(void) {
433 assert_se(base32hexchar(0) == '0');
434 assert_se(base32hexchar(9) == '9');
435 assert_se(base32hexchar(10) == 'A');
436 assert_se(base32hexchar(31) == 'V');
439 static void test_unbase32hexchar(void) {
440 assert_se(unbase32hexchar('0') == 0);
441 assert_se(unbase32hexchar('9') == 9);
442 assert_se(unbase32hexchar('A') == 10);
443 assert_se(unbase32hexchar('V') == 31);
444 assert_se(unbase32hexchar('=') == -EINVAL);
447 static void test_base64char(void) {
448 assert_se(base64char(0) == 'A');
449 assert_se(base64char(26) == 'a');
450 assert_se(base64char(63) == '/');
453 static void test_unbase64char(void) {
454 assert_se(unbase64char('A') == 0);
455 assert_se(unbase64char('Z') == 25);
456 assert_se(unbase64char('a') == 26);
457 assert_se(unbase64char('z') == 51);
458 assert_se(unbase64char('0') == 52);
459 assert_se(unbase64char('9') == 61);
460 assert_se(unbase64char('+') == 62);
461 assert_se(unbase64char('/') == 63);
462 assert_se(unbase64char('=') == -EINVAL);
465 static void test_octchar(void) {
466 assert_se(octchar(00) == '0');
467 assert_se(octchar(07) == '7');
470 static void test_unoctchar(void) {
471 assert_se(unoctchar('0') == 00);
472 assert_se(unoctchar('7') == 07);
475 static void test_decchar(void) {
476 assert_se(decchar(0) == '0');
477 assert_se(decchar(9) == '9');
480 static void test_undecchar(void) {
481 assert_se(undecchar('0') == 0);
482 assert_se(undecchar('9') == 9);
485 static void test_unhexmem(void) {
486 const char *hex = "efa214921";
487 const char *hex_invalid = "efa214921o";
488 _cleanup_free_ char *hex2 = NULL;
489 _cleanup_free_ void *mem = NULL;
492 assert_se(unhexmem(hex, strlen(hex), &mem, &len) == 0);
493 assert_se(unhexmem(hex, strlen(hex) + 1, &mem, &len) == -EINVAL);
494 assert_se(unhexmem(hex_invalid, strlen(hex_invalid), &mem, &len) == -EINVAL);
496 assert_se((hex2 = hexmem(mem, len)));
500 assert_se(memcmp(hex, hex2, strlen(hex)) == 0);
504 assert_se(unhexmem(hex, strlen(hex) - 1, &mem, &len) == 0);
505 assert_se((hex2 = hexmem(mem, len)));
506 assert_se(memcmp(hex, hex2, strlen(hex) - 1) == 0);
509 /* https://tools.ietf.org/html/rfc4648#section-10 */
510 static void test_base32hexmem(void) {
513 b32 = base32hexmem("", strlen(""), true);
515 assert_se(streq(b32, ""));
518 b32 = base32hexmem("f", strlen("f"), true);
520 assert_se(streq(b32, "CO======"));
523 b32 = base32hexmem("fo", strlen("fo"), true);
525 assert_se(streq(b32, "CPNG===="));
528 b32 = base32hexmem("foo", strlen("foo"), true);
530 assert_se(streq(b32, "CPNMU==="));
533 b32 = base32hexmem("foob", strlen("foob"), true);
535 assert_se(streq(b32, "CPNMUOG="));
538 b32 = base32hexmem("fooba", strlen("fooba"), true);
540 assert_se(streq(b32, "CPNMUOJ1"));
543 b32 = base32hexmem("foobar", strlen("foobar"), true);
545 assert_se(streq(b32, "CPNMUOJ1E8======"));
548 b32 = base32hexmem("", strlen(""), false);
550 assert_se(streq(b32, ""));
553 b32 = base32hexmem("f", strlen("f"), false);
555 assert_se(streq(b32, "CO"));
558 b32 = base32hexmem("fo", strlen("fo"), false);
560 assert_se(streq(b32, "CPNG"));
563 b32 = base32hexmem("foo", strlen("foo"), false);
565 assert_se(streq(b32, "CPNMU"));
568 b32 = base32hexmem("foob", strlen("foob"), false);
570 assert_se(streq(b32, "CPNMUOG"));
573 b32 = base32hexmem("fooba", strlen("fooba"), false);
575 assert_se(streq(b32, "CPNMUOJ1"));
578 b32 = base32hexmem("foobar", strlen("foobar"), false);
580 assert_se(streq(b32, "CPNMUOJ1E8"));
584 static void test_unbase32hexmem(void) {
588 assert_se(unbase32hexmem("", strlen(""), true, &mem, &len) == 0);
589 assert_se(streq(strndupa(mem, len), ""));
592 assert_se(unbase32hexmem("CO======", strlen("CO======"), true, &mem, &len) == 0);
593 assert_se(streq(strndupa(mem, len), "f"));
596 assert_se(unbase32hexmem("CPNG====", strlen("CPNG===="), true, &mem, &len) == 0);
597 assert_se(streq(strndupa(mem, len), "fo"));
600 assert_se(unbase32hexmem("CPNMU===", strlen("CPNMU==="), true, &mem, &len) == 0);
601 assert_se(streq(strndupa(mem, len), "foo"));
604 assert_se(unbase32hexmem("CPNMUOG=", strlen("CPNMUOG="), true, &mem, &len) == 0);
605 assert_se(streq(strndupa(mem, len), "foob"));
608 assert_se(unbase32hexmem("CPNMUOJ1", strlen("CPNMUOJ1"), true, &mem, &len) == 0);
609 assert_se(streq(strndupa(mem, len), "fooba"));
612 assert_se(unbase32hexmem("CPNMUOJ1E8======", strlen("CPNMUOJ1E8======"), true, &mem, &len) == 0);
613 assert_se(streq(strndupa(mem, len), "foobar"));
616 assert_se(unbase32hexmem("A", strlen("A"), true, &mem, &len) == -EINVAL);
617 assert_se(unbase32hexmem("A=======", strlen("A======="), true, &mem, &len) == -EINVAL);
618 assert_se(unbase32hexmem("AAA=====", strlen("AAA====="), true, &mem, &len) == -EINVAL);
619 assert_se(unbase32hexmem("AAAAAA==", strlen("AAAAAA=="), true, &mem, &len) == -EINVAL);
620 assert_se(unbase32hexmem("AB======", strlen("AB======"), true, &mem, &len) == -EINVAL);
621 assert_se(unbase32hexmem("AAAB====", strlen("AAAB===="), true, &mem, &len) == -EINVAL);
622 assert_se(unbase32hexmem("AAAAB===", strlen("AAAAB==="), true, &mem, &len) == -EINVAL);
623 assert_se(unbase32hexmem("AAAAAAB=", strlen("AAAAAAB="), true, &mem, &len) == -EINVAL);
625 assert_se(unbase32hexmem("XPNMUOJ1", strlen("CPNMUOJ1"), true, &mem, &len) == -EINVAL);
626 assert_se(unbase32hexmem("CXNMUOJ1", strlen("CPNMUOJ1"), true, &mem, &len) == -EINVAL);
627 assert_se(unbase32hexmem("CPXMUOJ1", strlen("CPNMUOJ1"), true, &mem, &len) == -EINVAL);
628 assert_se(unbase32hexmem("CPNXUOJ1", strlen("CPNMUOJ1"), true, &mem, &len) == -EINVAL);
629 assert_se(unbase32hexmem("CPNMXOJ1", strlen("CPNMUOJ1"), true, &mem, &len) == -EINVAL);
630 assert_se(unbase32hexmem("CPNMUXJ1", strlen("CPNMUOJ1"), true, &mem, &len) == -EINVAL);
631 assert_se(unbase32hexmem("CPNMUOX1", strlen("CPNMUOJ1"), true, &mem, &len) == -EINVAL);
632 assert_se(unbase32hexmem("CPNMUOJX", strlen("CPNMUOJ1"), true, &mem, &len) == -EINVAL);
634 assert_se(unbase32hexmem("", strlen(""), false, &mem, &len) == 0);
635 assert_se(streq(strndupa(mem, len), ""));
638 assert_se(unbase32hexmem("CO", strlen("CO"), false, &mem, &len) == 0);
639 assert_se(streq(strndupa(mem, len), "f"));
642 assert_se(unbase32hexmem("CPNG", strlen("CPNG"), false, &mem, &len) == 0);
643 assert_se(streq(strndupa(mem, len), "fo"));
646 assert_se(unbase32hexmem("CPNMU", strlen("CPNMU"), false, &mem, &len) == 0);
647 assert_se(streq(strndupa(mem, len), "foo"));
650 assert_se(unbase32hexmem("CPNMUOG", strlen("CPNMUOG"), false, &mem, &len) == 0);
651 assert_se(streq(strndupa(mem, len), "foob"));
654 assert_se(unbase32hexmem("CPNMUOJ1", strlen("CPNMUOJ1"), false, &mem, &len) == 0);
655 assert_se(streq(strndupa(mem, len), "fooba"));
658 assert_se(unbase32hexmem("CPNMUOJ1E8", strlen("CPNMUOJ1E8"), false, &mem, &len) == 0);
659 assert_se(streq(strndupa(mem, len), "foobar"));
662 assert_se(unbase32hexmem("CPNMUOG=", strlen("CPNMUOG="), false, &mem, &len) == -EINVAL);
663 assert_se(unbase32hexmem("CPNMUOJ1E8======", strlen("CPNMUOJ1E8======"), false, &mem, &len) == -EINVAL);
664 assert_se(unbase32hexmem("A", strlen("A"), false, &mem, &len) == -EINVAL);
665 assert_se(unbase32hexmem("A", strlen("A"), false, &mem, &len) == -EINVAL);
666 assert_se(unbase32hexmem("AAA", strlen("AAA"), false, &mem, &len) == -EINVAL);
667 assert_se(unbase32hexmem("AAAAAA", strlen("AAAAAA"), false, &mem, &len) == -EINVAL);
668 assert_se(unbase32hexmem("AB", strlen("AB"), false, &mem, &len) == -EINVAL);
669 assert_se(unbase32hexmem("AAAB", strlen("AAAB"), false, &mem, &len) == -EINVAL);
670 assert_se(unbase32hexmem("AAAAB", strlen("AAAAB"), false, &mem, &len) == -EINVAL);
671 assert_se(unbase32hexmem("AAAAAAB", strlen("AAAAAAB"), false, &mem, &len) == -EINVAL);
674 /* https://tools.ietf.org/html/rfc4648#section-10 */
675 static void test_base64mem(void) {
678 b64 = base64mem("", strlen(""));
680 assert_se(streq(b64, ""));
683 b64 = base64mem("f", strlen("f"));
685 assert_se(streq(b64, "Zg=="));
688 b64 = base64mem("fo", strlen("fo"));
690 assert_se(streq(b64, "Zm8="));
693 b64 = base64mem("foo", strlen("foo"));
695 assert_se(streq(b64, "Zm9v"));
698 b64 = base64mem("foob", strlen("foob"));
700 assert_se(streq(b64, "Zm9vYg=="));
703 b64 = base64mem("fooba", strlen("fooba"));
705 assert_se(streq(b64, "Zm9vYmE="));
708 b64 = base64mem("foobar", strlen("foobar"));
710 assert_se(streq(b64, "Zm9vYmFy"));
714 static void test_unbase64mem(void) {
718 assert_se(unbase64mem("", strlen(""), &mem, &len) == 0);
719 assert_se(streq(strndupa(mem, len), ""));
722 assert_se(unbase64mem("Zg==", strlen("Zg=="), &mem, &len) == 0);
723 assert_se(streq(strndupa(mem, len), "f"));
726 assert_se(unbase64mem("Zm8=", strlen("Zm8="), &mem, &len) == 0);
727 assert_se(streq(strndupa(mem, len), "fo"));
730 assert_se(unbase64mem("Zm9v", strlen("Zm9v"), &mem, &len) == 0);
731 assert_se(streq(strndupa(mem, len), "foo"));
734 assert_se(unbase64mem("Zm9vYg==", strlen("Zm9vYg=="), &mem, &len) == 0);
735 assert_se(streq(strndupa(mem, len), "foob"));
738 assert_se(unbase64mem("Zm9vYmE=", strlen("Zm9vYmE="), &mem, &len) == 0);
739 assert_se(streq(strndupa(mem, len), "fooba"));
742 assert_se(unbase64mem("Zm9vYmFy", strlen("Zm9vYmFy"), &mem, &len) == 0);
743 assert_se(streq(strndupa(mem, len), "foobar"));
746 assert_se(unbase64mem("A", strlen("A"), &mem, &len) == -EINVAL);
747 assert_se(unbase64mem("A====", strlen("A===="), &mem, &len) == -EINVAL);
748 assert_se(unbase64mem("AAB==", strlen("AAB=="), &mem, &len) == -EINVAL);
749 assert_se(unbase64mem("AAAB=", strlen("AAAB="), &mem, &len) == -EINVAL);
752 static void test_cescape(void) {
753 _cleanup_free_ char *escaped;
755 assert_se(escaped = cescape("abc\\\"\b\f\n\r\t\v\a\003\177\234\313"));
756 assert_se(streq(escaped, "abc\\\\\\\"\\b\\f\\n\\r\\t\\v\\a\\003\\177\\234\\313"));
759 static void test_cunescape(void) {
760 _cleanup_free_ char *unescaped;
762 assert_se(cunescape("abc\\\\\\\"\\b\\f\\a\\n\\r\\t\\v\\003\\177\\234\\313\\000\\x00", 0, &unescaped) < 0);
763 assert_se(cunescape("abc\\\\\\\"\\b\\f\\a\\n\\r\\t\\v\\003\\177\\234\\313\\000\\x00", UNESCAPE_RELAX, &unescaped) >= 0);
764 assert_se(streq_ptr(unescaped, "abc\\\"\b\f\a\n\r\t\v\003\177\234\313\\000\\x00"));
765 unescaped = mfree(unescaped);
767 /* incomplete sequences */
768 assert_se(cunescape("\\x0", 0, &unescaped) < 0);
769 assert_se(cunescape("\\x0", UNESCAPE_RELAX, &unescaped) >= 0);
770 assert_se(streq_ptr(unescaped, "\\x0"));
771 unescaped = mfree(unescaped);
773 assert_se(cunescape("\\x", 0, &unescaped) < 0);
774 assert_se(cunescape("\\x", UNESCAPE_RELAX, &unescaped) >= 0);
775 assert_se(streq_ptr(unescaped, "\\x"));
776 unescaped = mfree(unescaped);
778 assert_se(cunescape("\\", 0, &unescaped) < 0);
779 assert_se(cunescape("\\", UNESCAPE_RELAX, &unescaped) >= 0);
780 assert_se(streq_ptr(unescaped, "\\"));
781 unescaped = mfree(unescaped);
783 assert_se(cunescape("\\11", 0, &unescaped) < 0);
784 assert_se(cunescape("\\11", UNESCAPE_RELAX, &unescaped) >= 0);
785 assert_se(streq_ptr(unescaped, "\\11"));
786 unescaped = mfree(unescaped);
788 assert_se(cunescape("\\1", 0, &unescaped) < 0);
789 assert_se(cunescape("\\1", UNESCAPE_RELAX, &unescaped) >= 0);
790 assert_se(streq_ptr(unescaped, "\\1"));
791 unescaped = mfree(unescaped);
793 assert_se(cunescape("\\u0000", 0, &unescaped) < 0);
794 assert_se(cunescape("\\u00DF\\U000000df\\u03a0\\U00000041", UNESCAPE_RELAX, &unescaped) >= 0);
795 assert_se(streq_ptr(unescaped, "ßßΠA"));
796 unescaped = mfree(unescaped);
798 assert_se(cunescape("\\073", 0, &unescaped) >= 0);
799 assert_se(streq_ptr(unescaped, ";"));
802 static void test_foreach_word(void) {
803 const char *word, *state;
806 const char test[] = "test abc d\te f ";
807 const char * const expected[] = {
817 FOREACH_WORD(word, l, test, state)
818 assert_se(strneq(expected[i++], word, l));
821 static void check(const char *test, char** expected, bool trailing) {
822 const char *word, *state;
826 printf("<<<%s>>>\n", test);
827 FOREACH_WORD_QUOTED(word, l, test, state) {
828 _cleanup_free_ char *t = NULL;
830 assert_se(t = strndup(word, l));
831 assert_se(strneq(expected[i++], word, l));
834 printf("<<<%s>>>\n", state);
835 assert_se(expected[i] == NULL);
836 assert_se(isempty(state) == !trailing);
839 static void test_foreach_word_quoted(void) {
840 check("test a b c 'd' e '' '' hhh '' '' \"a b c\"",
864 static void test_memdup_multiply(void) {
865 int org[] = {1, 2, 3};
868 dup = (int*)memdup_multiply(org, sizeof(int), 3);
871 assert_se(dup[0] == 1);
872 assert_se(dup[1] == 2);
873 assert_se(dup[2] == 3);
877 static void test_u64log2(void) {
878 assert_se(u64log2(0) == 0);
879 assert_se(u64log2(8) == 3);
880 assert_se(u64log2(9) == 3);
881 assert_se(u64log2(15) == 3);
882 assert_se(u64log2(16) == 4);
883 assert_se(u64log2(1024*1024) == 20);
884 assert_se(u64log2(1024*1024+5) == 20);
887 static void test_protect_errno(void) {
893 assert_se(errno == 12);
896 static void test_parse_size(void) {
899 assert_se(parse_size("111", 1024, &bytes) == 0);
900 assert_se(bytes == 111);
902 assert_se(parse_size("111.4", 1024, &bytes) == 0);
903 assert_se(bytes == 111);
905 assert_se(parse_size(" 112 B", 1024, &bytes) == 0);
906 assert_se(bytes == 112);
908 assert_se(parse_size(" 112.6 B", 1024, &bytes) == 0);
909 assert_se(bytes == 112);
911 assert_se(parse_size("3.5 K", 1024, &bytes) == 0);
912 assert_se(bytes == 3*1024 + 512);
914 assert_se(parse_size("3. K", 1024, &bytes) == 0);
915 assert_se(bytes == 3*1024);
917 assert_se(parse_size("3.0 K", 1024, &bytes) == 0);
918 assert_se(bytes == 3*1024);
920 assert_se(parse_size("3. 0 K", 1024, &bytes) == -EINVAL);
922 assert_se(parse_size(" 4 M 11.5K", 1024, &bytes) == 0);
923 assert_se(bytes == 4*1024*1024 + 11 * 1024 + 512);
925 assert_se(parse_size("3B3.5G", 1024, &bytes) == -EINVAL);
927 assert_se(parse_size("3.5G3B", 1024, &bytes) == 0);
928 assert_se(bytes == 3ULL*1024*1024*1024 + 512*1024*1024 + 3);
930 assert_se(parse_size("3.5G 4B", 1024, &bytes) == 0);
931 assert_se(bytes == 3ULL*1024*1024*1024 + 512*1024*1024 + 4);
933 assert_se(parse_size("3B3G4T", 1024, &bytes) == -EINVAL);
935 assert_se(parse_size("4T3G3B", 1024, &bytes) == 0);
936 assert_se(bytes == (4ULL*1024 + 3)*1024*1024*1024 + 3);
938 assert_se(parse_size(" 4 T 3 G 3 B", 1024, &bytes) == 0);
939 assert_se(bytes == (4ULL*1024 + 3)*1024*1024*1024 + 3);
941 assert_se(parse_size("12P", 1024, &bytes) == 0);
942 assert_se(bytes == 12ULL * 1024*1024*1024*1024*1024);
944 assert_se(parse_size("12P12P", 1024, &bytes) == -EINVAL);
946 assert_se(parse_size("3E 2P", 1024, &bytes) == 0);
947 assert_se(bytes == (3 * 1024 + 2ULL) * 1024*1024*1024*1024*1024);
949 assert_se(parse_size("12X", 1024, &bytes) == -EINVAL);
951 assert_se(parse_size("12.5X", 1024, &bytes) == -EINVAL);
953 assert_se(parse_size("12.5e3", 1024, &bytes) == -EINVAL);
955 assert_se(parse_size("1024E", 1024, &bytes) == -ERANGE);
956 assert_se(parse_size("-1", 1024, &bytes) == -ERANGE);
957 assert_se(parse_size("-1024E", 1024, &bytes) == -ERANGE);
959 assert_se(parse_size("-1024P", 1024, &bytes) == -ERANGE);
961 assert_se(parse_size("-10B 20K", 1024, &bytes) == -ERANGE);
964 static void test_parse_cpu_set(void) {
969 /* Simple range (from CPUAffinity example) */
970 ncpus = parse_cpu_set_and_warn("1 2", &c, NULL, "fake", 1, "CPUAffinity");
971 assert_se(ncpus >= 1024);
972 assert_se(CPU_ISSET_S(1, CPU_ALLOC_SIZE(ncpus), c));
973 assert_se(CPU_ISSET_S(2, CPU_ALLOC_SIZE(ncpus), c));
974 assert_se(CPU_COUNT_S(CPU_ALLOC_SIZE(ncpus), c) == 2);
977 /* A more interesting range */
978 ncpus = parse_cpu_set_and_warn("0 1 2 3 8 9 10 11", &c, NULL, "fake", 1, "CPUAffinity");
979 assert_se(ncpus >= 1024);
980 assert_se(CPU_COUNT_S(CPU_ALLOC_SIZE(ncpus), c) == 8);
981 for (cpu = 0; cpu < 4; cpu++)
982 assert_se(CPU_ISSET_S(cpu, CPU_ALLOC_SIZE(ncpus), c));
983 for (cpu = 8; cpu < 12; cpu++)
984 assert_se(CPU_ISSET_S(cpu, CPU_ALLOC_SIZE(ncpus), c));
988 ncpus = parse_cpu_set_and_warn("8 '9' 10 \"11\"", &c, NULL, "fake", 1, "CPUAffinity");
989 assert_se(ncpus >= 1024);
990 assert_se(CPU_COUNT_S(CPU_ALLOC_SIZE(ncpus), c) == 4);
991 for (cpu = 8; cpu < 12; cpu++)
992 assert_se(CPU_ISSET_S(cpu, CPU_ALLOC_SIZE(ncpus), c));
995 /* Use commas as separators */
996 ncpus = parse_cpu_set_and_warn("0,1,2,3 8,9,10,11", &c, NULL, "fake", 1, "CPUAffinity");
997 assert_se(ncpus < 0);
1001 ncpus = parse_cpu_set_and_warn("0-3,8-11", &c, NULL, "fake", 1, "CPUAffinity");
1002 assert_se(ncpus < 0);
1006 ncpus = parse_cpu_set_and_warn("0 1 2 3 garbage", &c, NULL, "fake", 1, "CPUAffinity");
1007 assert_se(ncpus < 0);
1012 ncpus = parse_cpu_set_and_warn("", &c, NULL, "fake", 1, "CPUAffinity");
1013 assert_se(ncpus == 0); /* empty string returns 0 */
1016 /* Runnaway quoted string */
1017 ncpus = parse_cpu_set_and_warn("0 1 2 3 \"4 5 6 7 ", &c, NULL, "fake", 1, "CPUAffinity");
1018 assert_se(ncpus < 0);
1022 static void test_config_parse_iec_uint64(void) {
1023 uint64_t offset = 0;
1024 assert_se(config_parse_iec_uint64(NULL, "/this/file", 11, "Section", 22, "Size", 0, "4M", &offset, NULL) == 0);
1025 assert_se(offset == 4 * 1024 * 1024);
1027 assert_se(config_parse_iec_uint64(NULL, "/this/file", 11, "Section", 22, "Size", 0, "4.5M", &offset, NULL) == 0);
1030 static void test_strextend(void) {
1031 _cleanup_free_ char *str = strdup("0123");
1032 strextend(&str, "456", "78", "9", NULL);
1033 assert_se(streq(str, "0123456789"));
1036 static void test_strrep(void) {
1037 _cleanup_free_ char *one, *three, *zero;
1038 one = strrep("waldo", 1);
1039 three = strrep("waldo", 3);
1040 zero = strrep("waldo", 0);
1042 assert_se(streq(one, "waldo"));
1043 assert_se(streq(three, "waldowaldowaldo"));
1044 assert_se(streq(zero, ""));
1047 static void test_split_pair(void) {
1048 _cleanup_free_ char *a = NULL, *b = NULL;
1050 assert_se(split_pair("", "", &a, &b) == -EINVAL);
1051 assert_se(split_pair("foo=bar", "", &a, &b) == -EINVAL);
1052 assert_se(split_pair("", "=", &a, &b) == -EINVAL);
1053 assert_se(split_pair("foo=bar", "=", &a, &b) >= 0);
1054 assert_se(streq(a, "foo"));
1055 assert_se(streq(b, "bar"));
1058 assert_se(split_pair("==", "==", &a, &b) >= 0);
1059 assert_se(streq(a, ""));
1060 assert_se(streq(b, ""));
1064 assert_se(split_pair("===", "==", &a, &b) >= 0);
1065 assert_se(streq(a, ""));
1066 assert_se(streq(b, "="));
1069 static void test_fstab_node_to_udev_node(void) {
1072 n = fstab_node_to_udev_node("LABEL=applé/jack");
1074 assert_se(streq(n, "/dev/disk/by-label/applé\\x2fjack"));
1077 n = fstab_node_to_udev_node("PARTLABEL=pinkié pie");
1079 assert_se(streq(n, "/dev/disk/by-partlabel/pinkié\\x20pie"));
1082 n = fstab_node_to_udev_node("UUID=037b9d94-148e-4ee4-8d38-67bfe15bb535");
1084 assert_se(streq(n, "/dev/disk/by-uuid/037b9d94-148e-4ee4-8d38-67bfe15bb535"));
1087 n = fstab_node_to_udev_node("PARTUUID=037b9d94-148e-4ee4-8d38-67bfe15bb535");
1089 assert_se(streq(n, "/dev/disk/by-partuuid/037b9d94-148e-4ee4-8d38-67bfe15bb535"));
1092 n = fstab_node_to_udev_node("PONIES=awesome");
1094 assert_se(streq(n, "PONIES=awesome"));
1097 n = fstab_node_to_udev_node("/dev/xda1");
1099 assert_se(streq(n, "/dev/xda1"));
1103 static void test_get_files_in_directory(void) {
1104 _cleanup_strv_free_ char **l = NULL, **t = NULL;
1106 assert_se(get_files_in_directory("/tmp", &l) >= 0);
1107 assert_se(get_files_in_directory(".", &t) >= 0);
1108 assert_se(get_files_in_directory(".", NULL) >= 0);
1111 static void test_in_set(void) {
1112 assert_se(IN_SET(1, 1));
1113 assert_se(IN_SET(1, 1, 2, 3, 4));
1114 assert_se(IN_SET(2, 1, 2, 3, 4));
1115 assert_se(IN_SET(3, 1, 2, 3, 4));
1116 assert_se(IN_SET(4, 1, 2, 3, 4));
1117 assert_se(!IN_SET(0, 1));
1118 assert_se(!IN_SET(0, 1, 2, 3, 4));
1121 static void test_writing_tmpfile(void) {
1122 char name[] = "/tmp/test-systemd_writing_tmpfile.XXXXXX";
1123 _cleanup_free_ char *contents = NULL;
1126 struct iovec iov[3];
1128 IOVEC_SET_STRING(iov[0], "abc\n");
1129 IOVEC_SET_STRING(iov[1], ALPHANUMERICAL "\n");
1130 IOVEC_SET_STRING(iov[2], "");
1132 fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
1133 printf("tmpfile: %s", name);
1135 r = writev(fd, iov, 3);
1138 r = read_full_file(name, &contents, &size);
1140 printf("contents: %s", contents);
1141 assert_se(streq(contents, "abc\n" ALPHANUMERICAL "\n"));
1146 static void test_hexdump(void) {
1150 hexdump(stdout, NULL, 0);
1151 hexdump(stdout, "", 0);
1152 hexdump(stdout, "", 1);
1153 hexdump(stdout, "x", 1);
1154 hexdump(stdout, "x", 2);
1155 hexdump(stdout, "foobar", 7);
1156 hexdump(stdout, "f\nobar", 7);
1157 hexdump(stdout, "xxxxxxxxxxxxxxxxxxxxyz", 23);
1159 for (i = 0; i < ELEMENTSOF(data); i++)
1162 hexdump(stdout, data, sizeof(data));
1165 static void test_log2i(void) {
1166 assert_se(log2i(1) == 0);
1167 assert_se(log2i(2) == 1);
1168 assert_se(log2i(3) == 1);
1169 assert_se(log2i(4) == 2);
1170 assert_se(log2i(32) == 5);
1171 assert_se(log2i(33) == 5);
1172 assert_se(log2i(63) == 5);
1173 assert_se(log2i(INT_MAX) == sizeof(int)*8-2);
1176 static void test_foreach_string(void) {
1177 const char * const t[] = {
1186 FOREACH_STRING(x, "foo", "bar", "waldo")
1187 assert_se(streq_ptr(t[i++], x));
1191 FOREACH_STRING(x, "zzz")
1192 assert_se(streq(x, "zzz"));
1195 static void test_filename_is_valid(void) {
1196 char foo[FILENAME_MAX+2];
1199 assert_se(!filename_is_valid(""));
1200 assert_se(!filename_is_valid("/bar/foo"));
1201 assert_se(!filename_is_valid("/"));
1202 assert_se(!filename_is_valid("."));
1203 assert_se(!filename_is_valid(".."));
1205 for (i=0; i<FILENAME_MAX+1; i++)
1207 foo[FILENAME_MAX+1] = '\0';
1209 assert_se(!filename_is_valid(foo));
1211 assert_se(filename_is_valid("foo_bar-333"));
1212 assert_se(filename_is_valid("o.o"));
1215 static void test_string_has_cc(void) {
1216 assert_se(string_has_cc("abc\1", NULL));
1217 assert_se(string_has_cc("abc\x7f", NULL));
1218 assert_se(string_has_cc("abc\x7f", NULL));
1219 assert_se(string_has_cc("abc\t\x7f", "\t"));
1220 assert_se(string_has_cc("abc\t\x7f", "\t"));
1221 assert_se(string_has_cc("\x7f", "\t"));
1222 assert_se(string_has_cc("\x7f", "\t\a"));
1224 assert_se(!string_has_cc("abc\t\t", "\t"));
1225 assert_se(!string_has_cc("abc\t\t\a", "\t\a"));
1226 assert_se(!string_has_cc("a\ab\tc", "\t\a"));
1229 static void test_ascii_strlower(void) {
1230 char a[] = "AabBcC Jk Ii Od LKJJJ kkd LK";
1231 assert_se(streq(ascii_strlower(a), "aabbcc jk ii od lkjjj kkd lk"));
1234 static void test_files_same(void) {
1235 _cleanup_close_ int fd = -1;
1236 char name[] = "/tmp/test-files_same.XXXXXX";
1237 char name_alias[] = "/tmp/test-files_same.alias";
1239 fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
1241 assert_se(symlink(name, name_alias) >= 0);
1243 assert_se(files_same(name, name));
1244 assert_se(files_same(name, name_alias));
1250 static void test_is_valid_documentation_url(void) {
1251 assert_se(documentation_url_is_valid("http://www.freedesktop.org/wiki/Software/systemd"));
1252 assert_se(documentation_url_is_valid("https://www.kernel.org/doc/Documentation/binfmt_misc.txt"));
1253 assert_se(documentation_url_is_valid("file:/foo/foo"));
1254 assert_se(documentation_url_is_valid("man:systemd.special(7)"));
1255 assert_se(documentation_url_is_valid("info:bar"));
1257 assert_se(!documentation_url_is_valid("foo:"));
1258 assert_se(!documentation_url_is_valid("info:"));
1259 assert_se(!documentation_url_is_valid(""));
1262 static void test_file_in_same_dir(void) {
1265 t = file_in_same_dir("/", "a");
1266 assert_se(streq(t, "/a"));
1269 t = file_in_same_dir("/", "/a");
1270 assert_se(streq(t, "/a"));
1273 t = file_in_same_dir("", "a");
1274 assert_se(streq(t, "a"));
1277 t = file_in_same_dir("a/", "a");
1278 assert_se(streq(t, "a/a"));
1281 t = file_in_same_dir("bar/foo", "bar");
1282 assert_se(streq(t, "bar/bar"));
1286 static void test_endswith(void) {
1287 assert_se(endswith("foobar", "bar"));
1288 assert_se(endswith("foobar", ""));
1289 assert_se(endswith("foobar", "foobar"));
1290 assert_se(endswith("", ""));
1292 assert_se(!endswith("foobar", "foo"));
1293 assert_se(!endswith("foobar", "foobarfoofoo"));
1296 static void test_endswith_no_case(void) {
1297 assert_se(endswith_no_case("fooBAR", "bar"));
1298 assert_se(endswith_no_case("foobar", ""));
1299 assert_se(endswith_no_case("foobar", "FOOBAR"));
1300 assert_se(endswith_no_case("", ""));
1302 assert_se(!endswith_no_case("foobar", "FOO"));
1303 assert_se(!endswith_no_case("foobar", "FOOBARFOOFOO"));
1306 static void test_close_nointr(void) {
1307 char name[] = "/tmp/test-test-close_nointr.XXXXXX";
1310 fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
1312 assert_se(close_nointr(fd) >= 0);
1313 assert_se(close_nointr(fd) < 0);
1319 static void test_unlink_noerrno(void) {
1320 char name[] = "/tmp/test-close_nointr.XXXXXX";
1323 fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
1325 assert_se(close_nointr(fd) >= 0);
1330 assert_se(unlink_noerrno(name) >= 0);
1331 assert_se(errno == -42);
1332 assert_se(unlink_noerrno(name) < 0);
1333 assert_se(errno == -42);
1337 static void test_readlink_and_make_absolute(void) {
1338 char tempdir[] = "/tmp/test-readlink_and_make_absolute";
1339 char name[] = "/tmp/test-readlink_and_make_absolute/original";
1340 char name2[] = "test-readlink_and_make_absolute/original";
1341 char name_alias[] = "/tmp/test-readlink_and_make_absolute-alias";
1344 assert_se(mkdir_safe(tempdir, 0755, getuid(), getgid()) >= 0);
1345 assert_se(touch(name) >= 0);
1347 assert_se(symlink(name, name_alias) >= 0);
1348 assert_se(readlink_and_make_absolute(name_alias, &r) >= 0);
1349 assert_se(streq(r, name));
1351 assert_se(unlink(name_alias) >= 0);
1353 assert_se(chdir(tempdir) >= 0);
1354 assert_se(symlink(name2, name_alias) >= 0);
1355 assert_se(readlink_and_make_absolute(name_alias, &r) >= 0);
1356 assert_se(streq(r, name));
1358 assert_se(unlink(name_alias) >= 0);
1360 assert_se(rm_rf(tempdir, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0);
1363 static void test_ignore_signals(void) {
1364 assert_se(ignore_signals(SIGINT, -1) >= 0);
1365 assert_se(kill(getpid(), SIGINT) >= 0);
1366 assert_se(ignore_signals(SIGUSR1, SIGUSR2, SIGTERM, SIGPIPE, -1) >= 0);
1367 assert_se(kill(getpid(), SIGUSR1) >= 0);
1368 assert_se(kill(getpid(), SIGUSR2) >= 0);
1369 assert_se(kill(getpid(), SIGTERM) >= 0);
1370 assert_se(kill(getpid(), SIGPIPE) >= 0);
1371 assert_se(default_signals(SIGINT, SIGUSR1, SIGUSR2, SIGTERM, SIGPIPE, -1) >= 0);
1374 static void test_strshorten(void) {
1375 char s[] = "foobar";
1377 assert_se(strlen(strshorten(s, 6)) == 6);
1378 assert_se(strlen(strshorten(s, 12)) == 6);
1379 assert_se(strlen(strshorten(s, 2)) == 2);
1380 assert_se(strlen(strshorten(s, 0)) == 0);
1383 static void test_strjoina(void) {
1386 actual = strjoina("", "foo", "bar");
1387 assert_se(streq(actual, "foobar"));
1389 actual = strjoina("foo", "bar", "baz");
1390 assert_se(streq(actual, "foobarbaz"));
1392 actual = strjoina("foo", "", "bar", "baz");
1393 assert_se(streq(actual, "foobarbaz"));
1395 actual = strjoina("foo");
1396 assert_se(streq(actual, "foo"));
1398 actual = strjoina(NULL);
1399 assert_se(streq(actual, ""));
1401 actual = strjoina(NULL, "foo");
1402 assert_se(streq(actual, ""));
1404 actual = strjoina("foo", NULL, "bar");
1405 assert_se(streq(actual, "foo"));
1408 static void test_is_symlink(void) {
1409 char name[] = "/tmp/test-is_symlink.XXXXXX";
1410 char name_link[] = "/tmp/test-is_symlink.link";
1411 _cleanup_close_ int fd = -1;
1413 fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
1415 assert_se(symlink(name, name_link) >= 0);
1417 assert_se(is_symlink(name) == 0);
1418 assert_se(is_symlink(name_link) == 1);
1419 assert_se(is_symlink("/a/file/which/does/not/exist/i/guess") < 0);
1426 static void test_search_and_fopen(void) {
1427 const char *dirs[] = {"/tmp/foo/bar", "/tmp", NULL};
1428 char name[] = "/tmp/test-search_and_fopen.XXXXXX";
1433 fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
1437 r = search_and_fopen(basename(name), "r", NULL, dirs, &f);
1441 r = search_and_fopen(name, "r", NULL, dirs, &f);
1445 r = search_and_fopen(basename(name), "r", "/", dirs, &f);
1449 r = search_and_fopen("/a/file/which/does/not/exist/i/guess", "r", NULL, dirs, &f);
1451 r = search_and_fopen("afilewhichdoesnotexistiguess", "r", NULL, dirs, &f);
1457 r = search_and_fopen(basename(name), "r", NULL, dirs, &f);
1462 static void test_search_and_fopen_nulstr(void) {
1463 const char dirs[] = "/tmp/foo/bar\0/tmp\0";
1464 char name[] = "/tmp/test-search_and_fopen.XXXXXX";
1469 fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
1473 r = search_and_fopen_nulstr(basename(name), "r", NULL, dirs, &f);
1477 r = search_and_fopen_nulstr(name, "r", NULL, dirs, &f);
1481 r = search_and_fopen_nulstr("/a/file/which/does/not/exist/i/guess", "r", NULL, dirs, &f);
1483 r = search_and_fopen_nulstr("afilewhichdoesnotexistiguess", "r", NULL, dirs, &f);
1489 r = search_and_fopen_nulstr(basename(name), "r", NULL, dirs, &f);
1493 static void test_glob_exists(void) {
1494 char name[] = "/tmp/test-glob_exists.XXXXXX";
1498 fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
1502 r = glob_exists("/tmp/test-glob_exists*");
1507 r = glob_exists("/tmp/test-glob_exists*");
1511 static void test_execute_directory(void) {
1512 char template_lo[] = "/tmp/test-readlink_and_make_absolute-lo.XXXXXXX";
1513 char template_hi[] = "/tmp/test-readlink_and_make_absolute-hi.XXXXXXX";
1514 const char * dirs[] = {template_hi, template_lo, NULL};
1515 const char *name, *name2, *name3, *overridden, *override, *masked, *mask;
1517 assert_se(mkdtemp(template_lo));
1518 assert_se(mkdtemp(template_hi));
1520 name = strjoina(template_lo, "/script");
1521 name2 = strjoina(template_hi, "/script2");
1522 name3 = strjoina(template_lo, "/useless");
1523 overridden = strjoina(template_lo, "/overridden");
1524 override = strjoina(template_hi, "/overridden");
1525 masked = strjoina(template_lo, "/masked");
1526 mask = strjoina(template_hi, "/masked");
1528 assert_se(write_string_file(name, "#!/bin/sh\necho 'Executing '$0\ntouch $(dirname $0)/it_works", WRITE_STRING_FILE_CREATE) == 0);
1529 assert_se(write_string_file(name2, "#!/bin/sh\necho 'Executing '$0\ntouch $(dirname $0)/it_works2", WRITE_STRING_FILE_CREATE) == 0);
1530 assert_se(write_string_file(overridden, "#!/bin/sh\necho 'Executing '$0\ntouch $(dirname $0)/failed", WRITE_STRING_FILE_CREATE) == 0);
1531 assert_se(write_string_file(override, "#!/bin/sh\necho 'Executing '$0", WRITE_STRING_FILE_CREATE) == 0);
1532 assert_se(write_string_file(masked, "#!/bin/sh\necho 'Executing '$0\ntouch $(dirname $0)/failed", WRITE_STRING_FILE_CREATE) == 0);
1533 assert_se(symlink("/dev/null", mask) == 0);
1534 assert_se(chmod(name, 0755) == 0);
1535 assert_se(chmod(name2, 0755) == 0);
1536 assert_se(chmod(overridden, 0755) == 0);
1537 assert_se(chmod(override, 0755) == 0);
1538 assert_se(chmod(masked, 0755) == 0);
1539 assert_se(touch(name3) >= 0);
1541 execute_directories(dirs, DEFAULT_TIMEOUT_USEC, NULL);
1543 assert_se(chdir(template_lo) == 0);
1544 assert_se(access("it_works", F_OK) >= 0);
1545 assert_se(access("failed", F_OK) < 0);
1547 assert_se(chdir(template_hi) == 0);
1548 assert_se(access("it_works2", F_OK) >= 0);
1549 assert_se(access("failed", F_OK) < 0);
1551 (void) rm_rf(template_lo, REMOVE_ROOT|REMOVE_PHYSICAL);
1552 (void) rm_rf(template_hi, REMOVE_ROOT|REMOVE_PHYSICAL);
1555 static void test_extract_first_word(void) {
1556 const char *p, *original;
1559 p = original = "foobar waldo";
1560 assert_se(extract_first_word(&p, &t, NULL, 0) > 0);
1561 assert_se(streq(t, "foobar"));
1563 assert_se(p == original + 7);
1565 assert_se(extract_first_word(&p, &t, NULL, 0) > 0);
1566 assert_se(streq(t, "waldo"));
1568 assert_se(isempty(p));
1570 assert_se(extract_first_word(&p, &t, NULL, 0) == 0);
1572 assert_se(isempty(p));
1574 p = original = "\"foobar\" \'waldo\'";
1575 assert_se(extract_first_word(&p, &t, NULL, 0) > 0);
1576 assert_se(streq(t, "\"foobar\""));
1578 assert_se(p == original + 9);
1580 assert_se(extract_first_word(&p, &t, NULL, 0) > 0);
1581 assert_se(streq(t, "\'waldo\'"));
1583 assert_se(isempty(p));
1585 assert_se(extract_first_word(&p, &t, NULL, 0) == 0);
1587 assert_se(isempty(p));
1589 p = original = "\"foobar\" \'waldo\'";
1590 assert_se(extract_first_word(&p, &t, NULL, EXTRACT_QUOTES) > 0);
1591 assert_se(streq(t, "foobar"));
1593 assert_se(p == original + 9);
1595 assert_se(extract_first_word(&p, &t, NULL, EXTRACT_QUOTES) > 0);
1596 assert_se(streq(t, "waldo"));
1598 assert_se(isempty(p));
1600 assert_se(extract_first_word(&p, &t, NULL, 0) == 0);
1602 assert_se(isempty(p));
1604 p = original = "\"";
1605 assert_se(extract_first_word(&p, &t, NULL, 0) == 1);
1606 assert_se(streq(t, "\""));
1608 assert_se(isempty(p));
1610 p = original = "\"";
1611 assert_se(extract_first_word(&p, &t, NULL, EXTRACT_QUOTES) == -EINVAL);
1612 assert_se(p == original + 1);
1614 p = original = "\'";
1615 assert_se(extract_first_word(&p, &t, NULL, 0) == 1);
1616 assert_se(streq(t, "\'"));
1618 assert_se(isempty(p));
1620 p = original = "\'";
1621 assert_se(extract_first_word(&p, &t, NULL, EXTRACT_QUOTES) == -EINVAL);
1622 assert_se(p == original + 1);
1624 p = original = "\'fooo";
1625 assert_se(extract_first_word(&p, &t, NULL, 0) == 1);
1626 assert_se(streq(t, "\'fooo"));
1628 assert_se(isempty(p));
1630 p = original = "\'fooo";
1631 assert_se(extract_first_word(&p, &t, NULL, EXTRACT_QUOTES) == -EINVAL);
1632 assert_se(p == original + 5);
1634 p = original = "\'fooo";
1635 assert_se(extract_first_word(&p, &t, NULL, EXTRACT_QUOTES|EXTRACT_RELAX) > 0);
1636 assert_se(streq(t, "fooo"));
1638 assert_se(isempty(p));
1640 p = original = "yay\'foo\'bar";
1641 assert_se(extract_first_word(&p, &t, NULL, 0) > 0);
1642 assert_se(streq(t, "yay\'foo\'bar"));
1644 assert_se(isempty(p));
1646 p = original = "yay\'foo\'bar";
1647 assert_se(extract_first_word(&p, &t, NULL, EXTRACT_QUOTES) > 0);
1648 assert_se(streq(t, "yayfoobar"));
1650 assert_se(isempty(p));
1652 p = original = " foobar ";
1653 assert_se(extract_first_word(&p, &t, NULL, 0) > 0);
1654 assert_se(streq(t, "foobar"));
1656 assert_se(isempty(p));
1658 p = original = " foo\\ba\\x6ar ";
1659 assert_se(extract_first_word(&p, &t, NULL, EXTRACT_CUNESCAPE) > 0);
1660 assert_se(streq(t, "foo\ba\x6ar"));
1662 assert_se(isempty(p));
1664 p = original = " foo\\ba\\x6ar ";
1665 assert_se(extract_first_word(&p, &t, NULL, 0) > 0);
1666 assert_se(streq(t, "foobax6ar"));
1668 assert_se(isempty(p));
1670 p = original = " f\\u00f6o \"pi\\U0001F4A9le\" ";
1671 assert_se(extract_first_word(&p, &t, NULL, EXTRACT_CUNESCAPE) > 0);
1672 assert_se(streq(t, "föo"));
1674 assert_se(p == original + 13);
1676 assert_se(extract_first_word(&p, &t, NULL, EXTRACT_QUOTES|EXTRACT_CUNESCAPE) > 0);
1677 assert_se(streq(t, "pi\360\237\222\251le"));
1679 assert_se(isempty(p));
1681 p = original = "fooo\\";
1682 assert_se(extract_first_word(&p, &t, NULL, EXTRACT_RELAX) > 0);
1683 assert_se(streq(t, "fooo"));
1685 assert_se(isempty(p));
1687 p = original = "fooo\\";
1688 assert_se(extract_first_word(&p, &t, NULL, EXTRACT_CUNESCAPE_RELAX) > 0);
1689 assert_se(streq(t, "fooo\\"));
1691 assert_se(isempty(p));
1693 p = original = "fooo\\";
1694 assert_se(extract_first_word(&p, &t, NULL, EXTRACT_CUNESCAPE_RELAX|EXTRACT_RELAX) > 0);
1695 assert_se(streq(t, "fooo\\"));
1697 assert_se(isempty(p));
1699 p = original = "fooo\\";
1700 assert_se(extract_first_word(&p, &t, NULL, EXTRACT_CUNESCAPE|EXTRACT_CUNESCAPE_RELAX) > 0);
1701 assert_se(streq(t, "fooo\\"));
1703 assert_se(isempty(p));
1705 p = original = "\"foo\\";
1706 assert_se(extract_first_word(&p, &t, NULL, 0) == -EINVAL);
1707 assert_se(p == original + 5);
1709 p = original = "\"foo\\";
1710 assert_se(extract_first_word(&p, &t, NULL, EXTRACT_QUOTES|EXTRACT_RELAX) > 0);
1711 assert_se(streq(t, "foo"));
1713 assert_se(isempty(p));
1715 p = original = "foo::bar";
1716 assert_se(extract_first_word(&p, &t, ":", 0) == 1);
1717 assert_se(streq(t, "foo"));
1719 assert_se(p == original + 5);
1721 assert_se(extract_first_word(&p, &t, ":", 0) == 1);
1722 assert_se(streq(t, "bar"));
1724 assert_se(isempty(p));
1726 assert_se(extract_first_word(&p, &t, ":", 0) == 0);
1728 assert_se(isempty(p));
1730 p = original = "foo\\:bar::waldo";
1731 assert_se(extract_first_word(&p, &t, ":", 0) == 1);
1732 assert_se(streq(t, "foo:bar"));
1734 assert_se(p == original + 10);
1736 assert_se(extract_first_word(&p, &t, ":", 0) == 1);
1737 assert_se(streq(t, "waldo"));
1739 assert_se(isempty(p));
1741 assert_se(extract_first_word(&p, &t, ":", 0) == 0);
1743 assert_se(isempty(p));
1745 p = original = "\"foo\\";
1746 assert_se(extract_first_word(&p, &t, NULL, EXTRACT_QUOTES|EXTRACT_CUNESCAPE_RELAX) == -EINVAL);
1747 assert_se(p == original + 5);
1749 p = original = "\"foo\\";
1750 assert_se(extract_first_word(&p, &t, NULL, EXTRACT_QUOTES|EXTRACT_CUNESCAPE_RELAX|EXTRACT_RELAX) > 0);
1751 assert_se(streq(t, "foo\\"));
1753 assert_se(isempty(p));
1755 p = original = "\"foo\\";
1756 assert_se(extract_first_word(&p, &t, NULL, EXTRACT_QUOTES|EXTRACT_CUNESCAPE|EXTRACT_CUNESCAPE_RELAX|EXTRACT_RELAX) > 0);
1757 assert_se(streq(t, "foo\\"));
1759 assert_se(isempty(p));
1761 p = original = "fooo\\ bar quux";
1762 assert_se(extract_first_word(&p, &t, NULL, EXTRACT_RELAX) > 0);
1763 assert_se(streq(t, "fooo bar"));
1765 assert_se(p == original + 10);
1767 p = original = "fooo\\ bar quux";
1768 assert_se(extract_first_word(&p, &t, NULL, EXTRACT_CUNESCAPE_RELAX) > 0);
1769 assert_se(streq(t, "fooo bar"));
1771 assert_se(p == original + 10);
1773 p = original = "fooo\\ bar quux";
1774 assert_se(extract_first_word(&p, &t, NULL, EXTRACT_CUNESCAPE_RELAX|EXTRACT_RELAX) > 0);
1775 assert_se(streq(t, "fooo bar"));
1777 assert_se(p == original + 10);
1779 p = original = "fooo\\ bar quux";
1780 assert_se(extract_first_word(&p, &t, NULL, EXTRACT_CUNESCAPE) == -EINVAL);
1781 assert_se(p == original + 5);
1783 p = original = "fooo\\ bar quux";
1784 assert_se(extract_first_word(&p, &t, NULL, EXTRACT_CUNESCAPE|EXTRACT_CUNESCAPE_RELAX) > 0);
1785 assert_se(streq(t, "fooo\\ bar"));
1787 assert_se(p == original + 10);
1789 p = original = "\\w+@\\K[\\d.]+";
1790 assert_se(extract_first_word(&p, &t, NULL, EXTRACT_CUNESCAPE) == -EINVAL);
1791 assert_se(p == original + 1);
1793 p = original = "\\w+@\\K[\\d.]+";
1794 assert_se(extract_first_word(&p, &t, NULL, EXTRACT_CUNESCAPE|EXTRACT_CUNESCAPE_RELAX) > 0);
1795 assert_se(streq(t, "\\w+@\\K[\\d.]+"));
1797 assert_se(isempty(p));
1799 p = original = "\\w+\\b";
1800 assert_se(extract_first_word(&p, &t, NULL, EXTRACT_CUNESCAPE|EXTRACT_CUNESCAPE_RELAX) > 0);
1801 assert_se(streq(t, "\\w+\b"));
1803 assert_se(isempty(p));
1805 p = original = "-N ''";
1806 assert_se(extract_first_word(&p, &t, NULL, EXTRACT_QUOTES) > 0);
1807 assert_se(streq(t, "-N"));
1809 assert_se(p == original + 3);
1811 assert_se(extract_first_word(&p, &t, NULL, EXTRACT_QUOTES) > 0);
1812 assert_se(streq(t, ""));
1814 assert_se(isempty(p));
1816 p = original = ":foo\\:bar::waldo:";
1817 assert_se(extract_first_word(&p, &t, ":", EXTRACT_DONT_COALESCE_SEPARATORS) == 1);
1819 assert_se(streq(t, ""));
1821 assert_se(p == original + 1);
1823 assert_se(extract_first_word(&p, &t, ":", EXTRACT_DONT_COALESCE_SEPARATORS) == 1);
1824 assert_se(streq(t, "foo:bar"));
1826 assert_se(p == original + 10);
1828 assert_se(extract_first_word(&p, &t, ":", EXTRACT_DONT_COALESCE_SEPARATORS) == 1);
1830 assert_se(streq(t, ""));
1832 assert_se(p == original + 11);
1834 assert_se(extract_first_word(&p, &t, ":", EXTRACT_DONT_COALESCE_SEPARATORS) == 1);
1835 assert_se(streq(t, "waldo"));
1837 assert_se(p == original + 17);
1839 assert_se(extract_first_word(&p, &t, ":", EXTRACT_DONT_COALESCE_SEPARATORS) == 1);
1840 assert_se(streq(t, ""));
1842 assert_se(p == NULL);
1844 assert_se(extract_first_word(&p, &t, ":", EXTRACT_DONT_COALESCE_SEPARATORS) == 0);
1849 static void test_extract_first_word_and_warn(void) {
1850 const char *p, *original;
1853 p = original = "foobar waldo";
1854 assert_se(extract_first_word_and_warn(&p, &t, NULL, 0, NULL, "fake", 1, original) > 0);
1855 assert_se(streq(t, "foobar"));
1857 assert_se(p == original + 7);
1859 assert_se(extract_first_word_and_warn(&p, &t, NULL, 0, NULL, "fake", 1, original) > 0);
1860 assert_se(streq(t, "waldo"));
1862 assert_se(isempty(p));
1864 assert_se(extract_first_word_and_warn(&p, &t, NULL, 0, NULL, "fake", 1, original) == 0);
1866 assert_se(isempty(p));
1868 p = original = "\"foobar\" \'waldo\'";
1869 assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_QUOTES, NULL, "fake", 1, original) > 0);
1870 assert_se(streq(t, "foobar"));
1872 assert_se(p == original + 9);
1874 assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_QUOTES, NULL, "fake", 1, original) > 0);
1875 assert_se(streq(t, "waldo"));
1877 assert_se(isempty(p));
1879 assert_se(extract_first_word_and_warn(&p, &t, NULL, 0, NULL, "fake", 1, original) == 0);
1881 assert_se(isempty(p));
1883 p = original = "\"";
1884 assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_QUOTES, NULL, "fake", 1, original) == -EINVAL);
1885 assert_se(p == original + 1);
1887 p = original = "\'";
1888 assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_QUOTES, NULL, "fake", 1, original) == -EINVAL);
1889 assert_se(p == original + 1);
1891 p = original = "\'fooo";
1892 assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_QUOTES, NULL, "fake", 1, original) == -EINVAL);
1893 assert_se(p == original + 5);
1895 p = original = "\'fooo";
1896 assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_QUOTES|EXTRACT_RELAX, NULL, "fake", 1, original) > 0);
1897 assert_se(streq(t, "fooo"));
1899 assert_se(isempty(p));
1901 p = original = " foo\\ba\\x6ar ";
1902 assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_CUNESCAPE, NULL, "fake", 1, original) > 0);
1903 assert_se(streq(t, "foo\ba\x6ar"));
1905 assert_se(isempty(p));
1907 p = original = " foo\\ba\\x6ar ";
1908 assert_se(extract_first_word_and_warn(&p, &t, NULL, 0, NULL, "fake", 1, original) > 0);
1909 assert_se(streq(t, "foobax6ar"));
1911 assert_se(isempty(p));
1913 p = original = " f\\u00f6o \"pi\\U0001F4A9le\" ";
1914 assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_CUNESCAPE, NULL, "fake", 1, original) > 0);
1915 assert_se(streq(t, "föo"));
1917 assert_se(p == original + 13);
1919 assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_QUOTES|EXTRACT_CUNESCAPE, NULL, "fake", 1, original) > 0);
1920 assert_se(streq(t, "pi\360\237\222\251le"));
1922 assert_se(isempty(p));
1924 p = original = "fooo\\";
1925 assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_RELAX, NULL, "fake", 1, original) > 0);
1926 assert_se(streq(t, "fooo"));
1928 assert_se(isempty(p));
1930 p = original = "fooo\\";
1931 assert_se(extract_first_word_and_warn(&p, &t, NULL, 0, NULL, "fake", 1, original) > 0);
1932 assert_se(streq(t, "fooo\\"));
1934 assert_se(isempty(p));
1936 p = original = "fooo\\";
1937 assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_CUNESCAPE, NULL, "fake", 1, original) > 0);
1938 assert_se(streq(t, "fooo\\"));
1940 assert_se(isempty(p));
1942 p = original = "\"foo\\";
1943 assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_QUOTES, NULL, "fake", 1, original) == -EINVAL);
1944 assert_se(p == original + 5);
1946 p = original = "\"foo\\";
1947 assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_QUOTES|EXTRACT_RELAX, NULL, "fake", 1, original) > 0);
1948 assert_se(streq(t, "foo"));
1950 assert_se(isempty(p));
1952 p = original = "\"foo\\";
1953 assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_QUOTES|EXTRACT_CUNESCAPE, NULL, "fake", 1, original) == -EINVAL);
1954 assert_se(p == original + 5);
1956 p = original = "\"foo\\";
1957 assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_QUOTES|EXTRACT_CUNESCAPE|EXTRACT_RELAX, NULL, "fake", 1, original) > 0);
1958 assert_se(streq(t, "foo"));
1960 assert_se(isempty(p));
1962 p = original = "fooo\\ bar quux";
1963 assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_RELAX, NULL, "fake", 1, original) > 0);
1964 assert_se(streq(t, "fooo bar"));
1966 assert_se(p == original + 10);
1968 p = original = "fooo\\ bar quux";
1969 assert_se(extract_first_word_and_warn(&p, &t, NULL, 0, NULL, "fake", 1, original) > 0);
1970 assert_se(streq(t, "fooo bar"));
1972 assert_se(p == original + 10);
1974 p = original = "fooo\\ bar quux";
1975 assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_CUNESCAPE, NULL, "fake", 1, original) > 0);
1976 assert_se(streq(t, "fooo\\ bar"));
1978 assert_se(p == original + 10);
1980 p = original = "\\w+@\\K[\\d.]+";
1981 assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_CUNESCAPE, NULL, "fake", 1, original) > 0);
1982 assert_se(streq(t, "\\w+@\\K[\\d.]+"));
1984 assert_se(isempty(p));
1986 p = original = "\\w+\\b";
1987 assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_CUNESCAPE, NULL, "fake", 1, original) > 0);
1988 assert_se(streq(t, "\\w+\b"));
1990 assert_se(isempty(p));
1993 static void test_extract_many_words(void) {
1994 const char *p, *original;
1997 p = original = "foobar waldi piep";
1998 assert_se(extract_many_words(&p, NULL, 0, &a, &b, &c, NULL) == 3);
1999 assert_se(isempty(p));
2000 assert_se(streq_ptr(a, "foobar"));
2001 assert_se(streq_ptr(b, "waldi"));
2002 assert_se(streq_ptr(c, "piep"));
2007 p = original = "'foobar' wa\"ld\"i ";
2008 assert_se(extract_many_words(&p, NULL, 0, &a, &b, &c, NULL) == 2);
2009 assert_se(isempty(p));
2010 assert_se(streq_ptr(a, "'foobar'"));
2011 assert_se(streq_ptr(b, "wa\"ld\"i"));
2012 assert_se(streq_ptr(c, NULL));
2016 p = original = "'foobar' wa\"ld\"i ";
2017 assert_se(extract_many_words(&p, NULL, EXTRACT_QUOTES, &a, &b, &c, NULL) == 2);
2018 assert_se(isempty(p));
2019 assert_se(streq_ptr(a, "foobar"));
2020 assert_se(streq_ptr(b, "waldi"));
2021 assert_se(streq_ptr(c, NULL));
2026 assert_se(extract_many_words(&p, NULL, 0, &a, &b, &c, NULL) == 0);
2027 assert_se(isempty(p));
2028 assert_se(streq_ptr(a, NULL));
2029 assert_se(streq_ptr(b, NULL));
2030 assert_se(streq_ptr(c, NULL));
2033 assert_se(extract_many_words(&p, NULL, 0, &a, &b, &c, NULL) == 0);
2034 assert_se(isempty(p));
2035 assert_se(streq_ptr(a, NULL));
2036 assert_se(streq_ptr(b, NULL));
2037 assert_se(streq_ptr(c, NULL));
2039 p = original = "foobar";
2040 assert_se(extract_many_words(&p, NULL, 0, NULL) == 0);
2041 assert_se(p == original);
2043 p = original = "foobar waldi";
2044 assert_se(extract_many_words(&p, NULL, 0, &a, NULL) == 1);
2045 assert_se(p == original+7);
2046 assert_se(streq_ptr(a, "foobar"));
2049 p = original = " foobar ";
2050 assert_se(extract_many_words(&p, NULL, 0, &a, NULL) == 1);
2051 assert_se(isempty(p));
2052 assert_se(streq_ptr(a, "foobar"));
2056 static int parse_item(const char *key, const char *value) {
2059 log_info("kernel cmdline option <%s> = <%s>", key, strna(value));
2063 static void test_parse_proc_cmdline(void) {
2064 assert_se(parse_proc_cmdline(parse_item) >= 0);
2067 static void test_raw_clone(void) {
2068 pid_t parent, pid, pid2;
2071 log_info("before clone: getpid()→"PID_FMT, parent);
2072 assert_se(raw_getpid() == parent);
2074 pid = raw_clone(0, NULL);
2075 assert_se(pid >= 0);
2077 pid2 = raw_getpid();
2078 log_info("raw_clone: "PID_FMT" getpid()→"PID_FMT" raw_getpid()→"PID_FMT,
2079 pid, getpid(), pid2);
2081 assert_se(pid2 != parent);
2082 _exit(EXIT_SUCCESS);
2086 assert_se(pid2 == parent);
2087 waitpid(pid, &status, __WCLONE);
2088 assert_se(WIFEXITED(status) && WEXITSTATUS(status) == EXIT_SUCCESS);
2092 static void test_same_fd(void) {
2093 _cleanup_close_pair_ int p[2] = { -1, -1 };
2094 _cleanup_close_ int a = -1, b = -1, c = -1;
2096 assert_se(pipe2(p, O_CLOEXEC) >= 0);
2097 assert_se((a = dup(p[0])) >= 0);
2098 assert_se((b = open("/dev/null", O_RDONLY|O_CLOEXEC)) >= 0);
2099 assert_se((c = dup(a)) >= 0);
2101 assert_se(same_fd(p[0], p[0]) > 0);
2102 assert_se(same_fd(p[1], p[1]) > 0);
2103 assert_se(same_fd(a, a) > 0);
2104 assert_se(same_fd(b, b) > 0);
2106 assert_se(same_fd(a, p[0]) > 0);
2107 assert_se(same_fd(p[0], a) > 0);
2108 assert_se(same_fd(c, p[0]) > 0);
2109 assert_se(same_fd(p[0], c) > 0);
2110 assert_se(same_fd(a, c) > 0);
2111 assert_se(same_fd(c, a) > 0);
2113 assert_se(same_fd(p[0], p[1]) == 0);
2114 assert_se(same_fd(p[1], p[0]) == 0);
2115 assert_se(same_fd(p[0], b) == 0);
2116 assert_se(same_fd(b, p[0]) == 0);
2117 assert_se(same_fd(p[1], a) == 0);
2118 assert_se(same_fd(a, p[1]) == 0);
2119 assert_se(same_fd(p[1], b) == 0);
2120 assert_se(same_fd(b, p[1]) == 0);
2122 assert_se(same_fd(a, b) == 0);
2123 assert_se(same_fd(b, a) == 0);
2126 static void test_uid_ptr(void) {
2128 assert_se(UID_TO_PTR(0) != NULL);
2129 assert_se(UID_TO_PTR(1000) != NULL);
2131 assert_se(PTR_TO_UID(UID_TO_PTR(0)) == 0);
2132 assert_se(PTR_TO_UID(UID_TO_PTR(1000)) == 1000);
2135 static void test_sparse_write_one(int fd, const char *buffer, size_t n) {
2138 assert_se(lseek(fd, 0, SEEK_SET) == 0);
2139 assert_se(ftruncate(fd, 0) >= 0);
2140 assert_se(sparse_write(fd, buffer, n, 4) == (ssize_t) n);
2142 assert_se(lseek(fd, 0, SEEK_CUR) == (off_t) n);
2143 assert_se(ftruncate(fd, n) >= 0);
2145 assert_se(lseek(fd, 0, SEEK_SET) == 0);
2146 assert_se(read(fd, check, n) == (ssize_t) n);
2148 assert_se(memcmp(buffer, check, n) == 0);
2151 static void test_sparse_write(void) {
2152 const char test_a[] = "test";
2153 const char test_b[] = "\0\0\0\0test\0\0\0\0";
2154 const char test_c[] = "\0\0test\0\0\0\0";
2155 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";
2156 const char test_e[] = "test\0\0\0\0test";
2157 _cleanup_close_ int fd = -1;
2158 char fn[] = "/tmp/sparseXXXXXX";
2160 fd = mkostemp(fn, O_CLOEXEC);
2164 test_sparse_write_one(fd, test_a, sizeof(test_a));
2165 test_sparse_write_one(fd, test_b, sizeof(test_b));
2166 test_sparse_write_one(fd, test_c, sizeof(test_c));
2167 test_sparse_write_one(fd, test_d, sizeof(test_d));
2168 test_sparse_write_one(fd, test_e, sizeof(test_e));
2171 static void test_shell_escape_one(const char *s, const char *bad, const char *expected) {
2172 _cleanup_free_ char *r;
2174 assert_se(r = shell_escape(s, bad));
2175 assert_se(streq_ptr(r, expected));
2178 static void test_shell_escape(void) {
2179 test_shell_escape_one("", "", "");
2180 test_shell_escape_one("\\", "", "\\\\");
2181 test_shell_escape_one("foobar", "", "foobar");
2182 test_shell_escape_one("foobar", "o", "f\\o\\obar");
2183 test_shell_escape_one("foo:bar,baz", ",:", "foo\\:bar\\,baz");
2186 static void test_shell_maybe_quote_one(const char *s, const char *expected) {
2187 _cleanup_free_ char *r;
2189 assert_se(r = shell_maybe_quote(s));
2190 assert_se(streq(r, expected));
2193 static void test_shell_maybe_quote(void) {
2195 test_shell_maybe_quote_one("", "");
2196 test_shell_maybe_quote_one("\\", "\"\\\\\"");
2197 test_shell_maybe_quote_one("\"", "\"\\\"\"");
2198 test_shell_maybe_quote_one("foobar", "foobar");
2199 test_shell_maybe_quote_one("foo bar", "\"foo bar\"");
2200 test_shell_maybe_quote_one("foo \"bar\" waldo", "\"foo \\\"bar\\\" waldo\"");
2201 test_shell_maybe_quote_one("foo$bar", "\"foo\\$bar\"");
2204 static void test_parse_mode(void) {
2207 assert_se(parse_mode("-1", &m) < 0);
2208 assert_se(parse_mode("", &m) < 0);
2209 assert_se(parse_mode("888", &m) < 0);
2210 assert_se(parse_mode("77777", &m) < 0);
2212 assert_se(parse_mode("544", &m) >= 0 && m == 0544);
2213 assert_se(parse_mode("777", &m) >= 0 && m == 0777);
2214 assert_se(parse_mode("7777", &m) >= 0 && m == 07777);
2215 assert_se(parse_mode("0", &m) >= 0 && m == 0);
2218 static void test_tempfn(void) {
2219 char *ret = NULL, *p;
2221 assert_se(tempfn_xxxxxx("/foo/bar/waldo", NULL, &ret) >= 0);
2222 assert_se(streq_ptr(ret, "/foo/bar/.#waldoXXXXXX"));
2225 assert_se(tempfn_xxxxxx("/foo/bar/waldo", "[miau]", &ret) >= 0);
2226 assert_se(streq_ptr(ret, "/foo/bar/.#[miau]waldoXXXXXX"));
2229 assert_se(tempfn_random("/foo/bar/waldo", NULL, &ret) >= 0);
2230 assert_se(p = startswith(ret, "/foo/bar/.#waldo"));
2231 assert_se(strlen(p) == 16);
2232 assert_se(in_charset(p, "0123456789abcdef"));
2235 assert_se(tempfn_random("/foo/bar/waldo", "[wuff]", &ret) >= 0);
2236 assert_se(p = startswith(ret, "/foo/bar/.#[wuff]waldo"));
2237 assert_se(strlen(p) == 16);
2238 assert_se(in_charset(p, "0123456789abcdef"));
2241 assert_se(tempfn_random_child("/foo/bar/waldo", NULL, &ret) >= 0);
2242 assert_se(p = startswith(ret, "/foo/bar/waldo/.#"));
2243 assert_se(strlen(p) == 16);
2244 assert_se(in_charset(p, "0123456789abcdef"));
2247 assert_se(tempfn_random_child("/foo/bar/waldo", "[kikiriki]", &ret) >= 0);
2248 assert_se(p = startswith(ret, "/foo/bar/waldo/.#[kikiriki]"));
2249 assert_se(strlen(p) == 16);
2250 assert_se(in_charset(p, "0123456789abcdef"));
2254 static void test_strcmp_ptr(void) {
2255 assert_se(strcmp_ptr(NULL, NULL) == 0);
2256 assert_se(strcmp_ptr("", NULL) > 0);
2257 assert_se(strcmp_ptr("foo", NULL) > 0);
2258 assert_se(strcmp_ptr(NULL, "") < 0);
2259 assert_se(strcmp_ptr(NULL, "bar") < 0);
2260 assert_se(strcmp_ptr("foo", "bar") > 0);
2261 assert_se(strcmp_ptr("bar", "baz") < 0);
2262 assert_se(strcmp_ptr("foo", "foo") == 0);
2263 assert_se(strcmp_ptr("", "") == 0);
2266 int main(int argc, char *argv[]) {
2267 log_parse_environment();
2271 test_align_power2();
2273 test_container_of();
2275 test_div_round_up();
2278 test_parse_boolean();
2287 test_delete_chars();
2291 test_base32hexchar();
2292 test_unbase32hexchar();
2294 test_unbase64char();
2300 test_base32hexmem();
2301 test_unbase32hexmem();
2306 test_foreach_word();
2307 test_foreach_word_quoted();
2308 test_memdup_multiply();
2310 test_protect_errno();
2312 test_parse_cpu_set();
2313 test_config_parse_iec_uint64();
2317 test_fstab_node_to_udev_node();
2318 test_get_files_in_directory();
2320 test_writing_tmpfile();
2323 test_foreach_string();
2324 test_filename_is_valid();
2325 test_string_has_cc();
2326 test_ascii_strlower();
2328 test_is_valid_documentation_url();
2329 test_file_in_same_dir();
2331 test_endswith_no_case();
2332 test_close_nointr();
2333 test_unlink_noerrno();
2334 test_readlink_and_make_absolute();
2335 test_ignore_signals();
2339 test_search_and_fopen();
2340 test_search_and_fopen_nulstr();
2342 test_execute_directory();
2343 test_extract_first_word();
2344 test_extract_first_word_and_warn();
2345 test_extract_many_words();
2346 test_parse_proc_cmdline();
2350 test_sparse_write();
2351 test_shell_escape();
2352 test_shell_maybe_quote();