Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/security/ssl/AppOutputStream/NoExceptionOnClose.java
41152 views
1
/*
2
* Copyright (c) 2001, 2018, 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 1.3 01/03/08
31
* @bug 4378397
32
* @summary JSSE socket output stream doesn't throw after socket is closed
33
* @run main/othervm NoExceptionOnClose
34
* @author Jaya Hangal
35
*/
36
37
import java.io.*;
38
import java.net.*;
39
import javax.net.ssl.*;
40
41
public class NoExceptionOnClose {
42
43
/*
44
* =============================================================
45
* Set the various variables needed for the tests, then
46
* specify what tests to run on each side.
47
*/
48
49
/*
50
* Should we run the client or server in a separate thread?
51
* Both sides can throw exceptions, but do you have a preference
52
* as to which side should be the main thread.
53
*/
54
static boolean separateServerThread = true;
55
56
/*
57
* Where do we find the keystores?
58
*/
59
static String pathToStores = "../../../../javax/net/ssl/etc";
60
static String keyStoreFile = "keystore";
61
static String trustStoreFile = "truststore";
62
static String passwd = "passphrase";
63
64
/*
65
* Is the server ready to serve?
66
*/
67
volatile static boolean serverReady = false;
68
69
/*
70
* Turn on SSL debugging?
71
*/
72
static boolean debug = false;
73
74
boolean useSSL = true;
75
/*
76
* If the client or server is doing some kind of object creation
77
* that the other side depends on, and that thread prematurely
78
* exits, you may experience a hang. The test harness will
79
* terminate all hung threads after its timeout has expired,
80
* currently 3 minutes by default, but you might try to be
81
* smart about it....
82
*/
83
84
/*
85
* Define the server side of the test.
86
*
87
* If the server prematurely exits, serverReady will be set to true
88
* to avoid infinite hangs.
89
*/
90
void doServerSide() throws Exception {
91
ServerSocket serverSocket;
92
if (useSSL) {
93
SSLServerSocketFactory sslssf =
94
(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
95
serverSocket = (SSLServerSocket) sslssf.
96
createServerSocket(serverPort);
97
} else {
98
serverSocket = new ServerSocket(serverPort);
99
}
100
serverPort = serverSocket.getLocalPort();
101
102
/*
103
* Signal Client, we're ready for his connect.
104
*/
105
serverReady = true;
106
107
Socket socket = serverSocket.accept();
108
InputStream sslIS = socket.getInputStream();
109
OutputStream sslOS = socket.getOutputStream();
110
111
int read = sslIS.read();
112
System.out.println("Server read: " + read);
113
sslOS.write(85);
114
sslOS.flush();
115
socket.close();
116
socket.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
Socket socket;
134
if (useSSL) {
135
SSLSocketFactory sslsf =
136
(SSLSocketFactory) SSLSocketFactory.getDefault();
137
socket = (SSLSocket)
138
sslsf.createSocket("localhost", serverPort);
139
} else
140
socket = new Socket("localhost", serverPort);
141
142
InputStream sslIS = socket.getInputStream();
143
OutputStream sslOS = socket.getOutputStream();
144
145
sslOS.write(80);
146
sslOS.flush();
147
int read = sslIS.read();
148
System.out.println("client read: " + read);
149
socket.close();
150
/*
151
* The socket closed exception must be thrown here
152
*/
153
boolean isSocketClosedThrown = false;
154
try {
155
sslOS.write(22);
156
sslOS.flush();
157
} catch (SSLException | SocketException socketClosed) {
158
System.out.println("Received \"" + socketClosed.getMessage()
159
+ "\" exception as expected");
160
isSocketClosedThrown = true;
161
}
162
if (!isSocketClosedThrown) {
163
throw new Exception("No Exception thrown on write() after"
164
+ " close()");
165
}
166
}
167
168
/*
169
* =============================================================
170
* The remainder is just support stuff
171
*/
172
173
// use any free port by default
174
volatile int serverPort = 0;
175
176
volatile Exception serverException = null;
177
volatile Exception clientException = null;
178
179
public static void main(String[] args) throws Exception {
180
String keyFilename =
181
System.getProperty("test.src", "./") + "/" + pathToStores +
182
"/" + keyStoreFile;
183
String trustFilename =
184
System.getProperty("test.src", "./") + "/" + pathToStores +
185
"/" + trustStoreFile;
186
187
System.setProperty("javax.net.ssl.keyStore", keyFilename);
188
System.setProperty("javax.net.ssl.keyStorePassword", passwd);
189
System.setProperty("javax.net.ssl.trustStore", trustFilename);
190
System.setProperty("javax.net.ssl.trustStorePassword", passwd);
191
192
if (debug)
193
System.setProperty("javax.net.debug", "all");
194
195
/*
196
* Start the tests.
197
*/
198
new NoExceptionOnClose();
199
}
200
201
Thread clientThread = null;
202
Thread serverThread = null;
203
204
/*
205
* Primary constructor, used to drive remainder of the test.
206
*
207
* Fork off the other side, then do your work.
208
*/
209
NoExceptionOnClose() throws Exception {
210
if (separateServerThread) {
211
startServer(true);
212
startClient(false);
213
} else {
214
startClient(true);
215
startServer(false);
216
}
217
218
/*
219
* Wait for other side to close down.
220
*/
221
if (separateServerThread) {
222
serverThread.join();
223
} else {
224
clientThread.join();
225
}
226
227
/*
228
* When we get here, the test is pretty much over.
229
*
230
* If the main thread excepted, that propagates back
231
* immediately. If the other thread threw an exception, we
232
* should report back.
233
*/
234
if (serverException != null)
235
throw serverException;
236
if (clientException != null)
237
throw clientException;
238
}
239
240
void startServer(boolean newThread) throws Exception {
241
if (newThread) {
242
serverThread = new Thread() {
243
public void run() {
244
try {
245
doServerSide();
246
} catch (Exception e) {
247
/*
248
* Our server thread just died.
249
*
250
* Release the client, if not active already...
251
*/
252
System.err.println("Server died...");
253
serverReady = true;
254
serverException = e;
255
}
256
}
257
};
258
serverThread.start();
259
} else {
260
doServerSide();
261
}
262
}
263
264
void startClient(boolean newThread) throws Exception {
265
if (newThread) {
266
clientThread = new Thread() {
267
public void run() {
268
try {
269
doClientSide();
270
} catch (Exception e) {
271
/*
272
* Our client thread just died.
273
*/
274
System.err.println("Client died...");
275
clientException = e;
276
}
277
}
278
};
279
clientThread.start();
280
} else {
281
doClientSide();
282
}
283
}
284
}
285
286