1 /*
   2  * Copyright (c) 2015, 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
  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 
  42 import static sun.misc.Version.jdkMinorVersion;
  43 
  44 import org.testng.Assert;
  45 import org.testng.annotations.AfterClass;
  46 import org.testng.annotations.BeforeClass;
  47 import org.testng.annotations.Test;
  48 
  49 
  50 public class MultiReleaseJarAPI {
  51     String userdir = System.getProperty("user.dir",".");
  52     File unversioned = new File(userdir, "unversioned.jar");
  53     File multirelease = new File(userdir, "multi-release.jar");
  54     File signedmultirelease = new File(userdir, "signed-multi-release.jar");
  55     int base_version;
  56 
  57     @BeforeClass
  58     public void initialize() throws Exception {
  59         CreateMultiReleaseTestJars creator =  new CreateMultiReleaseTestJars();
  60         creator.compileEntries();
  61         creator.buildUnversionedJar();
  62         creator.buildMultiReleaseJar();
  63         creator.buildSignedMultiReleaseJar();
  64         JarFile jf = new JarFile(multirelease).setVersioned(0);
  65         base_version = jf.getVersioned();
  66         jf.close();
  67     }
  68 
  69     @AfterClass
  70     public void close() throws IOException {
  71         Files.delete(unversioned.toPath());
  72         Files.delete(multirelease.toPath());
  73         Files.delete(signedmultirelease.toPath());
  74     }
  75 
  76     @Test
  77     public void isMultiReleaseJar() throws Exception {
  78         try (JarFile jf = new JarFile(unversioned)) {
  79             Assert.assertFalse(jf.isMultiRelease());
  80         }
  81 
  82         try (JarFile jf = new JarFile(multirelease)) {
  83             Assert.assertTrue(jf.isMultiRelease());
  84         }
  85     }
  86 
  87     @Test
  88     public void testVersioning() throws Exception {
  89         try (JarFile jf = new JarFile(multirelease)) {
  90             Assert.assertEquals(base_version, jf.getVersioned());
  91             jf.setVersioned(base_version);
  92             Assert.assertEquals(base_version, jf.getVersioned());
  93             jf.setVersioned(-1);
  94             Assert.assertEquals(base_version, jf.getVersioned());
  95             jf.setVersioned(0);
  96             Assert.assertEquals(base_version, jf.getVersioned());
  97             jf.setVersioned(1);
  98             Assert.assertEquals(base_version, jf.getVersioned());
  99             jf.setVersioned(8);
 100             Assert.assertEquals(base_version, jf.getVersioned());
 101             jf.setVersioned(9);
 102             Assert.assertEquals(9, jf.getVersioned());
 103             jf.setVersioned(10);
 104             Assert.assertEquals(10, jf.getVersioned());
 105             jf.setVersioned(25);
 106             Assert.assertEquals(25, jf.getVersioned());
 107             jf.setRuntimeVersioned();
 108             Assert.assertEquals(jdkMinorVersion(), jf.getVersioned());
 109         }
 110 
 111         try (JarFile jf = new JarFile(unversioned)) {
 112             jf.setRuntimeVersioned();
 113             Assert.assertEquals(base_version, jf.getVersioned());
 114             jf.setVersioned(10);
 115             Assert.assertEquals(base_version, jf.getVersioned());
 116         }
 117     }
 118 
 119     @Test
 120     public void testAliasing() throws Exception {
 121         JarFile jf = null;
 122         try {
 123             jf = new JarFile(multirelease);
 124             jf = readAndCompare(jf, base_version, "README", "README");
 125             jf = readAndCompare(jf, 0, "README", "README");
 126             jf = readAndCompare(jf, 8, "README", "README");
 127             jf = readAndCompare(jf, 9, "README", "META-INF/versions/9/README");
 128             jf = readAndCompare(jf, 10, "README", "META-INF/versions/10/README");
 129             jf = readAndCompare(jf, 100, "README", "META-INF/versions/10/README");
 130             jf.close();
 131             jf = new JarFile(multirelease);
 132             jf = readAndCompare(jf, base_version, "version/Version.class", "version/Version.class");
 133             jf = readAndCompare(jf, 0, "version/Version.class", "version/Version.class");
 134             jf = readAndCompare(jf, 9, "version/Version.class", "META-INF/versions/9/version/Version.class");
 135             jf = readAndCompare(jf, 10, "version/Version.class", "META-INF/versions/10/version/Version.class");
 136             jf = readAndCompare(jf, 100, "version/Version.class", "META-INF/versions/10/version/Version.class");
 137         } finally {
 138             jf.close();
 139         }
 140 
 141         // now do exactly the same thing with a signed multi-release jar
 142         try {
 143             jf = new JarFile(signedmultirelease, true);
 144             jf = readAndCompare(jf, base_version, "README", "README");
 145             jf = readAndCompare(jf, 0, "README", "README");
 146             jf = readAndCompare(jf, 9, "README", "META-INF/versions/9/README");
 147             jf = readAndCompare(jf, 10, "README", "META-INF/versions/10/README");
 148             jf = readAndCompare(jf, 100, "README", "META-INF/versions/10/README");
 149             jf.close();
 150             jf = new JarFile(signedmultirelease);
 151             jf = readAndCompare(jf, base_version, "version/Version.class", "version/Version.class");
 152             jf = readAndCompare(jf, 0, "version/Version.class", "version/Version.class");
 153             jf = readAndCompare(jf, 9, "version/Version.class", "META-INF/versions/9/version/Version.class");
 154             jf = readAndCompare(jf, 10, "version/Version.class", "META-INF/versions/10/version/Version.class");
 155             jf = readAndCompare(jf, 100, "version/Version.class", "META-INF/versions/10/version/Version.class");
 156         } finally {
 157             jf.close();
 158         }
 159     }
 160 
 161     private JarFile readAndCompare(JarFile jf, int version, String name, String realName) throws Exception {
 162         byte[] rootBytes;
 163         byte[] versionedBytes;
 164         ZipEntry ze = jf.getEntry(realName);
 165         try (InputStream is = jf.getInputStream(ze)) {
 166             rootBytes = is.readAllBytes();
 167         }
 168         assert rootBytes.length > 0;
 169         try {
 170             jf.setVersioned(version);
 171         } catch (IllegalStateException x) {
 172             String jarName = jf.getName();
 173             jf.close();
 174             jf = new JarFile(jarName).setVersioned(version);
 175         }
 176         ze = jf.getEntry(name);
 177         try (InputStream is = jf.getInputStream(ze)) {
 178             versionedBytes = is.readAllBytes();
 179         }
 180         Assert.assertTrue(Arrays.equals(rootBytes, versionedBytes));
 181         return jf;
 182     }
 183 
 184     @Test
 185     public void testNames() throws Exception {
 186         String rname = "version/Version.class";
 187         String vname = "META-INF/versions/10/version/Version.class";
 188         ZipEntry ze1;
 189         ZipEntry ze2;
 190         try (JarFile jf = new JarFile(multirelease)) {
 191             ze1 = jf.getEntry(vname);
 192         }
 193         Assert.assertEquals(vname, ze1.getName());
 194         try (JarFile jf = new JarFile(multirelease).setVersioned(10)) {
 195             ze2 = jf.getEntry(rname);
 196         }
 197         Assert.assertEquals(rname, ze2.getName());
 198         Assert.assertNotEquals(ze1.getName(), ze2.getName());
 199     }
 200 
 201     @Test
 202     public void testConfigurationLock() throws IOException {
 203         boolean iseCaught = false;
 204         try (JarFile jf = new JarFile(multirelease)) {
 205             // reconfigure as much as needed before reading entry
 206             jf.setRuntimeVersioned();
 207             jf.setVersioned(0);
 208             jf.getEntry("README");
 209             try {
 210                 // but not after reading entry
 211                 jf.setRuntimeVersioned();
 212             } catch (IllegalStateException x) {
 213                 iseCaught = true;
 214             }
 215             // can keep reading entries if desired
 216             jf.getEntry("README");
 217         }
 218         Assert.assertTrue(iseCaught);
 219     }
 220 }