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

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: New approach: FactoryForTests 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
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 }
230
231 void InitFilterWithBufferSize(Filter::FilterType type, int buffer_size) {
232 std::vector<Filter::FilterType> filter_types;
233 filter_types.push_back(type);
234 filter_.reset(Filter::FactoryForTests(filter_types, filter_context_,
235 buffer_size));
236 ASSERT_TRUE(filter_.get());
237 }
238
223 const char* source_buffer() const { return source_buffer_.data(); } 239 const char* source_buffer() const { return source_buffer_.data(); }
224 int source_len() const { return static_cast<int>(source_buffer_.size()); } 240 int source_len() const { return static_cast<int>(source_buffer_.size()); }
225 241
242 scoped_ptr<Filter> filter_;
243
226 std::string source_buffer_; 244 std::string source_buffer_;
227 245
228 char* deflate_encode_buffer_; 246 char* deflate_encode_buffer_;
229 int deflate_encode_len_; 247 int deflate_encode_len_;
230 248
231 char* gzip_encode_buffer_; 249 char* gzip_encode_buffer_;
232 int gzip_encode_len_; 250 int gzip_encode_len_;
251
252 private:
253 net::MockFilterContext filter_context_;
233 }; 254 };
234 255
235 // Basic scenario: decoding deflate data with big enough buffer. 256 // Basic scenario: decoding deflate data with big enough buffer.
236 TEST_F(GZipUnitTest, DecodeDeflate) { 257 TEST_F(GZipUnitTest, DecodeDeflate) {
237 // Decode the compressed data with filter 258 // Decode the compressed data with filter
238 std::vector<Filter::FilterType> filter_types; 259 InitFilter(Filter::FILTER_TYPE_DEFLATE);
239 filter_types.push_back(Filter::FILTER_TYPE_DEFLATE); 260 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_); 261 deflate_encode_len_);
jar (doing other things) 2011/03/01 22:44:03 With this code not being explicit about buffer siz
adamk 2011/03/01 23:13:54 I've added this assert to InitBuffer(). There are
245 filter->FlushStreamBuffer(deflate_encode_len_); 262 filter_->FlushStreamBuffer(deflate_encode_len_);
246 263
247 char deflate_decode_buffer[kDefaultBufferSize]; 264 char deflate_decode_buffer[kDefaultBufferSize];
248 int deflate_decode_size = kDefaultBufferSize; 265 int deflate_decode_size = kDefaultBufferSize;
249 filter->ReadData(deflate_decode_buffer, &deflate_decode_size); 266 filter_->ReadData(deflate_decode_buffer, &deflate_decode_size);
250 267
251 // Compare the decoding result with source data 268 // Compare the decoding result with source data
252 EXPECT_TRUE(deflate_decode_size == source_len()); 269 EXPECT_TRUE(deflate_decode_size == source_len());
253 EXPECT_EQ(memcmp(source_buffer(), deflate_decode_buffer, source_len()), 0); 270 EXPECT_EQ(memcmp(source_buffer(), deflate_decode_buffer, source_len()), 0);
254 } 271 }
255 272
256 // Basic scenario: decoding gzip data with big enough buffer. 273 // Basic scenario: decoding gzip data with big enough buffer.
257 TEST_F(GZipUnitTest, DecodeGZip) { 274 TEST_F(GZipUnitTest, DecodeGZip) {
258 // Decode the compressed data with filter 275 // Decode the compressed data with filter
259 std::vector<Filter::FilterType> filter_types; 276 InitFilter(Filter::FILTER_TYPE_GZIP);
260 filter_types.push_back(Filter::FILTER_TYPE_GZIP); 277 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_); 278 gzip_encode_len_);
266 filter->FlushStreamBuffer(gzip_encode_len_); 279 filter_->FlushStreamBuffer(gzip_encode_len_);
267 280
268 char gzip_decode_buffer[kDefaultBufferSize]; 281 char gzip_decode_buffer[kDefaultBufferSize];
269 int gzip_decode_size = kDefaultBufferSize; 282 int gzip_decode_size = kDefaultBufferSize;
270 filter->ReadData(gzip_decode_buffer, &gzip_decode_size); 283 filter_->ReadData(gzip_decode_buffer, &gzip_decode_size);
271 284
272 // Compare the decoding result with source data 285 // Compare the decoding result with source data
273 EXPECT_TRUE(gzip_decode_size == source_len()); 286 EXPECT_TRUE(gzip_decode_size == source_len());
274 EXPECT_EQ(memcmp(source_buffer(), gzip_decode_buffer, source_len()), 0); 287 EXPECT_EQ(memcmp(source_buffer(), gzip_decode_buffer, source_len()), 0);
275 } 288 }
276 289
277 // Tests we can call filter repeatedly to get all the data decoded. 290 // 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 291 // To do that, we create a filter with a small buffer that can not hold all
279 // the input data. 292 // the input data.
280 TEST_F(GZipUnitTest, DecodeWithSmallBuffer) { 293 TEST_F(GZipUnitTest, DecodeWithSmallBuffer) {
281 std::vector<Filter::FilterType> filter_types; 294 InitFilterWithBufferSize(Filter::FILTER_TYPE_DEFLATE, kSmallBufferSize);
282 filter_types.push_back(Filter::FILTER_TYPE_DEFLATE); 295 DecodeAndCompareWithFilter(filter_.get(), source_buffer(), source_len(),
283 net::MockFilterContext filter_context(kSmallBufferSize);
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_, 296 deflate_encode_buffer_, deflate_encode_len_,
288 kDefaultBufferSize); 297 kDefaultBufferSize);
289 } 298 }
290 299
291 // Tests we can still decode with just 1 byte buffer in the filter. 300 // 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 301 // 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 302 // header correctly. (2) Sometimes the filter will consume input without
294 // generating output. Verify filter can handle it correctly. 303 // generating output. Verify filter can handle it correctly.
295 TEST_F(GZipUnitTest, DecodeWithOneByteBuffer) { 304 TEST_F(GZipUnitTest, DecodeWithOneByteBuffer) {
296 std::vector<Filter::FilterType> filter_types; 305 InitFilterWithBufferSize(Filter::FILTER_TYPE_GZIP, 1);
297 filter_types.push_back(Filter::FILTER_TYPE_GZIP); 306 DecodeAndCompareWithFilter(filter_.get(), source_buffer(), source_len(),
298 net::MockFilterContext filter_context(1);
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_, 307 gzip_encode_buffer_, gzip_encode_len_,
303 kDefaultBufferSize); 308 kDefaultBufferSize);
304 } 309 }
305 310
306 // Tests we can decode when caller has small buffer to read out from filter. 311 // Tests we can decode when caller has small buffer to read out from filter.
307 TEST_F(GZipUnitTest, DecodeWithSmallOutputBuffer) { 312 TEST_F(GZipUnitTest, DecodeWithSmallOutputBuffer) {
308 std::vector<Filter::FilterType> filter_types; 313 InitFilter(Filter::FILTER_TYPE_DEFLATE);
309 filter_types.push_back(Filter::FILTER_TYPE_DEFLATE); 314 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_, 315 deflate_encode_buffer_, deflate_encode_len_,
315 kSmallBufferSize); 316 kSmallBufferSize);
316 } 317 }
317 318
318 // Tests we can still decode with just 1 byte buffer in the filter and just 1 319 // Tests we can still decode with just 1 byte buffer in the filter and just 1
319 // byte buffer in the caller. 320 // byte buffer in the caller.
320 TEST_F(GZipUnitTest, DecodeWithOneByteInputAndOutputBuffer) { 321 TEST_F(GZipUnitTest, DecodeWithOneByteInputAndOutputBuffer) {
321 std::vector<Filter::FilterType> filter_types; 322 InitFilterWithBufferSize(Filter::FILTER_TYPE_GZIP, 1);
322 filter_types.push_back(Filter::FILTER_TYPE_GZIP); 323 DecodeAndCompareWithFilter(filter_.get(), source_buffer(), source_len(),
323 net::MockFilterContext filter_context(1);
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); 324 gzip_encode_buffer_, gzip_encode_len_, 1);
328 } 325 }
329 326
330 // Decoding deflate stream with corrupted data. 327 // Decoding deflate stream with corrupted data.
331 TEST_F(GZipUnitTest, DecodeCorruptedData) { 328 TEST_F(GZipUnitTest, DecodeCorruptedData) {
332 char corrupt_data[kDefaultBufferSize]; 329 char corrupt_data[kDefaultBufferSize];
333 int corrupt_data_len = deflate_encode_len_; 330 int corrupt_data_len = deflate_encode_len_;
334 memcpy(corrupt_data, deflate_encode_buffer_, deflate_encode_len_); 331 memcpy(corrupt_data, deflate_encode_buffer_, deflate_encode_len_);
335 332
336 int pos = corrupt_data_len / 2; 333 int pos = corrupt_data_len / 2;
337 corrupt_data[pos] = !corrupt_data[pos]; 334 corrupt_data[pos] = !corrupt_data[pos];
338 335
339 // Decode the corrupted data with filter 336 // Decode the corrupted data with filter
340 std::vector<Filter::FilterType> filter_types; 337 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]; 338 char corrupt_decode_buffer[kDefaultBufferSize];
346 int corrupt_decode_size = kDefaultBufferSize; 339 int corrupt_decode_size = kDefaultBufferSize;
347 340
348 int code = DecodeAllWithFilter(filter.get(), corrupt_data, corrupt_data_len, 341 int code = DecodeAllWithFilter(filter_.get(), corrupt_data, corrupt_data_len,
349 corrupt_decode_buffer, &corrupt_decode_size); 342 corrupt_decode_buffer, &corrupt_decode_size);
350 343
351 // Expect failures 344 // Expect failures
352 EXPECT_TRUE(code == Filter::FILTER_ERROR); 345 EXPECT_TRUE(code == Filter::FILTER_ERROR);
353 } 346 }
354 347
355 // Decoding deflate stream with missing data. 348 // Decoding deflate stream with missing data.
356 TEST_F(GZipUnitTest, DecodeMissingData) { 349 TEST_F(GZipUnitTest, DecodeMissingData) {
357 char corrupt_data[kDefaultBufferSize]; 350 char corrupt_data[kDefaultBufferSize];
358 int corrupt_data_len = deflate_encode_len_; 351 int corrupt_data_len = deflate_encode_len_;
359 memcpy(corrupt_data, deflate_encode_buffer_, deflate_encode_len_); 352 memcpy(corrupt_data, deflate_encode_buffer_, deflate_encode_len_);
360 353
361 int pos = corrupt_data_len / 2; 354 int pos = corrupt_data_len / 2;
362 int len = corrupt_data_len - pos - 1; 355 int len = corrupt_data_len - pos - 1;
363 memmove(&corrupt_data[pos], &corrupt_data[pos+1], len); 356 memmove(&corrupt_data[pos], &corrupt_data[pos+1], len);
364 --corrupt_data_len; 357 --corrupt_data_len;
365 358
366 // Decode the corrupted data with filter 359 // Decode the corrupted data with filter
367 std::vector<Filter::FilterType> filter_types; 360 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]; 361 char corrupt_decode_buffer[kDefaultBufferSize];
373 int corrupt_decode_size = kDefaultBufferSize; 362 int corrupt_decode_size = kDefaultBufferSize;
374 363
375 int code = DecodeAllWithFilter(filter.get(), corrupt_data, corrupt_data_len, 364 int code = DecodeAllWithFilter(filter_.get(), corrupt_data, corrupt_data_len,
376 corrupt_decode_buffer, &corrupt_decode_size); 365 corrupt_decode_buffer, &corrupt_decode_size);
377 366
378 // Expect failures 367 // Expect failures
379 EXPECT_EQ(Filter::FILTER_ERROR, code); 368 EXPECT_EQ(Filter::FILTER_ERROR, code);
380 } 369 }
381 370
382 // Decoding gzip stream with corrupted header. 371 // Decoding gzip stream with corrupted header.
383 TEST_F(GZipUnitTest, DecodeCorruptedHeader) { 372 TEST_F(GZipUnitTest, DecodeCorruptedHeader) {
384 char corrupt_data[kDefaultBufferSize]; 373 char corrupt_data[kDefaultBufferSize];
385 int corrupt_data_len = gzip_encode_len_; 374 int corrupt_data_len = gzip_encode_len_;
386 memcpy(corrupt_data, gzip_encode_buffer_, gzip_encode_len_); 375 memcpy(corrupt_data, gzip_encode_buffer_, gzip_encode_len_);
387 376
388 corrupt_data[2] = !corrupt_data[2]; 377 corrupt_data[2] = !corrupt_data[2];
389 378
390 // Decode the corrupted data with filter 379 // Decode the corrupted data with filter
391 std::vector<Filter::FilterType> filter_types; 380 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]; 381 char corrupt_decode_buffer[kDefaultBufferSize];
397 int corrupt_decode_size = kDefaultBufferSize; 382 int corrupt_decode_size = kDefaultBufferSize;
398 383
399 int code = DecodeAllWithFilter(filter.get(), corrupt_data, corrupt_data_len, 384 int code = DecodeAllWithFilter(filter_.get(), corrupt_data, corrupt_data_len,
400 corrupt_decode_buffer, &corrupt_decode_size); 385 corrupt_decode_buffer, &corrupt_decode_size);
401 386
402 // Expect failures 387 // Expect failures
403 EXPECT_TRUE(code == Filter::FILTER_ERROR); 388 EXPECT_TRUE(code == Filter::FILTER_ERROR);
404 } 389 }
405
406 } // namespace
OLDNEW
« no previous file with comments | « net/base/gzip_filter.h ('k') | net/base/mock_filter_context.h » ('j') | net/base/sdch_filter.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698