jdk/test/sun/security/ssl/AppInputStream/ReadHandshake.java
author xuelei
Tue, 02 Jun 2015 04:01:04 +0000
changeset 30904 ec0224270f90
parent 23052 241885315119
permissions -rw-r--r--
8043758: Datagram Transport Layer Security (DTLS) Reviewed-by: jnimeh, weijun, mullan, wetmore
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) 2001, 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
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 23052
diff changeset
    24
//
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 23052
diff changeset
    25
// SunJSSE does not support dynamic system properties, no way to re-use
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 23052
diff changeset
    26
// system properties in samevm/agentvm mode.
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 23052
diff changeset
    27
//
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 23052
diff changeset
    28
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @bug 4514971
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * @summary Verify applications do not read handshake data after failure
10328
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
    33
 * @run main/othervm ReadHandshake
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.net.ssl.*;
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 23052
diff changeset
    39
import java.security.Security;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
public class ReadHandshake {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
     * =============================================================
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
     * Set the various variables needed for the tests, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     * specify what tests to run on each side.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     * Should we run the client or server in a separate thread?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * Both sides can throw exceptions, but do you have a preference
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * as to which side should be the main thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    static boolean separateServerThread = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    // Note: we use anonymous ciphersuites only, no keys/ trusted certs needed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private final static String[] CLIENT_SUITES = new String[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        "SSL_DH_anon_WITH_3DES_EDE_CBC_SHA",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private final static String[] SERVER_SUITES = new String[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        "SSL_DH_anon_WITH_RC4_128_MD5",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * Is the server ready to serve?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    volatile static boolean serverReady = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * Turn on SSL debugging?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    static boolean debug = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * If the client or server is doing some kind of object creation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * that the other side depends on, and that thread prematurely
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * exits, you may experience a hang.  The test harness will
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * terminate all hung threads after its timeout has expired,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * currently 3 minutes by default, but you might try to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * smart about it....
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * Define the server side of the test.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * If the server prematurely exits, serverReady will be set to true
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * to avoid infinite hangs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    void doServerSide() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        SSLSocket sslSocket = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        SSLServerSocket sslServerSocket = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            SSLServerSocketFactory sslssf =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            sslServerSocket =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                (SSLServerSocket) sslssf.createServerSocket(serverPort);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            serverPort = sslServerSocket.getLocalPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            sslServerSocket.setEnabledCipherSuites(SERVER_SUITES);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
             * Signal Client, we're ready for his connect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            serverReady = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            System.out.println("Server waiting for connection");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            sslSocket = (SSLSocket) sslServerSocket.accept();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            InputStream sslIS = sslSocket.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            OutputStream sslOS = sslSocket.getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            System.out.println("Server starting handshake...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                sslIS.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                throw new Exception("No handshake exception on server side");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                System.out.println("Handshake failed on server side, OK");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            for (int i = 0; i < 3; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                    int ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                    if ((ch = sslIS.read()) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                        throw new Exception("Read succeeded server side: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                            + ch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                    System.out.println("Exception for read() on server, OK");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            closeSocket(sslSocket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            closeSocket(sslServerSocket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    private static void closeSocket(Socket s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            if (s != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                s.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            // ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    private static void closeSocket(ServerSocket s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            if (s != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                s.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            // ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * Define the client side of the test.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * If the server prematurely exits, serverReady will be set to true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * to avoid infinite hangs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    void doClientSide() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
         * Wait for server to get started.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        while (!serverReady) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            Thread.sleep(80);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        SSLSocket sslSocket = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            SSLSocketFactory sslsf =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                (SSLSocketFactory) SSLSocketFactory.getDefault();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            sslSocket = (SSLSocket)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                sslsf.createSocket("localhost", serverPort);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            sslSocket.setEnabledCipherSuites(CLIENT_SUITES);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            InputStream sslIS = sslSocket.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            OutputStream sslOS = sslSocket.getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            System.out.println("Client starting handshake...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                sslIS.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                throw new Exception("No handshake exception on client side");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                System.out.println("Handshake failed on client side, OK");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            for (int i = 0; i < 3; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                    int ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                    if ((ch = sslIS.read()) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                        throw new Exception("Read succeeded on client side: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                            + ch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                    System.out.println("Exception for read() on client, OK");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            sslSocket.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * =============================================================
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * The remainder is just support stuff
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    volatile int serverPort = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    volatile Exception serverException = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    volatile Exception clientException = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    public static void main(String[] args) throws Exception {
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 23052
diff changeset
   225
        // reset security properties to make sure that the algorithms
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 23052
diff changeset
   226
        // and keys used in this test are not disabled.
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 23052
diff changeset
   227
        Security.setProperty("jdk.tls.disabledAlgorithms", "");
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 23052
diff changeset
   228
        Security.setProperty("jdk.certpath.disabledAlgorithms", "");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        if (debug)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            System.setProperty("javax.net.debug", "all");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
         * Start the tests.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        new ReadHandshake();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    Thread clientThread = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    Thread serverThread = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * Primary constructor, used to drive remainder of the test.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * Fork off the other side, then do your work.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    ReadHandshake() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        startServer(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        startClient(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        serverThread.join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        clientThread.join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
         * When we get here, the test is pretty much over.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
         * If the main thread excepted, that propagates back
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
         * immediately.  If the other thread threw an exception, we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
         * should report back.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        if (serverException != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            if (clientException != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                System.out.println("Client exception:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                clientException.printStackTrace(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            throw serverException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        if (clientException != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            throw clientException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    void startServer(boolean newThread) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        if (newThread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            serverThread = new Thread() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                        doServerSide();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                    } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                         * Our server thread just died.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                         * Release the client, if not active already...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                        System.err.println("Server died...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                        serverReady = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                        serverException = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            serverThread.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            doServerSide();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    void startClient(boolean newThread) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        if (newThread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            clientThread = new Thread() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                        doClientSide();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                    } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                         * Our client thread just died.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                        System.err.println("Client died...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                        clientException = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            clientThread.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            doClientSide();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
}