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 javax.crypto;
23
24import java.security.AlgorithmParameters;
25import java.security.InvalidAlgorithmParameterException;
26import java.security.InvalidKeyException;
27import java.security.Key;
28import java.security.spec.AlgorithmParameterSpec;
29
30import org.apache.harmony.crypto.tests.support.MyExemptionMechanismSpi;
31
32import junit.framework.TestCase;
33
34
35/**
36 * Tests for <code>ExemptionMechanismSpi</code> class constructors and
37 * methods.
38 */
39
40public class ExemptionMechanismSpiTest extends TestCase {
41    /**
42     * Constructor for ExemptionMechanismSpiTests.
43     *
44     * @param arg0
45     */
46    public ExemptionMechanismSpiTest(String arg0) {
47        super(arg0);
48    }
49
50    /**
51     * Test for <code>ExemptionMechanismSpi</code> constructor Assertion:
52     * constructs ExemptionMechanismSpi
53     */
54    public void testExemptionMechanismSpi01()
55            throws ExemptionMechanismException,
56            ShortBufferException, InvalidKeyException,
57            InvalidAlgorithmParameterException {
58        ExemptionMechanismSpi emSpi = new MyExemptionMechanismSpi();
59        int len = MyExemptionMechanismSpi.getLength();
60        byte[] bbRes = emSpi.engineGenExemptionBlob();
61        assertEquals("Incorrect length", bbRes.length, len);
62        assertEquals("Incorrect result",
63                emSpi.engineGenExemptionBlob(new byte[1], len), len);
64        assertEquals("Incorrect output size", 10, emSpi.engineGetOutputSize(100));
65        Key key = null;
66        AlgorithmParameters params = null;
67        AlgorithmParameterSpec parSpec = null;
68        try {
69            emSpi.engineInit(key);
70            fail("InvalidKeyException must be thrown");
71        } catch (InvalidKeyException e) {
72        }
73        try {
74            emSpi.engineInit(key, params);
75            fail("InvalidKeyException must be thrown");
76        } catch (InvalidKeyException e) {
77        }
78        try {
79            emSpi.engineInit(key, parSpec);
80            fail("InvalidKeyException must be thrown");
81        } catch (InvalidKeyException e) {
82        }
83        key = ((MyExemptionMechanismSpi) emSpi).new tmp1Key("Proba", new byte[0]);
84        try {
85            emSpi.engineInit(key);
86            fail("ExemptionMechanismException must be thrown");
87        } catch (ExemptionMechanismException e) {
88        }
89        try {
90            emSpi.engineInit(key, params);
91            fail("ExemptionMechanismException must be thrown");
92        } catch (ExemptionMechanismException e) {
93        }
94        try {
95            emSpi.engineInit(key, parSpec);
96            fail("ExemptionMechanismException must be thrown");
97        } catch (ExemptionMechanismException e) {
98        }
99        key = ((MyExemptionMechanismSpi) emSpi).new tmpKey("Proba", new byte[0]);
100        emSpi.engineInit(key);
101        emSpi.engineInit(key, params);
102        emSpi.engineInit(key, parSpec);
103
104        assertEquals("Incorrect result", 10, emSpi.engineGetOutputSize(100));
105    }
106}
107