changeset 55336:43439afaab4a

8223638: Replace wildcard address with loopback or local host in tests - part 6 Reviewed-by: dfuchs
author aefimov
date Tue, 14 May 2019 13:34:49 +0100
parents a82655619efd
children f9c8e16db3dd
files test/jdk/java/net/ServerSocket/AcceptCauseFileDescriptorLeak.java test/jdk/java/net/ServerSocket/UnreferencedSockets.java test/jdk/java/net/URL/PerConnectionProxy.java test/jdk/java/net/URLConnection/Redirect307Test.java test/jdk/java/net/URLConnection/RedirectLimit.java test/jdk/java/util/jar/JarFile/mrjar/MultiReleaseJarHttpProperties.java test/jdk/lib/testlibrary/java/util/jar/SimpleHttpServer.java test/jdk/sun/net/www/protocol/http/RetryUponTimeout.java test/jdk/sun/net/www/protocol/jar/MultiReleaseJarURLConnection.java
diffstat 9 files changed, 91 insertions(+), 39 deletions(-) [+]
line wrap: on
line diff
--- a/test/jdk/java/net/ServerSocket/AcceptCauseFileDescriptorLeak.java	Tue May 14 10:54:23 2019 +0200
+++ b/test/jdk/java/net/ServerSocket/AcceptCauseFileDescriptorLeak.java	Tue May 14 13:34:49 2019 +0100
@@ -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.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -41,6 +41,7 @@
  */
 
 import java.io.IOException;
+import java.net.InetAddress;
 import java.net.ServerSocket;
 import java.net.Socket;
 import java.util.List;
@@ -80,7 +81,7 @@
             }
         }
 
-        final ServerSocket ss = new ServerSocket(0) {
+        final ServerSocket ss = new ServerSocket(0, 0, InetAddress.getLoopbackAddress()) {
             public Socket accept() throws IOException {
                 Socket s = new Socket() {
                 };
@@ -93,7 +94,7 @@
             public void run() {
                 try {
                     for (int i = 0; i < REPS; i++) {
-                        (new Socket("localhost", ss.getLocalPort())).close();
+                        (new Socket(InetAddress.getLoopbackAddress(), ss.getLocalPort())).close();
                     }
                 } catch (IOException e) {
                     e.printStackTrace();
--- a/test/jdk/java/net/ServerSocket/UnreferencedSockets.java	Tue May 14 10:54:23 2019 +0200
+++ b/test/jdk/java/net/ServerSocket/UnreferencedSockets.java	Tue May 14 13:34:49 2019 +0100
@@ -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.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -39,6 +39,7 @@
 import java.lang.ref.WeakReference;
 import java.lang.reflect.Field;
 import java.io.IOException;
+import java.net.InetAddress;
 import java.net.ServerSocket;
 import java.net.Socket;
 import java.net.SocketImpl;
@@ -71,8 +72,8 @@
 
         ServerSocket ss;
 
-        Server() throws IOException {
-            ss = new ServerSocket(0);
+        Server(InetAddress address) throws IOException {
+            ss = new ServerSocket(0, 0, address);
             pendingSockets.add(new NamedWeak(ss, pendingQueue, "serverSocket"));
             extractRefs(ss, "serverSocket");
         }
@@ -81,7 +82,6 @@
             return ss.getLocalPort();
         }
 
-
         public void run() {
             try {
                 Socket s = ss.accept();
@@ -111,9 +111,9 @@
 
     public static void main(String args[]) throws Exception {
         IPSupport.throwSkippedExceptionIfNonOperational();
-
+        InetAddress lba = InetAddress.getLoopbackAddress();
         // 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
             s.getLocalPort();   // no-op
         }
@@ -122,11 +122,11 @@
         listProcFD();
 
         // start a server
-        Server svr = new Server();
+        Server svr = new Server(lba);
         Thread thr = new Thread(svr);
         thr.start();
 
-        Socket s = new Socket("localhost", svr.localPort());
+        Socket s = new Socket(lba, svr.localPort());
         pendingSockets.add(new NamedWeak(s, pendingQueue, "clientSocket"));
         extractRefs(s, "clientSocket");
 
--- a/test/jdk/java/net/URL/PerConnectionProxy.java	Tue May 14 10:54:23 2019 +0200
+++ b/test/jdk/java/net/URL/PerConnectionProxy.java	Tue May 14 13:34:49 2019 +0100
@@ -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.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -25,7 +25,7 @@
  * @bug 4920526
  * @summary Needs per connection proxy support for URLs
  * @modules java.base/sun.net.www
- * @library ../../../sun/net/www/httptest/
+ * @library ../../../sun/net/www/httptest/ /test/lib
  * @build ClosedChannelList TestHttpServer HttpTransaction HttpCallback
  * @compile PerConnectionProxy.java
  * @run main/othervm -Dhttp.proxyHost=inexistant -Dhttp.proxyPort=8080 PerConnectionProxy
@@ -33,7 +33,8 @@
 
 import java.net.*;
 import java.io.*;
-import sun.net.www.*;
+
+import jdk.test.lib.net.URIBuilder;
 
 public class PerConnectionProxy implements HttpCallback {
     static TestHttpServer server;
@@ -49,12 +50,17 @@
 
     public static void main(String[] args) {
         try {
-            server = new TestHttpServer (new PerConnectionProxy(), 1, 10, 0);
-            ProxyServer pserver = new ProxyServer(InetAddress.getByName("localhost"), server.getLocalPort());
+            InetAddress loopbackAddress = InetAddress.getLoopbackAddress();
+            server = new TestHttpServer(new PerConnectionProxy(), 1, 10, loopbackAddress, 0);
+            ProxyServer pserver = new ProxyServer(loopbackAddress, server.getLocalPort());
             // start proxy server
             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
             try {
@@ -80,7 +86,9 @@
             // for a normal proxy setting expect to see connection
             // goes through that proxy
             try {
-                InetSocketAddress isa = InetSocketAddress.createUnresolved("localhost", pserver.getPort());
+                InetSocketAddress isa = InetSocketAddress.createUnresolved(
+                        loopbackAddress.getHostAddress(),
+                        pserver.getPort());
                 Proxy p = new Proxy(Proxy.Type.HTTP, isa);
                 HttpURLConnection urlc = (HttpURLConnection)url.openConnection (p);
                 int respCode = urlc.getResponseCode();
@@ -115,7 +123,7 @@
         public ProxyServer(InetAddress server, int port) throws IOException {
             serverInetAddr = server;
             serverPort = port;
-            ss = new ServerSocket(0);
+            ss = new ServerSocket(0, 0, InetAddress.getLoopbackAddress());
         }
 
         public void run() {
--- a/test/jdk/java/net/URLConnection/Redirect307Test.java	Tue May 14 10:54:23 2019 +0200
+++ b/test/jdk/java/net/URLConnection/Redirect307Test.java	Tue May 14 13:34:49 2019 +0100
@@ -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.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -24,11 +24,14 @@
 /**
  * @test
  * @bug 4380568 7095949
+ * @library /test/lib
  * @summary  HttpURLConnection does not support 307 redirects
  */
 import java.io.*;
 import java.net.*;
 
+import jdk.test.lib.net.URIBuilder;
+
 class RedirServer extends Thread {
 
     static final int TIMEOUT = 10 * 1000;
@@ -100,12 +103,16 @@
 
 public class Redirect307Test {
     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();
         RedirServer server = new RedirServer(sock);
         server.start();
 
-        URL url = new URL("http://localhost:" + port);
+        URL url = URIBuilder.newBuilder()
+                .scheme("http")
+                .loopback()
+                .port(port)
+                .toURL();
         URLConnection conURL =  url.openConnection();
         conURL.setDoInput(true);
         conURL.setAllowUserInteraction(false);
--- a/test/jdk/java/net/URLConnection/RedirectLimit.java	Tue May 14 10:54:23 2019 +0200
+++ b/test/jdk/java/net/URLConnection/RedirectLimit.java	Tue May 14 13:34:49 2019 +0100
@@ -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.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -24,6 +24,7 @@
 /**
  * @test
  * @bug 4458085 7095949
+ * @library /test/lib
  * @summary  Redirects Limited to 5
  */
 
@@ -35,6 +36,8 @@
 import java.io.*;
 import java.net.*;
 
+import jdk.test.lib.net.URIBuilder;
+
 class RedirLimitServer extends Thread {
     static final int TIMEOUT = 10 * 1000;
     static final int NUM_REDIRECTS = 9;
@@ -105,12 +108,16 @@
 
 public class RedirectLimit {
     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();
         RedirLimitServer server = new RedirLimitServer(ss);
         server.start();
 
-        URL url = new URL("http://localhost:" + port);
+        URL url = URIBuilder.newBuilder()
+                .scheme("http")
+                .loopback()
+                .port(port)
+                .toURL();
         URLConnection conURL =  url.openConnection();
 
         conURL.setDoInput(true);
--- a/test/jdk/java/util/jar/JarFile/mrjar/MultiReleaseJarHttpProperties.java	Tue May 14 10:54:23 2019 +0200
+++ b/test/jdk/java/util/jar/JarFile/mrjar/MultiReleaseJarHttpProperties.java	Tue May 14 13:34:49 2019 +0100
@@ -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.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -25,7 +25,7 @@
  * @test
  * @bug 8132734 8194070
  * @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
  *          jdk.compiler
  *          jdk.httpserver
@@ -44,9 +44,12 @@
  */
 
 import java.io.IOException;
+import java.net.InetAddress;
 import java.net.URL;
 import java.net.URLClassLoader;
 
+import jdk.test.lib.net.URIBuilder;
+
 import org.testng.Assert;
 import org.testng.annotations.AfterClass;
 import org.testng.annotations.BeforeClass;
@@ -57,7 +60,7 @@
 
     @BeforeClass
     public void initialize() throws Exception {
-        server = new SimpleHttpServer();
+        server = new SimpleHttpServer(InetAddress.getLoopbackAddress());
         server.start();
         super.initialize();
     }
@@ -65,7 +68,8 @@
     @Override
     protected void initializeClassLoader() throws Exception {
         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);
         // load any class, Main is convenient and in the root entries
--- a/test/jdk/lib/testlibrary/java/util/jar/SimpleHttpServer.java	Tue May 14 10:54:23 2019 +0200
+++ b/test/jdk/lib/testlibrary/java/util/jar/SimpleHttpServer.java	Tue May 14 13:34:49 2019 +0100
@@ -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.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -26,6 +26,7 @@
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.net.InetAddress;
 import java.net.InetSocketAddress;
 import java.nio.file.Files;
 import java.nio.file.Path;
@@ -42,13 +43,19 @@
     private static final Path multirelease = Paths.get(userdir, "multi-release.jar");
 
     private final HttpServer server;
+    private final InetAddress address;
 
     public SimpleHttpServer() throws IOException {
+        this(null);
+    }
+
+    public SimpleHttpServer(InetAddress addr) throws IOException {
+        address = addr;
         server = HttpServer.create();
     }
 
     public void start() throws IOException {
-        server.bind(new InetSocketAddress(0), 0);
+        server.bind(new InetSocketAddress(address, 0), 0);
         server.createContext("/multi-release.jar", t -> {
             try (InputStream is = t.getRequestBody()) {
                 is.readAllBytes();  // probably not necessary to consume request
--- a/test/jdk/sun/net/www/protocol/http/RetryUponTimeout.java	Tue May 14 10:54:23 2019 +0200
+++ b/test/jdk/sun/net/www/protocol/http/RetryUponTimeout.java	Tue May 14 13:34:49 2019 +0100
@@ -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.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -61,7 +61,7 @@
     static int count = 0;
     public static void main(String[] args) throws Exception {
         try {
-            server = new ServerSocket (0);
+            server = new ServerSocket(0, 0, InetAddress.getLoopbackAddress());
             int port = server.getLocalPort ();
             new Thread(new RetryUponTimeout()).start ();
 
--- a/test/jdk/sun/net/www/protocol/jar/MultiReleaseJarURLConnection.java	Tue May 14 10:54:23 2019 +0200
+++ b/test/jdk/sun/net/www/protocol/jar/MultiReleaseJarURLConnection.java	Tue May 14 13:34:49 2019 +0100
@@ -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.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -25,7 +25,7 @@
  * @test
  * @bug 8132734 8144062 8159785 8194070
  * @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
  *          jdk.httpserver
  *          jdk.jartool
@@ -38,7 +38,11 @@
 import java.lang.invoke.MethodHandle;
 import java.lang.invoke.MethodHandles;
 import java.lang.invoke.MethodType;
+import java.net.InetAddress;
 import java.net.JarURLConnection;
+import java.net.MalformedURLException;
+import java.net.URI;
+import java.net.URISyntaxException;
 import java.net.URL;
 import java.net.URLClassLoader;
 import java.net.URLConnection;
@@ -47,6 +51,8 @@
 import java.util.Enumeration;
 import java.util.jar.JarFile;
 
+import jdk.test.lib.net.URIBuilder;
+
 import org.testng.Assert;
 import org.testng.annotations.AfterClass;
 import org.testng.annotations.BeforeClass;
@@ -68,7 +74,7 @@
         creator.buildMultiReleaseJar();
         creator.buildSignedMultiReleaseJar();
 
-        server = new SimpleHttpServer();
+        server = new SimpleHttpServer(InetAddress.getLoopbackAddress());
         server.start();
 
     }
@@ -167,9 +173,9 @@
                 {"unsigned", new URL("jar:file:" + unsigned + "!/")},
                 {"signed", new URL("jar:file:" + signed + "!/")},
                 // external jar received via http protocol
-                {"http", new URL("jar:http://localhost:" + server.getPort() + "/multi-release.jar!/")},
-                {"http", new URL("http://localhost:" + server.getPort() + "/multi-release.jar")},
-
+                {"http", toHttpJarURL(server.getPort(), "/multi-release.jar", "!/")},
+                {"http", URIBuilder.newBuilder().scheme("http").port(server.getPort())
+                        .loopback().path("/multi-release.jar").toURL()},
         };
     }
 
@@ -220,6 +226,18 @@
         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 {
         boolean result;
         // necessary to do it this way, instead of openStream(), so we can