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.nio.ByteBuffer;
024: import java.security.InvalidAlgorithmParameterException;
025: import java.security.InvalidKeyException;
026: import java.security.Key;
027: import java.security.spec.AlgorithmParameterSpec;
028:
029: import javax.crypto.spec.SecretKeySpec;
030:
031: import org.apache.harmony.crypto.tests.support.MyMacSpi;
032:
033: import junit.framework.TestCase;
034:
035: /**
036: * Tests for <code>MacSpi</code> class constructors and methods.
037: *
038: */
039:
040: public class MacSpiTest extends TestCase {
041:
042: /**
043: * Constructor for MacSpiTests.
044: *
045: * @param arg0
046: */
047: public MacSpiTest(String arg0) {
048: super (arg0);
049: }
050:
051: /**
052: * Test for <code>MacSpi</code> constructor
053: * Assertion: constructs MacSpi
054: */
055: public void testMacSpiTests01() throws Exception {
056: MacSpi mSpi = new MyMacSpi();
057:
058: byte[] bb1 = { (byte) 1, (byte) 2, (byte) 3, (byte) 4, (byte) 5 };
059: SecretKeySpec sks = new SecretKeySpec(bb1, "SHA1");
060:
061: assertEquals("Incorrect MacLength", mSpi.engineGetMacLength(),
062: 0);
063:
064: try {
065: mSpi.engineInit(null, null);
066: fail("IllegalArgumentException must be thrown");
067: } catch (IllegalArgumentException e) {
068: }
069:
070: mSpi.engineInit(sks, null);
071:
072: byte[] bb = mSpi.engineDoFinal();
073: assertEquals(bb.length, 0);
074: try {
075: mSpi.clone();
076: fail("CloneNotSupportedException was not thrown as expected");
077: } catch (CloneNotSupportedException e) {
078: }
079:
080: MacSpi mSpi1 = new MyMacSpi1();
081: mSpi1.clone();
082:
083: byte[] bbb = new byte[10];
084: for (int i = 0; i < bbb.length; i++) {
085: bbb[i] = (byte) i;
086: }
087: try {
088: mSpi1.engineInit(null, null);
089: fail("IllegalArgumentException must be thrown");
090: } catch (IllegalArgumentException e) {
091: }
092: mSpi1.engineInit(sks, null);
093:
094: ByteBuffer byteBuf = ByteBuffer.allocate(10);
095: byteBuf.put(bbb);
096: byteBuf.position(5);
097: int beforeUp = byteBuf.remaining();
098: mSpi1.engineUpdate(byteBuf);
099: bb = mSpi1.engineDoFinal();
100: assertEquals("Incorrect result of engineDoFinal", bb.length,
101: beforeUp);
102:
103: MacSpi mSpi2 = new MyMacSpi2();
104:
105: mSpi2.engineInit(null, null);
106: mSpi2.engineInit(sks, null);
107:
108: try {
109: mSpi2.clone();
110: } catch (CloneNotSupportedException e) {
111: }
112:
113: byte[] bbuf = { (byte) 5, (byte) 4, (byte) 3, (byte) 2,
114: (byte) 1 };
115: byteBuf = ByteBuffer.allocate(5);
116: byteBuf.put(bbuf);
117: byteBuf.position(5);
118: if (!byteBuf.hasRemaining()) {
119: mSpi2.engineUpdate(byteBuf);
120: }
121: }
122: }
123:
124: class MyMacSpi1 extends MyMacSpi {
125: public Object clone() throws CloneNotSupportedException {
126: return new MyMacSpi1();
127: }
128: }
129:
130: class MyMacSpi2 extends MacSpi {
131: protected int engineGetMacLength() {
132: return 0;
133: }
134:
135: protected void engineInit(Key key, AlgorithmParameterSpec params)
136: throws InvalidKeyException,
137: InvalidAlgorithmParameterException {
138: }
139:
140: protected void engineUpdate(byte input) {
141: }
142:
143: protected void engineUpdate(byte[] input, int offset, int len) {
144: }
145:
146: protected byte[] engineDoFinal() {
147: return new byte[0];
148: }
149:
150: protected void engineReset() {
151: }
152: }
|