test/jdk/sun/security/ssl/SSLSocketImpl/NonAutoClose.java
author xuelei
Mon, 25 Jun 2018 13:41:39 -0700
changeset 50768 68fa3d4026ea
parent 47216 71c04702a3d5
permissions -rw-r--r--
8196584: TLS 1.3 Implementation Reviewed-by: ascarpino, coffeys, dfuchs, jjiang, jnimeh, mullan, rhalade, ssahoo, valeriep, weijun, wetmore, xuelei Contributed-by: Adam Petcher <adam.petcher@oracle.com>, Amanda Jiang <amanda.jiang@oracle.com>, Anthony Scarpino <anthony.scarpino@oracle.com>, Bradford Wetmore <bradford.wetmore@oracle.com>, Jamil Nimeh <jamil.j.nimeh@oracle.com>, John Jiang <sha.jiang@oracle.com>, Rajan Halade <rajan.halade@oracle.com>, Sibabrata Sahoo <sibabrata.sahoo@oracle.com>, Valerie Peng <valerie.peng@oracle.com>, Weijun Wang <weijun.wang@oracle.com>, Xuelei Fan <xuelei.fan@oracle.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
     2
 * Copyright (c) 2001, 2018, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    24
//
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    25
// SunJSSE does not support dynamic system properties, no way to re-use
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    26
// system properties in samevm/agentvm mode.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    27
//
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    28
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @bug 4404399
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    32
 * @ignore this test does not work any more as the TLS spec changes the
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    33
 *         behaviors of close_notify.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * @summary When a layered SSL socket is closed, it should wait for close_notify
10328
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
    35
 * @run main/othervm NonAutoClose
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * @author Brad Wetmore
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.net.ServerSocket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.net.Socket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.net.ssl.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.security.cert.X509Certificate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.security.cert.CertificateException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
public class NonAutoClose {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     * =============================================================
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     * Set the various variables needed for the tests, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * specify what tests to run on each side.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * Should we run the client or server in a separate thread?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * Both sides can throw exceptions, but do you have a preference
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * as to which side should be the main thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private static boolean separateServerThread = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * Where do we find the keystores?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     */
23052
241885315119 8032473: Restructure JSSE regression test hierarchy in jdk test
xuelei
parents: 14342
diff changeset
    64
    private final static String pathToStores = "../../../../javax/net/ssl/etc";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private final static String keyStoreFile = "keystore";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private final static String trustStoreFile = "truststore";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private final static String passwd = "passphrase";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private final static char[] cpasswd = "passphrase".toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * Is the server ready to serve?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    volatile static boolean serverReady = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * Turn on SSL debugging?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private final static boolean DEBUG = false;
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    79
    private final static boolean VERBOSE = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    private final static int NUM_ITERATIONS  = 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private final static int PLAIN_SERVER_VAL = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    private final static int PLAIN_CLIENT_VAL = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    private final static int TLS_SERVER_VAL = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    private final static int TLS_CLIENT_VAL = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * If the client or server is doing some kind of object creation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * that the other side depends on, and that thread prematurely
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * exits, you may experience a hang.  The test harness will
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * terminate all hung threads after its timeout has expired,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * currently 3 minutes by default, but you might try to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * smart about it....
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    void expectValue(int got, int expected, String msg) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        if (VERBOSE) {
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    97
            System.err.println(msg + ": read (" + got + ")");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        if (got != expected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            throw new IOException(msg + ": read (" + got
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                + ") but expecting(" + expected + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * Define the server side of the test.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * If the server prematurely exits, serverReady will be set to true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * to avoid infinite hangs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     void doServerSide() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        if (VERBOSE) {
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   115
            System.err.println("Starting server");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
         * Setup the SSL stuff
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        SSLSocketFactory sslsf =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
             (SSLSocketFactory) SSLSocketFactory.getDefault();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        ServerSocket serverSocket = new ServerSocket(SERVER_PORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        SERVER_PORT = serverSocket.getLocalPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
         * Signal Client, we're ready for his connect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        serverReady = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        Socket plainSocket = serverSocket.accept();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        InputStream is = plainSocket.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        OutputStream os = plainSocket.getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        expectValue(is.read(), PLAIN_CLIENT_VAL, "Server");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        os.write(PLAIN_SERVER_VAL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        os.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        for (int i = 1; i <= NUM_ITERATIONS; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            if (VERBOSE) {
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   144
                System.err.println("=================================");
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   145
                System.err.println("Server Iteration #" + i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            SSLSocket ssls = (SSLSocket) sslsf.createSocket(plainSocket,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                SERVER_NAME, plainSocket.getPort(), false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            ssls.setUseClientMode(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            InputStream sslis = ssls.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            OutputStream sslos = ssls.getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            expectValue(sslis.read(), TLS_CLIENT_VAL, "Server");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            sslos.write(TLS_SERVER_VAL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            sslos.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            sslis.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            sslos.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            ssls.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            if (VERBOSE) {
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   165
                System.err.println("TLS socket is closed");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        expectValue(is.read(), PLAIN_CLIENT_VAL, "Server");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        os.write(PLAIN_SERVER_VAL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        os.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        os.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        plainSocket.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        if (VERBOSE) {
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   179
            System.err.println("Server plain socket is closed");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * Define the client side of the test.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * If the server prematurely exits, serverReady will be set to true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * to avoid infinite hangs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    private void doClientSide() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
         * Wait for server to get started.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        while (!serverReady) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            Thread.sleep(50);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        if (VERBOSE) {
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   198
            System.err.println("Starting client");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
         * Setup the SSL stuff
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        SSLSocketFactory sslsf =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
             (SSLSocketFactory) SSLSocketFactory.getDefault();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        Socket plainSocket = new Socket(SERVER_NAME, SERVER_PORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        InputStream is = plainSocket.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        OutputStream os = plainSocket.getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        os.write(PLAIN_CLIENT_VAL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        os.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        expectValue(is.read(), PLAIN_SERVER_VAL, "Client");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        for (int i = 1; i <= NUM_ITERATIONS; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            if (VERBOSE) {
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   218
                System.err.println("===================================");
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   219
                System.err.println("Client Iteration #" + i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
              }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            SSLSocket ssls = (SSLSocket) sslsf.createSocket(plainSocket,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
               SERVER_NAME, plainSocket.getPort(), false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            ssls.setUseClientMode(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            InputStream sslis = ssls.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            OutputStream sslos = ssls.getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            sslos.write(TLS_CLIENT_VAL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            sslos.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            expectValue(sslis.read(), TLS_SERVER_VAL, "Client");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            sslis.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            sslos.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            ssls.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            if (VERBOSE) {
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   240
                System.err.println("Client TLS socket is closed");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        os.write(PLAIN_CLIENT_VAL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        os.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        expectValue(is.read(), PLAIN_SERVER_VAL, "Client");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        os.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        plainSocket.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        if (VERBOSE) {
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   254
            System.err.println("Client plain socket is closed");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * =============================================================
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * The remainder is just support stuff
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    private volatile int SERVER_PORT = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    private final static String SERVER_NAME = "localhost";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    private volatile Exception serverException = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    private volatile Exception clientException = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    private final static String keyFilename =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        System.getProperty("test.src", ".") + "/" + pathToStores +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        "/" + keyStoreFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    private final static String trustFilename =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        System.getProperty("test.src", ".") + "/" + pathToStores +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        "/" + trustStoreFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
   // Used for running test standalone
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        System.setProperty("javax.net.ssl.keyStore", keyFilename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        System.setProperty("javax.net.ssl.keyStorePassword", passwd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        System.setProperty("javax.net.ssl.trustStore", trustFilename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        System.setProperty("javax.net.ssl.trustStorePassword", passwd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        if (DEBUG)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            System.setProperty("javax.net.debug", "all");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
         * Start the tests.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        new NonAutoClose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    private Thread clientThread = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    private Thread serverThread = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * Primary constructor, used to drive remainder of the test.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * Fork off the other side, then do your work.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    NonAutoClose() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        if (separateServerThread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            startServer(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            startClient(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            startClient(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            startServer(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
         * Wait for other side to close down.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        if (separateServerThread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            serverThread.join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            clientThread.join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
         * When we get here, the test is pretty much over.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
         * If the main thread excepted, that propagates back
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
         * immediately.  If the other thread threw an exception, we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
         * should report back.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        if (serverException != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            System.err.print("Server Exception:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            throw serverException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        if (clientException != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            System.err.print("Client Exception:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            throw clientException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    private void startServer(boolean newThread) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        if (newThread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            serverThread = new Thread() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                        doServerSide();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                    } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                         * Our server thread just died.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                         * Release the client, if not active already...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                        System.err.println("Server died...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                        serverReady = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                        serverException = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            serverThread.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            doServerSide();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    private void startClient(boolean newThread) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        if (newThread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            clientThread = new Thread() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                        doClientSide();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                    } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                         * Our client thread just died.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                        System.err.println("Client died...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                        clientException = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            clientThread.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            doClientSide();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
}