200 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9// REQUIRES: can-create-symlinks10// UNSUPPORTED: c++03, c++11, c++1411// UNSUPPORTED: no-filesystem12 13// Android's fchmodat seems broken on various OS versions -- see D140183. This14// 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}}17 18// <filesystem>19 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);24 25#include <filesystem>26 27#include "test_macros.h"28#include "filesystem_test_helper.h"29namespace fs = std::filesystem;30using namespace fs;31 32using PR = fs::perms;33 34static void test_signatures()35{36 const path p; ((void)p);37 const perms pr{}; ((void)pr);38 const perm_options opts{}; ((void)opts);39 std::error_code ec; ((void)ec);40 ASSERT_NOT_NOEXCEPT(fs::permissions(p, pr));41 ASSERT_NOT_NOEXCEPT(fs::permissions(p, pr, opts));42 ASSERT_NOEXCEPT(fs::permissions(p, pr, ec));43 LIBCPP_ASSERT_NOT_NOEXCEPT(fs::permissions(p, pr, opts, ec));44}45 46static void test_error_reporting()47{48 auto checkThrow = [](path const& f, fs::perms opts,49 const std::error_code& ec)50 {51#ifndef TEST_HAS_NO_EXCEPTIONS52 try {53 fs::permissions(f, opts);54 return false;55 } catch (filesystem_error const& err) {56 return err.path1() == f57 && err.path2() == ""58 && err.code() == ec;59 }60#else61 ((void)f); ((void)opts); ((void)ec);62 return true;63#endif64 };65 66 scoped_test_env env;67 const path dne = env.make_env_path("dne");68 const path dne_sym = env.create_symlink(dne, "dne_sym");69 { // !exists70 std::error_code ec = GetTestEC();71 fs::permissions(dne, fs::perms{}, ec);72 assert(ec);73 assert(ec != GetTestEC());74 assert(checkThrow(dne, fs::perms{}, ec));75 }76 {77 std::error_code ec = GetTestEC();78 fs::permissions(dne_sym, fs::perms{}, ec);79 assert(ec);80 assert(ec != GetTestEC());81 assert(checkThrow(dne_sym, fs::perms{}, ec));82 }83}84 85static void basic_permissions_test()86{87 scoped_test_env env;88 const path file = env.create_file("file1", 42);89 const path dir = env.create_dir("dir1");90 const path file_for_sym = env.create_file("file2", 42);91 const path sym = env.create_symlink(file_for_sym, "sym");92 const perm_options AP = perm_options::add;93 const perm_options RP = perm_options::remove;94 const perm_options NF = perm_options::nofollow;95 struct TestCase {96 path p;97 perms set_perms;98 perms expected;99 perm_options opts;100 TestCase(path xp, perms xperms, perms xexpect,101 perm_options xopts = perm_options::replace)102 : p(xp), set_perms(xperms), expected(xexpect), opts(xopts) {}103 } cases[] = {104 // test file105 {file, perms::none, perms::none},106 {file, perms::owner_all, perms::owner_all},107 {file, perms::group_all, perms::owner_all | perms::group_all, AP},108 {file, perms::group_all, perms::owner_all, RP},109 // test directory110 {dir, perms::none, perms::none},111 {dir, perms::owner_all, perms::owner_all},112 {dir, perms::group_all, perms::owner_all | perms::group_all, AP},113 {dir, perms::group_all, perms::owner_all, RP},114 // test symlink without symlink_nofollow115 {sym, perms::none, perms::none},116 {sym, perms::owner_all, perms::owner_all},117 {sym, perms::group_all, perms::owner_all | perms::group_all, AP},118 {sym, perms::group_all, perms::owner_all, RP},119 // test non-symlink with symlink_nofollow. The last test on file/dir120 // will have set their permissions to perms::owner_all121 {file, perms::group_all, perms::owner_all | perms::group_all, AP | NF},122 {dir, perms::group_all, perms::owner_all | perms::group_all, AP | NF}123 };124 for (auto const& TC : cases) {125 assert(status(TC.p).permissions() != TC.expected);126 {127 std::error_code ec = GetTestEC();128 permissions(TC.p, TC.set_perms, TC.opts, ec);129 assert(!ec);130 auto pp = status(TC.p).permissions();131 assert(pp == NormalizeExpectedPerms(TC.expected));132 }133 if (TC.opts == perm_options::replace) {134 std::error_code ec = GetTestEC();135 permissions(TC.p, TC.set_perms, ec);136 assert(!ec);137 auto pp = status(TC.p).permissions();138 assert(pp == NormalizeExpectedPerms(TC.expected));139 }140 }141}142 143#ifndef _WIN32144// This test isn't currently meaningful on Windows; the Windows file145// permissions visible via std::filesystem doesn't show any difference146// between owner/group/others.147static void test_no_resolve_symlink_on_symlink()148{149 scoped_test_env env;150 const path file = env.create_file("file", 42);151 const path sym = env.create_symlink(file, "sym");152 const auto file_perms = status(file).permissions();153 154 struct TestCase {155 perms set_perms;156 perms expected; // only expected on platform that support symlink perms.157 perm_options opts = perm_options::replace;158 TestCase(perms xperms, perms xexpect,159 perm_options xopts = perm_options::replace)160 : set_perms(xperms), expected(xexpect), opts(xopts) {}161 } cases[] = {162 {perms::owner_all, perms::owner_all},163 {perms::group_all, perms::owner_all | perms::group_all, perm_options::add},164 {perms::owner_all, perms::group_all, perm_options::remove},165 };166 for (auto const& TC : cases) {167#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(_AIX)168 // On OS X symlink permissions are supported. We should get an empty169 // error code and the expected permissions.170 const auto expected_link_perms = TC.expected;171 std::error_code expected_ec;172#else173 // On linux symlink permissions are not supported. The error code should174 // be 'operation_not_supported' and the symlink permissions should be175 // unchanged.176 const auto expected_link_perms = symlink_status(sym).permissions();177 std::error_code expected_ec = std::make_error_code(std::errc::operation_not_supported);178#endif179 std::error_code ec = GetTestEC();180 permissions(sym, TC.set_perms, TC.opts | perm_options::nofollow, ec);181 if (expected_ec)182 assert(ErrorIs(ec, static_cast<std::errc>(expected_ec.value())));183 else184 assert(!ec);185 assert(status(file).permissions() == file_perms);186 assert(symlink_status(sym).permissions() == expected_link_perms);187 }188}189#endif // _WIN32190 191int main(int, char**) {192 test_signatures();193 test_error_reporting();194 basic_permissions_test();195#ifndef _WIN32196 test_no_resolve_symlink_on_symlink();197#endif198 return 0;199}200