Path: blob/master/test/jdk/java/net/Socks/SocksIPv6Test.java
41149 views
/*1* Copyright (c) 2013, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 710095725* @modules jdk.httpserver26* @library /test/lib27* @summary Java doesn't correctly handle the SOCKS protocol when used over IPv6.28* @run testng SocksIPv6Test29*/3031import java.io.BufferedReader;32import java.io.IOException;33import java.io.InputStreamReader;34import java.io.OutputStreamWriter;35import java.net.Authenticator;36import java.net.InetSocketAddress;37import java.net.URL;38import java.net.Proxy;39import java.lang.Override;40import java.net.InetAddress;41import java.net.Inet6Address;42import java.net.ServerSocket;43import java.net.SocketException;44import java.net.NetworkInterface;45import java.util.Collections;46import java.util.List;47import com.sun.net.httpserver.*;48import java.io.BufferedWriter;49import org.testng.SkipException;50import org.testng.annotations.AfterClass;51import org.testng.annotations.BeforeClass;52import org.testng.annotations.Test;5354import jdk.test.lib.NetworkConfiguration;5556import static org.testng.Assert.*;5758public class SocksIPv6Test {5960private HttpServer server;61private SocksServer socks;62private String response = "Hello.";6364@BeforeClass65public void setUp() throws Exception {66if (!ensureInet6AddressFamily() || !ensureIPv6OnLoopback()) {67NetworkConfiguration.printSystemConfiguration(System.out);68throw new SkipException("Host does not support IPv6");69}7071server = HttpServer.create(new InetSocketAddress("::1", 0), 0);72server.createContext("/", ex -> {73ex.sendResponseHeaders(200, response.length());74try (BufferedWriter writer = new BufferedWriter(75new OutputStreamWriter(ex.getResponseBody(), "UTF-8"))) {76writer.write(response);77}78ex.close();79});80server.start();8182socks = new SocksServer(InetAddress.getByName("::1"), 0, false);83socks.addUser("user", "pass");84socks.start();8586Authenticator.setDefault(new Authenticator() {87@Override88protected java.net.PasswordAuthentication getPasswordAuthentication() {89return new java.net.PasswordAuthentication(90"user", "pass".toCharArray());91}92});93}9495private boolean ensureIPv6OnLoopback() throws Exception {96boolean ipv6 = false;9798List<NetworkInterface> nics = Collections.list(NetworkInterface.getNetworkInterfaces());99for (NetworkInterface nic : nics) {100if (!nic.isLoopback()) {101continue;102}103List<InetAddress> addrs = Collections.list(nic.getInetAddresses());104for (InetAddress addr : addrs) {105if (addr instanceof Inet6Address) {106ipv6 = true;107break;108}109}110}111if (!ipv6)112System.out.println("IPv6 is not enabled on loopback. Skipping test suite.");113return ipv6;114}115116private boolean ensureInet6AddressFamily() throws IOException {117try (ServerSocket s = new ServerSocket()) {118s.bind(new InetSocketAddress("::1", 0));119return true;120} catch (SocketException e) {121System.out.println("Inet 6 address family is not available. Skipping test suite.");122}123return false;124}125126@Test(groups = "unit")127public void testSocksOverIPv6() throws Exception {128Proxy proxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress("::1",129socks.getPort()));130URL url = new URL("http://[::1]:" + server.getAddress().getPort());131java.net.URLConnection conn = url.openConnection(proxy);132String actual = "";133try (BufferedReader reader = new BufferedReader(134new InputStreamReader(conn.getInputStream()))) {135actual = reader.readLine();136}137assertEquals(actual, response);138}139140@Test(groups = "unit")141public void testSocksOverIPv6Hostname() throws Exception {142InetAddress ipv6Loopback = InetAddress.getByName("::1");143String ipv6Hostname = ipv6Loopback.getHostName();144String ipv6HostAddress = ipv6Loopback.getHostAddress();145InetAddress ipv4Loopback;146String ipv4Hostname;147String ipv4HostAddress;148try {149ipv4Loopback = InetAddress.getByName("127.0.0.1");150ipv4Hostname = ipv4Loopback == null ? null : ipv4Loopback.getHostName();151ipv4HostAddress = ipv4Loopback == null ? null : ipv4Loopback.getHostAddress();152} catch (IOException io) {153ipv4Hostname = null;154ipv4HostAddress = null;155}156157System.out.println("ipv6Hostname: " + ipv6Hostname + " / " + ipv6HostAddress);158System.out.println("ipv4Hostname: " + ipv4Hostname + " / " + ipv4HostAddress);159160if (ipv6Hostname.equals(ipv6HostAddress)) {161System.out.println("Unable to get the hostname of the IPv6 loopback "162+ "address. Skipping test case.");163return;164}165166if (ipv4Hostname != null && ipv6Hostname.equals(ipv4Hostname)) {167System.out.println("IPv6 and IPv4 loopback addresses map to the"168+ " same hostname. Skipping test case.");169return;170}171172if (!InetAddress.getByName(ipv6Hostname).getHostAddress()173.equals(ipv6HostAddress)) {174System.out.println(ipv6Hostname + " resolves to \""175+ InetAddress.getByName(ipv6Hostname).getHostAddress()176+ "\", not \"" + ipv6HostAddress +177"\". Skipping test case.");178return;179}180181Proxy proxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress(ipv6Hostname,182socks.getPort()));183URL url = new URL("http://" + ipv6Hostname + ":" + server.getAddress().getPort());184java.net.URLConnection conn = url.openConnection(proxy);185String actual = "";186try (BufferedReader reader = new BufferedReader(187new InputStreamReader(conn.getInputStream()))) {188actual = reader.readLine();189}190assertEquals(actual, response);191}192193@AfterClass194public void tearDown() {195if (server != null) {196server.stop(1);197}198if (socks != null) {199socks.close();200}201}202}203204205