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