Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/crypto/NullCipher/TestNPE.java
38838 views
/*1* Copyright (c) 2003, 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 493785326* @summary Make sure normal calls of NullCipher does not throw NPE.27* @author Valerie Peng28* @key randomness29*/30import java.util.Arrays;31import java.security.AlgorithmParameters;32import java.security.Key;33import java.security.SecureRandom;34import java.security.cert.Certificate;35import java.security.spec.AlgorithmParameterSpec;36import javax.crypto.Cipher;37import javax.crypto.NullCipher;38import javax.crypto.spec.SecretKeySpec;3940public class TestNPE {41private static byte[] BYTES = new byte[16];42static {43new SecureRandom().nextBytes(BYTES);44}4546public static void main(String[] args) throws Exception {47NullCipher nc = new NullCipher();48// testing init(...)49nc.init(Cipher.ENCRYPT_MODE, (Certificate) null);50nc.init(Cipher.ENCRYPT_MODE, (Certificate) null, (SecureRandom) null);51nc.init(Cipher.ENCRYPT_MODE, (Key) null);52nc.init(Cipher.ENCRYPT_MODE, (Key) null, (AlgorithmParameters) null);53nc.init(Cipher.ENCRYPT_MODE, (Key) null, (AlgorithmParameterSpec) null);54nc.init(Cipher.ENCRYPT_MODE, (Key) null, (AlgorithmParameterSpec) null,55(SecureRandom) null);56nc.init(Cipher.ENCRYPT_MODE, (Key) null, (AlgorithmParameters) null,57(SecureRandom) null);58nc.init(Cipher.ENCRYPT_MODE, (Key) null, (SecureRandom) null);59// testing getBlockSize()60if (nc.getBlockSize() != 1) {61throw new Exception("Error with getBlockSize()");62}63// testing update(...)64byte[] out = nc.update(BYTES);65if (!Arrays.equals(out, BYTES)) {66throw new Exception("Error with update(byte[])");67}68out = nc.update(BYTES, 0, BYTES.length);69if (!Arrays.equals(out, BYTES)) {70throw new Exception("Error with update(byte[], int, int)");71}72if (nc.update(BYTES, 0, BYTES.length, out) != BYTES.length) {73throw new Exception("Error with update(byte[], int, int, byte[])");74}75if (nc.update(BYTES, 0, BYTES.length, out, 0) != BYTES.length) {76throw new Exception(77"Error with update(byte[], int, int, byte[], int)");78}79// testing doFinal(...)80if (nc.doFinal() != null) {81throw new Exception("Error with doFinal()");82}83if (nc.doFinal(out, 0) != 0) {84throw new Exception("Error with doFinal(byte[], 0)");85}86out = nc.doFinal(BYTES);87if (!Arrays.equals(out, BYTES)) {88throw new Exception("Error with doFinal(byte[])");89}90out = nc.doFinal(BYTES, 0, BYTES.length);91if (!Arrays.equals(out, BYTES)) {92throw new Exception("Error with doFinal(byte[], int, int)");93}94if (nc.doFinal(BYTES, 0, BYTES.length, out) != BYTES.length) {95throw new Exception(96"Error with doFinal(byte[], int, int, byte[])");97}98if (nc.doFinal(BYTES, 0, BYTES.length, out, 0) != BYTES.length) {99throw new Exception(100"Error with doFinal(byte[], int, int, byte[], int)");101}102// testing getExemptionMechanism()103if (nc.getExemptionMechanism() != null) {104throw new Exception("Error with getExemptionMechanism()");105}106// testing getOutputSize(int)107if (nc.getOutputSize(5) != 5) {108throw new Exception("Error with getOutputSize()");109}110// testing getIV(), getParameters(), getAlgorithm(), etc.111if (nc.getIV() == null) { // should've been null;112// left it as is for backward-compatibility113throw new Exception("Error with getIV()");114}115if (nc.getParameters() != null) {116throw new Exception("Error with getParameters()");117}118if (nc.getAlgorithm() != null) {119throw new Exception("Error with getAlgorithm()");120}121if (nc.getProvider() != null) { // not associated with any provider122throw new Exception("Error with getProvider()");123}124System.out.println("Test Done");125}126}127128129