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.security.spec.AlgorithmParameterSpec; 26 27import org.apache.harmony.security.tests.support.MyKeyPairGenerator1; 28import org.apache.harmony.security.tests.support.MyKeyPairGenerator2; 29import org.apache.harmony.security.tests.support.SpiEngUtils; 30 31import junit.framework.TestCase; 32 33 34/** 35 * Tests for <code>KeyPairGenerator</code> class constructors and methods. 36 */ 37 38public class KeyPairGenerator2Test extends TestCase { 39 private String KeyPairGeneratorProviderClass = ""; 40 41 private static final String KeyPairGeneratorProviderClass1 = "org.apache.harmony.security.tests.support.MyKeyPairGenerator1"; 42 private static final String KeyPairGeneratorProviderClass2 = "org.apache.harmony.security.tests.support.MyKeyPairGenerator2"; 43 private static final String KeyPairGeneratorProviderClass3 = "org.apache.harmony.security.tests.support.MyKeyPairGenerator3"; 44 private static final String KeyPairGeneratorProviderClass4 = "org.apache.harmony.security.tests.support.MyKeyPairGeneratorSpi"; 45 46 private static final String defaultAlg = "KPGen"; 47 48 private static final String[] invalidValues = SpiEngUtils.invalidValues; 49 50 private static final String[] validValues; 51 52 String post; 53 54 static { 55 validValues = new String[4]; 56 validValues[0] = defaultAlg; 57 validValues[1] = defaultAlg.toLowerCase(); 58 validValues[2] = "kpGEN"; 59 validValues[3] = "kPGEn"; 60 } 61 62 Provider mProv; 63 String resAlg; 64 65 /* 66 * @see TestCase#tearDown() 67 */ 68 protected void tearDown() throws Exception { 69 super.tearDown(); 70 Security.removeProvider(mProv.getName()); 71 } 72 73 protected void setProv() { 74 mProv = (new SpiEngUtils()).new MyProvider("MyKPGenProvider".concat(post), 75 "Testing provider", KeyPairGenerator1Test.srvKeyPairGenerator.concat(".") 76 .concat(defaultAlg.concat(post)), 77 KeyPairGeneratorProviderClass); 78 Security.insertProviderAt(mProv, 1); 79 } 80 81 public KeyPairGenerator2Test(String arg0) { 82 super(arg0); 83 } 84 85 private void checkResult(KeyPairGenerator keyPairGen, int mode) 86 throws InvalidAlgorithmParameterException { 87 AlgorithmParameterSpec pp = null; 88 switch (mode) { 89 case 1: 90 try { 91 keyPairGen.initialize(pp, new SecureRandom()); 92 fail("InvalidAlgorithmParameterException must be thrown"); 93 } catch (InvalidAlgorithmParameterException e) { 94 } 95 keyPairGen.initialize(1000, new SecureRandom()); 96 try { 97 keyPairGen.initialize(-1024, new SecureRandom()); 98 fail("InvalidParameterException must be thrown"); 99 } catch (InvalidParameterException e) { 100 assertEquals("Incorrect exception", e.getMessage(), 101 "Incorrect keysize parameter"); 102 } 103 try { 104 keyPairGen.initialize(100, null); 105 fail("InvalidParameterException must be thrown"); 106 } catch (InvalidParameterException e) { 107 assertEquals("Incorrect exception", e.getMessage(), 108 "Incorrect random"); 109 } 110 keyPairGen.generateKeyPair(); 111 keyPairGen.genKeyPair(); 112 break; 113 case 2: 114 try { 115 keyPairGen.initialize(pp, new SecureRandom()); 116 } catch (UnsupportedOperationException e) { 117 // js2e does not throw this exception 118 } 119 keyPairGen.initialize(1000, new SecureRandom()); 120 try { 121 keyPairGen.initialize(63, new SecureRandom()); 122 fail("InvalidParameterException must be thrown"); 123 } catch (InvalidParameterException e) { 124 } 125 keyPairGen.initialize(100, null); 126 assertNull("Not null KeyPair", keyPairGen.generateKeyPair()); 127 assertNull("Not null KeyPair", keyPairGen.genKeyPair()); 128 break; 129 case 3: 130 keyPairGen.initialize(pp, new SecureRandom()); 131 keyPairGen.initialize(pp); 132 keyPairGen.initialize(1000, new SecureRandom()); 133 keyPairGen.initialize(100); 134 135 assertNotNull("Null KeyPair", keyPairGen.generateKeyPair()); 136 assertNotNull("Null KeyPair", keyPairGen.genKeyPair()); 137 break; 138 case 4: 139 try { 140 keyPairGen.initialize(pp, null); 141 fail("UnsupportedOperationException must be thrown"); 142 } catch (UnsupportedOperationException e) { 143 } 144 keyPairGen.initialize(pp, new SecureRandom()); 145 keyPairGen.initialize(101, new SecureRandom()); 146 keyPairGen.initialize(10000); 147 try { 148 keyPairGen.initialize(101, null); 149 fail("IllegalArgumentException must be thrown for null random"); 150 } catch (IllegalArgumentException e) { 151 } 152 try { 153 keyPairGen.initialize(99, new SecureRandom()); 154 fail("InvalidParameterException must be thrown for invalid key"); 155 } catch (InvalidParameterException e) { 156 } 157 try { 158 keyPairGen.initialize(99); 159 fail("InvalidParameterException must be thrown for invalid key"); 160 } catch (InvalidParameterException e) { 161 } 162 try { 163 keyPairGen.initialize(199, null); 164 fail("IllegalArgumentException must be thrown for null random"); 165 } catch (IllegalArgumentException e) { 166 } 167 assertNull("Not null KeyPair", keyPairGen.generateKeyPair()); 168 assertNull("Not null KeyPair", keyPairGen.genKeyPair()); 169 break; 170 } 171 172 } 173 174 /** 175 * Test for <code>getInstance(String algorithm)</code> method Assertions: 176 * throws NullPointerException when algorithm is null throws 177 * NoSuchAlgorithmException when algorithm is incorrect; returns 178 * KeyPairGenerator object 179 */ 180 private void GetInstance01(int mode) throws NoSuchAlgorithmException, 181 InvalidAlgorithmParameterException { 182 try { 183 KeyPairGenerator.getInstance(null); 184 fail("NullPointerException or KeyStoreException must be thrown"); 185 } catch (NoSuchAlgorithmException e) { 186 } catch (NullPointerException e) { 187 } 188 for (int i = 0; i < invalidValues.length; i++) { 189 try { 190 KeyPairGenerator.getInstance(invalidValues[i]); 191 fail("NoSuchAlgorithmException must be thrown (algorithm: " 192 .concat(invalidValues[i]).concat(")")); 193 } catch (NoSuchAlgorithmException e) { 194 } 195 } 196 KeyPairGenerator kpG; 197 for (int i = 0; i < validValues.length; i++) { 198 String alg = validValues[i].concat(post); 199 kpG = KeyPairGenerator.getInstance(alg); 200 assertEquals("Incorrect algorithm", kpG.getAlgorithm() 201 .toUpperCase(), (mode <= 2 ? resAlg : alg).toUpperCase()); 202 assertEquals("Incorrect provider", kpG.getProvider(), mProv); 203 checkResult(kpG, mode); 204 } 205 } 206 207 /** 208 * Test for <code>getInstance(String algorithm, String provider)</code> 209 * method 210 * Assertions: 211 * throws NullPointerException when algorithm is null 212 * throws NoSuchAlgorithmException when algorithm is incorrect; 213 * throws IllegalArgumentException when provider is null; 214 * throws NoSuchProviderException when provider is available; 215 * returns 216 * KeyPairGenerator object 217 */ 218 public void GetInstance02(int mode) throws NoSuchAlgorithmException, 219 NoSuchProviderException, IllegalArgumentException, 220 InvalidAlgorithmParameterException { 221 try { 222 KeyPairGenerator.getInstance(null, mProv.getName()); 223 fail("NullPointerException or KeyStoreException must be thrown"); 224 } catch (NoSuchAlgorithmException e) { 225 } catch (NullPointerException e) { 226 } 227 for (int i = 0; i < invalidValues.length; i++) { 228 try { 229 KeyPairGenerator.getInstance(invalidValues[i], mProv.getName()); 230 fail("NoSuchAlgorithmException must be thrown (algorithm: " 231 .concat(invalidValues[i]).concat(")")); 232 } catch (NoSuchAlgorithmException e) { 233 } 234 } 235 String prov = null; 236 for (int i = 0; i < validValues.length; i++) { 237 String alg = validValues[i].concat(post); 238 try { 239 KeyPairGenerator.getInstance(alg, prov); 240 fail("IllegalArgumentException must be thrown when provider is null (algorithm: " 241 .concat(alg).concat(")")); 242 } catch (IllegalArgumentException e) { 243 } 244 } 245 for (int i = 0; i < validValues.length; i++) { 246 String alg = validValues[i].concat(post); 247 for (int j = 1; j < invalidValues.length; j++) { 248 try { 249 KeyPairGenerator.getInstance(alg, invalidValues[j]); 250 fail("NoSuchProviderException must be thrown (algorithm: " 251 .concat(alg).concat(" provider: ").concat( 252 invalidValues[j]).concat(")")); 253 } catch (NoSuchProviderException e) { 254 } 255 } 256 } 257 KeyPairGenerator kpG; 258 for (int i = 0; i < validValues.length; i++) { 259 String alg = validValues[i].concat(post); 260 kpG = KeyPairGenerator.getInstance(alg, mProv.getName()); 261 assertEquals("Incorrect algorithm", kpG.getAlgorithm() 262 .toUpperCase(), (mode <= 2 ? resAlg : alg).toUpperCase()); 263 assertEquals("Incorrect provider", kpG.getProvider().getName(), 264 mProv.getName()); 265 checkResult(kpG, mode); 266 } 267 } 268 269 /** 270 * Test for <code>getInstance(String algorithm, Provider provider)</code> 271 * method 272 * Assertions: 273 * throws NullPointerException when algorithm is null 274 * throws NoSuchAlgorithmException when algorithm is incorrect; 275 * throws IllegalArgumentException when provider is null; 276 * returns KeyPairGenerator object 277 */ 278 private void GetInstance03(int mode) throws NoSuchAlgorithmException, 279 IllegalArgumentException, InvalidAlgorithmParameterException { 280 try { 281 KeyPairGenerator.getInstance(null, mProv); 282 fail("NullPointerException or KeyStoreException must be thrown"); 283 } catch (NoSuchAlgorithmException e) { 284 } catch (NullPointerException e) { 285 } 286 for (int i = 0; i < invalidValues.length; i++) { 287 try { 288 KeyPairGenerator.getInstance(invalidValues[i], mProv); 289 fail("NoSuchAlgorithmException must be thrown (algorithm: " 290 .concat(invalidValues[i]).concat(")")); 291 } catch (NoSuchAlgorithmException e) { 292 } 293 } 294 Provider prov = null; 295 for (int i = 0; i < validValues.length; i++) { 296 String alg = validValues[i].concat(post); 297 try { 298 KeyPairGenerator.getInstance(alg, prov); 299 fail("IllegalArgumentException must be thrown when provider is null (algorithm: " 300 .concat(alg).concat(")")); 301 } catch (IllegalArgumentException e) { 302 } 303 } 304 KeyPairGenerator kpG; 305 for (int i = 0; i < validValues.length; i++) { 306 String alg = validValues[i].concat(post); 307 kpG = KeyPairGenerator.getInstance(alg, mProv); 308 assertEquals("Incorrect algorithm", kpG.getAlgorithm() 309 .toUpperCase(), (mode <= 2 ? resAlg : alg).toUpperCase()); 310 assertEquals("Incorrect provider", kpG.getProvider(), mProv); 311 checkResult(kpG, mode); 312 } 313 } 314 315 public void testGetInstance01() throws NoSuchAlgorithmException, 316 InvalidAlgorithmParameterException { 317 KeyPairGeneratorProviderClass = KeyPairGeneratorProviderClass1; 318 resAlg = MyKeyPairGenerator1.getResAlgorithm(); 319 post = "_1"; 320 setProv(); 321 GetInstance01(1); 322 } 323 324 public void testGetInstance02() throws NoSuchAlgorithmException, 325 NoSuchProviderException, IllegalArgumentException, 326 InvalidAlgorithmParameterException { 327 KeyPairGeneratorProviderClass = KeyPairGeneratorProviderClass1; 328 resAlg = MyKeyPairGenerator1.getResAlgorithm(); 329 post = "_1"; 330 setProv(); 331 GetInstance02(1); 332 } 333 334 public void testGetInstance03() throws NoSuchAlgorithmException, 335 IllegalArgumentException, InvalidAlgorithmParameterException { 336 KeyPairGeneratorProviderClass = KeyPairGeneratorProviderClass1; 337 resAlg = MyKeyPairGenerator1.getResAlgorithm(); 338 post = "_1"; 339 setProv(); 340 GetInstance03(1); 341 } 342 343 public void testGetInstance04() throws NoSuchAlgorithmException, 344 InvalidAlgorithmParameterException { 345 KeyPairGeneratorProviderClass = KeyPairGeneratorProviderClass2; 346 resAlg = MyKeyPairGenerator2.getResAlgorithm(); 347 post = "_2"; 348 setProv(); 349 GetInstance01(2); 350 } 351 352 public void testGetInstance05() throws NoSuchAlgorithmException, 353 NoSuchProviderException, IllegalArgumentException, 354 InvalidAlgorithmParameterException { 355 KeyPairGeneratorProviderClass = KeyPairGeneratorProviderClass2; 356 resAlg = MyKeyPairGenerator2.getResAlgorithm(); 357 post = "_2"; 358 setProv(); 359 GetInstance02(2); 360 } 361 362 public void testGetInstance06() throws NoSuchAlgorithmException, 363 IllegalArgumentException, InvalidAlgorithmParameterException { 364 KeyPairGeneratorProviderClass = KeyPairGeneratorProviderClass2; 365 resAlg = MyKeyPairGenerator2.getResAlgorithm(); 366 post = "_2"; 367 setProv(); 368 GetInstance03(2); 369 } 370 371 public void testGetInstance07() throws NoSuchAlgorithmException, 372 InvalidAlgorithmParameterException { 373 KeyPairGeneratorProviderClass = KeyPairGeneratorProviderClass3; 374 resAlg = ""; 375 post = "_3"; 376 setProv(); 377 GetInstance01(3); 378 } 379 380 public void testGetInstance08() throws NoSuchAlgorithmException, 381 NoSuchProviderException, IllegalArgumentException, 382 InvalidAlgorithmParameterException { 383 KeyPairGeneratorProviderClass = KeyPairGeneratorProviderClass3; 384 resAlg = ""; 385 post = "_3"; 386 setProv(); 387 GetInstance02(3); 388 } 389 390 public void testGetInstance09() throws NoSuchAlgorithmException, 391 IllegalArgumentException, InvalidAlgorithmParameterException { 392 KeyPairGeneratorProviderClass = KeyPairGeneratorProviderClass3; 393 resAlg = ""; 394 post = "_3"; 395 setProv(); 396 GetInstance03(3); 397 } 398 399 public void testGetInstance10() throws NoSuchAlgorithmException, 400 InvalidAlgorithmParameterException { 401 KeyPairGeneratorProviderClass = KeyPairGeneratorProviderClass4; 402 resAlg = ""; 403 post = "_4"; 404 setProv(); 405 GetInstance01(4); 406 } 407 408 public void testGetInstance11() throws NoSuchAlgorithmException, 409 NoSuchProviderException, IllegalArgumentException, 410 InvalidAlgorithmParameterException { 411 KeyPairGeneratorProviderClass = KeyPairGeneratorProviderClass4; 412 resAlg = ""; 413 post = "_4"; 414 setProv(); 415 GetInstance02(4); 416 } 417 418 public void testGetInstance12() throws NoSuchAlgorithmException, 419 IllegalArgumentException, InvalidAlgorithmParameterException { 420 KeyPairGeneratorProviderClass = KeyPairGeneratorProviderClass4; 421 resAlg = ""; 422 post = "_4"; 423 setProv(); 424 GetInstance03(4); 425 } 426} 427