summaryrefslogtreecommitdiffstats
path: root/bcprov/src/main/java/org/bouncycastle/asn1/test/TypeOfBiometricDataUnitTest.java
blob: add21ce37136ff80ac330f46312761fff0b9c401 (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package org.bouncycastle.asn1.test;

import java.io.IOException;

import org.bouncycastle.asn1.ASN1InputStream;
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
import org.bouncycastle.asn1.ASN1Primitive;
import org.bouncycastle.asn1.x509.qualified.TypeOfBiometricData;
import org.bouncycastle.util.test.SimpleTest;

public class TypeOfBiometricDataUnitTest 
    extends SimpleTest
{
    public String getName()
    {
        return "TypeOfBiometricData";
    }

    public void performTest() 
        throws Exception
    {
        //
        // predefined
        //
        checkPredefinedType(TypeOfBiometricData.PICTURE);
        
        checkPredefinedType(TypeOfBiometricData.HANDWRITTEN_SIGNATURE);
        
        //
        // non-predefined
        //
        ASN1ObjectIdentifier localType = new ASN1ObjectIdentifier("1.1");
        
        TypeOfBiometricData type = new TypeOfBiometricData(localType);

        checkNonPredefined(type, localType);
        
        type = TypeOfBiometricData.getInstance(type);
        
        checkNonPredefined(type, localType);
        
        ASN1Primitive obj = type.toASN1Primitive();
        
        type = TypeOfBiometricData.getInstance(obj);
        
        checkNonPredefined(type, localType);
        
        type = TypeOfBiometricData.getInstance(null);
        
        if (type != null)
        {
            fail("null getInstance() failed.");
        }
        
        try
        {
            TypeOfBiometricData.getInstance(new Object());
            
            fail("getInstance() failed to detect bad object.");
        }
        catch (IllegalArgumentException e)
        {
            // expected
        }
        
        try
        {
            new TypeOfBiometricData(100);
            
            fail("constructor failed to detect bad predefined type.");
        }
        catch (IllegalArgumentException e)
        {
            // expected
        }
        
        if (TypeOfBiometricData.PICTURE != 0)
        {
            fail("predefined picture should be 0");
        }
        
        if (TypeOfBiometricData.HANDWRITTEN_SIGNATURE != 1)
        {
            fail("predefined handwritten signature should be 1");
        }
    }

    private void checkPredefinedType(
        int predefinedType)
        throws IOException
    {
        TypeOfBiometricData type = new TypeOfBiometricData(predefinedType);

        checkPredefined(type, predefinedType);
        
        type = TypeOfBiometricData.getInstance(type);
        
        checkPredefined(type, predefinedType);
        
        ASN1InputStream aIn = new ASN1InputStream(type.toASN1Object().getEncoded());

        ASN1Primitive obj = aIn.readObject();

        type = TypeOfBiometricData.getInstance(obj);
        
        checkPredefined(type, predefinedType);
    }

    private void checkPredefined(
        TypeOfBiometricData type,
        int                 value)
    {
        if (!type.isPredefined())
        {
            fail("predefined type expected but not found.");
        }
        
        if (type.getPredefinedBiometricType() != value)
        {
            fail("predefined type does not match.");
        }
    }
    
    private void checkNonPredefined(
        TypeOfBiometricData type,
        ASN1ObjectIdentifier value)
    {
        if (type.isPredefined())
        {
            fail("predefined type found when not expected.");
        }
        
        if (!type.getBiometricDataOid().equals(value))
        {
            fail("data oid does not match.");
        }
    }
    
    public static void main(
        String[]    args)
    {
        runTest(new TypeOfBiometricDataUnitTest());
    }
}