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;
23
24import java.security.*;
25import java.security.spec.AlgorithmParameterSpec;
26
27import org.apache.harmony.security.tests.support.MyAlgorithmParameterGeneratorSpi;
28import org.apache.harmony.security.tests.support.SpiEngUtils;
29
30import junit.framework.TestCase;
31
32
33/**
34 * Tests for <code>AlgorithmParameterGenerator</code> class constructors and
35 * methods.
36 */
37
38public class AlgorithmParameterGenerator1Test extends TestCase {
39    /**
40     * Constructor for AlgorithmParameterGeneratorTests.
41     *
42     * @param arg0
43     */
44    public AlgorithmParameterGenerator1Test(String arg0) {
45        super(arg0);
46    }
47
48    private static String[] invalidValues = SpiEngUtils.invalidValues;
49    private static String validAlgName = "DSA";
50    private static String[] algs = {
51            "DSA", "dsa", "Dsa", "DsA", "dsA" };
52
53    public static final String srvAlgorithmParameterGenerator = "AlgorithmParameterGenerator";
54
55
56    private static String validProviderName = null;
57
58    private static Provider validProvider = null;
59
60    private static boolean DSASupported = false;
61
62    static {
63        validProvider = SpiEngUtils.isSupport(
64                validAlgName,
65                srvAlgorithmParameterGenerator);
66        DSASupported = (validProvider != null);
67        validProviderName = (DSASupported ? validProvider.getName() : null);
68    }
69
70    protected AlgorithmParameterGenerator[] createAPGen() {
71        if (!DSASupported) {
72            fail(validAlgName + " algorithm is not supported");
73            return null;
74        }
75        AlgorithmParameterGenerator[] apg = new AlgorithmParameterGenerator[3];
76        try {
77            apg[0] = AlgorithmParameterGenerator.getInstance(validAlgName);
78            apg[1] = AlgorithmParameterGenerator.getInstance(validAlgName,
79                    validProvider);
80            apg[2] = AlgorithmParameterGenerator.getInstance(validAlgName,
81                    validProviderName);
82            return apg;
83        } catch (Exception e) {
84            e.printStackTrace();
85            return null;
86        }
87    }
88
89    /**
90     * Test for <code>getInstance(String algorithm)</code> method
91     * Assertion:
92     * throws NullPointerException must be thrown is null
93     * throws NoSuchAlgorithmException must be thrown if algorithm is not available
94     */
95    public void testAlgorithmParameterGenerator01()
96            throws NoSuchAlgorithmException {
97        try {
98            AlgorithmParameterGenerator.getInstance(null);
99            fail("NullPointerException or NoSuchAlgorithmException should be thrown");
100        } catch (NullPointerException e) {
101        } catch (NoSuchAlgorithmException e) {
102        }
103        for (int i = 0; i < invalidValues.length; i++) {
104            try {
105                AlgorithmParameterGenerator.getInstance(invalidValues[i]);
106                fail("NoSuchAlgorithmException should be thrown");
107            } catch (NoSuchAlgorithmException e) {
108            }
109        }
110    }
111
112    /**
113     * Test for <code>getInstance(String algorithm)</code> method
114     * Assertion: returns AlgorithmParameterGenerator instance
115     * when algorithm is DSA
116     */
117    public void testAlgorithmParameterGenerator02()
118            throws NoSuchAlgorithmException {
119        if (!DSASupported) {
120            fail(validAlgName + " algorithm is not supported");
121            return;
122        }
123        AlgorithmParameterGenerator apg;
124        for (int i = 0; i < algs.length; i++) {
125            apg = AlgorithmParameterGenerator.getInstance(algs[i]);
126            assertEquals("Incorrect algorithm", apg.getAlgorithm(), algs[i]);
127        }
128    }
129
130    /**
131     * Test for <code>getInstance(String algorithm, String provider)</code>
132     * method
133     * Assertion:
134     * throws IllegalArgumentException if provider is null or empty
135     */
136    public void testAlgorithmParameterGenerator03()
137            throws NoSuchAlgorithmException, NoSuchProviderException {
138        if (!DSASupported) {
139            fail(validAlgName + " algorithm is not supported");
140            return;
141        }
142        String provider = null;
143        for (int i = 0; i < algs.length; i++) {
144            try {
145                AlgorithmParameterGenerator.getInstance(algs[i], provider);
146                fail("IllegalArgumentException must be thrown when provider is null");
147            } catch (IllegalArgumentException e) {
148            }
149            try {
150                AlgorithmParameterGenerator.getInstance(algs[i], "");
151                fail("IllegalArgumentException must be thrown when provider is empty");
152            } catch (IllegalArgumentException e) {
153            }
154        }
155    }
156
157    /**
158     * Test for <code>getInstance(String algorithm, String provider)</code>
159     * method
160     * Assertion: throws NoSuchProviderException if provider is not
161     * available
162     */
163    public void testAlgorithmParameterGenerator04()
164            throws NoSuchAlgorithmException {
165        if (!DSASupported) {
166            fail(validAlgName + " algorithm is not supported");
167            return;
168        }
169        for (int i = 0; i < algs.length; i++) {
170            for (int j = 1; j < invalidValues.length; j++) {
171                try {
172                    AlgorithmParameterGenerator.getInstance(algs[i],
173                            invalidValues[j]);
174                    fail("NoSuchProviderException must be thrown (provider: "
175                            .concat(invalidValues[j]));
176                } catch (NoSuchProviderException e) {
177                }
178            }
179        }
180    }
181
182    /**
183     * Test for <code>getInstance(String algorithm, String provider)</code>
184     * method
185     * Assertion:
186     * throws NullPointerException must be thrown is null
187     * throws NoSuchAlgorithmException must be thrown if algorithm is not available
188     */
189    public void testAlgorithmParameterGenerator05()
190            throws NoSuchProviderException {
191        if (!DSASupported) {
192            fail(validAlgName + " algorithm is not supported");
193            return;
194        }
195        try {
196            AlgorithmParameterGenerator.getInstance(null, validProviderName);
197            fail("NullPointerException or NoSuchAlgorithmException should be thrown");
198        } catch (NullPointerException e) {
199        } catch (NoSuchAlgorithmException e) {
200        }
201        for (int i = 0; i < invalidValues.length; i++) {
202            try {
203                AlgorithmParameterGenerator.getInstance(invalidValues[i],
204                        validProviderName);
205                fail("NoSuchAlgorithmException must be thrown when (algorithm: "
206                        .concat(invalidValues[i].concat(")")));
207            } catch (NoSuchAlgorithmException e) {
208            }
209        }
210    }
211
212    /**
213     * Test for <code>getInstance(String algorithm, String provider)</code>
214     * method
215     * Assertion: return AlgorithmParameterGenerator
216     */
217    public void testAlgorithmParameterGenerator06()
218            throws NoSuchAlgorithmException, NoSuchProviderException {
219        if (!DSASupported) {
220            fail(validAlgName + " algorithm is not supported");
221            return;
222        }
223        AlgorithmParameterGenerator apg;
224        for (int i = 0; i < algs.length; i++) {
225            apg = AlgorithmParameterGenerator.getInstance(algs[i],
226                    validProviderName);
227            assertEquals("Incorrect algorithm", algs[i], apg.getAlgorithm());
228            assertEquals("Incorrect provider", apg.getProvider().getName(),
229                    validProviderName);
230        }
231    }
232
233    /**
234     * Test for <code>getInstance(String algorithm, Provider provider)</code>
235     * method
236     * Assertion: throws IllegalArgumentException when provider is null
237     */
238    public void testAlgorithmParameterGenerator07()
239            throws NoSuchAlgorithmException {
240        if (!DSASupported) {
241            fail(validAlgName + " algorithm is not supported");
242            return;
243        }
244        Provider provider = null;
245        for (int i = 0; i < algs.length; i++) {
246            try {
247                AlgorithmParameterGenerator.getInstance(algs[i], provider);
248                fail("IllegalArgumentException must be thrown when provider is null");
249            } catch (IllegalArgumentException e) {
250            }
251        }
252    }
253
254    /**
255     * Test for <code>getInstance(String algorithm, Provider provider)</code>
256     * method
257     * Assertion:
258     * throws NullPointerException must be thrown is null
259     * throws NoSuchAlgorithmException must be thrown if algorithm is not available
260     */
261    public void testAlgorithmParameterGenerator08() {
262        if (!DSASupported) {
263            fail(validAlgName + " algorithm is not supported");
264            return;
265        }
266        try {
267            AlgorithmParameterGenerator.getInstance(null, validProvider);
268            fail("NullPointerException or NoSuchAlgorithmException should be thrown");
269        } catch (NullPointerException e) {
270        } catch (NoSuchAlgorithmException e) {
271        }
272        for (int i = 0; i < invalidValues.length; i++) {
273            try {
274                AlgorithmParameterGenerator.getInstance(invalidValues[i],
275                        validProvider);
276                fail("NoSuchAlgorithmException must be thrown (algorithm: "
277                        .concat(invalidValues[i]).concat(")"));
278            } catch (NoSuchAlgorithmException e) {
279            }
280        }
281    }
282
283    /**
284     * Test for <code>getInstance(String algorithm, Provider provider)</code>
285     * method
286     * Assertion: returns AlgorithmParameterGenerator object
287     */
288    public void testAlgorithmParameterGenerator09()
289            throws NoSuchAlgorithmException {
290        if (!DSASupported) {
291            fail(validAlgName + " algorithm is not supported");
292            return;
293        }
294        AlgorithmParameterGenerator apg;
295        for (int i = 0; i < algs.length; i++) {
296            apg = AlgorithmParameterGenerator.getInstance(algs[i],
297                    validProvider);
298            assertEquals("Incorrect algorithm", apg.getAlgorithm(), algs[i]);
299            assertEquals("Incorrect provider", apg.getProvider(), validProvider);
300        }
301    }
302
303    /**
304     * Test for <code>generateParameters()</code> method
305     * Assertion: returns AlgorithmParameters object
306     */
307    public void testAlgorithmParameterGenerator10()
308            throws NoSuchAlgorithmException {
309        if (!DSASupported) {
310            fail(validAlgName + " algorithm is not supported");
311            return;
312        }
313        AlgorithmParameterGenerator apg = AlgorithmParameterGenerator
314                .getInstance(validAlgName);
315        apg.init(512);
316        AlgorithmParameters ap = apg.generateParameters();
317        assertEquals("Incorrect algorithm", ap.getAlgorithm().toUpperCase(),
318                apg.getAlgorithm().toUpperCase());
319    }
320
321    /**
322     * Test for <code>init(AlgorithmParameterSpec param)</code> and
323     * <code>init(AlgorithmParameterSpec param, SecureRandom random<code>
324     * methods
325     * Assertion: throws InvalidAlgorithmParameterException when param is null
326     */
327    public void testAlgorithmParameterGenerator12() {
328        if (!DSASupported) {
329            fail(validAlgName + " algorithm is not supported");
330            return;
331        }
332        SecureRandom random = new SecureRandom();
333        AlgorithmParameterSpec aps = null;
334        AlgorithmParameterGenerator[] apgs = createAPGen();
335        assertNotNull("AlgorithmParameterGenerator objects were not created",
336                apgs);
337        for (int i = 0; i < apgs.length; i++) {
338            try {
339                apgs[i].init(aps, random);
340                fail("InvalidAlgorithmParameterException must be throws when param is null");
341            } catch (InvalidAlgorithmParameterException e) {
342            }
343        }
344    }
345
346    /**
347     * Test for <code>AlgorithmParameterGenerator</code> constructor
348     * Assertion: returns AlgorithmParameterGenerator object
349     */
350    public void testAlgorithmParameterGeneratorConstr() throws NoSuchAlgorithmException {
351        if (!DSASupported) {
352            fail(validAlgName + " algorithm is not supported");
353            return;
354        }
355        AlgorithmParameterGeneratorSpi spi = new MyAlgorithmParameterGeneratorSpi();
356        AlgorithmParameterGenerator apg =
357                new myAlgPG(spi, validProvider, validAlgName);
358        assertEquals("Incorrect algorithm", apg.getAlgorithm(), validAlgName);
359        assertEquals("Incorrect provider", apg.getProvider(), validProvider);
360        try {
361            apg.init(-10, null);
362            fail("IllegalArgumentException must be thrown");
363        } catch (IllegalArgumentException e) {
364        }
365
366        apg = new myAlgPG(null, null, null);
367        assertNull("Incorrect algorithm", apg.getAlgorithm());
368        assertNull("Incorrect provider", apg.getProvider());
369        try {
370            apg.init(-10, null);
371            fail("NullPointerException must be thrown");
372        } catch (NullPointerException e) {
373        }
374    }
375
376}
377
378/**
379 * Additional class to verify AlgorithmParameterGenerator constructor
380 */
381class myAlgPG extends AlgorithmParameterGenerator {
382    public myAlgPG(AlgorithmParameterGeneratorSpi spi, Provider prov, String alg) {
383        super(spi, prov, alg);
384    }
385}
386