diff options
11 files changed, 1081 insertions, 1258 deletions
diff --git a/luni/src/test/java/libcore/java/security/KeyStoreTest.java b/luni/src/test/java/libcore/java/security/KeyStoreTest.java index af66ded..0a4d966 100644 --- a/luni/src/test/java/libcore/java/security/KeyStoreTest.java +++ b/luni/src/test/java/libcore/java/security/KeyStoreTest.java @@ -54,22 +54,11 @@ import libcore.io.IoUtils; public class KeyStoreTest extends TestCase { - private static final PrivateKeyEntry PRIVATE_KEY - = TestKeyStore.getServer().getPrivateKey("RSA", "RSA"); - private static final PrivateKeyEntry PRIVATE_KEY_2 - = TestKeyStore.getClientCertificate().getPrivateKey("RSA", "RSA"); + private static PrivateKeyEntry PRIVATE_KEY; + private static PrivateKeyEntry PRIVATE_KEY_2; - private static final SecretKey SECRET_KEY = generateSecretKey(); - private static final SecretKey SECRET_KEY_2 = generateSecretKey(); - - private static SecretKey generateSecretKey() { - try { - KeyGenerator kg = KeyGenerator.getInstance("DES"); - return kg.generateKey(); - } catch (NoSuchAlgorithmException e) { - throw new RuntimeException(e); - } - } + private static SecretKey SECRET_KEY; + private static SecretKey SECRET_KEY_2; private static final String ALIAS_PRIVATE = "private"; private static final String ALIAS_CERTIFICATE = "certificate"; @@ -90,6 +79,43 @@ public class KeyStoreTest extends TestCase { private static final ProtectionParameter PARAM_KEY = new PasswordProtection(PASSWORD_KEY); private static final ProtectionParameter PARAM_BAD = new PasswordProtection(PASSWORD_BAD); + private static PrivateKeyEntry getPrivateKey() { + if (PRIVATE_KEY == null) { + PRIVATE_KEY = TestKeyStore.getServer().getPrivateKey("RSA", "RSA"); + } + return PRIVATE_KEY; + } + + private static PrivateKeyEntry getPrivateKey2() { + if (PRIVATE_KEY_2 == null) { + PRIVATE_KEY_2 = TestKeyStore.getClientCertificate().getPrivateKey("RSA", "RSA"); + } + return PRIVATE_KEY_2; + } + + private static SecretKey getSecretKey() { + if (SECRET_KEY == null) { + SECRET_KEY = generateSecretKey(); + } + return SECRET_KEY; + } + + private static SecretKey getSecretKey2() { + if (SECRET_KEY_2 == null) { + SECRET_KEY_2 = generateSecretKey(); + } + return SECRET_KEY_2; + } + + private static SecretKey generateSecretKey() { + try { + KeyGenerator kg = KeyGenerator.getInstance("DES"); + return kg.generateKey(); + } catch (NoSuchAlgorithmException e) { + throw new RuntimeException(e); + } + } + public static List<KeyStore> keyStores() throws Exception { List<KeyStore> keyStores = new ArrayList<KeyStore>(); Provider[] providers = Security.getProviders(); @@ -185,19 +211,19 @@ public class KeyStoreTest extends TestCase { setPrivateKey(ks); if (isNullPasswordAllowed(ks)) { ks.setKeyEntry(ALIAS_NO_PASSWORD_PRIVATE, - PRIVATE_KEY.getPrivateKey(), + getPrivateKey().getPrivateKey(), null, - PRIVATE_KEY.getCertificateChain()); + getPrivateKey().getCertificateChain()); } if (isCertificateEnabled(ks)) { ks.setCertificateEntry(ALIAS_CERTIFICATE, - PRIVATE_KEY.getCertificate()); + getPrivateKey().getCertificate()); } if (isSecretKeyEnabled(ks)) { setSecretKey(ks); if (isNullPasswordAllowed(ks)) { ks.setKeyEntry(ALIAS_NO_PASSWORD_SECRET, - SECRET_KEY, + getSecretKey(), null, null); } @@ -208,7 +234,7 @@ public class KeyStoreTest extends TestCase { setPrivateKey(ks, ALIAS_PRIVATE); } public static void setPrivateKey(KeyStore ks, String alias) throws Exception { - setPrivateKey(ks, alias, PRIVATE_KEY); + setPrivateKey(ks, alias, getPrivateKey()); } public static void setPrivateKey(KeyStore ks, String alias, @@ -224,7 +250,7 @@ public class KeyStoreTest extends TestCase { setPrivateKeyBytes(ks, ALIAS_PRIVATE); } public static void setPrivateKeyBytes(KeyStore ks, String alias) throws Exception { - setPrivateKeyBytes(ks, alias, PRIVATE_KEY); + setPrivateKeyBytes(ks, alias, getPrivateKey()); } public static void setPrivateKeyBytes(KeyStore ks, String alias, @@ -239,7 +265,7 @@ public class KeyStoreTest extends TestCase { setSecretKey(ks, ALIAS_SECRET); } public static void setSecretKey(KeyStore ks, String alias) throws Exception { - setSecretKey(ks, alias, SECRET_KEY); + setSecretKey(ks, alias, getSecretKey()); } public static void setSecretKey(KeyStore ks, String alias, SecretKey key) throws Exception { ks.setKeyEntry(alias, @@ -252,7 +278,7 @@ public class KeyStoreTest extends TestCase { setSecretKeyBytes(ks, ALIAS_SECRET); } public static void setSecretKeyBytes(KeyStore ks, String alias) throws Exception { - setSecretKeyBytes(ks, alias, SECRET_KEY); + setSecretKeyBytes(ks, alias, getSecretKey()); } public static void setSecretKeyBytes(KeyStore ks, String alias, SecretKey key) throws Exception { @@ -265,7 +291,7 @@ public class KeyStoreTest extends TestCase { setCertificate(ks, ALIAS_CERTIFICATE); } public static void setCertificate(KeyStore ks, String alias) throws Exception { - setCertificate(ks, alias, PRIVATE_KEY.getCertificate()); + setCertificate(ks, alias, getPrivateKey().getCertificate()); } public static void setCertificate(KeyStore ks, String alias, Certificate certificate) throws Exception { @@ -274,55 +300,55 @@ public class KeyStoreTest extends TestCase { public static void assertPrivateKey(Key actual) throws Exception { - assertEquals(PRIVATE_KEY.getPrivateKey(), actual); + assertEquals(getPrivateKey().getPrivateKey(), actual); } public static void assertPrivateKey2(Key actual) throws Exception { - assertEquals(PRIVATE_KEY_2.getPrivateKey(), actual); + assertEquals(getPrivateKey2().getPrivateKey(), actual); } public static void assertPrivateKey(Entry actual) throws Exception { assertNotNull(actual); assertSame(PrivateKeyEntry.class, actual.getClass()); PrivateKeyEntry privateKey = (PrivateKeyEntry) actual; - assertEquals(PRIVATE_KEY.getPrivateKey(), privateKey.getPrivateKey()); - assertEquals(PRIVATE_KEY.getCertificate(), privateKey.getCertificate()); - assertEquals(Arrays.asList(PRIVATE_KEY.getCertificateChain()), + assertEquals(getPrivateKey().getPrivateKey(), privateKey.getPrivateKey()); + assertEquals(getPrivateKey().getCertificate(), privateKey.getCertificate()); + assertEquals(Arrays.asList(getPrivateKey().getCertificateChain()), Arrays.asList(privateKey.getCertificateChain())); } public static void assertSecretKey(Key actual) throws Exception { - assertEquals(SECRET_KEY, actual); + assertEquals(getSecretKey(), actual); } public static void assertSecretKey2(Key actual) throws Exception { - assertEquals(SECRET_KEY_2, actual); + assertEquals(getSecretKey2(), actual); } public static void assertSecretKey(Entry actual) throws Exception { assertSame(SecretKeyEntry.class, actual.getClass()); - assertEquals(SECRET_KEY, ((SecretKeyEntry) actual).getSecretKey()); + assertEquals(getSecretKey(), ((SecretKeyEntry) actual).getSecretKey()); } public static void assertCertificate(Certificate actual) throws Exception { - assertEquals(PRIVATE_KEY.getCertificate(), actual); + assertEquals(getPrivateKey().getCertificate(), actual); } public static void assertCertificate2(Certificate actual) throws Exception { - assertEquals(PRIVATE_KEY_2.getCertificate(), actual); + assertEquals(getPrivateKey2().getCertificate(), actual); } public static void assertCertificate(Entry actual) throws Exception { assertSame(TrustedCertificateEntry.class, actual.getClass()); - assertEquals(PRIVATE_KEY.getCertificate(), + assertEquals(getPrivateKey().getCertificate(), ((TrustedCertificateEntry) actual).getTrustedCertificate()); } public static void assertCertificateChain(Certificate[] actual) throws Exception { - assertEquals(Arrays.asList(PRIVATE_KEY.getCertificateChain()), + assertEquals(Arrays.asList(getPrivateKey().getCertificateChain()), Arrays.asList(actual)); } @@ -703,7 +729,7 @@ public class KeyStoreTest extends TestCase { } try { keyStore.setKeyEntry(ALIAS_PRIVATE, - PRIVATE_KEY.getPrivateKey(), + getPrivateKey().getPrivateKey(), PASSWORD_KEY, null); fail(); @@ -718,7 +744,7 @@ public class KeyStoreTest extends TestCase { assertNull(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY)); if (isReadOnly(keyStore)) { try { - keyStore.setKeyEntry(ALIAS_SECRET, SECRET_KEY, PASSWORD_KEY, null); + keyStore.setKeyEntry(ALIAS_SECRET, getSecretKey(), PASSWORD_KEY, null); fail(); } catch (UnsupportedOperationException expected) { } @@ -733,7 +759,7 @@ public class KeyStoreTest extends TestCase { assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY)); } else { try { - keyStore.setKeyEntry(ALIAS_SECRET, SECRET_KEY, PASSWORD_KEY, null); + keyStore.setKeyEntry(ALIAS_SECRET, getSecretKey(), PASSWORD_KEY, null); fail(); } catch (Exception e) { if (e.getClass() != KeyStoreException.class @@ -755,27 +781,27 @@ public class KeyStoreTest extends TestCase { } else if (isCaseSensitive(keyStore)) { assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY)); assertNull(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY)); - setPrivateKey(keyStore, ALIAS_ALT_CASE_PRIVATE, PRIVATE_KEY_2); + setPrivateKey(keyStore, ALIAS_ALT_CASE_PRIVATE, getPrivateKey2()); assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY)); assertPrivateKey2(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY)); if (isSecretKeyEnabled(keyStore)) { assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY)); assertNull(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY)); - setSecretKey(keyStore, ALIAS_ALT_CASE_SECRET, SECRET_KEY_2); + setSecretKey(keyStore, ALIAS_ALT_CASE_SECRET, getSecretKey2()); assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY)); assertSecretKey2(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY)); } } else { assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY)); assertPrivateKey(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY)); - setPrivateKey(keyStore, ALIAS_ALT_CASE_PRIVATE, PRIVATE_KEY_2); + setPrivateKey(keyStore, ALIAS_ALT_CASE_PRIVATE, getPrivateKey2()); assertPrivateKey2(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY)); assertPrivateKey2(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY)); if (isSecretKeyEnabled(keyStore)) { assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY)); assertSecretKey(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY)); - setSecretKey(keyStore, ALIAS_ALT_CASE_PRIVATE, SECRET_KEY_2); + setSecretKey(keyStore, ALIAS_ALT_CASE_PRIVATE, getSecretKey2()); assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY)); assertSecretKey(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY)); } @@ -787,9 +813,9 @@ public class KeyStoreTest extends TestCase { if (isReadOnly(keyStore)) { try { keyStore.setKeyEntry(ALIAS_PRIVATE, - PRIVATE_KEY.getPrivateKey(), + getPrivateKey().getPrivateKey(), null, - PRIVATE_KEY.getCertificateChain()); + getPrivateKey().getCertificateChain()); fail(); } catch (UnsupportedOperationException expected) { } @@ -799,16 +825,16 @@ public class KeyStoreTest extends TestCase { // test with null passwords if (isNullPasswordAllowed(keyStore) || isKeyPasswordIgnored(keyStore)) { keyStore.setKeyEntry(ALIAS_PRIVATE, - PRIVATE_KEY.getPrivateKey(), + getPrivateKey().getPrivateKey(), null, - PRIVATE_KEY.getCertificateChain()); + getPrivateKey().getCertificateChain()); assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, null)); } else { try { keyStore.setKeyEntry(ALIAS_PRIVATE, - PRIVATE_KEY.getPrivateKey(), + getPrivateKey().getPrivateKey(), null, - PRIVATE_KEY.getCertificateChain()); + getPrivateKey().getCertificateChain()); fail(); } catch (Exception e) { if (e.getClass() != UnrecoverableKeyException.class @@ -820,11 +846,11 @@ public class KeyStoreTest extends TestCase { } if (isSecretKeyEnabled(keyStore)) { if (isNullPasswordAllowed(keyStore) || isKeyPasswordIgnored(keyStore)) { - keyStore.setKeyEntry(ALIAS_SECRET, SECRET_KEY, null, null); + keyStore.setKeyEntry(ALIAS_SECRET, getSecretKey(), null, null); assertSecretKey(keyStore.getKey(ALIAS_SECRET, null)); } else { try { - keyStore.setKeyEntry(ALIAS_SECRET, SECRET_KEY, null, null); + keyStore.setKeyEntry(ALIAS_SECRET, getSecretKey(), null, null); fail(); } catch (Exception e) { if (e.getClass() != UnrecoverableKeyException.class @@ -904,7 +930,7 @@ public class KeyStoreTest extends TestCase { assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY)); } else { try { - keyStore.setKeyEntry(ALIAS_SECRET, SECRET_KEY.getEncoded(), null); + keyStore.setKeyEntry(ALIAS_SECRET, getSecretKey().getEncoded(), null); fail(); } catch (KeyStoreException expected) { } @@ -931,28 +957,28 @@ public class KeyStoreTest extends TestCase { } else if (isCaseSensitive(keyStore)) { assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY)); assertNull(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY)); - setPrivateKeyBytes(keyStore, ALIAS_ALT_CASE_PRIVATE, PRIVATE_KEY_2); + setPrivateKeyBytes(keyStore, ALIAS_ALT_CASE_PRIVATE, getPrivateKey2()); assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY)); assertPrivateKey2(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY)); if (isSecretKeyEnabled(keyStore)) { assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY)); assertNull(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY)); - setSecretKeyBytes(keyStore, ALIAS_ALT_CASE_PRIVATE, SECRET_KEY_2); + setSecretKeyBytes(keyStore, ALIAS_ALT_CASE_PRIVATE, getSecretKey2()); assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY)); assertSecretKey2(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY)); } } else { assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY)); assertPrivateKey(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY)); - setPrivateKeyBytes(keyStore, ALIAS_ALT_CASE_PRIVATE, PRIVATE_KEY_2); + setPrivateKeyBytes(keyStore, ALIAS_ALT_CASE_PRIVATE, getPrivateKey2()); assertPrivateKey2(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY)); assertPrivateKey2(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY)); if (isSecretKeyEnabled(keyStore)) { assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY)); assertSecretKey(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY)); - setSecretKeyBytes(keyStore, ALIAS_ALT_CASE_PRIVATE, SECRET_KEY_2); + setSecretKeyBytes(keyStore, ALIAS_ALT_CASE_PRIVATE, getSecretKey2()); assertSecretKey2(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY)); assertSecretKey2(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY)); } @@ -1056,7 +1082,7 @@ public class KeyStoreTest extends TestCase { assertNull(keyStore.getCertificate(ALIAS_ALT_CASE_CERTIFICATE)); setCertificate(keyStore, ALIAS_ALT_CASE_CERTIFICATE, - PRIVATE_KEY_2.getCertificate()); + getPrivateKey2().getCertificate()); assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE)); assertCertificate2(keyStore.getCertificate(ALIAS_ALT_CASE_CERTIFICATE)); } else { @@ -1064,7 +1090,7 @@ public class KeyStoreTest extends TestCase { assertCertificate(keyStore.getCertificate(ALIAS_ALT_CASE_CERTIFICATE)); setCertificate(keyStore, ALIAS_ALT_CASE_CERTIFICATE, - PRIVATE_KEY_2.getCertificate()); + getPrivateKey2().getCertificate()); assertCertificate2(keyStore.getCertificate(ALIAS_CERTIFICATE)); assertCertificate2(keyStore.getCertificate(ALIAS_ALT_CASE_CERTIFICATE)); } @@ -1406,9 +1432,9 @@ public class KeyStoreTest extends TestCase { if (isCertificateEnabled(keyStore)) { expected.add(ALIAS_CERTIFICATE); } - String actual = keyStore.getCertificateAlias(PRIVATE_KEY.getCertificate()); + String actual = keyStore.getCertificateAlias(getPrivateKey().getCertificate()); assertEquals(!isReadOnly(keyStore), expected.contains(actual)); - assertNull(keyStore.getCertificateAlias(PRIVATE_KEY_2.getCertificate())); + assertNull(keyStore.getCertificateAlias(getPrivateKey2().getCertificate())); } } @@ -1771,22 +1797,22 @@ public class KeyStoreTest extends TestCase { assertNull(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY)); if (isReadOnly(keyStore)) { try { - keyStore.setEntry(ALIAS_PRIVATE, PRIVATE_KEY, PARAM_KEY); + keyStore.setEntry(ALIAS_PRIVATE, getPrivateKey(), PARAM_KEY); fail(); } catch (UnsupportedOperationException expected) { } continue; } - keyStore.setEntry(ALIAS_PRIVATE, PRIVATE_KEY, PARAM_KEY); + keyStore.setEntry(ALIAS_PRIVATE, getPrivateKey(), PARAM_KEY); assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY)); assertCertificateChain(keyStore.getCertificateChain(ALIAS_PRIVATE)); if (isSecretKeyEnabled(keyStore)) { assertNull(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY)); - keyStore.setEntry(ALIAS_SECRET, new SecretKeyEntry(SECRET_KEY), PARAM_KEY); + keyStore.setEntry(ALIAS_SECRET, new SecretKeyEntry(getSecretKey()), PARAM_KEY); assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY)); } else { try { - keyStore.setKeyEntry(ALIAS_SECRET, SECRET_KEY, PASSWORD_KEY, null); + keyStore.setKeyEntry(ALIAS_SECRET, getSecretKey(), PASSWORD_KEY, null); fail(); } catch (KeyStoreException expected) { } @@ -1794,13 +1820,13 @@ public class KeyStoreTest extends TestCase { if (isCertificateEnabled(keyStore)) { assertNull(keyStore.getCertificate(ALIAS_CERTIFICATE)); keyStore.setEntry(ALIAS_CERTIFICATE, - new TrustedCertificateEntry(PRIVATE_KEY.getCertificate()), + new TrustedCertificateEntry(getPrivateKey().getCertificate()), null); assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE)); } else { try { keyStore.setEntry(ALIAS_CERTIFICATE, - new TrustedCertificateEntry(PRIVATE_KEY.getCertificate()), + new TrustedCertificateEntry(getPrivateKey().getCertificate()), null); fail(); } catch (KeyStoreException expected) { @@ -1819,7 +1845,7 @@ public class KeyStoreTest extends TestCase { } else if (isCaseSensitive(keyStore)) { assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY)); assertNull(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY)); - keyStore.setEntry(ALIAS_ALT_CASE_PRIVATE, PRIVATE_KEY_2, PARAM_KEY); + keyStore.setEntry(ALIAS_ALT_CASE_PRIVATE, getPrivateKey2(), PARAM_KEY); assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY)); assertPrivateKey2(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY)); @@ -1827,7 +1853,7 @@ public class KeyStoreTest extends TestCase { assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY)); assertNull(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY)); keyStore.setEntry(ALIAS_ALT_CASE_SECRET, - new SecretKeyEntry(SECRET_KEY_2), + new SecretKeyEntry(getSecretKey2()), PARAM_KEY); assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY)); assertSecretKey2(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY)); @@ -1837,7 +1863,8 @@ public class KeyStoreTest extends TestCase { assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE)); assertNull(keyStore.getCertificate(ALIAS_ALT_CASE_CERTIFICATE)); keyStore.setEntry(ALIAS_ALT_CASE_CERTIFICATE, - new TrustedCertificateEntry(PRIVATE_KEY_2.getCertificate()), + new TrustedCertificateEntry( + getPrivateKey2().getCertificate()), null); assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE)); assertCertificate2(keyStore.getCertificate(ALIAS_ALT_CASE_CERTIFICATE)); @@ -1845,7 +1872,7 @@ public class KeyStoreTest extends TestCase { } else { assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY)); assertPrivateKey(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY)); - keyStore.setEntry(ALIAS_ALT_CASE_PRIVATE, PRIVATE_KEY_2, PARAM_KEY); + keyStore.setEntry(ALIAS_ALT_CASE_PRIVATE, getPrivateKey2(), PARAM_KEY); assertPrivateKey2(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY)); assertPrivateKey2(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY)); @@ -1853,7 +1880,7 @@ public class KeyStoreTest extends TestCase { assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY)); assertSecretKey(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY)); keyStore.setEntry(ALIAS_ALT_CASE_SECRET, - new SecretKeyEntry(SECRET_KEY_2), + new SecretKeyEntry(getSecretKey2()), PARAM_KEY); assertSecretKey2(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY)); assertSecretKey2(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY)); @@ -1863,7 +1890,8 @@ public class KeyStoreTest extends TestCase { assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE)); assertCertificate(keyStore.getCertificate(ALIAS_ALT_CASE_CERTIFICATE)); keyStore.setEntry(ALIAS_ALT_CASE_CERTIFICATE, - new TrustedCertificateEntry(PRIVATE_KEY_2.getCertificate()), + new TrustedCertificateEntry( + getPrivateKey2().getCertificate()), null); assertCertificate2(keyStore.getCertificate(ALIAS_CERTIFICATE)); assertCertificate2(keyStore.getCertificate(ALIAS_ALT_CASE_CERTIFICATE)); @@ -1876,7 +1904,7 @@ public class KeyStoreTest extends TestCase { // test with null/non-null passwords try { - keyStore.setEntry(ALIAS_PRIVATE, PRIVATE_KEY, null); + keyStore.setEntry(ALIAS_PRIVATE, getPrivateKey(), null); fail(); } catch (Exception e) { if (e.getClass() != UnrecoverableKeyException.class @@ -1887,7 +1915,7 @@ public class KeyStoreTest extends TestCase { } if (isSecretKeyEnabled(keyStore)) { try { - keyStore.setEntry(ALIAS_SECRET, new SecretKeyEntry(SECRET_KEY), null); + keyStore.setEntry(ALIAS_SECRET, new SecretKeyEntry(getSecretKey()), null); fail(); } catch (Exception e) { if (e.getClass() != UnrecoverableKeyException.class @@ -1900,7 +1928,7 @@ public class KeyStoreTest extends TestCase { if (isReadOnly(keyStore)) { try { keyStore.setEntry(ALIAS_CERTIFICATE, - new TrustedCertificateEntry(PRIVATE_KEY.getCertificate()), + new TrustedCertificateEntry(getPrivateKey().getCertificate()), PARAM_KEY); fail(); } catch (UnsupportedOperationException expected) { @@ -1910,13 +1938,14 @@ public class KeyStoreTest extends TestCase { if (isCertificateEnabled(keyStore)) { if (isNullPasswordAllowed(keyStore) || isKeyPasswordIgnored(keyStore)) { keyStore.setEntry(ALIAS_CERTIFICATE, - new TrustedCertificateEntry(PRIVATE_KEY.getCertificate()), + new TrustedCertificateEntry(getPrivateKey().getCertificate()), PARAM_KEY); assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE)); } else { try { keyStore.setEntry(ALIAS_CERTIFICATE, - new TrustedCertificateEntry(PRIVATE_KEY.getCertificate()), + new TrustedCertificateEntry( + getPrivateKey().getCertificate()), PARAM_KEY); fail(); } catch (KeyStoreException expected) { 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 a6bdc07..f2d36c8 100644 --- a/luni/src/test/java/libcore/javax/net/ssl/KeyManagerFactoryTest.java +++ b/luni/src/test/java/libcore/javax/net/ssl/KeyManagerFactoryTest.java @@ -39,18 +39,17 @@ 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; + private static TestKeyStore TEST_KEY_STORE; - static { - try { + // note the rare usage of DSA keys here in addition to RSA + private static TestKeyStore getTestKeyStore() throws Exception { + if (TEST_KEY_STORE == null) { TEST_KEY_STORE = new TestKeyStore.Builder() .keyAlgorithms("RSA", "DSA", "EC", "EC_RSA") .aliasPrefix("rsa-dsa-ec") .build(); - } catch (Exception e) { - throw new RuntimeException(e); } + return TEST_KEY_STORE; } public void test_KeyManagerFactory_getDefaultAlgorithm() throws Exception { @@ -102,8 +101,8 @@ public class KeyManagerFactoryTest extends TestCase { } // init with KeyStoreBuilderParameters ManagerFactoryParameters - PasswordProtection pp = new PasswordProtection(TEST_KEY_STORE.storePassword); - Builder builder = Builder.newInstance(TEST_KEY_STORE.keyStore, pp); + PasswordProtection pp = new PasswordProtection(getTestKeyStore().storePassword); + Builder builder = Builder.newInstance(getTestKeyStore().keyStore, pp); KeyStoreBuilderParameters ksbp = new KeyStoreBuilderParameters(builder); if (supportsManagerFactoryParameters(kmf.getAlgorithm())) { kmf.init(ksbp); @@ -121,7 +120,7 @@ public class KeyManagerFactoryTest extends TestCase { test_KeyManagerFactory_getKeyManagers(kmf, true); // init with specific key store and password - kmf.init(TEST_KEY_STORE.keyStore, TEST_KEY_STORE.storePassword); + kmf.init(getTestKeyStore().keyStore, getTestKeyStore().storePassword); test_KeyManagerFactory_getKeyManagers(kmf, false); } @@ -252,7 +251,7 @@ public class KeyManagerFactoryTest extends TestCase { } } - PrivateKeyEntry privateKeyEntry = TEST_KEY_STORE.getPrivateKey(keyAlgName, sigAlgName); + PrivateKeyEntry privateKeyEntry = getTestKeyStore().getPrivateKey(keyAlgName, sigAlgName); if (!"EC".equals(keyAlgName)) { assertEquals(keyType, Arrays.<Certificate>asList(privateKeyEntry.getCertificateChain()), 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 b82b4d2..8a3fe25 100644 --- a/luni/src/test/java/libcore/javax/net/ssl/TrustManagerFactoryTest.java +++ b/luni/src/test/java/libcore/javax/net/ssl/TrustManagerFactoryTest.java @@ -39,18 +39,18 @@ import libcore.java.security.TestKeyStore; public class TrustManagerFactoryTest extends TestCase { private static final String [] KEY_TYPES = new String[] { "RSA", "DSA", "EC", "EC_RSA" }; + + private static TestKeyStore TEST_KEY_STORE; + // note the rare usage of DSA keys here in addition to RSA - private static final TestKeyStore TEST_KEY_STORE; - static { - try { + private static TestKeyStore getTestKeyStore() throws Exception { + if (TEST_KEY_STORE == null) { TEST_KEY_STORE = new TestKeyStore.Builder() .keyAlgorithms(KEY_TYPES) .aliasPrefix("rsa-dsa-ec") - .ca(true) .build(); - } catch (Exception e) { - throw new RuntimeException(e); } + return TEST_KEY_STORE; } public void test_TrustManagerFactory_getDefaultAlgorithm() throws Exception { @@ -92,7 +92,7 @@ public class TrustManagerFactoryTest extends TestCase { // init with PKIXParameters ManagerFactoryParameters try { - PKIXParameters pp = new PKIXParameters(TEST_KEY_STORE.keyStore); + PKIXParameters pp = new PKIXParameters(getTestKeyStore().keyStore); CertPathTrustManagerParameters cptmp = new CertPathTrustManagerParameters(pp); tmf.init(cptmp); fail(); @@ -101,7 +101,7 @@ public class TrustManagerFactoryTest extends TestCase { // init with PKIXBuilderParameters ManagerFactoryParameters X509CertSelector xcs = new X509CertSelector(); - PKIXBuilderParameters pbp = new PKIXBuilderParameters(TEST_KEY_STORE.keyStore, xcs); + PKIXBuilderParameters pbp = new PKIXBuilderParameters(getTestKeyStore().keyStore, xcs); CertPathTrustManagerParameters cptmp = new CertPathTrustManagerParameters(pbp); if (supportsManagerFactoryParameters) { tmf.init(cptmp); @@ -119,7 +119,7 @@ public class TrustManagerFactoryTest extends TestCase { test_TrustManagerFactory_getTrustManagers(tmf); // init with specific key store - tmf.init(TEST_KEY_STORE.keyStore); + tmf.init(getTestKeyStore().keyStore); test_TrustManagerFactory_getTrustManagers(tmf); } @@ -148,7 +148,7 @@ public class TrustManagerFactoryTest extends TestCase { String keyAlgName = TestKeyStore.keyAlgorithm(keyType); String sigAlgName = TestKeyStore.signatureAlgorithm(keyType); - PrivateKeyEntry pke = TEST_KEY_STORE.getPrivateKey(keyAlgName, sigAlgName); + PrivateKeyEntry pke = getTestKeyStore().getPrivateKey(keyAlgName, sigAlgName); X509Certificate[] chain = (X509Certificate[]) pke.getCertificateChain(); if (defaultTrustManager) { try { @@ -245,7 +245,7 @@ public class TrustManagerFactoryTest extends TestCase { // create a KeyStore containing only a private key with chain. // unlike PKIXParameters(KeyStore), the cert chain of the key should be trusted. KeyStore ks = TestKeyStore.createKeyStore(); - KeyStore.PrivateKeyEntry pke = TEST_KEY_STORE.getPrivateKey("RSA", "RSA"); + KeyStore.PrivateKeyEntry pke = getTestKeyStore().getPrivateKey("RSA", "RSA"); ks.setKeyEntry("key", pke.getPrivateKey(), "pw".toCharArray(), pke.getCertificateChain()); String algorithm = TrustManagerFactory.getDefaultAlgorithm(); diff --git a/luni/src/test/java/org/apache/harmony/security/tests/java/security/Identity2Test.java b/luni/src/test/java/org/apache/harmony/security/tests/java/security/Identity2Test.java index c1a7ac4..3625dee 100644 --- a/luni/src/test/java/org/apache/harmony/security/tests/java/security/Identity2Test.java +++ b/luni/src/test/java/org/apache/harmony/security/tests/java/security/Identity2Test.java @@ -17,10 +17,10 @@ package org.apache.harmony.security.tests.java.security; +import dalvik.annotation.AndroidOnly; import java.io.ByteArrayInputStream; import java.io.InputStream; import java.io.OutputStream; -import java.io.UnsupportedEncodingException; import java.security.Certificate; import java.security.Identity; import java.security.IdentityScope; @@ -30,21 +30,17 @@ import java.security.Principal; import java.security.PublicKey; import java.security.cert.CertificateFactory; import java.security.cert.X509Certificate; - import org.apache.harmony.security.tests.java.security.IdentityScope2Test.IdentityScopeSubclass; -import dalvik.annotation.AndroidOnly; - @SuppressWarnings("deprecation") public class Identity2Test extends junit.framework.TestCase { - static PublicKey pubKey; - static { - try { - pubKey = KeyPairGenerator.getInstance("DSA").genKeyPair().getPublic(); - } catch (Exception e) { - fail(e.toString()); + private static PublicKey PUB_KEY; + private static PublicKey getPubKey() throws Exception { + if (PUB_KEY == null) { + PUB_KEY = KeyPairGenerator.getInstance("DSA").genKeyPair().getPublic(); } + return PUB_KEY; } public static class CertificateImpl implements java.security.Certificate { @@ -102,8 +98,7 @@ public class Identity2Test extends junit.framework.TestCase { + "1777XQ9UEZyrKJvF5ntleeO0ayBqLGVKCWzWZX9YsXCpv47FNLZbupE=\n" + "-----END CERTIFICATE-----\n"; - ByteArrayInputStream certArray = new ByteArrayInputStream(certificate - .getBytes()); + ByteArrayInputStream certArray = new ByteArrayInputStream(certificate.getBytes()); String certificate2 = "-----BEGIN CERTIFICATE-----\n" + "MIICZzCCAdCgAwIBAgIBGzANBgkqhkiG9w0BAQUFADBhMQswCQYDVQQGEwJVUzEY\n" @@ -121,8 +116,7 @@ public class Identity2Test extends junit.framework.TestCase { + "VGQRYYlt+myhl2vy6yPzEVCjiKwMEb1Spu0irCf+lFW2hsdjvmSQMtZvOw==\n" + "-----END CERTIFICATE-----\n"; - ByteArrayInputStream certArray2 = new ByteArrayInputStream(certificate2 - .getBytes()); + ByteArrayInputStream certArray2 = new ByteArrayInputStream(certificate2.getBytes()); public static class IdentitySubclass extends Identity { @@ -161,7 +155,8 @@ public class Identity2Test extends junit.framework.TestCase { assertNotNull(is); assertTrue(is instanceof Identity); } catch (Exception e) { - fail("Unexpected exception for Identity(java.lang.String) with parameter " + str[i]); + fail("Unexpected exception for Identity(java.lang.String) with parameter " + + str[i]); } } } @@ -170,7 +165,7 @@ public class Identity2Test extends junit.framework.TestCase { * java.security.Identity#Identity(java.lang.String, * java.security.IdentityScope) */ - public void test_ConstructorLjava_lang_StringLjava_security_IdentityScope() { + public void test_ConstructorLjava_lang_StringLjava_security_IdentityScope() throws Exception { String nameNull = null; String[] str = {"test", "", "!@#$%^&*()", "identity name"}; IdentityScopeSubclass iss = new IdentityScopeSubclass("name"); @@ -198,10 +193,7 @@ public class Identity2Test extends junit.framework.TestCase { is = new IdentitySubclass("test", iss); is = new IdentitySubclass("test", iss); fail("KeyManagementException was not thrown"); - } catch (KeyManagementException npe) { - //expected - } catch (Exception e) { - fail("Incorrect exception " + e + " was thrown instead of KeyManagementException"); + } catch (KeyManagementException expected) { } } @@ -209,59 +201,59 @@ public class Identity2Test extends junit.framework.TestCase { * java.security.Identity#getScope() */ public void test_getScope() throws Exception { - IdentityScope scope = new IdentityScopeSubclass(); - IdentitySubclass sub = new IdentitySubclass("test", scope); - IdentityScope returnedScope = sub.getScope(); - assertEquals("Wrong Scope returned", scope, returnedScope); + IdentityScope scope = new IdentityScopeSubclass(); + IdentitySubclass sub = new IdentitySubclass("test", scope); + IdentityScope returnedScope = sub.getScope(); + assertEquals("Wrong Scope returned", scope, returnedScope); } /** * java.security.Identity#getPublicKey() */ public void test_getPublicKey() throws Exception { - IdentitySubclass sub = new IdentitySubclass("test", - new IdentityScopeSubclass()); - sub.setPublicKey(pubKey); - PublicKey returnedPubKey = sub.getPublicKey(); - assertEquals("Wrong PublicKey returned", pubKey, returnedPubKey); + IdentitySubclass sub = new IdentitySubclass("test", + new IdentityScopeSubclass()); + sub.setPublicKey(getPubKey()); + PublicKey returnedPubKey = sub.getPublicKey(); + assertEquals("Wrong PublicKey returned", getPubKey(), returnedPubKey); } /** * java.security.Identity#getName() */ public void test_getName() throws Exception { - String name = "test"; - IdentitySubclass sub = new IdentitySubclass(name, - new IdentityScopeSubclass()); - assertEquals("Wrong Name returned", name, sub.getName()); + String name = "test"; + IdentitySubclass sub = new IdentitySubclass(name, + new IdentityScopeSubclass()); + assertEquals("Wrong Name returned", name, sub.getName()); } /** * java.security.Identity#getInfo() */ public void test_getInfo() throws Exception { - String info = "This is the general information."; - IdentitySubclass sub = new IdentitySubclass("test", - new IdentityScopeSubclass()); - sub.setInfo(info); - assertEquals("Wrong Info returned", info, sub.getInfo()); + String info = "This is the general information."; + IdentitySubclass sub = new IdentitySubclass("test", + new IdentityScopeSubclass()); + sub.setInfo(info); + assertEquals("Wrong Info returned", info, sub.getInfo()); } /** * java.security.Identity#certificates() */ public void test_certificates() throws Exception { - IdentitySubclass sub = new IdentitySubclass("test", - new IdentityScopeSubclass()); - CertificateFactory cf = CertificateFactory.getInstance("X.509"); - X509Certificate cert[] = new X509Certificate[1]; - cert[0] = (X509Certificate) cf.generateCertificate(certArray); - sub.setPublicKey(cert[0].getPublicKey()); - CertificateImpl certImpl = new CertificateImpl(cert[0]); - sub.addCertificate(certImpl); - java.security.Certificate[] certs = sub.certificates(); - assertEquals("Certificate not contained in the identity", - certs[0], certImpl); + IdentitySubclass sub = new IdentitySubclass("test", + new IdentityScopeSubclass()); + CertificateFactory cf = CertificateFactory.getInstance("X.509"); + X509Certificate cert[] = new X509Certificate[1]; + cert[0] = (X509Certificate) cf.generateCertificate(certArray); + sub.setPublicKey(cert[0].getPublicKey()); + CertificateImpl certImpl = new CertificateImpl(cert[0]); + sub.addCertificate(certImpl); + java.security.Certificate[] certs = sub.certificates(); + assertEquals("Certificate not contained in the identity", + certs[0], certImpl); } /** @@ -282,8 +274,7 @@ public class Identity2Test extends junit.framework.TestCase { try { sub.removeCertificate(null); fail("Test 1: KeyManagementException expected."); - } catch (KeyManagementException e) { - // Expected. + } catch (KeyManagementException expected) { } assertEquals("Test 2: Certificate should not have been removed.", 1, sub.certificates().length); @@ -296,8 +287,7 @@ public class Identity2Test extends junit.framework.TestCase { try { sub.removeCertificate(certImpl); fail("Test 4: KeyManagementException expected."); - } catch (KeyManagementException e) { - // Expected. + } catch (KeyManagementException expected) { } } @@ -306,78 +296,73 @@ public class Identity2Test extends junit.framework.TestCase { * java.security.Identity#equals(java.lang.Object) */ public void test_equalsLjava_lang_Object() throws Exception { - IdentitySubclass sub = new IdentitySubclass("test", - new IdentityScopeSubclass()); - CertificateFactory cf = CertificateFactory.getInstance("X.509"); - X509Certificate cert[] = new X509Certificate[1]; - cert[0] = (X509Certificate) cf.generateCertificate(certArray); - sub.setPublicKey(cert[0].getPublicKey()); - CertificateImpl certImpl = new CertificateImpl(cert[0]); - sub.addCertificate(certImpl); - IdentitySubclass sub2 = new IdentitySubclass("test", - new IdentityScopeSubclass()); - IdentitySubclass sub3 = new IdentitySubclass("identity name", - new IdentityScopeSubclass()); - assertEquals("the two Identity objects are not equal", sub2, sub); - boolean res1 = sub.equals(sub2); //true - if (!res1) fail("Method equals() should returned TRUE"); - res1 = sub.equals(sub3); //false - if (res1) fail("Method equals() should returned FALSE"); + IdentitySubclass sub = new IdentitySubclass("test", + new IdentityScopeSubclass()); + CertificateFactory cf = CertificateFactory.getInstance("X.509"); + X509Certificate cert[] = new X509Certificate[1]; + cert[0] = (X509Certificate) cf.generateCertificate(certArray); + sub.setPublicKey(cert[0].getPublicKey()); + CertificateImpl certImpl = new CertificateImpl(cert[0]); + sub.addCertificate(certImpl); + IdentitySubclass sub2 = new IdentitySubclass("test", + new IdentityScopeSubclass()); + IdentitySubclass sub3 = new IdentitySubclass("identity name", + new IdentityScopeSubclass()); + assertEquals("the two Identity objects are not equal", sub2, sub); + assertEquals(sub, sub2); + assertFalse(sub.equals(sub3)); } /** * java.security.Identity#identityEquals(java.security.Identity) */ public void test_identityEqualsLjava_security_Identity() throws Exception { - IdentitySubclass sub = new IdentitySubclass("test", null); - CertificateFactory cf = CertificateFactory.getInstance("X.509"); - X509Certificate cert[] = new X509Certificate[1]; - cert[0] = (X509Certificate) cf.generateCertificate(certArray); - sub.setPublicKey(cert[0].getPublicKey()); - CertificateImpl certImpl = new CertificateImpl(cert[0]); - sub.addCertificate(certImpl); - IdentitySubclass sub2 = new IdentitySubclass("test", null); - sub2.setPublicKey(cert[0].getPublicKey()); - assertEquals("the two Identity objects are not identity-equal", - sub2, sub); + IdentitySubclass sub = new IdentitySubclass("test", null); + CertificateFactory cf = CertificateFactory.getInstance("X.509"); + X509Certificate cert[] = new X509Certificate[1]; + cert[0] = (X509Certificate) cf.generateCertificate(certArray); + sub.setPublicKey(cert[0].getPublicKey()); + CertificateImpl certImpl = new CertificateImpl(cert[0]); + sub.addCertificate(certImpl); + IdentitySubclass sub2 = new IdentitySubclass("test", null); + sub2.setPublicKey(cert[0].getPublicKey()); + assertEquals("the two Identity objects are not identity-equal", + sub2, sub); } /** * java.security.Identity#toString() */ public void test_toString() throws Exception { - IdentitySubclass sub = new IdentitySubclass("test", null); - assertNotNull(sub.toString()); - assertTrue("The String returned is not valid", sub.toString() - .length() > 0); - // Regression for HARMONY-1566 - assertNotNull(new IdentitySubclass().toString()); + IdentitySubclass sub = new IdentitySubclass("test", null); + assertNotNull(sub.toString()); + assertTrue("The String returned is not valid", sub.toString().length() > 0); + // Regression for HARMONY-1566 + assertNotNull(new IdentitySubclass().toString()); } /** * java.security.Identity#toString(boolean) */ public void test_toStringZ() throws Exception { - IdentitySubclass sub = new IdentitySubclass("test", null); - assertNotNull(sub.toString(true)); - assertTrue("The String returned is not valid", sub.toString(true) - .length() > 0); + IdentitySubclass sub = new IdentitySubclass("test", null); + assertNotNull(sub.toString(true)); + assertTrue("The String returned is not valid", sub.toString(true).length() > 0); } /** * java.security.Identity#hashCode() */ public void test_hashCode() throws Exception { - IdentitySubclass sub = new IdentitySubclass("test", null); - IdentitySubclass sub2 = new IdentitySubclass("test", null); - assertEquals("The 2 hash codes are not equal", sub.hashCode(), sub2 - .hashCode()); + IdentitySubclass sub = new IdentitySubclass("test", null); + IdentitySubclass sub2 = new IdentitySubclass("test", null); + assertEquals("The 2 hash codes are not equal", sub.hashCode(), sub2.hashCode()); } /** * java.security.Identity#setInfo(String) */ - public void testSetInfo() throws Exception{ + public void testSetInfo() throws Exception { String[] info = {"This is the general information.", "test", "", null}; IdentitySubclass sub = new IdentitySubclass("test", new IdentityScopeSubclass()); @@ -397,13 +382,13 @@ public class Identity2Test extends junit.framework.TestCase { */ public void testSetPublicKey() throws Exception{ IdentitySubclass sub = new IdentitySubclass("test", - new IdentityScopeSubclass()); - sub.setPublicKey(pubKey); - PublicKey returnedPubKey = sub.getPublicKey(); - assertEquals("Wrong PublicKey returned", pubKey, returnedPubKey); + new IdentityScopeSubclass()); + sub.setPublicKey(getPubKey()); + PublicKey returnedPubKey = sub.getPublicKey(); + assertEquals("Wrong PublicKey returned", getPubKey(), returnedPubKey); - sub.setPublicKey(null); - assertEquals("Wrong PublicKey returned", null, sub.getPublicKey()); + sub.setPublicKey(null); + assertEquals("Wrong PublicKey returned", null, sub.getPublicKey()); } } diff --git a/luni/src/test/java/org/apache/harmony/security/tests/java/security/IdentityScope2Test.java b/luni/src/test/java/org/apache/harmony/security/tests/java/security/IdentityScope2Test.java index 3562dce..f68e030 100644 --- a/luni/src/test/java/org/apache/harmony/security/tests/java/security/IdentityScope2Test.java +++ b/luni/src/test/java/org/apache/harmony/security/tests/java/security/IdentityScope2Test.java @@ -30,13 +30,12 @@ import org.apache.harmony.security.tests.java.security.Identity2Test.IdentitySub @SuppressWarnings("deprecation") public class IdentityScope2Test extends junit.framework.TestCase { - static PublicKey pubKey; - static { - try { - pubKey = KeyPairGenerator.getInstance("DSA").genKeyPair().getPublic(); - } catch (Exception e) { - fail(e.toString()); + private static PublicKey PUB_KEY; + private static PublicKey getPubKey() throws Exception { + if (PUB_KEY == null) { + PUB_KEY = KeyPairGenerator.getInstance("DSA").genKeyPair().getPublic(); } + return PUB_KEY; } public static class IdentityScopeSubclass extends IdentityScope { @@ -181,52 +180,50 @@ public class IdentityScope2Test extends junit.framework.TestCase { * java.security.IdentityScope#addIdentity(java.security.Identity) */ public void test_addIdentityLjava_security_Identity() throws Exception { - IdentityScopeSubclass sub = new IdentityScopeSubclass("test", - new IdentityScopeSubclass()); - Identity id = new IdentitySubclass("id1"); - id.setPublicKey(pubKey); - sub.addIdentity(id); - try { - Identity id2 = new IdentitySubclass("id2"); - id2.setPublicKey(pubKey); - sub.addIdentity(id2); - fail("KeyManagementException should have been thrown"); - } catch (KeyManagementException e) { - // Expected - } + IdentityScopeSubclass sub = new IdentityScopeSubclass("test", + new IdentityScopeSubclass()); + Identity id = new IdentitySubclass("id1"); + id.setPublicKey(getPubKey()); + sub.addIdentity(id); + try { + Identity id2 = new IdentitySubclass("id2"); + id2.setPublicKey(getPubKey()); + sub.addIdentity(id2); + fail("KeyManagementException should have been thrown"); + } catch (KeyManagementException e) { + // Expected + } } /** * java.security.IdentityScope#removeIdentity(java.security.Identity) */ public void test_removeIdentityLjava_security_Identity() throws Exception { - IdentityScopeSubclass sub = new IdentityScopeSubclass("test", - new IdentityScopeSubclass()); - Identity id = new IdentitySubclass(); - id.setPublicKey(pubKey); - sub.addIdentity(id); - sub.removeIdentity(id); - try { - sub.removeIdentity(id); - fail("KeyManagementException should have been thrown"); - } catch (KeyManagementException e) { - // expected - } + IdentityScopeSubclass sub = new IdentityScopeSubclass("test", + new IdentityScopeSubclass()); + Identity id = new IdentitySubclass(); + id.setPublicKey(getPubKey()); + sub.addIdentity(id); + sub.removeIdentity(id); + try { + sub.removeIdentity(id); + fail("KeyManagementException should have been thrown"); + } catch (KeyManagementException expected) { + } } /** * java.security.IdentityScope#identities() */ public void test_identities() throws Exception { - IdentityScopeSubclass sub = new IdentityScopeSubclass("test", - new IdentityScopeSubclass()); - Identity id = new IdentitySubclass(); - id.setPublicKey(pubKey); - sub.addIdentity(id); - Enumeration<Identity> en = sub.identities(); - assertTrue("Wrong object contained in identities", en.nextElement() - .equals(id)); - assertTrue("Contains too many elements", !en.hasMoreElements()); + IdentityScopeSubclass sub = new IdentityScopeSubclass("test", + new IdentityScopeSubclass()); + Identity id = new IdentitySubclass(); + id.setPublicKey(getPubKey()); + sub.addIdentity(id); + Enumeration<Identity> en = sub.identities(); + assertEquals("Wrong object contained in identities", en.nextElement(), id); + assertFalse("Contains too many elements", en.hasMoreElements()); } /** @@ -234,33 +231,29 @@ public class IdentityScope2Test extends junit.framework.TestCase { */ public void test_getIdentityLjava_security_Principal() throws Exception { Identity id = new IdentitySubclass("principal name"); - id.setPublicKey(pubKey); + id.setPublicKey(getPubKey()); IdentityScopeSubclass sub = new IdentityScopeSubclass("test", new IdentityScopeSubclass()); try { sub.getIdentity((java.security.Principal) null); fail("Test 1: NullPointerException expected."); - } catch (NullPointerException e) { - // Expected. + } catch (NullPointerException expected) { } sub.addIdentity(id); Identity returnedId = sub.getIdentity(id); - assertEquals("Test 2: Returned Identity not the same as the added one;", - id, returnedId); + assertEquals("Test 2: Returned Identity not the same as the added one;", id, returnedId); Identity id2 = new IdentitySubclass("Another identity"); - id2.setPublicKey(pubKey); + id2.setPublicKey(getPubKey()); - assertNull("Test 3: Null value expected.", - sub.getIdentity(id2)); + assertNull("Test 3: Null value expected.", sub.getIdentity(id2)); try { sub.getIdentity((java.security.Principal) null); fail("Test 4: NullPointerException expected."); - } catch (NullPointerException e) { - // Expected. + } catch (NullPointerException expected) { } } @@ -272,18 +265,15 @@ public class IdentityScope2Test extends junit.framework.TestCase { IdentityScopeSubclass sub = new IdentityScopeSubclass("test", new IdentityScopeSubclass()); Identity id = new IdentitySubclass(); - id.setPublicKey(pubKey); + id.setPublicKey(getPubKey()); sub.addIdentity(id); - Identity returnedId = sub.getIdentity(pubKey); - assertEquals("Test 1: Returned Identity not the same as the added one;", - id, returnedId); + Identity returnedId = sub.getIdentity(getPubKey()); + assertEquals("Test 1: Returned Identity not the same as the added one;", id, returnedId); - assertNull("Test 2: Null value expected.", - sub.getIdentity((PublicKey) null)); + assertNull("Test 2: Null value expected.", sub.getIdentity((PublicKey) null)); PublicKey anotherKey = KeyPairGenerator.getInstance("DSA").genKeyPair().getPublic(); - assertNull("Test 3: Null value expected.", - sub.getIdentity(anotherKey)); + assertNull("Test 3: Null value expected.", sub.getIdentity(anotherKey)); } /** @@ -293,36 +283,35 @@ public class IdentityScope2Test extends junit.framework.TestCase { IdentityScopeSubclass sub = new IdentityScopeSubclass("test", new IdentityScopeSubclass()); Identity id = new IdentitySubclass("test"); - id.setPublicKey(pubKey); + id.setPublicKey(getPubKey()); sub.addIdentity(id); Identity returnedId = sub.getIdentity("test"); - assertEquals("Returned Identity not the same as the added one", id, - returnedId); + assertEquals("Returned Identity not the same as the added one", id, returnedId); } /** * java.security.IdentityScope#size() */ public void test_size() throws Exception { - IdentityScopeSubclass sub = new IdentityScopeSubclass("test", - new IdentityScopeSubclass()); - Identity id = new IdentitySubclass(); - id.setPublicKey(pubKey); - sub.addIdentity(id); - assertEquals("Wrong size", 1, sub.size()); + IdentityScopeSubclass sub = new IdentityScopeSubclass("test", + new IdentityScopeSubclass()); + Identity id = new IdentitySubclass(); + id.setPublicKey(getPubKey()); + sub.addIdentity(id); + assertEquals("Wrong size", 1, sub.size()); } /** * java.security.IdentityScope#toString() */ public void test_toString() throws Exception { - IdentityScopeSubclass sub = new IdentityScopeSubclass("test", - new IdentityScopeSubclass()); - Identity id = new IdentitySubclass(); - id.setPublicKey(pubKey); - sub.addIdentity(id); - assertNotNull("toString returned a null", sub.toString()); - assertTrue("Not a valid String ", sub.toString().length() > 0); + IdentityScopeSubclass sub = new IdentityScopeSubclass("test", + new IdentityScopeSubclass()); + Identity id = new IdentitySubclass(); + id.setPublicKey(getPubKey()); + sub.addIdentity(id); + assertNotNull("toString returned a null", sub.toString()); + assertTrue("Not a valid String ", sub.toString().length() > 0); } public void test_getIdentity() throws Exception { @@ -330,7 +319,8 @@ public class IdentityScope2Test extends junit.framework.TestCase { IdentityScope scope = IdentityScope.getSystemScope(); try { scope.getIdentity((String) null); - fail("NPE expected"); - } catch (NullPointerException npe) {} + fail(); + } catch (NullPointerException expected) { + } } } diff --git a/luni/src/test/java/org/apache/harmony/security/tests/java/security/KeyStore2Test.java b/luni/src/test/java/org/apache/harmony/security/tests/java/security/KeyStore2Test.java index fae3422..5ac2da6 100644 --- a/luni/src/test/java/org/apache/harmony/security/tests/java/security/KeyStore2Test.java +++ b/luni/src/test/java/org/apache/harmony/security/tests/java/security/KeyStore2Test.java @@ -33,10 +33,8 @@ import java.security.NoSuchProviderException; import java.security.PrivateKey; import java.security.Provider; import java.security.PublicKey; -import java.security.SecureRandom; import java.security.Security; import java.security.SignatureException; -import java.security.UnrecoverableEntryException; import java.security.UnrecoverableKeyException; import java.security.cert.Certificate; import java.security.cert.CertificateEncodingException; @@ -45,7 +43,6 @@ import java.security.cert.CertificateFactory; import java.security.cert.X509Certificate; import java.security.interfaces.DSAPrivateKey; import java.security.spec.DSAPrivateKeySpec; -import java.security.spec.InvalidKeySpecException; import java.util.Arrays; import java.util.Calendar; import java.util.Enumeration; @@ -54,18 +51,17 @@ import java.util.Set; import tests.support.Support_TestProvider; public class KeyStore2Test extends junit.framework.TestCase { - static PrivateKey privateKey; - static { - try { - KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("DSA"); - SecureRandom secureRandom = new SecureRandom(); - keyPairGenerator.initialize(1024, secureRandom); + private static PrivateKey PRIVATE_KEY; + + private static PrivateKey getPrivateKey() throws Exception { + if (PRIVATE_KEY == null) { + KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("DSA"); + keyPairGenerator.initialize(1024, null); KeyPair keyPair = keyPairGenerator.genKeyPair(); - privateKey = keyPair.getPrivate(); - } catch (Exception e) { - fail("initialization failed: " + e); + PRIVATE_KEY = keyPair.getPrivate(); } + return PRIVATE_KEY; } final char[] pssWord = { 'a', 'b', 'c' }; @@ -148,7 +144,7 @@ public class KeyStore2Test extends junit.framework.TestCase { keyTest.setCertificateEntry("alias1", cert[0]); // alias 2 - keyTest.setKeyEntry("alias2", privateKey, pssWord, cert); + keyTest.setKeyEntry("alias2", getPrivateKey(), pssWord, cert); // alias 3 keyTest.setCertificateEntry("alias3", cert[1]); @@ -175,9 +171,8 @@ public class KeyStore2Test extends junit.framework.TestCase { try { keyTest.aliases(); - fail("expected KeyStoreException"); - } catch (KeyStoreException e) { - // ok + fail(); + } catch (KeyStoreException expected) { } keyTest.load(null, null); @@ -221,9 +216,8 @@ public class KeyStore2Test extends junit.framework.TestCase { try { keyTest.containsAlias("alias1"); - fail("expected KeyStoreException"); - } catch (KeyStoreException e) { - // ok + fail(); + } catch (KeyStoreException expected) { } keyTest.load(null, null); @@ -240,9 +234,8 @@ public class KeyStore2Test extends junit.framework.TestCase { try { keyTest.containsAlias(null); - fail("expected NullPointerException"); - } catch (NullPointerException e) { - // ok + fail(); + } catch (NullPointerException expected) { } } @@ -260,9 +253,8 @@ public class KeyStore2Test extends junit.framework.TestCase { try { keyTest.getCertificate("anAlias"); - fail("expected KeyStoreException"); - } catch (KeyStoreException e) { - // ok + fail(); + } catch (KeyStoreException expected) { } keyTest.load(null, null); @@ -308,7 +300,7 @@ public class KeyStore2Test extends junit.framework.TestCase { // key entry - keyTest.setKeyEntry("alias2", privateKey, pssWord, cert); + keyTest.setKeyEntry("alias2", getPrivateKey(), pssWord, cert); alias = keyTest.getCertificateAlias(cert[0]); assertEquals("key entry - the alias returned for this certificate was wrong", "alias2", alias); @@ -334,9 +326,8 @@ public class KeyStore2Test extends junit.framework.TestCase { try { keyTest.getCertificateChain("anAlias"); - fail("expected KeyStoreException"); - } catch (KeyStoreException e) { - // ok + fail(); + } catch (KeyStoreException expected) { } keyTest.load(null, null); @@ -345,7 +336,7 @@ public class KeyStore2Test extends junit.framework.TestCase { keyTest.setCertificateEntry("alias1", cert[0]); // alias 2 - keyTest.setKeyEntry("alias2", privateKey, pssWord, cert); + keyTest.setKeyEntry("alias2", getPrivateKey(), pssWord, cert); Certificate[] certRes = keyTest.getCertificateChain("alias2"); assertEquals("there are more than two certificate returned from getCertificateChain", @@ -360,9 +351,8 @@ public class KeyStore2Test extends junit.framework.TestCase { try { keyTest.getCertificateChain(null); - fail("expected NullPointerException"); - } catch (NullPointerException e) { - // ok + fail(); + } catch (NullPointerException expected) { } } @@ -393,19 +383,18 @@ public class KeyStore2Test extends junit.framework.TestCase { KeyStore keyTest = KeyStore.getInstance(KeyStore.getDefaultType()); keyTest.load(null, null); - keyTest.setKeyEntry("alias2", privateKey, pssWord, cert); + keyTest.setKeyEntry("alias2", getPrivateKey(), pssWord, cert); PrivateKey returnedKey = (PrivateKey) keyTest.getKey("alias2", pssWord); byte[] retB = returnedKey.getEncoded(); - byte[] priB = privateKey.getEncoded(); + byte[] priB = getPrivateKey().getEncoded(); assertTrue(Arrays.equals(retB, priB)); - assertEquals(privateKey.getAlgorithm(), returnedKey.getAlgorithm()); - assertEquals(privateKey.getFormat(), returnedKey.getFormat()); + assertEquals(getPrivateKey().getAlgorithm(), returnedKey.getAlgorithm()); + assertEquals(getPrivateKey().getFormat(), returnedKey.getFormat()); try { keyTest.getKey("alias2", "wrong".toCharArray()); - fail("Should have thrown UnrecoverableKeyException"); - } catch (UnrecoverableKeyException e) { - // expected + fail(); + } catch (UnrecoverableKeyException expected) { } keyTest.setCertificateEntry("alias1", cert[1]); @@ -428,9 +417,8 @@ public class KeyStore2Test extends junit.framework.TestCase { try { keyTest.isCertificateEntry("alias"); - fail("expected KeyStoreException"); - } catch (KeyStoreException e) { - // ok + fail(); + } catch (KeyStoreException expected) { } keyTest.load(null, null); @@ -438,7 +426,7 @@ public class KeyStore2Test extends junit.framework.TestCase { keyTest.setCertificateEntry("alias1", cert[0]); // alias 2 - keyTest.setKeyEntry("alias2", privateKey, pssWord, cert); + keyTest.setKeyEntry("alias2", getPrivateKey(), pssWord, cert); assertTrue("isCertificateEntry method returns false for a certificate", keyTest.isCertificateEntry("alias1")); @@ -460,9 +448,8 @@ public class KeyStore2Test extends junit.framework.TestCase { try { keyTest.isKeyEntry("alias"); - fail("expected KeyStoreException"); - } catch (KeyStoreException e) { - // ok + fail(); + } catch (KeyStoreException expected) { } keyTest.load(null, null); @@ -470,7 +457,7 @@ public class KeyStore2Test extends junit.framework.TestCase { keyTest.setCertificateEntry("alias1", cert[0]); // alias 2 - keyTest.setKeyEntry("alias2", privateKey, pssWord, cert); + keyTest.setKeyEntry("alias2", getPrivateKey(), pssWord, cert); assertTrue("isKeyEntry method returns false for a certificate", keyTest.isKeyEntry("alias2")); @@ -507,17 +494,10 @@ public class KeyStore2Test extends junit.framework.TestCase { /** * java.security.KeyStore#load(KeyStore.LoadStoreParameter param) */ - public void test_loadLjava_security_KeyStoreLoadStoreParameter() { - try { - KeyStore keyTest = KeyStore.getInstance(KeyStore.getDefaultType()); - keyTest.load(null); - - } catch (Exception e ) { - fail("Unexpected Exception " + e); - } - - + public void test_loadLjava_security_KeyStoreLoadStoreParameter() throws Exception { + KeyStore.getInstance(KeyStore.getDefaultType()).load(null); } + /** * java.security.KeyStore#setCertificateEntry(java.lang.String, * java.security.cert.Certificate) @@ -533,9 +513,8 @@ public class KeyStore2Test extends junit.framework.TestCase { try { keyTest.setCertificateEntry("alias", cert); - fail("expected KeyStoreException"); - } catch (KeyStoreException e) { - // ok + fail(); + } catch (KeyStoreException expected) { } keyTest.load(null, null); @@ -570,23 +549,21 @@ public class KeyStore2Test extends junit.framework.TestCase { KeyStore keyTest = KeyStore.getInstance(KeyStore.getDefaultType()); try { - keyTest.setKeyEntry("alias3", privateKey, pssWord, cert); - fail("expected KeyStoreException"); - } catch (KeyStoreException e) { - // ok + keyTest.setKeyEntry("alias3", getPrivateKey(), pssWord, cert); + fail(); + } catch (KeyStoreException expected) { } keyTest.load(null, null); - keyTest.setKeyEntry("alias3", privateKey, pssWord, cert); + keyTest.setKeyEntry("alias3", getPrivateKey(), pssWord, cert); assertTrue("the entry specified by the alias alias3 is not a keyEntry", keyTest.isKeyEntry("alias3")); try { - keyTest.setKeyEntry("alias4", privateKey, pssWord, new Certificate[] {}); - fail("expected IllegalArgumentException"); - } catch (IllegalArgumentException e) { - // ok + keyTest.setKeyEntry("alias4", getPrivateKey(), pssWord, new Certificate[] {}); + fail(); + } catch (IllegalArgumentException expected) { } } @@ -605,9 +582,8 @@ public class KeyStore2Test extends junit.framework.TestCase { try { keyTest.size(); - fail("expected KeyStoreException"); - } catch (KeyStoreException e) { - // ok + fail(); + } catch (KeyStoreException expected) { } keyTest.load(null, null); @@ -615,7 +591,7 @@ public class KeyStore2Test extends junit.framework.TestCase { keyTest.setCertificateEntry("alias1", cert[0]); // alias 2 - keyTest.setKeyEntry("alias2", privateKey, pssWord, cert); + keyTest.setKeyEntry("alias2", getPrivateKey(), pssWord, cert); // alias 3 keyTest.setCertificateEntry("alias3", cert[1]); @@ -623,66 +599,41 @@ public class KeyStore2Test extends junit.framework.TestCase { assertEquals("the size of the keyStore is not 3", 3, keyTest.size()); } - public void test_deleteEmptyEntry() { - try { - KeyStore keyTest = KeyStore.getInstance(KeyStore.getDefaultType()); - keyTest.load(null, null); - keyTest.deleteEntry(""); - } catch (Exception e) { - fail("Unexpected Exception " + e); - } + public void test_deleteEmptyEntryEmptyAlias() throws Exception { + KeyStore keyTest = KeyStore.getInstance(KeyStore.getDefaultType()); + keyTest.load(null, null); + keyTest.deleteEntry(""); + } - try { - KeyStore keyTest = KeyStore.getInstance(KeyStore.getDefaultType()); - keyTest.load(null, null); - keyTest.deleteEntry("entry"); - } catch (Exception e) { - fail("Unexpected Exception " + e); - } + public void test_deleteEmptyEntryBogusAlias() throws Exception { + KeyStore keyTest = KeyStore.getInstance(KeyStore.getDefaultType()); + keyTest.load(null, null); + keyTest.deleteEntry("bogus"); } /** * java.security.KeyStore#deleteEntry(String) */ - public void test_deleteEntry() { + public void test_deleteEntry() throws Exception { try { KeyStore keyTest = KeyStore.getInstance(KeyStore.getDefaultType()); keyTest.load(null, null); keyTest.deleteEntry(null); - fail("NullPointerException expected"); - } catch (NullPointerException e) { - // expected - } catch (Exception e) { - fail("Unexpected Exception " + e); + fail(); + } catch (NullPointerException expected) { } - try { - KeyStore keyTest = KeyStore.getInstance(KeyStore.getDefaultType()); - keyTest.load(null, "password".toCharArray()); - - KeyStore.PasswordProtection pp = new KeyStore.PasswordProtection( - pssWord); - Certificate[] chain = { new MyCertificate("DSA", testEncoding), - new MyCertificate("DSA", testEncoding) }; - KeyStore.PrivateKeyEntry pkEntry = new KeyStore.PrivateKeyEntry( - privateKey, chain); - - keyTest.setEntry("symKey", pkEntry, pp); - - keyTest.deleteEntry("symKey"); - - } catch (KeyStoreException e) { - fail("Unexpected KeyStoreException " + e.getMessage()); - } catch (IOException e) { - fail("Unexpected IOException " + e.getMessage()); - } catch (NoSuchAlgorithmException e) { - fail("Unexpected NoSuchAlgorithmException " + e.getMessage()); - } catch (CertificateException e) { - fail("Unexpected CertificateException " + e.getMessage()); - } catch (Exception e) { - fail("Unexpected Exception " + e); - } + KeyStore keyTest = KeyStore.getInstance(KeyStore.getDefaultType()); + keyTest.load(null, "password".toCharArray()); + + KeyStore.PasswordProtection pp = new KeyStore.PasswordProtection(pssWord); + Certificate[] chain = { new MyCertificate("DSA", testEncoding), + new MyCertificate("DSA", testEncoding) }; + KeyStore.PrivateKeyEntry pkEntry = new KeyStore.PrivateKeyEntry(getPrivateKey(), chain); + keyTest.setEntry("symKey", pkEntry, pp); + + keyTest.deleteEntry("symKey"); } /** @@ -695,9 +646,8 @@ public class KeyStore2Test extends junit.framework.TestCase { try { keyTest.getCreationDate("anAlias"); - fail("expected KeyStoreException"); - } catch (KeyStoreException e) { - // ok + fail(); + } catch (KeyStoreException expected) { } keyTest.load(null, pssWord); @@ -705,9 +655,8 @@ public class KeyStore2Test extends junit.framework.TestCase { assertNull(keyTest.getCreationDate("")); try { keyTest.getCreationDate(null); - fail("NullPointerException expected"); - } catch (NullPointerException e) { - // expected + fail(); + } catch (NullPointerException expected) { } Certificate[] chain = { new MyCertificate(type, testEncoding), @@ -718,7 +667,7 @@ public class KeyStore2Test extends junit.framework.TestCase { KeyStore.PasswordProtection pp = new KeyStore.PasswordProtection( pssWord); - KeyStore.PrivateKeyEntry pke = new KeyStore.PrivateKeyEntry(privateKey, + KeyStore.PrivateKeyEntry pke = new KeyStore.PrivateKeyEntry(getPrivateKey(), chain); KeyStore.PrivateKeyEntry pke1 = new KeyStore.PrivateKeyEntry( privateKey1, chain); @@ -733,8 +682,7 @@ public class KeyStore2Test extends junit.framework.TestCase { int hourExpected = Calendar.getInstance().get(Calendar.HOUR); int minuteExpected = Calendar.getInstance().get(Calendar.MINUTE); - Calendar.getInstance().setTimeInMillis( - keyTest.getCreationDate("alias1").getTime()); + Calendar.getInstance().setTimeInMillis(keyTest.getCreationDate("alias1").getTime()); int dayActual1 = Calendar.getInstance().get(Calendar.DAY_OF_MONTH); int monthActual1 = Calendar.getInstance().get(Calendar.MONTH); int yearActual1 = Calendar.getInstance().get(Calendar.YEAR); @@ -763,9 +711,8 @@ public class KeyStore2Test extends junit.framework.TestCase { try { keyTest.getCreationDate(null); - fail("expected NullPointerException"); - } catch (NullPointerException e) { - // ok + fail(); + } catch (NullPointerException expected) { } } @@ -773,268 +720,145 @@ public class KeyStore2Test extends junit.framework.TestCase { * java.security.KeyStore#getEntry(String, * KeyStore.ProtectionParameter) */ - public void test_getEntry() { + public void test_getEntry() throws Exception { String type = "DSA"; - KeyStore keyTest = null; - KeyStore.PasswordProtection pp = null; - - try { - keyTest = KeyStore.getInstance(KeyStore.getDefaultType()); - } catch (KeyStoreException e) { - fail("unexpected exception: " + e); - } - - try { - keyTest.getEntry("anAlias", new KeyStore.PasswordProtection(new char[] {})); - fail("expected KeyStoreException"); - } catch (KeyStoreException e) { - // ok - } catch (NoSuchAlgorithmException e) { - fail("unexpected exception: " + e); - } catch (UnrecoverableEntryException e) { - fail("unexpected exception: " + e); - } - - try { - keyTest.load(null, pssWord); - } catch (NoSuchAlgorithmException e) { - fail("unexpected exception: " + e); - } catch (CertificateException e) { - fail("unexpected exception: " + e); - } catch (IOException e) { - fail("unexpected exception: " + e); - } - - try { - keyTest.getEntry(null, new KeyStore.PasswordProtection(new char[] {})); - fail("expected NullPointerException"); - } catch (NullPointerException e) { - // ok - } catch (NoSuchAlgorithmException e) { - fail("unexpected exception: " + e); - } catch (UnrecoverableEntryException e) { - fail("unexpected exception: " + e); - } catch (KeyStoreException e) { - fail("unexpected exception: " + e); - } - - try { - keyTest.getEntry("anAlias", null); - } catch (NullPointerException e) { - fail("unexpected exception: " + e); - } catch (NoSuchAlgorithmException e) { - fail("unexpected exception: " + e); - } catch (UnrecoverableEntryException e) { - fail("unexpected exception: " + e); - } catch (KeyStoreException e) { - fail("unexpected exception: " + e); - } - - try { - keyTest.getEntry(null, null); - fail("expected NullPointerException"); - } catch (NullPointerException e) { - // ok - } catch (NoSuchAlgorithmException e) { - fail("unexpected exception: " + e); - } catch (UnrecoverableEntryException e) { - fail("unexpected exception: " + e); - } catch (KeyStoreException e) { - fail("unexpected exception: " + e); - } - - - try { - assertNull(keyTest.getEntry("alias", pp)); - } catch (NoSuchAlgorithmException e) { - fail("unexpected exception: " + e); - } catch (UnrecoverableEntryException e) { - fail("unexpected exception: " + e); - } catch (KeyStoreException e) { - fail("unexpected exception: " + e); - } - - Certificate[] chain = { new MyCertificate(type, testEncoding), - new MyCertificate(type, testEncoding) }; - - DSAPrivateKey privateKey1 = null; - try { - privateKey1 = (DSAPrivateKey) KeyFactory.getInstance(type) - .generatePrivate( - new DSAPrivateKeySpec(new BigInteger("1"), - new BigInteger("2"), new BigInteger("3"), - new BigInteger("4"))); - } catch (InvalidKeySpecException e) { - fail("unexpected exception: " + e); - } catch (NoSuchAlgorithmException e) { - fail("unexpected exception: " + e); - } - - pp = new KeyStore.PasswordProtection(pssWord); - - try { - assertNull(keyTest.getEntry("alias", pp)); - } catch (NoSuchAlgorithmException e) { - fail("unexpected exception: " + e); - } catch (UnrecoverableEntryException e) { - fail("unexpected exception: " + e); - } catch (KeyStoreException e) { - fail("unexpected exception: " + e); - } - - KeyStore.PrivateKeyEntry pke1 = new KeyStore.PrivateKeyEntry( - privateKey, chain); - KeyStore.PrivateKeyEntry pke2 = new KeyStore.PrivateKeyEntry( - privateKey1, chain); - - try { - keyTest.setEntry("alias1", pke1, pp); - } catch (KeyStoreException e) { - fail("unexpected exception: " + e); - } - try { - keyTest.setEntry("alias2", pke2, pp); - } catch (KeyStoreException e) { - fail("unexpected exception: " + e); - } - - try { - assertNull(keyTest.getEntry("alias", pp)); - } catch (NoSuchAlgorithmException e) { - fail("unexpected exception: " + e); - } catch (UnrecoverableEntryException e) { - fail("unexpected exception: " + e); - } catch (KeyStoreException e) { - fail("unexpected exception: " + e); - } - KeyStore.PrivateKeyEntry pkeActual1 = null; - try { - pkeActual1 = (KeyStore.PrivateKeyEntry) keyTest.getEntry("alias1", pp); - } catch (NoSuchAlgorithmException e) { - fail("unexpected exception: " + e); - } catch (UnrecoverableEntryException e) { - fail("unexpected exception: " + e); - } catch (KeyStoreException e) { - fail("unexpected exception: " + e); - } - KeyStore.PrivateKeyEntry pkeActual2 = null; - try { - pkeActual2 = (KeyStore.PrivateKeyEntry) keyTest.getEntry("alias2", pp); - } catch (NoSuchAlgorithmException e) { - fail("unexpected exception: " + e); - } catch (UnrecoverableEntryException e) { - fail("unexpected exception: " + e); - } catch (KeyStoreException e) { - fail("unexpected exception: " + e); - } - - assertTrue(Arrays.equals(chain, pkeActual1.getCertificateChain())); - assertEquals(privateKey, pkeActual1.getPrivateKey()); - assertEquals(new MyCertificate(type, testEncoding), pkeActual1.getCertificate()); - try { - assertTrue(keyTest.entryInstanceOf("alias1", KeyStore.PrivateKeyEntry.class)); - } catch (KeyStoreException e) { - fail("unexpected exception: " + e); - } - - assertTrue(Arrays.equals(chain, pkeActual2.getCertificateChain())); - DSAPrivateKey entryPrivateKey = (DSAPrivateKey) pkeActual2.getPrivateKey(); - assertEquals(privateKey1.getX(), entryPrivateKey.getX()); - assertEquals(privateKey1.getParams().getG(), entryPrivateKey.getParams().getG()); - assertEquals(privateKey1.getParams().getP(), entryPrivateKey.getParams().getP()); - assertEquals(privateKey1.getParams().getQ(), entryPrivateKey.getParams().getQ()); - - assertEquals(new MyCertificate(type, testEncoding), pkeActual2.getCertificate()); - try { - assertTrue(keyTest.entryInstanceOf("alias2", KeyStore.PrivateKeyEntry.class)); - } catch (KeyStoreException e) { - fail("unexpected exception: " + e); - } + + KeyStore keyTest = KeyStore.getInstance(KeyStore.getDefaultType()); + + try { + keyTest.getEntry("anAlias", new KeyStore.PasswordProtection(new char[] {})); + fail(); + } catch (KeyStoreException expected) { + } + + keyTest.load(null, pssWord); + + try { + keyTest.getEntry(null, new KeyStore.PasswordProtection(new char[] {})); + fail(); + } catch (NullPointerException expected) { + } + + keyTest.getEntry("anAlias", null); + + try { + keyTest.getEntry(null, null); + fail(); + } catch (NullPointerException expected) { + } + assertNull(keyTest.getEntry("alias", null)); + Certificate[] chain = { new MyCertificate(type, testEncoding), + new MyCertificate(type, testEncoding) }; + + DSAPrivateKey privateKey1 = (DSAPrivateKey) + KeyFactory.getInstance(type).generatePrivate( + new DSAPrivateKeySpec(new BigInteger("1"), + new BigInteger("2"), + new BigInteger("3"), + new BigInteger("4"))); + + KeyStore.PasswordProtection pp = new KeyStore.PasswordProtection(pssWord); + assertNull(keyTest.getEntry("alias", pp)); + + KeyStore.PrivateKeyEntry pke1 = new KeyStore.PrivateKeyEntry(getPrivateKey(), chain); + KeyStore.PrivateKeyEntry pke2 = new KeyStore.PrivateKeyEntry(privateKey1, chain); + + keyTest.setEntry("alias1", pke1, pp); + keyTest.setEntry("alias2", pke2, pp); + + assertNull(keyTest.getEntry("alias", pp)); + KeyStore.PrivateKeyEntry pkeActual1 = (KeyStore.PrivateKeyEntry) keyTest.getEntry("alias1", + pp); + KeyStore.PrivateKeyEntry pkeActual2 = (KeyStore.PrivateKeyEntry) keyTest.getEntry("alias2", + pp); + + assertTrue(Arrays.equals(chain, pkeActual1.getCertificateChain())); + assertEquals(getPrivateKey(), pkeActual1.getPrivateKey()); + assertEquals(new MyCertificate(type, testEncoding), pkeActual1.getCertificate()); + assertTrue(keyTest.entryInstanceOf("alias1", KeyStore.PrivateKeyEntry.class)); + + assertTrue(Arrays.equals(chain, pkeActual2.getCertificateChain())); + DSAPrivateKey entryPrivateKey = (DSAPrivateKey) pkeActual2.getPrivateKey(); + assertEquals(privateKey1.getX(), entryPrivateKey.getX()); + assertEquals(privateKey1.getParams().getG(), entryPrivateKey.getParams().getG()); + assertEquals(privateKey1.getParams().getP(), entryPrivateKey.getParams().getP()); + assertEquals(privateKey1.getParams().getQ(), entryPrivateKey.getParams().getQ()); + + assertEquals(new MyCertificate(type, testEncoding), pkeActual2.getCertificate()); + assertTrue(keyTest.entryInstanceOf("alias2", KeyStore.PrivateKeyEntry.class)); } /** * java.security.KeyStore#setEntry(String, KeyStore.Entry, * KeyStore.ProtectionParameter) */ - public void test_setEntry() { + public void test_setEntry() throws Exception { String type = "DSA"; - KeyStore keyTest = null; - KeyStore.PasswordProtection pp = null; + + KeyStore keyTest = KeyStore.getInstance(KeyStore.getDefaultType()); + keyTest.load(null, pssWord); + + Certificate[] chain = { new MyCertificate(type, testEncoding), + new MyCertificate(type, testEncoding) }; + DSAPrivateKey privateKey1 = (DSAPrivateKey) + KeyFactory.getInstance(type).generatePrivate( + new DSAPrivateKeySpec(new BigInteger("1"), + new BigInteger("2"), + new BigInteger("3"), + new BigInteger("4"))); + + KeyStore.PasswordProtection pp = new KeyStore.PasswordProtection(pssWord); + KeyStore.PrivateKeyEntry pke = new KeyStore.PrivateKeyEntry(getPrivateKey(), chain); + KeyStore.PrivateKeyEntry pke1 = new KeyStore.PrivateKeyEntry(privateKey1, chain); try { - keyTest = KeyStore.getInstance(KeyStore.getDefaultType()); - keyTest.load(null, pssWord); - - Certificate[] chain = { new MyCertificate(type, testEncoding), - new MyCertificate(type, testEncoding) }; - DSAPrivateKey privateKey1 = (DSAPrivateKey) KeyFactory.getInstance(type) - .generatePrivate( - new DSAPrivateKeySpec(new BigInteger("1"), - new BigInteger("2"), new BigInteger("3"), - new BigInteger("4"))); - - pp = new KeyStore.PasswordProtection(pssWord); - KeyStore.PrivateKeyEntry pke = new KeyStore.PrivateKeyEntry(privateKey, chain); - KeyStore.PrivateKeyEntry pke1 = new KeyStore.PrivateKeyEntry(privateKey1, chain); - - try { - keyTest.setEntry("alias", pke, null); - fail("Exception expected"); - } catch (Exception e) { - // expected - } - - keyTest.setEntry("alias", pke, pp); - - KeyStore.PrivateKeyEntry pkeActual = (KeyStore.PrivateKeyEntry) - keyTest.getEntry("alias",pp); - - assertTrue(Arrays.equals(chain, pkeActual.getCertificateChain())); - assertEquals(privateKey, pkeActual.getPrivateKey()); - assertEquals(new MyCertificate(type, testEncoding), pkeActual.getCertificate()); - assertTrue(keyTest.entryInstanceOf("alias", KeyStore.PrivateKeyEntry.class)); - - keyTest.setEntry("alias", pke1, pp); - pkeActual = (KeyStore.PrivateKeyEntry) keyTest.getEntry("alias", pp); - - assertTrue(Arrays.equals(chain, pkeActual.getCertificateChain())); - DSAPrivateKey actualPrivateKey = (DSAPrivateKey) pkeActual.getPrivateKey(); - assertEquals(privateKey1.getX(), actualPrivateKey.getX()); - assertEquals(privateKey1.getParams().getG(), actualPrivateKey.getParams().getG()); - assertEquals(privateKey1.getParams().getP(), actualPrivateKey.getParams().getP()); - assertEquals(privateKey1.getParams().getQ(), actualPrivateKey.getParams().getQ()); - assertEquals(new MyCertificate(type, testEncoding), pkeActual.getCertificate()); - assertTrue(keyTest.entryInstanceOf("alias", KeyStore.PrivateKeyEntry.class)); - - keyTest.setEntry("alias2", pke1, pp); - pkeActual = (KeyStore.PrivateKeyEntry) keyTest.getEntry("alias2", pp); - - assertTrue(Arrays.equals(chain, pkeActual.getCertificateChain())); - actualPrivateKey = (DSAPrivateKey) pkeActual.getPrivateKey(); - assertEquals(privateKey1.getX(), actualPrivateKey.getX()); - assertEquals(privateKey1.getParams().getG(), actualPrivateKey.getParams().getG()); - assertEquals(privateKey1.getParams().getP(), actualPrivateKey.getParams().getP()); - assertEquals(privateKey1.getParams().getQ(), actualPrivateKey.getParams().getQ()); - assertEquals(new MyCertificate(type, testEncoding), pkeActual.getCertificate()); - assertTrue(keyTest.entryInstanceOf("alias2", KeyStore.PrivateKeyEntry.class)); - - } catch (Exception e) { - fail("Unexpected Exception " + e); + keyTest.setEntry("alias", pke, null); + fail(); + } catch (Exception expected) { } + keyTest.setEntry("alias", pke, pp); + + KeyStore.PrivateKeyEntry pkeActual = (KeyStore.PrivateKeyEntry) + keyTest.getEntry("alias",pp); + + assertTrue(Arrays.equals(chain, pkeActual.getCertificateChain())); + assertEquals(getPrivateKey(), pkeActual.getPrivateKey()); + assertEquals(new MyCertificate(type, testEncoding), pkeActual.getCertificate()); + assertTrue(keyTest.entryInstanceOf("alias", KeyStore.PrivateKeyEntry.class)); + + keyTest.setEntry("alias", pke1, pp); + pkeActual = (KeyStore.PrivateKeyEntry) keyTest.getEntry("alias", pp); + + assertTrue(Arrays.equals(chain, pkeActual.getCertificateChain())); + DSAPrivateKey actualPrivateKey = (DSAPrivateKey) pkeActual.getPrivateKey(); + assertEquals(privateKey1.getX(), actualPrivateKey.getX()); + assertEquals(privateKey1.getParams().getG(), actualPrivateKey.getParams().getG()); + assertEquals(privateKey1.getParams().getP(), actualPrivateKey.getParams().getP()); + assertEquals(privateKey1.getParams().getQ(), actualPrivateKey.getParams().getQ()); + assertEquals(new MyCertificate(type, testEncoding), pkeActual.getCertificate()); + assertTrue(keyTest.entryInstanceOf("alias", KeyStore.PrivateKeyEntry.class)); + + keyTest.setEntry("alias2", pke1, pp); + pkeActual = (KeyStore.PrivateKeyEntry) keyTest.getEntry("alias2", pp); + + assertTrue(Arrays.equals(chain, pkeActual.getCertificateChain())); + actualPrivateKey = (DSAPrivateKey) pkeActual.getPrivateKey(); + assertEquals(privateKey1.getX(), actualPrivateKey.getX()); + assertEquals(privateKey1.getParams().getG(), actualPrivateKey.getParams().getG()); + assertEquals(privateKey1.getParams().getP(), actualPrivateKey.getParams().getP()); + assertEquals(privateKey1.getParams().getQ(), actualPrivateKey.getParams().getQ()); + assertEquals(new MyCertificate(type, testEncoding), pkeActual.getCertificate()); + assertTrue(keyTest.entryInstanceOf("alias2", KeyStore.PrivateKeyEntry.class)); try { keyTest.setEntry(null, null, null); - fail("NullPointerException expected"); - } catch (NullPointerException e) { - // expected - } catch (Exception e) { - fail("Unexpected exception"); + fail(); + } catch (NullPointerException expected) { } - } /* @@ -1047,9 +871,8 @@ public class KeyStore2Test extends junit.framework.TestCase { try { keyStore.entryInstanceOf("anAlias", KeyStore.SecretKeyEntry.class); - fail("expected KeyStoreException"); - } catch (KeyStoreException e) { - // ok + fail(); + } catch (KeyStoreException expected) { } keyStore.load(null, "pwd".toCharArray()); @@ -1059,23 +882,20 @@ public class KeyStore2Test extends junit.framework.TestCase { Certificate[] chain = { new MyCertificate("DSA", testEncoding), new MyCertificate("DSA", testEncoding) }; - keyStore.setKeyEntry(alias, privateKey, "pwd".toCharArray(), chain); + keyStore.setKeyEntry(alias, getPrivateKey(), "pwd".toCharArray(), chain); assertTrue(keyStore.entryInstanceOf(alias, KeyStore.PrivateKeyEntry.class)); assertFalse(keyStore.entryInstanceOf(alias, KeyStore.SecretKeyEntry.class)); assertFalse(keyStore.entryInstanceOf(alias, KeyStore.TrustedCertificateEntry.class)); try { keyStore.entryInstanceOf(null, KeyStore.SecretKeyEntry.class); - fail("expected NullPointerException"); - } catch (NullPointerException e) { - // ok + } catch (NullPointerException expected) { } try { keyStore.entryInstanceOf("anAlias", null); - fail("expected NullPointerException"); - } catch (NullPointerException e) { - // ok + fail(); + } catch (NullPointerException expected) { } } @@ -1088,9 +908,8 @@ public class KeyStore2Test extends junit.framework.TestCase { keyStore.load(null, "pwd".toCharArray()); try { keyStore.store(null); - fail("UnsupportedOperationException expected"); - } catch (UnsupportedOperationException e) { - // expected + fail(); + } catch (UnsupportedOperationException expected) { } } @@ -1102,21 +921,15 @@ public class KeyStore2Test extends junit.framework.TestCase { try { keyStore.store(new ByteArrayOutputStream(), "pwd".toCharArray()); - fail("expected KeyStoreException"); - } catch (KeyStoreException e) { - // ok + } catch (KeyStoreException expected) { } keyStore.load(null, "pwd".toCharArray()); try { keyStore.store(null, "pwd".toCharArray()); - fail("NullPointerException or IOException expected"); - } catch (NullPointerException e) { - // expected - } catch (IOException e) { - // also ok - } catch (Exception e) { - fail("Unexpected Exception " + e); + fail(); + } catch (NullPointerException expected) { + } catch (IOException expected) { } } diff --git a/luni/src/test/java/org/apache/harmony/security/tests/java/security/Signature2Test.java b/luni/src/test/java/org/apache/harmony/security/tests/java/security/Signature2Test.java index 44e956a..39423ee 100644 --- a/luni/src/test/java/org/apache/harmony/security/tests/java/security/Signature2Test.java +++ b/luni/src/test/java/org/apache/harmony/security/tests/java/security/Signature2Test.java @@ -24,6 +24,8 @@ import java.security.InvalidKeyException; import java.security.InvalidParameterException; import java.security.KeyPair; import java.security.KeyPairGenerator; +import java.security.NoSuchAlgorithmException; +import java.security.NoSuchProviderException; import java.security.PrivateKey; import java.security.Provider; import java.security.PublicKey; @@ -34,29 +36,31 @@ import java.security.SignatureException; import java.security.cert.Certificate; import java.security.spec.DSAParameterSpec; import java.util.HashSet; -import java.util.Locale; import java.util.Set; -import java.security.NoSuchAlgorithmException; -import java.security.NoSuchProviderException; public class Signature2Test extends junit.framework.TestCase { private static final String MESSAGE = "abc"; - static KeyPair dsaKeys; - static KeyPair rsaKeys; - static { - try { + private static KeyPair DSA_KEYS; + private static KeyPair RSA_KEYS; + + private static KeyPair getDsaKeys() throws Exception { + if (DSA_KEYS == null) { KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA"); keyGen.initialize(1024); - dsaKeys = keyGen.generateKeyPair(); + DSA_KEYS = keyGen.generateKeyPair(); + } + return DSA_KEYS; + } - KeyPairGenerator keyGen2 = KeyPairGenerator.getInstance("RSA"); - keyGen2.initialize(1024); - rsaKeys = keyGen2.generateKeyPair(); - } catch (Exception e) { - fail(e.toString()); + private static KeyPair getRsaKeys() throws Exception { + if (RSA_KEYS == null) { + KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA"); + keyGen.initialize(1024); + RSA_KEYS = keyGen.generateKeyPair(); } + return RSA_KEYS; } /** @@ -66,9 +70,8 @@ public class Signature2Test extends junit.framework.TestCase { Signature s = Signature.getInstance("DSA"); try { s.clone(); - fail("A Signature may not be cloneable"); - } catch (CloneNotSupportedException e) { - // Expected - a Signature may not be cloneable + fail(); + } catch (CloneNotSupportedException expected) { } } @@ -84,18 +87,13 @@ public class Signature2Test extends junit.framework.TestCase { /** * java.security.Signature#getInstance(java.lang.String) */ - public void test_getInstanceLjava_lang_String() { - try { - Signature.getInstance("DSA"); - } catch (Exception e) { - fail("Unexpected exception for DSA algorithm"); - } + public void test_getInstanceLjava_lang_String() throws Exception { + Signature.getInstance("DSA"); try { - Signature.getInstance("SHA-256"); - fail("NoSuchAlgorithmException was not thrown for unavailable algorithm"); - } catch (NoSuchAlgorithmException e) { - //expected + Signature.getInstance("bogus"); + fail(); + } catch (NoSuchAlgorithmException expected) { } } @@ -115,30 +113,26 @@ public class Signature2Test extends junit.framework.TestCase { try { Signature.getInstance((String) null, (Provider) null); - fail("NullPointerException expected"); - } catch (NullPointerException e) { - // expected + fail(); + } catch (NullPointerException expected) { } try { Signature.getInstance("DSA", (Provider) null); - fail("IllegalArgumentException expected"); - } catch (IllegalArgumentException e) { - // expected + fail(); + } catch (IllegalArgumentException expected) { } try { Signature.getInstance((String) null, providers[0]); - fail("NullPointerException expected"); - } catch (NullPointerException e) { - // expected + fail(); + } catch (NullPointerException expected) { } try { - Signature.getInstance("SHA-256", providers[0]); - fail("NoSuchAlgorithmException expected"); - } catch (NoSuchAlgorithmException e) { - // expected + Signature.getInstance("bogus", providers[0]); + fail(); + } catch (NoSuchAlgorithmException expected) { } } @@ -152,22 +146,20 @@ public class Signature2Test extends junit.framework.TestCase { for (int i = 0; i < providers.length; i++) { Signature.getInstance("DSA", providers[i].getName()); - }// end for + } try { - Signature.getInstance("SHA-256", providers[0].getName()); - fail("NoSuchAlgorithmException expected"); - } catch (NoSuchAlgorithmException e) { - // expected + Signature.getInstance("bogus", providers[0].getName()); + fail(); + } catch (NoSuchAlgorithmException expected) { } Provider[] pp = Security.getProviders(); for (int i = 0; i < pp.length; i++) { try { Signature.getInstance("DSA", pp[i].toString()); - fail("NoSuchProviderException expected"); - } catch (NoSuchProviderException e) { - // expected + fail(pp[i].toString()); + } catch (NoSuchProviderException expected) { } } @@ -175,9 +167,8 @@ public class Signature2Test extends junit.framework.TestCase { for (int i = 0; i < sp.length; i++) { try { Signature.getInstance("DSA", sp[i]); - fail("IllegalArgumentException was not throw for " + sp[i]); - } catch (IllegalArgumentException e) { - // expected + fail(sp[i]); + } catch (IllegalArgumentException expected) { } } } @@ -196,17 +187,12 @@ public class Signature2Test extends junit.framework.TestCase { try { MySignature sig2 = new MySignature("test"); sig2.getParameters(); - fail("expected UnsupportedOperationException"); - } catch (UnsupportedOperationException e) { - // ok + fail(); + } catch (UnsupportedOperationException expected) { } - try { - MySignature sig2 = new MySignature("ABC"); - sig2.getParameters(); - } catch (UnsupportedOperationException e) { - fail("unexpected: " + e); - } + MySignature sig2 = new MySignature("ABC"); + sig2.getParameters(); } /** @@ -235,39 +221,28 @@ public class Signature2Test extends junit.framework.TestCase { * java.security.Signature#initSign(java.security.PrivateKey) */ public void test_initSignLjava_security_PrivateKey() throws Exception { - try { - Signature.getInstance("DSA").initSign(dsaKeys.getPrivate()); - } catch (InvalidKeyException e) { - fail("unexpected: " + e); - } + Signature.getInstance("DSA").initSign(getDsaKeys().getPrivate()); try { - Signature.getInstance("DSA").initSign(rsaKeys.getPrivate()); - fail("expected InvalidKeyException"); - } catch (InvalidKeyException e) { - // ok + Signature.getInstance("DSA").initSign(getRsaKeys().getPrivate()); + fail(); + } catch (InvalidKeyException expected) { } } - public void test_initSignLjava_security_PrivateKeyLjava_security_SecureRandom() { - - try { - Signature sig = Signature.getInstance("DSA"); - sig.initSign(dsaKeys.getPrivate(), new SecureRandom()); - } catch (NoSuchAlgorithmException e) { - fail("unexpected: " + e); - } catch (InvalidKeyException e) { - fail("unexpected: " + e); - } + public void test_initSignLjava_security_PrivateKeyLjava_security_SecureRandom() + throws Exception { + Signature sig = Signature.getInstance("DSA"); + sig.initSign(getDsaKeys().getPrivate(), new SecureRandom()); + } + public void test_initSignLjava_security_PrivateKeyLjava_security_SecureRandom_mismatch() + throws Exception { try { Signature sig = Signature.getInstance("DSA"); - sig.initSign(rsaKeys.getPrivate(), new SecureRandom()); - fail("expected InvalidKeyException"); - } catch (InvalidKeyException e) { - // ok - } catch (NoSuchAlgorithmException e) { - fail("unexpected: " + e); + sig.initSign(getRsaKeys().getPrivate(), new SecureRandom()); + fail(); + } catch (InvalidKeyException expected) { } } @@ -275,15 +250,13 @@ public class Signature2Test extends junit.framework.TestCase { * java.security.Signature#initVerify(java.security.PublicKey) */ public void test_initVerifyLjava_security_PublicKey() throws Exception { - Signature.getInstance("DSA").initVerify(dsaKeys.getPublic()); + Signature.getInstance("DSA").initVerify(getDsaKeys().getPublic()); try { - Signature.getInstance("DSA").initVerify(rsaKeys.getPublic()); - fail("expected InvalidKeyException"); - } catch (InvalidKeyException e) { - // ok + Signature.getInstance("DSA").initVerify(getRsaKeys().getPublic()); + fail(); + } catch (InvalidKeyException expected) { } - } /** @@ -304,9 +277,8 @@ public class Signature2Test extends junit.framework.TestCase { try { Signature.getInstance("DSA").initVerify((Certificate) null); - fail("NullPointerException expected"); - } catch (NullPointerException e) { - // fail + fail(); + } catch (NullPointerException expected) { } } finally { Security.removeProvider(myProvider.getName()); @@ -355,7 +327,7 @@ public class Signature2Test extends junit.framework.TestCase { */ public void test_sign() throws Exception { Signature sig = Signature.getInstance("DSA"); - sig.initSign(dsaKeys.getPrivate()); + sig.initSign(getDsaKeys().getPrivate()); sig.update(MESSAGE.getBytes()); sig.sign(); } @@ -373,7 +345,7 @@ public class Signature2Test extends junit.framework.TestCase { */ public void test_update$B() throws Exception { Signature sig = Signature.getInstance("DSA"); - sig.initSign(dsaKeys.getPrivate()); + sig.initSign(getDsaKeys().getPrivate()); byte[] bytes = MESSAGE.getBytes(); sig.update(bytes); @@ -381,9 +353,8 @@ public class Signature2Test extends junit.framework.TestCase { try { Signature sig2 = Signature.getInstance("DSA"); sig2.update(MESSAGE.getBytes()); - fail("expected SignatureException"); - } catch (SignatureException e) { - // ok + fail(); + } catch (SignatureException expected) { } } @@ -396,12 +367,11 @@ public class Signature2Test extends junit.framework.TestCase { try { sig.update(bytes, 0, bytes.length); - fail("expected SignatureException"); - } catch (SignatureException e) { - // ok; + fail(); + } catch (SignatureException expected) { } - sig.initSign(dsaKeys.getPrivate()); + sig.initSign(getDsaKeys().getPrivate()); sig.update(bytes, 0, bytes.length); @@ -410,16 +380,14 @@ public class Signature2Test extends junit.framework.TestCase { try { sig.update(bytes, bytes.length -3, 4); - fail("expected IllegalArgumentException"); - } catch (IllegalArgumentException e) { - // ok + fail(); + } catch (IllegalArgumentException expected) { } try { sig.update(null, 0, 5); - fail("expected IllegalArgumentException"); - } catch (IllegalArgumentException e) { - // ok + fail(); + } catch (IllegalArgumentException expected) { } } @@ -428,7 +396,7 @@ public class Signature2Test extends junit.framework.TestCase { */ public void test_updateB() throws Exception { Signature sig = Signature.getInstance("DSA"); - sig.initSign(dsaKeys.getPrivate()); + sig.initSign(getDsaKeys().getPrivate()); sig.update(MESSAGE.getBytes()[0]); @@ -443,17 +411,13 @@ public class Signature2Test extends junit.framework.TestCase { try { sig.update(buffer); - fail("SignatureException expected"); - } catch (SignatureException e) { - // expected - } - try { - sig.initSign(dsaKeys.getPrivate()); - sig.update(buffer); - } catch (Exception e) { - fail("Unexpected exception " + e.getMessage()); + fail(); + } catch (SignatureException expected) { } + sig.initSign(getDsaKeys().getPrivate()); + sig.update(buffer); + } /** @@ -464,16 +428,15 @@ public class Signature2Test extends junit.framework.TestCase { try { sig.verify(new byte[] { 0,1,2,3 }); - fail("expected SignatureException"); - } catch (SignatureException e) { - // ok + fail(); + } catch (SignatureException expected) { } - sig.initSign(dsaKeys.getPrivate()); + sig.initSign(getDsaKeys().getPrivate()); sig.update(MESSAGE.getBytes()); byte[] signature = sig.sign(); - sig.initVerify(dsaKeys.getPublic()); + sig.initVerify(getDsaKeys().getPublic()); sig.update(MESSAGE.getBytes()); assertTrue("Sign/Verify does not pass", sig.verify(signature)); } @@ -483,41 +446,37 @@ public class Signature2Test extends junit.framework.TestCase { */ public void test_verify$BII() throws Exception { Signature sig = Signature.getInstance("DSA"); - sig.initSign(dsaKeys.getPrivate()); + sig.initSign(getDsaKeys().getPrivate()); sig.update(MESSAGE.getBytes()); byte[] signature = sig.sign(); - sig.initVerify(dsaKeys.getPublic()); + sig.initVerify(getDsaKeys().getPublic()); sig.update(MESSAGE.getBytes()); assertTrue("Sign/Verify does not pass", sig.verify(signature, 0, signature.length)); try { sig.verify(null, 0, signature.length); - fail("IllegalArgumentException expected"); - } catch (IllegalArgumentException e) { - // expected + fail(); + } catch (IllegalArgumentException expected) { } try { sig.verify(signature, -5, signature.length); - fail("IllegalArgumentException expected"); - } catch (IllegalArgumentException e) { - // expected + fail(); + } catch (IllegalArgumentException expected) { } try { sig.verify(signature, signature.length, 0); - fail("SignatureException expected"); - } catch (SignatureException e) { - // expected + fail(); + } catch (SignatureException expected) { } try { sig.verify(signature, 0, signature.length * 2); - fail("IllegalArgumentException expected"); - } catch (IllegalArgumentException e) { - // expected + fail(); + } catch (IllegalArgumentException expected) { } } diff --git a/luni/src/test/java/org/apache/harmony/xnet/provider/jsse/NativeCryptoTest.java b/luni/src/test/java/org/apache/harmony/xnet/provider/jsse/NativeCryptoTest.java index b5f8a82..1dc59c0 100644 --- a/luni/src/test/java/org/apache/harmony/xnet/provider/jsse/NativeCryptoTest.java +++ b/luni/src/test/java/org/apache/harmony/xnet/provider/jsse/NativeCryptoTest.java @@ -54,6 +54,68 @@ public class NativeCryptoTest extends TestCase { private static final long TIMEOUT_SECONDS = 5; + private static byte[] SERVER_PRIVATE_KEY; + private static byte[][] SERVER_CERTIFICATES; + private static byte[] CLIENT_PRIVATE_KEY; + private static byte[][] CLIENT_CERTIFICATES; + private static byte[][] CA_PRINCIPALS; + + private static byte[] getServerPrivateKey() { + initCerts(); + return SERVER_PRIVATE_KEY; + } + + private static byte[][] getServerCertificates() { + initCerts(); + return SERVER_CERTIFICATES; + } + + private static byte[] getClientPrivateKey() { + initCerts(); + return CLIENT_PRIVATE_KEY; + } + + private static byte[][] getClientCertificates() { + initCerts(); + return CLIENT_CERTIFICATES; + } + + private static byte[][] getCaPrincipals() { + initCerts(); + return CA_PRINCIPALS; + } + + /** + * Lazily create shared test certificates. + */ + private static synchronized void initCerts() { + if (SERVER_PRIVATE_KEY != null) { + return; + } + + try { + PrivateKeyEntry serverPrivateKeyEntry + = TestKeyStore.getServer().getPrivateKey("RSA", "RSA"); + SERVER_PRIVATE_KEY = serverPrivateKeyEntry.getPrivateKey().getEncoded(); + SERVER_CERTIFICATES = NativeCrypto.encodeCertificates( + serverPrivateKeyEntry.getCertificateChain()); + + PrivateKeyEntry clientPrivateKeyEntry + = TestKeyStore.getClientCertificate().getPrivateKey("RSA", "RSA"); + CLIENT_PRIVATE_KEY = clientPrivateKeyEntry.getPrivateKey().getEncoded(); + CLIENT_CERTIFICATES = NativeCrypto.encodeCertificates( + clientPrivateKeyEntry.getCertificateChain()); + + KeyStore ks = TestKeyStore.getClient().keyStore; + String caCertAlias = ks.aliases().nextElement(); + X509Certificate certificate = (X509Certificate) ks.getCertificate(caCertAlias); + X500Principal principal = certificate.getIssuerX500Principal(); + CA_PRINCIPALS = new byte[][] { principal.getEncoded() }; + } catch (Exception e) { + throw new RuntimeException(e); + } + } + public static void assertEqualSessions(int expected, int actual) { assertEqualByteArrays(NativeCrypto.SSL_SESSION_session_id(expected), NativeCrypto.SSL_SESSION_session_id(actual)); @@ -108,40 +170,6 @@ public class NativeCryptoTest extends TestCase { NativeCrypto.SSL_CTX_free(c); } - private static final PrivateKeyEntry SERVER_PRIVATE_KEY_ENTRY - = TestKeyStore.getServer().getPrivateKey("RSA", "RSA"); - private static final byte[] SERVER_PRIVATE_KEY - = SERVER_PRIVATE_KEY_ENTRY.getPrivateKey().getEncoded(); - private static final byte[][] SERVER_CERTIFICATES - = encodeCertificates(SERVER_PRIVATE_KEY_ENTRY.getCertificateChain()); - private static final PrivateKeyEntry CLIENT_PRIVATE_KEY_ENTRY - = TestKeyStore.getClientCertificate().getPrivateKey("RSA", "RSA"); - private static final byte[] CLIENT_PRIVATE_KEY - = CLIENT_PRIVATE_KEY_ENTRY.getPrivateKey().getEncoded(); - private static final byte[][] CLIENT_CERTIFICATES - = encodeCertificates(CLIENT_PRIVATE_KEY_ENTRY.getCertificateChain()); - - private static byte[][] encodeCertificates (Certificate[] certificates) { - try { - return NativeCrypto.encodeCertificates(certificates); - } catch (CertificateEncodingException e) { - throw new RuntimeException(e); - } - } - - private static final byte[][] CA_PRINCIPALS; - static { - try { - KeyStore ks = TestKeyStore.getClient().keyStore; - String caCertAlias = ks.aliases().nextElement(); - X509Certificate certificate = (X509Certificate) ks.getCertificate(caCertAlias); - X500Principal principal = certificate.getIssuerX500Principal(); - CA_PRINCIPALS = new byte[][] { principal.getEncoded() }; - } catch (KeyStoreException e) { - throw new RuntimeException(e); - } - } - public void test_SSL_use_certificate() throws Exception { try { NativeCrypto.SSL_use_certificate(NULL, null); @@ -158,7 +186,7 @@ public class NativeCryptoTest extends TestCase { } catch (NullPointerException expected) { } - NativeCrypto.SSL_use_certificate(s, SERVER_CERTIFICATES); + NativeCrypto.SSL_use_certificate(s, getServerCertificates()); NativeCrypto.SSL_free(s); NativeCrypto.SSL_CTX_free(c); @@ -180,7 +208,7 @@ public class NativeCryptoTest extends TestCase { } catch (NullPointerException expected) { } - NativeCrypto.SSL_use_PrivateKey(s, SERVER_PRIVATE_KEY); + NativeCrypto.SSL_use_PrivateKey(s, getServerPrivateKey()); NativeCrypto.SSL_free(s); NativeCrypto.SSL_CTX_free(c); @@ -214,7 +242,7 @@ public class NativeCryptoTest extends TestCase { int s = NativeCrypto.SSL_new(c); // first certificate, then private - NativeCrypto.SSL_use_certificate(s, SERVER_CERTIFICATES); + NativeCrypto.SSL_use_certificate(s, getServerCertificates()); try { NativeCrypto.SSL_check_private_key(s); @@ -222,7 +250,7 @@ public class NativeCryptoTest extends TestCase { } catch (SSLException expected) { } - NativeCrypto.SSL_use_PrivateKey(s, SERVER_PRIVATE_KEY); + NativeCrypto.SSL_use_PrivateKey(s, getServerPrivateKey()); NativeCrypto.SSL_check_private_key(s); NativeCrypto.SSL_free(s); @@ -233,7 +261,7 @@ public class NativeCryptoTest extends TestCase { int s = NativeCrypto.SSL_new(c); // first private, then certificate - NativeCrypto.SSL_use_PrivateKey(s, SERVER_PRIVATE_KEY); + NativeCrypto.SSL_use_PrivateKey(s, getServerPrivateKey()); try { NativeCrypto.SSL_check_private_key(s); @@ -241,7 +269,7 @@ public class NativeCryptoTest extends TestCase { } catch (SSLException expected) { } - NativeCrypto.SSL_use_certificate(s, SERVER_CERTIFICATES); + NativeCrypto.SSL_use_certificate(s, getServerCertificates()); NativeCrypto.SSL_check_private_key(s); NativeCrypto.SSL_free(s); @@ -630,13 +658,13 @@ public class NativeCryptoTest extends TestCase { return s; } }; - Hooks sHooks = new ServerHooks(SERVER_PRIVATE_KEY, SERVER_CERTIFICATES); + Hooks sHooks = new ServerHooks(getServerPrivateKey(), getServerCertificates()); Future<TestSSLHandshakeCallbacks> client = handshake(listener, 0, true, cHooks); Future<TestSSLHandshakeCallbacks> server = handshake(listener, 0, false, sHooks); TestSSLHandshakeCallbacks clientCallback = client.get(TIMEOUT_SECONDS, TimeUnit.SECONDS); TestSSLHandshakeCallbacks serverCallback = server.get(TIMEOUT_SECONDS, TimeUnit.SECONDS); assertTrue(clientCallback.verifyCertificateChainCalled); - assertEqualCertificateChains(SERVER_CERTIFICATES, + assertEqualCertificateChains(getServerCertificates(), clientCallback.asn1DerEncodedCertificateChain); assertEquals("RSA", clientCallback.authMethod); assertFalse(serverCallback.verifyCertificateChainCalled); @@ -660,15 +688,15 @@ public class NativeCryptoTest extends TestCase { @Override public void clientCertificateRequested(int s) { super.clientCertificateRequested(s); - NativeCrypto.SSL_use_PrivateKey(s, CLIENT_PRIVATE_KEY); - NativeCrypto.SSL_use_certificate(s, CLIENT_CERTIFICATES); + NativeCrypto.SSL_use_PrivateKey(s, getClientPrivateKey()); + NativeCrypto.SSL_use_certificate(s, getClientCertificates()); } }; - Hooks sHooks = new ServerHooks(SERVER_PRIVATE_KEY, SERVER_CERTIFICATES) { + Hooks sHooks = new ServerHooks(getServerPrivateKey(), getServerCertificates()) { @Override public int beforeHandshake(int c) throws SSLException { int s = super.beforeHandshake(c); - NativeCrypto.SSL_set_client_CA_list(s, CA_PRINCIPALS); + NativeCrypto.SSL_set_client_CA_list(s, getCaPrincipals()); NativeCrypto.SSL_set_verify(s, NativeCrypto.SSL_VERIFY_PEER); return s; } @@ -678,11 +706,11 @@ public class NativeCryptoTest extends TestCase { TestSSLHandshakeCallbacks clientCallback = client.get(TIMEOUT_SECONDS, TimeUnit.SECONDS); TestSSLHandshakeCallbacks serverCallback = server.get(TIMEOUT_SECONDS, TimeUnit.SECONDS); assertTrue(clientCallback.verifyCertificateChainCalled); - assertEqualCertificateChains(SERVER_CERTIFICATES, + assertEqualCertificateChains(getServerCertificates(), clientCallback.asn1DerEncodedCertificateChain); assertEquals("RSA", clientCallback.authMethod); assertTrue(serverCallback.verifyCertificateChainCalled); - assertEqualCertificateChains(CLIENT_CERTIFICATES, + assertEqualCertificateChains(getClientCertificates(), serverCallback.asn1DerEncodedCertificateChain); assertEquals("RSA", serverCallback.authMethod); @@ -694,7 +722,7 @@ public class NativeCryptoTest extends TestCase { assertEquals("RSA", CipherSuite.getClientKeyType(clientCallback.keyTypes[0])); assertEquals("DSA", CipherSuite.getClientKeyType(clientCallback.keyTypes[1])); assertEquals("EC", CipherSuite.getClientKeyType(clientCallback.keyTypes[2])); - assertEqualPrincipals(CA_PRINCIPALS, + assertEqualPrincipals(getCaPrincipals(), clientCallback.asn1DerEncodedX500Principals); assertFalse(serverCallback.clientCertificateRequestedCalled); @@ -707,11 +735,11 @@ public class NativeCryptoTest extends TestCase { final ServerSocket listener = new ServerSocket(0); try { Hooks cHooks = new Hooks(); - Hooks sHooks = new ServerHooks(SERVER_PRIVATE_KEY, SERVER_CERTIFICATES) { + Hooks sHooks = new ServerHooks(getServerPrivateKey(), getServerCertificates()) { @Override public int beforeHandshake(int c) throws SSLException { int s = super.beforeHandshake(c); - NativeCrypto.SSL_set_client_CA_list(s, CA_PRINCIPALS); + NativeCrypto.SSL_set_client_CA_list(s, getCaPrincipals()); NativeCrypto.SSL_set_verify(s, NativeCrypto.SSL_VERIFY_PEER | NativeCrypto.SSL_VERIFY_FAIL_IF_NO_PEER_CERT); @@ -732,7 +760,7 @@ public class NativeCryptoTest extends TestCase { final ServerSocket listener = new ServerSocket(0); try { Hooks cHooks = new Hooks(); - Hooks sHooks = new ServerHooks(SERVER_PRIVATE_KEY, SERVER_CERTIFICATES); + Hooks sHooks = new ServerHooks(getServerPrivateKey(), getServerCertificates()); Future<TestSSLHandshakeCallbacks> client = handshake(listener, 1, true, cHooks); Future<TestSSLHandshakeCallbacks> server = handshake(listener, -1, false, sHooks); client.get(TIMEOUT_SECONDS, TimeUnit.SECONDS); @@ -747,7 +775,7 @@ public class NativeCryptoTest extends TestCase { final ServerSocket listener = new ServerSocket(0); try { Hooks cHooks = new Hooks(); - Hooks sHooks = new ServerHooks(SERVER_PRIVATE_KEY, SERVER_CERTIFICATES); + Hooks sHooks = new ServerHooks(getServerPrivateKey(), getServerCertificates()); Future<TestSSLHandshakeCallbacks> client = handshake(listener, -1, true, cHooks); Future<TestSSLHandshakeCallbacks> server = handshake(listener, 1, false, sHooks); server.get(TIMEOUT_SECONDS, TimeUnit.SECONDS); @@ -793,7 +821,7 @@ public class NativeCryptoTest extends TestCase { clientSession[0] = session; } }; - Hooks sHooks = new ServerHooks(SERVER_PRIVATE_KEY, SERVER_CERTIFICATES) { + Hooks sHooks = new ServerHooks(getServerPrivateKey(), getServerCertificates()) { @Override public int getContext() throws SSLException { return serverContext; @@ -834,7 +862,7 @@ public class NativeCryptoTest extends TestCase { super.afterHandshake(NULL, s, NULL, sock, fd, callback); } }; - Hooks sHooks = new ServerHooks(SERVER_PRIVATE_KEY, SERVER_CERTIFICATES) { + Hooks sHooks = new ServerHooks(getServerPrivateKey(), getServerCertificates()) { @Override public int getContext() throws SSLException { return serverContext; @@ -888,7 +916,7 @@ public class NativeCryptoTest extends TestCase { return s; } }; - Hooks sHooks = new ServerHooks(SERVER_PRIVATE_KEY, SERVER_CERTIFICATES); + Hooks sHooks = new ServerHooks(getServerPrivateKey(), getServerCertificates()); Future<TestSSLHandshakeCallbacks> client = handshake(listener, 0, true, cHooks); Future<TestSSLHandshakeCallbacks> server = handshake(listener, 0, false, sHooks); client.get(TIMEOUT_SECONDS, TimeUnit.SECONDS); @@ -900,7 +928,7 @@ public class NativeCryptoTest extends TestCase { // negative test case for SSL_set_session_creation_enabled(false) on server try { Hooks cHooks = new Hooks(); - Hooks sHooks = new ServerHooks(SERVER_PRIVATE_KEY, SERVER_CERTIFICATES) { + Hooks sHooks = new ServerHooks(getServerPrivateKey(), getServerCertificates()) { @Override public int beforeHandshake(int c) throws SSLException { int s = super.beforeHandshake(c); @@ -966,7 +994,7 @@ public class NativeCryptoTest extends TestCase { return s; } }; - Hooks sHooks = new ServerHooks(SERVER_PRIVATE_KEY, SERVER_CERTIFICATES) { + Hooks sHooks = new ServerHooks(getServerPrivateKey(), getServerCertificates()) { @Override public void afterHandshake(int session, int s, int c, Socket sock, FileDescriptor fd, @@ -1017,7 +1045,7 @@ public class NativeCryptoTest extends TestCase { super.afterHandshake(session, s, c, sock, fd, callback); } }; - Hooks sHooks = new ServerHooks(SERVER_PRIVATE_KEY, SERVER_CERTIFICATES) { + Hooks sHooks = new ServerHooks(getServerPrivateKey(), getServerCertificates()) { @Override public void afterHandshake(int session, int s, int c, Socket sock, FileDescriptor fd, @@ -1052,14 +1080,14 @@ public class NativeCryptoTest extends TestCase { super.afterHandshake(session, s, c, sock, fd, callback); } }; - Hooks sHooks = new ServerHooks(SERVER_PRIVATE_KEY, SERVER_CERTIFICATES) { + Hooks sHooks = new ServerHooks(getServerPrivateKey(), getServerCertificates()) { @Override public void afterHandshake(int session, int s, int c, Socket sock, FileDescriptor fd, SSLHandshakeCallbacks callback) throws Exception { assertEqualCertificateChains( - SERVER_CERTIFICATES, + getServerCertificates(), NativeCrypto.SSL_get_certificate(s)); super.afterHandshake(session, s, c, sock, fd, callback); } @@ -1086,11 +1114,11 @@ public class NativeCryptoTest extends TestCase { SSLHandshakeCallbacks callback) throws Exception { byte[][] cc = NativeCrypto.SSL_get_peer_cert_chain(s); - assertEqualCertificateChains(SERVER_CERTIFICATES, cc); + assertEqualCertificateChains(getServerCertificates(), cc); super.afterHandshake(session, s, c, sock, fd, callback); } }; - Hooks sHooks = new ServerHooks(SERVER_PRIVATE_KEY, SERVER_CERTIFICATES); + Hooks sHooks = new ServerHooks(getServerPrivateKey(), getServerCertificates()); Future<TestSSLHandshakeCallbacks> client = handshake(listener, 0, true, cHooks); Future<TestSSLHandshakeCallbacks> server = handshake(listener, 0, false, sHooks); client.get(TIMEOUT_SECONDS, TimeUnit.SECONDS); @@ -1157,7 +1185,7 @@ public class NativeCryptoTest extends TestCase { super.afterHandshake(session, s, c, sock, fd, callback); } }; - Hooks sHooks = new ServerHooks(SERVER_PRIVATE_KEY, SERVER_CERTIFICATES) { + Hooks sHooks = new ServerHooks(getServerPrivateKey(), getServerCertificates()) { @Override public void afterHandshake(int session, int s, int c, Socket sock, FileDescriptor fd, @@ -1185,7 +1213,7 @@ public class NativeCryptoTest extends TestCase { fail(); } }; - Hooks sHooks = new ServerHooks(SERVER_PRIVATE_KEY, SERVER_CERTIFICATES) { + Hooks sHooks = new ServerHooks(getServerPrivateKey(), getServerCertificates()) { @Override public void afterHandshake(int session, int s, int c, Socket sock, FileDescriptor fd, @@ -1292,7 +1320,7 @@ public class NativeCryptoTest extends TestCase { super.afterHandshake(session, s, c, sock, fd, callback); } }; - Hooks sHooks = new ServerHooks(SERVER_PRIVATE_KEY, SERVER_CERTIFICATES) { + Hooks sHooks = new ServerHooks(getServerPrivateKey(), getServerCertificates()) { @Override public void afterHandshake(int session, int s, int c, Socket sock, FileDescriptor fd, @@ -1320,7 +1348,7 @@ public class NativeCryptoTest extends TestCase { fail(); } }; - Hooks sHooks = new ServerHooks(SERVER_PRIVATE_KEY, SERVER_CERTIFICATES) { + Hooks sHooks = new ServerHooks(getServerPrivateKey(), getServerCertificates()) { @Override public void afterHandshake(int session, int s, int c, Socket sock, FileDescriptor fd, @@ -1476,7 +1504,7 @@ public class NativeCryptoTest extends TestCase { super.afterHandshake(session, s, c, sock, fd, callback); } }; - Hooks sHooks = new ServerHooks(SERVER_PRIVATE_KEY, SERVER_CERTIFICATES) { + Hooks sHooks = new ServerHooks(getServerPrivateKey(), getServerCertificates()) { @Override public void afterHandshake(int session, final int s, int c, Socket sock, FileDescriptor fd, @@ -1568,7 +1596,7 @@ public class NativeCryptoTest extends TestCase { super.afterHandshake(session, s, c, sock, fd, callback); } }; - Hooks sHooks = new ServerHooks(SERVER_PRIVATE_KEY, SERVER_CERTIFICATES); + Hooks sHooks = new ServerHooks(getServerPrivateKey(), getServerCertificates()); Future<TestSSLHandshakeCallbacks> client = handshake(listener, 0, true, cHooks); Future<TestSSLHandshakeCallbacks> server = handshake(listener, 0, false, sHooks); client.get(TIMEOUT_SECONDS, TimeUnit.SECONDS); @@ -1597,7 +1625,7 @@ public class NativeCryptoTest extends TestCase { super.afterHandshake(session, s, c, sock, fd, callback); } }; - Hooks sHooks = new ServerHooks(SERVER_PRIVATE_KEY, SERVER_CERTIFICATES); + Hooks sHooks = new ServerHooks(getServerPrivateKey(), getServerCertificates()); Future<TestSSLHandshakeCallbacks> client = handshake(listener, 0, true, cHooks); Future<TestSSLHandshakeCallbacks> server = handshake(listener, 0, false, sHooks); client.get(TIMEOUT_SECONDS, TimeUnit.SECONDS); @@ -1625,7 +1653,7 @@ public class NativeCryptoTest extends TestCase { super.afterHandshake(session, s, c, sock, fd, callback); } }; - Hooks sHooks = new ServerHooks(SERVER_PRIVATE_KEY, SERVER_CERTIFICATES); + Hooks sHooks = new ServerHooks(getServerPrivateKey(), getServerCertificates()); Future<TestSSLHandshakeCallbacks> client = handshake(listener, 0, true, cHooks); Future<TestSSLHandshakeCallbacks> server = handshake(listener, 0, false, sHooks); client.get(TIMEOUT_SECONDS, TimeUnit.SECONDS); @@ -1652,7 +1680,7 @@ public class NativeCryptoTest extends TestCase { super.afterHandshake(session, s, c, sock, fd, callback); } }; - Hooks sHooks = new ServerHooks(SERVER_PRIVATE_KEY, SERVER_CERTIFICATES); + Hooks sHooks = new ServerHooks(getServerPrivateKey(), getServerCertificates()); Future<TestSSLHandshakeCallbacks> client = handshake(listener, 0, true, cHooks); Future<TestSSLHandshakeCallbacks> server = handshake(listener, 0, false, sHooks); client.get(TIMEOUT_SECONDS, TimeUnit.SECONDS); @@ -1689,7 +1717,7 @@ public class NativeCryptoTest extends TestCase { super.afterHandshake(session, s, c, sock, fd, callback); } }; - Hooks sHooks = new ServerHooks(SERVER_PRIVATE_KEY, SERVER_CERTIFICATES); + Hooks sHooks = new ServerHooks(getServerPrivateKey(), getServerCertificates()); Future<TestSSLHandshakeCallbacks> client = handshake(listener, 0, true, cHooks); Future<TestSSLHandshakeCallbacks> server = handshake(listener, 0, false, sHooks); client.get(TIMEOUT_SECONDS, TimeUnit.SECONDS); @@ -1715,7 +1743,7 @@ public class NativeCryptoTest extends TestCase { super.afterHandshake(session, s, c, sock, fd, callback); } }; - Hooks sHooks = new ServerHooks(SERVER_PRIVATE_KEY, SERVER_CERTIFICATES) { + Hooks sHooks = new ServerHooks(getServerPrivateKey(), getServerCertificates()) { @Override public int beforeHandshake(int c) throws SSLException { int s = super.beforeHandshake(c); @@ -1763,7 +1791,7 @@ public class NativeCryptoTest extends TestCase { super.afterHandshake(session, s, c, sock, fd, callback); } }; - Hooks sHooks = new ServerHooks(SERVER_PRIVATE_KEY, SERVER_CERTIFICATES); + Hooks sHooks = new ServerHooks(getServerPrivateKey(), getServerCertificates()); Future<TestSSLHandshakeCallbacks> client = handshake(listener, 0, true, cHooks); Future<TestSSLHandshakeCallbacks> server = handshake(listener, 0, false, sHooks); client.get(TIMEOUT_SECONDS, TimeUnit.SECONDS); diff --git a/luni/src/test/java/org/apache/harmony/xnet/provider/jsse/TrustedCertificateStoreTest.java b/luni/src/test/java/org/apache/harmony/xnet/provider/jsse/TrustedCertificateStoreTest.java index 7bac8e8..5141b43 100644 --- a/luni/src/test/java/org/apache/harmony/xnet/provider/jsse/TrustedCertificateStoreTest.java +++ b/luni/src/test/java/org/apache/harmony/xnet/provider/jsse/TrustedCertificateStoreTest.java @@ -41,40 +41,151 @@ public class TrustedCertificateStoreTest extends TestCase { private static final File DIR_ADDED = new File(DIR_TEST, "added"); private static final File DIR_DELETED = new File(DIR_TEST, "removed"); - private static final X509Certificate CA1 = TestKeyStore.getClient().getRootCertificate("RSA"); - private static final X509Certificate CA2 - = TestKeyStore.getClientCA2().getRootCertificate("RSA"); - - private static final KeyStore.PrivateKeyEntry PRIVATE - = TestKeyStore.getServer().getPrivateKey("RSA", "RSA"); - private static final X509Certificate[] CHAIN = (X509Certificate[])PRIVATE.getCertificateChain(); - - private static final X509Certificate CA3_WITH_CA1_SUBJECT - = new TestKeyStore.Builder() - .aliasPrefix("unused") - .subject(CA1.getSubjectX500Principal()) - .ca(true) - .build().getRootCertificate("RSA"); - - - private static final String ALIAS_SYSTEM_CA1 = alias(false, CA1, 0); - private static final String ALIAS_SYSTEM_CA2 = alias(false, CA2, 0); - private static final String ALIAS_USER_CA1 = alias(true, CA1, 0); - private static final String ALIAS_USER_CA2 = alias(true, CA2, 0); - - private static final String ALIAS_SYSTEM_CHAIN0 = alias(false, CHAIN[0], 0); - private static final String ALIAS_SYSTEM_CHAIN1 = alias(false, CHAIN[1], 0); - private static final String ALIAS_SYSTEM_CHAIN2 = alias(false, CHAIN[2], 0); - private static final String ALIAS_USER_CHAIN0 = alias(true, CHAIN[0], 0); - private static final String ALIAS_USER_CHAIN1 = alias(true, CHAIN[1], 0); - private static final String ALIAS_USER_CHAIN2 = alias(true, CHAIN[2], 0); - - private static final String ALIAS_SYSTEM_CA3 = alias(false, CA3_WITH_CA1_SUBJECT, 0); - private static final String ALIAS_SYSTEM_CA3_COLLISION - = alias(false, CA3_WITH_CA1_SUBJECT, 1); - private static final String ALIAS_USER_CA3 = alias(true, CA3_WITH_CA1_SUBJECT, 0); - private static final String ALIAS_USER_CA3_COLLISION - = alias(true, CA3_WITH_CA1_SUBJECT, 1); + private static X509Certificate CA1; + private static X509Certificate CA2; + + private static KeyStore.PrivateKeyEntry PRIVATE; + private static X509Certificate[] CHAIN; + + private static X509Certificate CA3_WITH_CA1_SUBJECT; + private static String ALIAS_SYSTEM_CA1; + private static String ALIAS_SYSTEM_CA2; + private static String ALIAS_USER_CA1; + private static String ALIAS_USER_CA2; + + private static String ALIAS_SYSTEM_CHAIN0; + private static String ALIAS_SYSTEM_CHAIN1; + private static String ALIAS_SYSTEM_CHAIN2; + private static String ALIAS_USER_CHAIN0; + private static String ALIAS_USER_CHAIN1; + private static String ALIAS_USER_CHAIN2; + + private static String ALIAS_SYSTEM_CA3; + private static String ALIAS_SYSTEM_CA3_COLLISION; + private static String ALIAS_USER_CA3; + private static String ALIAS_USER_CA3_COLLISION; + + private static X509Certificate getCa1() { + initCerts(); + return CA1; + } + private static X509Certificate getCa2() { + initCerts(); + return CA2; + } + + private static KeyStore.PrivateKeyEntry getPrivate() { + initCerts(); + return PRIVATE; + } + private static X509Certificate[] getChain() { + initCerts(); + return CHAIN; + } + + private static X509Certificate getCa3WithCa1Subject() { + initCerts(); + return CA3_WITH_CA1_SUBJECT; + } + + private static String getAliasSystemCa1() { + initCerts(); + return ALIAS_SYSTEM_CA1; + } + private static String getAliasSystemCa2() { + initCerts(); + return ALIAS_SYSTEM_CA2; + } + private static String getAliasUserCa1() { + initCerts(); + return ALIAS_USER_CA1; + } + private static String getAliasUserCa2() { + initCerts(); + return ALIAS_USER_CA2; + } + + private static String getAliasSystemChain0() { + initCerts(); + return ALIAS_SYSTEM_CHAIN0; + } + private static String getAliasSystemChain1() { + initCerts(); + return ALIAS_SYSTEM_CHAIN1; + } + private static String getAliasSystemChain2() { + initCerts(); + return ALIAS_SYSTEM_CHAIN2; + } + private static String getAliasUserChain0() { + initCerts(); + return ALIAS_USER_CHAIN0; + } + private static String getAliasUserChain1() { + initCerts(); + return ALIAS_USER_CHAIN1; + } + private static String getAliasUserChain2() { + initCerts(); + return ALIAS_USER_CHAIN2; + } + + private static String getAliasSystemCa3() { + initCerts(); + return ALIAS_SYSTEM_CA3; + } + private static String getAliasSystemCa3Collision() { + initCerts(); + return ALIAS_SYSTEM_CA3_COLLISION; + } + private static String getAliasUserCa3() { + initCerts(); + return ALIAS_USER_CA3; + } + private static String getAliasUserCa3Collision() { + initCerts(); + return ALIAS_USER_CA3_COLLISION; + } + + /** + * Lazily create shared test certificates. + */ + private static synchronized void initCerts() { + if (CA1 != null) { + return; + } + try { + CA1 = TestKeyStore.getClient().getRootCertificate("RSA"); + CA2 = TestKeyStore.getClientCA2().getRootCertificate("RSA"); + PRIVATE = TestKeyStore.getServer().getPrivateKey("RSA", "RSA"); + CHAIN = (X509Certificate[]) PRIVATE.getCertificateChain(); + CA3_WITH_CA1_SUBJECT = new TestKeyStore.Builder() + .aliasPrefix("unused") + .subject(CA1.getSubjectX500Principal()) + .ca(true) + .build().getRootCertificate("RSA"); + + + ALIAS_SYSTEM_CA1 = alias(false, CA1, 0); + ALIAS_SYSTEM_CA2 = alias(false, CA2, 0); + ALIAS_USER_CA1 = alias(true, CA1, 0); + ALIAS_USER_CA2 = alias(true, CA2, 0); + + ALIAS_SYSTEM_CHAIN0 = alias(false, getChain()[0], 0); + ALIAS_SYSTEM_CHAIN1 = alias(false, getChain()[1], 0); + ALIAS_SYSTEM_CHAIN2 = alias(false, getChain()[2], 0); + ALIAS_USER_CHAIN0 = alias(true, getChain()[0], 0); + ALIAS_USER_CHAIN1 = alias(true, getChain()[1], 0); + ALIAS_USER_CHAIN2 = alias(true, getChain()[2], 0); + + ALIAS_SYSTEM_CA3 = alias(false, CA3_WITH_CA1_SUBJECT, 0); + ALIAS_SYSTEM_CA3_COLLISION = alias(false, CA3_WITH_CA1_SUBJECT, 1); + ALIAS_USER_CA3 = alias(true, CA3_WITH_CA1_SUBJECT, 0); + ALIAS_USER_CA3_COLLISION = alias(true, CA3_WITH_CA1_SUBJECT, 1); + } catch (Exception e) { + throw new RuntimeException(e); + } + } private TrustedCertificateStore store; @@ -118,24 +229,24 @@ public class TrustedCertificateStoreTest extends TestCase { } public void testOneSystemOneDeleted() throws Exception { - install(CA1, ALIAS_SYSTEM_CA1); - store.deleteCertificateEntry(ALIAS_SYSTEM_CA1); + install(getCa1(), getAliasSystemCa1()); + store.deleteCertificateEntry(getAliasSystemCa1()); assertEmpty(); - assertDeleted(CA1, ALIAS_SYSTEM_CA1); + assertDeleted(getCa1(), getAliasSystemCa1()); } public void testTwoSystemTwoDeleted() throws Exception { - install(CA1, ALIAS_SYSTEM_CA1); - store.deleteCertificateEntry(ALIAS_SYSTEM_CA1); - install(CA2, ALIAS_SYSTEM_CA2); - store.deleteCertificateEntry(ALIAS_SYSTEM_CA2); + install(getCa1(), getAliasSystemCa1()); + store.deleteCertificateEntry(getAliasSystemCa1()); + install(getCa2(), getAliasSystemCa2()); + store.deleteCertificateEntry(getAliasSystemCa2()); assertEmpty(); - assertDeleted(CA1, ALIAS_SYSTEM_CA1); - assertDeleted(CA2, ALIAS_SYSTEM_CA2); + assertDeleted(getCa1(), getAliasSystemCa1()); + assertDeleted(getCa2(), getAliasSystemCa2()); } public void testPartialFileIsIgnored() throws Exception { - File file = file(ALIAS_SYSTEM_CA1); + File file = file(getAliasSystemCa1()); OutputStream os = new FileOutputStream(file); os.write(0); os.close(); @@ -172,21 +283,21 @@ public class TrustedCertificateStoreTest extends TestCase { assertFalse(store.containsAlias("")); assertNull(store.getCertificateAlias(null)); - assertNull(store.getCertificateAlias(CA1)); + assertNull(store.getCertificateAlias(getCa1())); try { store.isTrustAnchor(null); fail(); } catch (NullPointerException expected) { } - assertFalse(store.isTrustAnchor(CA1)); + assertFalse(store.isTrustAnchor(getCa1())); try { store.findIssuer(null); fail(); } catch (NullPointerException expected) { } - assertNull(store.findIssuer(CA1)); + assertNull(store.findIssuer(getCa1())); try { store.installCertificate(null); @@ -202,100 +313,100 @@ public class TrustedCertificateStoreTest extends TestCase { } public void testTwoSystem() throws Exception { - testTwo(CA1, ALIAS_SYSTEM_CA1, - CA2, ALIAS_SYSTEM_CA2); + testTwo(getCa1(), getAliasSystemCa1(), + getCa2(), getAliasSystemCa2()); } public void testTwoUser() throws Exception { - testTwo(CA1, ALIAS_USER_CA1, - CA2, ALIAS_USER_CA2); + testTwo(getCa1(), getAliasUserCa1(), + getCa2(), getAliasUserCa2()); } public void testOneSystemOneUser() throws Exception { - testTwo(CA1, ALIAS_SYSTEM_CA1, - CA2, ALIAS_USER_CA2); + testTwo(getCa1(), getAliasSystemCa1(), + getCa2(), getAliasUserCa2()); } public void testTwoSystemSameSubject() throws Exception { - testTwo(CA1, ALIAS_SYSTEM_CA1, - CA3_WITH_CA1_SUBJECT, ALIAS_SYSTEM_CA3_COLLISION); + testTwo(getCa1(), getAliasSystemCa1(), + getCa3WithCa1Subject(), getAliasSystemCa3Collision()); } public void testTwoUserSameSubject() throws Exception { - testTwo(CA1, ALIAS_USER_CA1, - CA3_WITH_CA1_SUBJECT, ALIAS_USER_CA3_COLLISION); - - store.deleteCertificateEntry(ALIAS_USER_CA1); - assertDeleted(CA1, ALIAS_USER_CA1); - assertTombstone(ALIAS_USER_CA1); - assertRootCA(CA3_WITH_CA1_SUBJECT, ALIAS_USER_CA3_COLLISION); - assertAliases(ALIAS_USER_CA3_COLLISION); - - store.deleteCertificateEntry(ALIAS_USER_CA3_COLLISION); - assertDeleted(CA3_WITH_CA1_SUBJECT, ALIAS_USER_CA3_COLLISION); - assertNoTombstone(ALIAS_USER_CA3_COLLISION); - assertNoTombstone(ALIAS_USER_CA1); + testTwo(getCa1(), getAliasUserCa1(), + getCa3WithCa1Subject(), getAliasUserCa3Collision()); + + store.deleteCertificateEntry(getAliasUserCa1()); + assertDeleted(getCa1(), getAliasUserCa1()); + assertTombstone(getAliasUserCa1()); + assertRootCa(getCa3WithCa1Subject(), getAliasUserCa3Collision()); + assertAliases(getAliasUserCa3Collision()); + + store.deleteCertificateEntry(getAliasUserCa3Collision()); + assertDeleted(getCa3WithCa1Subject(), getAliasUserCa3Collision()); + assertNoTombstone(getAliasUserCa3Collision()); + assertNoTombstone(getAliasUserCa1()); assertEmpty(); } public void testOneSystemOneUserSameSubject() throws Exception { - testTwo(CA1, ALIAS_SYSTEM_CA1, - CA3_WITH_CA1_SUBJECT, ALIAS_USER_CA3); - testTwo(CA1, ALIAS_USER_CA1, - CA3_WITH_CA1_SUBJECT, ALIAS_SYSTEM_CA3); + testTwo(getCa1(), getAliasSystemCa1(), + getCa3WithCa1Subject(), getAliasUserCa3()); + testTwo(getCa1(), getAliasUserCa1(), + getCa3WithCa1Subject(), getAliasSystemCa3()); } private void testTwo(X509Certificate x1, String alias1, X509Certificate x2, String alias2) { install(x1, alias1); install(x2, alias2); - assertRootCA(x1, alias1); - assertRootCA(x2, alias2); + assertRootCa(x1, alias1); + assertRootCa(x2, alias2); assertAliases(alias1, alias2); } public void testOneSystemOneUserOneDeleted() throws Exception { - install(CA1, ALIAS_SYSTEM_CA1); - store.installCertificate(CA2); - store.deleteCertificateEntry(ALIAS_SYSTEM_CA1); - assertDeleted(CA1, ALIAS_SYSTEM_CA1); - assertRootCA(CA2, ALIAS_USER_CA2); - assertAliases(ALIAS_USER_CA2); + install(getCa1(), getAliasSystemCa1()); + store.installCertificate(getCa2()); + store.deleteCertificateEntry(getAliasSystemCa1()); + assertDeleted(getCa1(), getAliasSystemCa1()); + assertRootCa(getCa2(), getAliasUserCa2()); + assertAliases(getAliasUserCa2()); } public void testOneSystemOneUserOneDeletedSameSubject() throws Exception { - install(CA1, ALIAS_SYSTEM_CA1); - store.installCertificate(CA3_WITH_CA1_SUBJECT); - store.deleteCertificateEntry(ALIAS_SYSTEM_CA1); - assertDeleted(CA1, ALIAS_SYSTEM_CA1); - assertRootCA(CA3_WITH_CA1_SUBJECT, ALIAS_USER_CA3); - assertAliases(ALIAS_USER_CA3); + install(getCa1(), getAliasSystemCa1()); + store.installCertificate(getCa3WithCa1Subject()); + store.deleteCertificateEntry(getAliasSystemCa1()); + assertDeleted(getCa1(), getAliasSystemCa1()); + assertRootCa(getCa3WithCa1Subject(), getAliasUserCa3()); + assertAliases(getAliasUserCa3()); } public void testUserMaskingSystem() throws Exception { - install(CA1, ALIAS_SYSTEM_CA1); - install(CA1, ALIAS_USER_CA1); - assertMasked(CA1, ALIAS_SYSTEM_CA1); - assertRootCA(CA1, ALIAS_USER_CA1); - assertAliases(ALIAS_SYSTEM_CA1, ALIAS_USER_CA1); + install(getCa1(), getAliasSystemCa1()); + install(getCa1(), getAliasUserCa1()); + assertMasked(getCa1(), getAliasSystemCa1()); + assertRootCa(getCa1(), getAliasUserCa1()); + assertAliases(getAliasSystemCa1(), getAliasUserCa1()); } public void testChain() throws Exception { - testChain(ALIAS_SYSTEM_CHAIN1, ALIAS_SYSTEM_CHAIN2); - testChain(ALIAS_SYSTEM_CHAIN1, ALIAS_USER_CHAIN2); - testChain(ALIAS_USER_CHAIN1, ALIAS_SYSTEM_CA1); - testChain(ALIAS_USER_CHAIN1, ALIAS_USER_CHAIN2); + testChain(getAliasSystemChain1(), getAliasSystemChain2()); + testChain(getAliasSystemChain1(), getAliasUserChain2()); + testChain(getAliasUserChain1(), getAliasSystemCa1()); + testChain(getAliasUserChain1(), getAliasUserChain2()); } private void testChain(String alias1, String alias2) throws Exception { - install(CHAIN[1], alias1); - install(CHAIN[2], alias2); - assertIntermediateCA(CHAIN[1], alias1); - assertRootCA(CHAIN[2], alias2); + install(getChain()[1], alias1); + install(getChain()[2], alias2); + assertIntermediateCa(getChain()[1], alias1); + assertRootCa(getChain()[2], alias2); assertAliases(alias1, alias2); - assertEquals(CHAIN[2], store.findIssuer(CHAIN[1])); - assertEquals(CHAIN[1], store.findIssuer(CHAIN[0])); + assertEquals(getChain()[2], store.findIssuer(getChain()[1])); + assertEquals(getChain()[1], store.findIssuer(getChain()[0])); resetStore(); } @@ -311,104 +422,104 @@ public class TrustedCertificateStoreTest extends TestCase { public void testWithExistingUserDirectories() throws Exception { DIR_ADDED.mkdirs(); DIR_DELETED.mkdirs(); - install(CA1, ALIAS_SYSTEM_CA1); - assertRootCA(CA1, ALIAS_SYSTEM_CA1); - assertAliases(ALIAS_SYSTEM_CA1); + install(getCa1(), getAliasSystemCa1()); + assertRootCa(getCa1(), getAliasSystemCa1()); + assertAliases(getAliasSystemCa1()); } - public void testIsTrustAnchorWithReissuedCA() throws Exception { - PublicKey publicKey = PRIVATE.getCertificate().getPublicKey(); - PrivateKey privateKey = PRIVATE.getPrivateKey(); + public void testIsTrustAnchorWithReissuedgetCa() throws Exception { + PublicKey publicKey = getPrivate().getCertificate().getPublicKey(); + PrivateKey privateKey = getPrivate().getPrivateKey(); String name = "CN=CA4"; - X509Certificate ca1 = TestKeyStore.createCA(publicKey, privateKey, name); + X509Certificate ca1 = TestKeyStore.createCa(publicKey, privateKey, name); Thread.sleep(1 * 1000); // wait to ensure CAs vary by expiration - X509Certificate ca2 = TestKeyStore.createCA(publicKey, privateKey, name); + X509Certificate ca2 = TestKeyStore.createCa(publicKey, privateKey, name); assertFalse(ca1.equals(ca2)); String systemAlias = alias(false, ca1, 0); install(ca1, systemAlias); - assertRootCA(ca1, systemAlias); + assertRootCa(ca1, systemAlias); assertTrue(store.isTrustAnchor(ca2)); assertEquals(ca1, store.findIssuer(ca2)); resetStore(); String userAlias = alias(true, ca1, 0); store.installCertificate(ca1); - assertRootCA(ca1, userAlias); + assertRootCa(ca1, userAlias); assertTrue(store.isTrustAnchor(ca2)); assertEquals(ca1, store.findIssuer(ca2)); resetStore(); } public void testInstallEmpty() throws Exception { - store.installCertificate(CA1); - assertRootCA(CA1, ALIAS_USER_CA1); - assertAliases(ALIAS_USER_CA1); + store.installCertificate(getCa1()); + assertRootCa(getCa1(), getAliasUserCa1()); + assertAliases(getAliasUserCa1()); // reinstalling should not change anything - store.installCertificate(CA1); - assertRootCA(CA1, ALIAS_USER_CA1); - assertAliases(ALIAS_USER_CA1); + store.installCertificate(getCa1()); + assertRootCa(getCa1(), getAliasUserCa1()); + assertAliases(getAliasUserCa1()); } public void testInstallEmptySystemExists() throws Exception { - install(CA1, ALIAS_SYSTEM_CA1); - assertRootCA(CA1, ALIAS_SYSTEM_CA1); - assertAliases(ALIAS_SYSTEM_CA1); + install(getCa1(), getAliasSystemCa1()); + assertRootCa(getCa1(), getAliasSystemCa1()); + assertAliases(getAliasSystemCa1()); // reinstalling should not affect system CA - store.installCertificate(CA1); - assertRootCA(CA1, ALIAS_SYSTEM_CA1); - assertAliases(ALIAS_SYSTEM_CA1); + store.installCertificate(getCa1()); + assertRootCa(getCa1(), getAliasSystemCa1()); + assertAliases(getAliasSystemCa1()); } public void testInstallEmptyDeletedSystemExists() throws Exception { - install(CA1, ALIAS_SYSTEM_CA1); - store.deleteCertificateEntry(ALIAS_SYSTEM_CA1); + install(getCa1(), getAliasSystemCa1()); + store.deleteCertificateEntry(getAliasSystemCa1()); assertEmpty(); - assertDeleted(CA1, ALIAS_SYSTEM_CA1); + assertDeleted(getCa1(), getAliasSystemCa1()); // installing should restore deleted system CA - store.installCertificate(CA1); - assertRootCA(CA1, ALIAS_SYSTEM_CA1); - assertAliases(ALIAS_SYSTEM_CA1); + store.installCertificate(getCa1()); + assertRootCa(getCa1(), getAliasSystemCa1()); + assertAliases(getAliasSystemCa1()); } public void testDeleteEmpty() throws Exception { - store.deleteCertificateEntry(ALIAS_SYSTEM_CA1); + store.deleteCertificateEntry(getAliasSystemCa1()); assertEmpty(); - assertDeleted(CA1, ALIAS_SYSTEM_CA1); + assertDeleted(getCa1(), getAliasSystemCa1()); } public void testDeleteUser() throws Exception { - store.installCertificate(CA1); - assertRootCA(CA1, ALIAS_USER_CA1); - assertAliases(ALIAS_USER_CA1); + store.installCertificate(getCa1()); + assertRootCa(getCa1(), getAliasUserCa1()); + assertAliases(getAliasUserCa1()); - store.deleteCertificateEntry(ALIAS_USER_CA1); + store.deleteCertificateEntry(getAliasUserCa1()); assertEmpty(); - assertDeleted(CA1, ALIAS_USER_CA1); - assertNoTombstone(ALIAS_USER_CA1); + assertDeleted(getCa1(), getAliasUserCa1()); + assertNoTombstone(getAliasUserCa1()); } public void testDeleteSystem() throws Exception { - install(CA1, ALIAS_SYSTEM_CA1); - assertRootCA(CA1, ALIAS_SYSTEM_CA1); - assertAliases(ALIAS_SYSTEM_CA1); + install(getCa1(), getAliasSystemCa1()); + assertRootCa(getCa1(), getAliasSystemCa1()); + assertAliases(getAliasSystemCa1()); - store.deleteCertificateEntry(ALIAS_SYSTEM_CA1); + store.deleteCertificateEntry(getAliasSystemCa1()); assertEmpty(); - assertDeleted(CA1, ALIAS_SYSTEM_CA1); + assertDeleted(getCa1(), getAliasSystemCa1()); // deleting again should not change anything - store.deleteCertificateEntry(ALIAS_SYSTEM_CA1); + store.deleteCertificateEntry(getAliasSystemCa1()); assertEmpty(); - assertDeleted(CA1, ALIAS_SYSTEM_CA1); + assertDeleted(getCa1(), getAliasSystemCa1()); } - private void assertRootCA(X509Certificate x, String alias) { - assertIntermediateCA(x, alias); + private void assertRootCa(X509Certificate x, String alias) { + assertIntermediateCa(x, alias); assertEquals(x, store.findIssuer(x)); } @@ -419,7 +530,7 @@ public class TrustedCertificateStoreTest extends TestCase { assertTrue(store.isTrustAnchor(x)); } - private void assertIntermediateCA(X509Certificate x, String alias) { + private void assertIntermediateCa(X509Certificate x, String alias) { assertTrusted(x, alias); assertEquals(alias, store.getCertificateAlias(x)); } diff --git a/luni/src/test/java/tests/api/javax/net/ssl/TrustManagerFactory1Test.java b/luni/src/test/java/tests/api/javax/net/ssl/TrustManagerFactory1Test.java index 1140984..2f9da81 100644 --- a/luni/src/test/java/tests/api/javax/net/ssl/TrustManagerFactory1Test.java +++ b/luni/src/test/java/tests/api/javax/net/ssl/TrustManagerFactory1Test.java @@ -18,7 +18,6 @@ package tests.api.javax.net.ssl; import dalvik.annotation.KnownFailure; - import java.io.IOException; import java.security.InvalidAlgorithmParameterException; import java.security.KeyStore; @@ -29,25 +28,20 @@ import java.security.Provider; import java.security.PublicKey; import java.security.Security; import java.security.cert.CertificateException; - +import java.security.cert.PKIXBuilderParameters; +import java.security.cert.TrustAnchor; +import java.security.cert.X509CertSelector; +import java.util.HashSet; +import java.util.Set; +import javax.net.ssl.CertPathTrustManagerParameters; import javax.net.ssl.ManagerFactoryParameters; import javax.net.ssl.TrustManager; import javax.net.ssl.TrustManagerFactory; import javax.net.ssl.TrustManagerFactorySpi; - +import junit.framework.TestCase; import org.apache.harmony.security.tests.support.SpiEngUtils; import org.apache.harmony.security.tests.support.TestKeyPair; import org.apache.harmony.xnet.tests.support.MyTrustManagerFactorySpi; -import junit.framework.TestCase; - -// -import java.security.cert.TrustAnchor; -import java.security.cert.X509CertSelector; -import java.security.cert.PKIXBuilderParameters; -import javax.net.ssl.CertPathTrustManagerParameters; - -import java.util.HashSet; -import java.util.Set; /** * Tests for <code>TrustManagerFactory</code> class constructors and methods. @@ -57,72 +51,60 @@ public class TrustManagerFactory1Test extends TestCase { private static final String srvTrustManagerFactory = "TrustManagerFactory"; - private static String defaultAlgorithm = null; - - private static String defaultProviderName = null; - - private static Provider defaultProvider = null; - - private static boolean DEFSupported = false; - - private static final String NotSupportedMsg = "There is no suitable provider for TrustManagerFactory"; - private static final String[] invalidValues = SpiEngUtils.invalidValues; - private static String[] validValues = new String[3]; - static { - defaultAlgorithm = Security - .getProperty("ssl.TrustManagerFactory.algorithm"); - if (defaultAlgorithm != null) { - defaultProvider = SpiEngUtils.isSupport(defaultAlgorithm, - srvTrustManagerFactory); - DEFSupported = (defaultProvider != null); - defaultProviderName = (DEFSupported ? defaultProvider.getName() - : null); - validValues[0] = defaultAlgorithm; - validValues[1] = defaultAlgorithm.toUpperCase(); - validValues[2] = defaultAlgorithm.toLowerCase(); + private static String DEFAULT_ALGORITHM; + private static String DEFAULT_PROVIDER_NAME; + private static Provider DEFAULT_PROVIDER; + private static String[] VALID_VALUES; + + private static String getDefaultAlgorithm() { + init(); + return DEFAULT_ALGORITHM; + } + private static String getDefaultProviderName() { + init(); + return DEFAULT_PROVIDER_NAME; + } + private static Provider getDefaultProvider() { + init(); + return DEFAULT_PROVIDER; + } + private static String[] getValidValues() { + init(); + return VALID_VALUES; + } + + private static synchronized void init() { + if (DEFAULT_ALGORITHM != null) { + return; } + DEFAULT_ALGORITHM = Security.getProperty("ssl.TrustManagerFactory.algorithm"); + assertNotNull(DEFAULT_ALGORITHM); + DEFAULT_PROVIDER = SpiEngUtils.isSupport(DEFAULT_ALGORITHM, srvTrustManagerFactory); + DEFAULT_PROVIDER_NAME = DEFAULT_PROVIDER.getName(); + VALID_VALUES = new String[] { DEFAULT_ALGORITHM, + DEFAULT_ALGORITHM.toUpperCase(), + DEFAULT_ALGORITHM.toLowerCase() }; } - protected TrustManagerFactory[] createTMFac() { - if (!DEFSupported) { - fail(defaultAlgorithm + " algorithm is not supported"); - return null; - } - TrustManagerFactory[] tMF = new TrustManagerFactory[3]; - try { - tMF[0] = TrustManagerFactory.getInstance(defaultAlgorithm); - tMF[1] = TrustManagerFactory.getInstance(defaultAlgorithm, - defaultProvider); - tMF[2] = TrustManagerFactory.getInstance(defaultAlgorithm, - defaultProviderName); - return tMF; - } catch (Exception e) { - e.printStackTrace(); - return null; - } + private static TrustManagerFactory[] createTMFac() throws Exception { + return new TrustManagerFactory[] { + TrustManagerFactory.getInstance(getDefaultAlgorithm()), + TrustManagerFactory.getInstance(getDefaultAlgorithm(), getDefaultProvider()), + TrustManagerFactory.getInstance(getDefaultAlgorithm(), getDefaultProviderName()) + }; } - /** - * Test for - * <code>TrustManagerFactory(TrustManagerFactorySpi impl, Provider prov, String algoriyjm) </code> - * constructor - * Assertion: created new TrustManagerFactory object - */ public void test_ConstructorLjavax_net_ssl_TrustManagerFactorySpiLjava_security_ProviderLjava_lang_String() - throws NoSuchAlgorithmException { - if (!DEFSupported) { - fail(NotSupportedMsg); - return; - } + throws NoSuchAlgorithmException { TrustManagerFactorySpi spi = new MyTrustManagerFactorySpi(); - TrustManagerFactory tmF = new myTrustManagerFactory(spi, defaultProvider, - defaultAlgorithm); + TrustManagerFactory tmF = new myTrustManagerFactory(spi, getDefaultProvider(), + getDefaultAlgorithm()); assertTrue("Not CertStore object", tmF instanceof TrustManagerFactory); assertEquals("Incorrect algorithm", tmF.getAlgorithm(), - defaultAlgorithm); - assertEquals("Incorrect provider", tmF.getProvider(), defaultProvider); + getDefaultAlgorithm()); + assertEquals("Incorrect provider", tmF.getProvider(), getDefaultProvider()); assertNull("Incorrect result", tmF.getTrustManagers()); tmF = new myTrustManagerFactory(null, null, null); @@ -144,19 +126,18 @@ public class TrustManagerFactory1Test extends TestCase { */ public void test_getAlgorithm() throws NoSuchAlgorithmException, NoSuchProviderException { - if (!DEFSupported) fail(NotSupportedMsg); assertEquals("Incorrect algorithm", - defaultAlgorithm, + getDefaultAlgorithm(), TrustManagerFactory - .getInstance(defaultAlgorithm).getAlgorithm()); + .getInstance(getDefaultAlgorithm()).getAlgorithm()); assertEquals("Incorrect algorithm", - defaultAlgorithm, + getDefaultAlgorithm(), TrustManagerFactory - .getInstance(defaultAlgorithm, defaultProviderName) + .getInstance(getDefaultAlgorithm(), getDefaultProviderName()) .getAlgorithm()); assertEquals("Incorrect algorithm", - defaultAlgorithm, - TrustManagerFactory.getInstance(defaultAlgorithm, defaultProvider) + getDefaultAlgorithm(), + TrustManagerFactory.getInstance(getDefaultAlgorithm(), getDefaultProvider()) .getAlgorithm()); } @@ -165,25 +146,21 @@ public class TrustManagerFactory1Test extends TestCase { * Assertion: returns value which is specifoed in security property */ public void test_getDefaultAlgorithm() { - if (!DEFSupported) { - fail(NotSupportedMsg); - return; - } String def = TrustManagerFactory.getDefaultAlgorithm(); - if (defaultAlgorithm == null) { + if (getDefaultAlgorithm() == null) { assertNull("DefaultAlgorithm must be null", def); } else { - assertEquals("Invalid default algorithm", def, defaultAlgorithm); + assertEquals("Invalid default algorithm", def, getDefaultAlgorithm()); } String defA = "Proba.trustmanagerfactory.defaul.type"; Security.setProperty("ssl.TrustManagerFactory.algorithm", defA); - assertEquals("Incorrect defaultAlgorithm", + assertEquals("Incorrect getDefaultAlgorithm()", TrustManagerFactory.getDefaultAlgorithm(), defA); if (def == null) { def = ""; } Security.setProperty("ssl.TrustManagerFactory.algorithm", def); - assertEquals("Incorrect defaultAlgorithm", + assertEquals("Incorrect getDefaultAlgorithm()", TrustManagerFactory.getDefaultAlgorithm(), def); } @@ -193,17 +170,11 @@ public class TrustManagerFactory1Test extends TestCase { * returns instance of TrustManagerFactory */ public void test_getInstanceLjava_lang_String01() throws NoSuchAlgorithmException { - if (!DEFSupported) { - fail(NotSupportedMsg); - return; - } - TrustManagerFactory trustMF; - for (int i = 0; i < validValues.length; i++) { - trustMF = TrustManagerFactory.getInstance(validValues[i]); + for (String validValue : getValidValues()) { + TrustManagerFactory trustMF = TrustManagerFactory.getInstance(validValue); assertTrue("Not TrustManagerFactory object", - trustMF instanceof TrustManagerFactory); - assertEquals("Invalid algorithm", trustMF.getAlgorithm(), - validValues[i]); + trustMF instanceof TrustManagerFactory); + assertEquals("Invalid algorithm", trustMF.getAlgorithm(), validValue); } } @@ -216,9 +187,9 @@ public class TrustManagerFactory1Test extends TestCase { public void test_getInstanceLjava_lang_String02() { try { TrustManagerFactory.getInstance(null); - fail("NoSuchAlgorithmException or NullPointerException should be thrown (algorithm is null"); - } catch (NoSuchAlgorithmException e) { - } catch (NullPointerException e) { + fail(); + } catch (NoSuchAlgorithmException expected) { + } catch (NullPointerException expected) { } for (int i = 0; i < invalidValues.length; i++) { try { @@ -236,23 +207,17 @@ public class TrustManagerFactory1Test extends TestCase { * Assertion: throws IllegalArgumentException when provider is null * or empty */ - public void test_getInstanceLjava_lang_StringLjava_lang_String01() throws NoSuchProviderException, - NoSuchAlgorithmException { - if (!DEFSupported) { - fail(NotSupportedMsg); - return; - } - String provider = null; - for (int i = 0; i < validValues.length; i++) { + public void test_getInstanceLjava_lang_StringLjava_lang_String01() throws Exception { + for (String validValue : getValidValues()) { try { - TrustManagerFactory.getInstance(validValues[i], provider); - fail("IllegalArgumentException must be thrown when provider is null"); - } catch (IllegalArgumentException e) { + TrustManagerFactory.getInstance(validValue, (String) null); + fail(); + } catch (IllegalArgumentException expected) { } try { - TrustManagerFactory.getInstance(validValues[i], ""); - fail("IllegalArgumentException must be thrown when provider is empty"); - } catch (IllegalArgumentException e) { + TrustManagerFactory.getInstance(validValue, ""); + fail(); + } catch (IllegalArgumentException expected) { } } } @@ -264,21 +229,17 @@ public class TrustManagerFactory1Test extends TestCase { * throws NullPointerException when algorithm is null; * throws NoSuchAlgorithmException when algorithm is not correct; */ - public void test_getInstanceLjava_lang_StringLjava_lang_String02() throws NoSuchProviderException { - if (!DEFSupported) { - fail(NotSupportedMsg); - return; - } + public void test_getInstanceLjava_lang_StringLjava_lang_String02() throws Exception { try { - TrustManagerFactory.getInstance(null, defaultProviderName); - fail("NoSuchAlgorithmException or NullPointerException should be thrown (algorithm is null"); - } catch (NoSuchAlgorithmException e) { - } catch (NullPointerException e) { + TrustManagerFactory.getInstance(null, getDefaultProviderName()); + fail(); + } catch (NoSuchAlgorithmException expected) { + } catch (NullPointerException expected) { } for (int i = 0; i < invalidValues.length; i++) { try { TrustManagerFactory.getInstance(invalidValues[i], - defaultProviderName); + getDefaultProviderName()); fail("NoSuchAlgorithmException must be thrown (algorithm: " .concat(invalidValues[i]).concat(")")); } catch (NoSuchAlgorithmException e) { @@ -292,20 +253,15 @@ public class TrustManagerFactory1Test extends TestCase { * Assertion: throws NoSuchProviderException when provider has * invalid value */ - public void test_getInstanceLjava_lang_StringLjava_lang_String03() throws NoSuchAlgorithmException { - if (!DEFSupported) { - fail(NotSupportedMsg); - return; - } - for (int i = 1; i < invalidValues.length; i++) { - for (int j = 0; j < validValues.length; j++) { + public void test_getInstanceLjava_lang_StringLjava_lang_String03() throws Exception { + for (String invalidValue : invalidValues) { + for (String validValue : getValidValues()) { try { - TrustManagerFactory.getInstance(validValues[j], - invalidValues[i]); - fail("NuSuchProviderException must be thrown (algorithm: " - .concat(validValues[j]).concat(" provider: ") - .concat(invalidValues[i]).concat(")")); - } catch (NoSuchProviderException e) { + TrustManagerFactory.getInstance(validValue, invalidValue); + fail("NoSuchProviderException must be thrown (algorithm: " + .concat(validValue).concat(" provider: ") + .concat(invalidValue).concat(")")); + } catch (NoSuchProviderException expected) { } } } @@ -316,22 +272,13 @@ public class TrustManagerFactory1Test extends TestCase { * method * Assertion: returns instance of TrustManagerFactory */ - public void test_getInstanceLjava_lang_StringLjava_lang_String04() throws NoSuchAlgorithmException, - NoSuchProviderException { - if (!DEFSupported) { - fail(NotSupportedMsg); - return; - } - TrustManagerFactory trustMF; - for (int i = 0; i < validValues.length; i++) { - trustMF = TrustManagerFactory.getInstance(validValues[i], - defaultProviderName); + public void test_getInstanceLjava_lang_StringLjava_lang_String04() throws Exception { + for (String validValue : getValidValues()) { + TrustManagerFactory trustMF = TrustManagerFactory.getInstance(validValue, getDefaultProviderName()); assertTrue("Not TrustManagerFactory object", - trustMF instanceof TrustManagerFactory); - assertEquals("Invalid algorithm", trustMF.getAlgorithm(), - validValues[i]); - assertEquals("Invalid provider", trustMF.getProvider(), - defaultProvider); + trustMF instanceof TrustManagerFactory); + assertEquals("Invalid algorithm", trustMF.getAlgorithm(), validValue); + assertEquals("Invalid provider", trustMF.getProvider(), getDefaultProvider()); } } @@ -340,17 +287,11 @@ public class TrustManagerFactory1Test extends TestCase { * method * Assertion: throws IllegalArgumentException when provider is null */ - public void test_getInstanceLjava_lang_StringLjava_security_Provider01() throws NoSuchAlgorithmException { - if (!DEFSupported) { - fail(NotSupportedMsg); - return; - } - Provider provider = null; - for (int i = 0; i < validValues.length; i++) { + public void test_getInstanceLjava_lang_StringLjava_security_Provider01() throws Exception { + for (String validValue : getValidValues()) { try { - TrustManagerFactory.getInstance(validValues[i], provider); - fail("IllegalArgumentException must be thrown when provider is null"); - } catch (IllegalArgumentException e) { + TrustManagerFactory.getInstance(validValue, (Provider) null); + } catch (IllegalArgumentException expected) { } } } @@ -363,20 +304,16 @@ public class TrustManagerFactory1Test extends TestCase { * throws NoSuchAlgorithmException when algorithm is not correct; */ public void test_getInstanceLjava_lang_StringLjava_security_Provider02() { - if (!DEFSupported) { - fail(NotSupportedMsg); - return; - } try { - TrustManagerFactory.getInstance(null, defaultProvider); - fail("NoSuchAlgorithmException or NullPointerException should be thrown (algorithm is null"); - } catch (NoSuchAlgorithmException e) { - } catch (NullPointerException e) { + TrustManagerFactory.getInstance(null, getDefaultProvider()); + fail(""); + } catch (NoSuchAlgorithmException expected) { + } catch (NullPointerException expected) { } for (int i = 0; i < invalidValues.length; i++) { try { TrustManagerFactory.getInstance(invalidValues[i], - defaultProvider); + getDefaultProvider()); fail("NoSuchAlgorithmException must be thrown (algorithm: " .concat(invalidValues[i]).concat(")")); } catch (NoSuchAlgorithmException e) { @@ -389,21 +326,14 @@ public class TrustManagerFactory1Test extends TestCase { * method * Assertion: returns instance of TrustManagerFactory */ - public void test_getInstanceLjava_lang_StringLjava_security_Provider03() throws NoSuchAlgorithmException { - if (!DEFSupported) { - fail(NotSupportedMsg); - return; - } - TrustManagerFactory trustMF; - for (int i = 0; i < validValues.length; i++) { - trustMF = TrustManagerFactory.getInstance(validValues[i], - defaultProvider); + public void test_getInstanceLjava_lang_StringLjava_security_Provider03() throws Exception { + for (String validValue : getValidValues()) { + TrustManagerFactory trustMF = TrustManagerFactory.getInstance(validValue, + getDefaultProvider()); assertTrue("Not TrustManagerFactory object", - trustMF instanceof TrustManagerFactory); - assertEquals("Invalid algorithm", trustMF.getAlgorithm(), - validValues[i]); - assertEquals("Invalid provider", trustMF.getProvider(), - defaultProvider); + trustMF instanceof TrustManagerFactory); + assertEquals("Invalid algorithm", trustMF.getAlgorithm(), validValue); + assertEquals("Invalid provider", trustMF.getProvider(), getDefaultProvider()); } } @@ -414,19 +344,18 @@ public class TrustManagerFactory1Test extends TestCase { */ public void test_getProvider() throws NoSuchAlgorithmException, NoSuchProviderException { - if (!DEFSupported) fail(NotSupportedMsg); assertEquals("Incorrect provider", - defaultProvider, + getDefaultProvider(), TrustManagerFactory - .getInstance(defaultAlgorithm).getProvider()); + .getInstance(getDefaultAlgorithm()).getProvider()); assertEquals("Incorrect provider", - defaultProvider, + getDefaultProvider(), TrustManagerFactory - .getInstance(defaultAlgorithm, defaultProviderName) + .getInstance(getDefaultAlgorithm(), getDefaultProviderName()) .getProvider()); assertEquals("Incorrect provider", - defaultProvider, - TrustManagerFactory.getInstance(defaultAlgorithm, defaultProvider) + getDefaultProvider(), + TrustManagerFactory.getInstance(getDefaultAlgorithm(), getDefaultProvider()) .getProvider()); } @@ -439,7 +368,7 @@ public class TrustManagerFactory1Test extends TestCase { */ public void test_getTrustManagers() { try { - TrustManagerFactory trustMF = TrustManagerFactory.getInstance(defaultAlgorithm); + TrustManagerFactory trustMF = TrustManagerFactory.getInstance(getDefaultAlgorithm()); KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType()); ks.load(null, null); trustMF.init(ks); @@ -456,12 +385,7 @@ public class TrustManagerFactory1Test extends TestCase { * Test for <code>init(KeyStore keyStore)</code> * Assertion: call method with null parameter */ - public void test_initLjava_security_KeyStore_01() { - if (!DEFSupported) { - fail(NotSupportedMsg); - return; - } - + public void test_initLjava_security_KeyStore_01() throws Exception { KeyStore ksNull = null; TrustManagerFactory[] trustMF = createTMFac(); assertNotNull("TrustManagerFactory objects were not created", trustMF); @@ -477,23 +401,13 @@ public class TrustManagerFactory1Test extends TestCase { * Test for <code>init(KeyStore keyStore)</code> * Assertion: call method with not null parameter */ - public void test_initLjava_security_KeyStore_02() throws KeyStoreException { - if (!DEFSupported) { - fail(NotSupportedMsg); - return; - } - - KeyStore ks; - ks = KeyStore.getInstance(KeyStore.getDefaultType()); + public void test_initLjava_security_KeyStore_02() throws Exception { + KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType()); TrustManagerFactory[] trustMF = createTMFac(); assertNotNull("TrustManagerFactory objects were not created", trustMF); // not null parameter - try { - trustMF[0].init(ks); - } catch (Exception ex) { - fail(ex + " unexpected exception was thrown for not null parameter"); - } + trustMF[0].init(ks); } /** @@ -501,13 +415,9 @@ public class TrustManagerFactory1Test extends TestCase { * Assertion: * throws InvalidAlgorithmParameterException when params is null */ - @KnownFailure("ManagerFactoryParameters object is not supported " + - "and InvalidAlgorithmParameterException was thrown.") - public void test_initLjavax_net_ssl_ManagerFactoryParameters() { - if (!DEFSupported) { - fail(NotSupportedMsg); - return; - } + @KnownFailure("ManagerFactoryParameters object is not supported " + + "and InvalidAlgorithmParameterException was thrown.") + public void test_initLjavax_net_ssl_ManagerFactoryParameters() throws Exception { ManagerFactoryParameters par = null; TrustManagerFactory[] trustMF = createTMFac(); assertNotNull("TrustManagerFactory objects were not created", trustMF); @@ -519,14 +429,13 @@ public class TrustManagerFactory1Test extends TestCase { } } - // String keyAlg = "DSA"; - String validCaNameRfc2253 = "CN=Test CA," + - "OU=Testing Division," + - "O=Test It All," + - "L=Test Town," + - "ST=Testifornia," + - "C=Testland"; + String validCaNameRfc2253 = ("CN=Test CA," + + "OU=Testing Division," + + "O=Test It All," + + "L=Test Town," + + "ST=Testifornia," + + "C=Testland"); try { KeyStore kStore = KeyStore.getInstance(KeyStore.getDefaultType()); @@ -538,7 +447,7 @@ public class TrustManagerFactory1Test extends TestCase { X509CertSelector xcs = new X509CertSelector(); PKIXBuilderParameters pkixBP = new PKIXBuilderParameters(trustAnchors, xcs); CertPathTrustManagerParameters cptmp = new CertPathTrustManagerParameters(pkixBP); - TrustManagerFactory tmf = TrustManagerFactory.getInstance(defaultAlgorithm); + TrustManagerFactory tmf = TrustManagerFactory.getInstance(getDefaultAlgorithm()); try { tmf.init(cptmp); } catch (Exception ex) { diff --git a/support/src/test/java/libcore/java/security/TestKeyStore.java b/support/src/test/java/libcore/java/security/TestKeyStore.java index 6bb44a4..17b8be5 100644 --- a/support/src/test/java/libcore/java/security/TestKeyStore.java +++ b/support/src/test/java/libcore/java/security/TestKeyStore.java @@ -442,7 +442,7 @@ public final class TestKeyStore extends Assert { } } - public static X509Certificate createCA(PublicKey publicKey, + public static X509Certificate createCa(PublicKey publicKey, PrivateKey privateKey, String subject) { try { |