Path: blob/master/test/jdk/sun/security/ssl/SSLContextImpl/GoodProvider.java
41152 views
/*1* Copyright (c) 2003, 2011, 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* @test25* @bug 491914726* @summary Support for token-based KeyStores27* @run main/othervm GoodProvider28*29* SunJSSE does not support dynamic system properties, no way to re-use30* system properties in samevm/agentvm mode.31*/3233import java.io.*;34import java.net.*;35import javax.net.ssl.*;3637public class GoodProvider {3839/*40* =============================================================41* Set the various variables needed for the tests, then42* specify what tests to run on each side.43*/4445/*46* Should we run the client or server in a separate thread?47* Both sides can throw exceptions, but do you have a preference48* as to which side should be the main thread.49*/50static boolean separateServerThread = false;5152/*53* Where do we find the keystores?54*/55static String pathToStores = "../../../../javax/net/ssl/etc";56static String keyStoreFile = "keystore";57static String trustStoreFile = "truststore";58static String passwd = "passphrase";5960/*61* Is the server ready to serve?62*/63volatile static boolean serverReady = false;6465/*66* Turn on SSL debugging?67*/68static boolean debug = false;6970/*71* If the client or server is doing some kind of object creation72* that the other side depends on, and that thread prematurely73* exits, you may experience a hang. The test harness will74* terminate all hung threads after its timeout has expired,75* currently 3 minutes by default, but you might try to be76* smart about it....77*/7879/*80* Define the server side of the test.81*82* If the server prematurely exits, serverReady will be set to true83* to avoid infinite hangs.84*/85void doServerSide() throws Exception {86SSLServerSocketFactory sslssf =87(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();88SSLServerSocket sslServerSocket =89(SSLServerSocket) sslssf.createServerSocket(serverPort);9091serverPort = sslServerSocket.getLocalPort();9293/*94* Signal Client, we're ready for his connect.95*/96serverReady = true;9798SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept();99InputStream sslIS = sslSocket.getInputStream();100OutputStream sslOS = sslSocket.getOutputStream();101102sslIS.read();103sslOS.write(85);104sslOS.flush();105106sslSocket.close();107}108109/*110* Define the client side of the test.111*112* If the server prematurely exits, serverReady will be set to true113* to avoid infinite hangs.114*/115void doClientSide() throws Exception {116117/*118* Wait for server to get started.119*/120while (!serverReady) {121Thread.sleep(50);122}123124SSLSocketFactory sslsf =125(SSLSocketFactory) SSLSocketFactory.getDefault();126SSLSocket sslSocket = (SSLSocket)127sslsf.createSocket("localhost", serverPort);128129InputStream sslIS = sslSocket.getInputStream();130OutputStream sslOS = sslSocket.getOutputStream();131132sslOS.write(280);133sslOS.flush();134sslIS.read();135136sslSocket.close();137}138139/*140* =============================================================141* The remainder is just support stuff142*/143144// use any free port by default145volatile int serverPort = 0;146147volatile Exception serverException = null;148volatile Exception clientException = null;149150public static void main(String[] args) throws Exception {151String keyFilename =152System.getProperty("test.src", "./") + "/" + pathToStores +153"/" + keyStoreFile;154String trustFilename =155System.getProperty("test.src", "./") + "/" + pathToStores +156"/" + trustStoreFile;157158// first test a good provider name159160System.setProperty("javax.net.ssl.keyStore", keyFilename);161System.setProperty("javax.net.ssl.keyStoreProvider", "SUN");162System.setProperty("javax.net.ssl.keyStorePassword", passwd);163System.setProperty("javax.net.ssl.trustStore", trustFilename);164System.setProperty("javax.net.ssl.trustStoreProvider", "SUN");165System.setProperty("javax.net.ssl.trustStorePassword", passwd);166167if (debug)168System.setProperty("javax.net.debug", "ssl,defaultctx");169170new GoodProvider();171}172173Thread clientThread = null;174Thread serverThread = null;175176/*177* Primary constructor, used to drive remainder of the test.178*179* Fork off the other side, then do your work.180*/181GoodProvider() throws Exception {182try {183if (separateServerThread) {184startServer(true);185startClient(false);186} else {187startClient(true);188startServer(false);189}190} catch (Exception e) {191//swallow for now. Show later192}193194/*195* Wait for other side to close down.196*/197if (separateServerThread) {198serverThread.join();199} else {200clientThread.join();201}202203/*204* When we get here, the test is pretty much over.205* Which side threw the error?206*/207Exception local;208Exception remote;209String whichRemote;210211if (separateServerThread) {212remote = serverException;213local = clientException;214whichRemote = "server";215} else {216remote = clientException;217local = serverException;218whichRemote = "client";219}220221/*222* If both failed, return the curthread's exception, but also223* print the remote side Exception224*/225if ((local != null) && (remote != null)) {226System.out.println(whichRemote + " also threw:");227remote.printStackTrace();228System.out.println();229throw local;230}231232if (remote != null) {233throw remote;234}235236if (local != null) {237throw local;238}239}240241void startServer(boolean newThread) throws Exception {242if (newThread) {243serverThread = new Thread() {244public void run() {245try {246doServerSide();247} catch (Exception e) {248/*249* Our server thread just died.250*251* Release the client, if not active already...252*/253System.err.println("Server died...");254serverReady = true;255serverException = e;256}257}258};259serverThread.start();260} else {261try {262doServerSide();263} catch (Exception e) {264serverException = e;265} finally {266serverReady = true;267}268}269}270271void startClient(boolean newThread) throws Exception {272if (newThread) {273clientThread = new Thread() {274public void run() {275try {276doClientSide();277} catch (Exception e) {278/*279* Our client thread just died.280*/281System.err.println("Client died...");282clientException = e;283}284}285};286clientThread.start();287} else {288try {289doClientSide();290} catch (Exception e) {291clientException = e;292}293}294}295}296297298