Skip to content

Commit 7d4520c

Browse files
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: chegar, msheppar
1 parent bbd9000 commit 7d4520c

File tree

24 files changed

+209
-71
lines changed

24 files changed

+209
-71
lines changed

test/jdk/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-
InetAddress.getLoopbackAddress(), 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;

test/jdk/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
@@ -121,13 +121,14 @@ public static void main (String[] args) throws Exception {
121121
MyAuthenticator auth = new MyAuthenticator ();
122122
Authenticator.setDefault (auth);
123123
try {
124-
server = new TestHttpServer (new B4722333(), 1, 10, 0);
124+
InetAddress loopback = InetAddress.getLoopbackAddress();
125+
server = new TestHttpServer (new B4722333(), 1, 10, loopback, 0);
125126
System.out.println ("Server started: listening on port: " + server.getLocalPort());
126-
client ("http://localhost:"+server.getLocalPort()+"/d1/d2/d3/foo.html");
127-
client ("http://localhost:"+server.getLocalPort()+"/ASD/d3/x.html");
128-
client ("http://localhost:"+server.getLocalPort()+"/biz/d3/x.html");
129-
client ("http://localhost:"+server.getLocalPort()+"/bar/d3/x.html");
130-
client ("http://localhost:"+server.getLocalPort()+"/fuzz/d3/x.html");
127+
client ("http://" + server.getAuthority() + "/d1/d2/d3/foo.html");
128+
client ("http://" + server.getAuthority() + "/ASD/d3/x.html");
129+
client ("http://" + server.getAuthority() + "/biz/d3/x.html");
130+
client ("http://" + server.getAuthority() + "/bar/d3/x.html");
131+
client ("http://" + server.getAuthority() + "/fuzz/d3/x.html");
131132
} catch (Exception e) {
132133
if (server != null) {
133134
server.terminate();

test/jdk/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 /test/lib
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.test.lib.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-

test/jdk/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, 2014, 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,13 +24,15 @@
2424
/* @test
2525
* @summary Unit test for java.net.ResponseCache
2626
* @bug 4837267
27+
* @library /test/lib
2728
* @author Yingxian Wang
2829
*/
2930

3031
import java.net.*;
3132
import java.util.*;
3233
import java.io.*;
3334
import javax.net.ssl.*;
35+
import jdk.test.lib.net.URIBuilder;
3436

3537
/**
3638
* Request should get serviced by the cache handler. Response get
@@ -90,14 +92,17 @@ public void run() {
9092
try { fis.close(); } catch (IOException unused) {}
9193
}
9294
}
93-
static class NameVerifier implements HostnameVerifier {
95+
static class NameVerifier implements HostnameVerifier {
9496
public boolean verify(String hostname, SSLSession session) {
9597
return true;
9698
}
9799
}
98100
ResponseCacheTest() throws Exception {
99101
/* start the server */
100-
ss = new ServerSocket(0);
102+
InetAddress loopback = InetAddress.getLoopbackAddress();
103+
ss = new ServerSocket();
104+
ss.bind(new InetSocketAddress(loopback, 0));
105+
101106
(new Thread(this)).start();
102107
/* establish http connection to server */
103108
url1 = new URL("http://localhost/file1.cache");
@@ -126,8 +131,12 @@ public boolean verify(String hostname, SSLSession session) {
126131
http.disconnect();
127132

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

test/jdk/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());

test/jdk/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);

test/jdk/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];

test/jdk/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, 2018, 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
@@ -39,8 +39,9 @@ public class TestAfterClose
3939

4040
public static void main(String[] args) {
4141
try {
42-
ServerSocket ss = new ServerSocket(0, 0, null);
43-
Socket socket = new Socket("localhost", ss.getLocalPort());
42+
InetAddress loopback = InetAddress.getLoopbackAddress();
43+
ServerSocket ss = new ServerSocket(0, 0, loopback);
44+
Socket socket = new Socket(loopback, ss.getLocalPort());
4445
ss.accept();
4546
ss.close();
4647
test(socket);

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

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

test/jdk/java/net/SocketOption/OptionsTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ static NetworkInterface getNetworkInterface() {
9999

100100
static void doSocketTests() throws Exception {
101101
try (
102-
ServerSocket srv = new ServerSocket(0);
102+
ServerSocket srv = new ServerSocket(0, 50, InetAddress.getLoopbackAddress());
103103
Socket c = new Socket(InetAddress.getLoopbackAddress(), srv.getLocalPort());
104104
Socket s = srv.accept();
105105
) {

0 commit comments

Comments
 (0)