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