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" |
11 #include "net/base/sdch_manager.h" | 11 #include "net/base/sdch_manager.h" |
| 12 #include "net/base/sdch_observer.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 #include "url/gurl.h" |
13 | 15 |
14 namespace net { | 16 namespace net { |
15 | 17 |
16 //------------------------------------------------------------------------------ | 18 //------------------------------------------------------------------------------ |
17 // Workaround for http://crbug.com/418975; remove when fixed. | |
18 #if !defined(OS_IOS) | |
19 | |
20 //------------------------------------------------------------------------------ | |
21 // Provide sample data and compression results with a sample VCDIFF dictionary. | 19 // Provide sample data and compression results with a sample VCDIFF dictionary. |
22 // Note an SDCH dictionary has extra meta-data before the VCDIFF dictionary. | 20 // Note an SDCH dictionary has extra meta-data before the VCDIFF dictionary. |
23 static const char kTestVcdiffDictionary[] = "DictionaryFor" | 21 static const char kTestVcdiffDictionary[] = "DictionaryFor" |
24 "SdchCompression1SdchCompression2SdchCompression3SdchCompression\n"; | 22 "SdchCompression1SdchCompression2SdchCompression3SdchCompression\n"; |
25 | 23 |
26 //------------------------------------------------------------------------------ | 24 //------------------------------------------------------------------------------ |
27 | 25 |
| 26 class MockSdchObserver : public SdchObserver { |
| 27 public: |
| 28 MockSdchObserver() : get_dictionary_notifications_(0) {} |
| 29 |
| 30 const GURL& last_dictionary_request_url() { |
| 31 return last_dictionary_request_url_; |
| 32 } |
| 33 const GURL& last_dictionary_url() { return last_dictionary_url_; } |
| 34 int get_dictionary_notifications() { return get_dictionary_notifications_; } |
| 35 |
| 36 // SdchObserver implementation |
| 37 void OnGetDictionary(SdchManager* manager, |
| 38 const GURL& request_url, |
| 39 const GURL& dictionary_url) override { |
| 40 ++get_dictionary_notifications_; |
| 41 last_dictionary_request_url_ = request_url; |
| 42 last_dictionary_url_ = dictionary_url; |
| 43 } |
| 44 void OnClearDictionaries(SdchManager* manager) override {} |
| 45 |
| 46 private: |
| 47 int get_dictionary_notifications_; |
| 48 GURL last_dictionary_request_url_; |
| 49 GURL last_dictionary_url_; |
| 50 }; |
| 51 |
28 class SdchManagerTest : public testing::Test { | 52 class SdchManagerTest : public testing::Test { |
29 protected: | 53 protected: |
30 SdchManagerTest() | 54 SdchManagerTest() |
31 : sdch_manager_(new SdchManager), | 55 : sdch_manager_(new SdchManager), |
32 default_support_(false), | 56 default_support_(false), |
33 default_https_support_(false) { | 57 default_https_support_(false) { |
34 default_support_ = sdch_manager_->sdch_enabled(); | 58 default_support_ = sdch_manager_->sdch_enabled(); |
35 default_https_support_ = sdch_manager_->secure_scheme_supported(); | 59 default_https_support_ = sdch_manager_->secure_scheme_supported(); |
36 } | 60 } |
37 | 61 |
| 62 virtual ~SdchManagerTest() {} |
| 63 |
38 SdchManager* sdch_manager() { return sdch_manager_.get(); } | 64 SdchManager* sdch_manager() { return sdch_manager_.get(); } |
39 | 65 |
40 // Reset globals back to default state. | 66 // Reset globals back to default state. |
41 void TearDown() override { | 67 void TearDown() override { |
42 SdchManager::EnableSdchSupport(default_support_); | 68 SdchManager::EnableSdchSupport(default_support_); |
43 SdchManager::EnableSecureSchemeSupport(default_https_support_); | 69 SdchManager::EnableSecureSchemeSupport(default_https_support_); |
44 } | 70 } |
45 | 71 |
46 // Attempt to add a dictionary to the manager and probe for success or | 72 // Attempt to add a dictionary to the manager and probe for success or |
47 // failure. | 73 // failure. |
(...skipping 466 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
514 server_hash_1, | 540 server_hash_1, |
515 GURL("http://" + dictionary_domain_1 + "/random_url"), | 541 GURL("http://" + dictionary_domain_1 + "/random_url"), |
516 &dictionary); | 542 &dictionary); |
517 EXPECT_FALSE(dictionary.get()); | 543 EXPECT_FALSE(dictionary.get()); |
518 } | 544 } |
519 | 545 |
520 TEST_F(SdchManagerTest, HttpsCorrectlySupported) { | 546 TEST_F(SdchManagerTest, HttpsCorrectlySupported) { |
521 GURL url("http://www.google.com"); | 547 GURL url("http://www.google.com"); |
522 GURL secure_url("https://www.google.com"); | 548 GURL secure_url("https://www.google.com"); |
523 | 549 |
524 #if !defined(OS_IOS) | |
525 // Workaround for http://crbug.com/418975; remove when fixed. | |
526 bool expect_https_support = true; | 550 bool expect_https_support = true; |
527 #else | |
528 bool expect_https_support = false; | |
529 #endif | |
530 | 551 |
531 EXPECT_TRUE(sdch_manager()->IsInSupportedDomain(url)); | 552 EXPECT_TRUE(sdch_manager()->IsInSupportedDomain(url)); |
532 EXPECT_EQ(expect_https_support, | 553 EXPECT_EQ(expect_https_support, |
533 sdch_manager()->IsInSupportedDomain(secure_url)); | 554 sdch_manager()->IsInSupportedDomain(secure_url)); |
534 | 555 |
535 SdchManager::EnableSecureSchemeSupport(!expect_https_support); | 556 SdchManager::EnableSecureSchemeSupport(!expect_https_support); |
536 EXPECT_TRUE(sdch_manager()->IsInSupportedDomain(url)); | 557 EXPECT_TRUE(sdch_manager()->IsInSupportedDomain(url)); |
537 EXPECT_NE(expect_https_support, | 558 EXPECT_NE(expect_https_support, |
538 sdch_manager()->IsInSupportedDomain(secure_url)); | 559 sdch_manager()->IsInSupportedDomain(secure_url)); |
539 } | 560 } |
(...skipping 25 matching lines...) Expand all Loading... |
565 | 586 |
566 dictionary = NULL; | 587 dictionary = NULL; |
567 sdch_manager()->GetVcdiffDictionary( | 588 sdch_manager()->GetVcdiffDictionary( |
568 server_hash, | 589 server_hash, |
569 GURL("http://" + dictionary_domain + "/random_url"), | 590 GURL("http://" + dictionary_domain + "/random_url"), |
570 &dictionary); | 591 &dictionary); |
571 EXPECT_FALSE(dictionary.get()); | 592 EXPECT_FALSE(dictionary.get()); |
572 EXPECT_TRUE(sdch_manager()->IsInSupportedDomain(blacklist_url)); | 593 EXPECT_TRUE(sdch_manager()->IsInSupportedDomain(blacklist_url)); |
573 } | 594 } |
574 | 595 |
575 #else | 596 TEST_F(SdchManagerTest, GetDictionaryNotification) { |
| 597 GURL test_request_gurl(GURL("http://www.example.com/data")); |
| 598 GURL test_dictionary_gurl(GURL("http://www.example.com/dict")); |
| 599 MockSdchObserver observer; |
| 600 sdch_manager()->AddObserver(&observer); |
576 | 601 |
577 TEST(SdchManagerTest, SdchOffByDefault) { | 602 EXPECT_EQ(0, observer.get_dictionary_notifications()); |
578 GURL google_url("http://www.google.com"); | 603 sdch_manager()->OnGetDictionary(test_request_gurl, test_dictionary_gurl); |
579 SdchManager* sdch_manager(new SdchManager); | 604 EXPECT_EQ(1, observer.get_dictionary_notifications()); |
| 605 EXPECT_EQ(test_request_gurl, observer.last_dictionary_request_url()); |
| 606 EXPECT_EQ(test_dictionary_gurl, observer.last_dictionary_url()); |
580 | 607 |
581 EXPECT_FALSE(sdch_manager->IsInSupportedDomain(google_url)); | 608 sdch_manager()->RemoveObserver(&observer); |
582 SdchManager::EnableSdchSupport(true); | 609 sdch_manager()->OnGetDictionary(test_request_gurl, test_dictionary_gurl); |
583 EXPECT_TRUE(sdch_manager->IsInSupportedDomain(google_url)); | 610 EXPECT_EQ(1, observer.get_dictionary_notifications()); |
| 611 EXPECT_EQ(test_request_gurl, observer.last_dictionary_request_url()); |
| 612 EXPECT_EQ(test_dictionary_gurl, observer.last_dictionary_url()); |
584 } | 613 } |
585 | 614 |
586 #endif // !defined(OS_IOS) | |
587 | |
588 } // namespace net | 615 } // namespace net |
OLD | NEW |