Path: blob/master/test/jdk/java/nio/file/Files/Misc.java
41153 views
/*1* Copyright (c) 2008, 2019, 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 8005566 8032220 8215467 825557625* @summary Unit test for miscellenous methods in java.nio.file.Files26* @library ..27*/2829import java.nio.file.*;30import static java.nio.file.Files.*;31import static java.nio.file.LinkOption.*;32import java.nio.file.attribute.*;33import java.io.IOException;34import java.util.*;3536public class Misc {3738public static void main(String[] args) throws IOException {39Path dir = TestUtil.createTemporaryDirectory();40try {41testCreateDirectories(dir);42testIsHidden(dir);43testIsSameFile(dir);44testFileTypeMethods(dir);45testAccessMethods(dir);46} finally {47TestUtil.removeAll(dir);48}49}5051/**52* Tests createDirectories53*/54static void testCreateDirectories(Path tmpdir) throws IOException {55// a no-op56createDirectories(tmpdir);5758// create one directory59Path subdir = tmpdir.resolve("a");60createDirectories(subdir);61assertTrue(exists(subdir));6263// create parents64subdir = subdir.resolve("b/c/d");65createDirectories(subdir);66assertTrue(exists(subdir));6768// existing file is not a directory69Path file = createFile(tmpdir.resolve("x"));70try {71createDirectories(file);72throw new RuntimeException("failure expected");73} catch (FileAlreadyExistsException x) { }74try {75createDirectories(file.resolve("y"));76throw new RuntimeException("failure expected");77} catch (IOException x) { }7879// the root directory always exists80Path root = Paths.get("/");81Files.createDirectories(root);82Files.createDirectories(root.toAbsolutePath());83}8485/**86* Tests isHidden87*/88static void testIsHidden(Path tmpdir) throws IOException {89// passing an empty path must not throw any runtime exception90assertTrue(!isHidden(Path.of("")));9192assertTrue(!isHidden(tmpdir));9394Path file = tmpdir.resolve(".foo");95if (System.getProperty("os.name").startsWith("Windows")) {96createFile(file);97try {98setAttribute(file, "dos:hidden", true);99try {100assertTrue(isHidden(file));101} finally {102setAttribute(file, "dos:hidden", false);103}104} finally {105delete(file);106}107Path dir = tmpdir.resolve("hidden");108createDirectory(dir);109try {110setAttribute(dir, "dos:hidden", true);111try {112assertTrue(isHidden(dir));113} finally {114setAttribute(dir, "dos:hidden", false);115}116} finally {117delete(dir);118}119} else {120assertTrue(isHidden(file));121}122}123124/**125* Tests isSameFile126*/127static void testIsSameFile(Path tmpdir) throws IOException {128Path thisFile = tmpdir.resolve("thisFile");129Path thatFile = tmpdir.resolve("thatFile");130131/**132* Test: isSameFile for self133*/134assertTrue(isSameFile(thisFile, thisFile));135136/**137* Test: Neither files exist138*/139try {140isSameFile(thisFile, thatFile);141throw new RuntimeException("IOException not thrown");142} catch (IOException x) {143}144try {145isSameFile(thatFile, thisFile);146throw new RuntimeException("IOException not thrown");147} catch (IOException x) {148}149150createFile(thisFile);151try {152/**153* Test: One file exists154*/155try {156isSameFile(thisFile, thatFile);157throw new RuntimeException("IOException not thrown");158} catch (IOException x) {159}160try {161isSameFile(thatFile, thisFile);162throw new RuntimeException("IOException not thrown");163} catch (IOException x) {164}165166/**167* Test: Both file exists168*/169createFile(thatFile);170try {171assertTrue(!isSameFile(thisFile, thatFile));172assertTrue(!isSameFile(thatFile, thisFile));173} finally {174delete(thatFile);175}176177/**178* Test: Symbolic links179*/180if (TestUtil.supportsLinks(tmpdir)) {181createSymbolicLink(thatFile, thisFile);182try {183assertTrue(isSameFile(thisFile, thatFile));184assertTrue(isSameFile(thatFile, thisFile));185} finally {186TestUtil.deleteUnchecked(thatFile);187}188}189} finally {190delete(thisFile);191}192193// nulls194try {195isSameFile(thisFile, null);196throw new RuntimeException("NullPointerException expected");197} catch (NullPointerException ignore) { }198try {199isSameFile(null, thatFile);200throw new RuntimeException("NullPointerException expected");201} catch (NullPointerException ignore) { }202}203204/**205* Exercise isRegularFile, isDirectory, isSymbolicLink206*/207static void testFileTypeMethods(Path tmpdir) throws IOException {208assertTrue(!isRegularFile(tmpdir));209assertTrue(!isRegularFile(tmpdir, NOFOLLOW_LINKS));210assertTrue(isDirectory(tmpdir));211assertTrue(isDirectory(tmpdir, NOFOLLOW_LINKS));212assertTrue(!isSymbolicLink(tmpdir));213214Path file = createFile(tmpdir.resolve("foo"));215try {216assertTrue(isRegularFile(file));217assertTrue(isRegularFile(file, NOFOLLOW_LINKS));218assertTrue(!isDirectory(file));219assertTrue(!isDirectory(file, NOFOLLOW_LINKS));220assertTrue(!isSymbolicLink(file));221222if (TestUtil.supportsLinks(tmpdir)) {223Path link = tmpdir.resolve("link");224225createSymbolicLink(link, tmpdir);226try {227assertTrue(!isRegularFile(link));228assertTrue(!isRegularFile(link, NOFOLLOW_LINKS));229assertTrue(isDirectory(link));230assertTrue(!isDirectory(link, NOFOLLOW_LINKS));231assertTrue(isSymbolicLink(link));232} finally {233delete(link);234}235236createSymbolicLink(link, file);237try {238assertTrue(isRegularFile(link));239assertTrue(!isRegularFile(link, NOFOLLOW_LINKS));240assertTrue(!isDirectory(link));241assertTrue(!isDirectory(link, NOFOLLOW_LINKS));242assertTrue(isSymbolicLink(link));243} finally {244delete(link);245}246247createLink(link, file);248try {249assertTrue(isRegularFile(link));250assertTrue(isRegularFile(link, NOFOLLOW_LINKS));251assertTrue(!isDirectory(link));252assertTrue(!isDirectory(link, NOFOLLOW_LINKS));253assertTrue(!isSymbolicLink(link));254} finally {255delete(link);256}257}258259} finally {260delete(file);261}262}263264/**265* Exercise isReadbale, isWritable, isExecutable, exists, notExists266*/267static void testAccessMethods(Path tmpdir) throws IOException {268// should return false when file does not exist269Path doesNotExist = tmpdir.resolve("doesNotExist");270assertTrue(!isReadable(doesNotExist));271assertTrue(!isWritable(doesNotExist));272assertTrue(!isExecutable(doesNotExist));273assertTrue(!exists(doesNotExist));274assertTrue(notExists(doesNotExist));275276Path file = createFile(tmpdir.resolve("foo"));277try {278// files exist279assertTrue(isReadable(file));280assertTrue(isWritable(file));281assertTrue(exists(file));282assertTrue(!notExists(file));283assertTrue(isReadable(tmpdir));284assertTrue(isWritable(tmpdir));285assertTrue(exists(tmpdir));286assertTrue(!notExists(tmpdir));287288289// sym link exists290if (TestUtil.supportsLinks(tmpdir)) {291Path link = tmpdir.resolve("link");292293createSymbolicLink(link, file);294try {295assertTrue(isReadable(link));296assertTrue(isWritable(link));297assertTrue(exists(link));298assertTrue(!notExists(link));299} finally {300delete(link);301}302303createSymbolicLink(link, doesNotExist);304try {305assertTrue(!isReadable(link));306assertTrue(!isWritable(link));307assertTrue(!exists(link));308assertTrue(exists(link, NOFOLLOW_LINKS));309assertTrue(notExists(link));310assertTrue(!notExists(link, NOFOLLOW_LINKS));311} finally {312delete(link);313}314}315316/**317* Test: Edit ACL to deny WRITE and EXECUTE318*/319if (getFileStore(file).supportsFileAttributeView("acl")) {320AclFileAttributeView view =321getFileAttributeView(file, AclFileAttributeView.class);322UserPrincipal owner = view.getOwner();323List<AclEntry> acl = view.getAcl();324325// Insert entry to deny WRITE and EXECUTE326AclEntry entry = AclEntry.newBuilder()327.setType(AclEntryType.DENY)328.setPrincipal(owner)329.setPermissions(AclEntryPermission.WRITE_DATA,330AclEntryPermission.EXECUTE)331.build();332acl.add(0, entry);333view.setAcl(acl);334try {335if (isRoot()) {336// root has all permissions337assertTrue(isWritable(file));338assertTrue(isExecutable(file));339} else {340assertTrue(!isWritable(file));341assertTrue(!isExecutable(file));342}343} finally {344// Restore ACL345acl.remove(0);346view.setAcl(acl);347}348}349350/**351* Test: Windows DOS read-only attribute352*/353if (System.getProperty("os.name").startsWith("Windows")) {354setAttribute(file, "dos:readonly", true);355try {356assertTrue(!isWritable(file));357} finally {358setAttribute(file, "dos:readonly", false);359}360361// Read-only attribute does not make direcory read-only362DosFileAttributeView view =363getFileAttributeView(tmpdir, DosFileAttributeView.class);364boolean save = view.readAttributes().isReadOnly();365view.setReadOnly(true);366try {367assertTrue(isWritable(file));368} finally {369view.setReadOnly(save);370}371}372} finally {373delete(file);374}375}376377static void assertTrue(boolean okay) {378if (!okay)379throw new RuntimeException("Assertion Failed");380}381382private static boolean isRoot() {383if (System.getProperty("os.name").startsWith("Windows"))384return false;385386Path passwd = Paths.get("/etc/passwd");387return Files.isWritable(passwd);388}389}390391392