Path: blob/master/test/jdk/sun/security/ssl/AppInputStream/ReadHandshake.java
41152 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// SunJSSE does not support dynamic system properties, no way to re-use25// system properties in samevm/agentvm mode.26//2728/*29* @test30* @bug 451497131* @summary Verify applications do not read handshake data after failure32* @run main/othervm ReadHandshake33*/3435import java.io.*;36import java.net.*;37import javax.net.ssl.*;38import java.security.Security;3940public class ReadHandshake {4142/*43* =============================================================44* Set the various variables needed for the tests, then45* specify what tests to run on each side.46*/4748/*49* Should we run the client or server in a separate thread?50* Both sides can throw exceptions, but do you have a preference51* as to which side should be the main thread.52*/53static boolean separateServerThread = true;5455// Note: we use anonymous ciphersuites only, no keys/ trusted certs needed5657private final static String[] CLIENT_SUITES = new String[] {58"SSL_DH_anon_WITH_3DES_EDE_CBC_SHA",59};6061private final static String[] SERVER_SUITES = new String[] {62"SSL_DH_anon_WITH_RC4_128_MD5",63};6465/*66* Is the server ready to serve?67*/68volatile static boolean serverReady = false;6970/*71* Turn on SSL debugging?72*/73static boolean debug = false;7475/*76* If the client or server is doing some kind of object creation77* that the other side depends on, and that thread prematurely78* exits, you may experience a hang. The test harness will79* terminate all hung threads after its timeout has expired,80* currently 3 minutes by default, but you might try to be81* smart about it....82*/8384/*85* Define the server side of the test.86*87* If the server prematurely exits, serverReady will be set to true88* to avoid infinite hangs.89*/90void doServerSide() throws Exception {91SSLSocket sslSocket = null;92SSLServerSocket sslServerSocket = null;93try {94SSLServerSocketFactory sslssf =95(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();96sslServerSocket =97(SSLServerSocket) sslssf.createServerSocket(serverPort);98serverPort = sslServerSocket.getLocalPort();99100sslServerSocket.setEnabledCipherSuites(SERVER_SUITES);101102/*103* Signal Client, we're ready for his connect.104*/105serverReady = true;106107System.out.println("Server waiting for connection");108109sslSocket = (SSLSocket) sslServerSocket.accept();110InputStream sslIS = sslSocket.getInputStream();111OutputStream sslOS = sslSocket.getOutputStream();112113System.out.println("Server starting handshake...");114115116try {117sslIS.read();118throw new Exception("No handshake exception on server side");119} catch (IOException e) {120System.out.println("Handshake failed on server side, OK");121}122123for (int i = 0; i < 3; i++) {124try {125int ch;126if ((ch = sslIS.read()) != -1) {127throw new Exception("Read succeeded server side: "128+ ch);129}130} catch (IOException e) {131System.out.println("Exception for read() on server, OK");132}133}134135} finally {136closeSocket(sslSocket);137closeSocket(sslServerSocket);138}139}140141private static void closeSocket(Socket s) {142try {143if (s != null) {144s.close();145}146} catch (Exception e) {147// ignore148}149}150151private static void closeSocket(ServerSocket s) {152try {153if (s != null) {154s.close();155}156} catch (Exception e) {157// ignore158}159}160161/*162* Define the client side of the test.163*164* If the server prematurely exits, serverReady will be set to true165* to avoid infinite hangs.166*/167void doClientSide() throws Exception {168169/*170* Wait for server to get started.171*/172while (!serverReady) {173Thread.sleep(80);174}175176SSLSocket sslSocket = null;177try {178179SSLSocketFactory sslsf =180(SSLSocketFactory) SSLSocketFactory.getDefault();181sslSocket = (SSLSocket)182sslsf.createSocket("localhost", serverPort);183sslSocket.setEnabledCipherSuites(CLIENT_SUITES);184185InputStream sslIS = sslSocket.getInputStream();186OutputStream sslOS = sslSocket.getOutputStream();187188System.out.println("Client starting handshake...");189190try {191sslIS.read();192throw new Exception("No handshake exception on client side");193} catch (IOException e) {194System.out.println("Handshake failed on client side, OK");195}196197for (int i = 0; i < 3; i++) {198try {199int ch;200if ((ch = sslIS.read()) != -1) {201throw new Exception("Read succeeded on client side: "202+ ch);203}204} catch (IOException e) {205System.out.println("Exception for read() on client, OK");206}207}208} finally {209sslSocket.close();210}211}212213/*214* =============================================================215* The remainder is just support stuff216*/217218volatile int serverPort = 0;219220volatile Exception serverException = null;221volatile Exception clientException = null;222223public static void main(String[] args) throws Exception {224// reset security properties to make sure that the algorithms225// and keys used in this test are not disabled.226Security.setProperty("jdk.tls.disabledAlgorithms", "");227Security.setProperty("jdk.certpath.disabledAlgorithms", "");228229if (debug)230System.setProperty("javax.net.debug", "all");231232/*233* Start the tests.234*/235new ReadHandshake();236}237238Thread clientThread = null;239Thread serverThread = null;240241/*242* Primary constructor, used to drive remainder of the test.243*244* Fork off the other side, then do your work.245*/246ReadHandshake() throws Exception {247startServer(true);248startClient(true);249250serverThread.join();251clientThread.join();252253/*254* When we get here, the test is pretty much over.255*256* If the main thread excepted, that propagates back257* immediately. If the other thread threw an exception, we258* should report back.259*/260if (serverException != null) {261if (clientException != null) {262System.out.println("Client exception:");263clientException.printStackTrace(System.out);264}265throw serverException;266}267if (clientException != null) {268throw clientException;269}270}271272void startServer(boolean newThread) throws Exception {273if (newThread) {274serverThread = new Thread() {275public void run() {276try {277doServerSide();278} catch (Exception e) {279/*280* Our server thread just died.281*282* Release the client, if not active already...283*/284System.err.println("Server died...");285serverReady = true;286serverException = e;287}288}289};290serverThread.start();291} else {292doServerSide();293}294}295296void startClient(boolean newThread) throws Exception {297if (newThread) {298clientThread = new Thread() {299public void run() {300try {301doClientSide();302} catch (Exception e) {303/*304* Our client thread just died.305*/306System.err.println("Client died...");307clientException = e;308}309}310};311clientThread.start();312} else {313doClientSide();314}315}316}317318319