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.CertStore; 30 import java.security.cert.CertStoreException; 31 import java.security.cert.CertStoreParameters; 32 import java.security.cert.CertStoreSpi; 33 import java.security.cert.CollectionCertStoreParameters; 34 import java.security.cert.LDAPCertStoreParameters; 35 import java.util.Collection; 36 37 import org.apache.harmony.security.tests.support.SpiEngUtils; 38 import org.apache.harmony.security.tests.support.cert.MyCertStoreParameters; 39 import org.apache.harmony.security.tests.support.cert.MyCertStoreSpi; 40 41 import junit.framework.TestCase; 42 43 /** 44 * Tests for <code>CertStore</code> class constructors and 45 * methods. 46 */ 47 48 public class CertStore1Test extends TestCase { 49 50 /** 51 * Constructor for CertStoreTests. 52 * 53 * @param arg0 54 */ CertStore1Test(String arg0)55 public CertStore1Test(String arg0) { 56 super(arg0); 57 } 58 59 public static final String srvCertStore = "CertStore"; 60 61 private static final String defaultType = "LDAP"; 62 public static final String[] validValues = { 63 "LDAP", "ldap", "Ldap", "lDAP", "lDaP" }; 64 public static String[] validValuesC = null; 65 66 private static String[] invalidValues = SpiEngUtils.invalidValues; 67 68 private static boolean LDAPSupport = false; 69 private static final String CollectionType = "Collection"; 70 private static boolean CollectionSupport = false; 71 72 private static Provider defaultProvider; 73 private static String defaultProviderName; 74 private static Provider defaultProviderCol; 75 private static String defaultProviderColName; 76 77 private static String NotSupportMsg = ""; 78 79 static { 80 defaultProvider = SpiEngUtils.isSupport(defaultType, 81 srvCertStore); 82 LDAPSupport = (defaultProvider != null); 83 defaultProviderName = (LDAPSupport ? defaultProvider.getName() : null); 84 NotSupportMsg = "LDAP and Collection algorithm are not supported"; 85 86 defaultProviderCol = SpiEngUtils.isSupport(CollectionType, 87 srvCertStore); 88 CollectionSupport = (defaultProviderCol != null); 89 defaultProviderColName = (CollectionSupport ? defaultProviderCol.getName() : null); 90 if (CollectionSupport) { 91 validValuesC = new String[3]; 92 validValuesC[0] = CollectionType; 93 validValuesC[1] = CollectionType.toUpperCase(); 94 validValuesC[2] = CollectionType.toLowerCase(); 95 } 96 } 97 98 private Provider dProv = null; 99 private String dName = null; 100 private String dType = null; 101 private CertStoreParameters dParams = null; 102 private String[] dValid; 103 initParams()104 private boolean initParams() { 105 if (!LDAPSupport && !CollectionSupport) { 106 fail(NotSupportMsg); 107 return false; 108 } 109 dParams = (CollectionSupport ? (CertStoreParameters) new CollectionCertStoreParameters() : 110 (CertStoreParameters) new LDAPCertStoreParameters()); 111 dType = (CollectionSupport ? CollectionType : defaultType); 112 dProv = (CollectionSupport ? defaultProviderCol : defaultProvider); 113 dName = (CollectionSupport ? defaultProviderColName : defaultProviderName); 114 dValid = (CollectionSupport ? validValuesC : validValues); 115 return true; 116 } 117 createCS()118 private CertStore[] createCS() { 119 if (!LDAPSupport && !CollectionSupport) { 120 fail(NotSupportMsg); 121 return null; 122 } 123 try { 124 CertStore[] ss = new CertStore[3]; 125 ss[0] = CertStore.getInstance(dType, dParams); 126 ss[1] = CertStore.getInstance(dType, dParams, dProv); 127 ss[2] = CertStore.getInstance(dType, dParams, dName); 128 return ss; 129 } catch (Exception e) { 130 return null; 131 } 132 } 133 134 135 /** 136 * Test for <code>getDefaultType()</code> method 137 * Assertion: returns security property "certstore.type" or "LDAP" 138 */ testCertStore01()139 public void testCertStore01() { 140 if (!LDAPSupport) { 141 return; 142 } 143 String dt = CertStore.getDefaultType(); 144 String sn = Security.getProperty("certstore.type"); 145 String def = "Proba.cert.store.type"; 146 if (sn == null) { 147 sn = defaultType; 148 } 149 assertNotNull("Default type have not be null", dt); 150 assertEquals("Incorrect default type", dt, sn); 151 152 Security.setProperty("certstore.type", def); 153 dt = CertStore.getDefaultType(); 154 assertEquals("Incorrect default type", dt, def); 155 Security.setProperty("certstore.type", sn); 156 assertEquals("Incorrect default type", Security.getProperty("certstore.type"), sn); 157 } 158 159 /** 160 * Test for 161 * <code>CertStore</code> constructor 162 * Assertion: returns CertStore object 163 */ 164 testCertStore02()165 public void testCertStore02() throws NoSuchAlgorithmException, 166 InvalidAlgorithmParameterException, CertStoreException { 167 if (!initParams()) { 168 return; 169 } 170 MyCertStoreParameters pp = new MyCertStoreParameters(); 171 CertStoreSpi spi = new MyCertStoreSpi(pp); 172 CertStore certS = new myCertStore(spi, dProv, dType, pp); 173 assertEquals("Incorrect algorithm", certS.getType(), dType); 174 assertEquals("Incorrect provider", certS.getProvider(), dProv); 175 assertTrue("Incorrect parameters", certS.getCertStoreParameters() 176 instanceof MyCertStoreParameters); 177 try { 178 certS.getCertificates(null); 179 fail("CertStoreException must be thrown"); 180 } catch (CertStoreException e) { 181 } 182 certS = new myCertStore(null, null, null, null); 183 assertNull("Incorrect algorithm", certS.getType()); 184 assertNull("Incorrect provider", certS.getProvider()); 185 assertNull("Incorrect parameters", certS.getCertStoreParameters()); 186 try { 187 certS.getCertificates(null); 188 fail("NullPointerException must be thrown"); 189 } catch (NullPointerException e) { 190 } 191 } 192 193 /** 194 * Test for <code>getInstance(String type, CertStoreParameters params)</code> method 195 * Assertion: 196 * throws NullPointerException when type is null 197 * throws NoSuchAlgorithmException when type is incorrect; 198 */ testCertStore03()199 public void testCertStore03() throws InvalidAlgorithmParameterException { 200 if (!initParams()) { 201 return; 202 } 203 try { 204 CertStore.getInstance(null, dParams); 205 fail("NullPointerException or NoSuchAlgorithmException must be thrown when type is null"); 206 } catch (NullPointerException e) { 207 } catch (NoSuchAlgorithmException e) { 208 } 209 for (int i = 0; i < invalidValues.length; i++) { 210 try { 211 CertStore.getInstance(invalidValues[i], dParams); 212 fail("NoSuchAlgorithmException must be thrown"); 213 } catch (NoSuchAlgorithmException e) { 214 } 215 } 216 } 217 218 /** 219 * Test for <code>getInstance(String type, CertStoreParameters params)</code> method 220 * Assertion: return CertStore object 221 */ testCertStore05()222 public void testCertStore05() 223 throws InvalidAlgorithmParameterException, NoSuchAlgorithmException { 224 if (!initParams()) { 225 return; 226 } 227 CertStore certS; 228 for (int i = 0; i < dValid.length; i++) { 229 certS = CertStore.getInstance(dValid[i], dParams); 230 assertEquals("Incorrect type", certS.getType(), dValid[i]); 231 certS.getCertStoreParameters(); 232 } 233 } 234 235 /** 236 * Test for method 237 * <code>getInstance(String type, CertStoreParameters params, String provider)</code> 238 * Assertion: throws IllegalArgumentException when provider is null or empty 239 * <p/> 240 * FIXME: verify IllegalArgumentException when provider is empty 241 */ testCertStore06()242 public void testCertStore06() 243 throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, 244 NoSuchProviderException { 245 if (!initParams()) { 246 return; 247 } 248 String provider = null; 249 for (int i = 0; i < dValid.length; i++) { 250 try { 251 CertStore.getInstance(dValid[i], dParams, provider); 252 fail("IllegalArgumentException must be thrown"); 253 } catch (IllegalArgumentException e) { 254 } 255 try { 256 CertStore.getInstance(dValid[i], dParams, ""); 257 fail("IllegalArgumentException must be thrown"); 258 } catch (IllegalArgumentException e) { 259 } 260 } 261 } 262 263 /** 264 * Test for method 265 * <code>getInstance(String type, CertStoreParameters params, String provider)</code> 266 * Assertion: throws NoSuchProviderException when provider has invalid value 267 */ testCertStore07()268 public void testCertStore07() 269 throws InvalidAlgorithmParameterException, NoSuchAlgorithmException { 270 if (!initParams()) { 271 return; 272 } 273 for (int i = 0; i < dValid.length; i++) { 274 for (int j = 1; j < invalidValues.length; j++) { 275 try { 276 CertStore.getInstance(dValid[i], dParams, invalidValues[j]); 277 fail("NoSuchProviderException must be thrown"); 278 } catch (NoSuchProviderException e) { 279 } 280 } 281 } 282 } 283 284 /** 285 * Test for method 286 * <code>getInstance(String type, CertStoreParameters params, String provider)</code> 287 * Assertion: 288 * throws NullPointerException when type is null 289 * throws NoSuchAlgorithmException when type is incorrect; 290 */ testCertStore08()291 public void testCertStore08() 292 throws InvalidAlgorithmParameterException, NoSuchProviderException, 293 NoSuchAlgorithmException { 294 if (!initParams()) { 295 return; 296 } 297 for (int i = 0; i < invalidValues.length; i++) { 298 try { 299 CertStore.getInstance(invalidValues[i], dParams, dName); 300 fail("NoSuchAlgorithmException must be thrown"); 301 } catch (NoSuchAlgorithmException e) { 302 } 303 } 304 try { 305 CertStore.getInstance(null, dParams, dName); 306 fail("NullPointerException or NoSuchAlgorithmException must be thrown when type is null"); 307 } catch (NullPointerException e) { 308 } catch (NoSuchAlgorithmException e) { 309 } 310 } 311 312 /** 313 * Test for method 314 * <code>getInstance(String type, CertStoreParameters params, String provider)</code> 315 * Assertion: return CertStore object 316 */ testCertStore10()317 public void testCertStore10() 318 throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchProviderException { 319 if (!initParams()) { 320 return; 321 } 322 CertStore certS; 323 for (int i = 0; i < dValid.length; i++) { 324 certS = CertStore.getInstance(dValid[i], dParams, dName); 325 assertEquals("Incorrect type", certS.getType(), dValid[i]); 326 certS.getCertStoreParameters(); 327 } 328 } 329 330 /** 331 * Test for method 332 * <code>getInstance(String type, CertStoreParameters params, Provider provider)</code> 333 * Assertion: throws IllegalArgumentException when provider is null 334 */ testCertStore11()335 public void testCertStore11() 336 throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, 337 NoSuchProviderException { 338 if (!initParams()) { 339 return; 340 } 341 Provider provider = null; 342 for (int i = 0; i < dValid.length; i++) { 343 try { 344 CertStore.getInstance(dValid[i], dParams, provider); 345 fail("IllegalArgumentException must be thrown"); 346 } catch (IllegalArgumentException e) { 347 } 348 } 349 } 350 351 /** 352 * Test for <code>getInstance(String type, CertStoreParameters params, Provider provider)</code> method 353 * Assertion: 354 * throws NullPointerException when type is null 355 * throws NoSuchAlgorithmException when type is incorrect; 356 */ testCertStore12()357 public void testCertStore12() 358 throws InvalidAlgorithmParameterException, 359 NoSuchAlgorithmException { 360 if (!initParams()) { 361 return; 362 } 363 try { 364 CertStore.getInstance(null, dParams, dProv); 365 fail("NullPointerException or NoSuchAlgorithmException must be thrown when type is null"); 366 } catch (NullPointerException e) { 367 } catch (NoSuchAlgorithmException e) { 368 } 369 for (int i = 0; i < invalidValues.length; i++) { 370 try { 371 CertStore.getInstance(invalidValues[i], dParams, dProv); 372 fail("NoSuchAlgorithmException must be thrown"); 373 } catch (NoSuchAlgorithmException e) { 374 } 375 } 376 } 377 378 /** 379 * Test for method 380 * <code>getInstance(String type, CertStoreParameters params, Provider provider)</code> 381 * Assertion: return CertStore object 382 */ testCertStore14()383 public void testCertStore14() 384 throws InvalidAlgorithmParameterException, NoSuchAlgorithmException { 385 if (!initParams()) { 386 return; 387 } 388 CertStore certS; 389 for (int i = 0; i < dValid.length; i++) { 390 certS = CertStore.getInstance(dValid[i], dParams, dProv); 391 assertEquals("Incorrect type", certS.getType(), dValid[i]); 392 certS.getCertStoreParameters(); 393 } 394 } 395 396 /** 397 * Test for methods 398 * <code>getCertificates(CertSelector selector)</code> 399 * <code>getCRLs(CRLSelector selector)</code> 400 * Assertion: returns empty Collection when selector is null 401 */ testCertStore15()402 public void testCertStore15() 403 throws NoSuchAlgorithmException, InvalidAlgorithmParameterException, 404 CertStoreException { 405 if (!initParams()) { 406 return; 407 } 408 CertStore[] certS = createCS(); 409 assertNotNull("CertStore object were not created", certS); 410 Collection coll; 411 for (int i = 0; i < certS.length; i++) { 412 coll = certS[i].getCertificates(null); 413 assertTrue("Result collection not empty", coll.isEmpty()); 414 coll = certS[i].getCRLs(null); 415 assertTrue("Result collection not empty", coll.isEmpty()); 416 } 417 } 418 } 419 420 /** 421 * Additional class to verify CertStore constructor 422 */ 423 class myCertStore extends CertStore { myCertStore(CertStoreSpi spi, Provider prov, String type, CertStoreParameters params)424 public myCertStore(CertStoreSpi spi, Provider prov, String type, CertStoreParameters params) { 425 super(spi, prov, type, params); 426 } 427 } 428