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.tests.javax.net.ssl;
19
20import java.security.InvalidAlgorithmParameterException;
21import java.security.KeyStore;
22import java.security.KeyStoreException;
23import java.security.NoSuchAlgorithmException;
24import java.security.NoSuchProviderException;
25import java.security.Provider;
26import java.security.Security;
27import java.security.UnrecoverableKeyException;
28
29import javax.net.ssl.KeyManagerFactory;
30import javax.net.ssl.ManagerFactoryParameters;
31
32import org.apache.harmony.xnet.tests.support.SpiEngUtils;
33import org.apache.harmony.xnet.tests.support.MyKeyManagerFactorySpi;
34import junit.framework.TestCase;
35
36/**
37 * Tests for KeyManagerFactory class constructors and methods
38 */
39
40public class KeyManagerFactory2Test extends TestCase {
41    private static final String srvKeyManagerFactory = "KeyManagerFactory";
42
43    private static final String defaultAlg = "KeyMF";
44
45    private static final String KeyManagerFactoryProviderClass = "org.apache.harmony.xnet.tests.support.MyKeyManagerFactorySpi";
46
47    private static final String[] invalidValues = SpiEngUtils.invalidValues;
48
49    private static final String[] validValues;
50
51    static {
52        validValues = new String[4];
53        validValues[0] = defaultAlg;
54        validValues[1] = defaultAlg.toLowerCase();
55        validValues[2] = "Keymf";
56        validValues[3] = "kEYMF";
57    }
58
59    Provider mProv;
60
61    @Override
62    protected void setUp() throws Exception {
63        super.setUp();
64        mProv = (new SpiEngUtils()).new MyProvider("MyKMFProvider",
65                "Provider for testing", srvKeyManagerFactory.concat(".")
66                .concat(defaultAlg), KeyManagerFactoryProviderClass);
67        Security.insertProviderAt(mProv, 2);
68    }
69
70    /*
71     * @see TestCase#tearDown()
72     */
73    @Override
74    protected void tearDown() throws Exception {
75        super.tearDown();
76        Security.removeProvider(mProv.getName());
77    }
78
79    private void checkResult(KeyManagerFactory keyMF)
80            throws Exception {
81        KeyStore kStore = null;
82        ManagerFactoryParameters mfp = null;
83
84        char[] pass = { 'a', 'b', 'c' };
85
86        try {
87            keyMF.init(kStore, null);
88            fail("KeyStoreException must be thrown");
89        } catch (KeyStoreException e) {
90        }
91        try {
92            keyMF.init(kStore, pass);
93            fail("UnrecoverableKeyException must be thrown");
94        } catch (UnrecoverableKeyException e) {
95        }
96        try {
97            keyMF.init(mfp);
98            fail("InvalidAlgorithmParameterException must be thrown");
99        } catch (InvalidAlgorithmParameterException e) {
100        }
101        assertNull("getKeyManagers() should return null object", keyMF
102                .getKeyManagers());
103
104        try {
105            kStore = KeyStore.getInstance(KeyStore.getDefaultType());
106            kStore.load(null, null);
107        } catch (KeyStoreException e) {
108            fail("default keystore is not supported");
109            return;
110        }
111        keyMF.init(kStore, pass);
112
113        mfp = (ManagerFactoryParameters) new MyKeyManagerFactorySpi.Parameters(kStore, null);
114        try {
115            keyMF.init(mfp);
116            fail("InvalidAlgorithmParameterException must be thrown");
117        } catch (InvalidAlgorithmParameterException e) {
118        }
119        mfp = (ManagerFactoryParameters) new MyKeyManagerFactorySpi.Parameters(kStore, pass);
120        keyMF.init(mfp);
121    }
122
123    /**
124     * Test for <code>getInstance(String algorithm)</code> method
125     * Assertions:
126     * throws NullPointerException when algorithm is null;
127     * throws NoSuchAlgorithmException when algorithm is not correct;
128     * returns KeyManagerFactory object
129     */
130    public void testGetInstance01() throws Exception {
131        try {
132            KeyManagerFactory.getInstance(null);
133            fail("NoSuchAlgorithmException or NullPointerException should be thrown (algorithm is null");
134        } catch (NoSuchAlgorithmException e) {
135        } catch (NullPointerException e) {
136        }
137        for (int i = 0; i < invalidValues.length; i++) {
138            try {
139                KeyManagerFactory.getInstance(invalidValues[i]);
140                fail("NoSuchAlgorithmException must be thrown (algorithm: "
141                        .concat(invalidValues[i]).concat(")"));
142            } catch (NoSuchAlgorithmException e) {
143            }
144        }
145        KeyManagerFactory keyMF;
146        for (int i = 0; i < validValues.length; i++) {
147            keyMF = KeyManagerFactory.getInstance(validValues[i]);
148            assertTrue("Not instanceof KeyManagerFactory object",
149                    keyMF instanceof KeyManagerFactory);
150            assertEquals("Incorrect algorithm", keyMF.getAlgorithm(),
151                    validValues[i]);
152            assertEquals("Incorrect provider", keyMF.getProvider(), mProv);
153            checkResult(keyMF);
154        }
155    }
156
157    /**
158     * Test for <code>getInstance(String algorithm, String provider)</code>
159     * method
160     * Assertions:
161     * throws NullPointerException when algorithm is null;
162     * throws NoSuchAlgorithmException when algorithm is not correct;
163     * throws IllegalArgumentException when provider is null or empty;
164     * throws NoSuchProviderException when provider is available;
165     * returns KeyManagerFactory object
166     */
167    public void testGetInstance02() throws Exception {
168        try {
169            KeyManagerFactory.getInstance(null, mProv.getName());
170            fail("NoSuchAlgorithmException or NullPointerException should be thrown (algorithm is null");
171        } catch (NoSuchAlgorithmException e) {
172        } catch (NullPointerException e) {
173        }
174        for (int i = 0; i < invalidValues.length; i++) {
175            try {
176                KeyManagerFactory
177                        .getInstance(invalidValues[i], mProv.getName());
178                fail("NoSuchAlgorithmException must be thrown (algorithm: "
179                        .concat(invalidValues[i]).concat(")"));
180            } catch (NoSuchAlgorithmException e) {
181            }
182        }
183        String prov = null;
184        for (int i = 0; i < validValues.length; i++) {
185            try {
186                KeyManagerFactory.getInstance(validValues[i], prov);
187                fail("IllegalArgumentException must be thrown when provider is null (algorithm: "
188                        .concat(invalidValues[i]).concat(")"));
189            } catch (IllegalArgumentException e) {
190            }
191            try {
192                KeyManagerFactory.getInstance(validValues[i], "");
193                fail("IllegalArgumentException must be thrown when provider is empty (algorithm: "
194                        .concat(invalidValues[i]).concat(")"));
195            } catch (IllegalArgumentException e) {
196            }
197        }
198        for (int i = 0; i < validValues.length; i++) {
199            for (int j = 1; j < invalidValues.length; j++) {
200                try {
201                    KeyManagerFactory.getInstance(validValues[i],
202                            invalidValues[j]);
203                    fail("NoSuchProviderException must be thrown (algorithm: "
204                            .concat(invalidValues[i]).concat(" provider: ")
205                            .concat(invalidValues[j]).concat(")"));
206                } catch (NoSuchProviderException e) {
207                }
208            }
209        }
210        KeyManagerFactory keyMF;
211        for (int i = 0; i < validValues.length; i++) {
212            keyMF = KeyManagerFactory.getInstance(validValues[i], mProv
213                    .getName());
214            assertTrue("Not instanceof KeyManagerFactory object",
215                    keyMF instanceof KeyManagerFactory);
216            assertEquals("Incorrect algorithm", keyMF.getAlgorithm(),
217                    validValues[i]);
218            assertEquals("Incorrect provider", keyMF.getProvider().getName(),
219                    mProv.getName());
220            checkResult(keyMF);
221        }
222    }
223
224    /**
225     * Test for <code>getInstance(String algorithm, Provider provider)</code>
226     * method
227     * Assertions:
228     * throws NullPointerException when algorithm is null;
229     * throws NoSuchAlgorithmException when algorithm is not correct;
230     * throws IllegalArgumentException when provider is null;
231     * returns KeyManagerFactory object
232     */
233    public void testGetInstance03() throws Exception {
234        try {
235            KeyManagerFactory.getInstance(null, mProv);
236            fail("NoSuchAlgorithmException or NullPointerException should be thrown (algorithm is null");
237        } catch (NoSuchAlgorithmException e) {
238        } catch (NullPointerException e) {
239        }
240        for (int i = 0; i < invalidValues.length; i++) {
241            try {
242                KeyManagerFactory.getInstance(invalidValues[i], mProv);
243                fail("NoSuchAlgorithmException must be thrown (algorithm: "
244                        .concat(invalidValues[i]).concat(")"));
245            } catch (NoSuchAlgorithmException e) {
246            }
247        }
248        Provider prov = null;
249        for (int i = 0; i < validValues.length; i++) {
250            try {
251                KeyManagerFactory.getInstance(validValues[i], prov);
252                fail("IllegalArgumentException must be thrown when provider is null (algorithm: "
253                        .concat(invalidValues[i]).concat(")"));
254            } catch (IllegalArgumentException e) {
255            }
256        }
257        KeyManagerFactory keyMF;
258        for (int i = 0; i < validValues.length; i++) {
259            keyMF = KeyManagerFactory.getInstance(validValues[i], mProv);
260            assertTrue("Not instanceof KeyManagerFactory object",
261                    keyMF instanceof KeyManagerFactory);
262            assertEquals("Incorrect algorithm", keyMF.getAlgorithm(),
263                    validValues[i]);
264            assertEquals("Incorrect provider", keyMF.getProvider(), mProv);
265            checkResult(keyMF);
266        }
267    }
268}