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.security.InvalidAlgorithmParameterException; 31 import java.security.SecureRandom; 32 import java.security.spec.AlgorithmParameterSpec; 33 import javax.crypto.KeyGeneratorSpi; 34 import javax.crypto.SecretKey; 35 36 import org.apache.harmony.crypto.tests.support.MyKeyGeneratorSpi; 37 38 import junit.framework.TestCase; 39 40 @TestTargetClass(KeyGeneratorSpi.class) 41 /** 42 * Tests for <code>KeyGeneratorSpi</code> class constructors and methods. 43 * 44 */ 45 46 public class KeyGeneratorSpiTest extends TestCase { 47 class Mock_KeyGeneratorSpi extends MyKeyGeneratorSpi { 48 49 @Override 50 protected SecretKey engineGenerateKey() { 51 return super.engineGenerateKey(); 52 } 53 54 @Override 55 protected void engineInit(SecureRandom random) { 56 super.engineInit(random); 57 } 58 59 @Override 60 protected void engineInit(AlgorithmParameterSpec params, SecureRandom random) 61 throws InvalidAlgorithmParameterException { 62 super.engineInit(params, random); 63 } 64 65 @Override 66 protected void engineInit(int keysize, SecureRandom random) { 67 super.engineInit(keysize, random); 68 } 69 70 } 71 72 /** 73 * Test for <code>KeyGeneratorSpi</code> constructor Assertion: constructs 74 * KeyGeneratorSpi 75 */ 76 @TestTargetNew(level = TestLevel.COMPLETE, notes = "", method = "KeyGeneratorSpi", args = {}) 77 public void testKeyGeneratorSpi01() throws InvalidAlgorithmParameterException { 78 Mock_KeyGeneratorSpi kgSpi = new Mock_KeyGeneratorSpi(); 79 assertNull("Not null result", kgSpi.engineGenerateKey()); 80 try { 81 kgSpi.engineInit(77, new SecureRandom()); 82 fail("IllegalArgumentException must be thrown"); 83 } catch (IllegalArgumentException e) { 84 } 85 try { 86 kgSpi.engineInit(new SecureRandom()); 87 fail("IllegalArgumentException must be thrown"); 88 } catch (IllegalArgumentException e) { 89 } 90 AlgorithmParameterSpec aps = null; 91 try { 92 kgSpi.engineInit(aps, new SecureRandom()); 93 fail("InvalidAlgorithmParameterException must be thrown when parameter is null"); 94 } catch (InvalidAlgorithmParameterException e) { 95 } 96 aps = new APSpecSpi(); 97 kgSpi.engineInit(aps, new SecureRandom()); 98 } 99 100 } 101 102 class APSpecSpi implements AlgorithmParameterSpec { 103 104 } 105