01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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:
18: package org.apache.harmony.xnet.provider.jsse;
19:
20: import java.io.IOException;
21: import java.security.SecureRandom;
22: import java.util.Arrays;
23:
24: import junit.framework.TestCase;
25:
26: /**
27: * Tests for <code>ClientHello</code> constructor and methods
28: *
29: */
30: public class ClientHelloTest extends TestCase {
31:
32: /*
33: * Test for ClientHello(SecureRandom, byte[], byte[], CipherSuite[]),
34: * ClientHello(HandshakeIODataStream, int), getType(), getRandom(), and
35: * send();
36: */
37: public void testClientHello() throws Exception {
38: byte[] ses_id = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
39: byte[] version = new byte[] { 3, 1 };
40: CipherSuite[] cipher_suite = new CipherSuite[] { CipherSuite.TLS_RSA_WITH_RC4_128_MD5 };
41: ClientHello message = new ClientHello(new SecureRandom(),
42: version, ses_id, cipher_suite);
43: assertEquals("incorrect type", Handshake.CLIENT_HELLO, message
44: .getType());
45: assertEquals("incorrect length", 51, message.length());
46: assertEquals("incorrect random", 32, message.getRandom().length);
47:
48: HandshakeIODataStream out = new HandshakeIODataStream();
49: message.send(out);
50: byte[] encoded = out.getData(1000);
51: assertEquals("incorrect out data length", message.length(),
52: encoded.length);
53:
54: HandshakeIODataStream in = new HandshakeIODataStream();
55: in.append(encoded);
56: ClientHello message_2 = new ClientHello(in, message.length());
57:
58: assertTrue("Incorrect message decoding", Arrays.equals(
59: message.client_version, message_2.client_version));
60: assertTrue("Incorrect message decoding", Arrays.equals(message
61: .getRandom(), message_2.getRandom()));
62:
63: in.append(encoded);
64: try {
65: message_2 = new ClientHello(in, message.length() - 1);
66: fail("Small length: No expected AlertException");
67: } catch (AlertException e) {
68: }
69:
70: in.append(encoded);
71: try {
72: message_2 = new ClientHello(in, message.length() + 1);
73: fail("Big length: No expected IO exception");
74: } catch (IOException e) {
75: }
76:
77: in.append(encoded);
78: in.append(new byte[] { 1, 2, 3 });
79: new ClientHello(in, message.length() + 3); // extra bytes must be
80: // ignored
81: }
82:
83: }
|