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.TestUtils;
29import tests.support.resource.Support_Resources;
30
31import java.io.ByteArrayInputStream;
32import java.io.InputStream;
33import java.security.Provider;
34import java.security.cert.CertPath;
35import java.security.cert.Certificate;
36import java.security.cert.CertificateFactory;
37import java.util.Collection;
38import java.util.Iterator;
39import java.util.List;
40import java.util.Vector;
41
42/**
43 * Tests for <code>CertificateFactory</code> class methods
44 */
45public class CertificateFactory3Test extends TestCase {
46
47    private static String defaultProviderName = null;
48
49    private static Provider defaultProvider = null;
50
51    private static String defaultType = CertificateFactory1Test.defaultType;
52
53    public static String fileCertPathPki = "java/security/cert/CertPath.PkiPath";
54
55    private static boolean X509Support = false;
56
57    private static String NotSupportMsg = "";
58
59    static {
60        defaultProvider = SpiEngUtils.isSupport(defaultType,
61                CertificateFactory1Test.srvCertificateFactory);
62        X509Support = defaultProvider != null;
63        defaultProviderName = X509Support ? defaultProvider.getName() : null;
64
65        NotSupportMsg = defaultType.concat(" is not supported");
66    }
67
68    private static CertificateFactory[] initCertFs() throws Exception {
69        if (!X509Support) {
70            fail(NotSupportMsg);
71        }
72
73        CertificateFactory[] certFs = new CertificateFactory[3];
74        certFs[0] = CertificateFactory.getInstance(defaultType);
75        certFs[1] = CertificateFactory.getInstance(defaultType,
76                defaultProviderName);
77        certFs[2] = CertificateFactory
78                .getInstance(defaultType, defaultProvider);
79        return certFs;
80    }
81
82    /**
83     * Test for <code>generateCertificate(InputStream inStream)</code> method
84     * Assertion: returns Certificate
85     */
86    public void testGenerateCertificate() throws Exception {
87        CertificateFactory[] certFs = initCertFs();
88        assertNotNull("CertificateFactory objects were not created", certFs);
89        Certificate[] certs = new Certificate[3];
90        for (int i = 0; i < certFs.length; i++) {
91            certs[i] = certFs[i].generateCertificate(new ByteArrayInputStream(
92                    TestUtils.getEncodedX509Certificate()));
93        }
94        assertEquals(certs[0], certs[1]);
95        assertEquals(certs[0], certs[2]);
96    }
97
98    /**
99     * Test for <code>generateCertificates(InputStream inStream)</code> method
100     * Assertion: returns Collection which consists of 1 Certificate
101     */
102    public void testGenerateCertificates() throws Exception {
103        CertificateFactory[] certFs = initCertFs();
104        assertNotNull("CertificateFactory objects were not created", certFs);
105        Certificate cert = certFs[0]
106                .generateCertificate(new ByteArrayInputStream(TestUtils
107                        .getEncodedX509Certificate()));
108        for (int i = 0; i < certFs.length; i++) {
109            Collection<? extends Certificate> col = null;
110            col = certFs[i].generateCertificates(new ByteArrayInputStream(
111                    TestUtils.getEncodedX509Certificate()));
112            Iterator<? extends Certificate> it = col.iterator();
113            assertEquals("Incorrect Collection size", col.size(), 1);
114            assertEquals("Incorrect Certificate in Collection", cert, it.next());
115        }
116    }
117
118    /**
119     * Test for <code>generateCertPath(List certificates)</code> method
120     * Assertion: returns CertPath with 1 Certificate
121     */
122    public void testGenerateCertPath01() throws Exception {
123        CertificateFactory[] certFs = initCertFs();
124        assertNotNull("CertificateFactory objects were not created", certFs);
125        // create list of certificates with one certificate
126        Certificate cert = certFs[0]
127                .generateCertificate(new ByteArrayInputStream(TestUtils
128                        .getEncodedX509Certificate()));
129        List<Certificate> list = new Vector<Certificate>();
130        list.add(cert);
131        for (int i = 0; i < certFs.length; i++) {
132            CertPath certPath = null;
133            certPath = certFs[i].generateCertPath(list);
134            assertEquals(cert.getType(), certPath.getType());
135            List<? extends Certificate> list1 = certPath.getCertificates();
136            assertFalse("Result list is empty", list1.isEmpty());
137            Iterator<? extends Certificate> it = list1.iterator();
138            assertEquals("Incorrect Certificate in CertPath", cert, it.next());
139        }
140    }
141
142    /**
143     * Test for
144     * <code>generateCertPath(InputStream inStream, String encoding)</code>
145     * method Assertion: returns CertPath with 1 Certificate
146     */
147    public void testGenerateCertPath02() throws Exception {
148        CertificateFactory[] certFs = initCertFs();
149        assertNotNull("CertificateFactory objects were not created", certFs);
150        for (int i = 0; i < certFs.length; i++) {
151            CertPath certPath = null;
152            InputStream fis = Support_Resources
153                    .getResourceStream(fileCertPathPki);
154            certPath = certFs[i].generateCertPath(fis, "PkiPath");
155            fis.close();
156            assertEquals(defaultType, certPath.getType());
157
158            List<? extends Certificate> list1 = certPath.getCertificates();
159            assertFalse("Result list is empty", list1.isEmpty());
160        }
161    }
162
163    /**
164     * Test for <code>generateCertPath(InputStream inStream)</code> method
165     * Assertion: returns CertPath with 1 Certificate
166     */
167    public void testGenerateCertPath03() throws Exception {
168        String certPathEncoding = "PkiPath";
169        CertificateFactory[] certFs = initCertFs();
170        assertNotNull("CertificateFactory objects were not created", certFs);
171        for (int i = 0; i < certFs.length; i++) {
172            Iterator<String> it = certFs[0].getCertPathEncodings();
173
174            assertTrue("no CertPath encodings", it.hasNext());
175
176            assertEquals("Incorrect default encoding", certPathEncoding, it
177                    .next());
178
179            CertPath certPath = null;
180            InputStream fis = Support_Resources
181                    .getResourceStream(fileCertPathPki);
182            certPath = certFs[i].generateCertPath(fis);
183            fis.close();
184            assertEquals(defaultType, certPath.getType());
185
186            List<? extends Certificate> list1 = certPath.getCertificates();
187            assertFalse("Result list is empty", list1.isEmpty());
188        }
189    }
190}
191