OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_rules_ registry.h" | 5 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_rules_ registry.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/memory/linked_ptr.h" | 9 #include "base/memory/linked_ptr.h" |
10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
11 #include "base/stl_util.h" | |
11 #include "base/values.h" | 12 #include "base/values.h" |
12 #include "chrome/common/extensions/matcher/url_matcher_constants.h" | 13 #include "chrome/common/extensions/matcher/url_matcher_constants.h" |
13 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_consta nts.h" | 14 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_consta nts.h" |
14 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_rule.h " | 15 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_rule.h " |
15 #include "chrome/browser/extensions/api/web_request/web_request_api_helpers.h" | 16 #include "chrome/browser/extensions/api/web_request/web_request_api_helpers.h" |
16 #include "content/public/test/test_browser_thread.h" | 17 #include "content/public/test/test_browser_thread.h" |
17 #include "net/url_request/url_request_test_util.h" | 18 #include "net/url_request/url_request_test_util.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
19 | 20 |
20 namespace { | 21 namespace { |
(...skipping 14 matching lines...) Expand all Loading... | |
35 class TestWebRequestRulesRegistry : public WebRequestRulesRegistry { | 36 class TestWebRequestRulesRegistry : public WebRequestRulesRegistry { |
36 public: | 37 public: |
37 TestWebRequestRulesRegistry() | 38 TestWebRequestRulesRegistry() |
38 : WebRequestRulesRegistry(NULL, NULL), | 39 : WebRequestRulesRegistry(NULL, NULL), |
39 num_clear_cache_calls_(0) {} | 40 num_clear_cache_calls_(0) {} |
40 | 41 |
41 // Returns how often the in-memory caches of the renderers were instructed | 42 // Returns how often the in-memory caches of the renderers were instructed |
42 // to be cleared. | 43 // to be cleared. |
43 int num_clear_cache_calls() const { return num_clear_cache_calls_; } | 44 int num_clear_cache_calls() const { return num_clear_cache_calls_; } |
44 | 45 |
46 // How many rules are there which have some conditions not triggered by URL | |
47 // matches. | |
48 size_t RulesWithoutTriggers() const { | |
49 return rules_with_untriggered_conditions_for_test().size(); | |
50 } | |
51 | |
45 protected: | 52 protected: |
46 virtual ~TestWebRequestRulesRegistry() {} | 53 virtual ~TestWebRequestRulesRegistry() {} |
47 | 54 |
48 virtual base::Time GetExtensionInstallationTime( | 55 virtual base::Time GetExtensionInstallationTime( |
49 const std::string& extension_id) const { | 56 const std::string& extension_id) const { |
50 if (extension_id == kExtensionId) | 57 if (extension_id == kExtensionId) |
51 return base::Time() + base::TimeDelta::FromDays(1); | 58 return base::Time() + base::TimeDelta::FromDays(1); |
52 else if (extension_id == kExtensionId2) | 59 else if (extension_id == kExtensionId2) |
53 return base::Time() + base::TimeDelta::FromDays(2); | 60 return base::Time() + base::TimeDelta::FromDays(2); |
54 else | 61 else |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
198 | 205 |
199 linked_ptr<RulesRegistry::Rule> rule = | 206 linked_ptr<RulesRegistry::Rule> rule = |
200 make_linked_ptr(new RulesRegistry::Rule); | 207 make_linked_ptr(new RulesRegistry::Rule); |
201 rule->id.reset(new std::string(kRuleId4)); | 208 rule->id.reset(new std::string(kRuleId4)); |
202 rule->priority.reset(new int(200)); | 209 rule->priority.reset(new int(200)); |
203 rule->actions.push_back(action); | 210 rule->actions.push_back(action); |
204 rule->conditions.push_back(condition); | 211 rule->conditions.push_back(condition); |
205 return rule; | 212 return rule; |
206 } | 213 } |
207 | 214 |
215 // Create a rule with the ID |rule_id| and with a single condition and a | |
216 // cancelling action. The condition contains a non-matching non-URL attribute, | |
217 // and optionally, according to |with_url_attribute| also a URL attribute | |
218 // matching any (!) URL. | |
219 linked_ptr<RulesRegistry::Rule> CreateNonMatchingRule( | |
220 bool with_url_attribute, | |
221 const char* rule_id) { | |
222 linked_ptr<json_schema_compiler::any::Any> condition = make_linked_ptr( | |
223 new json_schema_compiler::any::Any); | |
224 DictionaryValue condition_dict; | |
Jeffrey Yasskin
2013/01/10 22:00:57
FWIW, I would probably try to initialize this usin
vabr (Chromium)
2013/01/15 10:24:57
Done.
The way I did it I separated the condition
| |
225 condition_dict.SetString(keys::kInstanceTypeKey, keys::kRequestMatcherType); | |
226 ListValue* resource_condition_list = new ListValue(); | |
227 // In this unit-test we don't generate requests with the stylesheet resource | |
228 // type, so this attribute is not going to match. | |
229 resource_condition_list->AppendString("stylesheet"); | |
230 condition_dict.Set(keys::kResourceTypeKey, resource_condition_list); | |
231 if (with_url_attribute) { | |
232 DictionaryValue* http_condition_dict = new DictionaryValue(); | |
233 http_condition_dict->SetString(keys2::kPathContainsKey, ""); | |
234 condition_dict.Set(keys::kUrlKey, http_condition_dict); | |
235 } | |
236 condition->Init(condition_dict); | |
237 | |
238 DictionaryValue action_dict; | |
239 action_dict.SetString(keys::kInstanceTypeKey, keys::kCancelRequestType); | |
240 linked_ptr<json_schema_compiler::any::Any> action = make_linked_ptr( | |
241 new json_schema_compiler::any::Any); | |
242 action->Init(action_dict); | |
243 | |
244 linked_ptr<RulesRegistry::Rule> rule = | |
245 make_linked_ptr(new RulesRegistry::Rule); | |
Jeffrey Yasskin
2013/01/10 22:00:57
Just use
linked_ptr<RulesRegistry::Rule> rule(ne
vabr (Chromium)
2013/01/15 10:24:57
Oh, right, thanks for catching this.
I corrected t
| |
246 rule->id.reset(new std::string(rule_id)); | |
247 rule->priority.reset(new int(1)); | |
248 rule->actions.push_back(action); | |
249 rule->conditions.push_back(condition); | |
250 return rule; | |
251 } | |
252 | |
208 protected: | 253 protected: |
209 MessageLoop message_loop; | 254 MessageLoop message_loop; |
210 content::TestBrowserThread ui; | 255 content::TestBrowserThread ui; |
211 content::TestBrowserThread io; | 256 content::TestBrowserThread io; |
212 }; | 257 }; |
213 | 258 |
214 TEST_F(WebRequestRulesRegistryTest, AddRulesImpl) { | 259 TEST_F(WebRequestRulesRegistryTest, AddRulesImpl) { |
215 scoped_refptr<TestWebRequestRulesRegistry> registry( | 260 scoped_refptr<TestWebRequestRulesRegistry> registry( |
216 new TestWebRequestRulesRegistry()); | 261 new TestWebRequestRulesRegistry()); |
217 std::string error; | 262 std::string error; |
218 | 263 |
219 std::vector<linked_ptr<RulesRegistry::Rule> > rules; | 264 std::vector<linked_ptr<RulesRegistry::Rule> > rules; |
220 rules.push_back(CreateRule1()); | 265 rules.push_back(CreateRule1()); |
221 rules.push_back(CreateRule2()); | 266 rules.push_back(CreateRule2()); |
222 | 267 |
223 error = registry->AddRules(kExtensionId, rules); | 268 error = registry->AddRules(kExtensionId, rules); |
224 EXPECT_EQ("", error); | 269 EXPECT_EQ("", error); |
225 EXPECT_EQ(1, registry->num_clear_cache_calls()); | 270 EXPECT_EQ(1, registry->num_clear_cache_calls()); |
226 | 271 |
227 std::set<WebRequestRule::GlobalRuleId> matches; | 272 std::set<const WebRequestRule*> matches; |
228 | 273 |
229 GURL http_url("http://www.example.com"); | 274 GURL http_url("http://www.example.com"); |
230 net::TestURLRequestContext context; | 275 net::TestURLRequestContext context; |
231 net::TestURLRequest http_request(http_url, NULL, &context); | 276 net::TestURLRequest http_request(http_url, NULL, &context); |
232 matches = registry->GetMatches( | 277 matches = registry->GetMatches( |
233 WebRequestRule::RequestData(&http_request, ON_BEFORE_REQUEST)); | 278 WebRequestRule::RequestData(&http_request, ON_BEFORE_REQUEST)); |
234 EXPECT_EQ(2u, matches.size()); | 279 EXPECT_EQ(2u, matches.size()); |
235 EXPECT_TRUE(matches.find(std::make_pair(kExtensionId, kRuleId1)) != | 280 |
236 matches.end()); | 281 std::set<WebRequestRule::GlobalRuleId> matches_ids; |
237 EXPECT_TRUE(matches.find(std::make_pair(kExtensionId, kRuleId2)) != | 282 for (std::set<const WebRequestRule*>::const_iterator it = matches.begin(); |
238 matches.end()); | 283 it != matches.end(); ++it) |
284 matches_ids.insert((*it)->id()); | |
285 EXPECT_TRUE(ContainsKey(matches_ids, std::make_pair(kExtensionId, kRuleId1))); | |
286 EXPECT_TRUE(ContainsKey(matches_ids, std::make_pair(kExtensionId, kRuleId2))); | |
239 | 287 |
240 GURL foobar_url("http://www.foobar.com"); | 288 GURL foobar_url("http://www.foobar.com"); |
241 net::TestURLRequest foobar_request(foobar_url, NULL, &context); | 289 net::TestURLRequest foobar_request(foobar_url, NULL, &context); |
242 matches = registry->GetMatches( | 290 matches = registry->GetMatches( |
243 WebRequestRule::RequestData(&foobar_request, ON_BEFORE_REQUEST)); | 291 WebRequestRule::RequestData(&foobar_request, ON_BEFORE_REQUEST)); |
244 EXPECT_EQ(1u, matches.size()); | 292 EXPECT_EQ(1u, matches.size()); |
245 EXPECT_TRUE(matches.find(std::make_pair(kExtensionId, kRuleId2)) != | 293 WebRequestRule::GlobalRuleId expected_pair = |
246 matches.end()); | 294 std::make_pair(kExtensionId, kRuleId2); |
295 EXPECT_EQ(expected_pair, (*matches.begin())->id()); | |
247 } | 296 } |
248 | 297 |
249 TEST_F(WebRequestRulesRegistryTest, RemoveRulesImpl) { | 298 TEST_F(WebRequestRulesRegistryTest, RemoveRulesImpl) { |
250 scoped_refptr<TestWebRequestRulesRegistry> registry( | 299 scoped_refptr<TestWebRequestRulesRegistry> registry( |
251 new TestWebRequestRulesRegistry()); | 300 new TestWebRequestRulesRegistry()); |
252 std::string error; | 301 std::string error; |
253 | 302 |
254 // Setup RulesRegistry to contain two rules. | 303 // Setup RulesRegistry to contain two rules. |
255 std::vector<linked_ptr<RulesRegistry::Rule> > rules_to_add; | 304 std::vector<linked_ptr<RulesRegistry::Rule> > rules_to_add; |
256 rules_to_add.push_back(CreateRule1()); | 305 rules_to_add.push_back(CreateRule1()); |
257 rules_to_add.push_back(CreateRule2()); | 306 rules_to_add.push_back(CreateRule2()); |
258 error = registry->AddRules(kExtensionId, rules_to_add); | 307 error = registry->AddRules(kExtensionId, rules_to_add); |
259 EXPECT_EQ("", error); | 308 EXPECT_EQ("", error); |
260 EXPECT_EQ(1, registry->num_clear_cache_calls()); | 309 EXPECT_EQ(1, registry->num_clear_cache_calls()); |
261 | 310 |
262 // Verify initial state. | 311 // Verify initial state. |
263 std::vector<linked_ptr<RulesRegistry::Rule> > registered_rules; | 312 std::vector<linked_ptr<RulesRegistry::Rule> > registered_rules; |
264 registry->GetAllRules(kExtensionId, ®istered_rules); | 313 registry->GetAllRules(kExtensionId, ®istered_rules); |
265 EXPECT_EQ(2u, registered_rules.size()); | 314 EXPECT_EQ(2u, registered_rules.size()); |
315 EXPECT_EQ(1u, registry->RulesWithoutTriggers()); | |
266 | 316 |
267 // Remove first rule. | 317 // Remove first rule. |
268 std::vector<std::string> rules_to_remove; | 318 std::vector<std::string> rules_to_remove; |
269 rules_to_remove.push_back(kRuleId1); | 319 rules_to_remove.push_back(kRuleId1); |
270 error = registry->RemoveRules(kExtensionId, rules_to_remove); | 320 error = registry->RemoveRules(kExtensionId, rules_to_remove); |
271 EXPECT_EQ("", error); | 321 EXPECT_EQ("", error); |
272 EXPECT_EQ(2, registry->num_clear_cache_calls()); | 322 EXPECT_EQ(2, registry->num_clear_cache_calls()); |
273 | 323 |
274 // Verify that only one rule is left. | 324 // Verify that only one rule is left. |
275 registered_rules.clear(); | 325 registered_rules.clear(); |
276 registry->GetAllRules(kExtensionId, ®istered_rules); | 326 registry->GetAllRules(kExtensionId, ®istered_rules); |
277 EXPECT_EQ(1u, registered_rules.size()); | 327 EXPECT_EQ(1u, registered_rules.size()); |
328 EXPECT_EQ(1u, registry->RulesWithoutTriggers()); | |
278 | 329 |
279 // Now rules_to_remove contains both rules, i.e. one that does not exist in | 330 // Now rules_to_remove contains both rules, i.e. one that does not exist in |
280 // the rules registry anymore. Effectively we only remove the second rule. | 331 // the rules registry anymore. Effectively we only remove the second rule. |
281 rules_to_remove.push_back(kRuleId2); | 332 rules_to_remove.push_back(kRuleId2); |
282 error = registry->RemoveRules(kExtensionId, rules_to_remove); | 333 error = registry->RemoveRules(kExtensionId, rules_to_remove); |
283 EXPECT_EQ("", error); | 334 EXPECT_EQ("", error); |
284 EXPECT_EQ(3, registry->num_clear_cache_calls()); | 335 EXPECT_EQ(3, registry->num_clear_cache_calls()); |
285 | 336 |
286 // Verify that everything is gone. | 337 // Verify that everything is gone. |
287 registered_rules.clear(); | 338 registered_rules.clear(); |
288 registry->GetAllRules(kExtensionId, ®istered_rules); | 339 registry->GetAllRules(kExtensionId, ®istered_rules); |
289 EXPECT_EQ(0u, registered_rules.size()); | 340 EXPECT_EQ(0u, registered_rules.size()); |
341 EXPECT_EQ(0u, registry->RulesWithoutTriggers()); | |
290 | 342 |
291 EXPECT_TRUE(registry->IsEmpty()); | 343 EXPECT_TRUE(registry->IsEmpty()); |
292 } | 344 } |
293 | 345 |
294 TEST_F(WebRequestRulesRegistryTest, RemoveAllRulesImpl) { | 346 TEST_F(WebRequestRulesRegistryTest, RemoveAllRulesImpl) { |
295 scoped_refptr<TestWebRequestRulesRegistry> registry( | 347 scoped_refptr<TestWebRequestRulesRegistry> registry( |
296 new TestWebRequestRulesRegistry()); | 348 new TestWebRequestRulesRegistry()); |
297 std::string error; | 349 std::string error; |
298 | 350 |
299 // Setup RulesRegistry to contain two rules, one for each extension. | 351 // Setup RulesRegistry to contain two rules, one for each extension. |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
419 | 471 |
420 // The redirect by the first extension is ignored due to the ignore rule. | 472 // The redirect by the first extension is ignored due to the ignore rule. |
421 ASSERT_EQ(1u, deltas.size()); | 473 ASSERT_EQ(1u, deltas.size()); |
422 LinkedPtrEventResponseDelta effective_rule = *(deltas.begin()); | 474 LinkedPtrEventResponseDelta effective_rule = *(deltas.begin()); |
423 | 475 |
424 EXPECT_EQ(kExtensionId2, effective_rule->extension_id); | 476 EXPECT_EQ(kExtensionId2, effective_rule->extension_id); |
425 EXPECT_EQ(base::Time() + base::TimeDelta::FromDays(2), | 477 EXPECT_EQ(base::Time() + base::TimeDelta::FromDays(2), |
426 effective_rule->extension_install_time); | 478 effective_rule->extension_install_time); |
427 EXPECT_EQ(GURL("http://www.bar.com"), effective_rule->new_url); | 479 EXPECT_EQ(GURL("http://www.bar.com"), effective_rule->new_url); |
428 } | 480 } |
481 | |
482 // Test that rules failing IsFulfilled on their conditions are never returned by | |
483 // GetMatches. | |
484 TEST_F(WebRequestRulesRegistryTest, GetMatchesCheckFulfilled) { | |
vabr (Chromium)
2013/01/10 15:12:22
I almost forgot to check IsFulfilled in the second
| |
485 scoped_refptr<TestWebRequestRulesRegistry> registry( | |
486 new TestWebRequestRulesRegistry()); | |
487 std::string error; | |
488 | |
489 std::vector<linked_ptr<RulesRegistry::Rule> > rules; | |
490 // Both rules have one condition, neither of them should fire. | |
491 // This rule's condition has only one, non-matching and non-URL attribute. | |
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 | |
497 error = registry->AddRules(kExtensionId, rules); | |
498 EXPECT_EQ("", error); | |
499 EXPECT_EQ(1, registry->num_clear_cache_calls()); | |
500 | |
501 std::set<const WebRequestRule*> matches; | |
502 | |
503 GURL http_url("http://www.example.com"); | |
504 net::TestURLRequestContext context; | |
505 net::TestURLRequest http_request(http_url, NULL, &context); | |
506 matches = registry->GetMatches( | |
507 WebRequestRule::RequestData(&http_request, ON_BEFORE_REQUEST)); | |
508 EXPECT_EQ(0u, matches.size()); | |
509 } | |
510 | |
429 } // namespace extensions | 511 } // namespace extensions |
OLD | NEW |