Path: blob/master/test/jdk/java/io/File/NulFile.java
41149 views
/*1* Copyright (c) 2013, 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 8003992 802715525* @summary Test a file whose path name is embedded with NUL character, and26* ensure it is handled correctly.27* @author Dan Xu28*/2930import java.io.File;31import java.io.FileFilter;32import java.io.FileInputStream;33import java.io.FileOutputStream;34import java.io.RandomAccessFile;35import java.io.FileNotFoundException;36import java.io.FilenameFilter;37import java.io.IOException;38import java.net.MalformedURLException;39import java.nio.file.InvalidPathException;40import java.io.ByteArrayInputStream;41import java.io.ByteArrayOutputStream;42import java.io.ObjectOutputStream;43import java.io.ObjectInputStream;4445public class NulFile {4647private static final char CHAR_NUL = '\u0000';4849private static final String ExceptionMsg = "Invalid file path";5051public static void main(String[] args) {52testFile();53testFileInUnix();54testFileInWindows();55testTempFile();56}5758private static void testFile() {59test(new File(new StringBuilder().append(CHAR_NUL).toString()));60test(new File(61new StringBuilder().append("").append(CHAR_NUL).toString()));62test(new File(63new StringBuilder().append(CHAR_NUL).append("").toString()));64}6566private static void testFileInUnix() {67String osName = System.getProperty("os.name");68if (osName.startsWith("Windows"))69return;7071String unixFile = "/";72test(unixFile);7374unixFile = "//";75test(unixFile);7677unixFile = "data/info";78test(unixFile);7980unixFile = "/data/info";81test(unixFile);8283unixFile = "//data//info";84test(unixFile);85}8687private static void testFileInWindows() {88String osName = System.getProperty("os.name");89if (!osName.startsWith("Windows"))90return;9192String windowsFile = "\\";93test(windowsFile);9495windowsFile = "\\\\";96test(windowsFile);9798windowsFile = "/";99test(windowsFile);100101windowsFile = "//";102test(windowsFile);103104windowsFile = "/\\";105test(windowsFile);106107windowsFile = "\\/";108test(windowsFile);109110windowsFile = "data\\info";111test(windowsFile);112113windowsFile = "\\data\\info";114test(windowsFile);115116windowsFile = "\\\\server\\data\\info";117test(windowsFile);118119windowsFile = "z:data\\info";120test(windowsFile);121122windowsFile = "z:\\data\\info";123test(windowsFile);124}125126private static void test(final String name) {127int length = name.length();128129for (int i = 0; i <= length; i++) {130StringBuilder sbName = new StringBuilder(name);131sbName.insert(i, CHAR_NUL);132String curName = sbName.toString();133134// test File(String parent, String child)135File testFile = new File(curName, "child");136test(testFile);137testFile = new File("parent", curName);138test(testFile);139140// test File(String pathname)141testFile = new File(curName);142test(testFile);143144// test File(File parent, String child)145testFile = new File(new File(curName), "child");146test(testFile);147testFile = new File(new File("parent"), curName);148test(testFile);149150// test FileInputStream151testFileInputStream(curName);152153// test FileOutputStream154testFileOutputStream(curName);155156// test RandomAccessFile157testRandomAccessFile(curName);158}159}160161private static void testFileInputStream(final String str) {162boolean exceptionThrown = false;163FileInputStream is = null;164try {165is = new FileInputStream(str);166} catch (FileNotFoundException ex) {167if (ExceptionMsg.equals(ex.getMessage()))168exceptionThrown = true;169}170if (!exceptionThrown) {171throw new RuntimeException("FileInputStream constructor"172+ " should throw FileNotFoundException");173}174if (is != null) {175throw new RuntimeException("FileInputStream constructor"176+ " should fail");177}178179exceptionThrown = false;180is = null;181try {182is = new FileInputStream(new File(str));183} catch (FileNotFoundException ex) {184if (ExceptionMsg.equals(ex.getMessage()))185exceptionThrown = true;186}187if (!exceptionThrown) {188throw new RuntimeException("FileInputStream constructor"189+ " should throw FileNotFoundException");190}191if (is != null) {192throw new RuntimeException("FileInputStream constructor"193+ " should fail");194}195}196197private static void testFileOutputStream(final String str) {198boolean exceptionThrown = false;199FileOutputStream os = null;200try {201os = new FileOutputStream(str);202} catch (FileNotFoundException ex) {203if (ExceptionMsg.equals(ex.getMessage()))204exceptionThrown = true;205}206if (!exceptionThrown) {207throw new RuntimeException("FileOutputStream constructor"208+ " should throw FileNotFoundException");209}210if (os != null) {211throw new RuntimeException("FileOutputStream constructor"212+ " should fail");213}214215exceptionThrown = false;216os = null;217try {218os = new FileOutputStream(new File(str));219} catch (FileNotFoundException ex) {220if (ExceptionMsg.equals(ex.getMessage()))221exceptionThrown = true;222}223if (!exceptionThrown) {224throw new RuntimeException("FileOutputStream constructor"225+ " should throw FileNotFoundException");226}227if (os != null) {228throw new RuntimeException("FileOutputStream constructor"229+ " should fail");230}231}232233private static void testRandomAccessFile(final String str) {234boolean exceptionThrown = false;235RandomAccessFile raf = null;236String[] modes = {"r", "rw", "rws", "rwd"};237238for (String mode : modes) {239try {240raf = new RandomAccessFile(str, mode);241} catch (FileNotFoundException ex) {242if (ExceptionMsg.equals(ex.getMessage()))243exceptionThrown = true;244}245if (!exceptionThrown) {246throw new RuntimeException("RandomAccessFile constructor"247+ " should throw FileNotFoundException");248}249if (raf != null) {250throw new RuntimeException("RandomAccessFile constructor"251+ " should fail");252}253254exceptionThrown = false;255raf = null;256try {257raf = new RandomAccessFile(new File(str), mode);258} catch (FileNotFoundException ex) {259if (ExceptionMsg.equals(ex.getMessage()))260exceptionThrown = true;261}262if (!exceptionThrown) {263throw new RuntimeException("RandomAccessFile constructor"264+ " should throw FileNotFoundException");265}266if (raf != null) {267throw new RuntimeException("RandomAccessFile constructor"268+ " should fail");269}270}271}272273private static void test(File testFile) {274test(testFile, false);275// test serialization276testSerialization(testFile);277}278279@SuppressWarnings("deprecation")280private static void test(File testFile, boolean derived) {281boolean exceptionThrown = false;282283if (testFile == null) {284throw new RuntimeException("test file should not be null.");285}286287// getPath()288if (testFile.getPath().indexOf(CHAR_NUL) < 0) {289throw new RuntimeException(290"File path should contain Nul character");291}292// getAbsolutePath()293if (testFile.getAbsolutePath().indexOf(CHAR_NUL) < 0) {294throw new RuntimeException(295"File absolute path should contain Nul character");296}297// getAbsoluteFile()298File derivedAbsFile = testFile.getAbsoluteFile();299if (derived) {300if (derivedAbsFile.getPath().indexOf(CHAR_NUL) < 0) {301throw new RuntimeException(302"Derived file path should also contain Nul character");303}304} else {305test(derivedAbsFile, true);306}307// getCanonicalPath()308try {309exceptionThrown = false;310testFile.getCanonicalPath();311} catch (IOException ex) {312if (ExceptionMsg.equals(ex.getMessage()))313exceptionThrown = true;314}315if (!exceptionThrown) {316throw new RuntimeException(317"getCanonicalPath() should throw IOException with"318+ " message \"" + ExceptionMsg + "\"");319}320// getCanonicalFile()321try {322exceptionThrown = false;323testFile.getCanonicalFile();324} catch (IOException ex) {325if (ExceptionMsg.equals(ex.getMessage()))326exceptionThrown = true;327}328if (!exceptionThrown) {329throw new RuntimeException(330"getCanonicalFile() should throw IOException with"331+ " message \"" + ExceptionMsg + "\"");332}333// toURL()334try {335exceptionThrown = false;336testFile.toURL();337} catch (MalformedURLException ex) {338if (ExceptionMsg.equals(ex.getMessage()))339exceptionThrown = true;340}341if (!exceptionThrown) {342throw new RuntimeException("toURL() should throw IOException with"343+ " message \"" + ExceptionMsg + "\"");344}345// canRead()346if (testFile.canRead())347throw new RuntimeException("File should not be readable");348// canWrite()349if (testFile.canWrite())350throw new RuntimeException("File should not be writable");351// exists()352if (testFile.exists())353throw new RuntimeException("File should not be existed");354// isDirectory()355if (testFile.isDirectory())356throw new RuntimeException("File should not be a directory");357// isFile()358if (testFile.isFile())359throw new RuntimeException("File should not be a file");360// isHidden()361if (testFile.isHidden())362throw new RuntimeException("File should not be hidden");363// lastModified()364if (testFile.lastModified() != 0L)365throw new RuntimeException("File last modified time should be 0L");366// length()367if (testFile.length() != 0L)368throw new RuntimeException("File length should be 0L");369// createNewFile()370try {371exceptionThrown = false;372testFile.createNewFile();373} catch (IOException ex) {374if (ExceptionMsg.equals(ex.getMessage()))375exceptionThrown = true;376}377if (!exceptionThrown) {378throw new RuntimeException(379"createNewFile() should throw IOException with"380+ " message \"" + ExceptionMsg + "\"");381}382// delete()383if (testFile.delete())384throw new RuntimeException("Delete operation should fail");385// list()386if (testFile.list() != null)387throw new RuntimeException("File list() should return null");388// list(FilenameFilter)389FilenameFilter fnFilter = new FilenameFilter() {390@Override391public boolean accept(File dir, String name) {392return false;393}394};395if (testFile.list(fnFilter) != null) {396throw new RuntimeException("File list(FilenameFilter) should"397+ " return null");398}399// listFiles()400if (testFile.listFiles() != null)401throw new RuntimeException("File listFiles() should return null");402// listFiles(FilenameFilter)403if (testFile.listFiles(fnFilter) != null) {404throw new RuntimeException("File listFiles(FilenameFilter)"405+ " should return null");406}407// listFiles(FileFilter)408FileFilter fFilter = new FileFilter() {409@Override410public boolean accept(File file) {411return false;412}413};414if (testFile.listFiles(fFilter) != null) {415throw new RuntimeException("File listFiles(FileFilter)"416+ " should return null");417}418// mkdir()419if (testFile.mkdir()) {420throw new RuntimeException("File should not be able to"421+ " create directory");422}423// mkdirs()424if (testFile.mkdirs()) {425throw new RuntimeException("File should not be able to"426+ " create directories");427}428// renameTo(File)429if (testFile.renameTo(new File("dest")))430throw new RuntimeException("File rename should fail");431if (new File("dest").renameTo(testFile))432throw new RuntimeException("File rename should fail");433try {434exceptionThrown = false;435testFile.renameTo(null);436} catch (NullPointerException ex) {437exceptionThrown = true;438}439if (!exceptionThrown) {440throw new RuntimeException("File rename should thrown NPE");441}442// setLastModified(long)443if (testFile.setLastModified(0L)) {444throw new RuntimeException("File should fail to set"445+ " last modified time");446}447try {448exceptionThrown = false;449testFile.setLastModified(-1);450} catch (IllegalArgumentException ex) {451if ("Negative time".equals(ex.getMessage()))452exceptionThrown = true;453}454if (!exceptionThrown) {455throw new RuntimeException("File should fail to set"456+ " last modified time with message \"Negative time\"");457}458// setReadOnly()459if (testFile.setReadOnly())460throw new RuntimeException("File should fail to set read-only");461// setWritable(boolean writable, boolean ownerOnly)462if (testFile.setWritable(true, true))463throw new RuntimeException("File should fail to set writable");464if (testFile.setWritable(true, false))465throw new RuntimeException("File should fail to set writable");466if (testFile.setWritable(false, true))467throw new RuntimeException("File should fail to set writable");468if (testFile.setWritable(false, false))469throw new RuntimeException("File should fail to set writable");470// setWritable(boolean writable)471if (testFile.setWritable(false))472throw new RuntimeException("File should fail to set writable");473if (testFile.setWritable(true))474throw new RuntimeException("File should fail to set writable");475// setReadable(boolean readable, boolean ownerOnly)476if (testFile.setReadable(true, true))477throw new RuntimeException("File should fail to set readable");478if (testFile.setReadable(true, false))479throw new RuntimeException("File should fail to set readable");480if (testFile.setReadable(false, true))481throw new RuntimeException("File should fail to set readable");482if (testFile.setReadable(false, false))483throw new RuntimeException("File should fail to set readable");484// setReadable(boolean readable)485if (testFile.setReadable(false))486throw new RuntimeException("File should fail to set readable");487if (testFile.setReadable(true))488throw new RuntimeException("File should fail to set readable");489// setExecutable(boolean executable, boolean ownerOnly)490if (testFile.setExecutable(true, true))491throw new RuntimeException("File should fail to set executable");492if (testFile.setExecutable(true, false))493throw new RuntimeException("File should fail to set executable");494if (testFile.setExecutable(false, true))495throw new RuntimeException("File should fail to set executable");496if (testFile.setExecutable(false, false))497throw new RuntimeException("File should fail to set executable");498// setExecutable(boolean executable)499if (testFile.setExecutable(false))500throw new RuntimeException("File should fail to set executable");501if (testFile.setExecutable(true))502throw new RuntimeException("File should fail to set executable");503// canExecute()504if (testFile.canExecute())505throw new RuntimeException("File should not be executable");506// getTotalSpace()507if (testFile.getTotalSpace() != 0L)508throw new RuntimeException("The total space should be 0L");509// getFreeSpace()510if (testFile.getFreeSpace() != 0L)511throw new RuntimeException("The free space should be 0L");512// getUsableSpace()513if (testFile.getUsableSpace() != 0L)514throw new RuntimeException("The usable space should be 0L");515// compareTo(File null)516try {517exceptionThrown = false;518testFile.compareTo(null);519} catch (NullPointerException ex) {520exceptionThrown = true;521}522if (!exceptionThrown) {523throw new RuntimeException("compareTo(null) should throw NPE");524}525// toString()526if (testFile.toString().indexOf(CHAR_NUL) < 0) {527throw new RuntimeException(528"File path should contain Nul character");529}530// toPath()531try {532exceptionThrown = false;533testFile.toPath();534} catch (InvalidPathException ex) {535exceptionThrown = true;536}537if (!exceptionThrown) {538throw new RuntimeException("toPath() should throw"539+ " InvalidPathException");540}541}542543private static void testSerialization(File testFile) {544String path = testFile.getPath();545try {546// serialize test file547ByteArrayOutputStream baos = new ByteArrayOutputStream();548ObjectOutputStream oos = new ObjectOutputStream(baos);549oos.writeObject(testFile);550oos.close();551// deserialize test file552byte[] bytes = baos.toByteArray();553ByteArrayInputStream is = new ByteArrayInputStream(bytes);554ObjectInputStream ois = new ObjectInputStream(is);555File newFile = (File) ois.readObject();556// test557String newPath = newFile.getPath();558if (!path.equals(newPath)) {559throw new RuntimeException(560"Serialization should not change file path");561}562test(newFile, false);563} catch (IOException | ClassNotFoundException ex) {564System.err.println("Exception happens in testSerialization");565System.err.println(ex.getMessage());566}567}568569private static void testTempFile() {570final String[] names = {"x", "xx", "xxx", "xxxx"};571final String shortPrefix = "sp";572final String prefix = "prefix";573final String suffix = "suffix";574File tmpDir = new File("tmpDir");575576for (String name : names) {577int length = name.length();578for (int i = 0; i <= length; i++) {579StringBuilder sbName = new StringBuilder(name);580sbName.insert(i, CHAR_NUL);581String curName = sbName.toString();582583// test prefix584testCreateTempFile(curName, suffix, tmpDir);585// test suffix586testCreateTempFile(shortPrefix, curName, tmpDir);587testCreateTempFile(prefix, curName, tmpDir);588// test directory589testCreateTempFile(shortPrefix, suffix, new File(curName));590testCreateTempFile(prefix, suffix, new File(curName));591}592}593}594595private static void testCreateTempFile(String prefix, String suffix,596File directory) {597// createTempFile(String prefix, String suffix, File directory)598boolean exceptionThrown = false;599boolean shortPrefix = (prefix.length() < 3);600if (shortPrefix) {601try {602File.createTempFile(prefix, suffix, directory);603} catch (IllegalArgumentException ex) {604String actual = ex.getMessage();605String expected = "Prefix string \"" + prefix +606"\" too short: length must be at least 3";607if (actual != null && actual.equals(expected))608exceptionThrown = true;609} catch (IOException ioe) {610System.err.println("IOException happens in testCreateTempFile");611System.err.println(ioe.getMessage());612}613} else {614try {615File.createTempFile(prefix, suffix, directory);616} catch (IOException ex) {617String err = "Unable to create temporary file";618if (ex.getMessage() != null && ex.getMessage().startsWith(err))619exceptionThrown = true;620else {621throw new RuntimeException("Get IOException with message, "622+ ex.getMessage() + ", expect message, "+ err);623}624}625}626if (!exceptionThrown) {627throw new RuntimeException("createTempFile() should throw"628+ (shortPrefix ? " IllegalArgumentException"629: " IOException"));630}631}632}633634635