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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
|
#include "zlib.h"
#include <library/cpp/testing/unittest/registar.h>
#include "file.h"
#include <util/system/tempfile.h>
#include <util/random/entropy.h>
#define ZDATA "./zdata"
class TThrowingStream: public IOutputStream {
public:
TThrowingStream(int limit)
: Limit_(limit)
{
}
void DoWrite(const void*, size_t size) override {
if (Ignore) {
return;
}
Limit_ -= size;
if (Limit_ < 0) {
throw yexception() << "catch this";
}
}
void DoFinish() override {
if (Ignore) {
return;
}
if (Limit_ < 0) {
throw yexception() << "catch this";
}
}
void DoFlush() override {
if (Ignore) {
return;
}
if (Limit_ < 0) {
throw yexception() << "catch this";
}
}
bool Ignore = false;
private:
int Limit_;
};
Y_UNIT_TEST_SUITE(TZLibTest) {
static const TString DATA = "8s7d5vc6s5vc67sa4c65ascx6asd4xcv76adsfxv76s";
static const TString DATA2 = "cn8wk2bd9vb3vdfif83g1ks94bfiovtwv";
Y_UNIT_TEST(Compress) {
TUnbufferedFileOutput o(ZDATA);
TZLibCompress c(&o, ZLib::ZLib);
c.Write(DATA.data(), DATA.size());
c.Finish();
o.Finish();
}
Y_UNIT_TEST(Decompress) {
TTempFile tmpFile(ZDATA);
{
TUnbufferedFileInput i(ZDATA);
TZLibDecompress d(&i);
UNIT_ASSERT_EQUAL(d.ReadAll(), DATA);
}
}
Y_UNIT_TEST(Dictionary) {
static constexpr TStringBuf data = "<html><body></body></html>";
static constexpr TStringBuf dict = "</<html><body>";
for (auto type : {ZLib::Raw, ZLib::ZLib}) {
TStringStream compressed;
{
TZLibCompress compressor(TZLibCompress::TParams(&compressed).SetDict(dict).SetType(type));
compressor.Write(data);
}
TZLibDecompress decompressor(&compressed, type, ZLib::ZLIB_BUF_LEN, dict);
UNIT_ASSERT_STRINGS_EQUAL(decompressor.ReadAll(), data);
}
}
Y_UNIT_TEST(DecompressTwoStreams) {
// Check that Decompress(Compress(X) + Compress(Y)) == X + Y
TTempFile tmpFile(ZDATA);
{
TUnbufferedFileOutput o(ZDATA);
TZLibCompress c1(&o, ZLib::ZLib);
c1.Write(DATA.data(), DATA.size());
c1.Finish();
TZLibCompress c2(&o, ZLib::ZLib);
c2.Write(DATA2.data(), DATA2.size());
c2.Finish();
o.Finish();
}
{
TUnbufferedFileInput i(ZDATA);
TZLibDecompress d(&i);
UNIT_ASSERT_EQUAL(d.ReadAll(), DATA + DATA2);
}
}
Y_UNIT_TEST(CompressionExceptionSegfault) {
TVector<char> buf(512 * 1024);
EntropyPool().Load(buf.data(), buf.size());
TThrowingStream o(128 * 1024);
TZLibCompress c(&o, ZLib::GZip, 4, 1 << 15);
try {
c.Write(buf.data(), buf.size());
} catch (...) {
}
o.Ignore = true;
TVector<char>().swap(buf);
}
Y_UNIT_TEST(DecompressFirstOfTwoStreams) {
// Check that Decompress(Compress(X) + Compress(Y)) == X when single stream is allowed
TTempFile tmpFile(ZDATA);
{
TUnbufferedFileOutput o(ZDATA);
TZLibCompress c1(&o, ZLib::ZLib);
c1.Write(DATA.data(), DATA.size());
c1.Finish();
TZLibCompress c2(&o, ZLib::ZLib);
c2.Write(DATA2.data(), DATA2.size());
c2.Finish();
o.Finish();
}
{
TUnbufferedFileInput i(ZDATA);
TZLibDecompress d(&i);
d.SetAllowMultipleStreams(false);
UNIT_ASSERT_EQUAL(d.ReadAll(), DATA);
}
}
Y_UNIT_TEST(CompressFlush) {
TString data = "";
for (size_t i = 0; i < 32; ++i) {
TTempFile tmpFile(ZDATA);
TUnbufferedFileOutput output(ZDATA);
TZLibCompress compressor(&output, ZLib::ZLib);
compressor.Write(data.data(), data.size());
compressor.Flush();
{
TUnbufferedFileInput input(ZDATA);
TZLibDecompress decompressor(&input);
UNIT_ASSERT_EQUAL(decompressor.ReadAll(), data);
}
data += 'A' + i;
}
}
Y_UNIT_TEST(CompressEmptyFlush) {
TTempFile tmpFile(ZDATA);
TUnbufferedFileOutput output(ZDATA);
TZLibCompress compressor(&output, ZLib::ZLib);
TUnbufferedFileInput input(ZDATA);
compressor.Write(DATA.data(), DATA.size());
compressor.Flush();
{
TZLibDecompress decompressor(&input);
UNIT_ASSERT_EQUAL(decompressor.ReadAll(), DATA);
}
for (size_t i = 0; i < 10; ++i) {
compressor.Flush();
}
UNIT_ASSERT_EQUAL(input.ReadAll(), "");
}
Y_UNIT_TEST(CompressFlushSmallBuffer) {
for (size_t bufferSize = 16; bufferSize < 32; ++bufferSize) {
TString firstData = "";
for (size_t firstDataSize = 0; firstDataSize < 16; ++firstDataSize) {
TString secondData = "";
for (size_t secondDataSize = 0; secondDataSize < 16; ++secondDataSize) {
TTempFile tmpFile(ZDATA);
TUnbufferedFileOutput output(ZDATA);
TZLibCompress compressor(TZLibCompress::TParams(&output).SetType(ZLib::ZLib).SetBufLen(bufferSize));
TUnbufferedFileInput input(ZDATA);
TZLibDecompress decompressor(&input);
compressor.Write(firstData.data(), firstData.size());
compressor.Flush();
UNIT_ASSERT_EQUAL(decompressor.ReadAll(), firstData);
compressor.Write(secondData.data(), secondData.size());
compressor.Flush();
UNIT_ASSERT_EQUAL(decompressor.ReadAll(), secondData);
secondData += 'A' + secondDataSize;
}
firstData += 'A' + firstDataSize;
}
}
}
} // Y_UNIT_TEST_SUITE(TZLibTest)
|