Path: blob/master/test/jdk/sun/security/ssl/SSLSocketImpl/SocketExceptionForSocketIssues.java
41152 views
/*1* Copyright (c) 2018, 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// Please run in othervm mode. SunJSSE does not support dynamic system25// properties, no way to re-use system properties in samevm/agentvm mode.26//2728/*29* @test30* @bug 821433931* @summary SSLSocketImpl erroneously wraps SocketException32* @library /javax/net/ssl/templates33* @run main/othervm SocketExceptionForSocketIssues34*/3536import javax.net.ssl.*;37import java.io.*;38import java.net.*;3940public class SocketExceptionForSocketIssues implements SSLContextTemplate {4142public static void main(String[] args) throws Exception {43System.err.println("===================================");44new SocketExceptionForSocketIssues().test();45}4647private void test() throws Exception {48SSLServerSocket listenSocket = null;49SSLSocket serverSocket = null;50ClientSocket clientSocket = null;51try {52SSLServerSocketFactory serversocketfactory =53createServerSSLContext().getServerSocketFactory();54listenSocket =55(SSLServerSocket)serversocketfactory.createServerSocket(0);56listenSocket.setNeedClientAuth(false);57listenSocket.setEnableSessionCreation(true);58listenSocket.setUseClientMode(false);5960System.err.println("Starting client");61clientSocket = new ClientSocket(listenSocket.getLocalPort());62clientSocket.start();6364System.err.println("Accepting client requests");65serverSocket = (SSLSocket)listenSocket.accept();6667if (!clientSocket.isDone) {68System.err.println("Waiting 3 seconds for client ");69Thread.sleep(3000);70}7172System.err.println("Sending data to client ...");73String serverData = "Hi, I am server";74BufferedWriter os = new BufferedWriter(75new OutputStreamWriter(serverSocket.getOutputStream()));76os.write(serverData, 0, serverData.length());77os.newLine();78os.flush();79} catch (SSLProtocolException | SSLHandshakeException sslhe) {80throw sslhe;81} catch (SocketException se) {82// the expected exception, ignore it83System.err.println("server exception: " + se);84} finally {85if (listenSocket != null) {86listenSocket.close();87}8889if (serverSocket != null) {90serverSocket.close();91}92}9394if (clientSocket != null && clientSocket.clientException != null) {95throw clientSocket.clientException;96}97}9899100101private class ClientSocket extends Thread{102boolean isDone = false;103int serverPort = 0;104Exception clientException;105106public ClientSocket(int serverPort) {107this.serverPort = serverPort;108}109110@Override111public void run() {112SSLSocket clientSocket = null;113String clientData = "Hi, I am client";114try {115System.err.println(116"Connecting to server at port " + serverPort);117SSLSocketFactory sslSocketFactory =118createClientSSLContext().getSocketFactory();119clientSocket = (SSLSocket)sslSocketFactory.createSocket(120InetAddress.getLocalHost(), serverPort);121clientSocket.setSoLinger(true, 3);122clientSocket.setSoTimeout(100);123124125System.err.println("Sending data to server ...");126127BufferedWriter os = new BufferedWriter(128new OutputStreamWriter(clientSocket.getOutputStream()));129os.write(clientData, 0, clientData.length());130os.newLine();131os.flush();132133System.err.println("Reading data from server");134BufferedReader is = new BufferedReader(135new InputStreamReader(clientSocket.getInputStream()));136String data = is.readLine();137System.err.println("Received Data from server: " + data);138} catch (SSLProtocolException | SSLHandshakeException sslhe) {139clientException = sslhe;140System.err.println("unexpected client exception: " + sslhe);141} catch (SSLException | SocketTimeoutException ssle) {142// the expected exception, ignore it143System.err.println("expected client exception: " + ssle);144} catch (Exception e) {145clientException = e;146System.err.println("unexpected client exception: " + e);147} finally {148if (clientSocket != null) {149try {150clientSocket.close();151System.err.println("client socket closed");152} catch (IOException ioe) {153clientException = ioe;154}155}156157isDone = true;158}159}160}161}162163164