8224035: Replace wildcard address with loopback or local host in tests - part 9

Reviewed-by: dfuchs
This commit is contained in:
Aleksei Efimov 2019-05-27 13:29:11 +01:00
parent 247729cdd7
commit 57d319210a
10 changed files with 101 additions and 43 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2002, 2016, 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. * 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,7 +39,8 @@ public class ThreadStop {
ServerSocket ss; ServerSocket ss;
Server() throws IOException { Server() throws IOException {
ss = new ServerSocket(0); ss = new ServerSocket();
ss.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
} }
public int localPort() { public int localPort() {
@ -81,7 +82,7 @@ public class ThreadStop {
// still in accept() so we connect to server which causes // still in accept() so we connect to server which causes
// it to unblock and do JNI-stuff with a pending exception // it to unblock and do JNI-stuff with a pending exception
try (Socket s = new Socket("localhost", svr.localPort())) { try (Socket s = new Socket(svr.ss.getInetAddress(), svr.localPort())) {
} catch (IOException ioe) { } catch (IOException ioe) {
} }
thr.join(); thr.join();

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. * 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
@ -28,6 +28,8 @@
*/ */
import java.io.InputStream; import java.io.InputStream;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket; import java.net.ServerSocket;
import java.net.Socket; import java.net.Socket;
import java.net.ConnectException; import java.net.ConnectException;
@ -40,12 +42,14 @@ public class Race {
final static int THREADS = 100; final static int THREADS = 100;
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
try (ServerSocket ss = new ServerSocket(0)) { try (ServerSocket ss = new ServerSocket()) {
InetAddress loopback = InetAddress.getLoopbackAddress();
ss.bind(new InetSocketAddress(loopback, 0));
final int port = ss.getLocalPort(); final int port = ss.getLocalPort();
final Phaser phaser = new Phaser(THREADS + 1); final Phaser phaser = new Phaser(THREADS + 1);
for (int i=0; i<100; i++) { for (int i=0; i<100; i++) {
try { try {
final Socket s = new Socket("localhost", port); final Socket s = new Socket(loopback, port);
s.setSoLinger(false, 0); s.setSoLinger(false, 0);
try (Socket sa = ss.accept()) { try (Socket sa = ss.accept()) {
sa.setSoLinger(false, 0); sa.setSoLinger(false, 0);

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. * 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 4636331 * @bug 4636331
* @library /test/lib
* @summary Check that URLClassLoader doesn't create excessive http * @summary Check that URLClassLoader doesn't create excessive http
* connections * connections
*/ */
@ -31,6 +32,8 @@ import java.net.*;
import java.io.*; import java.io.*;
import java.util.*; import java.util.*;
import jdk.test.lib.net.URIBuilder;
public class HttpTest { public class HttpTest {
/* /*
@ -119,7 +122,8 @@ public class HttpTest {
} }
HttpServer() throws Exception { HttpServer() throws Exception {
ss = new ServerSocket(0); ss = new ServerSocket();
ss.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
} }
public void run() { public void run() {
@ -200,9 +204,12 @@ public class HttpTest {
HttpServer svr = HttpServer.create(); HttpServer svr = HttpServer.create();
// create class loader // create class loader
URL urls[] = URL urls[] = {
{ new URL("http://localhost:" + svr.port() + "/dir1/"), URIBuilder.newBuilder().scheme("http").loopback().port(svr.port())
new URL("http://localhost:" + svr.port() + "/dir2/") }; .path("/dir1/").toURL(),
URIBuilder.newBuilder().scheme("http").loopback().port(svr.port())
.path("/dir2/").toURL(),
};
URLClassLoader cl = new URLClassLoader(urls); URLClassLoader cl = new URLClassLoader(urls);
// Test 1 - check that getResource does single HEAD request // Test 1 - check that getResource does single HEAD request

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2001, 2010, 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
@ -67,7 +67,8 @@ public class TimeoutTest {
} }
public void test() throws Exception { public void test() throws Exception {
ServerSocket ss = new ServerSocket(0); ServerSocket ss = new ServerSocket();
ss.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
Server s = new Server (ss); Server s = new Server (ss);
try{ try{
URL url = URIBuilder.newBuilder() URL url = URIBuilder.newBuilder()

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2012, 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. * 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 7129083 * @bug 7129083
* @library /test/lib
* @summary Cookiemanager does not store cookies if url is read * @summary Cookiemanager does not store cookies if url is read
* before setting cookiemanager * before setting cookiemanager
*/ */
@ -31,12 +32,16 @@
import java.net.CookieHandler; import java.net.CookieHandler;
import java.net.CookieManager; import java.net.CookieManager;
import java.net.CookiePolicy; import java.net.CookiePolicy;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket; import java.net.ServerSocket;
import java.net.Socket; import java.net.Socket;
import java.net.URL; import java.net.URL;
import java.io.InputStream; import java.io.InputStream;
import java.io.IOException; import java.io.IOException;
import jdk.test.lib.net.URIBuilder;
public class CookieHttpClientTest implements Runnable { public class CookieHttpClientTest implements Runnable {
final ServerSocket ss; final ServerSocket ss;
static final int TIMEOUT = 10 * 1000; static final int TIMEOUT = 10 * 1000;
@ -85,10 +90,15 @@ public class CookieHttpClientTest implements Runnable {
CookieHttpClientTest() throws Exception { CookieHttpClientTest() throws Exception {
/* start the server */ /* start the server */
ss = new ServerSocket(0); ss = new ServerSocket();
ss.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
(new Thread(this)).start(); (new Thread(this)).start();
URL url = new URL("http://localhost:" + ss.getLocalPort() +"/"); URL url = URIBuilder.newBuilder()
.scheme("http")
.loopback()
.port(ss.getLocalPort())
.path("/").toURL();
// Run without a CookieHandler first // Run without a CookieHandler first
InputStream in = url.openConnection().getInputStream(); InputStream in = url.openConnection().getInputStream();

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2003, 2016, 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
@ -23,6 +23,7 @@
/* @test /* @test
* @bug 4937598 * @bug 4937598
* @library /test/lib
* @summary http://www.clipstream.com video does not play; read() problem * @summary http://www.clipstream.com video does not play; read() problem
*/ */
@ -30,10 +31,14 @@
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.ServerSocket; import java.net.ServerSocket;
import java.net.Socket; import java.net.Socket;
import java.net.URL; import java.net.URL;
import jdk.test.lib.net.URIBuilder;
public class HttpInputStream { public class HttpInputStream {
private static final int CONTENT_LENGTH = 20; private static final int CONTENT_LENGTH = 20;
@ -45,7 +50,9 @@ public class HttpInputStream {
static final int TIMEOUT = 10 * 1000; static final int TIMEOUT = 10 * 1000;
Server() throws IOException { Server() throws IOException {
serverSocket = new ServerSocket(0); serverSocket = new ServerSocket();
serverSocket.bind(new InetSocketAddress(
InetAddress.getLoopbackAddress(), 0));
} }
void readOneRequest(InputStream is) throws IOException { void readOneRequest(InputStream is) throws IOException {
@ -106,7 +113,12 @@ public class HttpInputStream {
public static void main(String args[]) throws IOException { public static void main(String args[]) throws IOException {
try (Server server = new Server()) { try (Server server = new Server()) {
(new Thread(server)).start(); (new Thread(server)).start();
URL url = new URL("http://localhost:" + server.getPort() + "/anything"); URL url = URIBuilder.newBuilder()
.scheme("http")
.loopback()
.port(server.getPort())
.path("/anything")
.toURLUnchecked();
try (InputStream is = url.openConnection().getInputStream()) { try (InputStream is = url.openConnection().getInputStream()) {
if (read(is) != CONTENT_LENGTH) { if (read(is) != CONTENT_LENGTH) {
throw new RuntimeException("HttpInputStream.read() failed with 0xff"); throw new RuntimeException("HttpInputStream.read() failed with 0xff");

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2013, 2016, 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. * 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 8011719 * @bug 8011719
* @library /test/lib
* @modules jdk.httpserver * @modules jdk.httpserver
* @summary Basic checks to verify behavior of returned input streams * @summary Basic checks to verify behavior of returned input streams
*/ */
@ -36,6 +37,8 @@ import java.net.*;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.*; import java.util.*;
import jdk.test.lib.net.URIBuilder;
public class HttpStreams { public class HttpStreams {
void client(String u) throws Exception { void client(String u) throws Exception {
@ -56,24 +59,33 @@ public class HttpStreams {
expectThrow(() -> { is.read(ba, 0, 2); }, "read on closed stream should throw: " + u); expectThrow(() -> { is.read(ba, 0, 2); }, "read on closed stream should throw: " + u);
} }
String constructUrlString(int port, String path) throws Exception {
return URIBuilder.newBuilder()
.scheme("http")
.port(port)
.loopback()
.path(path)
.toURL().toString();
}
void test() throws Exception { void test() throws Exception {
HttpServer server = null; HttpServer server = null;
try { try {
server = startHttpServer(); server = startHttpServer();
String baseUrl = "http://localhost:" + server.getAddress().getPort() + "/"; int serverPort = server.getAddress().getPort();
client(baseUrl + "chunked/"); client(constructUrlString(serverPort, "/chunked/"));
client(baseUrl + "fixed/"); client(constructUrlString(serverPort, "/fixed/"));
client(baseUrl + "error/"); client(constructUrlString(serverPort, "/error/"));
client(baseUrl + "chunkedError/"); client(constructUrlString(serverPort, "/chunkedError/"));
// Test with a response cache // Test with a response cache
ResponseCache ch = ResponseCache.getDefault(); ResponseCache ch = ResponseCache.getDefault();
ResponseCache.setDefault(new TrivialCacheHandler()); ResponseCache.setDefault(new TrivialCacheHandler());
try { try {
client(baseUrl + "chunked/"); client(constructUrlString(serverPort, "/chunked/"));
client(baseUrl + "fixed/"); client(constructUrlString(serverPort, "/fixed/"));
client(baseUrl + "error/"); client(constructUrlString(serverPort, "/error/"));
client(baseUrl + "chunkedError/"); client(constructUrlString(serverPort, "/chunkedError/"));
} finally { } finally {
ResponseCache.setDefault(ch); ResponseCache.setDefault(ch);
} }
@ -93,7 +105,8 @@ public class HttpStreams {
// HTTP Server // HTTP Server
HttpServer startHttpServer() throws IOException { HttpServer startHttpServer() throws IOException {
HttpServer httpServer = HttpServer.create(new InetSocketAddress(0), 0); HttpServer httpServer = HttpServer.create();
httpServer.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), 0);
httpServer.createContext("/chunked/", new ChunkedHandler()); httpServer.createContext("/chunked/", new ChunkedHandler());
httpServer.createContext("/fixed/", new FixedHandler()); httpServer.createContext("/fixed/", new FixedHandler());
httpServer.createContext("/error/", new ErrorHandler()); httpServer.createContext("/error/", new ErrorHandler());

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2013, 2018, 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. * 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
@ -165,7 +165,8 @@ public class RedirectOnPost {
private static HttpServer getHttpServer(ExecutorService execs) private static HttpServer getHttpServer(ExecutorService execs)
throws Exception throws Exception
{ {
InetSocketAddress inetAddress = new InetSocketAddress(0); InetSocketAddress inetAddress = new InetSocketAddress(
InetAddress.getLoopbackAddress(), 0);
HttpServer testServer = HttpServer.create(inetAddress, 15); HttpServer testServer = HttpServer.create(inetAddress, 15);
int port = testServer.getAddress().getPort(); int port = testServer.getAddress().getPort();
testServer.setExecutor(execs); testServer.setExecutor(execs);
@ -180,7 +181,8 @@ public class RedirectOnPost {
) )
throws Exception throws Exception
{ {
InetSocketAddress inetAddress = new InetSocketAddress(0); InetSocketAddress inetAddress = new InetSocketAddress(
InetAddress.getLoopbackAddress(), 0);
HttpsServer testServer = HttpsServer.create(inetAddress, 15); HttpsServer testServer = HttpsServer.create(inetAddress, 15);
int port = testServer.getAddress().getPort(); int port = testServer.getAddress().getPort();
testServer.setExecutor(execs); testServer.setExecutor(execs);

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2004, 2012, 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. * 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
@ -67,7 +67,8 @@ public class SetChunkedStreamingMode implements HttpCallback {
public static void main (String[] args) throws Exception { public static void main (String[] args) throws Exception {
try { try {
server = new TestHttpServer (new SetChunkedStreamingMode(), 1, 10, 0); server = new TestHttpServer(new SetChunkedStreamingMode(), 1, 10,
InetAddress.getLoopbackAddress(), 0);
System.out.println ("Server: listening on port: " + server.getLocalPort()); System.out.println ("Server: listening on port: " + server.getLocalPort());
URL url = URIBuilder.newBuilder() URL url = URIBuilder.newBuilder()
.scheme("http") .scheme("http")

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 1999, 2000, 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. * 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
@ -22,13 +22,15 @@
*/ */
/* @test /* @test
@bug 4213164 8172253 * @bug 4213164 8172253
@summary setIfModifiedSince mehtod in HttpURLConnection sometimes fails * @library /test/lib
* @summary setIfModifiedSince method in HttpURLConnection sometimes fails
*/ */
import java.util.*; import java.util.*;
import java.io.*; import java.io.*;
import java.net.*; import java.net.*;
import java.text.*;
import jdk.test.lib.net.URIBuilder;
public class SetIfModifiedSince implements Runnable { public class SetIfModifiedSince implements Runnable {
@ -75,7 +77,8 @@ public class SetIfModifiedSince implements Runnable {
public SetIfModifiedSince() throws Exception { public SetIfModifiedSince() throws Exception {
serverSock = new ServerSocket(0); serverSock = new ServerSocket();
serverSock.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
int port = serverSock.getLocalPort(); int port = serverSock.getLocalPort();
Thread thr = new Thread(this); Thread thr = new Thread(this);
@ -86,8 +89,12 @@ public class SetIfModifiedSince implements Runnable {
HttpURLConnection con; HttpURLConnection con;
//url = new URL(args[0]); //url = new URL(args[0]);
url = new URL("http://localhost:" + String.valueOf(port) + url = URIBuilder.newBuilder()
"/anything"); .scheme("http")
.loopback()
.port(port)
.path("/anything")
.toURL();
con = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY); con = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);
con.setIfModifiedSince(date.getTime()); con.setIfModifiedSince(date.getTime());