8229486: Replace wildcard address with loopback or local host in tests - part 21

Reviewed-by: chegar
This commit is contained in:
Daniel Fuchs 2019-08-15 12:58:27 +01:00
parent ca919052e9
commit 77d09f0f14
11 changed files with 209 additions and 83 deletions

View File

@ -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 @@ public class TcpKeepAliveTest {
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 @@ public class TcpKeepAliveTest {
}
}
}
private static ServerSocket boundServer(InetAddress address) throws IOException {
var socketAddress = new InetSocketAddress(address, 0);
var server = new ServerSocket();
server.bind(socketAddress);
return server;
}
}

View File

@ -36,6 +36,7 @@ import jdk.test.lib.net.URIBuilder;
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 class SetIfModifiedSince {
}
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");
}
break;
}
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;
}
successfulHeaderCheck = true;
s.close ();
srv.close (); // or else the HTTPURLConnection will retry
} catch (IOException e) {}
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);
}
}
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 @@ public class SetIfModifiedSince {
.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;

View File

@ -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();
}

View File

@ -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);

View File

@ -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));
URL url1 = new URL("http://localhost:" + ss.getLocalPort());
HttpClient c1 = HttpClient.New(url1);
try (ServerSocket toclose = ss) {
Method available = HttpClient.class.
getDeclaredMethod("available", null);
available.setAccessible(true);
URL url1 = URIBuilder.newBuilder()
.scheme("http")
.loopback()
.port(ss.getLocalPort())
.toURL();
c1.setReadTimeout(readTimeout);
boolean a = (boolean) available.invoke(c1);
if (!a) {
throw new RuntimeException("connection should be available");
HttpClient c1 = HttpClient.New(url1);
Method available = HttpClient.class.
getDeclaredMethod("available", null);
available.setAccessible(true);
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");
}
c1.closeServer();
a = (boolean) available.invoke(c1);
if (a) {
throw new RuntimeException("connection shouldn't be available");
}
}
if (c1.getReadTimeout() != readTimeout) {
throw new RuntimeException("read timeout has been altered");
}
c1.closeServer();
a = (boolean) available.invoke(c1);
if (a) {
throw new RuntimeException("connection shouldn't be available");
}
ss.close();
}
}

View File

@ -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 = URIBuilder.newBuilder()
.scheme("http")
.loopback()
.port(ss.getLocalPort())
.toURL();
HttpClient c1 = HttpClient.New(url1);
boolean keepAlive = c1.isKeepingAlive();
}
URL url1 = new URL("http://localhost:" + ss.getLocalPort());
HttpClient c1 = HttpClient.New(url1);
boolean keepAlive = c1.isKeepingAlive();
ss.close();
}
}

View File

@ -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 {

View File

@ -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 class KeepAliveStreamCloseWithWrongContentLength {
}
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 @@ public class KeepAliveStreamCloseWithWrongContentLength {
} catch (NullPointerException e) {
throw new RuntimeException (e);
} finally {
if (serversocket != null) serversocket.close();
serversocket.close();
}
}
}

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.
*
* This code is free software; you can redistribute it and/or modify it
@ -60,7 +60,7 @@ public class StreamingOutputStream
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 @@ public class StreamingOutputStream
* 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();
}

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.
*
* This code is free software; you can redistribute it and/or modify it
@ -81,7 +81,19 @@ public class UserAuth
* 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());

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.
*
* This code is free software; you can redistribute it and/or modify it
@ -78,7 +78,19 @@ public class UserCookie
* 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());