]>
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" |
3ffd4af2 LP |
9 | #include "def.h" |
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 LP |
18 | #include "strv.h" |
19 | #include "util.h" | |
f73141d7 LP |
20 | |
21 | static void test_parse_env_file(void) { | |
627d2bac ZJS |
22 | _cleanup_(unlink_tempfilep) char |
23 | t[] = "/tmp/test-fileio-in-XXXXXX", | |
095b30cb | 24 | p[] = "/tmp/test-fileio-out-XXXXXX"; |
f73141d7 LP |
25 | int fd, r; |
26 | FILE *f; | |
ebc05a09 HH |
27 | _cleanup_free_ char *one = NULL, *two = NULL, *three = NULL, *four = NULL, *five = NULL, |
28 | *six = NULL, *seven = NULL, *eight = NULL, *nine = NULL, *ten = NULL; | |
768100ef | 29 | _cleanup_strv_free_ char **a = NULL, **b = NULL; |
f73141d7 | 30 | char **i; |
768100ef | 31 | unsigned k; |
f73141d7 | 32 | |
646853bd | 33 | fd = mkostemp_safe(p); |
d514feaa TA |
34 | assert_se(fd >= 0); |
35 | close(fd); | |
095b30cb | 36 | |
646853bd | 37 | fd = mkostemp_safe(t); |
f73141d7 LP |
38 | assert_se(fd >= 0); |
39 | ||
40 | f = fdopen(fd, "w"); | |
41 | assert_se(f); | |
42 | ||
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" | |
53 | "five = \'55\\\'55\' \"FIVE\" cinco \n" | |
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" | |
ebc05a09 | 61 | "ten=", f); |
f73141d7 LP |
62 | |
63 | fflush(f); | |
64 | fclose(f); | |
65 | ||
717603e3 | 66 | r = load_env_file(NULL, t, NULL, &a); |
ebc05a09 HH |
67 | assert_se(r >= 0); |
68 | ||
69 | STRV_FOREACH(i, a) | |
70 | log_info("Got: <%s>", *i); | |
71 | ||
5fba7bbf TA |
72 | assert_se(streq_ptr(a[0], "one=BAR")); |
73 | assert_se(streq_ptr(a[1], "two=bar")); | |
74 | assert_se(streq_ptr(a[2], "three=333\nxxxx")); | |
75 | assert_se(streq_ptr(a[3], "four=44\"44")); | |
76 | assert_se(streq_ptr(a[4], "five=55\'55FIVEcinco")); | |
77 | assert_se(streq_ptr(a[5], "six=seis sechs sis")); | |
78 | assert_se(streq_ptr(a[6], "seven=sevenval#nocomment")); | |
79 | assert_se(streq_ptr(a[7], "eight=eightval #nocomment")); | |
80 | assert_se(streq_ptr(a[8], "export nine=nineval")); | |
81 | assert_se(streq_ptr(a[9], "ten=")); | |
ebc05a09 HH |
82 | assert_se(a[10] == NULL); |
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( |
1a5a177e | 93 | NULL, t, NULL, |
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 HH |
102 | "export nine", &nine, |
103 | "ten", &ten, | |
f73141d7 LP |
104 | NULL); |
105 | ||
106 | assert_se(r >= 0); | |
107 | ||
108 | log_info("one=[%s]", strna(one)); | |
109 | log_info("two=[%s]", strna(two)); | |
110 | log_info("three=[%s]", strna(three)); | |
111 | log_info("four=[%s]", strna(four)); | |
112 | log_info("five=[%s]", strna(five)); | |
113 | log_info("six=[%s]", strna(six)); | |
114 | log_info("seven=[%s]", strna(seven)); | |
db537209 | 115 | log_info("eight=[%s]", strna(eight)); |
ebc05a09 HH |
116 | log_info("export nine=[%s]", strna(nine)); |
117 | log_info("ten=[%s]", strna(nine)); | |
f73141d7 LP |
118 | |
119 | assert_se(streq(one, "BAR")); | |
120 | assert_se(streq(two, "bar")); | |
121 | assert_se(streq(three, "333\nxxxx")); | |
122 | assert_se(streq(four, "44\"44")); | |
123 | assert_se(streq(five, "55\'55FIVEcinco")); | |
124 | assert_se(streq(six, "seis sechs sis")); | |
98f59e59 HH |
125 | assert_se(streq(seven, "sevenval#nocomment")); |
126 | assert_se(streq(eight, "eightval #nocomment")); | |
ebc05a09 HH |
127 | assert_se(streq(nine, "nineval")); |
128 | assert_se(ten == NULL); | |
f73141d7 | 129 | |
095b30cb | 130 | r = write_env_file(p, a); |
98f59e59 HH |
131 | assert_se(r >= 0); |
132 | ||
717603e3 | 133 | r = load_env_file(NULL, p, NULL, &b); |
98f59e59 | 134 | assert_se(r >= 0); |
f73141d7 LP |
135 | } |
136 | ||
ac4c8d6d | 137 | static void test_parse_multiline_env_file(void) { |
627d2bac ZJS |
138 | _cleanup_(unlink_tempfilep) char |
139 | t[] = "/tmp/test-fileio-in-XXXXXX", | |
ac4c8d6d ZJS |
140 | p[] = "/tmp/test-fileio-out-XXXXXX"; |
141 | int fd, r; | |
142 | FILE *f; | |
143 | _cleanup_strv_free_ char **a = NULL, **b = NULL; | |
144 | char **i; | |
145 | ||
646853bd | 146 | fd = mkostemp_safe(p); |
d514feaa TA |
147 | assert_se(fd >= 0); |
148 | close(fd); | |
ac4c8d6d | 149 | |
646853bd | 150 | fd = mkostemp_safe(t); |
ac4c8d6d ZJS |
151 | assert_se(fd >= 0); |
152 | ||
153 | f = fdopen(fd, "w"); | |
154 | assert_se(f); | |
155 | ||
156 | fputs("one=BAR\\\n" | |
157 | " VAR\\\n" | |
158 | "\tGAR\n" | |
159 | "#comment\n" | |
160 | "two=\"bar\\\n" | |
161 | " var\\\n" | |
162 | "\tgar\"\n" | |
163 | "#comment\n" | |
164 | "tri=\"bar \\\n" | |
165 | " var \\\n" | |
166 | "\tgar \"\n", f); | |
167 | ||
168 | fflush(f); | |
169 | fclose(f); | |
170 | ||
717603e3 | 171 | r = load_env_file(NULL, t, NULL, &a); |
ac4c8d6d ZJS |
172 | assert_se(r >= 0); |
173 | ||
174 | STRV_FOREACH(i, a) | |
175 | log_info("Got: <%s>", *i); | |
176 | ||
5fba7bbf TA |
177 | assert_se(streq_ptr(a[0], "one=BAR VAR\tGAR")); |
178 | assert_se(streq_ptr(a[1], "two=bar var\tgar")); | |
179 | assert_se(streq_ptr(a[2], "tri=bar var \tgar ")); | |
ac4c8d6d ZJS |
180 | assert_se(a[3] == NULL); |
181 | ||
182 | r = write_env_file(p, a); | |
183 | assert_se(r >= 0); | |
184 | ||
717603e3 | 185 | r = load_env_file(NULL, p, NULL, &b); |
ac4c8d6d | 186 | assert_se(r >= 0); |
ac4c8d6d ZJS |
187 | } |
188 | ||
37f3ffca | 189 | static void test_merge_env_file(void) { |
627d2bac | 190 | _cleanup_(unlink_tempfilep) char t[] = "/tmp/test-fileio-XXXXXX"; |
37f3ffca | 191 | int fd, r; |
9707d552 | 192 | _cleanup_fclose_ FILE *f = NULL; |
37f3ffca RS |
193 | _cleanup_strv_free_ char **a = NULL; |
194 | char **i; | |
195 | ||
196 | fd = mkostemp_safe(t); | |
197 | assert_se(fd >= 0); | |
198 | ||
199 | log_info("/* %s (%s) */", __func__, t); | |
200 | ||
201 | f = fdopen(fd, "w"); | |
202 | assert_se(f); | |
203 | ||
204 | r = write_string_stream(f, | |
205 | "one=1 \n" | |
206 | "twelve=${one}2\n" | |
207 | "twentyone=2${one}\n" | |
208 | "one=2\n" | |
ccad1fd0 ZJS |
209 | "twentytwo=2${one}\n" |
210 | "xxx_minus_three=$xxx - 3\n" | |
211 | "xxx=0x$one$one$one\n" | |
b82f58bf RS |
212 | "yyy=${one:-fallback}\n" |
213 | "zzz=${one:+replacement}\n" | |
214 | "zzzz=${foobar:-${nothing}}\n" | |
215 | "zzzzz=${nothing:+${nothing}}\n" | |
b1837133 | 216 | , WRITE_STRING_FILE_AVOID_NEWLINE); |
37f3ffca RS |
217 | assert(r >= 0); |
218 | ||
219 | r = merge_env_file(&a, NULL, t); | |
220 | assert_se(r >= 0); | |
221 | strv_sort(a); | |
222 | ||
223 | STRV_FOREACH(i, a) | |
224 | log_info("Got: <%s>", *i); | |
225 | ||
226 | assert_se(streq(a[0], "one=2")); | |
227 | assert_se(streq(a[1], "twelve=12")); | |
228 | assert_se(streq(a[2], "twentyone=21")); | |
229 | assert_se(streq(a[3], "twentytwo=22")); | |
ccad1fd0 ZJS |
230 | assert_se(streq(a[4], "xxx=0x222")); |
231 | assert_se(streq(a[5], "xxx_minus_three= - 3")); | |
b82f58bf RS |
232 | assert_se(streq(a[6], "yyy=2")); |
233 | assert_se(streq(a[7], "zzz=replacement")); | |
234 | assert_se(streq(a[8], "zzzz=")); | |
235 | assert_se(streq(a[9], "zzzzz=")); | |
236 | assert_se(a[10] == NULL); | |
37f3ffca RS |
237 | |
238 | r = merge_env_file(&a, NULL, t); | |
239 | assert_se(r >= 0); | |
240 | strv_sort(a); | |
241 | ||
242 | STRV_FOREACH(i, a) | |
243 | log_info("Got2: <%s>", *i); | |
244 | ||
245 | assert_se(streq(a[0], "one=2")); | |
246 | assert_se(streq(a[1], "twelve=12")); | |
247 | assert_se(streq(a[2], "twentyone=21")); | |
248 | assert_se(streq(a[3], "twentytwo=22")); | |
ccad1fd0 ZJS |
249 | assert_se(streq(a[4], "xxx=0x222")); |
250 | assert_se(streq(a[5], "xxx_minus_three=0x222 - 3")); | |
b82f58bf RS |
251 | assert_se(streq(a[6], "yyy=2")); |
252 | assert_se(streq(a[7], "zzz=replacement")); | |
253 | assert_se(streq(a[8], "zzzz=")); | |
254 | assert_se(streq(a[9], "zzzzz=")); | |
255 | assert_se(a[10] == NULL); | |
37f3ffca | 256 | } |
ac4c8d6d | 257 | |
184d1904 | 258 | static void test_merge_env_file_invalid(void) { |
627d2bac | 259 | _cleanup_(unlink_tempfilep) char t[] = "/tmp/test-fileio-XXXXXX"; |
184d1904 | 260 | int fd, r; |
9707d552 | 261 | _cleanup_fclose_ FILE *f = NULL; |
184d1904 ZJS |
262 | _cleanup_strv_free_ char **a = NULL; |
263 | char **i; | |
264 | ||
265 | fd = mkostemp_safe(t); | |
266 | assert_se(fd >= 0); | |
267 | ||
268 | log_info("/* %s (%s) */", __func__, t); | |
269 | ||
270 | f = fdopen(fd, "w"); | |
271 | assert_se(f); | |
272 | ||
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"; |
68fee104 | 299 | int fd, r; |
627d2bac | 300 | _cleanup_fclose_ FILE *f = NULL; |
68fee104 ZJS |
301 | char *command; |
302 | ||
646853bd | 303 | fd = mkostemp_safe(t); |
68fee104 ZJS |
304 | assert_se(fd >= 0); |
305 | ||
306 | f = fdopen(fd, "w"); | |
307 | assert_se(f); | |
308 | ||
309 | fputs("#! /bin/script -a -b \ngoo goo", f); | |
310 | fflush(f); | |
311 | ||
312 | r = executable_is_script(t, &command); | |
313 | assert_se(r > 0); | |
314 | assert_se(streq(command, "/bin/script")); | |
315 | free(command); | |
316 | ||
317 | r = executable_is_script("/bin/sh", &command); | |
318 | assert_se(r == 0); | |
319 | ||
320 | r = executable_is_script("/usr/bin/yum", &command); | |
321 | assert_se(r > 0 || r == -ENOENT); | |
322 | if (r > 0) { | |
323 | assert_se(startswith(command, "/")); | |
324 | free(command); | |
325 | } | |
68fee104 ZJS |
326 | } |
327 | ||
69ab8088 | 328 | static void test_status_field(void) { |
1e5413f7 ZJS |
329 | _cleanup_free_ char *t = NULL, *p = NULL, *s = NULL, *z = NULL; |
330 | unsigned long long total = 0, buffers = 0; | |
442e0083 | 331 | int r; |
69ab8088 | 332 | |
c4cd1d4d | 333 | assert_se(get_proc_field("/proc/self/status", "Threads", WHITESPACE, &t) == 0); |
69ab8088 ZJS |
334 | puts(t); |
335 | assert_se(streq(t, "1")); | |
336 | ||
c4cd1d4d | 337 | r = get_proc_field("/proc/meminfo", "MemTotal", WHITESPACE, &p); |
1e5413f7 | 338 | if (r != -ENOENT) { |
bdf7026e | 339 | assert_se(r == 0); |
1e5413f7 ZJS |
340 | puts(p); |
341 | assert_se(safe_atollu(p, &total) == 0); | |
342 | } | |
69ab8088 | 343 | |
c4cd1d4d | 344 | r = get_proc_field("/proc/meminfo", "Buffers", WHITESPACE, &s); |
1e5413f7 | 345 | if (r != -ENOENT) { |
bdf7026e | 346 | assert_se(r == 0); |
1e5413f7 ZJS |
347 | puts(s); |
348 | assert_se(safe_atollu(s, &buffers) == 0); | |
349 | } | |
69ab8088 | 350 | |
2b01a801 | 351 | if (p) |
bdf7026e | 352 | assert_se(buffers < total); |
1e5413f7 ZJS |
353 | |
354 | /* Seccomp should be a good test for field full of zeros. */ | |
c4cd1d4d | 355 | r = get_proc_field("/proc/meminfo", "Seccomp", WHITESPACE, &z); |
1e5413f7 | 356 | if (r != -ENOENT) { |
bdf7026e | 357 | assert_se(r == 0); |
1e5413f7 ZJS |
358 | puts(z); |
359 | assert_se(safe_atollu(z, &buffers) == 0); | |
360 | } | |
361 | } | |
362 | ||
363 | static void test_capeff(void) { | |
364 | int pid, p; | |
365 | ||
366 | for (pid = 0; pid < 2; pid++) { | |
367 | _cleanup_free_ char *capeff = NULL; | |
368 | int r; | |
369 | ||
370 | r = get_process_capeff(0, &capeff); | |
371 | log_info("capeff: '%s' (r=%d)", capeff, r); | |
372 | ||
4c701096 | 373 | if (IN_SET(r, -ENOENT, -EPERM)) |
1e5413f7 ZJS |
374 | return; |
375 | ||
bdf7026e TA |
376 | assert_se(r == 0); |
377 | assert_se(*capeff); | |
1a7906ae | 378 | p = capeff[strspn(capeff, HEXDIGITS)]; |
bdf7026e | 379 | assert_se(!p || isspace(p)); |
1e5413f7 | 380 | } |
69ab8088 ZJS |
381 | } |
382 | ||
0709b743 | 383 | static void test_write_string_stream(void) { |
627d2bac ZJS |
384 | _cleanup_(unlink_tempfilep) char fn[] = "/tmp/test-write_string_stream-XXXXXX"; |
385 | _cleanup_fclose_ FILE *f = NULL; | |
0709b743 RC |
386 | int fd; |
387 | char buf[64]; | |
388 | ||
646853bd | 389 | fd = mkostemp_safe(fn); |
0709b743 RC |
390 | assert_se(fd >= 0); |
391 | ||
392 | f = fdopen(fd, "r"); | |
393 | assert_se(f); | |
b1837133 | 394 | assert_se(write_string_stream(f, "boohoo", 0) < 0); |
36297390 | 395 | f = safe_fclose(f); |
0709b743 | 396 | |
36297390 | 397 | f = fopen(fn, "r+"); |
0709b743 RC |
398 | assert_se(f); |
399 | ||
b1837133 | 400 | assert_se(write_string_stream(f, "boohoo", 0) == 0); |
0709b743 RC |
401 | rewind(f); |
402 | ||
403 | assert_se(fgets(buf, sizeof(buf), f)); | |
404 | assert_se(streq(buf, "boohoo\n")); | |
36297390 | 405 | f = safe_fclose(f); |
0709b743 | 406 | |
36297390 | 407 | f = fopen(fn, "w+"); |
40beecdb DM |
408 | assert_se(f); |
409 | ||
b1837133 | 410 | assert_se(write_string_stream(f, "boohoo", WRITE_STRING_FILE_AVOID_NEWLINE) == 0); |
40beecdb DM |
411 | rewind(f); |
412 | ||
413 | assert_se(fgets(buf, sizeof(buf), f)); | |
414 | printf(">%s<", buf); | |
415 | assert_se(streq(buf, "boohoo")); | |
0709b743 RC |
416 | } |
417 | ||
418 | static void test_write_string_file(void) { | |
627d2bac | 419 | _cleanup_(unlink_tempfilep) char fn[] = "/tmp/test-write_string_file-XXXXXX"; |
76082570 RC |
420 | char buf[64] = {}; |
421 | _cleanup_close_ int fd; | |
0709b743 | 422 | |
646853bd | 423 | fd = mkostemp_safe(fn); |
0709b743 RC |
424 | assert_se(fd >= 0); |
425 | ||
4c1fc3e4 | 426 | assert_se(write_string_file(fn, "boohoo", WRITE_STRING_FILE_CREATE) == 0); |
0709b743 | 427 | |
cca0efb0 | 428 | assert_se(read(fd, buf, sizeof(buf)) == 7); |
0709b743 | 429 | assert_se(streq(buf, "boohoo\n")); |
0709b743 RC |
430 | } |
431 | ||
e07995a3 | 432 | static void test_write_string_file_no_create(void) { |
627d2bac | 433 | _cleanup_(unlink_tempfilep) char fn[] = "/tmp/test-write_string_file_no_create-XXXXXX"; |
e07995a3 RC |
434 | _cleanup_close_ int fd; |
435 | char buf[64] = {0}; | |
436 | ||
646853bd | 437 | fd = mkostemp_safe(fn); |
e07995a3 RC |
438 | assert_se(fd >= 0); |
439 | ||
4c1fc3e4 DM |
440 | assert_se(write_string_file("/a/file/which/does/not/exists/i/guess", "boohoo", 0) < 0); |
441 | assert_se(write_string_file(fn, "boohoo", 0) == 0); | |
e07995a3 | 442 | |
fbd0b64f | 443 | assert_se(read(fd, buf, sizeof(buf)) == STRLEN("boohoo\n")); |
e07995a3 | 444 | assert_se(streq(buf, "boohoo\n")); |
e07995a3 RC |
445 | } |
446 | ||
eb3da901 LP |
447 | static void test_write_string_file_verify(void) { |
448 | _cleanup_free_ char *buf = NULL, *buf2 = NULL; | |
449 | int r; | |
450 | ||
451 | assert_se(read_one_line_file("/proc/cmdline", &buf) >= 0); | |
dcd6361e | 452 | assert_se(buf2 = strjoin(buf, "\n")); |
eb3da901 LP |
453 | |
454 | r = write_string_file("/proc/cmdline", buf, 0); | |
4c701096 | 455 | assert_se(IN_SET(r, -EACCES, -EIO)); |
eb3da901 | 456 | r = write_string_file("/proc/cmdline", buf2, 0); |
4c701096 | 457 | assert_se(IN_SET(r, -EACCES, -EIO)); |
eb3da901 LP |
458 | |
459 | assert_se(write_string_file("/proc/cmdline", buf, WRITE_STRING_FILE_VERIFY_ON_FAILURE) == 0); | |
460 | assert_se(write_string_file("/proc/cmdline", buf2, WRITE_STRING_FILE_VERIFY_ON_FAILURE) == 0); | |
461 | ||
462 | r = write_string_file("/proc/cmdline", buf, WRITE_STRING_FILE_VERIFY_ON_FAILURE|WRITE_STRING_FILE_AVOID_NEWLINE); | |
4c701096 | 463 | assert_se(IN_SET(r, -EACCES, -EIO)); |
eb3da901 LP |
464 | assert_se(write_string_file("/proc/cmdline", buf2, WRITE_STRING_FILE_VERIFY_ON_FAILURE|WRITE_STRING_FILE_AVOID_NEWLINE) == 0); |
465 | } | |
466 | ||
e07995a3 | 467 | static void test_load_env_file_pairs(void) { |
627d2bac ZJS |
468 | _cleanup_(unlink_tempfilep) char fn[] = "/tmp/test-load_env_file_pairs-XXXXXX"; |
469 | int fd, r; | |
e07995a3 RC |
470 | _cleanup_fclose_ FILE *f = NULL; |
471 | _cleanup_strv_free_ char **l = NULL; | |
472 | char **k, **v; | |
473 | ||
646853bd | 474 | fd = mkostemp_safe(fn); |
e07995a3 RC |
475 | assert_se(fd >= 0); |
476 | ||
477 | r = write_string_file(fn, | |
478 | "NAME=\"Arch Linux\"\n" | |
479 | "ID=arch\n" | |
480 | "PRETTY_NAME=\"Arch Linux\"\n" | |
481 | "ANSI_COLOR=\"0;36\"\n" | |
482 | "HOME_URL=\"https://www.archlinux.org/\"\n" | |
483 | "SUPPORT_URL=\"https://bbs.archlinux.org/\"\n" | |
4c1fc3e4 DM |
484 | "BUG_REPORT_URL=\"https://bugs.archlinux.org/\"\n", |
485 | WRITE_STRING_FILE_CREATE); | |
e07995a3 RC |
486 | assert_se(r == 0); |
487 | ||
488 | f = fdopen(fd, "r"); | |
489 | assert_se(f); | |
490 | ||
491 | r = load_env_file_pairs(f, fn, NULL, &l); | |
492 | assert_se(r >= 0); | |
493 | ||
494 | assert_se(strv_length(l) == 14); | |
495 | STRV_FOREACH_PAIR(k, v, l) { | |
496 | assert_se(STR_IN_SET(*k, "NAME", "ID", "PRETTY_NAME", "ANSI_COLOR", "HOME_URL", "SUPPORT_URL", "BUG_REPORT_URL")); | |
497 | printf("%s=%s\n", *k, *v); | |
498 | if (streq(*k, "NAME")) assert_se(streq(*v, "Arch Linux")); | |
499 | if (streq(*k, "ID")) assert_se(streq(*v, "arch")); | |
500 | if (streq(*k, "PRETTY_NAME")) assert_se(streq(*v, "Arch Linux")); | |
501 | if (streq(*k, "ANSI_COLOR")) assert_se(streq(*v, "0;36")); | |
502 | if (streq(*k, "HOME_URL")) assert_se(streq(*v, "https://www.archlinux.org/")); | |
503 | if (streq(*k, "SUPPORT_URL")) assert_se(streq(*v, "https://bbs.archlinux.org/")); | |
504 | if (streq(*k, "BUG_REPORT_URL")) assert_se(streq(*v, "https://bugs.archlinux.org/")); | |
505 | } | |
e07995a3 RC |
506 | } |
507 | ||
897891f0 RC |
508 | static void test_search_and_fopen(void) { |
509 | const char *dirs[] = {"/tmp/foo/bar", "/tmp", NULL}; | |
627d2bac | 510 | |
897891f0 | 511 | char name[] = "/tmp/test-search_and_fopen.XXXXXX"; |
627d2bac | 512 | int fd, r; |
897891f0 RC |
513 | FILE *f; |
514 | ||
646853bd | 515 | fd = mkostemp_safe(name); |
897891f0 RC |
516 | assert_se(fd >= 0); |
517 | close(fd); | |
518 | ||
519 | r = search_and_fopen(basename(name), "r", NULL, dirs, &f); | |
520 | assert_se(r >= 0); | |
521 | fclose(f); | |
522 | ||
523 | r = search_and_fopen(name, "r", NULL, dirs, &f); | |
524 | assert_se(r >= 0); | |
525 | fclose(f); | |
526 | ||
527 | r = search_and_fopen(basename(name), "r", "/", dirs, &f); | |
528 | assert_se(r >= 0); | |
529 | fclose(f); | |
530 | ||
531 | r = search_and_fopen("/a/file/which/does/not/exist/i/guess", "r", NULL, dirs, &f); | |
532 | assert_se(r < 0); | |
533 | r = search_and_fopen("afilewhichdoesnotexistiguess", "r", NULL, dirs, &f); | |
534 | assert_se(r < 0); | |
535 | ||
536 | r = unlink(name); | |
537 | assert_se(r == 0); | |
538 | ||
539 | r = search_and_fopen(basename(name), "r", NULL, dirs, &f); | |
540 | assert_se(r < 0); | |
541 | } | |
542 | ||
897891f0 RC |
543 | static void test_search_and_fopen_nulstr(void) { |
544 | const char dirs[] = "/tmp/foo/bar\0/tmp\0"; | |
627d2bac ZJS |
545 | |
546 | _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-search_and_fopen.XXXXXX"; | |
547 | int fd, r; | |
897891f0 RC |
548 | FILE *f; |
549 | ||
646853bd | 550 | fd = mkostemp_safe(name); |
897891f0 RC |
551 | assert_se(fd >= 0); |
552 | close(fd); | |
553 | ||
554 | r = search_and_fopen_nulstr(basename(name), "r", NULL, dirs, &f); | |
555 | assert_se(r >= 0); | |
556 | fclose(f); | |
557 | ||
558 | r = search_and_fopen_nulstr(name, "r", NULL, dirs, &f); | |
559 | assert_se(r >= 0); | |
560 | fclose(f); | |
561 | ||
562 | r = search_and_fopen_nulstr("/a/file/which/does/not/exist/i/guess", "r", NULL, dirs, &f); | |
563 | assert_se(r < 0); | |
564 | r = search_and_fopen_nulstr("afilewhichdoesnotexistiguess", "r", NULL, dirs, &f); | |
565 | assert_se(r < 0); | |
566 | ||
567 | r = unlink(name); | |
568 | assert_se(r == 0); | |
569 | ||
570 | r = search_and_fopen_nulstr(basename(name), "r", NULL, dirs, &f); | |
571 | assert_se(r < 0); | |
572 | } | |
573 | ||
574 | static void test_writing_tmpfile(void) { | |
627d2bac | 575 | _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-systemd_writing_tmpfile.XXXXXX"; |
897891f0 RC |
576 | _cleanup_free_ char *contents = NULL; |
577 | size_t size; | |
9a92e255 | 578 | _cleanup_close_ int fd = -1; |
897891f0 | 579 | struct iovec iov[3]; |
627d2bac | 580 | int r; |
897891f0 | 581 | |
e6a7ec4b LP |
582 | iov[0] = IOVEC_MAKE_STRING("abc\n"); |
583 | iov[1] = IOVEC_MAKE_STRING(ALPHANUMERICAL "\n"); | |
584 | iov[2] = IOVEC_MAKE_STRING(""); | |
897891f0 | 585 | |
646853bd | 586 | fd = mkostemp_safe(name); |
897891f0 RC |
587 | printf("tmpfile: %s", name); |
588 | ||
589 | r = writev(fd, iov, 3); | |
590 | assert_se(r >= 0); | |
591 | ||
592 | r = read_full_file(name, &contents, &size); | |
593 | assert_se(r == 0); | |
594 | printf("contents: %s", contents); | |
595 | assert_se(streq(contents, "abc\n" ALPHANUMERICAL "\n")); | |
897891f0 RC |
596 | } |
597 | ||
598 | static void test_tempfn(void) { | |
599 | char *ret = NULL, *p; | |
600 | ||
601 | assert_se(tempfn_xxxxxx("/foo/bar/waldo", NULL, &ret) >= 0); | |
602 | assert_se(streq_ptr(ret, "/foo/bar/.#waldoXXXXXX")); | |
603 | free(ret); | |
604 | ||
605 | assert_se(tempfn_xxxxxx("/foo/bar/waldo", "[miau]", &ret) >= 0); | |
606 | assert_se(streq_ptr(ret, "/foo/bar/.#[miau]waldoXXXXXX")); | |
607 | free(ret); | |
608 | ||
609 | assert_se(tempfn_random("/foo/bar/waldo", NULL, &ret) >= 0); | |
610 | assert_se(p = startswith(ret, "/foo/bar/.#waldo")); | |
611 | assert_se(strlen(p) == 16); | |
612 | assert_se(in_charset(p, "0123456789abcdef")); | |
613 | free(ret); | |
614 | ||
615 | assert_se(tempfn_random("/foo/bar/waldo", "[wuff]", &ret) >= 0); | |
616 | assert_se(p = startswith(ret, "/foo/bar/.#[wuff]waldo")); | |
617 | assert_se(strlen(p) == 16); | |
618 | assert_se(in_charset(p, "0123456789abcdef")); | |
619 | free(ret); | |
620 | ||
621 | assert_se(tempfn_random_child("/foo/bar/waldo", NULL, &ret) >= 0); | |
622 | assert_se(p = startswith(ret, "/foo/bar/waldo/.#")); | |
623 | assert_se(strlen(p) == 16); | |
624 | assert_se(in_charset(p, "0123456789abcdef")); | |
625 | free(ret); | |
626 | ||
627 | assert_se(tempfn_random_child("/foo/bar/waldo", "[kikiriki]", &ret) >= 0); | |
628 | assert_se(p = startswith(ret, "/foo/bar/waldo/.#[kikiriki]")); | |
629 | assert_se(strlen(p) == 16); | |
630 | assert_se(in_charset(p, "0123456789abcdef")); | |
631 | free(ret); | |
632 | } | |
633 | ||
2c9de139 ZJS |
634 | static const char buffer[] = |
635 | "Some test data\n" | |
636 | "With newlines, and a NUL byte\0" | |
637 | "\n" | |
638 | "an empty line\n" | |
639 | "an ignored line\n" | |
640 | "and a very long line that is supposed to be truncated, because it is so long\n"; | |
641 | ||
642 | static void test_read_line_one_file(FILE *f) { | |
4f9a66a3 LP |
643 | _cleanup_free_ char *line = NULL; |
644 | ||
4f9a66a3 LP |
645 | assert_se(read_line(f, (size_t) -1, &line) == 15 && streq(line, "Some test data")); |
646 | line = mfree(line); | |
647 | ||
648 | assert_se(read_line(f, 1024, &line) == 30 && streq(line, "With newlines, and a NUL byte")); | |
649 | line = mfree(line); | |
650 | ||
651 | assert_se(read_line(f, 1024, &line) == 1 && streq(line, "")); | |
652 | line = mfree(line); | |
653 | ||
654 | assert_se(read_line(f, 1024, &line) == 14 && streq(line, "an empty line")); | |
655 | line = mfree(line); | |
656 | ||
657 | assert_se(read_line(f, (size_t) -1, NULL) == 16); | |
658 | ||
659 | assert_se(read_line(f, 16, &line) == -ENOBUFS); | |
660 | line = mfree(line); | |
661 | ||
662 | /* read_line() stopped when it hit the limit, that means when we continue reading we'll read at the first | |
663 | * character after the previous limit. Let's make use of tha to continue our test. */ | |
664 | assert_se(read_line(f, 1024, &line) == 61 && streq(line, "line that is supposed to be truncated, because it is so long")); | |
665 | line = mfree(line); | |
666 | ||
667 | assert_se(read_line(f, 1024, &line) == 1 && streq(line, "")); | |
668 | line = mfree(line); | |
669 | ||
670 | assert_se(read_line(f, 1024, &line) == 0 && streq(line, "")); | |
671 | } | |
672 | ||
2c9de139 ZJS |
673 | static void test_read_line(void) { |
674 | _cleanup_fclose_ FILE *f = NULL; | |
2c9de139 ZJS |
675 | |
676 | f = fmemopen((void*) buffer, sizeof(buffer), "re"); | |
677 | assert_se(f); | |
678 | ||
679 | test_read_line_one_file(f); | |
680 | } | |
681 | ||
682 | static void test_read_line2(void) { | |
627d2bac | 683 | _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-fileio.XXXXXX"; |
2c9de139 ZJS |
684 | int fd; |
685 | _cleanup_fclose_ FILE *f = NULL; | |
686 | ||
687 | fd = mkostemp_safe(name); | |
688 | assert_se(fd >= 0); | |
689 | assert_se((size_t) write(fd, buffer, sizeof(buffer)) == sizeof(buffer)); | |
690 | ||
691 | assert_se(lseek(fd, 0, SEEK_SET) == 0); | |
692 | assert_se(f = fdopen(fd, "r")); | |
693 | ||
694 | test_read_line_one_file(f); | |
695 | } | |
696 | ||
697 | static void test_read_line3(void) { | |
698 | _cleanup_fclose_ FILE *f = NULL; | |
699 | _cleanup_free_ char *line = NULL; | |
700 | int r; | |
701 | ||
702 | f = fopen("/proc/cmdline", "re"); | |
703 | if (!f && IN_SET(errno, ENOENT, EPERM)) | |
704 | return; | |
705 | assert_se(f); | |
706 | ||
707 | r = read_line(f, LINE_MAX, &line); | |
708 | assert_se((size_t) r == strlen(line) + 1); | |
709 | assert_se(read_line(f, LINE_MAX, NULL) == 0); | |
710 | } | |
711 | ||
f73141d7 | 712 | int main(int argc, char *argv[]) { |
37f3ffca | 713 | log_set_max_level(LOG_DEBUG); |
1e5413f7 ZJS |
714 | log_parse_environment(); |
715 | log_open(); | |
716 | ||
f73141d7 | 717 | test_parse_env_file(); |
ac4c8d6d | 718 | test_parse_multiline_env_file(); |
37f3ffca | 719 | test_merge_env_file(); |
184d1904 | 720 | test_merge_env_file_invalid(); |
68fee104 | 721 | test_executable_is_script(); |
69ab8088 | 722 | test_status_field(); |
1e5413f7 | 723 | test_capeff(); |
0709b743 RC |
724 | test_write_string_stream(); |
725 | test_write_string_file(); | |
e07995a3 | 726 | test_write_string_file_no_create(); |
eb3da901 | 727 | test_write_string_file_verify(); |
e07995a3 | 728 | test_load_env_file_pairs(); |
897891f0 RC |
729 | test_search_and_fopen(); |
730 | test_search_and_fopen_nulstr(); | |
731 | test_writing_tmpfile(); | |
732 | test_tempfn(); | |
4f9a66a3 | 733 | test_read_line(); |
2c9de139 ZJS |
734 | test_read_line2(); |
735 | test_read_line3(); | |
1e5413f7 | 736 | |
f73141d7 LP |
737 | return 0; |
738 | } |