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; 23import java.security.*; 24import java.security.spec.AlgorithmParameterSpec; 25 26import org.apache.harmony.security.tests.support.MyAlgorithmParameterGeneratorSpi; 27import org.apache.harmony.security.tests.support.SpiEngUtils; 28 29import junit.framework.TestCase; 30 31 32/** 33 * Tests for <code>AlgorithmParameterGenerator</code> class constructors and 34 * methods. 35 * 36 */ 37 38public class AlgorithmParameterGenerator1Test extends TestCase { 39 /** 40 * Constructor for AlgorithmParameterGeneratorTests. 41 * 42 * @param arg0 43 */ 44 public AlgorithmParameterGenerator1Test(String arg0) { 45 super(arg0); 46 } 47 48 private static String[] invalidValues = SpiEngUtils.invalidValues; 49 private static String validAlgName = "DSA"; 50 private static String[] algs = { 51 "DSA", "dsa", "Dsa", "DsA", "dsA" }; 52 53 public static final String srvAlgorithmParameterGenerator = "AlgorithmParameterGenerator"; 54 55 56 private static String validProviderName = null; 57 58 private static Provider validProvider = null; 59 60 private static boolean DSASupported = false; 61 62 static { 63 validProvider = SpiEngUtils.isSupport( 64 validAlgName, 65 srvAlgorithmParameterGenerator); 66 DSASupported = (validProvider != null); 67 validProviderName = (DSASupported ? validProvider.getName() : null); 68 } 69 70 protected AlgorithmParameterGenerator[] createAPGen() { 71 if (!DSASupported) { 72 fail(validAlgName + " algorithm is not supported"); 73 return null; 74 } 75 AlgorithmParameterGenerator[] apg = new AlgorithmParameterGenerator[3]; 76 try { 77 apg[0] = AlgorithmParameterGenerator.getInstance(validAlgName); 78 apg[1] = AlgorithmParameterGenerator.getInstance(validAlgName, 79 validProvider); 80 apg[2] = AlgorithmParameterGenerator.getInstance(validAlgName, 81 validProviderName); 82 return apg; 83 } catch (Exception e) { 84 e.printStackTrace(); 85 return null; 86 } 87 } 88 89 /** 90 * Test for <code>getInstance(String algorithm)</code> method 91 * Assertion: 92 * throws NullPointerException must be thrown is null 93 * throws NoSuchAlgorithmException must be thrown if algorithm is not available 94 * 95 */ 96 public void testAlgorithmParameterGenerator01() 97 throws NoSuchAlgorithmException { 98 try { 99 AlgorithmParameterGenerator.getInstance(null); 100 fail("NullPointerException or NoSuchAlgorithmException should be thrown"); 101 } catch (NullPointerException e) { 102 } catch (NoSuchAlgorithmException e) { 103 } 104 for (int i = 0; i < invalidValues.length; i++) { 105 try { 106 AlgorithmParameterGenerator.getInstance(invalidValues[i]); 107 fail("NoSuchAlgorithmException should be thrown"); 108 } catch (NoSuchAlgorithmException e) { 109 } 110 } 111 } 112 113 /** 114 * Test for <code>getInstance(String algorithm)</code> method 115 * Assertion: returns AlgorithmParameterGenerator instance 116 * when algorithm is DSA 117 */ 118 public void testAlgorithmParameterGenerator02() 119 throws NoSuchAlgorithmException { 120 if (!DSASupported) { 121 fail(validAlgName + " algorithm is not supported"); 122 return; 123 } 124 AlgorithmParameterGenerator apg; 125 for (int i = 0; i < algs.length; i++) { 126 apg = AlgorithmParameterGenerator.getInstance(algs[i]); 127 assertEquals("Incorrect algorithm", apg.getAlgorithm(), algs[i]); 128 } 129 } 130 131 /** 132 * Test for <code>getInstance(String algorithm, String provider)</code> 133 * method 134 * Assertion: 135 * throws IllegalArgumentException if provider is null or empty 136 */ 137 public void testAlgorithmParameterGenerator03() 138 throws NoSuchAlgorithmException, NoSuchProviderException { 139 if (!DSASupported) { 140 fail(validAlgName + " algorithm is not supported"); 141 return; 142 } 143 String provider = null; 144 for (int i = 0; i < algs.length; i++) { 145 try { 146 AlgorithmParameterGenerator.getInstance(algs[i], provider); 147 fail("IllegalArgumentException must be thrown when provider is null"); 148 } catch (IllegalArgumentException e) { 149 } 150 try { 151 AlgorithmParameterGenerator.getInstance(algs[i], ""); 152 fail("IllegalArgumentException must be thrown when provider is empty"); 153 } catch (IllegalArgumentException e) { 154 } 155 } 156 } 157 158 /** 159 * Test for <code>getInstance(String algorithm, String provider)</code> 160 * method 161 * Assertion: throws NoSuchProviderException if provider is not 162 * available 163 */ 164 public void testAlgorithmParameterGenerator04() 165 throws NoSuchAlgorithmException { 166 if (!DSASupported) { 167 fail(validAlgName + " algorithm is not supported"); 168 return; 169 } 170 for (int i = 0; i < algs.length; i++) { 171 for (int j = 1; j < invalidValues.length; j++) { 172 try { 173 AlgorithmParameterGenerator.getInstance(algs[i], 174 invalidValues[j]); 175 fail("NoSuchProviderException must be thrown (provider: " 176 .concat(invalidValues[j])); 177 } catch (NoSuchProviderException e) { 178 } 179 } 180 } 181 } 182 183 /** 184 * Test for <code>getInstance(String algorithm, String provider)</code> 185 * method 186 * Assertion: 187 * throws NullPointerException must be thrown is null 188 * throws NoSuchAlgorithmException must be thrown if algorithm is not available 189 */ 190 public void testAlgorithmParameterGenerator05() 191 throws NoSuchProviderException { 192 if (!DSASupported) { 193 fail(validAlgName + " algorithm is not supported"); 194 return; 195 } 196 try { 197 AlgorithmParameterGenerator.getInstance(null, validProviderName); 198 fail("NullPointerException or NoSuchAlgorithmException should be thrown"); 199 } catch (NullPointerException e) { 200 } catch (NoSuchAlgorithmException e) { 201 } 202 for (int i = 0; i < invalidValues.length; i++) { 203 try { 204 AlgorithmParameterGenerator.getInstance(invalidValues[i], 205 validProviderName); 206 fail("NoSuchAlgorithmException must be thrown when (algorithm: " 207 .concat(invalidValues[i].concat(")"))); 208 } catch (NoSuchAlgorithmException e) { 209 } 210 } 211 } 212 213 /** 214 * Test for <code>getInstance(String algorithm, String provider)</code> 215 * method 216 * Assertion: return AlgorithmParameterGenerator 217 */ 218 public void testAlgorithmParameterGenerator06() 219 throws NoSuchAlgorithmException, NoSuchProviderException { 220 if (!DSASupported) { 221 fail(validAlgName + " algorithm is not supported"); 222 return; 223 } 224 AlgorithmParameterGenerator apg; 225 for (int i = 0; i < algs.length; i++) { 226 apg = AlgorithmParameterGenerator.getInstance(algs[i], 227 validProviderName); 228 assertEquals("Incorrect algorithm", algs[i], apg.getAlgorithm()); 229 assertEquals("Incorrect provider", apg.getProvider().getName(), 230 validProviderName); 231 } 232 } 233 234 /** 235 * Test for <code>getInstance(String algorithm, Provider provider)</code> 236 * method 237 * Assertion: throws IllegalArgumentException when provider is null 238 */ 239 public void testAlgorithmParameterGenerator07() 240 throws NoSuchAlgorithmException { 241 if (!DSASupported) { 242 fail(validAlgName + " algorithm is not supported"); 243 return; 244 } 245 Provider provider = null; 246 for (int i = 0; i < algs.length; i++) { 247 try { 248 AlgorithmParameterGenerator.getInstance(algs[i], provider); 249 fail("IllegalArgumentException must be thrown when provider is null"); 250 } catch (IllegalArgumentException e) { 251 } 252 } 253 } 254 255 /** 256 * Test for <code>getInstance(String algorithm, Provider provider)</code> 257 * method 258 * Assertion: 259 * throws NullPointerException must be thrown is null 260 * throws NoSuchAlgorithmException must be thrown if algorithm is not available 261 */ 262 public void testAlgorithmParameterGenerator08() { 263 if (!DSASupported) { 264 fail(validAlgName + " algorithm is not supported"); 265 return; 266 } 267 try { 268 AlgorithmParameterGenerator.getInstance(null, validProvider); 269 fail("NullPointerException or NoSuchAlgorithmException should be thrown"); 270 } catch (NullPointerException e) { 271 } catch (NoSuchAlgorithmException e) { 272 } 273 for (int i = 0; i < invalidValues.length; i++) { 274 try { 275 AlgorithmParameterGenerator.getInstance(invalidValues[i], 276 validProvider); 277 fail("NoSuchAlgorithmException must be thrown (algorithm: " 278 .concat(invalidValues[i]).concat(")")); 279 } catch (NoSuchAlgorithmException e) { 280 } 281 } 282 } 283 284 /** 285 * Test for <code>getInstance(String algorithm, Provider provider)</code> 286 * method 287 * Assertion: returns AlgorithmParameterGenerator object 288 */ 289 public void testAlgorithmParameterGenerator09() 290 throws NoSuchAlgorithmException { 291 if (!DSASupported) { 292 fail(validAlgName + " algorithm is not supported"); 293 return; 294 } 295 AlgorithmParameterGenerator apg; 296 for (int i = 0; i < algs.length; i++) { 297 apg = AlgorithmParameterGenerator.getInstance(algs[i], 298 validProvider); 299 assertEquals("Incorrect algorithm", apg.getAlgorithm(), algs[i]); 300 assertEquals("Incorrect provider", apg.getProvider(), validProvider); 301 } 302 } 303 304 /** 305 * Test for <code>generateParameters()</code> method 306 * Assertion: returns AlgorithmParameters object 307 */ 308 public void testAlgorithmParameterGenerator10() 309 throws NoSuchAlgorithmException { 310 if (!DSASupported) { 311 fail(validAlgName + " algorithm is not supported"); 312 return; 313 } 314 AlgorithmParameterGenerator apg = AlgorithmParameterGenerator 315 .getInstance(validAlgName); 316 apg.init(512); 317 AlgorithmParameters ap = apg.generateParameters(); 318 assertEquals("Incorrect algorithm", ap.getAlgorithm().toUpperCase(), 319 apg.getAlgorithm().toUpperCase()); 320 } 321 322 /** 323 * Test for <code>init(AlgorithmParameterSpec param)</code> and 324 * <code>init(AlgorithmParameterSpec param, SecureRandom random<code> 325 * methods 326 * Assertion: throws InvalidAlgorithmParameterException when param is null 327 */ 328 public void testAlgorithmParameterGenerator12() { 329 if (!DSASupported) { 330 fail(validAlgName + " algorithm is not supported"); 331 return; 332 } 333 SecureRandom random = new SecureRandom(); 334 AlgorithmParameterSpec aps = null; 335 AlgorithmParameterGenerator[] apgs = createAPGen(); 336 assertNotNull("AlgorithmParameterGenerator objects were not created", 337 apgs); 338 for (int i = 0; i < apgs.length; i++) { 339 try { 340 apgs[i].init(aps, random); 341 fail("InvalidAlgorithmParameterException must be throws when param is null"); 342 } catch (InvalidAlgorithmParameterException e) { 343 } 344 } 345 } 346 347 /** 348 * Test for <code>AlgorithmParameterGenerator</code> constructor 349 * Assertion: returns AlgorithmParameterGenerator object 350 */ 351 public void testAlgorithmParameterGeneratorConstr() throws NoSuchAlgorithmException { 352 if (!DSASupported) { 353 fail(validAlgName + " algorithm is not supported"); 354 return; 355 } 356 AlgorithmParameterGeneratorSpi spi = new MyAlgorithmParameterGeneratorSpi(); 357 AlgorithmParameterGenerator apg = 358 new myAlgPG(spi, validProvider, validAlgName); 359 assertEquals("Incorrect algorithm", apg.getAlgorithm(), validAlgName); 360 assertEquals("Incorrect provider",apg.getProvider(),validProvider); 361 try { 362 apg.init(-10, null); 363 fail("IllegalArgumentException must be thrown"); 364 } catch (IllegalArgumentException e) { 365 } 366 367 apg = new myAlgPG(null, null, null); 368 assertNull("Incorrect algorithm", apg.getAlgorithm()); 369 assertNull("Incorrect provider", apg.getProvider()); 370 try { 371 apg.init(-10, null); 372 fail("NullPointerException must be thrown"); 373 } catch (NullPointerException e) { 374 } 375 } 376 377} 378/** 379 * Additional class to verify AlgorithmParameterGenerator constructor 380 */ 381class myAlgPG extends AlgorithmParameterGenerator { 382 public myAlgPG(AlgorithmParameterGeneratorSpi spi, Provider prov, String alg) { 383 super(spi, prov, alg); 384 } 385} 386