1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright 2012 Lennart Poettering
4 Copyright 2013 Zbigniew Jędrzejewski-Szmek
11 #include <sys/capability.h>
14 #include "alloc-util.h"
15 #include "all-units.h"
16 #include "capability-util.h"
17 #include "conf-parser.h"
22 #include "hostname-util.h"
23 #include "install-printf.h"
25 #include "load-fragment.h"
28 #include "specifier.h"
29 #include "string-util.h"
31 #include "test-helper.h"
33 #include "user-util.h"
36 static int test_unit_file_get_set(void) {
42 h
= hashmap_new(&string_hash_ops
);
45 r
= unit_file_get_list(UNIT_FILE_SYSTEM
, NULL
, h
, NULL
, NULL
);
47 if (IN_SET(r
, -EPERM
, -EACCES
)) {
48 log_notice_errno(r
, "Skipping test: unit_file_get_list: %m");
49 return EXIT_TEST_SKIP
;
52 log_full_errno(r
== 0 ? LOG_INFO
: LOG_ERR
, r
,
53 "unit_file_get_list: %m");
57 HASHMAP_FOREACH(p
, h
, i
)
58 printf("%s = %s\n", p
->path
, unit_file_state_to_string(p
->state
));
60 unit_file_list_free(h
);
65 static void check_execcommand(ExecCommand
*c
,
74 log_info("expect: \"%s\" [\"%s\" \"%s\" \"%s\"]",
75 path
, argv0
?: path
, argv1
, argv2
);
76 n
= strv_length(c
->argv
);
77 log_info("actual: \"%s\" [\"%s\" \"%s\" \"%s\"]",
78 c
->path
, c
->argv
[0], n
> 0 ? c
->argv
[1] : NULL
, n
> 1 ? c
->argv
[2] : NULL
);
79 assert_se(streq(c
->path
, path
));
80 assert_se(streq(c
->argv
[0], argv0
?: path
));
82 assert_se(streq_ptr(c
->argv
[1], argv1
));
84 assert_se(streq_ptr(c
->argv
[2], argv2
));
85 assert_se(!!(c
->flags
& EXEC_COMMAND_IGNORE_FAILURE
) == ignore
);
88 static void test_config_parse_exec(void) {
89 /* int config_parse_exec(
94 unsigned section_line,
102 ExecCommand
*c
= NULL
, *c1
;
104 _cleanup_(manager_freep
) Manager
*m
= NULL
;
105 _cleanup_(unit_freep
) Unit
*u
= NULL
;
107 r
= manager_new(UNIT_FILE_USER
, MANAGER_TEST_RUN_MINIMAL
, &m
);
108 if (MANAGER_SKIP_TEST(r
)) {
109 log_notice_errno(r
, "Skipping test: manager_new: %m");
114 assert_se(manager_startup(m
, NULL
, NULL
) >= 0);
116 assert_se(u
= unit_new(m
, sizeof(Service
)));
118 log_info("/* basic test */");
119 r
= config_parse_exec(NULL
, "fake", 1, "section", 1,
120 "LValue", 0, "/RValue r1",
123 check_execcommand(c
, "/RValue", "/RValue", "r1", NULL
, false);
125 r
= config_parse_exec(NULL
, "fake", 2, "section", 1,
126 "LValue", 0, "/RValue///slashes r1///",
129 log_info("/* test slashes */");
131 c1
= c
->command_next
;
132 check_execcommand(c1
, "/RValue/slashes", "/RValue///slashes", "r1///", NULL
, false);
134 log_info("/* trailing slash */");
135 r
= config_parse_exec(NULL
, "fake", 4, "section", 1,
136 "LValue", 0, "/RValue/ argv0 r1",
138 assert_se(r
== -ENOEXEC
);
139 assert_se(c1
->command_next
== NULL
);
141 log_info("/* honour_argv0 */");
142 r
= config_parse_exec(NULL
, "fake", 3, "section", 1,
143 "LValue", 0, "@/RValue///slashes2 ///argv0 r1",
146 c1
= c1
->command_next
;
147 check_execcommand(c1
, "/RValue/slashes2", "///argv0", "r1", NULL
, false);
149 log_info("/* honour_argv0, no args */");
150 r
= config_parse_exec(NULL
, "fake", 3, "section", 1,
151 "LValue", 0, "@/RValue",
153 assert_se(r
== -ENOEXEC
);
154 assert_se(c1
->command_next
== NULL
);
156 log_info("/* no command, whitespace only, reset */");
157 r
= config_parse_exec(NULL
, "fake", 3, "section", 1,
161 assert_se(c
== NULL
);
163 log_info("/* ignore && honour_argv0 */");
164 r
= config_parse_exec(NULL
, "fake", 4, "section", 1,
165 "LValue", 0, "-@/RValue///slashes3 argv0a r1",
169 check_execcommand(c1
, "/RValue/slashes3", "argv0a", "r1", NULL
, true);
171 log_info("/* ignore && honour_argv0 */");
172 r
= config_parse_exec(NULL
, "fake", 4, "section", 1,
173 "LValue", 0, "@-/RValue///slashes4 argv0b r1",
176 c1
= c1
->command_next
;
177 check_execcommand(c1
, "/RValue/slashes4", "argv0b", "r1", NULL
, true);
179 log_info("/* ignore && ignore */");
180 r
= config_parse_exec(NULL
, "fake", 4, "section", 1,
181 "LValue", 0, "--/RValue argv0 r1",
184 assert_se(c1
->command_next
== NULL
);
186 log_info("/* ignore && ignore (2) */");
187 r
= config_parse_exec(NULL
, "fake", 4, "section", 1,
188 "LValue", 0, "-@-/RValue argv0 r1",
191 assert_se(c1
->command_next
== NULL
);
193 log_info("/* semicolon */");
194 r
= config_parse_exec(NULL
, "fake", 5, "section", 1,
196 "-@/RValue argv0 r1 ; "
200 c1
= c1
->command_next
;
201 check_execcommand(c1
, "/RValue", "argv0", "r1", NULL
, true);
203 c1
= c1
->command_next
;
204 check_execcommand(c1
, "/goo/goo", NULL
, "boo", NULL
, false);
206 log_info("/* two semicolons in a row */");
207 r
= config_parse_exec(NULL
, "fake", 5, "section", 1,
209 "-@/RValue argv0 r1 ; ; "
212 assert_se(r
== -ENOEXEC
);
213 c1
= c1
->command_next
;
214 check_execcommand(c1
, "/RValue", "argv0", "r1", NULL
, true);
216 /* second command fails because the executable name is ";" */
217 assert_se(c1
->command_next
== NULL
);
219 log_info("/* trailing semicolon */");
220 r
= config_parse_exec(NULL
, "fake", 5, "section", 1,
222 "-@/RValue argv0 r1 ; ",
225 c1
= c1
->command_next
;
226 check_execcommand(c1
, "/RValue", "argv0", "r1", NULL
, true);
228 assert_se(c1
->command_next
== NULL
);
230 log_info("/* trailing semicolon, no whitespace */");
231 r
= config_parse_exec(NULL
, "fake", 5, "section", 1,
233 "-@/RValue argv0 r1 ;",
236 c1
= c1
->command_next
;
237 check_execcommand(c1
, "/RValue", "argv0", "r1", NULL
, true);
239 assert_se(c1
->command_next
== NULL
);
241 log_info("/* trailing semicolon in single quotes */");
242 r
= config_parse_exec(NULL
, "fake", 5, "section", 1,
244 "-@/RValue argv0 r1 ';'",
247 c1
= c1
->command_next
;
248 check_execcommand(c1
, "/RValue", "argv0", "r1", ";", true);
250 log_info("/* escaped semicolon */");
251 r
= config_parse_exec(NULL
, "fake", 5, "section", 1,
256 c1
= c1
->command_next
;
257 check_execcommand(c1
, "/bin/find", NULL
, ";", NULL
, false);
259 log_info("/* escaped semicolon with following arg */");
260 r
= config_parse_exec(NULL
, "fake", 5, "section", 1,
265 c1
= c1
->command_next
;
266 check_execcommand(c1
,
267 "/sbin/find", NULL
, ";", "/x", false);
269 log_info("/* escaped semicolon as part of an expression */");
270 r
= config_parse_exec(NULL
, "fake", 5, "section", 1,
275 c1
= c1
->command_next
;
276 check_execcommand(c1
,
277 "/sbin/find", NULL
, "\\;x", NULL
, false);
279 log_info("/* encoded semicolon */");
280 r
= config_parse_exec(NULL
, "fake", 5, "section", 1,
285 c1
= c1
->command_next
;
286 check_execcommand(c1
, "/bin/find", NULL
, ";", NULL
, false);
288 log_info("/* quoted semicolon */");
289 r
= config_parse_exec(NULL
, "fake", 5, "section", 1,
294 c1
= c1
->command_next
;
295 check_execcommand(c1
, "/bin/find", NULL
, ";", NULL
, false);
297 log_info("/* quoted semicolon with following arg */");
298 r
= config_parse_exec(NULL
, "fake", 5, "section", 1,
300 "/sbin/find \";\" /x",
303 c1
= c1
->command_next
;
304 check_execcommand(c1
,
305 "/sbin/find", NULL
, ";", "/x", false);
307 log_info("/* spaces in the filename */");
308 r
= config_parse_exec(NULL
, "fake", 5, "section", 1,
310 "\"/PATH WITH SPACES/daemon\" -1 -2",
313 c1
= c1
->command_next
;
314 check_execcommand(c1
,
315 "/PATH WITH SPACES/daemon", NULL
, "-1", "-2", false);
317 log_info("/* spaces in the filename, no args */");
318 r
= config_parse_exec(NULL
, "fake", 5, "section", 1,
320 "\"/PATH WITH SPACES/daemon -1 -2\"",
323 c1
= c1
->command_next
;
324 check_execcommand(c1
,
325 "/PATH WITH SPACES/daemon -1 -2", NULL
, NULL
, NULL
, false);
327 log_info("/* spaces in the filename, everything quoted */");
328 r
= config_parse_exec(NULL
, "fake", 5, "section", 1,
330 "\"/PATH WITH SPACES/daemon\" \"-1\" '-2'",
333 c1
= c1
->command_next
;
334 check_execcommand(c1
,
335 "/PATH WITH SPACES/daemon", NULL
, "-1", "-2", false);
337 log_info("/* escaped spaces in the filename */");
338 r
= config_parse_exec(NULL
, "fake", 5, "section", 1,
340 "\"/PATH\\sWITH\\sSPACES/daemon\" '-1 -2'",
343 c1
= c1
->command_next
;
344 check_execcommand(c1
,
345 "/PATH WITH SPACES/daemon", NULL
, "-1 -2", NULL
, false);
347 log_info("/* escaped spaces in the filename (2) */");
348 r
= config_parse_exec(NULL
, "fake", 5, "section", 1,
350 "\"/PATH\\x20WITH\\x20SPACES/daemon\" \"-1 -2\"",
353 c1
= c1
->command_next
;
354 check_execcommand(c1
,
355 "/PATH WITH SPACES/daemon", NULL
, "-1 -2", NULL
, false);
357 for (ccc
= "abfnrtv\\\'\"x"; *ccc
; ccc
++) {
358 /* \\x is an incomplete hexadecimal sequence, invalid because of the slash */
359 char path
[] = "/path\\X";
360 path
[sizeof(path
) - 2] = *ccc
;
362 log_info("/* invalid character: \\%c */", *ccc
);
363 r
= config_parse_exec(NULL
, "fake", 4, "section", 1,
366 assert_se(r
== -ENOEXEC
);
367 assert_se(c1
->command_next
== NULL
);
370 log_info("/* valid character: \\s */");
371 r
= config_parse_exec(NULL
, "fake", 4, "section", 1,
372 "LValue", 0, "/path\\s",
375 c1
= c1
->command_next
;
376 check_execcommand(c1
, "/path ", NULL
, NULL
, NULL
, false);
378 log_info("/* quoted backslashes */");
379 r
= config_parse_exec(NULL
, "fake", 5, "section", 1,
381 "/bin/grep '\\w+\\K'",
384 c1
= c1
->command_next
;
385 check_execcommand(c1
, "/bin/grep", NULL
, "\\w+\\K", NULL
, false);
387 log_info("/* trailing backslash: \\ */");
388 /* backslash is invalid */
389 r
= config_parse_exec(NULL
, "fake", 4, "section", 1,
390 "LValue", 0, "/path\\",
392 assert_se(r
== -ENOEXEC
);
393 assert_se(c1
->command_next
== NULL
);
395 log_info("/* missing ending ' */");
396 r
= config_parse_exec(NULL
, "fake", 4, "section", 1,
397 "LValue", 0, "/path 'foo",
399 assert_se(r
== -ENOEXEC
);
400 assert_se(c1
->command_next
== NULL
);
402 log_info("/* missing ending ' with trailing backslash */");
403 r
= config_parse_exec(NULL
, "fake", 4, "section", 1,
404 "LValue", 0, "/path 'foo\\",
406 assert_se(r
== -ENOEXEC
);
407 assert_se(c1
->command_next
== NULL
);
409 log_info("/* invalid space between modifiers */");
410 r
= config_parse_exec(NULL
, "fake", 4, "section", 1,
411 "LValue", 0, "- /path",
414 assert_se(c1
->command_next
== NULL
);
416 log_info("/* only modifiers, no path */");
417 r
= config_parse_exec(NULL
, "fake", 4, "section", 1,
421 assert_se(c1
->command_next
== NULL
);
423 log_info("/* empty argument, reset */");
424 r
= config_parse_exec(NULL
, "fake", 4, "section", 1,
428 assert_se(c
== NULL
);
430 exec_command_free_list(c
);
433 static void test_config_parse_log_extra_fields(void) {
434 /* int config_parse_log_extra_fields(
436 const char *filename,
439 unsigned section_line,
448 _cleanup_(manager_freep
) Manager
*m
= NULL
;
449 _cleanup_(unit_freep
) Unit
*u
= NULL
;
452 r
= manager_new(UNIT_FILE_USER
, MANAGER_TEST_RUN_MINIMAL
, &m
);
453 if (MANAGER_SKIP_TEST(r
)) {
454 log_notice_errno(r
, "Skipping test: manager_new: %m");
459 assert_se(manager_startup(m
, NULL
, NULL
) >= 0);
461 assert_se(u
= unit_new(m
, sizeof(Service
)));
463 log_info("/* %s – basic test */", __func__
);
464 r
= config_parse_log_extra_fields(NULL
, "fake", 1, "section", 1,
465 "LValue", 0, "FOO=BAR \"QOOF=quux ' ' \"",
468 assert_se(c
.n_log_extra_fields
== 2);
469 assert_se(strneq(c
.log_extra_fields
[0].iov_base
, "FOO=BAR", c
.log_extra_fields
[0].iov_len
));
470 assert_se(strneq(c
.log_extra_fields
[1].iov_base
, "QOOF=quux ' ' ", c
.log_extra_fields
[1].iov_len
));
472 log_info("/* %s – add some */", __func__
);
473 r
= config_parse_log_extra_fields(NULL
, "fake", 1, "section", 1,
474 "LValue", 0, "FOO2=BAR2 QOOF2=quux ' '",
477 assert_se(c
.n_log_extra_fields
== 4);
478 assert_se(strneq(c
.log_extra_fields
[0].iov_base
, "FOO=BAR", c
.log_extra_fields
[0].iov_len
));
479 assert_se(strneq(c
.log_extra_fields
[1].iov_base
, "QOOF=quux ' ' ", c
.log_extra_fields
[1].iov_len
));
480 assert_se(strneq(c
.log_extra_fields
[2].iov_base
, "FOO2=BAR2", c
.log_extra_fields
[2].iov_len
));
481 assert_se(strneq(c
.log_extra_fields
[3].iov_base
, "QOOF2=quux", c
.log_extra_fields
[3].iov_len
));
483 exec_context_dump(&c
, stdout
, " --> ");
485 log_info("/* %s – reset */", __func__
);
486 r
= config_parse_log_extra_fields(NULL
, "fake", 1, "section", 1,
490 assert_se(c
.n_log_extra_fields
== 0);
492 exec_context_free_log_extra_fields(&c
);
494 log_info("/* %s – bye */", __func__
);
512 "#SPAMD_ARGS=\"-d --socketpath=/var/lib/bulwark/spamd \\\n" \
513 "#--nouser-config \\\n" \
519 "HWMON_MODULES=\"coretemp f71882fg\"\n" \
521 "# For compatibility reasons\n" \
523 "MODULE_0=coretemp\n" \
530 static void test_load_env_file_1(void) {
531 _cleanup_strv_free_
char **data
= NULL
;
534 _cleanup_(unlink_tempfilep
) char name
[] = "/tmp/test-load-env-file.XXXXXX";
535 _cleanup_close_
int fd
;
537 fd
= mkostemp_safe(name
);
539 assert_se(write(fd
, env_file_1
, sizeof(env_file_1
)) == sizeof(env_file_1
));
541 r
= load_env_file(NULL
, name
, NULL
, &data
);
543 assert_se(streq(data
[0], "a=a"));
544 assert_se(streq(data
[1], "b=bc"));
545 assert_se(streq(data
[2], "d=def"));
546 assert_se(streq(data
[3], "g=g "));
547 assert_se(streq(data
[4], "h=h"));
548 assert_se(streq(data
[5], "i=i"));
549 assert_se(data
[6] == NULL
);
552 static void test_load_env_file_2(void) {
553 _cleanup_strv_free_
char **data
= NULL
;
556 _cleanup_(unlink_tempfilep
) char name
[] = "/tmp/test-load-env-file.XXXXXX";
557 _cleanup_close_
int fd
;
559 fd
= mkostemp_safe(name
);
561 assert_se(write(fd
, env_file_2
, sizeof(env_file_2
)) == sizeof(env_file_2
));
563 r
= load_env_file(NULL
, name
, NULL
, &data
);
565 assert_se(streq(data
[0], "a=a"));
566 assert_se(data
[1] == NULL
);
569 static void test_load_env_file_3(void) {
570 _cleanup_strv_free_
char **data
= NULL
;
573 _cleanup_(unlink_tempfilep
) char name
[] = "/tmp/test-load-env-file.XXXXXX";
574 _cleanup_close_
int fd
;
576 fd
= mkostemp_safe(name
);
578 assert_se(write(fd
, env_file_3
, sizeof(env_file_3
)) == sizeof(env_file_3
));
580 r
= load_env_file(NULL
, name
, NULL
, &data
);
582 assert_se(data
== NULL
);
585 static void test_load_env_file_4(void) {
586 _cleanup_strv_free_
char **data
= NULL
;
587 _cleanup_(unlink_tempfilep
) char name
[] = "/tmp/test-load-env-file.XXXXXX";
588 _cleanup_close_
int fd
;
591 fd
= mkostemp_safe(name
);
593 assert_se(write(fd
, env_file_4
, sizeof(env_file_4
)) == sizeof(env_file_4
));
595 r
= load_env_file(NULL
, name
, NULL
, &data
);
597 assert_se(streq(data
[0], "HWMON_MODULES=coretemp f71882fg"));
598 assert_se(streq(data
[1], "MODULE_0=coretemp"));
599 assert_se(streq(data
[2], "MODULE_1=f71882fg"));
600 assert_se(data
[3] == NULL
);
603 static void test_load_env_file_5(void) {
604 _cleanup_strv_free_
char **data
= NULL
;
607 _cleanup_(unlink_tempfilep
) char name
[] = "/tmp/test-load-env-file.XXXXXX";
608 _cleanup_close_
int fd
;
610 fd
= mkostemp_safe(name
);
612 assert_se(write(fd
, env_file_5
, sizeof(env_file_5
)) == sizeof(env_file_5
));
614 r
= load_env_file(NULL
, name
, NULL
, &data
);
616 assert_se(streq(data
[0], "a="));
617 assert_se(streq(data
[1], "b="));
618 assert_se(data
[2] == NULL
);
621 static void test_install_printf(void) {
622 char name
[] = "name.service",
623 path
[] = "/run/systemd/system/name.service";
624 UnitFileInstallInfo i
= { .name
= name
, .path
= path
, };
625 UnitFileInstallInfo i2
= { .name
= name
, .path
= path
, };
626 char name3
[] = "name@inst.service",
627 path3
[] = "/run/systemd/system/name.service";
628 UnitFileInstallInfo i3
= { .name
= name3
, .path
= path3
, };
629 UnitFileInstallInfo i4
= { .name
= name3
, .path
= path3
, };
631 _cleanup_free_
char *mid
= NULL
, *bid
= NULL
, *host
= NULL
, *uid
= NULL
, *user
= NULL
;
633 assert_se(specifier_machine_id('m', NULL
, NULL
, &mid
) >= 0 && mid
);
634 assert_se(specifier_boot_id('b', NULL
, NULL
, &bid
) >= 0 && bid
);
635 assert_se(host
= gethostname_malloc());
636 assert_se(user
= uid_to_name(getuid()));
637 assert_se(asprintf(&uid
, UID_FMT
, getuid()) >= 0);
639 #define expect(src, pattern, result) \
641 _cleanup_free_ char *t = NULL; \
642 _cleanup_free_ char \
643 *d1 = strdup(i.name), \
644 *d2 = strdup(i.path); \
645 assert_se(install_full_printf(&src, pattern, &t) >= 0 || !result); \
646 memzero(i.name, strlen(i.name)); \
647 memzero(i.path, strlen(i.path)); \
648 assert_se(d1 && d2); \
651 assert_se(streq(t, result)); \
652 } else assert_se(t == NULL); \
653 strcpy(i.name, d1); \
654 strcpy(i.path, d2); \
657 expect(i
, "%n", "name.service");
658 expect(i
, "%N", "name");
659 expect(i
, "%p", "name");
661 expect(i
, "%j", "name");
662 expect(i
, "%u", user
);
663 expect(i
, "%U", uid
);
665 expect(i
, "%m", mid
);
666 expect(i
, "%b", bid
);
667 expect(i
, "%H", host
);
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
, "%u", user
);
676 expect(i3
, "%U", uid
);
678 expect(i3
, "%m", mid
);
679 expect(i3
, "%b", bid
);
680 expect(i3
, "%H", host
);
682 expect(i4
, "%u", user
);
683 expect(i4
, "%U", uid
);
686 static uint64_t make_cap(int cap
) {
687 return ((uint64_t) 1ULL << (uint64_t) cap
);
690 static void test_config_parse_capability_set(void) {
691 /* int config_parse_capability_set(
693 const char *filename,
696 unsigned section_line,
703 uint64_t capability_bounding_set
= 0;
705 r
= config_parse_capability_set(NULL
, "fake", 1, "section", 1,
706 "CapabilityBoundingSet", 0, "CAP_NET_RAW",
707 &capability_bounding_set
, NULL
);
709 assert_se(capability_bounding_set
== make_cap(CAP_NET_RAW
));
711 r
= config_parse_capability_set(NULL
, "fake", 1, "section", 1,
712 "CapabilityBoundingSet", 0, "CAP_NET_ADMIN",
713 &capability_bounding_set
, NULL
);
715 assert_se(capability_bounding_set
== (make_cap(CAP_NET_RAW
) | make_cap(CAP_NET_ADMIN
)));
717 r
= config_parse_capability_set(NULL
, "fake", 1, "section", 1,
718 "CapabilityBoundingSet", 0, "~CAP_NET_ADMIN",
719 &capability_bounding_set
, NULL
);
721 assert_se(capability_bounding_set
== make_cap(CAP_NET_RAW
));
723 r
= config_parse_capability_set(NULL
, "fake", 1, "section", 1,
724 "CapabilityBoundingSet", 0, "",
725 &capability_bounding_set
, NULL
);
727 assert_se(capability_bounding_set
== UINT64_C(0));
729 r
= config_parse_capability_set(NULL
, "fake", 1, "section", 1,
730 "CapabilityBoundingSet", 0, "~",
731 &capability_bounding_set
, NULL
);
733 assert_se(cap_test_all(capability_bounding_set
));
735 capability_bounding_set
= 0;
736 r
= config_parse_capability_set(NULL
, "fake", 1, "section", 1,
737 "CapabilityBoundingSet", 0, " 'CAP_NET_RAW' WAT_CAP??? CAP_NET_ADMIN CAP'_trailing_garbage",
738 &capability_bounding_set
, NULL
);
740 assert_se(capability_bounding_set
== (make_cap(CAP_NET_RAW
) | make_cap(CAP_NET_ADMIN
)));
743 static void test_config_parse_rlimit(void) {
744 struct rlimit
* rl
[_RLIMIT_MAX
] = {};
746 assert_se(config_parse_rlimit(NULL
, "fake", 1, "section", 1, "LimitNOFILE", RLIMIT_NOFILE
, "55", rl
, NULL
) >= 0);
747 assert_se(rl
[RLIMIT_NOFILE
]);
748 assert_se(rl
[RLIMIT_NOFILE
]->rlim_cur
== 55);
749 assert_se(rl
[RLIMIT_NOFILE
]->rlim_cur
== rl
[RLIMIT_NOFILE
]->rlim_max
);
751 assert_se(config_parse_rlimit(NULL
, "fake", 1, "section", 1, "LimitNOFILE", RLIMIT_NOFILE
, "55:66", 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_max
== 66);
756 assert_se(config_parse_rlimit(NULL
, "fake", 1, "section", 1, "LimitNOFILE", RLIMIT_NOFILE
, "infinity", rl
, NULL
) >= 0);
757 assert_se(rl
[RLIMIT_NOFILE
]);
758 assert_se(rl
[RLIMIT_NOFILE
]->rlim_cur
== RLIM_INFINITY
);
759 assert_se(rl
[RLIMIT_NOFILE
]->rlim_cur
== rl
[RLIMIT_NOFILE
]->rlim_max
);
761 assert_se(config_parse_rlimit(NULL
, "fake", 1, "section", 1, "LimitNOFILE", RLIMIT_NOFILE
, "infinity: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 rl
[RLIMIT_NOFILE
]->rlim_cur
= 10;
767 rl
[RLIMIT_NOFILE
]->rlim_max
= 20;
769 /* Invalid values don't change rl */
770 assert_se(config_parse_rlimit(NULL
, "fake", 1, "section", 1, "LimitNOFILE", RLIMIT_NOFILE
, "10:20:30", rl
, NULL
) >= 0);
771 assert_se(rl
[RLIMIT_NOFILE
]);
772 assert_se(rl
[RLIMIT_NOFILE
]->rlim_cur
== 10);
773 assert_se(rl
[RLIMIT_NOFILE
]->rlim_max
== 20);
775 assert_se(config_parse_rlimit(NULL
, "fake", 1, "section", 1, "LimitNOFILE", RLIMIT_NOFILE
, "wat:wat", 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
, "66: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
, "200:100", 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 rl
[RLIMIT_NOFILE
] = mfree(rl
[RLIMIT_NOFILE
]);
792 assert_se(config_parse_rlimit(NULL
, "fake", 1, "section", 1, "LimitCPU", RLIMIT_CPU
, "56", rl
, NULL
) >= 0);
793 assert_se(rl
[RLIMIT_CPU
]);
794 assert_se(rl
[RLIMIT_CPU
]->rlim_cur
== 56);
795 assert_se(rl
[RLIMIT_CPU
]->rlim_cur
== rl
[RLIMIT_CPU
]->rlim_max
);
797 assert_se(config_parse_rlimit(NULL
, "fake", 1, "section", 1, "LimitCPU", RLIMIT_CPU
, "57s", rl
, NULL
) >= 0);
798 assert_se(rl
[RLIMIT_CPU
]);
799 assert_se(rl
[RLIMIT_CPU
]->rlim_cur
== 57);
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
, "40s:1m", rl
, NULL
) >= 0);
803 assert_se(rl
[RLIMIT_CPU
]);
804 assert_se(rl
[RLIMIT_CPU
]->rlim_cur
== 40);
805 assert_se(rl
[RLIMIT_CPU
]->rlim_max
== 60);
807 assert_se(config_parse_rlimit(NULL
, "fake", 1, "section", 1, "LimitCPU", RLIMIT_CPU
, "infinity", rl
, NULL
) >= 0);
808 assert_se(rl
[RLIMIT_CPU
]);
809 assert_se(rl
[RLIMIT_CPU
]->rlim_cur
== RLIM_INFINITY
);
810 assert_se(rl
[RLIMIT_CPU
]->rlim_cur
== rl
[RLIMIT_CPU
]->rlim_max
);
812 assert_se(config_parse_rlimit(NULL
, "fake", 1, "section", 1, "LimitCPU", RLIMIT_CPU
, "1234ms", rl
, NULL
) >= 0);
813 assert_se(rl
[RLIMIT_CPU
]);
814 assert_se(rl
[RLIMIT_CPU
]->rlim_cur
== 2);
815 assert_se(rl
[RLIMIT_CPU
]->rlim_cur
== rl
[RLIMIT_CPU
]->rlim_max
);
817 rl
[RLIMIT_CPU
] = mfree(rl
[RLIMIT_CPU
]);
819 assert_se(config_parse_rlimit(NULL
, "fake", 1, "section", 1, "LimitRTTIME", RLIMIT_RTTIME
, "58", rl
, NULL
) >= 0);
820 assert_se(rl
[RLIMIT_RTTIME
]);
821 assert_se(rl
[RLIMIT_RTTIME
]->rlim_cur
== 58);
822 assert_se(rl
[RLIMIT_RTTIME
]->rlim_cur
== rl
[RLIMIT_RTTIME
]->rlim_max
);
824 assert_se(config_parse_rlimit(NULL
, "fake", 1, "section", 1, "LimitRTTIME", RLIMIT_RTTIME
, "58:60", 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_max
== 60);
829 assert_se(config_parse_rlimit(NULL
, "fake", 1, "section", 1, "LimitRTTIME", RLIMIT_RTTIME
, "59s", rl
, NULL
) >= 0);
830 assert_se(rl
[RLIMIT_RTTIME
]);
831 assert_se(rl
[RLIMIT_RTTIME
]->rlim_cur
== 59 * USEC_PER_SEC
);
832 assert_se(rl
[RLIMIT_RTTIME
]->rlim_cur
== rl
[RLIMIT_RTTIME
]->rlim_max
);
834 assert_se(config_parse_rlimit(NULL
, "fake", 1, "section", 1, "LimitRTTIME", RLIMIT_RTTIME
, "59s:123s", 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_max
== 123 * USEC_PER_SEC
);
839 assert_se(config_parse_rlimit(NULL
, "fake", 1, "section", 1, "LimitRTTIME", RLIMIT_RTTIME
, "infinity", rl
, NULL
) >= 0);
840 assert_se(rl
[RLIMIT_RTTIME
]);
841 assert_se(rl
[RLIMIT_RTTIME
]->rlim_cur
== RLIM_INFINITY
);
842 assert_se(rl
[RLIMIT_RTTIME
]->rlim_cur
== rl
[RLIMIT_RTTIME
]->rlim_max
);
844 assert_se(config_parse_rlimit(NULL
, "fake", 1, "section", 1, "LimitRTTIME", RLIMIT_RTTIME
, "infinity: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
, "2345ms", rl
, NULL
) >= 0);
850 assert_se(rl
[RLIMIT_RTTIME
]);
851 assert_se(rl
[RLIMIT_RTTIME
]->rlim_cur
== 2345 * USEC_PER_MSEC
);
852 assert_se(rl
[RLIMIT_RTTIME
]->rlim_cur
== rl
[RLIMIT_RTTIME
]->rlim_max
);
854 rl
[RLIMIT_RTTIME
] = mfree(rl
[RLIMIT_RTTIME
]);
857 static void test_config_parse_pass_environ(void) {
858 /* int config_parse_pass_environ(
860 const char *filename,
863 unsigned section_line,
870 _cleanup_strv_free_
char **passenv
= NULL
;
872 r
= config_parse_pass_environ(NULL
, "fake", 1, "section", 1,
873 "PassEnvironment", 0, "A B",
876 assert_se(strv_length(passenv
) == 2);
877 assert_se(streq(passenv
[0], "A"));
878 assert_se(streq(passenv
[1], "B"));
880 r
= config_parse_pass_environ(NULL
, "fake", 1, "section", 1,
881 "PassEnvironment", 0, "",
884 assert_se(strv_isempty(passenv
));
886 r
= config_parse_pass_environ(NULL
, "fake", 1, "section", 1,
887 "PassEnvironment", 0, "'invalid name' 'normal_name' A=1 \\",
890 assert_se(strv_length(passenv
) == 1);
891 assert_se(streq(passenv
[0], "normal_name"));
895 static void test_unit_dump_config_items(void) {
896 unit_dump_config_items(stdout
);
899 int main(int argc
, char *argv
[]) {
900 _cleanup_(rm_rf_physical_and_freep
) char *runtime_dir
= NULL
;
903 log_parse_environment();
906 r
= enter_cgroup_subroot();
907 if (r
== -ENOMEDIUM
) {
908 log_notice_errno(r
, "Skipping test: cgroupfs not available");
909 return EXIT_TEST_SKIP
;
912 assert_se(runtime_dir
= setup_fake_runtime_dir());
914 r
= test_unit_file_get_set();
915 test_config_parse_exec();
916 test_config_parse_log_extra_fields();
917 test_config_parse_capability_set();
918 test_config_parse_rlimit();
919 test_config_parse_pass_environ();
920 test_load_env_file_1();
921 test_load_env_file_2();
922 test_load_env_file_3();
923 test_load_env_file_4();
924 test_load_env_file_5();
925 TEST_REQ_RUNNING_SYSTEMD(test_install_printf());
926 test_unit_dump_config_items();