test/jdk/javax/net/ssl/SSLSession/JSSERenegotiate.java
author mullan
Thu, 25 Oct 2018 13:55:28 -0400
changeset 52286 d98fb44ad6bf
parent 47216 71c04702a3d5
permissions -rw-r--r--
8211883: Disable anon and NULL cipher suites Reviewed-by: jnimeh
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
52286
d98fb44ad6bf 8211883: Disable anon and NULL cipher suites
mullan
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: 5182
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5182
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5182
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 4280338
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary "Unsupported SSL message version" SSLProtocolException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *      w/SSL_RSA_WITH_NULL_MD5
10328
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 6856
diff changeset
    29
 * @run main/othervm JSSERenegotiate
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 6856
diff changeset
    30
 *
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 6856
diff changeset
    31
 *     SunJSSE does not support dynamic system properties, no way to re-use
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 6856
diff changeset
    32
 *     system properties in samevm/agentvm mode.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * @author Ram Marti
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * @author Brad Wetmore
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.net.*;
52286
d98fb44ad6bf 8211883: Disable anon and NULL cipher suites
mullan
parents: 47216
diff changeset
    40
import java.security.Security;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.net.ssl.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
public class JSSERenegotiate {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    static final String suite1 = "SSL_RSA_WITH_NULL_MD5";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    static final String suite2 = "SSL_RSA_WITH_NULL_SHA";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    static final String dataString = "This is a test";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * =============================================================
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * Set the various variables needed for the tests, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * specify what tests to run on each side.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * Should we run the client or server in a separate thread?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * Both sides can throw exceptions, but do you have a preference
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * as to which side should be the main thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    static boolean separateServerThread = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * Where do we find the keystores?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     */
23052
241885315119 8032473: Restructure JSSE regression test hierarchy in jdk test
xuelei
parents: 14342
diff changeset
    67
    static String pathToStores = "../etc";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    static String keyStoreFile = "keystore";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    static String trustStoreFile = "truststore";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    static String passwd = "passphrase";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * Is the server ready to serve?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    volatile static boolean serverReady = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * Turn on SSL debugging?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    static boolean debug = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * If the client or server is doing some kind of object creation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * that the other side depends on, and that thread prematurely
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * exits, you may experience a hang.  The test harness will
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * terminate all hung threads after its timeout has expired,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * currently 3 minutes by default, but you might try to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * smart about it....
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * Define the server side of the test.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * If the server prematurely exits, serverReady will be set to true
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * to avoid infinite hangs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    void doServerSide() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        SSLServerSocketFactory sslssf =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        SSLServerSocket sslServerSocket =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            (SSLServerSocket) sslssf.createServerSocket(serverPort, 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        sslServerSocket.setNeedClientAuth(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        sslServerSocket.setEnabledCipherSuites(new String[] {suite1, suite2 });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        serverPort = sslServerSocket.getLocalPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
         * Signal Client, we're ready for his connect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        serverReady = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        DataInputStream sslIS =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            new DataInputStream(sslSocket.getInputStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        DataOutputStream sslOS =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            new DataOutputStream(sslSocket.getOutputStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                System.out.println("Received: " + sslIS.readUTF());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            } catch (SSLException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                System.out.println ("Received wrong exception");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                System.out.println ("Received right exception");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        sslSocket.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * Define the client side of the test.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * If the server prematurely exits, serverReady will be set to true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * to avoid infinite hangs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    void doClientSide() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
         * Wait for server to get started.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        while (!serverReady) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            Thread.sleep(50);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        SSLSocketFactory sslsf =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            (SSLSocketFactory) SSLSocketFactory.getDefault();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        SSLSocket sslSocket = (SSLSocket)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            sslsf.createSocket("localhost", serverPort);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        sslSocket.setEnabledCipherSuites(new String[] { suite1 });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        System.out.println("Enabled " + suite1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        DataInputStream sslIS =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            new DataInputStream(sslSocket.getInputStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        DataOutputStream sslOS =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            new DataOutputStream(sslSocket.getOutputStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        BufferedReader in = new BufferedReader(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            new InputStreamReader(sslSocket.getInputStream()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        sslOS.writeUTF("With " + suite1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        sslSocket.setEnabledCipherSuites(new String[] { suite2 });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        sslSocket.startHandshake();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        System.out.println("Enabled " + suite2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
         // write the message a few times - see bug 4462616 why we do this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        sslOS.writeUTF("With " + suite2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        sslOS.writeUTF("With " + suite2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        sslOS.writeUTF("With " + suite2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        sslSocket.setEnabledCipherSuites(new String[] { suite1 });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        sslSocket.startHandshake();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        System.out.println("Re-enabled " + suite1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        sslOS.writeUTF("With " + suite1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        sslOS.writeUTF("With " + suite1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        sslOS.writeUTF("With " + suite1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        sslSocket.close();
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
     * The remainder is just support stuff
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    // use any free port by default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    volatile int serverPort = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    volatile Exception serverException = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    volatile Exception clientException = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    public static void main(String[] args) throws Exception {
52286
d98fb44ad6bf 8211883: Disable anon and NULL cipher suites
mullan
parents: 47216
diff changeset
   194
        // reset the security property to make sure that the cipher suites
d98fb44ad6bf 8211883: Disable anon and NULL cipher suites
mullan
parents: 47216
diff changeset
   195
        // used in this test are not disabled
d98fb44ad6bf 8211883: Disable anon and NULL cipher suites
mullan
parents: 47216
diff changeset
   196
        Security.setProperty("jdk.tls.disabledAlgorithms", "");
d98fb44ad6bf 8211883: Disable anon and NULL cipher suites
mullan
parents: 47216
diff changeset
   197
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        String keyFilename =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            System.getProperty("test.src", "./") + "/" + pathToStores +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                "/" + keyStoreFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        String trustFilename =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            System.getProperty("test.src", "./") + "/" + pathToStores +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                "/" + trustStoreFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        System.setProperty("javax.net.ssl.keyStore", keyFilename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        System.setProperty("javax.net.ssl.keyStorePassword", passwd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        System.setProperty("javax.net.ssl.trustStore", trustFilename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        System.setProperty("javax.net.ssl.trustStorePassword", passwd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        if (debug)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            System.setProperty("javax.net.debug", "all");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
         * Start the tests.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        new JSSERenegotiate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    Thread clientThread = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    Thread serverThread = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * Primary constructor, used to drive remainder of the test.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * Fork off the other side, then do your work.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    JSSERenegotiate() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            if (separateServerThread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                startServer(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                startClient(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                startClient(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                startServer(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            // swallow for now.  Show later
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
         * Wait for other side to close down.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        if (separateServerThread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            serverThread.join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            clientThread.join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
         * When we get here, the test is pretty much over.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
         * Which side threw the error?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        Exception local;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        Exception remote;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        String whichRemote;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        if (separateServerThread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            remote = serverException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            local = clientException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            whichRemote = "server";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            remote = clientException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            local = serverException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            whichRemote = "client";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
         * If both failed, return the curthread's exception, but also
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
         * print the remote side Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        if ((local != null) && (remote != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            System.out.println(whichRemote + " also threw:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            remote.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            throw local;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        if (remote != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            throw remote;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        if (local != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            throw local;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    void startServer(boolean newThread) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        if (newThread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            serverThread = new Thread() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                        doServerSide();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                    } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                         * Our server thread just died.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                         * Release the client, if not active already...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                        System.err.println("Server died...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                        serverReady = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                        serverException = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            serverThread.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                doServerSide();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                serverException = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                serverReady = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    void startClient(boolean newThread) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        if (newThread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            clientThread = new Thread() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                        doClientSide();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                    } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                         * Our client thread just died.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                        System.err.println("Client died...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                        clientException = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            clientThread.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                doClientSide();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                clientException = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
}