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/HttpsCreateSockTest.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 6771432
27
* @summary createSocket() - smpatch fails using 1.6.0_10 because of
28
* "Unconnected sockets not implemented"
29
* @run main/othervm HttpsCreateSockTest
30
*
31
* SunJSSE does not support dynamic system properties, no way to re-use
32
* system properties in samevm/agentvm mode.
33
*/
34
35
import javax.net.SocketFactory;
36
import javax.net.ssl.HostnameVerifier;
37
import javax.net.ssl.HttpsURLConnection;
38
import javax.net.ssl.SSLContext;
39
import javax.net.ssl.SSLSession;
40
import javax.net.ssl.SSLSocketFactory;
41
import java.security.NoSuchAlgorithmException;
42
import java.net.InetAddress;
43
import java.net.InetSocketAddress;
44
import java.net.Socket;
45
import java.net.URL;
46
import java.io.BufferedWriter;
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 HttpsCreateSockTest
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 HttpsCreateSockTest();
88
}
89
90
public HttpsCreateSockTest() {
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
106
URL url = new URL("https://localhost:" + address.getPort() + "/");
107
System.out.println("trying to connect to " + url + "...");
108
109
HttpsURLConnection uc = (HttpsURLConnection) url.openConnection();
110
uc.setHostnameVerifier(new AllHostnameVerifier());
111
if (uc instanceof javax.net.ssl.HttpsURLConnection) {
112
((javax.net.ssl.HttpsURLConnection) uc).setSSLSocketFactory(new SimpleSSLSocketFactory());
113
System.out.println("Using TestSocketFactory");
114
}
115
uc.connect();
116
System.out.println("CONNECTED " + uc);
117
System.out.println(uc.getResponseMessage());
118
uc.disconnect();
119
}
120
121
/**
122
* Https Server
123
*/
124
public void startHttpsServer() throws IOException, NoSuchAlgorithmException {
125
httpsServer = com.sun.net.httpserver.HttpsServer.create(new InetSocketAddress(0), 0);
126
httpsServer.createContext("/", new MyHandler());
127
httpsServer.setHttpsConfigurator(new HttpsConfigurator(SSLContext.getDefault()));
128
httpsServer.start();
129
}
130
131
class MyHandler implements HttpHandler {
132
private String message = "This is a message!";
133
134
@Override
135
public void handle(HttpExchange t) throws IOException {
136
t.sendResponseHeaders(200, message.length());
137
BufferedWriter writer = new BufferedWriter( new OutputStreamWriter(t.getResponseBody(), "ISO8859-1"));
138
writer.write(message, 0, message.length());
139
writer.close();
140
t.close();
141
}
142
}
143
144
/**
145
* Simple wrapper on default SSLSocketFactory
146
*/
147
class SimpleSSLSocketFactory extends SSLSocketFactory
148
{
149
/*
150
* true if this factory has been used to create a new Socket, i.e.
151
* one of the SocketFactory methods has been called.
152
*/
153
boolean socketCreated = false;
154
155
/*
156
* true if this factory has been used to wrap a Socket, i.e.
157
* the SSLSocketFactory method,
158
* createSocket(Socket, String, int, boolean), has been called.
159
*/
160
boolean socketWrapped = false;
161
162
@Override
163
public Socket createSocket(InetAddress host, int port) throws IOException {
164
socketCreated = true;
165
return SocketFactory.getDefault().createSocket(host, port);
166
}
167
168
@Override
169
public Socket createSocket(InetAddress address, int port, InetAddress localAddress,
170
int localPort) throws IOException {
171
socketCreated = true;
172
return SocketFactory.getDefault().createSocket(address, port, localAddress, localPort);
173
}
174
175
@Override
176
public Socket createSocket(String host, int port) throws IOException {
177
socketCreated = true;
178
return SocketFactory.getDefault().createSocket(host, port);
179
}
180
181
@Override
182
public Socket createSocket(String host, int port, InetAddress localHost,
183
int localPort) throws IOException {
184
socketCreated = true;
185
return SocketFactory.getDefault().createSocket(host, port, localHost, localPort);
186
}
187
188
// methods from SSLSocketFactory
189
@Override
190
public Socket createSocket(Socket s, String host, int port,
191
boolean autoClose) throws IOException {
192
socketWrapped = true;
193
return ((SSLSocketFactory) SSLSocketFactory.getDefault()).createSocket
194
(s, host, port, autoClose);
195
}
196
197
@Override
198
public String[] getDefaultCipherSuites() {
199
return ((SSLSocketFactory) SSLSocketFactory.getDefault()).getDefaultCipherSuites();
200
}
201
202
@Override
203
public String[] getSupportedCipherSuites() {
204
return ((SSLSocketFactory) SSLSocketFactory.getDefault()).getSupportedCipherSuites();
205
}
206
}
207
208
class AllHostnameVerifier implements HostnameVerifier
209
{
210
@Override
211
public boolean verify(String hostname, SSLSession session) {
212
return true;
213
}
214
}
215
}
216
217