Path: blob/master/test/jdk/java/nio/file/Files/walkFileTree/WalkWithSecurity.java
41155 views
/*1* Copyright (c) 2010, 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 687654125* @summary Test Files.walkFileTree in the presence of a security manager26* @build WalkWithSecurity27* @run main/othervm -Djava.security.manager=allow WalkWithSecurity grantAll.policy pass28* @run main/othervm -Djava.security.manager=allow WalkWithSecurity denyAll.policy fail29* @run main/othervm -Djava.security.manager=allow WalkWithSecurity grantTopOnly.policy top_only30*/3132import java.nio.file.*;33import java.nio.file.attribute.BasicFileAttributes;34import java.io.IOException;3536public class WalkWithSecurity {3738public static void main(String[] args) throws IOException {39String policyFile = args[0];40ExpectedResult expectedResult = ExpectedResult.valueOf(args[1].toUpperCase());4142String here = System.getProperty("user.dir");43String testSrc = System.getProperty("test.src");44if (testSrc == null)45throw new RuntimeException("This test must be run by jtreg");46Path dir = Paths.get(testSrc);4748// Sanity check the environment49if (Files.isSameFile(Paths.get(here), dir))50throw new RuntimeException("Working directory cannot be " + dir);51try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) {52if (!stream.iterator().hasNext())53throw new RuntimeException(testSrc + " is empty");54}5556// Install security manager with the given policy file57System.setProperty("java.security.policy",58dir.resolve(policyFile).toString());59System.setSecurityManager(new SecurityManager());6061// Walk the source tree62CountingVisitor visitor = new CountingVisitor();63SecurityException exception = null;64try {65Files.walkFileTree(dir, visitor);66} catch (SecurityException se) {67exception = se;68}6970// Check result71switch (expectedResult) {72case PASS:73if (exception != null) {74exception.printStackTrace();75throw new RuntimeException("SecurityException not expected");76}77if (visitor.count() == 0)78throw new RuntimeException("No files visited");79break;80case FAIL:81if (exception == null)82throw new RuntimeException("SecurityException expected");83if (visitor.count() > 0)84throw new RuntimeException("Files were visited");85break;86case TOP_ONLY:87if (exception != null) {88exception.printStackTrace();89throw new RuntimeException("SecurityException not expected");90}91if (visitor.count() == 0)92throw new RuntimeException("Starting file not visited");93if (visitor.count() > 1)94throw new RuntimeException("More than starting file visited");95break;96default:97throw new RuntimeException("Should not get here");98}99}100101static enum ExpectedResult {102PASS,103FAIL,104TOP_ONLY;105}106107static class CountingVisitor extends SimpleFileVisitor<Path> {108private int count;109110int count() {111return count;112}113114@Override115public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {116System.out.println(dir);117count++;118return FileVisitResult.CONTINUE;119}120121@Override122public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {123System.out.println(file);124count++;125return FileVisitResult.CONTINUE;126}127}128}129130131