diff --git a/test/jdk/java/net/Socket/LingerTest.java b/test/jdk/java/net/Socket/LingerTest.java index 83e0be833d9..9a084c69df9 100644 --- a/test/jdk/java/net/Socket/LingerTest.java +++ b/test/jdk/java/net/Socket/LingerTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -71,11 +71,13 @@ public void run() { } static class Other implements Runnable { - int port; - long delay; + final InetAddress address; + final int port; + final long delay; boolean connected = false; - public Other(int port, long delay) { + public Other(InetAddress address, int port, long delay) { + this.address = address; this.port = port; this.delay = delay; } @@ -85,7 +87,7 @@ public void run() { try { Thread.sleep(delay); System.out.println ("Other opening socket"); - Socket s = new Socket("localhost", port); + Socket s = new Socket(address, port); synchronized (this) { connected = true; } @@ -103,9 +105,10 @@ public synchronized boolean connected() { } public static void main(String args[]) throws Exception { - ServerSocket ss = new ServerSocket(0); + InetAddress loopback = InetAddress.getLoopbackAddress(); + ServerSocket ss = new ServerSocket(0, 50, loopback); - Socket s1 = new Socket("localhost", ss.getLocalPort()); + Socket s1 = new Socket(loopback, ss.getLocalPort()); Socket s2 = ss.accept(); // setup conditions for untransmitted data and lengthy @@ -119,7 +122,7 @@ public static void main(String args[]) throws Exception { senderThread.start(); // other thread that will connect after 5 seconds. - Other other = new Other(ss.getLocalPort(), 5000); + Other other = new Other(loopback, ss.getLocalPort(), 5000); Thread otherThread = new Thread(other); otherThread.start(); diff --git a/test/jdk/java/net/Socket/ShutdownBoth.java b/test/jdk/java/net/Socket/ShutdownBoth.java index 421d52b7361..2adc67cb13c 100644 --- a/test/jdk/java/net/Socket/ShutdownBoth.java +++ b/test/jdk/java/net/Socket/ShutdownBoth.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2001, 2019, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -32,7 +32,8 @@ public class ShutdownBoth { public static void main(String args[]) throws Exception { - ServerSocket ss = new ServerSocket(0); + InetAddress loopback = InetAddress.getLoopbackAddress(); + ServerSocket ss = new ServerSocket(0, 50, loopback); Socket s1 = new Socket(ss.getInetAddress(), ss.getLocalPort()); Socket s2 = ss.accept(); diff --git a/test/jdk/java/net/Socks/SocksIPv6Test.java b/test/jdk/java/net/Socks/SocksIPv6Test.java index a09db166d73..a104209df6d 100644 --- a/test/jdk/java/net/Socks/SocksIPv6Test.java +++ b/test/jdk/java/net/Socks/SocksIPv6Test.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2013, 2016, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2013, 2019, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -30,7 +30,6 @@ import java.io.BufferedReader; import java.io.IOException; -import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.Authenticator; @@ -43,7 +42,6 @@ import java.net.ServerSocket; import java.net.SocketException; import java.net.NetworkInterface; -import java.net.UnknownHostException; import java.util.Collections; import java.util.List; import com.sun.net.httpserver.*; @@ -65,7 +63,7 @@ public class SocksIPv6Test { public void setUp() throws Exception { shouldRun = ensureInet6AddressFamily() && ensureIPv6OnLoopback(); - server = HttpServer.create(new InetSocketAddress(0), 0); + server = HttpServer.create(new InetSocketAddress("::1", 0), 0); server.createContext("/", ex -> { ex.sendResponseHeaders(200, response.length()); try (BufferedWriter writer = new BufferedWriter( @@ -76,7 +74,7 @@ public void setUp() throws Exception { }); server.start(); - socks = new SocksServer(0, false); + socks = new SocksServer(InetAddress.getByName("::1"), 0, false); socks.addUser("user", "pass"); socks.start(); @@ -140,21 +138,45 @@ public void testSocksOverIPv6() throws Exception { public void testSocksOverIPv6Hostname() throws Exception { if (!shouldRun) return; - String ipv6Hostname = InetAddress.getByName("::1").getHostName(); - String ipv4Hostname = InetAddress.getByName("127.0.0.1").getHostName(); + InetAddress ipv6Loopback = InetAddress.getByName("::1"); + String ipv6Hostname = ipv6Loopback.getHostName(); + String ipv6HostAddress = ipv6Loopback.getHostAddress(); + InetAddress ipv4Loopback; + String ipv4Hostname; + String ipv4HostAddress; + try { + ipv4Loopback = InetAddress.getByName("127.0.0.1"); + ipv4Hostname = ipv4Loopback == null ? null : ipv4Loopback.getHostName(); + ipv4HostAddress = ipv4Loopback == null ? null : ipv4Loopback.getHostAddress(); + } catch (IOException io) { + ipv4Hostname = null; + ipv4HostAddress = null; + } + + System.out.println("ipv6Hostname: " + ipv6Hostname + " / " + ipv6HostAddress); + System.out.println("ipv4Hostname: " + ipv4Hostname + " / " + ipv4HostAddress); - if (ipv6Hostname.equals(InetAddress.getByName("::1").getHostAddress())) { + if (ipv6Hostname.equals(ipv6HostAddress)) { System.out.println("Unable to get the hostname of the IPv6 loopback " + "address. Skipping test case."); return; } - if (ipv6Hostname.equals(ipv4Hostname)) { + if (ipv4Hostname != null && ipv6Hostname.equals(ipv4Hostname)) { System.out.println("IPv6 and IPv4 loopback addresses map to the" + " same hostname. Skipping test case."); return; } + if (!InetAddress.getByName(ipv6Hostname).getHostAddress() + .equals(ipv6HostAddress)) { + System.out.println(ipv6Hostname + " resolves to \"" + + InetAddress.getByName(ipv6Hostname).getHostAddress() + + "\", not \"" + ipv6HostAddress + + "\". Skipping test case."); + return; + } + Proxy proxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress(ipv6Hostname, socks.getPort())); URL url = new URL("http://" + ipv6Hostname + ":" + server.getAddress().getPort()); diff --git a/test/jdk/java/net/Socks/SocksServer.java b/test/jdk/java/net/Socks/SocksServer.java index f2d6ef82bfe..6d9ded42ab5 100644 --- a/test/jdk/java/net/Socks/SocksServer.java +++ b/test/jdk/java/net/Socks/SocksServer.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2002, 2012, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2002, 2019, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -491,6 +491,25 @@ public SocksServer(int port) throws IOException { } } + public SocksServer(InetAddress addr, int port, boolean useV4) throws IOException { + this.port = port; + this.useV4 = useV4; + server = new ServerSocket(); + if (port == 0 && addr == null) { + server.bind(null); + this.port = server.getLocalPort(); + } else if (port == 0 && addr != null) { + server.bind(new InetSocketAddress(addr, 0)); + this.port = server.getLocalPort(); + } else if (addr == null) { + assert port != 0; + server.bind(new InetSocketAddress(port)); + } else { + assert port != 0; + server.bind(new InetSocketAddress(addr, port)); + } + } + public SocksServer() throws IOException { this (DEFAULT_PORT); } diff --git a/test/jdk/sun/net/www/http/HttpURLConnection/PostOnDelete.java b/test/jdk/sun/net/www/http/HttpURLConnection/PostOnDelete.java index 104be1d6d94..e785c426156 100644 --- a/test/jdk/sun/net/www/http/HttpURLConnection/PostOnDelete.java +++ b/test/jdk/sun/net/www/http/HttpURLConnection/PostOnDelete.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2013, 2016, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2013, 2019, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -49,7 +49,7 @@ public void runTest() throws Exception { try { s = new Server(); s.startServer(); - URL url = new URL("http://localhost:" + s.getPort()); + URL url = new URL("http://" + s.getAuthority()); HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection(); urlConnection.setRequestMethod("DELETE"); urlConnection.setDoOutput(true); @@ -70,7 +70,8 @@ class Server { HttpServer server; public void startServer() { - InetSocketAddress addr = new InetSocketAddress(0); + InetAddress loopback = InetAddress.getLoopbackAddress(); + InetSocketAddress addr = new InetSocketAddress(loopback,0); try { server = HttpServer.create(addr, 0); } catch (IOException ioe) { @@ -81,6 +82,12 @@ public void startServer() { server.start(); } + public String getAuthority() { + String address = server.getAddress().getHostString(); + address = (address.indexOf(':') >= 0) ? ("[" + address + "]") : address; + return address + ":" + getPort(); + } + public int getPort() { return server.getAddress().getPort(); }