1 /* SPDX-License-Identifier: LGPL-2.1+ */
8 #include "alloc-util.h"
15 #include "path-util.h"
17 #include "string-util.h"
23 typedef void (*test_function_t
)(Manager
*m
);
25 static int setup_test(Manager
**m
) {
26 char **tests_path
= STRV_MAKE("exists", "existsglobFOOBAR", "changed", "modified", "unit",
27 "directorynotempty", "makedirectory");
34 r
= enter_cgroup_subroot(NULL
);
36 return log_tests_skipped("cgroupfs not available");
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");
42 assert_se(manager_startup(tmp
, NULL
, NULL
) >= 0);
44 STRV_FOREACH(test_path
, tests_path
) {
45 _cleanup_free_
char *p
= NULL
;
47 p
= strjoin("/tmp/test-path_", *test_path
);
50 (void) rm_rf(p
, REMOVE_ROOT
|REMOVE_PHYSICAL
);
58 static void shutdown_test(Manager
*m
) {
64 static Service
*service_for_path(Manager
*m
, Path
*path
, const char *service_name
) {
65 _cleanup_free_
char *tmp
= NULL
;
66 Unit
*service_unit
= NULL
;
72 assert_se(tmp
= strreplace(UNIT(path
)->id
, ".path", ".service"));
73 service_unit
= manager_get_unit(m
, tmp
);
75 service_unit
= manager_get_unit(m
, service_name
);
76 assert_se(service_unit
);
78 return SERVICE(service_unit
);
81 static void check_states(Manager
*m
, Path
*path
, Service
*service
, PathState path_state
, ServiceState service_state
) {
83 usec_t timeout
= 2 * USEC_PER_SEC
;
88 ts
= now(CLOCK_MONOTONIC
);
90 while (path
->result
!= PATH_SUCCESS
|| service
->result
!= SERVICE_SUCCESS
||
91 path
->state
!= path_state
|| service
->state
!= service_state
) {
95 r
= sd_event_run(m
->event
, 100 * USEC_PER_MSEC
);
98 printf("%s: state = %s; result = %s \n",
100 path_state_to_string(path
->state
),
101 path_result_to_string(path
->result
));
102 printf("%s: state = %s; result = %s \n",
104 service_state_to_string(service
->state
),
105 service_result_to_string(service
->result
));
107 n
= now(CLOCK_MONOTONIC
);
108 if (ts
+ timeout
< n
) {
109 log_error("Test timeout when testing %s", UNIT(path
)->id
);
115 static void test_path_exists(Manager
*m
) {
116 const char *test_path
= "/tmp/test-path_exists";
119 Service
*service
= NULL
;
123 assert_se(manager_load_startable_unit_or_warn(m
, "path-exists.path", NULL
, &unit
) >= 0);
126 service
= service_for_path(m
, path
, NULL
);
128 assert_se(unit_start(unit
) >= 0);
129 check_states(m
, path
, service
, PATH_WAITING
, SERVICE_DEAD
);
131 assert_se(touch(test_path
) >= 0);
132 check_states(m
, path
, service
, PATH_RUNNING
, SERVICE_RUNNING
);
134 /* Service restarts if file still exists */
135 assert_se(unit_stop(UNIT(service
)) >= 0);
136 check_states(m
, path
, service
, PATH_RUNNING
, SERVICE_RUNNING
);
138 assert_se(rm_rf(test_path
, REMOVE_ROOT
|REMOVE_PHYSICAL
) == 0);
139 assert_se(unit_stop(UNIT(service
)) >= 0);
140 check_states(m
, path
, service
, PATH_WAITING
, SERVICE_DEAD
);
142 assert_se(unit_stop(unit
) >= 0);
145 static void test_path_existsglob(Manager
*m
) {
146 const char *test_path
= "/tmp/test-path_existsglobFOOBAR";
149 Service
*service
= NULL
;
153 assert_se(manager_load_startable_unit_or_warn(m
, "path-existsglob.path", NULL
, &unit
) >= 0);
156 service
= service_for_path(m
, path
, NULL
);
158 assert_se(unit_start(unit
) >= 0);
159 check_states(m
, path
, service
, PATH_WAITING
, SERVICE_DEAD
);
161 assert_se(touch(test_path
) >= 0);
162 check_states(m
, path
, service
, PATH_RUNNING
, SERVICE_RUNNING
);
164 /* Service restarts if file still exists */
165 assert_se(unit_stop(UNIT(service
)) >= 0);
166 check_states(m
, path
, service
, PATH_RUNNING
, SERVICE_RUNNING
);
168 assert_se(rm_rf(test_path
, REMOVE_ROOT
|REMOVE_PHYSICAL
) == 0);
169 assert_se(unit_stop(UNIT(service
)) >= 0);
170 check_states(m
, path
, service
, PATH_WAITING
, SERVICE_DEAD
);
172 assert_se(unit_stop(unit
) >= 0);
175 static void test_path_changed(Manager
*m
) {
176 const char *test_path
= "/tmp/test-path_changed";
180 Service
*service
= NULL
;
184 assert_se(manager_load_startable_unit_or_warn(m
, "path-changed.path", NULL
, &unit
) >= 0);
187 service
= service_for_path(m
, path
, NULL
);
189 assert_se(unit_start(unit
) >= 0);
190 check_states(m
, path
, service
, PATH_WAITING
, SERVICE_DEAD
);
192 assert_se(touch(test_path
) >= 0);
193 check_states(m
, path
, service
, PATH_RUNNING
, SERVICE_RUNNING
);
195 /* Service does not restart if file still exists */
196 assert_se(unit_stop(UNIT(service
)) >= 0);
197 check_states(m
, path
, service
, PATH_WAITING
, SERVICE_DEAD
);
199 f
= fopen(test_path
, "w");
203 check_states(m
, path
, service
, PATH_RUNNING
, SERVICE_RUNNING
);
205 assert_se(unit_stop(UNIT(service
)) >= 0);
206 check_states(m
, path
, service
, PATH_WAITING
, SERVICE_DEAD
);
208 (void) rm_rf(test_path
, REMOVE_ROOT
|REMOVE_PHYSICAL
);
209 assert_se(unit_stop(unit
) >= 0);
212 static void test_path_modified(Manager
*m
) {
213 _cleanup_fclose_
FILE *f
= NULL
;
214 const char *test_path
= "/tmp/test-path_modified";
217 Service
*service
= NULL
;
221 assert_se(manager_load_startable_unit_or_warn(m
, "path-modified.path", NULL
, &unit
) >= 0);
224 service
= service_for_path(m
, path
, NULL
);
226 assert_se(unit_start(unit
) >= 0);
227 check_states(m
, path
, service
, PATH_WAITING
, SERVICE_DEAD
);
229 assert_se(touch(test_path
) >= 0);
230 check_states(m
, path
, service
, PATH_RUNNING
, SERVICE_RUNNING
);
232 /* Service does not restart if file still exists */
233 assert_se(unit_stop(UNIT(service
)) >= 0);
234 check_states(m
, path
, service
, PATH_WAITING
, SERVICE_DEAD
);
236 f
= fopen(test_path
, "w");
240 check_states(m
, path
, service
, PATH_RUNNING
, SERVICE_RUNNING
);
242 assert_se(unit_stop(UNIT(service
)) >= 0);
243 check_states(m
, path
, service
, PATH_WAITING
, SERVICE_DEAD
);
245 (void) rm_rf(test_path
, REMOVE_ROOT
|REMOVE_PHYSICAL
);
246 assert_se(unit_stop(unit
) >= 0);
249 static void test_path_unit(Manager
*m
) {
250 const char *test_path
= "/tmp/test-path_unit";
253 Service
*service
= NULL
;
257 assert_se(manager_load_startable_unit_or_warn(m
, "path-unit.path", NULL
, &unit
) >= 0);
260 service
= service_for_path(m
, path
, "path-mycustomunit.service");
262 assert_se(unit_start(unit
) >= 0);
263 check_states(m
, path
, service
, PATH_WAITING
, SERVICE_DEAD
);
265 assert_se(touch(test_path
) >= 0);
266 check_states(m
, path
, service
, PATH_RUNNING
, SERVICE_RUNNING
);
268 assert_se(rm_rf(test_path
, REMOVE_ROOT
|REMOVE_PHYSICAL
) == 0);
269 assert_se(unit_stop(UNIT(service
)) >= 0);
270 check_states(m
, path
, service
, PATH_WAITING
, SERVICE_DEAD
);
272 assert_se(unit_stop(unit
) >= 0);
275 static void test_path_directorynotempty(Manager
*m
) {
276 const char *test_path
= "/tmp/test-path_directorynotempty/";
279 Service
*service
= NULL
;
283 assert_se(manager_load_startable_unit_or_warn(m
, "path-directorynotempty.path", NULL
, &unit
) >= 0);
286 service
= service_for_path(m
, path
, NULL
);
288 assert_se(access(test_path
, F_OK
) < 0);
290 assert_se(unit_start(unit
) >= 0);
291 check_states(m
, path
, service
, PATH_WAITING
, SERVICE_DEAD
);
293 /* MakeDirectory default to no */
294 assert_se(access(test_path
, F_OK
) < 0);
296 assert_se(mkdir_p(test_path
, 0755) >= 0);
297 assert_se(touch(strjoina(test_path
, "test_file")) >= 0);
298 check_states(m
, path
, service
, PATH_RUNNING
, SERVICE_RUNNING
);
300 /* Service restarts if directory is still not empty */
301 assert_se(unit_stop(UNIT(service
)) >= 0);
302 check_states(m
, path
, service
, PATH_RUNNING
, SERVICE_RUNNING
);
304 assert_se(rm_rf(test_path
, REMOVE_ROOT
|REMOVE_PHYSICAL
) == 0);
305 assert_se(unit_stop(UNIT(service
)) >= 0);
306 check_states(m
, path
, service
, PATH_WAITING
, SERVICE_DEAD
);
308 assert_se(unit_stop(unit
) >= 0);
311 static void test_path_makedirectory_directorymode(Manager
*m
) {
312 const char *test_path
= "/tmp/test-path_makedirectory/";
318 assert_se(manager_load_startable_unit_or_warn(m
, "path-makedirectory.path", NULL
, &unit
) >= 0);
320 assert_se(access(test_path
, F_OK
) < 0);
322 assert_se(unit_start(unit
) >= 0);
324 /* Check if the directory has been created */
325 assert_se(access(test_path
, F_OK
) >= 0);
327 /* Check the mode we specified with DirectoryMode=0744 */
328 assert_se(stat(test_path
, &s
) >= 0);
329 assert_se((s
.st_mode
& S_IRWXU
) == 0700);
330 assert_se((s
.st_mode
& S_IRWXG
) == 0040);
331 assert_se((s
.st_mode
& S_IRWXO
) == 0004);
333 assert_se(unit_stop(unit
) >= 0);
334 (void) rm_rf(test_path
, REMOVE_ROOT
|REMOVE_PHYSICAL
);
337 int main(int argc
, char *argv
[]) {
338 static const test_function_t tests
[] = {
340 test_path_existsglob
,
344 test_path_directorynotempty
,
345 test_path_makedirectory_directorymode
,
349 _cleanup_free_
char *test_path
= NULL
;
350 _cleanup_(rm_rf_physical_and_freep
) char *runtime_dir
= NULL
;
354 test_setup_logging(LOG_INFO
);
356 assert_se(get_testdata_dir("test-path", &test_path
) >= 0);
357 assert_se(set_unit_path(test_path
) >= 0);
358 assert_se(runtime_dir
= setup_fake_runtime_dir());
360 for (const test_function_t
*test
= tests
; test
&& *test
; test
++) {
364 /* We create a clean environment for each test */