8156671: Refactor sun/security/rsa/SignatureTest.java

Reviewed-by: valeriep
This commit is contained in:
Rajan Halade 2016-05-12 16:40:01 -07:00
parent 9d15bdddf6
commit 21c525f41e

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -39,9 +39,9 @@ import java.security.spec.RSAPrivateKeySpec;
import java.security.spec.RSAPublicKeySpec; import java.security.spec.RSAPublicKeySpec;
import java.security.spec.X509EncodedKeySpec; import java.security.spec.X509EncodedKeySpec;
import java.util.Arrays; import java.util.Arrays;
import java.util.stream.IntStream;
import static javax.crypto.Cipher.PRIVATE_KEY; import static javax.crypto.Cipher.PRIVATE_KEY;
import static javax.crypto.Cipher.PUBLIC_KEY; import static javax.crypto.Cipher.PUBLIC_KEY;
import jdk.testlibrary.RandomFactory;
/** /**
* @test * @test
@ -49,36 +49,13 @@ import jdk.testlibrary.RandomFactory;
* @summary Create a signature for RSA and get its signed data. re-initiate * @summary Create a signature for RSA and get its signed data. re-initiate
* the signature with the public key. The signature can be verified * the signature with the public key. The signature can be verified
* by acquired signed data. * by acquired signed data.
* @key randomness * @run main SignatureTest 512
* @library ../../../lib/testlibrary * @run main SignatureTest 768
* @run main SignatureTest MD2withRSA 512 * @run main SignatureTest 1024
* @run main SignatureTest MD5withRSA 512 * @run main SignatureTest 2048
* @run main SignatureTest SHA1withRSA 512 * @run main/timeout=240 SignatureTest 4096
* @run main SignatureTest SHA256withRSA 512 * @run main/timeout=240 SignatureTest 5120
* @run main SignatureTest MD2withRSA 768 * @run main/timeout=240 SignatureTest 6144
* @run main SignatureTest MD5withRSA 768
* @run main SignatureTest SHA1withRSA 768
* @run main SignatureTest SHA256withRSA 768
* @run main SignatureTest MD2withRSA 1024
* @run main SignatureTest MD5withRSA 1024
* @run main SignatureTest SHA1withRSA 1024
* @run main SignatureTest SHA256withRSA 1024
* @run main SignatureTest MD2withRSA 2048
* @run main SignatureTest MD5withRSA 2048
* @run main SignatureTest SHA1withRSA 2048
* @run main SignatureTest SHA256withRSA 2048
* @run main/timeout=240 SignatureTest MD2withRSA 4096
* @run main/timeout=240 SignatureTest MD5withRSA 4096
* @run main/timeout=240 SignatureTest SHA1withRSA 4096
* @run main/timeout=240 SignatureTest SHA256withRSA 4096
* @run main/timeout=240 SignatureTest MD2withRSA 5120
* @run main/timeout=240 SignatureTest MD5withRSA 5120
* @run main/timeout=240 SignatureTest SHA1withRSA 5120
* @run main/timeout=240 SignatureTest SHA256withRSA 5120
* @run main/timeout=240 SignatureTest MD2withRSA 6144
* @run main/timeout=240 SignatureTest MD5withRSA 6144
* @run main/timeout=240 SignatureTest SHA1withRSA 6144
* @run main/timeout=240 SignatureTest SHA256withRSA 6144
*/ */
public class SignatureTest { public class SignatureTest {
/** /**
@ -101,12 +78,19 @@ public class SignatureTest {
*/ */
private static final int UPDATE_TIMES_HUNDRED = 100; private static final int UPDATE_TIMES_HUNDRED = 100;
/**
* Signature algorithms to test
*/
private static final String[] SIGN_ALG = {"MD2withRSA", "MD5withRSA",
"SHA1withRSA", "SHA256withRSA"};
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
String testAlg = args[0]; int testSize = Integer.parseInt(args[0]);
int testSize = Integer.parseInt(args[1]);
byte[] data = new byte[100]; byte[] data = new byte[100];
RandomFactory.getRandom().nextBytes(data); IntStream.range(0, data.length).forEach(j -> {
data[j] = (byte) j;
});
// create a key pair // create a key pair
KeyPair kpair = generateKeys(KEYALG, testSize); KeyPair kpair = generateKeys(KEYALG, testSize);
@ -115,16 +99,17 @@ public class SignatureTest {
// For signature algorithm, create and verify a signature // For signature algorithm, create and verify a signature
Arrays.stream(privs).forEach(priv Arrays.stream(privs).forEach(priv
-> Arrays.stream(pubs).forEach(pub -> { -> Arrays.stream(pubs).forEach(pub
try { -> Arrays.stream(SIGN_ALG).forEach(testAlg -> {
checkSignature(data, (PublicKey) pub, (PrivateKey) priv, try {
testAlg); checkSignature(data, (PublicKey) pub, (PrivateKey) priv,
} catch (NoSuchAlgorithmException | InvalidKeyException testAlg);
| SignatureException | NoSuchProviderException ex) { } catch (NoSuchAlgorithmException | InvalidKeyException |
throw new RuntimeException(ex); SignatureException | NoSuchProviderException ex) {
} throw new RuntimeException(ex);
} }
)); }
)));
} }