diff options
author | Jesse Wilson <jessewilson@google.com> | 2011-02-23 18:45:29 -0800 |
---|---|---|
committer | Jesse Wilson <jessewilson@google.com> | 2011-02-24 08:42:52 -0800 |
commit | 8bc378b1cec65bc06766a14a9cc575fec931b418 (patch) | |
tree | df8f98ce0ffadc67857b0e27093265392a30c780 | |
parent | 9ebb5807fa40f3bddf3567906a2c77ee053174bb (diff) | |
download | libcore-8bc378b1cec65bc06766a14a9cc575fec931b418.zip libcore-8bc378b1cec65bc06766a14a9cc575fec931b418.tar.gz libcore-8bc378b1cec65bc06766a14a9cc575fec931b418.tar.bz2 |
Don't generate large DH keys when small ones will do.
This dramatically improves the runtime of these tests from a few
minutes to a few seconds.
Also update known failures to cover the new reasons why these
tests are failing.
Change-Id: I82b738f3f1fb24a08d334fa960153692a0c9144f
http://b/3474446
4 files changed, 62 insertions, 147 deletions
diff --git a/expectations/knownfailures.txt b/expectations/knownfailures.txt index b60eaea..5b5f57e 100644 --- a/expectations/knownfailures.txt +++ b/expectations/knownfailures.txt @@ -3,19 +3,21 @@ */ [ { + description: "Encoded bytes don't match for EC elliptic curve keys created through KeyFactory.generatePrivate()", + names: [ + "org.apache.harmony.security.tests.java.security.KeyFactory2Test#test_generatePrivateLjava_security_spec_KeySpec", + "org.apache.harmony.security.tests.java.security.KeyFactory2Test#test_getKeySpecLjava_security_KeyLjava_lang_Class" + ], + bug: 3483365 +}, +{ description: "Test fails, Intermediate certificate lacks BasicConstraints", name: "com.android.org.bouncycastle.jce.provider.PKIXCertPathValidatorSpiTest#testTrustAndRemoteCertificatesWithDifferentEncodings", bug: 3474648 }, { - description: "java.math.NativeBN.BN_generate_prime_ex takes far too long, causes tests to time out", - names: [ - "org.apache.harmony.crypto.tests.javax.crypto.interfaces.DHPrivateKeyTest#test_getParams", - "org.apache.harmony.security.tests.java.security.KeyFactory2Test#test_generatePrivateLjava_security_spec_KeySpec", - "org.apache.harmony.security.tests.java.security.KeyFactory2Test#test_generatePublicLjava_security_spec_KeySpec", - "org.apache.harmony.security.tests.java.security.KeyFactory2Test#test_getKeySpecLjava_security_KeyLjava_lang_Class", - "org.apache.harmony.security.tests.java.security.KeyFactory2Test#test_translateKeyLjava_security_Key" - ], + description: "DHParametersHelper.generateSafePrimes sometimes takes long time, other times takes a very long time", + name: "org.apache.harmony.crypto.tests.javax.crypto.interfaces.DHPrivateKeyTest#test_getParams", bug: 3474446 }, { diff --git a/luni/src/test/java/org/apache/harmony/crypto/tests/javax/crypto/interfaces/DHPrivateKeyTest.java b/luni/src/test/java/org/apache/harmony/crypto/tests/javax/crypto/interfaces/DHPrivateKeyTest.java index 4624ae9..24bfed0 100644 --- a/luni/src/test/java/org/apache/harmony/crypto/tests/javax/crypto/interfaces/DHPrivateKeyTest.java +++ b/luni/src/test/java/org/apache/harmony/crypto/tests/javax/crypto/interfaces/DHPrivateKeyTest.java @@ -73,10 +73,9 @@ public class DHPrivateKeyTest extends TestCase { args = {} ) }) - @BrokenTest("Too slow - disabling for now") public void test_getParams() throws Exception { KeyPairGenerator kg = KeyPairGenerator.getInstance("DH"); - kg.initialize(1024); + kg.initialize(192); KeyPair kp1 = kg.genKeyPair(); KeyPair kp2 = kg.genKeyPair(); DHPrivateKey pk1 = (DHPrivateKey) kp1.getPrivate(); diff --git a/luni/src/test/java/org/apache/harmony/security/tests/java/security/KeyFactory2Test.java b/luni/src/test/java/org/apache/harmony/security/tests/java/security/KeyFactory2Test.java index 1ed5909..1be7fa6 100644 --- a/luni/src/test/java/org/apache/harmony/security/tests/java/security/KeyFactory2Test.java +++ b/luni/src/test/java/org/apache/harmony/security/tests/java/security/KeyFactory2Test.java @@ -17,10 +17,6 @@ package org.apache.harmony.security.tests.java.security; -import dalvik.annotation.TestTargetClass; -import dalvik.annotation.TestLevel; -import dalvik.annotation.TestTargetNew; - import java.security.InvalidKeyException; import java.security.Key; import java.security.KeyFactory; @@ -41,8 +37,8 @@ import java.security.spec.X509EncodedKeySpec; import java.util.Arrays; import java.util.Enumeration; import java.util.Vector; +import libcore.java.security.StandardNames; -@TestTargetClass(KeyFactory.class) public class KeyFactory2Test extends junit.framework.TestCase { private static final String KEYFACTORY_ID = "KeyFactory."; @@ -90,16 +86,6 @@ public class KeyFactory2Test extends junit.framework.TestCase { return null; } - /** - * @tests java.security.KeyFactory#KeyFactory(java.security.KeyFactorySpi, - * java.security.Provider, java.lang.String) - */ - @TestTargetNew( - level = TestLevel.COMPLETE, - notes = "", - method = "KeyFactory", - args = {java.security.KeyFactorySpi.class, java.security.Provider.class, java.lang.String.class} - ) public void test_constructor() { KeyFactorySpi kfs = new KeyFactorySpiStub(); @@ -122,15 +108,6 @@ public class KeyFactory2Test extends junit.framework.TestCase { } } - /** - * @tests java.security.KeyFactory#generatePrivate(java.security.spec.KeySpec) - */ - @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, - notes = "", - method = "generatePrivate", - args = {java.security.spec.KeySpec.class} - ) public void test_generatePrivateLjava_security_spec_KeySpec() { // Test for method java.security.PrivateKey // java.security.KeyFactory.generatePrivate(java.security.spec.KeySpec) @@ -142,7 +119,7 @@ public class KeyFactory2Test extends junit.framework.TestCase { .getInstance(keyfactAlgs[i]); SecureRandom random = new SecureRandom(); // We don't use // getInstance - keyGen.initialize(1024, random); + keyGen.initialize(StandardNames.getMinimumKeySize(keyfactAlgs[i]), random); KeepAlive keepalive = createKeepAlive(keyfactAlgs[i]); KeyPair keys = keyGen.generateKeyPair(); if (keepalive != null) { @@ -150,7 +127,7 @@ public class KeyFactory2Test extends junit.framework.TestCase { } KeySpec privateKeySpec = fact.getKeySpec(keys.getPrivate(), - getPrivateKeySpecClass(keyfactAlgs[i])); + StandardNames.getPrivateKeySpecClass(keyfactAlgs[i])); PrivateKey privateKey = fact.generatePrivate(privateKeySpec); boolean samePrivate = Arrays.equals(keys.getPrivate() .getEncoded(), privateKey.getEncoded()); @@ -170,15 +147,6 @@ public class KeyFactory2Test extends junit.framework.TestCase { } } - /** - * @tests java.security.KeyFactory#generatePublic(java.security.spec.KeySpec) - */ - @TestTargetNew( - level = TestLevel.PARTIAL, - notes = "InvalidKeySpecException checking missed", - method = "generatePublic", - args = {java.security.spec.KeySpec.class} - ) public void test_generatePublicLjava_security_spec_KeySpec() { // Test for method java.security.PublicKey // java.security.KeyFactory.generatePublic(java.security.spec.KeySpec) @@ -190,14 +158,14 @@ public class KeyFactory2Test extends junit.framework.TestCase { .getInstance(keyfactAlgs[i]); // We don't use getInstance SecureRandom random = new SecureRandom(); - keyGen.initialize(1024, random); + keyGen.initialize(StandardNames.getMinimumKeySize(keyfactAlgs[i]), random); KeepAlive keepalive = createKeepAlive(keyfactAlgs[i]); KeyPair keys = keyGen.generateKeyPair(); if (keepalive != null) { keepalive.interrupt(); } KeySpec publicKeySpec = fact.getKeySpec(keys.getPublic(), - getPublicKeySpecClass(keyfactAlgs[i])); + StandardNames.getPublicKeySpecClass(keyfactAlgs[i])); PublicKey publicKey = fact.generatePublic(publicKeySpec); boolean samePublic = Arrays.equals(keys.getPublic() .getEncoded(), publicKey.getEncoded()); @@ -214,15 +182,6 @@ public class KeyFactory2Test extends junit.framework.TestCase { } } - /** - * @tests java.security.KeyFactory#getAlgorithm() - */ - @TestTargetNew( - level = TestLevel.COMPLETE, - notes = "", - method = "getAlgorithm", - args = {} - ) public void test_getAlgorithm() { // Test for method java.lang.String // java.security.KeyFactory.getAlgorithm() @@ -240,15 +199,6 @@ public class KeyFactory2Test extends junit.framework.TestCase { }// end for } - /** - * @tests java.security.KeyFactory#getInstance(java.lang.String) - */ - @TestTargetNew( - level = TestLevel.PARTIAL, - notes = "NoSuchAlgorithmException checking missed", - method = "getInstance", - args = {java.lang.String.class} - ) public void test_getInstanceLjava_lang_String() { // Test for method java.security.KeyFactory // java.security.KeyFactory.getInstance(java.lang.String) @@ -261,18 +211,7 @@ public class KeyFactory2Test extends junit.framework.TestCase { }// end for } - /** - * @tests java.security.KeyFactory#getInstance(java.lang.String, - * java.lang.String) - */ - @TestTargetNew( - level = TestLevel.PARTIAL, - notes = "NoSuchAlgorithmException, NoSuchProviderException checking missed", - method = "getInstance", - args = {java.lang.String.class, java.lang.String.class} - ) public void test_getInstanceLjava_lang_StringLjava_lang_String() { - // Test1: Test for method java.security.KeyFactory // java.security.KeyFactory.getInstance(java.lang.String, // java.lang.String) @@ -302,17 +241,7 @@ public class KeyFactory2Test extends junit.framework.TestCase { } } - /** - * @tests java.security.KeyFactory#getInstance(java.lang.String, Provider) - */ - @TestTargetNew( - level = TestLevel.PARTIAL, - notes = "NoSuchAlgorithmException checking missed", - method = "getInstance", - args = {java.lang.String.class, java.security.Provider.class} - ) public void test_getInstanceLjava_lang_StringLjava_security_Provider() { - // Test1: Test for method java.security.KeyFactory // java.security.KeyFactory.getInstance(java.lang.String, // java.security.Provider) @@ -342,16 +271,6 @@ public class KeyFactory2Test extends junit.framework.TestCase { } } - /** - * @tests java.security.KeyFactory#getKeySpec(java.security.Key, - * java.lang.Class) - */ - @TestTargetNew( - level = TestLevel.PARTIAL, - notes = "InvalidKeySpecException checking missed", - method = "getKeySpec", - args = {java.security.Key.class, java.lang.Class.class} - ) public void test_getKeySpecLjava_security_KeyLjava_lang_Class() { // Test for method java.security.spec.KeySpec // java.security.KeyFactory.getKeySpec(java.security.Key, @@ -365,16 +284,16 @@ public class KeyFactory2Test extends junit.framework.TestCase { // We don't use getInstance SecureRandom random = new SecureRandom(); - keyGen.initialize(1024, random); + keyGen.initialize(StandardNames.getMinimumKeySize(keyfactAlgs[i]), random); KeepAlive keepalive = createKeepAlive(keyfactAlgs[i]); KeyPair keys = keyGen.generateKeyPair(); if (keepalive != null) { keepalive.interrupt(); } KeySpec privateKeySpec = fact.getKeySpec(keys.getPrivate(), - getPrivateKeySpecClass(keyfactAlgs[i])); + StandardNames.getPrivateKeySpecClass(keyfactAlgs[i])); KeySpec publicKeySpec = fact.getKeySpec(keys.getPublic(), - getPublicKeySpecClass(keyfactAlgs[i])); + StandardNames.getPublicKeySpecClass(keyfactAlgs[i])); PrivateKey privateKey = fact.generatePrivate(privateKeySpec); PublicKey publicKey = fact.generatePublic(publicKeySpec); boolean samePublic = Arrays.equals(keys.getPublic() @@ -406,15 +325,6 @@ public class KeyFactory2Test extends junit.framework.TestCase { } } - /** - * @tests java.security.KeyFactory#getProvider() - */ - @TestTargetNew( - level = TestLevel.COMPLETE, - notes = "", - method = "getProvider", - args = {} - ) public void test_getProvider() { // Test for method java.security.Provider // java.security.KeyFactory.getProvider() @@ -430,15 +340,6 @@ public class KeyFactory2Test extends junit.framework.TestCase { }// end for } - /** - * @tests java.security.KeyFactory#translateKey(java.security.Key) - */ - @TestTargetNew( - level = TestLevel.PARTIAL, - notes = "InvalidKeyException checking missed", - method = "translateKey", - args = {java.security.Key.class} - ) public void test_translateKeyLjava_security_Key() { // Test for method java.security.Key // java.security.KeyFactory.translateKey(java.security.Key) @@ -451,7 +352,7 @@ public class KeyFactory2Test extends junit.framework.TestCase { // We don't use getInstance SecureRandom random = new SecureRandom(); - keyGen.initialize(1024, random); + keyGen.initialize(StandardNames.getMinimumKeySize(keyfactAlgs[i]), random); KeepAlive keepalive = createKeepAlive(keyfactAlgs[i]); KeyPair keys = keyGen.generateKeyPair(); if (keepalive != null) { @@ -504,34 +405,6 @@ public class KeyFactory2Test extends junit.framework.TestCase { return algs.toArray(new String[algs.size()]); } - /** - * Returns the public key spec class for a given algorithm, or null if it is - * not known. - */ - private Class<? extends KeySpec> getPrivateKeySpecClass(String algName) { - if (algName.equals("RSA")) { - return java.security.spec.RSAPrivateCrtKeySpec.class; - } - if (algName.equals("DSA")) { - return java.security.spec.DSAPrivateKeySpec.class; - } - return null; - } - - /** - * Returns the private key spec class for a given algorithm, or null if it - * is not known. - */ - private Class<? extends KeySpec> getPublicKeySpecClass(String algName) { - if (algName.equals("RSA")) { - return java.security.spec.RSAPublicKeySpec.class; - } - if (algName.equals("DSA")) { - return java.security.spec.DSAPublicKeySpec.class; - } - return null; - } - public class KeyFactoryStub extends KeyFactory { public KeyFactoryStub(KeyFactorySpi keyFacSpi, Provider provider, String algorithm) { diff --git a/support/src/test/java/libcore/java/security/StandardNames.java b/support/src/test/java/libcore/java/security/StandardNames.java index 58aaf28..9f32309 100644 --- a/support/src/test/java/libcore/java/security/StandardNames.java +++ b/support/src/test/java/libcore/java/security/StandardNames.java @@ -17,8 +17,14 @@ package libcore.java.security; import java.security.Security; +import java.security.spec.DSAPrivateKeySpec; +import java.security.spec.DSAPublicKeySpec; +import java.security.spec.ECPrivateKeySpec; +import java.security.spec.ECPublicKeySpec; +import java.security.spec.KeySpec; +import java.security.spec.RSAPrivateCrtKeySpec; +import java.security.spec.RSAPublicKeySpec; import java.util.Arrays; -import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; @@ -27,6 +33,8 @@ import java.util.LinkedHashSet; import java.util.List; import java.util.Map; import java.util.Set; +import javax.crypto.spec.DHPrivateKeySpec; +import javax.crypto.spec.DHPublicKeySpec; import junit.framework.Assert; /** @@ -663,6 +671,39 @@ public final class StandardNames extends Assert { } } } + + public static final Map<String, Class<? extends KeySpec>> PRIVATE_KEY_SPEC_CLASSES; + public static final Map<String, Class<? extends KeySpec>> PUBLIC_KEY_SPEC_CLASSES; + public static final Map<String, Integer> MINIMUM_KEY_SIZE; + static { + PRIVATE_KEY_SPEC_CLASSES = new HashMap<String, Class<? extends KeySpec>>(); + PUBLIC_KEY_SPEC_CLASSES = new HashMap<String, Class<? extends KeySpec>>(); + MINIMUM_KEY_SIZE = new HashMap<String, Integer>(); + PRIVATE_KEY_SPEC_CLASSES.put("RSA", RSAPrivateCrtKeySpec.class); + PUBLIC_KEY_SPEC_CLASSES.put("RSA", RSAPublicKeySpec.class); + MINIMUM_KEY_SIZE.put("RSA", 256); + PRIVATE_KEY_SPEC_CLASSES.put("DSA", DSAPrivateKeySpec.class); + PUBLIC_KEY_SPEC_CLASSES.put("DSA", DSAPublicKeySpec.class); + MINIMUM_KEY_SIZE.put("DSA", 512); + PRIVATE_KEY_SPEC_CLASSES.put("DH", DHPrivateKeySpec.class); + PUBLIC_KEY_SPEC_CLASSES.put("DH", DHPublicKeySpec.class); + MINIMUM_KEY_SIZE.put("DH", 256); + PRIVATE_KEY_SPEC_CLASSES.put("EC", ECPrivateKeySpec.class); + PUBLIC_KEY_SPEC_CLASSES.put("EC", ECPublicKeySpec.class); + MINIMUM_KEY_SIZE.put("EC", 256); + } + + public static Class<? extends KeySpec> getPrivateKeySpecClass(String algName) { + return PRIVATE_KEY_SPEC_CLASSES.get(algName); + } + + public static Class<? extends KeySpec> getPublicKeySpecClass(String algName) { + return PUBLIC_KEY_SPEC_CLASSES.get(algName); + } + + public static int getMinimumKeySize(String algName) { + return MINIMUM_KEY_SIZE.get(algName); + } /** * Asserts that the cipher suites array is non-null and that it |