001: package org.bouncycastle.openpgp.test;
002:
003: import org.bouncycastle.jce.provider.BouncyCastleProvider;
004: import org.bouncycastle.openpgp.PGPCompressedData;
005: import org.bouncycastle.openpgp.PGPCompressedDataGenerator;
006: import org.bouncycastle.openpgp.PGPException;
007: import org.bouncycastle.openpgp.PGPObjectFactory;
008: import org.bouncycastle.util.test.SimpleTest;
009: import org.bouncycastle.util.test.UncloseableOutputStream;
010:
011: import java.io.ByteArrayOutputStream;
012: import java.io.IOException;
013: import java.io.InputStream;
014: import java.io.OutputStream;
015: import java.security.Security;
016:
017: public class PGPCompressionTest extends SimpleTest {
018: public void performTest() throws Exception {
019: testCompression(PGPCompressedData.UNCOMPRESSED);
020: testCompression(PGPCompressedData.ZIP);
021: testCompression(PGPCompressedData.ZLIB);
022: testCompression(PGPCompressedData.BZIP2);
023:
024: //
025: // new style - using stream close
026: //
027: ByteArrayOutputStream bOut = new ByteArrayOutputStream();
028: PGPCompressedDataGenerator cPacket = new PGPCompressedDataGenerator(
029: PGPCompressedData.ZIP);
030:
031: OutputStream out = cPacket.open(new UncloseableOutputStream(
032: bOut), new byte[4]);
033:
034: out.write("hello world! !dlrow olleh".getBytes());
035:
036: out.close();
037:
038: validateData(bOut.toByteArray());
039:
040: try {
041: out.close();
042: cPacket.close();
043: } catch (Exception e) {
044: fail("Redundant close() should be ignored");
045: }
046:
047: //
048: // new style - using generator close
049: //
050: bOut = new ByteArrayOutputStream();
051: cPacket = new PGPCompressedDataGenerator(PGPCompressedData.ZIP);
052:
053: out = cPacket.open(new UncloseableOutputStream(bOut),
054: new byte[4]);
055:
056: out.write("hello world! !dlrow olleh".getBytes());
057:
058: cPacket.close();
059:
060: validateData(bOut.toByteArray());
061:
062: try {
063: out.close();
064: cPacket.close();
065: } catch (Exception e) {
066: fail("Redundant close() should be ignored");
067: }
068: }
069:
070: private void validateData(byte[] data) throws IOException,
071: PGPException {
072: PGPObjectFactory pgpFact = new PGPObjectFactory(data);
073: PGPCompressedData c1 = (PGPCompressedData) pgpFact.nextObject();
074: InputStream pIn = c1.getDataStream();
075:
076: ByteArrayOutputStream bOut = new ByteArrayOutputStream();
077:
078: int ch;
079: while ((ch = pIn.read()) >= 0) {
080: bOut.write(ch);
081: }
082:
083: if (!areEqual(bOut.toByteArray(), "hello world! !dlrow olleh"
084: .getBytes())) {
085: fail("compression test failed");
086: }
087: }
088:
089: private void testCompression(int type) throws IOException,
090: PGPException {
091: ByteArrayOutputStream bOut = new ByteArrayOutputStream();
092: PGPCompressedDataGenerator cPacket = new PGPCompressedDataGenerator(
093: type);
094:
095: OutputStream out = cPacket.open(new UncloseableOutputStream(
096: bOut));
097:
098: out.write("hello world!".getBytes());
099:
100: out.close();
101:
102: PGPObjectFactory pgpFact = new PGPObjectFactory(bOut
103: .toByteArray());
104: PGPCompressedData c1 = (PGPCompressedData) pgpFact.nextObject();
105: InputStream pIn = c1.getDataStream();
106:
107: bOut.reset();
108:
109: int ch;
110: while ((ch = pIn.read()) >= 0) {
111: bOut.write(ch);
112: }
113:
114: if (!areEqual(bOut.toByteArray(), "hello world!".getBytes())) {
115: fail("compression test failed");
116: }
117: }
118:
119: public String getName() {
120: return "PGPCompressionTest";
121: }
122:
123: public static void main(String[] args) {
124: Security.addProvider(new BouncyCastleProvider());
125:
126: runTest(new PGPCompressionTest());
127: }
128: }
|