Path: blob/master/test/jdk/java/nio/file/Files/TemporaryFiles.java
41153 views
/*1* Copyright (c) 2008, 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 4313887 6838333 7006126 702303425* @summary Unit test for Files.createTempXXX26* @library ..27*/2829import java.nio.file.*;30import static java.nio.file.StandardOpenOption.*;31import java.nio.file.attribute.*;32import java.io.IOException;33import java.util.Set;3435public class TemporaryFiles {3637static void checkInDirectory(Path file, Path dir) {38if (dir == null)39dir = Paths.get(System.getProperty("java.io.tmpdir"));40if (!file.getParent().equals(dir))41throw new RuntimeException("Not in expected directory");42}4344static void testTempFile(String prefix, String suffix, Path dir)45throws IOException46{47Path file = (dir == null) ?48Files.createTempFile(prefix, suffix) :49Files.createTempFile(dir, prefix, suffix);50try {51// check file name52String name = file.getFileName().toString();53if (prefix != null && !name.startsWith(prefix))54throw new RuntimeException("Should start with " + prefix);55if (suffix == null && !name.endsWith(".tmp"))56throw new RuntimeException("Should end with .tmp");57if (suffix != null && !name.endsWith(suffix))58throw new RuntimeException("Should end with " + suffix);5960// check file is in expected directory61checkInDirectory(file, dir);6263// check that file can be opened for reading and writing64Files.newByteChannel(file, READ).close();65Files.newByteChannel(file, WRITE).close();66Files.newByteChannel(file, READ,WRITE).close();6768// check file permissions are 0600 or more secure69if (Files.getFileStore(file).supportsFileAttributeView("posix")) {70Set<PosixFilePermission> perms = Files.getPosixFilePermissions(file);71perms.remove(PosixFilePermission.OWNER_READ);72perms.remove(PosixFilePermission.OWNER_WRITE);73if (!perms.isEmpty())74throw new RuntimeException("Temporary file is not secure");75}76} finally {77Files.delete(file);78}79}8081static void testTempFile(String prefix, String suffix)82throws IOException83{84testTempFile(prefix, suffix, null);85}8687static void testTempDirectory(String prefix, Path dir) throws IOException {88Path subdir = (dir == null) ?89Files.createTempDirectory(prefix) :90Files.createTempDirectory(dir, prefix);91try {92// check file name93String name = subdir.getFileName().toString();94if (prefix != null && !name.startsWith(prefix))95throw new RuntimeException("Should start with " + prefix);9697// check directory is in expected directory98checkInDirectory(subdir, dir);99100// check directory is empty101DirectoryStream<Path> stream = Files.newDirectoryStream(subdir);102try {103if (stream.iterator().hasNext())104throw new RuntimeException("Tempory directory not empty");105} finally {106stream.close();107}108109// check that we can create file in directory110Path file = Files.createFile(subdir.resolve("foo"));111try {112Files.newByteChannel(file, READ,WRITE).close();113} finally {114Files.delete(file);115}116117// check file permissions are 0700 or more secure118if (Files.getFileStore(subdir).supportsFileAttributeView("posix")) {119Set<PosixFilePermission> perms = Files.getPosixFilePermissions(subdir);120perms.remove(PosixFilePermission.OWNER_READ);121perms.remove(PosixFilePermission.OWNER_WRITE);122perms.remove(PosixFilePermission.OWNER_EXECUTE);123if (!perms.isEmpty())124throw new RuntimeException("Temporary directory is not secure");125}126} finally {127Files.delete(subdir);128}129}130131static void testTempDirectory(String prefix) throws IOException {132testTempDirectory(prefix, null);133}134135static void testInvalidFileTemp(String prefix, String suffix) throws IOException {136try {137Path file = Files.createTempFile(prefix, suffix);138Files.delete(file);139throw new RuntimeException("IllegalArgumentException expected");140} catch (IllegalArgumentException expected) { }141}142143public static void main(String[] args) throws IOException {144// temporary-file directory145testTempFile("blah", ".dat");146testTempFile("blah", null);147testTempFile(null, ".dat");148testTempFile(null, null);149testTempDirectory("blah");150testTempDirectory(null);151152// a given directory153Path dir = Files.createTempDirectory("tmpdir");154try {155testTempFile("blah", ".dat", dir);156testTempFile("blah", null, dir);157testTempFile(null, ".dat", dir);158testTempFile(null, null, dir);159testTempDirectory("blah", dir);160testTempDirectory(null, dir);161} finally {162Files.delete(dir);163}164165// invalid prefix and suffix166testInvalidFileTemp("../blah", null);167testInvalidFileTemp("dir/blah", null);168testInvalidFileTemp("blah", ".dat/foo");169170// nulls171try {172Files.createTempFile("blah", ".tmp", (FileAttribute<?>[])null);173throw new RuntimeException("NullPointerException expected");174} catch (NullPointerException ignore) { }175try {176Files.createTempFile("blah", ".tmp", new FileAttribute<?>[] { null });177throw new RuntimeException("NullPointerException expected");178} catch (NullPointerException ignore) { }179try {180Files.createTempDirectory("blah", (FileAttribute<?>[])null);181throw new RuntimeException("NullPointerException expected");182} catch (NullPointerException ignore) { }183try {184Files.createTempDirectory("blah", new FileAttribute<?>[] { null });185throw new RuntimeException("NullPointerException expected");186} catch (NullPointerException ignore) { }187try {188Files.createTempFile((Path)null, "blah", ".tmp");189throw new RuntimeException("NullPointerException expected");190} catch (NullPointerException ignore) { }191try {192Files.createTempDirectory((Path)null, "blah");193throw new RuntimeException("NullPointerException expected");194} catch (NullPointerException ignore) { }195}196}197198199