001: package org.bouncycastle.openpgp.test;
002:
003: import java.io.ByteArrayOutputStream;
004: import java.io.ByteArrayInputStream;
005:
006: import org.bouncycastle.bcpg.ArmoredOutputStream;
007: import org.bouncycastle.bcpg.ArmoredInputStream;
008: import org.bouncycastle.util.encoders.Base64;
009: import org.bouncycastle.util.encoders.Hex;
010: import org.bouncycastle.util.test.SimpleTest;
011: import org.bouncycastle.openpgp.PGPObjectFactory;
012:
013: public class PGPArmoredTest extends SimpleTest {
014: byte[] sample = Base64
015: .decode("mQGiBEA83v0RBADzKVLVCnpWQxX0LCsevw/3OLs0H7MOcLBQ4wMO9sYmzGYn"
016: + "xpVj+4e4PiCP7QBayWyy4lugL6Lnw7tESvq3A4v3fefcxaCTkJrryiKn4+Cg"
017: + "y5rIBbrSKNtCEhVi7xjtdnDjP5kFKgHYjVOeIKn4Cz/yzPG3qz75kDknldLf"
018: + "yHxp2wCgwW1vAE5EnZU4/UmY7l8kTNkMltMEAJP4/uY4zcRwLI9Q2raPqAOJ"
019: + "TYLd7h+3k/BxI0gIw96niQ3KmUZDlobbWBI+VHM6H99vcttKU3BgevNf8M9G"
020: + "x/AbtW3SS4De64wNSU3189XDG8vXf0vuyW/K6Pcrb8exJWY0E1zZQ1WXT0gZ"
021: + "W0kH3g5ro//Tusuil9q2lVLF2ovJA/0W+57bPzi318dWeNs0tTq6Njbc/GTG"
022: + "FUAVJ8Ss5v2u6h7gyJ1DB334ExF/UdqZGldp0ugkEXaSwBa2R7d3HBgaYcoP"
023: + "Ck1TrovZzEY8gm7JNVy7GW6mdOZuDOHTxyADEEP2JPxh6eRcZbzhGuJuYIif"
024: + "IIeLOTI5Dc4XKeV32a+bWrQidGVzdCAoVGVzdCBrZXkpIDx0ZXN0QHViaWNh"
025: + "bGwuY29tPohkBBMRAgAkBQJAPN79AhsDBQkB4TOABgsJCAcDAgMVAgMDFgIB"
026: + "Ah4BAheAAAoJEJh8Njfhe8KmGDcAoJWr8xgPr75y/Cp1kKn12oCCOb8zAJ4p"
027: + "xSvk4K6tB2jYbdeSrmoWBZLdMLACAAC5AQ0EQDzfARAEAJeUAPvUzJJbKcc5"
028: + "5Iyb13+Gfb8xBWE3HinQzhGr1v6A1aIZbRj47UPAD/tQxwz8VAwJySx82ggN"
029: + "LxCk4jW9YtTL3uZqfczsJngV25GoIN10f4/j2BVqZAaX3q79a3eMiql1T0oE"
030: + "AGmD7tO1LkTvWfm3VvA0+t8/6ZeRLEiIqAOHAAQNBACD0mVMlAUgd7REYy/1"
031: + "mL99Zlu9XU0uKyUex99sJNrcx1aj8rIiZtWaHz6CN1XptdwpDeSYEOFZ0PSu"
032: + "qH9ByM3OfjU/ya0//xdvhwYXupn6P1Kep85efMBA9jUv/DeBOzRWMFG6sC6y"
033: + "k8NGG7Swea7EHKeQI40G3jgO/+xANtMyTIhPBBgRAgAPBQJAPN8BAhsMBQkB"
034: + "4TOAAAoJEJh8Njfhe8KmG7kAn00mTPGJCWqmskmzgdzeky5fWd7rAKCNCp3u"
035: + "ZJhfg0htdgAfIy8ppm05vLACAAA=");
036:
037: byte[] marker = Hex
038: .decode("2d2d2d2d2d454e4420504750205055424c4943204b455920424c4f434b2d2d2d2d2d");
039:
040: private int markerCount(byte[] data) {
041: int ind = 0;
042: int matches = 0;
043:
044: while (ind < data.length) {
045: if (data[ind] == 0x2d) {
046: int count = 0;
047: while (count < marker.length) {
048: if (data[ind + count] != marker[count]) {
049: break;
050: }
051: count++;
052: }
053:
054: if (count == marker.length) {
055: matches++;
056: }
057:
058: ind += count;
059: } else {
060: ind++;
061: }
062: }
063:
064: return matches;
065: }
066:
067: public void performTest() throws Exception {
068: //
069: // test immediate close
070: //
071: ByteArrayOutputStream bOut = new ByteArrayOutputStream();
072: ArmoredOutputStream aOut = new ArmoredOutputStream(bOut);
073:
074: aOut.close();
075:
076: byte[] data = bOut.toByteArray();
077:
078: if (data.length != 0) {
079: fail("No data should have been written");
080: }
081:
082: //
083: // multiple close
084: //
085: bOut = new ByteArrayOutputStream();
086: aOut = new ArmoredOutputStream(bOut);
087:
088: aOut.write(sample);
089:
090: aOut.close();
091:
092: aOut.close();
093:
094: int mc = markerCount(bOut.toByteArray());
095:
096: if (mc < 1) {
097: fail("No end marker found");
098: }
099:
100: if (mc > 1) {
101: fail("More than one end marker found");
102: }
103:
104: //
105: // writing and reading single objects
106: //
107: bOut = new ByteArrayOutputStream();
108: aOut = new ArmoredOutputStream(bOut);
109:
110: aOut.write(sample);
111:
112: aOut.close();
113:
114: ArmoredInputStream aIn = new ArmoredInputStream(
115: new ByteArrayInputStream(bOut.toByteArray()));
116:
117: PGPObjectFactory fact = new PGPObjectFactory(aIn);
118: int count = 0;
119:
120: while (fact.nextObject() != null) {
121: count++;
122: }
123:
124: if (count != 1) {
125: fail("wrong number of objects found: " + count);
126: }
127:
128: //
129: // writing and reading multiple objects - in single block
130: //
131: bOut = new ByteArrayOutputStream();
132: aOut = new ArmoredOutputStream(bOut);
133:
134: aOut.write(sample);
135: aOut.write(sample);
136:
137: aOut.close();
138:
139: aIn = new ArmoredInputStream(new ByteArrayInputStream(bOut
140: .toByteArray()));
141:
142: fact = new PGPObjectFactory(aIn);
143: count = 0;
144:
145: while (fact.nextObject() != null) {
146: count++;
147: }
148:
149: if (count != 2) {
150: fail("wrong number of objects found: " + count);
151: }
152:
153: //
154: // writing and reading multiple objects - in single block
155: //
156: bOut = new ByteArrayOutputStream();
157: aOut = new ArmoredOutputStream(bOut);
158:
159: aOut.write(sample);
160:
161: aOut.close(); // does not close underlying stream
162:
163: aOut = new ArmoredOutputStream(bOut);
164:
165: aOut.write(sample);
166:
167: aOut.close();
168:
169: aIn = new ArmoredInputStream(new ByteArrayInputStream(bOut
170: .toByteArray()));
171:
172: count = 0;
173: boolean atLeastOne;
174: do {
175: atLeastOne = false;
176: fact = new PGPObjectFactory(aIn);
177:
178: while (fact.nextObject() != null) {
179: atLeastOne = true;
180: count++;
181: }
182: } while (atLeastOne);
183:
184: if (count != 2) {
185: fail("wrong number of objects found: " + count);
186: }
187: }
188:
189: public String getName() {
190: return "PGPArmoredTest";
191: }
192:
193: public static void main(String[] args) {
194: runTest(new PGPArmoredTest());
195: }
196: }
|