NulFile.java revision 6092
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* 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
* @summary Test a file whose path name is embedded with NUL character, and
* ensure it is handled correctly.
* @author Dan Xu
*/
public class NulFile {
private static final char CHAR_NUL = '\u0000';
testFile();
testTempFile();
}
private static void testFile() {
}
private static void testFileInUnix() {
return;
unixFile = "//";
unixFile = "//data//info";
}
private static void testFileInWindows() {
return;
windowsFile = "\\\\";
windowsFile = "/";
windowsFile = "//";
windowsFile = "/\\";
windowsFile = "\\/";
windowsFile = "data\\info";
windowsFile = "\\data\\info";
windowsFile = "\\\\server\\data\\info";
windowsFile = "z:data\\info";
windowsFile = "z:\\data\\info";
}
for (int i = 0; i <= length; i++) {
// test File(String parent, String child)
// test File(String pathname)
// test File(File parent, String child)
// test FileInputStream
// test FileOutputStream
// test RandomAccessFile
}
}
boolean exceptionThrown = false;
try {
} catch (FileNotFoundException ex) {
exceptionThrown = true;
}
if (!exceptionThrown) {
throw new RuntimeException("FileInputStream constructor"
+ " should throw FileNotFoundException");
}
throw new RuntimeException("FileInputStream constructor"
+ " should fail");
}
exceptionThrown = false;
try {
} catch (FileNotFoundException ex) {
exceptionThrown = true;
}
if (!exceptionThrown) {
throw new RuntimeException("FileInputStream constructor"
+ " should throw FileNotFoundException");
}
throw new RuntimeException("FileInputStream constructor"
+ " should fail");
}
}
boolean exceptionThrown = false;
try {
} catch (FileNotFoundException ex) {
exceptionThrown = true;
}
if (!exceptionThrown) {
throw new RuntimeException("FileOutputStream constructor"
+ " should throw FileNotFoundException");
}
throw new RuntimeException("FileOutputStream constructor"
+ " should fail");
}
exceptionThrown = false;
try {
} catch (FileNotFoundException ex) {
exceptionThrown = true;
}
if (!exceptionThrown) {
throw new RuntimeException("FileOutputStream constructor"
+ " should throw FileNotFoundException");
}
throw new RuntimeException("FileOutputStream constructor"
+ " should fail");
}
}
boolean exceptionThrown = false;
try {
} catch (FileNotFoundException ex) {
exceptionThrown = true;
}
if (!exceptionThrown) {
throw new RuntimeException("RandomAccessFile constructor"
+ " should throw FileNotFoundException");
}
throw new RuntimeException("RandomAccessFile constructor"
+ " should fail");
}
exceptionThrown = false;
try {
} catch (FileNotFoundException ex) {
exceptionThrown = true;
}
if (!exceptionThrown) {
throw new RuntimeException("RandomAccessFile constructor"
+ " should throw FileNotFoundException");
}
throw new RuntimeException("RandomAccessFile constructor"
+ " should fail");
}
}
}
// test serialization
}
@SuppressWarnings("deprecation")
boolean exceptionThrown = false;
throw new RuntimeException("test file should not be null.");
}
// getPath()
throw new RuntimeException(
"File path should contain Nul character");
}
// getAbsolutePath()
throw new RuntimeException(
"File absolute path should contain Nul character");
}
// getAbsoluteFile()
if (derived) {
throw new RuntimeException(
"Derived file path should also contain Nul character");
}
} else {
test(derivedAbsFile, true);
}
// getCanonicalPath()
try {
exceptionThrown = false;
} catch (IOException ex) {
exceptionThrown = true;
}
if (!exceptionThrown) {
throw new RuntimeException(
"getCanonicalPath() should throw IOException with"
}
// getCanonicalFile()
try {
exceptionThrown = false;
} catch (IOException ex) {
exceptionThrown = true;
}
if (!exceptionThrown) {
throw new RuntimeException(
"getCanonicalFile() should throw IOException with"
}
// toURL()
try {
exceptionThrown = false;
} catch (MalformedURLException ex) {
exceptionThrown = true;
}
if (!exceptionThrown) {
throw new RuntimeException("toURL() should throw IOException with"
}
// canRead()
throw new RuntimeException("File should not be readable");
// canWrite()
throw new RuntimeException("File should not be writable");
// exists()
throw new RuntimeException("File should not be existed");
// isDirectory()
if (testFile.isDirectory())
throw new RuntimeException("File should not be a directory");
// isFile()
throw new RuntimeException("File should not be a file");
// isHidden()
throw new RuntimeException("File should not be hidden");
// lastModified()
throw new RuntimeException("File last modified time should be 0L");
// length()
throw new RuntimeException("File length should be 0L");
// createNewFile()
try {
exceptionThrown = false;
} catch (IOException ex) {
exceptionThrown = true;
}
if (!exceptionThrown) {
throw new RuntimeException(
"createNewFile() should throw IOException with"
}
// delete()
throw new RuntimeException("Delete operation should fail");
// list()
throw new RuntimeException("File list() should return null");
// list(FilenameFilter)
return false;
}
};
throw new RuntimeException("File list(FilenameFilter) should"
+ " return null");
}
// listFiles()
throw new RuntimeException("File listFiles() should return null");
// listFiles(FilenameFilter)
throw new RuntimeException("File listFiles(FilenameFilter)"
+ " should return null");
}
// listFiles(FileFilter)
return false;
}
};
throw new RuntimeException("File listFiles(FileFilter)"
+ " should return null");
}
// mkdir()
throw new RuntimeException("File should not be able to"
+ " create directory");
}
// mkdirs()
throw new RuntimeException("File should not be able to"
+ " create directories");
}
// renameTo(File)
throw new RuntimeException("File rename should fail");
throw new RuntimeException("File rename should fail");
try {
exceptionThrown = false;
} catch (NullPointerException ex) {
exceptionThrown = true;
}
if (!exceptionThrown) {
throw new RuntimeException("File rename should thrown NPE");
}
// setLastModified(long)
throw new RuntimeException("File should fail to set"
+ " last modified time");
}
try {
exceptionThrown = false;
} catch (IllegalArgumentException ex) {
exceptionThrown = true;
}
if (!exceptionThrown) {
throw new RuntimeException("File should fail to set"
+ " last modified time with message \"Negative time\"");
}
// setReadOnly()
if (testFile.setReadOnly())
throw new RuntimeException("File should fail to set read-only");
// setWritable(boolean writable, boolean ownerOnly)
if (testFile.setWritable(true, true))
throw new RuntimeException("File should fail to set writable");
if (testFile.setWritable(true, false))
throw new RuntimeException("File should fail to set writable");
if (testFile.setWritable(false, true))
throw new RuntimeException("File should fail to set writable");
if (testFile.setWritable(false, false))
throw new RuntimeException("File should fail to set writable");
// setWritable(boolean writable)
if (testFile.setWritable(false))
throw new RuntimeException("File should fail to set writable");
if (testFile.setWritable(true))
throw new RuntimeException("File should fail to set writable");
// setReadable(boolean readable, boolean ownerOnly)
if (testFile.setReadable(true, true))
throw new RuntimeException("File should fail to set readable");
if (testFile.setReadable(true, false))
throw new RuntimeException("File should fail to set readable");
if (testFile.setReadable(false, true))
throw new RuntimeException("File should fail to set readable");
if (testFile.setReadable(false, false))
throw new RuntimeException("File should fail to set readable");
// setReadable(boolean readable)
if (testFile.setReadable(false))
throw new RuntimeException("File should fail to set readable");
if (testFile.setReadable(true))
throw new RuntimeException("File should fail to set readable");
// setExecutable(boolean executable, boolean ownerOnly)
if (testFile.setExecutable(true, true))
throw new RuntimeException("File should fail to set executable");
if (testFile.setExecutable(true, false))
throw new RuntimeException("File should fail to set executable");
if (testFile.setExecutable(false, true))
throw new RuntimeException("File should fail to set executable");
if (testFile.setExecutable(false, false))
throw new RuntimeException("File should fail to set executable");
// setExecutable(boolean executable)
if (testFile.setExecutable(false))
throw new RuntimeException("File should fail to set executable");
if (testFile.setExecutable(true))
throw new RuntimeException("File should fail to set executable");
// canExecute()
if (testFile.canExecute())
throw new RuntimeException("File should not be executable");
// getTotalSpace()
throw new RuntimeException("The total space should be 0L");
// getFreeSpace()
throw new RuntimeException("The free space should be 0L");
// getUsableSpace()
throw new RuntimeException("The usable space should be 0L");
// compareTo(File null)
try {
exceptionThrown = false;
} catch (NullPointerException ex) {
exceptionThrown = true;
}
if (!exceptionThrown) {
throw new RuntimeException("compareTo(null) should throw NPE");
}
// toString()
throw new RuntimeException(
"File path should contain Nul character");
}
// toPath()
try {
exceptionThrown = false;
} catch (InvalidPathException ex) {
exceptionThrown = true;
}
if (!exceptionThrown) {
throw new RuntimeException("toPath() should throw"
+ " InvalidPathException");
}
}
try {
// serialize test file
// deserialize test file
// test
throw new RuntimeException(
"Serialization should not change file path");
}
}
}
private static void testTempFile() {
for (int i = 0; i <= length; i++) {
// test prefix
// test suffix
// test directory
}
}
}
// createTempFile(String prefix, String suffix, File directory)
boolean exceptionThrown = false;
if (shortPrefix) {
try {
} catch (IllegalArgumentException ex) {
exceptionThrown = true;
} catch (IOException ioe) {
}
} else {
try {
} catch (IOException ex) {
exceptionThrown = true;
}
}
if (!exceptionThrown) {
throw new RuntimeException("createTempFile() should throw"
+ (shortPrefix ? " IllegalArgumentException"
: " IOException"));
}
}
}