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 dalvik.annotation.KnownFailure;
26
27import org.apache.harmony.security.tests.support.SpiEngUtils;
28import org.apache.harmony.security.tests.support.cert.MyCertPathBuilderSpi;
29import org.apache.harmony.security.tests.support.cert.TestUtils;
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.CertPath;
37import java.security.cert.CertPathBuilder;
38import java.security.cert.CertPathBuilderException;
39import java.security.cert.CertPathBuilderResult;
40import java.security.cert.CertPathBuilderSpi;
41import java.security.cert.CertPathParameters;
42import java.security.cert.CertificateException;
43
44import junit.framework.TestCase;
45
46/**
47 * Tests for <code>CertPathBuilder</code> class constructors and
48 * methods.
49 *
50 */
51public class CertPathBuilder1Test extends TestCase {
52
53    public static final String srvCertPathBuilder = "CertPathBuilder";
54
55    public static final String defaultType = "PKIX";
56    public static final String [] validValues = {
57            "PKIX", "pkix", "PkiX", "pKiX" };
58
59    private static String [] invalidValues = SpiEngUtils.invalidValues;
60
61    private static boolean PKIXSupport = false;
62
63    private static Provider defaultProvider;
64    private static String defaultProviderName;
65
66    private static String NotSupportMsg = "";
67
68    public static final String DEFAULT_TYPE_PROPERTY = "certpathbuilder.type";
69
70    static {
71        defaultProvider = SpiEngUtils.isSupport(defaultType,
72                srvCertPathBuilder);
73        PKIXSupport = (defaultProvider != null);
74        defaultProviderName = (PKIXSupport ? defaultProvider.getName() : null);
75        NotSupportMsg = defaultType.concat(" is not supported");
76    }
77    private static CertPathBuilder[] createCPBs() {
78        if (!PKIXSupport) {
79            fail(NotSupportMsg);
80            return null;
81        }
82        try {
83            CertPathBuilder[] certPBs = new CertPathBuilder[3];
84            certPBs[0] = CertPathBuilder.getInstance(defaultType);
85            certPBs[1] = CertPathBuilder.getInstance(defaultType,
86                    defaultProviderName);
87            certPBs[2] = CertPathBuilder.getInstance(defaultType,
88                    defaultProvider);
89            return certPBs;
90        } catch (Exception e) {
91            return null;
92        }
93    }
94
95    /**
96     * java.security.cert.CertPathBuilder#getDefaultType()
97     */
98    public void test_getDefaultType() throws Exception {
99
100        // Regression for HARMONY-2785
101
102        // test: default value
103        assertNull(Security.getProperty(DEFAULT_TYPE_PROPERTY));
104        assertEquals("PKIX", CertPathBuilder.getDefaultType());
105    }
106
107    /**
108     * Test for <code>getInstance(String algorithm)</code> method
109     * Assertion:
110     * throws NullPointerException when algorithm is null
111     * throws NoSuchAlgorithmException when algorithm  is not correct
112     * or it is not available
113     */
114    public void testCertPathBuilder02() throws NoSuchAlgorithmException {
115        try {
116            CertPathBuilder.getInstance(null);
117            fail("No expected NullPointerException");
118        } catch (NullPointerException e) {
119        }
120        for (int i = 0; i < invalidValues.length; i++) {
121            try {
122                CertPathBuilder.getInstance(invalidValues[i]);
123                fail("NoSuchAlgorithmException must be thrown");
124            } catch (NoSuchAlgorithmException e) {
125            }
126        }
127    }
128
129    /**
130     * Test for <code>getInstance(String algorithm)</code> method
131     * Assertion: returns CertPathBuilder object
132     */
133    public void testCertPathBuilder03() throws NoSuchAlgorithmException  {
134        if (!PKIXSupport) {
135            fail(NotSupportMsg);
136            return;
137        }
138        for (int i = 0; i < validValues.length; i++) {
139            CertPathBuilder cpb = CertPathBuilder.getInstance(validValues[i]);
140            assertEquals("Incorrect algorithm", cpb.getAlgorithm(), validValues[i]);
141        }
142    }
143    /**
144     * Test for <code>getInstance(String algorithm, String provider)</code> method
145     * Assertion: throws IllegalArgumentException when provider is null or empty
146     *
147     * FIXME: verify what exception will be thrown if provider is empty
148     */
149    public void testCertPathBuilder04()
150            throws NoSuchAlgorithmException, NoSuchProviderException  {
151        if (!PKIXSupport) {
152            fail(NotSupportMsg);
153            return;
154        }
155        String provider = null;
156        for (int i = 0; i < validValues.length; i++) {
157            try {
158                CertPathBuilder.getInstance(validValues[i], provider);
159                fail("IllegalArgumentException must be thrown thrown");
160            } catch (IllegalArgumentException e) {
161            }
162            try {
163                CertPathBuilder.getInstance(validValues[i], "");
164                fail("IllegalArgumentException must be thrown thrown");
165            } catch (IllegalArgumentException e) {
166            }
167        }
168    }
169
170    /**
171     * Test for <code>getInstance(String algorithm, String provider)</code> method
172     * Assertion:
173     * throws NoSuchProviderException when provider has invalid value
174     */
175    public void testCertPathBuilder05()
176            throws NoSuchAlgorithmException  {
177        if (!PKIXSupport) {
178            fail(NotSupportMsg);
179            return;
180        }
181        for (int i = 0; i < validValues.length; i++ ) {
182            for (int j = 1; j < invalidValues.length; j++) {
183                try {
184                    CertPathBuilder.getInstance(validValues[i], invalidValues[j]);
185                    fail("NoSuchProviderException must be hrown");
186                } catch (NoSuchProviderException e1) {
187                }
188            }
189        }
190    }
191    /**
192     * Test for <code>getInstance(String algorithm, String provider)</code> method
193     * Assertion:
194     * throws NullPointerException when algorithm is null
195     * throws NoSuchAlgorithmException when algorithm  is not correct
196     */
197    public void testCertPathBuilder06()
198            throws NoSuchAlgorithmException, NoSuchProviderException  {
199        if (!PKIXSupport) {
200            fail(NotSupportMsg);
201            return;
202        }
203        try {
204            CertPathBuilder.getInstance(null, defaultProviderName);
205            fail("No expected NullPointerException");
206        } catch (NullPointerException e) {
207        }
208        for (int i = 0; i < invalidValues.length; i++) {
209            try {
210                CertPathBuilder.getInstance(invalidValues[i], defaultProviderName);
211                fail("NoSuchAlgorithmException must be thrown");
212            } catch (NoSuchAlgorithmException e1) {
213            }
214        }
215    }
216
217    /**
218     * Test for <code>getInstance(String algorithm, String provider)</code> method
219     * Assertion: returns CertPathBuilder object
220     */
221    public void testCertPathBuilder07()
222            throws NoSuchAlgorithmException, NoSuchProviderException  {
223        if (!PKIXSupport) {
224            fail(NotSupportMsg);
225            return;
226        }
227        CertPathBuilder certPB;
228        for (int i = 0; i < validValues.length; i++) {
229            certPB = CertPathBuilder.getInstance(validValues[i], defaultProviderName);
230            assertEquals("Incorrect algorithm", certPB.getAlgorithm(), validValues[i]);
231            assertEquals("Incorrect provider name", certPB.getProvider().getName(), defaultProviderName);
232        }
233    }
234
235    /**
236     * Test for <code>getInstance(String algorithm, Provider provider)</code> method
237     * Assertion: throws IllegalArgumentException when provider is null
238     */
239    public void testCertPathBuilder08()
240            throws NoSuchAlgorithmException  {
241        if (!PKIXSupport) {
242            fail(NotSupportMsg);
243            return;
244        }
245        Provider prov = null;
246        for (int t = 0; t < validValues.length; t++ ) {
247            try {
248                CertPathBuilder.getInstance(validValues[t], prov);
249                fail("IllegalArgumentException must be thrown");
250            } catch (IllegalArgumentException e1) {
251            }
252        }
253    }
254
255    /**
256     * Test for <code>getInstance(String algorithm, String provider)</code> method
257     * Assertion:
258     * throws NullPointerException when algorithm is null
259     * throws NoSuchAlgorithmException when algorithm  is not correct
260     */
261    public void testCertPathBuilder09()
262            throws NoSuchAlgorithmException, NoSuchProviderException  {
263        if (!PKIXSupport) {
264            fail(NotSupportMsg);
265            return;
266        }
267        try {
268            CertPathBuilder.getInstance(null, defaultProvider);
269            fail("No expected NullPointerException");
270        } catch (NullPointerException e) {
271        }
272        for (int i = 0; i < invalidValues.length; i++) {
273            try {
274                CertPathBuilder.getInstance(invalidValues[i], defaultProvider);
275                fail("NoSuchAlgorithm must be thrown");
276            } catch (NoSuchAlgorithmException e1) {
277            }
278        }
279    }
280    /**
281     * Test for <code>getInstance(String algorithm, String provider)</code> method
282     * Assertion: returns CertPathBuilder object
283     */
284    public void testCertPathBuilder10()
285            throws NoSuchAlgorithmException, NoSuchProviderException  {
286        if (!PKIXSupport) {
287            fail(NotSupportMsg);
288            return;
289        }
290        CertPathBuilder certPB;
291        for (int i = 0; i < invalidValues.length; i++) {
292            certPB = CertPathBuilder.getInstance(validValues[i], defaultProvider);
293            assertEquals("Incorrect algorithm", certPB.getAlgorithm(), validValues[i]);
294            assertEquals("Incorrect provider name", certPB.getProvider(), defaultProvider);
295        }
296    }
297    /**
298     * Test for <code>build(CertPathParameters params)</code> method
299     * Assertion: throws InvalidAlgorithmParameterException params is null
300     */
301    public void testCertPathBuilder11()
302            throws NoSuchAlgorithmException, NoSuchProviderException,
303            CertPathBuilderException {
304        if (!PKIXSupport) {
305            fail(NotSupportMsg);
306            return;
307        }
308        CertPathBuilder [] certPB = createCPBs();
309        assertNotNull("CertPathBuilder objects were not created", certPB);
310        for (int i = 0; i < certPB.length; i++ ){
311            try {
312                certPB[i].build(null);
313                fail("InvalidAlgorithmParameterException must be thrown");
314            } catch(InvalidAlgorithmParameterException e) {
315            }
316        }
317    }
318
319    // Test passed on RI
320    @KnownFailure(value="expired certificate bug 2322662")
321    public void testBuild() throws Exception {
322        TestUtils.initCertPathSSCertChain();
323        CertPathParameters params = TestUtils.getCertPathParameters();
324        CertPathBuilder builder = TestUtils.getCertPathBuilder();
325
326        try {
327            CertPathBuilderResult result = builder.build(params);
328            assertNotNull("builder result is null", result);
329            CertPath certPath = result.getCertPath();
330            assertNotNull("certpath of builder result is null", certPath);
331        } catch (InvalidAlgorithmParameterException e) {
332            fail("unexpected Exception: " + e);
333        }
334
335    }
336    /**
337     * Test for
338     * <code>CertPathBuilder</code> constructor
339     * Assertion: returns CertPathBuilder object
340     */
341    public void testCertPathBuilder12()
342            throws CertificateException, NoSuchProviderException,
343            NoSuchAlgorithmException, InvalidAlgorithmParameterException,
344            CertPathBuilderException {
345        if (!PKIXSupport) {
346            fail(NotSupportMsg);
347            return;
348        }
349        CertPathBuilderSpi spi = new MyCertPathBuilderSpi();
350        CertPathBuilder certPB = new myCertPathBuilder(spi,
351                    defaultProvider, defaultType);
352        assertEquals("Incorrect algorithm", certPB.getAlgorithm(), defaultType);
353        assertEquals("Incorrect provider", certPB.getProvider(), defaultProvider);
354        try {
355            certPB.build(null);
356            fail("CertPathBuilderException must be thrown ");
357        } catch (CertPathBuilderException e) {
358        }
359        certPB = new myCertPathBuilder(null, null, null);
360        assertNull("Incorrect algorithm", certPB.getAlgorithm());
361        assertNull("Incorrect provider", certPB.getProvider());
362        try {
363            certPB.build(null);
364            fail("NullPointerException must be thrown ");
365        } catch (NullPointerException e) {
366        }
367    }
368
369    /**
370     * Test for <code>getAlgorithm()</code> method Assertion: returns
371     * CertPathBuilder object
372     */
373    public void testCertPathBuilder13() throws NoSuchAlgorithmException {
374        if (!PKIXSupport) {
375            fail(NotSupportMsg);
376            return;
377        }
378
379        for (int i = 0; i < validValues.length; i++) {
380            CertPathBuilder cpb = CertPathBuilder.getInstance(validValues[i]);
381            assertEquals("Incorrect algorithm", cpb.getAlgorithm(),
382                    validValues[i]);
383            try {
384                cpb = CertPathBuilder.getInstance(validValues[i],
385                        defaultProviderName);
386                assertEquals("Incorrect algorithm", cpb.getAlgorithm(),
387                        validValues[i]);
388            } catch (NoSuchProviderException e) {
389                fail("Unexpected NoSuchProviderException exeption "
390                        + e.getMessage());
391            }
392
393            try {
394                cpb = CertPathBuilder.getInstance(validValues[i],
395                        defaultProviderName);
396                assertEquals("Incorrect algorithm", cpb.getAlgorithm(),
397                        validValues[i]);
398            } catch (NoSuchProviderException e) {
399                fail("Unexpected NoSuchProviderException " + e.getMessage());
400            }
401        }
402    }
403
404    /**
405     * Test for <code>getProvider()</code> method Assertion: returns
406     * CertPathBuilder object
407     */
408    public void testCertPathBuilder14() throws NoSuchAlgorithmException {
409        if (!PKIXSupport) {
410            fail(NotSupportMsg);
411            return;
412        }
413
414        for (int i = 0; i < validValues.length; i++) {
415            CertPathBuilder cpb2 = CertPathBuilder.getInstance(validValues[i],
416                    defaultProvider);
417            assertEquals("Incorrect provider", cpb2.getProvider(),
418                    defaultProvider);
419
420            try {
421                CertPathBuilder cpb3 = CertPathBuilder.getInstance(
422                        validValues[i], defaultProviderName);
423                assertEquals("Incorrect provider", cpb3.getProvider(),
424                        defaultProvider);
425            } catch (NoSuchProviderException e) {
426                fail("Unexpected NoSuchProviderException " + e.getMessage());
427            }
428        }
429
430    }
431}
432/**
433 * Additional class to verify CertPathBuilder constructor
434 */
435class myCertPathBuilder extends CertPathBuilder {
436
437    private static Provider provider;
438
439    public myCertPathBuilder(CertPathBuilderSpi spi, Provider prov, String type) {
440        super(spi, prov, type);
441    }
442
443    public static CertPathBuilder getInstance(String algorithm)
444            throws NoSuchAlgorithmException {
445        myCertPathBuilder mcpb = new myCertPathBuilder(null, null, null);
446        provider = mcpb.new MyProvider();
447        return CertPathBuilder.getInstance(algorithm);
448    }
449
450    public Provider getMyProvider() {
451        return provider;
452    }
453
454    public class MyProvider extends Provider {
455
456        private static final long serialVersionUID = -6537447905658191184L;
457
458        MyProvider() {
459            super("MyProvider", 1.0, "Provider for testing");
460        }
461
462        MyProvider(String name, double version, String info) {
463            super(name, version, info);
464        }
465
466        public void putService(Provider.Service s) {
467            super.putService(s);
468        }
469
470        public void removeService(Provider.Service s) {
471            super.removeService(s);
472        }
473    }
474
475}
476