MacTest.java revision 0d4ee1f9b8c37fb33cd74da4efac5905ba138e45
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 23package org.apache.harmony.crypto.tests.javax.crypto; 24 25import java.math.BigInteger; 26import java.nio.ByteBuffer; 27import java.security.InvalidAlgorithmParameterException; 28import java.security.InvalidKeyException; 29import java.security.NoSuchAlgorithmException; 30import java.security.NoSuchProviderException; 31import java.security.Provider; 32import java.security.Security; 33import java.security.spec.DSAParameterSpec; 34import java.security.spec.PSSParameterSpec; 35import java.util.Arrays; 36 37import javax.crypto.Mac; 38import javax.crypto.MacSpi; 39import javax.crypto.SecretKey; 40import javax.crypto.ShortBufferException; 41import javax.crypto.spec.DHGenParameterSpec; 42 43import javax.crypto.spec.SecretKeySpec; 44 45import org.apache.harmony.crypto.tests.support.MyMacSpi; 46import org.apache.harmony.security.tests.support.SpiEngUtils; 47 48import junit.framework.TestCase; 49 50import junit.framework.Test; 51import junit.framework.TestSuite; 52 53/** 54 * Tests for Mac class constructors and methods 55 * 56 */ 57public class MacTest extends TestCase { 58 59 public static final String srvMac = "Mac"; 60 61 private static String defaultAlgorithm = null; 62 63 private static String defaultProviderName = null; 64 65 private static Provider defaultProvider = null; 66 67 private static boolean DEFSupported = false; 68 69 private static final String NotSupportedMsg = "There is no suitable provider for Mac"; 70 71 private static final String[] invalidValues = SpiEngUtils.invalidValues; 72 73 private static String[] validValues = new String[3]; 74 75 public static final String validAlgorithmsMac [] = 76 {"HmacSHA1", "HmacMD5", "HmacSHA256", "HmacSHA384", "HmacSHA512"}; 77 78 79 static { 80 for (int i = 0; i < validAlgorithmsMac.length; i++) { 81 defaultProvider = SpiEngUtils.isSupport(validAlgorithmsMac[i], 82 srvMac); 83 DEFSupported = (defaultProvider != null); 84 if (DEFSupported) { 85 defaultAlgorithm = validAlgorithmsMac[i]; 86 defaultProviderName = defaultProvider.getName(); 87 validValues[0] = defaultAlgorithm; 88 validValues[1] = defaultAlgorithm.toUpperCase(); 89 validValues[2] = defaultAlgorithm.toLowerCase(); 90 break; 91 } 92 } 93 } 94 95 private Mac [] createMacs() { 96 if (!DEFSupported) { 97 fail(NotSupportedMsg); 98 return null; 99 } 100 try { 101 Mac m [] = new Mac[3]; 102 m[0] = Mac.getInstance(defaultAlgorithm); 103 m[1] = Mac.getInstance(defaultAlgorithm, defaultProvider); 104 m[2] = Mac.getInstance(defaultAlgorithm, defaultProviderName); 105 return m; 106 } catch (Exception e) { 107 return null; 108 } 109 } 110 111 /** 112 * Test for <code>getInstance(String algorithm)</code> method 113 * Assertion: 114 * throws NullPointerException when algorithm is null 115 * throws NoSuchAlgorithmException when algorithm is not available 116 */ 117 public void testMac01() { 118 try { 119 Mac.getInstance(null); 120 fail("NullPointerException or NoSuchAlgorithmException should be thrown when algorithm is null"); 121 } catch (NullPointerException e) { 122 } catch (NoSuchAlgorithmException e) { 123 } 124 for (int i = 0; i < invalidValues.length; i++) { 125 try { 126 Mac.getInstance(invalidValues[i]); 127 fail("NoSuchAlgorithmException must be thrown when algorithm is not available: " 128 .concat(invalidValues[i])); 129 } catch (NoSuchAlgorithmException e) { 130 } 131 } 132 } 133 134 /** 135 * Test for <code>getInstance(String algorithm)</code> method 136 * Assertion: returns Mac object 137 */ 138 public void testMac02() throws NoSuchAlgorithmException { 139 if (!DEFSupported) { 140 fail(NotSupportedMsg); 141 return; 142 } 143 Mac mac; 144 for (int i = 0; i < validValues.length; i++) { 145 mac = Mac.getInstance(validValues[i]); 146 assertEquals("Incorrect algorithm", mac.getAlgorithm(), validValues[i]); 147 } 148 } 149 /** 150 * Test for <code>getInstance(String algorithm, String provider)</code> method 151 * Assertion: 152 * throws IllegalArgumentException when provider is null or empty 153 * throws NoSuchProviderException when provider is not available 154 */ 155 public void testMac03() throws NoSuchAlgorithmException, NoSuchProviderException { 156 if (!DEFSupported) { 157 fail(NotSupportedMsg); 158 return; 159 } 160 String provider = null; 161 for (int i = 0; i < validValues.length; i++) { 162 try { 163 Mac.getInstance(validValues[i], provider); 164 fail("IllegalArgumentException must be thrown when provider is null"); 165 } catch (IllegalArgumentException e) { 166 } 167 try { 168 Mac.getInstance(validValues[i], ""); 169 fail("IllegalArgumentException must be thrown when provider is empty"); 170 } catch (IllegalArgumentException e) { 171 } 172 for (int j = 1; j < invalidValues.length; j++) { 173 try { 174 Mac.getInstance(validValues[i], invalidValues[j]); 175 fail("NoSuchProviderException must be thrown (algorithm: " 176 .concat(validValues[i]).concat(" provider: ") 177 .concat(invalidValues[j]).concat(")")); 178 } catch (NoSuchProviderException e) { 179 } 180 } 181 } 182 } 183 184 /** 185 * Test for <code>getInstance(String algorithm, String provider)</code> method 186 * Assertion: 187 * throws NullPointerException when algorithm is null 188 * throws NoSuchAlgorithmException when algorithm is not available 189 */ 190 public void testMac04() throws NoSuchAlgorithmException, 191 IllegalArgumentException, NoSuchProviderException { 192 if (!DEFSupported) { 193 fail(NotSupportedMsg); 194 return; 195 } 196 try { 197 Mac.getInstance(null, defaultProviderName); 198 fail("NullPointerException or NoSuchAlgorithmException should be thrown when algorithm is null"); 199 } catch (NullPointerException e) { 200 } catch (NoSuchAlgorithmException e) { 201 } 202 for (int i = 0; i < invalidValues.length; i++) { 203 try { 204 Mac.getInstance(invalidValues[i], defaultProviderName); 205 fail("NoSuchAlgorithmException must be throws when algorithm is not available: " 206 .concat(invalidValues[i])); 207 } catch( NoSuchAlgorithmException e) { 208 } 209 } 210 } 211 /** 212 * Test for <code>getInstance(String algorithm, String provider)</code> method 213 * Assertion: returns Mac object 214 */ 215 public void testMac05() throws NoSuchAlgorithmException, NoSuchProviderException, 216 IllegalArgumentException { 217 if (!DEFSupported) { 218 fail(NotSupportedMsg); 219 return; 220 } 221 Mac mac; 222 for (int i = 0; i < validValues.length; i++) { 223 mac = Mac.getInstance(validValues[i], defaultProviderName); 224 assertEquals("Incorrect algorithm", mac.getAlgorithm(), validValues[i]); 225 assertEquals("Incorrect provider", mac.getProvider().getName(), 226 defaultProviderName); 227 } 228 } 229 230 /** 231 * Test for <code>getInstance(String algorithm, Provider provider)</code> method 232 * Assertion: throws IllegalArgumentException when provider is null 233 */ 234 public void testMac06() throws NoSuchAlgorithmException, NoSuchProviderException { 235 if (!DEFSupported) { 236 fail(NotSupportedMsg); 237 return; 238 } 239 Provider provider = null; 240 for (int i = 0; i < validValues.length; i++) { 241 try { 242 Mac.getInstance(validValues[i], provider); 243 fail("IllegalArgumentException must be thrown when provider is null"); 244 } catch (IllegalArgumentException e) { 245 } 246 } 247 } 248 /** 249 * Test for <code>getInstance(String algorithm, Provider provider)</code> method 250 * Assertion: 251 * throws NullPointerException when algorithm is null 252 * throws NoSuchAlgorithmException when algorithm is not available 253 */ 254 public void testMac07() throws NoSuchAlgorithmException, 255 NoSuchProviderException, IllegalArgumentException { 256 if (!DEFSupported) { 257 fail(NotSupportedMsg); 258 return; 259 } 260 try { 261 Mac.getInstance(null, defaultProvider); 262 fail("NullPointerException or NoSuchAlgorithmException should be thrown when algorithm is null"); 263 } catch (NullPointerException e) { 264 } catch (NoSuchAlgorithmException e) { 265 } 266 for (int i = 0; i < invalidValues.length; i++) { 267 try { 268 Mac.getInstance(invalidValues[i], defaultProvider); 269 fail("NoSuchAlgorithmException must be thrown when algorithm is not available: " 270 .concat(invalidValues[i])); 271 } catch (NoSuchAlgorithmException e) { 272 } 273 } 274 } 275 276 /** 277 * Test for <code>getInstance(String algorithm, Provider provider)</code> method 278 * Assertion: returns Mac object 279 */ 280 public void testMac08() throws NoSuchAlgorithmException, NoSuchProviderException, 281 IllegalArgumentException { 282 if (!DEFSupported) { 283 fail(NotSupportedMsg); 284 return; 285 } 286 Mac mac; 287 for (int i = 0; i < validValues.length; i++) { 288 mac = Mac.getInstance(validValues[i], defaultProvider); 289 assertEquals("Incorrect algorithm", mac.getAlgorithm(), validValues[i]); 290 assertEquals("Incorrect provider", mac.getProvider(), defaultProvider); 291 } 292 } 293 /** 294 * Test for <code>update</code> and <code>doFinal</code> methods 295 * Assertion: throws IllegalStateException when Mac is not initialized 296 * @throws Exception 297 */ 298 public void testMac09() throws Exception { 299 if (!DEFSupported) { 300 fail(NotSupportedMsg); 301 return; 302 } 303 Mac [] macs = createMacs(); 304 assertNotNull("Mac objects were not created", macs); 305 byte [] buf = new byte[10]; 306 ByteBuffer bBuf = ByteBuffer.wrap(buf, 0, 10); 307 byte [] bb = {(byte)1, (byte)2, (byte)3, (byte)4, (byte)5}; 308 SecretKeySpec sks = new SecretKeySpec(bb, "SHA1"); 309 for (int i = 0; i < macs.length; i++) { 310 try { 311 macs[i].update((byte)0); 312 fail("IllegalStateException must be thrown"); 313 } catch (IllegalStateException e) { 314 } 315 try { 316 macs[i].update(buf); 317 fail("IllegalStateException must be thrown"); 318 } catch (IllegalStateException e) { 319 } 320 try { 321 macs[i].update(buf, 0, 3); 322 fail("IllegalStateException must be thrown"); 323 } catch (IllegalStateException e) { 324 } 325 try { 326 macs[i].update(bBuf); 327 fail("IllegalStateException must be thrown"); 328 } catch (IllegalStateException e) { 329 } 330 try { 331 macs[i].doFinal(); 332 fail("IllegalStateException must be thrown"); 333 } catch (IllegalStateException e) { 334 } 335 try { 336 macs[i].doFinal(new byte[10]); 337 fail("IllegalStateException must be thrown"); 338 } catch (IllegalStateException e) { 339 } 340 try { 341 macs[i].doFinal(new byte[10], 0); 342 fail("IllegalStateException must be thrown"); 343 } catch (IllegalStateException e) { 344 } 345 346 macs[i].init(sks); 347 try { 348 macs[i].doFinal(new byte[1], 0); 349 fail("ShortBufferException expected"); 350 } catch (ShortBufferException e) { 351 //expected 352 } 353 } 354 } 355 /** 356 * Test for <code>doFinal(byte[] output, int outOffset)</code> method 357 * Assertion: 358 * throws ShotBufferException when outOffset is negative or 359 * outOffset >= output.length or when given buffer is small 360 */ 361 public void testMac10() throws NoSuchAlgorithmException, 362 NoSuchProviderException, IllegalArgumentException, 363 IllegalStateException, InvalidKeyException { 364 if (!DEFSupported) { 365 fail(NotSupportedMsg); 366 return; 367 } 368 Mac[] macs = createMacs(); 369 assertNotNull("Mac objects were not created", macs); 370 byte[] b = { (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0 }; 371 byte[] byteA = new byte[b.length]; 372 SecretKeySpec sks = new SecretKeySpec(b, "SHA1"); 373 for (int i = 0; i < macs.length; i++) { 374 macs[i].init(sks); 375 try { 376 macs[i].doFinal(null, 10); 377 fail("ShortBufferException must be thrown"); 378 } catch (ShortBufferException e) { 379 } 380 try { 381 macs[i].doFinal(byteA, -4); 382 fail("ShortBufferException must be thrown"); 383 } catch (ShortBufferException e) { 384 } 385 try { 386 macs[i].doFinal(byteA, 10); 387 fail("ShortBufferException must be thrown"); 388 } catch (ShortBufferException e) { 389 } 390 try { 391 macs[i].doFinal(new byte[1], 0); 392 fail("ShortBufferException must be thrown"); 393 } catch (ShortBufferException e) { 394 } 395 byte[] res = macs[i].doFinal(); 396 try { 397 macs[i].doFinal(new byte[res.length - 1], 0); 398 fail("ShortBufferException must be thrown"); 399 } catch (ShortBufferException e) { 400 } 401 } 402 } 403 404 /** 405 * Test for <code>doFinal(byte[] output, int outOffset)</code> and 406 * <code>doFinal()</code> methods Assertion: Mac result is stored in 407 * output buffer 408 */ 409 public void testMac11() throws NoSuchAlgorithmException, NoSuchProviderException, 410 IllegalArgumentException, IllegalStateException, 411 InvalidKeyException, ShortBufferException { 412 if (!DEFSupported) { 413 fail(NotSupportedMsg); 414 return; 415 } 416 Mac [] macs = createMacs(); 417 assertNotNull("Mac objects were not created", macs); 418 byte [] b = {(byte)0, (byte)0, (byte)0, (byte)0, (byte)0}; 419 SecretKeySpec scs = new SecretKeySpec(b, "SHA1"); 420 for (int i = 0; i < macs.length; i++) { 421 macs[i].init(scs); 422 byte [] res1 = macs[i].doFinal(); 423 byte [] res2 = new byte[res1.length + 10]; 424 macs[i].doFinal(res2, 0); 425 for (int j = 0; j < res1.length; j++) { 426 assertEquals("Not equals byte number: " 427 .concat(Integer.toString(j)), res1[j], res2[j]); 428 } 429 } 430 } 431 /** 432 * Test for <code>doFinal(byte[] input)</code> method 433 * Assertion: update Mac and returns result 434 */ 435 public void testMac12() throws NoSuchAlgorithmException, NoSuchProviderException, 436 IllegalArgumentException, IllegalStateException, 437 InvalidKeyException { 438 if (!DEFSupported) { 439 fail(NotSupportedMsg); 440 return; 441 } 442 Mac [] macs = createMacs(); 443 assertNotNull("Mac objects were not created", macs); 444 byte [] b = {(byte)0, (byte)0, (byte)0, (byte)0, (byte)0}; 445 byte [] upd = {(byte)5, (byte)4, (byte)3, (byte)2, (byte)1, (byte)0}; 446 SecretKeySpec scs = new SecretKeySpec(b, "SHA1"); 447 for (int i = 0; i < macs.length; i++) { 448 macs[i].init(scs); 449 byte[] res1 = macs[i].doFinal(); 450 byte[] res2 = macs[i].doFinal(); 451 assertEquals("Results are not the same", 452 IntegralToString.bytesToHexString(res1, false), 453 IntegralToString.bytesToHexString(res2, false)); 454 455 res2 = macs[i].doFinal(upd); 456 macs[i].update(upd); 457 res1 = macs[i].doFinal(); 458 assertEquals("Results are not the same", 459 IntegralToString.bytesToHexString(res1, false), 460 IntegralToString.bytesToHexString(res2, false)); 461 } 462 } 463 464 /** 465 * Test for <code>update(byte[] input, int outset, int len)</code> method 466 * Assertion: throws IllegalArgumentException when offset or len is negative, 467 * offset + len >= input.length 468 */ 469 public void testMac13() throws NoSuchAlgorithmException, 470 NoSuchProviderException, IllegalArgumentException, IllegalStateException, 471 InvalidKeyException { 472 if (!DEFSupported) { 473 fail(NotSupportedMsg); 474 return; 475 } 476 Mac [] macs = createMacs(); 477 assertNotNull("Mac objects were not created", macs); 478 byte [] b = {(byte)0, (byte)0, (byte)0, (byte)0, (byte)0}; 479 SecretKeySpec scs = new SecretKeySpec(b, "SHA1"); 480 for (int i = 0; i < macs.length; i++) { 481 macs[i].init(scs); 482 try { 483 macs[i].update(b, -10, b.length); 484 fail("IllegalArgumentException must be thrown"); 485 } catch (IllegalArgumentException e) { 486 } 487 try { 488 macs[i].update(b, 0, -10); 489 fail("IllegalArgumentException must be thrown"); 490 } catch (IllegalArgumentException e) { 491 } 492 try { 493 macs[i].update(b, 0, b.length + 1); 494 fail("IllegalArgumentException must be thrown"); 495 } catch (IllegalArgumentException e) { 496 } 497 try { 498 macs[i].update(b, b.length - 1, 2); 499 fail("IllegalArgumentException must be thrown"); 500 } catch (IllegalArgumentException e) { 501 } 502 } 503 } 504 /** 505 * Test for <code>update(byte[] input, int outset, int len)</code> and 506 * <code>update(byte[] input</code> 507 * methods 508 * Assertion: updates Mac 509 */ 510 public void testMac14() throws NoSuchAlgorithmException, 511 NoSuchProviderException, IllegalArgumentException, IllegalStateException, 512 InvalidKeyException { 513 if (!DEFSupported) { 514 fail(NotSupportedMsg); 515 return; 516 } 517 Mac [] macs = createMacs(); 518 assertNotNull("Mac objects were not created", macs); 519 byte [] b = {(byte)0, (byte)0, (byte)0, (byte)0, (byte)0}; 520 byte [] upd1 = {(byte)0, (byte)1, (byte)5, (byte)4, (byte)3, (byte)2}; 521 byte [] upd2 = {(byte)5, (byte)4, (byte)3, (byte)2}; 522 byte [] res1; 523 byte [] res2; 524 SecretKeySpec scs = new SecretKeySpec(b, "SHA1"); 525 for (int i = 0; i < macs.length; i++) { 526 macs[i].init(scs); 527 macs[i].update(upd1, 2, 4); 528 res1 = macs[i].doFinal(); 529 macs[i].init(scs); 530 macs[i].update(upd2); 531 res2 = macs[i].doFinal(); 532 assertEquals("Results are not the same", res1.length, res2.length); 533 for(int t = 0; t < res1.length; t++) { 534 assertEquals("Results are not the same", res1[t], res2[t]); 535 } 536 macs[i].init(scs); 537 macs[i].update((byte)5); 538 res1 = macs[i].doFinal(); 539 macs[i].init(scs); 540 macs[i].update(upd1,2,1); 541 res2 = macs[i].doFinal(); 542 assertEquals("Results are not the same", res1.length, res2.length); 543 for(int t = 0; t < res1.length; t++) { 544 assertEquals("Results are not the same", res1[t], res2[t]); 545 } 546 } 547 } 548 /** 549 * Test for <code>clone()</code> method 550 * Assertion: returns Mac object or throws CloneNotSupportedException 551 */ 552 public void testMacClone() throws NoSuchAlgorithmException, CloneNotSupportedException { 553 if (!DEFSupported) { 554 fail(NotSupportedMsg); 555 return; 556 } 557 Mac [] macs = createMacs(); 558 assertNotNull("Mac objects were not created", macs); 559 for (int i = 0; i < macs.length; i++) { 560 try { 561 Mac mac1 = (Mac) macs[i].clone(); 562 assertEquals(mac1.getAlgorithm(), macs[i].getAlgorithm()); 563 assertEquals(mac1.getProvider(), macs[i].getProvider()); 564 assertFalse(macs[i].equals(mac1)); 565 } catch (CloneNotSupportedException e) { 566 } 567 } 568 } 569 /** 570 * Test for 571 * <code>init(Key key, AlgorithmParameterSpec params)</code> 572 * <code>init(Key key)</code> 573 * methods 574 * Assertion: throws InvalidKeyException and InvalidAlgorithmParameterException 575 * when parameters are not appropriate 576 */ 577 public void testInit() throws NoSuchAlgorithmException, NoSuchProviderException, 578 IllegalArgumentException, IllegalStateException, InvalidAlgorithmParameterException, 579 InvalidKeyException { 580 if (!DEFSupported) { 581 fail(NotSupportedMsg); 582 return; 583 } 584 Mac [] macs = createMacs(); 585 assertNotNull("Mac objects were not created", macs); 586 byte [] b = {(byte)1, (byte)2, (byte)3, (byte)4, (byte)5}; 587 SecretKeySpec sks = new SecretKeySpec(b, "SHA1"); 588 DHGenParameterSpec algPS = new DHGenParameterSpec(1, 2); 589 PSSParameterSpec algPSS = new PSSParameterSpec(20); 590 SecretKeySpec sks1 = new SecretKeySpec(b, "RSA"); 591 592 for (int i = 0; i < macs.length; i++) { 593 macs[i].init(sks); 594 try { 595 macs[i].init(sks1, algPSS); 596 fail("init(..) accepts incorrect AlgorithmParameterSpec parameter"); 597 } catch (InvalidAlgorithmParameterException e) { 598 } 599 try { 600 macs[i].init(sks, algPS); 601 fail("init(..) accepts incorrect AlgorithmParameterSpec parameter"); 602 } catch (InvalidAlgorithmParameterException e) { 603 } 604 605 try { 606 macs[i].init(null, null); 607 fail("InvalidKeyException must be thrown"); 608 } catch (InvalidKeyException e) { 609 } 610 611 try { 612 macs[i].init(null); 613 fail("InvalidKeyException must be thrown"); 614 } catch (InvalidKeyException e) { 615 } 616// macs[i].init(sks, null); 617 } 618 } 619 620 /** 621 * Test for <code>update(ByteBuffer input)</code> 622 * <code>update(byte[] input, int offset, int len)</code> 623 * methods 624 * Assertion: processes Mac; if input is null then do nothing 625 */ 626 public void testUpdateByteBuffer01() throws NoSuchAlgorithmException, NoSuchProviderException, 627 IllegalArgumentException, IllegalStateException, InvalidAlgorithmParameterException, 628 InvalidKeyException { 629 if (!DEFSupported) { 630 fail(NotSupportedMsg); 631 return; 632 } 633 Mac [] macs = createMacs(); 634 assertNotNull("Mac objects were not created", macs); 635 byte [] bb = {(byte)1, (byte)2, (byte)3, (byte)4, (byte)5}; 636 SecretKeySpec sks = new SecretKeySpec(bb, "SHA1"); 637 ByteBuffer byteNull = null; 638 ByteBuffer byteBuff = ByteBuffer.allocate(0); 639 byte [] bb1; 640 byte [] bb2; 641 for (int i = 0; i < macs.length; i++) { 642 macs[i].init(sks); 643 bb1 = macs[i].doFinal(); 644 try { 645 macs[i].update(byteNull); 646 fail("IllegalArgumentException must be thrown because buffer is null"); 647 } catch (IllegalArgumentException e) { 648 } 649 macs[i].update(byteBuff); 650 bb2 = macs[i].doFinal(); 651 for (int t = 0; t < bb1.length; t++) { 652 assertEquals("Incorrect doFinal result", bb1[t], bb2[t]); 653 } 654 macs[i].init(sks); 655 bb1 = macs[i].doFinal(); 656 macs[i].update(null, 0, 0); 657 bb2 = macs[i].doFinal(); 658 for (int t = 0; t < bb1.length; t++) { 659 assertEquals("Incorrect doFinal result", bb1[t], bb2[t]); 660 } 661 } 662 } 663 /** 664 * Test for <code>update(ByteBuffer input)</code> 665 * <code>update(byte[] input, int offset, int len)</code> 666 * methods 667 * Assertion: processes Mac 668 */ 669 public void testUpdateByteBuffer02() throws NoSuchAlgorithmException, NoSuchProviderException, 670 IllegalArgumentException, IllegalStateException, InvalidAlgorithmParameterException, 671 InvalidKeyException { 672 if (!DEFSupported) { 673 fail(NotSupportedMsg); 674 return; 675 } 676 Mac [] macs = createMacs(); 677 assertNotNull("Mac objects were not created", macs); 678 byte [] bb = {(byte)1, (byte)2, (byte)3, (byte)4, (byte)5}; 679 SecretKeySpec sks = new SecretKeySpec(bb, "SHA1"); 680 byte [] bbuf = {(byte)5, (byte)4, (byte)3, (byte)2, (byte)1}; 681 ByteBuffer byteBuf; 682 byte [] bb1; 683 byte [] bb2; 684 for (int i = 0; i < macs.length; i++) { 685 byteBuf = ByteBuffer.allocate(5); 686 byteBuf.put(bbuf); 687 byteBuf.position(2); 688 macs[i].init(sks); 689 macs[i].update(byteBuf); 690 bb1 = macs[i].doFinal(); 691 692 macs[i].init(sks); 693 macs[i].update(bbuf, 2, 3); 694 bb2 = macs[i].doFinal(); 695 for (int t = 0; t < bb1.length; t++) { 696 assertEquals("Incorrect doFinal result", bb1[t], bb2[t]); 697 } 698 } 699 } 700 /** 701 * Test for <code>clone()</code> method 702 * Assertion: clone if provider is clo 703 */ 704 public void testClone() { 705 if (!DEFSupported) { 706 fail(NotSupportedMsg); 707 return; 708 } 709 Mac [] macs = createMacs(); 710 assertNotNull("Mac objects were not created", macs); 711 Mac res; 712 for (int i = 0; i < macs.length; i++) { 713 try { 714 res = (Mac)macs[i].clone(); 715 assertTrue("Object should not be equals", !macs[i].equals(res)); 716 assertEquals("Incorrect class", macs[i].getClass(), res.getClass()); 717 } catch (CloneNotSupportedException e) { 718 } 719 } 720 } 721 /** 722 * Test for <code>getMacLength()</code> method 723 * Assertion: return Mac length 724 */ 725 public void testGetMacLength() { 726 if (!DEFSupported) { 727 fail(NotSupportedMsg); 728 return; 729 } 730 Mac [] macs = createMacs(); 731 assertNotNull("Mac objects were not created", macs); 732 for (int i = 0; i < macs.length; i++) { 733 assertTrue("Length should be positive", (macs[i].getMacLength() >= 0)); 734 } 735 } 736 737 /** 738 * Test for <code>reset()</code> method 739 * Assertion: return Mac length 740 */ 741 public void testReset() throws InvalidKeyException { 742 if (!DEFSupported) { 743 fail(NotSupportedMsg); 744 return; 745 } 746 Mac [] macs = createMacs(); 747 assertNotNull("Mac objects were not created", macs); 748 byte [] bb = {(byte)1, (byte)2, (byte)3, (byte)4, (byte)5}; 749 SecretKeySpec sks = new SecretKeySpec(bb, "SHA1"); 750 byte [] bbuf = {(byte)5, (byte)4, (byte)3, (byte)2, (byte)1}; 751 byte [] bb1; 752 byte [] bb2; 753 for (int i = 0; i < macs.length; i++) { 754 macs[i].init(sks); 755 bb1 = macs[i].doFinal(); 756 macs[i].reset(); 757 bb2 = macs[i].doFinal(); 758 assertEquals("incorrect result",bb1.length, bb2.length); 759 for (int t = 0; t < bb1.length; t++) { 760 assertEquals("Incorrect doFinal result", bb1[t], bb2[t]); 761 } 762 macs[i].reset(); 763 macs[i].update(bbuf); 764 bb1 = macs[i].doFinal(); 765 macs[i].reset(); 766 macs[i].update(bbuf, 0, bbuf.length); 767 bb2 = macs[i].doFinal(); 768 assertEquals("incorrect result",bb1.length, bb2.length); 769 for (int t = 0; t < bb1.length; t++) { 770 assertEquals("Incorrect doFinal result", bb1[t], bb2[t]); 771 } 772 } 773 } 774 /** 775 * Test for <code>Mac</code> constructor 776 * Assertion: returns Mac object 777 */ 778 public void testMacConstructor() throws NoSuchAlgorithmException, 779 InvalidKeyException, InvalidAlgorithmParameterException { 780 if (!DEFSupported) { 781 fail(NotSupportedMsg); 782 return; 783 } 784 MacSpi spi = new MyMacSpi(); 785 Mac mac = new myMac(spi, defaultProvider, defaultAlgorithm); 786 assertEquals("Incorrect algorithm", mac.getAlgorithm(), 787 defaultAlgorithm); 788 assertEquals("Incorrect provider", mac.getProvider(), defaultProvider); 789 try { 790 mac.init(null, null); 791 fail("Exception should be thrown because init(..) uses incorrect parameters"); 792 } catch (Exception e) { 793 } 794 assertEquals("Invalid mac length", mac.getMacLength(), 0); 795 796 mac = new myMac(null, null, null); 797 assertNull("Algorithm must be null", mac.getAlgorithm()); 798 assertNull("Provider must be null", mac.getProvider()); 799 try { 800 mac.init(null, null); 801 fail("Exception should be thrown because init(..) uses incorrect parameters"); 802 } catch (Exception e) { 803 } 804 try { 805 mac.getMacLength(); 806 fail("NullPointerException must be thrown"); 807 } catch (NullPointerException e) { 808 } 809 } 810 811 public void test_getAlgorithm() throws NoSuchAlgorithmException { 812 Mac mac; 813 for (int i = 0; i < validValues.length; i++) { 814 mac = Mac.getInstance(validValues[i]); 815 assertEquals("Incorrect algorithm", mac.getAlgorithm(), validValues[i]); 816 } 817 818 mac = new Mock_Mac(null, null, null); 819 assertNull(mac.getAlgorithm()); 820 } 821 822 public void test_getProvider() throws NoSuchAlgorithmException { 823 Mac mac; 824 for (int i = 0; i < validValues.length; i++) { 825 mac = Mac.getInstance(validValues[i]); 826 assertNotNull(mac.getProvider()); 827 } 828 829 mac = new Mock_Mac(null, null, null); 830 assertNull(mac.getProvider()); 831 } 832 833 private static final byte[] TEST_INPUT = new byte[] { 834 0x01, (byte) 0xFF, 0x55, (byte) 0xAA 835 }; 836 837 public void test_ConsistentBetweenProviders() throws Exception { 838 SecretKey key = new SecretKeySpec(new byte[] { 839 (byte) 0x7b, (byte) 0x10, (byte) 0x6d, (byte) 0x68, (byte) 0x3f, (byte) 0x70, 840 (byte) 0xa3, (byte) 0xb5, (byte) 0xa3, (byte) 0xdd, (byte) 0x9f, (byte) 0x54, 841 (byte) 0x74, (byte) 0x36, (byte) 0xde, (byte) 0xa7, (byte) 0x88, (byte) 0x81, 842 (byte) 0x0d, (byte) 0x89, (byte) 0xef, (byte) 0x2e, (byte) 0x42, (byte) 0x4f, 843 }, "HmacMD5"); 844 byte[] label = new byte[] { 845 (byte) 0x6b, (byte) 0x65, (byte) 0x79, (byte) 0x20, (byte) 0x65, (byte) 0x78, 846 (byte) 0x70, (byte) 0x61, (byte) 0x6e, (byte) 0x73, (byte) 0x69, (byte) 0x6f, 847 (byte) 0x6e, 848 }; 849 byte[] seed = new byte[] { 850 (byte) 0x50, (byte) 0xf9, (byte) 0xce, (byte) 0x14, (byte) 0xb2, (byte) 0xdd, 851 (byte) 0x3d, (byte) 0xfa, (byte) 0x96, (byte) 0xd9, (byte) 0xfe, (byte) 0x3a, 852 (byte) 0x1a, (byte) 0xe5, (byte) 0x79, (byte) 0x55, (byte) 0xe7, (byte) 0xbc, 853 (byte) 0x84, (byte) 0x68, (byte) 0x0e, (byte) 0x2d, (byte) 0x20, (byte) 0xd0, 854 (byte) 0x6e, (byte) 0xb4, (byte) 0x03, (byte) 0xbf, (byte) 0xa2, (byte) 0xe6, 855 (byte) 0xc4, (byte) 0x9d, (byte) 0x50, (byte) 0xf9, (byte) 0xce, (byte) 0x14, 856 (byte) 0xbc, (byte) 0xc5, (byte) 0x9e, (byte) 0x9a, (byte) 0x36, (byte) 0xa7, 857 (byte) 0xaa, (byte) 0xfe, (byte) 0x3b, (byte) 0xca, (byte) 0xcb, (byte) 0x4c, 858 (byte) 0xfa, (byte) 0x87, (byte) 0x9a, (byte) 0xac, (byte) 0x02, (byte) 0x25, 859 (byte) 0xce, (byte) 0xda, (byte) 0x74, (byte) 0x10, (byte) 0x86, (byte) 0x9c, 860 (byte) 0x03, (byte) 0x18, (byte) 0x0f, (byte) 0xe2, 861 }; 862 Provider[] providers = Security.getProviders("Mac.HmacMD5"); 863 Provider defProvider = null; 864 byte[] output = null; 865 byte[] output2 = null; 866 for (int i = 0; i < providers.length; i++) { 867 System.out.println("provider = " + providers[i].getName()); 868 Mac mac = Mac.getInstance("HmacMD5", providers[i]); 869 mac.init(key); 870 mac.update(label); 871 mac.update(seed); 872 if (output == null) { 873 output = new byte[mac.getMacLength()]; 874 defProvider = providers[i]; 875 mac.doFinal(output, 0); 876 mac.init(new SecretKeySpec(label, "HmacMD5")); 877 output2 = mac.doFinal(output); 878 } else { 879 byte[] tmp = new byte[mac.getMacLength()]; 880 mac.doFinal(tmp, 0); 881 assertEquals(defProvider.getName() + " vs. " + providers[i].getName(), 882 Arrays.toString(output), Arrays.toString(tmp)); 883 mac.init(new SecretKeySpec(label, "HmacMD5")); 884 assertEquals(defProvider.getName() + " vs. " + providers[i].getName(), 885 Arrays.toString(output2), Arrays.toString(mac.doFinal(output))); 886 } 887 888 } 889 } 890 891 class Mock_Mac extends Mac { 892 protected Mock_Mac(MacSpi arg0, Provider arg1, String arg2) { 893 super(arg0, arg1, arg2); 894 } 895 } 896 897 public static Test suite() { 898 return new TestSuite(MacTest.class); 899 } 900} 901/** 902 * Additional class for Mac constructor verification 903 */ 904class myMac extends Mac { 905 906 public myMac(MacSpi macSpi, Provider provider, 907 String algorithm) { 908 super(macSpi, provider, algorithm); 909 } 910} 911