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 22 package org.apache.harmony.security.tests.java.security.cert; 23 24 import java.security.InvalidAlgorithmParameterException; 25 import java.security.NoSuchAlgorithmException; 26 import java.security.NoSuchProviderException; 27 import java.security.Provider; 28 import java.security.Security; 29 import java.security.cert.CertPathBuilder; 30 import java.security.cert.CertPathBuilderException; 31 import java.security.cert.CertPathBuilderSpi; 32 import java.security.cert.CertificateException; 33 34 import junit.framework.TestCase; 35 36 import org.apache.harmony.security.tests.support.SpiEngUtils; 37 import org.apache.harmony.security.tests.support.cert.MyCertPathBuilderSpi; 38 39 import tests.support.Support_Exec; 40 41 /** 42 * Tests for <code>CertPathBuilder</code> class constructors and 43 * methods. 44 */ 45 46 public class CertPathBuilder1Test extends TestCase { 47 48 /** 49 * Constructor for CertPathBuilderTests. 50 * 51 * @param name 52 */ CertPathBuilder1Test(String name)53 public CertPathBuilder1Test(String name) { 54 super(name); 55 } 56 57 public static final String srvCertPathBuilder = "CertPathBuilder"; 58 59 public static final String defaultType = "PKIX"; 60 public static final String[] validValues = { 61 "PKIX", "pkix", "PkiX", "pKiX" }; 62 63 private static String[] invalidValues = SpiEngUtils.invalidValues; 64 65 private static boolean PKIXSupport = false; 66 67 private static Provider defaultProvider; 68 private static String defaultProviderName; 69 70 private static String NotSupportMsg = ""; 71 72 public static final String DEFAULT_TYPE_PROPERTY = "certpathbuilder.type"; 73 74 static { 75 defaultProvider = SpiEngUtils.isSupport(defaultType, 76 srvCertPathBuilder); 77 PKIXSupport = (defaultProvider != null); 78 defaultProviderName = (PKIXSupport ? defaultProvider.getName() : null); 79 NotSupportMsg = defaultType.concat(" is not supported"); 80 } 81 createCPBs()82 private static CertPathBuilder[] createCPBs() { 83 if (!PKIXSupport) { 84 fail(NotSupportMsg); 85 return null; 86 } 87 try { 88 CertPathBuilder[] certPBs = new CertPathBuilder[3]; 89 certPBs[0] = CertPathBuilder.getInstance(defaultType); 90 certPBs[1] = CertPathBuilder.getInstance(defaultType, 91 defaultProviderName); 92 certPBs[2] = CertPathBuilder.getInstance(defaultType, 93 defaultProvider); 94 return certPBs; 95 } catch (Exception e) { 96 return null; 97 } 98 } 99 100 /** 101 * @tests java.security.cert.CertPathBuilder#getDefaultType() 102 */ test_getDefaultType()103 public void test_getDefaultType() throws Exception { 104 105 // Regression for HARMONY-2785 106 107 // test: default value 108 assertNull(Security.getProperty(DEFAULT_TYPE_PROPERTY)); 109 assertEquals("PKIX", CertPathBuilder.getDefaultType()); 110 111 // test: security property. fork new VM to keep testing env. clean 112 Support_Exec.execJava(new String[] { DefaultType.class.getName() }, 113 null, true); 114 } 115 116 public static class DefaultType { 117 main(String[] args)118 public static void main(String[] args) { 119 120 Security.setProperty(DEFAULT_TYPE_PROPERTY, "MyType"); 121 assertEquals("MyType", CertPathBuilder.getDefaultType()); 122 123 Security.setProperty(DEFAULT_TYPE_PROPERTY, "AnotherType"); 124 assertEquals("AnotherType", CertPathBuilder.getDefaultType()); 125 } 126 } 127 128 /** 129 * Test for <code>getInstance(String algorithm)</code> method 130 * Assertion: 131 * throws NullPointerException when algorithm is null 132 * throws NoSuchAlgorithmException when algorithm is not correct 133 * or it is not available 134 */ testCertPathBuilder02()135 public void testCertPathBuilder02() throws NoSuchAlgorithmException { 136 try { 137 CertPathBuilder.getInstance(null); 138 fail("No expected NullPointerException"); 139 } catch (NullPointerException e) { 140 } 141 for (int i = 0; i < invalidValues.length; i++) { 142 try { 143 CertPathBuilder.getInstance(invalidValues[i]); 144 fail("NoSuchAlgorithmException must be thrown"); 145 } catch (NoSuchAlgorithmException e) { 146 } 147 } 148 } 149 150 /** 151 * Test for <code>getInstance(String algorithm)</code> method 152 * Assertion: returns CertPathBuilder object 153 */ testCertPathBuilder03()154 public void testCertPathBuilder03() throws NoSuchAlgorithmException { 155 if (!PKIXSupport) { 156 fail(NotSupportMsg); 157 return; 158 } 159 for (int i = 0; i < validValues.length; i++) { 160 CertPathBuilder cpb = CertPathBuilder.getInstance(validValues[i]); 161 assertEquals("Incorrect algorithm", cpb.getAlgorithm(), validValues[i]); 162 } 163 } 164 165 /** 166 * Test for <code>getInstance(String algorithm, String provider)</code> method 167 * Assertion: throws IllegalArgumentException when provider is null or empty 168 * <p/> 169 * FIXME: verify what exception will be thrown if provider is empty 170 */ testCertPathBuilder04()171 public void testCertPathBuilder04() 172 throws NoSuchAlgorithmException, NoSuchProviderException { 173 if (!PKIXSupport) { 174 fail(NotSupportMsg); 175 return; 176 } 177 String provider = null; 178 for (int i = 0; i < validValues.length; i++) { 179 try { 180 CertPathBuilder.getInstance(validValues[i], provider); 181 fail("IllegalArgumentException must be thrown thrown"); 182 } catch (IllegalArgumentException e) { 183 } 184 try { 185 CertPathBuilder.getInstance(validValues[i], ""); 186 fail("IllegalArgumentException must be thrown thrown"); 187 } catch (IllegalArgumentException e) { 188 } 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 */ testCertPathBuilder05()197 public void testCertPathBuilder05() 198 throws NoSuchAlgorithmException { 199 if (!PKIXSupport) { 200 fail(NotSupportMsg); 201 return; 202 } 203 for (int i = 0; i < validValues.length; i++) { 204 for (int j = 1; j < invalidValues.length; j++) { 205 try { 206 CertPathBuilder.getInstance(validValues[i], invalidValues[j]); 207 fail("NoSuchProviderException must be hrown"); 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 correct 219 */ testCertPathBuilder06()220 public void testCertPathBuilder06() 221 throws NoSuchAlgorithmException, NoSuchProviderException { 222 if (!PKIXSupport) { 223 fail(NotSupportMsg); 224 return; 225 } 226 try { 227 CertPathBuilder.getInstance(null, defaultProviderName); 228 fail("No expected NullPointerException"); 229 } catch (NullPointerException e) { 230 } 231 for (int i = 0; i < invalidValues.length; i++) { 232 try { 233 CertPathBuilder.getInstance(invalidValues[i], defaultProviderName); 234 fail("NoSuchAlgorithmException must be thrown"); 235 } catch (NoSuchAlgorithmException e1) { 236 } 237 } 238 } 239 240 /** 241 * Test for <code>getInstance(String algorithm, String provider)</code> method 242 * Assertion: returns CertPathBuilder object 243 */ testCertPathBuilder07()244 public void testCertPathBuilder07() 245 throws NoSuchAlgorithmException, NoSuchProviderException { 246 if (!PKIXSupport) { 247 fail(NotSupportMsg); 248 return; 249 } 250 CertPathBuilder certPB; 251 for (int i = 0; i < validValues.length; i++) { 252 certPB = CertPathBuilder.getInstance(validValues[i], defaultProviderName); 253 assertEquals("Incorrect algorithm", certPB.getAlgorithm(), validValues[i]); 254 assertEquals("Incorrect provider name", certPB.getProvider().getName(), defaultProviderName); 255 } 256 } 257 258 /** 259 * Test for <code>getInstance(String algorithm, Provider provider)</code> method 260 * Assertion: throws IllegalArgumentException when provider is null 261 */ testCertPathBuilder08()262 public void testCertPathBuilder08() 263 throws NoSuchAlgorithmException { 264 if (!PKIXSupport) { 265 fail(NotSupportMsg); 266 return; 267 } 268 Provider prov = null; 269 for (int t = 0; t < validValues.length; t++) { 270 try { 271 CertPathBuilder.getInstance(validValues[t], prov); 272 fail("IllegalArgumentException must be thrown"); 273 } catch (IllegalArgumentException e1) { 274 } 275 } 276 } 277 278 /** 279 * Test for <code>getInstance(String algorithm, String provider)</code> method 280 * Assertion: 281 * throws NullPointerException when algorithm is null 282 * throws NoSuchAlgorithmException when algorithm is not correct 283 */ testCertPathBuilder09()284 public void testCertPathBuilder09() 285 throws NoSuchAlgorithmException, NoSuchProviderException { 286 if (!PKIXSupport) { 287 fail(NotSupportMsg); 288 return; 289 } 290 try { 291 CertPathBuilder.getInstance(null, defaultProvider); 292 fail("No expected NullPointerException"); 293 } catch (NullPointerException e) { 294 } 295 for (int i = 0; i < invalidValues.length; i++) { 296 try { 297 CertPathBuilder.getInstance(invalidValues[i], defaultProvider); 298 fail("NoSuchAlgorithm must be thrown"); 299 } catch (NoSuchAlgorithmException e1) { 300 } 301 } 302 } 303 304 /** 305 * Test for <code>getInstance(String algorithm, String provider)</code> method 306 * Assertion: returns CertPathBuilder object 307 */ testCertPathBuilder10()308 public void testCertPathBuilder10() 309 throws NoSuchAlgorithmException, NoSuchProviderException { 310 if (!PKIXSupport) { 311 fail(NotSupportMsg); 312 return; 313 } 314 CertPathBuilder certPB; 315 for (int i = 0; i < invalidValues.length; i++) { 316 certPB = CertPathBuilder.getInstance(validValues[i], defaultProvider); 317 assertEquals("Incorrect algorithm", certPB.getAlgorithm(), validValues[i]); 318 assertEquals("Incorrect provider name", certPB.getProvider(), defaultProvider); 319 } 320 } 321 322 /** 323 * Test for <code>build(CertPathParameters params)</code> method 324 * Assertion: throws InvalidAlgorithmParameterException params is null 325 */ testCertPathBuilder11()326 public void testCertPathBuilder11() 327 throws NoSuchAlgorithmException, NoSuchProviderException, 328 CertPathBuilderException { 329 if (!PKIXSupport) { 330 fail(NotSupportMsg); 331 return; 332 } 333 CertPathBuilder[] certPB = createCPBs(); 334 assertNotNull("CertPathBuilder objects were not created", certPB); 335 for (int i = 0; i < certPB.length; i++) { 336 try { 337 certPB[i].build(null); 338 fail("InvalidAlgorithmParameterException must be thrown"); 339 } catch (InvalidAlgorithmParameterException e) { 340 } 341 } 342 } 343 344 /** 345 * Test for 346 * <code>CertPathBuilder</code> constructor 347 * Assertion: returns CertPathBuilder object 348 */ testCertPathBuilder12()349 public void testCertPathBuilder12() 350 throws CertificateException, NoSuchProviderException, 351 NoSuchAlgorithmException, InvalidAlgorithmParameterException, 352 CertPathBuilderException { 353 if (!PKIXSupport) { 354 fail(NotSupportMsg); 355 return; 356 } 357 CertPathBuilderSpi spi = new MyCertPathBuilderSpi(); 358 CertPathBuilder certPB = new myCertPathBuilder(spi, 359 defaultProvider, defaultType); 360 assertEquals("Incorrect algorithm", certPB.getAlgorithm(), defaultType); 361 assertEquals("Incorrect provider", certPB.getProvider(), defaultProvider); 362 try { 363 certPB.build(null); 364 fail("CertPathBuilderException must be thrown "); 365 } catch (CertPathBuilderException e) { 366 } 367 certPB = new myCertPathBuilder(null, null, null); 368 assertNull("Incorrect algorithm", certPB.getAlgorithm()); 369 assertNull("Incorrect provider", certPB.getProvider()); 370 try { 371 certPB.build(null); 372 fail("NullPointerException must be thrown "); 373 } catch (NullPointerException e) { 374 } 375 } 376 } 377 378 /** 379 * Additional class to verify CertPathBuilder constructor 380 */ 381 class myCertPathBuilder extends CertPathBuilder { 382 myCertPathBuilder(CertPathBuilderSpi spi, Provider prov, String type)383 public myCertPathBuilder(CertPathBuilderSpi spi, Provider prov, String type) { 384 super(spi, prov, type); 385 } 386 } 387