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.security.tests.java.security;
19
20import junit.framework.TestCase;
21
22import org.apache.harmony.security.tests.java.security.AlgorithmParametersTest.MyAlgorithmParameters;
23
24import java.io.ByteArrayInputStream;
25import java.io.IOException;
26import java.io.InputStream;
27import java.io.OutputStream;
28import java.security.Key;
29import java.security.KeyPair;
30import java.security.KeyPairGenerator;
31import java.security.KeyStore;
32import java.security.KeyStoreException;
33import java.security.KeyStoreSpi;
34import java.security.NoSuchAlgorithmException;
35import java.security.Provider;
36import java.security.UnrecoverableKeyException;
37import java.security.cert.Certificate;
38import java.security.cert.CertificateException;
39import java.security.cert.CertificateFactory;
40import java.util.Date;
41import java.util.Enumeration;
42
43public class KeyStore3Test extends TestCase {
44
45    private KeyStore mockKeyStore;
46
47    private KeyPair keyPair;
48
49    private Certificate certificate;
50
51    public KeyStore3Test() throws Exception {
52        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("DSA");
53        keyPair = keyPairGenerator.generateKeyPair();
54
55        String certificateData = "-----BEGIN CERTIFICATE-----\n"
56                + "MIICZTCCAdICBQL3AAC2MA0GCSqGSIb3DQEBAgUAMF8xCzAJBgNVBAYTAlVTMSAw\n"
57                + "HgYDVQQKExdSU0EgRGF0YSBTZWN1cml0eSwgSW5jLjEuMCwGA1UECxMlU2VjdXJl\n"
58                + "IFNlcnZlciBDZXJ0aWZpY2F0aW9uIEF1dGhvcml0eTAeFw05NzAyMjAwMDAwMDBa\n"
59                + "Fw05ODAyMjAyMzU5NTlaMIGWMQswCQYDVQQGEwJVUzETMBEGA1UECBMKQ2FsaWZv\n"
60                + "cm5pYTESMBAGA1UEBxMJUGFsbyBBbHRvMR8wHQYDVQQKExZTdW4gTWljcm9zeXN0\n"
61                + "ZW1zLCBJbmMuMSEwHwYDVQQLExhUZXN0IGFuZCBFdmFsdWF0aW9uIE9ubHkxGjAY\n"
62                + "BgNVBAMTEWFyZ29uLmVuZy5zdW4uY29tMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCB\n"
63                + "iQKBgQCofmdY+PiUWN01FOzEewf+GaG+lFf132UpzATmYJkA4AEA/juW7jSi+LJk\n"
64                + "wJKi5GO4RyZoyimAL/5yIWDV6l1KlvxyKslr0REhMBaD/3Z3EsLTTEf5gVrQS6sT\n"
65                + "WMoSZAyzB39kFfsB6oUXNtV8+UKKxSxKbxvhQn267PeCz5VX2QIDAQABMA0GCSqG\n"
66                + "SIb3DQEBAgUAA34AXl3at6luiV/7I9MN5CXYoPJYI8Bcdc1hBagJvTMcmlqL2uOZ\n"
67                + "H9T5hNMEL9Tk6aI7yZPXcw/xI2K6pOR/FrMp0UwJmdxX7ljV6ZtUZf7pY492UqwC\n"
68                + "1777XQ9UEZyrKJvF5ntleeO0ayBqLGVKCWzWZX9YsXCpv47FNLZbupE=\n"
69                + "-----END CERTIFICATE-----\n";
70
71        ByteArrayInputStream certArray = new ByteArrayInputStream(
72                certificateData.getBytes());
73        CertificateFactory cf = CertificateFactory.getInstance("X.509");
74        certificate = cf.generateCertificate(certArray);
75    }
76
77    public void test_load() throws Exception {
78        // No exception should be thrown out.
79        mockKeyStore.load(null);
80    }
81
82    public void test_store() throws Exception {
83        try {
84            mockKeyStore.store(null);
85            fail("should throw KeyStoreException: not initialized");
86        } catch (KeyStoreException e) {
87            // expected
88        }
89
90        // No exception should be thrown out.
91        mockKeyStore.load(null, null);
92        mockKeyStore.store(null);
93    }
94
95    public void test_setKeyEntry_null() throws Exception {
96        mockKeyStore.load(null, null);
97        // No exception should be thrown out.
98        mockKeyStore.setKeyEntry(null, null, null, null);
99    }
100
101    public void test_setKeyEntry_key_is_null() throws Exception {
102        mockKeyStore.load(null, null);
103        // No exception should be thrown out.
104        mockKeyStore.setKeyEntry("Alias", null, null, new Certificate[]{certificate});
105    }
106
107    public void test_setKeyEntry_key_is_private() throws Exception {
108        mockKeyStore.load(null, null);
109        Key key = keyPair.getPrivate();
110        try {
111            mockKeyStore.setKeyEntry("Alias", key, null, null);
112            fail("should throw IllegalArgumentException");
113        } catch (IllegalArgumentException e) {
114            // expected
115        }
116
117        try {
118            mockKeyStore.setKeyEntry("Alias", key, null,
119                    new Certificate[0]);
120            fail("should throw IllegalArgumentException");
121        } catch (IllegalArgumentException e) {
122            // expected
123        }
124
125        mockKeyStore.setKeyEntry("Alias", key, null, new Certificate[]{certificate});
126    }
127
128    public void test_setKeyEntry_key_is_public() throws Exception
129    {
130        mockKeyStore.load(null, null);
131        Key key = keyPair.getPublic();
132        mockKeyStore.setKeyEntry("Alias1", key, null, null);
133        mockKeyStore.setKeyEntry("Alias2", key, null,
134                new Certificate[0]);
135        mockKeyStore.setKeyEntry("Alias3", key, null, new Certificate[]{certificate});
136    }
137
138    public void test_setCertificateEntry_null() throws Exception {
139        mockKeyStore.load(null, null);
140
141        mockKeyStore.setCertificateEntry(null, null);
142
143        mockKeyStore.setCertificateEntry(null, certificate);
144
145        mockKeyStore.setCertificateEntry("Alias", null);
146    }
147
148    @SuppressWarnings("cast")
149    public void test_KeyStore() {
150        Provider p = new MyProvider();
151        try {
152            MyKeyStore ks = new MyKeyStore(new MyKeyStoreSpi(), p, "MyKeyStore");
153            assertNotNull(ks);
154            assertTrue(ks instanceof KeyStore);
155        } catch (Exception e) {
156            fail("Exception should be not thrown");
157        }
158
159        try {
160            MyKeyStore ks = new MyKeyStore(null, null, null);
161            assertNotNull(ks);
162            assertTrue(ks instanceof KeyStore);
163        } catch (Exception e) {
164            fail("Exception should be not thrown");
165        }
166
167    }
168
169    protected void setUp() throws Exception {
170        super.setUp();
171        mockKeyStore = new MyKeyStore(new MyKeyStoreSpi(), null, "MyKeyStore");
172    }
173
174    private static class MyKeyStore extends KeyStore {
175
176        public MyKeyStore(KeyStoreSpi keyStoreSpi, Provider provider,
177                String type) {
178            super(keyStoreSpi, provider, type);
179        }
180    }
181
182    @SuppressWarnings("unused")
183    private static class MyKeyStoreSpi extends KeyStoreSpi {
184
185        public Enumeration<String> engineAliases() {
186            return null;
187        }
188
189        public boolean engineContainsAlias(String arg0) {
190            return false;
191        }
192
193        public void engineDeleteEntry(String arg0) throws KeyStoreException {
194        }
195
196        public Certificate engineGetCertificate(String arg0) {
197            return null;
198        }
199
200        public String engineGetCertificateAlias(Certificate arg0) {
201            return null;
202        }
203
204        public Certificate[] engineGetCertificateChain(String arg0) {
205            return null;
206        }
207
208        public Date engineGetCreationDate(String arg0) {
209            return null;
210        }
211
212        public Key engineGetKey(String arg0, char[] arg1)
213                throws NoSuchAlgorithmException, UnrecoverableKeyException {
214            return null;
215        }
216
217        public boolean engineIsCertificateEntry(String arg0) {
218            return false;
219        }
220
221        public boolean engineIsKeyEntry(String arg0) {
222            return false;
223        }
224
225        public void engineLoad(InputStream arg0, char[] arg1)
226                throws IOException, NoSuchAlgorithmException,
227                CertificateException {
228            return;
229        }
230
231        public void engineSetCertificateEntry(String arg0, Certificate arg1)
232                throws KeyStoreException {
233            return;
234        }
235
236        public void engineSetKeyEntry(String arg0, byte[] arg1,
237                Certificate[] arg2) throws KeyStoreException {
238            return;
239        }
240
241        public void engineSetKeyEntry(String arg0, Key arg1, char[] arg2,
242                Certificate[] arg3) throws KeyStoreException {
243            return;
244        }
245
246        public int engineSize() {
247            return 0;
248        }
249
250        public void engineStore(KeyStore.LoadStoreParameter param){
251            return;
252        }
253
254        public void engineStore(OutputStream arg0, char[] arg1)
255                throws IOException, NoSuchAlgorithmException,
256                CertificateException {
257            return;
258        }
259    }
260
261    @SuppressWarnings("serial")
262    private class MyProvider extends Provider {
263        MyProvider() {
264            super("MyProvider", 1.0, "Provider for testing");
265            put("AlgorithmParameters.ABC", MyAlgorithmParameters.class
266                    .getName());
267        }
268
269        MyProvider(String name, double version, String info) {
270            super(name, version, info);
271        }
272    }
273
274}
275