1/*
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
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
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.
16 */
17
18/**
19* @author Vera Y. Petrashkova
20* @version $Revision$
21*/
22
23package org.apache.harmony.security.tests.java.security;
24
25import java.security.InvalidAlgorithmParameterException;
26import java.security.InvalidParameterException;
27import java.security.KeyPairGenerator;
28import java.security.NoSuchAlgorithmException;
29import java.security.NoSuchProviderException;
30import java.security.Provider;
31import java.security.SecureRandom;
32import java.security.Security;
33import java.security.spec.AlgorithmParameterSpec;
34
35import org.apache.harmony.security.tests.support.MyKeyPairGenerator1;
36import org.apache.harmony.security.tests.support.MyKeyPairGenerator2;
37import org.apache.harmony.security.tests.support.SpiEngUtils;
38
39import junit.framework.TestCase;
40
41/**
42 * Tests for <code>KeyPairGenerator</code> class constructors and methods.
43 *
44 */
45public class KeyPairGenerator2Test extends TestCase {
46    private String KeyPairGeneratorProviderClass = "";
47
48    private static final String KeyPairGeneratorProviderClass1 = "org.apache.harmony.security.tests.support.MyKeyPairGenerator1";
49    private static final String KeyPairGeneratorProviderClass2 = "org.apache.harmony.security.tests.support.MyKeyPairGenerator2";
50    private static final String KeyPairGeneratorProviderClass3 = "org.apache.harmony.security.tests.support.MyKeyPairGenerator3";
51    private static final String KeyPairGeneratorProviderClass4 = "org.apache.harmony.security.tests.support.MyKeyPairGeneratorSpi";
52
53    private static final String defaultAlg = "KPGen";
54
55    private static final String[] invalidValues = SpiEngUtils.invalidValues;
56
57    private static final String[] validValues;
58
59    String post;
60
61    static {
62        validValues = new String[4];
63        validValues[0] = defaultAlg;
64        validValues[1] = defaultAlg.toLowerCase();
65        validValues[2] = "kpGEN";
66        validValues[3] = "kPGEn";
67    }
68
69    Provider mProv;
70    String resAlg;
71
72    /*
73     * @see TestCase#tearDown()
74     */
75    protected void tearDown() throws Exception {
76        super.tearDown();
77        Security.removeProvider(mProv.getName());
78    }
79
80    protected void setProv() {
81        mProv = (new SpiEngUtils()).new MyProvider("MyKPGenProvider".concat(post),
82                "Testing provider", KeyPairGenerator1Test.srvKeyPairGenerator.concat(".")
83                        .concat(defaultAlg.concat(post)),
84                KeyPairGeneratorProviderClass);
85        Security.insertProviderAt(mProv, 1);
86    }
87
88    private void checkResult(KeyPairGenerator keyPairGen, int mode)
89            throws InvalidAlgorithmParameterException {
90        AlgorithmParameterSpec pp = null;
91        switch (mode) {
92        case 1:
93            try {
94                keyPairGen.initialize(pp, new SecureRandom());
95                fail("InvalidAlgorithmParameterException must be thrown");
96            } catch (InvalidAlgorithmParameterException e) {
97            }
98            keyPairGen.initialize(1000, new SecureRandom());
99            try {
100                keyPairGen.initialize(-1024, new SecureRandom());
101                fail("InvalidParameterException must be thrown");
102            } catch (InvalidParameterException e) {
103                assertEquals("Incorrect exception", e.getMessage(),
104                        "Incorrect keysize parameter");
105            }
106            try {
107                keyPairGen.initialize(100, null);
108                fail("InvalidParameterException must be thrown");
109            } catch (InvalidParameterException e) {
110                assertEquals("Incorrect exception", e.getMessage(),
111                        "Incorrect random");
112            }
113            keyPairGen.generateKeyPair();
114            keyPairGen.genKeyPair();
115            break;
116        case 2:
117            try {
118                keyPairGen.initialize(pp, new SecureRandom());
119            } catch (UnsupportedOperationException e) {
120                // js2e does not throw this exception
121            }
122            keyPairGen.initialize(1000, new SecureRandom());
123            try {
124                keyPairGen.initialize(63, new SecureRandom());
125                fail("InvalidParameterException must be thrown");
126            } catch (InvalidParameterException e) {
127            }
128            keyPairGen.initialize(100, null);
129            assertNull("Not null KeyPair", keyPairGen.generateKeyPair());
130            assertNull("Not null KeyPair", keyPairGen.genKeyPair());
131            break;
132        case 3:
133            keyPairGen.initialize(pp, new SecureRandom());
134            keyPairGen.initialize(pp);
135            keyPairGen.initialize(1000, new SecureRandom());
136            keyPairGen.initialize(100);
137
138            assertNotNull("Null KeyPair", keyPairGen.generateKeyPair());
139            assertNotNull("Null KeyPair", keyPairGen.genKeyPair());
140            break;
141        case 4:
142            try {
143                keyPairGen.initialize(pp, null);
144                fail("UnsupportedOperationException must be thrown");
145            } catch (UnsupportedOperationException e) {
146            }
147            keyPairGen.initialize(pp, new SecureRandom());
148            keyPairGen.initialize(101, new SecureRandom());
149            keyPairGen.initialize(10000);
150            try {
151                keyPairGen.initialize(101, null);
152                fail("IllegalArgumentException must be thrown for null random");
153            } catch (IllegalArgumentException e) {
154            }
155            try {
156                keyPairGen.initialize(99, new SecureRandom());
157                fail("InvalidParameterException must be thrown for invalid key");
158            } catch (InvalidParameterException e) {
159            }
160            try {
161                keyPairGen.initialize(99);
162                fail("InvalidParameterException must be thrown for invalid key");
163            } catch (InvalidParameterException e) {
164            }
165            try {
166                keyPairGen.initialize(199, null);
167                fail("IllegalArgumentException must be thrown for null random");
168            } catch (IllegalArgumentException e) {
169            }
170            assertNull("Not null KeyPair", keyPairGen.generateKeyPair());
171            assertNull("Not null KeyPair", keyPairGen.genKeyPair());
172            break;
173        }
174
175    }
176
177    /**
178     * Test for <code>getInstance(String algorithm)</code> method Assertions:
179     * throws NullPointerException when algorithm is null throws
180     * NoSuchAlgorithmException when algorithm is incorrect; returns
181     * KeyPairGenerator object
182     *
183     */
184    private void GetInstance01(int mode) throws NoSuchAlgorithmException,
185            InvalidAlgorithmParameterException {
186        try {
187            KeyPairGenerator.getInstance(null);
188            fail("NullPointerException or KeyStoreException must be thrown");
189        } catch (NoSuchAlgorithmException e) {
190        } catch (NullPointerException e) {
191        }
192        for (int i = 0; i < invalidValues.length; i++) {
193            try {
194                KeyPairGenerator.getInstance(invalidValues[i]);
195                fail("NoSuchAlgorithmException must be thrown (algorithm: "
196                        .concat(invalidValues[i]).concat(")"));
197            } catch (NoSuchAlgorithmException e) {
198            }
199        }
200        KeyPairGenerator kpG;
201        for (int i = 0; i < validValues.length; i++) {
202            String alg = validValues[i].concat(post);
203            kpG = KeyPairGenerator.getInstance(alg);
204            assertEquals("Incorrect algorithm", kpG.getAlgorithm()
205                    .toUpperCase(), (mode <= 2 ? resAlg : alg).toUpperCase());
206            assertEquals("Incorrect provider", kpG.getProvider(), mProv);
207            checkResult(kpG, mode);
208        }
209    }
210
211    /**
212     * Test for <code>getInstance(String algorithm, String provider)</code>
213     * method
214     * Assertions:
215     * throws NullPointerException  when algorithm is null
216     * throws NoSuchAlgorithmException when algorithm is incorrect;
217     * throws IllegalArgumentException when provider is null;
218     * throws NoSuchProviderException when provider is available;
219     * returns
220     * KeyPairGenerator object
221     */
222    public void GetInstance02(int mode) throws NoSuchAlgorithmException,
223            NoSuchProviderException, IllegalArgumentException,
224            InvalidAlgorithmParameterException {
225        try {
226            KeyPairGenerator.getInstance(null, mProv.getName());
227            fail("NullPointerException or KeyStoreException must be thrown");
228        } catch (NoSuchAlgorithmException e) {
229        } catch (NullPointerException e) {
230        }
231        for (int i = 0; i < invalidValues.length; i++) {
232            try {
233                KeyPairGenerator.getInstance(invalidValues[i], mProv.getName());
234                fail("NoSuchAlgorithmException must be thrown (algorithm: "
235                        .concat(invalidValues[i]).concat(")"));
236            } catch (NoSuchAlgorithmException e) {
237            }
238        }
239        String prov = null;
240        for (int i = 0; i < validValues.length; i++) {
241            String alg = validValues[i].concat(post);
242            try {
243                KeyPairGenerator.getInstance(alg, prov);
244                fail("IllegalArgumentException must be thrown when provider is null (algorithm: "
245                        .concat(alg).concat(")"));
246            } catch (IllegalArgumentException e) {
247            }
248        }
249        for (int i = 0; i < validValues.length; i++) {
250            String alg = validValues[i].concat(post);
251            for (int j = 1; j < invalidValues.length; j++) {
252                try {
253                    KeyPairGenerator.getInstance(alg, invalidValues[j]);
254                    fail("NoSuchProviderException must be thrown (algorithm: "
255                            .concat(alg).concat(" provider: ").concat(
256                                    invalidValues[j]).concat(")"));
257                } catch (NoSuchProviderException e) {
258                }
259            }
260        }
261        KeyPairGenerator kpG;
262        for (int i = 0; i < validValues.length; i++) {
263            String alg = validValues[i].concat(post);
264            kpG = KeyPairGenerator.getInstance(alg, mProv.getName());
265            assertEquals("Incorrect algorithm", kpG.getAlgorithm()
266                    .toUpperCase(), (mode <= 2 ? resAlg : alg).toUpperCase());
267            assertEquals("Incorrect provider", kpG.getProvider().getName(),
268                    mProv.getName());
269            checkResult(kpG, mode);
270        }
271    }
272
273    /**
274     * Test for <code>getInstance(String algorithm, Provider provider)</code>
275     * method
276     * Assertions:
277     * throws NullPointerException  when algorithm is null
278     * throws NoSuchAlgorithmException when algorithm is incorrect;
279     * throws IllegalArgumentException when provider is null;
280     * returns KeyPairGenerator object
281     */
282    private void GetInstance03(int mode) throws NoSuchAlgorithmException,
283            IllegalArgumentException, InvalidAlgorithmParameterException {
284        try {
285            KeyPairGenerator.getInstance(null, mProv);
286            fail("NullPointerException or KeyStoreException must be thrown");
287        } catch (NoSuchAlgorithmException e) {
288        } catch (NullPointerException e) {
289        }
290        for (int i = 0; i < invalidValues.length; i++) {
291            try {
292                KeyPairGenerator.getInstance(invalidValues[i], mProv);
293                fail("NoSuchAlgorithmException must be thrown (algorithm: "
294                        .concat(invalidValues[i]).concat(")"));
295            } catch (NoSuchAlgorithmException e) {
296            }
297        }
298        Provider prov = null;
299        for (int i = 0; i < validValues.length; i++) {
300            String alg = validValues[i].concat(post);
301            try {
302                KeyPairGenerator.getInstance(alg, prov);
303                fail("IllegalArgumentException must be thrown when provider is null (algorithm: "
304                        .concat(alg).concat(")"));
305            } catch (IllegalArgumentException e) {
306            }
307        }
308        KeyPairGenerator kpG;
309        for (int i = 0; i < validValues.length; i++) {
310            String alg = validValues[i].concat(post);
311            kpG = KeyPairGenerator.getInstance(alg, mProv);
312            assertEquals("Incorrect algorithm", kpG.getAlgorithm()
313                    .toUpperCase(), (mode <= 2 ? resAlg : alg).toUpperCase());
314            assertEquals("Incorrect provider", kpG.getProvider(), mProv);
315            checkResult(kpG, mode);
316        }
317    }
318
319    public void testGetInstance01() throws NoSuchAlgorithmException,
320            InvalidAlgorithmParameterException {
321        KeyPairGeneratorProviderClass = KeyPairGeneratorProviderClass1;
322        resAlg = MyKeyPairGenerator1.getResAlgorithm();
323        post = "_1";
324        setProv();
325        GetInstance01(1);
326    }
327
328    public void testGetInstance02() throws NoSuchAlgorithmException,
329            NoSuchProviderException, IllegalArgumentException,
330            InvalidAlgorithmParameterException {
331        KeyPairGeneratorProviderClass = KeyPairGeneratorProviderClass1;
332        resAlg = MyKeyPairGenerator1.getResAlgorithm();
333        post = "_1";
334        setProv();
335        GetInstance02(1);
336    }
337
338    public void testGetInstance03() throws NoSuchAlgorithmException,
339            IllegalArgumentException, InvalidAlgorithmParameterException {
340        KeyPairGeneratorProviderClass = KeyPairGeneratorProviderClass1;
341        resAlg = MyKeyPairGenerator1.getResAlgorithm();
342        post = "_1";
343        setProv();
344        GetInstance03(1);
345    }
346
347    public void testGetInstance04() throws NoSuchAlgorithmException,
348            InvalidAlgorithmParameterException {
349        KeyPairGeneratorProviderClass = KeyPairGeneratorProviderClass2;
350        resAlg = MyKeyPairGenerator2.getResAlgorithm();
351        post = "_2";
352        setProv();
353        GetInstance01(2);
354    }
355
356    public void testGetInstance05() throws NoSuchAlgorithmException,
357            NoSuchProviderException, IllegalArgumentException,
358            InvalidAlgorithmParameterException {
359        KeyPairGeneratorProviderClass = KeyPairGeneratorProviderClass2;
360        resAlg = MyKeyPairGenerator2.getResAlgorithm();
361        post = "_2";
362        setProv();
363        GetInstance02(2);
364    }
365
366    public void testGetInstance06() throws NoSuchAlgorithmException,
367            IllegalArgumentException, InvalidAlgorithmParameterException {
368        KeyPairGeneratorProviderClass = KeyPairGeneratorProviderClass2;
369        resAlg = MyKeyPairGenerator2.getResAlgorithm();
370        post = "_2";
371        setProv();
372        GetInstance03(2);
373    }
374
375    public void testGetInstance07() throws NoSuchAlgorithmException,
376            InvalidAlgorithmParameterException {
377        KeyPairGeneratorProviderClass = KeyPairGeneratorProviderClass3;
378        resAlg = "";
379        post = "_3";
380        setProv();
381        GetInstance01(3);
382    }
383
384    public void testGetInstance08() throws NoSuchAlgorithmException,
385            NoSuchProviderException, IllegalArgumentException,
386            InvalidAlgorithmParameterException {
387        KeyPairGeneratorProviderClass = KeyPairGeneratorProviderClass3;
388        resAlg = "";
389        post = "_3";
390        setProv();
391        GetInstance02(3);
392    }
393
394    public void testGetInstance09() throws NoSuchAlgorithmException,
395            IllegalArgumentException, InvalidAlgorithmParameterException {
396        KeyPairGeneratorProviderClass = KeyPairGeneratorProviderClass3;
397        resAlg = "";
398        post = "_3";
399        setProv();
400        GetInstance03(3);
401    }
402
403    public void testGetInstance10() throws NoSuchAlgorithmException,
404            InvalidAlgorithmParameterException {
405        KeyPairGeneratorProviderClass = KeyPairGeneratorProviderClass4;
406        resAlg = "";
407        post = "_4";
408        setProv();
409        GetInstance01(4);
410    }
411
412    public void testGetInstance11() throws NoSuchAlgorithmException,
413            NoSuchProviderException, IllegalArgumentException,
414            InvalidAlgorithmParameterException {
415        KeyPairGeneratorProviderClass = KeyPairGeneratorProviderClass4;
416        resAlg = "";
417        post = "_4";
418        setProv();
419        GetInstance02(4);
420    }
421
422    public void testGetInstance12() throws NoSuchAlgorithmException,
423            IllegalArgumentException, InvalidAlgorithmParameterException {
424        KeyPairGeneratorProviderClass = KeyPairGeneratorProviderClass4;
425        resAlg = "";
426        post = "_4";
427        setProv();
428        GetInstance03(4);
429    }
430}
431