summaryrefslogtreecommitdiff
path: root/security/src/test/api/java/org/apache/harmony/security/tests/java/security/KSPrivateKeyEntryTest.java
blob: e594c3dd0ae291b84e607aee188700e8847011d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

/**
 * @author Vera Y. Petrashkova
 */

package org.apache.harmony.security.tests.java.security;

import java.security.*;
import java.security.cert.Certificate;

import org.apache.harmony.security.tests.support.cert.MyCertificate;

import junit.framework.TestCase;

import junit.framework.Test;
import junit.framework.TestSuite;

/**
 * Tests for <code>KeyStore.PrivateKeyEntry</code>  class constructor and methods
 */

public class KSPrivateKeyEntryTest extends TestCase {

    /**
     * Constructor for KSPrivateKeyEntryTest.
     *
     * @param arg0
     */
    public KSPrivateKeyEntryTest(String arg0) {
        super(arg0);
    }

    private PrivateKey testPrivateKey;
    private Certificate[] testChain;

    private void createParams(boolean diffCerts, boolean diffKeys) {
        byte[] encoded = { (byte) 0, (byte) 1, (byte) 2, (byte) 3 };
        testChain = new Certificate[5];
        for (int i = 0; i < testChain.length; i++) {
            String s = (diffCerts ? Integer.toString(i) : "NEW");
            testChain[i] = new MyCertificate("MY_TEST_CERTIFICATE_"
                    .concat(s), encoded);
        }
        testPrivateKey = (diffKeys ? (PrivateKey) new tmpPrivateKey() :
                (PrivateKey) new tmpPrivateKey(testChain[0].getPublicKey().getAlgorithm()));
    }

    /**
     * Test for <code>PrivateKeyEntry(PrivateKey privateKey, Certificate[] chain)</code>
     * constructor
     * Assertion: throws NullPointerException when privateKey is null
     */
    public void testPrivateKeyEntry01() {
        Certificate[] certs = new MyCertificate[1];//new Certificate[1];
        PrivateKey pk = null;
        try {
            new KeyStore.PrivateKeyEntry(pk, certs);
            fail("NullPointerException must be thrown when privateKey is null");
        } catch (NullPointerException e) {
        }
    }

    /**
     * Test for <code>PrivateKeyEntry(PrivateKey privateKey, Certificate[] chain)</code>
     * constructor
     * Assertion: throws NullPointerException when chain is null
     * and throws IllegalArgumentException when chain length is 0
     */
    public void testPrivateKeyEntry02() {
        Certificate[] chain = null;
        PrivateKey pk = new tmpPrivateKey();
        try {
            new KeyStore.PrivateKeyEntry(pk, chain);
            fail("NullPointerException must be thrown when chain is null");
        } catch (NullPointerException e) {
        }
        try {
            chain = new Certificate[0];
            new KeyStore.PrivateKeyEntry(pk, chain);
            fail("IllegalArgumentException must be thrown when chain length is 0");
        } catch (IllegalArgumentException e) {
        }
    }

    /**
     * Test for <code>PrivateKeyEntry(PrivateKey privateKey, Certificate[] chain)</code>
     * constructor
     * Assertion: throws IllegalArgumentException when chain contains certificates
     * of different types
     */
    public void testPrivateKeyEntry03() {
        createParams(true, false);
        try {
            new KeyStore.PrivateKeyEntry(testPrivateKey, testChain);
            fail("IllegalArgumentException must be thrown when chain contains certificates of different types");
        } catch (IllegalArgumentException e) {
        }
    }

    /**
     * Test for <code>PrivateKeyEntry(PrivateKey privateKey, Certificate[] chain)</code>
     * constructor
     * Assertion: throws IllegalArgumentException when algorithm of privateKey
     * does not match the algorithm of PublicKey in the end certificate (with 0 index)
     */
    public void testPrivateKeyEntry04() {
        createParams(false, true);
        try {
            new KeyStore.PrivateKeyEntry(testPrivateKey, testChain);
            fail("IllegalArgumentException must be thrown when key algorithms do not match");
        } catch (IllegalArgumentException e) {
        }
    }

    /**
     * Test for <code>getPrivateKey()</code> method
     * Assertion: returns PrivateKey object
     */
    public void testGetPrivateKey() {
        createParams(false, false);
        KeyStore.PrivateKeyEntry ksPKE = new KeyStore.PrivateKeyEntry(
                testPrivateKey, testChain);
        assertEquals("Incorrect PrivateKey", testPrivateKey, ksPKE
                .getPrivateKey());
    }

    /**
     * Test for <code>getCertificateChain()</code> method Assertion: returns
     * array of the Certificates corresponding to chain
     */
    public void testGetCertificateChain() {
        createParams(false, false);
        KeyStore.PrivateKeyEntry ksPKE = new KeyStore.PrivateKeyEntry(
                testPrivateKey, testChain);
        Certificate[] res = ksPKE.getCertificateChain();
        assertEquals("Incorrect chain length", testChain.length, res.length);
        for (int i = 0; i < res.length; i++) {
            assertEquals("Incorrect chain element: "
                    .concat(Integer.toString(i)), testChain[i], res[i]);
        }
    }

    /**
     * Test for <code>getCertificate()</code> method
     * Assertion: returns end Certificate (with 0 index in chain)
     */
    public void testGetCertificate() {
        createParams(false, false);
        KeyStore.PrivateKeyEntry ksPKE = new KeyStore.PrivateKeyEntry(
                testPrivateKey, testChain);
        Certificate res = ksPKE.getCertificate();
        assertEquals("Incorrect end certificate (number 0)", testChain[0], res);
    }

    /**
     * Test for <code>toString()</code> method
     * Assertion: returns non null String
     */
    public void testToString() {
        createParams(false, false);
        KeyStore.PrivateKeyEntry ksPKE = new KeyStore.PrivateKeyEntry(
                testPrivateKey, testChain);
        String res = ksPKE.toString();
        assertNotNull("toString() returns null", res);
    }

    public static Test suite() {
        return new TestSuite(KSPrivateKeyEntryTest.class);
    }

    private static class tmpPrivateKey implements PrivateKey {
        private String alg = "My algorithm";

        public String getAlgorithm() {
            return alg;
        }

        public String getFormat() {
            return "My Format";
        }

        public byte[] getEncoded() {
            return new byte[1];
        }

        public tmpPrivateKey() {
        }

        public tmpPrivateKey(String algorithm) {
            super();
            alg = algorithm;
        }
    }
}