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

@ -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.
*
* This code is free software; you can redistribute it and/or modify it
@ -24,10 +24,14 @@
/**
* @test
* @bug 4473092
* @library /test/lib
* @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.io.*;
import jdk.test.lib.net.URIBuilder;
public class HttpResponseCode implements Runnable {
ServerSocket ss;
@ -61,14 +65,19 @@ public class HttpResponseCode implements Runnable {
HttpResponseCode() throws Exception {
/* start the server */
ss = new ServerSocket(0);
InetAddress loopback = InetAddress.getLoopbackAddress();
ss = new ServerSocket();
ss.bind(new InetSocketAddress(loopback, 0));
(new Thread(this)).start();
/* establish http connection to server */
String url = "http://localhost:" +
Integer.toString(ss.getLocalPort()) +
"/missing.nothtml";
URLConnection uc = new URL(url).openConnection();
URL url = URIBuilder.newBuilder()
.scheme("http")
.loopback()
.port(ss.getLocalPort())
.path("/missing.nothtml")
.toURL();
URLConnection uc = url.openConnection(Proxy.NO_PROXY);
int respCode1 = ((HttpURLConnection)uc).getResponseCode();
((HttpURLConnection)uc).disconnect();
int respCode2 = ((HttpURLConnection)uc).getResponseCode();

@ -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.
*
* This code is free software; you can redistribute it and/or modify it
@ -24,10 +24,12 @@
/*
* @test
* @bug 8161016
* @library /test/lib
* @summary When proxy is set HttpURLConnection should not use DIRECT connection.
* @run main/othervm HttpURLConWithProxy
*/
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.ProxySelector;
@ -38,10 +40,11 @@ import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import jdk.test.lib.net.URIBuilder;
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
System.setProperty("http.nonProxyHosts", "");
@ -51,11 +54,18 @@ public class HttpURLConWithProxy {
ServerSocket ss;
URL url;
URLConnection con;
InetAddress loopback = InetAddress.getLoopbackAddress();
InetSocketAddress address = new InetSocketAddress(loopback, 0);
// Test1: using Proxy set by System Property:
try {
ss = new ServerSocket(0);
url = new URL("http://localhost:" + ss.getLocalPort());
ss = new ServerSocket();
ss.bind(address);
url = URIBuilder.newBuilder()
.scheme("http")
.loopback()
.port(ss.getLocalPort())
.toURL();
con = url.openConnection();
con.setConnectTimeout(10 * 1000);
con.connect();
@ -69,8 +79,13 @@ public class HttpURLConWithProxy {
MyProxySelector myProxySel = new MyProxySelector();
ProxySelector.setDefault(myProxySel);
try {
ss = new ServerSocket(0);
url = new URL("http://localhost:" + ss.getLocalPort());
ss = new ServerSocket();
ss.bind(address);
url = URIBuilder.newBuilder()
.scheme("http")
.loopback()
.port(ss.getLocalPort())
.toURL();
con = url.openConnection();
con.setConnectTimeout(10 * 1000);
con.connect();

@ -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.
*
* This code is free software; you can redistribute it and/or modify it
@ -23,8 +23,11 @@
/*
* @test
* @key intermittent
* @bug 6558853
* @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
* @build jdk.test.lib.NetworkConfiguration
* jdk.test.lib.Platform

@ -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.
*
* 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 {
ServerSocket server = new ServerSocket(0);
ServerSocket server = new ServerSocket(0, 0, ia);
Socket s = new Socket(ia, server.getLocalPort());
s.close();
server.close();

@ -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.
*
* This code is free software; you can redistribute it and/or modify it
@ -40,6 +40,7 @@ public class NoLoopbackPackets {
return osname.contains("Windows");
}
private static final String MESSAGE = "hello world (" + System.nanoTime() + ")";
public static void main(String[] args) throws Exception {
if (isWindows()) {
System.out.println("The test only run on non-Windows OS. Bye.");
@ -49,6 +50,7 @@ public class NoLoopbackPackets {
MulticastSocket msock = null;
List<SocketAddress> failedGroups = new ArrayList<SocketAddress>();
Sender sender = null;
Thread senderThread = null;
try {
msock = new MulticastSocket();
int port = msock.getLocalPort();
@ -69,7 +71,8 @@ public class NoLoopbackPackets {
}
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
// since we disable loopback mode.
@ -77,20 +80,41 @@ public class NoLoopbackPackets {
msock.setSoTimeout(5000); // 5 seconds
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);
byte[] expected = MESSAGE.getBytes();
assert expected.length <= buf.length;
for (SocketAddress group : groups) {
System.out.println("joining group: " + group);
msock.joinGroup(group, null);
try {
msock.receive(packet);
do {
for (int i = 0; i < buf.length; i++) {
buf[i] = (byte) 'a';
}
msock.receive(packet);
byte[] data = packet.getData();
int len = packet.getLength();
// it is an error if we receive something
failedGroups.add(group);
if (expected(data, len, expected)) {
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) {
// we expect this
System.out.println("Received expected exception from " + group + ": " + e);
} finally {
msock.leaveGroup(group, null);
}
msock.leaveGroup(group, null);
}
} finally {
if (msock != null) try { msock.close(); } catch (Exception e) {}
@ -98,15 +122,28 @@ public class NoLoopbackPackets {
sender.stop();
}
}
if (failedGroups.size() > 0) {
System.out.println("We should not receive anything from following groups, but we did:");
for (SocketAddress group : failedGroups)
System.out.println(group);
throw new RuntimeException("test failed.");
try {
if (failedGroups.size() > 0) {
System.out.println("We should not receive anything from following groups, but we did:");
for (SocketAddress group : failedGroups)
System.out.println(group);
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 {
private List<SocketAddress> sendToGroups;
private volatile boolean stop;
@ -116,16 +153,15 @@ public class NoLoopbackPackets {
}
public void run() {
byte[] buf = "hello world".getBytes();
byte[] buf = MESSAGE.getBytes();
List<DatagramPacket> packets = new ArrayList<DatagramPacket>();
try {
try (MulticastSocket msock = new MulticastSocket()) {
for (SocketAddress group : sendToGroups) {
DatagramPacket packet = new DatagramPacket(buf, buf.length, group);
packets.add(packet);
}
MulticastSocket msock = new MulticastSocket();
msock.setLoopbackMode(true); // disable loopback mode
while (!stop) {
for (DatagramPacket packet : packets) {

@ -29,6 +29,8 @@
*/
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketTimeoutException;
@ -123,7 +125,7 @@ public class AsyncShutdown {
{
Socket s1 = null;
Socket s2 = null;
try (ServerSocket ss = new ServerSocket(0)) {
try (ServerSocket ss = createBoundServer()) {
s1 = new Socket();
s1.connect(ss.getLocalSocketAddress());
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;
}
}

@ -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.
*
* This code is free software; you can redistribute it and/or modify it
@ -25,13 +25,17 @@
* @test
* @bug 6210227
* @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/othervm -Djava.net.preferIPv4Stack=true B6210227
* @run main/othervm -Djava.net.preferIPv6Addresses=true B6210227
*/
import java.util.*;
import java.net.*;
import java.util.stream.IntStream;
import java.util.stream.Collectors;
import jdk.test.lib.net.IPSupport;
public class B6210227 {
@ -42,15 +46,21 @@ public class B6210227 {
ServerSocket ss = new ServerSocket(0);
int port = ss.getLocalPort();
byte[] bad = {0,0,0,0};
try {
InetSocketAddress isa = new InetSocketAddress(InetAddress.getLocalHost(), port);
Socket s = new Socket();
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();
if (Arrays.equals (iaLocal.getAddress(), bad)) {
throw new RuntimeException ("0.0.0.0 returned");
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");
}
}
System.out.println("local hostname is "+sLocalHostname );
} catch(Exception e) {
@ -60,5 +70,11 @@ public class B6210227 {
ss.close();
}
}
}
private static boolean isWildcard(byte[] bytes) {
for (int i = 0; i < bytes.length ; i++) {
if (bytes[i] != 0) return false;
}
return true;
}
}

@ -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.
*
* 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
* 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();
try {
@ -87,7 +88,7 @@ public class LinkLocal {
}
DatagramSocket ds1 = new DatagramSocket();
DatagramSocket ds2 = new DatagramSocket();
DatagramSocket ds2 = new DatagramSocket(0, ia);
try {
byte b[] = "Hello".getBytes();

@ -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.
*
* 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 {
IPSupport.throwSkippedExceptionIfNonOperational();
ServerSocket ss = new ServerSocket(0);
ServerSocket ss = new ServerSocket(0, 0, InetAddress.getLocalHost());
Socket client = new Socket(InetAddress.getLocalHost(),
ss.getLocalPort());
Socket server = ss.accept();

@ -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.
*
* 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 {
try (ServerSocket ssock = new ServerSocket(0);
try (ServerSocket ssock = createBoundServer(sin);
Socket sock = new Socket()) {
int port = ssock.getLocalPort();
sock.connect(new InetSocketAddress(sin, port), 100);
@ -82,7 +82,7 @@ public class B6521014 {
}
static void test2(Inet6Address sin) throws Exception {
try (ServerSocket ssock = new ServerSocket(0);
try (ServerSocket ssock = createBoundServer(sin);
Socket sock = new Socket()) {
int port = ssock.getLocalPort();
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 {
Optional<Inet6Address> oaddr = getLocalAddr();
if (!oaddr.isPresent()) {