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 package org.apache.harmony.xnet.tests.javax.net.ssl; 19 20 import java.security.InvalidAlgorithmParameterException; 21 import java.security.KeyStore; 22 import java.security.KeyStoreException; 23 import java.security.NoSuchAlgorithmException; 24 import java.security.NoSuchProviderException; 25 import java.security.Provider; 26 import java.security.Security; 27 28 import javax.net.ssl.ManagerFactoryParameters; 29 import javax.net.ssl.TrustManager; 30 import javax.net.ssl.TrustManagerFactory; 31 import javax.net.ssl.TrustManagerFactorySpi; 32 33 import org.apache.harmony.xnet.tests.support.SpiEngUtils; 34 import org.apache.harmony.xnet.tests.support.MyTrustManagerFactorySpi; 35 import junit.framework.TestCase; 36 37 /** 38 * Tests for <code>TrustManagerFactory</code> class constructors and methods. 39 * 40 */ 41 42 public class TrustManagerFactory1Test extends TestCase { 43 44 private static final String srvTrustManagerFactory = "TrustManagerFactory"; 45 46 private static String defaultAlgorithm = null; 47 48 private static String defaultProviderName = null; 49 50 private static Provider defaultProvider = null; 51 52 private static boolean DEFSupported = false; 53 54 private static final String NotSupportedMsg = "There is no suitable provider for TrustManagerFactory"; 55 56 private static final String[] invalidValues = SpiEngUtils.invalidValues; 57 58 private static String[] validValues = new String[3]; 59 static { 60 defaultAlgorithm = Security 61 .getProperty("ssl.TrustManagerFactory.algorithm"); 62 if (defaultAlgorithm != null) { 63 defaultProvider = SpiEngUtils.isSupport(defaultAlgorithm, 64 srvTrustManagerFactory); 65 DEFSupported = (defaultProvider != null); 66 defaultProviderName = (DEFSupported ? defaultProvider.getName() 67 : null); 68 validValues[0] = defaultAlgorithm; 69 validValues[1] = defaultAlgorithm.toUpperCase(); 70 validValues[2] = defaultAlgorithm.toLowerCase(); 71 } 72 } 73 74 protected TrustManagerFactory[] createTMFac() { 75 if (!DEFSupported) { 76 fail(defaultAlgorithm + " algorithm is not supported"); 77 return null; 78 } 79 TrustManagerFactory[] tMF = new TrustManagerFactory[3]; 80 try { 81 tMF[0] = TrustManagerFactory.getInstance(defaultAlgorithm); 82 tMF[1] = TrustManagerFactory.getInstance(defaultAlgorithm, 83 defaultProvider); 84 tMF[2] = TrustManagerFactory.getInstance(defaultAlgorithm, 85 defaultProviderName); 86 return tMF; 87 } catch (Exception e) { 88 e.printStackTrace(); 89 return null; 90 } 91 } 92 93 /** 94 * Test for <code>getDefaultAlgorithm()</code> method 95 * Assertion: returns value which is specifoed in security property 96 */ 97 public void testGetDefaultAlgorithm() { 98 if (!DEFSupported) { 99 fail(NotSupportedMsg); 100 return; 101 } 102 String def = TrustManagerFactory.getDefaultAlgorithm(); 103 if (defaultAlgorithm == null) { 104 assertNull("DefaultAlgorithm must be null", def); 105 } else { 106 assertEquals("Invalid default algorithm", def, defaultAlgorithm); 107 } 108 String defA = "Proba.trustmanagerfactory.defaul.type"; 109 Security.setProperty("ssl.TrustManagerFactory.algorithm", defA); 110 assertEquals("Incorrect defaultAlgorithm", 111 TrustManagerFactory.getDefaultAlgorithm(), defA); 112 if (def == null) { 113 def = ""; 114 } 115 Security.setProperty("ssl.TrustManagerFactory.algorithm", def); 116 assertEquals("Incorrect defaultAlgorithm", 117 TrustManagerFactory.getDefaultAlgorithm(), def); 118 } 119 /** 120 * Test for <code>getInstance(String algorithm)</code> method 121 * Assertions: returns security property "ssl.TrustManagerFactory.algorithm"; 122 * returns instance of TrustManagerFactory 123 */ 124 public void testTrustManagerFactory01() throws NoSuchAlgorithmException { 125 if (!DEFSupported) { 126 fail(NotSupportedMsg); 127 return; 128 } 129 TrustManagerFactory trustMF; 130 for (int i = 0; i < validValues.length; i++) { 131 trustMF = TrustManagerFactory.getInstance(validValues[i]); 132 assertTrue("Not TrustManagerFactory object", 133 trustMF instanceof TrustManagerFactory); 134 assertEquals("Invalid algorithm", trustMF.getAlgorithm(), 135 validValues[i]); 136 } 137 } 138 139 /** 140 * Test for <code>getInstance(String algorithm)</code> method 141 * Assertion: 142 * throws NullPointerException when algorithm is null; 143 * throws NoSuchAlgorithmException when algorithm is not correct; 144 */ 145 public void testTrustManagerFactory02() { 146 try { 147 TrustManagerFactory.getInstance(null); 148 fail("NoSuchAlgorithmException or NullPointerException should be thrown (algorithm is null"); 149 } catch (NoSuchAlgorithmException e) { 150 } catch (NullPointerException e) { 151 } 152 for (int i = 0; i < invalidValues.length; i++) { 153 try { 154 TrustManagerFactory.getInstance(invalidValues[i]); 155 fail("NoSuchAlgorithmException was not thrown as expected for algorithm: " 156 .concat(invalidValues[i])); 157 } catch (NoSuchAlgorithmException e) { 158 } 159 } 160 } 161 162 /** 163 * Test for <code>getInstance(String algorithm, String provider)</code> 164 * method 165 * Assertion: throws IllegalArgumentException when provider is null 166 * or empty 167 */ 168 public void testTrustManagerFactory03() throws NoSuchProviderException, 169 NoSuchAlgorithmException { 170 if (!DEFSupported) { 171 fail(NotSupportedMsg); 172 return; 173 } 174 String provider = null; 175 for (int i = 0; i < validValues.length; i++) { 176 try { 177 TrustManagerFactory.getInstance(validValues[i], provider); 178 fail("IllegalArgumentException must be thrown when provider is null"); 179 } catch (IllegalArgumentException e) { 180 } 181 try { 182 TrustManagerFactory.getInstance(validValues[i], ""); 183 fail("IllegalArgumentException must be thrown when provider is empty"); 184 } catch (IllegalArgumentException e) { 185 } 186 } 187 } 188 189 /** 190 * Test for <code>getInstance(String algorithm, String provider)</code> 191 * method 192 * Assertion: 193 * throws NullPointerException when algorithm is null; 194 * throws NoSuchAlgorithmException when algorithm is not correct; 195 */ 196 public void testTrustManagerFactory04() throws NoSuchProviderException { 197 if (!DEFSupported) { 198 fail(NotSupportedMsg); 199 return; 200 } 201 try { 202 TrustManagerFactory.getInstance(null, defaultProviderName); 203 fail("NoSuchAlgorithmException or NullPointerException should be thrown (algorithm is null"); 204 } catch (NoSuchAlgorithmException e) { 205 } catch (NullPointerException e) { 206 } 207 for (int i = 0; i < invalidValues.length; i++) { 208 try { 209 TrustManagerFactory.getInstance(invalidValues[i], 210 defaultProviderName); 211 fail("NoSuchAlgorithmException must be thrown (algorithm: " 212 .concat(invalidValues[i]).concat(")")); 213 } catch (NoSuchAlgorithmException e) { 214 } 215 } 216 } 217 218 /** 219 * Test for <code>getInstance(String algorithm, String provider)</code> 220 * method 221 * Assertion: throws NoSuchProviderException when provider has 222 * invalid value 223 */ 224 public void testTrustManagerFactory05() throws NoSuchAlgorithmException { 225 if (!DEFSupported) { 226 fail(NotSupportedMsg); 227 return; 228 } 229 for (int i = 1; i < invalidValues.length; i++) { 230 for (int j = 0; j < validValues.length; j++) { 231 try { 232 TrustManagerFactory.getInstance(validValues[j], 233 invalidValues[i]); 234 fail("NuSuchProviderException must be thrown (algorithm: " 235 .concat(validValues[j]).concat(" provider: ") 236 .concat(invalidValues[i]).concat(")")); 237 } catch (NoSuchProviderException e) { 238 } 239 } 240 } 241 } 242 243 /** 244 * Test for <code>getInstance(String algorithm, String provider)</code> 245 * method 246 * Assertion: returns instance of TrustManagerFactory 247 */ 248 public void testTrustManagerFactory06() throws NoSuchAlgorithmException, 249 NoSuchProviderException { 250 if (!DEFSupported) { 251 fail(NotSupportedMsg); 252 return; 253 } 254 TrustManagerFactory trustMF; 255 for (int i = 0; i < validValues.length; i++) { 256 trustMF = TrustManagerFactory.getInstance(validValues[i], 257 defaultProviderName); 258 assertTrue("Not TrustManagerFactory object", 259 trustMF instanceof TrustManagerFactory); 260 assertEquals("Invalid algorithm", trustMF.getAlgorithm(), 261 validValues[i]); 262 assertEquals("Invalid provider", trustMF.getProvider(), 263 defaultProvider); 264 } 265 } 266 267 /** 268 * Test for <code>getInstance(String algorithm, Provider provider)</code> 269 * method 270 * Assertion: throws IllegalArgumentException when provider is null 271 */ 272 public void testTrustManagerFactory07() throws NoSuchAlgorithmException { 273 if (!DEFSupported) { 274 fail(NotSupportedMsg); 275 return; 276 } 277 Provider provider = null; 278 for (int i = 0; i < validValues.length; i++) { 279 try { 280 TrustManagerFactory.getInstance(validValues[i], provider); 281 fail("IllegalArgumentException must be thrown when provider is null"); 282 } catch (IllegalArgumentException e) { 283 } 284 } 285 } 286 287 /** 288 * Test for <code>getInstance(String algorithm, Provider provider)</code> 289 * method 290 * Assertion: 291 * throws NullPointerException when algorithm is null; 292 * throws NoSuchAlgorithmException when algorithm is not correct; 293 */ 294 public void testTrustManagerFactory08() { 295 if (!DEFSupported) { 296 fail(NotSupportedMsg); 297 return; 298 } 299 try { 300 TrustManagerFactory.getInstance(null, defaultProvider); 301 fail("NoSuchAlgorithmException or NullPointerException should be thrown (algorithm is null"); 302 } catch (NoSuchAlgorithmException e) { 303 } catch (NullPointerException e) { 304 } 305 for (int i = 0; i < invalidValues.length; i++) { 306 try { 307 TrustManagerFactory.getInstance(invalidValues[i], 308 defaultProvider); 309 fail("NoSuchAlgorithmException must be thrown (algorithm: " 310 .concat(invalidValues[i]).concat(")")); 311 } catch (NoSuchAlgorithmException e) { 312 } 313 } 314 } 315 316 /** 317 * Test for <code>getInstance(String algorithm, Provider provider)</code> 318 * method 319 * Assertion: returns instance of TrustManagerFactory 320 */ 321 public void testTrustManagerFactory09() throws NoSuchAlgorithmException { 322 if (!DEFSupported) { 323 fail(NotSupportedMsg); 324 return; 325 } 326 TrustManagerFactory trustMF; 327 for (int i = 0; i < validValues.length; i++) { 328 trustMF = TrustManagerFactory.getInstance(validValues[i], 329 defaultProvider); 330 assertTrue("Not TrustManagerFactory object", 331 trustMF instanceof TrustManagerFactory); 332 assertEquals("Invalid algorithm", trustMF.getAlgorithm(), 333 validValues[i]); 334 assertEquals("Invalid provider", trustMF.getProvider(), 335 defaultProvider); 336 } 337 } 338 339 /** 340 * Test for 341 * <code>TrustManagerFactory(TrustManagerFactorySpi impl, Provider prov, String algoriyjm) </code> 342 * constructor 343 * Assertion: created new TrustManagerFactory object 344 */ 345 public void testTrustManagerFactory10() throws NoSuchAlgorithmException { 346 if (!DEFSupported) { 347 fail(NotSupportedMsg); 348 return; 349 } 350 TrustManagerFactorySpi spi = new MyTrustManagerFactorySpi(); 351 TrustManagerFactory tmF = new myTrustManagerFactory(spi, defaultProvider, 352 defaultAlgorithm); 353 assertTrue("Not CertStore object", tmF instanceof TrustManagerFactory); 354 assertEquals("Incorrect algorithm", tmF.getAlgorithm(), 355 defaultAlgorithm); 356 assertEquals("Incorrect provider", tmF.getProvider(), defaultProvider); 357 assertNull("Incorrect result", tmF.getTrustManagers()); 358 359 tmF = new myTrustManagerFactory(null, null, null); 360 assertTrue("Not CertStore object", tmF instanceof TrustManagerFactory); 361 assertNull("Provider must be null", tmF.getProvider()); 362 assertNull("Algorithm must be null", tmF.getAlgorithm()); 363 try { 364 tmF.getTrustManagers(); 365 fail("NullPointerException must be thrown"); 366 } catch (NullPointerException e) { 367 } 368 } 369 370 /** 371 * Test for <code>init(KeyStore keyStore)</code> and 372 * <code>getTrustManagers()</code> 373 * Assertion: returns not empty TrustManager array 374 */ 375 public void testTrustManagerFactory11() throws Exception { 376 if (!DEFSupported) { 377 fail(NotSupportedMsg); 378 return; 379 } 380 KeyStore ks; 381 KeyStore ksNull = null; 382 ks = KeyStore.getInstance(KeyStore.getDefaultType()); 383 ks.load(null, null); 384 385 TrustManager[] tm; 386 TrustManagerFactory[] trustMF = createTMFac(); 387 assertNotNull("TrustManagerFactory objects were not created", trustMF); 388 for (int i = 0; i < trustMF.length; i++) { 389 try { 390 trustMF[i].init(ksNull); 391 } catch (KeyStoreException e) { 392 } 393 trustMF[i].init(ks); 394 tm = trustMF[i].getTrustManagers(); 395 assertNotNull("Result has not be null", tm); 396 assertTrue("Length of result TrustManager array should not be 0", 397 (tm.length > 0)); 398 } 399 } 400 401 /** 402 * Test for <code>init(ManagerFactoryParameters params)</code> 403 * Assertion: 404 * throws InvalidAlgorithmParameterException when params is null 405 */ 406 public void testTrustManagerFactory12() throws NoSuchAlgorithmException, 407 KeyStoreException, InvalidAlgorithmParameterException { 408 if (!DEFSupported) { 409 fail(NotSupportedMsg); 410 return; 411 } 412 ManagerFactoryParameters par = null; 413 ManagerFactoryParameters par1 = new myManagerFactoryParam(); 414 TrustManagerFactory[] trustMF = createTMFac(); 415 assertNotNull("TrustManagerFactory objects were not created", trustMF); 416 for (int i = 0; i < trustMF.length; i++) { 417 try { 418 trustMF[i].init(par); 419 fail("InvalidAlgorithmParameterException must be thrown"); 420 } catch (InvalidAlgorithmParameterException e) { 421 } 422 try { 423 trustMF[i].init(par1); 424 fail("InvalidAlgorithmParameterException must be thrown"); 425 } catch (InvalidAlgorithmParameterException e) { 426 } 427 } 428 } 429 430 } 431 432 /** 433 * Addifional class to verify TrustManagerFactory constructor 434 */ 435 436 class myTrustManagerFactory extends TrustManagerFactory { 437 public myTrustManagerFactory(TrustManagerFactorySpi spi, Provider prov, 438 String alg) { 439 super(spi, prov, alg); 440 } 441 } 442 443 class myManagerFactoryParam implements ManagerFactoryParameters { 444 445 }