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.AlgorithmParameterGenerator;
024: import java.security.InvalidParameterException;
025: import java.security.Provider;
026: import java.security.SecureRandom;
027:
028: import org.apache.harmony.security.tests.support.SpiEngUtils;
029:
030: import junit.framework.TestCase;
031:
032: /**
033: * Tests for <code>AlgorithmParameterGenerator</code> class constructors and
034: * methods.
035: */
036: public class AlgorithmParameterGenerator_ImplTest extends TestCase {
037:
038: private static String validAlgName = "DSA";
039:
040: public static final String srvAlgorithmParameterGenerator = "AlgorithmParameterGenerator";
041:
042: private static String validProviderName = null;
043:
044: private static Provider validProvider = null;
045:
046: private static boolean DSASupported = false;
047:
048: static {
049: validProvider = SpiEngUtils.isSupport(validAlgName,
050: srvAlgorithmParameterGenerator);
051: DSASupported = (validProvider != null);
052: validProviderName = (DSASupported ? validProvider.getName()
053: : null);
054: }
055:
056: protected AlgorithmParameterGenerator[] createAPGen()
057: throws Exception {
058: if (!DSASupported) {
059: fail(validAlgName + " algorithm is not supported");
060: return null;
061: }
062: AlgorithmParameterGenerator[] apg = new AlgorithmParameterGenerator[3];
063: apg[0] = AlgorithmParameterGenerator.getInstance(validAlgName);
064: apg[1] = AlgorithmParameterGenerator.getInstance(validAlgName,
065: validProvider);
066: apg[2] = AlgorithmParameterGenerator.getInstance(validAlgName,
067: validProviderName);
068: return apg;
069: }
070:
071: /**
072: * Test for <code>init(int size)</code> and
073: * <code>init(int size, SecureRandom random<code> methods
074: * Assertion: throws InvalidParameterException when size is incorrect
075: */
076: public void testAlgorithmParameterGenerator11() throws Exception {
077: if (!DSASupported) {
078: fail(validAlgName + " algorithm is not supported");
079: return;
080: }
081: // Invalid key strengths (strength must be from 512 - 1024 and a multiple of 64)
082: int[] keys = { -10000, -512, -1, 0, 10000 };
083: SecureRandom random = new SecureRandom();
084: AlgorithmParameterGenerator[] apgs = createAPGen();
085: assertNotNull(
086: "AlgorithmParameterGenerator objects were not created",
087: apgs);
088:
089: for (int i = 0; i < apgs.length; i++) {
090:
091: // Test invalid strengths
092: for (int j = 0; j < keys.length; j++) {
093: try {
094: apgs[i].init(keys[j]);
095: fail("Expected an invalid parameter exception for strength "
096: + keys[j]);
097: } catch (InvalidParameterException e) {
098: // expected
099: }
100: try {
101: apgs[i].init(keys[j], random);
102: fail("Expected an invalid parameter exception for strength "
103: + keys[j]);
104: } catch (InvalidParameterException e) {
105: // expected
106: }
107: try {
108: apgs[i].init(keys[j], null);
109: fail("Expected an invalid parameter exception for strength "
110: + keys[j]);
111: } catch (InvalidParameterException e) {
112: // expected
113: }
114: }
115:
116: // Test valid strengths
117: apgs[i].init(512);
118: apgs[i].init(512, random);
119: apgs[i].init(512 + 64);
120: apgs[i].init(512 + 64 + 64, random);
121: apgs[i].init(1024);
122: apgs[i].init(1024, random);
123: }
124: }
125:
126: }
|