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
17package libcore.javax.net.ssl;
18
19import libcore.java.security.StandardNames;
20import libcore.java.security.TestKeyStore;
21import java.util.Arrays;
22import javax.net.ssl.SSLPeerUnverifiedException;
23import junit.framework.TestCase;
24
25public class SSLSessionTest extends TestCase {
26
27    public void test_SSLSocket_TestSSLSessions_create() {
28        TestSSLSessions s = TestSSLSessions.create();
29        assertNotNull(s.invalid);
30        assertFalse(s.invalid.isValid());
31        assertTrue(s.server.isValid());
32        assertTrue(s.client.isValid());
33        s.close();
34    }
35
36    public void test_SSLSession_getApplicationBufferSize() {
37        TestSSLSessions s = TestSSLSessions.create();
38        assertTrue(s.invalid.getApplicationBufferSize() > 0);
39        assertTrue(s.server.getApplicationBufferSize() > 0);
40        assertTrue(s.client.getApplicationBufferSize() > 0);
41        s.close();
42    }
43
44    public void test_SSLSession_getCipherSuite() {
45        TestSSLSessions s = TestSSLSessions.create();
46        assertNotNull(s.invalid.getCipherSuite());
47        assertEquals(StandardNames.CIPHER_SUITE_INVALID, s.invalid.getCipherSuite());
48        assertNotNull(s.server.getCipherSuite());
49        assertNotNull(s.client.getCipherSuite());
50        assertEquals(s.server.getCipherSuite(),
51                     s.client.getCipherSuite());
52        assertTrue(StandardNames.CIPHER_SUITES.contains(s.server.getCipherSuite()));
53        s.close();
54    }
55
56    public void test_SSLSession_getCreationTime() {
57        // We use OpenSSL, which only returns times accurate to the nearest second.
58        // NativeCrypto just multiplies by 1000, which looks like truncation, which
59        // would make it appear as if the OpenSSL side of things was created before
60        // we called it.
61        long t0 = System.currentTimeMillis() / 1000;
62        TestSSLSessions s = TestSSLSessions.create();
63        long t1 = System.currentTimeMillis() / 1000;
64
65        assertTrue(s.invalid.getCreationTime() > 0);
66
67        long sTime = s.server.getCreationTime() / 1000;
68        assertTrue(sTime + " >= " + t0, sTime >= t0);
69        assertTrue(sTime + " <= " + t1, sTime <= t1);
70
71        long cTime = s.client.getCreationTime() / 1000;
72        assertTrue(cTime + " >= " + t0, cTime >= t0);
73        assertTrue(cTime + " <= " + t1, cTime <= t1);
74
75        s.close();
76    }
77
78    public void test_SSLSession_getId() {
79        TestSSLSessions s = TestSSLSessions.create();
80        assertNotNull(s.invalid.getId());
81        assertNotNull(s.server.getId());
82        assertNotNull(s.client.getId());
83        assertEquals(0, s.invalid.getId().length);
84        if (TestSSLContext.sslServerSocketSupportsSessionTickets()) {
85            assertEquals(0, s.server.getId().length);
86        } else {
87            assertEquals(32, s.server.getId().length);
88            assertTrue(Arrays.equals(s.server.getId(), s.client.getId()));
89        }
90        assertEquals(32, s.client.getId().length);
91        s.close();
92    }
93
94    public void test_SSLSession_getLastAccessedTime() {
95        TestSSLSessions s = TestSSLSessions.create();
96        assertTrue(s.invalid.getLastAccessedTime() > 0);
97        assertTrue(s.server.getLastAccessedTime() > 0);
98        assertTrue(s.client.getLastAccessedTime() > 0);
99        assertTrue("s.server.getLastAccessedTime()=" + s.server.getLastAccessedTime() + " " +
100                   "s.client.getLastAccessedTime()=" + s.client.getLastAccessedTime(),
101                   Math.abs(s.server.getLastAccessedTime()
102                            - s.client.getLastAccessedTime()) < 1 * 1000);
103        assertTrue(s.server.getLastAccessedTime() >=
104                   s.server.getCreationTime());
105        assertTrue(s.client.getLastAccessedTime() >=
106                   s.client.getCreationTime());
107        s.close();
108    }
109
110    public void test_SSLSession_getLocalCertificates() throws Exception {
111        TestSSLSessions s = TestSSLSessions.create();
112        assertNull(s.invalid.getLocalCertificates());
113        assertNull(s.client.getLocalCertificates());
114        assertNotNull(s.server.getLocalCertificates());
115        TestKeyStore.assertChainLength(s.server.getLocalCertificates());
116        TestSSLContext.assertServerCertificateChain(s.s.c.serverTrustManager,
117                                                    s.server.getLocalCertificates());
118        TestSSLContext.assertCertificateInKeyStore(s.server.getLocalCertificates()[0],
119                                                   s.s.c.serverKeyStore);
120        s.close();
121    }
122
123    public void test_SSLSession_getLocalPrincipal() throws Exception {
124        TestSSLSessions s = TestSSLSessions.create();
125        assertNull(s.invalid.getLocalPrincipal());
126        assertNull(s.client.getLocalPrincipal());
127        assertNotNull(s.server.getLocalPrincipal());
128        assertNotNull(s.server.getLocalPrincipal().getName());
129        TestSSLContext.assertCertificateInKeyStore(s.server.getLocalPrincipal(),
130                                                   s.s.c.serverKeyStore);
131        s.close();
132    }
133
134    public void test_SSLSession_getPacketBufferSize() {
135        TestSSLSessions s = TestSSLSessions.create();
136        assertTrue(s.invalid.getPacketBufferSize() > 0);
137        assertTrue(s.server.getPacketBufferSize() > 0);
138        assertTrue(s.client.getPacketBufferSize() > 0);
139        s.close();
140    }
141
142    public void test_SSLSession_getPeerCertificateChain() throws Exception {
143        TestSSLSessions s = TestSSLSessions.create();
144        try {
145            s.invalid.getPeerCertificateChain();
146            fail();
147        } catch (SSLPeerUnverifiedException expected) {
148        }
149        assertNotNull(s.client.getPeerCertificates());
150        TestKeyStore.assertChainLength(s.client.getPeerCertificateChain());
151        try {
152            assertNull(s.server.getPeerCertificateChain());
153            fail();
154        } catch (SSLPeerUnverifiedException expected) {
155        }
156        s.close();
157    }
158
159    public void test_SSLSession_getPeerCertificates() throws Exception {
160        TestSSLSessions s = TestSSLSessions.create();
161        try {
162            s.invalid.getPeerCertificates();
163            fail();
164        } catch (SSLPeerUnverifiedException expected) {
165        }
166        assertNotNull(s.client.getPeerCertificates());
167        TestKeyStore.assertChainLength(s.client.getPeerCertificates());
168        TestSSLContext.assertServerCertificateChain(s.s.c.serverTrustManager,
169                                                    s.client.getPeerCertificates());
170        TestSSLContext.assertCertificateInKeyStore(s.client.getPeerCertificates()[0],
171                                                   s.s.c.serverKeyStore);
172        try {
173            s.server.getPeerCertificates();
174            fail();
175        } catch (SSLPeerUnverifiedException expected) {
176        }
177        s.close();
178    }
179
180    public void test_SSLSession_getPeerHost() {
181        TestSSLSessions s = TestSSLSessions.create();
182        assertNull(s.invalid.getPeerHost());
183        assertNotNull(s.server.getPeerHost());
184        assertNotNull(s.client.getPeerHost());
185        s.close();
186    }
187
188    public void test_SSLSession_getPeerPort() {
189        TestSSLSessions s = TestSSLSessions.create();
190        assertEquals(-1, s.invalid.getPeerPort());
191        assertTrue(s.server.getPeerPort() > 0);
192        assertEquals(s.s.c.port, s.client.getPeerPort());
193        s.close();
194    }
195
196    public void test_SSLSession_getPeerPrincipal() throws Exception {
197        TestSSLSessions s = TestSSLSessions.create();
198        try {
199            s.invalid.getPeerPrincipal();
200            fail();
201        } catch (SSLPeerUnverifiedException expected) {
202        }
203        try {
204            s.server.getPeerPrincipal();
205            fail();
206        } catch (SSLPeerUnverifiedException expected) {
207        }
208        assertNotNull(s.client.getPeerPrincipal());
209        assertNotNull(s.client.getPeerPrincipal().getName());
210        TestSSLContext.assertCertificateInKeyStore(s.client.getPeerPrincipal(),
211                                                   s.s.c.serverKeyStore);
212        s.close();
213    }
214
215    public void test_SSLSession_getProtocol() {
216        TestSSLSessions s = TestSSLSessions.create();
217        assertNotNull(s.invalid.getProtocol());
218        assertEquals("NONE", s.invalid.getProtocol());
219        assertNotNull(s.server.getProtocol());
220        assertNotNull(s.client.getProtocol());
221        assertEquals(s.server.getProtocol(),
222                     s.client.getProtocol());
223        assertTrue(StandardNames.SSL_SOCKET_PROTOCOLS.contains(s.server.getProtocol()));
224        s.close();
225    }
226
227    public void test_SSLSession_getSessionContext() {
228        TestSSLSessions s = TestSSLSessions.create();
229        assertNull(s.invalid.getSessionContext());
230        assertNotNull(s.server.getSessionContext());
231        assertNotNull(s.client.getSessionContext());
232        assertEquals(s.s.c.serverContext.getServerSessionContext(),
233                     s.server.getSessionContext());
234        assertEquals(s.s.c.clientContext.getClientSessionContext(),
235                     s.client.getSessionContext());
236        assertNotSame(s.server.getSessionContext(),
237                      s.client.getSessionContext());
238        s.close();
239    }
240
241    public void test_SSLSession_getValue() {
242        TestSSLSessions s = TestSSLSessions.create();
243        try {
244            s.invalid.getValue(null);
245        } catch (IllegalArgumentException expected) {
246        }
247        assertNull(s.invalid.getValue("BOGUS"));
248        s.close();
249    }
250
251    public void test_SSLSession_getValueNames() {
252        TestSSLSessions s = TestSSLSessions.create();
253        assertNotNull(s.invalid.getValueNames());
254        assertEquals(0, s.invalid.getValueNames().length);
255        s.close();
256    }
257
258    public void test_SSLSession_invalidate() {
259        TestSSLSessions s = TestSSLSessions.create();
260
261        assertFalse(s.invalid.isValid());
262        s.invalid.invalidate();
263        assertFalse(s.invalid.isValid());
264        assertNull(s.invalid.getSessionContext());
265
266        assertTrue(s.server.isValid());
267        s.server.invalidate();
268        assertFalse(s.server.isValid());
269        assertNull(s.server.getSessionContext());
270
271        assertTrue(s.client.isValid());
272        s.client.invalidate();
273        assertFalse(s.client.isValid());
274        assertNull(s.client.getSessionContext());
275
276        s.close();
277    }
278
279    public void test_SSLSession_isValid() {
280        TestSSLSessions s = TestSSLSessions.create();
281        assertFalse(s.invalid.isValid());
282        assertTrue(s.server.isValid());
283        assertTrue(s.client.isValid());
284        s.close();
285    }
286
287    public void test_SSLSession_putValue() {
288        TestSSLSessions s = TestSSLSessions.create();
289        String key = "KEY";
290        String value = "VALUE";
291        assertNull(s.invalid.getValue(key));
292        assertEquals(0, s.invalid.getValueNames().length);
293        s.invalid.putValue(key, value);
294        assertSame(value, s.invalid.getValue(key));
295        assertEquals(1, s.invalid.getValueNames().length);
296        assertEquals(key, s.invalid.getValueNames()[0]);
297        s.close();
298    }
299
300    public void test_SSLSession_removeValue() {
301        TestSSLSessions s = TestSSLSessions.create();
302        String key = "KEY";
303        String value = "VALUE";
304        s.invalid.putValue(key, value);
305        assertEquals(1, s.invalid.getValueNames().length);
306        assertEquals(key, s.invalid.getValueNames()[0]);
307        s.invalid.removeValue(key);
308        assertNull(s.invalid.getValue(key));
309        assertEquals(0, s.invalid.getValueNames().length);
310        s.close();
311    }
312}
313