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 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
;
69 usec_t timeout
= 2 * USEC_PER_SEC
;
76 assert_se(tmp
= strreplace(unit
->id
, ".path", ".service"));
77 service_unit
= manager_get_unit(m
, tmp
);
79 service_unit
= manager_get_unit(m
, service_name
);
80 assert_se(service_unit
);
81 service
= SERVICE(service_unit
);
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
) {
89 r
= sd_event_run(m
->event
, 100 * USEC_PER_MSEC
);
92 printf("%s: state = %s; result = %s \n",
94 service_state_to_string(service
->state
),
95 service_result_to_string(service
->result
));
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
);
105 assert_se(unit_stop(unit
) >= 0);
106 (void) rm_rf(test_path
, REMOVE_ROOT
|REMOVE_PHYSICAL
);
109 static void test_path_exists(Manager
*m
) {
110 const char *test_path
= "/tmp/test-path_exists";
115 assert_se(manager_load_startable_unit_or_warn(m
, "path-exists.path", NULL
, &unit
) >= 0);
116 assert_se(unit_start(unit
) >= 0);
118 assert_se(touch(test_path
) >= 0);
120 check_stop_unlink(m
, unit
, test_path
, NULL
);
123 static void test_path_existsglob(Manager
*m
) {
124 const char *test_path
= "/tmp/test-path_existsglobFOOBAR";
128 assert_se(manager_load_startable_unit_or_warn(m
, "path-existsglob.path", NULL
, &unit
) >= 0);
129 assert_se(unit_start(unit
) >= 0);
131 assert_se(touch(test_path
) >= 0);
133 check_stop_unlink(m
, unit
, test_path
, NULL
);
136 static void test_path_changed(Manager
*m
) {
137 const char *test_path
= "/tmp/test-path_changed";
143 assert_se(touch(test_path
) >= 0);
145 assert_se(manager_load_startable_unit_or_warn(m
, "path-changed.path", NULL
, &unit
) >= 0);
146 assert_se(unit_start(unit
) >= 0);
148 f
= fopen(test_path
, "w");
152 check_stop_unlink(m
, unit
, test_path
, NULL
);
155 static void test_path_modified(Manager
*m
) {
156 _cleanup_fclose_
FILE *f
= NULL
;
157 const char *test_path
= "/tmp/test-path_modified";
162 assert_se(touch(test_path
) >= 0);
164 assert_se(manager_load_startable_unit_or_warn(m
, "path-modified.path", NULL
, &unit
) >= 0);
165 assert_se(unit_start(unit
) >= 0);
167 f
= fopen(test_path
, "w");
171 check_stop_unlink(m
, unit
, test_path
, NULL
);
174 static void test_path_unit(Manager
*m
) {
175 const char *test_path
= "/tmp/test-path_unit";
180 assert_se(manager_load_startable_unit_or_warn(m
, "path-unit.path", NULL
, &unit
) >= 0);
181 assert_se(unit_start(unit
) >= 0);
183 assert_se(touch(test_path
) >= 0);
185 check_stop_unlink(m
, unit
, test_path
, "path-mycustomunit.service");
188 static void test_path_directorynotempty(Manager
*m
) {
189 const char *test_path
= "/tmp/test-path_directorynotempty/";
194 assert_se(access(test_path
, F_OK
) < 0);
196 assert_se(manager_load_startable_unit_or_warn(m
, "path-directorynotempty.path", NULL
, &unit
) >= 0);
197 assert_se(unit_start(unit
) >= 0);
199 /* MakeDirectory default to no */
200 assert_se(access(test_path
, F_OK
) < 0);
202 assert_se(mkdir_p(test_path
, 0755) >= 0);
203 assert_se(touch(strjoina(test_path
, "test_file")) >= 0);
205 check_stop_unlink(m
, unit
, test_path
, NULL
);
208 static void test_path_makedirectory_directorymode(Manager
*m
) {
209 const char *test_path
= "/tmp/test-path_makedirectory/";
215 assert_se(access(test_path
, F_OK
) < 0);
217 assert_se(manager_load_startable_unit_or_warn(m
, "path-makedirectory.path", NULL
, &unit
) >= 0);
218 assert_se(unit_start(unit
) >= 0);
220 /* Check if the directory has been created */
221 assert_se(access(test_path
, F_OK
) >= 0);
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);
229 assert_se(unit_stop(unit
) >= 0);
230 (void) rm_rf(test_path
, REMOVE_ROOT
|REMOVE_PHYSICAL
);
233 int main(int argc
, char *argv
[]) {
234 static const test_function_t tests
[] = {
236 test_path_existsglob
,
240 test_path_directorynotempty
,
241 test_path_makedirectory_directorymode
,
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
;
252 test_setup_logging(LOG_INFO
);
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());
258 for (test
= tests
; test
&& *test
; test
++) {
261 /* We create a clean environment for each test */