[RISCV] Fix the cost of `llvm.vector.reduce.and` (#119160)
[llvm-project.git] / libcxx / test / std / input.output / filesystems / fs.op.funcs / fs.op.permissions / permissions.pass.cpp
blob085fa65e3559f5d3267ef81dd997ebd4d2b28809
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 //===----------------------------------------------------------------------===//
9 // REQUIRES: can-create-symlinks
10 // UNSUPPORTED: c++03, c++11, c++14
11 // UNSUPPORTED: no-filesystem
12 // UNSUPPORTED: availability-filesystem-missing
14 // Android's fchmodat seems broken on various OS versions -- see D140183. This
15 // test probably passes on new-enough phones (not the emulator).
16 // XFAIL: LIBCXX-ANDROID-FIXME && target={{i686|x86_64}}-{{.+}}-android{{.*}}
17 // XFAIL: LIBCXX-ANDROID-FIXME && android-device-api={{21|22}}
19 // <filesystem>
21 // void permissions(const path& p, perms prms,
22 // perm_options opts = perm_options::replace);
23 // void permissions(const path& p, perms prms, std::error_code& ec) noexcept;
24 // void permissions(const path& p, perms prms, perm_options opts, std::error_code);
26 #include <filesystem>
28 #include "test_macros.h"
29 #include "filesystem_test_helper.h"
30 namespace fs = std::filesystem;
31 using namespace fs;
33 using PR = fs::perms;
35 static void test_signatures()
37 const path p; ((void)p);
38 const perms pr{}; ((void)pr);
39 const perm_options opts{}; ((void)opts);
40 std::error_code ec; ((void)ec);
41 ASSERT_NOT_NOEXCEPT(fs::permissions(p, pr));
42 ASSERT_NOT_NOEXCEPT(fs::permissions(p, pr, opts));
43 ASSERT_NOEXCEPT(fs::permissions(p, pr, ec));
44 LIBCPP_ASSERT_NOT_NOEXCEPT(fs::permissions(p, pr, opts, ec));
47 static void test_error_reporting()
49 auto checkThrow = [](path const& f, fs::perms opts,
50 const std::error_code& ec)
52 #ifndef TEST_HAS_NO_EXCEPTIONS
53 try {
54 fs::permissions(f, opts);
55 return false;
56 } catch (filesystem_error const& err) {
57 return err.path1() == f
58 && err.path2() == ""
59 && err.code() == ec;
61 #else
62 ((void)f); ((void)opts); ((void)ec);
63 return true;
64 #endif
67 scoped_test_env env;
68 const path dne = env.make_env_path("dne");
69 const path dne_sym = env.create_symlink(dne, "dne_sym");
70 { // !exists
71 std::error_code ec = GetTestEC();
72 fs::permissions(dne, fs::perms{}, ec);
73 assert(ec);
74 assert(ec != GetTestEC());
75 assert(checkThrow(dne, fs::perms{}, ec));
78 std::error_code ec = GetTestEC();
79 fs::permissions(dne_sym, fs::perms{}, ec);
80 assert(ec);
81 assert(ec != GetTestEC());
82 assert(checkThrow(dne_sym, fs::perms{}, ec));
86 static void basic_permissions_test()
88 scoped_test_env env;
89 const path file = env.create_file("file1", 42);
90 const path dir = env.create_dir("dir1");
91 const path file_for_sym = env.create_file("file2", 42);
92 const path sym = env.create_symlink(file_for_sym, "sym");
93 const perm_options AP = perm_options::add;
94 const perm_options RP = perm_options::remove;
95 const perm_options NF = perm_options::nofollow;
96 struct TestCase {
97 path p;
98 perms set_perms;
99 perms expected;
100 perm_options opts;
101 TestCase(path xp, perms xperms, perms xexpect,
102 perm_options xopts = perm_options::replace)
103 : p(xp), set_perms(xperms), expected(xexpect), opts(xopts) {}
104 } cases[] = {
105 // test file
106 {file, perms::none, perms::none},
107 {file, perms::owner_all, perms::owner_all},
108 {file, perms::group_all, perms::owner_all | perms::group_all, AP},
109 {file, perms::group_all, perms::owner_all, RP},
110 // test directory
111 {dir, perms::none, perms::none},
112 {dir, perms::owner_all, perms::owner_all},
113 {dir, perms::group_all, perms::owner_all | perms::group_all, AP},
114 {dir, perms::group_all, perms::owner_all, RP},
115 // test symlink without symlink_nofollow
116 {sym, perms::none, perms::none},
117 {sym, perms::owner_all, perms::owner_all},
118 {sym, perms::group_all, perms::owner_all | perms::group_all, AP},
119 {sym, perms::group_all, perms::owner_all, RP},
120 // test non-symlink with symlink_nofollow. The last test on file/dir
121 // will have set their permissions to perms::owner_all
122 {file, perms::group_all, perms::owner_all | perms::group_all, AP | NF},
123 {dir, perms::group_all, perms::owner_all | perms::group_all, AP | NF}
125 for (auto const& TC : cases) {
126 assert(status(TC.p).permissions() != TC.expected);
128 std::error_code ec = GetTestEC();
129 permissions(TC.p, TC.set_perms, TC.opts, ec);
130 assert(!ec);
131 auto pp = status(TC.p).permissions();
132 assert(pp == NormalizeExpectedPerms(TC.expected));
134 if (TC.opts == perm_options::replace) {
135 std::error_code ec = GetTestEC();
136 permissions(TC.p, TC.set_perms, ec);
137 assert(!ec);
138 auto pp = status(TC.p).permissions();
139 assert(pp == NormalizeExpectedPerms(TC.expected));
144 #ifndef _WIN32
145 // This test isn't currently meaningful on Windows; the Windows file
146 // permissions visible via std::filesystem doesn't show any difference
147 // between owner/group/others.
148 static void test_no_resolve_symlink_on_symlink()
150 scoped_test_env env;
151 const path file = env.create_file("file", 42);
152 const path sym = env.create_symlink(file, "sym");
153 const auto file_perms = status(file).permissions();
155 struct TestCase {
156 perms set_perms;
157 perms expected; // only expected on platform that support symlink perms.
158 perm_options opts = perm_options::replace;
159 TestCase(perms xperms, perms xexpect,
160 perm_options xopts = perm_options::replace)
161 : set_perms(xperms), expected(xexpect), opts(xopts) {}
162 } cases[] = {
163 {perms::owner_all, perms::owner_all},
164 {perms::group_all, perms::owner_all | perms::group_all, perm_options::add},
165 {perms::owner_all, perms::group_all, perm_options::remove},
167 for (auto const& TC : cases) {
168 #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(_AIX)
169 // On OS X symlink permissions are supported. We should get an empty
170 // error code and the expected permissions.
171 const auto expected_link_perms = TC.expected;
172 std::error_code expected_ec;
173 #else
174 // On linux symlink permissions are not supported. The error code should
175 // be 'operation_not_supported' and the symlink permissions should be
176 // unchanged.
177 const auto expected_link_perms = symlink_status(sym).permissions();
178 std::error_code expected_ec = std::make_error_code(std::errc::operation_not_supported);
179 #endif
180 std::error_code ec = GetTestEC();
181 permissions(sym, TC.set_perms, TC.opts | perm_options::nofollow, ec);
182 if (expected_ec)
183 assert(ErrorIs(ec, static_cast<std::errc>(expected_ec.value())));
184 else
185 assert(!ec);
186 assert(status(file).permissions() == file_perms);
187 assert(symlink_status(sym).permissions() == expected_link_perms);
190 #endif // _WIN32
192 int main(int, char**) {
193 test_signatures();
194 test_error_reporting();
195 basic_permissions_test();
196 #ifndef _WIN32
197 test_no_resolve_symlink_on_symlink();
198 #endif
199 return 0;