| OLD | NEW |
| 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 <limits.h> | 5 #include <limits.h> |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/scoped_ptr.h" | 12 #include "base/scoped_ptr.h" |
| 13 #include "net/base/filter.h" | 13 #include "net/base/filter.h" |
| 14 #include "net/base/filter_unittest.h" |
| 14 #include "net/base/sdch_filter.h" | 15 #include "net/base/sdch_filter.h" |
| 15 #include "net/url_request/url_request_http_job.cc" | 16 #include "net/url_request/url_request_http_job.cc" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 17 #include "third_party/zlib/zlib.h" | 18 #include "third_party/zlib/zlib.h" |
| 18 | 19 |
| 19 //------------------------------------------------------------------------------ | 20 //------------------------------------------------------------------------------ |
| 20 // Provide sample data and compression results with a sample VCDIFF dictionary. | 21 // Provide sample data and compression results with a sample VCDIFF dictionary. |
| 21 // Note an SDCH dictionary has extra meta-data before the VCDIFF dictionary. | 22 // Note an SDCH dictionary has extra meta-data before the VCDIFF dictionary. |
| 22 static const char kTestVcdiffDictionary[] = "DictionaryFor" | 23 static const char kTestVcdiffDictionary[] = "DictionaryFor" |
| 23 "SdchCompression1SdchCompression2SdchCompression3SdchCompression\n"; | 24 "SdchCompression1SdchCompression2SdchCompression3SdchCompression\n"; |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 144 return dictionary; | 145 return dictionary; |
| 145 } | 146 } |
| 146 | 147 |
| 147 //------------------------------------------------------------------------------ | 148 //------------------------------------------------------------------------------ |
| 148 | 149 |
| 149 TEST_F(SdchFilterTest, BasicBadDictionary) { | 150 TEST_F(SdchFilterTest, BasicBadDictionary) { |
| 150 std::vector<Filter::FilterType> filter_types; | 151 std::vector<Filter::FilterType> filter_types; |
| 151 filter_types.push_back(Filter::FILTER_TYPE_SDCH); | 152 filter_types.push_back(Filter::FILTER_TYPE_SDCH); |
| 152 const int kInputBufferSize(30); | 153 const int kInputBufferSize(30); |
| 153 char output_buffer[20]; | 154 char output_buffer[20]; |
| 154 scoped_ptr<Filter> filter(Filter::Factory(filter_types, kInputBufferSize)); | 155 MockFilterContext filter_context(kInputBufferSize); |
| 155 std::string url_string("http://ignore.com"); | 156 std::string url_string("http://ignore.com"); |
| 156 filter->SetURL(GURL(url_string)); | 157 filter_context.SetURL(GURL(url_string)); |
| 158 scoped_ptr<Filter> filter(Filter::Factory(filter_types, filter_context)); |
| 157 | 159 |
| 158 | 160 |
| 159 // With no input data, try to read output. | 161 // With no input data, try to read output. |
| 160 int output_bytes_or_buffer_size = sizeof(output_buffer); | 162 int output_bytes_or_buffer_size = sizeof(output_buffer); |
| 161 Filter::FilterStatus status = filter->ReadData(output_buffer, | 163 Filter::FilterStatus status = filter->ReadData(output_buffer, |
| 162 &output_bytes_or_buffer_size); | 164 &output_bytes_or_buffer_size); |
| 163 | 165 |
| 164 EXPECT_EQ(0, output_bytes_or_buffer_size); | 166 EXPECT_EQ(0, output_bytes_or_buffer_size); |
| 165 EXPECT_EQ(Filter::FILTER_NEED_MORE_DATA, status); | 167 EXPECT_EQ(Filter::FILTER_NEED_MORE_DATA, status); |
| 166 | 168 |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 239 GURL url(url_string); | 241 GURL url(url_string); |
| 240 EXPECT_TRUE(sdch_manager_->AddSdchDictionary(dictionary, url)); | 242 EXPECT_TRUE(sdch_manager_->AddSdchDictionary(dictionary, url)); |
| 241 | 243 |
| 242 std::string compressed(NewSdchCompressedData(dictionary)); | 244 std::string compressed(NewSdchCompressedData(dictionary)); |
| 243 | 245 |
| 244 std::vector<Filter::FilterType> filter_types; | 246 std::vector<Filter::FilterType> filter_types; |
| 245 filter_types.push_back(Filter::FILTER_TYPE_SDCH); | 247 filter_types.push_back(Filter::FILTER_TYPE_SDCH); |
| 246 | 248 |
| 247 // Decode with a large buffer (larger than test input, or compressed data). | 249 // Decode with a large buffer (larger than test input, or compressed data). |
| 248 const int kInputBufferSize(100); | 250 const int kInputBufferSize(100); |
| 249 scoped_ptr<Filter> filter(Filter::Factory(filter_types, kInputBufferSize)); | 251 MockFilterContext filter_context(kInputBufferSize); |
| 250 filter->SetURL(url); | 252 filter_context.SetURL(url); |
| 253 |
| 254 scoped_ptr<Filter> filter(Filter::Factory(filter_types, filter_context)); |
| 251 | 255 |
| 252 size_t feed_block_size = 100; | 256 size_t feed_block_size = 100; |
| 253 size_t output_block_size = 100; | 257 size_t output_block_size = 100; |
| 254 std::string output; | 258 std::string output; |
| 255 EXPECT_TRUE(FilterTestData(compressed, feed_block_size, output_block_size, | 259 EXPECT_TRUE(FilterTestData(compressed, feed_block_size, output_block_size, |
| 256 filter.get(), &output)); | 260 filter.get(), &output)); |
| 257 EXPECT_EQ(output, expanded_); | 261 EXPECT_EQ(output, expanded_); |
| 258 | 262 |
| 259 // Decode with really small buffers (size 1) to check for edge effects. | 263 // Decode with really small buffers (size 1) to check for edge effects. |
| 260 filter.reset((Filter::Factory(filter_types, kInputBufferSize))); | 264 filter.reset((Filter::Factory(filter_types, filter_context))); |
| 261 filter->SetURL(url); | |
| 262 | 265 |
| 263 feed_block_size = 1; | 266 feed_block_size = 1; |
| 264 output_block_size = 1; | 267 output_block_size = 1; |
| 265 output.clear(); | 268 output.clear(); |
| 266 EXPECT_TRUE(FilterTestData(compressed, feed_block_size, output_block_size, | 269 EXPECT_TRUE(FilterTestData(compressed, feed_block_size, output_block_size, |
| 267 filter.get(), &output)); | 270 filter.get(), &output)); |
| 268 EXPECT_EQ(output, expanded_); | 271 EXPECT_EQ(output, expanded_); |
| 269 } | 272 } |
| 270 | 273 |
| 271 TEST_F(SdchFilterTest, NoDecodeHttps) { | 274 TEST_F(SdchFilterTest, NoDecodeHttps) { |
| 272 // Construct a valid SDCH dictionary from a VCDIFF dictionary. | 275 // Construct a valid SDCH dictionary from a VCDIFF dictionary. |
| 273 const std::string kSampleDomain = "sdchtest.com"; | 276 const std::string kSampleDomain = "sdchtest.com"; |
| 274 std::string dictionary(NewSdchDictionary(kSampleDomain)); | 277 std::string dictionary(NewSdchDictionary(kSampleDomain)); |
| 275 | 278 |
| 276 std::string url_string = "http://" + kSampleDomain; | 279 std::string url_string = "http://" + kSampleDomain; |
| 277 | 280 |
| 278 GURL url(url_string); | 281 GURL url(url_string); |
| 279 EXPECT_TRUE(sdch_manager_->AddSdchDictionary(dictionary, url)); | 282 EXPECT_TRUE(sdch_manager_->AddSdchDictionary(dictionary, url)); |
| 280 | 283 |
| 281 std::string compressed(NewSdchCompressedData(dictionary)); | 284 std::string compressed(NewSdchCompressedData(dictionary)); |
| 282 | 285 |
| 283 std::vector<Filter::FilterType> filter_types; | 286 std::vector<Filter::FilterType> filter_types; |
| 284 filter_types.push_back(Filter::FILTER_TYPE_SDCH); | 287 filter_types.push_back(Filter::FILTER_TYPE_SDCH); |
| 285 | 288 |
| 286 const int kInputBufferSize(100); | 289 const int kInputBufferSize(100); |
| 287 scoped_ptr<Filter> filter(Filter::Factory(filter_types, kInputBufferSize)); | 290 MockFilterContext filter_context(kInputBufferSize); |
| 291 filter_context.SetURL(GURL("https://" + kSampleDomain)); |
| 292 scoped_ptr<Filter> filter(Filter::Factory(filter_types, filter_context)); |
| 293 |
| 288 const size_t feed_block_size(100); | 294 const size_t feed_block_size(100); |
| 289 const size_t output_block_size(100); | 295 const size_t output_block_size(100); |
| 290 std::string output; | 296 std::string output; |
| 291 | 297 |
| 292 filter->SetURL(GURL("https://" + kSampleDomain)); | |
| 293 EXPECT_FALSE(FilterTestData(compressed, feed_block_size, output_block_size, | 298 EXPECT_FALSE(FilterTestData(compressed, feed_block_size, output_block_size, |
| 294 filter.get(), &output)); | 299 filter.get(), &output)); |
| 295 } | 300 } |
| 296 | 301 |
| 297 // Current failsafe TODO/hack refuses to decode any content that doesn't use | 302 // Current failsafe TODO/hack refuses to decode any content that doesn't use |
| 298 // http as the scheme (see use of DICTIONARY_SELECTED_FOR_NON_HTTP). | 303 // http as the scheme (see use of DICTIONARY_SELECTED_FOR_NON_HTTP). |
| 299 // The following tests this blockage. Note that blacklisting results, so we | 304 // The following tests this blockage. Note that blacklisting results, so we |
| 300 // we need separate tests for each of these. | 305 // we need separate tests for each of these. |
| 301 TEST_F(SdchFilterTest, NoDecodeFtp) { | 306 TEST_F(SdchFilterTest, NoDecodeFtp) { |
| 302 // Construct a valid SDCH dictionary from a VCDIFF dictionary. | 307 // Construct a valid SDCH dictionary from a VCDIFF dictionary. |
| 303 const std::string kSampleDomain = "sdchtest.com"; | 308 const std::string kSampleDomain = "sdchtest.com"; |
| 304 std::string dictionary(NewSdchDictionary(kSampleDomain)); | 309 std::string dictionary(NewSdchDictionary(kSampleDomain)); |
| 305 | 310 |
| 306 std::string url_string = "http://" + kSampleDomain; | 311 std::string url_string = "http://" + kSampleDomain; |
| 307 | 312 |
| 308 GURL url(url_string); | 313 GURL url(url_string); |
| 309 EXPECT_TRUE(sdch_manager_->AddSdchDictionary(dictionary, url)); | 314 EXPECT_TRUE(sdch_manager_->AddSdchDictionary(dictionary, url)); |
| 310 | 315 |
| 311 std::string compressed(NewSdchCompressedData(dictionary)); | 316 std::string compressed(NewSdchCompressedData(dictionary)); |
| 312 | 317 |
| 313 std::vector<Filter::FilterType> filter_types; | 318 std::vector<Filter::FilterType> filter_types; |
| 314 filter_types.push_back(Filter::FILTER_TYPE_SDCH); | 319 filter_types.push_back(Filter::FILTER_TYPE_SDCH); |
| 315 | 320 |
| 316 const int kInputBufferSize(100); | 321 const int kInputBufferSize(100); |
| 317 scoped_ptr<Filter> filter(Filter::Factory(filter_types, kInputBufferSize)); | 322 MockFilterContext filter_context(kInputBufferSize); |
| 323 filter_context.SetURL(GURL("ftp://" + kSampleDomain)); |
| 324 scoped_ptr<Filter> filter(Filter::Factory(filter_types, filter_context)); |
| 325 |
| 318 const size_t feed_block_size(100); | 326 const size_t feed_block_size(100); |
| 319 const size_t output_block_size(100); | 327 const size_t output_block_size(100); |
| 320 std::string output; | 328 std::string output; |
| 321 | 329 |
| 322 filter->SetURL(GURL("ftp://" + kSampleDomain)); | |
| 323 EXPECT_FALSE(FilterTestData(compressed, feed_block_size, output_block_size, | 330 EXPECT_FALSE(FilterTestData(compressed, feed_block_size, output_block_size, |
| 324 filter.get(), &output)); | 331 filter.get(), &output)); |
| 325 } | 332 } |
| 326 | 333 |
| 327 TEST_F(SdchFilterTest, NoDecodeFileColon) { | 334 TEST_F(SdchFilterTest, NoDecodeFileColon) { |
| 328 // Construct a valid SDCH dictionary from a VCDIFF dictionary. | 335 // Construct a valid SDCH dictionary from a VCDIFF dictionary. |
| 329 const std::string kSampleDomain = "sdchtest.com"; | 336 const std::string kSampleDomain = "sdchtest.com"; |
| 330 std::string dictionary(NewSdchDictionary(kSampleDomain)); | 337 std::string dictionary(NewSdchDictionary(kSampleDomain)); |
| 331 | 338 |
| 332 std::string url_string = "http://" + kSampleDomain; | 339 std::string url_string = "http://" + kSampleDomain; |
| 333 | 340 |
| 334 GURL url(url_string); | 341 GURL url(url_string); |
| 335 EXPECT_TRUE(sdch_manager_->AddSdchDictionary(dictionary, url)); | 342 EXPECT_TRUE(sdch_manager_->AddSdchDictionary(dictionary, url)); |
| 336 | 343 |
| 337 std::string compressed(NewSdchCompressedData(dictionary)); | 344 std::string compressed(NewSdchCompressedData(dictionary)); |
| 338 | 345 |
| 339 std::vector<Filter::FilterType> filter_types; | 346 std::vector<Filter::FilterType> filter_types; |
| 340 filter_types.push_back(Filter::FILTER_TYPE_SDCH); | 347 filter_types.push_back(Filter::FILTER_TYPE_SDCH); |
| 341 | 348 |
| 342 const int kInputBufferSize(100); | 349 const int kInputBufferSize(100); |
| 343 scoped_ptr<Filter> filter(Filter::Factory(filter_types, kInputBufferSize)); | 350 MockFilterContext filter_context(kInputBufferSize); |
| 351 filter_context.SetURL(GURL("file://" + kSampleDomain)); |
| 352 scoped_ptr<Filter> filter(Filter::Factory(filter_types, filter_context)); |
| 353 |
| 344 const size_t feed_block_size(100); | 354 const size_t feed_block_size(100); |
| 345 const size_t output_block_size(100); | 355 const size_t output_block_size(100); |
| 346 std::string output; | 356 std::string output; |
| 347 | 357 |
| 348 filter->SetURL(GURL("file://" + kSampleDomain)); | |
| 349 EXPECT_FALSE(FilterTestData(compressed, feed_block_size, output_block_size, | 358 EXPECT_FALSE(FilterTestData(compressed, feed_block_size, output_block_size, |
| 350 filter.get(), &output)); | 359 filter.get(), &output)); |
| 351 } | 360 } |
| 352 | 361 |
| 353 TEST_F(SdchFilterTest, NoDecodeAboutColon) { | 362 TEST_F(SdchFilterTest, NoDecodeAboutColon) { |
| 354 // Construct a valid SDCH dictionary from a VCDIFF dictionary. | 363 // Construct a valid SDCH dictionary from a VCDIFF dictionary. |
| 355 const std::string kSampleDomain = "sdchtest.com"; | 364 const std::string kSampleDomain = "sdchtest.com"; |
| 356 std::string dictionary(NewSdchDictionary(kSampleDomain)); | 365 std::string dictionary(NewSdchDictionary(kSampleDomain)); |
| 357 | 366 |
| 358 std::string url_string = "http://" + kSampleDomain; | 367 std::string url_string = "http://" + kSampleDomain; |
| 359 | 368 |
| 360 GURL url(url_string); | 369 GURL url(url_string); |
| 361 EXPECT_TRUE(sdch_manager_->AddSdchDictionary(dictionary, url)); | 370 EXPECT_TRUE(sdch_manager_->AddSdchDictionary(dictionary, url)); |
| 362 | 371 |
| 363 std::string compressed(NewSdchCompressedData(dictionary)); | 372 std::string compressed(NewSdchCompressedData(dictionary)); |
| 364 | 373 |
| 365 std::vector<Filter::FilterType> filter_types; | 374 std::vector<Filter::FilterType> filter_types; |
| 366 filter_types.push_back(Filter::FILTER_TYPE_SDCH); | 375 filter_types.push_back(Filter::FILTER_TYPE_SDCH); |
| 367 | 376 |
| 368 const int kInputBufferSize(100); | 377 const int kInputBufferSize(100); |
| 369 scoped_ptr<Filter> filter(Filter::Factory(filter_types, kInputBufferSize)); | 378 MockFilterContext filter_context(kInputBufferSize); |
| 379 filter_context.SetURL(GURL("about://" + kSampleDomain)); |
| 380 scoped_ptr<Filter> filter(Filter::Factory(filter_types, filter_context)); |
| 381 |
| 370 const size_t feed_block_size(100); | 382 const size_t feed_block_size(100); |
| 371 const size_t output_block_size(100); | 383 const size_t output_block_size(100); |
| 372 std::string output; | 384 std::string output; |
| 373 | 385 |
| 374 filter->SetURL(GURL("about://" + kSampleDomain)); | |
| 375 EXPECT_FALSE(FilterTestData(compressed, feed_block_size, output_block_size, | 386 EXPECT_FALSE(FilterTestData(compressed, feed_block_size, output_block_size, |
| 376 filter.get(), &output)); | 387 filter.get(), &output)); |
| 377 } | 388 } |
| 378 | 389 |
| 379 TEST_F(SdchFilterTest, NoDecodeJavaScript) { | 390 TEST_F(SdchFilterTest, NoDecodeJavaScript) { |
| 380 // Construct a valid SDCH dictionary from a VCDIFF dictionary. | 391 // Construct a valid SDCH dictionary from a VCDIFF dictionary. |
| 381 const std::string kSampleDomain = "sdchtest.com"; | 392 const std::string kSampleDomain = "sdchtest.com"; |
| 382 std::string dictionary(NewSdchDictionary(kSampleDomain)); | 393 std::string dictionary(NewSdchDictionary(kSampleDomain)); |
| 383 | 394 |
| 384 std::string url_string = "http://" + kSampleDomain; | 395 std::string url_string = "http://" + kSampleDomain; |
| 385 | 396 |
| 386 GURL url(url_string); | 397 GURL url(url_string); |
| 387 EXPECT_TRUE(sdch_manager_->AddSdchDictionary(dictionary, url)); | 398 EXPECT_TRUE(sdch_manager_->AddSdchDictionary(dictionary, url)); |
| 388 | 399 |
| 389 std::string compressed(NewSdchCompressedData(dictionary)); | 400 std::string compressed(NewSdchCompressedData(dictionary)); |
| 390 | 401 |
| 391 std::vector<Filter::FilterType> filter_types; | 402 std::vector<Filter::FilterType> filter_types; |
| 392 filter_types.push_back(Filter::FILTER_TYPE_SDCH); | 403 filter_types.push_back(Filter::FILTER_TYPE_SDCH); |
| 393 | 404 |
| 394 const int kInputBufferSize(100); | 405 const int kInputBufferSize(100); |
| 395 scoped_ptr<Filter> filter(Filter::Factory(filter_types, kInputBufferSize)); | 406 MockFilterContext filter_context(kInputBufferSize); |
| 407 filter_context.SetURL(GURL("javascript://" + kSampleDomain)); |
| 408 scoped_ptr<Filter> filter(Filter::Factory(filter_types, filter_context)); |
| 409 |
| 396 const size_t feed_block_size(100); | 410 const size_t feed_block_size(100); |
| 397 const size_t output_block_size(100); | 411 const size_t output_block_size(100); |
| 398 std::string output; | 412 std::string output; |
| 399 | 413 |
| 400 filter->SetURL(GURL("javascript://" + kSampleDomain)); | |
| 401 EXPECT_FALSE(FilterTestData(compressed, feed_block_size, output_block_size, | 414 EXPECT_FALSE(FilterTestData(compressed, feed_block_size, output_block_size, |
| 402 filter.get(), &output)); | 415 filter.get(), &output)); |
| 403 } | 416 } |
| 404 | 417 |
| 405 TEST_F(SdchFilterTest, CanStillDecodeHttp) { | 418 TEST_F(SdchFilterTest, CanStillDecodeHttp) { |
| 406 // Construct a valid SDCH dictionary from a VCDIFF dictionary. | 419 // Construct a valid SDCH dictionary from a VCDIFF dictionary. |
| 407 const std::string kSampleDomain = "sdchtest.com"; | 420 const std::string kSampleDomain = "sdchtest.com"; |
| 408 std::string dictionary(NewSdchDictionary(kSampleDomain)); | 421 std::string dictionary(NewSdchDictionary(kSampleDomain)); |
| 409 | 422 |
| 410 std::string url_string = "http://" + kSampleDomain; | 423 std::string url_string = "http://" + kSampleDomain; |
| 411 | 424 |
| 412 GURL url(url_string); | 425 GURL url(url_string); |
| 413 EXPECT_TRUE(sdch_manager_->AddSdchDictionary(dictionary, url)); | 426 EXPECT_TRUE(sdch_manager_->AddSdchDictionary(dictionary, url)); |
| 414 | 427 |
| 415 std::string compressed(NewSdchCompressedData(dictionary)); | 428 std::string compressed(NewSdchCompressedData(dictionary)); |
| 416 | 429 |
| 417 std::vector<Filter::FilterType> filter_types; | 430 std::vector<Filter::FilterType> filter_types; |
| 418 filter_types.push_back(Filter::FILTER_TYPE_SDCH); | 431 filter_types.push_back(Filter::FILTER_TYPE_SDCH); |
| 419 | 432 |
| 420 const int kInputBufferSize(100); | 433 const int kInputBufferSize(100); |
| 421 scoped_ptr<Filter> filter(Filter::Factory(filter_types, kInputBufferSize)); | 434 MockFilterContext filter_context(kInputBufferSize); |
| 435 filter_context.SetURL(GURL("http://" + kSampleDomain)); |
| 436 scoped_ptr<Filter> filter(Filter::Factory(filter_types, filter_context)); |
| 437 |
| 422 const size_t feed_block_size(100); | 438 const size_t feed_block_size(100); |
| 423 const size_t output_block_size(100); | 439 const size_t output_block_size(100); |
| 424 std::string output; | 440 std::string output; |
| 425 | 441 |
| 426 filter->SetURL(GURL("http://" + kSampleDomain)); | |
| 427 EXPECT_TRUE(FilterTestData(compressed, feed_block_size, output_block_size, | 442 EXPECT_TRUE(FilterTestData(compressed, feed_block_size, output_block_size, |
| 428 filter.get(), &output)); | 443 filter.get(), &output)); |
| 429 } | 444 } |
| 430 | 445 |
| 431 TEST_F(SdchFilterTest, CrossDomainDictionaryUse) { | 446 TEST_F(SdchFilterTest, CrossDomainDictionaryUse) { |
| 432 // Construct a valid SDCH dictionary from a VCDIFF dictionary. | 447 // Construct a valid SDCH dictionary from a VCDIFF dictionary. |
| 433 const std::string kSampleDomain = "sdchtest.com"; | 448 const std::string kSampleDomain = "sdchtest.com"; |
| 434 std::string dictionary(NewSdchDictionary(kSampleDomain)); | 449 std::string dictionary(NewSdchDictionary(kSampleDomain)); |
| 435 | 450 |
| 436 std::string url_string = "http://" + kSampleDomain; | 451 std::string url_string = "http://" + kSampleDomain; |
| 437 | 452 |
| 438 GURL url(url_string); | 453 GURL url(url_string); |
| 439 EXPECT_TRUE(sdch_manager_->AddSdchDictionary(dictionary, url)); | 454 EXPECT_TRUE(sdch_manager_->AddSdchDictionary(dictionary, url)); |
| 440 | 455 |
| 441 std::string compressed(NewSdchCompressedData(dictionary)); | 456 std::string compressed(NewSdchCompressedData(dictionary)); |
| 442 | 457 |
| 443 std::vector<Filter::FilterType> filter_types; | 458 std::vector<Filter::FilterType> filter_types; |
| 444 filter_types.push_back(Filter::FILTER_TYPE_SDCH); | 459 filter_types.push_back(Filter::FILTER_TYPE_SDCH); |
| 445 const int kInputBufferSize(100); | 460 const int kInputBufferSize(100); |
| 446 | 461 |
| 447 // Decode with content arriving from the "wrong" domain. | 462 // Decode with content arriving from the "wrong" domain. |
| 448 // This tests CanSet() in the sdch_manager_-> | 463 // This tests SdchManager::CanSet(). |
| 449 scoped_ptr<Filter> filter((Filter::Factory(filter_types, kInputBufferSize))); | 464 MockFilterContext filter_context(kInputBufferSize); |
| 450 GURL wrong_domain_url("http://www.wrongdomain.com"); | 465 GURL wrong_domain_url("http://www.wrongdomain.com"); |
| 451 filter->SetURL(wrong_domain_url); | 466 filter_context.SetURL(wrong_domain_url); |
| 467 scoped_ptr<Filter> filter((Filter::Factory(filter_types, filter_context))); |
| 452 | 468 |
| 453 size_t feed_block_size = 100; | 469 size_t feed_block_size = 100; |
| 454 size_t output_block_size = 100; | 470 size_t output_block_size = 100; |
| 455 std::string output; | 471 std::string output; |
| 456 EXPECT_FALSE(FilterTestData(compressed, feed_block_size, output_block_size, | 472 EXPECT_FALSE(FilterTestData(compressed, feed_block_size, output_block_size, |
| 457 filter.get(), &output)); | 473 filter.get(), &output)); |
| 458 EXPECT_EQ(output.size(), 0u); // No output written. | 474 EXPECT_EQ(output.size(), 0u); // No output written. |
| 459 | 475 |
| 460 EXPECT_TRUE(SdchManager::Global()->IsInSupportedDomain(GURL(url_string))); | 476 EXPECT_TRUE(SdchManager::Global()->IsInSupportedDomain(GURL(url_string))); |
| 461 EXPECT_FALSE(SdchManager::Global()->IsInSupportedDomain(wrong_domain_url)); | 477 EXPECT_FALSE(SdchManager::Global()->IsInSupportedDomain(wrong_domain_url)); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 479 dictionary_with_path.append(dictionary); | 495 dictionary_with_path.append(dictionary); |
| 480 EXPECT_TRUE(sdch_manager_->AddSdchDictionary(dictionary_with_path, url)); | 496 EXPECT_TRUE(sdch_manager_->AddSdchDictionary(dictionary_with_path, url)); |
| 481 | 497 |
| 482 std::string compressed_for_path(NewSdchCompressedData(dictionary_with_path)); | 498 std::string compressed_for_path(NewSdchCompressedData(dictionary_with_path)); |
| 483 | 499 |
| 484 std::vector<Filter::FilterType> filter_types; | 500 std::vector<Filter::FilterType> filter_types; |
| 485 filter_types.push_back(Filter::FILTER_TYPE_SDCH); | 501 filter_types.push_back(Filter::FILTER_TYPE_SDCH); |
| 486 const int kInputBufferSize(100); | 502 const int kInputBufferSize(100); |
| 487 | 503 |
| 488 // Test decode the path data, arriving from a valid path. | 504 // Test decode the path data, arriving from a valid path. |
| 489 scoped_ptr<Filter> filter((Filter::Factory(filter_types, kInputBufferSize))); | 505 MockFilterContext filter_context(kInputBufferSize); |
| 490 filter->SetURL(GURL(url_string + path)); | 506 filter_context.SetURL(GURL(url_string + path)); |
| 507 scoped_ptr<Filter> filter((Filter::Factory(filter_types, filter_context))); |
| 491 | 508 |
| 492 size_t feed_block_size = 100; | 509 size_t feed_block_size = 100; |
| 493 size_t output_block_size = 100; | 510 size_t output_block_size = 100; |
| 494 std::string output; | 511 std::string output; |
| 495 | 512 |
| 496 EXPECT_TRUE(FilterTestData(compressed_for_path, feed_block_size, | 513 EXPECT_TRUE(FilterTestData(compressed_for_path, feed_block_size, |
| 497 output_block_size, filter.get(), &output)); | 514 output_block_size, filter.get(), &output)); |
| 498 EXPECT_EQ(output, expanded_); | 515 EXPECT_EQ(output, expanded_); |
| 499 | 516 |
| 500 // Test decode the path data, arriving from a invalid path. | 517 // Test decode the path data, arriving from a invalid path. |
| 501 filter.reset((Filter::Factory(filter_types, kInputBufferSize))); | 518 filter_context.SetURL(GURL(url_string)); |
| 502 filter->SetURL(GURL(url_string)); | 519 filter.reset((Filter::Factory(filter_types, filter_context))); |
| 503 | 520 |
| 504 feed_block_size = 100; | 521 feed_block_size = 100; |
| 505 output_block_size = 100; | 522 output_block_size = 100; |
| 506 output.clear(); | 523 output.clear(); |
| 507 EXPECT_FALSE(FilterTestData(compressed_for_path, feed_block_size, | 524 EXPECT_FALSE(FilterTestData(compressed_for_path, feed_block_size, |
| 508 output_block_size, filter.get(), &output)); | 525 output_block_size, filter.get(), &output)); |
| 509 EXPECT_EQ(output.size(), 0u); // No output written. | 526 EXPECT_EQ(output.size(), 0u); // No output written. |
| 510 | 527 |
| 511 EXPECT_FALSE(SdchManager::Global()->IsInSupportedDomain(GURL(url_string))); | 528 EXPECT_FALSE(SdchManager::Global()->IsInSupportedDomain(GURL(url_string))); |
| 512 SdchManager::ClearBlacklistings(); | 529 SdchManager::ClearBlacklistings(); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 532 EXPECT_TRUE(sdch_manager_->AddSdchDictionary(dictionary_with_port, | 549 EXPECT_TRUE(sdch_manager_->AddSdchDictionary(dictionary_with_port, |
| 533 GURL(url_string + ":" + port))); | 550 GURL(url_string + ":" + port))); |
| 534 | 551 |
| 535 std::string compressed_for_port(NewSdchCompressedData(dictionary_with_port)); | 552 std::string compressed_for_port(NewSdchCompressedData(dictionary_with_port)); |
| 536 | 553 |
| 537 std::vector<Filter::FilterType> filter_types; | 554 std::vector<Filter::FilterType> filter_types; |
| 538 filter_types.push_back(Filter::FILTER_TYPE_SDCH); | 555 filter_types.push_back(Filter::FILTER_TYPE_SDCH); |
| 539 const int kInputBufferSize(100); | 556 const int kInputBufferSize(100); |
| 540 | 557 |
| 541 // Test decode the port data, arriving from a valid port. | 558 // Test decode the port data, arriving from a valid port. |
| 542 scoped_ptr<Filter> filter((Filter::Factory(filter_types, kInputBufferSize))); | 559 MockFilterContext filter_context(kInputBufferSize); |
| 543 filter->SetURL(GURL(url_string + ":" + port)); | 560 filter_context.SetURL(GURL(url_string + ":" + port)); |
| 561 scoped_ptr<Filter> filter((Filter::Factory(filter_types, filter_context))); |
| 544 | 562 |
| 545 size_t feed_block_size = 100; | 563 size_t feed_block_size = 100; |
| 546 size_t output_block_size = 100; | 564 size_t output_block_size = 100; |
| 547 std::string output; | 565 std::string output; |
| 548 EXPECT_TRUE(FilterTestData(compressed_for_port, feed_block_size, | 566 EXPECT_TRUE(FilterTestData(compressed_for_port, feed_block_size, |
| 549 output_block_size, filter.get(), &output)); | 567 output_block_size, filter.get(), &output)); |
| 550 EXPECT_EQ(output, expanded_); | 568 EXPECT_EQ(output, expanded_); |
| 551 | 569 |
| 552 // Test decode the port data, arriving from a valid (default) port. | 570 // Test decode the port data, arriving from a valid (default) port. |
| 553 filter.reset((Filter::Factory(filter_types, kInputBufferSize))); | 571 filter_context.SetURL(GURL(url_string)); // Default port. |
| 554 filter->SetURL(GURL(url_string)); // Default port. | 572 filter.reset((Filter::Factory(filter_types, filter_context))); |
| 555 | 573 |
| 556 feed_block_size = 100; | 574 feed_block_size = 100; |
| 557 output_block_size = 100; | 575 output_block_size = 100; |
| 558 output.clear(); | 576 output.clear(); |
| 559 EXPECT_TRUE(FilterTestData(compressed_for_port, feed_block_size, | 577 EXPECT_TRUE(FilterTestData(compressed_for_port, feed_block_size, |
| 560 output_block_size, filter.get(), &output)); | 578 output_block_size, filter.get(), &output)); |
| 561 EXPECT_EQ(output, expanded_); | 579 EXPECT_EQ(output, expanded_); |
| 562 | 580 |
| 563 // Test decode the port data, arriving from a invalid port. | 581 // Test decode the port data, arriving from a invalid port. |
| 564 filter.reset((Filter::Factory(filter_types, kInputBufferSize))); | 582 filter_context.SetURL(GURL(url_string + ":" + port + "1")); |
| 565 filter->SetURL(GURL(url_string + ":" + port + "1")); | 583 filter.reset((Filter::Factory(filter_types, filter_context))); |
| 566 | 584 |
| 567 feed_block_size = 100; | 585 feed_block_size = 100; |
| 568 output_block_size = 100; | 586 output_block_size = 100; |
| 569 output.clear(); | 587 output.clear(); |
| 570 EXPECT_FALSE(FilterTestData(compressed_for_port, feed_block_size, | 588 EXPECT_FALSE(FilterTestData(compressed_for_port, feed_block_size, |
| 571 output_block_size, filter.get(), &output)); | 589 output_block_size, filter.get(), &output)); |
| 572 EXPECT_EQ(output.size(), 0u); // No output written. | 590 EXPECT_EQ(output.size(), 0u); // No output written. |
| 573 | 591 |
| 574 EXPECT_FALSE(SdchManager::Global()->IsInSupportedDomain(GURL(url_string))); | 592 EXPECT_FALSE(SdchManager::Global()->IsInSupportedDomain(GURL(url_string))); |
| 575 SdchManager::ClearBlacklistings(); | 593 SdchManager::ClearBlacklistings(); |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 654 // Construct a chained filter. | 672 // Construct a chained filter. |
| 655 std::vector<Filter::FilterType> filter_types; | 673 std::vector<Filter::FilterType> filter_types; |
| 656 filter_types.push_back(Filter::FILTER_TYPE_SDCH); | 674 filter_types.push_back(Filter::FILTER_TYPE_SDCH); |
| 657 filter_types.push_back(Filter::FILTER_TYPE_GZIP); | 675 filter_types.push_back(Filter::FILTER_TYPE_GZIP); |
| 658 | 676 |
| 659 // First try with a large buffer (larger than test input, or compressed data). | 677 // First try with a large buffer (larger than test input, or compressed data). |
| 660 const size_t kLargeInputBufferSize(1000); // Used internally in filters. | 678 const size_t kLargeInputBufferSize(1000); // Used internally in filters. |
| 661 CHECK(kLargeInputBufferSize > gzip_compressed_sdch.size()); | 679 CHECK(kLargeInputBufferSize > gzip_compressed_sdch.size()); |
| 662 CHECK(kLargeInputBufferSize > sdch_compressed.size()); | 680 CHECK(kLargeInputBufferSize > sdch_compressed.size()); |
| 663 CHECK(kLargeInputBufferSize > expanded_.size()); | 681 CHECK(kLargeInputBufferSize > expanded_.size()); |
| 664 scoped_ptr<Filter> filter(Filter::Factory(filter_types, | 682 MockFilterContext filter_context(kLargeInputBufferSize); |
| 665 kLargeInputBufferSize)); | 683 filter_context.SetURL(url); |
| 666 filter->SetURL(url); | 684 scoped_ptr<Filter> filter(Filter::Factory(filter_types, filter_context)); |
| 667 | 685 |
| 668 // Verify that chained filter is waiting for data. | 686 // Verify that chained filter is waiting for data. |
| 669 char tiny_output_buffer[10]; | 687 char tiny_output_buffer[10]; |
| 670 int tiny_output_size = sizeof(tiny_output_buffer); | 688 int tiny_output_size = sizeof(tiny_output_buffer); |
| 671 EXPECT_EQ(Filter::FILTER_NEED_MORE_DATA, | 689 EXPECT_EQ(Filter::FILTER_NEED_MORE_DATA, |
| 672 filter->ReadData(tiny_output_buffer, &tiny_output_size)); | 690 filter->ReadData(tiny_output_buffer, &tiny_output_size)); |
| 673 | 691 |
| 674 // Make chain process all data. | 692 // Make chain process all data. |
| 675 size_t feed_block_size = kLargeInputBufferSize; | 693 size_t feed_block_size = kLargeInputBufferSize; |
| 676 size_t output_block_size = kLargeInputBufferSize; | 694 size_t output_block_size = kLargeInputBufferSize; |
| 677 std::string output; | 695 std::string output; |
| 678 EXPECT_TRUE(FilterTestData(gzip_compressed_sdch, feed_block_size, | 696 EXPECT_TRUE(FilterTestData(gzip_compressed_sdch, feed_block_size, |
| 679 output_block_size, filter.get(), &output)); | 697 output_block_size, filter.get(), &output)); |
| 680 EXPECT_EQ(output, expanded_); | 698 EXPECT_EQ(output, expanded_); |
| 681 | 699 |
| 682 // Next try with a mid-sized internal buffer size. | 700 // Next try with a mid-sized internal buffer size. |
| 683 const size_t kMidSizedInputBufferSize(100); | 701 const size_t kMidSizedInputBufferSize(100); |
| 684 // Buffer should be big enough to swallow whole gzip content. | 702 // Buffer should be big enough to swallow whole gzip content. |
| 685 CHECK(kMidSizedInputBufferSize > gzip_compressed_sdch.size()); | 703 CHECK(kMidSizedInputBufferSize > gzip_compressed_sdch.size()); |
| 686 // Buffer should be small enough that entire SDCH content can't fit. | 704 // Buffer should be small enough that entire SDCH content can't fit. |
| 687 // We'll go even further, and force the chain to flush the buffer between the | 705 // We'll go even further, and force the chain to flush the buffer between the |
| 688 // two filters more than once (that is why we multiply by 2). | 706 // two filters more than once (that is why we multiply by 2). |
| 689 CHECK(kMidSizedInputBufferSize * 2 < sdch_compressed.size()); | 707 CHECK(kMidSizedInputBufferSize * 2 < sdch_compressed.size()); |
| 690 filter.reset(Filter::Factory(filter_types, kMidSizedInputBufferSize)); | 708 filter_context.SetBufferSize(kMidSizedInputBufferSize); |
| 691 filter->SetURL(url); | 709 filter_context.SetURL(url); |
| 710 filter.reset(Filter::Factory(filter_types, filter_context)); |
| 692 | 711 |
| 693 feed_block_size = kMidSizedInputBufferSize; | 712 feed_block_size = kMidSizedInputBufferSize; |
| 694 output_block_size = kMidSizedInputBufferSize; | 713 output_block_size = kMidSizedInputBufferSize; |
| 695 output.clear(); | 714 output.clear(); |
| 696 EXPECT_TRUE(FilterTestData(gzip_compressed_sdch, feed_block_size, | 715 EXPECT_TRUE(FilterTestData(gzip_compressed_sdch, feed_block_size, |
| 697 output_block_size, filter.get(), &output)); | 716 output_block_size, filter.get(), &output)); |
| 698 EXPECT_EQ(output, expanded_); | 717 EXPECT_EQ(output, expanded_); |
| 699 | 718 |
| 700 // Next try with a tiny input and output buffer to cover edge effects. | 719 // Next try with a tiny input and output buffer to cover edge effects. |
| 701 filter.reset(Filter::Factory(filter_types, kLargeInputBufferSize)); | 720 filter_context.SetBufferSize(kLargeInputBufferSize); |
| 702 filter->SetURL(url); | 721 filter.reset(Filter::Factory(filter_types, filter_context)); |
| 703 | 722 |
| 704 feed_block_size = 1; | 723 feed_block_size = 1; |
| 705 output_block_size = 1; | 724 output_block_size = 1; |
| 706 output.clear(); | 725 output.clear(); |
| 707 EXPECT_TRUE(FilterTestData(gzip_compressed_sdch, feed_block_size, | 726 EXPECT_TRUE(FilterTestData(gzip_compressed_sdch, feed_block_size, |
| 708 output_block_size, filter.get(), &output)); | 727 output_block_size, filter.get(), &output)); |
| 709 EXPECT_EQ(output, expanded_); | 728 EXPECT_EQ(output, expanded_); |
| 710 } | 729 } |
| 711 | 730 |
| 712 TEST_F(SdchFilterTest, DefaultGzipIfSdch) { | 731 TEST_F(SdchFilterTest, DefaultGzipIfSdch) { |
| (...skipping 12 matching lines...) Expand all Loading... |
| 725 std::string gzip_compressed_sdch = gzip_compress(sdch_compressed); | 744 std::string gzip_compressed_sdch = gzip_compress(sdch_compressed); |
| 726 | 745 |
| 727 // Only claim to have sdch content, but really use the gzipped sdch content. | 746 // Only claim to have sdch content, but really use the gzipped sdch content. |
| 728 // System should automatically add the missing (optional) gzip. | 747 // System should automatically add the missing (optional) gzip. |
| 729 std::vector<Filter::FilterType> filter_types; | 748 std::vector<Filter::FilterType> filter_types; |
| 730 filter_types.push_back(Filter::FILTER_TYPE_SDCH); | 749 filter_types.push_back(Filter::FILTER_TYPE_SDCH); |
| 731 Filter::FixupEncodingTypes(true, "anything/mime", &filter_types); | 750 Filter::FixupEncodingTypes(true, "anything/mime", &filter_types); |
| 732 | 751 |
| 733 // First try with a large buffer (larger than test input, or compressed data). | 752 // First try with a large buffer (larger than test input, or compressed data). |
| 734 const int kInputBufferSize(100); | 753 const int kInputBufferSize(100); |
| 735 scoped_ptr<Filter> filter(Filter::Factory(filter_types, kInputBufferSize)); | 754 MockFilterContext filter_context(kInputBufferSize); |
| 736 filter->SetURL(url); | 755 filter_context.SetURL(url); |
| 756 scoped_ptr<Filter> filter(Filter::Factory(filter_types, filter_context)); |
| 757 |
| 737 | 758 |
| 738 // Verify that chained filter is waiting for data. | 759 // Verify that chained filter is waiting for data. |
| 739 char tiny_output_buffer[10]; | 760 char tiny_output_buffer[10]; |
| 740 int tiny_output_size = sizeof(tiny_output_buffer); | 761 int tiny_output_size = sizeof(tiny_output_buffer); |
| 741 EXPECT_EQ(Filter::FILTER_NEED_MORE_DATA, | 762 EXPECT_EQ(Filter::FILTER_NEED_MORE_DATA, |
| 742 filter->ReadData(tiny_output_buffer, &tiny_output_size)); | 763 filter->ReadData(tiny_output_buffer, &tiny_output_size)); |
| 743 | 764 |
| 744 size_t feed_block_size = 100; | 765 size_t feed_block_size = 100; |
| 745 size_t output_block_size = 100; | 766 size_t output_block_size = 100; |
| 746 std::string output; | 767 std::string output; |
| 747 EXPECT_TRUE(FilterTestData(gzip_compressed_sdch, feed_block_size, | 768 EXPECT_TRUE(FilterTestData(gzip_compressed_sdch, feed_block_size, |
| 748 output_block_size, filter.get(), &output)); | 769 output_block_size, filter.get(), &output)); |
| 749 EXPECT_EQ(output, expanded_); | 770 EXPECT_EQ(output, expanded_); |
| 750 | 771 |
| 751 // Next try with a tiny buffer to cover edge effects. | 772 // Next try with a tiny buffer to cover edge effects. |
| 752 filter.reset(Filter::Factory(filter_types, kInputBufferSize)); | 773 filter.reset(Filter::Factory(filter_types, filter_context)); |
| 753 filter->SetURL(url); | |
| 754 | 774 |
| 755 feed_block_size = 1; | 775 feed_block_size = 1; |
| 756 output_block_size = 1; | 776 output_block_size = 1; |
| 757 output.clear(); | 777 output.clear(); |
| 758 EXPECT_TRUE(FilterTestData(gzip_compressed_sdch, feed_block_size, | 778 EXPECT_TRUE(FilterTestData(gzip_compressed_sdch, feed_block_size, |
| 759 output_block_size, filter.get(), &output)); | 779 output_block_size, filter.get(), &output)); |
| 760 EXPECT_EQ(output, expanded_); | 780 EXPECT_EQ(output, expanded_); |
| 761 } | 781 } |
| 762 | 782 |
| 763 TEST_F(SdchFilterTest, DomainSupported) { | 783 TEST_F(SdchFilterTest, DomainSupported) { |
| (...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 936 TEST_F(SdchFilterTest, DictionaryTooLarge) { | 956 TEST_F(SdchFilterTest, DictionaryTooLarge) { |
| 937 std::string dictionary_domain(".google.com"); | 957 std::string dictionary_domain(".google.com"); |
| 938 std::string dictionary_text(NewSdchDictionary(dictionary_domain)); | 958 std::string dictionary_text(NewSdchDictionary(dictionary_domain)); |
| 939 | 959 |
| 940 dictionary_text.append( | 960 dictionary_text.append( |
| 941 SdchManager::kMaxDictionarySize + 1 - dictionary_text.size(), ' '); | 961 SdchManager::kMaxDictionarySize + 1 - dictionary_text.size(), ' '); |
| 942 EXPECT_FALSE(sdch_manager_->AddSdchDictionary(dictionary_text, | 962 EXPECT_FALSE(sdch_manager_->AddSdchDictionary(dictionary_text, |
| 943 GURL("http://" + dictionary_domain))); | 963 GURL("http://" + dictionary_domain))); |
| 944 } | 964 } |
| 945 | 965 |
| OLD | NEW |