Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/nio/file/WatchService/WithSecurityManager.java
38828 views
1
/*
2
* Copyright (c) 2008, 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 4313887
26
* @summary Unit test for Watchable#register's permission checks
27
* @build WithSecurityManager
28
* @run main/othervm WithSecurityManager denyAll.policy - fail
29
* @run main/othervm WithSecurityManager denyAll.policy tree fail
30
* @run main/othervm WithSecurityManager grantDirOnly.policy - pass
31
* @run main/othervm WithSecurityManager grantDirOnly.policy tree fail
32
* @run main/othervm WithSecurityManager grantDirAndOneLevel.policy - pass
33
* @run main/othervm WithSecurityManager grantDirAndOneLevel.policy tree fail
34
* @run main/othervm WithSecurityManager grantDirAndTree.policy - pass
35
* @run main/othervm WithSecurityManager grantDirAndTree.policy tree pass
36
*/
37
38
import java.nio.file.*;
39
import java.io.IOException;
40
import com.sun.nio.file.ExtendedWatchEventModifier;
41
42
public class WithSecurityManager {
43
44
public static void main(String[] args) throws IOException {
45
String policyFile = args[0];
46
boolean recursive = args[1].equals("tree");
47
boolean expectedToFail = args[2].equals("fail");
48
49
// install security manager with the given policy file
50
String testSrc = System.getProperty("test.src");
51
if (testSrc == null)
52
throw new RuntimeException("This test must be run by jtreg");
53
Path dir = Paths.get(testSrc);
54
System.setProperty("java.security.policy", dir.resolve(policyFile).toString());
55
System.setSecurityManager(new SecurityManager());
56
57
// initialize optional modifier
58
WatchEvent.Modifier[] modifiers;
59
if (recursive) {
60
modifiers = new WatchEvent.Modifier[1];
61
modifiers[0] = ExtendedWatchEventModifier.FILE_TREE;
62
} else {
63
modifiers = new WatchEvent.Modifier[0];
64
}
65
66
// attempt to register directory
67
try {
68
dir.register(dir.getFileSystem().newWatchService(),
69
new WatchEvent.Kind<?>[]{ StandardWatchEventKinds.ENTRY_CREATE },
70
modifiers);
71
if (expectedToFail)
72
throw new RuntimeException("SecurityException not thrown");
73
} catch (SecurityException e) {
74
if (!expectedToFail)
75
throw e;
76
} catch (UnsupportedOperationException e) {
77
// FILE_TREE modifier only supported on some platforms
78
if (!recursive)
79
throw new RuntimeException(e);
80
System.out.println("FILE_TREE option not supported");
81
}
82
}
83
}
84
85