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