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.security.SecureRandom;
21import java.util.Arrays;
22
23import javax.net.ssl.SSLPeerUnverifiedException;
24
25import junit.framework.TestCase;
26
27/**
28 * Tests for <code>SSLSessionContextImp</code> constructor and methods
29 */
30public class SSLSessionImplTest extends TestCase {
31
32    /*
33     * Class under test for void SSLSessionImpl(CipherSuite, SecureRandom)
34     */
35    public void testSSLSessionImplCipherSuiteSecureRandom() {
36        SSLSessionImpl session = new SSLSessionImpl(null, null);
37        assertEquals(session.getCipherSuite(),
38                CipherSuite.TLS_NULL_WITH_NULL_NULL.getName());
39
40        session = new SSLSessionImpl(CipherSuite.TLS_RSA_WITH_NULL_MD5,
41                new SecureRandom());
42        session.protocol = ProtocolVersion.TLSv1;
43        assertEquals("Incorrect protocol", "TLSv1", session.getProtocol());
44        assertEquals("Incorrect cipher suite", session.getCipherSuite(),
45                CipherSuite.TLS_RSA_WITH_NULL_MD5.getName());
46        assertEquals("Incorrect id", 32, session.getId().length);
47        assertTrue("Incorrect isValid", session.isValid());
48        assertTrue("Incorrect isServer", session.isServer);
49        long time = session.getCreationTime();
50        assertTrue("Incorrect CreationTime", time <= System.currentTimeMillis());
51        assertEquals("Incorrect LastAccessedTime", time, session.getLastAccessedTime());
52        assertNull("Incorrect LocalCertificates", session.getLocalCertificates());
53        assertNull("Incorrect LocalPrincipal", session.getLocalPrincipal());
54        assertNull(session.getPeerHost());
55        assertEquals(-1, session.getPeerPort());
56        assertNull(session.getSessionContext());
57
58        try {
59            session.getPeerCertificateChain();
60            fail("getPeerCertificateChain: No expected SSLPeerUnverifiedException");
61        } catch (SSLPeerUnverifiedException e) {
62        }
63
64        try {
65            session.getPeerCertificates();
66            fail("getPeerCertificates: No expected SSLPeerUnverifiedException");
67        } catch (SSLPeerUnverifiedException e) {
68        }
69
70        try {
71            session.getPeerPrincipal();
72            fail("getPeerPrincipal: No expected SSLPeerUnverifiedException");
73        } catch (SSLPeerUnverifiedException e) {
74        }
75    }
76
77    public void testGetApplicationBufferSize() {
78        assertEquals(SSLSessionImpl.NULL_SESSION.getApplicationBufferSize(),
79                SSLRecordProtocol.MAX_DATA_LENGTH);
80    }
81
82    public void testGetPacketBufferSize() {
83        assertEquals(SSLSessionImpl.NULL_SESSION.getPacketBufferSize(),
84                SSLRecordProtocol.MAX_SSL_PACKET_SIZE);
85    }
86
87    public void testInvalidate() {
88        SSLSessionImpl session = new SSLSessionImpl(
89                CipherSuite.TLS_RSA_WITH_NULL_MD5, new SecureRandom());
90        session.invalidate();
91        assertFalse("Incorrect isValid", session.isValid());
92
93    }
94
95    public void testSetPeer() {
96        SSLSessionImpl session = new SSLSessionImpl(null);
97        session.setPeer("someHost", 8080);
98        assertEquals("someHost", session.getPeerHost());
99        assertEquals(8080, session.getPeerPort());
100    }
101
102
103    public void testGetValue() {
104        SSLSessionImpl session = new SSLSessionImpl(null);
105
106        assertEquals(0, session.getValueNames().length);
107
108        try {
109            session.getValue(null);
110            fail("No expected IllegalArgumentException");
111        } catch (IllegalArgumentException e) {
112        }
113        assertNull(session.getValue("abc"));
114
115        try {
116            session.removeValue(null);
117            fail("No expected IllegalArgumentException");
118        } catch (IllegalArgumentException e) {
119        }
120        session.removeValue("abc");
121
122        try {
123            session.putValue(null, "1");
124            fail("No expected IllegalArgumentException");
125        } catch (IllegalArgumentException e) {
126        }
127
128        try {
129            session.putValue("abc", null);
130            fail("No expected IllegalArgumentException");
131        } catch (IllegalArgumentException e) {
132        }
133
134        Object o = new Object();
135        session.putValue("abc", o);
136        assertSame(session.getValue("abc"), o);
137        assertEquals("abc", session.getValueNames()[0]);
138
139        session.removeValue("abc");
140        assertNull(session.getValue("abc"));
141    }
142
143    public void testClone() {
144        SSLSessionImpl session1 = new SSLSessionImpl(
145                CipherSuite.TLS_RSA_WITH_NULL_MD5, new SecureRandom());
146        SSLSessionImpl session2 = (SSLSessionImpl) session1.clone();
147        assertTrue(Arrays.equals(session1.getId(), session2.getId()));
148    }
149
150}