OSDN Git Service

original
[gb-231r1-is01/Gingerbread_2.3.3_r1_IS01.git] / libcore / luni / src / test / java / org / apache / harmony / security / tests / java / security / KeyPairGenerator3Test.java
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 dalvik.annotation.TestTargetClass;
26 import dalvik.annotation.TestTargets;
27 import dalvik.annotation.TestLevel;
28 import dalvik.annotation.TestTargetNew;
29
30 import java.security.AlgorithmParameters;
31 import java.security.AlgorithmParametersSpi;
32 import java.security.KeyPair;
33 import java.security.KeyPairGenerator;
34 import java.security.NoSuchAlgorithmException;
35 import java.security.NoSuchProviderException;
36 import java.security.Provider;
37 import java.security.SecureRandom;
38
39 import org.apache.harmony.security.tests.java.security.AlgorithmParametersTest.MyAlgorithmParameters;
40 import org.apache.harmony.security.tests.java.security.AlgorithmParametersTest.myAlgP;
41 import org.apache.harmony.security.tests.support.SpiEngUtils;
42
43 import junit.framework.TestCase;
44 @TestTargetClass(KeyPairGenerator.class)
45 /**
46  * Tests for KeyPairGenerator class
47  *
48  */
49
50 public class KeyPairGenerator3Test extends TestCase {
51
52     private static String validProviderName = null;
53
54     public static Provider validProvider = null;
55
56     private static boolean DSASupported = false;
57
58     private static String NotSupportMsg = KeyPairGenerator1Test.NotSupportMsg;
59
60     static {
61         validProvider = SpiEngUtils.isSupport(
62                 KeyPairGenerator1Test.validAlgName,
63                 KeyPairGenerator1Test.srvKeyPairGenerator);
64         DSASupported = (validProvider != null);
65         validProviderName = (DSASupported ? validProvider.getName() : null);
66     }
67
68     protected KeyPairGenerator[] createKPGen() {
69         if (!DSASupported) {
70             fail(KeyPairGenerator1Test.validAlgName
71                     + " algorithm is not supported");
72             return null;
73         }
74         KeyPairGenerator[] kpg = new KeyPairGenerator[3];
75         try {
76             kpg[0] = KeyPairGenerator
77                     .getInstance(KeyPairGenerator1Test.validAlgName);
78             kpg[1] = KeyPairGenerator.getInstance(
79                     KeyPairGenerator1Test.validAlgName, validProvider);
80             kpg[2] = KeyPairGenerator.getInstance(
81                     KeyPairGenerator1Test.validAlgName, validProviderName);
82             return kpg;
83         } catch (Exception e) {
84             e.printStackTrace();
85             return null;
86         }
87     }
88
89
90     /**
91      * Test for <code>generateKeyPair()</code> and <code>genKeyPair()</code>
92      * methods
93      * Assertion: KeyPairGenerator was initialized before the invocation
94      * of these methods
95      */
96     @TestTargets({
97         @TestTargetNew(
98             level = TestLevel.PARTIAL_COMPLETE,
99             notes = "",
100             method = "generateKeyPair",
101             args = {}
102         ),
103         @TestTargetNew(
104             level = TestLevel.PARTIAL_COMPLETE,
105             notes = "",
106             method = "genKeyPair",
107             args = {}
108         )
109     })
110     public void testGenKeyPair01() throws NoSuchAlgorithmException,
111             NoSuchProviderException, IllegalArgumentException {
112         if (!DSASupported) {
113             fail(NotSupportMsg);
114             return;
115         }
116         KeyPairGenerator[] kpg = createKPGen();
117         assertNotNull("KeyPairGenerator objects were not created", kpg);
118         KeyPair kp, kp1;
119         SecureRandom rr = new SecureRandom();
120         for (int i = 0; i < kpg.length; i++) {
121             kpg[i].initialize(512, rr);
122             kp = kpg[i].generateKeyPair();
123             kp1 = kpg[i].genKeyPair();
124             assertFalse("Incorrect private key", kp.getPrivate().equals(
125                     kp1.getPrivate()));
126             assertFalse("Incorrect public key", kp.getPublic().equals(
127                     kp1.getPublic()));
128         }
129     }
130
131     /**
132      * Test for <code>generateKeyPair()</code> and <code>genKeyPair()</code>
133      * methods
134      * Assertion: these methods are used without previously initialization
135      */
136     @TestTargets({
137         @TestTargetNew(
138             level = TestLevel.PARTIAL_COMPLETE,
139             notes = "",
140             method = "generateKeyPair",
141             args = {}
142         ),
143         @TestTargetNew(
144             level = TestLevel.PARTIAL_COMPLETE,
145             notes = "",
146             method = "genKeyPair",
147             args = {}
148         )
149     })
150     public void testGenKeyPair02() throws NoSuchAlgorithmException,
151             NoSuchProviderException, IllegalArgumentException {
152         if (!DSASupported) {
153             fail(NotSupportMsg);
154             return;
155         }
156         KeyPairGenerator[] kpg = createKPGen();
157         assertNotNull("KeyPairGenerator objects were not created", kpg);
158         KeyPair kp, kp1;
159         for (int i = 0; i < kpg.length; i++) {
160             kp = kpg[i].generateKeyPair();
161             kp1 = kpg[i].genKeyPair();
162             assertFalse("Incorrect private key", kp.getPrivate().equals(
163                 kp1.getPrivate()));
164             assertFalse("Incorrect public key", kp.getPublic().equals(
165                 kp1.getPublic()));
166         }
167     }
168
169     /**
170      * Test for <code>KeyPairGenerator</code> constructor
171      * Assertion: returns KeyPairGenerator object
172      */
173     @TestTargetNew(
174         level = TestLevel.COMPLETE,
175         notes = "",
176         method = "KeyPairGenerator",
177         args = {java.lang.String.class}
178     )
179     public void testKeyPairGeneratorConst() {
180         String[] alg = {null, "", "AsDfGh!#$*", "DSA", "RSA"};
181         MykeyPGen kpg;
182
183         for (int i = 0; i < alg.length; i++) {
184             try {
185                 kpg = new MykeyPGen(alg[i]);
186                 assertNotNull(kpg);
187                 assertTrue(kpg instanceof KeyPairGenerator);
188             } catch (Exception e){
189                 fail("Exception should not be thrown");
190             }
191         }
192     }
193
194     /**
195      * Additional class to verify KeyPairGenerator constructor
196      */
197     class MykeyPGen extends KeyPairGenerator {
198         public MykeyPGen(String alg) {
199             super(alg);
200         }
201     }
202 }