< prev index next >

test/jdk/sun/security/ssl/SSLSocketImpl/AsyncSSLSocketClose.java

Print this page


   1 /*
   2  * Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   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
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  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 // SunJSSE does not support dynamic system properties, no way to re-use
  26 // system properties in samevm/agentvm mode.
  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 
  33 /*
  34  * @test
  35  * @bug 6447412
  36  * @summary Issue with socket.close() for ssl sockets when poweroff on
  37  *          other system
  38  * @run main/othervm AsyncSSLSocketClose
  39  */
  40 
  41 import javax.net.ssl.*;
  42 import java.io.*;


  43 
  44 public class AsyncSSLSocketClose implements Runnable
  45 {
  46     SSLSocket socket;
  47     SSLServerSocket ss;
  48 



  49     // Where do we find the keystores?
  50     static String pathToStores = "../../../../javax/net/ssl/etc";
  51     static String keyStoreFile = "keystore";
  52     static String trustStoreFile = "truststore";
  53     static String passwd = "passphrase";
  54 
  55     public static void main(String[] args) {
  56         String keyFilename =
  57             System.getProperty("test.src", "./") + "/" + pathToStores +
  58                 "/" + keyStoreFile;
  59         String trustFilename =
  60             System.getProperty("test.src", "./") + "/" + pathToStores +
  61                 "/" + trustStoreFile;
  62 
  63         System.setProperty("javax.net.ssl.keyStore", keyFilename);
  64         System.setProperty("javax.net.ssl.keyStorePassword", passwd);
  65         System.setProperty("javax.net.ssl.trustStore", trustFilename);
  66         System.setProperty("javax.net.ssl.trustStorePassword", passwd);
  67 
  68         new AsyncSSLSocketClose();
  69     }
  70 
  71     public AsyncSSLSocketClose() {
  72         try {
  73             SSLServerSocketFactory sslssf =
  74                 (SSLServerSocketFactory)SSLServerSocketFactory.getDefault();
  75             ss = (SSLServerSocket) sslssf.createServerSocket(0);
  76 
  77             SSLSocketFactory sslsf =
  78                 (SSLSocketFactory)SSLSocketFactory.getDefault();
  79             socket = (SSLSocket)sslsf.createSocket("localhost",
  80                                                         ss.getLocalPort());
  81             SSLSocket serverSoc = (SSLSocket) ss.accept();
  82             ss.close();
  83 
  84             (new Thread(this)).start();
  85             serverSoc.startHandshake();
  86 

  87             try {
  88                 Thread.sleep(5000);
  89             } catch (Exception e) {
  90                 e.printStackTrace();
  91             }
  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         }
 102 
 103     }
 104 
 105     // block in write
 106     public void run() {
 107         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             int count = 0;



















 114             while (true) {
 115                 count += ba.length;
 116                 System.out.println(count + " bytes to be written");
 117                 os.write(ba);
 118                 System.out.println(count + " bytes written");
 119             }
 120         } catch (IOException e) {
 121             e.printStackTrace();




 122         }
 123     }
 124 
 125 }


   1 /*
   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.
   4  *
   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
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  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 // SunJSSE does not support dynamic system properties, no way to re-use
  26 // system properties in samevm/agentvm mode.
  27 //




  28 
  29 /*
  30  * @test
  31  * @bug 6447412
  32  * @summary Issue with socket.close() for ssl sockets when poweroff on
  33  *          other system
  34  * @run main/othervm AsyncSSLSocketClose
  35  */
  36 
  37 import javax.net.ssl.*;
  38 import java.io.*;
  39 import java.util.concurrent.locks.*;
  40 import java.util.concurrent.TimeUnit;
  41 
  42 public class AsyncSSLSocketClose implements Runnable {

  43     SSLSocket socket;
  44     SSLServerSocket ss;
  45 
  46     final Lock lock = new ReentrantLock();
  47     final Condition isRunning = lock.newCondition(); 
  48 
  49     // Where do we find the keystores?
  50     static String pathToStores = "../../../../javax/net/ssl/etc";
  51     static String keyStoreFile = "keystore";
  52     static String trustStoreFile = "truststore";
  53     static String passwd = "passphrase";
  54 
  55     public static void main(String[] args) throws Exception {
  56         String keyFilename =
  57             System.getProperty("test.src", "./") + "/" + pathToStores +
  58                 "/" + keyStoreFile;
  59         String trustFilename =
  60             System.getProperty("test.src", "./") + "/" + pathToStores +
  61                 "/" + trustStoreFile;
  62 
  63         System.setProperty("javax.net.ssl.keyStore", keyFilename);
  64         System.setProperty("javax.net.ssl.keyStorePassword", passwd);
  65         System.setProperty("javax.net.ssl.trustStore", trustFilename);
  66         System.setProperty("javax.net.ssl.trustStorePassword", passwd);
  67 
  68         new AsyncSSLSocketClose();
  69     }
  70 
  71     public AsyncSSLSocketClose() throws Exception {

  72         SSLServerSocketFactory sslssf =
  73                 (SSLServerSocketFactory)SSLServerSocketFactory.getDefault();
  74         ss = (SSLServerSocket) sslssf.createServerSocket(0);
  75 
  76         SSLSocketFactory sslsf =
  77             (SSLSocketFactory)SSLSocketFactory.getDefault();
  78         socket = (SSLSocket)sslsf.createSocket("localhost", ss.getLocalPort());
  79         SSLSocket serverSoc = (SSLSocket)ss.accept();

  80         ss.close();
  81 
  82         (new Thread(this)).start();
  83         serverSoc.startHandshake();
  84         if (lock.tryLock() || lock.tryLock(5000, TimeUnit.MILLISECONDS)) {
  85             boolean started = false;
  86             try {
  87                 started = isRunning.await(5000, TimeUnit.MILLISECONDS);
  88             } finally {
  89                 lock.unlock();
  90             }
  91             if (started) {
  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();
  97             } else {
  98                 throw new Exception("Did not get the signal in main thread");
  99             }
 100         } else {
 101             throw new Exception("Unable get the lock in main thread");
 102         }

 103     }
 104 
 105     // block in write
 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 
 112         try {
 113             OutputStream os = socket.getOutputStream();
 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
 134             while (true) {
 135                 count += ba.length;
 136                 System.out.println(count + " bytes to be written");
 137                 os.write(ba);
 138                 System.out.println(count + " bytes written");
 139             }
 140         } catch (Exception e) {
 141             if (socket.isClosed()) {
 142                 System.out.println("interrupted, the socket is closed");
 143             } else {
 144                 throw new RuntimeException("interrupted?", e);
 145             }
 146         }
 147     }

 148 }
 149 
 150 
< prev index next >