test/jdk/sun/security/ssl/SSLSocketImpl/AsyncSSLSocketClose.java
author xuelei
Tue, 14 Aug 2018 18:16:47 -0700
changeset 51407 910f7b56592f
parent 50768 68fa3d4026ea
permissions -rw-r--r--
8207009: TLS 1.3 half-close and synchronization issues Reviewed-by: jnimeh, mullan, wetmore
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
100
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
diff changeset
     1
/*
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
     2
 * Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.
100
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
diff changeset
     4
 *
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
diff changeset
     7
 * published by the Free Software Foundation.
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
diff changeset
     8
 *
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
diff changeset
    13
 * accompanied this code).
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
diff changeset
    14
 *
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3111
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3111
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3111
diff changeset
    21
 * questions.
100
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
diff changeset
    22
 */
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
diff changeset
    23
21676
36165f46628f 8014266: regression test AsyncSSLSocketClose.java time out.
xuelei
parents: 14342
diff changeset
    24
//
36165f46628f 8014266: regression test AsyncSSLSocketClose.java time out.
xuelei
parents: 14342
diff changeset
    25
// SunJSSE does not support dynamic system properties, no way to re-use
36165f46628f 8014266: regression test AsyncSSLSocketClose.java time out.
xuelei
parents: 14342
diff changeset
    26
// system properties in samevm/agentvm mode.
36165f46628f 8014266: regression test AsyncSSLSocketClose.java time out.
xuelei
parents: 14342
diff changeset
    27
//
36165f46628f 8014266: regression test AsyncSSLSocketClose.java time out.
xuelei
parents: 14342
diff changeset
    28
100
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
diff changeset
    29
/*
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
diff changeset
    30
 * @test
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
diff changeset
    31
 * @bug 6447412
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
diff changeset
    32
 * @summary Issue with socket.close() for ssl sockets when poweroff on
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
diff changeset
    33
 *          other system
10328
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
    34
 * @run main/othervm AsyncSSLSocketClose
100
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
diff changeset
    35
 */
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
diff changeset
    36
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
diff changeset
    37
import javax.net.ssl.*;
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
diff changeset
    38
import java.io.*;
51407
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
    39
import java.net.SocketException;
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    40
import java.util.concurrent.CountDownLatch;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    41
import java.util.concurrent.TimeUnit;
100
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
diff changeset
    42
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    43
public class AsyncSSLSocketClose implements Runnable {
100
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
diff changeset
    44
    SSLSocket socket;
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
diff changeset
    45
    SSLServerSocket ss;
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
diff changeset
    46
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    47
    // Is the socket ready to close?
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    48
    private final CountDownLatch closeCondition = new CountDownLatch(1);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    49
100
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
diff changeset
    50
    // Where do we find the keystores?
23052
241885315119 8032473: Restructure JSSE regression test hierarchy in jdk test
xuelei
parents: 21676
diff changeset
    51
    static String pathToStores = "../../../../javax/net/ssl/etc";
100
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
diff changeset
    52
    static String keyStoreFile = "keystore";
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
diff changeset
    53
    static String trustStoreFile = "truststore";
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
diff changeset
    54
    static String passwd = "passphrase";
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
diff changeset
    55
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    56
    public static void main(String[] args) throws Exception {
100
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
diff changeset
    57
        String keyFilename =
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
diff changeset
    58
            System.getProperty("test.src", "./") + "/" + pathToStores +
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
diff changeset
    59
                "/" + keyStoreFile;
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
diff changeset
    60
        String trustFilename =
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
diff changeset
    61
            System.getProperty("test.src", "./") + "/" + pathToStores +
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
diff changeset
    62
                "/" + trustStoreFile;
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
diff changeset
    63
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
diff changeset
    64
        System.setProperty("javax.net.ssl.keyStore", keyFilename);
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
diff changeset
    65
        System.setProperty("javax.net.ssl.keyStorePassword", passwd);
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
diff changeset
    66
        System.setProperty("javax.net.ssl.trustStore", trustFilename);
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
diff changeset
    67
        System.setProperty("javax.net.ssl.trustStorePassword", passwd);
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
diff changeset
    68
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
diff changeset
    69
        new AsyncSSLSocketClose();
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
diff changeset
    70
    }
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
diff changeset
    71
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    72
    public AsyncSSLSocketClose() throws Exception {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    73
        SSLServerSocketFactory sslssf =
100
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
diff changeset
    74
                (SSLServerSocketFactory)SSLServerSocketFactory.getDefault();
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    75
        ss = (SSLServerSocket) sslssf.createServerSocket(0);
100
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
diff changeset
    76
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    77
        SSLSocketFactory sslsf =
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    78
            (SSLSocketFactory)SSLSocketFactory.getDefault();
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    79
        socket = (SSLSocket)sslsf.createSocket("localhost", ss.getLocalPort());
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    80
        SSLSocket serverSoc = (SSLSocket)ss.accept();
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    81
        ss.close();
100
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
diff changeset
    82
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    83
        (new Thread(this)).start();
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    84
        serverSoc.startHandshake();
100
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
diff changeset
    85
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    86
        boolean closeIsReady = closeCondition.await(90L, TimeUnit.SECONDS);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    87
        if (!closeIsReady) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    88
            System.out.println(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    89
                    "Ignore, the closure is not ready yet in 90 seconds.");
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    90
            return;
100
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
diff changeset
    91
        }
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
diff changeset
    92
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    93
        socket.setSoLinger(true, 10);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    94
        System.out.println("Calling Socket.close");
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    95
        socket.close();
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    96
        System.out.println("ssl socket get closed");
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    97
        System.out.flush();
100
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
diff changeset
    98
    }
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
diff changeset
    99
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
diff changeset
   100
    // block in write
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
diff changeset
   101
    public void run() {
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   102
        byte[] ba = new byte[1024];
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   103
        for (int i = 0; i < ba.length; i++) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   104
            ba[i] = 0x7A;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   105
        }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   106
100
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
diff changeset
   107
        try {
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
diff changeset
   108
            OutputStream os = socket.getOutputStream();
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
diff changeset
   109
            int count = 0;
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   110
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   111
            // 1st round write
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   112
            count += ba.length;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   113
            System.out.println(count + " bytes to be written");
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   114
            os.write(ba);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   115
            System.out.println(count + " bytes written");
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   116
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   117
            // Signal, ready to close.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   118
            closeCondition.countDown();
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   119
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   120
            // write more
100
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
diff changeset
   121
            while (true) {
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
diff changeset
   122
                count += ba.length;
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
diff changeset
   123
                System.out.println(count + " bytes to be written");
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
diff changeset
   124
                os.write(ba);
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
diff changeset
   125
                System.out.println(count + " bytes written");
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
diff changeset
   126
            }
51407
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   127
        } catch (SocketException se) {
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   128
            // the closing may be in progress
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   129
            System.out.println("interrupted? " + se);
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   130
        } catch (Exception e) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   131
            if (socket.isClosed() || socket.isOutputShutdown()) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   132
                System.out.println("interrupted, the socket is closed");
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   133
            } else {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   134
                throw new RuntimeException("interrupted?", e);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   135
            }
100
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
diff changeset
   136
        }
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
diff changeset
   137
    }
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   138
}
100
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
diff changeset
   139