01: package org.bouncycastle.openpgp.test;
02:
03: import org.bouncycastle.jce.provider.BouncyCastleProvider;
04: import org.bouncycastle.openpgp.PGPLiteralData;
05: import org.bouncycastle.openpgp.PGPLiteralDataGenerator;
06: import org.bouncycastle.openpgp.PGPObjectFactory;
07: import org.bouncycastle.util.test.SimpleTest;
08: import org.bouncycastle.util.test.UncloseableOutputStream;
09:
10: import java.io.ByteArrayOutputStream;
11: import java.io.IOException;
12: import java.io.InputStream;
13: import java.io.OutputStream;
14: import java.security.Security;
15: import java.util.Date;
16: import java.util.Random;
17:
18: public class PGPPacketTest extends SimpleTest {
19: private static int MAX = 32000;
20:
21: private void readBackTest(PGPLiteralDataGenerator generator)
22: throws IOException {
23: Random rand = new Random();
24: byte[] buf = new byte[MAX];
25:
26: rand.nextBytes(buf);
27:
28: for (int i = 1; i <= 200; i++) {
29: bufferTest(generator, buf, i);
30: }
31:
32: bufferTest(generator, buf, 8382);
33: bufferTest(generator, buf, 8383);
34: bufferTest(generator, buf, 8384);
35: bufferTest(generator, buf, 8385);
36:
37: for (int i = 200; i < MAX; i += 100) {
38: bufferTest(generator, buf, i);
39: }
40: }
41:
42: private void bufferTest(PGPLiteralDataGenerator generator,
43: byte[] buf, int i) throws IOException {
44: ByteArrayOutputStream bOut = new ByteArrayOutputStream();
45: OutputStream out = generator.open(new UncloseableOutputStream(
46: bOut), PGPLiteralData.BINARY, PGPLiteralData.CONSOLE,
47: i, new Date());
48:
49: out.write(buf, 0, i);
50:
51: generator.close();
52:
53: PGPObjectFactory fact = new PGPObjectFactory(bOut.toByteArray());
54: PGPLiteralData data = (PGPLiteralData) fact.nextObject();
55: InputStream in = data.getInputStream();
56:
57: for (int count = 0; count != i; count++) {
58: if (in.read() != (buf[count] & 0xff)) {
59: fail("failed readback test - length = " + i);
60: }
61: }
62: }
63:
64: public void performTest() throws IOException {
65: PGPLiteralDataGenerator oldGenerator = new PGPLiteralDataGenerator(
66: true);
67:
68: readBackTest(oldGenerator);
69:
70: PGPLiteralDataGenerator newGenerator = new PGPLiteralDataGenerator(
71: false);
72:
73: readBackTest(newGenerator);
74: }
75:
76: public String getName() {
77: return "PGPPacketTest";
78: }
79:
80: public static void main(String[] args) {
81: Security.addProvider(new BouncyCastleProvider());
82:
83: runTest(new PGPPacketTest());
84: }
85: }
|