blob: b69d10bb2a62bc2a5d3aaaca44ef297a8be524ce [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2003-2007 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any 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 */
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;
39
40public class TestNPE {
41 private static byte[] BYTES = new byte[16];
42 static {
43 new SecureRandom().nextBytes(BYTES);
44 }
45
46 public static void main(String[] args) throws Exception {
47 NullCipher nc = new NullCipher();
48 // testing init(...)
49 nc.init(Cipher.ENCRYPT_MODE, (Certificate) null);
50 nc.init(Cipher.ENCRYPT_MODE, (Certificate) null, (SecureRandom) null);
51 nc.init(Cipher.ENCRYPT_MODE, (Key) null);
52 nc.init(Cipher.ENCRYPT_MODE, (Key) null, (AlgorithmParameters) null);
53 nc.init(Cipher.ENCRYPT_MODE, (Key) null, (AlgorithmParameterSpec) null);
54 nc.init(Cipher.ENCRYPT_MODE, (Key) null, (AlgorithmParameterSpec) null,
55 (SecureRandom) null);
56 nc.init(Cipher.ENCRYPT_MODE, (Key) null, (AlgorithmParameters) null,
57 (SecureRandom) null);
58 nc.init(Cipher.ENCRYPT_MODE, (Key) null, (SecureRandom) null);
59 // testing getBlockSize()
60 if (nc.getBlockSize() != 1) {
61 throw new Exception("Error with getBlockSize()");
62 }
63 // testing update(...)
64 byte[] out = nc.update(BYTES);
65 if (!Arrays.equals(out, BYTES)) {
66 throw new Exception("Error with update(byte[])");
67 }
68 out = nc.update(BYTES, 0, BYTES.length);
69 if (!Arrays.equals(out, BYTES)) {
70 throw new Exception("Error with update(byte[], int, int)");
71 }
72 if (nc.update(BYTES, 0, BYTES.length, out) != BYTES.length) {
73 throw new Exception("Error with update(byte[], int, int, byte[])");
74 }
75 if (nc.update(BYTES, 0, BYTES.length, out, 0) != BYTES.length) {
76 throw new Exception(
77 "Error with update(byte[], int, int, byte[], int)");
78 }
79 // testing doFinal(...)
80 if (nc.doFinal() != null) {
81 throw new Exception("Error with doFinal()");
82 }
83 if (nc.doFinal(out, 0) != 0) {
84 throw new Exception("Error with doFinal(byte[], 0)");
85 }
86 out = nc.doFinal(BYTES);
87 if (!Arrays.equals(out, BYTES)) {
88 throw new Exception("Error with doFinal(byte[])");
89 }
90 out = nc.doFinal(BYTES, 0, BYTES.length);
91 if (!Arrays.equals(out, BYTES)) {
92 throw new Exception("Error with doFinal(byte[], int, int)");
93 }
94 if (nc.doFinal(BYTES, 0, BYTES.length, out) != BYTES.length) {
95 throw new Exception(
96 "Error with doFinal(byte[], int, int, byte[])");
97 }
98 if (nc.doFinal(BYTES, 0, BYTES.length, out, 0) != BYTES.length) {
99 throw new Exception(
100 "Error with doFinal(byte[], int, int, byte[], int)");
101 }
102 // testing getExemptionMechanism()
103 if (nc.getExemptionMechanism() != null) {
104 throw new Exception("Error with getExemptionMechanism()");
105 }
106 // testing getOutputSize(int)
107 if (nc.getOutputSize(5) != 5) {
108 throw new Exception("Error with getOutputSize()");
109 }
110 // testing getIV(), getParameters(), getAlgorithm(), etc.
111 if (nc.getIV() == null) { // should've been null;
112 // left it as is for backward-compatibility
113 throw new Exception("Error with getIV()");
114 }
115 if (nc.getParameters() != null) {
116 throw new Exception("Error with getParameters()");
117 }
118 if (nc.getAlgorithm() != null) {
119 throw new Exception("Error with getAlgorithm()");
120 }
121 if (nc.getProvider() != null) { // not associated with any provider
122 throw new Exception("Error with getProvider()");
123 }
124 System.out.println("Test Done");
125 }
126}