]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
f73141d7 | 2 | |
f73141d7 | 3 | #include <fcntl.h> |
72fd79b3 | 4 | #include <limits.h> |
cf0fbc49 | 5 | #include <stdio.h> |
f73141d7 LP |
6 | #include <unistd.h> |
7 | ||
b5efdb8a | 8 | #include "alloc-util.h" |
1e5413f7 | 9 | #include "ctype.h" |
686d13b9 | 10 | #include "env-file.h" |
3ffd4af2 LP |
11 | #include "env-util.h" |
12 | #include "fd-util.h" | |
13 | #include "fileio.h" | |
627d2bac | 14 | #include "fs-util.h" |
897891f0 | 15 | #include "io-util.h" |
6bedfcbb | 16 | #include "parse-util.h" |
3ffd4af2 | 17 | #include "process-util.h" |
d3dcf4e3 | 18 | #include "random-util.h" |
b93d3f6b LP |
19 | #include "rm-rf.h" |
20 | #include "socket-util.h" | |
07630cea | 21 | #include "string-util.h" |
3ffd4af2 | 22 | #include "strv.h" |
6d7c4033 | 23 | #include "tests.h" |
e4de7287 | 24 | #include "tmpfile-util.h" |
3ffd4af2 | 25 | #include "util.h" |
f73141d7 LP |
26 | |
27 | static void test_parse_env_file(void) { | |
627d2bac ZJS |
28 | _cleanup_(unlink_tempfilep) char |
29 | t[] = "/tmp/test-fileio-in-XXXXXX", | |
095b30cb | 30 | p[] = "/tmp/test-fileio-out-XXXXXX"; |
f73141d7 | 31 | FILE *f; |
ebc05a09 | 32 | _cleanup_free_ char *one = NULL, *two = NULL, *three = NULL, *four = NULL, *five = NULL, |
e4a8db1f LT |
33 | *six = NULL, *seven = NULL, *eight = NULL, *nine = NULL, *ten = NULL, |
34 | *eleven = NULL, *twelve = NULL, *thirteen = NULL; | |
768100ef | 35 | _cleanup_strv_free_ char **a = NULL, **b = NULL; |
f73141d7 | 36 | char **i; |
768100ef | 37 | unsigned k; |
d8351049 | 38 | int r; |
f73141d7 | 39 | |
d8351049 | 40 | assert_se(fmkostemp_safe(t, "w", &f) == 0); |
f73141d7 LP |
41 | fputs("one=BAR \n" |
42 | "# comment\n" | |
43 | " # comment \n" | |
98f59e59 | 44 | " ; comment \n" |
f73141d7 LP |
45 | " two = bar \n" |
46 | "invalid line\n" | |
98f59e59 | 47 | "invalid line #comment\n" |
f73141d7 LP |
48 | "three = \"333\n" |
49 | "xxxx\"\n" | |
50 | "four = \'44\\\"44\'\n" | |
e4a8db1f | 51 | "five = \"55\\\"55\" \"FIVE\" cinco \n" |
f73141d7 LP |
52 | "six = seis sechs\\\n" |
53 | " sis\n" | |
98f59e59 HH |
54 | "seven=\"sevenval\" #nocomment\n" |
55 | "eight=eightval #nocomment\n" | |
ebc05a09 | 56 | "export nine=nineval\n" |
99003e01 ZJS |
57 | "ten=ignored\n" |
58 | "ten=ignored\n" | |
e4a8db1f LT |
59 | "ten=\n" |
60 | "eleven=\\value\n" | |
61 | "twelve=\"\\value\"\n" | |
62 | "thirteen='\\value'", f); | |
f73141d7 LP |
63 | |
64 | fflush(f); | |
65 | fclose(f); | |
66 | ||
aa8fbc74 | 67 | r = load_env_file(NULL, t, &a); |
ebc05a09 HH |
68 | assert_se(r >= 0); |
69 | ||
70 | STRV_FOREACH(i, a) | |
71 | log_info("Got: <%s>", *i); | |
72 | ||
5fba7bbf TA |
73 | assert_se(streq_ptr(a[0], "one=BAR")); |
74 | assert_se(streq_ptr(a[1], "two=bar")); | |
75 | assert_se(streq_ptr(a[2], "three=333\nxxxx")); | |
e4a8db1f LT |
76 | assert_se(streq_ptr(a[3], "four=44\\\"44")); |
77 | assert_se(streq_ptr(a[4], "five=55\"55FIVEcinco")); | |
5fba7bbf TA |
78 | assert_se(streq_ptr(a[5], "six=seis sechs sis")); |
79 | assert_se(streq_ptr(a[6], "seven=sevenval#nocomment")); | |
80 | assert_se(streq_ptr(a[7], "eight=eightval #nocomment")); | |
81 | assert_se(streq_ptr(a[8], "export nine=nineval")); | |
82 | assert_se(streq_ptr(a[9], "ten=")); | |
e4a8db1f LT |
83 | assert_se(streq_ptr(a[10], "eleven=value")); |
84 | assert_se(streq_ptr(a[11], "twelve=\\value")); | |
85 | assert_se(streq_ptr(a[12], "thirteen=\\value")); | |
86 | assert_se(a[13] == NULL); | |
ebc05a09 | 87 | |
039f0e70 | 88 | strv_env_clean(a); |
ebc05a09 | 89 | |
ebc05a09 HH |
90 | k = 0; |
91 | STRV_FOREACH(i, b) { | |
92 | log_info("Got2: <%s>", *i); | |
93 | assert_se(streq(*i, a[k++])); | |
94 | } | |
95 | ||
f73141d7 | 96 | r = parse_env_file( |
aa8fbc74 | 97 | NULL, t, |
f73141d7 LP |
98 | "one", &one, |
99 | "two", &two, | |
100 | "three", &three, | |
101 | "four", &four, | |
102 | "five", &five, | |
103 | "six", &six, | |
104 | "seven", &seven, | |
db537209 | 105 | "eight", &eight, |
ebc05a09 | 106 | "export nine", &nine, |
e4a8db1f LT |
107 | "ten", &ten, |
108 | "eleven", &eleven, | |
109 | "twelve", &twelve, | |
110 | "thirteen", &thirteen); | |
f73141d7 LP |
111 | |
112 | assert_se(r >= 0); | |
113 | ||
114 | log_info("one=[%s]", strna(one)); | |
115 | log_info("two=[%s]", strna(two)); | |
116 | log_info("three=[%s]", strna(three)); | |
117 | log_info("four=[%s]", strna(four)); | |
118 | log_info("five=[%s]", strna(five)); | |
119 | log_info("six=[%s]", strna(six)); | |
120 | log_info("seven=[%s]", strna(seven)); | |
db537209 | 121 | log_info("eight=[%s]", strna(eight)); |
ebc05a09 HH |
122 | log_info("export nine=[%s]", strna(nine)); |
123 | log_info("ten=[%s]", strna(nine)); | |
e4a8db1f LT |
124 | log_info("eleven=[%s]", strna(eleven)); |
125 | log_info("twelve=[%s]", strna(twelve)); | |
126 | log_info("thirteen=[%s]", strna(thirteen)); | |
f73141d7 LP |
127 | |
128 | assert_se(streq(one, "BAR")); | |
129 | assert_se(streq(two, "bar")); | |
130 | assert_se(streq(three, "333\nxxxx")); | |
e4a8db1f LT |
131 | assert_se(streq(four, "44\\\"44")); |
132 | assert_se(streq(five, "55\"55FIVEcinco")); | |
f73141d7 | 133 | assert_se(streq(six, "seis sechs sis")); |
98f59e59 HH |
134 | assert_se(streq(seven, "sevenval#nocomment")); |
135 | assert_se(streq(eight, "eightval #nocomment")); | |
ebc05a09 HH |
136 | assert_se(streq(nine, "nineval")); |
137 | assert_se(ten == NULL); | |
e4a8db1f LT |
138 | assert_se(streq(eleven, "value")); |
139 | assert_se(streq(twelve, "\\value")); | |
140 | assert_se(streq(thirteen, "\\value")); | |
f73141d7 | 141 | |
d8351049 ZJS |
142 | { |
143 | /* prepare a temporary file to write the environment to */ | |
144 | _cleanup_close_ int fd = mkostemp_safe(p); | |
145 | assert_se(fd >= 0); | |
146 | } | |
147 | ||
095b30cb | 148 | r = write_env_file(p, a); |
98f59e59 HH |
149 | assert_se(r >= 0); |
150 | ||
aa8fbc74 | 151 | r = load_env_file(NULL, p, &b); |
98f59e59 | 152 | assert_se(r >= 0); |
f73141d7 LP |
153 | } |
154 | ||
df8b14b5 LP |
155 | static void test_one_shell_var(const char *file, const char *variable, const char *value) { |
156 | _cleanup_free_ char *cmd = NULL, *from_shell = NULL; | |
649bde89 | 157 | _cleanup_pclose_ FILE *f = NULL; |
df8b14b5 LP |
158 | size_t sz; |
159 | ||
160 | assert_se(cmd = strjoin(". ", file, " && /bin/echo -n \"$", variable, "\"")); | |
161 | assert_se(f = popen(cmd, "re")); | |
162 | assert_se(read_full_stream(f, &from_shell, &sz) >= 0); | |
163 | assert_se(sz == strlen(value)); | |
164 | assert_se(streq(from_shell, value)); | |
165 | } | |
166 | ||
ac4c8d6d | 167 | static void test_parse_multiline_env_file(void) { |
627d2bac ZJS |
168 | _cleanup_(unlink_tempfilep) char |
169 | t[] = "/tmp/test-fileio-in-XXXXXX", | |
ac4c8d6d | 170 | p[] = "/tmp/test-fileio-out-XXXXXX"; |
ac4c8d6d ZJS |
171 | FILE *f; |
172 | _cleanup_strv_free_ char **a = NULL, **b = NULL; | |
173 | char **i; | |
d8351049 | 174 | int r; |
ac4c8d6d | 175 | |
d8351049 | 176 | assert_se(fmkostemp_safe(t, "w", &f) == 0); |
ac4c8d6d | 177 | fputs("one=BAR\\\n" |
6fe31963 LP |
178 | "\\ \\ \\ \\ VAR\\\n" |
179 | "\\\tGAR\n" | |
ac4c8d6d ZJS |
180 | "#comment\n" |
181 | "two=\"bar\\\n" | |
182 | " var\\\n" | |
183 | "\tgar\"\n" | |
184 | "#comment\n" | |
185 | "tri=\"bar \\\n" | |
186 | " var \\\n" | |
187 | "\tgar \"\n", f); | |
188 | ||
6fe31963 | 189 | assert_se(fflush_and_check(f) >= 0); |
ac4c8d6d ZJS |
190 | fclose(f); |
191 | ||
df8b14b5 LP |
192 | test_one_shell_var(t, "one", "BAR VAR\tGAR"); |
193 | test_one_shell_var(t, "two", "bar var\tgar"); | |
194 | test_one_shell_var(t, "tri", "bar var \tgar "); | |
195 | ||
aa8fbc74 | 196 | r = load_env_file(NULL, t, &a); |
ac4c8d6d ZJS |
197 | assert_se(r >= 0); |
198 | ||
199 | STRV_FOREACH(i, a) | |
200 | log_info("Got: <%s>", *i); | |
201 | ||
5fba7bbf TA |
202 | assert_se(streq_ptr(a[0], "one=BAR VAR\tGAR")); |
203 | assert_se(streq_ptr(a[1], "two=bar var\tgar")); | |
204 | assert_se(streq_ptr(a[2], "tri=bar var \tgar ")); | |
ac4c8d6d ZJS |
205 | assert_se(a[3] == NULL); |
206 | ||
d8351049 ZJS |
207 | { |
208 | _cleanup_close_ int fd = mkostemp_safe(p); | |
209 | assert_se(fd >= 0); | |
210 | } | |
211 | ||
ac4c8d6d ZJS |
212 | r = write_env_file(p, a); |
213 | assert_se(r >= 0); | |
214 | ||
aa8fbc74 | 215 | r = load_env_file(NULL, p, &b); |
ac4c8d6d | 216 | assert_se(r >= 0); |
ac4c8d6d ZJS |
217 | } |
218 | ||
37f3ffca | 219 | static void test_merge_env_file(void) { |
627d2bac | 220 | _cleanup_(unlink_tempfilep) char t[] = "/tmp/test-fileio-XXXXXX"; |
9707d552 | 221 | _cleanup_fclose_ FILE *f = NULL; |
37f3ffca RS |
222 | _cleanup_strv_free_ char **a = NULL; |
223 | char **i; | |
d8351049 | 224 | int r; |
37f3ffca | 225 | |
d8351049 | 226 | assert_se(fmkostemp_safe(t, "w", &f) == 0); |
37f3ffca RS |
227 | log_info("/* %s (%s) */", __func__, t); |
228 | ||
37f3ffca RS |
229 | r = write_string_stream(f, |
230 | "one=1 \n" | |
231 | "twelve=${one}2\n" | |
232 | "twentyone=2${one}\n" | |
233 | "one=2\n" | |
ccad1fd0 ZJS |
234 | "twentytwo=2${one}\n" |
235 | "xxx_minus_three=$xxx - 3\n" | |
236 | "xxx=0x$one$one$one\n" | |
b82f58bf RS |
237 | "yyy=${one:-fallback}\n" |
238 | "zzz=${one:+replacement}\n" | |
239 | "zzzz=${foobar:-${nothing}}\n" | |
240 | "zzzzz=${nothing:+${nothing}}\n" | |
b1837133 | 241 | , WRITE_STRING_FILE_AVOID_NEWLINE); |
37f3ffca RS |
242 | assert(r >= 0); |
243 | ||
244 | r = merge_env_file(&a, NULL, t); | |
245 | assert_se(r >= 0); | |
246 | strv_sort(a); | |
247 | ||
248 | STRV_FOREACH(i, a) | |
249 | log_info("Got: <%s>", *i); | |
250 | ||
251 | assert_se(streq(a[0], "one=2")); | |
252 | assert_se(streq(a[1], "twelve=12")); | |
253 | assert_se(streq(a[2], "twentyone=21")); | |
254 | assert_se(streq(a[3], "twentytwo=22")); | |
ccad1fd0 ZJS |
255 | assert_se(streq(a[4], "xxx=0x222")); |
256 | assert_se(streq(a[5], "xxx_minus_three= - 3")); | |
b82f58bf RS |
257 | assert_se(streq(a[6], "yyy=2")); |
258 | assert_se(streq(a[7], "zzz=replacement")); | |
259 | assert_se(streq(a[8], "zzzz=")); | |
260 | assert_se(streq(a[9], "zzzzz=")); | |
261 | assert_se(a[10] == NULL); | |
37f3ffca RS |
262 | |
263 | r = merge_env_file(&a, NULL, t); | |
264 | assert_se(r >= 0); | |
265 | strv_sort(a); | |
266 | ||
267 | STRV_FOREACH(i, a) | |
268 | log_info("Got2: <%s>", *i); | |
269 | ||
270 | assert_se(streq(a[0], "one=2")); | |
271 | assert_se(streq(a[1], "twelve=12")); | |
272 | assert_se(streq(a[2], "twentyone=21")); | |
273 | assert_se(streq(a[3], "twentytwo=22")); | |
ccad1fd0 ZJS |
274 | assert_se(streq(a[4], "xxx=0x222")); |
275 | assert_se(streq(a[5], "xxx_minus_three=0x222 - 3")); | |
b82f58bf RS |
276 | assert_se(streq(a[6], "yyy=2")); |
277 | assert_se(streq(a[7], "zzz=replacement")); | |
278 | assert_se(streq(a[8], "zzzz=")); | |
279 | assert_se(streq(a[9], "zzzzz=")); | |
280 | assert_se(a[10] == NULL); | |
37f3ffca | 281 | } |
ac4c8d6d | 282 | |
184d1904 | 283 | static void test_merge_env_file_invalid(void) { |
627d2bac | 284 | _cleanup_(unlink_tempfilep) char t[] = "/tmp/test-fileio-XXXXXX"; |
9707d552 | 285 | _cleanup_fclose_ FILE *f = NULL; |
184d1904 ZJS |
286 | _cleanup_strv_free_ char **a = NULL; |
287 | char **i; | |
d8351049 | 288 | int r; |
184d1904 | 289 | |
d8351049 | 290 | assert_se(fmkostemp_safe(t, "w", &f) == 0); |
184d1904 ZJS |
291 | log_info("/* %s (%s) */", __func__, t); |
292 | ||
184d1904 ZJS |
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 | 317 | static void test_executable_is_script(void) { |
627d2bac | 318 | _cleanup_(unlink_tempfilep) char t[] = "/tmp/test-fileio-XXXXXX"; |
627d2bac | 319 | _cleanup_fclose_ FILE *f = NULL; |
68fee104 | 320 | char *command; |
d8351049 | 321 | int r; |
68fee104 | 322 | |
d8351049 | 323 | assert_se(fmkostemp_safe(t, "w", &f) == 0); |
68fee104 ZJS |
324 | fputs("#! /bin/script -a -b \ngoo goo", f); |
325 | fflush(f); | |
326 | ||
327 | r = executable_is_script(t, &command); | |
328 | assert_se(r > 0); | |
329 | assert_se(streq(command, "/bin/script")); | |
330 | free(command); | |
331 | ||
332 | r = executable_is_script("/bin/sh", &command); | |
333 | assert_se(r == 0); | |
334 | ||
335 | r = executable_is_script("/usr/bin/yum", &command); | |
336 | assert_se(r > 0 || r == -ENOENT); | |
337 | if (r > 0) { | |
338 | assert_se(startswith(command, "/")); | |
339 | free(command); | |
340 | } | |
68fee104 ZJS |
341 | } |
342 | ||
69ab8088 | 343 | static void test_status_field(void) { |
1e5413f7 ZJS |
344 | _cleanup_free_ char *t = NULL, *p = NULL, *s = NULL, *z = NULL; |
345 | unsigned long long total = 0, buffers = 0; | |
442e0083 | 346 | int r; |
69ab8088 | 347 | |
c4cd1d4d | 348 | assert_se(get_proc_field("/proc/self/status", "Threads", WHITESPACE, &t) == 0); |
69ab8088 ZJS |
349 | puts(t); |
350 | assert_se(streq(t, "1")); | |
351 | ||
c4cd1d4d | 352 | r = get_proc_field("/proc/meminfo", "MemTotal", WHITESPACE, &p); |
1e5413f7 | 353 | if (r != -ENOENT) { |
bdf7026e | 354 | assert_se(r == 0); |
1e5413f7 ZJS |
355 | puts(p); |
356 | assert_se(safe_atollu(p, &total) == 0); | |
357 | } | |
69ab8088 | 358 | |
c4cd1d4d | 359 | r = get_proc_field("/proc/meminfo", "Buffers", WHITESPACE, &s); |
1e5413f7 | 360 | if (r != -ENOENT) { |
bdf7026e | 361 | assert_se(r == 0); |
1e5413f7 ZJS |
362 | puts(s); |
363 | assert_se(safe_atollu(s, &buffers) == 0); | |
364 | } | |
69ab8088 | 365 | |
2b01a801 | 366 | if (p) |
bdf7026e | 367 | assert_se(buffers < total); |
1e5413f7 ZJS |
368 | |
369 | /* Seccomp should be a good test for field full of zeros. */ | |
c4cd1d4d | 370 | r = get_proc_field("/proc/meminfo", "Seccomp", WHITESPACE, &z); |
1e5413f7 | 371 | if (r != -ENOENT) { |
bdf7026e | 372 | assert_se(r == 0); |
1e5413f7 ZJS |
373 | puts(z); |
374 | assert_se(safe_atollu(z, &buffers) == 0); | |
375 | } | |
376 | } | |
377 | ||
378 | static void test_capeff(void) { | |
379 | int pid, p; | |
380 | ||
381 | for (pid = 0; pid < 2; pid++) { | |
382 | _cleanup_free_ char *capeff = NULL; | |
383 | int r; | |
384 | ||
385 | r = get_process_capeff(0, &capeff); | |
386 | log_info("capeff: '%s' (r=%d)", capeff, r); | |
387 | ||
4c701096 | 388 | if (IN_SET(r, -ENOENT, -EPERM)) |
1e5413f7 ZJS |
389 | return; |
390 | ||
bdf7026e TA |
391 | assert_se(r == 0); |
392 | assert_se(*capeff); | |
1a7906ae | 393 | p = capeff[strspn(capeff, HEXDIGITS)]; |
bdf7026e | 394 | assert_se(!p || isspace(p)); |
1e5413f7 | 395 | } |
69ab8088 ZJS |
396 | } |
397 | ||
0709b743 | 398 | static void test_write_string_stream(void) { |
627d2bac ZJS |
399 | _cleanup_(unlink_tempfilep) char fn[] = "/tmp/test-write_string_stream-XXXXXX"; |
400 | _cleanup_fclose_ FILE *f = NULL; | |
0709b743 RC |
401 | int fd; |
402 | char buf[64]; | |
403 | ||
646853bd | 404 | fd = mkostemp_safe(fn); |
0709b743 RC |
405 | assert_se(fd >= 0); |
406 | ||
407 | f = fdopen(fd, "r"); | |
408 | assert_se(f); | |
b1837133 | 409 | assert_se(write_string_stream(f, "boohoo", 0) < 0); |
36297390 | 410 | f = safe_fclose(f); |
0709b743 | 411 | |
36297390 | 412 | f = fopen(fn, "r+"); |
0709b743 RC |
413 | assert_se(f); |
414 | ||
b1837133 | 415 | assert_se(write_string_stream(f, "boohoo", 0) == 0); |
0709b743 RC |
416 | rewind(f); |
417 | ||
418 | assert_se(fgets(buf, sizeof(buf), f)); | |
419 | assert_se(streq(buf, "boohoo\n")); | |
36297390 | 420 | f = safe_fclose(f); |
0709b743 | 421 | |
36297390 | 422 | f = fopen(fn, "w+"); |
40beecdb DM |
423 | assert_se(f); |
424 | ||
b1837133 | 425 | assert_se(write_string_stream(f, "boohoo", WRITE_STRING_FILE_AVOID_NEWLINE) == 0); |
40beecdb DM |
426 | rewind(f); |
427 | ||
428 | assert_se(fgets(buf, sizeof(buf), f)); | |
429 | printf(">%s<", buf); | |
430 | assert_se(streq(buf, "boohoo")); | |
0709b743 RC |
431 | } |
432 | ||
433 | static void test_write_string_file(void) { | |
627d2bac | 434 | _cleanup_(unlink_tempfilep) char fn[] = "/tmp/test-write_string_file-XXXXXX"; |
76082570 RC |
435 | char buf[64] = {}; |
436 | _cleanup_close_ int fd; | |
0709b743 | 437 | |
646853bd | 438 | fd = mkostemp_safe(fn); |
0709b743 RC |
439 | assert_se(fd >= 0); |
440 | ||
4c1fc3e4 | 441 | assert_se(write_string_file(fn, "boohoo", WRITE_STRING_FILE_CREATE) == 0); |
0709b743 | 442 | |
cca0efb0 | 443 | assert_se(read(fd, buf, sizeof(buf)) == 7); |
0709b743 | 444 | assert_se(streq(buf, "boohoo\n")); |
0709b743 RC |
445 | } |
446 | ||
e07995a3 | 447 | static void test_write_string_file_no_create(void) { |
627d2bac | 448 | _cleanup_(unlink_tempfilep) char fn[] = "/tmp/test-write_string_file_no_create-XXXXXX"; |
e07995a3 | 449 | _cleanup_close_ int fd; |
2aa07538 | 450 | char buf[64] = {}; |
e07995a3 | 451 | |
646853bd | 452 | fd = mkostemp_safe(fn); |
e07995a3 RC |
453 | assert_se(fd >= 0); |
454 | ||
4c1fc3e4 DM |
455 | assert_se(write_string_file("/a/file/which/does/not/exists/i/guess", "boohoo", 0) < 0); |
456 | assert_se(write_string_file(fn, "boohoo", 0) == 0); | |
e07995a3 | 457 | |
2aa07538 | 458 | assert_se(read(fd, buf, sizeof buf) == (ssize_t) strlen("boohoo\n")); |
e07995a3 | 459 | assert_se(streq(buf, "boohoo\n")); |
e07995a3 RC |
460 | } |
461 | ||
eb3da901 LP |
462 | static void test_write_string_file_verify(void) { |
463 | _cleanup_free_ char *buf = NULL, *buf2 = NULL; | |
464 | int r; | |
465 | ||
3c14dc61 TM |
466 | r = read_one_line_file("/proc/version", &buf); |
467 | if (ERRNO_IS_PRIVILEGE(r)) | |
468 | return; | |
469 | assert_se(r >= 0); | |
dcd6361e | 470 | assert_se(buf2 = strjoin(buf, "\n")); |
eb3da901 | 471 | |
30b84c78 | 472 | r = write_string_file("/proc/version", buf, 0); |
4c701096 | 473 | assert_se(IN_SET(r, -EACCES, -EIO)); |
30b84c78 | 474 | r = write_string_file("/proc/version", buf2, 0); |
4c701096 | 475 | assert_se(IN_SET(r, -EACCES, -EIO)); |
eb3da901 | 476 | |
30b84c78 ZJS |
477 | assert_se(write_string_file("/proc/version", buf, WRITE_STRING_FILE_VERIFY_ON_FAILURE) == 0); |
478 | assert_se(write_string_file("/proc/version", buf2, WRITE_STRING_FILE_VERIFY_ON_FAILURE) == 0); | |
eb3da901 | 479 | |
30b84c78 | 480 | r = write_string_file("/proc/version", buf, WRITE_STRING_FILE_VERIFY_ON_FAILURE|WRITE_STRING_FILE_AVOID_NEWLINE); |
4c701096 | 481 | assert_se(IN_SET(r, -EACCES, -EIO)); |
30b84c78 | 482 | assert_se(write_string_file("/proc/version", buf2, WRITE_STRING_FILE_VERIFY_ON_FAILURE|WRITE_STRING_FILE_AVOID_NEWLINE) == 0); |
eb3da901 LP |
483 | } |
484 | ||
e07995a3 | 485 | static void test_load_env_file_pairs(void) { |
627d2bac ZJS |
486 | _cleanup_(unlink_tempfilep) char fn[] = "/tmp/test-load_env_file_pairs-XXXXXX"; |
487 | int fd, r; | |
e07995a3 RC |
488 | _cleanup_fclose_ FILE *f = NULL; |
489 | _cleanup_strv_free_ char **l = NULL; | |
490 | char **k, **v; | |
491 | ||
646853bd | 492 | fd = mkostemp_safe(fn); |
e07995a3 RC |
493 | assert_se(fd >= 0); |
494 | ||
495 | r = write_string_file(fn, | |
496 | "NAME=\"Arch Linux\"\n" | |
497 | "ID=arch\n" | |
498 | "PRETTY_NAME=\"Arch Linux\"\n" | |
499 | "ANSI_COLOR=\"0;36\"\n" | |
500 | "HOME_URL=\"https://www.archlinux.org/\"\n" | |
501 | "SUPPORT_URL=\"https://bbs.archlinux.org/\"\n" | |
4c1fc3e4 DM |
502 | "BUG_REPORT_URL=\"https://bugs.archlinux.org/\"\n", |
503 | WRITE_STRING_FILE_CREATE); | |
e07995a3 RC |
504 | assert_se(r == 0); |
505 | ||
506 | f = fdopen(fd, "r"); | |
507 | assert_se(f); | |
508 | ||
aa8fbc74 | 509 | r = load_env_file_pairs(f, fn, &l); |
e07995a3 RC |
510 | assert_se(r >= 0); |
511 | ||
512 | assert_se(strv_length(l) == 14); | |
513 | STRV_FOREACH_PAIR(k, v, l) { | |
514 | assert_se(STR_IN_SET(*k, "NAME", "ID", "PRETTY_NAME", "ANSI_COLOR", "HOME_URL", "SUPPORT_URL", "BUG_REPORT_URL")); | |
515 | printf("%s=%s\n", *k, *v); | |
516 | if (streq(*k, "NAME")) assert_se(streq(*v, "Arch Linux")); | |
517 | if (streq(*k, "ID")) assert_se(streq(*v, "arch")); | |
518 | if (streq(*k, "PRETTY_NAME")) assert_se(streq(*v, "Arch Linux")); | |
519 | if (streq(*k, "ANSI_COLOR")) assert_se(streq(*v, "0;36")); | |
520 | if (streq(*k, "HOME_URL")) assert_se(streq(*v, "https://www.archlinux.org/")); | |
521 | if (streq(*k, "SUPPORT_URL")) assert_se(streq(*v, "https://bbs.archlinux.org/")); | |
522 | if (streq(*k, "BUG_REPORT_URL")) assert_se(streq(*v, "https://bugs.archlinux.org/")); | |
523 | } | |
e07995a3 RC |
524 | } |
525 | ||
897891f0 RC |
526 | static void test_search_and_fopen(void) { |
527 | const char *dirs[] = {"/tmp/foo/bar", "/tmp", NULL}; | |
627d2bac | 528 | |
897891f0 | 529 | char name[] = "/tmp/test-search_and_fopen.XXXXXX"; |
627d2bac | 530 | int fd, r; |
897891f0 RC |
531 | FILE *f; |
532 | ||
646853bd | 533 | fd = mkostemp_safe(name); |
897891f0 RC |
534 | assert_se(fd >= 0); |
535 | close(fd); | |
536 | ||
537 | r = search_and_fopen(basename(name), "r", NULL, dirs, &f); | |
538 | assert_se(r >= 0); | |
539 | fclose(f); | |
540 | ||
541 | r = search_and_fopen(name, "r", NULL, dirs, &f); | |
542 | assert_se(r >= 0); | |
543 | fclose(f); | |
544 | ||
545 | r = search_and_fopen(basename(name), "r", "/", dirs, &f); | |
546 | assert_se(r >= 0); | |
547 | fclose(f); | |
548 | ||
549 | r = search_and_fopen("/a/file/which/does/not/exist/i/guess", "r", NULL, dirs, &f); | |
550 | assert_se(r < 0); | |
551 | r = search_and_fopen("afilewhichdoesnotexistiguess", "r", NULL, dirs, &f); | |
552 | assert_se(r < 0); | |
553 | ||
554 | r = unlink(name); | |
555 | assert_se(r == 0); | |
556 | ||
557 | r = search_and_fopen(basename(name), "r", NULL, dirs, &f); | |
558 | assert_se(r < 0); | |
559 | } | |
560 | ||
897891f0 RC |
561 | static void test_search_and_fopen_nulstr(void) { |
562 | const char dirs[] = "/tmp/foo/bar\0/tmp\0"; | |
627d2bac ZJS |
563 | |
564 | _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-search_and_fopen.XXXXXX"; | |
565 | int fd, r; | |
897891f0 RC |
566 | FILE *f; |
567 | ||
646853bd | 568 | fd = mkostemp_safe(name); |
897891f0 RC |
569 | assert_se(fd >= 0); |
570 | close(fd); | |
571 | ||
572 | r = search_and_fopen_nulstr(basename(name), "r", NULL, dirs, &f); | |
573 | assert_se(r >= 0); | |
574 | fclose(f); | |
575 | ||
576 | r = search_and_fopen_nulstr(name, "r", NULL, dirs, &f); | |
577 | assert_se(r >= 0); | |
578 | fclose(f); | |
579 | ||
580 | r = search_and_fopen_nulstr("/a/file/which/does/not/exist/i/guess", "r", NULL, dirs, &f); | |
581 | assert_se(r < 0); | |
582 | r = search_and_fopen_nulstr("afilewhichdoesnotexistiguess", "r", NULL, dirs, &f); | |
583 | assert_se(r < 0); | |
584 | ||
585 | r = unlink(name); | |
586 | assert_se(r == 0); | |
587 | ||
588 | r = search_and_fopen_nulstr(basename(name), "r", NULL, dirs, &f); | |
589 | assert_se(r < 0); | |
590 | } | |
591 | ||
592 | static void test_writing_tmpfile(void) { | |
627d2bac | 593 | _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-systemd_writing_tmpfile.XXXXXX"; |
897891f0 RC |
594 | _cleanup_free_ char *contents = NULL; |
595 | size_t size; | |
9a92e255 | 596 | _cleanup_close_ int fd = -1; |
897891f0 | 597 | struct iovec iov[3]; |
627d2bac | 598 | int r; |
897891f0 | 599 | |
e6a7ec4b LP |
600 | iov[0] = IOVEC_MAKE_STRING("abc\n"); |
601 | iov[1] = IOVEC_MAKE_STRING(ALPHANUMERICAL "\n"); | |
602 | iov[2] = IOVEC_MAKE_STRING(""); | |
897891f0 | 603 | |
646853bd | 604 | fd = mkostemp_safe(name); |
897891f0 RC |
605 | printf("tmpfile: %s", name); |
606 | ||
607 | r = writev(fd, iov, 3); | |
608 | assert_se(r >= 0); | |
609 | ||
610 | r = read_full_file(name, &contents, &size); | |
611 | assert_se(r == 0); | |
612 | printf("contents: %s", contents); | |
613 | assert_se(streq(contents, "abc\n" ALPHANUMERICAL "\n")); | |
897891f0 RC |
614 | } |
615 | ||
616 | static void test_tempfn(void) { | |
617 | char *ret = NULL, *p; | |
618 | ||
619 | assert_se(tempfn_xxxxxx("/foo/bar/waldo", NULL, &ret) >= 0); | |
620 | assert_se(streq_ptr(ret, "/foo/bar/.#waldoXXXXXX")); | |
621 | free(ret); | |
622 | ||
623 | assert_se(tempfn_xxxxxx("/foo/bar/waldo", "[miau]", &ret) >= 0); | |
624 | assert_se(streq_ptr(ret, "/foo/bar/.#[miau]waldoXXXXXX")); | |
625 | free(ret); | |
626 | ||
627 | assert_se(tempfn_random("/foo/bar/waldo", NULL, &ret) >= 0); | |
628 | assert_se(p = startswith(ret, "/foo/bar/.#waldo")); | |
629 | assert_se(strlen(p) == 16); | |
630 | assert_se(in_charset(p, "0123456789abcdef")); | |
631 | free(ret); | |
632 | ||
633 | assert_se(tempfn_random("/foo/bar/waldo", "[wuff]", &ret) >= 0); | |
634 | assert_se(p = startswith(ret, "/foo/bar/.#[wuff]waldo")); | |
635 | assert_se(strlen(p) == 16); | |
636 | assert_se(in_charset(p, "0123456789abcdef")); | |
637 | free(ret); | |
638 | ||
639 | assert_se(tempfn_random_child("/foo/bar/waldo", NULL, &ret) >= 0); | |
640 | assert_se(p = startswith(ret, "/foo/bar/waldo/.#")); | |
641 | assert_se(strlen(p) == 16); | |
642 | assert_se(in_charset(p, "0123456789abcdef")); | |
643 | free(ret); | |
644 | ||
645 | assert_se(tempfn_random_child("/foo/bar/waldo", "[kikiriki]", &ret) >= 0); | |
646 | assert_se(p = startswith(ret, "/foo/bar/waldo/.#[kikiriki]")); | |
647 | assert_se(strlen(p) == 16); | |
648 | assert_se(in_charset(p, "0123456789abcdef")); | |
649 | free(ret); | |
650 | } | |
651 | ||
7f9d1aed | 652 | static const char chars[] = |
cdce33f9 | 653 | "Aąę„”\n루\377"; |
7f9d1aed | 654 | |
6028d766 | 655 | DISABLE_WARNING_TYPE_LIMITS; |
d3bdba38 | 656 | |
7f9d1aed ZJS |
657 | static void test_fgetc(void) { |
658 | _cleanup_fclose_ FILE *f = NULL; | |
659 | char c; | |
660 | ||
673a1e6f | 661 | f = fmemopen_unlocked((void*) chars, sizeof(chars), "re"); |
7f9d1aed ZJS |
662 | assert_se(f); |
663 | ||
72fd79b3 | 664 | for (size_t i = 0; i < sizeof(chars); i++) { |
7f9d1aed ZJS |
665 | assert_se(safe_fgetc(f, &c) == 1); |
666 | assert_se(c == chars[i]); | |
667 | ||
72fd79b3 LP |
668 | if (ungetc(c, f) == EOF) { |
669 | /* EOF is -1, and hence we can't push value 255 in this way – if char is signed */ | |
670 | assert_se(c == (char) EOF); | |
671 | assert_se(CHAR_MIN == -128); /* verify that char is signed on this platform */ | |
672 | } else { | |
673 | assert_se(safe_fgetc(f, &c) == 1); | |
674 | assert_se(c == chars[i]); | |
675 | } | |
7f9d1aed | 676 | |
cdce33f9 | 677 | /* But it works when we push it properly cast */ |
7f9d1aed ZJS |
678 | assert_se(ungetc((unsigned char) c, f) != EOF); |
679 | assert_se(safe_fgetc(f, &c) == 1); | |
680 | assert_se(c == chars[i]); | |
681 | } | |
682 | ||
683 | assert_se(safe_fgetc(f, &c) == 0); | |
684 | } | |
685 | ||
6028d766 | 686 | REENABLE_WARNING; |
d3bdba38 | 687 | |
2c9de139 ZJS |
688 | static const char buffer[] = |
689 | "Some test data\n" | |
7f9d1aed | 690 | "루Non-ascii chars: ąę„”\n" |
838894b0 LP |
691 | "terminators\r\n" |
692 | "and even more\n\r" | |
693 | "now the same with a NUL\n\0" | |
694 | "and more\r\0" | |
695 | "and even more\r\n\0" | |
696 | "and yet even more\n\r\0" | |
2c9de139 ZJS |
697 | "With newlines, and a NUL byte\0" |
698 | "\n" | |
699 | "an empty line\n" | |
700 | "an ignored line\n" | |
701 | "and a very long line that is supposed to be truncated, because it is so long\n"; | |
702 | ||
703 | static void test_read_line_one_file(FILE *f) { | |
4f9a66a3 LP |
704 | _cleanup_free_ char *line = NULL; |
705 | ||
4f9a66a3 LP |
706 | assert_se(read_line(f, (size_t) -1, &line) == 15 && streq(line, "Some test data")); |
707 | line = mfree(line); | |
708 | ||
7f9d1aed | 709 | assert_se(read_line(f, (size_t) -1, &line) > 0 && streq(line, "루Non-ascii chars: ąę„”")); |
838894b0 LP |
710 | line = mfree(line); |
711 | ||
712 | assert_se(read_line(f, (size_t) -1, &line) == 13 && streq(line, "terminators")); | |
713 | line = mfree(line); | |
714 | ||
715 | assert_se(read_line(f, (size_t) -1, &line) == 15 && streq(line, "and even more")); | |
716 | line = mfree(line); | |
717 | ||
718 | assert_se(read_line(f, (size_t) -1, &line) == 25 && streq(line, "now the same with a NUL")); | |
719 | line = mfree(line); | |
720 | ||
721 | assert_se(read_line(f, (size_t) -1, &line) == 10 && streq(line, "and more")); | |
722 | line = mfree(line); | |
723 | ||
724 | assert_se(read_line(f, (size_t) -1, &line) == 16 && streq(line, "and even more")); | |
725 | line = mfree(line); | |
726 | ||
727 | assert_se(read_line(f, (size_t) -1, &line) == 20 && streq(line, "and yet even more")); | |
728 | line = mfree(line); | |
729 | ||
4f9a66a3 LP |
730 | assert_se(read_line(f, 1024, &line) == 30 && streq(line, "With newlines, and a NUL byte")); |
731 | line = mfree(line); | |
732 | ||
733 | assert_se(read_line(f, 1024, &line) == 1 && streq(line, "")); | |
734 | line = mfree(line); | |
735 | ||
736 | assert_se(read_line(f, 1024, &line) == 14 && streq(line, "an empty line")); | |
737 | line = mfree(line); | |
738 | ||
739 | assert_se(read_line(f, (size_t) -1, NULL) == 16); | |
740 | ||
741 | assert_se(read_line(f, 16, &line) == -ENOBUFS); | |
742 | line = mfree(line); | |
743 | ||
744 | /* read_line() stopped when it hit the limit, that means when we continue reading we'll read at the first | |
5238e957 | 745 | * character after the previous limit. Let's make use of that to continue our test. */ |
838894b0 | 746 | assert_se(read_line(f, 1024, &line) == 62 && streq(line, "line that is supposed to be truncated, because it is so long")); |
4f9a66a3 LP |
747 | line = mfree(line); |
748 | ||
749 | assert_se(read_line(f, 1024, &line) == 0 && streq(line, "")); | |
750 | } | |
751 | ||
2c9de139 ZJS |
752 | static void test_read_line(void) { |
753 | _cleanup_fclose_ FILE *f = NULL; | |
2c9de139 | 754 | |
673a1e6f | 755 | f = fmemopen_unlocked((void*) buffer, sizeof(buffer), "re"); |
2c9de139 ZJS |
756 | assert_se(f); |
757 | ||
758 | test_read_line_one_file(f); | |
759 | } | |
760 | ||
761 | static void test_read_line2(void) { | |
627d2bac | 762 | _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-fileio.XXXXXX"; |
2c9de139 ZJS |
763 | int fd; |
764 | _cleanup_fclose_ FILE *f = NULL; | |
765 | ||
766 | fd = mkostemp_safe(name); | |
767 | assert_se(fd >= 0); | |
768 | assert_se((size_t) write(fd, buffer, sizeof(buffer)) == sizeof(buffer)); | |
769 | ||
770 | assert_se(lseek(fd, 0, SEEK_SET) == 0); | |
771 | assert_se(f = fdopen(fd, "r")); | |
772 | ||
773 | test_read_line_one_file(f); | |
774 | } | |
775 | ||
776 | static void test_read_line3(void) { | |
777 | _cleanup_fclose_ FILE *f = NULL; | |
778 | _cleanup_free_ char *line = NULL; | |
779 | int r; | |
780 | ||
30b84c78 | 781 | f = fopen("/proc/uptime", "re"); |
2c9de139 ZJS |
782 | if (!f && IN_SET(errno, ENOENT, EPERM)) |
783 | return; | |
784 | assert_se(f); | |
785 | ||
786 | r = read_line(f, LINE_MAX, &line); | |
b4555637 ZJS |
787 | assert_se(r >= 0); |
788 | if (r == 0) | |
789 | assert_se(line && isempty(line)); | |
790 | else | |
791 | assert_se((size_t) r == strlen(line) + 1); | |
2c9de139 ZJS |
792 | assert_se(read_line(f, LINE_MAX, NULL) == 0); |
793 | } | |
794 | ||
6baac700 LP |
795 | static void test_read_line4(void) { |
796 | static const struct { | |
797 | size_t length; | |
798 | const char *string; | |
799 | } eof_endings[] = { | |
800 | /* Each of these will be followed by EOF and should generate the one same single string */ | |
801 | { 3, "foo" }, | |
802 | { 4, "foo\n" }, | |
803 | { 4, "foo\r" }, | |
804 | { 4, "foo\0" }, | |
805 | { 5, "foo\n\0" }, | |
806 | { 5, "foo\r\0" }, | |
807 | { 5, "foo\r\n" }, | |
808 | { 5, "foo\n\r" }, | |
809 | { 6, "foo\r\n\0" }, | |
810 | { 6, "foo\n\r\0" }, | |
811 | }; | |
812 | ||
813 | size_t i; | |
814 | int r; | |
815 | ||
816 | for (i = 0; i < ELEMENTSOF(eof_endings); i++) { | |
817 | _cleanup_fclose_ FILE *f = NULL; | |
818 | _cleanup_free_ char *s = NULL; | |
819 | ||
673a1e6f | 820 | assert_se(f = fmemopen_unlocked((void*) eof_endings[i].string, eof_endings[i].length, "r")); |
6baac700 LP |
821 | |
822 | r = read_line(f, (size_t) -1, &s); | |
823 | assert_se((size_t) r == eof_endings[i].length); | |
824 | assert_se(streq_ptr(s, "foo")); | |
825 | ||
826 | assert_se(read_line(f, (size_t) -1, NULL) == 0); /* Ensure we hit EOF */ | |
827 | } | |
828 | } | |
829 | ||
3946d576 LP |
830 | static void test_read_nul_string(void) { |
831 | static const char test[] = "string nr. 1\0" | |
832 | "string nr. 2\n\0" | |
517b7760 | 833 | "\377empty string follows\0" |
3946d576 LP |
834 | "\0" |
835 | "final string\n is empty\0" | |
836 | "\0"; | |
837 | ||
838 | _cleanup_fclose_ FILE *f = NULL; | |
839 | _cleanup_free_ char *s = NULL; | |
840 | ||
673a1e6f | 841 | assert_se(f = fmemopen_unlocked((void*) test, sizeof(test)-1, "r")); |
3946d576 LP |
842 | |
843 | assert_se(read_nul_string(f, LONG_LINE_MAX, &s) == 13 && streq_ptr(s, "string nr. 1")); | |
844 | s = mfree(s); | |
845 | ||
846 | assert_se(read_nul_string(f, LONG_LINE_MAX, &s) == 14 && streq_ptr(s, "string nr. 2\n")); | |
847 | s = mfree(s); | |
848 | ||
517b7760 | 849 | assert_se(read_nul_string(f, LONG_LINE_MAX, &s) == 22 && streq_ptr(s, "\377empty string follows")); |
3946d576 LP |
850 | s = mfree(s); |
851 | ||
852 | assert_se(read_nul_string(f, LONG_LINE_MAX, &s) == 1 && streq_ptr(s, "")); | |
853 | s = mfree(s); | |
854 | ||
855 | assert_se(read_nul_string(f, LONG_LINE_MAX, &s) == 23 && streq_ptr(s, "final string\n is empty")); | |
856 | s = mfree(s); | |
857 | ||
858 | assert_se(read_nul_string(f, LONG_LINE_MAX, &s) == 1 && streq_ptr(s, "")); | |
859 | s = mfree(s); | |
860 | ||
861 | assert_se(read_nul_string(f, LONG_LINE_MAX, &s) == 0 && streq_ptr(s, "")); | |
862 | } | |
863 | ||
b93d3f6b LP |
864 | static void test_read_full_file_socket(void) { |
865 | _cleanup_(rm_rf_physical_and_freep) char *z = NULL; | |
866 | _cleanup_close_ int listener = -1; | |
d3dcf4e3 | 867 | _cleanup_free_ char *data = NULL, *clientname = NULL; |
b93d3f6b LP |
868 | union sockaddr_union sa; |
869 | const char *j; | |
870 | size_t size; | |
871 | pid_t pid; | |
872 | int r; | |
873 | ||
874 | log_info("/* %s */", __func__); | |
875 | ||
876 | listener = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0); | |
877 | assert_se(listener >= 0); | |
878 | ||
879 | assert_se(mkdtemp_malloc(NULL, &z) >= 0); | |
880 | j = strjoina(z, "/socket"); | |
881 | ||
882 | assert_se(sockaddr_un_set_path(&sa.un, j) >= 0); | |
883 | ||
884 | assert_se(bind(listener, &sa.sa, SOCKADDR_UN_LEN(sa.un)) >= 0); | |
885 | assert_se(listen(listener, 1) >= 0); | |
886 | ||
d3dcf4e3 LP |
887 | /* Bind the *client* socket to some randomized name, to verify that this works correctly. */ |
888 | assert_se(asprintf(&clientname, "@%" PRIx64 "/test-bindname", random_u64()) >= 0); | |
889 | ||
b93d3f6b LP |
890 | r = safe_fork("(server)", FORK_DEATHSIG|FORK_LOG, &pid); |
891 | assert_se(r >= 0); | |
892 | if (r == 0) { | |
d3dcf4e3 LP |
893 | union sockaddr_union peer = {}; |
894 | socklen_t peerlen = sizeof(peer); | |
b93d3f6b LP |
895 | _cleanup_close_ int rfd = -1; |
896 | /* child */ | |
897 | ||
898 | rfd = accept4(listener, NULL, 0, SOCK_CLOEXEC); | |
899 | assert_se(rfd >= 0); | |
900 | ||
d3dcf4e3 LP |
901 | assert_se(getpeername(rfd, &peer.sa, &peerlen) >= 0); |
902 | ||
903 | assert_se(peer.un.sun_family == AF_UNIX); | |
904 | assert_se(peerlen > offsetof(struct sockaddr_un, sun_path)); | |
905 | assert_se(peer.un.sun_path[0] == 0); | |
906 | assert_se(streq(peer.un.sun_path + 1, clientname + 1)); | |
907 | ||
b93d3f6b LP |
908 | #define TEST_STR "This is a test\nreally." |
909 | ||
910 | assert_se(write(rfd, TEST_STR, strlen(TEST_STR)) == strlen(TEST_STR)); | |
911 | _exit(EXIT_SUCCESS); | |
912 | } | |
913 | ||
d3dcf4e3 LP |
914 | assert_se(read_full_file_full(AT_FDCWD, j, 0, NULL, &data, &size) == -ENXIO); |
915 | assert_se(read_full_file_full(AT_FDCWD, j, READ_FULL_FILE_CONNECT_SOCKET, clientname, &data, &size) >= 0); | |
b93d3f6b LP |
916 | assert_se(size == strlen(TEST_STR)); |
917 | assert_se(streq(data, TEST_STR)); | |
918 | ||
919 | assert_se(wait_for_terminate_and_check("(server)", pid, WAIT_LOG) >= 0); | |
920 | #undef TEST_STR | |
921 | } | |
922 | ||
f73141d7 | 923 | int main(int argc, char *argv[]) { |
6d7c4033 | 924 | test_setup_logging(LOG_DEBUG); |
1e5413f7 | 925 | |
f73141d7 | 926 | test_parse_env_file(); |
ac4c8d6d | 927 | test_parse_multiline_env_file(); |
37f3ffca | 928 | test_merge_env_file(); |
184d1904 | 929 | test_merge_env_file_invalid(); |
68fee104 | 930 | test_executable_is_script(); |
69ab8088 | 931 | test_status_field(); |
1e5413f7 | 932 | test_capeff(); |
0709b743 RC |
933 | test_write_string_stream(); |
934 | test_write_string_file(); | |
e07995a3 | 935 | test_write_string_file_no_create(); |
eb3da901 | 936 | test_write_string_file_verify(); |
e07995a3 | 937 | test_load_env_file_pairs(); |
897891f0 RC |
938 | test_search_and_fopen(); |
939 | test_search_and_fopen_nulstr(); | |
940 | test_writing_tmpfile(); | |
941 | test_tempfn(); | |
7f9d1aed | 942 | test_fgetc(); |
4f9a66a3 | 943 | test_read_line(); |
2c9de139 ZJS |
944 | test_read_line2(); |
945 | test_read_line3(); | |
6baac700 | 946 | test_read_line4(); |
3946d576 | 947 | test_read_nul_string(); |
b93d3f6b | 948 | test_read_full_file_socket(); |
1e5413f7 | 949 | |
f73141d7 LP |
950 | return 0; |
951 | } |