]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-fileio.c
Merge pull request #26953 from poettering/encrypted-cred-mini-refactor
[thirdparty/systemd.git] / src / test / test-fileio.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
f73141d7 2
f73141d7 3#include <fcntl.h>
72fd79b3 4#include <limits.h>
cf0fbc49 5#include <stdio.h>
f73141d7
LP
6#include <unistd.h>
7
b5efdb8a 8#include "alloc-util.h"
1e5413f7 9#include "ctype.h"
686d13b9 10#include "env-file.h"
3ffd4af2 11#include "env-util.h"
bca895c4 12#include "errno-util.h"
3ffd4af2
LP
13#include "fd-util.h"
14#include "fileio.h"
627d2bac 15#include "fs-util.h"
897891f0 16#include "io-util.h"
6bedfcbb 17#include "parse-util.h"
2708160c 18#include "path-util.h"
3ffd4af2 19#include "process-util.h"
d3dcf4e3 20#include "random-util.h"
b93d3f6b
LP
21#include "rm-rf.h"
22#include "socket-util.h"
07630cea 23#include "string-util.h"
3ffd4af2 24#include "strv.h"
6d7c4033 25#include "tests.h"
e4de7287 26#include "tmpfile-util.h"
f73141d7 27
4f7452a8 28TEST(parse_env_file) {
627d2bac
ZJS
29 _cleanup_(unlink_tempfilep) char
30 t[] = "/tmp/test-fileio-in-XXXXXX",
095b30cb 31 p[] = "/tmp/test-fileio-out-XXXXXX";
f73141d7 32 FILE *f;
ebc05a09 33 _cleanup_free_ char *one = NULL, *two = NULL, *three = NULL, *four = NULL, *five = NULL,
e4a8db1f
LT
34 *six = NULL, *seven = NULL, *eight = NULL, *nine = NULL, *ten = NULL,
35 *eleven = NULL, *twelve = NULL, *thirteen = NULL;
768100ef 36 _cleanup_strv_free_ char **a = NULL, **b = NULL;
768100ef 37 unsigned k;
d8351049 38 int r;
f73141d7 39
d8351049 40 assert_se(fmkostemp_safe(t, "w", &f) == 0);
f73141d7
LP
41 fputs("one=BAR \n"
42 "# comment\n"
43 " # comment \n"
98f59e59 44 " ; comment \n"
f73141d7
LP
45 " two = bar \n"
46 "invalid line\n"
98f59e59 47 "invalid line #comment\n"
f73141d7
LP
48 "three = \"333\n"
49 "xxxx\"\n"
50 "four = \'44\\\"44\'\n"
e4a8db1f 51 "five = \"55\\\"55\" \"FIVE\" cinco \n"
f73141d7
LP
52 "six = seis sechs\\\n"
53 " sis\n"
98f59e59
HH
54 "seven=\"sevenval\" #nocomment\n"
55 "eight=eightval #nocomment\n"
ebc05a09 56 "export nine=nineval\n"
99003e01
ZJS
57 "ten=ignored\n"
58 "ten=ignored\n"
e4a8db1f
LT
59 "ten=\n"
60 "eleven=\\value\n"
61 "twelve=\"\\value\"\n"
62 "thirteen='\\value'", f);
f73141d7
LP
63
64 fflush(f);
65 fclose(f);
66
aa8fbc74 67 r = load_env_file(NULL, t, &a);
ebc05a09
HH
68 assert_se(r >= 0);
69
70 STRV_FOREACH(i, a)
71 log_info("Got: <%s>", *i);
72
5fba7bbf
TA
73 assert_se(streq_ptr(a[0], "one=BAR"));
74 assert_se(streq_ptr(a[1], "two=bar"));
75 assert_se(streq_ptr(a[2], "three=333\nxxxx"));
e4a8db1f
LT
76 assert_se(streq_ptr(a[3], "four=44\\\"44"));
77 assert_se(streq_ptr(a[4], "five=55\"55FIVEcinco"));
5fba7bbf
TA
78 assert_se(streq_ptr(a[5], "six=seis sechs sis"));
79 assert_se(streq_ptr(a[6], "seven=sevenval#nocomment"));
80 assert_se(streq_ptr(a[7], "eight=eightval #nocomment"));
81 assert_se(streq_ptr(a[8], "export nine=nineval"));
82 assert_se(streq_ptr(a[9], "ten="));
e4a8db1f
LT
83 assert_se(streq_ptr(a[10], "eleven=value"));
84 assert_se(streq_ptr(a[11], "twelve=\\value"));
85 assert_se(streq_ptr(a[12], "thirteen=\\value"));
86 assert_se(a[13] == NULL);
ebc05a09 87
039f0e70 88 strv_env_clean(a);
ebc05a09 89
ebc05a09
HH
90 k = 0;
91 STRV_FOREACH(i, b) {
92 log_info("Got2: <%s>", *i);
93 assert_se(streq(*i, a[k++]));
94 }
95
f73141d7 96 r = parse_env_file(
aa8fbc74 97 NULL, t,
f73141d7
LP
98 "one", &one,
99 "two", &two,
100 "three", &three,
101 "four", &four,
102 "five", &five,
103 "six", &six,
104 "seven", &seven,
db537209 105 "eight", &eight,
ebc05a09 106 "export nine", &nine,
e4a8db1f
LT
107 "ten", &ten,
108 "eleven", &eleven,
109 "twelve", &twelve,
110 "thirteen", &thirteen);
99aad9a2 111 assert_se(r == 0);
f73141d7
LP
112
113 log_info("one=[%s]", strna(one));
114 log_info("two=[%s]", strna(two));
115 log_info("three=[%s]", strna(three));
116 log_info("four=[%s]", strna(four));
117 log_info("five=[%s]", strna(five));
118 log_info("six=[%s]", strna(six));
119 log_info("seven=[%s]", strna(seven));
db537209 120 log_info("eight=[%s]", strna(eight));
ebc05a09
HH
121 log_info("export nine=[%s]", strna(nine));
122 log_info("ten=[%s]", strna(nine));
e4a8db1f
LT
123 log_info("eleven=[%s]", strna(eleven));
124 log_info("twelve=[%s]", strna(twelve));
125 log_info("thirteen=[%s]", strna(thirteen));
f73141d7
LP
126
127 assert_se(streq(one, "BAR"));
128 assert_se(streq(two, "bar"));
129 assert_se(streq(three, "333\nxxxx"));
e4a8db1f
LT
130 assert_se(streq(four, "44\\\"44"));
131 assert_se(streq(five, "55\"55FIVEcinco"));
f73141d7 132 assert_se(streq(six, "seis sechs sis"));
98f59e59
HH
133 assert_se(streq(seven, "sevenval#nocomment"));
134 assert_se(streq(eight, "eightval #nocomment"));
ebc05a09
HH
135 assert_se(streq(nine, "nineval"));
136 assert_se(ten == NULL);
e4a8db1f
LT
137 assert_se(streq(eleven, "value"));
138 assert_se(streq(twelve, "\\value"));
139 assert_se(streq(thirteen, "\\value"));
f73141d7 140
d8351049
ZJS
141 {
142 /* prepare a temporary file to write the environment to */
143 _cleanup_close_ int fd = mkostemp_safe(p);
144 assert_se(fd >= 0);
145 }
146
095b30cb 147 r = write_env_file(p, a);
98f59e59
HH
148 assert_se(r >= 0);
149
aa8fbc74 150 r = load_env_file(NULL, p, &b);
98f59e59 151 assert_se(r >= 0);
f73141d7
LP
152}
153
df8b14b5
LP
154static void test_one_shell_var(const char *file, const char *variable, const char *value) {
155 _cleanup_free_ char *cmd = NULL, *from_shell = NULL;
649bde89 156 _cleanup_pclose_ FILE *f = NULL;
df8b14b5
LP
157 size_t sz;
158
159 assert_se(cmd = strjoin(". ", file, " && /bin/echo -n \"$", variable, "\""));
160 assert_se(f = popen(cmd, "re"));
161 assert_se(read_full_stream(f, &from_shell, &sz) >= 0);
162 assert_se(sz == strlen(value));
163 assert_se(streq(from_shell, value));
164}
165
4f7452a8 166TEST(parse_multiline_env_file) {
627d2bac
ZJS
167 _cleanup_(unlink_tempfilep) char
168 t[] = "/tmp/test-fileio-in-XXXXXX",
ac4c8d6d 169 p[] = "/tmp/test-fileio-out-XXXXXX";
ac4c8d6d
ZJS
170 FILE *f;
171 _cleanup_strv_free_ char **a = NULL, **b = NULL;
d8351049 172 int r;
ac4c8d6d 173
d8351049 174 assert_se(fmkostemp_safe(t, "w", &f) == 0);
ac4c8d6d 175 fputs("one=BAR\\\n"
6fe31963
LP
176 "\\ \\ \\ \\ VAR\\\n"
177 "\\\tGAR\n"
ac4c8d6d
ZJS
178 "#comment\n"
179 "two=\"bar\\\n"
180 " var\\\n"
181 "\tgar\"\n"
182 "#comment\n"
183 "tri=\"bar \\\n"
184 " var \\\n"
185 "\tgar \"\n", f);
186
6fe31963 187 assert_se(fflush_and_check(f) >= 0);
ac4c8d6d
ZJS
188 fclose(f);
189
df8b14b5
LP
190 test_one_shell_var(t, "one", "BAR VAR\tGAR");
191 test_one_shell_var(t, "two", "bar var\tgar");
192 test_one_shell_var(t, "tri", "bar var \tgar ");
193
aa8fbc74 194 r = load_env_file(NULL, t, &a);
ac4c8d6d
ZJS
195 assert_se(r >= 0);
196
197 STRV_FOREACH(i, a)
198 log_info("Got: <%s>", *i);
199
5fba7bbf
TA
200 assert_se(streq_ptr(a[0], "one=BAR VAR\tGAR"));
201 assert_se(streq_ptr(a[1], "two=bar var\tgar"));
202 assert_se(streq_ptr(a[2], "tri=bar var \tgar "));
ac4c8d6d
ZJS
203 assert_se(a[3] == NULL);
204
d8351049
ZJS
205 {
206 _cleanup_close_ int fd = mkostemp_safe(p);
207 assert_se(fd >= 0);
208 }
209
ac4c8d6d
ZJS
210 r = write_env_file(p, a);
211 assert_se(r >= 0);
212
aa8fbc74 213 r = load_env_file(NULL, p, &b);
ac4c8d6d 214 assert_se(r >= 0);
ac4c8d6d
ZJS
215}
216
4f7452a8 217TEST(merge_env_file) {
627d2bac 218 _cleanup_(unlink_tempfilep) char t[] = "/tmp/test-fileio-XXXXXX";
9707d552 219 _cleanup_fclose_ FILE *f = NULL;
37f3ffca 220 _cleanup_strv_free_ char **a = NULL;
d8351049 221 int r;
37f3ffca 222
d8351049 223 assert_se(fmkostemp_safe(t, "w", &f) == 0);
37f3ffca
RS
224 log_info("/* %s (%s) */", __func__, t);
225
37f3ffca
RS
226 r = write_string_stream(f,
227 "one=1 \n"
228 "twelve=${one}2\n"
229 "twentyone=2${one}\n"
230 "one=2\n"
ccad1fd0
ZJS
231 "twentytwo=2${one}\n"
232 "xxx_minus_three=$xxx - 3\n"
233 "xxx=0x$one$one$one\n"
b82f58bf
RS
234 "yyy=${one:-fallback}\n"
235 "zzz=${one:+replacement}\n"
236 "zzzz=${foobar:-${nothing}}\n"
237 "zzzzz=${nothing:+${nothing}}\n"
b1837133 238 , WRITE_STRING_FILE_AVOID_NEWLINE);
f21b863e 239 assert_se(r >= 0);
37f3ffca
RS
240
241 r = merge_env_file(&a, NULL, t);
242 assert_se(r >= 0);
243 strv_sort(a);
244
245 STRV_FOREACH(i, a)
246 log_info("Got: <%s>", *i);
247
248 assert_se(streq(a[0], "one=2"));
249 assert_se(streq(a[1], "twelve=12"));
250 assert_se(streq(a[2], "twentyone=21"));
251 assert_se(streq(a[3], "twentytwo=22"));
ccad1fd0
ZJS
252 assert_se(streq(a[4], "xxx=0x222"));
253 assert_se(streq(a[5], "xxx_minus_three= - 3"));
b82f58bf
RS
254 assert_se(streq(a[6], "yyy=2"));
255 assert_se(streq(a[7], "zzz=replacement"));
256 assert_se(streq(a[8], "zzzz="));
257 assert_se(streq(a[9], "zzzzz="));
258 assert_se(a[10] == NULL);
37f3ffca
RS
259
260 r = merge_env_file(&a, NULL, t);
261 assert_se(r >= 0);
262 strv_sort(a);
263
264 STRV_FOREACH(i, a)
265 log_info("Got2: <%s>", *i);
266
267 assert_se(streq(a[0], "one=2"));
268 assert_se(streq(a[1], "twelve=12"));
269 assert_se(streq(a[2], "twentyone=21"));
270 assert_se(streq(a[3], "twentytwo=22"));
ccad1fd0
ZJS
271 assert_se(streq(a[4], "xxx=0x222"));
272 assert_se(streq(a[5], "xxx_minus_three=0x222 - 3"));
b82f58bf
RS
273 assert_se(streq(a[6], "yyy=2"));
274 assert_se(streq(a[7], "zzz=replacement"));
275 assert_se(streq(a[8], "zzzz="));
276 assert_se(streq(a[9], "zzzzz="));
277 assert_se(a[10] == NULL);
37f3ffca 278}
ac4c8d6d 279
4f7452a8 280TEST(merge_env_file_invalid) {
627d2bac 281 _cleanup_(unlink_tempfilep) char t[] = "/tmp/test-fileio-XXXXXX";
9707d552 282 _cleanup_fclose_ FILE *f = NULL;
184d1904 283 _cleanup_strv_free_ char **a = NULL;
d8351049 284 int r;
184d1904 285
d8351049 286 assert_se(fmkostemp_safe(t, "w", &f) == 0);
184d1904
ZJS
287 log_info("/* %s (%s) */", __func__, t);
288
184d1904
ZJS
289 r = write_string_stream(f,
290 "unset one \n"
291 "unset one= \n"
292 "unset one=1 \n"
293 "one \n"
294 "one = \n"
295 "one two =\n"
296 "\x20two=\n"
297 "#comment=comment\n"
298 ";comment2=comment2\n"
299 "#\n"
300 "\n\n" /* empty line */
b1837133 301 , WRITE_STRING_FILE_AVOID_NEWLINE);
f21b863e 302 assert_se(r >= 0);
184d1904
ZJS
303
304 r = merge_env_file(&a, NULL, t);
305 assert_se(r >= 0);
306
307 STRV_FOREACH(i, a)
308 log_info("Got: <%s>", *i);
309
310 assert_se(strv_isempty(a));
311}
312
4f7452a8 313TEST(executable_is_script) {
627d2bac 314 _cleanup_(unlink_tempfilep) char t[] = "/tmp/test-fileio-XXXXXX";
627d2bac 315 _cleanup_fclose_ FILE *f = NULL;
68fee104 316 char *command;
d8351049 317 int r;
68fee104 318
d8351049 319 assert_se(fmkostemp_safe(t, "w", &f) == 0);
68fee104
ZJS
320 fputs("#! /bin/script -a -b \ngoo goo", f);
321 fflush(f);
322
323 r = executable_is_script(t, &command);
324 assert_se(r > 0);
325 assert_se(streq(command, "/bin/script"));
326 free(command);
327
328 r = executable_is_script("/bin/sh", &command);
329 assert_se(r == 0);
330
331 r = executable_is_script("/usr/bin/yum", &command);
332 assert_se(r > 0 || r == -ENOENT);
333 if (r > 0) {
334 assert_se(startswith(command, "/"));
335 free(command);
336 }
68fee104
ZJS
337}
338
4f7452a8 339TEST(status_field) {
6aa90884 340 _cleanup_free_ char *p = NULL, *s = NULL, *z = NULL;
1e5413f7 341 unsigned long long total = 0, buffers = 0;
442e0083 342 int r;
69ab8088 343
c4cd1d4d 344 r = get_proc_field("/proc/meminfo", "MemTotal", WHITESPACE, &p);
1e5413f7 345 if (r != -ENOENT) {
bdf7026e 346 assert_se(r == 0);
1e5413f7
ZJS
347 puts(p);
348 assert_se(safe_atollu(p, &total) == 0);
349 }
69ab8088 350
c4cd1d4d 351 r = get_proc_field("/proc/meminfo", "Buffers", WHITESPACE, &s);
1e5413f7 352 if (r != -ENOENT) {
bdf7026e 353 assert_se(r == 0);
1e5413f7
ZJS
354 puts(s);
355 assert_se(safe_atollu(s, &buffers) == 0);
356 }
69ab8088 357
2b01a801 358 if (p)
bdf7026e 359 assert_se(buffers < total);
1e5413f7
ZJS
360
361 /* Seccomp should be a good test for field full of zeros. */
c4cd1d4d 362 r = get_proc_field("/proc/meminfo", "Seccomp", WHITESPACE, &z);
1e5413f7 363 if (r != -ENOENT) {
bdf7026e 364 assert_se(r == 0);
1e5413f7
ZJS
365 puts(z);
366 assert_se(safe_atollu(z, &buffers) == 0);
367 }
368}
369
4f7452a8 370TEST(capeff) {
d12ccbc3 371 for (int pid = 0; pid < 2; pid++) {
1e5413f7 372 _cleanup_free_ char *capeff = NULL;
d12ccbc3 373 int r, p;
1e5413f7
ZJS
374
375 r = get_process_capeff(0, &capeff);
376 log_info("capeff: '%s' (r=%d)", capeff, r);
377
4c701096 378 if (IN_SET(r, -ENOENT, -EPERM))
1e5413f7
ZJS
379 return;
380
bdf7026e
TA
381 assert_se(r == 0);
382 assert_se(*capeff);
1a7906ae 383 p = capeff[strspn(capeff, HEXDIGITS)];
bdf7026e 384 assert_se(!p || isspace(p));
1e5413f7 385 }
69ab8088
ZJS
386}
387
d175b709
ZJS
388TEST(read_one_line_file) {
389 _cleanup_(unlink_tempfilep) char fn[] = "/tmp/test-fileio-1lf-XXXXXX";
390 int fd;
391 _cleanup_fclose_ FILE *f = NULL;
392 _cleanup_free_ char *buf, *buf2, *buf3, *buf4, *buf5;
393
394 fd = mkostemp_safe(fn);
395 assert_se(fd >= 0);
396
397 f = fdopen(fd, "we");
398 assert_se(f);
399
400 assert_se(read_one_line_file(fn, &buf) == 0);
401 assert_se(streq_ptr(buf, ""));
402 assert_se(read_one_line_file(fn, &buf2) == 0);
403 assert_se(streq_ptr(buf2, ""));
404
405 assert_se(write_string_stream(f, "x", WRITE_STRING_FILE_AVOID_NEWLINE) >= 0);
406 fflush(f);
407
408 assert_se(read_one_line_file(fn, &buf3) == 1);
409 assert_se(streq_ptr(buf3, "x"));
410
411 assert_se(write_string_stream(f, "\n", WRITE_STRING_FILE_AVOID_NEWLINE) >= 0);
412 fflush(f);
413
414 assert_se(read_one_line_file(fn, &buf4) == 2);
415 assert_se(streq_ptr(buf4, "x"));
416
417 assert_se(write_string_stream(f, "\n", WRITE_STRING_FILE_AVOID_NEWLINE) >= 0);
418 fflush(f);
419
420 assert_se(read_one_line_file(fn, &buf5) == 2);
421 assert_se(streq_ptr(buf5, "x"));
422}
423
4f7452a8 424TEST(write_string_stream) {
627d2bac
ZJS
425 _cleanup_(unlink_tempfilep) char fn[] = "/tmp/test-write_string_stream-XXXXXX";
426 _cleanup_fclose_ FILE *f = NULL;
0709b743
RC
427 int fd;
428 char buf[64];
429
646853bd 430 fd = mkostemp_safe(fn);
0709b743
RC
431 assert_se(fd >= 0);
432
433 f = fdopen(fd, "r");
434 assert_se(f);
b1837133 435 assert_se(write_string_stream(f, "boohoo", 0) < 0);
36297390 436 f = safe_fclose(f);
0709b743 437
36297390 438 f = fopen(fn, "r+");
0709b743
RC
439 assert_se(f);
440
b1837133 441 assert_se(write_string_stream(f, "boohoo", 0) == 0);
0709b743
RC
442 rewind(f);
443
444 assert_se(fgets(buf, sizeof(buf), f));
445 assert_se(streq(buf, "boohoo\n"));
36297390 446 f = safe_fclose(f);
0709b743 447
36297390 448 f = fopen(fn, "w+");
40beecdb
DM
449 assert_se(f);
450
b1837133 451 assert_se(write_string_stream(f, "boohoo", WRITE_STRING_FILE_AVOID_NEWLINE) == 0);
40beecdb
DM
452 rewind(f);
453
454 assert_se(fgets(buf, sizeof(buf), f));
455 printf(">%s<", buf);
456 assert_se(streq(buf, "boohoo"));
0709b743
RC
457}
458
4f7452a8 459TEST(write_string_file) {
627d2bac 460 _cleanup_(unlink_tempfilep) char fn[] = "/tmp/test-write_string_file-XXXXXX";
76082570
RC
461 char buf[64] = {};
462 _cleanup_close_ int fd;
0709b743 463
646853bd 464 fd = mkostemp_safe(fn);
0709b743
RC
465 assert_se(fd >= 0);
466
4c1fc3e4 467 assert_se(write_string_file(fn, "boohoo", WRITE_STRING_FILE_CREATE) == 0);
0709b743 468
cca0efb0 469 assert_se(read(fd, buf, sizeof(buf)) == 7);
0709b743 470 assert_se(streq(buf, "boohoo\n"));
0709b743
RC
471}
472
4f7452a8 473TEST(write_string_file_no_create) {
627d2bac 474 _cleanup_(unlink_tempfilep) char fn[] = "/tmp/test-write_string_file_no_create-XXXXXX";
e07995a3 475 _cleanup_close_ int fd;
2aa07538 476 char buf[64] = {};
e07995a3 477
646853bd 478 fd = mkostemp_safe(fn);
e07995a3
RC
479 assert_se(fd >= 0);
480
4c1fc3e4
DM
481 assert_se(write_string_file("/a/file/which/does/not/exists/i/guess", "boohoo", 0) < 0);
482 assert_se(write_string_file(fn, "boohoo", 0) == 0);
e07995a3 483
2aa07538 484 assert_se(read(fd, buf, sizeof buf) == (ssize_t) strlen("boohoo\n"));
e07995a3 485 assert_se(streq(buf, "boohoo\n"));
e07995a3
RC
486}
487
4f7452a8 488TEST(write_string_file_verify) {
eb3da901
LP
489 _cleanup_free_ char *buf = NULL, *buf2 = NULL;
490 int r;
491
3c14dc61
TM
492 r = read_one_line_file("/proc/version", &buf);
493 if (ERRNO_IS_PRIVILEGE(r))
494 return;
495 assert_se(r >= 0);
dcd6361e 496 assert_se(buf2 = strjoin(buf, "\n"));
eb3da901 497
30b84c78 498 r = write_string_file("/proc/version", buf, 0);
4c701096 499 assert_se(IN_SET(r, -EACCES, -EIO));
30b84c78 500 r = write_string_file("/proc/version", buf2, 0);
4c701096 501 assert_se(IN_SET(r, -EACCES, -EIO));
eb3da901 502
30b84c78
ZJS
503 assert_se(write_string_file("/proc/version", buf, WRITE_STRING_FILE_VERIFY_ON_FAILURE) == 0);
504 assert_se(write_string_file("/proc/version", buf2, WRITE_STRING_FILE_VERIFY_ON_FAILURE) == 0);
eb3da901 505
30b84c78 506 r = write_string_file("/proc/version", buf, WRITE_STRING_FILE_VERIFY_ON_FAILURE|WRITE_STRING_FILE_AVOID_NEWLINE);
4c701096 507 assert_se(IN_SET(r, -EACCES, -EIO));
30b84c78 508 assert_se(write_string_file("/proc/version", buf2, WRITE_STRING_FILE_VERIFY_ON_FAILURE|WRITE_STRING_FILE_AVOID_NEWLINE) == 0);
eb3da901
LP
509}
510
4f7452a8 511TEST(load_env_file_pairs) {
627d2bac
ZJS
512 _cleanup_(unlink_tempfilep) char fn[] = "/tmp/test-load_env_file_pairs-XXXXXX";
513 int fd, r;
e07995a3
RC
514 _cleanup_fclose_ FILE *f = NULL;
515 _cleanup_strv_free_ char **l = NULL;
e07995a3 516
646853bd 517 fd = mkostemp_safe(fn);
e07995a3
RC
518 assert_se(fd >= 0);
519
520 r = write_string_file(fn,
521 "NAME=\"Arch Linux\"\n"
522 "ID=arch\n"
523 "PRETTY_NAME=\"Arch Linux\"\n"
524 "ANSI_COLOR=\"0;36\"\n"
525 "HOME_URL=\"https://www.archlinux.org/\"\n"
526 "SUPPORT_URL=\"https://bbs.archlinux.org/\"\n"
4c1fc3e4
DM
527 "BUG_REPORT_URL=\"https://bugs.archlinux.org/\"\n",
528 WRITE_STRING_FILE_CREATE);
e07995a3
RC
529 assert_se(r == 0);
530
531 f = fdopen(fd, "r");
532 assert_se(f);
533
aa8fbc74 534 r = load_env_file_pairs(f, fn, &l);
e07995a3
RC
535 assert_se(r >= 0);
536
537 assert_se(strv_length(l) == 14);
538 STRV_FOREACH_PAIR(k, v, l) {
539 assert_se(STR_IN_SET(*k, "NAME", "ID", "PRETTY_NAME", "ANSI_COLOR", "HOME_URL", "SUPPORT_URL", "BUG_REPORT_URL"));
540 printf("%s=%s\n", *k, *v);
541 if (streq(*k, "NAME")) assert_se(streq(*v, "Arch Linux"));
542 if (streq(*k, "ID")) assert_se(streq(*v, "arch"));
543 if (streq(*k, "PRETTY_NAME")) assert_se(streq(*v, "Arch Linux"));
544 if (streq(*k, "ANSI_COLOR")) assert_se(streq(*v, "0;36"));
545 if (streq(*k, "HOME_URL")) assert_se(streq(*v, "https://www.archlinux.org/"));
546 if (streq(*k, "SUPPORT_URL")) assert_se(streq(*v, "https://bbs.archlinux.org/"));
547 if (streq(*k, "BUG_REPORT_URL")) assert_se(streq(*v, "https://bugs.archlinux.org/"));
548 }
e07995a3
RC
549}
550
4f7452a8 551TEST(search_and_fopen) {
2708160c
LP
552 static const char* const dirs[] = {
553 "/tmp/foo/bar",
554 "/tmp",
555 NULL
556 };
897891f0 557 char name[] = "/tmp/test-search_and_fopen.XXXXXX";
2708160c
LP
558 _cleanup_fclose_ FILE *f = NULL;
559 _cleanup_free_ char *p = NULL;
254d1313 560 _cleanup_close_ int fd = -EBADF;
2708160c
LP
561 const char *e;
562 int r;
897891f0 563
646853bd 564 fd = mkostemp_safe(name);
897891f0 565 assert_se(fd >= 0);
2708160c 566 fd = safe_close(fd);
897891f0 567
2708160c 568 r = search_and_fopen(basename(name), "re", NULL, (const char**) dirs, &f, &p);
897891f0 569 assert_se(r >= 0);
2708160c
LP
570 assert_se(e = path_startswith(p, "/tmp/"));
571 assert_se(streq(basename(name), e));
572 f = safe_fclose(f);
573 p = mfree(p);
897891f0 574
2708160c 575 r = search_and_fopen(name, "re", NULL, (const char**) dirs, &f, &p);
897891f0 576 assert_se(r >= 0);
2708160c
LP
577 assert_se(path_equal(name, p));
578 f = safe_fclose(f);
579 p = mfree(p);
897891f0 580
2708160c 581 r = search_and_fopen(basename(name), "re", "/", (const char**) dirs, &f, &p);
897891f0 582 assert_se(r >= 0);
2708160c
LP
583 assert_se(e = path_startswith(p, "/tmp/"));
584 assert_se(streq(basename(name), e));
585 f = safe_fclose(f);
586 p = mfree(p);
897891f0 587
a3ee0916 588 r = search_and_fopen("/a/file/which/does/not/exist/i/guess", "re", NULL, (const char**) dirs, &f, &p);
2708160c 589 assert_se(r == -ENOENT);
a3ee0916 590 r = search_and_fopen("afilewhichdoesnotexistiguess", "re", NULL, (const char**) dirs, &f, &p);
2708160c 591 assert_se(r == -ENOENT);
897891f0
RC
592
593 r = unlink(name);
594 assert_se(r == 0);
595
a3ee0916 596 r = search_and_fopen(basename(name), "re", NULL, (const char**) dirs, &f, &p);
2708160c 597 assert_se(r == -ENOENT);
897891f0
RC
598}
599
4f7452a8 600TEST(search_and_fopen_nulstr) {
2708160c
LP
601 static const char dirs[] =
602 "/tmp/foo/bar\0"
603 "/tmp\0";
627d2bac
ZJS
604
605 _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-search_and_fopen.XXXXXX";
2708160c
LP
606 _cleanup_fclose_ FILE *f = NULL;
607 _cleanup_free_ char *p = NULL;
254d1313 608 _cleanup_close_ int fd = -EBADF;
2708160c
LP
609 const char *e;
610 int r;
897891f0 611
646853bd 612 fd = mkostemp_safe(name);
897891f0 613 assert_se(fd >= 0);
2708160c 614 fd = safe_close(fd);
897891f0 615
2708160c 616 r = search_and_fopen_nulstr(basename(name), "re", NULL, dirs, &f, &p);
897891f0 617 assert_se(r >= 0);
2708160c
LP
618 assert_se(e = path_startswith(p, "/tmp/"));
619 assert_se(streq(basename(name), e));
620 f = safe_fclose(f);
621 p = mfree(p);
897891f0 622
2708160c 623 r = search_and_fopen_nulstr(name, "re", NULL, dirs, &f, &p);
897891f0 624 assert_se(r >= 0);
2708160c
LP
625 assert_se(path_equal(name, p));
626 f = safe_fclose(f);
627 p = mfree(p);
897891f0 628
a3ee0916 629 r = search_and_fopen_nulstr("/a/file/which/does/not/exist/i/guess", "re", NULL, dirs, &f, &p);
2708160c 630 assert_se(r == -ENOENT);
a3ee0916 631 r = search_and_fopen_nulstr("afilewhichdoesnotexistiguess", "re", NULL, dirs, &f, &p);
2708160c 632 assert_se(r == -ENOENT);
897891f0
RC
633
634 r = unlink(name);
635 assert_se(r == 0);
636
a3ee0916 637 r = search_and_fopen_nulstr(basename(name), "re", NULL, dirs, &f, &p);
2708160c 638 assert_se(r == -ENOENT);
897891f0
RC
639}
640
4f7452a8 641TEST(writing_tmpfile) {
627d2bac 642 _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-systemd_writing_tmpfile.XXXXXX";
897891f0
RC
643 _cleanup_free_ char *contents = NULL;
644 size_t size;
254d1313 645 _cleanup_close_ int fd = -EBADF;
627d2bac 646 int r;
897891f0 647
d12ccbc3
ZJS
648 struct iovec iov[] = {
649 IOVEC_MAKE_STRING("abc\n"),
650 IOVEC_MAKE_STRING(ALPHANUMERICAL "\n"),
651 IOVEC_MAKE_STRING(""),
652 };
897891f0 653
646853bd 654 fd = mkostemp_safe(name);
897891f0
RC
655 printf("tmpfile: %s", name);
656
657 r = writev(fd, iov, 3);
658 assert_se(r >= 0);
659
660 r = read_full_file(name, &contents, &size);
661 assert_se(r == 0);
662 printf("contents: %s", contents);
663 assert_se(streq(contents, "abc\n" ALPHANUMERICAL "\n"));
897891f0
RC
664}
665
4f7452a8 666TEST(tempfn) {
897891f0
RC
667 char *ret = NULL, *p;
668
669 assert_se(tempfn_xxxxxx("/foo/bar/waldo", NULL, &ret) >= 0);
670 assert_se(streq_ptr(ret, "/foo/bar/.#waldoXXXXXX"));
671 free(ret);
672
673 assert_se(tempfn_xxxxxx("/foo/bar/waldo", "[miau]", &ret) >= 0);
674 assert_se(streq_ptr(ret, "/foo/bar/.#[miau]waldoXXXXXX"));
675 free(ret);
676
677 assert_se(tempfn_random("/foo/bar/waldo", NULL, &ret) >= 0);
678 assert_se(p = startswith(ret, "/foo/bar/.#waldo"));
679 assert_se(strlen(p) == 16);
680 assert_se(in_charset(p, "0123456789abcdef"));
681 free(ret);
682
683 assert_se(tempfn_random("/foo/bar/waldo", "[wuff]", &ret) >= 0);
684 assert_se(p = startswith(ret, "/foo/bar/.#[wuff]waldo"));
685 assert_se(strlen(p) == 16);
686 assert_se(in_charset(p, "0123456789abcdef"));
687 free(ret);
688
689 assert_se(tempfn_random_child("/foo/bar/waldo", NULL, &ret) >= 0);
690 assert_se(p = startswith(ret, "/foo/bar/waldo/.#"));
691 assert_se(strlen(p) == 16);
692 assert_se(in_charset(p, "0123456789abcdef"));
693 free(ret);
694
695 assert_se(tempfn_random_child("/foo/bar/waldo", "[kikiriki]", &ret) >= 0);
696 assert_se(p = startswith(ret, "/foo/bar/waldo/.#[kikiriki]"));
697 assert_se(strlen(p) == 16);
698 assert_se(in_charset(p, "0123456789abcdef"));
699 free(ret);
700}
701
7f9d1aed 702static const char chars[] =
cdce33f9 703 "Aąę„”\n루\377";
7f9d1aed 704
6028d766 705DISABLE_WARNING_TYPE_LIMITS;
d3bdba38 706
4f7452a8 707TEST(fgetc) {
7f9d1aed
ZJS
708 _cleanup_fclose_ FILE *f = NULL;
709 char c;
710
f3bd4b3d 711 assert_se(f = fmemopen_unlocked((void*) chars, sizeof(chars), "r"));
7f9d1aed 712
72fd79b3 713 for (size_t i = 0; i < sizeof(chars); i++) {
7f9d1aed
ZJS
714 assert_se(safe_fgetc(f, &c) == 1);
715 assert_se(c == chars[i]);
716
72fd79b3
LP
717 if (ungetc(c, f) == EOF) {
718 /* EOF is -1, and hence we can't push value 255 in this way – if char is signed */
719 assert_se(c == (char) EOF);
720 assert_se(CHAR_MIN == -128); /* verify that char is signed on this platform */
721 } else {
722 assert_se(safe_fgetc(f, &c) == 1);
723 assert_se(c == chars[i]);
724 }
7f9d1aed 725
cdce33f9 726 /* But it works when we push it properly cast */
7f9d1aed
ZJS
727 assert_se(ungetc((unsigned char) c, f) != EOF);
728 assert_se(safe_fgetc(f, &c) == 1);
729 assert_se(c == chars[i]);
730 }
731
732 assert_se(safe_fgetc(f, &c) == 0);
733}
734
6028d766 735REENABLE_WARNING;
d3bdba38 736
2c9de139
ZJS
737static const char buffer[] =
738 "Some test data\n"
7f9d1aed 739 "루Non-ascii chars: ąę„”\n"
838894b0
LP
740 "terminators\r\n"
741 "and even more\n\r"
742 "now the same with a NUL\n\0"
743 "and more\r\0"
744 "and even more\r\n\0"
745 "and yet even more\n\r\0"
2c9de139
ZJS
746 "With newlines, and a NUL byte\0"
747 "\n"
748 "an empty line\n"
749 "an ignored line\n"
750 "and a very long line that is supposed to be truncated, because it is so long\n";
751
752static void test_read_line_one_file(FILE *f) {
4f9a66a3
LP
753 _cleanup_free_ char *line = NULL;
754
f5fbe71d 755 assert_se(read_line(f, SIZE_MAX, &line) == 15 && streq(line, "Some test data"));
4f9a66a3
LP
756 line = mfree(line);
757
f5fbe71d 758 assert_se(read_line(f, SIZE_MAX, &line) > 0 && streq(line, "루Non-ascii chars: ąę„”"));
838894b0
LP
759 line = mfree(line);
760
f5fbe71d 761 assert_se(read_line(f, SIZE_MAX, &line) == 13 && streq(line, "terminators"));
838894b0
LP
762 line = mfree(line);
763
f5fbe71d 764 assert_se(read_line(f, SIZE_MAX, &line) == 15 && streq(line, "and even more"));
838894b0
LP
765 line = mfree(line);
766
f5fbe71d 767 assert_se(read_line(f, SIZE_MAX, &line) == 25 && streq(line, "now the same with a NUL"));
838894b0
LP
768 line = mfree(line);
769
f5fbe71d 770 assert_se(read_line(f, SIZE_MAX, &line) == 10 && streq(line, "and more"));
838894b0
LP
771 line = mfree(line);
772
f5fbe71d 773 assert_se(read_line(f, SIZE_MAX, &line) == 16 && streq(line, "and even more"));
838894b0
LP
774 line = mfree(line);
775
f5fbe71d 776 assert_se(read_line(f, SIZE_MAX, &line) == 20 && streq(line, "and yet even more"));
838894b0
LP
777 line = mfree(line);
778
4f9a66a3
LP
779 assert_se(read_line(f, 1024, &line) == 30 && streq(line, "With newlines, and a NUL byte"));
780 line = mfree(line);
781
782 assert_se(read_line(f, 1024, &line) == 1 && streq(line, ""));
783 line = mfree(line);
784
785 assert_se(read_line(f, 1024, &line) == 14 && streq(line, "an empty line"));
786 line = mfree(line);
787
f5fbe71d 788 assert_se(read_line(f, SIZE_MAX, NULL) == 16);
4f9a66a3
LP
789
790 assert_se(read_line(f, 16, &line) == -ENOBUFS);
791 line = mfree(line);
792
793 /* read_line() stopped when it hit the limit, that means when we continue reading we'll read at the first
5238e957 794 * character after the previous limit. Let's make use of that to continue our test. */
838894b0 795 assert_se(read_line(f, 1024, &line) == 62 && streq(line, "line that is supposed to be truncated, because it is so long"));
4f9a66a3
LP
796 line = mfree(line);
797
798 assert_se(read_line(f, 1024, &line) == 0 && streq(line, ""));
799}
800
4f7452a8 801TEST(read_line1) {
2c9de139 802 _cleanup_fclose_ FILE *f = NULL;
2c9de139 803
f3bd4b3d 804 assert_se(f = fmemopen_unlocked((void*) buffer, sizeof(buffer), "r"));
2c9de139
ZJS
805 test_read_line_one_file(f);
806}
807
4f7452a8 808TEST(read_line2) {
627d2bac 809 _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-fileio.XXXXXX";
2c9de139
ZJS
810 int fd;
811 _cleanup_fclose_ FILE *f = NULL;
812
813 fd = mkostemp_safe(name);
814 assert_se(fd >= 0);
815 assert_se((size_t) write(fd, buffer, sizeof(buffer)) == sizeof(buffer));
816
817 assert_se(lseek(fd, 0, SEEK_SET) == 0);
818 assert_se(f = fdopen(fd, "r"));
819
820 test_read_line_one_file(f);
821}
822
4f7452a8 823TEST(read_line3) {
2c9de139
ZJS
824 _cleanup_fclose_ FILE *f = NULL;
825 _cleanup_free_ char *line = NULL;
826 int r;
827
30b84c78 828 f = fopen("/proc/uptime", "re");
2c9de139
ZJS
829 if (!f && IN_SET(errno, ENOENT, EPERM))
830 return;
831 assert_se(f);
832
833 r = read_line(f, LINE_MAX, &line);
b4555637
ZJS
834 assert_se(r >= 0);
835 if (r == 0)
836 assert_se(line && isempty(line));
837 else
838 assert_se((size_t) r == strlen(line) + 1);
2c9de139
ZJS
839 assert_se(read_line(f, LINE_MAX, NULL) == 0);
840}
841
4f7452a8 842TEST(read_line4) {
6baac700
LP
843 static const struct {
844 size_t length;
845 const char *string;
846 } eof_endings[] = {
847 /* Each of these will be followed by EOF and should generate the one same single string */
848 { 3, "foo" },
849 { 4, "foo\n" },
850 { 4, "foo\r" },
851 { 4, "foo\0" },
852 { 5, "foo\n\0" },
853 { 5, "foo\r\0" },
854 { 5, "foo\r\n" },
855 { 5, "foo\n\r" },
856 { 6, "foo\r\n\0" },
857 { 6, "foo\n\r\0" },
858 };
859
6baac700
LP
860 int r;
861
d12ccbc3 862 for (size_t i = 0; i < ELEMENTSOF(eof_endings); i++) {
6baac700
LP
863 _cleanup_fclose_ FILE *f = NULL;
864 _cleanup_free_ char *s = NULL;
865
673a1e6f 866 assert_se(f = fmemopen_unlocked((void*) eof_endings[i].string, eof_endings[i].length, "r"));
6baac700 867
f5fbe71d 868 r = read_line(f, SIZE_MAX, &s);
6baac700
LP
869 assert_se((size_t) r == eof_endings[i].length);
870 assert_se(streq_ptr(s, "foo"));
871
f5fbe71d 872 assert_se(read_line(f, SIZE_MAX, NULL) == 0); /* Ensure we hit EOF */
6baac700
LP
873 }
874}
875
4f7452a8 876TEST(read_nul_string) {
3946d576
LP
877 static const char test[] = "string nr. 1\0"
878 "string nr. 2\n\0"
517b7760 879 "\377empty string follows\0"
3946d576
LP
880 "\0"
881 "final string\n is empty\0"
882 "\0";
883
884 _cleanup_fclose_ FILE *f = NULL;
885 _cleanup_free_ char *s = NULL;
886
673a1e6f 887 assert_se(f = fmemopen_unlocked((void*) test, sizeof(test)-1, "r"));
3946d576
LP
888
889 assert_se(read_nul_string(f, LONG_LINE_MAX, &s) == 13 && streq_ptr(s, "string nr. 1"));
890 s = mfree(s);
891
892 assert_se(read_nul_string(f, LONG_LINE_MAX, &s) == 14 && streq_ptr(s, "string nr. 2\n"));
893 s = mfree(s);
894
517b7760 895 assert_se(read_nul_string(f, LONG_LINE_MAX, &s) == 22 && streq_ptr(s, "\377empty string follows"));
3946d576
LP
896 s = mfree(s);
897
898 assert_se(read_nul_string(f, LONG_LINE_MAX, &s) == 1 && streq_ptr(s, ""));
899 s = mfree(s);
900
901 assert_se(read_nul_string(f, LONG_LINE_MAX, &s) == 23 && streq_ptr(s, "final string\n is empty"));
902 s = mfree(s);
903
904 assert_se(read_nul_string(f, LONG_LINE_MAX, &s) == 1 && streq_ptr(s, ""));
905 s = mfree(s);
906
907 assert_se(read_nul_string(f, LONG_LINE_MAX, &s) == 0 && streq_ptr(s, ""));
908}
909
4f7452a8 910TEST(read_full_file_socket) {
b93d3f6b 911 _cleanup_(rm_rf_physical_and_freep) char *z = NULL;
5bb1d7fb 912 _cleanup_close_ int listener = -EBADF;
d3dcf4e3 913 _cleanup_free_ char *data = NULL, *clientname = NULL;
b93d3f6b 914 union sockaddr_union sa;
28ae8da9 915 const char *j, *jj;
b93d3f6b
LP
916 size_t size;
917 pid_t pid;
918 int r;
919
b93d3f6b
LP
920 listener = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0);
921 assert_se(listener >= 0);
922
923 assert_se(mkdtemp_malloc(NULL, &z) >= 0);
924 j = strjoina(z, "/socket");
925
926 assert_se(sockaddr_un_set_path(&sa.un, j) >= 0);
927
928 assert_se(bind(listener, &sa.sa, SOCKADDR_UN_LEN(sa.un)) >= 0);
929 assert_se(listen(listener, 1) >= 0);
930
28ae8da9
LP
931 /* Make sure the socket doesn't fit into a struct sockaddr_un, but we can still access it */
932 jj = strjoina(z, "/a_very_long_patha_very_long_patha_very_long_patha_very_long_patha_very_long_patha_very_long_patha_very_long_patha_very_long_path");
933 assert_se(strlen(jj) > sizeof_field(struct sockaddr_un, sun_path));
934 assert_se(rename(j, jj) >= 0);
935
d3dcf4e3
LP
936 /* Bind the *client* socket to some randomized name, to verify that this works correctly. */
937 assert_se(asprintf(&clientname, "@%" PRIx64 "/test-bindname", random_u64()) >= 0);
938
b93d3f6b
LP
939 r = safe_fork("(server)", FORK_DEATHSIG|FORK_LOG, &pid);
940 assert_se(r >= 0);
941 if (r == 0) {
d3dcf4e3
LP
942 union sockaddr_union peer = {};
943 socklen_t peerlen = sizeof(peer);
254d1313 944 _cleanup_close_ int rfd = -EBADF;
b93d3f6b
LP
945 /* child */
946
947 rfd = accept4(listener, NULL, 0, SOCK_CLOEXEC);
948 assert_se(rfd >= 0);
949
d3dcf4e3
LP
950 assert_se(getpeername(rfd, &peer.sa, &peerlen) >= 0);
951
952 assert_se(peer.un.sun_family == AF_UNIX);
953 assert_se(peerlen > offsetof(struct sockaddr_un, sun_path));
954 assert_se(peer.un.sun_path[0] == 0);
955 assert_se(streq(peer.un.sun_path + 1, clientname + 1));
956
b93d3f6b
LP
957#define TEST_STR "This is a test\nreally."
958
959 assert_se(write(rfd, TEST_STR, strlen(TEST_STR)) == strlen(TEST_STR));
960 _exit(EXIT_SUCCESS);
961 }
962
28ae8da9
LP
963 assert_se(read_full_file_full(AT_FDCWD, jj, UINT64_MAX, SIZE_MAX, 0, NULL, &data, &size) == -ENXIO);
964 assert_se(read_full_file_full(AT_FDCWD, jj, UINT64_MAX, SIZE_MAX, READ_FULL_FILE_CONNECT_SOCKET, clientname, &data, &size) >= 0);
b93d3f6b
LP
965 assert_se(size == strlen(TEST_STR));
966 assert_se(streq(data, TEST_STR));
967
968 assert_se(wait_for_terminate_and_check("(server)", pid, WAIT_LOG) >= 0);
969#undef TEST_STR
970}
971
4f7452a8 972TEST(read_full_file_offset_size) {
986311c2
LP
973 _cleanup_fclose_ FILE *f = NULL;
974 _cleanup_(unlink_and_freep) char *fn = NULL;
975 _cleanup_free_ char *rbuf = NULL;
976 size_t rbuf_size;
977 uint8_t buf[4711];
978
979 random_bytes(buf, sizeof(buf));
980
981 assert_se(tempfn_random_child(NULL, NULL, &fn) >= 0);
982 assert_se(f = fopen(fn, "we"));
983 assert_se(fwrite(buf, 1, sizeof(buf), f) == sizeof(buf));
984 assert_se(fflush_and_check(f) >= 0);
985
986 assert_se(read_full_file_full(AT_FDCWD, fn, UINT64_MAX, SIZE_MAX, 0, NULL, &rbuf, &rbuf_size) >= 0);
987 assert_se(rbuf_size == sizeof(buf));
988 assert_se(memcmp(buf, rbuf, rbuf_size) == 0);
989 rbuf = mfree(rbuf);
990
991 assert_se(read_full_file_full(AT_FDCWD, fn, UINT64_MAX, 128, 0, NULL, &rbuf, &rbuf_size) >= 0);
992 assert_se(rbuf_size == 128);
993 assert_se(memcmp(buf, rbuf, rbuf_size) == 0);
994 rbuf = mfree(rbuf);
995
7b0da71d
LP
996 assert_se(read_full_file_full(AT_FDCWD, fn, UINT64_MAX, 128, READ_FULL_FILE_FAIL_WHEN_LARGER, NULL, &rbuf, &rbuf_size) == -E2BIG);
997 assert_se(read_full_file_full(AT_FDCWD, fn, UINT64_MAX, sizeof(buf)-1, READ_FULL_FILE_FAIL_WHEN_LARGER, NULL, &rbuf, &rbuf_size) == -E2BIG);
998 assert_se(read_full_file_full(AT_FDCWD, fn, UINT64_MAX, sizeof(buf), READ_FULL_FILE_FAIL_WHEN_LARGER, NULL, &rbuf, &rbuf_size) >= 0);
999 assert_se(rbuf_size == sizeof(buf));
1000 assert_se(memcmp(buf, rbuf, rbuf_size) == 0);
1001 rbuf = mfree(rbuf);
1002
1003 assert_se(read_full_file_full(AT_FDCWD, fn, 47, 128, READ_FULL_FILE_FAIL_WHEN_LARGER, NULL, &rbuf, &rbuf_size) == -E2BIG);
1004 assert_se(read_full_file_full(AT_FDCWD, fn, 47, sizeof(buf)-47-1, READ_FULL_FILE_FAIL_WHEN_LARGER, NULL, &rbuf, &rbuf_size) == -E2BIG);
1005 assert_se(read_full_file_full(AT_FDCWD, fn, 47, sizeof(buf)-47, READ_FULL_FILE_FAIL_WHEN_LARGER, NULL, &rbuf, &rbuf_size) >= 0);
1006 assert_se(rbuf_size == sizeof(buf)-47);
1007 assert_se(memcmp(buf+47, rbuf, rbuf_size) == 0);
1008 rbuf = mfree(rbuf);
1009
1010 assert_se(read_full_file_full(AT_FDCWD, fn, UINT64_MAX, sizeof(buf)+1, READ_FULL_FILE_FAIL_WHEN_LARGER, NULL, &rbuf, &rbuf_size) >= 0);
1011 assert_se(rbuf_size == sizeof(buf));
1012 assert_se(memcmp(buf, rbuf, rbuf_size) == 0);
1013 rbuf = mfree(rbuf);
1014
986311c2
LP
1015 assert_se(read_full_file_full(AT_FDCWD, fn, 1234, SIZE_MAX, 0, NULL, &rbuf, &rbuf_size) >= 0);
1016 assert_se(rbuf_size == sizeof(buf) - 1234);
1017 assert_se(memcmp(buf + 1234, rbuf, rbuf_size) == 0);
1018 rbuf = mfree(rbuf);
1019
1020 assert_se(read_full_file_full(AT_FDCWD, fn, 2345, 777, 0, NULL, &rbuf, &rbuf_size) >= 0);
1021 assert_se(rbuf_size == 777);
1022 assert_se(memcmp(buf + 2345, rbuf, rbuf_size) == 0);
1023 rbuf = mfree(rbuf);
1024
1025 assert_se(read_full_file_full(AT_FDCWD, fn, 4700, 20, 0, NULL, &rbuf, &rbuf_size) >= 0);
1026 assert_se(rbuf_size == 11);
1027 assert_se(memcmp(buf + 4700, rbuf, rbuf_size) == 0);
1028 rbuf = mfree(rbuf);
1029
1030 assert_se(read_full_file_full(AT_FDCWD, fn, 10000, 99, 0, NULL, &rbuf, &rbuf_size) >= 0);
1031 assert_se(rbuf_size == 0);
1032 rbuf = mfree(rbuf);
1033}
1034
4f7452a8 1035static void test_read_virtual_file_one(size_t max_size) {
bca895c4
ZJS
1036 int r;
1037
d12ccbc3
ZJS
1038 log_info("/* %s (max_size=%zu) */", __func__, max_size);
1039
bca895c4
ZJS
1040 FOREACH_STRING(filename,
1041 "/proc/1/cmdline",
1042 "/etc/nsswitch.conf",
f3b75122
LP
1043 "/sys/kernel/uevent_seqnum",
1044 "/proc/kcore",
1045 "/proc/kallsyms",
1046 "/proc/self/exe",
1047 "/proc/self/pagemap") {
bca895c4
ZJS
1048
1049 _cleanup_free_ char *buf = NULL;
1050 size_t size = 0;
1051
ad0e687c 1052 r = read_virtual_file(filename, max_size, &buf, &size);
8461d6da
LP
1053 if (r < 0) {
1054 log_info_errno(r, "read_virtual_file(\"%s\", %zu): %m", filename, max_size);
f3b75122
LP
1055 assert_se(ERRNO_IS_PRIVILEGE(r) || /* /proc/kcore is not accessible to unpriv */
1056 IN_SET(r,
1057 -ENOENT, /* Some of the files might be absent */
1058 -EINVAL, /* too small reads from /proc/self/pagemap trigger EINVAL */
1059 -EFBIG)); /* /proc/kcore and /proc/self/pagemap should be too large */
8461d6da
LP
1060 } else
1061 log_info("read_virtual_file(\"%s\", %zu): %s (%zu bytes)", filename, max_size, r ? "non-truncated" : "truncated", size);
bca895c4
ZJS
1062 }
1063}
1064
4f7452a8
JJ
1065TEST(test_read_virtual_file) {
1066 test_read_virtual_file_one(0);
1067 test_read_virtual_file_one(1);
1068 test_read_virtual_file_one(2);
1069 test_read_virtual_file_one(20);
1070 test_read_virtual_file_one(4096);
1071 test_read_virtual_file_one(4097);
1072 test_read_virtual_file_one(SIZE_MAX);
f73141d7 1073}
4f7452a8
JJ
1074
1075DEFINE_TEST_MAIN(LOG_DEBUG);