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"
16 #include "format-util.h"
19 #include "hostname-util.h"
20 #include "install-printf.h"
22 #include "load-fragment.h"
24 #include "memory-util.h"
26 #include "specifier.h"
27 #include "string-util.h"
29 #include "test-helper.h"
31 #include "tmpfile-util.h"
32 #include "user-util.h"
34 static int test_unit_file_get_set(void) {
40 h
= hashmap_new(&string_hash_ops
);
43 r
= unit_file_get_list(UNIT_FILE_SYSTEM
, NULL
, h
, NULL
, NULL
);
44 if (IN_SET(r
, -EPERM
, -EACCES
))
45 return log_tests_skipped_errno(r
, "unit_file_get_list");
47 log_full_errno(r
== 0 ? LOG_INFO
: LOG_ERR
, r
,
48 "unit_file_get_list: %m");
52 HASHMAP_FOREACH(p
, h
, i
)
53 printf("%s = %s\n", p
->path
, unit_file_state_to_string(p
->state
));
55 unit_file_list_free(h
);
60 static void check_execcommand(ExecCommand
*c
,
69 log_info("expect: \"%s\" [\"%s\" \"%s\" \"%s\"]",
70 path
, argv0
?: path
, argv1
, argv2
);
71 n
= strv_length(c
->argv
);
72 log_info("actual: \"%s\" [\"%s\" \"%s\" \"%s\"]",
73 c
->path
, c
->argv
[0], n
> 0 ? c
->argv
[1] : NULL
, n
> 1 ? c
->argv
[2] : NULL
);
74 assert_se(streq(c
->path
, path
));
75 assert_se(streq(c
->argv
[0], argv0
?: path
));
77 assert_se(streq_ptr(c
->argv
[1], argv1
));
79 assert_se(streq_ptr(c
->argv
[2], argv2
));
80 assert_se(!!(c
->flags
& EXEC_COMMAND_IGNORE_FAILURE
) == ignore
);
83 static void test_config_parse_exec(void) {
84 /* int config_parse_exec(
89 unsigned section_line,
97 ExecCommand
*c
= NULL
, *c1
;
99 _cleanup_(manager_freep
) Manager
*m
= NULL
;
100 _cleanup_(unit_freep
) Unit
*u
= NULL
;
102 r
= manager_new(UNIT_FILE_USER
, MANAGER_TEST_RUN_MINIMAL
, &m
);
103 if (MANAGER_SKIP_TEST(r
)) {
104 log_notice_errno(r
, "Skipping test: manager_new: %m");
109 assert_se(manager_startup(m
, NULL
, NULL
) >= 0);
111 assert_se(u
= unit_new(m
, sizeof(Service
)));
113 log_info("/* basic test */");
114 r
= config_parse_exec(NULL
, "fake", 1, "section", 1,
115 "LValue", 0, "/RValue r1",
118 check_execcommand(c
, "/RValue", "/RValue", "r1", NULL
, false);
120 r
= config_parse_exec(NULL
, "fake", 2, "section", 1,
121 "LValue", 0, "/RValue///slashes r1///",
124 log_info("/* test slashes */");
126 c1
= c
->command_next
;
127 check_execcommand(c1
, "/RValue/slashes", "/RValue///slashes", "r1///", NULL
, false);
129 log_info("/* trailing slash */");
130 r
= config_parse_exec(NULL
, "fake", 4, "section", 1,
131 "LValue", 0, "/RValue/ argv0 r1",
133 assert_se(r
== -ENOEXEC
);
134 assert_se(c1
->command_next
== NULL
);
136 log_info("/* honour_argv0 */");
137 r
= config_parse_exec(NULL
, "fake", 3, "section", 1,
138 "LValue", 0, "@/RValue///slashes2 ///argv0 r1",
141 c1
= c1
->command_next
;
142 check_execcommand(c1
, "/RValue/slashes2", "///argv0", "r1", NULL
, false);
144 log_info("/* honour_argv0, no args */");
145 r
= config_parse_exec(NULL
, "fake", 3, "section", 1,
146 "LValue", 0, "@/RValue",
148 assert_se(r
== -ENOEXEC
);
149 assert_se(c1
->command_next
== NULL
);
151 log_info("/* no command, whitespace only, reset */");
152 r
= config_parse_exec(NULL
, "fake", 3, "section", 1,
156 assert_se(c
== NULL
);
158 log_info("/* ignore && honour_argv0 */");
159 r
= config_parse_exec(NULL
, "fake", 4, "section", 1,
160 "LValue", 0, "-@/RValue///slashes3 argv0a r1",
164 check_execcommand(c1
, "/RValue/slashes3", "argv0a", "r1", NULL
, true);
166 log_info("/* ignore && honour_argv0 */");
167 r
= config_parse_exec(NULL
, "fake", 4, "section", 1,
168 "LValue", 0, "@-/RValue///slashes4 argv0b r1",
171 c1
= c1
->command_next
;
172 check_execcommand(c1
, "/RValue/slashes4", "argv0b", "r1", NULL
, true);
174 log_info("/* ignore && ignore */");
175 r
= config_parse_exec(NULL
, "fake", 4, "section", 1,
176 "LValue", 0, "--/RValue argv0 r1",
179 assert_se(c1
->command_next
== NULL
);
181 log_info("/* ignore && ignore (2) */");
182 r
= config_parse_exec(NULL
, "fake", 4, "section", 1,
183 "LValue", 0, "-@-/RValue argv0 r1",
186 assert_se(c1
->command_next
== NULL
);
188 log_info("/* semicolon */");
189 r
= config_parse_exec(NULL
, "fake", 5, "section", 1,
191 "-@/RValue argv0 r1 ; "
195 c1
= c1
->command_next
;
196 check_execcommand(c1
, "/RValue", "argv0", "r1", NULL
, true);
198 c1
= c1
->command_next
;
199 check_execcommand(c1
, "/goo/goo", NULL
, "boo", NULL
, false);
201 log_info("/* two semicolons in a row */");
202 r
= config_parse_exec(NULL
, "fake", 5, "section", 1,
204 "-@/RValue argv0 r1 ; ; "
207 assert_se(r
== -ENOEXEC
);
208 c1
= c1
->command_next
;
209 check_execcommand(c1
, "/RValue", "argv0", "r1", NULL
, true);
211 /* second command fails because the executable name is ";" */
212 assert_se(c1
->command_next
== NULL
);
214 log_info("/* trailing semicolon */");
215 r
= config_parse_exec(NULL
, "fake", 5, "section", 1,
217 "-@/RValue argv0 r1 ; ",
220 c1
= c1
->command_next
;
221 check_execcommand(c1
, "/RValue", "argv0", "r1", NULL
, true);
223 assert_se(c1
->command_next
== NULL
);
225 log_info("/* trailing semicolon, no whitespace */");
226 r
= config_parse_exec(NULL
, "fake", 5, "section", 1,
228 "-@/RValue argv0 r1 ;",
231 c1
= c1
->command_next
;
232 check_execcommand(c1
, "/RValue", "argv0", "r1", NULL
, true);
234 assert_se(c1
->command_next
== NULL
);
236 log_info("/* trailing semicolon in single quotes */");
237 r
= config_parse_exec(NULL
, "fake", 5, "section", 1,
239 "-@/RValue argv0 r1 ';'",
242 c1
= c1
->command_next
;
243 check_execcommand(c1
, "/RValue", "argv0", "r1", ";", true);
245 log_info("/* escaped semicolon */");
246 r
= config_parse_exec(NULL
, "fake", 5, "section", 1,
251 c1
= c1
->command_next
;
252 check_execcommand(c1
, "/bin/find", NULL
, ";", NULL
, false);
254 log_info("/* escaped semicolon with following arg */");
255 r
= config_parse_exec(NULL
, "fake", 5, "section", 1,
260 c1
= c1
->command_next
;
261 check_execcommand(c1
,
262 "/sbin/find", NULL
, ";", "/x", false);
264 log_info("/* escaped semicolon as part of an expression */");
265 r
= config_parse_exec(NULL
, "fake", 5, "section", 1,
270 c1
= c1
->command_next
;
271 check_execcommand(c1
,
272 "/sbin/find", NULL
, "\\;x", NULL
, false);
274 log_info("/* encoded semicolon */");
275 r
= config_parse_exec(NULL
, "fake", 5, "section", 1,
280 c1
= c1
->command_next
;
281 check_execcommand(c1
, "/bin/find", NULL
, ";", NULL
, false);
283 log_info("/* quoted semicolon */");
284 r
= config_parse_exec(NULL
, "fake", 5, "section", 1,
289 c1
= c1
->command_next
;
290 check_execcommand(c1
, "/bin/find", NULL
, ";", NULL
, false);
292 log_info("/* quoted semicolon with following arg */");
293 r
= config_parse_exec(NULL
, "fake", 5, "section", 1,
295 "/sbin/find \";\" /x",
298 c1
= c1
->command_next
;
299 check_execcommand(c1
,
300 "/sbin/find", NULL
, ";", "/x", false);
302 log_info("/* spaces in the filename */");
303 r
= config_parse_exec(NULL
, "fake", 5, "section", 1,
305 "\"/PATH WITH SPACES/daemon\" -1 -2",
308 c1
= c1
->command_next
;
309 check_execcommand(c1
,
310 "/PATH WITH SPACES/daemon", NULL
, "-1", "-2", false);
312 log_info("/* spaces in the filename, no args */");
313 r
= config_parse_exec(NULL
, "fake", 5, "section", 1,
315 "\"/PATH WITH SPACES/daemon -1 -2\"",
318 c1
= c1
->command_next
;
319 check_execcommand(c1
,
320 "/PATH WITH SPACES/daemon -1 -2", NULL
, NULL
, NULL
, false);
322 log_info("/* spaces in the filename, everything quoted */");
323 r
= config_parse_exec(NULL
, "fake", 5, "section", 1,
325 "\"/PATH WITH SPACES/daemon\" \"-1\" '-2'",
328 c1
= c1
->command_next
;
329 check_execcommand(c1
,
330 "/PATH WITH SPACES/daemon", NULL
, "-1", "-2", false);
332 log_info("/* escaped spaces in the filename */");
333 r
= config_parse_exec(NULL
, "fake", 5, "section", 1,
335 "\"/PATH\\sWITH\\sSPACES/daemon\" '-1 -2'",
338 c1
= c1
->command_next
;
339 check_execcommand(c1
,
340 "/PATH WITH SPACES/daemon", NULL
, "-1 -2", NULL
, false);
342 log_info("/* escaped spaces in the filename (2) */");
343 r
= config_parse_exec(NULL
, "fake", 5, "section", 1,
345 "\"/PATH\\x20WITH\\x20SPACES/daemon\" \"-1 -2\"",
348 c1
= c1
->command_next
;
349 check_execcommand(c1
,
350 "/PATH WITH SPACES/daemon", NULL
, "-1 -2", NULL
, false);
352 for (ccc
= "abfnrtv\\\'\"x"; *ccc
; ccc
++) {
353 /* \\x is an incomplete hexadecimal sequence, invalid because of the slash */
354 char path
[] = "/path\\X";
355 path
[sizeof(path
) - 2] = *ccc
;
357 log_info("/* invalid character: \\%c */", *ccc
);
358 r
= config_parse_exec(NULL
, "fake", 4, "section", 1,
361 assert_se(r
== -ENOEXEC
);
362 assert_se(c1
->command_next
== NULL
);
365 log_info("/* valid character: \\s */");
366 r
= config_parse_exec(NULL
, "fake", 4, "section", 1,
367 "LValue", 0, "/path\\s",
370 c1
= c1
->command_next
;
371 check_execcommand(c1
, "/path ", NULL
, NULL
, NULL
, false);
373 log_info("/* quoted backslashes */");
374 r
= config_parse_exec(NULL
, "fake", 5, "section", 1,
376 "/bin/grep '\\w+\\K'",
379 c1
= c1
->command_next
;
380 check_execcommand(c1
, "/bin/grep", NULL
, "\\w+\\K", NULL
, false);
382 log_info("/* trailing backslash: \\ */");
383 /* backslash is invalid */
384 r
= config_parse_exec(NULL
, "fake", 4, "section", 1,
385 "LValue", 0, "/path\\",
387 assert_se(r
== -ENOEXEC
);
388 assert_se(c1
->command_next
== NULL
);
390 log_info("/* missing ending ' */");
391 r
= config_parse_exec(NULL
, "fake", 4, "section", 1,
392 "LValue", 0, "/path 'foo",
394 assert_se(r
== -ENOEXEC
);
395 assert_se(c1
->command_next
== NULL
);
397 log_info("/* missing ending ' with trailing backslash */");
398 r
= config_parse_exec(NULL
, "fake", 4, "section", 1,
399 "LValue", 0, "/path 'foo\\",
401 assert_se(r
== -ENOEXEC
);
402 assert_se(c1
->command_next
== NULL
);
404 log_info("/* invalid space between modifiers */");
405 r
= config_parse_exec(NULL
, "fake", 4, "section", 1,
406 "LValue", 0, "- /path",
409 assert_se(c1
->command_next
== NULL
);
411 log_info("/* only modifiers, no path */");
412 r
= config_parse_exec(NULL
, "fake", 4, "section", 1,
416 assert_se(c1
->command_next
== NULL
);
418 log_info("/* empty argument, reset */");
419 r
= config_parse_exec(NULL
, "fake", 4, "section", 1,
423 assert_se(c
== NULL
);
425 exec_command_free_list(c
);
428 static void test_config_parse_log_extra_fields(void) {
429 /* int config_parse_log_extra_fields(
431 const char *filename,
434 unsigned section_line,
443 _cleanup_(manager_freep
) Manager
*m
= NULL
;
444 _cleanup_(unit_freep
) Unit
*u
= NULL
;
447 r
= manager_new(UNIT_FILE_USER
, MANAGER_TEST_RUN_MINIMAL
, &m
);
448 if (MANAGER_SKIP_TEST(r
)) {
449 log_notice_errno(r
, "Skipping test: manager_new: %m");
454 assert_se(manager_startup(m
, NULL
, NULL
) >= 0);
456 assert_se(u
= unit_new(m
, sizeof(Service
)));
458 log_info("/* %s – basic test */", __func__
);
459 r
= config_parse_log_extra_fields(NULL
, "fake", 1, "section", 1,
460 "LValue", 0, "FOO=BAR \"QOOF=quux ' ' \"",
463 assert_se(c
.n_log_extra_fields
== 2);
464 assert_se(strneq(c
.log_extra_fields
[0].iov_base
, "FOO=BAR", c
.log_extra_fields
[0].iov_len
));
465 assert_se(strneq(c
.log_extra_fields
[1].iov_base
, "QOOF=quux ' ' ", c
.log_extra_fields
[1].iov_len
));
467 log_info("/* %s – add some */", __func__
);
468 r
= config_parse_log_extra_fields(NULL
, "fake", 1, "section", 1,
469 "LValue", 0, "FOO2=BAR2 QOOF2=quux ' '",
472 assert_se(c
.n_log_extra_fields
== 4);
473 assert_se(strneq(c
.log_extra_fields
[0].iov_base
, "FOO=BAR", c
.log_extra_fields
[0].iov_len
));
474 assert_se(strneq(c
.log_extra_fields
[1].iov_base
, "QOOF=quux ' ' ", c
.log_extra_fields
[1].iov_len
));
475 assert_se(strneq(c
.log_extra_fields
[2].iov_base
, "FOO2=BAR2", c
.log_extra_fields
[2].iov_len
));
476 assert_se(strneq(c
.log_extra_fields
[3].iov_base
, "QOOF2=quux", c
.log_extra_fields
[3].iov_len
));
478 exec_context_dump(&c
, stdout
, " --> ");
480 log_info("/* %s – reset */", __func__
);
481 r
= config_parse_log_extra_fields(NULL
, "fake", 1, "section", 1,
485 assert_se(c
.n_log_extra_fields
== 0);
487 exec_context_free_log_extra_fields(&c
);
489 log_info("/* %s – bye */", __func__
);
507 "#SPAMD_ARGS=\"-d --socketpath=/var/lib/bulwark/spamd \\\n" \
508 "#--nouser-config \\\n" \
514 "HWMON_MODULES=\"coretemp f71882fg\"\n" \
516 "# For compatibility reasons\n" \
518 "MODULE_0=coretemp\n" \
525 static void test_load_env_file_1(void) {
526 _cleanup_strv_free_
char **data
= NULL
;
529 _cleanup_(unlink_tempfilep
) char name
[] = "/tmp/test-load-env-file.XXXXXX";
530 _cleanup_close_
int fd
;
532 fd
= mkostemp_safe(name
);
534 assert_se(write(fd
, env_file_1
, strlen(env_file_1
)) == strlen(env_file_1
));
536 r
= load_env_file(NULL
, name
, &data
);
538 assert_se(streq(data
[0], "a=a"));
539 assert_se(streq(data
[1], "b=bc"));
540 assert_se(streq(data
[2], "d=def"));
541 assert_se(streq(data
[3], "g=g "));
542 assert_se(streq(data
[4], "h=h"));
543 assert_se(streq(data
[5], "i=i"));
544 assert_se(data
[6] == NULL
);
547 static void test_load_env_file_2(void) {
548 _cleanup_strv_free_
char **data
= NULL
;
551 _cleanup_(unlink_tempfilep
) char name
[] = "/tmp/test-load-env-file.XXXXXX";
552 _cleanup_close_
int fd
;
554 fd
= mkostemp_safe(name
);
556 assert_se(write(fd
, env_file_2
, strlen(env_file_2
)) == strlen(env_file_2
));
558 r
= load_env_file(NULL
, name
, &data
);
560 assert_se(streq(data
[0], "a=a"));
561 assert_se(data
[1] == NULL
);
564 static void test_load_env_file_3(void) {
565 _cleanup_strv_free_
char **data
= NULL
;
568 _cleanup_(unlink_tempfilep
) char name
[] = "/tmp/test-load-env-file.XXXXXX";
569 _cleanup_close_
int fd
;
571 fd
= mkostemp_safe(name
);
573 assert_se(write(fd
, env_file_3
, strlen(env_file_3
)) == strlen(env_file_3
));
575 r
= load_env_file(NULL
, name
, &data
);
577 assert_se(data
== NULL
);
580 static void test_load_env_file_4(void) {
581 _cleanup_strv_free_
char **data
= NULL
;
582 _cleanup_(unlink_tempfilep
) char name
[] = "/tmp/test-load-env-file.XXXXXX";
583 _cleanup_close_
int fd
;
586 fd
= mkostemp_safe(name
);
588 assert_se(write(fd
, env_file_4
, strlen(env_file_4
)) == strlen(env_file_4
));
590 r
= load_env_file(NULL
, name
, &data
);
592 assert_se(streq(data
[0], "HWMON_MODULES=coretemp f71882fg"));
593 assert_se(streq(data
[1], "MODULE_0=coretemp"));
594 assert_se(streq(data
[2], "MODULE_1=f71882fg"));
595 assert_se(data
[3] == NULL
);
598 static void test_load_env_file_5(void) {
599 _cleanup_strv_free_
char **data
= NULL
;
602 _cleanup_(unlink_tempfilep
) char name
[] = "/tmp/test-load-env-file.XXXXXX";
603 _cleanup_close_
int fd
;
605 fd
= mkostemp_safe(name
);
607 assert_se(write(fd
, env_file_5
, strlen(env_file_5
)) == strlen(env_file_5
));
609 r
= load_env_file(NULL
, name
, &data
);
611 assert_se(streq(data
[0], "a="));
612 assert_se(streq(data
[1], "b="));
613 assert_se(data
[2] == NULL
);
616 static void test_install_printf(void) {
617 char name
[] = "name.service",
618 path
[] = "/run/systemd/system/name.service";
619 UnitFileInstallInfo i
= { .name
= name
, .path
= path
, };
620 UnitFileInstallInfo i2
= { .name
= name
, .path
= path
, };
621 char name3
[] = "name@inst.service",
622 path3
[] = "/run/systemd/system/name.service";
623 UnitFileInstallInfo i3
= { .name
= name3
, .path
= path3
, };
624 UnitFileInstallInfo i4
= { .name
= name3
, .path
= path3
, };
626 _cleanup_free_
char *mid
= NULL
, *bid
= NULL
, *host
= NULL
, *gid
= NULL
, *group
= NULL
, *uid
= NULL
, *user
= NULL
;
628 assert_se(specifier_machine_id('m', NULL
, NULL
, &mid
) >= 0 && mid
);
629 assert_se(specifier_boot_id('b', NULL
, NULL
, &bid
) >= 0 && bid
);
630 assert_se(host
= gethostname_malloc());
631 assert_se(group
= gid_to_name(getgid()));
632 assert_se(asprintf(&gid
, UID_FMT
, getgid()) >= 0);
633 assert_se(user
= uid_to_name(getuid()));
634 assert_se(asprintf(&uid
, UID_FMT
, getuid()) >= 0);
636 #define expect(src, pattern, result) \
638 _cleanup_free_ char *t = NULL; \
639 _cleanup_free_ char \
640 *d1 = strdup(i.name), \
641 *d2 = strdup(i.path); \
642 assert_se(install_full_printf(&src, pattern, &t) >= 0 || !result); \
643 memzero(i.name, strlen(i.name)); \
644 memzero(i.path, strlen(i.path)); \
645 assert_se(d1 && d2); \
648 assert_se(streq(t, result)); \
649 } else assert_se(t == NULL); \
650 strcpy(i.name, d1); \
651 strcpy(i.path, d2); \
654 expect(i
, "%n", "name.service");
655 expect(i
, "%N", "name");
656 expect(i
, "%p", "name");
658 expect(i
, "%j", "name");
659 expect(i
, "%g", group
);
660 expect(i
, "%G", gid
);
661 expect(i
, "%u", user
);
662 expect(i
, "%U", uid
);
664 expect(i
, "%m", mid
);
665 expect(i
, "%b", bid
);
666 expect(i
, "%H", host
);
668 expect(i2
, "%g", group
);
669 expect(i2
, "%G", gid
);
670 expect(i2
, "%u", user
);
671 expect(i2
, "%U", uid
);
673 expect(i3
, "%n", "name@inst.service");
674 expect(i3
, "%N", "name@inst");
675 expect(i3
, "%p", "name");
676 expect(i3
, "%g", group
);
677 expect(i3
, "%G", gid
);
678 expect(i3
, "%u", user
);
679 expect(i3
, "%U", uid
);
681 expect(i3
, "%m", mid
);
682 expect(i3
, "%b", bid
);
683 expect(i3
, "%H", host
);
685 expect(i4
, "%g", group
);
686 expect(i4
, "%G", gid
);
687 expect(i4
, "%u", user
);
688 expect(i4
, "%U", uid
);
691 static uint64_t make_cap(int cap
) {
692 return ((uint64_t) 1ULL << (uint64_t) cap
);
695 static void test_config_parse_capability_set(void) {
696 /* int config_parse_capability_set(
698 const char *filename,
701 unsigned section_line,
708 uint64_t capability_bounding_set
= 0;
710 r
= config_parse_capability_set(NULL
, "fake", 1, "section", 1,
711 "CapabilityBoundingSet", 0, "CAP_NET_RAW",
712 &capability_bounding_set
, NULL
);
714 assert_se(capability_bounding_set
== make_cap(CAP_NET_RAW
));
716 r
= config_parse_capability_set(NULL
, "fake", 1, "section", 1,
717 "CapabilityBoundingSet", 0, "CAP_NET_ADMIN",
718 &capability_bounding_set
, NULL
);
720 assert_se(capability_bounding_set
== (make_cap(CAP_NET_RAW
) | make_cap(CAP_NET_ADMIN
)));
722 r
= config_parse_capability_set(NULL
, "fake", 1, "section", 1,
723 "CapabilityBoundingSet", 0, "~CAP_NET_ADMIN",
724 &capability_bounding_set
, NULL
);
726 assert_se(capability_bounding_set
== make_cap(CAP_NET_RAW
));
728 r
= config_parse_capability_set(NULL
, "fake", 1, "section", 1,
729 "CapabilityBoundingSet", 0, "",
730 &capability_bounding_set
, NULL
);
732 assert_se(capability_bounding_set
== UINT64_C(0));
734 r
= config_parse_capability_set(NULL
, "fake", 1, "section", 1,
735 "CapabilityBoundingSet", 0, "~",
736 &capability_bounding_set
, NULL
);
738 assert_se(cap_test_all(capability_bounding_set
));
740 capability_bounding_set
= 0;
741 r
= config_parse_capability_set(NULL
, "fake", 1, "section", 1,
742 "CapabilityBoundingSet", 0, " 'CAP_NET_RAW' WAT_CAP??? CAP_NET_ADMIN CAP'_trailing_garbage",
743 &capability_bounding_set
, NULL
);
745 assert_se(capability_bounding_set
== (make_cap(CAP_NET_RAW
) | make_cap(CAP_NET_ADMIN
)));
748 static void test_config_parse_rlimit(void) {
749 struct rlimit
* rl
[_RLIMIT_MAX
] = {};
751 assert_se(config_parse_rlimit(NULL
, "fake", 1, "section", 1, "LimitNOFILE", RLIMIT_NOFILE
, "55", rl
, NULL
) >= 0);
752 assert_se(rl
[RLIMIT_NOFILE
]);
753 assert_se(rl
[RLIMIT_NOFILE
]->rlim_cur
== 55);
754 assert_se(rl
[RLIMIT_NOFILE
]->rlim_cur
== rl
[RLIMIT_NOFILE
]->rlim_max
);
756 assert_se(config_parse_rlimit(NULL
, "fake", 1, "section", 1, "LimitNOFILE", RLIMIT_NOFILE
, "55:66", rl
, NULL
) >= 0);
757 assert_se(rl
[RLIMIT_NOFILE
]);
758 assert_se(rl
[RLIMIT_NOFILE
]->rlim_cur
== 55);
759 assert_se(rl
[RLIMIT_NOFILE
]->rlim_max
== 66);
761 assert_se(config_parse_rlimit(NULL
, "fake", 1, "section", 1, "LimitNOFILE", RLIMIT_NOFILE
, "infinity", rl
, NULL
) >= 0);
762 assert_se(rl
[RLIMIT_NOFILE
]);
763 assert_se(rl
[RLIMIT_NOFILE
]->rlim_cur
== RLIM_INFINITY
);
764 assert_se(rl
[RLIMIT_NOFILE
]->rlim_cur
== rl
[RLIMIT_NOFILE
]->rlim_max
);
766 assert_se(config_parse_rlimit(NULL
, "fake", 1, "section", 1, "LimitNOFILE", RLIMIT_NOFILE
, "infinity:infinity", rl
, NULL
) >= 0);
767 assert_se(rl
[RLIMIT_NOFILE
]);
768 assert_se(rl
[RLIMIT_NOFILE
]->rlim_cur
== RLIM_INFINITY
);
769 assert_se(rl
[RLIMIT_NOFILE
]->rlim_cur
== rl
[RLIMIT_NOFILE
]->rlim_max
);
771 rl
[RLIMIT_NOFILE
]->rlim_cur
= 10;
772 rl
[RLIMIT_NOFILE
]->rlim_max
= 20;
774 /* Invalid values don't change rl */
775 assert_se(config_parse_rlimit(NULL
, "fake", 1, "section", 1, "LimitNOFILE", RLIMIT_NOFILE
, "10:20:30", rl
, NULL
) >= 0);
776 assert_se(rl
[RLIMIT_NOFILE
]);
777 assert_se(rl
[RLIMIT_NOFILE
]->rlim_cur
== 10);
778 assert_se(rl
[RLIMIT_NOFILE
]->rlim_max
== 20);
780 assert_se(config_parse_rlimit(NULL
, "fake", 1, "section", 1, "LimitNOFILE", RLIMIT_NOFILE
, "wat:wat", rl
, NULL
) >= 0);
781 assert_se(rl
[RLIMIT_NOFILE
]);
782 assert_se(rl
[RLIMIT_NOFILE
]->rlim_cur
== 10);
783 assert_se(rl
[RLIMIT_NOFILE
]->rlim_max
== 20);
785 assert_se(config_parse_rlimit(NULL
, "fake", 1, "section", 1, "LimitNOFILE", RLIMIT_NOFILE
, "66:wat", rl
, NULL
) >= 0);
786 assert_se(rl
[RLIMIT_NOFILE
]);
787 assert_se(rl
[RLIMIT_NOFILE
]->rlim_cur
== 10);
788 assert_se(rl
[RLIMIT_NOFILE
]->rlim_max
== 20);
790 assert_se(config_parse_rlimit(NULL
, "fake", 1, "section", 1, "LimitNOFILE", RLIMIT_NOFILE
, "200:100", rl
, NULL
) >= 0);
791 assert_se(rl
[RLIMIT_NOFILE
]);
792 assert_se(rl
[RLIMIT_NOFILE
]->rlim_cur
== 10);
793 assert_se(rl
[RLIMIT_NOFILE
]->rlim_max
== 20);
795 rl
[RLIMIT_NOFILE
] = mfree(rl
[RLIMIT_NOFILE
]);
797 assert_se(config_parse_rlimit(NULL
, "fake", 1, "section", 1, "LimitCPU", RLIMIT_CPU
, "56", rl
, NULL
) >= 0);
798 assert_se(rl
[RLIMIT_CPU
]);
799 assert_se(rl
[RLIMIT_CPU
]->rlim_cur
== 56);
800 assert_se(rl
[RLIMIT_CPU
]->rlim_cur
== rl
[RLIMIT_CPU
]->rlim_max
);
802 assert_se(config_parse_rlimit(NULL
, "fake", 1, "section", 1, "LimitCPU", RLIMIT_CPU
, "57s", rl
, NULL
) >= 0);
803 assert_se(rl
[RLIMIT_CPU
]);
804 assert_se(rl
[RLIMIT_CPU
]->rlim_cur
== 57);
805 assert_se(rl
[RLIMIT_CPU
]->rlim_cur
== rl
[RLIMIT_CPU
]->rlim_max
);
807 assert_se(config_parse_rlimit(NULL
, "fake", 1, "section", 1, "LimitCPU", RLIMIT_CPU
, "40s:1m", rl
, NULL
) >= 0);
808 assert_se(rl
[RLIMIT_CPU
]);
809 assert_se(rl
[RLIMIT_CPU
]->rlim_cur
== 40);
810 assert_se(rl
[RLIMIT_CPU
]->rlim_max
== 60);
812 assert_se(config_parse_rlimit(NULL
, "fake", 1, "section", 1, "LimitCPU", RLIMIT_CPU
, "infinity", rl
, NULL
) >= 0);
813 assert_se(rl
[RLIMIT_CPU
]);
814 assert_se(rl
[RLIMIT_CPU
]->rlim_cur
== RLIM_INFINITY
);
815 assert_se(rl
[RLIMIT_CPU
]->rlim_cur
== rl
[RLIMIT_CPU
]->rlim_max
);
817 assert_se(config_parse_rlimit(NULL
, "fake", 1, "section", 1, "LimitCPU", RLIMIT_CPU
, "1234ms", rl
, NULL
) >= 0);
818 assert_se(rl
[RLIMIT_CPU
]);
819 assert_se(rl
[RLIMIT_CPU
]->rlim_cur
== 2);
820 assert_se(rl
[RLIMIT_CPU
]->rlim_cur
== rl
[RLIMIT_CPU
]->rlim_max
);
822 rl
[RLIMIT_CPU
] = mfree(rl
[RLIMIT_CPU
]);
824 assert_se(config_parse_rlimit(NULL
, "fake", 1, "section", 1, "LimitRTTIME", RLIMIT_RTTIME
, "58", rl
, NULL
) >= 0);
825 assert_se(rl
[RLIMIT_RTTIME
]);
826 assert_se(rl
[RLIMIT_RTTIME
]->rlim_cur
== 58);
827 assert_se(rl
[RLIMIT_RTTIME
]->rlim_cur
== rl
[RLIMIT_RTTIME
]->rlim_max
);
829 assert_se(config_parse_rlimit(NULL
, "fake", 1, "section", 1, "LimitRTTIME", RLIMIT_RTTIME
, "58:60", rl
, NULL
) >= 0);
830 assert_se(rl
[RLIMIT_RTTIME
]);
831 assert_se(rl
[RLIMIT_RTTIME
]->rlim_cur
== 58);
832 assert_se(rl
[RLIMIT_RTTIME
]->rlim_max
== 60);
834 assert_se(config_parse_rlimit(NULL
, "fake", 1, "section", 1, "LimitRTTIME", RLIMIT_RTTIME
, "59s", rl
, NULL
) >= 0);
835 assert_se(rl
[RLIMIT_RTTIME
]);
836 assert_se(rl
[RLIMIT_RTTIME
]->rlim_cur
== 59 * USEC_PER_SEC
);
837 assert_se(rl
[RLIMIT_RTTIME
]->rlim_cur
== rl
[RLIMIT_RTTIME
]->rlim_max
);
839 assert_se(config_parse_rlimit(NULL
, "fake", 1, "section", 1, "LimitRTTIME", RLIMIT_RTTIME
, "59s:123s", rl
, NULL
) >= 0);
840 assert_se(rl
[RLIMIT_RTTIME
]);
841 assert_se(rl
[RLIMIT_RTTIME
]->rlim_cur
== 59 * USEC_PER_SEC
);
842 assert_se(rl
[RLIMIT_RTTIME
]->rlim_max
== 123 * USEC_PER_SEC
);
844 assert_se(config_parse_rlimit(NULL
, "fake", 1, "section", 1, "LimitRTTIME", RLIMIT_RTTIME
, "infinity", rl
, NULL
) >= 0);
845 assert_se(rl
[RLIMIT_RTTIME
]);
846 assert_se(rl
[RLIMIT_RTTIME
]->rlim_cur
== RLIM_INFINITY
);
847 assert_se(rl
[RLIMIT_RTTIME
]->rlim_cur
== rl
[RLIMIT_RTTIME
]->rlim_max
);
849 assert_se(config_parse_rlimit(NULL
, "fake", 1, "section", 1, "LimitRTTIME", RLIMIT_RTTIME
, "infinity:infinity", rl
, NULL
) >= 0);
850 assert_se(rl
[RLIMIT_RTTIME
]);
851 assert_se(rl
[RLIMIT_RTTIME
]->rlim_cur
== RLIM_INFINITY
);
852 assert_se(rl
[RLIMIT_RTTIME
]->rlim_cur
== rl
[RLIMIT_RTTIME
]->rlim_max
);
854 assert_se(config_parse_rlimit(NULL
, "fake", 1, "section", 1, "LimitRTTIME", RLIMIT_RTTIME
, "2345ms", rl
, NULL
) >= 0);
855 assert_se(rl
[RLIMIT_RTTIME
]);
856 assert_se(rl
[RLIMIT_RTTIME
]->rlim_cur
== 2345 * USEC_PER_MSEC
);
857 assert_se(rl
[RLIMIT_RTTIME
]->rlim_cur
== rl
[RLIMIT_RTTIME
]->rlim_max
);
859 rl
[RLIMIT_RTTIME
] = mfree(rl
[RLIMIT_RTTIME
]);
862 static void test_config_parse_pass_environ(void) {
863 /* int config_parse_pass_environ(
865 const char *filename,
868 unsigned section_line,
875 _cleanup_strv_free_
char **passenv
= NULL
;
877 r
= config_parse_pass_environ(NULL
, "fake", 1, "section", 1,
878 "PassEnvironment", 0, "A B",
881 assert_se(strv_length(passenv
) == 2);
882 assert_se(streq(passenv
[0], "A"));
883 assert_se(streq(passenv
[1], "B"));
885 r
= config_parse_pass_environ(NULL
, "fake", 1, "section", 1,
886 "PassEnvironment", 0, "",
889 assert_se(strv_isempty(passenv
));
891 r
= config_parse_pass_environ(NULL
, "fake", 1, "section", 1,
892 "PassEnvironment", 0, "'invalid name' 'normal_name' A=1 \\",
895 assert_se(strv_length(passenv
) == 1);
896 assert_se(streq(passenv
[0], "normal_name"));
900 static void test_unit_dump_config_items(void) {
901 unit_dump_config_items(stdout
);
904 int main(int argc
, char *argv
[]) {
905 _cleanup_(rm_rf_physical_and_freep
) char *runtime_dir
= NULL
;
908 test_setup_logging(LOG_INFO
);
910 r
= enter_cgroup_subroot();
912 return log_tests_skipped("cgroupfs not available");
914 assert_se(runtime_dir
= setup_fake_runtime_dir());
916 r
= test_unit_file_get_set();
917 test_config_parse_exec();
918 test_config_parse_log_extra_fields();
919 test_config_parse_capability_set();
920 test_config_parse_rlimit();
921 test_config_parse_pass_environ();
922 test_load_env_file_1();
923 test_load_env_file_2();
924 test_load_env_file_3();
925 test_load_env_file_4();
926 test_load_env_file_5();
927 TEST_REQ_RUNNING_SYSTEMD(test_install_printf());
928 test_unit_dump_config_items();