FileDocCategorySizeDatePackage
KeyManagerFactory2Test.javaAPI DocAndroid 1.5 API11127Wed May 06 22:41:06 BST 2009tests.api.javax.net.ssl

KeyManagerFactory2Test

public class KeyManagerFactory2Test extends TestCase
Tests for KeyManagerFactory class constructors and methods

Fields Summary
private static final String
srvKeyManagerFactory
private static final String
defaultAlg
private static final String
KeyManagerFactoryProviderClass
private static final String[]
invalidValues
private static final String[]
validValues
Provider
mProv
Constructors Summary
Methods Summary
private voidcheckResult(javax.net.ssl.KeyManagerFactory keyMF)

        KeyStore kStore = null;
        ManagerFactoryParameters mfp = null;
        
        char[] pass = { 'a", 'b", 'c" };

        try {
            keyMF.init(kStore, null);
            fail("KeyStoreException must be thrown");
        } catch (KeyStoreException e) {
        }
        try {
            keyMF.init(kStore, pass);
            fail("UnrecoverableKeyException must be thrown");
        } catch (UnrecoverableKeyException e) {
        }
        try {
            keyMF.init(mfp);
            fail("InvalidAlgorithmParameterException must be thrown");
        } catch (InvalidAlgorithmParameterException e) {
        }
        assertNull("getKeyManagers() should return null object", keyMF
                .getKeyManagers());
        
        try {
            kStore = KeyStore.getInstance(KeyStore.getDefaultType());
            kStore.load(null, null);            
        } catch (KeyStoreException e) {
            fail("default keystore is not supported");
            return;
        }
        keyMF.init(kStore, pass);

        mfp = new MyKeyManagerFactorySpi.Parameters(kStore, null);
        try {
            keyMF.init(mfp);
            fail("InvalidAlgorithmParameterException must be thrown");
        } catch (InvalidAlgorithmParameterException e) {
        }
        mfp = new MyKeyManagerFactorySpi.Parameters(kStore, pass);
        keyMF.init(mfp);
    
protected voidsetUp()

        super.setUp();
        mProv = (new SpiEngUtils()).new MyProvider("MyKMFProvider",
                "Provider for testing", srvKeyManagerFactory.concat(".")
                        .concat(defaultAlg), KeyManagerFactoryProviderClass);
        Security.insertProviderAt(mProv, 2);
    
protected voidtearDown()

        super.tearDown();
        Security.removeProvider(mProv.getName());
    
public voidtest_getInstanceLjava_lang_String()
Test for getInstance(String algorithm) method Assertions: throws NullPointerException when algorithm is null; throws NoSuchAlgorithmException when algorithm is not correct; returns KeyManagerFactory object

        try {
            KeyManagerFactory.getInstance(null);
            fail("NoSuchAlgorithmException or NullPointerException should be thrown (algorithm is null");
        } catch (NoSuchAlgorithmException e) {
        } catch (NullPointerException e) {
        }
        for (int i = 0; i < invalidValues.length; i++) {
            try {
                KeyManagerFactory.getInstance(invalidValues[i]);
                fail("NoSuchAlgorithmException must be thrown (algorithm: "
                        .concat(invalidValues[i]).concat(")"));
            } catch (NoSuchAlgorithmException e) {
            }
        }
        KeyManagerFactory keyMF;
        for (int i = 0; i < validValues.length; i++) {
            keyMF = KeyManagerFactory.getInstance(validValues[i]);
            assertEquals("Incorrect algorithm", keyMF.getAlgorithm(),
                    validValues[i]);
            assertEquals("Incorrect provider", keyMF.getProvider(), mProv);
            checkResult(keyMF);
        }
    
public voidtest_getInstanceLjava_lang_StringLjava_lang_String()
Test for getInstance(String algorithm, String provider) method Assertions: throws NullPointerException when algorithm is null; throws NoSuchAlgorithmException when algorithm is not correct; throws IllegalArgumentException when provider is null or empty; throws NoSuchProviderException when provider is available; returns KeyManagerFactory object

        try {
            KeyManagerFactory.getInstance(null, mProv.getName());
            fail("NoSuchAlgorithmException or NullPointerException should be thrown (algorithm is null");
        } catch (NoSuchAlgorithmException e) {
        } catch (NullPointerException e) {
        }
        for (int i = 0; i < invalidValues.length; i++) {
            try {
                KeyManagerFactory
                        .getInstance(invalidValues[i], mProv.getName());
                fail("NoSuchAlgorithmException must be thrown (algorithm: "
                        .concat(invalidValues[i]).concat(")"));
            } catch (NoSuchAlgorithmException e) {
            }
        }
        String prov = null;
        for (int i = 0; i < validValues.length; i++) {
            try {
                KeyManagerFactory.getInstance(validValues[i], prov);
                fail("IllegalArgumentException must be thrown when provider is null (algorithm: "
                        .concat(invalidValues[i]).concat(")"));
            } catch (IllegalArgumentException e) {
            }
            try {
                KeyManagerFactory.getInstance(validValues[i], "");
                fail("IllegalArgumentException must be thrown when provider is empty (algorithm: "
                        .concat(invalidValues[i]).concat(")"));
            } catch (IllegalArgumentException e) {
            }
        }
        for (int i = 0; i < validValues.length; i++) {
            for (int j = 1; j < invalidValues.length; j++) {
                try {
                    KeyManagerFactory.getInstance(validValues[i],
                            invalidValues[j]);
                    fail("NoSuchProviderException must be thrown (algorithm: "
                            .concat(invalidValues[i]).concat(" provider: ")
                            .concat(invalidValues[j]).concat(")"));
                } catch (NoSuchProviderException e) {
                }
            }
        }
        KeyManagerFactory keyMF;
        for (int i = 0; i < validValues.length; i++) {
            keyMF = KeyManagerFactory.getInstance(validValues[i], mProv
                    .getName());
            assertEquals("Incorrect algorithm", keyMF.getAlgorithm(),
                    validValues[i]);
            assertEquals("Incorrect provider", keyMF.getProvider().getName(),
                    mProv.getName());
            checkResult(keyMF);
        }
    
public voidtest_getInstanceLjava_lang_StringLjava_security_Provider()
Test for getInstance(String algorithm, Provider provider) method Assertions: throws NullPointerException when algorithm is null; throws NoSuchAlgorithmException when algorithm is not correct; throws IllegalArgumentException when provider is null; returns KeyManagerFactory object

        try {
            KeyManagerFactory.getInstance(null, mProv);
            fail("NoSuchAlgorithmException or NullPointerException should be thrown (algorithm is null");
        } catch (NoSuchAlgorithmException e) {
        } catch (NullPointerException e) {
        }
        for (int i = 0; i < invalidValues.length; i++) {
            try {
                KeyManagerFactory.getInstance(invalidValues[i], mProv);
                fail("NoSuchAlgorithmException must be thrown (algorithm: "
                        .concat(invalidValues[i]).concat(")"));
            } catch (NoSuchAlgorithmException e) {
            }
        }
        Provider prov = null;
        for (int i = 0; i < validValues.length; i++) {
            try {
                KeyManagerFactory.getInstance(validValues[i], prov);
                fail("IllegalArgumentException must be thrown when provider is null (algorithm: "
                        .concat(invalidValues[i]).concat(")"));
            } catch (IllegalArgumentException e) {
            }
        }
        KeyManagerFactory keyMF;
        for (int i = 0; i < validValues.length; i++) {
            keyMF = KeyManagerFactory.getInstance(validValues[i], mProv);
            assertEquals("Incorrect algorithm", keyMF.getAlgorithm(),
                    validValues[i]);
            assertEquals("Incorrect provider", keyMF.getProvider(), mProv);
            checkResult(keyMF);
       }