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
  27  * @summary Test the extended API and the aliasing additions in JarFile that
  28  *          support multi-release jar files
  29  * @library /lib/testlibrary/java/util/jar
  30  * @build Compiler JarBuilder CreateMultiReleaseTestJars
  31  * @run testng MultiReleaseJarAPI
  32  */
  33 
  34 import java.io.File;
  35 import java.io.IOException;
  36 import java.io.InputStream;
  37 import java.nio.file.Files;
  38 import java.util.Arrays;
  39 import java.util.jar.JarFile;
  40 import java.util.zip.ZipEntry;
  41 import java.util.zip.ZipFile;
  42 
  43 import static java.util.jar.JarFile.Release;
  44 
  45 import org.testng.Assert;
  46 import org.testng.annotations.AfterClass;
  47 import org.testng.annotations.BeforeClass;
  48 import org.testng.annotations.Test;
  49 
  50 
  51 public class MultiReleaseJarAPI {
  52 
  53     static final int MAJOR_VERSION = Runtime.version().major();
  54 
  55     String userdir = System.getProperty("user.dir",".");
  56     CreateMultiReleaseTestJars creator =  new CreateMultiReleaseTestJars();
  57     File unversioned = new File(userdir, "unversioned.jar");
  58     File multirelease = new File(userdir, "multi-release.jar");
  59     File signedmultirelease = new File(userdir, "signed-multi-release.jar");
  60     Release[] values = JarFile.Release.values();
  61 
  62 
  63     @BeforeClass
  64     public void initialize() throws Exception {
  65         creator.compileEntries();
  66         creator.buildUnversionedJar();
  67         creator.buildMultiReleaseJar();
  68         creator.buildSignedMultiReleaseJar();
  69     }
  70 
  71     @AfterClass
  72     public void close() throws IOException {
  73         Files.delete(unversioned.toPath());
  74         Files.delete(multirelease.toPath());
  75         Files.delete(signedmultirelease.toPath());
  76     }
  77 
  78     @Test
  79     public void isMultiReleaseJar() throws Exception {
  80         try (JarFile jf = new JarFile(unversioned)) {
  81             Assert.assertFalse(jf.isMultiRelease());
  82         }
  83 
  84         try (JarFile jf = new JarFile(unversioned, true, ZipFile.OPEN_READ, Release.RUNTIME)) {
  85             Assert.assertFalse(jf.isMultiRelease());
  86         }
  87 
  88         try (JarFile jf = new JarFile(multirelease)) {
  89             Assert.assertFalse(jf.isMultiRelease());
  90         }
  91 
  92         try (JarFile jf = new JarFile(multirelease, true, ZipFile.OPEN_READ, Release.RUNTIME)) {
  93             Assert.assertTrue(jf.isMultiRelease());
  94         }
  95 
  96         testCustomMultiReleaseValue("true", true);
  97         testCustomMultiReleaseValue("true\r\nOther: value", true);
  98         testCustomMultiReleaseValue("true\nOther: value", true);
  99         testCustomMultiReleaseValue("true\rOther: value", true);
 100 
 101         testCustomMultiReleaseValue("false", false);
 102         testCustomMultiReleaseValue(" true", false);
 103         testCustomMultiReleaseValue("true ", false);
 104         testCustomMultiReleaseValue("true\n ", false);
 105         testCustomMultiReleaseValue("true\r ", false);
 106         testCustomMultiReleaseValue("true\n true", false);
 107         testCustomMultiReleaseValue("true\r\n true", false);
 108     }
 109 
 110     private void testCustomMultiReleaseValue(String value, boolean expected) throws Exception {
 111         creator.buildCustomMultiReleaseJar("custom-mr.jar", value);
 112         File custom = new File(userdir, "custom-mr.jar");
 113         try (JarFile jf = new JarFile(custom, true, ZipFile.OPEN_READ, Release.RUNTIME)) {
 114             Assert.assertEquals(jf.isMultiRelease(), expected);
 115         }
 116         Files.delete(custom.toPath());
 117     }
 118 
 119     @Test
 120     public void testVersioning() throws Exception {
 121         // multi-release jar
 122         JarFile jar = new JarFile(multirelease);
 123         Assert.assertEquals(Release.BASE, jar.getVersion());
 124         jar.close();
 125 
 126         for (Release value : values) {
 127             System.err.println("test versioning for Release " + value);
 128             try (JarFile jf = new JarFile(multirelease, true, ZipFile.OPEN_READ, value)) {
 129                 Assert.assertEquals(value, jf.getVersion());
 130             }
 131         }
 132 
 133         // regular, unversioned, jar
 134         for (Release value : values) {
 135             try (JarFile jf = new JarFile(unversioned, true, ZipFile.OPEN_READ, value)) {
 136                 Assert.assertEquals(Release.BASE, jf.getVersion());
 137             }
 138         }
 139 
 140         // assure that we have a Release object corresponding to the actual runtime version
 141         String version = "VERSION_" + MAJOR_VERSION;
 142         boolean runtimeVersionExists = false;
 143         for (Release value : values) {
 144             if (version.equals(value.name())) runtimeVersionExists = true;
 145         }
 146         Assert.assertTrue(runtimeVersionExists);
 147     }
 148 
 149     @Test
 150     public void testAliasing() throws Exception {
 151         for (Release value : values) {
 152             System.err.println("test aliasing for Release " + value);
 153             String name = value.name();
 154             String prefix;
 155             if (name.equals("BASE")) {
 156                 prefix = "";
 157             } else if (name.equals("RUNTIME")) {
 158                 prefix = "META-INF/versions/" + MAJOR_VERSION + "/";
 159             } else {
 160                 prefix = "META-INF/versions/" + name.substring(8) + "/";
 161             }
 162             // test both multi-release jars
 163             readAndCompare(multirelease, value, "README", prefix + "README");
 164             readAndCompare(multirelease, value, "version/Version.class", prefix + "version/Version.class");
 165             // and signed multi-release jars
 166             readAndCompare(signedmultirelease, value, "README", prefix + "README");
 167             readAndCompare(signedmultirelease, value, "version/Version.class", prefix + "version/Version.class");
 168         }
 169     }
 170 
 171     private void readAndCompare(File jar, Release version, String name, String realName) throws Exception {
 172         byte[] baseBytes;
 173         byte[] versionedBytes;
 174         try (JarFile jf = new JarFile(jar, true, ZipFile.OPEN_READ, Release.BASE)) {
 175             ZipEntry ze = jf.getEntry(realName);
 176             try (InputStream is = jf.getInputStream(ze)) {
 177                 baseBytes = is.readAllBytes();
 178             }
 179         }
 180         assert baseBytes.length > 0;
 181 
 182         try (JarFile jf = new JarFile(jar, true, ZipFile.OPEN_READ, version)) {
 183             ZipEntry ze = jf.getEntry(name);
 184             try (InputStream is = jf.getInputStream(ze)) {
 185                 versionedBytes = is.readAllBytes();
 186             }
 187         }
 188         assert versionedBytes.length > 0;
 189 
 190         Assert.assertTrue(Arrays.equals(baseBytes, versionedBytes));
 191     }
 192 
 193     @Test
 194     public void testNames() throws Exception {
 195         String rname = "version/Version.class";
 196         String vname = "META-INF/versions/9/version/Version.class";
 197         ZipEntry ze1;
 198         ZipEntry ze2;
 199         try (JarFile jf = new JarFile(multirelease)) {
 200             ze1 = jf.getEntry(vname);
 201         }
 202         Assert.assertEquals(ze1.getName(), vname);
 203         try (JarFile jf = new JarFile(multirelease, true, ZipFile.OPEN_READ, Release.VERSION_9)) {
 204             ze2 = jf.getEntry(rname);
 205         }
 206         Assert.assertEquals(ze2.getName(), rname);
 207         Assert.assertNotEquals(ze1.getName(), ze2.getName());
 208     }
 209 }