/* * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions.
*/
/* @test * @bug 8003992 8027155 * @summary Test a file whose path name is embedded with NUL character, and * ensure it is handled correctly. * @author Dan Xu
*/
if (testFile == null) { thrownew RuntimeException("test file should not be null.");
}
// getPath() if (testFile.getPath().indexOf(CHAR_NUL) < 0) { thrownew RuntimeException( "File path should contain Nul character");
} // getAbsolutePath() if (testFile.getAbsolutePath().indexOf(CHAR_NUL) < 0) { thrownew RuntimeException( "File absolute path should contain Nul character");
} // getAbsoluteFile()
File derivedAbsFile = testFile.getAbsoluteFile(); if (derived) { if (derivedAbsFile.getPath().indexOf(CHAR_NUL) < 0) { thrownew RuntimeException( "Derived file path should also contain Nul character");
}
} else {
test(derivedAbsFile, true);
} // getCanonicalPath() try {
exceptionThrown = false;
testFile.getCanonicalPath();
} catch (IOException ex) { if (ExceptionMsg.equals(ex.getMessage()))
exceptionThrown = true;
} if (!exceptionThrown) { thrownew RuntimeException( "getCanonicalPath() should throw IOException with"
+ " message \"" + ExceptionMsg + "\"");
} // getCanonicalFile() try {
exceptionThrown = false;
testFile.getCanonicalFile();
} catch (IOException ex) { if (ExceptionMsg.equals(ex.getMessage()))
exceptionThrown = true;
} if (!exceptionThrown) { thrownew RuntimeException( "getCanonicalFile() should throw IOException with"
+ " message \"" + ExceptionMsg + "\"");
} // toURL() try {
exceptionThrown = false;
testFile.toURL();
} catch (MalformedURLException ex) { if (ExceptionMsg.equals(ex.getMessage()))
exceptionThrown = true;
} if (!exceptionThrown) { thrownew RuntimeException("toURL() should throw IOException with"
+ " message \"" + ExceptionMsg + "\"");
} // canRead() if (testFile.canRead()) thrownew RuntimeException("File should not be readable"); // canWrite() if (testFile.canWrite()) thrownew RuntimeException("File should not be writable"); // exists() if (testFile.exists()) thrownew RuntimeException("File should not be existed"); // isDirectory() if (testFile.isDirectory()) thrownew RuntimeException("File should not be a directory"); // isFile() if (testFile.isFile()) thrownew RuntimeException("File should not be a file"); // isHidden() if (testFile.isHidden()) thrownew RuntimeException("File should not be hidden"); // lastModified() if (testFile.lastModified() != 0L) thrownew RuntimeException("File last modified time should be 0L"); // length() if (testFile.length() != 0L) thrownew RuntimeException("File length should be 0L"); // createNewFile() try {
exceptionThrown = false;
testFile.createNewFile();
} catch (IOException ex) { if (ExceptionMsg.equals(ex.getMessage()))
exceptionThrown = true;
} if (!exceptionThrown) { thrownew RuntimeException( "createNewFile() should throw IOException with"
+ " message \"" + ExceptionMsg + "\"");
} // delete() if (testFile.delete()) thrownew RuntimeException("Delete operation should fail"); // list() if (testFile.list() != null) thrownew RuntimeException("File list() should return null"); // list(FilenameFilter)
FilenameFilter fnFilter = new FilenameFilter() {
@Override publicboolean accept(File dir, String name) { returnfalse;
}
}; if (testFile.list(fnFilter) != null) { thrownew RuntimeException("File list(FilenameFilter) should"
+ " return null");
} // listFiles() if (testFile.listFiles() != null) thrownew RuntimeException("File listFiles() should return null"); // listFiles(FilenameFilter) if (testFile.listFiles(fnFilter) != null) { thrownew RuntimeException("File listFiles(FilenameFilter)"
+ " should return null");
} // listFiles(FileFilter)
FileFilter fFilter = new FileFilter() {
@Override publicboolean accept(File file) { returnfalse;
}
}; if (testFile.listFiles(fFilter) != null) { thrownew RuntimeException("File listFiles(FileFilter)"
+ " should return null");
} // mkdir() if (testFile.mkdir()) { thrownew RuntimeException("File should not be able to"
+ " create directory");
} // mkdirs() if (testFile.mkdirs()) { thrownew RuntimeException("File should not be able to"
+ " create directories");
} // renameTo(File) if (testFile.renameTo(new File("dest"))) thrownew RuntimeException("File rename should fail"); if (new File("dest").renameTo(testFile)) thrownew RuntimeException("File rename should fail"); try {
exceptionThrown = false;
testFile.renameTo(null);
} catch (NullPointerException ex) {
exceptionThrown = true;
} if (!exceptionThrown) { thrownew RuntimeException("File rename should thrown NPE");
} // setLastModified(long) if (testFile.setLastModified(0L)) { thrownew RuntimeException("File should fail to set"
+ " last modified time");
} try {
exceptionThrown = false;
testFile.setLastModified(-1);
} catch (IllegalArgumentException ex) { if ("Negative time".equals(ex.getMessage()))
exceptionThrown = true;
} if (!exceptionThrown) { thrownew RuntimeException("File should fail to set"
+ " last modified time with message \"Negative time\"");
} // setReadOnly() if (testFile.setReadOnly()) thrownew RuntimeException("File should fail to set read-only"); // setWritable(boolean writable, boolean ownerOnly) if (testFile.setWritable(true, true)) thrownew RuntimeException("File should fail to set writable"); if (testFile.setWritable(true, false)) thrownew RuntimeException("File should fail to set writable"); if (testFile.setWritable(false, true)) thrownew RuntimeException("File should fail to set writable"); if (testFile.setWritable(false, false)) thrownew RuntimeException("File should fail to set writable"); // setWritable(boolean writable) if (testFile.setWritable(false)) thrownew RuntimeException("File should fail to set writable"); if (testFile.setWritable(true)) thrownew RuntimeException("File should fail to set writable"); // setReadable(boolean readable, boolean ownerOnly) if (testFile.setReadable(true, true)) thrownew RuntimeException("File should fail to set readable"); if (testFile.setReadable(true, false)) thrownew RuntimeException("File should fail to set readable"); if (testFile.setReadable(false, true)) thrownew RuntimeException("File should fail to set readable"); if (testFile.setReadable(false, false)) thrownew RuntimeException("File should fail to set readable"); // setReadable(boolean readable) if (testFile.setReadable(false)) thrownew RuntimeException("File should fail to set readable"); if (testFile.setReadable(true)) thrownew RuntimeException("File should fail to set readable"); // setExecutable(boolean executable, boolean ownerOnly) if (testFile.setExecutable(true, true)) thrownew RuntimeException("File should fail to set executable"); if (testFile.setExecutable(true, false)) thrownew RuntimeException("File should fail to set executable"); if (testFile.setExecutable(false, true)) thrownew RuntimeException("File should fail to set executable"); if (testFile.setExecutable(false, false)) thrownew RuntimeException("File should fail to set executable"); // setExecutable(boolean executable) if (testFile.setExecutable(false)) thrownew RuntimeException("File should fail to set executable"); if (testFile.setExecutable(true)) thrownew RuntimeException("File should fail to set executable"); // canExecute() if (testFile.canExecute()) thrownew RuntimeException("File should not be executable"); // getTotalSpace() if (testFile.getTotalSpace() != 0L) thrownew RuntimeException("The total space should be 0L"); // getFreeSpace() if (testFile.getFreeSpace() != 0L) thrownew RuntimeException("The free space should be 0L"); // getUsableSpace() if (testFile.getUsableSpace() != 0L) thrownew RuntimeException("The usable space should be 0L"); // compareTo(File null) try {
exceptionThrown = false;
testFile.compareTo(null);
} catch (NullPointerException ex) {
exceptionThrown = true;
} if (!exceptionThrown) { thrownew RuntimeException("compareTo(null) should throw NPE");
} // toString() if (testFile.toString().indexOf(CHAR_NUL) < 0) { thrownew RuntimeException( "File path should contain Nul character");
} // toPath() try {
exceptionThrown = false;
testFile.toPath();
} catch (InvalidPathException ex) {
exceptionThrown = true;
} if (!exceptionThrown) { thrownew RuntimeException("toPath() should throw"
+ " InvalidPathException");
}
}
privatestaticvoid testSerialization(File testFile) {
String path = testFile.getPath(); try { // serialize test file
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(testFile);
oos.close(); // deserialize test file byte[] bytes = baos.toByteArray();
ByteArrayInputStream is = new ByteArrayInputStream(bytes);
ObjectInputStream ois = new ObjectInputStream(is);
File newFile = (File) ois.readObject(); // test
String newPath = newFile.getPath(); if (!path.equals(newPath)) { thrownew RuntimeException( "Serialization should not change file path");
}
test(newFile, false);
} catch (IOException | ClassNotFoundException ex) {
System.err.println("Exception happens in testSerialization");
System.err.println(ex.getMessage());
}
}
privatestaticvoid testTempFile() { final String[] names = {"x", "xx", "xxx", "xxxx"}; final String shortPrefix = "sp"; final String prefix = "prefix"; final String suffix = "suffix";
File tmpDir = new File("tmpDir");
for (String name : names) { int length = name.length(); for (int i = 0; i <= length; i++) {
StringBuilder sbName = new StringBuilder(name);
sbName.insert(i, CHAR_NUL);
String curName = sbName.toString();
// test prefix
testCreateTempFile(curName, suffix, tmpDir); // test suffix
testCreateTempFile(shortPrefix, curName, tmpDir);
testCreateTempFile(prefix, curName, tmpDir); // test directory
testCreateTempFile(shortPrefix, suffix, new File(curName));
testCreateTempFile(prefix, suffix, new File(curName));
}
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.