1 package org.bouncycastle.asn1.test; 2 3 import java.security.SecureRandom; 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 { getName()20 public String getName() 21 { 22 return "BiometricData"; 23 } 24 generateHash()25 private byte[] generateHash() 26 { 27 SecureRandom rand = new SecureRandom(); 28 byte[] bytes = new byte[20]; 29 30 rand.nextBytes(bytes); 31 32 return bytes; 33 } 34 performTest()35 public void performTest() 36 throws Exception 37 { 38 TypeOfBiometricData dataType = new TypeOfBiometricData(TypeOfBiometricData.HANDWRITTEN_SIGNATURE); 39 AlgorithmIdentifier hashAlgorithm = new AlgorithmIdentifier(OIWObjectIdentifiers.idSHA1, DERNull.INSTANCE); 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 checkConstruction( BiometricData bd, TypeOfBiometricData dataType, AlgorithmIdentifier hashAlgorithm, ASN1OctetString dataHash, DERIA5String dataUri)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.toASN1Primitive().getEncoded()); 85 86 ASN1Sequence seq = (ASN1Sequence)aIn.readObject(); 87 88 bd = BiometricData.getInstance(seq); 89 90 checkValues(bd, dataType, hashAlgorithm, dataHash, dataUri); 91 } 92 checkValues( BiometricData bd, TypeOfBiometricData dataType, AlgorithmIdentifier algID, ASN1OctetString dataHash, DERIA5String sourceDataURI)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 main( String[] args)128 public static void main( 129 String[] args) 130 { 131 runTest(new BiometricDataUnitTest()); 132 } 133 } 134