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

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

Issue 11827026: Overhaul JSON Schema Compiler to support a number of features required to (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 7 years, 11 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
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/json/json_reader.h" 9 #include "base/json/json_reader.h"
10 #include "base/memory/linked_ptr.h" 10 #include "base/memory/linked_ptr.h"
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
105 scheme_http->Append(Value::CreateStringValue("https")); 105 scheme_http->Append(Value::CreateStringValue("https"));
106 DictionaryValue* https_condition_dict = new DictionaryValue(); 106 DictionaryValue* https_condition_dict = new DictionaryValue();
107 https_condition_dict->Set(keys2::kSchemesKey, scheme_https); 107 https_condition_dict->Set(keys2::kSchemesKey, scheme_https);
108 https_condition_dict->SetString(keys2::kHostSuffixKey, "example.com"); 108 https_condition_dict->SetString(keys2::kHostSuffixKey, "example.com");
109 https_condition_dict->SetString(keys2::kHostPrefixKey, "www"); 109 https_condition_dict->SetString(keys2::kHostPrefixKey, "www");
110 DictionaryValue https_condition_url_filter; 110 DictionaryValue https_condition_url_filter;
111 https_condition_url_filter.Set(keys::kUrlKey, https_condition_dict); 111 https_condition_url_filter.Set(keys::kUrlKey, https_condition_dict);
112 https_condition_url_filter.SetString(keys::kInstanceTypeKey, 112 https_condition_url_filter.SetString(keys::kInstanceTypeKey,
113 keys::kRequestMatcherType); 113 keys::kRequestMatcherType);
114 114
115 linked_ptr<json_schema_compiler::any::Any> condition1(
116 new json_schema_compiler::any::Any);
117 condition1->Init(http_condition_url_filter);
118
119 linked_ptr<json_schema_compiler::any::Any> condition2(
120 new json_schema_compiler::any::Any);
121 condition2->Init(https_condition_url_filter);
122
123 DictionaryValue action_dict; 115 DictionaryValue action_dict;
124 action_dict.SetString(keys::kInstanceTypeKey, keys::kCancelRequestType); 116 action_dict.SetString(keys::kInstanceTypeKey, keys::kCancelRequestType);
125 117
126 linked_ptr<json_schema_compiler::any::Any> action1(
127 new json_schema_compiler::any::Any);
128 action1->Init(action_dict);
129
130 linked_ptr<RulesRegistry::Rule> rule(new RulesRegistry::Rule); 118 linked_ptr<RulesRegistry::Rule> rule(new RulesRegistry::Rule);
131 rule->id.reset(new std::string(kRuleId1)); 119 rule->id.reset(new std::string(kRuleId1));
132 rule->priority.reset(new int(100)); 120 rule->priority.reset(new int(100));
133 rule->actions.push_back(action1); 121 rule->actions.push_back(linked_ptr<base::Value>(action_dict.DeepCopy()));
134 rule->conditions.push_back(condition1); 122 rule->conditions.push_back(
135 rule->conditions.push_back(condition2); 123 linked_ptr<base::Value>(http_condition_url_filter.DeepCopy()));
124 rule->conditions.push_back(
125 linked_ptr<base::Value>(https_condition_url_filter.DeepCopy()));
136 return rule; 126 return rule;
137 } 127 }
138 128
139 // Returns a rule that matches anything and cancels it. 129 // Returns a rule that matches anything and cancels it.
140 linked_ptr<RulesRegistry::Rule> CreateRule2() { 130 linked_ptr<RulesRegistry::Rule> CreateRule2() {
141 DictionaryValue condition_dict; 131 DictionaryValue condition_dict;
142 condition_dict.SetString(keys::kInstanceTypeKey, keys::kRequestMatcherType); 132 condition_dict.SetString(keys::kInstanceTypeKey, keys::kRequestMatcherType);
143 133
144 linked_ptr<json_schema_compiler::any::Any> condition(
145 new json_schema_compiler::any::Any);
146 condition->Init(condition_dict);
147
148 DictionaryValue action_dict; 134 DictionaryValue action_dict;
149 action_dict.SetString(keys::kInstanceTypeKey, keys::kCancelRequestType); 135 action_dict.SetString(keys::kInstanceTypeKey, keys::kCancelRequestType);
150 136
151 linked_ptr<json_schema_compiler::any::Any> action(
152 new json_schema_compiler::any::Any);
153 action->Init(action_dict);
154
155 linked_ptr<RulesRegistry::Rule> rule(new RulesRegistry::Rule); 137 linked_ptr<RulesRegistry::Rule> rule(new RulesRegistry::Rule);
156 rule->id.reset(new std::string(kRuleId2)); 138 rule->id.reset(new std::string(kRuleId2));
157 rule->priority.reset(new int(100)); 139 rule->priority.reset(new int(100));
158 rule->actions.push_back(action); 140 rule->actions.push_back(linked_ptr<base::Value>(action_dict.DeepCopy()));
159 rule->conditions.push_back(condition); 141 rule->conditions.push_back(
142 linked_ptr<base::Value>(condition_dict.DeepCopy()));
160 return rule; 143 return rule;
161 } 144 }
162 145
163 linked_ptr<RulesRegistry::Rule> CreateRedirectRule( 146 linked_ptr<RulesRegistry::Rule> CreateRedirectRule(
164 const std::string& destination) { 147 const std::string& destination) {
165 DictionaryValue condition_dict; 148 DictionaryValue condition_dict;
166 condition_dict.SetString(keys::kInstanceTypeKey, keys::kRequestMatcherType); 149 condition_dict.SetString(keys::kInstanceTypeKey, keys::kRequestMatcherType);
167 150
168 linked_ptr<json_schema_compiler::any::Any> condition(
169 new json_schema_compiler::any::Any);
170 condition->Init(condition_dict);
171
172 DictionaryValue action_dict; 151 DictionaryValue action_dict;
173 action_dict.SetString(keys::kInstanceTypeKey, keys::kRedirectRequestType); 152 action_dict.SetString(keys::kInstanceTypeKey, keys::kRedirectRequestType);
174 action_dict.SetString(keys::kRedirectUrlKey, destination); 153 action_dict.SetString(keys::kRedirectUrlKey, destination);
175 154
176 linked_ptr<json_schema_compiler::any::Any> action(
177 new json_schema_compiler::any::Any);
178 action->Init(action_dict);
179
180 linked_ptr<RulesRegistry::Rule> rule(new RulesRegistry::Rule); 155 linked_ptr<RulesRegistry::Rule> rule(new RulesRegistry::Rule);
181 rule->id.reset(new std::string(kRuleId3)); 156 rule->id.reset(new std::string(kRuleId3));
182 rule->priority.reset(new int(100)); 157 rule->priority.reset(new int(100));
183 rule->actions.push_back(action); 158 rule->actions.push_back(linked_ptr<base::Value>(action_dict.DeepCopy()));
184 rule->conditions.push_back(condition); 159 rule->conditions.push_back(
160 linked_ptr<base::Value>(condition_dict.DeepCopy()));
185 return rule; 161 return rule;
186 } 162 }
187 163
188 // Create a rule to ignore all other rules for a destination that 164 // Create a rule to ignore all other rules for a destination that
189 // contains index.html. 165 // contains index.html.
190 linked_ptr<RulesRegistry::Rule> CreateIgnoreRule() { 166 linked_ptr<RulesRegistry::Rule> CreateIgnoreRule() {
191 linked_ptr<json_schema_compiler::any::Any> condition(
192 new json_schema_compiler::any::Any);
193 DictionaryValue condition_dict; 167 DictionaryValue condition_dict;
194 DictionaryValue* http_condition_dict = new DictionaryValue(); 168 DictionaryValue* http_condition_dict = new DictionaryValue();
195 http_condition_dict->SetString(keys2::kPathContainsKey, "index.html"); 169 http_condition_dict->SetString(keys2::kPathContainsKey, "index.html");
196 condition_dict.SetString(keys::kInstanceTypeKey, keys::kRequestMatcherType); 170 condition_dict.SetString(keys::kInstanceTypeKey, keys::kRequestMatcherType);
197 condition_dict.Set(keys::kUrlKey, http_condition_dict); 171 condition_dict.Set(keys::kUrlKey, http_condition_dict);
198 condition->Init(condition_dict);
199 172
200 DictionaryValue action_dict; 173 DictionaryValue action_dict;
201 action_dict.SetString(keys::kInstanceTypeKey, keys::kIgnoreRulesType); 174 action_dict.SetString(keys::kInstanceTypeKey, keys::kIgnoreRulesType);
202 action_dict.SetInteger(keys::kLowerPriorityThanKey, 150); 175 action_dict.SetInteger(keys::kLowerPriorityThanKey, 150);
203 linked_ptr<json_schema_compiler::any::Any> action(
204 new json_schema_compiler::any::Any);
205 action->Init(action_dict);
206 176
207 linked_ptr<RulesRegistry::Rule> rule(new RulesRegistry::Rule); 177 linked_ptr<RulesRegistry::Rule> rule(new RulesRegistry::Rule);
208 rule->id.reset(new std::string(kRuleId4)); 178 rule->id.reset(new std::string(kRuleId4));
209 rule->priority.reset(new int(200)); 179 rule->priority.reset(new int(200));
210 rule->actions.push_back(action); 180 rule->actions.push_back(linked_ptr<base::Value>(action_dict.DeepCopy()));
211 rule->conditions.push_back(condition); 181 rule->conditions.push_back(
182 linked_ptr<base::Value>(condition_dict.DeepCopy()));
212 return rule; 183 return rule;
213 } 184 }
214 185
215 // Create a condition with the attributes specified. An example value of a 186 // Create a condition with the attributes specified. An example value of a
216 // string from |attributes| is: "\"resourceType\": [\"stylesheet\"], \n". 187 // string from |attributes| is: "\"resourceType\": [\"stylesheet\"], \n".
217 linked_ptr<json_schema_compiler::any::Any> CreateCondition( 188 linked_ptr<base::Value> CreateCondition(
218 const std::vector<const char *>& attributes) { 189 const std::vector<const char *>& attributes) {
219 linked_ptr<json_schema_compiler::any::Any> condition(
220 new json_schema_compiler::any::Any);
221
222 // Starting boilerplate. 190 // Starting boilerplate.
223 std::string json_description = 191 std::string json_description =
224 "{ \n" 192 "{ \n"
225 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n"; 193 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n";
226 for (size_t i = 0; i < attributes.size(); ++i) { 194 for (size_t i = 0; i < attributes.size(); ++i) {
227 json_description += attributes[i]; 195 json_description += attributes[i];
228 } 196 }
229 // Ending boilerplate. 197 // Ending boilerplate.
230 json_description += "}"; 198 json_description += "}";
231 condition->Init(*base::test::ParseJson(json_description));
232 199
233 return condition; 200 return linked_ptr<base::Value>(
201 base::test::ParseJson(json_description).release());
234 } 202 }
235 203
236 // Create a rule with the ID |rule_id| and with a single condition and a 204 // Create a rule with the ID |rule_id| and with a single condition and a
237 // cancelling action. The condition contains a non-matching non-URL attribute, 205 // cancelling action. The condition contains a non-matching non-URL attribute,
238 // and optionally, according to |with_url_attribute| also a URL attribute 206 // and optionally, according to |with_url_attribute| also a URL attribute
239 // matching any (!) URL. 207 // matching any (!) URL.
240 linked_ptr<RulesRegistry::Rule> CreateNonMatchingRule( 208 linked_ptr<RulesRegistry::Rule> CreateNonMatchingRule(
241 bool with_url_attribute, 209 bool with_url_attribute,
242 const char* rule_id) { 210 const char* rule_id) {
243 std::vector<const char*> attributes; 211 std::vector<const char*> attributes;
244 if (with_url_attribute) 212 if (with_url_attribute)
245 attributes.push_back("\"url\": { \"pathContains\": \"\" }, \n"); 213 attributes.push_back("\"url\": { \"pathContains\": \"\" }, \n");
246 214
247 // The following attribute never matches in this unit test. 215 // The following attribute never matches in this unit test.
248 attributes.push_back("\"resourceType\": [\"stylesheet\"], \n"); 216 attributes.push_back("\"resourceType\": [\"stylesheet\"], \n");
249 217
250 linked_ptr<json_schema_compiler::any::Any> condition =
251 CreateCondition(attributes);
252
253 DictionaryValue action_dict; 218 DictionaryValue action_dict;
254 action_dict.SetString(keys::kInstanceTypeKey, keys::kCancelRequestType); 219 action_dict.SetString(keys::kInstanceTypeKey, keys::kCancelRequestType);
255 linked_ptr<json_schema_compiler::any::Any> action(
256 new json_schema_compiler::any::Any);
257 action->Init(action_dict);
258 220
259 linked_ptr<RulesRegistry::Rule> rule(new RulesRegistry::Rule); 221 linked_ptr<RulesRegistry::Rule> rule(new RulesRegistry::Rule);
260 rule->id.reset(new std::string(rule_id)); 222 rule->id.reset(new std::string(rule_id));
261 rule->priority.reset(new int(1)); 223 rule->priority.reset(new int(1));
262 rule->actions.push_back(action); 224 rule->actions.push_back(linked_ptr<base::Value>(action_dict.DeepCopy()));
263 rule->conditions.push_back(condition); 225 rule->conditions.push_back(CreateCondition(attributes));
264 return rule; 226 return rule;
265 } 227 }
266 228
267 // Create a rule with the ID |rule_id| and with two conditions and a 229 // Create a rule with the ID |rule_id| and with two conditions and a
268 // cancelling action. One condition contains a non-matching non-URL attribute, 230 // cancelling action. One condition contains a non-matching non-URL attribute,
269 // and the other one a URL attribute matching any URL. 231 // and the other one a URL attribute matching any URL.
270 linked_ptr<RulesRegistry::Rule> CreateRuleWithTwoConditions( 232 linked_ptr<RulesRegistry::Rule> CreateRuleWithTwoConditions(
271 const char* rule_id) { 233 const char* rule_id) {
272 std::vector<const char*> attributes; 234 std::vector<const char*> url_attributes;
273 attributes.push_back("\"url\": { \"pathContains\": \"\" }, \n"); 235 url_attributes.push_back("\"url\": { \"pathContains\": \"\" }, \n");
274 linked_ptr<json_schema_compiler::any::Any> url_condition =
275 CreateCondition(attributes);
276 236
277 attributes.clear();
278 // The following attribute never matches in this unit test. 237 // The following attribute never matches in this unit test.
279 attributes.push_back("\"resourceType\": [\"stylesheet\"], \n"); 238 std::vector<const char*> non_matching_attributes;
280 linked_ptr<json_schema_compiler::any::Any> non_matching_condition = 239 non_matching_attributes.push_back("\"resourceType\": [\"stylesheet\"], \n");
281 CreateCondition(attributes);
282 240
283 DictionaryValue action_dict; 241 DictionaryValue action_dict;
284 action_dict.SetString(keys::kInstanceTypeKey, keys::kCancelRequestType); 242 action_dict.SetString(keys::kInstanceTypeKey, keys::kCancelRequestType);
285 linked_ptr<json_schema_compiler::any::Any> action(
286 new json_schema_compiler::any::Any);
287 action->Init(action_dict);
288 243
289 linked_ptr<RulesRegistry::Rule> rule(new RulesRegistry::Rule); 244 linked_ptr<RulesRegistry::Rule> rule(new RulesRegistry::Rule);
290 rule->id.reset(new std::string(rule_id)); 245 rule->id.reset(new std::string(rule_id));
291 rule->priority.reset(new int(1)); 246 rule->priority.reset(new int(1));
292 rule->actions.push_back(action); 247 rule->actions.push_back(linked_ptr<base::Value>(action_dict.DeepCopy()));
293 rule->conditions.push_back(url_condition); 248 rule->conditions.push_back(CreateCondition(url_attributes));
294 rule->conditions.push_back(non_matching_condition); 249 rule->conditions.push_back(CreateCondition(non_matching_attributes));
295 return rule; 250 return rule;
296 } 251 }
297 252
298 protected: 253 protected:
299 MessageLoop message_loop; 254 MessageLoop message_loop;
300 content::TestBrowserThread ui; 255 content::TestBrowserThread ui;
301 content::TestBrowserThread io; 256 content::TestBrowserThread io;
302 }; 257 };
303 258
304 TEST_F(WebRequestRulesRegistryTest, AddRulesImpl) { 259 TEST_F(WebRequestRulesRegistryTest, AddRulesImpl) {
(...skipping 289 matching lines...) Expand 10 before | Expand all | Expand 10 after
594 new TestWebRequestRulesRegistry()); 549 new TestWebRequestRulesRegistry());
595 550
596 URLMatcher matcher; 551 URLMatcher matcher;
597 std::string error = registry->AddRulesImpl(kExtensionId, rules); 552 std::string error = registry->AddRulesImpl(kExtensionId, rules);
598 EXPECT_THAT(error, HasSubstr("no time in the request life-cycle")); 553 EXPECT_THAT(error, HasSubstr("no time in the request life-cycle"));
599 EXPECT_TRUE(registry->IsEmpty()); 554 EXPECT_TRUE(registry->IsEmpty());
600 } 555 }
601 556
602 557
603 } // namespace extensions 558 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698