~brian-thomason/+junk/bouncycastle

« back to all changes in this revision

Viewing changes to test/src/org/bouncycastle/asn1/test/TagTest.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.ASN1TaggedObject;
 
5
import org.bouncycastle.asn1.DERApplicationSpecific;
 
6
import org.bouncycastle.asn1.ASN1Object;
 
7
import org.bouncycastle.util.encoders.Base64;
 
8
import org.bouncycastle.util.encoders.Hex;
 
9
import org.bouncycastle.util.test.SimpleTest;
 
10
 
 
11
import java.io.IOException;
 
12
import java.security.SecureRandom;
 
13
 
 
14
 
 
15
/**
 
16
 * X.690 test example
 
17
 */
 
18
public class TagTest
 
19
    extends SimpleTest
 
20
{
 
21
    byte[] longTagged = Base64.decode(
 
22
                  "ZSRzIp8gEEZFRENCQTk4NzY1NDMyMTCfIQwyMDA2MDQwMTEyMzSUCCAFERVz"
 
23
                + "A4kCAHEXGBkalAggBRcYGRqUCCAFZS6QAkRFkQlURUNITklLRVKSBQECAwQF"
 
24
                + "kxAREhMUFRYXGBkalAggBREVcwOJAgBxFxgZGpQIIAUXGBkalAggBWUukAJE"
 
25
                + "RZEJVEVDSE5JS0VSkgUBAgMEBZMQERITFBUWFxgZGpQIIAURFXMDiQIAcRcY"
 
26
                + "GRqUCCAFFxgZGpQIIAVlLpACREWRCVRFQ0hOSUtFUpIFAQIDBAWTEBESExQV"
 
27
                + "FhcYGRqUCCAFERVzA4kCAHEXGBkalAggBRcYGRqUCCAFFxgZGpQIIAUXGBka"
 
28
                + "lAg=");
 
29
 
 
30
    byte[] longAppSpecificTag = Hex.decode("5F610101");
 
31
 
 
32
    public String getName()
 
33
    {
 
34
        return "Tag";
 
35
    }
 
36
    
 
37
    public void performTest()
 
38
        throws IOException
 
39
    {
 
40
        ASN1InputStream aIn = new ASN1InputStream(longTagged);
 
41
 
 
42
        DERApplicationSpecific app = (DERApplicationSpecific)aIn.readObject();
 
43
        
 
44
        aIn = new ASN1InputStream(app.getContents());
 
45
 
 
46
        app = (DERApplicationSpecific)aIn.readObject();
 
47
 
 
48
        aIn = new ASN1InputStream(app.getContents());
 
49
 
 
50
        ASN1TaggedObject tagged = (ASN1TaggedObject)aIn.readObject();
 
51
 
 
52
        if (tagged.getTagNo() != 32)
 
53
        {
 
54
            fail("unexpected tag value found - not 32");
 
55
        }
 
56
 
 
57
        tagged = (ASN1TaggedObject)ASN1Object.fromByteArray(tagged.getEncoded());
 
58
 
 
59
        if (tagged.getTagNo() != 32)
 
60
        {
 
61
            fail("unexpected tag value found on recode - not 32");
 
62
        }
 
63
 
 
64
        tagged = (ASN1TaggedObject)aIn.readObject();
 
65
 
 
66
        if (tagged.getTagNo() != 33)
 
67
        {
 
68
            fail("unexpected tag value found - not 33");
 
69
        }
 
70
 
 
71
        tagged = (ASN1TaggedObject)ASN1Object.fromByteArray(tagged.getEncoded());
 
72
 
 
73
        if (tagged.getTagNo() != 33)
 
74
        {
 
75
            fail("unexpected tag value found on recode - not 33");
 
76
        }
 
77
 
 
78
        aIn = new ASN1InputStream(longAppSpecificTag);
 
79
 
 
80
        app = (DERApplicationSpecific)aIn.readObject();
 
81
 
 
82
        if (app.getApplicationTag() != 97)
 
83
        {
 
84
            fail("incorrect tag number read");
 
85
        }
 
86
 
 
87
        app = (DERApplicationSpecific)ASN1Object.fromByteArray(app.getEncoded());
 
88
 
 
89
        if (app.getApplicationTag() != 97)
 
90
        {
 
91
            fail("incorrect tag number read on recode");
 
92
        }
 
93
 
 
94
        SecureRandom sr = new SecureRandom();
 
95
        for (int i = 0; i < 100; ++i)
 
96
        {
 
97
            int testTag = sr.nextInt() >>> (1 + (sr.nextInt() >>> 1) % 26);
 
98
            app = new DERApplicationSpecific(testTag, new byte[]{ 1 });
 
99
            app = (DERApplicationSpecific)ASN1Object.fromByteArray(app.getEncoded());
 
100
 
 
101
            if (app.getApplicationTag() != testTag)
 
102
            {
 
103
                fail("incorrect tag number read on recode (random test value: " + testTag + ")");
 
104
            }
 
105
        }
 
106
    }
 
107
 
 
108
    public static void main(
 
109
        String[]    args)
 
110
    {
 
111
        runTest(new TagTest());
 
112
    }
 
113
}