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 */
16package tests.security;
17
18import java.security.Key;
19import java.security.KeyFactory;
20import java.security.KeyPair;
21import java.security.NoSuchAlgorithmException;
22import java.security.PrivateKey;
23import java.security.PublicKey;
24import java.security.spec.InvalidKeySpecException;
25import java.security.spec.KeySpec;
26import junit.framework.TestCase;
27
28public 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) {}
64}
65