jdk/test/javax/net/ssl/SSLEngine/ConnectionTest.java
author xuelei
Wed, 05 Mar 2014 07:24:34 +0000
changeset 23052 241885315119
parent 6856 jdk/test/sun/security/ssl/javax/net/ssl/NewAPIs/SSLEngine/ConnectionTest.java@533f4ad71f88
child 29902 dc24eacaae11
permissions -rw-r--r--
8032473: Restructure JSSE regression test hierarchy in jdk test Reviewed-by: weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
     2
 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5182
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5182
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5182
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 4495742
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Add non-blocking SSL/TLS functionality, usable with any
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *      I/O abstraction
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * This is a bit hacky, meant to test various conditions.  The main
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * thing I wanted to do with this was to do buffer reads/writes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * when buffers were not empty.  (buffer.position() = 10)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * The code could certainly be tightened up a lot.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * @author Brad Wetmore
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.net.ssl.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.net.ssl.SSLEngineResult.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.nio.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
public class ConnectionTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private SSLContext sslc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private SSLEngine ssle1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private SSLEngine ssle2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
23052
241885315119 8032473: Restructure JSSE regression test hierarchy in jdk test
xuelei
parents: 6856
diff changeset
    50
    private static String pathToStores = "../etc";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private static String keyStoreFile = "keystore";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private static String trustStoreFile = "truststore";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private static String passwd = "passphrase";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private static String keyFilename =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            System.getProperty("test.src", "./") + "/" + pathToStores +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
                "/" + keyStoreFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private static String trustFilename =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            System.getProperty("test.src", "./") + "/" + pathToStores +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                "/" + trustStoreFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private ByteBuffer appIn1, appOut1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private ByteBuffer appIn2, appOut2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private ByteBuffer oneToTwo, twoToOne;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private ByteBuffer emptyBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private ByteBuffer  oneToTwoShifter, twoToOneShifter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private String hostname = "hostname";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private int portNumber = 77;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    public ConnectionTest()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        sslc = getSSLContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        ssle1 = sslc.createSSLEngine(hostname, portNumber);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        ssle2 = sslc.createSSLEngine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        ssle1.setEnabledCipherSuites(new String [] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            "SSL_RSA_WITH_RC4_128_MD5"});
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        createBuffers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    private SSLContext getSSLContext() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        KeyStore ks = KeyStore.getInstance("JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        KeyStore ts = KeyStore.getInstance("JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        char[] passphrase = "passphrase".toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        ks.load(new FileInputStream(keyFilename), passphrase);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        ts.load(new FileInputStream(trustFilename), passphrase);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        kmf.init(ks, passphrase);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        tmf.init(ts);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        SSLContext sslCtx = SSLContext.getInstance("TLS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        sslCtx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        return sslCtx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    private void createBuffers() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        // Size the buffers as appropriate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        SSLSession session = ssle1.getSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        int appBufferMax = session.getApplicationBufferSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        int netBufferMax = session.getPacketBufferSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        appIn1 = ByteBuffer.allocateDirect(appBufferMax + 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        appIn2 = ByteBuffer.allocateDirect(appBufferMax + 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        appIn1.position(10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        appIn2.position(10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        oneToTwo = ByteBuffer.allocateDirect(netBufferMax + 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        twoToOne = ByteBuffer.allocateDirect(netBufferMax + 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        oneToTwo.position(10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        twoToOne.position(10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        oneToTwoShifter = oneToTwo.slice();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        twoToOneShifter = twoToOne.slice();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        appOut1 = ByteBuffer.wrap("Hi Engine2, I'm SSLEngine1".getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        appOut2 = ByteBuffer.wrap("Hello Engine1, I'm SSLEngine2".getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        emptyBuffer = ByteBuffer.allocate(10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        emptyBuffer.limit(5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        emptyBuffer.position(emptyBuffer.limit());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        System.out.println("AppOut1 = " + appOut1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        System.out.println("AppOut2 = " + appOut2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    private void checkResult(SSLEngineResult result, Status status,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            HandshakeStatus hsStatus, int consumed, int produced,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            boolean done) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        if ((status != null) && (result.getStatus() != status)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            throw new Exception("Unexpected Status: need = " + status +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                " got = " + result.getStatus());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        if ((hsStatus != null) && (result.getHandshakeStatus() != hsStatus)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            throw new Exception("Unexpected hsStatus: need = " + hsStatus +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                " got = " + result.getHandshakeStatus());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        if ((consumed != -1) && (consumed != result.bytesConsumed())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            throw new Exception("Unexpected consumed: need = " + consumed +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                " got = " + result.bytesConsumed());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        if ((produced != -1) && (produced != result.bytesProduced())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            throw new Exception("Unexpected produced: need = " + produced +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                " got = " + result.bytesProduced());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        if (done && (hsStatus == HandshakeStatus.FINISHED)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            throw new Exception(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                "Handshake already reported finished");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    private boolean isHandshaking(SSLEngine e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        return (e.getHandshakeStatus() != HandshakeStatus.NOT_HANDSHAKING);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    private void test() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        ssle1.setUseClientMode(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        ssle2.setUseClientMode(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        ssle2.setNeedClientAuth(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        System.out.println("Testing for early unwrap/wrap");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        SSLEngineResult result1 = ssle1.unwrap(twoToOne, appIn1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        SSLEngineResult result2 = ssle2.wrap(appOut2, oneToTwo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
         * These should not consume/produce data, because they
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
         * are client and server, respectively, and don't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
         * start handshaking this way.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        checkResult(result1, Status.OK, HandshakeStatus.NEED_WRAP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            0, 0, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        checkResult(result2, Status.OK, HandshakeStatus.NEED_UNWRAP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            0, 0, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        System.out.println("Doing Initial Handshake");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        boolean done1 = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        boolean done2 = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
         * Do initial handshaking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        while (isHandshaking(ssle1) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                isHandshaking(ssle2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            System.out.println("================");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            result1 = ssle1.wrap(emptyBuffer, oneToTwo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            checkResult(result1, null, null, 0, -1, done1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            result2 = ssle2.wrap(emptyBuffer, twoToOne);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            checkResult(result2, null, null, 0, -1, done2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            if (result1.getHandshakeStatus() == HandshakeStatus.FINISHED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                done1 = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            if (result2.getHandshakeStatus() == HandshakeStatus.FINISHED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                done2 = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            System.out.println("wrap1 = " + result1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            System.out.println("wrap2 = " + result2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            if (result1.getHandshakeStatus() == HandshakeStatus.NEED_TASK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                Runnable runnable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                while ((runnable = ssle1.getDelegatedTask()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                    runnable.run();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            if (result2.getHandshakeStatus() == HandshakeStatus.NEED_TASK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                Runnable runnable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                while ((runnable = ssle2.getDelegatedTask()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                    runnable.run();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            oneToTwo.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            twoToOne.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            oneToTwo.position(10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            twoToOne.position(10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            System.out.println("----");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            result1 = ssle1.unwrap(twoToOne, appIn1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            checkResult(result1, null, null, -1, 0, done1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            result2 = ssle2.unwrap(oneToTwo, appIn2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            checkResult(result2, null, null, -1, 0, done2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            if (result1.getHandshakeStatus() == HandshakeStatus.FINISHED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                done1 = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            if (result2.getHandshakeStatus() == HandshakeStatus.FINISHED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                done2 = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            if (result1.getHandshakeStatus() == HandshakeStatus.NEED_TASK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                Runnable runnable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                while ((runnable = ssle1.getDelegatedTask()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                    runnable.run();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            if (result2.getHandshakeStatus() == HandshakeStatus.NEED_TASK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                Runnable runnable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                while ((runnable = ssle2.getDelegatedTask()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                    runnable.run();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            System.out.println("unwrap1 = " + result1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            System.out.println("unwrap2 = " + result2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            oneToTwoShifter.position(oneToTwo.position() - 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            oneToTwoShifter.limit(oneToTwo.limit() - 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            twoToOneShifter.position(twoToOne.position() - 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            twoToOneShifter.limit(twoToOne.limit() - 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            oneToTwoShifter.compact();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            twoToOneShifter.compact();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            oneToTwo.position(oneToTwoShifter.position() + 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            oneToTwo.limit(oneToTwoShifter.limit() + 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            twoToOne.position(twoToOneShifter.position() + 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            twoToOne.limit(twoToOneShifter.limit() + 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        System.out.println("\nDONE HANDSHAKING");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        System.out.println("================");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        if (!done1 || !done2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            throw new Exception("Both should be true:\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                " done1 = " + done1 + " done2 = " + done2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        String host = ssle1.getPeerHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        int port = ssle1.getPeerPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        if (!host.equals(hostname) || (port != portNumber)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            throw new Exception("unexpected host/port " + host + ":" + port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        host = ssle2.getPeerHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        port = ssle2.getPeerPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        if ((host != null) || (port != -1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            throw new Exception("unexpected host/port " + host + ":" + port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        SSLSession ssls1 = ssle1.getSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        host = ssls1.getPeerHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        port = ssls1.getPeerPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        if (!host.equals(hostname) || (port != portNumber)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            throw new Exception("unexpected host/port " + host + ":" + port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        SSLSession ssls2 = ssle2.getSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        host = ssls2.getPeerHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        port = ssls2.getPeerPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        if ((host != null) || (port != -1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            throw new Exception("unexpected host/port " + host + ":" + port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
         * Should be able to write/read a small buffer like this.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        int appOut1Len = appOut1.remaining();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        int appOut2Len = appOut2.remaining();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        int net1Len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        int net2Len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        result1 = ssle1.wrap(appOut1, oneToTwo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        checkResult(result1, Status.OK, HandshakeStatus.NOT_HANDSHAKING,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            appOut1Len, -1, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        result2 = ssle2.wrap(appOut2, twoToOne);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        checkResult(result2, Status.OK, HandshakeStatus.NOT_HANDSHAKING,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            appOut2Len, -1, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        net1Len = result1.bytesProduced();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        net2Len = result2.bytesProduced();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        System.out.println("wrap1 = " + result1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        System.out.println("wrap2 = " + result2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        oneToTwo.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        twoToOne.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        oneToTwo.position(10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        twoToOne.position(10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        System.out.println("----");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        result1 = ssle1.unwrap(twoToOne, appIn1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        checkResult(result1, Status.OK, HandshakeStatus.NOT_HANDSHAKING,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            net2Len, appOut2Len, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        result2 = ssle2.unwrap(oneToTwo, appIn2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        checkResult(result2, Status.OK, HandshakeStatus.NOT_HANDSHAKING,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            net1Len, appOut1Len, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        System.out.println("unwrap1 = " + result1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        System.out.println("unwrap2 = " + result2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        oneToTwoShifter.position(oneToTwo.position() - 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        oneToTwoShifter.limit(oneToTwo.limit() - 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        twoToOneShifter.position(twoToOne.position() - 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        twoToOneShifter.limit(twoToOne.limit() - 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        oneToTwoShifter.compact();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        twoToOneShifter.compact();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        oneToTwo.position(oneToTwoShifter.position() + 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        oneToTwo.limit(oneToTwoShifter.limit() + 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        twoToOne.position(twoToOneShifter.position() + 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        twoToOne.limit(twoToOneShifter.limit() + 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        ssls2.invalidate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        ssle2.beginHandshake();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        System.out.println("\nRENEGOTIATING");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        System.out.println("=============");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        done1 = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        done2 = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        appIn1.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        appIn2.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
         * Do a quick test to see if this can do a switch
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
         * into client mode, at this point, you shouldn't be able
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
         * to switch back.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            System.out.println("Try to change client mode");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            ssle2.setUseClientMode(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            throw new Exception("Should have thrown IllegalArgumentException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        } catch (IllegalArgumentException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            System.out.println("Caught correct IllegalArgumentException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        while (isHandshaking(ssle1) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                isHandshaking(ssle2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            System.out.println("================");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            result1 = ssle1.wrap(emptyBuffer, oneToTwo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            checkResult(result1, null, null, 0, -1, done1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            result2 = ssle2.wrap(emptyBuffer, twoToOne);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            checkResult(result2, null, null, 0, -1, done2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            if (result1.getHandshakeStatus() == HandshakeStatus.FINISHED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                done1 = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            if (result2.getHandshakeStatus() == HandshakeStatus.FINISHED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                done2 = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            System.out.println("wrap1 = " + result1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            System.out.println("wrap2 = " + result2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            if (result1.getHandshakeStatus() == HandshakeStatus.NEED_TASK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                Runnable runnable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                while ((runnable = ssle1.getDelegatedTask()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                    runnable.run();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            if (result2.getHandshakeStatus() == HandshakeStatus.NEED_TASK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                Runnable runnable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                while ((runnable = ssle2.getDelegatedTask()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                    runnable.run();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            oneToTwo.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            twoToOne.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            oneToTwo.position(10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            twoToOne.position(10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            System.out.println("----");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            result1 = ssle1.unwrap(twoToOne, appIn1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            checkResult(result1, null, null, -1, 0, done1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            result2 = ssle2.unwrap(oneToTwo, appIn2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            checkResult(result2, null, null, -1, 0, done2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            if (result1.getHandshakeStatus() == HandshakeStatus.FINISHED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                done1 = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            if (result2.getHandshakeStatus() == HandshakeStatus.FINISHED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                done2 = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            System.out.println("unwrap1 = " + result1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            System.out.println("unwrap2 = " + result2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            if (result1.getHandshakeStatus() == HandshakeStatus.NEED_TASK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                Runnable runnable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                while ((runnable = ssle1.getDelegatedTask()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                    runnable.run();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            if (result2.getHandshakeStatus() == HandshakeStatus.NEED_TASK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                Runnable runnable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                while ((runnable = ssle2.getDelegatedTask()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                    runnable.run();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            oneToTwoShifter.position(oneToTwo.position() - 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            oneToTwoShifter.limit(oneToTwo.limit() - 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            twoToOneShifter.position(twoToOne.position() - 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            twoToOneShifter.limit(twoToOne.limit() - 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            oneToTwoShifter.compact();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            twoToOneShifter.compact();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            oneToTwo.position(oneToTwoShifter.position() + 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            oneToTwo.limit(oneToTwoShifter.limit() + 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            twoToOne.position(twoToOneShifter.position() + 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            twoToOne.limit(twoToOneShifter.limit() + 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        host = ssle1.getPeerHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        port = ssle1.getPeerPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        if (!host.equals(hostname) || (port != portNumber)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            throw new Exception("unexpected host/port " + host + ":" + port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        host = ssle2.getPeerHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        port = ssle2.getPeerPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        if ((host != null) || (port != -1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            throw new Exception("unexpected host/port " + host + ":" + port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        SSLSession ssls3 = ssle2.getSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        host = ssls1.getPeerHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        port = ssls1.getPeerPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        if (!host.equals(hostname) || (port != portNumber)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            throw new Exception("unexpected host/port " + host + ":" + port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        SSLSession ssls4 = ssle2.getSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        host = ssls2.getPeerHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        port = ssls2.getPeerPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        if ((host != null) || (port != -1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            throw new Exception("unexpected host/port " + host + ":" + port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        System.out.println("\nDoing close");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        System.out.println("===========");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        ssle1.closeOutbound();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        ssle2.closeOutbound();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        oneToTwo.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        twoToOne.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        oneToTwo.position(10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        twoToOne.position(10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        appIn1.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        appIn2.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        System.out.println("LAST UNWRAP");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        result1 = ssle1.unwrap(twoToOne, appIn1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        checkResult(result1, Status.BUFFER_UNDERFLOW,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
            HandshakeStatus.NEED_WRAP, 0, 0, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        result2 = ssle2.unwrap(oneToTwo, appIn2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        checkResult(result2, Status.BUFFER_UNDERFLOW,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            HandshakeStatus.NEED_WRAP, 0, 0, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        System.out.println("unwrap1 = " + result1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        System.out.println("unwrap2 = " + result2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        oneToTwoShifter.position(oneToTwo.position() - 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        oneToTwoShifter.limit(oneToTwo.limit() - 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        twoToOneShifter.position(twoToOne.position() - 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        twoToOneShifter.limit(twoToOne.limit() - 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        oneToTwoShifter.compact();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        twoToOneShifter.compact();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        oneToTwo.position(oneToTwoShifter.position() + 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        oneToTwo.limit(oneToTwoShifter.limit() + 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        twoToOne.position(twoToOneShifter.position() + 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        twoToOne.limit(twoToOneShifter.limit() + 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        System.out.println("LAST WRAP");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        result1 = ssle1.wrap(appOut1, oneToTwo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        checkResult(result1, Status.CLOSED, HandshakeStatus.NEED_UNWRAP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            0, -1, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        result2 = ssle2.wrap(appOut2, twoToOne);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        checkResult(result2, Status.CLOSED, HandshakeStatus.NEED_UNWRAP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            0, -1, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        System.out.println("wrap1 = " + result1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        System.out.println("wrap2 = " + result2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        net1Len = result1.bytesProduced();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        net2Len = result2.bytesProduced();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        oneToTwo.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        twoToOne.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        oneToTwo.position(10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        twoToOne.position(10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        result1 = ssle1.unwrap(twoToOne, appIn1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        checkResult(result1, Status.CLOSED, HandshakeStatus.NOT_HANDSHAKING,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
            net1Len, 0, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        result2 = ssle2.unwrap(oneToTwo, appIn2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        checkResult(result2, Status.CLOSED, HandshakeStatus.NOT_HANDSHAKING,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            net2Len, 0, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        System.out.println("unwrap1 = " + result1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        System.out.println("unwrap2 = " + result2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        oneToTwoShifter.position(oneToTwo.position() - 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        oneToTwoShifter.limit(oneToTwo.limit() - 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        twoToOneShifter.position(twoToOne.position() - 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        twoToOneShifter.limit(twoToOne.limit() - 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        oneToTwoShifter.compact();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        twoToOneShifter.compact();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        oneToTwo.position(oneToTwoShifter.position() + 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        oneToTwo.limit(oneToTwoShifter.limit() + 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        twoToOne.position(twoToOneShifter.position() + 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        twoToOne.limit(twoToOneShifter.limit() + 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        System.out.println("EXTRA WRAP");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        result1 = ssle1.wrap(appOut1, oneToTwo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        checkResult(result1, Status.CLOSED, HandshakeStatus.NOT_HANDSHAKING,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
            0, 0, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        result2 = ssle2.wrap(appOut2, twoToOne);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        checkResult(result2, Status.CLOSED, HandshakeStatus.NOT_HANDSHAKING,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
            0, 0, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        System.out.println("wrap1 = " + result1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        System.out.println("wrap2 = " + result2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        oneToTwo.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        twoToOne.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        oneToTwo.position(10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        twoToOne.position(10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        System.out.println("EXTRA UNWRAP");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        result1 = ssle1.unwrap(twoToOne, appIn1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        checkResult(result1, Status.CLOSED, HandshakeStatus.NOT_HANDSHAKING,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
            0, 0, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        result2 = ssle2.unwrap(oneToTwo, appIn2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        checkResult(result2, Status.CLOSED, HandshakeStatus.NOT_HANDSHAKING,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
            0, 0, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        System.out.println("unwrap1 = " + result1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        System.out.println("unwrap2 = " + result2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        checkSession(ssls1, ssls2, ssls3, ssls4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        System.out.println(ssle1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        System.out.println(ssle2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
    private static void checkSession(SSLSession ssls1, SSLSession ssls2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
            SSLSession ssls3, SSLSession ssls4) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        System.out.println("\nSession Info for SSLEngine1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        System.out.println(ssls1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        System.out.println(ssls1.getCreationTime());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        String peer1 = ssls1.getPeerHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        System.out.println(peer1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        String protocol1 = ssls1.getProtocol();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        System.out.println(protocol1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        java.security.cert.Certificate cert1 = ssls1.getPeerCertificates()[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        System.out.println(cert1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        String ciphersuite1 = ssls1.getCipherSuite();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        System.out.println(ciphersuite1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        System.out.println("\nSession Info for SSLEngine2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
        System.out.println(ssls2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        System.out.println(ssls2.getCreationTime());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        String peer2 = ssls2.getPeerHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        System.out.println(peer2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        String protocol2 = ssls2.getProtocol();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        System.out.println(protocol2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        java.security.cert.Certificate cert2 = ssls2.getPeerCertificates()[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        System.out.println(cert2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        String ciphersuite2 = ssls2.getCipherSuite();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        System.out.println(ciphersuite2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        if (peer1.equals(peer2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
            throw new Exception("peer hostnames not equal");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        if (!protocol1.equals(protocol2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
            throw new Exception("protocols not equal");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        if (!cert1.equals(cert2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
            throw new Exception("certs not equal");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        if (!ciphersuite1.equals(ciphersuite2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
            throw new Exception("ciphersuites not equal");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        System.out.println("\nSession Info for SSLEngine3");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        System.out.println(ssls3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        System.out.println("\nSession Info for SSLEngine4");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        System.out.println(ssls4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        if (ssls3.equals(ssls1) || ssls4.equals(ssls2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
            throw new Exception("sessions should not be equals");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
    public static void main(String args[]) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        ConnectionTest ct = new ConnectionTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        ct.test();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
}