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.MyCertificateFactorySpi;
29
30import java.io.ByteArrayInputStream;
31import java.io.DataInputStream;
32import java.security.NoSuchProviderException;
33import java.security.Provider;
34import java.security.Security;
35import java.security.cert.CRL;
36import java.security.cert.CRLException;
37import java.security.cert.CertPath;
38import java.security.cert.Certificate;
39import java.security.cert.CertificateException;
40import java.security.cert.CertificateFactory;
41import java.util.Collection;
42import java.util.Iterator;
43import java.util.List;
44
45/**
46 * Tests for CertificateFactory class constructors and methods
47 *
48 */
49public class CertificateFactory2Test extends TestCase {
50    private static final String defaultAlg = "CertFac";
51    private static final String CertificateFactoryProviderClass = "org.apache.harmony.security.tests.support.cert.MyCertificateFactorySpi";
52
53    private static final String[] invalidValues = SpiEngUtils.invalidValues;
54
55    private static final String[] validValues;
56
57    static {
58        validValues = new String[4];
59        validValues[0] = defaultAlg;
60        validValues[1] = defaultAlg.toLowerCase();
61        validValues[2] = "CeRtFaC";
62        validValues[3] = "cerTFac";
63    }
64
65    Provider mProv;
66
67    protected void setUp() throws Exception {
68        super.setUp();
69        mProv = (new SpiEngUtils()).new MyProvider("MyCFProvider",
70                "Provider for testing", CertificateFactory1Test.srvCertificateFactory
71                        .concat(".").concat(defaultAlg),
72                CertificateFactoryProviderClass);
73        Security.insertProviderAt(mProv, 1);
74    }
75
76    /*
77     * @see TestCase#tearDown()
78     */
79    protected void tearDown() throws Exception {
80        super.tearDown();
81        Security.removeProvider(mProv.getName());
82    }
83
84    private void checkResult(CertificateFactory certFactory, boolean mode)
85            throws CertificateException, CRLException {
86        MyCertificateFactorySpi.putMode(mode);
87
88        ByteArrayInputStream bais = new ByteArrayInputStream(new byte[0]);
89        DataInputStream dis = new DataInputStream(bais);
90        try {
91            certFactory.generateCertPath(bais);
92            fail("CertificateException must be thrown");
93        } catch (CertificateException e) {
94        }
95        try {
96            certFactory.generateCertPath(dis);
97            if (!mode) {
98                fail("CertificateException must be thrown because encodings list is empty");
99            }
100        } catch (CertificateException e) {
101            if (mode) {
102                fail("Unexpected CertificateFactoryException was thrown");
103            }
104        }
105        try {
106            certFactory.generateCertPath(bais, "aa");
107            fail("CertificateException must be thrown");
108        } catch (CertificateException e) {
109        }
110        try {
111            certFactory.generateCertPath(dis, "");
112            if (mode) {
113                fail("IllegalArgumentException must be thrown");
114            }
115        } catch (IllegalArgumentException e) {
116            if (!mode) {
117                fail("Unexpected IllegalArgumentException was thrown");
118            }
119        }
120        certFactory.generateCertPath(dis, "ss");
121
122        try {
123            certFactory.generateCertificate(bais);
124            fail("CertificateException must be thrown");
125        } catch (CertificateException e) {
126        }
127        try {
128            certFactory.generateCertificates(null);
129            fail("CertificateException must be thrown");
130        } catch (CertificateException e) {
131        }
132        Certificate cert = certFactory.generateCertificate(dis);
133        assertNull("Result must be null", cert);
134        Collection<? extends Certificate> col = certFactory.generateCertificates(dis);
135        assertNull("Result must be null", col);
136
137        try {
138            certFactory.generateCRL(bais);
139            fail("CRLException must be thrown");
140        } catch (CRLException e) {
141        }
142        try {
143            certFactory.generateCRLs(null);
144            fail("CRLException must be thrown");
145        } catch (CRLException e) {
146        }
147        CRL crl = certFactory.generateCRL(dis);
148        assertNull("Result must be null", crl);
149        Collection<? extends CRL> colc = certFactory.generateCRLs(dis);
150        assertNull("Result must be null", colc);
151
152        List<Certificate> list = null;
153        CertPath cp;
154        try {
155            cp = certFactory.generateCertPath(list);
156            if (mode) {
157                fail("NullPointerException must be thrown");
158            } else {
159                assertNull("Must be null", cp);
160            }
161        } catch (NullPointerException e) {
162            if (!mode) {
163                fail("Unexpected NullPointerException was thrown");
164            }
165        }
166        Iterator<String> it = certFactory.getCertPathEncodings();
167        if (mode) {
168            assertTrue(it.hasNext());
169        } else {
170            assertFalse(it.hasNext());
171        }
172    }
173
174    /**
175     * Test for <code>getInstance(String type)</code> method
176     * Assertions:
177     * throws NullPointerException when type is null
178     * throws CertificateException when type is not available
179     * returns CertificateFactory object
180     */
181    public void GetInstance01(boolean mode) throws CertificateException, CRLException {
182        try {
183            CertificateFactory.getInstance(null);
184            fail("NullPointerException or CertificateException must be thrown when type is null");
185        } catch (CertificateException e) {
186        } catch (NullPointerException e) {
187        }
188        for (int i = 0; i < invalidValues.length; i++) {
189            try {
190                CertificateFactory.getInstance(invalidValues[i]);
191                fail("CertificateException must be thrown (type: ".concat(
192                        invalidValues[i]).concat(")"));
193            } catch (CertificateException e) {
194            }
195        }
196        CertificateFactory cerF;
197        for (int i = 0; i < validValues.length; i++) {
198            cerF = CertificateFactory.getInstance(validValues[i]);
199            assertEquals("Incorrect type", cerF.getType(), validValues[i]);
200            assertEquals("Incorrect provider", cerF.getProvider(), mProv);
201            checkResult(cerF, mode);
202        }
203    }
204
205    /**
206     * Test for <code>getInstance(String type, String provider)</code> method
207     * Assertions:
208     * throws NullPointerException when type is null
209     * throws CertificateException when type is not available
210     * throws IllegalArgumentException when provider is null or empty;
211     * throws NoSuchProviderException when provider is available;
212     * returns CertificateFactory object
213     */
214
215    public void GetInstance02(boolean mode) throws CertificateException,
216            NoSuchProviderException, IllegalArgumentException, CRLException {
217        try {
218            CertificateFactory.getInstance(null, mProv.getName());
219            fail("NullPointerException or CertificateException must be thrown when type is null");
220        } catch (CertificateException e) {
221        } catch (NullPointerException e) {
222        }
223        for (int i = 0; i < invalidValues.length; i++) {
224            try {
225                CertificateFactory.getInstance(invalidValues[i], mProv
226                        .getName());
227                fail("CertificateException must be thrown (type: ".concat(
228                        invalidValues[i]).concat(")"));
229            } catch (CertificateException e) {
230            }
231        }
232        String prov = null;
233        for (int i = 0; i < validValues.length; i++) {
234            try {
235                CertificateFactory.getInstance(validValues[i], prov);
236                fail("IllegalArgumentException must be thrown when provider is null (type: "
237                        .concat(validValues[i]).concat(")"));
238            } catch (IllegalArgumentException e) {
239            }
240            try {
241                CertificateFactory.getInstance(validValues[i], "");
242                fail("IllegalArgumentException must be thrown when provider is empty (type: "
243                        .concat(validValues[i]).concat(")"));
244            } catch (IllegalArgumentException e) {
245            }
246        }
247        for (int i = 0; i < validValues.length; i++) {
248            for (int j = 1; j < invalidValues.length; j++) {
249                try {
250                    CertificateFactory.getInstance(validValues[i],
251                            invalidValues[j]);
252                    fail("NoSuchProviderException must be thrown (type: "
253                            .concat(validValues[i]).concat(" provider: ")
254                            .concat(invalidValues[j]).concat(")"));
255                } catch (NoSuchProviderException e) {
256                }
257            }
258        }
259        CertificateFactory cerF;
260        for (int i = 0; i < validValues.length; i++) {
261            cerF = CertificateFactory.getInstance(validValues[i], mProv
262                    .getName());
263            assertEquals("Incorrect type", cerF.getType(), validValues[i]);
264            assertEquals("Incorrect provider", cerF.getProvider().getName(),
265                    mProv.getName());
266            checkResult(cerF, mode);
267        }
268    }
269
270    /**
271     * Test for <code>getInstance(String type, Provider provider)</code>
272     * method
273     * Assertions:
274     * throws NullPointerException when type is null
275     * throws CertificateException when type is not available
276     * throws IllegalArgumentException when provider is null;
277     * returns CertificateFactory object
278     */
279
280    public void GetInstance03(boolean mode) throws CertificateException,
281            IllegalArgumentException, CRLException {
282        try {
283            CertificateFactory.getInstance(null, mProv);
284            fail("NullPointerException or CertificateException must be thrown when type is null");
285        } catch (CertificateException e) {
286        } catch (NullPointerException e) {
287        }
288        for (int i = 0; i < invalidValues.length; i++) {
289            try {
290                CertificateFactory.getInstance(invalidValues[i], mProv);
291                fail("CertificateException must be thrown (type: ".concat(
292                        invalidValues[i]).concat(")"));
293            } catch (CertificateException e) {
294            }
295        }
296        Provider prov = null;
297        for (int i = 0; i < validValues.length; i++) {
298            try {
299                CertificateFactory.getInstance(validValues[i], prov);
300                fail("IllegalArgumentException must be thrown when provider is null (type: "
301                        .concat(validValues[i]).concat(")"));
302            } catch (IllegalArgumentException e) {
303            }
304        }
305        CertificateFactory cerF;
306        for (int i = 0; i < validValues.length; i++) {
307            cerF = CertificateFactory.getInstance(validValues[i], mProv);
308            assertEquals("Incorrect type", cerF.getType(), validValues[i]);
309            assertEquals("Incorrect provider", cerF.getProvider(), mProv);
310            checkResult(cerF,  mode);
311        }
312    }
313    public void testGetInstance01() throws CertificateException, CRLException {
314        GetInstance01(true);
315    }
316    public void testGetInstance02() throws CertificateException,
317        NoSuchProviderException, IllegalArgumentException, CRLException {
318        GetInstance02(true);
319    }
320    public void testGetInstance03() throws CertificateException,
321        IllegalArgumentException, CRLException {
322        GetInstance03(true);
323    }
324    public void testGetInstance04() throws CertificateException, CRLException {
325        GetInstance01(false);
326    }
327    public void testGetInstance05() throws CertificateException,
328        NoSuchProviderException, IllegalArgumentException, CRLException {
329        GetInstance02(false);
330    }
331    public void testGetInstance06() throws CertificateException,
332        IllegalArgumentException, CRLException {
333        GetInstance03(false);
334    }
335}
336