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.CertPathBuilderResult;
32
33import org.apache.harmony.security.tests.java.security.cert.CertPathBuilder1Test;
34import org.apache.harmony.security.tests.support.SpiEngUtils;
35
36import junit.framework.TestCase;
37
38/**
39 * Tests for CertPathBuilder class constructors and methods
40 *
41 */
42
43public class CertPathBuilder2Test extends TestCase {
44    private static final String defaultAlg = "CertPB";
45    private static final String CertPathBuilderProviderClass = "org.apache.harmony.security.tests.support.cert.MyCertPathBuilderSpi";
46
47    private static final String[] invalidValues = SpiEngUtils.invalidValues;
48
49    private static final String[] validValues;
50
51    static {
52        validValues = new String[4];
53        validValues[0] = defaultAlg;
54        validValues[1] = defaultAlg.toLowerCase();
55        validValues[2] = "CeRtPb";
56        validValues[3] = "cERTpb";
57    }
58
59    Provider mProv;
60
61    protected void setUp() throws Exception {
62        super.setUp();
63        mProv = (new SpiEngUtils()).new MyProvider("MyCertPathBuilderProvider",
64                "Provider for testing", CertPathBuilder1Test.srvCertPathBuilder
65                        .concat(".").concat(defaultAlg),
66                CertPathBuilderProviderClass);
67        Security.insertProviderAt(mProv, 1);
68    }
69
70    /*
71     * @see TestCase#tearDown()
72     */
73    protected void tearDown() throws Exception {
74        super.tearDown();
75        Security.removeProvider(mProv.getName());
76    }
77
78    /**
79     * Constructor for CertPathBuilder2Test.
80     *
81     * @param arg0
82     */
83    public CertPathBuilder2Test(String arg0) {
84        super(arg0);
85    }
86
87    private void checkResult(CertPathBuilder certBuild)
88            throws InvalidAlgorithmParameterException,
89            CertPathBuilderException {
90        String dt = CertPathBuilder.getDefaultType();
91        String propName = CertPathBuilder1Test.DEFAULT_TYPE_PROPERTY;
92        String dtN;
93        for (int i = 0; i <invalidValues.length; i++) {
94            Security.setProperty(propName, invalidValues[i]);
95            dtN = CertPathBuilder.getDefaultType();
96            if (!dtN.equals(invalidValues[i]) && !dtN.equals(dt)) {
97                fail("Incorrect default type: ".concat(dtN));
98            }
99        }
100        Security.setProperty(propName, dt);
101        assertEquals("Incorrect default type", CertPathBuilder.getDefaultType(),
102                dt);
103        try {
104            certBuild.build(null);
105            fail("CertPathBuilderException must be thrown");
106        } catch (CertPathBuilderException e) {
107        }
108        CertPathBuilderResult cpbResult = certBuild.build(null);
109        assertNull("Not null CertPathBuilderResult", cpbResult);
110    }
111
112    /**
113     * Test for <code>getInstance(String algorithm)</code> method
114     * Assertions:
115     * throws
116	 * throws NullPointerException when algorithm is null
117	 * throws NoSuchAlgorithmException when algorithm  is not correct
118     * returns CertPathBuilder object
119     */
120    public void testGetInstance01() throws NoSuchAlgorithmException,
121            InvalidAlgorithmParameterException, CertPathBuilderException {
122        try {
123            CertPathBuilder.getInstance(null);
124            fail("NullPointerException or NoSuchAlgorithmException must be thrown when algorithm is null");
125        } catch (NullPointerException e) {
126        } catch (NoSuchAlgorithmException e) {
127        }
128        for (int i = 0; i < invalidValues.length; i++) {
129            try {
130                CertPathBuilder.getInstance(invalidValues[i]);
131                fail("NoSuchAlgorithmException must be thrown (type: ".concat(
132                        invalidValues[i]).concat(")"));
133            } catch (NoSuchAlgorithmException e) {
134            }
135        }
136        CertPathBuilder cerPB;
137        for (int i = 0; i < validValues.length; i++) {
138            cerPB = CertPathBuilder.getInstance(validValues[i]);
139            assertEquals("Incorrect type", cerPB.getAlgorithm(), validValues[i]);
140            assertEquals("Incorrect provider", cerPB.getProvider(), mProv);
141            checkResult(cerPB);
142        }
143    }
144
145    /**
146     * Test for <code>getInstance(String algorithm, String provider)</code> method
147     * Assertions:
148	 * throws NullPointerException when algorithm is null
149	 * throws NoSuchAlgorithmException when algorithm  is not correct
150     * throws IllegalArgumentException when provider is null or empty;
151     * throws NoSuchProviderException when provider is available;
152     * returns CertPathBuilder object
153     */
154    public void testGetInstance02() throws NoSuchAlgorithmException,
155            NoSuchProviderException, IllegalArgumentException,
156            InvalidAlgorithmParameterException, CertPathBuilderException {
157        try {
158            CertPathBuilder.getInstance(null, mProv.getName());
159            fail("NullPointerException or NoSuchAlgorithmException must be thrown when algorithm is null");
160        } catch (NullPointerException e) {
161        } catch (NoSuchAlgorithmException e) {
162        }
163        for (int i = 0; i < invalidValues.length; i++) {
164            try {
165                CertPathBuilder.getInstance(invalidValues[i], mProv
166                        .getName());
167                fail("NoSuchAlgorithmException must be thrown (type: ".concat(
168                        invalidValues[i]).concat(")"));
169            } catch (NoSuchAlgorithmException e) {
170            }
171        }
172        String prov = null;
173        for (int i = 0; i < validValues.length; i++) {
174            try {
175                CertPathBuilder.getInstance(validValues[i], prov);
176                fail("IllegalArgumentException must be thrown when provider is null (type: "
177                        .concat(validValues[i]).concat(")"));
178            } catch (IllegalArgumentException e) {
179            }
180            try {
181                CertPathBuilder.getInstance(validValues[i], "");
182                fail("IllegalArgumentException must be thrown when provider is empty (type: "
183                        .concat(validValues[i]).concat(")"));
184            } catch (IllegalArgumentException e) {
185            }
186        }
187        for (int i = 0; i < validValues.length; i++) {
188            for (int j = 1; j < invalidValues.length; j++) {
189                try {
190                    CertPathBuilder.getInstance(validValues[i],
191                            invalidValues[j]);
192                    fail("NoSuchProviderException must be thrown (type: "
193                            .concat(validValues[i]).concat(" provider: ")
194                            .concat(invalidValues[j]).concat(")"));
195                } catch (NoSuchProviderException e) {
196                }
197            }
198        }
199        CertPathBuilder cerPB;
200        for (int i = 0; i < validValues.length; i++) {
201            cerPB = CertPathBuilder.getInstance(validValues[i], mProv
202                    .getName());
203            assertEquals("Incorrect type", cerPB.getAlgorithm(), validValues[i]);
204            assertEquals("Incorrect provider", cerPB.getProvider().getName(),
205                    mProv.getName());
206            checkResult(cerPB);
207        }
208    }
209
210    /**
211     * Test for <code>getInstance(String algorithm, Provider provider)</code>
212     * method
213     * Assertions:
214	 * throws NullPointerException when algorithm is null
215	 * throws NoSuchAlgorithmException when algorithm  is not correct
216     * returns CertPathBuilder object
217     */
218    public void testGetInstance03() throws NoSuchAlgorithmException,
219            IllegalArgumentException,
220            InvalidAlgorithmParameterException, CertPathBuilderException {
221        try {
222            CertPathBuilder.getInstance(null, mProv);
223            fail("NullPointerException or NoSuchAlgorithmException must be thrown when algorithm is null");
224        } catch (NullPointerException e) {
225        } catch (NoSuchAlgorithmException e) {
226        }
227        for (int i = 0; i < invalidValues.length; i++) {
228            try {
229                CertPathBuilder.getInstance(invalidValues[i], mProv);
230                fail("NoSuchAlgorithmException must be thrown (type: ".concat(
231                        invalidValues[i]).concat(")"));
232            } catch (NoSuchAlgorithmException e) {
233            }
234        }
235        Provider prov = null;
236        for (int i = 0; i < validValues.length; i++) {
237            try {
238                CertPathBuilder.getInstance(validValues[i], prov);
239                fail("IllegalArgumentException must be thrown when provider is null (type: "
240                        .concat(validValues[i]).concat(")"));
241            } catch (IllegalArgumentException e) {
242            }
243        }
244        CertPathBuilder cerPB;
245        for (int i = 0; i < validValues.length; i++) {
246            cerPB = CertPathBuilder.getInstance(validValues[i], mProv);
247            assertEquals("Incorrect type", cerPB.getAlgorithm(), validValues[i]);
248            assertEquals("Incorrect provider", cerPB.getProvider(), mProv);
249            checkResult(cerPB);
250        }
251    }
252
253}
254