Skip to content

Commit 2a028c4

Browse files
committed
8180568: Refactor javax/crypto shell tests to plain java tests
Reviewed-by: mdoerr Backport-of: 20ad428
1 parent 7410b74 commit 2a028c4

File tree

4 files changed

+156
-201
lines changed

4 files changed

+156
-201
lines changed
Lines changed: 76 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2016, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -23,38 +23,94 @@
2323
* questions.
2424
*/
2525

26+
/*
27+
* @test
28+
* @bug 8161527 8180568
29+
* @summary NPE is thrown if exempt application is bundled with specific
30+
* cryptoPerms
31+
* @requires java.runtime.name ~= "OpenJDK.*"
32+
* @library /test/lib
33+
* @run main TestExemption
34+
*/
2635
import javax.crypto.*;
36+
import java.nio.file.Path;
2737
import java.security.*;
38+
import java.util.ArrayList;
39+
import java.util.List;
40+
import jdk.test.lib.process.OutputAnalyzer;
41+
import jdk.test.lib.process.ProcessTools;
42+
import jdk.test.lib.util.JarUtils;
2843

2944
public class TestExemption {
3045

31-
public static void main(String[] args) throws Exception {
46+
private static final String SRC = System.getProperty("test.src");
47+
private static final String CLASSES = System.getProperty("test.classes");
48+
private static final String NAME = TestExemption.class.getName();
49+
private static final String SRC_CLS = NAME + ".class";
50+
private static final String JAR_FILE = NAME + ".jar";
51+
private static final String CRYPT_PERM = "cryptoPerms";
3252

33-
KeyGenerator kg = KeyGenerator.getInstance("AES");
34-
kg.init(128);
35-
SecretKey key128 = kg.generateKey();
53+
public static void main(String[] args) throws Exception {
3654

37-
kg.init(192);
38-
SecretKey key192 = kg.generateKey();
55+
// With no argument passed, compile the same class, jar it and run the
56+
// test section of the jar file which is nothing but else section here.
57+
if (args.length == 0) {
58+
JarUtils.createJarFile(
59+
Path.of(JAR_FILE), Path.of(CLASSES), Path.of(SRC_CLS));
60+
JarUtils.updateJarFile(
61+
Path.of(JAR_FILE), Path.of(SRC), Path.of(CRYPT_PERM));
62+
OutputAnalyzer oa = ProcessTools.executeTestJava(
63+
getParameters().toArray(String[]::new));
64+
System.out.println(oa.getOutput());
65+
oa.shouldHaveExitValue(0);
66+
} else {
67+
// Set the crypto policy to limited so that additional policy can be
68+
// supplemented through cryptoPerms when bundled inside a jar file.
69+
Security.setProperty("crypto.policy", "limited");
70+
KeyGenerator kg = KeyGenerator.getInstance("AES");
71+
kg.init(128);
72+
SecretKey key128 = kg.generateKey();
3973

40-
kg.init(256);
41-
SecretKey key256 = kg.generateKey();
74+
kg.init(192);
75+
SecretKey key192 = kg.generateKey();
4276

43-
Cipher c = Cipher.getInstance("AES/CBC/NoPadding");
77+
kg.init(256);
78+
SecretKey key256 = kg.generateKey();
4479

45-
System.out.println("Testing 128-bit");
46-
c.init(Cipher.ENCRYPT_MODE, key128);
80+
int maxAllowed = Cipher.getMaxAllowedKeyLength("AES");
81+
System.out.println("Max allowed: " + maxAllowed);
82+
// With limited crypto and bundled cryptoPerms maximum allowed
83+
// length of AES is upto 192.
84+
if (maxAllowed > 192) {
85+
throw new RuntimeException(">192 not supported");
86+
}
4787

48-
System.out.println("Testing 192-bit");
49-
c.init(Cipher.ENCRYPT_MODE, key192);
88+
Cipher c = Cipher.getInstance("AES/CBC/NoPadding");
89+
System.out.println("Testing 128-bit");
90+
c.init(Cipher.ENCRYPT_MODE, key128);
5091

51-
try {
52-
System.out.println("Testing 256-bit");
53-
c.init(Cipher.ENCRYPT_MODE, key256);
54-
} catch (InvalidKeyException e) {
55-
System.out.println("Caught the right exception");
92+
System.out.println("Testing 192-bit");
93+
c.init(Cipher.ENCRYPT_MODE, key192);
94+
try {
95+
System.out.println("Testing 256-bit");
96+
c.init(Cipher.ENCRYPT_MODE, key256);
97+
throw new RuntimeException("Shouldn't reach here");
98+
} catch (InvalidKeyException e) {
99+
System.out.println("Caught the right exception");
100+
}
101+
System.out.println("DONE!");
56102
}
103+
}
104+
105+
private static List<String> getParameters() {
57106

58-
System.out.println("DONE!");
107+
List<String> cmds = new ArrayList<>();
108+
cmds.add("-cp");
109+
cmds.add(JAR_FILE);
110+
cmds.add(NAME);
111+
// Argument to run the Test section of class inside the jar file.
112+
cmds.add("run");
113+
return cmds;
59114
}
115+
60116
}

test/jdk/javax/crypto/CryptoPermissions/TestExemption.sh

Lines changed: 0 additions & 79 deletions
This file was deleted.

test/jdk/javax/crypto/SecretKeyFactory/FailOverTest.sh

Lines changed: 0 additions & 102 deletions
This file was deleted.
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Copyright (c) 2006, 2021, 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 java.io.File;
25+
import java.nio.file.Path;
26+
import java.nio.file.Paths;
27+
import java.util.ArrayList;
28+
import java.util.List;
29+
import jdk.test.lib.compiler.CompilerUtils;
30+
import jdk.test.lib.process.OutputAnalyzer;
31+
import jdk.test.lib.process.ProcessTools;
32+
33+
/*
34+
* @test
35+
* @bug 6370923 8180568
36+
* @summary SecretKeyFactory failover does not work
37+
* @library /test/lib
38+
* @build jdk.test.lib.compiler.CompilerUtils
39+
* @run main TestFailOver
40+
*/
41+
public class TestFailOver {
42+
43+
private static final Path SRC = Paths.get(System.getProperty("test.src"));
44+
private static final String P1_JAR
45+
= SRC.resolve("P1.jar").toFile().getAbsolutePath();
46+
private static final String P2_JAR
47+
= SRC.resolve("P2.jar").toFile().getAbsolutePath();
48+
private static final String SEC_PROP
49+
= SRC.resolve("security.properties").toFile().getAbsolutePath();
50+
private static final String JF_NAME = "FailOverTest";
51+
private static final Path SRC_PATH = SRC.resolve(JF_NAME + ".java");
52+
private static final Path COMPILE_PATH = Paths.get(".");
53+
private static final String PS = File.pathSeparator;
54+
55+
public static void main(String[] args) throws Exception {
56+
57+
List<String> params = getParameters();
58+
// Compile all source files.
59+
boolean done = CompilerUtils.compile(SRC_PATH, COMPILE_PATH,
60+
params.toArray(String[]::new));
61+
if (!done) {
62+
throw new RuntimeException("Test setup failed.");
63+
}
64+
params.add(0, "-Djava.security.properties=" + SEC_PROP);
65+
params.add(JF_NAME);
66+
OutputAnalyzer oa = ProcessTools.executeTestJava(
67+
params.toArray(String[]::new));
68+
System.out.println(oa.getOutput());
69+
oa.shouldHaveExitValue(0);
70+
}
71+
72+
private static List<String> getParameters() {
73+
74+
List<String> cmds = new ArrayList<>();
75+
cmds.add("-cp");
76+
cmds.add(P1_JAR + PS + P2_JAR + PS + COMPILE_PATH);
77+
return cmds;
78+
}
79+
80+
}

0 commit comments

Comments
 (0)