KSPrivateKeyEntryTest.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.cert.Certificate;
26
27import org.apache.harmony.security.tests.support.cert.MyCertificate;
28
29import junit.framework.TestCase;
30
31import junit.framework.Test;
32import junit.framework.TestSuite;
33
34/**
35 * Tests for <code>KeyStore.PrivateKeyEntry</code>  class constructor and methods
36 *
37 */
38
39public class KSPrivateKeyEntryTest extends TestCase {
40
41    /**
42     * Constructor for KSPrivateKeyEntryTest.
43     * @param arg0
44     */
45    public KSPrivateKeyEntryTest(String arg0) {
46        super(arg0);
47    }
48    private PrivateKey testPrivateKey;
49    private Certificate [] testChain;
50
51    private void createParams(boolean diffCerts, boolean diffKeys) {
52        byte[] encoded = {(byte)0, (byte)1, (byte)2, (byte)3};
53        testChain = new Certificate[5];
54        for (int i = 0; i < testChain.length; i++) {
55            String s = (diffCerts ? Integer.toString(i) : "NEW");
56            testChain[i] = new MyCertificate("MY_TEST_CERTIFICATE_"
57                    .concat(s), encoded);
58        }
59        testPrivateKey = (diffKeys ? (PrivateKey)new tmpPrivateKey() :
60            (PrivateKey)new tmpPrivateKey(testChain[0].getPublicKey().getAlgorithm()));
61    }
62
63    /**
64     * Test for <code>PrivateKeyEntry(PrivateKey privateKey, Certificate[] chain)</code>
65     * constructor
66     * Assertion: throws NullPointerException when privateKey is null
67     */
68    public void testPrivateKeyEntry01() {
69        Certificate[] certs = new MyCertificate[1];//new Certificate[1];
70        PrivateKey pk = null;
71        try {
72            new KeyStore.PrivateKeyEntry(pk, certs);
73            fail("NullPointerException must be thrown when privateKey is null");
74        } catch (NullPointerException e) {
75        }
76    }
77
78    /**
79     * Test for <code>PrivateKeyEntry(PrivateKey privateKey, Certificate[] chain)</code>
80     * constructor
81     * Assertion: throws NullPointerException when chain is null
82     * and throws IllegalArgumentException when chain length is 0
83     */
84    public void testPrivateKeyEntry02() {
85        Certificate[] chain = null;
86        PrivateKey pk = new tmpPrivateKey();
87        try {
88            new KeyStore.PrivateKeyEntry(pk, chain);
89            fail("NullPointerException must be thrown when chain is null");
90        } catch (NullPointerException e) {
91        }
92        try {
93            chain = new Certificate[0];
94            new KeyStore.PrivateKeyEntry(pk, chain);
95            fail("IllegalArgumentException must be thrown when chain length is 0");
96        } catch (IllegalArgumentException e) {
97        }
98    }
99    /**
100     * Test for <code>PrivateKeyEntry(PrivateKey privateKey, Certificate[] chain)</code>
101     * constructor
102     * Assertion: throws IllegalArgumentException when chain contains certificates
103     * of different types
104     */
105    public void testPrivateKeyEntry03() {
106        createParams(true, false);
107        try {
108            new KeyStore.PrivateKeyEntry(testPrivateKey, testChain);
109            fail("IllegalArgumentException must be thrown when chain contains certificates of different types");
110        } catch (IllegalArgumentException e) {
111        }
112    }
113
114    /**
115     * Test for <code>PrivateKeyEntry(PrivateKey privateKey, Certificate[] chain)</code>
116     * constructor
117     * Assertion: throws IllegalArgumentException when algorithm of privateKey
118     * does not match the algorithm of PublicKey in the end certificate (with 0 index)
119     */
120    public void testPrivateKeyEntry04() {
121        createParams(false, true);
122        try {
123            new KeyStore.PrivateKeyEntry(testPrivateKey, testChain);
124            fail("IllegalArgumentException must be thrown when key algorithms do not match");
125        } catch (IllegalArgumentException e) {
126        }
127    }
128
129    /**
130     * Test for <code>getPrivateKey()</code> method
131     * Assertion: returns PrivateKey object
132     */
133    public void testGetPrivateKey() {
134        createParams(false, false);
135        KeyStore.PrivateKeyEntry ksPKE = new KeyStore.PrivateKeyEntry(
136                testPrivateKey, testChain);
137        assertEquals("Incorrect PrivateKey", testPrivateKey, ksPKE
138                .getPrivateKey());
139    }
140
141    /**
142     * Test for <code>getCertificateChain()</code> method Assertion: returns
143     * array of the Certificates corresponding to chain
144     */
145    public void testGetCertificateChain() {
146        createParams(false, false);
147        KeyStore.PrivateKeyEntry ksPKE = new KeyStore.PrivateKeyEntry(
148                testPrivateKey, testChain);
149        Certificate[] res = ksPKE.getCertificateChain();
150        assertEquals("Incorrect chain length", testChain.length, res.length);
151        for (int i = 0; i < res.length; i++) {
152            assertEquals("Incorrect chain element: "
153                    .concat(Integer.toString(i)), testChain[i], res[i]);
154        }
155    }
156
157    /**
158     * Test for <code>getCertificate()</code> method
159     * Assertion: returns end Certificate (with 0 index in chain)
160     */
161    public void testGetCertificate() {
162        createParams(false, false);
163        KeyStore.PrivateKeyEntry ksPKE = new KeyStore.PrivateKeyEntry(
164                testPrivateKey, testChain);
165        Certificate res = ksPKE.getCertificate();
166        assertEquals("Incorrect end certificate (number 0)", testChain[0], res);
167    }
168
169    /**
170     * Test for <code>toString()</code> method
171     * Assertion: returns non null String
172     */
173    public void testToString() {
174        createParams(false, false);
175        KeyStore.PrivateKeyEntry ksPKE = new KeyStore.PrivateKeyEntry(
176                testPrivateKey, testChain);
177        String res = ksPKE.toString();
178        assertNotNull("toString() returns null", res);
179    }
180
181    public static Test suite() {
182        return new TestSuite(KSPrivateKeyEntryTest.class);
183    }
184
185    private static class tmpPrivateKey implements PrivateKey {
186        private String alg = "My algorithm";
187
188        public String getAlgorithm() {
189            return alg;
190        }
191
192        public String getFormat() {
193            return "My Format";
194        }
195
196        public byte[] getEncoded() {
197            return new byte[1];
198        }
199
200        public tmpPrivateKey() {
201        }
202
203        public tmpPrivateKey(String algorithm) {
204            super();
205            alg = algorithm;
206        }
207    }
208}
209