Path: blob/master/test/jdk/java/io/IOException/LastErrorString.java
41149 views
/*1* Copyright (c) 1998, 2011, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24@bug 416793725@ignore 7042603 - Test truncates system files when run as root26@summary Test code paths that use the JVM_LastErrorString procedure27*/2829import java.io.IOException;30import java.io.File;31import java.io.FileInputStream;32import java.io.FileOutputStream;33import java.io.RandomAccessFile;343536public class LastErrorString {3738static String UNWRITEABLE_DIR;39static String UNREADABLE_FILE;40static String READABLE_FILE;41static String WRITEABLE_FILE;42static String INVALID_PATH;4344static {45if (File.separatorChar == '/') {46UNWRITEABLE_DIR = "/etc/dfs";47UNREADABLE_FILE = "/etc/shadow";48} else if (File.separatorChar == '\\') {49UNREADABLE_FILE = "c:/pagefile.sys";50UNWRITEABLE_DIR = "z:/fooBAR/baz/GORP";51} else {52throw new RuntimeException("What kind of system is this?");53}54File d = new File(System.getProperty("test.src", "."));55READABLE_FILE = new File(d, "LastErrorString.java").getPath();56WRITEABLE_FILE = "x.LastErrorString";57String s = "foo/";58for (;;) {59s = s + s;60if (s.length() > 8192) break;61}62s += "bar";63INVALID_PATH = s;64}656667abstract static class Test {6869String name;7071public Test(String name) {72this.name = name;73}7475public abstract void run() throws IOException;7677public void go() throws IOException {78try {79this.run();80} catch (IOException x) {81System.err.println(name);82System.err.println(" " + x);83return;84}85System.err.println("WARNING: No exception for " + name);86}8788}8990abstract static class ClosedFISTest extends Test {9192FileInputStream in;9394public ClosedFISTest(String name) {95super("FileInputStream." + name);96}9798public void go() throws IOException {99this.in = new FileInputStream(READABLE_FILE);100this.in.close();101super.go();102}103104}105106abstract static class ClosedFOSTest extends Test {107108FileOutputStream out;109110public ClosedFOSTest(String name) {111super("FileOutputStream." + name);112}113114public void go() throws IOException {115this.out = new FileOutputStream(WRITEABLE_FILE);116this.out.close();117super.go();118}119120}121122abstract static class ClosedRAFTest extends Test {123124RandomAccessFile raf;125126public ClosedRAFTest(String name) {127super("RandomAccessFile." + name);128}129130public void go() throws IOException {131this.raf = new RandomAccessFile(WRITEABLE_FILE, "rw");132this.raf.close();133super.go();134}135136}137138abstract static class ReadOnlyRAFTest extends Test {139140RandomAccessFile raf;141142public ReadOnlyRAFTest(String name) {143super("RandomAccessFile." + name);144}145146public void go() throws IOException {147this.raf = new RandomAccessFile(READABLE_FILE, "r");148super.go();149this.raf.close();150}151152}153154155static void go() throws Exception {156157new Test("File.createNewFile") {158public void run() throws IOException {159new File(UNWRITEABLE_DIR, "foo").createNewFile();160}}.go();161162new Test("File.getCanonicalpath") {163public void run() throws IOException {164new File(INVALID_PATH).getCanonicalPath();165}}.go();166167new Test("FileInputStream(file)") {168public void run() throws IOException {169new FileInputStream(UNREADABLE_FILE);170}}.go();171172new Test("FileInputStream(dir)") {173public void run() throws IOException {174new FileInputStream(".");175}}.go();176177new ClosedFISTest("read()") {178public void run() throws IOException {179in.read();180}}.go();181182new ClosedFISTest("read(byte[])") {183public void run() throws IOException {184byte[] b = new byte[10];185in.read(b);186}}.go();187188new ClosedFISTest("skip") {189public void run() throws IOException {190in.skip(10);191}}.go();192193new ClosedFISTest("available") {194public void run() throws IOException {195in.available();196}}.go();197198new Test("FileOutputStream") {199public void run() throws IOException {200new FileOutputStream(UNREADABLE_FILE);201}}.go();202203new ClosedFOSTest("write()") {204public void run() throws IOException {205out.write(10);206}}.go();207208new ClosedFOSTest("write(byte[])") {209public void run() throws IOException {210out.write(new byte[] { 1, 2, 3 });211}}.go();212213new Test("RandomAccessFile") {214public void run() throws IOException {215new RandomAccessFile(UNREADABLE_FILE, "r");216}}.go();217218new ClosedRAFTest("getFilePointer") {219public void run() throws IOException {220raf.getFilePointer();221}}.go();222223new ClosedRAFTest("length") {224public void run() throws IOException {225raf.length();226}}.go();227228new ClosedRAFTest("seek") {229public void run() throws IOException {230raf.seek(20);231}}.go();232233new ClosedRAFTest("setLength") {234public void run() throws IOException {235raf.setLength(0);236}}.go();237238new ClosedRAFTest("readShort") {239public void run() throws IOException {240raf.readShort();241}}.go();242243new ClosedRAFTest("readInt") {244public void run() throws IOException {245raf.readInt();246}}.go();247248new ReadOnlyRAFTest("writeShort") {249public void run() throws IOException {250raf.writeShort(10);251}}.go();252253new ReadOnlyRAFTest("getFilePointer") {254public void run() throws IOException {255raf.writeInt(10);256}}.go();257258}259260261public static void main(String[] args) throws Exception {262go();263}264265}266267268