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/HttpsCreateSockTest.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 6771432
27
* @summary createSocket() - smpatch fails using 1.6.0_10 because of
28
* "Unconnected sockets not implemented"
29
* @modules jdk.httpserver
30
* @library /test/lib
31
* @run main/othervm HttpsCreateSockTest
32
* @run main/othervm -Djava.net.preferIPv6Addresses=true HttpsCreateSockTest
33
*
34
* SunJSSE does not support dynamic system properties, no way to re-use
35
* system properties in samevm/agentvm mode.
36
*/
37
38
import javax.net.SocketFactory;
39
import javax.net.ssl.HostnameVerifier;
40
import javax.net.ssl.HttpsURLConnection;
41
import javax.net.ssl.SSLContext;
42
import javax.net.ssl.SSLSession;
43
import javax.net.ssl.SSLSocketFactory;
44
import java.security.NoSuchAlgorithmException;
45
import java.net.InetAddress;
46
import java.net.InetSocketAddress;
47
import java.net.Proxy;
48
import java.net.Socket;
49
import java.net.URL;
50
import java.io.BufferedWriter;
51
import java.io.IOException;
52
import java.io.OutputStreamWriter;
53
import com.sun.net.httpserver.HttpExchange;
54
import com.sun.net.httpserver.HttpHandler;
55
import com.sun.net.httpserver.HttpsConfigurator;
56
57
import jdk.test.lib.net.URIBuilder;
58
59
/*
60
* This class tests that the HTTPS protocol handler is using its socket factory for
61
* creating new Sockets. It does this by wrapping the default SSLSocketFactory with
62
* its own socket factory, SimpleSSLSocketFactory, and verifying that when a https
63
* connection is made one of the socket factories createSocket methods, that
64
* actually creates a Socket, is being invoked by the protocol handler.
65
*/
66
67
public class HttpsCreateSockTest
68
{
69
/*
70
* Where do we find the keystores?
71
*/
72
static String pathToStores = "../../../../../../javax/net/ssl/etc";
73
static String keyStoreFile = "keystore";
74
static String trustStoreFile = "truststore";
75
static String passwd = "passphrase";
76
77
com.sun.net.httpserver.HttpsServer httpsServer;
78
MyHandler httpHandler;
79
80
public static void main(String[] args) {
81
String keyFilename =
82
System.getProperty("test.src", "./") + "/" + pathToStores +
83
"/" + keyStoreFile;
84
String trustFilename =
85
System.getProperty("test.src", "./") + "/" + pathToStores +
86
"/" + trustStoreFile;
87
88
System.setProperty("javax.net.ssl.keyStore", keyFilename);
89
System.setProperty("javax.net.ssl.keyStorePassword", passwd);
90
System.setProperty("javax.net.ssl.trustStore", trustFilename);
91
System.setProperty("javax.net.ssl.trustStorePassword", passwd);
92
93
new HttpsCreateSockTest();
94
}
95
96
public HttpsCreateSockTest() {
97
try {
98
startHttpsServer();
99
doClient();
100
} catch (NoSuchAlgorithmException e) {
101
e.printStackTrace();
102
} catch (IOException ioe) {
103
ioe.printStackTrace();
104
} finally {
105
httpsServer.stop(1);
106
}
107
}
108
109
void doClient() throws IOException {
110
InetSocketAddress address = httpsServer.getAddress();
111
112
URL url = URIBuilder.newBuilder()
113
.scheme("https")
114
.host(address.getAddress())
115
.port(address.getPort())
116
.path("/")
117
.toURLUnchecked();
118
System.out.println("trying to connect to " + url + "...");
119
120
HttpsURLConnection uc = (HttpsURLConnection) url.openConnection(Proxy.NO_PROXY);
121
uc.setHostnameVerifier(new AllHostnameVerifier());
122
if (uc instanceof javax.net.ssl.HttpsURLConnection) {
123
((javax.net.ssl.HttpsURLConnection) uc).setSSLSocketFactory(new SimpleSSLSocketFactory());
124
System.out.println("Using TestSocketFactory");
125
}
126
uc.connect();
127
System.out.println("CONNECTED " + uc);
128
System.out.println(uc.getResponseMessage());
129
uc.disconnect();
130
}
131
132
/**
133
* Https Server
134
*/
135
public void startHttpsServer() throws IOException, NoSuchAlgorithmException {
136
InetAddress loopback = InetAddress.getLoopbackAddress();
137
InetSocketAddress address = new InetSocketAddress(loopback, 0);
138
httpsServer = com.sun.net.httpserver.HttpsServer.create(address, 0);
139
httpsServer.createContext("/", new MyHandler());
140
httpsServer.setHttpsConfigurator(new HttpsConfigurator(SSLContext.getDefault()));
141
httpsServer.start();
142
}
143
144
class MyHandler implements HttpHandler {
145
private String message = "This is a message!";
146
147
@Override
148
public void handle(HttpExchange t) throws IOException {
149
t.sendResponseHeaders(200, message.length());
150
BufferedWriter writer = new BufferedWriter( new OutputStreamWriter(t.getResponseBody(), "ISO8859-1"));
151
writer.write(message, 0, message.length());
152
writer.close();
153
t.close();
154
}
155
}
156
157
/**
158
* Simple wrapper on default SSLSocketFactory
159
*/
160
class SimpleSSLSocketFactory extends SSLSocketFactory
161
{
162
/*
163
* true if this factory has been used to create a new Socket, i.e.
164
* one of the SocketFactory methods has been called.
165
*/
166
boolean socketCreated = false;
167
168
/*
169
* true if this factory has been used to wrap a Socket, i.e.
170
* the SSLSocketFactory method,
171
* createSocket(Socket, String, int, boolean), has been called.
172
*/
173
boolean socketWrapped = false;
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