Home | History | Annotate | Download | only in jsse
      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.security.KeyManagementException;
     21 import java.security.KeyPairGenerator;
     22 import java.security.PublicKey;
     23 import java.security.SecureRandom;
     24 
     25 import javax.net.ssl.SSLEngineResult;
     26 
     27 import junit.framework.TestCase;
     28 
     29 /**
     30  * Tests for <code>HandshakeProtocol</code> constructor and methods
     31  *
     32  */
     33 public class HandshakeProtocolTest extends TestCase {
     34 
     35     public void testGetStatus() throws Exception {
     36         HandshakeProtocol protocol = new ClientHandshakeImpl(new SSLEngineImpl(
     37                 new SSLParameters(null, null, null,
     38                         new SSLSessionContextImpl(),
     39                         new SSLSessionContextImpl())));
     40 
     41         assertEquals(protocol.getStatus(),
     42                 SSLEngineResult.HandshakeStatus.NOT_HANDSHAKING);
     43 
     44         protocol.status = HandshakeProtocol.NEED_UNWRAP;
     45         assertEquals(protocol.getStatus(),
     46                 SSLEngineResult.HandshakeStatus.NEED_UNWRAP);
     47 
     48         protocol.status = HandshakeProtocol.FINISHED;
     49         assertEquals(protocol.getStatus(),
     50                 SSLEngineResult.HandshakeStatus.FINISHED);
     51         assertEquals(protocol.status, HandshakeProtocol.NOT_HANDSHAKING);
     52 
     53         protocol.delegatedTaskErr = new Exception();
     54         assertEquals(protocol.getStatus(),
     55                 SSLEngineResult.HandshakeStatus.NEED_WRAP);
     56         protocol.delegatedTaskErr = null;
     57 
     58         protocol.delegatedTasks.add(new DelegatedTask(null, null, null));
     59         assertEquals(protocol.getStatus(),
     60                 SSLEngineResult.HandshakeStatus.NEED_TASK);
     61         protocol.delegatedTasks.clear();
     62 
     63         protocol.io_stream.write(new byte[] { 1, 2, 3 });
     64         assertEquals(protocol.getStatus(),
     65                 SSLEngineResult.HandshakeStatus.NEED_WRAP);
     66     }
     67 
     68     public void testSendChangeCipherSpec() throws Exception {
     69         HandshakeProtocol protocol = new ServerHandshakeImpl(new SSLEngineImpl(
     70                 new SSLParameters(null, null, null,
     71                         new SSLSessionContextImpl(),
     72                         new SSLSessionContextImpl())));
     73 
     74         protocol.sendChangeCipherSpec();
     75         assertEquals(protocol.getStatus(),
     76                 SSLEngineResult.HandshakeStatus.NEED_WRAP);
     77     }
     78 
     79     public void testWrap() throws Exception {
     80         HandshakeProtocol protocol = new ClientHandshakeImpl(new SSLEngineImpl(
     81                 new SSLParameters(null, null, null,
     82                         new SSLSessionContextImpl(),
     83                         new SSLSessionContextImpl())));
     84 
     85         assertNull(protocol.wrap());
     86 
     87         protocol.delegatedTaskErr = new Exception();
     88         try {
     89             protocol.wrap();
     90             fail("No expected AlertException");
     91         } catch (AlertException e) {
     92             assertEquals(e.getDescriptionCode(),
     93                     AlertProtocol.HANDSHAKE_FAILURE);
     94             assertNull(protocol.delegatedTaskErr);
     95         }
     96     }
     97 
     98     public void testcomputerVerifyDataTLS() throws Exception {
     99         HandshakeProtocol hs_protocol = new ClientHandshakeImpl(
    100                 new SSLEngineImpl(new SSLParameters(null, null, null,
    101                         new SSLSessionContextImpl(),
    102                         new SSLSessionContextImpl())));
    103 
    104         SecureRandom sr = new SecureRandom();
    105         SSLSessionImpl ses = new SSLSessionImpl(sr);
    106         hs_protocol.session = ses;
    107         hs_protocol.session.protocol = ProtocolVersion.TLSv1;
    108         assertSame(hs_protocol.getSession(), ses);
    109 
    110         hs_protocol.clientHello = new ClientHello(
    111                 sr,
    112                 hs_protocol.session.protocol.version,
    113                 hs_protocol.session.id,
    114                 new CipherSuite[] { CipherSuite.TLS_DH_DSS_EXPORT_WITH_DES40_CBC_SHA });
    115         hs_protocol.serverHello = new ServerHello(sr,
    116                 hs_protocol.session.protocol.version, hs_protocol.session.id,
    117                 CipherSuite.TLS_DH_DSS_EXPORT_WITH_DES40_CBC_SHA, (byte) 0);
    118 
    119         hs_protocol.preMasterSecret = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
    120         hs_protocol.computerMasterSecret();
    121         assertNull(hs_protocol.preMasterSecret);
    122         assertEquals(48, hs_protocol.session.master_secret.length);
    123 
    124         hs_protocol.send(hs_protocol.clientHello);
    125         hs_protocol.send(hs_protocol.serverHello);
    126 
    127         hs_protocol.computerReferenceVerifyDataTLS("test");
    128 
    129         byte[] data = new byte[12];
    130         hs_protocol.computerVerifyDataTLS("test", data);
    131 
    132         hs_protocol.verifyFinished(data);
    133 
    134         try {
    135             hs_protocol.verifyFinished(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9,
    136                     0, 1, 2 });
    137             fail("No expected AlertException");
    138         } catch (AlertException e) {
    139         }
    140     }
    141 
    142     public void testComputerReferenceVerifyDataSSLv3() throws Exception {
    143         HandshakeProtocol hs_protocol = new ClientHandshakeImpl(
    144                 new SSLEngineImpl(new SSLParameters(null, null, null,
    145                         new SSLSessionContextImpl(),
    146                         new SSLSessionContextImpl())));
    147 
    148         SecureRandom sr = new SecureRandom();
    149         SSLSessionImpl ses = new SSLSessionImpl(sr);
    150         hs_protocol.session = ses;
    151         hs_protocol.session.protocol = ProtocolVersion.SSLv3;
    152         assertSame(hs_protocol.getSession(), ses);
    153 
    154         hs_protocol.clientHello = new ClientHello(
    155                 sr,
    156                 hs_protocol.session.protocol.version,
    157                 hs_protocol.session.id,
    158                 new CipherSuite[] { CipherSuite.TLS_DH_DSS_EXPORT_WITH_DES40_CBC_SHA });
    159         hs_protocol.serverHello = new ServerHello(sr,
    160                 hs_protocol.session.protocol.version, hs_protocol.session.id,
    161                 CipherSuite.TLS_DH_DSS_EXPORT_WITH_DES40_CBC_SHA, (byte) 0);
    162 
    163         hs_protocol.preMasterSecret = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
    164         hs_protocol.computerMasterSecret();
    165         assertNull(hs_protocol.preMasterSecret);
    166         assertEquals(48, hs_protocol.session.master_secret.length);
    167 
    168         hs_protocol.send(hs_protocol.clientHello);
    169         hs_protocol.send(hs_protocol.serverHello);
    170 
    171         hs_protocol.computerReferenceVerifyDataSSLv3(SSLv3Constants.client);
    172 
    173         byte[] data = new byte[36];
    174         hs_protocol.computerVerifyDataSSLv3(SSLv3Constants.client, data);
    175 
    176         hs_protocol.verifyFinished(data);
    177 
    178         try {
    179             hs_protocol.verifyFinished(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9,
    180                     0, 1, 2 });
    181             fail("No expected AlertException");
    182         } catch (AlertException e) {
    183         }
    184     }
    185 
    186     public void testUnexpectedMessage() throws Exception {
    187         HandshakeProtocol protocol = new ClientHandshakeImpl(new SSLEngineImpl(
    188                 new SSLParameters(null, null, null,
    189                         new SSLSessionContextImpl(),
    190                         new SSLSessionContextImpl())));
    191         try {
    192             protocol.unexpectedMessage();
    193             fail("No expected AlertException");
    194         } catch (AlertException e) {
    195             assertEquals(e.getDescriptionCode(),
    196                     AlertProtocol.UNEXPECTED_MESSAGE);
    197         }
    198     }
    199 
    200     public void testGetTask() throws Exception {
    201         HandshakeProtocol protocol = new ClientHandshakeImpl(new SSLEngineImpl(
    202                 new SSLParameters(null, null, null,
    203                         new SSLSessionContextImpl(),
    204                         new SSLSessionContextImpl())));
    205 
    206         DelegatedTask task = new DelegatedTask(null, null, null);
    207         protocol.delegatedTasks.add(task);
    208         assertSame(protocol.getTask(), task);
    209         assertNull(protocol.getTask());
    210     }
    211 
    212     public void testGetRSAKeyLength() throws Exception {
    213         KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
    214         kpg.initialize(512);
    215         PublicKey key = kpg.genKeyPair().getPublic();
    216 
    217         assertEquals(512, HandshakeProtocol.getRSAKeyLength(key));
    218 
    219     }
    220 
    221 }