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 *eleven
= NULL
, *twelve
= NULL
, *thirteen
= NULL
;
31 _cleanup_strv_free_
char **a
= NULL
, **b
= NULL
;
36 assert_se(fmkostemp_safe(t
, "w", &f
) == 0);
43 "invalid line #comment\n"
46 "four = \'44\\\"44\'\n"
47 "five = \"55\\\"55\" \"FIVE\" cinco \n"
48 "six = seis sechs\\\n"
50 "seven=\"sevenval\" #nocomment\n"
51 "eight=eightval #nocomment\n"
52 "export nine=nineval\n"
57 "twelve=\"\\value\"\n"
58 "thirteen='\\value'", f
);
63 r
= load_env_file(NULL
, t
, &a
);
67 log_info("Got: <%s>", *i
);
69 assert_se(streq_ptr(a
[0], "one=BAR"));
70 assert_se(streq_ptr(a
[1], "two=bar"));
71 assert_se(streq_ptr(a
[2], "three=333\nxxxx"));
72 assert_se(streq_ptr(a
[3], "four=44\\\"44"));
73 assert_se(streq_ptr(a
[4], "five=55\"55FIVEcinco"));
74 assert_se(streq_ptr(a
[5], "six=seis sechs sis"));
75 assert_se(streq_ptr(a
[6], "seven=sevenval#nocomment"));
76 assert_se(streq_ptr(a
[7], "eight=eightval #nocomment"));
77 assert_se(streq_ptr(a
[8], "export nine=nineval"));
78 assert_se(streq_ptr(a
[9], "ten="));
79 assert_se(streq_ptr(a
[10], "eleven=value"));
80 assert_se(streq_ptr(a
[11], "twelve=\\value"));
81 assert_se(streq_ptr(a
[12], "thirteen=\\value"));
82 assert_se(a
[13] == NULL
);
88 log_info("Got2: <%s>", *i
);
89 assert_se(streq(*i
, a
[k
++]));
102 "export nine", &nine
,
106 "thirteen", &thirteen
);
110 log_info("one=[%s]", strna(one
));
111 log_info("two=[%s]", strna(two
));
112 log_info("three=[%s]", strna(three
));
113 log_info("four=[%s]", strna(four
));
114 log_info("five=[%s]", strna(five
));
115 log_info("six=[%s]", strna(six
));
116 log_info("seven=[%s]", strna(seven
));
117 log_info("eight=[%s]", strna(eight
));
118 log_info("export nine=[%s]", strna(nine
));
119 log_info("ten=[%s]", strna(nine
));
120 log_info("eleven=[%s]", strna(eleven
));
121 log_info("twelve=[%s]", strna(twelve
));
122 log_info("thirteen=[%s]", strna(thirteen
));
124 assert_se(streq(one
, "BAR"));
125 assert_se(streq(two
, "bar"));
126 assert_se(streq(three
, "333\nxxxx"));
127 assert_se(streq(four
, "44\\\"44"));
128 assert_se(streq(five
, "55\"55FIVEcinco"));
129 assert_se(streq(six
, "seis sechs sis"));
130 assert_se(streq(seven
, "sevenval#nocomment"));
131 assert_se(streq(eight
, "eightval #nocomment"));
132 assert_se(streq(nine
, "nineval"));
133 assert_se(ten
== NULL
);
134 assert_se(streq(eleven
, "value"));
135 assert_se(streq(twelve
, "\\value"));
136 assert_se(streq(thirteen
, "\\value"));
139 /* prepare a temporary file to write the environment to */
140 _cleanup_close_
int fd
= mkostemp_safe(p
);
144 r
= write_env_file(p
, a
);
147 r
= load_env_file(NULL
, p
, &b
);
151 static void test_parse_multiline_env_file(void) {
152 _cleanup_(unlink_tempfilep
) char
153 t
[] = "/tmp/test-fileio-in-XXXXXX",
154 p
[] = "/tmp/test-fileio-out-XXXXXX";
156 _cleanup_strv_free_
char **a
= NULL
, **b
= NULL
;
160 assert_se(fmkostemp_safe(t
, "w", &f
) == 0);
176 r
= load_env_file(NULL
, t
, &a
);
180 log_info("Got: <%s>", *i
);
182 assert_se(streq_ptr(a
[0], "one=BAR VAR\tGAR"));
183 assert_se(streq_ptr(a
[1], "two=bar var\tgar"));
184 assert_se(streq_ptr(a
[2], "tri=bar var \tgar "));
185 assert_se(a
[3] == NULL
);
188 _cleanup_close_
int fd
= mkostemp_safe(p
);
192 r
= write_env_file(p
, a
);
195 r
= load_env_file(NULL
, p
, &b
);
199 static void test_merge_env_file(void) {
200 _cleanup_(unlink_tempfilep
) char t
[] = "/tmp/test-fileio-XXXXXX";
201 _cleanup_fclose_
FILE *f
= NULL
;
202 _cleanup_strv_free_
char **a
= NULL
;
206 assert_se(fmkostemp_safe(t
, "w", &f
) == 0);
207 log_info("/* %s (%s) */", __func__
, t
);
209 r
= write_string_stream(f
,
212 "twentyone=2${one}\n"
214 "twentytwo=2${one}\n"
215 "xxx_minus_three=$xxx - 3\n"
216 "xxx=0x$one$one$one\n"
217 "yyy=${one:-fallback}\n"
218 "zzz=${one:+replacement}\n"
219 "zzzz=${foobar:-${nothing}}\n"
220 "zzzzz=${nothing:+${nothing}}\n"
221 , WRITE_STRING_FILE_AVOID_NEWLINE
);
224 r
= merge_env_file(&a
, NULL
, t
);
229 log_info("Got: <%s>", *i
);
231 assert_se(streq(a
[0], "one=2"));
232 assert_se(streq(a
[1], "twelve=12"));
233 assert_se(streq(a
[2], "twentyone=21"));
234 assert_se(streq(a
[3], "twentytwo=22"));
235 assert_se(streq(a
[4], "xxx=0x222"));
236 assert_se(streq(a
[5], "xxx_minus_three= - 3"));
237 assert_se(streq(a
[6], "yyy=2"));
238 assert_se(streq(a
[7], "zzz=replacement"));
239 assert_se(streq(a
[8], "zzzz="));
240 assert_se(streq(a
[9], "zzzzz="));
241 assert_se(a
[10] == NULL
);
243 r
= merge_env_file(&a
, NULL
, t
);
248 log_info("Got2: <%s>", *i
);
250 assert_se(streq(a
[0], "one=2"));
251 assert_se(streq(a
[1], "twelve=12"));
252 assert_se(streq(a
[2], "twentyone=21"));
253 assert_se(streq(a
[3], "twentytwo=22"));
254 assert_se(streq(a
[4], "xxx=0x222"));
255 assert_se(streq(a
[5], "xxx_minus_three=0x222 - 3"));
256 assert_se(streq(a
[6], "yyy=2"));
257 assert_se(streq(a
[7], "zzz=replacement"));
258 assert_se(streq(a
[8], "zzzz="));
259 assert_se(streq(a
[9], "zzzzz="));
260 assert_se(a
[10] == NULL
);
263 static void test_merge_env_file_invalid(void) {
264 _cleanup_(unlink_tempfilep
) char t
[] = "/tmp/test-fileio-XXXXXX";
265 _cleanup_fclose_
FILE *f
= NULL
;
266 _cleanup_strv_free_
char **a
= NULL
;
270 assert_se(fmkostemp_safe(t
, "w", &f
) == 0);
271 log_info("/* %s (%s) */", __func__
, t
);
273 r
= write_string_stream(f
,
282 ";comment2=comment2\n"
284 "\n\n" /* empty line */
285 , WRITE_STRING_FILE_AVOID_NEWLINE
);
288 r
= merge_env_file(&a
, NULL
, t
);
292 log_info("Got: <%s>", *i
);
294 assert_se(strv_isempty(a
));
297 static void test_executable_is_script(void) {
298 _cleanup_(unlink_tempfilep
) char t
[] = "/tmp/test-fileio-XXXXXX";
299 _cleanup_fclose_
FILE *f
= NULL
;
303 assert_se(fmkostemp_safe(t
, "w", &f
) == 0);
304 fputs("#! /bin/script -a -b \ngoo goo", f
);
307 r
= executable_is_script(t
, &command
);
309 assert_se(streq(command
, "/bin/script"));
312 r
= executable_is_script("/bin/sh", &command
);
315 r
= executable_is_script("/usr/bin/yum", &command
);
316 assert_se(r
> 0 || r
== -ENOENT
);
318 assert_se(startswith(command
, "/"));
323 static void test_status_field(void) {
324 _cleanup_free_
char *t
= NULL
, *p
= NULL
, *s
= NULL
, *z
= NULL
;
325 unsigned long long total
= 0, buffers
= 0;
328 assert_se(get_proc_field("/proc/self/status", "Threads", WHITESPACE
, &t
) == 0);
330 assert_se(streq(t
, "1"));
332 r
= get_proc_field("/proc/meminfo", "MemTotal", WHITESPACE
, &p
);
336 assert_se(safe_atollu(p
, &total
) == 0);
339 r
= get_proc_field("/proc/meminfo", "Buffers", WHITESPACE
, &s
);
343 assert_se(safe_atollu(s
, &buffers
) == 0);
347 assert_se(buffers
< total
);
349 /* Seccomp should be a good test for field full of zeros. */
350 r
= get_proc_field("/proc/meminfo", "Seccomp", WHITESPACE
, &z
);
354 assert_se(safe_atollu(z
, &buffers
) == 0);
358 static void test_capeff(void) {
361 for (pid
= 0; pid
< 2; pid
++) {
362 _cleanup_free_
char *capeff
= NULL
;
365 r
= get_process_capeff(0, &capeff
);
366 log_info("capeff: '%s' (r=%d)", capeff
, r
);
368 if (IN_SET(r
, -ENOENT
, -EPERM
))
373 p
= capeff
[strspn(capeff
, HEXDIGITS
)];
374 assert_se(!p
|| isspace(p
));
378 static void test_write_string_stream(void) {
379 _cleanup_(unlink_tempfilep
) char fn
[] = "/tmp/test-write_string_stream-XXXXXX";
380 _cleanup_fclose_
FILE *f
= NULL
;
384 fd
= mkostemp_safe(fn
);
389 assert_se(write_string_stream(f
, "boohoo", 0) < 0);
395 assert_se(write_string_stream(f
, "boohoo", 0) == 0);
398 assert_se(fgets(buf
, sizeof(buf
), f
));
399 assert_se(streq(buf
, "boohoo\n"));
405 assert_se(write_string_stream(f
, "boohoo", WRITE_STRING_FILE_AVOID_NEWLINE
) == 0);
408 assert_se(fgets(buf
, sizeof(buf
), f
));
410 assert_se(streq(buf
, "boohoo"));
413 static void test_write_string_file(void) {
414 _cleanup_(unlink_tempfilep
) char fn
[] = "/tmp/test-write_string_file-XXXXXX";
416 _cleanup_close_
int fd
;
418 fd
= mkostemp_safe(fn
);
421 assert_se(write_string_file(fn
, "boohoo", WRITE_STRING_FILE_CREATE
) == 0);
423 assert_se(read(fd
, buf
, sizeof(buf
)) == 7);
424 assert_se(streq(buf
, "boohoo\n"));
427 static void test_write_string_file_no_create(void) {
428 _cleanup_(unlink_tempfilep
) char fn
[] = "/tmp/test-write_string_file_no_create-XXXXXX";
429 _cleanup_close_
int fd
;
432 fd
= mkostemp_safe(fn
);
435 assert_se(write_string_file("/a/file/which/does/not/exists/i/guess", "boohoo", 0) < 0);
436 assert_se(write_string_file(fn
, "boohoo", 0) == 0);
438 assert_se(read(fd
, buf
, sizeof buf
) == (ssize_t
) strlen("boohoo\n"));
439 assert_se(streq(buf
, "boohoo\n"));
442 static void test_write_string_file_verify(void) {
443 _cleanup_free_
char *buf
= NULL
, *buf2
= NULL
;
446 assert_se(read_one_line_file("/proc/cmdline", &buf
) >= 0);
447 assert_se(buf2
= strjoin(buf
, "\n"));
449 r
= write_string_file("/proc/cmdline", buf
, 0);
450 assert_se(IN_SET(r
, -EACCES
, -EIO
));
451 r
= write_string_file("/proc/cmdline", buf2
, 0);
452 assert_se(IN_SET(r
, -EACCES
, -EIO
));
454 assert_se(write_string_file("/proc/cmdline", buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
) == 0);
455 assert_se(write_string_file("/proc/cmdline", buf2
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
) == 0);
457 r
= write_string_file("/proc/cmdline", buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
|WRITE_STRING_FILE_AVOID_NEWLINE
);
458 assert_se(IN_SET(r
, -EACCES
, -EIO
));
459 assert_se(write_string_file("/proc/cmdline", buf2
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
|WRITE_STRING_FILE_AVOID_NEWLINE
) == 0);
462 static void test_load_env_file_pairs(void) {
463 _cleanup_(unlink_tempfilep
) char fn
[] = "/tmp/test-load_env_file_pairs-XXXXXX";
465 _cleanup_fclose_
FILE *f
= NULL
;
466 _cleanup_strv_free_
char **l
= NULL
;
469 fd
= mkostemp_safe(fn
);
472 r
= write_string_file(fn
,
473 "NAME=\"Arch Linux\"\n"
475 "PRETTY_NAME=\"Arch Linux\"\n"
476 "ANSI_COLOR=\"0;36\"\n"
477 "HOME_URL=\"https://www.archlinux.org/\"\n"
478 "SUPPORT_URL=\"https://bbs.archlinux.org/\"\n"
479 "BUG_REPORT_URL=\"https://bugs.archlinux.org/\"\n",
480 WRITE_STRING_FILE_CREATE
);
486 r
= load_env_file_pairs(f
, fn
, &l
);
489 assert_se(strv_length(l
) == 14);
490 STRV_FOREACH_PAIR(k
, v
, l
) {
491 assert_se(STR_IN_SET(*k
, "NAME", "ID", "PRETTY_NAME", "ANSI_COLOR", "HOME_URL", "SUPPORT_URL", "BUG_REPORT_URL"));
492 printf("%s=%s\n", *k
, *v
);
493 if (streq(*k
, "NAME")) assert_se(streq(*v
, "Arch Linux"));
494 if (streq(*k
, "ID")) assert_se(streq(*v
, "arch"));
495 if (streq(*k
, "PRETTY_NAME")) assert_se(streq(*v
, "Arch Linux"));
496 if (streq(*k
, "ANSI_COLOR")) assert_se(streq(*v
, "0;36"));
497 if (streq(*k
, "HOME_URL")) assert_se(streq(*v
, "https://www.archlinux.org/"));
498 if (streq(*k
, "SUPPORT_URL")) assert_se(streq(*v
, "https://bbs.archlinux.org/"));
499 if (streq(*k
, "BUG_REPORT_URL")) assert_se(streq(*v
, "https://bugs.archlinux.org/"));
503 static void test_search_and_fopen(void) {
504 const char *dirs
[] = {"/tmp/foo/bar", "/tmp", NULL
};
506 char name
[] = "/tmp/test-search_and_fopen.XXXXXX";
510 fd
= mkostemp_safe(name
);
514 r
= search_and_fopen(basename(name
), "r", NULL
, dirs
, &f
);
518 r
= search_and_fopen(name
, "r", NULL
, dirs
, &f
);
522 r
= search_and_fopen(basename(name
), "r", "/", dirs
, &f
);
526 r
= search_and_fopen("/a/file/which/does/not/exist/i/guess", "r", NULL
, dirs
, &f
);
528 r
= search_and_fopen("afilewhichdoesnotexistiguess", "r", NULL
, dirs
, &f
);
534 r
= search_and_fopen(basename(name
), "r", NULL
, dirs
, &f
);
538 static void test_search_and_fopen_nulstr(void) {
539 const char dirs
[] = "/tmp/foo/bar\0/tmp\0";
541 _cleanup_(unlink_tempfilep
) char name
[] = "/tmp/test-search_and_fopen.XXXXXX";
545 fd
= mkostemp_safe(name
);
549 r
= search_and_fopen_nulstr(basename(name
), "r", NULL
, dirs
, &f
);
553 r
= search_and_fopen_nulstr(name
, "r", NULL
, dirs
, &f
);
557 r
= search_and_fopen_nulstr("/a/file/which/does/not/exist/i/guess", "r", NULL
, dirs
, &f
);
559 r
= search_and_fopen_nulstr("afilewhichdoesnotexistiguess", "r", NULL
, dirs
, &f
);
565 r
= search_and_fopen_nulstr(basename(name
), "r", NULL
, dirs
, &f
);
569 static void test_writing_tmpfile(void) {
570 _cleanup_(unlink_tempfilep
) char name
[] = "/tmp/test-systemd_writing_tmpfile.XXXXXX";
571 _cleanup_free_
char *contents
= NULL
;
573 _cleanup_close_
int fd
= -1;
577 iov
[0] = IOVEC_MAKE_STRING("abc\n");
578 iov
[1] = IOVEC_MAKE_STRING(ALPHANUMERICAL
"\n");
579 iov
[2] = IOVEC_MAKE_STRING("");
581 fd
= mkostemp_safe(name
);
582 printf("tmpfile: %s", name
);
584 r
= writev(fd
, iov
, 3);
587 r
= read_full_file(name
, &contents
, &size
);
589 printf("contents: %s", contents
);
590 assert_se(streq(contents
, "abc\n" ALPHANUMERICAL
"\n"));
593 static void test_tempfn(void) {
594 char *ret
= NULL
, *p
;
596 assert_se(tempfn_xxxxxx("/foo/bar/waldo", NULL
, &ret
) >= 0);
597 assert_se(streq_ptr(ret
, "/foo/bar/.#waldoXXXXXX"));
600 assert_se(tempfn_xxxxxx("/foo/bar/waldo", "[miau]", &ret
) >= 0);
601 assert_se(streq_ptr(ret
, "/foo/bar/.#[miau]waldoXXXXXX"));
604 assert_se(tempfn_random("/foo/bar/waldo", NULL
, &ret
) >= 0);
605 assert_se(p
= startswith(ret
, "/foo/bar/.#waldo"));
606 assert_se(strlen(p
) == 16);
607 assert_se(in_charset(p
, "0123456789abcdef"));
610 assert_se(tempfn_random("/foo/bar/waldo", "[wuff]", &ret
) >= 0);
611 assert_se(p
= startswith(ret
, "/foo/bar/.#[wuff]waldo"));
612 assert_se(strlen(p
) == 16);
613 assert_se(in_charset(p
, "0123456789abcdef"));
616 assert_se(tempfn_random_child("/foo/bar/waldo", NULL
, &ret
) >= 0);
617 assert_se(p
= startswith(ret
, "/foo/bar/waldo/.#"));
618 assert_se(strlen(p
) == 16);
619 assert_se(in_charset(p
, "0123456789abcdef"));
622 assert_se(tempfn_random_child("/foo/bar/waldo", "[kikiriki]", &ret
) >= 0);
623 assert_se(p
= startswith(ret
, "/foo/bar/waldo/.#[kikiriki]"));
624 assert_se(strlen(p
) == 16);
625 assert_se(in_charset(p
, "0123456789abcdef"));
629 static const char chars
[] =
632 #pragma GCC diagnostic push
633 #pragma GCC diagnostic ignored "-Wtype-limits"
635 static void test_fgetc(void) {
636 _cleanup_fclose_
FILE *f
= NULL
;
639 f
= fmemopen_unlocked((void*) chars
, sizeof(chars
), "re");
642 for (unsigned i
= 0; i
< sizeof(chars
); i
++) {
643 assert_se(safe_fgetc(f
, &c
) == 1);
644 assert_se(c
== chars
[i
]);
646 /* EOF is -1, and hence we can't push value 255 in this way if char is signed */
647 assert_se(ungetc(c
, f
) != EOF
|| c
== EOF
);
648 assert_se(c
== EOF
|| safe_fgetc(f
, &c
) == 1);
649 assert_se(c
== chars
[i
]);
651 /* But it works when we push it properly cast */
652 assert_se(ungetc((unsigned char) c
, f
) != EOF
);
653 assert_se(safe_fgetc(f
, &c
) == 1);
654 assert_se(c
== chars
[i
]);
657 assert_se(safe_fgetc(f
, &c
) == 0);
660 #pragma GCC diagnostic pop
662 static const char buffer
[] =
664 "루Non-ascii chars: ąę„”\n"
667 "now the same with a NUL\n\0"
669 "and even more\r\n\0"
670 "and yet even more\n\r\0"
671 "With newlines, and a NUL byte\0"
675 "and a very long line that is supposed to be truncated, because it is so long\n";
677 static void test_read_line_one_file(FILE *f
) {
678 _cleanup_free_
char *line
= NULL
;
680 assert_se(read_line(f
, (size_t) -1, &line
) == 15 && streq(line
, "Some test data"));
683 assert_se(read_line(f
, (size_t) -1, &line
) > 0 && streq(line
, "루Non-ascii chars: ąę„”"));
686 assert_se(read_line(f
, (size_t) -1, &line
) == 13 && streq(line
, "terminators"));
689 assert_se(read_line(f
, (size_t) -1, &line
) == 15 && streq(line
, "and even more"));
692 assert_se(read_line(f
, (size_t) -1, &line
) == 25 && streq(line
, "now the same with a NUL"));
695 assert_se(read_line(f
, (size_t) -1, &line
) == 10 && streq(line
, "and more"));
698 assert_se(read_line(f
, (size_t) -1, &line
) == 16 && streq(line
, "and even more"));
701 assert_se(read_line(f
, (size_t) -1, &line
) == 20 && streq(line
, "and yet even more"));
704 assert_se(read_line(f
, 1024, &line
) == 30 && streq(line
, "With newlines, and a NUL byte"));
707 assert_se(read_line(f
, 1024, &line
) == 1 && streq(line
, ""));
710 assert_se(read_line(f
, 1024, &line
) == 14 && streq(line
, "an empty line"));
713 assert_se(read_line(f
, (size_t) -1, NULL
) == 16);
715 assert_se(read_line(f
, 16, &line
) == -ENOBUFS
);
718 /* read_line() stopped when it hit the limit, that means when we continue reading we'll read at the first
719 * character after the previous limit. Let's make use of that to continue our test. */
720 assert_se(read_line(f
, 1024, &line
) == 62 && streq(line
, "line that is supposed to be truncated, because it is so long"));
723 assert_se(read_line(f
, 1024, &line
) == 0 && streq(line
, ""));
726 static void test_read_line(void) {
727 _cleanup_fclose_
FILE *f
= NULL
;
729 f
= fmemopen_unlocked((void*) buffer
, sizeof(buffer
), "re");
732 test_read_line_one_file(f
);
735 static void test_read_line2(void) {
736 _cleanup_(unlink_tempfilep
) char name
[] = "/tmp/test-fileio.XXXXXX";
738 _cleanup_fclose_
FILE *f
= NULL
;
740 fd
= mkostemp_safe(name
);
742 assert_se((size_t) write(fd
, buffer
, sizeof(buffer
)) == sizeof(buffer
));
744 assert_se(lseek(fd
, 0, SEEK_SET
) == 0);
745 assert_se(f
= fdopen(fd
, "r"));
747 test_read_line_one_file(f
);
750 static void test_read_line3(void) {
751 _cleanup_fclose_
FILE *f
= NULL
;
752 _cleanup_free_
char *line
= NULL
;
755 f
= fopen("/proc/cmdline", "re");
756 if (!f
&& IN_SET(errno
, ENOENT
, EPERM
))
760 r
= read_line(f
, LINE_MAX
, &line
);
763 assert_se(line
&& isempty(line
));
765 assert_se((size_t) r
== strlen(line
) + 1);
766 assert_se(read_line(f
, LINE_MAX
, NULL
) == 0);
769 static void test_read_line4(void) {
770 static const struct {
774 /* Each of these will be followed by EOF and should generate the one same single string */
790 for (i
= 0; i
< ELEMENTSOF(eof_endings
); i
++) {
791 _cleanup_fclose_
FILE *f
= NULL
;
792 _cleanup_free_
char *s
= NULL
;
794 assert_se(f
= fmemopen_unlocked((void*) eof_endings
[i
].string
, eof_endings
[i
].length
, "r"));
796 r
= read_line(f
, (size_t) -1, &s
);
797 assert_se((size_t) r
== eof_endings
[i
].length
);
798 assert_se(streq_ptr(s
, "foo"));
800 assert_se(read_line(f
, (size_t) -1, NULL
) == 0); /* Ensure we hit EOF */
804 static void test_read_nul_string(void) {
805 static const char test
[] = "string nr. 1\0"
807 "\377empty string follows\0"
809 "final string\n is empty\0"
812 _cleanup_fclose_
FILE *f
= NULL
;
813 _cleanup_free_
char *s
= NULL
;
815 assert_se(f
= fmemopen_unlocked((void*) test
, sizeof(test
)-1, "r"));
817 assert_se(read_nul_string(f
, LONG_LINE_MAX
, &s
) == 13 && streq_ptr(s
, "string nr. 1"));
820 assert_se(read_nul_string(f
, LONG_LINE_MAX
, &s
) == 14 && streq_ptr(s
, "string nr. 2\n"));
823 assert_se(read_nul_string(f
, LONG_LINE_MAX
, &s
) == 22 && streq_ptr(s
, "\377empty string follows"));
826 assert_se(read_nul_string(f
, LONG_LINE_MAX
, &s
) == 1 && streq_ptr(s
, ""));
829 assert_se(read_nul_string(f
, LONG_LINE_MAX
, &s
) == 23 && streq_ptr(s
, "final string\n is empty"));
832 assert_se(read_nul_string(f
, LONG_LINE_MAX
, &s
) == 1 && streq_ptr(s
, ""));
835 assert_se(read_nul_string(f
, LONG_LINE_MAX
, &s
) == 0 && streq_ptr(s
, ""));
838 int main(int argc
, char *argv
[]) {
839 test_setup_logging(LOG_DEBUG
);
841 test_parse_env_file();
842 test_parse_multiline_env_file();
843 test_merge_env_file();
844 test_merge_env_file_invalid();
845 test_executable_is_script();
848 test_write_string_stream();
849 test_write_string_file();
850 test_write_string_file_no_create();
851 test_write_string_file_verify();
852 test_load_env_file_pairs();
853 test_search_and_fopen();
854 test_search_and_fopen_nulstr();
855 test_writing_tmpfile();
862 test_read_nul_string();