1 /* 2 * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved. 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 * 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. 22 */ 23 24 /* @test 25 @bug 4167472 5097703 6216563 6284003 26 @summary Basic test for setWritable/Readable/Executable methods 27 */ 28 29 import java.io.*; 30 31 public class SetAccess { 32 public static void main(String[] args) throws Exception { 33 File d = new File(System.getProperty("test.dir", ".")); 34 35 File f = new File(d, "x.SetAccessPermission"); 36 if (f.exists() && !f.delete()) 37 throw new Exception("Can't delete test file: " + f); 38 OutputStream o = new FileOutputStream(f); 39 o.write('x'); 40 o.close(); 41 doTest(f); 42 43 f = new File(d, "x.SetAccessPermission.dir"); 44 if (f.exists() && !f.delete()) 45 throw new Exception("Can't delete test dir: " + f); 46 if (!f.mkdir()) 47 throw new Exception(f + ": Cannot create directory"); 48 doTest(f); 49 } 50 51 public static void doTest(File f) throws Exception { 52 f.setReadOnly(); 53 if (!System.getProperty("os.name").startsWith("Windows")) { 54 if (!f.setWritable(true, true) || 55 !f.canWrite() || 56 permission(f).charAt(2) != 'w') 57 throw new Exception(f + ": setWritable(true, ture) Failed"); 58 if (!f.setWritable(false, true) || 59 f.canWrite() || 60 permission(f).charAt(2) != '-') 61 throw new Exception(f + ": setWritable(false, true) Failed"); 62 if (!f.setWritable(true, false) || 63 !f.canWrite() || 64 !permission(f).matches(".(.w.){3}")) 65 throw new Exception(f + ": setWritable(true, false) Failed"); 66 if (!f.setWritable(false, false) || 67 f.canWrite() || 68 !permission(f).matches(".(.-.){3}")) 69 throw new Exception(f + ": setWritable(false, true) Failed"); 70 if (!f.setWritable(true) || !f.canWrite() || 71 permission(f).charAt(2) != 'w') 72 throw new Exception(f + ": setWritable(true, ture) Failed"); 73 if (!f.setWritable(false) || f.canWrite() || 74 permission(f).charAt(2) != '-') 75 throw new Exception(f + ": setWritable(false, true) Failed"); 76 if (!f.setExecutable(true, true) || 77 !f.canExecute() || 78 permission(f).charAt(3) != 'x') 79 throw new Exception(f + ": setExecutable(true, true) Failed"); 80 if (!f.setExecutable(false, true) || 81 f.canExecute() || 82 permission(f).charAt(3) != '-') 83 throw new Exception(f + ": setExecutable(false, true) Failed"); 84 if (!f.setExecutable(true, false) || 85 !f.canExecute() || 86 !permission(f).matches(".(..x){3}")) 87 throw new Exception(f + ": setExecutable(true, false) Failed"); 88 if (!f.setExecutable(false, false) || 89 f.canExecute() || 90 !permission(f).matches(".(..-){3}")) 91 throw new Exception(f + ": setExecutable(false, false) Failed"); 92 if (!f.setExecutable(true) || !f.canExecute() || 93 permission(f).charAt(3) != 'x') 94 throw new Exception(f + ": setExecutable(true, true) Failed"); 95 if (!f.setExecutable(false) || f.canExecute() || 96 permission(f).charAt(3) != '-') 97 throw new Exception(f + ": setExecutable(false, true) Failed"); 98 if (!f.setReadable(true, true) || 99 !f.canRead() || 100 permission(f).charAt(1) != 'r') 101 throw new Exception(f + ": setReadable(true, true) Failed"); 102 if (!f.setReadable(false, true) || 103 f.canRead() || 104 permission(f).charAt(1) != '-') 105 throw new Exception(f + ": setReadable(false, true) Failed"); 106 if (!f.setReadable(true, false) || 107 !f.canRead() || 108 !permission(f).matches(".(r..){3}")) 109 throw new Exception(f + ": setReadable(true, false) Failed"); 110 if (!f.setReadable(false, false) || 111 f.canRead() || 112 !permission(f).matches(".(-..){3}")) 113 throw new Exception(f + ": setReadable(false, false) Failed"); 114 if (!f.setReadable(true) || !f.canRead() || 115 permission(f).charAt(1) != 'r') 116 throw new Exception(f + ": setReadable(true, true) Failed"); 117 if (!f.setReadable(false) || f.canRead() || 118 permission(f).charAt(1) != '-') 119 throw new Exception(f + ": setReadable(false, true) Failed"); 120 } else { 121 //Windows platform 122 if (!f.setWritable(true, true) || !f.canWrite()) 123 throw new Exception(f + ": setWritable(true, ture) Failed"); 124 if (!f.setWritable(true, false) || !f.canWrite()) 125 throw new Exception(f + ": setWritable(true, false) Failed"); 126 if (!f.setWritable(true) || !f.canWrite()) 127 throw new Exception(f + ": setWritable(true, ture) Failed"); 128 if (!f.setExecutable(true, true) || !f.canExecute()) 129 throw new Exception(f + ": setExecutable(true, true) Failed"); 130 if (!f.setExecutable(true, false) || !f.canExecute()) 131 throw new Exception(f + ": setExecutable(true, false) Failed"); 132 if (!f.setExecutable(true) || !f.canExecute()) 133 throw new Exception(f + ": setExecutable(true, true) Failed"); 134 if (!f.setReadable(true, true) || !f.canRead()) 135 throw new Exception(f + ": setReadable(true, true) Failed"); 136 if (!f.setReadable(true, false) || !f.canRead()) 137 throw new Exception(f + ": setReadable(true, false) Failed"); 138 if (!f.setReadable(true) || !f.canRead()) 139 throw new Exception(f + ": setReadable(true, true) Failed"); 140 if (f.isDirectory()) { 141 //All directories on Windows always have read&write access perm, 142 //setting a directory to "unwritable" actually means "not deletable" 143 if (!f.setWritable(false, true) || !f.canWrite()) 144 throw new Exception(f + ": setWritable(false, true) Failed"); 145 if (!f.setWritable(false, false) || !f.canWrite()) 146 throw new Exception(f + ": setWritable(false, true) Failed"); 147 if (!f.setWritable(false) || !f.canWrite()) 148 throw new Exception(f + ": setWritable(false, true) Failed"); 149 } else { 150 if (!f.setWritable(false, true) || f.canWrite()) 151 throw new Exception(f + ": setWritable(false, true) Failed"); 152 if (!f.setWritable(false, false) || f.canWrite()) 153 throw new Exception(f + ": setWritable(false, true) Failed"); 154 if (!f.setWritable(false) || f.canWrite()) 155 throw new Exception(f + ": setWritable(false, true) Failed"); 156 } 157 if (f.setExecutable(false, true)) 158 throw new Exception(f + ": setExecutable(false, true) Failed"); 159 if (f.setExecutable(false, false)) 160 throw new Exception(f + ": setExecutable(false, false) Failed"); 161 if (f.setExecutable(false)) 162 throw new Exception(f + ": setExecutable(false, true) Failed"); 163 if (f.setReadable(false, true)) 164 throw new Exception(f + ": setReadable(false, true) Failed"); 165 if (f.setReadable(false, false)) 166 throw new Exception(f + ": setReadable(false, false) Failed"); 167 if (f.setReadable(false)) 168 throw new Exception(f + ": setReadable(false, true) Failed"); 169 } 170 if (f.exists() && !f.delete()) 171 throw new Exception("Can't delete test dir: " + f); 172 } 173 174 private static String permission(File f) throws Exception { 175 byte[] bb = new byte[1024]; 176 String command = f.isDirectory()?"ls -dl ":"ls -l "; 177 int len = Runtime.getRuntime() 178 .exec(command + f.getPath()) 179 .getInputStream() 180 .read(bb, 0, 1024); 181 if (len > 0) 182 return new String(bb, 0, len).substring(0, 10); 183 return ""; 184 } 185 }