| 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 <string> |
| 7 #include <vector> | 8 #include <vector> |
| 8 | 9 |
| 10 #include "base/basictypes.h" |
| 9 #include "base/json/json_reader.h" | 11 #include "base/json/json_reader.h" |
| 10 #include "base/memory/linked_ptr.h" | 12 #include "base/memory/linked_ptr.h" |
| 11 #include "base/message_loop.h" | 13 #include "base/message_loop.h" |
| 12 #include "base/stl_util.h" | 14 #include "base/stl_util.h" |
| 13 #include "base/test/values_test_util.h" | 15 #include "base/test/values_test_util.h" |
| 14 #include "base/values.h" | 16 #include "base/values.h" |
| 15 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_consta
nts.h" | 17 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_consta
nts.h" |
| 16 #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" |
| 17 #include "chrome/common/extensions/matcher/url_matcher_constants.h" | 19 #include "chrome/common/extensions/matcher/url_matcher_constants.h" |
| 18 #include "content/public/test/test_browser_thread.h" | 20 #include "content/public/test/test_browser_thread.h" |
| 19 #include "net/url_request/url_request_test_util.h" | 21 #include "net/url_request/url_request_test_util.h" |
| 20 #include "testing/gmock/include/gmock/gmock.h" | 22 #include "testing/gmock/include/gmock/gmock.h" |
| 23 #include "testing/gtest/include/gtest/gtest-message.h" |
| 21 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
| 22 | 25 |
| 23 namespace { | 26 namespace { |
| 24 const char kExtensionId[] = "ext1"; | 27 const char kExtensionId[] = "ext1"; |
| 25 const char kExtensionId2[] = "ext2"; | 28 const char kExtensionId2[] = "ext2"; |
| 26 const char kRuleId1[] = "rule1"; | 29 const char kRuleId1[] = "rule1"; |
| 27 const char kRuleId2[] = "rule2"; | 30 const char kRuleId2[] = "rule2"; |
| 28 const char kRuleId3[] = "rule3"; | 31 const char kRuleId3[] = "rule3"; |
| 29 const char kRuleId4[] = "rule4"; | 32 const char kRuleId4[] = "rule4"; |
| 30 } // namespace | 33 } // namespace |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 176 | 179 |
| 177 linked_ptr<RulesRegistry::Rule> rule(new RulesRegistry::Rule); | 180 linked_ptr<RulesRegistry::Rule> rule(new RulesRegistry::Rule); |
| 178 rule->id.reset(new std::string(kRuleId4)); | 181 rule->id.reset(new std::string(kRuleId4)); |
| 179 rule->priority.reset(new int(200)); | 182 rule->priority.reset(new int(200)); |
| 180 rule->actions.push_back(linked_ptr<base::Value>(action_dict.DeepCopy())); | 183 rule->actions.push_back(linked_ptr<base::Value>(action_dict.DeepCopy())); |
| 181 rule->conditions.push_back( | 184 rule->conditions.push_back( |
| 182 linked_ptr<base::Value>(condition_dict.DeepCopy())); | 185 linked_ptr<base::Value>(condition_dict.DeepCopy())); |
| 183 return rule; | 186 return rule; |
| 184 } | 187 } |
| 185 | 188 |
| 186 // Create a condition with the attributes specified. An example value of a | 189 // Create a condition with the attributes specified. An example value of |
| 187 // string from |attributes| is: "\"resourceType\": [\"stylesheet\"], \n". | 190 // |attributes| is: "\"resourceType\": [\"stylesheet\"], \n". |
| 188 linked_ptr<base::Value> CreateCondition( | 191 linked_ptr<base::Value> CreateCondition(const std::string& attributes) { |
| 189 const std::vector<const char *>& attributes) { | |
| 190 // Starting boilerplate. | |
| 191 std::string json_description = | 192 std::string json_description = |
| 192 "{ \n" | 193 "{ \n" |
| 193 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n"; | 194 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n"; |
| 194 for (size_t i = 0; i < attributes.size(); ++i) { | 195 json_description += attributes; |
| 195 json_description += attributes[i]; | |
| 196 } | |
| 197 // Ending boilerplate. | |
| 198 json_description += "}"; | 196 json_description += "}"; |
| 199 | 197 |
| 200 return linked_ptr<base::Value>( | 198 return linked_ptr<base::Value>( |
| 201 base::test::ParseJson(json_description).release()); | 199 base::test::ParseJson(json_description).release()); |
| 202 } | 200 } |
| 203 | 201 |
| 204 // Create a rule with the ID |rule_id| and with a single condition and a | 202 // Create a rule with the ID |rule_id| and with conditions created from the |
| 205 // cancelling action. The condition contains a non-matching non-URL attribute, | 203 // |attributes| specified (one entry one condition). An example value of a |
| 206 // and optionally, according to |with_url_attribute| also a URL attribute | 204 // string from |attributes| is: "\"resourceType\": [\"stylesheet\"], \n". |
| 207 // matching any (!) URL. | 205 linked_ptr<RulesRegistry::Rule> CreateCancellingRule( |
| 208 linked_ptr<RulesRegistry::Rule> CreateNonMatchingRule( | 206 const char* rule_id, |
| 209 bool with_url_attribute, | 207 const std::vector<const std::string*>& attributes) { |
| 210 const char* rule_id) { | |
| 211 std::vector<const char*> attributes; | |
| 212 if (with_url_attribute) | |
| 213 attributes.push_back("\"url\": { \"pathContains\": \"\" }, \n"); | |
| 214 | |
| 215 // The following attribute never matches in this unit test. | |
| 216 attributes.push_back("\"resourceType\": [\"stylesheet\"], \n"); | |
| 217 | |
| 218 DictionaryValue action_dict; | 208 DictionaryValue action_dict; |
| 219 action_dict.SetString(keys::kInstanceTypeKey, keys::kCancelRequestType); | 209 action_dict.SetString(keys::kInstanceTypeKey, keys::kCancelRequestType); |
| 220 | 210 |
| 221 linked_ptr<RulesRegistry::Rule> rule(new RulesRegistry::Rule); | |
| 222 rule->id.reset(new std::string(rule_id)); | |
| 223 rule->priority.reset(new int(1)); | |
| 224 rule->actions.push_back(linked_ptr<base::Value>(action_dict.DeepCopy())); | |
| 225 rule->conditions.push_back(CreateCondition(attributes)); | |
| 226 return rule; | |
| 227 } | |
| 228 | |
| 229 // Create a rule with the ID |rule_id| and with two conditions and a | |
| 230 // cancelling action. One condition contains a non-matching non-URL attribute, | |
| 231 // and the other one a URL attribute matching any URL. | |
| 232 linked_ptr<RulesRegistry::Rule> CreateRuleWithTwoConditions( | |
| 233 const char* rule_id) { | |
| 234 std::vector<const char*> url_attributes; | |
| 235 url_attributes.push_back("\"url\": { \"pathContains\": \"\" }, \n"); | |
| 236 | |
| 237 // The following attribute never matches in this unit test. | |
| 238 std::vector<const char*> non_matching_attributes; | |
| 239 non_matching_attributes.push_back("\"resourceType\": [\"stylesheet\"], \n"); | |
| 240 | |
| 241 DictionaryValue action_dict; | |
| 242 action_dict.SetString(keys::kInstanceTypeKey, keys::kCancelRequestType); | |
| 243 | |
| 244 linked_ptr<RulesRegistry::Rule> rule(new RulesRegistry::Rule); | 211 linked_ptr<RulesRegistry::Rule> rule(new RulesRegistry::Rule); |
| 245 rule->id.reset(new std::string(rule_id)); | 212 rule->id.reset(new std::string(rule_id)); |
| 246 rule->priority.reset(new int(1)); | 213 rule->priority.reset(new int(1)); |
| 247 rule->actions.push_back(linked_ptr<base::Value>(action_dict.DeepCopy())); | 214 rule->actions.push_back(linked_ptr<base::Value>(action_dict.DeepCopy())); |
| 248 rule->conditions.push_back(CreateCondition(url_attributes)); | 215 for (std::vector<const std::string*>::const_iterator it = |
| 249 rule->conditions.push_back(CreateCondition(non_matching_attributes)); | 216 attributes.begin(); |
| 217 it != attributes.end(); ++it) |
| 218 rule->conditions.push_back(CreateCondition(**it)); |
| 250 return rule; | 219 return rule; |
| 251 } | 220 } |
| 252 | 221 |
| 253 protected: | 222 protected: |
| 254 MessageLoop message_loop; | 223 MessageLoop message_loop; |
| 255 content::TestBrowserThread ui; | 224 content::TestBrowserThread ui; |
| 256 content::TestBrowserThread io; | 225 content::TestBrowserThread io; |
| 257 }; | 226 }; |
| 258 | 227 |
| 259 TEST_F(WebRequestRulesRegistryTest, AddRulesImpl) { | 228 TEST_F(WebRequestRulesRegistryTest, AddRulesImpl) { |
| 260 scoped_refptr<TestWebRequestRulesRegistry> registry( | 229 scoped_refptr<TestWebRequestRulesRegistry> registry( |
| 261 new TestWebRequestRulesRegistry()); | 230 new TestWebRequestRulesRegistry()); |
| 262 std::string error; | 231 std::string error; |
| 263 | 232 |
| 264 std::vector<linked_ptr<RulesRegistry::Rule> > rules; | 233 std::vector<linked_ptr<RulesRegistry::Rule> > rules; |
| 265 rules.push_back(CreateRule1()); | 234 rules.push_back(CreateRule1()); |
| 266 rules.push_back(CreateRule2()); | 235 rules.push_back(CreateRule2()); |
| 267 | 236 |
| 268 error = registry->AddRules(kExtensionId, rules); | 237 error = registry->AddRules(kExtensionId, rules); |
| 269 EXPECT_EQ("", error); | 238 EXPECT_EQ("", error); |
| 270 EXPECT_EQ(1, registry->num_clear_cache_calls()); | 239 EXPECT_EQ(1, registry->num_clear_cache_calls()); |
| 271 | 240 |
| 272 std::set<const WebRequestRule*> matches; | 241 std::set<const WebRequestRule*> matches; |
| 273 | 242 |
| 274 GURL http_url("http://www.example.com"); | 243 GURL http_url("http://www.example.com"); |
| 275 net::TestURLRequestContext context; | 244 net::TestURLRequestContext context; |
| 276 net::TestURLRequest http_request(http_url, NULL, &context); | 245 net::TestURLRequest http_request(http_url, NULL, &context); |
| 277 matches = registry->GetMatches( | 246 WebRequestData request_data(&http_request, ON_BEFORE_REQUEST); |
| 278 DeclarativeWebRequestData(&http_request, ON_BEFORE_REQUEST)); | 247 matches = registry->GetMatches(request_data); |
| 279 EXPECT_EQ(2u, matches.size()); | 248 EXPECT_EQ(2u, matches.size()); |
| 280 | 249 |
| 281 std::set<WebRequestRule::GlobalRuleId> matches_ids; | 250 std::set<WebRequestRule::GlobalRuleId> matches_ids; |
| 282 for (std::set<const WebRequestRule*>::const_iterator it = matches.begin(); | 251 for (std::set<const WebRequestRule*>::const_iterator it = matches.begin(); |
| 283 it != matches.end(); ++it) | 252 it != matches.end(); ++it) |
| 284 matches_ids.insert((*it)->id()); | 253 matches_ids.insert((*it)->id()); |
| 285 EXPECT_TRUE(ContainsKey(matches_ids, std::make_pair(kExtensionId, kRuleId1))); | 254 EXPECT_TRUE(ContainsKey(matches_ids, std::make_pair(kExtensionId, kRuleId1))); |
| 286 EXPECT_TRUE(ContainsKey(matches_ids, std::make_pair(kExtensionId, kRuleId2))); | 255 EXPECT_TRUE(ContainsKey(matches_ids, std::make_pair(kExtensionId, kRuleId2))); |
| 287 | 256 |
| 288 GURL foobar_url("http://www.foobar.com"); | 257 GURL foobar_url("http://www.foobar.com"); |
| 289 net::TestURLRequest foobar_request(foobar_url, NULL, &context); | 258 net::TestURLRequest foobar_request(foobar_url, NULL, &context); |
| 290 matches = registry->GetMatches( | 259 request_data.request = &foobar_request; |
| 291 DeclarativeWebRequestData(&foobar_request, ON_BEFORE_REQUEST)); | 260 matches = registry->GetMatches(request_data); |
| 292 EXPECT_EQ(1u, matches.size()); | 261 EXPECT_EQ(1u, matches.size()); |
| 293 WebRequestRule::GlobalRuleId expected_pair = | 262 WebRequestRule::GlobalRuleId expected_pair = |
| 294 std::make_pair(kExtensionId, kRuleId2); | 263 std::make_pair(kExtensionId, kRuleId2); |
| 295 EXPECT_EQ(expected_pair, (*matches.begin())->id()); | 264 EXPECT_EQ(expected_pair, (*matches.begin())->id()); |
| 296 } | 265 } |
| 297 | 266 |
| 298 TEST_F(WebRequestRulesRegistryTest, RemoveRulesImpl) { | 267 TEST_F(WebRequestRulesRegistryTest, RemoveRulesImpl) { |
| 299 scoped_refptr<TestWebRequestRulesRegistry> registry( | 268 scoped_refptr<TestWebRequestRulesRegistry> registry( |
| 300 new TestWebRequestRulesRegistry()); | 269 new TestWebRequestRulesRegistry()); |
| 301 std::string error; | 270 std::string error; |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 406 EXPECT_EQ("", error); | 375 EXPECT_EQ("", error); |
| 407 | 376 |
| 408 std::vector<linked_ptr<RulesRegistry::Rule> > rules_to_add_2(1); | 377 std::vector<linked_ptr<RulesRegistry::Rule> > rules_to_add_2(1); |
| 409 rules_to_add_2[0] = CreateRedirectRule("http://www.bar.com"); | 378 rules_to_add_2[0] = CreateRedirectRule("http://www.bar.com"); |
| 410 error = registry->AddRules(kExtensionId2, rules_to_add_2); | 379 error = registry->AddRules(kExtensionId2, rules_to_add_2); |
| 411 EXPECT_EQ("", error); | 380 EXPECT_EQ("", error); |
| 412 | 381 |
| 413 GURL url("http://www.google.com"); | 382 GURL url("http://www.google.com"); |
| 414 net::TestURLRequestContext context; | 383 net::TestURLRequestContext context; |
| 415 net::TestURLRequest request(url, NULL, &context); | 384 net::TestURLRequest request(url, NULL, &context); |
| 385 WebRequestData request_data(&request, ON_BEFORE_REQUEST); |
| 416 std::list<LinkedPtrEventResponseDelta> deltas = | 386 std::list<LinkedPtrEventResponseDelta> deltas = |
| 417 registry->CreateDeltas( | 387 registry->CreateDeltas(NULL, request_data, false); |
| 418 NULL, | |
| 419 DeclarativeWebRequestData(&request, ON_BEFORE_REQUEST), | |
| 420 false); | |
| 421 | 388 |
| 422 // The second extension is installed later and will win for this reason | 389 // The second extension is installed later and will win for this reason |
| 423 // in conflict resolution. | 390 // in conflict resolution. |
| 424 ASSERT_EQ(2u, deltas.size()); | 391 ASSERT_EQ(2u, deltas.size()); |
| 425 deltas.sort(&helpers::InDecreasingExtensionInstallationTimeOrder); | 392 deltas.sort(&helpers::InDecreasingExtensionInstallationTimeOrder); |
| 426 | 393 |
| 427 std::list<LinkedPtrEventResponseDelta>::iterator i = deltas.begin(); | 394 std::list<LinkedPtrEventResponseDelta>::iterator i = deltas.begin(); |
| 428 LinkedPtrEventResponseDelta winner = *i++; | 395 LinkedPtrEventResponseDelta winner = *i++; |
| 429 LinkedPtrEventResponseDelta loser = *i; | 396 LinkedPtrEventResponseDelta loser = *i; |
| 430 | 397 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 456 EXPECT_EQ("", error); | 423 EXPECT_EQ("", error); |
| 457 | 424 |
| 458 std::vector<linked_ptr<RulesRegistry::Rule> > rules_to_add_3(1); | 425 std::vector<linked_ptr<RulesRegistry::Rule> > rules_to_add_3(1); |
| 459 rules_to_add_3[0] = CreateIgnoreRule(); | 426 rules_to_add_3[0] = CreateIgnoreRule(); |
| 460 error = registry->AddRules(kExtensionId, rules_to_add_3); | 427 error = registry->AddRules(kExtensionId, rules_to_add_3); |
| 461 EXPECT_EQ("", error); | 428 EXPECT_EQ("", error); |
| 462 | 429 |
| 463 GURL url("http://www.google.com/index.html"); | 430 GURL url("http://www.google.com/index.html"); |
| 464 net::TestURLRequestContext context; | 431 net::TestURLRequestContext context; |
| 465 net::TestURLRequest request(url, NULL, &context); | 432 net::TestURLRequest request(url, NULL, &context); |
| 433 WebRequestData request_data(&request, ON_BEFORE_REQUEST); |
| 466 std::list<LinkedPtrEventResponseDelta> deltas = | 434 std::list<LinkedPtrEventResponseDelta> deltas = |
| 467 registry->CreateDeltas( | 435 registry->CreateDeltas(NULL, request_data, false); |
| 468 NULL, | |
| 469 DeclarativeWebRequestData(&request, ON_BEFORE_REQUEST), | |
| 470 false); | |
| 471 | 436 |
| 472 // The redirect by the first extension is ignored due to the ignore rule. | 437 // The redirect by the first extension is ignored due to the ignore rule. |
| 473 ASSERT_EQ(1u, deltas.size()); | 438 ASSERT_EQ(1u, deltas.size()); |
| 474 LinkedPtrEventResponseDelta effective_rule = *(deltas.begin()); | 439 LinkedPtrEventResponseDelta effective_rule = *(deltas.begin()); |
| 475 | 440 |
| 476 EXPECT_EQ(kExtensionId2, effective_rule->extension_id); | 441 EXPECT_EQ(kExtensionId2, effective_rule->extension_id); |
| 477 EXPECT_EQ(base::Time() + base::TimeDelta::FromDays(2), | 442 EXPECT_EQ(base::Time() + base::TimeDelta::FromDays(2), |
| 478 effective_rule->extension_install_time); | 443 effective_rule->extension_install_time); |
| 479 EXPECT_EQ(GURL("http://www.bar.com"), effective_rule->new_url); | 444 EXPECT_EQ(GURL("http://www.bar.com"), effective_rule->new_url); |
| 480 } | 445 } |
| 481 | 446 |
| 482 // Test that rules failing IsFulfilled on their conditions are never returned by | 447 // Test that rules failing IsFulfilled on their conditions are never returned by |
| 483 // GetMatches. | 448 // GetMatches. |
| 484 TEST_F(WebRequestRulesRegistryTest, GetMatchesCheckFulfilled) { | 449 TEST_F(WebRequestRulesRegistryTest, GetMatchesCheckFulfilled) { |
| 485 scoped_refptr<TestWebRequestRulesRegistry> registry( | 450 scoped_refptr<TestWebRequestRulesRegistry> registry( |
| 486 new TestWebRequestRulesRegistry()); | 451 new TestWebRequestRulesRegistry()); |
| 452 const std::string kMatchingUrlAttribute( |
| 453 "\"url\": { \"pathContains\": \"\" }, \n"); |
| 454 const std::string kNonMatchingNonUrlAttribute( |
| 455 "\"resourceType\": [\"stylesheet\"], \n"); |
| 456 const std::string kBothAttributes(kMatchingUrlAttribute + |
| 457 kNonMatchingNonUrlAttribute); |
| 487 std::string error; | 458 std::string error; |
| 459 std::vector<const std::string*> attributes; |
| 460 std::vector<linked_ptr<RulesRegistry::Rule> > rules; |
| 488 | 461 |
| 489 std::vector<linked_ptr<RulesRegistry::Rule> > rules; | 462 // Rules 1 and 2 have one condition, neither of them should fire. |
| 490 // Both rules have one condition, neither of them should fire. | 463 attributes.push_back(&kNonMatchingNonUrlAttribute); |
| 491 // This rule's condition has only one, non-matching and non-URL attribute. | 464 rules.push_back(CreateCancellingRule(kRuleId1, attributes)); |
| 492 rules.push_back(CreateNonMatchingRule(false, kRuleId1)); | |
| 493 // This rule's condition has two attributes: a matching URL, and a | |
| 494 // non-matching non-URL attribute. | |
| 495 rules.push_back(CreateNonMatchingRule(true, kRuleId2)); | |
| 496 | 465 |
| 497 // The 3rd rule has two conditions, one with a matching URL attribute, and one | 466 attributes.clear(); |
| 467 attributes.push_back(&kBothAttributes); |
| 468 rules.push_back(CreateCancellingRule(kRuleId2, attributes)); |
| 469 |
| 470 // Rule 3 has two conditions, one with a matching URL attribute, and one |
| 498 // with a non-matching non-URL attribute. | 471 // with a non-matching non-URL attribute. |
| 499 rules.push_back(CreateRuleWithTwoConditions(kRuleId3)); | 472 attributes.clear(); |
| 473 attributes.push_back(&kMatchingUrlAttribute); |
| 474 attributes.push_back(&kNonMatchingNonUrlAttribute); |
| 475 rules.push_back(CreateCancellingRule(kRuleId3, attributes)); |
| 500 | 476 |
| 501 error = registry->AddRules(kExtensionId, rules); | 477 error = registry->AddRules(kExtensionId, rules); |
| 502 EXPECT_EQ("", error); | 478 EXPECT_EQ("", error); |
| 503 EXPECT_EQ(1, registry->num_clear_cache_calls()); | 479 EXPECT_EQ(1, registry->num_clear_cache_calls()); |
| 504 | 480 |
| 505 std::set<const WebRequestRule*> matches; | 481 std::set<const WebRequestRule*> matches; |
| 506 | 482 |
| 507 GURL http_url("http://www.example.com"); | 483 GURL http_url("http://www.example.com"); |
| 508 net::TestURLRequestContext context; | 484 net::TestURLRequestContext context; |
| 509 net::TestURLRequest http_request(http_url, NULL, &context); | 485 net::TestURLRequest http_request(http_url, NULL, &context); |
| 510 matches = registry->GetMatches( | 486 WebRequestData request_data(&http_request, ON_BEFORE_REQUEST); |
| 511 DeclarativeWebRequestData(&http_request, ON_BEFORE_REQUEST)); | 487 matches = registry->GetMatches(request_data); |
| 512 EXPECT_EQ(1u, matches.size()); | 488 EXPECT_EQ(1u, matches.size()); |
| 513 WebRequestRule::GlobalRuleId expected_pair = std::make_pair(kExtensionId, | 489 WebRequestRule::GlobalRuleId expected_pair = std::make_pair(kExtensionId, |
| 514 kRuleId3); | 490 kRuleId3); |
| 515 EXPECT_EQ(expected_pair, (*matches.begin())->id()); | 491 EXPECT_EQ(expected_pair, (*matches.begin())->id()); |
| 516 } | 492 } |
| 517 | 493 |
| 494 // Test that the url and firstPartyForCookiesUrl attributes are evaluated |
| 495 // against corresponding URLs. Tested on requests where these URLs actually |
| 496 // differ. |
| 497 TEST_F(WebRequestRulesRegistryTest, GetMatchesDifferentUrls) { |
| 498 scoped_refptr<TestWebRequestRulesRegistry> registry( |
| 499 new TestWebRequestRulesRegistry()); |
| 500 const std::string kUrlAttribute( |
| 501 "\"url\": { \"hostContains\": \"url\" }, \n"); |
| 502 const std::string kFirstPartyUrlAttribute( |
| 503 "\"firstPartyForCookiesUrl\": { \"hostContains\": \"fpfc\" }, \n"); |
| 504 std::string error; |
| 505 std::vector<const std::string*> attributes; |
| 506 std::vector<linked_ptr<RulesRegistry::Rule> > rules; |
| 507 |
| 508 // Rule 1 has one condition, with a url attribute |
| 509 attributes.push_back(&kUrlAttribute); |
| 510 rules.push_back(CreateCancellingRule(kRuleId1, attributes)); |
| 511 |
| 512 // Rule 2 has one condition, with a firstPartyForCookiesUrl attribute |
| 513 attributes.clear(); |
| 514 attributes.push_back(&kFirstPartyUrlAttribute); |
| 515 rules.push_back(CreateCancellingRule(kRuleId2, attributes)); |
| 516 |
| 517 error = registry->AddRules(kExtensionId, rules); |
| 518 EXPECT_EQ("", error); |
| 519 EXPECT_EQ(1, registry->num_clear_cache_calls()); |
| 520 |
| 521 std::set<const WebRequestRule*> matches; |
| 522 |
| 523 const GURL urls[] = { |
| 524 GURL("http://url.example.com"), // matching |
| 525 GURL("http://www.example.com") // non-matching |
| 526 }; |
| 527 const GURL firstPartyUrls[] = { |
| 528 GURL("http://www.example.com"), // non-matching |
| 529 GURL("http://fpfc.example.com") // matching |
| 530 }; |
| 531 // Which rules should match in subsequent test iterations. |
| 532 const char* matchingRuleIds[] = { kRuleId1, kRuleId2 }; |
| 533 COMPILE_ASSERT(arraysize(urls) == arraysize(firstPartyUrls), |
| 534 urls_and_firstPartyUrls_need_to_have_the_same_size); |
| 535 COMPILE_ASSERT(arraysize(urls) == arraysize(matchingRuleIds), |
| 536 urls_and_matchingRuleIds_need_to_have_the_same_size); |
| 537 net::TestURLRequestContext context; |
| 538 |
| 539 for (size_t i = 0; i < arraysize(matchingRuleIds); ++i) { |
| 540 // Construct the inputs. |
| 541 net::TestURLRequest http_request(urls[i], NULL, &context); |
| 542 WebRequestData request_data(&http_request, ON_BEFORE_REQUEST); |
| 543 http_request.set_first_party_for_cookies(firstPartyUrls[i]); |
| 544 // Now run both rules on the input. |
| 545 matches = registry->GetMatches(request_data); |
| 546 SCOPED_TRACE(testing::Message("i = ") << i << ", rule id = " |
| 547 << matchingRuleIds[i]); |
| 548 // Make sure that the right rule succeeded. |
| 549 EXPECT_EQ(1u, matches.size()); |
| 550 EXPECT_EQ(WebRequestRule::GlobalRuleId(std::make_pair(kExtensionId, |
| 551 matchingRuleIds[i])), |
| 552 (*matches.begin())->id()); |
| 553 } |
| 554 } |
| 555 |
| 518 TEST_F(WebRequestRulesRegistryTest, CheckConsistency) { | 556 TEST_F(WebRequestRulesRegistryTest, CheckConsistency) { |
| 519 // The contentType condition can only be evaluated during ON_HEADERS_RECEIVED | 557 // The contentType condition can only be evaluated during ON_HEADERS_RECEIVED |
| 520 // but the redirect action can only be executed during ON_BEFORE_REQUEST. | 558 // but the redirect action can only be executed during ON_BEFORE_REQUEST. |
| 521 // Therefore, this is an inconsistent rule that needs to be flagged. | 559 // Therefore, this is an inconsistent rule that needs to be flagged. |
| 522 const char kRule[] = | 560 const char kRule[] = |
| 523 "{ \n" | 561 "{ \n" |
| 524 " \"id\": \"rule1\", \n" | 562 " \"id\": \"rule1\", \n" |
| 525 " \"conditions\": [ \n" | 563 " \"conditions\": [ \n" |
| 526 " { \n" | 564 " { \n" |
| 527 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n" | 565 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n" |
| (...skipping 21 matching lines...) Expand all Loading... |
| 549 new TestWebRequestRulesRegistry()); | 587 new TestWebRequestRulesRegistry()); |
| 550 | 588 |
| 551 URLMatcher matcher; | 589 URLMatcher matcher; |
| 552 std::string error = registry->AddRulesImpl(kExtensionId, rules); | 590 std::string error = registry->AddRulesImpl(kExtensionId, rules); |
| 553 EXPECT_THAT(error, HasSubstr("no time in the request life-cycle")); | 591 EXPECT_THAT(error, HasSubstr("no time in the request life-cycle")); |
| 554 EXPECT_TRUE(registry->IsEmpty()); | 592 EXPECT_TRUE(registry->IsEmpty()); |
| 555 } | 593 } |
| 556 | 594 |
| 557 | 595 |
| 558 } // namespace extensions | 596 } // namespace extensions |
| OLD | NEW |