0N/A/*
2362N/A * Copyright (c) 2004, 2007, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/**
0N/A * @test
0N/A * @bug 5000980
0N/A * @summary Check NullPointerException for cipherSpi.engineUpdate(x, null)
0N/A */
0N/A
0N/Aimport javax.crypto.CipherSpi;
0N/Aimport java.security.InvalidKeyException;
0N/Aimport java.security.AlgorithmParameters;
0N/Aimport java.security.InvalidAlgorithmParameterException;
0N/Aimport java.security.NoSuchAlgorithmException;
0N/Aimport java.security.SecureRandom;
0N/Aimport java.security.spec.AlgorithmParameterSpec;
0N/Aimport java.security.Key;
0N/Aimport java.security.Security;
0N/Aimport javax.crypto.IllegalBlockSizeException;
0N/Aimport javax.crypto.BadPaddingException;
0N/Aimport javax.crypto.ShortBufferException;
0N/Aimport javax.crypto.NoSuchPaddingException;
0N/Aimport java.security.GeneralSecurityException;
0N/Aimport java.nio.ByteBuffer;
0N/A
0N/Apublic class ByteBuffersNull {
0N/A
0N/A static final int bufSize = 1024;
0N/A
0N/A public void testCase010() throws Exception {
0N/A CipherSpiImpl c = new CipherSpiImpl();
0N/A BufferDescr[] inBuffers = getByteBuffersForTest(bufSize);
0N/A int failureCount = 0;
0N/A
0N/A for (int i = 0; i < inBuffers.length; i++) {
0N/A String key = inBuffers[i].descr;
0N/A ByteBuffer bb = inBuffers[i].buf;
0N/A
0N/A try {
0N/A
0N/A c.engineUpdate(bb, null);
0N/A
0N/A throw new Exception("No Exception?!");
0N/A } catch (NullPointerException npe) {
0N/A // Expected behaviour - pass
0N/A System.out.println("OK: " + npe);
0N/A }
0N/A }
0N/A }
0N/A
0N/A // Creates a ByteBuffer with a desired properties
0N/A ByteBuffer getByteBuffer(int capacity, int position,
0N/A boolean limitAt0) {
0N/A ByteBuffer bb = ByteBuffer.allocate(capacity);
0N/A
0N/A bb.position(position);
0N/A
0N/A if (limitAt0)
0N/A bb.limit(0);
0N/A
0N/A return bb;
0N/A }
0N/A
0N/A BufferDescr[] getByteBuffersForTest(int defaultSize) {
0N/A int caseNum = 4;
0N/A
0N/A BufferDescr[] buffers = new BufferDescr[caseNum];
0N/A
0N/A // ByteBuffer with capacity 0
0N/A buffers[0]= new BufferDescr("ByteBuffer with capacity == 0",
0N/A getByteBuffer(0,0,false));
0N/A
0N/A // ByteBuffer with some space but limit = 0
0N/A buffers[1] = new BufferDescr(
0N/A "ByteBuffer with some capacity but limit == 0",
0N/A getByteBuffer(defaultSize, 0, true));
0N/A
0N/A // ByteBuffer with some remaining data (limit = capacity)
0N/A buffers[2] = new BufferDescr("ByteBuffer with some data",
0N/A getByteBuffer(defaultSize,0,false));
0N/A
0N/A // ByteBuffer with some data but position is at the limit
0N/A buffers[3] = new BufferDescr(
0N/A "ByteBuffer with data but position at the limit",
0N/A getByteBuffer(defaultSize, defaultSize,false));
0N/A
0N/A return buffers;
0N/A }
0N/A
0N/A public static void main(String argv[]) throws Exception {
0N/A ByteBuffersNull test = new ByteBuffersNull();
0N/A test.testCase010();
0N/A }
0N/A
0N/A class BufferDescr {
0N/A BufferDescr(String d, ByteBuffer b) {
0N/A descr = d;
0N/A buf = b;
0N/A }
0N/A
0N/A public String descr;
0N/A public ByteBuffer buf;
0N/A }
0N/A
0N/A public class CipherSpiImpl extends CipherSpi {
0N/A
0N/A public CipherSpiImpl() {
0N/A super();
0N/A }
0N/A
0N/A public void engineSetMode(String mode)
0N/A throws NoSuchAlgorithmException { }
0N/A
0N/A public void engineSetPadding(String padding)
0N/A throws NoSuchPaddingException { }
0N/A
0N/A public int engineGetBlockSize() {
0N/A return 0;
0N/A }
0N/A
0N/A public int engineGetOutputSize(int inputLen) {
0N/A return 0;
0N/A }
0N/A
0N/A public byte[] engineGetIV() {
0N/A return null;
0N/A }
0N/A
0N/A public AlgorithmParameters engineGetParameters() {
0N/A return null;
0N/A }
0N/A
0N/A public void engineInit(int opmode, Key key, SecureRandom random)
0N/A throws InvalidKeyException { }
0N/A
0N/A public void engineInit(int opmode, Key key,
0N/A AlgorithmParameterSpec params, SecureRandom random)
0N/A throws InvalidKeyException, InvalidAlgorithmParameterException { }
0N/A
0N/A public void engineInit(int opmode, Key key, AlgorithmParameters params,
0N/A SecureRandom random)
0N/A throws InvalidKeyException, InvalidAlgorithmParameterException { }
0N/A
0N/A public byte[] engineUpdate(byte[] input, int offset, int len) {
0N/A return null;
0N/A }
0N/A
0N/A public int engineUpdate(byte[] input, int inputOffset, int inputLen,
0N/A byte[] output, int outputOffset)
0N/A throws ShortBufferException {
0N/A return 0;
0N/A }
0N/A
0N/A public byte[] engineDoFinal(byte[] input, int inputOffset, int inputLen)
0N/A throws IllegalBlockSizeException, BadPaddingException {
0N/A return null;
0N/A }
0N/A
0N/A public int engineDoFinal(byte[] input, int inputOffset, int inputLen,
0N/A byte[] output, int outputOffset)
0N/A throws ShortBufferException, IllegalBlockSizeException,
0N/A BadPaddingException {
0N/A return 0;
0N/A }
0N/A
0N/A public byte[] engineWrap(Key key)
0N/A throws IllegalBlockSizeException, InvalidKeyException {
0N/A return super.engineWrap(key);
0N/A }
0N/A
0N/A public Key engineUnwrap(byte[] wKey, String wKeyAlgorithm,
0N/A int wKeyType) throws InvalidKeyException,
0N/A NoSuchAlgorithmException {
0N/A return super.engineUnwrap(wKey, wKeyAlgorithm, wKeyType);
0N/A }
0N/A
0N/A public int engineGetKeySize(Key key) throws InvalidKeyException {
0N/A return super.engineGetKeySize(key);
0N/A }
0N/A
0N/A public int engineDoFinal(ByteBuffer input, ByteBuffer output)
0N/A throws ShortBufferException, IllegalBlockSizeException,
0N/A BadPaddingException {
0N/A return super.engineDoFinal(input, output);
0N/A }
0N/A
0N/A public int engineUpdate(ByteBuffer input, ByteBuffer output)
0N/A throws ShortBufferException {
0N/A return super.engineUpdate(input, output);
0N/A }
0N/A }
0N/A}