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.DERUTF8String;
07: import org.bouncycastle.asn1.ess.ContentHints;
08:
09: import java.io.IOException;
10:
11: public class ContentHintsUnitTest extends ASN1UnitTest {
12: public String getName() {
13: return "ContentHints";
14: }
15:
16: public void performTest() throws Exception {
17: DERUTF8String contentDescription = new DERUTF8String(
18: "Description");
19: DERObjectIdentifier contentType = new DERObjectIdentifier(
20: "1.2.2.3");
21:
22: ContentHints hints = new ContentHints(contentType);
23:
24: checkConstruction(hints, contentType, null);
25:
26: hints = new ContentHints(contentType, contentDescription);
27:
28: checkConstruction(hints, contentType, contentDescription);
29:
30: hints = ContentHints.getInstance(null);
31:
32: if (hints != null) {
33: fail("null getInstance() failed.");
34: }
35:
36: try {
37: ContentHints.getInstance(new Object());
38:
39: fail("getInstance() failed to detect bad object.");
40: } catch (IllegalArgumentException e) {
41: // expected
42: }
43: }
44:
45: private void checkConstruction(ContentHints hints,
46: DERObjectIdentifier contentType, DERUTF8String description)
47: throws IOException {
48: checkValues(hints, contentType, description);
49:
50: hints = ContentHints.getInstance(hints);
51:
52: checkValues(hints, contentType, description);
53:
54: ASN1InputStream aIn = new ASN1InputStream(hints.toASN1Object()
55: .getEncoded());
56:
57: ASN1Sequence seq = (ASN1Sequence) aIn.readObject();
58:
59: hints = ContentHints.getInstance(seq);
60:
61: checkValues(hints, contentType, description);
62: }
63:
64: private void checkValues(ContentHints hints,
65: DERObjectIdentifier contentType, DERUTF8String description) {
66: checkMandatoryField("contentType", contentType, hints
67: .getContentType());
68: checkOptionalField("description", description, hints
69: .getContentDescription());
70: }
71:
72: public static void main(String[] args) {
73: runTest(new ContentHintsUnitTest());
74: }
75: }
|