~brian-thomason/+junk/bouncycastle

« back to all changes in this revision

Viewing changes to test/src/org/bouncycastle/asn1/test/DeclarationOfMajorityUnitTest.java

  • Committer: Brian Thomason
  • Date: 2011-12-20 17:20:32 UTC
  • Revision ID: brian.thomason@canonical.com-20111220172032-rdtm13jgdxtksacr
Initial import

Show diffs side-by-side

added added

removed removed

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