hotspot/test/runtime/modules/AccessCheck/DiffCL_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 an unnamed package
    28  * @summary class p3.c3 defined in module m1x tries to access c4 defined in an unnamed package
    29  *          and an unnamed module.
    29  *          and an unnamed module.
    30  * @modules java.base/jdk.internal.misc
    30  * @modules java.base/jdk.internal.misc
    31  * @library /test/lib
    31  * @library /test/lib
    32  * @compile myloaders/MyDiffClassLoader.java
    32  * @compile myloaders/MyDiffClassLoader.java
    33  * @compile c4.java
    33  * @compile c4.java
    46 import java.util.Map;
    46 import java.util.Map;
    47 import java.util.Set;
    47 import java.util.Set;
    48 import myloaders.MyDiffClassLoader;
    48 import myloaders.MyDiffClassLoader;
    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 DiffCL_UmodUpkg {
    64 public class DiffCL_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      MyDiffClassLoader.loader1 = new MyDiffClassLoader();
    91      MyDiffClassLoader.loader1 = new MyDiffClassLoader();
    92      MyDiffClassLoader.loader2 = new MyDiffClassLoader();
    92      MyDiffClassLoader.loader2 = new MyDiffClassLoader();
    93 
    93 
    94      // map module m1 to class loader.
    94      // map module m1x to class loader.
    95      // class c2 will be loaded in an unnamed module/loader2
    95      // class c2 will be loaded in an unnamed module/loader2
    96      // to achieve differing class loaders.
    96      // to achieve differing class loaders.
    97      Map<String, ClassLoader> map = new HashMap<>();
    97      Map<String, ClassLoader> map = new HashMap<>();
    98      map.put("m1", MyDiffClassLoader.loader1);
    98      map.put("m1x", MyDiffClassLoader.loader1);
    99 
    99 
   100      // Create Layer that contains m1
   100      // Create Layer that contains m1x
   101      Layer layer = Layer.boot().defineModules(cf, map::get);
   101      Layer layer = Layer.boot().defineModules(cf, map::get);
   102 
   102 
   103      assertTrue(layer.findLoader("m1") == MyDiffClassLoader.loader1);
   103      assertTrue(layer.findLoader("m1x") == MyDiffClassLoader.loader1);
   104      assertTrue(layer.findLoader("java.base") == null);
   104      assertTrue(layer.findLoader("java.base") == null);
   105 
   105 
   106      // now use the same loader to load class p3.c3
   106      // now use the same loader to load class p3.c3
   107      Class p3_c3_class = MyDiffClassLoader.loader1.loadClass("p3.c3");
   107      Class p3_c3_class = MyDiffClassLoader.loader1.loadClass("p3.c3");
   108 
   108 
   109      // Attempt access
   109      // Attempt access
   110      try {
   110      try {
   111          p3_c3_class.newInstance();
   111          p3_c3_class.newInstance();
   112          throw new RuntimeException("Test Failed, strict module m1 should not be able to access " +
   112          throw new RuntimeException("Test Failed, strict module m1x should not be able to access " +
   113                                     "public type c4 defined in unnamed module");
   113                                     "public type c4 defined in unnamed module");
   114      } catch (IllegalAccessError e) {
   114      } catch (IllegalAccessError e) {
   115      }
   115      }
   116 }
   116 }
   117 
   117 
   118  // Module m1 is a strict module and has established
   118  // Module m1x is a strict module and has established
   119  // readability to an unnamed module that c4 is defined in.
   119  // readability to an unnamed module that c4 is defined in.
   120  public void test_strictModuleUnnamedReadableLayer() throws Throwable {
   120  public void test_strictModuleUnnamedReadableLayer() throws Throwable {
   121 
   121 
   122      // Define module:     m1
   122      // Define module:     m1x
   123      // Can read:          java.base
   123      // Can read:          java.base
   124      // Packages:          p3
   124      // Packages:          p3
   125      // Packages exported: p3 is exported unqualifiedly
   125      // Packages exported: p3 is exported unqualifiedly
   126      ModuleDescriptor descriptor_m1 =
   126      ModuleDescriptor descriptor_m1x =
   127              ModuleDescriptor.module("m1")
   127              ModuleDescriptor.newModule("m1x")
   128                      .requires("java.base")
   128                      .requires("java.base")
   129                      .exports("p3")
   129                      .exports("p3")
   130                      .build();
   130                      .build();
   131 
   131 
   132      // Set up a ModuleFinder containing all modules for this layer.
   132      // Set up a ModuleFinder containing all modules for this layer.
   133      ModuleFinder finder = ModuleLibrary.of(descriptor_m1);
   133      ModuleFinder finder = ModuleLibrary.of(descriptor_m1x);
   134 
   134 
   135      // Resolves "m1"
   135      // Resolves "m1x"
   136      Configuration cf = Layer.boot()
   136      Configuration cf = Layer.boot()
   137              .configuration()
   137              .configuration()
   138              .resolveRequires(finder, ModuleFinder.of(), Set.of("m1"));
   138              .resolve(finder, ModuleFinder.of(), Set.of("m1x"));
   139 
   139 
   140      MyDiffClassLoader.loader1 = new MyDiffClassLoader();
   140      MyDiffClassLoader.loader1 = new MyDiffClassLoader();
   141      MyDiffClassLoader.loader2 = new MyDiffClassLoader();
   141      MyDiffClassLoader.loader2 = new MyDiffClassLoader();
   142 
   142 
   143      // map module m1 to class loader.
   143      // map module m1x to class loader.
   144      // class c2 will be loaded in an unnamed module/loader2
   144      // class c2 will be loaded in an unnamed module/loader2
   145      // to achieve differing class loaders.
   145      // to achieve differing class loaders.
   146      Map<String, ClassLoader> map = new HashMap<>();
   146      Map<String, ClassLoader> map = new HashMap<>();
   147      map.put("m1", MyDiffClassLoader.loader1);
   147      map.put("m1x", MyDiffClassLoader.loader1);
   148 
   148 
   149      // Create Layer that contains m1
   149      // Create Layer that contains m1x
   150      Layer layer = Layer.boot().defineModules(cf, map::get);
   150      Layer layer = Layer.boot().defineModules(cf, map::get);
   151 
   151 
   152      assertTrue(layer.findLoader("m1") == MyDiffClassLoader.loader1);
   152      assertTrue(layer.findLoader("m1x") == MyDiffClassLoader.loader1);
   153      assertTrue(layer.findLoader("java.base") == null);
   153      assertTrue(layer.findLoader("java.base") == null);
   154 
   154 
   155      // now use the same loader to load class p3.c3ReadEdgeDiffLoader
   155      // now use the same loader to load class p3.c3ReadEdgeDiffLoader
   156      Class p3_c3_class = MyDiffClassLoader.loader1.loadClass("p3.c3ReadEdgeDiffLoader");
   156      Class p3_c3_class = MyDiffClassLoader.loader1.loadClass("p3.c3ReadEdgeDiffLoader");
   157 
   157 
   158      try {
   158      try {
   159         // Read edge between m1 and the unnamed module that loads c4 is established in
   159         // Read edge between m1x and the unnamed module that loads c4 is established in
   160         // C3ReadEdgeDiffLoader's ctor before attempting access.
   160         // C3ReadEdgeDiffLoader's ctor before attempting access.
   161         p3_c3_class.newInstance();
   161         p3_c3_class.newInstance();
   162      } catch (IllegalAccessError e) {
   162      } catch (IllegalAccessError e) {
   163          throw new RuntimeException("Test Failed, module m1 has established readability to " +
   163          throw new RuntimeException("Test Failed, module m1x has established readability to " +
   164                                     "c4 loader's unnamed module, access should be allowed: " + e.getMessage());
   164                                     "c4 loader's unnamed module, access should be allowed: " + e.getMessage());
   165      }
   165      }
   166  }
   166  }
   167 
   167 
   168  public static void main(String args[]) throws Throwable {
   168  public static void main(String args[]) throws Throwable {