| 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/browser/extensions/api/declarative/url_matcher.h" |
| 6 |
| 7 #include "base/string_util.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" |
| 9 |
| 10 namespace extensions { |
| 11 |
| 12 TEST(URLMatcherConditionTest, Constructor) { |
| 13 SubstringPattern pattern("example.com", 1); |
| 14 URLMatcherCondition m1(URLMatcherCondition::HOST_SUFFIX, &pattern); |
| 15 EXPECT_EQ(URLMatcherCondition::HOST_SUFFIX, m1.criterion()); |
| 16 EXPECT_EQ(&pattern, m1.substring_pattern()); |
| 17 } |
| 18 |
| 19 TEST(URLMatcherConditionTest, IsFullUrlCondition) { |
| 20 SubstringPattern pattern("example.com", 1); |
| 21 EXPECT_FALSE(URLMatcherCondition(URLMatcherCondition::HOST_SUFFIX, |
| 22 &pattern).IsFullUrlCondition()); |
| 23 |
| 24 EXPECT_TRUE(URLMatcherCondition(URLMatcherCondition::HOST_CONTAINS, |
| 25 &pattern).IsFullUrlCondition()); |
| 26 EXPECT_TRUE(URLMatcherCondition(URLMatcherCondition::PATH_CONTAINS, |
| 27 &pattern).IsFullUrlCondition()); |
| 28 EXPECT_TRUE(URLMatcherCondition(URLMatcherCondition::QUERY_CONTAINS, |
| 29 &pattern).IsFullUrlCondition()); |
| 30 |
| 31 EXPECT_TRUE(URLMatcherCondition(URLMatcherCondition::URL_PREFIX, |
| 32 &pattern).IsFullUrlCondition()); |
| 33 EXPECT_TRUE(URLMatcherCondition(URLMatcherCondition::URL_SUFFIX, |
| 34 &pattern).IsFullUrlCondition()); |
| 35 EXPECT_TRUE(URLMatcherCondition(URLMatcherCondition::URL_CONTAINS, |
| 36 &pattern).IsFullUrlCondition()); |
| 37 EXPECT_TRUE(URLMatcherCondition(URLMatcherCondition::URL_EQUALS, |
| 38 &pattern).IsFullUrlCondition()); |
| 39 } |
| 40 |
| 41 TEST(URLMatcherConditionTest, SubstringMatching) { |
| 42 // TODO(battre): Re-implement this. |
| 43 /* |
| 44 URLMatcherConditionFactory condition_factory; |
| 45 |
| 46 // Check that substring_pattern is filled. |
| 47 URLMatcherCondition m1(URLMatcherCondition::HOST_SUFFIX, "example.com"); |
| 48 EXPECT_EQ(condition_factory.CreateHostSuffixPattern("example.com"), |
| 49 m1.substring_pattern()); |
| 50 |
| 51 // Check that patterns are recycled. |
| 52 URLMatcherCondition m2(URLMatcherCondition::HOST_SUFFIX, "example.com"); |
| 53 m2.BuildSubstringPattern(&condition_factory); |
| 54 EXPECT_EQ(m1.substring_pattern(), m1.substring_pattern()); |
| 55 |
| 56 // Check that SubstringPattern::ID are handled correctly. |
| 57 GURL url("http://www.example.com/index.html"); |
| 58 |
| 59 std::set<SubstringPattern::ID> not_matching; |
| 60 not_matching.insert(m1.substring_pattern().id() + 1); |
| 61 EXPECT_FALSE(m1.IsMatch(not_matching, url)); |
| 62 |
| 63 std::set<SubstringPattern::ID> matching; |
| 64 matching.insert(m1.substring_pattern().id()); |
| 65 EXPECT_TRUE(m1.IsMatch(matching, url)); |
| 66 |
| 67 // Check that URLs are tested correctly for containment. |
| 68 URLMatcherCondition m3(URLMatcherCondition::HOST_CONTAINS, "example"); |
| 69 m3.BuildSubstringPattern(&condition_factory); |
| 70 matching.insert(m3.substring_pattern().id()); |
| 71 EXPECT_TRUE(m3.IsFullUrlCondition()); |
| 72 EXPECT_FALSE(m3.IsMatch(matching, GURL("http://www.foo.com/example"))); |
| 73 EXPECT_TRUE(m3.IsMatch(matching, GURL("http://www.example.com/foo"))); |
| 74 */ |
| 75 } |
| 76 |
| 77 |
| 78 namespace { |
| 79 |
| 80 bool Matches(scoped_ptr<URLMatcherCondition> condition, std::string text) { |
| 81 return text.find(condition->substring_pattern()->pattern()) != |
| 82 std::string::npos; |
| 83 } |
| 84 |
| 85 } // namespace |
| 86 |
| 87 TEST(URLMatcherConditionFactoryTest, GURLCharacterSet) { |
| 88 // GURL guarantees that neither domain, nor path, nor query may contain |
| 89 // non ASCII-7 characters. We test this here, because a change to this |
| 90 // guarantee breaks this implementation horribly. |
| 91 GURL url("http://www.föö.com/föö?föö#föö"); |
| 92 EXPECT_TRUE(IsStringASCII(url.host())); |
| 93 EXPECT_TRUE(IsStringASCII(url.path())); |
| 94 EXPECT_TRUE(IsStringASCII(url.query())); |
| 95 EXPECT_FALSE(IsStringASCII(url.ref())); |
| 96 } |
| 97 |
| 98 // Basic tests of SubstringPattern objects |
| 99 TEST(URLMatcherConditionFactoryTest, TestSingletonProperty) { |
| 100 // TODO(battre): Re-implement this. |
| 101 /*URLMatcherConditionFactory matcher; |
| 102 SubstringPattern p1 = matcher.CreateHostEqualsCondition("www.google.com"); |
| 103 SubstringPattern p2 = matcher.CreateHostEqualsCondition("www.google.com"); |
| 104 EXPECT_EQ(p1, p2); |
| 105 SubstringPattern p3 = matcher.CreateHostEqualsCondition("www.google.de"); |
| 106 EXPECT_NE(p2.id(), p3.id()); |
| 107 EXPECT_NE(p2.pattern(), p3.pattern());*/ |
| 108 } |
| 109 |
| 110 TEST(URLMatcherConditionFactoryTest, TestComponentSearches) { |
| 111 GURL gurl("https://www.google.com/webhp?sourceid=chrome-instant&ie=UTF-8" |
| 112 "&ion=1#hl=en&output=search&sclient=psy-ab&q=chrome%20is%20awesome"); |
| 113 URLMatcherConditionFactory matcher; |
| 114 std::string url = matcher.CanonlicalizeURLForComponentSearches(gurl); |
| 115 |
| 116 // Test host component. |
| 117 EXPECT_TRUE(Matches(matcher.CreateHostPrefixCondition(""), url)); |
| 118 EXPECT_TRUE(Matches(matcher.CreateHostPrefixCondition("www.goog"), url)); |
| 119 EXPECT_TRUE( |
| 120 Matches(matcher.CreateHostPrefixCondition("www.google.com"), url)); |
| 121 EXPECT_TRUE( |
| 122 Matches(matcher.CreateHostPrefixCondition(".www.google.com"), url)); |
| 123 EXPECT_FALSE(Matches(matcher.CreateHostPrefixCondition("google.com"), url)); |
| 124 EXPECT_FALSE( |
| 125 Matches(matcher.CreateHostPrefixCondition("www.google.com/"), url)); |
| 126 EXPECT_FALSE(Matches(matcher.CreateHostPrefixCondition("webhp"), url)); |
| 127 |
| 128 EXPECT_TRUE(Matches(matcher.CreateHostSuffixCondition(""), url)); |
| 129 EXPECT_TRUE(Matches(matcher.CreateHostSuffixCondition("com"), url)); |
| 130 EXPECT_TRUE(Matches(matcher.CreateHostSuffixCondition(".com"), url)); |
| 131 EXPECT_TRUE( |
| 132 Matches(matcher.CreateHostSuffixCondition("www.google.com"), url)); |
| 133 EXPECT_TRUE( |
| 134 Matches(matcher.CreateHostSuffixCondition(".www.google.com"), url)); |
| 135 EXPECT_FALSE(Matches(matcher.CreateHostSuffixCondition("www"), url)); |
| 136 EXPECT_FALSE( |
| 137 Matches(matcher.CreateHostSuffixCondition("www.google.com/"), url)); |
| 138 EXPECT_FALSE(Matches(matcher.CreateHostSuffixCondition("webhp"), url)); |
| 139 |
| 140 EXPECT_FALSE(Matches(matcher.CreateHostEqualsCondition(""), url)); |
| 141 EXPECT_FALSE(Matches(matcher.CreateHostEqualsCondition("www"), url)); |
| 142 EXPECT_TRUE( |
| 143 Matches(matcher.CreateHostEqualsCondition("www.google.com"), url)); |
| 144 EXPECT_FALSE( |
| 145 Matches(matcher.CreateHostEqualsCondition("www.google.com/"), url)); |
| 146 |
| 147 |
| 148 // Test path component. |
| 149 EXPECT_TRUE(Matches(matcher.CreatePathPrefixCondition(""), url)); |
| 150 EXPECT_TRUE(Matches(matcher.CreatePathPrefixCondition("/web"), url)); |
| 151 EXPECT_TRUE(Matches(matcher.CreatePathPrefixCondition("/webhp"), url)); |
| 152 EXPECT_FALSE(Matches(matcher.CreatePathPrefixCondition("webhp"), url)); |
| 153 EXPECT_FALSE(Matches(matcher.CreatePathPrefixCondition("/webhp?"), url)); |
| 154 |
| 155 EXPECT_TRUE(Matches(matcher.CreatePathSuffixCondition(""), url)); |
| 156 EXPECT_TRUE(Matches(matcher.CreatePathSuffixCondition("webhp"), url)); |
| 157 EXPECT_TRUE(Matches(matcher.CreatePathSuffixCondition("/webhp"), url)); |
| 158 EXPECT_FALSE(Matches(matcher.CreatePathSuffixCondition("/web"), url)); |
| 159 EXPECT_FALSE(Matches(matcher.CreatePathSuffixCondition("/webhp?"), url)); |
| 160 |
| 161 EXPECT_TRUE(Matches(matcher.CreatePathEqualsCondition("/webhp"), url)); |
| 162 EXPECT_FALSE(Matches(matcher.CreatePathEqualsCondition("webhp"), url)); |
| 163 EXPECT_FALSE(Matches(matcher.CreatePathEqualsCondition("/webhp?"), url)); |
| 164 EXPECT_FALSE( |
| 165 Matches(matcher.CreatePathEqualsCondition("www.google.com"), url)); |
| 166 |
| 167 |
| 168 // Test query component. |
| 169 EXPECT_TRUE(Matches(matcher.CreateQueryPrefixCondition(""), url)); |
| 170 EXPECT_TRUE(Matches(matcher.CreateQueryPrefixCondition("?sourceid"), url)); |
| 171 EXPECT_FALSE(Matches(matcher.CreatePathPrefixCondition("sourceid"), url)); |
| 172 |
| 173 EXPECT_TRUE(Matches(matcher.CreateQuerySuffixCondition(""), url)); |
| 174 EXPECT_TRUE(Matches(matcher.CreateQuerySuffixCondition("ion=1"), url)); |
| 175 EXPECT_FALSE(Matches(matcher.CreatePathPrefixCondition("?sourceid"), url)); |
| 176 EXPECT_FALSE(Matches(matcher.CreateQuerySuffixCondition("www"), url)); |
| 177 |
| 178 EXPECT_TRUE(Matches(matcher.CreateQueryEqualsCondition( |
| 179 "?sourceid=chrome-instant&ie=UTF-8&ion=1"), url)); |
| 180 EXPECT_FALSE(Matches(matcher.CreateQueryEqualsCondition( |
| 181 "sourceid=chrome-instant&ie=UTF-8&ion="), url)); |
| 182 EXPECT_FALSE( |
| 183 Matches(matcher.CreateQueryEqualsCondition("www.google.com"), url)); |
| 184 |
| 185 |
| 186 // Test adjacent components |
| 187 EXPECT_TRUE(Matches(matcher.CreateHostSuffixPathPrefixCondition( |
| 188 "google.com", "/webhp"), url)); |
| 189 EXPECT_TRUE(Matches(matcher.CreateHostSuffixPathPrefixCondition( |
| 190 "", "/webhp"), url)); |
| 191 EXPECT_TRUE(Matches(matcher.CreateHostSuffixPathPrefixCondition( |
| 192 "google.com", ""), url)); |
| 193 EXPECT_FALSE(Matches(matcher.CreateHostSuffixPathPrefixCondition( |
| 194 "www", ""), url)); |
| 195 } |
| 196 |
| 197 TEST(URLMatcherConditionFactoryTest, TestFullSearches) { |
| 198 GURL gurl("https://www.google.com/webhp?sourceid=chrome-instant&ie=UTF-8" |
| 199 "&ion=1#hl=en&output=search&sclient=psy-ab&q=chrome%20is%20awesome"); |
| 200 URLMatcherConditionFactory matcher; |
| 201 std::string url = matcher.CanonlicalizeURLForFullSearches(gurl); |
| 202 |
| 203 EXPECT_TRUE(Matches(matcher.CreateURLPrefixCondition(""), url)); |
| 204 EXPECT_TRUE(Matches(matcher.CreateURLPrefixCondition("www.goog"), url)); |
| 205 EXPECT_TRUE(Matches(matcher.CreateURLPrefixCondition("www.google.com"), url)); |
| 206 EXPECT_TRUE( |
| 207 Matches(matcher.CreateURLPrefixCondition(".www.google.com"), url)); |
| 208 EXPECT_TRUE( |
| 209 Matches(matcher.CreateURLPrefixCondition("www.google.com/"), url)); |
| 210 EXPECT_FALSE(Matches(matcher.CreateURLPrefixCondition("webhp"), url)); |
| 211 |
| 212 EXPECT_TRUE(Matches(matcher.CreateURLSuffixCondition(""), url)); |
| 213 EXPECT_TRUE(Matches(matcher.CreateURLSuffixCondition("ion=1"), url)); |
| 214 EXPECT_FALSE(Matches(matcher.CreateURLSuffixCondition("www"), url)); |
| 215 |
| 216 EXPECT_TRUE(Matches(matcher.CreateURLContainsCondition(""), url)); |
| 217 EXPECT_TRUE(Matches(matcher.CreateURLContainsCondition("www.goog"), url)); |
| 218 EXPECT_TRUE(Matches(matcher.CreateURLContainsCondition(".www.goog"), url)); |
| 219 EXPECT_TRUE(Matches(matcher.CreateURLContainsCondition("webhp"), url)); |
| 220 EXPECT_TRUE(Matches(matcher.CreateURLContainsCondition("?"), url)); |
| 221 EXPECT_TRUE(Matches(matcher.CreateURLContainsCondition("sourceid"), url)); |
| 222 EXPECT_TRUE(Matches(matcher.CreateURLContainsCondition("ion=1"), url)); |
| 223 EXPECT_FALSE(Matches(matcher.CreateURLContainsCondition("foobar"), url)); |
| 224 EXPECT_FALSE(Matches(matcher.CreateURLContainsCondition("search"), url)); |
| 225 |
| 226 EXPECT_TRUE(Matches(matcher.CreateURLEqualsCondition( |
| 227 "www.google.com/webhp?sourceid=chrome-instant&ie=UTF-8&ion=1"), url)); |
| 228 EXPECT_FALSE( |
| 229 Matches(matcher.CreateURLEqualsCondition("www.google.com"), url)); |
| 230 } |
| 231 |
| 232 /* |
| 233 // TODO(battre): Re-implement this. |
| 234 TEST(URLMatcherConditionSetTest, Constructors) { |
| 235 URLMatcherConditionFactory condition_factory; |
| 236 URLMatcherCondition m1(URLMatcherCondition::HOST_SUFFIX, "example.com"); |
| 237 m1.BuildSubstringPattern(&condition_factory); |
| 238 URLMatcherCondition m2(URLMatcherCondition::PATH_CONTAINS, "foo"); |
| 239 m2.BuildSubstringPattern(&condition_factory); |
| 240 |
| 241 std::vector<URLMatcherCondition> conditions; |
| 242 conditions.push_back(m1); |
| 243 conditions.push_back(m2); |
| 244 |
| 245 URLMatcherConditionSet condition_set(1, conditions); |
| 246 EXPECT_EQ(1, condition_set.id()); |
| 247 EXPECT_EQ(2u, condition_set.conditions().size()); |
| 248 |
| 249 std::vector<URLMatcherCondition> other_conditions; |
| 250 other_conditions.push_back(m1); |
| 251 URLMatcherConditionSet condition_set2(2, other_conditions); |
| 252 condition_set2 = condition_set; |
| 253 EXPECT_EQ(1, condition_set2.id()); |
| 254 EXPECT_EQ(2u, condition_set2.conditions().size()); |
| 255 |
| 256 URLMatcherConditionSet condition_set3(condition_set); |
| 257 EXPECT_EQ(1, condition_set2.id()); |
| 258 EXPECT_EQ(2u, condition_set2.conditions().size()); |
| 259 } |
| 260 |
| 261 TEST(URLMatcherConditionSetTest, Matching) { |
| 262 GURL url1("http://www.example.com/foo?bar=1"); |
| 263 GURL url2("http://foo.example.com/index.html"); |
| 264 |
| 265 URLMatcherConditionFactory condition_factory; |
| 266 URLMatcherCondition m1(URLMatcherCondition::HOST_SUFFIX, "example.com"); |
| 267 m1.BuildSubstringPattern(&condition_factory); |
| 268 URLMatcherCondition m2(URLMatcherCondition::PATH_CONTAINS, "foo"); |
| 269 m2.BuildSubstringPattern(&condition_factory); |
| 270 |
| 271 std::vector<URLMatcherCondition> conditions; |
| 272 conditions.push_back(m1); |
| 273 conditions.push_back(m2); |
| 274 |
| 275 URLMatcherConditionSet condition_set(1, conditions); |
| 276 EXPECT_EQ(1, condition_set.id()); |
| 277 EXPECT_EQ(2u, condition_set.conditions().size()); |
| 278 |
| 279 std::set<SubstringPattern::ID> matching_substring_patterns; |
| 280 matching_substring_patterns.insert(m1.substring_pattern().id()); |
| 281 EXPECT_FALSE(condition_set.IsMatch(matching_substring_patterns, url1)); |
| 282 |
| 283 matching_substring_patterns.insert(m2.substring_pattern().id()); |
| 284 EXPECT_TRUE(condition_set.IsMatch(matching_substring_patterns, url1)); |
| 285 EXPECT_FALSE(condition_set.IsMatch(matching_substring_patterns, url2)); |
| 286 } |
| 287 */ |
| 288 |
| 289 TEST(URLMatcherTest, FullTest) { |
| 290 GURL url1("http://www.example.com/foo?bar=1"); |
| 291 GURL url2("http://foo.example.com/index.html"); |
| 292 |
| 293 URLMatcher matcher; |
| 294 URLMatcherConditionFactory* factory = matcher.condition_factory(); |
| 295 |
| 296 // First insert. |
| 297 scoped_ptr<URLMatcherConditionSet::Conditions> conditions1( |
| 298 new URLMatcherConditionSet::Conditions()); |
| 299 conditions1->push_back( |
| 300 factory->CreateHostSuffixCondition("example.com").release()); |
| 301 conditions1->push_back( |
| 302 factory->CreatePathContainsCondition("foo").release()); |
| 303 |
| 304 const int kConditionSetId1 = 1; |
| 305 scoped_ptr<ScopedVector<const URLMatcherConditionSet> > insert1( |
| 306 new ScopedVector<const URLMatcherConditionSet>()); |
| 307 insert1->push_back( |
| 308 new URLMatcherConditionSet(kConditionSetId1, conditions1.Pass())); |
| 309 matcher.AddConditionSets(insert1.Pass()); |
| 310 EXPECT_EQ(1u, matcher.MatchUrl(url1).size()); |
| 311 EXPECT_EQ(0u, matcher.MatchUrl(url2).size()); |
| 312 |
| 313 // Second insert. |
| 314 scoped_ptr<URLMatcherConditionSet::Conditions> conditions2( |
| 315 new URLMatcherConditionSet::Conditions()); |
| 316 conditions2->push_back( |
| 317 factory->CreateHostSuffixCondition("example.com").release()); |
| 318 |
| 319 const int kConditionSetId2 = 2; |
| 320 scoped_ptr<ScopedVector<const URLMatcherConditionSet> > insert2( |
| 321 new ScopedVector<const URLMatcherConditionSet>()); |
| 322 insert2->push_back( |
| 323 new URLMatcherConditionSet(kConditionSetId2, conditions2.Pass())); |
| 324 matcher.AddConditionSets(insert2.Pass()); |
| 325 EXPECT_EQ(2u, matcher.MatchUrl(url1).size()); |
| 326 EXPECT_EQ(1u, matcher.MatchUrl(url2).size()); |
| 327 |
| 328 // This should be the cached singleton. |
| 329 int patternId1 = factory->CreateHostSuffixCondition( |
| 330 "example.com")->substring_pattern()->id(); |
| 331 |
| 332 // Removal of last insert. |
| 333 std::vector<URLMatcherConditionSet::ID> remove2; |
| 334 remove2.push_back(kConditionSetId2); |
| 335 matcher.RemoveConditionSets(remove2); |
| 336 EXPECT_EQ(1u, matcher.MatchUrl(url1).size()); |
| 337 EXPECT_EQ(0u, matcher.MatchUrl(url2).size()); |
| 338 |
| 339 // Removal of first insert. |
| 340 std::vector<URLMatcherConditionSet::ID> remove1; |
| 341 remove1.push_back(kConditionSetId1); |
| 342 matcher.RemoveConditionSets(remove1); |
| 343 EXPECT_EQ(0u, matcher.MatchUrl(url1).size()); |
| 344 EXPECT_EQ(0u, matcher.MatchUrl(url2).size()); |
| 345 |
| 346 // The cached singleton in matcher.condition_factory_ should be destroyed to |
| 347 // free memory. |
| 348 int patternId2 = factory->CreateHostSuffixCondition( |
| 349 "example.com")->substring_pattern()->id(); |
| 350 // If patternId1 and patternId2 are different that indicates that |
| 351 // matcher.condition_factory_ does not leak memory. |
| 352 EXPECT_NE(patternId1, patternId2); |
| 353 } |
| 354 |
| 355 } // namespace extensions |
| OLD | NEW |