1658N/A/*
3909N/A * Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
1658N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1658N/A *
1658N/A * This code is free software; you can redistribute it and/or modify it
1658N/A * under the terms of the GNU General Public License version 2 only, as
1658N/A * published by the Free Software Foundation.
1658N/A *
1658N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1658N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1658N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1658N/A * version 2 for more details (a copy is included in the LICENSE file that
1658N/A * accompanied this code).
1658N/A *
1658N/A * You should have received a copy of the GNU General Public License version
1658N/A * 2 along with this work; if not, write to the Free Software Foundation,
1658N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1658N/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.
1658N/A */
1658N/A
1658N/A/* @test
1658N/A * @bug 6876541
1658N/A * @summary Test Files.walkFileTree in the presence of a security manager
1658N/A * @build WalkWithSecurity
1658N/A * @run main/othervm WalkWithSecurity grantAll.policy pass
1658N/A * @run main/othervm WalkWithSecurity denyAll.policy fail
1658N/A * @run main/othervm WalkWithSecurity grantTopOnly.policy top_only
1658N/A */
1658N/A
1658N/Aimport java.nio.file.*;
1658N/Aimport java.nio.file.attribute.BasicFileAttributes;
1658N/Aimport java.io.IOException;
1658N/A
1658N/Apublic class WalkWithSecurity {
1658N/A
1658N/A public static void main(String[] args) throws IOException {
1658N/A String policyFile = args[0];
1658N/A ExpectedResult expectedResult = ExpectedResult.valueOf(args[1].toUpperCase());
1658N/A
1658N/A String here = System.getProperty("user.dir");
1658N/A String testSrc = System.getProperty("test.src");
1658N/A if (testSrc == null)
1658N/A throw new RuntimeException("This test must be run by jtreg");
1658N/A Path dir = Paths.get(testSrc);
1658N/A
1658N/A // Sanity check the environment
3471N/A if (Files.isSameFile(Paths.get(here), dir))
1658N/A throw new RuntimeException("Working directory cannot be " + dir);
3471N/A try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) {
1658N/A if (!stream.iterator().hasNext())
1658N/A throw new RuntimeException(testSrc + " is empty");
1658N/A }
1658N/A
1658N/A // Install security manager with the given policy file
1658N/A System.setProperty("java.security.policy",
1658N/A dir.resolve(policyFile).toString());
1658N/A System.setSecurityManager(new SecurityManager());
1658N/A
1658N/A // Walk the source tree
1658N/A CountingVisitor visitor = new CountingVisitor();
1658N/A SecurityException exception = null;
1658N/A try {
1658N/A Files.walkFileTree(dir, visitor);
1658N/A } catch (SecurityException se) {
1658N/A exception = se;
1658N/A }
1658N/A
1658N/A // Check result
1658N/A switch (expectedResult) {
1658N/A case PASS:
1658N/A if (exception != null) {
1658N/A exception.printStackTrace();
1658N/A throw new RuntimeException("SecurityException not expected");
1658N/A }
1658N/A if (visitor.count() == 0)
1658N/A throw new RuntimeException("No files visited");
1658N/A break;
1658N/A case FAIL:
1658N/A if (exception == null)
1658N/A throw new RuntimeException("SecurityException expected");
1658N/A if (visitor.count() > 0)
1658N/A throw new RuntimeException("Files were visited");
1658N/A break;
1658N/A case TOP_ONLY:
1658N/A if (exception != null) {
1658N/A exception.printStackTrace();
1658N/A throw new RuntimeException("SecurityException not expected");
1658N/A }
1658N/A if (visitor.count() == 0)
1658N/A throw new RuntimeException("Starting file not visited");
1658N/A if (visitor.count() > 1)
1658N/A throw new RuntimeException("More than starting file visited");
1658N/A break;
1658N/A default:
1658N/A throw new RuntimeException("Should not get here");
1658N/A }
1658N/A }
1658N/A
1658N/A static enum ExpectedResult {
1658N/A PASS,
1658N/A FAIL,
1658N/A TOP_ONLY;
1658N/A }
1658N/A
1658N/A static class CountingVisitor extends SimpleFileVisitor<Path> {
1658N/A private int count;
1658N/A
1658N/A int count() {
1658N/A return count;
1658N/A }
1658N/A
1658N/A @Override
2826N/A public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
1658N/A System.out.println(dir);
1658N/A count++;
1658N/A return FileVisitResult.CONTINUE;
1658N/A }
1658N/A
1658N/A @Override
1658N/A public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
1658N/A System.out.println(file);
1658N/A count++;
1658N/A return FileVisitResult.CONTINUE;
1658N/A }
1658N/A }
1658N/A}