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

Side by Side Diff: chrome/common/extensions/matcher/url_matcher_unittest.cc

Issue 10910179: Event matching by regular expression matching on URLs. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: ascii artiste Created 8 years, 3 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/common/extensions/matcher/url_matcher.h" 5 #include "chrome/common/extensions/matcher/url_matcher.h"
6 6
7 #include "base/string_util.h" 7 #include "base/string_util.h"
8 #include "googleurl/src/gurl.h" 8 #include "googleurl/src/gurl.h"
9 #include "testing/gtest/include/gtest/gtest.h" 9 #include "testing/gtest/include/gtest/gtest.h"
10 10
11 namespace extensions { 11 namespace extensions {
12 12
13 // 13 //
14 // URLMatcherCondition 14 // URLMatcherCondition
15 // 15 //
16 16
17 TEST(URLMatcherConditionTest, Constructors) { 17 TEST(URLMatcherConditionTest, Constructors) {
18 SubstringPattern pattern("example.com", 1); 18 StringPattern pattern("example.com", 1);
19 URLMatcherCondition m1(URLMatcherCondition::HOST_SUFFIX, &pattern); 19 URLMatcherCondition m1(URLMatcherCondition::HOST_SUFFIX, &pattern);
20 EXPECT_EQ(URLMatcherCondition::HOST_SUFFIX, m1.criterion()); 20 EXPECT_EQ(URLMatcherCondition::HOST_SUFFIX, m1.criterion());
21 EXPECT_EQ(&pattern, m1.substring_pattern()); 21 EXPECT_EQ(&pattern, m1.string_pattern());
22 22
23 URLMatcherCondition m2; 23 URLMatcherCondition m2;
24 m2 = m1; 24 m2 = m1;
25 EXPECT_EQ(URLMatcherCondition::HOST_SUFFIX, m2.criterion()); 25 EXPECT_EQ(URLMatcherCondition::HOST_SUFFIX, m2.criterion());
26 EXPECT_EQ(&pattern, m2.substring_pattern()); 26 EXPECT_EQ(&pattern, m2.string_pattern());
27 27
28 URLMatcherCondition m3(m1); 28 URLMatcherCondition m3(m1);
29 EXPECT_EQ(URLMatcherCondition::HOST_SUFFIX, m3.criterion()); 29 EXPECT_EQ(URLMatcherCondition::HOST_SUFFIX, m3.criterion());
30 EXPECT_EQ(&pattern, m3.substring_pattern()); 30 EXPECT_EQ(&pattern, m3.string_pattern());
31 } 31 }
32 32
33 TEST(URLMatcherSchemeFilter, TestMatching) { 33 TEST(URLMatcherSchemeFilter, TestMatching) {
34 URLMatcherSchemeFilter filter1("https"); 34 URLMatcherSchemeFilter filter1("https");
35 std::vector<std::string> filter2_content; 35 std::vector<std::string> filter2_content;
36 filter2_content.push_back("http"); 36 filter2_content.push_back("http");
37 filter2_content.push_back("https"); 37 filter2_content.push_back("https");
38 URLMatcherSchemeFilter filter2(filter2_content); 38 URLMatcherSchemeFilter filter2(filter2_content);
39 39
40 GURL matching_url("https://www.foobar.com"); 40 GURL matching_url("https://www.foobar.com");
(...skipping 13 matching lines...) Expand all
54 EXPECT_TRUE(filter.IsMatch(GURL("http://www.example.com:80"))); 54 EXPECT_TRUE(filter.IsMatch(GURL("http://www.example.com:80")));
55 EXPECT_TRUE(filter.IsMatch(GURL("http://www.example.com:81"))); 55 EXPECT_TRUE(filter.IsMatch(GURL("http://www.example.com:81")));
56 EXPECT_TRUE(filter.IsMatch(GURL("http://www.example.com:90"))); 56 EXPECT_TRUE(filter.IsMatch(GURL("http://www.example.com:90")));
57 EXPECT_TRUE(filter.IsMatch(GURL("http://www.example.com:8080"))); 57 EXPECT_TRUE(filter.IsMatch(GURL("http://www.example.com:8080")));
58 EXPECT_FALSE(filter.IsMatch(GURL("http://www.example.com:79"))); 58 EXPECT_FALSE(filter.IsMatch(GURL("http://www.example.com:79")));
59 EXPECT_FALSE(filter.IsMatch(GURL("http://www.example.com:91"))); 59 EXPECT_FALSE(filter.IsMatch(GURL("http://www.example.com:91")));
60 EXPECT_FALSE(filter.IsMatch(GURL("https://www.example.com"))); 60 EXPECT_FALSE(filter.IsMatch(GURL("https://www.example.com")));
61 } 61 }
62 62
63 TEST(URLMatcherConditionTest, IsFullURLCondition) { 63 TEST(URLMatcherConditionTest, IsFullURLCondition) {
64 SubstringPattern pattern("example.com", 1); 64 StringPattern pattern("example.com", 1);
65 EXPECT_FALSE(URLMatcherCondition(URLMatcherCondition::HOST_SUFFIX, 65 EXPECT_FALSE(URLMatcherCondition(URLMatcherCondition::HOST_SUFFIX,
66 &pattern).IsFullURLCondition()); 66 &pattern).IsFullURLCondition());
67 67
68 EXPECT_TRUE(URLMatcherCondition(URLMatcherCondition::HOST_CONTAINS, 68 EXPECT_TRUE(URLMatcherCondition(URLMatcherCondition::HOST_CONTAINS,
69 &pattern).IsFullURLCondition()); 69 &pattern).IsFullURLCondition());
70 EXPECT_TRUE(URLMatcherCondition(URLMatcherCondition::PATH_CONTAINS, 70 EXPECT_TRUE(URLMatcherCondition(URLMatcherCondition::PATH_CONTAINS,
71 &pattern).IsFullURLCondition()); 71 &pattern).IsFullURLCondition());
72 EXPECT_TRUE(URLMatcherCondition(URLMatcherCondition::QUERY_CONTAINS, 72 EXPECT_TRUE(URLMatcherCondition(URLMatcherCondition::QUERY_CONTAINS,
73 &pattern).IsFullURLCondition()); 73 &pattern).IsFullURLCondition());
74 74
75 EXPECT_TRUE(URLMatcherCondition(URLMatcherCondition::URL_PREFIX, 75 EXPECT_TRUE(URLMatcherCondition(URLMatcherCondition::URL_PREFIX,
76 &pattern).IsFullURLCondition()); 76 &pattern).IsFullURLCondition());
77 EXPECT_TRUE(URLMatcherCondition(URLMatcherCondition::URL_SUFFIX, 77 EXPECT_TRUE(URLMatcherCondition(URLMatcherCondition::URL_SUFFIX,
78 &pattern).IsFullURLCondition()); 78 &pattern).IsFullURLCondition());
79 EXPECT_TRUE(URLMatcherCondition(URLMatcherCondition::URL_CONTAINS, 79 EXPECT_TRUE(URLMatcherCondition(URLMatcherCondition::URL_CONTAINS,
80 &pattern).IsFullURLCondition()); 80 &pattern).IsFullURLCondition());
81 EXPECT_TRUE(URLMatcherCondition(URLMatcherCondition::URL_EQUALS, 81 EXPECT_TRUE(URLMatcherCondition(URLMatcherCondition::URL_EQUALS,
82 &pattern).IsFullURLCondition()); 82 &pattern).IsFullURLCondition());
83 } 83 }
84 84
85 TEST(URLMatcherConditionTest, IsMatch) { 85 TEST(URLMatcherConditionTest, IsMatch) {
86 GURL url1("http://www.example.com/www.foobar.com/index.html"); 86 GURL url1("http://www.example.com/www.foobar.com/index.html");
87 GURL url2("http://www.foobar.com/example.com/index.html"); 87 GURL url2("http://www.foobar.com/example.com/index.html");
88 88
89 SubstringPattern pattern("example.com", 1); 89 StringPattern pattern("example.com", 1);
90 URLMatcherCondition m1(URLMatcherCondition::HOST_SUFFIX, &pattern); 90 URLMatcherCondition m1(URLMatcherCondition::HOST_SUFFIX, &pattern);
91 91
92 std::set<SubstringPattern::ID> matching_substring_patterns; 92 std::set<StringPattern::ID> matching_patterns;
93 93
94 // matches = {0} --> matcher did not indicate that m1 was a match. 94 // matches = {0} --> matcher did not indicate that m1 was a match.
95 matching_substring_patterns.insert(0); 95 matching_patterns.insert(0);
96 EXPECT_FALSE(m1.IsMatch(matching_substring_patterns, url1)); 96 EXPECT_FALSE(m1.IsMatch(matching_patterns, url1));
97 97
98 // matches = {0, 1} --> matcher did indicate that m1 was a match. 98 // matches = {0, 1} --> matcher did indicate that m1 was a match.
99 matching_substring_patterns.insert(1); 99 matching_patterns.insert(1);
100 EXPECT_TRUE(m1.IsMatch(matching_substring_patterns, url1)); 100 EXPECT_TRUE(m1.IsMatch(matching_patterns, url1));
101 101
102 // For m2 we use a HOST_CONTAINS test, which requires a post-validation 102 // For m2 we use a HOST_CONTAINS test, which requires a post-validation
103 // whether the match reported by the SubstringSetMatcher occurs really 103 // whether the match reported by the SubstringSetMatcher occurs really
104 // in the correct url component. 104 // in the correct url component.
105 URLMatcherCondition m2(URLMatcherCondition::HOST_CONTAINS, &pattern); 105 URLMatcherCondition m2(URLMatcherCondition::HOST_CONTAINS, &pattern);
106 EXPECT_TRUE(m2.IsMatch(matching_substring_patterns, url1)); 106 EXPECT_TRUE(m2.IsMatch(matching_patterns, url1));
107 EXPECT_FALSE(m2.IsMatch(matching_substring_patterns, url2)); 107 EXPECT_FALSE(m2.IsMatch(matching_patterns, url2));
108 } 108 }
109 109
110 TEST(URLMatcherConditionTest, Comparison) { 110 TEST(URLMatcherConditionTest, Comparison) {
111 SubstringPattern p1("foobar.com", 1); 111 StringPattern p1("foobar.com", 1);
112 SubstringPattern p2("foobar.com", 2); 112 StringPattern p2("foobar.com", 2);
113 // The first component of each test is expected to be < than the second. 113 // The first component of each test is expected to be < than the second.
114 URLMatcherCondition test_smaller[][2] = { 114 URLMatcherCondition test_smaller[][2] = {
115 {URLMatcherCondition(URLMatcherCondition::HOST_PREFIX, &p1), 115 {URLMatcherCondition(URLMatcherCondition::HOST_PREFIX, &p1),
116 URLMatcherCondition(URLMatcherCondition::HOST_SUFFIX, &p1)}, 116 URLMatcherCondition(URLMatcherCondition::HOST_SUFFIX, &p1)},
117 {URLMatcherCondition(URLMatcherCondition::HOST_PREFIX, &p1), 117 {URLMatcherCondition(URLMatcherCondition::HOST_PREFIX, &p1),
118 URLMatcherCondition(URLMatcherCondition::HOST_PREFIX, &p2)}, 118 URLMatcherCondition(URLMatcherCondition::HOST_PREFIX, &p2)},
119 {URLMatcherCondition(URLMatcherCondition::HOST_PREFIX, NULL), 119 {URLMatcherCondition(URLMatcherCondition::HOST_PREFIX, NULL),
120 URLMatcherCondition(URLMatcherCondition::HOST_PREFIX, &p2)}, 120 URLMatcherCondition(URLMatcherCondition::HOST_PREFIX, &p2)},
121 {URLMatcherCondition(URLMatcherCondition::HOST_PREFIX, &p1), 121 {URLMatcherCondition(URLMatcherCondition::HOST_PREFIX, &p1),
122 URLMatcherCondition(URLMatcherCondition::HOST_SUFFIX, NULL)}, 122 URLMatcherCondition(URLMatcherCondition::HOST_SUFFIX, NULL)},
(...skipping 18 matching lines...) Expand all
141 } 141 }
142 } 142 }
143 143
144 // 144 //
145 // URLMatcherConditionFactory 145 // URLMatcherConditionFactory
146 // 146 //
147 147
148 namespace { 148 namespace {
149 149
150 bool Matches(const URLMatcherCondition& condition, std::string text) { 150 bool Matches(const URLMatcherCondition& condition, std::string text) {
151 return text.find(condition.substring_pattern()->pattern()) != 151 return text.find(condition.string_pattern()->pattern()) !=
152 std::string::npos; 152 std::string::npos;
153 } 153 }
154 154
155 } // namespace 155 } // namespace
156 156
157 TEST(URLMatcherConditionFactoryTest, GURLCharacterSet) { 157 TEST(URLMatcherConditionFactoryTest, GURLCharacterSet) {
158 // GURL guarantees that neither domain, nor path, nor query may contain 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 159 // non ASCII-7 characters. We test this here, because a change to this
160 // guarantee breaks this implementation horribly. 160 // guarantee breaks this implementation horribly.
161 GURL url("http://www.föö.com/föö?föö#föö"); 161 GURL url("http://www.föö.com/föö?föö#föö");
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
198 factory.CreateHostEqualsPathPrefixCondition("foo", 198 factory.CreateHostEqualsPathPrefixCondition("foo",
199 "bar").criterion()); 199 "bar").criterion());
200 EXPECT_EQ(URLMatcherCondition::URL_PREFIX, 200 EXPECT_EQ(URLMatcherCondition::URL_PREFIX,
201 factory.CreateURLPrefixCondition("foo").criterion()); 201 factory.CreateURLPrefixCondition("foo").criterion());
202 EXPECT_EQ(URLMatcherCondition::URL_SUFFIX, 202 EXPECT_EQ(URLMatcherCondition::URL_SUFFIX,
203 factory.CreateURLSuffixCondition("foo").criterion()); 203 factory.CreateURLSuffixCondition("foo").criterion());
204 EXPECT_EQ(URLMatcherCondition::URL_CONTAINS, 204 EXPECT_EQ(URLMatcherCondition::URL_CONTAINS,
205 factory.CreateURLContainsCondition("foo").criterion()); 205 factory.CreateURLContainsCondition("foo").criterion());
206 EXPECT_EQ(URLMatcherCondition::URL_EQUALS, 206 EXPECT_EQ(URLMatcherCondition::URL_EQUALS,
207 factory.CreateURLEqualsCondition("foo").criterion()); 207 factory.CreateURLEqualsCondition("foo").criterion());
208 EXPECT_EQ(URLMatcherCondition::URL_MATCHES,
209 factory.CreateURLMatchesCondition("foo").criterion());
208 } 210 }
209 211
210 TEST(URLMatcherConditionFactoryTest, TestSingletonProperty) { 212 TEST(URLMatcherConditionFactoryTest, TestSingletonProperty) {
211 URLMatcherConditionFactory factory; 213 URLMatcherConditionFactory factory;
212 URLMatcherCondition c1 = factory.CreateHostEqualsCondition("www.google.com"); 214 URLMatcherCondition c1 = factory.CreateHostEqualsCondition("www.google.com");
213 URLMatcherCondition c2 = factory.CreateHostEqualsCondition("www.google.com"); 215 URLMatcherCondition c2 = factory.CreateHostEqualsCondition("www.google.com");
214 EXPECT_EQ(c1.criterion(), c2.criterion()); 216 EXPECT_EQ(c1.criterion(), c2.criterion());
215 EXPECT_EQ(c1.substring_pattern(), c2.substring_pattern()); 217 EXPECT_EQ(c1.string_pattern(), c2.string_pattern());
216 URLMatcherCondition c3 = factory.CreateHostEqualsCondition("www.google.de"); 218 URLMatcherCondition c3 = factory.CreateHostEqualsCondition("www.google.de");
217 EXPECT_EQ(c2.criterion(), c3.criterion()); 219 EXPECT_EQ(c2.criterion(), c3.criterion());
218 EXPECT_NE(c2.substring_pattern(), c3.substring_pattern()); 220 EXPECT_NE(c2.string_pattern(), c3.string_pattern());
219 EXPECT_NE(c2.substring_pattern()->id(), c3.substring_pattern()->id()); 221 EXPECT_NE(c2.string_pattern()->id(), c3.string_pattern()->id());
220 EXPECT_NE(c2.substring_pattern()->pattern(), 222 EXPECT_NE(c2.string_pattern()->pattern(),
221 c3.substring_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());
222 230
223 // Check that all SubstringPattern singletons are freed if we call 231 // Check that all StringPattern singletons are freed if we call
224 // ForgetUnusedPatterns. 232 // ForgetUnusedPatterns.
225 SubstringPattern::ID old_id_1 = c1.substring_pattern()->id(); 233 StringPattern::ID old_id_1 = c1.string_pattern()->id();
226 factory.ForgetUnusedPatterns(std::set<SubstringPattern::ID>()); 234 StringPattern::ID old_id_4 = c4.string_pattern()->id();
235 factory.ForgetUnusedPatterns(std::set<StringPattern::ID>());
227 EXPECT_TRUE(factory.IsEmpty()); 236 EXPECT_TRUE(factory.IsEmpty());
228 URLMatcherCondition c4 = factory.CreateHostEqualsCondition("www.google.com"); 237 URLMatcherCondition c6 = factory.CreateHostEqualsCondition("www.google.com");
229 EXPECT_NE(old_id_1, c4.substring_pattern()->id()); 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());
230 } 241 }
231 242
232 TEST(URLMatcherConditionFactoryTest, TestComponentSearches) { 243 TEST(URLMatcherConditionFactoryTest, TestComponentSearches) {
233 GURL gurl("https://www.google.com:1234/webhp?sourceid=chrome-instant&ie=UTF-8" 244 GURL gurl("https://www.google.com:1234/webhp?sourceid=chrome-instant&ie=UTF-8"
234 "&ion=1#hl=en&output=search&sclient=psy-ab&q=chrome%20is%20awesome"); 245 "&ion=1#hl=en&output=search&sclient=psy-ab&q=chrome%20is%20awesome");
235 URLMatcherConditionFactory factory; 246 URLMatcherConditionFactory factory;
236 std::string url = factory.CanonicalizeURLForComponentSearches(gurl); 247 std::string url = factory.CanonicalizeURLForComponentSearches(gurl);
237 248
238 // Test host component. 249 // Test host component.
239 EXPECT_TRUE(Matches(factory.CreateHostPrefixCondition(""), url)); 250 EXPECT_TRUE(Matches(factory.CreateHostPrefixCondition(""), url));
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after
367 // Same as above but this time with a non-standard port. 378 // Same as above but this time with a non-standard port.
368 gurl = GURL("https://www.google.com:1234/webhp?sourceid=chrome-instant&" 379 gurl = GURL("https://www.google.com:1234/webhp?sourceid=chrome-instant&"
369 "ie=UTF-8&ion=1#hl=en&output=search&sclient=psy-ab&q=chrome%20is%20" 380 "ie=UTF-8&ion=1#hl=en&output=search&sclient=psy-ab&q=chrome%20is%20"
370 "awesome"); 381 "awesome");
371 url = factory.CanonicalizeURLForFullSearches(gurl); 382 url = factory.CanonicalizeURLForFullSearches(gurl);
372 EXPECT_TRUE(Matches(factory.CreateURLPrefixCondition( 383 EXPECT_TRUE(Matches(factory.CreateURLPrefixCondition(
373 "https://www.google.com:1234/webhp?"), url)); 384 "https://www.google.com:1234/webhp?"), url));
374 EXPECT_TRUE(Matches(factory.CreateURLContainsCondition(":1234"), url)); 385 EXPECT_TRUE(Matches(factory.CreateURLContainsCondition(":1234"), url));
375 } 386 }
376 387
377
378 // 388 //
379 // URLMatcherConditionSet 389 // URLMatcherConditionSet
380 // 390 //
381 391
382 TEST(URLMatcherConditionSetTest, Constructor) { 392 TEST(URLMatcherConditionSetTest, Constructor) {
383 URLMatcherConditionFactory factory; 393 URLMatcherConditionFactory factory;
384 URLMatcherCondition m1 = factory.CreateHostSuffixCondition("example.com"); 394 URLMatcherCondition m1 = factory.CreateHostSuffixCondition("example.com");
385 URLMatcherCondition m2 = factory.CreatePathContainsCondition("foo"); 395 URLMatcherCondition m2 = factory.CreatePathContainsCondition("foo");
386 396
387 std::set<URLMatcherCondition> conditions; 397 std::set<URLMatcherCondition> conditions;
(...skipping 18 matching lines...) Expand all
406 416
407 std::set<URLMatcherCondition> conditions; 417 std::set<URLMatcherCondition> conditions;
408 conditions.insert(m1); 418 conditions.insert(m1);
409 conditions.insert(m2); 419 conditions.insert(m2);
410 420
411 scoped_refptr<URLMatcherConditionSet> condition_set( 421 scoped_refptr<URLMatcherConditionSet> condition_set(
412 new URLMatcherConditionSet(1, conditions)); 422 new URLMatcherConditionSet(1, conditions));
413 EXPECT_EQ(1, condition_set->id()); 423 EXPECT_EQ(1, condition_set->id());
414 EXPECT_EQ(2u, condition_set->conditions().size()); 424 EXPECT_EQ(2u, condition_set->conditions().size());
415 425
416 std::set<SubstringPattern::ID> matching_substring_patterns; 426 std::set<StringPattern::ID> matching_patterns;
417 matching_substring_patterns.insert(m1.substring_pattern()->id()); 427 matching_patterns.insert(m1.string_pattern()->id());
418 EXPECT_FALSE(condition_set->IsMatch(matching_substring_patterns, url1)); 428 EXPECT_FALSE(condition_set->IsMatch(matching_patterns, url1));
419 429
420 matching_substring_patterns.insert(m2.substring_pattern()->id()); 430 matching_patterns.insert(m2.string_pattern()->id());
421 EXPECT_TRUE(condition_set->IsMatch(matching_substring_patterns, url1)); 431 EXPECT_TRUE(condition_set->IsMatch(matching_patterns, url1));
422 EXPECT_FALSE(condition_set->IsMatch(matching_substring_patterns, url2)); 432 EXPECT_FALSE(condition_set->IsMatch(matching_patterns, url2));
423 433
424 // Test scheme filters. 434 // Test scheme filters.
425 scoped_refptr<URLMatcherConditionSet> condition_set2( 435 scoped_refptr<URLMatcherConditionSet> condition_set2(
426 new URLMatcherConditionSet(1, conditions, 436 new URLMatcherConditionSet(1, conditions,
427 scoped_ptr<URLMatcherSchemeFilter>( 437 scoped_ptr<URLMatcherSchemeFilter>(
428 new URLMatcherSchemeFilter("https")), 438 new URLMatcherSchemeFilter("https")),
429 scoped_ptr<URLMatcherPortFilter>(NULL))); 439 scoped_ptr<URLMatcherPortFilter>(NULL)));
430 EXPECT_FALSE(condition_set2->IsMatch(matching_substring_patterns, url1)); 440 EXPECT_FALSE(condition_set2->IsMatch(matching_patterns, url1));
431 scoped_refptr<URLMatcherConditionSet> condition_set3( 441 scoped_refptr<URLMatcherConditionSet> condition_set3(
432 new URLMatcherConditionSet(1, conditions, 442 new URLMatcherConditionSet(1, conditions,
433 scoped_ptr<URLMatcherSchemeFilter>( 443 scoped_ptr<URLMatcherSchemeFilter>(
434 new URLMatcherSchemeFilter("http")), 444 new URLMatcherSchemeFilter("http")),
435 scoped_ptr<URLMatcherPortFilter>(NULL))); 445 scoped_ptr<URLMatcherPortFilter>(NULL)));
436 EXPECT_TRUE(condition_set3->IsMatch(matching_substring_patterns, url1)); 446 EXPECT_TRUE(condition_set3->IsMatch(matching_patterns, url1));
437 447
438 // Test port filters. 448 // Test port filters.
439 std::vector<URLMatcherPortFilter::Range> ranges; 449 std::vector<URLMatcherPortFilter::Range> ranges;
440 ranges.push_back(URLMatcherPortFilter::CreateRange(80)); 450 ranges.push_back(URLMatcherPortFilter::CreateRange(80));
441 scoped_ptr<URLMatcherPortFilter> filter(new URLMatcherPortFilter(ranges)); 451 scoped_ptr<URLMatcherPortFilter> filter(new URLMatcherPortFilter(ranges));
442 scoped_refptr<URLMatcherConditionSet> condition_set4( 452 scoped_refptr<URLMatcherConditionSet> condition_set4(
443 new URLMatcherConditionSet(1, conditions, 453 new URLMatcherConditionSet(1, conditions,
444 scoped_ptr<URLMatcherSchemeFilter>(NULL), filter.Pass())); 454 scoped_ptr<URLMatcherSchemeFilter>(NULL), filter.Pass()));
445 EXPECT_TRUE(condition_set4->IsMatch(matching_substring_patterns, url1)); 455 EXPECT_TRUE(condition_set4->IsMatch(matching_patterns, url1));
446 EXPECT_TRUE(condition_set4->IsMatch(matching_substring_patterns, url3)); 456 EXPECT_TRUE(condition_set4->IsMatch(matching_patterns, url3));
447 EXPECT_FALSE(condition_set4->IsMatch(matching_substring_patterns, url4)); 457 EXPECT_FALSE(condition_set4->IsMatch(matching_patterns, url4));
458
459 // Test regex patterns.
460 matching_patterns.clear();
461 URLMatcherCondition r1 = factory.CreateURLMatchesCondition("/fo?oo");
462 std::set<URLMatcherCondition> regex_conditions;
463 regex_conditions.insert(r1);
464 scoped_refptr<URLMatcherConditionSet> condition_set5(
465 new URLMatcherConditionSet(1, regex_conditions));
466 EXPECT_FALSE(condition_set5->IsMatch(matching_patterns, url1));
467 matching_patterns.insert(r1.string_pattern()->id());
468 EXPECT_TRUE(condition_set5->IsMatch(matching_patterns, url1));
469
470 regex_conditions.insert(m1);
471 scoped_refptr<URLMatcherConditionSet> condition_set6(
472 new URLMatcherConditionSet(1, regex_conditions));
473 EXPECT_FALSE(condition_set6->IsMatch(matching_patterns, url1));
474 matching_patterns.insert(m1.string_pattern()->id());
475 EXPECT_TRUE(condition_set6->IsMatch(matching_patterns, url1));
448 } 476 }
449 477
450 478
451 // 479 //
452 // URLMatcher 480 // URLMatcher
453 // 481 //
454 482
455 TEST(URLMatcherTest, FullTest) { 483 TEST(URLMatcherTest, FullTest) {
456 GURL url1("http://www.example.com/foo?bar=1"); 484 GURL url1("http://www.example.com/foo?bar=1");
457 GURL url2("http://foo.example.com/index.html"); 485 GURL url2("http://foo.example.com/index.html");
(...skipping 21 matching lines...) Expand all
479 const int kConditionSetId2 = 2; 507 const int kConditionSetId2 = 2;
480 URLMatcherConditionSet::Vector insert2; 508 URLMatcherConditionSet::Vector insert2;
481 insert2.push_back(make_scoped_refptr( 509 insert2.push_back(make_scoped_refptr(
482 new URLMatcherConditionSet(kConditionSetId2, conditions2))); 510 new URLMatcherConditionSet(kConditionSetId2, conditions2)));
483 matcher.AddConditionSets(insert2); 511 matcher.AddConditionSets(insert2);
484 EXPECT_EQ(2u, matcher.MatchURL(url1).size()); 512 EXPECT_EQ(2u, matcher.MatchURL(url1).size());
485 EXPECT_EQ(1u, matcher.MatchURL(url2).size()); 513 EXPECT_EQ(1u, matcher.MatchURL(url2).size());
486 514
487 // This should be the cached singleton. 515 // This should be the cached singleton.
488 int patternId1 = factory->CreateHostSuffixCondition( 516 int patternId1 = factory->CreateHostSuffixCondition(
489 "example.com").substring_pattern()->id(); 517 "example.com").string_pattern()->id();
490 518
491 // Removal of last insert. 519 // Third insert.
520 URLMatcherConditionSet::Conditions conditions3;
521 conditions3.insert(factory->CreateHostSuffixCondition("example.com"));
522 conditions3.insert(factory->CreateURLMatchesCondition("x.*[0-9]"));
523
524 const int kConditionSetId3 = 3;
525 URLMatcherConditionSet::Vector insert3;
526 insert3.push_back(make_scoped_refptr(
527 new URLMatcherConditionSet(kConditionSetId3, conditions3)));
528 matcher.AddConditionSets(insert3);
529 EXPECT_EQ(3u, matcher.MatchURL(url1).size());
530 EXPECT_EQ(1u, matcher.MatchURL(url2).size());
531
532 // Removal of third insert.
533 std::vector<URLMatcherConditionSet::ID> remove3;
534 remove3.push_back(kConditionSetId3);
535 matcher.RemoveConditionSets(remove3);
536 EXPECT_EQ(2u, matcher.MatchURL(url1).size());
537 EXPECT_EQ(1u, matcher.MatchURL(url2).size());
538
539 // Removal of second insert.
492 std::vector<URLMatcherConditionSet::ID> remove2; 540 std::vector<URLMatcherConditionSet::ID> remove2;
493 remove2.push_back(kConditionSetId2); 541 remove2.push_back(kConditionSetId2);
494 matcher.RemoveConditionSets(remove2); 542 matcher.RemoveConditionSets(remove2);
495 EXPECT_EQ(1u, matcher.MatchURL(url1).size()); 543 EXPECT_EQ(1u, matcher.MatchURL(url1).size());
496 EXPECT_EQ(0u, matcher.MatchURL(url2).size()); 544 EXPECT_EQ(0u, matcher.MatchURL(url2).size());
497 545
498 // Removal of first insert. 546 // Removal of first insert.
499 std::vector<URLMatcherConditionSet::ID> remove1; 547 std::vector<URLMatcherConditionSet::ID> remove1;
500 remove1.push_back(kConditionSetId1); 548 remove1.push_back(kConditionSetId1);
501 matcher.RemoveConditionSets(remove1); 549 matcher.RemoveConditionSets(remove1);
502 EXPECT_EQ(0u, matcher.MatchURL(url1).size()); 550 EXPECT_EQ(0u, matcher.MatchURL(url1).size());
503 EXPECT_EQ(0u, matcher.MatchURL(url2).size()); 551 EXPECT_EQ(0u, matcher.MatchURL(url2).size());
504 552
505 EXPECT_TRUE(matcher.IsEmpty()); 553 EXPECT_TRUE(matcher.IsEmpty());
506 554
507 // The cached singleton in matcher.condition_factory_ should be destroyed to 555 // The cached singleton in matcher.condition_factory_ should be destroyed to
508 // free memory. 556 // free memory.
509 int patternId2 = factory->CreateHostSuffixCondition( 557 int patternId2 = factory->CreateHostSuffixCondition(
510 "example.com").substring_pattern()->id(); 558 "example.com").string_pattern()->id();
511 // If patternId1 and patternId2 are different that indicates that 559 // If patternId1 and patternId2 are different that indicates that
512 // matcher.condition_factory_ does not leak memory. 560 // matcher.condition_factory_ does not leak memory by holding onto
561 // unused patterns.
513 EXPECT_NE(patternId1, patternId2); 562 EXPECT_NE(patternId1, patternId2);
514 } 563 }
515 564
516 } // namespace extensions 565 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698