DiffCL_UmodUpkg.java revision 12290:8953c0318163
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.  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 m1 tries to access c4 defined in an unnamed package
29 *          and an unnamed module.
30 * @modules java.base/jdk.internal.misc
31 * @library /test/lib
32 * @compile myloaders/MyDiffClassLoader.java
33 * @compile c4.java
34 * @compile p3/c3.jcod
35 * @compile p3/c3ReadEdgeDiffLoader.jcod
36 * @run main/othervm -Xbootclasspath/a:. DiffCL_UmodUpkg
37 */
38
39import static jdk.test.lib.Asserts.*;
40
41import java.lang.reflect.Layer;
42import java.lang.module.Configuration;
43import java.lang.module.ModuleDescriptor;
44import java.lang.module.ModuleFinder;
45import java.util.HashMap;
46import java.util.Map;
47import java.util.Set;
48import myloaders.MyDiffClassLoader;
49
50//
51// ClassLoader1 --> defines m1 --> packages p3
52//                  package p3 in m1 is exported unqualifiedly
53//
54// class p3.c3 defined in m1 tries to access c4 defined in
55// in unnamed module.
56//
57// Two access attempts occur in this test:
58//   1. The first access is not allowed because a strict module
59//      cannot read an unnamed module.
60//   2. In this scenario a strict module establishes readability
61//      to the particular unnamed module it is trying to access.
62//      Access is allowed.
63//
64public class DiffCL_UmodUpkg {
65
66 // Create Layers over the boot layer to test different
67 // accessing scenarios of a named module to an unnamed module.
68
69 // Module m1 is a strict module and has not established
70 // readability to an unnamed module that c4 is defined in.
71 public void test_strictModuleLayer() throws Throwable {
72
73     // Define module:     m1
74     // Can read:          java.base
75     // Packages:          p3
76     // Packages exported: p3 is exported unqualifiedly
77     ModuleDescriptor descriptor_m1 =
78             ModuleDescriptor.module("m1")
79                     .requires("java.base")
80                     .exports("p3")
81                     .build();
82
83     // Set up a ModuleFinder containing all modules for this layer.
84     ModuleFinder finder = ModuleLibrary.of(descriptor_m1);
85
86     // Resolves "m1"
87     Configuration cf = Layer.boot()
88             .configuration()
89             .resolveRequires(finder, ModuleFinder.of(), Set.of("m1"));
90
91     MyDiffClassLoader.loader1 = new MyDiffClassLoader();
92     MyDiffClassLoader.loader2 = new MyDiffClassLoader();
93
94     // map module m1 to class loader.
95     // class c2 will be loaded in an unnamed module/loader2
96     // to achieve differing class loaders.
97     Map<String, ClassLoader> map = new HashMap<>();
98     map.put("m1", MyDiffClassLoader.loader1);
99
100     // Create Layer that contains m1
101     Layer layer = Layer.boot().defineModules(cf, map::get);
102
103     assertTrue(layer.findLoader("m1") == MyDiffClassLoader.loader1);
104     assertTrue(layer.findLoader("java.base") == null);
105
106     // now use the same loader to load class p3.c3
107     Class p3_c3_class = MyDiffClassLoader.loader1.loadClass("p3.c3");
108
109     // Attempt access
110     try {
111         p3_c3_class.newInstance();
112         throw new RuntimeException("Test Failed, strict module m1 should not be able to access " +
113                                    "public type c4 defined in unnamed module");
114     } catch (IllegalAccessError e) {
115     }
116}
117
118 // Module m1 is a strict module and has established
119 // readability to an unnamed module that c4 is defined in.
120 public void test_strictModuleUnnamedReadableLayer() throws Throwable {
121
122     // Define module:     m1
123     // Can read:          java.base
124     // Packages:          p3
125     // Packages exported: p3 is exported unqualifiedly
126     ModuleDescriptor descriptor_m1 =
127             ModuleDescriptor.module("m1")
128                     .requires("java.base")
129                     .exports("p3")
130                     .build();
131
132     // Set up a ModuleFinder containing all modules for this layer.
133     ModuleFinder finder = ModuleLibrary.of(descriptor_m1);
134
135     // Resolves "m1"
136     Configuration cf = Layer.boot()
137             .configuration()
138             .resolveRequires(finder, ModuleFinder.of(), Set.of("m1"));
139
140     MyDiffClassLoader.loader1 = new MyDiffClassLoader();
141     MyDiffClassLoader.loader2 = new MyDiffClassLoader();
142
143     // map module m1 to class loader.
144     // class c2 will be loaded in an unnamed module/loader2
145     // to achieve differing class loaders.
146     Map<String, ClassLoader> map = new HashMap<>();
147     map.put("m1", MyDiffClassLoader.loader1);
148
149     // Create Layer that contains m1
150     Layer layer = Layer.boot().defineModules(cf, map::get);
151
152     assertTrue(layer.findLoader("m1") == MyDiffClassLoader.loader1);
153     assertTrue(layer.findLoader("java.base") == null);
154
155     // now use the same loader to load class p3.c3ReadEdgeDiffLoader
156     Class p3_c3_class = MyDiffClassLoader.loader1.loadClass("p3.c3ReadEdgeDiffLoader");
157
158     try {
159        // Read edge between m1 and the unnamed module that loads c4 is established in
160        // C3ReadEdgeDiffLoader's ctor before attempting access.
161        p3_c3_class.newInstance();
162     } catch (IllegalAccessError e) {
163         throw new RuntimeException("Test Failed, module m1 has established readability to " +
164                                    "c4 loader's unnamed module, access should be allowed: " + e.getMessage());
165     }
166 }
167
168 public static void main(String args[]) throws Throwable {
169   DiffCL_UmodUpkg test = new DiffCL_UmodUpkg();
170   test.test_strictModuleLayer();                // access denied
171   test.test_strictModuleUnnamedReadableLayer(); // access allowed
172 }
173}
174