1 //===----------------------------------------------------------------------===//
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
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}}
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);
28 #include "test_macros.h"
29 #include "filesystem_test_helper.h"
30 namespace fs
= std::filesystem
;
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
54 fs::permissions(f
, opts
);
56 } catch (filesystem_error
const& err
) {
57 return err
.path1() == f
62 ((void)f
); ((void)opts
); ((void)ec
);
68 const path dne
= env
.make_env_path("dne");
69 const path dne_sym
= env
.create_symlink(dne
, "dne_sym");
71 std::error_code ec
= GetTestEC();
72 fs::permissions(dne
, fs::perms
{}, 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
);
81 assert(ec
!= GetTestEC());
82 assert(checkThrow(dne_sym
, fs::perms
{}, ec
));
86 static void basic_permissions_test()
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
;
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
) {}
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
},
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
);
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
);
138 auto pp
= status(TC
.p
).permissions();
139 assert(pp
== NormalizeExpectedPerms(TC
.expected
));
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()
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();
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
) {}
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
;
174 // On linux symlink permissions are not supported. The error code should
175 // be 'operation_not_supported' and the symlink permissions should be
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
);
180 std::error_code ec
= GetTestEC();
181 permissions(sym
, TC
.set_perms
, TC
.opts
| perm_options::nofollow
, ec
);
183 assert(ErrorIs(ec
, static_cast<std::errc
>(expected_ec
.value())));
186 assert(status(file
).permissions() == file_perms
);
187 assert(symlink_status(sym
).permissions() == expected_link_perms
);
192 int main(int, char**) {
194 test_error_reporting();
195 basic_permissions_test();
197 test_no_resolve_symlink_on_symlink();