OSDN Git Service

original
[gb-231r1-is01/Gingerbread_2.3.3_r1_IS01.git] / libcore / luni / src / test / java / libcore / javax / net / ssl / SSLContextTest.java
1 /*
2  * Copyright (C) 2010 The Android Open Source Project
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package libcore.javax.net.ssl;
18
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;
31
32 public class SSLContextTest extends TestCase {
33
34     public void test_SSLContext_getDefault() throws Exception {
35         SSLContext sslContext = SSLContext.getDefault();
36         assertNotNull(sslContext);
37         try {
38             sslContext.init(null, null, null);
39         } catch (KeyManagementException expected) {
40         }
41     }
42
43     public void test_SSLContext_setDefault() throws Exception {
44         try {
45             SSLContext.setDefault(null);
46         } catch (NullPointerException expected) {
47         }
48
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());
58         }
59         SSLContext.setDefault(defaultContext);
60     }
61
62     public void test_SSLContext_getInstance() throws Exception {
63         try {
64             SSLContext.getInstance(null);
65             fail();
66         } catch (NullPointerException expected) {
67         }
68         for (String protocol : StandardNames.SSL_CONTEXT_PROTOCOLS) {
69             assertNotNull(SSLContext.getInstance(protocol));
70             assertNotSame(SSLContext.getInstance(protocol),
71                           SSLContext.getInstance(protocol));
72         }
73
74         try {
75             SSLContext.getInstance(null, (String) null);
76             fail();
77         } catch (IllegalArgumentException expected) {
78         }
79         try {
80             SSLContext.getInstance(null, "");
81             fail();
82         } catch (IllegalArgumentException expected) {
83         }
84         for (String protocol : StandardNames.SSL_CONTEXT_PROTOCOLS) {
85             try {
86                 SSLContext.getInstance(protocol, (String) null);
87                 fail();
88             } catch (IllegalArgumentException expected) {
89             }
90         }
91         try {
92             SSLContext.getInstance(null, StandardNames.JSSE_PROVIDER_NAME);
93             fail();
94         } catch (NullPointerException expected) {
95         }
96     }
97
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));
103         }
104     }
105
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());
110     }
111
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)) {
116                 try {
117                     sslContext.init(null, null, null);
118                 } catch (KeyManagementException expected) {
119                 }
120             } else {
121                 sslContext.init(null, null, null);
122             }
123         }
124     }
125
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();
130             } else {
131                 try {
132                     SSLContext.getInstance(protocol).getSocketFactory();
133                     fail();
134                 } catch (IllegalStateException expected) {
135                 }
136             }
137
138             SSLContext sslContext = SSLContext.getInstance(protocol);
139             if (!protocol.equals(StandardNames.SSL_CONTEXT_PROTOCOLS_DEFAULT)) {
140                 sslContext.init(null, null, null);
141             }
142             SocketFactory sf = sslContext.getSocketFactory();
143             assertNotNull(sf);
144             assertTrue(SSLSocketFactory.class.isAssignableFrom(sf.getClass()));
145         }
146     }
147
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();
152             } else {
153                 try {
154                     SSLContext.getInstance(protocol).getServerSocketFactory();
155                     fail();
156                 } catch (IllegalStateException expected) {
157                 }
158             }
159
160             SSLContext sslContext = SSLContext.getInstance(protocol);
161             if (!protocol.equals(StandardNames.SSL_CONTEXT_PROTOCOLS_DEFAULT)) {
162                 sslContext.init(null, null, null);
163             }
164             ServerSocketFactory ssf = sslContext.getServerSocketFactory();
165             assertNotNull(ssf);
166             assertTrue(SSLServerSocketFactory.class.isAssignableFrom(ssf.getClass()));
167         }
168     }
169
170     public void test_SSLContext_createSSLEngine() throws Exception {
171         for (String protocol : StandardNames.SSL_CONTEXT_PROTOCOLS) {
172
173             if (protocol.equals(StandardNames.SSL_CONTEXT_PROTOCOLS_DEFAULT)) {
174                 SSLContext.getInstance(protocol).createSSLEngine();
175             } else {
176                 try {
177                     SSLContext.getInstance(protocol).createSSLEngine();
178                     fail();
179                 } catch (IllegalStateException expected) {
180                 }
181             }
182
183             if (protocol.equals(StandardNames.SSL_CONTEXT_PROTOCOLS_DEFAULT)) {
184                 SSLContext.getInstance(protocol).createSSLEngine(null, -1);
185             } else {
186                 try {
187                     SSLContext.getInstance(protocol).createSSLEngine(null, -1);
188                     fail();
189                 } catch (IllegalStateException expected) {
190                 }
191             }
192
193             {
194                 SSLContext sslContext = SSLContext.getInstance(protocol);
195                 if (!protocol.equals(StandardNames.SSL_CONTEXT_PROTOCOLS_DEFAULT)) {
196                     sslContext.init(null, null, null);
197                 }
198                 SSLEngine se = sslContext.createSSLEngine();
199                 assertNotNull(se);
200             }
201
202             {
203                 SSLContext sslContext = SSLContext.getInstance(protocol);
204                 if (!protocol.equals(StandardNames.SSL_CONTEXT_PROTOCOLS_DEFAULT)) {
205                     sslContext.init(null, null, null);
206                 }
207                 SSLEngine se = sslContext.createSSLEngine(null, -1);
208                 assertNotNull(se);
209             }
210         }
211     }
212
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);
218
219             if (protocol.equals(StandardNames.SSL_CONTEXT_PROTOCOLS_DEFAULT)) {
220                 assertSame(SSLContext.getInstance(protocol).getServerSessionContext(),
221                            sessionContext);
222             } else {
223                 assertNotSame(SSLContext.getInstance(protocol).getServerSessionContext(),
224                               sessionContext);
225             }
226         }
227     }
228
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);
234
235             if (protocol.equals(StandardNames.SSL_CONTEXT_PROTOCOLS_DEFAULT)) {
236                 assertSame(SSLContext.getInstance(protocol).getClientSessionContext(),
237                            sessionContext);
238             } else {
239                 assertNotSame(SSLContext.getInstance(protocol).getClientSessionContext(),
240                               sessionContext);
241             }
242         }
243     }
244
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);
250             }
251
252             SSLParameters p = sslContext.getDefaultSSLParameters();
253             assertNotNull(p);
254
255             String[] cipherSuites = p.getCipherSuites();
256             assertNotNull(cipherSuites);
257             StandardNames.assertValidCipherSuites(StandardNames.CIPHER_SUITES, cipherSuites);
258
259             String[] protocols = p.getProtocols();
260             assertNotNull(protocols);
261             StandardNames.assertValidCipherSuites(StandardNames.SSL_SOCKET_PROTOCOLS, protocols);
262
263             assertFalse(p.getWantClientAuth());
264             assertFalse(p.getNeedClientAuth());
265         }
266     }
267
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);
273             }
274
275             SSLParameters p = sslContext.getSupportedSSLParameters();
276             assertNotNull(p);
277
278             String[] cipherSuites = p.getCipherSuites();
279             assertNotNull(cipherSuites);
280             StandardNames.assertSupportedCipherSuites(StandardNames.CIPHER_SUITES, cipherSuites);
281
282             String[] protocols = p.getProtocols();
283             assertNotNull(protocols);
284             StandardNames.assertSupportedProtocols(StandardNames.SSL_SOCKET_PROTOCOLS,
285                                                    protocols);
286
287             assertFalse(p.getWantClientAuth());
288             assertFalse(p.getNeedClientAuth());
289         }
290     }
291
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);
308     }
309 }