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

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

Issue 14358004: Almost all actions in Declarative Web Request require all_urls host permissions (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: All URLs -> all hosts; also rebased Created 7 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 | Annotate | Revision Log
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 "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 <string> 7 #include <string>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/basictypes.h" 10 #include "base/basictypes.h"
11 #include "base/json/json_reader.h" 11 #include "base/json/json_reader.h"
12 #include "base/memory/linked_ptr.h" 12 #include "base/memory/linked_ptr.h"
13 #include "base/message_loop.h" 13 #include "base/message_loop.h"
14 #include "base/stl_util.h" 14 #include "base/stl_util.h"
15 #include "base/test/values_test_util.h" 15 #include "base/test/values_test_util.h"
16 #include "base/values.h" 16 #include "base/values.h"
17 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_consta nts.h" 17 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_consta nts.h"
18 #include "chrome/browser/extensions/api/web_request/web_request_api_helpers.h" 18 #include "chrome/browser/extensions/api/web_request/web_request_api_helpers.h"
19 #include "chrome/common/extensions/extension_test_util.h"
19 #include "content/public/test/test_browser_thread.h" 20 #include "content/public/test/test_browser_thread.h"
20 #include "extensions/common/matcher/url_matcher_constants.h" 21 #include "extensions/common/matcher/url_matcher_constants.h"
21 #include "net/url_request/url_request_test_util.h" 22 #include "net/url_request/url_request_test_util.h"
22 #include "testing/gmock/include/gmock/gmock.h" 23 #include "testing/gmock/include/gmock/gmock.h"
23 #include "testing/gtest/include/gtest/gtest-message.h" 24 #include "testing/gtest/include/gtest/gtest-message.h"
24 #include "testing/gtest/include/gtest/gtest.h" 25 #include "testing/gtest/include/gtest/gtest.h"
25 26
26 namespace { 27 namespace {
27 const char kExtensionId[] = "ext1"; 28 const char kExtensionId[] = "ext1";
28 const char kExtensionId2[] = "ext2"; 29 const char kExtensionId2[] = "ext2";
29 const char kRuleId1[] = "rule1"; 30 const char kRuleId1[] = "rule1";
30 const char kRuleId2[] = "rule2"; 31 const char kRuleId2[] = "rule2";
31 const char kRuleId3[] = "rule3"; 32 const char kRuleId3[] = "rule3";
32 const char kRuleId4[] = "rule4"; 33 const char kRuleId4[] = "rule4";
33 } // namespace 34 } // namespace
34 35
35 namespace extensions { 36 namespace extensions {
36 37
38 using base::Value;
39 using extension_test_util::LoadManifest;
40 using extension_test_util::LoadManifestUnchecked;
37 using testing::HasSubstr; 41 using testing::HasSubstr;
38 42
39 namespace helpers = extension_web_request_api_helpers; 43 namespace helpers = extension_web_request_api_helpers;
40 namespace keys = declarative_webrequest_constants; 44 namespace keys = declarative_webrequest_constants;
41 namespace keys2 = url_matcher_constants; 45 namespace keys2 = url_matcher_constants;
42 46
43 class TestWebRequestRulesRegistry : public WebRequestRulesRegistry { 47 class TestWebRequestRulesRegistry : public WebRequestRulesRegistry {
44 public: 48 public:
45 TestWebRequestRulesRegistry() 49 explicit TestWebRequestRulesRegistry(
46 : WebRequestRulesRegistry(NULL, NULL), 50 scoped_refptr<ExtensionInfoMap> extension_info_map)
47 num_clear_cache_calls_(0) {} 51 : WebRequestRulesRegistry(NULL, NULL), num_clear_cache_calls_(0) {
52 SetExtensionInfoMapForTesting(extension_info_map);
53 }
48 54
49 // Returns how often the in-memory caches of the renderers were instructed 55 // Returns how often the in-memory caches of the renderers were instructed
50 // to be cleared. 56 // to be cleared.
51 int num_clear_cache_calls() const { return num_clear_cache_calls_; } 57 int num_clear_cache_calls() const { return num_clear_cache_calls_; }
52 58
53 // How many rules are there which have some conditions not triggered by URL 59 // How many rules are there which have some conditions not triggered by URL
54 // matches. 60 // matches.
55 size_t RulesWithoutTriggers() const { 61 size_t RulesWithoutTriggers() const {
56 return rules_with_untriggered_conditions_for_test().size(); 62 return rules_with_untriggered_conditions_for_test().size();
57 } 63 }
58 64
59 protected: 65 protected:
60 virtual ~TestWebRequestRulesRegistry() {} 66 virtual ~TestWebRequestRulesRegistry() {}
61 67
62 virtual base::Time GetExtensionInstallationTime(
63 const std::string& extension_id) const OVERRIDE {
64 if (extension_id == kExtensionId)
65 return base::Time() + base::TimeDelta::FromDays(1);
66 else if (extension_id == kExtensionId2)
67 return base::Time() + base::TimeDelta::FromDays(2);
68 else
69 return base::Time();
70 }
71
72 virtual void ClearCacheOnNavigation() OVERRIDE { 68 virtual void ClearCacheOnNavigation() OVERRIDE {
73 ++num_clear_cache_calls_; 69 ++num_clear_cache_calls_;
74 } 70 }
75 71
76 private: 72 private:
77 int num_clear_cache_calls_; 73 int num_clear_cache_calls_;
78 }; 74 };
79 75
80 class WebRequestRulesRegistryTest : public testing::Test { 76 class WebRequestRulesRegistryTest : public testing::Test {
81 public: 77 public:
82 WebRequestRulesRegistryTest() 78 WebRequestRulesRegistryTest()
83 : message_loop(MessageLoop::TYPE_IO), 79 : message_loop(MessageLoop::TYPE_IO),
84 ui(content::BrowserThread::UI, &message_loop), 80 ui(content::BrowserThread::UI, &message_loop),
85 io(content::BrowserThread::IO, &message_loop) {} 81 io(content::BrowserThread::IO, &message_loop) {}
86 82
87 virtual ~WebRequestRulesRegistryTest() {} 83 virtual ~WebRequestRulesRegistryTest() {}
88 84
85 virtual void SetUp() OVERRIDE;
86
89 virtual void TearDown() OVERRIDE { 87 virtual void TearDown() OVERRIDE {
90 // Make sure that deletion traits of all registries are executed. 88 // Make sure that deletion traits of all registries are executed.
91 message_loop.RunUntilIdle(); 89 message_loop.RunUntilIdle();
92 } 90 }
93 91
94 // Returns a rule that roughly matches http://*.example.com and 92 // Returns a rule that roughly matches http://*.example.com and
95 // https://www.example.com and cancels it 93 // https://www.example.com and cancels it
96 linked_ptr<RulesRegistry::Rule> CreateRule1() { 94 linked_ptr<RulesRegistry::Rule> CreateRule1() {
97 ListValue* scheme_http = new ListValue(); 95 ListValue* scheme_http = new ListValue();
98 scheme_http->Append(Value::CreateStringValue("http")); 96 scheme_http->Append(Value::CreateStringValue("http"));
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after
216 attributes.begin(); 214 attributes.begin();
217 it != attributes.end(); ++it) 215 it != attributes.end(); ++it)
218 rule->conditions.push_back(CreateCondition(**it)); 216 rule->conditions.push_back(CreateCondition(**it));
219 return rule; 217 return rule;
220 } 218 }
221 219
222 protected: 220 protected:
223 MessageLoop message_loop; 221 MessageLoop message_loop;
224 content::TestBrowserThread ui; 222 content::TestBrowserThread ui;
225 content::TestBrowserThread io; 223 content::TestBrowserThread io;
224 // Two extensions with host permissions for all URLs and the DWR permission.
225 // Installation times will be so that |extension_| is older than
226 // |extension2_|.
227 scoped_refptr<Extension> extension_;
228 scoped_refptr<Extension> extension2_;
229 scoped_refptr<ExtensionInfoMap> extension_info_map_;
226 }; 230 };
227 231
232 void WebRequestRulesRegistryTest::SetUp() {
233 testing::Test::SetUp();
234
235 std::string error;
236 extension_ = LoadManifestUnchecked("permissions",
237 "web_request_all_host_permissions.json",
238 Manifest::INVALID_LOCATION,
239 Extension::NO_FLAGS,
240 kExtensionId,
241 &error);
242 ASSERT_TRUE(extension_) << error;
243 extension2_ =
244 LoadManifestUnchecked("permissions",
245 "web_request_all_host_permissions.json",
246 Manifest::INVALID_LOCATION,
247 Extension::NO_FLAGS,
248 kExtensionId2,
249 &error);
250 ASSERT_TRUE(extension2_) << error;
251 extension_info_map_ = new ExtensionInfoMap;
252 ASSERT_TRUE(extension_info_map_);
253 extension_info_map_->AddExtension(extension_.get(),
254 base::Time() + base::TimeDelta::FromDays(1),
255 false /*incognito_enabled*/);
256 extension_info_map_->AddExtension(extension2_.get(),
257 base::Time() + base::TimeDelta::FromDays(2),
258 false /*incognito_enabled*/);
259 }
260
261
228 TEST_F(WebRequestRulesRegistryTest, AddRulesImpl) { 262 TEST_F(WebRequestRulesRegistryTest, AddRulesImpl) {
229 scoped_refptr<TestWebRequestRulesRegistry> registry( 263 scoped_refptr<TestWebRequestRulesRegistry> registry(
230 new TestWebRequestRulesRegistry()); 264 new TestWebRequestRulesRegistry(extension_info_map_));
231 std::string error; 265 std::string error;
232 266
233 std::vector<linked_ptr<RulesRegistry::Rule> > rules; 267 std::vector<linked_ptr<RulesRegistry::Rule> > rules;
234 rules.push_back(CreateRule1()); 268 rules.push_back(CreateRule1());
235 rules.push_back(CreateRule2()); 269 rules.push_back(CreateRule2());
236 270
237 error = registry->AddRules(kExtensionId, rules); 271 error = registry->AddRules(kExtensionId, rules);
238 EXPECT_EQ("", error); 272 EXPECT_EQ("", error);
239 EXPECT_EQ(1, registry->num_clear_cache_calls()); 273 EXPECT_EQ(1, registry->num_clear_cache_calls());
240 274
(...skipping 18 matching lines...) Expand all
259 request_data.request = &foobar_request; 293 request_data.request = &foobar_request;
260 matches = registry->GetMatches(request_data); 294 matches = registry->GetMatches(request_data);
261 EXPECT_EQ(1u, matches.size()); 295 EXPECT_EQ(1u, matches.size());
262 WebRequestRule::GlobalRuleId expected_pair = 296 WebRequestRule::GlobalRuleId expected_pair =
263 std::make_pair(kExtensionId, kRuleId2); 297 std::make_pair(kExtensionId, kRuleId2);
264 EXPECT_EQ(expected_pair, (*matches.begin())->id()); 298 EXPECT_EQ(expected_pair, (*matches.begin())->id());
265 } 299 }
266 300
267 TEST_F(WebRequestRulesRegistryTest, RemoveRulesImpl) { 301 TEST_F(WebRequestRulesRegistryTest, RemoveRulesImpl) {
268 scoped_refptr<TestWebRequestRulesRegistry> registry( 302 scoped_refptr<TestWebRequestRulesRegistry> registry(
269 new TestWebRequestRulesRegistry()); 303 new TestWebRequestRulesRegistry(extension_info_map_));
270 std::string error; 304 std::string error;
271 305
272 // Setup RulesRegistry to contain two rules. 306 // Setup RulesRegistry to contain two rules.
273 std::vector<linked_ptr<RulesRegistry::Rule> > rules_to_add; 307 std::vector<linked_ptr<RulesRegistry::Rule> > rules_to_add;
274 rules_to_add.push_back(CreateRule1()); 308 rules_to_add.push_back(CreateRule1());
275 rules_to_add.push_back(CreateRule2()); 309 rules_to_add.push_back(CreateRule2());
276 error = registry->AddRules(kExtensionId, rules_to_add); 310 error = registry->AddRules(kExtensionId, rules_to_add);
277 EXPECT_EQ("", error); 311 EXPECT_EQ("", error);
278 EXPECT_EQ(1, registry->num_clear_cache_calls()); 312 EXPECT_EQ(1, registry->num_clear_cache_calls());
279 313
(...skipping 27 matching lines...) Expand all
307 registered_rules.clear(); 341 registered_rules.clear();
308 registry->GetAllRules(kExtensionId, &registered_rules); 342 registry->GetAllRules(kExtensionId, &registered_rules);
309 EXPECT_EQ(0u, registered_rules.size()); 343 EXPECT_EQ(0u, registered_rules.size());
310 EXPECT_EQ(0u, registry->RulesWithoutTriggers()); 344 EXPECT_EQ(0u, registry->RulesWithoutTriggers());
311 345
312 EXPECT_TRUE(registry->IsEmpty()); 346 EXPECT_TRUE(registry->IsEmpty());
313 } 347 }
314 348
315 TEST_F(WebRequestRulesRegistryTest, RemoveAllRulesImpl) { 349 TEST_F(WebRequestRulesRegistryTest, RemoveAllRulesImpl) {
316 scoped_refptr<TestWebRequestRulesRegistry> registry( 350 scoped_refptr<TestWebRequestRulesRegistry> registry(
317 new TestWebRequestRulesRegistry()); 351 new TestWebRequestRulesRegistry(extension_info_map_));
318 std::string error; 352 std::string error;
319 353
320 // Setup RulesRegistry to contain two rules, one for each extension. 354 // Setup RulesRegistry to contain two rules, one for each extension.
321 std::vector<linked_ptr<RulesRegistry::Rule> > rules_to_add(1); 355 std::vector<linked_ptr<RulesRegistry::Rule> > rules_to_add(1);
322 rules_to_add[0] = CreateRule1(); 356 rules_to_add[0] = CreateRule1();
323 error = registry->AddRules(kExtensionId, rules_to_add); 357 error = registry->AddRules(kExtensionId, rules_to_add);
324 EXPECT_EQ("", error); 358 EXPECT_EQ("", error);
325 EXPECT_EQ(1, registry->num_clear_cache_calls()); 359 EXPECT_EQ(1, registry->num_clear_cache_calls());
326 360
327 rules_to_add[0] = CreateRule2(); 361 rules_to_add[0] = CreateRule2();
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
359 error = registry->RemoveAllRules(kExtensionId2); 393 error = registry->RemoveAllRules(kExtensionId2);
360 EXPECT_EQ("", error); 394 EXPECT_EQ("", error);
361 EXPECT_EQ(5, registry->num_clear_cache_calls()); 395 EXPECT_EQ(5, registry->num_clear_cache_calls());
362 396
363 EXPECT_TRUE(registry->IsEmpty()); 397 EXPECT_TRUE(registry->IsEmpty());
364 } 398 }
365 399
366 // Test precedences between extensions. 400 // Test precedences between extensions.
367 TEST_F(WebRequestRulesRegistryTest, Precedences) { 401 TEST_F(WebRequestRulesRegistryTest, Precedences) {
368 scoped_refptr<WebRequestRulesRegistry> registry( 402 scoped_refptr<WebRequestRulesRegistry> registry(
369 new TestWebRequestRulesRegistry()); 403 new TestWebRequestRulesRegistry(extension_info_map_));
370 std::string error; 404 std::string error;
371 405
372 std::vector<linked_ptr<RulesRegistry::Rule> > rules_to_add_1(1); 406 std::vector<linked_ptr<RulesRegistry::Rule> > rules_to_add_1(1);
373 rules_to_add_1[0] = CreateRedirectRule("http://www.foo.com"); 407 rules_to_add_1[0] = CreateRedirectRule("http://www.foo.com");
374 error = registry->AddRules(kExtensionId, rules_to_add_1); 408 error = registry->AddRules(kExtensionId, rules_to_add_1);
375 EXPECT_EQ("", error); 409 EXPECT_EQ("", error);
376 410
377 std::vector<linked_ptr<RulesRegistry::Rule> > rules_to_add_2(1); 411 std::vector<linked_ptr<RulesRegistry::Rule> > rules_to_add_2(1);
378 rules_to_add_2[0] = CreateRedirectRule("http://www.bar.com"); 412 rules_to_add_2[0] = CreateRedirectRule("http://www.bar.com");
379 error = registry->AddRules(kExtensionId2, rules_to_add_2); 413 error = registry->AddRules(kExtensionId2, rules_to_add_2);
(...skipping 22 matching lines...) Expand all
402 436
403 EXPECT_EQ(kExtensionId, loser->extension_id); 437 EXPECT_EQ(kExtensionId, loser->extension_id);
404 EXPECT_EQ(base::Time() + base::TimeDelta::FromDays(1), 438 EXPECT_EQ(base::Time() + base::TimeDelta::FromDays(1),
405 loser->extension_install_time); 439 loser->extension_install_time);
406 EXPECT_EQ(GURL("http://www.foo.com"), loser->new_url); 440 EXPECT_EQ(GURL("http://www.foo.com"), loser->new_url);
407 } 441 }
408 442
409 // Test priorities of rules within one extension. 443 // Test priorities of rules within one extension.
410 TEST_F(WebRequestRulesRegistryTest, Priorities) { 444 TEST_F(WebRequestRulesRegistryTest, Priorities) {
411 scoped_refptr<WebRequestRulesRegistry> registry( 445 scoped_refptr<WebRequestRulesRegistry> registry(
412 new TestWebRequestRulesRegistry()); 446 new TestWebRequestRulesRegistry(extension_info_map_));
413 std::string error; 447 std::string error;
414 448
415 std::vector<linked_ptr<RulesRegistry::Rule> > rules_to_add_1(1); 449 std::vector<linked_ptr<RulesRegistry::Rule> > rules_to_add_1(1);
416 rules_to_add_1[0] = CreateRedirectRule("http://www.foo.com"); 450 rules_to_add_1[0] = CreateRedirectRule("http://www.foo.com");
417 error = registry->AddRules(kExtensionId, rules_to_add_1); 451 error = registry->AddRules(kExtensionId, rules_to_add_1);
418 EXPECT_EQ("", error); 452 EXPECT_EQ("", error);
419 453
420 std::vector<linked_ptr<RulesRegistry::Rule> > rules_to_add_2(1); 454 std::vector<linked_ptr<RulesRegistry::Rule> > rules_to_add_2(1);
421 rules_to_add_2[0] = CreateRedirectRule("http://www.bar.com"); 455 rules_to_add_2[0] = CreateRedirectRule("http://www.bar.com");
422 error = registry->AddRules(kExtensionId2, rules_to_add_2); 456 error = registry->AddRules(kExtensionId2, rules_to_add_2);
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
488 scoped_ptr<Value> value2(base::JSONReader::Read(kRule2)); 522 scoped_ptr<Value> value2(base::JSONReader::Read(kRule2));
489 ASSERT_TRUE(value2.get()); 523 ASSERT_TRUE(value2.get());
490 524
491 std::vector<linked_ptr<RulesRegistry::Rule> > rules; 525 std::vector<linked_ptr<RulesRegistry::Rule> > rules;
492 rules.push_back(make_linked_ptr(new RulesRegistry::Rule)); 526 rules.push_back(make_linked_ptr(new RulesRegistry::Rule));
493 rules.push_back(make_linked_ptr(new RulesRegistry::Rule)); 527 rules.push_back(make_linked_ptr(new RulesRegistry::Rule));
494 ASSERT_TRUE(RulesRegistry::Rule::Populate(*value1, rules[0].get())); 528 ASSERT_TRUE(RulesRegistry::Rule::Populate(*value1, rules[0].get()));
495 ASSERT_TRUE(RulesRegistry::Rule::Populate(*value2, rules[1].get())); 529 ASSERT_TRUE(RulesRegistry::Rule::Populate(*value2, rules[1].get()));
496 530
497 scoped_refptr<WebRequestRulesRegistry> registry( 531 scoped_refptr<WebRequestRulesRegistry> registry(
498 new TestWebRequestRulesRegistry()); 532 new TestWebRequestRulesRegistry(extension_info_map_));
499 std::string error = registry->AddRulesImpl(kExtensionId, rules); 533 std::string error = registry->AddRulesImpl(kExtensionId, rules);
500 EXPECT_EQ("", error); 534 EXPECT_EQ("", error);
501 EXPECT_FALSE(registry->IsEmpty()); 535 EXPECT_FALSE(registry->IsEmpty());
502 536
503 GURL url("http://www.foo.com/test"); 537 GURL url("http://www.foo.com/test");
504 net::TestURLRequestContext context; 538 net::TestURLRequestContext context;
505 net::TestURLRequest request(url, NULL, &context, NULL); 539 net::TestURLRequest request(url, NULL, &context, NULL);
506 WebRequestData request_data(&request, ON_BEFORE_REQUEST); 540 WebRequestData request_data(&request, ON_BEFORE_REQUEST);
507 std::list<LinkedPtrEventResponseDelta> deltas = 541 std::list<LinkedPtrEventResponseDelta> deltas =
508 registry->CreateDeltas(NULL, request_data, false); 542 registry->CreateDeltas(NULL, request_data, false);
509 543
510 // The redirect by the redirect rule is ignored due to the ignore rule. 544 // The redirect by the redirect rule is ignored due to the ignore rule.
511 std::set<const WebRequestRule*> matches = registry->GetMatches(request_data); 545 std::set<const WebRequestRule*> matches = registry->GetMatches(request_data);
512 EXPECT_EQ(2u, matches.size()); 546 EXPECT_EQ(2u, matches.size());
513 ASSERT_EQ(0u, deltas.size()); 547 ASSERT_EQ(0u, deltas.size());
514 } 548 }
515 549
516 // Test that rules failing IsFulfilled on their conditions are never returned by 550 // Test that rules failing IsFulfilled on their conditions are never returned by
517 // GetMatches. 551 // GetMatches.
518 TEST_F(WebRequestRulesRegistryTest, GetMatchesCheckFulfilled) { 552 TEST_F(WebRequestRulesRegistryTest, GetMatchesCheckFulfilled) {
519 scoped_refptr<TestWebRequestRulesRegistry> registry( 553 scoped_refptr<TestWebRequestRulesRegistry> registry(
520 new TestWebRequestRulesRegistry()); 554 new TestWebRequestRulesRegistry(extension_info_map_));
521 const std::string kMatchingUrlAttribute( 555 const std::string kMatchingUrlAttribute(
522 "\"url\": { \"pathContains\": \"\" }, \n"); 556 "\"url\": { \"pathContains\": \"\" }, \n");
523 const std::string kNonMatchingNonUrlAttribute( 557 const std::string kNonMatchingNonUrlAttribute(
524 "\"resourceType\": [\"stylesheet\"], \n"); 558 "\"resourceType\": [\"stylesheet\"], \n");
525 const std::string kBothAttributes(kMatchingUrlAttribute + 559 const std::string kBothAttributes(kMatchingUrlAttribute +
526 kNonMatchingNonUrlAttribute); 560 kNonMatchingNonUrlAttribute);
527 std::string error; 561 std::string error;
528 std::vector<const std::string*> attributes; 562 std::vector<const std::string*> attributes;
529 std::vector<linked_ptr<RulesRegistry::Rule> > rules; 563 std::vector<linked_ptr<RulesRegistry::Rule> > rules;
530 564
(...skipping 27 matching lines...) Expand all
558 WebRequestRule::GlobalRuleId expected_pair = std::make_pair(kExtensionId, 592 WebRequestRule::GlobalRuleId expected_pair = std::make_pair(kExtensionId,
559 kRuleId3); 593 kRuleId3);
560 EXPECT_EQ(expected_pair, (*matches.begin())->id()); 594 EXPECT_EQ(expected_pair, (*matches.begin())->id());
561 } 595 }
562 596
563 // Test that the url and firstPartyForCookiesUrl attributes are evaluated 597 // Test that the url and firstPartyForCookiesUrl attributes are evaluated
564 // against corresponding URLs. Tested on requests where these URLs actually 598 // against corresponding URLs. Tested on requests where these URLs actually
565 // differ. 599 // differ.
566 TEST_F(WebRequestRulesRegistryTest, GetMatchesDifferentUrls) { 600 TEST_F(WebRequestRulesRegistryTest, GetMatchesDifferentUrls) {
567 scoped_refptr<TestWebRequestRulesRegistry> registry( 601 scoped_refptr<TestWebRequestRulesRegistry> registry(
568 new TestWebRequestRulesRegistry()); 602 new TestWebRequestRulesRegistry(extension_info_map_));
569 const std::string kUrlAttribute( 603 const std::string kUrlAttribute(
570 "\"url\": { \"hostContains\": \"url\" }, \n"); 604 "\"url\": { \"hostContains\": \"url\" }, \n");
571 const std::string kFirstPartyUrlAttribute( 605 const std::string kFirstPartyUrlAttribute(
572 "\"firstPartyForCookiesUrl\": { \"hostContains\": \"fpfc\" }, \n"); 606 "\"firstPartyForCookiesUrl\": { \"hostContains\": \"fpfc\" }, \n");
573 std::string error; 607 std::string error;
574 std::vector<const std::string*> attributes; 608 std::vector<const std::string*> attributes;
575 std::vector<linked_ptr<RulesRegistry::Rule> > rules; 609 std::vector<linked_ptr<RulesRegistry::Rule> > rules;
576 610
577 // Rule 1 has one condition, with a url attribute 611 // Rule 1 has one condition, with a url attribute
578 attributes.push_back(&kUrlAttribute); 612 attributes.push_back(&kUrlAttribute);
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
615 SCOPED_TRACE(testing::Message("i = ") << i << ", rule id = " 649 SCOPED_TRACE(testing::Message("i = ") << i << ", rule id = "
616 << matchingRuleIds[i]); 650 << matchingRuleIds[i]);
617 // Make sure that the right rule succeeded. 651 // Make sure that the right rule succeeded.
618 EXPECT_EQ(1u, matches.size()); 652 EXPECT_EQ(1u, matches.size());
619 EXPECT_EQ(WebRequestRule::GlobalRuleId(std::make_pair(kExtensionId, 653 EXPECT_EQ(WebRequestRule::GlobalRuleId(std::make_pair(kExtensionId,
620 matchingRuleIds[i])), 654 matchingRuleIds[i])),
621 (*matches.begin())->id()); 655 (*matches.begin())->id());
622 } 656 }
623 } 657 }
624 658
625 TEST_F(WebRequestRulesRegistryTest, CheckConsistency) { 659 TEST(WebRequestRulesRegistrySimpleTest, StageChecker) {
626 // The contentType condition can only be evaluated during ON_HEADERS_RECEIVED 660 // The contentType condition can only be evaluated during ON_HEADERS_RECEIVED
627 // but the redirect action can only be executed during ON_BEFORE_REQUEST. 661 // but the redirect action can only be executed during ON_BEFORE_REQUEST.
628 // Therefore, this is an inconsistent rule that needs to be flagged. 662 // Therefore, this is an inconsistent rule that needs to be flagged.
629 const char kRule[] = 663 const char kRule[] =
630 "{ \n" 664 "{ \n"
631 " \"id\": \"rule1\", \n" 665 " \"id\": \"rule1\", \n"
632 " \"conditions\": [ \n" 666 " \"conditions\": [ \n"
633 " { \n" 667 " { \n"
634 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n" 668 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n"
635 " \"url\": {\"hostSuffix\": \"foo.com\"}, \n" 669 " \"url\": {\"hostSuffix\": \"foo.com\"}, \n"
636 " \"contentType\": [\"image/jpeg\"] \n" 670 " \"contentType\": [\"image/jpeg\"] \n"
637 " } \n" 671 " } \n"
638 " ], \n" 672 " ], \n"
639 " \"actions\": [ \n" 673 " \"actions\": [ \n"
640 " { \n" 674 " { \n"
641 " \"instanceType\": \"declarativeWebRequest.RedirectRequest\",\n" 675 " \"instanceType\": \"declarativeWebRequest.RedirectRequest\",\n"
642 " \"redirectUrl\": \"http://bar.com\" \n" 676 " \"redirectUrl\": \"http://bar.com\" \n"
643 " } \n" 677 " } \n"
644 " ], \n" 678 " ], \n"
645 " \"priority\": 200 \n" 679 " \"priority\": 200 \n"
646 "} "; 680 "} ";
647 681
648 scoped_ptr<Value> value(base::JSONReader::Read(kRule)); 682 scoped_ptr<Value> value(base::JSONReader::Read(kRule));
649 ASSERT_TRUE(value.get()); 683 ASSERT_TRUE(value);
650 684
651 std::vector<linked_ptr<RulesRegistry::Rule> > rules; 685 RulesRegistry::Rule rule;
652 rules.push_back(make_linked_ptr(new RulesRegistry::Rule)); 686 ASSERT_TRUE(RulesRegistry::Rule::Populate(*value, &rule));
653 ASSERT_TRUE(RulesRegistry::Rule::Populate(*value, rules.back().get()));
654 687
655 scoped_refptr<WebRequestRulesRegistry> registry( 688 std::string error;
656 new TestWebRequestRulesRegistry()); 689 URLMatcher matcher;
690 scoped_ptr<WebRequestConditionSet> conditions =
691 WebRequestConditionSet::Create(
692 matcher.condition_factory(), rule.conditions, &error);
693 ASSERT_TRUE(error.empty()) << error;
694 ASSERT_TRUE(conditions);
657 695
658 URLMatcher matcher; 696 bool bad_message = false;
659 std::string error = registry->AddRulesImpl(kExtensionId, rules); 697 scoped_ptr<WebRequestActionSet> actions =
698 WebRequestActionSet::Create(rule.actions, &error, &bad_message);
699 ASSERT_TRUE(error.empty()) << error;
700 ASSERT_FALSE(bad_message);
701 ASSERT_TRUE(actions);
702
703 EXPECT_FALSE(WebRequestRulesRegistry::StageChecker(
704 conditions.get(), actions.get(), &error));
660 EXPECT_THAT(error, HasSubstr("no time in the request life-cycle")); 705 EXPECT_THAT(error, HasSubstr("no time in the request life-cycle"));
661 EXPECT_TRUE(registry->IsEmpty()); 706 EXPECT_THAT(error, HasSubstr(actions->actions().back()->GetName()));
707 }
708
709 TEST(WebRequestRulesRegistrySimpleTest, HostPermissionsChecker) {
710 const char kAction[] = // This action requires all URLs host permission.
711 "{ \n"
712 " \"instanceType\": \"declarativeWebRequest.RedirectRequest\",\n"
713 " \"redirectUrl\": \"http://bar.com\" \n"
714 "} ";
715 scoped_ptr<Value> action_value(base::JSONReader::Read(kAction));
716 ASSERT_TRUE(action_value);
717
718 WebRequestActionSet::AnyVector actions;
719 actions.push_back(linked_ptr<base::Value>(action_value.release()));
720 ASSERT_TRUE(actions.back().get());
721
722 std::string error;
723 bool bad_message = false;
724 scoped_ptr<WebRequestActionSet> action_set(
725 WebRequestActionSet::Create(actions, &error, &bad_message));
726 ASSERT_TRUE(error.empty()) << error;
727 ASSERT_FALSE(bad_message);
728 ASSERT_TRUE(action_set);
729
730 scoped_refptr<Extension> extension_no_url(
731 LoadManifest("permissions", "web_request_no_host.json"));
732 scoped_refptr<Extension> extension_some_urls(
733 LoadManifest("permissions", "web_request_com_host_permissions.json"));
734 scoped_refptr<Extension> extension_all_urls(
735 LoadManifest("permissions", "web_request_all_host_permissions.json"));
736
737 EXPECT_TRUE(WebRequestRulesRegistry::HostPermissionsChecker(
738 extension_all_urls.get(), action_set.get(), &error));
739 EXPECT_TRUE(error.empty()) << error;
740
741 EXPECT_FALSE(WebRequestRulesRegistry::HostPermissionsChecker(
742 extension_some_urls.get(), action_set.get(), &error));
743 EXPECT_THAT(error, HasSubstr("permission for all"));
744 EXPECT_THAT(error, HasSubstr(action_set->actions().back()->GetName()));
745
746 EXPECT_FALSE(WebRequestRulesRegistry::HostPermissionsChecker(
747 extension_no_url.get(), action_set.get(), &error));
748 EXPECT_THAT(error, HasSubstr("permission for all"));
749 EXPECT_THAT(error, HasSubstr(action_set->actions().back()->GetName()));
662 } 750 }
663 751
664 TEST_F(WebRequestRulesRegistryTest, CheckOriginAndPathRegEx) { 752 TEST_F(WebRequestRulesRegistryTest, CheckOriginAndPathRegEx) {
665 const char kRule[] = 753 const char kRule[] =
666 "{ \n" 754 "{ \n"
667 " \"id\": \"rule1\", \n" 755 " \"id\": \"rule1\", \n"
668 " \"conditions\": [ \n" 756 " \"conditions\": [ \n"
669 " { \n" 757 " { \n"
670 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n" 758 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n"
671 " \"url\": {\"originAndPathMatches\": \"fo+.com\"} \n" 759 " \"url\": {\"originAndPathMatches\": \"fo+.com\"} \n"
672 " } \n" 760 " } \n"
673 " ], \n" 761 " ], \n"
674 " \"actions\": [ \n" 762 " \"actions\": [ \n"
675 " { \n" 763 " { \n"
676 " \"instanceType\": \"declarativeWebRequest.RedirectRequest\",\n" 764 " \"instanceType\": \"declarativeWebRequest.RedirectRequest\",\n"
677 " \"redirectUrl\": \"http://bar.com\" \n" 765 " \"redirectUrl\": \"http://bar.com\" \n"
678 " } \n" 766 " } \n"
679 " ], \n" 767 " ], \n"
680 " \"priority\": 200 \n" 768 " \"priority\": 200 \n"
681 "} "; 769 "} ";
682 770
683 scoped_ptr<Value> value(base::JSONReader::Read(kRule)); 771 scoped_ptr<Value> value(base::JSONReader::Read(kRule));
684 ASSERT_TRUE(value.get()); 772 ASSERT_TRUE(value.get());
685 773
686 std::vector<linked_ptr<RulesRegistry::Rule> > rules; 774 std::vector<linked_ptr<RulesRegistry::Rule> > rules;
687 rules.push_back(make_linked_ptr(new RulesRegistry::Rule)); 775 rules.push_back(make_linked_ptr(new RulesRegistry::Rule));
688 ASSERT_TRUE(RulesRegistry::Rule::Populate(*value, rules.back().get())); 776 ASSERT_TRUE(RulesRegistry::Rule::Populate(*value, rules.back().get()));
689 777
690 scoped_refptr<WebRequestRulesRegistry> registry( 778 scoped_refptr<WebRequestRulesRegistry> registry(
691 new TestWebRequestRulesRegistry()); 779 new TestWebRequestRulesRegistry(extension_info_map_));
692 780
693 URLMatcher matcher; 781 URLMatcher matcher;
694 std::string error = registry->AddRulesImpl(kExtensionId, rules); 782 std::string error = registry->AddRulesImpl(kExtensionId, rules);
695 EXPECT_EQ("", error); 783 EXPECT_EQ("", error);
696 784
697 net::TestURLRequestContext context; 785 net::TestURLRequestContext context;
698 std::list<LinkedPtrEventResponseDelta> deltas; 786 std::list<LinkedPtrEventResponseDelta> deltas;
699 787
700 // No match because match is in the query parameter. 788 // No match because match is in the query parameter.
701 GURL url1("http://bar.com/index.html?foo.com"); 789 GURL url1("http://bar.com/index.html?foo.com");
702 net::TestURLRequest request1(url1, NULL, &context, NULL); 790 net::TestURLRequest request1(url1, NULL, &context, NULL);
703 WebRequestData request_data1(&request1, ON_BEFORE_REQUEST); 791 WebRequestData request_data1(&request1, ON_BEFORE_REQUEST);
704 deltas = registry->CreateDeltas(NULL, request_data1, false); 792 deltas = registry->CreateDeltas(NULL, request_data1, false);
705 EXPECT_EQ(0u, deltas.size()); 793 EXPECT_EQ(0u, deltas.size());
706 794
707 // This is a correct match. 795 // This is a correct match.
708 GURL url2("http://foo.com/index.html"); 796 GURL url2("http://foo.com/index.html");
709 net::TestURLRequest request2(url2, NULL, &context, NULL); 797 net::TestURLRequest request2(url2, NULL, &context, NULL);
710 WebRequestData request_data2(&request2, ON_BEFORE_REQUEST); 798 WebRequestData request_data2(&request2, ON_BEFORE_REQUEST);
711 deltas = registry->CreateDeltas(NULL, request_data2, false); 799 deltas = registry->CreateDeltas(NULL, request_data2, false);
712 EXPECT_EQ(1u, deltas.size()); 800 EXPECT_EQ(1u, deltas.size());
713 } 801 }
714 802
715 } // namespace extensions 803 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698