Home | History | Annotate | Download | only in security
      1 /*
      2  * Copyright (C) 2009 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 package tests.security;
     17 
     18 import java.security.Key;
     19 import java.security.KeyFactory;
     20 import java.security.KeyPair;
     21 import java.security.NoSuchAlgorithmException;
     22 import java.security.PrivateKey;
     23 import java.security.PublicKey;
     24 import java.security.spec.InvalidKeySpecException;
     25 import java.security.spec.KeySpec;
     26 import junit.framework.TestCase;
     27 
     28 public abstract class KeyFactoryTest<PublicKeySpec extends KeySpec, PrivateKeySpec extends KeySpec>
     29         extends TestCase {
     30 
     31     private final String algorithmName;
     32     private final Class<PublicKeySpec> publicKeySpecClass;
     33     private final Class<PrivateKeySpec> privateKeySpecClass;
     34     private KeyFactory factory;
     35 
     36     public KeyFactoryTest(String algorithmName,
     37             Class<PublicKeySpec> publicKeySpecClass,
     38             Class<PrivateKeySpec> privateKeySpecClass) {
     39         this.algorithmName = algorithmName;
     40         this.publicKeySpecClass = publicKeySpecClass;
     41         this.privateKeySpecClass = privateKeySpecClass;
     42     }
     43 
     44     protected void setUp() throws Exception {
     45         super.setUp();
     46         factory = getFactory();
     47     }
     48 
     49     private KeyFactory getFactory() throws Exception {
     50         return KeyFactory.getInstance(algorithmName);
     51     }
     52 
     53     public void testKeyFactory() throws Exception {
     54         PrivateKeySpec privateKeySpec = factory.getKeySpec(DefaultKeys.getPrivateKey(algorithmName),
     55                                                            privateKeySpecClass);
     56         PrivateKey privateKey =  factory.generatePrivate(privateKeySpec);
     57         PublicKeySpec publicKeySpec = factory.getKeySpec(DefaultKeys.getPublicKey(algorithmName),
     58                                                          publicKeySpecClass);
     59         PublicKey publicKey = factory.generatePublic(publicKeySpec);
     60         check(new KeyPair(publicKey, privateKey));
     61     }
     62 
     63     protected void check(KeyPair keyPair) throws Exception {}
     64 }
     65