]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-fileio.c
update TODO
[thirdparty/systemd.git] / src / test / test-fileio.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
f73141d7 2
f73141d7 3#include <fcntl.h>
cf0fbc49 4#include <stdio.h>
f73141d7
LP
5#include <unistd.h>
6
b5efdb8a 7#include "alloc-util.h"
1e5413f7 8#include "ctype.h"
686d13b9 9#include "env-file.h"
3ffd4af2
LP
10#include "env-util.h"
11#include "fd-util.h"
12#include "fileio.h"
627d2bac 13#include "fs-util.h"
897891f0 14#include "io-util.h"
6bedfcbb 15#include "parse-util.h"
3ffd4af2 16#include "process-util.h"
07630cea 17#include "string-util.h"
3ffd4af2 18#include "strv.h"
6d7c4033 19#include "tests.h"
e4de7287 20#include "tmpfile-util.h"
3ffd4af2 21#include "util.h"
f73141d7
LP
22
23static void test_parse_env_file(void) {
627d2bac
ZJS
24 _cleanup_(unlink_tempfilep) char
25 t[] = "/tmp/test-fileio-in-XXXXXX",
095b30cb 26 p[] = "/tmp/test-fileio-out-XXXXXX";
f73141d7 27 FILE *f;
ebc05a09
HH
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;
768100ef 30 _cleanup_strv_free_ char **a = NULL, **b = NULL;
f73141d7 31 char **i;
768100ef 32 unsigned k;
d8351049 33 int r;
f73141d7 34
d8351049 35 assert_se(fmkostemp_safe(t, "w", &f) == 0);
f73141d7
LP
36 fputs("one=BAR \n"
37 "# comment\n"
38 " # comment \n"
98f59e59 39 " ; comment \n"
f73141d7
LP
40 " two = bar \n"
41 "invalid line\n"
98f59e59 42 "invalid line #comment\n"
f73141d7
LP
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"
98f59e59
HH
49 "seven=\"sevenval\" #nocomment\n"
50 "eight=eightval #nocomment\n"
ebc05a09 51 "export nine=nineval\n"
99003e01
ZJS
52 "ten=ignored\n"
53 "ten=ignored\n"
ebc05a09 54 "ten=", f);
f73141d7
LP
55
56 fflush(f);
57 fclose(f);
58
aa8fbc74 59 r = load_env_file(NULL, t, &a);
ebc05a09
HH
60 assert_se(r >= 0);
61
62 STRV_FOREACH(i, a)
63 log_info("Got: <%s>", *i);
64
5fba7bbf
TA
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="));
ebc05a09
HH
75 assert_se(a[10] == NULL);
76
039f0e70 77 strv_env_clean(a);
ebc05a09 78
ebc05a09
HH
79 k = 0;
80 STRV_FOREACH(i, b) {
81 log_info("Got2: <%s>", *i);
82 assert_se(streq(*i, a[k++]));
83 }
84
f73141d7 85 r = parse_env_file(
aa8fbc74 86 NULL, t,
f73141d7
LP
87 "one", &one,
88 "two", &two,
89 "three", &three,
90 "four", &four,
91 "five", &five,
92 "six", &six,
93 "seven", &seven,
db537209 94 "eight", &eight,
ebc05a09 95 "export nine", &nine,
13df9c39 96 "ten", &ten);
f73141d7
LP
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));
db537209 107 log_info("eight=[%s]", strna(eight));
ebc05a09
HH
108 log_info("export nine=[%s]", strna(nine));
109 log_info("ten=[%s]", strna(nine));
f73141d7
LP
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"));
98f59e59
HH
117 assert_se(streq(seven, "sevenval#nocomment"));
118 assert_se(streq(eight, "eightval #nocomment"));
ebc05a09
HH
119 assert_se(streq(nine, "nineval"));
120 assert_se(ten == NULL);
f73141d7 121
d8351049
ZJS
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
095b30cb 128 r = write_env_file(p, a);
98f59e59
HH
129 assert_se(r >= 0);
130
aa8fbc74 131 r = load_env_file(NULL, p, &b);
98f59e59 132 assert_se(r >= 0);
f73141d7
LP
133}
134
ac4c8d6d 135static void test_parse_multiline_env_file(void) {
627d2bac
ZJS
136 _cleanup_(unlink_tempfilep) char
137 t[] = "/tmp/test-fileio-in-XXXXXX",
ac4c8d6d 138 p[] = "/tmp/test-fileio-out-XXXXXX";
ac4c8d6d
ZJS
139 FILE *f;
140 _cleanup_strv_free_ char **a = NULL, **b = NULL;
141 char **i;
d8351049 142 int r;
ac4c8d6d 143
d8351049 144 assert_se(fmkostemp_safe(t, "w", &f) == 0);
ac4c8d6d
ZJS
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
aa8fbc74 160 r = load_env_file(NULL, t, &a);
ac4c8d6d
ZJS
161 assert_se(r >= 0);
162
163 STRV_FOREACH(i, a)
164 log_info("Got: <%s>", *i);
165
5fba7bbf
TA
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 "));
ac4c8d6d
ZJS
169 assert_se(a[3] == NULL);
170
d8351049
ZJS
171 {
172 _cleanup_close_ int fd = mkostemp_safe(p);
173 assert_se(fd >= 0);
174 }
175
ac4c8d6d
ZJS
176 r = write_env_file(p, a);
177 assert_se(r >= 0);
178
aa8fbc74 179 r = load_env_file(NULL, p, &b);
ac4c8d6d 180 assert_se(r >= 0);
ac4c8d6d
ZJS
181}
182
37f3ffca 183static void test_merge_env_file(void) {
627d2bac 184 _cleanup_(unlink_tempfilep) char t[] = "/tmp/test-fileio-XXXXXX";
9707d552 185 _cleanup_fclose_ FILE *f = NULL;
37f3ffca
RS
186 _cleanup_strv_free_ char **a = NULL;
187 char **i;
d8351049 188 int r;
37f3ffca 189
d8351049 190 assert_se(fmkostemp_safe(t, "w", &f) == 0);
37f3ffca
RS
191 log_info("/* %s (%s) */", __func__, t);
192
37f3ffca
RS
193 r = write_string_stream(f,
194 "one=1 \n"
195 "twelve=${one}2\n"
196 "twentyone=2${one}\n"
197 "one=2\n"
ccad1fd0
ZJS
198 "twentytwo=2${one}\n"
199 "xxx_minus_three=$xxx - 3\n"
200 "xxx=0x$one$one$one\n"
b82f58bf
RS
201 "yyy=${one:-fallback}\n"
202 "zzz=${one:+replacement}\n"
203 "zzzz=${foobar:-${nothing}}\n"
204 "zzzzz=${nothing:+${nothing}}\n"
b1837133 205 , WRITE_STRING_FILE_AVOID_NEWLINE);
37f3ffca
RS
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"));
ccad1fd0
ZJS
219 assert_se(streq(a[4], "xxx=0x222"));
220 assert_se(streq(a[5], "xxx_minus_three= - 3"));
b82f58bf
RS
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);
37f3ffca
RS
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"));
ccad1fd0
ZJS
238 assert_se(streq(a[4], "xxx=0x222"));
239 assert_se(streq(a[5], "xxx_minus_three=0x222 - 3"));
b82f58bf
RS
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);
37f3ffca 245}
ac4c8d6d 246
184d1904 247static void test_merge_env_file_invalid(void) {
627d2bac 248 _cleanup_(unlink_tempfilep) char t[] = "/tmp/test-fileio-XXXXXX";
9707d552 249 _cleanup_fclose_ FILE *f = NULL;
184d1904
ZJS
250 _cleanup_strv_free_ char **a = NULL;
251 char **i;
d8351049 252 int r;
184d1904 253
d8351049 254 assert_se(fmkostemp_safe(t, "w", &f) == 0);
184d1904
ZJS
255 log_info("/* %s (%s) */", __func__, t);
256
184d1904
ZJS
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 */
b1837133 269 , WRITE_STRING_FILE_AVOID_NEWLINE);
184d1904
ZJS
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
68fee104 281static void test_executable_is_script(void) {
627d2bac 282 _cleanup_(unlink_tempfilep) char t[] = "/tmp/test-fileio-XXXXXX";
627d2bac 283 _cleanup_fclose_ FILE *f = NULL;
68fee104 284 char *command;
d8351049 285 int r;
68fee104 286
d8351049 287 assert_se(fmkostemp_safe(t, "w", &f) == 0);
68fee104
ZJS
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 }
68fee104
ZJS
305}
306
69ab8088 307static void test_status_field(void) {
1e5413f7
ZJS
308 _cleanup_free_ char *t = NULL, *p = NULL, *s = NULL, *z = NULL;
309 unsigned long long total = 0, buffers = 0;
442e0083 310 int r;
69ab8088 311
c4cd1d4d 312 assert_se(get_proc_field("/proc/self/status", "Threads", WHITESPACE, &t) == 0);
69ab8088
ZJS
313 puts(t);
314 assert_se(streq(t, "1"));
315
c4cd1d4d 316 r = get_proc_field("/proc/meminfo", "MemTotal", WHITESPACE, &p);
1e5413f7 317 if (r != -ENOENT) {
bdf7026e 318 assert_se(r == 0);
1e5413f7
ZJS
319 puts(p);
320 assert_se(safe_atollu(p, &total) == 0);
321 }
69ab8088 322
c4cd1d4d 323 r = get_proc_field("/proc/meminfo", "Buffers", WHITESPACE, &s);
1e5413f7 324 if (r != -ENOENT) {
bdf7026e 325 assert_se(r == 0);
1e5413f7
ZJS
326 puts(s);
327 assert_se(safe_atollu(s, &buffers) == 0);
328 }
69ab8088 329
2b01a801 330 if (p)
bdf7026e 331 assert_se(buffers < total);
1e5413f7
ZJS
332
333 /* Seccomp should be a good test for field full of zeros. */
c4cd1d4d 334 r = get_proc_field("/proc/meminfo", "Seccomp", WHITESPACE, &z);
1e5413f7 335 if (r != -ENOENT) {
bdf7026e 336 assert_se(r == 0);
1e5413f7
ZJS
337 puts(z);
338 assert_se(safe_atollu(z, &buffers) == 0);
339 }
340}
341
342static 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
4c701096 352 if (IN_SET(r, -ENOENT, -EPERM))
1e5413f7
ZJS
353 return;
354
bdf7026e
TA
355 assert_se(r == 0);
356 assert_se(*capeff);
1a7906ae 357 p = capeff[strspn(capeff, HEXDIGITS)];
bdf7026e 358 assert_se(!p || isspace(p));
1e5413f7 359 }
69ab8088
ZJS
360}
361
0709b743 362static void test_write_string_stream(void) {
627d2bac
ZJS
363 _cleanup_(unlink_tempfilep) char fn[] = "/tmp/test-write_string_stream-XXXXXX";
364 _cleanup_fclose_ FILE *f = NULL;
0709b743
RC
365 int fd;
366 char buf[64];
367
646853bd 368 fd = mkostemp_safe(fn);
0709b743
RC
369 assert_se(fd >= 0);
370
371 f = fdopen(fd, "r");
372 assert_se(f);
b1837133 373 assert_se(write_string_stream(f, "boohoo", 0) < 0);
36297390 374 f = safe_fclose(f);
0709b743 375
36297390 376 f = fopen(fn, "r+");
0709b743
RC
377 assert_se(f);
378
b1837133 379 assert_se(write_string_stream(f, "boohoo", 0) == 0);
0709b743
RC
380 rewind(f);
381
382 assert_se(fgets(buf, sizeof(buf), f));
383 assert_se(streq(buf, "boohoo\n"));
36297390 384 f = safe_fclose(f);
0709b743 385
36297390 386 f = fopen(fn, "w+");
40beecdb
DM
387 assert_se(f);
388
b1837133 389 assert_se(write_string_stream(f, "boohoo", WRITE_STRING_FILE_AVOID_NEWLINE) == 0);
40beecdb
DM
390 rewind(f);
391
392 assert_se(fgets(buf, sizeof(buf), f));
393 printf(">%s<", buf);
394 assert_se(streq(buf, "boohoo"));
0709b743
RC
395}
396
397static void test_write_string_file(void) {
627d2bac 398 _cleanup_(unlink_tempfilep) char fn[] = "/tmp/test-write_string_file-XXXXXX";
76082570
RC
399 char buf[64] = {};
400 _cleanup_close_ int fd;
0709b743 401
646853bd 402 fd = mkostemp_safe(fn);
0709b743
RC
403 assert_se(fd >= 0);
404
4c1fc3e4 405 assert_se(write_string_file(fn, "boohoo", WRITE_STRING_FILE_CREATE) == 0);
0709b743 406
cca0efb0 407 assert_se(read(fd, buf, sizeof(buf)) == 7);
0709b743 408 assert_se(streq(buf, "boohoo\n"));
0709b743
RC
409}
410
e07995a3 411static void test_write_string_file_no_create(void) {
627d2bac 412 _cleanup_(unlink_tempfilep) char fn[] = "/tmp/test-write_string_file_no_create-XXXXXX";
e07995a3
RC
413 _cleanup_close_ int fd;
414 char buf[64] = {0};
415
646853bd 416 fd = mkostemp_safe(fn);
e07995a3
RC
417 assert_se(fd >= 0);
418
4c1fc3e4
DM
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);
e07995a3 421
fbd0b64f 422 assert_se(read(fd, buf, sizeof(buf)) == STRLEN("boohoo\n"));
e07995a3 423 assert_se(streq(buf, "boohoo\n"));
e07995a3
RC
424}
425
eb3da901
LP
426static 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);
dcd6361e 431 assert_se(buf2 = strjoin(buf, "\n"));
eb3da901
LP
432
433 r = write_string_file("/proc/cmdline", buf, 0);
4c701096 434 assert_se(IN_SET(r, -EACCES, -EIO));
eb3da901 435 r = write_string_file("/proc/cmdline", buf2, 0);
4c701096 436 assert_se(IN_SET(r, -EACCES, -EIO));
eb3da901
LP
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);
4c701096 442 assert_se(IN_SET(r, -EACCES, -EIO));
eb3da901
LP
443 assert_se(write_string_file("/proc/cmdline", buf2, WRITE_STRING_FILE_VERIFY_ON_FAILURE|WRITE_STRING_FILE_AVOID_NEWLINE) == 0);
444}
445
e07995a3 446static void test_load_env_file_pairs(void) {
627d2bac
ZJS
447 _cleanup_(unlink_tempfilep) char fn[] = "/tmp/test-load_env_file_pairs-XXXXXX";
448 int fd, r;
e07995a3
RC
449 _cleanup_fclose_ FILE *f = NULL;
450 _cleanup_strv_free_ char **l = NULL;
451 char **k, **v;
452
646853bd 453 fd = mkostemp_safe(fn);
e07995a3
RC
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"
4c1fc3e4
DM
463 "BUG_REPORT_URL=\"https://bugs.archlinux.org/\"\n",
464 WRITE_STRING_FILE_CREATE);
e07995a3
RC
465 assert_se(r == 0);
466
467 f = fdopen(fd, "r");
468 assert_se(f);
469
aa8fbc74 470 r = load_env_file_pairs(f, fn, &l);
e07995a3
RC
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 }
e07995a3
RC
485}
486
897891f0
RC
487static void test_search_and_fopen(void) {
488 const char *dirs[] = {"/tmp/foo/bar", "/tmp", NULL};
627d2bac 489
897891f0 490 char name[] = "/tmp/test-search_and_fopen.XXXXXX";
627d2bac 491 int fd, r;
897891f0
RC
492 FILE *f;
493
646853bd 494 fd = mkostemp_safe(name);
897891f0
RC
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
897891f0
RC
522static void test_search_and_fopen_nulstr(void) {
523 const char dirs[] = "/tmp/foo/bar\0/tmp\0";
627d2bac
ZJS
524
525 _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-search_and_fopen.XXXXXX";
526 int fd, r;
897891f0
RC
527 FILE *f;
528
646853bd 529 fd = mkostemp_safe(name);
897891f0
RC
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
553static void test_writing_tmpfile(void) {
627d2bac 554 _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-systemd_writing_tmpfile.XXXXXX";
897891f0
RC
555 _cleanup_free_ char *contents = NULL;
556 size_t size;
9a92e255 557 _cleanup_close_ int fd = -1;
897891f0 558 struct iovec iov[3];
627d2bac 559 int r;
897891f0 560
e6a7ec4b
LP
561 iov[0] = IOVEC_MAKE_STRING("abc\n");
562 iov[1] = IOVEC_MAKE_STRING(ALPHANUMERICAL "\n");
563 iov[2] = IOVEC_MAKE_STRING("");
897891f0 564
646853bd 565 fd = mkostemp_safe(name);
897891f0
RC
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"));
897891f0
RC
575}
576
577static 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
2c9de139
ZJS
613static const char buffer[] =
614 "Some test data\n"
838894b0
LP
615 "Some weird line\r"
616 "terminators\r\n"
617 "and even more\n\r"
618 "now the same with a NUL\n\0"
619 "and more\r\0"
620 "and even more\r\n\0"
621 "and yet even more\n\r\0"
2c9de139
ZJS
622 "With newlines, and a NUL byte\0"
623 "\n"
624 "an empty line\n"
625 "an ignored line\n"
626 "and a very long line that is supposed to be truncated, because it is so long\n";
627
628static void test_read_line_one_file(FILE *f) {
4f9a66a3
LP
629 _cleanup_free_ char *line = NULL;
630
4f9a66a3
LP
631 assert_se(read_line(f, (size_t) -1, &line) == 15 && streq(line, "Some test data"));
632 line = mfree(line);
633
838894b0
LP
634 assert_se(read_line(f, (size_t) -1, &line) == 16 && streq(line, "Some weird line"));
635 line = mfree(line);
636
637 assert_se(read_line(f, (size_t) -1, &line) == 13 && streq(line, "terminators"));
638 line = mfree(line);
639
640 assert_se(read_line(f, (size_t) -1, &line) == 15 && streq(line, "and even more"));
641 line = mfree(line);
642
643 assert_se(read_line(f, (size_t) -1, &line) == 25 && streq(line, "now the same with a NUL"));
644 line = mfree(line);
645
646 assert_se(read_line(f, (size_t) -1, &line) == 10 && streq(line, "and more"));
647 line = mfree(line);
648
649 assert_se(read_line(f, (size_t) -1, &line) == 16 && streq(line, "and even more"));
650 line = mfree(line);
651
652 assert_se(read_line(f, (size_t) -1, &line) == 20 && streq(line, "and yet even more"));
653 line = mfree(line);
654
4f9a66a3
LP
655 assert_se(read_line(f, 1024, &line) == 30 && streq(line, "With newlines, and a NUL byte"));
656 line = mfree(line);
657
658 assert_se(read_line(f, 1024, &line) == 1 && streq(line, ""));
659 line = mfree(line);
660
661 assert_se(read_line(f, 1024, &line) == 14 && streq(line, "an empty line"));
662 line = mfree(line);
663
664 assert_se(read_line(f, (size_t) -1, NULL) == 16);
665
666 assert_se(read_line(f, 16, &line) == -ENOBUFS);
667 line = mfree(line);
668
669 /* read_line() stopped when it hit the limit, that means when we continue reading we'll read at the first
670 * character after the previous limit. Let's make use of tha to continue our test. */
838894b0 671 assert_se(read_line(f, 1024, &line) == 62 && streq(line, "line that is supposed to be truncated, because it is so long"));
4f9a66a3
LP
672 line = mfree(line);
673
674 assert_se(read_line(f, 1024, &line) == 0 && streq(line, ""));
675}
676
2c9de139
ZJS
677static void test_read_line(void) {
678 _cleanup_fclose_ FILE *f = NULL;
2c9de139
ZJS
679
680 f = fmemopen((void*) buffer, sizeof(buffer), "re");
681 assert_se(f);
682
683 test_read_line_one_file(f);
684}
685
686static void test_read_line2(void) {
627d2bac 687 _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-fileio.XXXXXX";
2c9de139
ZJS
688 int fd;
689 _cleanup_fclose_ FILE *f = NULL;
690
691 fd = mkostemp_safe(name);
692 assert_se(fd >= 0);
693 assert_se((size_t) write(fd, buffer, sizeof(buffer)) == sizeof(buffer));
694
695 assert_se(lseek(fd, 0, SEEK_SET) == 0);
696 assert_se(f = fdopen(fd, "r"));
697
698 test_read_line_one_file(f);
699}
700
701static void test_read_line3(void) {
702 _cleanup_fclose_ FILE *f = NULL;
703 _cleanup_free_ char *line = NULL;
704 int r;
705
706 f = fopen("/proc/cmdline", "re");
707 if (!f && IN_SET(errno, ENOENT, EPERM))
708 return;
709 assert_se(f);
710
711 r = read_line(f, LINE_MAX, &line);
712 assert_se((size_t) r == strlen(line) + 1);
713 assert_se(read_line(f, LINE_MAX, NULL) == 0);
714}
715
6baac700
LP
716static void test_read_line4(void) {
717 static const struct {
718 size_t length;
719 const char *string;
720 } eof_endings[] = {
721 /* Each of these will be followed by EOF and should generate the one same single string */
722 { 3, "foo" },
723 { 4, "foo\n" },
724 { 4, "foo\r" },
725 { 4, "foo\0" },
726 { 5, "foo\n\0" },
727 { 5, "foo\r\0" },
728 { 5, "foo\r\n" },
729 { 5, "foo\n\r" },
730 { 6, "foo\r\n\0" },
731 { 6, "foo\n\r\0" },
732 };
733
734 size_t i;
735 int r;
736
737 for (i = 0; i < ELEMENTSOF(eof_endings); i++) {
738 _cleanup_fclose_ FILE *f = NULL;
739 _cleanup_free_ char *s = NULL;
740
741 assert_se(f = fmemopen((void*) eof_endings[i].string, eof_endings[i].length, "r"));
742
743 r = read_line(f, (size_t) -1, &s);
744 assert_se((size_t) r == eof_endings[i].length);
745 assert_se(streq_ptr(s, "foo"));
746
747 assert_se(read_line(f, (size_t) -1, NULL) == 0); /* Ensure we hit EOF */
748 }
749}
750
3946d576
LP
751static void test_read_nul_string(void) {
752 static const char test[] = "string nr. 1\0"
753 "string nr. 2\n\0"
754 "empty string follows\0"
755 "\0"
756 "final string\n is empty\0"
757 "\0";
758
759 _cleanup_fclose_ FILE *f = NULL;
760 _cleanup_free_ char *s = NULL;
761
762 assert_se(f = fmemopen((void*) test, sizeof(test)-1, "r"));
763
764 assert_se(read_nul_string(f, LONG_LINE_MAX, &s) == 13 && streq_ptr(s, "string nr. 1"));
765 s = mfree(s);
766
767 assert_se(read_nul_string(f, LONG_LINE_MAX, &s) == 14 && streq_ptr(s, "string nr. 2\n"));
768 s = mfree(s);
769
770 assert_se(read_nul_string(f, LONG_LINE_MAX, &s) == 21 && streq_ptr(s, "empty string follows"));
771 s = mfree(s);
772
773 assert_se(read_nul_string(f, LONG_LINE_MAX, &s) == 1 && streq_ptr(s, ""));
774 s = mfree(s);
775
776 assert_se(read_nul_string(f, LONG_LINE_MAX, &s) == 23 && streq_ptr(s, "final string\n is empty"));
777 s = mfree(s);
778
779 assert_se(read_nul_string(f, LONG_LINE_MAX, &s) == 1 && streq_ptr(s, ""));
780 s = mfree(s);
781
782 assert_se(read_nul_string(f, LONG_LINE_MAX, &s) == 0 && streq_ptr(s, ""));
783}
784
f73141d7 785int main(int argc, char *argv[]) {
6d7c4033 786 test_setup_logging(LOG_DEBUG);
1e5413f7 787
f73141d7 788 test_parse_env_file();
ac4c8d6d 789 test_parse_multiline_env_file();
37f3ffca 790 test_merge_env_file();
184d1904 791 test_merge_env_file_invalid();
68fee104 792 test_executable_is_script();
69ab8088 793 test_status_field();
1e5413f7 794 test_capeff();
0709b743
RC
795 test_write_string_stream();
796 test_write_string_file();
e07995a3 797 test_write_string_file_no_create();
eb3da901 798 test_write_string_file_verify();
e07995a3 799 test_load_env_file_pairs();
897891f0
RC
800 test_search_and_fopen();
801 test_search_and_fopen_nulstr();
802 test_writing_tmpfile();
803 test_tempfn();
4f9a66a3 804 test_read_line();
2c9de139
ZJS
805 test_read_line2();
806 test_read_line3();
6baac700 807 test_read_line4();
3946d576 808 test_read_nul_string();
1e5413f7 809
f73141d7
LP
810 return 0;
811}