test/jdk/javax/net/ssl/SSLSession/TestEnabledProtocols.java
author xuelei
Mon, 25 Jun 2018 13:41:39 -0700
changeset 50768 68fa3d4026ea
parent 47216 71c04702a3d5
child 51031 a40b75d39ecd
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
/*
23055
63ec3bde101c 8036676: Rename class name testEnabledProtocols to TestEnabledProtocols
xuelei
parents: 23052
diff changeset
     2
 * Copyright (c) 2001, 2014, 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
23055
63ec3bde101c 8036676: Rename class name testEnabledProtocols to TestEnabledProtocols
xuelei
parents: 23052
diff changeset
    24
// SunJSSE does not support dynamic system properties, no way to re-use
63ec3bde101c 8036676: Rename class name testEnabledProtocols to TestEnabledProtocols
xuelei
parents: 23052
diff changeset
    25
// system properties in samevm/agentvm mode.
63ec3bde101c 8036676: Rename class name testEnabledProtocols to TestEnabledProtocols
xuelei
parents: 23052
diff changeset
    26
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @bug 4416068 4478803 4479736
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @summary 4273544 JSSE request for function forceV3ClientHello()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 *          4479736 setEnabledProtocols API does not work correctly
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 *          4478803 Need APIs to determine the protocol versions used in an SSL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *                  session
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *          4701722 protocol mismatch exceptions should be consistent between
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *                  SSLv3 and TLSv1
23055
63ec3bde101c 8036676: Rename class name testEnabledProtocols to TestEnabledProtocols
xuelei
parents: 23052
diff changeset
    36
 * @run main/othervm TestEnabledProtocols
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * @author Ram Marti
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import javax.net.ssl.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.security.cert.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
23055
63ec3bde101c 8036676: Rename class name testEnabledProtocols to TestEnabledProtocols
xuelei
parents: 23052
diff changeset
    47
public class TestEnabledProtocols {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     * For each of the valid protocols combinations, start a server thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * that sets up an SSLServerSocket supporting that protocol. Then run
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * a client thread that attemps to open a connection with all
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * possible protocol combinataion.  Verify that we get handshake
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * exceptions correctly. Whenever the connection is established
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * successfully, verify that the negotiated protocol was correct.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * See results file in this directory for complete results.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    static final String[][] protocolStrings = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                                {"TLSv1"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                                {"TLSv1", "SSLv2Hello"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                                {"TLSv1", "SSLv3"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                                {"SSLv3", "SSLv2Hello"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                                {"SSLv3"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                                {"TLSv1", "SSLv3", "SSLv2Hello"}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                                };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    static final boolean [][] eXceptionArray = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        // Do we expect exception?       Protocols supported by the server
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        { false, true,  false, true,  true,  true }, // TLSv1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        { false, false, false, true,  true,  false}, // TLSv1,SSLv2Hello
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        { false, true,  false, true,  false, true }, // TLSv1,SSLv3
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        { true,  true,  false, false, false, false}, // SSLv3, SSLv2Hello
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        { true,  true,  false, true,  false, true }, // SSLv3
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        { false, false, false, false, false, false } // TLSv1,SSLv3,SSLv2Hello
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    static final String[][] protocolSelected = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        // TLSv1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        { "TLSv1",  null,   "TLSv1",  null,   null,     null },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        // TLSv1,SSLv2Hello
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        { "TLSv1", "TLSv1", "TLSv1",  null,   null,    "TLSv1"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        // TLSv1,SSLv3
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        { "TLSv1",  null,   "TLSv1",  null,   "SSLv3",  null },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        // SSLv3, SSLv2Hello
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        {  null,    null,   "SSLv3", "SSLv3", "SSLv3",  "SSLv3"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        // SSLv3
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        {  null,    null,   "SSLv3",  null,   "SSLv3",  null },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        // TLSv1,SSLv3,SSLv2Hello
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        { "TLSv1", "TLSv1", "TLSv1", "SSLv3", "SSLv3", "TLSv1" }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * Where do we find the keystores?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     */
23052
241885315119 8032473: Restructure JSSE regression test hierarchy in jdk test
xuelei
parents: 14342
diff changeset
   102
    final static String pathToStores = "../etc";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    static String passwd = "passphrase";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    static String keyStoreFile = "keystore";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    static String trustStoreFile = "truststore";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * Is the server ready to serve?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    volatile static boolean serverReady = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * Turn on SSL debugging?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    final static boolean debug = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    // use any free port by default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    volatile int serverPort = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    volatile Exception clientException = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    public static void main(String[] args) throws Exception {
28555
c7bf34f7b215 8061210: Issues in TLS
xuelei
parents: 23055
diff changeset
   123
        // reset the security property to make sure that the algorithms
c7bf34f7b215 8061210: Issues in TLS
xuelei
parents: 23055
diff changeset
   124
        // and keys used in this test are not disabled.
c7bf34f7b215 8061210: Issues in TLS
xuelei
parents: 23055
diff changeset
   125
        Security.setProperty("jdk.tls.disabledAlgorithms", "");
c7bf34f7b215 8061210: Issues in TLS
xuelei
parents: 23055
diff changeset
   126
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        String keyFilename =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            System.getProperty("test.src", "./") + "/" + pathToStores +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                "/" + keyStoreFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        String trustFilename =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            System.getProperty("test.src", "./") + "/" + pathToStores +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                "/" + trustStoreFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        System.setProperty("javax.net.ssl.keyStore", keyFilename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        System.setProperty("javax.net.ssl.keyStorePassword", passwd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        System.setProperty("javax.net.ssl.trustStore", trustFilename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        System.setProperty("javax.net.ssl.trustStorePassword", passwd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        if (debug)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            System.setProperty("javax.net.debug", "all");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
23055
63ec3bde101c 8036676: Rename class name testEnabledProtocols to TestEnabledProtocols
xuelei
parents: 23052
diff changeset
   142
        new TestEnabledProtocols();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
23055
63ec3bde101c 8036676: Rename class name testEnabledProtocols to TestEnabledProtocols
xuelei
parents: 23052
diff changeset
   145
    TestEnabledProtocols() throws Exception  {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
         * Start the tests.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        SSLServerSocketFactory sslssf =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        SSLServerSocket sslServerSocket =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            (SSLServerSocket) sslssf.createServerSocket(serverPort);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        serverPort = sslServerSocket.getLocalPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        // sslServerSocket.setNeedClientAuth(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        for (int i = 0; i < protocolStrings.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            String [] serverProtocols = protocolStrings[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            startServer ss = new startServer(serverProtocols,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                sslServerSocket, protocolStrings.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            ss.setDaemon(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            ss.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            for (int j = 0; j < protocolStrings.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                String [] clientProtocols = protocolStrings[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                startClient sc = new startClient(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                    clientProtocols, serverProtocols,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                    eXceptionArray[i][j], protocolSelected[i][j]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                sc.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                sc.join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                if (clientException != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                    ss.requestStop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                    throw clientException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            ss.requestStop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            System.out.println("Waiting for the server to complete");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            ss.join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    class startServer extends Thread  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        private String[] enabledP = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        SSLServerSocket sslServerSocket = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        int numExpConns;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        volatile boolean stopRequested = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        public startServer(String[] enabledProtocols,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                            SSLServerSocket sslServerSocket,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                            int numExpConns) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            super("Server Thread");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            serverReady = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            enabledP = enabledProtocols;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            this.sslServerSocket = sslServerSocket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            sslServerSocket.setEnabledProtocols(enabledP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            this.numExpConns = numExpConns;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        public void requestStop() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            stopRequested = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            int conns = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            while (!stopRequested) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                SSLSocket socket = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                    serverReady = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                    socket = (SSLSocket)sslServerSocket.accept();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                    conns++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                    // set ready to false. this is just to make the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                    // client wait and synchronise exception messages
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                    serverReady = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                    socket.startHandshake();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                    SSLSession session = socket.getSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                    session.invalidate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                    InputStream in = socket.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                    OutputStream out = socket.getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                    out.write(280);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                    in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                    socket.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                    // sleep for a while so that the server thread can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                    // stopped
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                    Thread.sleep(30);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                } catch (SSLHandshakeException se) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                    // ignore it; this is part of the testing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                    // log it for debugging
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                    System.out.println("Server SSLHandshakeException:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                    se.printStackTrace(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                } catch (java.io.InterruptedIOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                    // must have been interrupted, no harm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                } catch (java.lang.InterruptedException ie) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                    // must have been interrupted, no harm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                    break;
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   237
                } catch (SSLException ssle) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   238
                    // The client side may have closed the socket.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   239
                    System.out.println("Server SSLException:");
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   240
                    ssle.printStackTrace(System.out);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                    System.out.println("Server exception:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                    e.printStackTrace(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                    throw new RuntimeException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                        if (socket != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                            socket.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                    } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                        // ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                if (conns >= numExpConns) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                    break;
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    private static void showProtocols(String name, String[] protocols) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        System.out.println("Enabled protocols on the " + name + " are: " + Arrays.asList(protocols));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    class startClient extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        boolean hsCompleted = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        boolean exceptionExpected = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        private String[] enabledP = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        private String[] serverP = null; // used to print the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        private String protocolToUse = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        startClient(String[] enabledProtocol,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                    String[] serverP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                    boolean eXception,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                    String protocol) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            super("Client Thread");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            this.enabledP = enabledProtocol;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            this.serverP = serverP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            this.exceptionExpected = eXception;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            this.protocolToUse = protocol;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            SSLSocket sslSocket = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                while (!serverReady) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                    Thread.sleep(50);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                System.out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                System.out.println("=== Starting new test run ===");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                showProtocols("server", serverP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                showProtocols("client", enabledP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                SSLSocketFactory sslsf =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                    (SSLSocketFactory)SSLSocketFactory.getDefault();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                sslSocket = (SSLSocket)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                    sslsf.createSocket("localhost", serverPort);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                sslSocket.setEnabledProtocols(enabledP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                sslSocket.startHandshake();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                SSLSession session = sslSocket.getSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                session.invalidate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                String protocolName = session.getProtocol();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                System.out.println("Protocol name after getSession is " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                    protocolName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                if (protocolName.equals(protocolToUse)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                    System.out.println("** Success **");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                    System.out.println("** FAILURE ** ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                    throw new RuntimeException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                        ("expected protocol " + protocolToUse +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                         " but using " + protocolName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                InputStream in = sslSocket.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                OutputStream out = sslSocket.getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                out.write(280);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                sslSocket.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            } catch (SSLHandshakeException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                if (!exceptionExpected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                    System.out.println("Client got UNEXPECTED SSLHandshakeException:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                    e.printStackTrace(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                    System.out.println("** FAILURE **");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                    clientException = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                    System.out.println("Client got expected SSLHandshakeException:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                    e.printStackTrace(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                    System.out.println("** Success **");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            } catch (RuntimeException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                clientException = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                System.out.println("Client got UNEXPECTED Exception:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                e.printStackTrace(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                System.out.println("** FAILURE **");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                clientException = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
}