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

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: 1 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 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
197 EXPECT_EQ(URLMatcherCondition::HOST_EQUALS_PATH_PREFIX, 197 EXPECT_EQ(URLMatcherCondition::HOST_EQUALS_PATH_PREFIX,
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());
battre 2012/09/12 18:04:50 Can you add one more test for the regex matching h
Yoyo Zhou 2012/09/12 20:25:56 Done.
208 } 208 }
209 209
210 TEST(URLMatcherConditionFactoryTest, TestSingletonProperty) { 210 TEST(URLMatcherConditionFactoryTest, TestSingletonProperty) {
211 URLMatcherConditionFactory factory; 211 URLMatcherConditionFactory factory;
212 URLMatcherCondition c1 = factory.CreateHostEqualsCondition("www.google.com"); 212 URLMatcherCondition c1 = factory.CreateHostEqualsCondition("www.google.com");
213 URLMatcherCondition c2 = factory.CreateHostEqualsCondition("www.google.com"); 213 URLMatcherCondition c2 = factory.CreateHostEqualsCondition("www.google.com");
214 EXPECT_EQ(c1.criterion(), c2.criterion()); 214 EXPECT_EQ(c1.criterion(), c2.criterion());
215 EXPECT_EQ(c1.substring_pattern(), c2.substring_pattern()); 215 EXPECT_EQ(c1.string_pattern(), c2.string_pattern());
216 URLMatcherCondition c3 = factory.CreateHostEqualsCondition("www.google.de"); 216 URLMatcherCondition c3 = factory.CreateHostEqualsCondition("www.google.de");
217 EXPECT_EQ(c2.criterion(), c3.criterion()); 217 EXPECT_EQ(c2.criterion(), c3.criterion());
218 EXPECT_NE(c2.substring_pattern(), c3.substring_pattern()); 218 EXPECT_NE(c2.string_pattern(), c3.string_pattern());
219 EXPECT_NE(c2.substring_pattern()->id(), c3.substring_pattern()->id()); 219 EXPECT_NE(c2.string_pattern()->id(), c3.string_pattern()->id());
220 EXPECT_NE(c2.substring_pattern()->pattern(), 220 EXPECT_NE(c2.string_pattern()->pattern(),
221 c3.substring_pattern()->pattern()); 221 c3.string_pattern()->pattern());
222 222
223 // Check that all SubstringPattern singletons are freed if we call 223 // Check that all StringPattern singletons are freed if we call
224 // ForgetUnusedPatterns. 224 // ForgetUnusedPatterns.
battre 2012/09/12 18:04:50 can you test that this also holds for regex single
Yoyo Zhou 2012/09/12 20:25:56 Yes. I also made this test check that regex patter
225 SubstringPattern::ID old_id_1 = c1.substring_pattern()->id(); 225 StringPattern::ID old_id_1 = c1.string_pattern()->id();
226 factory.ForgetUnusedPatterns(std::set<SubstringPattern::ID>()); 226 factory.ForgetUnusedPatterns(std::set<StringPattern::ID>());
227 EXPECT_TRUE(factory.IsEmpty()); 227 EXPECT_TRUE(factory.IsEmpty());
228 URLMatcherCondition c4 = factory.CreateHostEqualsCondition("www.google.com"); 228 URLMatcherCondition c4 = factory.CreateHostEqualsCondition("www.google.com");
229 EXPECT_NE(old_id_1, c4.substring_pattern()->id()); 229 EXPECT_NE(old_id_1, c4.string_pattern()->id());
230 } 230 }
231 231
232 TEST(URLMatcherConditionFactoryTest, TestComponentSearches) { 232 TEST(URLMatcherConditionFactoryTest, TestComponentSearches) {
233 GURL gurl("https://www.google.com:1234/webhp?sourceid=chrome-instant&ie=UTF-8" 233 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"); 234 "&ion=1#hl=en&output=search&sclient=psy-ab&q=chrome%20is%20awesome");
235 URLMatcherConditionFactory factory; 235 URLMatcherConditionFactory factory;
236 std::string url = factory.CanonicalizeURLForComponentSearches(gurl); 236 std::string url = factory.CanonicalizeURLForComponentSearches(gurl);
237 237
238 // Test host component. 238 // Test host component.
239 EXPECT_TRUE(Matches(factory.CreateHostPrefixCondition(""), url)); 239 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. 367 // Same as above but this time with a non-standard port.
368 gurl = GURL("https://www.google.com:1234/webhp?sourceid=chrome-instant&" 368 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" 369 "ie=UTF-8&ion=1#hl=en&output=search&sclient=psy-ab&q=chrome%20is%20"
370 "awesome"); 370 "awesome");
371 url = factory.CanonicalizeURLForFullSearches(gurl); 371 url = factory.CanonicalizeURLForFullSearches(gurl);
372 EXPECT_TRUE(Matches(factory.CreateURLPrefixCondition( 372 EXPECT_TRUE(Matches(factory.CreateURLPrefixCondition(
373 "https://www.google.com:1234/webhp?"), url)); 373 "https://www.google.com:1234/webhp?"), url));
374 EXPECT_TRUE(Matches(factory.CreateURLContainsCondition(":1234"), url)); 374 EXPECT_TRUE(Matches(factory.CreateURLContainsCondition(":1234"), url));
375 } 375 }
376 376
377
378 // 377 //
379 // URLMatcherConditionSet 378 // URLMatcherConditionSet
380 // 379 //
381 380
382 TEST(URLMatcherConditionSetTest, Constructor) { 381 TEST(URLMatcherConditionSetTest, Constructor) {
383 URLMatcherConditionFactory factory; 382 URLMatcherConditionFactory factory;
384 URLMatcherCondition m1 = factory.CreateHostSuffixCondition("example.com"); 383 URLMatcherCondition m1 = factory.CreateHostSuffixCondition("example.com");
385 URLMatcherCondition m2 = factory.CreatePathContainsCondition("foo"); 384 URLMatcherCondition m2 = factory.CreatePathContainsCondition("foo");
386 385
387 std::set<URLMatcherCondition> conditions; 386 std::set<URLMatcherCondition> conditions;
(...skipping 18 matching lines...) Expand all
406 405
407 std::set<URLMatcherCondition> conditions; 406 std::set<URLMatcherCondition> conditions;
408 conditions.insert(m1); 407 conditions.insert(m1);
409 conditions.insert(m2); 408 conditions.insert(m2);
410 409
411 scoped_refptr<URLMatcherConditionSet> condition_set( 410 scoped_refptr<URLMatcherConditionSet> condition_set(
412 new URLMatcherConditionSet(1, conditions)); 411 new URLMatcherConditionSet(1, conditions));
413 EXPECT_EQ(1, condition_set->id()); 412 EXPECT_EQ(1, condition_set->id());
414 EXPECT_EQ(2u, condition_set->conditions().size()); 413 EXPECT_EQ(2u, condition_set->conditions().size());
415 414
416 std::set<SubstringPattern::ID> matching_substring_patterns; 415 std::set<StringPattern::ID> matching_patterns;
417 matching_substring_patterns.insert(m1.substring_pattern()->id()); 416 matching_patterns.insert(m1.string_pattern()->id());
418 EXPECT_FALSE(condition_set->IsMatch(matching_substring_patterns, url1)); 417 EXPECT_FALSE(condition_set->IsMatch(matching_patterns, url1));
419 418
420 matching_substring_patterns.insert(m2.substring_pattern()->id()); 419 matching_patterns.insert(m2.string_pattern()->id());
421 EXPECT_TRUE(condition_set->IsMatch(matching_substring_patterns, url1)); 420 EXPECT_TRUE(condition_set->IsMatch(matching_patterns, url1));
422 EXPECT_FALSE(condition_set->IsMatch(matching_substring_patterns, url2)); 421 EXPECT_FALSE(condition_set->IsMatch(matching_patterns, url2));
423 422
424 // Test scheme filters. 423 // Test scheme filters.
425 scoped_refptr<URLMatcherConditionSet> condition_set2( 424 scoped_refptr<URLMatcherConditionSet> condition_set2(
426 new URLMatcherConditionSet(1, conditions, 425 new URLMatcherConditionSet(1, conditions,
427 scoped_ptr<URLMatcherSchemeFilter>( 426 scoped_ptr<URLMatcherSchemeFilter>(
428 new URLMatcherSchemeFilter("https")), 427 new URLMatcherSchemeFilter("https")),
429 scoped_ptr<URLMatcherPortFilter>(NULL))); 428 scoped_ptr<URLMatcherPortFilter>(NULL)));
430 EXPECT_FALSE(condition_set2->IsMatch(matching_substring_patterns, url1)); 429 EXPECT_FALSE(condition_set2->IsMatch(matching_patterns, url1));
431 scoped_refptr<URLMatcherConditionSet> condition_set3( 430 scoped_refptr<URLMatcherConditionSet> condition_set3(
432 new URLMatcherConditionSet(1, conditions, 431 new URLMatcherConditionSet(1, conditions,
433 scoped_ptr<URLMatcherSchemeFilter>( 432 scoped_ptr<URLMatcherSchemeFilter>(
434 new URLMatcherSchemeFilter("http")), 433 new URLMatcherSchemeFilter("http")),
435 scoped_ptr<URLMatcherPortFilter>(NULL))); 434 scoped_ptr<URLMatcherPortFilter>(NULL)));
436 EXPECT_TRUE(condition_set3->IsMatch(matching_substring_patterns, url1)); 435 EXPECT_TRUE(condition_set3->IsMatch(matching_patterns, url1));
437 436
438 // Test port filters. 437 // Test port filters.
439 std::vector<URLMatcherPortFilter::Range> ranges; 438 std::vector<URLMatcherPortFilter::Range> ranges;
440 ranges.push_back(URLMatcherPortFilter::CreateRange(80)); 439 ranges.push_back(URLMatcherPortFilter::CreateRange(80));
441 scoped_ptr<URLMatcherPortFilter> filter(new URLMatcherPortFilter(ranges)); 440 scoped_ptr<URLMatcherPortFilter> filter(new URLMatcherPortFilter(ranges));
442 scoped_refptr<URLMatcherConditionSet> condition_set4( 441 scoped_refptr<URLMatcherConditionSet> condition_set4(
443 new URLMatcherConditionSet(1, conditions, 442 new URLMatcherConditionSet(1, conditions,
444 scoped_ptr<URLMatcherSchemeFilter>(NULL), filter.Pass())); 443 scoped_ptr<URLMatcherSchemeFilter>(NULL), filter.Pass()));
445 EXPECT_TRUE(condition_set4->IsMatch(matching_substring_patterns, url1)); 444 EXPECT_TRUE(condition_set4->IsMatch(matching_patterns, url1));
446 EXPECT_TRUE(condition_set4->IsMatch(matching_substring_patterns, url3)); 445 EXPECT_TRUE(condition_set4->IsMatch(matching_patterns, url3));
447 EXPECT_FALSE(condition_set4->IsMatch(matching_substring_patterns, url4)); 446 EXPECT_FALSE(condition_set4->IsMatch(matching_patterns, url4));
447
448 // Test regex patterns.
449 matching_patterns.clear();
450 URLMatcherCondition r1 = factory.CreateURLMatchesCondition("/fo?oo");
451 std::set<URLMatcherCondition> regex_conditions;
452 regex_conditions.insert(r1);
453 scoped_refptr<URLMatcherConditionSet> condition_set5(
454 new URLMatcherConditionSet(1, regex_conditions));
455 EXPECT_FALSE(condition_set5->IsMatch(matching_patterns, url1));
456 matching_patterns.insert(r1.string_pattern()->id());
457 EXPECT_TRUE(condition_set5->IsMatch(matching_patterns, url1));
458
459 regex_conditions.insert(m1);
460 scoped_refptr<URLMatcherConditionSet> condition_set6(
461 new URLMatcherConditionSet(1, regex_conditions));
462 EXPECT_FALSE(condition_set6->IsMatch(matching_patterns, url1));
463 matching_patterns.insert(m1.string_pattern()->id());
464 EXPECT_TRUE(condition_set6->IsMatch(matching_patterns, url1));
448 } 465 }
449 466
450 467
451 // 468 //
452 // URLMatcher 469 // URLMatcher
453 // 470 //
454 471
455 TEST(URLMatcherTest, FullTest) { 472 TEST(URLMatcherTest, FullTest) {
456 GURL url1("http://www.example.com/foo?bar=1"); 473 GURL url1("http://www.example.com/foo?bar=1");
457 GURL url2("http://foo.example.com/index.html"); 474 GURL url2("http://foo.example.com/index.html");
(...skipping 21 matching lines...) Expand all
479 const int kConditionSetId2 = 2; 496 const int kConditionSetId2 = 2;
480 URLMatcherConditionSet::Vector insert2; 497 URLMatcherConditionSet::Vector insert2;
481 insert2.push_back(make_scoped_refptr( 498 insert2.push_back(make_scoped_refptr(
482 new URLMatcherConditionSet(kConditionSetId2, conditions2))); 499 new URLMatcherConditionSet(kConditionSetId2, conditions2)));
483 matcher.AddConditionSets(insert2); 500 matcher.AddConditionSets(insert2);
484 EXPECT_EQ(2u, matcher.MatchURL(url1).size()); 501 EXPECT_EQ(2u, matcher.MatchURL(url1).size());
485 EXPECT_EQ(1u, matcher.MatchURL(url2).size()); 502 EXPECT_EQ(1u, matcher.MatchURL(url2).size());
486 503
487 // This should be the cached singleton. 504 // This should be the cached singleton.
488 int patternId1 = factory->CreateHostSuffixCondition( 505 int patternId1 = factory->CreateHostSuffixCondition(
489 "example.com").substring_pattern()->id(); 506 "example.com").string_pattern()->id();
490 507
491 // Removal of last insert. 508 // Third insert.
509 URLMatcherConditionSet::Conditions conditions3;
510 conditions3.insert(factory->CreateHostSuffixCondition("example.com"));
511 conditions3.insert(factory->CreateURLMatchesCondition("x.*[0-9]"));
512
513 const int kConditionSetId3 = 3;
514 URLMatcherConditionSet::Vector insert3;
515 insert3.push_back(make_scoped_refptr(
516 new URLMatcherConditionSet(kConditionSetId3, conditions3)));
517 matcher.AddConditionSets(insert3);
518 EXPECT_EQ(3u, matcher.MatchURL(url1).size());
519 EXPECT_EQ(1u, matcher.MatchURL(url2).size());
520
521 // Removal of third insert.
522 std::vector<URLMatcherConditionSet::ID> remove3;
523 remove3.push_back(kConditionSetId3);
524 matcher.RemoveConditionSets(remove3);
525 EXPECT_EQ(2u, matcher.MatchURL(url1).size());
526 EXPECT_EQ(1u, matcher.MatchURL(url2).size());
527
528 // Removal of second insert.
492 std::vector<URLMatcherConditionSet::ID> remove2; 529 std::vector<URLMatcherConditionSet::ID> remove2;
493 remove2.push_back(kConditionSetId2); 530 remove2.push_back(kConditionSetId2);
494 matcher.RemoveConditionSets(remove2); 531 matcher.RemoveConditionSets(remove2);
495 EXPECT_EQ(1u, matcher.MatchURL(url1).size()); 532 EXPECT_EQ(1u, matcher.MatchURL(url1).size());
496 EXPECT_EQ(0u, matcher.MatchURL(url2).size()); 533 EXPECT_EQ(0u, matcher.MatchURL(url2).size());
497 534
498 // Removal of first insert. 535 // Removal of first insert.
499 std::vector<URLMatcherConditionSet::ID> remove1; 536 std::vector<URLMatcherConditionSet::ID> remove1;
500 remove1.push_back(kConditionSetId1); 537 remove1.push_back(kConditionSetId1);
501 matcher.RemoveConditionSets(remove1); 538 matcher.RemoveConditionSets(remove1);
502 EXPECT_EQ(0u, matcher.MatchURL(url1).size()); 539 EXPECT_EQ(0u, matcher.MatchURL(url1).size());
503 EXPECT_EQ(0u, matcher.MatchURL(url2).size()); 540 EXPECT_EQ(0u, matcher.MatchURL(url2).size());
504 541
505 EXPECT_TRUE(matcher.IsEmpty()); 542 EXPECT_TRUE(matcher.IsEmpty());
506 543
507 // The cached singleton in matcher.condition_factory_ should be destroyed to 544 // The cached singleton in matcher.condition_factory_ should be destroyed to
508 // free memory. 545 // free memory.
509 int patternId2 = factory->CreateHostSuffixCondition( 546 int patternId2 = factory->CreateHostSuffixCondition(
510 "example.com").substring_pattern()->id(); 547 "example.com").string_pattern()->id();
511 // If patternId1 and patternId2 are different that indicates that 548 // If patternId1 and patternId2 are different that indicates that
512 // matcher.condition_factory_ does not leak memory. 549 // matcher.condition_factory_ does not leak memory by holding onto
550 // unused patterns.
513 EXPECT_NE(patternId1, patternId2); 551 EXPECT_NE(patternId1, patternId2);
514 } 552 }
515 553
516 } // namespace extensions 554 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698