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