1 /* 2 * Copyright (C) 2008 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package tests.targets.security; 18 19 import dalvik.annotation.TestLevel; 20 import dalvik.annotation.TestTargetClass; 21 import dalvik.annotation.TestTargetNew; 22 import dalvik.annotation.TestTargets; 23 24 import junit.framework.TestCase; 25 26 import java.security.KeyPair; 27 import java.security.KeyPairGenerator; 28 import java.security.NoSuchAlgorithmException; 29 30 public abstract class KeyPairGeneratorTest extends TestCase { 31 32 private final String algorithmName; 33 private final TestHelper<KeyPair> helper; 34 35 private KeyPairGenerator generator; 36 37 protected KeyPairGeneratorTest(String algorithmName, TestHelper<KeyPair> helper) { 38 this.algorithmName = algorithmName; 39 this.helper = helper; 40 } 41 42 /* (non-Javadoc) 43 * @see junit.framework.TestCase#setUp() 44 */ 45 protected void setUp() throws Exception { 46 super.setUp(); 47 generator = getKeyPairGenerator(); 48 } 49 50 private KeyPairGenerator getKeyPairGenerator() { 51 try { 52 return KeyPairGenerator.getInstance(algorithmName); 53 } catch (NoSuchAlgorithmException e) { 54 fail("cannot get KeyPairGenerator: " + e); 55 return null; 56 } 57 } 58 59 @TestTargets({ 60 @TestTargetNew( 61 level = TestLevel.ADDITIONAL, 62 method = "initialize", 63 args = {int.class} 64 ), 65 @TestTargetNew( 66 level = TestLevel.ADDITIONAL, 67 method = "generateKeyPair", 68 args = {} 69 ), 70 @TestTargetNew( 71 level=TestLevel.COMPLETE, 72 method="method", 73 args={} 74 ) 75 }) 76 public void testKeyPairGenerator() { 77 generator.initialize(1024); 78 79 KeyPair keyPair = generator.generateKeyPair(); 80 81 assertNotNull("no keypair generated", keyPair); 82 assertNotNull("no public key generated", keyPair.getPublic()); 83 assertNotNull("no private key generated", keyPair.getPrivate()); 84 85 helper.test(keyPair); 86 } 87 } 88