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.AlgorithmParameterGenerator;
26import java.security.AlgorithmParameters;
27import java.security.InvalidAlgorithmParameterException;
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.SpiEngUtils;
36
37import junit.framework.TestCase;
38
39/**
40 * Tests for <code>AlgorithmParameterGenerator</code> class constructors and
41 * methods.
42 */
43public class AlgorithmParameterGenerator2Test extends TestCase {
44
45    private static final String AlgorithmParameterGeneratorProviderClass = "org.apache.harmony.security.tests.support.MyAlgorithmParameterGeneratorSpi";
46
47    private static final String defaultAlg = "APG";
48
49    private static final String[] invalidValues = SpiEngUtils.invalidValues;
50
51    private static final String[] validValues;
52
53    static {
54        validValues = new String[4];
55        validValues[0] = defaultAlg;
56        validValues[1] = defaultAlg.toLowerCase();
57        validValues[2] = "apG";
58        validValues[3] = "ApG";
59    }
60
61    Provider mProv;
62
63    protected void setUp() throws Exception {
64        super.setUp();
65        mProv = (new SpiEngUtils()).new MyProvider("MyAPGProvider", "Testing provider",
66                AlgorithmParameterGenerator1Test.srvAlgorithmParameterGenerator.concat(".").concat(defaultAlg),
67                AlgorithmParameterGeneratorProviderClass);
68        Security.insertProviderAt(mProv, 1);
69    }
70
71    /*
72     * @see TestCase#tearDown()
73     */
74    protected void tearDown() throws Exception {
75        super.tearDown();
76        Security.removeProvider(mProv.getName());
77    }
78
79    private void checkResult(AlgorithmParameterGenerator algParGen)
80            throws InvalidAlgorithmParameterException {
81        AlgorithmParameters param = algParGen.generateParameters();
82        assertNull("Not null parameters", param);
83
84        AlgorithmParameterSpec pp = null;
85        algParGen.init(pp, new SecureRandom());
86        algParGen.init(pp);
87        try {
88            algParGen.init(pp, null);
89            fail("IllegalArgumentException must be thrown");
90        } catch (IllegalArgumentException e) {
91        }
92        pp = new tmpAlgorithmParameterSpec("Proba");
93        algParGen.init(pp, new SecureRandom());
94        algParGen.init(pp);
95
96        algParGen.init(0, null);
97        algParGen.init(0, new SecureRandom());
98
99        try {
100            algParGen.init(-10, null);
101            fail("IllegalArgumentException must be thrown");
102        } catch (IllegalArgumentException e) {
103        }
104        try {
105            algParGen.init(-10, new SecureRandom());
106            fail("IllegalArgumentException must be thrown");
107        } catch (IllegalArgumentException e) {
108        }
109    }
110    /**
111     * Test for <code>getInstance(String algorithm)</code> method
112     * Assertions:
113     * throws NullPointerException must be thrown is null
114     * throws NoSuchAlgorithmException must be thrown if algorithm is not available
115     * returns AlgorithmParameterGenerator object
116     */
117    public void testGetInstance01() throws NoSuchAlgorithmException,
118            InvalidAlgorithmParameterException {
119        try {
120            AlgorithmParameterGenerator.getInstance(null);
121            fail("NullPointerException or NoSuchAlgorithmException should be thrown");
122        } catch (NullPointerException e) {
123        } catch (NoSuchAlgorithmException e) {
124        }
125        for (int i = 0; i < invalidValues.length; i++) {
126            try {
127                AlgorithmParameterGenerator.getInstance(invalidValues[i]);
128                fail("NoSuchAlgorithmException must be thrown (algorithm: "
129                        .concat(invalidValues[i]).concat(")"));
130            } catch (NoSuchAlgorithmException e) {
131            }
132        }
133        AlgorithmParameterGenerator apG;
134        for (int i = 0; i < validValues.length; i++) {
135            apG = AlgorithmParameterGenerator.getInstance(validValues[i]);
136            assertEquals("Incorrect algorithm", apG.getAlgorithm(),
137                    validValues[i]);
138            assertEquals("Incorrect provider", apG.getProvider(), mProv);
139            checkResult(apG);
140        }
141    }
142
143    /**
144     * Test for <code>getInstance(String algorithm, String provider)</code>
145     * method
146     * Assertions:
147     * throws NullPointerException must be thrown is null
148     * throws NoSuchAlgorithmException must be thrown if algorithm is not available
149     * throws IllegalArgumentException when provider is null;
150     * throws NoSuchProviderException when provider is available;
151     * returns AlgorithmParameterGenerator object
152     */
153    public void testGetInstance02() throws NoSuchAlgorithmException,
154            NoSuchProviderException, IllegalArgumentException,
155            InvalidAlgorithmParameterException {
156        try {
157            AlgorithmParameterGenerator.getInstance(null, mProv.getName());
158            fail("NullPointerException or NoSuchAlgorithmException should be thrown");
159        } catch (NullPointerException e) {
160        } catch (NoSuchAlgorithmException e) {
161        }
162        for (int i = 0; i < invalidValues.length; i++) {
163            try {
164                AlgorithmParameterGenerator.getInstance(invalidValues[i], mProv
165                        .getName());
166                fail("NoSuchAlgorithmException must be thrown (algorithm: "
167                        .concat(invalidValues[i]).concat(")"));
168            } catch (NoSuchAlgorithmException e) {
169            }
170        }
171        String prov = null;
172        for (int i = 0; i < validValues.length; i++) {
173            try {
174                AlgorithmParameterGenerator.getInstance(validValues[i], prov);
175                fail("IllegalArgumentException must be thrown when provider is null (algorithm: "
176                        .concat(invalidValues[i]).concat(")"));
177            } catch (IllegalArgumentException e) {
178            }
179        }
180        for (int i = 0; i < validValues.length; i++) {
181            for (int j = 1; j < invalidValues.length; j++) {
182                try {
183                    AlgorithmParameterGenerator.getInstance(validValues[i],
184                            invalidValues[j]);
185                    fail("NoSuchProviderException must be thrown (algorithm: "
186                            .concat(invalidValues[i]).concat(" provider: ")
187                            .concat(invalidValues[j]).concat(")"));
188                } catch (NoSuchProviderException e) {
189                }
190            }
191        }
192        AlgorithmParameterGenerator apG;
193        for (int i = 0; i < validValues.length; i++) {
194            apG = AlgorithmParameterGenerator.getInstance(validValues[i], mProv
195                    .getName());
196            assertEquals("Incorrect algorithm", apG.getAlgorithm(),
197                    validValues[i]);
198            assertEquals("Incorrect provider", apG.getProvider().getName(),
199                    mProv.getName());
200            checkResult(apG);
201        }
202    }
203
204    /**
205     * Test for <code>getInstance(String algorithm, Provider provider)</code>
206     * method
207     * Assertions:
208     * throws NullPointerException must be thrown is null
209     * throws NoSuchAlgorithmException must be thrown if algorithm is not available
210     * throws IllegalArgumentException when provider is null;
211     * returns AlgorithmParameterGenerator object
212     */
213    public void testGetInstance03() throws NoSuchAlgorithmException,
214            IllegalArgumentException,
215            InvalidAlgorithmParameterException {
216        try {
217            AlgorithmParameterGenerator.getInstance(null, mProv);
218            fail("NullPointerException or NoSuchAlgorithmException should be thrown");
219        } catch (NullPointerException e) {
220        } catch (NoSuchAlgorithmException e) {
221        }
222        for (int i = 0; i < invalidValues.length; i++) {
223            try {
224                AlgorithmParameterGenerator.getInstance(invalidValues[i], mProv);
225                fail("NoSuchAlgorithmException must be thrown (algorithm: "
226                        .concat(invalidValues[i]).concat(")"));
227            } catch (NoSuchAlgorithmException e) {
228            }
229        }
230        Provider prov = null;
231        for (int i = 0; i < validValues.length; i++) {
232            try {
233                AlgorithmParameterGenerator.getInstance(validValues[i], prov);
234                fail("IllegalArgumentException must be thrown when provider is null (algorithm: "
235                        .concat(invalidValues[i]).concat(")"));
236            } catch (IllegalArgumentException e) {
237            }
238        }
239        AlgorithmParameterGenerator apG;
240        for (int i = 0; i < validValues.length; i++) {
241            apG = AlgorithmParameterGenerator.getInstance(validValues[i], mProv);
242            assertEquals("Incorrect algorithm", apG.getAlgorithm(),
243                    validValues[i]);
244            assertEquals("Incorrect provider", apG.getProvider(), mProv);
245            checkResult(apG);
246        }
247    }
248    /**
249     * Additional class for init(...) methods verification
250     */
251    class tmpAlgorithmParameterSpec implements AlgorithmParameterSpec {
252        private final String type;
253        public tmpAlgorithmParameterSpec(String type) {
254            this.type = type;
255        }
256        public String getType() {
257            return type;
258        }
259    }
260}
261