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.
18 package org.apache.harmony.security.tests.java.security;
20 import dalvik.annotation.TestTargetClass;
21 import dalvik.annotation.TestTargets;
22 import dalvik.annotation.TestLevel;
23 import dalvik.annotation.TestTargetNew;
25 import java.security.KeyPairGenerator;
26 import java.security.Provider;
27 import java.security.SecureRandom;
28 import java.security.Security;
29 import java.security.spec.DSAParameterSpec;
30 import java.security.interfaces.DSAPublicKey;
31 import java.security.interfaces.DSAParams;
33 @TestTargetClass(KeyPairGenerator.class)
34 public class KeyPairGenerator4Test extends junit.framework.TestCase {
37 * @tests java.security.KeyPairGenerator#genKeyPair()
40 level = TestLevel.COMPLETE,
42 method = "genKeyPair",
45 public void test_genKeyPair() throws Exception {
46 KeyPairGenerator gen = KeyPairGenerator.getInstance("DSA");
48 assertNotNull("KeyPair is null", gen.genKeyPair());
52 * @tests java.security.KeyPairGenerator#getAlgorithm()
55 level = TestLevel.COMPLETE,
57 method = "getAlgorithm",
60 public void test_getAlgorithm() throws Exception {
61 String alg = KeyPairGenerator.getInstance("DSA").getAlgorithm();
62 assertEquals("getAlgorithm returned unexpected value", "DSA", alg);
66 * @tests java.security.KeyPairGenerator#getInstance(java.lang.String)
69 level = TestLevel.PARTIAL,
70 notes = "Verification of other string parameters and exception cases missed",
71 method = "getInstance",
72 args = {java.lang.String.class}
74 public void test_getInstanceLjava_lang_String() throws Exception {
75 KeyPairGenerator.getInstance("DSA");
79 * @tests java.security.KeyPairGenerator#getInstance(java.lang.String,
83 level = TestLevel.PARTIAL,
84 notes = "NoSuchAlgorithmException, NoSuchProviderException checking missed",
85 method = "getInstance",
86 args = {java.lang.String.class, java.lang.String.class}
88 public void test_getInstanceLjava_lang_StringLjava_lang_String()
91 // Test1: Test with named provider
92 Provider[] providers = Security.getProviders("KeyPairGenerator.DSA");
94 for (int i = 0; i < providers.length; i++) {
95 KeyPairGenerator.getInstance("DSA", providers[i].getName());
98 // Test2: Test with empty provider name - should raise
99 // IllegalArgumentException
101 KeyPairGenerator.getInstance("DSA", "");
102 fail("Should have thrown IllegalArgumentException");
103 } catch (IllegalArgumentException e) {
109 * @tests java.security.KeyPairGenerator#getProvider()
112 level = TestLevel.COMPLETE,
114 method = "getProvider",
117 public void test_getProvider() throws Exception {
118 Provider p = KeyPairGenerator.getInstance("DSA").getProvider();
119 assertNotNull("provider is null", p);
123 * @tests java.security.KeyPairGenerator#initialize(int)
126 level = TestLevel.PARTIAL,
127 notes = "InvalidParameterException checking missed",
128 method = "initialize",
131 public void test_initializeI() throws Exception {
132 KeyPairGenerator keyPair = KeyPairGenerator.getInstance("DSA");
133 keyPair.initialize(1024);
137 * @tests java.security.KeyPairGenerator#initialize(int,
138 * java.security.SecureRandom)
141 level = TestLevel.PARTIAL,
142 notes = "InvalidParameterException checking missed",
143 method = "initialize",
144 args = {int.class, java.security.SecureRandom.class}
146 public void test_initializeILjava_security_SecureRandom() throws Exception {
147 KeyPairGenerator keyPair = KeyPairGenerator.getInstance("DSA");
148 keyPair.initialize(1024, new SecureRandom());
153 * @tests java.security.KeyPairGenerator#initialize(java.security.spec.AlgorithmParameterSpec)
156 level = TestLevel.PARTIAL,
157 notes = "InvalidAlgorithmParameterException checking missed",
158 method = "initialize",
159 args = {java.security.spec.AlgorithmParameterSpec.class}
161 public void test_initializeLjava_security_spec_AlgorithmParameterSpec()
164 KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("DSA");
165 keyPairGenerator.initialize(1024);
166 DSAPublicKey key = (DSAPublicKey) keyPairGenerator.genKeyPair()
168 DSAParams params = key.getParams();
170 KeyPairGenerator keyPair = KeyPairGenerator.getInstance("DSA");
171 keyPair.initialize(new DSAParameterSpec(params.getP(), params.getQ(),
176 * @tests java.security.KeyPairGenerator#initialize(java.security.spec.AlgorithmParameterSpec,
177 * java.security.SecureRandom)
180 level = TestLevel.PARTIAL,
181 notes = "InvalidAlgorithmParameterException checking missed",
182 method = "initialize",
183 args = {java.security.spec.AlgorithmParameterSpec.class, java.security.SecureRandom.class}
185 public void test_initializeLjava_security_spec_AlgorithmParameterSpecLjava_security_SecureRandom()
188 KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("DSA");
189 keyPairGenerator.initialize(1024);
190 DSAPublicKey key = (DSAPublicKey) keyPairGenerator.genKeyPair()
192 DSAParams params = key.getParams();
194 KeyPairGenerator keyPair = KeyPairGenerator.getInstance("DSA");
195 keyPair.initialize(new DSAParameterSpec(params.getP(), params.getQ(),
196 params.getG()), new SecureRandom());