01: package org.bouncycastle.asn1.test;
02:
03: import org.bouncycastle.asn1.ASN1EncodableVector;
04: import org.bouncycastle.asn1.ASN1Set;
05: import org.bouncycastle.asn1.ASN1TaggedObject;
06: import org.bouncycastle.asn1.BERSet;
07: import org.bouncycastle.asn1.DERBitString;
08: import org.bouncycastle.asn1.DERBoolean;
09: import org.bouncycastle.asn1.DERInteger;
10: import org.bouncycastle.asn1.DEROctetString;
11: import org.bouncycastle.asn1.DERSequence;
12: import org.bouncycastle.asn1.DERSet;
13: import org.bouncycastle.asn1.DERTaggedObject;
14: import org.bouncycastle.util.test.SimpleTestResult;
15: import org.bouncycastle.util.test.Test;
16: import org.bouncycastle.util.test.TestResult;
17:
18: /**
19: * Set sorting test example
20: */
21: public class SetTest implements Test {
22:
23: public String getName() {
24: return "Set";
25: }
26:
27: public TestResult perform() {
28: try {
29: ASN1EncodableVector v = new ASN1EncodableVector();
30: byte[] data = new byte[10];
31:
32: v.add(new DEROctetString(data));
33: v.add(new DERBitString(data));
34: v.add(new DERInteger(100));
35: v.add(new DERBoolean(true));
36:
37: ASN1Set s = new DERSet(v);
38:
39: if (!(s.getObjectAt(0) instanceof DERBoolean)) {
40: return new SimpleTestResult(false, getName()
41: + ": sorting failed.");
42: }
43:
44: s = new BERSet(v);
45:
46: if (!(s.getObjectAt(0) instanceof DEROctetString)) {
47: return new SimpleTestResult(false, getName()
48: + ": BER set sort order changed.");
49: }
50:
51: // create an implicitly tagged "set" without sorting
52: ASN1TaggedObject tag = new DERTaggedObject(false, 1,
53: new DERSequence(v));
54: s = ASN1Set.getInstance(tag, false);
55:
56: if (s.getObjectAt(0) instanceof DERBoolean) {
57: return new SimpleTestResult(false, getName()
58: + ": sorted when shouldn't be.");
59: }
60:
61: // equality test
62: v = new ASN1EncodableVector();
63:
64: v.add(new DERBoolean(true));
65: v.add(new DERBoolean(true));
66: v.add(new DERBoolean(true));
67:
68: s = new DERSet(v);
69:
70: return new SimpleTestResult(true, getName() + ": Okay");
71: } catch (Exception e) {
72: return new SimpleTestResult(false, getName()
73: + ": Exception - " + e.toString(), e);
74: }
75: }
76:
77: public static void main(String[] args) {
78: Test test = new SetTest();
79:
80: TestResult result = test.perform();
81:
82: System.out.println(result);
83: }
84: }
|