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