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 Alexander Y. Kleymenov
     20 * @version $Revision$
     21 */
     22 
     23 package tests.security.cert;
     24 
     25 import junit.framework.Test;
     26 import junit.framework.TestCase;
     27 import junit.framework.TestSuite;
     28 
     29 import java.io.ByteArrayInputStream;
     30 import java.math.BigInteger;
     31 import java.security.InvalidKeyException;
     32 import java.security.NoSuchAlgorithmException;
     33 import java.security.NoSuchProviderException;
     34 import java.security.Principal;
     35 import java.security.PublicKey;
     36 import java.security.SignatureException;
     37 import java.security.cert.CRLException;
     38 import java.security.cert.Certificate;
     39 import java.security.cert.CertificateFactory;
     40 import java.security.cert.X509CRL;
     41 import java.security.cert.X509CRLEntry;
     42 import java.security.cert.X509Certificate;
     43 import java.util.Date;
     44 import java.util.Set;
     45 
     46 import javax.security.auth.x500.X500Principal;
     47 
     48 import org.apache.harmony.security.tests.support.cert.TestUtils;
     49 
     50 import tests.security.cert.X509CRL2Test.MyX509CRL;
     51 /**
     52  */
     53 public class X509CRLTest extends TestCase {
     54 
     55     private X509CRL tbt_crl;
     56 
     57     String certificate = "-----BEGIN CERTIFICATE-----\n"
     58         + "MIICZTCCAdICBQL3AAC2MA0GCSqGSIb3DQEBAgUAMF8xCzAJBgNVBAYTAlVTMSAw\n"
     59         + "HgYDVQQKExdSU0EgRGF0YSBTZWN1cml0eSwgSW5jLjEuMCwGA1UECxMlU2VjdXJl\n"
     60         + "IFNlcnZlciBDZXJ0aWZpY2F0aW9uIEF1dGhvcml0eTAeFw05NzAyMjAwMDAwMDBa\n"
     61         + "Fw05ODAyMjAyMzU5NTlaMIGWMQswCQYDVQQGEwJVUzETMBEGA1UECBMKQ2FsaWZv\n"
     62         + "cm5pYTESMBAGA1UEBxMJUGFsbyBBbHRvMR8wHQYDVQQKExZTdW4gTWljcm9zeXN0\n"
     63         + "ZW1zLCBJbmMuMSEwHwYDVQQLExhUZXN0IGFuZCBFdmFsdWF0aW9uIE9ubHkxGjAY\n"
     64         + "BgNVBAMTEWFyZ29uLmVuZy5zdW4uY29tMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCB\n"
     65         + "iQKBgQCofmdY+PiUWN01FOzEewf+GaG+lFf132UpzATmYJkA4AEA/juW7jSi+LJk\n"
     66         + "wJKi5GO4RyZoyimAL/5yIWDV6l1KlvxyKslr0REhMBaD/3Z3EsLTTEf5gVrQS6sT\n"
     67         + "WMoSZAyzB39kFfsB6oUXNtV8+UKKxSxKbxvhQn267PeCz5VX2QIDAQABMA0GCSqG\n"
     68         + "SIb3DQEBAgUAA34AXl3at6luiV/7I9MN5CXYoPJYI8Bcdc1hBagJvTMcmlqL2uOZ\n"
     69         + "H9T5hNMEL9Tk6aI7yZPXcw/xI2K6pOR/FrMp0UwJmdxX7ljV6ZtUZf7pY492UqwC\n"
     70         + "1777XQ9UEZyrKJvF5ntleeO0ayBqLGVKCWzWZX9YsXCpv47FNLZbupE=\n"
     71         + "-----END CERTIFICATE-----\n";
     72 
     73     ByteArrayInputStream certArray = new ByteArrayInputStream(certificate
     74         .getBytes());
     75 
     76     /**
     77      * The stub class used for testing of non abstract methods.
     78      */
     79     private class TBTCRL extends X509CRL {
     80         public String toString() {
     81             return null;
     82         }
     83 
     84         public boolean isRevoked(Certificate cert) {
     85             return true;
     86         }
     87 
     88         public Set<String> getNonCriticalExtensionOIDs() {
     89             return null;
     90         }
     91 
     92         public Set<String> getCriticalExtensionOIDs() {
     93             return null;
     94         }
     95 
     96         public byte[] getExtensionValue(String oid) {
     97             return null;
     98         }
     99 
    100         public boolean hasUnsupportedCriticalExtension() {
    101             return false;
    102         }
    103 
    104         public byte[] getEncoded() {
    105             return null;
    106         }
    107 
    108         public void verify(PublicKey key)
    109                  throws CRLException, NoSuchAlgorithmException,
    110                         InvalidKeyException, NoSuchProviderException,
    111                         SignatureException
    112         {
    113         }
    114 
    115         public void verify(PublicKey key, String sigProvider)
    116                  throws CRLException, NoSuchAlgorithmException,
    117                         InvalidKeyException, NoSuchProviderException,
    118                         SignatureException
    119         {
    120         }
    121 
    122         public int getVersion() {
    123             return 2;
    124         }
    125 
    126         public Principal getIssuerDN() {
    127             return null;
    128         }
    129 
    130         public Date getThisUpdate() {
    131             return null;
    132         }
    133 
    134         public Date getNextUpdate() {
    135             return null;
    136         }
    137 
    138         public X509CRLEntry getRevokedCertificate(BigInteger serialNumber) {
    139             return null;
    140         }
    141 
    142         public Set<X509CRLEntry> getRevokedCertificates() {
    143             return null;
    144         }
    145 
    146         public byte[] getTBSCertList() {
    147             return null;
    148         }
    149 
    150         public byte[] getSignature() {
    151             return null;
    152         }
    153 
    154         public String getSigAlgName() {
    155             return null;
    156         }
    157 
    158         public String getSigAlgOID() {
    159             return null;
    160         }
    161 
    162         public byte[] getSigAlgParams() {
    163             return null;
    164         }
    165     }
    166 
    167 
    168     public X509CRLTest() {
    169 
    170     }
    171 
    172     public void setUp() {
    173         tbt_crl = new TBTCRL() {
    174             public byte[] getEncoded() {
    175                 return new byte[] {1, 2, 3};
    176             }
    177         };
    178     }
    179 
    180     /**
    181      * getType() method testing. Tests that getType() method returns
    182      * the value "X.509"
    183      */
    184     public void testGetType() {
    185         assertEquals("The type of X509CRL should be X.509",
    186                                             tbt_crl.getType(), "X.509");
    187     }
    188 
    189     /**
    190      * equals(Object other) method testing. Tests the correctness of equal
    191      * operation: it should be reflexive, symmetric, transitive, consistent
    192      * and should be false on null object.
    193      */
    194     public void testEquals() {
    195         TBTCRL tbt_crl_1 = new TBTCRL() {
    196             public byte[] getEncoded() {
    197                 return new byte[] {1, 2, 3};
    198             }
    199         };
    200 
    201         TBTCRL tbt_crl_2 = new TBTCRL() {
    202             public byte[] getEncoded() {
    203                 return new byte[] {1, 2, 3};
    204             }
    205         };
    206 
    207         TBTCRL tbt_crl_3 = new TBTCRL() {
    208             public byte[] getEncoded() {
    209                 return new byte[] {3, 2, 1};
    210             }
    211         };
    212 
    213         // checking for reflexive law:
    214         assertTrue("The equivalence relation should be reflexive.",
    215                                                     tbt_crl.equals(tbt_crl));
    216 
    217         assertEquals("The CRLs with equal encoded form should be equal",
    218                                                     tbt_crl, tbt_crl_1);
    219         // checking for symmetric law:
    220         assertTrue("The equivalence relation should be symmetric.",
    221                                                     tbt_crl_1.equals(tbt_crl));
    222 
    223         assertEquals("The CRLs with equal encoded form should be equal",
    224                                                     tbt_crl_1, tbt_crl_2);
    225         // checking for transitive law:
    226         assertTrue("The equivalence relation should be transitive.",
    227                                                     tbt_crl.equals(tbt_crl_2));
    228 
    229         assertFalse("Should not be equal to null object.",
    230                                                     tbt_crl.equals(null));
    231 
    232         assertFalse("The CRLs with differing encoded form should not be equal",
    233                                                     tbt_crl.equals(tbt_crl_3));
    234         assertFalse("The CRL should not be equals to the object which is not "
    235                     + "an instance of X509CRL", tbt_crl.equals(new Object()));
    236     }
    237 
    238     /**
    239      * hashCode() method testing. Tests that for equal objects hash codes
    240      * are equal.
    241      */
    242     public void testHashCode() {
    243         TBTCRL tbt_crl_1 = new TBTCRL() {
    244             public byte[] getEncoded() {
    245                 return new byte[] {1, 2, 3};
    246             }
    247         };
    248         assertTrue("Equal objects should have the same hash codes.",
    249                                     tbt_crl.hashCode() == tbt_crl_1.hashCode());
    250     }
    251 
    252     /**
    253      * java.security.cert.X509CRL#getIssuerX500Principal()
    254      */
    255     public void testGetIssuerX500Principal() {
    256         // return valid encoding
    257         TBTCRL crl = new TBTCRL() {
    258             public byte[] getEncoded() {
    259                 return TestUtils.getX509CRL_v1();
    260             }
    261         };
    262 
    263         assertEquals(new X500Principal("CN=Z"), crl.getIssuerX500Principal());
    264     }
    265 
    266     /**
    267      * getRevokedCertificate(X509Certificate certificate) method testing.
    268      * Check if the default implementation throws NullPointerException
    269      * on null input data.
    270      */
    271     // AndroidOnly("Test filed on RI: getRevokedCertificate throws " +
    272     //        "RuntimeException.")
    273     public void testGetRevokedCertificate() {
    274         TBTCRL crl = new TBTCRL() {
    275           @Override
    276           public byte[] getEncoded() {
    277             return TestUtils.getX509CRL_v1();
    278           }
    279         };
    280         try {
    281             crl.getRevokedCertificate((X509Certificate) null);
    282             fail("NullPointerException should be thrown "
    283                         + "in the case of null input data.");
    284         } catch (NullPointerException e) {
    285         }
    286 
    287 
    288         try {
    289             CertificateFactory cf = CertificateFactory.getInstance("X.509");
    290             X509Certificate cert = (X509Certificate) cf.generateCertificate(certArray);
    291             crl.getRevokedCertificate(cert);
    292         } catch (Exception e) {
    293             fail("Unexpected exception: " + e);
    294         }
    295     }
    296 
    297     public void testAbstractMethods() {
    298         TBTCRL crl = new TBTCRL() {
    299             public byte[] getEncoded() {
    300                 return TestUtils.getX509CRL_v1();
    301             }
    302         };
    303 
    304         try {
    305             crl.getEncoded();
    306             crl.getIssuerDN();
    307             crl.getNextUpdate();
    308             crl.getRevokedCertificate(BigInteger.ONE);
    309             crl.getRevokedCertificates();
    310             crl.getSigAlgName();
    311             crl.getSigAlgOID();
    312             crl.getSigAlgParams();
    313             crl.getSignature();
    314             crl.getTBSCertList();
    315             crl.getThisUpdate();
    316             crl.getVersion();
    317 
    318             crl.verify(null);
    319             crl.verify(null, "test");
    320         } catch (Exception e) {
    321             fail("Unexpected exception for constructor");
    322         }
    323     }
    324 
    325     public void testX509CRL() {
    326         try {
    327             TBTCRL crl = new TBTCRL();
    328             assertTrue(crl instanceof X509CRL);
    329         } catch (Exception e) {
    330             fail("Unexpected exception for constructor");
    331         }
    332     }
    333 
    334     public static Test suite() {
    335         return new TestSuite(X509CRLTest.class);
    336     }
    337 }
    338 
    339