| 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" |
| 11 #include "base/json/json_reader.h" | 11 #include "base/json/json_reader.h" |
| 12 #include "base/memory/linked_ptr.h" | 12 #include "base/memory/linked_ptr.h" |
| 13 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
| 14 #include "base/stl_util.h" | 15 #include "base/stl_util.h" |
| 15 #include "base/test/values_test_util.h" | 16 #include "base/test/values_test_util.h" |
| 16 #include "base/values.h" | 17 #include "base/values.h" |
| 17 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_consta
nts.h" | 18 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_consta
nts.h" |
| 18 #include "chrome/browser/extensions/api/web_request/web_request_api_helpers.h" | 19 #include "chrome/browser/extensions/api/web_request/web_request_api_helpers.h" |
| 19 #include "chrome/common/extensions/extension_test_util.h" | 20 #include "chrome/common/extensions/extension_test_util.h" |
| 20 #include "components/url_matcher/url_matcher_constants.h" | 21 #include "components/url_matcher/url_matcher_constants.h" |
| 21 #include "content/public/test/test_browser_thread.h" | 22 #include "content/public/test/test_browser_thread.h" |
| 22 #include "net/base/request_priority.h" | 23 #include "net/base/request_priority.h" |
| 24 #include "net/url_request/url_request.h" |
| 23 #include "net/url_request/url_request_test_util.h" | 25 #include "net/url_request/url_request_test_util.h" |
| 24 #include "testing/gmock/include/gmock/gmock.h" | 26 #include "testing/gmock/include/gmock/gmock.h" |
| 25 #include "testing/gtest/include/gtest/gtest-message.h" | 27 #include "testing/gtest/include/gtest/gtest-message.h" |
| 26 #include "testing/gtest/include/gtest/gtest.h" | 28 #include "testing/gtest/include/gtest/gtest.h" |
| 27 | 29 |
| 28 using base::Value; | 30 using base::Value; |
| 29 using extension_test_util::LoadManifest; | 31 using extension_test_util::LoadManifest; |
| 30 using extension_test_util::LoadManifestUnchecked; | 32 using extension_test_util::LoadManifestUnchecked; |
| 31 using testing::HasSubstr; | 33 using testing::HasSubstr; |
| 32 using url_matcher::URLMatcher; | 34 using url_matcher::URLMatcher; |
| (...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 274 rules.push_back(CreateRule2()); | 276 rules.push_back(CreateRule2()); |
| 275 | 277 |
| 276 error = registry->AddRules(kExtensionId, rules); | 278 error = registry->AddRules(kExtensionId, rules); |
| 277 EXPECT_EQ("", error); | 279 EXPECT_EQ("", error); |
| 278 EXPECT_EQ(1, registry->num_clear_cache_calls()); | 280 EXPECT_EQ(1, registry->num_clear_cache_calls()); |
| 279 | 281 |
| 280 std::set<const WebRequestRule*> matches; | 282 std::set<const WebRequestRule*> matches; |
| 281 | 283 |
| 282 GURL http_url("http://www.example.com"); | 284 GURL http_url("http://www.example.com"); |
| 283 net::TestURLRequestContext context; | 285 net::TestURLRequestContext context; |
| 284 net::TestURLRequest http_request( | 286 scoped_ptr<net::URLRequest> http_request(context.CreateRequest( |
| 285 http_url, net::DEFAULT_PRIORITY, NULL, &context); | 287 http_url, net::DEFAULT_PRIORITY, NULL, NULL)); |
| 286 WebRequestData request_data(&http_request, ON_BEFORE_REQUEST); | 288 WebRequestData request_data(http_request.get(), ON_BEFORE_REQUEST); |
| 287 matches = registry->GetMatches(request_data); | 289 matches = registry->GetMatches(request_data); |
| 288 EXPECT_EQ(2u, matches.size()); | 290 EXPECT_EQ(2u, matches.size()); |
| 289 | 291 |
| 290 std::set<WebRequestRule::GlobalRuleId> matches_ids; | 292 std::set<WebRequestRule::GlobalRuleId> matches_ids; |
| 291 for (std::set<const WebRequestRule*>::const_iterator it = matches.begin(); | 293 for (std::set<const WebRequestRule*>::const_iterator it = matches.begin(); |
| 292 it != matches.end(); ++it) | 294 it != matches.end(); ++it) |
| 293 matches_ids.insert((*it)->id()); | 295 matches_ids.insert((*it)->id()); |
| 294 EXPECT_TRUE(ContainsKey(matches_ids, std::make_pair(kExtensionId, kRuleId1))); | 296 EXPECT_TRUE(ContainsKey(matches_ids, std::make_pair(kExtensionId, kRuleId1))); |
| 295 EXPECT_TRUE(ContainsKey(matches_ids, std::make_pair(kExtensionId, kRuleId2))); | 297 EXPECT_TRUE(ContainsKey(matches_ids, std::make_pair(kExtensionId, kRuleId2))); |
| 296 | 298 |
| 297 GURL foobar_url("http://www.foobar.com"); | 299 GURL foobar_url("http://www.foobar.com"); |
| 298 net::TestURLRequest foobar_request( | 300 scoped_ptr<net::URLRequest> foobar_request(context.CreateRequest( |
| 299 foobar_url, net::DEFAULT_PRIORITY, NULL, &context); | 301 foobar_url, net::DEFAULT_PRIORITY, NULL, NULL)); |
| 300 request_data.request = &foobar_request; | 302 request_data.request = foobar_request.get(); |
| 301 matches = registry->GetMatches(request_data); | 303 matches = registry->GetMatches(request_data); |
| 302 EXPECT_EQ(1u, matches.size()); | 304 EXPECT_EQ(1u, matches.size()); |
| 303 WebRequestRule::GlobalRuleId expected_pair = | 305 WebRequestRule::GlobalRuleId expected_pair = |
| 304 std::make_pair(kExtensionId, kRuleId2); | 306 std::make_pair(kExtensionId, kRuleId2); |
| 305 EXPECT_EQ(expected_pair, (*matches.begin())->id()); | 307 EXPECT_EQ(expected_pair, (*matches.begin())->id()); |
| 306 } | 308 } |
| 307 | 309 |
| 308 TEST_F(WebRequestRulesRegistryTest, RemoveRulesImpl) { | 310 TEST_F(WebRequestRulesRegistryTest, RemoveRulesImpl) { |
| 309 scoped_refptr<TestWebRequestRulesRegistry> registry( | 311 scoped_refptr<TestWebRequestRulesRegistry> registry( |
| 310 new TestWebRequestRulesRegistry(extension_info_map_)); | 312 new TestWebRequestRulesRegistry(extension_info_map_)); |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 415 error = registry->AddRules(kExtensionId, rules_to_add_1); | 417 error = registry->AddRules(kExtensionId, rules_to_add_1); |
| 416 EXPECT_EQ("", error); | 418 EXPECT_EQ("", error); |
| 417 | 419 |
| 418 std::vector<linked_ptr<RulesRegistry::Rule> > rules_to_add_2(1); | 420 std::vector<linked_ptr<RulesRegistry::Rule> > rules_to_add_2(1); |
| 419 rules_to_add_2[0] = CreateRedirectRule("http://www.bar.com"); | 421 rules_to_add_2[0] = CreateRedirectRule("http://www.bar.com"); |
| 420 error = registry->AddRules(kExtensionId2, rules_to_add_2); | 422 error = registry->AddRules(kExtensionId2, rules_to_add_2); |
| 421 EXPECT_EQ("", error); | 423 EXPECT_EQ("", error); |
| 422 | 424 |
| 423 GURL url("http://www.google.com"); | 425 GURL url("http://www.google.com"); |
| 424 net::TestURLRequestContext context; | 426 net::TestURLRequestContext context; |
| 425 net::TestURLRequest request(url, net::DEFAULT_PRIORITY, NULL, &context); | 427 scoped_ptr<net::URLRequest> request(context.CreateRequest( |
| 426 WebRequestData request_data(&request, ON_BEFORE_REQUEST); | 428 url, net::DEFAULT_PRIORITY, NULL, NULL)); |
| 429 WebRequestData request_data(request.get(), ON_BEFORE_REQUEST); |
| 427 std::list<LinkedPtrEventResponseDelta> deltas = | 430 std::list<LinkedPtrEventResponseDelta> deltas = |
| 428 registry->CreateDeltas(NULL, request_data, false); | 431 registry->CreateDeltas(NULL, request_data, false); |
| 429 | 432 |
| 430 // The second extension is installed later and will win for this reason | 433 // The second extension is installed later and will win for this reason |
| 431 // in conflict resolution. | 434 // in conflict resolution. |
| 432 ASSERT_EQ(2u, deltas.size()); | 435 ASSERT_EQ(2u, deltas.size()); |
| 433 deltas.sort(&helpers::InDecreasingExtensionInstallationTimeOrder); | 436 deltas.sort(&helpers::InDecreasingExtensionInstallationTimeOrder); |
| 434 | 437 |
| 435 std::list<LinkedPtrEventResponseDelta>::iterator i = deltas.begin(); | 438 std::list<LinkedPtrEventResponseDelta>::iterator i = deltas.begin(); |
| 436 LinkedPtrEventResponseDelta winner = *i++; | 439 LinkedPtrEventResponseDelta winner = *i++; |
| (...skipping 26 matching lines...) Expand all Loading... |
| 463 error = registry->AddRules(kExtensionId2, rules_to_add_2); | 466 error = registry->AddRules(kExtensionId2, rules_to_add_2); |
| 464 EXPECT_EQ("", error); | 467 EXPECT_EQ("", error); |
| 465 | 468 |
| 466 std::vector<linked_ptr<RulesRegistry::Rule> > rules_to_add_3(1); | 469 std::vector<linked_ptr<RulesRegistry::Rule> > rules_to_add_3(1); |
| 467 rules_to_add_3[0] = CreateIgnoreRule(); | 470 rules_to_add_3[0] = CreateIgnoreRule(); |
| 468 error = registry->AddRules(kExtensionId, rules_to_add_3); | 471 error = registry->AddRules(kExtensionId, rules_to_add_3); |
| 469 EXPECT_EQ("", error); | 472 EXPECT_EQ("", error); |
| 470 | 473 |
| 471 GURL url("http://www.google.com/index.html"); | 474 GURL url("http://www.google.com/index.html"); |
| 472 net::TestURLRequestContext context; | 475 net::TestURLRequestContext context; |
| 473 net::TestURLRequest request(url, net::DEFAULT_PRIORITY, NULL, &context); | 476 scoped_ptr<net::URLRequest> request(context.CreateRequest( |
| 474 WebRequestData request_data(&request, ON_BEFORE_REQUEST); | 477 url, net::DEFAULT_PRIORITY, NULL, NULL)); |
| 478 WebRequestData request_data(request.get(), ON_BEFORE_REQUEST); |
| 475 std::list<LinkedPtrEventResponseDelta> deltas = | 479 std::list<LinkedPtrEventResponseDelta> deltas = |
| 476 registry->CreateDeltas(NULL, request_data, false); | 480 registry->CreateDeltas(NULL, request_data, false); |
| 477 | 481 |
| 478 // The redirect by the first extension is ignored due to the ignore rule. | 482 // The redirect by the first extension is ignored due to the ignore rule. |
| 479 ASSERT_EQ(1u, deltas.size()); | 483 ASSERT_EQ(1u, deltas.size()); |
| 480 LinkedPtrEventResponseDelta effective_rule = *(deltas.begin()); | 484 LinkedPtrEventResponseDelta effective_rule = *(deltas.begin()); |
| 481 | 485 |
| 482 EXPECT_EQ(kExtensionId2, effective_rule->extension_id); | 486 EXPECT_EQ(kExtensionId2, effective_rule->extension_id); |
| 483 EXPECT_EQ(base::Time() + base::TimeDelta::FromDays(2), | 487 EXPECT_EQ(base::Time() + base::TimeDelta::FromDays(2), |
| 484 effective_rule->extension_install_time); | 488 effective_rule->extension_install_time); |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 536 ASSERT_TRUE(RulesRegistry::Rule::Populate(*value2, rules[1].get())); | 540 ASSERT_TRUE(RulesRegistry::Rule::Populate(*value2, rules[1].get())); |
| 537 | 541 |
| 538 scoped_refptr<WebRequestRulesRegistry> registry( | 542 scoped_refptr<WebRequestRulesRegistry> registry( |
| 539 new TestWebRequestRulesRegistry(extension_info_map_)); | 543 new TestWebRequestRulesRegistry(extension_info_map_)); |
| 540 std::string error = registry->AddRulesImpl(kExtensionId, rules); | 544 std::string error = registry->AddRulesImpl(kExtensionId, rules); |
| 541 EXPECT_EQ("", error); | 545 EXPECT_EQ("", error); |
| 542 EXPECT_FALSE(registry->IsEmpty()); | 546 EXPECT_FALSE(registry->IsEmpty()); |
| 543 | 547 |
| 544 GURL url("http://www.foo.com/test"); | 548 GURL url("http://www.foo.com/test"); |
| 545 net::TestURLRequestContext context; | 549 net::TestURLRequestContext context; |
| 546 net::TestURLRequest request(url, net::DEFAULT_PRIORITY, NULL, &context); | 550 scoped_ptr<net::URLRequest> request(context.CreateRequest( |
| 547 WebRequestData request_data(&request, ON_BEFORE_REQUEST); | 551 url, net::DEFAULT_PRIORITY, NULL, NULL)); |
| 552 WebRequestData request_data(request.get(), ON_BEFORE_REQUEST); |
| 548 std::list<LinkedPtrEventResponseDelta> deltas = | 553 std::list<LinkedPtrEventResponseDelta> deltas = |
| 549 registry->CreateDeltas(NULL, request_data, false); | 554 registry->CreateDeltas(NULL, request_data, false); |
| 550 | 555 |
| 551 // The redirect by the redirect rule is ignored due to the ignore rule. | 556 // The redirect by the redirect rule is ignored due to the ignore rule. |
| 552 std::set<const WebRequestRule*> matches = registry->GetMatches(request_data); | 557 std::set<const WebRequestRule*> matches = registry->GetMatches(request_data); |
| 553 EXPECT_EQ(2u, matches.size()); | 558 EXPECT_EQ(2u, matches.size()); |
| 554 ASSERT_EQ(0u, deltas.size()); | 559 ASSERT_EQ(0u, deltas.size()); |
| 555 } | 560 } |
| 556 | 561 |
| 557 // Test that rules failing IsFulfilled on their conditions are never returned by | 562 // Test that rules failing IsFulfilled on their conditions are never returned by |
| (...skipping 27 matching lines...) Expand all Loading... |
| 585 rules.push_back(CreateCancellingRule(kRuleId3, attributes)); | 590 rules.push_back(CreateCancellingRule(kRuleId3, attributes)); |
| 586 | 591 |
| 587 error = registry->AddRules(kExtensionId, rules); | 592 error = registry->AddRules(kExtensionId, rules); |
| 588 EXPECT_EQ("", error); | 593 EXPECT_EQ("", error); |
| 589 EXPECT_EQ(1, registry->num_clear_cache_calls()); | 594 EXPECT_EQ(1, registry->num_clear_cache_calls()); |
| 590 | 595 |
| 591 std::set<const WebRequestRule*> matches; | 596 std::set<const WebRequestRule*> matches; |
| 592 | 597 |
| 593 GURL http_url("http://www.example.com"); | 598 GURL http_url("http://www.example.com"); |
| 594 net::TestURLRequestContext context; | 599 net::TestURLRequestContext context; |
| 595 net::TestURLRequest http_request( | 600 scoped_ptr<net::URLRequest> http_request(context.CreateRequest( |
| 596 http_url, net::DEFAULT_PRIORITY, NULL, &context); | 601 http_url, net::DEFAULT_PRIORITY, NULL, NULL)); |
| 597 WebRequestData request_data(&http_request, ON_BEFORE_REQUEST); | 602 WebRequestData request_data(http_request.get(), ON_BEFORE_REQUEST); |
| 598 matches = registry->GetMatches(request_data); | 603 matches = registry->GetMatches(request_data); |
| 599 EXPECT_EQ(1u, matches.size()); | 604 EXPECT_EQ(1u, matches.size()); |
| 600 WebRequestRule::GlobalRuleId expected_pair = std::make_pair(kExtensionId, | 605 WebRequestRule::GlobalRuleId expected_pair = std::make_pair(kExtensionId, |
| 601 kRuleId3); | 606 kRuleId3); |
| 602 EXPECT_EQ(expected_pair, (*matches.begin())->id()); | 607 EXPECT_EQ(expected_pair, (*matches.begin())->id()); |
| 603 } | 608 } |
| 604 | 609 |
| 605 // Test that the url and firstPartyForCookiesUrl attributes are evaluated | 610 // Test that the url and firstPartyForCookiesUrl attributes are evaluated |
| 606 // against corresponding URLs. Tested on requests where these URLs actually | 611 // against corresponding URLs. Tested on requests where these URLs actually |
| 607 // differ. | 612 // differ. |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 642 // Which rules should match in subsequent test iterations. | 647 // Which rules should match in subsequent test iterations. |
| 643 const char* matchingRuleIds[] = { kRuleId1, kRuleId2 }; | 648 const char* matchingRuleIds[] = { kRuleId1, kRuleId2 }; |
| 644 COMPILE_ASSERT(arraysize(urls) == arraysize(firstPartyUrls), | 649 COMPILE_ASSERT(arraysize(urls) == arraysize(firstPartyUrls), |
| 645 urls_and_firstPartyUrls_need_to_have_the_same_size); | 650 urls_and_firstPartyUrls_need_to_have_the_same_size); |
| 646 COMPILE_ASSERT(arraysize(urls) == arraysize(matchingRuleIds), | 651 COMPILE_ASSERT(arraysize(urls) == arraysize(matchingRuleIds), |
| 647 urls_and_matchingRuleIds_need_to_have_the_same_size); | 652 urls_and_matchingRuleIds_need_to_have_the_same_size); |
| 648 net::TestURLRequestContext context; | 653 net::TestURLRequestContext context; |
| 649 | 654 |
| 650 for (size_t i = 0; i < arraysize(matchingRuleIds); ++i) { | 655 for (size_t i = 0; i < arraysize(matchingRuleIds); ++i) { |
| 651 // Construct the inputs. | 656 // Construct the inputs. |
| 652 net::TestURLRequest http_request( | 657 scoped_ptr<net::URLRequest> http_request(context.CreateRequest( |
| 653 urls[i], net::DEFAULT_PRIORITY, NULL, &context); | 658 urls[i], net::DEFAULT_PRIORITY, NULL, NULL)); |
| 654 WebRequestData request_data(&http_request, ON_BEFORE_REQUEST); | 659 WebRequestData request_data(http_request.get(), ON_BEFORE_REQUEST); |
| 655 http_request.set_first_party_for_cookies(firstPartyUrls[i]); | 660 http_request->set_first_party_for_cookies(firstPartyUrls[i]); |
| 656 // Now run both rules on the input. | 661 // Now run both rules on the input. |
| 657 matches = registry->GetMatches(request_data); | 662 matches = registry->GetMatches(request_data); |
| 658 SCOPED_TRACE(testing::Message("i = ") << i << ", rule id = " | 663 SCOPED_TRACE(testing::Message("i = ") << i << ", rule id = " |
| 659 << matchingRuleIds[i]); | 664 << matchingRuleIds[i]); |
| 660 // Make sure that the right rule succeeded. | 665 // Make sure that the right rule succeeded. |
| 661 EXPECT_EQ(1u, matches.size()); | 666 EXPECT_EQ(1u, matches.size()); |
| 662 EXPECT_EQ(WebRequestRule::GlobalRuleId(std::make_pair(kExtensionId, | 667 EXPECT_EQ(WebRequestRule::GlobalRuleId(std::make_pair(kExtensionId, |
| 663 matchingRuleIds[i])), | 668 matchingRuleIds[i])), |
| 664 (*matches.begin())->id()); | 669 (*matches.begin())->id()); |
| 665 } | 670 } |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 791 | 796 |
| 792 URLMatcher matcher; | 797 URLMatcher matcher; |
| 793 std::string error = registry->AddRulesImpl(kExtensionId, rules); | 798 std::string error = registry->AddRulesImpl(kExtensionId, rules); |
| 794 EXPECT_EQ("", error); | 799 EXPECT_EQ("", error); |
| 795 | 800 |
| 796 net::TestURLRequestContext context; | 801 net::TestURLRequestContext context; |
| 797 std::list<LinkedPtrEventResponseDelta> deltas; | 802 std::list<LinkedPtrEventResponseDelta> deltas; |
| 798 | 803 |
| 799 // No match because match is in the query parameter. | 804 // No match because match is in the query parameter. |
| 800 GURL url1("http://bar.com/index.html?foo.com"); | 805 GURL url1("http://bar.com/index.html?foo.com"); |
| 801 net::TestURLRequest request1(url1, net::DEFAULT_PRIORITY, NULL, &context); | 806 scoped_ptr<net::URLRequest> request1(context.CreateRequest( |
| 802 WebRequestData request_data1(&request1, ON_BEFORE_REQUEST); | 807 url1, net::DEFAULT_PRIORITY, NULL, NULL)); |
| 808 WebRequestData request_data1(request1.get(), ON_BEFORE_REQUEST); |
| 803 deltas = registry->CreateDeltas(NULL, request_data1, false); | 809 deltas = registry->CreateDeltas(NULL, request_data1, false); |
| 804 EXPECT_EQ(0u, deltas.size()); | 810 EXPECT_EQ(0u, deltas.size()); |
| 805 | 811 |
| 806 // This is a correct match. | 812 // This is a correct match. |
| 807 GURL url2("http://foo.com/index.html"); | 813 GURL url2("http://foo.com/index.html"); |
| 808 net::TestURLRequest request2(url2, net::DEFAULT_PRIORITY, NULL, &context); | 814 scoped_ptr<net::URLRequest> request2(context.CreateRequest( |
| 809 WebRequestData request_data2(&request2, ON_BEFORE_REQUEST); | 815 url2, net::DEFAULT_PRIORITY, NULL, NULL)); |
| 816 WebRequestData request_data2(request2.get(), ON_BEFORE_REQUEST); |
| 810 deltas = registry->CreateDeltas(NULL, request_data2, false); | 817 deltas = registry->CreateDeltas(NULL, request_data2, false); |
| 811 EXPECT_EQ(1u, deltas.size()); | 818 EXPECT_EQ(1u, deltas.size()); |
| 812 } | 819 } |
| 813 | 820 |
| 814 } // namespace extensions | 821 } // namespace extensions |
| OLD | NEW |