PKIXParametersTest.java revision 561ee011997c6c2f1befbfaa9d5f0a99771c1d63
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 Vladimir N. Molotkov
20*/
21
22package org.apache.harmony.security.tests.java.security.cert;
23
24import java.io.IOException;
25import java.security.InvalidAlgorithmParameterException;
26import java.security.KeyStore;
27import java.security.KeyStoreException;
28import java.security.NoSuchAlgorithmException;
29import java.security.cert.CertPathParameters;
30import java.security.cert.CertPathValidatorException;
31import java.security.cert.CertStore;
32import java.security.cert.CollectionCertStoreParameters;
33import java.security.cert.PKIXCertPathChecker;
34import java.security.cert.PKIXParameters;
35import java.security.cert.X509CertSelector;
36import java.util.ArrayList;
37import java.util.Date;
38import java.util.HashSet;
39import java.util.List;
40import java.util.Set;
41
42import org.apache.harmony.security.tests.support.cert.TestUtils;
43
44import junit.framework.TestCase;
45
46/**
47 * Tests for <code>PKIXParameters</code> fields and methods
48 *
49 */
50public class PKIXParametersTest extends TestCase {
51    /**
52     * Some valid issuer name
53     */
54    private final static String testIssuer =
55        "CN=VM,OU=DRL Security,O=Intel,L=Novosibirsk,ST=NSO,C=RU";
56
57    /**
58     * Constructor for PKIXParametersTest.
59     * @param name
60     */
61    public PKIXParametersTest(String name) {
62        super(name);
63    }
64
65    //
66    // Tests
67    //
68
69    /**
70     * Test #1 for <code>PKIXParameters(Set)</code> constructor<br>
71     * Assertion: Creates an instance of <code>PKIXParameters</code> with the
72     * specified <code>Set</code> of most-trusted CAs. Each element of the set
73     * is a <code>TrustAnchor</code>
74     * @throws InvalidAlgorithmParameterException
75     */
76    public final void testPKIXParametersSet01()
77        throws InvalidAlgorithmParameterException {
78        Set taSet = TestUtils.getTrustAnchorSet();
79        if (taSet == null) {
80            fail(getName() + ": not performed (could not create test TrustAnchor set)");
81        }
82        // use valid parameter
83        CertPathParameters cpp = new PKIXParameters(taSet);
84        assertTrue(cpp instanceof PKIXParameters);
85    }
86
87    /**
88     * Test #2 for <code>PKIXParameters(Set)</code> constructor<br>
89     * Assertion: ... the <code>Set</code> is copied to protect against
90     * subsequent modifications
91     * @throws InvalidAlgorithmParameterException
92     */
93    public final void testPKIXParametersSet02()
94        throws InvalidAlgorithmParameterException {
95        Set taSet = TestUtils.getTrustAnchorSet();
96        if (taSet == null) {
97            fail(getName() + ": not performed (could not create test TrustAnchor set)");
98        }
99        HashSet originalSet = (HashSet)taSet;
100        HashSet originalSetCopy = (HashSet)originalSet.clone();
101        // create test object using originalSet
102        PKIXParameters pp = new PKIXParameters(originalSetCopy);
103        // modify originalSet
104        originalSetCopy.clear();
105        // check that test object's internal state
106        // has not been affected by the above modification
107        Set returnedSet = pp.getTrustAnchors();
108        assertEquals(originalSet, returnedSet);
109    }
110
111    /**
112     * Test #3 for <code>PKIXParameters(Set)</code> constructor<br>
113     * Assertion: <code>NullPointerException</code> -
114     * if the specified <code>Set</code> is null
115     */
116    public final void testPKIXParametersSet03() throws Exception {
117        try {
118            // pass null
119            new PKIXParameters((Set)null);
120            fail("NPE expected");
121        } catch (NullPointerException e) {
122        }
123    }
124
125    /**
126     * Test #4 for <code>PKIXParameters(Set)</code> constructor<br>
127     * Assertion: <code>InvalidAlgorithmParameterException</code> -
128     * if the specified <code>Set</code> is empty
129     * (<code>trustAnchors.isEmpty() == true</code>)
130     */
131    public final void testPKIXParametersSet04() {
132        try {
133            // use empty set
134            new PKIXParameters(new HashSet());
135            fail("InvalidAlgorithmParameterException expected");
136        } catch (InvalidAlgorithmParameterException e) {
137        }
138    }
139
140    /**
141     * Test #5 for <code>PKIXParameters(Set)</code> constructor<br>
142     * Assertion: <code>ClassCastException</code> -
143     * if any of the elements in the <code>Set</code> are not of type
144     * <code>java.security.cert.TrustAnchor</code>
145     */
146    public final void testPKIXParametersSet05() throws Exception {
147        Set taSet = TestUtils.getTrustAnchorSet();
148        if (taSet == null) {
149            fail(getName() + ": not performed (could not create test TrustAnchor set)");
150        }
151
152        // add wrong object to valid set
153        assertTrue(taSet.add(new Object()));
154        try {
155            new PKIXParameters(taSet);
156            fail("ClassCastException expected");
157        } catch (ClassCastException e) {
158        }
159    }
160
161    /**
162     * Test #3 for <code>PKIXParameters(KeyStore)</code> constructor<br>
163     * Assertion: <code>NullPointerException</code> -
164     * if the <code>keystore</code> is <code>null</code>
165     * @throws InvalidAlgorithmParameterException
166     * @throws KeyStoreException
167     */
168    public final void testPKIXParametersKeyStore03() throws Exception {
169        try {
170            // pass null
171            new PKIXParameters((KeyStore)null);
172            fail("NPE expected");
173        } catch (NullPointerException e) {
174        }
175    }
176
177    /**
178     * Test #1 for <code>getPolicyQualifiersRejected()</code> method<br>
179     * Assertion: When a <code>PKIXParameters</code> object is created,
180     * this flag is set to <code>true</code><br>
181     * Assertion: returns the current value of the PolicyQualifiersRejected flag
182     * @throws InvalidAlgorithmParameterException
183     */
184    public final void testGetPolicyQualifiersRejected() throws Exception {
185        Set taSet = TestUtils.getTrustAnchorSet();
186        if (taSet == null) {
187            fail(getName() + ": not performed (could not create test TrustAnchor set)");
188        }
189
190        PKIXParameters p = new PKIXParameters(taSet);
191        assertTrue(p.getPolicyQualifiersRejected());
192    }
193
194    /**
195     * Test for <code>setPolicyQualifiersRejected()</code> method<br>
196     * Assertion: set the new value of the
197     * <code>PolicyQualifiersRejected</code> flag
198     * @throws InvalidAlgorithmParameterException
199     */
200    public final void testSetPolicyQualifiersRejected() throws Exception {
201        Set taSet = TestUtils.getTrustAnchorSet();
202        if (taSet == null) {
203            fail(getName() + ": not performed (could not create test TrustAnchor set)");
204        }
205
206        PKIXParameters p = new PKIXParameters(taSet);
207        p.setPolicyQualifiersRejected(false);
208        assertFalse("setFalse",p.getPolicyQualifiersRejected());
209        p.setPolicyQualifiersRejected(true);
210        assertTrue("setTrue",p.getPolicyQualifiersRejected());
211    }
212
213    /**
214     * Test for <code>isAnyPolicyInhibited()</code> method<br>
215     * Assertion: returns <code>true</code> if the any policy
216     * OID is inhibited, <code>false</code> otherwise<br>
217     * Assertion: By default, the any policy OID is not inhibited
218     * (<code>isAnyPolicyInhibited()</code> returns false).
219     * @throws InvalidAlgorithmParameterException
220     */
221    public final void testIsAnyPolicyInhibited() throws Exception {
222        Set taSet = TestUtils.getTrustAnchorSet();
223        if (taSet == null) {
224            fail(getName() + ": not performed (could not create test TrustAnchor set)");
225        }
226
227        PKIXParameters p = new PKIXParameters(taSet);
228        assertFalse(p.isAnyPolicyInhibited());
229    }
230
231    /**
232     * Test for <code>setAnyPolicyInhibited()</code> method<br>
233     * Assertion: sets state to determine if the any policy OID
234     * should be processed if it is included in a certificate
235     * @throws InvalidAlgorithmParameterException
236     */
237    public final void testSetAnyPolicyInhibited() throws Exception {
238        Set taSet = TestUtils.getTrustAnchorSet();
239        if (taSet == null) {
240            fail(getName() + ": not performed (could not create test TrustAnchor set)");
241        }
242
243        PKIXParameters p = new PKIXParameters(taSet);
244        p.setAnyPolicyInhibited(true);
245        assertTrue("setTrue", p.isAnyPolicyInhibited());
246        p.setAnyPolicyInhibited(false);
247        assertFalse("setFalse", p.isAnyPolicyInhibited());
248    }
249
250    /**
251     * Test for <code>isExplicitPolicyRequired()</code> method<br>
252     * Assertion: returns <code>true</code> if explicit policy is required,
253     * <code>false</code> otherwise<br>
254     * Assertion: by default, the ExplicitPolicyRequired flag is false
255     * @throws InvalidAlgorithmParameterException
256     */
257    public final void testIsExplicitPolicyRequired() throws Exception {
258        Set taSet = TestUtils.getTrustAnchorSet();
259        if (taSet == null) {
260            fail(getName() + ": not performed (could not create test TrustAnchor set)");
261        }
262
263        PKIXParameters p = new PKIXParameters(taSet);
264        assertFalse(p.isExplicitPolicyRequired());
265    }
266
267    /**
268     * Test for <code>setExplicitPolicyRequired()</code> method<br>
269     * Assertion: sets the ExplicitPolicyRequired flag
270     * @throws InvalidAlgorithmParameterException
271     */
272    public final void testSetExplicitPolicyRequired() throws Exception {
273        Set taSet = TestUtils.getTrustAnchorSet();
274        if (taSet == null) {
275            fail(getName() + ": not performed (could not create test TrustAnchor set)");
276        }
277
278        PKIXParameters p = new PKIXParameters(taSet);
279        p.setExplicitPolicyRequired(true);
280        assertTrue("setTrue", p.isExplicitPolicyRequired());
281        p.setExplicitPolicyRequired(false);
282        assertFalse("setFalse", p.isExplicitPolicyRequired());
283    }
284
285    /**
286     * Test for <code>isPolicyMappingInhibited()</code> method<br>
287     * Assertion: returns true if policy mapping is inhibited, false otherwise
288     * Assertion: by default, policy mapping is not inhibited (the flag is false)
289     * @throws InvalidAlgorithmParameterException
290     */
291    public final void testIsPolicyMappingInhibited() throws Exception {
292        Set taSet = TestUtils.getTrustAnchorSet();
293        if (taSet == null) {
294            fail(getName() + ": not performed (could not create test TrustAnchor set)");
295        }
296
297        PKIXParameters p = new PKIXParameters(taSet);
298        assertFalse(p.isPolicyMappingInhibited());
299    }
300
301    /**
302     * Test for <code>setPolicyMappingInhibited()</code> method<br>
303     * Assertion: sets the PolicyMappingInhibited flag
304     * @throws InvalidAlgorithmParameterException
305     */
306    public final void testSetPolicyMappingInhibited() throws Exception {
307        Set taSet = TestUtils.getTrustAnchorSet();
308        if (taSet == null) {
309            fail(getName() + ": not performed (could not create test TrustAnchor set)");
310        }
311
312        PKIXParameters p = new PKIXParameters(taSet);
313        p.setPolicyMappingInhibited(true);
314        assertTrue("setTrue", p.isPolicyMappingInhibited());
315        p.setPolicyMappingInhibited(false);
316        assertFalse("setFalse", p.isPolicyMappingInhibited());
317    }
318
319    /**
320     * Test for <code>isPolicyMappingInhibited()</code> method<br>
321     * Assertion: returns the current value of the RevocationEnabled flag
322     * Assertion: when a <code>PKIXParameters</code> object is created,
323     * this flag is set to true
324     * @throws InvalidAlgorithmParameterException
325     */
326    public final void testIsRevocationEnabled() throws Exception {
327        Set taSet = TestUtils.getTrustAnchorSet();
328        if (taSet == null) {
329            fail(getName() + ": not performed (could not create test TrustAnchor set)");
330        }
331
332        PKIXParameters p = new PKIXParameters(taSet);
333        assertTrue(p.isRevocationEnabled());
334    }
335
336    /**
337     * Test for <code>isPolicyMappingInhibited()</code> method<br>
338     * Assertion: sets the RevocationEnabled flag
339     * @throws InvalidAlgorithmParameterException
340     */
341    public final void testSetRevocationEnabled() throws Exception {
342        Set taSet = TestUtils.getTrustAnchorSet();
343        if (taSet == null) {
344            fail(getName() + ": not performed (could not create test TrustAnchor set)");
345        }
346
347        PKIXParameters p = new PKIXParameters(taSet);
348        p.setRevocationEnabled(false);
349        assertFalse("setFalse", p.isRevocationEnabled());
350        p.setRevocationEnabled(true);
351        assertTrue("setTrue", p.isRevocationEnabled());
352    }
353
354    /**
355     * Test for <code>getSigProvider()</code> method<br>
356     * Assertion: returns the signature provider's name,
357     * or null if not set
358     * @throws InvalidAlgorithmParameterException
359     */
360    public final void testGetSigProvider() throws Exception {
361        Set taSet = TestUtils.getTrustAnchorSet();
362        if (taSet == null) {
363            fail(getName() + ": not performed (could not create test TrustAnchor set)");
364        }
365
366        PKIXParameters p = new PKIXParameters(taSet);
367        assertNull("not set", p.getSigProvider());
368        p.setSigProvider("Some Provider");
369        assertNotNull("set", p.getSigProvider());
370    }
371
372    /**
373     * Test for <code>setSigProvider(String)</code> method<br>
374     * Assertion: sets the signature provider's name
375     */
376    public final void testSetSigProvider() throws Exception {
377        Set taSet = TestUtils.getTrustAnchorSet();
378        if (taSet == null) {
379            fail(getName() + ": not performed (could not create test TrustAnchor set)");
380        }
381
382        PKIXParameters p = new PKIXParameters(taSet);
383        String sigProviderName = "Some Provider";
384        p.setSigProvider(sigProviderName);
385        assertTrue("set", sigProviderName.equals(p.getSigProvider()));
386        p.setSigProvider(null);
387        assertNull("unset", p.getSigProvider());
388    }
389
390    /**
391     * Test #1 for <code>getTargetCertConstraints()</code> method<br>
392     * Assertion: returns a <code>CertSelector</code> specifying
393     * the constraints on the target certificate (or <code>null</code>)
394     * @throws InvalidAlgorithmParameterException
395     */
396    public final void testGetTargetCertConstraints01() throws Exception {
397        Set taSet = TestUtils.getTrustAnchorSet();
398        if (taSet == null) {
399            fail(getName() + ": not performed (could not create test TrustAnchor set)");
400        }
401
402        PKIXParameters p = new PKIXParameters(taSet);
403        assertNull(p.getTargetCertConstraints());
404    }
405
406    /**
407     * Test #2 for <code>getTargetCertConstraints()</code> method<br>
408     * Assertion: note that the <code>CertSelector</code> returned
409     * is cloned to protect against subsequent modifications
410     * @throws InvalidAlgorithmParameterException
411     * @throws IOException
412     */
413    public final void testGetTargetCertConstraints02() throws Exception {
414        Set taSet = TestUtils.getTrustAnchorSet();
415        if (taSet == null) {
416            fail(getName() + ": not performed (could not create test TrustAnchor set)");
417        }
418
419        X509CertSelector x509cs = new X509CertSelector();
420        PKIXParameters p = new PKIXParameters(taSet);
421        p.setTargetCertConstraints(x509cs);
422        // get cert selector
423        X509CertSelector cs1 = (X509CertSelector)p.getTargetCertConstraints();
424        // modify returned selector
425        cs1.setIssuer(testIssuer);
426        // get cert selector again
427        X509CertSelector cs2 = (X509CertSelector)p.getTargetCertConstraints();
428        // check that selector is not the same
429        assertNotSame("notTheSame", cs1, cs2);
430        // check that selector's internal state has
431        // not been changed by above modification
432        assertFalse("stateNotChanged", testIssuer.equals(cs2.getIssuerAsString()));
433    }
434
435    /**
436     * Test for <code>setTargetCertConstraints(CertSelector)</code> method<br>
437     * Assertion: sets the required constraints on the target certificate.
438     * The constraints are specified as an instance of CertSelector<br>
439     * Assertion: ... If <code>null</code>, no constraints are defined
440     * @throws IOException
441     * @throws InvalidAlgorithmParameterException
442     */
443    public final void testSetTargetCertConstraints01() throws Exception {
444        Set taSet = TestUtils.getTrustAnchorSet();
445        if (taSet == null) {
446            fail(getName() + ": not performed (could not create test TrustAnchor set)");
447        }
448
449        X509CertSelector x509cs = new X509CertSelector();
450        x509cs.setIssuer(testIssuer);
451        PKIXParameters p = new PKIXParameters(taSet);
452        p.setTargetCertConstraints(x509cs);
453        assertEquals("set",
454          testIssuer,
455          ((X509CertSelector)p.getTargetCertConstraints()).getIssuerAsString());
456        p.setTargetCertConstraints(null);
457        assertNull("unset", p.getTargetCertConstraints());
458    }
459
460    /**
461     * Test #2 for <code>setTargetCertConstraints(CertSelector)</code> method<br>
462     * Assertion: ... the CertSelector specified is cloned to protect against
463     * subsequent modifications
464     * @throws IOException
465     * @throws InvalidAlgorithmParameterException
466     */
467    public final void testSetTargetCertConstraints02() throws Exception {
468        Set taSet = TestUtils.getTrustAnchorSet();
469        if (taSet == null) {
470            fail(getName() + ": not performed (could not create test TrustAnchor set)");
471        }
472
473        X509CertSelector x509cs = new X509CertSelector();
474        PKIXParameters p = new PKIXParameters(taSet);
475        p.setTargetCertConstraints(x509cs);
476        // modify selector
477        x509cs.setIssuer(testIssuer);
478        // get selector
479        X509CertSelector x509cs1 = (X509CertSelector)p.getTargetCertConstraints();
480        // check that selector's internal state has
481        // not been changed by above modification
482        assertFalse(testIssuer.equals(x509cs1.getIssuerAsString()));
483    }
484
485    /**
486     * Test #1 for <code>getCertStores()</code> method<br>
487     * Assertion: list ... (may be empty, but never <code>null</code>)
488     * @throws InvalidAlgorithmParameterException
489     */
490    public final void testGetCertStores01() throws Exception {
491        Set taSet = TestUtils.getTrustAnchorSet();
492        if (taSet == null) {
493            fail(getName() + ": not performed (could not create test TrustAnchor set)");
494        }
495
496        PKIXParameters p = new PKIXParameters(taSet);
497        assertNotNull("notNull", p.getCertStores());
498        assertTrue("isEmpty", p.getCertStores().isEmpty());
499    }
500
501    /**
502     * Test #2 for <code>getCertStores()</code> method<br>
503     * Assertion: returns an immutable <code>List</code>
504     * of <code>CertStores</code>
505     * @throws InvalidAlgorithmParameterException
506     */
507    public final void testGetCertStores02() throws Exception {
508        Set taSet = TestUtils.getTrustAnchorSet();
509        if (taSet == null) {
510            fail(getName() + ": not performed (could not create test TrustAnchor set)");
511        }
512
513        PKIXParameters p = new PKIXParameters(taSet);
514        List cs = p.getCertStores();
515
516        try {
517            // try to modify returned list
518            cs.add(new Object());
519            fail("must be immutable");
520        } catch (Exception e) {
521        }
522    }
523
524    /**
525     * Test #1 for <code>setCertStores(List)</code> method<br>
526     * Assertion: Sets the list of CertStores ...
527     * @throws NoSuchAlgorithmException
528     * @throws InvalidAlgorithmParameterException
529     */
530    public final void testSetCertStores01() throws Exception {
531        Set taSet = TestUtils.getTrustAnchorSet();
532        if (taSet == null) {
533            fail(getName() + ": not performed (could not create test TrustAnchor set)");
534        }
535
536        PKIXParameters p = new PKIXParameters(taSet);
537        p.setCertStores(TestUtils.getCollectionCertStoresList());
538        // check that list has been set
539        assertFalse(p.getCertStores().isEmpty());
540    }
541
542    /**
543     * Test #2 for <code>setCertStores(List)</code> method<br>
544     * Assertion: list ... may be <code>null</code>
545     * @throws InvalidAlgorithmParameterException
546     */
547    public final void testSetCertStores02() throws Exception {
548        Set taSet = TestUtils.getTrustAnchorSet();
549        if (taSet == null) {
550            fail(getName() + ": not performed (could not create test TrustAnchor set)");
551        }
552
553        PKIXParameters p = new PKIXParameters(taSet);
554        // add null
555        p.setCertStores(null);
556        // check that we have non null empty list now
557        assertNotNull("notNull1", p.getCertStores());
558        assertTrue("isEmpty1", p.getCertStores().isEmpty());
559        // add empty
560        p.setCertStores(new ArrayList());
561        assertNotNull("notNull2", p.getCertStores());
562        assertTrue("isEmpty2", p.getCertStores().isEmpty());
563    }
564
565    /**
566     * Test #3 for <code>setCertStores(List)</code> method<br>
567     * Assertion: list is copied to protect against subsequent modifications
568     * @throws NoSuchAlgorithmException
569     * @throws InvalidAlgorithmParameterException
570     */
571    public final void testSetCertStores03() throws Exception {
572        Set taSet = TestUtils.getTrustAnchorSet();
573        if (taSet == null) {
574            fail(getName() + ": not performed (could not create test TrustAnchor set)");
575        }
576
577        PKIXParameters p = new PKIXParameters(taSet);
578        List l = TestUtils.getCollectionCertStoresList();
579        p.setCertStores(l);
580        // modify list just set
581        l.clear();
582        // check that list maintained internally has
583        // not been changed by the above modification
584        assertFalse(p.getCertStores().isEmpty());
585    }
586
587    /**
588     * Test #4 for <code>setCertStores(List)</code> method<br>
589     * Assertion: <code>ClassCastException</code> -
590     * if any of the elements in the list are not of type
591     * <code>java.security.cert.CertStore</code>
592     * @throws InvalidAlgorithmParameterException
593     * @throws NoSuchAlgorithmException
594     */
595    public final void testSetCertStores04() throws Exception {
596        Set taSet = TestUtils.getTrustAnchorSet();
597        if (taSet == null) {
598            fail(getName() + ": not performed (could not create test TrustAnchor set)");
599        }
600
601        PKIXParameters p = new PKIXParameters(taSet);
602        List l = TestUtils.getCollectionCertStoresList();
603        // add wrong object to valid set
604        assertTrue(l.add(new Object()));
605
606        try {
607            p.setCertStores(l);
608            fail("ClassCastException expected");
609        } catch (ClassCastException e) {
610        }
611    }
612
613    /**
614     * Test #1 for <code>addCertStore(CertStore)</code> method<br>
615     * Assertion: adds a <code>CertStore</code> to the end of the
616     * list of <code>CertStores</code>
617     * @throws InvalidAlgorithmParameterException
618     * @throws NoSuchAlgorithmException
619     */
620    public final void testAddCertStore01() throws Exception {
621        Set taSet = TestUtils.getTrustAnchorSet();
622        if (taSet == null) {
623            fail(getName() + ": not performed (could not create test TrustAnchor set)");
624        }
625
626        PKIXParameters p = new PKIXParameters(taSet);
627        p.addCertStore(CertStore.getInstance("Collection",
628                new CollectionCertStoreParameters()));
629        assertFalse(p.getCertStores().isEmpty());
630    }
631
632    /**
633     * Test #2 for <code>addCertStore(CertStore)</code> method<br>
634     * Assertion: if <code>null</code>, the store is ignored (not added to list)
635     * @throws InvalidAlgorithmParameterException
636     */
637    public final void testAddCertStore02() throws Exception {
638        Set taSet = TestUtils.getTrustAnchorSet();
639        if (taSet == null) {
640            fail(getName() + ": not performed (could not create test TrustAnchor set)");
641        }
642
643        PKIXParameters p = new PKIXParameters(taSet);
644        p.addCertStore(null);
645        assertTrue(p.getCertStores().isEmpty());
646    }
647
648    /**
649     * Test #1 for <code>getCertPathCheckers()</code> method<br>
650     * Assertion: list ... may be empty, but not <code>null</code>
651     * @throws InvalidAlgorithmParameterException
652     */
653    public final void testGetCertPathCheckers01() throws Exception {
654        Set taSet = TestUtils.getTrustAnchorSet();
655        if (taSet == null) {
656            fail(getName() + ": not performed (could not create test TrustAnchor set)");
657        }
658
659        PKIXParameters p = new PKIXParameters(taSet);
660        List l = p.getCertPathCheckers();
661        assertNotNull("notNull", l);
662        assertTrue("isEmpty",l.isEmpty());
663    }
664
665    /**
666     * Test #2 for <code>getCertPathCheckers()</code> method<br>
667     * Assertion: returns an immutable <code>List</code>
668     * of <code>PKIXCertPathChecker</code>s
669     * @throws InvalidAlgorithmParameterException
670     */
671    public final void testGetCertPathCheckers02() throws Exception {
672        Set taSet = TestUtils.getTrustAnchorSet();
673        if (taSet == null) {
674            fail(getName() + ": not performed (could not create test TrustAnchor set)");
675        }
676
677        PKIXParameters p = new PKIXParameters(taSet);
678        List l = p.getCertPathCheckers();
679
680        try {
681            // try to modify returned list
682            l.add(new Object());
683            fail("must be immutable");
684        } catch (Exception e) {
685        }
686    }
687
688    /**
689     * Test #3 for <code>getCertPathCheckers()</code> method<br>
690     * Assertion: The returned List is immutable, and each
691     * <code>PKIXCertPathChecker</code> in the <code>List</code>
692     * is cloned to protect against subsequent modifications
693     * @throws InvalidAlgorithmParameterException
694     * @throws CertPathValidatorException
695     */
696    public final void testGetCertPathCheckers03() throws Exception {
697        Set taSet = TestUtils.getTrustAnchorSet();
698        if (taSet == null) {
699            fail(getName() + ": not performed (could not create test TrustAnchor set)");
700        }
701
702        PKIXParameters p = new PKIXParameters(taSet);
703        PKIXCertPathChecker cpc = TestUtils.getTestCertPathChecker();
704        List l = new ArrayList();
705        assertTrue("addedOk", l.add(cpc));
706        p.setCertPathCheckers(l);
707        // retrieve checker and modify it
708        PKIXCertPathChecker cpc1 = p.getCertPathCheckers().get(0);
709        cpc1.init(true);
710        assertTrue("modifiedOk", cpc1.isForwardCheckingSupported());
711        // retrieve checker again and check
712        // that its state has not been changed
713        // by the above modification
714        PKIXCertPathChecker cpc2 = p.getCertPathCheckers().get(0);
715        assertFalse("isCloned", cpc2.isForwardCheckingSupported());
716    }
717
718    /**
719     * Test #1 for <code>setCertPathCheckers(List)</code> method<br>
720     * Assertion: sets a <code>List</code> of additional
721     * certification path checkers
722     * @throws InvalidAlgorithmParameterException
723     */
724    public final void testSetCertPathCheckers01() throws Exception {
725        Set taSet = TestUtils.getTrustAnchorSet();
726        if (taSet == null) {
727            fail(getName() + ": not performed (could not create test TrustAnchor set)");
728        }
729
730        PKIXParameters p = new PKIXParameters(taSet);
731        PKIXCertPathChecker cpc = TestUtils.getTestCertPathChecker();
732        List l = new ArrayList();
733        assertTrue("addedOk", l.add(cpc));
734        p.setCertPathCheckers(l);
735        List l1 = p.getCertPathCheckers();
736        assertNotNull("notNull", l1);
737        assertFalse("isNotEmpty", l1.isEmpty());
738    }
739
740    /**
741     * Test #2 for <code>setCertPathCheckers(List)</code> method<br>
742     * Assertion: <code>List</code> ... may be null
743     * @throws InvalidAlgorithmParameterException
744     */
745    public final void testSetCertPathCheckers02() throws Exception {
746        Set taSet = TestUtils.getTrustAnchorSet();
747        if (taSet == null) {
748            fail(getName() + ": not performed (could not create test TrustAnchor set)");
749        }
750
751        PKIXParameters p = new PKIXParameters(taSet);
752        p.setCertPathCheckers(null);
753        List l1 = p.getCertPathCheckers();
754        assertNotNull("notNull1", l1);
755        assertTrue("isEmpty1", l1.isEmpty());
756        p.setCertPathCheckers(new ArrayList());
757        List l2 = p.getCertPathCheckers();
758        assertNotNull("notNull2", l2);
759        assertTrue("isEmpty2", l2.isEmpty());
760    }
761
762    /**
763     * Test #3 for <code>setCertPathCheckers(List)</code> method<br>
764     * Assertion: <code>List</code> supplied here is copied and each
765     * <code>PKIXCertPathChecker</code> in the list is cloned to protect
766     * against subsequent modifications
767     * @throws InvalidAlgorithmParameterException
768     */
769    public final void testSetCertPathCheckers03() throws Exception {
770        // checks that list copied
771        Set taSet = TestUtils.getTrustAnchorSet();
772        if (taSet == null) {
773            fail(getName() + ": not performed (could not create test TrustAnchor set)");
774        }
775
776        PKIXParameters p = new PKIXParameters(taSet);
777        PKIXCertPathChecker cpc = TestUtils.getTestCertPathChecker();
778        List l = new ArrayList();
779        assertTrue("addedOk", l.add(cpc));
780        p.setCertPathCheckers(l);
781        // modify list
782        l.clear();
783        // retrieve list and check
784        // that its state has not been changed
785        // by the above modification
786        assertFalse("isCopied", p.getCertPathCheckers().isEmpty());
787    }
788
789    /**
790     * Test #4 for <code>setCertPathCheckers(List)</code> method<br>
791     * Assertion: <code>List</code> supplied here is copied and each
792     * <code>PKIXCertPathChecker</code> in the list is cloned to protect
793     * against subsequent modifications
794     * @throws InvalidAlgorithmParameterException
795     * @throws InvalidAlgorithmParameterException
796     * @throws CertPathValidatorException
797     */
798    public final void testSetCertPathCheckers04() throws Exception {
799        // checks that checkers cloned
800        Set taSet = TestUtils.getTrustAnchorSet();
801        if (taSet == null) {
802            fail(getName() + ": not performed (could not create test TrustAnchor set)");
803        }
804
805        PKIXParameters p = new PKIXParameters(taSet);
806        PKIXCertPathChecker cpc = TestUtils.getTestCertPathChecker();
807        List l = new ArrayList();
808        assertTrue("addedOk", l.add(cpc));
809        p.setCertPathCheckers(l);
810        // modify checker
811        cpc.init(true);
812        // retrieve list and check that CertPathChecker's
813        // state it contains has not been changed by the
814        // above modification
815        PKIXCertPathChecker cpc1 = p.getCertPathCheckers().get(0);
816        assertFalse("isCopied", cpc1.isForwardCheckingSupported());
817    }
818
819    /**
820     * Test #5 for <code>setCertPathCheckers(List)</code> method<br>
821     * Assertion: <code>ClassCastException</code> -
822     * if any of the elements in the list are not of type
823     * <code>java.security.cert.PKIXCertPathChecker</code>
824     * @throws InvalidAlgorithmParameterException
825     */
826    public final void testSetCertPathCheckers05() throws Exception {
827        Set taSet = TestUtils.getTrustAnchorSet();
828        if (taSet == null) {
829            fail(getName() + ": not performed (could not create test TrustAnchor set)");
830        }
831
832        PKIXParameters p = new PKIXParameters(taSet);
833        PKIXCertPathChecker cpc = TestUtils.getTestCertPathChecker();
834        List l = new ArrayList();
835        assertTrue("addedOk", l.add(cpc));
836        // add wrong object to the list
837        assertTrue("addedOk", l.add(new Object()));
838
839        try {
840            p.setCertPathCheckers(l);
841            fail("ClassCastException expected");
842        } catch (ClassCastException e) {
843        }
844    }
845
846    /**
847     * Test #1 for <code>addCertPathChecker(PKIXCertPathChecker)</code> method<br>
848     * Assertion: adds a <code>CertPathChecker</code> to the end of the
849     * list of <code>CertPathChecker</code>s
850     * @throws CertPathValidatorException
851     */
852    public final void testAddCertPathChecker01() throws Exception {
853        Set taSet = TestUtils.getTrustAnchorSet();
854        if (taSet == null) {
855            fail(getName() + ": not performed (could not create test TrustAnchor set)");
856        }
857
858        PKIXParameters p = new PKIXParameters(taSet);
859        PKIXCertPathChecker cpc = TestUtils.getTestCertPathChecker();
860        List l = new ArrayList();
861        assertTrue("addedOk", l.add(cpc));
862        p.setCertPathCheckers(l);
863        // create one more PKIXCertPathChecker
864        PKIXCertPathChecker cpc1 = TestUtils.getTestCertPathChecker();
865        cpc1.init(true);
866        p.addCertPathChecker(cpc1);
867        // check that we have two PKIXCertPathCheckers and
868        // they are in right order
869        List l1 = p.getCertPathCheckers();
870        assertEquals("listSize", 2, l1.size());
871        assertFalse("order1",
872                ((PKIXCertPathChecker)l1.get(0)).isForwardCheckingSupported());
873        assertTrue("order2",
874                ((PKIXCertPathChecker)l1.get(1)).isForwardCheckingSupported());
875    }
876
877    /**
878     * Test #2 for <code>addCertPathChecker(PKIXCertPathChecker)</code> method<br>
879     * Assertion: if null, the checker is ignored (not added to list).
880     * @throws InvalidAlgorithmParameterException
881     */
882    public final void testAddCertPathChecker02() throws Exception {
883        Set taSet = TestUtils.getTrustAnchorSet();
884        if (taSet == null) {
885            fail(getName() + ": not performed (could not create test TrustAnchor set)");
886        }
887
888        PKIXParameters p = new PKIXParameters(taSet);
889        PKIXCertPathChecker cpc = TestUtils.getTestCertPathChecker();
890        List l = new ArrayList();
891        assertTrue("addedOk", l.add(cpc));
892        p.setCertPathCheckers(l);
893        // try to add null
894        p.addCertPathChecker(null);
895        // check that we have one PKIXCertPathChecker
896        List l1 = p.getCertPathCheckers();
897        assertEquals("listSize", 1, l1.size());
898    }
899
900    /**
901     * Test #3 for <code>addCertPathChecker(PKIXCertPathChecker)</code> method<br>
902     * Assertion: <code>PKIXCertPathChecker</code> is cloned to protect
903     * against subsequent modifications
904     * @throws InvalidAlgorithmParameterException
905     * @throws CertPathValidatorException
906     */
907    public final void testAddCertPathChecker03() throws Exception {
908        Set taSet = TestUtils.getTrustAnchorSet();
909        if (taSet == null) {
910            fail(getName() + ": not performed (could not create test TrustAnchor set)");
911        }
912
913        // checks that checkers cloned
914        PKIXParameters p = new PKIXParameters(taSet);
915        PKIXCertPathChecker cpc = TestUtils.getTestCertPathChecker();
916
917        p.addCertPathChecker(cpc);
918        // modify checker
919        cpc.init(true);
920        // retrieve list and check that CertPathChecker's
921        // state it contains has not been changed by the
922        // above modification
923        List l = p.getCertPathCheckers();
924        PKIXCertPathChecker cpc1 = (PKIXCertPathChecker)l.get(0);
925        assertEquals("listSize", 1, l.size());
926        assertFalse("isCopied", cpc1.isForwardCheckingSupported());
927    }
928
929    /**
930     * Test #1 for <code>getDate()</code> method<br>
931     * Assertion: the <code>Date</code>, or <code>null</code> if not set
932     * @throws InvalidAlgorithmParameterException
933     */
934    public final void testGetDate01() throws Exception {
935        Set taSet = TestUtils.getTrustAnchorSet();
936        if (taSet == null) {
937            fail(getName() + ": not performed (could not create test TrustAnchor set)");
938        }
939
940        PKIXParameters p = new PKIXParameters(taSet);
941        // the Date has not been set
942        // the method must return null
943        assertNull("null", p.getDate());
944        Date currentDate = new Date();
945        p.setDate(currentDate);
946        // the Date returned must match
947        assertEquals("notNull", currentDate, p.getDate());
948    }
949
950    /**
951     * Test #2 for <code>getDate()</code> method<br>
952     * Assertion: <code>Date</code> returned is copied to protect
953     * against subsequent modifications
954     * @throws InvalidAlgorithmParameterException
955     */
956    public final void testGetDate02() throws Exception {
957        Set taSet = TestUtils.getTrustAnchorSet();
958        if (taSet == null) {
959            fail(getName() + ": not performed (could not create test TrustAnchor set)");
960        }
961
962        PKIXParameters p = new PKIXParameters(taSet);
963        Date currentDate = new Date();
964        p.setDate((Date)currentDate.clone());
965        Date ret1 = p.getDate();
966        // modify Date returned
967        ret1.setTime(0L);
968        // check that internal Date has not been
969        // changed by the above modification
970        assertEquals(currentDate, p.getDate());
971    }
972
973    /**
974     * @tests java.security.cert.PKIXParameters#setDate(Date)
975     */
976    public final void test_setDateLjava_util_Date() throws Exception {
977        Set taSet = TestUtils.getTrustAnchorSet();
978        assertNotNull("could not create test TrustAnchor set", taSet);
979
980        // test: 'date' is unset and param is null
981        PKIXParameters p = new PKIXParameters(taSet);
982        p.setDate(null);
983        assertNull(p.getDate());
984
985        // test: 'date' is not null
986        p = new PKIXParameters(taSet);
987        Date toBeSet = new Date(555L);
988        p.setDate(toBeSet);
989        assertEquals(555L, p.getDate().getTime());
990        // modify initial 'date' - it should be copied by constructor
991        toBeSet.setTime(0L);
992        // check that internal 'date' has not been
993        // changed by the above modification
994        assertEquals(555L, p.getDate().getTime());
995        // set another 'date'
996        p.setDate(new Date(333L));
997        assertEquals(333L, p.getDate().getTime());
998
999        // Regression for HARMONY-2882 (non-bug difference from RI)
1000        p = new PKIXParameters(taSet);
1001        p.setDate(new Date(555L));
1002        p.setDate(null); // reset 'date' back to current time
1003        assertNull(p.getDate());
1004    }
1005
1006    /**
1007     * Test #1 for <code>getInitialPolicies()</code> method<br>
1008     * Assertion: The default return value is an empty <code>Set</code>
1009     * Assertion: Never returns <code>null</code>
1010     * @throws InvalidAlgorithmParameterException
1011     */
1012    public final void testGetInitialPolicies01() throws Exception {
1013        Set taSet = TestUtils.getTrustAnchorSet();
1014        if (taSet == null) {
1015            fail(getName() + ": not performed (could not create test TrustAnchor set)");
1016        }
1017
1018        PKIXParameters p = new PKIXParameters(taSet);
1019        assertNotNull("notNull", p.getInitialPolicies());
1020        assertTrue("isEmpty", p.getInitialPolicies().isEmpty());
1021    }
1022
1023    /**
1024     * Test #2 for <code>getInitialPolicies()</code> method<br>
1025     * Assertion: returns an immutable <code>Set</code> of initial
1026     * policy OIDs in <code>String</code> format<br>
1027     * @throws InvalidAlgorithmParameterException
1028     */
1029    public final void testGetInitialPolicies02() throws Exception {
1030        Set taSet = TestUtils.getTrustAnchorSet();
1031        if (taSet == null) {
1032            fail(getName() + ": not performed (could not create test TrustAnchor set)");
1033        }
1034
1035        PKIXParameters p = new PKIXParameters(taSet);
1036        Set s = p.getInitialPolicies();
1037        try {
1038            // try to modify returned set
1039            s.add(new Object());
1040            fail("must be immutable");
1041        } catch (Exception e) {
1042        }
1043    }
1044
1045    /**
1046     * Test #1 for <code>setInitialPolicies(Set)</code> method<br>
1047     * Assertion: sets the <code>Set</code> of initial policy
1048     * identifiers (OID strings)
1049     * @throws InvalidAlgorithmParameterException
1050     */
1051    public final void testSetInitialPolicies01() throws Exception {
1052        Set taSet = TestUtils.getTrustAnchorSet();
1053        if (taSet == null) {
1054            fail(getName() + ": not performed (could not create test TrustAnchor set)");
1055        }
1056
1057        Set s = new HashSet();
1058        s.add("1.2.3.4.5.6.7");
1059        PKIXParameters p = new PKIXParameters(taSet);
1060        p.setInitialPolicies(s);
1061        assertEquals(1, p.getInitialPolicies().size());
1062    }
1063
1064    /**
1065     * Test #2 for <code>setInitialPolicies(Set)</code> method<br>
1066     * Assertion: <code>Set</code> may be <code>null</code>
1067     * @throws InvalidAlgorithmParameterException
1068     */
1069    public final void testSetInitialPolicies02() throws Exception {
1070        Set taSet = TestUtils.getTrustAnchorSet();
1071        if (taSet == null) {
1072            fail(getName() + ": not performed (could not create test TrustAnchor set)");
1073        }
1074
1075        PKIXParameters p = new PKIXParameters(taSet);
1076        p.setInitialPolicies(null);
1077        assertTrue(p.getInitialPolicies().isEmpty());
1078    }
1079
1080    /**
1081     * Test #3 for <code>setInitialPolicies(Set)</code> method<br>
1082     * Assertion: <code>Set</code> may be empty
1083     */
1084    public final void testSetInitialPolicies03() throws Exception {
1085        Set taSet = TestUtils.getTrustAnchorSet();
1086        if (taSet == null) {
1087            fail(getName() + ": not performed (could not create test TrustAnchor set)");
1088        }
1089
1090        PKIXParameters p = new PKIXParameters(taSet);
1091        p.setInitialPolicies(new HashSet());
1092        assertTrue(p.getInitialPolicies().isEmpty());
1093    }
1094
1095    /**
1096     * Test #4 for <code>setInitialPolicies(Set)</code> method<br>
1097     * Assertion: <code>Set</code> is copied to protect against
1098     * subsequent modifications
1099     */
1100    public final void testSetInitialPolicies04() throws Exception {
1101        Set taSet = TestUtils.getTrustAnchorSet();
1102        if (taSet == null) {
1103            fail(getName() + ": not performed (could not create test TrustAnchor set)");
1104        }
1105
1106        Set s = new HashSet();
1107        s.add("1.2.3.4.5.6.7");
1108        s.add("1.2.3.4.5.6.8");
1109        PKIXParameters p = new PKIXParameters(taSet);
1110        p.setInitialPolicies(s);
1111        // modify original set
1112        s.clear();
1113        // check that set maintained internally has
1114        // not been changed by the above modification
1115        assertEquals(2, p.getInitialPolicies().size());
1116    }
1117
1118    /**
1119     * Test #5 for <code>setInitialPolicies(Set)</code> method<br>
1120     * Assertion: <code>ClassCastException</code> -
1121     * if any of the elements in the set are not of type <code>String</code>
1122     * @throws InvalidAlgorithmParameterException
1123     */
1124    public final void testSetInitialPolicies05() throws Exception {
1125        Set taSet = TestUtils.getTrustAnchorSet();
1126        if (taSet == null) {
1127            fail(getName() + ": not performed (could not create test TrustAnchor set)");
1128        }
1129
1130        Set s = new HashSet();
1131        s.add("1.2.3.4.5.6.7");
1132        s.add(new Object());
1133        PKIXParameters p = new PKIXParameters(taSet);
1134        try {
1135            p.setInitialPolicies(s);
1136            fail("ClassCastException expected");
1137        } catch (ClassCastException e) {
1138        }
1139    }
1140
1141    /**
1142     * Test #1 for <code>getTrustAnchors()</code> method<br>
1143     * Assertion: an immutable <code>Set</code> of <code>TrustAnchors</code>
1144     * (never <code>null</code>)
1145     * @throws InvalidAlgorithmParameterException
1146     */
1147    public final void testGetTrustAnchors01() throws Exception {
1148        Set taSet = TestUtils.getTrustAnchorSet();
1149        if (taSet == null) {
1150            fail(getName() + ": not performed (could not create test TrustAnchor set)");
1151        }
1152
1153        PKIXParameters p = new PKIXParameters(taSet);
1154        assertNotNull("notNull", p.getTrustAnchors());
1155    }
1156
1157    /**
1158     * Test #2 for <code>getTrustAnchors()</code> method<br>
1159     * Assertion: an immutable <code>Set</code> of <code>TrustAnchors</code>
1160     * (never <code>null</code>)
1161     * @throws InvalidAlgorithmParameterException
1162     */
1163    public final void testGetTrustAnchors02() throws Exception {
1164        Set taSet = TestUtils.getTrustAnchorSet();
1165        if (taSet == null) {
1166            fail(getName() + ": not performed (could not create test TrustAnchor set)");
1167        }
1168
1169        PKIXParameters p = new PKIXParameters(taSet);
1170        Set s = p.getTrustAnchors();
1171        try {
1172            // try to modify returned set
1173            s.add(new Object());
1174            fail("must be immutable");
1175        } catch (Exception e) {
1176        }
1177    }
1178
1179    /**
1180     * Test #1 for <code>setTrustAnchors(Set)</code> method<br>
1181     * Assertion: Sets the <code>Set</code> of most-trusted CAs
1182     * @throws InvalidAlgorithmParameterException
1183     */
1184    public final void testSetTrustAnchors01() throws Exception {
1185        Set taSet = TestUtils.getTrustAnchorSet();
1186        if (taSet == null) {
1187            fail(getName() + ": not performed (could not create test TrustAnchor set)");
1188        }
1189
1190        Set taSet1 = TestUtils.getTrustAnchorSet();
1191        PKIXParameters p = new PKIXParameters(taSet);
1192        p.setTrustAnchors(taSet1);
1193        assertFalse(p.getTrustAnchors().isEmpty());
1194    }
1195
1196    /**
1197     * Test #2 for <code>setTrustAnchors(Set)</code> method<br>
1198     * Assertion: <code>InvalidAlgorithmParameterException</code> -
1199     * if the specified <code>Set</code> is empty
1200     * (<code>trustAnchors.isEmpty() == true</code>)
1201     * @throws InvalidAlgorithmParameterException
1202     */
1203    public final void testSetTrustAnchors02() throws Exception {
1204        Set taSet = TestUtils.getTrustAnchorSet();
1205        if (taSet == null) {
1206            fail(getName() + ": not performed (could not create test TrustAnchor set)");
1207        }
1208
1209        PKIXParameters p = new PKIXParameters(taSet);
1210        try {
1211            // use empty set
1212            p.setTrustAnchors(new HashSet());
1213            fail("InvalidAlgorithmParameterException expected");
1214        } catch (InvalidAlgorithmParameterException e) {
1215        }
1216    }
1217
1218    /**
1219     * Test #3 for <code>setTrustAnchors(Set)</code> method<br>
1220     * Assertion: <code>NullPointerException</code> -
1221     * if the specified <code>Set</code> is <code>null</code>)
1222     */
1223    public final void testSetTrustAnchors03() throws Exception {
1224        Set taSet = TestUtils.getTrustAnchorSet();
1225        if (taSet == null) {
1226            fail(getName() + ": not performed (could not create test TrustAnchor set)");
1227        }
1228
1229        PKIXParameters p = new PKIXParameters(taSet);
1230        try {
1231            // use null
1232            p.setTrustAnchors(null);
1233            fail("NPE expected");
1234        } catch (NullPointerException e) {
1235        }
1236    }
1237
1238    /**
1239     * Test #4 for <code>setTrustAnchors(Set)</code> method<br>
1240     * Assertion: <code>ClassCastException</code> -
1241     * if any of the elements in the set are not of type
1242     * <code>java.security.cert.TrustAnchor</code>
1243     * @throws InvalidAlgorithmParameterException
1244     */
1245    public final void testSetTrustAnchors04() throws Exception {
1246        Set taSet = TestUtils.getTrustAnchorSet();
1247        if (taSet == null) {
1248            fail(getName() + ": not performed (could not create test TrustAnchor set)");
1249        }
1250
1251        PKIXParameters p = new PKIXParameters(taSet);
1252        Set s = new HashSet(p.getTrustAnchors());
1253        s.add(new Object());
1254        try {
1255            p.setTrustAnchors(s);
1256            fail("ClassCastException expected");
1257        } catch (ClassCastException e) {
1258        }
1259    }
1260
1261}
1262