author | ohair |
Wed, 06 Apr 2011 22:06:11 -0700 | |
changeset 9035 | 1255eb81cc2f |
parent 8158 | 77d9c0f1c19f |
permissions | -rw-r--r-- |
3847
821abab95d38
6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff
changeset
|
1 |
/* |
9035
1255eb81cc2f
7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents:
8158
diff
changeset
|
2 |
* Copyright (c) 2010, 2011, 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 | 19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
20 |
* or visit www.oracle.com if you need additional information or have any |
|
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 |
8158 | 50 |
if (Files.isSameFile(Paths.get(here), dir)) |
3847
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); |
8158 | 52 |
try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) { |
3847
821abab95d38
6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff
changeset
|
53 |
if (!stream.iterator().hasNext()) |
821abab95d38
6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff
changeset
|
54 |
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
|
55 |
} |
821abab95d38
6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff
changeset
|
56 |
|
821abab95d38
6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff
changeset
|
57 |
// 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
|
58 |
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
|
59 |
dir.resolve(policyFile).toString()); |
821abab95d38
6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff
changeset
|
60 |
System.setSecurityManager(new SecurityManager()); |
821abab95d38
6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff
changeset
|
61 |
|
821abab95d38
6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff
changeset
|
62 |
// Walk the source tree |
821abab95d38
6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff
changeset
|
63 |
CountingVisitor visitor = new CountingVisitor(); |
821abab95d38
6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff
changeset
|
64 |
SecurityException exception = null; |
821abab95d38
6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff
changeset
|
65 |
try { |
821abab95d38
6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff
changeset
|
66 |
Files.walkFileTree(dir, visitor); |
821abab95d38
6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff
changeset
|
67 |
} catch (SecurityException se) { |
821abab95d38
6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff
changeset
|
68 |
exception = se; |
821abab95d38
6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff
changeset
|
69 |
} |
821abab95d38
6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff
changeset
|
70 |
|
821abab95d38
6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff
changeset
|
71 |
// Check result |
821abab95d38
6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff
changeset
|
72 |
switch (expectedResult) { |
821abab95d38
6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff
changeset
|
73 |
case PASS: |
821abab95d38
6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff
changeset
|
74 |
if (exception != null) { |
821abab95d38
6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff
changeset
|
75 |
exception.printStackTrace(); |
821abab95d38
6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff
changeset
|
76 |
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
|
77 |
} |
821abab95d38
6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff
changeset
|
78 |
if (visitor.count() == 0) |
821abab95d38
6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff
changeset
|
79 |
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
|
80 |
break; |
821abab95d38
6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff
changeset
|
81 |
case FAIL: |
821abab95d38
6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff
changeset
|
82 |
if (exception == null) |
821abab95d38
6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff
changeset
|
83 |
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
|
84 |
if (visitor.count() > 0) |
821abab95d38
6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff
changeset
|
85 |
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
|
86 |
break; |
821abab95d38
6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff
changeset
|
87 |
case TOP_ONLY: |
821abab95d38
6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff
changeset
|
88 |
if (exception != null) { |
821abab95d38
6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff
changeset
|
89 |
exception.printStackTrace(); |
821abab95d38
6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff
changeset
|
90 |
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
|
91 |
} |
821abab95d38
6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff
changeset
|
92 |
if (visitor.count() == 0) |
821abab95d38
6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff
changeset
|
93 |
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
|
94 |
if (visitor.count() > 1) |
821abab95d38
6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff
changeset
|
95 |
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
|
96 |
break; |
821abab95d38
6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff
changeset
|
97 |
default: |
821abab95d38
6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff
changeset
|
98 |
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
|
99 |
} |
821abab95d38
6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff
changeset
|
100 |
} |
821abab95d38
6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff
changeset
|
101 |
|
821abab95d38
6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff
changeset
|
102 |
static enum ExpectedResult { |
821abab95d38
6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff
changeset
|
103 |
PASS, |
821abab95d38
6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff
changeset
|
104 |
FAIL, |
821abab95d38
6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff
changeset
|
105 |
TOP_ONLY; |
821abab95d38
6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff
changeset
|
106 |
} |
821abab95d38
6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff
changeset
|
107 |
|
821abab95d38
6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff
changeset
|
108 |
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
|
109 |
private int count; |
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 |
int count() { |
821abab95d38
6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff
changeset
|
112 |
return 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 |
|
821abab95d38
6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff
changeset
|
115 |
@Override |
6695
0e91810597f9
6907737: (file) FileVisitor and Files.walkFileTree issues
alanb
parents:
5506
diff
changeset
|
116 |
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
|
117 |
System.out.println(dir); |
821abab95d38
6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff
changeset
|
118 |
count++; |
821abab95d38
6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff
changeset
|
119 |
return FileVisitResult.CONTINUE; |
821abab95d38
6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff
changeset
|
120 |
} |
821abab95d38
6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff
changeset
|
121 |
|
821abab95d38
6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff
changeset
|
122 |
@Override |
821abab95d38
6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff
changeset
|
123 |
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
|
124 |
System.out.println(file); |
821abab95d38
6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff
changeset
|
125 |
count++; |
821abab95d38
6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff
changeset
|
126 |
return FileVisitResult.CONTINUE; |
821abab95d38
6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff
changeset
|
127 |
} |
821abab95d38
6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff
changeset
|
128 |
} |
821abab95d38
6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents:
diff
changeset
|
129 |
} |