Skip to content

Commit

Permalink
8224035: Replace wildcard address with loopback or local host in test…
Browse files Browse the repository at this point in the history
…s - part 9

Backport-of: 57d3192
  • Loading branch information
Andrew Lu authored and GoeLin committed Nov 28, 2023
1 parent f9bedac commit bd7420a
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 43 deletions.
7 changes: 4 additions & 3 deletions test/jdk/java/net/ServerSocket/ThreadStop.java
Original file line number Diff line number Diff line change
@@ -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.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -39,7 +39,8 @@ static class Server implements Runnable {
ServerSocket ss;

Server() throws IOException {
ss = new ServerSocket(0);
ss = new ServerSocket();
ss.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
}

public int localPort() {
Expand Down Expand Up @@ -81,7 +82,7 @@ public static void main(String args[]) throws Exception {
// still in accept() so we connect to server which causes
// 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) {
}
thr.join();
Expand Down
10 changes: 7 additions & 3 deletions test/jdk/java/net/Socket/asyncClose/Race.java
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -28,6 +28,8 @@
*/

import java.io.InputStream;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.ConnectException;
Expand All @@ -40,12 +42,14 @@ public class Race {
final static int THREADS = 100;

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 Phaser phaser = new Phaser(THREADS + 1);
for (int i=0; i<100; i++) {
try {
final Socket s = new Socket("localhost", port);
final Socket s = new Socket(loopback, port);
s.setSoLinger(false, 0);
try (Socket sa = ss.accept()) {
sa.setSoLinger(false, 0);
Expand Down
17 changes: 12 additions & 5 deletions test/jdk/java/net/URLClassLoader/HttpTest.java
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -24,13 +24,16 @@
/**
* @test
* @bug 4636331
* @library /test/lib
* @summary Check that URLClassLoader doesn't create excessive http
* connections
*/
import java.net.*;
import java.io.*;
import java.util.*;

import jdk.test.lib.net.URIBuilder;

public class HttpTest {

/*
Expand Down Expand Up @@ -119,7 +122,8 @@ public void run() {
}

HttpServer() throws Exception {
ss = new ServerSocket(0);
ss = new ServerSocket();
ss.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
}

public void run() {
Expand Down Expand Up @@ -200,9 +204,12 @@ public static void main(String args[]) throws Exception {
HttpServer svr = HttpServer.create();

// create class loader
URL urls[] =
{ new URL("http://localhost:" + svr.port() + "/dir1/"),
new URL("http://localhost:" + svr.port() + "/dir2/") };
URL urls[] = {
URIBuilder.newBuilder().scheme("http").loopback().port(svr.port())
.path("/dir1/").toURL(),
URIBuilder.newBuilder().scheme("http").loopback().port(svr.port())
.path("/dir2/").toURL(),
};
URLClassLoader cl = new URLClassLoader(urls);

// Test 1 - check that getResource does single HEAD request
Expand Down
5 changes: 3 additions & 2 deletions test/jdk/java/net/URLConnection/TimeoutTest.java
Original file line number Diff line number Diff line change
@@ -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.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -67,7 +67,8 @@ public static void main(String[] args) 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);
try{
URL url = URIBuilder.newBuilder()
Expand Down
16 changes: 13 additions & 3 deletions test/jdk/sun/net/www/http/HttpClient/CookieHttpClientTest.java
Original file line number Diff line number Diff line change
@@ -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.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -24,19 +24,24 @@
/*
* @test
* @bug 7129083
* @library /test/lib
* @summary Cookiemanager does not store cookies if url is read
* before setting cookiemanager
*/

import java.net.CookieHandler;
import java.net.CookieManager;
import java.net.CookiePolicy;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.URL;
import java.io.InputStream;
import java.io.IOException;

import jdk.test.lib.net.URIBuilder;

public class CookieHttpClientTest implements Runnable {
final ServerSocket ss;
static final int TIMEOUT = 10 * 1000;
Expand Down Expand Up @@ -85,10 +90,15 @@ static void readOneRequest(InputStream is) throws IOException {

CookieHttpClientTest() throws Exception {
/* start the server */
ss = new ServerSocket(0);
ss = new ServerSocket();
ss.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
(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
InputStream in = url.openConnection().getInputStream();
Expand Down
18 changes: 15 additions & 3 deletions test/jdk/sun/net/www/protocol/http/HttpInputStream.java
Original file line number Diff line number Diff line change
@@ -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.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -23,17 +23,22 @@

/* @test
* @bug 4937598
* @library /test/lib
* @summary http://www.clipstream.com video does not play; read() problem
*/


import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.URL;

import jdk.test.lib.net.URIBuilder;

public class HttpInputStream {

private static final int CONTENT_LENGTH = 20;
Expand All @@ -45,7 +50,9 @@ static class Server implements AutoCloseable, Runnable {
static final int TIMEOUT = 10 * 1000;

Server() throws IOException {
serverSocket = new ServerSocket(0);
serverSocket = new ServerSocket();
serverSocket.bind(new InetSocketAddress(
InetAddress.getLoopbackAddress(), 0));
}

void readOneRequest(InputStream is) throws IOException {
Expand Down Expand Up @@ -106,7 +113,12 @@ private static int read(InputStream is) throws IOException {
public static void main(String args[]) throws IOException {
try (Server server = new Server()) {
(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()) {
if (read(is) != CONTENT_LENGTH) {
throw new RuntimeException("HttpInputStream.read() failed with 0xff");
Expand Down
35 changes: 24 additions & 11 deletions test/jdk/sun/net/www/protocol/http/HttpStreams.java
Original file line number Diff line number Diff line change
@@ -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.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -24,6 +24,7 @@
/**
* @test
* @bug 8011719
* @library /test/lib
* @modules jdk.httpserver
* @summary Basic checks to verify behavior of returned input streams
*/
Expand All @@ -36,6 +37,8 @@
import java.nio.charset.StandardCharsets;
import java.util.*;

import jdk.test.lib.net.URIBuilder;

public class HttpStreams {

void client(String u) throws Exception {
Expand All @@ -56,24 +59,33 @@ void client(String u) throws Exception {
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 {
HttpServer server = null;
try {
server = startHttpServer();
String baseUrl = "http://localhost:" + server.getAddress().getPort() + "/";
client(baseUrl + "chunked/");
client(baseUrl + "fixed/");
client(baseUrl + "error/");
client(baseUrl + "chunkedError/");
int serverPort = server.getAddress().getPort();
client(constructUrlString(serverPort, "/chunked/"));
client(constructUrlString(serverPort, "/fixed/"));
client(constructUrlString(serverPort, "/error/"));
client(constructUrlString(serverPort, "/chunkedError/"));

// Test with a response cache
ResponseCache ch = ResponseCache.getDefault();
ResponseCache.setDefault(new TrivialCacheHandler());
try {
client(baseUrl + "chunked/");
client(baseUrl + "fixed/");
client(baseUrl + "error/");
client(baseUrl + "chunkedError/");
client(constructUrlString(serverPort, "/chunked/"));
client(constructUrlString(serverPort, "/fixed/"));
client(constructUrlString(serverPort, "/error/"));
client(constructUrlString(serverPort, "/chunkedError/"));
} finally {
ResponseCache.setDefault(ch);
}
Expand All @@ -93,7 +105,8 @@ public static void main(String[] args) throws Exception {

// HTTP Server
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("/fixed/", new FixedHandler());
httpServer.createContext("/error/", new ErrorHandler());
Expand Down
8 changes: 5 additions & 3 deletions test/jdk/sun/net/www/protocol/http/RedirectOnPost.java
Original file line number Diff line number Diff line change
@@ -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.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -166,7 +166,8 @@ public void handle(HttpExchange msg) {
private static HttpServer getHttpServer(ExecutorService execs)
throws Exception
{
InetSocketAddress inetAddress = new InetSocketAddress(0);
InetSocketAddress inetAddress = new InetSocketAddress(
InetAddress.getLoopbackAddress(), 0);
HttpServer testServer = HttpServer.create(inetAddress, 15);
int port = testServer.getAddress().getPort();
testServer.setExecutor(execs);
Expand All @@ -181,7 +182,8 @@ private static HttpsServer getHttpsServer(
)
throws Exception
{
InetSocketAddress inetAddress = new InetSocketAddress(0);
InetSocketAddress inetAddress = new InetSocketAddress(
InetAddress.getLoopbackAddress(), 0);
HttpsServer testServer = HttpsServer.create(inetAddress, 15);
int port = testServer.getAddress().getPort();
testServer.setExecutor(execs);
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -67,7 +67,8 @@ static void read (InputStream is) throws IOException {

public static void main (String[] args) throws Exception {
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());
URL url = URIBuilder.newBuilder()
.scheme("http")
Expand Down

1 comment on commit bd7420a

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.