test/jdk/javax/net/ssl/SSLEngine/Basics.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 23052 jdk/test/javax/net/ssl/SSLEngine/Basics.java@241885315119
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
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: 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 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
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
    29
 * @ignore JSSE supported cipher suites are changed with CR 6916074,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
    30
 *     need to update this test case in JDK 7 soon
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * This is intended to test many of the basic API calls to the SSLEngine
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * interface.  This doesn't really exercise much of the SSL code.
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 java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.nio.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.net.ssl.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.net.ssl.SSLEngineResult.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
public class Basics {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
23052
241885315119 8032473: Restructure JSSE regression test hierarchy in jdk test
xuelei
parents: 7043
diff changeset
    46
    private static String pathToStores = "../etc";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private static String keyStoreFile = "keystore";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private static String trustStoreFile = "truststore";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private static String passwd = "passphrase";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private static String keyFilename =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            System.getProperty("test.src", "./") + "/" + pathToStores +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
                "/" + keyStoreFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private static String trustFilename =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            System.getProperty("test.src", "./") + "/" + pathToStores +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
                "/" + trustStoreFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    public static void main(String args[]) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        KeyStore ks = KeyStore.getInstance("JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        KeyStore ts = KeyStore.getInstance("JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        char[] passphrase = "passphrase".toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        ks.load(new FileInputStream(keyFilename), passphrase);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        ts.load(new FileInputStream(trustFilename), passphrase);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        kmf.init(ks, passphrase);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        tmf.init(ks);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        SSLContext sslCtx = SSLContext.getInstance("TLS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        sslCtx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        SSLEngine ssle = sslCtx.createSSLEngine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        System.out.println(ssle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        String [] suites = ssle.getSupportedCipherSuites();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        String secondSuite = suites[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        String [] oneSuites = new String [] { secondSuite };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        printStrings("Supported Ciphersuites", suites);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        printStrings("Enabled Ciphersuites", ssle.getEnabledCipherSuites());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        ssle.setEnabledCipherSuites(oneSuites);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        printStrings("Set Ciphersuites", ssle.getEnabledCipherSuites());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        suites = ssle.getEnabledCipherSuites();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        if ((ssle.getEnabledCipherSuites().length != 1) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                !(suites[0].equals(secondSuite))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            throw new Exception("set ciphers not what was expected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        String [] protocols = ssle.getSupportedProtocols();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        String secondProtocol = protocols[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        String [] oneProtocols = new String [] { protocols[1] };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        printStrings("Supported Protocols", protocols);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        printStrings("Enabled Protocols", ssle.getEnabledProtocols());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        ssle.setEnabledProtocols(oneProtocols);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        printStrings("Set Protocols", ssle.getEnabledProtocols());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        protocols = ssle.getEnabledProtocols();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        if ((ssle.getEnabledProtocols().length != 1) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                !(protocols[0].equals(secondProtocol))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            throw new Exception("set protocols not what was expected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        System.out.println("Checking get/setUseClientMode");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        ssle.setUseClientMode(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        if (ssle.getUseClientMode() != true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            throw new Exception("set/getUseClientMode false");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        ssle.setUseClientMode(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        if (ssle.getUseClientMode() != false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            throw new Exception("set/getUseClientMode true");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        System.out.println("Checking get/setClientAuth");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        ssle.setNeedClientAuth(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        if (ssle.getNeedClientAuth() != false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            throw new Exception("set/getNeedClientAuth true");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        ssle.setNeedClientAuth(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        if (ssle.getNeedClientAuth() != true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            throw new Exception("set/getNeedClientAuth false");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        ssle.setWantClientAuth(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        if (ssle.getNeedClientAuth() == true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            throw new Exception("set/getWantClientAuth need = true");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        if (ssle.getWantClientAuth() != true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            throw new Exception("set/getNeedClientAuth false");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        ssle.setWantClientAuth(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        if (ssle.getWantClientAuth() != false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            throw new Exception("set/getNeedClientAuth true");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
         * Reset back to client mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        ssle.setUseClientMode(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        System.out.println("checking session creation");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        ssle.setEnableSessionCreation(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        if (ssle.getEnableSessionCreation() != false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            throw new Exception("set/getSessionCreation true");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        ssle.setEnableSessionCreation(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        if (ssle.getEnableSessionCreation() != true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            throw new Exception("set/getSessionCreation false");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        /* Checking for overflow wrap/unwrap() */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        ByteBuffer smallBB = ByteBuffer.allocate(10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        if (ssle.wrap(smallBB, smallBB).getStatus() !=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                Status.BUFFER_OVERFLOW) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            throw new Exception("wrap should have overflowed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        // For unwrap(), the BUFFER_OVERFLOW will not be generated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        // until received SSL/TLS application data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        // Test test/javax/net/ssl/NewAPIs/SSLEngine/LargePacket will check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        // BUFFER_OVERFLOW/UNDERFLOW for both wrap() and unwrap().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        //if (ssle.unwrap(smallBB, smallBB).getStatus() !=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        //      Status.BUFFER_OVERFLOW) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        //    throw new Exception("unwrap should have overflowed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        //}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        SSLSession ssls = ssle.getSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        ByteBuffer appBB =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            ByteBuffer.allocate(ssls.getApplicationBufferSize());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        ByteBuffer netBB =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            ByteBuffer.allocate(ssls.getPacketBufferSize());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        appBB.position(10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
         * start handshake, drain buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        if (ssle.wrap(appBB, netBB).getHandshakeStatus() !=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                HandshakeStatus.NEED_UNWRAP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            throw new Exception("initial client hello needs unwrap");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        /* Checking for overflow wrap/unwrap() */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        if (ssle.wrap(appBB, netBB).getStatus() !=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                Status.BUFFER_OVERFLOW) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            throw new Exception("unwrap should have overflowed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        ByteBuffer ro = appBB.asReadOnlyBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        System.out.println("checking for wrap/unwrap on RO Buffers");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            ssle.wrap(netBB, ro);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            throw new Exception("wrap wasn't ReadOnlyBufferException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        } catch (ReadOnlyBufferException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            System.out.println("Caught the ReadOnlyBuffer: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            ssle.unwrap(netBB, ro);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            throw new Exception("unwrap wasn't ReadOnlyBufferException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        } catch (ReadOnlyBufferException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            System.out.println("Caught the ReadOnlyBuffer: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        appBB.position(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        System.out.println("Check various UNDERFLOW conditions");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        SSLEngineResult sslER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        if ((sslER =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                ssle.unwrap(ByteBuffer.wrap(smallSSLHeader),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                appBB)).getStatus() !=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                Status.BUFFER_UNDERFLOW) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            System.out.println(sslER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            throw new Exception("unwrap should underflow");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        if ((sslER =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                ssle.unwrap(ByteBuffer.wrap(incompleteSSLHeader),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                appBB)).getStatus() !=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                Status.BUFFER_UNDERFLOW) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            System.out.println(sslER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            throw new Exception("unwrap should underflow");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        if ((sslER =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                ssle.unwrap(ByteBuffer.wrap(smallv2Header),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                appBB)).getStatus() !=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                Status.BUFFER_UNDERFLOW) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            System.out.println(sslER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            throw new Exception("unwrap should underflow");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        // junk inbound message
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            ssle.unwrap(ByteBuffer.wrap(gobblydegook), appBB);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            throw new Exception("Didn't catch the nasty SSLException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        } catch (SSLException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            System.out.println("caught the nasty SSLException: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        System.out.println("Test PASSED");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    static byte [] smallSSLHeader = new byte [] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        (byte) 0x16, (byte) 0x03, (byte) 0x01,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        (byte) 0x05 };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    static byte [] incompleteSSLHeader = new byte [] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        (byte) 0x16, (byte) 0x03, (byte) 0x01,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        (byte) 0x00, (byte) 0x5,  // 5 bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        (byte) 0x16, (byte) 0x03, (byte) 0x01, (byte) 0x00 };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    static byte [] smallv2Header = new byte [] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        (byte) 0x80, (byte) 0x03, (byte) 0x01,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        (byte) 0x00 };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    static byte [] gobblydegook = new byte [] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        // "HELLO HELLO"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        (byte) 0x48, (byte) 0x45, (byte) 0x4C, (byte) 0x4C, (byte) 0x20,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        (byte) 0x48, (byte) 0x45, (byte) 0x4C, (byte) 0x4C };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    static void printStrings(String label, String [] strs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        System.out.println(label);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        for (int i = 0; i < strs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            System.out.println("    " + strs[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
}