1 //===----------------------------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 // UNSUPPORTED: c++03 10 11 // <filesystem> 12 13 // file_status status(const path& p); 14 // file_status status(const path& p, error_code& ec) noexcept; 15 16 #include "filesystem_include.h" 17 18 #include "test_macros.h" 19 #include "rapid-cxx-test.h" 20 #include "filesystem_test_helper.h" 21 22 using namespace fs; 23 24 TEST_SUITE(filesystem_status_test_suite) 25 26 TEST_CASE(signature_test) 27 { 28 const path p; ((void)p); 29 std::error_code ec; ((void)ec); 30 ASSERT_NOT_NOEXCEPT(status(p)); 31 ASSERT_NOEXCEPT(status(p, ec)); 32 } 33 34 TEST_CASE(test_status_not_found) 35 { 36 static_test_env static_env; 37 const std::errc expect_errc = std::errc::no_such_file_or_directory; 38 const path cases[] { 39 static_env.DNE, 40 static_env.BadSymlink 41 }; 42 for (auto& p : cases) { 43 std::error_code ec = std::make_error_code(std::errc::address_in_use); 44 // test non-throwing overload. 45 file_status st = status(p, ec); 46 TEST_CHECK(ErrorIs(ec, expect_errc)); 47 TEST_CHECK(st.type() == file_type::not_found); 48 TEST_CHECK(st.permissions() == perms::unknown); 49 // test throwing overload. It should not throw even though it reports 50 // that the file was not found. 51 TEST_CHECK_NO_THROW(st = status(p)); 52 TEST_CHECK(st.type() == file_type::not_found); 53 TEST_CHECK(st.permissions() == perms::unknown); 54 } 55 } 56 57 TEST_CASE(test_status_cannot_resolve) 58 { 59 scoped_test_env env; 60 const path dir = env.create_dir("dir"); 61 const path file = env.create_file("dir/file", 42); 62 const path sym = env.create_symlink("dir/file", "sym"); 63 permissions(dir, perms::none); 64 65 const std::errc set_errc = std::errc::address_in_use; 66 const std::errc perm_errc = std::errc::permission_denied; 67 const std::errc name_too_long_errc = std::errc::filename_too_long; 68 69 struct TestCase { 70 path p; 71 std::errc expect_errc; 72 } const TestCases[] = { 73 {file, perm_errc}, 74 {sym, perm_errc}, 75 {path(std::string(2500, 'a')), name_too_long_errc} 76 }; 77 for (auto& TC : TestCases) 78 { 79 { // test non-throwing case 80 std::error_code ec = std::make_error_code(set_errc); 81 file_status st = status(TC.p, ec); 82 TEST_CHECK(ErrorIs(ec, TC.expect_errc)); 83 TEST_CHECK(st.type() == file_type::none); 84 TEST_CHECK(st.permissions() == perms::unknown); 85 } 86 #ifndef TEST_HAS_NO_EXCEPTIONS 87 { // test throwing case 88 try { 89 status(TC.p); 90 } catch (filesystem_error const& err) { 91 TEST_CHECK(err.path1() == TC.p); 92 TEST_CHECK(err.path2() == ""); 93 TEST_CHECK(ErrorIs(err.code(), TC.expect_errc)); 94 } 95 } 96 #endif 97 } 98 } 99 100 TEST_CASE(status_file_types_test) 101 { 102 static_test_env static_env; 103 scoped_test_env env; 104 struct TestCase { 105 path p; 106 file_type expect_type; 107 } cases[] = { 108 {static_env.File, file_type::regular}, 109 {static_env.SymlinkToFile, file_type::regular}, 110 {static_env.Dir, file_type::directory}, 111 {static_env.SymlinkToDir, file_type::directory}, 112 // Block files tested elsewhere 113 {static_env.CharFile, file_type::character}, 114 #if !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(_WIN32) // No support for domain sockets 115 {env.create_socket("socket"), file_type::socket}, 116 #endif 117 #ifndef _WIN32 118 {env.create_fifo("fifo"), file_type::fifo} 119 #endif 120 }; 121 for (const auto& TC : cases) { 122 // test non-throwing case 123 std::error_code ec = std::make_error_code(std::errc::address_in_use); 124 file_status st = status(TC.p, ec); 125 TEST_CHECK(!ec); 126 TEST_CHECK(st.type() == TC.expect_type); 127 TEST_CHECK(st.permissions() != perms::unknown); 128 // test throwing case 129 TEST_REQUIRE_NO_THROW(st = status(TC.p)); 130 TEST_CHECK(st.type() == TC.expect_type); 131 TEST_CHECK(st.permissions() != perms::unknown); 132 } 133 } 134 135 TEST_CASE(test_block_file) 136 { 137 const path possible_paths[] = { 138 "/dev/drive0", // Apple 139 "/dev/sda", 140 "/dev/loop0" 141 }; 142 path p; 143 for (const path& possible_p : possible_paths) { 144 std::error_code ec; 145 if (exists(possible_p, ec)) { 146 p = possible_p; 147 break; 148 } 149 } 150 if (p == path{}) { 151 TEST_UNSUPPORTED(); 152 } 153 // test non-throwing case 154 std::error_code ec = std::make_error_code(std::errc::address_in_use); 155 file_status st = status(p, ec); 156 TEST_CHECK(!ec); 157 TEST_CHECK(st.type() == file_type::block); 158 TEST_CHECK(st.permissions() != perms::unknown); 159 // test throwing case 160 TEST_REQUIRE_NO_THROW(st = status(p)); 161 TEST_CHECK(st.type() == file_type::block); 162 TEST_CHECK(st.permissions() != perms::unknown); 163 } 164 165 TEST_SUITE_END() 166