jdk/test/javax/net/ssl/DTLS/DTLSIncorrectAppDataTest.java
changeset 31057 babdeee3c007
child 40949 be7a612613ae
equal deleted inserted replaced
31008:5b500c93ce48 31057:babdeee3c007
       
     1 /*
       
     2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 
       
    24 /*
       
    25  * @test
       
    26  * @bug 8043758
       
    27  * @summary Testing DTLS incorrect app data packages unwrapping.
       
    28  * @key randomness
       
    29  * @library /sun/security/krb5/auto /lib/testlibrary /javax/net/ssl/TLSCommon
       
    30  * @run main/othervm -Dtest.security.protocol=DTLS
       
    31  *      -Dtest.mode=norm DTLSIncorrectAppDataTest
       
    32  * @run main/othervm -Dtest.security.protocol=DTLS
       
    33  *      -Dtest.mode=norm_sni DTLSIncorrectAppDataTest
       
    34  * @run main/othervm -Dtest.security.protocol=DTLS
       
    35  *      -Dtest.mode=krb DTLSIncorrectAppDataTest
       
    36  */
       
    37 
       
    38 import java.nio.ByteBuffer;
       
    39 import javax.net.ssl.SSLContext;
       
    40 import javax.net.ssl.SSLEngine;
       
    41 import javax.net.ssl.SSLEngineResult;
       
    42 import javax.net.ssl.SSLException;
       
    43 import java.util.Random;
       
    44 import jdk.testlibrary.RandomFactory;
       
    45 
       
    46 /**
       
    47  * Testing DTLS incorrect app data packages unwrapping. Incorrect application
       
    48  * data packages should be ignored by DTLS SSLEngine.
       
    49  */
       
    50 public class DTLSIncorrectAppDataTest extends SSLEngineTestCase {
       
    51 
       
    52     private final String MESSAGE = "Hello peer!";
       
    53 
       
    54     public static void main(String[] s) {
       
    55         DTLSIncorrectAppDataTest test = new DTLSIncorrectAppDataTest();
       
    56         setUpAndStartKDCIfNeeded();
       
    57         test.runTests();
       
    58     }
       
    59 
       
    60     @Override
       
    61     protected void testOneCipher(String cipher) {
       
    62         SSLContext context = getContext();
       
    63         int maxPacketSize = getMaxPacketSize();
       
    64         boolean useSNI = !TEST_MODE.equals("norm");
       
    65         SSLEngine clientEngine = getClientSSLEngine(context, useSNI);
       
    66         SSLEngine serverEngine = getServerSSLEngine(context, useSNI);
       
    67         clientEngine.setEnabledCipherSuites(new String[]{cipher});
       
    68         serverEngine.setEnabledCipherSuites(new String[]{cipher});
       
    69         serverEngine.setNeedClientAuth(!cipher.contains("anon"));
       
    70         try {
       
    71             doHandshake(clientEngine, serverEngine, maxPacketSize,
       
    72                     HandshakeMode.INITIAL_HANDSHAKE);
       
    73             checkIncorrectAppDataUnwrap(clientEngine, serverEngine);
       
    74             checkIncorrectAppDataUnwrap(serverEngine, clientEngine);
       
    75         } catch (SSLException ssle) {
       
    76             throw new AssertionError("Error during handshake or sending app data",
       
    77                     ssle);
       
    78         }
       
    79     }
       
    80 
       
    81     private void checkIncorrectAppDataUnwrap(SSLEngine sendEngine,
       
    82             SSLEngine recvEngine) throws SSLException {
       
    83         String direction = sendEngine.getUseClientMode() ? "client"
       
    84                 : "server";
       
    85         System.out.println("================================================="
       
    86                 + "===========");
       
    87         System.out.println("Testing DTLS incorrect app data packages unwrapping"
       
    88                 + " by sending data from " + direction);
       
    89         ByteBuffer app = ByteBuffer.wrap(MESSAGE.getBytes());
       
    90         ByteBuffer net = doWrap(sendEngine, direction, 0, app);
       
    91         final Random RNG = RandomFactory.getRandom();
       
    92         int randomPlace = RNG.nextInt(net.remaining());
       
    93         net.array()[randomPlace] += 1;
       
    94         app = ByteBuffer.allocate(recvEngine.getSession()
       
    95                 .getApplicationBufferSize());
       
    96         recvEngine.unwrap(net, app);
       
    97         app.flip();
       
    98         int length = app.remaining();
       
    99         System.out.println("Unwrapped " + length + " bytes.");
       
   100     }
       
   101 }