]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-path.c
Merge pull request #1654 from poettering/util-lib
[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 <stdbool.h>
21 #include <stdio.h>
22
23 #include "macro.h"
24 #include "manager.h"
25 #include "mkdir.h"
26 #include "rm-rf.h"
27 #include "string-util.h"
28 #include "strv.h"
29 #include "unit.h"
30 #include "util.h"
31 #include "fd-util.h"
32
33 typedef void (*test_function_t)(Manager *m);
34
35 static int setup_test(Manager **m) {
36 char **tests_path = STRV_MAKE("exists", "existsglobFOOBAR", "changed", "modified", "unit",
37 "directorynotempty", "makedirectory");
38 char **test_path;
39 Manager *tmp = NULL;
40 int r;
41
42 assert_se(m);
43
44 r = manager_new(MANAGER_USER, true, &tmp);
45 if (IN_SET(r, -EPERM, -EACCES, -EADDRINUSE, -EHOSTDOWN, -ENOENT, -ENOEXEC)) {
46 printf("Skipping test: manager_new: %s", strerror(-r));
47 return -EXIT_TEST_SKIP;
48 }
49 assert_se(r >= 0);
50 assert_se(manager_startup(tmp, NULL, NULL) >= 0);
51
52 STRV_FOREACH(test_path, tests_path) {
53 _cleanup_free_ char *p = NULL;
54
55 p = strjoin("/tmp/test-path_", *test_path, NULL);
56 assert_se(p);
57
58 (void) rm_rf(p, REMOVE_ROOT|REMOVE_PHYSICAL);
59 }
60
61 *m = tmp;
62
63 return 0;
64 }
65
66 static void shutdown_test(Manager *m) {
67 assert_se(m);
68
69 manager_free(m);
70 }
71
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;
76 usec_t ts;
77 usec_t timeout = 2 * USEC_PER_SEC;
78
79 assert_se(m);
80 assert_se(unit);
81 assert_se(test_path);
82
83 if (!service_name) {
84 assert_se(tmp = strreplace(unit->id, ".path", ".service"));
85 service_unit = manager_get_unit(m, tmp);
86 } else
87 service_unit = manager_get_unit(m, service_name);
88 assert_se(service_unit);
89 service = SERVICE(service_unit);
90
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) {
94 usec_t n;
95 int r;
96
97 r = sd_event_run(m->event, 100 * USEC_PER_MSEC);
98 assert_se(r >= 0);
99
100 printf("%s: state = %s; result = %s \n",
101 service_unit->id,
102 service_state_to_string(service->state),
103 service_result_to_string(service->result));
104
105
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);
110 exit(EXIT_FAILURE);
111 }
112 }
113
114 assert_se(UNIT_VTABLE(unit)->stop(unit) >= 0);
115 (void) rm_rf(test_path, REMOVE_ROOT|REMOVE_PHYSICAL);
116 }
117
118 static void test_path_exists(Manager *m) {
119 const char *test_path = "/tmp/test-path_exists";
120 Unit *unit = NULL;
121
122 assert_se(m);
123
124 assert_se(manager_load_unit(m, "path-exists.path", NULL, NULL, &unit) >= 0);
125 assert_se(UNIT_VTABLE(unit)->start(unit) >= 0);
126
127 assert_se(touch(test_path) >= 0);
128
129 check_stop_unlink(m, unit, test_path, NULL);
130 }
131
132 static void test_path_existsglob(Manager *m) {
133 const char *test_path = "/tmp/test-path_existsglobFOOBAR";
134 Unit *unit = NULL;
135
136 assert_se(m);
137 assert_se(manager_load_unit(m, "path-existsglob.path", NULL, NULL, &unit) >= 0);
138 assert_se(UNIT_VTABLE(unit)->start(unit) >= 0);
139
140 assert_se(touch(test_path) >= 0);
141
142 check_stop_unlink(m, unit, test_path, NULL);
143 }
144
145 static void test_path_changed(Manager *m) {
146 const char *test_path = "/tmp/test-path_changed";
147 FILE *f;
148 Unit *unit = NULL;
149
150 assert_se(m);
151
152 assert_se(touch(test_path) >= 0);
153
154 assert_se(manager_load_unit(m, "path-changed.path", NULL, NULL, &unit) >= 0);
155 assert_se(UNIT_VTABLE(unit)->start(unit) >= 0);
156
157 f = fopen(test_path, "w");
158 assert_se(f);
159 fclose(f);
160
161 check_stop_unlink(m, unit, test_path, NULL);
162 }
163
164 static void test_path_modified(Manager *m) {
165 _cleanup_fclose_ FILE *f = NULL;
166 const char *test_path = "/tmp/test-path_modified";
167 Unit *unit = NULL;
168
169 assert_se(m);
170
171 assert_se(touch(test_path) >= 0);
172
173 assert_se(manager_load_unit(m, "path-modified.path", NULL, NULL, &unit) >= 0);
174 assert_se(UNIT_VTABLE(unit)->start(unit) >= 0);
175
176 f = fopen(test_path, "w");
177 assert_se(f);
178 fputs("test", f);
179
180 check_stop_unlink(m, unit, test_path, NULL);
181 }
182
183 static void test_path_unit(Manager *m) {
184 const char *test_path = "/tmp/test-path_unit";
185 Unit *unit = NULL;
186
187 assert_se(m);
188
189 assert_se(manager_load_unit(m, "path-unit.path", NULL, NULL, &unit) >= 0);
190 assert_se(UNIT_VTABLE(unit)->start(unit) >= 0);
191
192 assert_se(touch(test_path) >= 0);
193
194 check_stop_unlink(m, unit, test_path, "path-mycustomunit.service");
195 }
196
197 static void test_path_directorynotempty(Manager *m) {
198 const char *test_path = "/tmp/test-path_directorynotempty/";
199 Unit *unit = NULL;
200
201 assert_se(m);
202
203 assert_se(access(test_path, F_OK) < 0);
204
205 assert_se(manager_load_unit(m, "path-directorynotempty.path", NULL, NULL, &unit) >= 0);
206 assert_se(UNIT_VTABLE(unit)->start(unit) >= 0);
207
208 /* MakeDirectory default to no */
209 assert_se(access(test_path, F_OK) < 0);
210
211 assert_se(mkdir_p(test_path, 0755) >= 0);
212 assert_se(touch(strjoina(test_path, "test_file")) >= 0);
213
214 check_stop_unlink(m, unit, test_path, NULL);
215 }
216
217 static void test_path_makedirectory_directorymode(Manager *m) {
218 const char *test_path = "/tmp/test-path_makedirectory/";
219 Unit *unit = NULL;
220 struct stat s;
221
222 assert_se(m);
223
224 assert_se(access(test_path, F_OK) < 0);
225
226 assert_se(manager_load_unit(m, "path-makedirectory.path", NULL, NULL, &unit) >= 0);
227 assert_se(UNIT_VTABLE(unit)->start(unit) >= 0);
228
229 /* Check if the directory has been created */
230 assert_se(access(test_path, F_OK) >= 0);
231
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);
237
238 assert_se(UNIT_VTABLE(unit)->stop(unit) >= 0);
239 (void) rm_rf(test_path, REMOVE_ROOT|REMOVE_PHYSICAL);
240 }
241
242 int main(int argc, char *argv[]) {
243 test_function_t tests[] = {
244 test_path_exists,
245 test_path_existsglob,
246 test_path_changed,
247 test_path_modified,
248 test_path_unit,
249 test_path_directorynotempty,
250 test_path_makedirectory_directorymode,
251 NULL,
252 };
253 test_function_t *test = NULL;
254 Manager *m = NULL;
255
256 log_parse_environment();
257 log_open();
258
259 assert_se(set_unit_path(TEST_DIR) >= 0);
260
261 for (test = tests; test && *test; test++) {
262 int r;
263
264 /* We create a clean environment for each test */
265 r = setup_test(&m);
266 if (r < 0)
267 return -r;
268
269 (*test)(m);
270
271 shutdown_test(m);
272 }
273
274 return 0;
275 }