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.KeyManagementException;
021: import java.security.KeyPairGenerator;
022: import java.security.PublicKey;
023: import java.security.SecureRandom;
024:
025: import javax.net.ssl.SSLEngineResult;
026:
027: import junit.framework.TestCase;
028:
029: /**
030: * Tests for <code>HandshakeProtocol</code> constructor and methods
031: *
032: */
033: public class HandshakeProtocolTest extends TestCase {
034:
035: public void testGetStatus() throws Exception {
036: HandshakeProtocol protocol = new ClientHandshakeImpl(
037: new SSLEngineImpl(new SSLParameters(null, null, null,
038: new SSLSessionContextImpl(),
039: new SSLSessionContextImpl())));
040:
041: assertEquals(protocol.getStatus(),
042: SSLEngineResult.HandshakeStatus.NOT_HANDSHAKING);
043:
044: protocol.status = HandshakeProtocol.NEED_UNWRAP;
045: assertEquals(protocol.getStatus(),
046: SSLEngineResult.HandshakeStatus.NEED_UNWRAP);
047:
048: protocol.status = HandshakeProtocol.FINISHED;
049: assertEquals(protocol.getStatus(),
050: SSLEngineResult.HandshakeStatus.FINISHED);
051: assertEquals(protocol.status, HandshakeProtocol.NOT_HANDSHAKING);
052:
053: protocol.delegatedTaskErr = new Exception();
054: assertEquals(protocol.getStatus(),
055: SSLEngineResult.HandshakeStatus.NEED_WRAP);
056: protocol.delegatedTaskErr = null;
057:
058: protocol.delegatedTasks
059: .add(new DelegatedTask(null, null, null));
060: assertEquals(protocol.getStatus(),
061: SSLEngineResult.HandshakeStatus.NEED_TASK);
062: protocol.delegatedTasks.clear();
063:
064: protocol.io_stream.write(new byte[] { 1, 2, 3 });
065: assertEquals(protocol.getStatus(),
066: SSLEngineResult.HandshakeStatus.NEED_WRAP);
067: }
068:
069: public void testSendChangeCipherSpec() throws Exception {
070: HandshakeProtocol protocol = new ServerHandshakeImpl(
071: new SSLEngineImpl(new SSLParameters(null, null, null,
072: new SSLSessionContextImpl(),
073: new SSLSessionContextImpl())));
074:
075: protocol.sendChangeCipherSpec();
076: assertEquals(protocol.getStatus(),
077: SSLEngineResult.HandshakeStatus.NEED_WRAP);
078: }
079:
080: public void testWrap() throws Exception {
081: HandshakeProtocol protocol = new ClientHandshakeImpl(
082: new SSLEngineImpl(new SSLParameters(null, null, null,
083: new SSLSessionContextImpl(),
084: new SSLSessionContextImpl())));
085:
086: assertNull(protocol.wrap());
087:
088: protocol.delegatedTaskErr = new Exception();
089: try {
090: protocol.wrap();
091: fail("No expected AlertException");
092: } catch (AlertException e) {
093: assertEquals(e.getDescriptionCode(),
094: AlertProtocol.HANDSHAKE_FAILURE);
095: assertNull(protocol.delegatedTaskErr);
096: }
097: }
098:
099: public void testcomputerVerifyDataTLS() throws Exception {
100: HandshakeProtocol hs_protocol = new ClientHandshakeImpl(
101: new SSLEngineImpl(new SSLParameters(null, null, null,
102: new SSLSessionContextImpl(),
103: new SSLSessionContextImpl())));
104:
105: SecureRandom sr = new SecureRandom();
106: SSLSessionImpl ses = new SSLSessionImpl(sr);
107: hs_protocol.session = ses;
108: hs_protocol.session.protocol = ProtocolVersion.TLSv1;
109: assertSame(hs_protocol.getSession(), ses);
110:
111: hs_protocol.clientHello = new ClientHello(
112: sr,
113: hs_protocol.session.protocol.version,
114: hs_protocol.session.id,
115: new CipherSuite[] { CipherSuite.TLS_DH_DSS_EXPORT_WITH_DES40_CBC_SHA });
116: hs_protocol.serverHello = new ServerHello(sr,
117: hs_protocol.session.protocol.version,
118: hs_protocol.session.id,
119: CipherSuite.TLS_DH_DSS_EXPORT_WITH_DES40_CBC_SHA,
120: (byte) 0);
121:
122: hs_protocol.preMasterSecret = new byte[] { 1, 2, 3, 4, 5, 6, 7,
123: 8, 9, 0 };
124: hs_protocol.computerMasterSecret();
125: assertNull(hs_protocol.preMasterSecret);
126: assertEquals(48, hs_protocol.session.master_secret.length);
127:
128: hs_protocol.send(hs_protocol.clientHello);
129: hs_protocol.send(hs_protocol.serverHello);
130:
131: hs_protocol.computerReferenceVerifyDataTLS("test");
132:
133: byte[] data = new byte[12];
134: hs_protocol.computerVerifyDataTLS("test", data);
135:
136: hs_protocol.verifyFinished(data);
137:
138: try {
139: hs_protocol.verifyFinished(new byte[] { 1, 2, 3, 4, 5, 6,
140: 7, 8, 9, 0, 1, 2 });
141: fail("No expected AlertException");
142: } catch (AlertException e) {
143: }
144: }
145:
146: public void testComputerReferenceVerifyDataSSLv3() throws Exception {
147: HandshakeProtocol hs_protocol = new ClientHandshakeImpl(
148: new SSLEngineImpl(new SSLParameters(null, null, null,
149: new SSLSessionContextImpl(),
150: new SSLSessionContextImpl())));
151:
152: SecureRandom sr = new SecureRandom();
153: SSLSessionImpl ses = new SSLSessionImpl(sr);
154: hs_protocol.session = ses;
155: hs_protocol.session.protocol = ProtocolVersion.SSLv3;
156: assertSame(hs_protocol.getSession(), ses);
157:
158: hs_protocol.clientHello = new ClientHello(
159: sr,
160: hs_protocol.session.protocol.version,
161: hs_protocol.session.id,
162: new CipherSuite[] { CipherSuite.TLS_DH_DSS_EXPORT_WITH_DES40_CBC_SHA });
163: hs_protocol.serverHello = new ServerHello(sr,
164: hs_protocol.session.protocol.version,
165: hs_protocol.session.id,
166: CipherSuite.TLS_DH_DSS_EXPORT_WITH_DES40_CBC_SHA,
167: (byte) 0);
168:
169: hs_protocol.preMasterSecret = new byte[] { 1, 2, 3, 4, 5, 6, 7,
170: 8, 9, 0 };
171: hs_protocol.computerMasterSecret();
172: assertNull(hs_protocol.preMasterSecret);
173: assertEquals(48, hs_protocol.session.master_secret.length);
174:
175: hs_protocol.send(hs_protocol.clientHello);
176: hs_protocol.send(hs_protocol.serverHello);
177:
178: hs_protocol
179: .computerReferenceVerifyDataSSLv3(SSLv3Constants.client);
180:
181: byte[] data = new byte[36];
182: hs_protocol
183: .computerVerifyDataSSLv3(SSLv3Constants.client, data);
184:
185: hs_protocol.verifyFinished(data);
186:
187: try {
188: hs_protocol.verifyFinished(new byte[] { 1, 2, 3, 4, 5, 6,
189: 7, 8, 9, 0, 1, 2 });
190: fail("No expected AlertException");
191: } catch (AlertException e) {
192: }
193: }
194:
195: public void testUnexpectedMessage() throws Exception {
196: HandshakeProtocol protocol = new ClientHandshakeImpl(
197: new SSLEngineImpl(new SSLParameters(null, null, null,
198: new SSLSessionContextImpl(),
199: new SSLSessionContextImpl())));
200: try {
201: protocol.unexpectedMessage();
202: fail("No expected AlertException");
203: } catch (AlertException e) {
204: assertEquals(e.getDescriptionCode(),
205: AlertProtocol.UNEXPECTED_MESSAGE);
206: }
207: }
208:
209: public void testGetTask() throws Exception {
210: HandshakeProtocol protocol = new ClientHandshakeImpl(
211: new SSLEngineImpl(new SSLParameters(null, null, null,
212: new SSLSessionContextImpl(),
213: new SSLSessionContextImpl())));
214:
215: DelegatedTask task = new DelegatedTask(null, null, null);
216: protocol.delegatedTasks.add(task);
217: assertSame(protocol.getTask(), task);
218: assertNull(protocol.getTask());
219: }
220:
221: public void testGetRSAKeyLength() throws Exception {
222: KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
223: kpg.initialize(512);
224: PublicKey key = kpg.genKeyPair().getPublic();
225:
226: assertEquals(512, HandshakeProtocol.getRSAKeyLength(key));
227:
228: }
229:
230: }
|