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