jdk/test/javax/crypto/Cipher/ByteBuffersNull.java
author dbuck
Tue, 18 Aug 2015 04:29:28 -0700
changeset 32417 6859107fc6c3
parent 5506 202f599c92aa
permissions -rw-r--r--
8133666: OperatingSystemMXBean reports abnormally high machine CPU consumption on Linux Reviewed-by: sla, mgronlun
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 5000980
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Check NullPointerException for cipherSpi.engineUpdate(x, null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import javax.crypto.CipherSpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.InvalidKeyException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.AlgorithmParameters;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.InvalidAlgorithmParameterException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.NoSuchAlgorithmException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.SecureRandom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.spec.AlgorithmParameterSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.security.Key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.security.Security;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.crypto.IllegalBlockSizeException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.crypto.BadPaddingException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.crypto.ShortBufferException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.crypto.NoSuchPaddingException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.security.GeneralSecurityException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.nio.ByteBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
public class ByteBuffersNull {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    static final int bufSize = 1024;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    public void testCase010() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        CipherSpiImpl c = new CipherSpiImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        BufferDescr[] inBuffers = getByteBuffersForTest(bufSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        int failureCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        for (int i = 0; i < inBuffers.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            String key = inBuffers[i].descr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            ByteBuffer bb = inBuffers[i].buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                c.engineUpdate(bb, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                throw new Exception("No Exception?!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            }  catch (NullPointerException npe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                // Expected behaviour - pass
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                System.out.println("OK: " + npe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    // Creates a ByteBuffer with a desired properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    ByteBuffer getByteBuffer(int capacity, int position,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            boolean limitAt0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        ByteBuffer bb = ByteBuffer.allocate(capacity);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        bb.position(position);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        if (limitAt0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            bb.limit(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        return bb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    BufferDescr[] getByteBuffersForTest(int defaultSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        int caseNum = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        BufferDescr[] buffers = new BufferDescr[caseNum];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        // ByteBuffer with capacity 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        buffers[0]= new BufferDescr("ByteBuffer with capacity == 0",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            getByteBuffer(0,0,false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        // ByteBuffer with some space but limit = 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        buffers[1] = new BufferDescr(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            "ByteBuffer with some capacity but limit == 0",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            getByteBuffer(defaultSize, 0, true));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        // ByteBuffer with some remaining data (limit = capacity)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        buffers[2] = new BufferDescr("ByteBuffer with some data",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            getByteBuffer(defaultSize,0,false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        // ByteBuffer with some data but position is at the limit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        buffers[3] = new BufferDescr(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            "ByteBuffer with data but position at the limit",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            getByteBuffer(defaultSize, defaultSize,false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        return buffers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    public static void main(String argv[]) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        ByteBuffersNull test = new ByteBuffersNull();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        test.testCase010();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    class BufferDescr {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        BufferDescr(String d, ByteBuffer b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            descr = d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            buf = b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        public String descr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        public ByteBuffer buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    public class CipherSpiImpl extends CipherSpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        public CipherSpiImpl() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        public void engineSetMode(String mode)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            throws NoSuchAlgorithmException { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        public void engineSetPadding(String padding)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            throws NoSuchPaddingException { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        public int engineGetBlockSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        public int engineGetOutputSize(int inputLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        public byte[] engineGetIV() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        public AlgorithmParameters engineGetParameters() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        public void engineInit(int opmode, Key key, SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            throws InvalidKeyException { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        public void engineInit(int opmode, Key key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                               AlgorithmParameterSpec params, SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            throws InvalidKeyException, InvalidAlgorithmParameterException { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        public void engineInit(int opmode, Key key, AlgorithmParameters params,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                               SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            throws InvalidKeyException, InvalidAlgorithmParameterException { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        public byte[] engineUpdate(byte[] input, int offset, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        public int engineUpdate(byte[] input, int inputOffset, int inputLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                                byte[] output, int outputOffset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            throws ShortBufferException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        public byte[] engineDoFinal(byte[] input, int inputOffset, int inputLen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            throws IllegalBlockSizeException, BadPaddingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        public int engineDoFinal(byte[] input, int inputOffset, int inputLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                                 byte[] output, int outputOffset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            throws ShortBufferException, IllegalBlockSizeException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            BadPaddingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        public byte[] engineWrap(Key key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            throws IllegalBlockSizeException, InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            return super.engineWrap(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        public Key engineUnwrap(byte[] wKey, String wKeyAlgorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                                int wKeyType) throws InvalidKeyException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            return super.engineUnwrap(wKey, wKeyAlgorithm, wKeyType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        public int engineGetKeySize(Key key) throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            return super.engineGetKeySize(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        public int engineDoFinal(ByteBuffer input, ByteBuffer output)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            throws ShortBufferException, IllegalBlockSizeException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            BadPaddingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            return super.engineDoFinal(input, output);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        public int engineUpdate(ByteBuffer input, ByteBuffer output)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            throws ShortBufferException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            return super.engineUpdate(input, output);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
}