2 This file is part of systemd.
4 Copyright 2013 Zbigniew Jędrzejewski-Szmek
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.
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.
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/>.
21 #include <sys/mount.h>
24 #include "alloc-util.h"
27 #include "mount-util.h"
28 #include "path-util.h"
30 #include "string-util.h"
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); \
41 static void test_path(void) {
42 _cleanup_close_ int fd = -1;
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);
50 test_path_compare("/goo/boo", "/goo//boo", 0);
51 test_path_compare("//goo/boo", "/goo/boo//", 0);
53 test_path_compare("/", "///", 0);
55 test_path_compare("/x", "x/", 1);
56 test_path_compare("x/", "/", -1);
58 test_path_compare("/x/./y", "x/y", 1);
59 test_path_compare("x/.y", "x/y", -1);
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);
68 assert_se(path_is_absolute("/"));
69 assert_se(!path_is_absolute("./"));
71 assert_se(is_path("/dir"));
72 assert_se(is_path("a/b"));
73 assert_se(!is_path("."));
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.../"), ""));
80 fd = open("/", O_RDONLY|O_CLOEXEC|O_DIRECTORY|O_NOCTTY);
82 assert_se(fd_is_mount_point(fd, "/", 0) > 0);
85 char p1[] = "aaa/bbb////ccc";
86 char p2[] = "//aaa/.////ccc";
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), "/./"));
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"));
100 assert_se(path_equal_ptr(NULL, NULL));
101 assert_se(path_equal_ptr("/a", "/a"));
102 assert_se(!path_equal_ptr("/a", "/b"));
103 assert_se(!path_equal_ptr("/a", NULL));
104 assert_se(!path_equal_ptr(NULL, "/a"));
107 static void test_path_equal_root(void) {
108 /* Nail down the details of how path_equal("/", ...) works. */
110 assert_se(path_equal("/", "/"));
111 assert_se(path_equal("/", "//"));
113 assert_se(!path_equal("/", "/./"));
114 assert_se(!path_equal("/", "/../"));
116 assert_se(!path_equal("/", "/.../"));
118 /* Make sure that files_same works as expected. */
120 assert_se(files_same("/", "/") > 0);
121 assert_se(files_same("/", "//") > 0);
123 assert_se(files_same("/", "/./") > 0);
124 assert_se(files_same("/", "/../") > 0);
126 assert_se(files_same("/", "/.../") == -ENOENT);
128 /* The same for path_equal_or_files_same. */
130 assert_se(path_equal_or_files_same("/", "/"));
131 assert_se(path_equal_or_files_same("/", "//"));
133 assert_se(path_equal_or_files_same("/", "/./"));
134 assert_se(path_equal_or_files_same("/", "/../"));
136 assert_se(!path_equal_or_files_same("/", "/.../"));
139 static void test_find_binary(const char *self) {
142 assert_se(find_binary("/bin/sh", &p) == 0);
144 assert_se(path_equal(p, "/bin/sh"));
147 assert_se(find_binary(self, &p) == 0);
149 /* libtool might prefix the binary name with "lt-" */
150 assert_se(endswith(p, "/lt-test-path-util") || endswith(p, "/test-path-util"));
151 assert_se(path_is_absolute(p));
154 assert_se(find_binary("sh", &p) == 0);
156 assert_se(endswith(p, "/sh"));
157 assert_se(path_is_absolute(p));
160 assert_se(find_binary("xxxx-xxxx", &p) == -ENOENT);
161 assert_se(find_binary("/some/dir/xxxx-xxxx", &p) == -ENOENT);
164 static void test_prefixes(void) {
165 static const char* values[] = { "/a/b/c/d", "/a/b/c", "/a/b", "/a", "", NULL};
171 PATH_FOREACH_PREFIX_MORE(s, "/a/b/c/d") {
172 log_error("---%s---", s);
173 assert_se(streq(s, values[i++]));
175 assert_se(values[i] == NULL);
178 PATH_FOREACH_PREFIX(s, "/a/b/c/d") {
179 log_error("---%s---", s);
180 assert_se(streq(s, values[i++]));
182 assert_se(values[i] == NULL);
185 PATH_FOREACH_PREFIX_MORE(s, "////a////b////c///d///////")
186 assert_se(streq(s, values[i++]));
187 assert_se(values[i] == NULL);
190 PATH_FOREACH_PREFIX(s, "////a////b////c///d///////")
191 assert_se(streq(s, values[i++]));
192 assert_se(values[i] == NULL);
194 PATH_FOREACH_PREFIX(s, "////")
195 assert_not_reached("Wut?");
198 PATH_FOREACH_PREFIX_MORE(s, "////") {
200 assert_se(streq(s, ""));
205 PATH_FOREACH_PREFIX(s, "")
206 assert_not_reached("wut?");
209 PATH_FOREACH_PREFIX_MORE(s, "") {
211 assert_se(streq(s, ""));
216 static void test_path_join(void) {
218 #define test_join(root, path, rest, expected) { \
219 _cleanup_free_ char *z = NULL; \
220 z = path_join(root, path, rest); \
221 assert_se(streq(z, expected)); \
224 test_join("/root", "/a/b", "/c", "/root/a/b/c");
225 test_join("/root", "a/b", "c", "/root/a/b/c");
226 test_join("/root", "/a/b", "c", "/root/a/b/c");
227 test_join("/root", "/", "c", "/root/c");
228 test_join("/root", "/", NULL, "/root/");
230 test_join(NULL, "/a/b", "/c", "/a/b/c");
231 test_join(NULL, "a/b", "c", "a/b/c");
232 test_join(NULL, "/a/b", "c", "/a/b/c");
233 test_join(NULL, "/", "c", "/c");
234 test_join(NULL, "/", NULL, "/");
237 #if 0 /// UNNEEDED by elogind
238 static void test_fsck_exists(void) {
239 /* Ensure we use a sane default for PATH. */
242 /* fsck.minix is provided by util-linux and will probably exist. */
243 assert_se(fsck_exists("minix") == 1);
245 assert_se(fsck_exists("AbCdE") == 0);
246 assert_se(fsck_exists("/../bin/") == 0);
249 static void test_make_relative(void) {
252 assert_se(path_make_relative("some/relative/path", "/some/path", &result) < 0);
253 assert_se(path_make_relative("/some/path", "some/relative/path", &result) < 0);
255 #define test(from_dir, to_path, expected) { \
256 _cleanup_free_ char *z = NULL; \
257 path_make_relative(from_dir, to_path, &z); \
258 assert_se(streq(z, expected)); \
262 test("/", "/some/path", "some/path");
263 test("/some/path", "/some/path", ".");
264 test("/some/path", "/some/path/in/subdir", "in/subdir");
265 test("/some/path", "/", "../..");
266 test("/some/path", "/some/other/path", "../other/path");
267 test("//extra/////slashes///won't////fool///anybody//", "////extra///slashes////are/just///fine///", "../../../are/just/fine");
271 static void test_strv_resolve(void) {
272 char tmp_dir[] = "/tmp/test-path-util-XXXXXX";
273 _cleanup_strv_free_ char **search_dirs = NULL;
274 _cleanup_strv_free_ char **absolute_dirs = NULL;
277 assert_se(mkdtemp(tmp_dir) != NULL);
279 search_dirs = strv_new("/dir1", "/dir2", "/dir3", NULL);
280 assert_se(search_dirs);
281 STRV_FOREACH(d, search_dirs) {
282 char *p = strappend(tmp_dir, *d);
284 assert_se(strv_push(&absolute_dirs, p) == 0);
287 assert_se(mkdir(absolute_dirs[0], 0700) == 0);
288 assert_se(mkdir(absolute_dirs[1], 0700) == 0);
289 assert_se(symlink("dir2", absolute_dirs[2]) == 0);
291 path_strv_resolve(search_dirs, tmp_dir);
292 assert_se(streq(search_dirs[0], "/dir1"));
293 assert_se(streq(search_dirs[1], "/dir2"));
294 assert_se(streq(search_dirs[2], "/dir2"));
296 assert_se(rm_rf(tmp_dir, REMOVE_ROOT|REMOVE_PHYSICAL) == 0);
299 static void test_path_startswith(void) {
302 p = path_startswith("/foo/bar/barfoo/", "/foo");
303 assert_se(streq_ptr(p, "bar/barfoo/"));
305 p = path_startswith("/foo/bar/barfoo/", "/foo/");
306 assert_se(streq_ptr(p, "bar/barfoo/"));
308 p = path_startswith("/foo/bar/barfoo/", "/");
309 assert_se(streq_ptr(p, "foo/bar/barfoo/"));
311 p = path_startswith("/foo/bar/barfoo/", "////");
312 assert_se(streq_ptr(p, "foo/bar/barfoo/"));
314 p = path_startswith("/foo/bar/barfoo/", "/foo//bar/////barfoo///");
315 assert_se(streq_ptr(p, ""));
317 p = path_startswith("/foo/bar/barfoo/", "/foo/bar/barfoo////");
318 assert_se(streq_ptr(p, ""));
320 p = path_startswith("/foo/bar/barfoo/", "/foo/bar///barfoo/");
321 assert_se(streq_ptr(p, ""));
323 p = path_startswith("/foo/bar/barfoo/", "/foo////bar/barfoo/");
324 assert_se(streq_ptr(p, ""));
326 p = path_startswith("/foo/bar/barfoo/", "////foo/bar/barfoo/");
327 assert_se(streq_ptr(p, ""));
329 p = path_startswith("/foo/bar/barfoo/", "/foo/bar/barfoo");
330 assert_se(streq_ptr(p, ""));
332 assert_se(!path_startswith("/foo/bar/barfoo/", "/foo/bar/barfooa/"));
333 assert_se(!path_startswith("/foo/bar/barfoo/", "/foo/bar/barfooa"));
334 assert_se(!path_startswith("/foo/bar/barfoo/", ""));
335 assert_se(!path_startswith("/foo/bar/barfoo/", "/bar/foo"));
336 assert_se(!path_startswith("/foo/bar/barfoo/", "/f/b/b/"));
339 static void test_prefix_root_one(const char *r, const char *p, const char *expected) {
340 _cleanup_free_ char *s = NULL;
343 assert_se(s = prefix_root(r, p));
344 assert_se(streq_ptr(s, expected));
346 t = prefix_roota(r, p);
348 assert_se(streq_ptr(t, expected));
351 static void test_prefix_root(void) {
352 test_prefix_root_one("/", "/foo", "/foo");
353 test_prefix_root_one(NULL, "/foo", "/foo");
354 test_prefix_root_one("", "/foo", "/foo");
355 test_prefix_root_one("///", "/foo", "/foo");
356 test_prefix_root_one("/", "////foo", "/foo");
357 test_prefix_root_one(NULL, "////foo", "/foo");
359 test_prefix_root_one("/foo", "/bar", "/foo/bar");
360 test_prefix_root_one("/foo", "bar", "/foo/bar");
361 test_prefix_root_one("foo", "bar", "foo/bar");
362 test_prefix_root_one("/foo/", "/bar", "/foo/bar");
363 test_prefix_root_one("/foo/", "//bar", "/foo/bar");
364 test_prefix_root_one("/foo///", "//bar", "/foo/bar");
367 static void test_path_is_mount_point(void) {
369 char tmp_dir[] = "/tmp/test-path-is-mount-point-XXXXXX";
370 _cleanup_free_ char *file1 = NULL, *file2 = NULL, *link1 = NULL, *link2 = NULL;
371 _cleanup_free_ char *dir1 = NULL, *dir1file = NULL, *dirlink1 = NULL, *dirlink1file = NULL;
372 _cleanup_free_ char *dir2 = NULL, *dir2file = NULL;
374 assert_se(path_is_mount_point("/", NULL, AT_SYMLINK_FOLLOW) > 0);
375 assert_se(path_is_mount_point("/", NULL, 0) > 0);
377 assert_se(path_is_mount_point("/proc", NULL, AT_SYMLINK_FOLLOW) > 0);
378 assert_se(path_is_mount_point("/proc", NULL, 0) > 0);
380 assert_se(path_is_mount_point("/proc/1", NULL, AT_SYMLINK_FOLLOW) == 0);
381 assert_se(path_is_mount_point("/proc/1", NULL, 0) == 0);
383 assert_se(path_is_mount_point("/sys", NULL, AT_SYMLINK_FOLLOW) > 0);
384 assert_se(path_is_mount_point("/sys", NULL, 0) > 0);
386 /* we'll create a hierarchy of different kinds of dir/file/link
389 * <tmp>/file1, <tmp>/file2
390 * <tmp>/link1 -> file1, <tmp>/link2 -> file2
393 * <tmp>/dirlink1 -> dir1
394 * <tmp>/dirlink1file -> dirlink1/file
399 /* file mountpoints */
400 assert_se(mkdtemp(tmp_dir) != NULL);
401 file1 = path_join(NULL, tmp_dir, "file1");
403 file2 = path_join(NULL, tmp_dir, "file2");
405 fd = open(file1, O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC, 0664);
408 fd = open(file2, O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC, 0664);
411 link1 = path_join(NULL, tmp_dir, "link1");
413 assert_se(symlink("file1", link1) == 0);
414 link2 = path_join(NULL, tmp_dir, "link2");
416 assert_se(symlink("file2", link2) == 0);
418 assert_se(path_is_mount_point(file1, NULL, AT_SYMLINK_FOLLOW) == 0);
419 assert_se(path_is_mount_point(file1, NULL, 0) == 0);
420 assert_se(path_is_mount_point(link1, NULL, AT_SYMLINK_FOLLOW) == 0);
421 assert_se(path_is_mount_point(link1, NULL, 0) == 0);
423 /* directory mountpoints */
424 dir1 = path_join(NULL, tmp_dir, "dir1");
426 assert_se(mkdir(dir1, 0755) == 0);
427 dirlink1 = path_join(NULL, tmp_dir, "dirlink1");
429 assert_se(symlink("dir1", dirlink1) == 0);
430 dirlink1file = path_join(NULL, tmp_dir, "dirlink1file");
431 assert_se(dirlink1file);
432 assert_se(symlink("dirlink1/file", dirlink1file) == 0);
433 dir2 = path_join(NULL, tmp_dir, "dir2");
435 assert_se(mkdir(dir2, 0755) == 0);
437 assert_se(path_is_mount_point(dir1, NULL, AT_SYMLINK_FOLLOW) == 0);
438 assert_se(path_is_mount_point(dir1, NULL, 0) == 0);
439 assert_se(path_is_mount_point(dirlink1, NULL, AT_SYMLINK_FOLLOW) == 0);
440 assert_se(path_is_mount_point(dirlink1, NULL, 0) == 0);
442 /* file in subdirectory mountpoints */
443 dir1file = path_join(NULL, dir1, "file");
445 fd = open(dir1file, O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC, 0664);
449 assert_se(path_is_mount_point(dir1file, NULL, AT_SYMLINK_FOLLOW) == 0);
450 assert_se(path_is_mount_point(dir1file, NULL, 0) == 0);
451 assert_se(path_is_mount_point(dirlink1file, NULL, AT_SYMLINK_FOLLOW) == 0);
452 assert_se(path_is_mount_point(dirlink1file, NULL, 0) == 0);
454 /* these tests will only work as root */
455 if (mount(file1, file2, NULL, MS_BIND, NULL) >= 0) {
456 int rt, rf, rlt, rlf, rl1t, rl1f;
459 /* capture results in vars, to avoid dangling mounts on failure */
460 rf = path_is_mount_point(file2, NULL, 0);
461 rt = path_is_mount_point(file2, NULL, AT_SYMLINK_FOLLOW);
462 rlf = path_is_mount_point(link2, NULL, 0);
463 rlt = path_is_mount_point(link2, NULL, AT_SYMLINK_FOLLOW);
465 assert_se(umount(file2) == 0);
473 dir2file = path_join(NULL, dir2, "file");
475 fd = open(dir2file, O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC, 0664);
479 assert_se(mount(dir2, dir1, NULL, MS_BIND, NULL) >= 0);
481 rf = path_is_mount_point(dir1, NULL, 0);
482 rt = path_is_mount_point(dir1, NULL, AT_SYMLINK_FOLLOW);
483 rlf = path_is_mount_point(dirlink1, NULL, 0);
484 rlt = path_is_mount_point(dirlink1, NULL, AT_SYMLINK_FOLLOW);
485 /* its parent is a mount point, but not /file itself */
486 rl1f = path_is_mount_point(dirlink1file, NULL, 0);
487 rl1t = path_is_mount_point(dirlink1file, NULL, AT_SYMLINK_FOLLOW);
489 assert_se(umount(dir1) == 0);
495 assert_se(rl1f == 0);
496 assert_se(rl1t == 0);
499 printf("Skipping bind mount file test: %m\n");
501 assert_se(rm_rf(tmp_dir, REMOVE_ROOT|REMOVE_PHYSICAL) == 0);
504 static void test_file_in_same_dir(void) {
507 t = file_in_same_dir("/", "a");
508 assert_se(streq(t, "/a"));
511 t = file_in_same_dir("/", "/a");
512 assert_se(streq(t, "/a"));
515 t = file_in_same_dir("", "a");
516 assert_se(streq(t, "a"));
519 t = file_in_same_dir("a/", "a");
520 assert_se(streq(t, "a/a"));
523 t = file_in_same_dir("bar/foo", "bar");
524 assert_se(streq(t, "bar/bar"));
528 static void test_filename_is_valid(void) {
529 char foo[FILENAME_MAX+2];
532 assert_se(!filename_is_valid(""));
533 assert_se(!filename_is_valid("/bar/foo"));
534 assert_se(!filename_is_valid("/"));
535 assert_se(!filename_is_valid("."));
536 assert_se(!filename_is_valid(".."));
538 for (i=0; i<FILENAME_MAX+1; i++)
540 foo[FILENAME_MAX+1] = '\0';
542 assert_se(!filename_is_valid(foo));
544 assert_se(filename_is_valid("foo_bar-333"));
545 assert_se(filename_is_valid("o.o"));
548 static void test_hidden_or_backup_file(void) {
549 assert_se(hidden_or_backup_file(".hidden"));
550 assert_se(hidden_or_backup_file("..hidden"));
551 assert_se(!hidden_or_backup_file("hidden."));
553 assert_se(hidden_or_backup_file("backup~"));
554 assert_se(hidden_or_backup_file(".backup~"));
556 assert_se(hidden_or_backup_file("lost+found"));
557 assert_se(hidden_or_backup_file("aquota.user"));
558 assert_se(hidden_or_backup_file("aquota.group"));
560 assert_se(hidden_or_backup_file("test.rpmnew"));
561 assert_se(hidden_or_backup_file("test.dpkg-old"));
562 assert_se(hidden_or_backup_file("test.dpkg-remove"));
563 assert_se(hidden_or_backup_file("test.swp"));
565 assert_se(!hidden_or_backup_file("test.rpmnew."));
566 assert_se(!hidden_or_backup_file("test.dpkg-old.foo"));
569 #if 0 /// UNNEEDED by elogind
570 static void test_systemd_installation_has_version(const char *path) {
572 const unsigned versions[] = {0, 231, atoi(PACKAGE_VERSION), 999};
575 for (i = 0; i < ELEMENTSOF(versions); i++) {
576 r = systemd_installation_has_version(path, versions[i]);
578 log_info("%s has systemd >= %u: %s",
579 path ?: "Current installation", versions[i], yes_no(r));
584 int main(int argc, char **argv) {
585 log_set_max_level(LOG_DEBUG);
586 log_parse_environment();
590 test_path_equal_root();
591 test_find_binary(argv[0]);
594 #if 0 /// UNNEEDED by elogind
596 test_make_relative();
599 test_path_startswith();
601 test_path_is_mount_point();
602 test_file_in_same_dir();
603 test_filename_is_valid();
604 test_hidden_or_backup_file();
606 #if 0 /// UNNEEDED by elogind
607 test_systemd_installation_has_version(argv[1]); /* NULL is OK */