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