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