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
17package org.conscrypt;
18
19import java.util.Enumeration;
20import java.util.HashSet;
21import java.util.Set;
22import javax.net.ssl.SSLSession;
23import junit.framework.TestCase;
24import libcore.javax.net.ssl.FakeSSLSession;
25
26public final class ClientSessionContextTest extends TestCase {
27
28    public void testSimpleAddition() {
29        ClientSessionContext context = new ClientSessionContext();
30        SSLSession a = new ValidSSLSession("a");
31        SSLSession b = new ValidSSLSession("b");
32
33        context.putSession(a);
34        assertSessionContextContents(context, new SSLSession[] { a }, new SSLSession[] { b });
35
36        context.putSession(b);
37        assertSessionContextContents(context, new SSLSession[] { a, b }, new SSLSession[0]);
38    }
39
40    public void testTrimToSize() {
41        ClientSessionContext context = new ClientSessionContext();
42        ValidSSLSession a = new ValidSSLSession("a");
43        ValidSSLSession b = new ValidSSLSession("b");
44        ValidSSLSession c = new ValidSSLSession("c");
45        ValidSSLSession d = new ValidSSLSession("d");
46
47        context.putSession(a);
48        assertSessionContextContents(context, new SSLSession[] { a }, new SSLSession[] { b, c, d });
49
50        context.putSession(b);
51        assertSessionContextContents(context, new SSLSession[] { a, b }, new SSLSession[] { c, d });
52
53        context.putSession(c);
54        assertSessionContextContents(context, new SSLSession[] { a, b, c }, new SSLSession[] { d });
55
56        context.putSession(d);
57        assertSessionContextContents(context, new SSLSession[] { a, b, c, d }, new SSLSession[0]);
58
59        context.setSessionCacheSize(2);
60        assertSessionContextContents(context, new SSLSession[] { c, d }, new SSLSession[] { a, b });
61    }
62
63    public void testImplicitRemovalOfOldest() {
64        ClientSessionContext context = new ClientSessionContext();
65        context.setSessionCacheSize(2);
66        ValidSSLSession a = new ValidSSLSession("a");
67        ValidSSLSession b = new ValidSSLSession("b");
68        ValidSSLSession c = new ValidSSLSession("c");
69        ValidSSLSession d = new ValidSSLSession("d");
70
71        context.putSession(a);
72        assertSessionContextContents(context, new SSLSession[] { a }, new SSLSession[] { b, c, d });
73
74        context.putSession(b);
75        assertSessionContextContents(context, new SSLSession[] { a, b }, new SSLSession[] { c, d });
76
77        context.putSession(c);
78        assertSessionContextContents(context, new SSLSession[] { b, c }, new SSLSession[] { a, d });
79
80        context.putSession(d);
81        assertSessionContextContents(context, new SSLSession[] { c, d }, new SSLSession[] { a, b });
82    }
83
84    private static void assertSessionContextContents(ClientSessionContext context,
85                                                     SSLSession[] contains,
86                                                     SSLSession[] exludes) {
87        assertEquals(contains.length, context.size());
88
89        for (SSLSession s : contains) {
90            assertSame(s.getPeerHost(), s, context.getSession(s.getId()));
91            assertSame(s.getPeerHost(), s, context.getSession(s.getPeerHost(), 443));
92        }
93        for (SSLSession s : exludes) {
94            assertNull(s.getPeerHost(), context.getSession(s.getId()));
95            assertNull(s.getPeerHost(), context.getSession(s.getPeerHost(), 443));
96        }
97
98        Set<SSLSession> sessions = new HashSet<SSLSession>();
99        Enumeration<byte[]> ids = context.getIds();
100        while (ids.hasMoreElements()) {
101            byte[] id = ids.nextElement();
102            sessions.add(context.getSession(id));
103        }
104
105        Set<SSLSession> expected = new HashSet<SSLSession>();
106        for (SSLSession s : sessions) {
107            expected.add(s);
108        }
109        assertEquals(expected, sessions);
110    }
111
112    static class ValidSSLSession extends FakeSSLSession {
113        ValidSSLSession(String host) {
114            super(host);
115        }
116        @Override public boolean isValid() {
117            return true;
118        }
119    }
120}
121