8227539: Replace wildcard address with loopback or local host in tests - part 20

Update some tests to stop using the wildcard address.

Reviewed-by: michaelm
This commit is contained in:
Daniel Fuchs 2019-07-10 22:33:23 +02:00
parent 8286318f2a
commit dc300483a7
10 changed files with 144 additions and 47 deletions

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2001, 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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -24,10 +24,14 @@
/** /**
* @test * @test
* @bug 4473092 * @bug 4473092
* @library /test/lib
* @summary Method throws IOException when object should be returned * @summary Method throws IOException when object should be returned
* @run main HttpResponseCode
* @run main/othervm -Djava.net.preferIPv6Addresses=true HttpResponseCode
*/ */
import java.net.*; import java.net.*;
import java.io.*; import java.io.*;
import jdk.test.lib.net.URIBuilder;
public class HttpResponseCode implements Runnable { public class HttpResponseCode implements Runnable {
ServerSocket ss; ServerSocket ss;
@ -61,14 +65,19 @@ public class HttpResponseCode implements Runnable {
HttpResponseCode() throws Exception { HttpResponseCode() throws Exception {
/* start the server */ /* start the server */
ss = new ServerSocket(0); InetAddress loopback = InetAddress.getLoopbackAddress();
ss = new ServerSocket();
ss.bind(new InetSocketAddress(loopback, 0));
(new Thread(this)).start(); (new Thread(this)).start();
/* establish http connection to server */ /* establish http connection to server */
String url = "http://localhost:" + URL url = URIBuilder.newBuilder()
Integer.toString(ss.getLocalPort()) + .scheme("http")
"/missing.nothtml"; .loopback()
URLConnection uc = new URL(url).openConnection(); .port(ss.getLocalPort())
.path("/missing.nothtml")
.toURL();
URLConnection uc = url.openConnection(Proxy.NO_PROXY);
int respCode1 = ((HttpURLConnection)uc).getResponseCode(); int respCode1 = ((HttpURLConnection)uc).getResponseCode();
((HttpURLConnection)uc).disconnect(); ((HttpURLConnection)uc).disconnect();
int respCode2 = ((HttpURLConnection)uc).getResponseCode(); int respCode2 = ((HttpURLConnection)uc).getResponseCode();

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -24,10 +24,12 @@
/* /*
* @test * @test
* @bug 8161016 * @bug 8161016
* @library /test/lib
* @summary When proxy is set HttpURLConnection should not use DIRECT connection. * @summary When proxy is set HttpURLConnection should not use DIRECT connection.
* @run main/othervm HttpURLConWithProxy * @run main/othervm HttpURLConWithProxy
*/ */
import java.io.IOException; import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.net.Proxy; import java.net.Proxy;
import java.net.ProxySelector; import java.net.ProxySelector;
@ -38,10 +40,11 @@ import java.net.URL;
import java.net.URLConnection; import java.net.URLConnection;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import jdk.test.lib.net.URIBuilder;
public class HttpURLConWithProxy { public class HttpURLConWithProxy {
public static void main(String... arg) { public static void main(String... arg) throws Exception {
// Remove the default nonProxyHosts to use localhost for testing // Remove the default nonProxyHosts to use localhost for testing
System.setProperty("http.nonProxyHosts", ""); System.setProperty("http.nonProxyHosts", "");
@ -51,11 +54,18 @@ public class HttpURLConWithProxy {
ServerSocket ss; ServerSocket ss;
URL url; URL url;
URLConnection con; URLConnection con;
InetAddress loopback = InetAddress.getLoopbackAddress();
InetSocketAddress address = new InetSocketAddress(loopback, 0);
// Test1: using Proxy set by System Property: // Test1: using Proxy set by System Property:
try { try {
ss = new ServerSocket(0); ss = new ServerSocket();
url = new URL("http://localhost:" + ss.getLocalPort()); ss.bind(address);
url = URIBuilder.newBuilder()
.scheme("http")
.loopback()
.port(ss.getLocalPort())
.toURL();
con = url.openConnection(); con = url.openConnection();
con.setConnectTimeout(10 * 1000); con.setConnectTimeout(10 * 1000);
con.connect(); con.connect();
@ -69,8 +79,13 @@ public class HttpURLConWithProxy {
MyProxySelector myProxySel = new MyProxySelector(); MyProxySelector myProxySel = new MyProxySelector();
ProxySelector.setDefault(myProxySel); ProxySelector.setDefault(myProxySel);
try { try {
ss = new ServerSocket(0); ss = new ServerSocket();
url = new URL("http://localhost:" + ss.getLocalPort()); ss.bind(address);
url = URIBuilder.newBuilder()
.scheme("http")
.loopback()
.port(ss.getLocalPort())
.toURL();
con = url.openConnection(); con = url.openConnection();
con.setConnectTimeout(10 * 1000); con.setConnectTimeout(10 * 1000);
con.connect(); con.connect();

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2008, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -23,8 +23,11 @@
/* /*
* @test * @test
* @key intermittent
* @bug 6558853 * @bug 6558853
* @summary getHostAddress() on connections using IPv6 link-local addrs should have zone id * @summary getHostAddress() on connections using IPv6 link-local addrs should have zone id
* This test needs to bind to the wildcard address and as such is succeptible to
* fail intermittently because of port reuse issues.
* @library /test/lib * @library /test/lib
* @build jdk.test.lib.NetworkConfiguration * @build jdk.test.lib.NetworkConfiguration
* jdk.test.lib.Platform * jdk.test.lib.Platform

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2003, 2010, 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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -74,7 +74,7 @@ public class CheckJNI {
} }
static void testSocket(InetAddress ia) throws Exception { static void testSocket(InetAddress ia) throws Exception {
ServerSocket server = new ServerSocket(0); ServerSocket server = new ServerSocket(0, 0, ia);
Socket s = new Socket(ia, server.getLocalPort()); Socket s = new Socket(ia, server.getLocalPort());
s.close(); s.close();
server.close(); server.close();

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2007, 2016, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2007, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -40,6 +40,7 @@ public class NoLoopbackPackets {
return osname.contains("Windows"); return osname.contains("Windows");
} }
private static final String MESSAGE = "hello world (" + System.nanoTime() + ")";
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
if (isWindows()) { if (isWindows()) {
System.out.println("The test only run on non-Windows OS. Bye."); System.out.println("The test only run on non-Windows OS. Bye.");
@ -49,6 +50,7 @@ public class NoLoopbackPackets {
MulticastSocket msock = null; MulticastSocket msock = null;
List<SocketAddress> failedGroups = new ArrayList<SocketAddress>(); List<SocketAddress> failedGroups = new ArrayList<SocketAddress>();
Sender sender = null; Sender sender = null;
Thread senderThread = null;
try { try {
msock = new MulticastSocket(); msock = new MulticastSocket();
int port = msock.getLocalPort(); int port = msock.getLocalPort();
@ -69,7 +71,8 @@ public class NoLoopbackPackets {
} }
sender = new Sender(groups); sender = new Sender(groups);
new Thread(sender).start(); senderThread = new Thread(sender);
senderThread.start();
// Now try to receive multicast packets. we should not see any of them // Now try to receive multicast packets. we should not see any of them
// since we disable loopback mode. // since we disable loopback mode.
@ -77,34 +80,68 @@ public class NoLoopbackPackets {
msock.setSoTimeout(5000); // 5 seconds msock.setSoTimeout(5000); // 5 seconds
byte[] buf = new byte[1024]; byte[] buf = new byte[1024];
for (int i = 0; i < buf.length; i++) {
buf[i] = (byte) 'z';
}
DatagramPacket packet = new DatagramPacket(buf, 0, buf.length); DatagramPacket packet = new DatagramPacket(buf, 0, buf.length);
byte[] expected = MESSAGE.getBytes();
assert expected.length <= buf.length;
for (SocketAddress group : groups) { for (SocketAddress group : groups) {
System.out.println("joining group: " + group);
msock.joinGroup(group, null); msock.joinGroup(group, null);
try { try {
do {
for (int i = 0; i < buf.length; i++) {
buf[i] = (byte) 'a';
}
msock.receive(packet); msock.receive(packet);
byte[] data = packet.getData();
int len = packet.getLength();
// it is an error if we receive something if (expected(data, len, expected)) {
failedGroups.add(group); failedGroups.add(group);
break;
} else {
System.err.println("WARNING: Unexpected packet received from "
+ group + ": "
+ len + " bytes");
System.err.println("\t as text: " + new String(data, 0, len));
}
} while (true);
} catch (SocketTimeoutException e) { } catch (SocketTimeoutException e) {
// we expect this // we expect this
} System.out.println("Received expected exception from " + group + ": " + e);
} finally {
msock.leaveGroup(group, null); msock.leaveGroup(group, null);
} }
}
} finally { } finally {
if (msock != null) try { msock.close(); } catch (Exception e) {} if (msock != null) try { msock.close(); } catch (Exception e) {}
if (sender != null) { if (sender != null) {
sender.stop(); sender.stop();
} }
} }
try {
if (failedGroups.size() > 0) { if (failedGroups.size() > 0) {
System.out.println("We should not receive anything from following groups, but we did:"); System.out.println("We should not receive anything from following groups, but we did:");
for (SocketAddress group : failedGroups) for (SocketAddress group : failedGroups)
System.out.println(group); System.out.println(group);
throw new RuntimeException("test failed."); throw new RuntimeException("test failed.");
} }
} finally {
if (senderThread != null) {
senderThread.join();
}
}
}
static boolean expected(byte[] data, int len, byte[] expected) {
if (len != expected.length) return false;
for (int i = 0; i < len; i++) {
if (data[i] != expected[i]) return false;
}
return true;
} }
static class Sender implements Runnable { static class Sender implements Runnable {
@ -116,16 +153,15 @@ public class NoLoopbackPackets {
} }
public void run() { public void run() {
byte[] buf = "hello world".getBytes(); byte[] buf = MESSAGE.getBytes();
List<DatagramPacket> packets = new ArrayList<DatagramPacket>(); List<DatagramPacket> packets = new ArrayList<DatagramPacket>();
try { try (MulticastSocket msock = new MulticastSocket()) {
for (SocketAddress group : sendToGroups) { for (SocketAddress group : sendToGroups) {
DatagramPacket packet = new DatagramPacket(buf, buf.length, group); DatagramPacket packet = new DatagramPacket(buf, buf.length, group);
packets.add(packet); packets.add(packet);
} }
MulticastSocket msock = new MulticastSocket();
msock.setLoopbackMode(true); // disable loopback mode msock.setLoopbackMode(true); // disable loopback mode
while (!stop) { while (!stop) {
for (DatagramPacket packet : packets) { for (DatagramPacket packet : packets) {

View file

@ -29,6 +29,8 @@
*/ */
import java.io.IOException; import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket; import java.net.ServerSocket;
import java.net.Socket; import java.net.Socket;
import java.net.SocketTimeoutException; import java.net.SocketTimeoutException;
@ -123,7 +125,7 @@ public class AsyncShutdown {
{ {
Socket s1 = null; Socket s1 = null;
Socket s2 = null; Socket s2 = null;
try (ServerSocket ss = new ServerSocket(0)) { try (ServerSocket ss = createBoundServer()) {
s1 = new Socket(); s1 = new Socket();
s1.connect(ss.getLocalSocketAddress()); s1.connect(ss.getLocalSocketAddress());
s2 = ss.accept(); s2 = ss.accept();
@ -134,4 +136,12 @@ public class AsyncShutdown {
} }
} }
static ServerSocket createBoundServer() throws IOException {
ServerSocket ss = new ServerSocket();
InetAddress loopback = InetAddress.getLoopbackAddress();
InetSocketAddress address = new InetSocketAddress(loopback, 0);
ss.bind(address);
return ss;
}
} }

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2012, 2018, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -25,13 +25,17 @@
* @test * @test
* @bug 6210227 * @bug 6210227
* @library /test/lib * @library /test/lib
* @summary REGRESSION: Socket.getLocalAddress() returns address of 0.0.0.0 on outbound TCP * @summary REGRESSION: Socket.getLocalAddress() returns address of 0.0.0.0 on outbound TCP.
* This test requires binding to the wildcard address.
* @run main B6210227 * @run main B6210227
* @run main/othervm -Djava.net.preferIPv4Stack=true B6210227 * @run main/othervm -Djava.net.preferIPv4Stack=true B6210227
* @run main/othervm -Djava.net.preferIPv6Addresses=true B6210227
*/ */
import java.util.*; import java.util.*;
import java.net.*; import java.net.*;
import java.util.stream.IntStream;
import java.util.stream.Collectors;
import jdk.test.lib.net.IPSupport; import jdk.test.lib.net.IPSupport;
public class B6210227 { public class B6210227 {
@ -42,16 +46,22 @@ public class B6210227 {
ServerSocket ss = new ServerSocket(0); ServerSocket ss = new ServerSocket(0);
int port = ss.getLocalPort(); int port = ss.getLocalPort();
byte[] bad = {0,0,0,0};
try { try {
InetSocketAddress isa = new InetSocketAddress(InetAddress.getLocalHost(), port); InetSocketAddress isa = new InetSocketAddress(InetAddress.getLocalHost(), port);
Socket s = new Socket(); Socket s = new Socket();
s.connect( isa, 1000 ); s.connect( isa, 1000 );
InetAddress iaLocal = s.getLocalAddress(); // if this comes back as 0.0.0.0 this would demonstrate issue InetAddress iaLocal = s.getLocalAddress(); // if this comes back as 0.0.0.0 this would demonstrate issue
String sLocalHostname = iaLocal.getHostName(); String sLocalHostname = iaLocal.getHostName();
if (Arrays.equals (iaLocal.getAddress(), bad)) { byte[] address = iaLocal.getAddress();
if (isWildcard(address)) {
if (iaLocal instanceof Inet6Address) {
String msg = IntStream.range(0, address.length)
.mapToObj(i -> "0").collect(Collectors.joining(":"));
throw new RuntimeException(msg + " returned");
} else {
throw new RuntimeException ("0.0.0.0 returned"); throw new RuntimeException ("0.0.0.0 returned");
} }
}
System.out.println("local hostname is "+sLocalHostname ); System.out.println("local hostname is "+sLocalHostname );
} catch(Exception e) { } catch(Exception e) {
System.out.println("Exception happened"); System.out.println("Exception happened");
@ -60,5 +70,11 @@ public class B6210227 {
ss.close(); ss.close();
} }
} }
}
private static boolean isWildcard(byte[] bytes) {
for (int i = 0; i < bytes.length ; i++) {
if (bytes[i] != 0) return false;
}
return true;
}
}

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2001, 2018, 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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -55,7 +55,8 @@ public class LinkLocal {
* Create ServerSocket on wildcard address and then * Create ServerSocket on wildcard address and then
* try to connect Socket to link-local address. * try to connect Socket to link-local address.
*/ */
ServerSocket ss = new ServerSocket(0); ServerSocket ss = new ServerSocket();
ss.bind(new InetSocketAddress(ia, 0));
Socket s = new Socket(); Socket s = new Socket();
try { try {
@ -87,7 +88,7 @@ public class LinkLocal {
} }
DatagramSocket ds1 = new DatagramSocket(); DatagramSocket ds1 = new DatagramSocket();
DatagramSocket ds2 = new DatagramSocket(); DatagramSocket ds2 = new DatagramSocket(0, ia);
try { try {
byte b[] = "Hello".getBytes(); byte b[] = "Hello".getBytes();

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2001, 2018, 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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -57,7 +57,7 @@ public class BrokenPipe {
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
IPSupport.throwSkippedExceptionIfNonOperational(); IPSupport.throwSkippedExceptionIfNonOperational();
ServerSocket ss = new ServerSocket(0); ServerSocket ss = new ServerSocket(0, 0, InetAddress.getLocalHost());
Socket client = new Socket(InetAddress.getLocalHost(), Socket client = new Socket(InetAddress.getLocalHost(),
ss.getLocalPort()); ss.getLocalPort());
Socket server = ss.accept(); Socket server = ss.accept();

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2007, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -71,7 +71,7 @@ public class B6521014 {
} }
static void test1(Inet6Address sin) throws Exception { static void test1(Inet6Address sin) throws Exception {
try (ServerSocket ssock = new ServerSocket(0); try (ServerSocket ssock = createBoundServer(sin);
Socket sock = new Socket()) { Socket sock = new Socket()) {
int port = ssock.getLocalPort(); int port = ssock.getLocalPort();
sock.connect(new InetSocketAddress(sin, port), 100); sock.connect(new InetSocketAddress(sin, port), 100);
@ -82,7 +82,7 @@ public class B6521014 {
} }
static void test2(Inet6Address sin) throws Exception { static void test2(Inet6Address sin) throws Exception {
try (ServerSocket ssock = new ServerSocket(0); try (ServerSocket ssock = createBoundServer(sin);
Socket sock = new Socket()) { Socket sock = new Socket()) {
int port = ssock.getLocalPort(); int port = ssock.getLocalPort();
ssock.setSoTimeout(100); ssock.setSoTimeout(100);
@ -94,6 +94,13 @@ public class B6521014 {
} }
} }
static ServerSocket createBoundServer(Inet6Address sin) throws IOException {
ServerSocket ss = new ServerSocket();
InetSocketAddress address = new InetSocketAddress(sin, 0);
ss.bind(address);
return ss;
}
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
Optional<Inet6Address> oaddr = getLocalAddr(); Optional<Inet6Address> oaddr = getLocalAddr();
if (!oaddr.isPresent()) { if (!oaddr.isPresent()) {