Line data Source code
1 : /* SPDX-License-Identifier: LGPL-2.1+ */
2 :
3 : #include <sched.h>
4 : #include <signal.h>
5 : #include <stdlib.h>
6 : #include <sys/mount.h>
7 : #include <sys/wait.h>
8 : #include <util.h>
9 :
10 : /* When we include libgen.h because we need dirname() we immediately
11 : * undefine basename() since libgen.h defines it as a macro to the POSIX
12 : * version which is really broken. We prefer GNU basename(). */
13 : #include <libgen.h>
14 : #undef basename
15 :
16 : #include "alloc-util.h"
17 : #include "env-file.h"
18 : #include "env-util.h"
19 : #include "fs-util.h"
20 : #include "log.h"
21 : #include "path-util.h"
22 : #include "strv.h"
23 : #include "tests.h"
24 :
25 7 : char* setup_fake_runtime_dir(void) {
26 7 : char t[] = "/tmp/fake-xdg-runtime-XXXXXX", *p;
27 :
28 7 : assert_se(mkdtemp(t));
29 7 : assert_se(setenv("XDG_RUNTIME_DIR", t, 1) >= 0);
30 7 : assert_se(p = strdup(t));
31 :
32 7 : return p;
33 : }
34 :
35 13 : static void load_testdata_env(void) {
36 : static bool called = false;
37 13 : _cleanup_free_ char *s = NULL;
38 13 : _cleanup_free_ char *envpath = NULL;
39 13 : _cleanup_strv_free_ char **pairs = NULL;
40 : char **k, **v;
41 :
42 13 : if (called)
43 4 : return;
44 9 : called = true;
45 :
46 9 : assert_se(readlink_and_make_absolute("/proc/self/exe", &s) >= 0);
47 9 : dirname(s);
48 :
49 9 : envpath = path_join(s, "systemd-runtest.env");
50 9 : if (load_env_file_pairs(NULL, envpath, &pairs) < 0)
51 0 : return;
52 :
53 27 : STRV_FOREACH_PAIR(k, v, pairs)
54 18 : setenv(*k, *v, 0);
55 : }
56 :
57 12 : const char* get_testdata_dir(void) {
58 : const char *env;
59 :
60 12 : load_testdata_env();
61 :
62 : /* if the env var is set, use that */
63 12 : env = getenv("SYSTEMD_TEST_DATA");
64 12 : if (!env)
65 0 : env = SYSTEMD_TEST_DATA;
66 12 : if (access(env, F_OK) < 0) {
67 0 : fprintf(stderr, "ERROR: $SYSTEMD_TEST_DATA directory [%s] does not exist\n", env);
68 0 : exit(EXIT_FAILURE);
69 : }
70 :
71 12 : return env;
72 : }
73 :
74 1 : const char* get_catalog_dir(void) {
75 : const char *env;
76 :
77 1 : load_testdata_env();
78 :
79 : /* if the env var is set, use that */
80 1 : env = getenv("SYSTEMD_CATALOG_DIR");
81 1 : if (!env)
82 0 : env = SYSTEMD_CATALOG_DIR;
83 1 : if (access(env, F_OK) < 0) {
84 0 : fprintf(stderr, "ERROR: $SYSTEMD_CATALOG_DIR directory [%s] does not exist\n", env);
85 0 : exit(EXIT_FAILURE);
86 : }
87 1 : return env;
88 : }
89 :
90 7 : bool slow_tests_enabled(void) {
91 : int r;
92 :
93 7 : r = getenv_bool("SYSTEMD_SLOW_TESTS");
94 7 : if (r >= 0)
95 0 : return r;
96 :
97 7 : if (r != -ENXIO)
98 0 : log_warning_errno(r, "Cannot parse $SYSTEMD_SLOW_TESTS, ignoring.");
99 7 : return SYSTEMD_SLOW_TESTS_DEFAULT;
100 : }
101 :
102 115 : void test_setup_logging(int level) {
103 115 : log_set_max_level(level);
104 115 : log_parse_environment();
105 115 : log_open();
106 115 : }
107 :
108 10 : int log_tests_skipped(const char *message) {
109 10 : log_notice("%s: %s, skipping tests.",
110 : program_invocation_short_name, message);
111 10 : return EXIT_TEST_SKIP;
112 : }
113 :
114 0 : int log_tests_skipped_errno(int r, const char *message) {
115 0 : log_notice_errno(r, "%s: %s, skipping tests: %m",
116 : program_invocation_short_name, message);
117 0 : return EXIT_TEST_SKIP;
118 : }
119 :
120 3 : bool have_namespaces(void) {
121 3 : siginfo_t si = {};
122 : pid_t pid;
123 :
124 : /* Checks whether namespaces are available. In some cases they aren't. We do this by calling unshare(), and we
125 : * do so in a child process in order not to affect our own process. */
126 :
127 3 : pid = fork();
128 3 : assert_se(pid >= 0);
129 :
130 3 : if (pid == 0) {
131 : /* child */
132 0 : if (unshare(CLONE_NEWNS) < 0)
133 0 : _exit(EXIT_FAILURE);
134 :
135 0 : if (mount(NULL, "/", NULL, MS_SLAVE|MS_REC, NULL) < 0)
136 0 : _exit(EXIT_FAILURE);
137 :
138 0 : _exit(EXIT_SUCCESS);
139 : }
140 :
141 3 : assert_se(waitid(P_PID, pid, &si, WEXITED) >= 0);
142 3 : assert_se(si.si_code == CLD_EXITED);
143 :
144 3 : if (si.si_status == EXIT_SUCCESS)
145 0 : return true;
146 :
147 3 : if (si.si_status == EXIT_FAILURE)
148 3 : return false;
149 :
150 0 : assert_not_reached("unexpected exit code");
151 : }
|