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* @version $Revision$
21*/
22
23package tests.security.cert;
24
25import java.io.ObjectStreamException;
26import java.security.cert.CertPath;
27import java.security.cert.CertificateEncodingException;
28import java.util.Arrays;
29import junit.framework.TestCase;
30import org.apache.harmony.security.tests.support.cert.MyCertPath;
31import org.apache.harmony.security.tests.support.cert.MyFailingCertPath;
32import org.apache.harmony.security.tests.support.cert.TestUtils;
33import org.apache.harmony.testframework.serialization.SerializationTest;
34
35/**
36 * Tests for <code>CertPath</code> fields and methods
37 *
38 */
39public class CertPathTest extends TestCase {
40    /**
41     * Meaningless cert path encoding just for testing purposes
42     */
43    private static final byte[] testEncoding = new byte[] {
44            (byte)1, (byte)2, (byte)3, (byte)4, (byte)5
45    };
46
47    private static final byte[] testEncoding1 = new byte[] {
48        (byte)1, (byte)2, (byte)3, (byte)4, (byte)5, (byte)6
49    };
50
51    //
52    // Tests
53    //
54
55    /**
56     * Test for <code>CertPath(String type)</code> method<br>
57     * Assertion: returns hash of the <code>Certificate</code> instance
58     */
59    public final void testCertPath() {
60        try {
61            CertPath cp1 = new MyCertPath(testEncoding);
62            assertEquals("MyEncoding", cp1.getType());
63            assertTrue(Arrays.equals(testEncoding, cp1.getEncoded()));
64        } catch (CertificateEncodingException e) {
65            fail("Unexpected CertificateEncodingException " + e.getMessage());
66        }
67
68        try {
69            CertPath cp1 = new MyCertPath(null);
70        } catch (Exception e) {
71            fail("Unexpected exception " + e.getMessage());
72        }
73    }
74
75    /**
76     * Test for <code>hashCode()</code> method<br>
77     * Assertion: returns hash of the <code>Certificate</code> instance
78     */
79    public final void testHashCode() {
80        CertPath cp1 = new MyCertPath(testEncoding);
81        CertPath cp2 = new MyCertPath(testEncoding);
82        CertPath cp3 = new MyCertPath(testEncoding1);
83
84        assertTrue(cp1.hashCode() == cp2.hashCode());
85        assertTrue(cp1.hashCode() != cp3.hashCode());
86    }
87
88    /**
89     * Test for <code>hashCode()</code> method<br>
90     * Assertion: hash code of equal objects should be the same
91     */
92    public final void testHashCodeEqualsObject() {
93        CertPath cp1 = new MyCertPath(testEncoding);
94        CertPath cp2 = new MyCertPath(testEncoding);
95        assertTrue((cp1.hashCode() == cp2.hashCode()) && cp1.equals(cp2));
96    }
97
98    /**
99     * Test for <code>getType()</code> method<br>
100     * Assertion: returns cert path type
101     */
102    public final void testGetType() {
103        assertEquals("MyEncoding", new MyCertPath(testEncoding).getType());
104    }
105
106    /**
107     * Test #1 for <code>equals(Object)</code> method<br>
108     * Assertion: object equals to itself
109     */
110    public final void testEqualsObject01() {
111        CertPath cp1 = new MyCertPath(testEncoding);
112        assertTrue(cp1.equals(cp1));
113    }
114
115    /**
116     * Test for <code>equals(Object)</code> method<br>
117     * Assertion: object equals to other <code>CertPath</code>
118     * instance with the same state
119     */
120    public final void testEqualsObject02() {
121        CertPath cp1 = new MyCertPath(testEncoding);
122        CertPath cp2 = new MyCertPath(testEncoding);
123        assertTrue(cp1.equals(cp2) && cp2.equals(cp1));
124    }
125
126    /**
127     * Test for <code>equals(Object)</code> method<br>
128     * Assertion: object not equals to <code>null</code>
129     */
130    public final void testEqualsObject03() {
131        CertPath cp1 = new MyCertPath(testEncoding);
132        assertFalse(cp1.equals(null));
133    }
134
135    /**
136     * Test for <code>equals(Object)</code> method<br>
137     * Assertion: object not equals to other which is not
138     * instance of <code>CertPath</code>
139     */
140    public final void testEqualsObject04() {
141        CertPath cp1 = new MyCertPath(testEncoding);
142        assertFalse(cp1.equals("MyEncoding"));
143    }
144
145    /**
146     * Test for <code>toString()</code> method<br>
147     * Assertion: returns string representation of
148     * <code>CertPath</code> object
149     */
150    public final void testToString() {
151        CertPath cp1 = new MyCertPath(testEncoding);
152        assertNotNull(cp1.toString());
153    }
154
155    //
156    // the following tests just call methods
157    // that are abstract in <code>CertPath</code>
158    // (So they just like signature tests)
159    //
160
161    /**
162     * This test just calls <code>getCertificates()</code> method<br>
163     */
164    public final void testGetCertificates() {
165        CertPath cp1 = new MyCertPath(testEncoding);
166        cp1.getCertificates();
167    }
168
169    /**
170     * This test just calls <code>getEncoded()</code> method<br>
171     *
172     * @throws CertificateEncodingException
173     */
174    public final void testGetEncoded() throws CertificateEncodingException {
175        CertPath cp1 = new MyCertPath(testEncoding);
176        cp1.getEncoded();
177    }
178
179    /**
180     * This test just calls <code>getEncoded(String)</code> method<br>
181     *
182     * @throws CertificateEncodingException
183     */
184    public final void testGetEncodedString() throws CertificateEncodingException {
185        CertPath cp1 = new MyCertPath(testEncoding);
186        cp1.getEncoded("MyEncoding");
187    }
188
189    /**
190     * This test just calls <code>getEncodings()</code> method<br>
191     */
192    public final void testGetEncodings() {
193        CertPath cp1 = new MyCertPath(testEncoding);
194        cp1.getEncodings();
195    }
196
197    /**
198     * This test just calls <code>writeReplace()</code> method<br>
199     */
200    public final void testWriteReplace() {
201        try {
202            MyCertPath cp1 = new MyCertPath(testEncoding);
203            Object obj = cp1.writeReplace();
204            assertTrue(obj.toString().contains(
205                    "java.security.cert.CertPath$CertPathRep"));
206        } catch (ObjectStreamException e) {
207            fail("Unexpected ObjectStreamException " + e.getMessage());
208        }
209    }
210
211    public final void testWriteReplace_ObjectStreamException() {
212        try {
213            MyFailingCertPath cp = new MyFailingCertPath(testEncoding);
214            Object obj = cp.writeReplace();
215            fail("expected ObjectStreamException");
216        } catch (ObjectStreamException e) {
217            // ok
218        }
219    }
220
221    /**
222     * serialization/deserialization compatibility.
223     */
224    public void testSerializationSelf() throws Exception {
225        TestUtils.initCertPathSSCertChain();
226        CertPath certPath = TestUtils.buildCertPathSSCertChain();
227
228        SerializationTest.verifySelf(certPath);
229    }
230
231    /**
232     * serialization/deserialization compatibility with RI.
233     */
234    public void testSerializationCompatibility() throws Exception {
235        TestUtils.initCertPathSSCertChain();
236        CertPath certPath = TestUtils.buildCertPathSSCertChain();
237
238        SerializationTest.verifyGolden(this, certPath);
239    }
240}
241