]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-path-util.c
tree-wide: introduce PATH_IN_SET macro
[thirdparty/systemd.git] / src / test / test-path-util.c
CommitLineData
76877b46
ZJS
1/***
2 This file is part of systemd.
3
4 Copyright 2013 Zbigniew Jędrzejewski-Szmek
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
a696dbef 20#include <stdio.h>
5d409034 21#include <sys/mount.h>
07630cea 22#include <unistd.h>
a696dbef 23
b5efdb8a 24#include "alloc-util.h"
3ffd4af2 25#include "fd-util.h"
76877b46 26#include "macro.h"
4349cd7c 27#include "mount-util.h"
07630cea 28#include "path-util.h"
c6878637 29#include "rm-rf.h"
07630cea
LP
30#include "string-util.h"
31#include "strv.h"
32#include "util.h"
76877b46 33
2230852b
MS
34#define test_path_compare(a, b, result) { \
35 assert_se(path_compare(a, b) == result); \
36 assert_se(path_compare(b, a) == -result); \
37 assert_se(path_equal(a, b) == !result); \
38 assert_se(path_equal(b, a) == !result); \
39 }
76877b46
ZJS
40
41static void test_path(void) {
3f72b427
LP
42 _cleanup_close_ int fd = -1;
43
2230852b
MS
44 test_path_compare("/goo", "/goo", 0);
45 test_path_compare("/goo", "/goo", 0);
46 test_path_compare("//goo", "/goo", 0);
47 test_path_compare("//goo/////", "/goo", 0);
48 test_path_compare("goo/////", "goo", 0);
76877b46 49
2230852b
MS
50 test_path_compare("/goo/boo", "/goo//boo", 0);
51 test_path_compare("//goo/boo", "/goo/boo//", 0);
76877b46 52
2230852b 53 test_path_compare("/", "///", 0);
76877b46 54
2230852b
MS
55 test_path_compare("/x", "x/", 1);
56 test_path_compare("x/", "/", -1);
76877b46 57
2230852b
MS
58 test_path_compare("/x/./y", "x/y", 1);
59 test_path_compare("x/.y", "x/y", -1);
60
61 test_path_compare("foo", "/foo", -1);
62 test_path_compare("/foo", "/foo/bar", -1);
63 test_path_compare("/foo/aaa", "/foo/b", -1);
64 test_path_compare("/foo/aaa", "/foo/b/a", -1);
65 test_path_compare("/foo/a", "/foo/aaa", -1);
66 test_path_compare("/foo/a/b", "/foo/aaa", -1);
76877b46
ZJS
67
68 assert_se(path_is_absolute("/"));
69 assert_se(!path_is_absolute("./"));
70
71 assert_se(is_path("/dir"));
72 assert_se(is_path("a/b"));
73 assert_se(!is_path("."));
74
2b6bf07d
ZJS
75 assert_se(streq(basename("./aa/bb/../file.da."), "file.da."));
76 assert_se(streq(basename("/aa///.file"), ".file"));
77 assert_se(streq(basename("/aa///file..."), "file..."));
78 assert_se(streq(basename("file.../"), ""));
76877b46 79
3f72b427
LP
80 fd = open("/", O_RDONLY|O_CLOEXEC|O_DIRECTORY|O_NOCTTY);
81 assert_se(fd >= 0);
5d409034 82 assert_se(fd_is_mount_point(fd, "/", 0) > 0);
76877b46
ZJS
83
84 {
85 char p1[] = "aaa/bbb////ccc";
86 char p2[] = "//aaa/.////ccc";
87 char p3[] = "/./";
88
8d95631e
FB
89 assert_se(path_equal(path_kill_slashes(p1), "aaa/bbb/ccc"));
90 assert_se(path_equal(path_kill_slashes(p2), "/aaa/./ccc"));
91 assert_se(path_equal(path_kill_slashes(p3), "/./"));
76877b46 92 }
3ae5990c
ZJS
93
94 assert_se(PATH_IN_SET("/bin", "/", "/bin", "/foo"));
95 assert_se(PATH_IN_SET("/bin", "/bin"));
96 assert_se(PATH_IN_SET("/bin", "/foo/bar", "/bin"));
97 assert_se(PATH_IN_SET("/", "/", "/", "/foo/bar"));
98 assert_se(!PATH_IN_SET("/", "/abc", "/def"));
76877b46
ZJS
99}
100
85eca92e 101static void test_find_binary(const char *self) {
c9d954b2
ZJS
102 char *p;
103
85eca92e 104 assert_se(find_binary("/bin/sh", &p) == 0);
c9d954b2 105 puts(p);
85eca92e 106 assert_se(path_equal(p, "/bin/sh"));
c9d954b2
ZJS
107 free(p);
108
85eca92e 109 assert_se(find_binary(self, &p) == 0);
c9d954b2 110 puts(p);
8d95631e
FB
111 assert_se(endswith(p, "/test-path-util"));
112 assert_se(path_is_absolute(p));
c9d954b2
ZJS
113 free(p);
114
85eca92e 115 assert_se(find_binary("sh", &p) == 0);
c9d954b2 116 puts(p);
8d95631e
FB
117 assert_se(endswith(p, "/sh"));
118 assert_se(path_is_absolute(p));
c9d954b2
ZJS
119 free(p);
120
85eca92e
LP
121 assert_se(find_binary("xxxx-xxxx", &p) == -ENOENT);
122 assert_se(find_binary("/some/dir/xxxx-xxxx", &p) == -ENOENT);
c9d954b2
ZJS
123}
124
fecffe5d 125static void test_prefixes(void) {
e203f7c3
LP
126 static const char* values[] = { "/a/b/c/d", "/a/b/c", "/a/b", "/a", "", NULL};
127 unsigned i;
fecffe5d 128 char s[PATH_MAX];
e203f7c3 129 bool b;
fecffe5d 130
e203f7c3
LP
131 i = 0;
132 PATH_FOREACH_PREFIX_MORE(s, "/a/b/c/d") {
fecffe5d
LP
133 log_error("---%s---", s);
134 assert_se(streq(s, values[i++]));
135 }
e203f7c3 136 assert_se(values[i] == NULL);
fecffe5d 137
e203f7c3
LP
138 i = 1;
139 PATH_FOREACH_PREFIX(s, "/a/b/c/d") {
140 log_error("---%s---", s);
141 assert_se(streq(s, values[i++]));
142 }
fecffe5d
LP
143 assert_se(values[i] == NULL);
144
145 i = 0;
e203f7c3 146 PATH_FOREACH_PREFIX_MORE(s, "////a////b////c///d///////")
fecffe5d 147 assert_se(streq(s, values[i++]));
e203f7c3 148 assert_se(values[i] == NULL);
fecffe5d 149
e203f7c3
LP
150 i = 1;
151 PATH_FOREACH_PREFIX(s, "////a////b////c///d///////")
152 assert_se(streq(s, values[i++]));
fecffe5d
LP
153 assert_se(values[i] == NULL);
154
155 PATH_FOREACH_PREFIX(s, "////")
e203f7c3
LP
156 assert_not_reached("Wut?");
157
158 b = false;
159 PATH_FOREACH_PREFIX_MORE(s, "////") {
160 assert_se(!b);
fecffe5d 161 assert_se(streq(s, ""));
e203f7c3
LP
162 b = true;
163 }
164 assert_se(b);
fecffe5d
LP
165
166 PATH_FOREACH_PREFIX(s, "")
167 assert_not_reached("wut?");
168
e203f7c3
LP
169 b = false;
170 PATH_FOREACH_PREFIX_MORE(s, "") {
8d95631e
FB
171 assert_se(!b);
172 assert_se(streq(s, ""));
e203f7c3
LP
173 b = true;
174 }
fecffe5d
LP
175}
176
0c6ea3a4 177static void test_path_join(void) {
59ae3a95
TA
178
179#define test_join(root, path, rest, expected) { \
180 _cleanup_free_ char *z = NULL; \
181 z = path_join(root, path, rest); \
182 assert_se(streq(z, expected)); \
183 }
184
185 test_join("/root", "/a/b", "/c", "/root/a/b/c");
186 test_join("/root", "a/b", "c", "/root/a/b/c");
187 test_join("/root", "/a/b", "c", "/root/a/b/c");
bc854dc7 188 test_join("/root", "/", "c", "/root/c");
59ae3a95
TA
189 test_join("/root", "/", NULL, "/root/");
190
191 test_join(NULL, "/a/b", "/c", "/a/b/c");
192 test_join(NULL, "a/b", "c", "a/b/c");
193 test_join(NULL, "/a/b", "c", "/a/b/c");
bc854dc7 194 test_join(NULL, "/", "c", "/c");
59ae3a95 195 test_join(NULL, "/", NULL, "/");
0c6ea3a4
ZJS
196}
197
eb66db55
MG
198static void test_fsck_exists(void) {
199 /* Ensure we use a sane default for PATH. */
200 unsetenv("PATH");
201
202 /* fsck.minix is provided by util-linux and will probably exist. */
85eca92e 203 assert_se(fsck_exists("minix") == 1);
eb66db55 204
85eca92e
LP
205 assert_se(fsck_exists("AbCdE") == 0);
206 assert_se(fsck_exists("/../bin/") == 0);
eb66db55
MG
207}
208
6b56a651
TK
209static void test_make_relative(void) {
210 char *result;
211
212 assert_se(path_make_relative("some/relative/path", "/some/path", &result) < 0);
213 assert_se(path_make_relative("/some/path", "some/relative/path", &result) < 0);
214
59ae3a95
TA
215#define test(from_dir, to_path, expected) { \
216 _cleanup_free_ char *z = NULL; \
217 path_make_relative(from_dir, to_path, &z); \
218 assert_se(streq(z, expected)); \
6b56a651
TK
219 }
220
221 test("/", "/", ".");
222 test("/", "/some/path", "some/path");
223 test("/some/path", "/some/path", ".");
224 test("/some/path", "/some/path/in/subdir", "in/subdir");
225 test("/some/path", "/", "../..");
226 test("/some/path", "/some/other/path", "../other/path");
227 test("//extra/////slashes///won't////fool///anybody//", "////extra///slashes////are/just///fine///", "../../../are/just/fine");
228}
229
3e8a78c8
MM
230static void test_strv_resolve(void) {
231 char tmp_dir[] = "/tmp/test-path-util-XXXXXX";
232 _cleanup_strv_free_ char **search_dirs = NULL;
233 _cleanup_strv_free_ char **absolute_dirs = NULL;
234 char **d;
235
236 assert_se(mkdtemp(tmp_dir) != NULL);
237
238 search_dirs = strv_new("/dir1", "/dir2", "/dir3", NULL);
239 assert_se(search_dirs);
240 STRV_FOREACH(d, search_dirs) {
241 char *p = strappend(tmp_dir, *d);
242 assert_se(p);
243 assert_se(strv_push(&absolute_dirs, p) == 0);
244 }
245
246 assert_se(mkdir(absolute_dirs[0], 0700) == 0);
247 assert_se(mkdir(absolute_dirs[1], 0700) == 0);
248 assert_se(symlink("dir2", absolute_dirs[2]) == 0);
249
250 path_strv_resolve(search_dirs, tmp_dir);
251 assert_se(streq(search_dirs[0], "/dir1"));
252 assert_se(streq(search_dirs[1], "/dir2"));
253 assert_se(streq(search_dirs[2], "/dir2"));
254
c6878637 255 assert_se(rm_rf(tmp_dir, REMOVE_ROOT|REMOVE_PHYSICAL) == 0);
3e8a78c8
MM
256}
257
5895b62f
RC
258static void test_path_startswith(void) {
259 assert_se(path_startswith("/foo/bar/barfoo/", "/foo"));
260 assert_se(path_startswith("/foo/bar/barfoo/", "/foo/"));
261 assert_se(path_startswith("/foo/bar/barfoo/", "/"));
262 assert_se(path_startswith("/foo/bar/barfoo/", "////"));
263 assert_se(path_startswith("/foo/bar/barfoo/", "/foo//bar/////barfoo///"));
264 assert_se(path_startswith("/foo/bar/barfoo/", "/foo/bar/barfoo////"));
265 assert_se(path_startswith("/foo/bar/barfoo/", "/foo/bar///barfoo/"));
266 assert_se(path_startswith("/foo/bar/barfoo/", "/foo////bar/barfoo/"));
267 assert_se(path_startswith("/foo/bar/barfoo/", "////foo/bar/barfoo/"));
268 assert_se(path_startswith("/foo/bar/barfoo/", "/foo/bar/barfoo"));
269
270 assert_se(!path_startswith("/foo/bar/barfoo/", "/foo/bar/barfooa/"));
271 assert_se(!path_startswith("/foo/bar/barfoo/", "/foo/bar/barfooa"));
272 assert_se(!path_startswith("/foo/bar/barfoo/", ""));
273 assert_se(!path_startswith("/foo/bar/barfoo/", "/bar/foo"));
274 assert_se(!path_startswith("/foo/bar/barfoo/", "/f/b/b/"));
275}
276
1d13f648
LP
277static void test_prefix_root_one(const char *r, const char *p, const char *expected) {
278 _cleanup_free_ char *s = NULL;
279 const char *t;
280
281 assert_se(s = prefix_root(r, p));
282 assert_se(streq_ptr(s, expected));
283
284 t = prefix_roota(r, p);
285 assert_se(t);
286 assert_se(streq_ptr(t, expected));
287}
288
289static void test_prefix_root(void) {
290 test_prefix_root_one("/", "/foo", "/foo");
291 test_prefix_root_one(NULL, "/foo", "/foo");
292 test_prefix_root_one("", "/foo", "/foo");
293 test_prefix_root_one("///", "/foo", "/foo");
294 test_prefix_root_one("/", "////foo", "/foo");
295 test_prefix_root_one(NULL, "////foo", "/foo");
296
297 test_prefix_root_one("/foo", "/bar", "/foo/bar");
298 test_prefix_root_one("/foo", "bar", "/foo/bar");
299 test_prefix_root_one("foo", "bar", "foo/bar");
300 test_prefix_root_one("/foo/", "/bar", "/foo/bar");
301 test_prefix_root_one("/foo/", "//bar", "/foo/bar");
302 test_prefix_root_one("/foo///", "//bar", "/foo/bar");
303}
304
5d409034 305static void test_path_is_mount_point(void) {
36908eb8 306 int fd;
5d409034
MP
307 char tmp_dir[] = "/tmp/test-path-is-mount-point-XXXXXX";
308 _cleanup_free_ char *file1 = NULL, *file2 = NULL, *link1 = NULL, *link2 = NULL;
36908eb8
MP
309 _cleanup_free_ char *dir1 = NULL, *dir1file = NULL, *dirlink1 = NULL, *dirlink1file = NULL;
310 _cleanup_free_ char *dir2 = NULL, *dir2file = NULL;
5d409034 311
e26d6ce5
MP
312 assert_se(path_is_mount_point("/", AT_SYMLINK_FOLLOW) > 0);
313 assert_se(path_is_mount_point("/", 0) > 0);
5d409034 314
e26d6ce5
MP
315 assert_se(path_is_mount_point("/proc", AT_SYMLINK_FOLLOW) > 0);
316 assert_se(path_is_mount_point("/proc", 0) > 0);
5d409034 317
e26d6ce5
MP
318 assert_se(path_is_mount_point("/proc/1", AT_SYMLINK_FOLLOW) == 0);
319 assert_se(path_is_mount_point("/proc/1", 0) == 0);
5d409034 320
e26d6ce5
MP
321 assert_se(path_is_mount_point("/sys", AT_SYMLINK_FOLLOW) > 0);
322 assert_se(path_is_mount_point("/sys", 0) > 0);
5d409034 323
36908eb8
MP
324 /* we'll create a hierarchy of different kinds of dir/file/link
325 * layouts:
326 *
327 * <tmp>/file1, <tmp>/file2
328 * <tmp>/link1 -> file1, <tmp>/link2 -> file2
329 * <tmp>/dir1/
330 * <tmp>/dir1/file
331 * <tmp>/dirlink1 -> dir1
332 * <tmp>/dirlink1file -> dirlink1/file
333 * <tmp>/dir2/
334 * <tmp>/dir2/file
335 */
336
5d409034
MP
337 /* file mountpoints */
338 assert_se(mkdtemp(tmp_dir) != NULL);
339 file1 = path_join(NULL, tmp_dir, "file1");
340 assert_se(file1);
341 file2 = path_join(NULL, tmp_dir, "file2");
342 assert_se(file2);
343 fd = open(file1, O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC, 0664);
344 assert_se(fd > 0);
345 close(fd);
346 fd = open(file2, O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC, 0664);
347 assert_se(fd > 0);
348 close(fd);
349 link1 = path_join(NULL, tmp_dir, "link1");
350 assert_se(link1);
351 assert_se(symlink("file1", link1) == 0);
352 link2 = path_join(NULL, tmp_dir, "link2");
353 assert_se(link1);
354 assert_se(symlink("file2", link2) == 0);
355
e26d6ce5
MP
356 assert_se(path_is_mount_point(file1, AT_SYMLINK_FOLLOW) == 0);
357 assert_se(path_is_mount_point(file1, 0) == 0);
358 assert_se(path_is_mount_point(link1, AT_SYMLINK_FOLLOW) == 0);
359 assert_se(path_is_mount_point(link1, 0) == 0);
5d409034 360
36908eb8
MP
361 /* directory mountpoints */
362 dir1 = path_join(NULL, tmp_dir, "dir1");
363 assert_se(dir1);
364 assert_se(mkdir(dir1, 0755) == 0);
365 dirlink1 = path_join(NULL, tmp_dir, "dirlink1");
366 assert_se(dirlink1);
367 assert_se(symlink("dir1", dirlink1) == 0);
368 dirlink1file = path_join(NULL, tmp_dir, "dirlink1file");
369 assert_se(dirlink1file);
370 assert_se(symlink("dirlink1/file", dirlink1file) == 0);
371 dir2 = path_join(NULL, tmp_dir, "dir2");
372 assert_se(dir2);
373 assert_se(mkdir(dir2, 0755) == 0);
374
375 assert_se(path_is_mount_point(dir1, AT_SYMLINK_FOLLOW) == 0);
376 assert_se(path_is_mount_point(dir1, 0) == 0);
377 assert_se(path_is_mount_point(dirlink1, AT_SYMLINK_FOLLOW) == 0);
378 assert_se(path_is_mount_point(dirlink1, 0) == 0);
379
380 /* file in subdirectory mountpoints */
381 dir1file = path_join(NULL, dir1, "file");
382 assert_se(dir1file);
383 fd = open(dir1file, O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC, 0664);
384 assert_se(fd > 0);
385 close(fd);
386
387 assert_se(path_is_mount_point(dir1file, AT_SYMLINK_FOLLOW) == 0);
388 assert_se(path_is_mount_point(dir1file, 0) == 0);
389 assert_se(path_is_mount_point(dirlink1file, AT_SYMLINK_FOLLOW) == 0);
390 assert_se(path_is_mount_point(dirlink1file, 0) == 0);
391
392 /* these tests will only work as root */
5d409034 393 if (mount(file1, file2, NULL, MS_BIND, NULL) >= 0) {
36908eb8
MP
394 int rt, rf, rlt, rlf, rl1t, rl1f;
395
396 /* files */
397 /* capture results in vars, to avoid dangling mounts on failure */
e26d6ce5
MP
398 rf = path_is_mount_point(file2, 0);
399 rt = path_is_mount_point(file2, AT_SYMLINK_FOLLOW);
400 rlf = path_is_mount_point(link2, 0);
401 rlt = path_is_mount_point(link2, AT_SYMLINK_FOLLOW);
5d409034
MP
402
403 assert_se(umount(file2) == 0);
404
405 assert_se(rf == 1);
406 assert_se(rt == 1);
407 assert_se(rlf == 0);
408 assert_se(rlt == 1);
36908eb8
MP
409
410 /* dirs */
411 dir2file = path_join(NULL, dir2, "file");
412 assert_se(dir2file);
413 fd = open(dir2file, O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC, 0664);
414 assert_se(fd > 0);
415 close(fd);
416
417 assert_se(mount(dir2, dir1, NULL, MS_BIND, NULL) >= 0);
418
419 rf = path_is_mount_point(dir1, 0);
420 rt = path_is_mount_point(dir1, AT_SYMLINK_FOLLOW);
421 rlf = path_is_mount_point(dirlink1, 0);
422 rlt = path_is_mount_point(dirlink1, AT_SYMLINK_FOLLOW);
423 /* its parent is a mount point, but not /file itself */
424 rl1f = path_is_mount_point(dirlink1file, 0);
425 rl1t = path_is_mount_point(dirlink1file, AT_SYMLINK_FOLLOW);
426
427 assert_se(umount(dir1) == 0);
428
429 assert_se(rf == 1);
430 assert_se(rt == 1);
431 assert_se(rlf == 0);
432 assert_se(rlt == 1);
433 assert_se(rl1f == 0);
434 assert_se(rl1t == 0);
435
5d409034
MP
436 } else
437 printf("Skipping bind mount file test: %m\n");
438
439 assert_se(rm_rf(tmp_dir, REMOVE_ROOT|REMOVE_PHYSICAL) == 0);
440}
441
63292663
RC
442static void test_file_in_same_dir(void) {
443 char *t;
444
445 t = file_in_same_dir("/", "a");
446 assert_se(streq(t, "/a"));
447 free(t);
448
449 t = file_in_same_dir("/", "/a");
450 assert_se(streq(t, "/a"));
451 free(t);
452
453 t = file_in_same_dir("", "a");
454 assert_se(streq(t, "a"));
455 free(t);
456
457 t = file_in_same_dir("a/", "a");
458 assert_se(streq(t, "a/a"));
459 free(t);
460
461 t = file_in_same_dir("bar/foo", "bar");
462 assert_se(streq(t, "bar/bar"));
463 free(t);
464}
465
466static void test_filename_is_valid(void) {
467 char foo[FILENAME_MAX+2];
468 int i;
469
470 assert_se(!filename_is_valid(""));
471 assert_se(!filename_is_valid("/bar/foo"));
472 assert_se(!filename_is_valid("/"));
473 assert_se(!filename_is_valid("."));
474 assert_se(!filename_is_valid(".."));
475
476 for (i=0; i<FILENAME_MAX+1; i++)
477 foo[i] = 'a';
478 foo[FILENAME_MAX+1] = '\0';
479
480 assert_se(!filename_is_valid(foo));
481
482 assert_se(filename_is_valid("foo_bar-333"));
483 assert_se(filename_is_valid("o.o"));
484}
485
7f076504 486int main(int argc, char **argv) {
76877b46 487 test_path();
85eca92e 488 test_find_binary(argv[0]);
fecffe5d 489 test_prefixes();
0c6ea3a4 490 test_path_join();
eb66db55 491 test_fsck_exists();
6b56a651 492 test_make_relative();
3e8a78c8 493 test_strv_resolve();
5895b62f 494 test_path_startswith();
1d13f648 495 test_prefix_root();
5d409034 496 test_path_is_mount_point();
63292663
RC
497 test_file_in_same_dir();
498 test_filename_is_valid();
5895b62f 499
76877b46
ZJS
500 return 0;
501}