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