0N/A/*
2362N/A * Copyright (c) 2003, 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 4937853
0N/A * @summary Make sure normal calls of NullCipher does not throw NPE.
0N/A * @author Valerie Peng
0N/A */
0N/Aimport java.util.Arrays;
0N/Aimport java.security.AlgorithmParameters;
0N/Aimport java.security.Key;
0N/Aimport java.security.SecureRandom;
0N/Aimport java.security.cert.Certificate;
0N/Aimport java.security.spec.AlgorithmParameterSpec;
0N/Aimport javax.crypto.Cipher;
0N/Aimport javax.crypto.NullCipher;
0N/Aimport javax.crypto.spec.SecretKeySpec;
0N/A
0N/Apublic class TestNPE {
0N/A private static byte[] BYTES = new byte[16];
0N/A static {
0N/A new SecureRandom().nextBytes(BYTES);
0N/A }
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A NullCipher nc = new NullCipher();
0N/A // testing init(...)
0N/A nc.init(Cipher.ENCRYPT_MODE, (Certificate) null);
0N/A nc.init(Cipher.ENCRYPT_MODE, (Certificate) null, (SecureRandom) null);
0N/A nc.init(Cipher.ENCRYPT_MODE, (Key) null);
0N/A nc.init(Cipher.ENCRYPT_MODE, (Key) null, (AlgorithmParameters) null);
0N/A nc.init(Cipher.ENCRYPT_MODE, (Key) null, (AlgorithmParameterSpec) null);
0N/A nc.init(Cipher.ENCRYPT_MODE, (Key) null, (AlgorithmParameterSpec) null,
0N/A (SecureRandom) null);
0N/A nc.init(Cipher.ENCRYPT_MODE, (Key) null, (AlgorithmParameters) null,
0N/A (SecureRandom) null);
0N/A nc.init(Cipher.ENCRYPT_MODE, (Key) null, (SecureRandom) null);
0N/A // testing getBlockSize()
0N/A if (nc.getBlockSize() != 1) {
0N/A throw new Exception("Error with getBlockSize()");
0N/A }
0N/A // testing update(...)
0N/A byte[] out = nc.update(BYTES);
0N/A if (!Arrays.equals(out, BYTES)) {
0N/A throw new Exception("Error with update(byte[])");
0N/A }
0N/A out = nc.update(BYTES, 0, BYTES.length);
0N/A if (!Arrays.equals(out, BYTES)) {
0N/A throw new Exception("Error with update(byte[], int, int)");
0N/A }
0N/A if (nc.update(BYTES, 0, BYTES.length, out) != BYTES.length) {
0N/A throw new Exception("Error with update(byte[], int, int, byte[])");
0N/A }
0N/A if (nc.update(BYTES, 0, BYTES.length, out, 0) != BYTES.length) {
0N/A throw new Exception(
0N/A "Error with update(byte[], int, int, byte[], int)");
0N/A }
0N/A // testing doFinal(...)
0N/A if (nc.doFinal() != null) {
0N/A throw new Exception("Error with doFinal()");
0N/A }
0N/A if (nc.doFinal(out, 0) != 0) {
0N/A throw new Exception("Error with doFinal(byte[], 0)");
0N/A }
0N/A out = nc.doFinal(BYTES);
0N/A if (!Arrays.equals(out, BYTES)) {
0N/A throw new Exception("Error with doFinal(byte[])");
0N/A }
0N/A out = nc.doFinal(BYTES, 0, BYTES.length);
0N/A if (!Arrays.equals(out, BYTES)) {
0N/A throw new Exception("Error with doFinal(byte[], int, int)");
0N/A }
0N/A if (nc.doFinal(BYTES, 0, BYTES.length, out) != BYTES.length) {
0N/A throw new Exception(
0N/A "Error with doFinal(byte[], int, int, byte[])");
0N/A }
0N/A if (nc.doFinal(BYTES, 0, BYTES.length, out, 0) != BYTES.length) {
0N/A throw new Exception(
0N/A "Error with doFinal(byte[], int, int, byte[], int)");
0N/A }
0N/A // testing getExemptionMechanism()
0N/A if (nc.getExemptionMechanism() != null) {
0N/A throw new Exception("Error with getExemptionMechanism()");
0N/A }
0N/A // testing getOutputSize(int)
0N/A if (nc.getOutputSize(5) != 5) {
0N/A throw new Exception("Error with getOutputSize()");
0N/A }
0N/A // testing getIV(), getParameters(), getAlgorithm(), etc.
0N/A if (nc.getIV() == null) { // should've been null;
0N/A // left it as is for backward-compatibility
0N/A throw new Exception("Error with getIV()");
0N/A }
0N/A if (nc.getParameters() != null) {
0N/A throw new Exception("Error with getParameters()");
0N/A }
0N/A if (nc.getAlgorithm() != null) {
0N/A throw new Exception("Error with getAlgorithm()");
0N/A }
0N/A if (nc.getProvider() != null) { // not associated with any provider
0N/A throw new Exception("Error with getProvider()");
0N/A }
0N/A System.out.println("Test Done");
0N/A }
0N/A}