1 /* SPDX-License-Identifier: LGPL-2.1+ */
7 #include <sys/capability.h>
10 #include "all-units.h"
11 #include "alloc-util.h"
12 #include "capability-util.h"
13 #include "conf-parser.h"
18 #include "hostname-util.h"
19 #include "install-printf.h"
21 #include "load-fragment.h"
24 #include "specifier.h"
25 #include "string-util.h"
27 #include "test-helper.h"
29 #include "tmpfile-util.h"
30 #include "user-util.h"
33 static int test_unit_file_get_set(void) {
39 h
= hashmap_new(&string_hash_ops
);
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");
46 log_full_errno(r
== 0 ? LOG_INFO
: LOG_ERR
, r
,
47 "unit_file_get_list: %m");
51 HASHMAP_FOREACH(p
, h
, i
)
52 printf("%s = %s\n", p
->path
, unit_file_state_to_string(p
->state
));
54 unit_file_list_free(h
);
59 static void check_execcommand(ExecCommand
*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
));
76 assert_se(streq_ptr(c
->argv
[1], argv1
));
78 assert_se(streq_ptr(c
->argv
[2], argv2
));
79 assert_se(!!(c
->flags
& EXEC_COMMAND_IGNORE_FAILURE
) == ignore
);
82 static void test_config_parse_exec(void) {
83 /* int config_parse_exec(
88 unsigned section_line,
96 ExecCommand
*c
= NULL
, *c1
;
98 _cleanup_(manager_freep
) Manager
*m
= NULL
;
99 _cleanup_(unit_freep
) Unit
*u
= NULL
;
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");
108 assert_se(manager_startup(m
, NULL
, NULL
) >= 0);
110 assert_se(u
= unit_new(m
, sizeof(Service
)));
112 log_info("/* basic test */");
113 r
= config_parse_exec(NULL
, "fake", 1, "section", 1,
114 "LValue", 0, "/RValue r1",
117 check_execcommand(c
, "/RValue", "/RValue", "r1", NULL
, false);
119 r
= config_parse_exec(NULL
, "fake", 2, "section", 1,
120 "LValue", 0, "/RValue///slashes r1///",
123 log_info("/* test slashes */");
125 c1
= c
->command_next
;
126 check_execcommand(c1
, "/RValue/slashes", "/RValue///slashes", "r1///", NULL
, false);
128 log_info("/* trailing slash */");
129 r
= config_parse_exec(NULL
, "fake", 4, "section", 1,
130 "LValue", 0, "/RValue/ argv0 r1",
132 assert_se(r
== -ENOEXEC
);
133 assert_se(c1
->command_next
== NULL
);
135 log_info("/* honour_argv0 */");
136 r
= config_parse_exec(NULL
, "fake", 3, "section", 1,
137 "LValue", 0, "@/RValue///slashes2 ///argv0 r1",
140 c1
= c1
->command_next
;
141 check_execcommand(c1
, "/RValue/slashes2", "///argv0", "r1", NULL
, false);
143 log_info("/* honour_argv0, no args */");
144 r
= config_parse_exec(NULL
, "fake", 3, "section", 1,
145 "LValue", 0, "@/RValue",
147 assert_se(r
== -ENOEXEC
);
148 assert_se(c1
->command_next
== NULL
);
150 log_info("/* no command, whitespace only, reset */");
151 r
= config_parse_exec(NULL
, "fake", 3, "section", 1,
155 assert_se(c
== NULL
);
157 log_info("/* ignore && honour_argv0 */");
158 r
= config_parse_exec(NULL
, "fake", 4, "section", 1,
159 "LValue", 0, "-@/RValue///slashes3 argv0a r1",
163 check_execcommand(c1
, "/RValue/slashes3", "argv0a", "r1", NULL
, true);
165 log_info("/* ignore && honour_argv0 */");
166 r
= config_parse_exec(NULL
, "fake", 4, "section", 1,
167 "LValue", 0, "@-/RValue///slashes4 argv0b r1",
170 c1
= c1
->command_next
;
171 check_execcommand(c1
, "/RValue/slashes4", "argv0b", "r1", NULL
, true);
173 log_info("/* ignore && ignore */");
174 r
= config_parse_exec(NULL
, "fake", 4, "section", 1,
175 "LValue", 0, "--/RValue argv0 r1",
178 assert_se(c1
->command_next
== NULL
);
180 log_info("/* ignore && ignore (2) */");
181 r
= config_parse_exec(NULL
, "fake", 4, "section", 1,
182 "LValue", 0, "-@-/RValue argv0 r1",
185 assert_se(c1
->command_next
== NULL
);
187 log_info("/* semicolon */");
188 r
= config_parse_exec(NULL
, "fake", 5, "section", 1,
190 "-@/RValue argv0 r1 ; "
194 c1
= c1
->command_next
;
195 check_execcommand(c1
, "/RValue", "argv0", "r1", NULL
, true);
197 c1
= c1
->command_next
;
198 check_execcommand(c1
, "/goo/goo", NULL
, "boo", NULL
, false);
200 log_info("/* two semicolons in a row */");
201 r
= config_parse_exec(NULL
, "fake", 5, "section", 1,
203 "-@/RValue argv0 r1 ; ; "
206 assert_se(r
== -ENOEXEC
);
207 c1
= c1
->command_next
;
208 check_execcommand(c1
, "/RValue", "argv0", "r1", NULL
, true);
210 /* second command fails because the executable name is ";" */
211 assert_se(c1
->command_next
== NULL
);
213 log_info("/* trailing semicolon */");
214 r
= config_parse_exec(NULL
, "fake", 5, "section", 1,
216 "-@/RValue argv0 r1 ; ",
219 c1
= c1
->command_next
;
220 check_execcommand(c1
, "/RValue", "argv0", "r1", NULL
, true);
222 assert_se(c1
->command_next
== NULL
);
224 log_info("/* trailing semicolon, no whitespace */");
225 r
= config_parse_exec(NULL
, "fake", 5, "section", 1,
227 "-@/RValue argv0 r1 ;",
230 c1
= c1
->command_next
;
231 check_execcommand(c1
, "/RValue", "argv0", "r1", NULL
, true);
233 assert_se(c1
->command_next
== NULL
);
235 log_info("/* trailing semicolon in single quotes */");
236 r
= config_parse_exec(NULL
, "fake", 5, "section", 1,
238 "-@/RValue argv0 r1 ';'",
241 c1
= c1
->command_next
;
242 check_execcommand(c1
, "/RValue", "argv0", "r1", ";", true);
244 log_info("/* escaped semicolon */");
245 r
= config_parse_exec(NULL
, "fake", 5, "section", 1,
250 c1
= c1
->command_next
;
251 check_execcommand(c1
, "/bin/find", NULL
, ";", NULL
, false);
253 log_info("/* escaped semicolon with following arg */");
254 r
= config_parse_exec(NULL
, "fake", 5, "section", 1,
259 c1
= c1
->command_next
;
260 check_execcommand(c1
,
261 "/sbin/find", NULL
, ";", "/x", false);
263 log_info("/* escaped semicolon as part of an expression */");
264 r
= config_parse_exec(NULL
, "fake", 5, "section", 1,
269 c1
= c1
->command_next
;
270 check_execcommand(c1
,
271 "/sbin/find", NULL
, "\\;x", NULL
, false);
273 log_info("/* encoded semicolon */");
274 r
= config_parse_exec(NULL
, "fake", 5, "section", 1,
279 c1
= c1
->command_next
;
280 check_execcommand(c1
, "/bin/find", NULL
, ";", NULL
, false);
282 log_info("/* quoted semicolon */");
283 r
= config_parse_exec(NULL
, "fake", 5, "section", 1,
288 c1
= c1
->command_next
;
289 check_execcommand(c1
, "/bin/find", NULL
, ";", NULL
, false);
291 log_info("/* quoted semicolon with following arg */");
292 r
= config_parse_exec(NULL
, "fake", 5, "section", 1,
294 "/sbin/find \";\" /x",
297 c1
= c1
->command_next
;
298 check_execcommand(c1
,
299 "/sbin/find", NULL
, ";", "/x", false);
301 log_info("/* spaces in the filename */");
302 r
= config_parse_exec(NULL
, "fake", 5, "section", 1,
304 "\"/PATH WITH SPACES/daemon\" -1 -2",
307 c1
= c1
->command_next
;
308 check_execcommand(c1
,
309 "/PATH WITH SPACES/daemon", NULL
, "-1", "-2", false);
311 log_info("/* spaces in the filename, no args */");
312 r
= config_parse_exec(NULL
, "fake", 5, "section", 1,
314 "\"/PATH WITH SPACES/daemon -1 -2\"",
317 c1
= c1
->command_next
;
318 check_execcommand(c1
,
319 "/PATH WITH SPACES/daemon -1 -2", NULL
, NULL
, NULL
, false);
321 log_info("/* spaces in the filename, everything quoted */");
322 r
= config_parse_exec(NULL
, "fake", 5, "section", 1,
324 "\"/PATH WITH SPACES/daemon\" \"-1\" '-2'",
327 c1
= c1
->command_next
;
328 check_execcommand(c1
,
329 "/PATH WITH SPACES/daemon", NULL
, "-1", "-2", false);
331 log_info("/* escaped spaces in the filename */");
332 r
= config_parse_exec(NULL
, "fake", 5, "section", 1,
334 "\"/PATH\\sWITH\\sSPACES/daemon\" '-1 -2'",
337 c1
= c1
->command_next
;
338 check_execcommand(c1
,
339 "/PATH WITH SPACES/daemon", NULL
, "-1 -2", NULL
, false);
341 log_info("/* escaped spaces in the filename (2) */");
342 r
= config_parse_exec(NULL
, "fake", 5, "section", 1,
344 "\"/PATH\\x20WITH\\x20SPACES/daemon\" \"-1 -2\"",
347 c1
= c1
->command_next
;
348 check_execcommand(c1
,
349 "/PATH WITH SPACES/daemon", NULL
, "-1 -2", NULL
, false);
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
;
356 log_info("/* invalid character: \\%c */", *ccc
);
357 r
= config_parse_exec(NULL
, "fake", 4, "section", 1,
360 assert_se(r
== -ENOEXEC
);
361 assert_se(c1
->command_next
== NULL
);
364 log_info("/* valid character: \\s */");
365 r
= config_parse_exec(NULL
, "fake", 4, "section", 1,
366 "LValue", 0, "/path\\s",
369 c1
= c1
->command_next
;
370 check_execcommand(c1
, "/path ", NULL
, NULL
, NULL
, false);
372 log_info("/* quoted backslashes */");
373 r
= config_parse_exec(NULL
, "fake", 5, "section", 1,
375 "/bin/grep '\\w+\\K'",
378 c1
= c1
->command_next
;
379 check_execcommand(c1
, "/bin/grep", NULL
, "\\w+\\K", NULL
, false);
381 log_info("/* trailing backslash: \\ */");
382 /* backslash is invalid */
383 r
= config_parse_exec(NULL
, "fake", 4, "section", 1,
384 "LValue", 0, "/path\\",
386 assert_se(r
== -ENOEXEC
);
387 assert_se(c1
->command_next
== NULL
);
389 log_info("/* missing ending ' */");
390 r
= config_parse_exec(NULL
, "fake", 4, "section", 1,
391 "LValue", 0, "/path 'foo",
393 assert_se(r
== -ENOEXEC
);
394 assert_se(c1
->command_next
== NULL
);
396 log_info("/* missing ending ' with trailing backslash */");
397 r
= config_parse_exec(NULL
, "fake", 4, "section", 1,
398 "LValue", 0, "/path 'foo\\",
400 assert_se(r
== -ENOEXEC
);
401 assert_se(c1
->command_next
== NULL
);
403 log_info("/* invalid space between modifiers */");
404 r
= config_parse_exec(NULL
, "fake", 4, "section", 1,
405 "LValue", 0, "- /path",
408 assert_se(c1
->command_next
== NULL
);
410 log_info("/* only modifiers, no path */");
411 r
= config_parse_exec(NULL
, "fake", 4, "section", 1,
415 assert_se(c1
->command_next
== NULL
);
417 log_info("/* empty argument, reset */");
418 r
= config_parse_exec(NULL
, "fake", 4, "section", 1,
422 assert_se(c
== NULL
);
424 exec_command_free_list(c
);
427 static void test_config_parse_log_extra_fields(void) {
428 /* int config_parse_log_extra_fields(
430 const char *filename,
433 unsigned section_line,
442 _cleanup_(manager_freep
) Manager
*m
= NULL
;
443 _cleanup_(unit_freep
) Unit
*u
= NULL
;
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");
453 assert_se(manager_startup(m
, NULL
, NULL
) >= 0);
455 assert_se(u
= unit_new(m
, sizeof(Service
)));
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 ' ' \"",
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
));
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 ' '",
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
));
477 exec_context_dump(&c
, stdout
, " --> ");
479 log_info("/* %s – reset */", __func__
);
480 r
= config_parse_log_extra_fields(NULL
, "fake", 1, "section", 1,
484 assert_se(c
.n_log_extra_fields
== 0);
486 exec_context_free_log_extra_fields(&c
);
488 log_info("/* %s – bye */", __func__
);
506 "#SPAMD_ARGS=\"-d --socketpath=/var/lib/bulwark/spamd \\\n" \
507 "#--nouser-config \\\n" \
513 "HWMON_MODULES=\"coretemp f71882fg\"\n" \
515 "# For compatibility reasons\n" \
517 "MODULE_0=coretemp\n" \
524 static void test_load_env_file_1(void) {
525 _cleanup_strv_free_
char **data
= NULL
;
528 _cleanup_(unlink_tempfilep
) char name
[] = "/tmp/test-load-env-file.XXXXXX";
529 _cleanup_close_
int fd
;
531 fd
= mkostemp_safe(name
);
533 assert_se(write(fd
, env_file_1
, sizeof(env_file_1
)) == sizeof(env_file_1
));
535 r
= load_env_file(NULL
, name
, &data
);
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
);
546 static void test_load_env_file_2(void) {
547 _cleanup_strv_free_
char **data
= NULL
;
550 _cleanup_(unlink_tempfilep
) char name
[] = "/tmp/test-load-env-file.XXXXXX";
551 _cleanup_close_
int fd
;
553 fd
= mkostemp_safe(name
);
555 assert_se(write(fd
, env_file_2
, sizeof(env_file_2
)) == sizeof(env_file_2
));
557 r
= load_env_file(NULL
, name
, &data
);
559 assert_se(streq(data
[0], "a=a"));
560 assert_se(data
[1] == NULL
);
563 static void test_load_env_file_3(void) {
564 _cleanup_strv_free_
char **data
= NULL
;
567 _cleanup_(unlink_tempfilep
) char name
[] = "/tmp/test-load-env-file.XXXXXX";
568 _cleanup_close_
int fd
;
570 fd
= mkostemp_safe(name
);
572 assert_se(write(fd
, env_file_3
, sizeof(env_file_3
)) == sizeof(env_file_3
));
574 r
= load_env_file(NULL
, name
, &data
);
576 assert_se(data
== NULL
);
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
;
585 fd
= mkostemp_safe(name
);
587 assert_se(write(fd
, env_file_4
, sizeof(env_file_4
)) == sizeof(env_file_4
));
589 r
= load_env_file(NULL
, name
, &data
);
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
);
597 static void test_load_env_file_5(void) {
598 _cleanup_strv_free_
char **data
= NULL
;
601 _cleanup_(unlink_tempfilep
) char name
[] = "/tmp/test-load-env-file.XXXXXX";
602 _cleanup_close_
int fd
;
604 fd
= mkostemp_safe(name
);
606 assert_se(write(fd
, env_file_5
, sizeof(env_file_5
)) == sizeof(env_file_5
));
608 r
= load_env_file(NULL
, name
, &data
);
610 assert_se(streq(data
[0], "a="));
611 assert_se(streq(data
[1], "b="));
612 assert_se(data
[2] == NULL
);
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
, };
625 _cleanup_free_
char *mid
= NULL
, *bid
= NULL
, *host
= NULL
, *gid
= NULL
, *group
= NULL
, *uid
= NULL
, *user
= NULL
;
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);
635 #define expect(src, pattern, result) \
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); \
647 assert_se(streq(t, result)); \
648 } else assert_se(t == NULL); \
649 strcpy(i.name, d1); \
650 strcpy(i.path, d2); \
653 expect(i
, "%n", "name.service");
654 expect(i
, "%N", "name");
655 expect(i
, "%p", "name");
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
);
663 expect(i
, "%m", mid
);
664 expect(i
, "%b", bid
);
665 expect(i
, "%H", host
);
667 expect(i2
, "%g", group
);
668 expect(i2
, "%G", gid
);
669 expect(i2
, "%u", user
);
670 expect(i2
, "%U", uid
);
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
);
680 expect(i3
, "%m", mid
);
681 expect(i3
, "%b", bid
);
682 expect(i3
, "%H", host
);
684 expect(i4
, "%g", group
);
685 expect(i4
, "%G", gid
);
686 expect(i4
, "%u", user
);
687 expect(i4
, "%U", uid
);
690 static uint64_t make_cap(int cap
) {
691 return ((uint64_t) 1ULL << (uint64_t) cap
);
694 static void test_config_parse_capability_set(void) {
695 /* int config_parse_capability_set(
697 const char *filename,
700 unsigned section_line,
707 uint64_t capability_bounding_set
= 0;
709 r
= config_parse_capability_set(NULL
, "fake", 1, "section", 1,
710 "CapabilityBoundingSet", 0, "CAP_NET_RAW",
711 &capability_bounding_set
, NULL
);
713 assert_se(capability_bounding_set
== make_cap(CAP_NET_RAW
));
715 r
= config_parse_capability_set(NULL
, "fake", 1, "section", 1,
716 "CapabilityBoundingSet", 0, "CAP_NET_ADMIN",
717 &capability_bounding_set
, NULL
);
719 assert_se(capability_bounding_set
== (make_cap(CAP_NET_RAW
) | make_cap(CAP_NET_ADMIN
)));
721 r
= config_parse_capability_set(NULL
, "fake", 1, "section", 1,
722 "CapabilityBoundingSet", 0, "~CAP_NET_ADMIN",
723 &capability_bounding_set
, NULL
);
725 assert_se(capability_bounding_set
== make_cap(CAP_NET_RAW
));
727 r
= config_parse_capability_set(NULL
, "fake", 1, "section", 1,
728 "CapabilityBoundingSet", 0, "",
729 &capability_bounding_set
, NULL
);
731 assert_se(capability_bounding_set
== UINT64_C(0));
733 r
= config_parse_capability_set(NULL
, "fake", 1, "section", 1,
734 "CapabilityBoundingSet", 0, "~",
735 &capability_bounding_set
, NULL
);
737 assert_se(cap_test_all(capability_bounding_set
));
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
);
744 assert_se(capability_bounding_set
== (make_cap(CAP_NET_RAW
) | make_cap(CAP_NET_ADMIN
)));
747 static void test_config_parse_rlimit(void) {
748 struct rlimit
* rl
[_RLIMIT_MAX
] = {};
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
);
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);
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
);
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
);
770 rl
[RLIMIT_NOFILE
]->rlim_cur
= 10;
771 rl
[RLIMIT_NOFILE
]->rlim_max
= 20;
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);
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);
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);
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);
794 rl
[RLIMIT_NOFILE
] = mfree(rl
[RLIMIT_NOFILE
]);
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
);
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
);
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);
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
);
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
);
821 rl
[RLIMIT_CPU
] = mfree(rl
[RLIMIT_CPU
]);
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
);
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);
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
);
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
);
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
);
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
);
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
);
858 rl
[RLIMIT_RTTIME
] = mfree(rl
[RLIMIT_RTTIME
]);
861 static void test_config_parse_pass_environ(void) {
862 /* int config_parse_pass_environ(
864 const char *filename,
867 unsigned section_line,
874 _cleanup_strv_free_
char **passenv
= NULL
;
876 r
= config_parse_pass_environ(NULL
, "fake", 1, "section", 1,
877 "PassEnvironment", 0, "A B",
880 assert_se(strv_length(passenv
) == 2);
881 assert_se(streq(passenv
[0], "A"));
882 assert_se(streq(passenv
[1], "B"));
884 r
= config_parse_pass_environ(NULL
, "fake", 1, "section", 1,
885 "PassEnvironment", 0, "",
888 assert_se(strv_isempty(passenv
));
890 r
= config_parse_pass_environ(NULL
, "fake", 1, "section", 1,
891 "PassEnvironment", 0, "'invalid name' 'normal_name' A=1 \\",
894 assert_se(strv_length(passenv
) == 1);
895 assert_se(streq(passenv
[0], "normal_name"));
899 static void test_unit_dump_config_items(void) {
900 unit_dump_config_items(stdout
);
903 int main(int argc
, char *argv
[]) {
904 _cleanup_(rm_rf_physical_and_freep
) char *runtime_dir
= NULL
;
907 test_setup_logging(LOG_INFO
);
909 r
= enter_cgroup_subroot();
911 return log_tests_skipped("cgroupfs not available");
913 assert_se(runtime_dir
= setup_fake_runtime_dir());
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();