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 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>ServerHello</code> constructor and methods 28 * 29 */ 30 public class ServerHelloTest extends TestCase { 31 32 public void testServerHello() throws Exception { 33 byte[] session_id = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 }; 34 CipherSuite cipher_suite = CipherSuite.TLS_DH_DSS_WITH_DES_CBC_SHA; 35 byte[] server_version = new byte[] { 3, 1 }; 36 ServerHello message = new ServerHello(new SecureRandom(), 37 server_version, session_id, cipher_suite, (byte) 0); 38 assertEquals("incorrect type", Handshake.SERVER_HELLO, message 39 .getType()); 40 41 assertTrue("incorrect CertificateRequest", Arrays.equals( 42 message.server_version, server_version)); 43 assertTrue("incorrect CertificateRequest", Arrays.equals( 44 message.session_id, session_id)); 45 assertEquals("incorrect CertificateRequest", cipher_suite, 46 message.cipher_suite); 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 ServerHello message_2 = new ServerHello(in, message.length()); 57 58 assertTrue("incorrect message decoding", Arrays.equals( 59 message.server_version, message_2.server_version)); 60 assertTrue("incorrect message decoding", Arrays.equals( 61 message.session_id, message_2.session_id)); 62 assertTrue("incorrect message decoding", Arrays.equals(message.random, 63 message_2.random)); 64 assertEquals("incorrect message decoding", message.cipher_suite, 65 message_2.cipher_suite); 66 67 in.append(encoded); 68 try { 69 new ServerHello(in, message.length() - 1); 70 fail("Small length: No expected AlertException"); 71 } catch (AlertException e) { 72 } 73 74 in.append(encoded); 75 in.append(new byte[] { 1, 2, 3 }); 76 try { 77 new ServerHello(in, message.length() + 3); 78 fail("Extra bytes: No expected AlertException "); 79 } catch (AlertException e) { 80 } 81 } 82 83 } 84