< prev index next >

test/sun/security/ssl/SSLSocketImpl/SSLExceptionForIOIssue.java

Print this page
rev 14406 : 8239798: SSLSocket closes socket both socket endpoints on a SocketTimeoutException
Reviewed-by: xuelei
Contributed-by: alexey@azul.com verghese@amazon.com


  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 //
  25 // Please run in othervm mode.  SunJSSE does not support dynamic system
  26 // properties, no way to re-use system properties in samevm/agentvm mode.
  27 //
  28 
  29 /*
  30  * @test
  31  * @bug 8214339
  32  * @summary SSLSocketImpl erroneously wraps SocketException
  33  * @library /javax/net/ssl/templates
  34  * @run main/othervm SSLExceptionForIOIssue
  35  */
  36 
  37 import javax.net.ssl.*;
  38 import java.io.*;
  39 import java.net.InetAddress;
  40 
  41 public class SSLExceptionForIOIssue implements SSLContextTemplate {
  42 
  43     public static void main(String[] args) throws Exception {
  44         System.err.println("===================================");
  45         new SSLExceptionForIOIssue().test();
  46     }
  47 
  48     private void test() throws Exception {
  49         SSLServerSocket listenSocket = null;
  50         SSLSocket serverSocket = null;
  51         ClientSocket clientSocket = null;
  52         try {
  53             SSLServerSocketFactory serversocketfactory =
  54                     createServerSSLContext().getServerSocketFactory();
  55             listenSocket =
  56                     (SSLServerSocket)serversocketfactory.createServerSocket(0);
  57             listenSocket.setNeedClientAuth(false);
  58             listenSocket.setEnableSessionCreation(true);
  59             listenSocket.setUseClientMode(false);


 122                 clientSocket.setSoLinger(true, 3);
 123                 clientSocket.setSoTimeout(100);
 124 
 125 
 126                 System.err.println("Sending data to server ...");
 127 
 128                 BufferedWriter os = new BufferedWriter(
 129                         new OutputStreamWriter(clientSocket.getOutputStream()));
 130                 os.write(clientData, 0, clientData.length());
 131                 os.newLine();
 132                 os.flush();
 133 
 134                 System.err.println("Reading data from server");
 135                 BufferedReader is = new BufferedReader(
 136                         new InputStreamReader(clientSocket.getInputStream()));
 137                 String data = is.readLine();
 138                 System.err.println("Received Data from server: " + data);
 139             } catch (SSLProtocolException | SSLHandshakeException sslhe) {
 140                 clientException = sslhe;
 141                 System.err.println("unexpected client exception: " + sslhe);
 142             } catch (SSLException ssle) {
 143                 // the expected exception, ignore it
 144                 System.err.println("expected client exception: " + ssle);
 145             } catch (Exception e) {
 146                 clientException = e;
 147                 System.err.println("unexpected client exception: " + e);
 148             } finally {
 149                 if (clientSocket != null) {
 150                     try {
 151                         clientSocket.close();
 152                         System.err.println("client socket closed");
 153                     } catch (IOException ioe) {
 154                         clientException = ioe;
 155                     }
 156                 }
 157 
 158                 isDone = true;
 159             }
 160         }
 161     }
 162 }


  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 //
  25 // Please run in othervm mode.  SunJSSE does not support dynamic system
  26 // properties, no way to re-use system properties in samevm/agentvm mode.
  27 //
  28 
  29 /*
  30  * @test
  31  * @bug 8214339
  32  * @summary SSLSocketImpl erroneously wraps SocketException
  33  * @library /javax/net/ssl/templates
  34  * @run main/othervm SSLExceptionForIOIssue
  35  */
  36 
  37 import javax.net.ssl.*;
  38 import java.io.*;
  39 import java.net.*;
  40 
  41 public class SSLExceptionForIOIssue implements SSLContextTemplate {
  42 
  43     public static void main(String[] args) throws Exception {
  44         System.err.println("===================================");
  45         new SSLExceptionForIOIssue().test();
  46     }
  47 
  48     private void test() throws Exception {
  49         SSLServerSocket listenSocket = null;
  50         SSLSocket serverSocket = null;
  51         ClientSocket clientSocket = null;
  52         try {
  53             SSLServerSocketFactory serversocketfactory =
  54                     createServerSSLContext().getServerSocketFactory();
  55             listenSocket =
  56                     (SSLServerSocket)serversocketfactory.createServerSocket(0);
  57             listenSocket.setNeedClientAuth(false);
  58             listenSocket.setEnableSessionCreation(true);
  59             listenSocket.setUseClientMode(false);


 122                 clientSocket.setSoLinger(true, 3);
 123                 clientSocket.setSoTimeout(100);
 124 
 125 
 126                 System.err.println("Sending data to server ...");
 127 
 128                 BufferedWriter os = new BufferedWriter(
 129                         new OutputStreamWriter(clientSocket.getOutputStream()));
 130                 os.write(clientData, 0, clientData.length());
 131                 os.newLine();
 132                 os.flush();
 133 
 134                 System.err.println("Reading data from server");
 135                 BufferedReader is = new BufferedReader(
 136                         new InputStreamReader(clientSocket.getInputStream()));
 137                 String data = is.readLine();
 138                 System.err.println("Received Data from server: " + data);
 139             } catch (SSLProtocolException | SSLHandshakeException sslhe) {
 140                 clientException = sslhe;
 141                 System.err.println("unexpected client exception: " + sslhe);
 142             } catch (SSLException | SocketTimeoutException ssle) {
 143                 // the expected exception, ignore it
 144                 System.err.println("expected client exception: " + ssle);
 145             } catch (Exception e) {
 146                 clientException = e;
 147                 System.err.println("unexpected client exception: " + e);
 148             } finally {
 149                 if (clientSocket != null) {
 150                     try {
 151                         clientSocket.close();
 152                         System.err.println("client socket closed");
 153                     } catch (IOException ioe) {
 154                         clientException = ioe;
 155                     }
 156                 }
 157 
 158                 isDone = true;
 159             }
 160         }
 161     }
 162 }
< prev index next >