jdk/test/javax/net/ssl/SSLEngine/Arrays.java
author prr
Thu, 18 Dec 2014 10:45:45 -0800
changeset 29908 83e2c403fefd
parent 23052 241885315119
permissions -rw-r--r--
8067050: Better font consistency checking Reviewed-by: bae, srl, mschoene
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2004, 2007, 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: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
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 5019096
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Add scatter/gather APIs for SSLEngine
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.net.ssl.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.net.ssl.SSLEngineResult.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.nio.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
public class Arrays {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    private static boolean debug = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    private SSLContext sslc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    private SSLEngine ssle1;    // client
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private SSLEngine ssle2;    // server
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
23052
241885315119 8032473: Restructure JSSE regression test hierarchy in jdk test
xuelei
parents: 5506
diff changeset
    45
    private static String pathToStores = "../etc";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private static String keyStoreFile = "keystore";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private static String trustStoreFile = "truststore";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private static String passwd = "passphrase";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private static String keyFilename =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            System.getProperty("test.src", "./") + "/" + pathToStores +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
                "/" + keyStoreFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private static String trustFilename =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            System.getProperty("test.src", "./") + "/" + pathToStores +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
                "/" + trustStoreFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private ByteBuffer [] appOutArray1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private ByteBuffer [] appInArray1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private ByteBuffer appOut2;         // write side of ssle2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private ByteBuffer appIn2;          // read side of ssle2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private ByteBuffer oneToTwo;        // "reliable" transport ssle1->ssle2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private ByteBuffer twoToOne;        // "reliable" transport ssle2->ssle1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * Majority of the test case is here, setup is done below.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private void createSSLEngines() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        ssle1 = sslc.createSSLEngine("client", 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        ssle1.setUseClientMode(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        ssle2 = sslc.createSSLEngine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        ssle2.setUseClientMode(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        ssle2.setNeedClientAuth(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private void runTest() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        boolean dataDone = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        createSSLEngines();
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
        SSLEngineResult result1;        // ssle1's results from last operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        SSLEngineResult result2;        // ssle2's results from last operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        while (!isEngineClosed(ssle1) || !isEngineClosed(ssle2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            log("================");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            result1 = ssle1.wrap(appOutArray1, oneToTwo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            result2 = ssle2.wrap(appOut2, twoToOne);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            log("wrap1:  " + result1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            log("oneToTwo  = " + oneToTwo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            log("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            log("wrap2:  " + result2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            log("twoToOne  = " + twoToOne);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            runDelegatedTasks(result1, ssle1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            runDelegatedTasks(result2, ssle2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            oneToTwo.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            twoToOne.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            log("----");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            result1 = ssle1.unwrap(twoToOne, appInArray1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            result2 = ssle2.unwrap(oneToTwo, appIn2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            log("unwrap1: " + result1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            log("twoToOne  = " + twoToOne);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            log("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            log("unwrap2: " + result2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            log("oneToTwo  = " + oneToTwo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            runDelegatedTasks(result1, ssle1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            runDelegatedTasks(result2, ssle2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            oneToTwo.compact();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            twoToOne.compact();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
             * If we've transfered all the data between app1 and app2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
             * we try to close and see what that gets us.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            if (!dataDone) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                boolean done = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                for (int i = 0; i < appOutArray1.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                    if (appOutArray1[i].remaining() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                        done = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                if (appOut2.remaining() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                    done = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                if (done) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                    log("Closing ssle1's *OUTBOUND*...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                    for (int i = 0; i < appOutArray1.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                        appOutArray1[i].rewind();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                    ssle1.closeOutbound();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                    dataDone = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        checkTransfer(appOutArray1,  appIn2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        appInArray1[appInArray1.length - 1].limit(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            appInArray1[appInArray1.length - 1].position());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        checkTransfer(appInArray1, appOut2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    public static void main(String args[]) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        Arrays test;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        test = new Arrays();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        test.createSSLEngines();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        test.runTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        System.out.println("Test Passed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * **********************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * Majority of the test case is above, below is just setup stuff
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * **********************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    public Arrays() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        sslc = getSSLContext(keyFilename, trustFilename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * Create an initialized SSLContext to use for this test.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    private SSLContext getSSLContext(String keyFile, String trustFile)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        KeyStore ks = KeyStore.getInstance("JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        KeyStore ts = KeyStore.getInstance("JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        char[] passphrase = "passphrase".toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        ks.load(new FileInputStream(keyFile), passphrase);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        ts.load(new FileInputStream(trustFile), passphrase);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        kmf.init(ks, passphrase);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        tmf.init(ts);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        SSLContext sslCtx = SSLContext.getInstance("TLS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        sslCtx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        return sslCtx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    private void createBuffers() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        // Size the buffers as appropriate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        SSLSession session = ssle1.getSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        int appBufferMax = session.getApplicationBufferSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        int netBufferMax = session.getPacketBufferSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        appIn2 = ByteBuffer.allocateDirect(appBufferMax + 50);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        oneToTwo = ByteBuffer.allocateDirect(netBufferMax);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        twoToOne = ByteBuffer.allocateDirect(netBufferMax);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        ByteBuffer strBB = ByteBuffer.wrap(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            "Hi Engine2, I'm SSLEngine1, So Be it" .getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        strBB.position(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        strBB.limit(5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        ByteBuffer appOut1a = strBB.slice();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        strBB.position(5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        strBB.limit(15);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        ByteBuffer appOut1b = strBB.slice();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        strBB.position(15);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        strBB.limit(strBB.capacity());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        ByteBuffer appOut1c = strBB.slice();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        strBB.rewind();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        appOutArray1 = new ByteBuffer [] { appOut1a, appOut1b, appOut1c };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        appOut2 = ByteBuffer.wrap("Hello Engine1, I'm SSLEngine2".getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        ByteBuffer appIn1a = ByteBuffer.allocateDirect(5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        ByteBuffer appIn1b = ByteBuffer.allocateDirect(10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        ByteBuffer appIn1c = ByteBuffer.allocateDirect(appBufferMax + 50);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        appInArray1 = new ByteBuffer [] { appIn1a, appIn1b, appIn1c };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        log("AppOut1a = " + appOut1a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        log("AppOut1a = " + appOut1b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        log("AppOut1a = " + appOut1c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        log("AppOut2 = " + appOut2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        log("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    private static void runDelegatedTasks(SSLEngineResult result,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            SSLEngine engine) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        if (result.getHandshakeStatus() == HandshakeStatus.NEED_TASK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            Runnable runnable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            while ((runnable = engine.getDelegatedTask()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                log("running delegated task...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                runnable.run();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    private static boolean isEngineClosed(SSLEngine engine) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        return (engine.isOutboundDone() && engine.isInboundDone());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    private static void checkTransfer(ByteBuffer [] a, ByteBuffer b)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        b.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        for (int i = 0; i < a.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            a[i].rewind();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            b.limit(b.position() + a[i].remaining());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            if (!a[i].equals(b)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                throw new Exception("Data didn't transfer cleanly");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            b.position(b.limit());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        log("Data transferred cleanly");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    private static void log(String str) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            System.out.println(str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
}