diff options
Diffstat (limited to 'src/test/test-path-util.c')
-rw-r--r-- | src/test/test-path-util.c | 137 |
1 files changed, 123 insertions, 14 deletions
diff --git a/src/test/test-path-util.c b/src/test/test-path-util.c index 11aa52aaed..0045ae6824 100644 --- a/src/test/test-path-util.c +++ b/src/test/test-path-util.c @@ -21,29 +21,47 @@ #include <stdio.h> #include <unistd.h> +#include <sys/mount.h> #include "path-util.h" #include "util.h" #include "macro.h" #include "strv.h" +#include "rm-rf.h" +#define test_path_compare(a, b, result) { \ + assert_se(path_compare(a, b) == result); \ + assert_se(path_compare(b, a) == -result); \ + assert_se(path_equal(a, b) == !result); \ + assert_se(path_equal(b, a) == !result); \ + } static void test_path(void) { - assert_se(path_equal("/goo", "/goo")); - assert_se(path_equal("//goo", "/goo")); - assert_se(path_equal("//goo/////", "/goo")); - assert_se(path_equal("goo/////", "goo")); + _cleanup_close_ int fd = -1; + + test_path_compare("/goo", "/goo", 0); + test_path_compare("/goo", "/goo", 0); + test_path_compare("//goo", "/goo", 0); + test_path_compare("//goo/////", "/goo", 0); + test_path_compare("goo/////", "goo", 0); + + test_path_compare("/goo/boo", "/goo//boo", 0); + test_path_compare("//goo/boo", "/goo/boo//", 0); - assert_se(path_equal("/goo/boo", "/goo//boo")); - assert_se(path_equal("//goo/boo", "/goo/boo//")); + test_path_compare("/", "///", 0); - assert_se(path_equal("/", "///")); + test_path_compare("/x", "x/", 1); + test_path_compare("x/", "/", -1); - assert_se(!path_equal("/x", "x/")); - assert_se(!path_equal("x/", "/")); + test_path_compare("/x/./y", "x/y", 1); + test_path_compare("x/.y", "x/y", -1); - assert_se(!path_equal("/x/./y", "x/y")); - assert_se(!path_equal("x/.y", "x/y")); + test_path_compare("foo", "/foo", -1); + test_path_compare("/foo", "/foo/bar", -1); + test_path_compare("/foo/aaa", "/foo/b", -1); + test_path_compare("/foo/aaa", "/foo/b/a", -1); + test_path_compare("/foo/a", "/foo/aaa", -1); + test_path_compare("/foo/a/b", "/foo/aaa", -1); assert_se(path_is_absolute("/")); assert_se(!path_is_absolute("./")); @@ -71,8 +89,9 @@ static void test_path(void) { test_parent("/aa///file...", "/aa///"); test_parent("file.../", NULL); - assert_se(path_is_mount_point("/", true)); - assert_se(path_is_mount_point("/", false)); + fd = open("/", O_RDONLY|O_CLOEXEC|O_DIRECTORY|O_NOCTTY); + assert_se(fd >= 0); + assert_se(fd_is_mount_point(fd, "/", 0) > 0); { char p1[] = "aaa/bbb////ccc"; @@ -242,7 +261,7 @@ static void test_strv_resolve(void) { assert_se(streq(search_dirs[1], "/dir2")); assert_se(streq(search_dirs[2], "/dir2")); - assert_se(rm_rf_dangerous(tmp_dir, false, true, false) == 0); + assert_se(rm_rf(tmp_dir, REMOVE_ROOT|REMOVE_PHYSICAL) == 0); } static void test_path_startswith(void) { @@ -264,6 +283,94 @@ static void test_path_startswith(void) { assert_se(!path_startswith("/foo/bar/barfoo/", "/f/b/b/")); } +static void test_prefix_root_one(const char *r, const char *p, const char *expected) { + _cleanup_free_ char *s = NULL; + const char *t; + + assert_se(s = prefix_root(r, p)); + assert_se(streq_ptr(s, expected)); + + t = prefix_roota(r, p); + assert_se(t); + assert_se(streq_ptr(t, expected)); +} + +static void test_prefix_root(void) { + test_prefix_root_one("/", "/foo", "/foo"); + test_prefix_root_one(NULL, "/foo", "/foo"); + test_prefix_root_one("", "/foo", "/foo"); + test_prefix_root_one("///", "/foo", "/foo"); + test_prefix_root_one("/", "////foo", "/foo"); + test_prefix_root_one(NULL, "////foo", "/foo"); + + test_prefix_root_one("/foo", "/bar", "/foo/bar"); + test_prefix_root_one("/foo", "bar", "/foo/bar"); + test_prefix_root_one("foo", "bar", "foo/bar"); + test_prefix_root_one("/foo/", "/bar", "/foo/bar"); + test_prefix_root_one("/foo/", "//bar", "/foo/bar"); + test_prefix_root_one("/foo///", "//bar", "/foo/bar"); +} + +static void test_path_is_mount_point(void) { + int fd, rt, rf, rlt, rlf; + char tmp_dir[] = "/tmp/test-path-is-mount-point-XXXXXX"; + _cleanup_free_ char *file1 = NULL, *file2 = NULL, *link1 = NULL, *link2 = NULL; + + assert_se(path_is_mount_point("/", AT_SYMLINK_FOLLOW) > 0); + assert_se(path_is_mount_point("/", 0) > 0); + + assert_se(path_is_mount_point("/proc", AT_SYMLINK_FOLLOW) > 0); + assert_se(path_is_mount_point("/proc", 0) > 0); + + assert_se(path_is_mount_point("/proc/1", AT_SYMLINK_FOLLOW) == 0); + assert_se(path_is_mount_point("/proc/1", 0) == 0); + + assert_se(path_is_mount_point("/sys", AT_SYMLINK_FOLLOW) > 0); + assert_se(path_is_mount_point("/sys", 0) > 0); + + /* file mountpoints */ + assert_se(mkdtemp(tmp_dir) != NULL); + file1 = path_join(NULL, tmp_dir, "file1"); + assert_se(file1); + file2 = path_join(NULL, tmp_dir, "file2"); + assert_se(file2); + fd = open(file1, O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC, 0664); + assert_se(fd > 0); + close(fd); + fd = open(file2, O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC, 0664); + assert_se(fd > 0); + close(fd); + link1 = path_join(NULL, tmp_dir, "link1"); + assert_se(link1); + assert_se(symlink("file1", link1) == 0); + link2 = path_join(NULL, tmp_dir, "link2"); + assert_se(link1); + assert_se(symlink("file2", link2) == 0); + + assert_se(path_is_mount_point(file1, AT_SYMLINK_FOLLOW) == 0); + assert_se(path_is_mount_point(file1, 0) == 0); + assert_se(path_is_mount_point(link1, AT_SYMLINK_FOLLOW) == 0); + assert_se(path_is_mount_point(link1, 0) == 0); + + /* this test will only work as root */ + if (mount(file1, file2, NULL, MS_BIND, NULL) >= 0) { + rf = path_is_mount_point(file2, 0); + rt = path_is_mount_point(file2, AT_SYMLINK_FOLLOW); + rlf = path_is_mount_point(link2, 0); + rlt = path_is_mount_point(link2, AT_SYMLINK_FOLLOW); + + assert_se(umount(file2) == 0); + + assert_se(rf == 1); + assert_se(rt == 1); + assert_se(rlf == 0); + assert_se(rlt == 1); + } else + printf("Skipping bind mount file test: %m\n"); + + assert_se(rm_rf(tmp_dir, REMOVE_ROOT|REMOVE_PHYSICAL) == 0); +} + int main(int argc, char **argv) { test_path(); test_find_binary(argv[0], true); @@ -274,6 +381,8 @@ int main(int argc, char **argv) { test_make_relative(); test_strv_resolve(); test_path_startswith(); + test_prefix_root(); + test_path_is_mount_point(); return 0; } |