OSDN Git Service

original
[gb-231r1-is01/Gingerbread_2.3.3_r1_IS01.git] / libcore / luni / src / test / java / tests / security / interfaces / DSAPublicKeyTest.java
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 package tests.security.interfaces;
17 import dalvik.annotation.BrokenTest;
18 import dalvik.annotation.TestLevel;
19 import dalvik.annotation.TestTargetNew;
20 import dalvik.annotation.TestTargetClass;
21
22 import junit.framework.TestCase;
23
24 import java.security.KeyPair;
25 import java.security.KeyPairGenerator;
26 import java.security.SecureRandom;
27 import java.security.interfaces.DSAPrivateKey;
28 import java.security.interfaces.DSAPublicKey;
29 import java.security.spec.DSAParameterSpec;
30
31 @TestTargetClass(DSAPublicKey.class)
32 public class DSAPublicKeyTest extends TestCase {
33
34     /**
35      * @tests java.security.interfaces.DSAPublicKey
36      * #getY()
37      * test covers following use cases
38      *   Case 1: check with predefined p, q, g, x
39      *   Case 2: check with random p, q, g, x. It takes some time (up to
40      *           minute)
41      */
42     @TestTargetNew(
43         level = TestLevel.COMPLETE,
44         notes = "",
45         method = "getY",
46         args = {}
47     )
48     public void test_getY() throws Exception {
49         KeyPairGenerator keyGen = null;
50         KeyPair keys = null;
51         DSAPrivateKey priv = null;
52         DSAPublicKey publ = null;
53
54         // Case 1: check with predefined p, q, g, x
55         keyGen = KeyPairGenerator.getInstance("DSA");
56         keyGen.initialize(new DSAParameterSpec(Util.P, Util.Q, Util.G),
57                 new SecureRandom(new MySecureRandomSpi(), null) {
58                 });
59         keys = keyGen.generateKeyPair();
60         priv = (DSAPrivateKey) keys.getPrivate();
61         publ = (DSAPublicKey) keys.getPublic();
62         assertNotNull("Invalid Y value", publ.getY());
63
64         // Case 2: check with random p, q, g, x. It takes some time (up to
65         // minute)
66         keyGen = KeyPairGenerator.getInstance("DSA");
67         keys = keyGen.generateKeyPair();
68         priv = (DSAPrivateKey) keys.getPrivate();
69         publ = (DSAPublicKey) keys.getPublic();
70         assertNotNull("Invalid Y value", publ.getY());
71     }
72 }