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.tests.javax.net.ssl; 19 20 import java.io.IOException; 21 import java.net.ServerSocket; 22 import java.security.cert.Certificate; 23 24 import javax.net.ssl.HandshakeCompletedEvent; 25 import javax.net.ssl.SSLPeerUnverifiedException; 26 import javax.net.ssl.SSLSession; 27 import javax.net.ssl.SSLSocket; 28 import javax.net.ssl.SSLSocketFactory; 29 30 import junit.framework.TestCase; 31 32 /** 33 * Tests for <code>HandshakeCompletedEvent</code> class constructors and methods. 34 */ 35 public class HandshakeCompletedEventTest extends TestCase { 36 37 int port; 38 39 ServerSocket ss; 40 41 SSLSocket soc; 42 43 boolean noFreePort = false; 44 boolean noSocket = false; 45 46 /* 47 * @see TestCase#setUp() 48 */ 49 @Override 50 protected void setUp() throws Exception { 51 super.setUp(); 52 SSLSocketFactory sf = (SSLSocketFactory) SSLSocketFactory.getDefault(); 53 try { 54 ss = new ServerSocket(0); 55 port = ss.getLocalPort(); 56 } catch (Exception e) { 57 e.printStackTrace(); 58 noFreePort = true; 59 return; 60 } 61 try { 62 soc = (SSLSocket) sf.createSocket("localhost", port); 63 } catch (IOException e) { 64 noSocket = true; 65 } 66 67 } 68 69 /* 70 * @see TestCase#tearDown() 71 */ 72 @Override 73 protected void tearDown() throws Exception { 74 super.tearDown(); 75 if (ss != null) { 76 ss.close(); 77 } 78 if (soc != null) { 79 soc.close(); 80 } 81 } 82 83 public final void testGetCipherSuite() { 84 if (noFreePort || noSocket) { 85 return; 86 } 87 SSLSession ses = new MySSLSession(); 88 89 HandshakeCompletedEvent event = new HandshakeCompletedEvent(soc, ses); 90 91 assertEquals(event.getCipherSuite(), ses.getCipherSuite()); 92 } 93 94 public final void testGetLocalCertificates() { 95 if (noFreePort || noSocket) { 96 return; 97 } 98 SSLSession ses = new MySSLSession(); 99 HandshakeCompletedEvent event = new HandshakeCompletedEvent(soc, ses); 100 101 Certificate[] certs = event.getLocalCertificates(); 102 Certificate[] ses_certs = ses.getLocalCertificates(); 103 if (certs == null && ses_certs == null) { 104 return; 105 } 106 if (certs == null || ses_certs == null) { 107 fail("incorrect LocalCertificates"); 108 } 109 for (int i = 0; i < certs.length; i++) { 110 if (certs[i] != ses_certs[i]) { 111 fail("incorrect LocalCertificates"); 112 } 113 } 114 } 115 116 public final void testGetPeerCertificates() { 117 if (noFreePort || noSocket) { 118 return; 119 } 120 SSLSession ses = new MySSLSession(); 121 HandshakeCompletedEvent event = new HandshakeCompletedEvent(soc, ses); 122 try { 123 event.getPeerCertificates(); 124 fail("No excpected SSLPeerUnverifiedException"); 125 } catch (SSLPeerUnverifiedException e) { 126 } 127 } 128 129 public final void testGetPeerCertificateChain() { 130 if (noFreePort || noSocket) { 131 return; 132 } 133 SSLSession ses = new MySSLSession(); 134 HandshakeCompletedEvent event = new HandshakeCompletedEvent(soc, ses); 135 try { 136 event.getPeerCertificateChain(); 137 fail("No excpected SSLPeerUnverifiedException"); 138 } catch (SSLPeerUnverifiedException e) { 139 } 140 } 141 142 public final void testHandshakeCompletedEvent() { 143 if (noFreePort || noSocket) { 144 return; 145 } 146 SSLSession ses = new MySSLSession(); 147 HandshakeCompletedEvent event = new HandshakeCompletedEvent(soc, ses); 148 149 assertEquals(ses, event.getSession()); 150 assertEquals(soc, event.getSocket()); 151 } 152 } 153