1 /*
2  * Copyright (c) 2010, 2017, 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 6856415 8154212 8154470
27  * @summary Miscellaneous tests, Exceptions
28  * @modules jdk.compiler
29  *          jdk.zipfs
30  * @compile -XDignore.symbol.file MiscTests.java
31  * @run main MiscTests
32  */
33 
34 
35 import java.io.File;
36 import java.io.IOException;
37 import java.util.ArrayList;
38 import java.util.HashMap;
39 import java.util.List;
40 import java.util.Map;
41 
42 public class MiscTests extends TestHelper {
43 
44     /**
45      * Test with class path set on the command line via -Djava.class.path
46      */
testWithClassPathSetViaProperty()47     static void testWithClassPathSetViaProperty() throws IOException {
48         final String mainClass = "Foo";
49 
50         File source = new File(mainClass + ".java");
51 
52         List<String> scratch = new ArrayList<>();
53         scratch.add("public class Foo {");
54         scratch.add("public static void main(String... args) {");
55         scratch.add("}");
56         scratch.add("}");
57         createFile(source, scratch);
58 
59         compile(mainClass + ".java");
60 
61         String dir = new File(mainClass + ".class").getAbsoluteFile().getParent();
62         TestResult tr = doExec(javaCmd, "-Djava.class.path=" + dir, mainClass);
63         for (String s : tr.testOutput) {
64             System.out.println(s);
65         }
66     }
67 
68     /**
69      * 6856415: Checks to ensure that proper exceptions are thrown by java
70      */
test6856415()71     static void test6856415() throws IOException {
72 
73         final String mainClass = "Foo6856415";
74 
75         List<String> scratch = new ArrayList<>();
76         scratch.add("public class Foo6856415 {");
77         scratch.add("public static void main(String... args) {");
78         scratch.add("java.security.Provider p = new sun.security.pkcs11.SunPKCS11();");
79         scratch.add("java.security.Security.insertProviderAt(p, 1);");
80         scratch.add("}");
81         scratch.add("}");
82         createFile(new File(mainClass + ".java"), scratch);
83 
84         compile(mainClass + ".java",
85                 "--add-modules=jdk.crypto.cryptoki",
86                 "--add-exports=jdk.crypto.cryptoki/sun.security.pkcs11=ALL-UNNAMED");
87 
88         File testJar = new File("Foo.jar");
89         testJar.delete();
90         String jarArgs[] = {
91             (debug) ? "cvfe" : "cfe",
92             testJar.getAbsolutePath(),
93             mainClass,
94             mainClass + ".class"
95         };
96         createJar(jarArgs);
97 
98         TestResult tr = doExec(javaCmd,
99                 "-Djava.security.manager", "-jar", testJar.getName(), "foo.bak");
100         if (!tr.contains("java.security.AccessControlException:" +
101                 " access denied (\"java.lang.RuntimePermission\"" +
102                 " \"accessClassInPackage.sun.security.pkcs11\")")) {
103             System.out.println(tr);
104         }
105     }
106 
testJLDEnv()107     static void testJLDEnv() {
108         final Map<String, String> envToSet = new HashMap<>();
109         envToSet.put("_JAVA_LAUNCHER_DEBUG", "true");
110         for (String cmd : new String[] { javaCmd, javacCmd }) {
111             TestResult tr = doExec(envToSet, cmd, "-version");
112             tr.checkPositive();
113             String javargs = cmd.equals(javacCmd) ? "on" : "off";
114             String progname = cmd.equals(javacCmd) ? "javac" : "java";
115             if (!tr.isOK()
116                 || !tr.matches("\\s*debug:on$")
117                 || !tr.matches("\\s*javargs:" + javargs + "$")
118                 || !tr.matches("\\s*program name:" + progname + "$")) {
119                 System.out.println(tr);
120             }
121         }
122     }
123 
main(String... args)124     public static void main(String... args) throws IOException {
125         testWithClassPathSetViaProperty();
126         test6856415();
127         testJLDEnv();
128         if (testExitValue != 0) {
129             throw new Error(testExitValue + " tests failed");
130         }
131     }
132 }
133