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.serialization;
23
24import java.io.ByteArrayInputStream;
25import java.io.ObjectStreamException;
26import java.security.cert.Certificate;
27import java.security.cert.CertificateFactory;
28
29import junit.framework.TestCase;
30
31import org.apache.harmony.security.tests.support.cert.MyCertificate;
32import org.apache.harmony.security.tests.support.cert.TestUtils;
33import org.apache.harmony.testframework.serialization.SerializationTest;
34
35/**
36 * Tests for <code>Certificate</code> serialization
37 */
38public class CertificateTest extends TestCase {
39
40    // certificate type to be created during testing
41    private static final String certType = "X.509";
42
43    /**
44     * @tests serialization/deserialization.
45     */
46    public void testSerializationSelf() throws Exception {
47
48        CertificateFactory cf = CertificateFactory.getInstance(certType);
49
50        Certificate cert = cf.generateCertificate(new ByteArrayInputStream(
51                TestUtils.getEncodedX509Certificate()));
52
53        SerializationTest.verifySelf(cert);
54    }
55
56    /**
57     * @tests serialization/deserialization compatibility with RI.
58     */
59    public void testSerializationCompatibility() throws Exception {
60
61        CertificateFactory cf = CertificateFactory.getInstance(certType);
62
63        Certificate cert = cf.generateCertificate(new ByteArrayInputStream(
64                TestUtils.getEncodedX509Certificate()));
65
66        SerializationTest.verifyGolden(this, cert);
67    }
68
69    /**
70     * Test for <code>Certificate.CertificateRep.readResolve()</code> method<br>
71     * <p/>
72     * Assertion: ObjectStreamException if a <code>CertPath</code> could not
73     * be constructed
74     */
75    public final void testCertificateRep_readResolve() throws Exception {
76        // Create object to be serialized
77        Certificate c1 = new MyCertificate("DUMMY", new byte[] { (byte) 0, (byte) 1 });
78
79        // try to serialize/deserialize cert
80        try {
81            SerializationTest.copySerializable(c1);
82            fail("No expected ObjectStreamException");
83        } catch (ObjectStreamException e) {
84        }
85    }
86
87    /**
88     * Test for <code>writeReplace()</code> method<br>
89     * ByteArray streams used.
90     */
91    public final void testWriteReplace() throws Exception {
92        // Create object to be serialized
93        Certificate c1 = new MyCertificate("DUMMY", null);
94
95        // Try to serialize cert
96        try {
97            SerializationTest.copySerializable(c1);
98            fail("No exception");
99        } catch (ObjectStreamException e) {
100        } catch (NullPointerException e) {
101        }
102    }
103}
104