Skip to content

Commit dc19eff

Browse files
author
SendaoYan
committed
8223145: Replace wildcard address with loopback or local host in tests - part 1
Replaces binding to wildacard with alternative less susceptible to intermittent failure in some intermittently failing tests. Reviewed-by: phh Backport-of: 7d4520c109f408a7dbcdcc424dfef121e3eeaaa7
1 parent 80eec4a commit dc19eff

File tree

21 files changed

+190
-59
lines changed

21 files changed

+190
-59
lines changed

jdk/test/com/sun/net/httpserver/bugs/B6361557.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ public void handle (HttpExchange t)
6969

7070
public static void main (String[] args) throws Exception {
7171
Handler handler = new Handler();
72-
InetSocketAddress addr = new InetSocketAddress (0);
72+
InetAddress loopback = InetAddress.getLoopbackAddress();
73+
InetSocketAddress addr = new InetSocketAddress (loopback, 0);
7374
HttpServer server = HttpServer.create (addr, 0);
7475
HttpContext ctx = server.createContext ("/test", handler);
7576

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

8081
InetSocketAddress destaddr = new InetSocketAddress (
81-
"127.0.0.1", server.getAddress().getPort()
82+
loopback, server.getAddress().getPort()
8283
);
8384
System.out.println ("destaddr " + destaddr);
8485

8586
Selector selector = Selector.open ();
8687
int requests = 0;
8788
int responses = 0;
8889
while (true) {
89-
int selres = selector.select (1);
90+
// we need to read responses from time to time: slightly
91+
// increase the timeout with the amount of pending responses
92+
// to give a chance to the server to reply.
93+
int selres = selector.select (requests - responses + 1);
9094
Set<SelectionKey> selkeys = selector.selectedKeys();
9195
for (SelectionKey key : selkeys) {
9296
if (key.isReadable()) {
@@ -95,14 +99,18 @@ public static void main (String[] args) throws Exception {
9599
try {
96100
int x = chan.read(buf);
97101
if (x == -1 || responseComplete(buf)) {
102+
System.out.print("_");
98103
key.attach(null);
99104
chan.close();
100105
responses++;
101106
}
102-
} catch (IOException e) {}
107+
} catch (IOException e) {
108+
System.out.println(e);
109+
}
103110
}
104111
}
105112
if (requests < NUM) {
113+
System.out.print(".");
106114
SocketChannel schan = SocketChannel.open(destaddr);
107115
requestBuf.rewind();
108116
int c = 0;

jdk/test/java/net/Authenticator/B4722333.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2002, 2012, 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
@@ -120,13 +120,14 @@ public static void main (String[] args) throws Exception {
120120
MyAuthenticator auth = new MyAuthenticator ();
121121
Authenticator.setDefault (auth);
122122
try {
123-
server = new TestHttpServer (new B4722333(), 1, 10, 0);
123+
InetAddress loopback = InetAddress.getLoopbackAddress();
124+
server = new TestHttpServer (new B4722333(), 1, 10, loopback, 0);
124125
System.out.println ("Server started: listening on port: " + server.getLocalPort());
125-
client ("http://localhost:"+server.getLocalPort()+"/d1/d2/d3/foo.html");
126-
client ("http://localhost:"+server.getLocalPort()+"/ASD/d3/x.html");
127-
client ("http://localhost:"+server.getLocalPort()+"/biz/d3/x.html");
128-
client ("http://localhost:"+server.getLocalPort()+"/bar/d3/x.html");
129-
client ("http://localhost:"+server.getLocalPort()+"/fuzz/d3/x.html");
126+
client ("http://" + server.getAuthority() + "/d1/d2/d3/foo.html");
127+
client ("http://" + server.getAuthority() + "/ASD/d3/x.html");
128+
client ("http://" + server.getAuthority() + "/biz/d3/x.html");
129+
client ("http://" + server.getAuthority() + "/bar/d3/x.html");
130+
client ("http://" + server.getAuthority() + "/fuzz/d3/x.html");
130131
} catch (Exception e) {
131132
if (server != null) {
132133
server.terminate();

jdk/test/java/net/HttpURLConnection/UnmodifiableMaps.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2012, 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
@@ -24,11 +24,13 @@
2424
/**
2525
* @test
2626
* @bug 7128648
27+
* @library /lib/testlibrary
2728
* @modules jdk.httpserver
2829
* @summary HttpURLConnection.getHeaderFields should return an unmodifiable Map
2930
*/
3031

3132
import java.io.IOException;
33+
import java.net.InetAddress;
3234
import java.net.InetSocketAddress;
3335
import java.net.URI;
3436
import java.net.HttpURLConnection;
@@ -41,14 +43,20 @@
4143
import com.sun.net.httpserver.HttpServer;
4244
import com.sun.net.httpserver.Headers;
4345
import static java.net.Proxy.NO_PROXY;
46+
import jdk.testlibrary.net.URIBuilder;
4447

4548
public class UnmodifiableMaps {
4649

4750
void test(String[] args) throws Exception {
4851
HttpServer server = startHttpServer();
4952
try {
5053
InetSocketAddress address = server.getAddress();
51-
URI uri = new URI("http://localhost:" + address.getPort() + "/foo");
54+
URI uri = URIBuilder.newBuilder()
55+
.scheme("http")
56+
.host(address.getAddress())
57+
.port(address.getPort())
58+
.path("/foo")
59+
.build();
5260
doClient(uri);
5361
} finally {
5462
server.stop(0);
@@ -78,7 +86,8 @@ void doClient(URI uri) throws Exception {
7886

7987
// HTTP Server
8088
HttpServer startHttpServer() throws IOException {
81-
HttpServer httpServer = HttpServer.create(new InetSocketAddress(0), 0);
89+
InetAddress loopback = InetAddress.getLoopbackAddress();
90+
HttpServer httpServer = HttpServer.create(new InetSocketAddress(loopback, 0), 0);
8291
httpServer.createContext("/foo", new SimpleHandler());
8392
httpServer.start();
8493
return httpServer;
@@ -146,4 +155,3 @@ public void instanceMain(String[] args) throws Throwable {
146155
System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
147156
if (failed > 0) throw new AssertionError("Some tests failed");}
148157
}
149-

jdk/test/java/net/ResponseCache/ResponseCacheTest.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2003, 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
@@ -24,6 +24,7 @@
2424
/* @test
2525
* @summary Unit test for java.net.ResponseCache
2626
* @bug 4837267
27+
* @library /lib/testlibrary
2728
* @author Yingxian Wang
2829
*/
2930

@@ -32,6 +33,7 @@
3233
import java.io.*;
3334
import sun.net.www.ParseUtil;
3435
import javax.net.ssl.*;
36+
import jdk.testlibrary.net.URIBuilder;
3537

3638
/**
3739
* Request should get serviced by the cache handler. Response get
@@ -91,14 +93,17 @@ public void run() {
9193
try { fis.close(); } catch (IOException unused) {}
9294
}
9395
}
94-
static class NameVerifier implements HostnameVerifier {
96+
static class NameVerifier implements HostnameVerifier {
9597
public boolean verify(String hostname, SSLSession session) {
9698
return true;
9799
}
98100
}
99101
ResponseCacheTest() throws Exception {
100102
/* start the server */
101-
ss = new ServerSocket(0);
103+
InetAddress loopback = InetAddress.getLoopbackAddress();
104+
ss = new ServerSocket();
105+
ss.bind(new InetSocketAddress(loopback, 0));
106+
102107
(new Thread(this)).start();
103108
/* establish http connection to server */
104109
url1 = new URL("http://localhost/file1.cache");
@@ -127,8 +132,12 @@ public boolean verify(String hostname, SSLSession session) {
127132
http.disconnect();
128133

129134
// testing ResponseCacheHandler.put()
130-
url2 = new URL("http://localhost:" +
131-
Integer.toString(ss.getLocalPort())+"/file2.1");
135+
url2 = URIBuilder.newBuilder()
136+
.scheme("http")
137+
.host(ss.getInetAddress())
138+
.port(ss.getLocalPort())
139+
.path("/file2.1")
140+
.toURL();
132141
http = (HttpURLConnection)url2.openConnection();
133142
System.out.println("responsecode2 is :"+http.getResponseCode());
134143
Map<String,List<String>> headers2 = http.getHeaderFields();

jdk/test/java/net/Socket/GetLocalAddress.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1998, 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
@@ -45,7 +45,8 @@ public static void main(String args[]) throws Exception {
4545
int linger = 65546;
4646
int value = 0;
4747
addr = InetAddress.getLocalHost();
48-
ss = new ServerSocket(0);
48+
ss = new ServerSocket();
49+
ss.bind(new InetSocketAddress(addr, 0));
4950
port = ss.getLocalPort();
5051

5152
Thread t = new Thread(new GetLocalAddress());

jdk/test/java/net/Socket/SetReceiveBufferSize.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2000, 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
@@ -28,6 +28,8 @@
2828
*
2929
*/
3030

31+
import java.net.InetAddress;
32+
import java.net.InetSocketAddress;
3133
import java.net.Socket;
3234
import java.net.ServerSocket;
3335

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

3941
public SetReceiveBufferSize() throws Exception {
40-
ServerSocket ss = new ServerSocket(0);
41-
Socket s = new Socket("localhost", ss.getLocalPort());
42+
ServerSocket ss = new ServerSocket();
43+
InetAddress loopback = InetAddress.getLoopbackAddress();
44+
ss.bind(new InetSocketAddress(loopback, 0));
45+
Socket s = new Socket(loopback, ss.getLocalPort());
4246
Socket accepted = ss.accept();
4347
try {
4448
s.setReceiveBufferSize(0);

jdk/test/java/net/Socket/SoTimeout.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1998, 2010, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1998, 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
@@ -44,7 +44,8 @@ public class SoTimeout implements Runnable {
4444

4545
public static void main(String[] args) throws Exception {
4646
addr = InetAddress.getLocalHost();
47-
serverSocket = new ServerSocket(0);
47+
serverSocket = new ServerSocket();
48+
serverSocket.bind(new InetSocketAddress(addr, 0));
4849
port = serverSocket.getLocalPort();
4950

5051
byte[] b = new byte[12];

jdk/test/java/net/Socket/TestAfterClose.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2007, 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,9 @@ public class TestAfterClose
3636

3737
public static void main(String[] args) {
3838
try {
39-
ServerSocket ss = new ServerSocket(0, 0, null);
40-
Socket socket = new Socket("localhost", ss.getLocalPort());
39+
InetAddress loopback = InetAddress.getLoopbackAddress();
40+
ServerSocket ss = new ServerSocket(0, 0, loopback);
41+
Socket socket = new Socket(loopback, ss.getLocalPort());
4142
ss.accept();
4243
ss.close();
4344
test(socket);

jdk/test/java/net/Socket/UrgentDataTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,12 @@ public static void main (String args[]) {
5353
try {
5454
UrgentDataTest test = new UrgentDataTest ();
5555
if (args.length == 0) {
56-
test.listener = new ServerSocket (0);
56+
InetAddress loopback = InetAddress.getLoopbackAddress();
57+
test.listener = new ServerSocket ();
58+
test.listener.bind(new InetSocketAddress(loopback, 0));
5759
test.isClient = true;
5860
test.isServer = true;
59-
test.clHost = "127.0.0.1";
61+
test.clHost = loopback.getHostAddress();
6062
test.clPort = test.listener.getLocalPort();
6163
test.run();
6264
} else if (args[0].equals ("-server")) {

jdk/test/java/net/URL/GetContent.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1998, 2010, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1998, 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
@@ -24,11 +24,13 @@
2424
/**
2525
* @test
2626
* @bug 4145315
27+
* @library /lib/testlibrary
2728
* @summary Test a read from nonexistant URL
2829
*/
2930

3031
import java.net.*;
3132
import java.io.*;
33+
import jdk.testlibrary.net.URIBuilder;
3234

3335
public class GetContent implements Runnable {
3436

@@ -71,10 +73,12 @@ public void run() {
7173

7274
boolean error = true;
7375
try {
74-
String name = "http://localhost:" + ss.getLocalPort() +
75-
"/no-such-name";
76-
java.net.URL url = null;
77-
url = new java.net.URL(name);
76+
java.net.URL url = URIBuilder.newBuilder()
77+
.scheme("http")
78+
.host(ss.getInetAddress())
79+
.port(ss.getLocalPort())
80+
.path("/no-such-name")
81+
.toURL();
7882
Object obj = url.getContent();
7983
InputStream in = (InputStream) obj;
8084
byte buff[] = new byte[200];

0 commit comments

Comments
 (0)