ClientHelloTest.java revision 8d8858e39800de641b50f6e8e864af9cf68bedea
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 */
29public class ClientHelloTest extends TestCase {
30
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[] {
41                CipherSuite.TLS_RSA_WITH_RC4_128_MD5 };
42        ClientHello message = new ClientHello(new SecureRandom(), version,
43                ses_id, cipher_suite);
44        assertEquals("incorrect type", Handshake.CLIENT_HELLO, message.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(), encoded.length);
52
53        HandshakeIODataStream in = new HandshakeIODataStream();
54        in.append(encoded);
55        ClientHello message_2 = new ClientHello(in, message.length());
56
57        assertTrue("Incorrect message decoding",
58                Arrays.equals(message.client_version, message_2.client_version));
59        assertTrue("Incorrect message decoding",
60                Arrays.equals(message.getRandom(), message_2.getRandom()));
61
62        in.append(encoded);
63        try {
64            message_2 = new ClientHello(in, message.length() - 1);
65            fail("Small length: No expected AlertException");
66        } catch (AlertException e) {
67        }
68
69        in.append(encoded);
70        try {
71            message_2 = new ClientHello(in, message.length() + 1);
72            fail("Big length: No expected IO exception");
73        } catch (IOException e) {
74        }
75
76        in.append(encoded);
77        in.append(new byte[] { 1, 2, 3 });
78        new ClientHello(in, message.length() + 3);    // extra bytes must be
79        // ignored
80    }
81
82}
83