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.cert;
23
24import java.security.InvalidAlgorithmParameterException;
25import java.security.NoSuchAlgorithmException;
26import java.security.NoSuchProviderException;
27import java.security.Provider;
28import java.security.Security;
29import java.security.cert.CRLSelector;
30import java.security.cert.CertSelector;
31import java.security.cert.CertStore;
32import java.security.cert.CertStoreException;
33import java.security.cert.CertStoreParameters;
34import java.security.cert.X509CRLSelector;
35import java.security.cert.X509CertSelector;
36import java.util.Collection;
37
38import org.apache.harmony.security.tests.support.SpiEngUtils;
39import org.apache.harmony.security.tests.support.SpiEngUtils.MyProvider;
40import org.apache.harmony.security.tests.support.cert.MyCertStoreParameters;
41
42import junit.framework.TestCase;
43
44/**
45 * Tests for <code>CertStore</code> class constructors and methods
46 *
47 */
48
49public class CertStore_ImplTest extends TestCase {
50    private static final String srvCertStore = "CertStore";
51    private static final String defaultAlg = "CertStore";
52    private static final String CertStoreProviderClass = "org.apache.harmony.security.tests.support.cert.MyCertStoreSpi";
53
54    private static final String[] invalidValues = SpiEngUtils.invalidValues;
55
56    private static final String[] validValues;
57
58    static {
59        validValues = new String[4];
60        validValues[0] = defaultAlg;
61        validValues[1] = defaultAlg.toLowerCase();
62        validValues[2] = "CeRTSTore";
63        validValues[3] = "CERTstore";
64    }
65
66    Provider mProv;
67
68    protected void setUp() throws Exception {
69        super.setUp();
70        mProv = (new SpiEngUtils()).new MyProvider("MyCertStoreProvider",
71                "Provider for testing", srvCertStore
72                        .concat(".").concat(defaultAlg),
73                CertStoreProviderClass);
74        Security.insertProviderAt(mProv, 1);
75    }
76
77    /*
78     * @see TestCase#tearDown()
79     */
80    protected void tearDown() throws Exception {
81        super.tearDown();
82        Security.removeProvider(mProv.getName());
83    }
84
85    private void checkResult(CertStore certS)   throws CertStoreException,
86            InvalidAlgorithmParameterException {
87        CertSelector certSelector = new X509CertSelector();
88        CRLSelector crlSelector = new X509CRLSelector();
89        Collection collection = certS.getCertificates(certSelector);
90        assertNull("Not null collection", collection);
91        collection = certS.getCRLs(crlSelector);
92        assertNull("Not null collection", collection);
93    }
94
95    /**
96     * Test for method
97     * <code>getInstance(String type, CertStoreParameters params)</code>
98     * Assertions:
99     * throws NullPointerException when type is null
100     * throws NoSuchAlgorithmException when type is incorrect;
101     * throws InvalidAlgorithmParameterException  when params is null
102     * or has incorrect value;
103     * returns CertStore object
104     */
105    public void testGetInstance01() throws NoSuchAlgorithmException,
106        InvalidAlgorithmParameterException, CertStoreException {
107        CertStoreParameters p = new MyCertStoreParameters();
108        mPar mp = new mPar("CertStore");
109        try {
110            CertStore.getInstance(null, p);
111            fail("NullPointerException or NoSuchAlgorithmException must be thrown when type is null");
112        } catch (NullPointerException e) {
113        } catch (NoSuchAlgorithmException e) {
114        }
115        for (int i = 0; i < invalidValues.length; i++) {
116            try {
117                CertStore.getInstance(invalidValues[i], p);
118                fail("NoSuchAlgorithmException must be thrown (type: ".concat(
119                        invalidValues[i]).concat(")"));
120            } catch (NoSuchAlgorithmException e) {
121            }
122        }
123        for (int i = 0; i < validValues.length; i++) {
124            try {
125                CertStore.getInstance(validValues[i], null);
126                fail("InvalidAlgorithmParameterException must be thrown when params is null");
127            } catch (InvalidAlgorithmParameterException e) {
128            }
129            try {
130                CertStore.getInstance(validValues[i], mp);
131                fail("InvalidAlgorithmParameterException must be thrown when params is incorrect");
132            } catch (InvalidAlgorithmParameterException e) {
133            }
134        }
135
136        CertStore certS;
137        for (int i = 0; i < validValues.length; i++) {
138            certS = CertStore.getInstance(validValues[i], p);
139            assertEquals("Incorrect type", certS.getType(), validValues[i]);
140            assertEquals("Incorrect provider", certS.getProvider(), mProv);
141            assertTrue("Invalid parameters",certS.getCertStoreParameters() instanceof MyCertStoreParameters);
142            checkResult(certS);
143        }
144    }
145
146    /**
147     * Test for method
148     * <code>getInstance(String type, CertStoreParameters params, String provider)</code>
149     * Assertions:
150     * throws NullPointerException when type is null
151     * throws NoSuchAlgorithmException when type is incorrect;
152     * throws IllegalArgumentException when provider is null or empty;
153     * throws NoSuchProviderException when provider is available;
154     * throws InvalidAlgorithmParameterException  when params is null
155     * returns CertStore object
156     */
157    public void testGetInstance02() throws NoSuchAlgorithmException,
158            NoSuchProviderException, IllegalArgumentException,
159            InvalidAlgorithmParameterException, CertStoreException {
160        CertStoreParameters p = new MyCertStoreParameters();
161        mPar mp = new mPar("CertStore");
162        try {
163            CertStore.getInstance(null, p, mProv.getName());
164            fail("NullPointerException or NoSuchAlgorithmException must be thrown when type is null");
165        } catch (NullPointerException e) {
166        } catch (NoSuchAlgorithmException e) {
167        }
168        for (int i = 0; i < invalidValues.length; i++) {
169            try {
170                CertStore.getInstance(invalidValues[i], p, mProv
171                        .getName());
172                fail("NoSuchAlgorithmException must be thrown (type: ".concat(
173                        invalidValues[i]).concat(")"));
174            } catch (NoSuchAlgorithmException e) {
175            }
176        }
177        String prov = null;
178        for (int i = 0; i < validValues.length; i++) {
179            try {
180                CertStore.getInstance(validValues[i], p, prov);
181                fail("IllegalArgumentException must be thrown when provider is null (type: "
182                        .concat(validValues[i]).concat(")"));
183            } catch (IllegalArgumentException e) {
184            }
185            try {
186                CertStore.getInstance(validValues[i], p, "");
187                fail("IllegalArgumentException must be thrown when provider is empty (type: "
188                        .concat(validValues[i]).concat(")"));
189            } catch (IllegalArgumentException e) {
190            }
191        }
192        for (int i = 0; i < validValues.length; i++) {
193            for (int j = 1; j < invalidValues.length; j++) {
194                try {
195                    CertStore.getInstance(validValues[i], p,
196                            invalidValues[j]);
197                    fail("NoSuchProviderException must be thrown (type: "
198                            .concat(validValues[i]).concat(" provider: ")
199                            .concat(invalidValues[j]).concat(")"));
200                } catch (NoSuchProviderException e) {
201                }
202            }
203        }
204        for (int i = 0; i < validValues.length; i++) {
205            try {
206                CertStore.getInstance(validValues[i], null, mProv.getName());
207                fail("InvalidAlgorithmParameterException must be thrown when params is null");
208            } catch (InvalidAlgorithmParameterException e) {
209            }
210            try {
211                CertStore.getInstance(validValues[i], mp, mProv.getName());
212                fail("InvalidAlgorithmParameterException must be thrown when params is incorrect");
213            } catch (InvalidAlgorithmParameterException e) {
214            }
215        }
216        CertStore certS;
217        for (int i = 0; i < validValues.length; i++) {
218            certS = CertStore.getInstance(validValues[i], p, mProv
219                    .getName());
220            assertEquals("Incorrect type", certS.getType(), validValues[i]);
221            assertEquals("Incorrect provider", certS.getProvider().getName(),
222                    mProv.getName());
223            assertTrue("Invalid parameters",certS.getCertStoreParameters() instanceof MyCertStoreParameters);
224            checkResult(certS);
225        }
226    }
227
228    /**
229     * Test for method
230     * <code>getInstance(String type, CertStoreParameters params, Provider provider)</code>
231     * Assertions:
232     * throws NullPointerException when type is null
233     * throws NoSuchAlgorithmException when type is incorrect;
234     * throws IllegalArgumentException when provider is null;
235     * throws InvalidAlgorithmParameterException when params is null or has incorrect value
236     * returns CertStore object
237     */
238    public void testGetInstance03() throws NoSuchAlgorithmException,
239            IllegalArgumentException,
240            InvalidAlgorithmParameterException, CertStoreException {
241        CertStoreParameters p = new MyCertStoreParameters();
242        mPar mp = new mPar("CertStore");
243        try {
244            CertStore.getInstance(null, p, mProv);
245            fail("NullPointerException or NoSuchAlgorithmException must be thrown when type is null");
246        } catch (NullPointerException e) {
247        } catch (NoSuchAlgorithmException e) {
248        }
249        for (int i = 0; i < invalidValues.length; i++) {
250            try {
251                CertStore.getInstance(invalidValues[i], p, mProv);
252                fail("NoSuchAlgorithmException must be thrown (type: ".concat(
253                        invalidValues[i]).concat(")"));
254            } catch (NoSuchAlgorithmException e) {
255            }
256        }
257        Provider prov = null;
258        for (int i = 0; i < validValues.length; i++) {
259            try {
260                CertStore.getInstance(validValues[i], p, prov);
261                fail("IllegalArgumentException must be thrown when provider is null (type: "
262                        .concat(validValues[i]).concat(")"));
263            } catch (IllegalArgumentException e) {
264            }
265        }
266        for (int i = 0; i < validValues.length; i++) {
267            try {
268                CertStore.getInstance(validValues[i], null, mProv);
269                fail("InvalidAlgorithmParameterException must be thrown when params is null");
270            } catch (InvalidAlgorithmParameterException e) {
271            }
272            try {
273                CertStore.getInstance(validValues[i], mp, mProv);
274                fail("InvalidAlgorithmParameterException must be thrown when params is incorrect");
275            } catch (InvalidAlgorithmParameterException e) {
276            }
277        }
278        CertStore certS;
279        for (int i = 0; i < validValues.length; i++) {
280            certS = CertStore.getInstance(validValues[i], p, mProv);
281            assertEquals("Incorrect type", certS.getType(), validValues[i]);
282            assertEquals("Incorrect provider", certS.getProvider(), mProv);
283            assertTrue("Invalid parameters",certS.getCertStoreParameters() instanceof MyCertStoreParameters);
284            checkResult(certS);
285        }
286    }
287}
288class mPar implements CertStoreParameters {
289    String par = "";
290    public mPar() {
291        super();
292    }
293    public mPar(String s) {
294        super();
295        par = s;
296    }
297
298    public Object clone() {
299        if (par == "") {
300            return null;
301        }
302        try {
303            return super.clone();
304        } catch (CloneNotSupportedException e) {
305            return null;
306        }
307    }
308}
309