jdk/test/java/util/jar/JarFile/MultiReleaseJarSecurity.java
changeset 37925 3560b89fb532
parent 37924 a78497edf9fb
parent 37916 627154540b60
child 37934 4c38661774bf
equal deleted inserted replaced
37924:a78497edf9fb 37925:3560b89fb532
     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 potential security related issues
       
    28  * @library /lib/testlibrary/java/util/jar
       
    29  * @build Compiler JarBuilder CreateMultiReleaseTestJars
       
    30  * @run testng MultiReleaseJarSecurity
       
    31  */
       
    32 
       
    33 import java.io.File;
       
    34 import java.io.IOException;
       
    35 import java.io.InputStream;
       
    36 import java.nio.file.Files;
       
    37 import java.security.CodeSigner;
       
    38 import java.security.cert.Certificate;
       
    39 import java.util.Arrays;
       
    40 import java.util.jar.JarEntry;
       
    41 import java.util.jar.JarFile;
       
    42 import java.util.zip.ZipFile;
       
    43 import jdk.Version;
       
    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 public class MultiReleaseJarSecurity {
       
    51 
       
    52     static final int MAJOR_VERSION = Version.current().major();
       
    53 
       
    54     String userdir = System.getProperty("user.dir",".");
       
    55     File multirelease = new File(userdir, "multi-release.jar");
       
    56     File signedmultirelease = new File(userdir, "signed-multi-release.jar");
       
    57 
       
    58     @BeforeClass
       
    59     public void initialize() throws Exception {
       
    60         CreateMultiReleaseTestJars creator =  new CreateMultiReleaseTestJars();
       
    61         creator.compileEntries();
       
    62         creator.buildMultiReleaseJar();
       
    63         creator.buildSignedMultiReleaseJar();
       
    64     }
       
    65 
       
    66     @AfterClass
       
    67     public void close() throws IOException {
       
    68         Files.delete(multirelease.toPath());
       
    69         Files.delete(signedmultirelease.toPath());
       
    70     }
       
    71 
       
    72     @Test
       
    73     public void testCertsAndSigners() throws IOException {
       
    74         try (JarFile jf = new JarFile(signedmultirelease, true, ZipFile.OPEN_READ, JarFile.Release.RUNTIME)) {
       
    75             CertsAndSigners vcas = new CertsAndSigners(jf, jf.getJarEntry("version/Version.class"));
       
    76             CertsAndSigners rcas = new CertsAndSigners(jf, jf.getJarEntry("META-INF/versions/" + MAJOR_VERSION + "/version/Version.class"));
       
    77             Assert.assertTrue(Arrays.equals(rcas.getCertificates(), vcas.getCertificates()));
       
    78             Assert.assertTrue(Arrays.equals(rcas.getCodeSigners(), vcas.getCodeSigners()));
       
    79         }
       
    80     }
       
    81 
       
    82     private static class CertsAndSigners {
       
    83         final private JarFile jf;
       
    84         final private JarEntry je;
       
    85         private boolean readComplete;
       
    86 
       
    87         CertsAndSigners(JarFile jf, JarEntry je) {
       
    88             this.jf = jf;
       
    89             this.je = je;
       
    90         }
       
    91 
       
    92         Certificate[] getCertificates() throws IOException {
       
    93             readEntry();
       
    94             return je.getCertificates();
       
    95         }
       
    96 
       
    97         CodeSigner[] getCodeSigners() throws IOException {
       
    98             readEntry();
       
    99             return je.getCodeSigners();
       
   100         }
       
   101 
       
   102         private void readEntry() throws IOException {
       
   103             if (!readComplete) {
       
   104                 try (InputStream is = jf.getInputStream(je)) {
       
   105                     is.readAllBytes();
       
   106                 }
       
   107                 readComplete = true;
       
   108             }
       
   109         }
       
   110     }
       
   111 }