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