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