Path: blob/master/test/jdk/sun/security/ssl/SSLSocketImpl/NotifyHandshakeTest.java
41152 views
/*1* Copyright (c) 2002, 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* This class may have some race conditions that I haven't25* accounted for. I've tried to put in sufficient sleeps and triggers26* that should cause everything to run correctly.27*28* This was hackish, but to make sure that there were no problems29* with permissions.30*31* Create a client, server, and interested party thread. The32* client and interested threads should receive the same33* session notification.34*/35package com;3637import java.net.*;38import java.io.*;39import javax.net.ssl.*;40import java.security.cert.*;41import java.security.*;4243public class NotifyHandshakeTest implements HandshakeCompletedListener {4445static String pathToStores = "../../../../javax/net/ssl/etc";46static String keyStoreFile = "keystore";47static String trustStoreFile = "truststore";48static String passwd = "passphrase";49volatile static int serverPort = 0;5051public boolean set;52SSLSession sess;5354static public int triggerState = 0;5556static public void trigger() {57triggerState++;58}5960public void handshakeCompleted(HandshakeCompletedEvent event) {61set = true;62sess = event.getSession();63trigger();64}6566public static void main(String[] args) throws Exception {6768String keyFilename =69System.getProperty("test.src", "./") + "/" + pathToStores +70"/" + keyStoreFile;71String trustFilename =72System.getProperty("test.src", "./") + "/" + pathToStores +73"/" + trustStoreFile;7475System.setProperty("javax.net.ssl.keyStore", keyFilename);76System.setProperty("javax.net.ssl.keyStorePassword", passwd);77System.setProperty("javax.net.ssl.trustStore", trustFilename);78System.setProperty("javax.net.ssl.trustStorePassword", passwd);7980SSLSocketFactory sslsf =81(SSLSocketFactory)SSLSocketFactory.getDefault();82SSLServerSocketFactory sslssf =83(SSLServerSocketFactory)SSLServerSocketFactory.getDefault();8485/*86* Start off the Server, give time to initialize.87*/88SSLServerSocket sslss =89(SSLServerSocket)sslssf.createServerSocket(serverPort);90sslss.setSoTimeout(30000); // 30 seconds91serverPort = sslss.getLocalPort();92Server server = new Server(sslss);93server.start();9495System.out.println("Server started...");9697/*98* Create the socket.99*/100SSLSocket socket =101(SSLSocket)sslsf.createSocket("localhost", serverPort);102103/*104* Create a second thread also interested in this socket105*/106edu.NotifyHandshakeTestHeyYou heyYou =107new edu.NotifyHandshakeTestHeyYou(socket);108heyYou.start();109while (triggerState < 1) {110Thread.sleep(500);111}112System.out.println("HeyYou thread ready...");113114NotifyHandshakeTest listener = new NotifyHandshakeTest();115socket.addHandshakeCompletedListener(listener);116117System.out.println("Client starting handshake...");118socket.startHandshake();119System.out.println("Client done handshaking...");120121InputStream is = socket.getInputStream();122if ((byte)is.read() != (byte)0x77) {123throw new Exception("problem reading byte");124}125126/*127* Wait for HeyYou and the client to get a slice, so128* they can receive their SSLSessions.129*/130while (triggerState < 3) {131Thread.sleep(500);132}133134/*135* Grab the variables before reaping the thread.136*/137boolean heyYouSet = heyYou.set;138AccessControlContext heyYouACC = heyYou.acc;139SSLSession heyYouSess = heyYou.ssls;140141heyYou.interrupt();142heyYou.join();143server.join();144145socket.close();146147if (!heyYouSet) {148throw new Exception("HeyYou's wasn't set");149}150if (!listener.set) {151throw new Exception("This' wasn't set");152}153154if (heyYouACC.equals(AccessController.getContext())) {155throw new Exception("Access Control Contexts were the same");156}157158if (!heyYouSess.equals(listener.sess)) {159throw new Exception("SSLSessions were not equal");160}161162System.out.println("Everything Passed");163}164165static class Server extends Thread {166167SSLServerSocket ss;168169Server(SSLServerSocket ss) {170this.ss = ss;171}172173public void run() {174try {175System.out.println("Server accepting socket...");176SSLSocket s = (SSLSocket) ss.accept();177System.out.println(178"Server accepted socket...starting handshake");179s.startHandshake();180System.out.println("Server done handshaking");181OutputStream os = s.getOutputStream();182os.write(0x77);183os.flush();184System.out.println("Server returning");185} catch (Exception e) {186System.out.println("Server died");187e.printStackTrace();188}189}190}191}192193194