Skip to content

Commit

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

Fixed intermittently failing tests

Backport-of: a974268
  • Loading branch information
GoeLin committed Nov 6, 2023
1 parent 4ebccd6 commit ef475f7
Show file tree
Hide file tree
Showing 11 changed files with 312 additions and 142 deletions.
86 changes: 50 additions & 36 deletions test/jdk/sun/net/InetAddress/nameservice/simple/DefaultCaching.java
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -33,98 +33,112 @@
import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;

public class DefaultCaching {

public static void main(String args[]) throws Exception {

String hostsFileName = System.getProperty("test.src", ".") + "/DefaultCachingHosts";
String hostsFileNameSrc = System.getProperty("test.src", ".") + "/DefaultCachingHosts";
String hostsFileName = System.getProperty("user.dir", ".") + "/DefaultCachingHosts";
if (!hostsFileNameSrc.equals(hostsFileName)) {
Files.copy(Path.of(hostsFileNameSrc), Path.of(hostsFileName), REPLACE_EXISTING);
System.out.println("Host file created: " + hostsFileName);
}
System.setProperty("jdk.net.hosts.file", hostsFileName);
// initial mapping
// name service needs to resolve this.
addMappingToHostsFile("theclub", "129.156.220.219", hostsFileName, false);

test ("theclub", "129.156.220.219", true); // lk: 1
test ("luster", "1.16.20.2", false); // lk: 2
test("theclub", "129.156.220.219", true); // lk: 1
test("luster", "1.16.20.2", false); // lk: 2

// name service now needs to know about luster
addMappingToHostsFile("luster", "10.5.18.21", hostsFileName, true);

test ("luster", "1.16.20.2", false); // lk: 2
sleep (10+1);
test("luster", "1.16.20.2", false); // lk: 2
sleep(10+1);
test("luster", "10.5.18.21", true, 3); // lk: 3
sleep (5);
sleep(5);

// new mapping for theclub and rewrite existing foo and luster mappings
addMappingToHostsFile("theclub", "129.156.220.1", hostsFileName, false);
addMappingToHostsFile("foo", "10.5.18.22", hostsFileName, true);
addMappingToHostsFile("luster", "10.5.18.21", hostsFileName, true);

test ("theclub", "129.156.220.219", true, 3);
test ("luster", "10.5.18.21", true, 3);
test ("bar", "10.5.18.22", false, 4);
test ("foo", "10.5.18.22", true, 5);
test("theclub", "129.156.220.219", true, 3);
test("luster", "10.5.18.21", true, 3);
test("bar", "10.5.18.22", false, 4);
test("foo", "10.5.18.22", true, 5);

// now delay to see if theclub has expired
sleep (5);
sleep(5);

test ("foo", "10.5.18.22", true, 5);
test ("theclub", "129.156.220.1", true, 6);
test("foo", "10.5.18.22", true, 5);
test("theclub", "129.156.220.1", true, 6);

sleep (11);
sleep(11);
// now see if luster has expired
test ("luster", "10.5.18.21", true, 7);
test ("theclub", "129.156.220.1", true, 7);
test("luster", "10.5.18.21", true, 7);
test("theclub", "129.156.220.1", true, 7);

// now delay to see if 3rd has expired
sleep (10+6);
sleep(10+6);

test ("theclub", "129.156.220.1", true, 8);
test ("luster", "10.5.18.21", true, 8);
test ("foo", "10.5.18.22", true, 9);
test("theclub", "129.156.220.1", true, 8);
test("luster", "10.5.18.21", true, 8);
test("foo", "10.5.18.22", true, 9);
}

/* throws RuntimeException if it fails */

static void test (String host, String address,
boolean shouldSucceed, int count) {
test (host, address, shouldSucceed);
static void test(String host, String address,
boolean shouldSucceed, int count) {
test(host, address, shouldSucceed);
}

static void sleep (int seconds) {
static void sleep(int seconds) {
try {
Thread.sleep (seconds * 1000);
Thread.sleep(seconds * 1000);
} catch (InterruptedException e) {}
}

static void test (String host, String address, boolean shouldSucceed) {
static void test(String host, String address, boolean shouldSucceed) {
InetAddress addr = null;
try {
addr = InetAddress.getByName (host);
addr = InetAddress.getByName(host);
if (!shouldSucceed) {
throw new RuntimeException (host+":"+address+": should fail");

throw new RuntimeException(host+":"+address+": should fail (got "
+ addr + ")");
}
if (!address.equals(addr.getHostAddress())) {
throw new RuntimeException(host+":"+address+": compare failed");
throw new RuntimeException(host+":"+address+": compare failed (found "
+ addr + ")");
}
System.out.println("test: " + host + "/" + address
+ " succeeded - got " + addr);
} catch (UnknownHostException e) {
if (shouldSucceed) {
throw new RuntimeException(host+":"+address+": should succeed");
} else {
System.out.println("test: " + host + "/" + address
+ " succeeded - got expected " + e);
}
}
}


private static void addMappingToHostsFile (String host,
String addr,
String hostsFileName,
boolean append)
private static void addMappingToHostsFile(String host,
String addr,
String hostsFileName,
boolean append)
throws Exception {
String mapping = addr + " " + host;
try (PrintWriter hfPWriter = new PrintWriter(new BufferedWriter(
new FileWriter(hostsFileName, append)))) {
hfPWriter.println(mapping);
}
}
}
}
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 @@ -26,6 +26,8 @@
* @library /test/lib
* @bug 4701299
* @summary Keep-Alive-Timer thread management in KeepAliveCache causes memory leak
* @run main KeepAliveTimerThread
* @run main/othervm -Djava.net.preferIPv6Addresses=true KeepAliveTimerThread
*/

import java.net.*;
Expand Down Expand Up @@ -103,8 +105,10 @@ public void run() {


public static void main(String args[]) throws Exception {
ServerSocket ss = new ServerSocket(0);
Server s = new Server (ss);
InetAddress loopback = InetAddress.getLoopbackAddress();
ServerSocket ss = new ServerSocket();
ss.bind(new InetSocketAddress(loopback, 0));
Server s = new Server(ss);
s.start();

URL url = URIBuilder.newBuilder()
Expand Down
18 changes: 15 additions & 3 deletions test/jdk/sun/net/www/http/KeepAliveStream/InfiniteLoop.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
* @bug 8004863
* @modules jdk.httpserver
* @summary Checks for proper close code in KeepAliveStream
* @library /test/lib
* @run main InfiniteLoop
* @run main/othervm -Djava.net.preferIPv6Addresses=true InfiniteLoop
*/

import com.sun.net.httpserver.HttpExchange;
Expand All @@ -35,10 +38,14 @@
import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;
import java.util.concurrent.Phaser;

import jdk.test.lib.net.URIBuilder;

// Racey test, will not always fail, but if it does then we have a problem.

public class InfiniteLoop {
Expand All @@ -49,11 +56,16 @@ public static void main(String[] args) throws Exception {
server.start();
try {
InetSocketAddress address = server.getAddress();
URL url = new URL("http://localhost:" + address.getPort()
+ "/test/InfiniteLoop");
URL url = URIBuilder.newBuilder()
.scheme("http")
.host(server.getAddress().getAddress())
.port(server.getAddress().getPort())
.path("/test/InfiniteLoop")
.toURL();
final Phaser phaser = new Phaser(2);
for (int i=0; i<10; i++) {
HttpURLConnection uc = (HttpURLConnection)url.openConnection();
HttpURLConnection uc = (HttpURLConnection)
url.openConnection(Proxy.NO_PROXY);
final InputStream is = uc.getInputStream();
final Thread thread = new Thread() {
public void run() {
Expand Down
10 changes: 6 additions & 4 deletions test/jdk/sun/net/www/protocol/http/B6369510.java
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -59,11 +59,12 @@ public B6369510()
void doClient() {
try {
InetSocketAddress address = httpServer.getAddress();
String urlString = "http://" + InetAddress.getLocalHost().getHostName() + ":" + address.getPort() + "/test/";
String urlString = "http://" + InetAddress.getLocalHost().getHostName()
+ ":" + address.getPort() + "/test/";
System.out.println("URL == " + urlString);

// GET Request
URL url = new URL("http://" + InetAddress.getLocalHost().getHostName() + ":" + address.getPort() + "/test/");
URL url = new URL(urlString);
HttpURLConnection uc = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);
int resp = uc.getResponseCode();
if (resp != 200)
Expand Down Expand Up @@ -95,7 +96,8 @@ void doClient() {
* Http Server
*/
public void startHttpServer() throws IOException {
httpServer = com.sun.net.httpserver.HttpServer.create(new InetSocketAddress(0), 0);
InetAddress localhost = InetAddress.getLocalHost();
httpServer = HttpServer.create(new InetSocketAddress(localhost, 0), 0);

// create HttpServer context
HttpContext ctx = httpServer.createContext("/test/", new MyHandler());
Expand Down
21 changes: 17 additions & 4 deletions test/jdk/sun/net/www/protocol/http/BasicLongCredentials.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 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 @@ -26,6 +26,9 @@
* @bug 6947917
* @modules jdk.httpserver
* @summary Error in basic authentication when user name and password are long
* @library /test/lib
* @run main BasicLongCredentials
* @run main/othervm -Djava.net.preferIPv6Addresses=true BasicLongCredentials
*/

import com.sun.net.httpserver.BasicAuthenticator;
Expand All @@ -37,11 +40,15 @@
import java.io.InputStream;
import java.io.IOException;
import java.net.Authenticator;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.PasswordAuthentication;
import java.net.Proxy;
import java.net.HttpURLConnection;
import java.net.URL;

import jdk.test.lib.net.URIBuilder;

public class BasicLongCredentials {

static final String USERNAME = "ThisIsMyReallyReallyReallyReallyReallyReally" +
Expand All @@ -51,7 +58,8 @@ public class BasicLongCredentials {
static final String REALM = "foobar@test.realm";

public static void main (String[] args) throws Exception {
HttpServer server = HttpServer.create(new InetSocketAddress(0), 0);
InetAddress loopback = InetAddress.getLoopbackAddress();
HttpServer server = HttpServer.create(new InetSocketAddress(loopback, 0), 0);
try {
Handler handler = new Handler();
HttpContext ctx = server.createContext("/test", handler);
Expand All @@ -66,8 +74,13 @@ public boolean checkCredentials (String username, String pw) {

Authenticator.setDefault(new MyAuthenticator());

URL url = new URL("http://localhost:"+server.getAddress().getPort()+"/test/");
HttpURLConnection urlc = (HttpURLConnection)url.openConnection();
URL url = URIBuilder.newBuilder()
.scheme("http")
.host(server.getAddress().getAddress())
.port(server.getAddress().getPort())
.path("/test/")
.toURL();
HttpURLConnection urlc = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);
InputStream is = urlc.getInputStream();
int c = 0;
while (is.read()!= -1) { c ++; }
Expand Down
Loading

1 comment on commit ef475f7

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