001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /**
019: * @author Vera Y. Petrashkova
020: * @version $Revision$
021: */package org.apache.harmony.security.tests.java.security;
022:
023: import java.security.*;
024: import java.security.spec.AlgorithmParameterSpec;
025:
026: import org.apache.harmony.security.tests.support.MyAlgorithmParameterGeneratorSpi;
027: import org.apache.harmony.security.tests.support.SpiEngUtils;
028:
029: import junit.framework.TestCase;
030:
031: /**
032: * Tests for <code>AlgorithmParameterGenerator</code> class constructors and
033: * methods.
034: *
035: */
036:
037: public class AlgorithmParameterGenerator1Test extends TestCase {
038: /**
039: * Constructor for AlgorithmParameterGeneratorTests.
040: *
041: * @param arg0
042: */
043: public AlgorithmParameterGenerator1Test(String arg0) {
044: super (arg0);
045: }
046:
047: private static String[] invalidValues = SpiEngUtils.invalidValues;
048: private static String validAlgName = "DSA";
049: private static String[] algs = { "DSA", "dsa", "Dsa", "DsA", "dsA" };
050:
051: public static final String srvAlgorithmParameterGenerator = "AlgorithmParameterGenerator";
052:
053: private static String validProviderName = null;
054:
055: private static Provider validProvider = null;
056:
057: private static boolean DSASupported = false;
058:
059: static {
060: validProvider = SpiEngUtils.isSupport(validAlgName,
061: srvAlgorithmParameterGenerator);
062: DSASupported = (validProvider != null);
063: validProviderName = (DSASupported ? validProvider.getName()
064: : null);
065: }
066:
067: protected AlgorithmParameterGenerator[] createAPGen() {
068: if (!DSASupported) {
069: fail(validAlgName + " algorithm is not supported");
070: return null;
071: }
072: AlgorithmParameterGenerator[] apg = new AlgorithmParameterGenerator[3];
073: try {
074: apg[0] = AlgorithmParameterGenerator
075: .getInstance(validAlgName);
076: apg[1] = AlgorithmParameterGenerator.getInstance(
077: validAlgName, validProvider);
078: apg[2] = AlgorithmParameterGenerator.getInstance(
079: validAlgName, validProviderName);
080: return apg;
081: } catch (Exception e) {
082: e.printStackTrace();
083: return null;
084: }
085: }
086:
087: /**
088: * Test for <code>getInstance(String algorithm)</code> method
089: * Assertion:
090: * throws NullPointerException must be thrown is null
091: * throws NoSuchAlgorithmException must be thrown if algorithm is not available
092: *
093: */
094: public void testAlgorithmParameterGenerator01()
095: throws NoSuchAlgorithmException {
096: try {
097: AlgorithmParameterGenerator.getInstance(null);
098: fail("NullPointerException or NoSuchAlgorithmException should be thrown");
099: } catch (NullPointerException e) {
100: } catch (NoSuchAlgorithmException e) {
101: }
102: for (int i = 0; i < invalidValues.length; i++) {
103: try {
104: AlgorithmParameterGenerator
105: .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(),
127: algs[i]);
128: }
129: }
130:
131: /**
132: * Test for <code>getInstance(String algorithm, String provider)</code>
133: * method
134: * Assertion:
135: * throws IllegalArgumentException if provider is null or empty
136: */
137: public void testAlgorithmParameterGenerator03()
138: throws NoSuchAlgorithmException, NoSuchProviderException {
139: if (!DSASupported) {
140: fail(validAlgName + " algorithm is not supported");
141: return;
142: }
143: String provider = null;
144: for (int i = 0; i < algs.length; i++) {
145: try {
146: AlgorithmParameterGenerator.getInstance(algs[i],
147: provider);
148: fail("IllegalArgumentException must be thrown when provider is null");
149: } catch (IllegalArgumentException e) {
150: }
151: try {
152: AlgorithmParameterGenerator.getInstance(algs[i], "");
153: fail("IllegalArgumentException must be thrown when provider is empty");
154: } catch (IllegalArgumentException e) {
155: }
156: }
157: }
158:
159: /**
160: * Test for <code>getInstance(String algorithm, String provider)</code>
161: * method
162: * Assertion: throws NoSuchProviderException if provider is not
163: * available
164: */
165: public void testAlgorithmParameterGenerator04()
166: throws NoSuchAlgorithmException {
167: if (!DSASupported) {
168: fail(validAlgName + " algorithm is not supported");
169: return;
170: }
171: for (int i = 0; i < algs.length; i++) {
172: for (int j = 1; j < invalidValues.length; j++) {
173: try {
174: AlgorithmParameterGenerator.getInstance(algs[i],
175: invalidValues[j]);
176: fail("NoSuchProviderException must be thrown (provider: "
177: .concat(invalidValues[j]));
178: } catch (NoSuchProviderException e) {
179: }
180: }
181: }
182: }
183:
184: /**
185: * Test for <code>getInstance(String algorithm, String provider)</code>
186: * method
187: * Assertion:
188: * throws NullPointerException must be thrown is null
189: * throws NoSuchAlgorithmException must be thrown if algorithm is not available
190: */
191: public void testAlgorithmParameterGenerator05()
192: throws NoSuchProviderException {
193: if (!DSASupported) {
194: fail(validAlgName + " algorithm is not supported");
195: return;
196: }
197: try {
198: AlgorithmParameterGenerator.getInstance(null,
199: validProviderName);
200: fail("NullPointerException or NoSuchAlgorithmException should be thrown");
201: } catch (NullPointerException e) {
202: } catch (NoSuchAlgorithmException e) {
203: }
204: for (int i = 0; i < invalidValues.length; i++) {
205: try {
206: AlgorithmParameterGenerator.getInstance(
207: invalidValues[i], validProviderName);
208: fail("NoSuchAlgorithmException must be thrown when (algorithm: "
209: .concat(invalidValues[i].concat(")")));
210: } catch (NoSuchAlgorithmException e) {
211: }
212: }
213: }
214:
215: /**
216: * Test for <code>getInstance(String algorithm, String provider)</code>
217: * method
218: * Assertion: return AlgorithmParameterGenerator
219: */
220: public void testAlgorithmParameterGenerator06()
221: throws NoSuchAlgorithmException, NoSuchProviderException {
222: if (!DSASupported) {
223: fail(validAlgName + " algorithm is not supported");
224: return;
225: }
226: AlgorithmParameterGenerator apg;
227: for (int i = 0; i < algs.length; i++) {
228: apg = AlgorithmParameterGenerator.getInstance(algs[i],
229: validProviderName);
230: assertEquals("Incorrect algorithm", algs[i], apg
231: .getAlgorithm());
232: assertEquals("Incorrect provider", apg.getProvider()
233: .getName(), validProviderName);
234: }
235: }
236:
237: /**
238: * Test for <code>getInstance(String algorithm, Provider provider)</code>
239: * method
240: * Assertion: throws IllegalArgumentException when provider is null
241: */
242: public void testAlgorithmParameterGenerator07()
243: throws NoSuchAlgorithmException {
244: if (!DSASupported) {
245: fail(validAlgName + " algorithm is not supported");
246: return;
247: }
248: Provider provider = null;
249: for (int i = 0; i < algs.length; i++) {
250: try {
251: AlgorithmParameterGenerator.getInstance(algs[i],
252: provider);
253: fail("IllegalArgumentException must be thrown when provider is null");
254: } catch (IllegalArgumentException e) {
255: }
256: }
257: }
258:
259: /**
260: * Test for <code>getInstance(String algorithm, Provider provider)</code>
261: * method
262: * Assertion:
263: * throws NullPointerException must be thrown is null
264: * throws NoSuchAlgorithmException must be thrown if algorithm is not available
265: */
266: public void testAlgorithmParameterGenerator08() {
267: if (!DSASupported) {
268: fail(validAlgName + " algorithm is not supported");
269: return;
270: }
271: try {
272: AlgorithmParameterGenerator
273: .getInstance(null, validProvider);
274: fail("NullPointerException or NoSuchAlgorithmException should be thrown");
275: } catch (NullPointerException e) {
276: } catch (NoSuchAlgorithmException e) {
277: }
278: for (int i = 0; i < invalidValues.length; i++) {
279: try {
280: AlgorithmParameterGenerator.getInstance(
281: invalidValues[i], validProvider);
282: fail("NoSuchAlgorithmException must be thrown (algorithm: "
283: .concat(invalidValues[i]).concat(")"));
284: } catch (NoSuchAlgorithmException e) {
285: }
286: }
287: }
288:
289: /**
290: * Test for <code>getInstance(String algorithm, Provider provider)</code>
291: * method
292: * Assertion: returns AlgorithmParameterGenerator object
293: */
294: public void testAlgorithmParameterGenerator09()
295: throws NoSuchAlgorithmException {
296: if (!DSASupported) {
297: fail(validAlgName + " algorithm is not supported");
298: return;
299: }
300: AlgorithmParameterGenerator apg;
301: for (int i = 0; i < algs.length; i++) {
302: apg = AlgorithmParameterGenerator.getInstance(algs[i],
303: validProvider);
304: assertEquals("Incorrect algorithm", apg.getAlgorithm(),
305: algs[i]);
306: assertEquals("Incorrect provider", apg.getProvider(),
307: validProvider);
308: }
309: }
310:
311: /**
312: * Test for <code>generateParameters()</code> method
313: * Assertion: returns AlgorithmParameters object
314: */
315: public void testAlgorithmParameterGenerator10()
316: throws NoSuchAlgorithmException {
317: if (!DSASupported) {
318: fail(validAlgName + " algorithm is not supported");
319: return;
320: }
321: AlgorithmParameterGenerator apg = AlgorithmParameterGenerator
322: .getInstance(validAlgName);
323: apg.init(512);
324: AlgorithmParameters ap = apg.generateParameters();
325: assertEquals("Incorrect algorithm", ap.getAlgorithm()
326: .toUpperCase(), apg.getAlgorithm().toUpperCase());
327: }
328:
329: /**
330: * Test for <code>init(AlgorithmParameterSpec param)</code> and
331: * <code>init(AlgorithmParameterSpec param, SecureRandom random<code>
332: * methods
333: * Assertion: throws InvalidAlgorithmParameterException when param is null
334: */
335: public void testAlgorithmParameterGenerator12() {
336: if (!DSASupported) {
337: fail(validAlgName + " algorithm is not supported");
338: return;
339: }
340: SecureRandom random = new SecureRandom();
341: AlgorithmParameterSpec aps = null;
342: AlgorithmParameterGenerator[] apgs = createAPGen();
343: assertNotNull(
344: "AlgorithmParameterGenerator objects were not created",
345: apgs);
346: for (int i = 0; i < apgs.length; i++) {
347: try {
348: apgs[i].init(aps, random);
349: fail("InvalidAlgorithmParameterException must be throws when param is null");
350: } catch (InvalidAlgorithmParameterException e) {
351: }
352: }
353: }
354:
355: /**
356: * Test for <code>AlgorithmParameterGenerator</code> constructor
357: * Assertion: returns AlgorithmParameterGenerator object
358: */
359: public void testAlgorithmParameterGeneratorConstr()
360: throws NoSuchAlgorithmException {
361: if (!DSASupported) {
362: fail(validAlgName + " algorithm is not supported");
363: return;
364: }
365: AlgorithmParameterGeneratorSpi spi = new MyAlgorithmParameterGeneratorSpi();
366: AlgorithmParameterGenerator apg = new myAlgPG(spi,
367: validProvider, validAlgName);
368: assertEquals("Incorrect algorithm", apg.getAlgorithm(),
369: validAlgName);
370: assertEquals("Incorrect provider", apg.getProvider(),
371: validProvider);
372: try {
373: apg.init(-10, null);
374: fail("IllegalArgumentException must be thrown");
375: } catch (IllegalArgumentException e) {
376: }
377:
378: apg = new myAlgPG(null, null, null);
379: assertNull("Incorrect algorithm", apg.getAlgorithm());
380: assertNull("Incorrect provider", apg.getProvider());
381: try {
382: apg.init(-10, null);
383: fail("NullPointerException must be thrown");
384: } catch (NullPointerException e) {
385: }
386: }
387:
388: public static void main(String args[]) {
389: junit.textui.TestRunner
390: .run(AlgorithmParameterGenerator1Test.class);
391: }
392: }
393:
394: /**
395: * Additional class to verify AlgorithmParameterGenerator constructor
396: */
397: class myAlgPG extends AlgorithmParameterGenerator {
398: public myAlgPG(AlgorithmParameterGeneratorSpi spi, Provider prov,
399: String alg) {
400: super(spi, prov, alg);
401: }
402: }
|