]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/27_io/filesystem/operations/permissions.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 27_io / filesystem / operations / permissions.cc
CommitLineData
a5544970 1// Copyright (C) 2016-2019 Free Software Foundation, Inc.
641cb5a6
JW
2//
3// This file is part of the GNU ISO C++ Library. This library is free
4// software; you can redistribute it and/or modify it under the
5// terms of the GNU General Public License as published by the
6// Free Software Foundation; either version 3, or (at your option)
7// any later version.
8
9// This library is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License along
15// with this library; see the file COPYING3. If not see
16// <http://www.gnu.org/licenses/>.
17
18// { dg-options "-std=gnu++17 -lstdc++fs" }
19// { dg-do run { target c++17 } }
20// { dg-require-filesystem-ts "" }
21
22// C++17 30.10.14.26 Permissions [fs.op.permissions]
23
24#include <filesystem>
25#include <testsuite_fs.h>
26#include <testsuite_hooks.h>
27
28void
29test01()
30{
31 using std::filesystem::perms;
32 using std::filesystem::perm_options;
33
34 auto p = __gnu_test::nonexistent_path();
35
36 __gnu_test::scoped_file f(p);
37 VERIFY( exists(p) );
38 permissions(p, perms::owner_all);
39 VERIFY( status(p).permissions() == perms::owner_all );
40 permissions(p, perms::group_read, perm_options::add);
41 VERIFY( status(p).permissions() == (perms::owner_all | perms::group_read) );
42 permissions(p, perms::group_read, perm_options::remove);
43 VERIFY( status(p).permissions() == perms::owner_all );
44}
45
46void
47test02()
48{
49 const std::error_code bad_ec = make_error_code(std::errc::invalid_argument);
50 using std::filesystem::perms;
51 using std::filesystem::perm_options;
52
53 auto p = __gnu_test::nonexistent_path();
54
55 std::error_code ec;
56 permissions(p, perms::owner_all, ec);
57 VERIFY( ec );
58
59 __gnu_test::scoped_file f(p);
60 VERIFY( exists(p) );
61
62 ec = bad_ec;
63 permissions(p, perms::owner_all, ec);
64 VERIFY( !ec );
65 VERIFY( status(p).permissions() == perms::owner_all );
66 ec = bad_ec;
67 permissions(p, perms::group_read, perm_options::add, ec);
68 VERIFY( !ec );
69 VERIFY( status(p).permissions() == (perms::owner_all | perms::group_read) );
70 ec = bad_ec;
71 permissions(p, perms::group_read, perm_options::remove, ec);
72 VERIFY( !ec );
73 VERIFY( status(p).permissions() == perms::owner_all );
74}
75
76void
77test03()
78{
79 using std::filesystem::perms;
80 using std::filesystem::perm_options;
81
82 __gnu_test::scoped_file f;
83 VERIFY( exists(f.path) );
84
85 auto p = __gnu_test::nonexistent_path();
86 create_symlink(f.path, p);
87
88 std::error_code ec = make_error_code(std::errc::no_such_file_or_directory);
641cb5a6
JW
89 permissions(p, perms::owner_all,
90 perm_options::replace|perm_options::nofollow, ec);
e921c7e5
JW
91 bool caught = false;
92 std::error_code ec2;
641cb5a6
JW
93 try
94 {
95 permissions(p, perms::owner_all,
96 perm_options::replace|perm_options::nofollow);
97 }
98 catch (const std::filesystem::filesystem_error& ex)
99 {
e921c7e5 100 caught = true;
641cb5a6
JW
101 ec2 = ex.code();
102 VERIFY( ex.path1() == p );
103 }
104 // Both calls should succeed, or both should fail with same error:
e921c7e5
JW
105 if (ec)
106 {
107 VERIFY( caught );
108 VERIFY( ec == ec2 );
109 }
110 else
111 VERIFY( !caught );
641cb5a6
JW
112
113 remove(p);
114}
115
116void
117test04()
118{
119 using perms = std::filesystem::perms;
120
121 auto p = __gnu_test::nonexistent_path();
122 create_symlink(__gnu_test::nonexistent_path(), p);
123
124 std::error_code ec = make_error_code(std::errc::no_such_file_or_directory);
641cb5a6
JW
125 permissions(p, perms::owner_all, ec);
126 VERIFY( ec );
e921c7e5 127 std::error_code ec2;
641cb5a6
JW
128 try
129 {
130 permissions(p, perms::owner_all);
131 }
132 catch (const std::filesystem::filesystem_error& ex)
133 {
134 ec2 = ex.code();
135 VERIFY( ex.path1() == p );
136 }
137 VERIFY( ec == ec2 );
138
139 remove(p);
140}
141
142void
143test05()
144{
145 const std::error_code bad_ec = make_error_code(std::errc::invalid_argument);
146 using std::filesystem::perms;
147 using std::filesystem::perm_options;
148 std::error_code ec;
149
150 __gnu_test::scoped_file f;
151 auto p = perms::owner_write;
152
153 // symlink_nofollow should not give an error for non-symlinks
154 ec = bad_ec;
155 permissions(f.path, p, perm_options::replace|perm_options::nofollow, ec);
156 VERIFY( !ec );
157 auto st = status(f.path);
158 VERIFY( st.permissions() == p );
159 p |= perms::owner_read;
160 ec = bad_ec;
161 permissions(f.path, p, perm_options::replace|perm_options::nofollow, ec);
162 VERIFY( !ec );
163 st = status(f.path);
164 VERIFY( st.permissions() == p );
165}
166
167int
168main()
169{
170 test01();
171 test02();
172 test03();
173 test04();
174 test05();
175}