Skip to content

Commit f71db30

Browse files
committed
8230435: Replace wildcard address with loopback or local host in tests - part 22
Fixes tests to use the loopback address whenever possible. It also fixes some safe publishing issues, or add diagnostics in some of the tests. Reviewed-by: michaelm
1 parent 7b49c40 commit f71db30

File tree

15 files changed

+200
-52
lines changed

15 files changed

+200
-52
lines changed

test/jdk/com/sun/net/httpserver/bugs/8199849/BasicAuthenticatorCharset.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ public static void main(String[] args) throws Exception {
157157
String defaultCharset = System.getProperty("file.encoding");
158158
boolean isUTF8 = defaultCharset.equalsIgnoreCase("UTF-8");
159159
testHandler = new Handler();
160-
InetSocketAddress addr = new InetSocketAddress(0);
160+
InetSocketAddress addr = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);
161161
testHttpServer = HttpServer.create(addr, 0);
162162

163163
// Set the passing credentials OLD client

test/jdk/java/net/Authenticator/B4678055.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@
3838

3939
public class B4678055 implements HttpCallback {
4040

41-
static int count = 0;
42-
static String authstring;
41+
static volatile int count = 0;
42+
static volatile String authstring;
4343

4444
void errorReply (HttpTransaction req, String reply) throws IOException {
4545
req.addResponseHeader ("Connection", "close");
@@ -56,6 +56,7 @@ void okReply (HttpTransaction req) throws IOException {
5656

5757
public void request (HttpTransaction req) {
5858
try {
59+
System.out.println("Server handling case: "+ count);
5960
authstring = req.getRequestHeader ("Authorization");
6061
System.out.println (authstring);
6162
switch (count) {
@@ -95,6 +96,7 @@ public void request (HttpTransaction req) {
9596
}
9697
count ++;
9798
} catch (IOException e) {
99+
System.err.println("Unexpected exception for case " + count + ": " + e);
98100
e.printStackTrace();
99101
}
100102
}
@@ -143,6 +145,8 @@ public static void main (String[] args) throws Exception {
143145
client(serverURL + "d2/foo.html");
144146
client(serverURL + "d2/foo.html");
145147
} catch (Exception e) {
148+
System.out.println("Client got exception: " + e);
149+
System.out.println("Terminating server");
146150
if (server != null) {
147151
server.terminate();
148152
}
@@ -156,10 +160,13 @@ public static void main (String[] args) throws Exception {
156160
if (!checkFinalAuth()) {
157161
except ("Wrong authorization string received from client");
158162
}
163+
System.out.println("Terminating server");
159164
server.terminate();
160165
}
161166

162167
public static void except (String s) {
168+
System.out.println("Check failed: " + s);
169+
System.out.println("Terminating server");
163170
server.terminate();
164171
throw new RuntimeException (s);
165172
}
@@ -169,7 +176,7 @@ static class MyAuthenticator extends Authenticator {
169176
super ();
170177
}
171178

172-
int count = 0;
179+
volatile int count = 0;
173180

174181
public PasswordAuthentication getPasswordAuthentication () {
175182
PasswordAuthentication pw;

test/jdk/java/net/DatagramSocket/PortUnreachable.java

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
/**
2525
* @test
2626
* @bug 4361783
27+
* @key intermittent
2728
* @summary Test to see if ICMP Port Unreachable on non-connected
2829
* DatagramSocket causes a SocketException "socket closed"
2930
* exception on Windows 2000.
@@ -43,7 +44,7 @@ public class PortUnreachable {
4344
public void serverSend() {
4445
try {
4546
InetAddress addr = InetAddress.getLocalHost();
46-
Thread.currentThread().sleep(1000);
47+
Thread.sleep(1000);
4748
// send a delayed packet which should mean a delayed icmp
4849
// port unreachable
4950
byte b[] = "A late msg".getBytes();
@@ -64,26 +65,70 @@ public void serverSend() {
6465
DatagramSocket recreateServerSocket (int serverPort) throws Exception {
6566
DatagramSocket serverSocket = null;
6667
int retryCount = 0;
68+
long sleeptime = 0;
6769
System.out.println("Attempting to recreate server socket with port: " +
6870
serverPort);
71+
// it's possible that this method intermittently fails, if some other
72+
// process running on the machine grabs the port we want before us,
73+
// and doesn't release it before the 5 * 500 ms are elapsed...
6974
while (serverSocket == null) {
7075
try {
7176
serverSocket = new DatagramSocket(serverPort, InetAddress.getLocalHost());
7277
} catch (BindException bEx) {
7378
if (retryCount++ < 5) {
74-
Thread.sleep(500);
79+
sleeptime += sleepAtLeast(500);
7580
} else {
76-
System.out.println("Give up after 5 retries");
81+
System.out.println("Give up after 5 retries and " + sleeptime(sleeptime));
82+
System.out.println("Has some other process grabbed port " + serverPort + "?");
7783
throw bEx;
7884
}
7985
}
8086
}
8187

8288
System.out.println("PortUnreachableTest.recreateServerSocket: returning socket == "
83-
+ serverSocket.getLocalAddress() + ":" + serverSocket.getLocalPort());
89+
+ serverSocket.getLocalAddress() + ":" + serverSocket.getLocalPort()
90+
+ " obtained at " + attempt(retryCount) + " attempt with " + sleeptime(sleeptime));
8491
return serverSocket;
8592
}
8693

94+
long sleepAtLeast(long millis) throws Exception {
95+
long start = System.nanoTime();
96+
long ms = millis;
97+
while (ms > 0) {
98+
assert ms < Long.MAX_VALUE/1000_000L;
99+
Thread.sleep(ms);
100+
long elapsedms = (System.nanoTime() - start)/1000_000L;
101+
ms = millis - elapsedms;
102+
}
103+
return millis - ms;
104+
}
105+
106+
String attempt(int retry) {
107+
switch (retry) {
108+
case 0: return "first";
109+
case 1: return "second";
110+
case 2: return "third";
111+
default: return retry + "th";
112+
}
113+
}
114+
115+
String sleeptime(long millis) {
116+
if (millis == 0) return "no sleep";
117+
long sec = millis / 1000L;
118+
long ms = millis % 1000L;
119+
String sleeptime = "";
120+
if (millis > 0) {
121+
if (sec > 0) {
122+
sleeptime = "" + sec + " s" +
123+
(ms > 0 ? " " : "");
124+
}
125+
if (ms > 0 ) {
126+
sleeptime += ms + " ms";
127+
}
128+
} else sleeptime = millis + " ms"; // should not happen
129+
return sleeptime + " of sleep time";
130+
}
131+
87132
PortUnreachable() throws Exception {
88133
clientSock = new DatagramSocket(new InetSocketAddress(InetAddress.getLocalHost(), 0));
89134
clientPort = clientSock.getLocalPort();
@@ -126,4 +171,3 @@ public static void main(String[] args) throws Exception {
126171
}
127172

128173
}
129-

test/jdk/java/net/URLConnection/RedirectLimit.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ class RedirLimitServer extends Thread {
7373
// Read until the end of a HTTP request
7474
void readOneRequest(InputStream is) throws IOException {
7575
int requestEndCount = 0, r;
76+
StringBuilder sb = new StringBuilder();
7677
while ((r = is.read()) != -1) {
78+
sb.append((char)r);
7779
if (r == requestEnd[requestEndCount]) {
7880
requestEndCount++;
7981
if (requestEndCount == 4) {
@@ -83,22 +85,27 @@ void readOneRequest(InputStream is) throws IOException {
8385
requestEndCount = 0;
8486
}
8587
}
88+
System.out.println("Server got request: " + sb.toString());
8689
}
8790

8891
public void run() {
8992
try {
9093
readyToStart.countDown();
9194
for (int i=0; i<NUM_REDIRECTS; i++) {
9295
try (Socket s = ss.accept()) {
96+
System.out.println("Server accepted socket: " + s);
9397
s.setSoTimeout(TIMEOUT);
9498
readOneRequest(s.getInputStream());
99+
System.out.println("Redirecting to: /redirect" + i);
95100
String reply = reply1 + port + "/redirect" + i + reply2;
96101
s.getOutputStream().write(reply.getBytes());
97102
}
98103
}
99104
try (Socket s = ss.accept()) {
105+
System.out.println("Server accepted socket: " + s);
100106
s.setSoTimeout(TIMEOUT);
101107
readOneRequest(s.getInputStream());
108+
System.out.println("Replying...");
102109
s.getOutputStream().write(reply3.getBytes());
103110
}
104111
} catch (Exception e) {

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ static Object[][] getTests() {
5656
* "HTTP/1.1 404 "
5757
*/
5858
static class HttpServer implements Runnable {
59-
ServerSocket ss;
59+
final ServerSocket ss;
60+
volatile boolean shutdown;
6061

6162
public HttpServer() {
6263
try {
@@ -83,14 +84,15 @@ public String authority() {
8384
}
8485

8586
public void shutdown() throws IOException {
87+
shutdown = true;
8688
ss.close();
8789
}
8890

8991
public void run() {
9092
Object[][] tests = getTests();
9193

9294
try {
93-
for (;;) {
95+
while(!shutdown) {
9496
Socket s = ss.accept();
9597

9698
BufferedReader in = new BufferedReader(
@@ -101,6 +103,7 @@ public void run() {
101103
int pos2 = req.indexOf(' ', pos1+1);
102104

103105
int i = Integer.parseInt(req.substring(pos1+2, pos2));
106+
System.out.println("Server replying to >" + tests[i][0] + "<");
104107

105108
PrintStream out = new PrintStream(
106109
new BufferedOutputStream(
@@ -117,6 +120,9 @@ public void run() {
117120
s.close();
118121
}
119122
} catch (Exception e) {
123+
if (!shutdown) {
124+
e.printStackTrace();
125+
}
120126
}
121127
}
122128
}
@@ -170,6 +176,7 @@ public static void main(String args[]) throws Exception {
170176
actualPhrase + ", expected: " + expectedPhrase);
171177
}
172178
} catch (IOException e) {
179+
System.err.println("Test failed for >" + tests[i][0] + "<: " + e);
173180
e.printStackTrace();
174181
failures++;
175182
}

test/jdk/javax/net/ssl/templates/SSLSocketTemplate.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import javax.net.ssl.SSLSocket;
4646
import javax.net.ssl.SSLSocketFactory;
4747
import javax.net.ssl.TrustManagerFactory;
48+
import java.net.InetAddress;
4849
import java.net.InetSocketAddress;
4950
import java.net.SocketTimeoutException;
5051
import java.security.KeyStore;
@@ -221,15 +222,25 @@ protected void configureServerSocket(SSLServerSocket socket) {
221222
*/
222223
protected volatile int serverPort = 0;
223224

225+
/*
226+
* What's the server address? null means binding to the wildcard.
227+
*/
228+
protected volatile InetAddress serverAddress = null;
229+
224230
/*
225231
* Define the server side of the test.
226232
*/
227233
protected void doServerSide() throws Exception {
228234
// kick start the server side service
229235
SSLContext context = createServerSSLContext();
230236
SSLServerSocketFactory sslssf = context.getServerSocketFactory();
231-
SSLServerSocket sslServerSocket =
232-
(SSLServerSocket)sslssf.createServerSocket(serverPort);
237+
InetAddress serverAddress = this.serverAddress;
238+
SSLServerSocket sslServerSocket = serverAddress == null ?
239+
(SSLServerSocket)sslssf.createServerSocket(serverPort)
240+
: (SSLServerSocket)sslssf.createServerSocket();
241+
if (serverAddress != null) {
242+
sslServerSocket.bind(new InetSocketAddress(serverAddress, serverPort));
243+
}
233244
configureServerSocket(sslServerSocket);
234245
serverPort = sslServerSocket.getLocalPort();
235246

@@ -317,8 +328,11 @@ protected void doClientSide() throws Exception {
317328
try (SSLSocket sslSocket = (SSLSocket)sslsf.createSocket()) {
318329
try {
319330
configureClientSocket(sslSocket);
320-
sslSocket.connect(
321-
new InetSocketAddress("localhost", serverPort), 15000);
331+
InetAddress serverAddress = this.serverAddress;
332+
InetSocketAddress connectAddress = serverAddress == null
333+
? new InetSocketAddress("localhost", serverPort)
334+
: new InetSocketAddress(serverAddress, serverPort);
335+
sslSocket.connect(connectAddress, 15000);
322336
} catch (IOException ioe) {
323337
// The server side may be impacted by naughty test cases or
324338
// third party routines, and cannot accept connections.

test/jdk/sun/net/InetAddress/nameservice/simple/DefaultCaching.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,22 @@ static void test(String host, String address,
101101

102102
static void sleep(int seconds) {
103103
try {
104-
Thread.sleep(seconds * 1000);
105-
} catch (InterruptedException e) {}
104+
sleepms(seconds * 1000);
105+
} catch (InterruptedException e) {
106+
e.printStackTrace();
107+
}
108+
}
109+
110+
static long sleepms(long millis) throws InterruptedException {
111+
long start = System.nanoTime();
112+
long ms = millis;
113+
while (ms > 0) {
114+
assert ms < Long.MAX_VALUE/1000_000L;
115+
Thread.sleep(ms);
116+
long elapsedms = (System.nanoTime() - start)/1000_000L;
117+
ms = millis - elapsedms;
118+
}
119+
return millis - ms;
106120
}
107121

108122
static void test(String host, String address, boolean shouldSucceed) {
@@ -114,7 +128,7 @@ static void test(String host, String address, boolean shouldSucceed) {
114128
+ addr + ")");
115129
}
116130
if (!address.equals(addr.getHostAddress())) {
117-
throw new RuntimeException(host+":"+address+": compare failed (found "
131+
throw new RuntimeException(host+"/"+address+": compare failed (found "
118132
+ addr + ")");
119133
}
120134
System.out.println("test: " + host + "/" + address

test/jdk/sun/net/www/AuthHeaderTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ static void read (InputStream is) throws IOException {
8585
static void client (String u) throws Exception {
8686
URL url = new URL (u);
8787
System.out.println ("client opening connection to: " + u);
88-
URLConnection urlc = url.openConnection ();
88+
URLConnection urlc = url.openConnection (Proxy.NO_PROXY);
8989
InputStream is = urlc.getInputStream ();
9090
read (is);
9191
is.close();

test/jdk/sun/net/www/http/HttpClient/RetryPost.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,17 @@ void doClient() throws Exception {
8484
throw new RuntimeException("Failed: POST request being retried");
8585

8686
} catch (SocketException se) {
87+
System.out.println("Got expected exception: " + se);
8788
// this is what we expect to happen and is OK.
88-
if (shouldRetry && httpHandler.getCallCount() != 2)
89+
if (shouldRetry && httpHandler.getCallCount() != 2) {
90+
se.printStackTrace(System.out);
8991
throw new RuntimeException("Failed: Handler should have been called twice. " +
9092
"It was called "+ httpHandler.getCallCount() + " times");
91-
else if (!shouldRetry && httpHandler.getCallCount() != 1)
93+
} else if (!shouldRetry && httpHandler.getCallCount() != 1) {
94+
se.printStackTrace(System.out);
9295
throw new RuntimeException("Failed: Handler should have only been called once" +
9396
"It was called "+ httpHandler.getCallCount() + " times");
97+
}
9498
} finally {
9599
httpServer.stop(1);
96100
executorService.shutdown();

0 commit comments

Comments
 (0)