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