[Notification] fix a bug that "update" overrides "add" change
[chromium-blink-merge.git] / net / filter / gzip_filter_unittest.cc
blob2f11e28a9fbbfa24e8ae215137984fca0a94fcfe
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include <fstream>
6 #include <ostream>
8 #include "base/files/file_util.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/path_service.h"
11 #include "net/base/io_buffer.h"
12 #include "net/filter/gzip_filter.h"
13 #include "net/filter/mock_filter_context.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "testing/platform_test.h"
16 #include "third_party/zlib/zlib.h"
18 namespace {
20 const int kDefaultBufferSize = 4096;
21 const int kSmallBufferSize = 128;
23 // The GZIP header (see RFC 1952):
24 // +---+---+---+---+---+---+---+---+---+---+
25 // |ID1|ID2|CM |FLG| MTIME |XFL|OS |
26 // +---+---+---+---+---+---+---+---+---+---+
27 // ID1 \037
28 // ID2 \213
29 // CM \010 (compression method == DEFLATE)
30 // FLG \000 (special flags that we do not support)
31 // MTIME Unix format modification time (0 means not available)
32 // XFL 2-4? DEFLATE flags
33 // OS ???? Operating system indicator (255 means unknown)
35 // Header value we generate:
36 const char kGZipHeader[] = { '\037', '\213', '\010', '\000', '\000',
37 '\000', '\000', '\000', '\002', '\377' };
39 enum EncodeMode {
40 ENCODE_GZIP, // Wrap the deflate with a GZip header.
41 ENCODE_DEFLATE // Raw deflate.
44 } // namespace
46 namespace net {
48 // These tests use the path service, which uses autoreleased objects on the
49 // Mac, so this needs to be a PlatformTest.
50 class GZipUnitTest : public PlatformTest {
51 protected:
52 void SetUp() override {
53 PlatformTest::SetUp();
55 deflate_encode_buffer_ = NULL;
56 gzip_encode_buffer_ = NULL;
58 // Get the path of source data file.
59 base::FilePath file_path;
60 PathService::Get(base::DIR_SOURCE_ROOT, &file_path);
61 file_path = file_path.AppendASCII("net");
62 file_path = file_path.AppendASCII("data");
63 file_path = file_path.AppendASCII("filter_unittests");
64 file_path = file_path.AppendASCII("google.txt");
66 // Read data from the file into buffer.
67 ASSERT_TRUE(base::ReadFileToString(file_path, &source_buffer_));
69 // Encode the data with deflate
70 deflate_encode_buffer_ = new char[kDefaultBufferSize];
71 ASSERT_TRUE(deflate_encode_buffer_ != NULL);
73 deflate_encode_len_ = kDefaultBufferSize;
74 int code = CompressAll(ENCODE_DEFLATE , source_buffer(), source_len(),
75 deflate_encode_buffer_, &deflate_encode_len_);
76 ASSERT_TRUE(code == Z_STREAM_END);
77 ASSERT_GT(deflate_encode_len_, 0);
78 ASSERT_TRUE(deflate_encode_len_ <= kDefaultBufferSize);
80 // Encode the data with gzip
81 gzip_encode_buffer_ = new char[kDefaultBufferSize];
82 ASSERT_TRUE(gzip_encode_buffer_ != NULL);
84 gzip_encode_len_ = kDefaultBufferSize;
85 code = CompressAll(ENCODE_GZIP, source_buffer(), source_len(),
86 gzip_encode_buffer_, &gzip_encode_len_);
87 ASSERT_TRUE(code == Z_STREAM_END);
88 ASSERT_GT(gzip_encode_len_, 0);
89 ASSERT_TRUE(gzip_encode_len_ <= kDefaultBufferSize);
92 void TearDown() override {
93 delete[] deflate_encode_buffer_;
94 deflate_encode_buffer_ = NULL;
96 delete[] gzip_encode_buffer_;
97 gzip_encode_buffer_ = NULL;
99 PlatformTest::TearDown();
102 // Compress the data in source with deflate encoding and write output to the
103 // buffer provided by dest. The function returns Z_OK if success, and returns
104 // other zlib error code if fail.
105 // The parameter mode specifies the encoding mechanism.
106 // The dest buffer should be large enough to hold all the output data.
107 int CompressAll(EncodeMode mode, const char* source, int source_size,
108 char* dest, int* dest_len) {
109 z_stream zlib_stream;
110 memset(&zlib_stream, 0, sizeof(zlib_stream));
111 int code;
113 // Initialize zlib
114 if (mode == ENCODE_GZIP) {
115 code = deflateInit2(&zlib_stream, Z_DEFAULT_COMPRESSION, Z_DEFLATED,
116 -MAX_WBITS,
117 8, // DEF_MEM_LEVEL
118 Z_DEFAULT_STRATEGY);
119 } else {
120 code = deflateInit(&zlib_stream, Z_DEFAULT_COMPRESSION);
123 if (code != Z_OK)
124 return code;
126 // Fill in zlib control block
127 zlib_stream.next_in = bit_cast<Bytef*>(source);
128 zlib_stream.avail_in = source_size;
129 zlib_stream.next_out = bit_cast<Bytef*>(dest);
130 zlib_stream.avail_out = *dest_len;
132 // Write header if needed
133 if (mode == ENCODE_GZIP) {
134 if (zlib_stream.avail_out < sizeof(kGZipHeader))
135 return Z_BUF_ERROR;
136 memcpy(zlib_stream.next_out, kGZipHeader, sizeof(kGZipHeader));
137 zlib_stream.next_out += sizeof(kGZipHeader);
138 zlib_stream.avail_out -= sizeof(kGZipHeader);
141 // Do deflate
142 code = deflate(&zlib_stream, Z_FINISH);
143 *dest_len = *dest_len - zlib_stream.avail_out;
145 deflateEnd(&zlib_stream);
146 return code;
149 // Use filter to decode compressed data, and compare the decoding result with
150 // the orginal Data.
151 // Parameters: Source and source_len are original data and its size.
152 // Encoded_source and encoded_source_len are compressed data and its size.
153 // Output_buffer_size specifies the size of buffer to read out data from
154 // filter.
155 void DecodeAndCompareWithFilter(Filter* filter,
156 const char* source,
157 int source_len,
158 const char* encoded_source,
159 int encoded_source_len,
160 int output_buffer_size) {
161 // Make sure we have enough space to hold the decoding output.
162 ASSERT_TRUE(source_len <= kDefaultBufferSize);
163 ASSERT_TRUE(output_buffer_size <= kDefaultBufferSize);
165 char decode_buffer[kDefaultBufferSize];
166 char* decode_next = decode_buffer;
167 int decode_avail_size = kDefaultBufferSize;
169 const char* encode_next = encoded_source;
170 int encode_avail_size = encoded_source_len;
172 int code = Filter::FILTER_OK;
173 while (code != Filter::FILTER_DONE) {
174 int encode_data_len;
175 encode_data_len = std::min(encode_avail_size,
176 filter->stream_buffer_size());
177 memcpy(filter->stream_buffer()->data(), encode_next, encode_data_len);
178 filter->FlushStreamBuffer(encode_data_len);
179 encode_next += encode_data_len;
180 encode_avail_size -= encode_data_len;
182 while (1) {
183 int decode_data_len = std::min(decode_avail_size, output_buffer_size);
185 code = filter->ReadData(decode_next, &decode_data_len);
186 decode_next += decode_data_len;
187 decode_avail_size -= decode_data_len;
189 ASSERT_TRUE(code != Filter::FILTER_ERROR);
191 if (code == Filter::FILTER_NEED_MORE_DATA ||
192 code == Filter::FILTER_DONE) {
193 break;
198 // Compare the decoding result with source data
199 int decode_total_data_len = kDefaultBufferSize - decode_avail_size;
200 EXPECT_TRUE(decode_total_data_len == source_len);
201 EXPECT_EQ(memcmp(source, decode_buffer, source_len), 0);
204 // Unsafe function to use filter to decode compressed data.
205 // Parameters: Source and source_len are compressed data and its size.
206 // Dest is the buffer for decoding results. Upon entry, *dest_len is the size
207 // of the dest buffer. Upon exit, *dest_len is the number of chars written
208 // into the buffer.
209 int DecodeAllWithFilter(Filter* filter, const char* source, int source_len,
210 char* dest, int* dest_len) {
211 memcpy(filter->stream_buffer()->data(), source, source_len);
212 filter->FlushStreamBuffer(source_len);
213 return filter->ReadData(dest, dest_len);
216 void InitFilter(Filter::FilterType type) {
217 std::vector<Filter::FilterType> filter_types;
218 filter_types.push_back(type);
219 filter_.reset(Filter::Factory(filter_types, filter_context_));
220 ASSERT_TRUE(filter_.get());
221 ASSERT_GE(filter_->stream_buffer_size(), kDefaultBufferSize);
224 void InitFilterWithBufferSize(Filter::FilterType type, int buffer_size) {
225 std::vector<Filter::FilterType> filter_types;
226 filter_types.push_back(type);
227 filter_.reset(Filter::FactoryForTests(filter_types, filter_context_,
228 buffer_size));
229 ASSERT_TRUE(filter_.get());
232 const char* source_buffer() const { return source_buffer_.data(); }
233 int source_len() const { return static_cast<int>(source_buffer_.size()); }
235 scoped_ptr<Filter> filter_;
237 std::string source_buffer_;
239 char* deflate_encode_buffer_;
240 int deflate_encode_len_;
242 char* gzip_encode_buffer_;
243 int gzip_encode_len_;
245 private:
246 MockFilterContext filter_context_;
249 // Basic scenario: decoding deflate data with big enough buffer.
250 TEST_F(GZipUnitTest, DecodeDeflate) {
251 // Decode the compressed data with filter
252 InitFilter(Filter::FILTER_TYPE_DEFLATE);
253 memcpy(filter_->stream_buffer()->data(), deflate_encode_buffer_,
254 deflate_encode_len_);
255 filter_->FlushStreamBuffer(deflate_encode_len_);
257 char deflate_decode_buffer[kDefaultBufferSize];
258 int deflate_decode_size = kDefaultBufferSize;
259 filter_->ReadData(deflate_decode_buffer, &deflate_decode_size);
261 // Compare the decoding result with source data
262 EXPECT_TRUE(deflate_decode_size == source_len());
263 EXPECT_EQ(memcmp(source_buffer(), deflate_decode_buffer, source_len()), 0);
266 // Basic scenario: decoding gzip data with big enough buffer.
267 TEST_F(GZipUnitTest, DecodeGZip) {
268 // Decode the compressed data with filter
269 InitFilter(Filter::FILTER_TYPE_GZIP);
270 memcpy(filter_->stream_buffer()->data(), gzip_encode_buffer_,
271 gzip_encode_len_);
272 filter_->FlushStreamBuffer(gzip_encode_len_);
274 char gzip_decode_buffer[kDefaultBufferSize];
275 int gzip_decode_size = kDefaultBufferSize;
276 filter_->ReadData(gzip_decode_buffer, &gzip_decode_size);
278 // Compare the decoding result with source data
279 EXPECT_TRUE(gzip_decode_size == source_len());
280 EXPECT_EQ(memcmp(source_buffer(), gzip_decode_buffer, source_len()), 0);
283 // Tests we can call filter repeatedly to get all the data decoded.
284 // To do that, we create a filter with a small buffer that can not hold all
285 // the input data.
286 TEST_F(GZipUnitTest, DecodeWithSmallBuffer) {
287 InitFilterWithBufferSize(Filter::FILTER_TYPE_DEFLATE, kSmallBufferSize);
288 EXPECT_EQ(kSmallBufferSize, filter_->stream_buffer_size());
289 DecodeAndCompareWithFilter(filter_.get(), source_buffer(), source_len(),
290 deflate_encode_buffer_, deflate_encode_len_,
291 kDefaultBufferSize);
294 // Tests we can still decode with just 1 byte buffer in the filter.
295 // The purpose of this tests are two: (1) Verify filter can parse partial GZip
296 // header correctly. (2) Sometimes the filter will consume input without
297 // generating output. Verify filter can handle it correctly.
298 TEST_F(GZipUnitTest, DecodeWithOneByteBuffer) {
299 InitFilterWithBufferSize(Filter::FILTER_TYPE_GZIP, 1);
300 EXPECT_EQ(1, filter_->stream_buffer_size());
301 DecodeAndCompareWithFilter(filter_.get(), source_buffer(), source_len(),
302 gzip_encode_buffer_, gzip_encode_len_,
303 kDefaultBufferSize);
306 // Tests we can decode when caller has small buffer to read out from filter.
307 TEST_F(GZipUnitTest, DecodeWithSmallOutputBuffer) {
308 InitFilter(Filter::FILTER_TYPE_DEFLATE);
309 DecodeAndCompareWithFilter(filter_.get(), source_buffer(), source_len(),
310 deflate_encode_buffer_, deflate_encode_len_,
311 kSmallBufferSize);
314 // Tests we can still decode with just 1 byte buffer in the filter and just 1
315 // byte buffer in the caller.
316 TEST_F(GZipUnitTest, DecodeWithOneByteInputAndOutputBuffer) {
317 InitFilterWithBufferSize(Filter::FILTER_TYPE_GZIP, 1);
318 EXPECT_EQ(1, filter_->stream_buffer_size());
319 DecodeAndCompareWithFilter(filter_.get(), source_buffer(), source_len(),
320 gzip_encode_buffer_, gzip_encode_len_, 1);
323 // Decoding deflate stream with corrupted data.
324 TEST_F(GZipUnitTest, DecodeCorruptedData) {
325 char corrupt_data[kDefaultBufferSize];
326 int corrupt_data_len = deflate_encode_len_;
327 memcpy(corrupt_data, deflate_encode_buffer_, deflate_encode_len_);
329 int pos = corrupt_data_len / 2;
330 corrupt_data[pos] = !corrupt_data[pos];
332 // Decode the corrupted data with filter
333 InitFilter(Filter::FILTER_TYPE_DEFLATE);
334 char corrupt_decode_buffer[kDefaultBufferSize];
335 int corrupt_decode_size = kDefaultBufferSize;
337 int code = DecodeAllWithFilter(filter_.get(), corrupt_data, corrupt_data_len,
338 corrupt_decode_buffer, &corrupt_decode_size);
340 // Expect failures
341 EXPECT_TRUE(code == Filter::FILTER_ERROR);
344 // Decoding deflate stream with missing data.
345 TEST_F(GZipUnitTest, DecodeMissingData) {
346 char corrupt_data[kDefaultBufferSize];
347 int corrupt_data_len = deflate_encode_len_;
348 memcpy(corrupt_data, deflate_encode_buffer_, deflate_encode_len_);
350 int pos = corrupt_data_len / 2;
351 int len = corrupt_data_len - pos - 1;
352 memmove(&corrupt_data[pos], &corrupt_data[pos+1], len);
353 --corrupt_data_len;
355 // Decode the corrupted data with filter
356 InitFilter(Filter::FILTER_TYPE_DEFLATE);
357 char corrupt_decode_buffer[kDefaultBufferSize];
358 int corrupt_decode_size = kDefaultBufferSize;
360 int code = DecodeAllWithFilter(filter_.get(), corrupt_data, corrupt_data_len,
361 corrupt_decode_buffer, &corrupt_decode_size);
363 // Expect failures
364 EXPECT_EQ(Filter::FILTER_ERROR, code);
367 // Decoding gzip stream with corrupted header.
368 TEST_F(GZipUnitTest, DecodeCorruptedHeader) {
369 char corrupt_data[kDefaultBufferSize];
370 int corrupt_data_len = gzip_encode_len_;
371 memcpy(corrupt_data, gzip_encode_buffer_, gzip_encode_len_);
373 corrupt_data[2] = !corrupt_data[2];
375 // Decode the corrupted data with filter
376 InitFilter(Filter::FILTER_TYPE_GZIP);
377 char corrupt_decode_buffer[kDefaultBufferSize];
378 int corrupt_decode_size = kDefaultBufferSize;
380 int code = DecodeAllWithFilter(filter_.get(), corrupt_data, corrupt_data_len,
381 corrupt_decode_buffer, &corrupt_decode_size);
383 // Expect failures
384 EXPECT_TRUE(code == Filter::FILTER_ERROR);
387 } // namespace net