Skip to content

Commit 3a2a0f9

Browse files
committed
8223463: Replace wildcard address with loopback or local host in tests - part 2
Removes (or documents) some usages of the wildcard address in intermittently failing tests. Backport-of: 1188188
1 parent 5685e58 commit 3a2a0f9

File tree

4 files changed

+75
-29
lines changed

4 files changed

+75
-29
lines changed

test/jdk/java/net/ServerSocket/AcceptInheritHandle.java

+17-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -32,6 +32,7 @@
3232
import java.net.*;
3333
import java.nio.channels.ServerSocketChannel;
3434
import java.util.ArrayList;
35+
import java.util.Arrays;
3536
import java.util.List;
3637
import java.util.concurrent.TimeUnit;
3738
import java.util.function.Supplier;
@@ -41,8 +42,8 @@ public class AcceptInheritHandle {
4142
enum ServerSocketProducer {
4243
JAVA_NET(() -> {
4344
try {
44-
return new ServerSocket(); }
45-
catch(IOException x) {
45+
return new ServerSocket();
46+
} catch(IOException x) {
4647
throw new UncheckedIOException(x);
4748
}
4849
}),
@@ -86,13 +87,13 @@ static void testNioServerSocketChannel() throws Exception {
8687
test(ServerSocketProducer.NIO_CHANNELS);
8788
}
8889

89-
static void test(ServerSocketProducer ssp, String... sysProps) throws Exception {
90+
static void test(ServerSocketProducer ssp, String... jvmArgs) throws Exception {
9091
System.out.println("\nStarting test for " + ssp.name());
9192

9293
List<String> commands = new ArrayList<>();
9394
commands.add(JAVA);
94-
for (String prop : sysProps)
95-
commands.add(prop);
95+
for (String arg : jvmArgs)
96+
commands.add(arg);
9697
commands.add("-cp");
9798
commands.add(CLASSPATH);
9899
commands.add("AcceptInheritHandle");
@@ -107,7 +108,14 @@ static void test(ServerSocketProducer ssp, String... sysProps) throws Exception
107108
int port = dis.readInt();
108109
System.out.println("Server process listening on " + port + ", connecting...");
109110

110-
Socket socket = new Socket("localhost", port);
111+
String address;
112+
if (Arrays.stream(jvmArgs).anyMatch("-Djava.net.preferIPv4Stack=true"::equals)) {
113+
address = "127.0.0.1";
114+
} else {
115+
InetAddress loopback = InetAddress.getLoopbackAddress();
116+
address = loopback.getHostAddress();
117+
}
118+
Socket socket = new Socket(address, port);
111119
String s = dis.readUTF();
112120
System.out.println("Server process said " + s);
113121

@@ -128,7 +136,8 @@ static void test(ServerSocketProducer ssp, String... sysProps) throws Exception
128136

129137
static void server(ServerSocketProducer producer) throws Exception {
130138
try (ServerSocket ss = producer.supplier().get()) {
131-
ss.bind(new InetSocketAddress(0));
139+
InetAddress loopback = InetAddress.getLoopbackAddress();
140+
ss.bind(new InetSocketAddress(loopback, 0));
132141
int port = ss.getLocalPort();
133142
DataOutputStream dos = new DataOutputStream(System.out);
134143
dos.writeInt(port);

test/jdk/java/net/URLConnection/Responses.java

+17-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2002, 2019, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -60,7 +60,9 @@ static class HttpServer implements Runnable {
6060

6161
public HttpServer() {
6262
try {
63-
ss = new ServerSocket(0);
63+
InetAddress loopback = InetAddress.getLoopbackAddress();
64+
ss = new ServerSocket();
65+
ss.bind(new InetSocketAddress(loopback, 0));
6466
} catch (IOException ioe) {
6567
throw new Error("Unable to create ServerSocket: " + ioe);
6668
}
@@ -70,6 +72,16 @@ public int port() {
7072
return ss.getLocalPort();
7173
}
7274

75+
public String authority() {
76+
InetAddress address = ss.getInetAddress();
77+
String hostaddr = address.isAnyLocalAddress()
78+
? "localhost" : address.getHostAddress();
79+
if (hostaddr.indexOf(':') > -1) {
80+
hostaddr = "[" + hostaddr + "]";
81+
}
82+
return hostaddr + ":" + port();
83+
}
84+
7385
public void shutdown() throws IOException {
7486
ss.close();
7587
}
@@ -116,7 +128,8 @@ public static void main(String args[]) throws Exception {
116128
HttpServer svr = new HttpServer();
117129
(new Thread(svr)).start();
118130

119-
int port = svr.port();
131+
String authority = svr.authority();
132+
System.out.println("Server listening on: " + authority);
120133

121134
/*
122135
* Iterate through each test case and check that getResponseCode
@@ -129,7 +142,7 @@ public static void main(String args[]) throws Exception {
129142
System.out.println("******************");
130143
System.out.println("Test with response: >" + tests[i][0] + "<");
131144

132-
URL url = new URL("http://localhost:" + port + "/" + i);
145+
URL url = new URL("http://" + authority + "/" + i);
133146
HttpURLConnection http = (HttpURLConnection)url.openConnection();
134147

135148
try {

test/jdk/java/net/ipv6tests/TcpTest.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@
2525
* @test
2626
* @bug 4868820
2727
* @key intermittent
28-
* @summary IPv6 support for Windows XP and 2003 server
28+
* @summary IPv6 support for Windows XP and 2003 server. This test requires
29+
* binding to the wildcard address, and as such is susceptible
30+
* of intermittent failures caused by port reuse policy.
2931
* @library /test/lib
3032
* @build jdk.test.lib.NetworkConfiguration
3133
* jdk.test.lib.Platform
@@ -240,4 +242,3 @@ static void test4 () throws Exception {
240242
System.out.println ("Test4: OK");
241243
}
242244
}
243-

test/jdk/sun/net/ftp/FtpURL.java

+38-15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2001, 2019, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -36,8 +36,8 @@ public class FtpURL {
3636
*/
3737

3838
private class FtpServer extends Thread {
39-
private ServerSocket server;
40-
private int port;
39+
private final ServerSocket server;
40+
private final int port;
4141
private boolean done = false;
4242
private boolean portEnabled = true;
4343
private boolean pasvEnabled = true;
@@ -253,8 +253,12 @@ public void run() {
253253
continue;
254254
}
255255
try {
256-
if (pasv == null)
257-
pasv = new ServerSocket(0);
256+
if (pasv == null) {
257+
// Not sure how to support PASV mode over
258+
// IPv6
259+
pasv = new ServerSocket();
260+
pasv.bind(new InetSocketAddress("127.0.0.1", 0));
261+
}
258262
int port = pasv.getLocalPort();
259263
out.println("227 Entering Passive Mode (127,0,0,1," +
260264
(port >> 8) + "," + (port & 0xff) +")");
@@ -369,21 +373,39 @@ public void run() {
369373
}
370374

371375
public FtpServer(int port) {
376+
this(InetAddress.getLoopbackAddress(), port);
377+
}
378+
379+
public FtpServer(InetAddress address, int port) {
372380
this.port = port;
373381
try {
374-
server = new ServerSocket(port);
382+
if (address == null) {
383+
server = new ServerSocket(port);
384+
} else {
385+
server = new ServerSocket();
386+
server.bind(new InetSocketAddress(address, port));
387+
}
375388
} catch (IOException e) {
389+
throw new UncheckedIOException(e);
376390
}
377391
}
378392

379393
public FtpServer() {
380-
this(21);
394+
this(null, 21);
381395
}
382396

383397
public int getPort() {
384-
if (server != null)
385-
return server.getLocalPort();
386-
return 0;
398+
return server.getLocalPort();
399+
}
400+
401+
public String getAuthority() {
402+
InetAddress address = server.getInetAddress();
403+
String hostaddr = address.isAnyLocalAddress()
404+
? "localhost" : address.getHostAddress();
405+
if (hostaddr.indexOf(':') > -1) {
406+
hostaddr = "[" + hostaddr +"]";
407+
}
408+
return hostaddr + ":" + getPort();
387409
}
388410

389411
/**
@@ -449,15 +471,17 @@ public static void main(String[] args) throws Exception {
449471
}
450472

451473
public FtpURL() throws Exception {
452-
FtpServer server = new FtpServer(0);
474+
FtpServer server = new FtpServer(InetAddress.getLoopbackAddress(), 0);
453475
BufferedReader in = null;
454476
try {
455477
server.start();
456-
int port = server.getPort();
478+
String authority = server.getAuthority();
479+
System.out.println("FTP server waiting for connections at: " + authority);
480+
assert authority != null;
457481

458482
// Now let's check the URL handler
459483

460-
URL url = new URL("ftp://user:password@localhost:" + port + "/%2Fetc/motd;type=a");
484+
URL url = new URL("ftp://user:password@" + authority + "/%2Fetc/motd;type=a");
461485
URLConnection con = url.openConnection();
462486
in = new BufferedReader(new InputStreamReader(con.getInputStream()));
463487
String s;
@@ -479,11 +503,10 @@ public FtpURL() throws Exception {
479503
// We're done!
480504

481505
// Second URL test
482-
port = server.getPort();
483506

484507
// Now let's check the URL handler
485508

486-
url = new URL("ftp://user2@localhost:" + port + "/%2Fusr/bin;type=d");
509+
url = new URL("ftp://user2@" + authority + "/%2Fusr/bin;type=d");
487510
con = url.openConnection();
488511
in = new BufferedReader(new InputStreamReader(con.getInputStream()));
489512
do {

0 commit comments

Comments
 (0)