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" | |
12 #include "base/test/values_test_util.h" | |
11 #include "base/values.h" | 13 #include "base/values.h" |
12 #include "chrome/common/extensions/matcher/url_matcher_constants.h" | 14 #include "chrome/common/extensions/matcher/url_matcher_constants.h" |
13 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_consta nts.h" | 15 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_consta nts.h" |
14 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_rule.h " | 16 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_rule.h " |
15 #include "chrome/browser/extensions/api/web_request/web_request_api_helpers.h" | 17 #include "chrome/browser/extensions/api/web_request/web_request_api_helpers.h" |
16 #include "content/public/test/test_browser_thread.h" | 18 #include "content/public/test/test_browser_thread.h" |
17 #include "net/url_request/url_request_test_util.h" | 19 #include "net/url_request/url_request_test_util.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
19 | 21 |
20 namespace { | 22 namespace { |
(...skipping 14 matching lines...) Expand all Loading... | |
35 class TestWebRequestRulesRegistry : public WebRequestRulesRegistry { | 37 class TestWebRequestRulesRegistry : public WebRequestRulesRegistry { |
36 public: | 38 public: |
37 TestWebRequestRulesRegistry() | 39 TestWebRequestRulesRegistry() |
38 : WebRequestRulesRegistry(NULL, NULL), | 40 : WebRequestRulesRegistry(NULL, NULL), |
39 num_clear_cache_calls_(0) {} | 41 num_clear_cache_calls_(0) {} |
40 | 42 |
41 // Returns how often the in-memory caches of the renderers were instructed | 43 // Returns how often the in-memory caches of the renderers were instructed |
42 // to be cleared. | 44 // to be cleared. |
43 int num_clear_cache_calls() const { return num_clear_cache_calls_; } | 45 int num_clear_cache_calls() const { return num_clear_cache_calls_; } |
44 | 46 |
47 // How many rules are there which have some conditions not triggered by URL | |
48 // matches. | |
49 size_t RulesWithoutTriggers() const { | |
50 return rules_with_untriggered_conditions_for_test().size(); | |
51 } | |
52 | |
45 protected: | 53 protected: |
46 virtual ~TestWebRequestRulesRegistry() {} | 54 virtual ~TestWebRequestRulesRegistry() {} |
47 | 55 |
48 virtual base::Time GetExtensionInstallationTime( | 56 virtual base::Time GetExtensionInstallationTime( |
49 const std::string& extension_id) const { | 57 const std::string& extension_id) const { |
50 if (extension_id == kExtensionId) | 58 if (extension_id == kExtensionId) |
51 return base::Time() + base::TimeDelta::FromDays(1); | 59 return base::Time() + base::TimeDelta::FromDays(1); |
52 else if (extension_id == kExtensionId2) | 60 else if (extension_id == kExtensionId2) |
53 return base::Time() + base::TimeDelta::FromDays(2); | 61 return base::Time() + base::TimeDelta::FromDays(2); |
54 else | 62 else |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
94 scheme_http->Append(Value::CreateStringValue("https")); | 102 scheme_http->Append(Value::CreateStringValue("https")); |
95 DictionaryValue* https_condition_dict = new DictionaryValue(); | 103 DictionaryValue* https_condition_dict = new DictionaryValue(); |
96 https_condition_dict->Set(keys2::kSchemesKey, scheme_https); | 104 https_condition_dict->Set(keys2::kSchemesKey, scheme_https); |
97 https_condition_dict->SetString(keys2::kHostSuffixKey, "example.com"); | 105 https_condition_dict->SetString(keys2::kHostSuffixKey, "example.com"); |
98 https_condition_dict->SetString(keys2::kHostPrefixKey, "www"); | 106 https_condition_dict->SetString(keys2::kHostPrefixKey, "www"); |
99 DictionaryValue https_condition_url_filter; | 107 DictionaryValue https_condition_url_filter; |
100 https_condition_url_filter.Set(keys::kUrlKey, https_condition_dict); | 108 https_condition_url_filter.Set(keys::kUrlKey, https_condition_dict); |
101 https_condition_url_filter.SetString(keys::kInstanceTypeKey, | 109 https_condition_url_filter.SetString(keys::kInstanceTypeKey, |
102 keys::kRequestMatcherType); | 110 keys::kRequestMatcherType); |
103 | 111 |
104 linked_ptr<json_schema_compiler::any::Any> condition1 = make_linked_ptr( | 112 linked_ptr<json_schema_compiler::any::Any> condition1( |
105 new json_schema_compiler::any::Any); | 113 new json_schema_compiler::any::Any); |
106 condition1->Init(http_condition_url_filter); | 114 condition1->Init(http_condition_url_filter); |
107 | 115 |
108 linked_ptr<json_schema_compiler::any::Any> condition2 = make_linked_ptr( | 116 linked_ptr<json_schema_compiler::any::Any> condition2( |
109 new json_schema_compiler::any::Any); | 117 new json_schema_compiler::any::Any); |
110 condition2->Init(https_condition_url_filter); | 118 condition2->Init(https_condition_url_filter); |
111 | 119 |
112 DictionaryValue action_dict; | 120 DictionaryValue action_dict; |
113 action_dict.SetString(keys::kInstanceTypeKey, keys::kCancelRequestType); | 121 action_dict.SetString(keys::kInstanceTypeKey, keys::kCancelRequestType); |
114 | 122 |
115 linked_ptr<json_schema_compiler::any::Any> action1 = make_linked_ptr( | 123 linked_ptr<json_schema_compiler::any::Any> action1( |
116 new json_schema_compiler::any::Any); | 124 new json_schema_compiler::any::Any); |
117 action1->Init(action_dict); | 125 action1->Init(action_dict); |
118 | 126 |
119 linked_ptr<RulesRegistry::Rule> rule = | 127 linked_ptr<RulesRegistry::Rule> rule(new RulesRegistry::Rule); |
120 make_linked_ptr(new RulesRegistry::Rule); | |
121 rule->id.reset(new std::string(kRuleId1)); | 128 rule->id.reset(new std::string(kRuleId1)); |
122 rule->priority.reset(new int(100)); | 129 rule->priority.reset(new int(100)); |
123 rule->actions.push_back(action1); | 130 rule->actions.push_back(action1); |
124 rule->conditions.push_back(condition1); | 131 rule->conditions.push_back(condition1); |
125 rule->conditions.push_back(condition2); | 132 rule->conditions.push_back(condition2); |
126 return rule; | 133 return rule; |
127 } | 134 } |
128 | 135 |
129 // Returns a rule that matches anything and cancels it. | 136 // Returns a rule that matches anything and cancels it. |
130 linked_ptr<RulesRegistry::Rule> CreateRule2() { | 137 linked_ptr<RulesRegistry::Rule> CreateRule2() { |
131 DictionaryValue condition_dict; | 138 DictionaryValue condition_dict; |
132 condition_dict.SetString(keys::kInstanceTypeKey, keys::kRequestMatcherType); | 139 condition_dict.SetString(keys::kInstanceTypeKey, keys::kRequestMatcherType); |
133 | 140 |
134 linked_ptr<json_schema_compiler::any::Any> condition = make_linked_ptr( | 141 linked_ptr<json_schema_compiler::any::Any> condition( |
135 new json_schema_compiler::any::Any); | 142 new json_schema_compiler::any::Any); |
136 condition->Init(condition_dict); | 143 condition->Init(condition_dict); |
137 | 144 |
138 DictionaryValue action_dict; | 145 DictionaryValue action_dict; |
139 action_dict.SetString(keys::kInstanceTypeKey, keys::kCancelRequestType); | 146 action_dict.SetString(keys::kInstanceTypeKey, keys::kCancelRequestType); |
140 | 147 |
141 linked_ptr<json_schema_compiler::any::Any> action = make_linked_ptr( | 148 linked_ptr<json_schema_compiler::any::Any> action( |
142 new json_schema_compiler::any::Any); | 149 new json_schema_compiler::any::Any); |
143 action->Init(action_dict); | 150 action->Init(action_dict); |
144 | 151 |
145 linked_ptr<RulesRegistry::Rule> rule = | 152 linked_ptr<RulesRegistry::Rule> rule(new RulesRegistry::Rule); |
146 make_linked_ptr(new RulesRegistry::Rule); | |
147 rule->id.reset(new std::string(kRuleId2)); | 153 rule->id.reset(new std::string(kRuleId2)); |
148 rule->priority.reset(new int(100)); | 154 rule->priority.reset(new int(100)); |
149 rule->actions.push_back(action); | 155 rule->actions.push_back(action); |
150 rule->conditions.push_back(condition); | 156 rule->conditions.push_back(condition); |
151 return rule; | 157 return rule; |
152 } | 158 } |
153 | 159 |
154 linked_ptr<RulesRegistry::Rule> CreateRedirectRule( | 160 linked_ptr<RulesRegistry::Rule> CreateRedirectRule( |
155 const std::string& destination) { | 161 const std::string& destination) { |
156 DictionaryValue condition_dict; | 162 DictionaryValue condition_dict; |
157 condition_dict.SetString(keys::kInstanceTypeKey, keys::kRequestMatcherType); | 163 condition_dict.SetString(keys::kInstanceTypeKey, keys::kRequestMatcherType); |
158 | 164 |
159 linked_ptr<json_schema_compiler::any::Any> condition = make_linked_ptr( | 165 linked_ptr<json_schema_compiler::any::Any> condition( |
160 new json_schema_compiler::any::Any); | 166 new json_schema_compiler::any::Any); |
161 condition->Init(condition_dict); | 167 condition->Init(condition_dict); |
162 | 168 |
163 DictionaryValue action_dict; | 169 DictionaryValue action_dict; |
164 action_dict.SetString(keys::kInstanceTypeKey, keys::kRedirectRequestType); | 170 action_dict.SetString(keys::kInstanceTypeKey, keys::kRedirectRequestType); |
165 action_dict.SetString(keys::kRedirectUrlKey, destination); | 171 action_dict.SetString(keys::kRedirectUrlKey, destination); |
166 | 172 |
167 linked_ptr<json_schema_compiler::any::Any> action = make_linked_ptr( | 173 linked_ptr<json_schema_compiler::any::Any> action( |
168 new json_schema_compiler::any::Any); | 174 new json_schema_compiler::any::Any); |
169 action->Init(action_dict); | 175 action->Init(action_dict); |
170 | 176 |
171 linked_ptr<RulesRegistry::Rule> rule = | 177 linked_ptr<RulesRegistry::Rule> rule(new RulesRegistry::Rule); |
172 make_linked_ptr(new RulesRegistry::Rule); | |
173 rule->id.reset(new std::string(kRuleId3)); | 178 rule->id.reset(new std::string(kRuleId3)); |
174 rule->priority.reset(new int(100)); | 179 rule->priority.reset(new int(100)); |
175 rule->actions.push_back(action); | 180 rule->actions.push_back(action); |
176 rule->conditions.push_back(condition); | 181 rule->conditions.push_back(condition); |
177 return rule; | 182 return rule; |
178 } | 183 } |
179 | 184 |
180 // Create a rule to ignore all other rules for a destination that | 185 // Create a rule to ignore all other rules for a destination that |
181 // contains index.html. | 186 // contains index.html. |
182 linked_ptr<RulesRegistry::Rule> CreateIgnoreRule() { | 187 linked_ptr<RulesRegistry::Rule> CreateIgnoreRule() { |
183 linked_ptr<json_schema_compiler::any::Any> condition = make_linked_ptr( | 188 linked_ptr<json_schema_compiler::any::Any> condition( |
184 new json_schema_compiler::any::Any); | 189 new json_schema_compiler::any::Any); |
185 DictionaryValue condition_dict; | 190 DictionaryValue condition_dict; |
186 DictionaryValue* http_condition_dict = new DictionaryValue(); | 191 DictionaryValue* http_condition_dict = new DictionaryValue(); |
187 http_condition_dict->SetString(keys2::kPathContainsKey, "index.html"); | 192 http_condition_dict->SetString(keys2::kPathContainsKey, "index.html"); |
188 condition_dict.SetString(keys::kInstanceTypeKey, keys::kRequestMatcherType); | 193 condition_dict.SetString(keys::kInstanceTypeKey, keys::kRequestMatcherType); |
189 condition_dict.Set(keys::kUrlKey, http_condition_dict); | 194 condition_dict.Set(keys::kUrlKey, http_condition_dict); |
190 condition->Init(condition_dict); | 195 condition->Init(condition_dict); |
191 | 196 |
192 DictionaryValue action_dict; | 197 DictionaryValue action_dict; |
193 action_dict.SetString(keys::kInstanceTypeKey, keys::kIgnoreRulesType); | 198 action_dict.SetString(keys::kInstanceTypeKey, keys::kIgnoreRulesType); |
194 action_dict.SetInteger(keys::kLowerPriorityThanKey, 150); | 199 action_dict.SetInteger(keys::kLowerPriorityThanKey, 150); |
195 linked_ptr<json_schema_compiler::any::Any> action = make_linked_ptr( | 200 linked_ptr<json_schema_compiler::any::Any> action( |
196 new json_schema_compiler::any::Any); | 201 new json_schema_compiler::any::Any); |
197 action->Init(action_dict); | 202 action->Init(action_dict); |
198 | 203 |
199 linked_ptr<RulesRegistry::Rule> rule = | 204 linked_ptr<RulesRegistry::Rule> rule(new RulesRegistry::Rule); |
200 make_linked_ptr(new RulesRegistry::Rule); | |
201 rule->id.reset(new std::string(kRuleId4)); | 205 rule->id.reset(new std::string(kRuleId4)); |
202 rule->priority.reset(new int(200)); | 206 rule->priority.reset(new int(200)); |
203 rule->actions.push_back(action); | 207 rule->actions.push_back(action); |
204 rule->conditions.push_back(condition); | 208 rule->conditions.push_back(condition); |
205 return rule; | 209 return rule; |
206 } | 210 } |
207 | 211 |
212 // Create a condition with the attributes specified. An example value of a | |
213 // string from |attributes| is: "\"resourceType\": [\"stylesheet\"], \n". | |
214 linked_ptr<json_schema_compiler::any::Any> CreateCondition( | |
215 const std::vector<const char *>& attributes) { | |
216 linked_ptr<json_schema_compiler::any::Any> condition( | |
217 new json_schema_compiler::any::Any); | |
218 | |
219 // Starting boilerplate. | |
220 std::string json_description = | |
221 "{ \n" | |
222 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n"; | |
223 for (size_t i = 0; i < attributes.size(); ++i) { | |
224 json_description += attributes[i]; | |
225 } | |
226 // Ending boilerplate. | |
227 json_description += "}"; | |
228 condition->Init(*base::test::ParseJson(json_description)); | |
229 | |
230 return condition; | |
231 } | |
232 | |
233 // Create a rule with the ID |rule_id| and with a single condition and a | |
234 // cancelling action. The condition contains a non-matching non-URL attribute, | |
235 // and optionally, according to |with_url_attribute| also a URL attribute | |
236 // matching any (!) URL. | |
237 linked_ptr<RulesRegistry::Rule> CreateNonMatchingRule( | |
238 bool with_url_attribute, | |
239 const char* rule_id) { | |
240 std::vector<const char*> attributes; | |
241 if (with_url_attribute) | |
242 attributes.push_back("\"url\": { \"pathContains\": \"\" }, \n"); | |
battre
2013/01/15 13:34:10
nit: please add a new line after this line (also b
vabr (Chromium)
2013/01/15 14:14:07
Done.
| |
243 // The following attribute never matches in this unit test. | |
244 attributes.push_back("\"resourceType\": [\"stylesheet\"], \n"); | |
245 linked_ptr<json_schema_compiler::any::Any> condition = | |
246 CreateCondition(attributes); | |
247 | |
248 DictionaryValue action_dict; | |
249 action_dict.SetString(keys::kInstanceTypeKey, keys::kCancelRequestType); | |
250 linked_ptr<json_schema_compiler::any::Any> action( | |
251 new json_schema_compiler::any::Any); | |
252 action->Init(action_dict); | |
253 | |
254 linked_ptr<RulesRegistry::Rule> rule(new RulesRegistry::Rule); | |
255 rule->id.reset(new std::string(rule_id)); | |
256 rule->priority.reset(new int(1)); | |
257 rule->actions.push_back(action); | |
258 rule->conditions.push_back(condition); | |
259 return rule; | |
260 } | |
261 | |
262 // Create a rule with the ID |rule_id| and with two conditions and a | |
263 // cancelling action. One condition contains a non-matching non-URL attribute, | |
264 // and the other one a URL attribute matching any URL. | |
265 linked_ptr<RulesRegistry::Rule> CreateRuleWithTwoConditions( | |
266 const char* rule_id) { | |
267 std::vector<const char*> attributes; | |
268 attributes.push_back("\"url\": { \"pathContains\": \"\" }, \n"); | |
269 linked_ptr<json_schema_compiler::any::Any> url_condition = | |
270 CreateCondition(attributes); | |
271 | |
272 attributes.clear(); | |
273 // The following attribute never matches in this unit test. | |
274 attributes.push_back("\"resourceType\": [\"stylesheet\"], \n"); | |
275 linked_ptr<json_schema_compiler::any::Any> non_matching_condition = | |
276 CreateCondition(attributes); | |
277 | |
278 DictionaryValue action_dict; | |
279 action_dict.SetString(keys::kInstanceTypeKey, keys::kCancelRequestType); | |
280 linked_ptr<json_schema_compiler::any::Any> action( | |
281 new json_schema_compiler::any::Any); | |
282 action->Init(action_dict); | |
283 | |
284 linked_ptr<RulesRegistry::Rule> rule(new RulesRegistry::Rule); | |
285 rule->id.reset(new std::string(rule_id)); | |
286 rule->priority.reset(new int(1)); | |
287 rule->actions.push_back(action); | |
288 rule->conditions.push_back(url_condition); | |
289 rule->conditions.push_back(non_matching_condition); | |
290 return rule; | |
291 } | |
292 | |
208 protected: | 293 protected: |
209 MessageLoop message_loop; | 294 MessageLoop message_loop; |
210 content::TestBrowserThread ui; | 295 content::TestBrowserThread ui; |
211 content::TestBrowserThread io; | 296 content::TestBrowserThread io; |
212 }; | 297 }; |
213 | 298 |
214 TEST_F(WebRequestRulesRegistryTest, AddRulesImpl) { | 299 TEST_F(WebRequestRulesRegistryTest, AddRulesImpl) { |
215 scoped_refptr<TestWebRequestRulesRegistry> registry( | 300 scoped_refptr<TestWebRequestRulesRegistry> registry( |
216 new TestWebRequestRulesRegistry()); | 301 new TestWebRequestRulesRegistry()); |
217 std::string error; | 302 std::string error; |
218 | 303 |
219 std::vector<linked_ptr<RulesRegistry::Rule> > rules; | 304 std::vector<linked_ptr<RulesRegistry::Rule> > rules; |
220 rules.push_back(CreateRule1()); | 305 rules.push_back(CreateRule1()); |
221 rules.push_back(CreateRule2()); | 306 rules.push_back(CreateRule2()); |
222 | 307 |
223 error = registry->AddRules(kExtensionId, rules); | 308 error = registry->AddRules(kExtensionId, rules); |
224 EXPECT_EQ("", error); | 309 EXPECT_EQ("", error); |
225 EXPECT_EQ(1, registry->num_clear_cache_calls()); | 310 EXPECT_EQ(1, registry->num_clear_cache_calls()); |
226 | 311 |
227 std::set<WebRequestRule::GlobalRuleId> matches; | 312 std::set<const WebRequestRule*> matches; |
228 | 313 |
229 GURL http_url("http://www.example.com"); | 314 GURL http_url("http://www.example.com"); |
230 net::TestURLRequestContext context; | 315 net::TestURLRequestContext context; |
231 net::TestURLRequest http_request(http_url, NULL, &context); | 316 net::TestURLRequest http_request(http_url, NULL, &context); |
232 matches = registry->GetMatches( | 317 matches = registry->GetMatches( |
233 WebRequestRule::RequestData(&http_request, ON_BEFORE_REQUEST)); | 318 WebRequestRule::RequestData(&http_request, ON_BEFORE_REQUEST)); |
234 EXPECT_EQ(2u, matches.size()); | 319 EXPECT_EQ(2u, matches.size()); |
235 EXPECT_TRUE(matches.find(std::make_pair(kExtensionId, kRuleId1)) != | 320 |
236 matches.end()); | 321 std::set<WebRequestRule::GlobalRuleId> matches_ids; |
237 EXPECT_TRUE(matches.find(std::make_pair(kExtensionId, kRuleId2)) != | 322 for (std::set<const WebRequestRule*>::const_iterator it = matches.begin(); |
238 matches.end()); | 323 it != matches.end(); ++it) |
324 matches_ids.insert((*it)->id()); | |
325 EXPECT_TRUE(ContainsKey(matches_ids, std::make_pair(kExtensionId, kRuleId1))); | |
326 EXPECT_TRUE(ContainsKey(matches_ids, std::make_pair(kExtensionId, kRuleId2))); | |
239 | 327 |
240 GURL foobar_url("http://www.foobar.com"); | 328 GURL foobar_url("http://www.foobar.com"); |
241 net::TestURLRequest foobar_request(foobar_url, NULL, &context); | 329 net::TestURLRequest foobar_request(foobar_url, NULL, &context); |
242 matches = registry->GetMatches( | 330 matches = registry->GetMatches( |
243 WebRequestRule::RequestData(&foobar_request, ON_BEFORE_REQUEST)); | 331 WebRequestRule::RequestData(&foobar_request, ON_BEFORE_REQUEST)); |
244 EXPECT_EQ(1u, matches.size()); | 332 EXPECT_EQ(1u, matches.size()); |
245 EXPECT_TRUE(matches.find(std::make_pair(kExtensionId, kRuleId2)) != | 333 WebRequestRule::GlobalRuleId expected_pair = |
246 matches.end()); | 334 std::make_pair(kExtensionId, kRuleId2); |
335 EXPECT_EQ(expected_pair, (*matches.begin())->id()); | |
247 } | 336 } |
248 | 337 |
249 TEST_F(WebRequestRulesRegistryTest, RemoveRulesImpl) { | 338 TEST_F(WebRequestRulesRegistryTest, RemoveRulesImpl) { |
250 scoped_refptr<TestWebRequestRulesRegistry> registry( | 339 scoped_refptr<TestWebRequestRulesRegistry> registry( |
251 new TestWebRequestRulesRegistry()); | 340 new TestWebRequestRulesRegistry()); |
252 std::string error; | 341 std::string error; |
253 | 342 |
254 // Setup RulesRegistry to contain two rules. | 343 // Setup RulesRegistry to contain two rules. |
255 std::vector<linked_ptr<RulesRegistry::Rule> > rules_to_add; | 344 std::vector<linked_ptr<RulesRegistry::Rule> > rules_to_add; |
256 rules_to_add.push_back(CreateRule1()); | 345 rules_to_add.push_back(CreateRule1()); |
257 rules_to_add.push_back(CreateRule2()); | 346 rules_to_add.push_back(CreateRule2()); |
258 error = registry->AddRules(kExtensionId, rules_to_add); | 347 error = registry->AddRules(kExtensionId, rules_to_add); |
259 EXPECT_EQ("", error); | 348 EXPECT_EQ("", error); |
260 EXPECT_EQ(1, registry->num_clear_cache_calls()); | 349 EXPECT_EQ(1, registry->num_clear_cache_calls()); |
261 | 350 |
262 // Verify initial state. | 351 // Verify initial state. |
263 std::vector<linked_ptr<RulesRegistry::Rule> > registered_rules; | 352 std::vector<linked_ptr<RulesRegistry::Rule> > registered_rules; |
264 registry->GetAllRules(kExtensionId, ®istered_rules); | 353 registry->GetAllRules(kExtensionId, ®istered_rules); |
265 EXPECT_EQ(2u, registered_rules.size()); | 354 EXPECT_EQ(2u, registered_rules.size()); |
355 EXPECT_EQ(1u, registry->RulesWithoutTriggers()); | |
266 | 356 |
267 // Remove first rule. | 357 // Remove first rule. |
268 std::vector<std::string> rules_to_remove; | 358 std::vector<std::string> rules_to_remove; |
269 rules_to_remove.push_back(kRuleId1); | 359 rules_to_remove.push_back(kRuleId1); |
270 error = registry->RemoveRules(kExtensionId, rules_to_remove); | 360 error = registry->RemoveRules(kExtensionId, rules_to_remove); |
271 EXPECT_EQ("", error); | 361 EXPECT_EQ("", error); |
272 EXPECT_EQ(2, registry->num_clear_cache_calls()); | 362 EXPECT_EQ(2, registry->num_clear_cache_calls()); |
273 | 363 |
274 // Verify that only one rule is left. | 364 // Verify that only one rule is left. |
275 registered_rules.clear(); | 365 registered_rules.clear(); |
276 registry->GetAllRules(kExtensionId, ®istered_rules); | 366 registry->GetAllRules(kExtensionId, ®istered_rules); |
277 EXPECT_EQ(1u, registered_rules.size()); | 367 EXPECT_EQ(1u, registered_rules.size()); |
368 EXPECT_EQ(1u, registry->RulesWithoutTriggers()); | |
278 | 369 |
279 // Now rules_to_remove contains both rules, i.e. one that does not exist in | 370 // 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. | 371 // the rules registry anymore. Effectively we only remove the second rule. |
281 rules_to_remove.push_back(kRuleId2); | 372 rules_to_remove.push_back(kRuleId2); |
282 error = registry->RemoveRules(kExtensionId, rules_to_remove); | 373 error = registry->RemoveRules(kExtensionId, rules_to_remove); |
283 EXPECT_EQ("", error); | 374 EXPECT_EQ("", error); |
284 EXPECT_EQ(3, registry->num_clear_cache_calls()); | 375 EXPECT_EQ(3, registry->num_clear_cache_calls()); |
285 | 376 |
286 // Verify that everything is gone. | 377 // Verify that everything is gone. |
287 registered_rules.clear(); | 378 registered_rules.clear(); |
288 registry->GetAllRules(kExtensionId, ®istered_rules); | 379 registry->GetAllRules(kExtensionId, ®istered_rules); |
289 EXPECT_EQ(0u, registered_rules.size()); | 380 EXPECT_EQ(0u, registered_rules.size()); |
381 EXPECT_EQ(0u, registry->RulesWithoutTriggers()); | |
290 | 382 |
291 EXPECT_TRUE(registry->IsEmpty()); | 383 EXPECT_TRUE(registry->IsEmpty()); |
292 } | 384 } |
293 | 385 |
294 TEST_F(WebRequestRulesRegistryTest, RemoveAllRulesImpl) { | 386 TEST_F(WebRequestRulesRegistryTest, RemoveAllRulesImpl) { |
295 scoped_refptr<TestWebRequestRulesRegistry> registry( | 387 scoped_refptr<TestWebRequestRulesRegistry> registry( |
296 new TestWebRequestRulesRegistry()); | 388 new TestWebRequestRulesRegistry()); |
297 std::string error; | 389 std::string error; |
298 | 390 |
299 // Setup RulesRegistry to contain two rules, one for each extension. | 391 // Setup RulesRegistry to contain two rules, one for each extension. |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
419 | 511 |
420 // The redirect by the first extension is ignored due to the ignore rule. | 512 // The redirect by the first extension is ignored due to the ignore rule. |
421 ASSERT_EQ(1u, deltas.size()); | 513 ASSERT_EQ(1u, deltas.size()); |
422 LinkedPtrEventResponseDelta effective_rule = *(deltas.begin()); | 514 LinkedPtrEventResponseDelta effective_rule = *(deltas.begin()); |
423 | 515 |
424 EXPECT_EQ(kExtensionId2, effective_rule->extension_id); | 516 EXPECT_EQ(kExtensionId2, effective_rule->extension_id); |
425 EXPECT_EQ(base::Time() + base::TimeDelta::FromDays(2), | 517 EXPECT_EQ(base::Time() + base::TimeDelta::FromDays(2), |
426 effective_rule->extension_install_time); | 518 effective_rule->extension_install_time); |
427 EXPECT_EQ(GURL("http://www.bar.com"), effective_rule->new_url); | 519 EXPECT_EQ(GURL("http://www.bar.com"), effective_rule->new_url); |
428 } | 520 } |
521 | |
522 // Test that rules failing IsFulfilled on their conditions are never returned by | |
523 // GetMatches. | |
524 TEST_F(WebRequestRulesRegistryTest, GetMatchesCheckFulfilled) { | |
525 scoped_refptr<TestWebRequestRulesRegistry> registry( | |
526 new TestWebRequestRulesRegistry()); | |
527 std::string error; | |
528 | |
529 std::vector<linked_ptr<RulesRegistry::Rule> > rules; | |
530 // Both rules have one condition, neither of them should fire. | |
531 // This rule's condition has only one, non-matching and non-URL attribute. | |
532 rules.push_back(CreateNonMatchingRule(false, kRuleId1)); | |
533 // This rule's condition has two attributes: a matching URL, and a | |
534 // non-matching non-URL attribute. | |
535 rules.push_back(CreateNonMatchingRule(true, kRuleId2)); | |
536 | |
537 // The 3rd rule has two conditions, one with a matching URL attribute, and one | |
538 // with a non-matching non-URL attribute. | |
539 rules.push_back(CreateRuleWithTwoConditions(kRuleId3)); | |
540 | |
541 error = registry->AddRules(kExtensionId, rules); | |
542 EXPECT_EQ("", error); | |
543 EXPECT_EQ(1, registry->num_clear_cache_calls()); | |
544 | |
545 std::set<const WebRequestRule*> matches; | |
546 | |
547 GURL http_url("http://www.example.com"); | |
548 net::TestURLRequestContext context; | |
549 net::TestURLRequest http_request(http_url, NULL, &context); | |
550 matches = registry->GetMatches( | |
551 WebRequestRule::RequestData(&http_request, ON_BEFORE_REQUEST)); | |
552 EXPECT_EQ(1u, matches.size()); | |
553 WebRequestRule::GlobalRuleId expected_pair = std::make_pair(kExtensionId, | |
554 kRuleId3); | |
555 EXPECT_EQ(expected_pair, (*matches.begin())->id()); | |
556 } | |
557 | |
429 } // namespace extensions | 558 } // namespace extensions |
OLD | NEW |