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