hotspot/test/runtime/modules/AccessCheck/DiffCL_CheckRead.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 Test that if module m1 can not read module m2, then class p1.c1
    28  * @summary Test that if module m1x can not read module m2x, then class p1.c1
    29  *          in module m1 can not access p2.c2 in module m2.
    29  *          in module m1x can not access p2.c2 in module m2x.
    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 p2/c2.java
    33  * @compile p2/c2.java
    34  * @compile p1/c1.java
    34  * @compile p1/c1.java
    45 import java.util.Map;
    45 import java.util.Map;
    46 import java.util.Set;
    46 import java.util.Set;
    47 import myloaders.MyDiffClassLoader;
    47 import myloaders.MyDiffClassLoader;
    48 
    48 
    49 //
    49 //
    50 // ClassLoader1 --> defines m1 --> packages p1
    50 // ClassLoader1 --> defines m1x --> packages p1
    51 // ClassLoader2 --> defines m2 --> packages p2
    51 // ClassLoader2 --> defines m2x --> packages p2
    52 //                  defines m3 --> packages p3
    52 //                  defines m3x --> packages p3
    53 //
    53 //
    54 // m1 can not read m2
    54 // m1x can not read m2x
    55 // package p2 in m2 is exported to m1
    55 // package p2 in m2x is exported to m1x
    56 //
    56 //
    57 // class p1.c1 defined in m1 tries to access p2.c2 defined in m2.
    57 // class p1.c1 defined in m1x tries to access p2.c2 defined in m2x.
    58 // Access denied since m1 can not read m2.
    58 // Access denied since m1x can not read m2x.
    59 //
    59 //
    60 public class DiffCL_CheckRead {
    60 public class DiffCL_CheckRead {
    61 
    61 
    62     // Create a Layer over the boot layer.
    62     // Create a Layer over the boot layer.
    63     // Define modules within this layer to test access between
    63     // Define modules within this layer to test access between
    64     // publicly defined classes within packages of those modules.
    64     // publicly defined classes within packages of those modules.
    65     public void createLayerOnBoot() throws Throwable {
    65     public void createLayerOnBoot() throws Throwable {
    66 
    66 
    67         // Define module:     m1
    67         // Define module:     m1x
    68         // Can read:          java.base, m3
    68         // Can read:          java.base, m3x
    69         // Packages:          p1
    69         // Packages:          p1
    70         // Packages exported: p1 is exported unqualifiedly
    70         // Packages exported: p1 is exported unqualifiedly
    71         ModuleDescriptor descriptor_m1 =
    71         ModuleDescriptor descriptor_m1x =
    72                 ModuleDescriptor.module("m1")
    72                 ModuleDescriptor.newModule("m1x")
    73                         .requires("java.base")
    73                         .requires("java.base")
    74                         .requires("m3")
    74                         .requires("m3x")
    75                         .exports("p1")
    75                         .exports("p1")
    76                         .build();
    76                         .build();
    77 
    77 
    78         // Define module:     m2
    78         // Define module:     m2x
    79         // Can read:          java.base
    79         // Can read:          java.base
    80         // Packages:          p2
    80         // Packages:          p2
    81         // Packages exported: p2 is exported to m1
    81         // Packages exported: p2 is exported to m1x
    82         ModuleDescriptor descriptor_m2 =
    82         ModuleDescriptor descriptor_m2x =
    83                 ModuleDescriptor.module("m2")
    83                 ModuleDescriptor.newModule("m2x")
    84                         .requires("java.base")
    84                         .requires("java.base")
    85                         .exports("p2", Set.of("m1"))
    85                         .exports("p2", Set.of("m1x"))
    86                         .build();
    86                         .build();
    87 
    87 
    88         // Define module:     m3
    88         // Define module:     m3x
    89         // Can read:          java.base, m2
    89         // Can read:          java.base, m2x
    90         // Packages:          p3
    90         // Packages:          p3
    91         // Packages exported: none
    91         // Packages exported: none
    92         ModuleDescriptor descriptor_m3 =
    92         ModuleDescriptor descriptor_m3x =
    93                 ModuleDescriptor.module("m3")
    93                 ModuleDescriptor.newModule("m3x")
    94                         .requires("java.base")
    94                         .requires("java.base")
    95                         .requires("m2")
    95                         .requires("m2x")
    96                         .contains("p3")
    96                         .packages(Set.of("p3"))
    97                         .build();
    97                         .build();
    98 
    98 
    99         // Set up a ModuleFinder containing all modules for this layer.
    99         // Set up a ModuleFinder containing all modules for this layer.
   100         ModuleFinder finder = ModuleLibrary.of(descriptor_m1, descriptor_m2, descriptor_m3);
   100         ModuleFinder finder = ModuleLibrary.of(descriptor_m1x, descriptor_m2x, descriptor_m3x);
   101 
   101 
   102         // Resolves "m1"
   102         // Resolves "m1x"
   103         Configuration cf = Layer.boot()
   103         Configuration cf = Layer.boot()
   104                 .configuration()
   104                 .configuration()
   105                 .resolveRequires(finder, ModuleFinder.of(), Set.of("m1"));
   105                 .resolve(finder, ModuleFinder.of(), Set.of("m1x"));
   106 
   106 
   107         // map each module to differing class loaders for this test
   107         // map each module to differing class loaders for this test
   108         Map<String, ClassLoader> map = new HashMap<>();
   108         Map<String, ClassLoader> map = new HashMap<>();
   109         map.put("m1", MyDiffClassLoader.loader1);
   109         map.put("m1x", MyDiffClassLoader.loader1);
   110         map.put("m2", MyDiffClassLoader.loader2);
   110         map.put("m2x", MyDiffClassLoader.loader2);
   111         map.put("m3", MyDiffClassLoader.loader2);
   111         map.put("m3x", MyDiffClassLoader.loader2);
   112 
   112 
   113         // Create Layer that contains m1, m2 and m3
   113         // Create Layer that contains m1x, m2x and m3x
   114         Layer layer = Layer.boot().defineModules(cf, map::get);
   114         Layer layer = Layer.boot().defineModules(cf, map::get);
   115 
   115 
   116         assertTrue(layer.findLoader("m1") == MyDiffClassLoader.loader1);
   116         assertTrue(layer.findLoader("m1x") == MyDiffClassLoader.loader1);
   117         assertTrue(layer.findLoader("m2") == MyDiffClassLoader.loader2);
   117         assertTrue(layer.findLoader("m2x") == MyDiffClassLoader.loader2);
   118         assertTrue(layer.findLoader("m3") == MyDiffClassLoader.loader2);
   118         assertTrue(layer.findLoader("m3x") == MyDiffClassLoader.loader2);
   119         assertTrue(layer.findLoader("java.base") == null);
   119         assertTrue(layer.findLoader("java.base") == null);
   120 
   120 
   121         // now use the same loader to load class p1.c1
   121         // now use the same loader to load class p1.c1
   122         Class p1_c1_class = MyDiffClassLoader.loader1.loadClass("p1.c1");
   122         Class p1_c1_class = MyDiffClassLoader.loader1.loadClass("p1.c1");
   123         try {
   123         try {
   124             p1_c1_class.newInstance();
   124             p1_c1_class.newInstance();
   125             throw new RuntimeException("Failed to get IAE (p2 in m2 is exported to m1 but m2 is not readable from m1)");
   125             throw new RuntimeException("Failed to get IAE (p2 in m2x is exported to m1x but m2x is not readable from m1x)");
   126         } catch (IllegalAccessError e) {
   126         } catch (IllegalAccessError e) {
   127             System.out.println(e.getMessage());
   127             System.out.println(e.getMessage());
   128             if (!e.getMessage().contains("cannot access")) {
   128             if (!e.getMessage().contains("cannot access")) {
   129                 throw new RuntimeException("Wrong message: " + e.getMessage());
   129                 throw new RuntimeException("Wrong message: " + e.getMessage());
   130             }
   130             }