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