OLD | NEW |
| (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/common/extensions/matcher/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 StringPattern pattern("example.com", 1); | |
19 URLMatcherCondition m1(URLMatcherCondition::HOST_SUFFIX, &pattern); | |
20 EXPECT_EQ(URLMatcherCondition::HOST_SUFFIX, m1.criterion()); | |
21 EXPECT_EQ(&pattern, m1.string_pattern()); | |
22 | |
23 URLMatcherCondition m2; | |
24 m2 = m1; | |
25 EXPECT_EQ(URLMatcherCondition::HOST_SUFFIX, m2.criterion()); | |
26 EXPECT_EQ(&pattern, m2.string_pattern()); | |
27 | |
28 URLMatcherCondition m3(m1); | |
29 EXPECT_EQ(URLMatcherCondition::HOST_SUFFIX, m3.criterion()); | |
30 EXPECT_EQ(&pattern, m3.string_pattern()); | |
31 } | |
32 | |
33 TEST(URLMatcherSchemeFilter, TestMatching) { | |
34 URLMatcherSchemeFilter filter1("https"); | |
35 std::vector<std::string> filter2_content; | |
36 filter2_content.push_back("http"); | |
37 filter2_content.push_back("https"); | |
38 URLMatcherSchemeFilter filter2(filter2_content); | |
39 | |
40 GURL matching_url("https://www.foobar.com"); | |
41 GURL non_matching_url("http://www.foobar.com"); | |
42 EXPECT_TRUE(filter1.IsMatch(matching_url)); | |
43 EXPECT_FALSE(filter1.IsMatch(non_matching_url)); | |
44 EXPECT_TRUE(filter2.IsMatch(matching_url)); | |
45 EXPECT_TRUE(filter2.IsMatch(non_matching_url)); | |
46 } | |
47 | |
48 TEST(URLMatcherPortFilter, TestMatching) { | |
49 std::vector<URLMatcherPortFilter::Range> ranges; | |
50 ranges.push_back(URLMatcherPortFilter::CreateRange(80, 90)); | |
51 ranges.push_back(URLMatcherPortFilter::CreateRange(8080)); | |
52 URLMatcherPortFilter filter(ranges); | |
53 EXPECT_TRUE(filter.IsMatch(GURL("http://www.example.com"))); | |
54 EXPECT_TRUE(filter.IsMatch(GURL("http://www.example.com:80"))); | |
55 EXPECT_TRUE(filter.IsMatch(GURL("http://www.example.com:81"))); | |
56 EXPECT_TRUE(filter.IsMatch(GURL("http://www.example.com:90"))); | |
57 EXPECT_TRUE(filter.IsMatch(GURL("http://www.example.com:8080"))); | |
58 EXPECT_FALSE(filter.IsMatch(GURL("http://www.example.com:79"))); | |
59 EXPECT_FALSE(filter.IsMatch(GURL("http://www.example.com:91"))); | |
60 EXPECT_FALSE(filter.IsMatch(GURL("https://www.example.com"))); | |
61 } | |
62 | |
63 TEST(URLMatcherConditionTest, IsFullURLCondition) { | |
64 StringPattern pattern("example.com", 1); | |
65 EXPECT_FALSE(URLMatcherCondition(URLMatcherCondition::HOST_SUFFIX, | |
66 &pattern).IsFullURLCondition()); | |
67 | |
68 EXPECT_TRUE(URLMatcherCondition(URLMatcherCondition::HOST_CONTAINS, | |
69 &pattern).IsFullURLCondition()); | |
70 EXPECT_TRUE(URLMatcherCondition(URLMatcherCondition::PATH_CONTAINS, | |
71 &pattern).IsFullURLCondition()); | |
72 EXPECT_TRUE(URLMatcherCondition(URLMatcherCondition::QUERY_CONTAINS, | |
73 &pattern).IsFullURLCondition()); | |
74 | |
75 EXPECT_TRUE(URLMatcherCondition(URLMatcherCondition::URL_PREFIX, | |
76 &pattern).IsFullURLCondition()); | |
77 EXPECT_TRUE(URLMatcherCondition(URLMatcherCondition::URL_SUFFIX, | |
78 &pattern).IsFullURLCondition()); | |
79 EXPECT_TRUE(URLMatcherCondition(URLMatcherCondition::URL_CONTAINS, | |
80 &pattern).IsFullURLCondition()); | |
81 EXPECT_TRUE(URLMatcherCondition(URLMatcherCondition::URL_EQUALS, | |
82 &pattern).IsFullURLCondition()); | |
83 } | |
84 | |
85 TEST(URLMatcherConditionTest, IsMatch) { | |
86 GURL url1("http://www.example.com/www.foobar.com/index.html"); | |
87 GURL url2("http://www.foobar.com/example.com/index.html"); | |
88 | |
89 StringPattern pattern("example.com", 1); | |
90 URLMatcherCondition m1(URLMatcherCondition::HOST_SUFFIX, &pattern); | |
91 | |
92 std::set<StringPattern::ID> matching_patterns; | |
93 | |
94 // matches = {0} --> matcher did not indicate that m1 was a match. | |
95 matching_patterns.insert(0); | |
96 EXPECT_FALSE(m1.IsMatch(matching_patterns, url1)); | |
97 | |
98 // matches = {0, 1} --> matcher did indicate that m1 was a match. | |
99 matching_patterns.insert(1); | |
100 EXPECT_TRUE(m1.IsMatch(matching_patterns, url1)); | |
101 | |
102 // For m2 we use a HOST_CONTAINS test, which requires a post-validation | |
103 // whether the match reported by the SubstringSetMatcher occurs really | |
104 // in the correct url component. | |
105 URLMatcherCondition m2(URLMatcherCondition::HOST_CONTAINS, &pattern); | |
106 EXPECT_TRUE(m2.IsMatch(matching_patterns, url1)); | |
107 EXPECT_FALSE(m2.IsMatch(matching_patterns, url2)); | |
108 } | |
109 | |
110 TEST(URLMatcherConditionTest, Comparison) { | |
111 StringPattern p1("foobar.com", 1); | |
112 StringPattern p2("foobar.com", 2); | |
113 // The first component of each test is expected to be < than the second. | |
114 URLMatcherCondition test_smaller[][2] = { | |
115 {URLMatcherCondition(URLMatcherCondition::HOST_PREFIX, &p1), | |
116 URLMatcherCondition(URLMatcherCondition::HOST_SUFFIX, &p1)}, | |
117 {URLMatcherCondition(URLMatcherCondition::HOST_PREFIX, &p1), | |
118 URLMatcherCondition(URLMatcherCondition::HOST_PREFIX, &p2)}, | |
119 {URLMatcherCondition(URLMatcherCondition::HOST_PREFIX, NULL), | |
120 URLMatcherCondition(URLMatcherCondition::HOST_PREFIX, &p2)}, | |
121 {URLMatcherCondition(URLMatcherCondition::HOST_PREFIX, &p1), | |
122 URLMatcherCondition(URLMatcherCondition::HOST_SUFFIX, NULL)}, | |
123 }; | |
124 for (size_t i = 0; i < arraysize(test_smaller); ++i) { | |
125 EXPECT_TRUE(test_smaller[i][0] < test_smaller[i][1]) | |
126 << "Test " << i << " of test_smaller failed"; | |
127 EXPECT_FALSE(test_smaller[i][1] < test_smaller[i][0]) | |
128 << "Test " << i << " of test_smaller failed"; | |
129 } | |
130 URLMatcherCondition test_equal[][2] = { | |
131 {URLMatcherCondition(URLMatcherCondition::HOST_PREFIX, &p1), | |
132 URLMatcherCondition(URLMatcherCondition::HOST_PREFIX, &p1)}, | |
133 {URLMatcherCondition(URLMatcherCondition::HOST_PREFIX, NULL), | |
134 URLMatcherCondition(URLMatcherCondition::HOST_PREFIX, NULL)}, | |
135 }; | |
136 for (size_t i = 0; i < arraysize(test_equal); ++i) { | |
137 EXPECT_FALSE(test_equal[i][0] < test_equal[i][1]) | |
138 << "Test " << i << " of test_equal failed"; | |
139 EXPECT_FALSE(test_equal[i][1] < test_equal[i][0]) | |
140 << "Test " << i << " of test_equal failed"; | |
141 } | |
142 } | |
143 | |
144 // | |
145 // URLMatcherConditionFactory | |
146 // | |
147 | |
148 namespace { | |
149 | |
150 bool Matches(const URLMatcherCondition& condition, std::string text) { | |
151 return text.find(condition.string_pattern()->pattern()) != | |
152 std::string::npos; | |
153 } | |
154 | |
155 } // namespace | |
156 | |
157 TEST(URLMatcherConditionFactoryTest, GURLCharacterSet) { | |
158 // GURL guarantees that neither domain, nor path, nor query may contain | |
159 // non ASCII-7 characters. We test this here, because a change to this | |
160 // guarantee breaks this implementation horribly. | |
161 GURL url("http://www.föö.com/föö?föö#föö"); | |
162 EXPECT_TRUE(IsStringASCII(url.host())); | |
163 EXPECT_TRUE(IsStringASCII(url.path())); | |
164 EXPECT_TRUE(IsStringASCII(url.query())); | |
165 EXPECT_FALSE(IsStringASCII(url.ref())); | |
166 } | |
167 | |
168 TEST(URLMatcherConditionFactoryTest, Criteria) { | |
169 URLMatcherConditionFactory factory; | |
170 EXPECT_EQ(URLMatcherCondition::HOST_PREFIX, | |
171 factory.CreateHostPrefixCondition("foo").criterion()); | |
172 EXPECT_EQ(URLMatcherCondition::HOST_SUFFIX, | |
173 factory.CreateHostSuffixCondition("foo").criterion()); | |
174 EXPECT_EQ(URLMatcherCondition::HOST_CONTAINS, | |
175 factory.CreateHostContainsCondition("foo").criterion()); | |
176 EXPECT_EQ(URLMatcherCondition::HOST_EQUALS, | |
177 factory.CreateHostEqualsCondition("foo").criterion()); | |
178 EXPECT_EQ(URLMatcherCondition::PATH_PREFIX, | |
179 factory.CreatePathPrefixCondition("foo").criterion()); | |
180 EXPECT_EQ(URLMatcherCondition::PATH_SUFFIX, | |
181 factory.CreatePathSuffixCondition("foo").criterion()); | |
182 EXPECT_EQ(URLMatcherCondition::PATH_CONTAINS, | |
183 factory.CreatePathContainsCondition("foo").criterion()); | |
184 EXPECT_EQ(URLMatcherCondition::PATH_EQUALS, | |
185 factory.CreatePathEqualsCondition("foo").criterion()); | |
186 EXPECT_EQ(URLMatcherCondition::QUERY_PREFIX, | |
187 factory.CreateQueryPrefixCondition("foo").criterion()); | |
188 EXPECT_EQ(URLMatcherCondition::QUERY_SUFFIX, | |
189 factory.CreateQuerySuffixCondition("foo").criterion()); | |
190 EXPECT_EQ(URLMatcherCondition::QUERY_CONTAINS, | |
191 factory.CreateQueryContainsCondition("foo").criterion()); | |
192 EXPECT_EQ(URLMatcherCondition::QUERY_EQUALS, | |
193 factory.CreateQueryEqualsCondition("foo").criterion()); | |
194 EXPECT_EQ(URLMatcherCondition::HOST_SUFFIX_PATH_PREFIX, | |
195 factory.CreateHostSuffixPathPrefixCondition("foo", | |
196 "bar").criterion()); | |
197 EXPECT_EQ(URLMatcherCondition::HOST_EQUALS_PATH_PREFIX, | |
198 factory.CreateHostEqualsPathPrefixCondition("foo", | |
199 "bar").criterion()); | |
200 EXPECT_EQ(URLMatcherCondition::URL_PREFIX, | |
201 factory.CreateURLPrefixCondition("foo").criterion()); | |
202 EXPECT_EQ(URLMatcherCondition::URL_SUFFIX, | |
203 factory.CreateURLSuffixCondition("foo").criterion()); | |
204 EXPECT_EQ(URLMatcherCondition::URL_CONTAINS, | |
205 factory.CreateURLContainsCondition("foo").criterion()); | |
206 EXPECT_EQ(URLMatcherCondition::URL_EQUALS, | |
207 factory.CreateURLEqualsCondition("foo").criterion()); | |
208 EXPECT_EQ(URLMatcherCondition::URL_MATCHES, | |
209 factory.CreateURLMatchesCondition("foo").criterion()); | |
210 } | |
211 | |
212 TEST(URLMatcherConditionFactoryTest, TestSingletonProperty) { | |
213 URLMatcherConditionFactory factory; | |
214 URLMatcherCondition c1 = factory.CreateHostEqualsCondition("www.google.com"); | |
215 URLMatcherCondition c2 = factory.CreateHostEqualsCondition("www.google.com"); | |
216 EXPECT_EQ(c1.criterion(), c2.criterion()); | |
217 EXPECT_EQ(c1.string_pattern(), c2.string_pattern()); | |
218 URLMatcherCondition c3 = factory.CreateHostEqualsCondition("www.google.de"); | |
219 EXPECT_EQ(c2.criterion(), c3.criterion()); | |
220 EXPECT_NE(c2.string_pattern(), c3.string_pattern()); | |
221 EXPECT_NE(c2.string_pattern()->id(), c3.string_pattern()->id()); | |
222 EXPECT_NE(c2.string_pattern()->pattern(), | |
223 c3.string_pattern()->pattern()); | |
224 URLMatcherCondition c4 = factory.CreateURLMatchesCondition("www.google.com"); | |
225 URLMatcherCondition c5 = factory.CreateURLContainsCondition("www.google.com"); | |
226 // Regex patterns and substring patterns do not share IDs. | |
227 EXPECT_EQ(c5.string_pattern()->pattern(), c4.string_pattern()->pattern()); | |
228 EXPECT_NE(c5.string_pattern(), c4.string_pattern()); | |
229 EXPECT_NE(c5.string_pattern()->id(), c4.string_pattern()->id()); | |
230 | |
231 // Check that all StringPattern singletons are freed if we call | |
232 // ForgetUnusedPatterns. | |
233 StringPattern::ID old_id_1 = c1.string_pattern()->id(); | |
234 StringPattern::ID old_id_4 = c4.string_pattern()->id(); | |
235 factory.ForgetUnusedPatterns(std::set<StringPattern::ID>()); | |
236 EXPECT_TRUE(factory.IsEmpty()); | |
237 URLMatcherCondition c6 = factory.CreateHostEqualsCondition("www.google.com"); | |
238 EXPECT_NE(old_id_1, c6.string_pattern()->id()); | |
239 URLMatcherCondition c7 = factory.CreateURLMatchesCondition("www.google.com"); | |
240 EXPECT_NE(old_id_4, c7.string_pattern()->id()); | |
241 } | |
242 | |
243 TEST(URLMatcherConditionFactoryTest, TestComponentSearches) { | |
244 GURL gurl("https://www.google.com:1234/webhp?sourceid=chrome-instant&ie=UTF-8" | |
245 "&ion=1#hl=en&output=search&sclient=psy-ab&q=chrome%20is%20awesome"); | |
246 URLMatcherConditionFactory factory; | |
247 std::string url = factory.CanonicalizeURLForComponentSearches(gurl); | |
248 | |
249 // Test host component. | |
250 EXPECT_TRUE(Matches(factory.CreateHostPrefixCondition(""), url)); | |
251 EXPECT_TRUE(Matches(factory.CreateHostPrefixCondition("www.goog"), url)); | |
252 EXPECT_TRUE( | |
253 Matches(factory.CreateHostPrefixCondition("www.google.com"), url)); | |
254 EXPECT_TRUE( | |
255 Matches(factory.CreateHostPrefixCondition(".www.google.com"), url)); | |
256 EXPECT_FALSE(Matches(factory.CreateHostPrefixCondition("google.com"), url)); | |
257 EXPECT_FALSE( | |
258 Matches(factory.CreateHostPrefixCondition("www.google.com/"), url)); | |
259 EXPECT_FALSE(Matches(factory.CreateHostPrefixCondition("webhp"), url)); | |
260 | |
261 EXPECT_TRUE(Matches(factory.CreateHostSuffixCondition(""), url)); | |
262 EXPECT_TRUE(Matches(factory.CreateHostSuffixCondition("com"), url)); | |
263 EXPECT_TRUE(Matches(factory.CreateHostSuffixCondition(".com"), url)); | |
264 EXPECT_TRUE( | |
265 Matches(factory.CreateHostSuffixCondition("www.google.com"), url)); | |
266 EXPECT_TRUE( | |
267 Matches(factory.CreateHostSuffixCondition(".www.google.com"), url)); | |
268 EXPECT_FALSE(Matches(factory.CreateHostSuffixCondition("www"), url)); | |
269 EXPECT_FALSE( | |
270 Matches(factory.CreateHostSuffixCondition("www.google.com/"), url)); | |
271 EXPECT_FALSE(Matches(factory.CreateHostSuffixCondition("webhp"), url)); | |
272 | |
273 EXPECT_FALSE(Matches(factory.CreateHostEqualsCondition(""), url)); | |
274 EXPECT_FALSE(Matches(factory.CreateHostEqualsCondition("www"), url)); | |
275 EXPECT_TRUE( | |
276 Matches(factory.CreateHostEqualsCondition("www.google.com"), url)); | |
277 EXPECT_FALSE( | |
278 Matches(factory.CreateHostEqualsCondition("www.google.com/"), url)); | |
279 | |
280 | |
281 // Test path component. | |
282 EXPECT_TRUE(Matches(factory.CreatePathPrefixCondition(""), url)); | |
283 EXPECT_TRUE(Matches(factory.CreatePathPrefixCondition("/web"), url)); | |
284 EXPECT_TRUE(Matches(factory.CreatePathPrefixCondition("/webhp"), url)); | |
285 EXPECT_FALSE(Matches(factory.CreatePathPrefixCondition("webhp"), url)); | |
286 EXPECT_FALSE(Matches(factory.CreatePathPrefixCondition("/webhp?"), url)); | |
287 EXPECT_FALSE(Matches(factory.CreatePathPrefixCondition("?sourceid"), url)); | |
288 | |
289 EXPECT_TRUE(Matches(factory.CreatePathSuffixCondition(""), url)); | |
290 EXPECT_TRUE(Matches(factory.CreatePathSuffixCondition("webhp"), url)); | |
291 EXPECT_TRUE(Matches(factory.CreatePathSuffixCondition("/webhp"), url)); | |
292 EXPECT_FALSE(Matches(factory.CreatePathSuffixCondition("/web"), url)); | |
293 EXPECT_FALSE(Matches(factory.CreatePathSuffixCondition("/webhp?"), url)); | |
294 | |
295 EXPECT_TRUE(Matches(factory.CreatePathEqualsCondition("/webhp"), url)); | |
296 EXPECT_FALSE(Matches(factory.CreatePathEqualsCondition("webhp"), url)); | |
297 EXPECT_FALSE(Matches(factory.CreatePathEqualsCondition("/webhp?"), url)); | |
298 EXPECT_FALSE( | |
299 Matches(factory.CreatePathEqualsCondition("www.google.com"), url)); | |
300 | |
301 | |
302 // Test query component. | |
303 EXPECT_TRUE(Matches(factory.CreateQueryPrefixCondition(""), url)); | |
304 EXPECT_TRUE(Matches(factory.CreateQueryPrefixCondition("sourceid"), url)); | |
305 // The '?' at the beginning is just ignored. | |
306 EXPECT_TRUE(Matches(factory.CreateQueryPrefixCondition("?sourceid"), url)); | |
307 | |
308 EXPECT_TRUE(Matches(factory.CreateQuerySuffixCondition(""), url)); | |
309 EXPECT_TRUE(Matches(factory.CreateQuerySuffixCondition("ion=1"), url)); | |
310 EXPECT_FALSE(Matches(factory.CreateQuerySuffixCondition("www"), url)); | |
311 // "Suffix" condition + pattern starting with '?' = "equals" condition. | |
312 EXPECT_FALSE(Matches(factory.CreateQuerySuffixCondition( | |
313 "?sourceid=chrome-instant&ie=UTF-8&ion="), url)); | |
314 EXPECT_TRUE(Matches(factory.CreateQuerySuffixCondition( | |
315 "?sourceid=chrome-instant&ie=UTF-8&ion=1"), url)); | |
316 | |
317 EXPECT_FALSE(Matches(factory.CreateQueryEqualsCondition( | |
318 "?sourceid=chrome-instant&ie=UTF-8&ion="), url)); | |
319 EXPECT_FALSE(Matches(factory.CreateQueryEqualsCondition( | |
320 "sourceid=chrome-instant&ie=UTF-8&ion="), url)); | |
321 EXPECT_TRUE(Matches(factory.CreateQueryEqualsCondition( | |
322 "sourceid=chrome-instant&ie=UTF-8&ion=1"), url)); | |
323 // The '?' at the beginning is just ignored. | |
324 EXPECT_TRUE(Matches(factory.CreateQueryEqualsCondition( | |
325 "?sourceid=chrome-instant&ie=UTF-8&ion=1"), url)); | |
326 EXPECT_FALSE( | |
327 Matches(factory.CreateQueryEqualsCondition("www.google.com"), url)); | |
328 | |
329 | |
330 // Test adjacent components | |
331 EXPECT_TRUE(Matches(factory.CreateHostSuffixPathPrefixCondition( | |
332 "google.com", "/webhp"), url)); | |
333 EXPECT_TRUE(Matches(factory.CreateHostSuffixPathPrefixCondition( | |
334 "", "/webhp"), url)); | |
335 EXPECT_TRUE(Matches(factory.CreateHostSuffixPathPrefixCondition( | |
336 "google.com", ""), url)); | |
337 EXPECT_FALSE(Matches(factory.CreateHostSuffixPathPrefixCondition( | |
338 "www", ""), url)); | |
339 | |
340 EXPECT_TRUE(Matches(factory.CreateHostEqualsPathPrefixCondition( | |
341 "www.google.com", "/webhp"), url)); | |
342 EXPECT_FALSE(Matches(factory.CreateHostEqualsPathPrefixCondition( | |
343 "", "/webhp"), url)); | |
344 EXPECT_TRUE(Matches(factory.CreateHostEqualsPathPrefixCondition( | |
345 "www.google.com", ""), url)); | |
346 EXPECT_FALSE(Matches(factory.CreateHostEqualsPathPrefixCondition( | |
347 "google.com", ""), url)); | |
348 } | |
349 | |
350 TEST(URLMatcherConditionFactoryTest, TestFullSearches) { | |
351 // The Port 443 is stripped because it is the default port for https. | |
352 GURL gurl("https://www.google.com:443/webhp?sourceid=chrome-instant&ie=UTF-8" | |
353 "&ion=1#hl=en&output=search&sclient=psy-ab&q=chrome%20is%20awesome"); | |
354 URLMatcherConditionFactory factory; | |
355 std::string url = factory.CanonicalizeURLForFullSearches(gurl); | |
356 | |
357 EXPECT_TRUE(Matches(factory.CreateURLPrefixCondition(""), url)); | |
358 EXPECT_TRUE(Matches(factory.CreateURLPrefixCondition( | |
359 "https://www.goog"), url)); | |
360 EXPECT_TRUE(Matches(factory.CreateURLPrefixCondition( | |
361 "https://www.google.com"), url)); | |
362 EXPECT_TRUE(Matches(factory.CreateURLPrefixCondition( | |
363 "https://www.google.com/webhp?"), url)); | |
364 EXPECT_FALSE(Matches(factory.CreateURLPrefixCondition( | |
365 "http://www.google.com"), url)); | |
366 EXPECT_FALSE(Matches(factory.CreateURLPrefixCondition("webhp"), url)); | |
367 | |
368 EXPECT_TRUE(Matches(factory.CreateURLSuffixCondition(""), url)); | |
369 EXPECT_TRUE(Matches(factory.CreateURLSuffixCondition("ion=1"), url)); | |
370 EXPECT_FALSE(Matches(factory.CreateURLSuffixCondition("www"), url)); | |
371 | |
372 EXPECT_TRUE(Matches(factory.CreateURLContainsCondition(""), url)); | |
373 EXPECT_TRUE(Matches(factory.CreateURLContainsCondition("www.goog"), url)); | |
374 EXPECT_TRUE(Matches(factory.CreateURLContainsCondition("webhp"), url)); | |
375 EXPECT_TRUE(Matches(factory.CreateURLContainsCondition("?"), url)); | |
376 EXPECT_TRUE(Matches(factory.CreateURLContainsCondition("sourceid"), url)); | |
377 EXPECT_TRUE(Matches(factory.CreateURLContainsCondition("ion=1"), url)); | |
378 EXPECT_FALSE(Matches(factory.CreateURLContainsCondition(".www.goog"), url)); | |
379 EXPECT_FALSE(Matches(factory.CreateURLContainsCondition("foobar"), url)); | |
380 EXPECT_FALSE(Matches(factory.CreateURLContainsCondition("search"), url)); | |
381 EXPECT_FALSE(Matches(factory.CreateURLContainsCondition(":443"), url)); | |
382 | |
383 EXPECT_TRUE(Matches(factory.CreateURLEqualsCondition( | |
384 "https://www.google.com/webhp?sourceid=chrome-instant&ie=UTF-8&ion=1"), | |
385 url)); | |
386 EXPECT_FALSE( | |
387 Matches(factory.CreateURLEqualsCondition("https://www.google.com"), url)); | |
388 | |
389 // Same as above but this time with a non-standard port. | |
390 gurl = GURL("https://www.google.com:1234/webhp?sourceid=chrome-instant&" | |
391 "ie=UTF-8&ion=1#hl=en&output=search&sclient=psy-ab&q=chrome%20is%20" | |
392 "awesome"); | |
393 url = factory.CanonicalizeURLForFullSearches(gurl); | |
394 EXPECT_TRUE(Matches(factory.CreateURLPrefixCondition( | |
395 "https://www.google.com:1234/webhp?"), url)); | |
396 EXPECT_TRUE(Matches(factory.CreateURLContainsCondition(":1234"), url)); | |
397 } | |
398 | |
399 // | |
400 // URLMatcherConditionSet | |
401 // | |
402 | |
403 TEST(URLMatcherConditionSetTest, Constructor) { | |
404 URLMatcherConditionFactory factory; | |
405 URLMatcherCondition m1 = factory.CreateHostSuffixCondition("example.com"); | |
406 URLMatcherCondition m2 = factory.CreatePathContainsCondition("foo"); | |
407 | |
408 std::set<URLMatcherCondition> conditions; | |
409 conditions.insert(m1); | |
410 conditions.insert(m2); | |
411 | |
412 scoped_refptr<URLMatcherConditionSet> condition_set( | |
413 new URLMatcherConditionSet(1, conditions)); | |
414 EXPECT_EQ(1, condition_set->id()); | |
415 EXPECT_EQ(2u, condition_set->conditions().size()); | |
416 } | |
417 | |
418 TEST(URLMatcherConditionSetTest, Matching) { | |
419 GURL url1("http://www.example.com/foo?bar=1"); | |
420 GURL url2("http://foo.example.com/index.html"); | |
421 GURL url3("http://www.example.com:80/foo?bar=1"); | |
422 GURL url4("http://www.example.com:8080/foo?bar=1"); | |
423 | |
424 URLMatcherConditionFactory factory; | |
425 URLMatcherCondition m1 = factory.CreateHostSuffixCondition("example.com"); | |
426 URLMatcherCondition m2 = factory.CreatePathContainsCondition("foo"); | |
427 | |
428 std::set<URLMatcherCondition> conditions; | |
429 conditions.insert(m1); | |
430 conditions.insert(m2); | |
431 | |
432 scoped_refptr<URLMatcherConditionSet> condition_set( | |
433 new URLMatcherConditionSet(1, conditions)); | |
434 EXPECT_EQ(1, condition_set->id()); | |
435 EXPECT_EQ(2u, condition_set->conditions().size()); | |
436 | |
437 std::set<StringPattern::ID> matching_patterns; | |
438 matching_patterns.insert(m1.string_pattern()->id()); | |
439 EXPECT_FALSE(condition_set->IsMatch(matching_patterns, url1)); | |
440 | |
441 matching_patterns.insert(m2.string_pattern()->id()); | |
442 EXPECT_TRUE(condition_set->IsMatch(matching_patterns, url1)); | |
443 EXPECT_FALSE(condition_set->IsMatch(matching_patterns, url2)); | |
444 | |
445 // Test scheme filters. | |
446 scoped_refptr<URLMatcherConditionSet> condition_set2( | |
447 new URLMatcherConditionSet(1, conditions, | |
448 scoped_ptr<URLMatcherSchemeFilter>( | |
449 new URLMatcherSchemeFilter("https")), | |
450 scoped_ptr<URLMatcherPortFilter>(NULL))); | |
451 EXPECT_FALSE(condition_set2->IsMatch(matching_patterns, url1)); | |
452 scoped_refptr<URLMatcherConditionSet> condition_set3( | |
453 new URLMatcherConditionSet(1, conditions, | |
454 scoped_ptr<URLMatcherSchemeFilter>( | |
455 new URLMatcherSchemeFilter("http")), | |
456 scoped_ptr<URLMatcherPortFilter>(NULL))); | |
457 EXPECT_TRUE(condition_set3->IsMatch(matching_patterns, url1)); | |
458 | |
459 // Test port filters. | |
460 std::vector<URLMatcherPortFilter::Range> ranges; | |
461 ranges.push_back(URLMatcherPortFilter::CreateRange(80)); | |
462 scoped_ptr<URLMatcherPortFilter> filter(new URLMatcherPortFilter(ranges)); | |
463 scoped_refptr<URLMatcherConditionSet> condition_set4( | |
464 new URLMatcherConditionSet(1, conditions, | |
465 scoped_ptr<URLMatcherSchemeFilter>(NULL), filter.Pass())); | |
466 EXPECT_TRUE(condition_set4->IsMatch(matching_patterns, url1)); | |
467 EXPECT_TRUE(condition_set4->IsMatch(matching_patterns, url3)); | |
468 EXPECT_FALSE(condition_set4->IsMatch(matching_patterns, url4)); | |
469 | |
470 // Test regex patterns. | |
471 matching_patterns.clear(); | |
472 URLMatcherCondition r1 = factory.CreateURLMatchesCondition("/fo?oo"); | |
473 std::set<URLMatcherCondition> regex_conditions; | |
474 regex_conditions.insert(r1); | |
475 scoped_refptr<URLMatcherConditionSet> condition_set5( | |
476 new URLMatcherConditionSet(1, regex_conditions)); | |
477 EXPECT_FALSE(condition_set5->IsMatch(matching_patterns, url1)); | |
478 matching_patterns.insert(r1.string_pattern()->id()); | |
479 EXPECT_TRUE(condition_set5->IsMatch(matching_patterns, url1)); | |
480 | |
481 regex_conditions.insert(m1); | |
482 scoped_refptr<URLMatcherConditionSet> condition_set6( | |
483 new URLMatcherConditionSet(1, regex_conditions)); | |
484 EXPECT_FALSE(condition_set6->IsMatch(matching_patterns, url1)); | |
485 matching_patterns.insert(m1.string_pattern()->id()); | |
486 EXPECT_TRUE(condition_set6->IsMatch(matching_patterns, url1)); | |
487 } | |
488 | |
489 | |
490 // | |
491 // URLMatcher | |
492 // | |
493 | |
494 TEST(URLMatcherTest, FullTest) { | |
495 GURL url1("http://www.example.com/foo?bar=1"); | |
496 GURL url2("http://foo.example.com/index.html"); | |
497 | |
498 URLMatcher matcher; | |
499 URLMatcherConditionFactory* factory = matcher.condition_factory(); | |
500 | |
501 // First insert. | |
502 URLMatcherConditionSet::Conditions conditions1; | |
503 conditions1.insert(factory->CreateHostSuffixCondition("example.com")); | |
504 conditions1.insert(factory->CreatePathContainsCondition("foo")); | |
505 | |
506 const int kConditionSetId1 = 1; | |
507 URLMatcherConditionSet::Vector insert1; | |
508 insert1.push_back(make_scoped_refptr( | |
509 new URLMatcherConditionSet(kConditionSetId1, conditions1))); | |
510 matcher.AddConditionSets(insert1); | |
511 EXPECT_EQ(1u, matcher.MatchURL(url1).size()); | |
512 EXPECT_EQ(0u, matcher.MatchURL(url2).size()); | |
513 | |
514 // Second insert. | |
515 URLMatcherConditionSet::Conditions conditions2; | |
516 conditions2.insert(factory->CreateHostSuffixCondition("example.com")); | |
517 | |
518 const int kConditionSetId2 = 2; | |
519 URLMatcherConditionSet::Vector insert2; | |
520 insert2.push_back(make_scoped_refptr( | |
521 new URLMatcherConditionSet(kConditionSetId2, conditions2))); | |
522 matcher.AddConditionSets(insert2); | |
523 EXPECT_EQ(2u, matcher.MatchURL(url1).size()); | |
524 EXPECT_EQ(1u, matcher.MatchURL(url2).size()); | |
525 | |
526 // This should be the cached singleton. | |
527 int patternId1 = factory->CreateHostSuffixCondition( | |
528 "example.com").string_pattern()->id(); | |
529 | |
530 // Third insert. | |
531 URLMatcherConditionSet::Conditions conditions3; | |
532 conditions3.insert(factory->CreateHostSuffixCondition("example.com")); | |
533 conditions3.insert(factory->CreateURLMatchesCondition("x.*[0-9]")); | |
534 | |
535 const int kConditionSetId3 = 3; | |
536 URLMatcherConditionSet::Vector insert3; | |
537 insert3.push_back(make_scoped_refptr( | |
538 new URLMatcherConditionSet(kConditionSetId3, conditions3))); | |
539 matcher.AddConditionSets(insert3); | |
540 EXPECT_EQ(3u, matcher.MatchURL(url1).size()); | |
541 EXPECT_EQ(1u, matcher.MatchURL(url2).size()); | |
542 | |
543 // Removal of third insert. | |
544 std::vector<URLMatcherConditionSet::ID> remove3; | |
545 remove3.push_back(kConditionSetId3); | |
546 matcher.RemoveConditionSets(remove3); | |
547 EXPECT_EQ(2u, matcher.MatchURL(url1).size()); | |
548 EXPECT_EQ(1u, matcher.MatchURL(url2).size()); | |
549 | |
550 // Removal of second insert. | |
551 std::vector<URLMatcherConditionSet::ID> remove2; | |
552 remove2.push_back(kConditionSetId2); | |
553 matcher.RemoveConditionSets(remove2); | |
554 EXPECT_EQ(1u, matcher.MatchURL(url1).size()); | |
555 EXPECT_EQ(0u, matcher.MatchURL(url2).size()); | |
556 | |
557 // Removal of first insert. | |
558 std::vector<URLMatcherConditionSet::ID> remove1; | |
559 remove1.push_back(kConditionSetId1); | |
560 matcher.RemoveConditionSets(remove1); | |
561 EXPECT_EQ(0u, matcher.MatchURL(url1).size()); | |
562 EXPECT_EQ(0u, matcher.MatchURL(url2).size()); | |
563 | |
564 EXPECT_TRUE(matcher.IsEmpty()); | |
565 | |
566 // The cached singleton in matcher.condition_factory_ should be destroyed to | |
567 // free memory. | |
568 int patternId2 = factory->CreateHostSuffixCondition( | |
569 "example.com").string_pattern()->id(); | |
570 // If patternId1 and patternId2 are different that indicates that | |
571 // matcher.condition_factory_ does not leak memory by holding onto | |
572 // unused patterns. | |
573 EXPECT_NE(patternId1, patternId2); | |
574 } | |
575 | |
576 TEST(URLMatcherTest, TestComponentsImplyContains) { | |
577 // Due to a different implementation of component (prefix, suffix and equals) | |
578 // and *Contains conditions we need to check that when a pattern matches a | |
579 // given part of a URL as equal, prefix or suffix, it also matches it in the | |
580 // "contains" test. | |
581 GURL url("https://www.google.com:1234/webhp?test=val&a=b"); | |
582 | |
583 URLMatcher matcher; | |
584 URLMatcherConditionFactory* factory = matcher.condition_factory(); | |
585 | |
586 URLMatcherConditionSet::Conditions conditions; | |
587 | |
588 // First insert all the matching equals => contains pairs. | |
589 conditions.insert(factory->CreateHostEqualsCondition("www.google.com")); | |
590 conditions.insert(factory->CreateHostContainsCondition("www.google.com")); | |
591 | |
592 conditions.insert(factory->CreateHostPrefixCondition("www.")); | |
593 conditions.insert(factory->CreateHostContainsCondition("www.")); | |
594 | |
595 conditions.insert(factory->CreateHostSuffixCondition("com")); | |
596 conditions.insert(factory->CreateHostContainsCondition("com")); | |
597 | |
598 conditions.insert(factory->CreatePathEqualsCondition("/webhp")); | |
599 conditions.insert(factory->CreatePathContainsCondition("/webhp")); | |
600 | |
601 conditions.insert(factory->CreatePathPrefixCondition("/we")); | |
602 conditions.insert(factory->CreatePathContainsCondition("/we")); | |
603 | |
604 conditions.insert(factory->CreatePathSuffixCondition("hp")); | |
605 conditions.insert(factory->CreatePathContainsCondition("hp")); | |
606 | |
607 conditions.insert(factory->CreateQueryEqualsCondition("test=val&a=b")); | |
608 conditions.insert(factory->CreateQueryContainsCondition("test=val&a=b")); | |
609 | |
610 conditions.insert(factory->CreateQueryPrefixCondition("test=v")); | |
611 conditions.insert(factory->CreateQueryContainsCondition("test=v")); | |
612 | |
613 conditions.insert(factory->CreateQuerySuffixCondition("l&a=b")); | |
614 conditions.insert(factory->CreateQueryContainsCondition("l&a=b")); | |
615 | |
616 // The '?' for equality is just ignored. | |
617 conditions.insert(factory->CreateQueryEqualsCondition("?test=val&a=b")); | |
618 // Due to '?' the condition created here is a prefix-testing condition. | |
619 conditions.insert(factory->CreateQueryContainsCondition("?test=val&a=b")); | |
620 | |
621 const int kConditionSetId = 1; | |
622 URLMatcherConditionSet::Vector insert; | |
623 insert.push_back(make_scoped_refptr( | |
624 new URLMatcherConditionSet(kConditionSetId, conditions))); | |
625 matcher.AddConditionSets(insert); | |
626 EXPECT_EQ(1u, matcher.MatchURL(url).size()); | |
627 } | |
628 | |
629 } // namespace extensions | |
OLD | NEW |