1/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17package tests.security.interfaces;
18
19import junit.framework.TestCase;
20
21import java.security.KeyPairGenerator;
22import java.security.interfaces.DSAKey;
23import java.security.interfaces.DSAParams;
24import java.security.spec.DSAParameterSpec;
25
26public class DSAKeyTest extends TestCase {
27
28    /**
29     * java.security.interfaces.DSAKey
30     * #getParams()
31     * test covers following use cases
32     *   Case 1: check private key
33     *   Case 2: check public key
34     */
35    public void test_getParams() throws Exception {
36        DSAParams param = new DSAParameterSpec(Util.P, Util.Q, Util.G);
37
38        KeyPairGenerator gen = KeyPairGenerator.getInstance("DSA");
39        gen.initialize((DSAParameterSpec) param);
40        DSAKey key = null;
41
42        // Case 1: check private key
43        key = (DSAKey) gen.generateKeyPair().getPrivate();
44        assertDSAParamsEquals(param, key.getParams());
45
46        // Case 2: check public key
47        key = (DSAKey) gen.generateKeyPair().getPublic();
48        assertDSAParamsEquals(param, key.getParams());
49    }
50
51    private void assertDSAParamsEquals(DSAParams expected, DSAParams actual) {
52        assertEquals("P differ", expected.getP(), actual.getP());
53        assertEquals("Q differ", expected.getQ(), actual.getQ());
54        assertEquals("G differ", expected.getG(), actual.getG());
55    }
56}
57