blob: c0a8cedc432b23d9c511cc2b074aae3debab7722 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2004-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 5000980
27 * @summary Check NullPointerException for cipherSpi.engineUpdate(x, null)
28 */
29
30import javax.crypto.CipherSpi;
31import java.security.InvalidKeyException;
32import java.security.AlgorithmParameters;
33import java.security.InvalidAlgorithmParameterException;
34import java.security.NoSuchAlgorithmException;
35import java.security.SecureRandom;
36import java.security.spec.AlgorithmParameterSpec;
37import java.security.Key;
38import java.security.Security;
39import javax.crypto.IllegalBlockSizeException;
40import javax.crypto.BadPaddingException;
41import javax.crypto.ShortBufferException;
42import javax.crypto.NoSuchPaddingException;
43import java.security.GeneralSecurityException;
44import java.nio.ByteBuffer;
45
46public class ByteBuffersNull {
47
48 static final int bufSize = 1024;
49
50 public void testCase010() throws Exception {
51 CipherSpiImpl c = new CipherSpiImpl();
52 BufferDescr[] inBuffers = getByteBuffersForTest(bufSize);
53 int failureCount = 0;
54
55 for (int i = 0; i < inBuffers.length; i++) {
56 String key = inBuffers[i].descr;
57 ByteBuffer bb = inBuffers[i].buf;
58
59 try {
60
61 c.engineUpdate(bb, null);
62
63 throw new Exception("No Exception?!");
64 } catch (NullPointerException npe) {
65 // Expected behaviour - pass
66 System.out.println("OK: " + npe);
67 }
68 }
69 }
70
71 // Creates a ByteBuffer with a desired properties
72 ByteBuffer getByteBuffer(int capacity, int position,
73 boolean limitAt0) {
74 ByteBuffer bb = ByteBuffer.allocate(capacity);
75
76 bb.position(position);
77
78 if (limitAt0)
79 bb.limit(0);
80
81 return bb;
82 }
83
84 BufferDescr[] getByteBuffersForTest(int defaultSize) {
85 int caseNum = 4;
86
87 BufferDescr[] buffers = new BufferDescr[caseNum];
88
89 // ByteBuffer with capacity 0
90 buffers[0]= new BufferDescr("ByteBuffer with capacity == 0",
91 getByteBuffer(0,0,false));
92
93 // ByteBuffer with some space but limit = 0
94 buffers[1] = new BufferDescr(
95 "ByteBuffer with some capacity but limit == 0",
96 getByteBuffer(defaultSize, 0, true));
97
98 // ByteBuffer with some remaining data (limit = capacity)
99 buffers[2] = new BufferDescr("ByteBuffer with some data",
100 getByteBuffer(defaultSize,0,false));
101
102 // ByteBuffer with some data but position is at the limit
103 buffers[3] = new BufferDescr(
104 "ByteBuffer with data but position at the limit",
105 getByteBuffer(defaultSize, defaultSize,false));
106
107 return buffers;
108 }
109
110 public static void main(String argv[]) throws Exception {
111 ByteBuffersNull test = new ByteBuffersNull();
112 test.testCase010();
113 }
114
115 class BufferDescr {
116 BufferDescr(String d, ByteBuffer b) {
117 descr = d;
118 buf = b;
119 }
120
121 public String descr;
122 public ByteBuffer buf;
123 }
124
125 public class CipherSpiImpl extends CipherSpi {
126
127 public CipherSpiImpl() {
128 super();
129 }
130
131 public void engineSetMode(String mode)
132 throws NoSuchAlgorithmException { }
133
134 public void engineSetPadding(String padding)
135 throws NoSuchPaddingException { }
136
137 public int engineGetBlockSize() {
138 return 0;
139 }
140
141 public int engineGetOutputSize(int inputLen) {
142 return 0;
143 }
144
145 public byte[] engineGetIV() {
146 return null;
147 }
148
149 public AlgorithmParameters engineGetParameters() {
150 return null;
151 }
152
153 public void engineInit(int opmode, Key key, SecureRandom random)
154 throws InvalidKeyException { }
155
156 public void engineInit(int opmode, Key key,
157 AlgorithmParameterSpec params, SecureRandom random)
158 throws InvalidKeyException, InvalidAlgorithmParameterException { }
159
160 public void engineInit(int opmode, Key key, AlgorithmParameters params,
161 SecureRandom random)
162 throws InvalidKeyException, InvalidAlgorithmParameterException { }
163
164 public byte[] engineUpdate(byte[] input, int offset, int len) {
165 return null;
166 }
167
168 public int engineUpdate(byte[] input, int inputOffset, int inputLen,
169 byte[] output, int outputOffset)
170 throws ShortBufferException {
171 return 0;
172 }
173
174 public byte[] engineDoFinal(byte[] input, int inputOffset, int inputLen)
175 throws IllegalBlockSizeException, BadPaddingException {
176 return null;
177 }
178
179 public int engineDoFinal(byte[] input, int inputOffset, int inputLen,
180 byte[] output, int outputOffset)
181 throws ShortBufferException, IllegalBlockSizeException,
182 BadPaddingException {
183 return 0;
184 }
185
186 public byte[] engineWrap(Key key)
187 throws IllegalBlockSizeException, InvalidKeyException {
188 return super.engineWrap(key);
189 }
190
191 public Key engineUnwrap(byte[] wKey, String wKeyAlgorithm,
192 int wKeyType) throws InvalidKeyException,
193 NoSuchAlgorithmException {
194 return super.engineUnwrap(wKey, wKeyAlgorithm, wKeyType);
195 }
196
197 public int engineGetKeySize(Key key) throws InvalidKeyException {
198 return super.engineGetKeySize(key);
199 }
200
201 public int engineDoFinal(ByteBuffer input, ByteBuffer output)
202 throws ShortBufferException, IllegalBlockSizeException,
203 BadPaddingException {
204 return super.engineDoFinal(input, output);
205 }
206
207 public int engineUpdate(ByteBuffer input, ByteBuffer output)
208 throws ShortBufferException {
209 return super.engineUpdate(input, output);
210 }
211 }
212}