[email protected] | 716c016 | 2013-12-13 20:36:53 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors. All rights reserved. |
[email protected] | fb5bcc0 | 2012-02-17 14:05:42 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 716c016 | 2013-12-13 20:36:53 | [diff] [blame] | 5 | #include "components/url_matcher/url_matcher.h" |
[email protected] | fb5bcc0 | 2012-02-17 14:05:42 | [diff] [blame] | 6 | |
avi | 5dd91f8 | 2015-12-25 22:30:46 | [diff] [blame] | 7 | #include <stddef.h> |
dcheng | 3f767dc3 | 2016-04-25 22:54:22 | [diff] [blame] | 8 | |
| 9 | #include <memory> |
dcheng | 51ace48a | 2015-12-26 22:45:17 | [diff] [blame] | 10 | #include <utility> |
avi | 5dd91f8 | 2015-12-25 22:30:46 | [diff] [blame] | 11 | |
| 12 | #include "base/macros.h" |
[email protected] | f439096 | 2013-06-11 07:29:22 | [diff] [blame] | 13 | #include "base/strings/string_util.h" |
[email protected] | fb5bcc0 | 2012-02-17 14:05:42 | [diff] [blame] | 14 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | 79fe227 | 2013-07-13 20:01:40 | [diff] [blame] | 15 | #include "url/gurl.h" |
[email protected] | fb5bcc0 | 2012-02-17 14:05:42 | [diff] [blame] | 16 | |
[email protected] | 716c016 | 2013-12-13 20:36:53 | [diff] [blame] | 17 | namespace url_matcher { |
[email protected] | fb5bcc0 | 2012-02-17 14:05:42 | [diff] [blame] | 18 | |
| 19 | // |
| 20 | // URLMatcherCondition |
| 21 | // |
| 22 | |
| 23 | TEST(URLMatcherConditionTest, Constructors) { |
[email protected] | 5bcf3b7 | 2012-09-14 00:20:28 | [diff] [blame] | 24 | StringPattern pattern("example.com", 1); |
[email protected] | fb5bcc0 | 2012-02-17 14:05:42 | [diff] [blame] | 25 | URLMatcherCondition m1(URLMatcherCondition::HOST_SUFFIX, &pattern); |
| 26 | EXPECT_EQ(URLMatcherCondition::HOST_SUFFIX, m1.criterion()); |
[email protected] | 5bcf3b7 | 2012-09-14 00:20:28 | [diff] [blame] | 27 | EXPECT_EQ(&pattern, m1.string_pattern()); |
[email protected] | fb5bcc0 | 2012-02-17 14:05:42 | [diff] [blame] | 28 | |
| 29 | URLMatcherCondition m2; |
| 30 | m2 = m1; |
| 31 | EXPECT_EQ(URLMatcherCondition::HOST_SUFFIX, m2.criterion()); |
[email protected] | 5bcf3b7 | 2012-09-14 00:20:28 | [diff] [blame] | 32 | EXPECT_EQ(&pattern, m2.string_pattern()); |
[email protected] | fb5bcc0 | 2012-02-17 14:05:42 | [diff] [blame] | 33 | |
| 34 | URLMatcherCondition m3(m1); |
| 35 | EXPECT_EQ(URLMatcherCondition::HOST_SUFFIX, m3.criterion()); |
[email protected] | 5bcf3b7 | 2012-09-14 00:20:28 | [diff] [blame] | 36 | EXPECT_EQ(&pattern, m3.string_pattern()); |
[email protected] | fb5bcc0 | 2012-02-17 14:05:42 | [diff] [blame] | 37 | } |
| 38 | |
[email protected] | faceb0f | 2012-04-12 17:07:19 | [diff] [blame] | 39 | TEST(URLMatcherSchemeFilter, TestMatching) { |
| 40 | URLMatcherSchemeFilter filter1("https"); |
| 41 | std::vector<std::string> filter2_content; |
| 42 | filter2_content.push_back("http"); |
| 43 | filter2_content.push_back("https"); |
| 44 | URLMatcherSchemeFilter filter2(filter2_content); |
| 45 | |
| 46 | GURL matching_url("https://www.foobar.com"); |
| 47 | GURL non_matching_url("http://www.foobar.com"); |
| 48 | EXPECT_TRUE(filter1.IsMatch(matching_url)); |
| 49 | EXPECT_FALSE(filter1.IsMatch(non_matching_url)); |
| 50 | EXPECT_TRUE(filter2.IsMatch(matching_url)); |
| 51 | EXPECT_TRUE(filter2.IsMatch(non_matching_url)); |
| 52 | } |
| 53 | |
[email protected] | 00520a5 | 2012-04-12 18:30:47 | [diff] [blame] | 54 | TEST(URLMatcherPortFilter, TestMatching) { |
| 55 | std::vector<URLMatcherPortFilter::Range> ranges; |
| 56 | ranges.push_back(URLMatcherPortFilter::CreateRange(80, 90)); |
| 57 | ranges.push_back(URLMatcherPortFilter::CreateRange(8080)); |
| 58 | URLMatcherPortFilter filter(ranges); |
| 59 | EXPECT_TRUE(filter.IsMatch(GURL("http://www.example.com"))); |
| 60 | EXPECT_TRUE(filter.IsMatch(GURL("http://www.example.com:80"))); |
| 61 | EXPECT_TRUE(filter.IsMatch(GURL("http://www.example.com:81"))); |
| 62 | EXPECT_TRUE(filter.IsMatch(GURL("http://www.example.com:90"))); |
| 63 | EXPECT_TRUE(filter.IsMatch(GURL("http://www.example.com:8080"))); |
| 64 | EXPECT_FALSE(filter.IsMatch(GURL("http://www.example.com:79"))); |
| 65 | EXPECT_FALSE(filter.IsMatch(GURL("http://www.example.com:91"))); |
| 66 | EXPECT_FALSE(filter.IsMatch(GURL("https://www.example.com"))); |
| 67 | } |
| 68 | |
[email protected] | fb5bcc0 | 2012-02-17 14:05:42 | [diff] [blame] | 69 | TEST(URLMatcherConditionTest, IsFullURLCondition) { |
[email protected] | 5bcf3b7 | 2012-09-14 00:20:28 | [diff] [blame] | 70 | StringPattern pattern("example.com", 1); |
[email protected] | fb5bcc0 | 2012-02-17 14:05:42 | [diff] [blame] | 71 | EXPECT_FALSE(URLMatcherCondition(URLMatcherCondition::HOST_SUFFIX, |
| 72 | &pattern).IsFullURLCondition()); |
| 73 | |
| 74 | EXPECT_TRUE(URLMatcherCondition(URLMatcherCondition::HOST_CONTAINS, |
| 75 | &pattern).IsFullURLCondition()); |
| 76 | EXPECT_TRUE(URLMatcherCondition(URLMatcherCondition::PATH_CONTAINS, |
| 77 | &pattern).IsFullURLCondition()); |
| 78 | EXPECT_TRUE(URLMatcherCondition(URLMatcherCondition::QUERY_CONTAINS, |
| 79 | &pattern).IsFullURLCondition()); |
| 80 | |
| 81 | EXPECT_TRUE(URLMatcherCondition(URLMatcherCondition::URL_PREFIX, |
| 82 | &pattern).IsFullURLCondition()); |
| 83 | EXPECT_TRUE(URLMatcherCondition(URLMatcherCondition::URL_SUFFIX, |
| 84 | &pattern).IsFullURLCondition()); |
| 85 | EXPECT_TRUE(URLMatcherCondition(URLMatcherCondition::URL_CONTAINS, |
| 86 | &pattern).IsFullURLCondition()); |
| 87 | EXPECT_TRUE(URLMatcherCondition(URLMatcherCondition::URL_EQUALS, |
| 88 | &pattern).IsFullURLCondition()); |
| 89 | } |
| 90 | |
| 91 | TEST(URLMatcherConditionTest, IsMatch) { |
| 92 | GURL url1("http://www.example.com/www.foobar.com/index.html"); |
| 93 | GURL url2("http://www.foobar.com/example.com/index.html"); |
| 94 | |
[email protected] | 5bcf3b7 | 2012-09-14 00:20:28 | [diff] [blame] | 95 | StringPattern pattern("example.com", 1); |
[email protected] | fb5bcc0 | 2012-02-17 14:05:42 | [diff] [blame] | 96 | URLMatcherCondition m1(URLMatcherCondition::HOST_SUFFIX, &pattern); |
| 97 | |
[email protected] | 5bcf3b7 | 2012-09-14 00:20:28 | [diff] [blame] | 98 | std::set<StringPattern::ID> matching_patterns; |
[email protected] | fb5bcc0 | 2012-02-17 14:05:42 | [diff] [blame] | 99 | |
| 100 | // matches = {0} --> matcher did not indicate that m1 was a match. |
[email protected] | 5bcf3b7 | 2012-09-14 00:20:28 | [diff] [blame] | 101 | matching_patterns.insert(0); |
| 102 | EXPECT_FALSE(m1.IsMatch(matching_patterns, url1)); |
[email protected] | fb5bcc0 | 2012-02-17 14:05:42 | [diff] [blame] | 103 | |
| 104 | // matches = {0, 1} --> matcher did indicate that m1 was a match. |
[email protected] | 5bcf3b7 | 2012-09-14 00:20:28 | [diff] [blame] | 105 | matching_patterns.insert(1); |
| 106 | EXPECT_TRUE(m1.IsMatch(matching_patterns, url1)); |
[email protected] | fb5bcc0 | 2012-02-17 14:05:42 | [diff] [blame] | 107 | |
| 108 | // For m2 we use a HOST_CONTAINS test, which requires a post-validation |
| 109 | // whether the match reported by the SubstringSetMatcher occurs really |
| 110 | // in the correct url component. |
| 111 | URLMatcherCondition m2(URLMatcherCondition::HOST_CONTAINS, &pattern); |
[email protected] | 5bcf3b7 | 2012-09-14 00:20:28 | [diff] [blame] | 112 | EXPECT_TRUE(m2.IsMatch(matching_patterns, url1)); |
| 113 | EXPECT_FALSE(m2.IsMatch(matching_patterns, url2)); |
[email protected] | fb5bcc0 | 2012-02-17 14:05:42 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | TEST(URLMatcherConditionTest, Comparison) { |
[email protected] | 5bcf3b7 | 2012-09-14 00:20:28 | [diff] [blame] | 117 | StringPattern p1("foobar.com", 1); |
| 118 | StringPattern p2("foobar.com", 2); |
[email protected] | fb5bcc0 | 2012-02-17 14:05:42 | [diff] [blame] | 119 | // The first component of each test is expected to be < than the second. |
| 120 | URLMatcherCondition test_smaller[][2] = { |
| 121 | {URLMatcherCondition(URLMatcherCondition::HOST_PREFIX, &p1), |
| 122 | URLMatcherCondition(URLMatcherCondition::HOST_SUFFIX, &p1)}, |
| 123 | {URLMatcherCondition(URLMatcherCondition::HOST_PREFIX, &p1), |
| 124 | URLMatcherCondition(URLMatcherCondition::HOST_PREFIX, &p2)}, |
Ivan Kotenkov | 75b1c3a | 2017-10-24 14:47:24 | [diff] [blame] | 125 | {URLMatcherCondition(URLMatcherCondition::HOST_PREFIX, nullptr), |
[email protected] | fb5bcc0 | 2012-02-17 14:05:42 | [diff] [blame] | 126 | URLMatcherCondition(URLMatcherCondition::HOST_PREFIX, &p2)}, |
| 127 | {URLMatcherCondition(URLMatcherCondition::HOST_PREFIX, &p1), |
Ivan Kotenkov | 75b1c3a | 2017-10-24 14:47:24 | [diff] [blame] | 128 | URLMatcherCondition(URLMatcherCondition::HOST_SUFFIX, nullptr)}, |
[email protected] | fb5bcc0 | 2012-02-17 14:05:42 | [diff] [blame] | 129 | }; |
| 130 | for (size_t i = 0; i < arraysize(test_smaller); ++i) { |
| 131 | EXPECT_TRUE(test_smaller[i][0] < test_smaller[i][1]) |
| 132 | << "Test " << i << " of test_smaller failed"; |
| 133 | EXPECT_FALSE(test_smaller[i][1] < test_smaller[i][0]) |
| 134 | << "Test " << i << " of test_smaller failed"; |
| 135 | } |
| 136 | URLMatcherCondition test_equal[][2] = { |
| 137 | {URLMatcherCondition(URLMatcherCondition::HOST_PREFIX, &p1), |
| 138 | URLMatcherCondition(URLMatcherCondition::HOST_PREFIX, &p1)}, |
Ivan Kotenkov | 75b1c3a | 2017-10-24 14:47:24 | [diff] [blame] | 139 | {URLMatcherCondition(URLMatcherCondition::HOST_PREFIX, nullptr), |
| 140 | URLMatcherCondition(URLMatcherCondition::HOST_PREFIX, nullptr)}, |
[email protected] | fb5bcc0 | 2012-02-17 14:05:42 | [diff] [blame] | 141 | }; |
| 142 | for (size_t i = 0; i < arraysize(test_equal); ++i) { |
| 143 | EXPECT_FALSE(test_equal[i][0] < test_equal[i][1]) |
| 144 | << "Test " << i << " of test_equal failed"; |
| 145 | EXPECT_FALSE(test_equal[i][1] < test_equal[i][0]) |
| 146 | << "Test " << i << " of test_equal failed"; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | // |
| 151 | // URLMatcherConditionFactory |
| 152 | // |
| 153 | |
| 154 | namespace { |
| 155 | |
ki.stfu | 939799a4 | 2015-09-28 04:41:20 | [diff] [blame] | 156 | bool Matches(const URLMatcherCondition& condition, const std::string& text) { |
[email protected] | 5bcf3b7 | 2012-09-14 00:20:28 | [diff] [blame] | 157 | return text.find(condition.string_pattern()->pattern()) != |
[email protected] | fb5bcc0 | 2012-02-17 14:05:42 | [diff] [blame] | 158 | std::string::npos; |
| 159 | } |
| 160 | |
| 161 | } // namespace |
| 162 | |
| 163 | TEST(URLMatcherConditionFactoryTest, GURLCharacterSet) { |
| 164 | // GURL guarantees that neither domain, nor path, nor query may contain |
| 165 | // non ASCII-7 characters. We test this here, because a change to this |
| 166 | // guarantee breaks this implementation horribly. |
| 167 | GURL url("http://www.föö.com/föö?föö#föö"); |
[email protected] | 52796541 | 2014-05-07 14:38:26 | [diff] [blame] | 168 | EXPECT_TRUE(base::IsStringASCII(url.host())); |
| 169 | EXPECT_TRUE(base::IsStringASCII(url.path())); |
| 170 | EXPECT_TRUE(base::IsStringASCII(url.query())); |
Mike West | f8f6ed5 | 2017-10-09 20:58:50 | [diff] [blame] | 171 | EXPECT_TRUE(base::IsStringASCII(url.ref())); |
[email protected] | fb5bcc0 | 2012-02-17 14:05:42 | [diff] [blame] | 172 | } |
| 173 | |
[email protected] | 6d8e5e4 | 2012-07-17 15:54:45 | [diff] [blame] | 174 | TEST(URLMatcherConditionFactoryTest, Criteria) { |
| 175 | URLMatcherConditionFactory factory; |
| 176 | EXPECT_EQ(URLMatcherCondition::HOST_PREFIX, |
| 177 | factory.CreateHostPrefixCondition("foo").criterion()); |
| 178 | EXPECT_EQ(URLMatcherCondition::HOST_SUFFIX, |
| 179 | factory.CreateHostSuffixCondition("foo").criterion()); |
| 180 | EXPECT_EQ(URLMatcherCondition::HOST_CONTAINS, |
| 181 | factory.CreateHostContainsCondition("foo").criterion()); |
| 182 | EXPECT_EQ(URLMatcherCondition::HOST_EQUALS, |
| 183 | factory.CreateHostEqualsCondition("foo").criterion()); |
| 184 | EXPECT_EQ(URLMatcherCondition::PATH_PREFIX, |
| 185 | factory.CreatePathPrefixCondition("foo").criterion()); |
| 186 | EXPECT_EQ(URLMatcherCondition::PATH_SUFFIX, |
| 187 | factory.CreatePathSuffixCondition("foo").criterion()); |
| 188 | EXPECT_EQ(URLMatcherCondition::PATH_CONTAINS, |
| 189 | factory.CreatePathContainsCondition("foo").criterion()); |
| 190 | EXPECT_EQ(URLMatcherCondition::PATH_EQUALS, |
| 191 | factory.CreatePathEqualsCondition("foo").criterion()); |
| 192 | EXPECT_EQ(URLMatcherCondition::QUERY_PREFIX, |
| 193 | factory.CreateQueryPrefixCondition("foo").criterion()); |
| 194 | EXPECT_EQ(URLMatcherCondition::QUERY_SUFFIX, |
| 195 | factory.CreateQuerySuffixCondition("foo").criterion()); |
| 196 | EXPECT_EQ(URLMatcherCondition::QUERY_CONTAINS, |
| 197 | factory.CreateQueryContainsCondition("foo").criterion()); |
| 198 | EXPECT_EQ(URLMatcherCondition::QUERY_EQUALS, |
| 199 | factory.CreateQueryEqualsCondition("foo").criterion()); |
| 200 | EXPECT_EQ(URLMatcherCondition::HOST_SUFFIX_PATH_PREFIX, |
| 201 | factory.CreateHostSuffixPathPrefixCondition("foo", |
| 202 | "bar").criterion()); |
| 203 | EXPECT_EQ(URLMatcherCondition::HOST_EQUALS_PATH_PREFIX, |
| 204 | factory.CreateHostEqualsPathPrefixCondition("foo", |
| 205 | "bar").criterion()); |
| 206 | EXPECT_EQ(URLMatcherCondition::URL_PREFIX, |
| 207 | factory.CreateURLPrefixCondition("foo").criterion()); |
| 208 | EXPECT_EQ(URLMatcherCondition::URL_SUFFIX, |
| 209 | factory.CreateURLSuffixCondition("foo").criterion()); |
| 210 | EXPECT_EQ(URLMatcherCondition::URL_CONTAINS, |
| 211 | factory.CreateURLContainsCondition("foo").criterion()); |
| 212 | EXPECT_EQ(URLMatcherCondition::URL_EQUALS, |
| 213 | factory.CreateURLEqualsCondition("foo").criterion()); |
[email protected] | 5bcf3b7 | 2012-09-14 00:20:28 | [diff] [blame] | 214 | EXPECT_EQ(URLMatcherCondition::URL_MATCHES, |
| 215 | factory.CreateURLMatchesCondition("foo").criterion()); |
[email protected] | 6d8e5e4 | 2012-07-17 15:54:45 | [diff] [blame] | 216 | } |
| 217 | |
[email protected] | fb5bcc0 | 2012-02-17 14:05:42 | [diff] [blame] | 218 | TEST(URLMatcherConditionFactoryTest, TestSingletonProperty) { |
| 219 | URLMatcherConditionFactory factory; |
| 220 | URLMatcherCondition c1 = factory.CreateHostEqualsCondition("www.google.com"); |
| 221 | URLMatcherCondition c2 = factory.CreateHostEqualsCondition("www.google.com"); |
| 222 | EXPECT_EQ(c1.criterion(), c2.criterion()); |
[email protected] | 5bcf3b7 | 2012-09-14 00:20:28 | [diff] [blame] | 223 | EXPECT_EQ(c1.string_pattern(), c2.string_pattern()); |
[email protected] | fb5bcc0 | 2012-02-17 14:05:42 | [diff] [blame] | 224 | URLMatcherCondition c3 = factory.CreateHostEqualsCondition("www.google.de"); |
| 225 | EXPECT_EQ(c2.criterion(), c3.criterion()); |
[email protected] | 5bcf3b7 | 2012-09-14 00:20:28 | [diff] [blame] | 226 | EXPECT_NE(c2.string_pattern(), c3.string_pattern()); |
| 227 | EXPECT_NE(c2.string_pattern()->id(), c3.string_pattern()->id()); |
| 228 | EXPECT_NE(c2.string_pattern()->pattern(), |
| 229 | c3.string_pattern()->pattern()); |
| 230 | URLMatcherCondition c4 = factory.CreateURLMatchesCondition("www.google.com"); |
| 231 | URLMatcherCondition c5 = factory.CreateURLContainsCondition("www.google.com"); |
| 232 | // Regex patterns and substring patterns do not share IDs. |
| 233 | EXPECT_EQ(c5.string_pattern()->pattern(), c4.string_pattern()->pattern()); |
| 234 | EXPECT_NE(c5.string_pattern(), c4.string_pattern()); |
| 235 | EXPECT_NE(c5.string_pattern()->id(), c4.string_pattern()->id()); |
[email protected] | fb5bcc0 | 2012-02-17 14:05:42 | [diff] [blame] | 236 | |
[email protected] | 5bcf3b7 | 2012-09-14 00:20:28 | [diff] [blame] | 237 | // Check that all StringPattern singletons are freed if we call |
[email protected] | fb5bcc0 | 2012-02-17 14:05:42 | [diff] [blame] | 238 | // ForgetUnusedPatterns. |
[email protected] | 5bcf3b7 | 2012-09-14 00:20:28 | [diff] [blame] | 239 | StringPattern::ID old_id_1 = c1.string_pattern()->id(); |
| 240 | StringPattern::ID old_id_4 = c4.string_pattern()->id(); |
| 241 | factory.ForgetUnusedPatterns(std::set<StringPattern::ID>()); |
[email protected] | 357c4db | 2012-03-29 07:51:57 | [diff] [blame] | 242 | EXPECT_TRUE(factory.IsEmpty()); |
[email protected] | 5bcf3b7 | 2012-09-14 00:20:28 | [diff] [blame] | 243 | URLMatcherCondition c6 = factory.CreateHostEqualsCondition("www.google.com"); |
| 244 | EXPECT_NE(old_id_1, c6.string_pattern()->id()); |
| 245 | URLMatcherCondition c7 = factory.CreateURLMatchesCondition("www.google.com"); |
| 246 | EXPECT_NE(old_id_4, c7.string_pattern()->id()); |
[email protected] | fb5bcc0 | 2012-02-17 14:05:42 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | TEST(URLMatcherConditionFactoryTest, TestComponentSearches) { |
mnissler | d9cdcd87 | 2015-05-29 13:57:41 | [diff] [blame] | 250 | URLMatcherConditionFactory factory; |
[email protected] | c640fd7c | 2012-08-17 08:19:25 | [diff] [blame] | 251 | GURL gurl("https://www.google.com:1234/webhp?sourceid=chrome-instant&ie=UTF-8" |
[email protected] | fb5bcc0 | 2012-02-17 14:05:42 | [diff] [blame] | 252 | "&ion=1#hl=en&output=search&sclient=psy-ab&q=chrome%20is%20awesome"); |
[email protected] | fb5bcc0 | 2012-02-17 14:05:42 | [diff] [blame] | 253 | std::string url = factory.CanonicalizeURLForComponentSearches(gurl); |
mnissler | d9cdcd87 | 2015-05-29 13:57:41 | [diff] [blame] | 254 | GURL gurl2("https://www.google.com.:1234/webhp?sourceid=chrome-instant" |
| 255 | "&ie=UTF-8&ion=1#hl=en&output=search&sclient=psy-ab" |
| 256 | "&q=chrome%20is%20awesome"); |
| 257 | std::string url2 = factory.CanonicalizeURLForComponentSearches(gurl2); |
[email protected] | fb5bcc0 | 2012-02-17 14:05:42 | [diff] [blame] | 258 | |
| 259 | // Test host component. |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 260 | EXPECT_TRUE(Matches(factory.CreateHostPrefixCondition(std::string()), url)); |
[email protected] | fb5bcc0 | 2012-02-17 14:05:42 | [diff] [blame] | 261 | EXPECT_TRUE(Matches(factory.CreateHostPrefixCondition("www.goog"), url)); |
| 262 | EXPECT_TRUE( |
| 263 | Matches(factory.CreateHostPrefixCondition("www.google.com"), url)); |
| 264 | EXPECT_TRUE( |
| 265 | Matches(factory.CreateHostPrefixCondition(".www.google.com"), url)); |
| 266 | EXPECT_FALSE(Matches(factory.CreateHostPrefixCondition("google.com"), url)); |
| 267 | EXPECT_FALSE( |
| 268 | Matches(factory.CreateHostPrefixCondition("www.google.com/"), url)); |
| 269 | EXPECT_FALSE(Matches(factory.CreateHostPrefixCondition("webhp"), url)); |
| 270 | |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 271 | EXPECT_TRUE(Matches(factory.CreateHostSuffixCondition(std::string()), url)); |
mnissler | d9cdcd87 | 2015-05-29 13:57:41 | [diff] [blame] | 272 | EXPECT_TRUE(Matches(factory.CreateHostSuffixCondition(std::string()), url2)); |
[email protected] | fb5bcc0 | 2012-02-17 14:05:42 | [diff] [blame] | 273 | EXPECT_TRUE(Matches(factory.CreateHostSuffixCondition("com"), url)); |
mnissler | d9cdcd87 | 2015-05-29 13:57:41 | [diff] [blame] | 274 | EXPECT_TRUE(Matches(factory.CreateHostSuffixCondition("com"), url2)); |
[email protected] | fb5bcc0 | 2012-02-17 14:05:42 | [diff] [blame] | 275 | EXPECT_TRUE(Matches(factory.CreateHostSuffixCondition(".com"), url)); |
| 276 | EXPECT_TRUE( |
| 277 | Matches(factory.CreateHostSuffixCondition("www.google.com"), url)); |
| 278 | EXPECT_TRUE( |
| 279 | Matches(factory.CreateHostSuffixCondition(".www.google.com"), url)); |
mnissler | d9cdcd87 | 2015-05-29 13:57:41 | [diff] [blame] | 280 | EXPECT_TRUE( |
| 281 | Matches(factory.CreateHostSuffixCondition(".www.google.com"), url2)); |
| 282 | EXPECT_TRUE( |
| 283 | Matches(factory.CreateHostSuffixCondition(".www.google.com."), url)); |
[email protected] | fb5bcc0 | 2012-02-17 14:05:42 | [diff] [blame] | 284 | EXPECT_FALSE(Matches(factory.CreateHostSuffixCondition("www"), url)); |
| 285 | EXPECT_FALSE( |
| 286 | Matches(factory.CreateHostSuffixCondition("www.google.com/"), url)); |
| 287 | EXPECT_FALSE(Matches(factory.CreateHostSuffixCondition("webhp"), url)); |
| 288 | |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 289 | EXPECT_FALSE(Matches(factory.CreateHostEqualsCondition(std::string()), url)); |
[email protected] | fb5bcc0 | 2012-02-17 14:05:42 | [diff] [blame] | 290 | EXPECT_FALSE(Matches(factory.CreateHostEqualsCondition("www"), url)); |
| 291 | EXPECT_TRUE( |
| 292 | Matches(factory.CreateHostEqualsCondition("www.google.com"), url)); |
mnissler | d9cdcd87 | 2015-05-29 13:57:41 | [diff] [blame] | 293 | EXPECT_TRUE( |
| 294 | Matches(factory.CreateHostEqualsCondition("www.google.com"), url2)); |
[email protected] | fb5bcc0 | 2012-02-17 14:05:42 | [diff] [blame] | 295 | EXPECT_FALSE( |
| 296 | Matches(factory.CreateHostEqualsCondition("www.google.com/"), url)); |
mnissler | d9cdcd87 | 2015-05-29 13:57:41 | [diff] [blame] | 297 | EXPECT_TRUE( |
| 298 | Matches(factory.CreateHostEqualsCondition(".www.google.com."), url)); |
| 299 | EXPECT_TRUE( |
| 300 | Matches(factory.CreateHostEqualsCondition(".www.google.com."), url2)); |
[email protected] | fb5bcc0 | 2012-02-17 14:05:42 | [diff] [blame] | 301 | |
| 302 | // Test path component. |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 303 | EXPECT_TRUE(Matches(factory.CreatePathPrefixCondition(std::string()), url)); |
[email protected] | fb5bcc0 | 2012-02-17 14:05:42 | [diff] [blame] | 304 | EXPECT_TRUE(Matches(factory.CreatePathPrefixCondition("/web"), url)); |
| 305 | EXPECT_TRUE(Matches(factory.CreatePathPrefixCondition("/webhp"), url)); |
| 306 | EXPECT_FALSE(Matches(factory.CreatePathPrefixCondition("webhp"), url)); |
| 307 | EXPECT_FALSE(Matches(factory.CreatePathPrefixCondition("/webhp?"), url)); |
[email protected] | ea6249b | 2012-12-06 18:45:20 | [diff] [blame] | 308 | EXPECT_FALSE(Matches(factory.CreatePathPrefixCondition("?sourceid"), url)); |
[email protected] | fb5bcc0 | 2012-02-17 14:05:42 | [diff] [blame] | 309 | |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 310 | EXPECT_TRUE(Matches(factory.CreatePathSuffixCondition(std::string()), url)); |
[email protected] | fb5bcc0 | 2012-02-17 14:05:42 | [diff] [blame] | 311 | EXPECT_TRUE(Matches(factory.CreatePathSuffixCondition("webhp"), url)); |
| 312 | EXPECT_TRUE(Matches(factory.CreatePathSuffixCondition("/webhp"), url)); |
| 313 | EXPECT_FALSE(Matches(factory.CreatePathSuffixCondition("/web"), url)); |
| 314 | EXPECT_FALSE(Matches(factory.CreatePathSuffixCondition("/webhp?"), url)); |
| 315 | |
| 316 | EXPECT_TRUE(Matches(factory.CreatePathEqualsCondition("/webhp"), url)); |
| 317 | EXPECT_FALSE(Matches(factory.CreatePathEqualsCondition("webhp"), url)); |
| 318 | EXPECT_FALSE(Matches(factory.CreatePathEqualsCondition("/webhp?"), url)); |
| 319 | EXPECT_FALSE( |
| 320 | Matches(factory.CreatePathEqualsCondition("www.google.com"), url)); |
| 321 | |
| 322 | |
| 323 | // Test query component. |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 324 | EXPECT_TRUE(Matches(factory.CreateQueryPrefixCondition(std::string()), url)); |
[email protected] | ea6249b | 2012-12-06 18:45:20 | [diff] [blame] | 325 | EXPECT_TRUE(Matches(factory.CreateQueryPrefixCondition("sourceid"), url)); |
| 326 | // The '?' at the beginning is just ignored. |
[email protected] | fb5bcc0 | 2012-02-17 14:05:42 | [diff] [blame] | 327 | EXPECT_TRUE(Matches(factory.CreateQueryPrefixCondition("?sourceid"), url)); |
[email protected] | fb5bcc0 | 2012-02-17 14:05:42 | [diff] [blame] | 328 | |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 329 | EXPECT_TRUE(Matches(factory.CreateQuerySuffixCondition(std::string()), url)); |
[email protected] | fb5bcc0 | 2012-02-17 14:05:42 | [diff] [blame] | 330 | EXPECT_TRUE(Matches(factory.CreateQuerySuffixCondition("ion=1"), url)); |
[email protected] | fb5bcc0 | 2012-02-17 14:05:42 | [diff] [blame] | 331 | EXPECT_FALSE(Matches(factory.CreateQuerySuffixCondition("www"), url)); |
[email protected] | ea6249b | 2012-12-06 18:45:20 | [diff] [blame] | 332 | // "Suffix" condition + pattern starting with '?' = "equals" condition. |
| 333 | EXPECT_FALSE(Matches(factory.CreateQuerySuffixCondition( |
| 334 | "?sourceid=chrome-instant&ie=UTF-8&ion="), url)); |
| 335 | EXPECT_TRUE(Matches(factory.CreateQuerySuffixCondition( |
| 336 | "?sourceid=chrome-instant&ie=UTF-8&ion=1"), url)); |
[email protected] | fb5bcc0 | 2012-02-17 14:05:42 | [diff] [blame] | 337 | |
[email protected] | ea6249b | 2012-12-06 18:45:20 | [diff] [blame] | 338 | EXPECT_FALSE(Matches(factory.CreateQueryEqualsCondition( |
| 339 | "?sourceid=chrome-instant&ie=UTF-8&ion="), url)); |
| 340 | EXPECT_FALSE(Matches(factory.CreateQueryEqualsCondition( |
| 341 | "sourceid=chrome-instant&ie=UTF-8&ion="), url)); |
| 342 | EXPECT_TRUE(Matches(factory.CreateQueryEqualsCondition( |
| 343 | "sourceid=chrome-instant&ie=UTF-8&ion=1"), url)); |
| 344 | // The '?' at the beginning is just ignored. |
[email protected] | fb5bcc0 | 2012-02-17 14:05:42 | [diff] [blame] | 345 | EXPECT_TRUE(Matches(factory.CreateQueryEqualsCondition( |
| 346 | "?sourceid=chrome-instant&ie=UTF-8&ion=1"), url)); |
[email protected] | fb5bcc0 | 2012-02-17 14:05:42 | [diff] [blame] | 347 | EXPECT_FALSE( |
| 348 | Matches(factory.CreateQueryEqualsCondition("www.google.com"), url)); |
| 349 | |
| 350 | |
| 351 | // Test adjacent components |
| 352 | EXPECT_TRUE(Matches(factory.CreateHostSuffixPathPrefixCondition( |
| 353 | "google.com", "/webhp"), url)); |
mnissler | d9cdcd87 | 2015-05-29 13:57:41 | [diff] [blame] | 354 | EXPECT_TRUE(Matches(factory.CreateHostSuffixPathPrefixCondition( |
| 355 | "google.com", "/webhp"), url2)); |
| 356 | EXPECT_TRUE(Matches(factory.CreateHostSuffixPathPrefixCondition( |
| 357 | "google.com.", "/webhp"), url)); |
| 358 | EXPECT_TRUE(Matches(factory.CreateHostSuffixPathPrefixCondition( |
| 359 | "google.com.", "/webhp"), url2)); |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 360 | EXPECT_TRUE(Matches( |
| 361 | factory.CreateHostSuffixPathPrefixCondition(std::string(), "/webhp"), |
| 362 | url)); |
| 363 | EXPECT_TRUE(Matches( |
| 364 | factory.CreateHostSuffixPathPrefixCondition("google.com", std::string()), |
| 365 | url)); |
| 366 | EXPECT_FALSE(Matches( |
| 367 | factory.CreateHostSuffixPathPrefixCondition("www", std::string()), url)); |
[email protected] | 6d8e5e4 | 2012-07-17 15:54:45 | [diff] [blame] | 368 | |
| 369 | EXPECT_TRUE(Matches(factory.CreateHostEqualsPathPrefixCondition( |
| 370 | "www.google.com", "/webhp"), url)); |
mnissler | d9cdcd87 | 2015-05-29 13:57:41 | [diff] [blame] | 371 | EXPECT_TRUE(Matches(factory.CreateHostEqualsPathPrefixCondition( |
| 372 | "www.google.com", "/webhp"), url2)); |
| 373 | EXPECT_TRUE(Matches(factory.CreateHostEqualsPathPrefixCondition( |
| 374 | ".www.google.com.", "/webhp"), url)); |
| 375 | EXPECT_TRUE(Matches(factory.CreateHostEqualsPathPrefixCondition( |
| 376 | ".www.google.com.", "/webhp"), url2)); |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 377 | EXPECT_FALSE(Matches( |
| 378 | factory.CreateHostEqualsPathPrefixCondition(std::string(), "/webhp"), |
| 379 | url)); |
[email protected] | 6d8e5e4 | 2012-07-17 15:54:45 | [diff] [blame] | 380 | EXPECT_TRUE(Matches(factory.CreateHostEqualsPathPrefixCondition( |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 381 | "www.google.com", std::string()), |
| 382 | url)); |
| 383 | EXPECT_FALSE(Matches( |
| 384 | factory.CreateHostEqualsPathPrefixCondition("google.com", std::string()), |
| 385 | url)); |
[email protected] | fb5bcc0 | 2012-02-17 14:05:42 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | TEST(URLMatcherConditionFactoryTest, TestFullSearches) { |
[email protected] | c640fd7c | 2012-08-17 08:19:25 | [diff] [blame] | 389 | // The Port 443 is stripped because it is the default port for https. |
| 390 | GURL gurl("https://www.google.com:443/webhp?sourceid=chrome-instant&ie=UTF-8" |
[email protected] | fb5bcc0 | 2012-02-17 14:05:42 | [diff] [blame] | 391 | "&ion=1#hl=en&output=search&sclient=psy-ab&q=chrome%20is%20awesome"); |
| 392 | URLMatcherConditionFactory factory; |
| 393 | std::string url = factory.CanonicalizeURLForFullSearches(gurl); |
| 394 | |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 395 | EXPECT_TRUE(Matches(factory.CreateURLPrefixCondition(std::string()), url)); |
| 396 | EXPECT_TRUE( |
| 397 | Matches(factory.CreateURLPrefixCondition("https://www.goog"), url)); |
[email protected] | c640fd7c | 2012-08-17 08:19:25 | [diff] [blame] | 398 | EXPECT_TRUE(Matches(factory.CreateURLPrefixCondition( |
| 399 | "https://www.google.com"), url)); |
| 400 | EXPECT_TRUE(Matches(factory.CreateURLPrefixCondition( |
| 401 | "https://www.google.com/webhp?"), url)); |
| 402 | EXPECT_FALSE(Matches(factory.CreateURLPrefixCondition( |
| 403 | "http://www.google.com"), url)); |
[email protected] | fb5bcc0 | 2012-02-17 14:05:42 | [diff] [blame] | 404 | EXPECT_FALSE(Matches(factory.CreateURLPrefixCondition("webhp"), url)); |
| 405 | |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 406 | EXPECT_TRUE(Matches(factory.CreateURLSuffixCondition(std::string()), url)); |
[email protected] | fb5bcc0 | 2012-02-17 14:05:42 | [diff] [blame] | 407 | EXPECT_TRUE(Matches(factory.CreateURLSuffixCondition("ion=1"), url)); |
| 408 | EXPECT_FALSE(Matches(factory.CreateURLSuffixCondition("www"), url)); |
| 409 | |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 410 | EXPECT_TRUE(Matches(factory.CreateURLContainsCondition(std::string()), url)); |
[email protected] | fb5bcc0 | 2012-02-17 14:05:42 | [diff] [blame] | 411 | EXPECT_TRUE(Matches(factory.CreateURLContainsCondition("www.goog"), url)); |
[email protected] | fb5bcc0 | 2012-02-17 14:05:42 | [diff] [blame] | 412 | EXPECT_TRUE(Matches(factory.CreateURLContainsCondition("webhp"), url)); |
| 413 | EXPECT_TRUE(Matches(factory.CreateURLContainsCondition("?"), url)); |
| 414 | EXPECT_TRUE(Matches(factory.CreateURLContainsCondition("sourceid"), url)); |
| 415 | EXPECT_TRUE(Matches(factory.CreateURLContainsCondition("ion=1"), url)); |
[email protected] | c640fd7c | 2012-08-17 08:19:25 | [diff] [blame] | 416 | EXPECT_FALSE(Matches(factory.CreateURLContainsCondition(".www.goog"), url)); |
[email protected] | fb5bcc0 | 2012-02-17 14:05:42 | [diff] [blame] | 417 | EXPECT_FALSE(Matches(factory.CreateURLContainsCondition("foobar"), url)); |
| 418 | EXPECT_FALSE(Matches(factory.CreateURLContainsCondition("search"), url)); |
[email protected] | c640fd7c | 2012-08-17 08:19:25 | [diff] [blame] | 419 | EXPECT_FALSE(Matches(factory.CreateURLContainsCondition(":443"), url)); |
[email protected] | fb5bcc0 | 2012-02-17 14:05:42 | [diff] [blame] | 420 | |
| 421 | EXPECT_TRUE(Matches(factory.CreateURLEqualsCondition( |
[email protected] | c640fd7c | 2012-08-17 08:19:25 | [diff] [blame] | 422 | "https://www.google.com/webhp?sourceid=chrome-instant&ie=UTF-8&ion=1"), |
| 423 | url)); |
[email protected] | fb5bcc0 | 2012-02-17 14:05:42 | [diff] [blame] | 424 | EXPECT_FALSE( |
[email protected] | c640fd7c | 2012-08-17 08:19:25 | [diff] [blame] | 425 | Matches(factory.CreateURLEqualsCondition("https://www.google.com"), url)); |
| 426 | |
| 427 | // Same as above but this time with a non-standard port. |
| 428 | gurl = GURL("https://www.google.com:1234/webhp?sourceid=chrome-instant&" |
| 429 | "ie=UTF-8&ion=1#hl=en&output=search&sclient=psy-ab&q=chrome%20is%20" |
| 430 | "awesome"); |
| 431 | url = factory.CanonicalizeURLForFullSearches(gurl); |
| 432 | EXPECT_TRUE(Matches(factory.CreateURLPrefixCondition( |
| 433 | "https://www.google.com:1234/webhp?"), url)); |
| 434 | EXPECT_TRUE(Matches(factory.CreateURLContainsCondition(":1234"), url)); |
[email protected] | fb5bcc0 | 2012-02-17 14:05:42 | [diff] [blame] | 435 | } |
| 436 | |
[email protected] | fb5bcc0 | 2012-02-17 14:05:42 | [diff] [blame] | 437 | // |
| 438 | // URLMatcherConditionSet |
| 439 | // |
| 440 | |
[email protected] | 3b001a0 | 2012-04-05 10:38:06 | [diff] [blame] | 441 | TEST(URLMatcherConditionSetTest, Constructor) { |
[email protected] | fb5bcc0 | 2012-02-17 14:05:42 | [diff] [blame] | 442 | URLMatcherConditionFactory factory; |
| 443 | URLMatcherCondition m1 = factory.CreateHostSuffixCondition("example.com"); |
| 444 | URLMatcherCondition m2 = factory.CreatePathContainsCondition("foo"); |
| 445 | |
| 446 | std::set<URLMatcherCondition> conditions; |
| 447 | conditions.insert(m1); |
| 448 | conditions.insert(m2); |
| 449 | |
[email protected] | 3b001a0 | 2012-04-05 10:38:06 | [diff] [blame] | 450 | scoped_refptr<URLMatcherConditionSet> condition_set( |
| 451 | new URLMatcherConditionSet(1, conditions)); |
| 452 | EXPECT_EQ(1, condition_set->id()); |
| 453 | EXPECT_EQ(2u, condition_set->conditions().size()); |
[email protected] | fb5bcc0 | 2012-02-17 14:05:42 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | TEST(URLMatcherConditionSetTest, Matching) { |
| 457 | GURL url1("http://www.example.com/foo?bar=1"); |
| 458 | GURL url2("http://foo.example.com/index.html"); |
[email protected] | 00520a5 | 2012-04-12 18:30:47 | [diff] [blame] | 459 | GURL url3("http://www.example.com:80/foo?bar=1"); |
| 460 | GURL url4("http://www.example.com:8080/foo?bar=1"); |
[email protected] | fb5bcc0 | 2012-02-17 14:05:42 | [diff] [blame] | 461 | |
| 462 | URLMatcherConditionFactory factory; |
| 463 | URLMatcherCondition m1 = factory.CreateHostSuffixCondition("example.com"); |
| 464 | URLMatcherCondition m2 = factory.CreatePathContainsCondition("foo"); |
| 465 | |
| 466 | std::set<URLMatcherCondition> conditions; |
| 467 | conditions.insert(m1); |
| 468 | conditions.insert(m2); |
| 469 | |
[email protected] | 3b001a0 | 2012-04-05 10:38:06 | [diff] [blame] | 470 | scoped_refptr<URLMatcherConditionSet> condition_set( |
| 471 | new URLMatcherConditionSet(1, conditions)); |
| 472 | EXPECT_EQ(1, condition_set->id()); |
| 473 | EXPECT_EQ(2u, condition_set->conditions().size()); |
[email protected] | fb5bcc0 | 2012-02-17 14:05:42 | [diff] [blame] | 474 | |
[email protected] | 5bcf3b7 | 2012-09-14 00:20:28 | [diff] [blame] | 475 | std::set<StringPattern::ID> matching_patterns; |
| 476 | matching_patterns.insert(m1.string_pattern()->id()); |
| 477 | EXPECT_FALSE(condition_set->IsMatch(matching_patterns, url1)); |
[email protected] | fb5bcc0 | 2012-02-17 14:05:42 | [diff] [blame] | 478 | |
[email protected] | 5bcf3b7 | 2012-09-14 00:20:28 | [diff] [blame] | 479 | matching_patterns.insert(m2.string_pattern()->id()); |
| 480 | EXPECT_TRUE(condition_set->IsMatch(matching_patterns, url1)); |
| 481 | EXPECT_FALSE(condition_set->IsMatch(matching_patterns, url2)); |
[email protected] | faceb0f | 2012-04-12 17:07:19 | [diff] [blame] | 482 | |
[email protected] | faceb0f | 2012-04-12 17:07:19 | [diff] [blame] | 483 | // Test scheme filters. |
| 484 | scoped_refptr<URLMatcherConditionSet> condition_set2( |
dcheng | 3f767dc3 | 2016-04-25 22:54:22 | [diff] [blame] | 485 | new URLMatcherConditionSet(1, conditions, |
| 486 | std::unique_ptr<URLMatcherSchemeFilter>( |
[email protected] | 7e6d3f6 | 2013-06-13 17:32:14 | [diff] [blame] | 487 | new URLMatcherSchemeFilter("https")), |
dcheng | 3f767dc3 | 2016-04-25 22:54:22 | [diff] [blame] | 488 | std::unique_ptr<URLMatcherPortFilter>())); |
[email protected] | 5bcf3b7 | 2012-09-14 00:20:28 | [diff] [blame] | 489 | EXPECT_FALSE(condition_set2->IsMatch(matching_patterns, url1)); |
[email protected] | faceb0f | 2012-04-12 17:07:19 | [diff] [blame] | 490 | scoped_refptr<URLMatcherConditionSet> condition_set3( |
dcheng | 3f767dc3 | 2016-04-25 22:54:22 | [diff] [blame] | 491 | new URLMatcherConditionSet(1, conditions, |
| 492 | std::unique_ptr<URLMatcherSchemeFilter>( |
[email protected] | 7e6d3f6 | 2013-06-13 17:32:14 | [diff] [blame] | 493 | new URLMatcherSchemeFilter("http")), |
dcheng | 3f767dc3 | 2016-04-25 22:54:22 | [diff] [blame] | 494 | std::unique_ptr<URLMatcherPortFilter>())); |
[email protected] | 5bcf3b7 | 2012-09-14 00:20:28 | [diff] [blame] | 495 | EXPECT_TRUE(condition_set3->IsMatch(matching_patterns, url1)); |
[email protected] | 00520a5 | 2012-04-12 18:30:47 | [diff] [blame] | 496 | |
| 497 | // Test port filters. |
| 498 | std::vector<URLMatcherPortFilter::Range> ranges; |
| 499 | ranges.push_back(URLMatcherPortFilter::CreateRange(80)); |
dcheng | 3f767dc3 | 2016-04-25 22:54:22 | [diff] [blame] | 500 | std::unique_ptr<URLMatcherPortFilter> filter( |
| 501 | new URLMatcherPortFilter(ranges)); |
[email protected] | 00520a5 | 2012-04-12 18:30:47 | [diff] [blame] | 502 | scoped_refptr<URLMatcherConditionSet> condition_set4( |
dcheng | 51ace48a | 2015-12-26 22:45:17 | [diff] [blame] | 503 | new URLMatcherConditionSet(1, conditions, |
dcheng | 3f767dc3 | 2016-04-25 22:54:22 | [diff] [blame] | 504 | std::unique_ptr<URLMatcherSchemeFilter>(), |
dcheng | 51ace48a | 2015-12-26 22:45:17 | [diff] [blame] | 505 | std::move(filter))); |
[email protected] | 5bcf3b7 | 2012-09-14 00:20:28 | [diff] [blame] | 506 | EXPECT_TRUE(condition_set4->IsMatch(matching_patterns, url1)); |
| 507 | EXPECT_TRUE(condition_set4->IsMatch(matching_patterns, url3)); |
| 508 | EXPECT_FALSE(condition_set4->IsMatch(matching_patterns, url4)); |
| 509 | |
| 510 | // Test regex patterns. |
| 511 | matching_patterns.clear(); |
| 512 | URLMatcherCondition r1 = factory.CreateURLMatchesCondition("/fo?oo"); |
| 513 | std::set<URLMatcherCondition> regex_conditions; |
| 514 | regex_conditions.insert(r1); |
| 515 | scoped_refptr<URLMatcherConditionSet> condition_set5( |
| 516 | new URLMatcherConditionSet(1, regex_conditions)); |
| 517 | EXPECT_FALSE(condition_set5->IsMatch(matching_patterns, url1)); |
| 518 | matching_patterns.insert(r1.string_pattern()->id()); |
| 519 | EXPECT_TRUE(condition_set5->IsMatch(matching_patterns, url1)); |
| 520 | |
| 521 | regex_conditions.insert(m1); |
| 522 | scoped_refptr<URLMatcherConditionSet> condition_set6( |
| 523 | new URLMatcherConditionSet(1, regex_conditions)); |
| 524 | EXPECT_FALSE(condition_set6->IsMatch(matching_patterns, url1)); |
| 525 | matching_patterns.insert(m1.string_pattern()->id()); |
| 526 | EXPECT_TRUE(condition_set6->IsMatch(matching_patterns, url1)); |
[email protected] | 2280dc8 | 2013-04-11 20:04:01 | [diff] [blame] | 527 | |
| 528 | matching_patterns.clear(); |
| 529 | regex_conditions.clear(); |
| 530 | URLMatcherCondition r2 = factory.CreateOriginAndPathMatchesCondition("b[a]r"); |
| 531 | regex_conditions.insert(r2); |
| 532 | scoped_refptr<URLMatcherConditionSet> condition_set7( |
| 533 | new URLMatcherConditionSet(1, regex_conditions)); |
| 534 | EXPECT_FALSE(condition_set7->IsMatch(matching_patterns, url1)); |
| 535 | matching_patterns.insert(r2.string_pattern()->id()); |
| 536 | EXPECT_TRUE(condition_set7->IsMatch(matching_patterns, url1)); |
[email protected] | fb5bcc0 | 2012-02-17 14:05:42 | [diff] [blame] | 537 | } |
| 538 | |
[email protected] | c967c00 | 2014-04-11 13:45:02 | [diff] [blame] | 539 | namespace { |
| 540 | |
| 541 | bool IsQueryMatch( |
| 542 | const std::string& url_query, |
| 543 | const std::string& key, |
| 544 | URLQueryElementMatcherCondition::QueryElementType query_element_type, |
| 545 | const std::string& value, |
| 546 | URLQueryElementMatcherCondition::QueryValueMatchType query_value_match_type, |
| 547 | URLQueryElementMatcherCondition::Type match_type) { |
| 548 | URLMatcherConditionFactory factory; |
| 549 | |
| 550 | URLMatcherCondition m1 = factory.CreateHostSuffixCondition("example.com"); |
| 551 | URLMatcherCondition m2 = factory.CreatePathContainsCondition("foo"); |
| 552 | URLMatcherConditionSet::Conditions conditions; |
| 553 | conditions.insert(m1); |
| 554 | conditions.insert(m2); |
| 555 | |
| 556 | URLQueryElementMatcherCondition q1(key, |
| 557 | value, |
| 558 | query_value_match_type, |
| 559 | query_element_type, |
| 560 | match_type, |
| 561 | &factory); |
| 562 | URLMatcherConditionSet::QueryConditions query_conditions; |
| 563 | query_conditions.insert(q1); |
| 564 | |
dcheng | 3f767dc3 | 2016-04-25 22:54:22 | [diff] [blame] | 565 | std::unique_ptr<URLMatcherSchemeFilter> scheme_filter; |
| 566 | std::unique_ptr<URLMatcherPortFilter> port_filter; |
[email protected] | c967c00 | 2014-04-11 13:45:02 | [diff] [blame] | 567 | |
| 568 | scoped_refptr<URLMatcherConditionSet> condition_set( |
dcheng | 51ace48a | 2015-12-26 22:45:17 | [diff] [blame] | 569 | new URLMatcherConditionSet(1, conditions, query_conditions, |
| 570 | std::move(scheme_filter), |
| 571 | std::move(port_filter))); |
[email protected] | c967c00 | 2014-04-11 13:45:02 | [diff] [blame] | 572 | |
| 573 | GURL url("http://www.example.com/foo?" + url_query); |
| 574 | |
| 575 | URLMatcher matcher; |
| 576 | URLMatcherConditionSet::Vector vector; |
| 577 | vector.push_back(condition_set); |
| 578 | matcher.AddConditionSets(vector); |
| 579 | |
| 580 | return matcher.MatchURL(url).size() == 1; |
| 581 | } |
| 582 | |
| 583 | } // namespace |
| 584 | |
| 585 | TEST(URLMatcherConditionSetTest, QueryMatching) { |
| 586 | EXPECT_TRUE( |
| 587 | IsQueryMatch("a=foo&b=foo&a=barr", |
| 588 | "a", |
| 589 | URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY_VALUE, |
| 590 | "bar", |
| 591 | URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_PREFIX, |
| 592 | URLQueryElementMatcherCondition::MATCH_ANY)); |
| 593 | EXPECT_FALSE( |
| 594 | IsQueryMatch("a=foo&b=foo&a=barr", |
| 595 | "a", |
| 596 | URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY_VALUE, |
| 597 | "bar", |
| 598 | URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_EXACT, |
| 599 | URLQueryElementMatcherCondition::MATCH_ANY)); |
| 600 | EXPECT_TRUE( |
| 601 | IsQueryMatch("a=foo&b=foo&a=barr", |
| 602 | "a", |
| 603 | URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY, |
| 604 | "bar", |
| 605 | URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_PREFIX, |
| 606 | URLQueryElementMatcherCondition::MATCH_ANY)); |
| 607 | EXPECT_FALSE( |
| 608 | IsQueryMatch("a=foo&b=foo&a=barr", |
| 609 | "a", |
| 610 | URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY, |
| 611 | "bar", |
| 612 | URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_EXACT, |
| 613 | URLQueryElementMatcherCondition::MATCH_ANY)); |
| 614 | EXPECT_TRUE( |
| 615 | IsQueryMatch("a&b=foo&a=barr", |
| 616 | "a", |
| 617 | URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY, |
| 618 | "bar", |
| 619 | URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_EXACT, |
| 620 | URLQueryElementMatcherCondition::MATCH_ANY)); |
| 621 | EXPECT_FALSE( |
| 622 | IsQueryMatch("a=foo&b=foo&a=barr", |
| 623 | "a", |
| 624 | URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY, |
| 625 | "bar", |
| 626 | URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_EXACT, |
| 627 | URLQueryElementMatcherCondition::MATCH_ANY)); |
| 628 | |
| 629 | EXPECT_FALSE( |
| 630 | IsQueryMatch("a=foo&b=foo&a=bar", |
| 631 | "a", |
| 632 | URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY_VALUE, |
| 633 | "bar", |
| 634 | URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_EXACT, |
| 635 | URLQueryElementMatcherCondition::MATCH_ALL)); |
| 636 | EXPECT_TRUE( |
| 637 | IsQueryMatch("a=bar&b=foo&a=bar", |
| 638 | "a", |
| 639 | URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY_VALUE, |
| 640 | "bar", |
| 641 | URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_EXACT, |
| 642 | URLQueryElementMatcherCondition::MATCH_ALL)); |
| 643 | EXPECT_TRUE( |
| 644 | IsQueryMatch("a=bar&b=foo&a=bar", |
| 645 | "b", |
| 646 | URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY_VALUE, |
| 647 | "foo", |
| 648 | URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_EXACT, |
| 649 | URLQueryElementMatcherCondition::MATCH_ALL)); |
| 650 | EXPECT_FALSE( |
| 651 | IsQueryMatch("a=bar&b=foo&a=bar", |
| 652 | "b", |
| 653 | URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY_VALUE, |
| 654 | "goo", |
| 655 | URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_EXACT, |
| 656 | URLQueryElementMatcherCondition::MATCH_ALL)); |
| 657 | EXPECT_FALSE( |
| 658 | IsQueryMatch("a=bar&b=foo&a=bar", |
| 659 | "c", |
| 660 | URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY_VALUE, |
| 661 | "goo", |
| 662 | URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_EXACT, |
| 663 | URLQueryElementMatcherCondition::MATCH_ALL)); |
| 664 | EXPECT_TRUE( |
| 665 | IsQueryMatch("a=foo1&b=foo&a=foo2", |
| 666 | "a", |
| 667 | URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY_VALUE, |
| 668 | "foo", |
| 669 | URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_PREFIX, |
| 670 | URLQueryElementMatcherCondition::MATCH_ALL)); |
| 671 | EXPECT_FALSE( |
| 672 | IsQueryMatch("a=foo1&b=foo&a=fo02", |
| 673 | "a", |
| 674 | URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY_VALUE, |
| 675 | "foo", |
| 676 | URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_PREFIX, |
| 677 | URLQueryElementMatcherCondition::MATCH_ALL)); |
| 678 | EXPECT_TRUE( |
| 679 | IsQueryMatch("a&b=foo&a", |
| 680 | "a", |
| 681 | URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY, |
| 682 | "foo", |
| 683 | URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_PREFIX, |
| 684 | URLQueryElementMatcherCondition::MATCH_ALL)); |
| 685 | EXPECT_TRUE( |
| 686 | IsQueryMatch("alt&b=foo", |
| 687 | "a", |
| 688 | URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY, |
| 689 | "foo", |
| 690 | URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_PREFIX, |
| 691 | URLQueryElementMatcherCondition::MATCH_ALL)); |
| 692 | EXPECT_TRUE( |
| 693 | IsQueryMatch("b=foo&a", |
| 694 | "a", |
| 695 | URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY, |
| 696 | "foo", |
| 697 | URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_PREFIX, |
| 698 | URLQueryElementMatcherCondition::MATCH_ALL)); |
| 699 | EXPECT_FALSE( |
| 700 | IsQueryMatch("b=foo", |
| 701 | "a", |
| 702 | URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY, |
| 703 | "foo", |
| 704 | URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_PREFIX, |
| 705 | URLQueryElementMatcherCondition::MATCH_ALL)); |
| 706 | EXPECT_TRUE( |
| 707 | IsQueryMatch("b=foo&a", |
| 708 | "a", |
| 709 | URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY, |
| 710 | "foo", |
| 711 | URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_EXACT, |
| 712 | URLQueryElementMatcherCondition::MATCH_ALL)); |
| 713 | |
| 714 | EXPECT_TRUE( |
| 715 | IsQueryMatch("a=foo&b=foo&a=bar", |
| 716 | "a", |
| 717 | URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY_VALUE, |
| 718 | "foo", |
| 719 | URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_EXACT, |
| 720 | URLQueryElementMatcherCondition::MATCH_FIRST)); |
| 721 | EXPECT_FALSE( |
| 722 | IsQueryMatch("a=foo&b=foo&a=bar", |
| 723 | "a", |
| 724 | URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY_VALUE, |
| 725 | "bar", |
| 726 | URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_EXACT, |
| 727 | URLQueryElementMatcherCondition::MATCH_FIRST)); |
| 728 | EXPECT_TRUE( |
| 729 | IsQueryMatch("a=foo1&b=foo&a=bar", |
| 730 | "a", |
| 731 | URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY_VALUE, |
| 732 | "foo", |
| 733 | URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_PREFIX, |
| 734 | URLQueryElementMatcherCondition::MATCH_FIRST)); |
| 735 | EXPECT_FALSE( |
| 736 | IsQueryMatch("a=foo1&b=foo&a=bar", |
| 737 | "a", |
| 738 | URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY_VALUE, |
| 739 | "foo", |
| 740 | URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_EXACT, |
| 741 | URLQueryElementMatcherCondition::MATCH_FIRST)); |
| 742 | EXPECT_TRUE( |
| 743 | IsQueryMatch("a&b=foo&a=bar", |
| 744 | "a", |
| 745 | URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY, |
| 746 | "foo", |
| 747 | URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_EXACT, |
| 748 | URLQueryElementMatcherCondition::MATCH_FIRST)); |
| 749 | EXPECT_TRUE( |
| 750 | IsQueryMatch("alt&b=foo&a=bar", |
| 751 | "a", |
| 752 | URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY, |
| 753 | "foo", |
| 754 | URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_PREFIX, |
| 755 | URLQueryElementMatcherCondition::MATCH_FIRST)); |
| 756 | EXPECT_FALSE( |
| 757 | IsQueryMatch("alt&b=foo&a=bar", |
| 758 | "a", |
| 759 | URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY, |
| 760 | "foo", |
| 761 | URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_EXACT, |
| 762 | URLQueryElementMatcherCondition::MATCH_FIRST)); |
| 763 | |
| 764 | EXPECT_FALSE( |
| 765 | IsQueryMatch("a=foo&b=foo&a=bar", |
| 766 | "a", |
| 767 | URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY_VALUE, |
| 768 | "foo", |
| 769 | URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_EXACT, |
| 770 | URLQueryElementMatcherCondition::MATCH_LAST)); |
| 771 | EXPECT_TRUE( |
| 772 | IsQueryMatch("a=foo&b=foo&a=bar", |
| 773 | "a", |
| 774 | URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY_VALUE, |
| 775 | "bar", |
| 776 | URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_EXACT, |
| 777 | URLQueryElementMatcherCondition::MATCH_LAST)); |
| 778 | EXPECT_FALSE( |
| 779 | IsQueryMatch("a=foo1&b=foo&a=bar", |
| 780 | "a", |
| 781 | URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY_VALUE, |
| 782 | "foo", |
| 783 | URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_PREFIX, |
| 784 | URLQueryElementMatcherCondition::MATCH_LAST)); |
| 785 | EXPECT_TRUE( |
| 786 | IsQueryMatch("a=foo1&b=foo&a=bar1", |
| 787 | "a", |
| 788 | URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY_VALUE, |
| 789 | "bar", |
| 790 | URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_PREFIX, |
| 791 | URLQueryElementMatcherCondition::MATCH_LAST)); |
| 792 | EXPECT_FALSE( |
| 793 | IsQueryMatch("a&b=foo&a=bar", |
| 794 | "a", |
| 795 | URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY, |
| 796 | "foo", |
| 797 | URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_EXACT, |
| 798 | URLQueryElementMatcherCondition::MATCH_LAST)); |
| 799 | EXPECT_TRUE( |
| 800 | IsQueryMatch("b=foo&alt", |
| 801 | "a", |
| 802 | URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY, |
| 803 | "foo", |
| 804 | URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_PREFIX, |
| 805 | URLQueryElementMatcherCondition::MATCH_LAST)); |
| 806 | EXPECT_FALSE( |
| 807 | IsQueryMatch("b=foo&alt", |
| 808 | "a", |
| 809 | URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY, |
| 810 | "foo", |
| 811 | URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_EXACT, |
| 812 | URLQueryElementMatcherCondition::MATCH_LAST)); |
| 813 | } |
[email protected] | fb5bcc0 | 2012-02-17 14:05:42 | [diff] [blame] | 814 | |
| 815 | // |
| 816 | // URLMatcher |
| 817 | // |
| 818 | |
| 819 | TEST(URLMatcherTest, FullTest) { |
| 820 | GURL url1("http://www.example.com/foo?bar=1"); |
| 821 | GURL url2("http://foo.example.com/index.html"); |
| 822 | |
| 823 | URLMatcher matcher; |
| 824 | URLMatcherConditionFactory* factory = matcher.condition_factory(); |
| 825 | |
| 826 | // First insert. |
| 827 | URLMatcherConditionSet::Conditions conditions1; |
| 828 | conditions1.insert(factory->CreateHostSuffixCondition("example.com")); |
| 829 | conditions1.insert(factory->CreatePathContainsCondition("foo")); |
| 830 | |
| 831 | const int kConditionSetId1 = 1; |
[email protected] | 3b001a0 | 2012-04-05 10:38:06 | [diff] [blame] | 832 | URLMatcherConditionSet::Vector insert1; |
kylechar | 1e0dd6b | 2017-10-03 16:07:51 | [diff] [blame] | 833 | insert1.push_back(base::MakeRefCounted<URLMatcherConditionSet>( |
| 834 | kConditionSetId1, conditions1)); |
[email protected] | fb5bcc0 | 2012-02-17 14:05:42 | [diff] [blame] | 835 | matcher.AddConditionSets(insert1); |
| 836 | EXPECT_EQ(1u, matcher.MatchURL(url1).size()); |
| 837 | EXPECT_EQ(0u, matcher.MatchURL(url2).size()); |
| 838 | |
| 839 | // Second insert. |
| 840 | URLMatcherConditionSet::Conditions conditions2; |
| 841 | conditions2.insert(factory->CreateHostSuffixCondition("example.com")); |
| 842 | |
| 843 | const int kConditionSetId2 = 2; |
[email protected] | 3b001a0 | 2012-04-05 10:38:06 | [diff] [blame] | 844 | URLMatcherConditionSet::Vector insert2; |
kylechar | 1e0dd6b | 2017-10-03 16:07:51 | [diff] [blame] | 845 | insert2.push_back(base::MakeRefCounted<URLMatcherConditionSet>( |
| 846 | kConditionSetId2, conditions2)); |
[email protected] | fb5bcc0 | 2012-02-17 14:05:42 | [diff] [blame] | 847 | matcher.AddConditionSets(insert2); |
| 848 | EXPECT_EQ(2u, matcher.MatchURL(url1).size()); |
| 849 | EXPECT_EQ(1u, matcher.MatchURL(url2).size()); |
| 850 | |
| 851 | // This should be the cached singleton. |
| 852 | int patternId1 = factory->CreateHostSuffixCondition( |
[email protected] | 5bcf3b7 | 2012-09-14 00:20:28 | [diff] [blame] | 853 | "example.com").string_pattern()->id(); |
[email protected] | fb5bcc0 | 2012-02-17 14:05:42 | [diff] [blame] | 854 | |
[email protected] | 5bcf3b7 | 2012-09-14 00:20:28 | [diff] [blame] | 855 | // Third insert. |
| 856 | URLMatcherConditionSet::Conditions conditions3; |
| 857 | conditions3.insert(factory->CreateHostSuffixCondition("example.com")); |
| 858 | conditions3.insert(factory->CreateURLMatchesCondition("x.*[0-9]")); |
| 859 | |
| 860 | const int kConditionSetId3 = 3; |
| 861 | URLMatcherConditionSet::Vector insert3; |
kylechar | 1e0dd6b | 2017-10-03 16:07:51 | [diff] [blame] | 862 | insert3.push_back(base::MakeRefCounted<URLMatcherConditionSet>( |
| 863 | kConditionSetId3, conditions3)); |
[email protected] | 5bcf3b7 | 2012-09-14 00:20:28 | [diff] [blame] | 864 | matcher.AddConditionSets(insert3); |
| 865 | EXPECT_EQ(3u, matcher.MatchURL(url1).size()); |
| 866 | EXPECT_EQ(1u, matcher.MatchURL(url2).size()); |
| 867 | |
| 868 | // Removal of third insert. |
| 869 | std::vector<URLMatcherConditionSet::ID> remove3; |
| 870 | remove3.push_back(kConditionSetId3); |
| 871 | matcher.RemoveConditionSets(remove3); |
| 872 | EXPECT_EQ(2u, matcher.MatchURL(url1).size()); |
| 873 | EXPECT_EQ(1u, matcher.MatchURL(url2).size()); |
| 874 | |
| 875 | // Removal of second insert. |
[email protected] | fb5bcc0 | 2012-02-17 14:05:42 | [diff] [blame] | 876 | std::vector<URLMatcherConditionSet::ID> remove2; |
| 877 | remove2.push_back(kConditionSetId2); |
| 878 | matcher.RemoveConditionSets(remove2); |
| 879 | EXPECT_EQ(1u, matcher.MatchURL(url1).size()); |
| 880 | EXPECT_EQ(0u, matcher.MatchURL(url2).size()); |
| 881 | |
| 882 | // Removal of first insert. |
| 883 | std::vector<URLMatcherConditionSet::ID> remove1; |
| 884 | remove1.push_back(kConditionSetId1); |
| 885 | matcher.RemoveConditionSets(remove1); |
| 886 | EXPECT_EQ(0u, matcher.MatchURL(url1).size()); |
| 887 | EXPECT_EQ(0u, matcher.MatchURL(url2).size()); |
| 888 | |
[email protected] | 357c4db | 2012-03-29 07:51:57 | [diff] [blame] | 889 | EXPECT_TRUE(matcher.IsEmpty()); |
| 890 | |
[email protected] | fb5bcc0 | 2012-02-17 14:05:42 | [diff] [blame] | 891 | // The cached singleton in matcher.condition_factory_ should be destroyed to |
| 892 | // free memory. |
| 893 | int patternId2 = factory->CreateHostSuffixCondition( |
[email protected] | 5bcf3b7 | 2012-09-14 00:20:28 | [diff] [blame] | 894 | "example.com").string_pattern()->id(); |
[email protected] | fb5bcc0 | 2012-02-17 14:05:42 | [diff] [blame] | 895 | // If patternId1 and patternId2 are different that indicates that |
[email protected] | 5bcf3b7 | 2012-09-14 00:20:28 | [diff] [blame] | 896 | // matcher.condition_factory_ does not leak memory by holding onto |
| 897 | // unused patterns. |
[email protected] | fb5bcc0 | 2012-02-17 14:05:42 | [diff] [blame] | 898 | EXPECT_NE(patternId1, patternId2); |
| 899 | } |
| 900 | |
[email protected] | ea6249b | 2012-12-06 18:45:20 | [diff] [blame] | 901 | TEST(URLMatcherTest, TestComponentsImplyContains) { |
| 902 | // Due to a different implementation of component (prefix, suffix and equals) |
| 903 | // and *Contains conditions we need to check that when a pattern matches a |
| 904 | // given part of a URL as equal, prefix or suffix, it also matches it in the |
| 905 | // "contains" test. |
| 906 | GURL url("https://www.google.com:1234/webhp?test=val&a=b"); |
| 907 | |
| 908 | URLMatcher matcher; |
| 909 | URLMatcherConditionFactory* factory = matcher.condition_factory(); |
| 910 | |
| 911 | URLMatcherConditionSet::Conditions conditions; |
| 912 | |
| 913 | // First insert all the matching equals => contains pairs. |
| 914 | conditions.insert(factory->CreateHostEqualsCondition("www.google.com")); |
| 915 | conditions.insert(factory->CreateHostContainsCondition("www.google.com")); |
| 916 | |
| 917 | conditions.insert(factory->CreateHostPrefixCondition("www.")); |
| 918 | conditions.insert(factory->CreateHostContainsCondition("www.")); |
| 919 | |
| 920 | conditions.insert(factory->CreateHostSuffixCondition("com")); |
| 921 | conditions.insert(factory->CreateHostContainsCondition("com")); |
| 922 | |
| 923 | conditions.insert(factory->CreatePathEqualsCondition("/webhp")); |
| 924 | conditions.insert(factory->CreatePathContainsCondition("/webhp")); |
| 925 | |
| 926 | conditions.insert(factory->CreatePathPrefixCondition("/we")); |
| 927 | conditions.insert(factory->CreatePathContainsCondition("/we")); |
| 928 | |
| 929 | conditions.insert(factory->CreatePathSuffixCondition("hp")); |
| 930 | conditions.insert(factory->CreatePathContainsCondition("hp")); |
| 931 | |
| 932 | conditions.insert(factory->CreateQueryEqualsCondition("test=val&a=b")); |
| 933 | conditions.insert(factory->CreateQueryContainsCondition("test=val&a=b")); |
| 934 | |
| 935 | conditions.insert(factory->CreateQueryPrefixCondition("test=v")); |
| 936 | conditions.insert(factory->CreateQueryContainsCondition("test=v")); |
| 937 | |
| 938 | conditions.insert(factory->CreateQuerySuffixCondition("l&a=b")); |
| 939 | conditions.insert(factory->CreateQueryContainsCondition("l&a=b")); |
| 940 | |
| 941 | // The '?' for equality is just ignored. |
| 942 | conditions.insert(factory->CreateQueryEqualsCondition("?test=val&a=b")); |
| 943 | // Due to '?' the condition created here is a prefix-testing condition. |
| 944 | conditions.insert(factory->CreateQueryContainsCondition("?test=val&a=b")); |
| 945 | |
| 946 | const int kConditionSetId = 1; |
| 947 | URLMatcherConditionSet::Vector insert; |
kylechar | 1e0dd6b | 2017-10-03 16:07:51 | [diff] [blame] | 948 | insert.push_back(base::MakeRefCounted<URLMatcherConditionSet>(kConditionSetId, |
| 949 | conditions)); |
[email protected] | ea6249b | 2012-12-06 18:45:20 | [diff] [blame] | 950 | matcher.AddConditionSets(insert); |
| 951 | EXPECT_EQ(1u, matcher.MatchURL(url).size()); |
| 952 | } |
| 953 | |
[email protected] | 2280dc8 | 2013-04-11 20:04:01 | [diff] [blame] | 954 | // Check that matches in everything but the query are found. |
| 955 | TEST(URLMatcherTest, TestOriginAndPathRegExPositive) { |
| 956 | GURL url("https://www.google.com:1234/webhp?test=val&a=b"); |
| 957 | |
| 958 | URLMatcher matcher; |
| 959 | URLMatcherConditionFactory* factory = matcher.condition_factory(); |
| 960 | |
| 961 | URLMatcherConditionSet::Conditions conditions; |
| 962 | |
| 963 | conditions.insert(factory->CreateOriginAndPathMatchesCondition("w..hp")); |
| 964 | const int kConditionSetId = 1; |
| 965 | URLMatcherConditionSet::Vector insert; |
kylechar | 1e0dd6b | 2017-10-03 16:07:51 | [diff] [blame] | 966 | insert.push_back(base::MakeRefCounted<URLMatcherConditionSet>(kConditionSetId, |
| 967 | conditions)); |
[email protected] | 2280dc8 | 2013-04-11 20:04:01 | [diff] [blame] | 968 | matcher.AddConditionSets(insert); |
| 969 | EXPECT_EQ(1u, matcher.MatchURL(url).size()); |
| 970 | } |
| 971 | |
| 972 | // Check that matches in the query are ignored. |
| 973 | TEST(URLMatcherTest, TestOriginAndPathRegExNegative) { |
| 974 | GURL url("https://www.google.com:1234/webhp?test=val&a=b"); |
| 975 | |
| 976 | URLMatcher matcher; |
| 977 | URLMatcherConditionFactory* factory = matcher.condition_factory(); |
| 978 | |
| 979 | URLMatcherConditionSet::Conditions conditions; |
| 980 | |
| 981 | conditions.insert(factory->CreateOriginAndPathMatchesCondition("val")); |
| 982 | const int kConditionSetId = 1; |
| 983 | URLMatcherConditionSet::Vector insert; |
kylechar | 1e0dd6b | 2017-10-03 16:07:51 | [diff] [blame] | 984 | insert.push_back(base::MakeRefCounted<URLMatcherConditionSet>(kConditionSetId, |
| 985 | conditions)); |
[email protected] | 2280dc8 | 2013-04-11 20:04:01 | [diff] [blame] | 986 | matcher.AddConditionSets(insert); |
| 987 | EXPECT_EQ(0u, matcher.MatchURL(url).size()); |
| 988 | } |
| 989 | |
[email protected] | 716c016 | 2013-12-13 20:36:53 | [diff] [blame] | 990 | } // namespace url_matcher |