Debian Codegraph @

libstdc++-v3/testsuite/27_io/filesystem/operations/permissions.cc

raw
1 // Copyright (C) 2016-2025 Free Software Foundation, Inc.
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-do run { target c++17 } }
19 // { dg-require-filesystem-ts "" }
20 // { dg-xfail-if "permissions not supported" { *-*-mingw* } }
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
28 void
29 test01()
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
46 void
47 test02()
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
76 void
77 test03()
78 {
79 #ifndef NO_SYMLINKS
80 using std::filesystem::perms;
81 using std::filesystem::perm_options;
82
83 __gnu_test::scoped_file f;
84 VERIFY( exists(f.path) );
85
86 auto p = __gnu_test::nonexistent_path();
87 create_symlink(f.path, p);
88
89 std::error_code ec = make_error_code(std::errc::no_such_file_or_directory);
90 permissions(p, perms::owner_all,
91 perm_options::replace|perm_options::nofollow, ec);
92 bool caught = false;
93 std::error_code ec2;
94 try
95 {
96 permissions(p, perms::owner_all,
97 perm_options::replace|perm_options::nofollow);
98 }
99 catch (const std::filesystem::filesystem_error& ex)
100 {
101 caught = true;
102 ec2 = ex.code();
103 VERIFY( ex.path1() == p );
104 }
105 // Both calls should succeed, or both should fail with same error:
106 if (ec)
107 {
108 VERIFY( caught );
109 VERIFY( ec == ec2 );
110 }
111 else
112 VERIFY( !caught );
113
114 remove(p);
115 #endif
116 }
117
118 void
119 test04()
120 {
121 #ifndef NO_SYMLINKS
122 using perms = std::filesystem::perms;
123
124 auto p = __gnu_test::nonexistent_path();
125 create_symlink(__gnu_test::nonexistent_path(), p);
126
127 std::error_code ec = make_error_code(std::errc::no_such_file_or_directory);
128 permissions(p, perms::owner_all, ec);
129 VERIFY( ec );
130 std::error_code ec2;
131 try
132 {
133 permissions(p, perms::owner_all);
134 }
135 catch (const std::filesystem::filesystem_error& ex)
136 {
137 ec2 = ex.code();
138 VERIFY( ex.path1() == p );
139 }
140 VERIFY( ec == ec2 );
141
142 remove(p);
143 #endif
144 }
145
146 void
147 test05()
148 {
149 const std::error_code bad_ec = make_error_code(std::errc::invalid_argument);
150 using std::filesystem::perms;
151 using std::filesystem::perm_options;
152 std::error_code ec;
153
154 __gnu_test::scoped_file f;
155 auto p = perms::owner_write;
156
157 // symlink_nofollow should not give an error for non-symlinks
158 ec = bad_ec;
159 permissions(f.path, p, perm_options::replace|perm_options::nofollow, ec);
160 VERIFY( !ec );
161 auto st = status(f.path);
162 VERIFY( st.permissions() == p );
163 p |= perms::owner_read;
164 ec = bad_ec;
165 permissions(f.path, p, perm_options::replace|perm_options::nofollow, ec);
166 VERIFY( !ec );
167 st = status(f.path);
168 VERIFY( st.permissions() == p );
169 }
170
171 int
172 main()
173 {
174 test01();
175 test02();
176 test03();
177 test04();
178 test05();
179 }