Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/net/www/protocol/https/HttpsURLConnection/HttpsSocketFacTest.java
41161 views
1
/*
2
* Copyright (c) 2010, 2019, 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
* @test
26
* @bug 6614957
27
* @summary HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
28
* @modules jdk.httpserver
29
* @library /test/lib
30
* @run main/othervm HttpsSocketFacTest
31
*
32
* SunJSSE does not support dynamic system properties, no way to re-use
33
* system properties in samevm/agentvm mode.
34
*/
35
36
import javax.net.SocketFactory;
37
import javax.net.ssl.HostnameVerifier;
38
import javax.net.ssl.HttpsURLConnection;
39
import javax.net.ssl.SSLContext;
40
import javax.net.ssl.SSLSession;
41
import javax.net.ssl.SSLSocketFactory;
42
import java.net.InetAddress;
43
import java.net.InetSocketAddress;
44
import java.net.Socket;
45
import java.net.URL;
46
import java.net.Proxy;
47
import java.security.NoSuchAlgorithmException;
48
import java.io.BufferedWriter;
49
import java.io.InputStream;
50
import java.io.IOException;
51
import java.io.OutputStreamWriter;
52
import com.sun.net.httpserver.HttpExchange;
53
import com.sun.net.httpserver.HttpHandler;
54
import com.sun.net.httpserver.HttpsConfigurator;
55
import jdk.test.lib.net.URIBuilder;
56
57
/*
58
* This class tests that the HTTPS protocol handler is using its socket factory for
59
* creating new Sockets. It does this by wrapping the default SSLSocketFactory with
60
* its own socket factory, SimpleSSLSocketFactory, and verifying that when a https
61
* connection is made one of the socket factories createSocket methods, that
62
* actually creates a Socket, is being invoked by the protocol handler.
63
*/
64
65
public class HttpsSocketFacTest
66
{
67
/*
68
* Where do we find the keystores?
69
*/
70
static String pathToStores = "../../../../../../javax/net/ssl/etc";
71
static String keyStoreFile = "keystore";
72
static String trustStoreFile = "truststore";
73
static String passwd = "passphrase";
74
75
com.sun.net.httpserver.HttpsServer httpsServer;
76
MyHandler httpHandler;
77
78
public static void main(String[] args) {
79
String keyFilename =
80
System.getProperty("test.src", "./") + "/" + pathToStores +
81
"/" + keyStoreFile;
82
String trustFilename =
83
System.getProperty("test.src", "./") + "/" + pathToStores +
84
"/" + trustStoreFile;
85
86
System.setProperty("javax.net.ssl.keyStore", keyFilename);
87
System.setProperty("javax.net.ssl.keyStorePassword", passwd);
88
System.setProperty("javax.net.ssl.trustStore", trustFilename);
89
System.setProperty("javax.net.ssl.trustStorePassword", passwd);
90
91
new HttpsSocketFacTest();
92
}
93
94
public HttpsSocketFacTest() {
95
try {
96
startHttpsServer();
97
doClient();
98
} catch (NoSuchAlgorithmException e) {
99
e.printStackTrace();
100
} catch (IOException ioe) {
101
ioe.printStackTrace();
102
} finally {
103
httpsServer.stop(1);
104
}
105
}
106
107
void doClient() throws IOException {
108
InetSocketAddress address = httpsServer.getAddress();
109
URL url = URIBuilder.newBuilder()
110
.scheme("https")
111
.loopback()
112
.port(address.getPort())
113
.path("/test6614957/")
114
.toURLUnchecked();
115
System.out.println("trying to connect to " + url + "...");
116
117
HttpsURLConnection uc = (HttpsURLConnection) url.openConnection(Proxy.NO_PROXY);
118
SimpleSSLSocketFactory sssf = new SimpleSSLSocketFactory();
119
uc.setSSLSocketFactory(sssf);
120
uc.setHostnameVerifier(new AllHostnameVerifier());
121
InputStream is = uc.getInputStream();
122
123
byte[] ba = new byte[1024];
124
int read = 0;
125
while ((read = is.read(ba)) != -1) {
126
System.out.println(new String(ba, 0, read));
127
}
128
129
System.out.println("SimpleSSLSocketFactory.socketCreated = " + sssf.socketCreated);
130
System.out.println("SimpleSSLSocketFactory.socketWrapped = " + sssf.socketWrapped);
131
132
if (!sssf.socketCreated)
133
throw new RuntimeException("Failed: Socket Factory not being called to create Socket");
134
}
135
136
/**
137
* Https Server
138
*/
139
public void startHttpsServer() throws IOException, NoSuchAlgorithmException {
140
httpsServer = com.sun.net.httpserver.HttpsServer
141
.create(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), 0);
142
httpsServer.createContext("/test6614957/", new MyHandler());
143
httpsServer.setHttpsConfigurator(new HttpsConfigurator(SSLContext.getDefault()));
144
httpsServer.start();
145
}
146
147
class MyHandler implements HttpHandler {
148
private String message = "This is a message!";
149
150
@Override
151
public void handle(HttpExchange t) throws IOException {
152
t.sendResponseHeaders(200, message.length());
153
BufferedWriter writer = new BufferedWriter( new OutputStreamWriter(t.getResponseBody(), "ISO8859-1"));
154
writer.write(message, 0, message.length());
155
writer.close();
156
t.close();
157
}
158
}
159
160
/**
161
* Simple wrapper on default SSLSocketFactory
162
*/
163
class SimpleSSLSocketFactory extends SSLSocketFactory
164
{
165
/*
166
* true if this factory has been used to create a new Socket, i.e.
167
* one of the SocketFactory methods has been called.
168
*/
169
boolean socketCreated = false;
170
171
/*
172
* true if this factory has been used to wrap a Socket, i.e.
173
* the SSLSocketFactory method,
174
* createSocket(Socket, String, int, boolean), has been called.
175
*/
176
boolean socketWrapped = false;
177
178
// methods for SocketFactory
179
@Override
180
public Socket createSocket() throws IOException {
181
socketCreated = true;
182
return SocketFactory.getDefault().createSocket();
183
}
184
185
@Override
186
public Socket createSocket(InetAddress host, int port) throws IOException {
187
socketCreated = true;
188
return SocketFactory.getDefault().createSocket(host, port);
189
}
190
191
@Override
192
public Socket createSocket(InetAddress address, int port, InetAddress localAddress,
193
int localPort) throws IOException {
194
socketCreated = true;
195
return SocketFactory.getDefault().createSocket(address, port, localAddress, localPort);
196
}
197
198
@Override
199
public Socket createSocket(String host, int port) throws IOException {
200
socketCreated = true;
201
return SocketFactory.getDefault().createSocket(host, port);
202
}
203
204
@Override
205
public Socket createSocket(String host, int port, InetAddress localHost,
206
int localPort) throws IOException {
207
socketCreated = true;
208
return SocketFactory.getDefault().createSocket(host, port, localHost, localPort);
209
}
210
211
// methods from SSLSocketFactory
212
@Override
213
public Socket createSocket(Socket s, String host, int port,
214
boolean autoClose) throws IOException {
215
socketWrapped = true;
216
return ((SSLSocketFactory) SSLSocketFactory.getDefault()).createSocket
217
(s, host, port, autoClose);
218
}
219
220
@Override
221
public String[] getDefaultCipherSuites() {
222
return ((SSLSocketFactory) SSLSocketFactory.getDefault()).getDefaultCipherSuites();
223
}
224
225
@Override
226
public String[] getSupportedCipherSuites() {
227
return ((SSLSocketFactory) SSLSocketFactory.getDefault()).getSupportedCipherSuites();
228
}
229
}
230
231
class AllHostnameVerifier implements HostnameVerifier
232
{
233
@Override
234
public boolean verify(String hostname, SSLSession session) {
235
return true;
236
}
237
}
238
}
239
240