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