FileDocCategorySizeDatePackage
CertificateFactory2Test.javaAPI DocAndroid 1.5 API14276Wed May 06 22:41:06 BST 2009tests.security.cert

CertificateFactory2Test

public class CertificateFactory2Test extends TestCase
Tests for CertificateFactory class constructors and methods

Fields Summary
private static final String
defaultAlg
private static final String
CertificateFactoryProviderClass
private static final String[]
invalidValues
private static final String[]
validValues
Provider
mProv
Constructors Summary
Methods Summary
public voidGetInstance01(boolean mode)
Test for getInstance(String type) method Assertions: throws NullPointerException when type is null throws CertificateException when type is not available returns CertificateFactory object

        try {
            CertificateFactory.getInstance(null);
            fail("NullPointerException or CertificateException must be thrown when type is null");
        } catch (CertificateException e) {
        } catch (NullPointerException e) {
        }
        for (int i = 0; i < invalidValues.length; i++) {
            try {
                CertificateFactory.getInstance(invalidValues[i]);
                fail("CertificateException must be thrown (type: ".concat(
                        invalidValues[i]).concat(")"));
            } catch (CertificateException e) {
            }
        }
        CertificateFactory cerF;
        for (int i = 0; i < validValues.length; i++) {
            cerF = CertificateFactory.getInstance(validValues[i]);
            assertEquals("Incorrect type", cerF.getType(), validValues[i]);
            assertEquals("Incorrect provider", cerF.getProvider(), mProv);
            checkResult(cerF, mode);
        }
    
public voidGetInstance02(boolean mode)
Test for getInstance(String type, String provider) method Assertions: throws NullPointerException when type is null throws CertificateException when type is not available throws IllegalArgumentException when provider is null or empty; throws NoSuchProviderException when provider is available; returns CertificateFactory object

        try {
            CertificateFactory.getInstance(null, mProv.getName());
            fail("NullPointerException or CertificateException must be thrown when type is null");
        } catch (CertificateException e) {
        } catch (NullPointerException e) {
        }
        for (int i = 0; i < invalidValues.length; i++) {
            try {
                CertificateFactory.getInstance(invalidValues[i], mProv
                        .getName());
                fail("CertificateException must be thrown (type: ".concat(
                        invalidValues[i]).concat(")"));
            } catch (CertificateException e) {
            }
        }
        String prov = null;
        for (int i = 0; i < validValues.length; i++) {
            try {
                CertificateFactory.getInstance(validValues[i], prov);
                fail("IllegalArgumentException must be thrown when provider is null (type: "
                        .concat(validValues[i]).concat(")"));
            } catch (IllegalArgumentException e) {
            }
            try {
                CertificateFactory.getInstance(validValues[i], "");
                fail("IllegalArgumentException must be thrown when provider is empty (type: "
                        .concat(validValues[i]).concat(")"));
            } catch (IllegalArgumentException e) {
            }
        }
        for (int i = 0; i < validValues.length; i++) {
            for (int j = 1; j < invalidValues.length; j++) {
                try {
                    CertificateFactory.getInstance(validValues[i],
                            invalidValues[j]);
                    fail("NoSuchProviderException must be thrown (type: "
                            .concat(validValues[i]).concat(" provider: ")
                            .concat(invalidValues[j]).concat(")"));
                } catch (NoSuchProviderException e) {
                }
            }
        }
        CertificateFactory cerF;
        for (int i = 0; i < validValues.length; i++) {
            cerF = CertificateFactory.getInstance(validValues[i], mProv
                    .getName());
            assertEquals("Incorrect type", cerF.getType(), validValues[i]);
            assertEquals("Incorrect provider", cerF.getProvider().getName(),
                    mProv.getName());
            checkResult(cerF, mode);
        }
    
public voidGetInstance03(boolean mode)
Test for getInstance(String type, Provider provider) method Assertions: throws NullPointerException when type is null throws CertificateException when type is not available throws IllegalArgumentException when provider is null; returns CertificateFactory object

        try {
            CertificateFactory.getInstance(null, mProv);
            fail("NullPointerException or CertificateException must be thrown when type is null");
        } catch (CertificateException e) {
        } catch (NullPointerException e) {
        }
        for (int i = 0; i < invalidValues.length; i++) {
            try {
                CertificateFactory.getInstance(invalidValues[i], mProv);
                fail("CertificateException must be thrown (type: ".concat(
                        invalidValues[i]).concat(")"));
            } catch (CertificateException e) {
            }
        }
        Provider prov = null;
        for (int i = 0; i < validValues.length; i++) {
            try {
                CertificateFactory.getInstance(validValues[i], prov);
                fail("IllegalArgumentException must be thrown when provider is null (type: "
                        .concat(validValues[i]).concat(")"));
            } catch (IllegalArgumentException e) {
            }
        }
        CertificateFactory cerF;
        for (int i = 0; i < validValues.length; i++) {
            cerF = CertificateFactory.getInstance(validValues[i], mProv);
            assertEquals("Incorrect type", cerF.getType(), validValues[i]);
            assertEquals("Incorrect provider", cerF.getProvider(), mProv);            
            checkResult(cerF,  mode);
        }
    
private voidcheckResult(java.security.cert.CertificateFactory certFactory, boolean mode)

        MyCertificateFactorySpi.putMode(mode);

        ByteArrayInputStream bais = new ByteArrayInputStream(new byte[0]);
        DataInputStream dis = new DataInputStream(bais);
        try {
            certFactory.generateCertPath(bais);
            fail("CertificateException must be thrown");
        } catch (CertificateException e) {
        }
        try {
            certFactory.generateCertPath(dis);
            if (!mode) {
                fail("CertificateException must be thrown because encodings list is empty");
            }
        } catch (CertificateException e) {
            if (mode) {
                fail("Unexpected CertificateFactoryException was thrown");                
            }
        }
        try {
            certFactory.generateCertPath(bais, "aa");
            fail("CertificateException must be thrown");
        } catch (CertificateException e) {
        }
        try {
            certFactory.generateCertPath(dis, "");
            if (mode) {
                fail("IllegalArgumentException must be thrown");
            }
        } catch (IllegalArgumentException e) {
            if (!mode) {
                fail("Unexpected IllegalArgumentException was thrown");
            }
        }
        certFactory.generateCertPath(dis, "ss");

        try {
            certFactory.generateCertificate(bais);
            fail("CertificateException must be thrown");
        } catch (CertificateException e) {
        }
        try {
            certFactory.generateCertificates(null);
            fail("CertificateException must be thrown");
        } catch (CertificateException e) {
        }
        Certificate cert = certFactory.generateCertificate(dis);
        assertNull("Result must be null", cert);
        Collection<? extends Certificate> col = certFactory.generateCertificates(dis);
        assertNull("Result must be null", col);

        try {
            certFactory.generateCRL(bais);
            fail("CRLException must be thrown");
        } catch (CRLException e) {
        }
        try {
            certFactory.generateCRLs(null);
            fail("CRLException must be thrown");
        } catch (CRLException e) {
        }
        CRL crl = certFactory.generateCRL(dis);
        assertNull("Result must be null", crl);
        Collection<? extends CRL> colc = certFactory.generateCRLs(dis);
        assertNull("Result must be null", colc);

        List<Certificate> list = null;
        CertPath cp;
        try {
            cp = certFactory.generateCertPath(list);
            if (mode) {
                fail("NullPointerException must be thrown");
            } else {
                assertNull("Must be null", cp);                
            }
        } catch (NullPointerException e) {
            if (!mode) {
                fail("Unexpected NullPointerException was thrown");
            }
        }
        Iterator<String> it = certFactory.getCertPathEncodings();
        if (mode) {
            assertTrue(it.hasNext());
        } else {
            assertFalse(it.hasNext());            
        }
    
protected voidsetUp()

        super.setUp();
        mProv = (new SpiEngUtils()).new MyProvider("MyCFProvider",
                "Provider for testing", CertificateFactory1Test.srvCertificateFactory
                        .concat(".").concat(defaultAlg),
                CertificateFactoryProviderClass);
        Security.insertProviderAt(mProv, 1);
    
protected voidtearDown()

        super.tearDown();
        Security.removeProvider(mProv.getName());
    
public voidtestGetInstance01()

        GetInstance01(true);   
    
public voidtestGetInstance02()

        GetInstance02(true);   
    
public voidtestGetInstance03()

        GetInstance03(true);   
    
public voidtestGetInstance04()

        GetInstance01(false);   
    
public voidtestGetInstance05()

        GetInstance02(false);   
    
public voidtestGetInstance06()

        GetInstance03(false);