001: package org.bouncycastle.sasn1.test;
002:
003: import java.io.ByteArrayOutputStream;
004: import java.math.BigInteger;
005: import java.util.Arrays;
006:
007: import junit.framework.Test;
008: import junit.framework.TestCase;
009: import junit.framework.TestSuite;
010:
011: import org.bouncycastle.sasn1.Asn1InputStream;
012: import org.bouncycastle.sasn1.Asn1Integer;
013: import org.bouncycastle.sasn1.Asn1ObjectIdentifier;
014: import org.bouncycastle.sasn1.Asn1Sequence;
015: import org.bouncycastle.sasn1.BerSequenceGenerator;
016: import org.bouncycastle.sasn1.DerSequenceGenerator;
017: import org.bouncycastle.util.encoders.Hex;
018:
019: public class Asn1SequenceTest extends TestCase {
020: private static final byte[] seqData = Hex
021: .decode("3006020100060129");
022: private static final byte[] nestedSeqData = Hex
023: .decode("300b0201000601293003020101");
024: private static final byte[] expTagSeqData = Hex
025: .decode("a1083006020100060129");
026: private static final byte[] implTagSeqData = Hex
027: .decode("a106020100060129");
028: private static final byte[] nestedSeqExpTagData = Hex
029: .decode("300d020100060129a1053003020101");
030: private static final byte[] nestedSeqImpTagData = Hex
031: .decode("300b020100060129a103020101");
032:
033: private static final byte[] berSeqData = Hex
034: .decode("30800201000601290000");
035: private static final byte[] berDerNestedSeqData = Hex
036: .decode("308002010006012930030201010000");
037: private static final byte[] berNestedSeqData = Hex
038: .decode("3080020100060129308002010100000000");
039: private static final byte[] berExpTagSeqData = Hex
040: .decode("a180308002010006012900000000");
041:
042: public void testDerWriting() throws Exception {
043: ByteArrayOutputStream bOut = new ByteArrayOutputStream();
044: DerSequenceGenerator seqGen = new DerSequenceGenerator(bOut);
045:
046: seqGen.addObject(new Asn1Integer(BigInteger.valueOf(0)));
047:
048: seqGen.addObject(new Asn1ObjectIdentifier("1.1"));
049:
050: seqGen.close();
051:
052: assertTrue("basic DER writing test failed.", Arrays.equals(
053: seqData, bOut.toByteArray()));
054: }
055:
056: public void testNestedDerWriting() throws Exception {
057: ByteArrayOutputStream bOut = new ByteArrayOutputStream();
058: DerSequenceGenerator seqGen1 = new DerSequenceGenerator(bOut);
059:
060: seqGen1.addObject(new Asn1Integer(BigInteger.valueOf(0)));
061:
062: seqGen1.addObject(new Asn1ObjectIdentifier("1.1"));
063:
064: DerSequenceGenerator seqGen2 = new DerSequenceGenerator(seqGen1
065: .getRawOutputStream());
066:
067: seqGen2.addObject(new Asn1Integer(BigInteger.valueOf(1)));
068:
069: seqGen2.close();
070:
071: seqGen1.close();
072:
073: assertTrue("nested DER writing test failed.", Arrays.equals(
074: nestedSeqData, bOut.toByteArray()));
075: }
076:
077: public void testDerExplicitTaggedSequenceWriting() throws Exception {
078: ByteArrayOutputStream bOut = new ByteArrayOutputStream();
079: DerSequenceGenerator seqGen = new DerSequenceGenerator(bOut, 1,
080: true);
081:
082: seqGen.addObject(new Asn1Integer(BigInteger.valueOf(0)));
083:
084: seqGen.addObject(new Asn1ObjectIdentifier("1.1"));
085:
086: seqGen.close();
087:
088: assertTrue("explicit tag writing test failed.", Arrays.equals(
089: expTagSeqData, bOut.toByteArray()));
090: }
091:
092: public void testDerImplicitTaggedSequenceWriting() throws Exception {
093: ByteArrayOutputStream bOut = new ByteArrayOutputStream();
094: DerSequenceGenerator seqGen = new DerSequenceGenerator(bOut, 1,
095: false);
096:
097: seqGen.addObject(new Asn1Integer(BigInteger.valueOf(0)));
098:
099: seqGen.addObject(new Asn1ObjectIdentifier("1.1"));
100:
101: seqGen.close();
102:
103: assertTrue("implicit tag writing test failed.", Arrays.equals(
104: implTagSeqData, bOut.toByteArray()));
105: }
106:
107: public void testNestedExplicitTagDerWriting() throws Exception {
108: ByteArrayOutputStream bOut = new ByteArrayOutputStream();
109: DerSequenceGenerator seqGen1 = new DerSequenceGenerator(bOut);
110:
111: seqGen1.addObject(new Asn1Integer(BigInteger.valueOf(0)));
112:
113: seqGen1.addObject(new Asn1ObjectIdentifier("1.1"));
114:
115: DerSequenceGenerator seqGen2 = new DerSequenceGenerator(seqGen1
116: .getRawOutputStream(), 1, true);
117:
118: seqGen2.addObject(new Asn1Integer(BigInteger.valueOf(1)));
119:
120: seqGen2.close();
121:
122: seqGen1.close();
123:
124: assertTrue("nested explicit tagged DER writing test failed.",
125: Arrays.equals(nestedSeqExpTagData, bOut.toByteArray()));
126: }
127:
128: public void testNestedImplicitTagDerWriting() throws Exception {
129: ByteArrayOutputStream bOut = new ByteArrayOutputStream();
130: DerSequenceGenerator seqGen1 = new DerSequenceGenerator(bOut);
131:
132: seqGen1.addObject(new Asn1Integer(BigInteger.valueOf(0)));
133:
134: seqGen1.addObject(new Asn1ObjectIdentifier("1.1"));
135:
136: DerSequenceGenerator seqGen2 = new DerSequenceGenerator(seqGen1
137: .getRawOutputStream(), 1, false);
138:
139: seqGen2.addObject(new Asn1Integer(BigInteger.valueOf(1)));
140:
141: seqGen2.close();
142:
143: seqGen1.close();
144:
145: assertTrue("nested implicit tagged DER writing test failed.",
146: Arrays.equals(nestedSeqImpTagData, bOut.toByteArray()));
147: }
148:
149: public void testBerWriting() throws Exception {
150: ByteArrayOutputStream bOut = new ByteArrayOutputStream();
151: BerSequenceGenerator seqGen = new BerSequenceGenerator(bOut);
152:
153: seqGen.addObject(new Asn1Integer(BigInteger.valueOf(0)));
154:
155: seqGen.addObject(new Asn1ObjectIdentifier("1.1"));
156:
157: seqGen.close();
158:
159: assertTrue("basic BER writing test failed.", Arrays.equals(
160: berSeqData, bOut.toByteArray()));
161: }
162:
163: public void testNestedBerDerWriting() throws Exception {
164: ByteArrayOutputStream bOut = new ByteArrayOutputStream();
165: BerSequenceGenerator seqGen1 = new BerSequenceGenerator(bOut);
166:
167: seqGen1.addObject(new Asn1Integer(BigInteger.valueOf(0)));
168:
169: seqGen1.addObject(new Asn1ObjectIdentifier("1.1"));
170:
171: DerSequenceGenerator seqGen2 = new DerSequenceGenerator(seqGen1
172: .getRawOutputStream());
173:
174: seqGen2.addObject(new Asn1Integer(BigInteger.valueOf(1)));
175:
176: seqGen2.close();
177:
178: seqGen1.close();
179:
180: assertTrue("nested BER/DER writing test failed.", Arrays
181: .equals(berDerNestedSeqData, bOut.toByteArray()));
182: }
183:
184: public void testNestedBerWriting() throws Exception {
185: ByteArrayOutputStream bOut = new ByteArrayOutputStream();
186: BerSequenceGenerator seqGen1 = new BerSequenceGenerator(bOut);
187:
188: seqGen1.addObject(new Asn1Integer(BigInteger.valueOf(0)));
189:
190: seqGen1.addObject(new Asn1ObjectIdentifier("1.1"));
191:
192: BerSequenceGenerator seqGen2 = new BerSequenceGenerator(seqGen1
193: .getRawOutputStream());
194:
195: seqGen2.addObject(new Asn1Integer(BigInteger.valueOf(1)));
196:
197: seqGen2.close();
198:
199: seqGen1.close();
200:
201: assertTrue("nested BER writing test failed.", Arrays.equals(
202: berNestedSeqData, bOut.toByteArray()));
203: }
204:
205: public void testDerReading() throws Exception {
206: Asn1InputStream aIn = new Asn1InputStream(seqData);
207:
208: Asn1Sequence seq = (Asn1Sequence) aIn.readObject();
209: Object o = null;
210: int count = 0;
211:
212: assertNotNull("null sequence returned", seq);
213:
214: while ((o = seq.readObject()) != null) {
215: switch (count) {
216: case 0:
217: assertTrue(o instanceof Asn1Integer);
218: break;
219: case 1:
220: assertTrue(o instanceof Asn1ObjectIdentifier);
221: break;
222: }
223: count++;
224: }
225:
226: assertEquals("wrong number of objects in sequence", 2, count);
227: }
228:
229: public void testNestedReading(byte[] data) throws Exception {
230: Asn1InputStream aIn = new Asn1InputStream(data);
231:
232: Asn1Sequence seq = (Asn1Sequence) aIn.readObject();
233: Object o = null;
234: int count = 0;
235:
236: assertNotNull("null sequence returned", seq);
237:
238: while ((o = seq.readObject()) != null) {
239: switch (count) {
240: case 0:
241: assertTrue(o instanceof Asn1Integer);
242: break;
243: case 1:
244: assertTrue(o instanceof Asn1ObjectIdentifier);
245: break;
246: case 2:
247: assertTrue(o instanceof Asn1Sequence);
248:
249: Asn1Sequence s = (Asn1Sequence) o;
250:
251: s.readObject();
252:
253: break;
254: }
255: count++;
256: }
257:
258: assertEquals("wrong number of objects in sequence", 3, count);
259: }
260:
261: public void testNestedDerReading() throws Exception {
262: testNestedReading(nestedSeqData);
263: }
264:
265: public void testBerReading() throws Exception {
266: Asn1InputStream aIn = new Asn1InputStream(berSeqData);
267:
268: Asn1Sequence seq = (Asn1Sequence) aIn.readObject();
269: Object o = null;
270: int count = 0;
271:
272: assertNotNull("null sequence returned", seq);
273:
274: while ((o = seq.readObject()) != null) {
275: switch (count) {
276: case 0:
277: assertTrue(o instanceof Asn1Integer);
278: break;
279: case 1:
280: assertTrue(o instanceof Asn1ObjectIdentifier);
281: break;
282: }
283: count++;
284: }
285:
286: assertEquals("wrong number of objects in sequence", 2, count);
287: }
288:
289: public void testNestedBerDerReading() throws Exception {
290: testNestedReading(berDerNestedSeqData);
291: }
292:
293: public void testNestedBerReading() throws Exception {
294: testNestedReading(berNestedSeqData);
295: }
296:
297: public void testBerExplicitTaggedSequenceWriting() throws Exception {
298: ByteArrayOutputStream bOut = new ByteArrayOutputStream();
299: BerSequenceGenerator seqGen = new BerSequenceGenerator(bOut, 1,
300: true);
301:
302: seqGen.addObject(new Asn1Integer(BigInteger.valueOf(0)));
303:
304: seqGen.addObject(new Asn1ObjectIdentifier("1.1"));
305:
306: seqGen.close();
307:
308: assertTrue("explicit BER tag writing test failed.", Arrays
309: .equals(berExpTagSeqData, bOut.toByteArray()));
310: }
311:
312: public static Test suite() {
313: return new TestSuite(Asn1SequenceTest.class);
314: }
315: }
|