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* @version $Revision$
21*/
22
23package org.apache.harmony.crypto.tests.javax.crypto;
24
25import java.security.InvalidAlgorithmParameterException;
26import java.security.SecureRandom;
27import java.security.spec.AlgorithmParameterSpec;
28import javax.crypto.KeyGeneratorSpi;
29import javax.crypto.SecretKey;
30
31import org.apache.harmony.crypto.tests.support.MyKeyGeneratorSpi;
32
33import junit.framework.TestCase;
34
35/**
36 * Tests for <code>KeyGeneratorSpi</code> class constructors and methods.
37 *
38 */
39public class KeyGeneratorSpiTest extends TestCase {
40    class Mock_KeyGeneratorSpi extends MyKeyGeneratorSpi {
41
42        @Override
43        protected SecretKey engineGenerateKey() {
44            return super.engineGenerateKey();
45        }
46
47        @Override
48        protected void engineInit(SecureRandom random) {
49            super.engineInit(random);
50        }
51
52        @Override
53        protected void engineInit(AlgorithmParameterSpec params, SecureRandom random)
54                throws InvalidAlgorithmParameterException {
55            super.engineInit(params, random);
56        }
57
58        @Override
59        protected void engineInit(int keysize, SecureRandom random) {
60            super.engineInit(keysize, random);
61        }
62
63    }
64
65    /**
66     * Test for <code>KeyGeneratorSpi</code> constructor Assertion: constructs
67     * KeyGeneratorSpi
68     */
69    public void testKeyGeneratorSpi01() throws InvalidAlgorithmParameterException {
70        Mock_KeyGeneratorSpi kgSpi = new Mock_KeyGeneratorSpi();
71        assertNull("Not null result", kgSpi.engineGenerateKey());
72        try {
73            kgSpi.engineInit(77, new SecureRandom());
74            fail("IllegalArgumentException must be thrown");
75        } catch (IllegalArgumentException e) {
76        }
77        try {
78            kgSpi.engineInit(new SecureRandom());
79            fail("IllegalArgumentException must be thrown");
80        } catch (IllegalArgumentException e) {
81        }
82        AlgorithmParameterSpec aps = null;
83        try {
84            kgSpi.engineInit(aps, new SecureRandom());
85            fail("InvalidAlgorithmParameterException must be thrown when parameter is null");
86        } catch (InvalidAlgorithmParameterException e) {
87        }
88        aps = new APSpecSpi();
89        kgSpi.engineInit(aps, new SecureRandom());
90    }
91
92}
93
94class APSpecSpi implements AlgorithmParameterSpec {
95
96}
97