1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 /** 19 * @author Vera Y. Petrashkova 20 * @version $Revision$ 21 */ 22 23 package tests.security.cert; 24 25 26 import junit.framework.TestCase; 27 28 import org.apache.harmony.security.tests.support.SpiEngUtils; 29 import org.apache.harmony.security.tests.support.cert.MyCertPath; 30 import org.apache.harmony.security.tests.support.cert.TestUtils; 31 32 import java.io.IOException; 33 import java.security.InvalidAlgorithmParameterException; 34 import java.security.NoSuchAlgorithmException; 35 import java.security.NoSuchProviderException; 36 import java.security.Provider; 37 import java.security.cert.CertPathParameters; 38 import java.security.cert.CertPathValidator; 39 import java.security.cert.CertPathValidatorException; 40 import java.security.cert.CertificateException; 41 import java.security.cert.PKIXParameters; 42 /** 43 * Tests for <code>CertPathValidator</code> class methods. 44 * 45 */ 46 public class CertPathValidator3Test extends TestCase { 47 48 private static final String defaultType = CertPathBuilder1Test.defaultType; 49 50 private static boolean PKIXSupport = false; 51 52 private static Provider defaultProvider; 53 private static String defaultProviderName; 54 55 private static String NotSupportMsg = ""; 56 57 static { 58 defaultProvider = SpiEngUtils.isSupport(defaultType, 59 CertPathValidator1Test.srvCertPathValidator); 60 PKIXSupport = (defaultProvider != null); 61 defaultProviderName = (PKIXSupport ? defaultProvider.getName() : null); 62 NotSupportMsg = defaultType.concat(" is not supported"); 63 } 64 65 private static CertPathValidator[] createCPVs() { 66 if (!PKIXSupport) { 67 fail(NotSupportMsg); 68 return null; 69 } 70 try { 71 CertPathValidator[] certPVs = new CertPathValidator[3]; 72 certPVs[0] = CertPathValidator.getInstance(defaultType); 73 certPVs[1] = CertPathValidator.getInstance(defaultType, 74 defaultProviderName); 75 certPVs[2] = CertPathValidator.getInstance(defaultType, 76 defaultProvider); 77 return certPVs; 78 } catch (Exception e) { 79 return null; 80 } 81 } 82 /** 83 * Test for <code>validate(CertPath certpath, CertPathParameters params)</code> method 84 * Assertion: throws InvalidAlgorithmParameterException 85 * when params is instance of PKIXParameters and 86 * certpath is not X.509 type 87 * 88 */ 89 public void testValidate01() throws InvalidAlgorithmParameterException, CertPathValidatorException { 90 if (!PKIXSupport) { 91 fail(NotSupportMsg); 92 return; 93 } 94 MyCertPath mCP = new MyCertPath(new byte[0]); 95 CertPathParameters params = new PKIXParameters(TestUtils.getTrustAnchorSet()); 96 CertPathValidator [] certPV = createCPVs(); 97 assertNotNull("CertPathValidator objects were not created", certPV); 98 for (int i = 0; i < certPV.length; i++) { 99 try { 100 certPV[i].validate(mCP, null); 101 fail("InvalidAlgorithmParameterException must be thrown"); 102 } catch(InvalidAlgorithmParameterException e) { 103 } 104 try { 105 certPV[i].validate(null, params); 106 fail("NullPointerException must be thrown"); 107 } catch(NullPointerException e) { 108 } 109 } 110 } 111 } 112