2 This file is part of systemd.
4 Copyright 2014 Ronny Chevalier
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
30 typedef void (*test_function_t
)(Manager
*m
);
32 static int setup_test(Manager
**m
) {
33 char **tests_path
= STRV_MAKE("exists", "existsglobFOOBAR", "changed", "modified", "unit",
34 "directorynotempty", "makedirectory");
41 r
= manager_new(SYSTEMD_USER
, true, &tmp
);
42 if (IN_SET(r
, -EPERM
, -EACCES
, -EADDRINUSE
, -EHOSTDOWN
, -ENOENT
)) {
43 printf("Skipping test: manager_new: %s", strerror(-r
));
44 return -EXIT_TEST_SKIP
;
47 assert_se(manager_startup(tmp
, NULL
, NULL
) >= 0);
49 STRV_FOREACH(test_path
, tests_path
) {
50 rm_rf_dangerous(strappenda("/tmp/test-path_", *test_path
), false, true, false);
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
));
98 /* But we timeout if the service has not been started in the allocated time */
99 n
= now(CLOCK_MONOTONIC
);
100 if (ts
+ timeout
< n
) {
101 log_error("Test timeout when testing %s", unit
->id
);
106 assert_se(UNIT_VTABLE(unit
)->stop(unit
) >= 0);
107 rm_rf_dangerous(test_path
, false, true, false);
110 static void test_path_exists(Manager
*m
) {
111 const char *test_path
= "/tmp/test-path_exists";
116 assert_se(manager_load_unit(m
, "path-exists.path", NULL
, NULL
, &unit
) >= 0);
117 assert_se(UNIT_VTABLE(unit
)->start(unit
) >= 0);
119 assert_se(touch(test_path
) >= 0);
121 check_stop_unlink(m
, unit
, test_path
, NULL
);
124 static void test_path_existsglob(Manager
*m
) {
125 const char *test_path
= "/tmp/test-path_existsglobFOOBAR";
129 assert_se(manager_load_unit(m
, "path-existsglob.path", NULL
, NULL
, &unit
) >= 0);
130 assert_se(UNIT_VTABLE(unit
)->start(unit
) >= 0);
132 assert_se(touch(test_path
) >= 0);
134 check_stop_unlink(m
, unit
, test_path
, NULL
);
137 static void test_path_changed(Manager
*m
) {
138 const char *test_path
= "/tmp/test-path_changed";
144 assert_se(touch(test_path
) >= 0);
146 assert_se(manager_load_unit(m
, "path-changed.path", NULL
, NULL
, &unit
) >= 0);
147 assert_se(UNIT_VTABLE(unit
)->start(unit
) >= 0);
149 f
= fopen(test_path
, "w");
153 check_stop_unlink(m
, unit
, test_path
, NULL
);
156 static void test_path_modified(Manager
*m
) {
157 _cleanup_fclose_
FILE *f
= NULL
;
158 const char *test_path
= "/tmp/test-path_modified";
163 assert_se(touch(test_path
) >= 0);
165 assert_se(manager_load_unit(m
, "path-modified.path", NULL
, NULL
, &unit
) >= 0);
166 assert_se(UNIT_VTABLE(unit
)->start(unit
) >= 0);
168 f
= fopen(test_path
, "w");
172 check_stop_unlink(m
, unit
, test_path
, NULL
);
175 static void test_path_unit(Manager
*m
) {
176 const char *test_path
= "/tmp/test-path_unit";
181 assert_se(manager_load_unit(m
, "path-unit.path", NULL
, NULL
, &unit
) >= 0);
182 assert_se(UNIT_VTABLE(unit
)->start(unit
) >= 0);
184 assert_se(touch(test_path
) >= 0);
186 check_stop_unlink(m
, unit
, test_path
, "path-mycustomunit.service");
189 static void test_path_directorynotempty(Manager
*m
) {
190 const char *test_path
= "/tmp/test-path_directorynotempty/";
195 assert_se(access(test_path
, F_OK
) < 0);
197 assert_se(manager_load_unit(m
, "path-directorynotempty.path", NULL
, NULL
, &unit
) >= 0);
198 assert_se(UNIT_VTABLE(unit
)->start(unit
) >= 0);
200 /* MakeDirectory default to no */
201 assert_se(access(test_path
, F_OK
) < 0);
203 assert_se(mkdir_p(test_path
, 0755) >= 0);
204 assert_se(touch(strappenda(test_path
, "test_file")) >= 0);
206 check_stop_unlink(m
, unit
, test_path
, NULL
);
209 static void test_path_makedirectory_directorymode(Manager
*m
) {
210 const char *test_path
= "/tmp/test-path_makedirectory/";
216 assert_se(access(test_path
, F_OK
) < 0);
218 assert_se(manager_load_unit(m
, "path-makedirectory.path", NULL
, NULL
, &unit
) >= 0);
219 assert_se(UNIT_VTABLE(unit
)->start(unit
) >= 0);
221 /* Check if the directory has been created */
222 assert_se(access(test_path
, F_OK
) >= 0);
224 /* Check the mode we specified with DirectoryMode=0744 */
225 assert_se(stat(test_path
, &s
) >= 0);
226 assert_se((s
.st_mode
& S_IRWXU
) == 0700);
227 assert_se((s
.st_mode
& S_IRWXG
) == 0040);
228 assert_se((s
.st_mode
& S_IRWXO
) == 0004);
230 assert_se(UNIT_VTABLE(unit
)->stop(unit
) >= 0);
231 rm_rf_dangerous(test_path
, false, true, false);
234 int main(int argc
, char *argv
[]) {
235 test_function_t tests
[] = {
237 test_path_existsglob
,
241 test_path_directorynotempty
,
242 test_path_makedirectory_directorymode
,
245 test_function_t
*test
= NULL
;
248 log_parse_environment();
251 /* It is needed otherwise cgroup creation fails */
253 return EXIT_TEST_SKIP
;
255 assert_se(set_unit_path(TEST_DIR
":") >= 0);
257 for (test
= tests
; test
&& *test
; test
++) {
260 /* We create a clean environment for each test */