]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-path.c
util: rework rm_rf() logic
[thirdparty/systemd.git] / src / test / test-path.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2014 Ronny Chevalier
5
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.
10
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.
15
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/>.
18 ***/
19
20 #include <stdio.h>
21 #include <stdbool.h>
22
23 #include "unit.h"
24 #include "manager.h"
25 #include "util.h"
26 #include "macro.h"
27 #include "strv.h"
28 #include "mkdir.h"
29 #include "rm-rf.h"
30
31 typedef void (*test_function_t)(Manager *m);
32
33 static int setup_test(Manager **m) {
34 char **tests_path = STRV_MAKE("exists", "existsglobFOOBAR", "changed", "modified", "unit",
35 "directorynotempty", "makedirectory");
36 char **test_path;
37 Manager *tmp = NULL;
38 int r;
39
40 assert_se(m);
41
42 r = manager_new(SYSTEMD_USER, true, &tmp);
43 if (IN_SET(r, -EPERM, -EACCES, -EADDRINUSE, -EHOSTDOWN, -ENOENT)) {
44 printf("Skipping test: manager_new: %s", strerror(-r));
45 return -EXIT_TEST_SKIP;
46 }
47 assert_se(r >= 0);
48 assert_se(manager_startup(tmp, NULL, NULL) >= 0);
49
50 STRV_FOREACH(test_path, tests_path) {
51 _cleanup_free_ char *p = NULL;
52
53 p = strjoin("/tmp/test-path_", *test_path, NULL);
54 assert_se(p);
55
56 (void) rm_rf(p, REMOVE_ROOT|REMOVE_PHYSICAL);
57 }
58
59 *m = tmp;
60
61 return 0;
62 }
63
64 static void shutdown_test(Manager *m) {
65 assert_se(m);
66
67 manager_free(m);
68 }
69
70 static void check_stop_unlink(Manager *m, Unit *unit, const char *test_path, const char *service_name) {
71 _cleanup_free_ char *tmp = NULL;
72 Unit *service_unit = NULL;
73 Service *service = NULL;
74 usec_t ts;
75 usec_t timeout = 2 * USEC_PER_SEC;
76
77 assert_se(m);
78 assert_se(unit);
79 assert_se(test_path);
80
81 if (!service_name) {
82 assert_se(tmp = strreplace(unit->id, ".path", ".service"));
83 service_unit = manager_get_unit(m, tmp);
84 } else
85 service_unit = manager_get_unit(m, service_name);
86 assert_se(service_unit);
87 service = SERVICE(service_unit);
88
89 ts = now(CLOCK_MONOTONIC);
90 /* We process events until the service related to the path has been successfully started */
91 while(service->result != SERVICE_SUCCESS || service->state != SERVICE_START) {
92 usec_t n;
93 int r;
94
95 r = sd_event_run(m->event, 100 * USEC_PER_MSEC);
96 assert_se(r >= 0);
97
98 printf("%s: state = %s; result = %s \n",
99 service_unit->id,
100 service_state_to_string(service->state),
101 service_result_to_string(service->result));
102
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_unit(m, "path-exists.path", NULL, 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_unit(m, "path-existsglob.path", NULL, 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_unit(m, "path-changed.path", NULL, 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_unit(m, "path-modified.path", NULL, 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_unit(m, "path-unit.path", NULL, 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_unit(m, "path-directorynotempty.path", NULL, 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_unit(m, "path-makedirectory.path", NULL, 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 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 test_function_t *test = NULL;
252 Manager *m = NULL;
253
254 log_parse_environment();
255 log_open();
256
257 assert_se(set_unit_path(TEST_DIR ":") >= 0);
258
259 for (test = tests; test && *test; test++) {
260 int r;
261
262 /* We create a clean environment for each test */
263 r = setup_test(&m);
264 if (r < 0)
265 return -r;
266
267 (*test)(m);
268
269 shutdown_test(m);
270 }
271
272 return 0;
273 }