001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /**
019: * @author Vera Y. Petrashkova
020: * @version $Revision$
021: */package javax.crypto;
022:
023: import java.security.spec.AlgorithmParameterSpec;
024: import java.security.InvalidAlgorithmParameterException;
025: import java.security.InvalidKeyException;
026: import java.security.Key;
027: import java.security.NoSuchAlgorithmException;
028: import java.security.SecureRandom;
029: import java.security.AlgorithmParameters;
030: import java.nio.ByteBuffer;
031:
032: import junit.framework.TestCase;
033:
034: /**
035: * Tests for <code>CipherSpi</code> class constructors and methods.
036: *
037: */
038:
039: public class CipherSpiTest extends TestCase {
040:
041: /**
042: * Constructor for CipherSpiTests.
043: *
044: * @param arg0
045: */
046: public CipherSpiTest(String arg0) {
047: super (arg0);
048: }
049:
050: /**
051: * Test for <code>CipherSpi</code> constructor
052: * Assertion: constructs CipherSpi
053: */
054: public void testCipherSpiTests01()
055: throws IllegalBlockSizeException, BadPaddingException,
056: ShortBufferException {
057:
058: CipherSpi cSpi = new myCipherSpi();
059: assertEquals("BlockSize is not 0", cSpi.engineGetBlockSize(), 0);
060: assertEquals("OutputSize is not 0",
061: cSpi.engineGetOutputSize(1), 0);
062: byte[] bb = cSpi.engineGetIV();
063: assertEquals("Length of result byte array is not 0", bb.length,
064: 0);
065: assertNull("Not null result", cSpi.engineGetParameters());
066: byte[] bb1 = new byte[10];
067: byte[] bb2 = new byte[10];
068: bb = cSpi.engineUpdate(bb1, 1, 2);
069: assertEquals(
070: "Incorrect result of engineUpdate(byte, int, int)",
071: bb.length, 2);
072: bb = cSpi.engineDoFinal(bb1, 1, 2);
073: assertEquals(
074: "Incorrect result of engineDoFinal(byte, int, int)", 2,
075: bb.length);
076: assertEquals(
077: "Incorrect result of engineUpdate(byte, int, int, byte, int)",
078: cSpi.engineUpdate(bb1, 1, 2, bb2, 7), 2);
079: assertEquals(
080: "Incorrect result of engineDoFinal(byte, int, int, byte, int)",
081: 2, cSpi.engineDoFinal(bb1, 1, 2, bb2, 0));
082: }
083:
084: /**
085: * Test for <code>engineGetKeySize(Key)</code> method
086: * Assertion: It throws UnsupportedOperationException if it is not overridden
087: */
088: public void testCipherSpi02() throws Exception {
089: CipherSpi cSpi = new myCipherSpi();
090: try {
091: cSpi.engineGetKeySize(null);
092: fail("UnsupportedOperationException must be thrown");
093: } catch (UnsupportedOperationException e) {
094: }
095: }
096:
097: /**
098: * Test for <code>engineWrap(Key)</code> method
099: * Assertion: It throws UnsupportedOperationException if it is not overridden
100: */
101: public void testCipherSpi03() throws Exception {
102: CipherSpi cSpi = new myCipherSpi();
103: try {
104: cSpi.engineWrap(null);
105: fail("UnsupportedOperationException must be thrown");
106: } catch (UnsupportedOperationException e) {
107: }
108: }
109:
110: /**
111: * Test for <code>engineUnwrap(byte[], String, int)</code> method
112: * Assertion: It throws UnsupportedOperationException if it is not overridden
113: */
114: public void testCipherSpi04() throws Exception {
115: CipherSpi cSpi = new myCipherSpi();
116: try {
117: cSpi.engineUnwrap(new byte[0], "", 0);
118: fail("UnsupportedOperationException must be thrown");
119: } catch (UnsupportedOperationException e) {
120: }
121: }
122:
123: /**
124: * Test for <code>engineUpdate(ByteBuffer, ByteBuffer)</code> method
125: * Assertions:
126: * throws NullPointerException if one of these buffers is null;
127: * throws ShortBufferException is there is no space in output to hold result
128: */
129: public void testCipherSpi05() throws ShortBufferException {
130: CipherSpi cSpi = new myCipherSpi();
131: byte[] bb = { (byte) 0, (byte) 1, (byte) 2, (byte) 3, (byte) 4,
132: (byte) 5, (byte) 6, (byte) 7, (byte) 8, (byte) 9,
133: (byte) 10 };
134: int pos = 5;
135: int len = bb.length;
136: ByteBuffer bbNull = null;
137: ByteBuffer bb1 = ByteBuffer.allocate(len);
138: bb1.put(bb);
139: bb1.position(0);
140: try {
141: cSpi.engineUpdate(bbNull, bb1);
142: fail("NullPointerException must be thrown");
143: } catch (NullPointerException e) {
144: }
145: try {
146: cSpi.engineUpdate(bb1, bbNull);
147: fail("NullPointerException must be thrown");
148: } catch (NullPointerException e) {
149: }
150: ByteBuffer bb2 = ByteBuffer.allocate(bb.length);
151: bb1.position(len);
152: assertEquals("Incorrect number of stored bytes", 0, cSpi
153: .engineUpdate(bb1, bb2));
154:
155: bb1.position(0);
156: bb2.position(len - 2);
157: try {
158: cSpi.engineUpdate(bb1, bb2);
159: fail("ShortBufferException bust be thrown. Output buffer remaining: "
160: .concat(Integer.toString(bb2.remaining())));
161: } catch (ShortBufferException e) {
162: }
163: bb1.position(10);
164: bb2.position(0);
165: assertTrue("Incorrect number of stored bytes", cSpi
166: .engineUpdate(bb1, bb2) > 0);
167: bb1.position(bb.length);
168: cSpi.engineUpdate(bb1, bb2);
169:
170: bb1.position(pos);
171: bb2.position(0);
172: int res = cSpi.engineUpdate(bb1, bb2);
173: assertTrue("Incorrect result", res > 0);
174: }
175:
176: /**
177: * Test for <code>engineDoFinal(ByteBuffer, ByteBuffer)</code> method
178: * Assertions:
179: * throws NullPointerException if one of these buffers is null;
180: * throws ShortBufferException is there is no space in output to hold result
181: */
182: public void testCipherSpi06() throws BadPaddingException,
183: ShortBufferException, IllegalBlockSizeException {
184: CipherSpi cSpi = new myCipherSpi();
185: int len = 10;
186: byte[] bbuf = new byte[len];
187: for (int i = 0; i < bbuf.length; i++) {
188: bbuf[i] = (byte) i;
189: }
190: ByteBuffer bb1 = ByteBuffer.wrap(bbuf);
191: ByteBuffer bbNull = null;
192: try {
193: cSpi.engineDoFinal(bbNull, bb1);
194: fail("NullPointerException must be thrown");
195: } catch (NullPointerException e) {
196: }
197: try {
198: cSpi.engineDoFinal(bb1, bbNull);
199: fail("NullPointerException must be thrown");
200: } catch (NullPointerException e) {
201: }
202: ByteBuffer bb2 = ByteBuffer.allocate(len);
203: bb1.position(bb1.limit());
204: assertEquals("Incorrect result", 0, cSpi
205: .engineDoFinal(bb1, bb2));
206:
207: bb1.position(0);
208: bb2.position(len - 2);
209: try {
210: cSpi.engineDoFinal(bb1, bb2);
211: fail("ShortBufferException must be thrown. Output buffer remaining: "
212: .concat(Integer.toString(bb2.remaining())));
213: } catch (ShortBufferException e) {
214: }
215: int pos = 5;
216: bb1.position(pos);
217: bb2.position(0);
218: assertTrue("Incorrect result", cSpi.engineDoFinal(bb1, bb2) > 0);
219: }
220: }
221:
222: /**
223: *
224: * Additional class for CipherGeneratorSpi constructor verification
225: */
226:
227: class myCipherSpi extends CipherSpi {
228: private byte[] initV;
229:
230: private static byte[] resV = { (byte) 7, (byte) 6, (byte) 5,
231: (byte) 4, (byte) 3, (byte) 2, (byte) 1, (byte) 0 };
232:
233: public myCipherSpi() {
234: this .initV = new byte[0];
235: }
236:
237: protected void engineSetMode(String mode)
238: throws NoSuchAlgorithmException {
239: }
240:
241: protected void engineSetPadding(String padding)
242: throws NoSuchPaddingException {
243: }
244:
245: protected int engineGetBlockSize() {
246: return 0;
247: }
248:
249: protected int engineGetOutputSize(int inputLen) {
250: return 0;
251: }
252:
253: protected byte[] engineGetIV() {
254: return new byte[0];
255: }
256:
257: protected AlgorithmParameters engineGetParameters() {
258: return null;
259: }
260:
261: protected void engineInit(int opmode, Key key, SecureRandom random)
262: throws InvalidKeyException {
263: }
264:
265: protected void engineInit(int opmode, Key key,
266: AlgorithmParameterSpec params, SecureRandom random)
267: throws InvalidKeyException,
268: InvalidAlgorithmParameterException {
269: }
270:
271: protected void engineInit(int opmode, Key key,
272: AlgorithmParameters params, SecureRandom random)
273: throws InvalidKeyException,
274: InvalidAlgorithmParameterException {
275: }
276:
277: protected byte[] engineUpdate(byte[] input, int inputOffset,
278: int inputLen) {
279: if (initV.length < inputLen) {
280: initV = new byte[inputLen];
281: }
282: for (int i = 0; i < inputLen; i++) {
283: initV[i] = input[inputOffset + i];
284: }
285: return initV;
286: }
287:
288: protected int engineUpdate(byte[] input, int inputOffset,
289: int inputLen, byte[] output, int outputOffset)
290: throws ShortBufferException {
291: byte[] res = engineUpdate(input, inputOffset, inputLen);
292: int t = res.length;
293: if ((output.length - outputOffset) < t) {
294: throw new ShortBufferException("Update");
295: }
296: for (int i = 0; i < t; i++) {
297: output[i + outputOffset] = initV[i];
298: }
299: return t;
300: }
301:
302: protected byte[] engineDoFinal(byte[] input, int inputOffset,
303: int inputLen) throws IllegalBlockSizeException,
304: BadPaddingException {
305: if (resV.length > inputLen) {
306: byte[] bb = new byte[inputLen];
307: for (int i = 0; i < inputLen; i++) {
308: bb[i] = resV[i];
309: }
310: return bb;
311: }
312: return resV;
313: }
314:
315: protected int engineDoFinal(byte[] input, int inputOffset,
316: int inputLen, byte[] output, int outputOffset)
317: throws ShortBufferException, IllegalBlockSizeException,
318: BadPaddingException {
319: byte[] res = engineDoFinal(input, inputOffset, inputLen);
320:
321: int t = res.length;
322: if ((output.length - outputOffset) < t) {
323: throw new ShortBufferException("DoFinal");
324: }
325: for (int i = 0; i < t; i++) {
326: output[i + outputOffset] = res[i];
327: }
328: return t;
329: }
330:
331: protected int engineUpdate(ByteBuffer input, ByteBuffer output)
332: throws ShortBufferException {
333: return super .engineUpdate(input, output);
334: }
335:
336: protected int engineDoFinal(ByteBuffer input, ByteBuffer output)
337: throws ShortBufferException, IllegalBlockSizeException,
338: BadPaddingException {
339: return super.engineDoFinal(input, output);
340: }
341: }
|