893N/A/*
3909N/A * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
893N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
893N/A *
893N/A * This code is free software; you can redistribute it and/or modify it
893N/A * under the terms of the GNU General Public License version 2 only, as
893N/A * published by the Free Software Foundation.
893N/A *
893N/A * This code is distributed in the hope that it will be useful, but WITHOUT
893N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
893N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
893N/A * version 2 for more details (a copy is included in the LICENSE file that
893N/A * accompanied this code).
893N/A *
893N/A * You should have received a copy of the GNU General Public License version
893N/A * 2 along with this work; if not, write to the Free Software Foundation,
893N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
893N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
893N/A */
893N/A
1319N/A/* @test
3661N/A * @bug 4313887 6838333 7006126 7023034
3471N/A * @summary Unit test for Files.createTempXXX
1319N/A * @library ..
1319N/A */
1319N/A
893N/Aimport java.nio.file.*;
893N/Aimport static java.nio.file.StandardOpenOption.*;
893N/Aimport java.nio.file.attribute.*;
893N/Aimport java.io.IOException;
893N/Aimport java.util.Set;
893N/A
893N/Apublic class TemporaryFiles {
893N/A
3471N/A static void checkInDirectory(Path file, Path dir) {
3471N/A if (dir == null)
3471N/A dir = Paths.get(System.getProperty("java.io.tmpdir"));
3471N/A if (!file.getParent().equals(dir))
3471N/A throw new RuntimeException("Not in expected directory");
1319N/A }
1319N/A
3471N/A static void testTempFile(String prefix, String suffix, Path dir)
3471N/A throws IOException
3471N/A {
3471N/A Path file = (dir == null) ?
3471N/A Files.createTempFile(prefix, suffix) :
3471N/A Files.createTempFile(dir, prefix, suffix);
3471N/A try {
3471N/A // check file name
3471N/A String name = file.getFileName().toString();
3471N/A if (prefix != null && !name.startsWith(prefix))
3471N/A throw new RuntimeException("Should start with " + prefix);
3471N/A if (suffix == null && !name.endsWith(".tmp"))
3471N/A throw new RuntimeException("Should end with .tmp");
3471N/A if (suffix != null && !name.endsWith(suffix))
3471N/A throw new RuntimeException("Should end with " + suffix);
893N/A
3471N/A // check file is in expected directory
3471N/A checkInDirectory(file, dir);
3471N/A
3471N/A // check that file can be opened for reading and writing
3471N/A Files.newByteChannel(file, READ).close();
3471N/A Files.newByteChannel(file, WRITE).close();
3471N/A Files.newByteChannel(file, READ,WRITE).close();
3471N/A
3471N/A // check file permissions are 0600 or more secure
3471N/A if (Files.getFileStore(file).supportsFileAttributeView("posix")) {
3471N/A Set<PosixFilePermission> perms = Files.getPosixFilePermissions(file);
3471N/A perms.remove(PosixFilePermission.OWNER_READ);
3471N/A perms.remove(PosixFilePermission.OWNER_WRITE);
3471N/A if (!perms.isEmpty())
3471N/A throw new RuntimeException("Temporary file is not secure");
3471N/A }
3471N/A } finally {
3471N/A Files.delete(file);
893N/A }
893N/A }
893N/A
3471N/A static void testTempFile(String prefix, String suffix)
3471N/A throws IOException
3471N/A {
3471N/A testTempFile(prefix, suffix, null);
3471N/A }
3471N/A
3471N/A static void testTempDirectory(String prefix, Path dir) throws IOException {
3471N/A Path subdir = (dir == null) ?
3471N/A Files.createTempDirectory(prefix) :
3471N/A Files.createTempDirectory(dir, prefix);
3471N/A try {
3471N/A // check file name
3471N/A String name = subdir.getFileName().toString();
3471N/A if (prefix != null && !name.startsWith(prefix))
3471N/A throw new RuntimeException("Should start with " + prefix);
3471N/A
3471N/A // check directory is in expected directory
3471N/A checkInDirectory(subdir, dir);
1319N/A
3471N/A // check directory is empty
3471N/A DirectoryStream<Path> stream = Files.newDirectoryStream(subdir);
3471N/A try {
3471N/A if (stream.iterator().hasNext())
3471N/A throw new RuntimeException("Tempory directory not empty");
3471N/A } finally {
3471N/A stream.close();
3471N/A }
3471N/A
3471N/A // check that we can create file in directory
3471N/A Path file = Files.createFile(subdir.resolve("foo"));
3471N/A try {
3471N/A Files.newByteChannel(file, READ,WRITE).close();
3471N/A } finally {
3471N/A Files.delete(file);
3471N/A }
3471N/A
3471N/A // check file permissions are 0700 or more secure
3471N/A if (Files.getFileStore(subdir).supportsFileAttributeView("posix")) {
3471N/A Set<PosixFilePermission> perms = Files.getPosixFilePermissions(subdir);
3471N/A perms.remove(PosixFilePermission.OWNER_READ);
3471N/A perms.remove(PosixFilePermission.OWNER_WRITE);
3471N/A perms.remove(PosixFilePermission.OWNER_EXECUTE);
3471N/A if (!perms.isEmpty())
3471N/A throw new RuntimeException("Temporary directory is not secure");
3471N/A }
1319N/A } finally {
3471N/A Files.delete(subdir);
1319N/A }
1319N/A }
1319N/A
3471N/A static void testTempDirectory(String prefix) throws IOException {
3471N/A testTempDirectory(prefix, null);
3471N/A }
3471N/A
3471N/A static void testInvalidFileTemp(String prefix, String suffix) throws IOException {
893N/A try {
3471N/A Path file = Files.createTempFile(prefix, suffix);
3471N/A Files.delete(file);
3471N/A throw new RuntimeException("IllegalArgumentException expected");
3471N/A } catch (IllegalArgumentException expected) { }
3471N/A }
3471N/A
3471N/A public static void main(String[] args) throws IOException {
3471N/A // temporary-file directory
3471N/A testTempFile("blah", ".dat");
3471N/A testTempFile("blah", null);
3471N/A testTempFile(null, ".dat");
3471N/A testTempFile(null, null);
3471N/A testTempDirectory("blah");
3471N/A testTempDirectory(null);
3471N/A
3471N/A // a given directory
3471N/A Path dir = Files.createTempDirectory("tmpdir");
3471N/A try {
3471N/A testTempFile("blah", ".dat", dir);
3471N/A testTempFile("blah", null, dir);
3471N/A testTempFile(null, ".dat", dir);
3471N/A testTempFile(null, null, dir);
3471N/A testTempDirectory("blah", dir);
3471N/A testTempDirectory(null, dir);
893N/A } finally {
3471N/A Files.delete(dir);
893N/A }
3471N/A
3471N/A // invalid prefix and suffix
3471N/A testInvalidFileTemp("../blah", null);
3471N/A testInvalidFileTemp("dir/blah", null);
3471N/A testInvalidFileTemp("blah", ".dat/foo");
3661N/A
3661N/A // nulls
3661N/A try {
3661N/A Files.createTempFile("blah", ".tmp", (FileAttribute<?>[])null);
3661N/A throw new RuntimeException("NullPointerException expected");
3661N/A } catch (NullPointerException ignore) { }
3661N/A try {
3661N/A Files.createTempFile("blah", ".tmp", new FileAttribute<?>[] { null });
3661N/A throw new RuntimeException("NullPointerException expected");
3661N/A } catch (NullPointerException ignore) { }
3661N/A try {
3661N/A Files.createTempDirectory("blah", (FileAttribute<?>[])null);
3661N/A throw new RuntimeException("NullPointerException expected");
3661N/A } catch (NullPointerException ignore) { }
3661N/A try {
3661N/A Files.createTempDirectory("blah", new FileAttribute<?>[] { null });
3661N/A throw new RuntimeException("NullPointerException expected");
3661N/A } catch (NullPointerException ignore) { }
3661N/A try {
3661N/A Files.createTempFile((Path)null, "blah", ".tmp");
3661N/A throw new RuntimeException("NullPointerException expected");
3661N/A } catch (NullPointerException ignore) { }
3661N/A try {
3661N/A Files.createTempDirectory((Path)null, "blah");
3661N/A throw new RuntimeException("NullPointerException expected");
3661N/A } catch (NullPointerException ignore) { }
893N/A }
893N/A}