1 /*
2  *  Licensed to the Apache Software Foundation (ASF) under one or more
3  *  contributor license agreements.  See the NOTICE file distributed with
4  *  this work for additional information regarding copyright ownership.
5  *  The ASF licenses this file to You under the Apache License, Version 2.0
6  *  (the "License"); you may not use this file except in compliance with
7  *  the License.  You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  */
17 
18 /**
19 * @author Vera Y. Petrashkova
20 * @version $Revision$
21 */
22 
23 package org.apache.harmony.security.tests.java.security;
24 
25 import java.security.AlgorithmParameters;
26 import java.security.AlgorithmParametersSpi;
27 import java.security.KeyPair;
28 import java.security.KeyPairGenerator;
29 import java.security.NoSuchAlgorithmException;
30 import java.security.NoSuchProviderException;
31 import java.security.Provider;
32 import java.security.SecureRandom;
33 
34 import org.apache.harmony.security.tests.java.security.AlgorithmParametersTest.MyAlgorithmParameters;
35 import org.apache.harmony.security.tests.java.security.AlgorithmParametersTest.myAlgP;
36 import org.apache.harmony.security.tests.support.SpiEngUtils;
37 
38 import junit.framework.TestCase;
39 
40 /**
41  * Tests for KeyPairGenerator class
42  *
43  */
44 public class KeyPairGenerator3Test extends TestCase {
45 
46     private static String validProviderName = null;
47 
48     public static Provider validProvider = null;
49 
50     private static boolean DSASupported = false;
51 
52     private static String NotSupportMsg = KeyPairGenerator1Test.NotSupportMsg;
53 
54     static {
55         validProvider = SpiEngUtils.isSupport(
56                 KeyPairGenerator1Test.validAlgName,
57                 KeyPairGenerator1Test.srvKeyPairGenerator);
58         DSASupported = (validProvider != null);
59         validProviderName = (DSASupported ? validProvider.getName() : null);
60     }
61 
createKPGen()62     protected KeyPairGenerator[] createKPGen() {
63         if (!DSASupported) {
64             fail(KeyPairGenerator1Test.validAlgName
65                     + " algorithm is not supported");
66             return null;
67         }
68         KeyPairGenerator[] kpg = new KeyPairGenerator[3];
69         try {
70             kpg[0] = KeyPairGenerator
71                     .getInstance(KeyPairGenerator1Test.validAlgName);
72             kpg[1] = KeyPairGenerator.getInstance(
73                     KeyPairGenerator1Test.validAlgName, validProvider);
74             kpg[2] = KeyPairGenerator.getInstance(
75                     KeyPairGenerator1Test.validAlgName, validProviderName);
76             return kpg;
77         } catch (Exception e) {
78             e.printStackTrace();
79             return null;
80         }
81     }
82 
83 
84     /**
85      * Test for <code>generateKeyPair()</code> and <code>genKeyPair()</code>
86      * methods
87      * Assertion: KeyPairGenerator was initialized before the invocation
88      * of these methods
89      */
testGenKeyPair01()90     public void testGenKeyPair01() throws NoSuchAlgorithmException,
91             NoSuchProviderException, IllegalArgumentException {
92         if (!DSASupported) {
93             fail(NotSupportMsg);
94             return;
95         }
96         KeyPairGenerator[] kpg = createKPGen();
97         assertNotNull("KeyPairGenerator objects were not created", kpg);
98         KeyPair kp, kp1;
99         SecureRandom rr = new SecureRandom();
100         for (int i = 0; i < kpg.length; i++) {
101             kpg[i].initialize(512, rr);
102             kp = kpg[i].generateKeyPair();
103             kp1 = kpg[i].genKeyPair();
104             assertFalse("Incorrect private key", kp.getPrivate().equals(
105                     kp1.getPrivate()));
106             assertFalse("Incorrect public key", kp.getPublic().equals(
107                     kp1.getPublic()));
108         }
109     }
110 
111     /**
112      * Test for <code>generateKeyPair()</code> and <code>genKeyPair()</code>
113      * methods
114      * Assertion: these methods are used without previously initialization
115      */
testGenKeyPair02()116     public void testGenKeyPair02() throws NoSuchAlgorithmException,
117             NoSuchProviderException, IllegalArgumentException {
118         if (!DSASupported) {
119             fail(NotSupportMsg);
120             return;
121         }
122         KeyPairGenerator[] kpg = createKPGen();
123         assertNotNull("KeyPairGenerator objects were not created", kpg);
124         KeyPair kp, kp1;
125         for (int i = 0; i < kpg.length; i++) {
126             kp = kpg[i].generateKeyPair();
127             kp1 = kpg[i].genKeyPair();
128             assertFalse("Incorrect private key", kp.getPrivate().equals(
129                 kp1.getPrivate()));
130             assertFalse("Incorrect public key", kp.getPublic().equals(
131                 kp1.getPublic()));
132         }
133     }
134 
135     /**
136      * Test for <code>KeyPairGenerator</code> constructor
137      * Assertion: returns KeyPairGenerator object
138      */
testKeyPairGeneratorConst()139     public void testKeyPairGeneratorConst() {
140         String[] alg = {null, "", "AsDfGh!#$*", "DSA", "RSA"};
141         MykeyPGen kpg;
142 
143         for (int i = 0; i < alg.length; i++) {
144             try {
145                 kpg = new MykeyPGen(alg[i]);
146                 assertNotNull(kpg);
147                 assertTrue(kpg instanceof KeyPairGenerator);
148             } catch (Exception e){
149                 fail("Exception should not be thrown");
150             }
151         }
152     }
153 
154     /**
155      * Additional class to verify KeyPairGenerator constructor
156      */
157     class MykeyPGen extends KeyPairGenerator {
MykeyPGen(String alg)158         public MykeyPGen(String alg) {
159             super(alg);
160         }
161     }
162 }
163