Debian Codegraph @

src/test/test-unit-serialize.c

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