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 Vladimir N. Molotkov
20 */
21
22package org.apache.harmony.security.tests.java.security;
23
24import java.security.*;
25import java.security.spec.InvalidKeySpecException;
26
27import junit.framework.TestCase;
28
29
30/**
31 * Tests for fields and methods of class <code>KeyPair</code>
32 */
33public class KeyPairTest extends TestCase {
34
35    private static class TestKeyPair {
36        static PublicKey getPublic() {
37            return new PublicKey() {
38                public String getAlgorithm() {
39                    return "never mind";
40                }
41
42                public String getFormat() {
43                    return "never mind";
44                }
45
46                public byte[] getEncoded() {
47                    return null;
48                }
49            };
50        }
51
52        static PrivateKey getPrivate() {
53            return new PrivateKey() {
54                public String getAlgorithm() {
55                    return "never mind";
56                }
57
58                public String getFormat() {
59                    return "never mind";
60                }
61
62                public byte[] getEncoded() {
63                    return null;
64                }
65            };
66        }
67    }
68
69    /**
70     * Constructor for KeyPairTest.
71     *
72     * @param name
73     */
74    public KeyPairTest(String name) {
75        super(name);
76    }
77
78    /**
79     * Test #1 for <code>KeyPair(PublicKey, PrivateKey)</code> constructor<br>
80     * Assertion: creates new <code>KeyPair</code> instance using valid
81     * parameters (both <code>null</code>)
82     */
83    public final void testKeyPair01() {
84        Object kp = new KeyPair(null, null);
85        assertTrue(kp instanceof KeyPair);
86    }
87
88    /**
89     * Test #2 for <code>KeyPair(PublicKey, PrivateKey)</code> constructor<br>
90     * Assertion: creates new <code>KeyPair</code> instance using valid
91     * parameters (both valid keys)
92     *
93     * @throws InvalidKeySpecException
94     */
95    public final void testKeyPair02() throws InvalidKeySpecException {
96        Object kp = new KeyPair(TestKeyPair.getPublic(), TestKeyPair.getPrivate());
97        assertTrue(kp instanceof KeyPair);
98    }
99
100    /**
101     * Test #1 for <code>getPrivate()</code> method<br>
102     * Assertion: returns private key (<code>null</code> in this case)
103     */
104    public final void testGetPrivate01() {
105        KeyPair kp = new KeyPair(null, null);
106        assertNull(kp.getPrivate());
107    }
108
109    /**
110     * Test #2 for <code>getPrivate()</code> method<br>
111     * Assertion: returns private key (valid private key in this case)
112     *
113     * @throws InvalidKeySpecException
114     */
115    public final void testGetPrivate02() throws InvalidKeySpecException {
116        PrivateKey pk = TestKeyPair.getPrivate();
117        KeyPair kp = new KeyPair(null, pk);
118        assertSame(pk, kp.getPrivate());
119    }
120
121    /**
122     * Test #1 for <code>getPublic()</code> method<br>
123     * Assertion: returns public key (<code>null</code> in this case)
124     */
125    public final void testGetPublic01() {
126        KeyPair kp = new KeyPair(null, null);
127        assertNull(kp.getPublic());
128    }
129
130    /**
131     * Test #2 for <code>getPublic()</code> method<br>
132     * Assertion: returns public key (valid public key in this case)
133     *
134     * @throws InvalidKeySpecException
135     */
136    public final void testGetPublic02() throws InvalidKeySpecException {
137        PublicKey pk = TestKeyPair.getPublic();
138        KeyPair kp = new KeyPair(pk, null);
139        assertSame(pk, kp.getPublic());
140    }
141
142}
143