OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 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/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/test/simple_test_clock.h" |
13 #include "net/base/io_buffer.h" | 14 #include "net/base/io_buffer.h" |
14 #include "net/filter/mock_filter_context.h" | 15 #include "net/filter/mock_filter_context.h" |
15 #include "net/filter/sdch_filter.h" | 16 #include "net/filter/sdch_filter.h" |
16 #include "net/url_request/url_request_context.h" | 17 #include "net/url_request/url_request_context.h" |
17 #include "net/url_request/url_request_http_job.h" | 18 #include "net/url_request/url_request_http_job.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
19 #include "third_party/zlib/zlib.h" | 20 #include "third_party/zlib/zlib.h" |
20 | 21 |
21 namespace net { | 22 namespace net { |
22 | 23 |
23 //------------------------------------------------------------------------------ | 24 //------------------------------------------------------------------------------ |
24 // Provide sample data and compression results with a sample VCDIFF dictionary. | 25 // Provide sample data and compression results with a sample VCDIFF dictionary. |
25 // Note an SDCH dictionary has extra meta-data before the VCDIFF dictionary. | 26 // Note an SDCH dictionary has extra meta-data before the VCDIFF dictionary. |
26 static const char kTestVcdiffDictionary[] = "DictionaryFor" | 27 static const char kTestVcdiffDictionary[] = "DictionaryFor" |
27 "SdchCompression1SdchCompression2SdchCompression3SdchCompression\n"; | 28 "SdchCompression1SdchCompression2SdchCompression3SdchCompression\n"; |
28 // Pre-compression test data. Note that we pad with a lot of highly gzip | 29 // Pre-compression test data. Note that we pad with a lot of highly gzip |
29 // compressible content to help to exercise the chaining pipeline. That is why | 30 // compressible content to help to exercise the chaining pipeline. That is why |
30 // there are a PILE of zeros at the start and end. | 31 // there are a PILE of zeros at the start and end. |
31 // This will ensure that gzip compressed data can be fed to the chain in one | 32 // This will ensure that gzip compressed data can be fed to the chain in one |
32 // gulp, but (with careful selection of intermediate buffers) that it takes | 33 // gulp, but (with careful selection of intermediate buffers) that it takes |
33 // several sdch buffers worth of data to satisfy the sdch filter. See detailed | 34 // several sdch buffers worth of data to satisfy the sdch filter. See detailed |
34 // CHECK() calls in FilterChaining test for specifics. | 35 // CHECK() calls in FilterChaining test for specifics. |
35 static const char kTestData[] = "0000000000000000000000000000000000000000000000" | 36 static const char kTestData[] = "0000000000000000000000000000000000000000000000" |
36 "0000000000000000000000000000TestData " | 37 "0000000000000000000000000000TestData " |
37 "SdchCompression1SdchCompression2SdchCompression3SdchCompression" | 38 "SdchCompression1SdchCompression2SdchCompression3SdchCompression" |
38 "00000000000000000000000000000000000000000000000000000000000000000000000000" | 39 "00000000000000000000000000000000000000000000000000000000000000000000000000" |
39 "000000000000000000000000000000000000000\n"; | 40 "000000000000000000000000000000000000000\n"; |
40 | 41 |
41 // Note SDCH compressed data will include a reference to the SDCH dictionary. | 42 // Note SDCH compressed data will include a reference to the SDCH dictionary. |
42 static const char kSdchCompressedTestData[] = | 43 static const char kSdchCompressedTestData[] = |
43 "\326\303\304\0\0\001M\0\201S\202\004\0\201E\006\001" | 44 "\326\303\304\0\0\001M\0\201S\202\004\0\201E\006\001" |
(...skipping 12 matching lines...) Expand all Loading... |
56 sizeof(kSdchCompressedTestData) - 1), | 57 sizeof(kSdchCompressedTestData) - 1), |
57 expanded_(kTestData, sizeof(kTestData) - 1), | 58 expanded_(kTestData, sizeof(kTestData) - 1), |
58 sdch_manager_(new SdchManager), | 59 sdch_manager_(new SdchManager), |
59 filter_context_(new MockFilterContext) { | 60 filter_context_(new MockFilterContext) { |
60 URLRequestContext* url_request_context = | 61 URLRequestContext* url_request_context = |
61 filter_context_->GetModifiableURLRequestContext(); | 62 filter_context_->GetModifiableURLRequestContext(); |
62 | 63 |
63 url_request_context->set_sdch_manager(sdch_manager_.get()); | 64 url_request_context->set_sdch_manager(sdch_manager_.get()); |
64 } | 65 } |
65 | 66 |
66 // Attempt to add a dictionary to the manager; returns whether or not | 67 // Attempt to add a dictionary to the manager and probe for success or |
67 // the attempt succeeded. | 68 // failure. |
68 bool AddSdchDictionary(const std::string& dictionary_text, | 69 bool AddSdchDictionary(const std::string& dictionary_text, |
69 const GURL& gurl) { | 70 const GURL& gurl) { |
70 return sdch_manager_->AddSdchDictionary(dictionary_text, gurl) == SDCH_OK; | 71 return sdch_manager_->AddSdchDictionary(dictionary_text, gurl) == SDCH_OK; |
71 } | 72 } |
72 | 73 |
73 MockFilterContext* filter_context() { return filter_context_.get(); } | 74 MockFilterContext* filter_context() { return filter_context_.get(); } |
74 | 75 |
| 76 // Sets both the GURL and the SDCH response for a filter context. |
| 77 void SetupFilterContextWithGURL(GURL url) { |
| 78 filter_context_->SetURL(url); |
| 79 filter_context_->SetSdchResponse( |
| 80 sdch_manager_->GetDictionarySet(url).Pass()); |
| 81 } |
| 82 |
75 std::string NewSdchCompressedData(const std::string dictionary) { | 83 std::string NewSdchCompressedData(const std::string dictionary) { |
76 std::string client_hash; | 84 std::string client_hash; |
77 std::string server_hash; | 85 std::string server_hash; |
78 SdchManager::GenerateHash(dictionary, &client_hash, &server_hash); | 86 SdchManager::GenerateHash(dictionary, &client_hash, &server_hash); |
79 | 87 |
80 // Build compressed data that refers to our dictionary. | 88 // Build compressed data that refers to our dictionary. |
81 std::string compressed(server_hash); | 89 std::string compressed(server_hash); |
82 compressed.append("\0", 1); | 90 compressed.append("\0", 1); |
83 compressed.append(vcdiff_compressed_data_); | 91 compressed.append(vcdiff_compressed_data_); |
84 return compressed; | 92 return compressed; |
(...skipping 16 matching lines...) Expand all Loading... |
101 SdchManager::GenerateHash(dictionary, &client_hash, &server_hash); | 109 SdchManager::GenerateHash(dictionary, &client_hash, &server_hash); |
102 | 110 |
103 EXPECT_EQ(client_hash, "lMQBjS3P"); | 111 EXPECT_EQ(client_hash, "lMQBjS3P"); |
104 EXPECT_EQ(server_hash, "MyciMVll"); | 112 EXPECT_EQ(server_hash, "MyciMVll"); |
105 } | 113 } |
106 | 114 |
107 | 115 |
108 //------------------------------------------------------------------------------ | 116 //------------------------------------------------------------------------------ |
109 // Provide a generic helper function for trying to filter data. | 117 // Provide a generic helper function for trying to filter data. |
110 // This function repeatedly calls the filter to process data, until the entire | 118 // This function repeatedly calls the filter to process data, until the entire |
111 // source is consumed. The return value from the filter is appended to output. | 119 // source is consumed. The return value from the filter is appended to output. |
112 // This allows us to vary input and output block sizes in order to test for edge | 120 // This allows us to vary input and output block sizes in order to test for edge |
113 // effects (boundary effects?) during the filtering process. | 121 // effects (boundary effects?) during the filtering process. |
114 // This function provides data to the filter in blocks of no-more-than the | 122 // This function provides data to the filter in blocks of no-more-than the |
115 // specified input_block_length. It allows the filter to fill no more than | 123 // specified input_block_length. It allows the filter to fill no more than |
116 // output_buffer_length in any one call to proccess (a.k.a., Read) data, and | 124 // output_buffer_length in any one call to proccess (a.k.a., Read) data, and |
117 // concatenates all these little output blocks into the singular output string. | 125 // concatenates all these little output blocks into the singular output string. |
118 static bool FilterTestData(const std::string& source, | 126 static bool FilterTestData(const std::string& source, |
119 size_t input_block_length, | 127 size_t input_block_length, |
120 const size_t output_buffer_length, | 128 const size_t output_buffer_length, |
121 Filter* filter, std::string* output) { | 129 Filter* filter, std::string* output) { |
122 CHECK_GT(input_block_length, 0u); | 130 CHECK_GT(input_block_length, 0u); |
123 Filter::FilterStatus status(Filter::FILTER_NEED_MORE_DATA); | 131 Filter::FilterStatus status(Filter::FILTER_NEED_MORE_DATA); |
124 size_t source_index = 0; | 132 size_t source_index = 0; |
125 scoped_ptr<char[]> output_buffer(new char[output_buffer_length]); | 133 scoped_ptr<char[]> output_buffer(new char[output_buffer_length]); |
(...skipping 26 matching lines...) Expand all Loading... |
152 if (!domain.empty()) { | 160 if (!domain.empty()) { |
153 dictionary.append("Domain: "); | 161 dictionary.append("Domain: "); |
154 dictionary.append(domain); | 162 dictionary.append(domain); |
155 dictionary.append("\n"); | 163 dictionary.append("\n"); |
156 } | 164 } |
157 dictionary.append("\n"); | 165 dictionary.append("\n"); |
158 dictionary.append(kTestVcdiffDictionary, sizeof(kTestVcdiffDictionary) - 1); | 166 dictionary.append(kTestVcdiffDictionary, sizeof(kTestVcdiffDictionary) - 1); |
159 return dictionary; | 167 return dictionary; |
160 } | 168 } |
161 | 169 |
| 170 static std::string NewSdchExpiredDictionary(const std::string& domain) { |
| 171 std::string dictionary; |
| 172 if (!domain.empty()) { |
| 173 dictionary.append("Domain: "); |
| 174 dictionary.append(domain); |
| 175 dictionary.append("\n"); |
| 176 } |
| 177 dictionary.append("Max-Age: 0\n"); |
| 178 dictionary.append("\n"); |
| 179 dictionary.append(kTestVcdiffDictionary, sizeof(kTestVcdiffDictionary) - 1); |
| 180 return dictionary; |
| 181 } |
| 182 |
162 //------------------------------------------------------------------------------ | 183 //------------------------------------------------------------------------------ |
163 | 184 |
164 TEST_F(SdchFilterTest, EmptyInputOk) { | 185 TEST_F(SdchFilterTest, EmptyInputOk) { |
165 std::vector<Filter::FilterType> filter_types; | 186 std::vector<Filter::FilterType> filter_types; |
166 filter_types.push_back(Filter::FILTER_TYPE_SDCH); | 187 filter_types.push_back(Filter::FILTER_TYPE_SDCH); |
167 char output_buffer[20]; | 188 char output_buffer[20]; |
168 std::string url_string("http://ignore.com"); | 189 std::string url_string("http://ignore.com"); |
169 filter_context()->SetURL(GURL(url_string)); | 190 filter_context()->SetURL(GURL(url_string)); |
170 scoped_ptr<Filter> filter(Filter::Factory(filter_types, *filter_context())); | 191 scoped_ptr<Filter> filter(Filter::Factory(filter_types, *filter_context())); |
171 | 192 |
(...skipping 18 matching lines...) Expand all Loading... |
190 scoped_ptr<Filter> filter(Filter::Factory(filter_types, *filter_context())); | 211 scoped_ptr<Filter> filter(Filter::Factory(filter_types, *filter_context())); |
191 | 212 |
192 // With no input data, try to read output. | 213 // With no input data, try to read output. |
193 int output_bytes_or_buffer_size = sizeof(output_buffer); | 214 int output_bytes_or_buffer_size = sizeof(output_buffer); |
194 Filter::FilterStatus status = filter->ReadData(output_buffer, | 215 Filter::FilterStatus status = filter->ReadData(output_buffer, |
195 &output_bytes_or_buffer_size); | 216 &output_bytes_or_buffer_size); |
196 | 217 |
197 EXPECT_EQ(0, output_bytes_or_buffer_size); | 218 EXPECT_EQ(0, output_bytes_or_buffer_size); |
198 EXPECT_EQ(Filter::FILTER_NEED_MORE_DATA, status); | 219 EXPECT_EQ(Filter::FILTER_NEED_MORE_DATA, status); |
199 | 220 |
200 // Partially tear down context. Anything that goes through request() | 221 // Partially tear down context. Anything that goes through request() |
201 // without checking it for null in the URLRequestJob::HttpFilterContext | 222 // without checking it for null in the URLRequestJob::HttpFilterContext |
202 // implementation is suspect. Everything that does check it for null should | 223 // implementation is suspect. Everything that does check it for null should |
203 // return null. This is to test for incorrectly relying on filter_context() | 224 // return null. This is to test for incorrectly relying on filter_context() |
204 // from the SdchFilter destructor. | 225 // from the SdchFilter destructor. |
205 filter_context()->NukeUnstableInterfaces(); | 226 filter_context()->NukeUnstableInterfaces(); |
206 } | 227 } |
207 | 228 |
208 TEST_F(SdchFilterTest, PassThroughWhenTentative) { | 229 TEST_F(SdchFilterTest, PassThroughWhenTentative) { |
209 std::vector<Filter::FilterType> filter_types; | 230 std::vector<Filter::FilterType> filter_types; |
210 // Selective a tentative filter (which can fall back to pass through). | 231 // Selective a tentative filter (which can fall back to pass through). |
211 filter_types.push_back(Filter::FILTER_TYPE_GZIP_HELPING_SDCH); | 232 filter_types.push_back(Filter::FILTER_TYPE_GZIP_HELPING_SDCH); |
212 char output_buffer[20]; | 233 char output_buffer[20]; |
213 // Response code needs to be 200 to allow a pass through. | 234 // Response code needs to be 200 to allow a pass through. |
(...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
444 std::string url_string = "http://" + kSampleDomain; | 465 std::string url_string = "http://" + kSampleDomain; |
445 | 466 |
446 GURL url(url_string); | 467 GURL url(url_string); |
447 EXPECT_TRUE(AddSdchDictionary(dictionary, url)); | 468 EXPECT_TRUE(AddSdchDictionary(dictionary, url)); |
448 | 469 |
449 std::string compressed(NewSdchCompressedData(dictionary)); | 470 std::string compressed(NewSdchCompressedData(dictionary)); |
450 | 471 |
451 std::vector<Filter::FilterType> filter_types; | 472 std::vector<Filter::FilterType> filter_types; |
452 filter_types.push_back(Filter::FILTER_TYPE_SDCH); | 473 filter_types.push_back(Filter::FILTER_TYPE_SDCH); |
453 | 474 |
454 filter_context()->SetURL(url); | 475 SetupFilterContextWithGURL(url); |
455 | 476 |
456 scoped_ptr<Filter> filter(Filter::Factory(filter_types, *filter_context())); | 477 scoped_ptr<Filter> filter(Filter::Factory(filter_types, *filter_context())); |
457 | 478 |
458 size_t feed_block_size = 100; | 479 size_t feed_block_size = 100; |
459 size_t output_block_size = 100; | 480 size_t output_block_size = 100; |
460 std::string output; | 481 std::string output; |
461 EXPECT_TRUE(FilterTestData(compressed, feed_block_size, output_block_size, | 482 EXPECT_TRUE(FilterTestData(compressed, feed_block_size, output_block_size, |
462 filter.get(), &output)); | 483 filter.get(), &output)); |
463 EXPECT_EQ(output, expanded_); | 484 EXPECT_EQ(output, expanded_); |
464 | 485 |
(...skipping 16 matching lines...) Expand all Loading... |
481 std::string url_string = "http://" + kSampleDomain; | 502 std::string url_string = "http://" + kSampleDomain; |
482 | 503 |
483 GURL url(url_string); | 504 GURL url(url_string); |
484 EXPECT_TRUE(AddSdchDictionary(dictionary, url)); | 505 EXPECT_TRUE(AddSdchDictionary(dictionary, url)); |
485 | 506 |
486 std::string compressed(NewSdchCompressedData(dictionary)); | 507 std::string compressed(NewSdchCompressedData(dictionary)); |
487 | 508 |
488 std::vector<Filter::FilterType> filter_types; | 509 std::vector<Filter::FilterType> filter_types; |
489 filter_types.push_back(Filter::FILTER_TYPE_SDCH); | 510 filter_types.push_back(Filter::FILTER_TYPE_SDCH); |
490 | 511 |
491 filter_context()->SetURL(GURL("https://" + kSampleDomain)); | 512 GURL filter_context_gurl("https://" + kSampleDomain); |
| 513 SetupFilterContextWithGURL(GURL("https://" + kSampleDomain)); |
492 scoped_ptr<Filter> filter(Filter::Factory(filter_types, *filter_context())); | 514 scoped_ptr<Filter> filter(Filter::Factory(filter_types, *filter_context())); |
493 | 515 |
494 const size_t feed_block_size(100); | 516 const size_t feed_block_size(100); |
495 const size_t output_block_size(100); | 517 const size_t output_block_size(100); |
496 std::string output; | 518 std::string output; |
497 | 519 |
498 EXPECT_FALSE(FilterTestData(compressed, feed_block_size, output_block_size, | 520 EXPECT_FALSE(FilterTestData(compressed, feed_block_size, output_block_size, |
499 filter.get(), &output)); | 521 filter.get(), &output)); |
500 } | 522 } |
501 | 523 |
502 // Current failsafe TODO/hack refuses to decode any content that doesn't use | 524 // Current failsafe TODO/hack refuses to decode any content that doesn't use |
503 // http as the scheme (see use of DICTIONARY_SELECTED_FOR_NON_HTTP). | 525 // http as the scheme (see use of DICTIONARY_SELECTED_FOR_NON_HTTP). |
504 // The following tests this blockage. Note that blacklisting results, so we | 526 // The following tests this blockage. Note that blacklisting results, so we |
505 // we need separate tests for each of these. | 527 // we need separate tests for each of these. |
506 TEST_F(SdchFilterTest, NoDecodeFtp) { | 528 TEST_F(SdchFilterTest, NoDecodeFtp) { |
507 // Construct a valid SDCH dictionary from a VCDIFF dictionary. | 529 // Construct a valid SDCH dictionary from a VCDIFF dictionary. |
508 const std::string kSampleDomain = "sdchtest.com"; | 530 const std::string kSampleDomain = "sdchtest.com"; |
509 std::string dictionary(NewSdchDictionary(kSampleDomain)); | 531 std::string dictionary(NewSdchDictionary(kSampleDomain)); |
510 | 532 |
511 std::string url_string = "http://" + kSampleDomain; | 533 std::string url_string = "http://" + kSampleDomain; |
512 | 534 |
513 GURL url(url_string); | 535 GURL url(url_string); |
514 EXPECT_TRUE(AddSdchDictionary(dictionary, url)); | 536 EXPECT_TRUE(AddSdchDictionary(dictionary, url)); |
515 | 537 |
516 std::string compressed(NewSdchCompressedData(dictionary)); | 538 std::string compressed(NewSdchCompressedData(dictionary)); |
517 | 539 |
518 std::vector<Filter::FilterType> filter_types; | 540 std::vector<Filter::FilterType> filter_types; |
519 filter_types.push_back(Filter::FILTER_TYPE_SDCH); | 541 filter_types.push_back(Filter::FILTER_TYPE_SDCH); |
520 | 542 |
521 filter_context()->SetURL(GURL("ftp://" + kSampleDomain)); | 543 SetupFilterContextWithGURL(GURL("ftp://" + kSampleDomain)); |
522 scoped_ptr<Filter> filter(Filter::Factory(filter_types, *filter_context())); | 544 scoped_ptr<Filter> filter(Filter::Factory(filter_types, *filter_context())); |
523 | 545 |
524 const size_t feed_block_size(100); | 546 const size_t feed_block_size(100); |
525 const size_t output_block_size(100); | 547 const size_t output_block_size(100); |
526 std::string output; | 548 std::string output; |
527 | 549 |
528 EXPECT_FALSE(FilterTestData(compressed, feed_block_size, output_block_size, | 550 EXPECT_FALSE(FilterTestData(compressed, feed_block_size, output_block_size, |
529 filter.get(), &output)); | 551 filter.get(), &output)); |
530 } | 552 } |
531 | 553 |
532 TEST_F(SdchFilterTest, NoDecodeFileColon) { | 554 TEST_F(SdchFilterTest, NoDecodeFileColon) { |
533 // Construct a valid SDCH dictionary from a VCDIFF dictionary. | 555 // Construct a valid SDCH dictionary from a VCDIFF dictionary. |
534 const std::string kSampleDomain = "sdchtest.com"; | 556 const std::string kSampleDomain = "sdchtest.com"; |
535 std::string dictionary(NewSdchDictionary(kSampleDomain)); | 557 std::string dictionary(NewSdchDictionary(kSampleDomain)); |
536 | 558 |
537 std::string url_string = "http://" + kSampleDomain; | 559 std::string url_string = "http://" + kSampleDomain; |
538 | 560 |
539 GURL url(url_string); | 561 GURL url(url_string); |
540 EXPECT_TRUE(AddSdchDictionary(dictionary, url)); | 562 EXPECT_TRUE(AddSdchDictionary(dictionary, url)); |
541 | 563 |
542 std::string compressed(NewSdchCompressedData(dictionary)); | 564 std::string compressed(NewSdchCompressedData(dictionary)); |
543 | 565 |
544 std::vector<Filter::FilterType> filter_types; | 566 std::vector<Filter::FilterType> filter_types; |
545 filter_types.push_back(Filter::FILTER_TYPE_SDCH); | 567 filter_types.push_back(Filter::FILTER_TYPE_SDCH); |
546 | 568 |
547 filter_context()->SetURL(GURL("file://" + kSampleDomain)); | 569 SetupFilterContextWithGURL(GURL("file://" + kSampleDomain)); |
548 scoped_ptr<Filter> filter(Filter::Factory(filter_types, *filter_context())); | 570 scoped_ptr<Filter> filter(Filter::Factory(filter_types, *filter_context())); |
549 | 571 |
550 const size_t feed_block_size(100); | 572 const size_t feed_block_size(100); |
551 const size_t output_block_size(100); | 573 const size_t output_block_size(100); |
552 std::string output; | 574 std::string output; |
553 | 575 |
554 EXPECT_FALSE(FilterTestData(compressed, feed_block_size, output_block_size, | 576 EXPECT_FALSE(FilterTestData(compressed, feed_block_size, output_block_size, |
555 filter.get(), &output)); | 577 filter.get(), &output)); |
556 } | 578 } |
557 | 579 |
558 TEST_F(SdchFilterTest, NoDecodeAboutColon) { | 580 TEST_F(SdchFilterTest, NoDecodeAboutColon) { |
559 // Construct a valid SDCH dictionary from a VCDIFF dictionary. | 581 // Construct a valid SDCH dictionary from a VCDIFF dictionary. |
560 const std::string kSampleDomain = "sdchtest.com"; | 582 const std::string kSampleDomain = "sdchtest.com"; |
561 std::string dictionary(NewSdchDictionary(kSampleDomain)); | 583 std::string dictionary(NewSdchDictionary(kSampleDomain)); |
562 | 584 |
563 std::string url_string = "http://" + kSampleDomain; | 585 std::string url_string = "http://" + kSampleDomain; |
564 | 586 |
565 GURL url(url_string); | 587 GURL url(url_string); |
566 EXPECT_TRUE(AddSdchDictionary(dictionary, url)); | 588 EXPECT_TRUE(AddSdchDictionary(dictionary, url)); |
567 | 589 |
568 std::string compressed(NewSdchCompressedData(dictionary)); | 590 std::string compressed(NewSdchCompressedData(dictionary)); |
569 | 591 |
570 std::vector<Filter::FilterType> filter_types; | 592 std::vector<Filter::FilterType> filter_types; |
571 filter_types.push_back(Filter::FILTER_TYPE_SDCH); | 593 filter_types.push_back(Filter::FILTER_TYPE_SDCH); |
572 | 594 |
573 filter_context()->SetURL(GURL("about://" + kSampleDomain)); | 595 SetupFilterContextWithGURL(GURL("about://" + kSampleDomain)); |
574 scoped_ptr<Filter> filter(Filter::Factory(filter_types, *filter_context())); | 596 scoped_ptr<Filter> filter(Filter::Factory(filter_types, *filter_context())); |
575 | 597 |
576 const size_t feed_block_size(100); | 598 const size_t feed_block_size(100); |
577 const size_t output_block_size(100); | 599 const size_t output_block_size(100); |
578 std::string output; | 600 std::string output; |
579 | 601 |
580 EXPECT_FALSE(FilterTestData(compressed, feed_block_size, output_block_size, | 602 EXPECT_FALSE(FilterTestData(compressed, feed_block_size, output_block_size, |
581 filter.get(), &output)); | 603 filter.get(), &output)); |
582 } | 604 } |
583 | 605 |
584 TEST_F(SdchFilterTest, NoDecodeJavaScript) { | 606 TEST_F(SdchFilterTest, NoDecodeJavaScript) { |
585 // Construct a valid SDCH dictionary from a VCDIFF dictionary. | 607 // Construct a valid SDCH dictionary from a VCDIFF dictionary. |
586 const std::string kSampleDomain = "sdchtest.com"; | 608 const std::string kSampleDomain = "sdchtest.com"; |
587 std::string dictionary(NewSdchDictionary(kSampleDomain)); | 609 std::string dictionary(NewSdchDictionary(kSampleDomain)); |
588 | 610 |
589 std::string url_string = "http://" + kSampleDomain; | 611 std::string url_string = "http://" + kSampleDomain; |
590 | 612 |
591 GURL url(url_string); | 613 GURL url(url_string); |
592 EXPECT_TRUE(AddSdchDictionary(dictionary, url)); | 614 EXPECT_TRUE(AddSdchDictionary(dictionary, url)); |
593 | 615 |
594 std::string compressed(NewSdchCompressedData(dictionary)); | 616 std::string compressed(NewSdchCompressedData(dictionary)); |
595 | 617 |
596 std::vector<Filter::FilterType> filter_types; | 618 std::vector<Filter::FilterType> filter_types; |
597 filter_types.push_back(Filter::FILTER_TYPE_SDCH); | 619 filter_types.push_back(Filter::FILTER_TYPE_SDCH); |
598 | 620 |
599 filter_context()->SetURL(GURL("javascript://" + kSampleDomain)); | 621 SetupFilterContextWithGURL(GURL("javascript://" + kSampleDomain)); |
600 scoped_ptr<Filter> filter(Filter::Factory(filter_types, *filter_context())); | 622 scoped_ptr<Filter> filter(Filter::Factory(filter_types, *filter_context())); |
601 | 623 |
602 const size_t feed_block_size(100); | 624 const size_t feed_block_size(100); |
603 const size_t output_block_size(100); | 625 const size_t output_block_size(100); |
604 std::string output; | 626 std::string output; |
605 | 627 |
606 EXPECT_FALSE(FilterTestData(compressed, feed_block_size, output_block_size, | 628 EXPECT_FALSE(FilterTestData(compressed, feed_block_size, output_block_size, |
607 filter.get(), &output)); | 629 filter.get(), &output)); |
608 } | 630 } |
609 | 631 |
610 TEST_F(SdchFilterTest, CanStillDecodeHttp) { | 632 TEST_F(SdchFilterTest, CanStillDecodeHttp) { |
611 // Construct a valid SDCH dictionary from a VCDIFF dictionary. | 633 // Construct a valid SDCH dictionary from a VCDIFF dictionary. |
612 const std::string kSampleDomain = "sdchtest.com"; | 634 const std::string kSampleDomain = "sdchtest.com"; |
613 std::string dictionary(NewSdchDictionary(kSampleDomain)); | 635 std::string dictionary(NewSdchDictionary(kSampleDomain)); |
614 | 636 |
615 std::string url_string = "http://" + kSampleDomain; | 637 std::string url_string = "http://" + kSampleDomain; |
616 | 638 |
617 GURL url(url_string); | 639 GURL url(url_string); |
618 EXPECT_TRUE(AddSdchDictionary(dictionary, url)); | 640 EXPECT_TRUE(AddSdchDictionary(dictionary, url)); |
619 | 641 |
620 std::string compressed(NewSdchCompressedData(dictionary)); | 642 std::string compressed(NewSdchCompressedData(dictionary)); |
621 | 643 |
622 std::vector<Filter::FilterType> filter_types; | 644 std::vector<Filter::FilterType> filter_types; |
623 filter_types.push_back(Filter::FILTER_TYPE_SDCH); | 645 filter_types.push_back(Filter::FILTER_TYPE_SDCH); |
624 | 646 |
625 filter_context()->SetURL(GURL("http://" + kSampleDomain)); | 647 SetupFilterContextWithGURL(GURL("http://" + kSampleDomain)); |
626 scoped_ptr<Filter> filter(Filter::Factory(filter_types, *filter_context())); | 648 scoped_ptr<Filter> filter(Filter::Factory(filter_types, *filter_context())); |
627 | 649 |
628 const size_t feed_block_size(100); | 650 const size_t feed_block_size(100); |
629 const size_t output_block_size(100); | 651 const size_t output_block_size(100); |
630 std::string output; | 652 std::string output; |
631 | 653 |
632 EXPECT_TRUE(FilterTestData(compressed, feed_block_size, output_block_size, | 654 EXPECT_TRUE(FilterTestData(compressed, feed_block_size, output_block_size, |
633 filter.get(), &output)); | 655 filter.get(), &output)); |
634 } | 656 } |
635 | 657 |
636 TEST_F(SdchFilterTest, CrossDomainDictionaryUse) { | 658 TEST_F(SdchFilterTest, CrossDomainDictionaryUse) { |
637 // Construct a valid SDCH dictionary from a VCDIFF dictionary. | 659 // Construct a valid SDCH dictionary from a VCDIFF dictionary. |
638 const std::string kSampleDomain = "sdchtest.com"; | 660 const std::string kSampleDomain = "sdchtest.com"; |
639 std::string dictionary(NewSdchDictionary(kSampleDomain)); | 661 std::string dictionary(NewSdchDictionary(kSampleDomain)); |
640 | 662 |
641 std::string url_string = "http://" + kSampleDomain; | 663 std::string url_string = "http://" + kSampleDomain; |
642 | 664 |
643 GURL url(url_string); | 665 GURL url(url_string); |
644 EXPECT_TRUE(AddSdchDictionary(dictionary, url)); | 666 EXPECT_TRUE(AddSdchDictionary(dictionary, url)); |
645 | 667 |
646 std::string compressed(NewSdchCompressedData(dictionary)); | 668 std::string compressed(NewSdchCompressedData(dictionary)); |
647 | 669 |
648 std::vector<Filter::FilterType> filter_types; | 670 std::vector<Filter::FilterType> filter_types; |
649 filter_types.push_back(Filter::FILTER_TYPE_SDCH); | 671 filter_types.push_back(Filter::FILTER_TYPE_SDCH); |
650 | 672 |
651 // Decode with content arriving from the "wrong" domain. | 673 // Decode with content arriving from the "wrong" domain. |
652 // This tests SdchManager::CanSet(). | 674 // This tests SdchManager::CanSet(). |
653 GURL wrong_domain_url("http://www.wrongdomain.com"); | 675 GURL wrong_domain_url("http://www.wrongdomain.com"); |
654 filter_context()->SetURL(wrong_domain_url); | 676 SetupFilterContextWithGURL(wrong_domain_url); |
655 scoped_ptr<Filter> filter(Filter::Factory(filter_types, *filter_context())); | 677 scoped_ptr<Filter> filter(Filter::Factory(filter_types, *filter_context())); |
656 | 678 |
657 size_t feed_block_size = 100; | 679 size_t feed_block_size = 100; |
658 size_t output_block_size = 100; | 680 size_t output_block_size = 100; |
659 std::string output; | 681 std::string output; |
660 EXPECT_FALSE(FilterTestData(compressed, feed_block_size, output_block_size, | 682 EXPECT_FALSE(FilterTestData(compressed, feed_block_size, output_block_size, |
661 filter.get(), &output)); | 683 filter.get(), &output)); |
662 EXPECT_EQ(output.size(), 0u); // No output written. | 684 EXPECT_EQ(output.size(), 0u); // No output written. |
663 | 685 |
664 EXPECT_EQ(SDCH_OK, sdch_manager_->IsInSupportedDomain(GURL(url_string))); | 686 EXPECT_EQ(SDCH_OK, sdch_manager_->IsInSupportedDomain(GURL(url_string))); |
(...skipping 24 matching lines...) Expand all Loading... |
689 dictionary_with_path.append(dictionary); | 711 dictionary_with_path.append(dictionary); |
690 GURL url2(url_string + path); | 712 GURL url2(url_string + path); |
691 EXPECT_TRUE(AddSdchDictionary(dictionary_with_path, url2)); | 713 EXPECT_TRUE(AddSdchDictionary(dictionary_with_path, url2)); |
692 | 714 |
693 std::string compressed_for_path(NewSdchCompressedData(dictionary_with_path)); | 715 std::string compressed_for_path(NewSdchCompressedData(dictionary_with_path)); |
694 | 716 |
695 std::vector<Filter::FilterType> filter_types; | 717 std::vector<Filter::FilterType> filter_types; |
696 filter_types.push_back(Filter::FILTER_TYPE_SDCH); | 718 filter_types.push_back(Filter::FILTER_TYPE_SDCH); |
697 | 719 |
698 // Test decode the path data, arriving from a valid path. | 720 // Test decode the path data, arriving from a valid path. |
699 filter_context()->SetURL(GURL(url_string + path)); | 721 SetupFilterContextWithGURL(GURL(url_string + path)); |
700 scoped_ptr<Filter> filter(Filter::Factory(filter_types, *filter_context())); | 722 scoped_ptr<Filter> filter(Filter::Factory(filter_types, *filter_context())); |
701 | 723 |
702 size_t feed_block_size = 100; | 724 size_t feed_block_size = 100; |
703 size_t output_block_size = 100; | 725 size_t output_block_size = 100; |
704 std::string output; | 726 std::string output; |
705 | 727 |
706 EXPECT_TRUE(FilterTestData(compressed_for_path, feed_block_size, | 728 EXPECT_TRUE(FilterTestData(compressed_for_path, feed_block_size, |
707 output_block_size, filter.get(), &output)); | 729 output_block_size, filter.get(), &output)); |
708 EXPECT_EQ(output, expanded_); | 730 EXPECT_EQ(output, expanded_); |
709 | 731 |
710 // Test decode the path data, arriving from a invalid path. | 732 // Test decode the path data, arriving from a invalid path. |
711 filter_context()->SetURL(GURL(url_string)); | 733 SetupFilterContextWithGURL(GURL(url_string)); |
712 filter.reset(Filter::Factory(filter_types, *filter_context())); | 734 filter.reset(Filter::Factory(filter_types, *filter_context())); |
713 | 735 |
714 feed_block_size = 100; | 736 feed_block_size = 100; |
715 output_block_size = 100; | 737 output_block_size = 100; |
716 output.clear(); | 738 output.clear(); |
717 EXPECT_FALSE(FilterTestData(compressed_for_path, feed_block_size, | 739 EXPECT_FALSE(FilterTestData(compressed_for_path, feed_block_size, |
718 output_block_size, filter.get(), &output)); | 740 output_block_size, filter.get(), &output)); |
719 EXPECT_EQ(output.size(), 0u); // No output written. | 741 EXPECT_EQ(output.size(), 0u); // No output written. |
720 | 742 |
721 EXPECT_EQ(SDCH_DOMAIN_BLACKLIST_INCLUDES_TARGET, | 743 EXPECT_EQ(SDCH_DOMAIN_BLACKLIST_INCLUDES_TARGET, |
(...skipping 24 matching lines...) Expand all Loading... |
746 dictionary_with_port.append(dictionary); | 768 dictionary_with_port.append(dictionary); |
747 EXPECT_TRUE(AddSdchDictionary(dictionary_with_port, | 769 EXPECT_TRUE(AddSdchDictionary(dictionary_with_port, |
748 GURL(url_string + ":" + port))); | 770 GURL(url_string + ":" + port))); |
749 | 771 |
750 std::string compressed_for_port(NewSdchCompressedData(dictionary_with_port)); | 772 std::string compressed_for_port(NewSdchCompressedData(dictionary_with_port)); |
751 | 773 |
752 std::vector<Filter::FilterType> filter_types; | 774 std::vector<Filter::FilterType> filter_types; |
753 filter_types.push_back(Filter::FILTER_TYPE_SDCH); | 775 filter_types.push_back(Filter::FILTER_TYPE_SDCH); |
754 | 776 |
755 // Test decode the port data, arriving from a valid port. | 777 // Test decode the port data, arriving from a valid port. |
756 filter_context()->SetURL(GURL(url_string + ":" + port)); | 778 SetupFilterContextWithGURL(GURL(url_string + ":" + port)); |
757 scoped_ptr<Filter> filter(Filter::Factory(filter_types, *filter_context())); | 779 scoped_ptr<Filter> filter(Filter::Factory(filter_types, *filter_context())); |
758 | 780 |
759 size_t feed_block_size = 100; | 781 size_t feed_block_size = 100; |
760 size_t output_block_size = 100; | 782 size_t output_block_size = 100; |
761 std::string output; | 783 std::string output; |
762 EXPECT_TRUE(FilterTestData(compressed_for_port, feed_block_size, | 784 EXPECT_TRUE(FilterTestData(compressed_for_port, feed_block_size, |
763 output_block_size, filter.get(), &output)); | 785 output_block_size, filter.get(), &output)); |
764 EXPECT_EQ(output, expanded_); | 786 EXPECT_EQ(output, expanded_); |
765 | 787 |
766 // Test decode the port data, arriving from a valid (default) port. | 788 // Test decode the port data, arriving from a valid (default) port. |
767 filter_context()->SetURL(GURL(url_string)); // Default port. | 789 SetupFilterContextWithGURL(GURL(url_string)); // Default port. |
768 filter.reset(Filter::Factory(filter_types, *filter_context())); | 790 filter.reset(Filter::Factory(filter_types, *filter_context())); |
769 | 791 |
770 feed_block_size = 100; | 792 feed_block_size = 100; |
771 output_block_size = 100; | 793 output_block_size = 100; |
772 output.clear(); | 794 output.clear(); |
773 EXPECT_TRUE(FilterTestData(compressed_for_port, feed_block_size, | 795 EXPECT_TRUE(FilterTestData(compressed_for_port, feed_block_size, |
774 output_block_size, filter.get(), &output)); | 796 output_block_size, filter.get(), &output)); |
775 EXPECT_EQ(output, expanded_); | 797 EXPECT_EQ(output, expanded_); |
776 | 798 |
777 // Test decode the port data, arriving from a invalid port. | 799 // Test decode the port data, arriving from a invalid port. |
778 filter_context()->SetURL(GURL(url_string + ":" + port + "1")); | 800 SetupFilterContextWithGURL(GURL(url_string + ":" + port + "1")); |
779 filter.reset(Filter::Factory(filter_types, *filter_context())); | 801 filter.reset(Filter::Factory(filter_types, *filter_context())); |
780 | 802 |
781 feed_block_size = 100; | 803 feed_block_size = 100; |
782 output_block_size = 100; | 804 output_block_size = 100; |
783 output.clear(); | 805 output.clear(); |
784 EXPECT_FALSE(FilterTestData(compressed_for_port, feed_block_size, | 806 EXPECT_FALSE(FilterTestData(compressed_for_port, feed_block_size, |
785 output_block_size, filter.get(), &output)); | 807 output_block_size, filter.get(), &output)); |
786 EXPECT_EQ(output.size(), 0u); // No output written. | 808 EXPECT_EQ(output.size(), 0u); // No output written. |
787 | 809 |
788 EXPECT_EQ(SDCH_DOMAIN_BLACKLIST_INCLUDES_TARGET, | 810 EXPECT_EQ(SDCH_DOMAIN_BLACKLIST_INCLUDES_TARGET, |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
849 | 871 |
850 class SdchFilterChainingTest { | 872 class SdchFilterChainingTest { |
851 public: | 873 public: |
852 static Filter* Factory(const std::vector<Filter::FilterType>& types, | 874 static Filter* Factory(const std::vector<Filter::FilterType>& types, |
853 const FilterContext& context, int size) { | 875 const FilterContext& context, int size) { |
854 return Filter::FactoryForTests(types, context, size); | 876 return Filter::FactoryForTests(types, context, size); |
855 } | 877 } |
856 }; | 878 }; |
857 | 879 |
858 // Test that filters can be cascaded (chained) so that the output of one filter | 880 // Test that filters can be cascaded (chained) so that the output of one filter |
859 // is processed by the next one. This is most critical for SDCH, which is | 881 // is processed by the next one. This is most critical for SDCH, which is |
860 // routinely followed by gzip (during encoding). The filter we'll test for will | 882 // routinely followed by gzip (during encoding). The filter we'll test for will |
861 // do the gzip decoding first, and then decode the SDCH content. | 883 // do the gzip decoding first, and then decode the SDCH content. |
862 TEST_F(SdchFilterTest, FilterChaining) { | 884 TEST_F(SdchFilterTest, FilterChaining) { |
863 // Construct a valid SDCH dictionary from a VCDIFF dictionary. | 885 // Construct a valid SDCH dictionary from a VCDIFF dictionary. |
864 const std::string kSampleDomain = "sdchtest.com"; | 886 const std::string kSampleDomain = "sdchtest.com"; |
865 std::string dictionary(NewSdchDictionary(kSampleDomain)); | 887 std::string dictionary(NewSdchDictionary(kSampleDomain)); |
866 | 888 |
867 std::string url_string = "http://" + kSampleDomain; | 889 std::string url_string = "http://" + kSampleDomain; |
868 | 890 |
869 GURL url(url_string); | 891 GURL url(url_string); |
870 EXPECT_TRUE(AddSdchDictionary(dictionary, url)); | 892 EXPECT_TRUE(AddSdchDictionary(dictionary, url)); |
871 | 893 |
872 std::string sdch_compressed(NewSdchCompressedData(dictionary)); | 894 std::string sdch_compressed(NewSdchCompressedData(dictionary)); |
873 | 895 |
874 // Use Gzip to compress the sdch sdch_compressed data. | 896 // Use Gzip to compress the sdch sdch_compressed data. |
875 std::string gzip_compressed_sdch = gzip_compress(sdch_compressed); | 897 std::string gzip_compressed_sdch = gzip_compress(sdch_compressed); |
876 | 898 |
877 // Construct a chained filter. | 899 // Construct a chained filter. |
878 std::vector<Filter::FilterType> filter_types; | 900 std::vector<Filter::FilterType> filter_types; |
879 filter_types.push_back(Filter::FILTER_TYPE_SDCH); | 901 filter_types.push_back(Filter::FILTER_TYPE_SDCH); |
880 filter_types.push_back(Filter::FILTER_TYPE_GZIP); | 902 filter_types.push_back(Filter::FILTER_TYPE_GZIP); |
881 | 903 |
882 // First try with a large buffer (larger than test input, or compressed data). | 904 // First try with a large buffer (larger than test input, or compressed data). |
883 const size_t kLargeInputBufferSize(1000); // Used internally in filters. | 905 const size_t kLargeInputBufferSize(1000); // Used internally in filters. |
884 CHECK_GT(kLargeInputBufferSize, gzip_compressed_sdch.size()); | 906 CHECK_GT(kLargeInputBufferSize, gzip_compressed_sdch.size()); |
885 CHECK_GT(kLargeInputBufferSize, sdch_compressed.size()); | 907 CHECK_GT(kLargeInputBufferSize, sdch_compressed.size()); |
886 CHECK_GT(kLargeInputBufferSize, expanded_.size()); | 908 CHECK_GT(kLargeInputBufferSize, expanded_.size()); |
887 filter_context()->SetURL(url); | 909 SetupFilterContextWithGURL(url); |
888 scoped_ptr<Filter> filter( | 910 scoped_ptr<Filter> filter( |
889 SdchFilterChainingTest::Factory(filter_types, *filter_context(), | 911 SdchFilterChainingTest::Factory(filter_types, *filter_context(), |
890 kLargeInputBufferSize)); | 912 kLargeInputBufferSize)); |
891 EXPECT_EQ(static_cast<int>(kLargeInputBufferSize), | 913 EXPECT_EQ(static_cast<int>(kLargeInputBufferSize), |
892 filter->stream_buffer_size()); | 914 filter->stream_buffer_size()); |
893 | 915 |
894 // Verify that chained filter is waiting for data. | 916 // Verify that chained filter is waiting for data. |
895 char tiny_output_buffer[10]; | 917 char tiny_output_buffer[10]; |
896 int tiny_output_size = sizeof(tiny_output_buffer); | 918 int tiny_output_size = sizeof(tiny_output_buffer); |
897 EXPECT_EQ(Filter::FILTER_NEED_MORE_DATA, | 919 EXPECT_EQ(Filter::FILTER_NEED_MORE_DATA, |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
955 | 977 |
956 // Use Gzip to compress the sdch sdch_compressed data. | 978 // Use Gzip to compress the sdch sdch_compressed data. |
957 std::string gzip_compressed_sdch = gzip_compress(sdch_compressed); | 979 std::string gzip_compressed_sdch = gzip_compress(sdch_compressed); |
958 | 980 |
959 // Only claim to have sdch content, but really use the gzipped sdch content. | 981 // Only claim to have sdch content, but really use the gzipped sdch content. |
960 // System should automatically add the missing (optional) gzip. | 982 // System should automatically add the missing (optional) gzip. |
961 std::vector<Filter::FilterType> filter_types; | 983 std::vector<Filter::FilterType> filter_types; |
962 filter_types.push_back(Filter::FILTER_TYPE_SDCH); | 984 filter_types.push_back(Filter::FILTER_TYPE_SDCH); |
963 | 985 |
964 filter_context()->SetMimeType("anything/mime"); | 986 filter_context()->SetMimeType("anything/mime"); |
965 filter_context()->SetSdchResponse(true); | 987 SetupFilterContextWithGURL(url); |
| 988 |
966 Filter::FixupEncodingTypes(*filter_context(), &filter_types); | 989 Filter::FixupEncodingTypes(*filter_context(), &filter_types); |
967 ASSERT_EQ(filter_types.size(), 2u); | 990 ASSERT_EQ(filter_types.size(), 2u); |
968 EXPECT_EQ(filter_types[0], Filter::FILTER_TYPE_SDCH); | 991 EXPECT_EQ(filter_types[0], Filter::FILTER_TYPE_SDCH); |
969 EXPECT_EQ(filter_types[1], Filter::FILTER_TYPE_GZIP_HELPING_SDCH); | 992 EXPECT_EQ(filter_types[1], Filter::FILTER_TYPE_GZIP_HELPING_SDCH); |
970 | 993 |
971 // First try with a large buffer (larger than test input, or compressed data). | 994 // First try with a large buffer (larger than test input, or compressed data). |
972 filter_context()->SetURL(url); | 995 scoped_ptr<Filter> filter(Filter::Factory(filter_types, *filter_context())); |
973 scoped_ptr<Filter> filter(Filter::Factory(filter_types, *filter_context())); | |
974 | |
975 | 996 |
976 // Verify that chained filter is waiting for data. | 997 // Verify that chained filter is waiting for data. |
977 char tiny_output_buffer[10]; | 998 char tiny_output_buffer[10]; |
978 int tiny_output_size = sizeof(tiny_output_buffer); | 999 int tiny_output_size = sizeof(tiny_output_buffer); |
979 EXPECT_EQ(Filter::FILTER_NEED_MORE_DATA, | 1000 EXPECT_EQ(Filter::FILTER_NEED_MORE_DATA, |
980 filter->ReadData(tiny_output_buffer, &tiny_output_size)); | 1001 filter->ReadData(tiny_output_buffer, &tiny_output_size)); |
981 | 1002 |
982 size_t feed_block_size = 100; | 1003 size_t feed_block_size = 100; |
983 size_t output_block_size = 100; | 1004 size_t output_block_size = 100; |
984 std::string output; | 1005 std::string output; |
(...skipping 28 matching lines...) Expand all Loading... |
1013 std::string gzip_compressed_sdch = gzip_compress(sdch_compressed); | 1034 std::string gzip_compressed_sdch = gzip_compress(sdch_compressed); |
1014 | 1035 |
1015 // Some proxies strip the content encoding statement down to a mere gzip, but | 1036 // Some proxies strip the content encoding statement down to a mere gzip, but |
1016 // pass through the original content (with full sdch,gzip encoding). | 1037 // pass through the original content (with full sdch,gzip encoding). |
1017 // Only claim to have gzip content, but really use the gzipped sdch content. | 1038 // Only claim to have gzip content, but really use the gzipped sdch content. |
1018 // System should automatically add the missing (optional) sdch. | 1039 // System should automatically add the missing (optional) sdch. |
1019 std::vector<Filter::FilterType> filter_types; | 1040 std::vector<Filter::FilterType> filter_types; |
1020 filter_types.push_back(Filter::FILTER_TYPE_GZIP); | 1041 filter_types.push_back(Filter::FILTER_TYPE_GZIP); |
1021 | 1042 |
1022 filter_context()->SetMimeType("anything/mime"); | 1043 filter_context()->SetMimeType("anything/mime"); |
1023 filter_context()->SetSdchResponse(true); | 1044 SetupFilterContextWithGURL(url); |
1024 Filter::FixupEncodingTypes(*filter_context(), &filter_types); | 1045 Filter::FixupEncodingTypes(*filter_context(), &filter_types); |
1025 ASSERT_EQ(filter_types.size(), 3u); | 1046 ASSERT_EQ(filter_types.size(), 3u); |
1026 EXPECT_EQ(filter_types[0], Filter::FILTER_TYPE_SDCH_POSSIBLE); | 1047 EXPECT_EQ(filter_types[0], Filter::FILTER_TYPE_SDCH_POSSIBLE); |
1027 EXPECT_EQ(filter_types[1], Filter::FILTER_TYPE_GZIP_HELPING_SDCH); | 1048 EXPECT_EQ(filter_types[1], Filter::FILTER_TYPE_GZIP_HELPING_SDCH); |
1028 EXPECT_EQ(filter_types[2], Filter::FILTER_TYPE_GZIP); | 1049 EXPECT_EQ(filter_types[2], Filter::FILTER_TYPE_GZIP); |
1029 | 1050 |
1030 // First try with a large buffer (larger than test input, or compressed data). | 1051 // First try with a large buffer (larger than test input, or compressed data). |
1031 filter_context()->SetURL(url); | |
1032 scoped_ptr<Filter> filter(Filter::Factory(filter_types, *filter_context())); | 1052 scoped_ptr<Filter> filter(Filter::Factory(filter_types, *filter_context())); |
1033 | 1053 |
1034 | |
1035 // Verify that chained filter is waiting for data. | 1054 // Verify that chained filter is waiting for data. |
1036 char tiny_output_buffer[10]; | 1055 char tiny_output_buffer[10]; |
1037 int tiny_output_size = sizeof(tiny_output_buffer); | 1056 int tiny_output_size = sizeof(tiny_output_buffer); |
1038 EXPECT_EQ(Filter::FILTER_NEED_MORE_DATA, | 1057 EXPECT_EQ(Filter::FILTER_NEED_MORE_DATA, |
1039 filter->ReadData(tiny_output_buffer, &tiny_output_size)); | 1058 filter->ReadData(tiny_output_buffer, &tiny_output_size)); |
1040 | 1059 |
1041 size_t feed_block_size = 100; | 1060 size_t feed_block_size = 100; |
1042 size_t output_block_size = 100; | 1061 size_t output_block_size = 100; |
1043 std::string output; | 1062 std::string output; |
1044 EXPECT_TRUE(FilterTestData(gzip_compressed_sdch, feed_block_size, | 1063 EXPECT_TRUE(FilterTestData(gzip_compressed_sdch, feed_block_size, |
(...skipping 25 matching lines...) Expand all Loading... |
1070 | 1089 |
1071 // Use Gzip to compress the sdch sdch_compressed data. | 1090 // Use Gzip to compress the sdch sdch_compressed data. |
1072 std::string gzip_compressed_sdch = gzip_compress(sdch_compressed); | 1091 std::string gzip_compressed_sdch = gzip_compress(sdch_compressed); |
1073 | 1092 |
1074 // Only claim to have non-encoded content, but really use the gzipped sdch | 1093 // Only claim to have non-encoded content, but really use the gzipped sdch |
1075 // content. | 1094 // content. |
1076 // System should automatically add the missing (optional) sdch,gzip. | 1095 // System should automatically add the missing (optional) sdch,gzip. |
1077 std::vector<Filter::FilterType> filter_types; | 1096 std::vector<Filter::FilterType> filter_types; |
1078 | 1097 |
1079 filter_context()->SetMimeType("anything/mime"); | 1098 filter_context()->SetMimeType("anything/mime"); |
1080 filter_context()->SetSdchResponse(true); | 1099 SetupFilterContextWithGURL(url); |
1081 Filter::FixupEncodingTypes(*filter_context(), &filter_types); | 1100 Filter::FixupEncodingTypes(*filter_context(), &filter_types); |
1082 ASSERT_EQ(filter_types.size(), 2u); | 1101 ASSERT_EQ(filter_types.size(), 2u); |
1083 EXPECT_EQ(filter_types[0], Filter::FILTER_TYPE_SDCH_POSSIBLE); | 1102 EXPECT_EQ(filter_types[0], Filter::FILTER_TYPE_SDCH_POSSIBLE); |
1084 EXPECT_EQ(filter_types[1], Filter::FILTER_TYPE_GZIP_HELPING_SDCH); | 1103 EXPECT_EQ(filter_types[1], Filter::FILTER_TYPE_GZIP_HELPING_SDCH); |
1085 | 1104 |
1086 // First try with a large buffer (larger than test input, or compressed data). | 1105 // First try with a large buffer (larger than test input, or compressed data). |
1087 filter_context()->SetURL(url); | |
1088 scoped_ptr<Filter> filter(Filter::Factory(filter_types, *filter_context())); | 1106 scoped_ptr<Filter> filter(Filter::Factory(filter_types, *filter_context())); |
1089 | 1107 |
1090 | |
1091 // Verify that chained filter is waiting for data. | 1108 // Verify that chained filter is waiting for data. |
1092 char tiny_output_buffer[10]; | 1109 char tiny_output_buffer[10]; |
1093 int tiny_output_size = sizeof(tiny_output_buffer); | 1110 int tiny_output_size = sizeof(tiny_output_buffer); |
1094 EXPECT_EQ(Filter::FILTER_NEED_MORE_DATA, | 1111 EXPECT_EQ(Filter::FILTER_NEED_MORE_DATA, |
1095 filter->ReadData(tiny_output_buffer, &tiny_output_size)); | 1112 filter->ReadData(tiny_output_buffer, &tiny_output_size)); |
1096 | 1113 |
1097 size_t feed_block_size = 100; | 1114 size_t feed_block_size = 100; |
1098 size_t output_block_size = 100; | 1115 size_t output_block_size = 100; |
1099 std::string output; | 1116 std::string output; |
1100 EXPECT_TRUE(FilterTestData(gzip_compressed_sdch, feed_block_size, | 1117 EXPECT_TRUE(FilterTestData(gzip_compressed_sdch, feed_block_size, |
(...skipping 30 matching lines...) Expand all Loading... |
1131 std::string double_gzip_compressed_sdch = gzip_compress(gzip_compress( | 1148 std::string double_gzip_compressed_sdch = gzip_compress(gzip_compress( |
1132 sdch_compressed)); | 1149 sdch_compressed)); |
1133 | 1150 |
1134 // Only claim to have gzip content, but really use the double gzipped sdch | 1151 // Only claim to have gzip content, but really use the double gzipped sdch |
1135 // content. | 1152 // content. |
1136 // System should automatically add the missing (optional) sdch, gzip decoders. | 1153 // System should automatically add the missing (optional) sdch, gzip decoders. |
1137 std::vector<Filter::FilterType> filter_types; | 1154 std::vector<Filter::FilterType> filter_types; |
1138 filter_types.push_back(Filter::FILTER_TYPE_GZIP); | 1155 filter_types.push_back(Filter::FILTER_TYPE_GZIP); |
1139 | 1156 |
1140 filter_context()->SetMimeType("anything/mime"); | 1157 filter_context()->SetMimeType("anything/mime"); |
1141 filter_context()->SetSdchResponse(true); | 1158 SetupFilterContextWithGURL(url); |
1142 Filter::FixupEncodingTypes(*filter_context(), &filter_types); | 1159 Filter::FixupEncodingTypes(*filter_context(), &filter_types); |
1143 ASSERT_EQ(filter_types.size(), 3u); | 1160 ASSERT_EQ(filter_types.size(), 3u); |
1144 EXPECT_EQ(filter_types[0], Filter::FILTER_TYPE_SDCH_POSSIBLE); | 1161 EXPECT_EQ(filter_types[0], Filter::FILTER_TYPE_SDCH_POSSIBLE); |
1145 EXPECT_EQ(filter_types[1], Filter::FILTER_TYPE_GZIP_HELPING_SDCH); | 1162 EXPECT_EQ(filter_types[1], Filter::FILTER_TYPE_GZIP_HELPING_SDCH); |
1146 EXPECT_EQ(filter_types[2], Filter::FILTER_TYPE_GZIP); | 1163 EXPECT_EQ(filter_types[2], Filter::FILTER_TYPE_GZIP); |
1147 | 1164 |
1148 // First try with a large buffer (larger than test input, or compressed data). | 1165 // First try with a large buffer (larger than test input, or compressed data). |
1149 filter_context()->SetURL(url); | |
1150 scoped_ptr<Filter> filter(Filter::Factory(filter_types, *filter_context())); | 1166 scoped_ptr<Filter> filter(Filter::Factory(filter_types, *filter_context())); |
1151 | 1167 |
1152 // Verify that chained filter is waiting for data. | 1168 // Verify that chained filter is waiting for data. |
1153 char tiny_output_buffer[10]; | 1169 char tiny_output_buffer[10]; |
1154 int tiny_output_size = sizeof(tiny_output_buffer); | 1170 int tiny_output_size = sizeof(tiny_output_buffer); |
1155 EXPECT_EQ(Filter::FILTER_NEED_MORE_DATA, | 1171 EXPECT_EQ(Filter::FILTER_NEED_MORE_DATA, |
1156 filter->ReadData(tiny_output_buffer, &tiny_output_size)); | 1172 filter->ReadData(tiny_output_buffer, &tiny_output_size)); |
1157 | 1173 |
1158 size_t feed_block_size = 100; | 1174 size_t feed_block_size = 100; |
1159 size_t output_block_size = 100; | 1175 size_t output_block_size = 100; |
1160 std::string output; | 1176 std::string output; |
1161 EXPECT_TRUE(FilterTestData(double_gzip_compressed_sdch, feed_block_size, | 1177 EXPECT_TRUE(FilterTestData(double_gzip_compressed_sdch, feed_block_size, |
1162 output_block_size, filter.get(), &output)); | 1178 output_block_size, filter.get(), &output)); |
1163 EXPECT_EQ(output, expanded_); | 1179 EXPECT_EQ(output, expanded_); |
1164 | 1180 |
1165 // Next try with a tiny buffer to cover edge effects. | 1181 // Next try with a tiny buffer to cover edge effects. |
1166 filter.reset(Filter::Factory(filter_types, *filter_context())); | 1182 filter.reset(Filter::Factory(filter_types, *filter_context())); |
1167 | 1183 |
1168 feed_block_size = 1; | 1184 feed_block_size = 1; |
1169 output_block_size = 1; | 1185 output_block_size = 1; |
1170 output.clear(); | 1186 output.clear(); |
1171 EXPECT_TRUE(FilterTestData(double_gzip_compressed_sdch, feed_block_size, | 1187 EXPECT_TRUE(FilterTestData(double_gzip_compressed_sdch, feed_block_size, |
1172 output_block_size, filter.get(), &output)); | 1188 output_block_size, filter.get(), &output)); |
1173 EXPECT_EQ(output, expanded_); | 1189 EXPECT_EQ(output, expanded_); |
1174 } | 1190 } |
1175 | 1191 |
| 1192 // Test to make sure we decode properly with an unexpected dictionary. |
| 1193 TEST_F(SdchFilterTest, UnexpectedDictionary) { |
| 1194 // Setup a dictionary, add it to the filter context, and create a filter |
| 1195 // based on that dictionary. |
| 1196 const std::string kSampleDomain = "sdchtest.com"; |
| 1197 std::string dictionary(NewSdchDictionary(kSampleDomain)); |
| 1198 std::string url_string = "http://" + kSampleDomain; |
| 1199 GURL url(url_string); |
| 1200 EXPECT_TRUE(AddSdchDictionary(dictionary, url)); |
| 1201 |
| 1202 SetupFilterContextWithGURL(url); |
| 1203 |
| 1204 std::vector<Filter::FilterType> filter_types; |
| 1205 filter_types.push_back(Filter::FILTER_TYPE_SDCH); |
| 1206 scoped_ptr<Filter> filter(Filter::Factory(filter_types, *filter_context())); |
| 1207 |
| 1208 // Setup another dictionary, expired. Don't add it to the filter context. |
| 1209 // Delete stored dictionaries first to handle platforms which only |
| 1210 // have room for a single dictionary. |
| 1211 sdch_manager_->ClearData(); |
| 1212 std::string expired_dictionary(NewSdchExpiredDictionary(kSampleDomain)); |
| 1213 |
| 1214 // Don't use the Helper function since its insertion check is indeterminate |
| 1215 // for a Max-Age: 0 dictionary. |
| 1216 sdch_manager_->AddSdchDictionary(expired_dictionary, url); |
| 1217 |
| 1218 std::string client_hash; |
| 1219 std::string server_hash; |
| 1220 SdchManager::GenerateHash(expired_dictionary, &client_hash, &server_hash); |
| 1221 |
| 1222 // Make sure Max-Age: 0 shows up as expired. |
| 1223 scoped_ptr<base::SimpleTestClock> clock(new base::SimpleTestClock); |
| 1224 clock->SetNow(base::Time::Now()); |
| 1225 clock->Advance(base::TimeDelta::FromMinutes(5)); |
| 1226 SdchProblemCode problem_code; |
| 1227 scoped_ptr<SdchManager::DictionarySet> hash_set( |
| 1228 sdch_manager_->GetDictionarySetByHash( |
| 1229 url, server_hash, &problem_code).Pass()); |
| 1230 ASSERT_TRUE(hash_set); |
| 1231 ASSERT_EQ(SDCH_OK, problem_code); |
| 1232 |
| 1233 const_cast<SdchManager::Dictionary*>( |
| 1234 hash_set->GetDictionary(server_hash))->SetClockForTesting( |
| 1235 clock.Pass()); |
| 1236 |
| 1237 // Encode output with the second dictionary. |
| 1238 std::string sdch_compressed(NewSdchCompressedData(expired_dictionary)); |
| 1239 |
| 1240 // See if the filter decodes it. |
| 1241 std::string output; |
| 1242 EXPECT_TRUE(FilterTestData(sdch_compressed, 100, 100, filter.get(), &output)); |
| 1243 EXPECT_EQ(expanded_, output); |
| 1244 } |
| 1245 |
1176 } // namespace net | 1246 } // namespace net |
OLD | NEW |