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.api.javax.security.cert; 24 25 import junit.framework.Test; 26 import junit.framework.TestCase; 27 import junit.framework.TestSuite; 28 29 import java.security.InvalidKeyException; 30 import java.security.NoSuchAlgorithmException; 31 import java.security.NoSuchProviderException; 32 import java.security.PublicKey; 33 import java.security.SignatureException; 34 35 import javax.security.cert.Certificate; 36 import javax.security.cert.CertificateEncodingException; 37 import javax.security.cert.CertificateException; 38 39 public class CertificateTest extends TestCase { 40 41 /** 42 * The stub class used for testing of non abstract methods. 43 */ 44 private class TBTCert extends Certificate { 45 public byte[] getEncoded() throws CertificateEncodingException { 46 return null; 47 } 48 49 public void verify(PublicKey key) throws CertificateException, 50 NoSuchAlgorithmException, InvalidKeyException, 51 NoSuchProviderException, SignatureException { 52 } 53 54 public void verify(PublicKey key, String sigProvider) 55 throws CertificateException, NoSuchAlgorithmException, 56 InvalidKeyException, NoSuchProviderException, 57 SignatureException { 58 } 59 60 public String toString() { 61 return "TBTCert"; 62 } 63 64 public PublicKey getPublicKey() { 65 return null; 66 } 67 } 68 69 /** 70 * Test for <code>Certificate()</code> constructor<br> 71 */ 72 public final void testCertificate() { 73 TBTCert tbt_cert = new TBTCert(); 74 75 assertNull("Public key should be null", tbt_cert.getPublicKey()); 76 assertEquals("Wrong string representation for Certificate", "TBTCert", tbt_cert.toString()); 77 } 78 79 /** 80 * equals(Object obj) method testing. Tests the correctness of equal 81 * operation: it should be reflexive, symmetric, transitive, consistent and 82 * should be false on null object. 83 */ 84 public void testEquals() { 85 TBTCert tbt_cert = new TBTCert() { 86 public byte[] getEncoded() { 87 return new byte[] { 1, 2, 3 }; 88 } 89 }; 90 91 TBTCert tbt_cert_1 = new TBTCert() { 92 public byte[] getEncoded() { 93 return new byte[] { 1, 2, 3 }; 94 } 95 }; 96 97 TBTCert tbt_cert_2 = new TBTCert() { 98 public byte[] getEncoded() { 99 return new byte[] { 1, 2, 3 }; 100 } 101 }; 102 103 TBTCert tbt_cert_3 = new TBTCert() { 104 public byte[] getEncoded() { 105 return new byte[] { 3, 2, 1 }; 106 } 107 }; 108 109 // checking for reflexive law: 110 assertTrue("The equivalence relation should be reflexive.", tbt_cert 111 .equals(tbt_cert)); 112 113 assertEquals( 114 "The Certificates with equal encoded form should be equal", 115 tbt_cert, tbt_cert_1); 116 // checking for symmetric law: 117 assertTrue("The equivalence relation should be symmetric.", tbt_cert_1 118 .equals(tbt_cert)); 119 120 assertEquals( 121 "The Certificates with equal encoded form should be equal", 122 tbt_cert_1, tbt_cert_2); 123 // checking for transitive law: 124 assertTrue("The equivalence relation should be transitive.", tbt_cert 125 .equals(tbt_cert_2)); 126 127 assertFalse("Should not be equal to null object.", tbt_cert 128 .equals(null)); 129 130 assertFalse("The Certificates with differing encoded form " 131 + "should not be equal", tbt_cert.equals(tbt_cert_3)); 132 assertFalse("The Certificates should not be equals to the object " 133 + "which is not an instance of Certificate", tbt_cert 134 .equals(new Object())); 135 } 136 137 /** 138 * hashCode() method testing. 139 */ 140 public void testHashCode() { 141 TBTCert tbt_cert = new TBTCert() { 142 public byte[] getEncoded() { 143 return new byte[] { 1, 2, 3 }; 144 } 145 }; 146 TBTCert tbt_cert_1 = new TBTCert() { 147 public byte[] getEncoded() { 148 return new byte[] { 1, 2, 3 }; 149 } 150 }; 151 152 assertTrue("Equal objects should have the same hash codes.", tbt_cert 153 .hashCode() == tbt_cert_1.hashCode()); 154 } 155 156 public static Test suite() { 157 return new TestSuite(CertificateTest.class); 158 } 159 } 160