]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
f73141d7 | 2 | |
f73141d7 | 3 | #include <fcntl.h> |
cf0fbc49 | 4 | #include <stdio.h> |
f73141d7 LP |
5 | #include <unistd.h> |
6 | ||
b5efdb8a | 7 | #include "alloc-util.h" |
1e5413f7 | 8 | #include "ctype.h" |
686d13b9 | 9 | #include "env-file.h" |
3ffd4af2 LP |
10 | #include "env-util.h" |
11 | #include "fd-util.h" | |
12 | #include "fileio.h" | |
627d2bac | 13 | #include "fs-util.h" |
897891f0 | 14 | #include "io-util.h" |
6bedfcbb | 15 | #include "parse-util.h" |
3ffd4af2 | 16 | #include "process-util.h" |
07630cea | 17 | #include "string-util.h" |
3ffd4af2 | 18 | #include "strv.h" |
6d7c4033 | 19 | #include "tests.h" |
e4de7287 | 20 | #include "tmpfile-util.h" |
3ffd4af2 | 21 | #include "util.h" |
f73141d7 LP |
22 | |
23 | static void test_parse_env_file(void) { | |
627d2bac ZJS |
24 | _cleanup_(unlink_tempfilep) char |
25 | t[] = "/tmp/test-fileio-in-XXXXXX", | |
095b30cb | 26 | p[] = "/tmp/test-fileio-out-XXXXXX"; |
f73141d7 | 27 | FILE *f; |
ebc05a09 | 28 | _cleanup_free_ char *one = NULL, *two = NULL, *three = NULL, *four = NULL, *five = NULL, |
e4a8db1f LT |
29 | *six = NULL, *seven = NULL, *eight = NULL, *nine = NULL, *ten = NULL, |
30 | *eleven = NULL, *twelve = NULL, *thirteen = NULL; | |
768100ef | 31 | _cleanup_strv_free_ char **a = NULL, **b = NULL; |
f73141d7 | 32 | char **i; |
768100ef | 33 | unsigned k; |
d8351049 | 34 | int r; |
f73141d7 | 35 | |
d8351049 | 36 | assert_se(fmkostemp_safe(t, "w", &f) == 0); |
f73141d7 LP |
37 | fputs("one=BAR \n" |
38 | "# comment\n" | |
39 | " # comment \n" | |
98f59e59 | 40 | " ; comment \n" |
f73141d7 LP |
41 | " two = bar \n" |
42 | "invalid line\n" | |
98f59e59 | 43 | "invalid line #comment\n" |
f73141d7 LP |
44 | "three = \"333\n" |
45 | "xxxx\"\n" | |
46 | "four = \'44\\\"44\'\n" | |
e4a8db1f | 47 | "five = \"55\\\"55\" \"FIVE\" cinco \n" |
f73141d7 LP |
48 | "six = seis sechs\\\n" |
49 | " sis\n" | |
98f59e59 HH |
50 | "seven=\"sevenval\" #nocomment\n" |
51 | "eight=eightval #nocomment\n" | |
ebc05a09 | 52 | "export nine=nineval\n" |
99003e01 ZJS |
53 | "ten=ignored\n" |
54 | "ten=ignored\n" | |
e4a8db1f LT |
55 | "ten=\n" |
56 | "eleven=\\value\n" | |
57 | "twelve=\"\\value\"\n" | |
58 | "thirteen='\\value'", f); | |
f73141d7 LP |
59 | |
60 | fflush(f); | |
61 | fclose(f); | |
62 | ||
aa8fbc74 | 63 | r = load_env_file(NULL, t, &a); |
ebc05a09 HH |
64 | assert_se(r >= 0); |
65 | ||
66 | STRV_FOREACH(i, a) | |
67 | log_info("Got: <%s>", *i); | |
68 | ||
5fba7bbf TA |
69 | assert_se(streq_ptr(a[0], "one=BAR")); |
70 | assert_se(streq_ptr(a[1], "two=bar")); | |
71 | assert_se(streq_ptr(a[2], "three=333\nxxxx")); | |
e4a8db1f LT |
72 | assert_se(streq_ptr(a[3], "four=44\\\"44")); |
73 | assert_se(streq_ptr(a[4], "five=55\"55FIVEcinco")); | |
5fba7bbf TA |
74 | assert_se(streq_ptr(a[5], "six=seis sechs sis")); |
75 | assert_se(streq_ptr(a[6], "seven=sevenval#nocomment")); | |
76 | assert_se(streq_ptr(a[7], "eight=eightval #nocomment")); | |
77 | assert_se(streq_ptr(a[8], "export nine=nineval")); | |
78 | assert_se(streq_ptr(a[9], "ten=")); | |
e4a8db1f LT |
79 | assert_se(streq_ptr(a[10], "eleven=value")); |
80 | assert_se(streq_ptr(a[11], "twelve=\\value")); | |
81 | assert_se(streq_ptr(a[12], "thirteen=\\value")); | |
82 | assert_se(a[13] == NULL); | |
ebc05a09 | 83 | |
039f0e70 | 84 | strv_env_clean(a); |
ebc05a09 | 85 | |
ebc05a09 HH |
86 | k = 0; |
87 | STRV_FOREACH(i, b) { | |
88 | log_info("Got2: <%s>", *i); | |
89 | assert_se(streq(*i, a[k++])); | |
90 | } | |
91 | ||
f73141d7 | 92 | r = parse_env_file( |
aa8fbc74 | 93 | NULL, t, |
f73141d7 LP |
94 | "one", &one, |
95 | "two", &two, | |
96 | "three", &three, | |
97 | "four", &four, | |
98 | "five", &five, | |
99 | "six", &six, | |
100 | "seven", &seven, | |
db537209 | 101 | "eight", &eight, |
ebc05a09 | 102 | "export nine", &nine, |
e4a8db1f LT |
103 | "ten", &ten, |
104 | "eleven", &eleven, | |
105 | "twelve", &twelve, | |
106 | "thirteen", &thirteen); | |
f73141d7 LP |
107 | |
108 | assert_se(r >= 0); | |
109 | ||
110 | log_info("one=[%s]", strna(one)); | |
111 | log_info("two=[%s]", strna(two)); | |
112 | log_info("three=[%s]", strna(three)); | |
113 | log_info("four=[%s]", strna(four)); | |
114 | log_info("five=[%s]", strna(five)); | |
115 | log_info("six=[%s]", strna(six)); | |
116 | log_info("seven=[%s]", strna(seven)); | |
db537209 | 117 | log_info("eight=[%s]", strna(eight)); |
ebc05a09 HH |
118 | log_info("export nine=[%s]", strna(nine)); |
119 | log_info("ten=[%s]", strna(nine)); | |
e4a8db1f LT |
120 | log_info("eleven=[%s]", strna(eleven)); |
121 | log_info("twelve=[%s]", strna(twelve)); | |
122 | log_info("thirteen=[%s]", strna(thirteen)); | |
f73141d7 LP |
123 | |
124 | assert_se(streq(one, "BAR")); | |
125 | assert_se(streq(two, "bar")); | |
126 | assert_se(streq(three, "333\nxxxx")); | |
e4a8db1f LT |
127 | assert_se(streq(four, "44\\\"44")); |
128 | assert_se(streq(five, "55\"55FIVEcinco")); | |
f73141d7 | 129 | assert_se(streq(six, "seis sechs sis")); |
98f59e59 HH |
130 | assert_se(streq(seven, "sevenval#nocomment")); |
131 | assert_se(streq(eight, "eightval #nocomment")); | |
ebc05a09 HH |
132 | assert_se(streq(nine, "nineval")); |
133 | assert_se(ten == NULL); | |
e4a8db1f LT |
134 | assert_se(streq(eleven, "value")); |
135 | assert_se(streq(twelve, "\\value")); | |
136 | assert_se(streq(thirteen, "\\value")); | |
f73141d7 | 137 | |
d8351049 ZJS |
138 | { |
139 | /* prepare a temporary file to write the environment to */ | |
140 | _cleanup_close_ int fd = mkostemp_safe(p); | |
141 | assert_se(fd >= 0); | |
142 | } | |
143 | ||
095b30cb | 144 | r = write_env_file(p, a); |
98f59e59 HH |
145 | assert_se(r >= 0); |
146 | ||
aa8fbc74 | 147 | r = load_env_file(NULL, p, &b); |
98f59e59 | 148 | assert_se(r >= 0); |
f73141d7 LP |
149 | } |
150 | ||
ac4c8d6d | 151 | static void test_parse_multiline_env_file(void) { |
627d2bac ZJS |
152 | _cleanup_(unlink_tempfilep) char |
153 | t[] = "/tmp/test-fileio-in-XXXXXX", | |
ac4c8d6d | 154 | p[] = "/tmp/test-fileio-out-XXXXXX"; |
ac4c8d6d ZJS |
155 | FILE *f; |
156 | _cleanup_strv_free_ char **a = NULL, **b = NULL; | |
157 | char **i; | |
d8351049 | 158 | int r; |
ac4c8d6d | 159 | |
d8351049 | 160 | assert_se(fmkostemp_safe(t, "w", &f) == 0); |
ac4c8d6d ZJS |
161 | fputs("one=BAR\\\n" |
162 | " VAR\\\n" | |
163 | "\tGAR\n" | |
164 | "#comment\n" | |
165 | "two=\"bar\\\n" | |
166 | " var\\\n" | |
167 | "\tgar\"\n" | |
168 | "#comment\n" | |
169 | "tri=\"bar \\\n" | |
170 | " var \\\n" | |
171 | "\tgar \"\n", f); | |
172 | ||
173 | fflush(f); | |
174 | fclose(f); | |
175 | ||
aa8fbc74 | 176 | r = load_env_file(NULL, t, &a); |
ac4c8d6d ZJS |
177 | assert_se(r >= 0); |
178 | ||
179 | STRV_FOREACH(i, a) | |
180 | log_info("Got: <%s>", *i); | |
181 | ||
5fba7bbf TA |
182 | assert_se(streq_ptr(a[0], "one=BAR VAR\tGAR")); |
183 | assert_se(streq_ptr(a[1], "two=bar var\tgar")); | |
184 | assert_se(streq_ptr(a[2], "tri=bar var \tgar ")); | |
ac4c8d6d ZJS |
185 | assert_se(a[3] == NULL); |
186 | ||
d8351049 ZJS |
187 | { |
188 | _cleanup_close_ int fd = mkostemp_safe(p); | |
189 | assert_se(fd >= 0); | |
190 | } | |
191 | ||
ac4c8d6d ZJS |
192 | r = write_env_file(p, a); |
193 | assert_se(r >= 0); | |
194 | ||
aa8fbc74 | 195 | r = load_env_file(NULL, p, &b); |
ac4c8d6d | 196 | assert_se(r >= 0); |
ac4c8d6d ZJS |
197 | } |
198 | ||
37f3ffca | 199 | static void test_merge_env_file(void) { |
627d2bac | 200 | _cleanup_(unlink_tempfilep) char t[] = "/tmp/test-fileio-XXXXXX"; |
9707d552 | 201 | _cleanup_fclose_ FILE *f = NULL; |
37f3ffca RS |
202 | _cleanup_strv_free_ char **a = NULL; |
203 | char **i; | |
d8351049 | 204 | int r; |
37f3ffca | 205 | |
d8351049 | 206 | assert_se(fmkostemp_safe(t, "w", &f) == 0); |
37f3ffca RS |
207 | log_info("/* %s (%s) */", __func__, t); |
208 | ||
37f3ffca RS |
209 | r = write_string_stream(f, |
210 | "one=1 \n" | |
211 | "twelve=${one}2\n" | |
212 | "twentyone=2${one}\n" | |
213 | "one=2\n" | |
ccad1fd0 ZJS |
214 | "twentytwo=2${one}\n" |
215 | "xxx_minus_three=$xxx - 3\n" | |
216 | "xxx=0x$one$one$one\n" | |
b82f58bf RS |
217 | "yyy=${one:-fallback}\n" |
218 | "zzz=${one:+replacement}\n" | |
219 | "zzzz=${foobar:-${nothing}}\n" | |
220 | "zzzzz=${nothing:+${nothing}}\n" | |
b1837133 | 221 | , WRITE_STRING_FILE_AVOID_NEWLINE); |
37f3ffca RS |
222 | assert(r >= 0); |
223 | ||
224 | r = merge_env_file(&a, NULL, t); | |
225 | assert_se(r >= 0); | |
226 | strv_sort(a); | |
227 | ||
228 | STRV_FOREACH(i, a) | |
229 | log_info("Got: <%s>", *i); | |
230 | ||
231 | assert_se(streq(a[0], "one=2")); | |
232 | assert_se(streq(a[1], "twelve=12")); | |
233 | assert_se(streq(a[2], "twentyone=21")); | |
234 | assert_se(streq(a[3], "twentytwo=22")); | |
ccad1fd0 ZJS |
235 | assert_se(streq(a[4], "xxx=0x222")); |
236 | assert_se(streq(a[5], "xxx_minus_three= - 3")); | |
b82f58bf RS |
237 | assert_se(streq(a[6], "yyy=2")); |
238 | assert_se(streq(a[7], "zzz=replacement")); | |
239 | assert_se(streq(a[8], "zzzz=")); | |
240 | assert_se(streq(a[9], "zzzzz=")); | |
241 | assert_se(a[10] == NULL); | |
37f3ffca RS |
242 | |
243 | r = merge_env_file(&a, NULL, t); | |
244 | assert_se(r >= 0); | |
245 | strv_sort(a); | |
246 | ||
247 | STRV_FOREACH(i, a) | |
248 | log_info("Got2: <%s>", *i); | |
249 | ||
250 | assert_se(streq(a[0], "one=2")); | |
251 | assert_se(streq(a[1], "twelve=12")); | |
252 | assert_se(streq(a[2], "twentyone=21")); | |
253 | assert_se(streq(a[3], "twentytwo=22")); | |
ccad1fd0 ZJS |
254 | assert_se(streq(a[4], "xxx=0x222")); |
255 | assert_se(streq(a[5], "xxx_minus_three=0x222 - 3")); | |
b82f58bf RS |
256 | assert_se(streq(a[6], "yyy=2")); |
257 | assert_se(streq(a[7], "zzz=replacement")); | |
258 | assert_se(streq(a[8], "zzzz=")); | |
259 | assert_se(streq(a[9], "zzzzz=")); | |
260 | assert_se(a[10] == NULL); | |
37f3ffca | 261 | } |
ac4c8d6d | 262 | |
184d1904 | 263 | static void test_merge_env_file_invalid(void) { |
627d2bac | 264 | _cleanup_(unlink_tempfilep) char t[] = "/tmp/test-fileio-XXXXXX"; |
9707d552 | 265 | _cleanup_fclose_ FILE *f = NULL; |
184d1904 ZJS |
266 | _cleanup_strv_free_ char **a = NULL; |
267 | char **i; | |
d8351049 | 268 | int r; |
184d1904 | 269 | |
d8351049 | 270 | assert_se(fmkostemp_safe(t, "w", &f) == 0); |
184d1904 ZJS |
271 | log_info("/* %s (%s) */", __func__, t); |
272 | ||
184d1904 ZJS |
273 | r = write_string_stream(f, |
274 | "unset one \n" | |
275 | "unset one= \n" | |
276 | "unset one=1 \n" | |
277 | "one \n" | |
278 | "one = \n" | |
279 | "one two =\n" | |
280 | "\x20two=\n" | |
281 | "#comment=comment\n" | |
282 | ";comment2=comment2\n" | |
283 | "#\n" | |
284 | "\n\n" /* empty line */ | |
b1837133 | 285 | , WRITE_STRING_FILE_AVOID_NEWLINE); |
184d1904 ZJS |
286 | assert(r >= 0); |
287 | ||
288 | r = merge_env_file(&a, NULL, t); | |
289 | assert_se(r >= 0); | |
290 | ||
291 | STRV_FOREACH(i, a) | |
292 | log_info("Got: <%s>", *i); | |
293 | ||
294 | assert_se(strv_isempty(a)); | |
295 | } | |
296 | ||
68fee104 | 297 | static void test_executable_is_script(void) { |
627d2bac | 298 | _cleanup_(unlink_tempfilep) char t[] = "/tmp/test-fileio-XXXXXX"; |
627d2bac | 299 | _cleanup_fclose_ FILE *f = NULL; |
68fee104 | 300 | char *command; |
d8351049 | 301 | int r; |
68fee104 | 302 | |
d8351049 | 303 | assert_se(fmkostemp_safe(t, "w", &f) == 0); |
68fee104 ZJS |
304 | fputs("#! /bin/script -a -b \ngoo goo", f); |
305 | fflush(f); | |
306 | ||
307 | r = executable_is_script(t, &command); | |
308 | assert_se(r > 0); | |
309 | assert_se(streq(command, "/bin/script")); | |
310 | free(command); | |
311 | ||
312 | r = executable_is_script("/bin/sh", &command); | |
313 | assert_se(r == 0); | |
314 | ||
315 | r = executable_is_script("/usr/bin/yum", &command); | |
316 | assert_se(r > 0 || r == -ENOENT); | |
317 | if (r > 0) { | |
318 | assert_se(startswith(command, "/")); | |
319 | free(command); | |
320 | } | |
68fee104 ZJS |
321 | } |
322 | ||
69ab8088 | 323 | static void test_status_field(void) { |
1e5413f7 ZJS |
324 | _cleanup_free_ char *t = NULL, *p = NULL, *s = NULL, *z = NULL; |
325 | unsigned long long total = 0, buffers = 0; | |
442e0083 | 326 | int r; |
69ab8088 | 327 | |
c4cd1d4d | 328 | assert_se(get_proc_field("/proc/self/status", "Threads", WHITESPACE, &t) == 0); |
69ab8088 ZJS |
329 | puts(t); |
330 | assert_se(streq(t, "1")); | |
331 | ||
c4cd1d4d | 332 | r = get_proc_field("/proc/meminfo", "MemTotal", WHITESPACE, &p); |
1e5413f7 | 333 | if (r != -ENOENT) { |
bdf7026e | 334 | assert_se(r == 0); |
1e5413f7 ZJS |
335 | puts(p); |
336 | assert_se(safe_atollu(p, &total) == 0); | |
337 | } | |
69ab8088 | 338 | |
c4cd1d4d | 339 | r = get_proc_field("/proc/meminfo", "Buffers", WHITESPACE, &s); |
1e5413f7 | 340 | if (r != -ENOENT) { |
bdf7026e | 341 | assert_se(r == 0); |
1e5413f7 ZJS |
342 | puts(s); |
343 | assert_se(safe_atollu(s, &buffers) == 0); | |
344 | } | |
69ab8088 | 345 | |
2b01a801 | 346 | if (p) |
bdf7026e | 347 | assert_se(buffers < total); |
1e5413f7 ZJS |
348 | |
349 | /* Seccomp should be a good test for field full of zeros. */ | |
c4cd1d4d | 350 | r = get_proc_field("/proc/meminfo", "Seccomp", WHITESPACE, &z); |
1e5413f7 | 351 | if (r != -ENOENT) { |
bdf7026e | 352 | assert_se(r == 0); |
1e5413f7 ZJS |
353 | puts(z); |
354 | assert_se(safe_atollu(z, &buffers) == 0); | |
355 | } | |
356 | } | |
357 | ||
358 | static void test_capeff(void) { | |
359 | int pid, p; | |
360 | ||
361 | for (pid = 0; pid < 2; pid++) { | |
362 | _cleanup_free_ char *capeff = NULL; | |
363 | int r; | |
364 | ||
365 | r = get_process_capeff(0, &capeff); | |
366 | log_info("capeff: '%s' (r=%d)", capeff, r); | |
367 | ||
4c701096 | 368 | if (IN_SET(r, -ENOENT, -EPERM)) |
1e5413f7 ZJS |
369 | return; |
370 | ||
bdf7026e TA |
371 | assert_se(r == 0); |
372 | assert_se(*capeff); | |
1a7906ae | 373 | p = capeff[strspn(capeff, HEXDIGITS)]; |
bdf7026e | 374 | assert_se(!p || isspace(p)); |
1e5413f7 | 375 | } |
69ab8088 ZJS |
376 | } |
377 | ||
0709b743 | 378 | static void test_write_string_stream(void) { |
627d2bac ZJS |
379 | _cleanup_(unlink_tempfilep) char fn[] = "/tmp/test-write_string_stream-XXXXXX"; |
380 | _cleanup_fclose_ FILE *f = NULL; | |
0709b743 RC |
381 | int fd; |
382 | char buf[64]; | |
383 | ||
646853bd | 384 | fd = mkostemp_safe(fn); |
0709b743 RC |
385 | assert_se(fd >= 0); |
386 | ||
387 | f = fdopen(fd, "r"); | |
388 | assert_se(f); | |
b1837133 | 389 | assert_se(write_string_stream(f, "boohoo", 0) < 0); |
36297390 | 390 | f = safe_fclose(f); |
0709b743 | 391 | |
36297390 | 392 | f = fopen(fn, "r+"); |
0709b743 RC |
393 | assert_se(f); |
394 | ||
b1837133 | 395 | assert_se(write_string_stream(f, "boohoo", 0) == 0); |
0709b743 RC |
396 | rewind(f); |
397 | ||
398 | assert_se(fgets(buf, sizeof(buf), f)); | |
399 | assert_se(streq(buf, "boohoo\n")); | |
36297390 | 400 | f = safe_fclose(f); |
0709b743 | 401 | |
36297390 | 402 | f = fopen(fn, "w+"); |
40beecdb DM |
403 | assert_se(f); |
404 | ||
b1837133 | 405 | assert_se(write_string_stream(f, "boohoo", WRITE_STRING_FILE_AVOID_NEWLINE) == 0); |
40beecdb DM |
406 | rewind(f); |
407 | ||
408 | assert_se(fgets(buf, sizeof(buf), f)); | |
409 | printf(">%s<", buf); | |
410 | assert_se(streq(buf, "boohoo")); | |
0709b743 RC |
411 | } |
412 | ||
413 | static void test_write_string_file(void) { | |
627d2bac | 414 | _cleanup_(unlink_tempfilep) char fn[] = "/tmp/test-write_string_file-XXXXXX"; |
76082570 RC |
415 | char buf[64] = {}; |
416 | _cleanup_close_ int fd; | |
0709b743 | 417 | |
646853bd | 418 | fd = mkostemp_safe(fn); |
0709b743 RC |
419 | assert_se(fd >= 0); |
420 | ||
4c1fc3e4 | 421 | assert_se(write_string_file(fn, "boohoo", WRITE_STRING_FILE_CREATE) == 0); |
0709b743 | 422 | |
cca0efb0 | 423 | assert_se(read(fd, buf, sizeof(buf)) == 7); |
0709b743 | 424 | assert_se(streq(buf, "boohoo\n")); |
0709b743 RC |
425 | } |
426 | ||
e07995a3 | 427 | static void test_write_string_file_no_create(void) { |
627d2bac | 428 | _cleanup_(unlink_tempfilep) char fn[] = "/tmp/test-write_string_file_no_create-XXXXXX"; |
e07995a3 RC |
429 | _cleanup_close_ int fd; |
430 | char buf[64] = {0}; | |
431 | ||
646853bd | 432 | fd = mkostemp_safe(fn); |
e07995a3 RC |
433 | assert_se(fd >= 0); |
434 | ||
4c1fc3e4 DM |
435 | assert_se(write_string_file("/a/file/which/does/not/exists/i/guess", "boohoo", 0) < 0); |
436 | assert_se(write_string_file(fn, "boohoo", 0) == 0); | |
e07995a3 | 437 | |
fbd0b64f | 438 | assert_se(read(fd, buf, sizeof(buf)) == STRLEN("boohoo\n")); |
e07995a3 | 439 | assert_se(streq(buf, "boohoo\n")); |
e07995a3 RC |
440 | } |
441 | ||
eb3da901 LP |
442 | static void test_write_string_file_verify(void) { |
443 | _cleanup_free_ char *buf = NULL, *buf2 = NULL; | |
444 | int r; | |
445 | ||
446 | assert_se(read_one_line_file("/proc/cmdline", &buf) >= 0); | |
dcd6361e | 447 | assert_se(buf2 = strjoin(buf, "\n")); |
eb3da901 LP |
448 | |
449 | r = write_string_file("/proc/cmdline", buf, 0); | |
4c701096 | 450 | assert_se(IN_SET(r, -EACCES, -EIO)); |
eb3da901 | 451 | r = write_string_file("/proc/cmdline", buf2, 0); |
4c701096 | 452 | assert_se(IN_SET(r, -EACCES, -EIO)); |
eb3da901 LP |
453 | |
454 | assert_se(write_string_file("/proc/cmdline", buf, WRITE_STRING_FILE_VERIFY_ON_FAILURE) == 0); | |
455 | assert_se(write_string_file("/proc/cmdline", buf2, WRITE_STRING_FILE_VERIFY_ON_FAILURE) == 0); | |
456 | ||
457 | r = write_string_file("/proc/cmdline", buf, WRITE_STRING_FILE_VERIFY_ON_FAILURE|WRITE_STRING_FILE_AVOID_NEWLINE); | |
4c701096 | 458 | assert_se(IN_SET(r, -EACCES, -EIO)); |
eb3da901 LP |
459 | assert_se(write_string_file("/proc/cmdline", buf2, WRITE_STRING_FILE_VERIFY_ON_FAILURE|WRITE_STRING_FILE_AVOID_NEWLINE) == 0); |
460 | } | |
461 | ||
e07995a3 | 462 | static void test_load_env_file_pairs(void) { |
627d2bac ZJS |
463 | _cleanup_(unlink_tempfilep) char fn[] = "/tmp/test-load_env_file_pairs-XXXXXX"; |
464 | int fd, r; | |
e07995a3 RC |
465 | _cleanup_fclose_ FILE *f = NULL; |
466 | _cleanup_strv_free_ char **l = NULL; | |
467 | char **k, **v; | |
468 | ||
646853bd | 469 | fd = mkostemp_safe(fn); |
e07995a3 RC |
470 | assert_se(fd >= 0); |
471 | ||
472 | r = write_string_file(fn, | |
473 | "NAME=\"Arch Linux\"\n" | |
474 | "ID=arch\n" | |
475 | "PRETTY_NAME=\"Arch Linux\"\n" | |
476 | "ANSI_COLOR=\"0;36\"\n" | |
477 | "HOME_URL=\"https://www.archlinux.org/\"\n" | |
478 | "SUPPORT_URL=\"https://bbs.archlinux.org/\"\n" | |
4c1fc3e4 DM |
479 | "BUG_REPORT_URL=\"https://bugs.archlinux.org/\"\n", |
480 | WRITE_STRING_FILE_CREATE); | |
e07995a3 RC |
481 | assert_se(r == 0); |
482 | ||
483 | f = fdopen(fd, "r"); | |
484 | assert_se(f); | |
485 | ||
aa8fbc74 | 486 | r = load_env_file_pairs(f, fn, &l); |
e07995a3 RC |
487 | assert_se(r >= 0); |
488 | ||
489 | assert_se(strv_length(l) == 14); | |
490 | STRV_FOREACH_PAIR(k, v, l) { | |
491 | assert_se(STR_IN_SET(*k, "NAME", "ID", "PRETTY_NAME", "ANSI_COLOR", "HOME_URL", "SUPPORT_URL", "BUG_REPORT_URL")); | |
492 | printf("%s=%s\n", *k, *v); | |
493 | if (streq(*k, "NAME")) assert_se(streq(*v, "Arch Linux")); | |
494 | if (streq(*k, "ID")) assert_se(streq(*v, "arch")); | |
495 | if (streq(*k, "PRETTY_NAME")) assert_se(streq(*v, "Arch Linux")); | |
496 | if (streq(*k, "ANSI_COLOR")) assert_se(streq(*v, "0;36")); | |
497 | if (streq(*k, "HOME_URL")) assert_se(streq(*v, "https://www.archlinux.org/")); | |
498 | if (streq(*k, "SUPPORT_URL")) assert_se(streq(*v, "https://bbs.archlinux.org/")); | |
499 | if (streq(*k, "BUG_REPORT_URL")) assert_se(streq(*v, "https://bugs.archlinux.org/")); | |
500 | } | |
e07995a3 RC |
501 | } |
502 | ||
897891f0 RC |
503 | static void test_search_and_fopen(void) { |
504 | const char *dirs[] = {"/tmp/foo/bar", "/tmp", NULL}; | |
627d2bac | 505 | |
897891f0 | 506 | char name[] = "/tmp/test-search_and_fopen.XXXXXX"; |
627d2bac | 507 | int fd, r; |
897891f0 RC |
508 | FILE *f; |
509 | ||
646853bd | 510 | fd = mkostemp_safe(name); |
897891f0 RC |
511 | assert_se(fd >= 0); |
512 | close(fd); | |
513 | ||
514 | r = search_and_fopen(basename(name), "r", NULL, dirs, &f); | |
515 | assert_se(r >= 0); | |
516 | fclose(f); | |
517 | ||
518 | r = search_and_fopen(name, "r", NULL, dirs, &f); | |
519 | assert_se(r >= 0); | |
520 | fclose(f); | |
521 | ||
522 | r = search_and_fopen(basename(name), "r", "/", dirs, &f); | |
523 | assert_se(r >= 0); | |
524 | fclose(f); | |
525 | ||
526 | r = search_and_fopen("/a/file/which/does/not/exist/i/guess", "r", NULL, dirs, &f); | |
527 | assert_se(r < 0); | |
528 | r = search_and_fopen("afilewhichdoesnotexistiguess", "r", NULL, dirs, &f); | |
529 | assert_se(r < 0); | |
530 | ||
531 | r = unlink(name); | |
532 | assert_se(r == 0); | |
533 | ||
534 | r = search_and_fopen(basename(name), "r", NULL, dirs, &f); | |
535 | assert_se(r < 0); | |
536 | } | |
537 | ||
897891f0 RC |
538 | static void test_search_and_fopen_nulstr(void) { |
539 | const char dirs[] = "/tmp/foo/bar\0/tmp\0"; | |
627d2bac ZJS |
540 | |
541 | _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-search_and_fopen.XXXXXX"; | |
542 | int fd, r; | |
897891f0 RC |
543 | FILE *f; |
544 | ||
646853bd | 545 | fd = mkostemp_safe(name); |
897891f0 RC |
546 | assert_se(fd >= 0); |
547 | close(fd); | |
548 | ||
549 | r = search_and_fopen_nulstr(basename(name), "r", NULL, dirs, &f); | |
550 | assert_se(r >= 0); | |
551 | fclose(f); | |
552 | ||
553 | r = search_and_fopen_nulstr(name, "r", NULL, dirs, &f); | |
554 | assert_se(r >= 0); | |
555 | fclose(f); | |
556 | ||
557 | r = search_and_fopen_nulstr("/a/file/which/does/not/exist/i/guess", "r", NULL, dirs, &f); | |
558 | assert_se(r < 0); | |
559 | r = search_and_fopen_nulstr("afilewhichdoesnotexistiguess", "r", NULL, dirs, &f); | |
560 | assert_se(r < 0); | |
561 | ||
562 | r = unlink(name); | |
563 | assert_se(r == 0); | |
564 | ||
565 | r = search_and_fopen_nulstr(basename(name), "r", NULL, dirs, &f); | |
566 | assert_se(r < 0); | |
567 | } | |
568 | ||
569 | static void test_writing_tmpfile(void) { | |
627d2bac | 570 | _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-systemd_writing_tmpfile.XXXXXX"; |
897891f0 RC |
571 | _cleanup_free_ char *contents = NULL; |
572 | size_t size; | |
9a92e255 | 573 | _cleanup_close_ int fd = -1; |
897891f0 | 574 | struct iovec iov[3]; |
627d2bac | 575 | int r; |
897891f0 | 576 | |
e6a7ec4b LP |
577 | iov[0] = IOVEC_MAKE_STRING("abc\n"); |
578 | iov[1] = IOVEC_MAKE_STRING(ALPHANUMERICAL "\n"); | |
579 | iov[2] = IOVEC_MAKE_STRING(""); | |
897891f0 | 580 | |
646853bd | 581 | fd = mkostemp_safe(name); |
897891f0 RC |
582 | printf("tmpfile: %s", name); |
583 | ||
584 | r = writev(fd, iov, 3); | |
585 | assert_se(r >= 0); | |
586 | ||
587 | r = read_full_file(name, &contents, &size); | |
588 | assert_se(r == 0); | |
589 | printf("contents: %s", contents); | |
590 | assert_se(streq(contents, "abc\n" ALPHANUMERICAL "\n")); | |
897891f0 RC |
591 | } |
592 | ||
593 | static void test_tempfn(void) { | |
594 | char *ret = NULL, *p; | |
595 | ||
596 | assert_se(tempfn_xxxxxx("/foo/bar/waldo", NULL, &ret) >= 0); | |
597 | assert_se(streq_ptr(ret, "/foo/bar/.#waldoXXXXXX")); | |
598 | free(ret); | |
599 | ||
600 | assert_se(tempfn_xxxxxx("/foo/bar/waldo", "[miau]", &ret) >= 0); | |
601 | assert_se(streq_ptr(ret, "/foo/bar/.#[miau]waldoXXXXXX")); | |
602 | free(ret); | |
603 | ||
604 | assert_se(tempfn_random("/foo/bar/waldo", NULL, &ret) >= 0); | |
605 | assert_se(p = startswith(ret, "/foo/bar/.#waldo")); | |
606 | assert_se(strlen(p) == 16); | |
607 | assert_se(in_charset(p, "0123456789abcdef")); | |
608 | free(ret); | |
609 | ||
610 | assert_se(tempfn_random("/foo/bar/waldo", "[wuff]", &ret) >= 0); | |
611 | assert_se(p = startswith(ret, "/foo/bar/.#[wuff]waldo")); | |
612 | assert_se(strlen(p) == 16); | |
613 | assert_se(in_charset(p, "0123456789abcdef")); | |
614 | free(ret); | |
615 | ||
616 | assert_se(tempfn_random_child("/foo/bar/waldo", NULL, &ret) >= 0); | |
617 | assert_se(p = startswith(ret, "/foo/bar/waldo/.#")); | |
618 | assert_se(strlen(p) == 16); | |
619 | assert_se(in_charset(p, "0123456789abcdef")); | |
620 | free(ret); | |
621 | ||
622 | assert_se(tempfn_random_child("/foo/bar/waldo", "[kikiriki]", &ret) >= 0); | |
623 | assert_se(p = startswith(ret, "/foo/bar/waldo/.#[kikiriki]")); | |
624 | assert_se(strlen(p) == 16); | |
625 | assert_se(in_charset(p, "0123456789abcdef")); | |
626 | free(ret); | |
627 | } | |
628 | ||
7f9d1aed | 629 | static const char chars[] = |
cdce33f9 | 630 | "Aąę„”\n루\377"; |
7f9d1aed ZJS |
631 | |
632 | static void test_fgetc(void) { | |
633 | _cleanup_fclose_ FILE *f = NULL; | |
634 | char c; | |
635 | ||
636 | f = fmemopen((void*) chars, sizeof(chars), "re"); | |
637 | assert_se(f); | |
638 | ||
639 | for (unsigned i = 0; i < sizeof(chars); i++) { | |
640 | assert_se(safe_fgetc(f, &c) == 1); | |
641 | assert_se(c == chars[i]); | |
642 | ||
cdce33f9 LP |
643 | /* EOF is -1, and hence we can't push value 255 in this way */ |
644 | assert_se(ungetc(c, f) != EOF || c == EOF); | |
645 | assert_se(c == EOF || safe_fgetc(f, &c) == 1); | |
7f9d1aed ZJS |
646 | assert_se(c == chars[i]); |
647 | ||
cdce33f9 | 648 | /* But it works when we push it properly cast */ |
7f9d1aed ZJS |
649 | assert_se(ungetc((unsigned char) c, f) != EOF); |
650 | assert_se(safe_fgetc(f, &c) == 1); | |
651 | assert_se(c == chars[i]); | |
652 | } | |
653 | ||
654 | assert_se(safe_fgetc(f, &c) == 0); | |
655 | } | |
656 | ||
2c9de139 ZJS |
657 | static const char buffer[] = |
658 | "Some test data\n" | |
7f9d1aed | 659 | "루Non-ascii chars: ąę„”\n" |
838894b0 LP |
660 | "terminators\r\n" |
661 | "and even more\n\r" | |
662 | "now the same with a NUL\n\0" | |
663 | "and more\r\0" | |
664 | "and even more\r\n\0" | |
665 | "and yet even more\n\r\0" | |
2c9de139 ZJS |
666 | "With newlines, and a NUL byte\0" |
667 | "\n" | |
668 | "an empty line\n" | |
669 | "an ignored line\n" | |
670 | "and a very long line that is supposed to be truncated, because it is so long\n"; | |
671 | ||
672 | static void test_read_line_one_file(FILE *f) { | |
4f9a66a3 LP |
673 | _cleanup_free_ char *line = NULL; |
674 | ||
4f9a66a3 LP |
675 | assert_se(read_line(f, (size_t) -1, &line) == 15 && streq(line, "Some test data")); |
676 | line = mfree(line); | |
677 | ||
7f9d1aed | 678 | assert_se(read_line(f, (size_t) -1, &line) > 0 && streq(line, "루Non-ascii chars: ąę„”")); |
838894b0 LP |
679 | line = mfree(line); |
680 | ||
681 | assert_se(read_line(f, (size_t) -1, &line) == 13 && streq(line, "terminators")); | |
682 | line = mfree(line); | |
683 | ||
684 | assert_se(read_line(f, (size_t) -1, &line) == 15 && streq(line, "and even more")); | |
685 | line = mfree(line); | |
686 | ||
687 | assert_se(read_line(f, (size_t) -1, &line) == 25 && streq(line, "now the same with a NUL")); | |
688 | line = mfree(line); | |
689 | ||
690 | assert_se(read_line(f, (size_t) -1, &line) == 10 && streq(line, "and more")); | |
691 | line = mfree(line); | |
692 | ||
693 | assert_se(read_line(f, (size_t) -1, &line) == 16 && streq(line, "and even more")); | |
694 | line = mfree(line); | |
695 | ||
696 | assert_se(read_line(f, (size_t) -1, &line) == 20 && streq(line, "and yet even more")); | |
697 | line = mfree(line); | |
698 | ||
4f9a66a3 LP |
699 | assert_se(read_line(f, 1024, &line) == 30 && streq(line, "With newlines, and a NUL byte")); |
700 | line = mfree(line); | |
701 | ||
702 | assert_se(read_line(f, 1024, &line) == 1 && streq(line, "")); | |
703 | line = mfree(line); | |
704 | ||
705 | assert_se(read_line(f, 1024, &line) == 14 && streq(line, "an empty line")); | |
706 | line = mfree(line); | |
707 | ||
708 | assert_se(read_line(f, (size_t) -1, NULL) == 16); | |
709 | ||
710 | assert_se(read_line(f, 16, &line) == -ENOBUFS); | |
711 | line = mfree(line); | |
712 | ||
713 | /* read_line() stopped when it hit the limit, that means when we continue reading we'll read at the first | |
714 | * character after the previous limit. Let's make use of tha to continue our test. */ | |
838894b0 | 715 | assert_se(read_line(f, 1024, &line) == 62 && streq(line, "line that is supposed to be truncated, because it is so long")); |
4f9a66a3 LP |
716 | line = mfree(line); |
717 | ||
718 | assert_se(read_line(f, 1024, &line) == 0 && streq(line, "")); | |
719 | } | |
720 | ||
2c9de139 ZJS |
721 | static void test_read_line(void) { |
722 | _cleanup_fclose_ FILE *f = NULL; | |
2c9de139 ZJS |
723 | |
724 | f = fmemopen((void*) buffer, sizeof(buffer), "re"); | |
725 | assert_se(f); | |
726 | ||
727 | test_read_line_one_file(f); | |
728 | } | |
729 | ||
730 | static void test_read_line2(void) { | |
627d2bac | 731 | _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-fileio.XXXXXX"; |
2c9de139 ZJS |
732 | int fd; |
733 | _cleanup_fclose_ FILE *f = NULL; | |
734 | ||
735 | fd = mkostemp_safe(name); | |
736 | assert_se(fd >= 0); | |
737 | assert_se((size_t) write(fd, buffer, sizeof(buffer)) == sizeof(buffer)); | |
738 | ||
739 | assert_se(lseek(fd, 0, SEEK_SET) == 0); | |
740 | assert_se(f = fdopen(fd, "r")); | |
741 | ||
742 | test_read_line_one_file(f); | |
743 | } | |
744 | ||
745 | static void test_read_line3(void) { | |
746 | _cleanup_fclose_ FILE *f = NULL; | |
747 | _cleanup_free_ char *line = NULL; | |
748 | int r; | |
749 | ||
750 | f = fopen("/proc/cmdline", "re"); | |
751 | if (!f && IN_SET(errno, ENOENT, EPERM)) | |
752 | return; | |
753 | assert_se(f); | |
754 | ||
755 | r = read_line(f, LINE_MAX, &line); | |
756 | assert_se((size_t) r == strlen(line) + 1); | |
757 | assert_se(read_line(f, LINE_MAX, NULL) == 0); | |
758 | } | |
759 | ||
6baac700 LP |
760 | static void test_read_line4(void) { |
761 | static const struct { | |
762 | size_t length; | |
763 | const char *string; | |
764 | } eof_endings[] = { | |
765 | /* Each of these will be followed by EOF and should generate the one same single string */ | |
766 | { 3, "foo" }, | |
767 | { 4, "foo\n" }, | |
768 | { 4, "foo\r" }, | |
769 | { 4, "foo\0" }, | |
770 | { 5, "foo\n\0" }, | |
771 | { 5, "foo\r\0" }, | |
772 | { 5, "foo\r\n" }, | |
773 | { 5, "foo\n\r" }, | |
774 | { 6, "foo\r\n\0" }, | |
775 | { 6, "foo\n\r\0" }, | |
776 | }; | |
777 | ||
778 | size_t i; | |
779 | int r; | |
780 | ||
781 | for (i = 0; i < ELEMENTSOF(eof_endings); i++) { | |
782 | _cleanup_fclose_ FILE *f = NULL; | |
783 | _cleanup_free_ char *s = NULL; | |
784 | ||
785 | assert_se(f = fmemopen((void*) eof_endings[i].string, eof_endings[i].length, "r")); | |
786 | ||
787 | r = read_line(f, (size_t) -1, &s); | |
788 | assert_se((size_t) r == eof_endings[i].length); | |
789 | assert_se(streq_ptr(s, "foo")); | |
790 | ||
791 | assert_se(read_line(f, (size_t) -1, NULL) == 0); /* Ensure we hit EOF */ | |
792 | } | |
793 | } | |
794 | ||
3946d576 LP |
795 | static void test_read_nul_string(void) { |
796 | static const char test[] = "string nr. 1\0" | |
797 | "string nr. 2\n\0" | |
517b7760 | 798 | "\377empty string follows\0" |
3946d576 LP |
799 | "\0" |
800 | "final string\n is empty\0" | |
801 | "\0"; | |
802 | ||
803 | _cleanup_fclose_ FILE *f = NULL; | |
804 | _cleanup_free_ char *s = NULL; | |
805 | ||
806 | assert_se(f = fmemopen((void*) test, sizeof(test)-1, "r")); | |
807 | ||
808 | assert_se(read_nul_string(f, LONG_LINE_MAX, &s) == 13 && streq_ptr(s, "string nr. 1")); | |
809 | s = mfree(s); | |
810 | ||
811 | assert_se(read_nul_string(f, LONG_LINE_MAX, &s) == 14 && streq_ptr(s, "string nr. 2\n")); | |
812 | s = mfree(s); | |
813 | ||
517b7760 | 814 | assert_se(read_nul_string(f, LONG_LINE_MAX, &s) == 22 && streq_ptr(s, "\377empty string follows")); |
3946d576 LP |
815 | s = mfree(s); |
816 | ||
817 | assert_se(read_nul_string(f, LONG_LINE_MAX, &s) == 1 && streq_ptr(s, "")); | |
818 | s = mfree(s); | |
819 | ||
820 | assert_se(read_nul_string(f, LONG_LINE_MAX, &s) == 23 && streq_ptr(s, "final string\n is empty")); | |
821 | s = mfree(s); | |
822 | ||
823 | assert_se(read_nul_string(f, LONG_LINE_MAX, &s) == 1 && streq_ptr(s, "")); | |
824 | s = mfree(s); | |
825 | ||
826 | assert_se(read_nul_string(f, LONG_LINE_MAX, &s) == 0 && streq_ptr(s, "")); | |
827 | } | |
828 | ||
f73141d7 | 829 | int main(int argc, char *argv[]) { |
6d7c4033 | 830 | test_setup_logging(LOG_DEBUG); |
1e5413f7 | 831 | |
f73141d7 | 832 | test_parse_env_file(); |
ac4c8d6d | 833 | test_parse_multiline_env_file(); |
37f3ffca | 834 | test_merge_env_file(); |
184d1904 | 835 | test_merge_env_file_invalid(); |
68fee104 | 836 | test_executable_is_script(); |
69ab8088 | 837 | test_status_field(); |
1e5413f7 | 838 | test_capeff(); |
0709b743 RC |
839 | test_write_string_stream(); |
840 | test_write_string_file(); | |
e07995a3 | 841 | test_write_string_file_no_create(); |
eb3da901 | 842 | test_write_string_file_verify(); |
e07995a3 | 843 | test_load_env_file_pairs(); |
897891f0 RC |
844 | test_search_and_fopen(); |
845 | test_search_and_fopen_nulstr(); | |
846 | test_writing_tmpfile(); | |
847 | test_tempfn(); | |
7f9d1aed | 848 | test_fgetc(); |
4f9a66a3 | 849 | test_read_line(); |
2c9de139 ZJS |
850 | test_read_line2(); |
851 | test_read_line3(); | |
6baac700 | 852 | test_read_line4(); |
3946d576 | 853 | test_read_nul_string(); |
1e5413f7 | 854 | |
f73141d7 LP |
855 | return 0; |
856 | } |