44359
|
1 |
/*
|
|
2 |
* Copyright (c) 2017, 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 |
/**
|
|
25 |
* @test
|
|
26 |
* @run main/othervm ModuleFinderWithSecurityManager allow
|
|
27 |
* @run main/othervm ModuleFinderWithSecurityManager deny
|
|
28 |
* @summary Basic test for ModuleFinder.ofSystem() with security manager
|
|
29 |
*/
|
|
30 |
|
|
31 |
import java.lang.module.ModuleFinder;
|
|
32 |
import java.lang.module.ModuleReference;
|
|
33 |
import java.nio.file.Path;
|
|
34 |
import java.nio.file.Paths;
|
|
35 |
import java.util.Set;
|
|
36 |
|
|
37 |
public class ModuleFinderWithSecurityManager {
|
|
38 |
public static void main(String[] args) throws Exception {
|
|
39 |
boolean allow = args[0].equals("allow");
|
|
40 |
|
|
41 |
// set security policy to allow access
|
|
42 |
if (allow) {
|
|
43 |
String testSrc = System.getProperty("test.src");
|
|
44 |
if (testSrc == null)
|
|
45 |
testSrc = ".";
|
|
46 |
Path policyFile = Paths.get(testSrc, "java.policy");
|
|
47 |
System.setProperty("java.security.policy", policyFile.toString());
|
|
48 |
}
|
|
49 |
|
|
50 |
System.setSecurityManager(new SecurityManager());
|
|
51 |
|
|
52 |
ModuleFinder finder = null;
|
|
53 |
try {
|
|
54 |
finder = ModuleFinder.ofSystem();
|
|
55 |
if (!allow) throw new RuntimeException("SecurityException expected");
|
|
56 |
} catch (SecurityException e) {
|
|
57 |
if (allow) throw new RuntimeException("SecurityException not expected");
|
|
58 |
}
|
|
59 |
|
|
60 |
// no additional permissions should be required to locate modules
|
|
61 |
if (finder != null) {
|
|
62 |
ModuleReference base = finder.find("java.base").orElse(null);
|
|
63 |
if (base == null)
|
|
64 |
throw new RuntimeException("java.base not found");
|
|
65 |
Set<ModuleReference> allModules = finder.findAll();
|
|
66 |
if (!allModules.contains(base))
|
|
67 |
throw new RuntimeException("java.base not in all modules");
|
|
68 |
}
|
|
69 |
}
|
|
70 |
}
|