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";
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"
235 r
= merge_env_file(&a
, NULL
, t
);
240 log_info("Got: <%s>", *i
);
242 assert_se(streq(a
[0], "one=2"));
243 assert_se(streq(a
[1], "twelve=12"));
244 assert_se(streq(a
[2], "twentyone=21"));
245 assert_se(streq(a
[3], "twentytwo=22"));
246 assert_se(streq(a
[4], "xxx=0x222"));
247 assert_se(streq(a
[5], "xxx_minus_three= - 3"));
248 assert_se(a
[6] == NULL
);
250 r
= merge_env_file(&a
, NULL
, t
);
255 log_info("Got2: <%s>", *i
);
257 assert_se(streq(a
[0], "one=2"));
258 assert_se(streq(a
[1], "twelve=12"));
259 assert_se(streq(a
[2], "twentyone=21"));
260 assert_se(streq(a
[3], "twentytwo=22"));
261 assert_se(streq(a
[4], "xxx=0x222"));
262 assert_se(streq(a
[5], "xxx_minus_three=0x222 - 3"));
263 assert_se(a
[6] == NULL
);
266 static void test_merge_env_file_invalid(void) {
267 char t
[] = "/tmp/test-fileio-XXXXXX";
270 _cleanup_strv_free_
char **a
= NULL
;
273 fd
= mkostemp_safe(t
);
276 log_info("/* %s (%s) */", __func__
, t
);
281 r
= write_string_stream(f
,
290 ";comment2=comment2\n"
292 "\n\n" /* empty line */
296 r
= merge_env_file(&a
, NULL
, t
);
300 log_info("Got: <%s>", *i
);
302 assert_se(strv_isempty(a
));
305 static void test_executable_is_script(void) {
306 char t
[] = "/tmp/test-executable-XXXXXX";
311 fd
= mkostemp_safe(t
);
317 fputs("#! /bin/script -a -b \ngoo goo", f
);
320 r
= executable_is_script(t
, &command
);
322 assert_se(streq(command
, "/bin/script"));
325 r
= executable_is_script("/bin/sh", &command
);
328 r
= executable_is_script("/usr/bin/yum", &command
);
329 assert_se(r
> 0 || r
== -ENOENT
);
331 assert_se(startswith(command
, "/"));
339 static void test_status_field(void) {
340 _cleanup_free_
char *t
= NULL
, *p
= NULL
, *s
= NULL
, *z
= NULL
;
341 unsigned long long total
= 0, buffers
= 0;
344 assert_se(get_proc_field("/proc/self/status", "Threads", WHITESPACE
, &t
) == 0);
346 assert_se(streq(t
, "1"));
348 r
= get_proc_field("/proc/meminfo", "MemTotal", WHITESPACE
, &p
);
352 assert_se(safe_atollu(p
, &total
) == 0);
355 r
= get_proc_field("/proc/meminfo", "Buffers", WHITESPACE
, &s
);
359 assert_se(safe_atollu(s
, &buffers
) == 0);
363 assert_se(buffers
< total
);
365 /* Seccomp should be a good test for field full of zeros. */
366 r
= get_proc_field("/proc/meminfo", "Seccomp", WHITESPACE
, &z
);
370 assert_se(safe_atollu(z
, &buffers
) == 0);
374 static void test_capeff(void) {
377 for (pid
= 0; pid
< 2; pid
++) {
378 _cleanup_free_
char *capeff
= NULL
;
381 r
= get_process_capeff(0, &capeff
);
382 log_info("capeff: '%s' (r=%d)", capeff
, r
);
384 if (r
== -ENOENT
|| r
== -EPERM
)
389 p
= capeff
[strspn(capeff
, HEXDIGITS
)];
390 assert_se(!p
|| isspace(p
));
394 static void test_write_string_stream(void) {
395 char fn
[] = "/tmp/test-write_string_stream-XXXXXX";
396 _cleanup_fclose_
FILE *f
= NULL
;
400 fd
= mkostemp_safe(fn
);
405 assert_se(write_string_stream(f
, "boohoo", true) < 0);
407 f
= freopen(fn
, "r+", f
);
410 assert_se(write_string_stream(f
, "boohoo", true) == 0);
413 assert_se(fgets(buf
, sizeof(buf
), f
));
414 assert_se(streq(buf
, "boohoo\n"));
416 f
= freopen(fn
, "w+", f
);
419 assert_se(write_string_stream(f
, "boohoo", false) == 0);
422 assert_se(fgets(buf
, sizeof(buf
), f
));
424 assert_se(streq(buf
, "boohoo"));
429 static void test_write_string_file(void) {
430 char fn
[] = "/tmp/test-write_string_file-XXXXXX";
432 _cleanup_close_
int fd
;
434 fd
= mkostemp_safe(fn
);
437 assert_se(write_string_file(fn
, "boohoo", WRITE_STRING_FILE_CREATE
) == 0);
439 assert_se(read(fd
, buf
, sizeof(buf
)) == 7);
440 assert_se(streq(buf
, "boohoo\n"));
445 static void test_write_string_file_no_create(void) {
446 char fn
[] = "/tmp/test-write_string_file_no_create-XXXXXX";
447 _cleanup_close_
int fd
;
450 fd
= mkostemp_safe(fn
);
453 assert_se(write_string_file("/a/file/which/does/not/exists/i/guess", "boohoo", 0) < 0);
454 assert_se(write_string_file(fn
, "boohoo", 0) == 0);
456 assert_se(read(fd
, buf
, sizeof(buf
)) == strlen("boohoo\n"));
457 assert_se(streq(buf
, "boohoo\n"));
462 static void test_write_string_file_verify(void) {
463 _cleanup_free_
char *buf
= NULL
, *buf2
= NULL
;
466 assert_se(read_one_line_file("/proc/cmdline", &buf
) >= 0);
467 assert_se((buf2
= strjoin(buf
, "\n")));
469 r
= write_string_file("/proc/cmdline", buf
, 0);
470 assert_se(r
== -EACCES
|| r
== -EIO
);
471 r
= write_string_file("/proc/cmdline", buf2
, 0);
472 assert_se(r
== -EACCES
|| r
== -EIO
);
474 assert_se(write_string_file("/proc/cmdline", buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
) == 0);
475 assert_se(write_string_file("/proc/cmdline", buf2
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
) == 0);
477 r
= write_string_file("/proc/cmdline", buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
|WRITE_STRING_FILE_AVOID_NEWLINE
);
478 assert_se(r
== -EACCES
|| r
== -EIO
);
479 assert_se(write_string_file("/proc/cmdline", buf2
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
|WRITE_STRING_FILE_AVOID_NEWLINE
) == 0);
482 static void test_load_env_file_pairs(void) {
483 char fn
[] = "/tmp/test-load_env_file_pairs-XXXXXX";
486 _cleanup_fclose_
FILE *f
= NULL
;
487 _cleanup_strv_free_
char **l
= NULL
;
490 fd
= mkostemp_safe(fn
);
493 r
= write_string_file(fn
,
494 "NAME=\"Arch Linux\"\n"
496 "PRETTY_NAME=\"Arch Linux\"\n"
497 "ANSI_COLOR=\"0;36\"\n"
498 "HOME_URL=\"https://www.archlinux.org/\"\n"
499 "SUPPORT_URL=\"https://bbs.archlinux.org/\"\n"
500 "BUG_REPORT_URL=\"https://bugs.archlinux.org/\"\n",
501 WRITE_STRING_FILE_CREATE
);
507 r
= load_env_file_pairs(f
, fn
, NULL
, &l
);
510 assert_se(strv_length(l
) == 14);
511 STRV_FOREACH_PAIR(k
, v
, l
) {
512 assert_se(STR_IN_SET(*k
, "NAME", "ID", "PRETTY_NAME", "ANSI_COLOR", "HOME_URL", "SUPPORT_URL", "BUG_REPORT_URL"));
513 printf("%s=%s\n", *k
, *v
);
514 if (streq(*k
, "NAME")) assert_se(streq(*v
, "Arch Linux"));
515 if (streq(*k
, "ID")) assert_se(streq(*v
, "arch"));
516 if (streq(*k
, "PRETTY_NAME")) assert_se(streq(*v
, "Arch Linux"));
517 if (streq(*k
, "ANSI_COLOR")) assert_se(streq(*v
, "0;36"));
518 if (streq(*k
, "HOME_URL")) assert_se(streq(*v
, "https://www.archlinux.org/"));
519 if (streq(*k
, "SUPPORT_URL")) assert_se(streq(*v
, "https://bbs.archlinux.org/"));
520 if (streq(*k
, "BUG_REPORT_URL")) assert_se(streq(*v
, "https://bugs.archlinux.org/"));
526 static void test_search_and_fopen(void) {
527 const char *dirs
[] = {"/tmp/foo/bar", "/tmp", NULL
};
528 char name
[] = "/tmp/test-search_and_fopen.XXXXXX";
533 fd
= mkostemp_safe(name
);
537 r
= search_and_fopen(basename(name
), "r", NULL
, dirs
, &f
);
541 r
= search_and_fopen(name
, "r", NULL
, dirs
, &f
);
545 r
= search_and_fopen(basename(name
), "r", "/", dirs
, &f
);
549 r
= search_and_fopen("/a/file/which/does/not/exist/i/guess", "r", NULL
, dirs
, &f
);
551 r
= search_and_fopen("afilewhichdoesnotexistiguess", "r", NULL
, dirs
, &f
);
557 r
= search_and_fopen(basename(name
), "r", NULL
, dirs
, &f
);
562 static void test_search_and_fopen_nulstr(void) {
563 const char dirs
[] = "/tmp/foo/bar\0/tmp\0";
564 char name
[] = "/tmp/test-search_and_fopen.XXXXXX";
569 fd
= mkostemp_safe(name
);
573 r
= search_and_fopen_nulstr(basename(name
), "r", NULL
, dirs
, &f
);
577 r
= search_and_fopen_nulstr(name
, "r", NULL
, dirs
, &f
);
581 r
= search_and_fopen_nulstr("/a/file/which/does/not/exist/i/guess", "r", NULL
, dirs
, &f
);
583 r
= search_and_fopen_nulstr("afilewhichdoesnotexistiguess", "r", NULL
, dirs
, &f
);
589 r
= search_and_fopen_nulstr(basename(name
), "r", NULL
, dirs
, &f
);
593 static void test_writing_tmpfile(void) {
594 char name
[] = "/tmp/test-systemd_writing_tmpfile.XXXXXX";
595 _cleanup_free_
char *contents
= NULL
;
600 IOVEC_SET_STRING(iov
[0], "abc\n");
601 IOVEC_SET_STRING(iov
[1], ALPHANUMERICAL
"\n");
602 IOVEC_SET_STRING(iov
[2], "");
604 fd
= mkostemp_safe(name
);
605 printf("tmpfile: %s", name
);
607 r
= writev(fd
, iov
, 3);
610 r
= read_full_file(name
, &contents
, &size
);
612 printf("contents: %s", contents
);
613 assert_se(streq(contents
, "abc\n" ALPHANUMERICAL
"\n"));
618 static void test_tempfn(void) {
619 char *ret
= NULL
, *p
;
621 assert_se(tempfn_xxxxxx("/foo/bar/waldo", NULL
, &ret
) >= 0);
622 assert_se(streq_ptr(ret
, "/foo/bar/.#waldoXXXXXX"));
625 assert_se(tempfn_xxxxxx("/foo/bar/waldo", "[miau]", &ret
) >= 0);
626 assert_se(streq_ptr(ret
, "/foo/bar/.#[miau]waldoXXXXXX"));
629 assert_se(tempfn_random("/foo/bar/waldo", NULL
, &ret
) >= 0);
630 assert_se(p
= startswith(ret
, "/foo/bar/.#waldo"));
631 assert_se(strlen(p
) == 16);
632 assert_se(in_charset(p
, "0123456789abcdef"));
635 assert_se(tempfn_random("/foo/bar/waldo", "[wuff]", &ret
) >= 0);
636 assert_se(p
= startswith(ret
, "/foo/bar/.#[wuff]waldo"));
637 assert_se(strlen(p
) == 16);
638 assert_se(in_charset(p
, "0123456789abcdef"));
641 assert_se(tempfn_random_child("/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_child("/foo/bar/waldo", "[kikiriki]", &ret
) >= 0);
648 assert_se(p
= startswith(ret
, "/foo/bar/waldo/.#[kikiriki]"));
649 assert_se(strlen(p
) == 16);
650 assert_se(in_charset(p
, "0123456789abcdef"));
654 int main(int argc
, char *argv
[]) {
655 log_set_max_level(LOG_DEBUG
);
656 log_parse_environment();
659 test_parse_env_file();
660 test_parse_multiline_env_file();
661 test_merge_env_file();
662 test_merge_env_file_invalid();
663 test_executable_is_script();
666 test_write_string_stream();
667 test_write_string_file();
668 test_write_string_file_no_create();
669 test_write_string_file_verify();
670 test_load_env_file_pairs();
671 test_search_and_fopen();
672 test_search_and_fopen_nulstr();
673 test_writing_tmpfile();