]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-fileio.c
util-lib: follow shell syntax for escape in quotes
[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 *eleven = NULL, *twelve = NULL, *thirteen = NULL;
31 _cleanup_strv_free_ char **a = NULL, **b = NULL;
32 char **i;
33 unsigned k;
34 int r;
35
36 assert_se(fmkostemp_safe(t, "w", &f) == 0);
37 fputs("one=BAR \n"
38 "# comment\n"
39 " # comment \n"
40 " ; comment \n"
41 " two = bar \n"
42 "invalid line\n"
43 "invalid line #comment\n"
44 "three = \"333\n"
45 "xxxx\"\n"
46 "four = \'44\\\"44\'\n"
47 "five = \"55\\\"55\" \"FIVE\" cinco \n"
48 "six = seis sechs\\\n"
49 " sis\n"
50 "seven=\"sevenval\" #nocomment\n"
51 "eight=eightval #nocomment\n"
52 "export nine=nineval\n"
53 "ten=ignored\n"
54 "ten=ignored\n"
55 "ten=\n"
56 "eleven=\\value\n"
57 "twelve=\"\\value\"\n"
58 "thirteen='\\value'", f);
59
60 fflush(f);
61 fclose(f);
62
63 r = load_env_file(NULL, t, &a);
64 assert_se(r >= 0);
65
66 STRV_FOREACH(i, a)
67 log_info("Got: <%s>", *i);
68
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);
83
84 strv_env_clean(a);
85
86 k = 0;
87 STRV_FOREACH(i, b) {
88 log_info("Got2: <%s>", *i);
89 assert_se(streq(*i, a[k++]));
90 }
91
92 r = parse_env_file(
93 NULL, t,
94 "one", &one,
95 "two", &two,
96 "three", &three,
97 "four", &four,
98 "five", &five,
99 "six", &six,
100 "seven", &seven,
101 "eight", &eight,
102 "export nine", &nine,
103 "ten", &ten,
104 "eleven", &eleven,
105 "twelve", &twelve,
106 "thirteen", &thirteen);
107
108 assert_se(r >= 0);
109
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));
123
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"));
137
138 {
139 /* prepare a temporary file to write the environment to */
140 _cleanup_close_ int fd = mkostemp_safe(p);
141 assert_se(fd >= 0);
142 }
143
144 r = write_env_file(p, a);
145 assert_se(r >= 0);
146
147 r = load_env_file(NULL, p, &b);
148 assert_se(r >= 0);
149 }
150
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";
155 FILE *f;
156 _cleanup_strv_free_ char **a = NULL, **b = NULL;
157 char **i;
158 int r;
159
160 assert_se(fmkostemp_safe(t, "w", &f) == 0);
161 fputs("one=BAR\\\n"
162 " VAR\\\n"
163 "\tGAR\n"
164 "#comment\n"
165 "two=\"bar\\\n"
166 " var\\\n"
167 "\tgar\"\n"
168 "#comment\n"
169 "tri=\"bar \\\n"
170 " var \\\n"
171 "\tgar \"\n", f);
172
173 fflush(f);
174 fclose(f);
175
176 r = load_env_file(NULL, t, &a);
177 assert_se(r >= 0);
178
179 STRV_FOREACH(i, a)
180 log_info("Got: <%s>", *i);
181
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);
186
187 {
188 _cleanup_close_ int fd = mkostemp_safe(p);
189 assert_se(fd >= 0);
190 }
191
192 r = write_env_file(p, a);
193 assert_se(r >= 0);
194
195 r = load_env_file(NULL, p, &b);
196 assert_se(r >= 0);
197 }
198
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;
203 char **i;
204 int r;
205
206 assert_se(fmkostemp_safe(t, "w", &f) == 0);
207 log_info("/* %s (%s) */", __func__, t);
208
209 r = write_string_stream(f,
210 "one=1 \n"
211 "twelve=${one}2\n"
212 "twentyone=2${one}\n"
213 "one=2\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);
222 assert(r >= 0);
223
224 r = merge_env_file(&a, NULL, t);
225 assert_se(r >= 0);
226 strv_sort(a);
227
228 STRV_FOREACH(i, a)
229 log_info("Got: <%s>", *i);
230
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);
242
243 r = merge_env_file(&a, NULL, t);
244 assert_se(r >= 0);
245 strv_sort(a);
246
247 STRV_FOREACH(i, a)
248 log_info("Got2: <%s>", *i);
249
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);
261 }
262
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;
267 char **i;
268 int r;
269
270 assert_se(fmkostemp_safe(t, "w", &f) == 0);
271 log_info("/* %s (%s) */", __func__, t);
272
273 r = write_string_stream(f,
274 "unset one \n"
275 "unset one= \n"
276 "unset one=1 \n"
277 "one \n"
278 "one = \n"
279 "one two =\n"
280 "\x20two=\n"
281 "#comment=comment\n"
282 ";comment2=comment2\n"
283 "#\n"
284 "\n\n" /* empty line */
285 , WRITE_STRING_FILE_AVOID_NEWLINE);
286 assert(r >= 0);
287
288 r = merge_env_file(&a, NULL, t);
289 assert_se(r >= 0);
290
291 STRV_FOREACH(i, a)
292 log_info("Got: <%s>", *i);
293
294 assert_se(strv_isempty(a));
295 }
296
297 static void test_executable_is_script(void) {
298 _cleanup_(unlink_tempfilep) char t[] = "/tmp/test-fileio-XXXXXX";
299 _cleanup_fclose_ FILE *f = NULL;
300 char *command;
301 int r;
302
303 assert_se(fmkostemp_safe(t, "w", &f) == 0);
304 fputs("#! /bin/script -a -b \ngoo goo", f);
305 fflush(f);
306
307 r = executable_is_script(t, &command);
308 assert_se(r > 0);
309 assert_se(streq(command, "/bin/script"));
310 free(command);
311
312 r = executable_is_script("/bin/sh", &command);
313 assert_se(r == 0);
314
315 r = executable_is_script("/usr/bin/yum", &command);
316 assert_se(r > 0 || r == -ENOENT);
317 if (r > 0) {
318 assert_se(startswith(command, "/"));
319 free(command);
320 }
321 }
322
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;
326 int r;
327
328 assert_se(get_proc_field("/proc/self/status", "Threads", WHITESPACE, &t) == 0);
329 puts(t);
330 assert_se(streq(t, "1"));
331
332 r = get_proc_field("/proc/meminfo", "MemTotal", WHITESPACE, &p);
333 if (r != -ENOENT) {
334 assert_se(r == 0);
335 puts(p);
336 assert_se(safe_atollu(p, &total) == 0);
337 }
338
339 r = get_proc_field("/proc/meminfo", "Buffers", WHITESPACE, &s);
340 if (r != -ENOENT) {
341 assert_se(r == 0);
342 puts(s);
343 assert_se(safe_atollu(s, &buffers) == 0);
344 }
345
346 if (p)
347 assert_se(buffers < total);
348
349 /* Seccomp should be a good test for field full of zeros. */
350 r = get_proc_field("/proc/meminfo", "Seccomp", WHITESPACE, &z);
351 if (r != -ENOENT) {
352 assert_se(r == 0);
353 puts(z);
354 assert_se(safe_atollu(z, &buffers) == 0);
355 }
356 }
357
358 static void test_capeff(void) {
359 int pid, p;
360
361 for (pid = 0; pid < 2; pid++) {
362 _cleanup_free_ char *capeff = NULL;
363 int r;
364
365 r = get_process_capeff(0, &capeff);
366 log_info("capeff: '%s' (r=%d)", capeff, r);
367
368 if (IN_SET(r, -ENOENT, -EPERM))
369 return;
370
371 assert_se(r == 0);
372 assert_se(*capeff);
373 p = capeff[strspn(capeff, HEXDIGITS)];
374 assert_se(!p || isspace(p));
375 }
376 }
377
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;
381 int fd;
382 char buf[64];
383
384 fd = mkostemp_safe(fn);
385 assert_se(fd >= 0);
386
387 f = fdopen(fd, "r");
388 assert_se(f);
389 assert_se(write_string_stream(f, "boohoo", 0) < 0);
390 f = safe_fclose(f);
391
392 f = fopen(fn, "r+");
393 assert_se(f);
394
395 assert_se(write_string_stream(f, "boohoo", 0) == 0);
396 rewind(f);
397
398 assert_se(fgets(buf, sizeof(buf), f));
399 assert_se(streq(buf, "boohoo\n"));
400 f = safe_fclose(f);
401
402 f = fopen(fn, "w+");
403 assert_se(f);
404
405 assert_se(write_string_stream(f, "boohoo", WRITE_STRING_FILE_AVOID_NEWLINE) == 0);
406 rewind(f);
407
408 assert_se(fgets(buf, sizeof(buf), f));
409 printf(">%s<", buf);
410 assert_se(streq(buf, "boohoo"));
411 }
412
413 static void test_write_string_file(void) {
414 _cleanup_(unlink_tempfilep) char fn[] = "/tmp/test-write_string_file-XXXXXX";
415 char buf[64] = {};
416 _cleanup_close_ int fd;
417
418 fd = mkostemp_safe(fn);
419 assert_se(fd >= 0);
420
421 assert_se(write_string_file(fn, "boohoo", WRITE_STRING_FILE_CREATE) == 0);
422
423 assert_se(read(fd, buf, sizeof(buf)) == 7);
424 assert_se(streq(buf, "boohoo\n"));
425 }
426
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;
430 char buf[64] = {0};
431
432 fd = mkostemp_safe(fn);
433 assert_se(fd >= 0);
434
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);
437
438 assert_se(read(fd, buf, sizeof(buf)) == STRLEN("boohoo\n"));
439 assert_se(streq(buf, "boohoo\n"));
440 }
441
442 static void test_write_string_file_verify(void) {
443 _cleanup_free_ char *buf = NULL, *buf2 = NULL;
444 int r;
445
446 assert_se(read_one_line_file("/proc/cmdline", &buf) >= 0);
447 assert_se(buf2 = strjoin(buf, "\n"));
448
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));
453
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);
456
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);
460 }
461
462 static void test_load_env_file_pairs(void) {
463 _cleanup_(unlink_tempfilep) char fn[] = "/tmp/test-load_env_file_pairs-XXXXXX";
464 int fd, r;
465 _cleanup_fclose_ FILE *f = NULL;
466 _cleanup_strv_free_ char **l = NULL;
467 char **k, **v;
468
469 fd = mkostemp_safe(fn);
470 assert_se(fd >= 0);
471
472 r = write_string_file(fn,
473 "NAME=\"Arch Linux\"\n"
474 "ID=arch\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);
481 assert_se(r == 0);
482
483 f = fdopen(fd, "r");
484 assert_se(f);
485
486 r = load_env_file_pairs(f, fn, &l);
487 assert_se(r >= 0);
488
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/"));
500 }
501 }
502
503 static void test_search_and_fopen(void) {
504 const char *dirs[] = {"/tmp/foo/bar", "/tmp", NULL};
505
506 char name[] = "/tmp/test-search_and_fopen.XXXXXX";
507 int fd, r;
508 FILE *f;
509
510 fd = mkostemp_safe(name);
511 assert_se(fd >= 0);
512 close(fd);
513
514 r = search_and_fopen(basename(name), "r", NULL, dirs, &f);
515 assert_se(r >= 0);
516 fclose(f);
517
518 r = search_and_fopen(name, "r", NULL, dirs, &f);
519 assert_se(r >= 0);
520 fclose(f);
521
522 r = search_and_fopen(basename(name), "r", "/", dirs, &f);
523 assert_se(r >= 0);
524 fclose(f);
525
526 r = search_and_fopen("/a/file/which/does/not/exist/i/guess", "r", NULL, dirs, &f);
527 assert_se(r < 0);
528 r = search_and_fopen("afilewhichdoesnotexistiguess", "r", NULL, dirs, &f);
529 assert_se(r < 0);
530
531 r = unlink(name);
532 assert_se(r == 0);
533
534 r = search_and_fopen(basename(name), "r", NULL, dirs, &f);
535 assert_se(r < 0);
536 }
537
538 static void test_search_and_fopen_nulstr(void) {
539 const char dirs[] = "/tmp/foo/bar\0/tmp\0";
540
541 _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-search_and_fopen.XXXXXX";
542 int fd, r;
543 FILE *f;
544
545 fd = mkostemp_safe(name);
546 assert_se(fd >= 0);
547 close(fd);
548
549 r = search_and_fopen_nulstr(basename(name), "r", NULL, dirs, &f);
550 assert_se(r >= 0);
551 fclose(f);
552
553 r = search_and_fopen_nulstr(name, "r", NULL, dirs, &f);
554 assert_se(r >= 0);
555 fclose(f);
556
557 r = search_and_fopen_nulstr("/a/file/which/does/not/exist/i/guess", "r", NULL, dirs, &f);
558 assert_se(r < 0);
559 r = search_and_fopen_nulstr("afilewhichdoesnotexistiguess", "r", NULL, dirs, &f);
560 assert_se(r < 0);
561
562 r = unlink(name);
563 assert_se(r == 0);
564
565 r = search_and_fopen_nulstr(basename(name), "r", NULL, dirs, &f);
566 assert_se(r < 0);
567 }
568
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;
572 size_t size;
573 _cleanup_close_ int fd = -1;
574 struct iovec iov[3];
575 int r;
576
577 iov[0] = IOVEC_MAKE_STRING("abc\n");
578 iov[1] = IOVEC_MAKE_STRING(ALPHANUMERICAL "\n");
579 iov[2] = IOVEC_MAKE_STRING("");
580
581 fd = mkostemp_safe(name);
582 printf("tmpfile: %s", name);
583
584 r = writev(fd, iov, 3);
585 assert_se(r >= 0);
586
587 r = read_full_file(name, &contents, &size);
588 assert_se(r == 0);
589 printf("contents: %s", contents);
590 assert_se(streq(contents, "abc\n" ALPHANUMERICAL "\n"));
591 }
592
593 static void test_tempfn(void) {
594 char *ret = NULL, *p;
595
596 assert_se(tempfn_xxxxxx("/foo/bar/waldo", NULL, &ret) >= 0);
597 assert_se(streq_ptr(ret, "/foo/bar/.#waldoXXXXXX"));
598 free(ret);
599
600 assert_se(tempfn_xxxxxx("/foo/bar/waldo", "[miau]", &ret) >= 0);
601 assert_se(streq_ptr(ret, "/foo/bar/.#[miau]waldoXXXXXX"));
602 free(ret);
603
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"));
608 free(ret);
609
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"));
614 free(ret);
615
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"));
620 free(ret);
621
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"));
626 free(ret);
627 }
628
629 static const char chars[] =
630 "Aąę„”\n\377";
631
632 static void test_fgetc(void) {
633 _cleanup_fclose_ FILE *f = NULL;
634 char c;
635
636 f = fmemopen((void*) chars, sizeof(chars), "re");
637 assert_se(f);
638
639 for (unsigned i = 0; i < sizeof(chars); i++) {
640 assert_se(safe_fgetc(f, &c) == 1);
641 assert_se(c == chars[i]);
642
643 /* EOF is -1, and hence we can't push value 255 in this way */
644 assert_se(ungetc(c, f) != EOF || c == EOF);
645 assert_se(c == EOF || safe_fgetc(f, &c) == 1);
646 assert_se(c == chars[i]);
647
648 /* But it works when we push it properly cast */
649 assert_se(ungetc((unsigned char) c, f) != EOF);
650 assert_se(safe_fgetc(f, &c) == 1);
651 assert_se(c == chars[i]);
652 }
653
654 assert_se(safe_fgetc(f, &c) == 0);
655 }
656
657 static const char buffer[] =
658 "Some test data\n"
659 "루Non-ascii chars: ąę„”\n"
660 "terminators\r\n"
661 "and even more\n\r"
662 "now the same with a NUL\n\0"
663 "and more\r\0"
664 "and even more\r\n\0"
665 "and yet even more\n\r\0"
666 "With newlines, and a NUL byte\0"
667 "\n"
668 "an empty line\n"
669 "an ignored line\n"
670 "and a very long line that is supposed to be truncated, because it is so long\n";
671
672 static void test_read_line_one_file(FILE *f) {
673 _cleanup_free_ char *line = NULL;
674
675 assert_se(read_line(f, (size_t) -1, &line) == 15 && streq(line, "Some test data"));
676 line = mfree(line);
677
678 assert_se(read_line(f, (size_t) -1, &line) > 0 && streq(line, "루Non-ascii chars: ąę„”"));
679 line = mfree(line);
680
681 assert_se(read_line(f, (size_t) -1, &line) == 13 && streq(line, "terminators"));
682 line = mfree(line);
683
684 assert_se(read_line(f, (size_t) -1, &line) == 15 && streq(line, "and even more"));
685 line = mfree(line);
686
687 assert_se(read_line(f, (size_t) -1, &line) == 25 && streq(line, "now the same with a NUL"));
688 line = mfree(line);
689
690 assert_se(read_line(f, (size_t) -1, &line) == 10 && streq(line, "and more"));
691 line = mfree(line);
692
693 assert_se(read_line(f, (size_t) -1, &line) == 16 && streq(line, "and even more"));
694 line = mfree(line);
695
696 assert_se(read_line(f, (size_t) -1, &line) == 20 && streq(line, "and yet even more"));
697 line = mfree(line);
698
699 assert_se(read_line(f, 1024, &line) == 30 && streq(line, "With newlines, and a NUL byte"));
700 line = mfree(line);
701
702 assert_se(read_line(f, 1024, &line) == 1 && streq(line, ""));
703 line = mfree(line);
704
705 assert_se(read_line(f, 1024, &line) == 14 && streq(line, "an empty line"));
706 line = mfree(line);
707
708 assert_se(read_line(f, (size_t) -1, NULL) == 16);
709
710 assert_se(read_line(f, 16, &line) == -ENOBUFS);
711 line = mfree(line);
712
713 /* read_line() stopped when it hit the limit, that means when we continue reading we'll read at the first
714 * character after the previous limit. Let's make use of tha to continue our test. */
715 assert_se(read_line(f, 1024, &line) == 62 && streq(line, "line that is supposed to be truncated, because it is so long"));
716 line = mfree(line);
717
718 assert_se(read_line(f, 1024, &line) == 0 && streq(line, ""));
719 }
720
721 static void test_read_line(void) {
722 _cleanup_fclose_ FILE *f = NULL;
723
724 f = fmemopen((void*) buffer, sizeof(buffer), "re");
725 assert_se(f);
726
727 test_read_line_one_file(f);
728 }
729
730 static void test_read_line2(void) {
731 _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-fileio.XXXXXX";
732 int fd;
733 _cleanup_fclose_ FILE *f = NULL;
734
735 fd = mkostemp_safe(name);
736 assert_se(fd >= 0);
737 assert_se((size_t) write(fd, buffer, sizeof(buffer)) == sizeof(buffer));
738
739 assert_se(lseek(fd, 0, SEEK_SET) == 0);
740 assert_se(f = fdopen(fd, "r"));
741
742 test_read_line_one_file(f);
743 }
744
745 static void test_read_line3(void) {
746 _cleanup_fclose_ FILE *f = NULL;
747 _cleanup_free_ char *line = NULL;
748 int r;
749
750 f = fopen("/proc/cmdline", "re");
751 if (!f && IN_SET(errno, ENOENT, EPERM))
752 return;
753 assert_se(f);
754
755 r = read_line(f, LINE_MAX, &line);
756 assert_se((size_t) r == strlen(line) + 1);
757 assert_se(read_line(f, LINE_MAX, NULL) == 0);
758 }
759
760 static void test_read_line4(void) {
761 static const struct {
762 size_t length;
763 const char *string;
764 } eof_endings[] = {
765 /* Each of these will be followed by EOF and should generate the one same single string */
766 { 3, "foo" },
767 { 4, "foo\n" },
768 { 4, "foo\r" },
769 { 4, "foo\0" },
770 { 5, "foo\n\0" },
771 { 5, "foo\r\0" },
772 { 5, "foo\r\n" },
773 { 5, "foo\n\r" },
774 { 6, "foo\r\n\0" },
775 { 6, "foo\n\r\0" },
776 };
777
778 size_t i;
779 int r;
780
781 for (i = 0; i < ELEMENTSOF(eof_endings); i++) {
782 _cleanup_fclose_ FILE *f = NULL;
783 _cleanup_free_ char *s = NULL;
784
785 assert_se(f = fmemopen((void*) eof_endings[i].string, eof_endings[i].length, "r"));
786
787 r = read_line(f, (size_t) -1, &s);
788 assert_se((size_t) r == eof_endings[i].length);
789 assert_se(streq_ptr(s, "foo"));
790
791 assert_se(read_line(f, (size_t) -1, NULL) == 0); /* Ensure we hit EOF */
792 }
793 }
794
795 static void test_read_nul_string(void) {
796 static const char test[] = "string nr. 1\0"
797 "string nr. 2\n\0"
798 "\377empty string follows\0"
799 "\0"
800 "final string\n is empty\0"
801 "\0";
802
803 _cleanup_fclose_ FILE *f = NULL;
804 _cleanup_free_ char *s = NULL;
805
806 assert_se(f = fmemopen((void*) test, sizeof(test)-1, "r"));
807
808 assert_se(read_nul_string(f, LONG_LINE_MAX, &s) == 13 && streq_ptr(s, "string nr. 1"));
809 s = mfree(s);
810
811 assert_se(read_nul_string(f, LONG_LINE_MAX, &s) == 14 && streq_ptr(s, "string nr. 2\n"));
812 s = mfree(s);
813
814 assert_se(read_nul_string(f, LONG_LINE_MAX, &s) == 22 && streq_ptr(s, "\377empty string follows"));
815 s = mfree(s);
816
817 assert_se(read_nul_string(f, LONG_LINE_MAX, &s) == 1 && streq_ptr(s, ""));
818 s = mfree(s);
819
820 assert_se(read_nul_string(f, LONG_LINE_MAX, &s) == 23 && streq_ptr(s, "final string\n is empty"));
821 s = mfree(s);
822
823 assert_se(read_nul_string(f, LONG_LINE_MAX, &s) == 1 && streq_ptr(s, ""));
824 s = mfree(s);
825
826 assert_se(read_nul_string(f, LONG_LINE_MAX, &s) == 0 && streq_ptr(s, ""));
827 }
828
829 int main(int argc, char *argv[]) {
830 test_setup_logging(LOG_DEBUG);
831
832 test_parse_env_file();
833 test_parse_multiline_env_file();
834 test_merge_env_file();
835 test_merge_env_file_invalid();
836 test_executable_is_script();
837 test_status_field();
838 test_capeff();
839 test_write_string_stream();
840 test_write_string_file();
841 test_write_string_file_no_create();
842 test_write_string_file_verify();
843 test_load_env_file_pairs();
844 test_search_and_fopen();
845 test_search_and_fopen_nulstr();
846 test_writing_tmpfile();
847 test_tempfn();
848 test_fgetc();
849 test_read_line();
850 test_read_line2();
851 test_read_line3();
852 test_read_line4();
853 test_read_nul_string();
854
855 return 0;
856 }