1 /* 2 * Copyright (C) 2009 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 org.conscrypt; 18 19 import static org.conscrypt.MockSessionBuilder.DEFAULT_PORT; 20 import static org.junit.Assert.assertEquals; 21 import static org.junit.Assert.assertNull; 22 import static org.junit.Assert.assertSame; 23 24 import java.security.KeyManagementException; 25 import org.junit.Test; 26 import org.junit.runner.RunWith; 27 import org.junit.runners.JUnit4; 28 29 @RunWith(JUnit4.class) 30 public class ClientSessionContextTest extends AbstractSessionContextTest<ClientSessionContext> { 31 32 @Override newContext()33 ClientSessionContext newContext() { 34 return new ClientSessionContext(); 35 } 36 37 @Override getCachedSession(ClientSessionContext context, NativeSslSession s)38 NativeSslSession getCachedSession(ClientSessionContext context, NativeSslSession s) { 39 return context.getCachedSession(s.getPeerHost(), DEFAULT_PORT, 40 getDefaultSSLParameters()); 41 } 42 43 @Override size(ClientSessionContext context)44 int size(ClientSessionContext context) { 45 return context.size(); 46 } 47 getDefaultSSLParameters()48 private static SSLParametersImpl getDefaultSSLParameters() { 49 try { 50 return SSLParametersImpl.getDefault(); 51 } catch (KeyManagementException e) { 52 throw new RuntimeException(e); 53 } 54 } 55 56 @Test testNoMixingOfSingleAndMultiUseSessions()57 public void testNoMixingOfSingleAndMultiUseSessions() { 58 ClientSessionContext context = newContext(); 59 60 NativeSslSession a = new MockSessionBuilder().host("a").singleUse(false).build(); 61 NativeSslSession bSingle1 = new MockSessionBuilder() 62 .id(new byte[] {1}).host("b").singleUse(true).build(); 63 NativeSslSession bSingle2 = new MockSessionBuilder() 64 .id(new byte[] {2}).host("b").singleUse(true).build(); 65 NativeSslSession bMulti = new MockSessionBuilder() 66 .id(new byte[] {3}).host("b").singleUse(false).build(); 67 68 context.cacheSession(a); 69 assertEquals(1, size(context)); 70 71 context.cacheSession(bSingle1); 72 assertEquals(2, size(context)); 73 74 context.cacheSession(bSingle2); 75 assertEquals(3, size(context)); 76 77 context.cacheSession(bMulti); 78 assertEquals(2, size(context)); 79 80 NativeSslSession out = context.getCachedSession( 81 "b", DEFAULT_PORT, getDefaultSSLParameters()); 82 assertEquals(bMulti, out); 83 84 context.cacheSession(bSingle2); 85 assertEquals(2, size(context)); 86 87 out = context.getCachedSession("b", DEFAULT_PORT, getDefaultSSLParameters()); 88 assertEquals(bSingle2, out); 89 90 out = context.getCachedSession("b", DEFAULT_PORT, getDefaultSSLParameters()); 91 assertNull(out); 92 } 93 94 @Test testCanRetrieveMultipleSingleUseSessions()95 public void testCanRetrieveMultipleSingleUseSessions() { 96 ClientSessionContext context = newContext(); 97 98 NativeSslSession single1 = new MockSessionBuilder() 99 .id(new byte[] {1}).host("host").singleUse(true).build(); 100 NativeSslSession single2 = new MockSessionBuilder() 101 .id(new byte[] {2}).host("host").singleUse(true).build(); 102 103 context.cacheSession(single1); 104 assertEquals(1, size(context)); 105 106 context.cacheSession(single2); 107 assertEquals(2, size(context)); 108 109 assertSame(single1, 110 context.getCachedSession("host", DEFAULT_PORT, getDefaultSSLParameters())); 111 assertEquals(1, size(context)); 112 assertSame(single2, 113 context.getCachedSession("host", DEFAULT_PORT, getDefaultSSLParameters())); 114 assertEquals(0, size(context)); 115 } 116 } 117