1 /*
   2  * Copyright (c) 2016, 2018, 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @summary Test that if module m1x can read module m2x, but package p2 in m2x is not
  27  *          exported, then class p1.c1 in m1x can not read p2.c2 in m2x.
  28  * @modules java.base/jdk.internal.misc
  29  * @library /test/lib
  30  * @compile myloaders/MySameClassLoader.java
  31  * @compile p2/c2.java
  32  * @compile p1/c1.java
  33  * @run main/othervm -Xbootclasspath/a:. PkgNotExp
  34  */
  35 
  36 import static jdk.test.lib.Asserts.*;
  37 
  38 import java.lang.module.Configuration;
  39 import java.lang.module.ModuleDescriptor;
  40 import java.lang.module.ModuleFinder;
  41 import java.util.HashMap;
  42 import java.util.Map;
  43 import java.util.Set;
  44 import myloaders.MySameClassLoader;
  45 
  46 //
  47 // ClassLoader1 --> defines m1x --> packages p1
  48 //                  defines m2x --> packages p2
  49 //
  50 // m1x can read m2x
  51 // package p2 in m2x is not exported
  52 //
  53 // class p1.c1 defined in m1x tries to access p2.c2 defined in m2x
  54 // Access denied since p2 is not exported.
  55 //
  56 public class PkgNotExp {
  57 
  58     // Create a layer over the boot layer.
  59     // Define modules within this layer to test access between
  60     // publically defined classes within packages of those modules.
  61     public void createLayerOnBoot() throws Throwable {
  62 
  63         // Define module:     m1x
  64         // Can read:          java.base, m2x
  65         // Packages:          p1
  66         // Packages exported: p1 is exported unqualifiedly
  67         ModuleDescriptor descriptor_m1x =
  68                 ModuleDescriptor.newModule("m1x")
  69                         .requires("java.base")
  70                         .requires("m2x")
  71                         .exports("p1")
  72                         .build();
  73 
  74         // Define module:     m2x
  75         // Can read:          java.base
  76         // Packages:          p2
  77         // Packages exported: none
  78         ModuleDescriptor descriptor_m2x =
  79                 ModuleDescriptor.newModule("m2x")
  80                         .requires("java.base")
  81                         .packages(Set.of("p2"))
  82                         .build();
  83 
  84         // Set up a ModuleFinder containing all modules for this layer.
  85         ModuleFinder finder = ModuleLibrary.of(descriptor_m1x, descriptor_m2x);
  86 
  87         // Resolves "m1x"
  88         Configuration cf = ModuleLayer.boot()
  89                 .configuration()
  90                 .resolve(finder, ModuleFinder.of(), Set.of("m1x"));
  91 
  92         // map each module to the same class loader for this test
  93         Map<String, ClassLoader> map = new HashMap<>();
  94         map.put("m1x", MySameClassLoader.loader1);
  95         map.put("m2x", MySameClassLoader.loader1);
  96 
  97         // Create layer that contains m1x and m2x
  98         ModuleLayer layer = ModuleLayer.boot().defineModules(cf, map::get);
  99 
 100         assertTrue(layer.findLoader("m1x") == MySameClassLoader.loader1);
 101         assertTrue(layer.findLoader("m2x") == MySameClassLoader.loader1);
 102 
 103         // now use the same loader to load class p1.c1
 104         Class p1_c1_class = MySameClassLoader.loader1.loadClass("p1.c1");
 105         try {
 106             p1_c1_class.newInstance();
 107             throw new RuntimeException("Failed to get IAE (p2 in m2x is not exported)");
 108         } catch (IllegalAccessError e) {
 109           System.out.println(e.getMessage());
 110           if (!e.getMessage().contains("does not export")) {
 111               throw new RuntimeException("Wrong message: " + e.getMessage());
 112           }
 113         }
 114     }
 115 
 116     public static void main(String args[]) throws Throwable {
 117       PkgNotExp test = new PkgNotExp();
 118       test.createLayerOnBoot();
 119     }
 120 }