aboutsummaryrefslogtreecommitdiffstats
path: root/libraries/spongycastle/core/src/test/java/org/spongycastle/asn1/test/RequestedCertificateUnitTest.java
blob: 389f07d3e55bc6bb70736012166cb49180c26f18 (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package org.spongycastle.asn1.test;

import java.io.IOException;

import org.spongycastle.asn1.ASN1InputStream;
import org.spongycastle.asn1.isismtt.ocsp.RequestedCertificate;
import org.spongycastle.asn1.x509.Certificate;
import org.spongycastle.util.encoders.Base64;

public class RequestedCertificateUnitTest
    extends ASN1UnitTest
{
   byte[]  certBytes = Base64.decode(
           "MIIBWzCCAQYCARgwDQYJKoZIhvcNAQEEBQAwODELMAkGA1UEBhMCQVUxDDAKBgNV"
         + "BAgTA1FMRDEbMBkGA1UEAxMSU1NMZWF5L3JzYSB0ZXN0IENBMB4XDTk1MDYxOTIz"
         + "MzMxMloXDTk1MDcxNzIzMzMxMlowOjELMAkGA1UEBhMCQVUxDDAKBgNVBAgTA1FM"
         + "RDEdMBsGA1UEAxMUU1NMZWF5L3JzYSB0ZXN0IGNlcnQwXDANBgkqhkiG9w0BAQEF"
         + "AANLADBIAkEAqtt6qS5GTxVxGZYWa0/4u+IwHf7p2LNZbcPBp9/OfIcYAXBQn8hO"
         + "/Re1uwLKXdCjIoaGs4DLdG88rkzfyK5dPQIDAQABMAwGCCqGSIb3DQIFBQADQQAE"
         + "Wc7EcF8po2/ZO6kNCwK/ICH6DobgLekA5lSLr5EvuioZniZp5lFzAw4+YzPQ7XKJ"
         + "zl9HYIMxATFyqSiD9jsx");

    public String getName()
    {
        return "RequestedCertificate";
    }

    public void performTest()
        throws Exception
    {
        int type = 1;
        byte[] certOctets = new byte[20];
        Certificate cert = Certificate.getInstance(certBytes);

        RequestedCertificate requested = new RequestedCertificate(type, certOctets);

        checkConstruction(requested, type, certOctets, null);

        requested = new RequestedCertificate(cert);

        checkConstruction(requested, RequestedCertificate.certificate, null, cert);

        requested = RequestedCertificate.getInstance(null);

        if (requested != null)
        {
            fail("null getInstance() failed.");
        }

        try
        {
            RequestedCertificate.getInstance(new Object());

            fail("getInstance() failed to detect bad object.");
        }
        catch (IllegalArgumentException e)
        {
            // expected
        }
    }

    private void checkConstruction(
        RequestedCertificate requested,
        int type,
        byte[] certOctets,
        Certificate cert)
        throws IOException
    {
        checkValues(requested, type, certOctets, cert);

        requested = RequestedCertificate.getInstance(requested);

        checkValues(requested, type, certOctets, cert);

        ASN1InputStream aIn = new ASN1InputStream(requested.toASN1Object().getEncoded());

        Object obj = aIn.readObject();

        requested = RequestedCertificate.getInstance(obj);

        checkValues(requested, type, certOctets, cert);
    }

    private void checkValues(
        RequestedCertificate requested,
        int type,
        byte[] certOctets,
        Certificate cert)
        throws IOException
    {
        checkMandatoryField("certType", type, requested.getType());

        if (requested.getType() == RequestedCertificate.certificate)
        {
            checkMandatoryField("certificate", cert.getEncoded(), requested.getCertificateBytes());
        }
        else
        {
            checkMandatoryField("certificateOctets", certOctets, requested.getCertificateBytes());
        }
    }

    public static void main(
        String[]    args)
    {
        runTest(new RequestedCertificateUnitTest());
    }
}