001: package org.bouncycastle.asn1.test;
002:
003: import java.util.Hashtable;
004:
005: import org.bouncycastle.asn1.ASN1EncodableVector;
006: import org.bouncycastle.asn1.DERObjectIdentifier;
007: import org.bouncycastle.asn1.DERSet;
008: import org.bouncycastle.asn1.cms.Attribute;
009: import org.bouncycastle.asn1.cms.AttributeTable;
010: import org.bouncycastle.util.test.SimpleTest;
011:
012: public class AttributeTableUnitTest extends SimpleTest {
013: private static final DERObjectIdentifier type1 = new DERObjectIdentifier(
014: "1.1.1");
015: private static final DERObjectIdentifier type2 = new DERObjectIdentifier(
016: "1.1.2");
017: private static final DERObjectIdentifier type3 = new DERObjectIdentifier(
018: "1.1.3");
019:
020: public String getName() {
021: return "AttributeTable";
022: }
023:
024: public void performTest() throws Exception {
025: ASN1EncodableVector v = new ASN1EncodableVector();
026:
027: v.add(new Attribute(type1, new DERSet(type1)));
028: v.add(new Attribute(type2, new DERSet(type2)));
029:
030: AttributeTable table = new AttributeTable(v);
031:
032: Attribute a = table.get(type1);
033: if (a == null) {
034: fail("type1 attribute not found.");
035: }
036: if (!a.getAttrValues().equals(new DERSet(type1))) {
037: fail("wrong value retrieved for type1!");
038: }
039:
040: a = table.get(type2);
041: if (a == null) {
042: fail("type2 attribute not found.");
043: }
044: if (!a.getAttrValues().equals(new DERSet(type2))) {
045: fail("wrong value retrieved for type2!");
046: }
047:
048: a = table.get(type3);
049: if (a != null) {
050: fail("type3 attribute found when none expected.");
051: }
052:
053: ASN1EncodableVector vec = table.getAll(type1);
054: if (vec.size() != 1) {
055: fail("wrong vector size for type1.");
056: }
057:
058: vec = table.getAll(type3);
059: if (vec.size() != 0) {
060: fail("wrong vector size for type3.");
061: }
062:
063: vec = table.toASN1EncodableVector();
064: if (vec.size() != 2) {
065: fail("wrong vector size for single.");
066: }
067:
068: Hashtable t = table.toHashtable();
069:
070: if (t.size() != 2) {
071: fail("hashtable wrong size.");
072: }
073:
074: // multiple
075:
076: v = new ASN1EncodableVector();
077:
078: v.add(new Attribute(type1, new DERSet(type1)));
079: v.add(new Attribute(type1, new DERSet(type2)));
080: v.add(new Attribute(type1, new DERSet(type3)));
081: v.add(new Attribute(type2, new DERSet(type2)));
082:
083: table = new AttributeTable(v);
084:
085: a = table.get(type1);
086: if (!a.getAttrValues().equals(new DERSet(type1))) {
087: fail("wrong value retrieved for type1 multi get!");
088: }
089:
090: vec = table.getAll(type1);
091: if (vec.size() != 3) {
092: fail("wrong vector size for multiple type1.");
093: }
094:
095: a = (Attribute) vec.get(0);
096: if (!a.getAttrValues().equals(new DERSet(type1))) {
097: fail("wrong value retrieved for type1(0)!");
098: }
099:
100: a = (Attribute) vec.get(1);
101: if (!a.getAttrValues().equals(new DERSet(type2))) {
102: fail("wrong value retrieved for type1(1)!");
103: }
104:
105: a = (Attribute) vec.get(2);
106: if (!a.getAttrValues().equals(new DERSet(type3))) {
107: fail("wrong value retrieved for type1(2)!");
108: }
109:
110: vec = table.getAll(type2);
111: if (vec.size() != 1) {
112: fail("wrong vector size for multiple type2.");
113: }
114:
115: vec = table.toASN1EncodableVector();
116: if (vec.size() != 4) {
117: fail("wrong vector size for multiple.");
118: }
119: }
120:
121: public static void main(String[] args) {
122: runTest(new AttributeTableUnitTest());
123: }
124: }
|