permissions.pass.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. // UNSUPPORTED: c++98, c++03
  10. // <experimental/filesystem>
  11. // void permissions(const path& p, perms prms);
  12. // void permissions(const path& p, perms prms, std::error_code& ec) noexcept;
  13. #include <experimental/filesystem>
  14. #include "test_macros.h"
  15. #include "rapid-cxx-test.hpp"
  16. #include "filesystem_test_helper.hpp"
  17. using namespace std::experimental::filesystem;
  18. namespace fs = std::experimental::filesystem;
  19. using PR = fs::perms;
  20. TEST_SUITE(filesystem_permissions_test_suite)
  21. TEST_CASE(test_signatures)
  22. {
  23. const path p; ((void)p);
  24. const perms opts{}; ((void)opts);
  25. std::error_code ec; ((void)ec);
  26. ASSERT_NOT_NOEXCEPT(fs::permissions(p, opts));
  27. // Not noexcept because of narrow contract
  28. LIBCPP_ONLY(
  29. ASSERT_NOT_NOEXCEPT(fs::permissions(p, opts, ec)));
  30. }
  31. TEST_CASE(test_error_reporting)
  32. {
  33. auto checkThrow = [](path const& f, fs::perms opts, const std::error_code& ec)
  34. {
  35. #ifndef TEST_HAS_NO_EXCEPTIONS
  36. try {
  37. fs::permissions(f, opts);
  38. return false;
  39. } catch (filesystem_error const& err) {
  40. return err.path1() == f
  41. && err.path2() == ""
  42. && err.code() == ec;
  43. }
  44. #else
  45. ((void)f); ((void)opts); ((void)ec);
  46. return true;
  47. #endif
  48. };
  49. scoped_test_env env;
  50. const path dne = env.make_env_path("dne");
  51. const path dne_sym = env.create_symlink(dne, "dne_sym");
  52. { // !exists
  53. std::error_code ec;
  54. fs::permissions(dne, fs::perms{}, ec);
  55. TEST_REQUIRE(ec);
  56. TEST_CHECK(checkThrow(dne, fs::perms{}, ec));
  57. }
  58. {
  59. std::error_code ec;
  60. fs::permissions(dne_sym, fs::perms{}, ec);
  61. TEST_REQUIRE(ec);
  62. TEST_CHECK(checkThrow(dne_sym, fs::perms{}, ec));
  63. }
  64. }
  65. TEST_CASE(basic_permissions_test)
  66. {
  67. scoped_test_env env;
  68. const path file = env.create_file("file1", 42);
  69. const path dir = env.create_dir("dir1");
  70. const path file_for_sym = env.create_file("file2", 42);
  71. const path sym = env.create_symlink(file_for_sym, "sym");
  72. const perms AP = perms::add_perms;
  73. const perms RP = perms::remove_perms;
  74. const perms NF = perms::symlink_nofollow;
  75. struct TestCase {
  76. path p;
  77. perms set_perms;
  78. perms expected;
  79. } cases[] = {
  80. // test file
  81. {file, perms::none, perms::none},
  82. {file, perms::owner_all, perms::owner_all},
  83. {file, perms::group_all | AP, perms::owner_all | perms::group_all},
  84. {file, perms::group_all | RP, perms::owner_all},
  85. // test directory
  86. {dir, perms::none, perms::none},
  87. {dir, perms::owner_all, perms::owner_all},
  88. {dir, perms::group_all | AP, perms::owner_all | perms::group_all},
  89. {dir, perms::group_all | RP, perms::owner_all},
  90. // test symlink without symlink_nofollow
  91. {sym, perms::none, perms::none},
  92. {sym, perms::owner_all, perms::owner_all},
  93. {sym, perms::group_all | AP, perms::owner_all | perms::group_all},
  94. {sym, perms::group_all | RP , perms::owner_all},
  95. // test non-symlink with symlink_nofollow. The last test on file/dir
  96. // will have set their permissions to perms::owner_all
  97. {file, perms::group_all | AP | NF, perms::owner_all | perms::group_all},
  98. {dir, perms::group_all | AP | NF, perms::owner_all | perms::group_all}
  99. };
  100. for (auto const& TC : cases) {
  101. TEST_CHECK(status(TC.p).permissions() != TC.expected);
  102. // Set the error code to ensure it's cleared.
  103. std::error_code ec = std::make_error_code(std::errc::bad_address);
  104. permissions(TC.p, TC.set_perms, ec);
  105. TEST_CHECK(!ec);
  106. auto pp = status(TC.p).permissions();
  107. TEST_CHECK(pp == TC.expected);
  108. }
  109. }
  110. TEST_CASE(test_no_resolve_symlink_on_symlink)
  111. {
  112. scoped_test_env env;
  113. const path file = env.create_file("file", 42);
  114. const path sym = env.create_symlink(file, "sym");
  115. const auto file_perms = status(file).permissions();
  116. struct TestCase {
  117. perms set_perms;
  118. perms expected; // only expected on platform that support symlink perms.
  119. } cases[] = {
  120. {perms::owner_all, perms::owner_all},
  121. {perms::group_all | perms::add_perms, perms::owner_all | perms::group_all},
  122. {perms::owner_all | perms::remove_perms, perms::group_all},
  123. };
  124. for (auto const& TC : cases) {
  125. #if defined(__APPLE__) || defined(__FreeBSD__)
  126. // On OS X symlink permissions are supported. We should get an empty
  127. // error code and the expected permissions.
  128. const auto expected_link_perms = TC.expected;
  129. std::error_code expected_ec;
  130. #else
  131. // On linux symlink permissions are not supported. The error code should
  132. // be 'operation_not_supported' and the symlink permissions should be
  133. // unchanged.
  134. const auto expected_link_perms = symlink_status(sym).permissions();
  135. std::error_code expected_ec = std::make_error_code(std::errc::operation_not_supported);
  136. #endif
  137. std::error_code ec = std::make_error_code(std::errc::bad_address);
  138. permissions(sym, TC.set_perms | perms::symlink_nofollow, ec);
  139. TEST_CHECK(ec == expected_ec);
  140. TEST_CHECK(status(file).permissions() == file_perms);
  141. TEST_CHECK(symlink_status(sym).permissions() == expected_link_perms);
  142. }
  143. }
  144. TEST_SUITE_END()