Path: blob/master/test/jdk/javax/net/ssl/FixingJavadocs/ImplicitHandshake.java
41153 views
/*1* Copyright (c) 2001, 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 438788226* @summary Need to revisit the javadocs for JSSE, especially the27* promoted classes.28* @run main/othervm ImplicitHandshake29*30* SunJSSE does not support dynamic system properties, no way to re-use31* system properties in samevm/agentvm mode.32* @author Brad Wetmore33*/3435import java.io.*;36import java.net.*;37import javax.net.ssl.*;3839public class ImplicitHandshake {4041/*42* =============================================================43* Set the various variables needed for the tests, then44* specify what tests to run on each side.45*/4647/*48* Should we run the client or server in a separate thread?49* Both sides can throw exceptions, but do you have a preference50* as to which side should be the main thread.51*/52static boolean separateServerThread = true;5354/*55* Where do we find the keystores?56*/57static String pathToStores = "../etc";58static String keyStoreFile = "keystore";59static String trustStoreFile = "truststore";60static String passwd = "passphrase";6162/*63* Is the server ready to serve?64*/65volatile static boolean serverReady = false;6667/*68* Turn on SSL debugging?69*/70static boolean debug = false;7172/*73* If the client or server is doing some kind of object creation74* that the other side depends on, and that thread prematurely75* exits, you may experience a hang. The test harness will76* terminate all hung threads after its timeout has expired,77* currently 3 minutes by default, but you might try to be78* smart about it....79*/8081/*82* Define the server side of the test.83*84* If the server prematurely exits, serverReady will be set to true85* to avoid infinite hangs.86*/87void doServerSide() throws Exception {88SSLServerSocketFactory sslssf =89(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();90SSLServerSocket sslServerSocket =91(SSLServerSocket) sslssf.createServerSocket(serverPort);92serverPort = sslServerSocket.getLocalPort();9394/*95* Signal Client, we're ready for his connect.96*/97serverReady = true;9899SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept();100101/*102* For grins, let's mix it up here, to make sure you103* can have a server really be a client. Don't think104* we have a test that does this yet.105*/106sslSocket.setUseClientMode(true);107108InputStream sslIS = sslSocket.getInputStream();109OutputStream sslOS = sslSocket.getOutputStream();110111sslIS.read();112sslOS.write(85);113sslOS.flush();114115sslSocket.close();116}117118/*119* Define the client side of the test.120*121* If the server prematurely exits, serverReady will be set to true122* to avoid infinite hangs.123*/124void doClientSide() throws Exception {125126/*127* Wait for server to get started.128*/129while (!serverReady) {130Thread.sleep(50);131}132133SSLSocketFactory sslsf =134(SSLSocketFactory) SSLSocketFactory.getDefault();135SSLSocket sslSocket = (SSLSocket)136sslsf.createSocket("localhost", serverPort);137138/*139* This is bogus, we should no longer get an tho...140*/141sslSocket.setUseClientMode(true);142sslSocket.setNeedClientAuth(true);143144/*145* For grins, let's mix it up here, to make sure you146* can have a client really be a server. Don't think147* we have a test that does this yet.148*/149sslSocket.setUseClientMode(false);150151System.out.println("Using Implicit handshake, ciphersuite is: " +152sslSocket.getSession().getCipherSuite());153154InputStream sslIS = sslSocket.getInputStream();155OutputStream sslOS = sslSocket.getOutputStream();156157sslOS.write(280);158sslOS.flush();159sslIS.read();160161/*162* Lastly, checking that you can't change modes once you've163* started/completed handshaking.164*/165try {166sslSocket.setUseClientMode(true);167} catch (IllegalArgumentException e) {168System.out.println("Caught proper exception");169}170171sslSocket.close();172}173174/*175* =============================================================176* The remainder is just support stuff177*/178179// use any free port by default180volatile int serverPort = 0;181182volatile Exception serverException = null;183volatile Exception clientException = null;184185public static void main(String[] args) throws Exception {186String keyFilename =187System.getProperty("test.src", "./") + "/" + pathToStores +188"/" + keyStoreFile;189String trustFilename =190System.getProperty("test.src", "./") + "/" + pathToStores +191"/" + trustStoreFile;192193System.setProperty("javax.net.ssl.keyStore", keyFilename);194System.setProperty("javax.net.ssl.keyStorePassword", passwd);195System.setProperty("javax.net.ssl.trustStore", trustFilename);196System.setProperty("javax.net.ssl.trustStorePassword", passwd);197198if (debug)199System.setProperty("javax.net.debug", "all");200201/*202* Start the tests.203*/204new ImplicitHandshake();205}206207Thread clientThread = null;208Thread serverThread = null;209210/*211* Primary constructor, used to drive remainder of the test.212*213* Fork off the other side, then do your work.214*/215ImplicitHandshake() throws Exception {216if (separateServerThread) {217startServer(true);218startClient(false);219} else {220startClient(true);221startServer(false);222}223224/*225* Wait for other side to close down.226*/227if (separateServerThread) {228serverThread.join();229} else {230clientThread.join();231}232233/*234* When we get here, the test is pretty much over.235*236* If the main thread excepted, that propagates back237* immediately. If the other thread threw an exception, we238* should report back.239*/240if (serverException != null) {241System.out.print("Server Exception:");242throw serverException;243}244if (clientException != null) {245System.out.print("Client Exception:");246throw clientException;247}248}249250void startServer(boolean newThread) throws Exception {251if (newThread) {252serverThread = new Thread() {253public void run() {254try {255doServerSide();256} catch (Exception e) {257/*258* Our server thread just died.259*260* Release the client, if not active already...261*/262System.err.println("Server died...");263serverReady = true;264serverException = e;265}266}267};268serverThread.start();269} else {270doServerSide();271}272}273274void startClient(boolean newThread) throws Exception {275if (newThread) {276clientThread = new Thread() {277public void run() {278try {279doClientSide();280} catch (Exception e) {281/*282* Our client thread just died.283*/284System.err.println("Client died...");285clientException = e;286}287}288};289clientThread.start();290} else {291doClientSide();292}293}294}295296297