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 18package org.apache.harmony.crypto.tests.javax.crypto; 19 20import java.math.BigInteger; 21import java.security.AlgorithmParameters; 22import java.security.InvalidAlgorithmParameterException; 23import java.security.InvalidKeyException; 24import java.security.Key; 25import java.security.NoSuchAlgorithmException; 26import java.security.NoSuchProviderException; 27import java.security.Provider; 28import java.security.SecureRandom; 29import java.security.spec.AlgorithmParameterSpec; 30import java.security.spec.RSAKeyGenParameterSpec; 31 32import javax.crypto.ExemptionMechanism; 33import javax.crypto.ExemptionMechanismException; 34import javax.crypto.ExemptionMechanismSpi; 35import javax.crypto.KeyGenerator; 36import javax.crypto.ShortBufferException; 37 38import org.apache.harmony.crypto.tests.support.MyExemptionMechanismSpi; 39import org.apache.harmony.security.tests.support.SpiEngUtils; 40 41import junit.framework.TestCase; 42 43/** 44 * Tests for <code>ExemptionMechanism</code> class constructors and methods 45 * 46 */ 47public class ExemptionMechanismTest extends TestCase { 48 49 private static final String srvExemptionMechanism = "ExemptionMechanism"; 50 51 private static final String defaultAlg = "EMech"; 52 53 private static final String ExemptionMechanismProviderClass = "org.apache.harmony.crypto.tests.support.MyExemptionMechanismSpi"; 54 55 /** 56 * Test for <code>ExemptionMechanism</code> constructor 57 * Assertion: creates new object using provider and mechanism name 58 */ 59 public void testExemptionMechanism() throws Exception { 60 Provider mProv = (new SpiEngUtils()).new MyProvider("MyExMechProvider", 61 "Provider for ExemptionMechanism testing", 62 srvExemptionMechanism.concat(".").concat(defaultAlg), 63 ExemptionMechanismProviderClass); 64 65 ExemptionMechanismSpi spi = new MyExemptionMechanismSpi(); 66 67 ExemptionMechanism em = new ExemptionMechanism(spi, mProv, defaultAlg) {}; 68 assertEquals("Incorrect provider", em.getProvider(), mProv); 69 assertEquals("Incorrect algorithm", em.getName(), defaultAlg); 70 try { 71 em.init(null); 72 fail("InvalidKeyException must be thrown"); 73 } catch (InvalidKeyException e) {} 74 75 try { 76 em.getOutputSize(100); 77 fail("IllegalStateException must be thrown"); 78 } catch (IllegalStateException e) {} 79 80 81 em = new ExemptionMechanism(null, null, null) {}; 82 assertNull("Incorrect mechanism", em.getName()); 83 assertNull("Incorrect provider", em.getProvider()); 84 try { 85 em.init(null); 86 fail("NullPointerException must be thrown"); 87 } catch (NullPointerException e) {} 88 try { 89 em.getOutputSize(100); 90 fail("IllegalStateException must be thrown"); 91 } catch (IllegalStateException e) {} 92 } 93 94 /** 95 * javax/crypto/ExemptionMechanism#getInstance(String algorithm, String provider) 96 * Checks exception order 97 */ 98 public void testGetInstance() throws Exception { 99 //Regression for HARMONY-762 100 try { 101 ExemptionMechanism.getInstance((String) null, "aaa"); 102 fail("NoSuchProviderException must be thrown"); 103 } catch (NoSuchProviderException pe) { 104 //expected 105 } 106 try { 107 ExemptionMechanism.getInstance("AlgName", (String)null); 108 fail("IllegalArgumentException expected"); 109 } catch (IllegalArgumentException e) { 110 //expected 111 } 112 } 113 114 /** 115 * Test for <code>isCryptoAllowed(Key key)</code> method 116 */ 117 public void testIsCryptoAllowed() throws Exception { 118 119 //Regression for HARMONY-1029 120 Provider mProv = (new SpiEngUtils()).new MyProvider("MyExMechProvider", 121 "Provider for ExemptionMechanism testing", 122 srvExemptionMechanism.concat(".").concat(defaultAlg), 123 ExemptionMechanismProviderClass); 124 125 ExemptionMechanism em = new ExemptionMechanism( 126 new MyExemptionMechanismSpi(), mProv, defaultAlg) { 127 }; 128 129 Key key = new MyExemptionMechanismSpi().new tmpKey("Proba", new byte[0]); 130 131 assertFalse(em.isCryptoAllowed(key)); 132 133 em.init(key); 134 assertFalse(em.isCryptoAllowed(key)); 135 136 em.genExemptionBlob(); 137 assertTrue(em.isCryptoAllowed(key)); 138 139 Key key1 = new MyExemptionMechanismSpi().new tmpKey("Proba", 140 new byte[] { 1 }); 141 assertFalse(em.isCryptoAllowed(key1)); 142 143 em.init(key1); 144 assertFalse(em.isCryptoAllowed(key)); 145 } 146 147 /** 148 * Test for <code>genExemptionBlob((byte[] output, int outputOffset)</code> method 149 */ 150 public void testGenExemptionBlob() throws Exception { 151 //Regression for HARMONY-1029 152 Provider mProv = (new SpiEngUtils()).new MyProvider("MyExMechProvider", 153 "Provider for ExemptionMechanism testing", 154 srvExemptionMechanism.concat(".").concat(defaultAlg), 155 ExemptionMechanismProviderClass); 156 157 ExemptionMechanism em = new ExemptionMechanism( 158 new MyExemptionMechanismSpi(), mProv, defaultAlg) { 159 }; 160 161 Key key = new MyExemptionMechanismSpi().new tmpKey("Proba", new byte[0]); 162 163 em.init(key); 164 // ExemptionMechanism doesn't check parameters 165 // it is a responsibility of ExemptionMechanismSpi 166 em.genExemptionBlob(null, 0); 167 em.genExemptionBlob(new byte[0], 0); 168 em.genExemptionBlob(new byte[10], -5); 169 } 170 171 class Mock_ExemptionMechanismSpi extends MyExemptionMechanismSpi { 172 @Override 173 protected byte[] engineGenExemptionBlob() 174 throws ExemptionMechanismException { 175 throw new ExemptionMechanismException(); 176 } 177 178 @Override 179 protected int engineGenExemptionBlob(byte[] output, int outputOffset) 180 throws ShortBufferException, ExemptionMechanismException { 181 if (output.length - outputOffset < 182 super.engineGenExemptionBlob(output, outputOffset)) { 183 throw new ShortBufferException(); 184 } 185 if (output[outputOffset + 3] == 33) { 186 throw new ExemptionMechanismException(); 187 } 188 return super.engineGenExemptionBlob(output, outputOffset); 189 } 190 } 191 192 public void test_genExemptionBlob() throws InvalidKeyException, 193 ExemptionMechanismException { 194 Provider mProv = (new SpiEngUtils()).new MyProvider("MyExMechProvider", 195 "Provider for ExemptionMechanism testing", 196 srvExemptionMechanism.concat(".").concat(defaultAlg), 197 ExemptionMechanismProviderClass); 198 199 ExemptionMechanism em = new ExemptionMechanism( 200 new MyExemptionMechanismSpi(), mProv, defaultAlg) { 201 }; 202 203 Key key = new MyExemptionMechanismSpi().new tmpKey("Proba", new byte[0]); 204 205 try { 206 em.genExemptionBlob(); 207 fail("IllegalStateException expected"); 208 } catch (IllegalStateException e) { 209 //failed 210 } 211 212 em.init(key); 213 214 assertNotNull(em.genExemptionBlob()); 215 216 em = new ExemptionMechanism( 217 new Mock_ExemptionMechanismSpi(), mProv, defaultAlg) { 218 }; 219 key = new Mock_ExemptionMechanismSpi().new tmpKey("Proba", new byte[0]); 220 em.init(key); 221 222 try { 223 em.genExemptionBlob(); 224 fail("ExemptionMechanismException expected"); 225 } catch (ExemptionMechanismException e) { 226 //failed 227 } 228 } 229 230 public void test_genExemptionBlob$B() throws InvalidKeyException, 231 ExemptionMechanismException, ShortBufferException { 232 Provider mProv = (new SpiEngUtils()).new MyProvider("MyExMechProvider", 233 "Provider for ExemptionMechanism testing", 234 srvExemptionMechanism.concat(".").concat(defaultAlg), 235 ExemptionMechanismProviderClass); 236 237 ExemptionMechanism em = new ExemptionMechanism( 238 new Mock_ExemptionMechanismSpi(), mProv, defaultAlg) { 239 }; 240 241 Key key = new Mock_ExemptionMechanismSpi().new tmpKey("Proba", new byte[0]); 242 243 try { 244 em.genExemptionBlob(new byte[10]); 245 fail("IllegalStateException expected"); 246 } catch (IllegalStateException e) { 247 //failed 248 } 249 250 em.init(key); 251 252 assertEquals(5, (em.genExemptionBlob(new byte[10]))); 253 254 try { 255 em.genExemptionBlob(new byte[2]); 256 fail("ShortBufferException expected"); 257 } catch (ShortBufferException e) { 258 //failed 259 } 260 byte[] b = new byte[] {0,0,0,33,0}; 261 262 try { 263 em.genExemptionBlob(b); 264 fail("ExemptionMechanismException expected"); 265 } catch (ExemptionMechanismException e) { 266 //failed 267 } 268 } 269 270 public void test_genExemptionBlob$BI() throws InvalidKeyException, 271 ExemptionMechanismException, ShortBufferException { 272 Provider mProv = (new SpiEngUtils()).new MyProvider("MyExMechProvider", 273 "Provider for ExemptionMechanism testing", 274 srvExemptionMechanism.concat(".").concat(defaultAlg), 275 ExemptionMechanismProviderClass); 276 277 ExemptionMechanism em = new ExemptionMechanism( 278 new Mock_ExemptionMechanismSpi(), mProv, defaultAlg) { 279 }; 280 281 Key key = new Mock_ExemptionMechanismSpi().new tmpKey("Proba", new byte[0]); 282 283 try { 284 em.genExemptionBlob(new byte[10], 2); 285 fail("IllegalStateException expected"); 286 } catch (IllegalStateException e) { 287 //failed 288 } 289 290 em.init(key); 291 292 assertEquals(5, (em.genExemptionBlob(new byte[10], 5))); 293 294 try { 295 em.genExemptionBlob(new byte[7], 3); 296 fail("ShortBufferException expected"); 297 } catch (ShortBufferException e) { 298 //failed 299 } 300 byte[] b = new byte[] {0, 0, 0, 1, 2, 3, 33, 0}; 301 302 try { 303 em.genExemptionBlob(b, 3); 304 fail("ExemptionMechanismException expected"); 305 } catch (ExemptionMechanismException e) { 306 //failed 307 } 308 } 309 310 public void test_getInstanceLjava_lang_String() throws Exception { 311 try { 312 ExemptionMechanism.getInstance((String) null); 313 fail("NullPointerException expected"); 314 } catch (NullPointerException e) { 315 //expected 316 } 317 318 Provider mProv = (new SpiEngUtils()).new MyProvider("MyExMechProvider", 319 "Provider for ExemptionMechanism testing", 320 srvExemptionMechanism.concat(".").concat(defaultAlg), 321 ExemptionMechanismProviderClass); 322 323 ExemptionMechanism em = new ExemptionMechanism( 324 new Mock_ExemptionMechanismSpi(), mProv, defaultAlg) { 325 }; 326 327 try { 328 em.getInstance("WrongAlgName"); 329 fail("NoSuchAlgorithmException expected"); 330 } catch (NoSuchAlgorithmException e) { 331 //expected 332 } 333 } 334 335 public void test_getInstanceLjava_lang_StringLjava_security_Provider() 336 throws Exception { 337 Provider mProv = (new SpiEngUtils()).new MyProvider("MyExMechProvider", 338 "Provider for ExemptionMechanism testing", 339 srvExemptionMechanism.concat(".").concat(defaultAlg), 340 ExemptionMechanismProviderClass); 341 342 try { 343 ExemptionMechanism.getInstance((String) null, mProv); 344 fail("NullPointerException expected"); 345 } catch (NullPointerException e) { 346 //expected 347 } 348 349 ExemptionMechanism em = new ExemptionMechanism( 350 new Mock_ExemptionMechanismSpi(), mProv, defaultAlg) { 351 }; 352 353 try { 354 em.getInstance("WrongAlgName", mProv); 355 fail("NoSuchAlgorithmException expected"); 356 } catch (NoSuchAlgorithmException e) { 357 //expected 358 } 359 360 try { 361 em.getInstance("WrongAlgName", (Provider)null); 362 fail("IllegalArgumentException expected"); 363 } catch (IllegalArgumentException e) { 364 //expected 365 } 366 } 367 368 public void test_getName() throws Exception { 369 Provider mProv = (new SpiEngUtils()).new MyProvider("MyExMechProvider", 370 "Provider for ExemptionMechanism testing", 371 srvExemptionMechanism.concat(".").concat(defaultAlg), 372 ExemptionMechanismProviderClass); 373 374 ExemptionMechanism em = new ExemptionMechanism( 375 new MyExemptionMechanismSpi(), mProv, defaultAlg) { 376 }; 377 378 Key key = new MyExemptionMechanismSpi().new tmpKey("Proba", new byte[0]); 379 380 assertEquals(defaultAlg, em.getName()); 381 } 382 383 public void test_getOutputSizeI() throws Exception { 384 Provider mProv = (new SpiEngUtils()).new MyProvider("MyExMechProvider", 385 "Provider for ExemptionMechanism testing", 386 srvExemptionMechanism.concat(".").concat(defaultAlg), 387 ExemptionMechanismProviderClass); 388 389 ExemptionMechanism em = new ExemptionMechanism( 390 new MyExemptionMechanismSpi(), mProv, defaultAlg) { 391 }; 392 393 Key key = new MyExemptionMechanismSpi().new tmpKey("Proba", new byte[0]); 394 395 try { 396 em.getOutputSize(10); 397 fail("IllegalStateException expected"); 398 } catch (IllegalStateException e) { 399 //failed 400 } 401 402 em.init(key); 403 assertEquals(10, em.getOutputSize(10)); 404 } 405 406 public void test_getProvider() throws Exception { 407 Provider mProv = (new SpiEngUtils()).new MyProvider("MyExMechProvider", 408 "Provider for ExemptionMechanism testing", 409 srvExemptionMechanism.concat(".").concat(defaultAlg), 410 ExemptionMechanismProviderClass); 411 412 ExemptionMechanism em = new ExemptionMechanism( 413 new MyExemptionMechanismSpi(), mProv, defaultAlg) { 414 }; 415 416 Key key = new MyExemptionMechanismSpi().new tmpKey("Proba", new byte[0]); 417 418 assertEquals(mProv, em.getProvider()); 419 } 420 421 public void test_initLjava_security_Key() throws Exception { 422 Provider mProv = (new SpiEngUtils()).new MyProvider("MyExMechProvider", 423 "Provider for ExemptionMechanism testing", 424 srvExemptionMechanism.concat(".").concat(defaultAlg), 425 ExemptionMechanismProviderClass); 426 427 ExemptionMechanism em = new ExemptionMechanism( 428 new MyExemptionMechanismSpi(), mProv, defaultAlg) { 429 }; 430 431 Key key = new MyExemptionMechanismSpi().new tmpKey("Proba", new byte[0]); 432 433 em.init(key); 434 435 KeyGenerator kg = KeyGenerator.getInstance("DES"); 436 kg.init(56, new SecureRandom()); 437 key = kg.generateKey(); 438 439 try { 440 em.init(null); 441 fail("InvalidKeyException expected"); 442 } catch (InvalidKeyException e) { 443 //expected 444 } 445 446 try { 447 em.init(key); 448 fail("ExemptionMechanismException expected"); 449 } catch (ExemptionMechanismException e) { 450 //expected 451 } 452 } 453 454 public void test_initLjava_security_KeyLjava_security_AlgorithmParameters() 455 throws Exception { 456 Provider mProv = (new SpiEngUtils()).new MyProvider("MyExMechProvider", 457 "Provider for ExemptionMechanism testing", 458 srvExemptionMechanism.concat(".").concat(defaultAlg), 459 ExemptionMechanismProviderClass); 460 461 ExemptionMechanism em = new ExemptionMechanism( 462 new MyExemptionMechanismSpi(), mProv, defaultAlg) { 463 }; 464 465 Key key = new MyExemptionMechanismSpi().new tmpKey("Proba", new byte[0]); 466 467 em.init(key, AlgorithmParameters.getInstance("DES")); 468 469 try { 470 em.init(key, (AlgorithmParameters)null); 471 fail("InvalidAlgorithmParameterException expected"); 472 } catch (InvalidAlgorithmParameterException e) { 473 //expected 474 } 475 476 KeyGenerator kg = KeyGenerator.getInstance("DES"); 477 kg.init(56, new SecureRandom()); 478 key = kg.generateKey(); 479 480 try { 481 em.init(null, AlgorithmParameters.getInstance("DES")); 482 fail("InvalidKeyException expected"); 483 } catch (InvalidKeyException e) { 484 //expected 485 } 486 487 try { 488 em.init(key, AlgorithmParameters.getInstance("DES")); 489 fail("ExemptionMechanismException expected"); 490 } catch (ExemptionMechanismException e) { 491 //expected 492 } 493 } 494 495 public void test_initLjava_security_KeyLjava_security_spec_AlgorithmParameterSpec() 496 throws Exception{ 497 Provider mProv = (new SpiEngUtils()).new MyProvider("MyExMechProvider", 498 "Provider for ExemptionMechanism testing", 499 srvExemptionMechanism.concat(".").concat(defaultAlg), 500 ExemptionMechanismProviderClass); 501 502 ExemptionMechanism em = new ExemptionMechanism( 503 new MyExemptionMechanismSpi(), mProv, defaultAlg) { 504 }; 505 506 Key key = new MyExemptionMechanismSpi().new tmpKey("Proba", new byte[0]); 507 508 em.init(key, new RSAKeyGenParameterSpec(10, new BigInteger("10"))); 509 510 try { 511 em.init(key, (AlgorithmParameterSpec)null); 512 fail("InvalidAlgorithmParameterException expected"); 513 } catch (InvalidAlgorithmParameterException e) { 514 //expected 515 } 516 517 KeyGenerator kg = KeyGenerator.getInstance("DES"); 518 kg.init(56, new SecureRandom()); 519 key = kg.generateKey(); 520 521 try { 522 em.init(null, new RSAKeyGenParameterSpec(10, new BigInteger("10"))); 523 fail("InvalidKeyException expected"); 524 } catch (InvalidKeyException e) { 525 //expected 526 } 527 528 try { 529 em.init(key, new RSAKeyGenParameterSpec(10, new BigInteger("10"))); 530 fail("ExemptionMechanismException expected"); 531 } catch (ExemptionMechanismException e) { 532 //expected 533 } 534 } 535} 536 537 538