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
TEST_CASE(signature_test)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
TEST_CASE(test_status_not_found)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 // Windows doesn't support setting perms::none to trigger failures
58 // reading directories. Imaginary files under GetWindowsInaccessibleDir()
59 // produce no_such_file_or_directory, not the error codes this test checks
60 // for. Finally, status() for a too long file name doesn't return errors
61 // on windows.
62 #ifndef TEST_WIN_NO_FILESYSTEM_PERMS_NONE
TEST_CASE(test_status_cannot_resolve)63 TEST_CASE(test_status_cannot_resolve)
64 {
65 scoped_test_env env;
66 const path dir = env.create_dir("dir");
67 const path file = env.create_file("dir/file", 42);
68 const path sym = env.create_symlink("dir/file", "sym");
69 permissions(dir, perms::none);
70
71 const std::errc set_errc = std::errc::address_in_use;
72 const std::errc perm_errc = std::errc::permission_denied;
73 const std::errc name_too_long_errc = std::errc::filename_too_long;
74
75 struct TestCase {
76 path p;
77 std::errc expect_errc;
78 } const TestCases[] = {
79 {file, perm_errc},
80 {sym, perm_errc},
81 {path(std::string(2500, 'a')), name_too_long_errc}
82 };
83 for (auto& TC : TestCases)
84 {
85 { // test non-throwing case
86 std::error_code ec = std::make_error_code(set_errc);
87 file_status st = status(TC.p, ec);
88 TEST_CHECK(ErrorIs(ec, TC.expect_errc));
89 TEST_CHECK(st.type() == file_type::none);
90 TEST_CHECK(st.permissions() == perms::unknown);
91 }
92 #ifndef TEST_HAS_NO_EXCEPTIONS
93 { // test throwing case
94 try {
95 status(TC.p);
96 } catch (filesystem_error const& err) {
97 TEST_CHECK(err.path1() == TC.p);
98 TEST_CHECK(err.path2() == "");
99 TEST_CHECK(ErrorIs(err.code(), TC.expect_errc));
100 }
101 }
102 #endif
103 }
104 }
105 #endif
106
TEST_CASE(status_file_types_test)107 TEST_CASE(status_file_types_test)
108 {
109 static_test_env static_env;
110 scoped_test_env env;
111 struct TestCase {
112 path p;
113 file_type expect_type;
114 } cases[] = {
115 {static_env.File, file_type::regular},
116 {static_env.SymlinkToFile, file_type::regular},
117 {static_env.Dir, file_type::directory},
118 {static_env.SymlinkToDir, file_type::directory},
119 // file_type::block files tested elsewhere
120 #ifndef _WIN32
121 {static_env.CharFile, file_type::character},
122 #endif
123 #if !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(_WIN32) // No support for domain sockets
124 {env.create_socket("socket"), file_type::socket},
125 #endif
126 #ifndef _WIN32
127 {env.create_fifo("fifo"), file_type::fifo}
128 #endif
129 };
130 for (const auto& TC : cases) {
131 // test non-throwing case
132 std::error_code ec = std::make_error_code(std::errc::address_in_use);
133 file_status st = status(TC.p, ec);
134 TEST_CHECK(!ec);
135 TEST_CHECK(st.type() == TC.expect_type);
136 TEST_CHECK(st.permissions() != perms::unknown);
137 // test throwing case
138 TEST_REQUIRE_NO_THROW(st = status(TC.p));
139 TEST_CHECK(st.type() == TC.expect_type);
140 TEST_CHECK(st.permissions() != perms::unknown);
141 }
142 }
143
TEST_CASE(test_block_file)144 TEST_CASE(test_block_file)
145 {
146 const path possible_paths[] = {
147 "/dev/drive0", // Apple
148 "/dev/sda",
149 "/dev/loop0"
150 };
151 path p;
152 for (const path& possible_p : possible_paths) {
153 std::error_code ec;
154 if (exists(possible_p, ec)) {
155 p = possible_p;
156 break;
157 }
158 }
159 if (p == path{}) {
160 TEST_UNSUPPORTED();
161 }
162 // test non-throwing case
163 std::error_code ec = std::make_error_code(std::errc::address_in_use);
164 file_status st = status(p, ec);
165 TEST_CHECK(!ec);
166 TEST_CHECK(st.type() == file_type::block);
167 TEST_CHECK(st.permissions() != perms::unknown);
168 // test throwing case
169 TEST_REQUIRE_NO_THROW(st = status(p));
170 TEST_CHECK(st.type() == file_type::block);
171 TEST_CHECK(st.permissions() != perms::unknown);
172 }
173
174 TEST_SUITE_END()
175