File: ConnectionTest.java

package info (click to toggle)
openjdk-11 11.0.4%2B11-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 757,028 kB
  • sloc: java: 5,016,041; xml: 1,191,974; cpp: 934,731; ansic: 555,697; sh: 24,299; objc: 12,703; python: 3,602; asm: 3,415; makefile: 2,772; awk: 351; sed: 172; perl: 114; jsp: 24; csh: 3
file content (685 lines) | stat: -rw-r--r-- 24,415 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
/*
 * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/*
 * @test
 * @bug 4495742
 * @summary Add non-blocking SSL/TLS functionality, usable with any
 *      I/O abstraction
 * @ignore the dependent implementation details are changed
 * @author Brad Wetmore
 *
 * @run main/othervm ConnectionTest
 */

/*
 * This is a bit hacky, meant to test various conditions.  The main
 * thing I wanted to do with this was to do buffer reads/writes
 * when buffers were not empty.  (buffer.position() = 10)
 * The code could certainly be tightened up a lot.
 */
import javax.net.ssl.*;
import javax.net.ssl.SSLEngineResult.*;
import java.io.*;
import java.security.*;
import java.nio.*;

public class ConnectionTest {

    private SSLContext sslc;
    private SSLEngine ssle1;
    private SSLEngine ssle2;

    private static String pathToStores = "../etc";
    private static String keyStoreFile = "keystore";
    private static String trustStoreFile = "truststore";
    private static String passwd = "passphrase";

    private static String keyFilename =
            System.getProperty("test.src", "./") + "/" + pathToStores +
                "/" + keyStoreFile;
    private static String trustFilename =
            System.getProperty("test.src", "./") + "/" + pathToStores +
                "/" + trustStoreFile;

    private ByteBuffer appIn1, appOut1;
    private ByteBuffer appIn2, appOut2;
    private ByteBuffer oneToTwo, twoToOne;
    private ByteBuffer emptyBuffer;

    private ByteBuffer  oneToTwoShifter, twoToOneShifter;

    private String hostname = "hostname";
    private int portNumber = 77;

    public ConnectionTest()
            throws Exception {

        sslc = getSSLContext();
        ssle1 = sslc.createSSLEngine(hostname, portNumber);
        ssle2 = sslc.createSSLEngine();

        ssle1.setEnabledCipherSuites(new String [] {
            "SSL_RSA_WITH_RC4_128_MD5"});

        ssle2.setEnabledCipherSuites(new String [] {
            "SSL_RSA_WITH_RC4_128_MD5"});

        createBuffers();
    }

    private SSLContext getSSLContext() throws Exception {
        KeyStore ks = KeyStore.getInstance("JKS");
        KeyStore ts = KeyStore.getInstance("JKS");
        char[] passphrase = "passphrase".toCharArray();

        ks.load(new FileInputStream(keyFilename), passphrase);
        ts.load(new FileInputStream(trustFilename), passphrase);

        KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
        kmf.init(ks, passphrase);

        TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
        tmf.init(ts);

        SSLContext sslCtx = SSLContext.getInstance("TLS");

        sslCtx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);

        return sslCtx;
    }

    private void createBuffers() {
        // Size the buffers as appropriate.
        SSLSession session = ssle1.getSession();
        int appBufferMax = session.getApplicationBufferSize();
        int netBufferMax = session.getPacketBufferSize();

        appIn1 = ByteBuffer.allocateDirect(appBufferMax + 10);
        appIn2 = ByteBuffer.allocateDirect(appBufferMax + 10);

        appIn1.position(10);
        appIn2.position(10);

        oneToTwo = ByteBuffer.allocateDirect(netBufferMax + 10);
        twoToOne = ByteBuffer.allocateDirect(netBufferMax + 10);

        oneToTwo.position(10);
        twoToOne.position(10);
        oneToTwoShifter = oneToTwo.slice();
        twoToOneShifter = twoToOne.slice();

        appOut1 = ByteBuffer.wrap("Hi Engine2, I'm SSLEngine1".getBytes());
        appOut2 = ByteBuffer.wrap("Hello Engine1, I'm SSLEngine2".getBytes());

        emptyBuffer = ByteBuffer.allocate(10);
        emptyBuffer.limit(5);
        emptyBuffer.position(emptyBuffer.limit());

        System.out.println("AppOut1 = " + appOut1);
        System.out.println("AppOut2 = " + appOut2);
        System.out.println();
    }

    private void checkResult(SSLEngineResult result, Status status,
            HandshakeStatus hsStatus, int consumed, int produced,
            boolean done) throws Exception {

        if ((status != null) && (result.getStatus() != status)) {
            throw new Exception("Unexpected Status: need = " + status +
                " got = " + result.getStatus());
        }

        if ((hsStatus != null) && (result.getHandshakeStatus() != hsStatus)) {
            throw new Exception("Unexpected hsStatus: need = " + hsStatus +
                " got = " + result.getHandshakeStatus());
        }

        if ((consumed != -1) && (consumed != result.bytesConsumed())) {
            throw new Exception("Unexpected consumed: need = " + consumed +
                " got = " + result.bytesConsumed());
        }

        if ((produced != -1) && (produced != result.bytesProduced())) {
            throw new Exception("Unexpected produced: need = " + produced +
                " got = " + result.bytesProduced());
        }

        if (done && (hsStatus == HandshakeStatus.FINISHED)) {
            throw new Exception(
                "Handshake already reported finished");
        }

    }

    private boolean isHandshaking(SSLEngine e) {
        return (e.getHandshakeStatus() != HandshakeStatus.NOT_HANDSHAKING);
    }

    private void test() throws Exception {
        ssle1.setUseClientMode(true);
        ssle2.setUseClientMode(false);
        ssle2.setNeedClientAuth(true);

        System.out.println("Testing for early unwrap/wrap");
        SSLEngineResult result1 = ssle1.unwrap(twoToOne, appIn1);
        SSLEngineResult result2 = ssle2.wrap(appOut2, oneToTwo);

        /*
         * These should not consume/produce data, because they
         * are client and server, respectively, and don't
         * start handshaking this way.
         */
        checkResult(result1, Status.OK, HandshakeStatus.NEED_WRAP,
            0, 0, false);
        checkResult(result2, Status.OK, HandshakeStatus.NEED_UNWRAP,
            0, 0, false);

        System.out.println("Doing Initial Handshake");

        boolean done1 = false;
        boolean done2 = false;

        /*
         * Do initial handshaking
         */
        while (isHandshaking(ssle1) ||
                isHandshaking(ssle2)) {

            System.out.println("================");

            result1 = ssle1.wrap(emptyBuffer, oneToTwo);
            checkResult(result1, null, null, 0, -1, done1);
            result2 = ssle2.wrap(emptyBuffer, twoToOne);
            checkResult(result2, null, null, 0, -1, done2);

            if (result1.getHandshakeStatus() == HandshakeStatus.FINISHED) {
                done1 = true;
            }

            if (result2.getHandshakeStatus() == HandshakeStatus.FINISHED) {
                done2 = true;
            }

            System.out.println("wrap1 = " + result1);
            System.out.println("wrap2 = " + result2);

            if (result1.getHandshakeStatus() == HandshakeStatus.NEED_TASK) {
                Runnable runnable;
                while ((runnable = ssle1.getDelegatedTask()) != null) {
                    runnable.run();
                }
            }

            if (result2.getHandshakeStatus() == HandshakeStatus.NEED_TASK) {
                Runnable runnable;
                while ((runnable = ssle2.getDelegatedTask()) != null) {
                    runnable.run();
                }
            }

            oneToTwo.flip();
            twoToOne.flip();

            oneToTwo.position(10);
            twoToOne.position(10);

            System.out.println("----");

            result1 = ssle1.unwrap(twoToOne, appIn1);
            checkResult(result1, null, null, -1, 0, done1);
            result2 = ssle2.unwrap(oneToTwo, appIn2);
            checkResult(result2, null, null, -1, 0, done2);

            if (result1.getHandshakeStatus() == HandshakeStatus.FINISHED) {
                done1 = true;
            }

            if (result2.getHandshakeStatus() == HandshakeStatus.FINISHED) {
                done2 = true;
            }

            if (result1.getHandshakeStatus() == HandshakeStatus.NEED_TASK) {
                Runnable runnable;
                while ((runnable = ssle1.getDelegatedTask()) != null) {
                    runnable.run();
                }
            }

            if (result2.getHandshakeStatus() == HandshakeStatus.NEED_TASK) {
                Runnable runnable;
                while ((runnable = ssle2.getDelegatedTask()) != null) {
                    runnable.run();
                }
            }

            System.out.println("unwrap1 = " + result1);
            System.out.println("unwrap2 = " + result2);

            oneToTwoShifter.position(oneToTwo.position() - 10);
            oneToTwoShifter.limit(oneToTwo.limit() - 10);
            twoToOneShifter.position(twoToOne.position() - 10);
            twoToOneShifter.limit(twoToOne.limit() - 10);
            oneToTwoShifter.compact();
            twoToOneShifter.compact();
            oneToTwo.position(oneToTwoShifter.position() + 10);
            oneToTwo.limit(oneToTwoShifter.limit() + 10);
            twoToOne.position(twoToOneShifter.position() + 10);
            twoToOne.limit(twoToOneShifter.limit() + 10);
        }

        System.out.println("\nDONE HANDSHAKING");
        System.out.println("================");

        if (!done1 || !done2) {
            throw new Exception("Both should be true:\n" +
                " done1 = " + done1 + " done2 = " + done2);
        }

        String host = ssle1.getPeerHost();
        int port = ssle1.getPeerPort();
        if (!host.equals(hostname) || (port != portNumber)) {
            throw new Exception("unexpected host/port " + host + ":" + port);
        }

        host = ssle2.getPeerHost();
        port = ssle2.getPeerPort();
        if ((host != null) || (port != -1)) {
            throw new Exception("unexpected host/port " + host + ":" + port);
        }

        SSLSession ssls1 = ssle1.getSession();

        host = ssls1.getPeerHost();
        port = ssls1.getPeerPort();
        if (!host.equals(hostname) || (port != portNumber)) {
            throw new Exception("unexpected host/port " + host + ":" + port);
        }

        SSLSession ssls2 = ssle2.getSession();

        host = ssls2.getPeerHost();
        port = ssls2.getPeerPort();
        if ((host != null) || (port != -1)) {
            throw new Exception("unexpected host/port " + host + ":" + port);
        }

        /*
         * Should be able to write/read a small buffer like this.
         */
        int appOut1Len = appOut1.remaining();
        int appOut2Len = appOut2.remaining();
        int net1Len;
        int net2Len;

        result1 = ssle1.wrap(appOut1, oneToTwo);
        checkResult(result1, Status.OK, HandshakeStatus.NOT_HANDSHAKING,
            appOut1Len, -1, false);
        result2 = ssle2.wrap(appOut2, twoToOne);
        checkResult(result2, Status.OK, HandshakeStatus.NOT_HANDSHAKING,
            appOut2Len, -1, false);
        net1Len = result1.bytesProduced();
        net2Len = result2.bytesProduced();

        System.out.println("wrap1 = " + result1);
        System.out.println("wrap2 = " + result2);

        oneToTwo.flip();
        twoToOne.flip();

        oneToTwo.position(10);
        twoToOne.position(10);

        System.out.println("----");

        result1 = ssle1.unwrap(twoToOne, appIn1);
        checkResult(result1, Status.OK, HandshakeStatus.NOT_HANDSHAKING,
            net2Len, appOut2Len, false);
        result2 = ssle2.unwrap(oneToTwo, appIn2);
        checkResult(result2, Status.OK, HandshakeStatus.NOT_HANDSHAKING,
            net1Len, appOut1Len, false);

        System.out.println("unwrap1 = " + result1);
        System.out.println("unwrap2 = " + result2);

        oneToTwoShifter.position(oneToTwo.position() - 10);
        oneToTwoShifter.limit(oneToTwo.limit() - 10);
        twoToOneShifter.position(twoToOne.position() - 10);
        twoToOneShifter.limit(twoToOne.limit() - 10);
        oneToTwoShifter.compact();
        twoToOneShifter.compact();
        oneToTwo.position(oneToTwoShifter.position() + 10);
        oneToTwo.limit(oneToTwoShifter.limit() + 10);
        twoToOne.position(twoToOneShifter.position() + 10);
        twoToOne.limit(twoToOneShifter.limit() + 10);

        ssls2.invalidate();
        ssle2.beginHandshake();

        System.out.println("\nRENEGOTIATING");
        System.out.println("=============");

        done1 = false;
        done2 = false;

        appIn1.clear();
        appIn2.clear();

        /*
         * Do a quick test to see if this can do a switch
         * into client mode, at this point, you shouldn't be able
         * to switch back.
         */
        try {
            System.out.println("Try to change client mode");
            ssle2.setUseClientMode(true);
            throw new Exception("Should have thrown IllegalArgumentException");
        } catch (IllegalArgumentException e) {
            System.out.println("Caught correct IllegalArgumentException");
        }

        while (isHandshaking(ssle1) ||
                isHandshaking(ssle2)) {

            System.out.println("================");

            result1 = ssle1.wrap(emptyBuffer, oneToTwo);
            checkResult(result1, null, null, 0, -1, done1);
            result2 = ssle2.wrap(emptyBuffer, twoToOne);
            checkResult(result2, null, null, 0, -1, done2);

            if (result1.getHandshakeStatus() == HandshakeStatus.FINISHED) {
                done1 = true;
            }

            if (result2.getHandshakeStatus() == HandshakeStatus.FINISHED) {
                done2 = true;
            }

            System.out.println("wrap1 = " + result1);
            System.out.println("wrap2 = " + result2);

            if (result1.getHandshakeStatus() == HandshakeStatus.NEED_TASK) {
                Runnable runnable;
                while ((runnable = ssle1.getDelegatedTask()) != null) {
                    runnable.run();
                }
            }

            if (result2.getHandshakeStatus() == HandshakeStatus.NEED_TASK) {
                Runnable runnable;
                while ((runnable = ssle2.getDelegatedTask()) != null) {
                    runnable.run();
                }
            }

            oneToTwo.flip();
            twoToOne.flip();

            oneToTwo.position(10);
            twoToOne.position(10);

            System.out.println("----");

            result1 = ssle1.unwrap(twoToOne, appIn1);
            checkResult(result1, null, null, -1, 0, done1);
            result2 = ssle2.unwrap(oneToTwo, appIn2);
            checkResult(result2, null, null, -1, 0, done2);

            if (result1.getHandshakeStatus() == HandshakeStatus.FINISHED) {
                done1 = true;
            }

            if (result2.getHandshakeStatus() == HandshakeStatus.FINISHED) {
                done2 = true;
            }

            System.out.println("unwrap1 = " + result1);
            System.out.println("unwrap2 = " + result2);

            if (result1.getHandshakeStatus() == HandshakeStatus.NEED_TASK) {
                Runnable runnable;
                while ((runnable = ssle1.getDelegatedTask()) != null) {
                    runnable.run();
                }
            }

            if (result2.getHandshakeStatus() == HandshakeStatus.NEED_TASK) {
                Runnable runnable;
                while ((runnable = ssle2.getDelegatedTask()) != null) {
                    runnable.run();
                }
            }

            oneToTwoShifter.position(oneToTwo.position() - 10);
            oneToTwoShifter.limit(oneToTwo.limit() - 10);
            twoToOneShifter.position(twoToOne.position() - 10);
            twoToOneShifter.limit(twoToOne.limit() - 10);
            oneToTwoShifter.compact();
            twoToOneShifter.compact();
            oneToTwo.position(oneToTwoShifter.position() + 10);
            oneToTwo.limit(oneToTwoShifter.limit() + 10);
            twoToOne.position(twoToOneShifter.position() + 10);
            twoToOne.limit(twoToOneShifter.limit() + 10);
        }

        host = ssle1.getPeerHost();
        port = ssle1.getPeerPort();
        if (!host.equals(hostname) || (port != portNumber)) {
            throw new Exception("unexpected host/port " + host + ":" + port);
        }

        host = ssle2.getPeerHost();
        port = ssle2.getPeerPort();
        if ((host != null) || (port != -1)) {
            throw new Exception("unexpected host/port " + host + ":" + port);
        }

        SSLSession ssls3 = ssle2.getSession();

        host = ssls1.getPeerHost();
        port = ssls1.getPeerPort();
        if (!host.equals(hostname) || (port != portNumber)) {
            throw new Exception("unexpected host/port " + host + ":" + port);
        }

        SSLSession ssls4 = ssle2.getSession();

        host = ssls2.getPeerHost();
        port = ssls2.getPeerPort();
        if ((host != null) || (port != -1)) {
            throw new Exception("unexpected host/port " + host + ":" + port);
        }

        System.out.println("\nDoing close");
        System.out.println("===========");

        ssle1.closeOutbound();
        ssle2.closeOutbound();

        oneToTwo.flip();
        twoToOne.flip();
        oneToTwo.position(10);
        twoToOne.position(10);

        appIn1.clear();
        appIn2.clear();

        System.out.println("LAST UNWRAP");
        result1 = ssle1.unwrap(twoToOne, appIn1);
        checkResult(result1, Status.BUFFER_UNDERFLOW,
            HandshakeStatus.NEED_WRAP, 0, 0, false);
        result2 = ssle2.unwrap(oneToTwo, appIn2);
        checkResult(result2, Status.BUFFER_UNDERFLOW,
            HandshakeStatus.NEED_WRAP, 0, 0, false);

        System.out.println("unwrap1 = " + result1);
        System.out.println("unwrap2 = " + result2);

        oneToTwoShifter.position(oneToTwo.position() - 10);
        oneToTwoShifter.limit(oneToTwo.limit() - 10);
        twoToOneShifter.position(twoToOne.position() - 10);
        twoToOneShifter.limit(twoToOne.limit() - 10);
        oneToTwoShifter.compact();
        twoToOneShifter.compact();
        oneToTwo.position(oneToTwoShifter.position() + 10);
        oneToTwo.limit(oneToTwoShifter.limit() + 10);
        twoToOne.position(twoToOneShifter.position() + 10);
        twoToOne.limit(twoToOneShifter.limit() + 10);

        System.out.println("LAST WRAP");
        result1 = ssle1.wrap(appOut1, oneToTwo);
        checkResult(result1, Status.CLOSED, HandshakeStatus.NEED_UNWRAP,
            0, -1, false);
        result2 = ssle2.wrap(appOut2, twoToOne);
        checkResult(result2, Status.CLOSED, HandshakeStatus.NEED_UNWRAP,
            0, -1, false);

        System.out.println("wrap1 = " + result1);
        System.out.println("wrap2 = " + result2);

        net1Len = result1.bytesProduced();
        net2Len = result2.bytesProduced();

        oneToTwo.flip();
        twoToOne.flip();

        oneToTwo.position(10);
        twoToOne.position(10);

        result1 = ssle1.unwrap(twoToOne, appIn1);
        checkResult(result1, Status.CLOSED, HandshakeStatus.NOT_HANDSHAKING,
            net1Len, 0, false);
        result2 = ssle2.unwrap(oneToTwo, appIn2);
        checkResult(result2, Status.CLOSED, HandshakeStatus.NOT_HANDSHAKING,
            net2Len, 0, false);

        System.out.println("unwrap1 = " + result1);
        System.out.println("unwrap2 = " + result2);

        oneToTwoShifter.position(oneToTwo.position() - 10);
        oneToTwoShifter.limit(oneToTwo.limit() - 10);
        twoToOneShifter.position(twoToOne.position() - 10);
        twoToOneShifter.limit(twoToOne.limit() - 10);
        oneToTwoShifter.compact();
        twoToOneShifter.compact();
        oneToTwo.position(oneToTwoShifter.position() + 10);
        oneToTwo.limit(oneToTwoShifter.limit() + 10);
        twoToOne.position(twoToOneShifter.position() + 10);
        twoToOne.limit(twoToOneShifter.limit() + 10);

        System.out.println("EXTRA WRAP");
        result1 = ssle1.wrap(appOut1, oneToTwo);
        checkResult(result1, Status.CLOSED, HandshakeStatus.NOT_HANDSHAKING,
            0, 0, false);
        result2 = ssle2.wrap(appOut2, twoToOne);
        checkResult(result2, Status.CLOSED, HandshakeStatus.NOT_HANDSHAKING,
            0, 0, false);

        System.out.println("wrap1 = " + result1);
        System.out.println("wrap2 = " + result2);

        oneToTwo.flip();
        twoToOne.flip();
        oneToTwo.position(10);
        twoToOne.position(10);

        System.out.println("EXTRA UNWRAP");
        result1 = ssle1.unwrap(twoToOne, appIn1);
        checkResult(result1, Status.CLOSED, HandshakeStatus.NOT_HANDSHAKING,
            0, 0, false);
        result2 = ssle2.unwrap(oneToTwo, appIn2);
        checkResult(result2, Status.CLOSED, HandshakeStatus.NOT_HANDSHAKING,
            0, 0, false);

        System.out.println("unwrap1 = " + result1);
        System.out.println("unwrap2 = " + result2);

        checkSession(ssls1, ssls2, ssls3, ssls4);
        System.out.println(ssle1);
        System.out.println(ssle2);
    }

    private static void checkSession(SSLSession ssls1, SSLSession ssls2,
            SSLSession ssls3, SSLSession ssls4) throws Exception {
        System.out.println("\nSession Info for SSLEngine1");
        System.out.println(ssls1);
        System.out.println(ssls1.getCreationTime());
        String peer1 = ssls1.getPeerHost();
        System.out.println(peer1);
        String protocol1 = ssls1.getProtocol();
        System.out.println(protocol1);
        java.security.cert.Certificate cert1 = ssls1.getPeerCertificates()[0];
        System.out.println(cert1);
        String ciphersuite1 = ssls1.getCipherSuite();
        System.out.println(ciphersuite1);
        System.out.println();

        System.out.println("\nSession Info for SSLEngine2");
        System.out.println(ssls2);
        System.out.println(ssls2.getCreationTime());
        String peer2 = ssls2.getPeerHost();
        System.out.println(peer2);
        String protocol2 = ssls2.getProtocol();
        System.out.println(protocol2);
        java.security.cert.Certificate cert2 = ssls2.getPeerCertificates()[0];
        System.out.println(cert2);
        String ciphersuite2 = ssls2.getCipherSuite();
        System.out.println(ciphersuite2);
        System.out.println();

        if (peer1.equals(peer2)) {
            throw new Exception("peer hostnames not equal");
        }

        if (!protocol1.equals(protocol2)) {
            throw new Exception("protocols not equal");
        }

        if (!cert1.equals(cert2)) {
            throw new Exception("certs not equal");
        }

        if (!ciphersuite1.equals(ciphersuite2)) {
            throw new Exception("ciphersuites not equal");
        }

        System.out.println("\nSession Info for SSLEngine3");
        System.out.println(ssls3);
        System.out.println("\nSession Info for SSLEngine4");
        System.out.println(ssls4);

        if (ssls3.equals(ssls1) || ssls4.equals(ssls2)) {
            throw new Exception("sessions should not be equals");
        }
    }

    public static void main(String args[]) throws Exception {
        // reset the security property to make sure that the algorithms
        // and keys used in this test are not disabled.
        Security.setProperty("jdk.tls.disabledAlgorithms", "");

        ConnectionTest ct = new ConnectionTest();
        ct.test();
    }
}