From 101547d4a82ba21031dc7cb62018720dbd493758 Mon Sep 17 00:00:00 2001 From: Jesse Wilson Date: Mon, 31 Jan 2011 19:27:16 -0800 Subject: Refactoring to add a builder for TestKeyStore. Change-Id: I346aea42a27042512f4ed97690f1e0ca1755257c --- .../test/java/libcore/javax/crypto/CipherTest.java | 20 +++++------ .../javax/net/ssl/KeyManagerFactoryTest.java | 42 ++++++++++++---------- .../java/libcore/javax/net/ssl/SSLEngineTest.java | 19 ++++------ .../java/libcore/javax/net/ssl/SSLSocketTest.java | 28 +++++++-------- .../javax/net/ssl/TrustManagerFactoryTest.java | 33 ++++++++--------- 5 files changed, 68 insertions(+), 74 deletions(-) (limited to 'luni/src') diff --git a/luni/src/test/java/libcore/javax/crypto/CipherTest.java b/luni/src/test/java/libcore/javax/crypto/CipherTest.java index 186355f..7919b35 100644 --- a/luni/src/test/java/libcore/javax/crypto/CipherTest.java +++ b/luni/src/test/java/libcore/javax/crypto/CipherTest.java @@ -49,9 +49,9 @@ public final class CipherTest extends TestCase { assertCipherInitWithKeyUsage(KeyUsage.keyAgreement | KeyUsage.decipherOnly, false, true, false, true); - // except when wraping a key is specifically allowed or + // except when wrapping a key is specifically allowed or assertCipherInitWithKeyUsage(KeyUsage.keyEncipherment, false, true, true, true); - // except when wraping data encryption is specifically allowed + // except when wrapping data encryption is specifically allowed assertCipherInitWithKeyUsage(KeyUsage.dataEncipherment, true, true, false, true); } @@ -100,16 +100,12 @@ public final class CipherTest extends TestCase { } } - private Certificate certificateWithKeyUsage(int keyUsage) { + private Certificate certificateWithKeyUsage(int keyUsage) throws Exception { // note the rare usage of non-zero keyUsage - return TestKeyStore.create(new String[] { "RSA" }, - null, - null, - "rsa-dsa-ec", - TestKeyStore.localhost(), - keyUsage, - true, - null, - null).getPrivateKey("RSA", "RSA").getCertificate(); + return new TestKeyStore.Builder() + .aliasPrefix("rsa-dsa-ec") + .keyUsage(keyUsage) + .build() + .getPrivateKey("RSA", "RSA").getCertificate(); } } diff --git a/luni/src/test/java/libcore/javax/net/ssl/KeyManagerFactoryTest.java b/luni/src/test/java/libcore/javax/net/ssl/KeyManagerFactoryTest.java index 2ff9567..a6bdc07 100644 --- a/luni/src/test/java/libcore/javax/net/ssl/KeyManagerFactoryTest.java +++ b/luni/src/test/java/libcore/javax/net/ssl/KeyManagerFactoryTest.java @@ -23,9 +23,9 @@ import java.security.KeyStore.PrivateKeyEntry; import java.security.PrivateKey; import java.security.Provider; import java.security.Security; +import java.security.cert.Certificate; import java.security.cert.X509Certificate; import java.util.Arrays; -import java.util.HashSet; import java.util.Set; import javax.net.ssl.KeyManager; import javax.net.ssl.KeyManagerFactory; @@ -40,16 +40,18 @@ import libcore.java.security.TestKeyStore; public class KeyManagerFactoryTest extends TestCase { // note the rare usage of DSA keys here in addition to RSA - private static final TestKeyStore TEST_KEY_STORE - = TestKeyStore.create(new String[] { "RSA", "DSA", "EC", "EC_RSA" }, - null, - null, - "rsa-dsa-ec", - TestKeyStore.localhost(), - 0, - true, - null, - null); + private static final TestKeyStore TEST_KEY_STORE; + + static { + try { + TEST_KEY_STORE = new TestKeyStore.Builder() + .keyAlgorithms("RSA", "DSA", "EC", "EC_RSA") + .aliasPrefix("rsa-dsa-ec") + .build(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } public void test_KeyManagerFactory_getDefaultAlgorithm() throws Exception { String algorithm = KeyManagerFactory.getDefaultAlgorithm(); @@ -58,7 +60,7 @@ public class KeyManagerFactoryTest extends TestCase { test_KeyManagerFactory(kmf); } - private static class UseslessManagerFactoryParameters implements ManagerFactoryParameters {} + private static class UselessManagerFactoryParameters implements ManagerFactoryParameters {} private static boolean supportsManagerFactoryParameters(String algorithm) { // Only the "New" one supports ManagerFactoryParameters @@ -94,7 +96,7 @@ public class KeyManagerFactoryTest extends TestCase { // init with useless ManagerFactoryParameters try { - kmf.init(new UseslessManagerFactoryParameters()); + kmf.init(new UselessManagerFactoryParameters()); fail(); } catch (InvalidAlgorithmParameterException expected) { } @@ -123,7 +125,8 @@ public class KeyManagerFactoryTest extends TestCase { test_KeyManagerFactory_getKeyManagers(kmf, false); } - private void test_KeyManagerFactory_getKeyManagers(KeyManagerFactory kmf, boolean empty) { + private void test_KeyManagerFactory_getKeyManagers(KeyManagerFactory kmf, boolean empty) + throws Exception { KeyManager[] keyManagers = kmf.getKeyManagers(); assertNotNull(keyManagers); assertTrue(keyManagers.length > 0); @@ -153,7 +156,8 @@ public class KeyManagerFactoryTest extends TestCase { // extra null at end requires no initialization } - private void test_X509KeyManager(X509KeyManager km, boolean empty, String algorithm) { + private void test_X509KeyManager(X509KeyManager km, boolean empty, String algorithm) + throws Exception { String[] keyTypes = keyTypes(algorithm); for (String keyType : keyTypes) { String[] aliases = km.getClientAliases(keyType, null); @@ -196,7 +200,7 @@ public class KeyManagerFactoryTest extends TestCase { } private void test_X509ExtendedKeyManager(X509ExtendedKeyManager km, - boolean empty, String algorithm) { + boolean empty, String algorithm) throws Exception { String[] keyTypes = keyTypes(algorithm); String a = km.chooseEngineClientAlias(keyTypes, null, null); test_X509KeyManager_alias(km, a, null, true, empty); @@ -215,7 +219,7 @@ public class KeyManagerFactoryTest extends TestCase { String alias, String keyType, boolean many, - boolean empty) { + boolean empty) throws Exception { if (empty || (!many && (keyType == null || keyType.isEmpty()))) { assertNull(keyType, alias); assertNull(keyType, km.getCertificateChain(alias)); @@ -251,8 +255,8 @@ public class KeyManagerFactoryTest extends TestCase { PrivateKeyEntry privateKeyEntry = TEST_KEY_STORE.getPrivateKey(keyAlgName, sigAlgName); if (!"EC".equals(keyAlgName)) { assertEquals(keyType, - Arrays.asList(privateKeyEntry.getCertificateChain()), - Arrays.asList(certificateChain)); + Arrays.asList(privateKeyEntry.getCertificateChain()), + Arrays.asList(certificateChain)); assertEquals(keyType, privateKeyEntry.getPrivateKey(), privateKey); } diff --git a/luni/src/test/java/libcore/javax/net/ssl/SSLEngineTest.java b/luni/src/test/java/libcore/javax/net/ssl/SSLEngineTest.java index d7e7c67..16a3359 100644 --- a/luni/src/test/java/libcore/javax/net/ssl/SSLEngineTest.java +++ b/luni/src/test/java/libcore/javax/net/ssl/SSLEngineTest.java @@ -16,8 +16,6 @@ package libcore.javax.net.ssl; -import libcore.java.security.StandardNames; -import libcore.java.security.TestKeyStore; import java.util.Arrays; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLEngine; @@ -27,6 +25,8 @@ import javax.net.ssl.SSLHandshakeException; import javax.net.ssl.SSLParameters; import javax.net.ssl.SSLSession; import junit.framework.TestCase; +import libcore.java.security.StandardNames; +import libcore.java.security.TestKeyStore; public class SSLEngineTest extends TestCase { @@ -69,16 +69,11 @@ public class SSLEngineTest extends TestCase { public void test_SSLEngine_getSupportedCipherSuites_connect() throws Exception { // note the rare usage of non-RSA keys - TestKeyStore testKeyStore - = TestKeyStore.create(new String[] { "RSA", "DSA", "EC", "EC_RSA" }, - null, - null, - "rsa-dsa-ec", - TestKeyStore.localhost(), - 0, - true, - null, - null); + TestKeyStore testKeyStore = new TestKeyStore.Builder() + .keyAlgorithms("RSA", "DSA", "EC", "EC_RSA") + .aliasPrefix("rsa-dsa-ec") + .ca(true) + .build(); test_SSLEngine_getSupportedCipherSuites_connect(testKeyStore, false); if (StandardNames.IS_RI) { test_SSLEngine_getSupportedCipherSuites_connect(testKeyStore, true); diff --git a/luni/src/test/java/libcore/javax/net/ssl/SSLSocketTest.java b/luni/src/test/java/libcore/javax/net/ssl/SSLSocketTest.java index 35346a2..7ca5a63 100644 --- a/luni/src/test/java/libcore/javax/net/ssl/SSLSocketTest.java +++ b/luni/src/test/java/libcore/javax/net/ssl/SSLSocketTest.java @@ -55,16 +55,11 @@ public class SSLSocketTest extends TestCase { public void test_SSLSocket_getSupportedCipherSuites_connect() throws Exception { // note the rare usage of non-RSA keys - TestKeyStore testKeyStore - = TestKeyStore.create(new String[] { "RSA", "DSA", "EC", "EC_RSA" }, - null, - null, - "rsa-dsa-ec", - TestKeyStore.localhost(), - 0, - true, - null, - null); + TestKeyStore testKeyStore = new TestKeyStore.Builder() + .keyAlgorithms("RSA", "DSA", "EC", "EC_RSA") + .aliasPrefix("rsa-dsa-ec") + .ca(true) + .build(); if (StandardNames.IS_RI) { test_SSLSocket_getSupportedCipherSuites_connect(testKeyStore, StandardNames.JSSE_PROVIDER_NAME, @@ -382,9 +377,12 @@ public class SSLSocketTest extends TestCase { if (false) { System.out.println("Session=" + session); System.out.println("CipherSuite=" + cipherSuite); - System.out.println("LocalCertificates=" + localCertificates); - System.out.println("PeerCertificates=" + peerCertificates); - System.out.println("PeerCertificateChain=" + peerCertificateChain); + System.out.println("LocalCertificates=" + + Arrays.toString(localCertificates)); + System.out.println("PeerCertificates=" + + Arrays.toString(peerCertificates)); + System.out.println("PeerCertificateChain=" + + Arrays.toString(peerCertificateChain)); System.out.println("PeerPrincipal=" + peerPrincipal); System.out.println("LocalPrincipal=" + localPrincipal); System.out.println("Socket=" + socket); @@ -1058,7 +1056,7 @@ public class SSLSocketTest extends TestCase { /** * Not run by default by JUnit, but can be run by Vogar by - * specifying it explictly (or with main method below) + * specifying it explicitly (or with main method below) */ public void stress_test_TestSSLSocketPair_create() { final boolean verbose = true; @@ -1081,7 +1079,7 @@ public class SSLSocketTest extends TestCase { } } - public static final void main (String[] args) { + public static void main (String[] args) { new SSLSocketTest().stress_test_TestSSLSocketPair_create(); } } diff --git a/luni/src/test/java/libcore/javax/net/ssl/TrustManagerFactoryTest.java b/luni/src/test/java/libcore/javax/net/ssl/TrustManagerFactoryTest.java index e92bf67..b82b4d2 100644 --- a/luni/src/test/java/libcore/javax/net/ssl/TrustManagerFactoryTest.java +++ b/luni/src/test/java/libcore/javax/net/ssl/TrustManagerFactoryTest.java @@ -17,11 +17,10 @@ package libcore.javax.net.ssl; import java.security.InvalidAlgorithmParameterException; -import java.security.KeyStore.PrivateKeyEntry; import java.security.KeyStore; +import java.security.KeyStore.PrivateKeyEntry; import java.security.Provider; import java.security.Security; -import java.security.cert.Certificate; import java.security.cert.CertificateException; import java.security.cert.PKIXBuilderParameters; import java.security.cert.PKIXParameters; @@ -41,16 +40,18 @@ public class TrustManagerFactoryTest extends TestCase { private static final String [] KEY_TYPES = new String[] { "RSA", "DSA", "EC", "EC_RSA" }; // note the rare usage of DSA keys here in addition to RSA - private static final TestKeyStore TEST_KEY_STORE - = TestKeyStore.create(KEY_TYPES, - null, - null, - "rsa-dsa-ec", - TestKeyStore.localhost(), - 0, - true, - null, - null); + private static final TestKeyStore TEST_KEY_STORE; + static { + try { + TEST_KEY_STORE = new TestKeyStore.Builder() + .keyAlgorithms(KEY_TYPES) + .aliasPrefix("rsa-dsa-ec") + .ca(true) + .build(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } public void test_TrustManagerFactory_getDefaultAlgorithm() throws Exception { String algorithm = TrustManagerFactory.getDefaultAlgorithm(); @@ -59,7 +60,7 @@ public class TrustManagerFactoryTest extends TestCase { test_TrustManagerFactory(tmf, StandardNames.IS_RI); } - private static class UseslessManagerFactoryParameters implements ManagerFactoryParameters {} + private static class UselessManagerFactoryParameters implements ManagerFactoryParameters {} private void test_TrustManagerFactory(TrustManagerFactory tmf, boolean supportsManagerFactoryParameters) @@ -84,7 +85,7 @@ public class TrustManagerFactoryTest extends TestCase { // init with useless ManagerFactoryParameters try { - tmf.init(new UseslessManagerFactoryParameters()); + tmf.init(new UselessManagerFactoryParameters()); fail(); } catch (InvalidAlgorithmParameterException expected) { } @@ -141,7 +142,7 @@ public class TrustManagerFactoryTest extends TestCase { assertNotNull(issuers); assertTrue(issuers.length > 1); assertNotSame(issuers, tm.getAcceptedIssuers()); - boolean defaultTrustmanager + boolean defaultTrustManager // RI de-duplicates certs from TrustedCertificateEntry and PrivateKeyEntry = issuers.length > (StandardNames.IS_RI ? 1 : 2) * KEY_TYPES.length; @@ -149,7 +150,7 @@ public class TrustManagerFactoryTest extends TestCase { String sigAlgName = TestKeyStore.signatureAlgorithm(keyType); PrivateKeyEntry pke = TEST_KEY_STORE.getPrivateKey(keyAlgName, sigAlgName); X509Certificate[] chain = (X509Certificate[]) pke.getCertificateChain(); - if (defaultTrustmanager) { + if (defaultTrustManager) { try { tm.checkClientTrusted(chain, keyType); fail(); -- cgit v1.1