blob: 745deab1f7b89529b5a57b4ff4199a238591ccea [file] [log] [blame]
duke6e45e102007-12-01 00:00:00 +00001/*
ohairf5857212010-12-28 15:53:50 -08002 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
duke6e45e102007-12-01 00:00:00 +00003 * 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 *
ohair2283b9d2010-05-25 15:58:33 -070019 * 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.
duke6e45e102007-12-01 00:00:00 +000022 */
23
24/* @test
alanb5ebcbc62010-10-12 08:49:33 +010025 @bug 4167472 5097703 6216563 6284003 6728842 6464744
duke6e45e102007-12-01 00:00:00 +000026 @summary Basic test for setWritable/Readable/Executable methods
27 */
28
29import java.io.*;
alanbb75d5382011-01-28 09:28:43 +000030import java.nio.file.*;
alanb5ebcbc62010-10-12 08:49:33 +010031import java.nio.file.attribute.*;
duke6e45e102007-12-01 00:00:00 +000032
33public class SetAccess {
34 public static void main(String[] args) throws Exception {
35 File d = new File(System.getProperty("test.dir", "."));
36
37 File f = new File(d, "x.SetAccessPermission");
38 if (f.exists() && !f.delete())
39 throw new Exception("Can't delete test file: " + f);
40 OutputStream o = new FileOutputStream(f);
41 o.write('x');
42 o.close();
43 doTest(f);
44
45 f = new File(d, "x.SetAccessPermission.dir");
46 if (f.exists() && !f.delete())
47 throw new Exception("Can't delete test dir: " + f);
48 if (!f.mkdir())
49 throw new Exception(f + ": Cannot create directory");
50 doTest(f);
51 }
52
53 public static void doTest(File f) throws Exception {
duke6e45e102007-12-01 00:00:00 +000054 if (!System.getProperty("os.name").startsWith("Windows")) {
alanb5ebcbc62010-10-12 08:49:33 +010055 if (!f.setReadOnly())
56 throw new Exception(f + ": setReadOnly Failed");
duke6e45e102007-12-01 00:00:00 +000057 if (!f.setWritable(true, true) ||
58 !f.canWrite() ||
59 permission(f).charAt(2) != 'w')
60 throw new Exception(f + ": setWritable(true, ture) Failed");
61 if (!f.setWritable(false, true) ||
62 f.canWrite() ||
63 permission(f).charAt(2) != '-')
64 throw new Exception(f + ": setWritable(false, true) Failed");
65 if (!f.setWritable(true, false) ||
66 !f.canWrite() ||
67 !permission(f).matches(".(.w.){3}"))
68 throw new Exception(f + ": setWritable(true, false) Failed");
69 if (!f.setWritable(false, false) ||
70 f.canWrite() ||
71 !permission(f).matches(".(.-.){3}"))
72 throw new Exception(f + ": setWritable(false, true) Failed");
73 if (!f.setWritable(true) || !f.canWrite() ||
74 permission(f).charAt(2) != 'w')
75 throw new Exception(f + ": setWritable(true, ture) Failed");
76 if (!f.setWritable(false) || f.canWrite() ||
77 permission(f).charAt(2) != '-')
78 throw new Exception(f + ": setWritable(false, true) Failed");
79 if (!f.setExecutable(true, true) ||
80 !f.canExecute() ||
81 permission(f).charAt(3) != 'x')
82 throw new Exception(f + ": setExecutable(true, true) Failed");
83 if (!f.setExecutable(false, true) ||
84 f.canExecute() ||
85 permission(f).charAt(3) != '-')
86 throw new Exception(f + ": setExecutable(false, true) Failed");
87 if (!f.setExecutable(true, false) ||
88 !f.canExecute() ||
89 !permission(f).matches(".(..x){3}"))
90 throw new Exception(f + ": setExecutable(true, false) Failed");
91 if (!f.setExecutable(false, false) ||
92 f.canExecute() ||
93 !permission(f).matches(".(..-){3}"))
94 throw new Exception(f + ": setExecutable(false, false) Failed");
95 if (!f.setExecutable(true) || !f.canExecute() ||
96 permission(f).charAt(3) != 'x')
97 throw new Exception(f + ": setExecutable(true, true) Failed");
98 if (!f.setExecutable(false) || f.canExecute() ||
99 permission(f).charAt(3) != '-')
100 throw new Exception(f + ": setExecutable(false, true) Failed");
101 if (!f.setReadable(true, true) ||
102 !f.canRead() ||
103 permission(f).charAt(1) != 'r')
104 throw new Exception(f + ": setReadable(true, true) Failed");
105 if (!f.setReadable(false, true) ||
106 f.canRead() ||
107 permission(f).charAt(1) != '-')
108 throw new Exception(f + ": setReadable(false, true) Failed");
109 if (!f.setReadable(true, false) ||
110 !f.canRead() ||
111 !permission(f).matches(".(r..){3}"))
112 throw new Exception(f + ": setReadable(true, false) Failed");
113 if (!f.setReadable(false, false) ||
114 f.canRead() ||
115 !permission(f).matches(".(-..){3}"))
116 throw new Exception(f + ": setReadable(false, false) Failed");
117 if (!f.setReadable(true) || !f.canRead() ||
118 permission(f).charAt(1) != 'r')
119 throw new Exception(f + ": setReadable(true, true) Failed");
120 if (!f.setReadable(false) || f.canRead() ||
121 permission(f).charAt(1) != '-')
122 throw new Exception(f + ": setReadable(false, true) Failed");
123 } else {
124 //Windows platform
alanb5ebcbc62010-10-12 08:49:33 +0100125 if (f.isFile()) {
126 if (!f.setReadOnly())
127 throw new Exception(f + ": setReadOnly Failed");
128 if (!f.setWritable(true, true) || !f.canWrite())
129 throw new Exception(f + ": setWritable(true, ture) Failed");
130 if (!f.setWritable(true, false) || !f.canWrite())
131 throw new Exception(f + ": setWritable(true, false) Failed");
132 if (!f.setWritable(true) || !f.canWrite())
133 throw new Exception(f + ": setWritable(true, ture) Failed");
134 if (!f.setExecutable(true, true) || !f.canExecute())
135 throw new Exception(f + ": setExecutable(true, true) Failed");
136 if (!f.setExecutable(true, false) || !f.canExecute())
137 throw new Exception(f + ": setExecutable(true, false) Failed");
138 if (!f.setExecutable(true) || !f.canExecute())
139 throw new Exception(f + ": setExecutable(true, true) Failed");
140 if (!f.setReadable(true, true) || !f.canRead())
141 throw new Exception(f + ": setReadable(true, true) Failed");
142 if (!f.setReadable(true, false) || !f.canRead())
143 throw new Exception(f + ": setReadable(true, false) Failed");
144 if (!f.setReadable(true) || !f.canRead())
145 throw new Exception(f + ": setReadable(true, true) Failed");
146 }
duke6e45e102007-12-01 00:00:00 +0000147 if (f.isDirectory()) {
alanb5ebcbc62010-10-12 08:49:33 +0100148 // setWritable should fail on directories because the DOS readonly
149 // attribute prevents a directory from being deleted.
150 if (f.setWritable(false, true))
151 throw new Exception(f + ": setWritable(false, true) Succeeded");
152 if (f.setWritable(false, false))
153 throw new Exception(f + ": setWritable(false, false) Succeeded");
154 if (f.setWritable(false))
155 throw new Exception(f + ": setWritable(false) Succeeded");
duke6e45e102007-12-01 00:00:00 +0000156 } else {
157 if (!f.setWritable(false, true) || f.canWrite())
158 throw new Exception(f + ": setWritable(false, true) Failed");
159 if (!f.setWritable(false, false) || f.canWrite())
alanb5ebcbc62010-10-12 08:49:33 +0100160 throw new Exception(f + ": setWritable(false, false) Failed");
duke6e45e102007-12-01 00:00:00 +0000161 if (!f.setWritable(false) || f.canWrite())
alanb5ebcbc62010-10-12 08:49:33 +0100162 throw new Exception(f + ": setWritable(false) Failed");
duke6e45e102007-12-01 00:00:00 +0000163 }
164 if (f.setExecutable(false, true))
165 throw new Exception(f + ": setExecutable(false, true) Failed");
166 if (f.setExecutable(false, false))
167 throw new Exception(f + ": setExecutable(false, false) Failed");
168 if (f.setExecutable(false))
169 throw new Exception(f + ": setExecutable(false, true) Failed");
170 if (f.setReadable(false, true))
171 throw new Exception(f + ": setReadable(false, true) Failed");
172 if (f.setReadable(false, false))
173 throw new Exception(f + ": setReadable(false, false) Failed");
174 if (f.setReadable(false))
175 throw new Exception(f + ": setReadable(false, true) Failed");
176 }
177 if (f.exists() && !f.delete())
178 throw new Exception("Can't delete test dir: " + f);
179 }
180
181 private static String permission(File f) throws Exception {
alanbb75d5382011-01-28 09:28:43 +0000182 PosixFileAttributes attrs = Files.readAttributes(f.toPath(), PosixFileAttributes.class);
alanb5ebcbc62010-10-12 08:49:33 +0100183 String type = attrs.isDirectory() ? "d" : " ";
184 return type + PosixFilePermissions.toString(attrs.permissions());
duke6e45e102007-12-01 00:00:00 +0000185 }
186}