KeyStore_Impl2Test.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
18/**
19* @author Vera Y. Petrashkova
20*/
21
22package org.apache.harmony.security.tests.java.security;
23
24import java.io.ByteArrayOutputStream;
25import java.io.IOException;
26import java.security.KeyStore;
27import java.security.KeyStoreException;
28import java.security.NoSuchAlgorithmException;
29import java.security.NoSuchProviderException;
30import java.security.Provider;
31import java.security.Security;
32import java.security.UnrecoverableEntryException;
33import java.security.UnrecoverableKeyException;
34import java.security.cert.Certificate;
35import java.security.cert.CertificateException;
36import java.util.Date;
37
38import javax.crypto.spec.SecretKeySpec;
39
40import org.apache.harmony.security.tests.support.KeyStoreTestSupport;
41import org.apache.harmony.security.tests.support.MyLoadStoreParams;
42import org.apache.harmony.security.tests.support.SpiEngUtils;
43import org.apache.harmony.security.tests.support.cert.MyCertificate;
44
45import junit.framework.TestCase;
46
47/**
48 * Tests for <code>KeyStore</code> constructor and methods
49 *
50 */
51
52public class KeyStore_Impl2Test extends TestCase {
53
54    private static final String KeyStoreProviderClass =
55        "org.apache.harmony.security.tests.support.MyKeyStoreSpi";
56
57    private static final String defaultAlg = "KeyStore";
58
59    private static final String[] invalidValues = SpiEngUtils.invalidValues;
60
61    private static final String[] validValues;
62
63    static {
64        validValues = new String[4];
65        validValues[0] = defaultAlg;
66        validValues[1] = defaultAlg.toLowerCase();
67        validValues[2] = "kEyStOrE";
68        validValues[3] = "KeysTORE";
69    }
70
71    Provider mProv;
72
73    protected void setUp() throws Exception {
74        super.setUp();
75        mProv = (new SpiEngUtils()).new MyProvider("MyKSProvider",
76                "Testing provider", KeyStoreTestSupport.srvKeyStore.concat(".").concat(
77                        defaultAlg), KeyStoreProviderClass);
78        Security.insertProviderAt(mProv, 2);
79    }
80
81    /*
82     * @see TestCase#tearDown()
83     */
84    protected void tearDown() throws Exception {
85        super.tearDown();
86        Security.removeProvider(mProv.getName());
87    }
88
89    private void checkResult(KeyStore keyS) throws KeyStoreException,
90            IOException, CertificateException, NoSuchAlgorithmException,
91            UnrecoverableKeyException {
92        char[] pass = { 'a', 'b', 'c' };
93        String alias = "aaa";
94        keyS.load(null, pass);
95        assertNull("getKey must return null", keyS.getKey(alias, pass));
96        assertNull("getCertificate must return null", keyS
97                .getCertificate(alias));
98        assertNull("getCertificateChain must return null", keyS
99                .getCertificateChain(alias));
100        assertEquals("Incorrect result of getCreationDate", keyS
101                .getCreationDate(alias), new Date(0));
102        assertEquals("Incorrect result of size", keyS.size(), 0);
103        assertFalse("Incorrect result of isCertificateEntry", keyS
104                .isCertificateEntry(alias));
105        assertFalse("Incorrect result of isKeyEntry", keyS.isKeyEntry(alias));
106        assertFalse("Incorrect result of containsAlias", keyS
107                .containsAlias(alias));
108        assertEquals("Incorrect result of getCertificateAlias", keyS
109                .getCertificateAlias(null), "");
110        try {
111            keyS.setCertificateEntry(alias, null);
112            fail("KeyStoreException must be thrown because this method is not supported");
113        } catch (KeyStoreException e) {
114        }
115        try {
116            keyS.setEntry(alias, null, null);
117            fail("NullPointerException must be thrown entry is null");
118        } catch (NullPointerException e) {
119        }
120        KeyStore.TrustedCertificateEntry entry = new KeyStore.TrustedCertificateEntry(
121                new MyCertificate("type", new byte[0]));
122        try {
123            keyS.setEntry(alias, entry, null);
124            fail("KeyStoreException must be thrown because this method is not supported");
125        } catch (KeyStoreException e) {
126        }
127        try {
128            keyS.setKeyEntry(alias, new byte[0], null);
129            fail("KeyStoreException must be thrown because this method is not supported");
130        } catch (KeyStoreException e) {
131        }
132        try {
133            keyS.setKeyEntry(alias, null, null);
134            fail("KeyStoreException must be thrown because this method is not supported");
135        } catch (KeyStoreException e) {
136        }
137        try {
138            keyS.store(new ByteArrayOutputStream(), null);
139            fail("IOException must be thrown");
140        } catch (IOException e) {
141        }
142        try {
143            keyS.store(null, new char[0]);
144            fail("IOException or NullPointerException must be thrown for null OutputStream");
145        } catch (IOException e) {
146        } catch (NullPointerException e) {
147        }
148        ByteArrayOutputStream ba = new ByteArrayOutputStream();
149        try {
150            keyS.store(ba, new char[0]);
151            fail("IOException must be thrown");
152        } catch (IOException e) {
153        }
154
155        KeyStore.LoadStoreParameter lParam = new MyLoadStoreParams(
156                new KeyStore.PasswordProtection(new char[0]));
157        try {
158            keyS.store(null);
159            fail("UnsupportedOperationException must be thrown");
160        } catch (UnsupportedOperationException e) {
161        }
162
163
164        //No exception should be thrown out.
165        keyS.load(null);
166
167        try {
168            keyS.store(lParam);
169            fail("UnsupportedOperationException must be thrown");
170        } catch (UnsupportedOperationException e) {
171        }
172
173        keyS.load(lParam);
174
175        //make it compilable on 1.5
176        Class c = alias.getClass();
177        assertFalse("Incorrect result of entryInstanceOf", keyS
178                .entryInstanceOf(alias, c));
179    }
180
181    private void checkKeyStoreException(KeyStore keyS)
182            throws KeyStoreException, UnrecoverableKeyException,
183            UnrecoverableEntryException, NoSuchAlgorithmException, IOException,
184            CertificateException {
185        String alias = "aaa";
186        String eMsg = "IllegalStateException must be thrown because key store was not initialized";
187        try {
188            keyS.aliases();
189            fail(eMsg);
190        } catch (KeyStoreException e) {
191        }
192        try {
193            keyS.containsAlias(alias);
194            fail(eMsg);
195        } catch (KeyStoreException e) {
196        }
197        try {
198            keyS.deleteEntry(alias);
199            fail(eMsg);
200        } catch (KeyStoreException e) {
201        }
202        try {
203            //make it compilable on 1.5
204            Class c = alias.getClass();
205            keyS.entryInstanceOf(alias, c);
206            fail(eMsg);
207        } catch (KeyStoreException e) {
208        }
209        try {
210            keyS.getCertificate(alias);
211            fail(eMsg);
212        } catch (KeyStoreException e) {
213        }
214        MyCertificate mc = new MyCertificate("type", new byte[0]);
215        try {
216            keyS.getCertificateAlias(mc);
217            fail(eMsg);
218        } catch (KeyStoreException e) {
219        }
220        try {
221            keyS.getCertificateChain(alias);
222            fail(eMsg);
223        } catch (KeyStoreException e) {
224        }
225        try {
226            keyS.getCreationDate(alias);
227            fail(eMsg);
228        } catch (KeyStoreException e) {
229        }
230        KeyStore.PasswordProtection pp = new KeyStore.PasswordProtection(
231                new char[0]);
232        try {
233            keyS.getEntry(alias, pp);
234            fail(eMsg);
235        } catch (KeyStoreException e) {
236        }
237        try {
238            keyS.getKey(alias, new char[0]);
239            fail(eMsg);
240        } catch (KeyStoreException e) {
241        }
242        try {
243            keyS.isCertificateEntry(alias);
244            fail(eMsg);
245        } catch (KeyStoreException e) {
246        }
247        try {
248            keyS.isKeyEntry(alias);
249            fail(eMsg);
250        } catch (KeyStoreException e) {
251        }
252        KeyStore.TrustedCertificateEntry entry = new KeyStore.TrustedCertificateEntry(
253                mc);
254        Certificate[] chain = { mc };
255        try {
256            keyS.setEntry(alias, entry, pp);
257            fail(eMsg);
258        } catch (KeyStoreException e) {
259        }
260        try {
261            keyS.setKeyEntry(alias, new byte[0], chain);
262            fail(eMsg);
263        } catch (KeyStoreException e) {
264        }
265        SecretKeySpec sks = new SecretKeySpec(new byte[10], "type");
266        try {
267            keyS.setKeyEntry(alias, sks, new char[0], chain);
268            fail(eMsg);
269        } catch (KeyStoreException e) {
270        }
271        try {
272            keyS.size();
273            fail(eMsg);
274        } catch (KeyStoreException e) {
275        }
276        ByteArrayOutputStream ba = new ByteArrayOutputStream();
277        try {
278            keyS.store(ba, new char[0]);
279            fail(eMsg);
280        } catch (KeyStoreException e) {
281        }
282        KeyStore.LoadStoreParameter lParam = new MyLoadStoreParams(
283                new KeyStore.PasswordProtection(new char[0]));
284        try {
285            keyS.store(lParam);
286            fail(eMsg);
287        } catch (KeyStoreException e) {
288        }
289    }
290
291
292    /**
293     * Test for <code>getInstance(String type)</code> method
294     * Assertions:
295     * throws NullPointerException when type is null
296     * throws KeyStoreException when type is  not available;
297     * returns KeyStore object
298     *
299     */
300    public void testGetInstance01() throws KeyStoreException,
301            UnrecoverableKeyException, UnrecoverableEntryException,
302            NoSuchAlgorithmException, IOException, CertificateException {
303        try {
304            KeyStore.getInstance(null);
305            fail("NullPointerException or KeyStoreException must be thrown");
306        } catch (KeyStoreException e) {
307        } catch (NullPointerException e) {
308        }
309        for (int i = 0; i < invalidValues.length; i++) {
310            try {
311                KeyStore.getInstance(invalidValues[i]);
312                fail("KeyStoreException must be thrown (type: ".concat(
313                        invalidValues[i]).concat(")"));
314            } catch (KeyStoreException e) {
315            }
316        }
317        KeyStore keyS;
318        for (int i = 0; i < validValues.length; i++) {
319            keyS = KeyStore.getInstance(validValues[i]);
320            assertEquals("Incorrect type", keyS.getType(), validValues[i]);
321            assertEquals("Incorrect provider", keyS.getProvider(), mProv);
322            checkKeyStoreException(keyS);
323            checkResult(keyS);
324        }
325    }
326
327    /**
328     * Test for <code>getInstance(String type, String provider)</code> method
329     * Assertions:
330     * throws NullPointerException when type is null
331     * throws KeyStoreException when type is  not available;
332     * throws IllegalArgumentException when provider is null;
333     * throws NoSuchProviderException when provider is available;
334     * returns KeyStore object
335     */
336    public void testGetInstance02() throws KeyStoreException,
337            NoSuchProviderException, IllegalArgumentException,
338            UnrecoverableKeyException, UnrecoverableEntryException,
339            NoSuchAlgorithmException, IOException, CertificateException {
340        try {
341            KeyStore.getInstance(null, mProv.getName());
342            fail("NullPointerException or KeyStoreException must be thrown");
343        } catch (KeyStoreException e) {
344        } catch (NullPointerException e) {
345        }
346        for (int i = 0; i < invalidValues.length; i++) {
347            try {
348                KeyStore.getInstance(invalidValues[i], mProv.getName());
349                fail("KeyStoreException must be thrown (type: ".concat(
350                        invalidValues[i]).concat(")"));
351            } catch (KeyStoreException e) {
352            }
353        }
354        String prov = null;
355        for (int i = 0; i < validValues.length; i++) {
356            try {
357                KeyStore.getInstance(validValues[i], prov);
358                fail("IllegalArgumentException must be thrown when provider is null (type: "
359                        .concat(invalidValues[i]).concat(")"));
360            } catch (IllegalArgumentException e) {
361            }
362        }
363        for (int i = 0; i < validValues.length; i++) {
364            for (int j = 1; j < invalidValues.length; j++) {
365                try {
366                    KeyStore.getInstance(validValues[i], invalidValues[j]);
367                    fail("NoSuchProviderException must be thrown (type: "
368                            .concat(invalidValues[i]).concat(" provider: ")
369                            .concat(invalidValues[j]).concat(")"));
370                } catch (NoSuchProviderException e) {
371                }
372            }
373        }
374        KeyStore keyS;
375        for (int i = 0; i < validValues.length; i++) {
376            keyS = KeyStore.getInstance(validValues[i], mProv.getName());
377            assertEquals("Incorrect type", keyS.getType(), validValues[i]);
378            assertEquals("Incorrect provider", keyS.getProvider().getName(),
379                    mProv.getName());
380            checkKeyStoreException(keyS);
381            checkResult(keyS);
382        }
383    }
384
385    /**
386     * Test for <code>getInstance(String type, Provider provider)</code>
387     * method
388     * Assertions:
389     * throws NullPointerException when type is null
390        } catch (KeyStoreException e) {
391     * throws IllegalArgumentException when provider is null;
392     * returns KeyStore object
393     */
394    public void testGetInstance03() throws KeyStoreException,
395            IllegalArgumentException, UnrecoverableKeyException,
396            UnrecoverableEntryException, NoSuchAlgorithmException, IOException,
397            CertificateException {
398        try {
399            KeyStore.getInstance(null, mProv);
400            fail("KeyStoreException must be thrown");
401        } catch (KeyStoreException e) {
402        } catch (NullPointerException e) {
403        }
404        for (int i = 0; i < invalidValues.length; i++) {
405            try {
406                KeyStore.getInstance(invalidValues[i], mProv);
407                fail("KeyStoreException must be thrown (type: ".concat(
408                        invalidValues[i]).concat(")"));
409            } catch (KeyStoreException e) {
410            }
411        }
412        Provider prov = null;
413        for (int i = 0; i < validValues.length; i++) {
414            try {
415                KeyStore.getInstance(validValues[i], prov);
416                fail("IllegalArgumentException must be thrown when provider is null (type: "
417                        .concat(invalidValues[i]).concat(")"));
418            } catch (IllegalArgumentException e) {
419            }
420        }
421        KeyStore keyS;
422        for (int i = 0; i < validValues.length; i++) {
423            keyS = KeyStore.getInstance(validValues[i], mProv);
424            assertEquals("Incorrect type", keyS.getType(), validValues[i]);
425            assertEquals("Incorrect provider", keyS.getProvider(), mProv);
426            checkKeyStoreException(keyS);
427            checkResult(keyS);
428        }
429    }
430
431}
432