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
41import javax.net.ssl.*;
42import java.io.*;
43
44public 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}
126