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

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

Issue 9390018: Implementation of a Matching strategy for URLs in the Declarative WebRequest API. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: MSVC does not support EXPECT_NE on iterators Created 8 years, 10 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/url_matcher.cc ('k') | chrome/chrome_browser.gypi » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/extensions/api/declarative/url_matcher.h"
6
7 #include "base/string_util.h"
8 #include "googleurl/src/gurl.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10
11 namespace extensions {
12
13 //
14 // URLMatcherCondition
15 //
16
17 TEST(URLMatcherConditionTest, Constructors) {
18 SubstringPattern pattern("example.com", 1);
19 URLMatcherCondition m1(URLMatcherCondition::HOST_SUFFIX, &pattern);
20 EXPECT_EQ(URLMatcherCondition::HOST_SUFFIX, m1.criterion());
21 EXPECT_EQ(&pattern, m1.substring_pattern());
22
23 URLMatcherCondition m2;
24 m2 = m1;
25 EXPECT_EQ(URLMatcherCondition::HOST_SUFFIX, m2.criterion());
26 EXPECT_EQ(&pattern, m2.substring_pattern());
27
28 URLMatcherCondition m3(m1);
29 EXPECT_EQ(URLMatcherCondition::HOST_SUFFIX, m3.criterion());
30 EXPECT_EQ(&pattern, m3.substring_pattern());
31 }
32
33 TEST(URLMatcherConditionTest, IsFullURLCondition) {
34 SubstringPattern pattern("example.com", 1);
35 EXPECT_FALSE(URLMatcherCondition(URLMatcherCondition::HOST_SUFFIX,
36 &pattern).IsFullURLCondition());
37
38 EXPECT_TRUE(URLMatcherCondition(URLMatcherCondition::HOST_CONTAINS,
39 &pattern).IsFullURLCondition());
40 EXPECT_TRUE(URLMatcherCondition(URLMatcherCondition::PATH_CONTAINS,
41 &pattern).IsFullURLCondition());
42 EXPECT_TRUE(URLMatcherCondition(URLMatcherCondition::QUERY_CONTAINS,
43 &pattern).IsFullURLCondition());
44
45 EXPECT_TRUE(URLMatcherCondition(URLMatcherCondition::URL_PREFIX,
46 &pattern).IsFullURLCondition());
47 EXPECT_TRUE(URLMatcherCondition(URLMatcherCondition::URL_SUFFIX,
48 &pattern).IsFullURLCondition());
49 EXPECT_TRUE(URLMatcherCondition(URLMatcherCondition::URL_CONTAINS,
50 &pattern).IsFullURLCondition());
51 EXPECT_TRUE(URLMatcherCondition(URLMatcherCondition::URL_EQUALS,
52 &pattern).IsFullURLCondition());
53 }
54
55 TEST(URLMatcherConditionTest, IsMatch) {
56 GURL url1("http://www.example.com/www.foobar.com/index.html");
57 GURL url2("http://www.foobar.com/example.com/index.html");
58
59 SubstringPattern pattern("example.com", 1);
60 URLMatcherCondition m1(URLMatcherCondition::HOST_SUFFIX, &pattern);
61
62 std::set<SubstringPattern::ID> matching_substring_patterns;
63
64 // matches = {0} --> matcher did not indicate that m1 was a match.
65 matching_substring_patterns.insert(0);
66 EXPECT_FALSE(m1.IsMatch(matching_substring_patterns, url1));
67
68 // matches = {0, 1} --> matcher did indicate that m1 was a match.
69 matching_substring_patterns.insert(1);
70 EXPECT_TRUE(m1.IsMatch(matching_substring_patterns, url1));
71
72 // For m2 we use a HOST_CONTAINS test, which requires a post-validation
73 // whether the match reported by the SubstringSetMatcher occurs really
74 // in the correct url component.
75 URLMatcherCondition m2(URLMatcherCondition::HOST_CONTAINS, &pattern);
76 EXPECT_TRUE(m2.IsMatch(matching_substring_patterns, url1));
77 EXPECT_FALSE(m2.IsMatch(matching_substring_patterns, url2));
78 }
79
80 TEST(URLMatcherConditionTest, Comparison) {
81 SubstringPattern p1("foobar.com", 1);
82 SubstringPattern p2("foobar.com", 2);
83 // The first component of each test is expected to be < than the second.
84 URLMatcherCondition test_smaller[][2] = {
85 {URLMatcherCondition(URLMatcherCondition::HOST_PREFIX, &p1),
86 URLMatcherCondition(URLMatcherCondition::HOST_SUFFIX, &p1)},
87 {URLMatcherCondition(URLMatcherCondition::HOST_PREFIX, &p1),
88 URLMatcherCondition(URLMatcherCondition::HOST_PREFIX, &p2)},
89 {URLMatcherCondition(URLMatcherCondition::HOST_PREFIX, NULL),
90 URLMatcherCondition(URLMatcherCondition::HOST_PREFIX, &p2)},
91 {URLMatcherCondition(URLMatcherCondition::HOST_PREFIX, &p1),
92 URLMatcherCondition(URLMatcherCondition::HOST_SUFFIX, NULL)},
93 };
94 for (size_t i = 0; i < arraysize(test_smaller); ++i) {
95 EXPECT_TRUE(test_smaller[i][0] < test_smaller[i][1])
96 << "Test " << i << " of test_smaller failed";
97 EXPECT_FALSE(test_smaller[i][1] < test_smaller[i][0])
98 << "Test " << i << " of test_smaller failed";
99 }
100 URLMatcherCondition test_equal[][2] = {
101 {URLMatcherCondition(URLMatcherCondition::HOST_PREFIX, &p1),
102 URLMatcherCondition(URLMatcherCondition::HOST_PREFIX, &p1)},
103 {URLMatcherCondition(URLMatcherCondition::HOST_PREFIX, NULL),
104 URLMatcherCondition(URLMatcherCondition::HOST_PREFIX, NULL)},
105 };
106 for (size_t i = 0; i < arraysize(test_equal); ++i) {
107 EXPECT_FALSE(test_equal[i][0] < test_equal[i][1])
108 << "Test " << i << " of test_equal failed";
109 EXPECT_FALSE(test_equal[i][1] < test_equal[i][0])
110 << "Test " << i << " of test_equal failed";
111 }
112 }
113
114 //
115 // URLMatcherConditionFactory
116 //
117
118 namespace {
119
120 bool Matches(const URLMatcherCondition& condition, std::string text) {
121 return text.find(condition.substring_pattern()->pattern()) !=
122 std::string::npos;
123 }
124
125 } // namespace
126
127 TEST(URLMatcherConditionFactoryTest, GURLCharacterSet) {
128 // GURL guarantees that neither domain, nor path, nor query may contain
129 // non ASCII-7 characters. We test this here, because a change to this
130 // guarantee breaks this implementation horribly.
131 GURL url("http://www.föö.com/föö?föö#föö");
132 EXPECT_TRUE(IsStringASCII(url.host()));
133 EXPECT_TRUE(IsStringASCII(url.path()));
134 EXPECT_TRUE(IsStringASCII(url.query()));
135 EXPECT_FALSE(IsStringASCII(url.ref()));
136 }
137
138 TEST(URLMatcherConditionFactoryTest, TestSingletonProperty) {
139 URLMatcherConditionFactory factory;
140 URLMatcherCondition c1 = factory.CreateHostEqualsCondition("www.google.com");
141 URLMatcherCondition c2 = factory.CreateHostEqualsCondition("www.google.com");
142 EXPECT_EQ(c1.criterion(), c2.criterion());
143 EXPECT_EQ(c1.substring_pattern(), c2.substring_pattern());
144 URLMatcherCondition c3 = factory.CreateHostEqualsCondition("www.google.de");
145 EXPECT_EQ(c2.criterion(), c3.criterion());
146 EXPECT_NE(c2.substring_pattern(), c3.substring_pattern());
147 EXPECT_NE(c2.substring_pattern()->id(), c3.substring_pattern()->id());
148 EXPECT_NE(c2.substring_pattern()->pattern(),
149 c3.substring_pattern()->pattern());
150
151 // Check that all SubstringPattern singletons are freed if we call
152 // ForgetUnusedPatterns.
153 SubstringPattern::ID old_id_1 = c1.substring_pattern()->id();
154 factory.ForgetUnusedPatterns(std::set<SubstringPattern::ID>());
155 URLMatcherCondition c4 = factory.CreateHostEqualsCondition("www.google.com");
156 EXPECT_NE(c1.substring_pattern(), c4.substring_pattern());
157 EXPECT_NE(old_id_1, c4.substring_pattern()->id());
158 }
159
160 TEST(URLMatcherConditionFactoryTest, TestComponentSearches) {
161 GURL gurl("https://www.google.com/webhp?sourceid=chrome-instant&ie=UTF-8"
162 "&ion=1#hl=en&output=search&sclient=psy-ab&q=chrome%20is%20awesome");
163 URLMatcherConditionFactory factory;
164 std::string url = factory.CanonicalizeURLForComponentSearches(gurl);
165
166 // Test host component.
167 EXPECT_TRUE(Matches(factory.CreateHostPrefixCondition(""), url));
168 EXPECT_TRUE(Matches(factory.CreateHostPrefixCondition("www.goog"), url));
169 EXPECT_TRUE(
170 Matches(factory.CreateHostPrefixCondition("www.google.com"), url));
171 EXPECT_TRUE(
172 Matches(factory.CreateHostPrefixCondition(".www.google.com"), url));
173 EXPECT_FALSE(Matches(factory.CreateHostPrefixCondition("google.com"), url));
174 EXPECT_FALSE(
175 Matches(factory.CreateHostPrefixCondition("www.google.com/"), url));
176 EXPECT_FALSE(Matches(factory.CreateHostPrefixCondition("webhp"), url));
177
178 EXPECT_TRUE(Matches(factory.CreateHostSuffixCondition(""), url));
179 EXPECT_TRUE(Matches(factory.CreateHostSuffixCondition("com"), url));
180 EXPECT_TRUE(Matches(factory.CreateHostSuffixCondition(".com"), url));
181 EXPECT_TRUE(
182 Matches(factory.CreateHostSuffixCondition("www.google.com"), url));
183 EXPECT_TRUE(
184 Matches(factory.CreateHostSuffixCondition(".www.google.com"), url));
185 EXPECT_FALSE(Matches(factory.CreateHostSuffixCondition("www"), url));
186 EXPECT_FALSE(
187 Matches(factory.CreateHostSuffixCondition("www.google.com/"), url));
188 EXPECT_FALSE(Matches(factory.CreateHostSuffixCondition("webhp"), url));
189
190 EXPECT_FALSE(Matches(factory.CreateHostEqualsCondition(""), url));
191 EXPECT_FALSE(Matches(factory.CreateHostEqualsCondition("www"), url));
192 EXPECT_TRUE(
193 Matches(factory.CreateHostEqualsCondition("www.google.com"), url));
194 EXPECT_FALSE(
195 Matches(factory.CreateHostEqualsCondition("www.google.com/"), url));
196
197
198 // Test path component.
199 EXPECT_TRUE(Matches(factory.CreatePathPrefixCondition(""), url));
200 EXPECT_TRUE(Matches(factory.CreatePathPrefixCondition("/web"), url));
201 EXPECT_TRUE(Matches(factory.CreatePathPrefixCondition("/webhp"), url));
202 EXPECT_FALSE(Matches(factory.CreatePathPrefixCondition("webhp"), url));
203 EXPECT_FALSE(Matches(factory.CreatePathPrefixCondition("/webhp?"), url));
204
205 EXPECT_TRUE(Matches(factory.CreatePathSuffixCondition(""), url));
206 EXPECT_TRUE(Matches(factory.CreatePathSuffixCondition("webhp"), url));
207 EXPECT_TRUE(Matches(factory.CreatePathSuffixCondition("/webhp"), url));
208 EXPECT_FALSE(Matches(factory.CreatePathSuffixCondition("/web"), url));
209 EXPECT_FALSE(Matches(factory.CreatePathSuffixCondition("/webhp?"), url));
210
211 EXPECT_TRUE(Matches(factory.CreatePathEqualsCondition("/webhp"), url));
212 EXPECT_FALSE(Matches(factory.CreatePathEqualsCondition("webhp"), url));
213 EXPECT_FALSE(Matches(factory.CreatePathEqualsCondition("/webhp?"), url));
214 EXPECT_FALSE(
215 Matches(factory.CreatePathEqualsCondition("www.google.com"), url));
216
217
218 // Test query component.
219 EXPECT_TRUE(Matches(factory.CreateQueryPrefixCondition(""), url));
220 EXPECT_TRUE(Matches(factory.CreateQueryPrefixCondition("?sourceid"), url));
221 EXPECT_FALSE(Matches(factory.CreatePathPrefixCondition("sourceid"), url));
222
223 EXPECT_TRUE(Matches(factory.CreateQuerySuffixCondition(""), url));
224 EXPECT_TRUE(Matches(factory.CreateQuerySuffixCondition("ion=1"), url));
225 EXPECT_FALSE(Matches(factory.CreatePathPrefixCondition("?sourceid"), url));
226 EXPECT_FALSE(Matches(factory.CreateQuerySuffixCondition("www"), url));
227
228 EXPECT_TRUE(Matches(factory.CreateQueryEqualsCondition(
229 "?sourceid=chrome-instant&ie=UTF-8&ion=1"), url));
230 EXPECT_FALSE(Matches(factory.CreateQueryEqualsCondition(
231 "sourceid=chrome-instant&ie=UTF-8&ion="), url));
232 EXPECT_FALSE(
233 Matches(factory.CreateQueryEqualsCondition("www.google.com"), url));
234
235
236 // Test adjacent components
237 EXPECT_TRUE(Matches(factory.CreateHostSuffixPathPrefixCondition(
238 "google.com", "/webhp"), url));
239 EXPECT_TRUE(Matches(factory.CreateHostSuffixPathPrefixCondition(
240 "", "/webhp"), url));
241 EXPECT_TRUE(Matches(factory.CreateHostSuffixPathPrefixCondition(
242 "google.com", ""), url));
243 EXPECT_FALSE(Matches(factory.CreateHostSuffixPathPrefixCondition(
244 "www", ""), url));
245 }
246
247 TEST(URLMatcherConditionFactoryTest, TestFullSearches) {
248 GURL gurl("https://www.google.com/webhp?sourceid=chrome-instant&ie=UTF-8"
249 "&ion=1#hl=en&output=search&sclient=psy-ab&q=chrome%20is%20awesome");
250 URLMatcherConditionFactory factory;
251 std::string url = factory.CanonicalizeURLForFullSearches(gurl);
252
253 EXPECT_TRUE(Matches(factory.CreateURLPrefixCondition(""), url));
254 EXPECT_TRUE(Matches(factory.CreateURLPrefixCondition("www.goog"), url));
255 EXPECT_TRUE(Matches(factory.CreateURLPrefixCondition("www.google.com"), url));
256 EXPECT_TRUE(
257 Matches(factory.CreateURLPrefixCondition(".www.google.com"), url));
258 EXPECT_TRUE(
259 Matches(factory.CreateURLPrefixCondition("www.google.com/"), url));
260 EXPECT_FALSE(Matches(factory.CreateURLPrefixCondition("webhp"), url));
261
262 EXPECT_TRUE(Matches(factory.CreateURLSuffixCondition(""), url));
263 EXPECT_TRUE(Matches(factory.CreateURLSuffixCondition("ion=1"), url));
264 EXPECT_FALSE(Matches(factory.CreateURLSuffixCondition("www"), url));
265
266 EXPECT_TRUE(Matches(factory.CreateURLContainsCondition(""), url));
267 EXPECT_TRUE(Matches(factory.CreateURLContainsCondition("www.goog"), url));
268 EXPECT_TRUE(Matches(factory.CreateURLContainsCondition(".www.goog"), url));
269 EXPECT_TRUE(Matches(factory.CreateURLContainsCondition("webhp"), url));
270 EXPECT_TRUE(Matches(factory.CreateURLContainsCondition("?"), url));
271 EXPECT_TRUE(Matches(factory.CreateURLContainsCondition("sourceid"), url));
272 EXPECT_TRUE(Matches(factory.CreateURLContainsCondition("ion=1"), url));
273 EXPECT_FALSE(Matches(factory.CreateURLContainsCondition("foobar"), url));
274 EXPECT_FALSE(Matches(factory.CreateURLContainsCondition("search"), url));
275
276 EXPECT_TRUE(Matches(factory.CreateURLEqualsCondition(
277 "www.google.com/webhp?sourceid=chrome-instant&ie=UTF-8&ion=1"), url));
278 EXPECT_FALSE(
279 Matches(factory.CreateURLEqualsCondition("www.google.com"), url));
280 }
281
282
283 //
284 // URLMatcherConditionSet
285 //
286
287 TEST(URLMatcherConditionSetTest, Constructors) {
288 URLMatcherConditionFactory factory;
289 URLMatcherCondition m1 = factory.CreateHostSuffixCondition("example.com");
290 URLMatcherCondition m2 = factory.CreatePathContainsCondition("foo");
291
292 std::set<URLMatcherCondition> conditions;
293 conditions.insert(m1);
294 conditions.insert(m2);
295
296 URLMatcherConditionSet condition_set(1, conditions);
297 EXPECT_EQ(1, condition_set.id());
298 EXPECT_EQ(2u, condition_set.conditions().size());
299
300 std::set<URLMatcherCondition> other_conditions;
301 other_conditions.insert(m1);
302 URLMatcherConditionSet condition_set2(2, other_conditions);
303 condition_set2 = condition_set;
304 EXPECT_EQ(1, condition_set2.id());
305 EXPECT_EQ(2u, condition_set2.conditions().size());
306
307 URLMatcherConditionSet condition_set3(condition_set);
308 EXPECT_EQ(1, condition_set2.id());
309 EXPECT_EQ(2u, condition_set2.conditions().size());
310 }
311
312 TEST(URLMatcherConditionSetTest, Matching) {
313 GURL url1("http://www.example.com/foo?bar=1");
314 GURL url2("http://foo.example.com/index.html");
315
316 URLMatcherConditionFactory factory;
317 URLMatcherCondition m1 = factory.CreateHostSuffixCondition("example.com");
318 URLMatcherCondition m2 = factory.CreatePathContainsCondition("foo");
319
320 std::set<URLMatcherCondition> conditions;
321 conditions.insert(m1);
322 conditions.insert(m2);
323
324 URLMatcherConditionSet condition_set(1, conditions);
325 EXPECT_EQ(1, condition_set.id());
326 EXPECT_EQ(2u, condition_set.conditions().size());
327
328 std::set<SubstringPattern::ID> matching_substring_patterns;
329 matching_substring_patterns.insert(m1.substring_pattern()->id());
330 EXPECT_FALSE(condition_set.IsMatch(matching_substring_patterns, url1));
331
332 matching_substring_patterns.insert(m2.substring_pattern()->id());
333 EXPECT_TRUE(condition_set.IsMatch(matching_substring_patterns, url1));
334 EXPECT_FALSE(condition_set.IsMatch(matching_substring_patterns, url2));
335 }
336
337
338 //
339 // URLMatcher
340 //
341
342 TEST(URLMatcherTest, FullTest) {
343 GURL url1("http://www.example.com/foo?bar=1");
344 GURL url2("http://foo.example.com/index.html");
345
346 URLMatcher matcher;
347 URLMatcherConditionFactory* factory = matcher.condition_factory();
348
349 // First insert.
350 URLMatcherConditionSet::Conditions conditions1;
351 conditions1.insert(factory->CreateHostSuffixCondition("example.com"));
352 conditions1.insert(factory->CreatePathContainsCondition("foo"));
353
354 const int kConditionSetId1 = 1;
355 std::vector<URLMatcherConditionSet> insert1;
356 insert1.push_back(URLMatcherConditionSet(kConditionSetId1, conditions1));
357 matcher.AddConditionSets(insert1);
358 EXPECT_EQ(1u, matcher.MatchURL(url1).size());
359 EXPECT_EQ(0u, matcher.MatchURL(url2).size());
360
361 // Second insert.
362 URLMatcherConditionSet::Conditions conditions2;
363 conditions2.insert(factory->CreateHostSuffixCondition("example.com"));
364
365 const int kConditionSetId2 = 2;
366 std::vector<URLMatcherConditionSet> insert2;
367 insert2.push_back(URLMatcherConditionSet(kConditionSetId2, conditions2));
368 matcher.AddConditionSets(insert2);
369 EXPECT_EQ(2u, matcher.MatchURL(url1).size());
370 EXPECT_EQ(1u, matcher.MatchURL(url2).size());
371
372 // This should be the cached singleton.
373 int patternId1 = factory->CreateHostSuffixCondition(
374 "example.com").substring_pattern()->id();
375
376 // Removal of last insert.
377 std::vector<URLMatcherConditionSet::ID> remove2;
378 remove2.push_back(kConditionSetId2);
379 matcher.RemoveConditionSets(remove2);
380 EXPECT_EQ(1u, matcher.MatchURL(url1).size());
381 EXPECT_EQ(0u, matcher.MatchURL(url2).size());
382
383 // Removal of first insert.
384 std::vector<URLMatcherConditionSet::ID> remove1;
385 remove1.push_back(kConditionSetId1);
386 matcher.RemoveConditionSets(remove1);
387 EXPECT_EQ(0u, matcher.MatchURL(url1).size());
388 EXPECT_EQ(0u, matcher.MatchURL(url2).size());
389
390 // The cached singleton in matcher.condition_factory_ should be destroyed to
391 // free memory.
392 int patternId2 = factory->CreateHostSuffixCondition(
393 "example.com").substring_pattern()->id();
394 // If patternId1 and patternId2 are different that indicates that
395 // matcher.condition_factory_ does not leak memory.
396 EXPECT_NE(patternId1, patternId2);
397 }
398
399 } // namespace extensions
OLDNEW
« no previous file with comments | « chrome/browser/extensions/api/declarative/url_matcher.cc ('k') | chrome/chrome_browser.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698