Skip to content

Commit

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

Replaces binding to wildacard with alternative less susceptible to intermittent failure in some intermittently failing tests.

Reviewed-by: mbaesken, phh
Backport-of: 7d4520c
  • Loading branch information
GoeLin committed Nov 6, 2023
1 parent d0045da commit 4ebccd6
Show file tree
Hide file tree
Showing 22 changed files with 154 additions and 69 deletions.
16 changes: 12 additions & 4 deletions test/jdk/com/sun/net/httpserver/bugs/B6361557.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ public void handle (HttpExchange t)

public static void main (String[] args) throws Exception {
Handler handler = new Handler();
InetSocketAddress addr = new InetSocketAddress (0);
InetAddress loopback = InetAddress.getLoopbackAddress();
InetSocketAddress addr = new InetSocketAddress (loopback, 0);
HttpServer server = HttpServer.create (addr, 0);
HttpContext ctx = server.createContext ("/test", handler);

Expand All @@ -78,15 +79,18 @@ public static void main (String[] args) throws Exception {
server.start ();

InetSocketAddress destaddr = new InetSocketAddress (
InetAddress.getLoopbackAddress(), server.getAddress().getPort()
loopback, server.getAddress().getPort()
);
System.out.println ("destaddr " + destaddr);

Selector selector = Selector.open ();
int requests = 0;
int responses = 0;
while (true) {
int selres = selector.select (1);
// we need to read responses from time to time: slightly
// increase the timeout with the amount of pending responses
// to give a chance to the server to reply.
int selres = selector.select (requests - responses + 1);
Set<SelectionKey> selkeys = selector.selectedKeys();
for (SelectionKey key : selkeys) {
if (key.isReadable()) {
Expand All @@ -95,14 +99,18 @@ public static void main (String[] args) throws Exception {
try {
int x = chan.read(buf);
if (x == -1 || responseComplete(buf)) {
System.out.print("_");
key.attach(null);
chan.close();
responses++;
}
} catch (IOException e) {}
} catch (IOException e) {
System.out.println(e);
}
}
}
if (requests < NUM) {
System.out.print(".");
SocketChannel schan = SocketChannel.open(destaddr);
requestBuf.rewind();
int c = 0;
Expand Down
15 changes: 8 additions & 7 deletions test/jdk/java/net/Authenticator/B4722333.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2002, 2012, 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 @@ -121,13 +121,14 @@ public static void main (String[] args) throws Exception {
MyAuthenticator auth = new MyAuthenticator ();
Authenticator.setDefault (auth);
try {
server = new TestHttpServer (new B4722333(), 1, 10, 0);
InetAddress loopback = InetAddress.getLoopbackAddress();
server = new TestHttpServer (new B4722333(), 1, 10, loopback, 0);
System.out.println ("Server started: listening on port: " + server.getLocalPort());
client ("http://localhost:"+server.getLocalPort()+"/d1/d2/d3/foo.html");
client ("http://localhost:"+server.getLocalPort()+"/ASD/d3/x.html");
client ("http://localhost:"+server.getLocalPort()+"/biz/d3/x.html");
client ("http://localhost:"+server.getLocalPort()+"/bar/d3/x.html");
client ("http://localhost:"+server.getLocalPort()+"/fuzz/d3/x.html");
client ("http://" + server.getAuthority() + "/d1/d2/d3/foo.html");
client ("http://" + server.getAuthority() + "/ASD/d3/x.html");
client ("http://" + server.getAuthority() + "/biz/d3/x.html");
client ("http://" + server.getAuthority() + "/bar/d3/x.html");
client ("http://" + server.getAuthority() + "/fuzz/d3/x.html");
} catch (Exception e) {
if (server != null) {
server.terminate();
Expand Down
16 changes: 12 additions & 4 deletions test/jdk/java/net/HttpURLConnection/UnmodifiableMaps.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,11 +24,13 @@
/**
* @test
* @bug 7128648
* @library /test/lib
* @modules jdk.httpserver
* @summary HttpURLConnection.getHeaderFields should return an unmodifiable Map
*/

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.URI;
import java.net.HttpURLConnection;
Expand All @@ -41,14 +43,20 @@
import com.sun.net.httpserver.HttpServer;
import com.sun.net.httpserver.Headers;
import static java.net.Proxy.NO_PROXY;
import jdk.test.lib.net.URIBuilder;

public class UnmodifiableMaps {

void test(String[] args) throws Exception {
HttpServer server = startHttpServer();
try {
InetSocketAddress address = server.getAddress();
URI uri = new URI("http://localhost:" + address.getPort() + "/foo");
URI uri = URIBuilder.newBuilder()
.scheme("http")
.host(address.getAddress())
.port(address.getPort())
.path("/foo")
.build();
doClient(uri);
} finally {
server.stop(0);
Expand Down Expand Up @@ -78,7 +86,8 @@ void doClient(URI uri) throws Exception {

// HTTP Server
HttpServer startHttpServer() throws IOException {
HttpServer httpServer = HttpServer.create(new InetSocketAddress(0), 0);
InetAddress loopback = InetAddress.getLoopbackAddress();
HttpServer httpServer = HttpServer.create(new InetSocketAddress(loopback, 0), 0);
httpServer.createContext("/foo", new SimpleHandler());
httpServer.start();
return httpServer;
Expand Down Expand Up @@ -146,4 +155,3 @@ public void instanceMain(String[] args) throws Throwable {
System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
if (failed > 0) throw new AssertionError("Some tests failed");}
}

19 changes: 14 additions & 5 deletions test/jdk/java/net/ResponseCache/ResponseCacheTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2014, 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 @@ -24,13 +24,15 @@
/* @test
* @summary Unit test for java.net.ResponseCache
* @bug 4837267
* @library /test/lib
* @author Yingxian Wang
*/

import java.net.*;
import java.util.*;
import java.io.*;
import javax.net.ssl.*;
import jdk.test.lib.net.URIBuilder;

/**
* Request should get serviced by the cache handler. Response get
Expand Down Expand Up @@ -90,14 +92,17 @@ public void run() {
try { fis.close(); } catch (IOException unused) {}
}
}
static class NameVerifier implements HostnameVerifier {
static class NameVerifier implements HostnameVerifier {
public boolean verify(String hostname, SSLSession session) {
return true;
}
}
ResponseCacheTest() throws Exception {
/* start the server */
ss = new ServerSocket(0);
InetAddress loopback = InetAddress.getLoopbackAddress();
ss = new ServerSocket();
ss.bind(new InetSocketAddress(loopback, 0));

(new Thread(this)).start();
/* establish http connection to server */
url1 = new URL("http://localhost/file1.cache");
Expand Down Expand Up @@ -126,8 +131,12 @@ public boolean verify(String hostname, SSLSession session) {
http.disconnect();

// testing ResponseCacheHandler.put()
url2 = new URL("http://localhost:" +
Integer.toString(ss.getLocalPort())+"/file2.1");
url2 = URIBuilder.newBuilder()
.scheme("http")
.host(ss.getInetAddress())
.port(ss.getLocalPort())
.path("/file2.1")
.toURL();
http = (HttpURLConnection)url2.openConnection();
System.out.println("responsecode2 is :"+http.getResponseCode());
Map<String,List<String>> headers2 = http.getHeaderFields();
Expand Down
5 changes: 3 additions & 2 deletions test/jdk/java/net/Socket/GetLocalAddress.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1998, 2013, 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
Expand Down Expand Up @@ -45,7 +45,8 @@ public static void main(String args[]) throws Exception {
int linger = 65546;
int value = 0;
addr = InetAddress.getLocalHost();
ss = new ServerSocket(0);
ss = new ServerSocket();
ss.bind(new InetSocketAddress(addr, 0));
port = ss.getLocalPort();

Thread t = new Thread(new GetLocalAddress());
Expand Down
10 changes: 7 additions & 3 deletions test/jdk/java/net/Socket/SetReceiveBufferSize.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 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.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.ServerSocket;

Expand All @@ -37,8 +39,10 @@ public static void main(String[] args) throws Exception {
}

public SetReceiveBufferSize() throws Exception {
ServerSocket ss = new ServerSocket(0);
Socket s = new Socket("localhost", ss.getLocalPort());
ServerSocket ss = new ServerSocket();
InetAddress loopback = InetAddress.getLoopbackAddress();
ss.bind(new InetSocketAddress(loopback, 0));
Socket s = new Socket(loopback, ss.getLocalPort());
Socket accepted = ss.accept();
try {
s.setReceiveBufferSize(0);
Expand Down
5 changes: 3 additions & 2 deletions test/jdk/java/net/Socket/SoTimeout.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1998, 2010, 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
Expand Down Expand Up @@ -44,7 +44,8 @@ public class SoTimeout implements Runnable {

public static void main(String[] args) throws Exception {
addr = InetAddress.getLocalHost();
serverSocket = new ServerSocket(0);
serverSocket = new ServerSocket();
serverSocket.bind(new InetSocketAddress(addr, 0));
port = serverSocket.getLocalPort();

byte[] b = new byte[12];
Expand Down
7 changes: 4 additions & 3 deletions test/jdk/java/net/Socket/TestAfterClose.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2007, 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,8 +39,9 @@ public class TestAfterClose

public static void main(String[] args) {
try {
ServerSocket ss = new ServerSocket(0, 0, null);
Socket socket = new Socket("localhost", ss.getLocalPort());
InetAddress loopback = InetAddress.getLoopbackAddress();
ServerSocket ss = new ServerSocket(0, 0, loopback);
Socket socket = new Socket(loopback, ss.getLocalPort());
ss.accept();
ss.close();
test(socket);
Expand Down
6 changes: 4 additions & 2 deletions test/jdk/java/net/Socket/UrgentDataTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,12 @@ public static void main (String args[]) {
try {
UrgentDataTest test = new UrgentDataTest ();
if (args.length == 0) {
test.listener = new ServerSocket (0);
InetAddress loopback = InetAddress.getLoopbackAddress();
test.listener = new ServerSocket ();
test.listener.bind(new InetSocketAddress(loopback, 0));
test.isClient = true;
test.isServer = true;
test.clHost = InetAddress.getLoopbackAddress().getHostAddress();
test.clHost = loopback.getHostAddress();
test.clPort = test.listener.getLocalPort();
test.run();
} else if (args[0].equals ("-server")) {
Expand Down
2 changes: 1 addition & 1 deletion test/jdk/java/net/SocketOption/OptionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ static NetworkInterface getNetworkInterface() {

static void doSocketTests() throws Exception {
try (
ServerSocket srv = new ServerSocket(0);
ServerSocket srv = new ServerSocket(0, 50, InetAddress.getLoopbackAddress());
Socket c = new Socket(InetAddress.getLoopbackAddress(), srv.getLocalPort());
Socket s = srv.accept();
) {
Expand Down
14 changes: 9 additions & 5 deletions test/jdk/java/net/URL/GetContent.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1998, 2010, 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
Expand All @@ -24,11 +24,13 @@
/**
* @test
* @bug 4145315
* @library /test/lib
* @summary Test a read from nonexistant URL
*/

import java.net.*;
import java.io.*;
import jdk.test.lib.net.URIBuilder;

public class GetContent implements Runnable {

Expand Down Expand Up @@ -71,10 +73,12 @@ public void run() {

boolean error = true;
try {
String name = "http://localhost:" + ss.getLocalPort() +
"/no-such-name";
java.net.URL url = null;
url = new java.net.URL(name);
java.net.URL url = URIBuilder.newBuilder()
.scheme("http")
.host(ss.getInetAddress())
.port(ss.getLocalPort())
.path("/no-such-name")
.toURL();
Object obj = url.getContent();
InputStream in = (InputStream) obj;
byte buff[] = new byte[200];
Expand Down
7 changes: 4 additions & 3 deletions test/jdk/java/net/URLConnection/B5052093.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2007, 2012, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2007, 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 @@ -64,9 +64,10 @@ public void request(HttpTransaction req) {
}

public static void main(String[] args) throws Exception {
server = new TestHttpServer(new B5052093(), 1, 10, 0);
InetAddress loopback = InetAddress.getLoopbackAddress();
server = new TestHttpServer(new B5052093(), 1, 10, loopback, 0);
try {
URL url = new URL("http://localhost:"+server.getLocalPort()+"/foo");
URL url = new URL("http://" + server.getAuthority() + "/foo");
URLConnection conn = url.openConnection();
int i = conn.getContentLength();
long l = conn.getContentLengthLong();
Expand Down
10 changes: 7 additions & 3 deletions test/jdk/java/net/URLPermission/nstest/LookupTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2017, 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 @@ -36,6 +36,8 @@
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.NetPermission;
import java.net.ProxySelector;
import java.net.ServerSocket;
Expand Down Expand Up @@ -104,7 +106,7 @@ public static void main(String args[]) throws Exception {
String hostsFileName = CWD + "/LookupTestHosts";
System.setProperty("jdk.net.hosts.file", hostsFileName);
addMappingToHostsFile("allowedAndFound.com",
"127.0.0.1",
InetAddress.getLoopbackAddress().getHostAddress(),
hostsFileName,
false);
addMappingToHostsFile("notAllowedButFound.com",
Expand All @@ -131,7 +133,9 @@ static class Server extends Thread {
private volatile boolean done;

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

Expand Down
Loading

1 comment on commit 4ebccd6

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