d11457d20ae49a01ff6ad0a8e0b89915db8808b5
[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 // uintmax_t hard_link_count() const;
17 // uintmax_t hard_link_count(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(signatures) {
29   using namespace fs;
30   {
31     const directory_entry e = {};
32     std::error_code ec;
33     static_assert(std::is_same<decltype(e.hard_link_count()), uintmax_t>::value, "");
34     static_assert(std::is_same<decltype(e.hard_link_count(ec)), uintmax_t>::value,
35                   "");
36     static_assert(noexcept(e.hard_link_count()) == false, "");
37     static_assert(noexcept(e.hard_link_count(ec)) == true, "");
38   }
39 }
40
41 TEST_CASE(basic) {
42   using namespace fs;
43
44   scoped_test_env env;
45   const path file = env.create_file("file", 42);
46   const path dir = env.create_dir("dir");
47   const path sym = env.create_symlink("file", "sym");
48
49   {
50     directory_entry ent(file);
51     uintmax_t expect = hard_link_count(ent);
52
53     // Remove the file to show that the results were already in the cache.
54     LIBCPP_ONLY(remove(file));
55
56     std::error_code ec = GetTestEC();
57     TEST_CHECK(ent.hard_link_count(ec) == expect);
58     TEST_CHECK(!ec);
59   }
60   {
61     directory_entry ent(dir);
62     uintmax_t expect = hard_link_count(ent);
63
64     LIBCPP_ONLY(remove(dir));
65
66     std::error_code ec = GetTestEC();
67     TEST_CHECK(ent.hard_link_count(ec) == expect);
68     TEST_CHECK(!ec);
69   }
70   env.create_file("file", 99);
71   env.create_hardlink("file", "hl");
72   {
73     directory_entry ent(sym);
74     std::error_code ec = GetTestEC();
75     TEST_CHECK(ent.hard_link_count(ec) == 2);
76     TEST_CHECK(!ec);
77   }
78 }
79
80 TEST_CASE(not_regular_file) {
81   using namespace fs;
82
83   scoped_test_env env;
84   const path dir = env.create_dir("dir");
85   const path dir2 = env.create_dir("dir/dir2");
86   const path fifo = env.create_fifo("dir/fifo");
87   const path sym_to_fifo = env.create_symlink("dir/fifo", "dir/sym");
88
89   const perms old_perms = status(dir).permissions();
90
91   for (auto p : {dir2, fifo, sym_to_fifo}) {
92     permissions(dir, old_perms);
93     std::error_code dummy_ec = GetTestEC();
94     directory_entry ent(p, dummy_ec);
95     TEST_CHECK(!dummy_ec);
96
97     uintmax_t expect = hard_link_count(p);
98
99     LIBCPP_ONLY(permissions(dir, perms::none));
100
101     std::error_code ec = GetTestEC();
102     TEST_CHECK(ent.hard_link_count(ec) == expect);
103     TEST_CHECK(!ec);
104     TEST_CHECK_NO_THROW(ent.hard_link_count());
105   }
106 }
107
108 TEST_CASE(error_reporting) {
109   using namespace fs;
110
111   scoped_test_env env;
112
113   const path dir = env.create_dir("dir");
114   const path file = env.create_file("dir/file", 42);
115   const path file_out_of_dir = env.create_file("file2", 101);
116   const path sym_out_of_dir = env.create_symlink("dir/file", "sym");
117   const path sym_in_dir = env.create_symlink("file2", "dir/sym2");
118
119   const perms old_perms = status(dir).permissions();
120
121   // test a file which doesn't exist
122   {
123     directory_entry ent;
124
125     std::error_code ec = GetTestEC();
126     ent.assign(StaticEnv::DNE, ec);
127     TEST_CHECK(ec);
128     TEST_REQUIRE(ent.path() == StaticEnv::DNE);
129     TEST_CHECK(ErrorIs(ec, std::errc::no_such_file_or_directory));
130
131     ec = GetTestEC();
132     TEST_CHECK(ent.hard_link_count(ec) == uintmax_t(-1));
133     TEST_CHECK(ErrorIs(ec, std::errc::no_such_file_or_directory));
134
135     ExceptionChecker Checker(StaticEnv::DNE,
136                              std::errc::no_such_file_or_directory,
137                              "directory_entry::hard_link_count");
138     TEST_CHECK_THROW_RESULT(filesystem_error, Checker, ent.hard_link_count());
139   }
140   // test a dead symlink
141   {
142     directory_entry ent;
143
144     std::error_code ec = GetTestEC();
145     uintmax_t expect_bad = hard_link_count(StaticEnv::BadSymlink, ec);
146     TEST_CHECK(expect_bad == uintmax_t(-1));
147     TEST_CHECK(ErrorIs(ec, std::errc::no_such_file_or_directory));
148
149     ec = GetTestEC();
150     ent.assign(StaticEnv::BadSymlink, ec);
151     TEST_REQUIRE(ent.path() == StaticEnv::BadSymlink);
152     TEST_CHECK(!ec);
153
154     ec = GetTestEC();
155     TEST_CHECK(ent.hard_link_count(ec) == expect_bad);
156     TEST_CHECK(ErrorIs(ec, std::errc::no_such_file_or_directory));
157
158     ExceptionChecker Checker(StaticEnv::BadSymlink,
159                              std::errc::no_such_file_or_directory,
160                              "directory_entry::hard_link_count");
161     TEST_CHECK_THROW_RESULT(filesystem_error, Checker, ent.hard_link_count());
162   }
163   // test a file w/o appropriate permissions.
164   {
165     directory_entry ent;
166     uintmax_t expect_good = hard_link_count(file);
167     permissions(dir, perms::none);
168
169     std::error_code ec = GetTestEC();
170     ent.assign(file, ec);
171     TEST_REQUIRE(ent.path() == file);
172     TEST_CHECK(ErrorIs(ec, std::errc::permission_denied));
173
174     ec = GetTestEC();
175     TEST_CHECK(ent.hard_link_count(ec) == uintmax_t(-1));
176     TEST_CHECK(ErrorIs(ec, std::errc::permission_denied));
177
178     ExceptionChecker Checker(file, std::errc::permission_denied,
179                              "hard_link_count");
180     TEST_CHECK_THROW_RESULT(filesystem_error, Checker, ent.hard_link_count());
181
182     permissions(dir, old_perms);
183     ec = GetTestEC();
184     TEST_CHECK(ent.hard_link_count(ec) == expect_good);
185     TEST_CHECK(!ec);
186     TEST_CHECK_NO_THROW(ent.hard_link_count());
187   }
188   permissions(dir, old_perms);
189   // test a symlink w/o appropriate permissions.
190   {
191     directory_entry ent;
192     uintmax_t expect_good = hard_link_count(sym_in_dir);
193     permissions(dir, perms::none);
194
195     std::error_code ec = GetTestEC();
196     ent.assign(sym_in_dir, ec);
197     TEST_REQUIRE(ent.path() == sym_in_dir);
198     TEST_CHECK(ErrorIs(ec, std::errc::permission_denied));
199
200     ec = GetTestEC();
201     TEST_CHECK(ent.hard_link_count(ec) == uintmax_t(-1));
202     TEST_CHECK(ErrorIs(ec, std::errc::permission_denied));
203
204     ExceptionChecker Checker(sym_in_dir, std::errc::permission_denied,
205                              "hard_link_count");
206     TEST_CHECK_THROW_RESULT(filesystem_error, Checker, ent.hard_link_count());
207
208     permissions(dir, old_perms);
209     ec = GetTestEC();
210     TEST_CHECK(ent.hard_link_count(ec) == expect_good);
211     TEST_CHECK(!ec);
212     TEST_CHECK_NO_THROW(ent.hard_link_count());
213   }
214   permissions(dir, old_perms);
215   // test a symlink to a file w/o appropriate permissions
216   {
217     directory_entry ent;
218     uintmax_t expect_good = hard_link_count(sym_out_of_dir);
219     permissions(dir, perms::none);
220
221     std::error_code ec = GetTestEC();
222     ent.assign(sym_out_of_dir, ec);
223     TEST_REQUIRE(ent.path() == sym_out_of_dir);
224     TEST_CHECK(!ec);
225
226     ec = GetTestEC();
227     TEST_CHECK(ent.hard_link_count(ec) == uintmax_t(-1));
228     TEST_CHECK(ErrorIs(ec, std::errc::permission_denied));
229
230     ExceptionChecker Checker(sym_out_of_dir, std::errc::permission_denied,
231                              "hard_link_count");
232     TEST_CHECK_THROW_RESULT(filesystem_error, Checker, ent.hard_link_count());
233
234     permissions(dir, old_perms);
235     ec = GetTestEC();
236     TEST_CHECK(ent.hard_link_count(ec) == expect_good);
237     TEST_CHECK(!ec);
238     TEST_CHECK_NO_THROW(ent.hard_link_count());
239   }
240 }
241
242 TEST_SUITE_END()