1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
8 #include "alloc-util.h"
12 #include "errno-util.h"
17 #include "memfd-util.h"
18 #include "parse-util.h"
19 #include "path-util.h"
20 #include "process-util.h"
21 #include "random-util.h"
23 #include "socket-util.h"
24 #include "string-util.h"
27 #include "tmpfile-util.h"
29 TEST(parse_env_file
) {
30 _cleanup_(unlink_tempfilep
) char
31 t
[] = "/tmp/test-fileio-in-XXXXXX",
32 p
[] = "/tmp/test-fileio-out-XXXXXX";
34 _cleanup_free_
char *one
= NULL
, *two
= NULL
, *three
= NULL
, *four
= NULL
, *five
= NULL
,
35 *six
= NULL
, *seven
= NULL
, *eight
= NULL
, *nine
= NULL
, *ten
= NULL
,
36 *eleven
= NULL
, *twelve
= NULL
, *thirteen
= NULL
;
37 _cleanup_strv_free_
char **a
= NULL
, **b
= NULL
;
41 assert_se(fmkostemp_safe(t
, "w", &f
) == 0);
48 "invalid line #comment\n"
51 "four = \'44\\\"44\'\n"
52 "five = \"55\\\"55\" \"FIVE\" cinco \n"
53 "six = seis sechs\\\n"
55 "seven=\"sevenval\" #nocomment\n"
56 "eight=eightval #nocomment\n"
57 "export nine=nineval\n"
62 "twelve=\"\\value\"\n"
63 "thirteen='\\value'", f
);
68 r
= load_env_file(NULL
, t
, &a
);
72 log_info("Got: <%s>", *i
);
74 assert_se(streq_ptr(a
[0], "one=BAR"));
75 assert_se(streq_ptr(a
[1], "two=bar"));
76 assert_se(streq_ptr(a
[2], "three=333\nxxxx"));
77 assert_se(streq_ptr(a
[3], "four=44\\\"44"));
78 assert_se(streq_ptr(a
[4], "five=55\"55FIVEcinco"));
79 assert_se(streq_ptr(a
[5], "six=seis sechs sis"));
80 assert_se(streq_ptr(a
[6], "seven=sevenval#nocomment"));
81 assert_se(streq_ptr(a
[7], "eight=eightval #nocomment"));
82 assert_se(streq_ptr(a
[8], "export nine=nineval"));
83 assert_se(streq_ptr(a
[9], "ten="));
84 assert_se(streq_ptr(a
[10], "eleven=value"));
85 assert_se(streq_ptr(a
[11], "twelve=\\value"));
86 assert_se(streq_ptr(a
[12], "thirteen=\\value"));
87 assert_se(a
[13] == NULL
);
93 log_info("Got2: <%s>", *i
);
94 assert_se(streq(*i
, a
[k
++]));
107 "export nine", &nine
,
111 "thirteen", &thirteen
);
114 log_info("one=[%s]", strna(one
));
115 log_info("two=[%s]", strna(two
));
116 log_info("three=[%s]", strna(three
));
117 log_info("four=[%s]", strna(four
));
118 log_info("five=[%s]", strna(five
));
119 log_info("six=[%s]", strna(six
));
120 log_info("seven=[%s]", strna(seven
));
121 log_info("eight=[%s]", strna(eight
));
122 log_info("export nine=[%s]", strna(nine
));
123 log_info("ten=[%s]", strna(nine
));
124 log_info("eleven=[%s]", strna(eleven
));
125 log_info("twelve=[%s]", strna(twelve
));
126 log_info("thirteen=[%s]", strna(thirteen
));
128 assert_se(streq(one
, "BAR"));
129 assert_se(streq(two
, "bar"));
130 assert_se(streq(three
, "333\nxxxx"));
131 assert_se(streq(four
, "44\\\"44"));
132 assert_se(streq(five
, "55\"55FIVEcinco"));
133 assert_se(streq(six
, "seis sechs sis"));
134 assert_se(streq(seven
, "sevenval#nocomment"));
135 assert_se(streq(eight
, "eightval #nocomment"));
136 assert_se(streq(nine
, "nineval"));
137 assert_se(ten
== NULL
);
138 assert_se(streq(eleven
, "value"));
139 assert_se(streq(twelve
, "\\value"));
140 assert_se(streq(thirteen
, "\\value"));
143 /* prepare a temporary file to write the environment to */
144 _cleanup_close_
int fd
= mkostemp_safe(p
);
148 r
= write_env_file(p
, a
);
151 r
= load_env_file(NULL
, p
, &b
);
155 static void test_one_shell_var(const char *file
, const char *variable
, const char *value
) {
156 _cleanup_free_
char *cmd
= NULL
, *from_shell
= NULL
;
157 _cleanup_pclose_
FILE *f
= NULL
;
160 assert_se(cmd
= strjoin(". ", file
, " && /bin/echo -n \"$", variable
, "\""));
161 assert_se(f
= popen(cmd
, "re"));
162 assert_se(read_full_stream(f
, &from_shell
, &sz
) >= 0);
163 assert_se(sz
== strlen(value
));
164 assert_se(streq(from_shell
, value
));
167 TEST(parse_multiline_env_file
) {
168 _cleanup_(unlink_tempfilep
) char
169 t
[] = "/tmp/test-fileio-in-XXXXXX",
170 p
[] = "/tmp/test-fileio-out-XXXXXX";
172 _cleanup_strv_free_
char **a
= NULL
, **b
= NULL
;
175 assert_se(fmkostemp_safe(t
, "w", &f
) == 0);
177 "\\ \\ \\ \\ VAR\\\n"
188 assert_se(fflush_and_check(f
) >= 0);
191 test_one_shell_var(t
, "one", "BAR VAR\tGAR");
192 test_one_shell_var(t
, "two", "bar var\tgar");
193 test_one_shell_var(t
, "tri", "bar var \tgar ");
195 r
= load_env_file(NULL
, t
, &a
);
199 log_info("Got: <%s>", *i
);
201 assert_se(streq_ptr(a
[0], "one=BAR VAR\tGAR"));
202 assert_se(streq_ptr(a
[1], "two=bar var\tgar"));
203 assert_se(streq_ptr(a
[2], "tri=bar var \tgar "));
204 assert_se(a
[3] == NULL
);
207 _cleanup_close_
int fd
= mkostemp_safe(p
);
211 r
= write_env_file(p
, a
);
214 r
= load_env_file(NULL
, p
, &b
);
218 TEST(merge_env_file
) {
219 _cleanup_(unlink_tempfilep
) char t
[] = "/tmp/test-fileio-XXXXXX";
220 _cleanup_fclose_
FILE *f
= NULL
;
221 _cleanup_strv_free_
char **a
= NULL
;
224 assert_se(fmkostemp_safe(t
, "w", &f
) == 0);
225 log_info("/* %s (%s) */", __func__
, t
);
227 r
= write_string_stream(f
,
230 "twentyone=2${one}\n"
232 "twentytwo=2${one}\n"
233 "xxx_minus_three=$xxx - 3\n"
234 "xxx=0x$one$one$one\n"
235 "yyy=${one:-fallback}\n"
236 "zzz=${one:+replacement}\n"
237 "zzzz=${foobar:-${nothing}}\n"
238 "zzzzz=${nothing:+${nothing}}\n"
239 , WRITE_STRING_FILE_AVOID_NEWLINE
);
242 r
= merge_env_file(&a
, NULL
, t
);
247 log_info("Got: <%s>", *i
);
249 assert_se(streq(a
[0], "one=2"));
250 assert_se(streq(a
[1], "twelve=12"));
251 assert_se(streq(a
[2], "twentyone=21"));
252 assert_se(streq(a
[3], "twentytwo=22"));
253 assert_se(streq(a
[4], "xxx=0x222"));
254 assert_se(streq(a
[5], "xxx_minus_three= - 3"));
255 assert_se(streq(a
[6], "yyy=2"));
256 assert_se(streq(a
[7], "zzz=replacement"));
257 assert_se(streq(a
[8], "zzzz="));
258 assert_se(streq(a
[9], "zzzzz="));
259 assert_se(a
[10] == NULL
);
261 r
= merge_env_file(&a
, NULL
, t
);
266 log_info("Got2: <%s>", *i
);
268 assert_se(streq(a
[0], "one=2"));
269 assert_se(streq(a
[1], "twelve=12"));
270 assert_se(streq(a
[2], "twentyone=21"));
271 assert_se(streq(a
[3], "twentytwo=22"));
272 assert_se(streq(a
[4], "xxx=0x222"));
273 assert_se(streq(a
[5], "xxx_minus_three=0x222 - 3"));
274 assert_se(streq(a
[6], "yyy=2"));
275 assert_se(streq(a
[7], "zzz=replacement"));
276 assert_se(streq(a
[8], "zzzz="));
277 assert_se(streq(a
[9], "zzzzz="));
278 assert_se(a
[10] == NULL
);
281 TEST(merge_env_file_invalid
) {
282 _cleanup_(unlink_tempfilep
) char t
[] = "/tmp/test-fileio-XXXXXX";
283 _cleanup_fclose_
FILE *f
= NULL
;
284 _cleanup_strv_free_
char **a
= NULL
;
287 assert_se(fmkostemp_safe(t
, "w", &f
) == 0);
288 log_info("/* %s (%s) */", __func__
, t
);
290 r
= write_string_stream(f
,
299 ";comment2=comment2\n"
301 "\n\n" /* empty line */
302 , WRITE_STRING_FILE_AVOID_NEWLINE
);
305 r
= merge_env_file(&a
, NULL
, t
);
309 log_info("Got: <%s>", *i
);
311 assert_se(strv_isempty(a
));
314 TEST(executable_is_script
) {
315 _cleanup_(unlink_tempfilep
) char t
[] = "/tmp/test-fileio-XXXXXX";
316 _cleanup_fclose_
FILE *f
= NULL
;
320 assert_se(fmkostemp_safe(t
, "w", &f
) == 0);
321 fputs("#! /bin/script -a -b \ngoo goo", f
);
324 r
= executable_is_script(t
, &command
);
326 assert_se(streq(command
, "/bin/script"));
329 r
= executable_is_script("/bin/sh", &command
);
332 r
= executable_is_script("/usr/bin/yum", &command
);
333 assert_se(r
> 0 || r
== -ENOENT
);
335 assert_se(startswith(command
, "/"));
341 _cleanup_free_
char *p
= NULL
, *s
= NULL
, *z
= NULL
;
342 unsigned long long total
= 0, buffers
= 0;
345 r
= get_proc_field("/proc/meminfo", "MemTotal", WHITESPACE
, &p
);
349 assert_se(safe_atollu(p
, &total
) == 0);
352 r
= get_proc_field("/proc/meminfo", "Buffers", WHITESPACE
, &s
);
356 assert_se(safe_atollu(s
, &buffers
) == 0);
360 assert_se(buffers
< total
);
362 /* Seccomp should be a good test for field full of zeros. */
363 r
= get_proc_field("/proc/meminfo", "Seccomp", WHITESPACE
, &z
);
367 assert_se(safe_atollu(z
, &buffers
) == 0);
372 for (int pid
= 0; pid
< 2; pid
++) {
373 _cleanup_free_
char *capeff
= NULL
;
376 r
= get_process_capeff(0, &capeff
);
377 log_info("capeff: '%s' (r=%d)", capeff
, r
);
379 if (IN_SET(r
, -ENOENT
, -EPERM
))
384 p
= capeff
[strspn(capeff
, HEXDIGITS
)];
385 assert_se(!p
|| isspace(p
));
389 TEST(read_one_line_file
) {
390 _cleanup_(unlink_tempfilep
) char fn
[] = "/tmp/test-fileio-1lf-XXXXXX";
392 _cleanup_fclose_
FILE *f
= NULL
;
393 _cleanup_free_
char *buf
, *buf2
, *buf3
, *buf4
, *buf5
;
395 fd
= mkostemp_safe(fn
);
398 f
= fdopen(fd
, "we");
401 assert_se(read_one_line_file(fn
, &buf
) == 0);
402 assert_se(streq_ptr(buf
, ""));
403 assert_se(read_one_line_file(fn
, &buf2
) == 0);
404 assert_se(streq_ptr(buf2
, ""));
406 assert_se(write_string_stream(f
, "x", WRITE_STRING_FILE_AVOID_NEWLINE
) >= 0);
409 assert_se(read_one_line_file(fn
, &buf3
) == 1);
410 assert_se(streq_ptr(buf3
, "x"));
412 assert_se(write_string_stream(f
, "\n", WRITE_STRING_FILE_AVOID_NEWLINE
) >= 0);
415 assert_se(read_one_line_file(fn
, &buf4
) == 2);
416 assert_se(streq_ptr(buf4
, "x"));
418 assert_se(write_string_stream(f
, "\n", WRITE_STRING_FILE_AVOID_NEWLINE
) >= 0);
421 assert_se(read_one_line_file(fn
, &buf5
) == 2);
422 assert_se(streq_ptr(buf5
, "x"));
425 TEST(write_string_stream
) {
426 _cleanup_(unlink_tempfilep
) char fn
[] = "/tmp/test-write_string_stream-XXXXXX";
427 _cleanup_fclose_
FILE *f
= NULL
;
431 fd
= mkostemp_safe(fn
);
436 assert_se(write_string_stream(f
, "boohoo", 0) < 0);
442 assert_se(write_string_stream(f
, "boohoo", 0) == 0);
445 assert_se(fgets(buf
, sizeof(buf
), f
));
446 assert_se(streq(buf
, "boohoo\n"));
452 assert_se(write_string_stream(f
, "boohoo", WRITE_STRING_FILE_AVOID_NEWLINE
) == 0);
455 assert_se(fgets(buf
, sizeof(buf
), f
));
457 assert_se(streq(buf
, "boohoo"));
460 TEST(write_string_file
) {
461 _cleanup_(unlink_tempfilep
) char fn
[] = "/tmp/test-write_string_file-XXXXXX";
463 _cleanup_close_
int fd
;
465 fd
= mkostemp_safe(fn
);
468 assert_se(write_string_file(fn
, "boohoo", WRITE_STRING_FILE_CREATE
) == 0);
470 assert_se(read(fd
, buf
, sizeof(buf
)) == 7);
471 assert_se(streq(buf
, "boohoo\n"));
474 TEST(write_string_file_no_create
) {
475 _cleanup_(unlink_tempfilep
) char fn
[] = "/tmp/test-write_string_file_no_create-XXXXXX";
476 _cleanup_close_
int fd
;
479 fd
= mkostemp_safe(fn
);
482 assert_se(write_string_file("/a/file/which/does/not/exists/i/guess", "boohoo", 0) < 0);
483 assert_se(write_string_file(fn
, "boohoo", 0) == 0);
485 assert_se(read(fd
, buf
, sizeof buf
) == (ssize_t
) strlen("boohoo\n"));
486 assert_se(streq(buf
, "boohoo\n"));
489 TEST(write_string_file_verify
) {
490 _cleanup_free_
char *buf
= NULL
, *buf2
= NULL
;
493 r
= read_one_line_file("/proc/version", &buf
);
494 if (ERRNO_IS_PRIVILEGE(r
))
497 assert_se(buf2
= strjoin(buf
, "\n"));
499 r
= write_string_file("/proc/version", buf
, 0);
500 assert_se(IN_SET(r
, -EACCES
, -EIO
));
501 r
= write_string_file("/proc/version", buf2
, 0);
502 assert_se(IN_SET(r
, -EACCES
, -EIO
));
504 assert_se(write_string_file("/proc/version", buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
) == 0);
505 assert_se(write_string_file("/proc/version", buf2
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
) == 0);
507 r
= write_string_file("/proc/version", buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
|WRITE_STRING_FILE_AVOID_NEWLINE
);
508 assert_se(IN_SET(r
, -EACCES
, -EIO
));
509 assert_se(write_string_file("/proc/version", buf2
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
|WRITE_STRING_FILE_AVOID_NEWLINE
) == 0);
512 static void check_file_pairs_one(char **l
) {
514 assert_se(strv_length(l
) == 14);
516 STRV_FOREACH_PAIR(k
, v
, l
) {
517 assert_se(STR_IN_SET(*k
, "NAME", "ID", "PRETTY_NAME", "ANSI_COLOR", "HOME_URL", "SUPPORT_URL", "BUG_REPORT_URL"));
518 printf("%s=%s\n", *k
, *v
);
519 assert_se(!streq(*k
, "NAME") || streq(*v
, "Arch Linux"));
520 assert_se(!streq(*k
, "ID") || streq(*v
, "arch"));
521 assert_se(!streq(*k
, "PRETTY_NAME") || streq(*v
, "Arch Linux"));
522 assert_se(!streq(*k
, "ANSI_COLOR") || streq(*v
, "0;36"));
523 assert_se(!streq(*k
, "HOME_URL") || streq(*v
, "https://www.archlinux.org/"));
524 assert_se(!streq(*k
, "SUPPORT_URL") || streq(*v
, "https://bbs.archlinux.org/"));
525 assert_se(!streq(*k
, "BUG_REPORT_URL") || streq(*v
, "https://bugs.archlinux.org/"));
529 TEST(load_env_file_pairs
) {
530 _cleanup_(unlink_tempfilep
) char fn
[] = "/tmp/test-load_env_file_pairs-XXXXXX";
532 _cleanup_fclose_
FILE *f
= NULL
;
533 _cleanup_strv_free_
char **l
= NULL
;
535 fd
= mkostemp_safe(fn
);
538 r
= write_string_file(fn
,
539 "NAME=\"Arch Linux\"\n"
541 "PRETTY_NAME=\"Arch Linux\"\n"
542 "ANSI_COLOR=\"0;36\"\n"
543 "HOME_URL=\"https://www.archlinux.org/\"\n"
544 "SUPPORT_URL=\"https://bbs.archlinux.org/\"\n"
545 "BUG_REPORT_URL=\"https://bugs.archlinux.org/\"\n",
546 WRITE_STRING_FILE_CREATE
);
549 r
= load_env_file_pairs_fd(fd
, fn
, &l
);
551 check_file_pairs_one(l
);
557 r
= load_env_file_pairs(f
, fn
, &l
);
559 check_file_pairs_one(l
);
562 TEST(search_and_fopen
) {
563 static const char* const dirs
[] = {
568 char name
[] = "/tmp/test-search_and_fopen.XXXXXX";
569 _cleanup_fclose_
FILE *f
= NULL
;
570 _cleanup_free_
char *p
= NULL
;
571 _cleanup_close_
int fd
= -EBADF
;
575 fd
= mkostemp_safe(name
);
579 r
= search_and_fopen(basename(name
), "re", NULL
, (const char**) dirs
, &f
, &p
);
581 assert_se(e
= path_startswith(p
, "/tmp/"));
582 assert_se(streq(basename(name
), e
));
586 r
= search_and_fopen(name
, "re", NULL
, (const char**) dirs
, &f
, &p
);
588 assert_se(path_equal(name
, p
));
592 r
= search_and_fopen(basename(name
), "re", "/", (const char**) dirs
, &f
, &p
);
594 assert_se(e
= path_startswith(p
, "/tmp/"));
595 assert_se(streq(basename(name
), e
));
599 r
= search_and_fopen("/a/file/which/does/not/exist/i/guess", "re", NULL
, (const char**) dirs
, &f
, &p
);
600 assert_se(r
== -ENOENT
);
601 r
= search_and_fopen("afilewhichdoesnotexistiguess", "re", NULL
, (const char**) dirs
, &f
, &p
);
602 assert_se(r
== -ENOENT
);
607 r
= search_and_fopen(basename(name
), "re", NULL
, (const char**) dirs
, &f
, &p
);
608 assert_se(r
== -ENOENT
);
611 TEST(search_and_fopen_nulstr
) {
612 static const char dirs
[] =
616 _cleanup_(unlink_tempfilep
) char name
[] = "/tmp/test-search_and_fopen.XXXXXX";
617 _cleanup_fclose_
FILE *f
= NULL
;
618 _cleanup_free_
char *p
= NULL
;
619 _cleanup_close_
int fd
= -EBADF
;
623 fd
= mkostemp_safe(name
);
627 r
= search_and_fopen_nulstr(basename(name
), "re", NULL
, dirs
, &f
, &p
);
629 assert_se(e
= path_startswith(p
, "/tmp/"));
630 assert_se(streq(basename(name
), e
));
634 r
= search_and_fopen_nulstr(name
, "re", NULL
, dirs
, &f
, &p
);
636 assert_se(path_equal(name
, p
));
640 r
= search_and_fopen_nulstr("/a/file/which/does/not/exist/i/guess", "re", NULL
, dirs
, &f
, &p
);
641 assert_se(r
== -ENOENT
);
642 r
= search_and_fopen_nulstr("afilewhichdoesnotexistiguess", "re", NULL
, dirs
, &f
, &p
);
643 assert_se(r
== -ENOENT
);
648 r
= search_and_fopen_nulstr(basename(name
), "re", NULL
, dirs
, &f
, &p
);
649 assert_se(r
== -ENOENT
);
652 TEST(writing_tmpfile
) {
653 _cleanup_(unlink_tempfilep
) char name
[] = "/tmp/test-systemd_writing_tmpfile.XXXXXX";
654 _cleanup_free_
char *contents
= NULL
;
656 _cleanup_close_
int fd
= -EBADF
;
659 struct iovec iov
[] = {
660 IOVEC_MAKE_STRING("abc\n"),
661 IOVEC_MAKE_STRING(ALPHANUMERICAL
"\n"),
662 IOVEC_MAKE_STRING(""),
665 fd
= mkostemp_safe(name
);
666 printf("tmpfile: %s", name
);
668 r
= writev(fd
, iov
, 3);
671 r
= read_full_file(name
, &contents
, &size
);
673 printf("contents: %s", contents
);
674 assert_se(streq(contents
, "abc\n" ALPHANUMERICAL
"\n"));
678 char *ret
= NULL
, *p
;
680 assert_se(tempfn_xxxxxx("/foo/bar/waldo", NULL
, &ret
) >= 0);
681 assert_se(streq_ptr(ret
, "/foo/bar/.#waldoXXXXXX"));
684 assert_se(tempfn_xxxxxx("/foo/bar/waldo", "[miau]", &ret
) >= 0);
685 assert_se(streq_ptr(ret
, "/foo/bar/.#[miau]waldoXXXXXX"));
688 assert_se(tempfn_random("/foo/bar/waldo", NULL
, &ret
) >= 0);
689 assert_se(p
= startswith(ret
, "/foo/bar/.#waldo"));
690 assert_se(strlen(p
) == 16);
691 assert_se(in_charset(p
, "0123456789abcdef"));
694 assert_se(tempfn_random("/foo/bar/waldo", "[wuff]", &ret
) >= 0);
695 assert_se(p
= startswith(ret
, "/foo/bar/.#[wuff]waldo"));
696 assert_se(strlen(p
) == 16);
697 assert_se(in_charset(p
, "0123456789abcdef"));
700 assert_se(tempfn_random_child("/foo/bar/waldo", NULL
, &ret
) >= 0);
701 assert_se(p
= startswith(ret
, "/foo/bar/waldo/.#"));
702 assert_se(strlen(p
) == 16);
703 assert_se(in_charset(p
, "0123456789abcdef"));
706 assert_se(tempfn_random_child("/foo/bar/waldo", "[kikiriki]", &ret
) >= 0);
707 assert_se(p
= startswith(ret
, "/foo/bar/waldo/.#[kikiriki]"));
708 assert_se(strlen(p
) == 16);
709 assert_se(in_charset(p
, "0123456789abcdef"));
713 static const char chars
[] =
716 DISABLE_WARNING_TYPE_LIMITS
;
719 _cleanup_fclose_
FILE *f
= NULL
;
722 assert_se(f
= fmemopen_unlocked((void*) chars
, sizeof(chars
), "r"));
724 for (size_t i
= 0; i
< sizeof(chars
); i
++) {
725 assert_se(safe_fgetc(f
, &c
) == 1);
726 assert_se(c
== chars
[i
]);
728 if (ungetc(c
, f
) == EOF
) {
729 /* EOF is -1, and hence we can't push value 255 in this way – if char is signed */
730 assert_se(c
== (char) EOF
);
731 assert_se(CHAR_MIN
== -128); /* verify that char is signed on this platform */
733 assert_se(safe_fgetc(f
, &c
) == 1);
734 assert_se(c
== chars
[i
]);
737 /* But it works when we push it properly cast */
738 assert_se(ungetc((unsigned char) c
, f
) != EOF
);
739 assert_se(safe_fgetc(f
, &c
) == 1);
740 assert_se(c
== chars
[i
]);
743 assert_se(safe_fgetc(f
, &c
) == 0);
748 static const char buffer
[] =
750 "루Non-ascii chars: ąę„”\n"
753 "now the same with a NUL\n\0"
755 "and even more\r\n\0"
756 "and yet even more\n\r\0"
757 "With newlines, and a NUL byte\0"
761 "and a very long line that is supposed to be truncated, because it is so long\n";
763 static void test_read_line_one_file(FILE *f
) {
764 _cleanup_free_
char *line
= NULL
;
766 assert_se(read_line(f
, SIZE_MAX
, &line
) == 15 && streq(line
, "Some test data"));
769 assert_se(read_line(f
, SIZE_MAX
, &line
) > 0 && streq(line
, "루Non-ascii chars: ąę„”"));
772 assert_se(read_line(f
, SIZE_MAX
, &line
) == 13 && streq(line
, "terminators"));
775 assert_se(read_line(f
, SIZE_MAX
, &line
) == 15 && streq(line
, "and even more"));
778 assert_se(read_line(f
, SIZE_MAX
, &line
) == 25 && streq(line
, "now the same with a NUL"));
781 assert_se(read_line(f
, SIZE_MAX
, &line
) == 10 && streq(line
, "and more"));
784 assert_se(read_line(f
, SIZE_MAX
, &line
) == 16 && streq(line
, "and even more"));
787 assert_se(read_line(f
, SIZE_MAX
, &line
) == 20 && streq(line
, "and yet even more"));
790 assert_se(read_line(f
, 1024, &line
) == 30 && streq(line
, "With newlines, and a NUL byte"));
793 assert_se(read_line(f
, 1024, &line
) == 1 && streq(line
, ""));
796 assert_se(read_line(f
, 1024, &line
) == 14 && streq(line
, "an empty line"));
799 assert_se(read_line(f
, SIZE_MAX
, NULL
) == 16);
801 assert_se(read_line(f
, 16, &line
) == -ENOBUFS
);
804 /* read_line() stopped when it hit the limit, that means when we continue reading we'll read at the first
805 * character after the previous limit. Let's make use of that to continue our test. */
806 assert_se(read_line(f
, 1024, &line
) == 62 && streq(line
, "line that is supposed to be truncated, because it is so long"));
809 assert_se(read_line(f
, 1024, &line
) == 0 && streq(line
, ""));
813 _cleanup_fclose_
FILE *f
= NULL
;
815 assert_se(f
= fmemopen_unlocked((void*) buffer
, sizeof(buffer
), "r"));
816 test_read_line_one_file(f
);
820 _cleanup_(unlink_tempfilep
) char name
[] = "/tmp/test-fileio.XXXXXX";
822 _cleanup_fclose_
FILE *f
= NULL
;
824 fd
= mkostemp_safe(name
);
826 assert_se((size_t) write(fd
, buffer
, sizeof(buffer
)) == sizeof(buffer
));
828 assert_se(lseek(fd
, 0, SEEK_SET
) == 0);
829 assert_se(f
= fdopen(fd
, "r"));
831 test_read_line_one_file(f
);
835 _cleanup_fclose_
FILE *f
= NULL
;
836 _cleanup_free_
char *line
= NULL
;
839 f
= fopen("/proc/uptime", "re");
840 if (!f
&& IN_SET(errno
, ENOENT
, EPERM
))
844 r
= read_line(f
, LINE_MAX
, &line
);
847 assert_se(line
&& isempty(line
));
849 assert_se((size_t) r
== strlen(line
) + 1);
850 assert_se(read_line(f
, LINE_MAX
, NULL
) == 0);
854 static const struct {
858 /* Each of these will be followed by EOF and should generate the one same single string */
873 for (size_t i
= 0; i
< ELEMENTSOF(eof_endings
); i
++) {
874 _cleanup_fclose_
FILE *f
= NULL
;
875 _cleanup_free_
char *s
= NULL
;
877 assert_se(f
= fmemopen_unlocked((void*) eof_endings
[i
].string
, eof_endings
[i
].length
, "r"));
879 r
= read_line(f
, SIZE_MAX
, &s
);
880 assert_se((size_t) r
== eof_endings
[i
].length
);
881 assert_se(streq_ptr(s
, "foo"));
883 assert_se(read_line(f
, SIZE_MAX
, NULL
) == 0); /* Ensure we hit EOF */
887 TEST(read_nul_string
) {
888 static const char test
[] = "string nr. 1\0"
890 "\377empty string follows\0"
892 "final string\n is empty\0"
895 _cleanup_fclose_
FILE *f
= NULL
;
896 _cleanup_free_
char *s
= NULL
;
898 assert_se(f
= fmemopen_unlocked((void*) test
, sizeof(test
)-1, "r"));
900 assert_se(read_nul_string(f
, LONG_LINE_MAX
, &s
) == 13 && streq_ptr(s
, "string nr. 1"));
903 assert_se(read_nul_string(f
, LONG_LINE_MAX
, &s
) == 14 && streq_ptr(s
, "string nr. 2\n"));
906 assert_se(read_nul_string(f
, LONG_LINE_MAX
, &s
) == 22 && streq_ptr(s
, "\377empty string follows"));
909 assert_se(read_nul_string(f
, LONG_LINE_MAX
, &s
) == 1 && streq_ptr(s
, ""));
912 assert_se(read_nul_string(f
, LONG_LINE_MAX
, &s
) == 23 && streq_ptr(s
, "final string\n is empty"));
915 assert_se(read_nul_string(f
, LONG_LINE_MAX
, &s
) == 1 && streq_ptr(s
, ""));
918 assert_se(read_nul_string(f
, LONG_LINE_MAX
, &s
) == 0 && streq_ptr(s
, ""));
921 TEST(read_full_file_socket
) {
922 _cleanup_(rm_rf_physical_and_freep
) char *z
= NULL
;
923 _cleanup_close_
int listener
= -EBADF
;
924 _cleanup_free_
char *data
= NULL
, *clientname
= NULL
;
925 union sockaddr_union sa
;
931 listener
= socket(AF_UNIX
, SOCK_STREAM
|SOCK_CLOEXEC
, 0);
932 assert_se(listener
>= 0);
934 assert_se(mkdtemp_malloc(NULL
, &z
) >= 0);
935 j
= strjoina(z
, "/socket");
937 assert_se(sockaddr_un_set_path(&sa
.un
, j
) >= 0);
939 assert_se(bind(listener
, &sa
.sa
, SOCKADDR_UN_LEN(sa
.un
)) >= 0);
940 assert_se(listen(listener
, 1) >= 0);
942 /* Make sure the socket doesn't fit into a struct sockaddr_un, but we can still access it */
943 jj
= strjoina(z
, "/a_very_long_patha_very_long_patha_very_long_patha_very_long_patha_very_long_patha_very_long_patha_very_long_patha_very_long_path");
944 assert_se(strlen(jj
) > sizeof_field(struct sockaddr_un
, sun_path
));
945 assert_se(rename(j
, jj
) >= 0);
947 /* Bind the *client* socket to some randomized name, to verify that this works correctly. */
948 assert_se(asprintf(&clientname
, "@%" PRIx64
"/test-bindname", random_u64()) >= 0);
950 r
= safe_fork("(server)", FORK_DEATHSIG
|FORK_LOG
, &pid
);
953 union sockaddr_union peer
= {};
954 socklen_t peerlen
= sizeof(peer
);
955 _cleanup_close_
int rfd
= -EBADF
;
958 rfd
= accept4(listener
, NULL
, 0, SOCK_CLOEXEC
);
961 assert_se(getpeername(rfd
, &peer
.sa
, &peerlen
) >= 0);
963 assert_se(peer
.un
.sun_family
== AF_UNIX
);
964 assert_se(peerlen
> offsetof(struct sockaddr_un
, sun_path
));
965 assert_se(peer
.un
.sun_path
[0] == 0);
966 assert_se(streq(peer
.un
.sun_path
+ 1, clientname
+ 1));
968 #define TEST_STR "This is a test\nreally."
970 assert_se(write(rfd
, TEST_STR
, strlen(TEST_STR
)) == strlen(TEST_STR
));
974 assert_se(read_full_file_full(AT_FDCWD
, jj
, UINT64_MAX
, SIZE_MAX
, 0, NULL
, &data
, &size
) == -ENXIO
);
975 assert_se(read_full_file_full(AT_FDCWD
, jj
, UINT64_MAX
, SIZE_MAX
, READ_FULL_FILE_CONNECT_SOCKET
, clientname
, &data
, &size
) >= 0);
976 assert_se(size
== strlen(TEST_STR
));
977 assert_se(streq(data
, TEST_STR
));
979 assert_se(wait_for_terminate_and_check("(server)", pid
, WAIT_LOG
) >= 0);
983 TEST(read_full_file_offset_size
) {
984 _cleanup_fclose_
FILE *f
= NULL
;
985 _cleanup_(unlink_and_freep
) char *fn
= NULL
;
986 _cleanup_free_
char *rbuf
= NULL
;
990 random_bytes(buf
, sizeof(buf
));
992 assert_se(tempfn_random_child(NULL
, NULL
, &fn
) >= 0);
993 assert_se(f
= fopen(fn
, "we"));
994 assert_se(fwrite(buf
, 1, sizeof(buf
), f
) == sizeof(buf
));
995 assert_se(fflush_and_check(f
) >= 0);
997 assert_se(read_full_file_full(AT_FDCWD
, fn
, UINT64_MAX
, SIZE_MAX
, 0, NULL
, &rbuf
, &rbuf_size
) >= 0);
998 assert_se(rbuf_size
== sizeof(buf
));
999 assert_se(memcmp(buf
, rbuf
, rbuf_size
) == 0);
1002 assert_se(read_full_file_full(AT_FDCWD
, fn
, UINT64_MAX
, 128, 0, NULL
, &rbuf
, &rbuf_size
) >= 0);
1003 assert_se(rbuf_size
== 128);
1004 assert_se(memcmp(buf
, rbuf
, rbuf_size
) == 0);
1007 assert_se(read_full_file_full(AT_FDCWD
, fn
, UINT64_MAX
, 128, READ_FULL_FILE_FAIL_WHEN_LARGER
, NULL
, &rbuf
, &rbuf_size
) == -E2BIG
);
1008 assert_se(read_full_file_full(AT_FDCWD
, fn
, UINT64_MAX
, sizeof(buf
)-1, READ_FULL_FILE_FAIL_WHEN_LARGER
, NULL
, &rbuf
, &rbuf_size
) == -E2BIG
);
1009 assert_se(read_full_file_full(AT_FDCWD
, fn
, UINT64_MAX
, sizeof(buf
), READ_FULL_FILE_FAIL_WHEN_LARGER
, NULL
, &rbuf
, &rbuf_size
) >= 0);
1010 assert_se(rbuf_size
== sizeof(buf
));
1011 assert_se(memcmp(buf
, rbuf
, rbuf_size
) == 0);
1014 assert_se(read_full_file_full(AT_FDCWD
, fn
, 47, 128, READ_FULL_FILE_FAIL_WHEN_LARGER
, NULL
, &rbuf
, &rbuf_size
) == -E2BIG
);
1015 assert_se(read_full_file_full(AT_FDCWD
, fn
, 47, sizeof(buf
)-47-1, READ_FULL_FILE_FAIL_WHEN_LARGER
, NULL
, &rbuf
, &rbuf_size
) == -E2BIG
);
1016 assert_se(read_full_file_full(AT_FDCWD
, fn
, 47, sizeof(buf
)-47, READ_FULL_FILE_FAIL_WHEN_LARGER
, NULL
, &rbuf
, &rbuf_size
) >= 0);
1017 assert_se(rbuf_size
== sizeof(buf
)-47);
1018 assert_se(memcmp(buf
+47, rbuf
, rbuf_size
) == 0);
1021 assert_se(read_full_file_full(AT_FDCWD
, fn
, UINT64_MAX
, sizeof(buf
)+1, READ_FULL_FILE_FAIL_WHEN_LARGER
, NULL
, &rbuf
, &rbuf_size
) >= 0);
1022 assert_se(rbuf_size
== sizeof(buf
));
1023 assert_se(memcmp(buf
, rbuf
, rbuf_size
) == 0);
1026 assert_se(read_full_file_full(AT_FDCWD
, fn
, 1234, SIZE_MAX
, 0, NULL
, &rbuf
, &rbuf_size
) >= 0);
1027 assert_se(rbuf_size
== sizeof(buf
) - 1234);
1028 assert_se(memcmp(buf
+ 1234, rbuf
, rbuf_size
) == 0);
1031 assert_se(read_full_file_full(AT_FDCWD
, fn
, 2345, 777, 0, NULL
, &rbuf
, &rbuf_size
) >= 0);
1032 assert_se(rbuf_size
== 777);
1033 assert_se(memcmp(buf
+ 2345, rbuf
, rbuf_size
) == 0);
1036 assert_se(read_full_file_full(AT_FDCWD
, fn
, 4700, 20, 0, NULL
, &rbuf
, &rbuf_size
) >= 0);
1037 assert_se(rbuf_size
== 11);
1038 assert_se(memcmp(buf
+ 4700, rbuf
, rbuf_size
) == 0);
1041 assert_se(read_full_file_full(AT_FDCWD
, fn
, 10000, 99, 0, NULL
, &rbuf
, &rbuf_size
) >= 0);
1042 assert_se(rbuf_size
== 0);
1046 static void test_read_virtual_file_one(size_t max_size
) {
1049 log_info("/* %s (max_size=%zu) */", __func__
, max_size
);
1051 FOREACH_STRING(filename
,
1053 "/etc/nsswitch.conf",
1054 "/sys/kernel/uevent_seqnum",
1058 "/proc/self/pagemap") {
1060 _cleanup_free_
char *buf
= NULL
;
1063 r
= read_virtual_file(filename
, max_size
, &buf
, &size
);
1065 log_info_errno(r
, "read_virtual_file(\"%s\", %zu): %m", filename
, max_size
);
1066 assert_se(ERRNO_IS_PRIVILEGE(r
) || /* /proc/kcore is not accessible to unpriv */
1068 -ENOENT
, /* Some of the files might be absent */
1069 -EINVAL
, /* too small reads from /proc/self/pagemap trigger EINVAL */
1070 -EFBIG
)); /* /proc/kcore and /proc/self/pagemap should be too large */
1072 log_info("read_virtual_file(\"%s\", %zu): %s (%zu bytes)", filename
, max_size
, r
? "non-truncated" : "truncated", size
);
1076 TEST(test_read_virtual_file
) {
1077 test_read_virtual_file_one(0);
1078 test_read_virtual_file_one(1);
1079 test_read_virtual_file_one(2);
1080 test_read_virtual_file_one(20);
1081 test_read_virtual_file_one(4096);
1082 test_read_virtual_file_one(4097);
1083 test_read_virtual_file_one(SIZE_MAX
);
1086 TEST(test_fdopen_independent
) {
1087 #define TEST_TEXT "this is some random test text we are going to write to a memfd"
1088 _cleanup_close_
int fd
= -EBADF
;
1089 _cleanup_fclose_
FILE *f
= NULL
;
1090 char buf
[STRLEN(TEST_TEXT
) + 1];
1092 fd
= memfd_new("fdopen_independent");
1094 assert_se(ERRNO_IS_NOT_SUPPORTED(fd
));
1098 assert_se(write(fd
, TEST_TEXT
, strlen(TEST_TEXT
)) == strlen(TEST_TEXT
));
1099 /* we'll leave the read offset at the end of the memfd, the fdopen_independent() descriptors should
1100 * start at the beginning anyway */
1102 assert_se(fdopen_independent(fd
, "re", &f
) >= 0);
1104 assert_se(fread(buf
, 1, sizeof(buf
), f
) == strlen(TEST_TEXT
));
1105 assert_se(streq(buf
, TEST_TEXT
));
1106 assert_se((fcntl(fileno(f
), F_GETFL
) & O_ACCMODE
) == O_RDONLY
);
1107 assert_se(FLAGS_SET(fcntl(fileno(f
), F_GETFD
), FD_CLOEXEC
));
1110 assert_se(fdopen_independent(fd
, "r", &f
) >= 0);
1112 assert_se(fread(buf
, 1, sizeof(buf
), f
) == strlen(TEST_TEXT
));
1113 assert_se(streq(buf
, TEST_TEXT
));
1114 assert_se((fcntl(fileno(f
), F_GETFL
) & O_ACCMODE
) == O_RDONLY
);
1115 assert_se(!FLAGS_SET(fcntl(fileno(f
), F_GETFD
), FD_CLOEXEC
));
1118 assert_se(fdopen_independent(fd
, "r+e", &f
) >= 0);
1120 assert_se(fread(buf
, 1, sizeof(buf
), f
) == strlen(TEST_TEXT
));
1121 assert_se(streq(buf
, TEST_TEXT
));
1122 assert_se((fcntl(fileno(f
), F_GETFL
) & O_ACCMODE
) == O_RDWR
);
1123 assert_se(FLAGS_SET(fcntl(fileno(f
), F_GETFD
), FD_CLOEXEC
));
1128 DEFINE_TEST_MAIN(LOG_DEBUG
);