Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/nio/file/Files/walkFileTree/WalkWithSecurity.java
41155 views
1
/*
2
* Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
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.
22
*/
23
24
/* @test
25
* @bug 6876541
26
* @summary Test Files.walkFileTree in the presence of a security manager
27
* @build WalkWithSecurity
28
* @run main/othervm -Djava.security.manager=allow WalkWithSecurity grantAll.policy pass
29
* @run main/othervm -Djava.security.manager=allow WalkWithSecurity denyAll.policy fail
30
* @run main/othervm -Djava.security.manager=allow WalkWithSecurity grantTopOnly.policy top_only
31
*/
32
33
import java.nio.file.*;
34
import java.nio.file.attribute.BasicFileAttributes;
35
import java.io.IOException;
36
37
public class WalkWithSecurity {
38
39
public static void main(String[] args) throws IOException {
40
String policyFile = args[0];
41
ExpectedResult expectedResult = ExpectedResult.valueOf(args[1].toUpperCase());
42
43
String here = System.getProperty("user.dir");
44
String testSrc = System.getProperty("test.src");
45
if (testSrc == null)
46
throw new RuntimeException("This test must be run by jtreg");
47
Path dir = Paths.get(testSrc);
48
49
// Sanity check the environment
50
if (Files.isSameFile(Paths.get(here), dir))
51
throw new RuntimeException("Working directory cannot be " + dir);
52
try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) {
53
if (!stream.iterator().hasNext())
54
throw new RuntimeException(testSrc + " is empty");
55
}
56
57
// Install security manager with the given policy file
58
System.setProperty("java.security.policy",
59
dir.resolve(policyFile).toString());
60
System.setSecurityManager(new SecurityManager());
61
62
// Walk the source tree
63
CountingVisitor visitor = new CountingVisitor();
64
SecurityException exception = null;
65
try {
66
Files.walkFileTree(dir, visitor);
67
} catch (SecurityException se) {
68
exception = se;
69
}
70
71
// Check result
72
switch (expectedResult) {
73
case PASS:
74
if (exception != null) {
75
exception.printStackTrace();
76
throw new RuntimeException("SecurityException not expected");
77
}
78
if (visitor.count() == 0)
79
throw new RuntimeException("No files visited");
80
break;
81
case FAIL:
82
if (exception == null)
83
throw new RuntimeException("SecurityException expected");
84
if (visitor.count() > 0)
85
throw new RuntimeException("Files were visited");
86
break;
87
case TOP_ONLY:
88
if (exception != null) {
89
exception.printStackTrace();
90
throw new RuntimeException("SecurityException not expected");
91
}
92
if (visitor.count() == 0)
93
throw new RuntimeException("Starting file not visited");
94
if (visitor.count() > 1)
95
throw new RuntimeException("More than starting file visited");
96
break;
97
default:
98
throw new RuntimeException("Should not get here");
99
}
100
}
101
102
static enum ExpectedResult {
103
PASS,
104
FAIL,
105
TOP_ONLY;
106
}
107
108
static class CountingVisitor extends SimpleFileVisitor<Path> {
109
private int count;
110
111
int count() {
112
return count;
113
}
114
115
@Override
116
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
117
System.out.println(dir);
118
count++;
119
return FileVisitResult.CONTINUE;
120
}
121
122
@Override
123
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
124
System.out.println(file);
125
count++;
126
return FileVisitResult.CONTINUE;
127
}
128
}
129
}
130
131