1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package libcore.java.security.cert;
18
19import junit.framework.TestCase;
20import java.security.InvalidAlgorithmParameterException;
21import java.security.KeyStore;
22import java.security.cert.PKIXParameters;
23import libcore.java.security.TestKeyStore;
24
25public class PKIXParametersTest extends TestCase {
26
27    public void testKeyStoreConstructor() throws Exception {
28        TestKeyStore server = TestKeyStore.getServer();
29        KeyStore.PrivateKeyEntry pke = server.getPrivateKey("RSA", "RSA");
30        char[] password = "password".toCharArray();
31
32        // contains CA and server certificates
33        assertEquals(2, new PKIXParameters(server.keyStore).getTrustAnchors().size());
34
35        // just copy server certificates
36        KeyStore ks = TestKeyStore.createKeyStore();
37        ks.setKeyEntry("key", pke.getPrivateKey(), password, pke.getCertificateChain());
38        ks.setCertificateEntry("cert", pke.getCertificateChain()[0]);
39        assertEquals(1, new PKIXParameters(ks).getTrustAnchors().size());
40
41        // should fail with just key, even though cert is present in key entry
42        try {
43            KeyStore keyOnly = TestKeyStore.createKeyStore();
44            keyOnly.setKeyEntry("key", pke.getPrivateKey(), password, pke.getCertificateChain());
45            new PKIXParameters(keyOnly);
46            fail();
47        } catch (InvalidAlgorithmParameterException expected) {
48        }
49
50        // should fail with empty KeyStore
51        try {
52            new PKIXParameters(TestKeyStore.createKeyStore());
53            fail();
54        } catch (InvalidAlgorithmParameterException expected) {
55        }
56    }
57}
58