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