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.math.BigInteger; 26 import java.security.AlgorithmParameters; 27 import java.security.InvalidAlgorithmParameterException; 28 import java.security.InvalidKeyException; 29 import java.security.Key; 30 import java.security.NoSuchAlgorithmException; 31 import java.security.spec.AlgorithmParameterSpec; 32 import java.security.spec.RSAKeyGenParameterSpec; 33 34 import javax.crypto.ExemptionMechanismException; 35 import javax.crypto.ShortBufferException; 36 import javax.crypto.ExemptionMechanismSpi; 37 import org.apache.harmony.crypto.tests.support.MyExemptionMechanismSpi; 38 39 import junit.framework.TestCase; 40 41 42 /** 43 * Tests for <code>ExemptionMechanismSpi</code> class constructors and 44 * methods. 45 * 46 */ 47 public class ExemptionMechanismSpiTest extends TestCase { 48 class Mock_ExemptionMechanismSpi extends MyExemptionMechanismSpi{ 49 50 @Override 51 protected byte[] engineGenExemptionBlob() throws ExemptionMechanismException { 52 return super.engineGenExemptionBlob(); 53 } 54 55 @Override 56 protected int engineGenExemptionBlob(byte[] output, int outputOffset) throws ShortBufferException, ExemptionMechanismException { 57 return super.engineGenExemptionBlob(output, outputOffset); 58 } 59 60 @Override 61 protected int engineGetOutputSize(int inputLen) { 62 return super.engineGetOutputSize(inputLen); 63 } 64 65 @Override 66 protected void engineInit(Key key) throws InvalidKeyException, ExemptionMechanismException { 67 super.engineInit(key); 68 69 } 70 71 @Override 72 protected void engineInit(Key key, AlgorithmParameterSpec params) throws InvalidKeyException, InvalidAlgorithmParameterException, ExemptionMechanismException { 73 super.engineInit(key, params); 74 75 } 76 77 @Override 78 protected void engineInit(Key key, AlgorithmParameters params) throws InvalidKeyException, InvalidAlgorithmParameterException, ExemptionMechanismException { 79 super.engineInit(key, params); 80 81 } 82 83 } 84 85 /** 86 * Test for <code>ExemptionMechanismSpi</code> constructor Assertion: 87 * constructs ExemptionMechanismSpi 88 * @throws Exception 89 */ 90 public void testExemptionMechanismSpi01() throws Exception { 91 Mock_ExemptionMechanismSpi emSpi = new Mock_ExemptionMechanismSpi(){}; 92 int len = MyExemptionMechanismSpi.getLength(); 93 byte [] bbRes = emSpi.engineGenExemptionBlob(); 94 assertEquals("Incorrect length", bbRes.length, len); 95 assertEquals("Incorrect result", 96 emSpi.engineGenExemptionBlob(new byte[1], len), len); 97 assertEquals("Incorrect output size", 10, emSpi.engineGetOutputSize(100)); 98 Key key = null; 99 AlgorithmParameters params = null; 100 AlgorithmParameterSpec parSpec = null; 101 try { 102 emSpi.engineInit(key); 103 fail("InvalidKeyException must be thrown"); 104 } catch (InvalidKeyException e) { 105 } 106 try { 107 emSpi.engineInit(key, params); 108 fail("InvalidKeyException must be thrown"); 109 } catch (InvalidKeyException e) { 110 } 111 try { 112 emSpi.engineInit(key, parSpec); 113 fail("InvalidKeyException must be thrown"); 114 } catch (InvalidKeyException e) { 115 } 116 key = ((MyExemptionMechanismSpi)emSpi).new tmp1Key("Proba", new byte[0]); 117 try { 118 emSpi.engineInit(key); 119 fail("ExemptionMechanismException must be thrown"); 120 } catch (ExemptionMechanismException e) { 121 } 122 try { 123 emSpi.engineInit(key, params); 124 fail("ExemptionMechanismException must be thrown"); 125 } catch (ExemptionMechanismException e) { 126 } 127 try { 128 emSpi.engineInit(key, parSpec); 129 fail("ExemptionMechanismException must be thrown"); 130 } catch (ExemptionMechanismException e) { 131 } 132 key = ((MyExemptionMechanismSpi)emSpi).new tmpKey("Proba", new byte[0]); 133 emSpi.engineInit(key); 134 emSpi.engineInit(key, AlgorithmParameters.getInstance("DH")); 135 emSpi.engineInit(key, new RSAKeyGenParameterSpec(10, new BigInteger ("10"))); 136 137 assertEquals("Incorrect result", 10, emSpi.engineGetOutputSize(100)); 138 } 139 } 140