1 /*
   2  * Copyright (c) 2019, 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 import java.io.ByteArrayInputStream;
  25 import java.io.File;
  26 import java.io.InputStream;
  27 import java.net.URL;
  28 import java.net.URLClassLoader;
  29 import java.nio.file.Files;
  30 import java.nio.file.Path;
  31 import java.nio.file.Paths;
  32 import java.nio.file.StandardCopyOption;
  33 import java.util.jar.Attributes;
  34 import java.util.jar.Manifest;
  35 import java.util.zip.ZipEntry;
  36 import java.util.zip.ZipOutputStream;
  37 import jdk.testlibrary.InMemoryJavaCompiler;
  38 import jdk.testlibrary.JarUtils;
  39 
  40 /*
  41  * @test
  42  * @bug 8216401
  43  * @summary Test loading of JAR Class-Path entry with file: scheme
  44  * @library /lib/testlibrary
  45  * @build jdk.testlibrary.* JarClassPathFileEntry
  46  *
  47  * @run main/othervm JarClassPathFileEntry
  48  * @run main/othervm -Djdk.net.URLClassPath.disableClassPathURLCheck=true JarClassPathFileEntry
  49  * @run main/othervm -Djdk.net.URLClassPath.disableClassPathURLCheck=false JarClassPathFileEntry
  50  */
  51 
  52 public class JarClassPathFileEntry {
  53     private final static boolean IS_WINDOWS = System.getProperty("os.name").startsWith("Windows");
  54 
  55     private final static String TEST_CLASSES = System.getProperty("test.classes");
  56     private final static String OTHER_DIR = TEST_CLASSES + "/OTHER/";
  57 
  58     private final static Path OTHER_JAR_PATH = Paths.get(OTHER_DIR, "Other.jar");
  59     private final static Path CONTEXT_JAR_PATH = Paths.get(TEST_CLASSES, "Context.jar");
  60 
  61     public static void main(String[] args) throws Throwable {
  62         // Create Other.class in OTHER_DIR, off the default classpath
  63         byte klassbuf[] = InMemoryJavaCompiler.compile("Other",
  64                                                        "public class Other {}");
  65         writeClassToDisk("Other", klassbuf, OTHER_DIR);
  66 
  67         // Create Other.jar in OTHER_DIR
  68         JarUtils.createJarFile(OTHER_JAR_PATH,
  69                                Paths.get(OTHER_DIR),
  70                                Paths.get(OTHER_DIR, "Other.class"));
  71 
  72         // Create Context.class
  73         klassbuf = InMemoryJavaCompiler.compile("Context",
  74                                                 "public class Context {}");
  75         writeClassToDisk("Context", klassbuf, TEST_CLASSES);
  76 
  77         // Create Context.jar w/ "file:" entry for Other.jar
  78         Manifest mf = new Manifest();
  79         Attributes attrs = mf.getMainAttributes();
  80         attrs.put(Attributes.Name.MANIFEST_VERSION, "1.0");
  81 
  82         String classPathEntry = "file:" + (IS_WINDOWS ? toUnixPath(OTHER_JAR_PATH.toString())
  83                                                       :            OTHER_JAR_PATH.toString());
  84         attrs.put(Attributes.Name.CLASS_PATH, classPathEntry);
  85 
  86         System.out.println("Creating Context.jar with Class-Path: " + classPathEntry);
  87         JarUtils.createJarFile(CONTEXT_JAR_PATH, mf,
  88                                Paths.get(TEST_CLASSES),
  89                                Paths.get(TEST_CLASSES, "Context.class"));
  90 
  91         // Use URLClassLoader w/ Context.jar to load Other.class, which will
  92         // load via the Class-Path entry
  93         URL url = CONTEXT_JAR_PATH.toUri().toURL();
  94         URLClassLoader ucl = new URLClassLoader(new URL[]{ url },
  95                                                 null); // don't delegate to App CL
  96         Class<?> otherClass = Class.forName("Other", true, ucl); // ClassNotFoundException -> fail
  97         System.out.println("Loaded: " + otherClass);
  98     }
  99 
 100     /* Convert a Windows path to a unix-style path, and remove any drive letter */
 101     private static String toUnixPath(String orig) {
 102         String retVal = new File(orig).toURI().getPath();
 103         int colonAt = retVal.indexOf(':');
 104 
 105         if (colonAt != -1 && colonAt < 3) {
 106             retVal = retVal.substring(colonAt + 1); // Start after the drive letter
 107         }
 108         return retVal;
 109     }
 110 
 111 
 112     private static void writeClassToDisk(String className, byte[] bytecode, String prependPath) throws Exception {
 113         writeClassToDisk(null, className, bytecode, prependPath);
 114     }
 115 
 116     private static void writeClassToDisk(ZipOutputStream zos, String className, byte[] bytecode, String prependPath) throws Exception {
 117         // Convert dotted class name to a path to a class file
 118         String pathName = className.replace('.', '/').concat(".class");
 119         if (prependPath.length() > 0) {
 120             pathName = prependPath + "/" + pathName;
 121         }
 122         writeToDisk(zos, pathName, new ByteArrayInputStream(bytecode));
 123     }
 124 
 125     private static void writeToDisk(ZipOutputStream zos, String pathName, InputStream is) throws Exception {
 126         if (zos != null) {
 127             ZipEntry ze = new ZipEntry(pathName);
 128             zos.putNextEntry(ze);
 129             byte[] buf = new byte[1024];
 130             int len;
 131             while ((len = is.read(buf))>0){
 132                 zos.write(buf, 0, len);
 133             }
 134         } else {
 135             // Create the class file's package directory
 136             Path p = Paths.get(pathName);
 137             if (pathName.contains("/")) {
 138                 Files.createDirectories(p.getParent());
 139             }
 140             // Create the class file
 141             Files.copy(is, p, StandardCopyOption.REPLACE_EXISTING);
 142         }
 143         is.close();
 144     }
 145 }