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 * @author Vera Y. Petrashkova
     19 * @version $Revision$
     20 */
     21 
     22 package tests.security.cert;
     23 
     24 import dalvik.annotation.TestLevel;
     25 import dalvik.annotation.TestTargetClass;
     26 import dalvik.annotation.TestTargetNew;
     27 import dalvik.annotation.TestTargets;
     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.MyCertPath;
     33 import org.apache.harmony.security.tests.support.cert.MyCertificate;
     34 import org.apache.harmony.security.tests.support.cert.MyCertificateFactorySpi;
     35 
     36 import java.io.ByteArrayInputStream;
     37 import java.io.ByteArrayOutputStream;
     38 import java.io.IOException;
     39 import java.io.InputStream;
     40 import java.io.ObjectOutputStream;
     41 import java.security.NoSuchProviderException;
     42 import java.security.Provider;
     43 import java.security.cert.CRL;
     44 import java.security.cert.CRLException;
     45 import java.security.cert.CertPath;
     46 import java.security.cert.Certificate;
     47 import java.security.cert.CertificateException;
     48 import java.security.cert.CertificateFactory;
     49 import java.security.cert.CertificateFactorySpi;
     50 import java.util.Collection;
     51 import java.util.Iterator;
     52 import java.util.List;
     53 import java.util.Vector;
     54 
     55 /**
     56  * Tests for <code>CertificateFactory</code> class methods and constructor
     57  *
     58  */
     59 @TestTargetClass(CertificateFactory.class)
     60 public class CertificateFactory1Test extends TestCase {
     61 
     62     public static final String srvCertificateFactory = "CertificateFactory";
     63 
     64     private static String defaultProviderName = null;
     65 
     66     private static Provider defaultProvider = null;
     67 
     68     private static boolean X509Support = false;
     69 
     70     public static String defaultType = "X.509";
     71 
     72     public static final String[] validValues = {
     73             "X.509", "x.509" };
     74 
     75     private final static String[] invalidValues = SpiEngUtils.invalidValues;
     76 
     77     private static String NotSupportMsg = "";
     78 
     79     static {
     80         defaultProvider = SpiEngUtils.isSupport(defaultType,
     81                 srvCertificateFactory);
     82         X509Support = (defaultProvider != null);
     83         defaultProviderName = (X509Support ? defaultProvider.getName() : null);
     84         NotSupportMsg = defaultType.concat(" is not supported");    }
     85 
     86     private static CertificateFactory[] initCertFs() {
     87         if (!X509Support) {
     88             fail(NotSupportMsg);
     89             return null;
     90         }
     91         try {
     92             CertificateFactory[] certFs = new CertificateFactory[3];
     93             certFs[0] = CertificateFactory.getInstance(defaultType);
     94             certFs[1] = CertificateFactory.getInstance(defaultType,
     95                     defaultProviderName);
     96             certFs[2] = CertificateFactory.getInstance(defaultType,
     97                     defaultProvider);
     98             return certFs;
     99         } catch (Exception e) {
    100             return null;
    101         }
    102     }
    103 
    104     private static MyCertificate createMC() {
    105         byte[] enc = { (byte) 0, (byte) 2, (byte) 3, (byte) 4, (byte) 5 };
    106         return new MyCertificate("Test_Test", enc);
    107     }
    108 
    109     /**
    110      * Test for <code>getInstance(String type)</code> method
    111      * Assertion: returns CertificateFactory if type is X.509
    112      */
    113     @TestTargetNew(
    114         level = TestLevel.PARTIAL_COMPLETE,
    115         notes = "Doesn't verify CertificateException.",
    116         method = "getInstance",
    117         args = {java.lang.String.class}
    118     )
    119     public void testCertificateFactory01() throws CertificateException {
    120         if (!X509Support) {
    121             fail(NotSupportMsg);
    122             return;
    123         }
    124         for (int i = 0; i < validValues.length; i++) {
    125             CertificateFactory certF = CertificateFactory
    126                     .getInstance(validValues[i]);
    127             assertEquals("Incorrect type: ", validValues[i], certF.getType());
    128         }
    129     }
    130 
    131     /**
    132      * Test for <code>getInstance(String type)</code> method
    133      * Assertion:
    134      * throws NullPointerException when type is null
    135      * throws CertificateException when type is not available
    136      */
    137     @TestTargetNew(
    138         level = TestLevel.PARTIAL_COMPLETE,
    139         notes = "Verifies CertificateException.",
    140         method = "getInstance",
    141         args = {java.lang.String.class}
    142     )
    143     public void testCertificateFactory02() {
    144         try {
    145             CertificateFactory.getInstance(null);
    146             fail("NullPointerException or CertificateException must be thrown when type is null");
    147         } catch (CertificateException e) {
    148         } catch (NullPointerException e) {
    149         }
    150         for (int i = 0; i < invalidValues.length; i++) {
    151             try {
    152                 CertificateFactory.getInstance(invalidValues[i]);
    153                 fail("CertificateException must be thrown when type: "
    154                         .concat(invalidValues[i]));
    155             } catch (CertificateException e) {
    156             }
    157         }
    158     }
    159 
    160     /**
    161      * Test for <code>getInstance(String type, String provider)</code> method
    162      * Assertion: throws IllegalArgumentException when provider is null or empty
    163      */
    164     @TestTargetNew(
    165         level = TestLevel.PARTIAL_COMPLETE,
    166         notes = "Verifies IllegalArgumentException. IllegalArgumentException was checked instead of NoSuchProviderException",
    167         method = "getInstance",
    168         args = {java.lang.String.class, java.lang.String.class}
    169     )
    170     public void testCertificateFactory03() throws CertificateException,
    171             NoSuchProviderException {
    172         if (!X509Support) {
    173             fail(NotSupportMsg);
    174             return;
    175         }
    176         String provider = null;
    177         for (int i = 0; i < validValues.length; i++) {
    178             try {
    179                 CertificateFactory.getInstance(validValues[i], provider);
    180                 fail("IllegalArgumentException must be thrown when provider is null");
    181             } catch (IllegalArgumentException e) {
    182             }
    183             try {
    184                 CertificateFactory.getInstance(validValues[i], "");
    185                 fail("IllegalArgumentException  must be thrown when provider is empty");
    186             } catch (IllegalArgumentException e) {
    187             }
    188         }
    189     }
    190 
    191     /**
    192      * Test for <code>getInstance(String type, String provider)</code> method
    193      * Assertion:
    194      * throws NullPointerException when type is null
    195      * throws CertificateException when type is not available
    196      */
    197     @TestTargetNew(
    198         level = TestLevel.PARTIAL_COMPLETE,
    199         notes = "Verifies CertificateException and NullPointerException.",
    200         method = "getInstance",
    201         args = {java.lang.String.class, java.lang.String.class}
    202     )
    203     public void testCertificateFactory04() throws NoSuchProviderException {
    204         if (!X509Support) {
    205             fail(NotSupportMsg);
    206             return;
    207         }
    208         try {
    209             CertificateFactory.getInstance(null, defaultProviderName);
    210             fail("NullPointerException or CertificateException must be thrown when type is null");
    211         } catch (CertificateException e) {
    212         } catch (NullPointerException e) {
    213         }
    214         for (int i = 0; i < invalidValues.length; i++) {
    215             try {
    216                 CertificateFactory.getInstance(invalidValues[i],
    217                         defaultProviderName);
    218                 fail("CertificateException must be thrown (type: ".concat(
    219                         invalidValues[i]).concat(" provider: ").concat(
    220                         defaultProviderName).concat(")"));
    221             } catch (CertificateException e) {
    222             }
    223         }
    224     }
    225 
    226     /**
    227      * Test for <code>getInstance(String type, String provider)</code> method
    228      * Assertion: returns CertificateFactory when type and provider have valid
    229      * values
    230      */
    231     @TestTargetNew(
    232         level = TestLevel.PARTIAL_COMPLETE,
    233         notes = "Verifies positive functionality.",
    234         method = "getInstance",
    235         args = {java.lang.String.class, java.lang.String.class}
    236     )
    237     public void testCertificateFactory05() throws CertificateException,
    238             NoSuchProviderException {
    239         if (!X509Support) {
    240             fail(NotSupportMsg);
    241             return;
    242         }
    243         CertificateFactory certF;
    244         for (int i = 0; i < validValues.length; i++) {
    245             certF = CertificateFactory.getInstance(validValues[i],
    246                     defaultProviderName);
    247             assertEquals("Incorrect type", certF.getType(), validValues[i]);
    248             assertEquals("Incorrect provider name", certF.getProvider()
    249                     .getName(), defaultProviderName);
    250         }
    251     }
    252 
    253     /**
    254      * Test for <code>getInstance(String type, Provider provider)</code>
    255      * method
    256      * Assertion: throws IllegalArgumentException when provider is null
    257      */
    258     @TestTargetNew(
    259         level = TestLevel.PARTIAL_COMPLETE,
    260         notes = "Verifies IllegalArgumentException.",
    261         method = "getInstance",
    262         args = {java.lang.String.class, java.security.Provider.class}
    263     )
    264     public void testCertificateFactory06() throws CertificateException {
    265         if (!X509Support) {
    266             fail(NotSupportMsg);
    267             return;
    268         }
    269         Provider provider = null;
    270         for (int i = 0; i < validValues.length; i++) {
    271             try {
    272                 CertificateFactory.getInstance(validValues[i], provider);
    273                 fail("IllegalArgumentException must be thrown  when provider is null");
    274             } catch (IllegalArgumentException e) {
    275             }
    276         }
    277     }
    278 
    279     /**
    280      * Test for <code>getInstance(String type, Provider provider)</code>
    281      * method
    282      * Assertion:
    283      * throws NullPointerException when type is null
    284      * throws CertificateException when type is not available
    285      */
    286     @TestTargetNew(
    287         level = TestLevel.PARTIAL_COMPLETE,
    288         notes = "Verifies CertificateException.",
    289         method = "getInstance",
    290         args = {java.lang.String.class, java.security.Provider.class}
    291     )
    292     public void testCertificateFactory07() {
    293         if (!X509Support) {
    294             fail(NotSupportMsg);
    295             return;
    296         }
    297         try {
    298             CertificateFactory.getInstance(null, defaultProvider);
    299             fail("NullPointerException or CertificateException must be thrown when type is null");
    300         } catch (CertificateException e) {
    301         } catch (NullPointerException e) {
    302         }
    303         for (int i = 0; i < invalidValues.length; i++) {
    304             try {
    305                 CertificateFactory.getInstance(invalidValues[i],
    306                         defaultProvider);
    307                 fail("CertificateException was not thrown as expected (type:"
    308                         .concat(invalidValues[i]).concat(" provider: ").concat(
    309                                 defaultProvider.getName()).concat(")"));
    310             } catch (CertificateException e) {
    311             }
    312         }
    313     }
    314 
    315     /**
    316      * Test for <code>getInstance(String type, Provider provider)</code>
    317      * method
    318      * Assertion: returns CertificateFactorythrows when type and provider
    319      * have valid values
    320      */
    321     @TestTargetNew(
    322         level = TestLevel.PARTIAL_COMPLETE,
    323         notes = "Verifies positive functionality of getInstance method.",
    324         method = "getInstance",
    325         args = {java.lang.String.class, java.security.Provider.class}
    326     )
    327     public void testCertificateFactory08() throws CertificateException {
    328         if (!X509Support) {
    329             fail(NotSupportMsg);
    330             return;
    331         }
    332         CertificateFactory certF;
    333         for (int i = 0; i < validValues.length; i++) {
    334             certF = CertificateFactory.getInstance(validValues[i],
    335                     defaultProvider);
    336             assertEquals("Incorrect provider", certF.getProvider(),
    337                     defaultProvider);
    338             assertEquals("Incorrect type", certF.getType(), validValues[i]);
    339         }
    340     }
    341 
    342     /**
    343      * Test for <code>getCertPathEncodings()</code> method
    344      * Assertion: returns encodings
    345      */
    346     @TestTargetNew(
    347         level = TestLevel.COMPLETE,
    348         notes = "",
    349         method = "getCertPathEncodings",
    350         args = {}
    351     )
    352     public void testCertificateFactory09() {
    353         if (!X509Support) {
    354             fail(NotSupportMsg);
    355             return;
    356         }
    357         CertificateFactory[] certFs = initCertFs();
    358         assertNotNull("CertificateFactory objects were not created", certFs);
    359         Iterator<String> it1 = certFs[0].getCertPathEncodings();
    360         Iterator<String> it2 = certFs[1].getCertPathEncodings();
    361         assertEquals("Incorrect encodings", it1.hasNext(), it2.hasNext());
    362         while (it1.hasNext()) {
    363             it2 = certFs[1].getCertPathEncodings();
    364             String s1 = it1.next();
    365             boolean yesNo = false;
    366             while (it2.hasNext()) {
    367                 if (s1.equals(it2.next())) {
    368                     yesNo = true;
    369                     break;
    370                 }
    371             }
    372             assertTrue("Encoding: ".concat(s1).concat(
    373                     " does not define for certF2 CertificateFactory"), yesNo);
    374         }
    375         it1 = certFs[0].getCertPathEncodings();
    376         it2 = certFs[2].getCertPathEncodings();
    377         assertEquals("Incorrect encodings", it1.hasNext(), it2.hasNext());
    378         while (it1.hasNext()) {
    379             it2 = certFs[2].getCertPathEncodings();
    380             String s1 = it1.next();
    381             boolean yesNo = false;
    382             while (it2.hasNext()) {
    383                 if (s1.equals(it2.next())) {
    384                     yesNo = true;
    385                     break;
    386                 }
    387             }
    388             assertTrue("Encoding: ".concat(s1).concat(
    389                     " does not define for certF3 CertificateFactory"), yesNo);
    390         }
    391     }
    392 
    393     /**
    394      * Test for <code>generateCertificate(InputStream inStream)</code>
    395      * <code>generateCertificates(InputStream inStream)</code>
    396      * <code>generateCRL(InputStream inStream)</code>
    397      * <code>generateCRLs(InputStream inStream)</code>
    398      * methods
    399      * Assertion: throw CertificateException and CRLException when
    400      * inStream is null or empty
    401      */
    402     @TestTargets({
    403         @TestTargetNew(
    404             level = TestLevel.PARTIAL_COMPLETE,
    405             notes = "Verifies methods with null and empty InputStream.",
    406             method = "generateCertificate",
    407             args = {java.io.InputStream.class}
    408         ),
    409         @TestTargetNew(
    410             level = TestLevel.PARTIAL_COMPLETE,
    411             notes = "Verifies methods with null and empty InputStream.",
    412             method = "generateCertificates",
    413             args = {java.io.InputStream.class}
    414         ),
    415         @TestTargetNew(
    416             level = TestLevel.PARTIAL_COMPLETE,
    417             notes = "Verifies methods with null and empty InputStream.",
    418             method = "generateCRL",
    419             args = {java.io.InputStream.class}
    420         ),
    421         @TestTargetNew(
    422             level = TestLevel.PARTIAL_COMPLETE,
    423             notes = "Verifies methods with null and empty InputStream.",
    424             method = "generateCRLs",
    425             args = {java.io.InputStream.class}
    426         )
    427     })
    428     public void testCertificateFactory10() {
    429         if (!X509Support) {
    430             fail(NotSupportMsg);
    431             return;
    432         }
    433         CertificateFactory[] certFs = initCertFs();
    434         assertNotNull("CertificateFactory objects were not created", certFs);
    435         byte [] bb = {};
    436         InputStream is = new ByteArrayInputStream(bb);
    437         Collection<?> colCer;
    438         Collection<?> colCrl;
    439         for (int i = 0; i < certFs.length; i++) {
    440             try {
    441                 certFs[i].generateCertificate(null);
    442                 fail("generateCertificate must thrown CertificateException or NullPointerEXception when input stream is null");
    443             } catch (CertificateException e) {
    444             } catch (NullPointerException e) {
    445             }
    446             is = new ByteArrayInputStream(bb);
    447             try {
    448                 certFs[i].generateCertificates(null);
    449                 fail("generateCertificates must throw CertificateException or NullPointerException when input stream is null");
    450             } catch (CertificateException e) {
    451             } catch (NullPointerException e) {
    452             }
    453             is = new ByteArrayInputStream(bb);
    454             try {
    455                 certFs[i].generateCertificate(is);
    456             } catch (CertificateException e) {
    457             }
    458             is = new ByteArrayInputStream(bb);
    459             try {
    460                 colCer = certFs[i].generateCertificates(is);
    461                 if (colCer != null) {
    462                     assertTrue("Not empty certificate collection", colCer.isEmpty());
    463                 }
    464             } catch (CertificateException e) {
    465             }
    466         }
    467         for (int i = 0; i < certFs.length; i++) {
    468             try {
    469                 certFs[i].generateCRL(null);
    470             } catch (CRLException e) {
    471             } catch (NullPointerException e) {
    472             }
    473             try {
    474                 colCrl = certFs[i].generateCRLs(null);
    475                 if (colCrl != null) {
    476                     assertTrue("Not empty CRL collection was returned from null stream", colCrl.isEmpty());
    477                 }
    478             } catch (CRLException e) {
    479             } catch (NullPointerException e) {
    480             }
    481             is = new ByteArrayInputStream(bb);
    482             try {
    483                  certFs[i].generateCRL(is);
    484             } catch (CRLException e) {
    485             }
    486             is = new ByteArrayInputStream(bb);
    487             try {
    488                 certFs[i].generateCRLs(is);
    489                 colCrl = certFs[i].generateCRLs(null);
    490                 if (colCrl != null) {
    491                     assertTrue("Not empty CRL collection was returned from empty stream", colCrl.isEmpty());
    492                 }
    493             } catch (CRLException e) {
    494             }
    495         }
    496     }
    497 
    498     /*
    499      * Test for <code> generateCertificate(InputStream inStream) </code><code>
    500      * generateCertificates(InputStream inStream) </code><code>
    501      * generateCRL(InputStream inStream) </code><code>
    502      * generateCRLs(InputStream inStream) </code>
    503      * methods
    504      * Assertion: throw CertificateException and CRLException when inStream
    505      * contains incompatible datas
    506      */
    507     @TestTargets({
    508         @TestTargetNew(
    509             level = TestLevel.PARTIAL_COMPLETE,
    510             notes = "Verifies positive functionality of methods.",
    511             method = "generateCertificate",
    512             args = {java.io.InputStream.class}
    513         ),
    514         @TestTargetNew(
    515             level = TestLevel.PARTIAL_COMPLETE,
    516             notes = "Verifies positive functionality of methods.",
    517             method = "generateCertificates",
    518             args = {java.io.InputStream.class}
    519         ),
    520         @TestTargetNew(
    521             level = TestLevel.PARTIAL_COMPLETE,
    522             notes = "Verifies positive functionality of methods.",
    523             method = "generateCRL",
    524             args = {java.io.InputStream.class}
    525         ),
    526         @TestTargetNew(
    527             level = TestLevel.PARTIAL_COMPLETE,
    528             notes = "Verifies positive functionality of methods.",
    529             method = "generateCRLs",
    530             args = {java.io.InputStream.class}
    531         )
    532     })
    533     public void testCertificateFactory11() throws IOException {
    534         if (!X509Support) {
    535             fail(NotSupportMsg);
    536             return;
    537         }
    538         CertificateFactory[] certFs = initCertFs();
    539         assertNotNull("CertificateFactory objects were not created", certFs);
    540         MyCertificate mc = createMC();
    541         ByteArrayOutputStream os = new ByteArrayOutputStream();
    542         ObjectOutputStream oos = new ObjectOutputStream(os);
    543         oos.writeObject(mc);
    544         oos.flush();
    545         oos.close();
    546 
    547         Certificate cer;
    548         Collection<?> colCer;
    549         CRL crl;
    550         Collection<?> colCrl;
    551 
    552         byte[] arr = os.toByteArray();
    553         ByteArrayInputStream is;
    554         for (int i = 0; i < certFs.length; i++) {
    555             is = new ByteArrayInputStream(arr);
    556             try {
    557                 cer = certFs[i].generateCertificate(is);
    558                 assertNull("Not null certificate was created", cer);
    559             } catch (CertificateException e) {
    560             }
    561             is = new ByteArrayInputStream(arr);
    562             try {
    563                 colCer = certFs[i].generateCertificates(is);
    564                 if (colCer != null) {
    565                     assertTrue("Not empty certificate Collection was created", colCer.isEmpty());
    566                 }
    567             } catch (CertificateException e) {
    568             }
    569             is = new ByteArrayInputStream(arr);
    570             try {
    571                 crl = certFs[i].generateCRL(is);
    572                 assertNull("Not null CRL was created", crl);
    573             } catch (CRLException e) {
    574             }
    575             is = new ByteArrayInputStream(arr);
    576             try {
    577                 colCrl = certFs[i].generateCRLs(is);
    578                 if (colCrl != null) {
    579                     assertTrue("Not empty CRL Collection was created", colCrl.isEmpty());
    580                 }
    581             } catch (CRLException e) {
    582             }
    583         }
    584     }
    585 
    586     /**
    587      * Test for <code>generateCertPath(InputStream inStream)</code>
    588      * <code>generateCertPath(InputStream inStream, String encoding)</code>
    589      * methods
    590      * Assertion: throws CertificateException when inStream is null or
    591      * when isStream contains invalid datas
    592      */
    593     @TestTargets({
    594         @TestTargetNew(
    595             level = TestLevel.PARTIAL_COMPLETE,
    596             notes = "Verifies CertificateException.",
    597             method = "generateCertPath",
    598             args = {java.io.InputStream.class, java.lang.String.class}
    599         ),
    600         @TestTargetNew(
    601             level = TestLevel.PARTIAL_COMPLETE,
    602             notes = "Verifies CertificateException.",
    603             method = "generateCertPath",
    604             args = {java.io.InputStream.class}
    605         )
    606     })
    607     public void testCertificateFactory12() {
    608         if (!X509Support) {
    609             fail(NotSupportMsg);
    610             return;
    611         }
    612         CertificateFactory[] certFs = initCertFs();
    613         assertNotNull("CertificateFactory objects were not created", certFs);
    614         InputStream is1 = null;
    615         InputStream is2 = new ByteArrayInputStream(new byte[10]);
    616 
    617         for (int i = 0; i < certFs.length; i++) {
    618             try {
    619                 certFs[i].generateCertPath(is1);
    620                 fail("generateCertificate must thrown CertificateException or NullPointerException when input stream is null");
    621             } catch (CertificateException e) {
    622             } catch (NullPointerException e) {
    623             }
    624             try {
    625                 certFs[i].generateCertPath(is2);
    626                 fail("generateCertificate must thrown CertificateException when input stream contains invalid datas");
    627             } catch (CertificateException e) {
    628             }
    629             Iterator<String> it = certFs[i].getCertPathEncodings();
    630             while (it.hasNext()) {
    631                 String enc = it.next();
    632                 try {
    633                     certFs[i].generateCertPath(is1, enc);
    634                     fail("generateCertificate must thrown CertificateException or NullPointerException when input stream is null and encodings "
    635                             .concat(enc));
    636                 } catch (CertificateException e) {
    637                 } catch (NullPointerException e) {
    638                 }
    639                 try {
    640                     certFs[i].generateCertPath(is2, enc);
    641                     fail("generateCertificate must thrown CertificateException when input stream contains invalid datas  and encodings "
    642                             .concat(enc));
    643                 } catch (CertificateException e) {
    644                 }
    645             }
    646         }
    647     }
    648 
    649     /**
    650      * Test for <code>generateCertPath(InputStream inStream)</code>
    651      * <code>generateCertPath(InputStream inStream, String encoding)</code>
    652      * methods
    653      * Assertion: throw CertificateException when isStream contains invalid datas
    654      */
    655     @TestTargets({
    656         @TestTargetNew(
    657             level = TestLevel.PARTIAL_COMPLETE,
    658             notes = "Verifies CertificateException.",
    659             method = "generateCertPath",
    660             args = {java.io.InputStream.class}
    661         ),
    662         @TestTargetNew(
    663             level = TestLevel.PARTIAL_COMPLETE,
    664             notes = "Verifies CertificateException.",
    665             method = "generateCertPath",
    666             args = {java.io.InputStream.class, java.lang.String.class}
    667         )
    668     })
    669     // Test passed on RI
    670     public void testCertificateFactory13() throws IOException {
    671         if (!X509Support) {
    672             fail(NotSupportMsg);
    673             return;
    674         }
    675         CertificateFactory[] certFs = initCertFs();
    676         assertNotNull("CertificateFactory objects were not created", certFs);
    677         byte[] enc = { (byte) 0, (byte) 2, (byte) 3, (byte) 4, (byte) 5 };
    678         MyCertPath mc = new MyCertPath(enc);
    679         ByteArrayOutputStream os = new ByteArrayOutputStream();
    680 
    681         ObjectOutputStream oos = new ObjectOutputStream(os);
    682         oos.writeObject(mc);
    683         oos.flush();
    684         oos.close();
    685 
    686         byte[] arr = os.toByteArray();
    687         ByteArrayInputStream is = new ByteArrayInputStream(arr);
    688 
    689         for (int i = 0; i < certFs.length; i++) {
    690             try {
    691                 certFs[i].generateCertPath(is);
    692                 fail("CertificateException must be thrown because input stream contains incorrect datas");
    693             } catch (CertificateException e) {
    694             }
    695             Iterator<String> it = certFs[i].getCertPathEncodings();
    696             while (it.hasNext()) {
    697                 try {
    698                     certFs[i].generateCertPath(is, it.next());
    699                     fail("CertificateException must be thrown because input stream contains incorrect datas");
    700                 } catch (CertificateException e) {
    701                 }
    702             }
    703         }
    704     }
    705 
    706     /**
    707      * Test for <code>generateCertPath(List certificates)</code> method
    708      * Assertion: throw NullPointerException certificates is null
    709      */
    710     @TestTargetNew(
    711         level = TestLevel.PARTIAL_COMPLETE,
    712         notes = "Verifies NullPointerException. Valid parameters checking missed.",
    713         method = "generateCertPath",
    714         args = {java.util.List.class}
    715     )
    716     public void testCertificateFactory14() throws CertificateException {
    717         if (!X509Support) {
    718             fail(NotSupportMsg);
    719             return;
    720         }
    721         CertificateFactory[] certFs = initCertFs();
    722         assertNotNull("CertificateFactory objects were not created", certFs);
    723         List<Certificate> list = null;
    724         for (int i = 0; i < certFs.length; i++) {
    725             try {
    726                 certFs[i].generateCertPath(list);
    727                 fail("generateCertificate must thrown CertificateException when list is null");
    728             } catch (NullPointerException e) {
    729             }
    730         }
    731     }
    732 
    733     /**
    734      * Test for <code>generateCertPath(List certificates)</code> method
    735      * Assertion: returns empty CertPath if certificates is empty
    736      */
    737     @TestTargetNew(
    738         level = TestLevel.PARTIAL_COMPLETE,
    739         notes = "Verifies that generateCertPath method returns empty CertPath if certificates is empty. Valid parameters checking missed.",
    740         method = "generateCertPath",
    741         args = {java.util.List.class}
    742     )
    743     public void testCertificateFactory15() throws CertificateException {
    744         if (!X509Support) {
    745             fail(NotSupportMsg);
    746             return;
    747         }
    748         CertificateFactory[] certFs = initCertFs();
    749         assertNotNull("CertificateFactory objects were not created", certFs);
    750         List<Certificate> list = new Vector<Certificate>();
    751         for (int i = 0; i < certFs.length; i++) {
    752             CertPath cp = certFs[i].generateCertPath(list);
    753             List<? extends Certificate> list1 = cp.getCertificates();
    754             assertTrue("List should be empty", list1.isEmpty());
    755         }
    756     }
    757 
    758     /**
    759      * Test for <code>generateCertPath(List certificates)</code> method
    760      * Assertion: throws CertificateException when certificates contains
    761      * incorrect Certificate
    762      */
    763     @TestTargetNew(
    764         level = TestLevel.PARTIAL_COMPLETE,
    765         notes = "Verifies CertificateException. Valid parameters checking missed.",
    766         method = "generateCertPath",
    767         args = {java.util.List.class}
    768     )
    769     public void testCertificateFactory16() {
    770         if (!X509Support) {
    771             fail(NotSupportMsg);
    772             return;
    773         }
    774         CertificateFactory[] certFs = initCertFs();
    775         assertNotNull("CertificateFactory objects were not created", certFs);
    776         MyCertificate ms = createMC();
    777         List<Certificate> list = new Vector<Certificate>();
    778         list.add(ms);
    779         for (int i = 0; i < certFs.length; i++) {
    780             try {
    781                 certFs[i].generateCertPath(list);
    782                 fail("CertificateException must be thrown");
    783             } catch (CertificateException e) {
    784             }
    785         }
    786     }
    787 
    788     /**
    789      * Test for <code>CertificateFactory</code> constructor
    790      * Assertion: returns CertificateFactory object
    791      */
    792     @TestTargetNew(
    793         level = TestLevel.PARTIAL_COMPLETE,
    794         notes = "Verifies CRLException and NullPointerException.",
    795         method = "generateCRLs",
    796         args = {java.io.InputStream.class}
    797     )
    798     public void testCertificateFactory17() throws CRLException {
    799         if (!X509Support) {
    800             fail(NotSupportMsg);
    801             return;
    802         }
    803         CertificateFactorySpi spi = new MyCertificateFactorySpi();
    804         CertificateFactory cf = new myCertificateFactory(spi, defaultProvider,
    805                 defaultType);
    806         assertEquals("Incorrect type", cf.getType(), defaultType);
    807         assertEquals("Incorrect provider", cf.getProvider(), defaultProvider);
    808         try {
    809             cf.generateCRLs(null);
    810             fail("CRLException must be thrown");
    811         } catch (CRLException e) {
    812         }
    813 
    814         cf = new myCertificateFactory(null, null, null);
    815         assertNull("Incorrect type", cf.getType());
    816         assertNull("Incorrect provider", cf.getProvider());
    817         try {
    818             cf.generateCRLs(null);
    819             fail("NullPointerException must be thrown");
    820         } catch (NullPointerException e) {
    821         }
    822     }
    823 
    824     /**
    825      * Test for <code>getType()</code> method
    826      */
    827     @TestTargetNew(
    828         level = TestLevel.COMPLETE,
    829         notes = "",
    830         method = "getType",
    831         args = {}
    832     )
    833     public void testCertificateFactory18() throws CertificateException {
    834         if (!X509Support) {
    835             fail(NotSupportMsg);
    836             return;
    837         }
    838         for (int i = 0; i < validValues.length; i++) {
    839             try {
    840                 CertificateFactory certF = CertificateFactory
    841                         .getInstance(validValues[i]);
    842                 assertEquals("Incorrect type: ", validValues[i], certF
    843                         .getType());
    844                 certF = CertificateFactory.getInstance(validValues[i],
    845                         defaultProviderName);
    846                 assertEquals("Incorrect type", certF.getType(), validValues[i]);
    847 
    848                 certF = CertificateFactory.getInstance(validValues[i],
    849                         defaultProvider);
    850                 assertEquals("Incorrect provider", certF.getProvider(),
    851                         defaultProvider);
    852                 assertEquals("Incorrect type", certF.getType(), validValues[i]);
    853 
    854             } catch (NoSuchProviderException e) {
    855                 fail("Unexpected NoSuchProviderException " + e.getMessage());
    856             }
    857         }
    858     }
    859 
    860     @SuppressWarnings("cast")
    861     @TestTargetNew(
    862         level = TestLevel.COMPLETE,
    863         notes = "",
    864         method = "CertificateFactory",
    865         args = {java.security.cert.CertificateFactorySpi.class, java.security.Provider.class, java.lang.String.class}
    866     )
    867     public void testCertificateFactory19() {
    868         if (!X509Support) {
    869             fail(NotSupportMsg);
    870             return;
    871         }
    872         CertificateFactorySpi spi = new MyCertificateFactorySpi();
    873         myCertificateFactory cf;
    874         try {
    875             cf = new myCertificateFactory(spi, defaultProvider,
    876                                       defaultType);
    877             assertEquals("Incorrect type", cf.getType(), defaultType);
    878             assertEquals("Incorrect provider", cf.getProvider(), defaultProvider);
    879             assertTrue(cf instanceof CertificateFactory);
    880         } catch (Exception e) {
    881             fail("Unexpected exception" + e);
    882         }
    883 
    884         try {
    885             cf = new myCertificateFactory(null, null, null);
    886         } catch (Exception e) {
    887             fail("Unexpected exception" + e);
    888         }
    889     }
    890 }
    891 /**
    892  * Additional class to verify CertificateFactory constructor
    893  */
    894 
    895 class myCertificateFactory extends CertificateFactory {
    896 
    897     public myCertificateFactory(CertificateFactorySpi spi, Provider prov,
    898             String type) {
    899         super(spi, prov, type);
    900     }
    901 }
    902