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 org.apache.harmony.security.tests.support.SpiEngUtils; 26 27import junit.framework.TestCase; 28 29 30/** 31 * Tests for KeyPairGenerator class 32 */ 33 34public class KeyPairGenerator3Test extends TestCase { 35 36 /** 37 * Constructor for KeyPairGenerator3Test. 38 * 39 * @param arg0 40 */ 41 public KeyPairGenerator3Test(String arg0) { 42 super(arg0); 43 } 44 45 private static String validProviderName = null; 46 47 public static Provider validProvider = null; 48 49 private static boolean DSASupported = false; 50 51 private static String NotSupportMsg = KeyPairGenerator1Test.NotSupportMsg; 52 53 static { 54 validProvider = SpiEngUtils.isSupport( 55 KeyPairGenerator1Test.validAlgName, 56 KeyPairGenerator1Test.srvKeyPairGenerator); 57 DSASupported = (validProvider != null); 58 validProviderName = (DSASupported ? validProvider.getName() : null); 59 } 60 61 protected KeyPairGenerator[] createKPGen() { 62 if (!DSASupported) { 63 fail(KeyPairGenerator1Test.validAlgName 64 + " algorithm is not supported"); 65 return null; 66 } 67 KeyPairGenerator[] kpg = new KeyPairGenerator[3]; 68 try { 69 kpg[0] = KeyPairGenerator 70 .getInstance(KeyPairGenerator1Test.validAlgName); 71 kpg[1] = KeyPairGenerator.getInstance( 72 KeyPairGenerator1Test.validAlgName, validProvider); 73 kpg[2] = KeyPairGenerator.getInstance( 74 KeyPairGenerator1Test.validAlgName, validProviderName); 75 return kpg; 76 } catch (Exception e) { 77 e.printStackTrace(); 78 return null; 79 } 80 } 81 82 83 /** 84 * Test for <code>generateKeyPair()</code> and <code>genKeyPair()</code> 85 * methods 86 * Assertion: KeyPairGenerator was initialized before the invocation 87 * of these methods 88 */ 89 public void testGenKeyPair01() throws NoSuchAlgorithmException, 90 NoSuchProviderException, IllegalArgumentException { 91 if (!DSASupported) { 92 fail(NotSupportMsg); 93 return; 94 } 95 KeyPairGenerator[] kpg = createKPGen(); 96 assertNotNull("KeyPairGenerator objects were not created", kpg); 97 KeyPair kp, kp1; 98 SecureRandom rr = new SecureRandom(); 99 for (int i = 0; i < kpg.length; i++) { 100 kpg[i].initialize(512, rr); 101 kp = kpg[i].generateKeyPair(); 102 kp1 = kpg[i].genKeyPair(); 103 assertFalse("Incorrect private key", kp.getPrivate().equals( 104 kp1.getPrivate())); 105 assertFalse("Incorrect public key", kp.getPublic().equals( 106 kp1.getPublic())); 107 } 108 } 109 110 /** 111 * Test for <code>generateKeyPair()</code> and <code>genKeyPair()</code> 112 * methods 113 * Assertion: these methods are used without previously initialization 114 */ 115 public void testGenKeyPair02() throws NoSuchAlgorithmException, 116 NoSuchProviderException, IllegalArgumentException { 117 if (!DSASupported) { 118 fail(NotSupportMsg); 119 return; 120 } 121 KeyPairGenerator[] kpg = createKPGen(); 122 assertNotNull("KeyPairGenerator objects were not created", kpg); 123 KeyPair kp, kp1; 124 for (int i = 0; i < kpg.length; i++) { 125 kp = kpg[i].generateKeyPair(); 126 kp1 = kpg[i].genKeyPair(); 127 assertFalse("Incorrect private key", kp.getPrivate().equals( 128 kp1.getPrivate())); 129 assertFalse("Incorrect public key", kp.getPublic().equals( 130 kp1.getPublic())); 131 } 132 } 133 134} 135