1 /*
   2  * Copyright (c) 2009, 2018, 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 6584033
  27  * @summary Stackoverflow error with security manager, signed jars and debug.
  28  * @library /test/lib
  29  * @build TimeZoneDatePermissionCheck
  30  * @run main TimeZoneDatePermissionCheckRun
  31  */
  32 
  33 import java.io.File;
  34 import java.util.List;
  35 
  36 import jdk.test.lib.JDKToolLauncher;
  37 import jdk.test.lib.Utils;
  38 import jdk.test.lib.process.ProcessTools;
  39 
  40 public class TimeZoneDatePermissionCheckRun {
  41     private static String storePath = Utils.TEST_CLASSES + File.separator
  42             + "timezonedatetest.store";
  43     private static String jarPath = Utils.TEST_CLASSES + File.separator
  44             + "timezonedatetest.jar";
  45 
  46     public static void main(String[] args) throws Throwable {
  47         try {
  48             //create a test keystore and dummy cert. Note that we use the COMPILEJAVA
  49             //as this test is a TimeZone test, it doesn't test keytool
  50             runJavaCmd("keytool",
  51                     List.of("-genkeypair", "-alias", "testcert", "-keystore",
  52                             storePath, "-storepass", "testpass", "-validity",
  53                             "360", "-keyalg", "rsa", "-dname",
  54                             "cn=Mark Wildebeest, ou=FreeSoft, o=Red Hat, c=NL",
  55                             "-keypass", "testpass"));
  56 
  57             //create a jar file to sign with the test class in it.
  58             runJavaCmd("jar", List.of("cf", jarPath, "-C", Utils.TEST_CLASSES,
  59                     "TimeZoneDatePermissionCheck.class"));
  60 
  61             //sign it
  62             runJavaCmd("jarsigner",
  63                     List.of("-keystore", storePath, "-storepass", "testpass",
  64                             jarPath, "testcert"));
  65 
  66             //run it with the security manager on, plus accesscontroller debugging
  67             //will go into infinite recursion trying to get enough permissions for
  68             //printing Date of failing certificate unless fix is applied.
  69             JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("java");
  70             launcher.addToolArg("-Djava.security.manager")
  71                     .addToolArg("-Djava.security.debug=access,failure,policy")
  72                     .addToolArg("-cp")
  73                     .addToolArg(jarPath)
  74                     .addToolArg("TimeZoneDatePermissionCheck");
  75 
  76             int exitCode = ProcessTools.executeCommand(launcher.getCommand())
  77                     .getExitValue();
  78             if (exitCode != 0) {
  79                 throw new RuntimeException("Unexpected exit code: " + exitCode);
  80             }
  81         } finally {
  82             //clean up the test files
  83             File storeFile = new File(storePath);
  84             if (storeFile.exists()) {
  85                 storeFile.delete();
  86             }
  87             File jarFile = new File(jarPath);
  88             if (jarFile.exists()) {
  89                 jarFile.delete();
  90             }
  91         }
  92     }
  93 
  94     private static void runJavaCmd(String cmd, List<String> javaParam)
  95             throws Throwable{
  96         JDKToolLauncher launcher = JDKToolLauncher.create(cmd);
  97         for (String para: javaParam) {
  98             launcher.addToolArg(para);
  99         }
 100 
 101         System.out.println(launcher.getCommand());
 102         int exitCode = ProcessTools.executeCommand(launcher.getCommand())
 103                 .getExitValue();
 104         if (exitCode != 0) {
 105             throw new RuntimeException("Unexpected exit code: " + exitCode);
 106         }
 107     }
 108 }