aboutsummaryrefslogtreecommitdiffstats
path: root/libraries/spongycastle/core/src/test/java/org/spongycastle/asn1/test/ContentHintsUnitTest.java
blob: b0610f648c94f9e17561a80c198040b61dd2acbe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package org.spongycastle.asn1.test;

import java.io.IOException;

import org.spongycastle.asn1.ASN1InputStream;
import org.spongycastle.asn1.ASN1ObjectIdentifier;
import org.spongycastle.asn1.ASN1Sequence;
import org.spongycastle.asn1.DERUTF8String;
import org.spongycastle.asn1.ess.ContentHints;

public class ContentHintsUnitTest
    extends ASN1UnitTest
{
    public String getName()
    {
        return "ContentHints";
    }

    public void performTest()
        throws Exception
    {
        DERUTF8String contentDescription = new DERUTF8String("Description");
        ASN1ObjectIdentifier contentType = new ASN1ObjectIdentifier("1.2.2.3");

        ContentHints hints = new ContentHints(contentType);

        checkConstruction(hints, contentType, null);

        hints = new ContentHints(contentType, contentDescription);

        checkConstruction(hints, contentType, contentDescription);

        hints = ContentHints.getInstance(null);

        if (hints != null)
        {
            fail("null getInstance() failed.");
        }

        try
        {
            ContentHints.getInstance(new Object());

            fail("getInstance() failed to detect bad object.");
        }
        catch (IllegalArgumentException e)
        {
            // expected
        }
    }

    private void checkConstruction(
        ContentHints hints,
        ASN1ObjectIdentifier contentType,
        DERUTF8String description)
        throws IOException
    {
        checkValues(hints, contentType, description);

        hints = ContentHints.getInstance(hints);

        checkValues(hints, contentType, description);

        ASN1InputStream aIn = new ASN1InputStream(hints.toASN1Primitive().getEncoded());

        ASN1Sequence seq = (ASN1Sequence)aIn.readObject();

        hints = ContentHints.getInstance(seq);

        checkValues(hints, contentType, description);
    }

    private void checkValues(
        ContentHints hints,
        ASN1ObjectIdentifier contentType,
        DERUTF8String description)
    {
        checkMandatoryField("contentType", contentType, hints.getContentType());
        checkOptionalField("description", description, hints.getContentDescription());
    }

    public static void main(
        String[]    args)
    {
        runTest(new ContentHintsUnitTest());
    }
}