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/values.h" | 12 #include "base/values.h" |
| 13 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_consta
nts.h" |
| 14 #include "chrome/browser/extensions/api/web_request/web_request_api_helpers.h" |
12 #include "chrome/common/extensions/matcher/url_matcher_constants.h" | 15 #include "chrome/common/extensions/matcher/url_matcher_constants.h" |
13 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_consta
nts.h" | |
14 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_rule.h
" | |
15 #include "chrome/browser/extensions/api/web_request/web_request_api_helpers.h" | |
16 #include "content/public/test/test_browser_thread.h" | 16 #include "content/public/test/test_browser_thread.h" |
17 #include "net/url_request/url_request_test_util.h" | 17 #include "net/url_request/url_request_test_util.h" |
| 18 #include "testing/gmock/include/gmock/gmock.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
19 | 20 |
20 namespace { | 21 namespace { |
21 const char kExtensionId[] = "ext1"; | 22 const char kExtensionId[] = "ext1"; |
22 const char kExtensionId2[] = "ext2"; | 23 const char kExtensionId2[] = "ext2"; |
23 const char kRuleId1[] = "rule1"; | 24 const char kRuleId1[] = "rule1"; |
24 const char kRuleId2[] = "rule2"; | 25 const char kRuleId2[] = "rule2"; |
25 const char kRuleId3[] = "rule3"; | 26 const char kRuleId3[] = "rule3"; |
26 const char kRuleId4[] = "rule4"; | 27 const char kRuleId4[] = "rule4"; |
27 } // namespace | 28 } // namespace |
28 | 29 |
29 namespace extensions { | 30 namespace extensions { |
30 | 31 |
| 32 using testing::HasSubstr; |
| 33 |
31 namespace helpers = extension_web_request_api_helpers; | 34 namespace helpers = extension_web_request_api_helpers; |
32 namespace keys = declarative_webrequest_constants; | 35 namespace keys = declarative_webrequest_constants; |
33 namespace keys2 = url_matcher_constants; | 36 namespace keys2 = url_matcher_constants; |
34 | 37 |
35 class TestWebRequestRulesRegistry : public WebRequestRulesRegistry { | 38 class TestWebRequestRulesRegistry : public WebRequestRulesRegistry { |
36 public: | 39 public: |
37 TestWebRequestRulesRegistry() | 40 TestWebRequestRulesRegistry() |
38 : WebRequestRulesRegistry(NULL, NULL), | 41 : WebRequestRulesRegistry(NULL, NULL), |
39 num_clear_cache_calls_(0) {} | 42 num_clear_cache_calls_(0) {} |
40 | 43 |
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
229 error = registry->AddRules(kExtensionId, rules); | 232 error = registry->AddRules(kExtensionId, rules); |
230 EXPECT_EQ("", error); | 233 EXPECT_EQ("", error); |
231 EXPECT_EQ(1, registry->num_clear_cache_calls()); | 234 EXPECT_EQ(1, registry->num_clear_cache_calls()); |
232 | 235 |
233 std::set<const WebRequestRule*> matches; | 236 std::set<const WebRequestRule*> matches; |
234 | 237 |
235 GURL http_url("http://www.example.com"); | 238 GURL http_url("http://www.example.com"); |
236 net::TestURLRequestContext context; | 239 net::TestURLRequestContext context; |
237 net::TestURLRequest http_request(http_url, NULL, &context); | 240 net::TestURLRequest http_request(http_url, NULL, &context); |
238 matches = registry->GetMatches( | 241 matches = registry->GetMatches( |
239 WebRequestRule::RequestData(&http_request, ON_BEFORE_REQUEST)); | 242 DeclarativeWebRequestData(&http_request, ON_BEFORE_REQUEST)); |
240 EXPECT_EQ(2u, matches.size()); | 243 EXPECT_EQ(2u, matches.size()); |
241 | 244 |
242 std::set<WebRequestRule::GlobalRuleId> matches_ids; | 245 std::set<WebRequestRule::GlobalRuleId> matches_ids; |
243 for (std::set<const WebRequestRule*>::const_iterator it = matches.begin(); | 246 for (std::set<const WebRequestRule*>::const_iterator it = matches.begin(); |
244 it != matches.end(); ++it) | 247 it != matches.end(); ++it) |
245 matches_ids.insert((*it)->id()); | 248 matches_ids.insert((*it)->id()); |
246 EXPECT_TRUE(matches_ids.find(std::make_pair(kExtensionId, kRuleId1)) != | 249 EXPECT_TRUE(matches_ids.find(std::make_pair(kExtensionId, kRuleId1)) != |
247 matches_ids.end()); | 250 matches_ids.end()); |
248 EXPECT_TRUE(matches_ids.find(std::make_pair(kExtensionId, kRuleId2)) != | 251 EXPECT_TRUE(matches_ids.find(std::make_pair(kExtensionId, kRuleId2)) != |
249 matches_ids.end()); | 252 matches_ids.end()); |
250 | 253 |
251 GURL foobar_url("http://www.foobar.com"); | 254 GURL foobar_url("http://www.foobar.com"); |
252 net::TestURLRequest foobar_request(foobar_url, NULL, &context); | 255 net::TestURLRequest foobar_request(foobar_url, NULL, &context); |
253 matches = registry->GetMatches( | 256 matches = registry->GetMatches( |
254 WebRequestRule::RequestData(&foobar_request, ON_BEFORE_REQUEST)); | 257 DeclarativeWebRequestData(&foobar_request, ON_BEFORE_REQUEST)); |
255 EXPECT_EQ(1u, matches.size()); | 258 EXPECT_EQ(1u, matches.size()); |
256 WebRequestRule::GlobalRuleId expected_pair = | 259 WebRequestRule::GlobalRuleId expected_pair = |
257 std::make_pair(kExtensionId, kRuleId2); | 260 std::make_pair(kExtensionId, kRuleId2); |
258 EXPECT_EQ(expected_pair, (*matches.begin())->id()); | 261 EXPECT_EQ(expected_pair, (*matches.begin())->id()); |
259 } | 262 } |
260 | 263 |
261 TEST_F(WebRequestRulesRegistryTest, RemoveRulesImpl) { | 264 TEST_F(WebRequestRulesRegistryTest, RemoveRulesImpl) { |
262 scoped_refptr<TestWebRequestRulesRegistry> registry( | 265 scoped_refptr<TestWebRequestRulesRegistry> registry( |
263 new TestWebRequestRulesRegistry()); | 266 new TestWebRequestRulesRegistry()); |
264 std::string error; | 267 std::string error; |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
372 rules_to_add_2[0] = CreateRedirectRule("http://www.bar.com"); | 375 rules_to_add_2[0] = CreateRedirectRule("http://www.bar.com"); |
373 error = registry->AddRules(kExtensionId2, rules_to_add_2); | 376 error = registry->AddRules(kExtensionId2, rules_to_add_2); |
374 EXPECT_EQ("", error); | 377 EXPECT_EQ("", error); |
375 | 378 |
376 GURL url("http://www.google.com"); | 379 GURL url("http://www.google.com"); |
377 net::TestURLRequestContext context; | 380 net::TestURLRequestContext context; |
378 net::TestURLRequest request(url, NULL, &context); | 381 net::TestURLRequest request(url, NULL, &context); |
379 std::list<LinkedPtrEventResponseDelta> deltas = | 382 std::list<LinkedPtrEventResponseDelta> deltas = |
380 registry->CreateDeltas( | 383 registry->CreateDeltas( |
381 NULL, | 384 NULL, |
382 WebRequestRule::RequestData(&request, ON_BEFORE_REQUEST), | 385 DeclarativeWebRequestData(&request, ON_BEFORE_REQUEST), |
383 false); | 386 false); |
384 | 387 |
385 // The second extension is installed later and will win for this reason | 388 // The second extension is installed later and will win for this reason |
386 // in conflict resolution. | 389 // in conflict resolution. |
387 ASSERT_EQ(2u, deltas.size()); | 390 ASSERT_EQ(2u, deltas.size()); |
388 deltas.sort(&helpers::InDecreasingExtensionInstallationTimeOrder); | 391 deltas.sort(&helpers::InDecreasingExtensionInstallationTimeOrder); |
389 | 392 |
390 std::list<LinkedPtrEventResponseDelta>::iterator i = deltas.begin(); | 393 std::list<LinkedPtrEventResponseDelta>::iterator i = deltas.begin(); |
391 LinkedPtrEventResponseDelta winner = *i++; | 394 LinkedPtrEventResponseDelta winner = *i++; |
392 LinkedPtrEventResponseDelta loser = *i; | 395 LinkedPtrEventResponseDelta loser = *i; |
(...skipping 29 matching lines...) Expand all Loading... |
422 rules_to_add_3[0] = CreateIgnoreRule(); | 425 rules_to_add_3[0] = CreateIgnoreRule(); |
423 error = registry->AddRules(kExtensionId, rules_to_add_3); | 426 error = registry->AddRules(kExtensionId, rules_to_add_3); |
424 EXPECT_EQ("", error); | 427 EXPECT_EQ("", error); |
425 | 428 |
426 GURL url("http://www.google.com/index.html"); | 429 GURL url("http://www.google.com/index.html"); |
427 net::TestURLRequestContext context; | 430 net::TestURLRequestContext context; |
428 net::TestURLRequest request(url, NULL, &context); | 431 net::TestURLRequest request(url, NULL, &context); |
429 std::list<LinkedPtrEventResponseDelta> deltas = | 432 std::list<LinkedPtrEventResponseDelta> deltas = |
430 registry->CreateDeltas( | 433 registry->CreateDeltas( |
431 NULL, | 434 NULL, |
432 WebRequestRule::RequestData(&request, ON_BEFORE_REQUEST), | 435 DeclarativeWebRequestData(&request, ON_BEFORE_REQUEST), |
433 false); | 436 false); |
434 | 437 |
435 // The redirect by the first extension is ignored due to the ignore rule. | 438 // The redirect by the first extension is ignored due to the ignore rule. |
436 ASSERT_EQ(1u, deltas.size()); | 439 ASSERT_EQ(1u, deltas.size()); |
437 LinkedPtrEventResponseDelta effective_rule = *(deltas.begin()); | 440 LinkedPtrEventResponseDelta effective_rule = *(deltas.begin()); |
438 | 441 |
439 EXPECT_EQ(kExtensionId2, effective_rule->extension_id); | 442 EXPECT_EQ(kExtensionId2, effective_rule->extension_id); |
440 EXPECT_EQ(base::Time() + base::TimeDelta::FromDays(2), | 443 EXPECT_EQ(base::Time() + base::TimeDelta::FromDays(2), |
441 effective_rule->extension_install_time); | 444 effective_rule->extension_install_time); |
442 EXPECT_EQ(GURL("http://www.bar.com"), effective_rule->new_url); | 445 EXPECT_EQ(GURL("http://www.bar.com"), effective_rule->new_url); |
443 } | 446 } |
| 447 |
| 448 TEST_F(WebRequestRulesRegistryTest, CheckConsistency) { |
| 449 // The contentType condition can only be evaluated during ON_HEADERS_RECEIVED |
| 450 // but the redirect action can only be executed during ON_BEFORE_REQUEST. |
| 451 // Therefore, this is an inconsistent rule that needs to be flagged. |
| 452 const char kRule[] = |
| 453 "{ \n" |
| 454 " \"id\": \"rule1\", \n" |
| 455 " \"conditions\": [ \n" |
| 456 " { \n" |
| 457 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n" |
| 458 " \"url\": {\"hostSuffix\": \"foo.com\"}, \n" |
| 459 " \"contentType\": [\"image/jpeg\"] \n" |
| 460 " } \n" |
| 461 " ], \n" |
| 462 " \"actions\": [ \n" |
| 463 " { \n" |
| 464 " \"instanceType\": \"declarativeWebRequest.RedirectRequest\",\n" |
| 465 " \"redirectUrl\": \"http://bar.com\" \n" |
| 466 " } \n" |
| 467 " ], \n" |
| 468 " \"priority\": 200 \n" |
| 469 "} "; |
| 470 |
| 471 scoped_ptr<Value> value(base::JSONReader::Read(kRule)); |
| 472 ASSERT_TRUE(value.get()); |
| 473 |
| 474 std::vector<linked_ptr<RulesRegistry::Rule> > rules; |
| 475 rules.push_back(make_linked_ptr(new RulesRegistry::Rule)); |
| 476 ASSERT_TRUE(RulesRegistry::Rule::Populate(*value, rules.back().get())); |
| 477 |
| 478 scoped_refptr<WebRequestRulesRegistry> registry( |
| 479 new TestWebRequestRulesRegistry()); |
| 480 |
| 481 URLMatcher matcher; |
| 482 std::string error = registry->AddRulesImpl(kExtensionId, rules); |
| 483 EXPECT_THAT(error, HasSubstr("no time in the request life-cycle")); |
| 484 EXPECT_TRUE(registry->IsEmpty()); |
| 485 } |
| 486 |
444 } // namespace extensions | 487 } // namespace extensions |
OLD | NEW |