a028de4b14ae4fe503dadb75be6eaf7e3e59bb6c
[platform/upstream/llvm.git] /
1 //===----------------------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 // UNSUPPORTED: c++98, c++03
11
12 // <experimental/filesystem>
13
14 // class directory_entry
15
16 // file_status status() const;
17 // file_status status(error_code const&) const noexcept;
18
19 #include "filesystem_include.hpp"
20 #include <type_traits>
21 #include <cassert>
22
23 #include "filesystem_test_helper.hpp"
24 #include "rapid-cxx-test.hpp"
25
26 TEST_SUITE(directory_entry_obs_testsuite)
27
28 TEST_CASE(file_dne) {
29   using namespace fs;
30   directory_entry p("dne");
31 }
32
33 TEST_CASE(signatures) {
34   using namespace fs;
35   const directory_entry e = {};
36   std::error_code ec;
37 #define TEST_FUNC(name)                                                        \
38   static_assert(std::is_same<decltype(e.name()), bool>::value,                 \
39                 "wrong return type");                                          \
40   static_assert(noexcept(e.name()) == false, "should not be noexcept");        \
41   static_assert(std::is_same<decltype(e.name(ec)), bool>::value,               \
42                 "wrong return type");                                          \
43   static_assert(noexcept(e.name(ec)) == true, "should be noexcept")
44
45   TEST_FUNC(exists);
46   TEST_FUNC(is_block_file);
47   TEST_FUNC(is_character_file);
48   TEST_FUNC(is_directory);
49   TEST_FUNC(is_fifo);
50   TEST_FUNC(is_other);
51   TEST_FUNC(is_regular_file);
52   TEST_FUNC(is_socket);
53   TEST_FUNC(is_symlink);
54
55 #undef TEST_FUNC
56 }
57
58 TEST_CASE(test_without_ec) {
59   using namespace fs;
60   using fs::directory_entry;
61   using fs::file_status;
62   using fs::path;
63
64   scoped_test_env env;
65   path f = env.create_file("foo", 42);
66   path d = env.create_dir("dir");
67   path fifo = env.create_fifo("fifo");
68   path hl = env.create_hardlink("foo", "hl");
69   for (auto p : {hl, f, d, fifo}) {
70     directory_entry e(p);
71     file_status st = status(p);
72     file_status sym_st = symlink_status(p);
73     fs::remove(p);
74     TEST_REQUIRE(e.exists());
75     TEST_REQUIRE(!exists(p));
76     TEST_CHECK(e.exists() == exists(st));
77     TEST_CHECK(e.is_block_file() == is_block_file(st));
78     TEST_CHECK(e.is_character_file() == is_character_file(st));
79     TEST_CHECK(e.is_directory() == is_directory(st));
80     TEST_CHECK(e.is_fifo() == is_fifo(st));
81     TEST_CHECK(e.is_other() == is_other(st));
82     TEST_CHECK(e.is_regular_file() == is_regular_file(st));
83     TEST_CHECK(e.is_socket() == is_socket(st));
84     TEST_CHECK(e.is_symlink() == is_symlink(sym_st));
85   }
86 }
87
88 TEST_CASE(test_with_ec) {
89   using namespace fs;
90   using fs::directory_entry;
91   using fs::file_status;
92   using fs::path;
93
94   scoped_test_env env;
95   path f = env.create_file("foo", 42);
96   path d = env.create_dir("dir");
97   path fifo = env.create_fifo("fifo");
98   path hl = env.create_hardlink("foo", "hl");
99   for (auto p : {hl, f, d, fifo}) {
100     directory_entry e(p);
101     std::error_code status_ec = GetTestEC();
102     std::error_code sym_status_ec = GetTestEC(1);
103     file_status st = status(p, status_ec);
104     file_status sym_st = symlink_status(p, sym_status_ec);
105     fs::remove(p);
106     std::error_code ec = GetTestEC(2);
107     auto CheckEC = [&](std::error_code const& other_ec) {
108       bool res = ec == other_ec;
109       ec = GetTestEC(2);
110       return res;
111     };
112
113     TEST_REQUIRE(e.exists(ec));
114     TEST_CHECK(CheckEC(status_ec));
115     TEST_REQUIRE(!exists(p));
116
117     TEST_CHECK(e.exists(ec) == exists(st));
118     TEST_CHECK(CheckEC(status_ec));
119
120     TEST_CHECK(e.is_block_file(ec) == is_block_file(st));
121     TEST_CHECK(CheckEC(status_ec));
122
123     TEST_CHECK(e.is_character_file(ec) == is_character_file(st));
124     TEST_CHECK(CheckEC(status_ec));
125
126     TEST_CHECK(e.is_directory(ec) == is_directory(st));
127     TEST_CHECK(CheckEC(status_ec));
128
129     TEST_CHECK(e.is_fifo(ec) == is_fifo(st));
130     TEST_CHECK(CheckEC(status_ec));
131
132     TEST_CHECK(e.is_other(ec) == is_other(st));
133     TEST_CHECK(CheckEC(status_ec));
134
135     TEST_CHECK(e.is_regular_file(ec) == is_regular_file(st));
136     TEST_CHECK(CheckEC(status_ec));
137
138     TEST_CHECK(e.is_socket(ec) == is_socket(st));
139     TEST_CHECK(CheckEC(status_ec));
140
141     TEST_CHECK(e.is_symlink(ec) == is_symlink(sym_st));
142     TEST_CHECK(CheckEC(sym_status_ec));
143   }
144 }
145
146 TEST_CASE(test_with_ec_dne) {
147   using namespace fs;
148   using fs::directory_entry;
149   using fs::file_status;
150   using fs::path;
151
152   for (auto p : {StaticEnv::DNE, StaticEnv::BadSymlink}) {
153
154     directory_entry e(p);
155     std::error_code status_ec = GetTestEC();
156     std::error_code sym_status_ec = GetTestEC(1);
157     file_status st = status(p, status_ec);
158     file_status sym_st = symlink_status(p, sym_status_ec);
159     std::error_code ec = GetTestEC(2);
160     auto CheckEC = [&](std::error_code const& other_ec) {
161       bool res = ec == other_ec;
162       ec = GetTestEC(2);
163       return res;
164     };
165
166     TEST_CHECK(e.exists(ec) == exists(st));
167     TEST_CHECK(CheckEC(status_ec));
168
169     TEST_CHECK(e.is_block_file(ec) == is_block_file(st));
170     TEST_CHECK(CheckEC(status_ec));
171
172     TEST_CHECK(e.is_character_file(ec) == is_character_file(st));
173     TEST_CHECK(CheckEC(status_ec));
174
175     TEST_CHECK(e.is_directory(ec) == is_directory(st));
176     TEST_CHECK(CheckEC(status_ec));
177
178     TEST_CHECK(e.is_fifo(ec) == is_fifo(st));
179     TEST_CHECK(CheckEC(status_ec));
180
181     TEST_CHECK(e.is_other(ec) == is_other(st));
182     TEST_CHECK(CheckEC(status_ec));
183
184     TEST_CHECK(e.is_regular_file(ec) == is_regular_file(st));
185     TEST_CHECK(CheckEC(status_ec));
186
187     TEST_CHECK(e.is_socket(ec) == is_socket(st));
188     TEST_CHECK(CheckEC(status_ec));
189
190     TEST_CHECK(e.is_symlink(ec) == is_symlink(sym_st));
191     TEST_CHECK(CheckEC(sym_status_ec));
192   }
193 }
194
195 TEST_CASE(test_with_ec_cannot_resolve) {
196   using namespace fs;
197   using fs::directory_entry;
198   using fs::file_status;
199   using fs::path;
200
201   scoped_test_env env;
202   const path dir = env.create_dir("dir");
203   const path file = env.create_file("dir/file", 42);
204   const path file_out_of_dir = env.create_file("file2", 99);
205   const path sym = env.create_symlink("file2", "dir/sym");
206
207   perms old_perms = fs::status(dir).permissions();
208
209   for (auto p : {file, sym}) {
210     permissions(dir, old_perms);
211     directory_entry e(p);
212
213     permissions(dir, perms::none);
214     std::error_code dummy_ec;
215     e.refresh(dummy_ec);
216     TEST_REQUIRE(dummy_ec);
217
218     std::error_code status_ec = GetTestEC();
219     std::error_code sym_status_ec = GetTestEC(1);
220     file_status st = status(p, status_ec);
221     file_status sym_st = symlink_status(p, sym_status_ec);
222     std::error_code ec = GetTestEC(2);
223     auto CheckEC = [&](std::error_code const& other_ec) {
224       bool res = ec == other_ec;
225       ec = GetTestEC(2);
226       return res;
227     };
228
229     TEST_CHECK(e.exists(ec) == exists(st));
230     TEST_CHECK(CheckEC(status_ec));
231
232     TEST_CHECK(e.is_block_file(ec) == is_block_file(st));
233     TEST_CHECK(CheckEC(status_ec));
234
235     TEST_CHECK(e.is_character_file(ec) == is_character_file(st));
236     TEST_CHECK(CheckEC(status_ec));
237
238     TEST_CHECK(e.is_directory(ec) == is_directory(st));
239     TEST_CHECK(CheckEC(status_ec));
240
241     TEST_CHECK(e.is_fifo(ec) == is_fifo(st));
242     TEST_CHECK(CheckEC(status_ec));
243
244     TEST_CHECK(e.is_other(ec) == is_other(st));
245     TEST_CHECK(CheckEC(status_ec));
246
247     TEST_CHECK(e.is_regular_file(ec) == is_regular_file(st));
248     TEST_CHECK(CheckEC(status_ec));
249
250     TEST_CHECK(e.is_socket(ec) == is_socket(st));
251     TEST_CHECK(CheckEC(status_ec));
252
253     TEST_CHECK(e.is_symlink(ec) == is_symlink(sym_st));
254     TEST_CHECK(CheckEC(sym_status_ec));
255   }
256 }
257
258 TEST_SUITE_END()