2 * Copyright (C) 2007 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package tests.security.interfaces;
18 import dalvik.annotation.TestLevel;
19 import dalvik.annotation.TestTargetNew;
20 import dalvik.annotation.TestTargetClass;
22 import junit.framework.TestCase;
24 import java.security.KeyPairGenerator;
25 import java.security.interfaces.DSAKey;
26 import java.security.interfaces.DSAParams;
27 import java.security.spec.DSAParameterSpec;
29 @TestTargetClass(DSAKey.class)
30 public class DSAKeyTest extends TestCase {
33 * @tests java.security.interfaces.DSAKey
35 * test covers following use cases
36 * Case 1: check private key
37 * Case 2: check public key
40 level = TestLevel.COMPLETE,
45 public void test_getParams() throws Exception {
46 DSAParams param = new DSAParameterSpec(Util.P, Util.Q, Util.G);
48 KeyPairGenerator gen = KeyPairGenerator.getInstance("DSA");
49 gen.initialize((DSAParameterSpec) param);
52 // Case 1: check private key
53 key = (DSAKey) gen.generateKeyPair().getPrivate();
54 assertDSAParamsEquals(param, key.getParams());
56 // Case 2: check public key
57 key = (DSAKey) gen.generateKeyPair().getPublic();
58 assertDSAParamsEquals(param, key.getParams());
61 private void assertDSAParamsEquals(DSAParams expected, DSAParams actual) {
62 assertEquals("P differ", expected.getP(), actual.getP());
63 assertEquals("Q differ", expected.getQ(), actual.getQ());
64 assertEquals("G differ", expected.getG(), actual.getG());