CertPathValidator1Test.java revision e98fbf8686c5289bf03fe5c3de7ff82d3a77104d
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 24import java.security.InvalidAlgorithmParameterException; 25import java.security.NoSuchAlgorithmException; 26import java.security.NoSuchProviderException; 27import java.security.Provider; 28import java.security.Security; 29import java.security.cert.CertPathParameters; 30import java.security.cert.CertPathValidator; 31import java.security.cert.CertPathValidatorException; 32import java.security.cert.CertPathValidatorSpi; 33import java.security.cert.CertificateException; 34 35import org.apache.harmony.security.tests.support.SpiEngUtils; 36import org.apache.harmony.security.tests.support.cert.MyCertPath; 37import org.apache.harmony.security.tests.support.cert.MyCertPathValidatorSpi; 38 39import junit.framework.TestCase; 40 41/** 42 * Tests for <code>CertPathValidator</code> class constructors and 43 * methods. 44 * 45 */ 46 47public class CertPathValidator1Test extends TestCase { 48 49 /** 50 * Constructor for CertPathValidatorTests. 51 * @param name 52 */ 53 public CertPathValidator1Test(String name) { 54 super(name); 55 } 56 public static final String srvCertPathValidator = "CertPathValidator"; 57 58 private static final String defaultType = "PKIX"; 59 public static final String [] validValues = { 60 "PKIX", "pkix", "PkiX", "pKiX" }; 61 62 private static String [] invalidValues = SpiEngUtils.invalidValues; 63 64 private static boolean PKIXSupport = false; 65 66 private static Provider defaultProvider; 67 private static String defaultProviderName; 68 69 private static String NotSupportMsg = ""; 70 71 static { 72 defaultProvider = SpiEngUtils.isSupport(defaultType, 73 srvCertPathValidator); 74 PKIXSupport = (defaultProvider != null); 75 defaultProviderName = (PKIXSupport ? defaultProvider.getName() : null); 76 NotSupportMsg = defaultType.concat(" is not supported"); 77 } 78 79 80 81 private static CertPathValidator[] createCPVs() { 82 if (!PKIXSupport) { 83 fail(NotSupportMsg); 84 return null; 85 } 86 try { 87 CertPathValidator[] certPVs = new CertPathValidator[3]; 88 certPVs[0] = CertPathValidator.getInstance(defaultType); 89 certPVs[1] = CertPathValidator.getInstance(defaultType, 90 defaultProviderName); 91 certPVs[2] = CertPathValidator.getInstance(defaultType, 92 defaultProvider); 93 return certPVs; 94 } catch (Exception e) { 95 return null; 96 } 97 } 98 99 100 /** 101 * Test for <code>getDefaultType()</code> method 102 * Assertion: returns security property "certpathvalidator.type" or "PKIX" 103 */ 104 public void testCertPathValidator01() { 105 if (!PKIXSupport) { 106 fail(NotSupportMsg); 107 return; 108 } 109 String propName = "certpathvalidator.type"; 110 String defCPV = Security.getProperty(propName); 111 112 String dt = CertPathValidator.getDefaultType(); 113 String resType = defCPV; 114 if (resType == null) { 115 resType = defaultType; 116 } 117 assertNotNull("Default type have not be null", dt); 118 assertEquals("Incorrect default type", dt, resType); 119 120 if (defCPV == null) { 121 Security.setProperty(propName, defaultType); 122 dt = CertPathValidator.getDefaultType(); 123 resType = Security.getProperty(propName); 124 assertNotNull("Incorrect default type", resType); 125 assertNotNull("Default type have not be null", dt); 126 assertEquals("Incorrect default type", dt, resType); 127 } 128 } 129 130 /** 131 * Test for <code>getInstance(String algorithm)</code> method 132 * Assertion: 133 * throws NullPointerException when algorithm is null 134 * throws NoSuchAlgorithmException when algorithm is not available 135 */ 136 public void testCertPathValidator02() { 137 try { 138 CertPathValidator.getInstance(null); 139 fail("NullPointerException or NoSuchAlgorithmException must be thrown when algorithm is null"); 140 } catch (NullPointerException e) { 141 } catch (NoSuchAlgorithmException e) { 142 } 143 for (int i = 0; i < invalidValues.length; i++) { 144 try { 145 CertPathValidator.getInstance(invalidValues[i]); 146 fail("NoSuchAlgorithmException must be thrown"); 147 } catch (NoSuchAlgorithmException e) { 148 } 149 } 150 } 151 /** 152 * Test for <code>getInstance(String algorithm)</code> method 153 * Assertion: returns CertPathValidator object 154 */ 155 public void testCertPathValidator03() throws NoSuchAlgorithmException { 156 if (!PKIXSupport) { 157 fail(NotSupportMsg); 158 return; 159 } 160 CertPathValidator certPV; 161 for (int i = 0; i < validValues.length; i++) { 162 certPV = CertPathValidator.getInstance(validValues[i]); 163 assertEquals("Incorrect algorithm", certPV.getAlgorithm(), validValues[i]); 164 } 165 } 166 /** 167 * Test for <code>getInstance(String algorithm, String provider)</code> method 168 * Assertion: throws IllegalArgumentException when provider is null or empty 169 * 170 * FIXME: verify what exception will be thrown if provider is empty 171 */ 172 public void testCertPathValidator04() 173 throws NoSuchAlgorithmException, NoSuchProviderException { 174 if (!PKIXSupport) { 175 fail(NotSupportMsg); 176 return; 177 } 178 String provider = null; 179 for (int i = 0; i < validValues.length; i++) { 180 try { 181 CertPathValidator.getInstance(validValues[i], provider); 182 fail("IllegalArgumentException must be thrown thrown"); 183 } catch (IllegalArgumentException e) { 184 } 185 try { 186 CertPathValidator.getInstance(validValues[i], ""); 187 fail("IllegalArgumentException must be thrown thrown"); 188 } catch (IllegalArgumentException e) { 189 } 190 } 191 } 192 /** 193 * Test for <code>getInstance(String algorithm, String provider)</code> method 194 * Assertion: 195 * throws NoSuchProviderException when provider has invalid value 196 */ 197 public void testCertPathValidator05() throws NoSuchAlgorithmException { 198 if (!PKIXSupport) { 199 fail(NotSupportMsg); 200 return; 201 } 202 for (int t = 0; t < validValues.length; t++) { 203 for (int i = 1; i < invalidValues.length; i++) { 204 try { 205 CertPathValidator.getInstance(validValues[t], 206 invalidValues[i]); 207 fail("NoSuchProviderException must be thrown"); 208 } catch (NoSuchProviderException e1) { 209 } 210 } 211 } 212 } 213 214 /** 215 * Test for <code>getInstance(String algorithm, String provider)</code> method 216 * Assertion: 217 * throws NullPointerException when algorithm is null 218 * throws NoSuchAlgorithmException when algorithm is not available 219 */ 220 public void testCertPathValidator06() 221 throws NoSuchAlgorithmException, NoSuchProviderException { 222 if (!PKIXSupport) { 223 fail(NotSupportMsg); 224 return; 225 } 226 try { 227 CertPathValidator.getInstance(null, defaultProviderName); 228 fail("NullPointerException or NoSuchAlgorithmException must be thrown when algorithm is null"); 229 } catch (NullPointerException e) { 230 } catch (NoSuchAlgorithmException e) { 231 } 232 for (int i = 0; i < invalidValues.length; i++) { 233 try { 234 CertPathValidator.getInstance(invalidValues[i], defaultProviderName); 235 fail("NoSuchAlgorithmException must be thrown"); 236 } catch (NoSuchAlgorithmException e1) { 237 } 238 } 239 } 240 /** 241 * Test for <code>getInstance(String algorithm, String provider)</code> method 242 * Assertion: returns CertPathValidator object 243 */ 244 public void testCertPathValidator07() throws NoSuchAlgorithmException, 245 NoSuchProviderException { 246 if (!PKIXSupport) { 247 fail(NotSupportMsg); 248 return; 249 } 250 CertPathValidator certPV; 251 for (int i = 0; i < validValues.length; i++) { 252 certPV = CertPathValidator.getInstance(validValues[i], 253 defaultProviderName); 254 assertEquals("Incorrect algorithm", certPV.getAlgorithm(), 255 validValues[i]); 256 assertEquals("Incorrect provider name", certPV.getProvider() 257 .getName(), defaultProviderName); 258 } 259 } 260 261 /** 262 * Test for <code>getInstance(String algorithm, Provider provider)</code> method 263 * Assertion: throws IllegalArgumentException when provider is null 264 */ 265 public void testCertPathValidator08() 266 throws NoSuchAlgorithmException { 267 if (!PKIXSupport) { 268 fail(NotSupportMsg); 269 return; 270 } 271 Provider prov = null; 272 for (int t = 0; t < validValues.length; t++ ) { 273 try { 274 CertPathValidator.getInstance(validValues[t], prov); 275 fail("IllegalArgumentException must be thrown"); 276 } catch (IllegalArgumentException e1) { 277 } 278 } 279 } 280 281 /** 282 * Test for <code>getInstance(String algorithm, String provider)</code> method 283 * Assertion: 284 * throws NullPointerException when algorithm is null 285 * throws NoSuchAlgorithmException when algorithm is not available 286 */ 287 public void testCertPathValidator09() 288 throws NoSuchAlgorithmException, NoSuchProviderException { 289 if (!PKIXSupport) { 290 fail(NotSupportMsg); 291 return; 292 } 293 try { 294 CertPathValidator.getInstance(null, defaultProvider); 295 fail("NullPointerException or NoSuchAlgorithmException must be thrown when algorithm is null"); 296 } catch (NullPointerException e) { 297 } catch (NoSuchAlgorithmException e) { 298 } 299 for (int i = 0; i < invalidValues.length; i++) { 300 try { 301 CertPathValidator.getInstance(invalidValues[i], defaultProvider); 302 fail("NoSuchAlgorithm must be thrown"); 303 } catch (NoSuchAlgorithmException e1) { 304 } 305 } 306 } 307 /** 308 * Test for <code>getInstance(String algorithm, String provider)</code> method 309 * Assertion: returns CertPathValidator object 310 */ 311 public void testCertPathValidator10() throws NoSuchAlgorithmException, 312 NoSuchProviderException { 313 if (!PKIXSupport) { 314 fail(NotSupportMsg); 315 return; 316 } 317 CertPathValidator certPV; 318 for (int i = 0; i < invalidValues.length; i++) { 319 certPV = CertPathValidator.getInstance(validValues[i], 320 defaultProvider); 321 assertEquals("Incorrect algorithm", certPV.getAlgorithm(), 322 validValues[i]); 323 assertEquals("Incorrect provider name", certPV.getProvider(), 324 defaultProvider); 325 } 326 } 327 328 /** 329 * Test for <code>validate(CertPath certpath, CertPathParameters params)</code> method 330 * Assertion: throws InvalidAlgorithmParameterException params is not 331 * instance of PKIXParameters or null 332 */ 333 public void testCertPathValidator11() 334 throws NoSuchAlgorithmException, NoSuchProviderException, CertPathValidatorException { 335 if (!PKIXSupport) { 336 fail(NotSupportMsg); 337 return; 338 } 339 CertPathValidator [] certPV = createCPVs(); 340 assertNotNull("CertPathValidator objects were not created", certPV); 341 MyCertPath mCP = new MyCertPath(new byte[0]); 342 invalidParams mPar = new invalidParams(); 343 for (int i = 0; i < certPV.length; i++) { 344 try { 345 certPV[i].validate(mCP, mPar); 346 fail("InvalidAlgorithmParameterException must be thrown"); 347 } catch(InvalidAlgorithmParameterException e) { 348 } 349 try { 350 certPV[i].validate(mCP, null); 351 fail("InvalidAlgorithmParameterException must be thrown"); 352 } catch(InvalidAlgorithmParameterException e) { 353 } 354 } 355 } 356 357 /** 358 * Test for 359 * <code>CertPathValidator</code> constructor 360 * Assertion: returns CertPathValidator object 361 */ 362 public void testCertPathValidator12() 363 throws CertificateException, NoSuchProviderException, NoSuchAlgorithmException, 364 CertPathValidatorException, InvalidAlgorithmParameterException { 365 if (!PKIXSupport) { 366 fail(NotSupportMsg); 367 return; 368 } 369 CertPathValidatorSpi spi = new MyCertPathValidatorSpi(); 370 CertPathValidator certPV = new myCertPathValidator(spi, 371 defaultProvider, defaultType); 372 assertEquals("Incorrect algorithm", certPV.getAlgorithm(), defaultType); 373 assertEquals("Incorrect provider", certPV.getProvider(), defaultProvider); 374 certPV.validate(null, null); 375 try { 376 certPV.validate(null, null); 377 fail("CertPathValidatorException must be thrown"); 378 } catch (CertPathValidatorException e) { 379 } 380 certPV = new myCertPathValidator(null, null, null); 381 assertNull("Incorrect algorithm", certPV.getAlgorithm()); 382 assertNull("Incorrect provider", certPV.getProvider()); 383 try { 384 certPV.validate(null, null); 385 fail("NullPointerException must be thrown"); 386 } catch (NullPointerException e) { 387 } 388 } 389} 390/** 391 * Additional class to verify CertPathValidator constructor 392 */ 393class myCertPathValidator extends CertPathValidator { 394 395 public myCertPathValidator(CertPathValidatorSpi spi, Provider prov, String type) { 396 super(spi, prov, type); 397 } 398} 399/** 400 * Additional class to verify validate method 401 */ 402class invalidParams implements CertPathParameters { 403 public Object clone() { 404 return new invalidParams(); 405 } 406} 407