author | xuelei |
Mon, 25 Jun 2018 13:41:39 -0700 | |
changeset 50768 | 68fa3d4026ea |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
50768 | 2 |
* Copyright (c) 2001, 2018, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 22 |
*/ |
23 |
||
50768 | 24 |
// |
25 |
// SunJSSE does not support dynamic system properties, no way to re-use |
|
26 |
// system properties in samevm/agentvm mode. |
|
27 |
// |
|
28 |
||
2 | 29 |
/* |
30 |
* @test |
|
31 |
* @bug 4404399 |
|
50768 | 32 |
* @ignore this test does not work any more as the TLS spec changes the |
33 |
* behaviors of close_notify. |
|
2 | 34 |
* @summary When a layered SSL socket is closed, it should wait for close_notify |
10328 | 35 |
* @run main/othervm NonAutoClose |
2 | 36 |
* @author Brad Wetmore |
37 |
*/ |
|
38 |
||
39 |
import java.io.*; |
|
40 |
import java.net.ServerSocket; |
|
41 |
import java.net.Socket; |
|
42 |
import javax.net.ssl.*; |
|
43 |
import java.security.cert.X509Certificate; |
|
44 |
import java.security.cert.CertificateException; |
|
45 |
||
46 |
||
47 |
public class NonAutoClose { |
|
48 |
/* |
|
49 |
* ============================================================= |
|
50 |
* Set the various variables needed for the tests, then |
|
51 |
* specify what tests to run on each side. |
|
52 |
*/ |
|
53 |
||
54 |
/* |
|
55 |
* Should we run the client or server in a separate thread? |
|
56 |
* Both sides can throw exceptions, but do you have a preference |
|
57 |
* as to which side should be the main thread. |
|
58 |
*/ |
|
59 |
private static boolean separateServerThread = true; |
|
60 |
||
61 |
/* |
|
62 |
* Where do we find the keystores? |
|
63 |
*/ |
|
23052
241885315119
8032473: Restructure JSSE regression test hierarchy in jdk test
xuelei
parents:
14342
diff
changeset
|
64 |
private final static String pathToStores = "../../../../javax/net/ssl/etc"; |
2 | 65 |
private final static String keyStoreFile = "keystore"; |
66 |
private final static String trustStoreFile = "truststore"; |
|
67 |
private final static String passwd = "passphrase"; |
|
68 |
private final static char[] cpasswd = "passphrase".toCharArray(); |
|
69 |
||
70 |
/* |
|
71 |
* Is the server ready to serve? |
|
72 |
*/ |
|
73 |
volatile static boolean serverReady = false; |
|
74 |
||
75 |
/* |
|
76 |
* Turn on SSL debugging? |
|
77 |
*/ |
|
78 |
private final static boolean DEBUG = false; |
|
50768 | 79 |
private final static boolean VERBOSE = true; |
2 | 80 |
private final static int NUM_ITERATIONS = 10; |
81 |
private final static int PLAIN_SERVER_VAL = 1; |
|
82 |
private final static int PLAIN_CLIENT_VAL = 2; |
|
83 |
private final static int TLS_SERVER_VAL = 3; |
|
84 |
private final static int TLS_CLIENT_VAL = 4; |
|
85 |
||
86 |
/* |
|
87 |
* If the client or server is doing some kind of object creation |
|
88 |
* that the other side depends on, and that thread prematurely |
|
89 |
* exits, you may experience a hang. The test harness will |
|
90 |
* terminate all hung threads after its timeout has expired, |
|
91 |
* currently 3 minutes by default, but you might try to be |
|
92 |
* smart about it.... |
|
93 |
*/ |
|
94 |
||
95 |
void expectValue(int got, int expected, String msg) throws IOException { |
|
96 |
if (VERBOSE) { |
|
50768 | 97 |
System.err.println(msg + ": read (" + got + ")"); |
2 | 98 |
} |
99 |
if (got != expected) { |
|
100 |
throw new IOException(msg + ": read (" + got |
|
101 |
+ ") but expecting(" + expected + ")"); |
|
102 |
} |
|
103 |
} |
|
104 |
||
105 |
||
106 |
/* |
|
107 |
* Define the server side of the test. |
|
108 |
* |
|
109 |
* If the server prematurely exits, serverReady will be set to true |
|
110 |
* to avoid infinite hangs. |
|
111 |
*/ |
|
112 |
||
113 |
void doServerSide() throws Exception { |
|
114 |
if (VERBOSE) { |
|
50768 | 115 |
System.err.println("Starting server"); |
2 | 116 |
} |
117 |
||
118 |
/* |
|
119 |
* Setup the SSL stuff |
|
120 |
*/ |
|
121 |
SSLSocketFactory sslsf = |
|
122 |
(SSLSocketFactory) SSLSocketFactory.getDefault(); |
|
123 |
||
124 |
ServerSocket serverSocket = new ServerSocket(SERVER_PORT); |
|
125 |
||
126 |
SERVER_PORT = serverSocket.getLocalPort(); |
|
127 |
||
128 |
/* |
|
129 |
* Signal Client, we're ready for his connect. |
|
130 |
*/ |
|
131 |
serverReady = true; |
|
132 |
||
133 |
Socket plainSocket = serverSocket.accept(); |
|
134 |
InputStream is = plainSocket.getInputStream(); |
|
135 |
OutputStream os = plainSocket.getOutputStream(); |
|
136 |
||
137 |
expectValue(is.read(), PLAIN_CLIENT_VAL, "Server"); |
|
138 |
||
139 |
os.write(PLAIN_SERVER_VAL); |
|
140 |
os.flush(); |
|
141 |
||
142 |
for (int i = 1; i <= NUM_ITERATIONS; i++) { |
|
143 |
if (VERBOSE) { |
|
50768 | 144 |
System.err.println("================================="); |
145 |
System.err.println("Server Iteration #" + i); |
|
2 | 146 |
} |
147 |
||
148 |
SSLSocket ssls = (SSLSocket) sslsf.createSocket(plainSocket, |
|
149 |
SERVER_NAME, plainSocket.getPort(), false); |
|
150 |
||
151 |
ssls.setUseClientMode(false); |
|
152 |
InputStream sslis = ssls.getInputStream(); |
|
153 |
OutputStream sslos = ssls.getOutputStream(); |
|
154 |
||
155 |
expectValue(sslis.read(), TLS_CLIENT_VAL, "Server"); |
|
156 |
||
157 |
sslos.write(TLS_SERVER_VAL); |
|
158 |
sslos.flush(); |
|
159 |
||
160 |
sslis.close(); |
|
161 |
sslos.close(); |
|
162 |
ssls.close(); |
|
163 |
||
164 |
if (VERBOSE) { |
|
50768 | 165 |
System.err.println("TLS socket is closed"); |
2 | 166 |
} |
167 |
} |
|
168 |
||
169 |
expectValue(is.read(), PLAIN_CLIENT_VAL, "Server"); |
|
170 |
||
171 |
os.write(PLAIN_SERVER_VAL); |
|
172 |
os.flush(); |
|
173 |
||
174 |
is.close(); |
|
175 |
os.close(); |
|
176 |
plainSocket.close(); |
|
177 |
||
178 |
if (VERBOSE) { |
|
50768 | 179 |
System.err.println("Server plain socket is closed"); |
2 | 180 |
} |
181 |
} |
|
182 |
||
183 |
/* |
|
184 |
* Define the client side of the test. |
|
185 |
* |
|
186 |
* If the server prematurely exits, serverReady will be set to true |
|
187 |
* to avoid infinite hangs. |
|
188 |
*/ |
|
189 |
private void doClientSide() throws Exception { |
|
190 |
/* |
|
191 |
* Wait for server to get started. |
|
192 |
*/ |
|
193 |
while (!serverReady) { |
|
194 |
Thread.sleep(50); |
|
195 |
} |
|
196 |
||
197 |
if (VERBOSE) { |
|
50768 | 198 |
System.err.println("Starting client"); |
2 | 199 |
} |
200 |
||
201 |
/* |
|
202 |
* Setup the SSL stuff |
|
203 |
*/ |
|
204 |
SSLSocketFactory sslsf = |
|
205 |
(SSLSocketFactory) SSLSocketFactory.getDefault(); |
|
206 |
||
207 |
Socket plainSocket = new Socket(SERVER_NAME, SERVER_PORT); |
|
208 |
InputStream is = plainSocket.getInputStream(); |
|
209 |
OutputStream os = plainSocket.getOutputStream(); |
|
210 |
||
211 |
os.write(PLAIN_CLIENT_VAL); |
|
212 |
os.flush(); |
|
213 |
||
214 |
expectValue(is.read(), PLAIN_SERVER_VAL, "Client"); |
|
215 |
||
216 |
for (int i = 1; i <= NUM_ITERATIONS; i++) { |
|
217 |
if (VERBOSE) { |
|
50768 | 218 |
System.err.println("==================================="); |
219 |
System.err.println("Client Iteration #" + i); |
|
2 | 220 |
} |
221 |
||
222 |
SSLSocket ssls = (SSLSocket) sslsf.createSocket(plainSocket, |
|
223 |
SERVER_NAME, plainSocket.getPort(), false); |
|
224 |
||
225 |
ssls.setUseClientMode(true); |
|
226 |
||
227 |
InputStream sslis = ssls.getInputStream(); |
|
228 |
OutputStream sslos = ssls.getOutputStream(); |
|
229 |
||
230 |
sslos.write(TLS_CLIENT_VAL); |
|
231 |
sslos.flush(); |
|
232 |
||
233 |
expectValue(sslis.read(), TLS_SERVER_VAL, "Client"); |
|
234 |
||
235 |
sslis.close(); |
|
236 |
sslos.close(); |
|
237 |
ssls.close(); |
|
238 |
||
239 |
if (VERBOSE) { |
|
50768 | 240 |
System.err.println("Client TLS socket is closed"); |
2 | 241 |
} |
242 |
} |
|
243 |
||
244 |
os.write(PLAIN_CLIENT_VAL); |
|
245 |
os.flush(); |
|
246 |
||
247 |
expectValue(is.read(), PLAIN_SERVER_VAL, "Client"); |
|
248 |
||
249 |
is.close(); |
|
250 |
os.close(); |
|
251 |
plainSocket.close(); |
|
252 |
||
253 |
if (VERBOSE) { |
|
50768 | 254 |
System.err.println("Client plain socket is closed"); |
2 | 255 |
} |
256 |
} |
|
257 |
||
258 |
/* |
|
259 |
* ============================================================= |
|
260 |
* The remainder is just support stuff |
|
261 |
*/ |
|
262 |
||
263 |
private volatile int SERVER_PORT = 0; |
|
264 |
private final static String SERVER_NAME = "localhost"; |
|
265 |
||
266 |
private volatile Exception serverException = null; |
|
267 |
private volatile Exception clientException = null; |
|
268 |
||
269 |
private final static String keyFilename = |
|
270 |
System.getProperty("test.src", ".") + "/" + pathToStores + |
|
271 |
"/" + keyStoreFile; |
|
272 |
private final static String trustFilename = |
|
273 |
System.getProperty("test.src", ".") + "/" + pathToStores + |
|
274 |
"/" + trustStoreFile; |
|
275 |
||
276 |
||
277 |
// Used for running test standalone |
|
278 |
public static void main(String[] args) throws Exception { |
|
279 |
System.setProperty("javax.net.ssl.keyStore", keyFilename); |
|
280 |
System.setProperty("javax.net.ssl.keyStorePassword", passwd); |
|
281 |
System.setProperty("javax.net.ssl.trustStore", trustFilename); |
|
282 |
System.setProperty("javax.net.ssl.trustStorePassword", passwd); |
|
283 |
||
284 |
if (DEBUG) |
|
285 |
System.setProperty("javax.net.debug", "all"); |
|
286 |
||
287 |
/* |
|
288 |
* Start the tests. |
|
289 |
*/ |
|
290 |
new NonAutoClose(); |
|
291 |
} |
|
292 |
||
293 |
private Thread clientThread = null; |
|
294 |
private Thread serverThread = null; |
|
295 |
||
296 |
/* |
|
297 |
* Primary constructor, used to drive remainder of the test. |
|
298 |
* |
|
299 |
* Fork off the other side, then do your work. |
|
300 |
*/ |
|
301 |
NonAutoClose() throws Exception { |
|
302 |
if (separateServerThread) { |
|
303 |
startServer(true); |
|
304 |
startClient(false); |
|
305 |
} else { |
|
306 |
startClient(true); |
|
307 |
startServer(false); |
|
308 |
} |
|
309 |
||
310 |
/* |
|
311 |
* Wait for other side to close down. |
|
312 |
*/ |
|
313 |
if (separateServerThread) { |
|
314 |
serverThread.join(); |
|
315 |
} else { |
|
316 |
clientThread.join(); |
|
317 |
} |
|
318 |
||
319 |
/* |
|
320 |
* When we get here, the test is pretty much over. |
|
321 |
* |
|
322 |
* If the main thread excepted, that propagates back |
|
323 |
* immediately. If the other thread threw an exception, we |
|
324 |
* should report back. |
|
325 |
*/ |
|
326 |
if (serverException != null) { |
|
327 |
System.err.print("Server Exception:"); |
|
328 |
throw serverException; |
|
329 |
} |
|
330 |
if (clientException != null) { |
|
331 |
System.err.print("Client Exception:"); |
|
332 |
throw clientException; |
|
333 |
} |
|
334 |
} |
|
335 |
||
336 |
private void startServer(boolean newThread) throws Exception { |
|
337 |
if (newThread) { |
|
338 |
serverThread = new Thread() { |
|
339 |
public void run() { |
|
340 |
try { |
|
341 |
doServerSide(); |
|
342 |
} catch (Exception e) { |
|
343 |
/* |
|
344 |
* Our server thread just died. |
|
345 |
* |
|
346 |
* Release the client, if not active already... |
|
347 |
*/ |
|
348 |
System.err.println("Server died..."); |
|
349 |
serverReady = true; |
|
350 |
serverException = e; |
|
351 |
} |
|
352 |
} |
|
353 |
}; |
|
354 |
serverThread.start(); |
|
355 |
} else { |
|
356 |
doServerSide(); |
|
357 |
} |
|
358 |
} |
|
359 |
||
360 |
private void startClient(boolean newThread) throws Exception { |
|
361 |
if (newThread) { |
|
362 |
clientThread = new Thread() { |
|
363 |
public void run() { |
|
364 |
try { |
|
365 |
doClientSide(); |
|
366 |
} catch (Exception e) { |
|
367 |
/* |
|
368 |
* Our client thread just died. |
|
369 |
*/ |
|
370 |
System.err.println("Client died..."); |
|
371 |
clientException = e; |
|
372 |
} |
|
373 |
} |
|
374 |
}; |
|
375 |
clientThread.start(); |
|
376 |
} else { |
|
377 |
doClientSide(); |
|
378 |
} |
|
379 |
} |
|
380 |
} |