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; 23 24import java.security.AlgorithmParameterGenerator; 25import java.security.InvalidParameterException; 26import java.security.Provider; 27import java.security.SecureRandom; 28 29import org.apache.harmony.security.tests.support.SpiEngUtils; 30 31import junit.framework.TestCase; 32 33/** 34 * Tests for <code>AlgorithmParameterGenerator</code> class constructors and 35 * methods. 36 */ 37public class AlgorithmParameterGenerator_ImplTest extends TestCase { 38 39 private static String validAlgName = "DSA"; 40 41 public static final String srvAlgorithmParameterGenerator = "AlgorithmParameterGenerator"; 42 43 44 private static String validProviderName = null; 45 46 private static Provider validProvider = null; 47 48 private static boolean DSASupported = false; 49 50 static { 51 validProvider = SpiEngUtils.isSupport( 52 validAlgName, 53 srvAlgorithmParameterGenerator); 54 DSASupported = (validProvider != null); 55 validProviderName = (DSASupported ? validProvider.getName() : null); 56 } 57 58 protected AlgorithmParameterGenerator[] createAPGen() throws Exception { 59 if (!DSASupported) { 60 fail(validAlgName + " algorithm is not supported"); 61 return null; 62 } 63 AlgorithmParameterGenerator[] apg = new AlgorithmParameterGenerator[3]; 64 apg[0] = AlgorithmParameterGenerator.getInstance(validAlgName); 65 apg[1] = AlgorithmParameterGenerator.getInstance(validAlgName, 66 validProvider); 67 apg[2] = AlgorithmParameterGenerator.getInstance(validAlgName, 68 validProviderName); 69 return apg; 70 } 71 72 /** 73 * Test for <code>init(int size)</code> and 74 * <code>init(int size, SecureRandom random<code> methods 75 * Assertion: throws InvalidParameterException when size is incorrect 76 */ 77 public void testAlgorithmParameterGenerator11() throws Exception { 78 if (!DSASupported) { 79 fail(validAlgName + " algorithm is not supported"); 80 return; 81 } 82 // Invalid key strengths (strength must be from 512 - 1024 and a multiple of 64) 83 int[] keys = { -10000, -512, -1, 0, 10000 }; 84 SecureRandom random = new SecureRandom(); 85 AlgorithmParameterGenerator[] apgs = createAPGen(); 86 assertNotNull("AlgorithmParameterGenerator objects were not created", 87 apgs); 88 89 for (int i = 0; i < apgs.length; i++) { 90 91 // Test invalid strengths 92 for (int j = 0; j < keys.length; j++) { 93 try { 94 apgs[i].init(keys[j]); 95 fail("Expected an invalid parameter exception for strength " 96 + keys[j]); 97 } catch (InvalidParameterException e) { 98 // expected 99 } 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} 127