1/* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18/** 19* @author Vera Y. Petrashkova 20*/ 21 22package javax.crypto; 23 24 25import java.nio.ByteBuffer; 26import java.security.InvalidAlgorithmParameterException; 27import java.security.InvalidKeyException; 28import java.security.Key; 29import java.security.spec.AlgorithmParameterSpec; 30 31import javax.crypto.spec.SecretKeySpec; 32 33import org.apache.harmony.crypto.tests.support.MyMacSpi; 34 35import junit.framework.TestCase; 36 37 38/** 39 * Tests for <code>MacSpi</code> class constructors and methods. 40 * 41 */ 42 43public class MacSpiTest extends TestCase { 44 45 /** 46 * Constructor for MacSpiTests. 47 * 48 * @param arg0 49 */ 50 public MacSpiTest(String arg0) { 51 super(arg0); 52 } 53 54 /** 55 * Test for <code>MacSpi</code> constructor 56 * Assertion: constructs MacSpi 57 */ 58 public void testMacSpiTests01() throws Exception { 59 MacSpi mSpi = new MyMacSpi(); 60 61 byte [] bb1 = {(byte)1, (byte)2, (byte)3, (byte)4, (byte)5}; 62 SecretKeySpec sks = new SecretKeySpec(bb1, "SHA1"); 63 64 assertEquals("Incorrect MacLength", mSpi.engineGetMacLength(), 0); 65 66 try { 67 mSpi.engineInit(null, null); 68 fail("IllegalArgumentException must be thrown"); 69 } catch (IllegalArgumentException e) { 70 } 71 72 mSpi.engineInit(sks, null); 73 74 byte[] bb = mSpi.engineDoFinal(); 75 assertEquals(bb.length, 0); 76 try { 77 mSpi.clone(); 78 fail("CloneNotSupportedException was not thrown as expected"); 79 } catch (CloneNotSupportedException e) { 80 } 81 82 MacSpi mSpi1 = new MyMacSpi1(); 83 mSpi1.clone(); 84 85 byte [] bbb = new byte[10]; 86 for (int i = 0; i < bbb.length; i++) { 87 bbb[i] = (byte)i; 88 } 89 try { 90 mSpi1.engineInit(null, null); 91 fail("IllegalArgumentException must be thrown"); 92 } catch (IllegalArgumentException e) { 93 } 94 mSpi1.engineInit(sks, null); 95 96 ByteBuffer byteBuf = ByteBuffer.allocate(10); 97 byteBuf.put(bbb); 98 byteBuf.position(5); 99 int beforeUp = byteBuf.remaining(); 100 mSpi1.engineUpdate(byteBuf); 101 bb = mSpi1.engineDoFinal(); 102 assertEquals("Incorrect result of engineDoFinal", bb.length, beforeUp); 103 104 MacSpi mSpi2 = new MyMacSpi2(); 105 106 mSpi2.engineInit(null, null); 107 mSpi2.engineInit(sks, null); 108 109 try { 110 mSpi2.clone(); 111 } catch (CloneNotSupportedException e) { 112 } 113 114 byte [] bbuf = {(byte)5, (byte)4, (byte)3, (byte)2, (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 125class MyMacSpi1 extends MyMacSpi { 126 public Object clone() throws CloneNotSupportedException { 127 return new MyMacSpi1(); 128 } 129} 130 131class MyMacSpi2 extends MacSpi { 132 protected int engineGetMacLength() { 133 return 0; 134 } 135 136 protected void engineInit(Key key, AlgorithmParameterSpec params) 137 throws InvalidKeyException, 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} 153