Path: blob/jdk8u272-b10-aarch32-20201026/jdk/test/java/net/Socks/SocksIPv6Test.java
48795 views
/*1* Copyright (c) 2013, 2014, 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* @summary Java doesn't correctly handle the SOCKS protocol when used over IPv6.26* @run testng SocksIPv6Test27*/2829import java.io.BufferedReader;30import java.io.IOException;31import java.io.InputStream;32import java.io.InputStreamReader;33import java.io.OutputStreamWriter;34import java.net.Authenticator;35import java.net.InetSocketAddress;36import java.net.URL;37import java.net.Proxy;38import java.lang.Override;39import java.net.InetAddress;40import java.net.Inet6Address;41import java.net.ServerSocket;42import java.net.SocketException;43import java.net.NetworkInterface;44import java.net.UnknownHostException;45import java.util.Collections;46import java.util.List;47import com.sun.net.httpserver.*;48import java.io.BufferedWriter;49import org.testng.annotations.AfterClass;50import org.testng.annotations.BeforeClass;51import org.testng.annotations.Test;5253import static org.testng.Assert.*;5455public class SocksIPv6Test {5657private HttpServer server;58private SocksServer socks;59private String response = "Hello.";60private static boolean shouldRun = false;6162@BeforeClass63public void setUp() throws Exception {64shouldRun = ensureInet6AddressFamily() && ensureIPv6OnLoopback();6566server = HttpServer.create(new InetSocketAddress(0), 0);67server.createContext("/", ex -> {68ex.sendResponseHeaders(200, response.length());69try (BufferedWriter writer = new BufferedWriter(70new OutputStreamWriter(ex.getResponseBody(), "UTF-8"))) {71writer.write(response);72}73ex.close();74});75server.start();7677socks = new SocksServer(0, false);78socks.addUser("user", "pass");79socks.start();8081Authenticator.setDefault(new Authenticator() {82@Override83protected java.net.PasswordAuthentication getPasswordAuthentication() {84return new java.net.PasswordAuthentication(85"user", "pass".toCharArray());86}87});88}8990private boolean ensureIPv6OnLoopback() throws Exception {91boolean ipv6 = false;9293List<NetworkInterface> nics = Collections.list(NetworkInterface.getNetworkInterfaces());94for (NetworkInterface nic : nics) {95if (!nic.isLoopback()) {96continue;97}98List<InetAddress> addrs = Collections.list(nic.getInetAddresses());99for (InetAddress addr : addrs) {100if (addr instanceof Inet6Address) {101ipv6 = true;102break;103}104}105}106if (!ipv6)107System.out.println("IPv6 is not enabled on loopback. Skipping test suite.");108return ipv6;109}110111private boolean ensureInet6AddressFamily() throws IOException {112try (ServerSocket s = new ServerSocket()) {113s.bind(new InetSocketAddress("::1", 0));114return true;115} catch (SocketException e) {116System.out.println("Inet 6 address family is not available. Skipping test suite.");117}118return false;119}120121@Test(groups = "unit")122public void testSocksOverIPv6() throws Exception {123if (!shouldRun) return;124125Proxy proxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress("::1",126socks.getPort()));127URL url = new URL("http://[::1]:" + server.getAddress().getPort());128java.net.URLConnection conn = url.openConnection(proxy);129String actual = "";130try (BufferedReader reader = new BufferedReader(131new InputStreamReader(conn.getInputStream()))) {132actual = reader.readLine();133}134assertEquals(actual, response);135}136137@Test(groups = "unit")138public void testSocksOverIPv6Hostname() throws Exception {139if (!shouldRun) return;140141String ipv6Hostname = InetAddress.getByName("::1").getHostName();142String ipv4Hostname = InetAddress.getByName("127.0.0.1").getHostName();143144if (ipv6Hostname.equals(InetAddress.getByName("::1").getHostAddress())) {145System.out.println("Unable to get the hostname of the IPv6 loopback "146+ "address. Skipping test case.");147return;148}149150if (ipv6Hostname.equals(ipv4Hostname)) {151System.out.println("IPv6 and IPv4 loopback addresses map to the"152+ " same hostname. Skipping test case.");153return;154}155156Proxy proxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress(ipv6Hostname,157socks.getPort()));158URL url = new URL("http://" + ipv6Hostname + ":" + server.getAddress().getPort());159java.net.URLConnection conn = url.openConnection(proxy);160String actual = "";161try (BufferedReader reader = new BufferedReader(162new InputStreamReader(conn.getInputStream()))) {163actual = reader.readLine();164}165assertEquals(actual, response);166}167168@AfterClass169public void tearDown() {170if (server != null) {171server.stop(1);172}173if (socks != null) {174socks.terminate();175}176}177}178179180