Home | History | Annotate | Download | only in conscrypt
      1 /* GENERATED SOURCE. DO NOT MODIFY. */
      2 /*
      3  * Copyright (C) 2009 The Android Open Source Project
      4  *
      5  * Licensed under the Apache License, Version 2.0 (the "License");
      6  * you may not use this file except in compliance with the License.
      7  * You may obtain a copy of the License at
      8  *
      9  *      http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  * Unless required by applicable law or agreed to in writing, software
     12  * distributed under the License is distributed on an "AS IS" BASIS,
     13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  * See the License for the specific language governing permissions and
     15  * limitations under the License.
     16  */
     17 
     18 package com.android.org.conscrypt;
     19 
     20 import static com.android.org.conscrypt.MockSessionBuilder.DEFAULT_PORT;
     21 import static org.junit.Assert.assertEquals;
     22 import static org.junit.Assert.assertNull;
     23 import static org.junit.Assert.assertSame;
     24 
     25 import java.security.KeyManagementException;
     26 import org.junit.Test;
     27 import org.junit.runner.RunWith;
     28 import org.junit.runners.JUnit4;
     29 
     30 /**
     31  * @hide This class is not part of the Android public SDK API
     32  */
     33 @RunWith(JUnit4.class)
     34 public class ClientSessionContextTest extends AbstractSessionContextTest<ClientSessionContext> {
     35 
     36     @Override
     37     ClientSessionContext newContext() {
     38         return new ClientSessionContext();
     39     }
     40 
     41     @Override
     42     NativeSslSession getCachedSession(ClientSessionContext context, NativeSslSession s) {
     43         return context.getCachedSession(s.getPeerHost(), DEFAULT_PORT,
     44                 getDefaultSSLParameters());
     45     }
     46 
     47     @Override
     48     int size(ClientSessionContext context) {
     49         return context.size();
     50     }
     51 
     52     private static SSLParametersImpl getDefaultSSLParameters() {
     53         try {
     54             return SSLParametersImpl.getDefault();
     55         } catch (KeyManagementException e) {
     56             throw new RuntimeException(e);
     57         }
     58     }
     59 
     60     @Test
     61     public void testNoMixingOfSingleAndMultiUseSessions() {
     62         ClientSessionContext context = newContext();
     63 
     64         NativeSslSession a = new MockSessionBuilder().host("a").singleUse(false).build();
     65         NativeSslSession bSingle1 = new MockSessionBuilder()
     66                 .id(new byte[] {1}).host("b").singleUse(true).build();
     67         NativeSslSession bSingle2 = new MockSessionBuilder()
     68                 .id(new byte[] {2}).host("b").singleUse(true).build();
     69         NativeSslSession bMulti = new MockSessionBuilder()
     70                 .id(new byte[] {3}).host("b").singleUse(false).build();
     71 
     72         context.cacheSession(a);
     73         assertEquals(1, size(context));
     74 
     75         context.cacheSession(bSingle1);
     76         assertEquals(2, size(context));
     77 
     78         context.cacheSession(bSingle2);
     79         assertEquals(3, size(context));
     80 
     81         context.cacheSession(bMulti);
     82         assertEquals(2, size(context));
     83 
     84         NativeSslSession out = context.getCachedSession(
     85                 "b", DEFAULT_PORT, getDefaultSSLParameters());
     86         assertEquals(bMulti, out);
     87 
     88         context.cacheSession(bSingle2);
     89         assertEquals(2, size(context));
     90 
     91         out = context.getCachedSession("b", DEFAULT_PORT, getDefaultSSLParameters());
     92         assertEquals(bSingle2, out);
     93 
     94         out = context.getCachedSession("b", DEFAULT_PORT, getDefaultSSLParameters());
     95         assertNull(out);
     96     }
     97 
     98     @Test
     99     public void testCanRetrieveMultipleSingleUseSessions() {
    100         ClientSessionContext context = newContext();
    101 
    102         NativeSslSession single1 = new MockSessionBuilder()
    103                 .id(new byte[] {1}).host("host").singleUse(true).build();
    104         NativeSslSession single2 = new MockSessionBuilder()
    105                 .id(new byte[] {2}).host("host").singleUse(true).build();
    106 
    107         context.cacheSession(single1);
    108         assertEquals(1, size(context));
    109 
    110         context.cacheSession(single2);
    111         assertEquals(2, size(context));
    112 
    113         assertSame(single1,
    114                 context.getCachedSession("host", DEFAULT_PORT, getDefaultSSLParameters()));
    115         assertEquals(1, size(context));
    116         assertSame(single2,
    117                 context.getCachedSession("host", DEFAULT_PORT, getDefaultSSLParameters()));
    118         assertEquals(0, size(context));
    119     }
    120 }
    121