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.ByteArrayInputStream;
25import java.io.ByteArrayOutputStream;
26import java.io.IOException;
27import java.security.KeyStore;
28import java.security.PrivateKey;
29import java.security.Provider;
30import java.security.Security;
31import java.security.cert.Certificate;
32
33import org.apache.harmony.security.tests.support.KeyStoreTestSupport;
34import org.apache.harmony.security.tests.support.SpiEngUtils;
35
36import junit.framework.TestCase;
37
38/**
39 * Tests for <code>KeyStore</code> constructor and methods
40 */
41
42public class KeyStore_Impl3Test extends TestCase {
43
44    private static final String KeyStoreProviderClass = "org.apache.harmony.security.tests.support.MyKeyStore";
45
46    private static final String defaultType = "KeyStore";
47
48    public static boolean KSSupported = false;
49
50    public static String defaultProviderName = null;
51
52    public static Provider defaultProvider = null;
53
54    private static String NotSupportMsg = "Default KeyStore type is not supported";
55
56    Provider mProv;
57
58    public KeyStore[] createKS() throws Exception {
59        assertTrue(NotSupportMsg, KSSupported);
60        KeyStore[] kpg = new KeyStore[3];
61
62        kpg[0] = KeyStore.getInstance(defaultType);
63        kpg[1] = KeyStore.getInstance(defaultType, defaultProvider);
64        kpg[2] = KeyStore.getInstance(defaultType, defaultProviderName);
65        return kpg;
66    }
67
68    protected void setUp() throws Exception {
69        super.setUp();
70        mProv = (new SpiEngUtils()).new MyProvider("MyKSProvider",
71                "Testing provider", KeyStoreTestSupport.srvKeyStore.concat(".")
72                .concat(defaultType), KeyStoreProviderClass);
73        Security.insertProviderAt(mProv, 2);
74        defaultProvider = SpiEngUtils.isSupport(defaultType,
75                KeyStoreTestSupport.srvKeyStore);
76        KSSupported = (defaultProvider != null);
77        defaultProviderName = (KSSupported ? defaultProvider.getName() : null);
78    }
79
80    /*
81     * @see TestCase#tearDown()
82     */
83    protected void tearDown() throws Exception {
84        super.tearDown();
85        Security.removeProvider(mProv.getName());
86    }
87
88    /**
89     * Test for <code>load(InputStream stream, char[] password)</code>
90     * <code>store(InputStream stream, char[] password)</code>
91     * <code>size()</code>
92     * <code>getCreationDate(String alias)</code>
93     * methods
94     * Assertions: store(...) throws NullPointerException when stream or
95     * password is null;
96     * getCreationDate(...) throws NullPointerException when alias is null;
97     * stores KeyStore and then loads it;
98     *
99     * @throws Exception
100     */
101
102    public void testLoadStore01() throws Exception {
103        assertTrue(NotSupportMsg, KSSupported);
104
105        String tType = "TestType";
106        KeyStore.TrustedCertificateEntry tCert = new KeyStore.TrustedCertificateEntry(
107                new KeyStoreTestSupport.MCertificate("type", new byte[0]));
108
109        Certificate certs[] = {
110                new KeyStoreTestSupport.MCertificate(tType, new byte[10]),
111                new KeyStoreTestSupport.MCertificate(tType, new byte[20]) };
112        PrivateKey pk = new KeyStoreTestSupport.MyPrivateKey(tType, "", new byte[10]);
113        KeyStore.PrivateKeyEntry pKey = new KeyStore.PrivateKeyEntry(pk, certs);
114        char[] pwd = { 'p', 'a', 's', 's', 'w', 'd' };
115        KeyStore.PasswordProtection pPath = new KeyStore.PasswordProtection(pwd);
116        String[] aliases = { "Alias1", "Alias2", "Alias3", "Alias4", "Alias5" };
117
118        KeyStore[] kss = createKS();
119        KeyStore[] kss1 = new KeyStore[kss.length];
120        assertNotNull("KeyStore objects were not created", kss);
121
122        for (int i = 0; i < kss.length; i++) {
123            kss1[i] = kss[i];
124            kss[i].load(null, null);
125            kss[i].setEntry(aliases[0], tCert, null);
126            kss[i].setEntry(aliases[1], pKey, pPath);
127            kss[i].setEntry(aliases[2], pKey, pPath);
128            try {
129                kss[i].getCreationDate(null);
130                fail("NullPointerException should be thrown when alias is null");
131            } catch (NullPointerException e) {
132            }
133
134            kss[i].setKeyEntry(aliases[3], pk, pwd, certs);
135            kss[i].setCertificateEntry(aliases[4], certs[0]);
136            ByteArrayOutputStream bos = new ByteArrayOutputStream();
137            try {
138                kss[i].store(null, pwd);
139                fail("IOException or NullPointerException should be thrown when stream is null");
140            } catch (IOException e) {
141            } catch (NullPointerException e) {
142            }
143
144            //RI does not throw exception while password is null.
145            kss[i].store(bos, null);
146
147            kss[i].store(bos, pwd);
148            ByteArrayInputStream bis = new ByteArrayInputStream(bos
149                    .toByteArray());
150            kss1[i].load(bis, pwd);
151            assertEquals("Incorrect size", kss1[i].size(), kss[i].size());
152            KeyStore.Entry en, en1;
153            for (int j = 0; j < 3; j++) {
154                en = kss[i].getEntry(aliases[j], (j == 0 ? null : pPath));
155                en1 = kss1[i].getEntry(aliases[j], (j == 0 ? null : pPath));
156                if (en instanceof KeyStore.TrustedCertificateEntry) {
157                    assertTrue("Incorrect entry 1",
158                            en1 instanceof KeyStore.TrustedCertificateEntry);
159                    assertEquals("Incorrect Certificate",
160                            ((KeyStore.TrustedCertificateEntry) en)
161                                    .getTrustedCertificate(),
162                            ((KeyStore.TrustedCertificateEntry) en1)
163                                    .getTrustedCertificate());
164                } else {
165                    if (en instanceof KeyStore.PrivateKeyEntry) {
166                        assertTrue("Incorrect entry 2",
167                                en1 instanceof KeyStore.PrivateKeyEntry);
168                        assertEquals(
169                                "Incorrect Certificate",
170                                ((KeyStore.PrivateKeyEntry) en).getPrivateKey(),
171                                ((KeyStore.PrivateKeyEntry) en1)
172                                        .getPrivateKey());
173                    } else {
174                        if (en instanceof KeyStore.SecretKeyEntry) {
175                            assertTrue("Incorrect entry 3",
176                                    en1 instanceof KeyStore.SecretKeyEntry);
177                            assertEquals("Incorrect Certificate",
178                                    ((KeyStore.SecretKeyEntry) en)
179                                            .getSecretKey(),
180                                    ((KeyStore.SecretKeyEntry) en1)
181                                            .getSecretKey());
182                        }
183                    }
184                }
185
186                assertEquals("Incorrect date", kss[i]
187                        .getCreationDate(aliases[j]), kss1[i]
188                        .getCreationDate(aliases[j]));
189            }
190            assertEquals("Incorrect entry", kss[i].getKey(aliases[3], pwd),
191                    kss1[i].getKey(aliases[3], pwd));
192            assertEquals("Incorrect date", kss[i].getCreationDate(aliases[3]),
193                    kss1[i].getCreationDate(aliases[3]));
194            assertEquals("Incorrect entry", kss[i].getCertificate(aliases[4]),
195                    kss1[i].getCertificate(aliases[4]));
196            assertEquals("Incorrect date", kss[i].getCreationDate(aliases[4]),
197                    kss1[i].getCreationDate(aliases[4]));
198        }
199    }
200}
201