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.*; 25import java.math.BigInteger; 26import java.security.spec.AlgorithmParameterSpec; 27 28import org.apache.harmony.security.tests.support.MyKeyPairGenerator1; 29import org.apache.harmony.security.tests.support.MyKeyPairGenerator2; 30import org.apache.harmony.security.tests.support.SpiEngUtils; 31 32import junit.framework.TestCase; 33 34/** 35 * Tests for <code>KeyPairGenerator</code> class constructors and methods. 36 * 37 */ 38 39public class KeyPairGenerator1Test extends TestCase { 40 41 /** 42 * Constructor for KayPairGeneratorTest. 43 * 44 * @param arg0 45 */ 46 public KeyPairGenerator1Test(String arg0) { 47 super(arg0); 48 } 49 50 private static String[] invalidValues = SpiEngUtils.invalidValues; 51 52 public static final String srvKeyPairGenerator = "KeyPairGenerator"; 53 54 public static String[] algs = { 55 "DSA", "dsa", "Dsa", "DsA", "dsA" }; 56 57 public static String validAlgName = "DSA"; 58 59 private static String validProviderName = null; 60 61 public static Provider validProvider = null; 62 63 private static boolean DSASupported = false; 64 65 public static String NotSupportMsg = ""; 66 67 static { 68 validProvider = SpiEngUtils.isSupport( 69 validAlgName, 70 srvKeyPairGenerator); 71 DSASupported = (validProvider != null); 72 if (!DSASupported) { 73 NotSupportMsg = validAlgName + " algorithm is not supported" ; 74 } 75 validProviderName = (DSASupported ? validProvider.getName() : null); 76 } 77 78 protected KeyPairGenerator [] createKPGen() { 79 if (!DSASupported) { 80 fail(NotSupportMsg); 81 return null; 82 } 83 KeyPairGenerator[] kpg = new KeyPairGenerator[3]; 84 try { 85 kpg[0] = KeyPairGenerator.getInstance(validAlgName); 86 kpg[1] = KeyPairGenerator.getInstance(validAlgName, validProvider); 87 kpg[2] = KeyPairGenerator.getInstance(validAlgName, validProviderName); 88 return kpg; 89 } catch (Exception e) { 90 e.printStackTrace(); 91 return null; 92 } 93 } 94 95 /** 96 * Test for <code>getInstance(String algorithm)</code> method 97 * Assertion: 98 * throws NullPointerException when algorithm is null 99 * throws NoSuchAlgorithmException when algorithm is incorrect; 100 */ 101 public void testKeyPairGenerator01() throws NoSuchAlgorithmException { 102 try { 103 KeyPairGenerator.getInstance(null); 104 fail("NullPointerException or NoSuchAlgorithmException must be thrown when algorithm is null"); 105 } catch (NoSuchAlgorithmException e) { 106 } catch (NullPointerException e) { 107 } 108 for (int i = 0; i < invalidValues.length; i++) { 109 try { 110 KeyPairGenerator.getInstance(invalidValues[i]); 111 fail("NoSuchAlgorithmException must be thrown when algorithm is not available: " 112 .concat(invalidValues[i])); 113 } catch (NoSuchAlgorithmException e) { 114 } 115 } 116 } 117 118 /** 119 * Test for <code>getInstance(String algorithm)</code> method 120 * Assertion: returns KeyPairGenerator object 121 */ 122 public void testKeyPairGenerator02() throws NoSuchAlgorithmException { 123 if (!DSASupported) { 124 fail(NotSupportMsg); 125 return; 126 } 127 KeyPairGenerator kpg; 128 for (int i = 0; i < algs.length; i++) { 129 kpg = KeyPairGenerator.getInstance(algs[i]); 130 assertEquals("Incorrect algorithm ", kpg.getAlgorithm().toUpperCase(), 131 algs[i].toUpperCase()); 132 } 133 } 134 135 /** 136 * Test for <code>getInstance(String algorithm, String provider)</code> 137 * method 138 * Assertion: throws IllegalArgumentException when provider is null or empty 139 */ 140 public void testKeyPairGenerator03() throws NoSuchAlgorithmException, 141 NoSuchProviderException { 142 if (!DSASupported) { 143 fail(NotSupportMsg); 144 return; 145 } 146 String provider = null; 147 for (int i = 0; i < algs.length; i++) { 148 try { 149 KeyPairGenerator.getInstance(algs[i], provider); 150 fail("IllegalArgumentException must be thrown when provider is null"); 151 } catch (IllegalArgumentException e) { 152 } 153 try { 154 KeyPairGenerator.getInstance(algs[i], ""); 155 fail("IllegalArgumentException must be thrown when provider is empty"); 156 } catch (IllegalArgumentException e) { 157 } 158 } 159 } 160 161 /** 162 * Test for <code>getInstance(String algorithm, String provider)</code> 163 * method 164 * Assertion: 165 * throws NullPointerException when algorithm is null 166 * throws NoSuchAlgorithmException when algorithm is incorrect; 167 */ 168 public void testKeyPairGenerator04() throws NoSuchAlgorithmException, 169 IllegalArgumentException { 170 if (!DSASupported) { 171 fail(NotSupportMsg); 172 return; 173 } 174 for (int i = 0; i < algs.length; i++) { 175 for (int j = 1; j < invalidValues.length; j++) { 176 try { 177 KeyPairGenerator.getInstance(algs[i], invalidValues[j]); 178 fail("NoSuchProviderException must be thrown (algorithm: " 179 .concat(algs[i]).concat(" provider: ").concat( 180 invalidValues[j]).concat(")")); 181 } catch (NoSuchProviderException e) { 182 } 183 } 184 } 185 } 186 187 /** 188 * Test for <code>getInstance(String algorithm, String provider)</code> 189 * method 190 * Assertion: throws NoSuchAlgorithmException when algorithm is not 191 * available oe null 192 */ 193 public void testKeyPairGenerator05() throws NoSuchProviderException, 194 IllegalArgumentException { 195 if (!DSASupported) { 196 fail(NotSupportMsg); 197 return; 198 } 199 try { 200 KeyPairGenerator.getInstance(null, validProviderName); 201 fail("NullPointerException or NoSuchAlgorithmException must be thrown when algorithm is null"); 202 } catch (NoSuchAlgorithmException e) { 203 } catch (NullPointerException e) { 204 } 205 for (int i = 0; i < invalidValues.length; i++) { 206 try { 207 KeyPairGenerator.getInstance(invalidValues[i], 208 validProviderName); 209 fail("NoSuchAlgorithmException must be thrown (algorithm: " 210 .concat(algs[i]).concat(" provider: ").concat( 211 validProviderName).concat(")")); 212 } catch (NoSuchAlgorithmException e) { 213 } 214 } 215 } 216 217 /** 218 * Test for <code>getInstance(String algorithm, String provider)</code> 219 * method 220 * Assertion: returns KeyPairGenerator object 221 */ 222 public void testKeyPairGenerator06() throws NoSuchProviderException, 223 NoSuchAlgorithmException, IllegalArgumentException { 224 if (!DSASupported) { 225 fail(NotSupportMsg); 226 return; 227 } 228 KeyPairGenerator kpg; 229 for (int i = 0; i < algs.length; i++) { 230 kpg = KeyPairGenerator.getInstance(algs[i], validProviderName); 231 assertEquals("Incorrect algorithm", kpg.getAlgorithm().toUpperCase(), 232 algs[i].toUpperCase()); 233 assertEquals("Incorrect provider", kpg.getProvider().getName(), 234 validProviderName); 235 } 236 } 237 238 /** 239 * Test for <code>getInstance(String algorithm, Provider provider)</code> 240 * method 241 * Assertion: throws IllegalArgumentException when provider is null 242 */ 243 public void testKeyPairGenerator07() throws NoSuchAlgorithmException { 244 if (!DSASupported) { 245 fail(NotSupportMsg); 246 return; 247 } 248 Provider provider = null; 249 for (int i = 0; i < algs.length; i++) { 250 try { 251 KeyPairGenerator.getInstance(algs[i], provider); 252 fail("IllegalArgumentException must be thrown when provider is null"); 253 } catch (IllegalArgumentException e) { 254 } 255 } 256 } 257 258 /** 259 * Test for <code>getInstance(String algorithm, Provider provider)</code> 260 * method 261 * Assertion: 262 * throws NullPointerException when algorithm is null 263 * throws NoSuchAlgorithmException when algorithm is incorrect; 264 */ 265 public void testKeyPairGenerator08() throws IllegalArgumentException { 266 if (!DSASupported) { 267 fail(NotSupportMsg); 268 return; 269 } 270 try { 271 KeyPairGenerator.getInstance(null, validProvider); 272 fail("NullPointerException or NoSuchAlgorithmException must be thrown when algorithm is null"); 273 } catch (NoSuchAlgorithmException e) { 274 } catch (NullPointerException e) { 275 } 276 for (int i = 0; i < invalidValues.length; i++) { 277 try { 278 KeyPairGenerator.getInstance(invalidValues[i], validProvider); 279 fail("NoSuchAlgorithmException must be thrown (algorithm: " 280 .concat(algs[i]).concat(" provider: ").concat( 281 validProviderName).concat(")")); 282 } catch (NoSuchAlgorithmException e) { 283 } 284 } 285 } 286 287 /** 288 * Test for <code>getInstance(String algorithm, Provider provider)</code> 289 * method 290 * Assertion: returns KeyPairGenerator object 291 */ 292 public void testKeyPairGenerator09() throws NoSuchAlgorithmException, 293 IllegalArgumentException { 294 if (!DSASupported) { 295 fail(NotSupportMsg); 296 return; 297 } 298 KeyPairGenerator kpg; 299 for (int i = 0; i < algs.length; i++) { 300 kpg = KeyPairGenerator.getInstance(algs[i], validProvider); 301 assertEquals("Incorrect algorithm", kpg.getAlgorithm().toUpperCase(), 302 algs[i].toUpperCase()); 303 assertEquals("Incorrect provider", kpg.getProvider(), validProvider); 304 } 305 } 306 307 /** 308 * Test for <code>generateKeyPair()</code> and <code>genKeyPair()</code> 309 * methods 310 * Assertion: KeyPairGenerator was initialized before the invocation 311 * of these methods 312 */ 313 public void testKeyPairGenerator10() throws NoSuchAlgorithmException, 314 NoSuchProviderException, IllegalArgumentException { 315 if (!DSASupported) { 316 fail(NotSupportMsg); 317 return; 318 } 319 KeyPairGenerator[] kpg = createKPGen(); 320 assertNotNull("KeyPairGenerator objects were not created", kpg); 321 KeyPair kp, kp1; 322 for (int i = 0; i < kpg.length; i++) { 323 kpg[i].initialize(512); 324 kp = kpg[i].generateKeyPair(); 325 kp1 = kpg[i].genKeyPair(); 326 327 assertFalse("Incorrect private key", kp.getPrivate().equals( 328 kp1.getPrivate())); 329 assertFalse("Incorrect public key", kp.getPublic().equals( 330 kp1.getPublic())); 331 } 332 } 333 334 /** 335 * Test for methods: 336 * <code>initialize(int keysize)</code> 337 * <code>initialize(int keysize, SecureRandom random)</code> 338 * <code>initialize(AlgorithmParameterSpec param)</code> 339 * <code>initialize(AlgorithmParameterSpec param, SecureRandom random)</code> 340 * Assertion: throws InvalidParameterException or 341 * InvalidAlgorithmParameterException when parameters keysize or param are 342 * incorrect 343 */ 344 public void testKeyPairGenerator11() throws NoSuchAlgorithmException, 345 NoSuchProviderException { 346 if (!DSASupported) { 347 fail(NotSupportMsg); 348 return; 349 } 350 int[] keys = { -10000, -1024, -1, 0, 10000 }; 351 KeyPairGenerator[] kpg = createKPGen(); 352 assertNotNull("KeyPairGenerator objects were not created", kpg); 353 SecureRandom random = new SecureRandom(); 354 AlgorithmParameterSpec aps = null; 355 356 for (int i = 0; i < kpg.length; i++) { 357 358 for (int j = 0; j < keys.length; j++) { 359 try { 360 kpg[i].initialize(keys[j]); 361 kpg[i].initialize(keys[j], random); 362 } catch (InvalidParameterException e) { 363 } 364 } 365 366 try { 367 kpg[i].initialize(aps); 368 kpg[i].initialize(aps, random); 369 } catch (InvalidAlgorithmParameterException e) { 370 } 371 } 372 } 373 374 /** 375 * Test for methods: <code>initialize(int keysize)</code> 376 * <code>initialize(int keysize, SecureRandom random)</code> 377 * <code>initialize(AlgorithmParameterSpec param)</code> 378 * <code>initialize(AlgorithmParameterSpec param, SecureRandom random)</code> 379 * <code>generateKeyPair()</code> 380 * <code>genKeyPair()</code> 381 * Assertion: throws InvalidParameterException or 382 * InvalidAlgorithmParameterException when parameters keysize or param are 383 * incorrect Assertion: generateKeyPair() and genKeyPair() return null 384 * KeyPair Additional class MyKeyPairGenerator1 is used 385 */ 386 public void testKeyPairGenerator12() { 387 int[] keys = { -1, -250, 1, 64, 512, 1024 }; 388 SecureRandom random = new SecureRandom(); 389 AlgorithmParameterSpec aps; 390 KeyPairGenerator mKPG = new MyKeyPairGenerator1(""); 391 assertEquals("Incorrect algorithm", mKPG.getAlgorithm(), 392 MyKeyPairGenerator1.getResAlgorithm()); 393 394 mKPG.generateKeyPair(); 395 mKPG.genKeyPair(); 396 397 for (int i = 0; i < keys.length; i++) { 398 try { 399 mKPG.initialize(keys[i]); 400 fail("InvalidParameterException must be thrown (key: " 401 + Integer.toString(keys[i]) + ")"); 402 } catch (InvalidParameterException e) { 403 } 404 try { 405 mKPG.initialize(keys[i], random); 406 fail("InvalidParameterException must be thrown (key: " 407 + Integer.toString(keys[i]) + ")"); 408 } catch (InvalidParameterException e) { 409 } 410 } 411 try { 412 mKPG.initialize(100, null); 413 fail("InvalidParameterException must be thrown when random is null"); 414 } catch (InvalidParameterException e) { 415 } 416 417 mKPG.initialize(100, random); 418 assertEquals("Incorrect random", random, 419 ((MyKeyPairGenerator1) mKPG).secureRandom); 420 assertEquals("Incorrect keysize", 100, 421 ((MyKeyPairGenerator1) mKPG).keySize); 422 try { 423 mKPG.initialize(null, random); 424 fail("InvalidAlgorithmParameterException must be thrown when param is null"); 425 } catch (InvalidAlgorithmParameterException e) { 426 } 427 if (DSASupported) { 428 BigInteger bInt = new BigInteger("1"); 429 aps = new java.security.spec.DSAParameterSpec(bInt, bInt, bInt); 430 try { 431 mKPG.initialize(aps, null); 432 fail("InvalidParameterException must be thrown when random is null"); 433 } catch (InvalidParameterException e) { 434 } catch (InvalidAlgorithmParameterException e) { 435 fail("Unexpected InvalidAlgorithmParameterException was thrown"); 436 } 437 try { 438 mKPG.initialize(aps, random); 439 assertEquals("Incorrect random", random, 440 ((MyKeyPairGenerator1) mKPG).secureRandom); 441 assertEquals("Incorrect params", aps, 442 ((MyKeyPairGenerator1) mKPG).paramSpec); 443 } catch (InvalidAlgorithmParameterException e) { 444 fail("Unexpected InvalidAlgorithmParameterException was thrown"); 445 } 446 } 447 } 448 449 /** 450 * Test for methods: <code>initialize(int keysize)</code> 451 * <code>initialize(int keysize, SecureRandom random)</code> 452 * <code>initialize(AlgorithmParameterSpec param)</code> 453 * <code>initialize(AlgorithmParameterSpec param, SecureRandom random)</code> 454 * <code>generateKeyPair()</code> 455 * <code>genKeyPair()</code> 456 * Assertion: initialize(int ...) throws InvalidParameterException when 457 * keysize in incorrect Assertion: initialize(AlgorithmParameterSpec 458 * ...)throws UnsupportedOperationException Assertion: generateKeyPair() and 459 * genKeyPair() return not null KeyPair Additional class MyKeyPairGenerator2 460 * is used 461 */ 462 public void testKeyPairGenerator13() { 463 int[] keys = { -1, -250, 1, 63, -512, -1024 }; 464 SecureRandom random = new SecureRandom(); 465 KeyPairGenerator mKPG = new MyKeyPairGenerator2(null); 466 assertEquals("Algorithm must be null", mKPG.getAlgorithm(), 467 MyKeyPairGenerator2.getResAlgorithm()); 468 assertNull("genKeyPair() must return null", mKPG.genKeyPair()); 469 assertNull("generateKeyPair() mut return null", mKPG.generateKeyPair()); 470 for (int i = 0; i < keys.length; i++) { 471 try { 472 mKPG.initialize(keys[i]); 473 fail("InvalidParameterException must be thrown (key: " 474 + Integer.toString(keys[i]) + ")"); 475 } catch (InvalidParameterException e) { 476 } 477 try { 478 mKPG.initialize(keys[i], random); 479 fail("InvalidParameterException must be thrown (key: " 480 + Integer.toString(keys[i]) + ")"); 481 } catch (InvalidParameterException e) { 482 } 483 } 484 try { 485 mKPG.initialize(64); 486 } catch (InvalidParameterException e) { 487 fail("Unexpected InvalidParameterException was thrown"); 488 } 489 try { 490 mKPG.initialize(64, null); 491 } catch (InvalidParameterException e) { 492 fail("Unexpected InvalidParameterException was thrown"); 493 } 494 try { 495 mKPG.initialize(null, random); 496 } catch (UnsupportedOperationException e) { 497 // on j2se1.4 this exception is not thrown 498 } catch (InvalidAlgorithmParameterException e) { 499 fail("Unexpected InvalidAlgorithmParameterException was thrown"); 500 } 501 } 502 503} 504