1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2014 Ronny Chevalier
11 #include <sys/types.h>
13 #include "alloc-util.h"
20 #include "string-util.h"
22 #include "test-helper.h"
27 typedef void (*test_function_t
)(Manager
*m
);
29 static int setup_test(Manager
**m
) {
30 char **tests_path
= STRV_MAKE("exists", "existsglobFOOBAR", "changed", "modified", "unit",
31 "directorynotempty", "makedirectory");
38 r
= enter_cgroup_subroot();
39 if (r
== -ENOMEDIUM
) {
40 log_notice_errno(r
, "Skipping test: cgroupfs not available");
41 return -EXIT_TEST_SKIP
;
44 r
= manager_new(UNIT_FILE_USER
, MANAGER_TEST_RUN_BASIC
, &tmp
);
45 if (MANAGER_SKIP_TEST(r
)) {
46 log_notice_errno(r
, "Skipping test: manager_new: %m");
47 return -EXIT_TEST_SKIP
;
50 assert_se(manager_startup(tmp
, NULL
, NULL
) >= 0);
52 STRV_FOREACH(test_path
, tests_path
) {
53 _cleanup_free_
char *p
= NULL
;
55 p
= strjoin("/tmp/test-path_", *test_path
);
58 (void) rm_rf(p
, REMOVE_ROOT
|REMOVE_PHYSICAL
);
66 static void shutdown_test(Manager
*m
) {
72 static void check_stop_unlink(Manager
*m
, Unit
*unit
, const char *test_path
, const char *service_name
) {
73 _cleanup_free_
char *tmp
= NULL
;
74 Unit
*service_unit
= NULL
;
75 Service
*service
= NULL
;
77 usec_t timeout
= 2 * USEC_PER_SEC
;
84 assert_se(tmp
= strreplace(unit
->id
, ".path", ".service"));
85 service_unit
= manager_get_unit(m
, tmp
);
87 service_unit
= manager_get_unit(m
, service_name
);
88 assert_se(service_unit
);
89 service
= SERVICE(service_unit
);
91 ts
= now(CLOCK_MONOTONIC
);
92 /* We process events until the service related to the path has been successfully started */
93 while (service
->result
!= SERVICE_SUCCESS
|| service
->state
!= SERVICE_START
) {
97 r
= sd_event_run(m
->event
, 100 * USEC_PER_MSEC
);
100 printf("%s: state = %s; result = %s \n",
102 service_state_to_string(service
->state
),
103 service_result_to_string(service
->result
));
106 /* But we timeout if the service has not been started in the allocated time */
107 n
= now(CLOCK_MONOTONIC
);
108 if (ts
+ timeout
< n
) {
109 log_error("Test timeout when testing %s", unit
->id
);
114 assert_se(UNIT_VTABLE(unit
)->stop(unit
) >= 0);
115 (void) rm_rf(test_path
, REMOVE_ROOT
|REMOVE_PHYSICAL
);
118 static void test_path_exists(Manager
*m
) {
119 const char *test_path
= "/tmp/test-path_exists";
124 assert_se(manager_load_unit(m
, "path-exists.path", NULL
, NULL
, &unit
) >= 0);
125 assert_se(UNIT_VTABLE(unit
)->start(unit
) >= 0);
127 assert_se(touch(test_path
) >= 0);
129 check_stop_unlink(m
, unit
, test_path
, NULL
);
132 static void test_path_existsglob(Manager
*m
) {
133 const char *test_path
= "/tmp/test-path_existsglobFOOBAR";
137 assert_se(manager_load_unit(m
, "path-existsglob.path", NULL
, NULL
, &unit
) >= 0);
138 assert_se(UNIT_VTABLE(unit
)->start(unit
) >= 0);
140 assert_se(touch(test_path
) >= 0);
142 check_stop_unlink(m
, unit
, test_path
, NULL
);
145 static void test_path_changed(Manager
*m
) {
146 const char *test_path
= "/tmp/test-path_changed";
152 assert_se(touch(test_path
) >= 0);
154 assert_se(manager_load_unit(m
, "path-changed.path", NULL
, NULL
, &unit
) >= 0);
155 assert_se(UNIT_VTABLE(unit
)->start(unit
) >= 0);
157 f
= fopen(test_path
, "w");
161 check_stop_unlink(m
, unit
, test_path
, NULL
);
164 static void test_path_modified(Manager
*m
) {
165 _cleanup_fclose_
FILE *f
= NULL
;
166 const char *test_path
= "/tmp/test-path_modified";
171 assert_se(touch(test_path
) >= 0);
173 assert_se(manager_load_unit(m
, "path-modified.path", NULL
, NULL
, &unit
) >= 0);
174 assert_se(UNIT_VTABLE(unit
)->start(unit
) >= 0);
176 f
= fopen(test_path
, "w");
180 check_stop_unlink(m
, unit
, test_path
, NULL
);
183 static void test_path_unit(Manager
*m
) {
184 const char *test_path
= "/tmp/test-path_unit";
189 assert_se(manager_load_unit(m
, "path-unit.path", NULL
, NULL
, &unit
) >= 0);
190 assert_se(UNIT_VTABLE(unit
)->start(unit
) >= 0);
192 assert_se(touch(test_path
) >= 0);
194 check_stop_unlink(m
, unit
, test_path
, "path-mycustomunit.service");
197 static void test_path_directorynotempty(Manager
*m
) {
198 const char *test_path
= "/tmp/test-path_directorynotempty/";
203 assert_se(access(test_path
, F_OK
) < 0);
205 assert_se(manager_load_unit(m
, "path-directorynotempty.path", NULL
, NULL
, &unit
) >= 0);
206 assert_se(UNIT_VTABLE(unit
)->start(unit
) >= 0);
208 /* MakeDirectory default to no */
209 assert_se(access(test_path
, F_OK
) < 0);
211 assert_se(mkdir_p(test_path
, 0755) >= 0);
212 assert_se(touch(strjoina(test_path
, "test_file")) >= 0);
214 check_stop_unlink(m
, unit
, test_path
, NULL
);
217 static void test_path_makedirectory_directorymode(Manager
*m
) {
218 const char *test_path
= "/tmp/test-path_makedirectory/";
224 assert_se(access(test_path
, F_OK
) < 0);
226 assert_se(manager_load_unit(m
, "path-makedirectory.path", NULL
, NULL
, &unit
) >= 0);
227 assert_se(UNIT_VTABLE(unit
)->start(unit
) >= 0);
229 /* Check if the directory has been created */
230 assert_se(access(test_path
, F_OK
) >= 0);
232 /* Check the mode we specified with DirectoryMode=0744 */
233 assert_se(stat(test_path
, &s
) >= 0);
234 assert_se((s
.st_mode
& S_IRWXU
) == 0700);
235 assert_se((s
.st_mode
& S_IRWXG
) == 0040);
236 assert_se((s
.st_mode
& S_IRWXO
) == 0004);
238 assert_se(UNIT_VTABLE(unit
)->stop(unit
) >= 0);
239 (void) rm_rf(test_path
, REMOVE_ROOT
|REMOVE_PHYSICAL
);
242 int main(int argc
, char *argv
[]) {
243 static const test_function_t tests
[] = {
245 test_path_existsglob
,
249 test_path_directorynotempty
,
250 test_path_makedirectory_directorymode
,
254 _cleanup_(rm_rf_physical_and_freep
) char *runtime_dir
= NULL
;
255 const test_function_t
*test
= NULL
;
260 log_parse_environment();
263 assert_se(set_unit_path(get_testdata_dir("/test-path")) >= 0);
264 assert_se(runtime_dir
= setup_fake_runtime_dir());
266 for (test
= tests
; test
&& *test
; test
++) {
269 /* We create a clean environment for each test */