1/*
2 * Copyright (c) 2013, 2014, 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 8027991
27 * @summary InputStream should be closed in sun.security.tools.jarsigner.Main
28 * @modules java.base/sun.security.tools.keytool
29 *          jdk.jartool/sun.security.tools.jarsigner
30 * @run main/othervm CertChainUnclosed
31 */
32
33import java.nio.file.Files;
34import java.nio.file.Paths;
35import java.security.AccessController;
36import java.security.PrivilegedAction;
37import java.util.Locale;
38
39public class CertChainUnclosed {
40
41    public static void main(String[] args) throws Exception {
42        String os = AccessController.doPrivileged(
43                    (PrivilegedAction<String>)() -> System.getProperty("os.name"));
44        if (!os.toUpperCase(Locale.US).contains("WINDOWS")) {
45            System.out.println("Not Windows. Skip test.");
46            return;
47        }
48
49        kt("-genkeypair -alias a -dname CN=A");
50        kt("-exportcert -file a.crt -alias a");
51        Files.copy(Paths.get(System.getProperty("test.src"), "AlgOptions.jar"),
52                Paths.get("test.jar"));
53        sun.security.tools.jarsigner.Main.main(
54                "-storepass changeit -keystore jks -certchain a.crt test.jar a"
55                        .split(" "));
56
57        // On Windows, if the file is still opened (or not if GC was
58        // performed) and the next line would fail
59        Files.delete(Paths.get("a.crt"));
60    }
61
62    static void kt(String args) throws Exception {
63        sun.security.tools.keytool.Main.main(
64            ("-keystore jks -storepass changeit -keypass changeit -keyalg rsa "
65                + args).split(" "));
66    }
67}
68