FileDocCategorySizeDatePackage
CertificateFactory3Test.javaAPI DocAndroid 1.5 API8779Wed May 06 22:41:06 BST 2009tests.security.cert

CertificateFactory3Test

public class CertificateFactory3Test extends TestCase
Tests for CertificateFactory class methods

Fields Summary
private static String
defaultProviderName
private static Provider
defaultProvider
private static String
defaultType
public static String
fileCertPathPki
private static boolean
X509Support
private static String
NotSupportMsg
Constructors Summary
Methods Summary
private static java.security.cert.CertificateFactory[]initCertFs()


     
        defaultProvider = SpiEngUtils.isSupport(defaultType,
                CertificateFactory1Test.srvCertificateFactory);
        X509Support = defaultProvider != null;
        defaultProviderName = X509Support ? defaultProvider.getName() : null;

        NotSupportMsg = defaultType.concat(" is not supported");
    
        if (!X509Support) {
            fail(NotSupportMsg);
        }

        CertificateFactory[] certFs = new CertificateFactory[3];
        certFs[0] = CertificateFactory.getInstance(defaultType);
        certFs[1] = CertificateFactory.getInstance(defaultType,
                defaultProviderName);
        certFs[2] = CertificateFactory
                .getInstance(defaultType, defaultProvider);
        return certFs;
    
public voidtestGenerateCertPath01()
Test for generateCertPath(List certificates) method Assertion: returns CertPath with 1 Certificate

        CertificateFactory[] certFs = initCertFs();
        assertNotNull("CertificateFactory objects were not created", certFs);
        // create list of certificates with one certificate
        Certificate cert = certFs[0]
                .generateCertificate(new ByteArrayInputStream(TestUtils
                        .getEncodedX509Certificate()));
        List<Certificate> list = new Vector<Certificate>();
        list.add(cert);
        for (int i = 0; i < certFs.length; i++) {
            CertPath certPath = null;
            certPath = certFs[i].generateCertPath(list);
            assertEquals(cert.getType(), certPath.getType());
            List<? extends Certificate> list1 = certPath.getCertificates();
            assertFalse("Result list is empty", list1.isEmpty());
            Iterator<? extends Certificate> it = list1.iterator();
            assertEquals("Incorrect Certificate in CertPath", cert, it.next());
        }
    
public voidtestGenerateCertPath02()
Test for generateCertPath(InputStream inStream, String encoding) method Assertion: returns CertPath with 1 Certificate

        CertificateFactory[] certFs = initCertFs();
        assertNotNull("CertificateFactory objects were not created", certFs);
        for (int i = 0; i < certFs.length; i++) {
            CertPath certPath = null;
            InputStream fis = Support_Resources
                    .getResourceStream(fileCertPathPki);
            certPath = certFs[i].generateCertPath(fis, "PkiPath");
            fis.close();
            assertEquals(defaultType, certPath.getType());

            List<? extends Certificate> list1 = certPath.getCertificates();
            assertFalse("Result list is empty", list1.isEmpty());
        }
    
public voidtestGenerateCertPath03()
Test for generateCertPath(InputStream inStream) method Assertion: returns CertPath with 1 Certificate

        String certPathEncoding = "PkiPath";
        CertificateFactory[] certFs = initCertFs();
        assertNotNull("CertificateFactory objects were not created", certFs);
        for (int i = 0; i < certFs.length; i++) {
            Iterator<String> it = certFs[0].getCertPathEncodings();

            assertTrue("no CertPath encodings", it.hasNext());

            assertEquals("Incorrect default encoding", certPathEncoding, it
                    .next());

            CertPath certPath = null;
            InputStream fis = Support_Resources
                    .getResourceStream(fileCertPathPki);
            certPath = certFs[i].generateCertPath(fis);
            fis.close();
            assertEquals(defaultType, certPath.getType());

            List<? extends Certificate> list1 = certPath.getCertificates();
            assertFalse("Result list is empty", list1.isEmpty());
        }
    
public voidtestGenerateCertificate()
Test for generateCertificate(InputStream inStream) method Assertion: returns Certificate

        CertificateFactory[] certFs = initCertFs();
        assertNotNull("CertificateFactory objects were not created", certFs);
        Certificate[] certs = new Certificate[3];
        for (int i = 0; i < certFs.length; i++) {
            certs[i] = certFs[i].generateCertificate(new ByteArrayInputStream(
                    TestUtils.getEncodedX509Certificate()));
        }
        assertEquals(certs[0], certs[1]);
        assertEquals(certs[0], certs[2]);
    
public voidtestGenerateCertificates()
Test for generateCertificates(InputStream inStream) method Assertion: returns Collection which consists of 1 Certificate

        CertificateFactory[] certFs = initCertFs();
        assertNotNull("CertificateFactory objects were not created", certFs);
        Certificate cert = certFs[0]
                .generateCertificate(new ByteArrayInputStream(TestUtils
                        .getEncodedX509Certificate()));
        for (int i = 0; i < certFs.length; i++) {
            Collection<? extends Certificate> col = null;
            col = certFs[i].generateCertificates(new ByteArrayInputStream(
                    TestUtils.getEncodedX509Certificate()));
            Iterator<? extends Certificate> it = col.iterator();
            assertEquals("Incorrect Collection size", col.size(), 1);
            assertEquals("Incorrect Certificate in Collection", cert, it.next());
        }