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