001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /**
019: * @author Vera Y. Petrashkova
020: * @version $Revision$
021: */package org.apache.harmony.security.tests.java.security;
022:
023: import java.security.*;
024: import org.apache.harmony.security.tests.support.SpiEngUtils;
025:
026: import junit.framework.TestCase;
027:
028: /**
029: * Tests for KeyPairGenerator class
030: *
031: */
032:
033: public class KeyPairGenerator3Test extends TestCase {
034:
035: /**
036: * Constructor for KeyPairGenerator3Test.
037: *
038: * @param arg0
039: */
040: public KeyPairGenerator3Test(String arg0) {
041: super (arg0);
042: }
043:
044: private static String validProviderName = null;
045:
046: public static Provider validProvider = null;
047:
048: private static boolean DSASupported = false;
049:
050: private static String NotSupportMsg = KeyPairGenerator1Test.NotSupportMsg;
051:
052: static {
053: validProvider = SpiEngUtils.isSupport(
054: KeyPairGenerator1Test.validAlgName,
055: KeyPairGenerator1Test.srvKeyPairGenerator);
056: DSASupported = (validProvider != null);
057: validProviderName = (DSASupported ? validProvider.getName()
058: : null);
059: }
060:
061: protected KeyPairGenerator[] createKPGen() {
062: if (!DSASupported) {
063: fail(KeyPairGenerator1Test.validAlgName
064: + " algorithm is not supported");
065: return null;
066: }
067: KeyPairGenerator[] kpg = new KeyPairGenerator[3];
068: try {
069: kpg[0] = KeyPairGenerator
070: .getInstance(KeyPairGenerator1Test.validAlgName);
071: kpg[1] = KeyPairGenerator.getInstance(
072: KeyPairGenerator1Test.validAlgName, validProvider);
073: kpg[2] = KeyPairGenerator.getInstance(
074: KeyPairGenerator1Test.validAlgName,
075: validProviderName);
076: return kpg;
077: } catch (Exception e) {
078: e.printStackTrace();
079: return null;
080: }
081: }
082:
083: /**
084: * Test for <code>generateKeyPair()</code> and <code>genKeyPair()</code>
085: * methods
086: * Assertion: KeyPairGenerator was initialized before the invocation
087: * of these methods
088: */
089: public void testGenKeyPair01() throws NoSuchAlgorithmException,
090: NoSuchProviderException, IllegalArgumentException {
091: if (!DSASupported) {
092: fail(NotSupportMsg);
093: return;
094: }
095: KeyPairGenerator[] kpg = createKPGen();
096: assertNotNull("KeyPairGenerator objects were not created", kpg);
097: KeyPair kp, kp1;
098: SecureRandom rr = new SecureRandom();
099: 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()
104: .equals(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()
128: .equals(kp1.getPrivate()));
129: assertFalse("Incorrect public key", kp.getPublic().equals(
130: kp1.getPublic()));
131: }
132: }
133:
134: public static void main(String args[]) {
135: junit.textui.TestRunner.run(KeyPairGenerator3Test.class);
136: }
137:
138: }
|