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 23package tests.security.cert; 24 25import junit.framework.TestCase; 26 27import tests.security.cert.myCertPathBuilder.MyProvider; 28 29import java.security.InvalidAlgorithmParameterException; 30import java.security.NoSuchAlgorithmException; 31import java.security.NoSuchProviderException; 32import java.security.Provider; 33import java.security.Security; 34import java.security.cert.CertPathBuilder; 35import java.security.cert.CertPathBuilderException; 36import java.security.cert.CertPathBuilderResult; 37 38import org.apache.harmony.security.tests.support.SpiEngUtils; 39/** 40 * Tests for CertPathBuilder class constructors and methods 41 * 42 */ 43public class CertPathBuilder2Test extends TestCase { 44 private static final String defaultAlg = "CertPB"; 45 private static final String CertPathBuilderProviderClass = "org.apache.harmony.security.tests.support.cert.MyCertPathBuilderSpi"; 46 47 private static final String[] invalidValues = SpiEngUtils.invalidValues; 48 49 private static final String[] validValues; 50 51 static { 52 validValues = new String[4]; 53 validValues[0] = defaultAlg; 54 validValues[1] = defaultAlg.toLowerCase(); 55 validValues[2] = "CeRtPb"; 56 validValues[3] = "cERTpb"; 57 } 58 59 Provider mProv; 60 61 protected void setUp() throws Exception { 62 super.setUp(); 63 mProv = (new SpiEngUtils()).new MyProvider("MyCertPathBuilderProvider", 64 "Provider for testing", CertPathBuilder1Test.srvCertPathBuilder 65 + "." + defaultAlg, 66 67 CertPathBuilderProviderClass); 68 Security.insertProviderAt(mProv, 1); 69 } 70 71 /* 72 * @see TestCase#tearDown() 73 */ 74 protected void tearDown() throws Exception { 75 super.tearDown(); 76 Security.removeProvider(mProv.getName()); 77 } 78 79 private void checkResult(CertPathBuilder certBuild) 80 throws InvalidAlgorithmParameterException, 81 CertPathBuilderException { 82 String dt = CertPathBuilder.getDefaultType(); 83 String propName = CertPathBuilder1Test.DEFAULT_TYPE_PROPERTY; 84 String dtN; 85 for (int i = 0; i <invalidValues.length; i++) { 86 Security.setProperty(propName, invalidValues[i]); 87 dtN = CertPathBuilder.getDefaultType(); 88 if (!dtN.equals(invalidValues[i]) && !dtN.equals(dt)) { 89 fail("Incorrect default type: ".concat(dtN)); 90 } 91 } 92 Security.setProperty(propName, dt); 93 assertEquals("Incorrect default type", CertPathBuilder.getDefaultType(), 94 dt); 95 try { 96 certBuild.build(null); 97 fail("CertPathBuilderException must be thrown"); 98 } catch (CertPathBuilderException e) { 99 } 100 CertPathBuilderResult cpbResult = certBuild.build(null); 101 assertNull("Not null CertPathBuilderResult", cpbResult); 102 } 103 104 /** 105 * Test for <code>getInstance(String algorithm)</code> method 106 * Assertions: 107 * throws 108 * throws NullPointerException when algorithm is null 109 * throws NoSuchAlgorithmException when algorithm is not correct 110 * returns CertPathBuilder object 111 */ 112 public void testGetInstance01() throws NoSuchAlgorithmException, 113 InvalidAlgorithmParameterException, CertPathBuilderException { 114 try { 115 CertPathBuilder.getInstance(null); 116 fail("NullPointerException or NoSuchAlgorithmException must be thrown when algorithm is null"); 117 } catch (NullPointerException e) { 118 } catch (NoSuchAlgorithmException e) { 119 } 120 for (int i = 0; i < invalidValues.length; i++) { 121 try { 122 CertPathBuilder.getInstance(invalidValues[i]); 123 fail("NoSuchAlgorithmException must be thrown (type: ".concat( 124 invalidValues[i]).concat(")")); 125 } catch (NoSuchAlgorithmException e) { 126 } 127 } 128 CertPathBuilder cerPB; 129 for (int i = 0; i < validValues.length; i++) { 130 cerPB = CertPathBuilder.getInstance(validValues[i]); 131 assertEquals("Incorrect type", cerPB.getAlgorithm(), validValues[i]); 132 assertEquals("Incorrect provider", cerPB.getProvider(), mProv); 133 checkResult(cerPB); 134 } 135 } 136 137 /** 138 * Test for <code>getInstance(String algorithm, String provider)</code> method 139 * Assertions: 140 * throws NullPointerException when algorithm is null 141 * throws NoSuchAlgorithmException when algorithm is not correct 142 * throws IllegalArgumentException when provider is null or empty; 143 * throws NoSuchProviderException when provider is available; 144 * returns CertPathBuilder object 145 */ 146 public void testGetInstance02() throws NoSuchAlgorithmException, 147 NoSuchProviderException, IllegalArgumentException, 148 InvalidAlgorithmParameterException, CertPathBuilderException { 149 try { 150 CertPathBuilder.getInstance(null, mProv.getName()); 151 fail("NullPointerException or NoSuchAlgorithmException must be thrown when algorithm is null"); 152 } catch (NullPointerException e) { 153 } catch (NoSuchAlgorithmException e) { 154 } 155 for (int i = 0; i < invalidValues.length; i++) { 156 try { 157 CertPathBuilder.getInstance(invalidValues[i], mProv 158 .getName()); 159 fail("NoSuchAlgorithmException must be thrown (type: ".concat( 160 invalidValues[i]).concat(")")); 161 } catch (NoSuchAlgorithmException e) { 162 } 163 } 164 String prov = null; 165 for (int i = 0; i < validValues.length; i++) { 166 try { 167 CertPathBuilder.getInstance(validValues[i], prov); 168 fail("IllegalArgumentException must be thrown when provider is null (type: " 169 .concat(validValues[i]).concat(")")); 170 } catch (IllegalArgumentException e) { 171 } 172 try { 173 CertPathBuilder.getInstance(validValues[i], ""); 174 fail("IllegalArgumentException must be thrown when provider is empty (type: " 175 .concat(validValues[i]).concat(")")); 176 } catch (IllegalArgumentException e) { 177 } 178 } 179 for (int i = 0; i < validValues.length; i++) { 180 for (int j = 1; j < invalidValues.length; j++) { 181 try { 182 CertPathBuilder.getInstance(validValues[i], 183 invalidValues[j]); 184 fail("NoSuchProviderException must be thrown (type: " 185 .concat(validValues[i]).concat(" provider: ") 186 .concat(invalidValues[j]).concat(")")); 187 } catch (NoSuchProviderException e) { 188 } 189 } 190 } 191 CertPathBuilder cerPB; 192 for (int i = 0; i < validValues.length; i++) { 193 cerPB = CertPathBuilder.getInstance(validValues[i], mProv 194 .getName()); 195 assertEquals("Incorrect type", cerPB.getAlgorithm(), validValues[i]); 196 assertEquals("Incorrect provider", cerPB.getProvider().getName(), 197 mProv.getName()); 198 checkResult(cerPB); 199 } 200 } 201 202 /** 203 * Test for <code>getInstance(String algorithm, Provider provider)</code> 204 * method 205 * Assertions: 206 * throws NullPointerException when algorithm is null 207 * throws NoSuchAlgorithmException when algorithm is not correct 208 * returns CertPathBuilder object 209 */ 210 public void testGetInstance03() throws NoSuchAlgorithmException, 211 IllegalArgumentException, 212 InvalidAlgorithmParameterException, CertPathBuilderException { 213 try { 214 CertPathBuilder.getInstance(null, mProv); 215 fail("NullPointerException or NoSuchAlgorithmException must be thrown when algorithm is null"); 216 } catch (NullPointerException e) { 217 } catch (NoSuchAlgorithmException e) { 218 } 219 for (int i = 0; i < invalidValues.length; i++) { 220 try { 221 CertPathBuilder.getInstance(invalidValues[i], mProv); 222 fail("NoSuchAlgorithmException must be thrown (type: ".concat( 223 invalidValues[i]).concat(")")); 224 } catch (NoSuchAlgorithmException e) { 225 } 226 } 227 Provider prov = null; 228 for (int i = 0; i < validValues.length; i++) { 229 try { 230 CertPathBuilder.getInstance(validValues[i], prov); 231 fail("IllegalArgumentException must be thrown when provider is null (type: " 232 .concat(validValues[i]).concat(")")); 233 } catch (IllegalArgumentException e) { 234 } 235 } 236 CertPathBuilder cerPB; 237 for (int i = 0; i < validValues.length; i++) { 238 cerPB = CertPathBuilder.getInstance(validValues[i], mProv); 239 assertEquals("Incorrect type", cerPB.getAlgorithm(), validValues[i]); 240 assertEquals("Incorrect provider", cerPB.getProvider(), mProv); 241 checkResult(cerPB); 242 } 243 } 244} 245