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.MyKeyPairGeneratorSpi;
28
29import junit.framework.TestCase;
30
31/**
32 * Tests for <code>KeyPairGeneratorSpi</code> class constructors and methods.
33 */
34
35public class KeyPairGeneratorSpiTest extends TestCase {
36
37    /**
38     * Constructor for KeyPairGeneratorSpiTest.
39     *
40     * @param arg0
41     */
42    public KeyPairGeneratorSpiTest(String arg0) {
43        super(arg0);
44    }
45
46    /**
47     * Test for <code>KeyPairGeneratorSpi</code> constructor
48     * Assertion: constructs KeyPairGeneratorSpi
49     */
50    public void testKeyPairGeneratorSpi01()
51            throws InvalidAlgorithmParameterException,
52            InvalidParameterException {
53        KeyPairGeneratorSpi keyPairGen = new MyKeyPairGeneratorSpi();
54
55        AlgorithmParameterSpec pp = null;
56        try {
57            keyPairGen.initialize(pp, null);
58            fail("UnsupportedOperationException must be thrown");
59        } catch (UnsupportedOperationException e) {
60        }
61        keyPairGen.initialize(pp, new SecureRandom());
62        keyPairGen.initialize(1024, new SecureRandom());
63        try {
64            keyPairGen.initialize(-1024, new SecureRandom());
65            fail("IllegalArgumentException must be thrown for incorrect keysize");
66        } catch (IllegalArgumentException e) {
67        }
68        try {
69            keyPairGen.initialize(1024, null);
70            fail("IllegalArgumentException must be thrown");
71        } catch (IllegalArgumentException e) {
72            assertEquals("Incorrect exception", e.getMessage(),
73                    "Invalid random");
74        }
75        KeyPair kp = keyPairGen.generateKeyPair();
76        assertNull("Not null KeyPair", kp);
77    }
78}
79