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/json/json_reader.h" |
10 #include "base/memory/linked_ptr.h" | 10 #include "base/memory/linked_ptr.h" |
(...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
267 | 267 |
268 error = registry->AddRules(kExtensionId, rules); | 268 error = registry->AddRules(kExtensionId, rules); |
269 EXPECT_EQ("", error); | 269 EXPECT_EQ("", error); |
270 EXPECT_EQ(1, registry->num_clear_cache_calls()); | 270 EXPECT_EQ(1, registry->num_clear_cache_calls()); |
271 | 271 |
272 std::set<const WebRequestRule*> matches; | 272 std::set<const WebRequestRule*> matches; |
273 | 273 |
274 GURL http_url("http://www.example.com"); | 274 GURL http_url("http://www.example.com"); |
275 net::TestURLRequestContext context; | 275 net::TestURLRequestContext context; |
276 net::TestURLRequest http_request(http_url, NULL, &context); | 276 net::TestURLRequest http_request(http_url, NULL, &context); |
277 matches = registry->GetMatches( | 277 DeclarativeWebRequestData request_data(&http_request, ON_BEFORE_REQUEST); |
278 DeclarativeWebRequestData(&http_request, ON_BEFORE_REQUEST)); | 278 matches = registry->GetMatches(&request_data); |
279 EXPECT_EQ(2u, matches.size()); | 279 EXPECT_EQ(2u, matches.size()); |
280 | 280 |
281 std::set<WebRequestRule::GlobalRuleId> matches_ids; | 281 std::set<WebRequestRule::GlobalRuleId> matches_ids; |
282 for (std::set<const WebRequestRule*>::const_iterator it = matches.begin(); | 282 for (std::set<const WebRequestRule*>::const_iterator it = matches.begin(); |
283 it != matches.end(); ++it) | 283 it != matches.end(); ++it) |
284 matches_ids.insert((*it)->id()); | 284 matches_ids.insert((*it)->id()); |
285 EXPECT_TRUE(ContainsKey(matches_ids, std::make_pair(kExtensionId, kRuleId1))); | 285 EXPECT_TRUE(ContainsKey(matches_ids, std::make_pair(kExtensionId, kRuleId1))); |
286 EXPECT_TRUE(ContainsKey(matches_ids, std::make_pair(kExtensionId, kRuleId2))); | 286 EXPECT_TRUE(ContainsKey(matches_ids, std::make_pair(kExtensionId, kRuleId2))); |
287 | 287 |
288 GURL foobar_url("http://www.foobar.com"); | 288 GURL foobar_url("http://www.foobar.com"); |
289 net::TestURLRequest foobar_request(foobar_url, NULL, &context); | 289 net::TestURLRequest foobar_request(foobar_url, NULL, &context); |
290 matches = registry->GetMatches( | 290 request_data.request = &foobar_request; |
291 DeclarativeWebRequestData(&foobar_request, ON_BEFORE_REQUEST)); | 291 matches = registry->GetMatches(&request_data); |
292 EXPECT_EQ(1u, matches.size()); | 292 EXPECT_EQ(1u, matches.size()); |
293 WebRequestRule::GlobalRuleId expected_pair = | 293 WebRequestRule::GlobalRuleId expected_pair = |
294 std::make_pair(kExtensionId, kRuleId2); | 294 std::make_pair(kExtensionId, kRuleId2); |
295 EXPECT_EQ(expected_pair, (*matches.begin())->id()); | 295 EXPECT_EQ(expected_pair, (*matches.begin())->id()); |
296 } | 296 } |
297 | 297 |
298 TEST_F(WebRequestRulesRegistryTest, RemoveRulesImpl) { | 298 TEST_F(WebRequestRulesRegistryTest, RemoveRulesImpl) { |
299 scoped_refptr<TestWebRequestRulesRegistry> registry( | 299 scoped_refptr<TestWebRequestRulesRegistry> registry( |
300 new TestWebRequestRulesRegistry()); | 300 new TestWebRequestRulesRegistry()); |
301 std::string error; | 301 std::string error; |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
406 EXPECT_EQ("", error); | 406 EXPECT_EQ("", error); |
407 | 407 |
408 std::vector<linked_ptr<RulesRegistry::Rule> > rules_to_add_2(1); | 408 std::vector<linked_ptr<RulesRegistry::Rule> > rules_to_add_2(1); |
409 rules_to_add_2[0] = CreateRedirectRule("http://www.bar.com"); | 409 rules_to_add_2[0] = CreateRedirectRule("http://www.bar.com"); |
410 error = registry->AddRules(kExtensionId2, rules_to_add_2); | 410 error = registry->AddRules(kExtensionId2, rules_to_add_2); |
411 EXPECT_EQ("", error); | 411 EXPECT_EQ("", error); |
412 | 412 |
413 GURL url("http://www.google.com"); | 413 GURL url("http://www.google.com"); |
414 net::TestURLRequestContext context; | 414 net::TestURLRequestContext context; |
415 net::TestURLRequest request(url, NULL, &context); | 415 net::TestURLRequest request(url, NULL, &context); |
| 416 DeclarativeWebRequestData request_data(&request, ON_BEFORE_REQUEST); |
416 std::list<LinkedPtrEventResponseDelta> deltas = | 417 std::list<LinkedPtrEventResponseDelta> deltas = |
417 registry->CreateDeltas( | 418 registry->CreateDeltas(NULL, &request_data, false); |
418 NULL, | |
419 DeclarativeWebRequestData(&request, ON_BEFORE_REQUEST), | |
420 false); | |
421 | 419 |
422 // The second extension is installed later and will win for this reason | 420 // The second extension is installed later and will win for this reason |
423 // in conflict resolution. | 421 // in conflict resolution. |
424 ASSERT_EQ(2u, deltas.size()); | 422 ASSERT_EQ(2u, deltas.size()); |
425 deltas.sort(&helpers::InDecreasingExtensionInstallationTimeOrder); | 423 deltas.sort(&helpers::InDecreasingExtensionInstallationTimeOrder); |
426 | 424 |
427 std::list<LinkedPtrEventResponseDelta>::iterator i = deltas.begin(); | 425 std::list<LinkedPtrEventResponseDelta>::iterator i = deltas.begin(); |
428 LinkedPtrEventResponseDelta winner = *i++; | 426 LinkedPtrEventResponseDelta winner = *i++; |
429 LinkedPtrEventResponseDelta loser = *i; | 427 LinkedPtrEventResponseDelta loser = *i; |
430 | 428 |
(...skipping 25 matching lines...) Expand all Loading... |
456 EXPECT_EQ("", error); | 454 EXPECT_EQ("", error); |
457 | 455 |
458 std::vector<linked_ptr<RulesRegistry::Rule> > rules_to_add_3(1); | 456 std::vector<linked_ptr<RulesRegistry::Rule> > rules_to_add_3(1); |
459 rules_to_add_3[0] = CreateIgnoreRule(); | 457 rules_to_add_3[0] = CreateIgnoreRule(); |
460 error = registry->AddRules(kExtensionId, rules_to_add_3); | 458 error = registry->AddRules(kExtensionId, rules_to_add_3); |
461 EXPECT_EQ("", error); | 459 EXPECT_EQ("", error); |
462 | 460 |
463 GURL url("http://www.google.com/index.html"); | 461 GURL url("http://www.google.com/index.html"); |
464 net::TestURLRequestContext context; | 462 net::TestURLRequestContext context; |
465 net::TestURLRequest request(url, NULL, &context); | 463 net::TestURLRequest request(url, NULL, &context); |
| 464 DeclarativeWebRequestData request_data(&request, ON_BEFORE_REQUEST); |
466 std::list<LinkedPtrEventResponseDelta> deltas = | 465 std::list<LinkedPtrEventResponseDelta> deltas = |
467 registry->CreateDeltas( | 466 registry->CreateDeltas(NULL, &request_data, false); |
468 NULL, | |
469 DeclarativeWebRequestData(&request, ON_BEFORE_REQUEST), | |
470 false); | |
471 | 467 |
472 // The redirect by the first extension is ignored due to the ignore rule. | 468 // The redirect by the first extension is ignored due to the ignore rule. |
473 ASSERT_EQ(1u, deltas.size()); | 469 ASSERT_EQ(1u, deltas.size()); |
474 LinkedPtrEventResponseDelta effective_rule = *(deltas.begin()); | 470 LinkedPtrEventResponseDelta effective_rule = *(deltas.begin()); |
475 | 471 |
476 EXPECT_EQ(kExtensionId2, effective_rule->extension_id); | 472 EXPECT_EQ(kExtensionId2, effective_rule->extension_id); |
477 EXPECT_EQ(base::Time() + base::TimeDelta::FromDays(2), | 473 EXPECT_EQ(base::Time() + base::TimeDelta::FromDays(2), |
478 effective_rule->extension_install_time); | 474 effective_rule->extension_install_time); |
479 EXPECT_EQ(GURL("http://www.bar.com"), effective_rule->new_url); | 475 EXPECT_EQ(GURL("http://www.bar.com"), effective_rule->new_url); |
480 } | 476 } |
(...skipping 19 matching lines...) Expand all Loading... |
500 | 496 |
501 error = registry->AddRules(kExtensionId, rules); | 497 error = registry->AddRules(kExtensionId, rules); |
502 EXPECT_EQ("", error); | 498 EXPECT_EQ("", error); |
503 EXPECT_EQ(1, registry->num_clear_cache_calls()); | 499 EXPECT_EQ(1, registry->num_clear_cache_calls()); |
504 | 500 |
505 std::set<const WebRequestRule*> matches; | 501 std::set<const WebRequestRule*> matches; |
506 | 502 |
507 GURL http_url("http://www.example.com"); | 503 GURL http_url("http://www.example.com"); |
508 net::TestURLRequestContext context; | 504 net::TestURLRequestContext context; |
509 net::TestURLRequest http_request(http_url, NULL, &context); | 505 net::TestURLRequest http_request(http_url, NULL, &context); |
510 matches = registry->GetMatches( | 506 DeclarativeWebRequestData request_data(&http_request, ON_BEFORE_REQUEST); |
511 DeclarativeWebRequestData(&http_request, ON_BEFORE_REQUEST)); | 507 matches = registry->GetMatches(&request_data); |
512 EXPECT_EQ(1u, matches.size()); | 508 EXPECT_EQ(1u, matches.size()); |
513 WebRequestRule::GlobalRuleId expected_pair = std::make_pair(kExtensionId, | 509 WebRequestRule::GlobalRuleId expected_pair = std::make_pair(kExtensionId, |
514 kRuleId3); | 510 kRuleId3); |
515 EXPECT_EQ(expected_pair, (*matches.begin())->id()); | 511 EXPECT_EQ(expected_pair, (*matches.begin())->id()); |
516 } | 512 } |
517 | 513 |
518 TEST_F(WebRequestRulesRegistryTest, CheckConsistency) { | 514 TEST_F(WebRequestRulesRegistryTest, CheckConsistency) { |
519 // The contentType condition can only be evaluated during ON_HEADERS_RECEIVED | 515 // The contentType condition can only be evaluated during ON_HEADERS_RECEIVED |
520 // but the redirect action can only be executed during ON_BEFORE_REQUEST. | 516 // but the redirect action can only be executed during ON_BEFORE_REQUEST. |
521 // Therefore, this is an inconsistent rule that needs to be flagged. | 517 // Therefore, this is an inconsistent rule that needs to be flagged. |
(...skipping 27 matching lines...) Expand all Loading... |
549 new TestWebRequestRulesRegistry()); | 545 new TestWebRequestRulesRegistry()); |
550 | 546 |
551 URLMatcher matcher; | 547 URLMatcher matcher; |
552 std::string error = registry->AddRulesImpl(kExtensionId, rules); | 548 std::string error = registry->AddRulesImpl(kExtensionId, rules); |
553 EXPECT_THAT(error, HasSubstr("no time in the request life-cycle")); | 549 EXPECT_THAT(error, HasSubstr("no time in the request life-cycle")); |
554 EXPECT_TRUE(registry->IsEmpty()); | 550 EXPECT_TRUE(registry->IsEmpty()); |
555 } | 551 } |
556 | 552 |
557 | 553 |
558 } // namespace extensions | 554 } // namespace extensions |
OLD | NEW |