Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/security/ssl/SSLSocketImpl/SetSoTimeout.java
41152 views
1
/*
2
* Copyright (c) 2020, 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
// SunJSSE does not support dynamic system properties, no way to re-use
25
// system properties in samevm/agentvm mode.
26
27
/*
28
* @test
29
* @bug 8246031
30
* @summary Socket timeouts for getSession.
31
* @run main/othervm SetSoTimeout
32
*/
33
34
import java.io.*;
35
import java.net.*;
36
import java.util.*;
37
import java.security.*;
38
import javax.net.ssl.*;
39
40
public class SetSoTimeout {
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 = "../../../../javax/net/ssl/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
/*
75
* Define the server side of the test.
76
*
77
* If the server prematurely exits, serverReady will be set to true
78
* to avoid infinite hangs.
79
*/
80
void doServerSide() throws Exception {
81
SSLServerSocketFactory sslssf =
82
(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
83
SSLServerSocket sslServerSocket =
84
(SSLServerSocket) sslssf.createServerSocket(serverPort);
85
86
serverPort = sslServerSocket.getLocalPort();
87
88
/*
89
* Signal Client, we're ready for his connect.
90
*/
91
serverReady = true;
92
93
SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept();
94
95
//Make server busy.
96
Thread.sleep(500);
97
98
sslSocket.close();
99
}
100
101
/*
102
* Define the client side of the test.
103
*
104
* If the server prematurely exits, serverReady will be set to true
105
* to avoid infinite hangs.
106
*/
107
void doClientSide() throws Exception {
108
boolean caught = false;
109
110
/*
111
* Wait for server to get started.
112
*/
113
while (!serverReady) {
114
Thread.sleep(50);
115
}
116
117
SSLSocketFactory sslsf = (SSLSocketFactory)SSLSocketFactory.getDefault();
118
SSLSocket sslSocket = (SSLSocket)sslsf.createSocket(
119
InetAddress.getLocalHost(),
120
serverPort );
121
122
sslSocket.setSoTimeout(100); // The stall timeout.
123
124
sslSocket.getSession();
125
//Check connection state.
126
if (!sslSocket.isClosed()) {
127
throw new RuntimeException("Expected connection to be closed");
128
}
129
130
sslSocket.close();
131
132
}
133
134
135
// use any free port by default
136
volatile int serverPort = 0;
137
138
volatile Exception serverException = null;
139
volatile Exception clientException = null;
140
141
142
public static void main(String[] args) throws Exception {
143
String keyFilename =
144
System.getProperty("test.src", "./") + "/" + pathToStores +
145
"/" + keyStoreFile;
146
String trustFilename =
147
System.getProperty("test.src", "./") + "/" + pathToStores +
148
"/" + trustStoreFile;
149
150
System.setProperty("javax.net.ssl.keyStore", keyFilename);
151
System.setProperty("javax.net.ssl.keyStorePassword", passwd);
152
System.setProperty("javax.net.ssl.trustStore", trustFilename);
153
System.setProperty("javax.net.ssl.trustStorePassword", passwd);
154
155
if (debug)
156
System.setProperty("javax.net.debug", "all");
157
158
/*
159
* Start the tests.
160
*/
161
new SetSoTimeout();
162
}
163
164
Thread clientThread = null;
165
Thread serverThread = null;
166
167
/*
168
* Primary constructor, used to drive remainder of the test.
169
*
170
* Fork off the other side, then do your work.
171
*/
172
SetSoTimeout() throws Exception {
173
if (separateServerThread) {
174
startServer(true);
175
startClient(false);
176
} else {
177
startClient(true);
178
startServer(false);
179
}
180
181
/*
182
* Wait for other side to close down.
183
*/
184
if (separateServerThread) {
185
serverThread.join();
186
} else {
187
clientThread.join();
188
}
189
190
/*
191
* When we get here, the test is pretty much over.
192
*
193
* If the main thread excepted, that propagates back
194
* immediately. If the other thread threw an exception, we
195
* should report back.
196
*/
197
if (serverException != null) {
198
System.out.print("Server Exception:");
199
throw serverException;
200
}
201
if (clientException != null) {
202
System.out.print("Client Exception:");
203
throw clientException;
204
}
205
}
206
207
void startServer(boolean newThread) throws Exception {
208
if (newThread) {
209
serverThread = new Thread() {
210
public void run() {
211
try {
212
doServerSide();
213
} catch (Exception e) {
214
/*
215
* Our server thread just died.
216
*
217
* Release the client, if not active already...
218
*/
219
System.err.println("Server died...");
220
System.err.println(e);
221
serverReady = true;
222
serverException = e;
223
}
224
}
225
};
226
serverThread.start();
227
} else {
228
doServerSide();
229
}
230
}
231
232
void startClient(boolean newThread) throws Exception {
233
if (newThread) {
234
clientThread = new Thread() {
235
public void run() {
236
try {
237
doClientSide();
238
} catch (Exception e) {
239
/*
240
* Our client thread just died.
241
*/
242
System.err.println("Client died...");
243
clientException = e;
244
}
245
}
246
};
247
clientThread.start();
248
} else {
249
doClientSide();
250
}
251
}
252
}
253
254