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"); |
| 243 |
| 244 // The following attribute never matches in this unit test. |
| 245 attributes.push_back("\"resourceType\": [\"stylesheet\"], \n"); |
| 246 |
| 247 linked_ptr<json_schema_compiler::any::Any> condition = |
| 248 CreateCondition(attributes); |
| 249 |
| 250 DictionaryValue action_dict; |
| 251 action_dict.SetString(keys::kInstanceTypeKey, keys::kCancelRequestType); |
| 252 linked_ptr<json_schema_compiler::any::Any> action( |
| 253 new json_schema_compiler::any::Any); |
| 254 action->Init(action_dict); |
| 255 |
| 256 linked_ptr<RulesRegistry::Rule> rule(new RulesRegistry::Rule); |
| 257 rule->id.reset(new std::string(rule_id)); |
| 258 rule->priority.reset(new int(1)); |
| 259 rule->actions.push_back(action); |
| 260 rule->conditions.push_back(condition); |
| 261 return rule; |
| 262 } |
| 263 |
| 264 // Create a rule with the ID |rule_id| and with two conditions and a |
| 265 // cancelling action. One condition contains a non-matching non-URL attribute, |
| 266 // and the other one a URL attribute matching any URL. |
| 267 linked_ptr<RulesRegistry::Rule> CreateRuleWithTwoConditions( |
| 268 const char* rule_id) { |
| 269 std::vector<const char*> attributes; |
| 270 attributes.push_back("\"url\": { \"pathContains\": \"\" }, \n"); |
| 271 linked_ptr<json_schema_compiler::any::Any> url_condition = |
| 272 CreateCondition(attributes); |
| 273 |
| 274 attributes.clear(); |
| 275 // The following attribute never matches in this unit test. |
| 276 attributes.push_back("\"resourceType\": [\"stylesheet\"], \n"); |
| 277 linked_ptr<json_schema_compiler::any::Any> non_matching_condition = |
| 278 CreateCondition(attributes); |
| 279 |
| 280 DictionaryValue action_dict; |
| 281 action_dict.SetString(keys::kInstanceTypeKey, keys::kCancelRequestType); |
| 282 linked_ptr<json_schema_compiler::any::Any> action( |
| 283 new json_schema_compiler::any::Any); |
| 284 action->Init(action_dict); |
| 285 |
| 286 linked_ptr<RulesRegistry::Rule> rule(new RulesRegistry::Rule); |
| 287 rule->id.reset(new std::string(rule_id)); |
| 288 rule->priority.reset(new int(1)); |
| 289 rule->actions.push_back(action); |
| 290 rule->conditions.push_back(url_condition); |
| 291 rule->conditions.push_back(non_matching_condition); |
| 292 return rule; |
| 293 } |
| 294 |
208 protected: | 295 protected: |
209 MessageLoop message_loop; | 296 MessageLoop message_loop; |
210 content::TestBrowserThread ui; | 297 content::TestBrowserThread ui; |
211 content::TestBrowserThread io; | 298 content::TestBrowserThread io; |
212 }; | 299 }; |
213 | 300 |
214 TEST_F(WebRequestRulesRegistryTest, AddRulesImpl) { | 301 TEST_F(WebRequestRulesRegistryTest, AddRulesImpl) { |
215 scoped_refptr<TestWebRequestRulesRegistry> registry( | 302 scoped_refptr<TestWebRequestRulesRegistry> registry( |
216 new TestWebRequestRulesRegistry()); | 303 new TestWebRequestRulesRegistry()); |
217 std::string error; | 304 std::string error; |
218 | 305 |
219 std::vector<linked_ptr<RulesRegistry::Rule> > rules; | 306 std::vector<linked_ptr<RulesRegistry::Rule> > rules; |
220 rules.push_back(CreateRule1()); | 307 rules.push_back(CreateRule1()); |
221 rules.push_back(CreateRule2()); | 308 rules.push_back(CreateRule2()); |
222 | 309 |
223 error = registry->AddRules(kExtensionId, rules); | 310 error = registry->AddRules(kExtensionId, rules); |
224 EXPECT_EQ("", error); | 311 EXPECT_EQ("", error); |
225 EXPECT_EQ(1, registry->num_clear_cache_calls()); | 312 EXPECT_EQ(1, registry->num_clear_cache_calls()); |
226 | 313 |
227 std::set<WebRequestRule::GlobalRuleId> matches; | 314 std::set<const WebRequestRule*> matches; |
228 | 315 |
229 GURL http_url("http://www.example.com"); | 316 GURL http_url("http://www.example.com"); |
230 net::TestURLRequestContext context; | 317 net::TestURLRequestContext context; |
231 net::TestURLRequest http_request(http_url, NULL, &context); | 318 net::TestURLRequest http_request(http_url, NULL, &context); |
232 matches = registry->GetMatches( | 319 matches = registry->GetMatches( |
233 WebRequestRule::RequestData(&http_request, ON_BEFORE_REQUEST)); | 320 WebRequestRule::RequestData(&http_request, ON_BEFORE_REQUEST)); |
234 EXPECT_EQ(2u, matches.size()); | 321 EXPECT_EQ(2u, matches.size()); |
235 EXPECT_TRUE(matches.find(std::make_pair(kExtensionId, kRuleId1)) != | 322 |
236 matches.end()); | 323 std::set<WebRequestRule::GlobalRuleId> matches_ids; |
237 EXPECT_TRUE(matches.find(std::make_pair(kExtensionId, kRuleId2)) != | 324 for (std::set<const WebRequestRule*>::const_iterator it = matches.begin(); |
238 matches.end()); | 325 it != matches.end(); ++it) |
| 326 matches_ids.insert((*it)->id()); |
| 327 EXPECT_TRUE(ContainsKey(matches_ids, std::make_pair(kExtensionId, kRuleId1))); |
| 328 EXPECT_TRUE(ContainsKey(matches_ids, std::make_pair(kExtensionId, kRuleId2))); |
239 | 329 |
240 GURL foobar_url("http://www.foobar.com"); | 330 GURL foobar_url("http://www.foobar.com"); |
241 net::TestURLRequest foobar_request(foobar_url, NULL, &context); | 331 net::TestURLRequest foobar_request(foobar_url, NULL, &context); |
242 matches = registry->GetMatches( | 332 matches = registry->GetMatches( |
243 WebRequestRule::RequestData(&foobar_request, ON_BEFORE_REQUEST)); | 333 WebRequestRule::RequestData(&foobar_request, ON_BEFORE_REQUEST)); |
244 EXPECT_EQ(1u, matches.size()); | 334 EXPECT_EQ(1u, matches.size()); |
245 EXPECT_TRUE(matches.find(std::make_pair(kExtensionId, kRuleId2)) != | 335 WebRequestRule::GlobalRuleId expected_pair = |
246 matches.end()); | 336 std::make_pair(kExtensionId, kRuleId2); |
| 337 EXPECT_EQ(expected_pair, (*matches.begin())->id()); |
247 } | 338 } |
248 | 339 |
249 TEST_F(WebRequestRulesRegistryTest, RemoveRulesImpl) { | 340 TEST_F(WebRequestRulesRegistryTest, RemoveRulesImpl) { |
250 scoped_refptr<TestWebRequestRulesRegistry> registry( | 341 scoped_refptr<TestWebRequestRulesRegistry> registry( |
251 new TestWebRequestRulesRegistry()); | 342 new TestWebRequestRulesRegistry()); |
252 std::string error; | 343 std::string error; |
253 | 344 |
254 // Setup RulesRegistry to contain two rules. | 345 // Setup RulesRegistry to contain two rules. |
255 std::vector<linked_ptr<RulesRegistry::Rule> > rules_to_add; | 346 std::vector<linked_ptr<RulesRegistry::Rule> > rules_to_add; |
256 rules_to_add.push_back(CreateRule1()); | 347 rules_to_add.push_back(CreateRule1()); |
257 rules_to_add.push_back(CreateRule2()); | 348 rules_to_add.push_back(CreateRule2()); |
258 error = registry->AddRules(kExtensionId, rules_to_add); | 349 error = registry->AddRules(kExtensionId, rules_to_add); |
259 EXPECT_EQ("", error); | 350 EXPECT_EQ("", error); |
260 EXPECT_EQ(1, registry->num_clear_cache_calls()); | 351 EXPECT_EQ(1, registry->num_clear_cache_calls()); |
261 | 352 |
262 // Verify initial state. | 353 // Verify initial state. |
263 std::vector<linked_ptr<RulesRegistry::Rule> > registered_rules; | 354 std::vector<linked_ptr<RulesRegistry::Rule> > registered_rules; |
264 registry->GetAllRules(kExtensionId, ®istered_rules); | 355 registry->GetAllRules(kExtensionId, ®istered_rules); |
265 EXPECT_EQ(2u, registered_rules.size()); | 356 EXPECT_EQ(2u, registered_rules.size()); |
| 357 EXPECT_EQ(1u, registry->RulesWithoutTriggers()); |
266 | 358 |
267 // Remove first rule. | 359 // Remove first rule. |
268 std::vector<std::string> rules_to_remove; | 360 std::vector<std::string> rules_to_remove; |
269 rules_to_remove.push_back(kRuleId1); | 361 rules_to_remove.push_back(kRuleId1); |
270 error = registry->RemoveRules(kExtensionId, rules_to_remove); | 362 error = registry->RemoveRules(kExtensionId, rules_to_remove); |
271 EXPECT_EQ("", error); | 363 EXPECT_EQ("", error); |
272 EXPECT_EQ(2, registry->num_clear_cache_calls()); | 364 EXPECT_EQ(2, registry->num_clear_cache_calls()); |
273 | 365 |
274 // Verify that only one rule is left. | 366 // Verify that only one rule is left. |
275 registered_rules.clear(); | 367 registered_rules.clear(); |
276 registry->GetAllRules(kExtensionId, ®istered_rules); | 368 registry->GetAllRules(kExtensionId, ®istered_rules); |
277 EXPECT_EQ(1u, registered_rules.size()); | 369 EXPECT_EQ(1u, registered_rules.size()); |
| 370 EXPECT_EQ(1u, registry->RulesWithoutTriggers()); |
278 | 371 |
279 // Now rules_to_remove contains both rules, i.e. one that does not exist in | 372 // 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. | 373 // the rules registry anymore. Effectively we only remove the second rule. |
281 rules_to_remove.push_back(kRuleId2); | 374 rules_to_remove.push_back(kRuleId2); |
282 error = registry->RemoveRules(kExtensionId, rules_to_remove); | 375 error = registry->RemoveRules(kExtensionId, rules_to_remove); |
283 EXPECT_EQ("", error); | 376 EXPECT_EQ("", error); |
284 EXPECT_EQ(3, registry->num_clear_cache_calls()); | 377 EXPECT_EQ(3, registry->num_clear_cache_calls()); |
285 | 378 |
286 // Verify that everything is gone. | 379 // Verify that everything is gone. |
287 registered_rules.clear(); | 380 registered_rules.clear(); |
288 registry->GetAllRules(kExtensionId, ®istered_rules); | 381 registry->GetAllRules(kExtensionId, ®istered_rules); |
289 EXPECT_EQ(0u, registered_rules.size()); | 382 EXPECT_EQ(0u, registered_rules.size()); |
| 383 EXPECT_EQ(0u, registry->RulesWithoutTriggers()); |
290 | 384 |
291 EXPECT_TRUE(registry->IsEmpty()); | 385 EXPECT_TRUE(registry->IsEmpty()); |
292 } | 386 } |
293 | 387 |
294 TEST_F(WebRequestRulesRegistryTest, RemoveAllRulesImpl) { | 388 TEST_F(WebRequestRulesRegistryTest, RemoveAllRulesImpl) { |
295 scoped_refptr<TestWebRequestRulesRegistry> registry( | 389 scoped_refptr<TestWebRequestRulesRegistry> registry( |
296 new TestWebRequestRulesRegistry()); | 390 new TestWebRequestRulesRegistry()); |
297 std::string error; | 391 std::string error; |
298 | 392 |
299 // Setup RulesRegistry to contain two rules, one for each extension. | 393 // Setup RulesRegistry to contain two rules, one for each extension. |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
419 | 513 |
420 // The redirect by the first extension is ignored due to the ignore rule. | 514 // The redirect by the first extension is ignored due to the ignore rule. |
421 ASSERT_EQ(1u, deltas.size()); | 515 ASSERT_EQ(1u, deltas.size()); |
422 LinkedPtrEventResponseDelta effective_rule = *(deltas.begin()); | 516 LinkedPtrEventResponseDelta effective_rule = *(deltas.begin()); |
423 | 517 |
424 EXPECT_EQ(kExtensionId2, effective_rule->extension_id); | 518 EXPECT_EQ(kExtensionId2, effective_rule->extension_id); |
425 EXPECT_EQ(base::Time() + base::TimeDelta::FromDays(2), | 519 EXPECT_EQ(base::Time() + base::TimeDelta::FromDays(2), |
426 effective_rule->extension_install_time); | 520 effective_rule->extension_install_time); |
427 EXPECT_EQ(GURL("http://www.bar.com"), effective_rule->new_url); | 521 EXPECT_EQ(GURL("http://www.bar.com"), effective_rule->new_url); |
428 } | 522 } |
| 523 |
| 524 // Test that rules failing IsFulfilled on their conditions are never returned by |
| 525 // GetMatches. |
| 526 TEST_F(WebRequestRulesRegistryTest, GetMatchesCheckFulfilled) { |
| 527 scoped_refptr<TestWebRequestRulesRegistry> registry( |
| 528 new TestWebRequestRulesRegistry()); |
| 529 std::string error; |
| 530 |
| 531 std::vector<linked_ptr<RulesRegistry::Rule> > rules; |
| 532 // Both rules have one condition, neither of them should fire. |
| 533 // This rule's condition has only one, non-matching and non-URL attribute. |
| 534 rules.push_back(CreateNonMatchingRule(false, kRuleId1)); |
| 535 // This rule's condition has two attributes: a matching URL, and a |
| 536 // non-matching non-URL attribute. |
| 537 rules.push_back(CreateNonMatchingRule(true, kRuleId2)); |
| 538 |
| 539 // The 3rd rule has two conditions, one with a matching URL attribute, and one |
| 540 // with a non-matching non-URL attribute. |
| 541 rules.push_back(CreateRuleWithTwoConditions(kRuleId3)); |
| 542 |
| 543 error = registry->AddRules(kExtensionId, rules); |
| 544 EXPECT_EQ("", error); |
| 545 EXPECT_EQ(1, registry->num_clear_cache_calls()); |
| 546 |
| 547 std::set<const WebRequestRule*> matches; |
| 548 |
| 549 GURL http_url("http://www.example.com"); |
| 550 net::TestURLRequestContext context; |
| 551 net::TestURLRequest http_request(http_url, NULL, &context); |
| 552 matches = registry->GetMatches( |
| 553 WebRequestRule::RequestData(&http_request, ON_BEFORE_REQUEST)); |
| 554 EXPECT_EQ(1u, matches.size()); |
| 555 WebRequestRule::GlobalRuleId expected_pair = std::make_pair(kExtensionId, |
| 556 kRuleId3); |
| 557 EXPECT_EQ(expected_pair, (*matches.begin())->id()); |
| 558 } |
| 559 |
429 } // namespace extensions | 560 } // namespace extensions |
OLD | NEW |