test/jdk/sun/net/www/protocol/https/HttpsURLConnection/ReadTimeout.java
author jjiang
Wed, 23 May 2018 17:02:13 +0800
branchJDK-8145252-TLS13-branch
changeset 56595 fa746939d740
parent 47216 71c04702a3d5
child 54938 8c977741c3c8
permissions -rw-r--r--
sun/net/www/protocol/https/HttpsURLConnection/ReadTimeout.java adapts to new exception hierarchy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
56595
fa746939d740 sun/net/www/protocol/https/HttpsURLConnection/ReadTimeout.java adapts to new exception hierarchy
jjiang
parents: 47216
diff changeset
     2
 * Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 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
34379
e9e364c8c44e 8143298: Test ReadTimeout.java fails intermittently
xuelei
parents: 23052
diff changeset
    24
//
e9e364c8c44e 8143298: Test ReadTimeout.java fails intermittently
xuelei
parents: 23052
diff changeset
    25
// SunJSSE does not support dynamic system properties, no way to re-use
e9e364c8c44e 8143298: Test ReadTimeout.java fails intermittently
xuelei
parents: 23052
diff changeset
    26
// system properties in samevm/agentvm mode.
e9e364c8c44e 8143298: Test ReadTimeout.java fails intermittently
xuelei
parents: 23052
diff changeset
    27
//
e9e364c8c44e 8143298: Test ReadTimeout.java fails intermittently
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 4811482 4700777 4905410
10328
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
    32
 * @summary sun.net.client.defaultConnectTimeout should work with
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
    33
 *     HttpsURLConnection; HTTP client: Connect and read timeouts;
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
    34
 *     Https needs to support new tiger features that went into http
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
    35
 * @run main/othervm ReadTimeout
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.net.ssl.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
public class ReadTimeout {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
     * =============================================================
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     * Set the various variables needed for the tests, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     * specify what tests to run on each side.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * Should we run the client or server in a separate thread?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * Both sides can throw exceptions, but do you have a preference
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * as to which side should be the main thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    static boolean separateServerThread = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * Where do we find the keystores?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     */
23052
241885315119 8032473: Restructure JSSE regression test hierarchy in jdk test
xuelei
parents: 10328
diff changeset
    60
    static String pathToStores = "../../../../../../javax/net/ssl/etc";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    static String keyStoreFile = "keystore";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    static String trustStoreFile = "truststore";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    static String passwd = "passphrase";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * Is the server ready to serve?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    volatile static boolean serverReady = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * Turn on SSL debugging?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    static boolean debug = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * Message posted
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    static String postMsg = "Testing HTTP post on a https server";
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
        serverPort = sslServerSocket.getLocalPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
         * Signal Client, we're ready for his connect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        serverReady = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        try {
34379
e9e364c8c44e 8143298: Test ReadTimeout.java fails intermittently
xuelei
parents: 23052
diff changeset
   107
            try (SSLSocket sslSocket = (SSLSocket)sslServerSocket.accept()) {
e9e364c8c44e 8143298: Test ReadTimeout.java fails intermittently
xuelei
parents: 23052
diff changeset
   108
                InputStream sslIS = sslSocket.getInputStream();
e9e364c8c44e 8143298: Test ReadTimeout.java fails intermittently
xuelei
parents: 23052
diff changeset
   109
                BufferedReader br =
e9e364c8c44e 8143298: Test ReadTimeout.java fails intermittently
xuelei
parents: 23052
diff changeset
   110
                        new BufferedReader(new InputStreamReader(sslIS));
e9e364c8c44e 8143298: Test ReadTimeout.java fails intermittently
xuelei
parents: 23052
diff changeset
   111
                br.readLine();
e9e364c8c44e 8143298: Test ReadTimeout.java fails intermittently
xuelei
parents: 23052
diff changeset
   112
                while (!finished())  {
e9e364c8c44e 8143298: Test ReadTimeout.java fails intermittently
xuelei
parents: 23052
diff changeset
   113
                    Thread.sleep(2000);
e9e364c8c44e 8143298: Test ReadTimeout.java fails intermittently
xuelei
parents: 23052
diff changeset
   114
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            // doing second test
34379
e9e364c8c44e 8143298: Test ReadTimeout.java fails intermittently
xuelei
parents: 23052
diff changeset
   119
            try (SSLSocket sslSocket = (SSLSocket)sslServerSocket.accept()) {
e9e364c8c44e 8143298: Test ReadTimeout.java fails intermittently
xuelei
parents: 23052
diff changeset
   120
                InputStream sslIS = sslSocket.getInputStream();
e9e364c8c44e 8143298: Test ReadTimeout.java fails intermittently
xuelei
parents: 23052
diff changeset
   121
                BufferedReader br =
e9e364c8c44e 8143298: Test ReadTimeout.java fails intermittently
xuelei
parents: 23052
diff changeset
   122
                        new BufferedReader(new InputStreamReader(sslIS));
e9e364c8c44e 8143298: Test ReadTimeout.java fails intermittently
xuelei
parents: 23052
diff changeset
   123
                br.readLine();
e9e364c8c44e 8143298: Test ReadTimeout.java fails intermittently
xuelei
parents: 23052
diff changeset
   124
                while (!finished())  {
e9e364c8c44e 8143298: Test ReadTimeout.java fails intermittently
xuelei
parents: 23052
diff changeset
   125
                    Thread.sleep(2000);
e9e364c8c44e 8143298: Test ReadTimeout.java fails intermittently
xuelei
parents: 23052
diff changeset
   126
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        } catch (Exception e) {
34379
e9e364c8c44e 8143298: Test ReadTimeout.java fails intermittently
xuelei
parents: 23052
diff changeset
   129
            System.out.println("Should be an expected exception: " + e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        } finally {
34379
e9e364c8c44e 8143298: Test ReadTimeout.java fails intermittently
xuelei
parents: 23052
diff changeset
   131
            sslServerSocket.close();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    boolean isFinished = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    synchronized boolean finished () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        return (isFinished);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    synchronized void done () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        isFinished = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    synchronized void reset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        isFinished = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * Define the client side of the test.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * If the server prematurely exits, serverReady will be set to true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * to avoid infinite hangs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    void doClientSide() throws Exception {
10328
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   155
        HostnameVerifier reservedHV =
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   156
            HttpsURLConnection.getDefaultHostnameVerifier();
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   157
        try {
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   158
            /*
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   159
             * Wait for server to get started.
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   160
             */
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   161
            while (!serverReady) {
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   162
                Thread.sleep(50);
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   163
            }
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   164
            HttpsURLConnection http = null;
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   165
            try {
34379
e9e364c8c44e 8143298: Test ReadTimeout.java fails intermittently
xuelei
parents: 23052
diff changeset
   166
                URL url = new URL("https://localhost:" + serverPort);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
10328
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   168
                // set read timeout through system property
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   169
                System.setProperty("sun.net.client.defaultReadTimeout", "2000");
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   170
                HttpsURLConnection.setDefaultHostnameVerifier(
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   171
                                          new NameVerifier());
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   172
                http = (HttpsURLConnection)url.openConnection();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
34379
e9e364c8c44e 8143298: Test ReadTimeout.java fails intermittently
xuelei
parents: 23052
diff changeset
   174
                InputStream is = http.getInputStream();
e9e364c8c44e 8143298: Test ReadTimeout.java fails intermittently
xuelei
parents: 23052
diff changeset
   175
e9e364c8c44e 8143298: Test ReadTimeout.java fails intermittently
xuelei
parents: 23052
diff changeset
   176
                throw new Exception(
e9e364c8c44e 8143298: Test ReadTimeout.java fails intermittently
xuelei
parents: 23052
diff changeset
   177
                        "system property timeout configuration does not work");
56595
fa746939d740 sun/net/www/protocol/https/HttpsURLConnection/ReadTimeout.java adapts to new exception hierarchy
jjiang
parents: 47216
diff changeset
   178
            } catch (SSLException | SocketTimeoutException ex) {
34379
e9e364c8c44e 8143298: Test ReadTimeout.java fails intermittently
xuelei
parents: 23052
diff changeset
   179
                System.out.println("Got expected timeout exception for " +
56595
fa746939d740 sun/net/www/protocol/https/HttpsURLConnection/ReadTimeout.java adapts to new exception hierarchy
jjiang
parents: 47216
diff changeset
   180
                        "system property timeout configuration: " + getCause(ex));
34379
e9e364c8c44e 8143298: Test ReadTimeout.java fails intermittently
xuelei
parents: 23052
diff changeset
   181
            } finally {
10328
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   182
                done();
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   183
                http.disconnect();
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   184
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
10328
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   186
            try {
34379
e9e364c8c44e 8143298: Test ReadTimeout.java fails intermittently
xuelei
parents: 23052
diff changeset
   187
                URL url = new URL("https://localhost:" + serverPort);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
10328
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   189
                HttpsURLConnection.setDefaultHostnameVerifier(
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   190
                                          new NameVerifier());
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   191
                http = (HttpsURLConnection)url.openConnection();
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   192
                // set read timeout through API
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   193
                http.setReadTimeout(2000);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
34379
e9e364c8c44e 8143298: Test ReadTimeout.java fails intermittently
xuelei
parents: 23052
diff changeset
   195
                InputStream is = http.getInputStream();
e9e364c8c44e 8143298: Test ReadTimeout.java fails intermittently
xuelei
parents: 23052
diff changeset
   196
e9e364c8c44e 8143298: Test ReadTimeout.java fails intermittently
xuelei
parents: 23052
diff changeset
   197
                throw new Exception(
e9e364c8c44e 8143298: Test ReadTimeout.java fails intermittently
xuelei
parents: 23052
diff changeset
   198
                        "HttpsURLConnection.setReadTimeout() does not work");
56595
fa746939d740 sun/net/www/protocol/https/HttpsURLConnection/ReadTimeout.java adapts to new exception hierarchy
jjiang
parents: 47216
diff changeset
   199
            } catch (SSLException | SocketTimeoutException ex) {
34379
e9e364c8c44e 8143298: Test ReadTimeout.java fails intermittently
xuelei
parents: 23052
diff changeset
   200
                System.out.println("Got expected timeout exception for " +
56595
fa746939d740 sun/net/www/protocol/https/HttpsURLConnection/ReadTimeout.java adapts to new exception hierarchy
jjiang
parents: 47216
diff changeset
   201
                        "HttpsURLConnection.setReadTimeout(): " + getCause(ex));
34379
e9e364c8c44e 8143298: Test ReadTimeout.java fails intermittently
xuelei
parents: 23052
diff changeset
   202
            } finally {
10328
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   203
                done();
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   204
                http.disconnect();
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   205
            }
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   206
        } finally {
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   207
            HttpsURLConnection.setDefaultHostnameVerifier(reservedHV);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
56595
fa746939d740 sun/net/www/protocol/https/HttpsURLConnection/ReadTimeout.java adapts to new exception hierarchy
jjiang
parents: 47216
diff changeset
   211
    private Exception getCause(Exception ex) {
fa746939d740 sun/net/www/protocol/https/HttpsURLConnection/ReadTimeout.java adapts to new exception hierarchy
jjiang
parents: 47216
diff changeset
   212
        Exception cause = null;
fa746939d740 sun/net/www/protocol/https/HttpsURLConnection/ReadTimeout.java adapts to new exception hierarchy
jjiang
parents: 47216
diff changeset
   213
        if (ex instanceof SSLException) {
fa746939d740 sun/net/www/protocol/https/HttpsURLConnection/ReadTimeout.java adapts to new exception hierarchy
jjiang
parents: 47216
diff changeset
   214
            cause = (Exception) ex.getCause();
fa746939d740 sun/net/www/protocol/https/HttpsURLConnection/ReadTimeout.java adapts to new exception hierarchy
jjiang
parents: 47216
diff changeset
   215
            if (!(cause instanceof SocketTimeoutException)) {
fa746939d740 sun/net/www/protocol/https/HttpsURLConnection/ReadTimeout.java adapts to new exception hierarchy
jjiang
parents: 47216
diff changeset
   216
                throw new RuntimeException("Unexpected cause", cause);
fa746939d740 sun/net/www/protocol/https/HttpsURLConnection/ReadTimeout.java adapts to new exception hierarchy
jjiang
parents: 47216
diff changeset
   217
            }
fa746939d740 sun/net/www/protocol/https/HttpsURLConnection/ReadTimeout.java adapts to new exception hierarchy
jjiang
parents: 47216
diff changeset
   218
        } else {
fa746939d740 sun/net/www/protocol/https/HttpsURLConnection/ReadTimeout.java adapts to new exception hierarchy
jjiang
parents: 47216
diff changeset
   219
            cause = ex;
fa746939d740 sun/net/www/protocol/https/HttpsURLConnection/ReadTimeout.java adapts to new exception hierarchy
jjiang
parents: 47216
diff changeset
   220
        }
fa746939d740 sun/net/www/protocol/https/HttpsURLConnection/ReadTimeout.java adapts to new exception hierarchy
jjiang
parents: 47216
diff changeset
   221
fa746939d740 sun/net/www/protocol/https/HttpsURLConnection/ReadTimeout.java adapts to new exception hierarchy
jjiang
parents: 47216
diff changeset
   222
        return cause;
fa746939d740 sun/net/www/protocol/https/HttpsURLConnection/ReadTimeout.java adapts to new exception hierarchy
jjiang
parents: 47216
diff changeset
   223
    }
fa746939d740 sun/net/www/protocol/https/HttpsURLConnection/ReadTimeout.java adapts to new exception hierarchy
jjiang
parents: 47216
diff changeset
   224
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    static class NameVerifier implements HostnameVerifier {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        public boolean verify(String hostname, SSLSession session) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * =============================================================
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * The remainder is just support stuff
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    // use any free port by default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    volatile int serverPort = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    volatile Exception serverException = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    volatile Exception clientException = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        String keyFilename =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            System.getProperty("test.src", "./") + "/" + pathToStores +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                "/" + keyStoreFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        String trustFilename =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            System.getProperty("test.src", "./") + "/" + pathToStores +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                "/" + trustStoreFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        System.setProperty("javax.net.ssl.keyStore", keyFilename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        System.setProperty("javax.net.ssl.keyStorePassword", passwd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        System.setProperty("javax.net.ssl.trustStore", trustFilename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        System.setProperty("javax.net.ssl.trustStorePassword", passwd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        if (debug)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            System.setProperty("javax.net.debug", "all");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
         * Start the tests.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        new ReadTimeout();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    Thread clientThread = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    Thread serverThread = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * Primary constructor, used to drive remainder of the test.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * Fork off the other side, then do your work.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    ReadTimeout() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        if (separateServerThread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            startServer(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            startClient(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            startClient(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            startServer(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
         * Wait for other side to close down.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        if (separateServerThread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            serverThread.join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            clientThread.join();
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
         * When we get here, the test is pretty much over.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
         * If the main thread excepted, that propagates back
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
         * immediately.  If the other thread threw an exception, we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
         * should report back.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        if (serverException != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            throw serverException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        if (clientException != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            throw clientException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    void startServer(boolean newThread) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        if (newThread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            serverThread = new Thread() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                        doServerSide();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                    } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                         * Our server thread just died.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                         * Release the client, if not active already...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                        System.err.println("Server died...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                        serverReady = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                        serverException = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            serverThread.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            doServerSide();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    void startClient(boolean newThread) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        if (newThread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            clientThread = new Thread() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                        doClientSide();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                    } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                         * Our client thread just died.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                        System.err.println("Client died...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                        clientException = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            clientThread.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            doClientSide();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
}