author | chegar |
Mon, 14 Dec 2015 19:24:33 +0000 | |
changeset 34687 | d302ed125dc9 |
parent 31536 | 3090612af7dd |
child 38446 | 91dcfdbe56be |
permissions | -rw-r--r-- |
30904 | 1 |
/* |
2 |
* Copyright (c) 2015, 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 8043758 |
|
30 |
* @summary Datagram Transport Layer Security (DTLS) |
|
34687
d302ed125dc9
8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents:
31536
diff
changeset
|
31 |
* @modules java.base/sun.security.util |
30904 | 32 |
* @run main/othervm DTLSOverDatagram |
33 |
*/ |
|
34 |
||
35 |
import java.io.*; |
|
36 |
import java.nio.*; |
|
37 |
import java.net.*; |
|
38 |
import java.util.*; |
|
39 |
import java.security.*; |
|
40 |
import java.security.cert.*; |
|
41 |
import javax.net.ssl.*; |
|
42 |
import java.util.concurrent.*; |
|
43 |
||
34687
d302ed125dc9
8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents:
31536
diff
changeset
|
44 |
import sun.security.util.HexDumpEncoder; |
30904 | 45 |
|
46 |
/** |
|
47 |
* An example to show the way to use SSLEngine in datagram connections. |
|
48 |
*/ |
|
49 |
public class DTLSOverDatagram { |
|
31536
3090612af7dd
8130460: Increase the stability of DTLS test CipherSuite.java
xuelei
parents:
30904
diff
changeset
|
50 |
private static int MAX_HANDSHAKE_LOOPS = 200; |
3090612af7dd
8130460: Increase the stability of DTLS test CipherSuite.java
xuelei
parents:
30904
diff
changeset
|
51 |
private static int MAX_APP_READ_LOOPS = 60; |
30904 | 52 |
|
53 |
/* |
|
54 |
* The following is to set up the keystores. |
|
55 |
*/ |
|
56 |
private static String pathToStores = "../etc"; |
|
57 |
private static String keyStoreFile = "keystore"; |
|
58 |
private static String trustStoreFile = "truststore"; |
|
59 |
private static String passwd = "passphrase"; |
|
60 |
||
61 |
private static String keyFilename = |
|
62 |
System.getProperty("test.src", ".") + "/" + pathToStores + |
|
63 |
"/" + keyStoreFile; |
|
64 |
private static String trustFilename = |
|
65 |
System.getProperty("test.src", ".") + "/" + pathToStores + |
|
66 |
"/" + trustStoreFile; |
|
67 |
private static Exception clientException = null; |
|
68 |
private static Exception serverException = null; |
|
69 |
||
70 |
private static ByteBuffer serverApp = |
|
71 |
ByteBuffer.wrap("Hi Client, I'm Server".getBytes()); |
|
72 |
private static ByteBuffer clientApp = |
|
73 |
ByteBuffer.wrap("Hi Server, I'm Client".getBytes()); |
|
74 |
||
75 |
/* |
|
76 |
* ============================================================= |
|
77 |
* The test case |
|
78 |
*/ |
|
79 |
public static void main(String[] args) throws Exception { |
|
80 |
DTLSOverDatagram testCase = new DTLSOverDatagram(); |
|
81 |
testCase.runTest(testCase); |
|
82 |
} |
|
83 |
||
84 |
/* |
|
85 |
* Define the server side of the test. |
|
86 |
*/ |
|
87 |
void doServerSide() throws Exception { |
|
88 |
DatagramSocket socket = serverDatagramSocket; |
|
89 |
socket.setSoTimeout(10000); // 10 second |
|
90 |
||
91 |
// create SSLEngine |
|
92 |
SSLEngine engine = createSSLEngine(false); |
|
93 |
||
94 |
// handshaking |
|
95 |
handshake(engine, socket, clientSocketAddr); |
|
96 |
||
97 |
// read client application data |
|
98 |
receiveAppData(engine, socket, clientApp); |
|
99 |
||
100 |
// write server application data |
|
101 |
deliverAppData(engine, socket, serverApp, clientSocketAddr); |
|
102 |
||
103 |
socket.close(); |
|
104 |
} |
|
105 |
||
106 |
/* |
|
107 |
* Define the client side of the test. |
|
108 |
*/ |
|
109 |
void doClientSide() throws Exception { |
|
110 |
DatagramSocket socket = clientDatagramSocket; |
|
111 |
socket.setSoTimeout(1000); // 1 second read timeout |
|
112 |
||
113 |
// create SSLEngine |
|
114 |
SSLEngine engine = createSSLEngine(true); |
|
115 |
||
116 |
// handshaking |
|
117 |
handshake(engine, socket, serverSocketAddr); |
|
118 |
||
119 |
// write client application data |
|
120 |
deliverAppData(engine, socket, clientApp, serverSocketAddr); |
|
121 |
||
122 |
// read server application data |
|
123 |
receiveAppData(engine, socket, serverApp); |
|
124 |
} |
|
125 |
||
126 |
/* |
|
127 |
* ============================================================= |
|
128 |
* The remainder is support stuff for DTLS operations. |
|
129 |
*/ |
|
130 |
SSLEngine createSSLEngine(boolean isClient) throws Exception { |
|
131 |
SSLContext context = getDTLSContext(); |
|
132 |
SSLEngine engine = context.createSSLEngine(); |
|
133 |
||
134 |
SSLParameters paras = engine.getSSLParameters(); |
|
135 |
paras.setMaximumPacketSize(1024); |
|
136 |
||
137 |
engine.setUseClientMode(isClient); |
|
138 |
engine.setSSLParameters(paras); |
|
139 |
||
140 |
return engine; |
|
141 |
} |
|
142 |
||
143 |
// handshake |
|
144 |
void handshake(SSLEngine engine, DatagramSocket socket, |
|
145 |
SocketAddress peerAddr) throws Exception { |
|
146 |
||
147 |
boolean endLoops = false; |
|
148 |
int loops = MAX_HANDSHAKE_LOOPS; |
|
149 |
engine.beginHandshake(); |
|
150 |
while (!endLoops && |
|
151 |
(serverException == null) && (clientException == null)) { |
|
152 |
||
153 |
if (--loops < 0) { |
|
154 |
throw new RuntimeException( |
|
155 |
"Too much loops to produce handshake packets"); |
|
156 |
} |
|
157 |
||
158 |
SSLEngineResult.HandshakeStatus hs = engine.getHandshakeStatus(); |
|
159 |
if (hs == SSLEngineResult.HandshakeStatus.NEED_UNWRAP || |
|
160 |
hs == SSLEngineResult.HandshakeStatus.NEED_UNWRAP_AGAIN) { |
|
161 |
||
162 |
ByteBuffer iNet; |
|
163 |
ByteBuffer iApp; |
|
164 |
if (hs == SSLEngineResult.HandshakeStatus.NEED_UNWRAP) { |
|
165 |
// receive ClientHello request and other SSL/TLS records |
|
166 |
byte[] buf = new byte[1024]; |
|
167 |
DatagramPacket packet = new DatagramPacket(buf, buf.length); |
|
168 |
try { |
|
169 |
socket.receive(packet); |
|
170 |
} catch (SocketTimeoutException ste) { |
|
171 |
List<DatagramPacket> packets = |
|
172 |
onReceiveTimeout(engine, peerAddr); |
|
173 |
for (DatagramPacket p : packets) { |
|
174 |
socket.send(p); |
|
175 |
} |
|
176 |
||
177 |
continue; |
|
178 |
} |
|
179 |
||
180 |
iNet = ByteBuffer.wrap(buf, 0, packet.getLength()); |
|
181 |
iApp = ByteBuffer.allocate(1024); |
|
182 |
} else { |
|
183 |
iNet = ByteBuffer.allocate(0); |
|
184 |
iApp = ByteBuffer.allocate(1024); |
|
185 |
} |
|
186 |
||
187 |
SSLEngineResult r = engine.unwrap(iNet, iApp); |
|
188 |
SSLEngineResult.Status rs = r.getStatus(); |
|
189 |
hs = r.getHandshakeStatus(); |
|
190 |
if (rs == SSLEngineResult.Status.BUFFER_OVERFLOW) { |
|
191 |
// the client maximum fragment size config does not work? |
|
192 |
throw new Exception("Buffer overflow: " + |
|
193 |
"incorrect client maximum fragment size"); |
|
194 |
} else if (rs == SSLEngineResult.Status.BUFFER_UNDERFLOW) { |
|
195 |
// bad packet, or the client maximum fragment size |
|
196 |
// config does not work? |
|
197 |
if (hs != SSLEngineResult.HandshakeStatus.NOT_HANDSHAKING) { |
|
198 |
throw new Exception("Buffer underflow: " + |
|
199 |
"incorrect client maximum fragment size"); |
|
200 |
} // otherwise, ignore this packet |
|
201 |
} else if (rs == SSLEngineResult.Status.CLOSED) { |
|
202 |
endLoops = true; |
|
203 |
} // otherwise, SSLEngineResult.Status.OK: |
|
204 |
||
205 |
if (rs != SSLEngineResult.Status.OK) { |
|
206 |
continue; |
|
207 |
} |
|
208 |
} else if (hs == SSLEngineResult.HandshakeStatus.NEED_WRAP) { |
|
209 |
List<DatagramPacket> packets = |
|
210 |
produceHandshakePackets(engine, peerAddr); |
|
211 |
for (DatagramPacket p : packets) { |
|
212 |
socket.send(p); |
|
213 |
} |
|
214 |
} else if (hs == SSLEngineResult.HandshakeStatus.NEED_TASK) { |
|
215 |
runDelegatedTasks(engine); |
|
216 |
} else if (hs == SSLEngineResult.HandshakeStatus.NOT_HANDSHAKING) { |
|
217 |
// OK, time to do application data exchange. |
|
218 |
endLoops = true; |
|
219 |
} else if (hs == SSLEngineResult.HandshakeStatus.FINISHED) { |
|
220 |
endLoops = true; |
|
221 |
} |
|
222 |
} |
|
223 |
||
224 |
SSLEngineResult.HandshakeStatus hs = engine.getHandshakeStatus(); |
|
225 |
if (hs != SSLEngineResult.HandshakeStatus.NOT_HANDSHAKING) { |
|
226 |
throw new Exception("Not ready for application data yet"); |
|
227 |
} |
|
228 |
} |
|
229 |
||
230 |
// deliver application data |
|
231 |
void deliverAppData(SSLEngine engine, DatagramSocket socket, |
|
232 |
ByteBuffer appData, SocketAddress peerAddr) throws Exception { |
|
233 |
||
234 |
// Note: have not consider the packet loses |
|
235 |
List<DatagramPacket> packets = |
|
236 |
produceApplicationPackets(engine, appData, peerAddr); |
|
237 |
appData.flip(); |
|
238 |
for (DatagramPacket p : packets) { |
|
239 |
socket.send(p); |
|
240 |
} |
|
241 |
} |
|
242 |
||
243 |
// receive application data |
|
244 |
void receiveAppData(SSLEngine engine, |
|
245 |
DatagramSocket socket, ByteBuffer expectedApp) throws Exception { |
|
246 |
||
247 |
int loops = MAX_APP_READ_LOOPS; |
|
248 |
while ((serverException == null) && (clientException == null)) { |
|
249 |
if (--loops < 0) { |
|
250 |
throw new RuntimeException( |
|
251 |
"Too much loops to receive application data"); |
|
252 |
} |
|
253 |
||
254 |
byte[] buf = new byte[1024]; |
|
255 |
DatagramPacket packet = new DatagramPacket(buf, buf.length); |
|
256 |
socket.receive(packet); |
|
257 |
ByteBuffer netBuffer = ByteBuffer.wrap(buf, 0, packet.getLength()); |
|
258 |
ByteBuffer recBuffer = ByteBuffer.allocate(1024); |
|
259 |
SSLEngineResult rs = engine.unwrap(netBuffer, recBuffer); |
|
260 |
recBuffer.flip(); |
|
261 |
if (recBuffer.remaining() != 0) { |
|
262 |
printHex("Received application data", recBuffer); |
|
263 |
if (!recBuffer.equals(expectedApp)) { |
|
264 |
System.out.println("Engine status is " + rs); |
|
265 |
throw new Exception("Not the right application data"); |
|
266 |
} |
|
267 |
break; |
|
268 |
} |
|
269 |
} |
|
270 |
} |
|
271 |
||
272 |
// produce handshake packets |
|
273 |
List<DatagramPacket> produceHandshakePackets( |
|
274 |
SSLEngine engine, SocketAddress socketAddr) throws Exception { |
|
275 |
||
276 |
List<DatagramPacket> packets = new ArrayList<>(); |
|
277 |
boolean endLoops = false; |
|
278 |
int loops = MAX_HANDSHAKE_LOOPS; |
|
279 |
while (!endLoops && |
|
280 |
(serverException == null) && (clientException == null)) { |
|
281 |
||
282 |
if (--loops < 0) { |
|
283 |
throw new RuntimeException( |
|
284 |
"Too much loops to produce handshake packets"); |
|
285 |
} |
|
286 |
||
287 |
ByteBuffer oNet = ByteBuffer.allocate(32768); |
|
288 |
ByteBuffer oApp = ByteBuffer.allocate(0); |
|
289 |
SSLEngineResult r = engine.wrap(oApp, oNet); |
|
290 |
oNet.flip(); |
|
291 |
||
292 |
SSLEngineResult.Status rs = r.getStatus(); |
|
293 |
SSLEngineResult.HandshakeStatus hs = r.getHandshakeStatus(); |
|
294 |
if (rs == SSLEngineResult.Status.BUFFER_OVERFLOW) { |
|
295 |
// the client maximum fragment size config does not work? |
|
296 |
throw new Exception("Buffer overflow: " + |
|
297 |
"incorrect server maximum fragment size"); |
|
298 |
} else if (rs == SSLEngineResult.Status.BUFFER_UNDERFLOW) { |
|
299 |
// bad packet, or the client maximum fragment size |
|
300 |
// config does not work? |
|
301 |
if (hs != SSLEngineResult.HandshakeStatus.NOT_HANDSHAKING) { |
|
302 |
throw new Exception("Buffer underflow: " + |
|
303 |
"incorrect server maximum fragment size"); |
|
304 |
} // otherwise, ignore this packet |
|
305 |
} else if (rs == SSLEngineResult.Status.CLOSED) { |
|
306 |
throw new Exception("SSLEngine has closed"); |
|
307 |
} // otherwise, SSLEngineResult.Status.OK |
|
308 |
||
309 |
// SSLEngineResult.Status.OK: |
|
310 |
if (oNet.hasRemaining()) { |
|
311 |
byte[] ba = new byte[oNet.remaining()]; |
|
312 |
oNet.get(ba); |
|
313 |
DatagramPacket packet = createHandshakePacket(ba, socketAddr); |
|
314 |
packets.add(packet); |
|
315 |
} |
|
316 |
boolean endInnerLoop = false; |
|
317 |
SSLEngineResult.HandshakeStatus nhs = hs; |
|
318 |
while (!endInnerLoop) { |
|
319 |
if (nhs == SSLEngineResult.HandshakeStatus.NEED_TASK) { |
|
320 |
runDelegatedTasks(engine); |
|
321 |
nhs = engine.getHandshakeStatus(); |
|
322 |
} else if ((nhs == SSLEngineResult.HandshakeStatus.FINISHED) || |
|
323 |
(nhs == SSLEngineResult.HandshakeStatus.NEED_UNWRAP) || |
|
324 |
(nhs == SSLEngineResult.HandshakeStatus.NOT_HANDSHAKING)) { |
|
325 |
||
326 |
endInnerLoop = true; |
|
327 |
endLoops = true; |
|
328 |
} else if (nhs == SSLEngineResult.HandshakeStatus.NEED_WRAP) { |
|
329 |
endInnerLoop = true; |
|
330 |
} |
|
331 |
} |
|
332 |
} |
|
333 |
||
334 |
return packets; |
|
335 |
} |
|
336 |
||
337 |
DatagramPacket createHandshakePacket(byte[] ba, SocketAddress socketAddr) { |
|
338 |
return new DatagramPacket(ba, ba.length, socketAddr); |
|
339 |
} |
|
340 |
||
341 |
// produce application packets |
|
342 |
List<DatagramPacket> produceApplicationPackets( |
|
343 |
SSLEngine engine, ByteBuffer source, |
|
344 |
SocketAddress socketAddr) throws Exception { |
|
345 |
||
346 |
List<DatagramPacket> packets = new ArrayList<>(); |
|
347 |
ByteBuffer appNet = ByteBuffer.allocate(32768); |
|
348 |
SSLEngineResult r = engine.wrap(source, appNet); |
|
349 |
appNet.flip(); |
|
350 |
||
351 |
SSLEngineResult.Status rs = r.getStatus(); |
|
352 |
if (rs == SSLEngineResult.Status.BUFFER_OVERFLOW) { |
|
353 |
// the client maximum fragment size config does not work? |
|
354 |
throw new Exception("Buffer overflow: " + |
|
355 |
"incorrect server maximum fragment size"); |
|
356 |
} else if (rs == SSLEngineResult.Status.BUFFER_UNDERFLOW) { |
|
357 |
// unlikely |
|
358 |
throw new Exception("Buffer underflow during wraping"); |
|
359 |
} else if (rs == SSLEngineResult.Status.CLOSED) { |
|
360 |
throw new Exception("SSLEngine has closed"); |
|
361 |
} // otherwise, SSLEngineResult.Status.OK |
|
362 |
||
363 |
// SSLEngineResult.Status.OK: |
|
364 |
if (appNet.hasRemaining()) { |
|
365 |
byte[] ba = new byte[appNet.remaining()]; |
|
366 |
appNet.get(ba); |
|
367 |
DatagramPacket packet = |
|
368 |
new DatagramPacket(ba, ba.length, socketAddr); |
|
369 |
packets.add(packet); |
|
370 |
} |
|
371 |
||
372 |
return packets; |
|
373 |
} |
|
374 |
||
375 |
// run delegated tasks |
|
376 |
void runDelegatedTasks(SSLEngine engine) throws Exception { |
|
377 |
Runnable runnable; |
|
378 |
while ((runnable = engine.getDelegatedTask()) != null) { |
|
379 |
runnable.run(); |
|
380 |
} |
|
381 |
||
382 |
SSLEngineResult.HandshakeStatus hs = engine.getHandshakeStatus(); |
|
383 |
if (hs == SSLEngineResult.HandshakeStatus.NEED_TASK) { |
|
384 |
throw new Exception("handshake shouldn't need additional tasks"); |
|
385 |
} |
|
386 |
} |
|
387 |
||
388 |
// retransmission if timeout |
|
389 |
List<DatagramPacket> onReceiveTimeout( |
|
390 |
SSLEngine engine, SocketAddress socketAddr) throws Exception { |
|
391 |
||
392 |
SSLEngineResult.HandshakeStatus hs = engine.getHandshakeStatus(); |
|
393 |
if (hs == SSLEngineResult.HandshakeStatus.NOT_HANDSHAKING) { |
|
394 |
return new ArrayList<DatagramPacket>(); |
|
395 |
} else { |
|
396 |
// retransmission of handshake messages |
|
397 |
return produceHandshakePackets(engine, socketAddr); |
|
398 |
} |
|
399 |
} |
|
400 |
||
401 |
// get DTSL context |
|
402 |
SSLContext getDTLSContext() throws Exception { |
|
403 |
KeyStore ks = KeyStore.getInstance("JKS"); |
|
404 |
KeyStore ts = KeyStore.getInstance("JKS"); |
|
405 |
||
406 |
char[] passphrase = "passphrase".toCharArray(); |
|
407 |
||
408 |
ks.load(new FileInputStream(keyFilename), passphrase); |
|
409 |
ts.load(new FileInputStream(trustFilename), passphrase); |
|
410 |
||
411 |
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509"); |
|
412 |
kmf.init(ks, passphrase); |
|
413 |
||
414 |
TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509"); |
|
415 |
tmf.init(ts); |
|
416 |
||
417 |
SSLContext sslCtx = SSLContext.getInstance("DTLS"); |
|
418 |
||
419 |
sslCtx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null); |
|
420 |
||
421 |
return sslCtx; |
|
422 |
} |
|
423 |
||
424 |
||
425 |
/* |
|
426 |
* ============================================================= |
|
427 |
* The remainder is support stuff to kickstart the testing. |
|
428 |
*/ |
|
429 |
||
430 |
// the server side SocketAddress |
|
431 |
volatile static SocketAddress serverSocketAddr = null; |
|
432 |
||
433 |
// the client side SocketAddress |
|
434 |
volatile static SocketAddress clientSocketAddr = null; |
|
435 |
||
436 |
// the server side DatagramSocket instance |
|
437 |
volatile static DatagramSocket serverDatagramSocket = null; |
|
438 |
||
439 |
// the server side DatagramSocket instance |
|
440 |
volatile static DatagramSocket clientDatagramSocket = null; |
|
441 |
||
442 |
// get server side SocketAddress object |
|
443 |
public SocketAddress getServerSocketAddress() { |
|
444 |
return serverSocketAddr; |
|
445 |
} |
|
446 |
||
447 |
// get client side SocketAddress object |
|
448 |
public SocketAddress getClientSocketAddress() { |
|
449 |
return clientSocketAddr; |
|
450 |
} |
|
451 |
||
452 |
// get server side DatagramSocket object |
|
453 |
public DatagramSocket getServerDatagramSocket() { |
|
454 |
return serverDatagramSocket; |
|
455 |
} |
|
456 |
||
457 |
// get client side DatagramSocket object |
|
458 |
public DatagramSocket getClientDatagramSocket() { |
|
459 |
return clientDatagramSocket; |
|
460 |
} |
|
461 |
||
462 |
// Will the handshaking and application data exchange succeed? |
|
463 |
public boolean isGoodJob() { |
|
464 |
return true; |
|
465 |
} |
|
466 |
||
467 |
public final void runTest(DTLSOverDatagram testCase) throws Exception { |
|
468 |
serverDatagramSocket = new DatagramSocket(); |
|
469 |
serverSocketAddr = new InetSocketAddress( |
|
470 |
InetAddress.getLocalHost(), serverDatagramSocket.getLocalPort()); |
|
471 |
||
472 |
clientDatagramSocket = new DatagramSocket(); |
|
473 |
clientSocketAddr = new InetSocketAddress( |
|
474 |
InetAddress.getLocalHost(), clientDatagramSocket.getLocalPort()); |
|
475 |
||
476 |
ExecutorService pool = Executors.newFixedThreadPool(2); |
|
477 |
List<Future<String>> list = new ArrayList<Future<String>>(); |
|
478 |
||
479 |
try { |
|
480 |
list.add(pool.submit(new ServerCallable(testCase))); // server task |
|
481 |
list.add(pool.submit(new ClientCallable(testCase))); // client task |
|
482 |
} finally { |
|
483 |
pool.shutdown(); |
|
484 |
} |
|
485 |
||
486 |
Exception reserved = null; |
|
487 |
for (Future<String> fut : list) { |
|
488 |
try { |
|
489 |
System.out.println(fut.get()); |
|
490 |
} catch (CancellationException | |
|
491 |
InterruptedException | ExecutionException cie) { |
|
492 |
if (reserved != null) { |
|
493 |
cie.addSuppressed(reserved); |
|
494 |
reserved = cie; |
|
495 |
} else { |
|
496 |
reserved = cie; |
|
497 |
} |
|
498 |
} |
|
499 |
} |
|
500 |
||
501 |
if (reserved != null) { |
|
502 |
throw reserved; |
|
503 |
} |
|
504 |
} |
|
505 |
||
506 |
final static class ServerCallable implements Callable<String> { |
|
507 |
DTLSOverDatagram testCase; |
|
508 |
||
509 |
ServerCallable(DTLSOverDatagram testCase) { |
|
510 |
this.testCase = testCase; |
|
511 |
} |
|
512 |
||
513 |
@Override |
|
514 |
public String call() throws Exception { |
|
515 |
try { |
|
516 |
testCase.doServerSide(); |
|
517 |
} catch (Exception e) { |
|
518 |
e.printStackTrace(System.out); |
|
519 |
serverException = e; |
|
520 |
||
521 |
if (testCase.isGoodJob()) { |
|
522 |
throw e; |
|
523 |
} else { |
|
524 |
return "Well done, server!"; |
|
525 |
} |
|
526 |
} finally { |
|
527 |
if (serverDatagramSocket != null) { |
|
528 |
serverDatagramSocket.close(); |
|
529 |
} |
|
530 |
} |
|
531 |
||
532 |
if (testCase.isGoodJob()) { |
|
533 |
return "Well done, server!"; |
|
534 |
} else { |
|
535 |
throw new Exception("No expected exception"); |
|
536 |
} |
|
537 |
} |
|
538 |
} |
|
539 |
||
540 |
final static class ClientCallable implements Callable<String> { |
|
541 |
DTLSOverDatagram testCase; |
|
542 |
||
543 |
ClientCallable(DTLSOverDatagram testCase) { |
|
544 |
this.testCase = testCase; |
|
545 |
} |
|
546 |
||
547 |
@Override |
|
548 |
public String call() throws Exception { |
|
549 |
try { |
|
550 |
testCase.doClientSide(); |
|
551 |
} catch (Exception e) { |
|
552 |
e.printStackTrace(System.out); |
|
553 |
clientException = e; |
|
554 |
if (testCase.isGoodJob()) { |
|
555 |
throw e; |
|
556 |
} else { |
|
557 |
return "Well done, client!"; |
|
558 |
} |
|
559 |
} finally { |
|
560 |
if (clientDatagramSocket != null) { |
|
561 |
clientDatagramSocket.close(); |
|
562 |
} |
|
563 |
} |
|
564 |
||
565 |
if (testCase.isGoodJob()) { |
|
566 |
return "Well done, client!"; |
|
567 |
} else { |
|
568 |
throw new Exception("No expected exception"); |
|
569 |
} |
|
570 |
} |
|
571 |
} |
|
572 |
||
573 |
final static void printHex(String prefix, ByteBuffer bb) { |
|
574 |
HexDumpEncoder dump = new HexDumpEncoder(); |
|
575 |
||
576 |
synchronized (System.out) { |
|
577 |
System.out.println(prefix); |
|
578 |
try { |
|
579 |
dump.encodeBuffer(bb.slice(), System.out); |
|
580 |
} catch (Exception e) { |
|
581 |
// ignore |
|
582 |
} |
|
583 |
System.out.flush(); |
|
584 |
} |
|
585 |
} |
|
586 |
||
587 |
final static void printHex(String prefix, |
|
588 |
byte[] bytes, int offset, int length) { |
|
589 |
||
590 |
HexDumpEncoder dump = new HexDumpEncoder(); |
|
591 |
||
592 |
synchronized (System.out) { |
|
593 |
System.out.println(prefix); |
|
594 |
try { |
|
595 |
ByteBuffer bb = ByteBuffer.wrap(bytes, offset, length); |
|
596 |
dump.encodeBuffer(bb, System.out); |
|
597 |
} catch (Exception e) { |
|
598 |
// ignore |
|
599 |
} |
|
600 |
System.out.flush(); |
|
601 |
} |
|
602 |
} |
|
603 |
} |