CertPathBuilder1Test.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 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.CertPathBuilder;
30import java.security.cert.CertPathBuilderException;
31import java.security.cert.CertPathBuilderSpi;
32import java.security.cert.CertificateException;
33
34import junit.framework.TestCase;
35
36import org.apache.harmony.security.tests.support.SpiEngUtils;
37import org.apache.harmony.security.tests.support.cert.MyCertPathBuilderSpi;
38
39import tests.support.Support_Exec;
40
41/**
42 * Tests for <code>CertPathBuilder</code> class constructors and
43 * methods.
44 *
45 */
46
47public class CertPathBuilder1Test extends TestCase {
48
49    /**
50     * Constructor for CertPathBuilderTests.
51     * @param name
52     */
53    public CertPathBuilder1Test(String name) {
54        super(name);
55    }
56    public static final String srvCertPathBuilder = "CertPathBuilder";
57
58    public static final String defaultType = "PKIX";
59    public static final String [] validValues = {
60            "PKIX", "pkix", "PkiX", "pKiX" };
61
62    private static String [] invalidValues = SpiEngUtils.invalidValues;
63
64    private static boolean PKIXSupport = false;
65
66    private static Provider defaultProvider;
67    private static String defaultProviderName;
68
69    private static String NotSupportMsg = "";
70
71    public static final String DEFAULT_TYPE_PROPERTY = "certpathbuilder.type";
72
73    static {
74        defaultProvider = SpiEngUtils.isSupport(defaultType,
75                srvCertPathBuilder);
76        PKIXSupport = (defaultProvider != null);
77        defaultProviderName = (PKIXSupport ? defaultProvider.getName() : null);
78        NotSupportMsg = defaultType.concat(" is not supported");
79    }
80    private static CertPathBuilder[] createCPBs() {
81        if (!PKIXSupport) {
82            fail(NotSupportMsg);
83            return null;
84        }
85        try {
86            CertPathBuilder[] certPBs = new CertPathBuilder[3];
87            certPBs[0] = CertPathBuilder.getInstance(defaultType);
88            certPBs[1] = CertPathBuilder.getInstance(defaultType,
89                    defaultProviderName);
90            certPBs[2] = CertPathBuilder.getInstance(defaultType,
91                    defaultProvider);
92            return certPBs;
93        } catch (Exception e) {
94            return null;
95        }
96    }
97
98    /**
99     * @tests java.security.cert.CertPathBuilder#getDefaultType()
100     */
101    public void test_getDefaultType() throws Exception {
102
103        // Regression for HARMONY-2785
104
105        // test: default value
106        assertNull(Security.getProperty(DEFAULT_TYPE_PROPERTY));
107        assertEquals("PKIX", CertPathBuilder.getDefaultType());
108
109        // test: security property. fork new VM to keep testing env. clean
110        Support_Exec.execJava(new String[] { DefaultType.class.getName() },
111                null, true);
112    }
113
114    public static class DefaultType {
115
116        public static void main(String[] args) {
117
118            Security.setProperty(DEFAULT_TYPE_PROPERTY, "MyType");
119            assertEquals("MyType", CertPathBuilder.getDefaultType());
120
121            Security.setProperty(DEFAULT_TYPE_PROPERTY, "AnotherType");
122            assertEquals("AnotherType", CertPathBuilder.getDefaultType());
123        }
124    }
125
126    /**
127     * Test for <code>getInstance(String algorithm)</code> method
128	 * Assertion:
129	 * throws NullPointerException when algorithm is null
130	 * throws NoSuchAlgorithmException when algorithm  is not correct
131	 * or it is not available
132     */
133    public void testCertPathBuilder02() throws NoSuchAlgorithmException {
134        try {
135            CertPathBuilder.getInstance(null);
136            fail("No expected NullPointerException");
137        } catch (NullPointerException e) {
138        }
139        for (int i = 0; i < invalidValues.length; i++) {
140            try {
141                CertPathBuilder.getInstance(invalidValues[i]);
142                fail("NoSuchAlgorithmException must be thrown");
143            } catch (NoSuchAlgorithmException e) {
144            }
145        }
146    }
147
148    /**
149     * Test for <code>getInstance(String algorithm)</code> method
150	 * Assertion: returns CertPathBuilder object
151     */
152    public void testCertPathBuilder03() throws NoSuchAlgorithmException  {
153        if (!PKIXSupport) {
154            fail(NotSupportMsg);
155            return;
156        }
157        for (int i = 0; i < validValues.length; i++) {
158            CertPathBuilder cpb = CertPathBuilder.getInstance(validValues[i]);
159            assertEquals("Incorrect algorithm", cpb.getAlgorithm(), validValues[i]);
160        }
161    }
162    /**
163     * Test for <code>getInstance(String algorithm, String provider)</code> method
164	 * Assertion: throws IllegalArgumentException when provider is null or empty
165	 *
166	 * FIXME: verify what exception will be thrown if provider is empty
167     */
168    public void testCertPathBuilder04()
169            throws NoSuchAlgorithmException, NoSuchProviderException  {
170        if (!PKIXSupport) {
171            fail(NotSupportMsg);
172            return;
173        }
174        String provider = null;
175        for (int i = 0; i < validValues.length; i++) {
176            try {
177                CertPathBuilder.getInstance(validValues[i], provider);
178                fail("IllegalArgumentException must be thrown thrown");
179            } catch (IllegalArgumentException e) {
180            }
181            try {
182                CertPathBuilder.getInstance(validValues[i], "");
183                fail("IllegalArgumentException must be thrown thrown");
184            } catch (IllegalArgumentException e) {
185            }
186        }
187    }
188
189    /**
190     * Test for <code>getInstance(String algorithm, String provider)</code> method
191	 * Assertion:
192	 * throws NoSuchProviderException when provider has invalid value
193     */
194    public void testCertPathBuilder05()
195            throws NoSuchAlgorithmException  {
196        if (!PKIXSupport) {
197            fail(NotSupportMsg);
198            return;
199        }
200        for (int i = 0; i < validValues.length; i++ ) {
201            for (int j = 1; j < invalidValues.length; j++) {
202                try {
203                    CertPathBuilder.getInstance(validValues[i], invalidValues[j]);
204                    fail("NoSuchProviderException must be hrown");
205                } catch (NoSuchProviderException e1) {
206                }
207            }
208        }
209    }
210    /**
211     * Test for <code>getInstance(String algorithm, String provider)</code> method
212	 * Assertion:
213	 * throws NullPointerException when algorithm is null
214	 * throws NoSuchAlgorithmException when algorithm  is not correct
215     */
216    public void testCertPathBuilder06()
217            throws NoSuchAlgorithmException, NoSuchProviderException  {
218        if (!PKIXSupport) {
219            fail(NotSupportMsg);
220            return;
221        }
222        try {
223            CertPathBuilder.getInstance(null, defaultProviderName);
224            fail("No expected NullPointerException");
225        } catch (NullPointerException e) {
226        }
227        for (int i = 0; i < invalidValues.length; i++) {
228            try {
229                CertPathBuilder.getInstance(invalidValues[i], defaultProviderName);
230                fail("NoSuchAlgorithmException must be thrown");
231            } catch (NoSuchAlgorithmException e1) {
232            }
233        }
234    }
235
236    /**
237     * Test for <code>getInstance(String algorithm, String provider)</code> method
238	 * Assertion: returns CertPathBuilder object
239     */
240    public void testCertPathBuilder07()
241            throws NoSuchAlgorithmException, NoSuchProviderException  {
242        if (!PKIXSupport) {
243            fail(NotSupportMsg);
244            return;
245        }
246        CertPathBuilder certPB;
247        for (int i = 0; i < validValues.length; i++) {
248            certPB = CertPathBuilder.getInstance(validValues[i], defaultProviderName);
249            assertEquals("Incorrect algorithm", certPB.getAlgorithm(), validValues[i]);
250            assertEquals("Incorrect provider name", certPB.getProvider().getName(), defaultProviderName);
251        }
252    }
253
254    /**
255     * Test for <code>getInstance(String algorithm, Provider provider)</code> method
256	 * Assertion: throws IllegalArgumentException when provider is null
257     */
258    public void testCertPathBuilder08()
259            throws NoSuchAlgorithmException  {
260        if (!PKIXSupport) {
261            fail(NotSupportMsg);
262            return;
263        }
264        Provider prov = null;
265        for (int t = 0; t < validValues.length; t++ ) {
266            try {
267                CertPathBuilder.getInstance(validValues[t], prov);
268                fail("IllegalArgumentException must be thrown");
269            } catch (IllegalArgumentException e1) {
270            }
271        }
272    }
273
274    /**
275     * Test for <code>getInstance(String algorithm, String provider)</code> method
276	 * Assertion:
277	 * throws NullPointerException when algorithm is null
278	 * throws NoSuchAlgorithmException when algorithm  is not correct
279     */
280    public void testCertPathBuilder09()
281            throws NoSuchAlgorithmException, NoSuchProviderException  {
282        if (!PKIXSupport) {
283            fail(NotSupportMsg);
284            return;
285        }
286        try {
287            CertPathBuilder.getInstance(null, defaultProvider);
288            fail("No expected NullPointerException");
289        } catch (NullPointerException e) {
290        }
291        for (int i = 0; i < invalidValues.length; i++) {
292            try {
293                CertPathBuilder.getInstance(invalidValues[i], defaultProvider);
294                fail("NoSuchAlgorithm must be thrown");
295            } catch (NoSuchAlgorithmException e1) {
296            }
297        }
298    }
299    /**
300     * Test for <code>getInstance(String algorithm, String provider)</code> method
301	 * Assertion: returns CertPathBuilder object
302     */
303    public void testCertPathBuilder10()
304            throws NoSuchAlgorithmException, NoSuchProviderException  {
305        if (!PKIXSupport) {
306            fail(NotSupportMsg);
307            return;
308        }
309        CertPathBuilder certPB;
310        for (int i = 0; i < invalidValues.length; i++) {
311            certPB = CertPathBuilder.getInstance(validValues[i], defaultProvider);
312            assertEquals("Incorrect algorithm", certPB.getAlgorithm(), validValues[i]);
313            assertEquals("Incorrect provider name", certPB.getProvider(), defaultProvider);
314        }
315    }
316    /**
317     * Test for <code>build(CertPathParameters params)</code> method
318	 * Assertion: throws InvalidAlgorithmParameterException params is null
319     */
320    public void testCertPathBuilder11()
321            throws NoSuchAlgorithmException, NoSuchProviderException,
322            CertPathBuilderException {
323        if (!PKIXSupport) {
324            fail(NotSupportMsg);
325            return;
326        }
327        CertPathBuilder [] certPB = createCPBs();
328        assertNotNull("CertPathBuilder objects were not created", certPB);
329        for (int i = 0; i < certPB.length; i++ ){
330            try {
331                certPB[i].build(null);
332                fail("InvalidAlgorithmParameterException must be thrown");
333            } catch(InvalidAlgorithmParameterException e) {
334            }
335        }
336    }
337    /**
338     * Test for
339     * <code>CertPathBuilder</code> constructor
340     * Assertion: returns CertPathBuilder object
341     */
342    public void testCertPathBuilder12()
343            throws CertificateException, NoSuchProviderException,
344            NoSuchAlgorithmException, InvalidAlgorithmParameterException,
345            CertPathBuilderException {
346        if (!PKIXSupport) {
347            fail(NotSupportMsg);
348            return;
349        }
350        CertPathBuilderSpi spi = new MyCertPathBuilderSpi();
351        CertPathBuilder certPB = new myCertPathBuilder(spi,
352                    defaultProvider, defaultType);
353        assertEquals("Incorrect algorithm", certPB.getAlgorithm(), defaultType);
354        assertEquals("Incorrect provider", certPB.getProvider(), defaultProvider);
355        try {
356            certPB.build(null);
357            fail("CertPathBuilderException must be thrown ");
358        } catch (CertPathBuilderException e) {
359        }
360        certPB = new myCertPathBuilder(null, null, null);
361        assertNull("Incorrect algorithm", certPB.getAlgorithm());
362        assertNull("Incorrect provider", certPB.getProvider());
363        try {
364            certPB.build(null);
365            fail("NullPointerException must be thrown ");
366        } catch (NullPointerException e) {
367        }
368    }
369    public static void main(String args[]) {
370        junit.textui.TestRunner.run(CertPathBuilder1Test.class);
371    }
372
373}
374/**
375 * Additional class to verify CertPathBuilder constructor
376 */
377class myCertPathBuilder extends CertPathBuilder {
378
379    public myCertPathBuilder(CertPathBuilderSpi spi, Provider prov, String type) {
380        super(spi, prov, type);
381    }
382}
383