Home | History | Annotate | Download | only in security
      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 dalvik.annotation.TestLevel;
     20 import dalvik.annotation.TestTargetNew;
     21 import dalvik.annotation.TestTargets;
     22 import java.security.KeyPair;
     23 import java.security.KeyPairGenerator;
     24 import java.security.NoSuchAlgorithmException;
     25 import junit.framework.TestCase;
     26 
     27 public abstract class KeyPairGeneratorTest extends TestCase {
     28 
     29     private final String algorithmName;
     30     private final TestHelper<KeyPair> helper;
     31 
     32     private KeyPairGenerator generator;
     33 
     34     protected KeyPairGeneratorTest(String algorithmName, TestHelper<KeyPair> helper) {
     35         this.algorithmName = algorithmName;
     36         this.helper = helper;
     37     }
     38 
     39     protected void setUp() throws Exception {
     40         super.setUp();
     41         generator = getKeyPairGenerator();
     42     }
     43 
     44     private KeyPairGenerator getKeyPairGenerator() {
     45         try {
     46             return KeyPairGenerator.getInstance(algorithmName);
     47         } catch (NoSuchAlgorithmException e) {
     48             fail("cannot get KeyPairGenerator: " + e);
     49             return null;
     50         }
     51     }
     52 
     53     @TestTargets({
     54         @TestTargetNew(
     55                 level = TestLevel.ADDITIONAL,
     56                 method = "initialize",
     57                 args = {int.class}
     58             ),
     59             @TestTargetNew(
     60                 level = TestLevel.ADDITIONAL,
     61                 method = "generateKeyPair",
     62                 args = {}
     63             ),
     64             @TestTargetNew(
     65                 level=TestLevel.COMPLETE,
     66                 method="method",
     67                 args={}
     68             )
     69     })
     70     public void testKeyPairGenerator() throws NoSuchAlgorithmException {
     71         generator.initialize(1024);
     72 
     73         KeyPair keyPair = generator.generateKeyPair();
     74 
     75         assertNotNull("no keypair generated", keyPair);
     76         assertNotNull("no public key generated", keyPair.getPublic());
     77         assertNotNull("no private key generated", keyPair.getPrivate());
     78 
     79         helper.test(keyPair);
     80     }
     81 }
     82