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