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

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

Issue 10406013: Support extension precedences in declarative WebRequest API (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/browser/extensions/api/declarative_webrequest/webrequest_rules_registry.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_rules_ registry.h" 5 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_rules_ registry.h"
6 6
7 #include <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/values.h" 11 #include "base/values.h"
12 #include "chrome/common/extensions/matcher/url_matcher_constants.h" 12 #include "chrome/common/extensions/matcher/url_matcher_constants.h"
13 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_consta nts.h" 13 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_consta nts.h"
14 #include "chrome/browser/extensions/api/web_request/web_request_api_helpers.h"
14 #include "content/test/test_browser_thread.h" 15 #include "content/test/test_browser_thread.h"
15 #include "net/url_request/url_request_test_util.h" 16 #include "net/url_request/url_request_test_util.h"
16 #include "testing/gtest/include/gtest/gtest.h" 17 #include "testing/gtest/include/gtest/gtest.h"
17 18
18 namespace { 19 namespace {
19 const char kExtensionId[] = "ext1"; 20 const char kExtensionId[] = "ext1";
20 const char kExtensionId2[] = "ext2"; 21 const char kExtensionId2[] = "ext2";
21 const char kRuleId1[] = "rule1"; 22 const char kRuleId1[] = "rule1";
22 const char kRuleId2[] = "rule2"; 23 const char kRuleId2[] = "rule2";
24 const char kRuleId3[] = "rule3";
23 } // namespace 25 } // namespace
24 26
25 namespace extensions { 27 namespace extensions {
26 28
29 namespace helpers = extension_web_request_api_helpers;
27 namespace keys = declarative_webrequest_constants; 30 namespace keys = declarative_webrequest_constants;
28 namespace keys2 = url_matcher_constants; 31 namespace keys2 = url_matcher_constants;
29 32
33 class TestWebRequestRulesRegistry : public WebRequestRulesRegistry {
34 public:
35 TestWebRequestRulesRegistry() : WebRequestRulesRegistry(NULL) {}
36 virtual ~TestWebRequestRulesRegistry() {}
37
38 protected:
39 virtual base::Time GetExtensionInstallationTime(
40 const std::string& extension_id) const {
41 if (extension_id == kExtensionId)
42 return base::Time() + base::TimeDelta::FromDays(1);
43 else if (extension_id == kExtensionId2)
44 return base::Time() + base::TimeDelta::FromDays(2);
45 else
46 return base::Time();
47 }
48 };
49
30 class WebRequestRulesRegistryTest : public testing::Test { 50 class WebRequestRulesRegistryTest : public testing::Test {
31 public: 51 public:
32 public: 52 public:
33 WebRequestRulesRegistryTest() 53 WebRequestRulesRegistryTest()
34 : message_loop(MessageLoop::TYPE_IO), 54 : message_loop(MessageLoop::TYPE_IO),
35 ui(content::BrowserThread::UI, &message_loop), 55 ui(content::BrowserThread::UI, &message_loop),
36 io(content::BrowserThread::IO, &message_loop) {} 56 io(content::BrowserThread::IO, &message_loop) {}
37 57
38 virtual ~WebRequestRulesRegistryTest() {} 58 virtual ~WebRequestRulesRegistryTest() {}
39 59
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
109 129
110 linked_ptr<RulesRegistry::Rule> rule = 130 linked_ptr<RulesRegistry::Rule> rule =
111 make_linked_ptr(new RulesRegistry::Rule); 131 make_linked_ptr(new RulesRegistry::Rule);
112 rule->id.reset(new std::string(kRuleId2)); 132 rule->id.reset(new std::string(kRuleId2));
113 rule->priority.reset(new int(100)); 133 rule->priority.reset(new int(100));
114 rule->actions.push_back(action); 134 rule->actions.push_back(action);
115 rule->conditions.push_back(condition); 135 rule->conditions.push_back(condition);
116 return rule; 136 return rule;
117 } 137 }
118 138
139 linked_ptr<RulesRegistry::Rule> CreateRedirectRule(
140 const std::string& destination) {
141 DictionaryValue condition_dict;
142 condition_dict.SetString(keys::kInstanceTypeKey, keys::kRequestMatcherType);
143
144 linked_ptr<json_schema_compiler::any::Any> condition = make_linked_ptr(
145 new json_schema_compiler::any::Any);
146 condition->Init(condition_dict);
147
148 DictionaryValue action_dict;
149 action_dict.SetString(keys::kInstanceTypeKey, keys::kRedirectRequestType);
150 action_dict.SetString(keys::kRedirectUrlKey, destination);
151
152 linked_ptr<json_schema_compiler::any::Any> action = make_linked_ptr(
153 new json_schema_compiler::any::Any);
154 action->Init(action_dict);
155
156 linked_ptr<RulesRegistry::Rule> rule =
157 make_linked_ptr(new RulesRegistry::Rule);
158 rule->id.reset(new std::string(kRuleId3));
159 rule->priority.reset(new int(100));
160 rule->actions.push_back(action);
161 rule->conditions.push_back(condition);
162 return rule;
163 }
164
119 protected: 165 protected:
120 MessageLoop message_loop; 166 MessageLoop message_loop;
121 content::TestBrowserThread ui; 167 content::TestBrowserThread ui;
122 content::TestBrowserThread io; 168 content::TestBrowserThread io;
123 }; 169 };
124 170
125 TEST_F(WebRequestRulesRegistryTest, AddRulesImpl) { 171 TEST_F(WebRequestRulesRegistryTest, AddRulesImpl) {
126 scoped_refptr<WebRequestRulesRegistry> registry(new WebRequestRulesRegistry); 172 scoped_refptr<WebRequestRulesRegistry> registry(
173 new WebRequestRulesRegistry(NULL));
Matt Perry 2012/05/17 00:42:17 could you make these all TestWebRequestRulesRegist
battre 2012/05/18 16:21:40 Done.
127 std::string error; 174 std::string error;
128 175
129 std::vector<linked_ptr<RulesRegistry::Rule> > rules; 176 std::vector<linked_ptr<RulesRegistry::Rule> > rules;
130 rules.push_back(CreateRule1()); 177 rules.push_back(CreateRule1());
131 rules.push_back(CreateRule2()); 178 rules.push_back(CreateRule2());
132 179
133 error = registry->AddRules(kExtensionId, rules); 180 error = registry->AddRules(kExtensionId, rules);
134 EXPECT_EQ("", error); 181 EXPECT_EQ("", error);
135 182
136 std::set<WebRequestRule::GlobalRuleId> matches; 183 std::set<WebRequestRule::GlobalRuleId> matches;
137 184
138 GURL http_url("http://www.example.com"); 185 GURL http_url("http://www.example.com");
139 TestURLRequest http_request(http_url, NULL); 186 TestURLRequest http_request(http_url, NULL);
140 matches = registry->GetMatches(&http_request, ON_BEFORE_REQUEST); 187 matches = registry->GetMatches(&http_request, ON_BEFORE_REQUEST);
141 EXPECT_EQ(2u, matches.size()); 188 EXPECT_EQ(2u, matches.size());
142 EXPECT_TRUE(matches.find(std::make_pair(kExtensionId, kRuleId1)) != 189 EXPECT_TRUE(matches.find(std::make_pair(kExtensionId, kRuleId1)) !=
143 matches.end()); 190 matches.end());
144 EXPECT_TRUE(matches.find(std::make_pair(kExtensionId, kRuleId2)) != 191 EXPECT_TRUE(matches.find(std::make_pair(kExtensionId, kRuleId2)) !=
145 matches.end()); 192 matches.end());
146 193
147 GURL foobar_url("http://www.foobar.com"); 194 GURL foobar_url("http://www.foobar.com");
148 TestURLRequest foobar_request(foobar_url, NULL); 195 TestURLRequest foobar_request(foobar_url, NULL);
149 matches = registry->GetMatches(&foobar_request, ON_BEFORE_REQUEST); 196 matches = registry->GetMatches(&foobar_request, ON_BEFORE_REQUEST);
150 EXPECT_EQ(1u, matches.size()); 197 EXPECT_EQ(1u, matches.size());
151 EXPECT_TRUE(matches.find(std::make_pair(kExtensionId, kRuleId2)) != 198 EXPECT_TRUE(matches.find(std::make_pair(kExtensionId, kRuleId2)) !=
152 matches.end()); 199 matches.end());
153 } 200 }
154 201
155 TEST_F(WebRequestRulesRegistryTest, RemoveRulesImpl) { 202 TEST_F(WebRequestRulesRegistryTest, RemoveRulesImpl) {
156 scoped_refptr<WebRequestRulesRegistry> registry(new WebRequestRulesRegistry); 203 scoped_refptr<WebRequestRulesRegistry> registry(
204 new WebRequestRulesRegistry(NULL));
157 std::string error; 205 std::string error;
158 206
159 // Setup RulesRegistry to contain two rules. 207 // Setup RulesRegistry to contain two rules.
160 std::vector<linked_ptr<RulesRegistry::Rule> > rules_to_add; 208 std::vector<linked_ptr<RulesRegistry::Rule> > rules_to_add;
161 rules_to_add.push_back(CreateRule1()); 209 rules_to_add.push_back(CreateRule1());
162 rules_to_add.push_back(CreateRule2()); 210 rules_to_add.push_back(CreateRule2());
163 error = registry->AddRules(kExtensionId, rules_to_add); 211 error = registry->AddRules(kExtensionId, rules_to_add);
164 EXPECT_EQ("", error); 212 EXPECT_EQ("", error);
165 213
166 // Verify initial state. 214 // Verify initial state.
(...skipping 20 matching lines...) Expand all
187 235
188 // Verify that everything is gone. 236 // Verify that everything is gone.
189 registered_rules.clear(); 237 registered_rules.clear();
190 registry->GetAllRules(kExtensionId, &registered_rules); 238 registry->GetAllRules(kExtensionId, &registered_rules);
191 EXPECT_EQ(0u, registered_rules.size()); 239 EXPECT_EQ(0u, registered_rules.size());
192 240
193 EXPECT_TRUE(registry->IsEmpty()); 241 EXPECT_TRUE(registry->IsEmpty());
194 } 242 }
195 243
196 TEST_F(WebRequestRulesRegistryTest, RemoveAllRulesImpl) { 244 TEST_F(WebRequestRulesRegistryTest, RemoveAllRulesImpl) {
197 scoped_refptr<WebRequestRulesRegistry> registry(new WebRequestRulesRegistry); 245 scoped_refptr<WebRequestRulesRegistry> registry(
246 new WebRequestRulesRegistry(NULL));
198 std::string error; 247 std::string error;
199 248
200 // Setup RulesRegistry to contain two rules, one for each extension. 249 // Setup RulesRegistry to contain two rules, one for each extension.
201 std::vector<linked_ptr<RulesRegistry::Rule> > rules_to_add(1); 250 std::vector<linked_ptr<RulesRegistry::Rule> > rules_to_add(1);
202 rules_to_add[0] = CreateRule1(); 251 rules_to_add[0] = CreateRule1();
203 error = registry->AddRules(kExtensionId, rules_to_add); 252 error = registry->AddRules(kExtensionId, rules_to_add);
204 EXPECT_EQ("", error); 253 EXPECT_EQ("", error);
205 254
206 rules_to_add[0] = CreateRule2(); 255 rules_to_add[0] = CreateRule2();
207 error = registry->AddRules(kExtensionId2, rules_to_add); 256 error = registry->AddRules(kExtensionId2, rules_to_add);
(...skipping 23 matching lines...) Expand all
231 error = registry->RemoveAllRules(kExtensionId); 280 error = registry->RemoveAllRules(kExtensionId);
232 EXPECT_EQ("", error); 281 EXPECT_EQ("", error);
233 282
234 // Remove rule from second extension. 283 // Remove rule from second extension.
235 error = registry->RemoveAllRules(kExtensionId2); 284 error = registry->RemoveAllRules(kExtensionId2);
236 EXPECT_EQ("", error); 285 EXPECT_EQ("", error);
237 286
238 EXPECT_TRUE(registry->IsEmpty()); 287 EXPECT_TRUE(registry->IsEmpty());
239 } 288 }
240 289
290 TEST_F(WebRequestRulesRegistryTest, Precedences) {
291 scoped_refptr<WebRequestRulesRegistry> registry(
292 new TestWebRequestRulesRegistry());
293 std::string error;
294
295 std::vector<linked_ptr<RulesRegistry::Rule> > rules_to_add_1(1);
296 rules_to_add_1[0] = CreateRedirectRule("http://www.foo.com");
297 error = registry->AddRules(kExtensionId, rules_to_add_1);
298 EXPECT_EQ("", error);
299
300 std::vector<linked_ptr<RulesRegistry::Rule> > rules_to_add_2(1);
301 rules_to_add_2[0] = CreateRedirectRule("http://www.bar.com");
302 error = registry->AddRules(kExtensionId2, rules_to_add_2);
303 EXPECT_EQ("", error);
304
305 GURL url("http://www.google.com");
306 TestURLRequest request(url, NULL);
307 std::list<LinkedPtrEventResponseDelta> deltas =
308 registry->CreateDeltas(&request, ON_BEFORE_REQUEST);
309
310 // The second extension is installed later and will win for this reason
311 // in conflict resolution.
312 ASSERT_EQ(2u, deltas.size());
313 deltas.sort(&helpers::InDecreasingExtensionInstallationTimeOrder);
314
315 std::list<LinkedPtrEventResponseDelta>::iterator i = deltas.begin();
316 LinkedPtrEventResponseDelta winner = *i;
Matt Perry 2012/05/17 00:42:17 *i++ works too
battre 2012/05/18 16:21:40 Done.
317 ++i;
318 LinkedPtrEventResponseDelta loser = *i;
319
320 EXPECT_EQ(kExtensionId2, winner->extension_id);
321 EXPECT_EQ(base::Time() + base::TimeDelta::FromDays(2),
322 winner->extension_install_time);
323 EXPECT_EQ(GURL("http://www.bar.com"), winner->new_url);
324
325 EXPECT_EQ(kExtensionId, loser->extension_id);
326 EXPECT_EQ(base::Time() + base::TimeDelta::FromDays(1),
327 loser->extension_install_time);
328 EXPECT_EQ(GURL("http://www.foo.com"), loser->new_url);
329 }
330
241 } // namespace extensions 331 } // namespace extensions
OLDNEW
« no previous file with comments | « chrome/browser/extensions/api/declarative_webrequest/webrequest_rules_registry.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698