1 /*
   2  * Copyright (c) 2003, 2007, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 4937853
  27  * @summary Make sure normal calls of NullCipher does not throw NPE.
  28  * @author Valerie Peng
  29  * @key randomness
  30  */
  31 import java.util.Arrays;
  32 import java.security.AlgorithmParameters;
  33 import java.security.Key;
  34 import java.security.SecureRandom;
  35 import java.security.cert.Certificate;
  36 import java.security.spec.AlgorithmParameterSpec;
  37 import javax.crypto.Cipher;
  38 import javax.crypto.NullCipher;
  39 import javax.crypto.spec.SecretKeySpec;
  40 
  41 public class TestNPE {
  42     private static byte[] BYTES = new byte[16];
  43     static {
  44         new SecureRandom().nextBytes(BYTES);
  45     }
  46 
  47     public static void main(String[] args) throws Exception {
  48         NullCipher nc = new NullCipher();
  49         // testing init(...)
  50         nc.init(Cipher.ENCRYPT_MODE, (Certificate) null);
  51         nc.init(Cipher.ENCRYPT_MODE, (Certificate) null, (SecureRandom) null);
  52         nc.init(Cipher.ENCRYPT_MODE, (Key) null);
  53         nc.init(Cipher.ENCRYPT_MODE, (Key) null, (AlgorithmParameters) null);
  54         nc.init(Cipher.ENCRYPT_MODE, (Key) null, (AlgorithmParameterSpec) null);
  55         nc.init(Cipher.ENCRYPT_MODE, (Key) null, (AlgorithmParameterSpec) null,
  56             (SecureRandom) null);
  57         nc.init(Cipher.ENCRYPT_MODE, (Key) null, (AlgorithmParameters) null,
  58             (SecureRandom) null);
  59         nc.init(Cipher.ENCRYPT_MODE, (Key) null, (SecureRandom) null);
  60         // testing getBlockSize()
  61         if (nc.getBlockSize() != 1) {
  62             throw new Exception("Error with getBlockSize()");
  63         }
  64         // testing update(...)
  65         byte[] out = nc.update(BYTES);
  66         if (!Arrays.equals(out, BYTES)) {
  67             throw new Exception("Error with update(byte[])");
  68         }
  69         out = nc.update(BYTES, 0, BYTES.length);
  70         if (!Arrays.equals(out, BYTES)) {
  71             throw new Exception("Error with update(byte[], int, int)");
  72         }
  73         if (nc.update(BYTES, 0, BYTES.length, out) != BYTES.length) {
  74             throw new Exception("Error with update(byte[], int, int, byte[])");
  75         }
  76         if (nc.update(BYTES, 0, BYTES.length, out, 0) != BYTES.length) {
  77             throw new Exception(
  78                 "Error with update(byte[], int, int, byte[], int)");
  79         }
  80         // testing doFinal(...)
  81         if (nc.doFinal() != null) {
  82             throw new Exception("Error with doFinal()");
  83         }
  84         if (nc.doFinal(out, 0) != 0) {
  85              throw new Exception("Error with doFinal(byte[], 0)");
  86         }
  87         out = nc.doFinal(BYTES);
  88         if (!Arrays.equals(out, BYTES)) {
  89             throw new Exception("Error with doFinal(byte[])");
  90         }
  91         out = nc.doFinal(BYTES, 0, BYTES.length);
  92         if (!Arrays.equals(out, BYTES)) {
  93             throw new Exception("Error with doFinal(byte[], int, int)");
  94         }
  95         if (nc.doFinal(BYTES, 0, BYTES.length, out) != BYTES.length) {
  96             throw new Exception(
  97                 "Error with doFinal(byte[], int, int, byte[])");
  98         }
  99         if (nc.doFinal(BYTES, 0, BYTES.length, out, 0) != BYTES.length) {
 100             throw new Exception(
 101                 "Error with doFinal(byte[], int, int, byte[], int)");
 102         }
 103         // testing getExemptionMechanism()
 104         if (nc.getExemptionMechanism() != null) {
 105             throw new Exception("Error with getExemptionMechanism()");
 106         }
 107         // testing getOutputSize(int)
 108         if (nc.getOutputSize(5) != 5) {
 109             throw new Exception("Error with getOutputSize()");
 110         }
 111         // testing getIV(), getParameters(), getAlgorithm(), etc.
 112         if (nc.getIV() == null) { // should've been null;
 113                                   // left it as is for backward-compatibility
 114             throw new Exception("Error with getIV()");
 115         }
 116         if (nc.getParameters() != null) {
 117             throw new Exception("Error with getParameters()");
 118         }
 119         if (nc.getAlgorithm() != null) {
 120             throw new Exception("Error with getAlgorithm()");
 121         }
 122         if (nc.getProvider() != null) { // not associated with any provider
 123             throw new Exception("Error with getProvider()");
 124         }
 125         System.out.println("Test Done");
 126     }
 127 }