1 /*
   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.
   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  * @modules java.base/jdk.internal.misc
  27  * @library /test/lib ..
  28  * @build sun.hotspot.WhiteBox
  29  * @compile/module=java.base java/lang/ModuleHelper.java
  30  * @run driver ClassFileInstaller sun.hotspot.WhiteBox
  31  *                              sun.hotspot.WhiteBox$WhiteBoxPermission
  32  * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI JVMDefineModule
  33  */
  34 
  35 import static jdk.test.lib.Asserts.*;
  36 import java.sql.Time;
  37 
  38 public class JVMDefineModule {
  39 
  40     public static void main(String args[]) throws Throwable {
  41         MyClassLoader cl = new MyClassLoader();
  42         Object m;
  43 
  44         // NULL classloader argument, expect success
  45         m = ModuleHelper.ModuleObject("mymodule", null, new String[] { "mypackage" });
  46         assertNotNull(m, "Module should not be null");
  47         ModuleHelper.DefineModule(m, false, "9.0", "mymodule/here", new String[] { "mypackage" });
  48 
  49 /* Invalid test, won't compile.
  50         // Invalid classloader argument, expect an IAE
  51         try {
  52             m = ModuleHelper.ModuleObject("mymodule_one", new Object(), new String[] { "mypackage1" });
  53             ModuleHelper.DefineModule(m, false, "9.0", "mymodule/here", new String[] { "mypackage1" });
  54             throw new RuntimeException("Failed to get expected IAE for bad loader");
  55         } catch(IllegalArgumentException e) {
  56             // Expected
  57         }
  58 */
  59 
  60         // NULL package argument, should not throw an exception
  61         m = ModuleHelper.ModuleObject("mymoduleTwo", cl, new String[] { "nullpkg" });
  62         assertNotNull(m, "Module should not be null");
  63         ModuleHelper.DefineModule(m, false, "9.0", "mymoduleTwo/here", null);
  64 
  65         // Null module argument, expect an NPE
  66         try {
  67             ModuleHelper.DefineModule(null, false, "9.0", "mymodule/here", new String[] { "mypackage1" });
  68             throw new RuntimeException("Failed to get expected NPE for null module");
  69         } catch(NullPointerException e) {
  70             if (!e.getMessage().contains("Null module object")) {
  71               throw new RuntimeException("Failed to get expected IAE message for null module: " + e.getMessage());
  72             }
  73             // Expected
  74         }
  75 
  76         // Invalid module argument, expect an IAE
  77         try {
  78             ModuleHelper.DefineModule(new Object(), false, "9.0", "mymodule/here", new String[] { "mypackage1" });
  79             throw new RuntimeException("Failed to get expected IAE or NPE for bad module");
  80         } catch(IllegalArgumentException e) {
  81             if (!e.getMessage().contains("module is not an instance of type java.lang.Module")) {
  82               throw new RuntimeException("Failed to get expected IAE message for bad module: " + e.getMessage());
  83             }
  84         }
  85 
  86         // NULL module name, expect an IAE or NPE
  87         try {
  88             m = ModuleHelper.ModuleObject(null, cl, new String[] { "mypackage2" });
  89             ModuleHelper.DefineModule(m, false, "9.0", "mymodule/here", new String[] { "mypackage2" });
  90             throw new RuntimeException("Failed to get expected NPE for NULL module");
  91         } catch(IllegalArgumentException e) {
  92             // Expected
  93         } catch(NullPointerException e) {
  94             // Expected
  95         }
  96 
  97         // module name is java.base, expect an IAE
  98         m = ModuleHelper.ModuleObject("java.base", cl, new String[] { "mypackage3" });
  99         try {
 100             ModuleHelper.DefineModule(m, false, "9.0", "mymodule/here", new String[] { "mypackage3" });
 101             throw new RuntimeException("Failed to get expected IAE for java.base, not defined with boot class loader");
 102         } catch(IllegalArgumentException e) {
 103             if (!e.getMessage().contains("Class loader must be the boot class loader")) {
 104               throw new RuntimeException("Failed to get expected IAE message for java.base: " + e.getMessage());
 105             }
 106         }
 107 
 108         // Empty entry in package list, expect an IAE
 109         m = ModuleHelper.ModuleObject("module.y", cl, new String[] { "mypackageX", "mypackageY" });
 110         try {
 111             ModuleHelper.DefineModule(m, false, "9.0", "mymodule/here", new String[] { "mypackageX", "", "mypackageY" });
 112             throw new RuntimeException("Failed to get IAE for empty package");
 113         } catch(IllegalArgumentException e) {
 114             if (!e.getMessage().contains("Invalid package name")) {
 115               throw new RuntimeException("Failed to get expected IAE message empty package entry: " + e.getMessage());
 116             }
 117         }
 118 
 119         // Duplicate module name, expect an ISE
 120         m = ModuleHelper.ModuleObject("Module_A", cl, new String[] { "mypackage6" });
 121         assertNotNull(m, "Module should not be null");
 122         ModuleHelper.DefineModule(m, false, "9.0", "module.name/here", new String[] { "mypackage6" });
 123         try {
 124             ModuleHelper.DefineModule(m, false, "9.0", "module.name/here", new String[] { "mypackage6a" });
 125             throw new RuntimeException("Failed to get ISE for duplicate module");
 126         } catch(IllegalStateException e) {
 127             if (!e.getMessage().contains("Module Module_A is already defined")) {
 128               throw new RuntimeException("Failed to get expected ISE message for duplicate module: " + e.getMessage());
 129             }
 130         }
 131 
 132         // Package is already defined for class loader, expect an ISE
 133         m = ModuleHelper.ModuleObject("dupl.pkg.module", cl, new String[] { "mypackage6b" });
 134         try {
 135             ModuleHelper.DefineModule(m, false, "9.0", "module.name/here", new String[] { "mypackage6" });
 136             throw new RuntimeException("Failed to get ISE for existing package");
 137         } catch(IllegalStateException e) {
 138             if (!e.getMessage().contains("Package mypackage6 for module dupl.pkg.module is already in another module, Module_A, defined to the class loader")) {
 139               throw new RuntimeException("Failed to get expected ISE message for duplicate package: " + e.getMessage());
 140             }
 141         }
 142 
 143         // Empty module name, expect an IAE
 144         try {
 145             m = ModuleHelper.ModuleObject("", cl, new String[] { "mypackage8" });
 146             ModuleHelper.DefineModule(m, false, "9.0", "module.name/here", new String[] { "mypackage8" });
 147             throw new RuntimeException("Failed to get expected IAE for empty module name");
 148         } catch(IllegalArgumentException e) {
 149             // Expected
 150         }
 151 
 152         // Module name with ';', not allowed in java source
 153         try {
 154             m = ModuleHelper.ModuleObject("bad;name", cl, new String[] { "mypackage9" });
 155             ModuleHelper.DefineModule(m, false, "9.0", "module.name/here", new String[] { "mypackage9" });
 156             throw new RuntimeException("Failed to get expected IAE for bad;name");
 157         } catch(IllegalArgumentException e) {
 158             // Expected
 159         }
 160 
 161         // Module name with leading dot, not allowed in java source
 162         try {
 163             m = ModuleHelper.ModuleObject(".leadingdot", cl, new String[] { "mypackage9a" });
 164             ModuleHelper.DefineModule(m, false, "9.0", "module.name/here", new String[] { "mypackage9a" });
 165             throw new RuntimeException("Failed to get expected IAE for .leadingdot");
 166         } catch(IllegalArgumentException e) {
 167             // Expected
 168         }
 169 
 170         // Module name with trailing dot, not allowed in java source
 171         try {
 172             m = ModuleHelper.ModuleObject("trailingdot.", cl, new String[] { "mypackage9b" });
 173             ModuleHelper.DefineModule(m, false, "9.0", "module.name/here", new String[] { "mypackage9b" });
 174             throw new RuntimeException("Failed to get expected IAE for trailingdot.");
 175         } catch(IllegalArgumentException e) {
 176             // Expected
 177         }
 178 
 179         // Module name with consecutive dots, not allowed in java source
 180         try {
 181             m = ModuleHelper.ModuleObject("trailingdot.", cl, new String[] { "mypackage9b" });
 182             ModuleHelper.DefineModule(m, false, "9.0", "module.name/here", new String[] { "mypackage9b" });
 183             throw new RuntimeException("Failed to get expected IAE for trailingdot.");
 184         } catch(IllegalArgumentException e) {
 185             // Expected
 186         }
 187 
 188         // module name with multiple dots, should be okay
 189         m = ModuleHelper.ModuleObject("more.than.one.dat", cl, new String[] { "mypackage9d" });
 190         assertNotNull(m, "Module should not be null");
 191         ModuleHelper.DefineModule(m, false, "9.0", "module.name/here", new String[] { "mypackage9d" });
 192 
 193         // Zero length package list, should be okay
 194         m = ModuleHelper.ModuleObject("zero.packages", cl, new String[] { });
 195         assertNotNull(m, "Module should not be null");
 196         ModuleHelper.DefineModule(m, false, "9.0", "module.name/here", new String[] { });
 197 
 198         // Package name with dots, should be okay
 199         m = ModuleHelper.ModuleObject("moduleFive", cl, new String[] { "your.apackage" });
 200         assertNotNull(m, "Module should not be null");
 201         ModuleHelper.DefineModule(m, false, "9.0", "moduleFive", new String[] { "your.apackage" });
 202 
 203         // Invalid package name, expect an IAE
 204         m = ModuleHelper.ModuleObject("moduleSix", cl, new String[] { "foo" }); // Name irrelevant
 205         try {
 206             ModuleHelper.DefineModule(m, false, "9.0", "module.name/here", new String[] { ";your/apackage" });
 207             throw new RuntimeException("Failed to get expected IAE for ;your.apackage");
 208         } catch(IllegalArgumentException e) {
 209             if (!e.getMessage().contains("Invalid package name")) {
 210               throw new RuntimeException("Failed to get expected IAE message for bad package name: " + e.getMessage());
 211             }
 212         }
 213 
 214         // Invalid package name, expect an IAE
 215         m = ModuleHelper.ModuleObject("moduleSeven", cl, new String[] { "foo" }); // Name irrelevant
 216         try {
 217             ModuleHelper.DefineModule(m, false, "9.0", "module.name/here", new String[] { "7[743" });
 218             throw new RuntimeException("Failed to get expected IAE for package 7[743");
 219         } catch(IllegalArgumentException e) {
 220             if (!e.getMessage().contains("Invalid package name")) {
 221               throw new RuntimeException("Failed to get expected IAE message for bad package name: " + e.getMessage());
 222             }
 223         }
 224 
 225         // Package named "java" defined to a class loader other than the boot or platform class loader, expect an IAE
 226         m = ModuleHelper.ModuleObject("modulejavapkgOne", cl, new String[] { "java/foo" });
 227         try {
 228             // module m is defined to an instance of MyClassLoader class loader
 229             ModuleHelper.DefineModule(m, false, "9.0", "modulejavapkgOne", new String[] { "java/foo" });
 230             throw new RuntimeException("Failed to get expected IAE for package java/foo");
 231         } catch(IllegalArgumentException e) {
 232             if (!e.getMessage().contains("prohibited package name")) {
 233               throw new RuntimeException("Failed to get expected IAE message for prohibited package name: " + e.getMessage());
 234             }
 235         }
 236 
 237         // Package named "javabar" defined to a class loader other than the boot or platform class loader, should be ok
 238         m = ModuleHelper.ModuleObject("modulejavapkgTwo", cl, new String[] { "javabar" });
 239         assertNotNull(m, "Module should not be null");
 240         ModuleHelper.DefineModule(m, false, "9.0", "modulejavapkgTwo", new String[] { "javabar" });
 241 
 242         // Package named "java" defined to the boot class loader, should be ok
 243         //   m's type is a java.lang.Object, module is java.base
 244         //   java.base module is defined to the boot loader
 245         ClassLoader boot_loader = m.getClass().getClassLoader();
 246         m = ModuleHelper.ModuleObject("modulejavapkgThree", boot_loader, new String[] { "java/foo" });
 247         assertNotNull(m, "Module should not be null");
 248         ModuleHelper.DefineModule(m, false, "9.0", "modulejavapkgThree", new String[] { "java/foo" });
 249 
 250         // Package named "java" defined to the platform class loader, should be ok
 251         //   java.sql module defined to the platform class loader.
 252         java.sql.Time jst = new java.sql.Time(45 * 1000);
 253         ClassLoader platform_loader = jst.getClass().getClassLoader();
 254         m = ModuleHelper.ModuleObject("modulejavapkgFour", platform_loader, new String[] { "java/foo" });
 255         assertNotNull(m, "Module should not be null");
 256         ModuleHelper.DefineModule(m, false, "9.0", "modulejavapkgFour", new String[] { "java/foo" });
 257 
 258         // module version that is null, should be okay
 259         m = ModuleHelper.ModuleObject("moduleEight", cl, new String[] { "a_package_8" });
 260         assertNotNull(m, "Module should not be null");
 261         ModuleHelper.DefineModule(m, false, null, "moduleEight/here", new String[] { "a_package_8" });
 262 
 263         // module version that is "", should be okay
 264         m = ModuleHelper.ModuleObject("moduleNine", cl, new String[] { "a_package_9" });
 265         assertNotNull(m, "Module should not be null");
 266         ModuleHelper.DefineModule(m, false, "", "moduleNine/here", new String[] { "a_package_9" });
 267 
 268         // module location that is null, should be okay
 269         m = ModuleHelper.ModuleObject("moduleTen", cl, new String[] { "a_package_10" });
 270         assertNotNull(m, "Module should not be null");
 271         ModuleHelper.DefineModule(m, false, "9.0", null, new String[] { "a_package_10" });
 272 
 273         // module location that is "", should be okay
 274         m = ModuleHelper.ModuleObject("moduleEleven", cl, new String[] { "a_package_11" });
 275         assertNotNull(m, "Module should not be null");
 276         ModuleHelper.DefineModule(m, false, "9.0", "", new String[] { "a_package_11" });
 277 
 278         // module with very long package names, should be okay
 279         String[] longPackageNames = new String[] {
 280          "mypackage/mypackage/mypackage/mypackage1",
 281          "mypackage/mypackage/mypackage/mypackage/" +
 282          "mypackage/mypackage/mypackage/mypackage/" +
 283          "mypackage/mypackage/mypackage/mypackage/" +
 284          "mypackage/mypackage/mypackage/mypackage/" +
 285          "mypackage/mypackage/mypackage/mypackage/" +
 286          "mypackage/mypackage/mypackage/mypackage/" +
 287          "mypackage/mypackage/mypackage/mypackage/" +
 288          "mypackage/mypackage/mypackage/mypackage2",
 289          "mypackage/mypackage/mypackage/mypackage/" +
 290          "mypackage/mypackage/mypackage/mypackage/" +
 291          "mypackage/mypackage/mypackage/mypackage/" +
 292          "mypackage/mypackage/mypackage/mypackage/" +
 293          "mypackage/mypackage/mypackage/mypackage/" +
 294          "mypackage/mypackage/mypackage/mypackage/" +
 295          "mypackage/mypackage/mypackage/mypackage/" +
 296          "mypackage/mypackage/mypackage/mypackage/" +
 297          "mypackage/mypackage/mypackage/mypackage/" +
 298          "mypackage/mypackage/mypackage/mypackage/" +
 299          "mypackage/mypackage/mypackage/mypackage/" +
 300          "mypackage/mypackage/mypackage/mypackage3"
 301         };
 302         m = ModuleHelper.ModuleObject("moduleTwelve", cl, longPackageNames);
 303         assertNotNull(m, "Module should not be null");
 304         ModuleHelper.DefineModule(m, false, "9.0", "moduleTwelve", longPackageNames);
 305         // Indirectly test that the package names doesn't get truncated when defined
 306         for (int i = 0; i < longPackageNames.length; i++) {
 307             ModuleHelper.AddModuleExportsToAllUnnamed(m, longPackageNames[i]);
 308         }
 309     }
 310 
 311     static class MyClassLoader extends ClassLoader { }
 312 }