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 * @version $Revision$ 21 */ 22 23 package org.apache.harmony.crypto.tests.javax.crypto; 24 25 import java.nio.ByteBuffer; 26 import java.security.InvalidAlgorithmParameterException; 27 import java.security.InvalidKeyException; 28 import java.security.Key; 29 import java.security.spec.AlgorithmParameterSpec; 30 31 import javax.crypto.spec.SecretKeySpec; 32 import javax.crypto.MacSpi; 33 34 import org.apache.harmony.crypto.tests.support.MyMacSpi; 35 36 import junit.framework.TestCase; 37 38 39 /** 40 * Tests for <code>MacSpi</code> class constructors and methods. 41 * 42 */ 43 public class MacSpiTest extends TestCase { 44 class Mock_MacSpi extends MyMacSpi { 45 46 @Override 47 protected byte[] engineDoFinal() { 48 return super.engineDoFinal(); 49 } 50 51 @Override 52 protected int engineGetMacLength() { 53 return super.engineGetMacLength(); 54 } 55 56 @Override 57 protected void engineInit(Key key, AlgorithmParameterSpec params) throws InvalidKeyException, InvalidAlgorithmParameterException { 58 super.engineInit(key, params); 59 } 60 61 @Override 62 protected void engineReset() { 63 super.engineReset(); 64 } 65 66 @Override 67 protected void engineUpdate(byte input) { 68 super.engineUpdate(input); 69 } 70 71 @Override 72 protected void engineUpdate(byte[] input, int offset, int len) { 73 super.engineUpdate(input, offset, len); 74 } 75 76 } 77 78 class Mock_MacSpi1 extends MyMacSpi1 { 79 80 @Override 81 protected byte[] engineDoFinal() { 82 return super.engineDoFinal(); 83 } 84 85 @Override 86 protected int engineGetMacLength() { 87 return super.engineGetMacLength(); 88 } 89 90 @Override 91 protected void engineInit(Key key, AlgorithmParameterSpec params) throws InvalidKeyException, InvalidAlgorithmParameterException { 92 super.engineInit(key, params); 93 } 94 95 @Override 96 protected void engineReset() { 97 super.engineReset(); 98 } 99 100 @Override 101 protected void engineUpdate(byte input) { 102 super.engineUpdate(input); 103 } 104 105 @Override 106 protected void engineUpdate(byte[] input, int offset, int len) { 107 super.engineUpdate(input, offset, len); 108 } 109 110 protected void engineUpdate(ByteBuffer input) { 111 super.engineUpdate(input); 112 } 113 114 } 115 116 117 class Mock_MacSpi2 extends MyMacSpi2 { 118 119 @Override 120 protected byte[] engineDoFinal() { 121 return super.engineDoFinal(); 122 } 123 124 @Override 125 protected int engineGetMacLength() { 126 return super.engineGetMacLength(); 127 } 128 129 @Override 130 protected void engineInit(Key key, AlgorithmParameterSpec params) throws InvalidKeyException, InvalidAlgorithmParameterException { 131 super.engineInit(key, params); 132 } 133 134 @Override 135 protected void engineReset() { 136 super.engineReset(); 137 } 138 139 @Override 140 protected void engineUpdate(byte input) { 141 super.engineUpdate(input); 142 } 143 144 @Override 145 protected void engineUpdate(byte[] input, int offset, int len) { 146 super.engineUpdate(input, offset, len); 147 } 148 149 protected void engineUpdate(ByteBuffer input) { 150 super.engineUpdate(input); 151 } 152 153 } 154 155 156 /** 157 * Test for <code>MacSpi</code> constructor 158 * Assertion: constructs MacSpi 159 */ 160 public void testMacSpiTests01() throws Exception { 161 Mock_MacSpi mSpi = new Mock_MacSpi(); 162 163 byte [] bb1 = {(byte)1, (byte)2, (byte)3, (byte)4, (byte)5}; 164 SecretKeySpec sks = new SecretKeySpec(bb1, "SHA1"); 165 166 assertEquals("Incorrect MacLength", mSpi.engineGetMacLength(), 0); 167 168 try { 169 mSpi.engineInit(null, null); 170 fail("IllegalArgumentException must be thrown"); 171 } catch (IllegalArgumentException e) { 172 } 173 174 mSpi.engineInit(sks, null); 175 176 byte[] bb = mSpi.engineDoFinal(); 177 assertEquals(bb.length, 0); 178 try { 179 mSpi.clone(); 180 fail("CloneNotSupportedException was not thrown as expected"); 181 } catch (CloneNotSupportedException e) { 182 } 183 184 Mock_MacSpi1 mSpi1 = new Mock_MacSpi1(); 185 mSpi1.clone(); 186 187 byte [] bbb = new byte[10]; 188 for (int i = 0; i < bbb.length; i++) { 189 bbb[i] = (byte)i; 190 } 191 try { 192 mSpi1.engineInit(null, null); 193 fail("IllegalArgumentException must be thrown"); 194 } catch (IllegalArgumentException e) { 195 } 196 mSpi1.engineInit(sks, null); 197 198 ByteBuffer byteBuf = ByteBuffer.allocate(10); 199 byteBuf.put(bbb); 200 byteBuf.position(5); 201 int beforeUp = byteBuf.remaining(); 202 mSpi1.engineUpdate(byteBuf); 203 bb = mSpi1.engineDoFinal(); 204 assertEquals("Incorrect result of engineDoFinal", bb.length, beforeUp); 205 206 Mock_MacSpi2 mSpi2 = new Mock_MacSpi2(); 207 208 mSpi2.engineInit(null, null); 209 mSpi2.engineInit(sks, null); 210 211 try { 212 mSpi2.clone(); 213 } catch (CloneNotSupportedException e) { 214 } 215 216 byte [] bbuf = {(byte)5, (byte)4, (byte)3, (byte)2, (byte)1}; 217 byteBuf = ByteBuffer.allocate(5); 218 byteBuf.put(bbuf); 219 byteBuf.position(5); 220 if (!byteBuf.hasRemaining()) { 221 mSpi2.engineUpdate(byteBuf); 222 } 223 } 224 } 225 226 227 class MyMacSpi1 extends MyMacSpi { 228 public Object clone() throws CloneNotSupportedException { 229 return new MyMacSpi1(); 230 } 231 } 232 233 class MyMacSpi2 extends MacSpi { 234 protected int engineGetMacLength() { 235 return 0; 236 } 237 238 protected void engineInit(Key key, AlgorithmParameterSpec params) 239 throws InvalidKeyException, InvalidAlgorithmParameterException { 240 } 241 242 protected void engineUpdate(byte input) { 243 } 244 245 protected void engineUpdate(byte[] input, int offset, int len) { 246 } 247 248 protected byte[] engineDoFinal() { 249 return new byte[0]; 250 } 251 252 protected void engineReset() { 253 } 254 } 255