Path: blob/master/test/jdk/javax/crypto/Cipher/ByteBuffersNull.java
41149 views
/*1* Copyright (c) 2004, 2007, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 500098026* @summary Check NullPointerException for cipherSpi.engineUpdate(x, null)27*/2829import javax.crypto.CipherSpi;30import java.security.InvalidKeyException;31import java.security.AlgorithmParameters;32import java.security.InvalidAlgorithmParameterException;33import java.security.NoSuchAlgorithmException;34import java.security.SecureRandom;35import java.security.spec.AlgorithmParameterSpec;36import java.security.Key;37import java.security.Security;38import javax.crypto.IllegalBlockSizeException;39import javax.crypto.BadPaddingException;40import javax.crypto.ShortBufferException;41import javax.crypto.NoSuchPaddingException;42import java.security.GeneralSecurityException;43import java.nio.ByteBuffer;4445public class ByteBuffersNull {4647static final int bufSize = 1024;4849public void testCase010() throws Exception {50CipherSpiImpl c = new CipherSpiImpl();51BufferDescr[] inBuffers = getByteBuffersForTest(bufSize);52int failureCount = 0;5354for (int i = 0; i < inBuffers.length; i++) {55String key = inBuffers[i].descr;56ByteBuffer bb = inBuffers[i].buf;5758try {5960c.engineUpdate(bb, null);6162throw new Exception("No Exception?!");63} catch (NullPointerException npe) {64// Expected behaviour - pass65System.out.println("OK: " + npe);66}67}68}6970// Creates a ByteBuffer with a desired properties71ByteBuffer getByteBuffer(int capacity, int position,72boolean limitAt0) {73ByteBuffer bb = ByteBuffer.allocate(capacity);7475bb.position(position);7677if (limitAt0)78bb.limit(0);7980return bb;81}8283BufferDescr[] getByteBuffersForTest(int defaultSize) {84int caseNum = 4;8586BufferDescr[] buffers = new BufferDescr[caseNum];8788// ByteBuffer with capacity 089buffers[0]= new BufferDescr("ByteBuffer with capacity == 0",90getByteBuffer(0,0,false));9192// ByteBuffer with some space but limit = 093buffers[1] = new BufferDescr(94"ByteBuffer with some capacity but limit == 0",95getByteBuffer(defaultSize, 0, true));9697// ByteBuffer with some remaining data (limit = capacity)98buffers[2] = new BufferDescr("ByteBuffer with some data",99getByteBuffer(defaultSize,0,false));100101// ByteBuffer with some data but position is at the limit102buffers[3] = new BufferDescr(103"ByteBuffer with data but position at the limit",104getByteBuffer(defaultSize, defaultSize,false));105106return buffers;107}108109public static void main(String argv[]) throws Exception {110ByteBuffersNull test = new ByteBuffersNull();111test.testCase010();112}113114class BufferDescr {115BufferDescr(String d, ByteBuffer b) {116descr = d;117buf = b;118}119120public String descr;121public ByteBuffer buf;122}123124public class CipherSpiImpl extends CipherSpi {125126public CipherSpiImpl() {127super();128}129130public void engineSetMode(String mode)131throws NoSuchAlgorithmException { }132133public void engineSetPadding(String padding)134throws NoSuchPaddingException { }135136public int engineGetBlockSize() {137return 0;138}139140public int engineGetOutputSize(int inputLen) {141return 0;142}143144public byte[] engineGetIV() {145return null;146}147148public AlgorithmParameters engineGetParameters() {149return null;150}151152public void engineInit(int opmode, Key key, SecureRandom random)153throws InvalidKeyException { }154155public void engineInit(int opmode, Key key,156AlgorithmParameterSpec params, SecureRandom random)157throws InvalidKeyException, InvalidAlgorithmParameterException { }158159public void engineInit(int opmode, Key key, AlgorithmParameters params,160SecureRandom random)161throws InvalidKeyException, InvalidAlgorithmParameterException { }162163public byte[] engineUpdate(byte[] input, int offset, int len) {164return null;165}166167public int engineUpdate(byte[] input, int inputOffset, int inputLen,168byte[] output, int outputOffset)169throws ShortBufferException {170return 0;171}172173public byte[] engineDoFinal(byte[] input, int inputOffset, int inputLen)174throws IllegalBlockSizeException, BadPaddingException {175return null;176}177178public int engineDoFinal(byte[] input, int inputOffset, int inputLen,179byte[] output, int outputOffset)180throws ShortBufferException, IllegalBlockSizeException,181BadPaddingException {182return 0;183}184185public byte[] engineWrap(Key key)186throws IllegalBlockSizeException, InvalidKeyException {187return super.engineWrap(key);188}189190public Key engineUnwrap(byte[] wKey, String wKeyAlgorithm,191int wKeyType) throws InvalidKeyException,192NoSuchAlgorithmException {193return super.engineUnwrap(wKey, wKeyAlgorithm, wKeyType);194}195196public int engineGetKeySize(Key key) throws InvalidKeyException {197return super.engineGetKeySize(key);198}199200public int engineDoFinal(ByteBuffer input, ByteBuffer output)201throws ShortBufferException, IllegalBlockSizeException,202BadPaddingException {203return super.engineDoFinal(input, output);204}205206public int engineUpdate(ByteBuffer input, ByteBuffer output)207throws ShortBufferException {208return super.engineUpdate(input, output);209}210}211}212213214