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