]>
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; | |
212 | FILE *f; | |
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" | |
232 | , false); | |
37f3ffca RS |
233 | assert(r >= 0); |
234 | ||
235 | r = merge_env_file(&a, NULL, t); | |
236 | assert_se(r >= 0); | |
237 | strv_sort(a); | |
238 | ||
239 | STRV_FOREACH(i, a) | |
240 | log_info("Got: <%s>", *i); | |
241 | ||
242 | assert_se(streq(a[0], "one=2")); | |
243 | assert_se(streq(a[1], "twelve=12")); | |
244 | assert_se(streq(a[2], "twentyone=21")); | |
245 | assert_se(streq(a[3], "twentytwo=22")); | |
ccad1fd0 ZJS |
246 | assert_se(streq(a[4], "xxx=0x222")); |
247 | assert_se(streq(a[5], "xxx_minus_three= - 3")); | |
248 | assert_se(a[6] == NULL); | |
37f3ffca RS |
249 | |
250 | r = merge_env_file(&a, NULL, t); | |
251 | assert_se(r >= 0); | |
252 | strv_sort(a); | |
253 | ||
254 | STRV_FOREACH(i, a) | |
255 | log_info("Got2: <%s>", *i); | |
256 | ||
257 | assert_se(streq(a[0], "one=2")); | |
258 | assert_se(streq(a[1], "twelve=12")); | |
259 | assert_se(streq(a[2], "twentyone=21")); | |
260 | assert_se(streq(a[3], "twentytwo=22")); | |
ccad1fd0 ZJS |
261 | assert_se(streq(a[4], "xxx=0x222")); |
262 | assert_se(streq(a[5], "xxx_minus_three=0x222 - 3")); | |
263 | assert_se(a[6] == NULL); | |
37f3ffca | 264 | } |
ac4c8d6d | 265 | |
68fee104 ZJS |
266 | static void test_executable_is_script(void) { |
267 | char t[] = "/tmp/test-executable-XXXXXX"; | |
268 | int fd, r; | |
269 | FILE *f; | |
270 | char *command; | |
271 | ||
646853bd | 272 | fd = mkostemp_safe(t); |
68fee104 ZJS |
273 | assert_se(fd >= 0); |
274 | ||
275 | f = fdopen(fd, "w"); | |
276 | assert_se(f); | |
277 | ||
278 | fputs("#! /bin/script -a -b \ngoo goo", f); | |
279 | fflush(f); | |
280 | ||
281 | r = executable_is_script(t, &command); | |
282 | assert_se(r > 0); | |
283 | assert_se(streq(command, "/bin/script")); | |
284 | free(command); | |
285 | ||
286 | r = executable_is_script("/bin/sh", &command); | |
287 | assert_se(r == 0); | |
288 | ||
289 | r = executable_is_script("/usr/bin/yum", &command); | |
290 | assert_se(r > 0 || r == -ENOENT); | |
291 | if (r > 0) { | |
292 | assert_se(startswith(command, "/")); | |
293 | free(command); | |
294 | } | |
295 | ||
296 | fclose(f); | |
297 | unlink(t); | |
298 | } | |
299 | ||
69ab8088 | 300 | static void test_status_field(void) { |
1e5413f7 ZJS |
301 | _cleanup_free_ char *t = NULL, *p = NULL, *s = NULL, *z = NULL; |
302 | unsigned long long total = 0, buffers = 0; | |
442e0083 | 303 | int r; |
69ab8088 | 304 | |
c4cd1d4d | 305 | assert_se(get_proc_field("/proc/self/status", "Threads", WHITESPACE, &t) == 0); |
69ab8088 ZJS |
306 | puts(t); |
307 | assert_se(streq(t, "1")); | |
308 | ||
c4cd1d4d | 309 | r = get_proc_field("/proc/meminfo", "MemTotal", WHITESPACE, &p); |
1e5413f7 | 310 | if (r != -ENOENT) { |
bdf7026e | 311 | assert_se(r == 0); |
1e5413f7 ZJS |
312 | puts(p); |
313 | assert_se(safe_atollu(p, &total) == 0); | |
314 | } | |
69ab8088 | 315 | |
c4cd1d4d | 316 | r = get_proc_field("/proc/meminfo", "Buffers", WHITESPACE, &s); |
1e5413f7 | 317 | if (r != -ENOENT) { |
bdf7026e | 318 | assert_se(r == 0); |
1e5413f7 ZJS |
319 | puts(s); |
320 | assert_se(safe_atollu(s, &buffers) == 0); | |
321 | } | |
69ab8088 | 322 | |
2b01a801 | 323 | if (p) |
bdf7026e | 324 | assert_se(buffers < total); |
1e5413f7 ZJS |
325 | |
326 | /* Seccomp should be a good test for field full of zeros. */ | |
c4cd1d4d | 327 | r = get_proc_field("/proc/meminfo", "Seccomp", WHITESPACE, &z); |
1e5413f7 | 328 | if (r != -ENOENT) { |
bdf7026e | 329 | assert_se(r == 0); |
1e5413f7 ZJS |
330 | puts(z); |
331 | assert_se(safe_atollu(z, &buffers) == 0); | |
332 | } | |
333 | } | |
334 | ||
335 | static void test_capeff(void) { | |
336 | int pid, p; | |
337 | ||
338 | for (pid = 0; pid < 2; pid++) { | |
339 | _cleanup_free_ char *capeff = NULL; | |
340 | int r; | |
341 | ||
342 | r = get_process_capeff(0, &capeff); | |
343 | log_info("capeff: '%s' (r=%d)", capeff, r); | |
344 | ||
345 | if (r == -ENOENT || r == -EPERM) | |
346 | return; | |
347 | ||
bdf7026e TA |
348 | assert_se(r == 0); |
349 | assert_se(*capeff); | |
1a7906ae | 350 | p = capeff[strspn(capeff, HEXDIGITS)]; |
bdf7026e | 351 | assert_se(!p || isspace(p)); |
1e5413f7 | 352 | } |
69ab8088 ZJS |
353 | } |
354 | ||
0709b743 RC |
355 | static void test_write_string_stream(void) { |
356 | char fn[] = "/tmp/test-write_string_stream-XXXXXX"; | |
357 | _cleanup_fclose_ FILE *f = NULL; | |
358 | int fd; | |
359 | char buf[64]; | |
360 | ||
646853bd | 361 | fd = mkostemp_safe(fn); |
0709b743 RC |
362 | assert_se(fd >= 0); |
363 | ||
364 | f = fdopen(fd, "r"); | |
365 | assert_se(f); | |
40beecdb | 366 | assert_se(write_string_stream(f, "boohoo", true) < 0); |
0709b743 | 367 | |
76082570 | 368 | f = freopen(fn, "r+", f); |
0709b743 RC |
369 | assert_se(f); |
370 | ||
40beecdb | 371 | assert_se(write_string_stream(f, "boohoo", true) == 0); |
0709b743 RC |
372 | rewind(f); |
373 | ||
374 | assert_se(fgets(buf, sizeof(buf), f)); | |
375 | assert_se(streq(buf, "boohoo\n")); | |
376 | ||
40beecdb DM |
377 | f = freopen(fn, "w+", f); |
378 | assert_se(f); | |
379 | ||
380 | assert_se(write_string_stream(f, "boohoo", false) == 0); | |
381 | rewind(f); | |
382 | ||
383 | assert_se(fgets(buf, sizeof(buf), f)); | |
384 | printf(">%s<", buf); | |
385 | assert_se(streq(buf, "boohoo")); | |
386 | ||
0709b743 RC |
387 | unlink(fn); |
388 | } | |
389 | ||
390 | static void test_write_string_file(void) { | |
391 | char fn[] = "/tmp/test-write_string_file-XXXXXX"; | |
76082570 RC |
392 | char buf[64] = {}; |
393 | _cleanup_close_ int fd; | |
0709b743 | 394 | |
646853bd | 395 | fd = mkostemp_safe(fn); |
0709b743 RC |
396 | assert_se(fd >= 0); |
397 | ||
4c1fc3e4 | 398 | assert_se(write_string_file(fn, "boohoo", WRITE_STRING_FILE_CREATE) == 0); |
0709b743 | 399 | |
cca0efb0 | 400 | assert_se(read(fd, buf, sizeof(buf)) == 7); |
0709b743 RC |
401 | assert_se(streq(buf, "boohoo\n")); |
402 | ||
403 | unlink(fn); | |
404 | } | |
405 | ||
e07995a3 RC |
406 | static void test_write_string_file_no_create(void) { |
407 | char fn[] = "/tmp/test-write_string_file_no_create-XXXXXX"; | |
408 | _cleanup_close_ int fd; | |
409 | char buf[64] = {0}; | |
410 | ||
646853bd | 411 | fd = mkostemp_safe(fn); |
e07995a3 RC |
412 | assert_se(fd >= 0); |
413 | ||
4c1fc3e4 DM |
414 | assert_se(write_string_file("/a/file/which/does/not/exists/i/guess", "boohoo", 0) < 0); |
415 | assert_se(write_string_file(fn, "boohoo", 0) == 0); | |
e07995a3 | 416 | |
696c24fc | 417 | assert_se(read(fd, buf, sizeof(buf)) == strlen("boohoo\n")); |
e07995a3 RC |
418 | assert_se(streq(buf, "boohoo\n")); |
419 | ||
420 | unlink(fn); | |
421 | } | |
422 | ||
eb3da901 LP |
423 | static void test_write_string_file_verify(void) { |
424 | _cleanup_free_ char *buf = NULL, *buf2 = NULL; | |
425 | int r; | |
426 | ||
427 | assert_se(read_one_line_file("/proc/cmdline", &buf) >= 0); | |
605405c6 | 428 | assert_se((buf2 = strjoin(buf, "\n"))); |
eb3da901 LP |
429 | |
430 | r = write_string_file("/proc/cmdline", buf, 0); | |
431 | assert_se(r == -EACCES || r == -EIO); | |
432 | r = write_string_file("/proc/cmdline", buf2, 0); | |
433 | assert_se(r == -EACCES || r == -EIO); | |
434 | ||
435 | assert_se(write_string_file("/proc/cmdline", buf, WRITE_STRING_FILE_VERIFY_ON_FAILURE) == 0); | |
436 | assert_se(write_string_file("/proc/cmdline", buf2, WRITE_STRING_FILE_VERIFY_ON_FAILURE) == 0); | |
437 | ||
438 | r = write_string_file("/proc/cmdline", buf, WRITE_STRING_FILE_VERIFY_ON_FAILURE|WRITE_STRING_FILE_AVOID_NEWLINE); | |
439 | assert_se(r == -EACCES || r == -EIO); | |
440 | assert_se(write_string_file("/proc/cmdline", buf2, WRITE_STRING_FILE_VERIFY_ON_FAILURE|WRITE_STRING_FILE_AVOID_NEWLINE) == 0); | |
441 | } | |
442 | ||
e07995a3 RC |
443 | static void test_load_env_file_pairs(void) { |
444 | char fn[] = "/tmp/test-load_env_file_pairs-XXXXXX"; | |
445 | int fd; | |
446 | int r; | |
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 | ||
468 | r = load_env_file_pairs(f, fn, NULL, &l); | |
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 | } | |
483 | ||
484 | unlink(fn); | |
485 | } | |
486 | ||
897891f0 RC |
487 | static void test_search_and_fopen(void) { |
488 | const char *dirs[] = {"/tmp/foo/bar", "/tmp", NULL}; | |
489 | char name[] = "/tmp/test-search_and_fopen.XXXXXX"; | |
490 | int fd = -1; | |
491 | int r; | |
492 | FILE *f; | |
493 | ||
646853bd | 494 | fd = mkostemp_safe(name); |
897891f0 RC |
495 | assert_se(fd >= 0); |
496 | close(fd); | |
497 | ||
498 | r = search_and_fopen(basename(name), "r", NULL, dirs, &f); | |
499 | assert_se(r >= 0); | |
500 | fclose(f); | |
501 | ||
502 | r = search_and_fopen(name, "r", NULL, dirs, &f); | |
503 | assert_se(r >= 0); | |
504 | fclose(f); | |
505 | ||
506 | r = search_and_fopen(basename(name), "r", "/", dirs, &f); | |
507 | assert_se(r >= 0); | |
508 | fclose(f); | |
509 | ||
510 | r = search_and_fopen("/a/file/which/does/not/exist/i/guess", "r", NULL, dirs, &f); | |
511 | assert_se(r < 0); | |
512 | r = search_and_fopen("afilewhichdoesnotexistiguess", "r", NULL, dirs, &f); | |
513 | assert_se(r < 0); | |
514 | ||
515 | r = unlink(name); | |
516 | assert_se(r == 0); | |
517 | ||
518 | r = search_and_fopen(basename(name), "r", NULL, dirs, &f); | |
519 | assert_se(r < 0); | |
520 | } | |
521 | ||
522 | ||
523 | static void test_search_and_fopen_nulstr(void) { | |
524 | const char dirs[] = "/tmp/foo/bar\0/tmp\0"; | |
525 | char name[] = "/tmp/test-search_and_fopen.XXXXXX"; | |
526 | int fd = -1; | |
527 | int r; | |
528 | FILE *f; | |
529 | ||
646853bd | 530 | fd = mkostemp_safe(name); |
897891f0 RC |
531 | assert_se(fd >= 0); |
532 | close(fd); | |
533 | ||
534 | r = search_and_fopen_nulstr(basename(name), "r", NULL, dirs, &f); | |
535 | assert_se(r >= 0); | |
536 | fclose(f); | |
537 | ||
538 | r = search_and_fopen_nulstr(name, "r", NULL, dirs, &f); | |
539 | assert_se(r >= 0); | |
540 | fclose(f); | |
541 | ||
542 | r = search_and_fopen_nulstr("/a/file/which/does/not/exist/i/guess", "r", NULL, dirs, &f); | |
543 | assert_se(r < 0); | |
544 | r = search_and_fopen_nulstr("afilewhichdoesnotexistiguess", "r", NULL, dirs, &f); | |
545 | assert_se(r < 0); | |
546 | ||
547 | r = unlink(name); | |
548 | assert_se(r == 0); | |
549 | ||
550 | r = search_and_fopen_nulstr(basename(name), "r", NULL, dirs, &f); | |
551 | assert_se(r < 0); | |
552 | } | |
553 | ||
554 | static void test_writing_tmpfile(void) { | |
555 | char name[] = "/tmp/test-systemd_writing_tmpfile.XXXXXX"; | |
556 | _cleanup_free_ char *contents = NULL; | |
557 | size_t size; | |
558 | int fd, r; | |
559 | struct iovec iov[3]; | |
560 | ||
561 | IOVEC_SET_STRING(iov[0], "abc\n"); | |
562 | IOVEC_SET_STRING(iov[1], ALPHANUMERICAL "\n"); | |
563 | IOVEC_SET_STRING(iov[2], ""); | |
564 | ||
646853bd | 565 | fd = mkostemp_safe(name); |
897891f0 RC |
566 | printf("tmpfile: %s", name); |
567 | ||
568 | r = writev(fd, iov, 3); | |
569 | assert_se(r >= 0); | |
570 | ||
571 | r = read_full_file(name, &contents, &size); | |
572 | assert_se(r == 0); | |
573 | printf("contents: %s", contents); | |
574 | assert_se(streq(contents, "abc\n" ALPHANUMERICAL "\n")); | |
575 | ||
576 | unlink(name); | |
577 | } | |
578 | ||
579 | static void test_tempfn(void) { | |
580 | char *ret = NULL, *p; | |
581 | ||
582 | assert_se(tempfn_xxxxxx("/foo/bar/waldo", NULL, &ret) >= 0); | |
583 | assert_se(streq_ptr(ret, "/foo/bar/.#waldoXXXXXX")); | |
584 | free(ret); | |
585 | ||
586 | assert_se(tempfn_xxxxxx("/foo/bar/waldo", "[miau]", &ret) >= 0); | |
587 | assert_se(streq_ptr(ret, "/foo/bar/.#[miau]waldoXXXXXX")); | |
588 | free(ret); | |
589 | ||
590 | assert_se(tempfn_random("/foo/bar/waldo", NULL, &ret) >= 0); | |
591 | assert_se(p = startswith(ret, "/foo/bar/.#waldo")); | |
592 | assert_se(strlen(p) == 16); | |
593 | assert_se(in_charset(p, "0123456789abcdef")); | |
594 | free(ret); | |
595 | ||
596 | assert_se(tempfn_random("/foo/bar/waldo", "[wuff]", &ret) >= 0); | |
597 | assert_se(p = startswith(ret, "/foo/bar/.#[wuff]waldo")); | |
598 | assert_se(strlen(p) == 16); | |
599 | assert_se(in_charset(p, "0123456789abcdef")); | |
600 | free(ret); | |
601 | ||
602 | assert_se(tempfn_random_child("/foo/bar/waldo", NULL, &ret) >= 0); | |
603 | assert_se(p = startswith(ret, "/foo/bar/waldo/.#")); | |
604 | assert_se(strlen(p) == 16); | |
605 | assert_se(in_charset(p, "0123456789abcdef")); | |
606 | free(ret); | |
607 | ||
608 | assert_se(tempfn_random_child("/foo/bar/waldo", "[kikiriki]", &ret) >= 0); | |
609 | assert_se(p = startswith(ret, "/foo/bar/waldo/.#[kikiriki]")); | |
610 | assert_se(strlen(p) == 16); | |
611 | assert_se(in_charset(p, "0123456789abcdef")); | |
612 | free(ret); | |
613 | } | |
614 | ||
f73141d7 | 615 | int main(int argc, char *argv[]) { |
37f3ffca | 616 | log_set_max_level(LOG_DEBUG); |
1e5413f7 ZJS |
617 | log_parse_environment(); |
618 | log_open(); | |
619 | ||
f73141d7 | 620 | test_parse_env_file(); |
ac4c8d6d | 621 | test_parse_multiline_env_file(); |
37f3ffca | 622 | test_merge_env_file(); |
68fee104 | 623 | test_executable_is_script(); |
69ab8088 | 624 | test_status_field(); |
1e5413f7 | 625 | test_capeff(); |
0709b743 RC |
626 | test_write_string_stream(); |
627 | test_write_string_file(); | |
e07995a3 | 628 | test_write_string_file_no_create(); |
eb3da901 | 629 | test_write_string_file_verify(); |
e07995a3 | 630 | test_load_env_file_pairs(); |
897891f0 RC |
631 | test_search_and_fopen(); |
632 | test_search_and_fopen_nulstr(); | |
633 | test_writing_tmpfile(); | |
634 | test_tempfn(); | |
1e5413f7 | 635 | |
f73141d7 LP |
636 | return 0; |
637 | } |