summaryrefslogtreecommitdiffstats
path: root/luni/src/test/java/tests/security/cert/CertificateCertificateRepTest.java
blob: 7d973e72ff95425dc38dfebe48d8fa6383a7a0f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package tests.security.cert;

import junit.framework.TestCase;

import org.apache.harmony.security.tests.support.cert.MyCertificate;
import org.apache.harmony.security.tests.support.cert.TestUtils;
import org.apache.harmony.security.tests.support.cert.MyCertificate.MyCertificateRep;

import java.io.ObjectStreamException;
import java.security.cert.Certificate;
import java.util.Arrays;

public class CertificateCertificateRepTest extends TestCase {

    private static final byte[] testEncoding = new byte[] { (byte) 1, (byte) 2,
            (byte) 3, (byte) 4, (byte) 5 };

    protected void setUp() throws Exception {
        super.setUp();
    }

    protected void tearDown() throws Exception {
        super.tearDown();
    }

    /**
     * Test for
     * <code>Certificate.CertificateRep(String type, byte[] data)</code>
     * method<br>
     */
    public final void testCertificateCertificateRep() {
        MyCertificate c1 = new MyCertificate("TEST_TYPE", testEncoding);
        MyCertificateRep rep = c1.new MyCertificateRep("TEST_TYPE", new byte[] {
                (byte) 1, (byte) 2, (byte) 3 });

        assertTrue(Arrays.equals(new byte[] { (byte) 1, (byte) 2, (byte) 3 },
                rep.getData()));
        assertEquals("TEST_TYPE", rep.getType());

        try {
            c1.new MyCertificateRep(null, null);
        } catch (Exception e) {
            fail("Unexpected exeption " + e.getMessage());
        }

        try {
            MyCertificate.MyCertificateRep rep1 = c1.new MyCertificateRep(
                    "X509", TestUtils.getX509Certificate_v3());
            assertEquals("X509", rep1.getType());
            assertTrue(Arrays.equals(TestUtils.getX509Certificate_v3(), rep1.getData()));
        } catch (Exception e) {
            fail("Unexpected exeption " + e.getMessage());
        }
    }

    /**
     * Test for <code>readResolve()</code> method<br>
     */
    public final void testReadResolve() {
        MyCertificate c1 = new MyCertificate("TEST_TYPE", testEncoding);
        MyCertificateRep rep = c1.new MyCertificateRep("TEST_TYPE", new byte[] {
                (byte) 1, (byte) 2, (byte) 3 });

        try {
            rep.readResolve();
            fail("ObjectStreamException expected");
        } catch (ObjectStreamException e) {
            // expected
        }

        MyCertificateRep rep1 = c1.new MyCertificateRep("X509", TestUtils
                .getX509Certificate_v3());
        try {
            Certificate obj = (Certificate) rep1.readResolve();
            assertEquals("0.3.5", obj.getPublicKey().getAlgorithm());
            assertEquals("X.509", obj.getPublicKey().getFormat());
            assertEquals("X.509", obj.getType());
        } catch (ObjectStreamException e) {
            fail("Unexpected ObjectStreamException " + e.getMessage());
        }
    }
}