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 18package tests.api.javax.net.ssl; 19 20import java.security.KeyManagementException; 21import java.security.NoSuchAlgorithmException; 22import java.security.NoSuchProviderException; 23import java.security.Provider; 24import java.security.SecureRandom; 25import java.security.Security; 26import javax.net.ssl.KeyManager; 27import javax.net.ssl.SSLContext; 28import javax.net.ssl.SSLEngine; 29import javax.net.ssl.TrustManager; 30import junit.framework.TestCase; 31import org.apache.harmony.security.tests.support.SpiEngUtils; 32import org.apache.harmony.xnet.tests.support.MySSLContextSpi; 33 34/** 35 * Tests for SSLContext class constructors and methods 36 * 37 */ 38public class SSLContext2Test extends TestCase { 39 40 private static String srvSSLContext = "SSLContext"; 41 42 private static final String defaultProtocol = "S+S+L"; 43 44 public static final String SSLContextProviderClass = MySSLContextSpi.class.getName(); 45 46 private static final String[] invalidValues = SpiEngUtils.invalidValues; 47 48 private static final String[] validValues; 49 static { 50 validValues = new String[4]; 51 validValues[0] = defaultProtocol; 52 validValues[1] = defaultProtocol.toLowerCase(); 53 validValues[2] = "s+S+L"; 54 validValues[3] = "S+s+L"; 55 } 56 57 Provider mProv; 58 59 protected void setUp() throws Exception { 60 super.setUp(); 61 mProv = (new SpiEngUtils()).new MyProvider("MySSLContextProvider", "Provider for testing", 62 srvSSLContext.concat(".").concat(defaultProtocol), 63 SSLContextProviderClass); 64 Security.insertProviderAt(mProv, 1); 65 } 66 67 /* 68 * @see TestCase#tearDown() 69 */ 70 protected void tearDown() throws Exception { 71 super.tearDown(); 72 Security.removeProvider(mProv.getName()); 73 } 74 75 private void checkSSLContext(SSLContext sslC) 76 throws KeyManagementException { 77 78 try { 79 sslC.getSocketFactory(); 80 fail("RuntimeException must be thrown"); 81 } catch (RuntimeException e) { 82 assertEquals("Incorrect message", "Not initialiazed", e.getMessage()); 83 } 84 try { 85 sslC.getServerSocketFactory(); 86 fail("RuntimeException must be thrown"); 87 } catch (RuntimeException e) { 88 assertEquals("Incorrect message", "Not initialiazed", e.getMessage()); 89 } 90 try { 91 sslC.getServerSessionContext(); 92 fail("RuntimeException must be thrown"); 93 } catch (RuntimeException e) { 94 assertEquals("Incorrect message", "Not initialiazed", e.getMessage()); 95 } 96 try { 97 sslC.getClientSessionContext(); 98 fail("RuntimeException must be thrown"); 99 } catch (RuntimeException e) { 100 assertEquals("Incorrect message", "Not initialiazed", e.getMessage()); 101 } 102 try { 103 sslC.createSSLEngine(); 104 fail("RuntimeException must be thrown"); 105 } catch (RuntimeException e) { 106 assertEquals("Incorrect message", "Not initialiazed", e.getMessage()); 107 } 108 try { 109 sslC.createSSLEngine("host",1); 110 fail("RuntimeException must be thrown"); 111 } catch (RuntimeException e) { 112 assertEquals("Incorrect message", "Not initialiazed", e.getMessage()); 113 } 114 TrustManager [] tm = new TManager[10]; 115 KeyManager [] km = new KManager[5]; 116 try { 117 sslC.init(km, tm, null); 118 fail("KeyManagementException must be thrown"); 119 } catch (KeyManagementException e) { 120 } 121 sslC.init(km, tm, new SecureRandom()); 122 123 SSLEngine sslE = sslC.createSSLEngine(); 124 assertTrue("Not null result",sslE instanceof SSLEngine); 125 assertNull("Incorrect host", sslE.getPeerHost()); 126 assertEquals("Incorrect port", 0, sslE.getPeerPort()); 127 String host = "ZZZ"; 128 int port = 8080; 129 sslE = sslC.createSSLEngine(host, port); 130 assertTrue("Not null result",sslE instanceof SSLEngine); 131 assertEquals("Incorrect host", sslE.getPeerHost(), host); 132 assertEquals("Incorrect port", sslE.getPeerPort(), port); 133 try { 134 assertNull("Not null result", sslC.getServerSessionContext()); 135 } catch (NullPointerException e) { 136 } 137 try { 138 assertNull("Not null result", sslC.getClientSessionContext()); 139 } catch (NullPointerException e) { 140 } 141 } 142 143 /** 144 * Test for <code>getInstance(String protocol)</code> method 145 * Assertions: 146 * throws NullPointerException when protocol is null; 147 * throws NoSuchAlgorithmException when protocol is not correct; 148 * returns SSLContext object 149 */ 150 public void test_getInstanceLjava_lang_String() throws NoSuchAlgorithmException, 151 KeyManagementException { 152 try { 153 SSLContext.getInstance(null); 154 fail("NoSuchAlgorithmException or NullPointerException should be thrown " 155 + "(protocol is null)"); 156 } catch (NoSuchAlgorithmException e) { 157 } catch (NullPointerException e) { 158 } 159 for (int i = 0; i < invalidValues.length; i++) { 160 try { 161 SSLContext.getInstance(invalidValues[i]); 162 fail("NoSuchAlgorithmException must be thrown (protocol: " 163 .concat(invalidValues[i]).concat(")")); 164 } catch (NoSuchAlgorithmException e) { 165 } 166 } 167 SSLContext sslC; 168 for (int i = 0; i < validValues.length; i++) { 169 sslC = SSLContext.getInstance(validValues[i]); 170 assertTrue("Not instanceof SSLContext object", sslC instanceof SSLContext); 171 assertEquals("Incorrect protocol", sslC.getProtocol(), validValues[i]); 172 assertEquals("Incorrect provider", sslC.getProvider(), mProv); 173 checkSSLContext(sslC); 174 } 175 } 176 177 /** 178 * Test for <code>getInstance(String protocol, String provider)</code> 179 * method 180 * Assertions: 181 * throws NullPointerException when protocol is null; 182 * throws NoSuchAlgorithmException when protocol is not correct; 183 * throws IllegalArgumentException when provider is null or empty; 184 * throws NoSuchProviderException when provider is available; 185 * returns SSLContext object 186 */ 187 public void test_getInstanceLjava_lang_StringLjava_lang_String() 188 throws NoSuchAlgorithmException, NoSuchProviderException, 189 IllegalArgumentException, KeyManagementException { 190 try { 191 SSLContext.getInstance(null, mProv.getName()); 192 fail("NoSuchAlgorithmException or NullPointerException should be thrown " 193 + "(protocol is null)"); 194 } catch (NoSuchAlgorithmException e) { 195 } catch (NullPointerException e) { 196 } 197 for (int i = 0; i < invalidValues.length; i++) { 198 try { 199 SSLContext.getInstance(invalidValues[i], mProv.getName()); 200 fail("NoSuchAlgorithmException must be thrown (protocol: " 201 .concat(invalidValues[i]).concat(")")); 202 } catch (NoSuchAlgorithmException e) { 203 } 204 } 205 String prov = null; 206 for (int i = 0; i < validValues.length; i++) { 207 try { 208 SSLContext.getInstance(validValues[i], prov); 209 fail("IllegalArgumentException must be thrown when provider is null (protocol: " 210 .concat(invalidValues[i]).concat(")")); 211 } catch (IllegalArgumentException e) { 212 } 213 try { 214 SSLContext.getInstance(validValues[i], ""); 215 fail("IllegalArgumentException must be thrown when provider is empty (protocol: " 216 .concat(invalidValues[i]).concat(")")); 217 } catch (IllegalArgumentException e) { 218 } 219 } 220 for (int i = 0; i < validValues.length; i++) { 221 for (int j = 1; j < invalidValues.length; j++) { 222 try { 223 SSLContext.getInstance(validValues[i], invalidValues[j]); 224 fail("NoSuchProviderException must be thrown (protocol: " 225 .concat(invalidValues[i]).concat(" provider: ") 226 .concat(invalidValues[j]).concat(")")); 227 } catch (NoSuchProviderException e) { 228 } 229 } 230 } 231 SSLContext sslC; 232 for (int i = 0; i < validValues.length; i++) { 233 sslC = SSLContext.getInstance(validValues[i], mProv.getName()); 234 assertTrue("Not instanceof SSLContext object", sslC instanceof SSLContext); 235 assertEquals("Incorrect protocol", sslC.getProtocol(), validValues[i]); 236 assertEquals("Incorrect provider", sslC.getProvider().getName(), mProv.getName()); 237 checkSSLContext(sslC); 238 } 239 } 240 241 /** 242 * Test for <code>getInstance(String protocol, Provider provider)</code> 243 * method 244 * Assertions: 245 * throws NullPointerException when protocol is null; 246 * throws NoSuchAlgorithmException when protocol is not correct; 247 * throws IllegalArgumentException when provider is null; 248 * returns SSLContext object 249 */ 250 public void test_getInstanceLjava_lang_StringLjava_security_Provider() 251 throws NoSuchAlgorithmException, 252 IllegalArgumentException, KeyManagementException { 253 try { 254 SSLContext.getInstance(null, mProv); 255 fail("NoSuchAlgorithmException or NullPointerException should be thrown " 256 + "(protocol is null)"); 257 } catch (NoSuchAlgorithmException e) { 258 } catch (NullPointerException e) { 259 } 260 for (int i = 0; i < invalidValues.length; i++) { 261 try { 262 SSLContext.getInstance(invalidValues[i], mProv); 263 fail("NoSuchAlgorithmException must be thrown (protocol: " 264 .concat(invalidValues[i]).concat(")")); 265 } catch (NoSuchAlgorithmException e) { 266 } 267 } 268 Provider prov = null; 269 for (int i = 0; i < validValues.length; i++) { 270 try { 271 SSLContext.getInstance(validValues[i], prov); 272 fail("IllegalArgumentException must be thrown when provider is null (protocol: " 273 .concat(invalidValues[i]).concat(")")); 274 } catch (IllegalArgumentException e) { 275 } 276 } 277 SSLContext sslC; 278 for (int i = 0; i < validValues.length; i++) { 279 sslC = SSLContext.getInstance(validValues[i], mProv); 280 assertTrue("Not instanceof SSLContext object", sslC instanceof SSLContext); 281 assertEquals("Incorrect protocol", sslC.getProtocol(), validValues[i]); 282 assertEquals("Incorrect provider", sslC.getProvider(), mProv); 283 checkSSLContext(sslC); 284 } 285 } 286 287 class TManager implements TrustManager { 288 289 } 290 class KManager implements KeyManager { 291 292 } 293} 294