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.NoSuchAlgorithmException;
025: import java.security.NoSuchProviderException;
026: import java.security.Provider;
027: import java.security.Security;
028: import java.security.spec.AlgorithmParameterSpec;
029:
030: import org.apache.harmony.security.tests.support.SpiEngUtils;
031:
032: import junit.framework.TestCase;
033:
034: /**
035: * Tests for <code>AlgorithmParameterGenerator</code> class constructors and
036: * methods.
037: */
038:
039: public class AlgorithmParameterGenerator2Test extends TestCase {
040:
041: private static final String AlgorithmParameterGeneratorProviderClass = "org.apache.harmony.security.tests.support.MyAlgorithmParameterGeneratorSpi";
042:
043: private static final String defaultAlg = "APG";
044:
045: private static final String[] invalidValues = SpiEngUtils.invalidValues;
046:
047: private static final String[] validValues;
048:
049: static {
050: validValues = new String[4];
051: validValues[0] = defaultAlg;
052: validValues[1] = defaultAlg.toLowerCase();
053: validValues[2] = "apG";
054: validValues[3] = "ApG";
055: }
056:
057: Provider mProv;
058:
059: protected void setUp() throws Exception {
060: super .setUp();
061: mProv = (new SpiEngUtils()).new MyProvider(
062: "MyAPGProvider",
063: "Testing provider",
064: AlgorithmParameterGenerator1Test.srvAlgorithmParameterGenerator
065: .concat(".").concat(defaultAlg),
066: AlgorithmParameterGeneratorProviderClass);
067: Security.insertProviderAt(mProv, 1);
068: }
069:
070: /*
071: * @see TestCase#tearDown()
072: */
073: protected void tearDown() throws Exception {
074: super .tearDown();
075: Security.removeProvider(mProv.getName());
076: }
077:
078: /**
079: * Constructor for SecurityManagerFactoryTest2.
080: *
081: * @param arg0
082: */
083: public AlgorithmParameterGenerator2Test(String arg0) {
084: super (arg0);
085: }
086:
087: private void checkResult(AlgorithmParameterGenerator algParGen)
088: throws InvalidAlgorithmParameterException {
089: AlgorithmParameters param = algParGen.generateParameters();
090: assertNull("Not null parameters", param);
091:
092: AlgorithmParameterSpec pp = null;
093: algParGen.init(pp, new SecureRandom());
094: algParGen.init(pp);
095: try {
096: algParGen.init(pp, null);
097: fail("IllegalArgumentException must be thrown");
098: } catch (IllegalArgumentException e) {
099: }
100: pp = new tmpAlgorithmParameterSpec("Proba");
101: algParGen.init(pp, new SecureRandom());
102: algParGen.init(pp);
103:
104: algParGen.init(0, null);
105: algParGen.init(0, new SecureRandom());
106:
107: try {
108: algParGen.init(-10, null);
109: fail("IllegalArgumentException must be thrown");
110: } catch (IllegalArgumentException e) {
111: }
112: try {
113: algParGen.init(-10, new SecureRandom());
114: fail("IllegalArgumentException must be thrown");
115: } catch (IllegalArgumentException e) {
116: }
117: }
118:
119: /**
120: * Test for <code>getInstance(String algorithm)</code> method
121: * Assertions:
122: * throws NullPointerException must be thrown is null
123: * throws NoSuchAlgorithmException must be thrown if algorithm is not available
124: * returns AlgorithmParameterGenerator object
125: */
126: public void testGetInstance01() throws NoSuchAlgorithmException,
127: InvalidAlgorithmParameterException {
128: try {
129: AlgorithmParameterGenerator.getInstance(null);
130: fail("NullPointerException or NoSuchAlgorithmException should be thrown");
131: } catch (NullPointerException e) {
132: } catch (NoSuchAlgorithmException e) {
133: }
134: for (int i = 0; i < invalidValues.length; i++) {
135: try {
136: AlgorithmParameterGenerator
137: .getInstance(invalidValues[i]);
138: fail("NoSuchAlgorithmException must be thrown (algorithm: "
139: .concat(invalidValues[i]).concat(")"));
140: } catch (NoSuchAlgorithmException e) {
141: }
142: }
143: AlgorithmParameterGenerator apG;
144: for (int i = 0; i < validValues.length; i++) {
145: apG = AlgorithmParameterGenerator
146: .getInstance(validValues[i]);
147: assertEquals("Incorrect algorithm", apG.getAlgorithm(),
148: validValues[i]);
149: assertEquals("Incorrect provider", apG.getProvider(), mProv);
150: checkResult(apG);
151: }
152: }
153:
154: /**
155: * Test for <code>getInstance(String algorithm, String provider)</code>
156: * method
157: * Assertions:
158: * throws NullPointerException must be thrown is null
159: * throws NoSuchAlgorithmException must be thrown if algorithm is not available
160: * throws IllegalArgumentException when provider is null;
161: * throws NoSuchProviderException when provider is available;
162: * returns AlgorithmParameterGenerator object
163: */
164: public void testGetInstance02() throws NoSuchAlgorithmException,
165: NoSuchProviderException, IllegalArgumentException,
166: InvalidAlgorithmParameterException {
167: try {
168: AlgorithmParameterGenerator.getInstance(null, mProv
169: .getName());
170: fail("NullPointerException or NoSuchAlgorithmException should be thrown");
171: } catch (NullPointerException e) {
172: } catch (NoSuchAlgorithmException e) {
173: }
174: for (int i = 0; i < invalidValues.length; i++) {
175: try {
176: AlgorithmParameterGenerator.getInstance(
177: invalidValues[i], mProv.getName());
178: fail("NoSuchAlgorithmException must be thrown (algorithm: "
179: .concat(invalidValues[i]).concat(")"));
180: } catch (NoSuchAlgorithmException e) {
181: }
182: }
183: String prov = null;
184: for (int i = 0; i < validValues.length; i++) {
185: try {
186: AlgorithmParameterGenerator.getInstance(validValues[i],
187: prov);
188: fail("IllegalArgumentException must be thrown when provider is null (algorithm: "
189: .concat(invalidValues[i]).concat(")"));
190: } catch (IllegalArgumentException e) {
191: }
192: }
193: for (int i = 0; i < validValues.length; i++) {
194: for (int j = 1; j < invalidValues.length; j++) {
195: try {
196: AlgorithmParameterGenerator.getInstance(
197: validValues[i], invalidValues[j]);
198: fail("NoSuchProviderException must be thrown (algorithm: "
199: .concat(invalidValues[i]).concat(
200: " provider: ").concat(
201: invalidValues[j]).concat(")"));
202: } catch (NoSuchProviderException e) {
203: }
204: }
205: }
206: AlgorithmParameterGenerator apG;
207: for (int i = 0; i < validValues.length; i++) {
208: apG = AlgorithmParameterGenerator.getInstance(
209: validValues[i], mProv.getName());
210: assertEquals("Incorrect algorithm", apG.getAlgorithm(),
211: validValues[i]);
212: assertEquals("Incorrect provider", apG.getProvider()
213: .getName(), mProv.getName());
214: checkResult(apG);
215: }
216: }
217:
218: /**
219: * Test for <code>getInstance(String algorithm, Provider provider)</code>
220: * method
221: * Assertions:
222: * throws NullPointerException must be thrown is null
223: * throws NoSuchAlgorithmException must be thrown if algorithm is not available
224: * throws IllegalArgumentException when provider is null;
225: * returns AlgorithmParameterGenerator object
226: */
227: public void testGetInstance03() throws NoSuchAlgorithmException,
228: IllegalArgumentException,
229: InvalidAlgorithmParameterException {
230: try {
231: AlgorithmParameterGenerator.getInstance(null, mProv);
232: fail("NullPointerException or NoSuchAlgorithmException should be thrown");
233: } catch (NullPointerException e) {
234: } catch (NoSuchAlgorithmException e) {
235: }
236: for (int i = 0; i < invalidValues.length; i++) {
237: try {
238: AlgorithmParameterGenerator.getInstance(
239: invalidValues[i], mProv);
240: fail("NoSuchAlgorithmException must be thrown (algorithm: "
241: .concat(invalidValues[i]).concat(")"));
242: } catch (NoSuchAlgorithmException e) {
243: }
244: }
245: Provider prov = null;
246: for (int i = 0; i < validValues.length; i++) {
247: try {
248: AlgorithmParameterGenerator.getInstance(validValues[i],
249: prov);
250: fail("IllegalArgumentException must be thrown when provider is null (algorithm: "
251: .concat(invalidValues[i]).concat(")"));
252: } catch (IllegalArgumentException e) {
253: }
254: }
255: AlgorithmParameterGenerator apG;
256: for (int i = 0; i < validValues.length; i++) {
257: apG = AlgorithmParameterGenerator.getInstance(
258: validValues[i], mProv);
259: assertEquals("Incorrect algorithm", apG.getAlgorithm(),
260: validValues[i]);
261: assertEquals("Incorrect provider", apG.getProvider(), mProv);
262: checkResult(apG);
263: }
264: }
265:
266: /**
267: * Additional class for init(...) methods verification
268: */
269: class tmpAlgorithmParameterSpec implements AlgorithmParameterSpec {
270: private final String type;
271:
272: public tmpAlgorithmParameterSpec(String type) {
273: this .type = type;
274: }
275:
276: public String getType() {
277: return type;
278: }
279: }
280: }
|