Path: blob/master/test/jdk/sun/security/ssl/SocketCreation/SocketCreation.java
41152 views
/*1* Copyright (c) 2001, 2016, 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 441484326* @summary This test tries all the different ways in which an SSL27* connection can be established to exercise different SSLSocketImpl28* constructors.29* @run main/othervm/timeout=300 SocketCreation30*31* SunJSSE does not support dynamic system properties, no way to re-use32* system properties in samevm/agentvm mode.33*/3435import java.io.*;36import java.net.*;37import javax.net.ssl.*;3839/**40* This test has been adapted from JSSEClientServerTemplate.java. It runs41* the client and server multiple times while it iterates through the42* different ways in which an SSL connection can be established.43*44* The meat of this test is contained in doClientSide() and45* doServerSide(). The loop is contained in the constructor46* SocketCreation().47*/48public class SocketCreation {4950/*51* =============================================================52* Set the various variables needed for the tests, then53* specify what tests to run on each side.54*/5556/*57* Should we run the client or server in a separate thread?58* Both sides can throw exceptions, but do you have a preference59* as to which side should be the main thread.60*/61static boolean separateServerThread = true;6263/*64* Where do we find the keystores?65*/66static String pathToStores = "../../../../javax/net/ssl/etc";67static String keyStoreFile = "keystore";68static String trustStoreFile = "truststore";69static String passwd = "passphrase";7071/*72* Is the server ready to serve?73*/74volatile static boolean serverReady = false;7576/*77* Turn on SSL debugging?78*/79static boolean debug = false;8081/*82* If the client or server is doing some kind of object creation83* that the other side depends on, and that thread prematurely84* exits, you may experience a hang. The test harness will85* terminate all hung threads after its timeout has expired,86* currently 3 minutes by default, but you might try to be87* smart about it....88*/8990/*91* Accepts a connection from a client and exchanges an int with it. The92* connection can be established in one of three different ways:93* 1. As a regular SSL server socket94* 2. As an SSL server socket that is first unbound95* 3. As an SSL socket layered over a regular TCP/IP socket96*97* If the server prematurely exits, serverReady will be set to true98* to avoid infinite hangs.99*/100void doServerSide(int style) throws Exception {101102Socket sslSocket = null;103104// Change the for loop in SocketCreation() if you add more cases105switch (style) {106case 0:107sslSocket = acceptNormally0();108break;109case 1:110sslSocket = acceptNormally1();111break;112case 2:113sslSocket = acceptNormally2();114break;115case 3:116sslSocket = acceptUnbound();117break;118case 4:119sslSocket = acceptLayered();120break;121default:122throw new Exception("Incorrectly written test for server side!");123}124125InputStream sslIS = sslSocket.getInputStream();126OutputStream sslOS = sslSocket.getOutputStream();127128System.out.println("Server read: " + sslIS.read());129sslOS.write(85);130sslOS.flush();131132sslSocket.close();133}134135private Socket acceptNormally0() throws Exception {136137SSLServerSocketFactory sslssf =138(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();139140System.out.println("Server: Will call createServerSocket(int)");141ServerSocket sslServerSocket = sslssf.createServerSocket(0);142serverPort = sslServerSocket.getLocalPort();143144System.out.println("Server: Will accept on SSL server socket...");145146serverReady = true;147148Socket sslSocket = sslServerSocket.accept();149sslServerSocket.close();150return sslSocket;151}152153private Socket acceptNormally1() throws Exception {154155SSLServerSocketFactory sslssf =156(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();157158System.out.println("Server: Will call createServerSocket(int, int)");159ServerSocket sslServerSocket = sslssf.createServerSocket(0,1601);161serverPort = sslServerSocket.getLocalPort();162163System.out.println("Server: Will accept on SSL server socket...");164165serverReady = true;166167Socket sslSocket = sslServerSocket.accept();168sslServerSocket.close();169return sslSocket;170}171172private Socket acceptNormally2() throws Exception {173174SSLServerSocketFactory sslssf =175(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();176177System.out.println("Server: Will call createServerSocket(int, " +178" int, InetAddress)");179ServerSocket sslServerSocket = sslssf.createServerSocket(0,1801,181InetAddress.getByName("localhost"));182serverPort = sslServerSocket.getLocalPort();183184System.out.println("Server: Will accept on SSL server socket...");185186serverReady = true;187188Socket sslSocket = sslServerSocket.accept();189sslServerSocket.close();190return sslSocket;191}192193private Socket acceptUnbound() throws Exception {194195SSLServerSocketFactory sslssf =196(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();197198System.out.println("Server: Will create unbound SSL server socket...");199200ServerSocket sslServerSocket = sslssf.createServerSocket();201202if (sslServerSocket.isBound())203throw new Exception("Server socket is already bound!");204205sslServerSocket.bind(new java.net.InetSocketAddress(0));206207if (!sslServerSocket.isBound())208throw new Exception("Server socket is not bound!");209210serverPort = sslServerSocket.getLocalPort();211System.out.println("Server: Bound SSL server socket to port " +212serverPort + "...");213214serverReady = true;215216System.out.println("Server: Will accept on SSL server socket...");217Socket sslSocket = sslServerSocket.accept();218sslServerSocket.close();219return sslSocket;220}221222private Socket acceptLayered() throws Exception {223224SSLSocketFactory sslsf =225(SSLSocketFactory) SSLSocketFactory.getDefault();226227ServerSocket ss = new ServerSocket(0);228serverPort = ss.getLocalPort();229System.out.println("Server: Created normal server socket bound"230+ " to port " + serverPort + "...");231System.out.println("Server: Will accept on server socket...");232serverReady = true;233Socket s = ss.accept();234ss.close();235System.out.println("Server: Will layer SSLSocket on top of" +236" server socket...");237SSLSocket sslSocket =238(SSLSocket) sslsf.createSocket(s,239s.getInetAddress().getHostName(),240s.getPort(),241true);242sslSocket.setUseClientMode(false);243244return sslSocket;245}246247/*248* Connects to a server and exchanges an int with it. The249* connection can be established in one of three different ways:250* 1. As a regular SSL socket251* 2. As an SSL socket that is first unconnected252* 3. As an SSL socket layered over a regular TCP/IP socket253*254* If the server prematurely exits, serverReady will be set to true255* to avoid infinite hangs.256*/257void doClientSide(int style) throws Exception {258259Socket sslSocket = null;260261/*262* Wait for server to get started.263*/264while (!serverReady) {265Thread.sleep(50);266}267268// Change the for loop in SocketCreation() if you add more cases269switch (style) {270case 0:271sslSocket = connectNormally0();272break;273case 1:274sslSocket = connectNormally1();275break;276case 2:277sslSocket = connectNormally2();278break;279case 3:280sslSocket = connectNormally3();281break;282case 4:283sslSocket = connectUnconnected();284break;285case 5:286sslSocket = connectLayered();287break;288default:289throw new Exception("Incorrectly written test for client side!");290}291292InputStream sslIS = sslSocket.getInputStream();293OutputStream sslOS = sslSocket.getOutputStream();294295sslOS.write(280);296sslOS.flush();297System.out.println("Client read: " + sslIS.read());298299sslSocket.close();300}301302private Socket connectNormally0() throws Exception {303304SSLSocketFactory sslsf =305(SSLSocketFactory) SSLSocketFactory.getDefault();306307System.out.println("Client: Will call createSocket(String, int)");308return sslsf.createSocket("localhost", serverPort);309}310311private Socket connectNormally1() throws Exception {312313SSLSocketFactory sslsf =314(SSLSocketFactory) SSLSocketFactory.getDefault();315316System.out.println("Client: Will call createSocket(InetAddress, int)");317return sslsf.createSocket(InetAddress.getByName("localhost"),318serverPort);319}320321private Socket connectNormally2() throws Exception {322323SSLSocketFactory sslsf =324(SSLSocketFactory) SSLSocketFactory.getDefault();325326System.out.println("Client: Will call createSocket(String," +327" int, InetAddress, int)");328return sslsf.createSocket("localhost", serverPort,329InetAddress.getByName("localhost"),3300);331}332333private Socket connectNormally3() throws Exception {334335SSLSocketFactory sslsf =336(SSLSocketFactory) SSLSocketFactory.getDefault();337338System.out.println("Client: Will call createSocket(InetAddress," +339" int, InetAddress, int)");340return sslsf.createSocket(InetAddress.getByName("localhost"),341serverPort,342InetAddress.getByName("localhost"),3430);344}345346private Socket connectUnconnected() throws Exception {347348SSLSocketFactory sslsf =349(SSLSocketFactory) SSLSocketFactory.getDefault();350351System.out.println("Client: Will call createSocket()");352Socket sslSocket = sslsf.createSocket();353354if (sslSocket.isConnected())355throw new Exception("Client socket is already connected!");356357System.out.println("Client: Will connect to server on port " +358serverPort + "...");359sslSocket.connect(new java.net.InetSocketAddress("localhost",360serverPort));361362if (!sslSocket.isConnected())363throw new Exception("Client socket is not connected!");364365return sslSocket;366}367368private Socket connectLayered() throws Exception {369370SSLSocketFactory sslsf =371(SSLSocketFactory) SSLSocketFactory.getDefault();372373System.out.println("Client: Will connect to server on port " +374serverPort + "...");375Socket s = new Socket("localhost", serverPort);376377System.out.println("Client: Will layer SSL socket on top...");378return sslsf.createSocket(s, "localhost", serverPort, true);379380}381382/*383* =============================================================384* The remainder is just support stuff385*/386387// use any free port by default388volatile int serverPort = 0;389390volatile Exception serverException = null;391volatile Exception clientException = null;392393public static void main(String[] args) throws Exception {394String keyFilename =395System.getProperty("test.src", "./") + "/" + pathToStores +396"/" + keyStoreFile;397String trustFilename =398System.getProperty("test.src", "./") + "/" + pathToStores +399"/" + trustStoreFile;400401System.setProperty("javax.net.ssl.keyStore", keyFilename);402System.setProperty("javax.net.ssl.keyStorePassword", passwd);403System.setProperty("javax.net.ssl.trustStore", trustFilename);404System.setProperty("javax.net.ssl.trustStorePassword", passwd);405406if (debug)407System.setProperty("javax.net.debug", "all");408409/*410* Start the tests.411*/412new SocketCreation();413}414415Thread clientThread = null;416Thread serverThread = null;417418/*419* Primary constructor, used to drive remainder of the test.420*421* Performs a loop where each iteration establishes one client-server422* connection using a particular way of socket creation. There are423* three ways in each side can create a socket:424* 1. Normal (The client has 4 variations of this and the server 3)425* 2. Unbound/Unconnected426* 3. Layered427* Each side goes through all three of them giving us a total of 5x6428* possibilites.429*/430SocketCreation() throws Exception {431432for (int serverStyle = 0; serverStyle < 5; serverStyle++) {433System.out.println("-------------------------------------");434for (int clientStyle = 0; clientStyle < 6; clientStyle++) {435436serverReady = false;437438startServer(separateServerThread, serverStyle);439startClient(!separateServerThread, clientStyle);440441/*442* Wait for other side to close down.443*/444if (separateServerThread) {445serverThread.join();446} else {447clientThread.join();448}449450/*451* When we get here, the test is pretty much over.452*453* If the main thread excepted, that propagates back454* immediately. If the other thread threw an exception, we455* should report back.456*/457if (serverException != null)458throw serverException;459if (clientException != null)460throw clientException;461System.out.println();462}463}464}465466void startServer(boolean newThread, final int style) throws Exception {467if (newThread) {468serverThread = new Thread() {469public void run() {470try {471doServerSide(style);472} catch (Exception e) {473/*474* Our server thread just died.475*476* Release the client, if not active already...477*/478System.err.println("Server died..." + e);479serverReady = true;480serverException = e;481}482}483};484serverThread.start();485} else {486doServerSide(style);487}488}489490void startClient(boolean newThread, final int style) throws Exception {491if (newThread) {492clientThread = new Thread() {493public void run() {494try {495doClientSide(style);496} catch (Exception e) {497/*498* Our client thread just died.499*/500System.err.println("Client died...");501clientException = e;502}503}504};505clientThread.start();506} else {507doClientSide(style);508}509}510}511512513