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