author | phh |
Sat, 30 Nov 2019 14:33:05 -0800 | |
changeset 59330 | 5b96c12f909d |
parent 47874 | a7d101e56b36 |
permissions | -rw-r--r-- |
47271 | 1 |
/* |
2 |
* Copyright (c) 2017, 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 8184328 |
|
27 |
* @summary JDK8u131-b34-socketRead0 hang at SSL read |
|
28 |
* @run main/othervm SSLSocketCloseHang |
|
29 |
*/ |
|
30 |
||
31 |
import java.io.*; |
|
32 |
import java.net.*; |
|
33 |
import java.util.*; |
|
34 |
import java.security.*; |
|
35 |
import javax.net.ssl.*; |
|
36 |
||
37 |
public class SSLSocketCloseHang { |
|
38 |
||
39 |
/* |
|
40 |
* ============================================================= |
|
41 |
* Set the various variables needed for the tests, then |
|
42 |
* specify what tests to run on each side. |
|
43 |
*/ |
|
44 |
||
45 |
/* |
|
46 |
* Should we run the client or server in a separate thread? |
|
47 |
* Both sides can throw exceptions, but do you have a preference |
|
48 |
* as to which side should be the main thread. |
|
49 |
*/ |
|
50 |
static boolean separateServerThread = true; |
|
51 |
||
52 |
/* |
|
53 |
* Where do we find the keystores? |
|
54 |
*/ |
|
55 |
static String pathToStores = "../../../../javax/net/ssl/etc"; |
|
56 |
static String keyStoreFile = "keystore"; |
|
57 |
static String trustStoreFile = "truststore"; |
|
58 |
static String passwd = "passphrase"; |
|
59 |
||
60 |
/* |
|
61 |
* Is the server ready to serve? |
|
62 |
*/ |
|
63 |
volatile static boolean serverReady = false; |
|
64 |
||
65 |
/* |
|
66 |
* Was the client responsible for closing the socket |
|
67 |
*/ |
|
68 |
volatile static boolean clientClosed = false; |
|
69 |
||
70 |
/* |
|
71 |
* Turn on SSL debugging? |
|
72 |
*/ |
|
73 |
static boolean debug = false; |
|
74 |
||
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 |
SSLServerSocketFactory sslssf = |
|
92 |
(SSLServerSocketFactory) SSLServerSocketFactory.getDefault(); |
|
93 |
SSLServerSocket sslServerSocket = |
|
94 |
(SSLServerSocket) sslssf.createServerSocket(serverPort); |
|
95 |
||
96 |
serverPort = sslServerSocket.getLocalPort(); |
|
97 |
||
98 |
/* |
|
99 |
* Signal Client, we're ready for his connect. |
|
100 |
*/ |
|
101 |
serverReady = true; |
|
102 |
||
47874
a7d101e56b36
8189646: sun/security/ssl/SSLSocketImpl/SSLSocketCloseHang.java failed with "java.net.SocketTimeoutException: Read timed out"
robm
parents:
47271
diff
changeset
|
103 |
System.err.println("Server accepting: " + System.nanoTime()); |
47271 | 104 |
SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept(); |
47874
a7d101e56b36
8189646: sun/security/ssl/SSLSocketImpl/SSLSocketCloseHang.java failed with "java.net.SocketTimeoutException: Read timed out"
robm
parents:
47271
diff
changeset
|
105 |
System.err.println("Server accepted: " + System.nanoTime()); |
47271 | 106 |
sslSocket.startHandshake(); |
47874
a7d101e56b36
8189646: sun/security/ssl/SSLSocketImpl/SSLSocketCloseHang.java failed with "java.net.SocketTimeoutException: Read timed out"
robm
parents:
47271
diff
changeset
|
107 |
System.err.println("Server handshake complete: " + System.nanoTime()); |
47271 | 108 |
while (!clientClosed) { |
109 |
Thread.sleep(500); |
|
110 |
} |
|
111 |
} |
|
112 |
||
113 |
/* |
|
114 |
* Define the client side of the test. |
|
115 |
* |
|
116 |
* If the server prematurely exits, serverReady will be set to true |
|
117 |
* to avoid infinite hangs. |
|
118 |
*/ |
|
119 |
void doClientSide() throws Exception { |
|
120 |
boolean caught = false; |
|
121 |
||
122 |
/* |
|
123 |
* Wait for server to get started. |
|
124 |
*/ |
|
125 |
System.out.println("waiting on server"); |
|
126 |
while (!serverReady) { |
|
127 |
Thread.sleep(50); |
|
128 |
} |
|
47874
a7d101e56b36
8189646: sun/security/ssl/SSLSocketImpl/SSLSocketCloseHang.java failed with "java.net.SocketTimeoutException: Read timed out"
robm
parents:
47271
diff
changeset
|
129 |
Thread.sleep(500); |
47271 | 130 |
System.out.println("server ready"); |
131 |
||
132 |
Socket baseSocket = new Socket("localhost", serverPort); |
|
47874
a7d101e56b36
8189646: sun/security/ssl/SSLSocketImpl/SSLSocketCloseHang.java failed with "java.net.SocketTimeoutException: Read timed out"
robm
parents:
47271
diff
changeset
|
133 |
baseSocket.setSoTimeout(1000); |
47271 | 134 |
|
135 |
SSLSocketFactory sslsf = |
|
136 |
(SSLSocketFactory) SSLSocketFactory.getDefault(); |
|
137 |
SSLSocket sslSocket = (SSLSocket) |
|
138 |
sslsf.createSocket(baseSocket, "localhost", serverPort, false); |
|
139 |
||
140 |
// handshaking |
|
47874
a7d101e56b36
8189646: sun/security/ssl/SSLSocketImpl/SSLSocketCloseHang.java failed with "java.net.SocketTimeoutException: Read timed out"
robm
parents:
47271
diff
changeset
|
141 |
System.err.println("Client starting handshake: " + System.nanoTime()); |
47271 | 142 |
sslSocket.startHandshake(); |
47874
a7d101e56b36
8189646: sun/security/ssl/SSLSocketImpl/SSLSocketCloseHang.java failed with "java.net.SocketTimeoutException: Read timed out"
robm
parents:
47271
diff
changeset
|
143 |
System.err.println("Client handshake done: " + System.nanoTime()); |
47271 | 144 |
|
145 |
Thread.sleep(500); |
|
47874
a7d101e56b36
8189646: sun/security/ssl/SSLSocketImpl/SSLSocketCloseHang.java failed with "java.net.SocketTimeoutException: Read timed out"
robm
parents:
47271
diff
changeset
|
146 |
System.err.println("Client closing: " + System.nanoTime()); |
47271 | 147 |
|
148 |
sslSocket.close(); |
|
149 |
clientClosed = true; |
|
47874
a7d101e56b36
8189646: sun/security/ssl/SSLSocketImpl/SSLSocketCloseHang.java failed with "java.net.SocketTimeoutException: Read timed out"
robm
parents:
47271
diff
changeset
|
150 |
System.err.println("Client closed: " + System.nanoTime()); |
47271 | 151 |
} |
152 |
||
153 |
/* |
|
154 |
* ============================================================= |
|
155 |
* The remainder is just support stuff |
|
156 |
*/ |
|
157 |
||
158 |
// use any free port by default |
|
159 |
volatile int serverPort = 0; |
|
160 |
||
161 |
volatile Exception serverException = null; |
|
162 |
volatile Exception clientException = null; |
|
163 |
||
164 |
volatile byte[] serverDigest = 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 |
* Start the tests. |
|
184 |
*/ |
|
185 |
new SSLSocketCloseHang(); |
|
186 |
} |
|
187 |
||
188 |
Thread clientThread = null; |
|
189 |
Thread serverThread = null; |
|
190 |
||
191 |
/* |
|
192 |
* Primary constructor, used to drive remainder of the test. |
|
193 |
* |
|
194 |
* Fork off the other side, then do your work. |
|
195 |
*/ |
|
196 |
SSLSocketCloseHang() throws Exception { |
|
197 |
if (separateServerThread) { |
|
198 |
startServer(true); |
|
199 |
startClient(false); |
|
200 |
} else { |
|
201 |
startClient(true); |
|
202 |
startServer(false); |
|
203 |
} |
|
204 |
||
205 |
/* |
|
206 |
* Wait for other side to close down. |
|
207 |
*/ |
|
208 |
if (separateServerThread) { |
|
209 |
serverThread.join(); |
|
210 |
} else { |
|
211 |
clientThread.join(); |
|
212 |
} |
|
213 |
||
214 |
/* |
|
215 |
* When we get here, the test is pretty much over. |
|
216 |
* |
|
217 |
* If the main thread excepted, that propagates back |
|
218 |
* immediately. If the other thread threw an exception, we |
|
219 |
* should report back. |
|
220 |
*/ |
|
221 |
if (serverException != null) { |
|
222 |
System.out.print("Server Exception:"); |
|
223 |
throw serverException; |
|
224 |
} |
|
225 |
if (clientException != null) { |
|
226 |
System.out.print("Client Exception:"); |
|
227 |
throw clientException; |
|
228 |
} |
|
229 |
} |
|
230 |
||
231 |
void startServer(boolean newThread) throws Exception { |
|
232 |
if (newThread) { |
|
233 |
serverThread = new Thread() { |
|
234 |
public void run() { |
|
235 |
try { |
|
236 |
doServerSide(); |
|
237 |
} catch (Exception e) { |
|
238 |
/* |
|
239 |
* Our server thread just died. |
|
240 |
* |
|
241 |
* Release the client, if not active already... |
|
242 |
*/ |
|
243 |
System.err.println("Server died..."); |
|
244 |
System.err.println(e); |
|
245 |
serverReady = true; |
|
246 |
serverException = e; |
|
247 |
} |
|
248 |
} |
|
249 |
}; |
|
250 |
serverThread.start(); |
|
251 |
} else { |
|
252 |
doServerSide(); |
|
253 |
} |
|
254 |
} |
|
255 |
||
256 |
void startClient(boolean newThread) throws Exception { |
|
257 |
if (newThread) { |
|
258 |
clientThread = new Thread() { |
|
259 |
public void run() { |
|
260 |
try { |
|
261 |
doClientSide(); |
|
262 |
} catch (Exception e) { |
|
263 |
/* |
|
264 |
* Our client thread just died. |
|
265 |
*/ |
|
266 |
System.err.println("Client died..."); |
|
267 |
clientException = e; |
|
268 |
} |
|
269 |
} |
|
270 |
}; |
|
271 |
clientThread.start(); |
|
272 |
} else { |
|
273 |
doClientSide(); |
|
274 |
} |
|
275 |
} |
|
276 |
} |