2 * Copyright (C) 2010 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package libcore.javax.net.ssl;
19 import java.security.KeyManagementException;
20 import java.security.Provider;
21 import libcore.java.security.StandardNames;
22 import javax.net.ServerSocketFactory;
23 import javax.net.SocketFactory;
24 import javax.net.ssl.SSLContext;
25 import javax.net.ssl.SSLEngine;
26 import javax.net.ssl.SSLParameters;
27 import javax.net.ssl.SSLServerSocketFactory;
28 import javax.net.ssl.SSLSessionContext;
29 import javax.net.ssl.SSLSocketFactory;
30 import junit.framework.TestCase;
32 public class SSLContextTest extends TestCase {
34 public void test_SSLContext_getDefault() throws Exception {
35 SSLContext sslContext = SSLContext.getDefault();
36 assertNotNull(sslContext);
38 sslContext.init(null, null, null);
39 } catch (KeyManagementException expected) {
43 public void test_SSLContext_setDefault() throws Exception {
45 SSLContext.setDefault(null);
46 } catch (NullPointerException expected) {
49 SSLContext defaultContext = SSLContext.getDefault();
50 for (String protocol : StandardNames.SSL_CONTEXT_PROTOCOLS) {
51 SSLContext oldContext = SSLContext.getDefault();
52 assertNotNull(oldContext);
53 SSLContext newContext = SSLContext.getInstance(protocol);
54 assertNotNull(newContext);
55 assertNotSame(oldContext, newContext);
56 SSLContext.setDefault(newContext);
57 assertSame(newContext, SSLContext.getDefault());
59 SSLContext.setDefault(defaultContext);
62 public void test_SSLContext_getInstance() throws Exception {
64 SSLContext.getInstance(null);
66 } catch (NullPointerException expected) {
68 for (String protocol : StandardNames.SSL_CONTEXT_PROTOCOLS) {
69 assertNotNull(SSLContext.getInstance(protocol));
70 assertNotSame(SSLContext.getInstance(protocol),
71 SSLContext.getInstance(protocol));
75 SSLContext.getInstance(null, (String) null);
77 } catch (IllegalArgumentException expected) {
80 SSLContext.getInstance(null, "");
82 } catch (IllegalArgumentException expected) {
84 for (String protocol : StandardNames.SSL_CONTEXT_PROTOCOLS) {
86 SSLContext.getInstance(protocol, (String) null);
88 } catch (IllegalArgumentException expected) {
92 SSLContext.getInstance(null, StandardNames.JSSE_PROVIDER_NAME);
94 } catch (NullPointerException expected) {
98 public void test_SSLContext_getProtocol() throws Exception {
99 for (String protocol : StandardNames.SSL_CONTEXT_PROTOCOLS) {
100 String protocolName = SSLContext.getInstance(protocol).getProtocol();
101 assertNotNull(protocolName);
102 assertTrue(protocol.startsWith(protocolName));
106 public void test_SSLContext_getProvider() throws Exception {
107 Provider provider = SSLContext.getDefault().getProvider();
108 assertNotNull(provider);
109 assertEquals(StandardNames.JSSE_PROVIDER_NAME, provider.getName());
112 public void test_SSLContext_init() throws Exception {
113 for (String protocol : StandardNames.SSL_CONTEXT_PROTOCOLS) {
114 SSLContext sslContext = SSLContext.getInstance(protocol);
115 if (protocol.equals(StandardNames.SSL_CONTEXT_PROTOCOLS_DEFAULT)) {
117 sslContext.init(null, null, null);
118 } catch (KeyManagementException expected) {
121 sslContext.init(null, null, null);
126 public void test_SSLContext_getSocketFactory() throws Exception {
127 for (String protocol : StandardNames.SSL_CONTEXT_PROTOCOLS) {
128 if (protocol.equals(StandardNames.SSL_CONTEXT_PROTOCOLS_DEFAULT)) {
129 SSLContext.getInstance(protocol).getSocketFactory();
132 SSLContext.getInstance(protocol).getSocketFactory();
134 } catch (IllegalStateException expected) {
138 SSLContext sslContext = SSLContext.getInstance(protocol);
139 if (!protocol.equals(StandardNames.SSL_CONTEXT_PROTOCOLS_DEFAULT)) {
140 sslContext.init(null, null, null);
142 SocketFactory sf = sslContext.getSocketFactory();
144 assertTrue(SSLSocketFactory.class.isAssignableFrom(sf.getClass()));
148 public void test_SSLContext_getServerSocketFactory() throws Exception {
149 for (String protocol : StandardNames.SSL_CONTEXT_PROTOCOLS) {
150 if (protocol.equals(StandardNames.SSL_CONTEXT_PROTOCOLS_DEFAULT)) {
151 SSLContext.getInstance(protocol).getServerSocketFactory();
154 SSLContext.getInstance(protocol).getServerSocketFactory();
156 } catch (IllegalStateException expected) {
160 SSLContext sslContext = SSLContext.getInstance(protocol);
161 if (!protocol.equals(StandardNames.SSL_CONTEXT_PROTOCOLS_DEFAULT)) {
162 sslContext.init(null, null, null);
164 ServerSocketFactory ssf = sslContext.getServerSocketFactory();
166 assertTrue(SSLServerSocketFactory.class.isAssignableFrom(ssf.getClass()));
170 public void test_SSLContext_createSSLEngine() throws Exception {
171 for (String protocol : StandardNames.SSL_CONTEXT_PROTOCOLS) {
173 if (protocol.equals(StandardNames.SSL_CONTEXT_PROTOCOLS_DEFAULT)) {
174 SSLContext.getInstance(protocol).createSSLEngine();
177 SSLContext.getInstance(protocol).createSSLEngine();
179 } catch (IllegalStateException expected) {
183 if (protocol.equals(StandardNames.SSL_CONTEXT_PROTOCOLS_DEFAULT)) {
184 SSLContext.getInstance(protocol).createSSLEngine(null, -1);
187 SSLContext.getInstance(protocol).createSSLEngine(null, -1);
189 } catch (IllegalStateException expected) {
194 SSLContext sslContext = SSLContext.getInstance(protocol);
195 if (!protocol.equals(StandardNames.SSL_CONTEXT_PROTOCOLS_DEFAULT)) {
196 sslContext.init(null, null, null);
198 SSLEngine se = sslContext.createSSLEngine();
203 SSLContext sslContext = SSLContext.getInstance(protocol);
204 if (!protocol.equals(StandardNames.SSL_CONTEXT_PROTOCOLS_DEFAULT)) {
205 sslContext.init(null, null, null);
207 SSLEngine se = sslContext.createSSLEngine(null, -1);
213 public void test_SSLContext_getServerSessionContext() throws Exception {
214 for (String protocol : StandardNames.SSL_CONTEXT_PROTOCOLS) {
215 SSLContext sslContext = SSLContext.getInstance(protocol);
216 SSLSessionContext sessionContext = sslContext.getServerSessionContext();
217 assertNotNull(sessionContext);
219 if (protocol.equals(StandardNames.SSL_CONTEXT_PROTOCOLS_DEFAULT)) {
220 assertSame(SSLContext.getInstance(protocol).getServerSessionContext(),
223 assertNotSame(SSLContext.getInstance(protocol).getServerSessionContext(),
229 public void test_SSLContext_getClientSessionContext() throws Exception {
230 for (String protocol : StandardNames.SSL_CONTEXT_PROTOCOLS) {
231 SSLContext sslContext = SSLContext.getInstance(protocol);
232 SSLSessionContext sessionContext = sslContext.getClientSessionContext();
233 assertNotNull(sessionContext);
235 if (protocol.equals(StandardNames.SSL_CONTEXT_PROTOCOLS_DEFAULT)) {
236 assertSame(SSLContext.getInstance(protocol).getClientSessionContext(),
239 assertNotSame(SSLContext.getInstance(protocol).getClientSessionContext(),
245 public void test_SSLContext_getDefaultSSLParameters() throws Exception {
246 for (String protocol : StandardNames.SSL_CONTEXT_PROTOCOLS) {
247 SSLContext sslContext = SSLContext.getInstance(protocol);
248 if (!protocol.equals(StandardNames.SSL_CONTEXT_PROTOCOLS_DEFAULT)) {
249 sslContext.init(null, null, null);
252 SSLParameters p = sslContext.getDefaultSSLParameters();
255 String[] cipherSuites = p.getCipherSuites();
256 assertNotNull(cipherSuites);
257 StandardNames.assertValidCipherSuites(StandardNames.CIPHER_SUITES, cipherSuites);
259 String[] protocols = p.getProtocols();
260 assertNotNull(protocols);
261 StandardNames.assertValidCipherSuites(StandardNames.SSL_SOCKET_PROTOCOLS, protocols);
263 assertFalse(p.getWantClientAuth());
264 assertFalse(p.getNeedClientAuth());
268 public void test_SSLContext_getSupportedSSLParameters() throws Exception {
269 for (String protocol : StandardNames.SSL_CONTEXT_PROTOCOLS) {
270 SSLContext sslContext = SSLContext.getInstance(protocol);
271 if (!protocol.equals(StandardNames.SSL_CONTEXT_PROTOCOLS_DEFAULT)) {
272 sslContext.init(null, null, null);
275 SSLParameters p = sslContext.getSupportedSSLParameters();
278 String[] cipherSuites = p.getCipherSuites();
279 assertNotNull(cipherSuites);
280 StandardNames.assertSupportedCipherSuites(StandardNames.CIPHER_SUITES, cipherSuites);
282 String[] protocols = p.getProtocols();
283 assertNotNull(protocols);
284 StandardNames.assertSupportedProtocols(StandardNames.SSL_SOCKET_PROTOCOLS,
287 assertFalse(p.getWantClientAuth());
288 assertFalse(p.getNeedClientAuth());
292 public void test_SSLContextTest_TestSSLContext_create() {
293 TestSSLContext testContext = TestSSLContext.create();
294 assertNotNull(testContext);
295 assertNotNull(testContext.clientKeyStore);
296 assertNull(testContext.clientStorePassword);
297 assertNotNull(testContext.serverKeyStore);
298 assertNull(testContext.serverStorePassword);
299 assertNotNull(testContext.clientKeyManager);
300 assertNotNull(testContext.serverKeyManager);
301 assertNotNull(testContext.clientTrustManager);
302 assertNotNull(testContext.serverTrustManager);
303 assertNotNull(testContext.clientContext);
304 assertNotNull(testContext.serverContext);
305 assertNotNull(testContext.serverSocket);
306 assertNotNull(testContext.host);
307 assertTrue(testContext.port != 0);