author | tschatzl |
Wed, 24 Jul 2019 11:49:39 +0200 | |
changeset 57508 | 28ab01c06755 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
13568
ce5ab758aeb5
6931128: (spec) File attribute tests fail when run as root.
robm
parents:
9035
diff
changeset
|
2 |
* Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. |
|
8 |
* |
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
13 |
* accompanied this code). |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License version |
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 |
* |
|
5506 | 19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
20 |
* or visit www.oracle.com if you need additional information or have any |
|
21 |
* questions. |
|
2 | 22 |
*/ |
23 |
||
24 |
/* @test |
|
6884
65b1fa0a0fae
6728842: File.setReadOnly does not make a directory read-only (win)
alanb
parents:
5506
diff
changeset
|
25 |
@bug 4167472 5097703 6216563 6284003 6728842 6464744 |
2 | 26 |
@summary Basic test for setWritable/Readable/Executable methods |
13568
ce5ab758aeb5
6931128: (spec) File attribute tests fail when run as root.
robm
parents:
9035
diff
changeset
|
27 |
@build SetAccess Util |
ce5ab758aeb5
6931128: (spec) File attribute tests fail when run as root.
robm
parents:
9035
diff
changeset
|
28 |
@run main SetAccess |
2 | 29 |
*/ |
30 |
||
31 |
import java.io.*; |
|
8158 | 32 |
import java.nio.file.*; |
6884
65b1fa0a0fae
6728842: File.setReadOnly does not make a directory read-only (win)
alanb
parents:
5506
diff
changeset
|
33 |
import java.nio.file.attribute.*; |
2 | 34 |
|
35 |
public class SetAccess { |
|
36 |
public static void main(String[] args) throws Exception { |
|
13568
ce5ab758aeb5
6931128: (spec) File attribute tests fail when run as root.
robm
parents:
9035
diff
changeset
|
37 |
if (Util.isPrivileged()) { |
ce5ab758aeb5
6931128: (spec) File attribute tests fail when run as root.
robm
parents:
9035
diff
changeset
|
38 |
System.out.println("Unable to test file permissions when running with privileges"); |
ce5ab758aeb5
6931128: (spec) File attribute tests fail when run as root.
robm
parents:
9035
diff
changeset
|
39 |
return; |
ce5ab758aeb5
6931128: (spec) File attribute tests fail when run as root.
robm
parents:
9035
diff
changeset
|
40 |
} |
ce5ab758aeb5
6931128: (spec) File attribute tests fail when run as root.
robm
parents:
9035
diff
changeset
|
41 |
|
2 | 42 |
File d = new File(System.getProperty("test.dir", ".")); |
43 |
||
44 |
File f = new File(d, "x.SetAccessPermission"); |
|
45 |
if (f.exists() && !f.delete()) |
|
46 |
throw new Exception("Can't delete test file: " + f); |
|
47 |
OutputStream o = new FileOutputStream(f); |
|
48 |
o.write('x'); |
|
49 |
o.close(); |
|
50 |
doTest(f); |
|
51 |
||
52 |
f = new File(d, "x.SetAccessPermission.dir"); |
|
53 |
if (f.exists() && !f.delete()) |
|
54 |
throw new Exception("Can't delete test dir: " + f); |
|
55 |
if (!f.mkdir()) |
|
56 |
throw new Exception(f + ": Cannot create directory"); |
|
57 |
doTest(f); |
|
58 |
} |
|
59 |
||
60 |
public static void doTest(File f) throws Exception { |
|
61 |
if (!System.getProperty("os.name").startsWith("Windows")) { |
|
6884
65b1fa0a0fae
6728842: File.setReadOnly does not make a directory read-only (win)
alanb
parents:
5506
diff
changeset
|
62 |
if (!f.setReadOnly()) |
65b1fa0a0fae
6728842: File.setReadOnly does not make a directory read-only (win)
alanb
parents:
5506
diff
changeset
|
63 |
throw new Exception(f + ": setReadOnly Failed"); |
2 | 64 |
if (!f.setWritable(true, true) || |
65 |
!f.canWrite() || |
|
66 |
permission(f).charAt(2) != 'w') |
|
67 |
throw new Exception(f + ": setWritable(true, ture) Failed"); |
|
68 |
if (!f.setWritable(false, true) || |
|
69 |
f.canWrite() || |
|
70 |
permission(f).charAt(2) != '-') |
|
71 |
throw new Exception(f + ": setWritable(false, true) Failed"); |
|
72 |
if (!f.setWritable(true, false) || |
|
73 |
!f.canWrite() || |
|
74 |
!permission(f).matches(".(.w.){3}")) |
|
75 |
throw new Exception(f + ": setWritable(true, false) Failed"); |
|
76 |
if (!f.setWritable(false, false) || |
|
77 |
f.canWrite() || |
|
78 |
!permission(f).matches(".(.-.){3}")) |
|
79 |
throw new Exception(f + ": setWritable(false, true) Failed"); |
|
80 |
if (!f.setWritable(true) || !f.canWrite() || |
|
81 |
permission(f).charAt(2) != 'w') |
|
82 |
throw new Exception(f + ": setWritable(true, ture) Failed"); |
|
83 |
if (!f.setWritable(false) || f.canWrite() || |
|
84 |
permission(f).charAt(2) != '-') |
|
85 |
throw new Exception(f + ": setWritable(false, true) Failed"); |
|
86 |
if (!f.setExecutable(true, true) || |
|
87 |
!f.canExecute() || |
|
88 |
permission(f).charAt(3) != 'x') |
|
89 |
throw new Exception(f + ": setExecutable(true, true) Failed"); |
|
90 |
if (!f.setExecutable(false, true) || |
|
91 |
f.canExecute() || |
|
92 |
permission(f).charAt(3) != '-') |
|
93 |
throw new Exception(f + ": setExecutable(false, true) Failed"); |
|
94 |
if (!f.setExecutable(true, false) || |
|
95 |
!f.canExecute() || |
|
96 |
!permission(f).matches(".(..x){3}")) |
|
97 |
throw new Exception(f + ": setExecutable(true, false) Failed"); |
|
98 |
if (!f.setExecutable(false, false) || |
|
99 |
f.canExecute() || |
|
100 |
!permission(f).matches(".(..-){3}")) |
|
101 |
throw new Exception(f + ": setExecutable(false, false) Failed"); |
|
102 |
if (!f.setExecutable(true) || !f.canExecute() || |
|
103 |
permission(f).charAt(3) != 'x') |
|
104 |
throw new Exception(f + ": setExecutable(true, true) Failed"); |
|
105 |
if (!f.setExecutable(false) || f.canExecute() || |
|
106 |
permission(f).charAt(3) != '-') |
|
107 |
throw new Exception(f + ": setExecutable(false, true) Failed"); |
|
108 |
if (!f.setReadable(true, true) || |
|
109 |
!f.canRead() || |
|
110 |
permission(f).charAt(1) != 'r') |
|
111 |
throw new Exception(f + ": setReadable(true, true) Failed"); |
|
112 |
if (!f.setReadable(false, true) || |
|
113 |
f.canRead() || |
|
114 |
permission(f).charAt(1) != '-') |
|
115 |
throw new Exception(f + ": setReadable(false, true) Failed"); |
|
116 |
if (!f.setReadable(true, false) || |
|
117 |
!f.canRead() || |
|
118 |
!permission(f).matches(".(r..){3}")) |
|
119 |
throw new Exception(f + ": setReadable(true, false) Failed"); |
|
120 |
if (!f.setReadable(false, false) || |
|
121 |
f.canRead() || |
|
122 |
!permission(f).matches(".(-..){3}")) |
|
123 |
throw new Exception(f + ": setReadable(false, false) Failed"); |
|
124 |
if (!f.setReadable(true) || !f.canRead() || |
|
125 |
permission(f).charAt(1) != 'r') |
|
126 |
throw new Exception(f + ": setReadable(true, true) Failed"); |
|
127 |
if (!f.setReadable(false) || f.canRead() || |
|
128 |
permission(f).charAt(1) != '-') |
|
129 |
throw new Exception(f + ": setReadable(false, true) Failed"); |
|
130 |
} else { |
|
131 |
//Windows platform |
|
6884
65b1fa0a0fae
6728842: File.setReadOnly does not make a directory read-only (win)
alanb
parents:
5506
diff
changeset
|
132 |
if (f.isFile()) { |
65b1fa0a0fae
6728842: File.setReadOnly does not make a directory read-only (win)
alanb
parents:
5506
diff
changeset
|
133 |
if (!f.setReadOnly()) |
65b1fa0a0fae
6728842: File.setReadOnly does not make a directory read-only (win)
alanb
parents:
5506
diff
changeset
|
134 |
throw new Exception(f + ": setReadOnly Failed"); |
65b1fa0a0fae
6728842: File.setReadOnly does not make a directory read-only (win)
alanb
parents:
5506
diff
changeset
|
135 |
if (!f.setWritable(true, true) || !f.canWrite()) |
65b1fa0a0fae
6728842: File.setReadOnly does not make a directory read-only (win)
alanb
parents:
5506
diff
changeset
|
136 |
throw new Exception(f + ": setWritable(true, ture) Failed"); |
65b1fa0a0fae
6728842: File.setReadOnly does not make a directory read-only (win)
alanb
parents:
5506
diff
changeset
|
137 |
if (!f.setWritable(true, false) || !f.canWrite()) |
65b1fa0a0fae
6728842: File.setReadOnly does not make a directory read-only (win)
alanb
parents:
5506
diff
changeset
|
138 |
throw new Exception(f + ": setWritable(true, false) Failed"); |
65b1fa0a0fae
6728842: File.setReadOnly does not make a directory read-only (win)
alanb
parents:
5506
diff
changeset
|
139 |
if (!f.setWritable(true) || !f.canWrite()) |
65b1fa0a0fae
6728842: File.setReadOnly does not make a directory read-only (win)
alanb
parents:
5506
diff
changeset
|
140 |
throw new Exception(f + ": setWritable(true, ture) Failed"); |
65b1fa0a0fae
6728842: File.setReadOnly does not make a directory read-only (win)
alanb
parents:
5506
diff
changeset
|
141 |
if (!f.setExecutable(true, true) || !f.canExecute()) |
65b1fa0a0fae
6728842: File.setReadOnly does not make a directory read-only (win)
alanb
parents:
5506
diff
changeset
|
142 |
throw new Exception(f + ": setExecutable(true, true) Failed"); |
65b1fa0a0fae
6728842: File.setReadOnly does not make a directory read-only (win)
alanb
parents:
5506
diff
changeset
|
143 |
if (!f.setExecutable(true, false) || !f.canExecute()) |
65b1fa0a0fae
6728842: File.setReadOnly does not make a directory read-only (win)
alanb
parents:
5506
diff
changeset
|
144 |
throw new Exception(f + ": setExecutable(true, false) Failed"); |
65b1fa0a0fae
6728842: File.setReadOnly does not make a directory read-only (win)
alanb
parents:
5506
diff
changeset
|
145 |
if (!f.setExecutable(true) || !f.canExecute()) |
65b1fa0a0fae
6728842: File.setReadOnly does not make a directory read-only (win)
alanb
parents:
5506
diff
changeset
|
146 |
throw new Exception(f + ": setExecutable(true, true) Failed"); |
65b1fa0a0fae
6728842: File.setReadOnly does not make a directory read-only (win)
alanb
parents:
5506
diff
changeset
|
147 |
if (!f.setReadable(true, true) || !f.canRead()) |
65b1fa0a0fae
6728842: File.setReadOnly does not make a directory read-only (win)
alanb
parents:
5506
diff
changeset
|
148 |
throw new Exception(f + ": setReadable(true, true) Failed"); |
65b1fa0a0fae
6728842: File.setReadOnly does not make a directory read-only (win)
alanb
parents:
5506
diff
changeset
|
149 |
if (!f.setReadable(true, false) || !f.canRead()) |
65b1fa0a0fae
6728842: File.setReadOnly does not make a directory read-only (win)
alanb
parents:
5506
diff
changeset
|
150 |
throw new Exception(f + ": setReadable(true, false) Failed"); |
65b1fa0a0fae
6728842: File.setReadOnly does not make a directory read-only (win)
alanb
parents:
5506
diff
changeset
|
151 |
if (!f.setReadable(true) || !f.canRead()) |
65b1fa0a0fae
6728842: File.setReadOnly does not make a directory read-only (win)
alanb
parents:
5506
diff
changeset
|
152 |
throw new Exception(f + ": setReadable(true, true) Failed"); |
65b1fa0a0fae
6728842: File.setReadOnly does not make a directory read-only (win)
alanb
parents:
5506
diff
changeset
|
153 |
} |
2 | 154 |
if (f.isDirectory()) { |
6884
65b1fa0a0fae
6728842: File.setReadOnly does not make a directory read-only (win)
alanb
parents:
5506
diff
changeset
|
155 |
// setWritable should fail on directories because the DOS readonly |
65b1fa0a0fae
6728842: File.setReadOnly does not make a directory read-only (win)
alanb
parents:
5506
diff
changeset
|
156 |
// attribute prevents a directory from being deleted. |
65b1fa0a0fae
6728842: File.setReadOnly does not make a directory read-only (win)
alanb
parents:
5506
diff
changeset
|
157 |
if (f.setWritable(false, true)) |
65b1fa0a0fae
6728842: File.setReadOnly does not make a directory read-only (win)
alanb
parents:
5506
diff
changeset
|
158 |
throw new Exception(f + ": setWritable(false, true) Succeeded"); |
65b1fa0a0fae
6728842: File.setReadOnly does not make a directory read-only (win)
alanb
parents:
5506
diff
changeset
|
159 |
if (f.setWritable(false, false)) |
65b1fa0a0fae
6728842: File.setReadOnly does not make a directory read-only (win)
alanb
parents:
5506
diff
changeset
|
160 |
throw new Exception(f + ": setWritable(false, false) Succeeded"); |
65b1fa0a0fae
6728842: File.setReadOnly does not make a directory read-only (win)
alanb
parents:
5506
diff
changeset
|
161 |
if (f.setWritable(false)) |
65b1fa0a0fae
6728842: File.setReadOnly does not make a directory read-only (win)
alanb
parents:
5506
diff
changeset
|
162 |
throw new Exception(f + ": setWritable(false) Succeeded"); |
2 | 163 |
} else { |
164 |
if (!f.setWritable(false, true) || f.canWrite()) |
|
165 |
throw new Exception(f + ": setWritable(false, true) Failed"); |
|
166 |
if (!f.setWritable(false, false) || f.canWrite()) |
|
6884
65b1fa0a0fae
6728842: File.setReadOnly does not make a directory read-only (win)
alanb
parents:
5506
diff
changeset
|
167 |
throw new Exception(f + ": setWritable(false, false) Failed"); |
2 | 168 |
if (!f.setWritable(false) || f.canWrite()) |
6884
65b1fa0a0fae
6728842: File.setReadOnly does not make a directory read-only (win)
alanb
parents:
5506
diff
changeset
|
169 |
throw new Exception(f + ": setWritable(false) Failed"); |
2 | 170 |
} |
171 |
if (f.setExecutable(false, true)) |
|
172 |
throw new Exception(f + ": setExecutable(false, true) Failed"); |
|
173 |
if (f.setExecutable(false, false)) |
|
174 |
throw new Exception(f + ": setExecutable(false, false) Failed"); |
|
175 |
if (f.setExecutable(false)) |
|
176 |
throw new Exception(f + ": setExecutable(false, true) Failed"); |
|
177 |
if (f.setReadable(false, true)) |
|
178 |
throw new Exception(f + ": setReadable(false, true) Failed"); |
|
179 |
if (f.setReadable(false, false)) |
|
180 |
throw new Exception(f + ": setReadable(false, false) Failed"); |
|
181 |
if (f.setReadable(false)) |
|
182 |
throw new Exception(f + ": setReadable(false, true) Failed"); |
|
183 |
} |
|
184 |
if (f.exists() && !f.delete()) |
|
185 |
throw new Exception("Can't delete test dir: " + f); |
|
186 |
} |
|
187 |
||
188 |
private static String permission(File f) throws Exception { |
|
8158 | 189 |
PosixFileAttributes attrs = Files.readAttributes(f.toPath(), PosixFileAttributes.class); |
6884
65b1fa0a0fae
6728842: File.setReadOnly does not make a directory read-only (win)
alanb
parents:
5506
diff
changeset
|
190 |
String type = attrs.isDirectory() ? "d" : " "; |
65b1fa0a0fae
6728842: File.setReadOnly does not make a directory read-only (win)
alanb
parents:
5506
diff
changeset
|
191 |
return type + PosixFilePermissions.toString(attrs.permissions()); |
2 | 192 |
} |
193 |
} |