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