001: package org.bouncycastle.asn1.test;
002:
003: import java.io.IOException;
004: import java.util.Random;
005:
006: import org.bouncycastle.asn1.ASN1EncodableVector;
007: import org.bouncycastle.asn1.ASN1InputStream;
008: import org.bouncycastle.asn1.ASN1Sequence;
009: import org.bouncycastle.asn1.DEROctetString;
010: import org.bouncycastle.asn1.DERSequence;
011: import org.bouncycastle.asn1.icao.DataGroupHash;
012: import org.bouncycastle.asn1.icao.LDSSecurityObject;
013: import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
014: import org.bouncycastle.util.test.SimpleTest;
015:
016: public class LDSSecurityObjectUnitTest extends SimpleTest {
017: public String getName() {
018: return "LDSSecurityObject";
019: }
020:
021: private byte[] generateHash() {
022: Random rand = new Random();
023: byte[] bytes = new byte[20];
024:
025: for (int i = 0; i != bytes.length; i++) {
026: bytes[i] = (byte) rand.nextInt();
027: }
028:
029: return bytes;
030: }
031:
032: public void performTest() throws Exception {
033: AlgorithmIdentifier algoId = new AlgorithmIdentifier(
034: "1.3.14.3.2.26");
035: DataGroupHash[] datas = new DataGroupHash[2];
036:
037: datas[0] = new DataGroupHash(1, new DEROctetString(
038: generateHash()));
039: datas[1] = new DataGroupHash(2, new DEROctetString(
040: generateHash()));
041:
042: LDSSecurityObject so = new LDSSecurityObject(algoId, datas);
043:
044: checkConstruction(so, algoId, datas);
045:
046: so = LDSSecurityObject.getInstance(null);
047:
048: if (so != null) {
049: fail("null getInstance() failed.");
050: }
051:
052: try {
053: LDSSecurityObject.getInstance(new Object());
054:
055: fail("getInstance() failed to detect bad object.");
056: } catch (IllegalArgumentException e) {
057: // expected
058: }
059:
060: try {
061: ASN1EncodableVector v = new ASN1EncodableVector();
062:
063: new LDSSecurityObject(new DERSequence(v));
064:
065: fail("constructor failed to detect empty sequence.");
066: } catch (IllegalArgumentException e) {
067: // expected
068: }
069:
070: try {
071: new LDSSecurityObject(algoId, new DataGroupHash[1]);
072:
073: fail("constructor failed to detect small DataGroupHash array.");
074: } catch (IllegalArgumentException e) {
075: // expected
076: }
077:
078: try {
079: new LDSSecurityObject(
080: algoId,
081: new DataGroupHash[LDSSecurityObject.ub_DataGroups + 1]);
082:
083: fail("constructor failed to out of bounds DataGroupHash array.");
084: } catch (IllegalArgumentException e) {
085: // expected
086: }
087: }
088:
089: private void checkConstruction(LDSSecurityObject so,
090: AlgorithmIdentifier digestAlgorithmIdentifier,
091: DataGroupHash[] datagroupHash) throws IOException {
092: checkStatement(so, digestAlgorithmIdentifier, datagroupHash);
093:
094: so = LDSSecurityObject.getInstance(so);
095:
096: checkStatement(so, digestAlgorithmIdentifier, datagroupHash);
097:
098: ASN1InputStream aIn = new ASN1InputStream(so.toASN1Object()
099: .getEncoded());
100:
101: ASN1Sequence seq = (ASN1Sequence) aIn.readObject();
102:
103: so = LDSSecurityObject.getInstance(seq);
104:
105: checkStatement(so, digestAlgorithmIdentifier, datagroupHash);
106: }
107:
108: private void checkStatement(LDSSecurityObject so,
109: AlgorithmIdentifier digestAlgorithmIdentifier,
110: DataGroupHash[] datagroupHash) {
111: if (digestAlgorithmIdentifier != null) {
112: if (!so.getDigestAlgorithmIdentifier().equals(
113: digestAlgorithmIdentifier)) {
114: fail("ids don't match.");
115: }
116: } else if (so.getDigestAlgorithmIdentifier() != null) {
117: fail("digest algorithm Id found when none expected.");
118: }
119:
120: if (datagroupHash != null) {
121: DataGroupHash[] datas = so.getDatagroupHash();
122:
123: for (int i = 0; i != datas.length; i++) {
124: if (!datagroupHash[i].equals(datas[i])) {
125: fail("name registration authorities don't match.");
126: }
127: }
128: } else if (so.getDatagroupHash() != null) {
129: fail("data hash groups found when none expected.");
130: }
131: }
132:
133: public static void main(String[] args) {
134: runTest(new LDSSecurityObjectUnitTest());
135: }
136: }
|