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* @version $Revision$
21*/
22
23package tests.security.cert;
24
25import junit.framework.TestCase;
26
27import org.apache.harmony.security.tests.support.SpiEngUtils;
28import org.apache.harmony.security.tests.support.cert.MyCertStoreParameters;
29import org.apache.harmony.security.tests.support.cert.MyCertStoreSpi;
30
31import java.security.InvalidAlgorithmParameterException;
32import java.security.NoSuchAlgorithmException;
33import java.security.NoSuchProviderException;
34import java.security.Provider;
35import java.security.Security;
36import java.security.cert.CertStore;
37import java.security.cert.CertStoreException;
38import java.security.cert.CertStoreParameters;
39import java.security.cert.CertStoreSpi;
40import java.security.cert.CollectionCertStoreParameters;
41import java.security.cert.LDAPCertStoreParameters;
42import java.util.Collection;
43
44/**
45 * Tests for <code>CertStore</code> class constructors and
46 * methods.
47 *
48 */
49public class CertStore1Test extends TestCase {
50
51    public static final String srvCertStore = "CertStore";
52
53    private static final String defaultType = "LDAP";
54    public static final String [] validValues =  {
55            "LDAP", "ldap", "Ldap", "lDAP", "lDaP" };
56    public static  String [] validValuesC = null;
57
58    private static String [] invalidValues = SpiEngUtils.invalidValues;
59
60    private static boolean LDAPSupport = false;
61    private static final String CollectionType = "Collection";
62    private static boolean CollectionSupport = false;
63
64    private static Provider defaultProvider;
65    private static String defaultProviderName;
66    private static Provider defaultProviderCol;
67    private static String defaultProviderColName;
68
69    private static String NotSupportMsg = "";
70
71    static {
72        defaultProvider = SpiEngUtils.isSupport(defaultType,
73                srvCertStore);
74        LDAPSupport = (defaultProvider != null);
75        defaultProviderName = (LDAPSupport ? defaultProvider.getName() : null);
76        NotSupportMsg = "LDAP and Collection algorithm are not supported";
77
78        defaultProviderCol = SpiEngUtils.isSupport(CollectionType,
79                srvCertStore);
80        CollectionSupport = (defaultProviderCol != null);
81        defaultProviderColName = (CollectionSupport ? defaultProviderCol.getName() : null);
82        if (CollectionSupport) {
83            validValuesC = new String[3];
84            validValuesC[0] = CollectionType;
85            validValuesC[1] = CollectionType.toUpperCase();
86            validValuesC[2] = CollectionType.toLowerCase();
87        }
88    }
89
90    private Provider dProv = null;
91    private String dName = null;
92    private String dType = null;
93    private CertStoreParameters dParams = null;
94    private String[] dValid;
95
96    private boolean initParams() {
97        if (!LDAPSupport && !CollectionSupport) {
98            fail(NotSupportMsg);
99            return false;
100        }
101        dParams = (CollectionSupport ? (CertStoreParameters)new CollectionCertStoreParameters() :
102            (CertStoreParameters)new LDAPCertStoreParameters());
103        dType = (CollectionSupport ? CollectionType : defaultType );
104        dProv = (CollectionSupport ? defaultProviderCol : defaultProvider );
105        dName = (CollectionSupport ? defaultProviderColName : defaultProviderName );
106        dValid = (CollectionSupport ? validValuesC : validValues );
107        return true;
108    }
109    private CertStore [] createCS() {
110        if (!LDAPSupport && !CollectionSupport) {
111            fail(NotSupportMsg);
112            return null;
113        }
114        try {
115            CertStore [] ss = new CertStore[3];
116            ss[0] = CertStore.getInstance(dType, dParams);
117            ss[1] = CertStore.getInstance(dType, dParams, dProv);
118            ss[2] = CertStore.getInstance(dType, dParams, dName);
119            return ss;
120        } catch (Exception e) {
121            return null;
122        }
123    }
124
125
126    /**
127     * Test for <code>getDefaultType()</code> method
128     * Assertion: returns security property "certstore.type" or "LDAP"
129     */
130    public void testCertStore01() {
131        if (!LDAPSupport) {
132            return;
133        }
134        String dt = CertStore.getDefaultType();
135        String sn = Security.getProperty("certstore.type");
136        String def = "Proba.cert.store.type";
137        if (sn == null) {
138            sn = defaultType;
139        }
140        assertNotNull("Default type have not be null", dt);
141        assertEquals("Incorrect default type", dt, sn);
142
143        Security.setProperty("certstore.type", def);
144        dt = CertStore.getDefaultType();
145        assertEquals("Incorrect default type", dt, def);
146        Security.setProperty("certstore.type", sn);
147        assertEquals("Incorrect default type", Security.getProperty("certstore.type"), sn );
148    }
149    /**
150     * Test for
151     * <code>CertStore</code> constructor
152     * Assertion: returns CertStore object
153     */
154    public void testCertStore02() throws InvalidAlgorithmParameterException, CertStoreException {
155        if (!initParams()) {
156            return;
157        }
158        MyCertStoreParameters pp = new MyCertStoreParameters();
159        CertStoreSpi spi = new MyCertStoreSpi(pp);
160        CertStore certS = new myCertStore(spi, dProv, dType, pp);
161        assertEquals("Incorrect algorithm", certS.getType(), dType);
162        assertEquals("Incorrect provider", certS.getProvider(), dProv);
163        assertTrue("Incorrect parameters", certS.getCertStoreParameters()
164                instanceof MyCertStoreParameters);
165        try {
166            certS.getCertificates(null);
167            fail("CertStoreException must be thrown");
168        } catch (CertStoreException e) {
169        }
170        certS = new myCertStore(null, null, null, null);
171        assertNull("Incorrect algorithm", certS.getType());
172        assertNull("Incorrect provider", certS.getProvider());
173        assertNull("Incorrect parameters", certS.getCertStoreParameters());
174        try {
175            certS.getCertificates(null);
176            fail("NullPointerException must be thrown");
177        } catch (NullPointerException e) {
178        }
179    }
180
181    /**
182     * Test for <code>getInstance(String type, CertStoreParameters params)</code> method
183     * Assertion:
184     * throws NullPointerException when type is null
185     * throws NoSuchAlgorithmException when type is incorrect;
186     */
187    public void testCertStore03() throws InvalidAlgorithmParameterException {
188        if (!initParams()) {
189            return;
190        }
191        try {
192            CertStore.getInstance(null, dParams);
193            fail("NullPointerException or NoSuchAlgorithmException must be thrown when type is null");
194        } catch (NullPointerException e) {
195        } catch (NoSuchAlgorithmException e) {
196        }
197        for (int i = 0; i < invalidValues.length; i++ ) {
198            try {
199                CertStore.getInstance(invalidValues[i], dParams);
200                fail("NoSuchAlgorithmException must be thrown");
201            } catch (NoSuchAlgorithmException e) {
202            }
203        }
204    }
205
206    /**
207     * Test for <code>getInstance(String type, CertStoreParameters params)</code> method
208     * Assertion: return CertStore object
209     */
210    public void testCertStore05()
211            throws InvalidAlgorithmParameterException, NoSuchAlgorithmException {
212        if (!initParams()) {
213            return;
214        }
215        CertStore certS;
216        for (int i = 0; i < dValid.length; i++) {
217            certS = CertStore.getInstance(dValid[i], dParams);
218            assertEquals("Incorrect type", certS.getType(), dValid[i]);
219            certS.getCertStoreParameters();
220        }
221    }
222    /**
223     * Test for method
224     * <code>getInstance(String type, CertStoreParameters params, String provider)</code>
225     * Assertion: throws IllegalArgumentException when provider is null or empty
226     *
227     */
228    public void testCertStore06()
229            throws InvalidAlgorithmParameterException, NoSuchAlgorithmException,
230                NoSuchProviderException {
231        if (!initParams()) {
232            return;
233        }
234        String provider = null;
235        for (int i = 0; i < dValid.length; i++) {
236            try {
237                CertStore.getInstance(dValid[i], dParams, provider);
238                fail("IllegalArgumentException must be thrown");
239            } catch (IllegalArgumentException e) {
240            }
241            try {
242                CertStore.getInstance(dValid[i], dParams, "");
243                fail("IllegalArgumentException must be thrown");
244            } catch (IllegalArgumentException e) {
245            }
246        }
247    }
248    /**
249     * Test for method
250     * <code>getInstance(String type, CertStoreParameters params, String provider)</code>
251     * Assertion: throws NoSuchProviderException when provider has invalid value
252     */
253    public void testCertStore07()
254            throws InvalidAlgorithmParameterException, NoSuchAlgorithmException {
255        if (!initParams()) {
256            return;
257        }
258        for (int i = 0; i < dValid.length; i++) {
259            for (int j = 1; j < invalidValues.length; j++ ) {
260                try {
261                    CertStore.getInstance(dValid[i], dParams, invalidValues[j]);
262                    fail("NoSuchProviderException must be thrown");
263                } catch (NoSuchProviderException e) {
264                }
265            }
266        }
267    }
268    /**
269     * Test for method
270     * <code>getInstance(String type, CertStoreParameters params, String provider)</code>
271     * Assertion:
272     * throws NullPointerException when type is null
273     * throws NoSuchAlgorithmException when type is incorrect;
274     */
275    public void testCertStore08() throws InvalidAlgorithmParameterException,
276            NoSuchProviderException {
277        if (!initParams()) {
278            return;
279        }
280        for (int i = 0; i < invalidValues.length; i++) {
281            try {
282                CertStore.getInstance(invalidValues[i], dParams, dName);
283                fail("NoSuchAlgorithmException must be thrown");
284            } catch (NoSuchAlgorithmException e){
285            }
286        }
287        try {
288            CertStore.getInstance(null, dParams, dName);
289            fail("NullPointerException or NoSuchAlgorithmException must be thrown when type is null");
290        } catch (NullPointerException e) {
291        } catch (NoSuchAlgorithmException e) {
292        }
293    }
294
295    /**
296     * Test for method
297     * <code>getInstance(String type, CertStoreParameters params, String provider)</code>
298     * Assertion: return CertStore object
299     */
300    public void testCertStore10()
301            throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchProviderException {
302        if (!initParams()) {
303            return;
304        }
305        CertStore certS;
306        for (int i = 0; i < dValid.length; i++) {
307            certS = CertStore.getInstance(dValid[i], dParams, dName);
308            assertEquals("Incorrect type", certS.getType(), dValid[i]);
309            certS.getCertStoreParameters();
310        }
311    }
312
313    /**
314     * Test for method
315     * <code>getInstance(String type, CertStoreParameters params, Provider provider)</code>
316     * Assertion: throws IllegalArgumentException when provider is null
317     */
318    public void testCertStore11() throws InvalidAlgorithmParameterException,
319            NoSuchAlgorithmException {
320        if (!initParams()) {
321            return;
322        }
323        Provider provider = null;
324        for (int i = 0; i < dValid.length; i++) {
325            try {
326                CertStore.getInstance(dValid[i], dParams, provider);
327                fail("IllegalArgumentException must be thrown");
328            } catch (IllegalArgumentException e) {
329            }
330        }
331    }
332    /**
333     * Test for <code>getInstance(String type, CertStoreParameters params, Provider provider)</code> method
334     * Assertion:
335     * throws NullPointerException when type is null
336     * throws NoSuchAlgorithmException when type is incorrect;
337     */
338    public void testCertStore12() throws InvalidAlgorithmParameterException {
339        if (!initParams()) {
340            return;
341        }
342        try {
343            CertStore.getInstance(null, dParams, dProv);
344            fail("NullPointerException or NoSuchAlgorithmException must be thrown when type is null");
345        } catch (NullPointerException e) {
346        } catch (NoSuchAlgorithmException e) {
347        }
348        for (int i = 0; i < invalidValues.length; i++ ) {
349            try {
350                CertStore.getInstance(invalidValues[i], dParams, dProv);
351                fail("NoSuchAlgorithmException must be thrown");
352            } catch (NoSuchAlgorithmException e) {
353            }
354        }
355    }
356
357    /**
358     * Test for method
359     * <code>getInstance(String type, CertStoreParameters params, Provider provider)</code>
360     * Assertion: return CertStore object
361     */
362    public void testCertStore14()
363            throws InvalidAlgorithmParameterException, NoSuchAlgorithmException {
364        if (!initParams()) {
365            return;
366        }
367        CertStore certS;
368        for (int i = 0; i < dValid.length; i++) {
369            certS = CertStore.getInstance(dValid[i], dParams, dProv);
370            assertEquals("Incorrect type", certS.getType(), dValid[i]);
371            certS.getCertStoreParameters();
372        }
373    }
374    /**
375     * Test for methods
376     * <code>getCertificates(CertSelector selector)</code>
377     * <code>getCRLs(CRLSelector selector)</code>
378     * Assertion: returns empty Collection when selector is null
379     */
380    public void testCertStore15() throws CertStoreException {
381        if (!initParams()) {
382            return;
383        }
384        CertStore [] certS = createCS();
385        assertNotNull("CertStore object were not created", certS);
386        Collection<?> coll;
387        for (int i = 0; i < certS.length; i++) {
388            coll = certS[i].getCertificates(null);
389            assertTrue("Result collection not empty",coll.isEmpty());
390            coll = certS[i].getCRLs(null);
391            assertTrue("Result collection not empty",coll.isEmpty());
392        }
393    }
394
395    /**
396     * Test for <code>getType()</code> method
397     */
398    public void testCertStore16() throws NoSuchAlgorithmException,
399            InvalidAlgorithmParameterException {
400        if (!initParams()) {
401            return;
402        }
403
404        CertStore certS;
405        for (int i = 0; i < dValid.length; i++) {
406            certS = CertStore.getInstance(dValid[i], dParams);
407            assertEquals("Incorrect type", certS.getType(), dValid[i]);
408
409            try {
410                certS = CertStore.getInstance(dValid[i], dParams,
411                        defaultProviderCol);
412                assertEquals("Incorrect type", certS.getType(), dValid[i]);
413            } catch (IllegalArgumentException e) {
414                fail("Unexpected IllegalArgumentException " + e.getMessage());
415            }
416
417            try {
418                certS = CertStore.getInstance(dValid[i], dParams,
419                        defaultProviderColName);
420                assertEquals("Incorrect type", certS.getType(), dValid[i]);
421            } catch (NoSuchProviderException e) {
422                fail("Unexpected IllegalArgumentException " + e.getMessage());
423            }
424        }
425    }
426
427    /**
428     * Test for <code>getProvider()</code> method
429     */
430    public void testCertStore17() throws NoSuchAlgorithmException,
431            InvalidAlgorithmParameterException {
432        if (!initParams()) {
433            return;
434        }
435
436        CertStore certS;
437        for (int i = 0; i < dValid.length; i++) {
438            try {
439                certS = CertStore.getInstance(dValid[i], dParams,
440                        defaultProviderCol);
441                assertEquals("Incorrect provider", certS.getProvider(),
442                        defaultProviderCol);
443            } catch (IllegalArgumentException e) {
444                fail("Unexpected IllegalArgumentException " + e.getMessage());
445            }
446
447            try {
448                certS = CertStore.getInstance(dValid[i], dParams,
449                        defaultProviderColName);
450                assertEquals("Incorrect provider", certS.getProvider(),
451                        defaultProviderCol);
452            } catch (NoSuchProviderException e) {
453                fail("Unexpected IllegalArgumentException " + e.getMessage());
454            }
455}
456    }
457
458    /**
459     * Test for <code>getCertStoreParameters()</code> method
460     */
461    public void testCertStore18() throws NoSuchAlgorithmException,
462            InvalidAlgorithmParameterException {
463        if (!initParams()) {
464            return;
465        }
466
467        CertStore certS;
468        for (int i = 0; i < dValid.length; i++) {
469            certS = CertStore.getInstance(dValid[i], dParams);
470            assertEquals("Incorrect parameters",
471                    ((CollectionCertStoreParameters) certS
472                            .getCertStoreParameters()).getCollection(),
473                    ((CollectionCertStoreParameters) dParams).getCollection());
474
475            try {
476                certS = CertStore.getInstance(dValid[i], dParams,
477                        defaultProviderCol);
478                assertEquals("Incorrect parameters",
479                        ((CollectionCertStoreParameters) certS
480                                .getCertStoreParameters()).getCollection(),
481                        ((CollectionCertStoreParameters) dParams)
482                                .getCollection());
483            } catch (IllegalArgumentException e) {
484                fail("Unexpected IllegalArgumentException " + e.getMessage());
485            }
486
487            try {
488                certS = CertStore.getInstance(dValid[i], dParams,
489                        defaultProviderColName);
490                assertEquals("Incorrect parameters",
491                        ((CollectionCertStoreParameters) certS
492                                .getCertStoreParameters()).getCollection(),
493                        ((CollectionCertStoreParameters) dParams)
494                                .getCollection());
495            } catch (NoSuchProviderException e) {
496                fail("Unexpected IllegalArgumentException " + e.getMessage());
497            }
498        }
499    }
500}
501
502/**
503 * Additional class to verify CertStore constructor
504 */
505class myCertStore extends CertStore {
506    public myCertStore(CertStoreSpi spi, Provider prov, String type, CertStoreParameters params) {
507        super(spi, prov, type, params);
508    }
509}
510