hotspot/test/runtime/modules/AccessCheck/UmodUPkg.java
changeset 43665 4bb003cad9b9
parent 42307 cefc81dc1d52
child 44520 0553e129e0ec
equal deleted inserted replaced
43606:a5aa7536131c 43665:4bb003cad9b9
     1 /*
     1 /*
     2  * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
     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.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    23  * questions.
    23  * questions.
    24  */
    24  */
    25 
    25 
    26 /*
    26 /*
    27  * @test
    27  * @test
    28  * @summary class p3.c3 defined in module m1 tries to access c4 defined in unnamed module.
    28  * @summary class p3.c3 defined in module m1x tries to access c4 defined in unnamed module.
    29  * @modules java.base/jdk.internal.misc
    29  * @modules java.base/jdk.internal.misc
    30  * @library /test/lib
    30  * @library /test/lib
    31  * @compile myloaders/MySameClassLoader.java
    31  * @compile myloaders/MySameClassLoader.java
    32  * @compile c4.java
    32  * @compile c4.java
    33  * @compile p3/c3.jcod
    33  * @compile p3/c3.jcod
    46 import java.util.Map;
    46 import java.util.Map;
    47 import java.util.Set;
    47 import java.util.Set;
    48 import myloaders.MySameClassLoader;
    48 import myloaders.MySameClassLoader;
    49 
    49 
    50 //
    50 //
    51 // ClassLoader1 --> defines m1 --> packages p3
    51 // ClassLoader1 --> defines m1x --> packages p3
    52 //                  package p3 in m1 is exported unqualifiedly
    52 //                  package p3 in m1x is exported unqualifiedly
    53 //
    53 //
    54 // class p3.c3 defined in m1 tries to access c4 defined in
    54 // class p3.c3 defined in m1x tries to access c4 defined in
    55 // in unnamed module.
    55 // in unnamed module.
    56 //
    56 //
    57 // Two access attempts occur in this test:
    57 // Two access attempts occur in this test:
    58 //   1. The first access is not allowed because a strict module
    58 //   1. The first access is not allowed because a strict module
    59 //      cannot read an unnamed module.
    59 //      cannot read an unnamed module.
    64 public class UmodUPkg {
    64 public class UmodUPkg {
    65 
    65 
    66  // Create Layers over the boot layer to test different
    66  // Create Layers over the boot layer to test different
    67  // accessing scenarios of a named module to an unnamed module.
    67  // accessing scenarios of a named module to an unnamed module.
    68 
    68 
    69  // Module m1 is a strict module and has not established
    69  // Module m1x is a strict module and has not established
    70  // readability to an unnamed module that c4 is defined in.
    70  // readability to an unnamed module that c4 is defined in.
    71  public void test_strictModuleLayer() throws Throwable {
    71  public void test_strictModuleLayer() throws Throwable {
    72 
    72 
    73      // Define module:     m1
    73      // Define module:     m1x
    74      // Can read:          java.base
    74      // Can read:          java.base
    75      // Packages:          p3
    75      // Packages:          p3
    76      // Packages exported: p3 is exported unqualifiedly
    76      // Packages exported: p3 is exported unqualifiedly
    77      ModuleDescriptor descriptor_m1 =
    77      ModuleDescriptor descriptor_m1x =
    78              ModuleDescriptor.module("m1")
    78              ModuleDescriptor.newModule("m1x")
    79                      .requires("java.base")
    79                      .requires("java.base")
    80                      .exports("p3")
    80                      .exports("p3")
    81                      .build();
    81                      .build();
    82 
    82 
    83      // Set up a ModuleFinder containing all modules for this layer.
    83      // Set up a ModuleFinder containing all modules for this layer.
    84      ModuleFinder finder = ModuleLibrary.of(descriptor_m1);
    84      ModuleFinder finder = ModuleLibrary.of(descriptor_m1x);
    85 
    85 
    86      // Resolves "m1"
    86      // Resolves "m1x"
    87      Configuration cf = Layer.boot()
    87      Configuration cf = Layer.boot()
    88              .configuration()
    88              .configuration()
    89              .resolveRequires(finder, ModuleFinder.of(), Set.of("m1"));
    89              .resolve(finder, ModuleFinder.of(), Set.of("m1x"));
    90 
    90 
    91      // map module m1 to class loader.
    91      // map module m1x to class loader.
    92      // class c4 will be loaded in an unnamed module/loader.
    92      // class c4 will be loaded in an unnamed module/loader.
    93      MySameClassLoader loader = new MySameClassLoader();
    93      MySameClassLoader loader = new MySameClassLoader();
    94      Map<String, ClassLoader> map = new HashMap<>();
    94      Map<String, ClassLoader> map = new HashMap<>();
    95      map.put("m1", loader);
    95      map.put("m1x", loader);
    96 
    96 
    97      // Create Layer that contains m1
    97      // Create Layer that contains m1x
    98      Layer layer = Layer.boot().defineModules(cf, map::get);
    98      Layer layer = Layer.boot().defineModules(cf, map::get);
    99 
    99 
   100      assertTrue(layer.findLoader("m1") == loader);
   100      assertTrue(layer.findLoader("m1x") == loader);
   101      assertTrue(layer.findLoader("java.base") == null);
   101      assertTrue(layer.findLoader("java.base") == null);
   102 
   102 
   103      // now use the same loader to load class p3.c3
   103      // now use the same loader to load class p3.c3
   104      Class p3_c3_class = loader.loadClass("p3.c3");
   104      Class p3_c3_class = loader.loadClass("p3.c3");
   105 
   105 
   106      // Attempt access
   106      // Attempt access
   107      try {
   107      try {
   108          p3_c3_class.newInstance();
   108          p3_c3_class.newInstance();
   109          throw new RuntimeException("Test Failed, strict module m1, type p3.c3, should not be able to access " +
   109          throw new RuntimeException("Test Failed, strict module m1x, type p3.c3, should not be able to access " +
   110                                     "public type c4 defined in unnamed module");
   110                                     "public type c4 defined in unnamed module");
   111      } catch (IllegalAccessError e) {
   111      } catch (IllegalAccessError e) {
   112      }
   112      }
   113  }
   113  }
   114 
   114 
   115  // Module m1 is a strict module and has established
   115  // Module m1x is a strict module and has established
   116  // readability to an unnamed module that c4 is defined in.
   116  // readability to an unnamed module that c4 is defined in.
   117  public void test_strictModuleUnnamedReadableLayer() throws Throwable {
   117  public void test_strictModuleUnnamedReadableLayer() throws Throwable {
   118 
   118 
   119      // Define module:     m1
   119      // Define module:     m1x
   120      // Can read:          java.base
   120      // Can read:          java.base
   121      // Packages:          p3
   121      // Packages:          p3
   122      // Packages exported: p3 is exported unqualifiedly
   122      // Packages exported: p3 is exported unqualifiedly
   123      ModuleDescriptor descriptor_m1 =
   123      ModuleDescriptor descriptor_m1x =
   124              ModuleDescriptor.module("m1")
   124              ModuleDescriptor.newModule("m1x")
   125                      .requires("java.base")
   125                      .requires("java.base")
   126                      .exports("p3")
   126                      .exports("p3")
   127                      .build();
   127                      .build();
   128 
   128 
   129      // Set up a ModuleFinder containing all modules for this layer.
   129      // Set up a ModuleFinder containing all modules for this layer.
   130      ModuleFinder finder = ModuleLibrary.of(descriptor_m1);
   130      ModuleFinder finder = ModuleLibrary.of(descriptor_m1x);
   131 
   131 
   132      // Resolves "m1"
   132      // Resolves "m1x"
   133      Configuration cf = Layer.boot()
   133      Configuration cf = Layer.boot()
   134              .configuration()
   134              .configuration()
   135              .resolveRequires(finder, ModuleFinder.of(), Set.of("m1"));
   135              .resolve(finder, ModuleFinder.of(), Set.of("m1x"));
   136 
   136 
   137      MySameClassLoader loader = new MySameClassLoader();
   137      MySameClassLoader loader = new MySameClassLoader();
   138      // map module m1 to class loader.
   138      // map module m1x to class loader.
   139      // class c4 will be loaded in an unnamed module/loader.
   139      // class c4 will be loaded in an unnamed module/loader.
   140      Map<String, ClassLoader> map = new HashMap<>();
   140      Map<String, ClassLoader> map = new HashMap<>();
   141      map.put("m1", loader);
   141      map.put("m1x", loader);
   142 
   142 
   143      // Create Layer that contains m1
   143      // Create Layer that contains m1x
   144      Layer layer = Layer.boot().defineModules(cf, map::get);
   144      Layer layer = Layer.boot().defineModules(cf, map::get);
   145 
   145 
   146      assertTrue(layer.findLoader("m1") == loader);
   146      assertTrue(layer.findLoader("m1x") == loader);
   147      assertTrue(layer.findLoader("java.base") == null);
   147      assertTrue(layer.findLoader("java.base") == null);
   148 
   148 
   149      // now use the same loader to load class p3.c3ReadEdge
   149      // now use the same loader to load class p3.c3ReadEdge
   150      Class p3_c3_class = loader.loadClass("p3.c3ReadEdge");
   150      Class p3_c3_class = loader.loadClass("p3.c3ReadEdge");
   151 
   151 
   152      try {
   152      try {
   153         // Read edge between m1 and the unnamed module that loads c4 is established in
   153         // Read edge between m1x and the unnamed module that loads c4 is established in
   154         // c3ReadEdge's ctor before attempting access.
   154         // c3ReadEdge's ctor before attempting access.
   155         p3_c3_class.newInstance();
   155         p3_c3_class.newInstance();
   156      } catch (IllegalAccessError e) {
   156      } catch (IllegalAccessError e) {
   157          throw new RuntimeException("Test Failed, module m1, type p3.c3ReadEdge, has established readability to " +
   157          throw new RuntimeException("Test Failed, module m1x, type p3.c3ReadEdge, has established readability to " +
   158                                     "c4 loader's unnamed module, access should be allowed: " + e.getMessage());
   158                                     "c4 loader's unnamed module, access should be allowed: " + e.getMessage());
   159      }
   159      }
   160  }
   160  }
   161 
   161 
   162  public static void main(String args[]) throws Throwable {
   162  public static void main(String args[]) throws Throwable {