1 /*
   2  * Copyright (c) 2015, 2016, 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  * @bug 8132734 8144062 8159785
  27  * @summary Test that URL connections to multi-release jars can be runtime versioned
  28  * @library /lib/testlibrary/java/util/jar
  29  * @modules jdk.compiler
  30  *          jdk.httpserver
  31  *          jdk.jartool
  32  * @build Compiler JarBuilder CreateMultiReleaseTestJars SimpleHttpServer
  33  * @run testng MultiReleaseJarURLConnection
  34  */
  35 
  36 import java.io.IOException;
  37 import java.io.InputStream;
  38 import java.lang.invoke.MethodHandle;
  39 import java.lang.invoke.MethodHandles;
  40 import java.lang.invoke.MethodType;
  41 import java.net.JarURLConnection;
  42 import java.net.URL;
  43 import java.net.URLClassLoader;
  44 import java.net.URLConnection;
  45 import java.nio.file.Files;
  46 import java.nio.file.Paths;
  47 import java.util.Enumeration;
  48 import java.util.jar.JarFile;
  49 
  50 import org.testng.Assert;
  51 import org.testng.annotations.AfterClass;
  52 import org.testng.annotations.BeforeClass;
  53 import org.testng.annotations.DataProvider;
  54 import org.testng.annotations.Test;
  55 
  56 public class MultiReleaseJarURLConnection {
  57     String userdir = System.getProperty("user.dir",".");
  58     String unversioned = userdir + "/unversioned.jar";
  59     String unsigned = userdir + "/multi-release.jar";
  60     String signed = userdir + "/signed-multi-release.jar";
  61     SimpleHttpServer server;
  62 
  63     @BeforeClass
  64     public void initialize() throws Exception {
  65         CreateMultiReleaseTestJars creator = new CreateMultiReleaseTestJars();
  66         creator.compileEntries();
  67         creator.buildUnversionedJar();
  68         creator.buildMultiReleaseJar();
  69         creator.buildSignedMultiReleaseJar();
  70 
  71         server = new SimpleHttpServer();
  72         server.start();
  73 
  74     }
  75 
  76     @AfterClass
  77     public void close() throws IOException {
  78         // Windows requires server to stop before file is deleted
  79         if (server != null)
  80             server.stop();
  81         Files.delete(Paths.get(unversioned));
  82         Files.delete(Paths.get(unsigned));
  83         Files.delete(Paths.get(signed));
  84     }
  85 
  86     @DataProvider(name = "data")
  87     public Object[][] createData() {
  88         return new Object[][]{
  89                 {"unversioned", unversioned},
  90                 {"unsigned", unsigned},
  91                 {"signed", signed}
  92         };
  93     }
  94 
  95     @Test(dataProvider = "data")
  96     public void testRuntimeVersioning(String style, String file) throws Exception {
  97         String urlFile = "jar:file:" + file + "!/";
  98         String baseUrlEntry = urlFile + "version/Version.java";
  99         String rtreturn = "return " + Runtime.version().major();
 100 
 101         Assert.assertTrue(readAndCompare(new URL(baseUrlEntry), "return 8"));
 102         // #runtime is "magic" for a multi-release jar, but not for unversioned jar
 103         Assert.assertTrue(readAndCompare(new URL(baseUrlEntry + "#runtime"),
 104                 style.equals("unversioned") ? "return 8" : rtreturn));
 105         // #fragment or any other fragment is not magic
 106         Assert.assertTrue(readAndCompare(new URL(baseUrlEntry + "#fragment"), "return 8"));
 107         // cached entities not affected
 108         Assert.assertTrue(readAndCompare(new URL(baseUrlEntry), "return 8"));
 109 
 110         // the following tests will not work with unversioned jars
 111         if (style.equals("unversioned")) return;
 112 
 113         // direct access to versioned entry
 114         String versUrlEntry = urlFile + "META-INF/versions/" + Runtime.version().major()
 115                 + "/version/Version.java";
 116         Assert.assertTrue(readAndCompare(new URL(versUrlEntry), rtreturn));
 117         // adding any fragment does not change things
 118         Assert.assertTrue(readAndCompare(new URL(versUrlEntry + "#runtime"), rtreturn));
 119         Assert.assertTrue(readAndCompare(new URL(versUrlEntry + "#fragment"), rtreturn));
 120 
 121         // it really doesn't change things
 122         versUrlEntry = urlFile + "META-INF/versions/10/version/Version.java";
 123         Assert.assertTrue(readAndCompare(new URL(versUrlEntry), "return 10"));
 124         Assert.assertTrue(readAndCompare(new URL(versUrlEntry + "#runtime"), "return 10"));
 125         Assert.assertTrue(readAndCompare(new URL(versUrlEntry + "#fragment"), "return 10"));
 126     }
 127 
 128     @Test(dataProvider = "data")
 129     public void testCachedJars(String style, String file) throws Exception {
 130         String urlFile = "jar:file:" + file + "!/";
 131 
 132         URL rootUrl = new URL(urlFile);
 133         JarURLConnection juc = (JarURLConnection)rootUrl.openConnection();
 134         JarFile rootJar = juc.getJarFile();
 135         Runtime.Version root = rootJar.getVersion();
 136 
 137         URL runtimeUrl = new URL(urlFile + "#runtime");
 138         juc = (JarURLConnection)runtimeUrl.openConnection();
 139         JarFile runtimeJar = juc.getJarFile();
 140         Runtime.Version runtime = runtimeJar.getVersion();
 141         if (style.equals("unversioned")) {
 142             Assert.assertEquals(root, runtime);
 143         } else {
 144             Assert.assertNotEquals(root, runtime);
 145         }
 146 
 147         juc = (JarURLConnection)rootUrl.openConnection();
 148         JarFile jar = juc.getJarFile();
 149         Assert.assertEquals(jar.getVersion(), root);
 150         Assert.assertEquals(jar, rootJar);
 151 
 152         juc = (JarURLConnection)runtimeUrl.openConnection();
 153         jar = juc.getJarFile();
 154         Assert.assertEquals(jar.getVersion(), runtime);
 155         Assert.assertEquals(jar, runtimeJar);
 156 
 157         rootJar.close();
 158         runtimeJar.close();
 159         jar.close(); // probably not needed
 160     }
 161 
 162     @DataProvider(name = "resourcedata")
 163     public Object[][] createResourceData() throws Exception {
 164         return new Object[][]{
 165                 {"unversioned", Paths.get(unversioned).toUri().toURL()},
 166                 {"unsigned", Paths.get(unsigned).toUri().toURL()},
 167                 {"signed", Paths.get(signed).toUri().toURL()},
 168                 {"unversioned", new URL("file:" + unversioned)},
 169                 {"unsigned", new URL("file:" + unsigned)},
 170                 {"signed", new URL("file:" + signed)},
 171                 {"unversioned", new URL("jar:file:" + unversioned + "!/")},
 172                 {"unsigned", new URL("jar:file:" + unsigned + "!/")},
 173                 {"signed", new URL("jar:file:" + signed + "!/")},
 174                 // external jar received via http protocol
 175                 {"http", new URL("jar:http://localhost:" + server.getPort() + "/multi-release.jar!/")},
 176                 {"http", new URL("http://localhost:" + server.getPort() + "/multi-release.jar")},
 177 
 178         };
 179     }
 180 
 181     @Test(dataProvider = "resourcedata")
 182     public void testResources(String style, URL url) throws Throwable {
 183         //System.out.println("  testing " + style + " url: " + url);
 184         URL[] urls = {url};
 185         URLClassLoader cldr = new URLClassLoader(urls);
 186         Class<?> vcls = cldr.loadClass("version.Version");
 187 
 188         // verify we are loading a runtime versioned class
 189         MethodType mt = MethodType.methodType(int.class);
 190         MethodHandle mh = MethodHandles.lookup().findVirtual(vcls, "getVersion", mt);
 191         Assert.assertEquals((int)mh.invoke(vcls.newInstance()),
 192                 style.equals("unversioned") ? 8 : Runtime.version().major());
 193 
 194         // now get a resource and verify that we don't have a fragment attached
 195         Enumeration<URL> vclsUrlEnum = cldr.getResources("version/Version.class");
 196         Assert.assertTrue(vclsUrlEnum.hasMoreElements());
 197         URL vclsUrls[] = new URL[] {
 198             vcls.getResource("/version/Version.class"),
 199             vcls.getResource("Version.class"),
 200             cldr.getResource("version/Version.class"),
 201             vclsUrlEnum.nextElement()
 202         };
 203         Assert.assertFalse(vclsUrlEnum.hasMoreElements());
 204         for (URL vclsUrl : vclsUrls) {
 205             String fragment = vclsUrl.getRef();
 206             Assert.assertNull(fragment);
 207 
 208             // and verify that the the url is a reified pointer to the runtime entry
 209             String rep = vclsUrl.toString();
 210             //System.out.println("    getResource(\"/version/Version.class\") returned: " + rep);
 211             if (style.equals("http")) {
 212                 Assert.assertTrue(rep.startsWith("jar:http:"));
 213             } else {
 214                 Assert.assertTrue(rep.startsWith("jar:file:"));
 215             }
 216             String suffix;
 217             if (style.equals("unversioned")) {
 218                 suffix = ".jar!/version/Version.class";
 219             } else {
 220                 suffix = ".jar!/META-INF/versions/" + Runtime.version().major()
 221                         + "/version/Version.class";
 222             }
 223             Assert.assertTrue(rep.endsWith(suffix));
 224         }
 225         cldr.close();
 226     }
 227 
 228 
 229     private boolean readAndCompare(URL url, String match) throws Exception {
 230         boolean result;
 231         // necessary to do it this way, instead of openStream(), so we can
 232         // close underlying JarFile, otherwise windows can't delete the file
 233         URLConnection conn = url.openConnection();
 234         try (InputStream is = conn.getInputStream()) {
 235             byte[] bytes = is.readAllBytes();
 236             result = (new String(bytes)).contains(match);
 237         }
 238         if (conn instanceof JarURLConnection) {
 239             ((JarURLConnection)conn).getJarFile().close();
 240         }
 241         return result;
 242     }
 243 }