test/jdk/sun/security/tools/jarsigner/MainAttributesConfused.java
changeset 57488 94691d8e746f
equal deleted inserted replaced
57487:643978a35f6e 57488:94691d8e746f
       
     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.nio.file.Files;
       
    25 import java.nio.file.Path;
       
    26 import java.util.jar.JarFile;
       
    27 import java.util.jar.Manifest;
       
    28 import java.util.jar.Attributes;
       
    29 import java.util.jar.Attributes.Name;
       
    30 import sun.security.util.ManifestDigester;
       
    31 import jdk.test.lib.util.JarUtils;
       
    32 import jdk.test.lib.SecurityTools;
       
    33 import org.testng.annotations.Test;
       
    34 import org.testng.annotations.BeforeClass;
       
    35 
       
    36 import static java.nio.charset.StandardCharsets.UTF_8;
       
    37 import static org.testng.Assert.*;
       
    38 
       
    39 /**
       
    40  * @test
       
    41  * @bug 8217375
       
    42  * @modules java.base/sun.security.util
       
    43  * @library /test/lib /lib/testlibrary
       
    44  * @run testng MainAttributesConfused
       
    45  * @summary Check that manifest individual section "Manifest-Main-Attributes"
       
    46  * does not interfere and is not confused with ManifestDigester internals.
       
    47  *
       
    48  * See also
       
    49  * jdk/test/jdk/sun/security/util/ManifestDigester/ManifestMainAttributes.java
       
    50  * for much more detailed api level tests
       
    51  */
       
    52 public class MainAttributesConfused {
       
    53 
       
    54     static final String KEYSTORE_FILENAME = "test.jks";
       
    55     static final String MAIN_ATTRIBUTES_MARKER = null;
       
    56 
       
    57     @BeforeClass
       
    58     void prepareKeyStore() throws Exception {
       
    59         SecurityTools.keytool("-genkeypair -keyalg EC -keystore "
       
    60                 + KEYSTORE_FILENAME + " -storepass changeit -keypass changeit"
       
    61                 + " -alias a -dname CN=X").shouldHaveExitValue(0);
       
    62     }
       
    63 
       
    64     void testAddManifestSection(String sectionName) throws Exception {
       
    65         // create a signed jar
       
    66         Manifest manifest = new Manifest();
       
    67         manifest.getMainAttributes().put(Name.MANIFEST_VERSION, "1.0");
       
    68         String testFile = "test-" + sectionName;
       
    69         Files.write(Path.of(testFile), testFile.getBytes(UTF_8));
       
    70         String jarFilename = sectionName + ".jar";
       
    71         JarUtils.createJarFile(Path.of(jarFilename), manifest,
       
    72                 Path.of("."), Path.of(testFile));
       
    73         SecurityTools.jarsigner("-keystore " + KEYSTORE_FILENAME +
       
    74                 " -storepass changeit -verbose -debug " + jarFilename + " a")
       
    75                 .shouldHaveExitValue(0);
       
    76 
       
    77         // get the manifest of the signed jar with the signature digests, add
       
    78         // a new individual section, and write it back
       
    79         try (JarFile jar = new JarFile(jarFilename)) {
       
    80             manifest = jar.getManifest();
       
    81         }
       
    82         Attributes attrs = sectionName == MAIN_ATTRIBUTES_MARKER
       
    83                 ? manifest.getMainAttributes()
       
    84                 : manifest.getEntries().computeIfAbsent(sectionName,
       
    85                         n -> new Attributes());
       
    86         attrs.put(new Name("Some-Key"), "Some-Value");
       
    87         String jarFilenameAttrs = sectionName + "-attrs.jar";
       
    88         JarUtils.updateManifest(jarFilename, jarFilenameAttrs, manifest);
       
    89 
       
    90         // having just added another manifest entry (individual section) not
       
    91         // modifying existing digests or main attributes should not invalidate
       
    92         // the existing signature.
       
    93         SecurityTools.jarsigner("-verify -keystore " + KEYSTORE_FILENAME +
       
    94                 " -storepass changeit -debug -verbose " + jarFilenameAttrs +
       
    95                 " a").shouldHaveExitValue(0);
       
    96     }
       
    97 
       
    98     @Test
       
    99     public void testAddOtherThanManifestMainAttributes() throws Exception {
       
   100         // any value but "Manifest-Main-Attributes", even lower case works
       
   101         testAddManifestSection("manifest-main-attributes");
       
   102     }
       
   103 
       
   104     @Test
       
   105     public void testAddMainAttributesHeader() throws Exception {
       
   106         // adding or changing existing attributes of the main section, however,
       
   107         // will invalidate the signature
       
   108         assertThrows(() -> testAddManifestSection(MAIN_ATTRIBUTES_MARKER));
       
   109     }
       
   110 
       
   111     @Test
       
   112     public void testAddManifestMainAttributesSection() throws Exception {
       
   113         testAddManifestSection(ManifestDigester.MF_MAIN_ATTRS);
       
   114     }
       
   115 
       
   116 }