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 <vector> | 7 #include <vector> |
8 | 8 |
| 9 #include "base/json/json_reader.h" |
9 #include "base/memory/linked_ptr.h" | 10 #include "base/memory/linked_ptr.h" |
10 #include "base/message_loop.h" | 11 #include "base/message_loop.h" |
11 #include "base/stl_util.h" | 12 #include "base/stl_util.h" |
12 #include "base/test/values_test_util.h" | 13 #include "base/test/values_test_util.h" |
13 #include "base/values.h" | 14 #include "base/values.h" |
| 15 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_consta
nts.h" |
| 16 #include "chrome/browser/extensions/api/web_request/web_request_api_helpers.h" |
14 #include "chrome/common/extensions/matcher/url_matcher_constants.h" | 17 #include "chrome/common/extensions/matcher/url_matcher_constants.h" |
15 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_consta
nts.h" | |
16 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_rule.h
" | |
17 #include "chrome/browser/extensions/api/web_request/web_request_api_helpers.h" | |
18 #include "content/public/test/test_browser_thread.h" | 18 #include "content/public/test/test_browser_thread.h" |
19 #include "net/url_request/url_request_test_util.h" | 19 #include "net/url_request/url_request_test_util.h" |
| 20 #include "testing/gmock/include/gmock/gmock.h" |
20 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
21 | 22 |
22 namespace { | 23 namespace { |
23 const char kExtensionId[] = "ext1"; | 24 const char kExtensionId[] = "ext1"; |
24 const char kExtensionId2[] = "ext2"; | 25 const char kExtensionId2[] = "ext2"; |
25 const char kRuleId1[] = "rule1"; | 26 const char kRuleId1[] = "rule1"; |
26 const char kRuleId2[] = "rule2"; | 27 const char kRuleId2[] = "rule2"; |
27 const char kRuleId3[] = "rule3"; | 28 const char kRuleId3[] = "rule3"; |
28 const char kRuleId4[] = "rule4"; | 29 const char kRuleId4[] = "rule4"; |
29 } // namespace | 30 } // namespace |
30 | 31 |
31 namespace extensions { | 32 namespace extensions { |
32 | 33 |
| 34 using testing::HasSubstr; |
| 35 |
33 namespace helpers = extension_web_request_api_helpers; | 36 namespace helpers = extension_web_request_api_helpers; |
34 namespace keys = declarative_webrequest_constants; | 37 namespace keys = declarative_webrequest_constants; |
35 namespace keys2 = url_matcher_constants; | 38 namespace keys2 = url_matcher_constants; |
36 | 39 |
37 class TestWebRequestRulesRegistry : public WebRequestRulesRegistry { | 40 class TestWebRequestRulesRegistry : public WebRequestRulesRegistry { |
38 public: | 41 public: |
39 TestWebRequestRulesRegistry() | 42 TestWebRequestRulesRegistry() |
40 : WebRequestRulesRegistry(NULL, NULL), | 43 : WebRequestRulesRegistry(NULL, NULL), |
41 num_clear_cache_calls_(0) {} | 44 num_clear_cache_calls_(0) {} |
42 | 45 |
(...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
310 error = registry->AddRules(kExtensionId, rules); | 313 error = registry->AddRules(kExtensionId, rules); |
311 EXPECT_EQ("", error); | 314 EXPECT_EQ("", error); |
312 EXPECT_EQ(1, registry->num_clear_cache_calls()); | 315 EXPECT_EQ(1, registry->num_clear_cache_calls()); |
313 | 316 |
314 std::set<const WebRequestRule*> matches; | 317 std::set<const WebRequestRule*> matches; |
315 | 318 |
316 GURL http_url("http://www.example.com"); | 319 GURL http_url("http://www.example.com"); |
317 net::TestURLRequestContext context; | 320 net::TestURLRequestContext context; |
318 net::TestURLRequest http_request(http_url, NULL, &context); | 321 net::TestURLRequest http_request(http_url, NULL, &context); |
319 matches = registry->GetMatches( | 322 matches = registry->GetMatches( |
320 WebRequestRule::RequestData(&http_request, ON_BEFORE_REQUEST)); | 323 DeclarativeWebRequestData(&http_request, ON_BEFORE_REQUEST)); |
321 EXPECT_EQ(2u, matches.size()); | 324 EXPECT_EQ(2u, matches.size()); |
322 | 325 |
323 std::set<WebRequestRule::GlobalRuleId> matches_ids; | 326 std::set<WebRequestRule::GlobalRuleId> matches_ids; |
324 for (std::set<const WebRequestRule*>::const_iterator it = matches.begin(); | 327 for (std::set<const WebRequestRule*>::const_iterator it = matches.begin(); |
325 it != matches.end(); ++it) | 328 it != matches.end(); ++it) |
326 matches_ids.insert((*it)->id()); | 329 matches_ids.insert((*it)->id()); |
327 EXPECT_TRUE(ContainsKey(matches_ids, std::make_pair(kExtensionId, kRuleId1))); | 330 EXPECT_TRUE(ContainsKey(matches_ids, std::make_pair(kExtensionId, kRuleId1))); |
328 EXPECT_TRUE(ContainsKey(matches_ids, std::make_pair(kExtensionId, kRuleId2))); | 331 EXPECT_TRUE(ContainsKey(matches_ids, std::make_pair(kExtensionId, kRuleId2))); |
329 | 332 |
330 GURL foobar_url("http://www.foobar.com"); | 333 GURL foobar_url("http://www.foobar.com"); |
331 net::TestURLRequest foobar_request(foobar_url, NULL, &context); | 334 net::TestURLRequest foobar_request(foobar_url, NULL, &context); |
332 matches = registry->GetMatches( | 335 matches = registry->GetMatches( |
333 WebRequestRule::RequestData(&foobar_request, ON_BEFORE_REQUEST)); | 336 DeclarativeWebRequestData(&foobar_request, ON_BEFORE_REQUEST)); |
334 EXPECT_EQ(1u, matches.size()); | 337 EXPECT_EQ(1u, matches.size()); |
335 WebRequestRule::GlobalRuleId expected_pair = | 338 WebRequestRule::GlobalRuleId expected_pair = |
336 std::make_pair(kExtensionId, kRuleId2); | 339 std::make_pair(kExtensionId, kRuleId2); |
337 EXPECT_EQ(expected_pair, (*matches.begin())->id()); | 340 EXPECT_EQ(expected_pair, (*matches.begin())->id()); |
338 } | 341 } |
339 | 342 |
340 TEST_F(WebRequestRulesRegistryTest, RemoveRulesImpl) { | 343 TEST_F(WebRequestRulesRegistryTest, RemoveRulesImpl) { |
341 scoped_refptr<TestWebRequestRulesRegistry> registry( | 344 scoped_refptr<TestWebRequestRulesRegistry> registry( |
342 new TestWebRequestRulesRegistry()); | 345 new TestWebRequestRulesRegistry()); |
343 std::string error; | 346 std::string error; |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
451 rules_to_add_2[0] = CreateRedirectRule("http://www.bar.com"); | 454 rules_to_add_2[0] = CreateRedirectRule("http://www.bar.com"); |
452 error = registry->AddRules(kExtensionId2, rules_to_add_2); | 455 error = registry->AddRules(kExtensionId2, rules_to_add_2); |
453 EXPECT_EQ("", error); | 456 EXPECT_EQ("", error); |
454 | 457 |
455 GURL url("http://www.google.com"); | 458 GURL url("http://www.google.com"); |
456 net::TestURLRequestContext context; | 459 net::TestURLRequestContext context; |
457 net::TestURLRequest request(url, NULL, &context); | 460 net::TestURLRequest request(url, NULL, &context); |
458 std::list<LinkedPtrEventResponseDelta> deltas = | 461 std::list<LinkedPtrEventResponseDelta> deltas = |
459 registry->CreateDeltas( | 462 registry->CreateDeltas( |
460 NULL, | 463 NULL, |
461 WebRequestRule::RequestData(&request, ON_BEFORE_REQUEST), | 464 DeclarativeWebRequestData(&request, ON_BEFORE_REQUEST), |
462 false); | 465 false); |
463 | 466 |
464 // The second extension is installed later and will win for this reason | 467 // The second extension is installed later and will win for this reason |
465 // in conflict resolution. | 468 // in conflict resolution. |
466 ASSERT_EQ(2u, deltas.size()); | 469 ASSERT_EQ(2u, deltas.size()); |
467 deltas.sort(&helpers::InDecreasingExtensionInstallationTimeOrder); | 470 deltas.sort(&helpers::InDecreasingExtensionInstallationTimeOrder); |
468 | 471 |
469 std::list<LinkedPtrEventResponseDelta>::iterator i = deltas.begin(); | 472 std::list<LinkedPtrEventResponseDelta>::iterator i = deltas.begin(); |
470 LinkedPtrEventResponseDelta winner = *i++; | 473 LinkedPtrEventResponseDelta winner = *i++; |
471 LinkedPtrEventResponseDelta loser = *i; | 474 LinkedPtrEventResponseDelta loser = *i; |
(...skipping 29 matching lines...) Expand all Loading... |
501 rules_to_add_3[0] = CreateIgnoreRule(); | 504 rules_to_add_3[0] = CreateIgnoreRule(); |
502 error = registry->AddRules(kExtensionId, rules_to_add_3); | 505 error = registry->AddRules(kExtensionId, rules_to_add_3); |
503 EXPECT_EQ("", error); | 506 EXPECT_EQ("", error); |
504 | 507 |
505 GURL url("http://www.google.com/index.html"); | 508 GURL url("http://www.google.com/index.html"); |
506 net::TestURLRequestContext context; | 509 net::TestURLRequestContext context; |
507 net::TestURLRequest request(url, NULL, &context); | 510 net::TestURLRequest request(url, NULL, &context); |
508 std::list<LinkedPtrEventResponseDelta> deltas = | 511 std::list<LinkedPtrEventResponseDelta> deltas = |
509 registry->CreateDeltas( | 512 registry->CreateDeltas( |
510 NULL, | 513 NULL, |
511 WebRequestRule::RequestData(&request, ON_BEFORE_REQUEST), | 514 DeclarativeWebRequestData(&request, ON_BEFORE_REQUEST), |
512 false); | 515 false); |
513 | 516 |
514 // The redirect by the first extension is ignored due to the ignore rule. | 517 // The redirect by the first extension is ignored due to the ignore rule. |
515 ASSERT_EQ(1u, deltas.size()); | 518 ASSERT_EQ(1u, deltas.size()); |
516 LinkedPtrEventResponseDelta effective_rule = *(deltas.begin()); | 519 LinkedPtrEventResponseDelta effective_rule = *(deltas.begin()); |
517 | 520 |
518 EXPECT_EQ(kExtensionId2, effective_rule->extension_id); | 521 EXPECT_EQ(kExtensionId2, effective_rule->extension_id); |
519 EXPECT_EQ(base::Time() + base::TimeDelta::FromDays(2), | 522 EXPECT_EQ(base::Time() + base::TimeDelta::FromDays(2), |
520 effective_rule->extension_install_time); | 523 effective_rule->extension_install_time); |
521 EXPECT_EQ(GURL("http://www.bar.com"), effective_rule->new_url); | 524 EXPECT_EQ(GURL("http://www.bar.com"), effective_rule->new_url); |
(...skipping 21 matching lines...) Expand all Loading... |
543 error = registry->AddRules(kExtensionId, rules); | 546 error = registry->AddRules(kExtensionId, rules); |
544 EXPECT_EQ("", error); | 547 EXPECT_EQ("", error); |
545 EXPECT_EQ(1, registry->num_clear_cache_calls()); | 548 EXPECT_EQ(1, registry->num_clear_cache_calls()); |
546 | 549 |
547 std::set<const WebRequestRule*> matches; | 550 std::set<const WebRequestRule*> matches; |
548 | 551 |
549 GURL http_url("http://www.example.com"); | 552 GURL http_url("http://www.example.com"); |
550 net::TestURLRequestContext context; | 553 net::TestURLRequestContext context; |
551 net::TestURLRequest http_request(http_url, NULL, &context); | 554 net::TestURLRequest http_request(http_url, NULL, &context); |
552 matches = registry->GetMatches( | 555 matches = registry->GetMatches( |
553 WebRequestRule::RequestData(&http_request, ON_BEFORE_REQUEST)); | 556 DeclarativeWebRequestData(&http_request, ON_BEFORE_REQUEST)); |
554 EXPECT_EQ(1u, matches.size()); | 557 EXPECT_EQ(1u, matches.size()); |
555 WebRequestRule::GlobalRuleId expected_pair = std::make_pair(kExtensionId, | 558 WebRequestRule::GlobalRuleId expected_pair = std::make_pair(kExtensionId, |
556 kRuleId3); | 559 kRuleId3); |
557 EXPECT_EQ(expected_pair, (*matches.begin())->id()); | 560 EXPECT_EQ(expected_pair, (*matches.begin())->id()); |
558 } | 561 } |
559 | 562 |
| 563 TEST_F(WebRequestRulesRegistryTest, CheckConsistency) { |
| 564 // The contentType condition can only be evaluated during ON_HEADERS_RECEIVED |
| 565 // but the redirect action can only be executed during ON_BEFORE_REQUEST. |
| 566 // Therefore, this is an inconsistent rule that needs to be flagged. |
| 567 const char kRule[] = |
| 568 "{ \n" |
| 569 " \"id\": \"rule1\", \n" |
| 570 " \"conditions\": [ \n" |
| 571 " { \n" |
| 572 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n" |
| 573 " \"url\": {\"hostSuffix\": \"foo.com\"}, \n" |
| 574 " \"contentType\": [\"image/jpeg\"] \n" |
| 575 " } \n" |
| 576 " ], \n" |
| 577 " \"actions\": [ \n" |
| 578 " { \n" |
| 579 " \"instanceType\": \"declarativeWebRequest.RedirectRequest\",\n" |
| 580 " \"redirectUrl\": \"http://bar.com\" \n" |
| 581 " } \n" |
| 582 " ], \n" |
| 583 " \"priority\": 200 \n" |
| 584 "} "; |
| 585 |
| 586 scoped_ptr<Value> value(base::JSONReader::Read(kRule)); |
| 587 ASSERT_TRUE(value.get()); |
| 588 |
| 589 std::vector<linked_ptr<RulesRegistry::Rule> > rules; |
| 590 rules.push_back(make_linked_ptr(new RulesRegistry::Rule)); |
| 591 ASSERT_TRUE(RulesRegistry::Rule::Populate(*value, rules.back().get())); |
| 592 |
| 593 scoped_refptr<WebRequestRulesRegistry> registry( |
| 594 new TestWebRequestRulesRegistry()); |
| 595 |
| 596 URLMatcher matcher; |
| 597 std::string error = registry->AddRulesImpl(kExtensionId, rules); |
| 598 EXPECT_THAT(error, HasSubstr("no time in the request life-cycle")); |
| 599 EXPECT_TRUE(registry->IsEmpty()); |
| 600 } |
| 601 |
| 602 |
560 } // namespace extensions | 603 } // namespace extensions |
OLD | NEW |