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*/ 21 22package org.apache.harmony.security.tests.java.security.cert; 23 24 25import java.security.InvalidAlgorithmParameterException; 26import java.security.NoSuchAlgorithmException; 27import java.security.NoSuchProviderException; 28import java.security.Provider; 29import java.security.cert.CertPathParameters; 30import java.security.cert.CertPathValidator; 31import java.security.cert.CertPathValidatorException; 32import java.security.cert.PKIXParameters; 33 34import org.apache.harmony.security.tests.java.security.cert.CertPathBuilder1Test; 35import org.apache.harmony.security.tests.java.security.cert.CertPathValidator1Test; 36import org.apache.harmony.security.tests.support.SpiEngUtils; 37import org.apache.harmony.security.tests.support.cert.MyCertPath; 38import org.apache.harmony.security.tests.support.cert.TestUtils; 39 40import junit.framework.TestCase; 41 42/** 43 * Tests for <code>CertPathValidator</code> class methods. 44 * 45 */ 46 47public class CertPathValidator3Test extends TestCase { 48 49 /** 50 * Constructor for CertPathValidatorTests. 51 * @param name 52 */ 53 public CertPathValidator3Test(String name) { 54 super(name); 55 } 56 private static final String defaultType = CertPathBuilder1Test.defaultType; 57 58 private static boolean PKIXSupport = false; 59 60 private static Provider defaultProvider; 61 private static String defaultProviderName; 62 63 private static String NotSupportMsg = ""; 64 65 static { 66 defaultProvider = SpiEngUtils.isSupport(defaultType, 67 CertPathValidator1Test.srvCertPathValidator); 68 PKIXSupport = (defaultProvider != null); 69 defaultProviderName = (PKIXSupport ? defaultProvider.getName() : null); 70 NotSupportMsg = defaultType.concat(" is not supported"); 71 } 72 73 private static CertPathValidator[] createCPVs() { 74 if (!PKIXSupport) { 75 fail(NotSupportMsg); 76 return null; 77 } 78 try { 79 CertPathValidator[] certPVs = new CertPathValidator[3]; 80 certPVs[0] = CertPathValidator.getInstance(defaultType); 81 certPVs[1] = CertPathValidator.getInstance(defaultType, 82 defaultProviderName); 83 certPVs[2] = CertPathValidator.getInstance(defaultType, 84 defaultProvider); 85 return certPVs; 86 } catch (Exception e) { 87 return null; 88 } 89 } 90 /** 91 * Test for <code>validate(CertPath certpath, CertPathParameters params)</code> method 92 * Assertion: throws InvalidAlgorithmParameterException 93 * when params is instance of PKIXParameters and 94 * certpath is not X.509 type 95 * 96 * FIXME: jrockit-j2re1.4.2_04 throws NullPointerException when certPath is null 97 */ 98 public void testValidate01() 99 throws NoSuchAlgorithmException, NoSuchProviderException, 100 CertPathValidatorException, InvalidAlgorithmParameterException { 101 if (!PKIXSupport) { 102 fail(NotSupportMsg); 103 return; 104 } 105 MyCertPath mCP = new MyCertPath(new byte[0]); 106 CertPathParameters params = new PKIXParameters(TestUtils.getTrustAnchorSet()); 107 CertPathValidator [] certPV = createCPVs(); 108 assertNotNull("CertPathValidator objects were not created", certPV); 109 for (int i = 0; i < certPV.length; i++) { 110 try { 111 certPV[i].validate(mCP, null); 112 fail("InvalidAlgorithmParameterException must be thrown"); 113 } catch(InvalidAlgorithmParameterException e) { 114 } 115 try { 116 certPV[i].validate(null, params); 117 fail("NullPointerException must be thrown"); 118 } catch(NullPointerException e) { 119 } 120 } 121 } 122 123} 124