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 //===----------------------------------------------------------------------===//
10 // UNSUPPORTED: no-filesystem
11 // UNSUPPORTED: availability-filesystem-missing
13 // Android's fchmodat seems broken on various OS versions -- see D140183. This
14 // test probably passes on new-enough phones (not the emulator).
15 // XFAIL: LIBCXX-ANDROID-FIXME && target={{i686|x86_64}}-{{.+}}-android{{.*}}
16 // XFAIL: LIBCXX-ANDROID-FIXME && android-device-api={{21|22}}
20 // void permissions(const path& p, perms prms,
21 // perm_options opts = perm_options::replace);
22 // void permissions(const path& p, perms prms, std::error_code& ec) noexcept;
23 // void permissions(const path& p, perms prms, perm_options opts, std::error_code);
27 #include "filesystem_include.h"
29 #include "test_macros.h"
30 #include "filesystem_test_helper.h"
36 static void test_signatures()
38 const path p
; ((void)p
);
39 const perms pr
{}; ((void)pr
);
40 const perm_options opts
{}; ((void)opts
);
41 std::error_code ec
; ((void)ec
);
42 ASSERT_NOT_NOEXCEPT(fs::permissions(p
, pr
));
43 ASSERT_NOT_NOEXCEPT(fs::permissions(p
, pr
, opts
));
44 ASSERT_NOEXCEPT(fs::permissions(p
, pr
, ec
));
45 LIBCPP_ONLY(ASSERT_NOT_NOEXCEPT(fs::permissions(p
, pr
, opts
, ec
)));
48 static void test_error_reporting()
50 auto checkThrow
= [](path
const& f
, fs::perms opts
,
51 const std::error_code
& ec
)
53 #ifndef TEST_HAS_NO_EXCEPTIONS
55 fs::permissions(f
, opts
);
57 } catch (filesystem_error
const& err
) {
58 return err
.path1() == f
63 ((void)f
); ((void)opts
); ((void)ec
);
69 const path dne
= env
.make_env_path("dne");
70 const path dne_sym
= env
.create_symlink(dne
, "dne_sym");
72 std::error_code ec
= GetTestEC();
73 fs::permissions(dne
, fs::perms
{}, ec
);
75 assert(ec
!= GetTestEC());
76 assert(checkThrow(dne
, fs::perms
{}, ec
));
79 std::error_code ec
= GetTestEC();
80 fs::permissions(dne_sym
, fs::perms
{}, ec
);
82 assert(ec
!= GetTestEC());
83 assert(checkThrow(dne_sym
, fs::perms
{}, ec
));
87 static void basic_permissions_test()
90 const path file
= env
.create_file("file1", 42);
91 const path dir
= env
.create_dir("dir1");
92 const path file_for_sym
= env
.create_file("file2", 42);
93 const path sym
= env
.create_symlink(file_for_sym
, "sym");
94 const perm_options AP
= perm_options::add
;
95 const perm_options RP
= perm_options::remove
;
96 const perm_options NF
= perm_options::nofollow
;
102 TestCase(path xp
, perms xperms
, perms xexpect
,
103 perm_options xopts
= perm_options::replace
)
104 : p(xp
), set_perms(xperms
), expected(xexpect
), opts(xopts
) {}
107 {file
, perms::none
, perms::none
},
108 {file
, perms::owner_all
, perms::owner_all
},
109 {file
, perms::group_all
, perms::owner_all
| perms::group_all
, AP
},
110 {file
, perms::group_all
, perms::owner_all
, RP
},
112 {dir
, perms::none
, perms::none
},
113 {dir
, perms::owner_all
, perms::owner_all
},
114 {dir
, perms::group_all
, perms::owner_all
| perms::group_all
, AP
},
115 {dir
, perms::group_all
, perms::owner_all
, RP
},
116 // test symlink without symlink_nofollow
117 {sym
, perms::none
, perms::none
},
118 {sym
, perms::owner_all
, perms::owner_all
},
119 {sym
, perms::group_all
, perms::owner_all
| perms::group_all
, AP
},
120 {sym
, perms::group_all
, perms::owner_all
, RP
},
121 // test non-symlink with symlink_nofollow. The last test on file/dir
122 // will have set their permissions to perms::owner_all
123 {file
, perms::group_all
, perms::owner_all
| perms::group_all
, AP
| NF
},
124 {dir
, perms::group_all
, perms::owner_all
| perms::group_all
, AP
| NF
}
126 for (auto const& TC
: cases
) {
127 assert(status(TC
.p
).permissions() != TC
.expected
);
129 std::error_code ec
= GetTestEC();
130 permissions(TC
.p
, TC
.set_perms
, TC
.opts
, ec
);
132 auto pp
= status(TC
.p
).permissions();
133 assert(pp
== NormalizeExpectedPerms(TC
.expected
));
135 if (TC
.opts
== perm_options::replace
) {
136 std::error_code ec
= GetTestEC();
137 permissions(TC
.p
, TC
.set_perms
, ec
);
139 auto pp
= status(TC
.p
).permissions();
140 assert(pp
== NormalizeExpectedPerms(TC
.expected
));
146 // This test isn't currently meaningful on Windows; the Windows file
147 // permissions visible via std::filesystem doesn't show any difference
148 // between owner/group/others.
149 static void test_no_resolve_symlink_on_symlink()
152 const path file
= env
.create_file("file", 42);
153 const path sym
= env
.create_symlink(file
, "sym");
154 const auto file_perms
= status(file
).permissions();
158 perms expected
; // only expected on platform that support symlink perms.
159 perm_options opts
= perm_options::replace
;
160 TestCase(perms xperms
, perms xexpect
,
161 perm_options xopts
= perm_options::replace
)
162 : set_perms(xperms
), expected(xexpect
), opts(xopts
) {}
164 {perms::owner_all
, perms::owner_all
},
165 {perms::group_all
, perms::owner_all
| perms::group_all
, perm_options::add
},
166 {perms::owner_all
, perms::group_all
, perm_options::remove
},
168 for (auto const& TC
: cases
) {
169 #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(_AIX)
170 // On OS X symlink permissions are supported. We should get an empty
171 // error code and the expected permissions.
172 const auto expected_link_perms
= TC
.expected
;
173 std::error_code expected_ec
;
175 // On linux symlink permissions are not supported. The error code should
176 // be 'operation_not_supported' and the symlink permissions should be
178 const auto expected_link_perms
= symlink_status(sym
).permissions();
179 std::error_code expected_ec
= std::make_error_code(std::errc::operation_not_supported
);
181 std::error_code ec
= GetTestEC();
182 permissions(sym
, TC
.set_perms
, TC
.opts
| perm_options::nofollow
, ec
);
184 assert(ErrorIs(ec
, static_cast<std::errc
>(expected_ec
.value())));
187 assert(status(file
).permissions() == file_perms
);
188 assert(symlink_status(sym
).permissions() == expected_link_perms
);
193 int main(int, char**) {
195 test_error_reporting();
196 basic_permissions_test();
198 test_no_resolve_symlink_on_symlink();