44545
|
1 |
/*
|
|
2 |
* Copyright (c) 2016, 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 |
* @modules jdk.compiler
|
|
27 |
* @summary Test java.lang.Module methods that specify permission checks
|
|
28 |
* @run main/othervm -Djava.security.policy=${test.src}/allow.policy WithSecurityManager allow
|
|
29 |
* @run main/othervm WithSecurityManager deny
|
|
30 |
*/
|
|
31 |
|
|
32 |
import java.io.IOException;
|
|
33 |
import java.io.InputStream;
|
|
34 |
import java.lang.module.Configuration;
|
|
35 |
import java.lang.module.ModuleFinder;
|
|
36 |
import java.lang.module.ModuleReference;
|
|
37 |
import java.util.Collections;
|
|
38 |
import java.util.Optional;
|
|
39 |
import java.util.Set;
|
|
40 |
|
|
41 |
/**
|
|
42 |
* Test java.lang.Module methods that specify permission checks.
|
|
43 |
*/
|
|
44 |
|
|
45 |
public class WithSecurityManager {
|
|
46 |
|
|
47 |
// a module that will be loaded into a child layer
|
|
48 |
static final String ANOTHER_MODULE = "jdk.compiler";
|
|
49 |
static final String ANOTHER_MODULE_RESOURCE = "com/sun/tools/javac/Main.class";
|
|
50 |
|
|
51 |
public static void main(String[] args) throws IOException {
|
|
52 |
boolean allow = args[0].equals("allow");
|
|
53 |
|
|
54 |
// base module, in the boot layer
|
|
55 |
Module base = Object.class.getModule();
|
|
56 |
|
|
57 |
// another module, in a child layer
|
|
58 |
Module other = loadModuleInChildLayer(ANOTHER_MODULE);
|
|
59 |
assertTrue(other.getLayer() != ModuleLayer.boot());
|
|
60 |
|
|
61 |
System.setSecurityManager(new SecurityManager());
|
|
62 |
|
|
63 |
test(base, "java/lang/Object.class", allow);
|
|
64 |
test(other, ANOTHER_MODULE_RESOURCE, allow);
|
|
65 |
}
|
|
66 |
|
|
67 |
/**
|
|
68 |
* Test the permission checks by invoking methods on the given module.
|
|
69 |
*
|
|
70 |
* If {@code allow} is {@code true} then the permission checks should succeed.
|
|
71 |
*/
|
|
72 |
static void test(Module m, String name, boolean allow) throws IOException {
|
|
73 |
|
|
74 |
// test Module::getClassLoader
|
|
75 |
System.out.format("Test getClassLoader on %s ...%n", m);
|
|
76 |
try {
|
|
77 |
ClassLoader cl = m.getClassLoader();
|
|
78 |
System.out.println(cl);
|
|
79 |
if (!allow)
|
|
80 |
assertTrue("getClassLoader should have failed", false);
|
|
81 |
} catch (SecurityException e) {
|
|
82 |
System.out.println(e + " thrown");
|
|
83 |
if (allow)
|
|
84 |
throw e;
|
|
85 |
}
|
|
86 |
|
|
87 |
// test Module::getResourceAsStream
|
|
88 |
System.out.format("Test getResourceAsStream(\"%s\") on %s ...%n", name, m);
|
|
89 |
try (InputStream in = m.getResourceAsStream(name)) {
|
|
90 |
System.out.println(in);
|
|
91 |
if (allow && (in == null))
|
|
92 |
assertTrue(name + " not found", false);
|
|
93 |
if (!allow && (in != null))
|
|
94 |
assertTrue(name + " should not be found", false);
|
|
95 |
}
|
|
96 |
|
|
97 |
}
|
|
98 |
|
|
99 |
/**
|
|
100 |
* Create a module layer that contains the given system module.
|
|
101 |
*/
|
|
102 |
static Module loadModuleInChildLayer(String mn) {
|
|
103 |
Optional<ModuleReference> omref = ModuleFinder.ofSystem().find(mn);
|
|
104 |
assertTrue("module " + mn + " not a system module", omref.isPresent());
|
|
105 |
|
|
106 |
// create a ModuleFinder that only finds this module
|
|
107 |
ModuleReference mref = omref.get();
|
|
108 |
ModuleFinder finder = new ModuleFinder() {
|
|
109 |
@Override
|
|
110 |
public Optional<ModuleReference> find(String name) {
|
|
111 |
if (name.equals(mn))
|
|
112 |
return Optional.of(mref);
|
|
113 |
else
|
|
114 |
return Optional.empty();
|
|
115 |
}
|
|
116 |
|
|
117 |
@Override
|
|
118 |
public Set<ModuleReference> findAll() {
|
|
119 |
return Collections.singleton(mref);
|
|
120 |
}
|
|
121 |
};
|
|
122 |
|
|
123 |
// create a child configuration and layer with this module
|
|
124 |
ModuleLayer bootLayer = ModuleLayer.boot();
|
|
125 |
Configuration cf = bootLayer
|
|
126 |
.configuration()
|
|
127 |
.resolve(finder, ModuleFinder.of(), Set.of(ANOTHER_MODULE));
|
|
128 |
ModuleLayer layer = bootLayer.defineModulesWithOneLoader(cf, null);
|
|
129 |
|
|
130 |
Optional<Module> om = layer.findModule(mn);
|
|
131 |
assertTrue("module " + mn + " not in child layer", om.isPresent());
|
|
132 |
return om.get();
|
|
133 |
}
|
|
134 |
|
|
135 |
static void assertTrue(String msg, boolean e) {
|
|
136 |
if (!e)
|
|
137 |
throw new RuntimeException(msg);
|
|
138 |
}
|
|
139 |
|
|
140 |
static void assertTrue(boolean e) {
|
|
141 |
if (!e)
|
|
142 |
throw new RuntimeException();
|
|
143 |
}
|
|
144 |
|
|
145 |
}
|