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 42public class MacSpiTest extends TestCase { 43 44 /** 45 * Constructor for MacSpiTests. 46 * 47 * @param arg0 48 */ 49 public MacSpiTest(String arg0) { 50 super(arg0); 51 } 52 53 /** 54 * Test for <code>MacSpi</code> constructor 55 * Assertion: constructs MacSpi 56 */ 57 public void testMacSpiTests01() throws Exception { 58 MacSpi mSpi = new MyMacSpi(); 59 60 byte[] bb1 = { (byte) 1, (byte) 2, (byte) 3, (byte) 4, (byte) 5 }; 61 SecretKeySpec sks = new SecretKeySpec(bb1, "SHA1"); 62 63 assertEquals("Incorrect MacLength", mSpi.engineGetMacLength(), 0); 64 65 try { 66 mSpi.engineInit(null, null); 67 fail("IllegalArgumentException must be thrown"); 68 } catch (IllegalArgumentException e) { 69 } 70 71 mSpi.engineInit(sks, null); 72 73 byte[] bb = mSpi.engineDoFinal(); 74 assertEquals(bb.length, 0); 75 try { 76 mSpi.clone(); 77 fail("CloneNotSupportedException was not thrown as expected"); 78 } catch (CloneNotSupportedException e) { 79 } 80 81 MacSpi mSpi1 = new MyMacSpi1(); 82 mSpi1.clone(); 83 84 byte[] bbb = new byte[10]; 85 for (int i = 0; i < bbb.length; i++) { 86 bbb[i] = (byte) i; 87 } 88 try { 89 mSpi1.engineInit(null, null); 90 fail("IllegalArgumentException must be thrown"); 91 } catch (IllegalArgumentException e) { 92 } 93 mSpi1.engineInit(sks, null); 94 95 ByteBuffer byteBuf = ByteBuffer.allocate(10); 96 byteBuf.put(bbb); 97 byteBuf.position(5); 98 int beforeUp = byteBuf.remaining(); 99 mSpi1.engineUpdate(byteBuf); 100 bb = mSpi1.engineDoFinal(); 101 assertEquals("Incorrect result of engineDoFinal", bb.length, 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, (byte) 1 }; 114 byteBuf = ByteBuffer.allocate(5); 115 byteBuf.put(bbuf); 116 byteBuf.position(5); 117 if (!byteBuf.hasRemaining()) { 118 mSpi2.engineUpdate(byteBuf); 119 } 120 } 121} 122 123 124class MyMacSpi1 extends MyMacSpi { 125 public Object clone() throws CloneNotSupportedException { 126 return new MyMacSpi1(); 127 } 128} 129 130class MyMacSpi2 extends MacSpi { 131 protected int engineGetMacLength() { 132 return 0; 133 } 134 135 protected void engineInit(Key key, AlgorithmParameterSpec params) 136 throws InvalidKeyException, InvalidAlgorithmParameterException { 137 } 138 139 protected void engineUpdate(byte input) { 140 } 141 142 protected void engineUpdate(byte[] input, int offset, int len) { 143 } 144 145 protected byte[] engineDoFinal() { 146 return new byte[0]; 147 } 148 149 protected void engineReset() { 150 } 151} 152