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.File; 21 import java.io.FileInputStream; 22 import java.security.KeyStore; 23 import java.security.SecureRandom; 24 25 import javax.net.ssl.KeyManagerFactory; 26 import javax.net.ssl.TrustManagerFactory; 27 28 import junit.framework.TestCase; 29 30 /** 31 * Tests for <code>ServerHandshakeImpl</code> constructor and methods 32 * 33 */ 34 public class ServerHandshakeImplTest extends TestCase { 35 // to store initialization Exception 36 private static Exception initException; 37 38 39 private SSLParameters sslParameters; 40 private ServerHandshakeImpl server; 41 42 @Override 43 public void setUp() throws Exception { 44 char[] pwd = JSSETestData.KS_PASSWORD; 45 KeyStore ks = JSSETestData.getKeyStore(); 46 47 KeyManagerFactory kmf = KeyManagerFactory.getInstance("X509"); 48 kmf.init(ks, pwd); 49 50 TrustManagerFactory tmf = TrustManagerFactory.getInstance("X509"); 51 tmf.init(ks); 52 53 sslParameters = new SSLParameters(kmf.getKeyManagers(), tmf 54 .getTrustManagers(), new SecureRandom(), 55 new SSLSessionContextImpl(), new SSLSessionContextImpl()); 56 57 server = new ServerHandshakeImpl(new SSLEngineImpl(sslParameters)); 58 59 SSLEngineAppData appData = new SSLEngineAppData(); 60 AlertProtocol alertProtocol = new AlertProtocol(); 61 SSLBufferedInput recProtIS = new SSLBufferedInput(); 62 SSLRecordProtocol recordProtocol = new SSLRecordProtocol(server, 63 alertProtocol, recProtIS, appData); 64 } 65 66 public void testUnwrap() { 67 byte[] ses_id = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 }; 68 byte[] version = new byte[] { 3, 1 }; 69 CipherSuite[] cipher_suite = new CipherSuite[] { 70 CipherSuite.TLS_RSA_WITH_RC4_128_MD5 }; 71 ClientHello message = new ClientHello(new SecureRandom(), version, 72 ses_id, cipher_suite); 73 HandshakeIODataStream out = new HandshakeIODataStream(); 74 out.writeUint8(message.getType()); 75 out.writeUint24(message.length()); 76 message.send(out); 77 byte[] encodedClientHello = out.getData(1000); 78 79 // ---------------------------------------- 80 // unwrap client hello (full handshake) 81 // precondition: session hash does not contains requested session 82 server.unwrap(encodedClientHello); 83 server.getTask().run(); // process client hello in delegated task 84 server.wrap(); // modelling of server respond sending 85 86 assertFalse(server.isResuming); 87 88 // unwrap unexpected second client hello 89 try { 90 server.unwrap(encodedClientHello); 91 fail("No expected AlertException"); 92 } catch (AlertException e) { 93 } 94 95 // unexpected ChangeCipherSpec 96 try { 97 server.receiveChangeCipherSpec(); 98 fail("No expected AlertException"); 99 } catch (AlertException e) { 100 } 101 102 // ---------------------------------------- 103 // unwrap client hello (abbreviated handshake) 104 // precondition: session hash contains requested session 105 clearServerData(); 106 SSLSessionImpl session = new SSLSessionImpl( 107 CipherSuite.TLS_RSA_WITH_RC4_128_MD5, new SecureRandom()); 108 session.id = ses_id; 109 // put session to hash 110 server.parameters.getServerSessionContext().putSession(session); 111 112 server.unwrap(encodedClientHello); 113 server.getTask().run(); // process client hello in delegated task 114 server.wrap(); // modelling of server respond sending 115 116 assertTrue(server.isResuming); 117 118 server.makeFinished(); // complete handshake 119 120 // expected ChangeCipherSpec 121 server.receiveChangeCipherSpec(); 122 } 123 124 public void testServerHandshakeImpl() { 125 assertEquals(server.status, HandshakeProtocol.NEED_UNWRAP); 126 assertTrue(server.nonBlocking); 127 assertSame(server.parameters.getKeyManager(), sslParameters 128 .getKeyManager()); 129 assertSame(server.parameters.getTrustManager(), sslParameters 130 .getTrustManager()); 131 assertNotNull(server.engineOwner); 132 assertNull(server.socketOwner); 133 } 134 135 private void clearServerData() { 136 server.clearMessages(); 137 server.io_stream = new HandshakeIODataStream(); 138 server.status = HandshakeProtocol.NEED_UNWRAP; 139 } 140 } 141