]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-path.c
journalctl: make libqrencode a weak dependency
[thirdparty/systemd.git] / src / test / test-path.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <stdbool.h>
4 #include <stdio.h>
5 #include <sys/stat.h>
6 #include <sys/types.h>
7
8 #include "alloc-util.h"
9 #include "all-units.h"
10 #include "fd-util.h"
11 #include "fs-util.h"
12 #include "macro.h"
13 #include "manager.h"
14 #include "mkdir.h"
15 #include "path-util.h"
16 #include "rm-rf.h"
17 #include "string-util.h"
18 #include "strv.h"
19 #include "tests.h"
20 #include "unit.h"
21 #include "util.h"
22
23 typedef void (*test_function_t)(Manager *m);
24
25 static int setup_test(Manager **m) {
26 char **tests_path = STRV_MAKE("exists", "existsglobFOOBAR", "changed", "modified", "unit",
27 "directorynotempty", "makedirectory");
28 char **test_path;
29 Manager *tmp = NULL;
30 int r;
31
32 assert_se(m);
33
34 r = enter_cgroup_subroot(NULL);
35 if (r == -ENOMEDIUM)
36 return log_tests_skipped("cgroupfs not available");
37
38 r = manager_new(UNIT_FILE_USER, MANAGER_TEST_RUN_BASIC, &tmp);
39 if (manager_errno_skip_test(r))
40 return log_tests_skipped_errno(r, "manager_new");
41 assert_se(r >= 0);
42 assert_se(manager_startup(tmp, NULL, NULL) >= 0);
43
44 STRV_FOREACH(test_path, tests_path) {
45 _cleanup_free_ char *p = NULL;
46
47 p = strjoin("/tmp/test-path_", *test_path);
48 assert_se(p);
49
50 (void) rm_rf(p, REMOVE_ROOT|REMOVE_PHYSICAL);
51 }
52
53 *m = tmp;
54
55 return 0;
56 }
57
58 static void shutdown_test(Manager *m) {
59 assert_se(m);
60
61 manager_free(m);
62 }
63
64 static void check_stop_unlink(Manager *m, Unit *unit, const char *test_path, const char *service_name) {
65 _cleanup_free_ char *tmp = NULL;
66 Unit *service_unit = NULL;
67 Service *service = NULL;
68 usec_t ts;
69 usec_t timeout = 2 * USEC_PER_SEC;
70
71 assert_se(m);
72 assert_se(unit);
73 assert_se(test_path);
74
75 if (!service_name) {
76 assert_se(tmp = strreplace(unit->id, ".path", ".service"));
77 service_unit = manager_get_unit(m, tmp);
78 } else
79 service_unit = manager_get_unit(m, service_name);
80 assert_se(service_unit);
81 service = SERVICE(service_unit);
82
83 ts = now(CLOCK_MONOTONIC);
84 /* We process events until the service related to the path has been successfully started */
85 while (service->result != SERVICE_SUCCESS || service->state != SERVICE_START) {
86 usec_t n;
87 int r;
88
89 r = sd_event_run(m->event, 100 * USEC_PER_MSEC);
90 assert_se(r >= 0);
91
92 printf("%s: state = %s; result = %s \n",
93 service_unit->id,
94 service_state_to_string(service->state),
95 service_result_to_string(service->result));
96
97 /* But we timeout if the service has not been started in the allocated time */
98 n = now(CLOCK_MONOTONIC);
99 if (ts + timeout < n) {
100 log_error("Test timeout when testing %s", unit->id);
101 exit(EXIT_FAILURE);
102 }
103 }
104
105 assert_se(unit_stop(unit) >= 0);
106 (void) rm_rf(test_path, REMOVE_ROOT|REMOVE_PHYSICAL);
107 }
108
109 static void test_path_exists(Manager *m) {
110 const char *test_path = "/tmp/test-path_exists";
111 Unit *unit = NULL;
112
113 assert_se(m);
114
115 assert_se(manager_load_startable_unit_or_warn(m, "path-exists.path", NULL, &unit) >= 0);
116 assert_se(unit_start(unit) >= 0);
117
118 assert_se(touch(test_path) >= 0);
119
120 check_stop_unlink(m, unit, test_path, NULL);
121 }
122
123 static void test_path_existsglob(Manager *m) {
124 const char *test_path = "/tmp/test-path_existsglobFOOBAR";
125 Unit *unit = NULL;
126
127 assert_se(m);
128 assert_se(manager_load_startable_unit_or_warn(m, "path-existsglob.path", NULL, &unit) >= 0);
129 assert_se(unit_start(unit) >= 0);
130
131 assert_se(touch(test_path) >= 0);
132
133 check_stop_unlink(m, unit, test_path, NULL);
134 }
135
136 static void test_path_changed(Manager *m) {
137 const char *test_path = "/tmp/test-path_changed";
138 FILE *f;
139 Unit *unit = NULL;
140
141 assert_se(m);
142
143 assert_se(touch(test_path) >= 0);
144
145 assert_se(manager_load_startable_unit_or_warn(m, "path-changed.path", NULL, &unit) >= 0);
146 assert_se(unit_start(unit) >= 0);
147
148 f = fopen(test_path, "w");
149 assert_se(f);
150 fclose(f);
151
152 check_stop_unlink(m, unit, test_path, NULL);
153 }
154
155 static void test_path_modified(Manager *m) {
156 _cleanup_fclose_ FILE *f = NULL;
157 const char *test_path = "/tmp/test-path_modified";
158 Unit *unit = NULL;
159
160 assert_se(m);
161
162 assert_se(touch(test_path) >= 0);
163
164 assert_se(manager_load_startable_unit_or_warn(m, "path-modified.path", NULL, &unit) >= 0);
165 assert_se(unit_start(unit) >= 0);
166
167 f = fopen(test_path, "w");
168 assert_se(f);
169 fputs("test", f);
170
171 check_stop_unlink(m, unit, test_path, NULL);
172 }
173
174 static void test_path_unit(Manager *m) {
175 const char *test_path = "/tmp/test-path_unit";
176 Unit *unit = NULL;
177
178 assert_se(m);
179
180 assert_se(manager_load_startable_unit_or_warn(m, "path-unit.path", NULL, &unit) >= 0);
181 assert_se(unit_start(unit) >= 0);
182
183 assert_se(touch(test_path) >= 0);
184
185 check_stop_unlink(m, unit, test_path, "path-mycustomunit.service");
186 }
187
188 static void test_path_directorynotempty(Manager *m) {
189 const char *test_path = "/tmp/test-path_directorynotempty/";
190 Unit *unit = NULL;
191
192 assert_se(m);
193
194 assert_se(access(test_path, F_OK) < 0);
195
196 assert_se(manager_load_startable_unit_or_warn(m, "path-directorynotempty.path", NULL, &unit) >= 0);
197 assert_se(unit_start(unit) >= 0);
198
199 /* MakeDirectory default to no */
200 assert_se(access(test_path, F_OK) < 0);
201
202 assert_se(mkdir_p(test_path, 0755) >= 0);
203 assert_se(touch(strjoina(test_path, "test_file")) >= 0);
204
205 check_stop_unlink(m, unit, test_path, NULL);
206 }
207
208 static void test_path_makedirectory_directorymode(Manager *m) {
209 const char *test_path = "/tmp/test-path_makedirectory/";
210 Unit *unit = NULL;
211 struct stat s;
212
213 assert_se(m);
214
215 assert_se(access(test_path, F_OK) < 0);
216
217 assert_se(manager_load_startable_unit_or_warn(m, "path-makedirectory.path", NULL, &unit) >= 0);
218 assert_se(unit_start(unit) >= 0);
219
220 /* Check if the directory has been created */
221 assert_se(access(test_path, F_OK) >= 0);
222
223 /* Check the mode we specified with DirectoryMode=0744 */
224 assert_se(stat(test_path, &s) >= 0);
225 assert_se((s.st_mode & S_IRWXU) == 0700);
226 assert_se((s.st_mode & S_IRWXG) == 0040);
227 assert_se((s.st_mode & S_IRWXO) == 0004);
228
229 assert_se(unit_stop(unit) >= 0);
230 (void) rm_rf(test_path, REMOVE_ROOT|REMOVE_PHYSICAL);
231 }
232
233 int main(int argc, char *argv[]) {
234 static const test_function_t tests[] = {
235 test_path_exists,
236 test_path_existsglob,
237 test_path_changed,
238 test_path_modified,
239 test_path_unit,
240 test_path_directorynotempty,
241 test_path_makedirectory_directorymode,
242 NULL,
243 };
244
245 _cleanup_(rm_rf_physical_and_freep) char *runtime_dir = NULL;
246 _cleanup_free_ char *test_path = NULL;
247 const test_function_t *test = NULL;
248 Manager *m = NULL;
249
250 umask(022);
251
252 test_setup_logging(LOG_INFO);
253
254 assert_se(get_testdata_dir("test-path", &test_path) >= 0);
255 assert_se(set_unit_path(test_path) >= 0);
256 assert_se(runtime_dir = setup_fake_runtime_dir());
257
258 for (test = tests; test && *test; test++) {
259 int r;
260
261 /* We create a clean environment for each test */
262 r = setup_test(&m);
263 if (r != 0)
264 return r;
265
266 (*test)(m);
267
268 shutdown_test(m);
269 }
270
271 return 0;
272 }