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