Home | History | Annotate | Download | only in net
      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 org.mockito.Mockito;
     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 
     40     public void testInstall_compatibleContext() throws Exception {
     41         final SSLContext ctx = SSLContext.getDefault();
     42         final SSLClientSessionCache mock = Mockito.mock(SSLClientSessionCache.class);
     43         final ClientSessionContext clientCtx = (ClientSessionContext) ctx.getClientSessionContext();
     44 
     45         try {
     46             SSLSessionCache.install(new SSLSessionCache(mock), ctx);
     47         } finally {
     48             // Restore cacheless behaviour.
     49             SSLSessionCache.install(null, ctx);
     50             Mockito.verifyNoMoreInteractions(mock);
     51         }
     52     }
     53 
     54     public void testInstall_incompatibleContext() {
     55         try {
     56             SSLSessionCache.install(
     57                     new SSLSessionCache(Mockito.mock(SSLClientSessionCache.class)),
     58                     new FakeSSLContext());
     59             fail();
     60         } catch (IllegalArgumentException expected) {}
     61     }
     62 
     63     static final class FakeSSLContext extends SSLContext {
     64         protected FakeSSLContext() {
     65             super(new FakeSSLContextSpi(), null, "test");
     66         }
     67     }
     68 
     69     static final class FakeSSLContextSpi extends SSLContextSpi {
     70         @Override
     71         protected void engineInit(KeyManager[] keyManagers, TrustManager[] trustManagers,
     72                 SecureRandom secureRandom) throws KeyManagementException {
     73         }
     74 
     75         @Override
     76         protected SSLSocketFactory engineGetSocketFactory() {
     77             return null;
     78         }
     79 
     80         @Override
     81         protected SSLServerSocketFactory engineGetServerSocketFactory() {
     82             return null;
     83         }
     84 
     85         @Override
     86         protected SSLEngine engineCreateSSLEngine(String s, int i) {
     87             return null;
     88         }
     89 
     90         @Override
     91         protected SSLEngine engineCreateSSLEngine() {
     92             return null;
     93         }
     94 
     95         @Override
     96         protected SSLSessionContext engineGetServerSessionContext() {
     97             return null;
     98         }
     99 
    100         @Override
    101         protected SSLSessionContext engineGetClientSessionContext() {
    102             return Mockito.mock(SSLSessionContext.class);
    103         }
    104     }
    105 }
    106