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 import java.security.UnrecoverableKeyException; 28 29 import javax.net.ssl.KeyManagerFactory; 30 import javax.net.ssl.ManagerFactoryParameters; 31 32 import org.apache.harmony.xnet.tests.support.SpiEngUtils; 33 import org.apache.harmony.xnet.tests.support.MyKeyManagerFactorySpi; 34 import junit.framework.TestCase; 35 36 /** 37 * Tests for KeyManagerFactory class constructors and methods 38 * 39 */ 40 41 public class KeyManagerFactory2Test extends TestCase { 42 private static final String srvKeyManagerFactory = "KeyManagerFactory"; 43 44 private static final String defaultAlg = "KeyMF"; 45 46 private static final String KeyManagerFactoryProviderClass = "org.apache.harmony.xnet.tests.support.MyKeyManagerFactorySpi"; 47 48 private static final String[] invalidValues = SpiEngUtils.invalidValues; 49 50 private static final String[] validValues; 51 52 static { 53 validValues = new String[4]; 54 validValues[0] = defaultAlg; 55 validValues[1] = defaultAlg.toLowerCase(); 56 validValues[2] = "Keymf"; 57 validValues[3] = "kEYMF"; 58 } 59 60 Provider mProv; 61 62 @Override 63 protected void setUp() throws Exception { 64 super.setUp(); 65 mProv = (new SpiEngUtils()).new MyProvider("MyKMFProvider", 66 "Provider for testing", srvKeyManagerFactory.concat(".") 67 .concat(defaultAlg), KeyManagerFactoryProviderClass); 68 Security.insertProviderAt(mProv, 2); 69 } 70 71 /* 72 * @see TestCase#tearDown() 73 */ 74 @Override 75 protected void tearDown() throws Exception { 76 super.tearDown(); 77 Security.removeProvider(mProv.getName()); 78 } 79 80 private void checkResult(KeyManagerFactory keyMF) 81 throws Exception { 82 KeyStore kStore = null; 83 ManagerFactoryParameters mfp = null; 84 85 char[] pass = { 'a', 'b', 'c' }; 86 87 try { 88 keyMF.init(kStore, null); 89 fail("KeyStoreException must be thrown"); 90 } catch (KeyStoreException e) { 91 } 92 try { 93 keyMF.init(kStore, pass); 94 fail("UnrecoverableKeyException must be thrown"); 95 } catch (UnrecoverableKeyException e) { 96 } 97 try { 98 keyMF.init(mfp); 99 fail("InvalidAlgorithmParameterException must be thrown"); 100 } catch (InvalidAlgorithmParameterException e) { 101 } 102 assertNull("getKeyManagers() should return null object", keyMF 103 .getKeyManagers()); 104 105 try { 106 kStore = KeyStore.getInstance(KeyStore.getDefaultType()); 107 kStore.load(null, null); 108 } catch (KeyStoreException e) { 109 fail("default keystore is not supported"); 110 return; 111 } 112 keyMF.init(kStore, pass); 113 114 mfp = (ManagerFactoryParameters) new MyKeyManagerFactorySpi.Parameters(kStore, null); 115 try { 116 keyMF.init(mfp); 117 fail("InvalidAlgorithmParameterException must be thrown"); 118 } catch (InvalidAlgorithmParameterException e) { 119 } 120 mfp = (ManagerFactoryParameters) new MyKeyManagerFactorySpi.Parameters(kStore, pass); 121 keyMF.init(mfp); 122 } 123 /** 124 * Test for <code>getInstance(String algorithm)</code> method 125 * Assertions: 126 * throws NullPointerException when algorithm is null; 127 * throws NoSuchAlgorithmException when algorithm is not correct; 128 * returns KeyManagerFactory object 129 */ 130 public void testGetInstance01() throws Exception { 131 try { 132 KeyManagerFactory.getInstance(null); 133 fail("NoSuchAlgorithmException or NullPointerException should be thrown (algorithm is null"); 134 } catch (NoSuchAlgorithmException e) { 135 } catch (NullPointerException e) { 136 } 137 for (int i = 0; i < invalidValues.length; i++) { 138 try { 139 KeyManagerFactory.getInstance(invalidValues[i]); 140 fail("NoSuchAlgorithmException must be thrown (algorithm: " 141 .concat(invalidValues[i]).concat(")")); 142 } catch (NoSuchAlgorithmException e) { 143 } 144 } 145 KeyManagerFactory keyMF; 146 for (int i = 0; i < validValues.length; i++) { 147 keyMF = KeyManagerFactory.getInstance(validValues[i]); 148 assertTrue("Not instanceof KeyManagerFactory object", 149 keyMF instanceof KeyManagerFactory); 150 assertEquals("Incorrect algorithm", keyMF.getAlgorithm(), 151 validValues[i]); 152 assertEquals("Incorrect provider", keyMF.getProvider(), mProv); 153 checkResult(keyMF); 154 } 155 } 156 157 /** 158 * Test for <code>getInstance(String algorithm, String provider)</code> 159 * method 160 * Assertions: 161 * throws NullPointerException when algorithm is null; 162 * throws NoSuchAlgorithmException when algorithm is not correct; 163 * throws IllegalArgumentException when provider is null or empty; 164 * throws NoSuchProviderException when provider is available; 165 * returns KeyManagerFactory object 166 */ 167 public void testGetInstance02() throws Exception { 168 try { 169 KeyManagerFactory.getInstance(null, mProv.getName()); 170 fail("NoSuchAlgorithmException or NullPointerException should be thrown (algorithm is null"); 171 } catch (NoSuchAlgorithmException e) { 172 } catch (NullPointerException e) { 173 } 174 for (int i = 0; i < invalidValues.length; i++) { 175 try { 176 KeyManagerFactory 177 .getInstance(invalidValues[i], mProv.getName()); 178 fail("NoSuchAlgorithmException must be thrown (algorithm: " 179 .concat(invalidValues[i]).concat(")")); 180 } catch (NoSuchAlgorithmException e) { 181 } 182 } 183 String prov = null; 184 for (int i = 0; i < validValues.length; i++) { 185 try { 186 KeyManagerFactory.getInstance(validValues[i], prov); 187 fail("IllegalArgumentException must be thrown when provider is null (algorithm: " 188 .concat(invalidValues[i]).concat(")")); 189 } catch (IllegalArgumentException e) { 190 } 191 try { 192 KeyManagerFactory.getInstance(validValues[i], ""); 193 fail("IllegalArgumentException must be thrown when provider is empty (algorithm: " 194 .concat(invalidValues[i]).concat(")")); 195 } catch (IllegalArgumentException e) { 196 } 197 } 198 for (int i = 0; i < validValues.length; i++) { 199 for (int j = 1; j < invalidValues.length; j++) { 200 try { 201 KeyManagerFactory.getInstance(validValues[i], 202 invalidValues[j]); 203 fail("NoSuchProviderException must be thrown (algorithm: " 204 .concat(invalidValues[i]).concat(" provider: ") 205 .concat(invalidValues[j]).concat(")")); 206 } catch (NoSuchProviderException e) { 207 } 208 } 209 } 210 KeyManagerFactory keyMF; 211 for (int i = 0; i < validValues.length; i++) { 212 keyMF = KeyManagerFactory.getInstance(validValues[i], mProv 213 .getName()); 214 assertTrue("Not instanceof KeyManagerFactory object", 215 keyMF instanceof KeyManagerFactory); 216 assertEquals("Incorrect algorithm", keyMF.getAlgorithm(), 217 validValues[i]); 218 assertEquals("Incorrect provider", keyMF.getProvider().getName(), 219 mProv.getName()); 220 checkResult(keyMF); 221 } 222 } 223 224 /** 225 * Test for <code>getInstance(String algorithm, Provider provider)</code> 226 * method 227 * Assertions: 228 * throws NullPointerException when algorithm is null; 229 * throws NoSuchAlgorithmException when algorithm is not correct; 230 * throws IllegalArgumentException when provider is null; 231 * returns KeyManagerFactory object 232 */ 233 public void testGetInstance03() throws Exception { 234 try { 235 KeyManagerFactory.getInstance(null, mProv); 236 fail("NoSuchAlgorithmException or NullPointerException should be thrown (algorithm is null"); 237 } catch (NoSuchAlgorithmException e) { 238 } catch (NullPointerException e) { 239 } 240 for (int i = 0; i < invalidValues.length; i++) { 241 try { 242 KeyManagerFactory.getInstance(invalidValues[i], mProv); 243 fail("NoSuchAlgorithmException must be thrown (algorithm: " 244 .concat(invalidValues[i]).concat(")")); 245 } catch (NoSuchAlgorithmException e) { 246 } 247 } 248 Provider prov = null; 249 for (int i = 0; i < validValues.length; i++) { 250 try { 251 KeyManagerFactory.getInstance(validValues[i], prov); 252 fail("IllegalArgumentException must be thrown when provider is null (algorithm: " 253 .concat(invalidValues[i]).concat(")")); 254 } catch (IllegalArgumentException e) { 255 } 256 } 257 KeyManagerFactory keyMF; 258 for (int i = 0; i < validValues.length; i++) { 259 keyMF = KeyManagerFactory.getInstance(validValues[i], mProv); 260 assertTrue("Not instanceof KeyManagerFactory object", 261 keyMF instanceof KeyManagerFactory); 262 assertEquals("Incorrect algorithm", keyMF.getAlgorithm(), 263 validValues[i]); 264 assertEquals("Incorrect provider", keyMF.getProvider(), mProv); 265 checkResult(keyMF); 266 } 267 } 268 }