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/>.
37 #include "conf-parser.h"
39 static void test_streq_ptr(void) {
40 assert_se(streq_ptr(NULL, NULL));
41 assert_se(!streq_ptr("abc", "cdef"));
44 static void test_align_power2(void) {
47 assert_se(ALIGN_POWER2(0) == 0);
48 assert_se(ALIGN_POWER2(1) == 1);
49 assert_se(ALIGN_POWER2(2) == 2);
50 assert_se(ALIGN_POWER2(3) == 4);
51 assert_se(ALIGN_POWER2(12) == 16);
53 assert_se(ALIGN_POWER2(ULONG_MAX) == 0);
54 assert_se(ALIGN_POWER2(ULONG_MAX - 1) == 0);
55 assert_se(ALIGN_POWER2(ULONG_MAX - 1024) == 0);
56 assert_se(ALIGN_POWER2(ULONG_MAX / 2) == ULONG_MAX / 2 + 1);
57 assert_se(ALIGN_POWER2(ULONG_MAX + 1) == 0);
59 for (i = 1; i < 131071; ++i) {
60 for (p2 = 1; p2 < i; p2 <<= 1)
63 assert_se(ALIGN_POWER2(i) == p2);
66 for (i = ULONG_MAX - 1024; i < ULONG_MAX; ++i) {
67 for (p2 = 1; p2 && p2 < i; p2 <<= 1)
70 assert_se(ALIGN_POWER2(i) == p2);
74 static void test_max(void) {
77 int b[CONST_MAX(10, 100)];
79 .a = CONST_MAX(10, 100),
83 assert_cc(sizeof(val1.b) == sizeof(int) * 100);
85 /* CONST_MAX returns (void) instead of a value if the passed arguments
86 * are not of the same type or not constant expressions. */
87 assert_cc(__builtin_types_compatible_p(typeof(CONST_MAX(1, 10)), int));
88 assert_cc(__builtin_types_compatible_p(typeof(CONST_MAX(1, 1U)), void));
90 assert_se(val1.a == 100);
91 assert_se(MAX(++d, 0) == 1);
95 static void test_first_word(void) {
96 assert_se(first_word("Hello", ""));
97 assert_se(first_word("Hello", "Hello"));
98 assert_se(first_word("Hello world", "Hello"));
99 assert_se(first_word("Hello\tworld", "Hello"));
100 assert_se(first_word("Hello\nworld", "Hello"));
101 assert_se(first_word("Hello\rworld", "Hello"));
102 assert_se(first_word("Hello ", "Hello"));
104 assert_se(!first_word("Hello", "Hellooo"));
105 assert_se(!first_word("Hello", "xxxxx"));
106 assert_se(!first_word("Hellooo", "Hello"));
109 static void test_close_many(void) {
111 char name0[] = "/tmp/test-close-many.XXXXXX";
112 char name1[] = "/tmp/test-close-many.XXXXXX";
113 char name2[] = "/tmp/test-close-many.XXXXXX";
115 fds[0] = mkostemp_safe(name0, O_RDWR|O_CLOEXEC);
116 fds[1] = mkostemp_safe(name1, O_RDWR|O_CLOEXEC);
117 fds[2] = mkostemp_safe(name2, O_RDWR|O_CLOEXEC);
121 assert_se(fcntl(fds[0], F_GETFD) == -1);
122 assert_se(fcntl(fds[1], F_GETFD) == -1);
123 assert_se(fcntl(fds[2], F_GETFD) >= 0);
132 static void test_parse_boolean(void) {
133 assert_se(parse_boolean("1") == 1);
134 assert_se(parse_boolean("y") == 1);
135 assert_se(parse_boolean("Y") == 1);
136 assert_se(parse_boolean("yes") == 1);
137 assert_se(parse_boolean("YES") == 1);
138 assert_se(parse_boolean("true") == 1);
139 assert_se(parse_boolean("TRUE") == 1);
140 assert_se(parse_boolean("on") == 1);
141 assert_se(parse_boolean("ON") == 1);
143 assert_se(parse_boolean("0") == 0);
144 assert_se(parse_boolean("n") == 0);
145 assert_se(parse_boolean("N") == 0);
146 assert_se(parse_boolean("no") == 0);
147 assert_se(parse_boolean("NO") == 0);
148 assert_se(parse_boolean("false") == 0);
149 assert_se(parse_boolean("FALSE") == 0);
150 assert_se(parse_boolean("off") == 0);
151 assert_se(parse_boolean("OFF") == 0);
153 assert_se(parse_boolean("garbage") < 0);
154 assert_se(parse_boolean("") < 0);
155 assert_se(parse_boolean("full") < 0);
158 static void test_parse_pid(void) {
162 r = parse_pid("100", &pid);
164 assert_se(pid == 100);
166 r = parse_pid("0x7FFFFFFF", &pid);
168 assert_se(pid == 2147483647);
170 pid = 65; /* pid is left unchanged on ERANGE. Set to known arbitrary value. */
171 r = parse_pid("0", &pid);
172 assert_se(r == -ERANGE);
173 assert_se(pid == 65);
175 pid = 65; /* pid is left unchanged on ERANGE. Set to known arbitrary value. */
176 r = parse_pid("-100", &pid);
177 assert_se(r == -ERANGE);
178 assert_se(pid == 65);
180 pid = 65; /* pid is left unchanged on ERANGE. Set to known arbitrary value. */
181 r = parse_pid("0xFFFFFFFFFFFFFFFFF", &pid);
182 assert(r == -ERANGE);
183 assert_se(pid == 65);
186 static void test_parse_uid(void) {
190 r = parse_uid("100", &uid);
192 assert_se(uid == 100);
195 static void test_safe_atolli(void) {
199 r = safe_atolli("12345", &l);
201 assert_se(l == 12345);
203 r = safe_atolli("junk", &l);
204 assert_se(r == -EINVAL);
207 static void test_safe_atod(void) {
212 r = safe_atod("junk", &d);
213 assert_se(r == -EINVAL);
215 r = safe_atod("0.2244", &d);
217 assert_se(fabs(d - 0.2244) < 0.000001);
219 r = safe_atod("0,5", &d);
220 assert_se(r == -EINVAL);
224 assert_se(*e == ',');
226 /* Check if this really is locale independent */
227 if (setlocale(LC_NUMERIC, "de_DE.utf8")) {
229 r = safe_atod("0.2244", &d);
231 assert_se(fabs(d - 0.2244) < 0.000001);
233 r = safe_atod("0,5", &d);
234 assert_se(r == -EINVAL);
237 assert_se(fabs(strtod("0,5", &e) - 0.5) < 0.00001);
240 /* And check again, reset */
241 assert_se(setlocale(LC_NUMERIC, "C"));
243 r = safe_atod("0.2244", &d);
245 assert_se(fabs(d - 0.2244) < 0.000001);
247 r = safe_atod("0,5", &d);
248 assert_se(r == -EINVAL);
252 assert_se(*e == ',');
255 static void test_strappend(void) {
256 _cleanup_free_ char *t1, *t2, *t3, *t4;
258 t1 = strappend(NULL, NULL);
259 assert_se(streq(t1, ""));
261 t2 = strappend(NULL, "suf");
262 assert_se(streq(t2, "suf"));
264 t3 = strappend("pre", NULL);
265 assert_se(streq(t3, "pre"));
267 t4 = strappend("pre", "suf");
268 assert_se(streq(t4, "presuf"));
271 static void test_strstrip(void) {
273 char input[] = " hello, waldo. ";
276 assert_se(streq(r, "hello, waldo."));
279 static void test_delete_chars(void) {
281 char input[] = " hello, waldo. abc";
283 r = delete_chars(input, WHITESPACE);
284 assert_se(streq(r, "hello,waldo.abc"));
287 static void test_in_charset(void) {
288 assert_se(in_charset("dddaaabbbcccc", "abcd"));
289 assert_se(!in_charset("dddaaabbbcccc", "abc f"));
292 static void test_hexchar(void) {
293 assert_se(hexchar(0xa) == 'a');
294 assert_se(hexchar(0x0) == '0');
297 static void test_unhexchar(void) {
298 assert_se(unhexchar('a') == 0xA);
299 assert_se(unhexchar('A') == 0xA);
300 assert_se(unhexchar('0') == 0x0);
303 static void test_octchar(void) {
304 assert_se(octchar(00) == '0');
305 assert_se(octchar(07) == '7');
308 static void test_unoctchar(void) {
309 assert_se(unoctchar('0') == 00);
310 assert_se(unoctchar('7') == 07);
313 static void test_decchar(void) {
314 assert_se(decchar(0) == '0');
315 assert_se(decchar(9) == '9');
318 static void test_undecchar(void) {
319 assert_se(undecchar('0') == 0);
320 assert_se(undecchar('9') == 9);
323 static void test_cescape(void) {
324 _cleanup_free_ char *escaped;
326 assert_se(escaped = cescape("abc\\\"\b\f\n\r\t\v\a\003\177\234\313"));
327 assert_se(streq(escaped, "abc\\\\\\\"\\b\\f\\n\\r\\t\\v\\a\\003\\177\\234\\313"));
330 static void test_cunescape(void) {
331 _cleanup_free_ char *unescaped;
333 assert_se(unescaped = cunescape("abc\\\\\\\"\\b\\f\\a\\n\\r\\t\\v\\003\\177\\234\\313\\000\\x00"));
334 assert_se(streq(unescaped, "abc\\\"\b\f\a\n\r\t\v\003\177\234\313\\000\\x00"));
337 static void test_foreach_word(void) {
338 const char *word, *state;
341 const char test[] = "test abc d\te f ";
342 const char * const expected[] = {
352 FOREACH_WORD(word, l, test, state)
353 assert_se(strneq(expected[i++], word, l));
356 static void test_foreach_word_quoted(void) {
357 const char *word, *state;
360 const char test[] = "test a b c 'd' e '' '' hhh '' '' \"a b c\"";
361 const char * const expected[] = {
377 printf("<%s>\n", test);
378 FOREACH_WORD_QUOTED(word, l, test, state) {
379 _cleanup_free_ char *t = NULL;
381 assert_se(t = strndup(word, l));
382 assert_se(strneq(expected[i++], word, l));
385 assert(isempty(state));
388 static void test_default_term_for_tty(void) {
389 puts(default_term_for_tty("/dev/tty23"));
390 puts(default_term_for_tty("/dev/ttyS23"));
391 puts(default_term_for_tty("/dev/tty0"));
392 puts(default_term_for_tty("/dev/pty0"));
393 puts(default_term_for_tty("/dev/pts/0"));
394 puts(default_term_for_tty("/dev/console"));
395 puts(default_term_for_tty("tty23"));
396 puts(default_term_for_tty("ttyS23"));
397 puts(default_term_for_tty("tty0"));
398 puts(default_term_for_tty("pty0"));
399 puts(default_term_for_tty("pts/0"));
400 puts(default_term_for_tty("console"));
403 static void test_memdup_multiply(void) {
404 int org[] = {1, 2, 3};
407 dup = (int*)memdup_multiply(org, sizeof(int), 3);
410 assert_se(dup[0] == 1);
411 assert_se(dup[1] == 2);
412 assert_se(dup[2] == 3);
416 static void test_hostname_is_valid(void) {
417 assert(hostname_is_valid("foobar"));
418 assert(hostname_is_valid("foobar.com"));
419 assert(!hostname_is_valid("fööbar"));
420 assert(!hostname_is_valid(""));
421 assert(!hostname_is_valid("."));
422 assert(!hostname_is_valid(".."));
423 assert(!hostname_is_valid("foobar."));
424 assert(!hostname_is_valid(".foobar"));
425 assert(!hostname_is_valid("foo..bar"));
426 assert(!hostname_is_valid("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"));
429 static void test_u64log2(void) {
430 assert(u64log2(0) == 0);
431 assert(u64log2(8) == 3);
432 assert(u64log2(9) == 3);
433 assert(u64log2(15) == 3);
434 assert(u64log2(16) == 4);
435 assert(u64log2(1024*1024) == 20);
436 assert(u64log2(1024*1024+5) == 20);
439 static void test_get_process_comm(void) {
441 _cleanup_free_ char *a = NULL, *c = NULL, *d = NULL, *f = NULL, *i = NULL;
442 unsigned long long b;
449 if (stat("/proc/1/comm", &st) == 0) {
450 assert_se(get_process_comm(1, &a) >= 0);
451 log_info("pid1 comm: '%s'", a);
453 log_warning("/proc/1/comm does not exist.");
456 assert_se(get_starttime_of_pid(1, &b) >= 0);
457 log_info("pid1 starttime: '%llu'", b);
459 assert_se(get_process_cmdline(1, 0, true, &c) >= 0);
460 log_info("pid1 cmdline: '%s'", c);
462 assert_se(get_process_cmdline(1, 8, false, &d) >= 0);
463 log_info("pid1 cmdline truncated: '%s'", d);
465 assert_se(get_parent_of_pid(1, &e) >= 0);
466 log_info("pid1 ppid: "PID_FMT, e);
469 assert_se(is_kernel_thread(1) == 0);
471 r = get_process_exe(1, &f);
472 assert_se(r >= 0 || r == -EACCES);
473 log_info("pid1 exe: '%s'", strna(f));
475 assert_se(get_process_uid(1, &u) == 0);
476 log_info("pid1 uid: "UID_FMT, u);
479 assert_se(get_process_gid(1, &g) == 0);
480 log_info("pid1 gid: "GID_FMT, g);
483 assert(get_ctty_devnr(1, &h) == -ENOENT);
485 getenv_for_pid(1, "PATH", &i);
486 log_info("pid1 $PATH: '%s'", strna(i));
489 static void test_protect_errno(void) {
498 static void test_parse_size(void) {
501 assert_se(parse_size("111", 1024, &bytes) == 0);
502 assert_se(bytes == 111);
504 assert_se(parse_size("111.4", 1024, &bytes) == 0);
505 assert_se(bytes == 111);
507 assert_se(parse_size(" 112 B", 1024, &bytes) == 0);
508 assert_se(bytes == 112);
510 assert_se(parse_size(" 112.6 B", 1024, &bytes) == 0);
511 assert_se(bytes == 112);
513 assert_se(parse_size("3.5 K", 1024, &bytes) == 0);
514 assert_se(bytes == 3*1024 + 512);
516 assert_se(parse_size("3. K", 1024, &bytes) == 0);
517 assert_se(bytes == 3*1024);
519 assert_se(parse_size("3.0 K", 1024, &bytes) == 0);
520 assert_se(bytes == 3*1024);
522 assert_se(parse_size("3. 0 K", 1024, &bytes) == -EINVAL);
524 assert_se(parse_size(" 4 M 11.5K", 1024, &bytes) == 0);
525 assert_se(bytes == 4*1024*1024 + 11 * 1024 + 512);
527 assert_se(parse_size("3B3.5G", 1024, &bytes) == -EINVAL);
529 assert_se(parse_size("3.5G3B", 1024, &bytes) == 0);
530 assert_se(bytes == 3ULL*1024*1024*1024 + 512*1024*1024 + 3);
532 assert_se(parse_size("3.5G 4B", 1024, &bytes) == 0);
533 assert_se(bytes == 3ULL*1024*1024*1024 + 512*1024*1024 + 4);
535 assert_se(parse_size("3B3G4T", 1024, &bytes) == -EINVAL);
537 assert_se(parse_size("4T3G3B", 1024, &bytes) == 0);
538 assert_se(bytes == (4ULL*1024 + 3)*1024*1024*1024 + 3);
540 assert_se(parse_size(" 4 T 3 G 3 B", 1024, &bytes) == 0);
541 assert_se(bytes == (4ULL*1024 + 3)*1024*1024*1024 + 3);
543 assert_se(parse_size("12P", 1024, &bytes) == 0);
544 assert_se(bytes == 12ULL * 1024*1024*1024*1024*1024);
546 assert_se(parse_size("12P12P", 1024, &bytes) == -EINVAL);
548 assert_se(parse_size("3E 2P", 1024, &bytes) == 0);
549 assert_se(bytes == (3 * 1024 + 2ULL) * 1024*1024*1024*1024*1024);
551 assert_se(parse_size("12X", 1024, &bytes) == -EINVAL);
553 assert_se(parse_size("12.5X", 1024, &bytes) == -EINVAL);
555 assert_se(parse_size("12.5e3", 1024, &bytes) == -EINVAL);
557 assert_se(parse_size("1024E", 1024, &bytes) == -ERANGE);
558 assert_se(parse_size("-1", 1024, &bytes) == -ERANGE);
559 assert_se(parse_size("-1024E", 1024, &bytes) == -ERANGE);
561 assert_se(parse_size("-1024P", 1024, &bytes) == -ERANGE);
563 assert_se(parse_size("-10B 20K", 1024, &bytes) == -ERANGE);
566 static void test_config_parse_iec_off(void) {
568 assert_se(config_parse_iec_off(NULL, "/this/file", 11, "Section", 22, "Size", 0, "4M", &offset, NULL) == 0);
569 assert_se(offset == 4 * 1024 * 1024);
571 assert_se(config_parse_iec_off(NULL, "/this/file", 11, "Section", 22, "Size", 0, "4.5M", &offset, NULL) == 0);
574 static void test_strextend(void) {
575 _cleanup_free_ char *str = strdup("0123");
576 strextend(&str, "456", "78", "9", NULL);
577 assert_se(streq(str, "0123456789"));
580 static void test_strrep(void) {
581 _cleanup_free_ char *one, *three, *zero;
582 one = strrep("waldo", 1);
583 three = strrep("waldo", 3);
584 zero = strrep("waldo", 0);
586 assert_se(streq(one, "waldo"));
587 assert_se(streq(three, "waldowaldowaldo"));
588 assert_se(streq(zero, ""));
591 static void test_split_pair(void) {
592 _cleanup_free_ char *a = NULL, *b = NULL;
594 assert_se(split_pair("", "", &a, &b) == -EINVAL);
595 assert_se(split_pair("foo=bar", "", &a, &b) == -EINVAL);
596 assert_se(split_pair("", "=", &a, &b) == -EINVAL);
597 assert_se(split_pair("foo=bar", "=", &a, &b) >= 0);
598 assert_se(streq(a, "foo"));
599 assert_se(streq(b, "bar"));
602 assert_se(split_pair("==", "==", &a, &b) >= 0);
603 assert_se(streq(a, ""));
604 assert_se(streq(b, ""));
608 assert_se(split_pair("===", "==", &a, &b) >= 0);
609 assert_se(streq(a, ""));
610 assert_se(streq(b, "="));
613 static void test_fstab_node_to_udev_node(void) {
616 n = fstab_node_to_udev_node("LABEL=applé/jack");
618 assert_se(streq(n, "/dev/disk/by-label/applé\\x2fjack"));
621 n = fstab_node_to_udev_node("PARTLABEL=pinkié pie");
623 assert_se(streq(n, "/dev/disk/by-partlabel/pinkié\\x20pie"));
626 n = fstab_node_to_udev_node("UUID=037b9d94-148e-4ee4-8d38-67bfe15bb535");
628 assert_se(streq(n, "/dev/disk/by-uuid/037b9d94-148e-4ee4-8d38-67bfe15bb535"));
631 n = fstab_node_to_udev_node("PARTUUID=037b9d94-148e-4ee4-8d38-67bfe15bb535");
633 assert_se(streq(n, "/dev/disk/by-partuuid/037b9d94-148e-4ee4-8d38-67bfe15bb535"));
636 n = fstab_node_to_udev_node("PONIES=awesome");
638 assert_se(streq(n, "PONIES=awesome"));
641 n = fstab_node_to_udev_node("/dev/xda1");
643 assert_se(streq(n, "/dev/xda1"));
647 static void test_get_files_in_directory(void) {
648 _cleanup_strv_free_ char **l = NULL, **t = NULL;
650 assert_se(get_files_in_directory("/tmp", &l) >= 0);
651 assert_se(get_files_in_directory(".", &t) >= 0);
652 assert_se(get_files_in_directory(".", NULL) >= 0);
655 static void test_in_set(void) {
656 assert_se(IN_SET(1, 1));
657 assert_se(IN_SET(1, 1, 2, 3, 4));
658 assert_se(IN_SET(2, 1, 2, 3, 4));
659 assert_se(IN_SET(3, 1, 2, 3, 4));
660 assert_se(IN_SET(4, 1, 2, 3, 4));
661 assert_se(!IN_SET(0, 1));
662 assert_se(!IN_SET(0, 1, 2, 3, 4));
665 static void test_writing_tmpfile(void) {
666 char name[] = "/tmp/test-systemd_writing_tmpfile.XXXXXX";
667 _cleanup_free_ char *contents = NULL;
672 IOVEC_SET_STRING(iov[0], "abc\n");
673 IOVEC_SET_STRING(iov[1], ALPHANUMERICAL "\n");
674 IOVEC_SET_STRING(iov[2], "");
676 fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
677 printf("tmpfile: %s", name);
679 r = writev(fd, iov, 3);
682 r = read_full_file(name, &contents, &size);
684 printf("contents: %s", contents);
685 assert(streq(contents, "abc\n" ALPHANUMERICAL "\n"));
690 static void test_hexdump(void) {
694 hexdump(stdout, NULL, 0);
695 hexdump(stdout, "", 0);
696 hexdump(stdout, "", 1);
697 hexdump(stdout, "x", 1);
698 hexdump(stdout, "x", 2);
699 hexdump(stdout, "foobar", 7);
700 hexdump(stdout, "f\nobar", 7);
701 hexdump(stdout, "xxxxxxxxxxxxxxxxxxxxyz", 23);
703 for (i = 0; i < ELEMENTSOF(data); i++)
706 hexdump(stdout, data, sizeof(data));
709 static void test_log2i(void) {
710 assert_se(log2i(1) == 0);
711 assert_se(log2i(2) == 1);
712 assert_se(log2i(3) == 1);
713 assert_se(log2i(4) == 2);
714 assert_se(log2i(32) == 5);
715 assert_se(log2i(33) == 5);
716 assert_se(log2i(63) == 5);
717 assert_se(log2i(INT_MAX) == sizeof(int)*8-2);
720 static void test_foreach_string(void) {
721 const char * const t[] = {
730 FOREACH_STRING(x, "foo", "bar", "waldo")
731 assert_se(streq_ptr(t[i++], x));
735 FOREACH_STRING(x, "zzz")
736 assert_se(streq(x, "zzz"));
739 static void test_filename_is_safe(void) {
740 char foo[FILENAME_MAX+2];
743 assert_se(!filename_is_safe(""));
744 assert_se(!filename_is_safe("/bar/foo"));
745 assert_se(!filename_is_safe("/"));
746 assert_se(!filename_is_safe("."));
747 assert_se(!filename_is_safe(".."));
749 for (i=0; i<FILENAME_MAX+1; i++)
751 foo[FILENAME_MAX+1] = '\0';
753 assert_se(!filename_is_safe(foo));
755 assert_se(filename_is_safe("foo_bar-333"));
756 assert_se(filename_is_safe("o.o"));
759 static void test_string_has_cc(void) {
760 assert_se(string_has_cc("abc\1", NULL));
761 assert_se(string_has_cc("abc\x7f", NULL));
762 assert_se(string_has_cc("abc\x7f", NULL));
763 assert_se(string_has_cc("abc\t\x7f", "\t"));
764 assert_se(string_has_cc("abc\t\x7f", "\t"));
765 assert_se(string_has_cc("\x7f", "\t"));
766 assert_se(string_has_cc("\x7f", "\t\a"));
768 assert_se(!string_has_cc("abc\t\t", "\t"));
769 assert_se(!string_has_cc("abc\t\t\a", "\t\a"));
770 assert_se(!string_has_cc("a\ab\tc", "\t\a"));
773 static void test_ascii_strlower(void) {
774 char a[] = "AabBcC Jk Ii Od LKJJJ kkd LK";
775 assert_se(streq(ascii_strlower(a), "aabbcc jk ii od lkjjj kkd lk"));
778 static void test_files_same(void) {
779 _cleanup_close_ int fd = -1;
780 char name[] = "/tmp/test-files_same.XXXXXX";
781 char name_alias[] = "/tmp/test-files_same.alias";
783 fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
785 assert_se(symlink(name, name_alias) >= 0);
787 assert_se(files_same(name, name));
788 assert_se(files_same(name, name_alias));
794 static void test_is_valid_documentation_url(void) {
795 assert_se(is_valid_documentation_url("http://www.freedesktop.org/wiki/Software/systemd"));
796 assert_se(is_valid_documentation_url("https://www.kernel.org/doc/Documentation/binfmt_misc.txt"));
797 assert_se(is_valid_documentation_url("file:foo"));
798 assert_se(is_valid_documentation_url("man:systemd.special(7)"));
799 assert_se(is_valid_documentation_url("info:bar"));
801 assert_se(!is_valid_documentation_url("foo:"));
802 assert_se(!is_valid_documentation_url("info:"));
803 assert_se(!is_valid_documentation_url(""));
806 static void test_file_in_same_dir(void) {
807 assert_se(streq(file_in_same_dir("/", "a"), "/a"));
808 assert_se(streq(file_in_same_dir("/", "/a"), "/a"));
809 assert_se(streq(file_in_same_dir("", "a"), "a"));
810 assert_se(streq(file_in_same_dir("a/", "a"), "a/a"));
811 assert_se(streq(file_in_same_dir("bar/foo", "bar"), "bar/bar"));
814 static void test_endswith(void) {
815 assert_se(endswith("foobar", "bar"));
816 assert_se(endswith("foobar", ""));
817 assert_se(endswith("foobar", "foobar"));
818 assert_se(endswith("", ""));
820 assert_se(!endswith("foobar", "foo"));
821 assert_se(!endswith("foobar", "foobarfoofoo"));
824 static void test_close_nointr(void) {
825 char name[] = "/tmp/test-test-close_nointr.XXXXXX";
828 fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
830 assert_se(close_nointr(fd) >= 0);
831 assert_se(close_nointr(fd) < 0);
837 static void test_unlink_noerrno(void) {
838 char name[] = "/tmp/test-close_nointr.XXXXXX";
841 fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
843 assert_se(close_nointr(fd) >= 0);
848 assert_se(unlink_noerrno(name) >= 0);
849 assert_se(errno == -42);
850 assert_se(unlink_noerrno(name) < 0);
851 assert_se(errno == -42);
855 static void test_readlink_and_make_absolute(void) {
856 char tempdir[] = "/tmp/test-readlink_and_make_absolute";
857 char name[] = "/tmp/test-readlink_and_make_absolute/original";
858 char name2[] = "test-readlink_and_make_absolute/original";
859 char name_alias[] = "/tmp/test-readlink_and_make_absolute-alias";
862 assert(mkdir_safe(tempdir, 0755, getuid(), getgid()) >= 0);
863 assert_se(touch(name) >= 0);
865 assert_se(symlink(name, name_alias) >= 0);
866 assert_se(readlink_and_make_absolute(name_alias, &r) >= 0);
867 assert_se(streq(r, name));
869 assert_se(unlink(name_alias) >= 0);
871 assert_se(chdir(tempdir) >= 0);
872 assert_se(symlink(name2, name_alias) >= 0);
873 assert_se(readlink_and_make_absolute(name_alias, &r) >= 0);
874 assert_se(streq(r, name));
876 assert_se(unlink(name_alias) >= 0);
878 assert_se(rm_rf_dangerous(tempdir, false, true, false) >= 0);
881 static void test_read_one_char(void) {
884 char name[] = "/tmp/test-read_one_char.XXXXXX";
885 _cleanup_close_ int fd = -1;
888 fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
890 file = fdopen(fd, "r+");
892 assert_se(fputs("c\n", file) >= 0);
895 assert_se(read_one_char(file, &r, 1000000, &need_nl) >= 0);
898 assert_se(read_one_char(file, &r, 1000000, &need_nl) < 0);
901 assert_se(fputs("foobar\n", file) >= 0);
903 assert_se(read_one_char(file, &r, 1000000, &need_nl) < 0);
906 assert_se(fputs("\n", file) >= 0);
908 assert_se(read_one_char(file, &r, 1000000, &need_nl) < 0);
913 static void test_ignore_signals(void) {
914 assert_se(ignore_signals(SIGINT, -1) >= 0);
915 assert_se(kill(getpid(), SIGINT) >= 0);
916 assert_se(ignore_signals(SIGUSR1, SIGUSR2, SIGTERM, SIGPIPE, -1) >= 0);
917 assert_se(kill(getpid(), SIGUSR1) >= 0);
918 assert_se(kill(getpid(), SIGUSR2) >= 0);
919 assert_se(kill(getpid(), SIGTERM) >= 0);
920 assert_se(kill(getpid(), SIGPIPE) >= 0);
921 assert_se(default_signals(SIGINT, SIGUSR1, SIGUSR2, SIGTERM, SIGPIPE, -1) >= 0);
924 static void test_strshorten(void) {
927 assert_se(strlen(strshorten(s, 6)) == 6);
928 assert_se(strlen(strshorten(s, 12)) == 6);
929 assert_se(strlen(strshorten(s, 2)) == 2);
930 assert_se(strlen(strshorten(s, 0)) == 0);
933 static void test_strappenda(void) {
936 actual = strappenda("", "foo", "bar");
937 assert_se(streq(actual, "foobar"));
939 actual = strappenda("foo", "bar", "baz");
940 assert_se(streq(actual, "foobarbaz"));
942 actual = strappenda("foo", "", "bar", "baz");
943 assert_se(streq(actual, "foobarbaz"));
946 static void test_is_symlink(void) {
947 char name[] = "/tmp/test-is_symlink.XXXXXX";
948 char name_link[] = "/tmp/test-is_symlink.link";
949 _cleanup_close_ int fd = -1;
951 fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
953 assert_se(symlink(name, name_link) >= 0);
955 assert_se(is_symlink(name) == 0);
956 assert_se(is_symlink(name_link) == 1);
957 assert_se(is_symlink("/a/file/which/does/not/exist/i/guess") < 0);
964 static void test_pid_is_unwaited(void) {
974 waitpid(pid, &status, 0);
975 assert_se(!pid_is_unwaited(pid));
977 assert_se(pid_is_unwaited(getpid()));
978 assert_se(!pid_is_unwaited(-1));
981 static void test_pid_is_alive(void) {
991 waitpid(pid, &status, 0);
992 assert_se(!pid_is_alive(pid));
994 assert_se(pid_is_alive(getpid()));
995 assert_se(!pid_is_alive(-1));
998 static void test_search_and_fopen(void) {
999 const char *dirs[] = {"/tmp/foo/bar", "/tmp", NULL};
1000 char name[] = "/tmp/test-search_and_fopen.XXXXXX";
1005 fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
1009 r = search_and_fopen(basename(name), "r", NULL, dirs, &f);
1013 r = search_and_fopen(name, "r", NULL, dirs, &f);
1017 r = search_and_fopen(basename(name), "r", "/", dirs, &f);
1021 r = search_and_fopen("/a/file/which/does/not/exist/i/guess", "r", NULL, dirs, &f);
1023 r = search_and_fopen("afilewhichdoesnotexistiguess", "r", NULL, dirs, &f);
1029 r = search_and_fopen(basename(name), "r", NULL, dirs, &f);
1034 static void test_search_and_fopen_nulstr(void) {
1035 const char dirs[] = "/tmp/foo/bar\0/tmp\0";
1036 char name[] = "/tmp/test-search_and_fopen.XXXXXX";
1041 fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
1045 r = search_and_fopen_nulstr(basename(name), "r", NULL, dirs, &f);
1049 r = search_and_fopen_nulstr(name, "r", NULL, dirs, &f);
1053 r = search_and_fopen_nulstr("/a/file/which/does/not/exist/i/guess", "r", NULL, dirs, &f);
1055 r = search_and_fopen_nulstr("afilewhichdoesnotexistiguess", "r", NULL, dirs, &f);
1061 r = search_and_fopen_nulstr(basename(name), "r", NULL, dirs, &f);
1065 static void test_glob_exists(void) {
1066 char name[] = "/tmp/test-glob_exists.XXXXXX";
1070 fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
1074 r = glob_exists("/tmp/test-glob_exists*");
1079 r = glob_exists("/tmp/test-glob_exists*");
1083 static void test_execute_directory(void) {
1084 char name[] = "/tmp/test-execute_directory/script1";
1085 char name2[] = "/tmp/test-execute_directory/script2";
1086 char name3[] = "/tmp/test-execute_directory/useless";
1087 char tempdir[] = "/tmp/test-execute_directory/";
1089 assert_se(mkdir_safe(tempdir, 0755, getuid(), getgid()) >= 0);
1090 assert_se(write_string_file(name, "#!/bin/sh\necho 'Executing '$0\ntouch /tmp/test-execute_directory/it_works") == 0);
1091 assert_se(write_string_file(name2, "#!/bin/sh\necho 'Executing '$0\ntouch /tmp/test-execute_directory/it_works2") == 0);
1092 assert_se(chmod(name, 0755) == 0);
1093 assert_se(chmod(name2, 0755) == 0);
1094 assert_se(touch(name3) >= 0);
1096 execute_directory(tempdir, NULL, DEFAULT_TIMEOUT_USEC, NULL);
1097 assert_se(access("/tmp/test-execute_directory/it_works", F_OK) >= 0);
1098 assert_se(access("/tmp/test-execute_directory/it_works2", F_OK) >= 0);
1100 rm_rf_dangerous(tempdir, false, true, false);
1103 static void test_unquote_first_word(void) {
1104 const char *p, *original;
1107 p = original = "foobar waldo";
1108 assert_se(unquote_first_word(&p, &t) > 0);
1109 assert_se(streq(t, "foobar"));
1111 assert_se(p == original + 7);
1113 assert_se(unquote_first_word(&p, &t) > 0);
1114 assert_se(streq(t, "waldo"));
1116 assert_se(p == original + 12);
1118 assert_se(unquote_first_word(&p, &t) == 0);
1120 assert_se(p == original + 12);
1122 p = original = "\"foobar\" \'waldo\'";
1123 assert_se(unquote_first_word(&p, &t) > 0);
1124 assert_se(streq(t, "foobar"));
1126 assert_se(p == original + 9);
1128 assert_se(unquote_first_word(&p, &t) > 0);
1129 assert_se(streq(t, "waldo"));
1131 assert_se(p == original + 16);
1133 assert_se(unquote_first_word(&p, &t) == 0);
1135 assert_se(p == original + 16);
1137 p = original = "\"";
1138 assert_se(unquote_first_word(&p, &t) == -EINVAL);
1139 assert_se(p == original + 1);
1141 p = original = "\'";
1142 assert_se(unquote_first_word(&p, &t) == -EINVAL);
1143 assert_se(p == original + 1);
1145 p = original = "yay\'foo\'bar";
1146 assert_se(unquote_first_word(&p, &t) > 0);
1147 assert_se(streq(t, "yayfoobar"));
1149 assert_se(p == original + 11);
1151 p = original = " foobar ";
1152 assert_se(unquote_first_word(&p, &t) > 0);
1153 assert_se(streq(t, "foobar"));
1155 assert_se(p == original + 12);
1158 static void test_unquote_many_words(void) {
1159 const char *p, *original;
1162 p = original = "foobar waldi piep";
1163 assert_se(unquote_many_words(&p, &a, &b, &c, NULL) == 3);
1164 assert_se(p == original + 17);
1165 assert_se(streq_ptr(a, "foobar"));
1166 assert_se(streq_ptr(b, "waldi"));
1167 assert_se(streq_ptr(c, "piep"));
1172 p = original = "'foobar' wa\"ld\"i ";
1173 assert_se(unquote_many_words(&p, &a, &b, &c, NULL) == 2);
1174 assert_se(p == original + 19);
1175 assert_se(streq_ptr(a, "foobar"));
1176 assert_se(streq_ptr(b, "waldi"));
1177 assert_se(streq_ptr(c, NULL));
1182 assert_se(unquote_many_words(&p, &a, &b, &c, NULL) == 0);
1183 assert_se(p == original);
1184 assert_se(streq_ptr(a, NULL));
1185 assert_se(streq_ptr(b, NULL));
1186 assert_se(streq_ptr(c, NULL));
1189 assert_se(unquote_many_words(&p, &a, &b, &c, NULL) == 0);
1190 assert_se(p == original+2);
1191 assert_se(streq_ptr(a, NULL));
1192 assert_se(streq_ptr(b, NULL));
1193 assert_se(streq_ptr(c, NULL));
1195 p = original = "foobar";
1196 assert_se(unquote_many_words(&p, NULL) == 0);
1197 assert_se(p == original);
1199 p = original = "foobar waldi";
1200 assert_se(unquote_many_words(&p, &a, NULL) == 1);
1201 assert_se(p == original+7);
1202 assert_se(streq_ptr(a, "foobar"));
1204 p = original = " foobar ";
1205 assert_se(unquote_many_words(&p, &a, NULL) == 1);
1206 assert_se(p == original+15);
1207 assert_se(streq_ptr(a, "foobar"));
1210 int main(int argc, char *argv[]) {
1211 log_parse_environment();
1215 test_align_power2();
1219 test_parse_boolean();
1226 test_delete_chars();
1236 test_foreach_word();
1237 test_foreach_word_quoted();
1238 test_default_term_for_tty();
1239 test_memdup_multiply();
1240 test_hostname_is_valid();
1242 test_get_process_comm();
1243 test_protect_errno();
1245 test_config_parse_iec_off();
1249 test_fstab_node_to_udev_node();
1250 test_get_files_in_directory();
1252 test_writing_tmpfile();
1255 test_foreach_string();
1256 test_filename_is_safe();
1257 test_string_has_cc();
1258 test_ascii_strlower();
1260 test_is_valid_documentation_url();
1261 test_file_in_same_dir();
1263 test_close_nointr();
1264 test_unlink_noerrno();
1265 test_readlink_and_make_absolute();
1266 test_read_one_char();
1267 test_ignore_signals();
1271 test_pid_is_unwaited();
1272 test_pid_is_alive();
1273 test_search_and_fopen();
1274 test_search_and_fopen_nulstr();
1276 test_execute_directory();
1277 test_unquote_first_word();
1278 test_unquote_many_words();