Path: blob/master/test/jdk/sun/security/tools/jarsigner/DiffEnd.java
41152 views
/*1* Copyright (c) 2010, 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*/2223/*24* @test25* @bug 6948909 821737526* @summary Jarsigner removes MANIFEST.MF info for badly packages jar's27* @library /test/lib28*/29/*30* See also InsufficientSectionDelimiter.java for similar tests including cases31* without or with different line breaks.32*/3334import jdk.test.lib.Asserts;35import jdk.test.lib.SecurityTools;36import jdk.test.lib.process.OutputAnalyzer;3738import java.io.FileOutputStream;39import java.nio.charset.StandardCharsets;40import java.nio.file.Files;41import java.nio.file.Path;42import java.util.jar.Attributes;43import java.util.jar.JarFile;44import java.util.zip.ZipEntry;45import java.util.zip.ZipOutputStream;4647public class DiffEnd {4849static void check() throws Exception {50String ksArgs = "-keystore " + Path.of(System.getProperty("test.src"))51.resolve("JarSigning.keystore") + " -storepass bbbbbb";5253SecurityTools.jarsigner(ksArgs + " -digestalg SHA1 "54+ "-signedjar diffend.signed.jar diffend.jar c")55.shouldHaveExitValue(0);56SecurityTools.jarsigner(" -verify " + ksArgs + " -verbose "57+ "diffend.signed.jar c")58.stdoutShouldMatch("^smk .* 1$").shouldHaveExitValue(0);5960try (JarFile jf = new JarFile("diffend.signed.jar")) {61Asserts.assertTrue(jf.getManifest().getMainAttributes()62.containsKey(new Attributes.Name("Today")));63}64}6566public static void main(String[] args) throws Exception {67// A MANIFEST.MF using \n as newlines and no double newlines at the end68byte[] manifest = ("Manifest-Version: 1.0\n"69+ "Created-By: 1.7.0-internal (Sun Microsystems Inc.)\n"70+ "Today: Monday\n").getBytes(StandardCharsets.UTF_8);7172// Without the fake .RSA file, to trigger the if (wasSigned) else block73try (FileOutputStream fos = new FileOutputStream("diffend.jar");74ZipOutputStream zos = new ZipOutputStream(fos)) {75zos.putNextEntry(new ZipEntry(JarFile.MANIFEST_NAME));76zos.write(manifest);77zos.putNextEntry(new ZipEntry("1"));78zos.write(new byte[10]);79}80check();8182// With the fake .RSA file, to trigger the if (wasSigned) block83try (FileOutputStream fos = new FileOutputStream("diffend.jar");84ZipOutputStream zos = new ZipOutputStream(fos)) {85zos.putNextEntry(new ZipEntry(JarFile.MANIFEST_NAME));86zos.write(manifest);87zos.putNextEntry(new ZipEntry("META-INF/x.RSA")); // fake .RSA88zos.putNextEntry(new ZipEntry("1"));89zos.write(new byte[10]);90}91check();92}93}949596