jdk/test/javax/net/ssl/SSLEngine/ExtendedKeySocket.java
author xuelei
Wed, 05 Mar 2014 07:24:34 +0000
changeset 23052 241885315119
parent 14342 jdk/test/sun/security/ssl/javax/net/ssl/NewAPIs/SSLEngine/ExtendedKeySocket.java@8435a30053c1
permissions -rw-r--r--
8032473: Restructure JSSE regression test hierarchy in jdk test Reviewed-by: weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 10328
diff changeset
     2
 * Copyright (c) 2004, 2011, 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
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 4981697
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Rework the X509KeyManager to avoid incompatibility issues
10328
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
    28
 * @run main/othervm ExtendedKeySocket
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
    29
 *
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
    30
 *     SunJSSE does not support dynamic system properties, no way to re-use
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
    31
 *     system properties in samevm/agentvm mode.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * @author Brad R. Wetmore
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.net.ssl.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
public class ExtendedKeySocket {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
     * =============================================================
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
     * Set the various variables needed for the tests, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
     * specify what tests to run on each side.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     * Should we run the client or server in a separate thread?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     * Both sides can throw exceptions, but do you have a preference
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * as to which side should be the main thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    static boolean separateServerThread = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * Where do we find the keystores?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     */
23052
241885315119 8032473: Restructure JSSE regression test hierarchy in jdk test
xuelei
parents: 14342
diff changeset
    58
    static String pathToStores = "../etc";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    static String keyStoreFile = "keystore";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    static String trustStoreFile = "truststore";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    static char [] passwd = "passphrase".toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * Is the server ready to serve?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    volatile static boolean serverReady = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * Turn on SSL debugging?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    static boolean debug = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * If the client or server is doing some kind of object creation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * that the other side depends on, and that thread prematurely
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * exits, you may experience a hang.  The test harness will
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * terminate all hung threads after its timeout has expired,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * currently 3 minutes by default, but you might try to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * smart about it....
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    private String keyFilename =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            System.getProperty("test.src", "./") + "/" + pathToStores +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                "/" + keyStoreFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    private String trustFilename =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            System.getProperty("test.src", "./") + "/" + pathToStores +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                "/" + trustStoreFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    SSLContext getSSLContext(boolean abs) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        SSLContext ctx = SSLContext.getInstance("TLS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        KeyStore keyKS = KeyStore.getInstance("JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        keyKS.load(new FileInputStream(keyFilename), passwd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        KeyStore trustKS = KeyStore.getInstance("JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        trustKS.load(new FileInputStream(trustFilename), passwd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        kmf.init(keyKS, passwd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        KeyManager [] kms = kmf.getKeyManagers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        if (!(kms[0] instanceof X509ExtendedKeyManager)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            throw new Exception("kms[0] not X509ExtendedKeyManager");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        if (abs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            kms = new KeyManager [] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                new MyX509ExtendedKeyManager((X509ExtendedKeyManager)kms[0])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            kms = new KeyManager [] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                new MyX509KeyManager((X509KeyManager)kms[0])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        tmf.init(trustKS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        TrustManager [] tms = tmf.getTrustManagers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        ctx.init(kms, tms, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        return ctx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * Define the server side of the test.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * If the server prematurely exits, serverReady will be set to true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * to avoid infinite hangs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    void doServerSide() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        System.out.println("Starting Server1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        doServerTest(getSSLContext(true));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        System.out.println("Starting Server2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        doServerTest(getSSLContext(false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        System.out.println("Finishing Server");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    void doServerTest(SSLContext ctx) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        serverPort = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        SSLServerSocketFactory sslssf = ctx.getServerSocketFactory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        SSLServerSocket sslServerSocket =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            (SSLServerSocket) sslssf.createServerSocket(serverPort);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        sslServerSocket.setNeedClientAuth(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        serverPort = sslServerSocket.getLocalPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
         * Signal Client, we're ready for his connect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        serverReady = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        InputStream sslIS = sslSocket.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        OutputStream sslOS = sslSocket.getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        sslIS.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        sslOS.write(85);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        sslOS.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        sslSocket.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * Define the client side of the test.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * If the server prematurely exits, serverReady will be set to true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * to avoid infinite hangs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    void doClientSide() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        System.out.println("Starting Client1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        doClientTest(getSSLContext(true));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        System.out.println("Starting Client2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        doClientTest(getSSLContext(false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        System.out.println("Finishing Client");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    void doClientTest(SSLContext ctx) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
         * Wait for server to get started.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        while (!serverReady) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            Thread.sleep(50);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
         * Reset for next time through.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        serverReady = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        SSLSocketFactory sslsf = ctx.getSocketFactory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        SSLSocket sslSocket = (SSLSocket)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            sslsf.createSocket("localhost", serverPort);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        InputStream sslIS = sslSocket.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        OutputStream sslOS = sslSocket.getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        sslOS.write(280);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        sslOS.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        sslIS.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        sslSocket.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * =============================================================
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * The remainder is just support stuff
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    // use any free port by default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    volatile int serverPort = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    volatile Exception serverException = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    volatile Exception clientException = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        if (debug)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            System.setProperty("javax.net.debug", "all");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
         * Start the tests.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        new ExtendedKeySocket();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    Thread clientThread = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    Thread serverThread = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * Primary constructor, used to drive remainder of the test.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * Fork off the other side, then do your work.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    ExtendedKeySocket() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            if (separateServerThread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                startServer(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                startClient(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                startClient(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                startServer(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            // swallow for now.  Show later
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
         * Wait for other side to close down.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        if (separateServerThread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            serverThread.join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            clientThread.join();
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
         * When we get here, the test is pretty much over.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
         * Which side threw the error?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        Exception local;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        Exception remote;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        String whichRemote;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        if (separateServerThread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            remote = serverException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            local = clientException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            whichRemote = "server";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            remote = clientException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            local = serverException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            whichRemote = "client";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
         * If both failed, return the curthread's exception, but also
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
         * print the remote side Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        if ((local != null) && (remote != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            System.out.println(whichRemote + " also threw:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            remote.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            throw local;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        if (remote != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            throw remote;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        if (local != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            throw local;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    void startServer(boolean newThread) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        if (newThread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            serverThread = new Thread() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                        doServerSide();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                    } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                         * Our server thread just died.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                         * Release the client, if not active already...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                        System.err.println("Server died...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                        serverReady = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                        serverException = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            serverThread.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                doServerSide();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                serverException = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                serverReady = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    void startClient(boolean newThread) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        if (newThread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            clientThread = new Thread() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                        doClientSide();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                    } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                         * Our client thread just died.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                        System.err.println("Client died...");
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
            clientThread.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                doClientSide();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                clientException = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
}