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