1 /* 2 * Copyright (C) 2017 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.junit.Assert.assertEquals; 20 import static org.junit.Assert.assertNull; 21 import static org.junit.Assert.assertSame; 22 import static org.mockito.Matchers.same; 23 import static org.mockito.Mockito.any; 24 import static org.mockito.Mockito.mock; 25 import static org.mockito.Mockito.verify; 26 import static org.mockito.Mockito.when; 27 28 import java.security.cert.Certificate; 29 import javax.net.ssl.SSLSession; 30 import org.junit.Before; 31 import org.junit.Test; 32 33 public abstract class AbstractSessionContextTest<T extends AbstractSessionContext> { 34 private T context; 35 36 @Before setup()37 public void setup() { 38 context = newContext(); 39 } 40 newContext()41 abstract T newContext(); size(T context)42 abstract int size(T context); getCachedSession(T context, SslSessionWrapper s)43 abstract SslSessionWrapper getCachedSession(T context, SslSessionWrapper s); 44 45 @Test testSimpleAddition()46 public void testSimpleAddition() { 47 SslSessionWrapper a = newSession("a"); 48 SslSessionWrapper b = newSession("b"); 49 50 context.cacheSession(a); 51 assertSessionContextContents(toArray(a), toArray(b)); 52 53 context.cacheSession(b); 54 assertSessionContextContents(toArray(a, b), toArray()); 55 } 56 57 @Test testTrimToSize()58 public void testTrimToSize() { 59 SslSessionWrapper a = newSession("a"); 60 SslSessionWrapper b = newSession("b"); 61 SslSessionWrapper c = newSession("c"); 62 SslSessionWrapper d = newSession("d"); 63 64 context.cacheSession(a); 65 context.cacheSession(b); 66 context.cacheSession(c); 67 context.cacheSession(d); 68 assertSessionContextContents(toArray(a, b, c, d), toArray()); 69 70 context.setSessionCacheSize(2); 71 assertSessionContextContents(toArray(c, d), toArray(a, b)); 72 } 73 74 @Test testImplicitRemovalOfOldest()75 public void testImplicitRemovalOfOldest() { 76 context.setSessionCacheSize(2); 77 SslSessionWrapper a = newSession("a"); 78 SslSessionWrapper b = newSession("b"); 79 SslSessionWrapper c = newSession("c"); 80 SslSessionWrapper d = newSession("d"); 81 82 context.cacheSession(a); 83 assertSessionContextContents(toArray(a), toArray(b, c, d)); 84 85 context.cacheSession(b); 86 assertSessionContextContents(toArray(a, b), toArray(c, d)); 87 88 context.cacheSession(c); 89 assertSessionContextContents(toArray(b, c), toArray(a, d)); 90 91 context.cacheSession(d); 92 assertSessionContextContents(toArray(c, d), toArray(a, b)); 93 } 94 95 @Test testSerializeSession()96 public void testSerializeSession() throws Exception { 97 Certificate mockCert = mock(Certificate.class); 98 when(mockCert.getEncoded()).thenReturn(new byte[] {0x05, 0x06, 0x07, 0x10}); 99 100 byte[] encodedBytes = new byte[] {0x01, 0x02, 0x03}; 101 SslSessionWrapper session = new MockSessionBuilder() 102 .id(new byte[] {0x11, 0x09, 0x03, 0x20}) 103 .host("ssl.example.com") 104 .encodedBytes(encodedBytes) 105 .build(); 106 107 SSLClientSessionCache mockCache = mock(SSLClientSessionCache.class); 108 ClientSessionContext context = new ClientSessionContext(); 109 context.setPersistentCache(mockCache); 110 111 context.cacheSession(session); 112 verify(mockCache).putSessionData(any(SSLSession.class), same(encodedBytes)); 113 } 114 assertSessionContextContents( SslSessionWrapper[] contains, SslSessionWrapper[] exludes)115 private void assertSessionContextContents( 116 SslSessionWrapper[] contains, SslSessionWrapper[] exludes) { 117 assertEquals(contains.length, size(context)); 118 119 for (SslSessionWrapper s : contains) { 120 assertSame(s.getPeerHost(), s, getCachedSession(context, s)); 121 } 122 for (SslSessionWrapper s : exludes) { 123 assertNull(s.getPeerHost(), getCachedSession(context, s)); 124 } 125 } 126 toArray(SslSessionWrapper... sessions)127 private static SslSessionWrapper[] toArray(SslSessionWrapper... sessions) { 128 return sessions; 129 } 130 newSession(String host)131 private SslSessionWrapper newSession(String host) { 132 return new MockSessionBuilder().host(host).build(); 133 } 134 } 135