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: package org.apache.harmony.security.tests.java.security;
019:
020: import java.math.BigInteger;
021: import java.security.AlgorithmParameterGenerator;
022: import java.security.AlgorithmParameters;
023: import java.security.InvalidAlgorithmParameterException;
024: import java.security.NoSuchProviderException;
025: import java.security.Provider;
026: import java.security.SecureRandom;
027: import java.security.Security;
028: import java.security.spec.DSAParameterSpec;
029:
030: public class AlgorithmParameterGenerator3Test extends
031: junit.framework.TestCase {
032:
033: /**
034: * @tests java.security.AlgorithmParameterGenerator#generateParameters()
035: */
036: public void test_generateParameters() throws Exception {
037:
038: //fail("Takes ages. Problem with SecureRandom and stub math ?");
039:
040: // Test for method java.security.AlgorithmParameters
041: // java.security.AlgorithmParameterGenerator.generateParameters()
042: AlgorithmParameterGenerator gen = AlgorithmParameterGenerator
043: .getInstance("DSA");
044: gen.init(1024);
045:
046: // WARNING - The next line can take MINUTES to run
047: AlgorithmParameters params = gen.generateParameters();
048: assertNotNull("params is null", params);
049: }
050:
051: /**
052: * @tests java.security.AlgorithmParameterGenerator#getAlgorithm()
053: */
054: public void test_getAlgorithm() throws Exception {
055: // Test for method java.lang.String
056: // java.security.AlgorithmParameterGenerator.getAlgorithm()
057: String alg = AlgorithmParameterGenerator.getInstance("DSA")
058: .getAlgorithm();
059: assertEquals("getAlgorithm ok", "DSA", alg);
060: }
061:
062: /**
063: * @tests java.security.AlgorithmParameterGenerator#getInstance(java.lang.String)
064: */
065: public void test_getInstanceLjava_lang_String() throws Exception {
066: // Test for method java.security.AlgorithmParameterGenerator
067: // java.security.AlgorithmParameterGenerator.getInstance(java.lang.String)
068: AlgorithmParameterGenerator.getInstance("DSA");
069: }
070:
071: /**
072: * @tests java.security.AlgorithmParameterGenerator#getInstance(java.lang.String,
073: * java.lang.String)
074: */
075: public void test_getInstanceLjava_lang_StringLjava_lang_String()
076: throws Exception {
077: // Test for method java.security.AlgorithmParameterGenerator
078: // java.security.AlgorithmParameterGenerator.getInstance(java.lang.String,
079: // java.lang.String)
080:
081: // Opting for DSA here as it is pretty widely supported
082: Provider[] provs = Security
083: .getProviders("AlgorithmParameterGenerator.DSA");
084:
085: for (int i = 0; i < provs.length; i++) {
086: AlgorithmParameterGenerator.getInstance("DSA", provs[i]
087: .getName());
088: }// end for
089:
090: // exception case - should throw IllegalArgumentException for null
091: // provider
092: try {
093: AlgorithmParameterGenerator.getInstance("DSA",
094: (String) null);
095: fail("Should have thrown IllegalArgumentException");
096: } catch (IllegalArgumentException e) {
097: // Expected
098: }
099:
100: // exception case - should throw IllegalArgumentException for empty
101: // provider
102: try {
103: AlgorithmParameterGenerator.getInstance("DSA", "");
104: fail("Should have thrown IllegalArgumentException");
105: } catch (IllegalArgumentException e) {
106: // Expected
107: }
108:
109: // exception case - should throw NoSuchProviderException
110: try {
111: AlgorithmParameterGenerator
112: .getInstance("DSA", "IDontExist");
113: fail("Should have thrown NoSuchProviderException");
114: } catch (NoSuchProviderException e) {
115: // Expected
116: }
117: }
118:
119: /**
120: * @tests java.security.AlgorithmParameterGenerator#getProvider()
121: */
122: public void test_getProvider() throws Exception {
123: // Test for method java.security.Provider
124: // java.security.AlgorithmParameterGenerator.getProvider()
125: // checks that no exception is thrown
126: Provider p = AlgorithmParameterGenerator.getInstance("DSA")
127: .getProvider();
128: assertNotNull("provider is null", p);
129: }
130:
131: /**
132: * @tests java.security.AlgorithmParameterGenerator#init(int)
133: */
134: public void test_initI() throws Exception {
135: // Test for method void
136: // java.security.AlgorithmParameterGenerator.init(int)
137: // checks that no exception is thrown
138: AlgorithmParameterGenerator gen = AlgorithmParameterGenerator
139: .getInstance("DSA");
140: gen.init(1024);
141: }
142:
143: /**
144: * @tests java.security.AlgorithmParameterGenerator#init(int,
145: * java.security.SecureRandom)
146: */
147: public void test_initILjava_security_SecureRandom()
148: throws Exception {
149: // Test for method void
150: // java.security.AlgorithmParameterGenerator.init(int,
151: // java.security.SecureRandom)
152: // checks that no exception is thrown
153: AlgorithmParameterGenerator gen = AlgorithmParameterGenerator
154: .getInstance("DSA");
155: gen.init(1024, new SecureRandom());
156: }
157:
158: /**
159: * @tests java.security.AlgorithmParameterGenerator#init(java.security.spec.AlgorithmParameterSpec)
160: */
161: public void test_initLjava_security_spec_AlgorithmParameterSpec()
162: throws Exception {
163: // Test for method void
164: // java.security.AlgorithmParameterGenerator.init(java.security.spec.AlgorithmParameterSpec)
165: // checks that InvalidAlgorithmParameterException is thrown
166: DSAParameterSpec spec = new DSAParameterSpec(BigInteger.ONE,
167: BigInteger.ONE, BigInteger.ONE);
168: AlgorithmParameterGenerator gen = AlgorithmParameterGenerator
169: .getInstance("DSA");
170: try {
171: gen.init(spec);
172: fail("No expected InvalidAlgorithmParameterException");
173: } catch (InvalidAlgorithmParameterException e) {
174: //expected
175: }
176: }
177:
178: /**
179: * @tests java.security.AlgorithmParameterGenerator#init(java.security.spec.AlgorithmParameterSpec,
180: * java.security.SecureRandom)
181: */
182: public void test_initLjava_security_spec_AlgorithmParameterSpecLjava_security_SecureRandom()
183: throws Exception {
184: // Test for method void
185: // java.security.AlgorithmParameterGenerator.init(java.security.spec.AlgorithmParameterSpec,
186: // java.security.SecureRandom)
187: // checks that InvalidAlgorithmParameterException is thrown
188: DSAParameterSpec spec = new DSAParameterSpec(BigInteger.ONE,
189: BigInteger.ONE, BigInteger.ONE);
190: AlgorithmParameterGenerator gen = AlgorithmParameterGenerator
191: .getInstance("DSA");
192: try {
193: gen.init(spec, new SecureRandom());
194: fail("No expected InvalidAlgorithmParameterException");
195: } catch (InvalidAlgorithmParameterException e) {
196: //expected
197: }
198: }
199: }
|