Skip to content

Commit

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

Fixes some intermittent test failures by replacing wildcard with loopback - or retrying once.

Backport-of: cd9e3c1
  • Loading branch information
GoeLin committed Jun 7, 2023
1 parent 7a35741 commit e3dd9dd
Show file tree
Hide file tree
Showing 13 changed files with 198 additions and 65 deletions.
44 changes: 32 additions & 12 deletions test/jdk/com/sun/net/httpserver/SimpleHttpServerTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2014, 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,8 +24,12 @@
/**
* @test
* @bug 8015692
* @key intermittent
* @summary Test HttpServer instantiation, start, and stop repeated in a loop
* Testing for Bind exception on Windows
* Testing for Bind exception on Windows. This test may fail
* intermittently if other tests / process manage to bind to
* the same port that the test is using in the short window
* time where the port might appear available again.
*/

import java.net.InetSocketAddress;
Expand All @@ -41,24 +45,40 @@ public static void main(String[] args) throws Exception {
System.out.println(System.getProperty("java.version"));
InetSocketAddress serverAddr = new InetSocketAddress(0);
HttpServer server = HttpServer.create(serverAddr, 0);
final int serverPort = server.getAddress().getPort();
int serverPort = server.getAddress().getPort();
server.start();
server.stop(0);
serverAddr = new InetSocketAddress(serverPort);
int exceptionCount = 0;
boolean failedOnce = false;
System.out.println("Using serverPort == " + serverPort);
for (int i = 0; i < 100; i++) {
try {
server = HttpServer.create(serverAddr, 0);
server.start();
server.stop(0);
} catch (Exception ex) {
ex.printStackTrace();
exceptionCount++;
RETRY: while (exceptionCount == 0) {
for (int i = 0; i < 100; i++) {
try {
server = HttpServer.create(serverAddr, 0);
server.start();
server.stop(0);
} catch (Exception ex) {
if (!failedOnce) {
failedOnce = true;
server = HttpServer.create(new InetSocketAddress(0), 0);
serverPort = server.getAddress().getPort();
server.start();
server.stop(0);
serverAddr = new InetSocketAddress(serverPort);
System.out.println("Retrying with serverPort == " + serverPort);
continue RETRY;
}
System.err.println("Got exception at iteration: " + i );
ex.printStackTrace();
exceptionCount++;
}
}
break;
}
if (exceptionCount > 0) {
throw new RuntimeException("Test Failed");
throw new RuntimeException("Test Failed: got "
+ exceptionCount + " exceptions.");
}
}
}
37 changes: 32 additions & 5 deletions test/jdk/java/net/BindException/Test.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2001, 2016, 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 @@ -45,18 +45,24 @@ static Object[][] getTestCombinations() {

static int count;
static int failures;
static boolean retried;

static void doTest(Object test[], InetAddress ia1, InetAddress ia2,
boolean silent) throws Exception {
String s1_type = (String)test[0];
String s2_type = (String)test[1];
int port = 0;

/*
* Increment test count
*/
count++;

doTest(test, count, ia1, ia2, silent, !retried);
}

static void doTest(Object test[], int count, InetAddress ia1, InetAddress ia2,
boolean silent, boolean retry) throws Exception {
String s1_type = (String)test[0];
String s2_type = (String)test[1];
int port = 0;

/*
* Do the test
*/
Expand All @@ -68,6 +74,8 @@ static void doTest(Object test[], InetAddress ia1, InetAddress ia2,
Socket sock1 = null;
ServerSocket ss = null;
DatagramSocket dsock1 = null;
boolean firstBound = false;

try {
/* bind the first socket */

Expand All @@ -89,6 +97,13 @@ static void doTest(Object test[], InetAddress ia1, InetAddress ia2,

/* bind the second socket */

// The fact that the port was available for ia1 does not
// guarantee that it will also be available for ia2 as something
// else might already be bound to that port.
// For the sake of test stability we will retry once in
// case of unexpected bind exception.

firstBound = true;
if (s2_type.equals("Socket")) {
try (Socket sock2 = new Socket()) {
sock2.bind( new InetSocketAddress(ia2, port));
Expand Down Expand Up @@ -141,6 +156,18 @@ static void doTest(Object test[], InetAddress ia1, InetAddress ia2,
return;
}

if (failed && retry && firstBound) {
// retry once at the first failure only
retried = true;
if (!silent) {
System.out.println("");
System.out.println("**************************");
System.out.println("Test " + count + ": Retrying...");
}
doTest(test, count, ia1, ia2, silent, false);
return;
}

if (failed || !silent) {
System.out.println("");
System.out.println("**************************");
Expand Down
8 changes: 5 additions & 3 deletions test/jdk/java/net/PlainSocketImpl/SetOption.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 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 @@ -35,8 +35,10 @@ public class SetOption {

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

ServerSocket ss = new ServerSocket(0);
Socket s1 = new Socket("localhost", ss.getLocalPort());
InetAddress loopback = InetAddress.getLoopbackAddress();
ServerSocket ss = new ServerSocket(0, 0, loopback);

Socket s1 = new Socket(loopback, ss.getLocalPort());
Socket s2 = ss.accept();

s1.close();
Expand Down
8 changes: 5 additions & 3 deletions test/jdk/java/net/Socket/RST.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2001, 2018, 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 @@ -44,8 +44,10 @@ public void run() {
}

RST() throws Exception {
ServerSocket ss = new ServerSocket(0);
client = new Socket("localhost", ss.getLocalPort());
InetAddress loopback = InetAddress.getLoopbackAddress();
ServerSocket ss = new ServerSocket();
ss.bind(new InetSocketAddress(loopback, 0));
client = new Socket(loopback, ss.getLocalPort());
Socket server = ss.accept();

Thread thr = new Thread(this);
Expand Down
28 changes: 19 additions & 9 deletions test/jdk/java/net/URLConnection/URLConnectionHeaders.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 @@ -27,12 +27,14 @@
* @summary URLConnection cannot enumerate request properties,
* and URLConnection can neither get nor set multiple
* request properties w/ same key
* @library /test/lib
*
*/

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

public class URLConnectionHeaders {

Expand Down Expand Up @@ -77,15 +79,22 @@ public void run() {
}
}

public static void main(String[] args) {
public static void main(String[] args) throws Exception {
try {
ServerSocket serversocket = new ServerSocket (0);
int port = serversocket.getLocalPort ();
XServer server = new XServer (serversocket);
server.start ();
Thread.sleep (200);
URL url = new URL ("http://localhost:"+port+"/index.html");
URLConnection uc = url.openConnection ();
InetAddress loopback = InetAddress.getLoopbackAddress();
ServerSocket serversocket = new ServerSocket();
serversocket.bind(new InetSocketAddress(loopback, 0));
int port = serversocket.getLocalPort();
XServer server = new XServer(serversocket);
server.start();
Thread.sleep(200);
URL url = URIBuilder.newBuilder()
.scheme("http")
.loopback()
.port(port)
.path("/index.html")
.toURL();
URLConnection uc = url.openConnection();

// add request properties
uc.addRequestProperty("Cookie", "cookie1");
Expand All @@ -106,6 +115,7 @@ public static void main(String[] args) {

} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
}
13 changes: 10 additions & 3 deletions test/jdk/java/net/ipv6tests/UdpTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2018, 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,11 +24,14 @@
/*
* @test
* @bug 4868820
* @summary IPv6 support for Windows XP and 2003 server
* @key intermittent
* @summary IPv6 support for Windows XP and 2003 server.
* This test requires binding to the wildcard address and as such
* may fail intermittently on some platforms.
* @library /test/lib
* @build jdk.test.lib.NetworkConfiguration
* jdk.test.lib.Platform
* @run main UdpTest
* @run main UdpTest -d
*/

import java.net.DatagramPacket;
Expand Down Expand Up @@ -88,6 +91,7 @@ public static void main (String[] args) throws Exception {
/* basic UDP connectivity test using IPv6 only and IPv4/IPv6 together */

static void test1 () throws Exception {
System.out.println("Test1 starting");
s1 = new DatagramSocket ();
s2 = new DatagramSocket ();
simpleDataExchange (s1, ia4addr, s2, ia4addr);
Expand Down Expand Up @@ -126,6 +130,7 @@ static void test1 () throws Exception {
/* check timeouts on receive */

static void test2 () throws Exception {
System.out.println("Test2 starting");
s1 = new DatagramSocket ();
s2 = new DatagramSocket ();
s1.setSoTimeout (4000);
Expand Down Expand Up @@ -176,6 +181,7 @@ public void run () {
/* check connected sockets */

static void test3 () throws Exception {
System.out.println("Test3 starting");
s1 = new DatagramSocket ();
s2 = new DatagramSocket ();
s1.connect (ia6addr, s2.getLocalPort());
Expand All @@ -187,6 +193,7 @@ static void test3 () throws Exception {
/* check PortUnreachable */

static void test4 () throws Exception {
System.out.println("Test4 starting");
s1 = new DatagramSocket ();
s1.connect (ia6addr, 5000);
s1.setSoTimeout (3000);
Expand Down
9 changes: 5 additions & 4 deletions test/jdk/sun/net/ftp/B6427768.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2006, 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 @@ -105,13 +105,14 @@ public boolean rename(String from, String to) {
}

public static void main(String[] args) throws IOException {
FtpServer server = new FtpServer(0);
InetAddress loopback = InetAddress.getLoopbackAddress();
FtpServer server = new FtpServer(loopback, 0);
int port = server.getLocalPort();
server.setFileSystemHandler(new MyFileSystemHandler("/"));
server.setAuthHandler(new MyAuthHandler());
server.start();
URL url = new URL("ftp://user:passwd@localhost:" + port + "/foo.txt");
URLConnection con = url.openConnection();
URL url = new URL("ftp://user:passwd@" + server.getAuthority() + "/foo.txt");
URLConnection con = url.openConnection(Proxy.NO_PROXY);
// triggers the connection
try {
con.getInputStream();
Expand Down
10 changes: 5 additions & 5 deletions test/jdk/sun/net/www/ftptest/FtpCommandHandler.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2006, 2018, 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 @@ -238,14 +238,14 @@ private void doPasv() {
return;
}
try {
if (pasv == null)
pasv = new ServerSocket(0);
int port = pasv.getLocalPort();
InetAddress rAddress = cmd.getLocalAddress();
if (rAddress instanceof Inet6Address) {
out.println("500 PASV illegal over IPv6 addresses, use EPSV.");
return;
}
if (pasv == null)
pasv = new ServerSocket(0, 0, rAddress);
int port = pasv.getLocalPort();
byte[] a = rAddress.getAddress();
out.println("227 Entering Passive Mode " + a[0] + "," + a[1] + "," + a[2] + "," + a[3] + "," +
(port >> 8) + "," + (port & 0xff) );
Expand All @@ -266,7 +266,7 @@ private void doEpsv(String arg) {
}
try {
if (pasv == null)
pasv = new ServerSocket(0);
pasv = new ServerSocket(0, 0, parent.getInetAddress());
int port = pasv.getLocalPort();
out.println("229 Entering Extended Passive Mode (|||" + port + "|)");
} catch (IOException e) {
Expand Down
6 changes: 5 additions & 1 deletion test/jdk/sun/net/www/ftptest/FtpServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,12 @@ public int getLocalPort() {
return listener.getLocalPort();
}

public InetAddress getInetAddress() {
return listener.getInetAddress();
}

public String getAuthority() {
InetAddress address = listener.getInetAddress();
InetAddress address = getInetAddress();
String hostaddr = address.isAnyLocalAddress()
? "localhost" : address.getHostAddress();
if (hostaddr.indexOf(':') > -1) {
Expand Down

1 comment on commit e3dd9dd

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