jdk/test/javax/crypto/CipherSpi/DirectBBRemaining.java
author prr
Thu, 18 Dec 2014 10:45:45 -0800
changeset 29908 83e2c403fefd
parent 11837 6a7fa5f263ce
child 30046 cf2c86e1819e
permissions -rw-r--r--
8067050: Better font consistency checking Reviewed-by: bae, srl, mschoene
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
11837
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
     1
/*
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
     2
 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
     4
 *
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
     7
 * published by the Free Software Foundation.
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
     8
 *
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
    13
 * accompanied this code).
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
    14
 *
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
    18
 *
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
    21
 * questions.
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
    22
 */
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
    23
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
    24
/**
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
    25
 * @test
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
    26
 * @bug 7142509
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
    27
 * @summary Cipher.doFinal(ByteBuffer,ByteBuffer) fails to
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
    28
 *     process when in.remaining() == 0
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
    29
 */
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
    30
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
    31
import java.nio.ByteBuffer;
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
    32
import java.security.SecureRandom;
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
    33
import java.util.Arrays;
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
    34
import java.util.Random;
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
    35
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
    36
import javax.crypto.Cipher;
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
    37
import javax.crypto.SecretKey;
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
    38
import javax.crypto.spec.SecretKeySpec;
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
    39
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
    40
/*
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
    41
 * Simple test case to show that Cipher.doFinal(ByteBuffer, ByteBuffer) fails to
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
    42
 * process the data internally buffered inBB the cipher when input.remaining()
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
    43
 * == 0 and at least one buffer is a direct buffer.
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
    44
 */
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
    45
public class DirectBBRemaining {
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
    46
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
    47
    private static Random random = new SecureRandom();
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
    48
    private static int testSizes = 40;
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
    49
    private static int outputFrequency = 5;
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
    50
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
    51
    public static void main(String args[]) throws Exception {
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
    52
        boolean failedOnce = false;
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
    53
        Exception failedReason = null;
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
    54
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
    55
        byte[] keyBytes = new byte[8];
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
    56
        random.nextBytes(keyBytes);
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
    57
        SecretKey key = new SecretKeySpec(keyBytes, "DES");
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
    58
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
    59
        Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding", "SunJCE");
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
    60
        cipher.init(Cipher.ENCRYPT_MODE, key);
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
    61
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
    62
        /*
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
    63
         * Iterate through various sizes to make sure that the code does empty
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
    64
         * blocks, single partial blocks, 1 full block, full + partial blocks,
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
    65
         * multiple full blocks, etc. 5 blocks (using DES) is probably overkill
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
    66
         * but will feel more confident the fix isn't breaking anything.
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
    67
         */
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
    68
        System.out.println("Output test results for every "
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
    69
                + outputFrequency + " tests...");
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
    70
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
    71
        for (int size = 0; size <= testSizes; size++) {
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
    72
            boolean output = (size % outputFrequency) == 0;
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
    73
            if (output) {
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
    74
                System.out.print("\nTesting buffer size: " + size + ":");
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
    75
            }
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
    76
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
    77
            int outSize = cipher.getOutputSize(size);
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
    78
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
    79
            try {
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
    80
                encrypt(cipher, size,
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
    81
                        ByteBuffer.allocate(size),
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
    82
                        ByteBuffer.allocate(outSize),
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
    83
                        ByteBuffer.allocateDirect(size),
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
    84
                        ByteBuffer.allocateDirect(outSize),
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
    85
                        output);
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
    86
            } catch (Exception e) {
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
    87
                System.out.print("\n    Failed with size " + size);
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
    88
                failedOnce = true;
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
    89
                failedReason = e;
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
    90
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
    91
                // If we got an exception, let's be safe for future
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
    92
                // testing and reset the cipher to a known good state.
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
    93
                cipher.init(Cipher.ENCRYPT_MODE, key);
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
    94
            }
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
    95
        }
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
    96
        if (failedOnce) {
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
    97
            throw failedReason;
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
    98
        }
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
    99
        System.out.println("\nTest Passed...");
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
   100
    }
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
   101
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
   102
    private enum TestVariant {
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
   103
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
   104
        HEAP_HEAP, HEAP_DIRECT, DIRECT_HEAP, DIRECT_DIRECT
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
   105
    };
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
   106
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
   107
    private static void encrypt(Cipher cipher, int size,
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
   108
            ByteBuffer heapIn, ByteBuffer heapOut,
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
   109
            ByteBuffer directIn, ByteBuffer directOut,
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
   110
            boolean output) throws Exception {
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
   111
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
   112
        ByteBuffer inBB = null;
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
   113
        ByteBuffer outBB = null;
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
   114
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
   115
        // Set up data and encrypt to known/expected values.
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
   116
        byte[] testdata = new byte[size];
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
   117
        random.nextBytes(testdata);
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
   118
        byte[] expected = cipher.doFinal(testdata);
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
   119
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
   120
        for (TestVariant tv : TestVariant.values()) {
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
   121
            if (output) {
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
   122
                System.out.print(" " + tv);
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
   123
            }
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
   124
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
   125
            switch (tv) {
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
   126
            case HEAP_HEAP:
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
   127
                inBB = heapIn;
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
   128
                outBB = heapOut;
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
   129
                break;
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
   130
            case HEAP_DIRECT:
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
   131
                inBB = heapIn;
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
   132
                outBB = directOut;
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
   133
                break;
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
   134
            case DIRECT_HEAP:
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
   135
                inBB = directIn;
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
   136
                outBB = heapOut;
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
   137
                break;
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
   138
            case DIRECT_DIRECT:
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
   139
                inBB = directIn;
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
   140
                outBB = directOut;
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
   141
                break;
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
   142
            }
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
   143
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
   144
            inBB.clear();
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
   145
            outBB.clear();
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
   146
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
   147
            inBB.put(testdata);
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
   148
            inBB.flip();
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
   149
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
   150
            // Process all data in one shot, but don't call doFinal() yet.
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
   151
            // May store up to n-1 bytes (w/block size n) internally.
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
   152
            cipher.update(inBB, outBB);
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
   153
            if (inBB.hasRemaining()) {
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
   154
                throw new Exception("buffer not empty");
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
   155
            }
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
   156
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
   157
            // finish encryption and process all data buffered
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
   158
            cipher.doFinal(inBB, outBB);
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
   159
            outBB.flip();
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
   160
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
   161
            // validate output size
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
   162
            if (outBB.remaining() != expected.length) {
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
   163
                throw new Exception(
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
   164
                        "incomplete encryption output, expected "
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
   165
                        + expected.length + " bytes but was only "
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
   166
                        + outBB.remaining() + " bytes");
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
   167
            }
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
   168
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
   169
            // validate output data
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
   170
            byte[] encrypted = new byte[outBB.remaining()];
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
   171
            outBB.get(encrypted);
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
   172
            if (!Arrays.equals(expected, encrypted)) {
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
   173
                throw new Exception("bad encryption output");
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
   174
            }
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
   175
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
   176
            if (!Arrays.equals(cipher.doFinal(), cipher.doFinal())) {
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
   177
                throw new Exception("Internal buffers still held data!");
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
   178
            }
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
   179
        }
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
   180
    }
6a7fa5f263ce 7142509: Cipher.doFinal(ByteBuffer,ByteBuffer) fails to process when in.remaining() == 0
wetmore
parents:
diff changeset
   181
}