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