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