test/jdk/sun/net/www/http/HttpClient/IsAvailable.java
author dfuchs
Thu, 15 Aug 2019 12:58:27 +0100
changeset 57760 ec948d19180a
parent 47216 71c04702a3d5
permissions -rw-r--r--
8229486: Replace wildcard address with loopback or local host in tests - part 21 Reviewed-by: chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16477
15ee12718efe 8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents:
diff changeset
     1
/*
57760
ec948d19180a 8229486: Replace wildcard address with loopback or local host in tests - part 21
dfuchs
parents: 47216
diff changeset
     2
 * Copyright (c) 2013, 2019, Oracle and/or its affiliates. All rights reserved.
16477
15ee12718efe 8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
15ee12718efe 8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents:
diff changeset
     4
 *
15ee12718efe 8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
15ee12718efe 8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
15ee12718efe 8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents:
diff changeset
     7
 * published by the Free Software Foundation.
15ee12718efe 8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents:
diff changeset
     8
 *
15ee12718efe 8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
15ee12718efe 8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15ee12718efe 8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15ee12718efe 8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
15ee12718efe 8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents:
diff changeset
    13
 * accompanied this code).
15ee12718efe 8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents:
diff changeset
    14
 *
15ee12718efe 8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
15ee12718efe 8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
15ee12718efe 8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
15ee12718efe 8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents:
diff changeset
    18
 *
15ee12718efe 8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
15ee12718efe 8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
15ee12718efe 8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents:
diff changeset
    21
 * questions.
15ee12718efe 8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents:
diff changeset
    22
 */
15ee12718efe 8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents:
diff changeset
    23
15ee12718efe 8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents:
diff changeset
    24
/*
15ee12718efe 8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents:
diff changeset
    25
 * @test
15ee12718efe 8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents:
diff changeset
    26
 * @bug 8009650
15ee12718efe 8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents:
diff changeset
    27
 * @summary HttpClient available() check throws SocketException when connection
15ee12718efe 8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents:
diff changeset
    28
 * has been closed
30820
0d4717a011d3 8081347: Add @modules to jdk_core tests
mchung
parents: 16477
diff changeset
    29
 * @modules java.base/sun.net
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 30820
diff changeset
    30
 *          java.base/sun.net.www.http:+open
57760
ec948d19180a 8229486: Replace wildcard address with loopback or local host in tests - part 21
dfuchs
parents: 47216
diff changeset
    31
 * @library /test/lib
16477
15ee12718efe 8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents:
diff changeset
    32
 */
15ee12718efe 8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents:
diff changeset
    33
57760
ec948d19180a 8229486: Replace wildcard address with loopback or local host in tests - part 21
dfuchs
parents: 47216
diff changeset
    34
import java.net.InetAddress;
ec948d19180a 8229486: Replace wildcard address with loopback or local host in tests - part 21
dfuchs
parents: 47216
diff changeset
    35
import java.net.InetSocketAddress;
16477
15ee12718efe 8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents:
diff changeset
    36
import java.net.URL;
15ee12718efe 8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents:
diff changeset
    37
import java.net.ServerSocket;
15ee12718efe 8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents:
diff changeset
    38
import sun.net.www.http.HttpClient;
15ee12718efe 8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents:
diff changeset
    39
import java.security.*;
15ee12718efe 8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents:
diff changeset
    40
import java.lang.reflect.Method;
57760
ec948d19180a 8229486: Replace wildcard address with loopback or local host in tests - part 21
dfuchs
parents: 47216
diff changeset
    41
import jdk.test.lib.net.URIBuilder;
16477
15ee12718efe 8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents:
diff changeset
    42
15ee12718efe 8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents:
diff changeset
    43
public class IsAvailable {
15ee12718efe 8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents:
diff changeset
    44
15ee12718efe 8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents:
diff changeset
    45
    public static void main(String[] args) throws Exception {
15ee12718efe 8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents:
diff changeset
    46
        int readTimeout = 20;
57760
ec948d19180a 8229486: Replace wildcard address with loopback or local host in tests - part 21
dfuchs
parents: 47216
diff changeset
    47
        ServerSocket ss = new ServerSocket();
ec948d19180a 8229486: Replace wildcard address with loopback or local host in tests - part 21
dfuchs
parents: 47216
diff changeset
    48
        InetAddress loopback = InetAddress.getLoopbackAddress();
ec948d19180a 8229486: Replace wildcard address with loopback or local host in tests - part 21
dfuchs
parents: 47216
diff changeset
    49
        ss.bind(new InetSocketAddress(loopback, 0));
ec948d19180a 8229486: Replace wildcard address with loopback or local host in tests - part 21
dfuchs
parents: 47216
diff changeset
    50
ec948d19180a 8229486: Replace wildcard address with loopback or local host in tests - part 21
dfuchs
parents: 47216
diff changeset
    51
        try (ServerSocket toclose = ss) {
16477
15ee12718efe 8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents:
diff changeset
    52
57760
ec948d19180a 8229486: Replace wildcard address with loopback or local host in tests - part 21
dfuchs
parents: 47216
diff changeset
    53
            URL url1 = URIBuilder.newBuilder()
ec948d19180a 8229486: Replace wildcard address with loopback or local host in tests - part 21
dfuchs
parents: 47216
diff changeset
    54
                .scheme("http")
ec948d19180a 8229486: Replace wildcard address with loopback or local host in tests - part 21
dfuchs
parents: 47216
diff changeset
    55
                .loopback()
ec948d19180a 8229486: Replace wildcard address with loopback or local host in tests - part 21
dfuchs
parents: 47216
diff changeset
    56
                .port(ss.getLocalPort())
ec948d19180a 8229486: Replace wildcard address with loopback or local host in tests - part 21
dfuchs
parents: 47216
diff changeset
    57
                .toURL();
16477
15ee12718efe 8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents:
diff changeset
    58
57760
ec948d19180a 8229486: Replace wildcard address with loopback or local host in tests - part 21
dfuchs
parents: 47216
diff changeset
    59
            HttpClient c1 = HttpClient.New(url1);
16477
15ee12718efe 8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents:
diff changeset
    60
57760
ec948d19180a 8229486: Replace wildcard address with loopback or local host in tests - part 21
dfuchs
parents: 47216
diff changeset
    61
            Method available = HttpClient.class.
ec948d19180a 8229486: Replace wildcard address with loopback or local host in tests - part 21
dfuchs
parents: 47216
diff changeset
    62
                    getDeclaredMethod("available", null);
ec948d19180a 8229486: Replace wildcard address with loopback or local host in tests - part 21
dfuchs
parents: 47216
diff changeset
    63
            available.setAccessible(true);
16477
15ee12718efe 8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents:
diff changeset
    64
57760
ec948d19180a 8229486: Replace wildcard address with loopback or local host in tests - part 21
dfuchs
parents: 47216
diff changeset
    65
            c1.setReadTimeout(readTimeout);
ec948d19180a 8229486: Replace wildcard address with loopback or local host in tests - part 21
dfuchs
parents: 47216
diff changeset
    66
            boolean a = (boolean) available.invoke(c1);
ec948d19180a 8229486: Replace wildcard address with loopback or local host in tests - part 21
dfuchs
parents: 47216
diff changeset
    67
            if (!a) {
ec948d19180a 8229486: Replace wildcard address with loopback or local host in tests - part 21
dfuchs
parents: 47216
diff changeset
    68
                throw new RuntimeException("connection should be available");
ec948d19180a 8229486: Replace wildcard address with loopback or local host in tests - part 21
dfuchs
parents: 47216
diff changeset
    69
            }
ec948d19180a 8229486: Replace wildcard address with loopback or local host in tests - part 21
dfuchs
parents: 47216
diff changeset
    70
            if (c1.getReadTimeout() != readTimeout) {
ec948d19180a 8229486: Replace wildcard address with loopback or local host in tests - part 21
dfuchs
parents: 47216
diff changeset
    71
                throw new RuntimeException("read timeout has been altered");
ec948d19180a 8229486: Replace wildcard address with loopback or local host in tests - part 21
dfuchs
parents: 47216
diff changeset
    72
            }
16477
15ee12718efe 8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents:
diff changeset
    73
57760
ec948d19180a 8229486: Replace wildcard address with loopback or local host in tests - part 21
dfuchs
parents: 47216
diff changeset
    74
            c1.closeServer();
ec948d19180a 8229486: Replace wildcard address with loopback or local host in tests - part 21
dfuchs
parents: 47216
diff changeset
    75
ec948d19180a 8229486: Replace wildcard address with loopback or local host in tests - part 21
dfuchs
parents: 47216
diff changeset
    76
            a = (boolean) available.invoke(c1);
ec948d19180a 8229486: Replace wildcard address with loopback or local host in tests - part 21
dfuchs
parents: 47216
diff changeset
    77
            if (a) {
ec948d19180a 8229486: Replace wildcard address with loopback or local host in tests - part 21
dfuchs
parents: 47216
diff changeset
    78
                throw new RuntimeException("connection shouldn't be available");
ec948d19180a 8229486: Replace wildcard address with loopback or local host in tests - part 21
dfuchs
parents: 47216
diff changeset
    79
            }
16477
15ee12718efe 8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents:
diff changeset
    80
        }
15ee12718efe 8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents:
diff changeset
    81
    }
15ee12718efe 8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents:
diff changeset
    82
}