Path: blob/master/test/jdk/sun/security/ssl/SSLSocketImpl/SSLSocketShouldThrowSocketException.java
41152 views
/*1* Copyright (c) 2021, Amazon 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 8214339 825966226* @summary When a SocketException is thrown by the underlying layer, It27* should be thrown as is and not be transformed to an SSLException.28* @library /javax/net/ssl/templates29* @run main/othervm SSLSocketShouldThrowSocketException30*/3132import java.io.*;33import java.net.*;34import java.util.*;35import java.security.*;36import javax.net.ssl.*;3738import java.util.concurrent.CountDownLatch;39import java.util.concurrent.TimeUnit;4041public class SSLSocketShouldThrowSocketException extends SSLSocketTemplate {4243boolean handshake;4445private final CountDownLatch clientTerminatedCondition = new CountDownLatch(1);4647SSLSocketShouldThrowSocketException(boolean handshake) {48this.handshake = handshake;49}5051@Override52protected boolean isCustomizedClientConnection() {53return true;54}5556@Override57protected void runServerApplication(SSLSocket socket) throws Exception {58clientTerminatedCondition.await(30L, TimeUnit.SECONDS);59}6061@Override62protected void runClientApplication(int serverPort) throws Exception {63Socket baseSocket = new Socket("localhost", serverPort);6465SSLSocketFactory sslsf =66(SSLSocketFactory) SSLSocketFactory.getDefault();67SSLSocket sslSocket = (SSLSocket)68sslsf.createSocket(baseSocket, "localhost", serverPort, false);6970if (this.handshake) {71testHandshakeClose(baseSocket, sslSocket);72} else {73testDataClose(baseSocket, sslSocket);74}7576clientTerminatedCondition.countDown();7778}7980private void testHandshakeClose(Socket baseSocket, SSLSocket sslSocket) throws Exception {81Thread aborter = new Thread() {82@Override83public void run() {8485try {86Thread.sleep(10);87System.err.println("Closing the client socket : " + System.nanoTime());88baseSocket.close();89} catch (Exception ieo) {90ieo.printStackTrace();91}92}93};9495aborter.start();9697try {98// handshaking99System.err.println("Client starting handshake: " + System.nanoTime());100sslSocket.startHandshake();101throw new Exception("Start handshake did not throw an exception");102} catch (SocketException se) {103System.err.println("Caught Expected SocketException");104}105106aborter.join();107}108109private void testDataClose(Socket baseSocket, SSLSocket sslSocket) throws Exception{110111CountDownLatch handshakeCondition = new CountDownLatch(1);112113Thread aborter = new Thread() {114@Override115public void run() {116117try {118handshakeCondition.await(10L, TimeUnit.SECONDS);119System.err.println("Closing the client socket : " + System.nanoTime());120baseSocket.close();121} catch (Exception ieo) {122ieo.printStackTrace();123}124}125};126127aborter.start();128129try {130// handshaking131System.err.println("Client starting handshake: " + System.nanoTime());132sslSocket.startHandshake();133handshakeCondition.countDown();134System.err.println("Reading data from server");135BufferedReader is = new BufferedReader(136new InputStreamReader(sslSocket.getInputStream()));137String data = is.readLine();138throw new Exception("Start handshake did not throw an exception");139} catch (SocketException se) {140System.err.println("Caught Expected SocketException");141}142143aborter.join();144}145146public static void main(String[] args) throws Exception {147// SocketException should be throws during a handshake phase.148(new SSLSocketShouldThrowSocketException(true)).run();149// SocketException should be throw during the application data phase.150(new SSLSocketShouldThrowSocketException(false)).run();151}152}153154155