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

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: Fixed lint Created 9 years, 10 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
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 165 matching lines...) Expand 10 before | Expand all | Expand 10 after
230 231
231 char* gzip_encode_buffer_; 232 char* gzip_encode_buffer_;
232 int gzip_encode_len_; 233 int gzip_encode_len_;
233 }; 234 };
234 235
235 // Basic scenario: decoding deflate data with big enough buffer. 236 // Basic scenario: decoding deflate data with big enough buffer.
236 TEST_F(GZipUnitTest, DecodeDeflate) { 237 TEST_F(GZipUnitTest, DecodeDeflate) {
237 // Decode the compressed data with filter 238 // Decode the compressed data with filter
238 std::vector<Filter::FilterType> filter_types; 239 std::vector<Filter::FilterType> filter_types;
239 filter_types.push_back(Filter::FILTER_TYPE_DEFLATE); 240 filter_types.push_back(Filter::FILTER_TYPE_DEFLATE);
240 net::MockFilterContext filter_context(kDefaultBufferSize); 241 net::MockFilterContext filter_context;
241 scoped_ptr<Filter> filter(Filter::Factory(filter_types, filter_context)); 242 scoped_ptr<Filter> filter(Filter::Factory(filter_types, filter_context));
242 ASSERT_TRUE(filter.get()); 243 ASSERT_TRUE(filter.get());
243 memcpy(filter->stream_buffer()->data(), deflate_encode_buffer_, 244 memcpy(filter->stream_buffer()->data(), deflate_encode_buffer_,
244 deflate_encode_len_); 245 deflate_encode_len_);
245 filter->FlushStreamBuffer(deflate_encode_len_); 246 filter->FlushStreamBuffer(deflate_encode_len_);
246 247
247 char deflate_decode_buffer[kDefaultBufferSize]; 248 char deflate_decode_buffer[kDefaultBufferSize];
248 int deflate_decode_size = kDefaultBufferSize; 249 int deflate_decode_size = kDefaultBufferSize;
249 filter->ReadData(deflate_decode_buffer, &deflate_decode_size); 250 filter->ReadData(deflate_decode_buffer, &deflate_decode_size);
250 251
251 // Compare the decoding result with source data 252 // Compare the decoding result with source data
252 EXPECT_TRUE(deflate_decode_size == source_len()); 253 EXPECT_TRUE(deflate_decode_size == source_len());
253 EXPECT_EQ(memcmp(source_buffer(), deflate_decode_buffer, source_len()), 0); 254 EXPECT_EQ(memcmp(source_buffer(), deflate_decode_buffer, source_len()), 0);
254 } 255 }
255 256
256 // Basic scenario: decoding gzip data with big enough buffer. 257 // Basic scenario: decoding gzip data with big enough buffer.
257 TEST_F(GZipUnitTest, DecodeGZip) { 258 TEST_F(GZipUnitTest, DecodeGZip) {
258 // Decode the compressed data with filter 259 // Decode the compressed data with filter
259 std::vector<Filter::FilterType> filter_types; 260 std::vector<Filter::FilterType> filter_types;
260 filter_types.push_back(Filter::FILTER_TYPE_GZIP); 261 filter_types.push_back(Filter::FILTER_TYPE_GZIP);
261 net::MockFilterContext filter_context(kDefaultBufferSize); 262 net::MockFilterContext filter_context;
262 scoped_ptr<Filter> filter(Filter::Factory(filter_types, filter_context)); 263 scoped_ptr<Filter> filter(Filter::Factory(filter_types, filter_context));
263 ASSERT_TRUE(filter.get()); 264 ASSERT_TRUE(filter.get());
264 memcpy(filter->stream_buffer()->data(), gzip_encode_buffer_, 265 memcpy(filter->stream_buffer()->data(), gzip_encode_buffer_,
265 gzip_encode_len_); 266 gzip_encode_len_);
266 filter->FlushStreamBuffer(gzip_encode_len_); 267 filter->FlushStreamBuffer(gzip_encode_len_);
267 268
268 char gzip_decode_buffer[kDefaultBufferSize]; 269 char gzip_decode_buffer[kDefaultBufferSize];
269 int gzip_decode_size = kDefaultBufferSize; 270 int gzip_decode_size = kDefaultBufferSize;
270 filter->ReadData(gzip_decode_buffer, &gzip_decode_size); 271 filter->ReadData(gzip_decode_buffer, &gzip_decode_size);
271 272
272 // Compare the decoding result with source data 273 // Compare the decoding result with source data
273 EXPECT_TRUE(gzip_decode_size == source_len()); 274 EXPECT_TRUE(gzip_decode_size == source_len());
274 EXPECT_EQ(memcmp(source_buffer(), gzip_decode_buffer, source_len()), 0); 275 EXPECT_EQ(memcmp(source_buffer(), gzip_decode_buffer, source_len()), 0);
275 } 276 }
276 277
277 // Tests we can call filter repeatedly to get all the data decoded. 278 // 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 279 // To do that, we create a filter with a small buffer that can not hold all
279 // the input data. 280 // the input data.
280 TEST_F(GZipUnitTest, DecodeWithSmallBuffer) { 281 TEST_F(GZipUnitTest, DecodeWithSmallBuffer) {
281 std::vector<Filter::FilterType> filter_types; 282 std::vector<Filter::FilterType> filter_types;
282 filter_types.push_back(Filter::FILTER_TYPE_DEFLATE); 283 filter_types.push_back(Filter::FILTER_TYPE_DEFLATE);
283 net::MockFilterContext filter_context(kSmallBufferSize); 284 net::MockFilterContext filter_context;
284 scoped_ptr<Filter> filter(Filter::Factory(filter_types, filter_context)); 285 scoped_ptr<Filter> filter(Filter::Factory(filter_types, filter_context));
285 ASSERT_TRUE(filter.get()); 286 ASSERT_TRUE(filter.get());
287 filter->ResetInputBufferForTest(kSmallBufferSize);
286 DecodeAndCompareWithFilter(filter.get(), source_buffer(), source_len(), 288 DecodeAndCompareWithFilter(filter.get(), source_buffer(), source_len(),
287 deflate_encode_buffer_, deflate_encode_len_, 289 deflate_encode_buffer_, deflate_encode_len_,
288 kDefaultBufferSize); 290 kDefaultBufferSize);
289 } 291 }
290 292
291 // Tests we can still decode with just 1 byte buffer in the filter. 293 // 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 294 // 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 295 // header correctly. (2) Sometimes the filter will consume input without
294 // generating output. Verify filter can handle it correctly. 296 // generating output. Verify filter can handle it correctly.
295 TEST_F(GZipUnitTest, DecodeWithOneByteBuffer) { 297 TEST_F(GZipUnitTest, DecodeWithOneByteBuffer) {
296 std::vector<Filter::FilterType> filter_types; 298 std::vector<Filter::FilterType> filter_types;
297 filter_types.push_back(Filter::FILTER_TYPE_GZIP); 299 filter_types.push_back(Filter::FILTER_TYPE_GZIP);
298 net::MockFilterContext filter_context(1); 300 net::MockFilterContext filter_context;
299 scoped_ptr<Filter> filter(Filter::Factory(filter_types, filter_context)); 301 scoped_ptr<Filter> filter(Filter::Factory(filter_types, filter_context));
300 ASSERT_TRUE(filter.get()); 302 ASSERT_TRUE(filter.get());
303 filter->ResetInputBufferForTest(1);
301 DecodeAndCompareWithFilter(filter.get(), source_buffer(), source_len(), 304 DecodeAndCompareWithFilter(filter.get(), source_buffer(), source_len(),
302 gzip_encode_buffer_, gzip_encode_len_, 305 gzip_encode_buffer_, gzip_encode_len_,
303 kDefaultBufferSize); 306 kDefaultBufferSize);
304 } 307 }
305 308
306 // Tests we can decode when caller has small buffer to read out from filter. 309 // Tests we can decode when caller has small buffer to read out from filter.
307 TEST_F(GZipUnitTest, DecodeWithSmallOutputBuffer) { 310 TEST_F(GZipUnitTest, DecodeWithSmallOutputBuffer) {
308 std::vector<Filter::FilterType> filter_types; 311 std::vector<Filter::FilterType> filter_types;
309 filter_types.push_back(Filter::FILTER_TYPE_DEFLATE); 312 filter_types.push_back(Filter::FILTER_TYPE_DEFLATE);
310 net::MockFilterContext filter_context(kDefaultBufferSize); 313 net::MockFilterContext filter_context;
311 scoped_ptr<Filter> filter(Filter::Factory(filter_types, filter_context)); 314 scoped_ptr<Filter> filter(Filter::Factory(filter_types, filter_context));
312 ASSERT_TRUE(filter.get()); 315 ASSERT_TRUE(filter.get());
313 DecodeAndCompareWithFilter(filter.get(), source_buffer(), source_len(), 316 DecodeAndCompareWithFilter(filter.get(), source_buffer(), source_len(),
314 deflate_encode_buffer_, deflate_encode_len_, 317 deflate_encode_buffer_, deflate_encode_len_,
315 kSmallBufferSize); 318 kSmallBufferSize);
316 } 319 }
317 320
318 // Tests we can still decode with just 1 byte buffer in the filter and just 1 321 // Tests we can still decode with just 1 byte buffer in the filter and just 1
319 // byte buffer in the caller. 322 // byte buffer in the caller.
320 TEST_F(GZipUnitTest, DecodeWithOneByteInputAndOutputBuffer) { 323 TEST_F(GZipUnitTest, DecodeWithOneByteInputAndOutputBuffer) {
321 std::vector<Filter::FilterType> filter_types; 324 std::vector<Filter::FilterType> filter_types;
322 filter_types.push_back(Filter::FILTER_TYPE_GZIP); 325 filter_types.push_back(Filter::FILTER_TYPE_GZIP);
323 net::MockFilterContext filter_context(1); 326 net::MockFilterContext filter_context;
324 scoped_ptr<Filter> filter(Filter::Factory(filter_types, filter_context)); 327 scoped_ptr<Filter> filter(Filter::Factory(filter_types, filter_context));
325 ASSERT_TRUE(filter.get()); 328 ASSERT_TRUE(filter.get());
329 filter->ResetInputBufferForTest(1);
326 DecodeAndCompareWithFilter(filter.get(), source_buffer(), source_len(), 330 DecodeAndCompareWithFilter(filter.get(), source_buffer(), source_len(),
327 gzip_encode_buffer_, gzip_encode_len_, 1); 331 gzip_encode_buffer_, gzip_encode_len_, 1);
328 } 332 }
329 333
330 // Decoding deflate stream with corrupted data. 334 // Decoding deflate stream with corrupted data.
331 TEST_F(GZipUnitTest, DecodeCorruptedData) { 335 TEST_F(GZipUnitTest, DecodeCorruptedData) {
332 char corrupt_data[kDefaultBufferSize]; 336 char corrupt_data[kDefaultBufferSize];
333 int corrupt_data_len = deflate_encode_len_; 337 int corrupt_data_len = deflate_encode_len_;
334 memcpy(corrupt_data, deflate_encode_buffer_, deflate_encode_len_); 338 memcpy(corrupt_data, deflate_encode_buffer_, deflate_encode_len_);
335 339
336 int pos = corrupt_data_len / 2; 340 int pos = corrupt_data_len / 2;
337 corrupt_data[pos] = !corrupt_data[pos]; 341 corrupt_data[pos] = !corrupt_data[pos];
338 342
339 // Decode the corrupted data with filter 343 // Decode the corrupted data with filter
340 std::vector<Filter::FilterType> filter_types; 344 std::vector<Filter::FilterType> filter_types;
341 filter_types.push_back(Filter::FILTER_TYPE_DEFLATE); 345 filter_types.push_back(Filter::FILTER_TYPE_DEFLATE);
342 net::MockFilterContext filter_context(kDefaultBufferSize); 346 net::MockFilterContext filter_context;
343 scoped_ptr<Filter> filter(Filter::Factory(filter_types, filter_context)); 347 scoped_ptr<Filter> filter(Filter::Factory(filter_types, filter_context));
344 ASSERT_TRUE(filter.get()); 348 ASSERT_TRUE(filter.get());
345 char corrupt_decode_buffer[kDefaultBufferSize]; 349 char corrupt_decode_buffer[kDefaultBufferSize];
346 int corrupt_decode_size = kDefaultBufferSize; 350 int corrupt_decode_size = kDefaultBufferSize;
347 351
348 int code = DecodeAllWithFilter(filter.get(), corrupt_data, corrupt_data_len, 352 int code = DecodeAllWithFilter(filter.get(), corrupt_data, corrupt_data_len,
349 corrupt_decode_buffer, &corrupt_decode_size); 353 corrupt_decode_buffer, &corrupt_decode_size);
350 354
351 // Expect failures 355 // Expect failures
352 EXPECT_TRUE(code == Filter::FILTER_ERROR); 356 EXPECT_TRUE(code == Filter::FILTER_ERROR);
353 } 357 }
354 358
355 // Decoding deflate stream with missing data. 359 // Decoding deflate stream with missing data.
356 TEST_F(GZipUnitTest, DecodeMissingData) { 360 TEST_F(GZipUnitTest, DecodeMissingData) {
357 char corrupt_data[kDefaultBufferSize]; 361 char corrupt_data[kDefaultBufferSize];
358 int corrupt_data_len = deflate_encode_len_; 362 int corrupt_data_len = deflate_encode_len_;
359 memcpy(corrupt_data, deflate_encode_buffer_, deflate_encode_len_); 363 memcpy(corrupt_data, deflate_encode_buffer_, deflate_encode_len_);
360 364
361 int pos = corrupt_data_len / 2; 365 int pos = corrupt_data_len / 2;
362 int len = corrupt_data_len - pos - 1; 366 int len = corrupt_data_len - pos - 1;
363 memmove(&corrupt_data[pos], &corrupt_data[pos+1], len); 367 memmove(&corrupt_data[pos], &corrupt_data[pos+1], len);
364 --corrupt_data_len; 368 --corrupt_data_len;
365 369
366 // Decode the corrupted data with filter 370 // Decode the corrupted data with filter
367 std::vector<Filter::FilterType> filter_types; 371 std::vector<Filter::FilterType> filter_types;
368 filter_types.push_back(Filter::FILTER_TYPE_DEFLATE); 372 filter_types.push_back(Filter::FILTER_TYPE_DEFLATE);
369 net::MockFilterContext filter_context(kDefaultBufferSize); 373 net::MockFilterContext filter_context;
370 scoped_ptr<Filter> filter(Filter::Factory(filter_types, filter_context)); 374 scoped_ptr<Filter> filter(Filter::Factory(filter_types, filter_context));
371 ASSERT_TRUE(filter.get()); 375 ASSERT_TRUE(filter.get());
372 char corrupt_decode_buffer[kDefaultBufferSize]; 376 char corrupt_decode_buffer[kDefaultBufferSize];
373 int corrupt_decode_size = kDefaultBufferSize; 377 int corrupt_decode_size = kDefaultBufferSize;
374 378
375 int code = DecodeAllWithFilter(filter.get(), corrupt_data, corrupt_data_len, 379 int code = DecodeAllWithFilter(filter.get(), corrupt_data, corrupt_data_len,
376 corrupt_decode_buffer, &corrupt_decode_size); 380 corrupt_decode_buffer, &corrupt_decode_size);
377 381
378 // Expect failures 382 // Expect failures
379 EXPECT_EQ(Filter::FILTER_ERROR, code); 383 EXPECT_EQ(Filter::FILTER_ERROR, code);
380 } 384 }
381 385
382 // Decoding gzip stream with corrupted header. 386 // Decoding gzip stream with corrupted header.
383 TEST_F(GZipUnitTest, DecodeCorruptedHeader) { 387 TEST_F(GZipUnitTest, DecodeCorruptedHeader) {
384 char corrupt_data[kDefaultBufferSize]; 388 char corrupt_data[kDefaultBufferSize];
385 int corrupt_data_len = gzip_encode_len_; 389 int corrupt_data_len = gzip_encode_len_;
386 memcpy(corrupt_data, gzip_encode_buffer_, gzip_encode_len_); 390 memcpy(corrupt_data, gzip_encode_buffer_, gzip_encode_len_);
387 391
388 corrupt_data[2] = !corrupt_data[2]; 392 corrupt_data[2] = !corrupt_data[2];
389 393
390 // Decode the corrupted data with filter 394 // Decode the corrupted data with filter
391 std::vector<Filter::FilterType> filter_types; 395 std::vector<Filter::FilterType> filter_types;
392 filter_types.push_back(Filter::FILTER_TYPE_GZIP); 396 filter_types.push_back(Filter::FILTER_TYPE_GZIP);
393 net::MockFilterContext filter_context(kDefaultBufferSize); 397 net::MockFilterContext filter_context;
394 scoped_ptr<Filter> filter(Filter::Factory(filter_types, filter_context)); 398 scoped_ptr<Filter> filter(Filter::Factory(filter_types, filter_context));
395 ASSERT_TRUE(filter.get()); 399 ASSERT_TRUE(filter.get());
396 char corrupt_decode_buffer[kDefaultBufferSize]; 400 char corrupt_decode_buffer[kDefaultBufferSize];
397 int corrupt_decode_size = kDefaultBufferSize; 401 int corrupt_decode_size = kDefaultBufferSize;
398 402
399 int code = DecodeAllWithFilter(filter.get(), corrupt_data, corrupt_data_len, 403 int code = DecodeAllWithFilter(filter.get(), corrupt_data, corrupt_data_len,
400 corrupt_decode_buffer, &corrupt_decode_size); 404 corrupt_decode_buffer, &corrupt_decode_size);
401 405
402 // Expect failures 406 // Expect failures
403 EXPECT_TRUE(code == Filter::FILTER_ERROR); 407 EXPECT_TRUE(code == Filter::FILTER_ERROR);
404 } 408 }
405
406 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698