Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/extensions/api/declarative_webrequest/webrequest_rules_ registry.h" | 5 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_rules_ registry.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 57 } | 57 } |
| 58 | 58 |
| 59 protected: | 59 protected: |
| 60 virtual ~TestWebRequestRulesRegistry() {} | 60 virtual ~TestWebRequestRulesRegistry() {} |
| 61 | 61 |
| 62 virtual base::Time GetExtensionInstallationTime( | 62 virtual base::Time GetExtensionInstallationTime( |
| 63 const std::string& extension_id) const OVERRIDE { | 63 const std::string& extension_id) const OVERRIDE { |
| 64 if (extension_id == kExtensionId) | 64 if (extension_id == kExtensionId) |
| 65 return base::Time() + base::TimeDelta::FromDays(1); | 65 return base::Time() + base::TimeDelta::FromDays(1); |
| 66 else if (extension_id == kExtensionId2) | 66 else if (extension_id == kExtensionId2) |
| 67 return base::Time() + base::TimeDelta::FromDays(2); | 67 return base::Time() + base::TimeDelta::FromDays(2); |
|
vabr (Chromium)
2013/04/26 09:58:52
The installation times are now set during SetUp wh
| |
| 68 else | 68 else |
| 69 return base::Time(); | 69 return base::Time(); |
| 70 } | 70 } |
| 71 | 71 |
| 72 virtual void ClearCacheOnNavigation() OVERRIDE { | 72 virtual void ClearCacheOnNavigation() OVERRIDE { |
| 73 ++num_clear_cache_calls_; | 73 ++num_clear_cache_calls_; |
| 74 } | 74 } |
| 75 | 75 |
| 76 private: | 76 private: |
| 77 int num_clear_cache_calls_; | 77 int num_clear_cache_calls_; |
| (...skipping 537 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 615 SCOPED_TRACE(testing::Message("i = ") << i << ", rule id = " | 615 SCOPED_TRACE(testing::Message("i = ") << i << ", rule id = " |
| 616 << matchingRuleIds[i]); | 616 << matchingRuleIds[i]); |
| 617 // Make sure that the right rule succeeded. | 617 // Make sure that the right rule succeeded. |
| 618 EXPECT_EQ(1u, matches.size()); | 618 EXPECT_EQ(1u, matches.size()); |
| 619 EXPECT_EQ(WebRequestRule::GlobalRuleId(std::make_pair(kExtensionId, | 619 EXPECT_EQ(WebRequestRule::GlobalRuleId(std::make_pair(kExtensionId, |
| 620 matchingRuleIds[i])), | 620 matchingRuleIds[i])), |
| 621 (*matches.begin())->id()); | 621 (*matches.begin())->id()); |
| 622 } | 622 } |
| 623 } | 623 } |
| 624 | 624 |
| 625 TEST_F(WebRequestRulesRegistryTest, CheckConsistency) { | |
| 626 // The contentType condition can only be evaluated during ON_HEADERS_RECEIVED | |
| 627 // but the redirect action can only be executed during ON_BEFORE_REQUEST. | |
| 628 // Therefore, this is an inconsistent rule that needs to be flagged. | |
| 629 const char kRule[] = | |
| 630 "{ \n" | |
| 631 " \"id\": \"rule1\", \n" | |
| 632 " \"conditions\": [ \n" | |
| 633 " { \n" | |
| 634 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n" | |
| 635 " \"url\": {\"hostSuffix\": \"foo.com\"}, \n" | |
| 636 " \"contentType\": [\"image/jpeg\"] \n" | |
| 637 " } \n" | |
| 638 " ], \n" | |
| 639 " \"actions\": [ \n" | |
| 640 " { \n" | |
| 641 " \"instanceType\": \"declarativeWebRequest.RedirectRequest\",\n" | |
| 642 " \"redirectUrl\": \"http://bar.com\" \n" | |
| 643 " } \n" | |
| 644 " ], \n" | |
| 645 " \"priority\": 200 \n" | |
| 646 "} "; | |
| 647 | |
| 648 scoped_ptr<Value> value(base::JSONReader::Read(kRule)); | |
| 649 ASSERT_TRUE(value.get()); | |
| 650 | |
| 651 std::vector<linked_ptr<RulesRegistry::Rule> > rules; | |
| 652 rules.push_back(make_linked_ptr(new RulesRegistry::Rule)); | |
| 653 ASSERT_TRUE(RulesRegistry::Rule::Populate(*value, rules.back().get())); | |
| 654 | |
| 655 scoped_refptr<WebRequestRulesRegistry> registry( | |
| 656 new TestWebRequestRulesRegistry()); | |
| 657 | |
| 658 URLMatcher matcher; | |
| 659 std::string error = registry->AddRulesImpl(kExtensionId, rules); | |
| 660 EXPECT_THAT(error, HasSubstr("no time in the request life-cycle")); | |
| 661 EXPECT_TRUE(registry->IsEmpty()); | |
| 662 } | |
| 663 | |
| 664 TEST_F(WebRequestRulesRegistryTest, CheckOriginAndPathRegEx) { | 625 TEST_F(WebRequestRulesRegistryTest, CheckOriginAndPathRegEx) { |
| 665 const char kRule[] = | 626 const char kRule[] = |
| 666 "{ \n" | 627 "{ \n" |
| 667 " \"id\": \"rule1\", \n" | 628 " \"id\": \"rule1\", \n" |
| 668 " \"conditions\": [ \n" | 629 " \"conditions\": [ \n" |
| 669 " { \n" | 630 " { \n" |
| 670 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n" | 631 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n" |
| 671 " \"url\": {\"originAndPathMatches\": \"fo+.com\"} \n" | 632 " \"url\": {\"originAndPathMatches\": \"fo+.com\"} \n" |
| 672 " } \n" | 633 " } \n" |
| 673 " ], \n" | 634 " ], \n" |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 706 | 667 |
| 707 // This is a correct match. | 668 // This is a correct match. |
| 708 GURL url2("http://foo.com/index.html"); | 669 GURL url2("http://foo.com/index.html"); |
| 709 net::TestURLRequest request2(url2, NULL, &context, NULL); | 670 net::TestURLRequest request2(url2, NULL, &context, NULL); |
| 710 WebRequestData request_data2(&request2, ON_BEFORE_REQUEST); | 671 WebRequestData request_data2(&request2, ON_BEFORE_REQUEST); |
| 711 deltas = registry->CreateDeltas(NULL, request_data2, false); | 672 deltas = registry->CreateDeltas(NULL, request_data2, false); |
| 712 EXPECT_EQ(1u, deltas.size()); | 673 EXPECT_EQ(1u, deltas.size()); |
| 713 } | 674 } |
| 714 | 675 |
| 715 } // namespace extensions | 676 } // namespace extensions |
| OLD | NEW |