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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * @author Vera Y. Petrashkova
23 package org.apache.harmony.security.tests.java.security;
25 import dalvik.annotation.TestTargetClass;
26 import dalvik.annotation.TestTargets;
27 import dalvik.annotation.TestLevel;
28 import dalvik.annotation.TestTargetNew;
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;
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;
43 import junit.framework.TestCase;
44 @TestTargetClass(KeyPairGenerator.class)
46 * Tests for KeyPairGenerator class
50 public class KeyPairGenerator3Test extends TestCase {
52 private static String validProviderName = null;
54 public static Provider validProvider = null;
56 private static boolean DSASupported = false;
58 private static String NotSupportMsg = KeyPairGenerator1Test.NotSupportMsg;
61 validProvider = SpiEngUtils.isSupport(
62 KeyPairGenerator1Test.validAlgName,
63 KeyPairGenerator1Test.srvKeyPairGenerator);
64 DSASupported = (validProvider != null);
65 validProviderName = (DSASupported ? validProvider.getName() : null);
68 protected KeyPairGenerator[] createKPGen() {
70 fail(KeyPairGenerator1Test.validAlgName
71 + " algorithm is not supported");
74 KeyPairGenerator[] kpg = new KeyPairGenerator[3];
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);
83 } catch (Exception e) {
91 * Test for <code>generateKeyPair()</code> and <code>genKeyPair()</code>
93 * Assertion: KeyPairGenerator was initialized before the invocation
98 level = TestLevel.PARTIAL_COMPLETE,
100 method = "generateKeyPair",
104 level = TestLevel.PARTIAL_COMPLETE,
106 method = "genKeyPair",
110 public void testGenKeyPair01() throws NoSuchAlgorithmException,
111 NoSuchProviderException, IllegalArgumentException {
116 KeyPairGenerator[] kpg = createKPGen();
117 assertNotNull("KeyPairGenerator objects were not created", kpg);
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(
126 assertFalse("Incorrect public key", kp.getPublic().equals(
132 * Test for <code>generateKeyPair()</code> and <code>genKeyPair()</code>
134 * Assertion: these methods are used without previously initialization
138 level = TestLevel.PARTIAL_COMPLETE,
140 method = "generateKeyPair",
144 level = TestLevel.PARTIAL_COMPLETE,
146 method = "genKeyPair",
150 public void testGenKeyPair02() throws NoSuchAlgorithmException,
151 NoSuchProviderException, IllegalArgumentException {
156 KeyPairGenerator[] kpg = createKPGen();
157 assertNotNull("KeyPairGenerator objects were not created", kpg);
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(
164 assertFalse("Incorrect public key", kp.getPublic().equals(
170 * Test for <code>KeyPairGenerator</code> constructor
171 * Assertion: returns KeyPairGenerator object
174 level = TestLevel.COMPLETE,
176 method = "KeyPairGenerator",
177 args = {java.lang.String.class}
179 public void testKeyPairGeneratorConst() {
180 String[] alg = {null, "", "AsDfGh!#$*", "DSA", "RSA"};
183 for (int i = 0; i < alg.length; i++) {
185 kpg = new MykeyPGen(alg[i]);
187 assertTrue(kpg instanceof KeyPairGenerator);
188 } catch (Exception e){
189 fail("Exception should not be thrown");
195 * Additional class to verify KeyPairGenerator constructor
197 class MykeyPGen extends KeyPairGenerator {
198 public MykeyPGen(String alg) {