FileDocCategorySizeDatePackage
KeyStore4Test.javaAPI DocAndroid 1.5 API23710Wed May 06 22:41:06 BST 2009org.apache.harmony.security.tests.java.security

KeyStore4Test

public class KeyStore4Test extends TestCase

Fields Summary
Provider
provider
KeyStore
keyStore
KeyStore
uninitialized
KeyStore
failing
public static final String
KEY_STORE_TYPE
Constructors Summary
Methods Summary
protected voidsetUp()

    
        
        super.setUp();
        
        Security.addProvider(new MyProvider());
        
        try {
            keyStore = KeyStore.getInstance(KEY_STORE_TYPE);
            keyStore.load(null, "PASSWORD".toCharArray());
        } catch (KeyStoreException e) {
            fail("test class not available");
        }
        
        try {
            uninitialized = KeyStore.getInstance(KEY_STORE_TYPE);
        } catch (KeyStoreException e) {
            fail("test keystore not available");
        }
        
    
protected voidtearDown()

        super.tearDown();
        
        Security.removeProvider(provider.getName());
    
public voidtestGetCertificateAlias()

        try {
            String alias = keyStore.getCertificateAlias(TestKeyStoreSpi.CERT);
            assertNotNull("alias is null", alias);
            assertEquals("alias is not expected", "certalias", alias);
        } catch (KeyStoreException e) {
            fail("unexpected exception: " + e);
        }
        
        try {
            uninitialized.getCertificateAlias(TestKeyStoreSpi.CERT);
            fail("expected KeyStoreException");
        } catch (KeyStoreException e) {
            // ok
        }
        
        try {
            keyStore.getCertificateAlias(null);
            fail("expected NullPointerException");
        } catch (KeyStoreException e) {
            fail("unexpected exception: " + e);
        } catch (NullPointerException e) {
            // ok
        }
        
        try {
            String certificateAlias = keyStore.getCertificateAlias(new MyCertificate("dummy", null));
            assertNull("alias was not null", certificateAlias);
        } catch (KeyStoreException e) {
            fail("unexpected exception: " + e);
        }
    
public voidtestGetEntry()

        try {
            Entry entry = keyStore.getEntry("certalias", null);
            assertNotNull("entry is null", entry);
            assertTrue("entry is not cert entry", entry instanceof KeyStore.TrustedCertificateEntry);
        } catch (NoSuchAlgorithmException e) {
            fail("unexpected exception: " + e);
        } catch (UnrecoverableEntryException e) {
            fail("unexpected exception: " + e);
        } catch (KeyStoreException e) {
            fail("unexpected exception: " + e);
        }
        
        try {
            Entry entry = keyStore.getEntry("certalias", new KeyStore.ProtectionParameter() {});
            assertNotNull(entry);
        } catch (NoSuchAlgorithmException e) {
            fail("unexpected exception: " + e);
        } catch (UnrecoverableEntryException e) {
            fail("unexpected exception: " + e);
        } catch (KeyStoreException e) {
            fail("unexpected exception: " + e);
        } catch (UnsupportedOperationException e) {
            // ok
        }
        
        try {
            Entry entry = keyStore.getEntry("keyalias", new KeyStore.PasswordProtection(new char[] {} ));
            assertNotNull(entry);
            assertTrue(entry instanceof KeyStore.SecretKeyEntry);
        } catch (NoSuchAlgorithmException e) {
            fail("unexpected exception: " + e);
        } catch (UnrecoverableEntryException e) {
            fail("unexpected exception: " + e);
        } catch (KeyStoreException e) {
            fail("unexpected exception: " + e);
        }
        
        try {
            keyStore.getEntry("unknownalias", new KeyStore.PasswordProtection(new char[] {}));
            fail("expected NoSuchAlgorithmException");
        } catch (NoSuchAlgorithmException e) {
            // ok
        } catch (UnrecoverableEntryException e) {
            fail("unexpected exception: " + e);
        } catch (KeyStoreException e) {
            fail("unexpected exception: " + e);
        } catch (UnsupportedOperationException e) {
            // also ok
        }
        
        try {
            keyStore.getEntry(null, new KeyStore.ProtectionParameter() {});
            fail("expected NullPointerException");
        } catch (NoSuchAlgorithmException e) {
            fail("unexpected exception: " + e);
        } catch (UnrecoverableEntryException e) {
            fail("unexpected exception: " + e);
        } catch (KeyStoreException e) {
            fail("unexpected exception: " + e);
        } catch (NullPointerException e) {
            // ok
        }
    
public voidtestGetInstanceString()

        try {
            KeyStore ks = KeyStore.getInstance("TestKeyStore");
            assertNotNull("keystore is null", ks);
            assertEquals("KeyStore is not of expected Type", "TestKeyStore", ks.getType());
        } catch (KeyStoreException e) {
            fail("unexpected exception: " + e);
        }
        
        try {
            KeyStore.getInstance("UnknownKeyStore");
            fail("expected KeyStoreException");
        } catch (KeyStoreException e) {
            // ok
        }
        
        try {
            KeyStore.getInstance(null);
            fail("expected NullPointerException");
        } catch (NullPointerException e) {
            // ok
        } catch (KeyStoreException e) {
            fail("unexpected exception: " + e);
        }
    
public voidtestGetInstanceStringProvider()

        try {
            KeyStore ks = KeyStore.getInstance("TestKeyStore", provider);
            assertNotNull("KeyStore is null", ks);
            assertEquals("KeyStore is not of expected type", "TestKeyStore", ks.getType());
        } catch (KeyStoreException e) {
            fail("unexpected exception: " + e);
        }
        
        try {
            KeyStore.getInstance("UnknownKeyStore", provider);
            fail("expected KeyStoreException");
        } catch (KeyStoreException e) {
            // ok;
        }
        
        try {
            KeyStore.getInstance("TestKeyStore", (Provider)null);
            fail("expected IllegalArgumentException");
        } catch (KeyStoreException e) {
            fail("unexpected exception: " + e);
        } catch (IllegalArgumentException e) {
            // ok
        }
        
        try {
            KeyStore.getInstance(null, provider);
            fail("expected NullPointerException");
        } catch (KeyStoreException e) {
            fail("unexpected exception: " + e);
        } catch (NullPointerException e) {
            // ok
        }
    
public voidtestGetInstanceStringString()

        try {
            KeyStore ks = KeyStore.getInstance("TestKeyStore", provider.getName());
            assertNotNull("keystore is null", ks);
            assertEquals("KeyStore is not of expected type", "TestKeyStore", ks.getType());
        } catch (KeyStoreException e) {
            fail("unexpected exception: " + e);
        } catch (NoSuchProviderException e) {
            fail("unexpected exception: " + e);
        }
        
        try {
            KeyStore.getInstance("UnknownKeyStore", provider.getName());
            fail("expected KeyStoreException");
        } catch (KeyStoreException e) {
            // ok
        } catch (NoSuchProviderException e) {
            fail("unexpected exception: " + e);
        }
        
        try {
            KeyStore.getInstance("TestKeyStore", (String)null);
            fail("expected IllegalArgumentException");
        } catch (KeyStoreException e) {
            fail("unexpected exception: " + e);
        } catch (NoSuchProviderException e) {
            fail("unexpected exception: " + e);
        } catch (IllegalArgumentException e) {
            // ok
        }

        try {
            KeyStore.getInstance("TestKeyStore", "");
            fail("expected IllegalArgumentException");
        } catch (KeyStoreException e) {
            fail("unexpected exception: " + e);
        } catch (NoSuchProviderException e) {
            fail("unexpected exception: " + e);
        } catch (IllegalArgumentException e) {
            // ok
        }
        
        try {
            KeyStore.getInstance(null, provider.getName());
            fail("expected KeyStoreException");
        } catch (KeyStoreException e) {
            // ok
        } catch (NoSuchProviderException e) {
            fail("unexpected exception: " + e);
        } catch (NullPointerException e) {
            // also ok            
        }
        
        try {
            KeyStore.getInstance("TestKeyStore", "UnknownProvider");
            fail("expected NoSuchProviderException");
        } catch (NoSuchProviderException e) {
            // ok
        } catch (KeyStoreException e) {
            fail("unexpected exception: " + e);
        }
    
public voidtestGetKey()

        try {
            Key key = keyStore.getKey("keyalias", null);
            assertNotNull(key);
        } catch (KeyStoreException e) {
            fail("unexpected exception: " + e);
        } catch (NoSuchAlgorithmException e) {
            fail("unexpected exception: " + e);
        } catch (UnrecoverableKeyException e) {
            fail("unexpected exception: " + e);
        }
        
        try {
            keyStore.getKey("certalias", null);
            fail("expected NoSuchAlgorithmException");
        } catch (KeyStoreException e) {
            fail("unexpected exception: " + e);
        } catch (NoSuchAlgorithmException e) {
            // ok
        } catch (UnrecoverableKeyException e) {
            fail("unexpected exception: " + e);
        }
        
        try {
            uninitialized.getKey("keyalias", null);
            fail("expected KeyStoreException");
        } catch (KeyStoreException e) {
            // ok
        } catch (NoSuchAlgorithmException e) {
            fail("unexpected exception: " + e);
        } catch (UnrecoverableKeyException e) {
            fail("unexpected exception: " + e);
        }
        
        try {
            keyStore.getKey("unknownalias", null);
            fail("expected NoSuchAlgorithmException");
        } catch (KeyStoreException e) {
            fail("unexpected exception: " + e);
        } catch (NoSuchAlgorithmException e) {
            // ok
        } catch (UnrecoverableKeyException e) {
            fail("unexpected exception: " + e);
        }
        
        try {
            keyStore.getKey("unknownalias", "PASSWORD".toCharArray());
            fail("expected UnrecoverableKeyException");
        } catch (UnrecoverableKeyException e) {
            // ok
        } catch (KeyStoreException e) {
            fail("unexpected exception: " + e);
        } catch (NoSuchAlgorithmException e) {
            fail("unexpected exception: " + e);
        }
        
    
public voidtestGetProvider()

        assertNotNull(keyStore.getProvider());
        assertEquals("not equal", provider, keyStore.getProvider());
    
public voidtestGetType()

        assertEquals(KEY_STORE_TYPE, keyStore.getType());
    
public voidtestLoadInputStreamCharArray()

        InputStream is = new ByteArrayInputStream("DATA".getBytes());
        char[] password = "PASSWORD".toCharArray();
        try {
            keyStore.load(is, password);
            assertTrue(keyStore.containsAlias("keyalias"));
        } catch (NoSuchAlgorithmException e) {
            fail("unexpected exception: " + e);
        } catch (CertificateException e) {
            fail("unexpected exception: " + e);
        } catch (IOException e) {
            fail("unexpected exception: " + e);
        } catch (KeyStoreException e) {
            fail("unexpected exception: " + e);
        }
        
        try {
            keyStore.load(new ByteArrayInputStream("".getBytes()), password);
            fail("expected IOException");
        } catch (NoSuchAlgorithmException e) {
            fail("unexpected exception: " + e);
        } catch (CertificateException e) {
            fail("unexpected exception: " + e);
        } catch (IOException e) {
            // ok
        }
        
        try {
            keyStore.load(is, null);
            fail("expected NoSuchAlgorithmException");
        } catch (NoSuchAlgorithmException e) {
            // ok
        } catch (CertificateException e) {
            fail("unexpected exception: " + e);
        } catch (IOException e) {
            fail("unexpected exception: " + e);
        }
        
        try {
            keyStore.load(is, new char[] {});
            fail("expected CertificateException");
        } catch (NoSuchAlgorithmException e) {
            fail("unexpected exception: " + e);
        } catch (CertificateException e) {
            // ok
        } catch (IOException e) {
            fail("unexpected exception: " + e);
        }
    
public voidtestLoadLoadStoreParameter()

        try {
            keyStore.load(null);
            fail("expected NoSuchAlgorithmException");
        } catch (NoSuchAlgorithmException e) {
            // ok
        } catch (CertificateException e) {
            fail("unexpected exception: " + e);
        } catch (IOException e) {
            fail("unexpected exception: " + e);
        }
        
        try {
            keyStore.load(new KeyStore.LoadStoreParameter() {

                public ProtectionParameter getProtectionParameter() {
                    return new KeyStore.PasswordProtection("PASSWORD".toCharArray());
                }
                
            });
        } catch (NoSuchAlgorithmException e) {
            fail("unexpected exception: " + e);
        } catch (CertificateException e) {
            fail("unexpected exception: " + e);
        } catch (IOException e) {
            fail("unexpected exception: " + e);
        }
        
        try {
            keyStore.load(new KeyStore.LoadStoreParameter() {

                public ProtectionParameter getProtectionParameter() {
                    return null;
                }
                
            });
            fail("expected NoSuchAlgorithmException");
        } catch (NoSuchAlgorithmException e) {
            // ok
        } catch (CertificateException e) {
            fail("unexpected exception: " + e);
        } catch (IOException e) {
            fail("unexpected exception: " + e);
        }

        try {
            keyStore.load(new KeyStore.LoadStoreParameter() {

                public ProtectionParameter getProtectionParameter() {
                    return new KeyStore.ProtectionParameter() {};
                }
                
            });
            fail("expected CertificateException");
        } catch (NoSuchAlgorithmException e) {
            fail("unexpected exception: " + e);
        } catch (CertificateException e) {
            // ok
        } catch (IOException e) {
            fail("unexpected exception: " + e);
        }
    
public voidtestStoreLoadStoreParameter()

        try {
            keyStore.store(new KeyStore.LoadStoreParameter() {

                public ProtectionParameter getProtectionParameter() {
                    return new KeyStore.PasswordProtection("PASSWORD".toCharArray());
                }});
        } catch (NoSuchAlgorithmException e) {
            fail("unexpected exception: " + e);
        } catch (CertificateException e) {
            fail("unexpected exception: " + e);
        } catch (IOException e) {
            fail("unexpected exception: " + e);
        } catch (KeyStoreException e) {
            fail("unexpected exception: " + e);
        }
        
        try {
            keyStore.store(null);
            fail("expected IOException");
        } catch (KeyStoreException e) {
            fail("unexpected exception: " + e);
        } catch (NoSuchAlgorithmException e) {
            fail("unexpected exception: " + e);
        } catch (CertificateException e) {
            fail("unexpected exception: " + e);
        } catch (IOException e) {
            // ok
        }
        
        try {
            keyStore.store(new KeyStore.LoadStoreParameter() {

                public ProtectionParameter getProtectionParameter() {
                    return null;
                }});
            fail("expected UnsupportedOperationException");
        } catch (KeyStoreException e) {
            fail("unexpected exception: " + e);
        } catch (NoSuchAlgorithmException e) {
            fail("unexpected exception: " + e);
        } catch (CertificateException e) {
            fail("unexpected exception: " + e);
        } catch (IOException e) {
            fail("unexpected exception: " + e);
        } catch (UnsupportedOperationException e) {
            // ok
        }
        
        try {
            keyStore.store(new KeyStore.LoadStoreParameter() {

                public ProtectionParameter getProtectionParameter() {
                    return new KeyStore.PasswordProtection("".toCharArray());
                }});
        } catch (KeyStoreException e) {
            fail("unexpected exception: " + e);
        } catch (NoSuchAlgorithmException e) {
            fail("unexpected exception: " + e);
        } catch (CertificateException e) {
            // ok
        } catch (IOException e) {
            fail("unexpected exception: " + e);
        }
        
        try {
            keyStore.store(new KeyStore.LoadStoreParameter() {

                public ProtectionParameter getProtectionParameter() {
                    return new KeyStore.PasswordProtection(null);
                }} );
        } catch (KeyStoreException e) {
            fail("unexpected exception: " + e);
        } catch (NoSuchAlgorithmException e) {
            // ok
        } catch (CertificateException e) {
            fail("unexpected exception: " + e);
        } catch (IOException e) {
            fail("unexpected exception: " + e);
        }
        
        try {
            uninitialized.store(null);
            fail("expected KeyStoreException");
        } catch (KeyStoreException e) {
            // ok
        } catch (NoSuchAlgorithmException e) {
            fail("unexpected exception: " + e);
        } catch (CertificateException e) {
            fail("unexpected exception: " + e);
        } catch (IOException e) {
            fail("unexpected exception: " + e);
        }
    
public voidtestStoreOutputStreamCharArray()

        OutputStream os = new ByteArrayOutputStream();
        char[] password = "PASSWORD".toCharArray();
        
        try {
            keyStore.store(os, password);
        } catch (KeyStoreException e) {
            fail("unexpected exception: " + e);
        } catch (NoSuchAlgorithmException e) {
            fail("unexpected exception: " + e);
        } catch (CertificateException e) {
            fail("unexpected exception: " + e);
        } catch (IOException e) {
            fail("unexpected exception: " + e);
        }
        
        try {
            keyStore.store(os, null);
            fail("expected NoSuchAlgorithmException");
        } catch (KeyStoreException e) {
            fail("unexpected exception: " + e);
        } catch (NoSuchAlgorithmException e) {
            // ok
        } catch (CertificateException e) {
            fail("unexpected exception: " + e);
        } catch (IOException e) {
            // ok
        }
        
        try {
            keyStore.store(os, "".toCharArray());
            fail("expected CertificateException");
        } catch (KeyStoreException e) {
            fail("unexpected exception: " + e);
        } catch (NoSuchAlgorithmException e) {
            fail("unexpected exception: " + e);
        } catch (CertificateException e) {
            // ok
        } catch (IOException e) {
            fail("unexpected exception: " + e);
        }
        
        try {
            keyStore.store(null, null);
            fail("expected IOException");
        } catch (KeyStoreException e) {
            fail("unexpected exception: " + e);
        } catch (NoSuchAlgorithmException e) {
            fail("unexpected exception: " + e);
        } catch (CertificateException e) {
            fail("unexpected exception: " + e);
        } catch (IOException e) {
            // ok
        }
        
        try {
            uninitialized.store(null, null);
            fail("expected KeyStoreException");
        } catch (KeyStoreException e) {
            // ok
        } catch (NoSuchAlgorithmException e) {
            fail("unexpected exception: " + e);
        } catch (CertificateException e) {
            fail("unexpected exception: " + e);
        } catch (IOException e) {
            fail("unexpected exception: " + e);
        }