]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-unit-file.c
Merge pull request #11827 from keszybz/pkgconfig-variables
[thirdparty/systemd.git] / src / test / test-unit-file.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <fcntl.h>
4 #include <stddef.h>
5 #include <stdio.h>
6 #include <string.h>
7 #include <sys/capability.h>
8 #include <unistd.h>
9
10 #include "all-units.h"
11 #include "alloc-util.h"
12 #include "capability-util.h"
13 #include "conf-parser.h"
14 #include "env-file.h"
15 #include "fd-util.h"
16 #include "fs-util.h"
17 #include "hashmap.h"
18 #include "hostname-util.h"
19 #include "install-printf.h"
20 #include "install.h"
21 #include "load-fragment.h"
22 #include "macro.h"
23 #include "rm-rf.h"
24 #include "specifier.h"
25 #include "string-util.h"
26 #include "strv.h"
27 #include "test-helper.h"
28 #include "tests.h"
29 #include "tmpfile-util.h"
30 #include "user-util.h"
31 #include "util.h"
32
33 static int test_unit_file_get_set(void) {
34 int r;
35 Hashmap *h;
36 Iterator i;
37 UnitFileList *p;
38
39 h = hashmap_new(&string_hash_ops);
40 assert_se(h);
41
42 r = unit_file_get_list(UNIT_FILE_SYSTEM, NULL, h, NULL, NULL);
43 if (IN_SET(r, -EPERM, -EACCES))
44 return log_tests_skipped_errno(r, "unit_file_get_list");
45
46 log_full_errno(r == 0 ? LOG_INFO : LOG_ERR, r,
47 "unit_file_get_list: %m");
48 if (r < 0)
49 return EXIT_FAILURE;
50
51 HASHMAP_FOREACH(p, h, i)
52 printf("%s = %s\n", p->path, unit_file_state_to_string(p->state));
53
54 unit_file_list_free(h);
55
56 return 0;
57 }
58
59 static void check_execcommand(ExecCommand *c,
60 const char* path,
61 const char* argv0,
62 const char* argv1,
63 const char* argv2,
64 bool ignore) {
65 size_t n;
66
67 assert_se(c);
68 log_info("expect: \"%s\" [\"%s\" \"%s\" \"%s\"]",
69 path, argv0 ?: path, argv1, argv2);
70 n = strv_length(c->argv);
71 log_info("actual: \"%s\" [\"%s\" \"%s\" \"%s\"]",
72 c->path, c->argv[0], n > 0 ? c->argv[1] : NULL, n > 1 ? c->argv[2] : NULL);
73 assert_se(streq(c->path, path));
74 assert_se(streq(c->argv[0], argv0 ?: path));
75 if (n > 0)
76 assert_se(streq_ptr(c->argv[1], argv1));
77 if (n > 1)
78 assert_se(streq_ptr(c->argv[2], argv2));
79 assert_se(!!(c->flags & EXEC_COMMAND_IGNORE_FAILURE) == ignore);
80 }
81
82 static void test_config_parse_exec(void) {
83 /* int config_parse_exec(
84 const char *unit,
85 const char *filename,
86 unsigned line,
87 const char *section,
88 unsigned section_line,
89 const char *lvalue,
90 int ltype,
91 const char *rvalue,
92 void *data,
93 void *userdata) */
94 int r;
95
96 ExecCommand *c = NULL, *c1;
97 const char *ccc;
98 _cleanup_(manager_freep) Manager *m = NULL;
99 _cleanup_(unit_freep) Unit *u = NULL;
100
101 r = manager_new(UNIT_FILE_USER, MANAGER_TEST_RUN_MINIMAL, &m);
102 if (MANAGER_SKIP_TEST(r)) {
103 log_notice_errno(r, "Skipping test: manager_new: %m");
104 return;
105 }
106
107 assert_se(r >= 0);
108 assert_se(manager_startup(m, NULL, NULL) >= 0);
109
110 assert_se(u = unit_new(m, sizeof(Service)));
111
112 log_info("/* basic test */");
113 r = config_parse_exec(NULL, "fake", 1, "section", 1,
114 "LValue", 0, "/RValue r1",
115 &c, u);
116 assert_se(r >= 0);
117 check_execcommand(c, "/RValue", "/RValue", "r1", NULL, false);
118
119 r = config_parse_exec(NULL, "fake", 2, "section", 1,
120 "LValue", 0, "/RValue///slashes r1///",
121 &c, u);
122
123 log_info("/* test slashes */");
124 assert_se(r >= 0);
125 c1 = c->command_next;
126 check_execcommand(c1, "/RValue/slashes", "/RValue///slashes", "r1///", NULL, false);
127
128 log_info("/* trailing slash */");
129 r = config_parse_exec(NULL, "fake", 4, "section", 1,
130 "LValue", 0, "/RValue/ argv0 r1",
131 &c, u);
132 assert_se(r == -ENOEXEC);
133 assert_se(c1->command_next == NULL);
134
135 log_info("/* honour_argv0 */");
136 r = config_parse_exec(NULL, "fake", 3, "section", 1,
137 "LValue", 0, "@/RValue///slashes2 ///argv0 r1",
138 &c, u);
139 assert_se(r >= 0);
140 c1 = c1->command_next;
141 check_execcommand(c1, "/RValue/slashes2", "///argv0", "r1", NULL, false);
142
143 log_info("/* honour_argv0, no args */");
144 r = config_parse_exec(NULL, "fake", 3, "section", 1,
145 "LValue", 0, "@/RValue",
146 &c, u);
147 assert_se(r == -ENOEXEC);
148 assert_se(c1->command_next == NULL);
149
150 log_info("/* no command, whitespace only, reset */");
151 r = config_parse_exec(NULL, "fake", 3, "section", 1,
152 "LValue", 0, " ",
153 &c, u);
154 assert_se(r == 0);
155 assert_se(c == NULL);
156
157 log_info("/* ignore && honour_argv0 */");
158 r = config_parse_exec(NULL, "fake", 4, "section", 1,
159 "LValue", 0, "-@/RValue///slashes3 argv0a r1",
160 &c, u);
161 assert_se(r >= 0);
162 c1 = c;
163 check_execcommand(c1, "/RValue/slashes3", "argv0a", "r1", NULL, true);
164
165 log_info("/* ignore && honour_argv0 */");
166 r = config_parse_exec(NULL, "fake", 4, "section", 1,
167 "LValue", 0, "@-/RValue///slashes4 argv0b r1",
168 &c, u);
169 assert_se(r >= 0);
170 c1 = c1->command_next;
171 check_execcommand(c1, "/RValue/slashes4", "argv0b", "r1", NULL, true);
172
173 log_info("/* ignore && ignore */");
174 r = config_parse_exec(NULL, "fake", 4, "section", 1,
175 "LValue", 0, "--/RValue argv0 r1",
176 &c, u);
177 assert_se(r == 0);
178 assert_se(c1->command_next == NULL);
179
180 log_info("/* ignore && ignore (2) */");
181 r = config_parse_exec(NULL, "fake", 4, "section", 1,
182 "LValue", 0, "-@-/RValue argv0 r1",
183 &c, u);
184 assert_se(r == 0);
185 assert_se(c1->command_next == NULL);
186
187 log_info("/* semicolon */");
188 r = config_parse_exec(NULL, "fake", 5, "section", 1,
189 "LValue", 0,
190 "-@/RValue argv0 r1 ; "
191 "/goo/goo boo",
192 &c, u);
193 assert_se(r >= 0);
194 c1 = c1->command_next;
195 check_execcommand(c1, "/RValue", "argv0", "r1", NULL, true);
196
197 c1 = c1->command_next;
198 check_execcommand(c1, "/goo/goo", NULL, "boo", NULL, false);
199
200 log_info("/* two semicolons in a row */");
201 r = config_parse_exec(NULL, "fake", 5, "section", 1,
202 "LValue", 0,
203 "-@/RValue argv0 r1 ; ; "
204 "/goo/goo boo",
205 &c, u);
206 assert_se(r == -ENOEXEC);
207 c1 = c1->command_next;
208 check_execcommand(c1, "/RValue", "argv0", "r1", NULL, true);
209
210 /* second command fails because the executable name is ";" */
211 assert_se(c1->command_next == NULL);
212
213 log_info("/* trailing semicolon */");
214 r = config_parse_exec(NULL, "fake", 5, "section", 1,
215 "LValue", 0,
216 "-@/RValue argv0 r1 ; ",
217 &c, u);
218 assert_se(r >= 0);
219 c1 = c1->command_next;
220 check_execcommand(c1, "/RValue", "argv0", "r1", NULL, true);
221
222 assert_se(c1->command_next == NULL);
223
224 log_info("/* trailing semicolon, no whitespace */");
225 r = config_parse_exec(NULL, "fake", 5, "section", 1,
226 "LValue", 0,
227 "-@/RValue argv0 r1 ;",
228 &c, u);
229 assert_se(r >= 0);
230 c1 = c1->command_next;
231 check_execcommand(c1, "/RValue", "argv0", "r1", NULL, true);
232
233 assert_se(c1->command_next == NULL);
234
235 log_info("/* trailing semicolon in single quotes */");
236 r = config_parse_exec(NULL, "fake", 5, "section", 1,
237 "LValue", 0,
238 "-@/RValue argv0 r1 ';'",
239 &c, u);
240 assert_se(r >= 0);
241 c1 = c1->command_next;
242 check_execcommand(c1, "/RValue", "argv0", "r1", ";", true);
243
244 log_info("/* escaped semicolon */");
245 r = config_parse_exec(NULL, "fake", 5, "section", 1,
246 "LValue", 0,
247 "/bin/find \\;",
248 &c, u);
249 assert_se(r >= 0);
250 c1 = c1->command_next;
251 check_execcommand(c1, "/bin/find", NULL, ";", NULL, false);
252
253 log_info("/* escaped semicolon with following arg */");
254 r = config_parse_exec(NULL, "fake", 5, "section", 1,
255 "LValue", 0,
256 "/sbin/find \\; /x",
257 &c, u);
258 assert_se(r >= 0);
259 c1 = c1->command_next;
260 check_execcommand(c1,
261 "/sbin/find", NULL, ";", "/x", false);
262
263 log_info("/* escaped semicolon as part of an expression */");
264 r = config_parse_exec(NULL, "fake", 5, "section", 1,
265 "LValue", 0,
266 "/sbin/find \\;x",
267 &c, u);
268 assert_se(r >= 0);
269 c1 = c1->command_next;
270 check_execcommand(c1,
271 "/sbin/find", NULL, "\\;x", NULL, false);
272
273 log_info("/* encoded semicolon */");
274 r = config_parse_exec(NULL, "fake", 5, "section", 1,
275 "LValue", 0,
276 "/bin/find \\073",
277 &c, u);
278 assert_se(r >= 0);
279 c1 = c1->command_next;
280 check_execcommand(c1, "/bin/find", NULL, ";", NULL, false);
281
282 log_info("/* quoted semicolon */");
283 r = config_parse_exec(NULL, "fake", 5, "section", 1,
284 "LValue", 0,
285 "/bin/find \";\"",
286 &c, u);
287 assert_se(r >= 0);
288 c1 = c1->command_next;
289 check_execcommand(c1, "/bin/find", NULL, ";", NULL, false);
290
291 log_info("/* quoted semicolon with following arg */");
292 r = config_parse_exec(NULL, "fake", 5, "section", 1,
293 "LValue", 0,
294 "/sbin/find \";\" /x",
295 &c, u);
296 assert_se(r >= 0);
297 c1 = c1->command_next;
298 check_execcommand(c1,
299 "/sbin/find", NULL, ";", "/x", false);
300
301 log_info("/* spaces in the filename */");
302 r = config_parse_exec(NULL, "fake", 5, "section", 1,
303 "LValue", 0,
304 "\"/PATH WITH SPACES/daemon\" -1 -2",
305 &c, u);
306 assert_se(r >= 0);
307 c1 = c1->command_next;
308 check_execcommand(c1,
309 "/PATH WITH SPACES/daemon", NULL, "-1", "-2", false);
310
311 log_info("/* spaces in the filename, no args */");
312 r = config_parse_exec(NULL, "fake", 5, "section", 1,
313 "LValue", 0,
314 "\"/PATH WITH SPACES/daemon -1 -2\"",
315 &c, u);
316 assert_se(r >= 0);
317 c1 = c1->command_next;
318 check_execcommand(c1,
319 "/PATH WITH SPACES/daemon -1 -2", NULL, NULL, NULL, false);
320
321 log_info("/* spaces in the filename, everything quoted */");
322 r = config_parse_exec(NULL, "fake", 5, "section", 1,
323 "LValue", 0,
324 "\"/PATH WITH SPACES/daemon\" \"-1\" '-2'",
325 &c, u);
326 assert_se(r >= 0);
327 c1 = c1->command_next;
328 check_execcommand(c1,
329 "/PATH WITH SPACES/daemon", NULL, "-1", "-2", false);
330
331 log_info("/* escaped spaces in the filename */");
332 r = config_parse_exec(NULL, "fake", 5, "section", 1,
333 "LValue", 0,
334 "\"/PATH\\sWITH\\sSPACES/daemon\" '-1 -2'",
335 &c, u);
336 assert_se(r >= 0);
337 c1 = c1->command_next;
338 check_execcommand(c1,
339 "/PATH WITH SPACES/daemon", NULL, "-1 -2", NULL, false);
340
341 log_info("/* escaped spaces in the filename (2) */");
342 r = config_parse_exec(NULL, "fake", 5, "section", 1,
343 "LValue", 0,
344 "\"/PATH\\x20WITH\\x20SPACES/daemon\" \"-1 -2\"",
345 &c, u);
346 assert_se(r >= 0);
347 c1 = c1->command_next;
348 check_execcommand(c1,
349 "/PATH WITH SPACES/daemon", NULL, "-1 -2", NULL, false);
350
351 for (ccc = "abfnrtv\\\'\"x"; *ccc; ccc++) {
352 /* \\x is an incomplete hexadecimal sequence, invalid because of the slash */
353 char path[] = "/path\\X";
354 path[sizeof(path) - 2] = *ccc;
355
356 log_info("/* invalid character: \\%c */", *ccc);
357 r = config_parse_exec(NULL, "fake", 4, "section", 1,
358 "LValue", 0, path,
359 &c, u);
360 assert_se(r == -ENOEXEC);
361 assert_se(c1->command_next == NULL);
362 }
363
364 log_info("/* valid character: \\s */");
365 r = config_parse_exec(NULL, "fake", 4, "section", 1,
366 "LValue", 0, "/path\\s",
367 &c, u);
368 assert_se(r >= 0);
369 c1 = c1->command_next;
370 check_execcommand(c1, "/path ", NULL, NULL, NULL, false);
371
372 log_info("/* quoted backslashes */");
373 r = config_parse_exec(NULL, "fake", 5, "section", 1,
374 "LValue", 0,
375 "/bin/grep '\\w+\\K'",
376 &c, u);
377 assert_se(r >= 0);
378 c1 = c1->command_next;
379 check_execcommand(c1, "/bin/grep", NULL, "\\w+\\K", NULL, false);
380
381 log_info("/* trailing backslash: \\ */");
382 /* backslash is invalid */
383 r = config_parse_exec(NULL, "fake", 4, "section", 1,
384 "LValue", 0, "/path\\",
385 &c, u);
386 assert_se(r == -ENOEXEC);
387 assert_se(c1->command_next == NULL);
388
389 log_info("/* missing ending ' */");
390 r = config_parse_exec(NULL, "fake", 4, "section", 1,
391 "LValue", 0, "/path 'foo",
392 &c, u);
393 assert_se(r == -ENOEXEC);
394 assert_se(c1->command_next == NULL);
395
396 log_info("/* missing ending ' with trailing backslash */");
397 r = config_parse_exec(NULL, "fake", 4, "section", 1,
398 "LValue", 0, "/path 'foo\\",
399 &c, u);
400 assert_se(r == -ENOEXEC);
401 assert_se(c1->command_next == NULL);
402
403 log_info("/* invalid space between modifiers */");
404 r = config_parse_exec(NULL, "fake", 4, "section", 1,
405 "LValue", 0, "- /path",
406 &c, u);
407 assert_se(r == 0);
408 assert_se(c1->command_next == NULL);
409
410 log_info("/* only modifiers, no path */");
411 r = config_parse_exec(NULL, "fake", 4, "section", 1,
412 "LValue", 0, "-",
413 &c, u);
414 assert_se(r == 0);
415 assert_se(c1->command_next == NULL);
416
417 log_info("/* empty argument, reset */");
418 r = config_parse_exec(NULL, "fake", 4, "section", 1,
419 "LValue", 0, "",
420 &c, u);
421 assert_se(r == 0);
422 assert_se(c == NULL);
423
424 exec_command_free_list(c);
425 }
426
427 static void test_config_parse_log_extra_fields(void) {
428 /* int config_parse_log_extra_fields(
429 const char *unit,
430 const char *filename,
431 unsigned line,
432 const char *section,
433 unsigned section_line,
434 const char *lvalue,
435 int ltype,
436 const char *rvalue,
437 void *data,
438 void *userdata) */
439
440 int r;
441
442 _cleanup_(manager_freep) Manager *m = NULL;
443 _cleanup_(unit_freep) Unit *u = NULL;
444 ExecContext c = {};
445
446 r = manager_new(UNIT_FILE_USER, MANAGER_TEST_RUN_MINIMAL, &m);
447 if (MANAGER_SKIP_TEST(r)) {
448 log_notice_errno(r, "Skipping test: manager_new: %m");
449 return;
450 }
451
452 assert_se(r >= 0);
453 assert_se(manager_startup(m, NULL, NULL) >= 0);
454
455 assert_se(u = unit_new(m, sizeof(Service)));
456
457 log_info("/* %s – basic test */", __func__);
458 r = config_parse_log_extra_fields(NULL, "fake", 1, "section", 1,
459 "LValue", 0, "FOO=BAR \"QOOF=quux ' ' \"",
460 &c, u);
461 assert_se(r >= 0);
462 assert_se(c.n_log_extra_fields == 2);
463 assert_se(strneq(c.log_extra_fields[0].iov_base, "FOO=BAR", c.log_extra_fields[0].iov_len));
464 assert_se(strneq(c.log_extra_fields[1].iov_base, "QOOF=quux ' ' ", c.log_extra_fields[1].iov_len));
465
466 log_info("/* %s – add some */", __func__);
467 r = config_parse_log_extra_fields(NULL, "fake", 1, "section", 1,
468 "LValue", 0, "FOO2=BAR2 QOOF2=quux ' '",
469 &c, u);
470 assert_se(r >= 0);
471 assert_se(c.n_log_extra_fields == 4);
472 assert_se(strneq(c.log_extra_fields[0].iov_base, "FOO=BAR", c.log_extra_fields[0].iov_len));
473 assert_se(strneq(c.log_extra_fields[1].iov_base, "QOOF=quux ' ' ", c.log_extra_fields[1].iov_len));
474 assert_se(strneq(c.log_extra_fields[2].iov_base, "FOO2=BAR2", c.log_extra_fields[2].iov_len));
475 assert_se(strneq(c.log_extra_fields[3].iov_base, "QOOF2=quux", c.log_extra_fields[3].iov_len));
476
477 exec_context_dump(&c, stdout, " --> ");
478
479 log_info("/* %s – reset */", __func__);
480 r = config_parse_log_extra_fields(NULL, "fake", 1, "section", 1,
481 "LValue", 0, "",
482 &c, u);
483 assert_se(r >= 0);
484 assert_se(c.n_log_extra_fields == 0);
485
486 exec_context_free_log_extra_fields(&c);
487
488 log_info("/* %s – bye */", __func__);
489 }
490
491 #define env_file_1 \
492 "a=a\n" \
493 "b=b\\\n" \
494 "c\n" \
495 "d=d\\\n" \
496 "e\\\n" \
497 "f\n" \
498 "g=g\\ \n" \
499 "h=h\n" \
500 "i=i\\"
501
502 #define env_file_2 \
503 "a=a\\\n"
504
505 #define env_file_3 \
506 "#SPAMD_ARGS=\"-d --socketpath=/var/lib/bulwark/spamd \\\n" \
507 "#--nouser-config \\\n" \
508 "normal=line"
509
510 #define env_file_4 \
511 "# Generated\n" \
512 "\n" \
513 "HWMON_MODULES=\"coretemp f71882fg\"\n" \
514 "\n" \
515 "# For compatibility reasons\n" \
516 "\n" \
517 "MODULE_0=coretemp\n" \
518 "MODULE_1=f71882fg"
519
520 #define env_file_5 \
521 "a=\n" \
522 "b="
523
524 static void test_load_env_file_1(void) {
525 _cleanup_strv_free_ char **data = NULL;
526 int r;
527
528 _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-load-env-file.XXXXXX";
529 _cleanup_close_ int fd;
530
531 fd = mkostemp_safe(name);
532 assert_se(fd >= 0);
533 assert_se(write(fd, env_file_1, strlen(env_file_1)) == strlen(env_file_1));
534
535 r = load_env_file(NULL, name, &data);
536 assert_se(r == 0);
537 assert_se(streq(data[0], "a=a"));
538 assert_se(streq(data[1], "b=bc"));
539 assert_se(streq(data[2], "d=def"));
540 assert_se(streq(data[3], "g=g "));
541 assert_se(streq(data[4], "h=h"));
542 assert_se(streq(data[5], "i=i"));
543 assert_se(data[6] == NULL);
544 }
545
546 static void test_load_env_file_2(void) {
547 _cleanup_strv_free_ char **data = NULL;
548 int r;
549
550 _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-load-env-file.XXXXXX";
551 _cleanup_close_ int fd;
552
553 fd = mkostemp_safe(name);
554 assert_se(fd >= 0);
555 assert_se(write(fd, env_file_2, strlen(env_file_2)) == strlen(env_file_2));
556
557 r = load_env_file(NULL, name, &data);
558 assert_se(r == 0);
559 assert_se(streq(data[0], "a=a"));
560 assert_se(data[1] == NULL);
561 }
562
563 static void test_load_env_file_3(void) {
564 _cleanup_strv_free_ char **data = NULL;
565 int r;
566
567 _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-load-env-file.XXXXXX";
568 _cleanup_close_ int fd;
569
570 fd = mkostemp_safe(name);
571 assert_se(fd >= 0);
572 assert_se(write(fd, env_file_3, strlen(env_file_3)) == strlen(env_file_3));
573
574 r = load_env_file(NULL, name, &data);
575 assert_se(r == 0);
576 assert_se(data == NULL);
577 }
578
579 static void test_load_env_file_4(void) {
580 _cleanup_strv_free_ char **data = NULL;
581 _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-load-env-file.XXXXXX";
582 _cleanup_close_ int fd;
583 int r;
584
585 fd = mkostemp_safe(name);
586 assert_se(fd >= 0);
587 assert_se(write(fd, env_file_4, strlen(env_file_4)) == strlen(env_file_4));
588
589 r = load_env_file(NULL, name, &data);
590 assert_se(r == 0);
591 assert_se(streq(data[0], "HWMON_MODULES=coretemp f71882fg"));
592 assert_se(streq(data[1], "MODULE_0=coretemp"));
593 assert_se(streq(data[2], "MODULE_1=f71882fg"));
594 assert_se(data[3] == NULL);
595 }
596
597 static void test_load_env_file_5(void) {
598 _cleanup_strv_free_ char **data = NULL;
599 int r;
600
601 _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-load-env-file.XXXXXX";
602 _cleanup_close_ int fd;
603
604 fd = mkostemp_safe(name);
605 assert_se(fd >= 0);
606 assert_se(write(fd, env_file_5, strlen(env_file_5)) == strlen(env_file_5));
607
608 r = load_env_file(NULL, name, &data);
609 assert_se(r == 0);
610 assert_se(streq(data[0], "a="));
611 assert_se(streq(data[1], "b="));
612 assert_se(data[2] == NULL);
613 }
614
615 static void test_install_printf(void) {
616 char name[] = "name.service",
617 path[] = "/run/systemd/system/name.service";
618 UnitFileInstallInfo i = { .name = name, .path = path, };
619 UnitFileInstallInfo i2 = { .name= name, .path = path, };
620 char name3[] = "name@inst.service",
621 path3[] = "/run/systemd/system/name.service";
622 UnitFileInstallInfo i3 = { .name = name3, .path = path3, };
623 UnitFileInstallInfo i4 = { .name = name3, .path = path3, };
624
625 _cleanup_free_ char *mid = NULL, *bid = NULL, *host = NULL, *gid = NULL, *group = NULL, *uid = NULL, *user = NULL;
626
627 assert_se(specifier_machine_id('m', NULL, NULL, &mid) >= 0 && mid);
628 assert_se(specifier_boot_id('b', NULL, NULL, &bid) >= 0 && bid);
629 assert_se(host = gethostname_malloc());
630 assert_se(group = gid_to_name(getgid()));
631 assert_se(asprintf(&gid, UID_FMT, getgid()) >= 0);
632 assert_se(user = uid_to_name(getuid()));
633 assert_se(asprintf(&uid, UID_FMT, getuid()) >= 0);
634
635 #define expect(src, pattern, result) \
636 do { \
637 _cleanup_free_ char *t = NULL; \
638 _cleanup_free_ char \
639 *d1 = strdup(i.name), \
640 *d2 = strdup(i.path); \
641 assert_se(install_full_printf(&src, pattern, &t) >= 0 || !result); \
642 memzero(i.name, strlen(i.name)); \
643 memzero(i.path, strlen(i.path)); \
644 assert_se(d1 && d2); \
645 if (result) { \
646 printf("%s\n", t); \
647 assert_se(streq(t, result)); \
648 } else assert_se(t == NULL); \
649 strcpy(i.name, d1); \
650 strcpy(i.path, d2); \
651 } while (false)
652
653 expect(i, "%n", "name.service");
654 expect(i, "%N", "name");
655 expect(i, "%p", "name");
656 expect(i, "%i", "");
657 expect(i, "%j", "name");
658 expect(i, "%g", group);
659 expect(i, "%G", gid);
660 expect(i, "%u", user);
661 expect(i, "%U", uid);
662
663 expect(i, "%m", mid);
664 expect(i, "%b", bid);
665 expect(i, "%H", host);
666
667 expect(i2, "%g", group);
668 expect(i2, "%G", gid);
669 expect(i2, "%u", user);
670 expect(i2, "%U", uid);
671
672 expect(i3, "%n", "name@inst.service");
673 expect(i3, "%N", "name@inst");
674 expect(i3, "%p", "name");
675 expect(i3, "%g", group);
676 expect(i3, "%G", gid);
677 expect(i3, "%u", user);
678 expect(i3, "%U", uid);
679
680 expect(i3, "%m", mid);
681 expect(i3, "%b", bid);
682 expect(i3, "%H", host);
683
684 expect(i4, "%g", group);
685 expect(i4, "%G", gid);
686 expect(i4, "%u", user);
687 expect(i4, "%U", uid);
688 }
689
690 static uint64_t make_cap(int cap) {
691 return ((uint64_t) 1ULL << (uint64_t) cap);
692 }
693
694 static void test_config_parse_capability_set(void) {
695 /* int config_parse_capability_set(
696 const char *unit,
697 const char *filename,
698 unsigned line,
699 const char *section,
700 unsigned section_line,
701 const char *lvalue,
702 int ltype,
703 const char *rvalue,
704 void *data,
705 void *userdata) */
706 int r;
707 uint64_t capability_bounding_set = 0;
708
709 r = config_parse_capability_set(NULL, "fake", 1, "section", 1,
710 "CapabilityBoundingSet", 0, "CAP_NET_RAW",
711 &capability_bounding_set, NULL);
712 assert_se(r >= 0);
713 assert_se(capability_bounding_set == make_cap(CAP_NET_RAW));
714
715 r = config_parse_capability_set(NULL, "fake", 1, "section", 1,
716 "CapabilityBoundingSet", 0, "CAP_NET_ADMIN",
717 &capability_bounding_set, NULL);
718 assert_se(r >= 0);
719 assert_se(capability_bounding_set == (make_cap(CAP_NET_RAW) | make_cap(CAP_NET_ADMIN)));
720
721 r = config_parse_capability_set(NULL, "fake", 1, "section", 1,
722 "CapabilityBoundingSet", 0, "~CAP_NET_ADMIN",
723 &capability_bounding_set, NULL);
724 assert_se(r >= 0);
725 assert_se(capability_bounding_set == make_cap(CAP_NET_RAW));
726
727 r = config_parse_capability_set(NULL, "fake", 1, "section", 1,
728 "CapabilityBoundingSet", 0, "",
729 &capability_bounding_set, NULL);
730 assert_se(r >= 0);
731 assert_se(capability_bounding_set == UINT64_C(0));
732
733 r = config_parse_capability_set(NULL, "fake", 1, "section", 1,
734 "CapabilityBoundingSet", 0, "~",
735 &capability_bounding_set, NULL);
736 assert_se(r >= 0);
737 assert_se(cap_test_all(capability_bounding_set));
738
739 capability_bounding_set = 0;
740 r = config_parse_capability_set(NULL, "fake", 1, "section", 1,
741 "CapabilityBoundingSet", 0, " 'CAP_NET_RAW' WAT_CAP??? CAP_NET_ADMIN CAP'_trailing_garbage",
742 &capability_bounding_set, NULL);
743 assert_se(r >= 0);
744 assert_se(capability_bounding_set == (make_cap(CAP_NET_RAW) | make_cap(CAP_NET_ADMIN)));
745 }
746
747 static void test_config_parse_rlimit(void) {
748 struct rlimit * rl[_RLIMIT_MAX] = {};
749
750 assert_se(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitNOFILE", RLIMIT_NOFILE, "55", rl, NULL) >= 0);
751 assert_se(rl[RLIMIT_NOFILE]);
752 assert_se(rl[RLIMIT_NOFILE]->rlim_cur == 55);
753 assert_se(rl[RLIMIT_NOFILE]->rlim_cur == rl[RLIMIT_NOFILE]->rlim_max);
754
755 assert_se(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitNOFILE", RLIMIT_NOFILE, "55:66", rl, NULL) >= 0);
756 assert_se(rl[RLIMIT_NOFILE]);
757 assert_se(rl[RLIMIT_NOFILE]->rlim_cur == 55);
758 assert_se(rl[RLIMIT_NOFILE]->rlim_max == 66);
759
760 assert_se(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitNOFILE", RLIMIT_NOFILE, "infinity", rl, NULL) >= 0);
761 assert_se(rl[RLIMIT_NOFILE]);
762 assert_se(rl[RLIMIT_NOFILE]->rlim_cur == RLIM_INFINITY);
763 assert_se(rl[RLIMIT_NOFILE]->rlim_cur == rl[RLIMIT_NOFILE]->rlim_max);
764
765 assert_se(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitNOFILE", RLIMIT_NOFILE, "infinity:infinity", rl, NULL) >= 0);
766 assert_se(rl[RLIMIT_NOFILE]);
767 assert_se(rl[RLIMIT_NOFILE]->rlim_cur == RLIM_INFINITY);
768 assert_se(rl[RLIMIT_NOFILE]->rlim_cur == rl[RLIMIT_NOFILE]->rlim_max);
769
770 rl[RLIMIT_NOFILE]->rlim_cur = 10;
771 rl[RLIMIT_NOFILE]->rlim_max = 20;
772
773 /* Invalid values don't change rl */
774 assert_se(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitNOFILE", RLIMIT_NOFILE, "10:20:30", rl, NULL) >= 0);
775 assert_se(rl[RLIMIT_NOFILE]);
776 assert_se(rl[RLIMIT_NOFILE]->rlim_cur == 10);
777 assert_se(rl[RLIMIT_NOFILE]->rlim_max == 20);
778
779 assert_se(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitNOFILE", RLIMIT_NOFILE, "wat:wat", rl, NULL) >= 0);
780 assert_se(rl[RLIMIT_NOFILE]);
781 assert_se(rl[RLIMIT_NOFILE]->rlim_cur == 10);
782 assert_se(rl[RLIMIT_NOFILE]->rlim_max == 20);
783
784 assert_se(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitNOFILE", RLIMIT_NOFILE, "66:wat", rl, NULL) >= 0);
785 assert_se(rl[RLIMIT_NOFILE]);
786 assert_se(rl[RLIMIT_NOFILE]->rlim_cur == 10);
787 assert_se(rl[RLIMIT_NOFILE]->rlim_max == 20);
788
789 assert_se(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitNOFILE", RLIMIT_NOFILE, "200:100", rl, NULL) >= 0);
790 assert_se(rl[RLIMIT_NOFILE]);
791 assert_se(rl[RLIMIT_NOFILE]->rlim_cur == 10);
792 assert_se(rl[RLIMIT_NOFILE]->rlim_max == 20);
793
794 rl[RLIMIT_NOFILE] = mfree(rl[RLIMIT_NOFILE]);
795
796 assert_se(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitCPU", RLIMIT_CPU, "56", rl, NULL) >= 0);
797 assert_se(rl[RLIMIT_CPU]);
798 assert_se(rl[RLIMIT_CPU]->rlim_cur == 56);
799 assert_se(rl[RLIMIT_CPU]->rlim_cur == rl[RLIMIT_CPU]->rlim_max);
800
801 assert_se(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitCPU", RLIMIT_CPU, "57s", rl, NULL) >= 0);
802 assert_se(rl[RLIMIT_CPU]);
803 assert_se(rl[RLIMIT_CPU]->rlim_cur == 57);
804 assert_se(rl[RLIMIT_CPU]->rlim_cur == rl[RLIMIT_CPU]->rlim_max);
805
806 assert_se(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitCPU", RLIMIT_CPU, "40s:1m", rl, NULL) >= 0);
807 assert_se(rl[RLIMIT_CPU]);
808 assert_se(rl[RLIMIT_CPU]->rlim_cur == 40);
809 assert_se(rl[RLIMIT_CPU]->rlim_max == 60);
810
811 assert_se(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitCPU", RLIMIT_CPU, "infinity", rl, NULL) >= 0);
812 assert_se(rl[RLIMIT_CPU]);
813 assert_se(rl[RLIMIT_CPU]->rlim_cur == RLIM_INFINITY);
814 assert_se(rl[RLIMIT_CPU]->rlim_cur == rl[RLIMIT_CPU]->rlim_max);
815
816 assert_se(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitCPU", RLIMIT_CPU, "1234ms", rl, NULL) >= 0);
817 assert_se(rl[RLIMIT_CPU]);
818 assert_se(rl[RLIMIT_CPU]->rlim_cur == 2);
819 assert_se(rl[RLIMIT_CPU]->rlim_cur == rl[RLIMIT_CPU]->rlim_max);
820
821 rl[RLIMIT_CPU] = mfree(rl[RLIMIT_CPU]);
822
823 assert_se(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitRTTIME", RLIMIT_RTTIME, "58", rl, NULL) >= 0);
824 assert_se(rl[RLIMIT_RTTIME]);
825 assert_se(rl[RLIMIT_RTTIME]->rlim_cur == 58);
826 assert_se(rl[RLIMIT_RTTIME]->rlim_cur == rl[RLIMIT_RTTIME]->rlim_max);
827
828 assert_se(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitRTTIME", RLIMIT_RTTIME, "58:60", rl, NULL) >= 0);
829 assert_se(rl[RLIMIT_RTTIME]);
830 assert_se(rl[RLIMIT_RTTIME]->rlim_cur == 58);
831 assert_se(rl[RLIMIT_RTTIME]->rlim_max == 60);
832
833 assert_se(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitRTTIME", RLIMIT_RTTIME, "59s", rl, NULL) >= 0);
834 assert_se(rl[RLIMIT_RTTIME]);
835 assert_se(rl[RLIMIT_RTTIME]->rlim_cur == 59 * USEC_PER_SEC);
836 assert_se(rl[RLIMIT_RTTIME]->rlim_cur == rl[RLIMIT_RTTIME]->rlim_max);
837
838 assert_se(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitRTTIME", RLIMIT_RTTIME, "59s:123s", rl, NULL) >= 0);
839 assert_se(rl[RLIMIT_RTTIME]);
840 assert_se(rl[RLIMIT_RTTIME]->rlim_cur == 59 * USEC_PER_SEC);
841 assert_se(rl[RLIMIT_RTTIME]->rlim_max == 123 * USEC_PER_SEC);
842
843 assert_se(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitRTTIME", RLIMIT_RTTIME, "infinity", rl, NULL) >= 0);
844 assert_se(rl[RLIMIT_RTTIME]);
845 assert_se(rl[RLIMIT_RTTIME]->rlim_cur == RLIM_INFINITY);
846 assert_se(rl[RLIMIT_RTTIME]->rlim_cur == rl[RLIMIT_RTTIME]->rlim_max);
847
848 assert_se(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitRTTIME", RLIMIT_RTTIME, "infinity:infinity", rl, NULL) >= 0);
849 assert_se(rl[RLIMIT_RTTIME]);
850 assert_se(rl[RLIMIT_RTTIME]->rlim_cur == RLIM_INFINITY);
851 assert_se(rl[RLIMIT_RTTIME]->rlim_cur == rl[RLIMIT_RTTIME]->rlim_max);
852
853 assert_se(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitRTTIME", RLIMIT_RTTIME, "2345ms", rl, NULL) >= 0);
854 assert_se(rl[RLIMIT_RTTIME]);
855 assert_se(rl[RLIMIT_RTTIME]->rlim_cur == 2345 * USEC_PER_MSEC);
856 assert_se(rl[RLIMIT_RTTIME]->rlim_cur == rl[RLIMIT_RTTIME]->rlim_max);
857
858 rl[RLIMIT_RTTIME] = mfree(rl[RLIMIT_RTTIME]);
859 }
860
861 static void test_config_parse_pass_environ(void) {
862 /* int config_parse_pass_environ(
863 const char *unit,
864 const char *filename,
865 unsigned line,
866 const char *section,
867 unsigned section_line,
868 const char *lvalue,
869 int ltype,
870 const char *rvalue,
871 void *data,
872 void *userdata) */
873 int r;
874 _cleanup_strv_free_ char **passenv = NULL;
875
876 r = config_parse_pass_environ(NULL, "fake", 1, "section", 1,
877 "PassEnvironment", 0, "A B",
878 &passenv, NULL);
879 assert_se(r >= 0);
880 assert_se(strv_length(passenv) == 2);
881 assert_se(streq(passenv[0], "A"));
882 assert_se(streq(passenv[1], "B"));
883
884 r = config_parse_pass_environ(NULL, "fake", 1, "section", 1,
885 "PassEnvironment", 0, "",
886 &passenv, NULL);
887 assert_se(r >= 0);
888 assert_se(strv_isempty(passenv));
889
890 r = config_parse_pass_environ(NULL, "fake", 1, "section", 1,
891 "PassEnvironment", 0, "'invalid name' 'normal_name' A=1 \\",
892 &passenv, NULL);
893 assert_se(r >= 0);
894 assert_se(strv_length(passenv) == 1);
895 assert_se(streq(passenv[0], "normal_name"));
896
897 }
898
899 static void test_unit_dump_config_items(void) {
900 unit_dump_config_items(stdout);
901 }
902
903 int main(int argc, char *argv[]) {
904 _cleanup_(rm_rf_physical_and_freep) char *runtime_dir = NULL;
905 int r;
906
907 test_setup_logging(LOG_INFO);
908
909 r = enter_cgroup_subroot();
910 if (r == -ENOMEDIUM)
911 return log_tests_skipped("cgroupfs not available");
912
913 assert_se(runtime_dir = setup_fake_runtime_dir());
914
915 r = test_unit_file_get_set();
916 test_config_parse_exec();
917 test_config_parse_log_extra_fields();
918 test_config_parse_capability_set();
919 test_config_parse_rlimit();
920 test_config_parse_pass_environ();
921 test_load_env_file_1();
922 test_load_env_file_2();
923 test_load_env_file_3();
924 test_load_env_file_4();
925 test_load_env_file_5();
926 TEST_REQ_RUNNING_SYSTEMD(test_install_printf());
927 test_unit_dump_config_items();
928
929 return r;
930 }