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 <string> | 7 #include <string> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
(...skipping 13 matching lines...) Expand all Loading... |
24 //------------------------------------------------------------------------------ | 24 //------------------------------------------------------------------------------ |
25 // Provide sample data and compression results with a sample VCDIFF dictionary. | 25 // Provide sample data and compression results with a sample VCDIFF dictionary. |
26 // 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. |
27 static const char kTestVcdiffDictionary[] = "DictionaryFor" | 27 static const char kTestVcdiffDictionary[] = "DictionaryFor" |
28 "SdchCompression1SdchCompression2SdchCompression3SdchCompression\n"; | 28 "SdchCompression1SdchCompression2SdchCompression3SdchCompression\n"; |
29 | 29 |
30 //------------------------------------------------------------------------------ | 30 //------------------------------------------------------------------------------ |
31 | 31 |
32 class MockSdchObserver : public SdchObserver { | 32 class MockSdchObserver : public SdchObserver { |
33 public: | 33 public: |
34 MockSdchObserver() : get_dictionary_notifications_(0) {} | 34 MockSdchObserver() |
| 35 : dictionary_used_notifications_(0), |
| 36 get_dictionary_notifications_(0), |
| 37 clear_dictionaries_notifications_(0) {} |
35 | 38 |
36 const GURL& last_dictionary_request_url() { | 39 std::string last_server_hash() const { return last_server_hash_; } |
| 40 int dictionary_used_notifications() const { |
| 41 return dictionary_used_notifications_; |
| 42 } |
| 43 const GURL& last_dictionary_request_url() const { |
37 return last_dictionary_request_url_; | 44 return last_dictionary_request_url_; |
38 } | 45 } |
39 const GURL& last_dictionary_url() { return last_dictionary_url_; } | 46 const GURL& last_dictionary_url() const { return last_dictionary_url_; } |
40 int get_dictionary_notifications() { return get_dictionary_notifications_; } | 47 int get_dictionary_notifications() const { |
| 48 return get_dictionary_notifications_; |
| 49 } |
| 50 |
| 51 int clear_dictionary_notifications() const { |
| 52 return clear_dictionaries_notifications_; |
| 53 } |
41 | 54 |
42 // SdchObserver implementation | 55 // SdchObserver implementation |
| 56 void OnDictionaryUsed(SdchManager* manager, |
| 57 const std::string& server_hash) override { |
| 58 last_server_hash_ = server_hash; |
| 59 ++dictionary_used_notifications_; |
| 60 } |
| 61 |
43 void OnGetDictionary(SdchManager* manager, | 62 void OnGetDictionary(SdchManager* manager, |
44 const GURL& request_url, | 63 const GURL& request_url, |
45 const GURL& dictionary_url) override { | 64 const GURL& dictionary_url) override { |
46 ++get_dictionary_notifications_; | 65 ++get_dictionary_notifications_; |
47 last_dictionary_request_url_ = request_url; | 66 last_dictionary_request_url_ = request_url; |
48 last_dictionary_url_ = dictionary_url; | 67 last_dictionary_url_ = dictionary_url; |
49 } | 68 } |
50 void OnClearDictionaries(SdchManager* manager) override {} | 69 void OnClearDictionaries(SdchManager* manager) override { |
| 70 ++clear_dictionaries_notifications_; |
| 71 } |
51 | 72 |
52 private: | 73 private: |
| 74 int dictionary_used_notifications_; |
53 int get_dictionary_notifications_; | 75 int get_dictionary_notifications_; |
| 76 int clear_dictionaries_notifications_; |
| 77 |
| 78 std::string last_server_hash_; |
54 GURL last_dictionary_request_url_; | 79 GURL last_dictionary_request_url_; |
55 GURL last_dictionary_url_; | 80 GURL last_dictionary_url_; |
| 81 |
| 82 DISALLOW_COPY_AND_ASSIGN(MockSdchObserver); |
56 }; | 83 }; |
57 | 84 |
58 class SdchManagerTest : public testing::Test { | 85 class SdchManagerTest : public testing::Test { |
59 protected: | 86 protected: |
60 SdchManagerTest() | 87 SdchManagerTest() |
61 : sdch_manager_(new SdchManager), | 88 : sdch_manager_(new SdchManager), |
62 default_support_(false), | 89 default_support_(false), |
63 default_https_support_(false) { | 90 default_https_support_(false) { |
64 default_support_ = sdch_manager_->sdch_enabled(); | 91 default_support_ = sdch_manager_->sdch_enabled(); |
65 default_https_support_ = sdch_manager_->secure_scheme_supported(); | 92 default_https_support_ = sdch_manager_->secure_scheme_supported(); |
66 } | 93 } |
67 | 94 |
68 ~SdchManagerTest() override {} | 95 ~SdchManagerTest() override {} |
69 | 96 |
70 SdchManager* sdch_manager() { return sdch_manager_.get(); } | 97 SdchManager* sdch_manager() { return sdch_manager_.get(); } |
71 | 98 |
72 // Reset globals back to default state. | 99 // Reset globals back to default state. |
73 void TearDown() override { | 100 void TearDown() override { |
74 SdchManager::EnableSdchSupport(default_support_); | 101 SdchManager::EnableSdchSupport(default_support_); |
75 SdchManager::EnableSecureSchemeSupport(default_https_support_); | 102 SdchManager::EnableSecureSchemeSupport(default_https_support_); |
76 } | 103 } |
77 | 104 |
78 // Attempt to add a dictionary to the manager and probe for success or | 105 // Attempt to add a dictionary to the manager and probe for success or |
79 // failure. | 106 // failure. |
80 bool AddSdchDictionary(const std::string& dictionary_text, | 107 bool AddSdchDictionary(const std::string& dictionary_text, |
81 const GURL& gurl) { | 108 const GURL& gurl) { |
82 return sdch_manager_->AddSdchDictionary(dictionary_text, gurl) == SDCH_OK; | 109 return sdch_manager_->AddSdchDictionary(dictionary_text, gurl, nullptr) == |
| 110 SDCH_OK; |
83 } | 111 } |
84 | 112 |
85 private: | 113 private: |
86 scoped_ptr<SdchManager> sdch_manager_; | 114 scoped_ptr<SdchManager> sdch_manager_; |
87 bool default_support_; | 115 bool default_support_; |
88 bool default_https_support_; | 116 bool default_https_support_; |
89 }; | 117 }; |
90 | 118 |
91 static std::string NewSdchDictionary(const std::string& domain) { | 119 static std::string NewSdchDictionary(const std::string& domain) { |
92 std::string dictionary; | 120 std::string dictionary; |
(...skipping 294 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
387 // See test CanSetExactMatchDictionary above for first try. | 415 // See test CanSetExactMatchDictionary above for first try. |
388 TEST_F(SdchManagerTest, CanStillSetExactMatchDictionary) { | 416 TEST_F(SdchManagerTest, CanStillSetExactMatchDictionary) { |
389 std::string dictionary_domain("x.y.z.google.com"); | 417 std::string dictionary_domain("x.y.z.google.com"); |
390 std::string dictionary_text(NewSdchDictionary(dictionary_domain)); | 418 std::string dictionary_text(NewSdchDictionary(dictionary_domain)); |
391 | 419 |
392 // Perfect match should *STILL* work. | 420 // Perfect match should *STILL* work. |
393 EXPECT_TRUE(AddSdchDictionary(dictionary_text, | 421 EXPECT_TRUE(AddSdchDictionary(dictionary_text, |
394 GURL("http://" + dictionary_domain))); | 422 GURL("http://" + dictionary_domain))); |
395 } | 423 } |
396 | 424 |
397 // Make sure the DOS protection precludes the addition of too many dictionaries. | |
398 TEST_F(SdchManagerTest, TooManyDictionaries) { | |
399 std::string dictionary_domain(".google.com"); | |
400 std::string dictionary_text(NewSdchDictionary(dictionary_domain)); | |
401 | |
402 for (size_t count = 0; count < SdchManager::kMaxDictionaryCount; ++count) { | |
403 EXPECT_TRUE(AddSdchDictionary(dictionary_text, | |
404 GURL("http://www.google.com"))); | |
405 dictionary_text += " "; // Create dictionary with different SHA signature. | |
406 } | |
407 EXPECT_FALSE( | |
408 AddSdchDictionary(dictionary_text, GURL("http://www.google.com"))); | |
409 } | |
410 | |
411 TEST_F(SdchManagerTest, DictionaryNotTooLarge) { | |
412 std::string dictionary_domain(".google.com"); | |
413 std::string dictionary_text(NewSdchDictionary(dictionary_domain)); | |
414 | |
415 dictionary_text.append( | |
416 SdchManager::kMaxDictionarySize - dictionary_text.size(), ' '); | |
417 EXPECT_TRUE(AddSdchDictionary(dictionary_text, | |
418 GURL("http://" + dictionary_domain))); | |
419 } | |
420 | |
421 TEST_F(SdchManagerTest, DictionaryTooLarge) { | |
422 std::string dictionary_domain(".google.com"); | |
423 std::string dictionary_text(NewSdchDictionary(dictionary_domain)); | |
424 | |
425 dictionary_text.append( | |
426 SdchManager::kMaxDictionarySize + 1 - dictionary_text.size(), ' '); | |
427 EXPECT_FALSE(AddSdchDictionary(dictionary_text, | |
428 GURL("http://" + dictionary_domain))); | |
429 } | |
430 | |
431 TEST_F(SdchManagerTest, PathMatch) { | 425 TEST_F(SdchManagerTest, PathMatch) { |
432 bool (*PathMatch)(const std::string& path, const std::string& restriction) = | 426 bool (*PathMatch)(const std::string& path, const std::string& restriction) = |
433 SdchManager::Dictionary::PathMatch; | 427 SdchManager::Dictionary::PathMatch; |
434 // Perfect match is supported. | 428 // Perfect match is supported. |
435 EXPECT_TRUE(PathMatch("/search", "/search")); | 429 EXPECT_TRUE(PathMatch("/search", "/search")); |
436 EXPECT_TRUE(PathMatch("/search/", "/search/")); | 430 EXPECT_TRUE(PathMatch("/search/", "/search/")); |
437 | 431 |
438 // Prefix only works if last character of restriction is a slash, or first | 432 // Prefix only works if last character of restriction is a slash, or first |
439 // character in path after a match is a slash. Validate each case separately. | 433 // character in path after a match is a slash. Validate each case separately. |
440 | 434 |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
521 | 515 |
522 SdchProblemCode problem_code; | 516 SdchProblemCode problem_code; |
523 dict_set = sdch_manager()->GetDictionarySetByHash( | 517 dict_set = sdch_manager()->GetDictionarySetByHash( |
524 GURL("http://" + dictionary_domain_1 + "/random_url"), | 518 GURL("http://" + dictionary_domain_1 + "/random_url"), |
525 server_hash_1, &problem_code); | 519 server_hash_1, &problem_code); |
526 EXPECT_TRUE(dict_set); | 520 EXPECT_TRUE(dict_set); |
527 EXPECT_TRUE(dict_set->GetDictionary(server_hash_1)); | 521 EXPECT_TRUE(dict_set->GetDictionary(server_hash_1)); |
528 EXPECT_EQ(SDCH_OK, problem_code); | 522 EXPECT_EQ(SDCH_OK, problem_code); |
529 | 523 |
530 second_manager.AddSdchDictionary( | 524 second_manager.AddSdchDictionary( |
531 dictionary_text_2, GURL("http://" + dictionary_domain_2)); | 525 dictionary_text_2, GURL("http://" + dictionary_domain_2), nullptr); |
532 dict_set = second_manager.GetDictionarySetByHash( | 526 dict_set = second_manager.GetDictionarySetByHash( |
533 GURL("http://" + dictionary_domain_2 + "/random_url"), | 527 GURL("http://" + dictionary_domain_2 + "/random_url"), |
534 server_hash_2, &problem_code); | 528 server_hash_2, &problem_code); |
535 EXPECT_TRUE(dict_set); | 529 EXPECT_TRUE(dict_set); |
536 EXPECT_TRUE(dict_set->GetDictionary(server_hash_2)); | 530 EXPECT_TRUE(dict_set->GetDictionary(server_hash_2)); |
537 EXPECT_EQ(SDCH_OK, problem_code); | 531 EXPECT_EQ(SDCH_OK, problem_code); |
538 | 532 |
539 dict_set = sdch_manager()->GetDictionarySetByHash( | 533 dict_set = sdch_manager()->GetDictionarySetByHash( |
540 GURL("http://" + dictionary_domain_2 + "/random_url"), | 534 GURL("http://" + dictionary_domain_2 + "/random_url"), |
541 server_hash_2, &problem_code); | 535 server_hash_2, &problem_code); |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
659 | 653 |
660 TEST_F(SdchManagerTest, SdchOnByDefault) { | 654 TEST_F(SdchManagerTest, SdchOnByDefault) { |
661 GURL google_url("http://www.google.com"); | 655 GURL google_url("http://www.google.com"); |
662 scoped_ptr<SdchManager> sdch_manager(new SdchManager); | 656 scoped_ptr<SdchManager> sdch_manager(new SdchManager); |
663 | 657 |
664 EXPECT_EQ(SDCH_OK, sdch_manager->IsInSupportedDomain(google_url)); | 658 EXPECT_EQ(SDCH_OK, sdch_manager->IsInSupportedDomain(google_url)); |
665 SdchManager::EnableSdchSupport(false); | 659 SdchManager::EnableSdchSupport(false); |
666 EXPECT_EQ(SDCH_DISABLED, sdch_manager->IsInSupportedDomain(google_url)); | 660 EXPECT_EQ(SDCH_DISABLED, sdch_manager->IsInSupportedDomain(google_url)); |
667 } | 661 } |
668 | 662 |
| 663 // Confirm dispatch of notification. |
| 664 TEST_F(SdchManagerTest, SdchDictionaryUsed) { |
| 665 MockSdchObserver observer; |
| 666 sdch_manager()->AddObserver(&observer); |
| 667 |
| 668 EXPECT_EQ(0, observer.dictionary_used_notifications()); |
| 669 sdch_manager()->OnDictionaryUsed("xyzzy"); |
| 670 EXPECT_EQ(1, observer.dictionary_used_notifications()); |
| 671 EXPECT_EQ("xyzzy", observer.last_server_hash()); |
| 672 |
| 673 std::string dictionary_domain("x.y.z.google.com"); |
| 674 GURL target_gurl("http://" + dictionary_domain); |
| 675 std::string dictionary_text(NewSdchDictionary(dictionary_domain)); |
| 676 std::string client_hash; |
| 677 std::string server_hash; |
| 678 SdchManager::GenerateHash(dictionary_text, &client_hash, &server_hash); |
| 679 EXPECT_TRUE(AddSdchDictionary(dictionary_text, target_gurl)); |
| 680 EXPECT_EQ("xyzzy", observer.last_server_hash()); |
| 681 EXPECT_EQ(1, observer.dictionary_used_notifications()); |
| 682 |
| 683 EXPECT_TRUE(sdch_manager()->GetDictionarySet(target_gurl)); |
| 684 EXPECT_EQ("xyzzy", observer.last_server_hash()); |
| 685 EXPECT_EQ(1, observer.dictionary_used_notifications()); |
| 686 |
| 687 sdch_manager()->RemoveObserver(&observer); |
| 688 EXPECT_EQ(1, observer.dictionary_used_notifications()); |
| 689 EXPECT_EQ("xyzzy", observer.last_server_hash()); |
| 690 sdch_manager()->OnDictionaryUsed("plugh"); |
| 691 EXPECT_EQ(1, observer.dictionary_used_notifications()); |
| 692 EXPECT_EQ("xyzzy", observer.last_server_hash()); |
| 693 } |
| 694 |
669 #else | 695 #else |
670 | 696 |
671 TEST(SdchManagerTest, SdchOffByDefault) { | 697 TEST(SdchManagerTest, SdchOffByDefault) { |
672 GURL google_url("http://www.google.com"); | 698 GURL google_url("http://www.google.com"); |
673 scoped_ptr<SdchManager> sdch_manager(new SdchManager); | 699 scoped_ptr<SdchManager> sdch_manager(new SdchManager); |
674 | 700 |
675 EXPECT_EQ(SDCH_DISABLED, sdch_manager->IsInSupportedDomain(google_url)); | 701 EXPECT_EQ(SDCH_DISABLED, sdch_manager->IsInSupportedDomain(google_url)); |
676 SdchManager::EnableSdchSupport(true); | 702 SdchManager::EnableSdchSupport(true); |
677 EXPECT_EQ(SDCH_OK, sdch_manager->IsInSupportedDomain(google_url)); | 703 EXPECT_EQ(SDCH_OK, sdch_manager->IsInSupportedDomain(google_url)); |
678 } | 704 } |
679 | 705 |
680 #endif // !defined(OS_IOS) | 706 #endif // !defined(OS_IOS) |
681 | 707 |
682 } // namespace net | 708 } // namespace net |
OLD | NEW |