01: package org.bouncycastle.asn1.test;
02:
03: import org.bouncycastle.asn1.ASN1InputStream;
04: import org.bouncycastle.asn1.DERGeneralizedTime;
05: import org.bouncycastle.asn1.DERTaggedObject;
06: import org.bouncycastle.asn1.isismtt.x509.DeclarationOfMajority;
07:
08: import java.io.IOException;
09:
10: public class DeclarationOfMajorityUnitTest extends ASN1UnitTest {
11: public String getName() {
12: return "DeclarationOfMajority";
13: }
14:
15: public void performTest() throws Exception {
16: DERGeneralizedTime dateOfBirth = new DERGeneralizedTime(
17: "20070315173729Z");
18: DeclarationOfMajority decl = new DeclarationOfMajority(
19: dateOfBirth);
20:
21: checkConstruction(decl, DeclarationOfMajority.dateOfBirth,
22: dateOfBirth, -1);
23:
24: decl = new DeclarationOfMajority(6);
25:
26: checkConstruction(decl, DeclarationOfMajority.notYoungerThan,
27: null, 6);
28:
29: decl = DeclarationOfMajority.getInstance(null);
30:
31: if (decl != null) {
32: fail("null getInstance() failed.");
33: }
34:
35: try {
36: DeclarationOfMajority.getInstance(new Object());
37:
38: fail("getInstance() failed to detect bad object.");
39: } catch (IllegalArgumentException e) {
40: // expected
41: }
42: }
43:
44: private void checkConstruction(DeclarationOfMajority decl,
45: int type, DERGeneralizedTime dateOfBirth, int notYoungerThan)
46: throws IOException {
47: checkValues(decl, type, dateOfBirth, notYoungerThan);
48:
49: decl = DeclarationOfMajority.getInstance(decl);
50:
51: checkValues(decl, type, dateOfBirth, notYoungerThan);
52:
53: ASN1InputStream aIn = new ASN1InputStream(decl.toASN1Object()
54: .getEncoded());
55:
56: DERTaggedObject info = (DERTaggedObject) aIn.readObject();
57:
58: decl = DeclarationOfMajority.getInstance(info);
59:
60: checkValues(decl, type, dateOfBirth, notYoungerThan);
61: }
62:
63: private void checkValues(DeclarationOfMajority decl, int type,
64: DERGeneralizedTime dateOfBirth, int notYoungerThan) {
65: checkMandatoryField("type", type, decl.getType());
66: checkOptionalField("dateOfBirth", dateOfBirth, decl
67: .getDateOfBirth());
68: if (notYoungerThan != -1
69: && notYoungerThan != decl.notYoungerThan()) {
70: fail("notYoungerThan mismatch");
71: }
72: }
73:
74: public static void main(String[] args) {
75: runTest(new DeclarationOfMajorityUnitTest());
76: }
77: }
|