]>
Commit | Line | Data |
---|---|---|
35243b77 ZJS |
1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
2 | ||
3 | #include "rm-rf.h" | |
4 | #include "service.h" | |
5 | #include "tests.h" | |
6 | ||
99839c7e LP |
7 | static char *runtime_dir = NULL; |
8 | ||
9 | STATIC_DESTRUCTOR_REGISTER(runtime_dir, rm_rf_physical_and_freep); | |
10 | ||
35243b77 ZJS |
11 | #define EXEC_START_ABSOLUTE \ |
12 | "ExecStart 0 /bin/sh \"sh\" \"-e\" \"-x\" \"-c\" \"systemctl --state=failed --no-legend --no-pager >/failed ; systemctl daemon-reload ; echo OK >/testok\"" | |
13 | #define EXEC_START_RELATIVE \ | |
14 | "ExecStart 0 sh \"sh\" \"-e\" \"-x\" \"-c\" \"systemctl --state=failed --no-legend --no-pager >/failed ; systemctl daemon-reload ; echo OK >/testok\"" | |
15 | ||
16 | static void test_deserialize_exec_command_one(Manager *m, const char *key, const char *line, int expected) { | |
17 | _cleanup_(unit_freep) Unit *u = NULL; | |
18 | int r; | |
19 | ||
20 | assert_se(unit_new_for_name(m, sizeof(Service), "test.service", &u) >= 0); | |
21 | ||
22 | r = service_deserialize_exec_command(u, key, line); | |
23 | log_debug("[%s] → %d (expected: %d)", line, r, expected); | |
f21b863e | 24 | assert_se(r == expected); |
35243b77 ZJS |
25 | |
26 | /* Note that the command doesn't match any command in the empty list of commands in 's', so it is | |
27 | * always rejected with "Current command vanished from the unit file", and we don't leak anything. */ | |
28 | } | |
29 | ||
26e555cb | 30 | TEST(deserialize_exec_command) { |
35243b77 ZJS |
31 | _cleanup_(manager_freep) Manager *m = NULL; |
32 | int r; | |
33 | ||
4870133b | 34 | r = manager_new(RUNTIME_SCOPE_USER, MANAGER_TEST_RUN_MINIMAL, &m); |
35243b77 ZJS |
35 | if (manager_errno_skip_test(r)) { |
36 | log_notice_errno(r, "Skipping test: manager_new: %m"); | |
37 | return; | |
38 | } | |
39 | ||
40 | assert_se(r >= 0); | |
41 | ||
42 | test_deserialize_exec_command_one(m, "main-command", EXEC_START_ABSOLUTE, 0); | |
43 | test_deserialize_exec_command_one(m, "main-command", EXEC_START_RELATIVE, 0); | |
44 | test_deserialize_exec_command_one(m, "control-command", EXEC_START_ABSOLUTE, 0); | |
45 | test_deserialize_exec_command_one(m, "control-command", EXEC_START_RELATIVE, 0); | |
46 | ||
47 | test_deserialize_exec_command_one(m, "control-command", "ExecStart 0 /bin/sh \"sh\"", 0); | |
48 | test_deserialize_exec_command_one(m, "control-command", "ExecStart 0 /no/command ", -EINVAL); | |
49 | test_deserialize_exec_command_one(m, "control-command", "ExecStart 0 /bad/quote \"", -EINVAL); | |
50 | test_deserialize_exec_command_one(m, "control-command", "ExecStart s /bad/id x y z", -EINVAL); | |
51 | test_deserialize_exec_command_one(m, "control-command", "ExecStart 11", -EINVAL); | |
52 | test_deserialize_exec_command_one(m, "control-command", "ExecWhat 11 /a/b c d e", -EINVAL); | |
53 | } | |
54 | ||
99839c7e LP |
55 | static int intro(void) { |
56 | if (enter_cgroup_subroot(NULL) == -ENOMEDIUM) | |
57 | return log_tests_skipped("cgroupfs not available"); | |
35243b77 | 58 | |
99839c7e LP |
59 | assert_se(runtime_dir = setup_fake_runtime_dir()); |
60 | return EXIT_SUCCESS; | |
61 | } | |
35243b77 | 62 | |
e85fdacc | 63 | DEFINE_TEST_MAIN_WITH_INTRO(LOG_DEBUG, intro); |