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 
17 package tests.security.interfaces;
18 
19 import junit.framework.TestCase;
20 
21 import java.math.BigInteger;
22 import java.security.interfaces.DSAKeyPairGenerator;
23 import java.security.interfaces.DSAParams;
24 import java.security.SecureRandom;
25 import java.security.spec.DSAParameterSpec;
26 import java.security.InvalidParameterException;
27 
28 import org.apache.harmony.security.tests.support.interfaces.DSAKeyPairGeneratorImpl;
29 
30 public class DSAKeyPairGeneratorTest extends TestCase {
31 
32     private final BigInteger p = new BigInteger("4");
33     private final BigInteger q = BigInteger.TEN;
34     private final BigInteger g = BigInteger.ZERO;
35 
36 
37     class MyDSA extends DSAKeyPairGeneratorImpl {
MyDSA(DSAParams dsaParams)38         public MyDSA(DSAParams dsaParams) {
39             super(dsaParams);
40         }
41     }
42 
43     /**
44      * java.security.interfaces.DSAKeyPairGenerator
45      * #initialize(DSAParams params, SecureRandom random)
46      */
test_DSAKeyPairGenerator01()47     public void test_DSAKeyPairGenerator01() {
48         DSAParams dsaParams = new DSAParameterSpec(p, q, g);
49         SecureRandom random = null;
50         MyDSA dsa = new MyDSA(dsaParams);
51         try {
52             random = SecureRandom.getInstance("SHA1PRNG");
53         } catch (Exception e) {
54             fail("Unexpected exception for SecureRandom: " + e);
55         }
56 
57         try {
58             dsa.initialize(dsaParams, random);
59         } catch (Exception e) {
60             fail("Unexpected exception: " + e);
61         }
62 
63         try {
64             dsa.initialize(dsaParams, null);
65             fail("InvalidParameterException was not thrown");
66         } catch (InvalidParameterException ipe) {
67             //expected
68         } catch (Exception e) {
69             fail(e + " was thrown instead of InvalidParameterException");
70         }
71         try {
72             dsa.initialize(null, random);
73             fail("InvalidParameterException was not thrown");
74         } catch (InvalidParameterException ipe) {
75             //expected
76         } catch (Exception e) {
77             fail(e + " was thrown instead of InvalidParameterException");
78         }
79     }
80 
81     /**
82      * java.security.interfaces.DSAKeyPairGenerator
83      * #initialize(int modlen, boolean genParams, SecureRandom randomm)
84      */
test_DSAKeyPairGenerator02()85     public void test_DSAKeyPairGenerator02() {
86         int[] invalidLen = {-1, 0, 511, 513, 650, 1023, 1025};
87         DSAParams dsaParams = new DSAParameterSpec(p, q, g);
88         SecureRandom random = null;
89         MyDSA dsa = new MyDSA(null);
90         try {
91             random = SecureRandom.getInstance("SHA1PRNG");
92         } catch (Exception e) {
93             fail("Unexpected exception for SecureRandom: " + e);
94         }
95 
96         //exception case
97         try {
98             dsa.initialize(520, false, random);
99             fail("InvalidParameterException was not thrown");
100         } catch (InvalidParameterException ipe) {
101             String str = ipe.getMessage();
102             if (!str.equals("there are not precomputed parameters")) {
103                 fail("Incorrect exception's message: " + str);
104             }
105         } catch (Exception e) {
106             fail(e + " was thrown instead of InvalidParameterException");
107         }
108 
109         //exception case
110         for (int i = 0; i < invalidLen.length; i++) {
111             try {
112                 dsa.initialize(invalidLen[i], true, random);
113                 fail("InvalidParameterException was not thrown");
114             } catch (InvalidParameterException ipe) {
115                 String str = ipe.getMessage();
116                 if (!str.equals("Incorrect modlen")) {
117                     fail("Incorrect exception's message: " + str);
118                 }
119             } catch (Exception e) {
120                 fail(e + " was thrown instead of InvalidParameterException");
121             }
122         }
123 
124         //positive case
125         dsa = new MyDSA(dsaParams);
126         try {
127             dsa.initialize(520, true, random);
128         } catch (Exception e) {
129             fail(e + " was thrown for subcase 1");
130         }
131 
132         //positive case
133         try {
134             dsa.initialize(520, false, random);
135         } catch (Exception e) {
136             fail(e + " was thrown for subcase 1");
137         }
138     }
139 }
140