test/jdk/sun/security/tools/jarsigner/EmptyJar.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.Path;
       
    25 import java.util.jar.Manifest;
       
    26 import java.util.jar.Attributes.Name;
       
    27 
       
    28 import jdk.test.lib.util.JarUtils;
       
    29 import jdk.test.lib.SecurityTools;
       
    30 import org.testng.annotations.Test;
       
    31 import org.testng.annotations.BeforeClass;
       
    32 
       
    33 import static java.nio.charset.StandardCharsets.UTF_8;
       
    34 import static org.testng.Assert.*;
       
    35 
       
    36 /**
       
    37  * @test
       
    38  * @bug 8217375
       
    39  * @modules java.base/sun.security.util
       
    40  * @library /test/lib /lib/testlibrary
       
    41  * @run testng EmptyJar
       
    42  * @summary Checks that signing an empty jar file does not result in an NPE or
       
    43  * other error condition.
       
    44  */
       
    45 public class EmptyJar {
       
    46 
       
    47     static final String KEYSTORE_FILENAME = "test.jks";
       
    48 
       
    49     @BeforeClass
       
    50     public void prepareKeyStore() throws Exception {
       
    51         SecurityTools.keytool("-genkeypair -keyalg EC -keystore "
       
    52                 + KEYSTORE_FILENAME + " -storepass changeit -keypass changeit"
       
    53                 + " -alias a -dname CN=A").shouldHaveExitValue(0);
       
    54     }
       
    55 
       
    56     @Test
       
    57     public void test() throws Exception {
       
    58         String jarFilename = "test.jar";
       
    59         JarUtils.createJarFile(Path.of(jarFilename), (Manifest) null,
       
    60                 Path.of("."));
       
    61         SecurityTools.jarsigner("-keystore " + KEYSTORE_FILENAME +
       
    62                 " -storepass changeit -verbose -debug " + jarFilename + " a")
       
    63                 .shouldHaveExitValue(0);
       
    64 
       
    65         // verify that jarsigner has added a default manifest
       
    66         byte[] mfBytes = Utils.readJarManifestBytes(jarFilename);
       
    67         Utils.echoManifest(mfBytes, "manifest");
       
    68         assertTrue(new String(mfBytes, UTF_8).startsWith(
       
    69                 Name.MANIFEST_VERSION + ": 1.0\r\nCreated-By: "));
       
    70     }
       
    71 
       
    72 }