test/jdk/sun/security/ssl/SSLSocketImpl/AsyncSSLSocketClose.java
branchJDK-8145252-TLS13-branch
changeset 56542 56aaa6cb3693
parent 47216 71c04702a3d5
child 56661 2a820e434f17
equal deleted inserted replaced
56541:92cbbfc996f3 56542:56aaa6cb3693
     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.locks.*;
       
    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     final Lock lock = new ReentrantLock();
       
    47     final Condition isRunning = lock.newCondition(); 
    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         if (lock.tryLock() || lock.tryLock(5000, TimeUnit.MILLISECONDS)) {
       
    85             boolean started = false;
    87             try {
    86             try {
    88                 Thread.sleep(5000);
    87                 started = isRunning.await(5000, TimeUnit.MILLISECONDS);
    89             } catch (Exception e) {
    88             } finally {
    90                 e.printStackTrace();
    89                 lock.unlock();
    91             }
    90             }
    92 
    91             if (started) {
    93             socket.setSoLinger(true, 10);
    92                 socket.setSoLinger(true, 10);
    94             System.out.println("Calling Socket.close");
    93                 System.out.println("Calling Socket.close");
    95             socket.close();
    94                 socket.close();
    96             System.out.println("ssl socket get closed");
    95                 System.out.println("ssl socket get closed");
    97             System.out.flush();
    96                 System.out.flush();
    98 
    97             } else {
    99         } catch (IOException e) {
    98                 throw new Exception("Did not get the signal in main thread");
   100             e.printStackTrace();
    99             }
       
   100         } else {
       
   101             throw new Exception("Unable get the lock in main thread");
   101         }
   102         }
   102 
       
   103     }
   103     }
   104 
   104 
   105     // block in write
   105     // block in write
   106     public void run() {
   106     public void run() {
       
   107         byte[] ba = new byte[1024];
       
   108         for (int i = 0; i < ba.length; i++) {
       
   109             ba[i] = 0x7A;
       
   110         }
       
   111 
   107         try {
   112         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();
   113             OutputStream os = socket.getOutputStream();
   113             int count = 0;
   114             int count = 0;
       
   115 
       
   116             // 1st round write
       
   117             count += ba.length;
       
   118             System.out.println(count + " bytes to be written");
       
   119             os.write(ba);
       
   120             System.out.println(count + " bytes written");
       
   121 
       
   122             if (lock.tryLock() || lock.tryLock(5000, TimeUnit.MILLISECONDS)) {
       
   123                 try {
       
   124                     isRunning.signal();
       
   125                 } finally {
       
   126                     lock.unlock();
       
   127                 }
       
   128             } else {
       
   129                 throw new RuntimeException(
       
   130                     "Unable get the lock in write thread");
       
   131             }
       
   132 
       
   133             // write more
   114             while (true) {
   134             while (true) {
   115                 count += ba.length;
   135                 count += ba.length;
   116                 System.out.println(count + " bytes to be written");
   136                 System.out.println(count + " bytes to be written");
   117                 os.write(ba);
   137                 os.write(ba);
   118                 System.out.println(count + " bytes written");
   138                 System.out.println(count + " bytes written");
   119             }
   139             }
   120         } catch (IOException e) {
   140         } catch (Exception e) {
   121             e.printStackTrace();
   141             if (socket.isClosed()) {
       
   142                 System.out.println("interrupted, the socket is closed");
       
   143             } else {
       
   144                 throw new RuntimeException("interrupted?", e);
       
   145             }
   122         }
   146         }
   123     }
   147     }
       
   148 }
   124 
   149 
   125 }
   150