test/jdk/java/net/Socks/SocksIPv6Test.java
author ngasson
Thu, 22 Aug 2019 10:53:48 +0800
changeset 57832 da4cf75505a7
parent 54634 59c01214e478
permissions -rw-r--r--
8229912: [TESTBUG] java/net/Socks/SocksIPv6Test fails without IPv6 Reviewed-by: alanb, dfuchs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
22276
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
     1
/*
54634
59c01214e478 8129315: java/net/Socket/LingerTest.java and java/net/Socket/ShutdownBoth.java timeout intermittently
dfuchs
parents: 53478
diff changeset
     2
 * Copyright (c) 2013, 2019, Oracle and/or its affiliates. All rights reserved.
22276
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
     4
 *
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
     7
 * published by the Free Software Foundation.
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
     8
 *
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
    13
 * accompanied this code).
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
    14
 *
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
    18
 *
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
    21
 * questions.
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
    22
 */
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
    23
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
    24
/* @test
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
    25
 * @bug 7100957
38883
d5de564f8089 8151913: Fix module dependencies in java/net tests
mli
parents: 22276
diff changeset
    26
 * @modules jdk.httpserver
57832
da4cf75505a7 8229912: [TESTBUG] java/net/Socks/SocksIPv6Test fails without IPv6
ngasson
parents: 54634
diff changeset
    27
 * @library /test/lib
22276
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
    28
 * @summary Java doesn't correctly handle the SOCKS protocol when used over IPv6.
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
    29
 * @run testng SocksIPv6Test
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
    30
 */
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
    31
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
    32
import java.io.BufferedReader;
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
    33
import java.io.IOException;
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
    34
import java.io.InputStreamReader;
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
    35
import java.io.OutputStreamWriter;
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
    36
import java.net.Authenticator;
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
    37
import java.net.InetSocketAddress;
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
    38
import java.net.URL;
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
    39
import java.net.Proxy;
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
    40
import java.lang.Override;
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
    41
import java.net.InetAddress;
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
    42
import java.net.Inet6Address;
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
    43
import java.net.ServerSocket;
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
    44
import java.net.SocketException;
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
    45
import java.net.NetworkInterface;
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
    46
import java.util.Collections;
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
    47
import java.util.List;
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
    48
import com.sun.net.httpserver.*;
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
    49
import java.io.BufferedWriter;
57832
da4cf75505a7 8229912: [TESTBUG] java/net/Socks/SocksIPv6Test fails without IPv6
ngasson
parents: 54634
diff changeset
    50
import org.testng.SkipException;
22276
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
    51
import org.testng.annotations.AfterClass;
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
    52
import org.testng.annotations.BeforeClass;
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
    53
import org.testng.annotations.Test;
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
    54
57832
da4cf75505a7 8229912: [TESTBUG] java/net/Socks/SocksIPv6Test fails without IPv6
ngasson
parents: 54634
diff changeset
    55
import jdk.test.lib.NetworkConfiguration;
da4cf75505a7 8229912: [TESTBUG] java/net/Socks/SocksIPv6Test fails without IPv6
ngasson
parents: 54634
diff changeset
    56
22276
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
    57
import static org.testng.Assert.*;
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
    58
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
    59
public class SocksIPv6Test {
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
    60
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
    61
    private HttpServer server;
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
    62
    private SocksServer socks;
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
    63
    private String response = "Hello.";
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
    64
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
    65
    @BeforeClass
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
    66
    public void setUp() throws Exception {
57832
da4cf75505a7 8229912: [TESTBUG] java/net/Socks/SocksIPv6Test fails without IPv6
ngasson
parents: 54634
diff changeset
    67
        if (!ensureInet6AddressFamily() || !ensureIPv6OnLoopback()) {
da4cf75505a7 8229912: [TESTBUG] java/net/Socks/SocksIPv6Test fails without IPv6
ngasson
parents: 54634
diff changeset
    68
            NetworkConfiguration.printSystemConfiguration(System.out);
da4cf75505a7 8229912: [TESTBUG] java/net/Socks/SocksIPv6Test fails without IPv6
ngasson
parents: 54634
diff changeset
    69
            throw new SkipException("Host does not support IPv6");
da4cf75505a7 8229912: [TESTBUG] java/net/Socks/SocksIPv6Test fails without IPv6
ngasson
parents: 54634
diff changeset
    70
        }
22276
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
    71
54634
59c01214e478 8129315: java/net/Socket/LingerTest.java and java/net/Socket/ShutdownBoth.java timeout intermittently
dfuchs
parents: 53478
diff changeset
    72
        server = HttpServer.create(new InetSocketAddress("::1", 0), 0);
22276
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
    73
        server.createContext("/", ex -> {
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
    74
            ex.sendResponseHeaders(200, response.length());
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
    75
            try (BufferedWriter writer = new BufferedWriter(
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
    76
                    new OutputStreamWriter(ex.getResponseBody(), "UTF-8"))) {
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
    77
                writer.write(response);
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
    78
            }
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
    79
            ex.close();
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
    80
        });
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
    81
        server.start();
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
    82
54634
59c01214e478 8129315: java/net/Socket/LingerTest.java and java/net/Socket/ShutdownBoth.java timeout intermittently
dfuchs
parents: 53478
diff changeset
    83
        socks = new SocksServer(InetAddress.getByName("::1"), 0, false);
22276
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
    84
        socks.addUser("user", "pass");
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
    85
        socks.start();
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
    86
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
    87
        Authenticator.setDefault(new Authenticator() {
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
    88
            @Override
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
    89
            protected java.net.PasswordAuthentication getPasswordAuthentication() {
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
    90
                return new java.net.PasswordAuthentication(
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
    91
                        "user", "pass".toCharArray());
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
    92
            }
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
    93
        });
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
    94
    }
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
    95
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
    96
    private boolean ensureIPv6OnLoopback() throws Exception {
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
    97
        boolean ipv6 = false;
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
    98
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
    99
        List<NetworkInterface> nics = Collections.list(NetworkInterface.getNetworkInterfaces());
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
   100
        for (NetworkInterface nic : nics) {
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
   101
            if (!nic.isLoopback()) {
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
   102
                continue;
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
   103
            }
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
   104
            List<InetAddress> addrs = Collections.list(nic.getInetAddresses());
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
   105
            for (InetAddress addr : addrs) {
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
   106
                if (addr instanceof Inet6Address) {
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
   107
                    ipv6 = true;
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
   108
                    break;
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
   109
                }
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
   110
            }
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
   111
        }
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
   112
        if (!ipv6)
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
   113
            System.out.println("IPv6 is not enabled on loopback. Skipping test suite.");
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
   114
        return ipv6;
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
   115
    }
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
   116
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
   117
    private boolean ensureInet6AddressFamily() throws IOException {
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
   118
        try (ServerSocket s = new ServerSocket()) {
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
   119
            s.bind(new InetSocketAddress("::1", 0));
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
   120
            return true;
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
   121
        } catch (SocketException e) {
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
   122
            System.out.println("Inet 6 address family is not available. Skipping test suite.");
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
   123
        }
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
   124
        return false;
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
   125
    }
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
   126
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
   127
    @Test(groups = "unit")
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
   128
    public void testSocksOverIPv6() throws Exception {
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
   129
        Proxy proxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress("::1",
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
   130
                socks.getPort()));
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
   131
        URL url = new URL("http://[::1]:" + server.getAddress().getPort());
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
   132
        java.net.URLConnection conn = url.openConnection(proxy);
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
   133
        String actual = "";
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
   134
        try (BufferedReader reader = new BufferedReader(
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
   135
                new InputStreamReader(conn.getInputStream()))) {
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
   136
            actual = reader.readLine();
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
   137
        }
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
   138
        assertEquals(actual, response);
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
   139
    }
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
   140
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
   141
    @Test(groups = "unit")
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
   142
    public void testSocksOverIPv6Hostname() throws Exception {
54634
59c01214e478 8129315: java/net/Socket/LingerTest.java and java/net/Socket/ShutdownBoth.java timeout intermittently
dfuchs
parents: 53478
diff changeset
   143
        InetAddress ipv6Loopback = InetAddress.getByName("::1");
59c01214e478 8129315: java/net/Socket/LingerTest.java and java/net/Socket/ShutdownBoth.java timeout intermittently
dfuchs
parents: 53478
diff changeset
   144
        String ipv6Hostname = ipv6Loopback.getHostName();
59c01214e478 8129315: java/net/Socket/LingerTest.java and java/net/Socket/ShutdownBoth.java timeout intermittently
dfuchs
parents: 53478
diff changeset
   145
        String ipv6HostAddress = ipv6Loopback.getHostAddress();
59c01214e478 8129315: java/net/Socket/LingerTest.java and java/net/Socket/ShutdownBoth.java timeout intermittently
dfuchs
parents: 53478
diff changeset
   146
        InetAddress ipv4Loopback;
59c01214e478 8129315: java/net/Socket/LingerTest.java and java/net/Socket/ShutdownBoth.java timeout intermittently
dfuchs
parents: 53478
diff changeset
   147
        String ipv4Hostname;
59c01214e478 8129315: java/net/Socket/LingerTest.java and java/net/Socket/ShutdownBoth.java timeout intermittently
dfuchs
parents: 53478
diff changeset
   148
        String ipv4HostAddress;
59c01214e478 8129315: java/net/Socket/LingerTest.java and java/net/Socket/ShutdownBoth.java timeout intermittently
dfuchs
parents: 53478
diff changeset
   149
        try {
59c01214e478 8129315: java/net/Socket/LingerTest.java and java/net/Socket/ShutdownBoth.java timeout intermittently
dfuchs
parents: 53478
diff changeset
   150
            ipv4Loopback = InetAddress.getByName("127.0.0.1");
59c01214e478 8129315: java/net/Socket/LingerTest.java and java/net/Socket/ShutdownBoth.java timeout intermittently
dfuchs
parents: 53478
diff changeset
   151
            ipv4Hostname = ipv4Loopback == null ? null : ipv4Loopback.getHostName();
59c01214e478 8129315: java/net/Socket/LingerTest.java and java/net/Socket/ShutdownBoth.java timeout intermittently
dfuchs
parents: 53478
diff changeset
   152
            ipv4HostAddress = ipv4Loopback == null ? null : ipv4Loopback.getHostAddress();
59c01214e478 8129315: java/net/Socket/LingerTest.java and java/net/Socket/ShutdownBoth.java timeout intermittently
dfuchs
parents: 53478
diff changeset
   153
        } catch (IOException io) {
59c01214e478 8129315: java/net/Socket/LingerTest.java and java/net/Socket/ShutdownBoth.java timeout intermittently
dfuchs
parents: 53478
diff changeset
   154
            ipv4Hostname = null;
59c01214e478 8129315: java/net/Socket/LingerTest.java and java/net/Socket/ShutdownBoth.java timeout intermittently
dfuchs
parents: 53478
diff changeset
   155
            ipv4HostAddress = null;
59c01214e478 8129315: java/net/Socket/LingerTest.java and java/net/Socket/ShutdownBoth.java timeout intermittently
dfuchs
parents: 53478
diff changeset
   156
        }
22276
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
   157
54634
59c01214e478 8129315: java/net/Socket/LingerTest.java and java/net/Socket/ShutdownBoth.java timeout intermittently
dfuchs
parents: 53478
diff changeset
   158
        System.out.println("ipv6Hostname: " + ipv6Hostname + " / " + ipv6HostAddress);
59c01214e478 8129315: java/net/Socket/LingerTest.java and java/net/Socket/ShutdownBoth.java timeout intermittently
dfuchs
parents: 53478
diff changeset
   159
        System.out.println("ipv4Hostname: " + ipv4Hostname + " / " + ipv4HostAddress);
59c01214e478 8129315: java/net/Socket/LingerTest.java and java/net/Socket/ShutdownBoth.java timeout intermittently
dfuchs
parents: 53478
diff changeset
   160
59c01214e478 8129315: java/net/Socket/LingerTest.java and java/net/Socket/ShutdownBoth.java timeout intermittently
dfuchs
parents: 53478
diff changeset
   161
        if (ipv6Hostname.equals(ipv6HostAddress)) {
22276
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
   162
            System.out.println("Unable to get the hostname of the IPv6 loopback "
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
   163
                    + "address. Skipping test case.");
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
   164
            return;
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
   165
        }
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
   166
54634
59c01214e478 8129315: java/net/Socket/LingerTest.java and java/net/Socket/ShutdownBoth.java timeout intermittently
dfuchs
parents: 53478
diff changeset
   167
        if (ipv4Hostname != null && ipv6Hostname.equals(ipv4Hostname)) {
22276
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
   168
            System.out.println("IPv6 and IPv4 loopback addresses map to the"
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
   169
                    + " same hostname. Skipping test case.");
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
   170
            return;
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
   171
        }
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
   172
54634
59c01214e478 8129315: java/net/Socket/LingerTest.java and java/net/Socket/ShutdownBoth.java timeout intermittently
dfuchs
parents: 53478
diff changeset
   173
        if (!InetAddress.getByName(ipv6Hostname).getHostAddress()
59c01214e478 8129315: java/net/Socket/LingerTest.java and java/net/Socket/ShutdownBoth.java timeout intermittently
dfuchs
parents: 53478
diff changeset
   174
                .equals(ipv6HostAddress)) {
59c01214e478 8129315: java/net/Socket/LingerTest.java and java/net/Socket/ShutdownBoth.java timeout intermittently
dfuchs
parents: 53478
diff changeset
   175
            System.out.println(ipv6Hostname + " resolves to \""
59c01214e478 8129315: java/net/Socket/LingerTest.java and java/net/Socket/ShutdownBoth.java timeout intermittently
dfuchs
parents: 53478
diff changeset
   176
                    + InetAddress.getByName(ipv6Hostname).getHostAddress()
59c01214e478 8129315: java/net/Socket/LingerTest.java and java/net/Socket/ShutdownBoth.java timeout intermittently
dfuchs
parents: 53478
diff changeset
   177
                    + "\", not \"" + ipv6HostAddress +
59c01214e478 8129315: java/net/Socket/LingerTest.java and java/net/Socket/ShutdownBoth.java timeout intermittently
dfuchs
parents: 53478
diff changeset
   178
                    "\". Skipping test case.");
59c01214e478 8129315: java/net/Socket/LingerTest.java and java/net/Socket/ShutdownBoth.java timeout intermittently
dfuchs
parents: 53478
diff changeset
   179
            return;
59c01214e478 8129315: java/net/Socket/LingerTest.java and java/net/Socket/ShutdownBoth.java timeout intermittently
dfuchs
parents: 53478
diff changeset
   180
        }
59c01214e478 8129315: java/net/Socket/LingerTest.java and java/net/Socket/ShutdownBoth.java timeout intermittently
dfuchs
parents: 53478
diff changeset
   181
22276
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
   182
        Proxy proxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress(ipv6Hostname,
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
   183
                socks.getPort()));
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
   184
        URL url = new URL("http://" + ipv6Hostname + ":" + server.getAddress().getPort());
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
   185
        java.net.URLConnection conn = url.openConnection(proxy);
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
   186
        String actual = "";
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
   187
        try (BufferedReader reader = new BufferedReader(
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
   188
                new InputStreamReader(conn.getInputStream()))) {
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
   189
            actual = reader.readLine();
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
   190
        }
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
   191
        assertEquals(actual, response);
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
   192
    }
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
   193
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
   194
    @AfterClass
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
   195
    public void tearDown() {
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
   196
        if (server != null) {
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
   197
            server.stop(1);
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
   198
        }
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
   199
        if (socks != null) {
53478
0716631b907f 8217740: SocksIPv6Test compilation error
rriggs
parents: 47216
diff changeset
   200
            socks.close();
22276
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
   201
        }
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
   202
    }
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
diff changeset
   203
}