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;
23import java.io.IOException;
24import java.io.InputStream;
25import java.security.KeyStore;
26import java.security.KeyStoreException;
27import java.security.KeyStoreSpi;
28import java.security.NoSuchAlgorithmException;
29import java.security.UnrecoverableEntryException;
30import java.security.UnrecoverableKeyException;
31import java.security.cert.Certificate;
32import java.security.cert.CertificateException;
33import java.util.Date;
34
35import junit.framework.TestCase;
36
37import org.apache.harmony.security.tests.support.MyKeyStoreSpi;
38import org.apache.harmony.security.tests.support.MyLoadStoreParams;
39import org.apache.harmony.security.tests.support.cert.MyCertificate;
40
41
42/**
43 * Tests for <code>KeyStoreSpi</code> constructor and methods
44 *
45 */
46
47public class KeyStoreSpiTest extends TestCase {
48
49    /**
50     * Constructor for KeyStoreSpi.
51     *
52     * @param arg0
53     */
54    public KeyStoreSpiTest(String arg0) {
55        super(arg0);
56    }
57
58    /**
59     * Test for <code>KeyStoreSpi()</code> constructor
60     * and the following methods:
61     * <code>engineLoad(KeyStore.LoadStoreParameter param)</code>
62     * <code>engineStore(KeyStore.LoadStoreParameter param)</code>
63     * <code>engineGetEntry(String alias, KeyStore.ProtectionParameter param)</code>
64     * <code>engineSetEntry(String alias, KeyStore.Entry entry, KeyStore.ProtectionParameter param)</code>
65     * Assertions:
66     * creates new KeyStoreSpi object;
67     * engineGetEntry(..) returns null entry;
68     * engineStore(..) throws UnexpectedOperationException;
69     * engineSetEntry(..) throws KeyStoreException or NullPointerException
70     */
71    public void testKeyStoteSpi01() throws IOException,
72            NoSuchAlgorithmException, CertificateException,
73            UnrecoverableEntryException, KeyStoreException {
74        KeyStoreSpi ksSpi = new MyKeyStoreSpi();
75
76        tmpEntry entry = new tmpEntry();
77        tmpProtection pPar = new tmpProtection();
78
79        try {
80            ksSpi.engineStore(null);
81        } catch (UnsupportedOperationException e) {
82        }
83        assertNull("Not null entry", ksSpi.engineGetEntry("aaa", null));
84        assertNull("Not null entry", ksSpi.engineGetEntry(null, pPar));
85        assertNull("Not null entry", ksSpi.engineGetEntry("aaa", pPar));
86
87        try {
88            ksSpi.engineSetEntry("", null, null);
89            fail("KeyStoreException or NullPointerException must be thrown");
90        } catch (KeyStoreException e) {
91        } catch (NullPointerException e) {
92        }
93
94        try {
95            ksSpi.engineSetEntry("", new KeyStore.TrustedCertificateEntry(
96                    new MyCertificate("type", new byte[0])), null);
97            fail("KeyStoreException must be thrown");
98        } catch (KeyStoreException e) {
99        }
100
101        try {
102            ksSpi.engineSetEntry("aaa", entry, null);
103            fail("KeyStoreException must be thrown");
104        } catch (KeyStoreException e) {
105        }
106    }
107
108
109    /**
110     * Test for <code>KeyStoreSpi()</code> constructor
111     * and abstract engine methods.
112     * Assertion: creates new KeyStoreSpi object.
113     */
114    public void testKeyStoteSpi02() throws NoSuchAlgorithmException,
115            UnrecoverableKeyException, CertificateException {
116        KeyStoreSpi ksSpi = new MyKeyStoreSpi();
117        assertNull("engineGetKey(..) must return null", ksSpi.engineGetKey("",
118                new char[0]));
119        assertNull("engineGetCertificateChain(..) must return null", ksSpi
120                .engineGetCertificateChain(""));
121        assertNull("engineGetCertificate(..) must return null", ksSpi
122                .engineGetCertificate(""));
123        assertEquals("engineGetCreationDate(..) must return Date(0)",
124                new Date(0), ksSpi.engineGetCreationDate(""));
125        try {
126            ksSpi.engineSetKeyEntry("", null, new char[0], new Certificate[0]);
127            fail("KeyStoreException must be thrown from engineSetKeyEntry(..)");
128        } catch (KeyStoreException e) {
129        }
130        try {
131            ksSpi.engineSetKeyEntry("", new byte[0], new Certificate[0]);
132            fail("KeyStoreException must be thrown from engineSetKeyEntry(..)");
133        } catch (KeyStoreException e) {
134        }
135        try {
136            ksSpi.engineSetCertificateEntry("", null);
137            fail("KeyStoreException must be thrown from engineSetCertificateEntry(..)");
138        } catch (KeyStoreException e) {
139        }
140        try {
141            ksSpi.engineDeleteEntry("");
142            fail("KeyStoreException must be thrown from engineDeleteEntry(..)");
143        } catch (KeyStoreException e) {
144        }
145        assertNull("engineAliases() must return null", ksSpi.engineAliases());
146        assertFalse("engineContainsAlias(..) must return false", ksSpi
147                .engineContainsAlias(""));
148        assertEquals("engineSize() must return 0", 0, ksSpi.engineSize());
149        try {
150            ksSpi.engineStore(null, null);
151            fail("IOException must be thrown");
152        } catch (IOException e) {
153        }
154    }
155
156    /**
157     * @tests java.security.KeyStoreSpi#engineLoad(KeyStore.LoadStoreParameter)
158     */
159    public void test_engineLoadLjava_security_KeyStore_LoadStoreParameter()
160            throws Exception {
161
162        final String msg = "error";
163
164        KeyStoreSpi ksSpi = new MyKeyStoreSpi() {
165            public void engineLoad(InputStream stream, char[] password) {
166                assertNull(stream);
167                assertNull(password);
168                throw new RuntimeException(msg);
169            }
170        };
171        try {
172            ksSpi.engineLoad(null);
173            fail("Should throw exception");
174        } catch (RuntimeException e) {
175            assertSame(msg, e.getMessage());
176        }
177
178        // test: protection parameter is null
179        try {
180            ksSpi.engineLoad(new MyLoadStoreParams(null));
181            fail("No expected UnsupportedOperationException");
182        } catch (UnsupportedOperationException e) {
183        }
184
185        // test: protection parameter is not instanceof
186        // PasswordProtection or CallbackHandlerProtection
187        try {
188            ksSpi.engineLoad(new MyLoadStoreParams(new tmpProtection()));
189            fail("No expected UnsupportedOperationException");
190        } catch (UnsupportedOperationException e) {
191        }
192    }
193
194}
195/**
196 * Additional class implements KeyStore.Entry interface
197 */
198class tmpEntry  implements KeyStore.Entry {
199}
200class tmpProtection implements KeyStore.ProtectionParameter {
201}
202