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