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

TrustManagerFactory2Test

public class TrustManagerFactory2Test extends TestCase
Tests for TrustManagerFactory class constructors and methods

Fields Summary
private static final String
srvTrustManagerFactory
private static final String
defaultAlg
private static final String
TrustManagerFactoryProviderClass
private static final String[]
invalidValues
private static final String[]
validValues
Provider
mProv
Constructors Summary
Methods Summary
private voidcheckResult(javax.net.ssl.TrustManagerFactory tmf)

        KeyStore kStore = null;
        ManagerFactoryParameters mfp = null;
        
        try {
            tmf.init(kStore);
            fail("KeyStoreException must be thrown");
        } catch (KeyStoreException e) {
        }
        try {
            tmf.init(mfp);
            fail("InvalidAlgorithmParameterException must be thrown");
        } catch (InvalidAlgorithmParameterException e) {
        }
        assertNull("getTrustManagers() should return null object", tmf
                .getTrustManagers());     
        
        try {
            kStore = KeyStore.getInstance(KeyStore.getDefaultType());
            kStore.load(null, null);            
        } catch (KeyStoreException e) {
            fail("default keystore is not supported");
            return;
        }
        tmf.init(kStore);
        mfp = (ManagerFactoryParameters) new MyTrustManagerFactorySpi.Parameters(null);
        try {
            tmf.init(mfp);
            fail("RuntimeException must be thrown");
        } catch (RuntimeException e) {
            assertTrue("Incorrect exception", e.getCause() instanceof KeyStoreException);
        }
        mfp = (ManagerFactoryParameters) new MyTrustManagerFactorySpi.Parameters(kStore);
        tmf.init(mfp);
    
protected voidsetUp()

        super.setUp();
        mProv = (new SpiEngUtils()).new MyProvider("MyTMFProvider",
                "Provider for testing", srvTrustManagerFactory.concat(".")
                        .concat(defaultAlg), TrustManagerFactoryProviderClass);
        Security.insertProviderAt(mProv, 1);
    
protected voidtearDown()

        super.tearDown();
        Security.removeProvider(mProv.getName());
    
public voidtestLjava_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 TrustManagerFactory object

        try {
            TrustManagerFactory.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 {
                TrustManagerFactory.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 {
                TrustManagerFactory.getInstance(validValues[i], prov);
                fail("IllegalArgumentException must be thrown when provider is null (algorithm: "
                        .concat(invalidValues[i]).concat(")"));
            } catch (IllegalArgumentException e) {
            }
        }
        TrustManagerFactory tmf;
        for (int i = 0; i < validValues.length; i++) {
            tmf = TrustManagerFactory.getInstance(validValues[i], mProv);
            assertTrue("Not instanceof TrustManagerFactory object",
                    tmf instanceof TrustManagerFactory);
            assertEquals("Incorrect algorithm", tmf.getAlgorithm(),
                    validValues[i]);
            assertEquals("Incorrect provider", tmf.getProvider(), mProv);
            checkResult(tmf);
        }
    
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 TrustManagerFactory object

        try {
            TrustManagerFactory.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 {
                TrustManagerFactory.getInstance(invalidValues[i]);
                fail("NoSuchAlgorithmException must be thrown (algorithm: "
                        .concat(invalidValues[i]).concat(")"));
            } catch (NoSuchAlgorithmException e) {
            }
        }
        TrustManagerFactory tmf;
        for (int i = 0; i < validValues.length; i++) {
            tmf = TrustManagerFactory.getInstance(validValues[i]);
            assertTrue("Not instanceof TrustManagerFactory object",
                    tmf instanceof TrustManagerFactory);
            assertEquals("Incorrect algorithm", tmf.getAlgorithm(),
                    validValues[i]);
            assertEquals("Incorrect provider", tmf.getProvider(), mProv);
            checkResult(tmf);
        }
    
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 TrustManagerFactory object

        try {
            TrustManagerFactory.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 {
                TrustManagerFactory.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 {
                TrustManagerFactory.getInstance(validValues[i], prov);
                fail("IllegalArgumentException must be thrown when provider is null (algorithm: "
                        .concat(invalidValues[i]).concat(")"));
            } catch (IllegalArgumentException e) {
            }
            try {
                TrustManagerFactory.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 {
                    TrustManagerFactory.getInstance(validValues[i],
                            invalidValues[j]);
                    fail("NoSuchProviderException must be thrown (algorithm: "
                            .concat(invalidValues[i]).concat(" provider: ")
                            .concat(invalidValues[j]).concat(")"));
                } catch (NoSuchProviderException e) {
                }
            }
        }
        TrustManagerFactory tmf;
        for (int i = 0; i < validValues.length; i++) {
            tmf = TrustManagerFactory.getInstance(validValues[i], mProv
                    .getName());
            assertTrue("Not instanceof TrustManagerFactory object",
                    tmf instanceof TrustManagerFactory);
            assertEquals("Incorrect algorithm", tmf.getAlgorithm(),
                    validValues[i]);
            assertEquals("Incorrect provider", tmf.getProvider().getName(),
                    mProv.getName());
            checkResult(tmf);
        }