1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
using System;
using System.IO;
using System.Text;
using NUnit.Framework;
using Org.BouncyCastle.Security;
using Org.BouncyCastle.Utilities.IO;
using Org.BouncyCastle.Utilities.Test;
namespace Org.BouncyCastle.Bcpg.OpenPgp.Tests
{
[TestFixture]
public class PgpCompressionTest
: SimpleTest
{
private static readonly SecureRandom Random = new SecureRandom();
private static readonly byte[] Data1 = new byte[0];
private static readonly byte[] Data2 = Encoding.ASCII.GetBytes("hello world! !dlrow olleh");
[Test]
public void TestBZip2()
{
DoTestCompression(Data1, CompressionAlgorithmTag.BZip2);
DoTestCompression(Data2, CompressionAlgorithmTag.BZip2);
DoTestCompression(RandomData(1000000), CompressionAlgorithmTag.BZip2);
}
[Test]
public void TestUncompressed()
{
DoTestCompression(Data1, CompressionAlgorithmTag.Uncompressed);
DoTestCompression(Data2, CompressionAlgorithmTag.Uncompressed);
DoTestCompression(RandomData(1000000), CompressionAlgorithmTag.Uncompressed);
}
[Test]
public void TestZip()
{
DoTestCompression(Data1, CompressionAlgorithmTag.Zip);
DoTestCompression(Data2, CompressionAlgorithmTag.Zip);
DoTestCompression(RandomData(1000000), CompressionAlgorithmTag.Zip);
}
[Test]
public void TestZLib()
{
DoTestCompression(Data1, CompressionAlgorithmTag.ZLib);
DoTestCompression(Data2, CompressionAlgorithmTag.ZLib);
DoTestCompression(RandomData(1000000), CompressionAlgorithmTag.ZLib);
}
public override void PerformTest()
{
TestBZip2();
TestUncompressed();
TestZip();
TestZLib();
}
private void DoTestCompression(byte[] data, CompressionAlgorithmTag type)
{
DoTestCompression(data, type, true);
DoTestCompression(data, type, false);
}
private void DoTestCompression(byte[] data, CompressionAlgorithmTag type, bool streamClose)
{
MemoryStream bOut = new MemoryStream();
PgpCompressedDataGenerator cPacket = new PgpCompressedDataGenerator(type);
Stream os = cPacket.Open(new UncloseableStream(bOut));
os.Write(data, 0, data.Length);
if (streamClose)
{
os.Dispose();
}
else
{
cPacket.Dispose();
}
ValidateData(data, bOut.ToArray());
try
{
os.Dispose();
cPacket.Dispose();
}
catch (Exception)
{
Fail("Redundant Close() should be ignored");
}
}
private byte[] RandomData(int length)
{
return SecureRandom.GetNextBytes(Random, length);
}
private void ValidateData(byte[] data, byte[] compressed)
{
PgpObjectFactory pgpFact = new PgpObjectFactory(compressed);
PgpCompressedData c1 = (PgpCompressedData) pgpFact.NextPgpObject();
Stream pIn = c1.GetDataStream();
byte[] bytes = Streams.ReadAll(pIn);
pIn.Close();
if (!AreEqual(bytes, data))
{
Fail("compression test failed");
}
}
public override string Name
{
get { return "PgpCompressionTest"; }
}
}
}
|