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 Boris V. Kuznetsov
020: * @version $Revision$
021: */package org.apache.harmony.crypto.internal;
022:
023: import java.nio.ByteBuffer;
024: import java.util.Arrays;
025:
026: import javax.crypto.ShortBufferException;
027:
028: import org.apache.harmony.crypto.internal.NullCipherSpi;
029: import junit.framework.TestCase;
030:
031: /**
032: *
033: * Tests for NullCipher implementation
034: */
035: public class NullCipherSpiTest extends TestCase {
036:
037: public void testEngineGetBlockSize() {
038: NullCipherSpi spi = new NullCipherSpi();
039: assertEquals("incorrect block size", 1, spi
040: .engineGetBlockSize());
041: }
042:
043: public void testEngineGetOutputSize() {
044: NullCipherSpi spi = new NullCipherSpi();
045: assertEquals("incorrect output size", 100, spi
046: .engineGetOutputSize(100));
047: }
048:
049: public void testEngineGetIV() {
050: NullCipherSpi spi = new NullCipherSpi();
051: assertTrue("Incorrect IV", Arrays.equals(spi.engineGetIV(),
052: new byte[8]));
053: }
054:
055: /*
056: * Class under test for byte[] engineUpdate(byte[], int, int)
057: */
058: public void testEngineUpdatebyteArrayintint() {
059: NullCipherSpi spi = new NullCipherSpi();
060: byte[] b = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
061: byte[] b1 = spi.engineUpdate(b, 3, 4);
062: for (int i = 0; i < 4; i++) {
063: assertEquals("incorrect update result", b[3 + i], b1[i]);
064: }
065: }
066:
067: /*
068: * Class under test for int engineUpdate(byte[], int, int, byte[], int)
069: */
070: public void testEngineUpdatebyteArrayintintbyteArrayint()
071: throws Exception {
072: NullCipherSpi spi = new NullCipherSpi();
073: byte[] b = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
074: byte[] b1 = new byte[10];
075: assertEquals("incorrect update result", 4, spi.engineUpdate(b,
076: 3, 4, b1, 5));
077: for (int i = 0; i < 4; i++) {
078: assertEquals("incorrect update result", b[3 + i], b1[5 + i]);
079: }
080: }
081:
082: /*
083: * Class under test for byte[] engineDoFinal(byte[], int, int)
084: */
085: public void testEngineDoFinalbyteArrayintint() throws Exception {
086: NullCipherSpi spi = new NullCipherSpi();
087: byte[] b = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
088: byte[] b1 = null;
089: b1 = spi.engineDoFinal(b, 3, 4);
090: for (int i = 0; i < 4; i++) {
091: assertEquals("incorrect doFinal result", b[3 + i], b1[i]);
092: }
093: }
094:
095: /*
096: * Class under test for int engineDoFinal(byte[], int, int, byte[], int)
097: */
098: public void testEngineDoFinalbyteArrayintintbyteArrayint()
099: throws Exception {
100: NullCipherSpi spi = new NullCipherSpi();
101: byte[] b = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
102: byte[] b1 = new byte[10];
103: assertEquals("incorrect doFinal result", 4, spi.engineDoFinal(
104: b, 3, 4, b1, 5));
105: for (int i = 0; i < 4; i++) {
106: assertEquals("incorrect doFinal result", b[3 + i],
107: b1[5 + i]);
108: }
109:
110: }
111:
112: /*
113: * Class under test for int engineUpdate(ByteBuffer, ByteBuffer)
114: */
115: public void testEngineUpdateByteBufferByteBuffer() throws Exception {
116: NullCipherSpi spi = new NullCipherSpi();
117: byte[] b = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
118:
119: ByteBuffer inbuf = ByteBuffer.wrap(b, 0, b.length);
120: ByteBuffer outbuf = ByteBuffer.allocate(6);
121:
122: try {
123: spi.engineUpdate(null, outbuf);
124: fail("No expected NullPointerException");
125: } catch (NullPointerException e) {
126: }
127:
128: try {
129: spi.engineUpdate(inbuf, null);
130: fail("No expected NullPointerException");
131: } catch (NullPointerException e) {
132: }
133:
134: inbuf.get();
135: inbuf.get();
136: inbuf.get();
137: inbuf.get();
138: int result = spi.engineUpdate(inbuf, outbuf);
139: assertEquals("incorrect result", b.length - 4, result);
140: for (int i = 0; i < result; i++) {
141: assertEquals("incorrect outbuf", i + 4, outbuf.get(i));
142: }
143:
144: inbuf = ByteBuffer.wrap(b, 0, b.length);
145: outbuf = ByteBuffer.allocate(5);
146: inbuf.get();
147: inbuf.get();
148: inbuf.get();
149: inbuf.get();
150: try {
151: spi.engineUpdate(inbuf, outbuf);
152: fail("No expected ShortBufferException");
153: } catch (ShortBufferException e) {
154: }
155: }
156:
157: /*
158: * Class under test for int engineDoFinal(ByteBuffer, ByteBuffer)
159: */
160: public void testEngineDoFinalByteBufferByteBuffer()
161: throws Exception {
162: NullCipherSpi spi = new NullCipherSpi();
163: byte[] b = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
164:
165: ByteBuffer inbuf = ByteBuffer.wrap(b, 0, b.length);
166: ByteBuffer outbuf = ByteBuffer.allocate(6);
167:
168: try {
169: spi.engineDoFinal(null, outbuf);
170: fail("No expected NullPointerException");
171: } catch (NullPointerException e) {
172: }
173:
174: try {
175: spi.engineDoFinal(inbuf, null);
176: fail("No expected NullPointerException");
177: } catch (NullPointerException e) {
178: }
179:
180: inbuf.get();
181: inbuf.get();
182: inbuf.get();
183: inbuf.get();
184: int result = spi.engineDoFinal(inbuf, outbuf);
185: assertEquals("incorrect result", b.length - 4, result);
186: for (int i = 0; i < result; i++) {
187: assertEquals("incorrect outbuf", i + 4, outbuf.get(i));
188: }
189:
190: inbuf = ByteBuffer.wrap(b, 0, b.length);
191: outbuf = ByteBuffer.allocate(5);
192: inbuf.get();
193: inbuf.get();
194: inbuf.get();
195: inbuf.get();
196: try {
197: spi.engineDoFinal(inbuf, outbuf);
198: fail("No expected ShortBufferException");
199: } catch (ShortBufferException e) {
200: }
201: }
202:
203: /*
204: * Class under test for byte[] engineWrap(Key)
205: */
206: public void testEngineWrapKey() throws Exception {
207: NullCipherSpi spi = new NullCipherSpi();
208: try {
209: spi.engineWrap(null);
210: fail("No expected UnsupportedOperationException");
211: } catch (UnsupportedOperationException e) {
212: }
213: }
214:
215: /*
216: * Class under test for Key engineUnwrap(byte[], String, int)
217: */
218: public void testEngineUnwrapbyteArrayStringint() throws Exception {
219: NullCipherSpi spi = new NullCipherSpi();
220: try {
221: spi.engineUnwrap(new byte[3], "", 10);
222: fail("No expected UnsupportedOperationException");
223: } catch (UnsupportedOperationException e) {
224: }
225: }
226:
227: /*
228: * Class under test for int engineGetKeySize(Key)
229: */
230: public void testEngineGetKeySize() throws Exception {
231: NullCipherSpi spi = new NullCipherSpi();
232: try {
233: spi.engineGetKeySize(null);
234: fail("No expected UnsupportedOperationException");
235: } catch (UnsupportedOperationException e) {
236: }
237: }
238:
239: }
|