1 /* 2 * Copyright (c) 1998, 2011, 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 4167937 26 @ignore Test truncates system files when run as root, see 7042603 27 @summary Test code paths that use the JVM_LastErrorString procedure 28 */ 29 30 import java.io.IOException; 31 import java.io.File; 32 import java.io.FileInputStream; 33 import java.io.FileOutputStream; 34 import java.io.RandomAccessFile; 35 36 37 public class LastErrorString { 38 39 static String UNWRITEABLE_DIR; 40 static String UNREADABLE_FILE; 41 static String READABLE_FILE; 42 static String WRITEABLE_FILE; 43 static String INVALID_PATH; 44 45 static { 46 if (File.separatorChar == '/') { 47 UNWRITEABLE_DIR = "/etc/dfs"; 48 UNREADABLE_FILE = "/etc/shadow"; 49 } else if (File.separatorChar == '\\') { 50 UNREADABLE_FILE = "c:/pagefile.sys"; 51 UNWRITEABLE_DIR = "z:/fooBAR/baz/GORP"; 52 } else { 53 throw new RuntimeException("What kind of system is this?"); 54 } 55 File d = new File(System.getProperty("test.src", ".")); 56 READABLE_FILE = new File(d, "LastErrorString.java").getPath(); 57 WRITEABLE_FILE = "x.LastErrorString"; 58 String s = "foo/"; 59 for (;;) { 60 s = s + s; 61 if (s.length() > 8192) break; 62 } 63 s += "bar"; 64 INVALID_PATH = s; 65 } 66 67 68 abstract static class Test { 69 70 String name; 71 Test(String name)72 public Test(String name) { 73 this.name = name; 74 } 75 run()76 public abstract void run() throws IOException; 77 go()78 public void go() throws IOException { 79 try { 80 this.run(); 81 } catch (IOException x) { 82 System.err.println(name); 83 System.err.println(" " + x); 84 return; 85 } 86 System.err.println("WARNING: No exception for " + name); 87 } 88 89 } 90 91 abstract static class ClosedFISTest extends Test { 92 93 FileInputStream in; 94 ClosedFISTest(String name)95 public ClosedFISTest(String name) { 96 super("FileInputStream." + name); 97 } 98 go()99 public void go() throws IOException { 100 this.in = new FileInputStream(READABLE_FILE); 101 this.in.close(); 102 super.go(); 103 } 104 105 } 106 107 abstract static class ClosedFOSTest extends Test { 108 109 FileOutputStream out; 110 ClosedFOSTest(String name)111 public ClosedFOSTest(String name) { 112 super("FileOutputStream." + name); 113 } 114 go()115 public void go() throws IOException { 116 this.out = new FileOutputStream(WRITEABLE_FILE); 117 this.out.close(); 118 super.go(); 119 } 120 121 } 122 123 abstract static class ClosedRAFTest extends Test { 124 125 RandomAccessFile raf; 126 ClosedRAFTest(String name)127 public ClosedRAFTest(String name) { 128 super("RandomAccessFile." + name); 129 } 130 go()131 public void go() throws IOException { 132 this.raf = new RandomAccessFile(WRITEABLE_FILE, "rw"); 133 this.raf.close(); 134 super.go(); 135 } 136 137 } 138 139 abstract static class ReadOnlyRAFTest extends Test { 140 141 RandomAccessFile raf; 142 ReadOnlyRAFTest(String name)143 public ReadOnlyRAFTest(String name) { 144 super("RandomAccessFile." + name); 145 } 146 go()147 public void go() throws IOException { 148 this.raf = new RandomAccessFile(READABLE_FILE, "r"); 149 super.go(); 150 this.raf.close(); 151 } 152 153 } 154 155 go()156 static void go() throws Exception { 157 158 new Test("File.createNewFile") { 159 public void run() throws IOException { 160 new File(UNWRITEABLE_DIR, "foo").createNewFile(); 161 }}.go(); 162 163 new Test("File.getCanonicalpath") { 164 public void run() throws IOException { 165 new File(INVALID_PATH).getCanonicalPath(); 166 }}.go(); 167 168 new Test("FileInputStream(file)") { 169 public void run() throws IOException { 170 new FileInputStream(UNREADABLE_FILE); 171 }}.go(); 172 173 new Test("FileInputStream(dir)") { 174 public void run() throws IOException { 175 new FileInputStream("."); 176 }}.go(); 177 178 new ClosedFISTest("read()") { 179 public void run() throws IOException { 180 in.read(); 181 }}.go(); 182 183 new ClosedFISTest("read(byte[])") { 184 public void run() throws IOException { 185 byte[] b = new byte[10]; 186 in.read(b); 187 }}.go(); 188 189 new ClosedFISTest("skip") { 190 public void run() throws IOException { 191 in.skip(10); 192 }}.go(); 193 194 new ClosedFISTest("available") { 195 public void run() throws IOException { 196 in.available(); 197 }}.go(); 198 199 new Test("FileOutputStream") { 200 public void run() throws IOException { 201 new FileOutputStream(UNREADABLE_FILE); 202 }}.go(); 203 204 new ClosedFOSTest("write()") { 205 public void run() throws IOException { 206 out.write(10); 207 }}.go(); 208 209 new ClosedFOSTest("write(byte[])") { 210 public void run() throws IOException { 211 out.write(new byte[] { 1, 2, 3 }); 212 }}.go(); 213 214 new Test("RandomAccessFile") { 215 public void run() throws IOException { 216 new RandomAccessFile(UNREADABLE_FILE, "r"); 217 }}.go(); 218 219 new ClosedRAFTest("getFilePointer") { 220 public void run() throws IOException { 221 raf.getFilePointer(); 222 }}.go(); 223 224 new ClosedRAFTest("length") { 225 public void run() throws IOException { 226 raf.length(); 227 }}.go(); 228 229 new ClosedRAFTest("seek") { 230 public void run() throws IOException { 231 raf.seek(20); 232 }}.go(); 233 234 new ClosedRAFTest("setLength") { 235 public void run() throws IOException { 236 raf.setLength(0); 237 }}.go(); 238 239 new ClosedRAFTest("readShort") { 240 public void run() throws IOException { 241 raf.readShort(); 242 }}.go(); 243 244 new ClosedRAFTest("readInt") { 245 public void run() throws IOException { 246 raf.readInt(); 247 }}.go(); 248 249 new ReadOnlyRAFTest("writeShort") { 250 public void run() throws IOException { 251 raf.writeShort(10); 252 }}.go(); 253 254 new ReadOnlyRAFTest("getFilePointer") { 255 public void run() throws IOException { 256 raf.writeInt(10); 257 }}.go(); 258 259 } 260 261 main(String[] args)262 public static void main(String[] args) throws Exception { 263 go(); 264 } 265 266 } 267