chiark / gitweb /
bus: when dumping string property values escape the chars we use as end-of-line and...
[elogind.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
30 typedef void (*test_function_t)(Manager *m);
31
32 static int setup_test(Manager **m) {
33         char **tests_path = STRV_MAKE("exists", "existsglobFOOBAR", "changed", "modified", "unit",
34                                       "directorynotempty", "makedirectory");
35         char **test_path;
36         Manager *tmp;
37         int r;
38
39         assert_se(m);
40
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;
45         }
46         assert_se(r >= 0);
47         assert_se(manager_startup(tmp, NULL, NULL) >= 0);
48
49         STRV_FOREACH(test_path, tests_path) {
50                rm_rf_dangerous(strappenda("/tmp/test-path_", *test_path), false, true, false);
51         }
52
53         *m = tmp;
54
55         return 0;
56 }
57
58 static void shutdown_test(Manager *m) {
59         assert_se(m);
60
61         manager_free(m);
62 }
63
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;
68         usec_t ts;
69         usec_t timeout = 2 * USEC_PER_SEC;
70
71         assert_se(m);
72         assert_se(unit);
73         assert_se(test_path);
74
75         if (!service_name) {
76                 assert_se(tmp = strreplace(unit->id, ".path", ".service"));
77                 service_unit = manager_get_unit(m, tmp);
78         } else
79                 service_unit = manager_get_unit(m, service_name);
80         assert_se(service_unit);
81         service = SERVICE(service_unit);
82
83         ts = now(CLOCK_MONOTONIC);
84         /* We proces events until the service related to the path has been successfully started */
85         while(service->result != SERVICE_SUCCESS || service->state != SERVICE_START) {
86                 usec_t n;
87                 int r;
88
89                 r = sd_event_run(m->event, 100 * USEC_PER_MSEC);
90                 assert_se(r >= 0);
91
92                 printf("%s: state = %s; result = %s \n",
93                                 service_unit->id,
94                                 service_state_to_string(service->state),
95                                 service_result_to_string(service->result));
96
97
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);
102                         exit(EXIT_FAILURE);
103                 }
104         }
105
106         assert_se(UNIT_VTABLE(unit)->stop(unit) >= 0);
107         assert_se(UNIT_VTABLE(service_unit)->stop(service_unit) >= 0);
108         rm_rf_dangerous(test_path, false, true, false);
109 }
110
111 static void test_path_exists(Manager *m) {
112         const char *test_path = "/tmp/test-path_exists";
113         Unit *unit = NULL;
114
115         assert_se(m);
116
117         assert_se(manager_load_unit(m, "path-exists.path", NULL, NULL, &unit) >= 0);
118         assert_se(UNIT_VTABLE(unit)->start(unit) >= 0);
119
120         assert_se(touch(test_path) >= 0);
121
122         check_stop_unlink(m, unit, test_path, NULL);
123 }
124
125 static void test_path_existsglob(Manager *m) {
126         const char *test_path = "/tmp/test-path_existsglobFOOBAR";
127         Unit *unit = NULL;
128
129         assert_se(m);
130         assert_se(manager_load_unit(m, "path-existsglob.path", NULL, NULL, &unit) >= 0);
131         assert_se(UNIT_VTABLE(unit)->start(unit) >= 0);
132
133         assert_se(touch(test_path) >= 0);
134
135         check_stop_unlink(m, unit, test_path, NULL);
136 }
137
138 static void test_path_changed(Manager *m) {
139         const char *test_path = "/tmp/test-path_changed";
140         FILE *f;
141         Unit *unit = NULL;
142
143         assert_se(m);
144
145         assert_se(touch(test_path) >= 0);
146
147         assert_se(manager_load_unit(m, "path-changed.path", NULL, NULL, &unit) >= 0);
148         assert_se(UNIT_VTABLE(unit)->start(unit) >= 0);
149
150         f = fopen(test_path, "w");
151         assert_se(f);
152         fclose(f);
153
154         check_stop_unlink(m, unit, test_path, NULL);
155 }
156
157 static void test_path_modified(Manager *m) {
158         _cleanup_fclose_ FILE *f = NULL;
159         const char *test_path = "/tmp/test-path_modified";
160         Unit *unit = NULL;
161
162         assert_se(m);
163
164         assert_se(touch(test_path) >= 0);
165
166         assert_se(manager_load_unit(m, "path-modified.path", NULL, NULL, &unit) >= 0);
167         assert_se(UNIT_VTABLE(unit)->start(unit) >= 0);
168
169         f = fopen(test_path, "w");
170         assert_se(f);
171         fputs("test", f);
172
173         check_stop_unlink(m, unit, test_path, NULL);
174 }
175
176 static void test_path_unit(Manager *m) {
177         const char *test_path = "/tmp/test-path_unit";
178         Unit *unit = NULL;
179
180         assert_se(m);
181
182         assert_se(manager_load_unit(m, "path-unit.path", NULL, NULL, &unit) >= 0);
183         assert_se(UNIT_VTABLE(unit)->start(unit) >= 0);
184
185         assert_se(touch(test_path) >= 0);
186
187         check_stop_unlink(m, unit, test_path, "path-mycustomunit.service");
188 }
189
190 static void test_path_directorynotempty(Manager *m) {
191         const char *test_path = "/tmp/test-path_directorynotempty/";
192         Unit *unit = NULL;
193
194         assert_se(m);
195
196         assert_se(access(test_path, F_OK) < 0);
197
198         assert_se(manager_load_unit(m, "path-directorynotempty.path", NULL, NULL, &unit) >= 0);
199         assert_se(UNIT_VTABLE(unit)->start(unit) >= 0);
200
201         /* MakeDirectory default to no */
202         assert_se(access(test_path, F_OK) < 0);
203
204         assert_se(mkdir_p(test_path, 0755) >= 0);
205         assert_se(touch(strappenda(test_path, "test_file")) >= 0);
206
207         check_stop_unlink(m, unit, test_path, NULL);
208 }
209
210 static void test_path_makedirectory_directorymode(Manager *m) {
211         const char *test_path = "/tmp/test-path_makedirectory/";
212         Unit *unit = NULL;
213         struct stat s;
214
215         assert_se(m);
216
217         assert_se(access(test_path, F_OK) < 0);
218
219         assert_se(manager_load_unit(m, "path-makedirectory.path", NULL, NULL, &unit) >= 0);
220         assert_se(UNIT_VTABLE(unit)->start(unit) >= 0);
221
222         /* Check if the directory has been created */
223         assert_se(access(test_path, F_OK) >= 0);
224
225         /* Check the mode we specified with DirectoryMode=0744 */
226         assert_se(stat(test_path, &s) >= 0);
227         assert_se((s.st_mode & S_IRWXU) == 0700);
228         assert_se((s.st_mode & S_IRWXG) == 0040);
229         assert_se((s.st_mode & S_IRWXO) == 0004);
230
231         assert_se(UNIT_VTABLE(unit)->stop(unit) >= 0);
232         rm_rf_dangerous(test_path, false, true, false);
233 }
234
235 int main(int argc, char *argv[]) {
236         test_function_t tests[] = {
237                 test_path_exists,
238                 test_path_existsglob,
239                 test_path_changed,
240                 test_path_modified,
241                 test_path_unit,
242                 test_path_directorynotempty,
243                 test_path_makedirectory_directorymode,
244                 NULL,
245         };
246         test_function_t *test = NULL;
247         Manager *m = NULL;
248
249         log_parse_environment();
250         log_open();
251
252         /* It is needed otherwise cgroup creation fails */
253         if (getuid() != 0)
254                 return EXIT_TEST_SKIP;
255
256         assert_se(set_unit_path(TEST_DIR ":") >= 0);
257
258         for (test = tests; test && *test; test++) {
259                 int r;
260
261                 /* We create a clean environment for each test */
262                 r = setup_test(&m);
263                 if (r < 0)
264                         return -r;
265
266                 (*test)(m);
267
268                 shutdown_test(m);
269         }
270
271         return 0;
272 }