test/jdk/sun/security/ssl/SSLSocketImpl/ClientTimeout.java
author xuelei
Mon, 25 Jun 2018 13:41:39 -0700
changeset 50768 68fa3d4026ea
parent 47216 71c04702a3d5
permissions -rw-r--r--
8196584: TLS 1.3 Implementation Reviewed-by: ascarpino, coffeys, dfuchs, jjiang, jnimeh, mullan, rhalade, ssahoo, valeriep, weijun, wetmore, xuelei Contributed-by: Adam Petcher <adam.petcher@oracle.com>, Amanda Jiang <amanda.jiang@oracle.com>, Anthony Scarpino <anthony.scarpino@oracle.com>, Bradford Wetmore <bradford.wetmore@oracle.com>, Jamil Nimeh <jamil.j.nimeh@oracle.com>, John Jiang <sha.jiang@oracle.com>, Rajan Halade <rajan.halade@oracle.com>, Sibabrata Sahoo <sibabrata.sahoo@oracle.com>, Valerie Peng <valerie.peng@oracle.com>, Weijun Wang <weijun.wang@oracle.com>, Xuelei Fan <xuelei.fan@oracle.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 10328
diff changeset
     2
 * Copyright (c) 2005, 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
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    24
// SunJSSE does not support dynamic system properties, no way to re-use
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    25
// system properties in samevm/agentvm mode.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    26
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @bug 4836493
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    30
 * @ignore need further evaluation
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @summary Socket timeouts for SSLSockets causes data corruption.
10328
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
    32
 * @run main/othervm ClientTimeout
2
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 java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.net.ssl.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
public class ClientTimeout {
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
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * Where do we find the keystores?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     */
23052
241885315119 8032473: Restructure JSSE regression test hierarchy in jdk test
xuelei
parents: 14342
diff changeset
    59
    static String pathToStores = "../../../../javax/net/ssl/etc";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    static String keyStoreFile = "keystore";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    static String trustStoreFile = "truststore";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    static String passwd = "passphrase";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * Is the server ready to serve?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    volatile static boolean serverReady = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * Turn on SSL debugging?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    static boolean debug = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * define the rhythm of timeout exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    static boolean rhythm = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * If the client or server is doing some kind of object creation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * that the other side depends on, and that thread prematurely
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * exits, you may experience a hang.  The test harness will
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * terminate all hung threads after its timeout has expired,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * currently 3 minutes by default, but you might try to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * smart about it....
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * Define the server side of the test.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * If the server prematurely exits, serverReady will be set to true
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * to avoid infinite hangs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    void doServerSide() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        SSLServerSocketFactory sslssf =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        SSLServerSocket sslServerSocket =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            (SSLServerSocket) sslssf.createServerSocket(serverPort);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        serverPort = sslServerSocket.getLocalPort();
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
        SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        InputStream sslIS = sslSocket.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        OutputStream sslOS = sslSocket.getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        sslSocket.startHandshake();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        // transfer a file to client.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        String transFilename =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            System.getProperty("test.src", "./") + "/" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                        this.getClass().getName() + ".java";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        MessageDigest md = MessageDigest.getInstance("SHA");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        DigestInputStream transIns = new DigestInputStream(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                new FileInputStream(transFilename), md);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        byte[] bytes = new byte[2000];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            // reset the cycle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            if (i >= bytes.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            int length = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            if ((length = transIns.read(bytes, 0, i++)) == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                sslOS.write(bytes, 0, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                sslOS.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                if (i % 3 == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                    Thread.sleep(300);  // Stall past the timeout...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        serverDigest = md.digest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        transIns.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        sslSocket.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * Define the client side of the test.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * If the server prematurely exits, serverReady will be set to true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * to avoid infinite hangs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    void doClientSide() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        boolean caught = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
         * Wait for server to get started.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        while (!serverReady) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            Thread.sleep(50);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        Socket baseSocket = new Socket("localhost", serverPort) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            MyInputStream ins = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            public InputStream getInputStream() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                if (ins != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                    return ins;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                    ins = new MyInputStream(super.getInputStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                    return ins;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        SSLSocketFactory sslsf =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            (SSLSocketFactory) SSLSocketFactory.getDefault();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        SSLSocket sslSocket = (SSLSocket)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            sslsf.createSocket(baseSocket, "localhost", serverPort, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        InputStream sslIS = sslSocket.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        OutputStream sslOS = sslSocket.getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        // handshaking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        sslSocket.setSoTimeout(100); // The stall timeout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                rhythm = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                sslSocket.startHandshake();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            } catch (SocketTimeoutException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                System.out.println("Handshaker exception: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                        + e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        // read application data from server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        MessageDigest md = MessageDigest.getInstance("SHA");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        DigestInputStream transIns = new DigestInputStream(sslIS, md);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        byte[] bytes = new byte[2000];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                rhythm = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                while (transIns.read(bytes, 0, 17) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                    rhythm = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            } catch (SocketTimeoutException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                System.out.println("InputStream Exception: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                        + e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        // Wait for server to get ready.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        while (serverDigest == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            Thread.sleep(20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        byte[] cliDigest = md.digest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        if (!Arrays.equals(cliDigest, serverDigest)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            throw new Exception("Application data trans error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        transIns.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        sslSocket.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * =============================================================
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * The remainder is just support stuff
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    static class MyInputStream extends InputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        InputStream ins = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        public MyInputStream(InputStream ins) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            this.ins = ins;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        public int read() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            return read(new byte[1], 0, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        public int read(byte[] data, int offset, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            if (!ClientTimeout.rhythm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                throw new SocketTimeoutException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                        "Throwing a timeout exception");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            ClientTimeout.rhythm = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            return ins.read(data, offset, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    // use any free port by default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    volatile int serverPort = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    volatile Exception serverException = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    volatile Exception clientException = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    volatile byte[] serverDigest = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        String keyFilename =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            System.getProperty("test.src", "./") + "/" + pathToStores +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                "/" + keyStoreFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        String trustFilename =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            System.getProperty("test.src", "./") + "/" + pathToStores +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                "/" + trustStoreFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        System.setProperty("javax.net.ssl.keyStore", keyFilename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        System.setProperty("javax.net.ssl.keyStorePassword", passwd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        System.setProperty("javax.net.ssl.trustStore", trustFilename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        System.setProperty("javax.net.ssl.trustStorePassword", passwd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        if (debug)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            System.setProperty("javax.net.debug", "all");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
         * Start the tests.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        new ClientTimeout();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    Thread clientThread = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    Thread serverThread = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * Primary constructor, used to drive remainder of the test.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * Fork off the other side, then do your work.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    ClientTimeout() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        if (separateServerThread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            startServer(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            startClient(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            startClient(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            startServer(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
         * Wait for other side to close down.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        if (separateServerThread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            serverThread.join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            clientThread.join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
         * When we get here, the test is pretty much over.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
         * If the main thread excepted, that propagates back
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
         * immediately.  If the other thread threw an exception, we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
         * should report back.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        if (serverException != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            System.out.print("Server Exception:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            throw serverException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        if (clientException != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            System.out.print("Client Exception:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            throw clientException;
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 startServer(boolean newThread) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        if (newThread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            serverThread = 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
                        doServerSide();
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 server thread just died.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                         * Release the client, if not active already...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                        System.err.println("Server died...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                        System.err.println(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                        serverReady = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                        serverException = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            serverThread.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            doServerSide();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    void startClient(boolean newThread) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        if (newThread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            clientThread = new Thread() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                        doClientSide();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                    } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                         * Our client thread just died.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                        System.err.println("Client died...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                        clientException = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            clientThread.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            doClientSide();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
}