Skip to content

Commit 0bc8b22

Browse files
committed
8230858: Replace wildcard address with loopback or local host in tests - part 23
Add new traces for better diagnosis, refrain binding to the wildcard address when possible. Backport-of: e9eaba3
1 parent 3592b70 commit 0bc8b22

File tree

13 files changed

+113
-42
lines changed

13 files changed

+113
-42
lines changed

test/jdk/java/net/CookieHandler/CookieManagerTest.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,16 @@
2626
* @summary Unit test for java.net.CookieManager
2727
* @bug 6244040 7150552 7051862
2828
* @modules jdk.httpserver
29-
* @run main/othervm -ea CookieManagerTest
29+
* java.logging
30+
* @run main/othervm -ea -esa CookieManagerTest
3031
* @author Edward Wang
3132
*/
3233

3334
import com.sun.net.httpserver.*;
3435
import java.io.IOException;
3536
import java.net.*;
37+
import java.util.logging.Level;
38+
import java.util.logging.Logger;
3639
import static java.net.Proxy.NO_PROXY;
3740

3841
public class CookieManagerTest {
@@ -59,6 +62,11 @@ static String getAddr() {
5962
}
6063

6164
public static void main(String[] args) throws Exception {
65+
// logs everything...
66+
Logger root = Logger.getLogger("");
67+
root.setLevel(Level.ALL);
68+
root.getHandlers()[0].setLevel(Level.ALL);
69+
6270
startHttpServer();
6371
makeHttpCall();
6472

test/jdk/java/net/Socket/HttpProxy.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ void startSimpleWriter(String threadName, final OutputStream os, final int start
132132
public void run() {
133133
try { simpleWrite(os, start); }
134134
catch (Exception e) {unexpected(e); }
135+
finally { out.println(threadName + ": done"); }
135136
}}, threadName)).start();
136137
}
137138

@@ -142,6 +143,7 @@ void simpleWrite(OutputStream os, int start) throws Exception {
142143
b[1] = (byte) (i % 256);
143144
os.write(b);
144145
}
146+
out.println("Wrote " + start + " -> " + (start + 100));
145147
}
146148

147149
void simpleRead(InputStream is, int start) throws Exception {
@@ -156,6 +158,7 @@ void simpleRead(InputStream is, int start) throws Exception {
156158
if (r != i)
157159
throw new Exception("read " + r + " expected " +i);
158160
}
161+
out.println("Read " + start + " -> " + (start + 100));
159162
}
160163

161164
int bytes(byte b1, byte b2) {
@@ -207,6 +210,7 @@ private void processRequest(Socket clientSocket) throws Exception {
207210

208211
// retrieve the host and port info from the status-line
209212
InetSocketAddress serverAddr = getConnectInfo(statusLine);
213+
out.println("Proxy serving CONNECT request to " + serverAddr);
210214

211215
//open socket to the server
212216
try (Socket serverSocket = new Socket(serverAddr.getAddress(),

test/jdk/java/net/Socket/NullHost.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,10 @@ public int getPort() {
4646
return svr.getLocalPort();
4747
}
4848

49+
volatile boolean done;
4950
public void shutdown() {
5051
try {
52+
done = true;
5153
svr.close();
5254
} catch (IOException e) {
5355
}
@@ -56,11 +58,12 @@ public void shutdown() {
5658
public void run() {
5759
Socket s;
5860
try {
59-
while (true) {
61+
while (!done) {
6062
s = svr.accept();
6163
s.close();
6264
}
6365
} catch (IOException e) {
66+
if (!done) e.printStackTrace();
6467
}
6568
}
6669
}
@@ -74,13 +77,9 @@ public NullHost () throws IOException {
7477
int port = s.getPort();
7578
s.start();
7679
try {
77-
Socket sock = new Socket((String)null, port);
78-
sock.close();
79-
sock = new Socket((String)null, port, true);
80-
sock.close();
81-
sock = new Socket((String)null, port, null, 0);
82-
sock.close();
83-
80+
try (var sock = new Socket((String)null, port)) {}
81+
try (var sock = new Socket((String)null, port, true)) {}
82+
try (var sock = new Socket((String)null, port, null, 0)) {}
8483
} catch (NullPointerException e) {
8584
throw new RuntimeException("Got a NPE");
8685
} finally {

test/jdk/sun/net/www/http/KeepAliveCache/B5045306.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2005, 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
@@ -62,7 +62,7 @@ public static void main(String[] args) throws Exception {
6262
public static void startHttpServer() {
6363
try {
6464
httpTrans = new SimpleHttpTransaction();
65-
server = new TestHttpServer(httpTrans, 1, 10, 0);
65+
server = new TestHttpServer(httpTrans, 1, 10, InetAddress.getLocalHost(), 0);
6666
} catch (IOException e) {
6767
e.printStackTrace();
6868
}
@@ -71,13 +71,14 @@ public static void startHttpServer() {
7171
public static void clientHttpCalls() {
7272
try {
7373
System.out.println("http server listen on: " + server.getLocalPort());
74-
String baseURLStr = "http://" + InetAddress.getLocalHost().getHostAddress() + ":" +
75-
server.getLocalPort() + "/";
74+
String hostAddr = InetAddress.getLocalHost().getHostAddress();
75+
if (hostAddr.indexOf(':') > -1) hostAddr = "[" + hostAddr + "]";
76+
String baseURLStr = "http://" + hostAddr + ":" + server.getLocalPort() + "/";
7677

7778
URL bigDataURL = new URL (baseURLStr + "firstCall");
7879
URL smallDataURL = new URL (baseURLStr + "secondCall");
7980

80-
HttpURLConnection uc = (HttpURLConnection)bigDataURL.openConnection();
81+
HttpURLConnection uc = (HttpURLConnection)bigDataURL.openConnection(Proxy.NO_PROXY);
8182

8283
//Only read 1 byte of response data and close the stream
8384
InputStream is = uc.getInputStream();
@@ -88,15 +89,15 @@ public static void clientHttpCalls() {
8889
// Allow the KeepAliveStreamCleaner thread to read the data left behind and cache the connection.
8990
try { Thread.sleep(2000); } catch (Exception e) {}
9091

91-
uc = (HttpURLConnection)smallDataURL.openConnection();
92+
uc = (HttpURLConnection)smallDataURL.openConnection(Proxy.NO_PROXY);
9293
uc.getResponseCode();
9394

9495
if (SimpleHttpTransaction.failed)
9596
throw new RuntimeException("Failed: Initial Keep Alive Connection is not being reused");
9697

9798
// Part 2
9899
URL part2Url = new URL (baseURLStr + "part2");
99-
uc = (HttpURLConnection)part2Url.openConnection();
100+
uc = (HttpURLConnection)part2Url.openConnection(Proxy.NO_PROXY);
100101
is = uc.getInputStream();
101102
is.close();
102103

test/jdk/sun/net/www/protocol/https/HttpsClient/ServerIdentityTest.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2001, 2016, 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
@@ -42,7 +42,10 @@
4242
import java.io.BufferedWriter;
4343
import java.io.OutputStreamWriter;
4444
import java.net.HttpURLConnection;
45+
import java.net.InetAddress;
46+
import java.net.Proxy;
4547
import java.net.URL;
48+
import java.net.UnknownHostException;
4649

4750
import javax.net.ssl.HttpsURLConnection;
4851
import javax.net.ssl.SSLContext;
@@ -64,6 +67,10 @@ public static void main(String[] args) throws Exception {
6467
(new ServerIdentityTest()).run();
6568
}
6669

70+
ServerIdentityTest() throws UnknownHostException {
71+
serverAddress = InetAddress.getByName(hostname);
72+
}
73+
6774
@Override
6875
protected boolean isCustomizedClientConnection() {
6976
return true;
@@ -88,7 +95,7 @@ protected void runClientApplication(int serverPort) throws Exception {
8895
HttpURLConnection urlc = null;
8996
InputStream is = null;
9097
try {
91-
urlc = (HttpURLConnection)url.openConnection();
98+
urlc = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);
9299
is = urlc.getInputStream();
93100
} finally {
94101
if (is != null) {

test/jdk/sun/net/www/protocol/https/HttpsURLConnection/DNSIdentities.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2010, 2015, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2010, 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
@@ -651,8 +651,13 @@ void doServerSide() throws Exception {
651651
serverModulus, serverPrivateExponent, passphrase);
652652
SSLServerSocketFactory sslssf = context.getServerSocketFactory();
653653

654+
// doClientSide() connects to "localhost"
655+
InetAddress localHost = InetAddress.getByName("localhost");
656+
InetSocketAddress address = new InetSocketAddress(localHost, serverPort);
657+
654658
sslServerSocket =
655-
(SSLServerSocket) sslssf.createServerSocket(serverPort);
659+
(SSLServerSocket) sslssf.createServerSocket();
660+
sslServerSocket.bind(address);
656661
serverPort = sslServerSocket.getLocalPort();
657662

658663
/*
@@ -717,7 +722,7 @@ void doClientSide() throws Exception {
717722
System.out.println("url is "+url.toString());
718723

719724
try {
720-
http = (HttpsURLConnection)url.openConnection();
725+
http = (HttpsURLConnection)url.openConnection(Proxy.NO_PROXY);
721726

722727
int respCode = http.getResponseCode();
723728
System.out.println("respCode = "+respCode);

test/jdk/sun/net/www/protocol/https/HttpsURLConnection/IPAddressDNSIdentities.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2010, 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
@@ -650,8 +650,13 @@ void doServerSide() throws Exception {
650650
serverModulus, serverPrivateExponent, passphrase);
651651
SSLServerSocketFactory sslssf = context.getServerSocketFactory();
652652

653+
// doClientSide() connects to the loopback address
654+
InetAddress loopback = InetAddress.getLoopbackAddress();
655+
InetSocketAddress address = new InetSocketAddress(loopback, serverPort);
656+
653657
sslServerSocket =
654-
(SSLServerSocket) sslssf.createServerSocket(serverPort);
658+
(SSLServerSocket) sslssf.createServerSocket();
659+
sslServerSocket.bind(address);
655660
serverPort = sslServerSocket.getLocalPort();
656661

657662
/*
@@ -721,7 +726,7 @@ void doClientSide() throws Exception {
721726
System.out.println("url is "+url.toString());
722727

723728
try {
724-
http = (HttpsURLConnection)url.openConnection();
729+
http = (HttpsURLConnection)url.openConnection(Proxy.NO_PROXY);
725730

726731
int respCode = http.getResponseCode();
727732
System.out.println("respCode = " + respCode);

test/jdk/sun/net/www/protocol/https/HttpsURLConnection/IPAddressIPIdentities.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2010, 2015, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2010, 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
@@ -654,8 +654,13 @@ void doServerSide() throws Exception {
654654
serverModulus, serverPrivateExponent, passphrase);
655655
SSLServerSocketFactory sslssf = context.getServerSocketFactory();
656656

657+
// doClientSide() connects to the loopback address
658+
InetAddress loopback = InetAddress.getLoopbackAddress();
659+
InetSocketAddress address = new InetSocketAddress(loopback, serverPort);
660+
657661
sslServerSocket =
658-
(SSLServerSocket) sslssf.createServerSocket(serverPort);
662+
(SSLServerSocket) sslssf.createServerSocket();
663+
sslServerSocket.bind(address);
659664
serverPort = sslServerSocket.getLocalPort();
660665

661666
/*
@@ -725,7 +730,7 @@ void doClientSide() throws Exception {
725730
System.out.println("url is "+url.toString());
726731

727732
try {
728-
http = (HttpsURLConnection)url.openConnection();
733+
http = (HttpsURLConnection)url.openConnection(Proxy.NO_PROXY);
729734

730735
int respCode = http.getResponseCode();
731736
System.out.println("respCode = "+respCode);

test/jdk/sun/net/www/protocol/https/HttpsURLConnection/IPIdentities.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
/* @test
3030
* @summary X509 certificate hostname checking is broken in JDK1.6.0_10
3131
* @bug 6766775
32+
* @library /test/lib
3233
* @run main/othervm IPIdentities
3334
* @author Xuelei Fan
3435
*/
@@ -45,6 +46,7 @@
4546
import java.security.spec.*;
4647
import java.security.interfaces.*;
4748
import java.math.BigInteger;
49+
import jdk.test.lib.net.URIBuilder;
4850

4951
/*
5052
* Certificates and key used in the test.
@@ -652,8 +654,13 @@ void doServerSide() throws Exception {
652654
serverModulus, serverPrivateExponent, passphrase);
653655
SSLServerSocketFactory sslssf = context.getServerSocketFactory();
654656

657+
// doClientSide() connects to the loopback address
658+
InetAddress loopback = InetAddress.getLoopbackAddress();
659+
InetSocketAddress address = new InetSocketAddress(loopback, serverPort);
660+
655661
sslServerSocket =
656-
(SSLServerSocket) sslssf.createServerSocket(serverPort);
662+
(SSLServerSocket) sslssf.createServerSocket();
663+
sslServerSocket.bind(address);
657664
serverPort = sslServerSocket.getLocalPort();
658665

659666
/*
@@ -713,11 +720,16 @@ void doClientSide() throws Exception {
713720
HttpsURLConnection http = null;
714721

715722
/* establish http connection to server */
716-
URL url = new URL("https://localhost:" + serverPort+"/");
723+
URL url = URIBuilder.newBuilder()
724+
.scheme("https")
725+
.loopback()
726+
.port(serverPort)
727+
.path("/")
728+
.toURL();
717729
System.out.println("url is "+url.toString());
718730

719731
try {
720-
http = (HttpsURLConnection)url.openConnection();
732+
http = (HttpsURLConnection)url.openConnection(Proxy.NO_PROXY);
721733

722734
int respCode = http.getResponseCode();
723735
System.out.println("respCode = "+respCode);

test/jdk/sun/net/www/protocol/https/HttpsURLConnection/Identities.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2010, 2015, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2010, 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
@@ -651,8 +651,13 @@ void doServerSide() throws Exception {
651651
serverModulus, serverPrivateExponent, passphrase);
652652
SSLServerSocketFactory sslssf = context.getServerSocketFactory();
653653

654+
// doClientSide() connects to "localhost"
655+
InetAddress localHost = InetAddress.getByName("localhost");
656+
InetSocketAddress address = new InetSocketAddress(localHost, serverPort);
657+
654658
sslServerSocket =
655-
(SSLServerSocket) sslssf.createServerSocket(serverPort);
659+
(SSLServerSocket) sslssf.createServerSocket();
660+
sslServerSocket.bind(address);
656661
serverPort = sslServerSocket.getLocalPort();
657662

658663
/*
@@ -717,7 +722,7 @@ void doClientSide() throws Exception {
717722
System.out.println("url is "+url.toString());
718723

719724
try {
720-
http = (HttpsURLConnection)url.openConnection();
725+
http = (HttpsURLConnection)url.openConnection(Proxy.NO_PROXY);
721726

722727
int respCode = http.getResponseCode();
723728
System.out.println("respCode = "+respCode);

0 commit comments

Comments
 (0)