]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-fileio.c
Merge pull request #11201 from keszybz/more-news
[thirdparty/systemd.git] / src / test / test-fileio.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <fcntl.h>
4 #include <stdio.h>
5 #include <unistd.h>
6
7 #include "alloc-util.h"
8 #include "ctype.h"
9 #include "env-file.h"
10 #include "env-util.h"
11 #include "fd-util.h"
12 #include "fileio.h"
13 #include "fs-util.h"
14 #include "io-util.h"
15 #include "parse-util.h"
16 #include "process-util.h"
17 #include "string-util.h"
18 #include "strv.h"
19 #include "tests.h"
20 #include "tmpfile-util.h"
21 #include "util.h"
22
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";
27 FILE *f;
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 _cleanup_strv_free_ char **a = NULL, **b = NULL;
31 char **i;
32 unsigned k;
33 int r;
34
35 assert_se(fmkostemp_safe(t, "w", &f) == 0);
36 fputs("one=BAR \n"
37 "# comment\n"
38 " # comment \n"
39 " ; comment \n"
40 " two = bar \n"
41 "invalid line\n"
42 "invalid line #comment\n"
43 "three = \"333\n"
44 "xxxx\"\n"
45 "four = \'44\\\"44\'\n"
46 "five = \'55\\\'55\' \"FIVE\" cinco \n"
47 "six = seis sechs\\\n"
48 " sis\n"
49 "seven=\"sevenval\" #nocomment\n"
50 "eight=eightval #nocomment\n"
51 "export nine=nineval\n"
52 "ten=ignored\n"
53 "ten=ignored\n"
54 "ten=", f);
55
56 fflush(f);
57 fclose(f);
58
59 r = load_env_file(NULL, t, &a);
60 assert_se(r >= 0);
61
62 STRV_FOREACH(i, a)
63 log_info("Got: <%s>", *i);
64
65 assert_se(streq_ptr(a[0], "one=BAR"));
66 assert_se(streq_ptr(a[1], "two=bar"));
67 assert_se(streq_ptr(a[2], "three=333\nxxxx"));
68 assert_se(streq_ptr(a[3], "four=44\"44"));
69 assert_se(streq_ptr(a[4], "five=55\'55FIVEcinco"));
70 assert_se(streq_ptr(a[5], "six=seis sechs sis"));
71 assert_se(streq_ptr(a[6], "seven=sevenval#nocomment"));
72 assert_se(streq_ptr(a[7], "eight=eightval #nocomment"));
73 assert_se(streq_ptr(a[8], "export nine=nineval"));
74 assert_se(streq_ptr(a[9], "ten="));
75 assert_se(a[10] == NULL);
76
77 strv_env_clean(a);
78
79 k = 0;
80 STRV_FOREACH(i, b) {
81 log_info("Got2: <%s>", *i);
82 assert_se(streq(*i, a[k++]));
83 }
84
85 r = parse_env_file(
86 NULL, t,
87 "one", &one,
88 "two", &two,
89 "three", &three,
90 "four", &four,
91 "five", &five,
92 "six", &six,
93 "seven", &seven,
94 "eight", &eight,
95 "export nine", &nine,
96 "ten", &ten);
97
98 assert_se(r >= 0);
99
100 log_info("one=[%s]", strna(one));
101 log_info("two=[%s]", strna(two));
102 log_info("three=[%s]", strna(three));
103 log_info("four=[%s]", strna(four));
104 log_info("five=[%s]", strna(five));
105 log_info("six=[%s]", strna(six));
106 log_info("seven=[%s]", strna(seven));
107 log_info("eight=[%s]", strna(eight));
108 log_info("export nine=[%s]", strna(nine));
109 log_info("ten=[%s]", strna(nine));
110
111 assert_se(streq(one, "BAR"));
112 assert_se(streq(two, "bar"));
113 assert_se(streq(three, "333\nxxxx"));
114 assert_se(streq(four, "44\"44"));
115 assert_se(streq(five, "55\'55FIVEcinco"));
116 assert_se(streq(six, "seis sechs sis"));
117 assert_se(streq(seven, "sevenval#nocomment"));
118 assert_se(streq(eight, "eightval #nocomment"));
119 assert_se(streq(nine, "nineval"));
120 assert_se(ten == NULL);
121
122 {
123 /* prepare a temporary file to write the environment to */
124 _cleanup_close_ int fd = mkostemp_safe(p);
125 assert_se(fd >= 0);
126 }
127
128 r = write_env_file(p, a);
129 assert_se(r >= 0);
130
131 r = load_env_file(NULL, p, &b);
132 assert_se(r >= 0);
133 }
134
135 static void test_parse_multiline_env_file(void) {
136 _cleanup_(unlink_tempfilep) char
137 t[] = "/tmp/test-fileio-in-XXXXXX",
138 p[] = "/tmp/test-fileio-out-XXXXXX";
139 FILE *f;
140 _cleanup_strv_free_ char **a = NULL, **b = NULL;
141 char **i;
142 int r;
143
144 assert_se(fmkostemp_safe(t, "w", &f) == 0);
145 fputs("one=BAR\\\n"
146 " VAR\\\n"
147 "\tGAR\n"
148 "#comment\n"
149 "two=\"bar\\\n"
150 " var\\\n"
151 "\tgar\"\n"
152 "#comment\n"
153 "tri=\"bar \\\n"
154 " var \\\n"
155 "\tgar \"\n", f);
156
157 fflush(f);
158 fclose(f);
159
160 r = load_env_file(NULL, t, &a);
161 assert_se(r >= 0);
162
163 STRV_FOREACH(i, a)
164 log_info("Got: <%s>", *i);
165
166 assert_se(streq_ptr(a[0], "one=BAR VAR\tGAR"));
167 assert_se(streq_ptr(a[1], "two=bar var\tgar"));
168 assert_se(streq_ptr(a[2], "tri=bar var \tgar "));
169 assert_se(a[3] == NULL);
170
171 {
172 _cleanup_close_ int fd = mkostemp_safe(p);
173 assert_se(fd >= 0);
174 }
175
176 r = write_env_file(p, a);
177 assert_se(r >= 0);
178
179 r = load_env_file(NULL, p, &b);
180 assert_se(r >= 0);
181 }
182
183 static void test_merge_env_file(void) {
184 _cleanup_(unlink_tempfilep) char t[] = "/tmp/test-fileio-XXXXXX";
185 _cleanup_fclose_ FILE *f = NULL;
186 _cleanup_strv_free_ char **a = NULL;
187 char **i;
188 int r;
189
190 assert_se(fmkostemp_safe(t, "w", &f) == 0);
191 log_info("/* %s (%s) */", __func__, t);
192
193 r = write_string_stream(f,
194 "one=1 \n"
195 "twelve=${one}2\n"
196 "twentyone=2${one}\n"
197 "one=2\n"
198 "twentytwo=2${one}\n"
199 "xxx_minus_three=$xxx - 3\n"
200 "xxx=0x$one$one$one\n"
201 "yyy=${one:-fallback}\n"
202 "zzz=${one:+replacement}\n"
203 "zzzz=${foobar:-${nothing}}\n"
204 "zzzzz=${nothing:+${nothing}}\n"
205 , WRITE_STRING_FILE_AVOID_NEWLINE);
206 assert(r >= 0);
207
208 r = merge_env_file(&a, NULL, t);
209 assert_se(r >= 0);
210 strv_sort(a);
211
212 STRV_FOREACH(i, a)
213 log_info("Got: <%s>", *i);
214
215 assert_se(streq(a[0], "one=2"));
216 assert_se(streq(a[1], "twelve=12"));
217 assert_se(streq(a[2], "twentyone=21"));
218 assert_se(streq(a[3], "twentytwo=22"));
219 assert_se(streq(a[4], "xxx=0x222"));
220 assert_se(streq(a[5], "xxx_minus_three= - 3"));
221 assert_se(streq(a[6], "yyy=2"));
222 assert_se(streq(a[7], "zzz=replacement"));
223 assert_se(streq(a[8], "zzzz="));
224 assert_se(streq(a[9], "zzzzz="));
225 assert_se(a[10] == NULL);
226
227 r = merge_env_file(&a, NULL, t);
228 assert_se(r >= 0);
229 strv_sort(a);
230
231 STRV_FOREACH(i, a)
232 log_info("Got2: <%s>", *i);
233
234 assert_se(streq(a[0], "one=2"));
235 assert_se(streq(a[1], "twelve=12"));
236 assert_se(streq(a[2], "twentyone=21"));
237 assert_se(streq(a[3], "twentytwo=22"));
238 assert_se(streq(a[4], "xxx=0x222"));
239 assert_se(streq(a[5], "xxx_minus_three=0x222 - 3"));
240 assert_se(streq(a[6], "yyy=2"));
241 assert_se(streq(a[7], "zzz=replacement"));
242 assert_se(streq(a[8], "zzzz="));
243 assert_se(streq(a[9], "zzzzz="));
244 assert_se(a[10] == NULL);
245 }
246
247 static void test_merge_env_file_invalid(void) {
248 _cleanup_(unlink_tempfilep) char t[] = "/tmp/test-fileio-XXXXXX";
249 _cleanup_fclose_ FILE *f = NULL;
250 _cleanup_strv_free_ char **a = NULL;
251 char **i;
252 int r;
253
254 assert_se(fmkostemp_safe(t, "w", &f) == 0);
255 log_info("/* %s (%s) */", __func__, t);
256
257 r = write_string_stream(f,
258 "unset one \n"
259 "unset one= \n"
260 "unset one=1 \n"
261 "one \n"
262 "one = \n"
263 "one two =\n"
264 "\x20two=\n"
265 "#comment=comment\n"
266 ";comment2=comment2\n"
267 "#\n"
268 "\n\n" /* empty line */
269 , WRITE_STRING_FILE_AVOID_NEWLINE);
270 assert(r >= 0);
271
272 r = merge_env_file(&a, NULL, t);
273 assert_se(r >= 0);
274
275 STRV_FOREACH(i, a)
276 log_info("Got: <%s>", *i);
277
278 assert_se(strv_isempty(a));
279 }
280
281 static void test_executable_is_script(void) {
282 _cleanup_(unlink_tempfilep) char t[] = "/tmp/test-fileio-XXXXXX";
283 _cleanup_fclose_ FILE *f = NULL;
284 char *command;
285 int r;
286
287 assert_se(fmkostemp_safe(t, "w", &f) == 0);
288 fputs("#! /bin/script -a -b \ngoo goo", f);
289 fflush(f);
290
291 r = executable_is_script(t, &command);
292 assert_se(r > 0);
293 assert_se(streq(command, "/bin/script"));
294 free(command);
295
296 r = executable_is_script("/bin/sh", &command);
297 assert_se(r == 0);
298
299 r = executable_is_script("/usr/bin/yum", &command);
300 assert_se(r > 0 || r == -ENOENT);
301 if (r > 0) {
302 assert_se(startswith(command, "/"));
303 free(command);
304 }
305 }
306
307 static void test_status_field(void) {
308 _cleanup_free_ char *t = NULL, *p = NULL, *s = NULL, *z = NULL;
309 unsigned long long total = 0, buffers = 0;
310 int r;
311
312 assert_se(get_proc_field("/proc/self/status", "Threads", WHITESPACE, &t) == 0);
313 puts(t);
314 assert_se(streq(t, "1"));
315
316 r = get_proc_field("/proc/meminfo", "MemTotal", WHITESPACE, &p);
317 if (r != -ENOENT) {
318 assert_se(r == 0);
319 puts(p);
320 assert_se(safe_atollu(p, &total) == 0);
321 }
322
323 r = get_proc_field("/proc/meminfo", "Buffers", WHITESPACE, &s);
324 if (r != -ENOENT) {
325 assert_se(r == 0);
326 puts(s);
327 assert_se(safe_atollu(s, &buffers) == 0);
328 }
329
330 if (p)
331 assert_se(buffers < total);
332
333 /* Seccomp should be a good test for field full of zeros. */
334 r = get_proc_field("/proc/meminfo", "Seccomp", WHITESPACE, &z);
335 if (r != -ENOENT) {
336 assert_se(r == 0);
337 puts(z);
338 assert_se(safe_atollu(z, &buffers) == 0);
339 }
340 }
341
342 static void test_capeff(void) {
343 int pid, p;
344
345 for (pid = 0; pid < 2; pid++) {
346 _cleanup_free_ char *capeff = NULL;
347 int r;
348
349 r = get_process_capeff(0, &capeff);
350 log_info("capeff: '%s' (r=%d)", capeff, r);
351
352 if (IN_SET(r, -ENOENT, -EPERM))
353 return;
354
355 assert_se(r == 0);
356 assert_se(*capeff);
357 p = capeff[strspn(capeff, HEXDIGITS)];
358 assert_se(!p || isspace(p));
359 }
360 }
361
362 static void test_write_string_stream(void) {
363 _cleanup_(unlink_tempfilep) char fn[] = "/tmp/test-write_string_stream-XXXXXX";
364 _cleanup_fclose_ FILE *f = NULL;
365 int fd;
366 char buf[64];
367
368 fd = mkostemp_safe(fn);
369 assert_se(fd >= 0);
370
371 f = fdopen(fd, "r");
372 assert_se(f);
373 assert_se(write_string_stream(f, "boohoo", 0) < 0);
374 f = safe_fclose(f);
375
376 f = fopen(fn, "r+");
377 assert_se(f);
378
379 assert_se(write_string_stream(f, "boohoo", 0) == 0);
380 rewind(f);
381
382 assert_se(fgets(buf, sizeof(buf), f));
383 assert_se(streq(buf, "boohoo\n"));
384 f = safe_fclose(f);
385
386 f = fopen(fn, "w+");
387 assert_se(f);
388
389 assert_se(write_string_stream(f, "boohoo", WRITE_STRING_FILE_AVOID_NEWLINE) == 0);
390 rewind(f);
391
392 assert_se(fgets(buf, sizeof(buf), f));
393 printf(">%s<", buf);
394 assert_se(streq(buf, "boohoo"));
395 }
396
397 static void test_write_string_file(void) {
398 _cleanup_(unlink_tempfilep) char fn[] = "/tmp/test-write_string_file-XXXXXX";
399 char buf[64] = {};
400 _cleanup_close_ int fd;
401
402 fd = mkostemp_safe(fn);
403 assert_se(fd >= 0);
404
405 assert_se(write_string_file(fn, "boohoo", WRITE_STRING_FILE_CREATE) == 0);
406
407 assert_se(read(fd, buf, sizeof(buf)) == 7);
408 assert_se(streq(buf, "boohoo\n"));
409 }
410
411 static void test_write_string_file_no_create(void) {
412 _cleanup_(unlink_tempfilep) char fn[] = "/tmp/test-write_string_file_no_create-XXXXXX";
413 _cleanup_close_ int fd;
414 char buf[64] = {0};
415
416 fd = mkostemp_safe(fn);
417 assert_se(fd >= 0);
418
419 assert_se(write_string_file("/a/file/which/does/not/exists/i/guess", "boohoo", 0) < 0);
420 assert_se(write_string_file(fn, "boohoo", 0) == 0);
421
422 assert_se(read(fd, buf, sizeof(buf)) == STRLEN("boohoo\n"));
423 assert_se(streq(buf, "boohoo\n"));
424 }
425
426 static void test_write_string_file_verify(void) {
427 _cleanup_free_ char *buf = NULL, *buf2 = NULL;
428 int r;
429
430 assert_se(read_one_line_file("/proc/cmdline", &buf) >= 0);
431 assert_se(buf2 = strjoin(buf, "\n"));
432
433 r = write_string_file("/proc/cmdline", buf, 0);
434 assert_se(IN_SET(r, -EACCES, -EIO));
435 r = write_string_file("/proc/cmdline", buf2, 0);
436 assert_se(IN_SET(r, -EACCES, -EIO));
437
438 assert_se(write_string_file("/proc/cmdline", buf, WRITE_STRING_FILE_VERIFY_ON_FAILURE) == 0);
439 assert_se(write_string_file("/proc/cmdline", buf2, WRITE_STRING_FILE_VERIFY_ON_FAILURE) == 0);
440
441 r = write_string_file("/proc/cmdline", buf, WRITE_STRING_FILE_VERIFY_ON_FAILURE|WRITE_STRING_FILE_AVOID_NEWLINE);
442 assert_se(IN_SET(r, -EACCES, -EIO));
443 assert_se(write_string_file("/proc/cmdline", buf2, WRITE_STRING_FILE_VERIFY_ON_FAILURE|WRITE_STRING_FILE_AVOID_NEWLINE) == 0);
444 }
445
446 static void test_load_env_file_pairs(void) {
447 _cleanup_(unlink_tempfilep) char fn[] = "/tmp/test-load_env_file_pairs-XXXXXX";
448 int fd, r;
449 _cleanup_fclose_ FILE *f = NULL;
450 _cleanup_strv_free_ char **l = NULL;
451 char **k, **v;
452
453 fd = mkostemp_safe(fn);
454 assert_se(fd >= 0);
455
456 r = write_string_file(fn,
457 "NAME=\"Arch Linux\"\n"
458 "ID=arch\n"
459 "PRETTY_NAME=\"Arch Linux\"\n"
460 "ANSI_COLOR=\"0;36\"\n"
461 "HOME_URL=\"https://www.archlinux.org/\"\n"
462 "SUPPORT_URL=\"https://bbs.archlinux.org/\"\n"
463 "BUG_REPORT_URL=\"https://bugs.archlinux.org/\"\n",
464 WRITE_STRING_FILE_CREATE);
465 assert_se(r == 0);
466
467 f = fdopen(fd, "r");
468 assert_se(f);
469
470 r = load_env_file_pairs(f, fn, &l);
471 assert_se(r >= 0);
472
473 assert_se(strv_length(l) == 14);
474 STRV_FOREACH_PAIR(k, v, l) {
475 assert_se(STR_IN_SET(*k, "NAME", "ID", "PRETTY_NAME", "ANSI_COLOR", "HOME_URL", "SUPPORT_URL", "BUG_REPORT_URL"));
476 printf("%s=%s\n", *k, *v);
477 if (streq(*k, "NAME")) assert_se(streq(*v, "Arch Linux"));
478 if (streq(*k, "ID")) assert_se(streq(*v, "arch"));
479 if (streq(*k, "PRETTY_NAME")) assert_se(streq(*v, "Arch Linux"));
480 if (streq(*k, "ANSI_COLOR")) assert_se(streq(*v, "0;36"));
481 if (streq(*k, "HOME_URL")) assert_se(streq(*v, "https://www.archlinux.org/"));
482 if (streq(*k, "SUPPORT_URL")) assert_se(streq(*v, "https://bbs.archlinux.org/"));
483 if (streq(*k, "BUG_REPORT_URL")) assert_se(streq(*v, "https://bugs.archlinux.org/"));
484 }
485 }
486
487 static void test_search_and_fopen(void) {
488 const char *dirs[] = {"/tmp/foo/bar", "/tmp", NULL};
489
490 char name[] = "/tmp/test-search_and_fopen.XXXXXX";
491 int fd, r;
492 FILE *f;
493
494 fd = mkostemp_safe(name);
495 assert_se(fd >= 0);
496 close(fd);
497
498 r = search_and_fopen(basename(name), "r", NULL, dirs, &f);
499 assert_se(r >= 0);
500 fclose(f);
501
502 r = search_and_fopen(name, "r", NULL, dirs, &f);
503 assert_se(r >= 0);
504 fclose(f);
505
506 r = search_and_fopen(basename(name), "r", "/", dirs, &f);
507 assert_se(r >= 0);
508 fclose(f);
509
510 r = search_and_fopen("/a/file/which/does/not/exist/i/guess", "r", NULL, dirs, &f);
511 assert_se(r < 0);
512 r = search_and_fopen("afilewhichdoesnotexistiguess", "r", NULL, dirs, &f);
513 assert_se(r < 0);
514
515 r = unlink(name);
516 assert_se(r == 0);
517
518 r = search_and_fopen(basename(name), "r", NULL, dirs, &f);
519 assert_se(r < 0);
520 }
521
522 static void test_search_and_fopen_nulstr(void) {
523 const char dirs[] = "/tmp/foo/bar\0/tmp\0";
524
525 _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-search_and_fopen.XXXXXX";
526 int fd, r;
527 FILE *f;
528
529 fd = mkostemp_safe(name);
530 assert_se(fd >= 0);
531 close(fd);
532
533 r = search_and_fopen_nulstr(basename(name), "r", NULL, dirs, &f);
534 assert_se(r >= 0);
535 fclose(f);
536
537 r = search_and_fopen_nulstr(name, "r", NULL, dirs, &f);
538 assert_se(r >= 0);
539 fclose(f);
540
541 r = search_and_fopen_nulstr("/a/file/which/does/not/exist/i/guess", "r", NULL, dirs, &f);
542 assert_se(r < 0);
543 r = search_and_fopen_nulstr("afilewhichdoesnotexistiguess", "r", NULL, dirs, &f);
544 assert_se(r < 0);
545
546 r = unlink(name);
547 assert_se(r == 0);
548
549 r = search_and_fopen_nulstr(basename(name), "r", NULL, dirs, &f);
550 assert_se(r < 0);
551 }
552
553 static void test_writing_tmpfile(void) {
554 _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-systemd_writing_tmpfile.XXXXXX";
555 _cleanup_free_ char *contents = NULL;
556 size_t size;
557 _cleanup_close_ int fd = -1;
558 struct iovec iov[3];
559 int r;
560
561 iov[0] = IOVEC_MAKE_STRING("abc\n");
562 iov[1] = IOVEC_MAKE_STRING(ALPHANUMERICAL "\n");
563 iov[2] = IOVEC_MAKE_STRING("");
564
565 fd = mkostemp_safe(name);
566 printf("tmpfile: %s", name);
567
568 r = writev(fd, iov, 3);
569 assert_se(r >= 0);
570
571 r = read_full_file(name, &contents, &size);
572 assert_se(r == 0);
573 printf("contents: %s", contents);
574 assert_se(streq(contents, "abc\n" ALPHANUMERICAL "\n"));
575 }
576
577 static void test_tempfn(void) {
578 char *ret = NULL, *p;
579
580 assert_se(tempfn_xxxxxx("/foo/bar/waldo", NULL, &ret) >= 0);
581 assert_se(streq_ptr(ret, "/foo/bar/.#waldoXXXXXX"));
582 free(ret);
583
584 assert_se(tempfn_xxxxxx("/foo/bar/waldo", "[miau]", &ret) >= 0);
585 assert_se(streq_ptr(ret, "/foo/bar/.#[miau]waldoXXXXXX"));
586 free(ret);
587
588 assert_se(tempfn_random("/foo/bar/waldo", NULL, &ret) >= 0);
589 assert_se(p = startswith(ret, "/foo/bar/.#waldo"));
590 assert_se(strlen(p) == 16);
591 assert_se(in_charset(p, "0123456789abcdef"));
592 free(ret);
593
594 assert_se(tempfn_random("/foo/bar/waldo", "[wuff]", &ret) >= 0);
595 assert_se(p = startswith(ret, "/foo/bar/.#[wuff]waldo"));
596 assert_se(strlen(p) == 16);
597 assert_se(in_charset(p, "0123456789abcdef"));
598 free(ret);
599
600 assert_se(tempfn_random_child("/foo/bar/waldo", NULL, &ret) >= 0);
601 assert_se(p = startswith(ret, "/foo/bar/waldo/.#"));
602 assert_se(strlen(p) == 16);
603 assert_se(in_charset(p, "0123456789abcdef"));
604 free(ret);
605
606 assert_se(tempfn_random_child("/foo/bar/waldo", "[kikiriki]", &ret) >= 0);
607 assert_se(p = startswith(ret, "/foo/bar/waldo/.#[kikiriki]"));
608 assert_se(strlen(p) == 16);
609 assert_se(in_charset(p, "0123456789abcdef"));
610 free(ret);
611 }
612
613 static const char chars[] =
614 "Aąę„”\n루";
615
616 static void test_fgetc(void) {
617 _cleanup_fclose_ FILE *f = NULL;
618 char c;
619
620 f = fmemopen((void*) chars, sizeof(chars), "re");
621 assert_se(f);
622
623 for (unsigned i = 0; i < sizeof(chars); i++) {
624 assert_se(safe_fgetc(f, &c) == 1);
625 assert_se(c == chars[i]);
626
627 assert_se(ungetc(c, f) != EOF);
628 assert_se(safe_fgetc(f, &c) == 1);
629 assert_se(c == chars[i]);
630
631 /* Check that ungetc doesn't care about unsigned char vs signed char */
632 assert_se(ungetc((unsigned char) c, f) != EOF);
633 assert_se(safe_fgetc(f, &c) == 1);
634 assert_se(c == chars[i]);
635 }
636
637 assert_se(safe_fgetc(f, &c) == 0);
638 }
639
640 static const char buffer[] =
641 "Some test data\n"
642 "루Non-ascii chars: ąę„”\n"
643 "terminators\r\n"
644 "and even more\n\r"
645 "now the same with a NUL\n\0"
646 "and more\r\0"
647 "and even more\r\n\0"
648 "and yet even more\n\r\0"
649 "With newlines, and a NUL byte\0"
650 "\n"
651 "an empty line\n"
652 "an ignored line\n"
653 "and a very long line that is supposed to be truncated, because it is so long\n";
654
655 static void test_read_line_one_file(FILE *f) {
656 _cleanup_free_ char *line = NULL;
657
658 assert_se(read_line(f, (size_t) -1, &line) == 15 && streq(line, "Some test data"));
659 line = mfree(line);
660
661 assert_se(read_line(f, (size_t) -1, &line) > 0 && streq(line, "루Non-ascii chars: ąę„”"));
662 line = mfree(line);
663
664 assert_se(read_line(f, (size_t) -1, &line) == 13 && streq(line, "terminators"));
665 line = mfree(line);
666
667 assert_se(read_line(f, (size_t) -1, &line) == 15 && streq(line, "and even more"));
668 line = mfree(line);
669
670 assert_se(read_line(f, (size_t) -1, &line) == 25 && streq(line, "now the same with a NUL"));
671 line = mfree(line);
672
673 assert_se(read_line(f, (size_t) -1, &line) == 10 && streq(line, "and more"));
674 line = mfree(line);
675
676 assert_se(read_line(f, (size_t) -1, &line) == 16 && streq(line, "and even more"));
677 line = mfree(line);
678
679 assert_se(read_line(f, (size_t) -1, &line) == 20 && streq(line, "and yet even more"));
680 line = mfree(line);
681
682 assert_se(read_line(f, 1024, &line) == 30 && streq(line, "With newlines, and a NUL byte"));
683 line = mfree(line);
684
685 assert_se(read_line(f, 1024, &line) == 1 && streq(line, ""));
686 line = mfree(line);
687
688 assert_se(read_line(f, 1024, &line) == 14 && streq(line, "an empty line"));
689 line = mfree(line);
690
691 assert_se(read_line(f, (size_t) -1, NULL) == 16);
692
693 assert_se(read_line(f, 16, &line) == -ENOBUFS);
694 line = mfree(line);
695
696 /* read_line() stopped when it hit the limit, that means when we continue reading we'll read at the first
697 * character after the previous limit. Let's make use of tha to continue our test. */
698 assert_se(read_line(f, 1024, &line) == 62 && streq(line, "line that is supposed to be truncated, because it is so long"));
699 line = mfree(line);
700
701 assert_se(read_line(f, 1024, &line) == 0 && streq(line, ""));
702 }
703
704 static void test_read_line(void) {
705 _cleanup_fclose_ FILE *f = NULL;
706
707 f = fmemopen((void*) buffer, sizeof(buffer), "re");
708 assert_se(f);
709
710 test_read_line_one_file(f);
711 }
712
713 static void test_read_line2(void) {
714 _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-fileio.XXXXXX";
715 int fd;
716 _cleanup_fclose_ FILE *f = NULL;
717
718 fd = mkostemp_safe(name);
719 assert_se(fd >= 0);
720 assert_se((size_t) write(fd, buffer, sizeof(buffer)) == sizeof(buffer));
721
722 assert_se(lseek(fd, 0, SEEK_SET) == 0);
723 assert_se(f = fdopen(fd, "r"));
724
725 test_read_line_one_file(f);
726 }
727
728 static void test_read_line3(void) {
729 _cleanup_fclose_ FILE *f = NULL;
730 _cleanup_free_ char *line = NULL;
731 int r;
732
733 f = fopen("/proc/cmdline", "re");
734 if (!f && IN_SET(errno, ENOENT, EPERM))
735 return;
736 assert_se(f);
737
738 r = read_line(f, LINE_MAX, &line);
739 assert_se((size_t) r == strlen(line) + 1);
740 assert_se(read_line(f, LINE_MAX, NULL) == 0);
741 }
742
743 static void test_read_line4(void) {
744 static const struct {
745 size_t length;
746 const char *string;
747 } eof_endings[] = {
748 /* Each of these will be followed by EOF and should generate the one same single string */
749 { 3, "foo" },
750 { 4, "foo\n" },
751 { 4, "foo\r" },
752 { 4, "foo\0" },
753 { 5, "foo\n\0" },
754 { 5, "foo\r\0" },
755 { 5, "foo\r\n" },
756 { 5, "foo\n\r" },
757 { 6, "foo\r\n\0" },
758 { 6, "foo\n\r\0" },
759 };
760
761 size_t i;
762 int r;
763
764 for (i = 0; i < ELEMENTSOF(eof_endings); i++) {
765 _cleanup_fclose_ FILE *f = NULL;
766 _cleanup_free_ char *s = NULL;
767
768 assert_se(f = fmemopen((void*) eof_endings[i].string, eof_endings[i].length, "r"));
769
770 r = read_line(f, (size_t) -1, &s);
771 assert_se((size_t) r == eof_endings[i].length);
772 assert_se(streq_ptr(s, "foo"));
773
774 assert_se(read_line(f, (size_t) -1, NULL) == 0); /* Ensure we hit EOF */
775 }
776 }
777
778 static void test_read_nul_string(void) {
779 static const char test[] = "string nr. 1\0"
780 "string nr. 2\n\0"
781 "empty string follows\0"
782 "\0"
783 "final string\n is empty\0"
784 "\0";
785
786 _cleanup_fclose_ FILE *f = NULL;
787 _cleanup_free_ char *s = NULL;
788
789 assert_se(f = fmemopen((void*) test, sizeof(test)-1, "r"));
790
791 assert_se(read_nul_string(f, LONG_LINE_MAX, &s) == 13 && streq_ptr(s, "string nr. 1"));
792 s = mfree(s);
793
794 assert_se(read_nul_string(f, LONG_LINE_MAX, &s) == 14 && streq_ptr(s, "string nr. 2\n"));
795 s = mfree(s);
796
797 assert_se(read_nul_string(f, LONG_LINE_MAX, &s) == 21 && streq_ptr(s, "empty string follows"));
798 s = mfree(s);
799
800 assert_se(read_nul_string(f, LONG_LINE_MAX, &s) == 1 && streq_ptr(s, ""));
801 s = mfree(s);
802
803 assert_se(read_nul_string(f, LONG_LINE_MAX, &s) == 23 && streq_ptr(s, "final string\n is empty"));
804 s = mfree(s);
805
806 assert_se(read_nul_string(f, LONG_LINE_MAX, &s) == 1 && streq_ptr(s, ""));
807 s = mfree(s);
808
809 assert_se(read_nul_string(f, LONG_LINE_MAX, &s) == 0 && streq_ptr(s, ""));
810 }
811
812 int main(int argc, char *argv[]) {
813 test_setup_logging(LOG_DEBUG);
814
815 test_parse_env_file();
816 test_parse_multiline_env_file();
817 test_merge_env_file();
818 test_merge_env_file_invalid();
819 test_executable_is_script();
820 test_status_field();
821 test_capeff();
822 test_write_string_stream();
823 test_write_string_file();
824 test_write_string_file_no_create();
825 test_write_string_file_verify();
826 test_load_env_file_pairs();
827 test_search_and_fopen();
828 test_search_and_fopen_nulstr();
829 test_writing_tmpfile();
830 test_tempfn();
831 test_fgetc();
832 test_read_line();
833 test_read_line2();
834 test_read_line3();
835 test_read_line4();
836 test_read_nul_string();
837
838 return 0;
839 }