Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(249)

Side by Side Diff: chrome/browser/extensions/api/declarative_webrequest/webrequest_rules_registry_unittest.cc

Issue 2804023003: Network traffic annotation added to chrome/browser/ unittests. (Closed)
Patch Set: Another missing header added. Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 <stddef.h> 7 #include <stddef.h>
8 8
9 #include <memory> 9 #include <memory>
10 #include <string> 10 #include <string>
11 #include <vector> 11 #include <vector>
12 12
13 #include "base/json/json_reader.h" 13 #include "base/json/json_reader.h"
14 #include "base/macros.h" 14 #include "base/macros.h"
15 #include "base/memory/linked_ptr.h" 15 #include "base/memory/linked_ptr.h"
16 #include "base/message_loop/message_loop.h" 16 #include "base/message_loop/message_loop.h"
17 #include "base/run_loop.h" 17 #include "base/run_loop.h"
18 #include "base/stl_util.h" 18 #include "base/stl_util.h"
19 #include "base/test/values_test_util.h" 19 #include "base/test/values_test_util.h"
20 #include "base/values.h" 20 #include "base/values.h"
21 #include "chrome/common/extensions/extension_test_util.h" 21 #include "chrome/common/extensions/extension_test_util.h"
22 #include "components/url_matcher/url_matcher_constants.h" 22 #include "components/url_matcher/url_matcher_constants.h"
23 #include "content/public/test/test_browser_thread.h" 23 #include "content/public/test/test_browser_thread.h"
24 #include "extensions/browser/api/declarative/rules_registry_service.h" 24 #include "extensions/browser/api/declarative/rules_registry_service.h"
25 #include "extensions/browser/api/declarative_webrequest/webrequest_constants.h" 25 #include "extensions/browser/api/declarative_webrequest/webrequest_constants.h"
26 #include "extensions/browser/api/web_request/web_request_api_helpers.h" 26 #include "extensions/browser/api/web_request/web_request_api_helpers.h"
27 #include "net/base/request_priority.h" 27 #include "net/base/request_priority.h"
28 #include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
28 #include "net/url_request/url_request.h" 29 #include "net/url_request/url_request.h"
29 #include "net/url_request/url_request_test_util.h" 30 #include "net/url_request/url_request_test_util.h"
30 #include "testing/gmock/include/gmock/gmock.h" 31 #include "testing/gmock/include/gmock/gmock.h"
31 #include "testing/gtest/include/gtest/gtest-message.h" 32 #include "testing/gtest/include/gtest/gtest-message.h"
32 #include "testing/gtest/include/gtest/gtest.h" 33 #include "testing/gtest/include/gtest/gtest.h"
33 34
34 using base::Value; 35 using base::Value;
35 using extension_test_util::LoadManifest; 36 using extension_test_util::LoadManifest;
36 using extension_test_util::LoadManifestUnchecked; 37 using extension_test_util::LoadManifestUnchecked;
37 using testing::HasSubstr; 38 using testing::HasSubstr;
(...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after
272 rules.push_back(CreateRule2()); 273 rules.push_back(CreateRule2());
273 274
274 error = registry->AddRules(kExtensionId, rules); 275 error = registry->AddRules(kExtensionId, rules);
275 EXPECT_EQ("", error); 276 EXPECT_EQ("", error);
276 EXPECT_EQ(1, registry->num_clear_cache_calls()); 277 EXPECT_EQ(1, registry->num_clear_cache_calls());
277 278
278 std::set<const WebRequestRule*> matches; 279 std::set<const WebRequestRule*> matches;
279 280
280 GURL http_url("http://www.example.com"); 281 GURL http_url("http://www.example.com");
281 net::TestURLRequestContext context; 282 net::TestURLRequestContext context;
282 std::unique_ptr<net::URLRequest> http_request( 283 std::unique_ptr<net::URLRequest> http_request(context.CreateRequest(
283 context.CreateRequest(http_url, net::DEFAULT_PRIORITY, NULL)); 284 http_url, net::DEFAULT_PRIORITY, NULL, TRAFFIC_ANNOTATION_FOR_TESTS));
284 WebRequestData request_data(http_request.get(), ON_BEFORE_REQUEST); 285 WebRequestData request_data(http_request.get(), ON_BEFORE_REQUEST);
285 matches = registry->GetMatches(request_data); 286 matches = registry->GetMatches(request_data);
286 EXPECT_EQ(2u, matches.size()); 287 EXPECT_EQ(2u, matches.size());
287 288
288 std::set<WebRequestRule::GlobalRuleId> matches_ids; 289 std::set<WebRequestRule::GlobalRuleId> matches_ids;
289 for (std::set<const WebRequestRule*>::const_iterator it = matches.begin(); 290 for (std::set<const WebRequestRule*>::const_iterator it = matches.begin();
290 it != matches.end(); ++it) 291 it != matches.end(); ++it)
291 matches_ids.insert((*it)->id()); 292 matches_ids.insert((*it)->id());
292 EXPECT_TRUE( 293 EXPECT_TRUE(
293 base::ContainsKey(matches_ids, std::make_pair(kExtensionId, kRuleId1))); 294 base::ContainsKey(matches_ids, std::make_pair(kExtensionId, kRuleId1)));
294 EXPECT_TRUE( 295 EXPECT_TRUE(
295 base::ContainsKey(matches_ids, std::make_pair(kExtensionId, kRuleId2))); 296 base::ContainsKey(matches_ids, std::make_pair(kExtensionId, kRuleId2)));
296 297
297 GURL foobar_url("http://www.foobar.com"); 298 GURL foobar_url("http://www.foobar.com");
298 std::unique_ptr<net::URLRequest> foobar_request( 299 std::unique_ptr<net::URLRequest> foobar_request(context.CreateRequest(
299 context.CreateRequest(foobar_url, net::DEFAULT_PRIORITY, NULL)); 300 foobar_url, net::DEFAULT_PRIORITY, NULL, TRAFFIC_ANNOTATION_FOR_TESTS));
300 request_data.request = foobar_request.get(); 301 request_data.request = foobar_request.get();
301 matches = registry->GetMatches(request_data); 302 matches = registry->GetMatches(request_data);
302 EXPECT_EQ(1u, matches.size()); 303 EXPECT_EQ(1u, matches.size());
303 WebRequestRule::GlobalRuleId expected_pair = 304 WebRequestRule::GlobalRuleId expected_pair =
304 std::make_pair(kExtensionId, kRuleId2); 305 std::make_pair(kExtensionId, kRuleId2);
305 EXPECT_EQ(expected_pair, (*matches.begin())->id()); 306 EXPECT_EQ(expected_pair, (*matches.begin())->id());
306 } 307 }
307 308
308 TEST_F(WebRequestRulesRegistryTest, RemoveRulesImpl) { 309 TEST_F(WebRequestRulesRegistryTest, RemoveRulesImpl) {
309 scoped_refptr<TestWebRequestRulesRegistry> registry( 310 scoped_refptr<TestWebRequestRulesRegistry> registry(
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
415 error = registry->AddRules(kExtensionId, rules_to_add_1); 416 error = registry->AddRules(kExtensionId, rules_to_add_1);
416 EXPECT_EQ("", error); 417 EXPECT_EQ("", error);
417 418
418 std::vector<linked_ptr<api::events::Rule>> rules_to_add_2(1); 419 std::vector<linked_ptr<api::events::Rule>> rules_to_add_2(1);
419 rules_to_add_2[0] = CreateRedirectRule("http://www.bar.com"); 420 rules_to_add_2[0] = CreateRedirectRule("http://www.bar.com");
420 error = registry->AddRules(kExtensionId2, rules_to_add_2); 421 error = registry->AddRules(kExtensionId2, rules_to_add_2);
421 EXPECT_EQ("", error); 422 EXPECT_EQ("", error);
422 423
423 GURL url("http://www.google.com"); 424 GURL url("http://www.google.com");
424 net::TestURLRequestContext context; 425 net::TestURLRequestContext context;
425 std::unique_ptr<net::URLRequest> request( 426 std::unique_ptr<net::URLRequest> request(context.CreateRequest(
426 context.CreateRequest(url, net::DEFAULT_PRIORITY, NULL)); 427 url, net::DEFAULT_PRIORITY, NULL, TRAFFIC_ANNOTATION_FOR_TESTS));
427 WebRequestData request_data(request.get(), ON_BEFORE_REQUEST); 428 WebRequestData request_data(request.get(), ON_BEFORE_REQUEST);
428 std::list<LinkedPtrEventResponseDelta> deltas = 429 std::list<LinkedPtrEventResponseDelta> deltas =
429 registry->CreateDeltas(NULL, request_data, false); 430 registry->CreateDeltas(NULL, request_data, false);
430 431
431 // The second extension is installed later and will win for this reason 432 // The second extension is installed later and will win for this reason
432 // in conflict resolution. 433 // in conflict resolution.
433 ASSERT_EQ(2u, deltas.size()); 434 ASSERT_EQ(2u, deltas.size());
434 deltas.sort(&helpers::InDecreasingExtensionInstallationTimeOrder); 435 deltas.sort(&helpers::InDecreasingExtensionInstallationTimeOrder);
435 436
436 std::list<LinkedPtrEventResponseDelta>::iterator i = deltas.begin(); 437 std::list<LinkedPtrEventResponseDelta>::iterator i = deltas.begin();
(...skipping 27 matching lines...) Expand all
464 error = registry->AddRules(kExtensionId2, rules_to_add_2); 465 error = registry->AddRules(kExtensionId2, rules_to_add_2);
465 EXPECT_EQ("", error); 466 EXPECT_EQ("", error);
466 467
467 std::vector<linked_ptr<api::events::Rule>> rules_to_add_3(1); 468 std::vector<linked_ptr<api::events::Rule>> rules_to_add_3(1);
468 rules_to_add_3[0] = CreateIgnoreRule(); 469 rules_to_add_3[0] = CreateIgnoreRule();
469 error = registry->AddRules(kExtensionId, rules_to_add_3); 470 error = registry->AddRules(kExtensionId, rules_to_add_3);
470 EXPECT_EQ("", error); 471 EXPECT_EQ("", error);
471 472
472 GURL url("http://www.google.com/index.html"); 473 GURL url("http://www.google.com/index.html");
473 net::TestURLRequestContext context; 474 net::TestURLRequestContext context;
474 std::unique_ptr<net::URLRequest> request( 475 std::unique_ptr<net::URLRequest> request(context.CreateRequest(
475 context.CreateRequest(url, net::DEFAULT_PRIORITY, NULL)); 476 url, net::DEFAULT_PRIORITY, NULL, TRAFFIC_ANNOTATION_FOR_TESTS));
476 WebRequestData request_data(request.get(), ON_BEFORE_REQUEST); 477 WebRequestData request_data(request.get(), ON_BEFORE_REQUEST);
477 std::list<LinkedPtrEventResponseDelta> deltas = 478 std::list<LinkedPtrEventResponseDelta> deltas =
478 registry->CreateDeltas(NULL, request_data, false); 479 registry->CreateDeltas(NULL, request_data, false);
479 480
480 // The redirect by the first extension is ignored due to the ignore rule. 481 // The redirect by the first extension is ignored due to the ignore rule.
481 ASSERT_EQ(1u, deltas.size()); 482 ASSERT_EQ(1u, deltas.size());
482 LinkedPtrEventResponseDelta effective_rule = *(deltas.begin()); 483 LinkedPtrEventResponseDelta effective_rule = *(deltas.begin());
483 484
484 EXPECT_EQ(kExtensionId2, effective_rule->extension_id); 485 EXPECT_EQ(kExtensionId2, effective_rule->extension_id);
485 EXPECT_EQ(base::Time() + base::TimeDelta::FromDays(2), 486 EXPECT_EQ(base::Time() + base::TimeDelta::FromDays(2),
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
538 ASSERT_TRUE(api::events::Rule::Populate(*value2, rules[1].get())); 539 ASSERT_TRUE(api::events::Rule::Populate(*value2, rules[1].get()));
539 540
540 scoped_refptr<WebRequestRulesRegistry> registry( 541 scoped_refptr<WebRequestRulesRegistry> registry(
541 new TestWebRequestRulesRegistry(extension_info_map_)); 542 new TestWebRequestRulesRegistry(extension_info_map_));
542 std::string error = registry->AddRulesImpl(kExtensionId, rules); 543 std::string error = registry->AddRulesImpl(kExtensionId, rules);
543 EXPECT_EQ("", error); 544 EXPECT_EQ("", error);
544 EXPECT_FALSE(registry->IsEmpty()); 545 EXPECT_FALSE(registry->IsEmpty());
545 546
546 GURL url("http://www.foo.com/test"); 547 GURL url("http://www.foo.com/test");
547 net::TestURLRequestContext context; 548 net::TestURLRequestContext context;
548 std::unique_ptr<net::URLRequest> request( 549 std::unique_ptr<net::URLRequest> request(context.CreateRequest(
549 context.CreateRequest(url, net::DEFAULT_PRIORITY, NULL)); 550 url, net::DEFAULT_PRIORITY, NULL, TRAFFIC_ANNOTATION_FOR_TESTS));
550 WebRequestData request_data(request.get(), ON_BEFORE_REQUEST); 551 WebRequestData request_data(request.get(), ON_BEFORE_REQUEST);
551 std::list<LinkedPtrEventResponseDelta> deltas = 552 std::list<LinkedPtrEventResponseDelta> deltas =
552 registry->CreateDeltas(NULL, request_data, false); 553 registry->CreateDeltas(NULL, request_data, false);
553 554
554 // The redirect by the redirect rule is ignored due to the ignore rule. 555 // The redirect by the redirect rule is ignored due to the ignore rule.
555 std::set<const WebRequestRule*> matches = registry->GetMatches(request_data); 556 std::set<const WebRequestRule*> matches = registry->GetMatches(request_data);
556 EXPECT_EQ(2u, matches.size()); 557 EXPECT_EQ(2u, matches.size());
557 ASSERT_EQ(0u, deltas.size()); 558 ASSERT_EQ(0u, deltas.size());
558 } 559 }
559 560
(...skipping 28 matching lines...) Expand all
588 rules.push_back(CreateCancellingRule(kRuleId3, attributes)); 589 rules.push_back(CreateCancellingRule(kRuleId3, attributes));
589 590
590 error = registry->AddRules(kExtensionId, rules); 591 error = registry->AddRules(kExtensionId, rules);
591 EXPECT_EQ("", error); 592 EXPECT_EQ("", error);
592 EXPECT_EQ(1, registry->num_clear_cache_calls()); 593 EXPECT_EQ(1, registry->num_clear_cache_calls());
593 594
594 std::set<const WebRequestRule*> matches; 595 std::set<const WebRequestRule*> matches;
595 596
596 GURL http_url("http://www.example.com"); 597 GURL http_url("http://www.example.com");
597 net::TestURLRequestContext context; 598 net::TestURLRequestContext context;
598 std::unique_ptr<net::URLRequest> http_request( 599 std::unique_ptr<net::URLRequest> http_request(context.CreateRequest(
599 context.CreateRequest(http_url, net::DEFAULT_PRIORITY, NULL)); 600 http_url, net::DEFAULT_PRIORITY, NULL, TRAFFIC_ANNOTATION_FOR_TESTS));
600 WebRequestData request_data(http_request.get(), ON_BEFORE_REQUEST); 601 WebRequestData request_data(http_request.get(), ON_BEFORE_REQUEST);
601 matches = registry->GetMatches(request_data); 602 matches = registry->GetMatches(request_data);
602 EXPECT_EQ(1u, matches.size()); 603 EXPECT_EQ(1u, matches.size());
603 WebRequestRule::GlobalRuleId expected_pair = std::make_pair(kExtensionId, 604 WebRequestRule::GlobalRuleId expected_pair = std::make_pair(kExtensionId,
604 kRuleId3); 605 kRuleId3);
605 EXPECT_EQ(expected_pair, (*matches.begin())->id()); 606 EXPECT_EQ(expected_pair, (*matches.begin())->id());
606 } 607 }
607 608
608 // Test that the url and firstPartyForCookiesUrl attributes are evaluated 609 // Test that the url and firstPartyForCookiesUrl attributes are evaluated
609 // against corresponding URLs. Tested on requests where these URLs actually 610 // against corresponding URLs. Tested on requests where these URLs actually
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
647 static_assert(arraysize(urls) == arraysize(firstPartyUrls), 648 static_assert(arraysize(urls) == arraysize(firstPartyUrls),
648 "urls and firstPartyUrls must have the same number " 649 "urls and firstPartyUrls must have the same number "
649 "of elements"); 650 "of elements");
650 static_assert(arraysize(urls) == arraysize(matchingRuleIds), 651 static_assert(arraysize(urls) == arraysize(matchingRuleIds),
651 "urls and matchingRuleIds must have the same number " 652 "urls and matchingRuleIds must have the same number "
652 "of elements"); 653 "of elements");
653 net::TestURLRequestContext context; 654 net::TestURLRequestContext context;
654 655
655 for (size_t i = 0; i < arraysize(matchingRuleIds); ++i) { 656 for (size_t i = 0; i < arraysize(matchingRuleIds); ++i) {
656 // Construct the inputs. 657 // Construct the inputs.
657 std::unique_ptr<net::URLRequest> http_request( 658 std::unique_ptr<net::URLRequest> http_request(context.CreateRequest(
658 context.CreateRequest(urls[i], net::DEFAULT_PRIORITY, NULL)); 659 urls[i], net::DEFAULT_PRIORITY, NULL, TRAFFIC_ANNOTATION_FOR_TESTS));
659 WebRequestData request_data(http_request.get(), ON_BEFORE_REQUEST); 660 WebRequestData request_data(http_request.get(), ON_BEFORE_REQUEST);
660 http_request->set_first_party_for_cookies(firstPartyUrls[i]); 661 http_request->set_first_party_for_cookies(firstPartyUrls[i]);
661 // Now run both rules on the input. 662 // Now run both rules on the input.
662 matches = registry->GetMatches(request_data); 663 matches = registry->GetMatches(request_data);
663 SCOPED_TRACE(testing::Message("i = ") << i << ", rule id = " 664 SCOPED_TRACE(testing::Message("i = ") << i << ", rule id = "
664 << matchingRuleIds[i]); 665 << matchingRuleIds[i]);
665 // Make sure that the right rule succeeded. 666 // Make sure that the right rule succeeded.
666 EXPECT_EQ(1u, matches.size()); 667 EXPECT_EQ(1u, matches.size());
667 EXPECT_EQ(WebRequestRule::GlobalRuleId(std::make_pair(kExtensionId, 668 EXPECT_EQ(WebRequestRule::GlobalRuleId(std::make_pair(kExtensionId,
668 matchingRuleIds[i])), 669 matchingRuleIds[i])),
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after
796 797
797 URLMatcher matcher; 798 URLMatcher matcher;
798 std::string error = registry->AddRulesImpl(kExtensionId, rules); 799 std::string error = registry->AddRulesImpl(kExtensionId, rules);
799 EXPECT_EQ("", error); 800 EXPECT_EQ("", error);
800 801
801 net::TestURLRequestContext context; 802 net::TestURLRequestContext context;
802 std::list<LinkedPtrEventResponseDelta> deltas; 803 std::list<LinkedPtrEventResponseDelta> deltas;
803 804
804 // No match because match is in the query parameter. 805 // No match because match is in the query parameter.
805 GURL url1("http://bar.com/index.html?foo.com"); 806 GURL url1("http://bar.com/index.html?foo.com");
806 std::unique_ptr<net::URLRequest> request1( 807 std::unique_ptr<net::URLRequest> request1(context.CreateRequest(
807 context.CreateRequest(url1, net::DEFAULT_PRIORITY, NULL)); 808 url1, net::DEFAULT_PRIORITY, NULL, TRAFFIC_ANNOTATION_FOR_TESTS));
808 WebRequestData request_data1(request1.get(), ON_BEFORE_REQUEST); 809 WebRequestData request_data1(request1.get(), ON_BEFORE_REQUEST);
809 deltas = registry->CreateDeltas(NULL, request_data1, false); 810 deltas = registry->CreateDeltas(NULL, request_data1, false);
810 EXPECT_EQ(0u, deltas.size()); 811 EXPECT_EQ(0u, deltas.size());
811 812
812 // This is a correct match. 813 // This is a correct match.
813 GURL url2("http://foo.com/index.html"); 814 GURL url2("http://foo.com/index.html");
814 std::unique_ptr<net::URLRequest> request2( 815 std::unique_ptr<net::URLRequest> request2(context.CreateRequest(
815 context.CreateRequest(url2, net::DEFAULT_PRIORITY, NULL)); 816 url2, net::DEFAULT_PRIORITY, NULL, TRAFFIC_ANNOTATION_FOR_TESTS));
816 WebRequestData request_data2(request2.get(), ON_BEFORE_REQUEST); 817 WebRequestData request_data2(request2.get(), ON_BEFORE_REQUEST);
817 deltas = registry->CreateDeltas(NULL, request_data2, false); 818 deltas = registry->CreateDeltas(NULL, request_data2, false);
818 EXPECT_EQ(1u, deltas.size()); 819 EXPECT_EQ(1u, deltas.size());
819 } 820 }
820 821
821 } // namespace extensions 822 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698