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