Path: blob/master/test/jdk/sun/security/tools/jarsigner/MainAttributesConfused.java
41152 views
/*1* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.nio.file.Files;24import java.nio.file.Path;25import java.util.jar.JarFile;26import java.util.jar.Manifest;27import java.util.jar.Attributes;28import java.util.jar.Attributes.Name;29import sun.security.util.ManifestDigester;30import jdk.test.lib.util.JarUtils;31import jdk.test.lib.SecurityTools;32import org.testng.annotations.Test;33import org.testng.annotations.BeforeClass;3435import static java.nio.charset.StandardCharsets.UTF_8;36import static org.testng.Assert.*;3738/**39* @test40* @bug 821737541* @modules java.base/sun.security.util42* @library /test/lib /lib/testlibrary43* @run testng MainAttributesConfused44* @summary Check that manifest individual section "Manifest-Main-Attributes"45* does not interfere and is not confused with ManifestDigester internals.46*47* See also48* jdk/test/jdk/sun/security/util/ManifestDigester/ManifestMainAttributes.java49* for much more detailed api level tests50*/51public class MainAttributesConfused {5253static final String KEYSTORE_FILENAME = "test.jks";54static final String MAIN_ATTRIBUTES_MARKER = null;5556@BeforeClass57void prepareKeyStore() throws Exception {58SecurityTools.keytool("-genkeypair -keyalg EC -keystore "59+ KEYSTORE_FILENAME + " -storepass changeit -keypass changeit"60+ " -alias a -dname CN=X").shouldHaveExitValue(0);61}6263void testAddManifestSection(String sectionName) throws Exception {64// create a signed jar65Manifest manifest = new Manifest();66manifest.getMainAttributes().put(Name.MANIFEST_VERSION, "1.0");67String testFile = "test-" + sectionName;68Files.write(Path.of(testFile), testFile.getBytes(UTF_8));69String jarFilename = sectionName + ".jar";70JarUtils.createJarFile(Path.of(jarFilename), manifest,71Path.of("."), Path.of(testFile));72SecurityTools.jarsigner("-keystore " + KEYSTORE_FILENAME +73" -storepass changeit -verbose -debug " + jarFilename + " a")74.shouldHaveExitValue(0);7576// get the manifest of the signed jar with the signature digests, add77// a new individual section, and write it back78try (JarFile jar = new JarFile(jarFilename)) {79manifest = jar.getManifest();80}81Attributes attrs = sectionName == MAIN_ATTRIBUTES_MARKER82? manifest.getMainAttributes()83: manifest.getEntries().computeIfAbsent(sectionName,84n -> new Attributes());85attrs.put(new Name("Some-Key"), "Some-Value");86String jarFilenameAttrs = sectionName + "-attrs.jar";87JarUtils.updateManifest(jarFilename, jarFilenameAttrs, manifest);8889// having just added another manifest entry (individual section) not90// modifying existing digests or main attributes should not invalidate91// the existing signature.92SecurityTools.jarsigner("-verify -keystore " + KEYSTORE_FILENAME +93" -storepass changeit -debug -verbose " + jarFilenameAttrs +94" a").shouldHaveExitValue(0);95}9697@Test98public void testAddOtherThanManifestMainAttributes() throws Exception {99// any value but "Manifest-Main-Attributes", even lower case works100testAddManifestSection("manifest-main-attributes");101}102103@Test104public void testAddMainAttributesHeader() throws Exception {105// adding or changing existing attributes of the main section, however,106// will invalidate the signature107assertThrows(() -> testAddManifestSection(MAIN_ATTRIBUTES_MARKER));108}109110@Test111public void testAddManifestMainAttributesSection() throws Exception {112testAddManifestSection(ManifestDigester.MF_MAIN_ATTRS);113}114115}116117118