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 */ 21 22 package org.apache.harmony.security.tests.java.security.cert; 23 24 import java.math.BigInteger; 25 import java.security.InvalidKeyException; 26 import java.security.NoSuchAlgorithmException; 27 import java.security.NoSuchProviderException; 28 import java.security.Principal; 29 import java.security.PublicKey; 30 import java.security.SignatureException; 31 import java.security.cert.CRLException; 32 import java.security.cert.Certificate; 33 import java.security.cert.X509CRL; 34 import java.security.cert.X509CRLEntry; 35 import java.security.cert.X509Certificate; 36 import java.util.Date; 37 import java.util.Set; 38 39 import javax.security.auth.x500.X500Principal; 40 41 import org.apache.harmony.security.tests.support.cert.TestUtils; 42 43 import junit.framework.Test; 44 import junit.framework.TestCase; 45 import junit.framework.TestSuite; 46 47 /** 48 */ 49 50 public class X509CRLTest extends TestCase { 51 52 private X509CRL tbt_crl; 53 54 /** 55 * The stub class used for testing of non abstract methods. 56 */ 57 private class TBTCRL extends X509CRL { toString()58 public String toString() { 59 return null; 60 } 61 isRevoked(Certificate cert)62 public boolean isRevoked(Certificate cert) { 63 return true; 64 } 65 getNonCriticalExtensionOIDs()66 public Set getNonCriticalExtensionOIDs() { 67 return null; 68 } 69 getCriticalExtensionOIDs()70 public Set getCriticalExtensionOIDs() { 71 return null; 72 } 73 getExtensionValue(String oid)74 public byte[] getExtensionValue(String oid) { 75 return null; 76 } 77 hasUnsupportedCriticalExtension()78 public boolean hasUnsupportedCriticalExtension() { 79 return false; 80 } 81 getEncoded()82 public byte[] getEncoded() { 83 return null; 84 } 85 verify(PublicKey key)86 public void verify(PublicKey key) 87 throws CRLException, NoSuchAlgorithmException, 88 InvalidKeyException, NoSuchProviderException, 89 SignatureException { 90 } 91 verify(PublicKey key, String sigProvider)92 public void verify(PublicKey key, String sigProvider) 93 throws CRLException, NoSuchAlgorithmException, 94 InvalidKeyException, NoSuchProviderException, 95 SignatureException { 96 } 97 getVersion()98 public int getVersion() { 99 return 2; 100 } 101 getIssuerDN()102 public Principal getIssuerDN() { 103 return null; 104 } 105 getThisUpdate()106 public Date getThisUpdate() { 107 return null; 108 } 109 getNextUpdate()110 public Date getNextUpdate() { 111 return null; 112 } 113 getRevokedCertificate(BigInteger serialNumber)114 public X509CRLEntry getRevokedCertificate(BigInteger serialNumber) { 115 return null; 116 } 117 getRevokedCertificates()118 public Set getRevokedCertificates() { 119 return null; 120 } 121 getTBSCertList()122 public byte[] getTBSCertList() { 123 return null; 124 } 125 getSignature()126 public byte[] getSignature() { 127 return null; 128 } 129 getSigAlgName()130 public String getSigAlgName() { 131 return null; 132 } 133 getSigAlgOID()134 public String getSigAlgOID() { 135 return null; 136 } 137 getSigAlgParams()138 public byte[] getSigAlgParams() { 139 return null; 140 } 141 } 142 143 X509CRLTest()144 public X509CRLTest() { 145 tbt_crl = new TBTCRL() { 146 public byte[] getEncoded() { 147 return new byte[] { 1, 2, 3 }; 148 } 149 }; 150 } 151 152 /** 153 * getType() method testing. Tests that getType() method returns 154 * the value "X.509" 155 */ testGetType()156 public void testGetType() { 157 assertEquals("The type of X509CRL should be X.509", 158 tbt_crl.getType(), "X.509"); 159 } 160 161 /** 162 * equals(Object other) method testing. Tests the correctness of equal 163 * operation: it should be reflexive, symmetric, transitive, consistent 164 * and should be false on null object. 165 */ testEquals()166 public void testEquals() { 167 TBTCRL tbt_crl_1 = new TBTCRL() { 168 public byte[] getEncoded() { 169 return new byte[] { 1, 2, 3 }; 170 } 171 }; 172 173 TBTCRL tbt_crl_2 = new TBTCRL() { 174 public byte[] getEncoded() { 175 return new byte[] { 1, 2, 3 }; 176 } 177 }; 178 179 TBTCRL tbt_crl_3 = new TBTCRL() { 180 public byte[] getEncoded() { 181 return new byte[] { 3, 2, 1 }; 182 } 183 }; 184 185 // checking for reflexive law: 186 assertTrue("The equivalence relation should be reflexive.", 187 tbt_crl.equals(tbt_crl)); 188 189 assertEquals("The CRLs with equal encoded form should be equal", 190 tbt_crl, tbt_crl_1); 191 // checking for symmetric law: 192 assertTrue("The equivalence relation should be symmetric.", 193 tbt_crl_1.equals(tbt_crl)); 194 195 assertEquals("The CRLs with equal encoded form should be equal", 196 tbt_crl_1, tbt_crl_2); 197 // checking for transitive law: 198 assertTrue("The equivalence relation should be transitive.", 199 tbt_crl.equals(tbt_crl_2)); 200 201 assertFalse("Should not be equal to null object.", 202 tbt_crl.equals(null)); 203 204 assertFalse("The CRLs with differing encoded form should not be equal", 205 tbt_crl.equals(tbt_crl_3)); 206 assertFalse("The CRL should not be equals to the object which is not " 207 + "an instance of X509CRL", tbt_crl.equals(new Object())); 208 } 209 210 /** 211 * hashCode() method testing. Tests that for equal objects hash codes 212 * are equal. 213 */ testHashCode()214 public void testHashCode() { 215 TBTCRL tbt_crl_1 = new TBTCRL() { 216 public byte[] getEncoded() { 217 return new byte[] { 1, 2, 3 }; 218 } 219 }; 220 assertTrue("Equal objects should have the same hash codes.", 221 tbt_crl.hashCode() == tbt_crl_1.hashCode()); 222 } 223 224 /** 225 * @tests java.security.cert.X509CRL#getIssuerX500Principal() 226 */ testGetIssuerX500Principal()227 public void testGetIssuerX500Principal() { 228 // return valid encoding 229 TBTCRL crl = new TBTCRL() { 230 public byte[] getEncoded() { 231 return TestUtils.getX509CRL_v1(); 232 } 233 234 ; 235 }; 236 237 assertEquals(new X500Principal("CN=Z"), crl.getIssuerX500Principal()); 238 } 239 240 /** 241 * getRevokedCertificate(X509Certificate certificate) method testing. 242 * Check if the default implementation throws NullPointerException 243 * on null input data. 244 */ testGetRevokedCertificate()245 public void testGetRevokedCertificate() { 246 try { 247 tbt_crl.getRevokedCertificate((X509Certificate) null); 248 fail("NullPointerException should be thrown " 249 + "in the case of null input data."); 250 } catch (NullPointerException e) { 251 } 252 } 253 suite()254 public static Test suite() { 255 return new TestSuite(X509CRLTest.class); 256 } 257 258 } 259