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

Side by Side Diff: chrome/browser/extensions/api/declarative/declarative_rule_unittest.cc

Issue 11414230: Declarative Web Request: firstPartyForCookiesUrl (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: More checks and one more unit test 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) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 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/declarative_rule.h" 5 #include "chrome/browser/extensions/api/declarative/declarative_rule.h"
6 6
7 #include "base/basictypes.h"
7 #include "base/message_loop.h" 8 #include "base/message_loop.h"
8 #include "base/test/values_test_util.h" 9 #include "base/test/values_test_util.h"
9 #include "base/values.h" 10 #include "base/values.h"
10 #include "chrome/common/extensions/matcher/url_matcher_constants.h" 11 #include "chrome/common/extensions/matcher/url_matcher_constants.h"
11 #include "testing/gmock/include/gmock/gmock.h" 12 #include "testing/gmock/include/gmock/gmock.h"
12 #include "testing/gtest/include/gtest/gtest.h" 13 #include "testing/gtest/include/gtest/gtest.h"
13 14
14 namespace extensions { 15 namespace extensions {
15 16
16 using base::test::ParseJson; 17 using base::test::ParseJson;
17 18
18 namespace { 19 namespace {
19 20
20 template<typename T> 21 template<typename T>
21 linked_ptr<T> ScopedToLinkedPtr(scoped_ptr<T> ptr) { 22 linked_ptr<T> ScopedToLinkedPtr(scoped_ptr<T> ptr) {
22 return linked_ptr<T>(ptr.release()); 23 return linked_ptr<T>(ptr.release());
23 } 24 }
24 25
25 } // namespace 26 } // namespace
26 27
28 struct MultipleFactoriesCondition {
vabr (Chromium) 2013/01/22 08:34:53 I don't think that the layout of this struct confo
29 typedef int MatchData;
30
31 static const char* kKey;
32
33 int number_of_factories_to_consider;
34
35 int GetURLMatcherConditionSets(
36 URLMatcherConditionSet::Vector* /*condition_sets*/,
37 int index) const {
38 if (0 <= index && index < number_of_factories_to_consider)
39 return index + 1;
40 return -1;
41 }
42
43 static scoped_ptr<MultipleFactoriesCondition> Create(
44 const std::vector<URLMatcherConditionFactory*>& /*factories*/,
45 const base::Value& condition,
46 std::string* error) {
47 const base::DictionaryValue* dict = NULL;
48 int value;
49 if (!condition.GetAsDictionary(&dict) || !dict->HasKey(kKey) ||
50 !dict->GetInteger(kKey, &value)) {
51 *error = "Invalid condition description.";
52 return scoped_ptr<MultipleFactoriesCondition>();
53 }
54
55 scoped_ptr<MultipleFactoriesCondition> result(
56 new MultipleFactoriesCondition());
57 result->number_of_factories_to_consider = value;
58 return result.Pass();
59 }
60 };
61 const char* MultipleFactoriesCondition::kKey = "numberOfFactoriesToConsider";
62 typedef DeclarativeConditionSet<MultipleFactoriesCondition>
63 MultipleFactoriesConditionSet;
64
65 namespace {
66
67 void TestCorrectIndexHandling(int number_of_factories) {
68 MultipleFactoriesConditionSet::AnyVector conditions;
69 for (int i = 0; i <= number_of_factories; ++i) {
70 scoped_ptr<DictionaryValue> dict(new DictionaryValue);
71 dict->SetInteger(MultipleFactoriesCondition::kKey, i);
72 conditions.push_back(ScopedToLinkedPtr(dict.Pass()));
73 }
74 std::string error;
75 std::vector<URLMatcherConditionFactory*> null_factories(number_of_factories);
76 scoped_ptr<MultipleFactoriesConditionSet> result =
77 MultipleFactoriesConditionSet::Create(null_factories, conditions, &error);
78 EXPECT_TRUE(error.empty());
79 ASSERT_TRUE(result);
80
81 for (int i = 0; i < number_of_factories; ++i) {
82 EXPECT_EQ(i + 1, result->GetURLMatcherConditionSets(NULL, i));
83 }
84 EXPECT_EQ(-1, result->GetURLMatcherConditionSets(NULL, number_of_factories));
85 }
86
87 } // namespace
88
89 TEST(DeclarativeConditionTest, GetURLMatcherConditionSets) {
90 static const int numbers_of_factories[] = { 0, 1, 2, 5, 20 };
91 for (size_t i = 0; i < arraysize(numbers_of_factories); ++i) {
92 TestCorrectIndexHandling(numbers_of_factories[i]);
93 }
94 }
95
27 struct RecordingCondition { 96 struct RecordingCondition {
28 typedef int MatchData; 97 typedef int MatchData;
29 98
30 URLMatcherConditionFactory* factory; 99 std::vector<URLMatcherConditionFactory*> factories;
31 scoped_ptr<base::Value> value; 100 scoped_ptr<base::Value> value;
32 101
33 void GetURLMatcherConditionSets( 102 int GetURLMatcherConditionSets(
34 URLMatcherConditionSet::Vector* condition_sets) const { 103 URLMatcherConditionSet::Vector* condition_sets,
104 int index) const {
35 // No condition sets. 105 // No condition sets.
36 } 106 return -1;
37
38 bool has_url_matcher_condition_set() const {
39 return false;
40 } 107 }
41 108
42 static scoped_ptr<RecordingCondition> Create( 109 static scoped_ptr<RecordingCondition> Create(
43 URLMatcherConditionFactory* url_matcher_condition_factory, 110 const std::vector<URLMatcherConditionFactory*>&
111 url_matcher_condition_factories,
44 const base::Value& condition, 112 const base::Value& condition,
45 std::string* error) { 113 std::string* error) {
46 const base::DictionaryValue* dict = NULL; 114 const base::DictionaryValue* dict = NULL;
47 if (condition.GetAsDictionary(&dict) && dict->HasKey("bad_key")) { 115 if (condition.GetAsDictionary(&dict) && dict->HasKey("bad_key")) {
48 *error = "Found error key"; 116 *error = "Found error key";
49 return scoped_ptr<RecordingCondition>(); 117 return scoped_ptr<RecordingCondition>();
50 } 118 }
51 119
52 scoped_ptr<RecordingCondition> result(new RecordingCondition()); 120 scoped_ptr<RecordingCondition> result(new RecordingCondition());
53 result->factory = url_matcher_condition_factory; 121 result->factories = url_matcher_condition_factories;
54 result->value.reset(condition.DeepCopy()); 122 result->value.reset(condition.DeepCopy());
55 return result.Pass(); 123 return result.Pass();
56 } 124 }
57 }; 125 };
58 typedef DeclarativeConditionSet<RecordingCondition> RecordingConditionSet; 126 typedef DeclarativeConditionSet<RecordingCondition> RecordingConditionSet;
59 127
60 TEST(DeclarativeConditionTest, ErrorConditionSet) { 128 TEST(DeclarativeConditionTest, ErrorConditionSet) {
61 URLMatcher matcher; 129 URLMatcher matcher;
62 RecordingConditionSet::AnyVector conditions; 130 RecordingConditionSet::AnyVector conditions;
63 conditions.push_back(ScopedToLinkedPtr(ParseJson("{\"key\": 1}"))); 131 conditions.push_back(ScopedToLinkedPtr(ParseJson("{\"key\": 1}")));
64 conditions.push_back(ScopedToLinkedPtr(ParseJson("{\"bad_key\": 2}"))); 132 conditions.push_back(ScopedToLinkedPtr(ParseJson("{\"bad_key\": 2}")));
65 133
66 std::string error; 134 std::string error;
135 std::vector<URLMatcherConditionFactory*> factories;
136 factories.push_back(matcher.condition_factory());
67 scoped_ptr<RecordingConditionSet> result = 137 scoped_ptr<RecordingConditionSet> result =
68 RecordingConditionSet::Create(matcher.condition_factory(), 138 RecordingConditionSet::Create(factories, conditions, &error);
69 conditions, &error);
70 EXPECT_EQ("Found error key", error); 139 EXPECT_EQ("Found error key", error);
71 ASSERT_FALSE(result); 140 ASSERT_FALSE(result);
72 } 141 }
73 142
74 TEST(DeclarativeConditionTest, CreateConditionSet) { 143 TEST(DeclarativeConditionTest, CreateConditionSet) {
75 URLMatcher matcher; 144 URLMatcher matcher;
76 RecordingConditionSet::AnyVector conditions; 145 RecordingConditionSet::AnyVector conditions;
77 conditions.push_back(ScopedToLinkedPtr(ParseJson("{\"key\": 1}"))); 146 conditions.push_back(ScopedToLinkedPtr(ParseJson("{\"key\": 1}")));
78 conditions.push_back(ScopedToLinkedPtr(ParseJson("[\"val1\", 2]"))); 147 conditions.push_back(ScopedToLinkedPtr(ParseJson("[\"val1\", 2]")));
79 148
80 // Test insertion 149 // Test insertion
81 std::string error; 150 std::string error;
151 std::vector<URLMatcherConditionFactory*> factories;
152 factories.push_back(matcher.condition_factory());
82 scoped_ptr<RecordingConditionSet> result = 153 scoped_ptr<RecordingConditionSet> result =
83 RecordingConditionSet::Create(matcher.condition_factory(), 154 RecordingConditionSet::Create(factories, conditions, &error);
84 conditions, &error);
85 EXPECT_EQ("", error); 155 EXPECT_EQ("", error);
86 ASSERT_TRUE(result); 156 ASSERT_TRUE(result);
87 EXPECT_EQ(2u, result->conditions().size()); 157 EXPECT_EQ(2u, result->conditions().size());
88 158
89 EXPECT_EQ(matcher.condition_factory(), result->conditions()[0]->factory); 159 EXPECT_EQ(1u, result->conditions()[0]->factories.size());
160 EXPECT_EQ(matcher.condition_factory(), result->conditions()[0]->factories[0]);
90 EXPECT_TRUE(ParseJson("{\"key\": 1}")->Equals( 161 EXPECT_TRUE(ParseJson("{\"key\": 1}")->Equals(
91 result->conditions()[0]->value.get())); 162 result->conditions()[0]->value.get()));
92 } 163 }
93 164
94 struct FulfillableCondition { 165 struct FulfillableCondition {
95 typedef int MatchData; 166 typedef int MatchData;
96 167
97 scoped_refptr<URLMatcherConditionSet> condition_set; 168 scoped_refptr<URLMatcherConditionSet> condition_set;
98 int condition_set_id; 169 int condition_set_id;
99 int max_value; 170 int max_value;
100 171
101 URLMatcherConditionSet::ID url_matcher_condition_set_id() const { 172 URLMatcherConditionSet::ID url_matcher_condition_set_id() const {
102 return condition_set_id; 173 return condition_set_id;
103 } 174 }
104 175
105 bool has_url_matcher_condition_set() const {
106 return true;
107 }
108
109 scoped_refptr<URLMatcherConditionSet> url_matcher_condition_set() const { 176 scoped_refptr<URLMatcherConditionSet> url_matcher_condition_set() const {
110 return condition_set; 177 return condition_set;
111 } 178 }
112 179
113 void GetURLMatcherConditionSets( 180 int GetURLMatcherConditionSets(
114 URLMatcherConditionSet::Vector* condition_sets) const { 181 URLMatcherConditionSet::Vector* condition_sets,
115 if (condition_set) 182 int index) const {
183 if (index == 0 && condition_set)
116 condition_sets->push_back(condition_set); 184 condition_sets->push_back(condition_set);
185 return -1;
117 } 186 }
118 187
119 bool IsFulfilled(const std::set<URLMatcherConditionSet::ID>& url_matches, 188 bool IsFulfilled(const std::set<URLMatcherConditionSet::ID>& url_matches,
120 int match_data) const { 189 int match_data) const {
121 if (condition_set_id != -1 && !ContainsKey(url_matches, condition_set_id)) 190 if (condition_set_id != -1 && !ContainsKey(url_matches, condition_set_id))
122 return false; 191 return false;
123 return match_data <= max_value; 192 return match_data <= max_value;
124 } 193 }
125 194
126 static scoped_ptr<FulfillableCondition> Create( 195 static scoped_ptr<FulfillableCondition> Create(
127 URLMatcherConditionFactory* url_matcher_condition_factory, 196 const std::vector<URLMatcherConditionFactory*>&
197 url_matcher_condition_factories,
128 const base::Value& condition, 198 const base::Value& condition,
129 std::string* error) { 199 std::string* error) {
130 scoped_ptr<FulfillableCondition> result(new FulfillableCondition()); 200 scoped_ptr<FulfillableCondition> result(new FulfillableCondition());
131 const base::DictionaryValue* dict; 201 const base::DictionaryValue* dict;
132 if (!condition.GetAsDictionary(&dict)) { 202 if (!condition.GetAsDictionary(&dict)) {
133 *error = "Expected dict"; 203 *error = "Expected dict";
134 return result.Pass(); 204 return result.Pass();
135 } 205 }
136 if (!dict->GetInteger("url_id", &result->condition_set_id)) 206 if (!dict->GetInteger("url_id", &result->condition_set_id))
137 result->condition_set_id = -1; 207 result->condition_set_id = -1;
(...skipping 15 matching lines...) Expand all
153 "{\"url_id\": 1, \"max\": 3}"))); 223 "{\"url_id\": 1, \"max\": 3}")));
154 conditions.push_back(ScopedToLinkedPtr(ParseJson( 224 conditions.push_back(ScopedToLinkedPtr(ParseJson(
155 "{\"url_id\": 2, \"max\": 5}"))); 225 "{\"url_id\": 2, \"max\": 5}")));
156 conditions.push_back(ScopedToLinkedPtr(ParseJson( 226 conditions.push_back(ScopedToLinkedPtr(ParseJson(
157 "{\"url_id\": 3, \"max\": 1}"))); 227 "{\"url_id\": 3, \"max\": 1}")));
158 conditions.push_back(ScopedToLinkedPtr(ParseJson( 228 conditions.push_back(ScopedToLinkedPtr(ParseJson(
159 "{\"max\": -5}"))); // No url. 229 "{\"max\": -5}"))); // No url.
160 230
161 // Test insertion 231 // Test insertion
162 std::string error; 232 std::string error;
233 std::vector<URLMatcherConditionFactory*> factories;
163 scoped_ptr<FulfillableConditionSet> result = 234 scoped_ptr<FulfillableConditionSet> result =
164 FulfillableConditionSet::Create(NULL, conditions, &error); 235 FulfillableConditionSet::Create(factories, conditions, &error);
165 ASSERT_EQ("", error); 236 ASSERT_EQ("", error);
166 ASSERT_TRUE(result); 237 ASSERT_TRUE(result);
167 EXPECT_EQ(4u, result->conditions().size()); 238 EXPECT_EQ(4u, result->conditions().size());
168 239
169 std::set<URLMatcherConditionSet::ID> url_matches; 240 std::set<URLMatcherConditionSet::ID> url_matches;
170 EXPECT_FALSE(result->IsFulfilled(1, url_matches, 0)) 241 EXPECT_FALSE(result->IsFulfilled(1, url_matches, 0))
171 << "Testing an ID that's not in url_matches forwards to the Condition, " 242 << "Testing an ID that's not in url_matches forwards to the Condition, "
172 << "which doesn't match."; 243 << "which doesn't match.";
173 EXPECT_FALSE(result->IsFulfilled(-1, url_matches, 0)) 244 EXPECT_FALSE(result->IsFulfilled(-1, url_matches, 0))
174 << "Testing the 'no ID' value tries to match the 4th condition, but " 245 << "Testing the 'no ID' value tries to match the 4th condition, but "
175 << "its max is too low."; 246 << "its max is too low.";
176 EXPECT_TRUE(result->IsFulfilled(-1, url_matches, -5)) 247 EXPECT_TRUE(result->IsFulfilled(-1, url_matches, -5))
177 << "Testing the 'no ID' value tries to match the 4th condition, and " 248 << "Testing the 'no ID' value tries to match the 4th condition, and "
178 << "this value is low enough."; 249 << "this value is low enough.";
179 250
180 url_matches.insert(1); 251 url_matches.insert(1);
181 EXPECT_TRUE(result->IsFulfilled(1, url_matches, 3)) 252 EXPECT_TRUE(result->IsFulfilled(1, url_matches, 3))
182 << "Tests a condition with a url matcher, for a matching value."; 253 << "Tests a condition with a url matcher, for a matching value.";
183 EXPECT_FALSE(result->IsFulfilled(1, url_matches, 4)) 254 EXPECT_FALSE(result->IsFulfilled(1, url_matches, 4))
184 << "Tests a condition with a url matcher, for a non-matching value " 255 << "Tests a condition with a url matcher, for a non-matching value "
185 << "that would match a different condition."; 256 << "that would match a different condition.";
186 url_matches.insert(2); 257 url_matches.insert(2);
187 EXPECT_TRUE(result->IsFulfilled(2, url_matches, 4)) 258 EXPECT_TRUE(result->IsFulfilled(2, url_matches, 4))
188 << "Tests with 2 elements in the match set."; 259 << "Tests with 2 elements in the match set.";
189 260
190 // Check the condition sets: 261 // Check the condition sets:
191 URLMatcherConditionSet::Vector condition_sets; 262 URLMatcherConditionSet::Vector condition_sets;
192 result->GetURLMatcherConditionSets(&condition_sets); 263 result->GetURLMatcherConditionSets(&condition_sets, 0);
193 ASSERT_EQ(3U, condition_sets.size()); 264 ASSERT_EQ(3U, condition_sets.size());
194 EXPECT_EQ(1, condition_sets[0]->id()); 265 EXPECT_EQ(1, condition_sets[0]->id());
195 EXPECT_EQ(2, condition_sets[1]->id()); 266 EXPECT_EQ(2, condition_sets[1]->id());
196 EXPECT_EQ(3, condition_sets[2]->id()); 267 EXPECT_EQ(3, condition_sets[2]->id());
197 } 268 }
198 269
199 // DeclarativeAction 270 // DeclarativeAction
200 271
201 struct SummingAction { 272 struct SummingAction {
202 typedef int ApplyInfo; 273 typedef int ApplyInfo;
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
298 " ], \n" 369 " ], \n"
299 " \"priority\": 200 \n" 370 " \"priority\": 200 \n"
300 "}"), 371 "}"),
301 json_rule.get())); 372 json_rule.get()));
302 373
303 const char kExtensionId[] = "ext1"; 374 const char kExtensionId[] = "ext1";
304 375
305 base::Time install_time = base::Time::Now(); 376 base::Time install_time = base::Time::Now();
306 377
307 URLMatcher matcher; 378 URLMatcher matcher;
379 std::vector<URLMatcherConditionFactory*> factories;
380 factories.push_back(matcher.condition_factory());
308 std::string error; 381 std::string error;
309 scoped_ptr<Rule> rule(Rule::Create(matcher.condition_factory(), kExtensionId, 382 scoped_ptr<Rule> rule(Rule::Create(factories, kExtensionId,
310 install_time, json_rule, NULL, &error)); 383 install_time, json_rule, NULL, &error));
311 EXPECT_EQ("", error); 384 EXPECT_EQ("", error);
312 ASSERT_TRUE(rule.get()); 385 ASSERT_TRUE(rule.get());
313 386
314 EXPECT_EQ(kExtensionId, rule->id().first); 387 EXPECT_EQ(kExtensionId, rule->id().first);
315 EXPECT_EQ("rule1", rule->id().second); 388 EXPECT_EQ("rule1", rule->id().second);
316 389
317 EXPECT_EQ(200, rule->priority()); 390 EXPECT_EQ(200, rule->priority());
318 391
319 const Rule::ConditionSet& condition_set = rule->conditions(); 392 const Rule::ConditionSet& condition_set = rule->conditions();
(...skipping 23 matching lines...) Expand all
343 } 416 }
344 return true; 417 return true;
345 } 418 }
346 419
347 TEST(DeclarativeRuleTest, CheckConsistency) { 420 TEST(DeclarativeRuleTest, CheckConsistency) {
348 typedef DeclarativeRule<FulfillableCondition, SummingAction> Rule; 421 typedef DeclarativeRule<FulfillableCondition, SummingAction> Rule;
349 URLMatcher matcher; 422 URLMatcher matcher;
350 std::string error; 423 std::string error;
351 linked_ptr<Rule::JsonRule> json_rule(new Rule::JsonRule); 424 linked_ptr<Rule::JsonRule> json_rule(new Rule::JsonRule);
352 const char kExtensionId[] = "ext1"; 425 const char kExtensionId[] = "ext1";
426 std::vector<URLMatcherConditionFactory*> factories;
427 factories.push_back(matcher.condition_factory());
353 428
354 ASSERT_TRUE(Rule::JsonRule::Populate( 429 ASSERT_TRUE(Rule::JsonRule::Populate(
355 *ParseJson("{ \n" 430 *ParseJson("{ \n"
356 " \"id\": \"rule1\", \n" 431 " \"id\": \"rule1\", \n"
357 " \"conditions\": [ \n" 432 " \"conditions\": [ \n"
358 " {\"url_id\": 1, \"max\": 3}, \n" 433 " {\"url_id\": 1, \"max\": 3}, \n"
359 " {\"url_id\": 2, \"max\": 5}, \n" 434 " {\"url_id\": 2, \"max\": 5}, \n"
360 " ], \n" 435 " ], \n"
361 " \"actions\": [ \n" 436 " \"actions\": [ \n"
362 " { \n" 437 " { \n"
363 " \"value\": 2 \n" 438 " \"value\": 2 \n"
364 " } \n" 439 " } \n"
365 " ], \n" 440 " ], \n"
366 " \"priority\": 200 \n" 441 " \"priority\": 200 \n"
367 "}"), 442 "}"),
368 json_rule.get())); 443 json_rule.get()));
369 scoped_ptr<Rule> rule( 444 scoped_ptr<Rule> rule(Rule::Create(factories, kExtensionId, base::Time(),
370 Rule::Create(matcher.condition_factory(), kExtensionId, base::Time(), 445 json_rule, &AtLeastOneCondition, &error));
371 json_rule, &AtLeastOneCondition, &error));
372 EXPECT_TRUE(rule); 446 EXPECT_TRUE(rule);
373 EXPECT_EQ("", error); 447 EXPECT_EQ("", error);
374 448
375 ASSERT_TRUE(Rule::JsonRule::Populate( 449 ASSERT_TRUE(Rule::JsonRule::Populate(
376 *ParseJson("{ \n" 450 *ParseJson("{ \n"
377 " \"id\": \"rule1\", \n" 451 " \"id\": \"rule1\", \n"
378 " \"conditions\": [ \n" 452 " \"conditions\": [ \n"
379 " ], \n" 453 " ], \n"
380 " \"actions\": [ \n" 454 " \"actions\": [ \n"
381 " { \n" 455 " { \n"
382 " \"value\": 2 \n" 456 " \"value\": 2 \n"
383 " } \n" 457 " } \n"
384 " ], \n" 458 " ], \n"
385 " \"priority\": 200 \n" 459 " \"priority\": 200 \n"
386 "}"), 460 "}"),
387 json_rule.get())); 461 json_rule.get()));
388 rule = Rule::Create(matcher.condition_factory(), kExtensionId, base::Time(), 462 rule = Rule::Create(factories, kExtensionId, base::Time(),
389 json_rule, &AtLeastOneCondition, &error); 463 json_rule, &AtLeastOneCondition, &error);
390 EXPECT_FALSE(rule); 464 EXPECT_FALSE(rule);
391 EXPECT_EQ("No conditions", error); 465 EXPECT_EQ("No conditions", error);
392 } 466 }
393 467
394 } // namespace extensions 468 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698