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 |
| 39 std::string last_server_hash() { return last_server_hash_; } |
| 40 int dictionary_used_notifications() { return dictionary_used_notifications_; } |
36 const GURL& last_dictionary_request_url() { | 41 const GURL& last_dictionary_request_url() { |
37 return last_dictionary_request_url_; | 42 return last_dictionary_request_url_; |
38 } | 43 } |
39 const GURL& last_dictionary_url() { return last_dictionary_url_; } | 44 const GURL& last_dictionary_url() { return last_dictionary_url_; } |
40 int get_dictionary_notifications() { return get_dictionary_notifications_; } | 45 int get_dictionary_notifications() { return get_dictionary_notifications_; } |
41 | 46 |
| 47 int clear_dictionary_notifications() { |
| 48 return clear_dictionaries_notifications_; |
| 49 } |
| 50 |
42 // SdchObserver implementation | 51 // SdchObserver implementation |
| 52 void OnDictionaryUsed(SdchManager* manager, |
| 53 const std::string& server_hash) override { |
| 54 last_server_hash_ = server_hash; |
| 55 ++dictionary_used_notifications_; |
| 56 } |
| 57 |
43 void OnGetDictionary(SdchManager* manager, | 58 void OnGetDictionary(SdchManager* manager, |
44 const GURL& request_url, | 59 const GURL& request_url, |
45 const GURL& dictionary_url) override { | 60 const GURL& dictionary_url) override { |
46 ++get_dictionary_notifications_; | 61 ++get_dictionary_notifications_; |
47 last_dictionary_request_url_ = request_url; | 62 last_dictionary_request_url_ = request_url; |
48 last_dictionary_url_ = dictionary_url; | 63 last_dictionary_url_ = dictionary_url; |
49 } | 64 } |
50 void OnClearDictionaries(SdchManager* manager) override {} | 65 void OnClearDictionaries(SdchManager* manager) override { |
| 66 ++clear_dictionaries_notifications_; |
| 67 } |
51 | 68 |
52 private: | 69 private: |
| 70 int dictionary_used_notifications_; |
53 int get_dictionary_notifications_; | 71 int get_dictionary_notifications_; |
| 72 int clear_dictionaries_notifications_; |
| 73 |
| 74 std::string last_server_hash_; |
54 GURL last_dictionary_request_url_; | 75 GURL last_dictionary_request_url_; |
55 GURL last_dictionary_url_; | 76 GURL last_dictionary_url_; |
56 }; | 77 }; |
57 | 78 |
58 class SdchManagerTest : public testing::Test { | 79 class SdchManagerTest : public testing::Test { |
59 protected: | 80 protected: |
60 SdchManagerTest() | 81 SdchManagerTest() |
61 : sdch_manager_(new SdchManager), | 82 : sdch_manager_(new SdchManager), |
62 default_support_(false), | 83 default_support_(false), |
63 default_https_support_(false) { | 84 default_https_support_(false) { |
64 default_support_ = sdch_manager_->sdch_enabled(); | 85 default_support_ = sdch_manager_->sdch_enabled(); |
65 default_https_support_ = sdch_manager_->secure_scheme_supported(); | 86 default_https_support_ = sdch_manager_->secure_scheme_supported(); |
66 } | 87 } |
67 | 88 |
68 ~SdchManagerTest() override {} | 89 ~SdchManagerTest() override {} |
69 | 90 |
70 SdchManager* sdch_manager() { return sdch_manager_.get(); } | 91 SdchManager* sdch_manager() { return sdch_manager_.get(); } |
71 | 92 |
72 // Reset globals back to default state. | 93 // Reset globals back to default state. |
73 void TearDown() override { | 94 void TearDown() override { |
74 SdchManager::EnableSdchSupport(default_support_); | 95 SdchManager::EnableSdchSupport(default_support_); |
75 SdchManager::EnableSecureSchemeSupport(default_https_support_); | 96 SdchManager::EnableSecureSchemeSupport(default_https_support_); |
76 } | 97 } |
77 | 98 |
78 // Attempt to add a dictionary to the manager and probe for success or | 99 // Attempt to add a dictionary to the manager and probe for success or |
79 // failure. | 100 // failure. |
80 bool AddSdchDictionary(const std::string& dictionary_text, | 101 bool AddSdchDictionary(const std::string& dictionary_text, |
81 const GURL& gurl) { | 102 const GURL& gurl) { |
82 return sdch_manager_->AddSdchDictionary(dictionary_text, gurl) == SDCH_OK; | 103 return sdch_manager_->AddSdchDictionary(dictionary_text, gurl, nullptr) == |
| 104 SDCH_OK; |
83 } | 105 } |
84 | 106 |
85 private: | 107 private: |
86 scoped_ptr<SdchManager> sdch_manager_; | 108 scoped_ptr<SdchManager> sdch_manager_; |
87 bool default_support_; | 109 bool default_support_; |
88 bool default_https_support_; | 110 bool default_https_support_; |
89 }; | 111 }; |
90 | 112 |
91 static std::string NewSdchDictionary(const std::string& domain) { | 113 static std::string NewSdchDictionary(const std::string& domain) { |
92 std::string dictionary; | 114 std::string dictionary; |
(...skipping 294 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
387 // See test CanSetExactMatchDictionary above for first try. | 409 // See test CanSetExactMatchDictionary above for first try. |
388 TEST_F(SdchManagerTest, CanStillSetExactMatchDictionary) { | 410 TEST_F(SdchManagerTest, CanStillSetExactMatchDictionary) { |
389 std::string dictionary_domain("x.y.z.google.com"); | 411 std::string dictionary_domain("x.y.z.google.com"); |
390 std::string dictionary_text(NewSdchDictionary(dictionary_domain)); | 412 std::string dictionary_text(NewSdchDictionary(dictionary_domain)); |
391 | 413 |
392 // Perfect match should *STILL* work. | 414 // Perfect match should *STILL* work. |
393 EXPECT_TRUE(AddSdchDictionary(dictionary_text, | 415 EXPECT_TRUE(AddSdchDictionary(dictionary_text, |
394 GURL("http://" + dictionary_domain))); | 416 GURL("http://" + dictionary_domain))); |
395 } | 417 } |
396 | 418 |
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) { | 419 TEST_F(SdchManagerTest, PathMatch) { |
432 bool (*PathMatch)(const std::string& path, const std::string& restriction) = | 420 bool (*PathMatch)(const std::string& path, const std::string& restriction) = |
433 SdchManager::Dictionary::PathMatch; | 421 SdchManager::Dictionary::PathMatch; |
434 // Perfect match is supported. | 422 // Perfect match is supported. |
435 EXPECT_TRUE(PathMatch("/search", "/search")); | 423 EXPECT_TRUE(PathMatch("/search", "/search")); |
436 EXPECT_TRUE(PathMatch("/search/", "/search/")); | 424 EXPECT_TRUE(PathMatch("/search/", "/search/")); |
437 | 425 |
438 // Prefix only works if last character of restriction is a slash, or first | 426 // 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. | 427 // character in path after a match is a slash. Validate each case separately. |
440 | 428 |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
521 | 509 |
522 SdchProblemCode problem_code; | 510 SdchProblemCode problem_code; |
523 dict_set = sdch_manager()->GetDictionarySetByHash( | 511 dict_set = sdch_manager()->GetDictionarySetByHash( |
524 GURL("http://" + dictionary_domain_1 + "/random_url"), | 512 GURL("http://" + dictionary_domain_1 + "/random_url"), |
525 server_hash_1, &problem_code); | 513 server_hash_1, &problem_code); |
526 EXPECT_TRUE(dict_set); | 514 EXPECT_TRUE(dict_set); |
527 EXPECT_TRUE(dict_set->GetDictionary(server_hash_1)); | 515 EXPECT_TRUE(dict_set->GetDictionary(server_hash_1)); |
528 EXPECT_EQ(SDCH_OK, problem_code); | 516 EXPECT_EQ(SDCH_OK, problem_code); |
529 | 517 |
530 second_manager.AddSdchDictionary( | 518 second_manager.AddSdchDictionary( |
531 dictionary_text_2, GURL("http://" + dictionary_domain_2)); | 519 dictionary_text_2, GURL("http://" + dictionary_domain_2), nullptr); |
532 dict_set = second_manager.GetDictionarySetByHash( | 520 dict_set = second_manager.GetDictionarySetByHash( |
533 GURL("http://" + dictionary_domain_2 + "/random_url"), | 521 GURL("http://" + dictionary_domain_2 + "/random_url"), |
534 server_hash_2, &problem_code); | 522 server_hash_2, &problem_code); |
535 EXPECT_TRUE(dict_set); | 523 EXPECT_TRUE(dict_set); |
536 EXPECT_TRUE(dict_set->GetDictionary(server_hash_2)); | 524 EXPECT_TRUE(dict_set->GetDictionary(server_hash_2)); |
537 EXPECT_EQ(SDCH_OK, problem_code); | 525 EXPECT_EQ(SDCH_OK, problem_code); |
538 | 526 |
539 dict_set = sdch_manager()->GetDictionarySetByHash( | 527 dict_set = sdch_manager()->GetDictionarySetByHash( |
540 GURL("http://" + dictionary_domain_2 + "/random_url"), | 528 GURL("http://" + dictionary_domain_2 + "/random_url"), |
541 server_hash_2, &problem_code); | 529 server_hash_2, &problem_code); |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
659 | 647 |
660 TEST_F(SdchManagerTest, SdchOnByDefault) { | 648 TEST_F(SdchManagerTest, SdchOnByDefault) { |
661 GURL google_url("http://www.google.com"); | 649 GURL google_url("http://www.google.com"); |
662 scoped_ptr<SdchManager> sdch_manager(new SdchManager); | 650 scoped_ptr<SdchManager> sdch_manager(new SdchManager); |
663 | 651 |
664 EXPECT_EQ(SDCH_OK, sdch_manager->IsInSupportedDomain(google_url)); | 652 EXPECT_EQ(SDCH_OK, sdch_manager->IsInSupportedDomain(google_url)); |
665 SdchManager::EnableSdchSupport(false); | 653 SdchManager::EnableSdchSupport(false); |
666 EXPECT_EQ(SDCH_DISABLED, sdch_manager->IsInSupportedDomain(google_url)); | 654 EXPECT_EQ(SDCH_DISABLED, sdch_manager->IsInSupportedDomain(google_url)); |
667 } | 655 } |
668 | 656 |
| 657 // Confirm dispatch of notification. |
| 658 TEST_F(SdchManagerTest, SdchDictionaryUsed) { |
| 659 MockSdchObserver observer; |
| 660 sdch_manager()->AddObserver(&observer); |
| 661 |
| 662 EXPECT_EQ(0, observer.dictionary_used_notifications()); |
| 663 sdch_manager()->OnDictionaryUsed("xyzzy"); |
| 664 EXPECT_EQ(1, observer.dictionary_used_notifications()); |
| 665 EXPECT_EQ("xyzzy", observer.last_server_hash()); |
| 666 |
| 667 std::string dictionary_domain("x.y.z.google.com"); |
| 668 GURL target_gurl("http://" + dictionary_domain); |
| 669 std::string dictionary_text(NewSdchDictionary(dictionary_domain)); |
| 670 std::string client_hash; |
| 671 std::string server_hash; |
| 672 SdchManager::GenerateHash(dictionary_text, &client_hash, &server_hash); |
| 673 EXPECT_TRUE(AddSdchDictionary(dictionary_text, target_gurl)); |
| 674 EXPECT_EQ("xyzzy", observer.last_server_hash()); |
| 675 EXPECT_EQ(1, observer.dictionary_used_notifications()); |
| 676 |
| 677 EXPECT_TRUE(sdch_manager()->GetDictionarySet(target_gurl)); |
| 678 EXPECT_EQ("xyzzy", observer.last_server_hash()); |
| 679 EXPECT_EQ(1, observer.dictionary_used_notifications()); |
| 680 |
| 681 sdch_manager()->RemoveObserver(&observer); |
| 682 EXPECT_EQ(1, observer.dictionary_used_notifications()); |
| 683 EXPECT_EQ("xyzzy", observer.last_server_hash()); |
| 684 sdch_manager()->OnDictionaryUsed("plugh"); |
| 685 EXPECT_EQ(1, observer.dictionary_used_notifications()); |
| 686 EXPECT_EQ("xyzzy", observer.last_server_hash()); |
| 687 } |
| 688 |
669 #else | 689 #else |
670 | 690 |
671 TEST(SdchManagerTest, SdchOffByDefault) { | 691 TEST(SdchManagerTest, SdchOffByDefault) { |
672 GURL google_url("http://www.google.com"); | 692 GURL google_url("http://www.google.com"); |
673 scoped_ptr<SdchManager> sdch_manager(new SdchManager); | 693 scoped_ptr<SdchManager> sdch_manager(new SdchManager); |
674 | 694 |
675 EXPECT_EQ(SDCH_DISABLED, sdch_manager->IsInSupportedDomain(google_url)); | 695 EXPECT_EQ(SDCH_DISABLED, sdch_manager->IsInSupportedDomain(google_url)); |
676 SdchManager::EnableSdchSupport(true); | 696 SdchManager::EnableSdchSupport(true); |
677 EXPECT_EQ(SDCH_OK, sdch_manager->IsInSupportedDomain(google_url)); | 697 EXPECT_EQ(SDCH_OK, sdch_manager->IsInSupportedDomain(google_url)); |
678 } | 698 } |
679 | 699 |
680 #endif // !defined(OS_IOS) | 700 #endif // !defined(OS_IOS) |
681 | 701 |
682 } // namespace net | 702 } // namespace net |
OLD | NEW |