test/jdk/sun/security/ssl/SSLSocketImpl/AsyncSSLSocketClose.java
changeset 50768 68fa3d4026ea
parent 47216 71c04702a3d5
child 51407 910f7b56592f
equal deleted inserted replaced
50767:356eaea05bf0 50768:68fa3d4026ea
     1 /*
     1 /*
     2  * Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    23 
    23 
    24 //
    24 //
    25 // SunJSSE does not support dynamic system properties, no way to re-use
    25 // SunJSSE does not support dynamic system properties, no way to re-use
    26 // system properties in samevm/agentvm mode.
    26 // system properties in samevm/agentvm mode.
    27 //
    27 //
    28 // The test may timeout occasionally on heavy loaded system because
       
    29 // there are lot of TLS transactions involved. Frequent timeout(s) should
       
    30 // be analyzed further.
       
    31 //
       
    32 
    28 
    33 /*
    29 /*
    34  * @test
    30  * @test
    35  * @bug 6447412
    31  * @bug 6447412
    36  * @summary Issue with socket.close() for ssl sockets when poweroff on
    32  * @summary Issue with socket.close() for ssl sockets when poweroff on
    38  * @run main/othervm AsyncSSLSocketClose
    34  * @run main/othervm AsyncSSLSocketClose
    39  */
    35  */
    40 
    36 
    41 import javax.net.ssl.*;
    37 import javax.net.ssl.*;
    42 import java.io.*;
    38 import java.io.*;
       
    39 import java.util.concurrent.CountDownLatch;
       
    40 import java.util.concurrent.TimeUnit;
    43 
    41 
    44 public class AsyncSSLSocketClose implements Runnable
    42 public class AsyncSSLSocketClose implements Runnable {
    45 {
       
    46     SSLSocket socket;
    43     SSLSocket socket;
    47     SSLServerSocket ss;
    44     SSLServerSocket ss;
       
    45 
       
    46     // Is the socket ready to close?
       
    47     private final CountDownLatch closeCondition = new CountDownLatch(1);
    48 
    48 
    49     // Where do we find the keystores?
    49     // Where do we find the keystores?
    50     static String pathToStores = "../../../../javax/net/ssl/etc";
    50     static String pathToStores = "../../../../javax/net/ssl/etc";
    51     static String keyStoreFile = "keystore";
    51     static String keyStoreFile = "keystore";
    52     static String trustStoreFile = "truststore";
    52     static String trustStoreFile = "truststore";
    53     static String passwd = "passphrase";
    53     static String passwd = "passphrase";
    54 
    54 
    55     public static void main(String[] args) {
    55     public static void main(String[] args) throws Exception {
    56         String keyFilename =
    56         String keyFilename =
    57             System.getProperty("test.src", "./") + "/" + pathToStores +
    57             System.getProperty("test.src", "./") + "/" + pathToStores +
    58                 "/" + keyStoreFile;
    58                 "/" + keyStoreFile;
    59         String trustFilename =
    59         String trustFilename =
    60             System.getProperty("test.src", "./") + "/" + pathToStores +
    60             System.getProperty("test.src", "./") + "/" + pathToStores +
    66         System.setProperty("javax.net.ssl.trustStorePassword", passwd);
    66         System.setProperty("javax.net.ssl.trustStorePassword", passwd);
    67 
    67 
    68         new AsyncSSLSocketClose();
    68         new AsyncSSLSocketClose();
    69     }
    69     }
    70 
    70 
    71     public AsyncSSLSocketClose() {
    71     public AsyncSSLSocketClose() throws Exception {
    72         try {
    72         SSLServerSocketFactory sslssf =
    73             SSLServerSocketFactory sslssf =
       
    74                 (SSLServerSocketFactory)SSLServerSocketFactory.getDefault();
    73                 (SSLServerSocketFactory)SSLServerSocketFactory.getDefault();
    75             ss = (SSLServerSocket) sslssf.createServerSocket(0);
    74         ss = (SSLServerSocket) sslssf.createServerSocket(0);
    76 
    75 
    77             SSLSocketFactory sslsf =
    76         SSLSocketFactory sslsf =
    78                 (SSLSocketFactory)SSLSocketFactory.getDefault();
    77             (SSLSocketFactory)SSLSocketFactory.getDefault();
    79             socket = (SSLSocket)sslsf.createSocket("localhost",
    78         socket = (SSLSocket)sslsf.createSocket("localhost", ss.getLocalPort());
    80                                                         ss.getLocalPort());
    79         SSLSocket serverSoc = (SSLSocket)ss.accept();
    81             SSLSocket serverSoc = (SSLSocket) ss.accept();
    80         ss.close();
    82             ss.close();
       
    83 
    81 
    84             (new Thread(this)).start();
    82         (new Thread(this)).start();
    85             serverSoc.startHandshake();
    83         serverSoc.startHandshake();
    86 
    84 
    87             try {
    85         boolean closeIsReady = closeCondition.await(90L, TimeUnit.SECONDS);
    88                 Thread.sleep(5000);
    86         if (!closeIsReady) {
    89             } catch (Exception e) {
    87             System.out.println(
    90                 e.printStackTrace();
    88                     "Ignore, the closure is not ready yet in 90 seconds.");
    91             }
    89             return;
    92 
       
    93             socket.setSoLinger(true, 10);
       
    94             System.out.println("Calling Socket.close");
       
    95             socket.close();
       
    96             System.out.println("ssl socket get closed");
       
    97             System.out.flush();
       
    98 
       
    99         } catch (IOException e) {
       
   100             e.printStackTrace();
       
   101         }
    90         }
   102 
    91 
       
    92         socket.setSoLinger(true, 10);
       
    93         System.out.println("Calling Socket.close");
       
    94         socket.close();
       
    95         System.out.println("ssl socket get closed");
       
    96         System.out.flush();
   103     }
    97     }
   104 
    98 
   105     // block in write
    99     // block in write
   106     public void run() {
   100     public void run() {
       
   101         byte[] ba = new byte[1024];
       
   102         for (int i = 0; i < ba.length; i++) {
       
   103             ba[i] = 0x7A;
       
   104         }
       
   105 
   107         try {
   106         try {
   108             byte[] ba = new byte[1024];
       
   109             for (int i=0; i<ba.length; i++)
       
   110                 ba[i] = 0x7A;
       
   111 
       
   112             OutputStream os = socket.getOutputStream();
   107             OutputStream os = socket.getOutputStream();
   113             int count = 0;
   108             int count = 0;
       
   109 
       
   110             // 1st round write
       
   111             count += ba.length;
       
   112             System.out.println(count + " bytes to be written");
       
   113             os.write(ba);
       
   114             System.out.println(count + " bytes written");
       
   115 
       
   116             // Signal, ready to close.
       
   117             closeCondition.countDown();
       
   118 
       
   119             // write more
   114             while (true) {
   120             while (true) {
   115                 count += ba.length;
   121                 count += ba.length;
   116                 System.out.println(count + " bytes to be written");
   122                 System.out.println(count + " bytes to be written");
   117                 os.write(ba);
   123                 os.write(ba);
   118                 System.out.println(count + " bytes written");
   124                 System.out.println(count + " bytes written");
   119             }
   125             }
   120         } catch (IOException e) {
   126         } catch (Exception e) {
   121             e.printStackTrace();
   127             if (socket.isClosed() || socket.isOutputShutdown()) {
       
   128                 System.out.println("interrupted, the socket is closed");
       
   129             } else {
       
   130                 throw new RuntimeException("interrupted?", e);
       
   131             }
   122         }
   132         }
   123     }
   133     }
       
   134 }
   124 
   135 
   125 }