8229486: Replace wildcard address with loopback or local host in tests - part 21
Reviewed-by: chegar
--- a/test/jdk/java/net/SocketOption/TcpKeepAliveTest.java Thu Aug 15 15:39:43 2019 +0800
+++ b/test/jdk/java/net/SocketOption/TcpKeepAliveTest.java Thu Aug 15 12:58:27 2019 +0100
@@ -31,6 +31,7 @@
import java.io.IOException;
import java.net.DatagramSocket;
import java.net.InetAddress;
+import java.net.InetSocketAddress;
import java.net.MulticastSocket;
import java.net.ServerSocket;
import java.net.Socket;
@@ -43,9 +44,9 @@
private static final int DEFAULT_KEEP_ALIVE_INTVL = 53;
public static void main(String args[]) throws IOException {
-
- try (ServerSocket ss = new ServerSocket(0);
- Socket s = new Socket(InetAddress.getLoopbackAddress(), ss.getLocalPort());
+ var loopback = InetAddress.getLoopbackAddress();
+ try (ServerSocket ss = boundServer(loopback);
+ Socket s = new Socket(loopback, ss.getLocalPort());
DatagramSocket ds = new DatagramSocket(0);
MulticastSocket mc = new MulticastSocket(0)) {
if (ss.supportedOptions().contains(ExtendedSocketOptions.TCP_KEEPIDLE)) {
@@ -110,4 +111,11 @@
}
}
}
+
+ private static ServerSocket boundServer(InetAddress address) throws IOException {
+ var socketAddress = new InetSocketAddress(address, 0);
+ var server = new ServerSocket();
+ server.bind(socketAddress);
+ return server;
+ }
}
--- a/test/jdk/java/net/URLConnection/SetIfModifiedSince.java Thu Aug 15 15:39:43 2019 +0800
+++ b/test/jdk/java/net/URLConnection/SetIfModifiedSince.java Thu Aug 15 12:58:27 2019 +0100
@@ -36,6 +36,7 @@
public class SetIfModifiedSince {
static volatile boolean successfulHeaderCheck = false;
+ static final String MARKER = "A-test-name";
static class XServer extends Thread {
ServerSocket srv;
@@ -52,28 +53,49 @@
}
public void run() {
- try {
+ boolean foundMarker = false;
+ while (!foundMarker) {
String x;
- s = srv.accept ();
- is = s.getInputStream ();
- BufferedReader r = new BufferedReader(new InputStreamReader(is));
- os = s.getOutputStream ();
- while ((x=r.readLine()) != null) {
- String header = "If-Modified-Since: ";
- if (x.startsWith(header)) {
- if (x.charAt(header.length()) == '?') {
- s.close ();
- srv.close (); // or else the HTTPURLConnection will retry
- throw new RuntimeException
- ("Invalid HTTP date specification");
+ try {
+ s = srv.accept();
+ System.out.println("Server: accepting connection from: " + s);
+ is = s.getInputStream ();
+ } catch (IOException io) {
+ System.err.println("Server: Failed to accept connection: " + io);
+ io.printStackTrace();
+ try { srv.close(); } catch (IOException ioc) { }
+ break;
+ }
+ try {
+ BufferedReader r = new BufferedReader(new InputStreamReader(is));
+ os = s.getOutputStream ();
+ boolean foundHeader;
+ while ((x=r.readLine()) != null) {
+ String testname = MARKER + ": ";
+ String header = "If-Modified-Since: ";
+ if (x.startsWith(header)) {
+ foundHeader = true;
+ System.out.println("Server: found header: " + x);
+ if (x.charAt(header.length()) == '?') {
+ s.close ();
+ srv.close (); // or else the HTTPURLConnection will retry
+ throw new RuntimeException
+ ("Invalid HTTP date specification");
+ }
+ if (foundMarker) break;
+ } else if (x.startsWith(testname)) {
+ foundMarker = true;
+ System.out.println("Server: found marker: " + x);
}
- break;
}
- }
- successfulHeaderCheck = true;
- s.close ();
- srv.close (); // or else the HTTPURLConnection will retry
- } catch (IOException e) {}
+ successfulHeaderCheck = true;
+ s.close ();
+ // only close server if connected from this test.
+ if (foundMarker) {
+ srv.close (); // or else the HTTPURLConnection will retry
+ }
+ } catch (IOException e) {}
+ }
}
}
@@ -94,6 +116,7 @@
.path("/index.html")
.toURLUnchecked();
URLConnection urlc = url.openConnection(Proxy.NO_PROXY);
+ urlc.setRequestProperty(MARKER, "SetIfModifiedSince");
urlc.setIfModifiedSince (10000000);
InputStream is = urlc.getInputStream ();
int i = 0, c;
--- a/test/jdk/sun/net/www/http/HttpClient/GetProxyPort.java Thu Aug 15 15:39:43 2019 +0800
+++ b/test/jdk/sun/net/www/http/HttpClient/GetProxyPort.java Thu Aug 15 12:58:27 2019 +0100
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2004, 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
@@ -27,15 +27,25 @@
* @summary REGRESSION: Sun implementation for HttpURLConnection could throw NPE
* @modules java.base/sun.net
* java.base/sun.net.www.http
+ * @library /test/lib
*/
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.URL;
import sun.net.www.http.HttpClient;
+import jdk.test.lib.net.URIBuilder;
public class GetProxyPort {
public static void main(String[] args) throws Exception {
- ServerSocket ss = new ServerSocket(0);
- URL myURL = new URL("http://localhost:" + ss.getLocalPort());
+ ServerSocket ss = new ServerSocket();
+ InetAddress loopback = InetAddress.getLoopbackAddress();
+ ss.bind(new InetSocketAddress(loopback, 0));
+ URL myURL = URIBuilder.newBuilder()
+ .scheme("http")
+ .loopback()
+ .port(ss.getLocalPort())
+ .toURL();
HttpClient httpC = new HttpClient(myURL, null, -1);
int port = httpC.getProxyPortUsed();
}
--- a/test/jdk/sun/net/www/http/HttpClient/ImplicitFileName.java Thu Aug 15 15:39:43 2019 +0800
+++ b/test/jdk/sun/net/www/http/HttpClient/ImplicitFileName.java Thu Aug 15 12:58:27 2019 +0100
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 1999, 2001, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1999, 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
@@ -27,19 +27,29 @@
* @summary Make sure that implicit filenames will be returned as "/"
* @modules java.base/sun.net
* java.base/sun.net.www.http
+ * @library /test/lib
*/
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
import java.net.URL;
import java.net.ServerSocket;
import sun.net.www.http.HttpClient;
+import jdk.test.lib.net.URIBuilder;
public class ImplicitFileName {
public static void main(String[] args) throws Exception {
- ServerSocket ss = new ServerSocket(0);
+ ServerSocket ss = new ServerSocket();
+ InetAddress loopback = InetAddress.getLoopbackAddress();
+ ss.bind(new InetSocketAddress(loopback, 0));
- URL url = new URL("http://localhost:" + ss.getLocalPort());
+ URL url = URIBuilder.newBuilder()
+ .scheme("http")
+ .loopback()
+ .port(ss.getLocalPort())
+ .toURL();
HttpClient c = HttpClient.New(url);
--- a/test/jdk/sun/net/www/http/HttpClient/IsAvailable.java Thu Aug 15 15:39:43 2019 +0800
+++ b/test/jdk/sun/net/www/http/HttpClient/IsAvailable.java Thu Aug 15 12:58:27 2019 +0100
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2013, 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
@@ -28,43 +28,55 @@
* has been closed
* @modules java.base/sun.net
* java.base/sun.net.www.http:+open
+ * @library /test/lib
*/
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
import java.net.URL;
import java.net.ServerSocket;
import sun.net.www.http.HttpClient;
import java.security.*;
import java.lang.reflect.Method;
+import jdk.test.lib.net.URIBuilder;
public class IsAvailable {
public static void main(String[] args) throws Exception {
int readTimeout = 20;
- ServerSocket ss = new ServerSocket(0);
+ ServerSocket ss = new ServerSocket();
+ InetAddress loopback = InetAddress.getLoopbackAddress();
+ ss.bind(new InetSocketAddress(loopback, 0));
+
+ try (ServerSocket toclose = ss) {
- URL url1 = new URL("http://localhost:" + ss.getLocalPort());
- HttpClient c1 = HttpClient.New(url1);
+ URL url1 = URIBuilder.newBuilder()
+ .scheme("http")
+ .loopback()
+ .port(ss.getLocalPort())
+ .toURL();
- Method available = HttpClient.class.
- getDeclaredMethod("available", null);
- available.setAccessible(true);
+ HttpClient c1 = HttpClient.New(url1);
- c1.setReadTimeout(readTimeout);
- boolean a = (boolean) available.invoke(c1);
- if (!a) {
- throw new RuntimeException("connection should be available");
- }
- if (c1.getReadTimeout() != readTimeout) {
- throw new RuntimeException("read timeout has been altered");
- }
+ Method available = HttpClient.class.
+ getDeclaredMethod("available", null);
+ available.setAccessible(true);
- c1.closeServer();
+ c1.setReadTimeout(readTimeout);
+ boolean a = (boolean) available.invoke(c1);
+ if (!a) {
+ throw new RuntimeException("connection should be available");
+ }
+ if (c1.getReadTimeout() != readTimeout) {
+ throw new RuntimeException("read timeout has been altered");
+ }
- a = (boolean) available.invoke(c1);
- if (a) {
- throw new RuntimeException("connection shouldn't be available");
+ c1.closeServer();
+
+ a = (boolean) available.invoke(c1);
+ if (a) {
+ throw new RuntimeException("connection shouldn't be available");
+ }
}
-
- ss.close();
}
}
--- a/test/jdk/sun/net/www/http/HttpClient/IsKeepingAlive.java Thu Aug 15 15:39:43 2019 +0800
+++ b/test/jdk/sun/net/www/http/HttpClient/IsKeepingAlive.java Thu Aug 15 12:58:27 2019 +0100
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 1999, 2001, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1999, 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
@@ -28,32 +28,42 @@
* doPrivileged() call at appropriate places.
* @modules java.base/sun.net
* java.base/sun.net.www.http
+ * @library /test/lib
* @run main/othervm/policy=IsKeepingAlive.policy IsKeepingAlive
*/
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
import java.net.URL;
import java.net.ServerSocket;
import sun.net.www.http.HttpClient;
import java.security.*;
+import jdk.test.lib.net.URIBuilder;
public class IsKeepingAlive {
public static void main(String[] args) throws Exception {
- ServerSocket ss = new ServerSocket(0);
+ ServerSocket ss = new ServerSocket();
+ InetAddress loopback = InetAddress.getLoopbackAddress();
+ ss.bind(new InetSocketAddress(loopback, 0));
- SecurityManager security = System.getSecurityManager();
- if (security == null) {
- security = new SecurityManager();
- System.setSecurityManager(security);
- }
+ try (ServerSocket toClose = ss) {
+ SecurityManager security = System.getSecurityManager();
+ if (security == null) {
+ security = new SecurityManager();
+ System.setSecurityManager(security);
+ }
- URL url1 = new URL("http://localhost:" + ss.getLocalPort());
-
- HttpClient c1 = HttpClient.New(url1);
+ URL url1 = URIBuilder.newBuilder()
+ .scheme("http")
+ .loopback()
+ .port(ss.getLocalPort())
+ .toURL();
- boolean keepAlive = c1.isKeepingAlive();
+ HttpClient c1 = HttpClient.New(url1);
- ss.close();
+ boolean keepAlive = c1.isKeepingAlive();
+ }
}
}
--- a/test/jdk/sun/net/www/http/HttpClient/OpenServer.java Thu Aug 15 15:39:43 2019 +0800
+++ b/test/jdk/sun/net/www/http/HttpClient/OpenServer.java Thu Aug 15 12:58:27 2019 +0100
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 1998, 2001, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1998, 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
@@ -27,22 +27,31 @@
* @summary Make sure HttpClient has
* doPrivileged() calls at appropriate places.
* @modules java.base/sun.net.www.http
+ * @library /test/lib
* @run main/othervm/policy=OpenServer.policy OpenServer
*/
import java.net.*;
import sun.net.www.http.HttpClient;
+import jdk.test.lib.net.URIBuilder;
public class OpenServer {
OpenServer() throws Exception {
- ServerSocket ss = new ServerSocket(0);
+ ServerSocket ss = new ServerSocket();
+ InetAddress loopback = InetAddress.getLoopbackAddress();
+ ss.bind(new InetSocketAddress(loopback, 0));
- URL myURL = new URL("http://localhost:" + ss.getLocalPort());
- HttpClient httpC = new HttpClient(myURL, null, -1);
+ try (ServerSocket toClose = ss) {
+ URL myURL = URIBuilder.newBuilder()
+ .scheme("http")
+ .loopback()
+ .port(ss.getLocalPort())
+ .toURL();
- ss.close();
+ HttpClient httpC = new HttpClient(myURL, null, -1);
+ }
}
public static void main(String [] args) throws Exception {
--- a/test/jdk/sun/net/www/http/KeepAliveStream/KeepAliveStreamCloseWithWrongContentLength.java Thu Aug 15 15:39:43 2019 +0800
+++ b/test/jdk/sun/net/www/http/KeepAliveStream/KeepAliveStreamCloseWithWrongContentLength.java Thu Aug 15 12:58:27 2019 +0100
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2002, 2010, 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
@@ -25,11 +25,13 @@
* @test
* @bug 4533243
* @summary Closing a keep alive stream gives NullPointerException
+ * @library /test/lib
* @run main/othervm/timeout=30 KeepAliveStreamCloseWithWrongContentLength
*/
import java.net.*;
import java.io.*;
+import jdk.test.lib.net.URIBuilder;
public class KeepAliveStreamCloseWithWrongContentLength {
@@ -75,13 +77,20 @@
}
public static void main (String[] args) throws Exception {
- ServerSocket serversocket = new ServerSocket (0);
+ final InetAddress loopback = InetAddress.getLoopbackAddress();
+ final ServerSocket serversocket = new ServerSocket();
+ serversocket.bind(new InetSocketAddress(loopback, 0));
+
try {
int port = serversocket.getLocalPort ();
XServer server = new XServer (serversocket);
server.start ();
- URL url = new URL ("http://localhost:"+port);
- HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
+ URL url = URIBuilder.newBuilder()
+ .scheme("http")
+ .loopback()
+ .port(port)
+ .toURL();
+ HttpURLConnection urlc = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);
InputStream is = urlc.getInputStream ();
int c = 0;
while (c != -1) {
@@ -98,7 +107,7 @@
} catch (NullPointerException e) {
throw new RuntimeException (e);
} finally {
- if (serversocket != null) serversocket.close();
+ serversocket.close();
}
}
}
--- a/test/jdk/sun/net/www/protocol/http/StreamingOutputStream.java Thu Aug 15 15:39:43 2019 +0800
+++ b/test/jdk/sun/net/www/protocol/http/StreamingOutputStream.java Thu Aug 15 12:58:27 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
@@ -60,7 +60,7 @@
InetSocketAddress address = httpServer.getAddress();
URL url = new URL("http://" + address.getHostName() + ":" + address.getPort() + "/test/");
- HttpURLConnection uc = (HttpURLConnection)url.openConnection();
+ HttpURLConnection uc = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);
uc.setDoOutput(true);
uc.setFixedLengthStreamingMode(1);
@@ -87,7 +87,18 @@
* Http Server
*/
void startHttpServer() throws IOException {
- httpServer = com.sun.net.httpserver.HttpServer.create(new InetSocketAddress(0), 0);
+ InetAddress address = InetAddress.getLocalHost();
+ if (!InetAddress.getByName(address.getHostName()).equals(address)) {
+ // if this happens then we should possibly change the client
+ // side to use the address literal in its URL instead of
+ // the host name.
+ throw new IOException(address.getHostName()
+ + " resolves to "
+ + InetAddress.getByName(address.getHostName())
+ + " not to "
+ + address + ": check host configuration.");
+ }
+ httpServer = com.sun.net.httpserver.HttpServer.create(new InetSocketAddress(address, 0), 0);
HttpContext ctx = httpServer.createContext("/test/", new MyHandler());
httpServer.start();
}
--- a/test/jdk/sun/net/www/protocol/http/UserAuth.java Thu Aug 15 15:39:43 2019 +0800
+++ b/test/jdk/sun/net/www/protocol/http/UserAuth.java Thu Aug 15 12:58:27 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
@@ -81,7 +81,19 @@
* Http Server
*/
void startHttpServer() throws IOException {
- httpServer = com.sun.net.httpserver.HttpServer.create(new InetSocketAddress(0), 0);
+ InetAddress address = InetAddress.getLocalHost();
+ if (!InetAddress.getByName(address.getHostName()).equals(address)) {
+ // if this happens then we should possibly change the client
+ // side to use the address literal in its URL instead of
+ // the host name.
+ throw new IOException(address.getHostName()
+ + " resolves to "
+ + InetAddress.getByName(address.getHostName())
+ + " not to "
+ + address + ": check host configuration.");
+ }
+
+ httpServer = com.sun.net.httpserver.HttpServer.create(new InetSocketAddress(address, 0), 0);
// create HttpServer context
HttpContext ctx = httpServer.createContext("/redirect/", new RedirectHandler());
--- a/test/jdk/sun/net/www/protocol/http/UserCookie.java Thu Aug 15 15:39:43 2019 +0800
+++ b/test/jdk/sun/net/www/protocol/http/UserCookie.java Thu Aug 15 12:58:27 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
@@ -78,7 +78,19 @@
* Http Server
*/
void startHttpServer() throws IOException {
- httpServer = com.sun.net.httpserver.HttpServer.create(new InetSocketAddress(0), 0);
+ InetAddress address = InetAddress.getLocalHost();
+ if (!InetAddress.getByName(address.getHostName()).equals(address)) {
+ // if this happens then we should possibly change the client
+ // side to use the address literal in its URL instead of
+ // the host name.
+ throw new IOException(address.getHostName()
+ + " resolves to "
+ + InetAddress.getByName(address.getHostName())
+ + " not to "
+ + address + ": check host configuration.");
+ }
+
+ httpServer = com.sun.net.httpserver.HttpServer.create(new InetSocketAddress(address, 0), 0);
// create HttpServer context
HttpContext ctx = httpServer.createContext("/test/", new MyHandler());