|
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 |
|
103 SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept(); |
|
104 sslSocket.startHandshake(); |
|
105 while (!clientClosed) { |
|
106 Thread.sleep(500); |
|
107 } |
|
108 } |
|
109 |
|
110 /* |
|
111 * Define the client side of the test. |
|
112 * |
|
113 * If the server prematurely exits, serverReady will be set to true |
|
114 * to avoid infinite hangs. |
|
115 */ |
|
116 void doClientSide() throws Exception { |
|
117 boolean caught = false; |
|
118 |
|
119 /* |
|
120 * Wait for server to get started. |
|
121 */ |
|
122 System.out.println("waiting on server"); |
|
123 while (!serverReady) { |
|
124 Thread.sleep(50); |
|
125 } |
|
126 System.out.println("server ready"); |
|
127 |
|
128 Socket baseSocket = new Socket("localhost", serverPort); |
|
129 baseSocket.setSoTimeout(100); |
|
130 |
|
131 SSLSocketFactory sslsf = |
|
132 (SSLSocketFactory) SSLSocketFactory.getDefault(); |
|
133 SSLSocket sslSocket = (SSLSocket) |
|
134 sslsf.createSocket(baseSocket, "localhost", serverPort, false); |
|
135 |
|
136 // handshaking |
|
137 sslSocket.startHandshake(); |
|
138 System.out.println("handshake done"); |
|
139 |
|
140 Thread.sleep(500); |
|
141 System.out.println("client closing"); |
|
142 |
|
143 sslSocket.close(); |
|
144 clientClosed = true; |
|
145 System.out.println("client closed"); |
|
146 } |
|
147 |
|
148 /* |
|
149 * ============================================================= |
|
150 * The remainder is just support stuff |
|
151 */ |
|
152 |
|
153 // use any free port by default |
|
154 volatile int serverPort = 0; |
|
155 |
|
156 volatile Exception serverException = null; |
|
157 volatile Exception clientException = null; |
|
158 |
|
159 volatile byte[] serverDigest = null; |
|
160 |
|
161 public static void main(String[] args) throws Exception { |
|
162 String keyFilename = |
|
163 System.getProperty("test.src", "./") + "/" + pathToStores + |
|
164 "/" + keyStoreFile; |
|
165 String trustFilename = |
|
166 System.getProperty("test.src", "./") + "/" + pathToStores + |
|
167 "/" + trustStoreFile; |
|
168 |
|
169 System.setProperty("javax.net.ssl.keyStore", keyFilename); |
|
170 System.setProperty("javax.net.ssl.keyStorePassword", passwd); |
|
171 System.setProperty("javax.net.ssl.trustStore", trustFilename); |
|
172 System.setProperty("javax.net.ssl.trustStorePassword", passwd); |
|
173 |
|
174 if (debug) |
|
175 System.setProperty("javax.net.debug", "all"); |
|
176 |
|
177 /* |
|
178 * Start the tests. |
|
179 */ |
|
180 new SSLSocketCloseHang(); |
|
181 } |
|
182 |
|
183 Thread clientThread = null; |
|
184 Thread serverThread = null; |
|
185 |
|
186 /* |
|
187 * Primary constructor, used to drive remainder of the test. |
|
188 * |
|
189 * Fork off the other side, then do your work. |
|
190 */ |
|
191 SSLSocketCloseHang() throws Exception { |
|
192 if (separateServerThread) { |
|
193 startServer(true); |
|
194 startClient(false); |
|
195 } else { |
|
196 startClient(true); |
|
197 startServer(false); |
|
198 } |
|
199 |
|
200 /* |
|
201 * Wait for other side to close down. |
|
202 */ |
|
203 if (separateServerThread) { |
|
204 serverThread.join(); |
|
205 } else { |
|
206 clientThread.join(); |
|
207 } |
|
208 |
|
209 /* |
|
210 * When we get here, the test is pretty much over. |
|
211 * |
|
212 * If the main thread excepted, that propagates back |
|
213 * immediately. If the other thread threw an exception, we |
|
214 * should report back. |
|
215 */ |
|
216 if (serverException != null) { |
|
217 System.out.print("Server Exception:"); |
|
218 throw serverException; |
|
219 } |
|
220 if (clientException != null) { |
|
221 System.out.print("Client Exception:"); |
|
222 throw clientException; |
|
223 } |
|
224 } |
|
225 |
|
226 void startServer(boolean newThread) throws Exception { |
|
227 if (newThread) { |
|
228 serverThread = new Thread() { |
|
229 public void run() { |
|
230 try { |
|
231 doServerSide(); |
|
232 } catch (Exception e) { |
|
233 /* |
|
234 * Our server thread just died. |
|
235 * |
|
236 * Release the client, if not active already... |
|
237 */ |
|
238 System.err.println("Server died..."); |
|
239 System.err.println(e); |
|
240 serverReady = true; |
|
241 serverException = e; |
|
242 } |
|
243 } |
|
244 }; |
|
245 serverThread.start(); |
|
246 } else { |
|
247 doServerSide(); |
|
248 } |
|
249 } |
|
250 |
|
251 void startClient(boolean newThread) throws Exception { |
|
252 if (newThread) { |
|
253 clientThread = new Thread() { |
|
254 public void run() { |
|
255 try { |
|
256 doClientSide(); |
|
257 } catch (Exception e) { |
|
258 /* |
|
259 * Our client thread just died. |
|
260 */ |
|
261 System.err.println("Client died..."); |
|
262 clientException = e; |
|
263 } |
|
264 } |
|
265 }; |
|
266 clientThread.start(); |
|
267 } else { |
|
268 doClientSide(); |
|
269 } |
|
270 } |
|
271 } |