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