Path: blob/master/test/jdk/sun/security/ssl/SSLSocketImpl/ServerTimeout.java
41152 views
/*1* Copyright (c) 2005, 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 483649326* @summary Socket timeouts for SSLSockets causes data corruption.27* @run main/othervm ServerTimeout28*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 java.util.*;36import java.security.*;37import javax.net.ssl.*;3839public class ServerTimeout {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 = "../../../../javax/net/ssl/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);9293serverPort = sslServerSocket.getLocalPort();9495/*96* Signal Client, we're ready for his connect.97*/98serverReady = true;99100SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept();101InputStream sslIS = sslSocket.getInputStream();102OutputStream sslOS = sslSocket.getOutputStream();103sslSocket.startHandshake();104105// read application data from client106MessageDigest md = MessageDigest.getInstance("SHA");107DigestInputStream transIns = new DigestInputStream(sslIS, md);108byte[] bytes = new byte[2000];109sslSocket.setSoTimeout(100); // The stall timeout110while (true) {111try {112while (transIns.read(bytes, 0, 17) != -1);113break;114} catch (SocketTimeoutException e) {115System.out.println("Server inputStream Exception: "116+ e.getMessage());117}118}119// wait for client to get ready120while (clientDigest == null) {121Thread.sleep(20);122}123124byte[] srvDigest = md.digest();125if (!Arrays.equals(clientDigest, srvDigest)) {126throw new Exception("Application data trans error");127}128129transIns.close();130sslSocket.close();131}132133/*134* Define the client side of the test.135*136* If the server prematurely exits, serverReady will be set to true137* to avoid infinite hangs.138*/139void doClientSide() throws Exception {140boolean caught = false;141142/*143* Wait for server to get started.144*/145while (!serverReady) {146Thread.sleep(50);147}148149SSLSocketFactory sslsf =150(SSLSocketFactory) SSLSocketFactory.getDefault();151SSLSocket sslSocket = (SSLSocket)152sslsf.createSocket("localhost", serverPort);153154InputStream sslIS = sslSocket.getInputStream();155OutputStream sslOS = sslSocket.getOutputStream();156sslSocket.startHandshake();157158// transfer a file to server159String transFilename =160System.getProperty("test.src", "./") + "/" +161this.getClass().getName() + ".java";162MessageDigest md = MessageDigest.getInstance("SHA");163DigestInputStream transIns = new DigestInputStream(164new FileInputStream(transFilename), md);165byte[] bytes = new byte[2000];166int i = 0;167while (true) {168// reset the cycle169if (i >= bytes.length) {170i = 0;171}172173int length = transIns.read(bytes, 0, i++);174if (length == -1) {175break;176} else {177sslOS.write(bytes, 0, length);178sslOS.flush();179180if (i % 3 == 0) {181Thread.sleep(300); // Stall past the timeout...182}183}184}185clientDigest = md.digest();186transIns.close();187sslSocket.close();188}189190/*191* =============================================================192* The remainder is just support stuff193*/194195// use any free port by default196volatile int serverPort = 0;197198volatile Exception serverException = null;199volatile Exception clientException = null;200201volatile byte[] clientDigest = null;202203public static void main(String[] args) throws Exception {204String keyFilename =205System.getProperty("test.src", "./") + "/" + pathToStores +206"/" + keyStoreFile;207String trustFilename =208System.getProperty("test.src", "./") + "/" + pathToStores +209"/" + trustStoreFile;210211System.setProperty("javax.net.ssl.keyStore", keyFilename);212System.setProperty("javax.net.ssl.keyStorePassword", passwd);213System.setProperty("javax.net.ssl.trustStore", trustFilename);214System.setProperty("javax.net.ssl.trustStorePassword", passwd);215216if (debug)217System.setProperty("javax.net.debug", "all");218219/*220* Start the tests.221*/222new ServerTimeout();223}224225Thread clientThread = null;226Thread serverThread = null;227228/*229* Primary constructor, used to drive remainder of the test.230*231* Fork off the other side, then do your work.232*/233ServerTimeout() throws Exception {234if (separateServerThread) {235startServer(true);236startClient(false);237} else {238startClient(true);239startServer(false);240}241242/*243* Wait for other side to close down.244*/245if (separateServerThread) {246serverThread.join();247} else {248clientThread.join();249}250251/*252* When we get here, the test is pretty much over.253*254* If the main thread excepted, that propagates back255* immediately. If the other thread threw an exception, we256* should report back.257*/258if (serverException != null) {259System.out.print("Server Exception:");260throw serverException;261}262if (clientException != null) {263System.out.print("Client Exception:");264throw clientException;265}266}267268void startServer(boolean newThread) throws Exception {269if (newThread) {270serverThread = new Thread() {271public void run() {272try {273doServerSide();274} catch (Exception e) {275/*276* Our server thread just died.277*278* Release the client, if not active already...279*/280System.err.println("Server died...");281System.err.println(e);282serverReady = true;283serverException = e;284}285}286};287serverThread.start();288} else {289doServerSide();290}291}292293void startClient(boolean newThread) throws Exception {294if (newThread) {295clientThread = new Thread() {296public void run() {297try {298doClientSide();299} catch (Exception e) {300/*301* Our client thread just died.302*/303System.err.println("Client died...");304clientException = e;305}306}307};308clientThread.start();309} else {310doClientSide();311}312}313}314315316