KeyManagerImplTest.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
18package org.apache.harmony.xnet.provider.jsse;
19
20import java.io.File;
21import java.io.FileInputStream;
22import java.net.Socket;
23import java.security.KeyStore;
24
25import junit.framework.TestCase;
26
27/**
28 * Tests for <code>KeyManagerImpl</code> constructor and methods
29 *
30 */
31public class KeyManagerImplTest extends TestCase {
32
33    public void testKeyManagerImpl1() throws Exception {
34        KeyStore ks = null;
35        ks = KeyStore.getInstance("BKS");
36        ks.load(null, null);
37
38        KeyManagerImpl km = new KeyManagerImpl(ks, new char[0]);
39        String[] keyType = {"RSA", "DSA"};
40        String al = km.chooseClientAlias(keyType, null, new Socket());
41        assertNull(al);
42
43        al = km.chooseEngineClientAlias(keyType, null, new SSLEngineImpl(null));
44        assertNull(al);
45
46        al = km.chooseEngineServerAlias("RSA", null, new SSLEngineImpl(null));
47        assertNull(al);
48
49        al = km.chooseServerAlias("RSA", null, new Socket());
50        assertNull(al);
51
52        assertNull(km.getClientAliases("RSA", null));
53
54        assertNull(km.getServerAliases("RSA", null));
55
56        assertNull(km.getCertificateChain("alias"));
57        assertNull(km.getPrivateKey("alias"));
58    }
59
60    public void testKeyManagerImpl2() throws Exception {
61
62        KeyStore ks = JSSETestData.getKeyStore();
63        char[] pwd = JSSETestData.KS_PASSWORD;
64
65        KeyManagerImpl km = new KeyManagerImpl(ks, pwd);
66        String[] keyType = { "RSA", "DSA" };
67        String al = km.chooseClientAlias(keyType, null, new Socket());
68        assertEquals("ssl_test_store", al);
69
70        al = km.chooseEngineClientAlias(keyType, null, new SSLEngineImpl(null));
71        assertEquals("ssl_test_store", al);
72
73        al = km.chooseEngineServerAlias("RSA", null, new SSLEngineImpl(null));
74        assertEquals("ssl_test_store", al);
75
76        al = km.chooseServerAlias("RSA", null, new Socket());
77        assertEquals("ssl_test_store", al);
78
79        assertTrue(km.getCertificateChain("ssl_test_store") != null);
80        assertTrue(km.getPrivateKey("ssl_test_store") != null);
81    }
82
83}
84