1 /* SPDX-License-Identifier: LGPL-2.1+ */
7 #include "alloc-util.h"
15 #include "parse-util.h"
16 #include "process-util.h"
17 #include "string-util.h"
20 #include "tmpfile-util.h"
23 static void test_parse_env_file(void) {
24 _cleanup_(unlink_tempfilep
) char
25 t
[] = "/tmp/test-fileio-in-XXXXXX",
26 p
[] = "/tmp/test-fileio-out-XXXXXX";
28 _cleanup_free_
char *one
= NULL
, *two
= NULL
, *three
= NULL
, *four
= NULL
, *five
= NULL
,
29 *six
= NULL
, *seven
= NULL
, *eight
= NULL
, *nine
= NULL
, *ten
= NULL
;
30 _cleanup_strv_free_
char **a
= NULL
, **b
= NULL
;
35 assert_se(fmkostemp_safe(t
, "w", &f
) == 0);
42 "invalid line #comment\n"
45 "four = \'44\\\"44\'\n"
46 "five = \'55\\\'55\' \"FIVE\" cinco \n"
47 "six = seis sechs\\\n"
49 "seven=\"sevenval\" #nocomment\n"
50 "eight=eightval #nocomment\n"
51 "export nine=nineval\n"
59 r
= load_env_file(NULL
, t
, &a
);
63 log_info("Got: <%s>", *i
);
65 assert_se(streq_ptr(a
[0], "one=BAR"));
66 assert_se(streq_ptr(a
[1], "two=bar"));
67 assert_se(streq_ptr(a
[2], "three=333\nxxxx"));
68 assert_se(streq_ptr(a
[3], "four=44\"44"));
69 assert_se(streq_ptr(a
[4], "five=55\'55FIVEcinco"));
70 assert_se(streq_ptr(a
[5], "six=seis sechs sis"));
71 assert_se(streq_ptr(a
[6], "seven=sevenval#nocomment"));
72 assert_se(streq_ptr(a
[7], "eight=eightval #nocomment"));
73 assert_se(streq_ptr(a
[8], "export nine=nineval"));
74 assert_se(streq_ptr(a
[9], "ten="));
75 assert_se(a
[10] == NULL
);
81 log_info("Got2: <%s>", *i
);
82 assert_se(streq(*i
, a
[k
++]));
100 log_info("one=[%s]", strna(one
));
101 log_info("two=[%s]", strna(two
));
102 log_info("three=[%s]", strna(three
));
103 log_info("four=[%s]", strna(four
));
104 log_info("five=[%s]", strna(five
));
105 log_info("six=[%s]", strna(six
));
106 log_info("seven=[%s]", strna(seven
));
107 log_info("eight=[%s]", strna(eight
));
108 log_info("export nine=[%s]", strna(nine
));
109 log_info("ten=[%s]", strna(nine
));
111 assert_se(streq(one
, "BAR"));
112 assert_se(streq(two
, "bar"));
113 assert_se(streq(three
, "333\nxxxx"));
114 assert_se(streq(four
, "44\"44"));
115 assert_se(streq(five
, "55\'55FIVEcinco"));
116 assert_se(streq(six
, "seis sechs sis"));
117 assert_se(streq(seven
, "sevenval#nocomment"));
118 assert_se(streq(eight
, "eightval #nocomment"));
119 assert_se(streq(nine
, "nineval"));
120 assert_se(ten
== NULL
);
123 /* prepare a temporary file to write the environment to */
124 _cleanup_close_
int fd
= mkostemp_safe(p
);
128 r
= write_env_file(p
, a
);
131 r
= load_env_file(NULL
, p
, &b
);
135 static void test_parse_multiline_env_file(void) {
136 _cleanup_(unlink_tempfilep
) char
137 t
[] = "/tmp/test-fileio-in-XXXXXX",
138 p
[] = "/tmp/test-fileio-out-XXXXXX";
140 _cleanup_strv_free_
char **a
= NULL
, **b
= NULL
;
144 assert_se(fmkostemp_safe(t
, "w", &f
) == 0);
160 r
= load_env_file(NULL
, t
, &a
);
164 log_info("Got: <%s>", *i
);
166 assert_se(streq_ptr(a
[0], "one=BAR VAR\tGAR"));
167 assert_se(streq_ptr(a
[1], "two=bar var\tgar"));
168 assert_se(streq_ptr(a
[2], "tri=bar var \tgar "));
169 assert_se(a
[3] == NULL
);
172 _cleanup_close_
int fd
= mkostemp_safe(p
);
176 r
= write_env_file(p
, a
);
179 r
= load_env_file(NULL
, p
, &b
);
183 static void test_merge_env_file(void) {
184 _cleanup_(unlink_tempfilep
) char t
[] = "/tmp/test-fileio-XXXXXX";
185 _cleanup_fclose_
FILE *f
= NULL
;
186 _cleanup_strv_free_
char **a
= NULL
;
190 assert_se(fmkostemp_safe(t
, "w", &f
) == 0);
191 log_info("/* %s (%s) */", __func__
, t
);
193 r
= write_string_stream(f
,
196 "twentyone=2${one}\n"
198 "twentytwo=2${one}\n"
199 "xxx_minus_three=$xxx - 3\n"
200 "xxx=0x$one$one$one\n"
201 "yyy=${one:-fallback}\n"
202 "zzz=${one:+replacement}\n"
203 "zzzz=${foobar:-${nothing}}\n"
204 "zzzzz=${nothing:+${nothing}}\n"
205 , WRITE_STRING_FILE_AVOID_NEWLINE
);
208 r
= merge_env_file(&a
, NULL
, t
);
213 log_info("Got: <%s>", *i
);
215 assert_se(streq(a
[0], "one=2"));
216 assert_se(streq(a
[1], "twelve=12"));
217 assert_se(streq(a
[2], "twentyone=21"));
218 assert_se(streq(a
[3], "twentytwo=22"));
219 assert_se(streq(a
[4], "xxx=0x222"));
220 assert_se(streq(a
[5], "xxx_minus_three= - 3"));
221 assert_se(streq(a
[6], "yyy=2"));
222 assert_se(streq(a
[7], "zzz=replacement"));
223 assert_se(streq(a
[8], "zzzz="));
224 assert_se(streq(a
[9], "zzzzz="));
225 assert_se(a
[10] == NULL
);
227 r
= merge_env_file(&a
, NULL
, t
);
232 log_info("Got2: <%s>", *i
);
234 assert_se(streq(a
[0], "one=2"));
235 assert_se(streq(a
[1], "twelve=12"));
236 assert_se(streq(a
[2], "twentyone=21"));
237 assert_se(streq(a
[3], "twentytwo=22"));
238 assert_se(streq(a
[4], "xxx=0x222"));
239 assert_se(streq(a
[5], "xxx_minus_three=0x222 - 3"));
240 assert_se(streq(a
[6], "yyy=2"));
241 assert_se(streq(a
[7], "zzz=replacement"));
242 assert_se(streq(a
[8], "zzzz="));
243 assert_se(streq(a
[9], "zzzzz="));
244 assert_se(a
[10] == NULL
);
247 static void test_merge_env_file_invalid(void) {
248 _cleanup_(unlink_tempfilep
) char t
[] = "/tmp/test-fileio-XXXXXX";
249 _cleanup_fclose_
FILE *f
= NULL
;
250 _cleanup_strv_free_
char **a
= NULL
;
254 assert_se(fmkostemp_safe(t
, "w", &f
) == 0);
255 log_info("/* %s (%s) */", __func__
, t
);
257 r
= write_string_stream(f
,
266 ";comment2=comment2\n"
268 "\n\n" /* empty line */
269 , WRITE_STRING_FILE_AVOID_NEWLINE
);
272 r
= merge_env_file(&a
, NULL
, t
);
276 log_info("Got: <%s>", *i
);
278 assert_se(strv_isempty(a
));
281 static void test_executable_is_script(void) {
282 _cleanup_(unlink_tempfilep
) char t
[] = "/tmp/test-fileio-XXXXXX";
283 _cleanup_fclose_
FILE *f
= NULL
;
287 assert_se(fmkostemp_safe(t
, "w", &f
) == 0);
288 fputs("#! /bin/script -a -b \ngoo goo", f
);
291 r
= executable_is_script(t
, &command
);
293 assert_se(streq(command
, "/bin/script"));
296 r
= executable_is_script("/bin/sh", &command
);
299 r
= executable_is_script("/usr/bin/yum", &command
);
300 assert_se(r
> 0 || r
== -ENOENT
);
302 assert_se(startswith(command
, "/"));
307 static void test_status_field(void) {
308 _cleanup_free_
char *t
= NULL
, *p
= NULL
, *s
= NULL
, *z
= NULL
;
309 unsigned long long total
= 0, buffers
= 0;
312 assert_se(get_proc_field("/proc/self/status", "Threads", WHITESPACE
, &t
) == 0);
314 assert_se(streq(t
, "1"));
316 r
= get_proc_field("/proc/meminfo", "MemTotal", WHITESPACE
, &p
);
320 assert_se(safe_atollu(p
, &total
) == 0);
323 r
= get_proc_field("/proc/meminfo", "Buffers", WHITESPACE
, &s
);
327 assert_se(safe_atollu(s
, &buffers
) == 0);
331 assert_se(buffers
< total
);
333 /* Seccomp should be a good test for field full of zeros. */
334 r
= get_proc_field("/proc/meminfo", "Seccomp", WHITESPACE
, &z
);
338 assert_se(safe_atollu(z
, &buffers
) == 0);
342 static void test_capeff(void) {
345 for (pid
= 0; pid
< 2; pid
++) {
346 _cleanup_free_
char *capeff
= NULL
;
349 r
= get_process_capeff(0, &capeff
);
350 log_info("capeff: '%s' (r=%d)", capeff
, r
);
352 if (IN_SET(r
, -ENOENT
, -EPERM
))
357 p
= capeff
[strspn(capeff
, HEXDIGITS
)];
358 assert_se(!p
|| isspace(p
));
362 static void test_write_string_stream(void) {
363 _cleanup_(unlink_tempfilep
) char fn
[] = "/tmp/test-write_string_stream-XXXXXX";
364 _cleanup_fclose_
FILE *f
= NULL
;
368 fd
= mkostemp_safe(fn
);
373 assert_se(write_string_stream(f
, "boohoo", 0) < 0);
379 assert_se(write_string_stream(f
, "boohoo", 0) == 0);
382 assert_se(fgets(buf
, sizeof(buf
), f
));
383 assert_se(streq(buf
, "boohoo\n"));
389 assert_se(write_string_stream(f
, "boohoo", WRITE_STRING_FILE_AVOID_NEWLINE
) == 0);
392 assert_se(fgets(buf
, sizeof(buf
), f
));
394 assert_se(streq(buf
, "boohoo"));
397 static void test_write_string_file(void) {
398 _cleanup_(unlink_tempfilep
) char fn
[] = "/tmp/test-write_string_file-XXXXXX";
400 _cleanup_close_
int fd
;
402 fd
= mkostemp_safe(fn
);
405 assert_se(write_string_file(fn
, "boohoo", WRITE_STRING_FILE_CREATE
) == 0);
407 assert_se(read(fd
, buf
, sizeof(buf
)) == 7);
408 assert_se(streq(buf
, "boohoo\n"));
411 static void test_write_string_file_no_create(void) {
412 _cleanup_(unlink_tempfilep
) char fn
[] = "/tmp/test-write_string_file_no_create-XXXXXX";
413 _cleanup_close_
int fd
;
416 fd
= mkostemp_safe(fn
);
419 assert_se(write_string_file("/a/file/which/does/not/exists/i/guess", "boohoo", 0) < 0);
420 assert_se(write_string_file(fn
, "boohoo", 0) == 0);
422 assert_se(read(fd
, buf
, sizeof(buf
)) == STRLEN("boohoo\n"));
423 assert_se(streq(buf
, "boohoo\n"));
426 static void test_write_string_file_verify(void) {
427 _cleanup_free_
char *buf
= NULL
, *buf2
= NULL
;
430 assert_se(read_one_line_file("/proc/cmdline", &buf
) >= 0);
431 assert_se(buf2
= strjoin(buf
, "\n"));
433 r
= write_string_file("/proc/cmdline", buf
, 0);
434 assert_se(IN_SET(r
, -EACCES
, -EIO
));
435 r
= write_string_file("/proc/cmdline", buf2
, 0);
436 assert_se(IN_SET(r
, -EACCES
, -EIO
));
438 assert_se(write_string_file("/proc/cmdline", buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
) == 0);
439 assert_se(write_string_file("/proc/cmdline", buf2
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
) == 0);
441 r
= write_string_file("/proc/cmdline", buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
|WRITE_STRING_FILE_AVOID_NEWLINE
);
442 assert_se(IN_SET(r
, -EACCES
, -EIO
));
443 assert_se(write_string_file("/proc/cmdline", buf2
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
|WRITE_STRING_FILE_AVOID_NEWLINE
) == 0);
446 static void test_load_env_file_pairs(void) {
447 _cleanup_(unlink_tempfilep
) char fn
[] = "/tmp/test-load_env_file_pairs-XXXXXX";
449 _cleanup_fclose_
FILE *f
= NULL
;
450 _cleanup_strv_free_
char **l
= NULL
;
453 fd
= mkostemp_safe(fn
);
456 r
= write_string_file(fn
,
457 "NAME=\"Arch Linux\"\n"
459 "PRETTY_NAME=\"Arch Linux\"\n"
460 "ANSI_COLOR=\"0;36\"\n"
461 "HOME_URL=\"https://www.archlinux.org/\"\n"
462 "SUPPORT_URL=\"https://bbs.archlinux.org/\"\n"
463 "BUG_REPORT_URL=\"https://bugs.archlinux.org/\"\n",
464 WRITE_STRING_FILE_CREATE
);
470 r
= load_env_file_pairs(f
, fn
, &l
);
473 assert_se(strv_length(l
) == 14);
474 STRV_FOREACH_PAIR(k
, v
, l
) {
475 assert_se(STR_IN_SET(*k
, "NAME", "ID", "PRETTY_NAME", "ANSI_COLOR", "HOME_URL", "SUPPORT_URL", "BUG_REPORT_URL"));
476 printf("%s=%s\n", *k
, *v
);
477 if (streq(*k
, "NAME")) assert_se(streq(*v
, "Arch Linux"));
478 if (streq(*k
, "ID")) assert_se(streq(*v
, "arch"));
479 if (streq(*k
, "PRETTY_NAME")) assert_se(streq(*v
, "Arch Linux"));
480 if (streq(*k
, "ANSI_COLOR")) assert_se(streq(*v
, "0;36"));
481 if (streq(*k
, "HOME_URL")) assert_se(streq(*v
, "https://www.archlinux.org/"));
482 if (streq(*k
, "SUPPORT_URL")) assert_se(streq(*v
, "https://bbs.archlinux.org/"));
483 if (streq(*k
, "BUG_REPORT_URL")) assert_se(streq(*v
, "https://bugs.archlinux.org/"));
487 static void test_search_and_fopen(void) {
488 const char *dirs
[] = {"/tmp/foo/bar", "/tmp", NULL
};
490 char name
[] = "/tmp/test-search_and_fopen.XXXXXX";
494 fd
= mkostemp_safe(name
);
498 r
= search_and_fopen(basename(name
), "r", NULL
, dirs
, &f
);
502 r
= search_and_fopen(name
, "r", NULL
, dirs
, &f
);
506 r
= search_and_fopen(basename(name
), "r", "/", dirs
, &f
);
510 r
= search_and_fopen("/a/file/which/does/not/exist/i/guess", "r", NULL
, dirs
, &f
);
512 r
= search_and_fopen("afilewhichdoesnotexistiguess", "r", NULL
, dirs
, &f
);
518 r
= search_and_fopen(basename(name
), "r", NULL
, dirs
, &f
);
522 static void test_search_and_fopen_nulstr(void) {
523 const char dirs
[] = "/tmp/foo/bar\0/tmp\0";
525 _cleanup_(unlink_tempfilep
) char name
[] = "/tmp/test-search_and_fopen.XXXXXX";
529 fd
= mkostemp_safe(name
);
533 r
= search_and_fopen_nulstr(basename(name
), "r", NULL
, dirs
, &f
);
537 r
= search_and_fopen_nulstr(name
, "r", NULL
, dirs
, &f
);
541 r
= search_and_fopen_nulstr("/a/file/which/does/not/exist/i/guess", "r", NULL
, dirs
, &f
);
543 r
= search_and_fopen_nulstr("afilewhichdoesnotexistiguess", "r", NULL
, dirs
, &f
);
549 r
= search_and_fopen_nulstr(basename(name
), "r", NULL
, dirs
, &f
);
553 static void test_writing_tmpfile(void) {
554 _cleanup_(unlink_tempfilep
) char name
[] = "/tmp/test-systemd_writing_tmpfile.XXXXXX";
555 _cleanup_free_
char *contents
= NULL
;
557 _cleanup_close_
int fd
= -1;
561 iov
[0] = IOVEC_MAKE_STRING("abc\n");
562 iov
[1] = IOVEC_MAKE_STRING(ALPHANUMERICAL
"\n");
563 iov
[2] = IOVEC_MAKE_STRING("");
565 fd
= mkostemp_safe(name
);
566 printf("tmpfile: %s", name
);
568 r
= writev(fd
, iov
, 3);
571 r
= read_full_file(name
, &contents
, &size
);
573 printf("contents: %s", contents
);
574 assert_se(streq(contents
, "abc\n" ALPHANUMERICAL
"\n"));
577 static void test_tempfn(void) {
578 char *ret
= NULL
, *p
;
580 assert_se(tempfn_xxxxxx("/foo/bar/waldo", NULL
, &ret
) >= 0);
581 assert_se(streq_ptr(ret
, "/foo/bar/.#waldoXXXXXX"));
584 assert_se(tempfn_xxxxxx("/foo/bar/waldo", "[miau]", &ret
) >= 0);
585 assert_se(streq_ptr(ret
, "/foo/bar/.#[miau]waldoXXXXXX"));
588 assert_se(tempfn_random("/foo/bar/waldo", NULL
, &ret
) >= 0);
589 assert_se(p
= startswith(ret
, "/foo/bar/.#waldo"));
590 assert_se(strlen(p
) == 16);
591 assert_se(in_charset(p
, "0123456789abcdef"));
594 assert_se(tempfn_random("/foo/bar/waldo", "[wuff]", &ret
) >= 0);
595 assert_se(p
= startswith(ret
, "/foo/bar/.#[wuff]waldo"));
596 assert_se(strlen(p
) == 16);
597 assert_se(in_charset(p
, "0123456789abcdef"));
600 assert_se(tempfn_random_child("/foo/bar/waldo", NULL
, &ret
) >= 0);
601 assert_se(p
= startswith(ret
, "/foo/bar/waldo/.#"));
602 assert_se(strlen(p
) == 16);
603 assert_se(in_charset(p
, "0123456789abcdef"));
606 assert_se(tempfn_random_child("/foo/bar/waldo", "[kikiriki]", &ret
) >= 0);
607 assert_se(p
= startswith(ret
, "/foo/bar/waldo/.#[kikiriki]"));
608 assert_se(strlen(p
) == 16);
609 assert_se(in_charset(p
, "0123456789abcdef"));
613 static const char buffer
[] =
615 "With newlines, and a NUL byte\0"
619 "and a very long line that is supposed to be truncated, because it is so long\n";
621 static void test_read_line_one_file(FILE *f
) {
622 _cleanup_free_
char *line
= NULL
;
624 assert_se(read_line(f
, (size_t) -1, &line
) == 15 && streq(line
, "Some test data"));
627 assert_se(read_line(f
, 1024, &line
) == 30 && streq(line
, "With newlines, and a NUL byte"));
630 assert_se(read_line(f
, 1024, &line
) == 1 && streq(line
, ""));
633 assert_se(read_line(f
, 1024, &line
) == 14 && streq(line
, "an empty line"));
636 assert_se(read_line(f
, (size_t) -1, NULL
) == 16);
638 assert_se(read_line(f
, 16, &line
) == -ENOBUFS
);
641 /* read_line() stopped when it hit the limit, that means when we continue reading we'll read at the first
642 * character after the previous limit. Let's make use of tha to continue our test. */
643 assert_se(read_line(f
, 1024, &line
) == 61 && streq(line
, "line that is supposed to be truncated, because it is so long"));
646 assert_se(read_line(f
, 1024, &line
) == 1 && streq(line
, ""));
649 assert_se(read_line(f
, 1024, &line
) == 0 && streq(line
, ""));
652 static void test_read_line(void) {
653 _cleanup_fclose_
FILE *f
= NULL
;
655 f
= fmemopen((void*) buffer
, sizeof(buffer
), "re");
658 test_read_line_one_file(f
);
661 static void test_read_line2(void) {
662 _cleanup_(unlink_tempfilep
) char name
[] = "/tmp/test-fileio.XXXXXX";
664 _cleanup_fclose_
FILE *f
= NULL
;
666 fd
= mkostemp_safe(name
);
668 assert_se((size_t) write(fd
, buffer
, sizeof(buffer
)) == sizeof(buffer
));
670 assert_se(lseek(fd
, 0, SEEK_SET
) == 0);
671 assert_se(f
= fdopen(fd
, "r"));
673 test_read_line_one_file(f
);
676 static void test_read_line3(void) {
677 _cleanup_fclose_
FILE *f
= NULL
;
678 _cleanup_free_
char *line
= NULL
;
681 f
= fopen("/proc/cmdline", "re");
682 if (!f
&& IN_SET(errno
, ENOENT
, EPERM
))
686 r
= read_line(f
, LINE_MAX
, &line
);
687 assert_se((size_t) r
== strlen(line
) + 1);
688 assert_se(read_line(f
, LINE_MAX
, NULL
) == 0);
691 int main(int argc
, char *argv
[]) {
692 test_setup_logging(LOG_DEBUG
);
694 test_parse_env_file();
695 test_parse_multiline_env_file();
696 test_merge_env_file();
697 test_merge_env_file_invalid();
698 test_executable_is_script();
701 test_write_string_stream();
702 test_write_string_file();
703 test_write_string_file_no_create();
704 test_write_string_file_verify();
705 test_load_env_file_pairs();
706 test_search_and_fopen();
707 test_search_and_fopen_nulstr();
708 test_writing_tmpfile();