jdk/test/java/nio/file/Files/WalkWithSecurity.java
author ohair
Tue, 28 Dec 2010 15:53:50 -0800
changeset 7668 d4a77089c587
parent 6695 0e91810597f9
permissions -rw-r--r--
6962318: Update copyright year Reviewed-by: xdono
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3847
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 6695
diff changeset
     2
 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
3847
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
     4
 *
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
     8
 *
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
    13
 * accompanied this code).
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
    14
 *
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3847
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3847
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3847
diff changeset
    21
 * questions.
3847
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
    22
 */
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
    23
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
    24
/* @test
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
    25
 * @bug 6876541
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
    26
 * @summary Test Files.walkFileTree in the presence of a security manager
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
    27
 * @build WalkWithSecurity
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
    28
 * @run main/othervm WalkWithSecurity grantAll.policy pass
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
    29
 * @run main/othervm WalkWithSecurity denyAll.policy fail
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
    30
 * @run main/othervm WalkWithSecurity grantTopOnly.policy top_only
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
    31
 */
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
    32
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
    33
import java.nio.file.*;
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
    34
import java.nio.file.attribute.BasicFileAttributes;
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
    35
import java.io.IOException;
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
    36
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
    37
public class WalkWithSecurity {
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
    38
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
    39
    public static void main(String[] args) throws IOException {
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
    40
        String policyFile = args[0];
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
    41
        ExpectedResult expectedResult = ExpectedResult.valueOf(args[1].toUpperCase());
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
    42
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
    43
        String here = System.getProperty("user.dir");
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
    44
        String testSrc = System.getProperty("test.src");
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
    45
        if (testSrc == null)
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
    46
            throw new RuntimeException("This test must be run by jtreg");
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
    47
        Path dir = Paths.get(testSrc);
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
    48
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
    49
        // Sanity check the environment
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
    50
        if (Paths.get(here).isSameFile(dir))
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
    51
            throw new RuntimeException("Working directory cannot be " + dir);
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
    52
        DirectoryStream<Path> stream = dir.newDirectoryStream();
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
    53
        try {
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
    54
            if (!stream.iterator().hasNext())
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
    55
                throw new RuntimeException(testSrc + " is empty");
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
    56
        } finally {
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
    57
            stream.close();
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
    58
        }
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
    59
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
    60
        // Install security manager with the given policy file
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
    61
        System.setProperty("java.security.policy",
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
    62
            dir.resolve(policyFile).toString());
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
    63
        System.setSecurityManager(new SecurityManager());
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
    64
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
    65
        // Walk the source tree
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
    66
        CountingVisitor visitor = new CountingVisitor();
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
    67
        SecurityException exception = null;
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
    68
        try {
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
    69
            Files.walkFileTree(dir, visitor);
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
    70
        } catch (SecurityException se) {
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
    71
            exception = se;
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
    72
        }
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
    73
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
    74
        // Check result
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
    75
        switch (expectedResult) {
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
    76
            case PASS:
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
    77
                if (exception != null) {
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
    78
                    exception.printStackTrace();
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
    79
                    throw new RuntimeException("SecurityException not expected");
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
    80
                }
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
    81
                if (visitor.count() == 0)
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
    82
                    throw new RuntimeException("No files visited");
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
    83
                break;
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
    84
            case FAIL:
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
    85
                if (exception == null)
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
    86
                    throw new RuntimeException("SecurityException expected");
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
    87
                if (visitor.count() > 0)
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
    88
                    throw new RuntimeException("Files were visited");
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
    89
                break;
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
    90
            case TOP_ONLY:
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
    91
                if (exception != null) {
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
    92
                    exception.printStackTrace();
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
    93
                    throw new RuntimeException("SecurityException not expected");
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
    94
                }
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
    95
                if (visitor.count() == 0)
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
    96
                    throw new RuntimeException("Starting file not visited");
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
    97
                if (visitor.count() > 1)
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
    98
                    throw new RuntimeException("More than starting file visited");
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
    99
                break;
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
   100
            default:
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
   101
                throw new RuntimeException("Should not get here");
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
   102
        }
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
   103
    }
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
   104
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
   105
    static enum ExpectedResult {
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
   106
        PASS,
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
   107
        FAIL,
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
   108
        TOP_ONLY;
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
   109
    }
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
   110
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
   111
    static class CountingVisitor extends SimpleFileVisitor<Path> {
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
   112
        private int count;
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
   113
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
   114
        int count() {
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
   115
            return count;
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
   116
        }
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
   117
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
   118
        @Override
6695
0e91810597f9 6907737: (file) FileVisitor and Files.walkFileTree issues
alanb
parents: 5506
diff changeset
   119
        public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
3847
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
   120
            System.out.println(dir);
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
   121
            count++;
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
   122
            return FileVisitResult.CONTINUE;
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
   123
        }
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
   124
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
   125
        @Override
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
   126
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
   127
            System.out.println(file);
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
   128
            count++;
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
   129
            return FileVisitResult.CONTINUE;
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
   130
        }
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
   131
    }
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff changeset
   132
}