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", false);
232 r
= merge_env_file(&a
, NULL
, t
);
237 log_info("Got: <%s>", *i
);
239 assert_se(streq(a
[0], "one=2"));
240 assert_se(streq(a
[1], "twelve=12"));
241 assert_se(streq(a
[2], "twentyone=21"));
242 assert_se(streq(a
[3], "twentytwo=22"));
243 assert_se(a
[4] == NULL
);
246 r
= merge_env_file(&a
, NULL
, t
);
251 log_info("Got2: <%s>", *i
);
253 assert_se(streq(a
[0], "one=2"));
254 assert_se(streq(a
[1], "twelve=12"));
255 assert_se(streq(a
[2], "twentyone=21"));
256 assert_se(streq(a
[3], "twentytwo=22"));
257 assert_se(a
[4] == NULL
);
260 static void test_executable_is_script(void) {
261 char t
[] = "/tmp/test-executable-XXXXXX";
266 fd
= mkostemp_safe(t
);
272 fputs("#! /bin/script -a -b \ngoo goo", f
);
275 r
= executable_is_script(t
, &command
);
277 assert_se(streq(command
, "/bin/script"));
280 r
= executable_is_script("/bin/sh", &command
);
283 r
= executable_is_script("/usr/bin/yum", &command
);
284 assert_se(r
> 0 || r
== -ENOENT
);
286 assert_se(startswith(command
, "/"));
294 static void test_status_field(void) {
295 _cleanup_free_
char *t
= NULL
, *p
= NULL
, *s
= NULL
, *z
= NULL
;
296 unsigned long long total
= 0, buffers
= 0;
299 assert_se(get_proc_field("/proc/self/status", "Threads", WHITESPACE
, &t
) == 0);
301 assert_se(streq(t
, "1"));
303 r
= get_proc_field("/proc/meminfo", "MemTotal", WHITESPACE
, &p
);
307 assert_se(safe_atollu(p
, &total
) == 0);
310 r
= get_proc_field("/proc/meminfo", "Buffers", WHITESPACE
, &s
);
314 assert_se(safe_atollu(s
, &buffers
) == 0);
318 assert_se(buffers
< total
);
320 /* Seccomp should be a good test for field full of zeros. */
321 r
= get_proc_field("/proc/meminfo", "Seccomp", WHITESPACE
, &z
);
325 assert_se(safe_atollu(z
, &buffers
) == 0);
329 static void test_capeff(void) {
332 for (pid
= 0; pid
< 2; pid
++) {
333 _cleanup_free_
char *capeff
= NULL
;
336 r
= get_process_capeff(0, &capeff
);
337 log_info("capeff: '%s' (r=%d)", capeff
, r
);
339 if (r
== -ENOENT
|| r
== -EPERM
)
344 p
= capeff
[strspn(capeff
, HEXDIGITS
)];
345 assert_se(!p
|| isspace(p
));
349 static void test_write_string_stream(void) {
350 char fn
[] = "/tmp/test-write_string_stream-XXXXXX";
351 _cleanup_fclose_
FILE *f
= NULL
;
355 fd
= mkostemp_safe(fn
);
360 assert_se(write_string_stream(f
, "boohoo", true) < 0);
362 f
= freopen(fn
, "r+", f
);
365 assert_se(write_string_stream(f
, "boohoo", true) == 0);
368 assert_se(fgets(buf
, sizeof(buf
), f
));
369 assert_se(streq(buf
, "boohoo\n"));
371 f
= freopen(fn
, "w+", f
);
374 assert_se(write_string_stream(f
, "boohoo", false) == 0);
377 assert_se(fgets(buf
, sizeof(buf
), f
));
379 assert_se(streq(buf
, "boohoo"));
384 static void test_write_string_file(void) {
385 char fn
[] = "/tmp/test-write_string_file-XXXXXX";
387 _cleanup_close_
int fd
;
389 fd
= mkostemp_safe(fn
);
392 assert_se(write_string_file(fn
, "boohoo", WRITE_STRING_FILE_CREATE
) == 0);
394 assert_se(read(fd
, buf
, sizeof(buf
)) == 7);
395 assert_se(streq(buf
, "boohoo\n"));
400 static void test_write_string_file_no_create(void) {
401 char fn
[] = "/tmp/test-write_string_file_no_create-XXXXXX";
402 _cleanup_close_
int fd
;
405 fd
= mkostemp_safe(fn
);
408 assert_se(write_string_file("/a/file/which/does/not/exists/i/guess", "boohoo", 0) < 0);
409 assert_se(write_string_file(fn
, "boohoo", 0) == 0);
411 assert_se(read(fd
, buf
, sizeof(buf
)) == strlen("boohoo\n"));
412 assert_se(streq(buf
, "boohoo\n"));
417 static void test_write_string_file_verify(void) {
418 _cleanup_free_
char *buf
= NULL
, *buf2
= NULL
;
421 assert_se(read_one_line_file("/proc/cmdline", &buf
) >= 0);
422 assert_se((buf2
= strjoin(buf
, "\n")));
424 r
= write_string_file("/proc/cmdline", buf
, 0);
425 assert_se(r
== -EACCES
|| r
== -EIO
);
426 r
= write_string_file("/proc/cmdline", buf2
, 0);
427 assert_se(r
== -EACCES
|| r
== -EIO
);
429 assert_se(write_string_file("/proc/cmdline", buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
) == 0);
430 assert_se(write_string_file("/proc/cmdline", buf2
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
) == 0);
432 r
= write_string_file("/proc/cmdline", buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
|WRITE_STRING_FILE_AVOID_NEWLINE
);
433 assert_se(r
== -EACCES
|| r
== -EIO
);
434 assert_se(write_string_file("/proc/cmdline", buf2
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
|WRITE_STRING_FILE_AVOID_NEWLINE
) == 0);
437 static void test_load_env_file_pairs(void) {
438 char fn
[] = "/tmp/test-load_env_file_pairs-XXXXXX";
441 _cleanup_fclose_
FILE *f
= NULL
;
442 _cleanup_strv_free_
char **l
= NULL
;
445 fd
= mkostemp_safe(fn
);
448 r
= write_string_file(fn
,
449 "NAME=\"Arch Linux\"\n"
451 "PRETTY_NAME=\"Arch Linux\"\n"
452 "ANSI_COLOR=\"0;36\"\n"
453 "HOME_URL=\"https://www.archlinux.org/\"\n"
454 "SUPPORT_URL=\"https://bbs.archlinux.org/\"\n"
455 "BUG_REPORT_URL=\"https://bugs.archlinux.org/\"\n",
456 WRITE_STRING_FILE_CREATE
);
462 r
= load_env_file_pairs(f
, fn
, NULL
, &l
);
465 assert_se(strv_length(l
) == 14);
466 STRV_FOREACH_PAIR(k
, v
, l
) {
467 assert_se(STR_IN_SET(*k
, "NAME", "ID", "PRETTY_NAME", "ANSI_COLOR", "HOME_URL", "SUPPORT_URL", "BUG_REPORT_URL"));
468 printf("%s=%s\n", *k
, *v
);
469 if (streq(*k
, "NAME")) assert_se(streq(*v
, "Arch Linux"));
470 if (streq(*k
, "ID")) assert_se(streq(*v
, "arch"));
471 if (streq(*k
, "PRETTY_NAME")) assert_se(streq(*v
, "Arch Linux"));
472 if (streq(*k
, "ANSI_COLOR")) assert_se(streq(*v
, "0;36"));
473 if (streq(*k
, "HOME_URL")) assert_se(streq(*v
, "https://www.archlinux.org/"));
474 if (streq(*k
, "SUPPORT_URL")) assert_se(streq(*v
, "https://bbs.archlinux.org/"));
475 if (streq(*k
, "BUG_REPORT_URL")) assert_se(streq(*v
, "https://bugs.archlinux.org/"));
481 static void test_search_and_fopen(void) {
482 const char *dirs
[] = {"/tmp/foo/bar", "/tmp", NULL
};
483 char name
[] = "/tmp/test-search_and_fopen.XXXXXX";
488 fd
= mkostemp_safe(name
);
492 r
= search_and_fopen(basename(name
), "r", NULL
, dirs
, &f
);
496 r
= search_and_fopen(name
, "r", NULL
, dirs
, &f
);
500 r
= search_and_fopen(basename(name
), "r", "/", dirs
, &f
);
504 r
= search_and_fopen("/a/file/which/does/not/exist/i/guess", "r", NULL
, dirs
, &f
);
506 r
= search_and_fopen("afilewhichdoesnotexistiguess", "r", NULL
, dirs
, &f
);
512 r
= search_and_fopen(basename(name
), "r", NULL
, dirs
, &f
);
517 static void test_search_and_fopen_nulstr(void) {
518 const char dirs
[] = "/tmp/foo/bar\0/tmp\0";
519 char name
[] = "/tmp/test-search_and_fopen.XXXXXX";
524 fd
= mkostemp_safe(name
);
528 r
= search_and_fopen_nulstr(basename(name
), "r", NULL
, dirs
, &f
);
532 r
= search_and_fopen_nulstr(name
, "r", NULL
, dirs
, &f
);
536 r
= search_and_fopen_nulstr("/a/file/which/does/not/exist/i/guess", "r", NULL
, dirs
, &f
);
538 r
= search_and_fopen_nulstr("afilewhichdoesnotexistiguess", "r", NULL
, dirs
, &f
);
544 r
= search_and_fopen_nulstr(basename(name
), "r", NULL
, dirs
, &f
);
548 static void test_writing_tmpfile(void) {
549 char name
[] = "/tmp/test-systemd_writing_tmpfile.XXXXXX";
550 _cleanup_free_
char *contents
= NULL
;
555 IOVEC_SET_STRING(iov
[0], "abc\n");
556 IOVEC_SET_STRING(iov
[1], ALPHANUMERICAL
"\n");
557 IOVEC_SET_STRING(iov
[2], "");
559 fd
= mkostemp_safe(name
);
560 printf("tmpfile: %s", name
);
562 r
= writev(fd
, iov
, 3);
565 r
= read_full_file(name
, &contents
, &size
);
567 printf("contents: %s", contents
);
568 assert_se(streq(contents
, "abc\n" ALPHANUMERICAL
"\n"));
573 static void test_tempfn(void) {
574 char *ret
= NULL
, *p
;
576 assert_se(tempfn_xxxxxx("/foo/bar/waldo", NULL
, &ret
) >= 0);
577 assert_se(streq_ptr(ret
, "/foo/bar/.#waldoXXXXXX"));
580 assert_se(tempfn_xxxxxx("/foo/bar/waldo", "[miau]", &ret
) >= 0);
581 assert_se(streq_ptr(ret
, "/foo/bar/.#[miau]waldoXXXXXX"));
584 assert_se(tempfn_random("/foo/bar/waldo", NULL
, &ret
) >= 0);
585 assert_se(p
= startswith(ret
, "/foo/bar/.#waldo"));
586 assert_se(strlen(p
) == 16);
587 assert_se(in_charset(p
, "0123456789abcdef"));
590 assert_se(tempfn_random("/foo/bar/waldo", "[wuff]", &ret
) >= 0);
591 assert_se(p
= startswith(ret
, "/foo/bar/.#[wuff]waldo"));
592 assert_se(strlen(p
) == 16);
593 assert_se(in_charset(p
, "0123456789abcdef"));
596 assert_se(tempfn_random_child("/foo/bar/waldo", NULL
, &ret
) >= 0);
597 assert_se(p
= startswith(ret
, "/foo/bar/waldo/.#"));
598 assert_se(strlen(p
) == 16);
599 assert_se(in_charset(p
, "0123456789abcdef"));
602 assert_se(tempfn_random_child("/foo/bar/waldo", "[kikiriki]", &ret
) >= 0);
603 assert_se(p
= startswith(ret
, "/foo/bar/waldo/.#[kikiriki]"));
604 assert_se(strlen(p
) == 16);
605 assert_se(in_charset(p
, "0123456789abcdef"));
609 int main(int argc
, char *argv
[]) {
610 log_set_max_level(LOG_DEBUG
);
611 log_parse_environment();
614 test_parse_env_file();
615 test_parse_multiline_env_file();
616 test_merge_env_file();
617 test_executable_is_script();
620 test_write_string_stream();
621 test_write_string_file();
622 test_write_string_file_no_create();
623 test_write_string_file_verify();
624 test_load_env_file_pairs();
625 test_search_and_fopen();
626 test_search_and_fopen_nulstr();
627 test_writing_tmpfile();