01: package org.bouncycastle.asn1.test;
02:
03: import org.bouncycastle.asn1.ASN1InputStream;
04: import org.bouncycastle.asn1.ASN1Sequence;
05: import org.bouncycastle.asn1.isismtt.ocsp.RequestedCertificate;
06: import org.bouncycastle.asn1.x509.X509CertificateStructure;
07: import org.bouncycastle.util.encoders.Base64;
08:
09: import java.io.IOException;
10:
11: public class RequestedCertificateUnitTest extends ASN1UnitTest {
12: byte[] certBytes = Base64
13: .decode("MIIBWzCCAQYCARgwDQYJKoZIhvcNAQEEBQAwODELMAkGA1UEBhMCQVUxDDAKBgNV"
14: + "BAgTA1FMRDEbMBkGA1UEAxMSU1NMZWF5L3JzYSB0ZXN0IENBMB4XDTk1MDYxOTIz"
15: + "MzMxMloXDTk1MDcxNzIzMzMxMlowOjELMAkGA1UEBhMCQVUxDDAKBgNVBAgTA1FM"
16: + "RDEdMBsGA1UEAxMUU1NMZWF5L3JzYSB0ZXN0IGNlcnQwXDANBgkqhkiG9w0BAQEF"
17: + "AANLADBIAkEAqtt6qS5GTxVxGZYWa0/4u+IwHf7p2LNZbcPBp9/OfIcYAXBQn8hO"
18: + "/Re1uwLKXdCjIoaGs4DLdG88rkzfyK5dPQIDAQABMAwGCCqGSIb3DQIFBQADQQAE"
19: + "Wc7EcF8po2/ZO6kNCwK/ICH6DobgLekA5lSLr5EvuioZniZp5lFzAw4+YzPQ7XKJ"
20: + "zl9HYIMxATFyqSiD9jsx");
21:
22: public String getName() {
23: return "RequestedCertificate";
24: }
25:
26: public void performTest() throws Exception {
27: int type = 1;
28: byte[] certOctets = new byte[20];
29: X509CertificateStructure cert = new X509CertificateStructure(
30: (ASN1Sequence) new ASN1InputStream(certBytes)
31: .readObject());
32:
33: RequestedCertificate requested = new RequestedCertificate(type,
34: certOctets);
35:
36: checkConstruction(requested, type, certOctets, null);
37:
38: requested = new RequestedCertificate(cert);
39:
40: checkConstruction(requested, RequestedCertificate.certificate,
41: null, cert);
42:
43: requested = RequestedCertificate.getInstance(null);
44:
45: if (requested != null) {
46: fail("null getInstance() failed.");
47: }
48:
49: try {
50: RequestedCertificate.getInstance(new Object());
51:
52: fail("getInstance() failed to detect bad object.");
53: } catch (IllegalArgumentException e) {
54: // expected
55: }
56: }
57:
58: private void checkConstruction(RequestedCertificate requested,
59: int type, byte[] certOctets, X509CertificateStructure cert)
60: throws IOException {
61: checkValues(requested, type, certOctets, cert);
62:
63: requested = RequestedCertificate.getInstance(requested);
64:
65: checkValues(requested, type, certOctets, cert);
66:
67: ASN1InputStream aIn = new ASN1InputStream(requested
68: .toASN1Object().getEncoded());
69:
70: Object obj = aIn.readObject();
71:
72: requested = RequestedCertificate.getInstance(obj);
73:
74: checkValues(requested, type, certOctets, cert);
75: }
76:
77: private void checkValues(RequestedCertificate requested, int type,
78: byte[] certOctets, X509CertificateStructure cert)
79: throws IOException {
80: checkMandatoryField("certType", type, requested.getType());
81:
82: if (requested.getType() == RequestedCertificate.certificate) {
83: checkMandatoryField("certificate", cert.getEncoded(),
84: requested.getCertificateBytes());
85: } else {
86: checkMandatoryField("certificateOctets", certOctets,
87: requested.getCertificateBytes());
88: }
89: }
90:
91: public static void main(String[] args) {
92: runTest(new RequestedCertificateUnitTest());
93: }
94: }
|