FileDocCategorySizeDatePackage
CertPathValidator2Test.javaAPI DocAndroid 1.5 API11876Wed May 06 22:41:06 BST 2009tests.security.cert

CertPathValidator2Test

public class CertPathValidator2Test extends TestCase
Tests for CertPathValidator class constructors and methods

Fields Summary
private static final String
defaultAlg
public static final String
CertPathValidatorProviderClass
private static final String[]
invalidValues
private static final String[]
validValues
Provider
mProv
Constructors Summary
Methods Summary
private voidcheckResult(java.security.cert.CertPathValidator certV)

        String dt = CertPathValidator.getDefaultType();
        String propName = "certpathvalidator.type";
        for (int i = 0; i < invalidValues.length; i++) {
            Security.setProperty(propName, invalidValues[i]);
            assertEquals("Incorrect default type", CertPathValidator.getDefaultType(),
                    invalidValues[i]);
        }
        Security.setProperty(propName, dt);
        assertEquals("Incorrect default type", CertPathValidator.getDefaultType(), dt);
        certV.validate(null, null);
        try {
            certV.validate(null, null);
        } catch (CertPathValidatorException e) {
        }
        try {
            certV.validate(null, null);
        } catch (InvalidAlgorithmParameterException e) {
        }
    
protected voidsetUp()

        super.setUp();
        mProv = (new SpiEngUtils()).new MyProvider("MyCertPathValidatorProvider",
                "Provider for testing", CertPathValidator1Test.srvCertPathValidator.concat(".")
                        .concat(defaultAlg), CertPathValidatorProviderClass);
        Security.insertProviderAt(mProv, 1);
    
protected voidtearDown()

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

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

        try {
            CertPathValidator.getInstance(null, mProv.getName());
            fail("NullPointerException or NoSuchAlgorithmException must be thrown when algorithm is null");
        } catch (NullPointerException e) {
        } catch (NoSuchAlgorithmException e) {
        }
        for (int i = 0; i < invalidValues.length; i++) {
            try {
                CertPathValidator.getInstance(invalidValues[i], mProv.getName());
                fail("NoSuchAlgorithmException must be thrown (type: ".concat(invalidValues[i])
                        .concat(")"));
            } catch (NoSuchAlgorithmException e) {
            }
        }
        String prov = null;
        for (int i = 0; i < validValues.length; i++) {
            try {
                CertPathValidator.getInstance(validValues[i], prov);
                fail("IllegalArgumentException must be thrown when provider is null (type: "
                        .concat(validValues[i]).concat(")"));
            } catch (IllegalArgumentException e) {
            }
            try {
                CertPathValidator.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 {
                    CertPathValidator.getInstance(validValues[i], invalidValues[j]);
                    fail("NoSuchProviderException must be thrown (type: ".concat(validValues[i])
                            .concat(" provider: ").concat(invalidValues[j]).concat(")"));
                } catch (NoSuchProviderException e) {
                }
            }
        }
        CertPathValidator cerPV;
        for (int i = 0; i < validValues.length; i++) {
            cerPV = CertPathValidator.getInstance(validValues[i], mProv.getName());
            assertEquals("Incorrect type", cerPV.getAlgorithm(), validValues[i]);
            assertEquals("Incorrect provider", cerPV.getProvider().getName(), mProv.getName());
            checkResult(cerPV);
        }
    
public voidtestGetInstance03()
Test for getInstance(String algorithm, Provider provider) method Assertions: throws NullPointerException when algorithm is null throws NoSuchAlgorithmException when algorithm is not available throws IllegalArgumentException when provider is null; returns CertPathValidator object

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

        MyCertPath mCP = new MyCertPath(new byte[0]);
        CertPathParameters params = new PKIXParameters(TestUtils.getTrustAnchorSet());
        CertPathValidator certPV = CertPathValidator.getInstance(defaultAlg);
        try {
            certPV.validate(mCP, params);
        } catch (InvalidAlgorithmParameterException e) {
            fail("unexpected exception: " + e);
        } catch (CertPathValidatorException e) {
            fail("unexpected exception: " + e);
        }
        try {
            certPV.validate(null, params);
            fail("NullPointerException must be thrown");
        } catch (InvalidAlgorithmParameterException e) {
            fail("unexpected exception: " + e);
        } catch (CertPathValidatorException e) {
            // ok
        }
        try {
            certPV.validate(mCP, null);
            fail("InvalidAlgorithmParameterException must be thrown");
        } catch (InvalidAlgorithmParameterException e) {
            // ok
        } catch (CertPathValidatorException e) {
            fail("unexpected exception");
        }