01: package org.bouncycastle.asn1.test;
02:
03: import org.bouncycastle.asn1.ASN1InputStream;
04: import org.bouncycastle.asn1.ASN1Sequence;
05: import org.bouncycastle.asn1.DERInteger;
06: import org.bouncycastle.asn1.DERObjectIdentifier;
07: import org.bouncycastle.asn1.ess.OtherCertID;
08: import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
09: import org.bouncycastle.asn1.x509.GeneralName;
10: import org.bouncycastle.asn1.x509.GeneralNames;
11: import org.bouncycastle.asn1.x509.IssuerSerial;
12: import org.bouncycastle.asn1.x509.X509Name;
13:
14: import java.io.IOException;
15:
16: public class OtherCertIDUnitTest extends ASN1UnitTest {
17: public String getName() {
18: return "OtherCertID";
19: }
20:
21: public void performTest() throws Exception {
22: AlgorithmIdentifier algId = new AlgorithmIdentifier(
23: new DERObjectIdentifier("1.2.2.3"));
24: byte[] digest = new byte[20];
25: IssuerSerial issuerSerial = new IssuerSerial(new GeneralNames(
26: new GeneralName(new X509Name("CN=test"))),
27: new DERInteger(1));
28:
29: OtherCertID certID = new OtherCertID(algId, digest);
30:
31: checkConstruction(certID, algId, digest, null);
32:
33: certID = new OtherCertID(algId, digest, issuerSerial);
34:
35: checkConstruction(certID, algId, digest, issuerSerial);
36:
37: certID = OtherCertID.getInstance(null);
38:
39: if (certID != null) {
40: fail("null getInstance() failed.");
41: }
42:
43: try {
44: OtherCertID.getInstance(new Object());
45:
46: fail("getInstance() failed to detect bad object.");
47: } catch (IllegalArgumentException e) {
48: // expected
49: }
50: }
51:
52: private void checkConstruction(OtherCertID certID,
53: AlgorithmIdentifier algId, byte[] digest,
54: IssuerSerial issuerSerial) throws IOException {
55: checkValues(certID, algId, digest, issuerSerial);
56:
57: certID = OtherCertID.getInstance(certID);
58:
59: checkValues(certID, algId, digest, issuerSerial);
60:
61: ASN1InputStream aIn = new ASN1InputStream(certID.toASN1Object()
62: .getEncoded());
63:
64: ASN1Sequence seq = (ASN1Sequence) aIn.readObject();
65:
66: certID = OtherCertID.getInstance(seq);
67:
68: checkValues(certID, algId, digest, issuerSerial);
69: }
70:
71: private void checkValues(OtherCertID certID,
72: AlgorithmIdentifier algId, byte[] digest,
73: IssuerSerial issuerSerial) {
74: checkMandatoryField("algorithmHash", algId, certID
75: .getAlgorithmHash());
76: checkMandatoryField("certHash", digest, certID.getCertHash());
77:
78: checkOptionalField("issuerSerial", issuerSerial, certID
79: .getIssuerSerial());
80: }
81:
82: public static void main(String[] args) {
83: runTest(new OtherCertIDUnitTest());
84: }
85: }
|