001: package org.bouncycastle.asn1.test;
002:
003: import java.io.ByteArrayOutputStream;
004: import java.io.InputStream;
005: import java.io.OutputStream;
006:
007: import junit.framework.Test;
008: import junit.framework.TestCase;
009: import junit.framework.TestSuite;
010:
011: import org.bouncycastle.asn1.cms.CMSObjectIdentifiers;
012: import org.bouncycastle.asn1.cms.ContentInfoParser;
013: import org.bouncycastle.asn1.cms.CompressedDataParser;
014: import org.bouncycastle.asn1.BEROctetStringGenerator;
015: import org.bouncycastle.asn1.ASN1StreamParser;
016: import org.bouncycastle.asn1.ASN1OctetStringParser;
017: import org.bouncycastle.asn1.BERSequenceGenerator;
018: import org.bouncycastle.asn1.ASN1SequenceParser;
019: import org.bouncycastle.asn1.DERInteger;
020: import org.bouncycastle.asn1.DERObjectIdentifier;
021: import org.bouncycastle.asn1.DERSequenceGenerator;
022: import org.bouncycastle.asn1.DERTags;
023:
024: public class OctetStringTest extends TestCase {
025: public void testReadingWriting() throws Exception {
026: ByteArrayOutputStream bOut = new ByteArrayOutputStream();
027: BEROctetStringGenerator octGen = new BEROctetStringGenerator(
028: bOut);
029:
030: OutputStream out = octGen.getOctetOutputStream();
031:
032: out.write(new byte[] { 1, 2, 3, 4 });
033: out.write(new byte[4]);
034:
035: out.close();
036:
037: ASN1StreamParser aIn = new ASN1StreamParser(bOut.toByteArray());
038:
039: ASN1OctetStringParser s = (ASN1OctetStringParser) aIn
040: .readObject();
041:
042: InputStream in = s.getOctetStream();
043: int count = 0;
044:
045: while (in.read() >= 0) {
046: count++;
047: }
048:
049: assertEquals(8, count);
050: }
051:
052: public void testReadingWritingZeroInLength() throws Exception {
053: ByteArrayOutputStream bOut = new ByteArrayOutputStream();
054: BEROctetStringGenerator octGen = new BEROctetStringGenerator(
055: bOut);
056:
057: OutputStream out = octGen.getOctetOutputStream();
058:
059: out.write(new byte[] { 1, 2, 3, 4 });
060: out.write(new byte[512]); // forces a zero to appear in length
061:
062: out.close();
063:
064: ASN1StreamParser aIn = new ASN1StreamParser(bOut.toByteArray());
065:
066: ASN1OctetStringParser s = (ASN1OctetStringParser) aIn
067: .readObject();
068:
069: InputStream in = s.getOctetStream();
070: int count = 0;
071:
072: while (in.read() >= 0) {
073: count++;
074: }
075:
076: assertEquals(516, count);
077: }
078:
079: public void testReadingWritingNested() throws Exception {
080: ByteArrayOutputStream bOut = new ByteArrayOutputStream();
081: BERSequenceGenerator sGen = new BERSequenceGenerator(bOut);
082: BEROctetStringGenerator octGen = new BEROctetStringGenerator(
083: sGen.getRawOutputStream());
084:
085: OutputStream out = octGen.getOctetOutputStream();
086:
087: BERSequenceGenerator inSGen = new BERSequenceGenerator(out);
088:
089: BEROctetStringGenerator inOctGen = new BEROctetStringGenerator(
090: inSGen.getRawOutputStream());
091:
092: OutputStream inOut = inOctGen.getOctetOutputStream();
093:
094: inOut.write(new byte[] { 1, 2, 3, 4 });
095: inOut.write(new byte[10]);
096:
097: inOut.close();
098:
099: inSGen.close();
100:
101: out.close();
102:
103: sGen.close();
104:
105: ASN1StreamParser aIn = new ASN1StreamParser(bOut.toByteArray());
106:
107: ASN1SequenceParser sq = (ASN1SequenceParser) aIn.readObject();
108:
109: ASN1OctetStringParser s = (ASN1OctetStringParser) sq
110: .readObject();
111:
112: ASN1StreamParser aIn2 = new ASN1StreamParser(s.getOctetStream());
113:
114: ASN1SequenceParser sq2 = (ASN1SequenceParser) aIn2.readObject();
115:
116: ASN1OctetStringParser inS = (ASN1OctetStringParser) sq2
117: .readObject();
118:
119: InputStream in = inS.getOctetStream();
120: int count = 0;
121:
122: while (in.read() >= 0) {
123: count++;
124: }
125:
126: assertEquals(14, count);
127: }
128:
129: public void testNestedStructure() throws Exception {
130: ByteArrayOutputStream bOut = new ByteArrayOutputStream();
131:
132: BERSequenceGenerator sGen = new BERSequenceGenerator(bOut);
133:
134: sGen.addObject(new DERObjectIdentifier(
135: CMSObjectIdentifiers.compressedData.getId()));
136:
137: BERSequenceGenerator cGen = new BERSequenceGenerator(sGen
138: .getRawOutputStream(), 0, true);
139:
140: cGen.addObject(new DERInteger(0));
141:
142: //
143: // AlgorithmIdentifier
144: //
145: DERSequenceGenerator algGen = new DERSequenceGenerator(cGen
146: .getRawOutputStream());
147:
148: algGen.addObject(new DERObjectIdentifier("1.2"));
149:
150: algGen.close();
151:
152: //
153: // Encapsulated ContentInfo
154: //
155: BERSequenceGenerator eiGen = new BERSequenceGenerator(cGen
156: .getRawOutputStream());
157:
158: eiGen.addObject(new DERObjectIdentifier("1.1"));
159:
160: BEROctetStringGenerator octGen = new BEROctetStringGenerator(
161: eiGen.getRawOutputStream(), 0, true);
162:
163: //
164: // output containing zeroes
165: //
166: OutputStream out = octGen.getOctetOutputStream();
167:
168: out.write(new byte[] { 1, 2, 3, 4 });
169: out.write(new byte[4]);
170: out.write(new byte[20]);
171:
172: out.close();
173: eiGen.close();
174: cGen.close();
175: sGen.close();
176:
177: //
178: // reading back
179: //
180: ASN1StreamParser aIn = new ASN1StreamParser(bOut.toByteArray());
181:
182: ContentInfoParser cp = new ContentInfoParser(
183: (ASN1SequenceParser) aIn.readObject());
184:
185: CompressedDataParser comData = new CompressedDataParser(
186: (ASN1SequenceParser) cp.getContent(DERTags.SEQUENCE));
187: ContentInfoParser content = comData.getEncapContentInfo();
188:
189: ASN1OctetStringParser bytes = (ASN1OctetStringParser) content
190: .getContent(DERTags.OCTET_STRING);
191:
192: InputStream in = bytes.getOctetStream();
193: int count = 0;
194:
195: while (in.read() >= 0) {
196: count++;
197: }
198:
199: assertEquals(28, count);
200: }
201:
202: public static Test suite() {
203: return new TestSuite(OctetStringTest.class);
204: }
205: }
|