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 "extensions/browser/api/declarative_webrequest/webrequest_rules_registr
y.h" | 5 #include "extensions/browser/api/declarative_webrequest/webrequest_rules_registr
y.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" |
(...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
267 | 267 |
268 TEST_F(WebRequestRulesRegistryTest, AddRulesImpl) { | 268 TEST_F(WebRequestRulesRegistryTest, AddRulesImpl) { |
269 scoped_refptr<TestWebRequestRulesRegistry> registry( | 269 scoped_refptr<TestWebRequestRulesRegistry> registry( |
270 new TestWebRequestRulesRegistry(extension_info_map_)); | 270 new TestWebRequestRulesRegistry(extension_info_map_)); |
271 std::string error; | 271 std::string error; |
272 | 272 |
273 std::vector<linked_ptr<RulesRegistry::Rule> > rules; | 273 std::vector<linked_ptr<RulesRegistry::Rule> > rules; |
274 rules.push_back(CreateRule1()); | 274 rules.push_back(CreateRule1()); |
275 rules.push_back(CreateRule2()); | 275 rules.push_back(CreateRule2()); |
276 | 276 |
277 error = registry->AddRules(kExtensionId, rules); | 277 error = registry->AddRules(kExtensionId, rules, false); |
278 EXPECT_EQ("", error); | 278 EXPECT_EQ("", error); |
279 EXPECT_EQ(1, registry->num_clear_cache_calls()); | 279 EXPECT_EQ(1, registry->num_clear_cache_calls()); |
280 | 280 |
281 std::set<const WebRequestRule*> matches; | 281 std::set<const WebRequestRule*> matches; |
282 | 282 |
283 GURL http_url("http://www.example.com"); | 283 GURL http_url("http://www.example.com"); |
284 net::TestURLRequestContext context; | 284 net::TestURLRequestContext context; |
285 scoped_ptr<net::URLRequest> http_request(context.CreateRequest( | 285 scoped_ptr<net::URLRequest> http_request(context.CreateRequest( |
286 http_url, net::DEFAULT_PRIORITY, NULL)); | 286 http_url, net::DEFAULT_PRIORITY, NULL)); |
287 WebRequestData request_data(http_request.get(), ON_BEFORE_REQUEST); | 287 WebRequestData request_data(http_request.get(), ON_BEFORE_REQUEST); |
(...skipping 20 matching lines...) Expand all Loading... |
308 | 308 |
309 TEST_F(WebRequestRulesRegistryTest, RemoveRulesImpl) { | 309 TEST_F(WebRequestRulesRegistryTest, RemoveRulesImpl) { |
310 scoped_refptr<TestWebRequestRulesRegistry> registry( | 310 scoped_refptr<TestWebRequestRulesRegistry> registry( |
311 new TestWebRequestRulesRegistry(extension_info_map_)); | 311 new TestWebRequestRulesRegistry(extension_info_map_)); |
312 std::string error; | 312 std::string error; |
313 | 313 |
314 // Setup RulesRegistry to contain two rules. | 314 // Setup RulesRegistry to contain two rules. |
315 std::vector<linked_ptr<RulesRegistry::Rule> > rules_to_add; | 315 std::vector<linked_ptr<RulesRegistry::Rule> > rules_to_add; |
316 rules_to_add.push_back(CreateRule1()); | 316 rules_to_add.push_back(CreateRule1()); |
317 rules_to_add.push_back(CreateRule2()); | 317 rules_to_add.push_back(CreateRule2()); |
318 error = registry->AddRules(kExtensionId, rules_to_add); | 318 error = registry->AddRules(kExtensionId, rules_to_add, false); |
319 EXPECT_EQ("", error); | 319 EXPECT_EQ("", error); |
320 EXPECT_EQ(1, registry->num_clear_cache_calls()); | 320 EXPECT_EQ(1, registry->num_clear_cache_calls()); |
321 | 321 |
322 // Verify initial state. | 322 // Verify initial state. |
323 std::vector<linked_ptr<RulesRegistry::Rule> > registered_rules; | 323 std::vector<linked_ptr<RulesRegistry::Rule> > registered_rules; |
324 registry->GetAllRules(kExtensionId, ®istered_rules); | 324 registry->GetAllRules(kExtensionId, ®istered_rules); |
325 EXPECT_EQ(2u, registered_rules.size()); | 325 EXPECT_EQ(2u, registered_rules.size()); |
326 EXPECT_EQ(1u, registry->RulesWithoutTriggers()); | 326 EXPECT_EQ(1u, registry->RulesWithoutTriggers()); |
327 | 327 |
328 // Remove first rule. | 328 // Remove first rule. |
(...skipping 26 matching lines...) Expand all Loading... |
355 } | 355 } |
356 | 356 |
357 TEST_F(WebRequestRulesRegistryTest, RemoveAllRulesImpl) { | 357 TEST_F(WebRequestRulesRegistryTest, RemoveAllRulesImpl) { |
358 scoped_refptr<TestWebRequestRulesRegistry> registry( | 358 scoped_refptr<TestWebRequestRulesRegistry> registry( |
359 new TestWebRequestRulesRegistry(extension_info_map_)); | 359 new TestWebRequestRulesRegistry(extension_info_map_)); |
360 std::string error; | 360 std::string error; |
361 | 361 |
362 // Setup RulesRegistry to contain two rules, one for each extension. | 362 // Setup RulesRegistry to contain two rules, one for each extension. |
363 std::vector<linked_ptr<RulesRegistry::Rule> > rules_to_add(1); | 363 std::vector<linked_ptr<RulesRegistry::Rule> > rules_to_add(1); |
364 rules_to_add[0] = CreateRule1(); | 364 rules_to_add[0] = CreateRule1(); |
365 error = registry->AddRules(kExtensionId, rules_to_add); | 365 error = registry->AddRules(kExtensionId, rules_to_add, false); |
366 EXPECT_EQ("", error); | 366 EXPECT_EQ("", error); |
367 EXPECT_EQ(1, registry->num_clear_cache_calls()); | 367 EXPECT_EQ(1, registry->num_clear_cache_calls()); |
368 | 368 |
369 rules_to_add[0] = CreateRule2(); | 369 rules_to_add[0] = CreateRule2(); |
370 error = registry->AddRules(kExtensionId2, rules_to_add); | 370 error = registry->AddRules(kExtensionId2, rules_to_add, false); |
371 EXPECT_EQ("", error); | 371 EXPECT_EQ("", error); |
372 EXPECT_EQ(2, registry->num_clear_cache_calls()); | 372 EXPECT_EQ(2, registry->num_clear_cache_calls()); |
373 | 373 |
374 // Verify initial state. | 374 // Verify initial state. |
375 std::vector<linked_ptr<RulesRegistry::Rule> > registered_rules; | 375 std::vector<linked_ptr<RulesRegistry::Rule> > registered_rules; |
376 registry->GetAllRules(kExtensionId, ®istered_rules); | 376 registry->GetAllRules(kExtensionId, ®istered_rules); |
377 EXPECT_EQ(1u, registered_rules.size()); | 377 EXPECT_EQ(1u, registered_rules.size()); |
378 registered_rules.clear(); | 378 registered_rules.clear(); |
379 registry->GetAllRules(kExtensionId2, ®istered_rules); | 379 registry->GetAllRules(kExtensionId2, ®istered_rules); |
380 EXPECT_EQ(1u, registered_rules.size()); | 380 EXPECT_EQ(1u, registered_rules.size()); |
(...skipping 25 matching lines...) Expand all Loading... |
406 } | 406 } |
407 | 407 |
408 // Test precedences between extensions. | 408 // Test precedences between extensions. |
409 TEST_F(WebRequestRulesRegistryTest, Precedences) { | 409 TEST_F(WebRequestRulesRegistryTest, Precedences) { |
410 scoped_refptr<WebRequestRulesRegistry> registry( | 410 scoped_refptr<WebRequestRulesRegistry> registry( |
411 new TestWebRequestRulesRegistry(extension_info_map_)); | 411 new TestWebRequestRulesRegistry(extension_info_map_)); |
412 std::string error; | 412 std::string error; |
413 | 413 |
414 std::vector<linked_ptr<RulesRegistry::Rule> > rules_to_add_1(1); | 414 std::vector<linked_ptr<RulesRegistry::Rule> > rules_to_add_1(1); |
415 rules_to_add_1[0] = CreateRedirectRule("http://www.foo.com"); | 415 rules_to_add_1[0] = CreateRedirectRule("http://www.foo.com"); |
416 error = registry->AddRules(kExtensionId, rules_to_add_1); | 416 error = registry->AddRules(kExtensionId, rules_to_add_1, false); |
417 EXPECT_EQ("", error); | 417 EXPECT_EQ("", error); |
418 | 418 |
419 std::vector<linked_ptr<RulesRegistry::Rule> > rules_to_add_2(1); | 419 std::vector<linked_ptr<RulesRegistry::Rule> > rules_to_add_2(1); |
420 rules_to_add_2[0] = CreateRedirectRule("http://www.bar.com"); | 420 rules_to_add_2[0] = CreateRedirectRule("http://www.bar.com"); |
421 error = registry->AddRules(kExtensionId2, rules_to_add_2); | 421 error = registry->AddRules(kExtensionId2, rules_to_add_2, false); |
422 EXPECT_EQ("", error); | 422 EXPECT_EQ("", error); |
423 | 423 |
424 GURL url("http://www.google.com"); | 424 GURL url("http://www.google.com"); |
425 net::TestURLRequestContext context; | 425 net::TestURLRequestContext context; |
426 scoped_ptr<net::URLRequest> request(context.CreateRequest( | 426 scoped_ptr<net::URLRequest> request(context.CreateRequest( |
427 url, net::DEFAULT_PRIORITY, NULL)); | 427 url, net::DEFAULT_PRIORITY, NULL)); |
428 WebRequestData request_data(request.get(), ON_BEFORE_REQUEST); | 428 WebRequestData request_data(request.get(), ON_BEFORE_REQUEST); |
429 std::list<LinkedPtrEventResponseDelta> deltas = | 429 std::list<LinkedPtrEventResponseDelta> deltas = |
430 registry->CreateDeltas(NULL, request_data, false); | 430 registry->CreateDeltas(NULL, request_data, false); |
431 | 431 |
(...skipping 18 matching lines...) Expand all Loading... |
450 } | 450 } |
451 | 451 |
452 // Test priorities of rules within one extension. | 452 // Test priorities of rules within one extension. |
453 TEST_F(WebRequestRulesRegistryTest, Priorities) { | 453 TEST_F(WebRequestRulesRegistryTest, Priorities) { |
454 scoped_refptr<WebRequestRulesRegistry> registry( | 454 scoped_refptr<WebRequestRulesRegistry> registry( |
455 new TestWebRequestRulesRegistry(extension_info_map_)); | 455 new TestWebRequestRulesRegistry(extension_info_map_)); |
456 std::string error; | 456 std::string error; |
457 | 457 |
458 std::vector<linked_ptr<RulesRegistry::Rule> > rules_to_add_1(1); | 458 std::vector<linked_ptr<RulesRegistry::Rule> > rules_to_add_1(1); |
459 rules_to_add_1[0] = CreateRedirectRule("http://www.foo.com"); | 459 rules_to_add_1[0] = CreateRedirectRule("http://www.foo.com"); |
460 error = registry->AddRules(kExtensionId, rules_to_add_1); | 460 error = registry->AddRules(kExtensionId, rules_to_add_1, false); |
461 EXPECT_EQ("", error); | 461 EXPECT_EQ("", error); |
462 | 462 |
463 std::vector<linked_ptr<RulesRegistry::Rule> > rules_to_add_2(1); | 463 std::vector<linked_ptr<RulesRegistry::Rule> > rules_to_add_2(1); |
464 rules_to_add_2[0] = CreateRedirectRule("http://www.bar.com"); | 464 rules_to_add_2[0] = CreateRedirectRule("http://www.bar.com"); |
465 error = registry->AddRules(kExtensionId2, rules_to_add_2); | 465 error = registry->AddRules(kExtensionId2, rules_to_add_2, false); |
466 EXPECT_EQ("", error); | 466 EXPECT_EQ("", error); |
467 | 467 |
468 std::vector<linked_ptr<RulesRegistry::Rule> > rules_to_add_3(1); | 468 std::vector<linked_ptr<RulesRegistry::Rule> > rules_to_add_3(1); |
469 rules_to_add_3[0] = CreateIgnoreRule(); | 469 rules_to_add_3[0] = CreateIgnoreRule(); |
470 error = registry->AddRules(kExtensionId, rules_to_add_3); | 470 error = registry->AddRules(kExtensionId, rules_to_add_3, false); |
471 EXPECT_EQ("", error); | 471 EXPECT_EQ("", error); |
472 | 472 |
473 GURL url("http://www.google.com/index.html"); | 473 GURL url("http://www.google.com/index.html"); |
474 net::TestURLRequestContext context; | 474 net::TestURLRequestContext context; |
475 scoped_ptr<net::URLRequest> request(context.CreateRequest( | 475 scoped_ptr<net::URLRequest> request(context.CreateRequest( |
476 url, net::DEFAULT_PRIORITY, NULL)); | 476 url, net::DEFAULT_PRIORITY, NULL)); |
477 WebRequestData request_data(request.get(), ON_BEFORE_REQUEST); | 477 WebRequestData request_data(request.get(), ON_BEFORE_REQUEST); |
478 std::list<LinkedPtrEventResponseDelta> deltas = | 478 std::list<LinkedPtrEventResponseDelta> deltas = |
479 registry->CreateDeltas(NULL, request_data, false); | 479 registry->CreateDeltas(NULL, request_data, false); |
480 | 480 |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
581 attributes.push_back(&kBothAttributes); | 581 attributes.push_back(&kBothAttributes); |
582 rules.push_back(CreateCancellingRule(kRuleId2, attributes)); | 582 rules.push_back(CreateCancellingRule(kRuleId2, attributes)); |
583 | 583 |
584 // Rule 3 has two conditions, one with a matching URL attribute, and one | 584 // Rule 3 has two conditions, one with a matching URL attribute, and one |
585 // with a non-matching non-URL attribute. | 585 // with a non-matching non-URL attribute. |
586 attributes.clear(); | 586 attributes.clear(); |
587 attributes.push_back(&kMatchingUrlAttribute); | 587 attributes.push_back(&kMatchingUrlAttribute); |
588 attributes.push_back(&kNonMatchingNonUrlAttribute); | 588 attributes.push_back(&kNonMatchingNonUrlAttribute); |
589 rules.push_back(CreateCancellingRule(kRuleId3, attributes)); | 589 rules.push_back(CreateCancellingRule(kRuleId3, attributes)); |
590 | 590 |
591 error = registry->AddRules(kExtensionId, rules); | 591 error = registry->AddRules(kExtensionId, rules, false); |
592 EXPECT_EQ("", error); | 592 EXPECT_EQ("", error); |
593 EXPECT_EQ(1, registry->num_clear_cache_calls()); | 593 EXPECT_EQ(1, registry->num_clear_cache_calls()); |
594 | 594 |
595 std::set<const WebRequestRule*> matches; | 595 std::set<const WebRequestRule*> matches; |
596 | 596 |
597 GURL http_url("http://www.example.com"); | 597 GURL http_url("http://www.example.com"); |
598 net::TestURLRequestContext context; | 598 net::TestURLRequestContext context; |
599 scoped_ptr<net::URLRequest> http_request(context.CreateRequest( | 599 scoped_ptr<net::URLRequest> http_request(context.CreateRequest( |
600 http_url, net::DEFAULT_PRIORITY, NULL)); | 600 http_url, net::DEFAULT_PRIORITY, NULL)); |
601 WebRequestData request_data(http_request.get(), ON_BEFORE_REQUEST); | 601 WebRequestData request_data(http_request.get(), ON_BEFORE_REQUEST); |
(...skipping 20 matching lines...) Expand all Loading... |
622 | 622 |
623 // Rule 1 has one condition, with a url attribute | 623 // Rule 1 has one condition, with a url attribute |
624 attributes.push_back(&kUrlAttribute); | 624 attributes.push_back(&kUrlAttribute); |
625 rules.push_back(CreateCancellingRule(kRuleId1, attributes)); | 625 rules.push_back(CreateCancellingRule(kRuleId1, attributes)); |
626 | 626 |
627 // Rule 2 has one condition, with a firstPartyForCookiesUrl attribute | 627 // Rule 2 has one condition, with a firstPartyForCookiesUrl attribute |
628 attributes.clear(); | 628 attributes.clear(); |
629 attributes.push_back(&kFirstPartyUrlAttribute); | 629 attributes.push_back(&kFirstPartyUrlAttribute); |
630 rules.push_back(CreateCancellingRule(kRuleId2, attributes)); | 630 rules.push_back(CreateCancellingRule(kRuleId2, attributes)); |
631 | 631 |
632 error = registry->AddRules(kExtensionId, rules); | 632 error = registry->AddRules(kExtensionId, rules, false); |
633 EXPECT_EQ("", error); | 633 EXPECT_EQ("", error); |
634 EXPECT_EQ(1, registry->num_clear_cache_calls()); | 634 EXPECT_EQ(1, registry->num_clear_cache_calls()); |
635 | 635 |
636 std::set<const WebRequestRule*> matches; | 636 std::set<const WebRequestRule*> matches; |
637 | 637 |
638 const GURL urls[] = { | 638 const GURL urls[] = { |
639 GURL("http://url.example.com"), // matching | 639 GURL("http://url.example.com"), // matching |
640 GURL("http://www.example.com") // non-matching | 640 GURL("http://www.example.com") // non-matching |
641 }; | 641 }; |
642 const GURL firstPartyUrls[] = { | 642 const GURL firstPartyUrls[] = { |
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
814 // This is a correct match. | 814 // This is a correct match. |
815 GURL url2("http://foo.com/index.html"); | 815 GURL url2("http://foo.com/index.html"); |
816 scoped_ptr<net::URLRequest> request2(context.CreateRequest( | 816 scoped_ptr<net::URLRequest> request2(context.CreateRequest( |
817 url2, net::DEFAULT_PRIORITY, NULL)); | 817 url2, net::DEFAULT_PRIORITY, NULL)); |
818 WebRequestData request_data2(request2.get(), ON_BEFORE_REQUEST); | 818 WebRequestData request_data2(request2.get(), ON_BEFORE_REQUEST); |
819 deltas = registry->CreateDeltas(NULL, request_data2, false); | 819 deltas = registry->CreateDeltas(NULL, request_data2, false); |
820 EXPECT_EQ(1u, deltas.size()); | 820 EXPECT_EQ(1u, deltas.size()); |
821 } | 821 } |
822 | 822 |
823 } // namespace extensions | 823 } // namespace extensions |
OLD | NEW |