1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  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
18package org.apache.harmony.xnet.provider.jsse;
19
20import java.util.Enumeration;
21import java.security.SecureRandom;
22
23import javax.net.ssl.SSLSession;
24
25import junit.framework.TestCase;
26
27/**
28 * Tests for <code>SSLSessionContextImp</code> constructor and methods
29 *
30 */
31public class SSLSessionContextImplTest extends TestCase {
32
33    public void testSSLSessionContextImpl() {
34        SecureRandom sr = new SecureRandom();
35        SSLSessionImpl ses1 = new SSLSessionImpl(
36                CipherSuite.TLS_DH_anon_EXPORT_WITH_DES40_CBC_SHA, sr);
37        SSLSessionImpl ses2 = new SSLSessionImpl(
38                CipherSuite.TLS_DH_anon_WITH_3DES_EDE_CBC_SHA, sr);
39        SSLSessionImpl ses3 = new SSLSessionImpl(
40                CipherSuite.TLS_RSA_EXPORT_WITH_RC2_CBC_40_MD5, sr);
41
42        SSLSessionContextImpl context = new SSLSessionContextImpl();
43        context.putSession(ses1);
44        context.putSession(ses2);
45        context.putSession(ses3);
46
47        for (Enumeration en = context.getIds(); en.hasMoreElements();) {
48            byte[] id = (byte[])en.nextElement();
49            assertTrue(context.getSession(id) != null);
50        }
51
52        SSLSession ses = context.getSession(ses1.getId());
53        assertSame(ses1, ses);
54
55        ses = context.getSession(ses3.getId());
56        assertSame(ses3, ses);
57    }
58
59    public void testGetSessionCacheSize() {
60        SSLSessionContextImpl context = new SSLSessionContextImpl();
61        assertEquals(0, context.getSessionCacheSize());
62
63        context.setSessionCacheSize(100);
64        assertEquals(100, context.getSessionCacheSize());
65
66        try {
67            context.setSessionCacheSize(-1);
68            fail("No expected IllegalArgumentException");
69        } catch (IllegalArgumentException e) {
70        }
71    }
72
73    public void testGetSessionTimeout() {
74        SSLSessionContextImpl context = new SSLSessionContextImpl();
75        assertEquals(0, context.getSessionTimeout());
76
77        context.setSessionTimeout(1000);
78        assertEquals(1000, context.getSessionTimeout());
79
80        try {
81            context.setSessionTimeout(-1);
82            fail("No expected IllegalArgumentException");
83        } catch (IllegalArgumentException e) {
84        }
85    }
86
87}