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