Skip to content

Commit

Permalink
8180568: Refactor javax/crypto shell tests to plain java tests
Browse files Browse the repository at this point in the history
Reviewed-by: wetmore
  • Loading branch information
Sibabrata Sahoo committed May 6, 2021
1 parent 138d573 commit 20ad428
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 201 deletions.
96 changes: 76 additions & 20 deletions test/jdk/javax/crypto/CryptoPermissions/TestExemption.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -23,38 +23,94 @@
* questions.
*/

/*
* @test
* @bug 8161527 8180568
* @summary NPE is thrown if exempt application is bundled with specific
* cryptoPerms
* @requires java.runtime.name ~= "OpenJDK.*"
* @library /test/lib
* @run main TestExemption
*/
import javax.crypto.*;
import java.nio.file.Path;
import java.security.*;
import java.util.ArrayList;
import java.util.List;
import jdk.test.lib.process.OutputAnalyzer;
import jdk.test.lib.process.ProcessTools;
import jdk.test.lib.util.JarUtils;

public class TestExemption {

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

KeyGenerator kg = KeyGenerator.getInstance("AES");
kg.init(128);
SecretKey key128 = kg.generateKey();
public static void main(String[] args) throws Exception {

kg.init(192);
SecretKey key192 = kg.generateKey();
// With no argument passed, compile the same class, jar it and run the
// test section of the jar file which is nothing but else section here.
if (args.length == 0) {
JarUtils.createJarFile(
Path.of(JAR_FILE), Path.of(CLASSES), Path.of(SRC_CLS));
JarUtils.updateJarFile(
Path.of(JAR_FILE), Path.of(SRC), Path.of(CRYPT_PERM));
OutputAnalyzer oa = ProcessTools.executeTestJava(
getParameters().toArray(String[]::new));
System.out.println(oa.getOutput());
oa.shouldHaveExitValue(0);
} else {
// Set the crypto policy to limited so that additional policy can be
// supplemented through cryptoPerms when bundled inside a jar file.
Security.setProperty("crypto.policy", "limited");
KeyGenerator kg = KeyGenerator.getInstance("AES");
kg.init(128);
SecretKey key128 = kg.generateKey();

kg.init(256);
SecretKey key256 = kg.generateKey();
kg.init(192);
SecretKey key192 = kg.generateKey();

Cipher c = Cipher.getInstance("AES/CBC/NoPadding");
kg.init(256);
SecretKey key256 = kg.generateKey();

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

System.out.println("Testing 192-bit");
c.init(Cipher.ENCRYPT_MODE, key192);
Cipher c = Cipher.getInstance("AES/CBC/NoPadding");
System.out.println("Testing 128-bit");
c.init(Cipher.ENCRYPT_MODE, key128);

try {
System.out.println("Testing 256-bit");
c.init(Cipher.ENCRYPT_MODE, key256);
} catch (InvalidKeyException e) {
System.out.println("Caught the right exception");
System.out.println("Testing 192-bit");
c.init(Cipher.ENCRYPT_MODE, key192);
try {
System.out.println("Testing 256-bit");
c.init(Cipher.ENCRYPT_MODE, key256);
throw new RuntimeException("Shouldn't reach here");
} catch (InvalidKeyException e) {
System.out.println("Caught the right exception");
}
System.out.println("DONE!");
}
}

private static List<String> getParameters() {

System.out.println("DONE!");
List<String> cmds = new ArrayList<>();
cmds.add("-cp");
cmds.add(JAR_FILE);
cmds.add(NAME);
// Argument to run the Test section of class inside the jar file.
cmds.add("run");
return cmds;
}

}
79 changes: 0 additions & 79 deletions test/jdk/javax/crypto/CryptoPermissions/TestExemption.sh

This file was deleted.

102 changes: 0 additions & 102 deletions test/jdk/javax/crypto/SecretKeyFactory/FailOverTest.sh

This file was deleted.

80 changes: 80 additions & 0 deletions test/jdk/javax/crypto/SecretKeyFactory/TestFailOver.java
@@ -0,0 +1,80 @@
/*
* Copyright (c) 2006, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import jdk.test.lib.compiler.CompilerUtils;
import jdk.test.lib.process.OutputAnalyzer;
import jdk.test.lib.process.ProcessTools;

/*
* @test
* @bug 6370923 8180568
* @summary SecretKeyFactory failover does not work
* @library /test/lib
* @build jdk.test.lib.compiler.CompilerUtils
* @run main TestFailOver
*/
public class TestFailOver {

private static final Path SRC = Paths.get(System.getProperty("test.src"));
private static final String P1_JAR
= SRC.resolve("P1.jar").toFile().getAbsolutePath();
private static final String P2_JAR
= SRC.resolve("P2.jar").toFile().getAbsolutePath();
private static final String SEC_PROP
= SRC.resolve("security.properties").toFile().getAbsolutePath();
private static final String JF_NAME = "FailOverTest";
private static final Path SRC_PATH = SRC.resolve(JF_NAME + ".java");
private static final Path COMPILE_PATH = Paths.get(".");
private static final String PS = File.pathSeparator;

public static void main(String[] args) throws Exception {

List<String> params = getParameters();
// Compile all source files.
boolean done = CompilerUtils.compile(SRC_PATH, COMPILE_PATH,
params.toArray(String[]::new));
if (!done) {
throw new RuntimeException("Test setup failed.");
}
params.add(0, "-Djava.security.properties=" + SEC_PROP);
params.add(JF_NAME);
OutputAnalyzer oa = ProcessTools.executeTestJava(
params.toArray(String[]::new));
System.out.println(oa.getOutput());
oa.shouldHaveExitValue(0);
}

private static List<String> getParameters() {

List<String> cmds = new ArrayList<>();
cmds.add("-cp");
cmds.add(P1_JAR + PS + P2_JAR + PS + COMPILE_PATH);
return cmds;
}

}

0 comments on commit 20ad428

Please sign in to comment.