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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * @author Vera Y. Petrashkova
23 package tests.security.cert;
25 import dalvik.annotation.TestLevel;
26 import dalvik.annotation.TestTargetClass;
27 import dalvik.annotation.TestTargetNew;
29 import junit.framework.TestCase;
31 import org.apache.harmony.security.tests.support.SpiEngUtils;
32 import org.apache.harmony.security.tests.support.cert.TestUtils;
33 import tests.support.resource.Support_Resources;
35 import java.io.ByteArrayInputStream;
36 import java.io.InputStream;
37 import java.security.Provider;
38 import java.security.cert.CertPath;
39 import java.security.cert.Certificate;
40 import java.security.cert.CertificateFactory;
41 import java.util.Collection;
42 import java.util.Iterator;
43 import java.util.List;
44 import java.util.Vector;
47 * Tests for <code>CertificateFactory</code> class methods
49 @TestTargetClass(CertificateFactory.class)
50 public class CertificateFactory3Test extends TestCase {
52 private static String defaultProviderName = null;
54 private static Provider defaultProvider = null;
56 private static String defaultType = CertificateFactory1Test.defaultType;
58 public static String fileCertPathPki = "java/security/cert/CertPath.PkiPath";
60 private static boolean X509Support = false;
62 private static String NotSupportMsg = "";
65 defaultProvider = SpiEngUtils.isSupport(defaultType,
66 CertificateFactory1Test.srvCertificateFactory);
67 X509Support = defaultProvider != null;
68 defaultProviderName = X509Support ? defaultProvider.getName() : null;
70 NotSupportMsg = defaultType.concat(" is not supported");
73 private static CertificateFactory[] initCertFs() throws Exception {
78 CertificateFactory[] certFs = new CertificateFactory[3];
79 certFs[0] = CertificateFactory.getInstance(defaultType);
80 certFs[1] = CertificateFactory.getInstance(defaultType,
82 certFs[2] = CertificateFactory
83 .getInstance(defaultType, defaultProvider);
88 * Test for <code>generateCertificate(InputStream inStream)</code> method
89 * Assertion: returns Certificate
92 level = TestLevel.PARTIAL_COMPLETE,
93 notes = "Doesn't verify CertificateException.",
94 method = "generateCertificate",
95 args = {java.io.InputStream.class}
97 public void testGenerateCertificate() throws Exception {
98 CertificateFactory[] certFs = initCertFs();
99 assertNotNull("CertificateFactory objects were not created", certFs);
100 Certificate[] certs = new Certificate[3];
101 for (int i = 0; i < certFs.length; i++) {
102 certs[i] = certFs[i].generateCertificate(new ByteArrayInputStream(
103 TestUtils.getEncodedX509Certificate()));
105 assertEquals(certs[0], certs[1]);
106 assertEquals(certs[0], certs[2]);
110 * Test for <code>generateCertificates(InputStream inStream)</code> method
111 * Assertion: returns Collection which consists of 1 Certificate
114 level = TestLevel.PARTIAL_COMPLETE,
115 notes = "Doesn't verify CertificateException.",
116 method = "generateCertificates",
117 args = {java.io.InputStream.class}
119 public void testGenerateCertificates() throws Exception {
120 CertificateFactory[] certFs = initCertFs();
121 assertNotNull("CertificateFactory objects were not created", certFs);
122 Certificate cert = certFs[0]
123 .generateCertificate(new ByteArrayInputStream(TestUtils
124 .getEncodedX509Certificate()));
125 for (int i = 0; i < certFs.length; i++) {
126 Collection<? extends Certificate> col = null;
127 col = certFs[i].generateCertificates(new ByteArrayInputStream(
128 TestUtils.getEncodedX509Certificate()));
129 Iterator<? extends Certificate> it = col.iterator();
130 assertEquals("Incorrect Collection size", col.size(), 1);
131 assertEquals("Incorrect Certificate in Collection", cert, it.next());
136 * Test for <code>generateCertPath(List certificates)</code> method
137 * Assertion: returns CertPath with 1 Certificate
140 level = TestLevel.PARTIAL_COMPLETE,
141 notes = "Doesn't verify CertificateException.",
142 method = "generateCertPath",
143 args = {java.util.List.class}
145 public void testGenerateCertPath01() throws Exception {
146 CertificateFactory[] certFs = initCertFs();
147 assertNotNull("CertificateFactory objects were not created", certFs);
148 // create list of certificates with one certificate
149 Certificate cert = certFs[0]
150 .generateCertificate(new ByteArrayInputStream(TestUtils
151 .getEncodedX509Certificate()));
152 List<Certificate> list = new Vector<Certificate>();
154 for (int i = 0; i < certFs.length; i++) {
155 CertPath certPath = null;
156 certPath = certFs[i].generateCertPath(list);
157 assertEquals(cert.getType(), certPath.getType());
158 List<? extends Certificate> list1 = certPath.getCertificates();
159 assertFalse("Result list is empty", list1.isEmpty());
160 Iterator<? extends Certificate> it = list1.iterator();
161 assertEquals("Incorrect Certificate in CertPath", cert, it.next());
167 * <code>generateCertPath(InputStream inStream, String encoding)</code>
168 * method Assertion: returns CertPath with 1 Certificate
171 level = TestLevel.PARTIAL_COMPLETE,
172 notes = "Doesn't verify CertificateException.",
173 method = "generateCertPath",
174 args = {java.io.InputStream.class, java.lang.String.class}
176 public void testGenerateCertPath02() throws Exception {
177 CertificateFactory[] certFs = initCertFs();
178 assertNotNull("CertificateFactory objects were not created", certFs);
179 for (int i = 0; i < certFs.length; i++) {
180 CertPath certPath = null;
181 InputStream fis = Support_Resources
182 .getResourceStream(fileCertPathPki);
183 certPath = certFs[i].generateCertPath(fis, "PkiPath");
185 assertEquals(defaultType, certPath.getType());
187 List<? extends Certificate> list1 = certPath.getCertificates();
188 assertFalse("Result list is empty", list1.isEmpty());
193 * Test for <code>generateCertPath(InputStream inStream)</code> method
194 * Assertion: returns CertPath with 1 Certificate
197 level = TestLevel.PARTIAL_COMPLETE,
198 notes = "Doesn't verify CertificateException.",
199 method = "generateCertPath",
200 args = {java.io.InputStream.class}
202 public void testGenerateCertPath03() throws Exception {
203 String certPathEncoding = "PkiPath";
204 CertificateFactory[] certFs = initCertFs();
205 assertNotNull("CertificateFactory objects were not created", certFs);
206 for (int i = 0; i < certFs.length; i++) {
207 Iterator<String> it = certFs[0].getCertPathEncodings();
209 assertTrue("no CertPath encodings", it.hasNext());
211 assertEquals("Incorrect default encoding", certPathEncoding, it
214 CertPath certPath = null;
215 InputStream fis = Support_Resources
216 .getResourceStream(fileCertPathPki);
217 certPath = certFs[i].generateCertPath(fis);
219 assertEquals(defaultType, certPath.getType());
221 List<? extends Certificate> list1 = certPath.getCertificates();
222 assertFalse("Result list is empty", list1.isEmpty());