CheckStatus.java revision 12072:6721ff11d592
1/*
2 * Copyright (c) 2003, 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/*
25 * @test
26 * @bug 4948079
27 * @summary SSLEngineResult needs updating [none yet]
28 *
29 * This is a simple hack to test a bunch of conditions and check
30 * their return codes.
31 *
32 * @run main/othervm -Djsse.enableCBCProtection=false CheckStatus
33 *
34 * @author Brad Wetmore
35 */
36
37import javax.net.ssl.*;
38import javax.net.ssl.SSLEngineResult.*;
39import java.io.*;
40import java.security.*;
41import java.nio.*;
42
43public class CheckStatus {
44
45    private static boolean debug = true;
46
47    private SSLContext sslc;
48    private SSLEngine ssle1;    // client
49    private SSLEngine ssle2;    // server
50
51    private static String pathToStores = "../etc";
52    private static String keyStoreFile = "keystore";
53    private static String trustStoreFile = "truststore";
54    private static String passwd = "passphrase";
55
56    private static String keyFilename =
57            System.getProperty("test.src", "./") + "/" + pathToStores +
58                "/" + keyStoreFile;
59    private static String trustFilename =
60            System.getProperty("test.src", "./") + "/" + pathToStores +
61                "/" + trustStoreFile;
62
63    private ByteBuffer appOut1;         // write side of ssle1
64    private ByteBuffer appIn1;          // read side of ssle1
65    private ByteBuffer appOut2;         // write side of ssle2
66    private ByteBuffer appIn2;          // read side of ssle2
67
68    private ByteBuffer oneToTwo;        // "reliable" transport ssle1->ssle2
69    private ByteBuffer twoToOne;        // "reliable" transport ssle2->ssle1
70
71    /*
72     * Majority of the test case is here, setup is done below.
73     */
74
75    private void createSSLEngines() throws Exception {
76        ssle1 = sslc.createSSLEngine("client", 1);
77        ssle1.setUseClientMode(true);
78
79        ssle2 = sslc.createSSLEngine("server", 2);
80        ssle2.setUseClientMode(false);
81    }
82
83    private boolean isHandshaking(SSLEngine e) {
84        return (e.getHandshakeStatus() != HandshakeStatus.NOT_HANDSHAKING);
85    }
86
87    private void checkResult(ByteBuffer bbIn, ByteBuffer bbOut,
88            SSLEngineResult result,
89            Status status, HandshakeStatus hsStatus,
90            int consumed, int produced)
91            throws Exception {
92
93        if ((status != null) && (result.getStatus() != status)) {
94            throw new Exception("Unexpected Status: need = " + status +
95                " got = " + result.getStatus());
96        }
97
98        if ((hsStatus != null) && (result.getHandshakeStatus() != hsStatus)) {
99            throw new Exception("Unexpected hsStatus: need = " + hsStatus +
100                " got = " + result.getHandshakeStatus());
101        }
102
103        if ((consumed != -1) && (consumed != result.bytesConsumed())) {
104            throw new Exception("Unexpected consumed: need = " + consumed +
105                " got = " + result.bytesConsumed());
106        }
107
108        if ((produced != -1) && (produced != result.bytesProduced())) {
109            throw new Exception("Unexpected produced: need = " + produced +
110                " got = " + result.bytesProduced());
111        }
112
113        if ((consumed != -1) && (bbIn.position() != result.bytesConsumed())) {
114            throw new Exception("Consumed " + bbIn.position() +
115                " != " + consumed);
116        }
117
118        if ((produced != -1) && (bbOut.position() != result.bytesProduced())) {
119            throw new Exception("produced " + bbOut.position() +
120                " != " + produced);
121        }
122    }
123
124    private void test() throws Exception {
125        createSSLEngines();
126        createBuffers();
127
128        SSLEngineResult result1;        // ssle1's results from last operation
129        SSLEngineResult result2;        // ssle2's results from last operation
130
131        String [] suite1 = new String [] {
132            "SSL_RSA_WITH_RC4_128_MD5" };
133        String [] suite2 = new String [] {
134            "SSL_DHE_DSS_WITH_3DES_EDE_CBC_SHA" };
135
136        ssle1.setEnabledCipherSuites(suite1);
137        ssle2.setEnabledCipherSuites(suite1);
138
139        log("================");
140
141        log("unexpected empty unwrap");
142        twoToOne.limit(0);
143        result1 = ssle1.unwrap(twoToOne, appIn1);
144        checkResult(twoToOne, appIn1, result1,
145            Status.OK, HandshakeStatus.NEED_WRAP, 0, 0);
146        twoToOne.limit(twoToOne.capacity());
147
148        log("======================================");
149        log("client hello");
150        result1 = ssle1.wrap(appOut1, oneToTwo);
151        checkResult(appOut1, oneToTwo, result1,
152             Status.OK, HandshakeStatus.NEED_UNWRAP, 0, -1);
153
154        oneToTwo.flip();
155        result2 = ssle2.unwrap(oneToTwo, appIn2);
156
157        checkResult(oneToTwo, appIn2, result2,
158             Status.OK, HandshakeStatus.NEED_TASK, result1.bytesProduced(), 0);
159        runDelegatedTasks(ssle2);
160
161        oneToTwo.compact();
162
163        log("Check for unwrap when wrap needed");
164        result2 = ssle2.unwrap(oneToTwo, appIn2);
165        checkResult(oneToTwo, appIn2, result2,
166            Status.OK, HandshakeStatus.NEED_WRAP, 0, 0);
167
168        log("======================================");
169        log("ServerHello");
170
171        result2 = ssle2.wrap(appOut2, twoToOne);
172        checkResult(appOut2, twoToOne, result2,
173            Status.OK, HandshakeStatus.NEED_UNWRAP, 0, -1);
174        twoToOne.flip();
175
176        result1 = ssle1.unwrap(twoToOne, appIn1);
177        checkResult(twoToOne, appIn1, result1,
178            Status.OK, HandshakeStatus.NEED_TASK, result2.bytesProduced(), 0);
179        twoToOne.compact();
180
181        runDelegatedTasks(ssle1);
182
183        log("======================================");
184        log("Key Exchange");
185        result1 = ssle1.wrap(appOut1, oneToTwo);
186        checkResult(appOut1, oneToTwo, result1,
187             Status.OK, HandshakeStatus.NEED_WRAP, 0, -1);
188
189        oneToTwo.flip();
190        result2 = ssle2.unwrap(oneToTwo, appIn2);
191
192        checkResult(oneToTwo, appIn2, result2,
193             Status.OK, HandshakeStatus.NEED_TASK, result1.bytesProduced(), 0);
194        runDelegatedTasks(ssle2);
195
196        oneToTwo.compact();
197
198        log("======================================");
199        log("CCS");
200        result1 = ssle1.wrap(appOut1, oneToTwo);
201        checkResult(appOut1, oneToTwo, result1,
202             Status.OK, HandshakeStatus.NEED_WRAP, 0, -1);
203
204        oneToTwo.flip();
205        result2 = ssle2.unwrap(oneToTwo, appIn2);
206
207        checkResult(oneToTwo, appIn2, result2,
208             Status.OK, HandshakeStatus.NEED_UNWRAP,
209             result1.bytesProduced(), 0);
210
211        oneToTwo.compact();
212
213        log("======================================");
214        log("Finished");
215        result1 = ssle1.wrap(appOut1, oneToTwo);
216        checkResult(appOut1, oneToTwo, result1,
217             Status.OK, HandshakeStatus.NEED_UNWRAP, 0, -1);
218
219        oneToTwo.flip();
220        result2 = ssle2.unwrap(oneToTwo, appIn2);
221
222        checkResult(oneToTwo, appIn2, result2,
223             Status.OK, HandshakeStatus.NEED_WRAP, result1.bytesProduced(), 0);
224
225        oneToTwo.compact();
226
227        log("======================================");
228        log("CCS");
229
230        result2 = ssle2.wrap(appOut2, twoToOne);
231        checkResult(appOut2, twoToOne, result2,
232            Status.OK, HandshakeStatus.NEED_WRAP, 0, -1);
233        twoToOne.flip();
234
235        result1 = ssle1.unwrap(twoToOne, appIn1);
236        checkResult(twoToOne, appIn1, result1,
237            Status.OK, HandshakeStatus.NEED_UNWRAP, result2.bytesProduced(), 0);
238        twoToOne.compact();
239
240        log("======================================");
241        log("FINISHED");
242
243        result2 = ssle2.wrap(appOut2, twoToOne);
244        checkResult(appOut2, twoToOne, result2,
245            Status.OK, HandshakeStatus.FINISHED, 0, -1);
246        twoToOne.flip();
247
248        result1 = ssle1.unwrap(twoToOne, appIn1);
249        checkResult(twoToOne, appIn1, result1,
250            Status.OK, HandshakeStatus.FINISHED, result2.bytesProduced(), 0);
251        twoToOne.compact();
252
253        log("======================================");
254        log("Check Session/Ciphers");
255
256        String suite = ssle1.getSession().getCipherSuite();
257        if (!suite.equals(suite1[0])) {
258            throw new Exception("suites not equal: " + suite + "/" +
259                suite1[0]);
260        }
261
262        suite = ssle2.getSession().getCipherSuite();
263        if (!suite.equals(suite1[0])) {
264            throw new Exception("suites not equal: " + suite + "/" +
265                suite1[0]);
266        }
267
268        log("======================================");
269        log("DATA");
270
271        result1 = ssle1.wrap(appOut1, oneToTwo);
272        checkResult(appOut1, oneToTwo, result1,
273            Status.OK, HandshakeStatus.NOT_HANDSHAKING,
274            appOut1.capacity(), -1);
275        oneToTwo.flip();
276
277        result2 = ssle2.wrap(appOut2, twoToOne);
278        checkResult(appOut2, twoToOne, result2,
279            Status.OK, HandshakeStatus.NOT_HANDSHAKING,
280            appOut2.capacity(), -1);
281        twoToOne.flip();
282
283        SSLEngineResult result3 = ssle1.unwrap(twoToOne, appIn1);
284        checkResult(twoToOne, appIn1, result3,
285            Status.OK, HandshakeStatus.NOT_HANDSHAKING,
286            result2.bytesProduced(), result2.bytesConsumed());
287        twoToOne.compact();
288
289        SSLEngineResult result4 = ssle2.unwrap(oneToTwo, appIn2);
290        checkResult(oneToTwo, appIn2, result4,
291            Status.OK, HandshakeStatus.NOT_HANDSHAKING,
292            result1.bytesProduced(), result1.bytesConsumed());
293        oneToTwo.compact();
294
295        appIn1.clear();
296        appIn2.clear();
297        appOut1.rewind();
298        appOut2.rewind();
299
300        log("======================================");
301        log("RENEGOTIATE");
302
303        ssle2.getSession().invalidate();
304        ssle2.setNeedClientAuth(true);
305
306        ssle1.setEnabledCipherSuites(suite2);
307        ssle2.setEnabledCipherSuites(suite2);
308
309        ssle2.beginHandshake();
310
311        log("======================================");
312        log("HelloRequest");
313
314        result2 = ssle2.wrap(appOut2, twoToOne);
315        checkResult(appOut2, twoToOne, result2,
316            Status.OK, HandshakeStatus.NEED_UNWRAP, 0, -1);
317        twoToOne.flip();
318
319        result1 = ssle1.unwrap(twoToOne, appIn1);
320        checkResult(twoToOne, appIn1, result1,
321            Status.OK, HandshakeStatus.NEED_TASK, result2.bytesProduced(), 0);
322        twoToOne.compact();
323
324        runDelegatedTasks(ssle1);
325
326        log("======================================");
327        log("ClientHello");
328
329        result1 = ssle1.wrap(appOut1, oneToTwo);
330        checkResult(appOut1, oneToTwo, result1,
331             Status.OK, HandshakeStatus.NEED_UNWRAP, 0, -1);
332
333        oneToTwo.flip();
334        result2 = ssle2.unwrap(oneToTwo, appIn2);
335
336        checkResult(oneToTwo, appIn2, result2,
337             Status.OK, HandshakeStatus.NEED_TASK, result1.bytesProduced(), 0);
338        runDelegatedTasks(ssle2);
339
340        oneToTwo.compact();
341
342        log("======================================");
343        log("CLIENT->SERVER DATA IN MIDDLE OF HANDSHAKE");
344
345        result1 = ssle1.wrap(appOut1, oneToTwo);
346        checkResult(appOut1, oneToTwo, result1,
347            Status.OK, HandshakeStatus.NEED_UNWRAP,
348            appOut1.capacity(), -1);
349        oneToTwo.flip();
350
351        result4 = ssle2.unwrap(oneToTwo, appIn2);
352        checkResult(oneToTwo, appIn2, result4,
353            Status.OK, HandshakeStatus.NEED_WRAP,
354            result1.bytesProduced(), result1.bytesConsumed());
355        oneToTwo.compact();
356
357        appIn2.clear();
358        appOut1.rewind();
359
360        log("======================================");
361        log("ServerHello");
362
363        result2 = ssle2.wrap(appOut2, twoToOne);
364        checkResult(appOut2, twoToOne, result2,
365            Status.OK, HandshakeStatus.NEED_UNWRAP, 0, -1);
366        twoToOne.flip();
367
368        result1 = ssle1.unwrap(twoToOne, appIn1);
369        checkResult(twoToOne, appIn1, result1,
370            Status.OK, HandshakeStatus.NEED_TASK, result2.bytesProduced(), 0);
371        twoToOne.compact();
372
373        runDelegatedTasks(ssle1);
374
375        log("======================================");
376        log("SERVER->CLIENT DATA IN MIDDLE OF HANDSHAKE");
377
378        result2 = ssle2.wrap(appOut2, twoToOne);
379        checkResult(appOut2, twoToOne, result2,
380            Status.OK, HandshakeStatus.NEED_UNWRAP,
381            appOut2.capacity(), -1);
382        twoToOne.flip();
383
384        result3 = ssle1.unwrap(twoToOne, appIn1);
385        checkResult(twoToOne, appIn1, result3,
386            Status.OK, HandshakeStatus.NEED_WRAP,
387            result2.bytesProduced(), result2.bytesConsumed());
388        twoToOne.compact();
389
390        appIn1.clear();
391        appOut2.rewind();
392
393        log("======================================");
394        log("Client Cert and Key Exchange");
395        result1 = ssle1.wrap(appOut1, oneToTwo);
396        checkResult(appOut1, oneToTwo, result1,
397             Status.OK, HandshakeStatus.NEED_WRAP, 0, -1);
398
399        oneToTwo.flip();
400        result2 = ssle2.unwrap(oneToTwo, appIn2);
401
402        checkResult(oneToTwo, appIn2, result2,
403             Status.OK, HandshakeStatus.NEED_TASK, result1.bytesProduced(), 0);
404        runDelegatedTasks(ssle2);
405
406        oneToTwo.compact();
407
408        log("======================================");
409        log("CCS");
410        result1 = ssle1.wrap(appOut1, oneToTwo);
411        checkResult(appOut1, oneToTwo, result1,
412             Status.OK, HandshakeStatus.NEED_WRAP, 0, -1);
413
414        oneToTwo.flip();
415        result2 = ssle2.unwrap(oneToTwo, appIn2);
416
417        checkResult(oneToTwo, appIn2, result2,
418             Status.OK, HandshakeStatus.NEED_UNWRAP,
419             result1.bytesProduced(), 0);
420
421        oneToTwo.compact();
422
423        log("======================================");
424        log("Finished");
425        result1 = ssle1.wrap(appOut1, oneToTwo);
426        checkResult(appOut1, oneToTwo, result1,
427             Status.OK, HandshakeStatus.NEED_UNWRAP, 0, -1);
428
429        oneToTwo.flip();
430        result2 = ssle2.unwrap(oneToTwo, appIn2);
431
432        checkResult(oneToTwo, appIn2, result2,
433             Status.OK, HandshakeStatus.NEED_WRAP, result1.bytesProduced(), 0);
434
435        oneToTwo.compact();
436
437        log("======================================");
438        log("CCS");
439
440        result2 = ssle2.wrap(appOut2, twoToOne);
441        checkResult(appOut2, twoToOne, result2,
442            Status.OK, HandshakeStatus.NEED_WRAP, 0, -1);
443        twoToOne.flip();
444
445        result1 = ssle1.unwrap(twoToOne, appIn1);
446        checkResult(twoToOne, appIn1, result1,
447            Status.OK, HandshakeStatus.NEED_UNWRAP, result2.bytesProduced(), 0);
448        twoToOne.compact();
449
450        log("======================================");
451        log("FINISHED");
452
453        result2 = ssle2.wrap(appOut2, twoToOne);
454        checkResult(appOut2, twoToOne, result2,
455            Status.OK, HandshakeStatus.FINISHED, 0, -1);
456        twoToOne.flip();
457
458        result1 = ssle1.unwrap(twoToOne, appIn1);
459        checkResult(twoToOne, appIn1, result1,
460            Status.OK, HandshakeStatus.FINISHED, result2.bytesProduced(), 0);
461        twoToOne.compact();
462
463        log("======================================");
464        log("Check Session/Ciphers");
465
466        suite = ssle1.getSession().getCipherSuite();
467        if (!suite.equals(suite2[0])) {
468            throw new Exception("suites not equal: " + suite + "/" +
469                suite2[0]);
470        }
471
472        suite = ssle2.getSession().getCipherSuite();
473        if (!suite.equals(suite2[0])) {
474            throw new Exception("suites not equal: " + suite + "/" +
475                suite2[0]);
476        }
477
478        log("======================================");
479        log("DATA USING NEW SESSION");
480
481        result1 = ssle1.wrap(appOut1, oneToTwo);
482        checkResult(appOut1, oneToTwo, result1,
483            Status.OK, HandshakeStatus.NOT_HANDSHAKING,
484            appOut1.capacity(), -1);
485        oneToTwo.flip();
486
487        result2 = ssle2.wrap(appOut2, twoToOne);
488        checkResult(appOut2, twoToOne, result2,
489            Status.OK, HandshakeStatus.NOT_HANDSHAKING,
490            appOut2.capacity(), -1);
491        twoToOne.flip();
492
493        result3 = ssle1.unwrap(twoToOne, appIn1);
494        checkResult(twoToOne, appIn1, result3,
495            Status.OK, HandshakeStatus.NOT_HANDSHAKING,
496            result2.bytesProduced(), result2.bytesConsumed());
497        twoToOne.compact();
498
499        result4 = ssle2.unwrap(oneToTwo, appIn2);
500        checkResult(oneToTwo, appIn2, result4,
501            Status.OK, HandshakeStatus.NOT_HANDSHAKING,
502            result1.bytesProduced(), result1.bytesConsumed());
503        oneToTwo.compact();
504
505        appIn1.clear();
506        appIn2.clear();
507        appOut1.rewind();
508        appOut2.rewind();
509
510        log("======================================");
511        log("CN");
512
513        if (isHandshaking(ssle1)) {
514            throw new Exception("ssle1 IS handshaking");
515        }
516
517        if (isHandshaking(ssle2)) {
518            throw new Exception("ssle2 IS handshaking");
519        }
520
521        ssle2.closeOutbound();
522
523        if (!isHandshaking(ssle2)) {
524            throw new Exception("ssle1 IS NOT handshaking");
525        }
526
527        appOut1.rewind();
528        appOut2.rewind();
529
530        result2 = ssle2.wrap(appOut2, twoToOne);
531        checkResult(appOut2, twoToOne, result2,
532            Status.CLOSED, HandshakeStatus.NEED_UNWRAP, 0, -1);
533        twoToOne.flip();
534
535        if (ssle1.isInboundDone()) {
536            throw new Exception("ssle1 inboundDone");
537        }
538
539        result1 = ssle1.unwrap(twoToOne, appIn1);
540        checkResult(twoToOne, appIn1, result1,
541            Status.CLOSED, HandshakeStatus.NEED_WRAP,
542            result2.bytesProduced(), 0);
543        twoToOne.compact();
544
545        if (!ssle1.isInboundDone()) {
546            throw new Exception("ssle1 inboundDone");
547        }
548
549        if (!isHandshaking(ssle1)) {
550            throw new Exception("ssle1 IS NOT handshaking");
551        }
552
553        result2 = ssle2.wrap(appOut2, twoToOne);
554        checkResult(appOut2, twoToOne, result2,
555            Status.CLOSED, HandshakeStatus.NEED_UNWRAP, 0, 0);
556        twoToOne.flip();
557
558        log("======================================");
559        log("CN response");
560
561        if (ssle1.isOutboundDone()) {
562            throw new Exception("ssle1 outboundDone");
563        }
564
565        result1 = ssle1.wrap(appOut1, oneToTwo);
566        checkResult(appOut1, oneToTwo, result1,
567             Status.CLOSED, HandshakeStatus.NOT_HANDSHAKING, 0, -1);
568
569        if (!ssle1.isOutboundDone()) {
570            throw new Exception("ssle1 outboundDone is NOT done");
571        }
572
573        if (isHandshaking(ssle1)) {
574            throw new Exception("ssle1 IS handshaking");
575        }
576
577        oneToTwo.flip();
578
579        if (!ssle2.isOutboundDone()) {
580            throw new Exception("ssle1 outboundDone");
581        }
582
583        if (ssle2.isInboundDone()) {
584            throw new Exception("ssle1 inboundDone");
585        }
586
587        result2 = ssle2.unwrap(oneToTwo, appIn2);
588
589        checkResult(oneToTwo, appIn2, result2,
590             Status.CLOSED, HandshakeStatus.NOT_HANDSHAKING,
591             result1.bytesProduced(), 0);
592
593        if (!ssle2.isOutboundDone()) {
594            throw new Exception("ssle1 outboundDone is NOT done");
595        }
596
597        if (!ssle2.isInboundDone()) {
598            throw new Exception("ssle1 inboundDone is NOT done");
599        }
600
601        if (isHandshaking(ssle2)) {
602            throw new Exception("ssle1 IS handshaking");
603        }
604
605        oneToTwo.compact();
606    }
607
608    public static void main(String args[]) throws Exception {
609        // reset the security property to make sure that the algorithms
610        // and keys used in this test are not disabled.
611        Security.setProperty("jdk.tls.disabledAlgorithms", "");
612
613        CheckStatus cs;
614
615        cs = new CheckStatus();
616
617        cs.createSSLEngines();
618
619        cs.test();
620
621        System.out.println("Test Passed.");
622    }
623
624    /*
625     * **********************************************************
626     * Majority of the test case is above, below is just setup stuff
627     * **********************************************************
628     */
629
630    public CheckStatus() throws Exception {
631        sslc = getSSLContext(keyFilename, trustFilename);
632    }
633
634    /*
635     * Create an initialized SSLContext to use for this test.
636     */
637    private SSLContext getSSLContext(String keyFile, String trustFile)
638            throws Exception {
639
640        KeyStore ks = KeyStore.getInstance("JKS");
641        KeyStore ts = KeyStore.getInstance("JKS");
642
643        char[] passphrase = "passphrase".toCharArray();
644
645        ks.load(new FileInputStream(keyFile), passphrase);
646        ts.load(new FileInputStream(trustFile), passphrase);
647
648        KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
649        kmf.init(ks, passphrase);
650
651        TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
652        tmf.init(ts);
653
654        SSLContext sslCtx = SSLContext.getInstance("TLS");
655
656        sslCtx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
657
658        return sslCtx;
659    }
660
661    private void createBuffers() {
662        // Size the buffers as appropriate.
663
664        SSLSession session = ssle1.getSession();
665        int appBufferMax = session.getApplicationBufferSize();
666        int netBufferMax = session.getPacketBufferSize();
667
668        appIn1 = ByteBuffer.allocateDirect(appBufferMax + 50);
669        appIn2 = ByteBuffer.allocateDirect(appBufferMax + 50);
670
671        oneToTwo = ByteBuffer.allocateDirect(netBufferMax);
672        twoToOne = ByteBuffer.allocateDirect(netBufferMax);
673
674        appOut1 = ByteBuffer.wrap("Hi Engine2, I'm SSLEngine1".getBytes());
675        appOut2 = ByteBuffer.wrap("Hello Engine1, I'm SSLEngine2".getBytes());
676
677        log("AppOut1 = " + appOut1);
678        log("AppOut2 = " + appOut2);
679        log("");
680    }
681
682    private static void runDelegatedTasks(SSLEngine engine) throws Exception {
683
684        Runnable runnable;
685        while ((runnable = engine.getDelegatedTask()) != null) {
686            log("running delegated task...");
687            runnable.run();
688        }
689    }
690
691    private static void checkTransfer(ByteBuffer a, ByteBuffer b)
692            throws Exception {
693        a.flip();
694        b.flip();
695
696        if (!a.equals(b)) {
697            throw new Exception("Data didn't transfer cleanly");
698        } else {
699            log("Data transferred cleanly");
700        }
701
702        a.position(a.limit());
703        b.position(b.limit());
704        a.limit(a.capacity());
705        b.limit(b.capacity());
706    }
707
708    private static void log(String str) {
709        if (debug) {
710            System.out.println(str);
711        }
712    }
713}
714