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/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
14 #include "base/stl_util.h" | 14 #include "base/stl_util.h" |
15 #include "base/test/values_test_util.h" | 15 #include "base/test/values_test_util.h" |
16 #include "base/values.h" | 16 #include "base/values.h" |
17 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_consta
nts.h" | 17 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_consta
nts.h" |
18 #include "chrome/browser/extensions/api/web_request/web_request_api_helpers.h" | 18 #include "chrome/browser/extensions/api/web_request/web_request_api_helpers.h" |
19 #include "chrome/common/extensions/extension_test_util.h" | 19 #include "chrome/common/extensions/extension_test_util.h" |
20 #include "content/public/test/test_browser_thread.h" | 20 #include "content/public/test/test_browser_thread.h" |
21 #include "extensions/common/matcher/url_matcher_constants.h" | 21 #include "extensions/common/matcher/url_matcher_constants.h" |
| 22 #include "net/base/request_priority.h" |
22 #include "net/url_request/url_request_test_util.h" | 23 #include "net/url_request/url_request_test_util.h" |
23 #include "testing/gmock/include/gmock/gmock.h" | 24 #include "testing/gmock/include/gmock/gmock.h" |
24 #include "testing/gtest/include/gtest/gtest-message.h" | 25 #include "testing/gtest/include/gtest/gtest-message.h" |
25 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
26 | 27 |
27 namespace { | 28 namespace { |
28 const char kExtensionId[] = "ext1"; | 29 const char kExtensionId[] = "ext1"; |
29 const char kExtensionId2[] = "ext2"; | 30 const char kExtensionId2[] = "ext2"; |
30 const char kRuleId1[] = "rule1"; | 31 const char kRuleId1[] = "rule1"; |
31 const char kRuleId2[] = "rule2"; | 32 const char kRuleId2[] = "rule2"; |
(...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
270 rules.push_back(CreateRule2()); | 271 rules.push_back(CreateRule2()); |
271 | 272 |
272 error = registry->AddRules(kExtensionId, rules); | 273 error = registry->AddRules(kExtensionId, rules); |
273 EXPECT_EQ("", error); | 274 EXPECT_EQ("", error); |
274 EXPECT_EQ(1, registry->num_clear_cache_calls()); | 275 EXPECT_EQ(1, registry->num_clear_cache_calls()); |
275 | 276 |
276 std::set<const WebRequestRule*> matches; | 277 std::set<const WebRequestRule*> matches; |
277 | 278 |
278 GURL http_url("http://www.example.com"); | 279 GURL http_url("http://www.example.com"); |
279 net::TestURLRequestContext context; | 280 net::TestURLRequestContext context; |
280 net::TestURLRequest http_request(http_url, NULL, &context, NULL); | 281 net::TestURLRequest http_request( |
| 282 http_url, net::DEFAULT_PRIORITY, NULL, &context); |
281 WebRequestData request_data(&http_request, ON_BEFORE_REQUEST); | 283 WebRequestData request_data(&http_request, ON_BEFORE_REQUEST); |
282 matches = registry->GetMatches(request_data); | 284 matches = registry->GetMatches(request_data); |
283 EXPECT_EQ(2u, matches.size()); | 285 EXPECT_EQ(2u, matches.size()); |
284 | 286 |
285 std::set<WebRequestRule::GlobalRuleId> matches_ids; | 287 std::set<WebRequestRule::GlobalRuleId> matches_ids; |
286 for (std::set<const WebRequestRule*>::const_iterator it = matches.begin(); | 288 for (std::set<const WebRequestRule*>::const_iterator it = matches.begin(); |
287 it != matches.end(); ++it) | 289 it != matches.end(); ++it) |
288 matches_ids.insert((*it)->id()); | 290 matches_ids.insert((*it)->id()); |
289 EXPECT_TRUE(ContainsKey(matches_ids, std::make_pair(kExtensionId, kRuleId1))); | 291 EXPECT_TRUE(ContainsKey(matches_ids, std::make_pair(kExtensionId, kRuleId1))); |
290 EXPECT_TRUE(ContainsKey(matches_ids, std::make_pair(kExtensionId, kRuleId2))); | 292 EXPECT_TRUE(ContainsKey(matches_ids, std::make_pair(kExtensionId, kRuleId2))); |
291 | 293 |
292 GURL foobar_url("http://www.foobar.com"); | 294 GURL foobar_url("http://www.foobar.com"); |
293 net::TestURLRequest foobar_request(foobar_url, NULL, &context, NULL); | 295 net::TestURLRequest foobar_request( |
| 296 foobar_url, net::DEFAULT_PRIORITY, NULL, &context); |
294 request_data.request = &foobar_request; | 297 request_data.request = &foobar_request; |
295 matches = registry->GetMatches(request_data); | 298 matches = registry->GetMatches(request_data); |
296 EXPECT_EQ(1u, matches.size()); | 299 EXPECT_EQ(1u, matches.size()); |
297 WebRequestRule::GlobalRuleId expected_pair = | 300 WebRequestRule::GlobalRuleId expected_pair = |
298 std::make_pair(kExtensionId, kRuleId2); | 301 std::make_pair(kExtensionId, kRuleId2); |
299 EXPECT_EQ(expected_pair, (*matches.begin())->id()); | 302 EXPECT_EQ(expected_pair, (*matches.begin())->id()); |
300 } | 303 } |
301 | 304 |
302 TEST_F(WebRequestRulesRegistryTest, RemoveRulesImpl) { | 305 TEST_F(WebRequestRulesRegistryTest, RemoveRulesImpl) { |
303 scoped_refptr<TestWebRequestRulesRegistry> registry( | 306 scoped_refptr<TestWebRequestRulesRegistry> registry( |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
409 error = registry->AddRules(kExtensionId, rules_to_add_1); | 412 error = registry->AddRules(kExtensionId, rules_to_add_1); |
410 EXPECT_EQ("", error); | 413 EXPECT_EQ("", error); |
411 | 414 |
412 std::vector<linked_ptr<RulesRegistry::Rule> > rules_to_add_2(1); | 415 std::vector<linked_ptr<RulesRegistry::Rule> > rules_to_add_2(1); |
413 rules_to_add_2[0] = CreateRedirectRule("http://www.bar.com"); | 416 rules_to_add_2[0] = CreateRedirectRule("http://www.bar.com"); |
414 error = registry->AddRules(kExtensionId2, rules_to_add_2); | 417 error = registry->AddRules(kExtensionId2, rules_to_add_2); |
415 EXPECT_EQ("", error); | 418 EXPECT_EQ("", error); |
416 | 419 |
417 GURL url("http://www.google.com"); | 420 GURL url("http://www.google.com"); |
418 net::TestURLRequestContext context; | 421 net::TestURLRequestContext context; |
419 net::TestURLRequest request(url, NULL, &context, NULL); | 422 net::TestURLRequest request(url, net::DEFAULT_PRIORITY, NULL, &context); |
420 WebRequestData request_data(&request, ON_BEFORE_REQUEST); | 423 WebRequestData request_data(&request, ON_BEFORE_REQUEST); |
421 std::list<LinkedPtrEventResponseDelta> deltas = | 424 std::list<LinkedPtrEventResponseDelta> deltas = |
422 registry->CreateDeltas(NULL, request_data, false); | 425 registry->CreateDeltas(NULL, request_data, false); |
423 | 426 |
424 // The second extension is installed later and will win for this reason | 427 // The second extension is installed later and will win for this reason |
425 // in conflict resolution. | 428 // in conflict resolution. |
426 ASSERT_EQ(2u, deltas.size()); | 429 ASSERT_EQ(2u, deltas.size()); |
427 deltas.sort(&helpers::InDecreasingExtensionInstallationTimeOrder); | 430 deltas.sort(&helpers::InDecreasingExtensionInstallationTimeOrder); |
428 | 431 |
429 std::list<LinkedPtrEventResponseDelta>::iterator i = deltas.begin(); | 432 std::list<LinkedPtrEventResponseDelta>::iterator i = deltas.begin(); |
(...skipping 27 matching lines...) Expand all Loading... |
457 error = registry->AddRules(kExtensionId2, rules_to_add_2); | 460 error = registry->AddRules(kExtensionId2, rules_to_add_2); |
458 EXPECT_EQ("", error); | 461 EXPECT_EQ("", error); |
459 | 462 |
460 std::vector<linked_ptr<RulesRegistry::Rule> > rules_to_add_3(1); | 463 std::vector<linked_ptr<RulesRegistry::Rule> > rules_to_add_3(1); |
461 rules_to_add_3[0] = CreateIgnoreRule(); | 464 rules_to_add_3[0] = CreateIgnoreRule(); |
462 error = registry->AddRules(kExtensionId, rules_to_add_3); | 465 error = registry->AddRules(kExtensionId, rules_to_add_3); |
463 EXPECT_EQ("", error); | 466 EXPECT_EQ("", error); |
464 | 467 |
465 GURL url("http://www.google.com/index.html"); | 468 GURL url("http://www.google.com/index.html"); |
466 net::TestURLRequestContext context; | 469 net::TestURLRequestContext context; |
467 net::TestURLRequest request(url, NULL, &context, NULL); | 470 net::TestURLRequest request(url, net::DEFAULT_PRIORITY, NULL, &context); |
468 WebRequestData request_data(&request, ON_BEFORE_REQUEST); | 471 WebRequestData request_data(&request, ON_BEFORE_REQUEST); |
469 std::list<LinkedPtrEventResponseDelta> deltas = | 472 std::list<LinkedPtrEventResponseDelta> deltas = |
470 registry->CreateDeltas(NULL, request_data, false); | 473 registry->CreateDeltas(NULL, request_data, false); |
471 | 474 |
472 // The redirect by the first extension is ignored due to the ignore rule. | 475 // The redirect by the first extension is ignored due to the ignore rule. |
473 ASSERT_EQ(1u, deltas.size()); | 476 ASSERT_EQ(1u, deltas.size()); |
474 LinkedPtrEventResponseDelta effective_rule = *(deltas.begin()); | 477 LinkedPtrEventResponseDelta effective_rule = *(deltas.begin()); |
475 | 478 |
476 EXPECT_EQ(kExtensionId2, effective_rule->extension_id); | 479 EXPECT_EQ(kExtensionId2, effective_rule->extension_id); |
477 EXPECT_EQ(base::Time() + base::TimeDelta::FromDays(2), | 480 EXPECT_EQ(base::Time() + base::TimeDelta::FromDays(2), |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
530 ASSERT_TRUE(RulesRegistry::Rule::Populate(*value2, rules[1].get())); | 533 ASSERT_TRUE(RulesRegistry::Rule::Populate(*value2, rules[1].get())); |
531 | 534 |
532 scoped_refptr<WebRequestRulesRegistry> registry( | 535 scoped_refptr<WebRequestRulesRegistry> registry( |
533 new TestWebRequestRulesRegistry(extension_info_map_)); | 536 new TestWebRequestRulesRegistry(extension_info_map_)); |
534 std::string error = registry->AddRulesImpl(kExtensionId, rules); | 537 std::string error = registry->AddRulesImpl(kExtensionId, rules); |
535 EXPECT_EQ("", error); | 538 EXPECT_EQ("", error); |
536 EXPECT_FALSE(registry->IsEmpty()); | 539 EXPECT_FALSE(registry->IsEmpty()); |
537 | 540 |
538 GURL url("http://www.foo.com/test"); | 541 GURL url("http://www.foo.com/test"); |
539 net::TestURLRequestContext context; | 542 net::TestURLRequestContext context; |
540 net::TestURLRequest request(url, NULL, &context, NULL); | 543 net::TestURLRequest request(url, net::DEFAULT_PRIORITY, NULL, &context); |
541 WebRequestData request_data(&request, ON_BEFORE_REQUEST); | 544 WebRequestData request_data(&request, ON_BEFORE_REQUEST); |
542 std::list<LinkedPtrEventResponseDelta> deltas = | 545 std::list<LinkedPtrEventResponseDelta> deltas = |
543 registry->CreateDeltas(NULL, request_data, false); | 546 registry->CreateDeltas(NULL, request_data, false); |
544 | 547 |
545 // The redirect by the redirect rule is ignored due to the ignore rule. | 548 // The redirect by the redirect rule is ignored due to the ignore rule. |
546 std::set<const WebRequestRule*> matches = registry->GetMatches(request_data); | 549 std::set<const WebRequestRule*> matches = registry->GetMatches(request_data); |
547 EXPECT_EQ(2u, matches.size()); | 550 EXPECT_EQ(2u, matches.size()); |
548 ASSERT_EQ(0u, deltas.size()); | 551 ASSERT_EQ(0u, deltas.size()); |
549 } | 552 } |
550 | 553 |
(...skipping 28 matching lines...) Expand all Loading... |
579 rules.push_back(CreateCancellingRule(kRuleId3, attributes)); | 582 rules.push_back(CreateCancellingRule(kRuleId3, attributes)); |
580 | 583 |
581 error = registry->AddRules(kExtensionId, rules); | 584 error = registry->AddRules(kExtensionId, rules); |
582 EXPECT_EQ("", error); | 585 EXPECT_EQ("", error); |
583 EXPECT_EQ(1, registry->num_clear_cache_calls()); | 586 EXPECT_EQ(1, registry->num_clear_cache_calls()); |
584 | 587 |
585 std::set<const WebRequestRule*> matches; | 588 std::set<const WebRequestRule*> matches; |
586 | 589 |
587 GURL http_url("http://www.example.com"); | 590 GURL http_url("http://www.example.com"); |
588 net::TestURLRequestContext context; | 591 net::TestURLRequestContext context; |
589 net::TestURLRequest http_request(http_url, NULL, &context, NULL); | 592 net::TestURLRequest http_request( |
| 593 http_url, net::DEFAULT_PRIORITY, NULL, &context); |
590 WebRequestData request_data(&http_request, ON_BEFORE_REQUEST); | 594 WebRequestData request_data(&http_request, ON_BEFORE_REQUEST); |
591 matches = registry->GetMatches(request_data); | 595 matches = registry->GetMatches(request_data); |
592 EXPECT_EQ(1u, matches.size()); | 596 EXPECT_EQ(1u, matches.size()); |
593 WebRequestRule::GlobalRuleId expected_pair = std::make_pair(kExtensionId, | 597 WebRequestRule::GlobalRuleId expected_pair = std::make_pair(kExtensionId, |
594 kRuleId3); | 598 kRuleId3); |
595 EXPECT_EQ(expected_pair, (*matches.begin())->id()); | 599 EXPECT_EQ(expected_pair, (*matches.begin())->id()); |
596 } | 600 } |
597 | 601 |
598 // Test that the url and firstPartyForCookiesUrl attributes are evaluated | 602 // Test that the url and firstPartyForCookiesUrl attributes are evaluated |
599 // against corresponding URLs. Tested on requests where these URLs actually | 603 // against corresponding URLs. Tested on requests where these URLs actually |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
635 // Which rules should match in subsequent test iterations. | 639 // Which rules should match in subsequent test iterations. |
636 const char* matchingRuleIds[] = { kRuleId1, kRuleId2 }; | 640 const char* matchingRuleIds[] = { kRuleId1, kRuleId2 }; |
637 COMPILE_ASSERT(arraysize(urls) == arraysize(firstPartyUrls), | 641 COMPILE_ASSERT(arraysize(urls) == arraysize(firstPartyUrls), |
638 urls_and_firstPartyUrls_need_to_have_the_same_size); | 642 urls_and_firstPartyUrls_need_to_have_the_same_size); |
639 COMPILE_ASSERT(arraysize(urls) == arraysize(matchingRuleIds), | 643 COMPILE_ASSERT(arraysize(urls) == arraysize(matchingRuleIds), |
640 urls_and_matchingRuleIds_need_to_have_the_same_size); | 644 urls_and_matchingRuleIds_need_to_have_the_same_size); |
641 net::TestURLRequestContext context; | 645 net::TestURLRequestContext context; |
642 | 646 |
643 for (size_t i = 0; i < arraysize(matchingRuleIds); ++i) { | 647 for (size_t i = 0; i < arraysize(matchingRuleIds); ++i) { |
644 // Construct the inputs. | 648 // Construct the inputs. |
645 net::TestURLRequest http_request(urls[i], NULL, &context, NULL); | 649 net::TestURLRequest http_request( |
| 650 urls[i], net::DEFAULT_PRIORITY, NULL, &context); |
646 WebRequestData request_data(&http_request, ON_BEFORE_REQUEST); | 651 WebRequestData request_data(&http_request, ON_BEFORE_REQUEST); |
647 http_request.set_first_party_for_cookies(firstPartyUrls[i]); | 652 http_request.set_first_party_for_cookies(firstPartyUrls[i]); |
648 // Now run both rules on the input. | 653 // Now run both rules on the input. |
649 matches = registry->GetMatches(request_data); | 654 matches = registry->GetMatches(request_data); |
650 SCOPED_TRACE(testing::Message("i = ") << i << ", rule id = " | 655 SCOPED_TRACE(testing::Message("i = ") << i << ", rule id = " |
651 << matchingRuleIds[i]); | 656 << matchingRuleIds[i]); |
652 // Make sure that the right rule succeeded. | 657 // Make sure that the right rule succeeded. |
653 EXPECT_EQ(1u, matches.size()); | 658 EXPECT_EQ(1u, matches.size()); |
654 EXPECT_EQ(WebRequestRule::GlobalRuleId(std::make_pair(kExtensionId, | 659 EXPECT_EQ(WebRequestRule::GlobalRuleId(std::make_pair(kExtensionId, |
655 matchingRuleIds[i])), | 660 matchingRuleIds[i])), |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
781 | 786 |
782 URLMatcher matcher; | 787 URLMatcher matcher; |
783 std::string error = registry->AddRulesImpl(kExtensionId, rules); | 788 std::string error = registry->AddRulesImpl(kExtensionId, rules); |
784 EXPECT_EQ("", error); | 789 EXPECT_EQ("", error); |
785 | 790 |
786 net::TestURLRequestContext context; | 791 net::TestURLRequestContext context; |
787 std::list<LinkedPtrEventResponseDelta> deltas; | 792 std::list<LinkedPtrEventResponseDelta> deltas; |
788 | 793 |
789 // No match because match is in the query parameter. | 794 // No match because match is in the query parameter. |
790 GURL url1("http://bar.com/index.html?foo.com"); | 795 GURL url1("http://bar.com/index.html?foo.com"); |
791 net::TestURLRequest request1(url1, NULL, &context, NULL); | 796 net::TestURLRequest request1(url1, net::DEFAULT_PRIORITY, NULL, &context); |
792 WebRequestData request_data1(&request1, ON_BEFORE_REQUEST); | 797 WebRequestData request_data1(&request1, ON_BEFORE_REQUEST); |
793 deltas = registry->CreateDeltas(NULL, request_data1, false); | 798 deltas = registry->CreateDeltas(NULL, request_data1, false); |
794 EXPECT_EQ(0u, deltas.size()); | 799 EXPECT_EQ(0u, deltas.size()); |
795 | 800 |
796 // This is a correct match. | 801 // This is a correct match. |
797 GURL url2("http://foo.com/index.html"); | 802 GURL url2("http://foo.com/index.html"); |
798 net::TestURLRequest request2(url2, NULL, &context, NULL); | 803 net::TestURLRequest request2(url2, net::DEFAULT_PRIORITY, NULL, &context); |
799 WebRequestData request_data2(&request2, ON_BEFORE_REQUEST); | 804 WebRequestData request_data2(&request2, ON_BEFORE_REQUEST); |
800 deltas = registry->CreateDeltas(NULL, request_data2, false); | 805 deltas = registry->CreateDeltas(NULL, request_data2, false); |
801 EXPECT_EQ(1u, deltas.size()); | 806 EXPECT_EQ(1u, deltas.size()); |
802 } | 807 } |
803 | 808 |
804 } // namespace extensions | 809 } // namespace extensions |
OLD | NEW |