Home | History | Annotate | Download | only in cert
      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 
     23 package tests.security.cert;
     24 
     25 import dalvik.annotation.TestLevel;
     26 import dalvik.annotation.TestTargetClass;
     27 import dalvik.annotation.TestTargetNew;
     28 
     29 import junit.framework.TestCase;
     30 
     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;
     34 
     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;
     45 
     46 /**
     47  * Tests for <code>CertificateFactory</code> class methods
     48  */
     49 @TestTargetClass(CertificateFactory.class)
     50 public class CertificateFactory3Test extends TestCase {
     51 
     52     private static String defaultProviderName = null;
     53 
     54     private static Provider defaultProvider = null;
     55 
     56     private static String defaultType = CertificateFactory1Test.defaultType;
     57 
     58     public static String fileCertPathPki = "java/security/cert/CertPath.PkiPath";
     59 
     60     private static boolean X509Support = false;
     61 
     62     private static String NotSupportMsg = "";
     63 
     64     static {
     65         defaultProvider = SpiEngUtils.isSupport(defaultType,
     66                 CertificateFactory1Test.srvCertificateFactory);
     67         X509Support = defaultProvider != null;
     68         defaultProviderName = X509Support ? defaultProvider.getName() : null;
     69 
     70         NotSupportMsg = defaultType.concat(" is not supported");
     71     }
     72 
     73     private static CertificateFactory[] initCertFs() throws Exception {
     74         if (!X509Support) {
     75             fail(NotSupportMsg);
     76         }
     77 
     78         CertificateFactory[] certFs = new CertificateFactory[3];
     79         certFs[0] = CertificateFactory.getInstance(defaultType);
     80         certFs[1] = CertificateFactory.getInstance(defaultType,
     81                 defaultProviderName);
     82         certFs[2] = CertificateFactory
     83                 .getInstance(defaultType, defaultProvider);
     84         return certFs;
     85     }
     86 
     87     /**
     88      * Test for <code>generateCertificate(InputStream inStream)</code> method
     89      * Assertion: returns Certificate
     90      */
     91     @TestTargetNew(
     92         level = TestLevel.PARTIAL_COMPLETE,
     93         notes = "Doesn't verify CertificateException.",
     94         method = "generateCertificate",
     95         args = {java.io.InputStream.class}
     96     )
     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()));
    104         }
    105         assertEquals(certs[0], certs[1]);
    106         assertEquals(certs[0], certs[2]);
    107     }
    108 
    109     /**
    110      * Test for <code>generateCertificates(InputStream inStream)</code> method
    111      * Assertion: returns Collection which consists of 1 Certificate
    112      */
    113     @TestTargetNew(
    114         level = TestLevel.PARTIAL_COMPLETE,
    115         notes = "Doesn't verify CertificateException.",
    116         method = "generateCertificates",
    117         args = {java.io.InputStream.class}
    118     )
    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());
    132         }
    133     }
    134 
    135     /**
    136      * Test for <code>generateCertPath(List certificates)</code> method
    137      * Assertion: returns CertPath with 1 Certificate
    138      */
    139     @TestTargetNew(
    140         level = TestLevel.PARTIAL_COMPLETE,
    141         notes = "Doesn't verify CertificateException.",
    142         method = "generateCertPath",
    143         args = {java.util.List.class}
    144     )
    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>();
    153         list.add(cert);
    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());
    162         }
    163     }
    164 
    165     /**
    166      * Test for
    167      * <code>generateCertPath(InputStream inStream, String encoding)</code>
    168      * method Assertion: returns CertPath with 1 Certificate
    169      */
    170     @TestTargetNew(
    171         level = TestLevel.PARTIAL_COMPLETE,
    172         notes = "Doesn't verify CertificateException.",
    173         method = "generateCertPath",
    174         args = {java.io.InputStream.class, java.lang.String.class}
    175     )
    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");
    184             fis.close();
    185             assertEquals(defaultType, certPath.getType());
    186 
    187             List<? extends Certificate> list1 = certPath.getCertificates();
    188             assertFalse("Result list is empty", list1.isEmpty());
    189         }
    190     }
    191 
    192     /**
    193      * Test for <code>generateCertPath(InputStream inStream)</code> method
    194      * Assertion: returns CertPath with 1 Certificate
    195      */
    196     @TestTargetNew(
    197         level = TestLevel.PARTIAL_COMPLETE,
    198         notes = "Doesn't verify CertificateException.",
    199         method = "generateCertPath",
    200         args = {java.io.InputStream.class}
    201     )
    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();
    208 
    209             assertTrue("no CertPath encodings", it.hasNext());
    210 
    211             assertEquals("Incorrect default encoding", certPathEncoding, it
    212                     .next());
    213 
    214             CertPath certPath = null;
    215             InputStream fis = Support_Resources
    216                     .getResourceStream(fileCertPathPki);
    217             certPath = certFs[i].generateCertPath(fis);
    218             fis.close();
    219             assertEquals(defaultType, certPath.getType());
    220 
    221             List<? extends Certificate> list1 = certPath.getCertificates();
    222             assertFalse("Result list is empty", list1.isEmpty());
    223         }
    224     }
    225 }
    226