jdk/test/com/sun/crypto/provider/Cipher/AES/TestShortBuffer.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 2 90ce3da70b43
child 35315 67dcc46f8241
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
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 4979126
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Ensure update()/doFinal() matches javadoc description
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * when ShortBufferException is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @author Valerie Peng
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.Arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.spec.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.math.BigInteger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.crypto.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.crypto.spec.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.security.Provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import com.sun.crypto.provider.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
public class TestShortBuffer {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    private static final String ALGO = "AES";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    private static final String[] MODES = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        "ECB", "CBC", "PCBC", "CFB16", "OFB8"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private static final SecretKey KEY =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        new SecretKeySpec(new byte[16], ALGO);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private static byte[] SHORTBUFFER = new byte[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private static final byte[] PLAINTEXT  = new byte[30];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        PLAINTEXT[0] = (byte)0x15;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private Cipher ci = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private byte[] in = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private byte[] expected = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private byte[] out = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private int outOffset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private TestShortBuffer(Cipher ci) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        this.ci = ci;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private void init(byte[] in, byte[] expected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        this.in = (byte[]) in.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        this.expected = (byte[]) expected.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        this.out = new byte[expected.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        this.outOffset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private static void runTest() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        // Initialization
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        for (int i = 0; i < MODES.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            System.out.println("===== TESTING MODE " + MODES[i] + " =====");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            Cipher ci = Cipher.getInstance(ALGO+"/"+MODES[i]+"/PKCS5Padding",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                                           "SunJCE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            TestShortBuffer test = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            int stored = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            AlgorithmParameters params = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            byte[] cipherText = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            byte[] shortBuffer = new byte[8];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            for (int k = 0; k < 2; k++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                byte[] expected = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                switch (k) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                case 0: // Encryption
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                    System.out.println("Testing with Cipher.ENCRYPT_MODE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                    ci.init(Cipher.ENCRYPT_MODE, KEY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                    cipherText = ci.doFinal(PLAINTEXT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                    test = new TestShortBuffer(ci);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                    test.init(PLAINTEXT, cipherText);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                    params = ci.getParameters();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                case 1: // Decryption
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                    System.out.println("Testing with Cipher.DECRYPT_MODE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                    ci.init(Cipher.DECRYPT_MODE, KEY, params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                    test = new TestShortBuffer(ci);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                    test.init(cipherText, PLAINTEXT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                int offset = 2 + i*5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                test.testUpdate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                test.testUpdateWithUpdate(offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                test.testDoFinal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                test.testDoFinalWithUpdate(offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    private void checkOutput() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        if (!Arrays.equals(out, expected)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            System.out.println("got: " + new BigInteger(out));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            System.out.println("expect: " + new BigInteger(expected));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            throw new Exception("Generated different outputs");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    private void testUpdate() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        outOffset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        int stored = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            stored = ci.update(in, 0, in.length, SHORTBUFFER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            throw new Exception("Should throw ShortBufferException!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        } catch (ShortBufferException sbe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            System.out.println("Expected SBE thrown....");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            // retry with a large-enough buffer according to javadoc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            stored = ci.update(in, 0, in.length, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            stored = ci.doFinal(out, outOffset += stored);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            if (out.length != (stored + outOffset)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                throw new Exception("Wrong number of output bytes");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        checkOutput();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    private void testUpdateWithUpdate(int offset) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        outOffset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        int stored = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        byte[] out1 = ci.update(in, 0, offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        if (out1 != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            System.arraycopy(out1, 0, out, 0, out1.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            outOffset += out1.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            stored = ci.update(in, offset, in.length-offset, SHORTBUFFER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            throw new Exception("Should throw ShortBufferException!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        } catch (ShortBufferException sbe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            System.out.println("Expected SBE thrown....");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            // retry with a large-enough buffer according to javadoc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            stored = ci.update(in, offset, in.length-offset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                               out, outOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            stored = ci.doFinal(out, outOffset+=stored);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            if (out.length != (stored + outOffset)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                throw new Exception("Wrong number of output bytes");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        checkOutput();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    private void testDoFinal() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        outOffset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        int stored = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            stored = ci.doFinal(in, 0, in.length, SHORTBUFFER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            throw new Exception("Should throw ShortBufferException!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        } catch (ShortBufferException sbe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            System.out.println("Expected SBE thrown....");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            // retry with a large-enough buffer according to javadoc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            stored = ci.doFinal(in, 0, in.length, out, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            if (out.length != stored) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                throw new Exception("Wrong number of output bytes");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        checkOutput();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    private void testDoFinalWithUpdate(int offset) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        outOffset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        int stored = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        byte[] out1 = ci.update(in, 0, offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        if (out1 != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            System.arraycopy(out1, 0, out, 0, out1.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            outOffset += out1.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            stored = ci.doFinal(in, offset, in.length-offset, SHORTBUFFER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            throw new Exception("Should throw ShortBufferException!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        } catch (ShortBufferException sbe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            System.out.println("Expected SBE thrown....");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            // retry with a large-enough buffer according to javadoc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            stored = ci.doFinal(in, offset, in.length-offset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                                out, outOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            if (out.length != (stored + outOffset)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                throw new Exception("Wrong number of output bytes");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        checkOutput();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    public static void main(String[] argv) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        runTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        System.out.println("Test Passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
}