Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/javax/net/ssl/ServerName/BestEffortOnLazyConnected.java
41152 views
1
/*
2
* Copyright (c) 2016, 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 8144566
32
* @summary Custom HostnameVerifier disables SNI extension
33
* @run main/othervm BestEffortOnLazyConnected
34
*/
35
36
import java.io.*;
37
import java.net.*;
38
import javax.net.ssl.*;
39
40
public class BestEffortOnLazyConnected {
41
42
/*
43
* =============================================================
44
* Set the various variables needed for the tests, then
45
* specify what tests to run on each side.
46
*/
47
48
/*
49
* Should we run the client or server in a separate thread?
50
* Both sides can throw exceptions, but do you have a preference
51
* as to which side should be the main thread.
52
*/
53
private static final boolean separateServerThread = true;
54
55
/*
56
* Where do we find the keystores?
57
*/
58
private static final String pathToStores = "../etc";
59
private static final String keyStoreFile = "keystore";
60
private static final String trustStoreFile = "truststore";
61
private static final String passwd = "passphrase";
62
63
/*
64
* Is the server ready to serve?
65
*/
66
private static volatile boolean serverReady = false;
67
68
/*
69
* Turn on SSL debugging?
70
*/
71
private static final boolean debug = false;
72
73
/*
74
* the fully qualified domain name of localhost
75
*/
76
private static String hostname = null;
77
78
/*
79
* If the client or server is doing some kind of object creation
80
* that the other side depends on, and that thread prematurely
81
* exits, you may experience a hang. The test harness will
82
* terminate all hung threads after its timeout has expired,
83
* currently 3 minutes by default, but you might try to be
84
* smart about it....
85
*/
86
87
/*
88
* Define the server side of the test.
89
*
90
* If the server prematurely exits, serverReady will be set to true
91
* to avoid infinite hangs.
92
*/
93
private void doServerSide() throws Exception {
94
SSLServerSocketFactory sslssf =
95
(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
96
try (SSLServerSocket sslServerSocket =
97
(SSLServerSocket) sslssf.createServerSocket(serverPort)) {
98
99
serverPort = sslServerSocket.getLocalPort();
100
101
/*
102
* Signal Client, we're ready for his connect.
103
*/
104
serverReady = true;
105
106
try (SSLSocket sslSocket = (SSLSocket)sslServerSocket.accept()) {
107
InputStream sslIS = sslSocket.getInputStream();
108
OutputStream sslOS = sslSocket.getOutputStream();
109
110
sslIS.read();
111
sslOS.write(85);
112
sslOS.flush();
113
114
ExtendedSSLSession session =
115
(ExtendedSSLSession)sslSocket.getSession();
116
if (session.getRequestedServerNames().isEmpty()) {
117
throw new Exception("No expected Server Name Indication");
118
}
119
}
120
}
121
}
122
123
/*
124
* Define the client side of the test.
125
*
126
* If the server prematurely exits, serverReady will be set to true
127
* to avoid infinite hangs.
128
*/
129
private void doClientSide() throws Exception {
130
131
/*
132
* Wait for server to get started.
133
*/
134
while (!serverReady) {
135
Thread.sleep(50);
136
}
137
138
SSLSocketFactory sslsf =
139
(SSLSocketFactory) SSLSocketFactory.getDefault();
140
141
try (SSLSocket sslSocket = (SSLSocket)sslsf.createSocket()) {
142
143
sslSocket.connect(new InetSocketAddress(hostname, serverPort), 0);
144
145
InputStream sslIS = sslSocket.getInputStream();
146
OutputStream sslOS = sslSocket.getOutputStream();
147
148
sslOS.write(280);
149
sslOS.flush();
150
sslIS.read();
151
}
152
}
153
154
155
/*
156
* =============================================================
157
* The remainder is just support stuff
158
*/
159
160
// use any free port by default
161
private volatile int serverPort = 0;
162
163
private volatile Exception serverException = null;
164
private volatile Exception clientException = null;
165
166
public static void main(String[] args) throws Exception {
167
String keyFilename =
168
System.getProperty("test.src", ".") + "/" + pathToStores +
169
"/" + keyStoreFile;
170
String trustFilename =
171
System.getProperty("test.src", ".") + "/" + pathToStores +
172
"/" + trustStoreFile;
173
174
System.setProperty("javax.net.ssl.keyStore", keyFilename);
175
System.setProperty("javax.net.ssl.keyStorePassword", passwd);
176
System.setProperty("javax.net.ssl.trustStore", trustFilename);
177
System.setProperty("javax.net.ssl.trustStorePassword", passwd);
178
179
if (debug) {
180
System.setProperty("javax.net.debug", "all");
181
}
182
183
try {
184
hostname = InetAddress.getLocalHost().getCanonicalHostName();
185
} catch (UnknownHostException uhe) {
186
System.out.println(
187
"Ignore the test as the local hostname cannot be determined");
188
189
return;
190
}
191
192
System.out.println(
193
"The fully qualified domain name of the local host is " +
194
hostname);
195
// Ignore the test if the hostname does not sound like a domain name.
196
if ((hostname == null) || hostname.isEmpty() ||
197
!hostname.contains(".") || hostname.endsWith(".") ||
198
hostname.startsWith("localhost") ||
199
Character.isDigit(hostname.charAt(hostname.length() - 1))) {
200
201
System.out.println("Ignore the test as the local hostname " +
202
"cannot be determined as fully qualified domain name");
203
204
return;
205
}
206
207
/*
208
* Start the tests.
209
*/
210
new BestEffortOnLazyConnected();
211
}
212
213
private Thread clientThread = null;
214
private Thread serverThread = null;
215
216
/*
217
* Primary constructor, used to drive remainder of the test.
218
*
219
* Fork off the other side, then do your work.
220
*/
221
BestEffortOnLazyConnected() throws Exception {
222
try {
223
if (separateServerThread) {
224
startServer(true);
225
startClient(false);
226
} else {
227
startClient(true);
228
startServer(false);
229
}
230
} catch (Exception e) {
231
// swallow for now. Show later
232
}
233
234
/*
235
* Wait for other side to close down.
236
*/
237
if (separateServerThread) {
238
serverThread.join();
239
} else {
240
clientThread.join();
241
}
242
243
/*
244
* When we get here, the test is pretty much over.
245
* Which side threw the error?
246
*/
247
Exception local;
248
Exception remote;
249
String whichRemote;
250
251
if (separateServerThread) {
252
remote = serverException;
253
local = clientException;
254
whichRemote = "server";
255
} else {
256
remote = clientException;
257
local = serverException;
258
whichRemote = "client";
259
}
260
261
/*
262
* If both failed, return the curthread's exception, but also
263
* print the remote side Exception
264
*/
265
if ((local != null) && (remote != null)) {
266
System.out.println(whichRemote + " also threw:");
267
remote.printStackTrace();
268
System.out.println();
269
throw local;
270
}
271
272
if (remote != null) {
273
throw remote;
274
}
275
276
if (local != null) {
277
throw local;
278
}
279
}
280
281
private void startServer(boolean newThread) throws Exception {
282
if (newThread) {
283
serverThread = new Thread() {
284
public void run() {
285
try {
286
doServerSide();
287
} catch (Exception e) {
288
/*
289
* Our server thread just died.
290
*
291
* Release the client, if not active already...
292
*/
293
System.err.println("Server died...");
294
serverReady = true;
295
serverException = e;
296
}
297
}
298
};
299
serverThread.start();
300
} else {
301
try {
302
doServerSide();
303
} catch (Exception e) {
304
serverException = e;
305
} finally {
306
serverReady = true;
307
}
308
}
309
}
310
311
private void startClient(boolean newThread) throws Exception {
312
if (newThread) {
313
clientThread = new Thread() {
314
public void run() {
315
try {
316
doClientSide();
317
} catch (Exception e) {
318
/*
319
* Our client thread just died.
320
*/
321
System.err.println("Client died...");
322
clientException = e;
323
}
324
}
325
};
326
clientThread.start();
327
} else {
328
try {
329
doClientSide();
330
} catch (Exception e) {
331
clientException = e;
332
}
333
}
334
}
335
}
336
337