]>
Commit | Line | Data |
---|---|---|
b5b46d59 LP |
1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ |
2 | ||
3 | /*** | |
4 | This file is part of systemd. | |
5 | ||
6 | Copyright 2012 Lennart Poettering | |
b9893505 | 7 | Copyright 2013 Zbigniew Jędrzejewski-Szmek |
b5b46d59 LP |
8 | |
9 | systemd is free software; you can redistribute it and/or modify it | |
10 | under the terms of the GNU Lesser General Public License as published by | |
11 | the Free Software Foundation; either version 2.1 of the License, or | |
12 | (at your option) any later version. | |
13 | ||
14 | systemd is distributed in the hope that it will be useful, but | |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
17 | Lesser General Public License for more details. | |
18 | ||
19 | You should have received a copy of the GNU Lesser General Public License | |
20 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
21 | ***/ | |
22 | ||
b5b46d59 LP |
23 | #include <stdio.h> |
24 | #include <stddef.h> | |
25 | #include <string.h> | |
b9893505 | 26 | #include <unistd.h> |
2d5bdf5b | 27 | #include <fcntl.h> |
b5b46d59 LP |
28 | |
29 | #include "install.h" | |
7742f7e9 ZJS |
30 | #include "install-printf.h" |
31 | #include "specifier.h" | |
b5b46d59 LP |
32 | #include "util.h" |
33 | #include "macro.h" | |
34 | #include "hashmap.h" | |
2c5417ad | 35 | #include "load-fragment.h" |
b9893505 | 36 | #include "strv.h" |
a5c32cff | 37 | #include "fileio.h" |
143bfdaf | 38 | #include "test-helper.h" |
958b66ea | 39 | #include "hostname-util.h" |
b5b46d59 | 40 | |
751e7576 | 41 | static int test_unit_file_get_set(void) { |
b5b46d59 LP |
42 | int r; |
43 | Hashmap *h; | |
44 | Iterator i; | |
45 | UnitFileList *p; | |
46 | ||
d5099efc | 47 | h = hashmap_new(&string_hash_ops); |
bdf7026e | 48 | assert_se(h); |
b5b46d59 LP |
49 | |
50 | r = unit_file_get_list(UNIT_FILE_SYSTEM, NULL, h); | |
552c693e CR |
51 | |
52 | if (r == -EPERM || r == -EACCES) { | |
53 | printf("Skipping test: unit_file_get_list: %s", strerror(-r)); | |
54 | return EXIT_TEST_SKIP; | |
55 | } | |
56 | ||
751e7576 CH |
57 | log_full(r == 0 ? LOG_INFO : LOG_ERR, |
58 | "unit_file_get_list: %s", strerror(-r)); | |
59 | if (r < 0) | |
60 | return EXIT_FAILURE; | |
b5b46d59 LP |
61 | |
62 | HASHMAP_FOREACH(p, h, i) | |
63 | printf("%s = %s\n", p->path, unit_file_state_to_string(p->state)); | |
64 | ||
65 | unit_file_list_free(h); | |
751e7576 CH |
66 | |
67 | return 0; | |
2c5417ad ZJS |
68 | } |
69 | ||
70 | static void check_execcommand(ExecCommand *c, | |
71 | const char* path, | |
72 | const char* argv0, | |
73 | const char* argv1, | |
503dbda6 | 74 | const char* argv2, |
2c5417ad | 75 | bool ignore) { |
4d8629de ZJS |
76 | size_t n; |
77 | ||
2c5417ad | 78 | assert_se(c); |
c8539536 ZJS |
79 | log_info("expect: \"%s\" [\"%s\" \"%s\" \"%s\"]", |
80 | path, argv0 ?: path, argv1, argv2); | |
4d8629de | 81 | n = strv_length(c->argv); |
c8539536 | 82 | log_info("actual: \"%s\" [\"%s\" \"%s\" \"%s\"]", |
4d8629de | 83 | c->path, c->argv[0], n > 0 ? c->argv[1] : NULL, n > 1 ? c->argv[2] : NULL); |
2c5417ad | 84 | assert_se(streq(c->path, path)); |
c8539536 | 85 | assert_se(streq(c->argv[0], argv0 ?: path)); |
4d8629de ZJS |
86 | if (n > 0) |
87 | assert_se(streq_ptr(c->argv[1], argv1)); | |
88 | if (n > 1) | |
89 | assert_se(streq_ptr(c->argv[2], argv2)); | |
2c5417ad ZJS |
90 | assert_se(c->ignore == ignore); |
91 | } | |
92 | ||
93 | static void test_config_parse_exec(void) { | |
c8539536 | 94 | /* int config_parse_exec( |
470dca63 | 95 | const char *unit, |
c8539536 ZJS |
96 | const char *filename, |
97 | unsigned line, | |
98 | const char *section, | |
99 | unsigned section_line, | |
100 | const char *lvalue, | |
101 | int ltype, | |
102 | const char *rvalue, | |
103 | void *data, | |
104 | void *userdata) */ | |
2c5417ad ZJS |
105 | int r; |
106 | ||
107 | ExecCommand *c = NULL, *c1; | |
c8539536 | 108 | const char *ccc; |
2c5417ad | 109 | |
c8539536 | 110 | log_info("/* basic test */"); |
71a61510 | 111 | r = config_parse_exec(NULL, "fake", 1, "section", 1, |
2c5417ad ZJS |
112 | "LValue", 0, "/RValue r1", |
113 | &c, NULL); | |
114 | assert_se(r >= 0); | |
503dbda6 | 115 | check_execcommand(c, "/RValue", "/RValue", "r1", NULL, false); |
2c5417ad | 116 | |
71a61510 | 117 | r = config_parse_exec(NULL, "fake", 2, "section", 1, |
c8539536 | 118 | "LValue", 0, "/RValue///slashes r1///", |
2c5417ad | 119 | &c, NULL); |
c8539536 ZJS |
120 | |
121 | log_info("/* test slashes */"); | |
2c5417ad ZJS |
122 | assert_se(r >= 0); |
123 | c1 = c->command_next; | |
c8539536 | 124 | check_execcommand(c1, "/RValue/slashes", "/RValue///slashes", "r1///", NULL, false); |
2c5417ad | 125 | |
c8539536 ZJS |
126 | log_info("/* trailing slash */"); |
127 | r = config_parse_exec(NULL, "fake", 4, "section", 1, | |
128 | "LValue", 0, "/RValue/ argv0 r1", | |
129 | &c, NULL); | |
130 | assert_se(r == 0); | |
131 | assert_se(c1->command_next == NULL); | |
132 | ||
133 | log_info("/* honour_argv0 */"); | |
71a61510 | 134 | r = config_parse_exec(NULL, "fake", 3, "section", 1, |
c8539536 | 135 | "LValue", 0, "@/RValue///slashes2 ///argv0 r1", |
2c5417ad ZJS |
136 | &c, NULL); |
137 | assert_se(r >= 0); | |
138 | c1 = c1->command_next; | |
c8539536 | 139 | check_execcommand(c1, "/RValue/slashes2", "///argv0", "r1", NULL, false); |
2c5417ad | 140 | |
e01ff428 ZJS |
141 | log_info("/* honour_argv0, no args */"); |
142 | r = config_parse_exec(NULL, "fake", 3, "section", 1, | |
143 | "LValue", 0, "@/RValue", | |
144 | &c, NULL); | |
145 | assert_se(r == 0); | |
146 | assert_se(c1->command_next == NULL); | |
147 | ||
148 | log_info("/* no command, check for bad memory access */"); | |
149 | r = config_parse_exec(NULL, "fake", 3, "section", 1, | |
150 | "LValue", 0, " ", | |
151 | &c, NULL); | |
152 | assert_se(r == 0); | |
153 | assert_se(c1->command_next == NULL); | |
154 | ||
c8539536 | 155 | log_info("/* ignore && honour_argv0 */"); |
71a61510 | 156 | r = config_parse_exec(NULL, "fake", 4, "section", 1, |
c8539536 | 157 | "LValue", 0, "-@/RValue///slashes3 argv0a r1", |
2c5417ad ZJS |
158 | &c, NULL); |
159 | assert_se(r >= 0); | |
160 | c1 = c1->command_next; | |
503dbda6 | 161 | check_execcommand(c1, "/RValue/slashes3", "argv0a", "r1", NULL, true); |
2c5417ad | 162 | |
c8539536 | 163 | log_info("/* ignore && honour_argv0 */"); |
71a61510 | 164 | r = config_parse_exec(NULL, "fake", 4, "section", 1, |
c8539536 | 165 | "LValue", 0, "@-/RValue///slashes4 argv0b r1", |
0f67f1ef ZJS |
166 | &c, NULL); |
167 | assert_se(r >= 0); | |
168 | c1 = c1->command_next; | |
503dbda6 | 169 | check_execcommand(c1, "/RValue/slashes4", "argv0b", "r1", NULL, true); |
0f67f1ef | 170 | |
c8539536 | 171 | log_info("/* ignore && ignore */"); |
71a61510 | 172 | r = config_parse_exec(NULL, "fake", 4, "section", 1, |
0f67f1ef ZJS |
173 | "LValue", 0, "--/RValue argv0 r1", |
174 | &c, NULL); | |
175 | assert_se(r == 0); | |
176 | assert_se(c1->command_next == NULL); | |
177 | ||
c8539536 | 178 | log_info("/* ignore && ignore (2) */"); |
71a61510 | 179 | r = config_parse_exec(NULL, "fake", 4, "section", 1, |
0f67f1ef ZJS |
180 | "LValue", 0, "-@-/RValue argv0 r1", |
181 | &c, NULL); | |
182 | assert_se(r == 0); | |
183 | assert_se(c1->command_next == NULL); | |
184 | ||
c8539536 | 185 | log_info("/* semicolon */"); |
71a61510 | 186 | r = config_parse_exec(NULL, "fake", 5, "section", 1, |
2c5417ad ZJS |
187 | "LValue", 0, |
188 | "-@/RValue argv0 r1 ; " | |
189 | "/goo/goo boo", | |
190 | &c, NULL); | |
191 | assert_se(r >= 0); | |
192 | c1 = c1->command_next; | |
503dbda6 | 193 | check_execcommand(c1, "/RValue", "argv0", "r1", NULL, true); |
2c5417ad ZJS |
194 | |
195 | c1 = c1->command_next; | |
c8539536 | 196 | check_execcommand(c1, "/goo/goo", NULL, "boo", NULL, false); |
2c5417ad | 197 | |
c8539536 | 198 | log_info("/* trailing semicolon */"); |
71a61510 | 199 | r = config_parse_exec(NULL, "fake", 5, "section", 1, |
2c5417ad ZJS |
200 | "LValue", 0, |
201 | "-@/RValue argv0 r1 ; ", | |
202 | &c, NULL); | |
203 | assert_se(r >= 0); | |
204 | c1 = c1->command_next; | |
503dbda6 | 205 | check_execcommand(c1, "/RValue", "argv0", "r1", NULL, true); |
2c5417ad ZJS |
206 | |
207 | assert_se(c1->command_next == NULL); | |
208 | ||
c8539536 | 209 | log_info("/* escaped semicolon */"); |
71a61510 | 210 | r = config_parse_exec(NULL, "fake", 5, "section", 1, |
7e1a84f5 | 211 | "LValue", 0, |
503dbda6 ZJS |
212 | "/bin/find \\;", |
213 | &c, NULL); | |
214 | assert_se(r >= 0); | |
215 | c1 = c1->command_next; | |
c8539536 | 216 | check_execcommand(c1, "/bin/find", NULL, ";", NULL, false); |
503dbda6 | 217 | |
c8539536 | 218 | log_info("/* escaped semicolon with following arg */"); |
503dbda6 ZJS |
219 | r = config_parse_exec(NULL, "fake", 5, "section", 1, |
220 | "LValue", 0, | |
221 | "/sbin/find \\; x", | |
7e1a84f5 OS |
222 | &c, NULL); |
223 | assert_se(r >= 0); | |
224 | c1 = c1->command_next; | |
225 | check_execcommand(c1, | |
c8539536 ZJS |
226 | "/sbin/find", NULL, ";", "x", false); |
227 | ||
80979f1c DM |
228 | log_info("/* encoded semicolon */"); |
229 | r = config_parse_exec(NULL, "fake", 5, "section", 1, | |
230 | "LValue", 0, | |
231 | "/bin/find \\073", | |
232 | &c, NULL); | |
233 | assert_se(r >= 0); | |
234 | c1 = c1->command_next; | |
ce54255f | 235 | check_execcommand(c1, "/bin/find", NULL, ";", NULL, false); |
80979f1c | 236 | |
c8539536 ZJS |
237 | log_info("/* spaces in the filename */"); |
238 | r = config_parse_exec(NULL, "fake", 5, "section", 1, | |
239 | "LValue", 0, | |
240 | "\"/PATH WITH SPACES/daemon\" -1 -2", | |
241 | &c, NULL); | |
242 | assert_se(r >= 0); | |
243 | c1 = c1->command_next; | |
244 | check_execcommand(c1, | |
245 | "/PATH WITH SPACES/daemon", NULL, "-1", "-2", false); | |
246 | ||
247 | log_info("/* spaces in the filename, no args */"); | |
248 | r = config_parse_exec(NULL, "fake", 5, "section", 1, | |
249 | "LValue", 0, | |
250 | "\"/PATH WITH SPACES/daemon -1 -2\"", | |
251 | &c, NULL); | |
252 | assert_se(r >= 0); | |
253 | c1 = c1->command_next; | |
254 | check_execcommand(c1, | |
255 | "/PATH WITH SPACES/daemon -1 -2", NULL, NULL, NULL, false); | |
256 | ||
257 | log_info("/* spaces in the filename, everything quoted */"); | |
258 | r = config_parse_exec(NULL, "fake", 5, "section", 1, | |
259 | "LValue", 0, | |
260 | "\"/PATH WITH SPACES/daemon\" \"-1\" '-2'", | |
261 | &c, NULL); | |
262 | assert_se(r >= 0); | |
263 | c1 = c1->command_next; | |
264 | check_execcommand(c1, | |
265 | "/PATH WITH SPACES/daemon", NULL, "-1", "-2", false); | |
266 | ||
267 | log_info("/* escaped spaces in the filename */"); | |
268 | r = config_parse_exec(NULL, "fake", 5, "section", 1, | |
269 | "LValue", 0, | |
270 | "\"/PATH\\sWITH\\sSPACES/daemon\" '-1 -2'", | |
271 | &c, NULL); | |
272 | assert_se(r >= 0); | |
273 | c1 = c1->command_next; | |
274 | check_execcommand(c1, | |
275 | "/PATH WITH SPACES/daemon", NULL, "-1 -2", NULL, false); | |
276 | ||
277 | log_info("/* escaped spaces in the filename (2) */"); | |
278 | r = config_parse_exec(NULL, "fake", 5, "section", 1, | |
279 | "LValue", 0, | |
280 | "\"/PATH\\x20WITH\\x20SPACES/daemon\" \"-1 -2\"", | |
281 | &c, NULL); | |
282 | assert_se(r >= 0); | |
283 | c1 = c1->command_next; | |
284 | check_execcommand(c1, | |
285 | "/PATH WITH SPACES/daemon", NULL, "-1 -2", NULL, false); | |
286 | ||
287 | for (ccc = "abfnrtv\\\'\"x"; *ccc; ccc++) { | |
288 | /* \\x is an incomplete hexadecimal sequence, invalid because of the slash */ | |
289 | char path[] = "/path\\X"; | |
290 | path[sizeof(path) - 2] = *ccc; | |
291 | ||
292 | log_info("/* invalid character: \\%c */", *ccc); | |
293 | r = config_parse_exec(NULL, "fake", 4, "section", 1, | |
294 | "LValue", 0, path, | |
295 | &c, NULL); | |
296 | assert_se(r == 0); | |
297 | assert_se(c1->command_next == NULL); | |
298 | } | |
299 | ||
300 | log_info("/* valid character: \\s */"); | |
301 | r = config_parse_exec(NULL, "fake", 4, "section", 1, | |
302 | "LValue", 0, "/path\\s", | |
303 | &c, NULL); | |
304 | assert_se(r >= 0); | |
305 | c1 = c1->command_next; | |
306 | check_execcommand(c1, "/path ", NULL, NULL, NULL, false); | |
307 | ||
80979f1c DM |
308 | log_info("/* quoted backslashes */"); |
309 | r = config_parse_exec(NULL, "fake", 5, "section", 1, | |
310 | "LValue", 0, | |
311 | "/bin/grep '\\w+\\K'", | |
312 | &c, NULL); | |
313 | assert_se(r >= 0); | |
314 | c1 = c1->command_next; | |
315 | check_execcommand(c1, "/bin/grep", NULL, "\\w+\\K", NULL, false); | |
316 | ||
317 | ||
c8539536 ZJS |
318 | log_info("/* trailing backslash: \\ */"); |
319 | /* backslash is invalid */ | |
320 | r = config_parse_exec(NULL, "fake", 4, "section", 1, | |
321 | "LValue", 0, "/path\\", | |
322 | &c, NULL); | |
323 | assert_se(r == 0); | |
324 | assert_se(c1->command_next == NULL); | |
7e1a84f5 | 325 | |
470dca63 MP |
326 | log_info("/* missing ending ' */"); |
327 | r = config_parse_exec(NULL, "fake", 4, "section", 1, | |
328 | "LValue", 0, "/path 'foo", | |
329 | &c, NULL); | |
330 | assert_se(r == 0); | |
331 | assert_se(c1->command_next == NULL); | |
332 | ||
333 | log_info("/* missing ending ' with trailing backslash */"); | |
334 | r = config_parse_exec(NULL, "fake", 4, "section", 1, | |
335 | "LValue", 0, "/path 'foo\\", | |
336 | &c, NULL); | |
337 | assert_se(r == 0); | |
338 | assert_se(c1->command_next == NULL); | |
339 | ||
35b1078e MP |
340 | log_info("/* invalid space between modifiers */"); |
341 | r = config_parse_exec(NULL, "fake", 4, "section", 1, | |
342 | "LValue", 0, "- /path", | |
343 | &c, NULL); | |
344 | assert_se(r == 0); | |
345 | assert_se(c1->command_next == NULL); | |
346 | ||
347 | log_info("/* only modifiers, no path */"); | |
348 | r = config_parse_exec(NULL, "fake", 4, "section", 1, | |
349 | "LValue", 0, "-", | |
350 | &c, NULL); | |
351 | assert_se(r == 0); | |
352 | assert_se(c1->command_next == NULL); | |
353 | ||
354 | log_info("/* empty argument, reset */"); | |
355 | r = config_parse_exec(NULL, "fake", 4, "section", 1, | |
356 | "LValue", 0, "", | |
357 | &c, NULL); | |
358 | assert_se(r == 0); | |
359 | assert_se(c == NULL); | |
360 | ||
2c5417ad ZJS |
361 | exec_command_free_list(c); |
362 | } | |
363 | ||
f73141d7 LP |
364 | #define env_file_1 \ |
365 | "a=a\n" \ | |
366 | "b=b\\\n" \ | |
367 | "c\n" \ | |
368 | "d=d\\\n" \ | |
369 | "e\\\n" \ | |
370 | "f\n" \ | |
371 | "g=g\\ \n" \ | |
372 | "h=h\n" \ | |
373 | "i=i\\" | |
374 | ||
375 | #define env_file_2 \ | |
376 | "a=a\\\n" | |
b9893505 | 377 | |
a3aa7ee6 ZJS |
378 | #define env_file_3 \ |
379 | "#SPAMD_ARGS=\"-d --socketpath=/var/lib/bulwark/spamd \\\n" \ | |
380 | "#--nouser-config \\\n" \ | |
381 | "normal=line" | |
382 | ||
d3b6d0c2 ZJS |
383 | #define env_file_4 \ |
384 | "# Generated\n" \ | |
385 | "\n" \ | |
386 | "HWMON_MODULES=\"coretemp f71882fg\"\n" \ | |
387 | "\n" \ | |
388 | "# For compatibility reasons\n" \ | |
389 | "\n" \ | |
390 | "MODULE_0=coretemp\n" \ | |
391 | "MODULE_1=f71882fg" | |
392 | ||
58f10d40 ILG |
393 | #define env_file_5 \ |
394 | "a=\n" \ | |
395 | "b=" | |
d3b6d0c2 | 396 | |
b9893505 | 397 | static void test_load_env_file_1(void) { |
7fd1b19b | 398 | _cleanup_strv_free_ char **data = NULL; |
b9893505 ZJS |
399 | int r; |
400 | ||
401 | char name[] = "/tmp/test-load-env-file.XXXXXX"; | |
2d5bdf5b LP |
402 | _cleanup_close_ int fd; |
403 | ||
404 | fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC); | |
bdf7026e | 405 | assert_se(fd >= 0); |
b9893505 ZJS |
406 | assert_se(write(fd, env_file_1, sizeof(env_file_1)) == sizeof(env_file_1)); |
407 | ||
717603e3 | 408 | r = load_env_file(NULL, name, NULL, &data); |
bdf7026e TA |
409 | assert_se(r == 0); |
410 | assert_se(streq(data[0], "a=a")); | |
411 | assert_se(streq(data[1], "b=bc")); | |
412 | assert_se(streq(data[2], "d=def")); | |
413 | assert_se(streq(data[3], "g=g ")); | |
414 | assert_se(streq(data[4], "h=h")); | |
415 | assert_se(streq(data[5], "i=i")); | |
416 | assert_se(data[6] == NULL); | |
b9893505 ZJS |
417 | unlink(name); |
418 | } | |
419 | ||
420 | static void test_load_env_file_2(void) { | |
7fd1b19b | 421 | _cleanup_strv_free_ char **data = NULL; |
b9893505 ZJS |
422 | int r; |
423 | ||
424 | char name[] = "/tmp/test-load-env-file.XXXXXX"; | |
2d5bdf5b LP |
425 | _cleanup_close_ int fd; |
426 | ||
427 | fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC); | |
bdf7026e | 428 | assert_se(fd >= 0); |
b9893505 ZJS |
429 | assert_se(write(fd, env_file_2, sizeof(env_file_2)) == sizeof(env_file_2)); |
430 | ||
717603e3 | 431 | r = load_env_file(NULL, name, NULL, &data); |
bdf7026e TA |
432 | assert_se(r == 0); |
433 | assert_se(streq(data[0], "a=a")); | |
434 | assert_se(data[1] == NULL); | |
b9893505 ZJS |
435 | unlink(name); |
436 | } | |
437 | ||
a3aa7ee6 | 438 | static void test_load_env_file_3(void) { |
7fd1b19b | 439 | _cleanup_strv_free_ char **data = NULL; |
a3aa7ee6 ZJS |
440 | int r; |
441 | ||
442 | char name[] = "/tmp/test-load-env-file.XXXXXX"; | |
2d5bdf5b LP |
443 | _cleanup_close_ int fd; |
444 | ||
445 | fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC); | |
bdf7026e | 446 | assert_se(fd >= 0); |
a3aa7ee6 ZJS |
447 | assert_se(write(fd, env_file_3, sizeof(env_file_3)) == sizeof(env_file_3)); |
448 | ||
717603e3 | 449 | r = load_env_file(NULL, name, NULL, &data); |
bdf7026e TA |
450 | assert_se(r == 0); |
451 | assert_se(data == NULL); | |
a3aa7ee6 ZJS |
452 | unlink(name); |
453 | } | |
454 | ||
d3b6d0c2 | 455 | static void test_load_env_file_4(void) { |
7fd1b19b | 456 | _cleanup_strv_free_ char **data = NULL; |
2d5bdf5b LP |
457 | char name[] = "/tmp/test-load-env-file.XXXXXX"; |
458 | _cleanup_close_ int fd; | |
d3b6d0c2 ZJS |
459 | int r; |
460 | ||
2d5bdf5b | 461 | fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC); |
bdf7026e | 462 | assert_se(fd >= 0); |
d3b6d0c2 ZJS |
463 | assert_se(write(fd, env_file_4, sizeof(env_file_4)) == sizeof(env_file_4)); |
464 | ||
717603e3 | 465 | r = load_env_file(NULL, name, NULL, &data); |
bdf7026e TA |
466 | assert_se(r == 0); |
467 | assert_se(streq(data[0], "HWMON_MODULES=coretemp f71882fg")); | |
468 | assert_se(streq(data[1], "MODULE_0=coretemp")); | |
469 | assert_se(streq(data[2], "MODULE_1=f71882fg")); | |
470 | assert_se(data[3] == NULL); | |
d3b6d0c2 ZJS |
471 | unlink(name); |
472 | } | |
473 | ||
58f10d40 ILG |
474 | static void test_load_env_file_5(void) { |
475 | _cleanup_strv_free_ char **data = NULL; | |
476 | int r; | |
477 | ||
478 | char name[] = "/tmp/test-load-env-file.XXXXXX"; | |
479 | _cleanup_close_ int fd; | |
480 | ||
481 | fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC); | |
482 | assert_se(fd >= 0); | |
483 | assert_se(write(fd, env_file_5, sizeof(env_file_5)) == sizeof(env_file_5)); | |
484 | ||
485 | r = load_env_file(NULL, name, NULL, &data); | |
486 | assert_se(r == 0); | |
487 | assert_se(streq(data[0], "a=")); | |
488 | assert_se(streq(data[1], "b=")); | |
489 | assert_se(data[2] == NULL); | |
490 | unlink(name); | |
491 | } | |
d3b6d0c2 | 492 | |
7742f7e9 ZJS |
493 | static void test_install_printf(void) { |
494 | char name[] = "name.service", | |
ad88e758 | 495 | path[] = "/run/systemd/system/name.service", |
7742f7e9 | 496 | user[] = "xxxx-no-such-user"; |
cab6235f LP |
497 | UnitFileInstallInfo i = {name, path, user}; |
498 | UnitFileInstallInfo i2 = {name, path, NULL}; | |
7742f7e9 | 499 | char name3[] = "name@inst.service", |
ad88e758 | 500 | path3[] = "/run/systemd/system/name.service"; |
cab6235f LP |
501 | UnitFileInstallInfo i3 = {name3, path3, user}; |
502 | UnitFileInstallInfo i4 = {name3, path3, NULL}; | |
7742f7e9 | 503 | |
7fd1b19b | 504 | _cleanup_free_ char *mid, *bid, *host; |
7742f7e9 | 505 | |
19f6d710 LP |
506 | assert_se(specifier_machine_id('m', NULL, NULL, &mid) >= 0 && mid); |
507 | assert_se(specifier_boot_id('b', NULL, NULL, &bid) >= 0 && bid); | |
7742f7e9 ZJS |
508 | assert_se((host = gethostname_malloc())); |
509 | ||
510 | #define expect(src, pattern, result) \ | |
f73141d7 | 511 | do { \ |
19f6d710 | 512 | _cleanup_free_ char *t = NULL; \ |
7fd1b19b | 513 | _cleanup_free_ char \ |
7742f7e9 ZJS |
514 | *d1 = strdup(i.name), \ |
515 | *d2 = strdup(i.path), \ | |
516 | *d3 = strdup(i.user); \ | |
19f6d710 | 517 | assert_se(install_full_printf(&src, pattern, &t) >= 0 || !result); \ |
7742f7e9 ZJS |
518 | memzero(i.name, strlen(i.name)); \ |
519 | memzero(i.path, strlen(i.path)); \ | |
520 | memzero(i.user, strlen(i.user)); \ | |
bdf7026e | 521 | assert_se(d1 && d2 && d3); \ |
7742f7e9 ZJS |
522 | if (result) { \ |
523 | printf("%s\n", t); \ | |
bdf7026e TA |
524 | assert_se(streq(t, result)); \ |
525 | } else assert_se(t == NULL); \ | |
7742f7e9 ZJS |
526 | strcpy(i.name, d1); \ |
527 | strcpy(i.path, d2); \ | |
528 | strcpy(i.user, d3); \ | |
f73141d7 | 529 | } while(false) |
7742f7e9 ZJS |
530 | |
531 | assert_se(setenv("USER", "root", 1) == 0); | |
532 | ||
533 | expect(i, "%n", "name.service"); | |
534 | expect(i, "%N", "name"); | |
535 | expect(i, "%p", "name"); | |
536 | expect(i, "%i", ""); | |
537 | expect(i, "%u", "xxxx-no-such-user"); | |
8fca4e30 LP |
538 | |
539 | DISABLE_WARNING_NONNULL; | |
7742f7e9 | 540 | expect(i, "%U", NULL); |
8fca4e30 LP |
541 | REENABLE_WARNING; |
542 | ||
7742f7e9 ZJS |
543 | expect(i, "%m", mid); |
544 | expect(i, "%b", bid); | |
545 | expect(i, "%H", host); | |
546 | ||
547 | expect(i2, "%u", "root"); | |
548 | expect(i2, "%U", "0"); | |
549 | ||
550 | expect(i3, "%n", "name@inst.service"); | |
551 | expect(i3, "%N", "name@inst"); | |
552 | expect(i3, "%p", "name"); | |
553 | expect(i3, "%u", "xxxx-no-such-user"); | |
8fca4e30 LP |
554 | |
555 | DISABLE_WARNING_NONNULL; | |
7742f7e9 | 556 | expect(i3, "%U", NULL); |
8fca4e30 LP |
557 | REENABLE_WARNING; |
558 | ||
7742f7e9 ZJS |
559 | expect(i3, "%m", mid); |
560 | expect(i3, "%b", bid); | |
561 | expect(i3, "%H", host); | |
562 | ||
563 | expect(i4, "%u", "root"); | |
564 | expect(i4, "%U", "0"); | |
565 | } | |
b9893505 | 566 | |
2c5417ad | 567 | int main(int argc, char *argv[]) { |
751e7576 | 568 | int r; |
2c5417ad | 569 | |
c1b6628d ZJS |
570 | log_parse_environment(); |
571 | log_open(); | |
572 | ||
751e7576 | 573 | r = test_unit_file_get_set(); |
2c5417ad | 574 | test_config_parse_exec(); |
b9893505 ZJS |
575 | test_load_env_file_1(); |
576 | test_load_env_file_2(); | |
a3aa7ee6 | 577 | test_load_env_file_3(); |
d3b6d0c2 | 578 | test_load_env_file_4(); |
58f10d40 | 579 | test_load_env_file_5(); |
143bfdaf | 580 | TEST_REQ_RUNNING_SYSTEMD(test_install_printf()); |
b5b46d59 | 581 | |
751e7576 | 582 | return r; |
b5b46d59 | 583 | } |