1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
|
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++98, c++03
// <filesystem>
// file_status symlink_status(const path& p);
// file_status symlink_status(const path& p, error_code& ec) noexcept;
#include "filesystem_include.hpp"
#include "test_macros.h"
#include "rapid-cxx-test.hpp"
#include "filesystem_test_helper.hpp"
using namespace fs;
TEST_SUITE(filesystem_symlink_status_test_suite)
TEST_CASE(signature_test)
{
const path p; ((void)p);
std::error_code ec; ((void)ec);
ASSERT_NOT_NOEXCEPT(symlink_status(p));
ASSERT_NOEXCEPT(symlink_status(p, ec));
}
TEST_CASE(test_symlink_status_not_found)
{
const std::error_code expect_ec =
std::make_error_code(std::errc::no_such_file_or_directory);
const path cases[] {
StaticEnv::DNE
};
for (auto& p : cases) {
std::error_code ec = std::make_error_code(std::errc::address_in_use);
// test non-throwing overload.
file_status st = symlink_status(p, ec);
TEST_CHECK(ec == expect_ec);
TEST_CHECK(st.type() == file_type::not_found);
TEST_CHECK(st.permissions() == perms::unknown);
// test throwing overload. It should not throw even though it reports
// that the file was not found.
TEST_CHECK_NO_THROW(st = status(p));
TEST_CHECK(st.type() == file_type::not_found);
TEST_CHECK(st.permissions() == perms::unknown);
}
}
TEST_CASE(test_symlink_status_cannot_resolve)
{
scoped_test_env env;
const path dir = env.create_dir("dir");
const path file_in_dir = env.create_file("dir/file", 42);
const path sym_in_dir = env.create_symlink("dir/file", "dir/bad_sym");
const path sym_points_in_dir = env.create_symlink("dir/file", "sym");
permissions(dir, perms::none);
const std::error_code set_ec =
std::make_error_code(std::errc::address_in_use);
const std::error_code expect_ec =
std::make_error_code(std::errc::permission_denied);
const path fail_cases[] = {
file_in_dir, sym_in_dir
};
for (auto& p : fail_cases)
{
{ // test non-throwing case
std::error_code ec = set_ec;
file_status st = symlink_status(p, ec);
TEST_CHECK(ec == expect_ec);
TEST_CHECK(st.type() == file_type::none);
TEST_CHECK(st.permissions() == perms::unknown);
}
#ifndef TEST_HAS_NO_EXCEPTIONS
{ // test throwing case
try {
symlink_status(p);
} catch (filesystem_error const& err) {
TEST_CHECK(err.path1() == p);
TEST_CHECK(err.path2() == "");
TEST_CHECK(err.code() == expect_ec);
}
}
#endif
}
// Test that a symlink that points into a directory without read perms
// can be stat-ed using symlink_status
{
std::error_code ec = set_ec;
file_status st = symlink_status(sym_points_in_dir, ec);
TEST_CHECK(!ec);
TEST_CHECK(st.type() == file_type::symlink);
TEST_CHECK(st.permissions() != perms::unknown);
// test non-throwing version
TEST_REQUIRE_NO_THROW(st = symlink_status(sym_points_in_dir));
TEST_CHECK(st.type() == file_type::symlink);
TEST_CHECK(st.permissions() != perms::unknown);
}
}
TEST_CASE(symlink_status_file_types_test)
{
scoped_test_env env;
struct TestCase {
path p;
file_type expect_type;
} cases[] = {
{StaticEnv::BadSymlink, file_type::symlink},
{StaticEnv::File, file_type::regular},
{StaticEnv::SymlinkToFile, file_type::symlink},
{StaticEnv::Dir, file_type::directory},
{StaticEnv::SymlinkToDir, file_type::symlink},
// Block files tested elsewhere
{StaticEnv::CharFile, file_type::character},
#if !defined(__APPLE__) && !defined(__FreeBSD__) // No support for domain sockets
{env.create_socket("socket"), file_type::socket},
#endif
{env.create_fifo("fifo"), file_type::fifo}
};
for (const auto& TC : cases) {
// test non-throwing case
std::error_code ec = std::make_error_code(std::errc::address_in_use);
file_status st = symlink_status(TC.p, ec);
TEST_CHECK(!ec);
TEST_CHECK(st.type() == TC.expect_type);
TEST_CHECK(st.permissions() != perms::unknown);
// test throwing case
TEST_REQUIRE_NO_THROW(st = symlink_status(TC.p));
TEST_CHECK(st.type() == TC.expect_type);
TEST_CHECK(st.permissions() != perms::unknown);
}
}
TEST_CASE(test_block_file)
{
const path possible_paths[] = {
"/dev/drive0", // Apple
"/dev/sda", // Linux
"/dev/loop0" // Linux
// No FreeBSD files known
};
path p;
for (const path& possible_p : possible_paths) {
std::error_code ec;
if (exists(possible_p, ec)) {
p = possible_p;
break;
}
}
if (p == path{}) {
TEST_UNSUPPORTED();
}
scoped_test_env env;
{ // test block file
// test non-throwing case
std::error_code ec = std::make_error_code(std::errc::address_in_use);
file_status st = symlink_status(p, ec);
TEST_CHECK(!ec);
TEST_CHECK(st.type() == file_type::block);
TEST_CHECK(st.permissions() != perms::unknown);
// test throwing case
TEST_REQUIRE_NO_THROW(st = symlink_status(p));
TEST_CHECK(st.type() == file_type::block);
TEST_CHECK(st.permissions() != perms::unknown);
}
const path sym = env.make_env_path("sym");
create_symlink(p, sym);
{ // test symlink to block file
// test non-throwing case
std::error_code ec = std::make_error_code(std::errc::address_in_use);
file_status st = symlink_status(sym, ec);
TEST_CHECK(!ec);
TEST_CHECK(st.type() == file_type::symlink);
TEST_CHECK(st.permissions() != perms::unknown);
// test throwing case
TEST_REQUIRE_NO_THROW(st = symlink_status(sym));
TEST_CHECK(st.type() == file_type::symlink);
TEST_CHECK(st.permissions() != perms::unknown);
}
}
TEST_SUITE_END()
|