ClientHelloTest.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.io.IOException;
21import java.security.SecureRandom;
22import java.util.Arrays;
23
24import junit.framework.TestCase;
25
26/**
27 * Tests for <code>ClientHello</code> constructor and methods
28 *
29 */
30public class ClientHelloTest extends TestCase {
31
32
33	/*
34	 * Test for ClientHello(SecureRandom, byte[], byte[], CipherSuite[]),
35	 * ClientHello(HandshakeIODataStream, int), getType(), getRandom(), and
36	 * send();
37	 */
38	public void testClientHello() throws Exception {
39		byte[] ses_id = new byte[] {1,2,3,4,5,6,7,8,9,0};
40		byte[] version = new byte[] {3, 1 };
41		CipherSuite[] cipher_suite = new CipherSuite[] {
42                CipherSuite.TLS_RSA_WITH_RC4_128_MD5};
43		ClientHello message = new ClientHello(new SecureRandom(), version,
44				ses_id, cipher_suite);
45        assertEquals("incorrect type", Handshake.CLIENT_HELLO, message.getType());
46        assertEquals("incorrect length", 51, message.length());
47        assertEquals("incorrect random", 32, message.getRandom().length);
48
49		HandshakeIODataStream out = new HandshakeIODataStream();
50		message.send(out);
51		byte[] encoded = out.getData(1000);
52        assertEquals("incorrect out data length", message.length(), 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",
59                Arrays.equals(message.client_version, message_2.client_version));
60        assertTrue("Incorrect message decoding",
61                Arrays.equals(message.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}
84