hotspot/test/runtime/modules/AccessCheck/DiffCL_Umod.java
changeset 36508 5f9eee6b383b
child 38152 80e5da81fb2c
child 37773 e5b3e9732c3c
equal deleted inserted replaced
36507:c80f6ecb0bb3 36508:5f9eee6b383b
       
     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 p1.c1 defined in m1 tries to access p2.c2 defined in unnamed module.
       
    29  * @library /testlibrary /test/lib
       
    30  * @modules java.base/jdk.internal.module
       
    31  * @compile myloaders/MyDiffClassLoader.java
       
    32  * @compile p2/c2.java
       
    33  * @compile p1/c1.java
       
    34  * @compile p1/c1ReadEdgeDiffLoader.java
       
    35  * @compile p1/c1Loose.java
       
    36  * @build DiffCL_Umod
       
    37  * @run main/othervm -Xbootclasspath/a:. DiffCL_Umod
       
    38  */
       
    39 
       
    40 import static jdk.test.lib.Asserts.*;
       
    41 
       
    42 import java.lang.module.Configuration;
       
    43 import java.lang.module.ModuleDescriptor;
       
    44 import java.lang.module.ModuleFinder;
       
    45 import java.lang.reflect.Layer;
       
    46 import java.lang.reflect.Module;
       
    47 import java.util.HashMap;
       
    48 import java.util.Map;
       
    49 import java.util.Set;
       
    50 import myloaders.MyDiffClassLoader;
       
    51 
       
    52 //
       
    53 // ClassLoader1 --> defines m1 --> packages p1
       
    54 //                  package p1 in m1 is exported unqualifiedly
       
    55 //
       
    56 // class p1.c1 defined in m1 tries to access p2.c2 defined in
       
    57 // in unnamed module.
       
    58 //
       
    59 // Three access attempts occur in this test:
       
    60 //   1. The first access is not allowed because a strict module
       
    61 //      cannot read an unnamed module.
       
    62 //   2. In this scenario a strict module establishes readability
       
    63 //      to the particular unnamed module it is trying to access.
       
    64 //      Access is allowed.
       
    65 //   3. Module m1 in the test_looseModuleLayer() method
       
    66 //      is transitioned to a loose module, access
       
    67 //      to all unnamed modules is allowed.
       
    68 //
       
    69 public class DiffCL_Umod {
       
    70 
       
    71  // Create Layers over the boot layer to test different
       
    72  // accessing scenarios of a named module to an unnamed module.
       
    73 
       
    74  // Module m1 is a strict module and has not established
       
    75  // readability to an unnamed module that p2.c2 is defined in.
       
    76  public void test_strictModuleLayer() throws Throwable {
       
    77 
       
    78      // Define module:     m1
       
    79      // Can read:          java.base
       
    80      // Packages:          p1
       
    81      // Packages exported: p1 is exported unqualifiedly
       
    82      ModuleDescriptor descriptor_m1 =
       
    83              new ModuleDescriptor.Builder("m1")
       
    84                      .requires("java.base")
       
    85                      .exports("p1")
       
    86                      .build();
       
    87 
       
    88      // Set up a ModuleFinder containing all modules for this layer.
       
    89      ModuleFinder finder = ModuleLibrary.of(descriptor_m1);
       
    90 
       
    91      // Resolves "m1"
       
    92      Configuration cf = Layer.boot()
       
    93              .configuration()
       
    94              .resolveRequires(finder, ModuleFinder.empty(), Set.of("m1"));
       
    95 
       
    96      MyDiffClassLoader.loader1 = new MyDiffClassLoader();
       
    97      MyDiffClassLoader.loader2 = new MyDiffClassLoader();
       
    98 
       
    99      // map module m1 to class loader.
       
   100      // class c2 will be loaded in an unnamed module/loader2
       
   101      // to achieve differing class loaders.
       
   102      Map<String, ClassLoader> map = new HashMap<>();
       
   103      map.put("m1", MyDiffClassLoader.loader1);
       
   104 
       
   105      // Create Layer that contains m1
       
   106      Layer layer = Layer.boot().defineModules(cf, map::get);
       
   107 
       
   108      assertTrue(layer.findLoader("m1") == MyDiffClassLoader.loader1);
       
   109      assertTrue(layer.findLoader("java.base") == null);
       
   110 
       
   111      // now use the same loader to load class p1.c1
       
   112      Class p1_c1_class = MyDiffClassLoader.loader1.loadClass("p1.c1");
       
   113 
       
   114      // Attempt access
       
   115      try {
       
   116          p1_c1_class.newInstance();
       
   117          throw new RuntimeException("Test Failed, strict module m1 should not be able " +
       
   118                                     "to access public type p2.c2 defined in unnamed module");
       
   119      } catch (IllegalAccessError e) {
       
   120      }
       
   121 }
       
   122 
       
   123  // Module m1 is a strict module and has established
       
   124  // readability to an unnamed module that p2.c2 is defined in.
       
   125  public void test_strictModuleUnnamedReadableLayer() throws Throwable {
       
   126 
       
   127      // Define module:     m1
       
   128      // Can read:          java.base
       
   129      // Packages:          p1
       
   130      // Packages exported: p1 is exported unqualifiedly
       
   131      ModuleDescriptor descriptor_m1 =
       
   132              new ModuleDescriptor.Builder("m1")
       
   133                      .requires("java.base")
       
   134                      .exports("p1")
       
   135                      .build();
       
   136 
       
   137      // Set up a ModuleFinder containing all modules for this layer.
       
   138      ModuleFinder finder = ModuleLibrary.of(descriptor_m1);
       
   139 
       
   140      // Resolves "m1"
       
   141      Configuration cf = Layer.boot()
       
   142              .configuration()
       
   143              .resolveRequires(finder, ModuleFinder.empty(), Set.of("m1"));
       
   144 
       
   145      MyDiffClassLoader.loader1 = new MyDiffClassLoader();
       
   146      MyDiffClassLoader.loader2 = new MyDiffClassLoader();
       
   147 
       
   148      // map module m1 to class loader.
       
   149      // class c2 will be loaded in an unnamed module/loader2
       
   150      // to achieve differing class loaders.
       
   151      Map<String, ClassLoader> map = new HashMap<>();
       
   152      map.put("m1", MyDiffClassLoader.loader1);
       
   153 
       
   154      // Create Layer that contains m1
       
   155      Layer layer = Layer.boot().defineModules(cf, map::get);
       
   156 
       
   157      assertTrue(layer.findLoader("m1") == MyDiffClassLoader.loader1);
       
   158      assertTrue(layer.findLoader("java.base") == null);
       
   159 
       
   160      // now use the same loader to load class p1.c1ReadEdgeDiffLoader
       
   161      Class p1_c1_class = MyDiffClassLoader.loader1.loadClass("p1.c1ReadEdgeDiffLoader");
       
   162 
       
   163      try {
       
   164         // Read edge between m1 and the unnamed module that loads p2.c2 is established in
       
   165         // c1ReadEdgeDiffLoader's ctor before attempting access.
       
   166         p1_c1_class.newInstance();
       
   167      } catch (IllegalAccessError e) {
       
   168          throw new RuntimeException("Test Failed, module m1 has established readability to p2/c2 loader's " +
       
   169                                     "unnamed module, access should be allowed: " + e.getMessage());
       
   170      }
       
   171  }
       
   172 
       
   173  // Module m1 is a loose module and thus can read all unnamed modules.
       
   174  public void test_looseModuleLayer() throws Throwable {
       
   175 
       
   176      // Define module:     m1
       
   177      // Can read:          java.base
       
   178      // Packages:          p1
       
   179      // Packages exported: p1 is exported unqualifiedly
       
   180      ModuleDescriptor descriptor_m1 =
       
   181              new ModuleDescriptor.Builder("m1")
       
   182                      .requires("java.base")
       
   183                      .exports("p1")
       
   184                      .build();
       
   185 
       
   186      // Set up a ModuleFinder containing all modules for this layer.
       
   187      ModuleFinder finder = ModuleLibrary.of(descriptor_m1);
       
   188 
       
   189      // Resolves "m1"
       
   190      Configuration cf = Layer.boot()
       
   191              .configuration()
       
   192              .resolveRequires(finder, ModuleFinder.empty(), Set.of("m1"));
       
   193 
       
   194      MyDiffClassLoader.loader1 = new MyDiffClassLoader();
       
   195      MyDiffClassLoader.loader2 = new MyDiffClassLoader();
       
   196 
       
   197      // map module m1 to class loader.
       
   198      // class c2 will be loaded in an unnamed module/loader2
       
   199      // to achieve differing class loaders.
       
   200      Map<String, ClassLoader> map = new HashMap<>();
       
   201      map.put("m1", MyDiffClassLoader.loader1);
       
   202 
       
   203      // Create Layer that contains m1
       
   204      Layer layer = Layer.boot().defineModules(cf, map::get);
       
   205 
       
   206      assertTrue(layer.findLoader("m1") == MyDiffClassLoader.loader1);
       
   207      assertTrue(layer.findLoader("java.base") == null);
       
   208 
       
   209      // now use the same loader to load class p1.c1Loose
       
   210      Class p1_c1_class = MyDiffClassLoader.loader1.loadClass("p1.c1Loose");
       
   211 
       
   212      // change m1 to be a loose module
       
   213      Module m1 = layer.findModule("m1").get();
       
   214      jdk.internal.module.Modules.addReads(m1, null);
       
   215 
       
   216      try {
       
   217          p1_c1_class.newInstance();
       
   218      } catch (IllegalAccessError e) {
       
   219          throw new RuntimeException("Test Failed, loose module m1 should be able to access " +
       
   220                                     "public type p2.c2 defined in unnamed module: " + e.getMessage());
       
   221      }
       
   222  }
       
   223 
       
   224  public static void main(String args[]) throws Throwable {
       
   225    DiffCL_Umod test = new DiffCL_Umod();
       
   226    test.test_strictModuleLayer();                // access denied
       
   227    test.test_strictModuleUnnamedReadableLayer(); // access allowed
       
   228    test.test_looseModuleLayer();                 // access allowed
       
   229  }
       
   230 }