Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/javax/net/ssl/FixingJavadocs/ImplicitHandshake.java
41153 views
1
/*
2
* Copyright (c) 2001, 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 4387882
27
* @summary Need to revisit the javadocs for JSSE, especially the
28
* promoted classes.
29
* @run main/othervm ImplicitHandshake
30
*
31
* SunJSSE does not support dynamic system properties, no way to re-use
32
* system properties in samevm/agentvm mode.
33
* @author Brad Wetmore
34
*/
35
36
import java.io.*;
37
import java.net.*;
38
import javax.net.ssl.*;
39
40
public class ImplicitHandshake {
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
static boolean separateServerThread = true;
54
55
/*
56
* Where do we find the keystores?
57
*/
58
static String pathToStores = "../etc";
59
static String keyStoreFile = "keystore";
60
static String trustStoreFile = "truststore";
61
static String passwd = "passphrase";
62
63
/*
64
* Is the server ready to serve?
65
*/
66
volatile static boolean serverReady = false;
67
68
/*
69
* Turn on SSL debugging?
70
*/
71
static boolean debug = false;
72
73
/*
74
* If the client or server is doing some kind of object creation
75
* that the other side depends on, and that thread prematurely
76
* exits, you may experience a hang. The test harness will
77
* terminate all hung threads after its timeout has expired,
78
* currently 3 minutes by default, but you might try to be
79
* smart about it....
80
*/
81
82
/*
83
* Define the server side of the test.
84
*
85
* If the server prematurely exits, serverReady will be set to true
86
* to avoid infinite hangs.
87
*/
88
void doServerSide() throws Exception {
89
SSLServerSocketFactory sslssf =
90
(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
91
SSLServerSocket sslServerSocket =
92
(SSLServerSocket) sslssf.createServerSocket(serverPort);
93
serverPort = sslServerSocket.getLocalPort();
94
95
/*
96
* Signal Client, we're ready for his connect.
97
*/
98
serverReady = true;
99
100
SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept();
101
102
/*
103
* For grins, let's mix it up here, to make sure you
104
* can have a server really be a client. Don't think
105
* we have a test that does this yet.
106
*/
107
sslSocket.setUseClientMode(true);
108
109
InputStream sslIS = sslSocket.getInputStream();
110
OutputStream sslOS = sslSocket.getOutputStream();
111
112
sslIS.read();
113
sslOS.write(85);
114
sslOS.flush();
115
116
sslSocket.close();
117
}
118
119
/*
120
* Define the client side of the test.
121
*
122
* If the server prematurely exits, serverReady will be set to true
123
* to avoid infinite hangs.
124
*/
125
void doClientSide() throws Exception {
126
127
/*
128
* Wait for server to get started.
129
*/
130
while (!serverReady) {
131
Thread.sleep(50);
132
}
133
134
SSLSocketFactory sslsf =
135
(SSLSocketFactory) SSLSocketFactory.getDefault();
136
SSLSocket sslSocket = (SSLSocket)
137
sslsf.createSocket("localhost", serverPort);
138
139
/*
140
* This is bogus, we should no longer get an tho...
141
*/
142
sslSocket.setUseClientMode(true);
143
sslSocket.setNeedClientAuth(true);
144
145
/*
146
* For grins, let's mix it up here, to make sure you
147
* can have a client really be a server. Don't think
148
* we have a test that does this yet.
149
*/
150
sslSocket.setUseClientMode(false);
151
152
System.out.println("Using Implicit handshake, ciphersuite is: " +
153
sslSocket.getSession().getCipherSuite());
154
155
InputStream sslIS = sslSocket.getInputStream();
156
OutputStream sslOS = sslSocket.getOutputStream();
157
158
sslOS.write(280);
159
sslOS.flush();
160
sslIS.read();
161
162
/*
163
* Lastly, checking that you can't change modes once you've
164
* started/completed handshaking.
165
*/
166
try {
167
sslSocket.setUseClientMode(true);
168
} catch (IllegalArgumentException e) {
169
System.out.println("Caught proper exception");
170
}
171
172
sslSocket.close();
173
}
174
175
/*
176
* =============================================================
177
* The remainder is just support stuff
178
*/
179
180
// use any free port by default
181
volatile int serverPort = 0;
182
183
volatile Exception serverException = null;
184
volatile Exception clientException = null;
185
186
public static void main(String[] args) throws Exception {
187
String keyFilename =
188
System.getProperty("test.src", "./") + "/" + pathToStores +
189
"/" + keyStoreFile;
190
String trustFilename =
191
System.getProperty("test.src", "./") + "/" + pathToStores +
192
"/" + trustStoreFile;
193
194
System.setProperty("javax.net.ssl.keyStore", keyFilename);
195
System.setProperty("javax.net.ssl.keyStorePassword", passwd);
196
System.setProperty("javax.net.ssl.trustStore", trustFilename);
197
System.setProperty("javax.net.ssl.trustStorePassword", passwd);
198
199
if (debug)
200
System.setProperty("javax.net.debug", "all");
201
202
/*
203
* Start the tests.
204
*/
205
new ImplicitHandshake();
206
}
207
208
Thread clientThread = null;
209
Thread serverThread = null;
210
211
/*
212
* Primary constructor, used to drive remainder of the test.
213
*
214
* Fork off the other side, then do your work.
215
*/
216
ImplicitHandshake() throws Exception {
217
if (separateServerThread) {
218
startServer(true);
219
startClient(false);
220
} else {
221
startClient(true);
222
startServer(false);
223
}
224
225
/*
226
* Wait for other side to close down.
227
*/
228
if (separateServerThread) {
229
serverThread.join();
230
} else {
231
clientThread.join();
232
}
233
234
/*
235
* When we get here, the test is pretty much over.
236
*
237
* If the main thread excepted, that propagates back
238
* immediately. If the other thread threw an exception, we
239
* should report back.
240
*/
241
if (serverException != null) {
242
System.out.print("Server Exception:");
243
throw serverException;
244
}
245
if (clientException != null) {
246
System.out.print("Client Exception:");
247
throw clientException;
248
}
249
}
250
251
void startServer(boolean newThread) throws Exception {
252
if (newThread) {
253
serverThread = new Thread() {
254
public void run() {
255
try {
256
doServerSide();
257
} catch (Exception e) {
258
/*
259
* Our server thread just died.
260
*
261
* Release the client, if not active already...
262
*/
263
System.err.println("Server died...");
264
serverReady = true;
265
serverException = e;
266
}
267
}
268
};
269
serverThread.start();
270
} else {
271
doServerSide();
272
}
273
}
274
275
void startClient(boolean newThread) throws Exception {
276
if (newThread) {
277
clientThread = new Thread() {
278
public void run() {
279
try {
280
doClientSide();
281
} catch (Exception e) {
282
/*
283
* Our client thread just died.
284
*/
285
System.err.println("Client died...");
286
clientException = e;
287
}
288
}
289
};
290
clientThread.start();
291
} else {
292
doClientSide();
293
}
294
}
295
}
296
297