| 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 "base/test/simple_test_clock.h" |
| 14 #include "net/base/io_buffer.h" | 14 #include "net/base/io_buffer.h" |
| 15 #include "net/base/sdch_observer.h" |
| 15 #include "net/filter/mock_filter_context.h" | 16 #include "net/filter/mock_filter_context.h" |
| 16 #include "net/filter/sdch_filter.h" | 17 #include "net/filter/sdch_filter.h" |
| 17 #include "net/url_request/url_request_context.h" | 18 #include "net/url_request/url_request_context.h" |
| 18 #include "net/url_request/url_request_http_job.h" | 19 #include "net/url_request/url_request_http_job.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 20 #include "third_party/zlib/zlib.h" | 21 #include "third_party/zlib/zlib.h" |
| 21 | 22 |
| 22 namespace net { | 23 namespace net { |
| 23 | 24 |
| 24 //------------------------------------------------------------------------------ | 25 //------------------------------------------------------------------------------ |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 61 URLRequestContext* url_request_context = | 62 URLRequestContext* url_request_context = |
| 62 filter_context_->GetModifiableURLRequestContext(); | 63 filter_context_->GetModifiableURLRequestContext(); |
| 63 | 64 |
| 64 url_request_context->set_sdch_manager(sdch_manager_.get()); | 65 url_request_context->set_sdch_manager(sdch_manager_.get()); |
| 65 } | 66 } |
| 66 | 67 |
| 67 // Attempt to add a dictionary to the manager and probe for success or | 68 // Attempt to add a dictionary to the manager and probe for success or |
| 68 // failure. | 69 // failure. |
| 69 bool AddSdchDictionary(const std::string& dictionary_text, | 70 bool AddSdchDictionary(const std::string& dictionary_text, |
| 70 const GURL& gurl) { | 71 const GURL& gurl) { |
| 71 return sdch_manager_->AddSdchDictionary(dictionary_text, gurl) == SDCH_OK; | 72 return sdch_manager_->AddSdchDictionary(dictionary_text, gurl, nullptr) == |
| 73 SDCH_OK; |
| 72 } | 74 } |
| 73 | 75 |
| 74 MockFilterContext* filter_context() { return filter_context_.get(); } | 76 MockFilterContext* filter_context() { return filter_context_.get(); } |
| 75 | 77 |
| 76 // Sets both the GURL and the SDCH response for a filter context. | 78 // Sets both the GURL and the SDCH response for a filter context. |
| 77 void SetupFilterContextWithGURL(GURL url) { | 79 void SetupFilterContextWithGURL(GURL url) { |
| 78 filter_context_->SetURL(url); | 80 filter_context_->SetURL(url); |
| 79 filter_context_->SetSdchResponse( | 81 filter_context_->SetSdchResponse( |
| 80 sdch_manager_->GetDictionarySet(url).Pass()); | 82 sdch_manager_->GetDictionarySet(url).Pass()); |
| 81 } | 83 } |
| (...skipping 11 matching lines...) Expand all Loading... |
| 93 } | 95 } |
| 94 | 96 |
| 95 const std::string test_vcdiff_dictionary_; | 97 const std::string test_vcdiff_dictionary_; |
| 96 const std::string vcdiff_compressed_data_; | 98 const std::string vcdiff_compressed_data_; |
| 97 const std::string expanded_; // Desired final, decompressed data. | 99 const std::string expanded_; // Desired final, decompressed data. |
| 98 | 100 |
| 99 scoped_ptr<SdchManager> sdch_manager_; | 101 scoped_ptr<SdchManager> sdch_manager_; |
| 100 scoped_ptr<MockFilterContext> filter_context_; | 102 scoped_ptr<MockFilterContext> filter_context_; |
| 101 }; | 103 }; |
| 102 | 104 |
| 103 //------------------------------------------------------------------------------ | |
| 104 | |
| 105 | |
| 106 TEST_F(SdchFilterTest, Hashing) { | 105 TEST_F(SdchFilterTest, Hashing) { |
| 107 std::string client_hash, server_hash; | 106 std::string client_hash, server_hash; |
| 108 std::string dictionary("test contents"); | 107 std::string dictionary("test contents"); |
| 109 SdchManager::GenerateHash(dictionary, &client_hash, &server_hash); | 108 SdchManager::GenerateHash(dictionary, &client_hash, &server_hash); |
| 110 | 109 |
| 111 EXPECT_EQ(client_hash, "lMQBjS3P"); | 110 EXPECT_EQ(client_hash, "lMQBjS3P"); |
| 112 EXPECT_EQ(server_hash, "MyciMVll"); | 111 EXPECT_EQ(server_hash, "MyciMVll"); |
| 113 } | 112 } |
| 114 | 113 |
| 115 | |
| 116 //------------------------------------------------------------------------------ | 114 //------------------------------------------------------------------------------ |
| 117 // Provide a generic helper function for trying to filter data. | 115 // Provide a generic helper function for trying to filter data. |
| 118 // This function repeatedly calls the filter to process data, until the entire | 116 // This function repeatedly calls the filter to process data, until the entire |
| 119 // source is consumed. The return value from the filter is appended to output. | 117 // source is consumed. The return value from the filter is appended to output. |
| 120 // This allows us to vary input and output block sizes in order to test for edge | 118 // This allows us to vary input and output block sizes in order to test for edge |
| 121 // effects (boundary effects?) during the filtering process. | 119 // effects (boundary effects?) during the filtering process. |
| 122 // This function provides data to the filter in blocks of no-more-than the | 120 // This function provides data to the filter in blocks of no-more-than the |
| 123 // specified input_block_length. It allows the filter to fill no more than | 121 // specified input_block_length. It allows the filter to fill no more than |
| 124 // output_buffer_length in any one call to proccess (a.k.a., Read) data, and | 122 // output_buffer_length in any one call to proccess (a.k.a., Read) data, and |
| 125 // concatenates all these little output blocks into the singular output string. | 123 // concatenates all these little output blocks into the singular output string. |
| (...skipping 21 matching lines...) Expand all Loading... |
| 147 output->append(output_buffer.get(), buffer_length); | 145 output->append(output_buffer.get(), buffer_length); |
| 148 if (status == Filter::FILTER_ERROR) | 146 if (status == Filter::FILTER_ERROR) |
| 149 return false; | 147 return false; |
| 150 // Callers assume that FILTER_OK with no output buffer means FILTER_DONE. | 148 // Callers assume that FILTER_OK with no output buffer means FILTER_DONE. |
| 151 if (Filter::FILTER_OK == status && 0 == buffer_length) | 149 if (Filter::FILTER_OK == status && 0 == buffer_length) |
| 152 return true; | 150 return true; |
| 153 if (copy_amount == 0 && buffer_length == 0) | 151 if (copy_amount == 0 && buffer_length == 0) |
| 154 return true; | 152 return true; |
| 155 } while (1); | 153 } while (1); |
| 156 } | 154 } |
| 157 //------------------------------------------------------------------------------ | 155 |
| 158 static std::string NewSdchDictionary(const std::string& domain) { | 156 static std::string NewSdchDictionary(const std::string& domain) { |
| 159 std::string dictionary; | 157 std::string dictionary; |
| 160 if (!domain.empty()) { | 158 if (!domain.empty()) { |
| 161 dictionary.append("Domain: "); | 159 dictionary.append("Domain: "); |
| 162 dictionary.append(domain); | 160 dictionary.append(domain); |
| 163 dictionary.append("\n"); | 161 dictionary.append("\n"); |
| 164 } | 162 } |
| 165 dictionary.append("\n"); | 163 dictionary.append("\n"); |
| 166 dictionary.append(kTestVcdiffDictionary, sizeof(kTestVcdiffDictionary) - 1); | 164 dictionary.append(kTestVcdiffDictionary, sizeof(kTestVcdiffDictionary) - 1); |
| 167 return dictionary; | 165 return dictionary; |
| 168 } | 166 } |
| 169 | 167 |
| 170 static std::string NewSdchExpiredDictionary(const std::string& domain) { | 168 static std::string NewSdchExpiredDictionary(const std::string& domain) { |
| 171 std::string dictionary; | 169 std::string dictionary; |
| 172 if (!domain.empty()) { | 170 if (!domain.empty()) { |
| 173 dictionary.append("Domain: "); | 171 dictionary.append("Domain: "); |
| 174 dictionary.append(domain); | 172 dictionary.append(domain); |
| 175 dictionary.append("\n"); | 173 dictionary.append("\n"); |
| 176 } | 174 } |
| 177 dictionary.append("Max-Age: 0\n"); | 175 dictionary.append("Max-Age: 0\n"); |
| 178 dictionary.append("\n"); | 176 dictionary.append("\n"); |
| 179 dictionary.append(kTestVcdiffDictionary, sizeof(kTestVcdiffDictionary) - 1); | 177 dictionary.append(kTestVcdiffDictionary, sizeof(kTestVcdiffDictionary) - 1); |
| 180 return dictionary; | 178 return dictionary; |
| 181 } | 179 } |
| 182 | 180 |
| 183 //------------------------------------------------------------------------------ | |
| 184 | |
| 185 TEST_F(SdchFilterTest, EmptyInputOk) { | 181 TEST_F(SdchFilterTest, EmptyInputOk) { |
| 186 std::vector<Filter::FilterType> filter_types; | 182 std::vector<Filter::FilterType> filter_types; |
| 187 filter_types.push_back(Filter::FILTER_TYPE_SDCH); | 183 filter_types.push_back(Filter::FILTER_TYPE_SDCH); |
| 188 char output_buffer[20]; | 184 char output_buffer[20]; |
| 189 std::string url_string("http://ignore.com"); | 185 std::string url_string("http://ignore.com"); |
| 190 filter_context()->SetURL(GURL(url_string)); | 186 filter_context()->SetURL(GURL(url_string)); |
| 191 scoped_ptr<Filter> filter(Filter::Factory(filter_types, *filter_context())); | 187 scoped_ptr<Filter> filter(Filter::Factory(filter_types, *filter_context())); |
| 192 | 188 |
| 193 // With no input data, try to read output. | 189 // With no input data, try to read output. |
| 194 int output_bytes_or_buffer_size = sizeof(output_buffer); | 190 int output_bytes_or_buffer_size = sizeof(output_buffer); |
| (...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 439 | 435 |
| 440 std::string url_string = "http://" + kSampleDomain; | 436 std::string url_string = "http://" + kSampleDomain; |
| 441 GURL url(url_string); | 437 GURL url(url_string); |
| 442 EXPECT_TRUE(AddSdchDictionary(dictionary, url)); | 438 EXPECT_TRUE(AddSdchDictionary(dictionary, url)); |
| 443 | 439 |
| 444 // Check we can't add it twice. | 440 // Check we can't add it twice. |
| 445 EXPECT_FALSE(AddSdchDictionary(dictionary, url)); | 441 EXPECT_FALSE(AddSdchDictionary(dictionary, url)); |
| 446 | 442 |
| 447 const std::string kSampleDomain2 = "sdchtest2.com"; | 443 const std::string kSampleDomain2 = "sdchtest2.com"; |
| 448 | 444 |
| 449 // Don't test adding a second dictionary if our limits are tight. | 445 // Construct a second SDCH dictionary from a VCDIFF dictionary. |
| 450 if (SdchManager::kMaxDictionaryCount > 1) { | 446 std::string dictionary2(NewSdchDictionary(kSampleDomain2)); |
| 451 // Construct a second SDCH dictionary from a VCDIFF dictionary. | |
| 452 std::string dictionary2(NewSdchDictionary(kSampleDomain2)); | |
| 453 | 447 |
| 454 std::string url_string2 = "http://" + kSampleDomain2; | 448 std::string url_string2 = "http://" + kSampleDomain2; |
| 455 GURL url2(url_string2); | 449 GURL url2(url_string2); |
| 456 EXPECT_TRUE(AddSdchDictionary(dictionary2, url2)); | 450 EXPECT_TRUE(AddSdchDictionary(dictionary2, url2)); |
| 457 } | |
| 458 } | 451 } |
| 459 | 452 |
| 460 TEST_F(SdchFilterTest, BasicDictionary) { | 453 TEST_F(SdchFilterTest, BasicDictionary) { |
| 461 // Construct a valid SDCH dictionary from a VCDIFF dictionary. | 454 // Construct a valid SDCH dictionary from a VCDIFF dictionary. |
| 462 const std::string kSampleDomain = "sdchtest.com"; | 455 const std::string kSampleDomain = "sdchtest.com"; |
| 463 std::string dictionary(NewSdchDictionary(kSampleDomain)); | 456 std::string dictionary(NewSdchDictionary(kSampleDomain)); |
| 464 | 457 |
| 465 std::string url_string = "http://" + kSampleDomain; | 458 std::string url_string = "http://" + kSampleDomain; |
| 466 | 459 |
| 467 GURL url(url_string); | 460 GURL url(url_string); |
| (...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 684 EXPECT_EQ(output.size(), 0u); // No output written. | 677 EXPECT_EQ(output.size(), 0u); // No output written. |
| 685 | 678 |
| 686 EXPECT_EQ(SDCH_OK, sdch_manager_->IsInSupportedDomain(GURL(url_string))); | 679 EXPECT_EQ(SDCH_OK, sdch_manager_->IsInSupportedDomain(GURL(url_string))); |
| 687 EXPECT_EQ(SDCH_DOMAIN_BLACKLIST_INCLUDES_TARGET, | 680 EXPECT_EQ(SDCH_DOMAIN_BLACKLIST_INCLUDES_TARGET, |
| 688 sdch_manager_->IsInSupportedDomain(wrong_domain_url)); | 681 sdch_manager_->IsInSupportedDomain(wrong_domain_url)); |
| 689 sdch_manager_->ClearBlacklistings(); | 682 sdch_manager_->ClearBlacklistings(); |
| 690 EXPECT_EQ(SDCH_OK, sdch_manager_->IsInSupportedDomain(wrong_domain_url)); | 683 EXPECT_EQ(SDCH_OK, sdch_manager_->IsInSupportedDomain(wrong_domain_url)); |
| 691 } | 684 } |
| 692 | 685 |
| 693 TEST_F(SdchFilterTest, DictionaryPathValidation) { | 686 TEST_F(SdchFilterTest, DictionaryPathValidation) { |
| 694 // Can't test path distinction between dictionaries if we aren't allowed | |
| 695 // more than one dictionary. | |
| 696 if (SdchManager::kMaxDictionaryCount <= 1) | |
| 697 return; | |
| 698 | |
| 699 // Construct a valid SDCH dictionary from a VCDIFF dictionary. | 687 // Construct a valid SDCH dictionary from a VCDIFF dictionary. |
| 700 const std::string kSampleDomain = "sdchtest.com"; | 688 const std::string kSampleDomain = "sdchtest.com"; |
| 701 std::string dictionary(NewSdchDictionary(kSampleDomain)); | 689 std::string dictionary(NewSdchDictionary(kSampleDomain)); |
| 702 | 690 |
| 703 std::string url_string = "http://" + kSampleDomain; | 691 std::string url_string = "http://" + kSampleDomain; |
| 704 | 692 |
| 705 GURL url(url_string); | 693 GURL url(url_string); |
| 706 EXPECT_TRUE(AddSdchDictionary(dictionary, url)); | 694 EXPECT_TRUE(AddSdchDictionary(dictionary, url)); |
| 707 | 695 |
| 708 // Create a dictionary with a path restriction, by prefixing dictionary. | 696 // Create a dictionary with a path restriction, by prefixing dictionary. |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 740 output_block_size, filter.get(), &output)); | 728 output_block_size, filter.get(), &output)); |
| 741 EXPECT_EQ(output.size(), 0u); // No output written. | 729 EXPECT_EQ(output.size(), 0u); // No output written. |
| 742 | 730 |
| 743 EXPECT_EQ(SDCH_DOMAIN_BLACKLIST_INCLUDES_TARGET, | 731 EXPECT_EQ(SDCH_DOMAIN_BLACKLIST_INCLUDES_TARGET, |
| 744 sdch_manager_->IsInSupportedDomain(GURL(url_string))); | 732 sdch_manager_->IsInSupportedDomain(GURL(url_string))); |
| 745 sdch_manager_->ClearBlacklistings(); | 733 sdch_manager_->ClearBlacklistings(); |
| 746 EXPECT_EQ(SDCH_OK, sdch_manager_->IsInSupportedDomain(GURL(url_string))); | 734 EXPECT_EQ(SDCH_OK, sdch_manager_->IsInSupportedDomain(GURL(url_string))); |
| 747 } | 735 } |
| 748 | 736 |
| 749 TEST_F(SdchFilterTest, DictionaryPortValidation) { | 737 TEST_F(SdchFilterTest, DictionaryPortValidation) { |
| 750 // Can't test port distinction between dictionaries if we aren't allowed | |
| 751 // more than one dictionary. | |
| 752 if (SdchManager::kMaxDictionaryCount <= 1) | |
| 753 return; | |
| 754 | |
| 755 // Construct a valid SDCH dictionary from a VCDIFF dictionary. | 738 // Construct a valid SDCH dictionary from a VCDIFF dictionary. |
| 756 const std::string kSampleDomain = "sdchtest.com"; | 739 const std::string kSampleDomain = "sdchtest.com"; |
| 757 std::string dictionary(NewSdchDictionary(kSampleDomain)); | 740 std::string dictionary(NewSdchDictionary(kSampleDomain)); |
| 758 | 741 |
| 759 std::string url_string = "http://" + kSampleDomain; | 742 std::string url_string = "http://" + kSampleDomain; |
| 760 | 743 |
| 761 GURL url(url_string); | 744 GURL url(url_string); |
| 762 EXPECT_TRUE(AddSdchDictionary(dictionary, url)); | 745 EXPECT_TRUE(AddSdchDictionary(dictionary, url)); |
| 763 | 746 |
| 764 // Create a dictionary with a port restriction, by prefixing old dictionary. | 747 // Create a dictionary with a port restriction, by prefixing old dictionary. |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 806 EXPECT_FALSE(FilterTestData(compressed_for_port, feed_block_size, | 789 EXPECT_FALSE(FilterTestData(compressed_for_port, feed_block_size, |
| 807 output_block_size, filter.get(), &output)); | 790 output_block_size, filter.get(), &output)); |
| 808 EXPECT_EQ(output.size(), 0u); // No output written. | 791 EXPECT_EQ(output.size(), 0u); // No output written. |
| 809 | 792 |
| 810 EXPECT_EQ(SDCH_DOMAIN_BLACKLIST_INCLUDES_TARGET, | 793 EXPECT_EQ(SDCH_DOMAIN_BLACKLIST_INCLUDES_TARGET, |
| 811 sdch_manager_->IsInSupportedDomain(GURL(url_string))); | 794 sdch_manager_->IsInSupportedDomain(GURL(url_string))); |
| 812 sdch_manager_->ClearBlacklistings(); | 795 sdch_manager_->ClearBlacklistings(); |
| 813 EXPECT_EQ(SDCH_OK, sdch_manager_->IsInSupportedDomain(GURL(url_string))); | 796 EXPECT_EQ(SDCH_OK, sdch_manager_->IsInSupportedDomain(GURL(url_string))); |
| 814 } | 797 } |
| 815 | 798 |
| 816 //------------------------------------------------------------------------------ | |
| 817 // Helper function to perform gzip compression of data. | 799 // Helper function to perform gzip compression of data. |
| 818 | |
| 819 static std::string gzip_compress(const std::string &input) { | 800 static std::string gzip_compress(const std::string &input) { |
| 820 z_stream zlib_stream; | 801 z_stream zlib_stream; |
| 821 memset(&zlib_stream, 0, sizeof(zlib_stream)); | 802 memset(&zlib_stream, 0, sizeof(zlib_stream)); |
| 822 int code; | 803 int code; |
| 823 | 804 |
| 824 // Initialize zlib | 805 // Initialize zlib |
| 825 code = deflateInit2(&zlib_stream, Z_DEFAULT_COMPRESSION, Z_DEFLATED, | 806 code = deflateInit2(&zlib_stream, Z_DEFAULT_COMPRESSION, Z_DEFLATED, |
| 826 -MAX_WBITS, | 807 -MAX_WBITS, |
| 827 8, // DEF_MEM_LEVEL | 808 8, // DEF_MEM_LEVEL |
| 828 Z_DEFAULT_STRATEGY); | 809 Z_DEFAULT_STRATEGY); |
| (...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 985 | 966 |
| 986 filter_context()->SetMimeType("anything/mime"); | 967 filter_context()->SetMimeType("anything/mime"); |
| 987 SetupFilterContextWithGURL(url); | 968 SetupFilterContextWithGURL(url); |
| 988 | 969 |
| 989 Filter::FixupEncodingTypes(*filter_context(), &filter_types); | 970 Filter::FixupEncodingTypes(*filter_context(), &filter_types); |
| 990 ASSERT_EQ(filter_types.size(), 2u); | 971 ASSERT_EQ(filter_types.size(), 2u); |
| 991 EXPECT_EQ(filter_types[0], Filter::FILTER_TYPE_SDCH); | 972 EXPECT_EQ(filter_types[0], Filter::FILTER_TYPE_SDCH); |
| 992 EXPECT_EQ(filter_types[1], Filter::FILTER_TYPE_GZIP_HELPING_SDCH); | 973 EXPECT_EQ(filter_types[1], Filter::FILTER_TYPE_GZIP_HELPING_SDCH); |
| 993 | 974 |
| 994 // First try with a large buffer (larger than test input, or compressed data). | 975 // First try with a large buffer (larger than test input, or compressed data). |
| 995 scoped_ptr<Filter> filter(Filter::Factory(filter_types, *filter_context())); | 976 scoped_ptr<Filter> filter(Filter::Factory(filter_types, *filter_context())); |
| 996 | 977 |
| 997 // Verify that chained filter is waiting for data. | 978 // Verify that chained filter is waiting for data. |
| 998 char tiny_output_buffer[10]; | 979 char tiny_output_buffer[10]; |
| 999 int tiny_output_size = sizeof(tiny_output_buffer); | 980 int tiny_output_size = sizeof(tiny_output_buffer); |
| 1000 EXPECT_EQ(Filter::FILTER_NEED_MORE_DATA, | 981 EXPECT_EQ(Filter::FILTER_NEED_MORE_DATA, |
| 1001 filter->ReadData(tiny_output_buffer, &tiny_output_size)); | 982 filter->ReadData(tiny_output_buffer, &tiny_output_size)); |
| 1002 | 983 |
| 1003 size_t feed_block_size = 100; | 984 size_t feed_block_size = 100; |
| 1004 size_t output_block_size = 100; | 985 size_t output_block_size = 100; |
| 1005 std::string output; | 986 std::string output; |
| (...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1206 scoped_ptr<Filter> filter(Filter::Factory(filter_types, *filter_context())); | 1187 scoped_ptr<Filter> filter(Filter::Factory(filter_types, *filter_context())); |
| 1207 | 1188 |
| 1208 // Setup another dictionary, expired. Don't add it to the filter context. | 1189 // Setup another dictionary, expired. Don't add it to the filter context. |
| 1209 // Delete stored dictionaries first to handle platforms which only | 1190 // Delete stored dictionaries first to handle platforms which only |
| 1210 // have room for a single dictionary. | 1191 // have room for a single dictionary. |
| 1211 sdch_manager_->ClearData(); | 1192 sdch_manager_->ClearData(); |
| 1212 std::string expired_dictionary(NewSdchExpiredDictionary(kSampleDomain)); | 1193 std::string expired_dictionary(NewSdchExpiredDictionary(kSampleDomain)); |
| 1213 | 1194 |
| 1214 // Don't use the Helper function since its insertion check is indeterminate | 1195 // Don't use the Helper function since its insertion check is indeterminate |
| 1215 // for a Max-Age: 0 dictionary. | 1196 // for a Max-Age: 0 dictionary. |
| 1216 sdch_manager_->AddSdchDictionary(expired_dictionary, url); | 1197 sdch_manager_->AddSdchDictionary(expired_dictionary, url, nullptr); |
| 1217 | 1198 |
| 1218 std::string client_hash; | 1199 std::string client_hash; |
| 1219 std::string server_hash; | 1200 std::string server_hash; |
| 1220 SdchManager::GenerateHash(expired_dictionary, &client_hash, &server_hash); | 1201 SdchManager::GenerateHash(expired_dictionary, &client_hash, &server_hash); |
| 1221 | 1202 |
| 1222 // Make sure Max-Age: 0 shows up as expired. | 1203 // Make sure Max-Age: 0 shows up as expired. |
| 1223 scoped_ptr<base::SimpleTestClock> clock(new base::SimpleTestClock); | 1204 scoped_ptr<base::SimpleTestClock> clock(new base::SimpleTestClock); |
| 1224 clock->SetNow(base::Time::Now()); | 1205 clock->SetNow(base::Time::Now()); |
| 1225 clock->Advance(base::TimeDelta::FromMinutes(5)); | 1206 clock->Advance(base::TimeDelta::FromMinutes(5)); |
| 1226 SdchProblemCode problem_code; | 1207 SdchProblemCode problem_code; |
| 1227 scoped_ptr<SdchManager::DictionarySet> hash_set( | 1208 scoped_ptr<SdchManager::DictionarySet> hash_set( |
| 1228 sdch_manager_->GetDictionarySetByHash( | 1209 sdch_manager_->GetDictionarySetByHash( |
| 1229 url, server_hash, &problem_code).Pass()); | 1210 url, server_hash, &problem_code).Pass()); |
| 1230 ASSERT_TRUE(hash_set); | 1211 ASSERT_TRUE(hash_set); |
| 1231 ASSERT_EQ(SDCH_OK, problem_code); | 1212 ASSERT_EQ(SDCH_OK, problem_code); |
| 1232 | 1213 |
| 1233 const_cast<SdchManager::Dictionary*>( | 1214 const_cast<SdchManager::Dictionary*>( |
| 1234 hash_set->GetDictionary(server_hash))->SetClockForTesting( | 1215 hash_set->GetDictionary(server_hash))->SetClockForTesting( |
| 1235 clock.Pass()); | 1216 clock.Pass()); |
| 1236 | 1217 |
| 1237 // Encode output with the second dictionary. | 1218 // Encode output with the second dictionary. |
| 1238 std::string sdch_compressed(NewSdchCompressedData(expired_dictionary)); | 1219 std::string sdch_compressed(NewSdchCompressedData(expired_dictionary)); |
| 1239 | 1220 |
| 1240 // See if the filter decodes it. | 1221 // See if the filter decodes it. |
| 1241 std::string output; | 1222 std::string output; |
| 1242 EXPECT_TRUE(FilterTestData(sdch_compressed, 100, 100, filter.get(), &output)); | 1223 EXPECT_TRUE(FilterTestData(sdch_compressed, 100, 100, filter.get(), &output)); |
| 1243 EXPECT_EQ(expanded_, output); | 1224 EXPECT_EQ(expanded_, output); |
| 1244 } | 1225 } |
| 1245 | 1226 |
| 1227 class SimpleSdchObserver : public SdchObserver { |
| 1228 public: |
| 1229 explicit SimpleSdchObserver(SdchManager* manager) |
| 1230 : dictionary_used_(0), manager_(manager) { |
| 1231 manager_->AddObserver(this); |
| 1232 } |
| 1233 ~SimpleSdchObserver() override { manager_->RemoveObserver(this); } |
| 1234 |
| 1235 // SdchObserver |
| 1236 void OnDictionaryUsed(SdchManager* manager, |
| 1237 const std::string& server_hash) override { |
| 1238 dictionary_used_++; |
| 1239 last_server_hash_ = server_hash; |
| 1240 } |
| 1241 |
| 1242 int dictionary_used_calls() const { return dictionary_used_; } |
| 1243 std::string last_server_hash() const { return last_server_hash_; } |
| 1244 |
| 1245 void OnGetDictionary(SdchManager* /* manager */, |
| 1246 const GURL& /* request_url */, |
| 1247 const GURL& /* dictionary_url */) override {} |
| 1248 void OnClearDictionaries(SdchManager* /* manager */) override {} |
| 1249 |
| 1250 private: |
| 1251 int dictionary_used_; |
| 1252 std::string last_server_hash_; |
| 1253 SdchManager* manager_; |
| 1254 |
| 1255 DISALLOW_COPY_AND_ASSIGN(SimpleSdchObserver); |
| 1256 }; |
| 1257 |
| 1258 TEST_F(SdchFilterTest, DictionaryUsedSignaled) { |
| 1259 // Construct a valid SDCH dictionary from a VCDIFF dictionary. |
| 1260 const std::string kSampleDomain = "sdchtest.com"; |
| 1261 std::string dictionary(NewSdchDictionary(kSampleDomain)); |
| 1262 SimpleSdchObserver observer(sdch_manager_.get()); |
| 1263 |
| 1264 std::string url_string = "http://" + kSampleDomain; |
| 1265 |
| 1266 GURL url(url_string); |
| 1267 EXPECT_TRUE(AddSdchDictionary(dictionary, url)); |
| 1268 |
| 1269 std::string client_hash; |
| 1270 std::string server_hash; |
| 1271 SdchManager::GenerateHash(dictionary, &client_hash, &server_hash); |
| 1272 |
| 1273 std::string compressed(NewSdchCompressedData(dictionary)); |
| 1274 |
| 1275 std::vector<Filter::FilterType> filter_types; |
| 1276 filter_types.push_back(Filter::FILTER_TYPE_SDCH); |
| 1277 |
| 1278 SetupFilterContextWithGURL(url); |
| 1279 |
| 1280 scoped_ptr<Filter> filter(Filter::Factory(filter_types, *filter_context())); |
| 1281 |
| 1282 size_t feed_block_size = 100; |
| 1283 size_t output_block_size = 100; |
| 1284 std::string output; |
| 1285 EXPECT_TRUE(FilterTestData(compressed, feed_block_size, output_block_size, |
| 1286 filter.get(), &output)); |
| 1287 EXPECT_EQ(output, expanded_); |
| 1288 |
| 1289 filter.reset(nullptr); |
| 1290 |
| 1291 // Confirm that we got a "DictionaryUsed" signal from the SdchManager |
| 1292 // for our dictionary. |
| 1293 EXPECT_EQ(1, observer.dictionary_used_calls()); |
| 1294 EXPECT_EQ(server_hash, observer.last_server_hash()); |
| 1295 } |
| 1296 |
| 1246 } // namespace net | 1297 } // namespace net |
| OLD | NEW |