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