1 /*
2  * Copyright (C) 2013 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 android.net;
18 
19 import com.android.org.conscrypt.ClientSessionContext;
20 import com.android.org.conscrypt.SSLClientSessionCache;
21 
22 import com.google.testing.littlemock.LittleMock;
23 
24 import junit.framework.TestCase;
25 
26 import java.security.KeyManagementException;
27 import java.security.SecureRandom;
28 
29 import javax.net.ssl.KeyManager;
30 import javax.net.ssl.SSLContext;
31 import javax.net.ssl.SSLContextSpi;
32 import javax.net.ssl.SSLEngine;
33 import javax.net.ssl.SSLServerSocketFactory;
34 import javax.net.ssl.SSLSessionContext;
35 import javax.net.ssl.SSLSocketFactory;
36 import javax.net.ssl.TrustManager;
37 
38 public class SSLSessionCacheTest extends TestCase {
39 
testInstall_compatibleContext()40     public void testInstall_compatibleContext() throws Exception {
41         final SSLContext ctx = SSLContext.getDefault();
42         final SSLClientSessionCache mock = LittleMock.mock(SSLClientSessionCache.class);
43         final ClientSessionContext clientCtx = (ClientSessionContext) ctx.getClientSessionContext();
44 
45         try {
46             SSLSessionCache.install(new SSLSessionCache(mock), ctx);
47             clientCtx.getSession("www.foogle.com", 443);
48             LittleMock.verify(mock).getSessionData(LittleMock.anyString(), LittleMock.anyInt());
49         } finally {
50             // Restore cacheless behaviour.
51             SSLSessionCache.install(null, ctx);
52             clientCtx.getSession("www.foogle.com", 443);
53             LittleMock.verifyNoMoreInteractions(mock);
54         }
55     }
56 
testInstall_incompatibleContext()57     public void testInstall_incompatibleContext() {
58         try {
59             SSLSessionCache.install(
60                     new SSLSessionCache(LittleMock.mock(SSLClientSessionCache.class)),
61                     new FakeSSLContext());
62             fail();
63         } catch (IllegalArgumentException expected) {}
64     }
65 
66     static final class FakeSSLContext extends SSLContext {
FakeSSLContext()67         protected FakeSSLContext() {
68             super(new FakeSSLContextSpi(), null, "test");
69         }
70     }
71 
72     static final class FakeSSLContextSpi extends SSLContextSpi {
73         @Override
engineInit(KeyManager[] keyManagers, TrustManager[] trustManagers, SecureRandom secureRandom)74         protected void engineInit(KeyManager[] keyManagers, TrustManager[] trustManagers,
75                 SecureRandom secureRandom) throws KeyManagementException {
76         }
77 
78         @Override
engineGetSocketFactory()79         protected SSLSocketFactory engineGetSocketFactory() {
80             return null;
81         }
82 
83         @Override
engineGetServerSocketFactory()84         protected SSLServerSocketFactory engineGetServerSocketFactory() {
85             return null;
86         }
87 
88         @Override
engineCreateSSLEngine(String s, int i)89         protected SSLEngine engineCreateSSLEngine(String s, int i) {
90             return null;
91         }
92 
93         @Override
engineCreateSSLEngine()94         protected SSLEngine engineCreateSSLEngine() {
95             return null;
96         }
97 
98         @Override
engineGetServerSessionContext()99         protected SSLSessionContext engineGetServerSessionContext() {
100             return null;
101         }
102 
103         @Override
engineGetClientSessionContext()104         protected SSLSessionContext engineGetClientSessionContext() {
105             return LittleMock.mock(SSLSessionContext.class);
106         }
107     }
108 }
109