1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
8 #include "alloc-util.h"
16 #include "parse-util.h"
17 #include "process-util.h"
18 #include "random-util.h"
20 #include "socket-util.h"
21 #include "string-util.h"
24 #include "tmpfile-util.h"
27 static void test_parse_env_file(void) {
28 _cleanup_(unlink_tempfilep
) char
29 t
[] = "/tmp/test-fileio-in-XXXXXX",
30 p
[] = "/tmp/test-fileio-out-XXXXXX";
32 _cleanup_free_
char *one
= NULL
, *two
= NULL
, *three
= NULL
, *four
= NULL
, *five
= NULL
,
33 *six
= NULL
, *seven
= NULL
, *eight
= NULL
, *nine
= NULL
, *ten
= NULL
,
34 *eleven
= NULL
, *twelve
= NULL
, *thirteen
= NULL
;
35 _cleanup_strv_free_
char **a
= NULL
, **b
= NULL
;
40 assert_se(fmkostemp_safe(t
, "w", &f
) == 0);
47 "invalid line #comment\n"
50 "four = \'44\\\"44\'\n"
51 "five = \"55\\\"55\" \"FIVE\" cinco \n"
52 "six = seis sechs\\\n"
54 "seven=\"sevenval\" #nocomment\n"
55 "eight=eightval #nocomment\n"
56 "export nine=nineval\n"
61 "twelve=\"\\value\"\n"
62 "thirteen='\\value'", f
);
67 r
= load_env_file(NULL
, t
, &a
);
71 log_info("Got: <%s>", *i
);
73 assert_se(streq_ptr(a
[0], "one=BAR"));
74 assert_se(streq_ptr(a
[1], "two=bar"));
75 assert_se(streq_ptr(a
[2], "three=333\nxxxx"));
76 assert_se(streq_ptr(a
[3], "four=44\\\"44"));
77 assert_se(streq_ptr(a
[4], "five=55\"55FIVEcinco"));
78 assert_se(streq_ptr(a
[5], "six=seis sechs sis"));
79 assert_se(streq_ptr(a
[6], "seven=sevenval#nocomment"));
80 assert_se(streq_ptr(a
[7], "eight=eightval #nocomment"));
81 assert_se(streq_ptr(a
[8], "export nine=nineval"));
82 assert_se(streq_ptr(a
[9], "ten="));
83 assert_se(streq_ptr(a
[10], "eleven=value"));
84 assert_se(streq_ptr(a
[11], "twelve=\\value"));
85 assert_se(streq_ptr(a
[12], "thirteen=\\value"));
86 assert_se(a
[13] == NULL
);
92 log_info("Got2: <%s>", *i
);
93 assert_se(streq(*i
, a
[k
++]));
106 "export nine", &nine
,
110 "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 static void test_parse_multiline_env_file(void) {
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
;
176 assert_se(fmkostemp_safe(t
, "w", &f
) == 0);
178 "\\ \\ \\ \\ VAR\\\n"
189 assert_se(fflush_and_check(f
) >= 0);
192 test_one_shell_var(t
, "one", "BAR VAR\tGAR");
193 test_one_shell_var(t
, "two", "bar var\tgar");
194 test_one_shell_var(t
, "tri", "bar var \tgar ");
196 r
= load_env_file(NULL
, t
, &a
);
200 log_info("Got: <%s>", *i
);
202 assert_se(streq_ptr(a
[0], "one=BAR VAR\tGAR"));
203 assert_se(streq_ptr(a
[1], "two=bar var\tgar"));
204 assert_se(streq_ptr(a
[2], "tri=bar var \tgar "));
205 assert_se(a
[3] == NULL
);
208 _cleanup_close_
int fd
= mkostemp_safe(p
);
212 r
= write_env_file(p
, a
);
215 r
= load_env_file(NULL
, p
, &b
);
219 static void test_merge_env_file(void) {
220 _cleanup_(unlink_tempfilep
) char t
[] = "/tmp/test-fileio-XXXXXX";
221 _cleanup_fclose_
FILE *f
= NULL
;
222 _cleanup_strv_free_
char **a
= NULL
;
226 assert_se(fmkostemp_safe(t
, "w", &f
) == 0);
227 log_info("/* %s (%s) */", __func__
, t
);
229 r
= write_string_stream(f
,
232 "twentyone=2${one}\n"
234 "twentytwo=2${one}\n"
235 "xxx_minus_three=$xxx - 3\n"
236 "xxx=0x$one$one$one\n"
237 "yyy=${one:-fallback}\n"
238 "zzz=${one:+replacement}\n"
239 "zzzz=${foobar:-${nothing}}\n"
240 "zzzzz=${nothing:+${nothing}}\n"
241 , WRITE_STRING_FILE_AVOID_NEWLINE
);
244 r
= merge_env_file(&a
, NULL
, t
);
249 log_info("Got: <%s>", *i
);
251 assert_se(streq(a
[0], "one=2"));
252 assert_se(streq(a
[1], "twelve=12"));
253 assert_se(streq(a
[2], "twentyone=21"));
254 assert_se(streq(a
[3], "twentytwo=22"));
255 assert_se(streq(a
[4], "xxx=0x222"));
256 assert_se(streq(a
[5], "xxx_minus_three= - 3"));
257 assert_se(streq(a
[6], "yyy=2"));
258 assert_se(streq(a
[7], "zzz=replacement"));
259 assert_se(streq(a
[8], "zzzz="));
260 assert_se(streq(a
[9], "zzzzz="));
261 assert_se(a
[10] == NULL
);
263 r
= merge_env_file(&a
, NULL
, t
);
268 log_info("Got2: <%s>", *i
);
270 assert_se(streq(a
[0], "one=2"));
271 assert_se(streq(a
[1], "twelve=12"));
272 assert_se(streq(a
[2], "twentyone=21"));
273 assert_se(streq(a
[3], "twentytwo=22"));
274 assert_se(streq(a
[4], "xxx=0x222"));
275 assert_se(streq(a
[5], "xxx_minus_three=0x222 - 3"));
276 assert_se(streq(a
[6], "yyy=2"));
277 assert_se(streq(a
[7], "zzz=replacement"));
278 assert_se(streq(a
[8], "zzzz="));
279 assert_se(streq(a
[9], "zzzzz="));
280 assert_se(a
[10] == NULL
);
283 static void test_merge_env_file_invalid(void) {
284 _cleanup_(unlink_tempfilep
) char t
[] = "/tmp/test-fileio-XXXXXX";
285 _cleanup_fclose_
FILE *f
= NULL
;
286 _cleanup_strv_free_
char **a
= NULL
;
290 assert_se(fmkostemp_safe(t
, "w", &f
) == 0);
291 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 _cleanup_(unlink_tempfilep
) char t
[] = "/tmp/test-fileio-XXXXXX";
319 _cleanup_fclose_
FILE *f
= NULL
;
323 assert_se(fmkostemp_safe(t
, "w", &f
) == 0);
324 fputs("#! /bin/script -a -b \ngoo goo", f
);
327 r
= executable_is_script(t
, &command
);
329 assert_se(streq(command
, "/bin/script"));
332 r
= executable_is_script("/bin/sh", &command
);
335 r
= executable_is_script("/usr/bin/yum", &command
);
336 assert_se(r
> 0 || r
== -ENOENT
);
338 assert_se(startswith(command
, "/"));
343 static void test_status_field(void) {
344 _cleanup_free_
char *t
= NULL
, *p
= NULL
, *s
= NULL
, *z
= NULL
;
345 unsigned long long total
= 0, buffers
= 0;
348 assert_se(get_proc_field("/proc/self/status", "Threads", WHITESPACE
, &t
) == 0);
350 assert_se(streq(t
, "1"));
352 r
= get_proc_field("/proc/meminfo", "MemTotal", WHITESPACE
, &p
);
356 assert_se(safe_atollu(p
, &total
) == 0);
359 r
= get_proc_field("/proc/meminfo", "Buffers", WHITESPACE
, &s
);
363 assert_se(safe_atollu(s
, &buffers
) == 0);
367 assert_se(buffers
< total
);
369 /* Seccomp should be a good test for field full of zeros. */
370 r
= get_proc_field("/proc/meminfo", "Seccomp", WHITESPACE
, &z
);
374 assert_se(safe_atollu(z
, &buffers
) == 0);
378 static void test_capeff(void) {
381 for (pid
= 0; pid
< 2; pid
++) {
382 _cleanup_free_
char *capeff
= NULL
;
385 r
= get_process_capeff(0, &capeff
);
386 log_info("capeff: '%s' (r=%d)", capeff
, r
);
388 if (IN_SET(r
, -ENOENT
, -EPERM
))
393 p
= capeff
[strspn(capeff
, HEXDIGITS
)];
394 assert_se(!p
|| isspace(p
));
398 static void test_write_string_stream(void) {
399 _cleanup_(unlink_tempfilep
) char fn
[] = "/tmp/test-write_string_stream-XXXXXX";
400 _cleanup_fclose_
FILE *f
= NULL
;
404 fd
= mkostemp_safe(fn
);
409 assert_se(write_string_stream(f
, "boohoo", 0) < 0);
415 assert_se(write_string_stream(f
, "boohoo", 0) == 0);
418 assert_se(fgets(buf
, sizeof(buf
), f
));
419 assert_se(streq(buf
, "boohoo\n"));
425 assert_se(write_string_stream(f
, "boohoo", WRITE_STRING_FILE_AVOID_NEWLINE
) == 0);
428 assert_se(fgets(buf
, sizeof(buf
), f
));
430 assert_se(streq(buf
, "boohoo"));
433 static void test_write_string_file(void) {
434 _cleanup_(unlink_tempfilep
) char fn
[] = "/tmp/test-write_string_file-XXXXXX";
436 _cleanup_close_
int fd
;
438 fd
= mkostemp_safe(fn
);
441 assert_se(write_string_file(fn
, "boohoo", WRITE_STRING_FILE_CREATE
) == 0);
443 assert_se(read(fd
, buf
, sizeof(buf
)) == 7);
444 assert_se(streq(buf
, "boohoo\n"));
447 static void test_write_string_file_no_create(void) {
448 _cleanup_(unlink_tempfilep
) char fn
[] = "/tmp/test-write_string_file_no_create-XXXXXX";
449 _cleanup_close_
int fd
;
452 fd
= mkostemp_safe(fn
);
455 assert_se(write_string_file("/a/file/which/does/not/exists/i/guess", "boohoo", 0) < 0);
456 assert_se(write_string_file(fn
, "boohoo", 0) == 0);
458 assert_se(read(fd
, buf
, sizeof buf
) == (ssize_t
) strlen("boohoo\n"));
459 assert_se(streq(buf
, "boohoo\n"));
462 static void test_write_string_file_verify(void) {
463 _cleanup_free_
char *buf
= NULL
, *buf2
= NULL
;
466 r
= read_one_line_file("/proc/version", &buf
);
467 if (ERRNO_IS_PRIVILEGE(r
))
470 assert_se(buf2
= strjoin(buf
, "\n"));
472 r
= write_string_file("/proc/version", buf
, 0);
473 assert_se(IN_SET(r
, -EACCES
, -EIO
));
474 r
= write_string_file("/proc/version", buf2
, 0);
475 assert_se(IN_SET(r
, -EACCES
, -EIO
));
477 assert_se(write_string_file("/proc/version", buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
) == 0);
478 assert_se(write_string_file("/proc/version", buf2
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
) == 0);
480 r
= write_string_file("/proc/version", buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
|WRITE_STRING_FILE_AVOID_NEWLINE
);
481 assert_se(IN_SET(r
, -EACCES
, -EIO
));
482 assert_se(write_string_file("/proc/version", buf2
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
|WRITE_STRING_FILE_AVOID_NEWLINE
) == 0);
485 static void test_load_env_file_pairs(void) {
486 _cleanup_(unlink_tempfilep
) char fn
[] = "/tmp/test-load_env_file_pairs-XXXXXX";
488 _cleanup_fclose_
FILE *f
= NULL
;
489 _cleanup_strv_free_
char **l
= NULL
;
492 fd
= mkostemp_safe(fn
);
495 r
= write_string_file(fn
,
496 "NAME=\"Arch Linux\"\n"
498 "PRETTY_NAME=\"Arch Linux\"\n"
499 "ANSI_COLOR=\"0;36\"\n"
500 "HOME_URL=\"https://www.archlinux.org/\"\n"
501 "SUPPORT_URL=\"https://bbs.archlinux.org/\"\n"
502 "BUG_REPORT_URL=\"https://bugs.archlinux.org/\"\n",
503 WRITE_STRING_FILE_CREATE
);
509 r
= load_env_file_pairs(f
, fn
, &l
);
512 assert_se(strv_length(l
) == 14);
513 STRV_FOREACH_PAIR(k
, v
, l
) {
514 assert_se(STR_IN_SET(*k
, "NAME", "ID", "PRETTY_NAME", "ANSI_COLOR", "HOME_URL", "SUPPORT_URL", "BUG_REPORT_URL"));
515 printf("%s=%s\n", *k
, *v
);
516 if (streq(*k
, "NAME")) assert_se(streq(*v
, "Arch Linux"));
517 if (streq(*k
, "ID")) assert_se(streq(*v
, "arch"));
518 if (streq(*k
, "PRETTY_NAME")) assert_se(streq(*v
, "Arch Linux"));
519 if (streq(*k
, "ANSI_COLOR")) assert_se(streq(*v
, "0;36"));
520 if (streq(*k
, "HOME_URL")) assert_se(streq(*v
, "https://www.archlinux.org/"));
521 if (streq(*k
, "SUPPORT_URL")) assert_se(streq(*v
, "https://bbs.archlinux.org/"));
522 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
};
529 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
);
561 static void test_search_and_fopen_nulstr(void) {
562 const char dirs
[] = "/tmp/foo/bar\0/tmp\0";
564 _cleanup_(unlink_tempfilep
) char name
[] = "/tmp/test-search_and_fopen.XXXXXX";
568 fd
= mkostemp_safe(name
);
572 r
= search_and_fopen_nulstr(basename(name
), "r", NULL
, dirs
, &f
);
576 r
= search_and_fopen_nulstr(name
, "r", NULL
, dirs
, &f
);
580 r
= search_and_fopen_nulstr("/a/file/which/does/not/exist/i/guess", "r", NULL
, dirs
, &f
);
582 r
= search_and_fopen_nulstr("afilewhichdoesnotexistiguess", "r", NULL
, dirs
, &f
);
588 r
= search_and_fopen_nulstr(basename(name
), "r", NULL
, dirs
, &f
);
592 static void test_writing_tmpfile(void) {
593 _cleanup_(unlink_tempfilep
) char name
[] = "/tmp/test-systemd_writing_tmpfile.XXXXXX";
594 _cleanup_free_
char *contents
= NULL
;
596 _cleanup_close_
int fd
= -1;
600 iov
[0] = IOVEC_MAKE_STRING("abc\n");
601 iov
[1] = IOVEC_MAKE_STRING(ALPHANUMERICAL
"\n");
602 iov
[2] = IOVEC_MAKE_STRING("");
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"));
616 static void test_tempfn(void) {
617 char *ret
= NULL
, *p
;
619 assert_se(tempfn_xxxxxx("/foo/bar/waldo", NULL
, &ret
) >= 0);
620 assert_se(streq_ptr(ret
, "/foo/bar/.#waldoXXXXXX"));
623 assert_se(tempfn_xxxxxx("/foo/bar/waldo", "[miau]", &ret
) >= 0);
624 assert_se(streq_ptr(ret
, "/foo/bar/.#[miau]waldoXXXXXX"));
627 assert_se(tempfn_random("/foo/bar/waldo", NULL
, &ret
) >= 0);
628 assert_se(p
= startswith(ret
, "/foo/bar/.#waldo"));
629 assert_se(strlen(p
) == 16);
630 assert_se(in_charset(p
, "0123456789abcdef"));
633 assert_se(tempfn_random("/foo/bar/waldo", "[wuff]", &ret
) >= 0);
634 assert_se(p
= startswith(ret
, "/foo/bar/.#[wuff]waldo"));
635 assert_se(strlen(p
) == 16);
636 assert_se(in_charset(p
, "0123456789abcdef"));
639 assert_se(tempfn_random_child("/foo/bar/waldo", NULL
, &ret
) >= 0);
640 assert_se(p
= startswith(ret
, "/foo/bar/waldo/.#"));
641 assert_se(strlen(p
) == 16);
642 assert_se(in_charset(p
, "0123456789abcdef"));
645 assert_se(tempfn_random_child("/foo/bar/waldo", "[kikiriki]", &ret
) >= 0);
646 assert_se(p
= startswith(ret
, "/foo/bar/waldo/.#[kikiriki]"));
647 assert_se(strlen(p
) == 16);
648 assert_se(in_charset(p
, "0123456789abcdef"));
652 static const char chars
[] =
655 DISABLE_WARNING_TYPE_LIMITS
;
657 static void test_fgetc(void) {
658 _cleanup_fclose_
FILE *f
= NULL
;
661 f
= fmemopen_unlocked((void*) chars
, sizeof(chars
), "re");
664 for (size_t i
= 0; i
< sizeof(chars
); i
++) {
665 assert_se(safe_fgetc(f
, &c
) == 1);
666 assert_se(c
== chars
[i
]);
668 if (ungetc(c
, f
) == EOF
) {
669 /* EOF is -1, and hence we can't push value 255 in this way – if char is signed */
670 assert_se(c
== (char) EOF
);
671 assert_se(CHAR_MIN
== -128); /* verify that char is signed on this platform */
673 assert_se(safe_fgetc(f
, &c
) == 1);
674 assert_se(c
== chars
[i
]);
677 /* But it works when we push it properly cast */
678 assert_se(ungetc((unsigned char) c
, f
) != EOF
);
679 assert_se(safe_fgetc(f
, &c
) == 1);
680 assert_se(c
== chars
[i
]);
683 assert_se(safe_fgetc(f
, &c
) == 0);
688 static const char buffer
[] =
690 "루Non-ascii chars: ąę„”\n"
693 "now the same with a NUL\n\0"
695 "and even more\r\n\0"
696 "and yet even more\n\r\0"
697 "With newlines, and a NUL byte\0"
701 "and a very long line that is supposed to be truncated, because it is so long\n";
703 static void test_read_line_one_file(FILE *f
) {
704 _cleanup_free_
char *line
= NULL
;
706 assert_se(read_line(f
, SIZE_MAX
, &line
) == 15 && streq(line
, "Some test data"));
709 assert_se(read_line(f
, SIZE_MAX
, &line
) > 0 && streq(line
, "루Non-ascii chars: ąę„”"));
712 assert_se(read_line(f
, SIZE_MAX
, &line
) == 13 && streq(line
, "terminators"));
715 assert_se(read_line(f
, SIZE_MAX
, &line
) == 15 && streq(line
, "and even more"));
718 assert_se(read_line(f
, SIZE_MAX
, &line
) == 25 && streq(line
, "now the same with a NUL"));
721 assert_se(read_line(f
, SIZE_MAX
, &line
) == 10 && streq(line
, "and more"));
724 assert_se(read_line(f
, SIZE_MAX
, &line
) == 16 && streq(line
, "and even more"));
727 assert_se(read_line(f
, SIZE_MAX
, &line
) == 20 && streq(line
, "and yet even more"));
730 assert_se(read_line(f
, 1024, &line
) == 30 && streq(line
, "With newlines, and a NUL byte"));
733 assert_se(read_line(f
, 1024, &line
) == 1 && streq(line
, ""));
736 assert_se(read_line(f
, 1024, &line
) == 14 && streq(line
, "an empty line"));
739 assert_se(read_line(f
, SIZE_MAX
, NULL
) == 16);
741 assert_se(read_line(f
, 16, &line
) == -ENOBUFS
);
744 /* read_line() stopped when it hit the limit, that means when we continue reading we'll read at the first
745 * character after the previous limit. Let's make use of that to continue our test. */
746 assert_se(read_line(f
, 1024, &line
) == 62 && streq(line
, "line that is supposed to be truncated, because it is so long"));
749 assert_se(read_line(f
, 1024, &line
) == 0 && streq(line
, ""));
752 static void test_read_line(void) {
753 _cleanup_fclose_
FILE *f
= NULL
;
755 f
= fmemopen_unlocked((void*) buffer
, sizeof(buffer
), "re");
758 test_read_line_one_file(f
);
761 static void test_read_line2(void) {
762 _cleanup_(unlink_tempfilep
) char name
[] = "/tmp/test-fileio.XXXXXX";
764 _cleanup_fclose_
FILE *f
= NULL
;
766 fd
= mkostemp_safe(name
);
768 assert_se((size_t) write(fd
, buffer
, sizeof(buffer
)) == sizeof(buffer
));
770 assert_se(lseek(fd
, 0, SEEK_SET
) == 0);
771 assert_se(f
= fdopen(fd
, "r"));
773 test_read_line_one_file(f
);
776 static void test_read_line3(void) {
777 _cleanup_fclose_
FILE *f
= NULL
;
778 _cleanup_free_
char *line
= NULL
;
781 f
= fopen("/proc/uptime", "re");
782 if (!f
&& IN_SET(errno
, ENOENT
, EPERM
))
786 r
= read_line(f
, LINE_MAX
, &line
);
789 assert_se(line
&& isempty(line
));
791 assert_se((size_t) r
== strlen(line
) + 1);
792 assert_se(read_line(f
, LINE_MAX
, NULL
) == 0);
795 static void test_read_line4(void) {
796 static const struct {
800 /* Each of these will be followed by EOF and should generate the one same single string */
816 for (i
= 0; i
< ELEMENTSOF(eof_endings
); i
++) {
817 _cleanup_fclose_
FILE *f
= NULL
;
818 _cleanup_free_
char *s
= NULL
;
820 assert_se(f
= fmemopen_unlocked((void*) eof_endings
[i
].string
, eof_endings
[i
].length
, "r"));
822 r
= read_line(f
, SIZE_MAX
, &s
);
823 assert_se((size_t) r
== eof_endings
[i
].length
);
824 assert_se(streq_ptr(s
, "foo"));
826 assert_se(read_line(f
, SIZE_MAX
, NULL
) == 0); /* Ensure we hit EOF */
830 static void test_read_nul_string(void) {
831 static const char test
[] = "string nr. 1\0"
833 "\377empty string follows\0"
835 "final string\n is empty\0"
838 _cleanup_fclose_
FILE *f
= NULL
;
839 _cleanup_free_
char *s
= NULL
;
841 assert_se(f
= fmemopen_unlocked((void*) test
, sizeof(test
)-1, "r"));
843 assert_se(read_nul_string(f
, LONG_LINE_MAX
, &s
) == 13 && streq_ptr(s
, "string nr. 1"));
846 assert_se(read_nul_string(f
, LONG_LINE_MAX
, &s
) == 14 && streq_ptr(s
, "string nr. 2\n"));
849 assert_se(read_nul_string(f
, LONG_LINE_MAX
, &s
) == 22 && streq_ptr(s
, "\377empty string follows"));
852 assert_se(read_nul_string(f
, LONG_LINE_MAX
, &s
) == 1 && streq_ptr(s
, ""));
855 assert_se(read_nul_string(f
, LONG_LINE_MAX
, &s
) == 23 && streq_ptr(s
, "final string\n is empty"));
858 assert_se(read_nul_string(f
, LONG_LINE_MAX
, &s
) == 1 && streq_ptr(s
, ""));
861 assert_se(read_nul_string(f
, LONG_LINE_MAX
, &s
) == 0 && streq_ptr(s
, ""));
864 static void test_read_full_file_socket(void) {
865 _cleanup_(rm_rf_physical_and_freep
) char *z
= NULL
;
866 _cleanup_close_
int listener
= -1;
867 _cleanup_free_
char *data
= NULL
, *clientname
= NULL
;
868 union sockaddr_union sa
;
874 log_info("/* %s */", __func__
);
876 listener
= socket(AF_UNIX
, SOCK_STREAM
|SOCK_CLOEXEC
, 0);
877 assert_se(listener
>= 0);
879 assert_se(mkdtemp_malloc(NULL
, &z
) >= 0);
880 j
= strjoina(z
, "/socket");
882 assert_se(sockaddr_un_set_path(&sa
.un
, j
) >= 0);
884 assert_se(bind(listener
, &sa
.sa
, SOCKADDR_UN_LEN(sa
.un
)) >= 0);
885 assert_se(listen(listener
, 1) >= 0);
887 /* Bind the *client* socket to some randomized name, to verify that this works correctly. */
888 assert_se(asprintf(&clientname
, "@%" PRIx64
"/test-bindname", random_u64()) >= 0);
890 r
= safe_fork("(server)", FORK_DEATHSIG
|FORK_LOG
, &pid
);
893 union sockaddr_union peer
= {};
894 socklen_t peerlen
= sizeof(peer
);
895 _cleanup_close_
int rfd
= -1;
898 rfd
= accept4(listener
, NULL
, 0, SOCK_CLOEXEC
);
901 assert_se(getpeername(rfd
, &peer
.sa
, &peerlen
) >= 0);
903 assert_se(peer
.un
.sun_family
== AF_UNIX
);
904 assert_se(peerlen
> offsetof(struct sockaddr_un
, sun_path
));
905 assert_se(peer
.un
.sun_path
[0] == 0);
906 assert_se(streq(peer
.un
.sun_path
+ 1, clientname
+ 1));
908 #define TEST_STR "This is a test\nreally."
910 assert_se(write(rfd
, TEST_STR
, strlen(TEST_STR
)) == strlen(TEST_STR
));
914 assert_se(read_full_file_full(AT_FDCWD
, j
, UINT64_MAX
, SIZE_MAX
, 0, NULL
, &data
, &size
) == -ENXIO
);
915 assert_se(read_full_file_full(AT_FDCWD
, j
, UINT64_MAX
, SIZE_MAX
, READ_FULL_FILE_CONNECT_SOCKET
, clientname
, &data
, &size
) >= 0);
916 assert_se(size
== strlen(TEST_STR
));
917 assert_se(streq(data
, TEST_STR
));
919 assert_se(wait_for_terminate_and_check("(server)", pid
, WAIT_LOG
) >= 0);
923 static void test_read_full_file_offset_size(void) {
924 _cleanup_fclose_
FILE *f
= NULL
;
925 _cleanup_(unlink_and_freep
) char *fn
= NULL
;
926 _cleanup_free_
char *rbuf
= NULL
;
930 random_bytes(buf
, sizeof(buf
));
932 assert_se(tempfn_random_child(NULL
, NULL
, &fn
) >= 0);
933 assert_se(f
= fopen(fn
, "we"));
934 assert_se(fwrite(buf
, 1, sizeof(buf
), f
) == sizeof(buf
));
935 assert_se(fflush_and_check(f
) >= 0);
937 assert_se(read_full_file_full(AT_FDCWD
, fn
, UINT64_MAX
, SIZE_MAX
, 0, NULL
, &rbuf
, &rbuf_size
) >= 0);
938 assert_se(rbuf_size
== sizeof(buf
));
939 assert_se(memcmp(buf
, rbuf
, rbuf_size
) == 0);
942 assert_se(read_full_file_full(AT_FDCWD
, fn
, UINT64_MAX
, 128, 0, NULL
, &rbuf
, &rbuf_size
) >= 0);
943 assert_se(rbuf_size
== 128);
944 assert_se(memcmp(buf
, rbuf
, rbuf_size
) == 0);
947 assert_se(read_full_file_full(AT_FDCWD
, fn
, 1234, SIZE_MAX
, 0, NULL
, &rbuf
, &rbuf_size
) >= 0);
948 assert_se(rbuf_size
== sizeof(buf
) - 1234);
949 assert_se(memcmp(buf
+ 1234, rbuf
, rbuf_size
) == 0);
952 assert_se(read_full_file_full(AT_FDCWD
, fn
, 2345, 777, 0, NULL
, &rbuf
, &rbuf_size
) >= 0);
953 assert_se(rbuf_size
== 777);
954 assert_se(memcmp(buf
+ 2345, rbuf
, rbuf_size
) == 0);
957 assert_se(read_full_file_full(AT_FDCWD
, fn
, 4700, 20, 0, NULL
, &rbuf
, &rbuf_size
) >= 0);
958 assert_se(rbuf_size
== 11);
959 assert_se(memcmp(buf
+ 4700, rbuf
, rbuf_size
) == 0);
962 assert_se(read_full_file_full(AT_FDCWD
, fn
, 10000, 99, 0, NULL
, &rbuf
, &rbuf_size
) >= 0);
963 assert_se(rbuf_size
== 0);
967 int main(int argc
, char *argv
[]) {
968 test_setup_logging(LOG_DEBUG
);
970 test_parse_env_file();
971 test_parse_multiline_env_file();
972 test_merge_env_file();
973 test_merge_env_file_invalid();
974 test_executable_is_script();
977 test_write_string_stream();
978 test_write_string_file();
979 test_write_string_file_no_create();
980 test_write_string_file_verify();
981 test_load_env_file_pairs();
982 test_search_and_fopen();
983 test_search_and_fopen_nulstr();
984 test_writing_tmpfile();
991 test_read_nul_string();
992 test_read_full_file_socket();
993 test_read_full_file_offset_size();