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