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.io.IOException;
021: import java.math.BigInteger;
022: import java.util.Arrays;
023:
024: import junit.framework.TestCase;
025:
026: /**
027: * Tests for <code>ClientKeyExchange</code> constructor and methods
028: *
029: */
030: public class ClientKeyExchangeTest extends TestCase {
031:
032: /*
033: * Test for void ClientKeyExchange(byte[], boolean)
034: */
035: public void testClientKeyExchangebyteArrayboolean()
036: throws Exception {
037: byte[] encrypted_pre_master_secret = new byte[] { 1, 2, 3, 4,
038: 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
039: boolean[] isTLS = new boolean[] { true, false };
040:
041: for (int i = 0; i < isTLS.length; i++) {
042: ClientKeyExchange message = new ClientKeyExchange(
043: encrypted_pre_master_secret, isTLS[i]);
044: assertEquals("incorrect type",
045: Handshake.CLIENT_KEY_EXCHANGE, message.getType());
046:
047: assertTrue("incorrect ClientKeyExchange", Arrays.equals(
048: message.exchange_keys, encrypted_pre_master_secret));
049:
050: HandshakeIODataStream out = new HandshakeIODataStream();
051: message.send(out);
052: byte[] encoded = out.getData(1000);
053: assertEquals("incorrect out data length ",
054: message.length(), encoded.length);
055:
056: HandshakeIODataStream in = new HandshakeIODataStream();
057: in.append(encoded);
058: ClientKeyExchange message_2 = new ClientKeyExchange(in,
059: message.length(), isTLS[i], true);
060:
061: assertTrue("Incorrect message decoding", Arrays.equals(
062: message.exchange_keys, message_2.exchange_keys));
063: assertEquals("Incorrect message decoding",
064: message.length(), message_2.length());
065:
066: in.append(encoded);
067: try {
068: message_2 = new ClientKeyExchange(in,
069: message.length() - 1, isTLS[i], true);
070: if (isTLS[i]) {
071: fail("Small length: No expected AlertException");
072: }
073: } catch (AlertException e) {
074: if (!isTLS[i]) {
075: fail(e.toString());
076: }
077: }
078:
079: in.append(encoded);
080: in.append(new byte[] { 1, 2, 3 });
081: try {
082: message_2 = new ClientKeyExchange(in,
083: message.length() + 3, isTLS[i], true);
084: if (isTLS[i]) {
085: fail("Extra bytes: No expected AlertException");
086: }
087: } catch (AlertException e) {
088: if (!isTLS[i]) {
089: fail(e.toString());
090: }
091: }
092: }
093: }
094:
095: /*
096: * Test for void ClientKeyExchange(BigInteger)
097: */
098: public void testClientKeyExchangeBigInteger() throws Exception {
099: BigInteger dh_Yc = new BigInteger("1234567890");
100: boolean[] isTLS = new boolean[] { true, false };
101:
102: for (int i = 0; i < isTLS.length; i++) {
103: ClientKeyExchange message = new ClientKeyExchange(dh_Yc);
104: assertEquals("incorrect type",
105: Handshake.CLIENT_KEY_EXCHANGE, message.getType());
106: assertEquals("incorrect ClientKeyExchange", dh_Yc,
107: new BigInteger(message.exchange_keys));
108:
109: HandshakeIODataStream out = new HandshakeIODataStream();
110: message.send(out);
111: byte[] encoded = out.getData(1000);
112: assertEquals("incorrect out data length", message.length(),
113: encoded.length);
114:
115: HandshakeIODataStream in = new HandshakeIODataStream();
116: in.append(encoded);
117: ClientKeyExchange message_2 = new ClientKeyExchange(in,
118: message.length(), isTLS[i], false);
119:
120: assertEquals("Incorrect message decoding",
121: message.length(), message_2.length());
122: assertTrue("Incorrect message decoding", Arrays.equals(
123: message.exchange_keys, message_2.exchange_keys));
124:
125: in.append(encoded);
126: try {
127: message_2 = new ClientKeyExchange(in,
128: message.length() - 1, isTLS[i], false);
129: fail("Small length: No expected AlertException");
130: } catch (AlertException e) {
131: }
132:
133: in.append(encoded);
134: in.append(new byte[] { 1, 2, 3 });
135: try {
136: message_2 = new ClientKeyExchange(in,
137: message.length() + 3, isTLS[i], false);
138: fail("Extra bytes: No expected AlertException");
139: } catch (AlertException e) {
140: }
141: }
142: }
143:
144: /*
145: * Test for void ClientKeyExchange()
146: */
147: public void testClientKeyExchange() throws Exception {
148:
149: ClientKeyExchange message = new ClientKeyExchange();
150: assertEquals("incorrect type", Handshake.CLIENT_KEY_EXCHANGE,
151: message.getType());
152: assertEquals("incorrect ClientKeyExchange", 0,
153: message.exchange_keys.length);
154: assertEquals("incorrect ClientKeyExchange", 0, message.length());
155: assertTrue("incorrect ClientKeyExchange", message.isEmpty());
156:
157: HandshakeIODataStream out = new HandshakeIODataStream();
158: message.send(out);
159: byte[] encoded = out.getData(1000);
160: assertEquals("incorrect ClientKeyExchange", 0, message.length());
161: assertEquals("incorrect out data length", message.length(),
162: encoded.length);
163:
164: HandshakeIODataStream in = new HandshakeIODataStream();
165: in.append(encoded);
166: ClientKeyExchange message_2 = new ClientKeyExchange(in, message
167: .length(), true, false);
168:
169: assertEquals("Incorrect message decoding", 0,
170: message_2.exchange_keys.length);
171: assertEquals("Incorrect message decoding", 0, message_2
172: .length());
173: assertTrue("Incorrect message decoding", message_2.isEmpty());
174:
175: in.append(encoded);
176: try {
177: message_2 = new ClientKeyExchange(in, message.length() - 1,
178: true, false);
179: fail("Small length: No expected IOException");
180: } catch (IOException e) {
181: }
182:
183: in.append(encoded);
184: in.append(new byte[] { 1, 2, 3 });
185: try {
186: message_2 = new ClientKeyExchange(in, message.length() + 3,
187: true, false);
188: fail("Extra bytes: No expected IOException");
189: } catch (IOException e) {
190: }
191: }
192:
193: }
|