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