Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(74)

Side by Side Diff: net/base/gzip_filter_unittest.cc

Issue 6516025: Remove GetInputStreamBufferSize() method from FilterContext. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Add some sanity checks Created 9 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « net/base/gzip_filter.h ('k') | net/base/mock_filter_context.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <fstream> 5 #include <fstream>
6 #include <iostream> 6 #include <iostream>
7 7
8 #if defined(USE_SYSTEM_ZLIB) 8 #if defined(USE_SYSTEM_ZLIB)
9 #include <zlib.h> 9 #include <zlib.h>
10 #else 10 #else
11 #include "third_party/zlib/zlib.h" 11 #include "third_party/zlib/zlib.h"
12 #endif 12 #endif
13 13
14 #include "base/file_util.h" 14 #include "base/file_util.h"
15 #include "base/path_service.h" 15 #include "base/path_service.h"
16 #include "base/scoped_ptr.h" 16 #include "base/scoped_ptr.h"
17 #include "net/base/gzip_filter.h" 17 #include "net/base/gzip_filter.h"
18 #include "net/base/mock_filter_context.h" 18 #include "net/base/mock_filter_context.h"
19 #include "net/base/io_buffer.h" 19 #include "net/base/io_buffer.h"
20 #include "testing/gtest/include/gtest/gtest.h" 20 #include "testing/gtest/include/gtest/gtest.h"
21 #include "testing/platform_test.h" 21 #include "testing/platform_test.h"
22 22
23 namespace { 23 namespace {
24 24
25 const int kDefaultBufferSize = 4096; 25 const int kDefaultBufferSize = 4096;
26 const int kSmallBufferSize = 128; 26 const int kSmallBufferSize = 128;
27 const int kMaxBufferSize = 1048576; // 1048576 == 2^20 == 1 MB
28 27
29 const char kApplicationOctetStream[] = "application/octet-stream"; 28 const char kApplicationOctetStream[] = "application/octet-stream";
30 const char kApplicationXGzip[] = "application/x-gzip"; 29 const char kApplicationXGzip[] = "application/x-gzip";
31 const char kApplicationGzip[] = "application/gzip"; 30 const char kApplicationGzip[] = "application/gzip";
32 const char kApplicationXGunzip[] = "application/x-gunzip"; 31 const char kApplicationXGunzip[] = "application/x-gunzip";
33 32
34 // The GZIP header (see RFC 1952): 33 // The GZIP header (see RFC 1952):
35 // +---+---+---+---+---+---+---+---+---+---+ 34 // +---+---+---+---+---+---+---+---+---+---+
36 // |ID1|ID2|CM |FLG| MTIME |XFL|OS | 35 // |ID1|ID2|CM |FLG| MTIME |XFL|OS |
37 // +---+---+---+---+---+---+---+---+---+---+ 36 // +---+---+---+---+---+---+---+---+---+---+
38 // ID1 \037 37 // ID1 \037
39 // ID2 \213 38 // ID2 \213
40 // CM \010 (compression method == DEFLATE) 39 // CM \010 (compression method == DEFLATE)
41 // FLG \000 (special flags that we do not support) 40 // FLG \000 (special flags that we do not support)
42 // MTIME Unix format modification time (0 means not available) 41 // MTIME Unix format modification time (0 means not available)
43 // XFL 2-4? DEFLATE flags 42 // XFL 2-4? DEFLATE flags
44 // OS ???? Operating system indicator (255 means unknown) 43 // OS ???? Operating system indicator (255 means unknown)
45 // 44 //
46 // Header value we generate: 45 // Header value we generate:
47 const char kGZipHeader[] = { '\037', '\213', '\010', '\000', '\000', 46 const char kGZipHeader[] = { '\037', '\213', '\010', '\000', '\000',
48 '\000', '\000', '\000', '\002', '\377' }; 47 '\000', '\000', '\000', '\002', '\377' };
49 48
50 enum EncodeMode { 49 enum EncodeMode {
51 ENCODE_GZIP, // Wrap the deflate with a GZip header. 50 ENCODE_GZIP, // Wrap the deflate with a GZip header.
52 ENCODE_DEFLATE // Raw deflate. 51 ENCODE_DEFLATE // Raw deflate.
53 }; 52 };
54 53
54 } // namespace
55
55 // These tests use the path service, which uses autoreleased objects on the 56 // These tests use the path service, which uses autoreleased objects on the
56 // Mac, so this needs to be a PlatformTest. 57 // Mac, so this needs to be a PlatformTest.
57 class GZipUnitTest : public PlatformTest { 58 class GZipUnitTest : public PlatformTest {
58 protected: 59 protected:
59 virtual void SetUp() { 60 virtual void SetUp() {
60 PlatformTest::SetUp(); 61 PlatformTest::SetUp();
61 62
62 deflate_encode_buffer_ = NULL; 63 deflate_encode_buffer_ = NULL;
63 gzip_encode_buffer_ = NULL; 64 gzip_encode_buffer_ = NULL;
64 65
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after
213 // Dest is the buffer for decoding results. Upon entry, *dest_len is the size 214 // Dest is the buffer for decoding results. Upon entry, *dest_len is the size
214 // of the dest buffer. Upon exit, *dest_len is the number of chars written 215 // of the dest buffer. Upon exit, *dest_len is the number of chars written
215 // into the buffer. 216 // into the buffer.
216 int DecodeAllWithFilter(Filter* filter, const char* source, int source_len, 217 int DecodeAllWithFilter(Filter* filter, const char* source, int source_len,
217 char* dest, int* dest_len) { 218 char* dest, int* dest_len) {
218 memcpy(filter->stream_buffer()->data(), source, source_len); 219 memcpy(filter->stream_buffer()->data(), source, source_len);
219 filter->FlushStreamBuffer(source_len); 220 filter->FlushStreamBuffer(source_len);
220 return filter->ReadData(dest, dest_len); 221 return filter->ReadData(dest, dest_len);
221 } 222 }
222 223
224 void InitFilter(Filter::FilterType type) {
225 std::vector<Filter::FilterType> filter_types;
226 filter_types.push_back(type);
227 filter_.reset(Filter::Factory(filter_types, filter_context_));
228 ASSERT_TRUE(filter_.get());
229 ASSERT_GE(filter_->stream_buffer_size(), kDefaultBufferSize);
230 }
231
232 void InitFilterWithBufferSize(Filter::FilterType type, int buffer_size) {
233 std::vector<Filter::FilterType> filter_types;
234 filter_types.push_back(type);
235 filter_.reset(Filter::FactoryForTests(filter_types, filter_context_,
236 buffer_size));
237 ASSERT_TRUE(filter_.get());
238 }
239
223 const char* source_buffer() const { return source_buffer_.data(); } 240 const char* source_buffer() const { return source_buffer_.data(); }
224 int source_len() const { return static_cast<int>(source_buffer_.size()); } 241 int source_len() const { return static_cast<int>(source_buffer_.size()); }
225 242
243 scoped_ptr<Filter> filter_;
244
226 std::string source_buffer_; 245 std::string source_buffer_;
227 246
228 char* deflate_encode_buffer_; 247 char* deflate_encode_buffer_;
229 int deflate_encode_len_; 248 int deflate_encode_len_;
230 249
231 char* gzip_encode_buffer_; 250 char* gzip_encode_buffer_;
232 int gzip_encode_len_; 251 int gzip_encode_len_;
252
253 private:
254 net::MockFilterContext filter_context_;
233 }; 255 };
234 256
235 // Basic scenario: decoding deflate data with big enough buffer. 257 // Basic scenario: decoding deflate data with big enough buffer.
236 TEST_F(GZipUnitTest, DecodeDeflate) { 258 TEST_F(GZipUnitTest, DecodeDeflate) {
237 // Decode the compressed data with filter 259 // Decode the compressed data with filter
238 std::vector<Filter::FilterType> filter_types; 260 InitFilter(Filter::FILTER_TYPE_DEFLATE);
239 filter_types.push_back(Filter::FILTER_TYPE_DEFLATE); 261 memcpy(filter_->stream_buffer()->data(), deflate_encode_buffer_,
240 net::MockFilterContext filter_context(kDefaultBufferSize);
241 scoped_ptr<Filter> filter(Filter::Factory(filter_types, filter_context));
242 ASSERT_TRUE(filter.get());
243 memcpy(filter->stream_buffer()->data(), deflate_encode_buffer_,
244 deflate_encode_len_); 262 deflate_encode_len_);
245 filter->FlushStreamBuffer(deflate_encode_len_); 263 filter_->FlushStreamBuffer(deflate_encode_len_);
246 264
247 char deflate_decode_buffer[kDefaultBufferSize]; 265 char deflate_decode_buffer[kDefaultBufferSize];
248 int deflate_decode_size = kDefaultBufferSize; 266 int deflate_decode_size = kDefaultBufferSize;
249 filter->ReadData(deflate_decode_buffer, &deflate_decode_size); 267 filter_->ReadData(deflate_decode_buffer, &deflate_decode_size);
250 268
251 // Compare the decoding result with source data 269 // Compare the decoding result with source data
252 EXPECT_TRUE(deflate_decode_size == source_len()); 270 EXPECT_TRUE(deflate_decode_size == source_len());
253 EXPECT_EQ(memcmp(source_buffer(), deflate_decode_buffer, source_len()), 0); 271 EXPECT_EQ(memcmp(source_buffer(), deflate_decode_buffer, source_len()), 0);
254 } 272 }
255 273
256 // Basic scenario: decoding gzip data with big enough buffer. 274 // Basic scenario: decoding gzip data with big enough buffer.
257 TEST_F(GZipUnitTest, DecodeGZip) { 275 TEST_F(GZipUnitTest, DecodeGZip) {
258 // Decode the compressed data with filter 276 // Decode the compressed data with filter
259 std::vector<Filter::FilterType> filter_types; 277 InitFilter(Filter::FILTER_TYPE_GZIP);
260 filter_types.push_back(Filter::FILTER_TYPE_GZIP); 278 memcpy(filter_->stream_buffer()->data(), gzip_encode_buffer_,
261 net::MockFilterContext filter_context(kDefaultBufferSize);
262 scoped_ptr<Filter> filter(Filter::Factory(filter_types, filter_context));
263 ASSERT_TRUE(filter.get());
264 memcpy(filter->stream_buffer()->data(), gzip_encode_buffer_,
265 gzip_encode_len_); 279 gzip_encode_len_);
266 filter->FlushStreamBuffer(gzip_encode_len_); 280 filter_->FlushStreamBuffer(gzip_encode_len_);
267 281
268 char gzip_decode_buffer[kDefaultBufferSize]; 282 char gzip_decode_buffer[kDefaultBufferSize];
269 int gzip_decode_size = kDefaultBufferSize; 283 int gzip_decode_size = kDefaultBufferSize;
270 filter->ReadData(gzip_decode_buffer, &gzip_decode_size); 284 filter_->ReadData(gzip_decode_buffer, &gzip_decode_size);
271 285
272 // Compare the decoding result with source data 286 // Compare the decoding result with source data
273 EXPECT_TRUE(gzip_decode_size == source_len()); 287 EXPECT_TRUE(gzip_decode_size == source_len());
274 EXPECT_EQ(memcmp(source_buffer(), gzip_decode_buffer, source_len()), 0); 288 EXPECT_EQ(memcmp(source_buffer(), gzip_decode_buffer, source_len()), 0);
275 } 289 }
276 290
277 // Tests we can call filter repeatedly to get all the data decoded. 291 // Tests we can call filter repeatedly to get all the data decoded.
278 // To do that, we create a filter with a small buffer that can not hold all 292 // To do that, we create a filter with a small buffer that can not hold all
279 // the input data. 293 // the input data.
280 TEST_F(GZipUnitTest, DecodeWithSmallBuffer) { 294 TEST_F(GZipUnitTest, DecodeWithSmallBuffer) {
281 std::vector<Filter::FilterType> filter_types; 295 InitFilterWithBufferSize(Filter::FILTER_TYPE_DEFLATE, kSmallBufferSize);
282 filter_types.push_back(Filter::FILTER_TYPE_DEFLATE); 296 EXPECT_EQ(kSmallBufferSize, filter_->stream_buffer_size());
283 net::MockFilterContext filter_context(kSmallBufferSize); 297 DecodeAndCompareWithFilter(filter_.get(), source_buffer(), source_len(),
284 scoped_ptr<Filter> filter(Filter::Factory(filter_types, filter_context));
285 ASSERT_TRUE(filter.get());
286 DecodeAndCompareWithFilter(filter.get(), source_buffer(), source_len(),
287 deflate_encode_buffer_, deflate_encode_len_, 298 deflate_encode_buffer_, deflate_encode_len_,
288 kDefaultBufferSize); 299 kDefaultBufferSize);
289 } 300 }
290 301
291 // Tests we can still decode with just 1 byte buffer in the filter. 302 // Tests we can still decode with just 1 byte buffer in the filter.
292 // The purpose of this tests are two: (1) Verify filter can parse partial GZip 303 // The purpose of this tests are two: (1) Verify filter can parse partial GZip
293 // header correctly. (2) Sometimes the filter will consume input without 304 // header correctly. (2) Sometimes the filter will consume input without
294 // generating output. Verify filter can handle it correctly. 305 // generating output. Verify filter can handle it correctly.
295 TEST_F(GZipUnitTest, DecodeWithOneByteBuffer) { 306 TEST_F(GZipUnitTest, DecodeWithOneByteBuffer) {
296 std::vector<Filter::FilterType> filter_types; 307 InitFilterWithBufferSize(Filter::FILTER_TYPE_GZIP, 1);
297 filter_types.push_back(Filter::FILTER_TYPE_GZIP); 308 EXPECT_EQ(1, filter_->stream_buffer_size());
298 net::MockFilterContext filter_context(1); 309 DecodeAndCompareWithFilter(filter_.get(), source_buffer(), source_len(),
299 scoped_ptr<Filter> filter(Filter::Factory(filter_types, filter_context));
300 ASSERT_TRUE(filter.get());
301 DecodeAndCompareWithFilter(filter.get(), source_buffer(), source_len(),
302 gzip_encode_buffer_, gzip_encode_len_, 310 gzip_encode_buffer_, gzip_encode_len_,
303 kDefaultBufferSize); 311 kDefaultBufferSize);
304 } 312 }
305 313
306 // Tests we can decode when caller has small buffer to read out from filter. 314 // Tests we can decode when caller has small buffer to read out from filter.
307 TEST_F(GZipUnitTest, DecodeWithSmallOutputBuffer) { 315 TEST_F(GZipUnitTest, DecodeWithSmallOutputBuffer) {
308 std::vector<Filter::FilterType> filter_types; 316 InitFilter(Filter::FILTER_TYPE_DEFLATE);
309 filter_types.push_back(Filter::FILTER_TYPE_DEFLATE); 317 DecodeAndCompareWithFilter(filter_.get(), source_buffer(), source_len(),
310 net::MockFilterContext filter_context(kDefaultBufferSize);
311 scoped_ptr<Filter> filter(Filter::Factory(filter_types, filter_context));
312 ASSERT_TRUE(filter.get());
313 DecodeAndCompareWithFilter(filter.get(), source_buffer(), source_len(),
314 deflate_encode_buffer_, deflate_encode_len_, 318 deflate_encode_buffer_, deflate_encode_len_,
315 kSmallBufferSize); 319 kSmallBufferSize);
316 } 320 }
317 321
318 // Tests we can still decode with just 1 byte buffer in the filter and just 1 322 // Tests we can still decode with just 1 byte buffer in the filter and just 1
319 // byte buffer in the caller. 323 // byte buffer in the caller.
320 TEST_F(GZipUnitTest, DecodeWithOneByteInputAndOutputBuffer) { 324 TEST_F(GZipUnitTest, DecodeWithOneByteInputAndOutputBuffer) {
321 std::vector<Filter::FilterType> filter_types; 325 InitFilterWithBufferSize(Filter::FILTER_TYPE_GZIP, 1);
322 filter_types.push_back(Filter::FILTER_TYPE_GZIP); 326 EXPECT_EQ(1, filter_->stream_buffer_size());
323 net::MockFilterContext filter_context(1); 327 DecodeAndCompareWithFilter(filter_.get(), source_buffer(), source_len(),
324 scoped_ptr<Filter> filter(Filter::Factory(filter_types, filter_context));
325 ASSERT_TRUE(filter.get());
326 DecodeAndCompareWithFilter(filter.get(), source_buffer(), source_len(),
327 gzip_encode_buffer_, gzip_encode_len_, 1); 328 gzip_encode_buffer_, gzip_encode_len_, 1);
328 } 329 }
329 330
330 // Decoding deflate stream with corrupted data. 331 // Decoding deflate stream with corrupted data.
331 TEST_F(GZipUnitTest, DecodeCorruptedData) { 332 TEST_F(GZipUnitTest, DecodeCorruptedData) {
332 char corrupt_data[kDefaultBufferSize]; 333 char corrupt_data[kDefaultBufferSize];
333 int corrupt_data_len = deflate_encode_len_; 334 int corrupt_data_len = deflate_encode_len_;
334 memcpy(corrupt_data, deflate_encode_buffer_, deflate_encode_len_); 335 memcpy(corrupt_data, deflate_encode_buffer_, deflate_encode_len_);
335 336
336 int pos = corrupt_data_len / 2; 337 int pos = corrupt_data_len / 2;
337 corrupt_data[pos] = !corrupt_data[pos]; 338 corrupt_data[pos] = !corrupt_data[pos];
338 339
339 // Decode the corrupted data with filter 340 // Decode the corrupted data with filter
340 std::vector<Filter::FilterType> filter_types; 341 InitFilter(Filter::FILTER_TYPE_DEFLATE);
341 filter_types.push_back(Filter::FILTER_TYPE_DEFLATE);
342 net::MockFilterContext filter_context(kDefaultBufferSize);
343 scoped_ptr<Filter> filter(Filter::Factory(filter_types, filter_context));
344 ASSERT_TRUE(filter.get());
345 char corrupt_decode_buffer[kDefaultBufferSize]; 342 char corrupt_decode_buffer[kDefaultBufferSize];
346 int corrupt_decode_size = kDefaultBufferSize; 343 int corrupt_decode_size = kDefaultBufferSize;
347 344
348 int code = DecodeAllWithFilter(filter.get(), corrupt_data, corrupt_data_len, 345 int code = DecodeAllWithFilter(filter_.get(), corrupt_data, corrupt_data_len,
349 corrupt_decode_buffer, &corrupt_decode_size); 346 corrupt_decode_buffer, &corrupt_decode_size);
350 347
351 // Expect failures 348 // Expect failures
352 EXPECT_TRUE(code == Filter::FILTER_ERROR); 349 EXPECT_TRUE(code == Filter::FILTER_ERROR);
353 } 350 }
354 351
355 // Decoding deflate stream with missing data. 352 // Decoding deflate stream with missing data.
356 TEST_F(GZipUnitTest, DecodeMissingData) { 353 TEST_F(GZipUnitTest, DecodeMissingData) {
357 char corrupt_data[kDefaultBufferSize]; 354 char corrupt_data[kDefaultBufferSize];
358 int corrupt_data_len = deflate_encode_len_; 355 int corrupt_data_len = deflate_encode_len_;
359 memcpy(corrupt_data, deflate_encode_buffer_, deflate_encode_len_); 356 memcpy(corrupt_data, deflate_encode_buffer_, deflate_encode_len_);
360 357
361 int pos = corrupt_data_len / 2; 358 int pos = corrupt_data_len / 2;
362 int len = corrupt_data_len - pos - 1; 359 int len = corrupt_data_len - pos - 1;
363 memmove(&corrupt_data[pos], &corrupt_data[pos+1], len); 360 memmove(&corrupt_data[pos], &corrupt_data[pos+1], len);
364 --corrupt_data_len; 361 --corrupt_data_len;
365 362
366 // Decode the corrupted data with filter 363 // Decode the corrupted data with filter
367 std::vector<Filter::FilterType> filter_types; 364 InitFilter(Filter::FILTER_TYPE_DEFLATE);
368 filter_types.push_back(Filter::FILTER_TYPE_DEFLATE);
369 net::MockFilterContext filter_context(kDefaultBufferSize);
370 scoped_ptr<Filter> filter(Filter::Factory(filter_types, filter_context));
371 ASSERT_TRUE(filter.get());
372 char corrupt_decode_buffer[kDefaultBufferSize]; 365 char corrupt_decode_buffer[kDefaultBufferSize];
373 int corrupt_decode_size = kDefaultBufferSize; 366 int corrupt_decode_size = kDefaultBufferSize;
374 367
375 int code = DecodeAllWithFilter(filter.get(), corrupt_data, corrupt_data_len, 368 int code = DecodeAllWithFilter(filter_.get(), corrupt_data, corrupt_data_len,
376 corrupt_decode_buffer, &corrupt_decode_size); 369 corrupt_decode_buffer, &corrupt_decode_size);
377 370
378 // Expect failures 371 // Expect failures
379 EXPECT_EQ(Filter::FILTER_ERROR, code); 372 EXPECT_EQ(Filter::FILTER_ERROR, code);
380 } 373 }
381 374
382 // Decoding gzip stream with corrupted header. 375 // Decoding gzip stream with corrupted header.
383 TEST_F(GZipUnitTest, DecodeCorruptedHeader) { 376 TEST_F(GZipUnitTest, DecodeCorruptedHeader) {
384 char corrupt_data[kDefaultBufferSize]; 377 char corrupt_data[kDefaultBufferSize];
385 int corrupt_data_len = gzip_encode_len_; 378 int corrupt_data_len = gzip_encode_len_;
386 memcpy(corrupt_data, gzip_encode_buffer_, gzip_encode_len_); 379 memcpy(corrupt_data, gzip_encode_buffer_, gzip_encode_len_);
387 380
388 corrupt_data[2] = !corrupt_data[2]; 381 corrupt_data[2] = !corrupt_data[2];
389 382
390 // Decode the corrupted data with filter 383 // Decode the corrupted data with filter
391 std::vector<Filter::FilterType> filter_types; 384 InitFilter(Filter::FILTER_TYPE_GZIP);
392 filter_types.push_back(Filter::FILTER_TYPE_GZIP);
393 net::MockFilterContext filter_context(kDefaultBufferSize);
394 scoped_ptr<Filter> filter(Filter::Factory(filter_types, filter_context));
395 ASSERT_TRUE(filter.get());
396 char corrupt_decode_buffer[kDefaultBufferSize]; 385 char corrupt_decode_buffer[kDefaultBufferSize];
397 int corrupt_decode_size = kDefaultBufferSize; 386 int corrupt_decode_size = kDefaultBufferSize;
398 387
399 int code = DecodeAllWithFilter(filter.get(), corrupt_data, corrupt_data_len, 388 int code = DecodeAllWithFilter(filter_.get(), corrupt_data, corrupt_data_len,
400 corrupt_decode_buffer, &corrupt_decode_size); 389 corrupt_decode_buffer, &corrupt_decode_size);
401 390
402 // Expect failures 391 // Expect failures
403 EXPECT_TRUE(code == Filter::FILTER_ERROR); 392 EXPECT_TRUE(code == Filter::FILTER_ERROR);
404 } 393 }
405
406 } // namespace
OLDNEW
« no previous file with comments | « net/base/gzip_filter.h ('k') | net/base/mock_filter_context.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698