chiark / gitweb /
Fix service file to match installed elogind binary location
[elogind.git] / src / test / test-fs-util.c
1 /***
2   This file is part of systemd.
3
4   Copyright 2010 Lennart Poettering
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 <unistd.h>
21
22 #include "alloc-util.h"
23 #include "fd-util.h"
24 #include "fileio.h"
25 #include "fs-util.h"
26 #include "macro.h"
27 #include "mkdir.h"
28 #include "path-util.h"
29 #include "rm-rf.h"
30 #include "string-util.h"
31 #include "strv.h"
32 #include "util.h"
33
34 static void test_chase_symlinks(void) {
35         _cleanup_free_ char *result = NULL;
36         char temp[] = "/tmp/test-chase.XXXXXX";
37         const char *top, *p, *q;
38         int r;
39
40         assert_se(mkdtemp(temp));
41
42         top = strjoina(temp, "/top");
43         assert_se(mkdir(top, 0700) >= 0);
44
45         p = strjoina(top, "/dot");
46         assert_se(symlink(".", p) >= 0);
47
48         p = strjoina(top, "/dotdot");
49         assert_se(symlink("..", p) >= 0);
50
51         p = strjoina(top, "/dotdota");
52         assert_se(symlink("../a", p) >= 0);
53
54         p = strjoina(temp, "/a");
55         assert_se(symlink("b", p) >= 0);
56
57         p = strjoina(temp, "/b");
58         assert_se(symlink("/usr", p) >= 0);
59
60         p = strjoina(temp, "/start");
61         assert_se(symlink("top/dot/dotdota", p) >= 0);
62
63         /* Paths that use symlinks underneath the "root" */
64
65         r = chase_symlinks(p, NULL, 0, &result);
66         assert_se(r > 0);
67         assert_se(path_equal(result, "/usr"));
68
69         result = mfree(result);
70         r = chase_symlinks(p, temp, 0, &result);
71         assert_se(r == -ENOENT);
72
73         q = strjoina(temp, "/usr");
74
75         r = chase_symlinks(p, temp, CHASE_NONEXISTENT, &result);
76         assert_se(r == 0);
77         assert_se(path_equal(result, q));
78
79         assert_se(mkdir(q, 0700) >= 0);
80
81         result = mfree(result);
82         r = chase_symlinks(p, temp, 0, &result);
83         assert_se(r > 0);
84         assert_se(path_equal(result, q));
85
86         p = strjoina(temp, "/slash");
87         assert_se(symlink("/", p) >= 0);
88
89         result = mfree(result);
90         r = chase_symlinks(p, NULL, 0, &result);
91         assert_se(r > 0);
92         assert_se(path_equal(result, "/"));
93
94         result = mfree(result);
95         r = chase_symlinks(p, temp, 0, &result);
96         assert_se(r > 0);
97         assert_se(path_equal(result, temp));
98
99         /* Paths that would "escape" outside of the "root" */
100
101         p = strjoina(temp, "/6dots");
102         assert_se(symlink("../../..", p) >= 0);
103
104         result = mfree(result);
105         r = chase_symlinks(p, temp, 0, &result);
106         assert_se(r > 0 && path_equal(result, temp));
107
108         p = strjoina(temp, "/6dotsusr");
109         assert_se(symlink("../../../usr", p) >= 0);
110
111         result = mfree(result);
112         r = chase_symlinks(p, temp, 0, &result);
113         assert_se(r > 0 && path_equal(result, q));
114
115         p = strjoina(temp, "/top/8dotsusr");
116         assert_se(symlink("../../../../usr", p) >= 0);
117
118         result = mfree(result);
119         r = chase_symlinks(p, temp, 0, &result);
120         assert_se(r > 0 && path_equal(result, q));
121
122         /* Paths that contain repeated slashes */
123
124         p = strjoina(temp, "/slashslash");
125         assert_se(symlink("///usr///", p) >= 0);
126
127         result = mfree(result);
128         r = chase_symlinks(p, NULL, 0, &result);
129         assert_se(r > 0);
130         assert_se(path_equal(result, "/usr"));
131
132         result = mfree(result);
133         r = chase_symlinks(p, temp, 0, &result);
134         assert_se(r > 0);
135         assert_se(path_equal(result, q));
136
137         /* Paths using . */
138
139         result = mfree(result);
140         r = chase_symlinks("/etc/./.././", NULL, 0, &result);
141         assert_se(r > 0);
142         assert_se(path_equal(result, "/"));
143
144         result = mfree(result);
145         r = chase_symlinks("/etc/./.././", "/etc", 0, &result);
146         assert_se(r > 0 && path_equal(result, "/etc"));
147
148         result = mfree(result);
149         r = chase_symlinks("/etc/machine-id/foo", NULL, 0, &result);
150         assert_se(r == -ENOTDIR);
151
152         /* Path that loops back to self */
153
154         result = mfree(result);
155         p = strjoina(temp, "/recursive-symlink");
156         assert_se(symlink("recursive-symlink", p) >= 0);
157         r = chase_symlinks(p, NULL, 0, &result);
158         assert_se(r == -ELOOP);
159
160         /* Path which doesn't exist */
161
162         p = strjoina(temp, "/idontexist");
163         r = chase_symlinks(p, NULL, 0, &result);
164         assert_se(r == -ENOENT);
165
166         r = chase_symlinks(p, NULL, CHASE_NONEXISTENT, &result);
167         assert_se(r == 0);
168         assert_se(path_equal(result, p));
169         result = mfree(result);
170
171         p = strjoina(temp, "/idontexist/meneither");
172         r = chase_symlinks(p, NULL, 0, &result);
173         assert_se(r == -ENOENT);
174
175         r = chase_symlinks(p, NULL, CHASE_NONEXISTENT, &result);
176         assert_se(r == 0);
177         assert_se(path_equal(result, p));
178         result = mfree(result);
179
180         /* Path which doesn't exist, but contains weird stuff */
181
182         p = strjoina(temp, "/idontexist/..");
183         r = chase_symlinks(p, NULL, 0, &result);
184         assert_se(r == -ENOENT);
185
186         r = chase_symlinks(p, NULL, CHASE_NONEXISTENT, &result);
187         assert_se(r == -ENOENT);
188
189         p = strjoina(temp, "/target");
190         q = strjoina(temp, "/top");
191         assert_se(symlink(q, p) >= 0);
192         p = strjoina(temp, "/target/idontexist");
193         r = chase_symlinks(p, NULL, 0, &result);
194         assert_se(r == -ENOENT);
195
196         assert_se(rm_rf(temp, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0);
197 }
198
199 static void test_unlink_noerrno(void) {
200         char name[] = "/tmp/test-close_nointr.XXXXXX";
201         int fd;
202
203         fd = mkostemp_safe(name);
204         assert_se(fd >= 0);
205         assert_se(close_nointr(fd) >= 0);
206
207         {
208                 PROTECT_ERRNO;
209                 errno = -42;
210                 assert_se(unlink_noerrno(name) >= 0);
211                 assert_se(errno == -42);
212                 assert_se(unlink_noerrno(name) < 0);
213                 assert_se(errno == -42);
214         }
215 }
216
217 static void test_readlink_and_make_absolute(void) {
218         char tempdir[] = "/tmp/test-readlink_and_make_absolute";
219         char name[] = "/tmp/test-readlink_and_make_absolute/original";
220         char name2[] = "test-readlink_and_make_absolute/original";
221         char name_alias[] = "/tmp/test-readlink_and_make_absolute-alias";
222         char *r = NULL;
223
224         assert_se(mkdir_safe(tempdir, 0755, getuid(), getgid()) >= 0);
225         assert_se(touch(name) >= 0);
226
227         assert_se(symlink(name, name_alias) >= 0);
228         assert_se(readlink_and_make_absolute(name_alias, &r) >= 0);
229         assert_se(streq(r, name));
230         free(r);
231         assert_se(unlink(name_alias) >= 0);
232
233         assert_se(chdir(tempdir) >= 0);
234         assert_se(symlink(name2, name_alias) >= 0);
235         assert_se(readlink_and_make_absolute(name_alias, &r) >= 0);
236         assert_se(streq(r, name));
237         free(r);
238         assert_se(unlink(name_alias) >= 0);
239
240         assert_se(rm_rf(tempdir, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0);
241 }
242
243 static void test_get_files_in_directory(void) {
244         _cleanup_strv_free_ char **l = NULL, **t = NULL;
245
246         assert_se(get_files_in_directory("/tmp", &l) >= 0);
247         assert_se(get_files_in_directory(".", &t) >= 0);
248         assert_se(get_files_in_directory(".", NULL) >= 0);
249 }
250
251 #if 0 /// UNNEEDED by elogind
252 static void test_var_tmp(void) {
253         _cleanup_free_ char *tmpdir_backup = NULL, *temp_backup = NULL, *tmp_backup = NULL;
254         const char *tmp_dir = NULL, *t;
255
256         t = getenv("TMPDIR");
257         if (t) {
258                 tmpdir_backup = strdup(t);
259                 assert_se(tmpdir_backup);
260         }
261
262         t = getenv("TEMP");
263         if (t) {
264                 temp_backup = strdup(t);
265                 assert_se(temp_backup);
266         }
267
268         t = getenv("TMP");
269         if (t) {
270                 tmp_backup = strdup(t);
271                 assert_se(tmp_backup);
272         }
273
274         assert(unsetenv("TMPDIR") >= 0);
275         assert(unsetenv("TEMP") >= 0);
276         assert(unsetenv("TMP") >= 0);
277
278         assert_se(var_tmp_dir(&tmp_dir) >= 0);
279         assert_se(streq(tmp_dir, "/var/tmp"));
280
281         assert_se(setenv("TMPDIR", "/tmp", true) >= 0);
282         assert_se(streq(getenv("TMPDIR"), "/tmp"));
283
284         assert_se(var_tmp_dir(&tmp_dir) >= 0);
285         assert_se(streq(tmp_dir, "/tmp"));
286
287         assert_se(setenv("TMPDIR", "/88_does_not_exist_88", true) >= 0);
288         assert_se(streq(getenv("TMPDIR"), "/88_does_not_exist_88"));
289
290         assert_se(var_tmp_dir(&tmp_dir) >= 0);
291         assert_se(streq(tmp_dir, "/var/tmp"));
292
293         if (tmpdir_backup)  {
294                 assert_se(setenv("TMPDIR", tmpdir_backup, true) >= 0);
295                 assert_se(streq(getenv("TMPDIR"), tmpdir_backup));
296         }
297
298         if (temp_backup)  {
299                 assert_se(setenv("TEMP", temp_backup, true) >= 0);
300                 assert_se(streq(getenv("TEMP"), temp_backup));
301         }
302
303         if (tmp_backup)  {
304                 assert_se(setenv("TMP", tmp_backup, true) >= 0);
305                 assert_se(streq(getenv("TMP"), tmp_backup));
306         }
307 }
308 #endif // 0
309
310 static void test_dot_or_dot_dot(void) {
311         assert_se(!dot_or_dot_dot(NULL));
312         assert_se(!dot_or_dot_dot(""));
313         assert_se(!dot_or_dot_dot("xxx"));
314         assert_se(dot_or_dot_dot("."));
315         assert_se(dot_or_dot_dot(".."));
316         assert_se(!dot_or_dot_dot(".foo"));
317         assert_se(!dot_or_dot_dot("..foo"));
318 }
319
320 int main(int argc, char *argv[]) {
321         test_unlink_noerrno();
322         test_get_files_in_directory();
323         test_readlink_and_make_absolute();
324 #if 0 /// UNNEEDED by elogind
325         test_var_tmp();
326 #endif // 0
327         test_chase_symlinks();
328         test_dot_or_dot_dot();
329
330         return 0;
331 }