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 18package org.apache.harmony.security.tests.java.security; 19 20import java.math.BigInteger; 21import java.security.AlgorithmParameterGenerator; 22import java.security.AlgorithmParameters; 23import java.security.InvalidAlgorithmParameterException; 24import java.security.NoSuchProviderException; 25import java.security.Provider; 26import java.security.SecureRandom; 27import java.security.Security; 28import java.security.spec.DSAParameterSpec; 29 30public class AlgorithmParameterGenerator3Test extends junit.framework.TestCase { 31 32 /** 33 * @tests java.security.AlgorithmParameterGenerator#generateParameters() 34 */ 35 public void test_generateParameters() throws Exception { 36 37 //fail("Takes ages. Problem with SecureRandom and stub math ?"); 38 39 // Test for method java.security.AlgorithmParameters 40 // java.security.AlgorithmParameterGenerator.generateParameters() 41 AlgorithmParameterGenerator gen = AlgorithmParameterGenerator 42 .getInstance("DSA"); 43 gen.init(1024); 44 45 // WARNING - The next line can take MINUTES to run 46 AlgorithmParameters params = gen.generateParameters(); 47 assertNotNull("params is null", params); 48 } 49 50 /** 51 * @tests java.security.AlgorithmParameterGenerator#getAlgorithm() 52 */ 53 public void test_getAlgorithm() throws Exception { 54 // Test for method java.lang.String 55 // java.security.AlgorithmParameterGenerator.getAlgorithm() 56 String alg = AlgorithmParameterGenerator.getInstance("DSA") 57 .getAlgorithm(); 58 assertEquals("getAlgorithm ok", "DSA", alg); 59 } 60 61 /** 62 * @tests java.security.AlgorithmParameterGenerator#getInstance(java.lang.String) 63 */ 64 public void test_getInstanceLjava_lang_String() throws Exception { 65 // Test for method java.security.AlgorithmParameterGenerator 66 // java.security.AlgorithmParameterGenerator.getInstance(java.lang.String) 67 AlgorithmParameterGenerator.getInstance("DSA"); 68 } 69 70 /** 71 * @tests java.security.AlgorithmParameterGenerator#getInstance(java.lang.String, 72 * java.lang.String) 73 */ 74 public void test_getInstanceLjava_lang_StringLjava_lang_String() throws Exception { 75 // Test for method java.security.AlgorithmParameterGenerator 76 // java.security.AlgorithmParameterGenerator.getInstance(java.lang.String, 77 // java.lang.String) 78 79 // Opting for DSA here as it is pretty widely supported 80 Provider[] provs = Security 81 .getProviders("AlgorithmParameterGenerator.DSA"); 82 83 for (int i = 0; i < provs.length; i++) { 84 AlgorithmParameterGenerator.getInstance("DSA", provs[i].getName()); 85 }// end for 86 87 88 // exception case - should throw IllegalArgumentException for null 89 // provider 90 try { 91 AlgorithmParameterGenerator.getInstance("DSA", (String) null); 92 fail("Should have thrown IllegalArgumentException"); 93 } catch (IllegalArgumentException e) { 94 // Expected 95 } 96 97 // exception case - should throw IllegalArgumentException for empty 98 // provider 99 try { 100 AlgorithmParameterGenerator.getInstance("DSA", ""); 101 fail("Should have thrown IllegalArgumentException"); 102 } catch (IllegalArgumentException e) { 103 // Expected 104 } 105 106 // exception case - should throw NoSuchProviderException 107 try { 108 AlgorithmParameterGenerator.getInstance("DSA", "IDontExist"); 109 fail("Should have thrown NoSuchProviderException"); 110 } catch (NoSuchProviderException e) { 111 // Expected 112 } 113 } 114 115 /** 116 * @tests java.security.AlgorithmParameterGenerator#getProvider() 117 */ 118 public void test_getProvider() throws Exception { 119 // Test for method java.security.Provider 120 // java.security.AlgorithmParameterGenerator.getProvider() 121 // checks that no exception is thrown 122 Provider p = AlgorithmParameterGenerator.getInstance("DSA") 123 .getProvider(); 124 assertNotNull("provider is null", p); 125 } 126 127 /** 128 * @tests java.security.AlgorithmParameterGenerator#init(int) 129 */ 130 public void test_initI() throws Exception { 131 // Test for method void 132 // java.security.AlgorithmParameterGenerator.init(int) 133 // checks that no exception is thrown 134 AlgorithmParameterGenerator gen = AlgorithmParameterGenerator 135 .getInstance("DSA"); 136 gen.init(1024); 137 } 138 139 /** 140 * @tests java.security.AlgorithmParameterGenerator#init(int, 141 * java.security.SecureRandom) 142 */ 143 public void test_initILjava_security_SecureRandom() throws Exception { 144 // Test for method void 145 // java.security.AlgorithmParameterGenerator.init(int, 146 // java.security.SecureRandom) 147 // checks that no exception is thrown 148 AlgorithmParameterGenerator gen = AlgorithmParameterGenerator 149 .getInstance("DSA"); 150 gen.init(1024, new SecureRandom()); 151 } 152 153 /** 154 * @tests java.security.AlgorithmParameterGenerator#init(java.security.spec.AlgorithmParameterSpec) 155 */ 156 public void test_initLjava_security_spec_AlgorithmParameterSpec() throws Exception { 157 // Test for method void 158 // java.security.AlgorithmParameterGenerator.init(java.security.spec.AlgorithmParameterSpec) 159 // checks that InvalidAlgorithmParameterException is thrown 160 DSAParameterSpec spec = new DSAParameterSpec(BigInteger.ONE, 161 BigInteger.ONE, BigInteger.ONE); 162 AlgorithmParameterGenerator gen = AlgorithmParameterGenerator 163 .getInstance("DSA"); 164 try { 165 gen.init(spec); 166 fail("No expected InvalidAlgorithmParameterException"); 167 } catch (InvalidAlgorithmParameterException e) { 168 //expected 169 } 170 } 171 172 /** 173 * @tests java.security.AlgorithmParameterGenerator#init(java.security.spec.AlgorithmParameterSpec, 174 * java.security.SecureRandom) 175 */ 176 public void test_initLjava_security_spec_AlgorithmParameterSpecLjava_security_SecureRandom() throws Exception { 177 // Test for method void 178 // java.security.AlgorithmParameterGenerator.init(java.security.spec.AlgorithmParameterSpec, 179 // java.security.SecureRandom) 180 // checks that InvalidAlgorithmParameterException is thrown 181 DSAParameterSpec spec = new DSAParameterSpec(BigInteger.ONE, 182 BigInteger.ONE, BigInteger.ONE); 183 AlgorithmParameterGenerator gen = AlgorithmParameterGenerator 184 .getInstance("DSA"); 185 try { 186 gen.init(spec, new SecureRandom()); 187 fail("No expected InvalidAlgorithmParameterException"); 188 } catch (InvalidAlgorithmParameterException e) { 189 //expected 190 } 191 } 192} 193