NulFile.java revision 6156
0N/A/*
1472N/A * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
1472N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1472N/A * or visit www.oracle.com if you need additional information or have any
1472N/A * questions.
0N/A */
0N/A
0N/A/* @test
0N/A * @bug 8003992
0N/A * @summary Test a file whose path name is embedded with NUL character, and
0N/A * ensure it is handled correctly.
0N/A * @author Dan Xu
0N/A */
0N/A
0N/Aimport java.io.File;
0N/Aimport java.io.FileFilter;
0N/Aimport java.io.FileInputStream;
0N/Aimport java.io.FileOutputStream;
0N/Aimport java.io.RandomAccessFile;
0N/Aimport java.io.FileNotFoundException;
0N/Aimport java.io.FilenameFilter;
0N/Aimport java.io.IOException;
0N/Aimport java.net.MalformedURLException;
0N/Aimport java.nio.file.InvalidPathException;
0N/Aimport java.io.ByteArrayInputStream;
0N/Aimport java.io.ByteArrayOutputStream;
0N/Aimport java.io.ObjectOutputStream;
0N/Aimport java.io.ObjectInputStream;
0N/A
0N/Apublic class NulFile {
0N/A
0N/A private static final char CHAR_NUL = '\u0000';
0N/A
0N/A private static final String ExceptionMsg = "Invalid file path";
0N/A
0N/A public static void main(String[] args) {
0N/A testFile();
0N/A testFileInUnix();
0N/A testFileInWindows();
0N/A testTempFile();
0N/A }
0N/A
0N/A private static void testFile() {
0N/A test(new File(new StringBuilder().append(CHAR_NUL).toString()));
0N/A test(new File(
0N/A new StringBuilder().append("").append(CHAR_NUL).toString()));
0N/A test(new File(
0N/A new StringBuilder().append(CHAR_NUL).append("").toString()));
0N/A }
0N/A
0N/A private static void testFileInUnix() {
0N/A String osName = System.getProperty("os.name");
0N/A if (osName.startsWith("Windows"))
0N/A return;
0N/A
0N/A String unixFile = "/";
0N/A test(unixFile);
0N/A
0N/A unixFile = "//";
0N/A test(unixFile);
0N/A
0N/A unixFile = "data/info";
0N/A test(unixFile);
0N/A
0N/A unixFile = "/data/info";
0N/A test(unixFile);
0N/A
0N/A unixFile = "//data//info";
0N/A test(unixFile);
417N/A }
417N/A
0N/A private static void testFileInWindows() {
417N/A String osName = System.getProperty("os.name");
0N/A if (!osName.startsWith("Windows"))
417N/A return;
0N/A
0N/A String windowsFile = "\\";
0N/A test(windowsFile);
0N/A
0N/A windowsFile = "\\\\";
0N/A test(windowsFile);
0N/A
0N/A windowsFile = "/";
0N/A test(windowsFile);
0N/A
0N/A windowsFile = "//";
0N/A test(windowsFile);
0N/A
0N/A windowsFile = "/\\";
0N/A test(windowsFile);
0N/A
0N/A windowsFile = "\\/";
0N/A test(windowsFile);
1204N/A
1204N/A windowsFile = "data\\info";
1204N/A test(windowsFile);
1204N/A
1204N/A windowsFile = "\\data\\info";
0N/A test(windowsFile);
0N/A
0N/A windowsFile = "\\\\server\\data\\info";
0N/A test(windowsFile);
0N/A
0N/A windowsFile = "z:data\\info";
0N/A test(windowsFile);
0N/A
0N/A windowsFile = "z:\\data\\info";
0N/A test(windowsFile);
0N/A }
0N/A
0N/A private static void test(final String name) {
0N/A int length = name.length();
0N/A
0N/A for (int i = 0; i <= length; i++) {
0N/A StringBuilder sbName = new StringBuilder(name);
0N/A sbName.insert(i, CHAR_NUL);
0N/A String curName = sbName.toString();
0N/A
0N/A // test File(String parent, String child)
0N/A File testFile = new File(curName, "child");
0N/A test(testFile);
0N/A testFile = new File("parent", curName);
0N/A test(testFile);
0N/A
0N/A // test File(String pathname)
0N/A testFile = new File(curName);
0N/A test(testFile);
0N/A
0N/A // test File(File parent, String child)
0N/A testFile = new File(new File(curName), "child");
0N/A test(testFile);
0N/A testFile = new File(new File("parent"), curName);
0N/A test(testFile);
0N/A
0N/A // test FileInputStream
0N/A testFileInputStream(curName);
0N/A
0N/A // test FileOutputStream
0N/A testFileOutputStream(curName);
0N/A
0N/A // test RandomAccessFile
0N/A testRandomAccessFile(curName);
0N/A }
0N/A }
0N/A
0N/A private static void testFileInputStream(final String str) {
0N/A boolean exceptionThrown = false;
0N/A FileInputStream is = null;
0N/A try {
0N/A is = new FileInputStream(str);
0N/A } catch (FileNotFoundException ex) {
0N/A if (ExceptionMsg.equals(ex.getMessage()))
0N/A exceptionThrown = true;
0N/A }
0N/A if (!exceptionThrown) {
0N/A throw new RuntimeException("FileInputStream constructor"
0N/A + " should throw FileNotFoundException");
0N/A }
0N/A if (is != null) {
0N/A throw new RuntimeException("FileInputStream constructor"
0N/A + " should fail");
0N/A }
0N/A
0N/A exceptionThrown = false;
0N/A is = null;
0N/A try {
0N/A is = new FileInputStream(new File(str));
0N/A } catch (FileNotFoundException ex) {
0N/A if (ExceptionMsg.equals(ex.getMessage()))
0N/A exceptionThrown = true;
0N/A }
0N/A if (!exceptionThrown) {
0N/A throw new RuntimeException("FileInputStream constructor"
0N/A + " should throw FileNotFoundException");
0N/A }
0N/A if (is != null) {
0N/A throw new RuntimeException("FileInputStream constructor"
0N/A + " should fail");
0N/A }
0N/A }
0N/A
0N/A private static void testFileOutputStream(final String str) {
0N/A boolean exceptionThrown = false;
0N/A FileOutputStream os = null;
0N/A try {
0N/A os = new FileOutputStream(str);
0N/A } catch (FileNotFoundException ex) {
0N/A if (ExceptionMsg.equals(ex.getMessage()))
0N/A exceptionThrown = true;
0N/A }
0N/A if (!exceptionThrown) {
0N/A throw new RuntimeException("FileOutputStream constructor"
0N/A + " should throw FileNotFoundException");
0N/A }
0N/A if (os != null) {
0N/A throw new RuntimeException("FileOutputStream constructor"
0N/A + " should fail");
0N/A }
0N/A
0N/A exceptionThrown = false;
0N/A os = null;
1647N/A try {
1647N/A os = new FileOutputStream(new File(str));
0N/A } catch (FileNotFoundException ex) {
0N/A if (ExceptionMsg.equals(ex.getMessage()))
0N/A exceptionThrown = true;
0N/A }
1647N/A if (!exceptionThrown) {
1647N/A throw new RuntimeException("FileOutputStream constructor"
1647N/A + " should throw FileNotFoundException");
0N/A }
0N/A if (os != null) {
0N/A throw new RuntimeException("FileOutputStream constructor"
0N/A + " should fail");
0N/A }
0N/A }
0N/A
0N/A private static void testRandomAccessFile(final String str) {
0N/A boolean exceptionThrown = false;
0N/A RandomAccessFile raf = null;
0N/A String[] modes = {"r", "rw", "rws", "rwd"};
0N/A
0N/A for (String mode : modes) {
0N/A try {
0N/A raf = new RandomAccessFile(str, mode);
0N/A } catch (FileNotFoundException ex) {
0N/A if (ExceptionMsg.equals(ex.getMessage()))
0N/A exceptionThrown = true;
0N/A }
0N/A if (!exceptionThrown) {
0N/A throw new RuntimeException("RandomAccessFile constructor"
0N/A + " should throw FileNotFoundException");
1647N/A }
1647N/A if (raf != null) {
1647N/A throw new RuntimeException("RandomAccessFile constructor"
1647N/A + " should fail");
1647N/A }
0N/A
0N/A exceptionThrown = false;
0N/A raf = null;
0N/A try {
0N/A raf = new RandomAccessFile(new File(str), mode);
0N/A } catch (FileNotFoundException ex) {
0N/A if (ExceptionMsg.equals(ex.getMessage()))
0N/A exceptionThrown = true;
0N/A }
0N/A if (!exceptionThrown) {
0N/A throw new RuntimeException("RandomAccessFile constructor"
0N/A + " should throw FileNotFoundException");
0N/A }
0N/A if (raf != null) {
0N/A throw new RuntimeException("RandomAccessFile constructor"
0N/A + " should fail");
0N/A }
0N/A }
0N/A }
0N/A
0N/A private static void test(File testFile) {
0N/A test(testFile, false);
0N/A // test serialization
0N/A testSerialization(testFile);
1204N/A }
1204N/A
1204N/A @SuppressWarnings("deprecation")
1204N/A private static void test(File testFile, boolean derived) {
1204N/A boolean exceptionThrown = false;
1204N/A
0N/A if (testFile == null) {
0N/A throw new RuntimeException("test file should not be null.");
0N/A }
1204N/A
0N/A // getPath()
0N/A if (testFile.getPath().indexOf(CHAR_NUL) < 0) {
0N/A throw new RuntimeException(
0N/A "File path should contain Nul character");
0N/A }
0N/A // getAbsolutePath()
0N/A if (testFile.getAbsolutePath().indexOf(CHAR_NUL) < 0) {
0N/A throw new RuntimeException(
0N/A "File absolute path should contain Nul character");
0N/A }
0N/A // getAbsoluteFile()
0N/A File derivedAbsFile = testFile.getAbsoluteFile();
0N/A if (derived) {
0N/A if (derivedAbsFile.getPath().indexOf(CHAR_NUL) < 0) {
0N/A throw new RuntimeException(
0N/A "Derived file path should also contain Nul character");
0N/A }
0N/A } else {
0N/A test(derivedAbsFile, true);
0N/A }
0N/A // getCanonicalPath()
0N/A try {
0N/A exceptionThrown = false;
0N/A testFile.getCanonicalPath();
0N/A } catch (IOException ex) {
0N/A if (ExceptionMsg.equals(ex.getMessage()))
0N/A exceptionThrown = true;
0N/A }
0N/A if (!exceptionThrown) {
0N/A throw new RuntimeException(
0N/A "getCanonicalPath() should throw IOException with"
0N/A + " message \"" + ExceptionMsg + "\"");
0N/A }
0N/A // getCanonicalFile()
0N/A try {
0N/A exceptionThrown = false;
0N/A testFile.getCanonicalFile();
0N/A } catch (IOException ex) {
0N/A if (ExceptionMsg.equals(ex.getMessage()))
0N/A exceptionThrown = true;
0N/A }
0N/A if (!exceptionThrown) {
0N/A throw new RuntimeException(
0N/A "getCanonicalFile() should throw IOException with"
0N/A + " message \"" + ExceptionMsg + "\"");
0N/A }
0N/A // toURL()
0N/A try {
0N/A exceptionThrown = false;
0N/A testFile.toURL();
0N/A } catch (MalformedURLException ex) {
0N/A if (ExceptionMsg.equals(ex.getMessage()))
0N/A exceptionThrown = true;
0N/A }
0N/A if (!exceptionThrown) {
0N/A throw new RuntimeException("toURL() should throw IOException with"
0N/A + " message \"" + ExceptionMsg + "\"");
0N/A }
0N/A // canRead()
0N/A if (testFile.canRead())
0N/A throw new RuntimeException("File should not be readable");
0N/A // canWrite()
0N/A if (testFile.canWrite())
0N/A throw new RuntimeException("File should not be writable");
0N/A // exists()
0N/A if (testFile.exists())
0N/A throw new RuntimeException("File should not be existed");
0N/A // isDirectory()
0N/A if (testFile.isDirectory())
0N/A throw new RuntimeException("File should not be a directory");
0N/A // isFile()
0N/A if (testFile.isFile())
0N/A throw new RuntimeException("File should not be a file");
0N/A // isHidden()
0N/A if (testFile.isHidden())
0N/A throw new RuntimeException("File should not be hidden");
0N/A // lastModified()
0N/A if (testFile.lastModified() != 0L)
0N/A throw new RuntimeException("File last modified time should be 0L");
0N/A // length()
0N/A if (testFile.length() != 0L)
0N/A throw new RuntimeException("File length should be 0L");
0N/A // createNewFile()
0N/A try {
0N/A exceptionThrown = false;
0N/A testFile.createNewFile();
0N/A } catch (IOException ex) {
0N/A if (ExceptionMsg.equals(ex.getMessage()))
0N/A exceptionThrown = true;
0N/A }
0N/A if (!exceptionThrown) {
0N/A throw new RuntimeException(
0N/A "createNewFile() should throw IOException with"
0N/A + " message \"" + ExceptionMsg + "\"");
0N/A }
0N/A // delete()
0N/A if (testFile.delete())
0N/A throw new RuntimeException("Delete operation should fail");
0N/A // list()
0N/A if (testFile.list() != null)
0N/A throw new RuntimeException("File list() should return null");
0N/A // list(FilenameFilter)
0N/A FilenameFilter fnFilter = new FilenameFilter() {
0N/A @Override
0N/A public boolean accept(File dir, String name) {
0N/A return false;
0N/A }
0N/A };
0N/A if (testFile.list(fnFilter) != null) {
0N/A throw new RuntimeException("File list(FilenameFilter) should"
0N/A + " return null");
0N/A }
0N/A // listFiles()
0N/A if (testFile.listFiles() != null)
0N/A throw new RuntimeException("File listFiles() should return null");
0N/A // listFiles(FilenameFilter)
0N/A if (testFile.listFiles(fnFilter) != null) {
0N/A throw new RuntimeException("File listFiles(FilenameFilter)"
0N/A + " should return null");
0N/A }
0N/A // listFiles(FileFilter)
0N/A FileFilter fFilter = new FileFilter() {
0N/A @Override
0N/A public boolean accept(File file) {
0N/A return false;
0N/A }
0N/A };
0N/A if (testFile.listFiles(fFilter) != null) {
0N/A throw new RuntimeException("File listFiles(FileFilter)"
0N/A + " should return null");
0N/A }
0N/A // mkdir()
0N/A if (testFile.mkdir()) {
0N/A throw new RuntimeException("File should not be able to"
0N/A + " create directory");
0N/A }
0N/A // mkdirs()
0N/A if (testFile.mkdirs()) {
0N/A throw new RuntimeException("File should not be able to"
0N/A + " create directories");
0N/A }
0N/A // renameTo(File)
0N/A if (testFile.renameTo(new File("dest")))
0N/A throw new RuntimeException("File rename should fail");
0N/A if (new File("dest").renameTo(testFile))
0N/A throw new RuntimeException("File rename should fail");
0N/A try {
0N/A exceptionThrown = false;
0N/A testFile.renameTo(null);
0N/A } catch (NullPointerException ex) {
0N/A exceptionThrown = true;
0N/A }
0N/A if (!exceptionThrown) {
0N/A throw new RuntimeException("File rename should thrown NPE");
0N/A }
0N/A // setLastModified(long)
0N/A if (testFile.setLastModified(0L)) {
0N/A throw new RuntimeException("File should fail to set"
0N/A + " last modified time");
0N/A }
0N/A try {
0N/A exceptionThrown = false;
0N/A testFile.setLastModified(-1);
0N/A } catch (IllegalArgumentException ex) {
0N/A if ("Negative time".equals(ex.getMessage()))
0N/A exceptionThrown = true;
0N/A }
0N/A if (!exceptionThrown) {
0N/A throw new RuntimeException("File should fail to set"
0N/A + " last modified time with message \"Negative time\"");
0N/A }
0N/A // setReadOnly()
0N/A if (testFile.setReadOnly())
0N/A throw new RuntimeException("File should fail to set read-only");
0N/A // setWritable(boolean writable, boolean ownerOnly)
0N/A if (testFile.setWritable(true, true))
0N/A throw new RuntimeException("File should fail to set writable");
0N/A if (testFile.setWritable(true, false))
0N/A throw new RuntimeException("File should fail to set writable");
0N/A if (testFile.setWritable(false, true))
0N/A throw new RuntimeException("File should fail to set writable");
0N/A if (testFile.setWritable(false, false))
0N/A throw new RuntimeException("File should fail to set writable");
0N/A // setWritable(boolean writable)
0N/A if (testFile.setWritable(false))
0N/A throw new RuntimeException("File should fail to set writable");
0N/A if (testFile.setWritable(true))
1426N/A throw new RuntimeException("File should fail to set writable");
0N/A // setReadable(boolean readable, boolean ownerOnly)
0N/A if (testFile.setReadable(true, true))
0N/A throw new RuntimeException("File should fail to set readable");
0N/A if (testFile.setReadable(true, false))
0N/A throw new RuntimeException("File should fail to set readable");
0N/A if (testFile.setReadable(false, true))
1426N/A throw new RuntimeException("File should fail to set readable");
0N/A if (testFile.setReadable(false, false))
0N/A throw new RuntimeException("File should fail to set readable");
0N/A // setReadable(boolean readable)
0N/A if (testFile.setReadable(false))
0N/A throw new RuntimeException("File should fail to set readable");
0N/A if (testFile.setReadable(true))
0N/A throw new RuntimeException("File should fail to set readable");
0N/A // setExecutable(boolean executable, boolean ownerOnly)
0N/A if (testFile.setExecutable(true, true))
0N/A throw new RuntimeException("File should fail to set executable");
0N/A if (testFile.setExecutable(true, false))
0N/A throw new RuntimeException("File should fail to set executable");
0N/A if (testFile.setExecutable(false, true))
0N/A throw new RuntimeException("File should fail to set executable");
0N/A if (testFile.setExecutable(false, false))
0N/A throw new RuntimeException("File should fail to set executable");
0N/A // setExecutable(boolean executable)
0N/A if (testFile.setExecutable(false))
0N/A throw new RuntimeException("File should fail to set executable");
0N/A if (testFile.setExecutable(true))
0N/A throw new RuntimeException("File should fail to set executable");
0N/A // canExecute()
0N/A if (testFile.canExecute())
0N/A throw new RuntimeException("File should not be executable");
0N/A // getTotalSpace()
0N/A if (testFile.getTotalSpace() != 0L)
0N/A throw new RuntimeException("The total space should be 0L");
0N/A // getFreeSpace()
0N/A if (testFile.getFreeSpace() != 0L)
0N/A throw new RuntimeException("The free space should be 0L");
0N/A // getUsableSpace()
0N/A if (testFile.getUsableSpace() != 0L)
0N/A throw new RuntimeException("The usable space should be 0L");
0N/A // compareTo(File null)
0N/A try {
0N/A exceptionThrown = false;
0N/A testFile.compareTo(null);
0N/A } catch (NullPointerException ex) {
0N/A exceptionThrown = true;
0N/A }
0N/A if (!exceptionThrown) {
0N/A throw new RuntimeException("compareTo(null) should throw NPE");
0N/A }
0N/A // toString()
0N/A if (testFile.toString().indexOf(CHAR_NUL) < 0) {
0N/A throw new RuntimeException(
0N/A "File path should contain Nul character");
0N/A }
0N/A // toPath()
0N/A try {
0N/A exceptionThrown = false;
0N/A testFile.toPath();
0N/A } catch (InvalidPathException ex) {
0N/A exceptionThrown = true;
0N/A }
0N/A if (!exceptionThrown) {
0N/A throw new RuntimeException("toPath() should throw"
1668N/A + " InvalidPathException");
1668N/A }
0N/A }
0N/A
0N/A private static void testSerialization(File testFile) {
0N/A String path = testFile.getPath();
0N/A try {
0N/A // serialize test file
0N/A ByteArrayOutputStream baos = new ByteArrayOutputStream();
0N/A ObjectOutputStream oos = new ObjectOutputStream(baos);
0N/A oos.writeObject(testFile);
0N/A oos.close();
0N/A // deserialize test file
0N/A byte[] bytes = baos.toByteArray();
0N/A ByteArrayInputStream is = new ByteArrayInputStream(bytes);
0N/A ObjectInputStream ois = new ObjectInputStream(is);
0N/A File newFile = (File) ois.readObject();
0N/A // test
0N/A String newPath = newFile.getPath();
0N/A if (!path.equals(newPath)) {
0N/A throw new RuntimeException(
0N/A "Serialization should not change file path");
0N/A }
0N/A test(newFile, false);
0N/A } catch (IOException | ClassNotFoundException ex) {
0N/A System.err.println("Exception happens in testSerialization");
0N/A System.err.println(ex.getMessage());
0N/A }
0N/A }
0N/A
0N/A private static void testTempFile() {
0N/A final String[] names = {"x", "xx", "xxx", "xxxx"};
0N/A final String shortPrefix = "sp";
0N/A final String prefix = "prefix";
0N/A final String suffix = "suffix";
0N/A File tmpDir = new File("tmpDir");
0N/A
1255N/A for (String name : names) {
0N/A int length = name.length();
1255N/A for (int i = 0; i <= length; i++) {
1255N/A StringBuilder sbName = new StringBuilder(name);
0N/A sbName.insert(i, CHAR_NUL);
1255N/A String curName = sbName.toString();
0N/A
0N/A // test prefix
0N/A testCreateTempFile(curName, suffix, tmpDir);
0N/A // test suffix
0N/A testCreateTempFile(shortPrefix, curName, tmpDir);
0N/A testCreateTempFile(prefix, curName, tmpDir);
0N/A // test directory
0N/A testCreateTempFile(shortPrefix, suffix, new File(curName));
0N/A testCreateTempFile(prefix, suffix, new File(curName));
0N/A }
0N/A }
0N/A }
0N/A
0N/A private static void testCreateTempFile(String prefix, String suffix,
0N/A File directory) {
0N/A // createTempFile(String prefix, String suffix, File directory)
0N/A boolean exceptionThrown = false;
0N/A boolean shortPrefix = (prefix.length() < 3);
0N/A if (shortPrefix) {
0N/A try {
0N/A File.createTempFile(prefix, suffix, directory);
0N/A } catch (IllegalArgumentException ex) {
0N/A if ("Prefix string too short".equals(ex.getMessage()))
0N/A exceptionThrown = true;
0N/A } catch (IOException ioe) {
0N/A System.err.println("IOException happens in testCreateTempFile");
0N/A System.err.println(ioe.getMessage());
0N/A }
0N/A } else {
0N/A try {
0N/A File.createTempFile(prefix, suffix, directory);
0N/A } catch (IOException ex) {
0N/A if (ExceptionMsg.equals(ex.getMessage()))
0N/A exceptionThrown = true;
0N/A }
0N/A }
0N/A if (!exceptionThrown) {
0N/A throw new RuntimeException("createTempFile() should throw"
0N/A + (shortPrefix ? " IllegalArgumentException"
0N/A : " IOException"));
0N/A }
0N/A }
0N/A}
0N/A