Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/test/jdk/sun/net/www/protocol/https/HttpsURLConnection/B6216082.java
66649 views
1
/*
2
* Copyright (c) 2005, 2021, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
//
25
// SunJSSE does not support dynamic system properties, no way to re-use
26
// system properties in samevm/agentvm mode.
27
//
28
29
/*
30
* @test
31
* @bug 6216082
32
* @summary Redirect problem with HttpsURLConnection using a proxy
33
* @modules java.base/sun.net.www
34
* @library .. /test/lib
35
* @build jdk.test.lib.NetworkConfiguration
36
* jdk.test.lib.Platform
37
* ClosedChannelList
38
* TunnelProxy
39
* @key intermittent
40
* @run main/othervm B6216082
41
*/
42
43
import java.io.FileInputStream;
44
import java.net.HttpURLConnection;
45
import java.net.InetAddress;
46
import java.net.InetSocketAddress;
47
import java.net.NetworkInterface;
48
import java.net.ProxySelector;
49
import java.net.URL;
50
import java.security.KeyStore;
51
import java.util.Optional;
52
import java.util.concurrent.Executors;
53
54
import javax.net.ssl.HostnameVerifier;
55
import javax.net.ssl.HttpsURLConnection;
56
import javax.net.ssl.KeyManagerFactory;
57
import javax.net.ssl.SSLContext;
58
import javax.net.ssl.SSLSession;
59
import javax.net.ssl.TrustManagerFactory;
60
61
import com.sun.net.httpserver.HttpExchange;
62
import com.sun.net.httpserver.HttpHandler;
63
import com.sun.net.httpserver.HttpsConfigurator;
64
import com.sun.net.httpserver.HttpsServer;
65
import jdk.test.lib.NetworkConfiguration;
66
67
public class B6216082 {
68
static SimpleHttpTransaction httpTrans;
69
static HttpsServer server;
70
static TunnelProxy proxy;
71
72
// it seems there's no proxy ever if a url points to 'localhost',
73
// even if proxy related properties are set. so we need to bind
74
// our simple http proxy and http server to a non-loopback address
75
static InetAddress firstNonLoAddress = null;
76
77
public static void main(String[] args) throws Exception {
78
HostnameVerifier reservedHV =
79
HttpsURLConnection.getDefaultHostnameVerifier();
80
try {
81
// XXX workaround for CNFE
82
Class.forName("java.nio.channels.ClosedByInterruptException");
83
if (!setupEnv()) {
84
return;
85
}
86
startHttpServer();
87
// https.proxyPort can only be set after the TunnelProxy has been
88
// created as it will use an ephemeral port.
89
ProxySelector.setDefault(ProxySelector.of(new InetSocketAddress(firstNonLoAddress, proxy.getLocalPort())));
90
makeHttpCall();
91
} finally {
92
if (proxy != null) {
93
proxy.terminate();
94
}
95
if (server != null) {
96
server.stop(1);
97
}
98
HttpsURLConnection.setDefaultHostnameVerifier(reservedHV);
99
}
100
}
101
102
/*
103
* Where do we find the keystores for ssl?
104
*/
105
static String pathToStores = "../../../../../../javax/net/ssl/etc";
106
static String keyStoreFile = "keystore";
107
static String trustStoreFile = "truststore";
108
static String passwd = "passphrase";
109
public static boolean setupEnv() throws Exception {
110
firstNonLoAddress = getNonLoAddress();
111
if (firstNonLoAddress == null) {
112
System.err.println("The test needs at least one non-loopback address to run. Quit now.");
113
return false;
114
}
115
System.out.println(firstNonLoAddress.getHostAddress());
116
// will use proxy
117
System.setProperty( "https.proxyHost", firstNonLoAddress.getHostAddress());
118
119
// setup properties to do ssl
120
String keyFilename = System.getProperty("test.src", "./") + "/" +
121
pathToStores + "/" + keyStoreFile;
122
String trustFilename = System.getProperty("test.src", "./") + "/" +
123
pathToStores + "/" + trustStoreFile;
124
125
System.setProperty("javax.net.ssl.keyStore", keyFilename);
126
System.setProperty("javax.net.ssl.keyStorePassword", passwd);
127
System.setProperty("javax.net.ssl.trustStore", trustFilename);
128
System.setProperty("javax.net.ssl.trustStorePassword", passwd);
129
HttpsURLConnection.setDefaultHostnameVerifier(new NameVerifier());
130
return true;
131
}
132
133
public static InetAddress getNonLoAddress() throws Exception {
134
InetAddress lh = InetAddress.getByName("localhost");
135
NetworkInterface loNIC = NetworkInterface.getByInetAddress(lh);
136
137
NetworkConfiguration nc = NetworkConfiguration.probe();
138
Optional<InetAddress> oaddr = nc.interfaces()
139
.filter(nif -> !nif.getName().equalsIgnoreCase(loNIC.getName()))
140
.flatMap(nif -> nc.addresses(nif))
141
.filter(a -> !a.isLoopbackAddress())
142
.findFirst();
143
144
return oaddr.orElseGet(() -> null);
145
}
146
147
public static void startHttpServer() throws Exception {
148
// Both the https server and the proxy let the
149
// system pick up an ephemeral port.
150
httpTrans = new SimpleHttpTransaction();
151
// create and initialize a SSLContext
152
KeyStore ks = KeyStore.getInstance("JKS");
153
KeyStore ts = KeyStore.getInstance("JKS");
154
char[] passphrase = "passphrase".toCharArray();
155
156
ks.load(new FileInputStream(System.getProperty("javax.net.ssl.keyStore")), passphrase);
157
ts.load(new FileInputStream(System.getProperty("javax.net.ssl.trustStore")), passphrase);
158
159
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
160
kmf.init(ks, passphrase);
161
162
TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
163
tmf.init(ts);
164
165
SSLContext sslCtx = SSLContext.getInstance("TLS");
166
167
sslCtx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
168
169
server = HttpsServer.create(new InetSocketAddress(firstNonLoAddress, 0), 10);
170
server.setHttpsConfigurator(new HttpsConfigurator(sslCtx));
171
server.createContext("/", httpTrans);
172
server.setExecutor(Executors.newSingleThreadExecutor());
173
server.start();
174
proxy = new TunnelProxy(1, 10, firstNonLoAddress, 0);
175
}
176
177
public static void makeHttpCall() throws Exception {
178
System.out.println("https server listen on: " + server.getAddress().getPort());
179
System.out.println("https proxy listen on: " + proxy.getLocalPort());
180
URL url = new URL("https" , firstNonLoAddress.getHostAddress(),
181
server.getAddress().getPort(), "/");
182
HttpURLConnection uc = (HttpURLConnection)url.openConnection();
183
System.out.println(uc.getResponseCode());
184
if(uc.getResponseCode() != 200) {
185
uc.disconnect();
186
throw new RuntimeException("Test failed : bad http request with response code : "+ uc.getResponseCode());
187
}
188
uc.disconnect();
189
}
190
191
static class NameVerifier implements HostnameVerifier {
192
public boolean verify(String hostname, SSLSession session) {
193
return true;
194
}
195
}
196
}
197
198
class SimpleHttpTransaction implements HttpHandler {
199
200
/*
201
* Our http server which simply redirect first call
202
*/
203
public void handle(HttpExchange trans) {
204
try {
205
String path = trans.getRequestURI().getPath();
206
if (path.equals("/")) {
207
// the first call, redirect it
208
String location = "/redirect";
209
trans.getResponseHeaders().set("Location", location);
210
trans.sendResponseHeaders(302, -1);
211
} else {
212
trans.sendResponseHeaders(200, -1);
213
}
214
} catch (Exception e) {
215
throw new RuntimeException(e);
216
}
217
}
218
}
219
220