1
+ /*
2
+ * Copyright (c) 2022, 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 jdk .test .lib .process .OutputAnalyzer ;
25
+ import jdk .test .lib .process .ProcessTools ;
26
+
27
+ import java .io .IOException ;
28
+ import java .io .UncheckedIOException ;
29
+ import java .nio .file .*;
30
+
31
+ import java .security .Security ;
32
+ import java .util .Arrays ;
33
+ import java .util .Optional ;
34
+
35
+ /*
36
+ * @test
37
+ * @summary Throw error if default java.security file is missing
38
+ * @bug 8155246
39
+ * @library /test/lib
40
+ * @run main ConfigFileTest
41
+ */
42
+ public class ConfigFileTest {
43
+
44
+ public static void main (String [] args ) throws Exception {
45
+ Path copyJdkDir = Path .of ("./jdk-8155246-tmpdir" );
46
+ Path copiedJava = Optional .of (
47
+ Path .of (copyJdkDir .toString (), "bin" , "java" ))
48
+ .orElseThrow (() -> new RuntimeException ("Unable to locate new JDK" )
49
+ );
50
+
51
+ if (args .length == 1 ) {
52
+ // set up is complete. Run code to exercise loading of java.security
53
+ System .out .println (Arrays .toString (Security .getProviders ()));
54
+ } else {
55
+ Files .createDirectory (copyJdkDir );
56
+ Path jdkTestDir = Path .of (Optional .of (System .getProperty ("test.jdk" ))
57
+ .orElseThrow (() -> new RuntimeException ("Couldn't load JDK Test Dir" ))
58
+ );
59
+
60
+ copyJDKMinusJavaSecurity (jdkTestDir , copyJdkDir );
61
+ String extraPropsFile = Path .of (System .getProperty ("test.src" ), "override.props" ).toString ();
62
+
63
+ // exercise some debug flags while we're here
64
+ // launch JDK without java.security file being present or specified
65
+ exerciseSecurity (copiedJava .toString (), "-cp" , System .getProperty ("test.classes" ),
66
+ "-Djava.security.debug=all" , "-Djavax.net.debug=all" , "ConfigFileTest" , "runner" );
67
+
68
+ // test the override functionality also. Should not be allowed since
69
+ // "security.overridePropertiesFile=true" Security property is missing.
70
+ exerciseSecurity (copiedJava .toString (), "-cp" , System .getProperty ("test.classes" ),
71
+ "-Djava.security.debug=all" , "-Djavax.net.debug=all" ,
72
+ "-Djava.security.properties==file://" + extraPropsFile , "ConfigFileTest" , "runner" );
73
+ }
74
+ }
75
+
76
+ private static void exerciseSecurity (String ... args ) throws Exception {
77
+ ProcessBuilder process = new ProcessBuilder (args );
78
+ OutputAnalyzer oa = ProcessTools .executeProcess (process );
79
+ oa .shouldHaveExitValue (1 ).shouldContain ("java.security file missing" );
80
+ }
81
+
82
+ private static void copyJDKMinusJavaSecurity (Path src , Path dst ) throws Exception {
83
+ Files .walk (src )
84
+ .skip (1 )
85
+ .filter (p -> !p .toString ().endsWith ("java.security" ))
86
+ .forEach (file -> {
87
+ try {
88
+ Files .copy (file , dst .resolve (src .relativize (file )), StandardCopyOption .COPY_ATTRIBUTES );
89
+ } catch (IOException ioe ) {
90
+ throw new UncheckedIOException (ioe );
91
+ }
92
+ });
93
+ }
94
+ }
0 commit comments