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.security; 18 19 import java.security.KeyPair; 20 import java.security.KeyPairGenerator; 21 import java.security.NoSuchAlgorithmException; 22 import junit.framework.TestCase; 23 24 public abstract class KeyPairGeneratorTest extends TestCase { 25 26 private final String algorithmName; 27 private final TestHelper<KeyPair> helper; 28 29 private KeyPairGenerator generator; 30 31 protected KeyPairGeneratorTest(String algorithmName, TestHelper<KeyPair> helper) { 32 this.algorithmName = algorithmName; 33 this.helper = helper; 34 } 35 36 protected void setUp() throws Exception { 37 super.setUp(); 38 generator = getKeyPairGenerator(); 39 } 40 41 private KeyPairGenerator getKeyPairGenerator() { 42 try { 43 return KeyPairGenerator.getInstance(algorithmName); 44 } catch (NoSuchAlgorithmException e) { 45 fail("cannot get KeyPairGenerator: " + e); 46 return null; 47 } 48 } 49 50 public void testKeyPairGenerator() throws NoSuchAlgorithmException { 51 generator.initialize(1024); 52 53 KeyPair keyPair = generator.generateKeyPair(); 54 55 assertNotNull("no keypair generated", keyPair); 56 assertNotNull("no public key generated", keyPair.getPublic()); 57 assertNotNull("no private key generated", keyPair.getPrivate()); 58 59 helper.test(keyPair); 60 } 61 } 62