FileDocCategorySizeDatePackage
CertPathBuilderTestPKIX.javaAPI DocAndroid 1.5 API5933Wed May 06 22:41:06 BST 2009tests.targets.security.cert

CertPathBuilderTestPKIX

public class CertPathBuilderTestPKIX extends CertPathBuilderTest

Fields Summary
private String
selfSignedCert
Constructors Summary
public CertPathBuilderTestPKIX()

        super("PKIX");
    
Methods Summary
java.security.cert.CertPathParametersgetCertPathParameters()

        KeyStore keyStore = null;
        try {
            keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
        } catch (KeyStoreException e) {
            fail(e.getMessage());
        }

        try {
            keyStore.load(null, null);
        } catch (NoSuchAlgorithmException e) {
            fail(e.getMessage());
        } catch (CertificateException e) {
            fail(e.getMessage());
        } catch (IOException e) {
            fail(e.getMessage());
        }

        CertificateFactory certificateFactory = null;
        try {
            certificateFactory = CertificateFactory.getInstance("X509");
        } catch (CertificateException e) {
            fail(e.getMessage());
        }

        X509Certificate selfSignedcertificate = null;
        try {
            selfSignedcertificate = (X509Certificate) certificateFactory
                    .generateCertificate(new ByteArrayInputStream(
                            selfSignedCert.getBytes()));
        } catch (CertificateException e) {
            fail(e.getMessage());
        }

        try {
            keyStore.setCertificateEntry("selfSignedCert",
                    selfSignedcertificate);
        } catch (KeyStoreException e) {
            fail(e.getMessage());
        }

        X509CertSelector targetConstraints = new X509CertSelector();
        targetConstraints.setCertificate(selfSignedcertificate);

        List<Certificate> certList = new ArrayList<Certificate>();
        certList.add(selfSignedcertificate);
        CertStoreParameters storeParams = new CollectionCertStoreParameters(
                certList);


        CertStore certStore = null;
        try {
            certStore = CertStore.getInstance("Collection", storeParams);
        } catch (InvalidAlgorithmParameterException e) {
            fail(e.getMessage());
        } catch (NoSuchAlgorithmException e) {
            fail(e.getMessage());
        }


        try {
            PKIXBuilderParameters parameters = new PKIXBuilderParameters(
                    keyStore, targetConstraints);
            parameters.addCertStore(certStore);
            parameters.setRevocationEnabled(false);
            return parameters;
        } catch (KeyStoreException e) {
            fail(e.getMessage());
        } catch (InvalidAlgorithmParameterException e) {
            fail(e.getMessage());
        }
        return null;
    
voidvalidateCertPath(java.security.cert.CertPath path)

        List<? extends Certificate> certificates = path.getCertificates();
        Certificate certificate = certificates.get(0);

        assertEquals("unexpected certificate type", "X.509", certificate
                .getType());

        X509Certificate x509Certificate = (X509Certificate) certificate;
        X500Principal subjectX500Principal = x509Certificate
                .getSubjectX500Principal();

        X500Principal expectedPrincipal = new X500Principal(
                "CN=Android CTS, OU=Android, O=Android, L=Android, ST=Android, C=AN");

        assertEquals("unexpected principal", expectedPrincipal,
                subjectX500Principal);