001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.harmony.xnet.provider.jsse;
019:
020: import java.security.SecureRandom;
021: import java.util.Arrays;
022:
023: import javax.net.ssl.SSLPeerUnverifiedException;
024:
025: import junit.framework.TestCase;
026:
027: /**
028: * Tests for <code>SSLSessionContextImp</code> constructor and methods
029: *
030: */
031: public class SSLSessionImplTest extends TestCase {
032:
033: /*
034: * Class under test for void SSLSessionImpl(CipherSuite, SecureRandom)
035: */
036: public void testSSLSessionImplCipherSuiteSecureRandom() {
037: SSLSessionImpl session = new SSLSessionImpl(null, null);
038: assertEquals(session.getCipherSuite(),
039: CipherSuite.TLS_NULL_WITH_NULL_NULL.getName());
040:
041: session = new SSLSessionImpl(CipherSuite.TLS_RSA_WITH_NULL_MD5,
042: new SecureRandom());
043: session.protocol = ProtocolVersion.TLSv1;
044: assertEquals("Incorrect protocol", "TLSv1", session
045: .getProtocol());
046: assertEquals("Incorrect cipher suite",
047: session.getCipherSuite(),
048: CipherSuite.TLS_RSA_WITH_NULL_MD5.getName());
049: assertEquals("Incorrect id", 32, session.getId().length);
050: assertTrue("Incorrect isValid", session.isValid());
051: assertTrue("Incorrect isServer", session.isServer);
052: long time = session.getCreationTime();
053: assertTrue("Incorrect CreationTime", time <= System
054: .currentTimeMillis());
055: assertEquals("Incorrect LastAccessedTime", time, session
056: .getLastAccessedTime());
057: assertNull("Incorrect LocalCertificates", session
058: .getLocalCertificates());
059: assertNull("Incorrect LocalPrincipal", session
060: .getLocalPrincipal());
061: assertNull(session.getPeerHost());
062: assertEquals(-1, session.getPeerPort());
063: assertNull(session.getSessionContext());
064:
065: try {
066: session.getPeerCertificateChain();
067: fail("getPeerCertificateChain: No expected SSLPeerUnverifiedException");
068: } catch (SSLPeerUnverifiedException e) {
069: }
070:
071: try {
072: session.getPeerCertificates();
073: fail("getPeerCertificates: No expected SSLPeerUnverifiedException");
074: } catch (SSLPeerUnverifiedException e) {
075: }
076:
077: try {
078: session.getPeerPrincipal();
079: fail("getPeerPrincipal: No expected SSLPeerUnverifiedException");
080: } catch (SSLPeerUnverifiedException e) {
081: }
082: }
083:
084: public void testGetApplicationBufferSize() {
085: assertEquals(SSLSessionImpl.NULL_SESSION
086: .getApplicationBufferSize(),
087: SSLRecordProtocol.MAX_DATA_LENGTH);
088: }
089:
090: public void testGetPacketBufferSize() {
091: assertEquals(SSLSessionImpl.NULL_SESSION.getPacketBufferSize(),
092: SSLRecordProtocol.MAX_SSL_PACKET_SIZE);
093: }
094:
095: public void testInvalidate() {
096: SSLSessionImpl session = new SSLSessionImpl(
097: CipherSuite.TLS_RSA_WITH_NULL_MD5, new SecureRandom());
098: session.invalidate();
099: assertFalse("Incorrect isValid", session.isValid());
100:
101: }
102:
103: public void testSetPeer() {
104: SSLSessionImpl session = new SSLSessionImpl(null);
105: session.setPeer("someHost", 8080);
106: assertEquals("someHost", session.getPeerHost());
107: assertEquals(8080, session.getPeerPort());
108: }
109:
110: public void testGetValue() {
111: SSLSessionImpl session = new SSLSessionImpl(null);
112:
113: assertEquals(0, session.getValueNames().length);
114:
115: try {
116: session.getValue(null);
117: fail("No expected IllegalArgumentException");
118: } catch (IllegalArgumentException e) {
119: }
120: assertNull(session.getValue("abc"));
121:
122: try {
123: session.removeValue(null);
124: fail("No expected IllegalArgumentException");
125: } catch (IllegalArgumentException e) {
126: }
127: session.removeValue("abc");
128:
129: try {
130: session.putValue(null, "1");
131: fail("No expected IllegalArgumentException");
132: } catch (IllegalArgumentException e) {
133: }
134:
135: try {
136: session.putValue("abc", null);
137: fail("No expected IllegalArgumentException");
138: } catch (IllegalArgumentException e) {
139: }
140:
141: Object o = new Object();
142: session.putValue("abc", o);
143: assertSame(session.getValue("abc"), o);
144: assertEquals("abc", session.getValueNames()[0]);
145:
146: session.removeValue("abc");
147: assertNull(session.getValue("abc"));
148: }
149:
150: public void testClone() {
151: SSLSessionImpl session1 = new SSLSessionImpl(
152: CipherSuite.TLS_RSA_WITH_NULL_MD5, new SecureRandom());
153: SSLSessionImpl session2 = (SSLSessionImpl) session1.clone();
154: assertTrue(Arrays.equals(session1.getId(), session2.getId()));
155: }
156:
157: }
|