author | amlu |
Mon, 05 Feb 2018 11:12:09 +0800 | |
changeset 48742 | 364944ba4e2f |
permissions | -rw-r--r-- |
48742
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
1 |
/* |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
2 |
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
4 |
* |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
8 |
* |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
13 |
* accompanied this code). |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
14 |
* |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
18 |
* |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
21 |
* questions. |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
22 |
*/ |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
23 |
|
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
24 |
/* |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
25 |
* @test |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
26 |
* @bug 8055206 |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
27 |
* @summary Check that each module loaded by the platform loader has the |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
28 |
* proper "accessClassInPackage" RuntimePermissions to access its |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
29 |
* qualified exports. |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
30 |
* @run main CheckAccessClassInPackagePermissions |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
31 |
*/ |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
32 |
|
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
33 |
import java.lang.module.ModuleDescriptor; |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
34 |
import java.lang.module.ModuleDescriptor.Exports; |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
35 |
import java.net.URL; |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
36 |
import java.security.CodeSigner; |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
37 |
import java.security.CodeSource; |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
38 |
import java.security.Policy; |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
39 |
import java.security.ProtectionDomain; |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
40 |
import java.util.ArrayList; |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
41 |
import java.util.Arrays; |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
42 |
import java.util.HashMap; |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
43 |
import java.util.List; |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
44 |
import java.util.Map; |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
45 |
import java.util.Map.Entry; |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
46 |
import java.util.Optional; |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
47 |
import java.util.Set; |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
48 |
import java.util.stream.Collectors; |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
49 |
import java.util.stream.Stream; |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
50 |
|
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
51 |
public class CheckAccessClassInPackagePermissions { |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
52 |
|
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
53 |
private static final String[] deployModules = { |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
54 |
"jdk.javaws", "jdk.plugin", "jdk.plugin.server", "jdk.deploy" }; |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
55 |
|
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
56 |
public static void main(String[] args) throws Exception { |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
57 |
|
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
58 |
// Get the modules in the boot layer loaded by the boot or platform |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
59 |
// loader |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
60 |
ModuleLayer bootLayer = ModuleLayer.boot(); |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
61 |
Set<Module> modules = bootLayer.modules() |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
62 |
.stream() |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
63 |
.filter(CheckAccessClassInPackagePermissions::isBootOrPlatformMod) |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
64 |
.collect(Collectors.toSet()); |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
65 |
|
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
66 |
// Create map of target module's qualified export packages |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
67 |
Map<String, List<String>> map = new HashMap<>(); |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
68 |
Set<Exports> qualExports = |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
69 |
modules.stream() |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
70 |
.map(Module::getDescriptor) |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
71 |
.map(ModuleDescriptor::exports) |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
72 |
.flatMap(Set::stream) |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
73 |
.filter(Exports::isQualified) |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
74 |
.collect(Collectors.toSet()); |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
75 |
for (Exports e : qualExports) { |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
76 |
Set<String> targets = e.targets(); |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
77 |
for (String t : targets) { |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
78 |
map.compute(t, (k, ov) -> { |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
79 |
if (ov == null) { |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
80 |
List<String> v = new ArrayList<>(); |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
81 |
v.add(e.source()); |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
82 |
return v; |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
83 |
} else { |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
84 |
ov.add(e.source()); |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
85 |
return ov; |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
86 |
} |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
87 |
}); |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
88 |
} |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
89 |
} |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
90 |
|
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
91 |
// Check if each target module has the right permissions to access |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
92 |
// its qualified exports |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
93 |
Policy policy = Policy.getPolicy(); |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
94 |
List<String> deployMods = Arrays.asList(deployModules); |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
95 |
for (Map.Entry<String, List<String>> me : map.entrySet()) { |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
96 |
String moduleName = me.getKey(); |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
97 |
|
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
98 |
// skip deploy modules since they are granted permissions in |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
99 |
// deployment policy file |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
100 |
if (deployMods.contains(moduleName)) { |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
101 |
continue; |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
102 |
} |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
103 |
|
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
104 |
// is this a module loaded by the platform loader? |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
105 |
Optional<Module> module = bootLayer.findModule(moduleName); |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
106 |
if (!module.isPresent()) { |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
107 |
continue; |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
108 |
} |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
109 |
Module mod = module.get(); |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
110 |
if (mod.getClassLoader() != ClassLoader.getPlatformClassLoader()) { |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
111 |
continue; |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
112 |
} |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
113 |
|
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
114 |
// create ProtectionDomain simulating module |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
115 |
URL url = new URL("jrt:/" + moduleName); |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
116 |
CodeSource cs = new CodeSource(url, (CodeSigner[])null); |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
117 |
ProtectionDomain pd = new ProtectionDomain(cs, null, null, null); |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
118 |
|
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
119 |
List<String> pkgs = me.getValue(); |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
120 |
for (String p : pkgs) { |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
121 |
RuntimePermission rp = |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
122 |
new RuntimePermission("accessClassInPackage." + p); |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
123 |
if (!policy.implies(pd, rp)) { |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
124 |
throw new Exception("Module " + mod + " has not been " + |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
125 |
"granted " + rp); |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
126 |
} |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
127 |
} |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
128 |
} |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
129 |
} |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
130 |
|
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
131 |
/** |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
132 |
* Returns true if the module's loader is the boot or platform loader. |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
133 |
*/ |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
134 |
private static boolean isBootOrPlatformMod(Module m) { |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
135 |
return m.getClassLoader() == null || |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
136 |
m.getClassLoader() == ClassLoader.getPlatformClassLoader(); |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
137 |
} |
364944ba4e2f
8195981: Move some tests to OpenJDK for jdk_lang test group
amlu
parents:
diff
changeset
|
138 |
} |