HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

src/test/test-chase.c

49,084 bytesGit mode 100644Download original fileLGPL-2.1-or-later

Git blob: e15a000489a5d61597a7cd82d1437cc2bb77c6cb

Lines 1001–1167 of 1,167

 
        /* (real-fd, XAT_FDROOT, absolute) resolving to root: "/outside" lives directly under
         * root_fd. */
        ASSERT_OK(chaseat(root_fd, XAT_FDROOT, "/outside", 0, &result, &fd));
        ASSERT_TRUE(inode_same_at(fd, NULL, root_fd, "outside", AT_EMPTY_PATH));
        ASSERT_STREQ(result, "outside");
        fd = safe_close(fd);
        result = mfree(result);
 
        /* (real-fd, XAT_FDROOT) with an absolute symlink: the symlink target "/outside" resolves
         * relative to root_fd, not the host root. Since dir_fd == root_fd (XAT_FDROOT was redirected),
         * the result stays relative. */
        ASSERT_OK(chaseat(root_fd, XAT_FDROOT, "sub/escape_abs", 0, &result, &fd));
        ASSERT_TRUE(inode_same_at(fd, NULL, root_fd, "outside", AT_EMPTY_PATH));
        ASSERT_STREQ(result, "outside");
        fd = safe_close(fd);
        result = mfree(result);
 
        /* (real-fd, XAT_FDROOT) with non-existent path. */
        ASSERT_OK_ZERO(chaseat(root_fd, XAT_FDROOT, "/nonexistent", CHASE_NONEXISTENT, &result, NULL));
        ASSERT_STREQ(result, "nonexistent");
        result = mfree(result);
        ASSERT_ERROR(chaseat(root_fd, XAT_FDROOT, "/nonexistent", 0, NULL, NULL), ENOENT);
}
 
TEST(chaseat_prefix_root) {
        _cleanup_free_ char *cwd = NULL, *ret = NULL, *expected = NULL;
 
        ASSERT_OK(safe_getcwd(&cwd));
 
        ASSERT_OK(chaseat_prefix_root("/hoge", NULL, &ret));
        ASSERT_STREQ(ret, "/hoge");
 
        ret = mfree(ret);
 
        ASSERT_OK(chaseat_prefix_root("/hoge", "a/b/c", &ret));
        ASSERT_STREQ(ret, "/hoge");
 
        ret = mfree(ret);
 
        ASSERT_OK(chaseat_prefix_root("hoge", "/a/b//./c///", &ret));
        ASSERT_STREQ(ret, "/a/b/c/hoge");
 
        ret = mfree(ret);
 
        ASSERT_OK(chaseat_prefix_root("hoge", "a/b//./c///", &ret));
        expected = ASSERT_NOT_NULL(path_join(cwd, "a/b/c/hoge"));
        ASSERT_STREQ(ret, expected);
 
        ret = mfree(ret);
        expected = mfree(expected);
 
        ASSERT_OK(chaseat_prefix_root("./hoge/aaa/../././b", "/a/b//./c///", &ret));
        ASSERT_STREQ(ret, "/a/b/c/hoge/aaa/../././b");
 
        ret = mfree(ret);
 
        ASSERT_OK(chaseat_prefix_root("./hoge/aaa/../././b", "a/b//./c///", &ret));
        expected = ASSERT_NOT_NULL(path_join(cwd, "a/b/c/hoge/aaa/../././b"));
        ASSERT_STREQ(ret, expected);
}
 
TEST(trailing_dot_dot) {
        _cleanup_free_ char *path = NULL, *fdpath = NULL;
        _cleanup_close_ int fd = -EBADF;
 
        ASSERT_OK(chase("/usr/..", NULL, CHASE_PARENT, &path, &fd));
        ASSERT_PATH_EQ(path, "/");
        ASSERT_OK(fd_get_path(fd, &fdpath));
        ASSERT_PATH_EQ(fdpath, "/");
 
        path = mfree(path);
        fdpath = mfree(fdpath);
        fd = safe_close(fd);
 
        _cleanup_(rm_rf_physical_and_freep) char *t = NULL;
        ASSERT_OK(mkdtemp_malloc(NULL, &t));
        _cleanup_free_ char *sub = ASSERT_PTR(path_join(t, "a/b/c/d"));
        ASSERT_OK(mkdir_p(sub, 0700));
        _cleanup_free_ char *suffixed = ASSERT_PTR(path_join(sub, ".."));
        ASSERT_OK(chase(suffixed, NULL, CHASE_PARENT, &path, &fd));
        _cleanup_free_ char *expected1 = ASSERT_PTR(path_join(t, "a/b/c"));
        _cleanup_free_ char *expected2 = ASSERT_PTR(path_join(t, "a/b"));
 
        ASSERT_PATH_EQ(path, expected1);
        ASSERT_OK(fd_get_path(fd, &fdpath));
        ASSERT_PATH_EQ(fdpath, expected2);
}
 
TEST(trailing_dot_dot_below_root) {
        _cleanup_(rm_rf_physical_and_freep) char *t = NULL;
        _cleanup_close_ int root_fd = -EBADF, a_fd = -EBADF;
 
        ASSERT_OK(mkdtemp_malloc(NULL, &t));
        _cleanup_free_ char *sub = ASSERT_PTR(path_join(t, "a/b"));
        ASSERT_OK(mkdir_p(sub, 0700));
        root_fd = ASSERT_OK_ERRNO(open(t, O_PATH|O_DIRECTORY|O_CLOEXEC));
        a_fd = ASSERT_OK_ERRNO(openat(root_fd, "a", O_PATH|O_DIRECTORY|O_CLOEXEC));
        ASSERT_OK_ERRNO(symlinkat("..", root_fd, "a/up"));
 
        /* The parent of the root directory is the root directory itself. */
        FOREACH_STRING(p, "..", "a/..", "/a/..", "a/up") {
                _cleanup_free_ char *path = NULL;
                _cleanup_close_ int fd = -EBADF;
 
                log_debug("/* %s(%s) */", __func__, p);
 
                ASSERT_OK(chaseat(root_fd, root_fd, p, CHASE_PARENT, &path, &fd));
                ASSERT_STREQ(path, ".");
                ASSERT_OK_POSITIVE(fd_inode_same(fd, root_fd));
        }
 
        _cleanup_free_ char *path = NULL;
        _cleanup_close_ int fd = -EBADF;
        ASSERT_OK(chaseat(root_fd, a_fd, "..", CHASE_PARENT, &path, &fd));
        ASSERT_STREQ(path, "..");
        ASSERT_OK_POSITIVE(fd_inode_same(fd, root_fd));
 
        /* A result of "." refers to dir_fd, so the fd refers to dir_fd as well. */
        path = mfree(path);
        fd = safe_close(fd);
        ASSERT_OK(chaseat(root_fd, a_fd, "b/..", CHASE_PARENT, &path, &fd));
        ASSERT_STREQ(path, ".");
        ASSERT_OK_POSITIVE(fd_inode_same(fd, a_fd));
 
        path = mfree(path);
        fd = safe_close(fd);
        ASSERT_OK(chaseat(root_fd, root_fd, "a/b/..", CHASE_PARENT, &path, &fd));
        ASSERT_STREQ(path, "a");
        ASSERT_OK_POSITIVE(fd_inode_same(fd, root_fd));
}
 
TEST(use_chase_as_mkdir_p) {
        _cleanup_free_ char *p = NULL;
        ASSERT_OK_ERRNO(asprintf(&p, "/tmp/chasemkdir%" PRIu64 "/a/b/c", random_u64()));
 
        _cleanup_close_ int fd = -EBADF;
        ASSERT_OK(chase(p, NULL, CHASE_PREFIX_ROOT|CHASE_MKDIR_0755, NULL, &fd));
 
        ASSERT_OK_EQ(inode_same_at(AT_FDCWD, p, fd, NULL, AT_EMPTY_PATH), 1);
 
        _cleanup_close_ int fd2 = -EBADF;
        ASSERT_OK(chase(p, p, CHASE_PREFIX_ROOT|CHASE_MKDIR_0755, NULL, &fd2));
 
        _cleanup_free_ char *pp = ASSERT_PTR(path_join(p, p));
 
        ASSERT_OK_EQ(inode_same_at(AT_FDCWD, pp, fd2, NULL, AT_EMPTY_PATH), 1);
 
        _cleanup_free_ char *f = NULL;
        ASSERT_OK(path_extract_directory(p, &f));
 
        _cleanup_free_ char *ff = NULL;
        ASSERT_OK(path_extract_directory(f, &ff));
 
        _cleanup_free_ char *fff = NULL;
        ASSERT_OK(path_extract_directory(ff, &fff));
 
        ASSERT_OK(rm_rf(fff, REMOVE_PHYSICAL));
}
 
static int intro(void) {
        arg_test_dir = saved_argv[1];
        return EXIT_SUCCESS;
}
 
DEFINE_TEST_MAIN_WITH_INTRO(LOG_INFO, intro);