~brian-thomason/+junk/bouncycastle

« back to all changes in this revision

Viewing changes to test/src/org/bouncycastle/asn1/test/BiometricDataUnitTest.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 java.util.Random;
 
4
 
 
5
import org.bouncycastle.asn1.ASN1InputStream;
 
6
import org.bouncycastle.asn1.ASN1OctetString;
 
7
import org.bouncycastle.asn1.ASN1Sequence;
 
8
import org.bouncycastle.asn1.DERIA5String;
 
9
import org.bouncycastle.asn1.DERNull;
 
10
import org.bouncycastle.asn1.DEROctetString;
 
11
import org.bouncycastle.asn1.oiw.OIWObjectIdentifiers;
 
12
import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
 
13
import org.bouncycastle.asn1.x509.qualified.BiometricData;
 
14
import org.bouncycastle.asn1.x509.qualified.TypeOfBiometricData;
 
15
import org.bouncycastle.util.test.SimpleTest;
 
16
 
 
17
public class BiometricDataUnitTest 
 
18
    extends SimpleTest
 
19
{
 
20
    public String getName()
 
21
    {
 
22
        return "BiometricData";
 
23
    }
 
24
 
 
25
    private byte[] generateHash()
 
26
    {
 
27
        Random rand = new Random();
 
28
        byte[] bytes = new byte[20];
 
29
        
 
30
        rand.nextBytes(bytes);
 
31
        
 
32
        return bytes;
 
33
    }
 
34
    
 
35
    public void performTest() 
 
36
        throws Exception
 
37
    {
 
38
        TypeOfBiometricData dataType = new TypeOfBiometricData(TypeOfBiometricData.HANDWRITTEN_SIGNATURE);
 
39
        AlgorithmIdentifier hashAlgorithm = new AlgorithmIdentifier(OIWObjectIdentifiers.idSHA1, new DERNull());
 
40
        ASN1OctetString     dataHash = new DEROctetString(generateHash());
 
41
        BiometricData       bd = new BiometricData(dataType, hashAlgorithm, dataHash);
 
42
 
 
43
        checkConstruction(bd, dataType, hashAlgorithm, dataHash, null);
 
44
        
 
45
        DERIA5String dataUri = new DERIA5String("http://test");
 
46
        
 
47
        bd = new BiometricData(dataType, hashAlgorithm, dataHash, dataUri);
 
48
        
 
49
        checkConstruction(bd, dataType, hashAlgorithm, dataHash, dataUri);
 
50
        
 
51
        bd = BiometricData.getInstance(null);
 
52
        
 
53
        if (bd != null)
 
54
        {
 
55
            fail("null getInstance() failed.");
 
56
        }
 
57
        
 
58
        try
 
59
        {
 
60
            BiometricData.getInstance(new Object());
 
61
            
 
62
            fail("getInstance() failed to detect bad object.");
 
63
        }
 
64
        catch (IllegalArgumentException e)
 
65
        {
 
66
            // expected
 
67
        }
 
68
    }
 
69
 
 
70
    private void checkConstruction(
 
71
        BiometricData bd,
 
72
        TypeOfBiometricData dataType, 
 
73
        AlgorithmIdentifier hashAlgorithm,
 
74
        ASN1OctetString dataHash, 
 
75
        DERIA5String dataUri)
 
76
        throws Exception
 
77
    {
 
78
        checkValues(bd, dataType, hashAlgorithm, dataHash, dataUri);
 
79
 
 
80
        bd = BiometricData.getInstance(bd);
 
81
 
 
82
        checkValues(bd, dataType, hashAlgorithm, dataHash, dataUri);
 
83
 
 
84
        ASN1InputStream aIn = new ASN1InputStream(bd.toASN1Object().getEncoded());
 
85
 
 
86
        ASN1Sequence seq = (ASN1Sequence)aIn.readObject();
 
87
 
 
88
        bd = BiometricData.getInstance(seq);
 
89
 
 
90
        checkValues(bd, dataType, hashAlgorithm, dataHash, dataUri);
 
91
    }
 
92
 
 
93
    private void checkValues(
 
94
        BiometricData       bd,
 
95
        TypeOfBiometricData dataType,
 
96
        AlgorithmIdentifier algID,
 
97
        ASN1OctetString     dataHash,
 
98
        DERIA5String        sourceDataURI)
 
99
    {
 
100
        if (!bd.getTypeOfBiometricData().equals(dataType))
 
101
        {
 
102
            fail("types don't match.");
 
103
        }
 
104
        
 
105
        if (!bd.getHashAlgorithm().equals(algID))
 
106
        {
 
107
            fail("hash algorithms don't match.");
 
108
        }
 
109
        
 
110
        if (!bd.getBiometricDataHash().equals(dataHash))
 
111
        {
 
112
            fail("hash algorithms don't match.");
 
113
        }
 
114
        
 
115
        if (sourceDataURI != null)
 
116
        {
 
117
            if (!bd.getSourceDataUri().equals(sourceDataURI))
 
118
            {
 
119
                fail("data uris don't match.");
 
120
            }
 
121
        }
 
122
        else if (bd.getSourceDataUri() != null)
 
123
        {
 
124
            fail("data uri found when none expected.");
 
125
        }
 
126
    }
 
127
    
 
128
    public static void main(
 
129
        String[]    args)
 
130
    {
 
131
        runTest(new BiometricDataUnitTest());
 
132
    }
 
133
}