8223638: Replace wildcard address with loopback or local host in tests - part 6

Reviewed-by: dfuchs
This commit is contained in:
Aleksei Efimov 2019-05-14 13:34:49 +01:00
parent 32ce957de5
commit 64b8734964
9 changed files with 91 additions and 39 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2006, 2016, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2006, 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
@ -41,6 +41,7 @@
*/ */
import java.io.IOException; import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket; import java.net.ServerSocket;
import java.net.Socket; import java.net.Socket;
import java.util.List; import java.util.List;
@ -80,7 +81,7 @@ public class AcceptCauseFileDescriptorLeak {
} }
} }
final ServerSocket ss = new ServerSocket(0) { final ServerSocket ss = new ServerSocket(0, 0, InetAddress.getLoopbackAddress()) {
public Socket accept() throws IOException { public Socket accept() throws IOException {
Socket s = new Socket() { Socket s = new Socket() {
}; };
@ -93,7 +94,7 @@ public class AcceptCauseFileDescriptorLeak {
public void run() { public void run() {
try { try {
for (int i = 0; i < REPS; i++) { for (int i = 0; i < REPS; i++) {
(new Socket("localhost", ss.getLocalPort())).close(); (new Socket(InetAddress.getLoopbackAddress(), ss.getLocalPort())).close();
} }
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2017, 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
@ -39,6 +39,7 @@ import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference; import java.lang.ref.WeakReference;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.io.IOException; import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket; import java.net.ServerSocket;
import java.net.Socket; import java.net.Socket;
import java.net.SocketImpl; import java.net.SocketImpl;
@ -71,8 +72,8 @@ public class UnreferencedSockets {
ServerSocket ss; ServerSocket ss;
Server() throws IOException { Server(InetAddress address) throws IOException {
ss = new ServerSocket(0); ss = new ServerSocket(0, 0, address);
pendingSockets.add(new NamedWeak(ss, pendingQueue, "serverSocket")); pendingSockets.add(new NamedWeak(ss, pendingQueue, "serverSocket"));
extractRefs(ss, "serverSocket"); extractRefs(ss, "serverSocket");
} }
@ -81,7 +82,6 @@ public class UnreferencedSockets {
return ss.getLocalPort(); return ss.getLocalPort();
} }
public void run() { public void run() {
try { try {
Socket s = ss.accept(); Socket s = ss.accept();
@ -111,9 +111,9 @@ public class UnreferencedSockets {
public static void main(String args[]) throws Exception { public static void main(String args[]) throws Exception {
IPSupport.throwSkippedExceptionIfNonOperational(); IPSupport.throwSkippedExceptionIfNonOperational();
InetAddress lba = InetAddress.getLoopbackAddress();
// Create and close a ServerSocket to warm up the FD count for side effects. // Create and close a ServerSocket to warm up the FD count for side effects.
try (ServerSocket s = new ServerSocket(0)) { try (ServerSocket s = new ServerSocket(0, 0, lba)) {
// no-op; close immediately // no-op; close immediately
s.getLocalPort(); // no-op s.getLocalPort(); // no-op
} }
@ -122,11 +122,11 @@ public class UnreferencedSockets {
listProcFD(); listProcFD();
// start a server // start a server
Server svr = new Server(); Server svr = new Server(lba);
Thread thr = new Thread(svr); Thread thr = new Thread(svr);
thr.start(); thr.start();
Socket s = new Socket("localhost", svr.localPort()); Socket s = new Socket(lba, svr.localPort());
pendingSockets.add(new NamedWeak(s, pendingQueue, "clientSocket")); pendingSockets.add(new NamedWeak(s, pendingQueue, "clientSocket"));
extractRefs(s, "clientSocket"); extractRefs(s, "clientSocket");

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2003, 2012, 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
@ -25,7 +25,7 @@
* @bug 4920526 * @bug 4920526
* @summary Needs per connection proxy support for URLs * @summary Needs per connection proxy support for URLs
* @modules java.base/sun.net.www * @modules java.base/sun.net.www
* @library ../../../sun/net/www/httptest/ * @library ../../../sun/net/www/httptest/ /test/lib
* @build ClosedChannelList TestHttpServer HttpTransaction HttpCallback * @build ClosedChannelList TestHttpServer HttpTransaction HttpCallback
* @compile PerConnectionProxy.java * @compile PerConnectionProxy.java
* @run main/othervm -Dhttp.proxyHost=inexistant -Dhttp.proxyPort=8080 PerConnectionProxy * @run main/othervm -Dhttp.proxyHost=inexistant -Dhttp.proxyPort=8080 PerConnectionProxy
@ -33,7 +33,8 @@
import java.net.*; import java.net.*;
import java.io.*; import java.io.*;
import sun.net.www.*;
import jdk.test.lib.net.URIBuilder;
public class PerConnectionProxy implements HttpCallback { public class PerConnectionProxy implements HttpCallback {
static TestHttpServer server; static TestHttpServer server;
@ -49,12 +50,17 @@ public class PerConnectionProxy implements HttpCallback {
public static void main(String[] args) { public static void main(String[] args) {
try { try {
server = new TestHttpServer (new PerConnectionProxy(), 1, 10, 0); InetAddress loopbackAddress = InetAddress.getLoopbackAddress();
ProxyServer pserver = new ProxyServer(InetAddress.getByName("localhost"), server.getLocalPort()); server = new TestHttpServer(new PerConnectionProxy(), 1, 10, loopbackAddress, 0);
ProxyServer pserver = new ProxyServer(loopbackAddress, server.getLocalPort());
// start proxy server // start proxy server
new Thread(pserver).start(); new Thread(pserver).start();
URL url = new URL("http://localhost:"+server.getLocalPort()); URL url = URIBuilder.newBuilder()
.scheme("http")
.loopback()
.port(server.getLocalPort())
.toURLUnchecked();
// for non existing proxy expect an IOException // for non existing proxy expect an IOException
try { try {
@ -80,7 +86,9 @@ public class PerConnectionProxy implements HttpCallback {
// for a normal proxy setting expect to see connection // for a normal proxy setting expect to see connection
// goes through that proxy // goes through that proxy
try { try {
InetSocketAddress isa = InetSocketAddress.createUnresolved("localhost", pserver.getPort()); InetSocketAddress isa = InetSocketAddress.createUnresolved(
loopbackAddress.getHostAddress(),
pserver.getPort());
Proxy p = new Proxy(Proxy.Type.HTTP, isa); Proxy p = new Proxy(Proxy.Type.HTTP, isa);
HttpURLConnection urlc = (HttpURLConnection)url.openConnection (p); HttpURLConnection urlc = (HttpURLConnection)url.openConnection (p);
int respCode = urlc.getResponseCode(); int respCode = urlc.getResponseCode();
@ -115,7 +123,7 @@ public class PerConnectionProxy implements HttpCallback {
public ProxyServer(InetAddress server, int port) throws IOException { public ProxyServer(InetAddress server, int port) throws IOException {
serverInetAddr = server; serverInetAddr = server;
serverPort = port; serverPort = port;
ss = new ServerSocket(0); ss = new ServerSocket(0, 0, InetAddress.getLoopbackAddress());
} }
public void run() { public void run() {

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2001, 2011, 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,11 +24,14 @@
/** /**
* @test * @test
* @bug 4380568 7095949 * @bug 4380568 7095949
* @library /test/lib
* @summary HttpURLConnection does not support 307 redirects * @summary HttpURLConnection does not support 307 redirects
*/ */
import java.io.*; import java.io.*;
import java.net.*; import java.net.*;
import jdk.test.lib.net.URIBuilder;
class RedirServer extends Thread { class RedirServer extends Thread {
static final int TIMEOUT = 10 * 1000; static final int TIMEOUT = 10 * 1000;
@ -100,12 +103,16 @@ class RedirServer extends Thread {
public class Redirect307Test { public class Redirect307Test {
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
ServerSocket sock = new ServerSocket(0); ServerSocket sock = new ServerSocket(0, 0, InetAddress.getLoopbackAddress());
int port = sock.getLocalPort(); int port = sock.getLocalPort();
RedirServer server = new RedirServer(sock); RedirServer server = new RedirServer(sock);
server.start(); server.start();
URL url = new URL("http://localhost:" + port); URL url = URIBuilder.newBuilder()
.scheme("http")
.loopback()
.port(port)
.toURL();
URLConnection conURL = url.openConnection(); URLConnection conURL = url.openConnection();
conURL.setDoInput(true); conURL.setDoInput(true);
conURL.setAllowUserInteraction(false); conURL.setAllowUserInteraction(false);

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2001, 2011, 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,6 +24,7 @@
/** /**
* @test * @test
* @bug 4458085 7095949 * @bug 4458085 7095949
* @library /test/lib
* @summary Redirects Limited to 5 * @summary Redirects Limited to 5
*/ */
@ -35,6 +36,8 @@
import java.io.*; import java.io.*;
import java.net.*; import java.net.*;
import jdk.test.lib.net.URIBuilder;
class RedirLimitServer extends Thread { class RedirLimitServer extends Thread {
static final int TIMEOUT = 10 * 1000; static final int TIMEOUT = 10 * 1000;
static final int NUM_REDIRECTS = 9; static final int NUM_REDIRECTS = 9;
@ -105,12 +108,16 @@ class RedirLimitServer extends Thread {
public class RedirectLimit { public class RedirectLimit {
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
ServerSocket ss = new ServerSocket (0); ServerSocket ss = new ServerSocket(0, 0, InetAddress.getLoopbackAddress());
int port = ss.getLocalPort(); int port = ss.getLocalPort();
RedirLimitServer server = new RedirLimitServer(ss); RedirLimitServer server = new RedirLimitServer(ss);
server.start(); server.start();
URL url = new URL("http://localhost:" + port); URL url = URIBuilder.newBuilder()
.scheme("http")
.loopback()
.port(port)
.toURL();
URLConnection conURL = url.openConnection(); URLConnection conURL = url.openConnection();
conURL.setDoInput(true); conURL.setDoInput(true);

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2015, 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,7 +25,7 @@
* @test * @test
* @bug 8132734 8194070 * @bug 8132734 8194070
* @summary Test the System properties for JarFile that support multi-release jar files * @summary Test the System properties for JarFile that support multi-release jar files
* @library /lib/testlibrary/java/util/jar * @library /lib/testlibrary/java/util/jar /test/lib
* @modules jdk.jartool * @modules jdk.jartool
* jdk.compiler * jdk.compiler
* jdk.httpserver * jdk.httpserver
@ -44,9 +44,12 @@
*/ */
import java.io.IOException; import java.io.IOException;
import java.net.InetAddress;
import java.net.URL; import java.net.URL;
import java.net.URLClassLoader; import java.net.URLClassLoader;
import jdk.test.lib.net.URIBuilder;
import org.testng.Assert; import org.testng.Assert;
import org.testng.annotations.AfterClass; import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeClass;
@ -57,7 +60,7 @@ public class MultiReleaseJarHttpProperties extends MultiReleaseJarProperties {
@BeforeClass @BeforeClass
public void initialize() throws Exception { public void initialize() throws Exception {
server = new SimpleHttpServer(); server = new SimpleHttpServer(InetAddress.getLoopbackAddress());
server.start(); server.start();
super.initialize(); super.initialize();
} }
@ -65,7 +68,8 @@ public class MultiReleaseJarHttpProperties extends MultiReleaseJarProperties {
@Override @Override
protected void initializeClassLoader() throws Exception { protected void initializeClassLoader() throws Exception {
URL[] urls = new URL[]{ URL[] urls = new URL[]{
new URL("http://localhost:" + server.getPort() + "/multi-release.jar") URIBuilder.newBuilder().scheme("http").port(server.getPort()).loopback()
.path("/multi-release.jar").toURL(),
}; };
cldr = new URLClassLoader(urls); cldr = new URLClassLoader(urls);
// load any class, Main is convenient and in the root entries // load any class, Main is convenient and in the root entries

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2015, 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
@ -26,6 +26,7 @@ import com.sun.net.httpserver.*;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.net.InetAddress;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
@ -42,13 +43,19 @@ class SimpleHttpServer {
private static final Path multirelease = Paths.get(userdir, "multi-release.jar"); private static final Path multirelease = Paths.get(userdir, "multi-release.jar");
private final HttpServer server; private final HttpServer server;
private final InetAddress address;
public SimpleHttpServer() throws IOException { public SimpleHttpServer() throws IOException {
this(null);
}
public SimpleHttpServer(InetAddress addr) throws IOException {
address = addr;
server = HttpServer.create(); server = HttpServer.create();
} }
public void start() throws IOException { public void start() throws IOException {
server.bind(new InetSocketAddress(0), 0); server.bind(new InetSocketAddress(address, 0), 0);
server.createContext("/multi-release.jar", t -> { server.createContext("/multi-release.jar", t -> {
try (InputStream is = t.getRequestBody()) { try (InputStream is = t.getRequestBody()) {
is.readAllBytes(); // probably not necessary to consume request is.readAllBytes(); // probably not necessary to consume request

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2003, 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
@ -61,7 +61,7 @@ public class RetryUponTimeout implements Runnable {
static int count = 0; static int count = 0;
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
try { try {
server = new ServerSocket (0); server = new ServerSocket(0, 0, InetAddress.getLoopbackAddress());
int port = server.getLocalPort (); int port = server.getLocalPort ();
new Thread(new RetryUponTimeout()).start (); new Thread(new RetryUponTimeout()).start ();

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2015, 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,7 +25,7 @@
* @test * @test
* @bug 8132734 8144062 8159785 8194070 * @bug 8132734 8144062 8159785 8194070
* @summary Test that URL connections to multi-release jars can be runtime versioned * @summary Test that URL connections to multi-release jars can be runtime versioned
* @library /lib/testlibrary/java/util/jar * @library /lib/testlibrary/java/util/jar /test/lib
* @modules jdk.compiler * @modules jdk.compiler
* jdk.httpserver * jdk.httpserver
* jdk.jartool * jdk.jartool
@ -38,7 +38,11 @@ import java.io.InputStream;
import java.lang.invoke.MethodHandle; import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles; import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType; import java.lang.invoke.MethodType;
import java.net.InetAddress;
import java.net.JarURLConnection; import java.net.JarURLConnection;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL; import java.net.URL;
import java.net.URLClassLoader; import java.net.URLClassLoader;
import java.net.URLConnection; import java.net.URLConnection;
@ -47,6 +51,8 @@ import java.nio.file.Paths;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.jar.JarFile; import java.util.jar.JarFile;
import jdk.test.lib.net.URIBuilder;
import org.testng.Assert; import org.testng.Assert;
import org.testng.annotations.AfterClass; import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeClass;
@ -68,7 +74,7 @@ public class MultiReleaseJarURLConnection {
creator.buildMultiReleaseJar(); creator.buildMultiReleaseJar();
creator.buildSignedMultiReleaseJar(); creator.buildSignedMultiReleaseJar();
server = new SimpleHttpServer(); server = new SimpleHttpServer(InetAddress.getLoopbackAddress());
server.start(); server.start();
} }
@ -167,9 +173,9 @@ public class MultiReleaseJarURLConnection {
{"unsigned", new URL("jar:file:" + unsigned + "!/")}, {"unsigned", new URL("jar:file:" + unsigned + "!/")},
{"signed", new URL("jar:file:" + signed + "!/")}, {"signed", new URL("jar:file:" + signed + "!/")},
// external jar received via http protocol // external jar received via http protocol
{"http", new URL("jar:http://localhost:" + server.getPort() + "/multi-release.jar!/")}, {"http", toHttpJarURL(server.getPort(), "/multi-release.jar", "!/")},
{"http", new URL("http://localhost:" + server.getPort() + "/multi-release.jar")}, {"http", URIBuilder.newBuilder().scheme("http").port(server.getPort())
.loopback().path("/multi-release.jar").toURL()},
}; };
} }
@ -220,6 +226,18 @@ public class MultiReleaseJarURLConnection {
cldr.close(); cldr.close();
} }
private static URL toHttpJarURL(int port, String jar, String file)
throws MalformedURLException, URISyntaxException {
assert file.startsWith("!/");
URI httpURI = URIBuilder.newBuilder()
.scheme("http")
.loopback()
.port(port)
.path(jar)
.build();
return new URL("jar:" + httpURI + file);
}
private boolean readAndCompare(URL url, String match) throws Exception { private boolean readAndCompare(URL url, String match) throws Exception {
boolean result; boolean result;
// necessary to do it this way, instead of openStream(), so we can // necessary to do it this way, instead of openStream(), so we can