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 Vladimir N. Molotkov 20*/ 21 22package org.apache.harmony.security.tests.java.security.cert; 23 24import java.security.InvalidKeyException; 25import java.security.NoSuchAlgorithmException; 26import java.security.NoSuchProviderException; 27import java.security.SignatureException; 28import java.security.cert.Certificate; 29import java.security.cert.CertificateEncodingException; 30import java.security.cert.CertificateException; 31 32import org.apache.harmony.security.tests.support.cert.MyCertificate; 33 34import junit.framework.TestCase; 35 36/** 37 * Tests for <code>Certificate</code> fields and methods 38 * 39 */ 40public class CertificateTest extends TestCase { 41 /** 42 * Meaningless cert encoding just for testing purposes 43 */ 44 private static final byte[] testEncoding = new byte[] { 45 (byte)1, (byte)2, (byte)3, (byte)4, (byte)5 46 }; 47 48 49 /** 50 * Constructor for CertificateTest. 51 * @param name 52 */ 53 public CertificateTest(String name) { 54 super(name); 55 } 56 57 // 58 // Tests 59 // 60 61 /** 62 * Test for <code>hashCode()</code> method<br> 63 * Assertion: returns hash of the <code>Certificate</code> instance 64 */ 65 public final void testHashCode() { 66 Certificate c1 = new MyCertificate("TEST_TYPE", testEncoding); 67 Certificate c2 = new MyCertificate("TEST_TYPE", testEncoding); 68 69 assertTrue(c1.hashCode() == c2.hashCode()); 70 } 71 72 /** 73 * Test for <code>hashCode()</code> method<br> 74 * Assertion: hash code of equal objects should be the same 75 */ 76 public final void testHashCodeEqualsObject() { 77 Certificate c1 = new MyCertificate("TEST_TYPE", testEncoding); 78 Certificate c2 = new MyCertificate("TEST_TYPE", testEncoding); 79 80 assertTrue((c1.hashCode() == c2.hashCode()) && c1.equals(c2)); 81 } 82 83 84 /** 85 * Test for <code>getType()</code> method<br> 86 * Assertion: returns this certificate type 87 */ 88 public final void testGetType() { 89 Certificate c1 = new MyCertificate("TEST_TYPE", testEncoding); 90 assertEquals("TEST_TYPE", c1.getType()); 91 } 92 93 /** 94 * Test #1 for <code>equals(Object)</code> method<br> 95 * Assertion: object equals to itself 96 */ 97 public final void testEqualsObject01() { 98 Certificate c1 = new MyCertificate("TEST_TYPE", testEncoding); 99 assertTrue(c1.equals(c1)); 100 } 101 102 /** 103 * Test for <code>equals(Object)</code> method<br> 104 * Assertion: object equals to other <code>Certificate</code> 105 * instance with the same state 106 */ 107 public final void testEqualsObject02() { 108 Certificate c1 = new MyCertificate("TEST_TYPE", testEncoding); 109 Certificate c2 = new MyCertificate("TEST_TYPE", testEncoding); 110 assertTrue(c1.equals(c2) && c2.equals(c1)); 111 } 112 113 /** 114 * Test for <code>equals(Object)</code> method<br> 115 * Assertion: object not equals to <code>null</code> 116 */ 117 public final void testEqualsObject03() { 118 Certificate c1 = new MyCertificate("TEST_TYPE", testEncoding); 119 assertFalse(c1.equals(null)); 120 } 121 122 /** 123 * Test for <code>equals(Object)</code> method<br> 124 * Assertion: object not equals to other which is not 125 * instance of <code>Certificate</code> 126 */ 127 public final void testEqualsObject04() { 128 Certificate c1 = new MyCertificate("TEST_TYPE", testEncoding); 129 assertFalse(c1.equals("TEST_TYPE")); 130 } 131 132 // 133 // the following tests just call methods 134 // that are abstract in <code>Certificate</code> 135 // (So they just like signature tests) 136 // 137 138 /** 139 * This test just calls <code>getEncoded()</code> method<br> 140 * @throws CertificateEncodingException 141 */ 142 public final void testGetEncoded() throws CertificateEncodingException { 143 Certificate c1 = new MyCertificate("TEST_TYPE", testEncoding); 144 c1.getEncoded(); 145 } 146 147 /** 148 * This test just calls <code>verify(PublicKey)</code> method<br> 149 * 150 * @throws InvalidKeyException 151 * @throws CertificateException 152 * @throws NoSuchAlgorithmException 153 * @throws NoSuchProviderException 154 * @throws SignatureException 155 */ 156 public final void testVerifyPublicKey() 157 throws InvalidKeyException, 158 CertificateException, 159 NoSuchAlgorithmException, 160 NoSuchProviderException, 161 SignatureException { 162 Certificate c1 = new MyCertificate("TEST_TYPE", testEncoding); 163 c1.verify(null); 164 } 165 166 /** 167 * This test just calls <code>verify(PublicKey,String)</code> method<br> 168 * 169 * @throws InvalidKeyException 170 * @throws CertificateException 171 * @throws NoSuchAlgorithmException 172 * @throws NoSuchProviderException 173 * @throws SignatureException 174 */ 175 public final void testVerifyPublicKeyString() 176 throws InvalidKeyException, 177 CertificateException, 178 NoSuchAlgorithmException, 179 NoSuchProviderException, 180 SignatureException { 181 Certificate c1 = new MyCertificate("TEST_TYPE", testEncoding); 182 c1.verify(null, null); 183 } 184 185 /** 186 * This test just calls <code>toString()</code> method<br> 187 */ 188 public final void testToString() { 189 Certificate c1 = new MyCertificate("TEST_TYPE", testEncoding); 190 c1.toString(); 191 } 192 193 /** 194 * This test just calls <code>testGetPublicKey()</code> method<br> 195 */ 196 public final void testGetPublicKey() { 197 Certificate c1 = new MyCertificate("TEST_TYPE", testEncoding); 198 c1.getPublicKey(); 199 } 200 201} 202