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