UmodUPkg.java revision 12723:afedee84773e
1/*
2 * Copyright (c) 2016, 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.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26/*
27 * @test
28 * @summary class p3.c3 defined in module m1x tries to access c4 defined in unnamed module.
29 * @modules java.base/jdk.internal.misc
30 * @library /test/lib
31 * @compile myloaders/MySameClassLoader.java
32 * @compile c4.java
33 * @compile p3/c3.jcod
34 * @compile p3/c3ReadEdge.jcod
35 * @run main/othervm -Xbootclasspath/a:. UmodUPkg
36 */
37
38import static jdk.test.lib.Asserts.*;
39
40import java.lang.module.Configuration;
41import java.lang.module.ModuleDescriptor;
42import java.lang.module.ModuleFinder;
43import java.util.HashMap;
44import java.util.Map;
45import java.util.Set;
46import myloaders.MySameClassLoader;
47
48//
49// ClassLoader1 --> defines m1x --> packages p3
50//                  package p3 in m1x is exported unqualifiedly
51//
52// class p3.c3 defined in m1x tries to access c4 defined in
53// in unnamed module.
54//
55// Two access attempts occur in this test:
56//   1. The first access is not allowed because a strict module
57//      cannot read an unnamed module.
58//   2. In this scenario a strict module establishes readability
59//      to the particular unnamed module it is trying to access.
60//      Access is allowed.
61//
62public class UmodUPkg {
63
64 // Create layers over the boot layer to test different
65 // accessing scenarios of a named module to an unnamed module.
66
67 // Module m1x is a strict module and has not established
68 // readability to an unnamed module that c4 is defined in.
69 public void test_strictModuleLayer() throws Throwable {
70
71     // Define module:     m1x
72     // Can read:          java.base
73     // Packages:          p3
74     // Packages exported: p3 is exported unqualifiedly
75     ModuleDescriptor descriptor_m1x =
76             ModuleDescriptor.newModule("m1x")
77                     .requires("java.base")
78                     .exports("p3")
79                     .build();
80
81     // Set up a ModuleFinder containing all modules for this layer.
82     ModuleFinder finder = ModuleLibrary.of(descriptor_m1x);
83
84     // Resolves "m1x"
85     Configuration cf = ModuleLayer.boot()
86             .configuration()
87             .resolve(finder, ModuleFinder.of(), Set.of("m1x"));
88
89     // map module m1x to class loader.
90     // class c4 will be loaded in an unnamed module/loader.
91     MySameClassLoader loader = new MySameClassLoader();
92     Map<String, ClassLoader> map = new HashMap<>();
93     map.put("m1x", loader);
94
95     // Create layer that contains m1x
96     ModuleLayer layer = ModuleLayer.boot().defineModules(cf, map::get);
97
98     assertTrue(layer.findLoader("m1x") == loader);
99     assertTrue(layer.findLoader("java.base") == null);
100
101     // now use the same loader to load class p3.c3
102     Class p3_c3_class = loader.loadClass("p3.c3");
103
104     // Attempt access
105     try {
106         p3_c3_class.newInstance();
107         throw new RuntimeException("Test Failed, strict module m1x, type p3.c3, should not be able to access " +
108                                    "public type c4 defined in unnamed module");
109     } catch (IllegalAccessError e) {
110     }
111 }
112
113 // Module m1x is a strict module and has established
114 // readability to an unnamed module that c4 is defined in.
115 public void test_strictModuleUnnamedReadableLayer() throws Throwable {
116
117     // Define module:     m1x
118     // Can read:          java.base
119     // Packages:          p3
120     // Packages exported: p3 is exported unqualifiedly
121     ModuleDescriptor descriptor_m1x =
122             ModuleDescriptor.newModule("m1x")
123                     .requires("java.base")
124                     .exports("p3")
125                     .build();
126
127     // Set up a ModuleFinder containing all modules for this layer.
128     ModuleFinder finder = ModuleLibrary.of(descriptor_m1x);
129
130     // Resolves "m1x"
131     Configuration cf = ModuleLayer.boot()
132             .configuration()
133             .resolve(finder, ModuleFinder.of(), Set.of("m1x"));
134
135     MySameClassLoader loader = new MySameClassLoader();
136     // map module m1x to class loader.
137     // class c4 will be loaded in an unnamed module/loader.
138     Map<String, ClassLoader> map = new HashMap<>();
139     map.put("m1x", loader);
140
141     // Create layer that contains m1x
142     ModuleLayer layer = ModuleLayer.boot().defineModules(cf, map::get);
143
144     assertTrue(layer.findLoader("m1x") == loader);
145     assertTrue(layer.findLoader("java.base") == null);
146
147     // now use the same loader to load class p3.c3ReadEdge
148     Class p3_c3_class = loader.loadClass("p3.c3ReadEdge");
149
150     try {
151        // Read edge between m1x and the unnamed module that loads c4 is established in
152        // c3ReadEdge's ctor before attempting access.
153        p3_c3_class.newInstance();
154     } catch (IllegalAccessError e) {
155         throw new RuntimeException("Test Failed, module m1x, type p3.c3ReadEdge, has established readability to " +
156                                    "c4 loader's unnamed module, access should be allowed: " + e.getMessage());
157     }
158 }
159
160 public static void main(String args[]) throws Throwable {
161   UmodUPkg test = new UmodUPkg();
162   test.test_strictModuleLayer();                // access denied
163   test.test_strictModuleUnnamedReadableLayer(); // access allowed
164 }
165}
166