2 This file is part of systemd.
4 Copyright 2013 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
24 #include "alloc-util.h"
31 #include "parse-util.h"
32 #include "process-util.h"
33 #include "string-util.h"
37 static void test_parse_env_file(void) {
38 char t
[] = "/tmp/test-fileio-in-XXXXXX",
39 p
[] = "/tmp/test-fileio-out-XXXXXX";
42 _cleanup_free_
char *one
= NULL
, *two
= NULL
, *three
= NULL
, *four
= NULL
, *five
= NULL
,
43 *six
= NULL
, *seven
= NULL
, *eight
= NULL
, *nine
= NULL
, *ten
= NULL
;
44 _cleanup_strv_free_
char **a
= NULL
, **b
= NULL
;
48 fd
= mkostemp_safe(p
);
52 fd
= mkostemp_safe(t
);
64 "invalid line #comment\n"
67 "four = \'44\\\"44\'\n"
68 "five = \'55\\\'55\' \"FIVE\" cinco \n"
69 "six = seis sechs\\\n"
71 "seven=\"sevenval\" #nocomment\n"
72 "eight=eightval #nocomment\n"
73 "export nine=nineval\n"
81 r
= load_env_file(NULL
, t
, NULL
, &a
);
85 log_info("Got: <%s>", *i
);
87 assert_se(streq_ptr(a
[0], "one=BAR"));
88 assert_se(streq_ptr(a
[1], "two=bar"));
89 assert_se(streq_ptr(a
[2], "three=333\nxxxx"));
90 assert_se(streq_ptr(a
[3], "four=44\"44"));
91 assert_se(streq_ptr(a
[4], "five=55\'55FIVEcinco"));
92 assert_se(streq_ptr(a
[5], "six=seis sechs sis"));
93 assert_se(streq_ptr(a
[6], "seven=sevenval#nocomment"));
94 assert_se(streq_ptr(a
[7], "eight=eightval #nocomment"));
95 assert_se(streq_ptr(a
[8], "export nine=nineval"));
96 assert_se(streq_ptr(a
[9], "ten="));
97 assert_se(a
[10] == NULL
);
103 log_info("Got2: <%s>", *i
);
104 assert_se(streq(*i
, a
[k
++]));
117 "export nine", &nine
,
123 log_info("one=[%s]", strna(one
));
124 log_info("two=[%s]", strna(two
));
125 log_info("three=[%s]", strna(three
));
126 log_info("four=[%s]", strna(four
));
127 log_info("five=[%s]", strna(five
));
128 log_info("six=[%s]", strna(six
));
129 log_info("seven=[%s]", strna(seven
));
130 log_info("eight=[%s]", strna(eight
));
131 log_info("export nine=[%s]", strna(nine
));
132 log_info("ten=[%s]", strna(nine
));
134 assert_se(streq(one
, "BAR"));
135 assert_se(streq(two
, "bar"));
136 assert_se(streq(three
, "333\nxxxx"));
137 assert_se(streq(four
, "44\"44"));
138 assert_se(streq(five
, "55\'55FIVEcinco"));
139 assert_se(streq(six
, "seis sechs sis"));
140 assert_se(streq(seven
, "sevenval#nocomment"));
141 assert_se(streq(eight
, "eightval #nocomment"));
142 assert_se(streq(nine
, "nineval"));
143 assert_se(ten
== NULL
);
145 r
= write_env_file(p
, a
);
148 r
= load_env_file(NULL
, p
, NULL
, &b
);
155 static void test_parse_multiline_env_file(void) {
156 char t
[] = "/tmp/test-fileio-in-XXXXXX",
157 p
[] = "/tmp/test-fileio-out-XXXXXX";
160 _cleanup_strv_free_
char **a
= NULL
, **b
= NULL
;
163 fd
= mkostemp_safe(p
);
167 fd
= mkostemp_safe(t
);
188 r
= load_env_file(NULL
, t
, NULL
, &a
);
192 log_info("Got: <%s>", *i
);
194 assert_se(streq_ptr(a
[0], "one=BAR VAR\tGAR"));
195 assert_se(streq_ptr(a
[1], "two=bar var\tgar"));
196 assert_se(streq_ptr(a
[2], "tri=bar var \tgar "));
197 assert_se(a
[3] == NULL
);
199 r
= write_env_file(p
, a
);
202 r
= load_env_file(NULL
, p
, NULL
, &b
);
209 static void test_merge_env_file(void) {
210 char t
[] = "/tmp/test-fileio-XXXXXX";
212 _cleanup_fclose_
FILE *f
= NULL
;
213 _cleanup_strv_free_
char **a
= NULL
;
216 fd
= mkostemp_safe(t
);
219 log_info("/* %s (%s) */", __func__
, t
);
224 r
= write_string_stream(f
,
227 "twentyone=2${one}\n"
229 "twentytwo=2${one}\n"
230 "xxx_minus_three=$xxx - 3\n"
231 "xxx=0x$one$one$one\n"
232 "yyy=${one:-fallback}\n"
233 "zzz=${one:+replacement}\n"
234 "zzzz=${foobar:-${nothing}}\n"
235 "zzzzz=${nothing:+${nothing}}\n"
236 , WRITE_STRING_FILE_AVOID_NEWLINE
);
239 r
= merge_env_file(&a
, NULL
, t
);
244 log_info("Got: <%s>", *i
);
246 assert_se(streq(a
[0], "one=2"));
247 assert_se(streq(a
[1], "twelve=12"));
248 assert_se(streq(a
[2], "twentyone=21"));
249 assert_se(streq(a
[3], "twentytwo=22"));
250 assert_se(streq(a
[4], "xxx=0x222"));
251 assert_se(streq(a
[5], "xxx_minus_three= - 3"));
252 assert_se(streq(a
[6], "yyy=2"));
253 assert_se(streq(a
[7], "zzz=replacement"));
254 assert_se(streq(a
[8], "zzzz="));
255 assert_se(streq(a
[9], "zzzzz="));
256 assert_se(a
[10] == NULL
);
258 r
= merge_env_file(&a
, NULL
, t
);
263 log_info("Got2: <%s>", *i
);
265 assert_se(streq(a
[0], "one=2"));
266 assert_se(streq(a
[1], "twelve=12"));
267 assert_se(streq(a
[2], "twentyone=21"));
268 assert_se(streq(a
[3], "twentytwo=22"));
269 assert_se(streq(a
[4], "xxx=0x222"));
270 assert_se(streq(a
[5], "xxx_minus_three=0x222 - 3"));
271 assert_se(streq(a
[6], "yyy=2"));
272 assert_se(streq(a
[7], "zzz=replacement"));
273 assert_se(streq(a
[8], "zzzz="));
274 assert_se(streq(a
[9], "zzzzz="));
275 assert_se(a
[10] == NULL
);
278 static void test_merge_env_file_invalid(void) {
279 char t
[] = "/tmp/test-fileio-XXXXXX";
281 _cleanup_fclose_
FILE *f
= NULL
;
282 _cleanup_strv_free_
char **a
= NULL
;
285 fd
= mkostemp_safe(t
);
288 log_info("/* %s (%s) */", __func__
, t
);
293 r
= write_string_stream(f
,
302 ";comment2=comment2\n"
304 "\n\n" /* empty line */
305 , WRITE_STRING_FILE_AVOID_NEWLINE
);
308 r
= merge_env_file(&a
, NULL
, t
);
312 log_info("Got: <%s>", *i
);
314 assert_se(strv_isempty(a
));
317 static void test_executable_is_script(void) {
318 char t
[] = "/tmp/test-executable-XXXXXX";
323 fd
= mkostemp_safe(t
);
329 fputs("#! /bin/script -a -b \ngoo goo", f
);
332 r
= executable_is_script(t
, &command
);
334 assert_se(streq(command
, "/bin/script"));
337 r
= executable_is_script("/bin/sh", &command
);
340 r
= executable_is_script("/usr/bin/yum", &command
);
341 assert_se(r
> 0 || r
== -ENOENT
);
343 assert_se(startswith(command
, "/"));
351 static void test_status_field(void) {
352 _cleanup_free_
char *t
= NULL
, *p
= NULL
, *s
= NULL
, *z
= NULL
;
353 unsigned long long total
= 0, buffers
= 0;
356 assert_se(get_proc_field("/proc/self/status", "Threads", WHITESPACE
, &t
) == 0);
358 assert_se(streq(t
, "1"));
360 r
= get_proc_field("/proc/meminfo", "MemTotal", WHITESPACE
, &p
);
364 assert_se(safe_atollu(p
, &total
) == 0);
367 r
= get_proc_field("/proc/meminfo", "Buffers", WHITESPACE
, &s
);
371 assert_se(safe_atollu(s
, &buffers
) == 0);
375 assert_se(buffers
< total
);
377 /* Seccomp should be a good test for field full of zeros. */
378 r
= get_proc_field("/proc/meminfo", "Seccomp", WHITESPACE
, &z
);
382 assert_se(safe_atollu(z
, &buffers
) == 0);
386 static void test_capeff(void) {
389 for (pid
= 0; pid
< 2; pid
++) {
390 _cleanup_free_
char *capeff
= NULL
;
393 r
= get_process_capeff(0, &capeff
);
394 log_info("capeff: '%s' (r=%d)", capeff
, r
);
396 if (IN_SET(r
, -ENOENT
, -EPERM
))
401 p
= capeff
[strspn(capeff
, HEXDIGITS
)];
402 assert_se(!p
|| isspace(p
));
406 static void test_write_string_stream(void) {
407 char fn
[] = "/tmp/test-write_string_stream-XXXXXX";
408 _cleanup_fclose_
FILE *f
= NULL
;
412 fd
= mkostemp_safe(fn
);
417 assert_se(write_string_stream(f
, "boohoo", 0) < 0);
419 f
= freopen(fn
, "r+", f
);
422 assert_se(write_string_stream(f
, "boohoo", 0) == 0);
425 assert_se(fgets(buf
, sizeof(buf
), f
));
426 assert_se(streq(buf
, "boohoo\n"));
428 f
= freopen(fn
, "w+", f
);
431 assert_se(write_string_stream(f
, "boohoo", WRITE_STRING_FILE_AVOID_NEWLINE
) == 0);
434 assert_se(fgets(buf
, sizeof(buf
), f
));
436 assert_se(streq(buf
, "boohoo"));
441 static void test_write_string_file(void) {
442 char fn
[] = "/tmp/test-write_string_file-XXXXXX";
444 _cleanup_close_
int fd
;
446 fd
= mkostemp_safe(fn
);
449 assert_se(write_string_file(fn
, "boohoo", WRITE_STRING_FILE_CREATE
) == 0);
451 assert_se(read(fd
, buf
, sizeof(buf
)) == 7);
452 assert_se(streq(buf
, "boohoo\n"));
457 static void test_write_string_file_no_create(void) {
458 char fn
[] = "/tmp/test-write_string_file_no_create-XXXXXX";
459 _cleanup_close_
int fd
;
462 fd
= mkostemp_safe(fn
);
465 assert_se(write_string_file("/a/file/which/does/not/exists/i/guess", "boohoo", 0) < 0);
466 assert_se(write_string_file(fn
, "boohoo", 0) == 0);
468 assert_se(read(fd
, buf
, sizeof(buf
)) == strlen("boohoo\n"));
469 assert_se(streq(buf
, "boohoo\n"));
474 static void test_write_string_file_verify(void) {
475 _cleanup_free_
char *buf
= NULL
, *buf2
= NULL
;
478 assert_se(read_one_line_file("/proc/cmdline", &buf
) >= 0);
479 assert_se((buf2
= strjoin(buf
, "\n")));
481 r
= write_string_file("/proc/cmdline", buf
, 0);
482 assert_se(IN_SET(r
, -EACCES
, -EIO
));
483 r
= write_string_file("/proc/cmdline", buf2
, 0);
484 assert_se(IN_SET(r
, -EACCES
, -EIO
));
486 assert_se(write_string_file("/proc/cmdline", buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
) == 0);
487 assert_se(write_string_file("/proc/cmdline", buf2
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
) == 0);
489 r
= write_string_file("/proc/cmdline", buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
|WRITE_STRING_FILE_AVOID_NEWLINE
);
490 assert_se(IN_SET(r
, -EACCES
, -EIO
));
491 assert_se(write_string_file("/proc/cmdline", buf2
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
|WRITE_STRING_FILE_AVOID_NEWLINE
) == 0);
494 static void test_load_env_file_pairs(void) {
495 char fn
[] = "/tmp/test-load_env_file_pairs-XXXXXX";
498 _cleanup_fclose_
FILE *f
= NULL
;
499 _cleanup_strv_free_
char **l
= NULL
;
502 fd
= mkostemp_safe(fn
);
505 r
= write_string_file(fn
,
506 "NAME=\"Arch Linux\"\n"
508 "PRETTY_NAME=\"Arch Linux\"\n"
509 "ANSI_COLOR=\"0;36\"\n"
510 "HOME_URL=\"https://www.archlinux.org/\"\n"
511 "SUPPORT_URL=\"https://bbs.archlinux.org/\"\n"
512 "BUG_REPORT_URL=\"https://bugs.archlinux.org/\"\n",
513 WRITE_STRING_FILE_CREATE
);
519 r
= load_env_file_pairs(f
, fn
, NULL
, &l
);
522 assert_se(strv_length(l
) == 14);
523 STRV_FOREACH_PAIR(k
, v
, l
) {
524 assert_se(STR_IN_SET(*k
, "NAME", "ID", "PRETTY_NAME", "ANSI_COLOR", "HOME_URL", "SUPPORT_URL", "BUG_REPORT_URL"));
525 printf("%s=%s\n", *k
, *v
);
526 if (streq(*k
, "NAME")) assert_se(streq(*v
, "Arch Linux"));
527 if (streq(*k
, "ID")) assert_se(streq(*v
, "arch"));
528 if (streq(*k
, "PRETTY_NAME")) assert_se(streq(*v
, "Arch Linux"));
529 if (streq(*k
, "ANSI_COLOR")) assert_se(streq(*v
, "0;36"));
530 if (streq(*k
, "HOME_URL")) assert_se(streq(*v
, "https://www.archlinux.org/"));
531 if (streq(*k
, "SUPPORT_URL")) assert_se(streq(*v
, "https://bbs.archlinux.org/"));
532 if (streq(*k
, "BUG_REPORT_URL")) assert_se(streq(*v
, "https://bugs.archlinux.org/"));
538 static void test_search_and_fopen(void) {
539 const char *dirs
[] = {"/tmp/foo/bar", "/tmp", NULL
};
540 char name
[] = "/tmp/test-search_and_fopen.XXXXXX";
545 fd
= mkostemp_safe(name
);
549 r
= search_and_fopen(basename(name
), "r", NULL
, dirs
, &f
);
553 r
= search_and_fopen(name
, "r", NULL
, dirs
, &f
);
557 r
= search_and_fopen(basename(name
), "r", "/", dirs
, &f
);
561 r
= search_and_fopen("/a/file/which/does/not/exist/i/guess", "r", NULL
, dirs
, &f
);
563 r
= search_and_fopen("afilewhichdoesnotexistiguess", "r", NULL
, dirs
, &f
);
569 r
= search_and_fopen(basename(name
), "r", NULL
, dirs
, &f
);
574 static void test_search_and_fopen_nulstr(void) {
575 const char dirs
[] = "/tmp/foo/bar\0/tmp\0";
576 char name
[] = "/tmp/test-search_and_fopen.XXXXXX";
581 fd
= mkostemp_safe(name
);
585 r
= search_and_fopen_nulstr(basename(name
), "r", NULL
, dirs
, &f
);
589 r
= search_and_fopen_nulstr(name
, "r", NULL
, dirs
, &f
);
593 r
= search_and_fopen_nulstr("/a/file/which/does/not/exist/i/guess", "r", NULL
, dirs
, &f
);
595 r
= search_and_fopen_nulstr("afilewhichdoesnotexistiguess", "r", NULL
, dirs
, &f
);
601 r
= search_and_fopen_nulstr(basename(name
), "r", NULL
, dirs
, &f
);
605 static void test_writing_tmpfile(void) {
606 char name
[] = "/tmp/test-systemd_writing_tmpfile.XXXXXX";
607 _cleanup_free_
char *contents
= NULL
;
612 iov
[0] = IOVEC_MAKE_STRING("abc\n");
613 iov
[1] = IOVEC_MAKE_STRING(ALPHANUMERICAL
"\n");
614 iov
[2] = IOVEC_MAKE_STRING("");
616 fd
= mkostemp_safe(name
);
617 printf("tmpfile: %s", name
);
619 r
= writev(fd
, iov
, 3);
622 r
= read_full_file(name
, &contents
, &size
);
624 printf("contents: %s", contents
);
625 assert_se(streq(contents
, "abc\n" ALPHANUMERICAL
"\n"));
630 static void test_tempfn(void) {
631 char *ret
= NULL
, *p
;
633 assert_se(tempfn_xxxxxx("/foo/bar/waldo", NULL
, &ret
) >= 0);
634 assert_se(streq_ptr(ret
, "/foo/bar/.#waldoXXXXXX"));
637 assert_se(tempfn_xxxxxx("/foo/bar/waldo", "[miau]", &ret
) >= 0);
638 assert_se(streq_ptr(ret
, "/foo/bar/.#[miau]waldoXXXXXX"));
641 assert_se(tempfn_random("/foo/bar/waldo", NULL
, &ret
) >= 0);
642 assert_se(p
= startswith(ret
, "/foo/bar/.#waldo"));
643 assert_se(strlen(p
) == 16);
644 assert_se(in_charset(p
, "0123456789abcdef"));
647 assert_se(tempfn_random("/foo/bar/waldo", "[wuff]", &ret
) >= 0);
648 assert_se(p
= startswith(ret
, "/foo/bar/.#[wuff]waldo"));
649 assert_se(strlen(p
) == 16);
650 assert_se(in_charset(p
, "0123456789abcdef"));
653 assert_se(tempfn_random_child("/foo/bar/waldo", NULL
, &ret
) >= 0);
654 assert_se(p
= startswith(ret
, "/foo/bar/waldo/.#"));
655 assert_se(strlen(p
) == 16);
656 assert_se(in_charset(p
, "0123456789abcdef"));
659 assert_se(tempfn_random_child("/foo/bar/waldo", "[kikiriki]", &ret
) >= 0);
660 assert_se(p
= startswith(ret
, "/foo/bar/waldo/.#[kikiriki]"));
661 assert_se(strlen(p
) == 16);
662 assert_se(in_charset(p
, "0123456789abcdef"));
666 static const char buffer
[] =
668 "With newlines, and a NUL byte\0"
672 "and a very long line that is supposed to be truncated, because it is so long\n";
674 static void test_read_line_one_file(FILE *f
) {
675 _cleanup_free_
char *line
= NULL
;
677 assert_se(read_line(f
, (size_t) -1, &line
) == 15 && streq(line
, "Some test data"));
680 assert_se(read_line(f
, 1024, &line
) == 30 && streq(line
, "With newlines, and a NUL byte"));
683 assert_se(read_line(f
, 1024, &line
) == 1 && streq(line
, ""));
686 assert_se(read_line(f
, 1024, &line
) == 14 && streq(line
, "an empty line"));
689 assert_se(read_line(f
, (size_t) -1, NULL
) == 16);
691 assert_se(read_line(f
, 16, &line
) == -ENOBUFS
);
694 /* read_line() stopped when it hit the limit, that means when we continue reading we'll read at the first
695 * character after the previous limit. Let's make use of tha to continue our test. */
696 assert_se(read_line(f
, 1024, &line
) == 61 && streq(line
, "line that is supposed to be truncated, because it is so long"));
699 assert_se(read_line(f
, 1024, &line
) == 1 && streq(line
, ""));
702 assert_se(read_line(f
, 1024, &line
) == 0 && streq(line
, ""));
705 static void test_read_line(void) {
706 _cleanup_fclose_
FILE *f
= NULL
;
708 f
= fmemopen((void*) buffer
, sizeof(buffer
), "re");
711 test_read_line_one_file(f
);
714 static void test_read_line2(void) {
715 char name
[] = "/tmp/test-fileio.XXXXXX";
717 _cleanup_fclose_
FILE *f
= NULL
;
719 fd
= mkostemp_safe(name
);
721 assert_se((size_t) write(fd
, buffer
, sizeof(buffer
)) == sizeof(buffer
));
723 assert_se(lseek(fd
, 0, SEEK_SET
) == 0);
724 assert_se(f
= fdopen(fd
, "r"));
726 test_read_line_one_file(f
);
729 static void test_read_line3(void) {
730 _cleanup_fclose_
FILE *f
= NULL
;
731 _cleanup_free_
char *line
= NULL
;
734 f
= fopen("/proc/cmdline", "re");
735 if (!f
&& IN_SET(errno
, ENOENT
, EPERM
))
739 r
= read_line(f
, LINE_MAX
, &line
);
740 assert_se((size_t) r
== strlen(line
) + 1);
741 assert_se(read_line(f
, LINE_MAX
, NULL
) == 0);
744 int main(int argc
, char *argv
[]) {
745 log_set_max_level(LOG_DEBUG
);
746 log_parse_environment();
749 test_parse_env_file();
750 test_parse_multiline_env_file();
751 test_merge_env_file();
752 test_merge_env_file_invalid();
753 test_executable_is_script();
756 test_write_string_stream();
757 test_write_string_file();
758 test_write_string_file_no_create();
759 test_write_string_file_verify();
760 test_load_env_file_pairs();
761 test_search_and_fopen();
762 test_search_and_fopen_nulstr();
763 test_writing_tmpfile();