blob: c9304a813d4dbf29cbbb67b06f943e7a281415c7 [file] [log] [blame]
[email protected]716c0162013-12-13 20:36:531// Copyright 2013 The Chromium Authors. All rights reserved.
[email protected]fb5bcc02012-02-17 14:05:422// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]716c0162013-12-13 20:36:535#include "components/url_matcher/url_matcher.h"
[email protected]fb5bcc02012-02-17 14:05:426
avi5dd91f82015-12-25 22:30:467#include <stddef.h>
dcheng3f767dc32016-04-25 22:54:228
9#include <memory>
dcheng51ace48a2015-12-26 22:45:1710#include <utility>
avi5dd91f82015-12-25 22:30:4611
12#include "base/macros.h"
[email protected]f4390962013-06-11 07:29:2213#include "base/strings/string_util.h"
[email protected]fb5bcc02012-02-17 14:05:4214#include "testing/gtest/include/gtest/gtest.h"
[email protected]79fe2272013-07-13 20:01:4015#include "url/gurl.h"
[email protected]fb5bcc02012-02-17 14:05:4216
[email protected]716c0162013-12-13 20:36:5317namespace url_matcher {
[email protected]fb5bcc02012-02-17 14:05:4218
19//
20// URLMatcherCondition
21//
22
23TEST(URLMatcherConditionTest, Constructors) {
[email protected]5bcf3b72012-09-14 00:20:2824 StringPattern pattern("example.com", 1);
[email protected]fb5bcc02012-02-17 14:05:4225 URLMatcherCondition m1(URLMatcherCondition::HOST_SUFFIX, &pattern);
26 EXPECT_EQ(URLMatcherCondition::HOST_SUFFIX, m1.criterion());
[email protected]5bcf3b72012-09-14 00:20:2827 EXPECT_EQ(&pattern, m1.string_pattern());
[email protected]fb5bcc02012-02-17 14:05:4228
29 URLMatcherCondition m2;
30 m2 = m1;
31 EXPECT_EQ(URLMatcherCondition::HOST_SUFFIX, m2.criterion());
[email protected]5bcf3b72012-09-14 00:20:2832 EXPECT_EQ(&pattern, m2.string_pattern());
[email protected]fb5bcc02012-02-17 14:05:4233
34 URLMatcherCondition m3(m1);
35 EXPECT_EQ(URLMatcherCondition::HOST_SUFFIX, m3.criterion());
[email protected]5bcf3b72012-09-14 00:20:2836 EXPECT_EQ(&pattern, m3.string_pattern());
[email protected]fb5bcc02012-02-17 14:05:4237}
38
[email protected]faceb0f2012-04-12 17:07:1939TEST(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]00520a52012-04-12 18:30:4754TEST(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]fb5bcc02012-02-17 14:05:4269TEST(URLMatcherConditionTest, IsFullURLCondition) {
[email protected]5bcf3b72012-09-14 00:20:2870 StringPattern pattern("example.com", 1);
[email protected]fb5bcc02012-02-17 14:05:4271 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
91TEST(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]5bcf3b72012-09-14 00:20:2895 StringPattern pattern("example.com", 1);
[email protected]fb5bcc02012-02-17 14:05:4296 URLMatcherCondition m1(URLMatcherCondition::HOST_SUFFIX, &pattern);
97
[email protected]5bcf3b72012-09-14 00:20:2898 std::set<StringPattern::ID> matching_patterns;
[email protected]fb5bcc02012-02-17 14:05:4299
100 // matches = {0} --> matcher did not indicate that m1 was a match.
[email protected]5bcf3b72012-09-14 00:20:28101 matching_patterns.insert(0);
102 EXPECT_FALSE(m1.IsMatch(matching_patterns, url1));
[email protected]fb5bcc02012-02-17 14:05:42103
104 // matches = {0, 1} --> matcher did indicate that m1 was a match.
[email protected]5bcf3b72012-09-14 00:20:28105 matching_patterns.insert(1);
106 EXPECT_TRUE(m1.IsMatch(matching_patterns, url1));
[email protected]fb5bcc02012-02-17 14:05:42107
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]5bcf3b72012-09-14 00:20:28112 EXPECT_TRUE(m2.IsMatch(matching_patterns, url1));
113 EXPECT_FALSE(m2.IsMatch(matching_patterns, url2));
[email protected]fb5bcc02012-02-17 14:05:42114}
115
116TEST(URLMatcherConditionTest, Comparison) {
[email protected]5bcf3b72012-09-14 00:20:28117 StringPattern p1("foobar.com", 1);
118 StringPattern p2("foobar.com", 2);
[email protected]fb5bcc02012-02-17 14:05:42119 // 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 Kotenkov75b1c3a2017-10-24 14:47:24125 {URLMatcherCondition(URLMatcherCondition::HOST_PREFIX, nullptr),
[email protected]fb5bcc02012-02-17 14:05:42126 URLMatcherCondition(URLMatcherCondition::HOST_PREFIX, &p2)},
127 {URLMatcherCondition(URLMatcherCondition::HOST_PREFIX, &p1),
Ivan Kotenkov75b1c3a2017-10-24 14:47:24128 URLMatcherCondition(URLMatcherCondition::HOST_SUFFIX, nullptr)},
[email protected]fb5bcc02012-02-17 14:05:42129 };
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 Kotenkov75b1c3a2017-10-24 14:47:24139 {URLMatcherCondition(URLMatcherCondition::HOST_PREFIX, nullptr),
140 URLMatcherCondition(URLMatcherCondition::HOST_PREFIX, nullptr)},
[email protected]fb5bcc02012-02-17 14:05:42141 };
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
154namespace {
155
ki.stfu939799a42015-09-28 04:41:20156bool Matches(const URLMatcherCondition& condition, const std::string& text) {
[email protected]5bcf3b72012-09-14 00:20:28157 return text.find(condition.string_pattern()->pattern()) !=
[email protected]fb5bcc02012-02-17 14:05:42158 std::string::npos;
159}
160
161} // namespace
162
163TEST(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]527965412014-05-07 14:38:26168 EXPECT_TRUE(base::IsStringASCII(url.host()));
169 EXPECT_TRUE(base::IsStringASCII(url.path()));
170 EXPECT_TRUE(base::IsStringASCII(url.query()));
Mike Westf8f6ed52017-10-09 20:58:50171 EXPECT_TRUE(base::IsStringASCII(url.ref()));
[email protected]fb5bcc02012-02-17 14:05:42172}
173
[email protected]6d8e5e42012-07-17 15:54:45174TEST(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]5bcf3b72012-09-14 00:20:28214 EXPECT_EQ(URLMatcherCondition::URL_MATCHES,
215 factory.CreateURLMatchesCondition("foo").criterion());
[email protected]6d8e5e42012-07-17 15:54:45216}
217
[email protected]fb5bcc02012-02-17 14:05:42218TEST(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]5bcf3b72012-09-14 00:20:28223 EXPECT_EQ(c1.string_pattern(), c2.string_pattern());
[email protected]fb5bcc02012-02-17 14:05:42224 URLMatcherCondition c3 = factory.CreateHostEqualsCondition("www.google.de");
225 EXPECT_EQ(c2.criterion(), c3.criterion());
[email protected]5bcf3b72012-09-14 00:20:28226 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]fb5bcc02012-02-17 14:05:42236
[email protected]5bcf3b72012-09-14 00:20:28237 // Check that all StringPattern singletons are freed if we call
[email protected]fb5bcc02012-02-17 14:05:42238 // ForgetUnusedPatterns.
[email protected]5bcf3b72012-09-14 00:20:28239 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]357c4db2012-03-29 07:51:57242 EXPECT_TRUE(factory.IsEmpty());
[email protected]5bcf3b72012-09-14 00:20:28243 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]fb5bcc02012-02-17 14:05:42247}
248
249TEST(URLMatcherConditionFactoryTest, TestComponentSearches) {
mnisslerd9cdcd872015-05-29 13:57:41250 URLMatcherConditionFactory factory;
[email protected]c640fd7c2012-08-17 08:19:25251 GURL gurl("https://www.google.com:1234/webhp?sourceid=chrome-instant&ie=UTF-8"
[email protected]fb5bcc02012-02-17 14:05:42252 "&ion=1#hl=en&output=search&sclient=psy-ab&q=chrome%20is%20awesome");
[email protected]fb5bcc02012-02-17 14:05:42253 std::string url = factory.CanonicalizeURLForComponentSearches(gurl);
mnisslerd9cdcd872015-05-29 13:57:41254 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]fb5bcc02012-02-17 14:05:42258
259 // Test host component.
[email protected]007b3f82013-04-09 08:46:45260 EXPECT_TRUE(Matches(factory.CreateHostPrefixCondition(std::string()), url));
[email protected]fb5bcc02012-02-17 14:05:42261 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]007b3f82013-04-09 08:46:45271 EXPECT_TRUE(Matches(factory.CreateHostSuffixCondition(std::string()), url));
mnisslerd9cdcd872015-05-29 13:57:41272 EXPECT_TRUE(Matches(factory.CreateHostSuffixCondition(std::string()), url2));
[email protected]fb5bcc02012-02-17 14:05:42273 EXPECT_TRUE(Matches(factory.CreateHostSuffixCondition("com"), url));
mnisslerd9cdcd872015-05-29 13:57:41274 EXPECT_TRUE(Matches(factory.CreateHostSuffixCondition("com"), url2));
[email protected]fb5bcc02012-02-17 14:05:42275 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));
mnisslerd9cdcd872015-05-29 13:57:41280 EXPECT_TRUE(
281 Matches(factory.CreateHostSuffixCondition(".www.google.com"), url2));
282 EXPECT_TRUE(
283 Matches(factory.CreateHostSuffixCondition(".www.google.com."), url));
[email protected]fb5bcc02012-02-17 14:05:42284 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]007b3f82013-04-09 08:46:45289 EXPECT_FALSE(Matches(factory.CreateHostEqualsCondition(std::string()), url));
[email protected]fb5bcc02012-02-17 14:05:42290 EXPECT_FALSE(Matches(factory.CreateHostEqualsCondition("www"), url));
291 EXPECT_TRUE(
292 Matches(factory.CreateHostEqualsCondition("www.google.com"), url));
mnisslerd9cdcd872015-05-29 13:57:41293 EXPECT_TRUE(
294 Matches(factory.CreateHostEqualsCondition("www.google.com"), url2));
[email protected]fb5bcc02012-02-17 14:05:42295 EXPECT_FALSE(
296 Matches(factory.CreateHostEqualsCondition("www.google.com/"), url));
mnisslerd9cdcd872015-05-29 13:57:41297 EXPECT_TRUE(
298 Matches(factory.CreateHostEqualsCondition(".www.google.com."), url));
299 EXPECT_TRUE(
300 Matches(factory.CreateHostEqualsCondition(".www.google.com."), url2));
[email protected]fb5bcc02012-02-17 14:05:42301
302 // Test path component.
[email protected]007b3f82013-04-09 08:46:45303 EXPECT_TRUE(Matches(factory.CreatePathPrefixCondition(std::string()), url));
[email protected]fb5bcc02012-02-17 14:05:42304 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]ea6249b2012-12-06 18:45:20308 EXPECT_FALSE(Matches(factory.CreatePathPrefixCondition("?sourceid"), url));
[email protected]fb5bcc02012-02-17 14:05:42309
[email protected]007b3f82013-04-09 08:46:45310 EXPECT_TRUE(Matches(factory.CreatePathSuffixCondition(std::string()), url));
[email protected]fb5bcc02012-02-17 14:05:42311 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]007b3f82013-04-09 08:46:45324 EXPECT_TRUE(Matches(factory.CreateQueryPrefixCondition(std::string()), url));
[email protected]ea6249b2012-12-06 18:45:20325 EXPECT_TRUE(Matches(factory.CreateQueryPrefixCondition("sourceid"), url));
326 // The '?' at the beginning is just ignored.
[email protected]fb5bcc02012-02-17 14:05:42327 EXPECT_TRUE(Matches(factory.CreateQueryPrefixCondition("?sourceid"), url));
[email protected]fb5bcc02012-02-17 14:05:42328
[email protected]007b3f82013-04-09 08:46:45329 EXPECT_TRUE(Matches(factory.CreateQuerySuffixCondition(std::string()), url));
[email protected]fb5bcc02012-02-17 14:05:42330 EXPECT_TRUE(Matches(factory.CreateQuerySuffixCondition("ion=1"), url));
[email protected]fb5bcc02012-02-17 14:05:42331 EXPECT_FALSE(Matches(factory.CreateQuerySuffixCondition("www"), url));
[email protected]ea6249b2012-12-06 18:45:20332 // "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]fb5bcc02012-02-17 14:05:42337
[email protected]ea6249b2012-12-06 18:45:20338 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]fb5bcc02012-02-17 14:05:42345 EXPECT_TRUE(Matches(factory.CreateQueryEqualsCondition(
346 "?sourceid=chrome-instant&ie=UTF-8&ion=1"), url));
[email protected]fb5bcc02012-02-17 14:05:42347 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));
mnisslerd9cdcd872015-05-29 13:57:41354 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]007b3f82013-04-09 08:46:45360 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]6d8e5e42012-07-17 15:54:45368
369 EXPECT_TRUE(Matches(factory.CreateHostEqualsPathPrefixCondition(
370 "www.google.com", "/webhp"), url));
mnisslerd9cdcd872015-05-29 13:57:41371 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]007b3f82013-04-09 08:46:45377 EXPECT_FALSE(Matches(
378 factory.CreateHostEqualsPathPrefixCondition(std::string(), "/webhp"),
379 url));
[email protected]6d8e5e42012-07-17 15:54:45380 EXPECT_TRUE(Matches(factory.CreateHostEqualsPathPrefixCondition(
[email protected]007b3f82013-04-09 08:46:45381 "www.google.com", std::string()),
382 url));
383 EXPECT_FALSE(Matches(
384 factory.CreateHostEqualsPathPrefixCondition("google.com", std::string()),
385 url));
[email protected]fb5bcc02012-02-17 14:05:42386}
387
388TEST(URLMatcherConditionFactoryTest, TestFullSearches) {
[email protected]c640fd7c2012-08-17 08:19:25389 // 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]fb5bcc02012-02-17 14:05:42391 "&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]007b3f82013-04-09 08:46:45395 EXPECT_TRUE(Matches(factory.CreateURLPrefixCondition(std::string()), url));
396 EXPECT_TRUE(
397 Matches(factory.CreateURLPrefixCondition("https://www.goog"), url));
[email protected]c640fd7c2012-08-17 08:19:25398 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]fb5bcc02012-02-17 14:05:42404 EXPECT_FALSE(Matches(factory.CreateURLPrefixCondition("webhp"), url));
405
[email protected]007b3f82013-04-09 08:46:45406 EXPECT_TRUE(Matches(factory.CreateURLSuffixCondition(std::string()), url));
[email protected]fb5bcc02012-02-17 14:05:42407 EXPECT_TRUE(Matches(factory.CreateURLSuffixCondition("ion=1"), url));
408 EXPECT_FALSE(Matches(factory.CreateURLSuffixCondition("www"), url));
409
[email protected]007b3f82013-04-09 08:46:45410 EXPECT_TRUE(Matches(factory.CreateURLContainsCondition(std::string()), url));
[email protected]fb5bcc02012-02-17 14:05:42411 EXPECT_TRUE(Matches(factory.CreateURLContainsCondition("www.goog"), url));
[email protected]fb5bcc02012-02-17 14:05:42412 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]c640fd7c2012-08-17 08:19:25416 EXPECT_FALSE(Matches(factory.CreateURLContainsCondition(".www.goog"), url));
[email protected]fb5bcc02012-02-17 14:05:42417 EXPECT_FALSE(Matches(factory.CreateURLContainsCondition("foobar"), url));
418 EXPECT_FALSE(Matches(factory.CreateURLContainsCondition("search"), url));
[email protected]c640fd7c2012-08-17 08:19:25419 EXPECT_FALSE(Matches(factory.CreateURLContainsCondition(":443"), url));
[email protected]fb5bcc02012-02-17 14:05:42420
421 EXPECT_TRUE(Matches(factory.CreateURLEqualsCondition(
[email protected]c640fd7c2012-08-17 08:19:25422 "https://www.google.com/webhp?sourceid=chrome-instant&ie=UTF-8&ion=1"),
423 url));
[email protected]fb5bcc02012-02-17 14:05:42424 EXPECT_FALSE(
[email protected]c640fd7c2012-08-17 08:19:25425 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]fb5bcc02012-02-17 14:05:42435}
436
[email protected]fb5bcc02012-02-17 14:05:42437//
438// URLMatcherConditionSet
439//
440
[email protected]3b001a02012-04-05 10:38:06441TEST(URLMatcherConditionSetTest, Constructor) {
[email protected]fb5bcc02012-02-17 14:05:42442 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]3b001a02012-04-05 10:38:06450 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]fb5bcc02012-02-17 14:05:42454}
455
456TEST(URLMatcherConditionSetTest, Matching) {
457 GURL url1("http://www.example.com/foo?bar=1");
458 GURL url2("http://foo.example.com/index.html");
[email protected]00520a52012-04-12 18:30:47459 GURL url3("http://www.example.com:80/foo?bar=1");
460 GURL url4("http://www.example.com:8080/foo?bar=1");
[email protected]fb5bcc02012-02-17 14:05:42461
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]3b001a02012-04-05 10:38:06470 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]fb5bcc02012-02-17 14:05:42474
[email protected]5bcf3b72012-09-14 00:20:28475 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]fb5bcc02012-02-17 14:05:42478
[email protected]5bcf3b72012-09-14 00:20:28479 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]faceb0f2012-04-12 17:07:19482
[email protected]faceb0f2012-04-12 17:07:19483 // Test scheme filters.
484 scoped_refptr<URLMatcherConditionSet> condition_set2(
dcheng3f767dc32016-04-25 22:54:22485 new URLMatcherConditionSet(1, conditions,
486 std::unique_ptr<URLMatcherSchemeFilter>(
[email protected]7e6d3f62013-06-13 17:32:14487 new URLMatcherSchemeFilter("https")),
dcheng3f767dc32016-04-25 22:54:22488 std::unique_ptr<URLMatcherPortFilter>()));
[email protected]5bcf3b72012-09-14 00:20:28489 EXPECT_FALSE(condition_set2->IsMatch(matching_patterns, url1));
[email protected]faceb0f2012-04-12 17:07:19490 scoped_refptr<URLMatcherConditionSet> condition_set3(
dcheng3f767dc32016-04-25 22:54:22491 new URLMatcherConditionSet(1, conditions,
492 std::unique_ptr<URLMatcherSchemeFilter>(
[email protected]7e6d3f62013-06-13 17:32:14493 new URLMatcherSchemeFilter("http")),
dcheng3f767dc32016-04-25 22:54:22494 std::unique_ptr<URLMatcherPortFilter>()));
[email protected]5bcf3b72012-09-14 00:20:28495 EXPECT_TRUE(condition_set3->IsMatch(matching_patterns, url1));
[email protected]00520a52012-04-12 18:30:47496
497 // Test port filters.
498 std::vector<URLMatcherPortFilter::Range> ranges;
499 ranges.push_back(URLMatcherPortFilter::CreateRange(80));
dcheng3f767dc32016-04-25 22:54:22500 std::unique_ptr<URLMatcherPortFilter> filter(
501 new URLMatcherPortFilter(ranges));
[email protected]00520a52012-04-12 18:30:47502 scoped_refptr<URLMatcherConditionSet> condition_set4(
dcheng51ace48a2015-12-26 22:45:17503 new URLMatcherConditionSet(1, conditions,
dcheng3f767dc32016-04-25 22:54:22504 std::unique_ptr<URLMatcherSchemeFilter>(),
dcheng51ace48a2015-12-26 22:45:17505 std::move(filter)));
[email protected]5bcf3b72012-09-14 00:20:28506 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]2280dc82013-04-11 20:04:01527
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]fb5bcc02012-02-17 14:05:42537}
538
[email protected]c967c002014-04-11 13:45:02539namespace {
540
541bool 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
dcheng3f767dc32016-04-25 22:54:22565 std::unique_ptr<URLMatcherSchemeFilter> scheme_filter;
566 std::unique_ptr<URLMatcherPortFilter> port_filter;
[email protected]c967c002014-04-11 13:45:02567
568 scoped_refptr<URLMatcherConditionSet> condition_set(
dcheng51ace48a2015-12-26 22:45:17569 new URLMatcherConditionSet(1, conditions, query_conditions,
570 std::move(scheme_filter),
571 std::move(port_filter)));
[email protected]c967c002014-04-11 13:45:02572
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
585TEST(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]fb5bcc02012-02-17 14:05:42814
815//
816// URLMatcher
817//
818
819TEST(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]3b001a02012-04-05 10:38:06832 URLMatcherConditionSet::Vector insert1;
kylechar1e0dd6b2017-10-03 16:07:51833 insert1.push_back(base::MakeRefCounted<URLMatcherConditionSet>(
834 kConditionSetId1, conditions1));
[email protected]fb5bcc02012-02-17 14:05:42835 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]3b001a02012-04-05 10:38:06844 URLMatcherConditionSet::Vector insert2;
kylechar1e0dd6b2017-10-03 16:07:51845 insert2.push_back(base::MakeRefCounted<URLMatcherConditionSet>(
846 kConditionSetId2, conditions2));
[email protected]fb5bcc02012-02-17 14:05:42847 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]5bcf3b72012-09-14 00:20:28853 "example.com").string_pattern()->id();
[email protected]fb5bcc02012-02-17 14:05:42854
[email protected]5bcf3b72012-09-14 00:20:28855 // 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;
kylechar1e0dd6b2017-10-03 16:07:51862 insert3.push_back(base::MakeRefCounted<URLMatcherConditionSet>(
863 kConditionSetId3, conditions3));
[email protected]5bcf3b72012-09-14 00:20:28864 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]fb5bcc02012-02-17 14:05:42876 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]357c4db2012-03-29 07:51:57889 EXPECT_TRUE(matcher.IsEmpty());
890
[email protected]fb5bcc02012-02-17 14:05:42891 // The cached singleton in matcher.condition_factory_ should be destroyed to
892 // free memory.
893 int patternId2 = factory->CreateHostSuffixCondition(
[email protected]5bcf3b72012-09-14 00:20:28894 "example.com").string_pattern()->id();
[email protected]fb5bcc02012-02-17 14:05:42895 // If patternId1 and patternId2 are different that indicates that
[email protected]5bcf3b72012-09-14 00:20:28896 // matcher.condition_factory_ does not leak memory by holding onto
897 // unused patterns.
[email protected]fb5bcc02012-02-17 14:05:42898 EXPECT_NE(patternId1, patternId2);
899}
900
[email protected]ea6249b2012-12-06 18:45:20901TEST(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;
kylechar1e0dd6b2017-10-03 16:07:51948 insert.push_back(base::MakeRefCounted<URLMatcherConditionSet>(kConditionSetId,
949 conditions));
[email protected]ea6249b2012-12-06 18:45:20950 matcher.AddConditionSets(insert);
951 EXPECT_EQ(1u, matcher.MatchURL(url).size());
952}
953
[email protected]2280dc82013-04-11 20:04:01954// Check that matches in everything but the query are found.
955TEST(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;
kylechar1e0dd6b2017-10-03 16:07:51966 insert.push_back(base::MakeRefCounted<URLMatcherConditionSet>(kConditionSetId,
967 conditions));
[email protected]2280dc82013-04-11 20:04:01968 matcher.AddConditionSets(insert);
969 EXPECT_EQ(1u, matcher.MatchURL(url).size());
970}
971
972// Check that matches in the query are ignored.
973TEST(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;
kylechar1e0dd6b2017-10-03 16:07:51984 insert.push_back(base::MakeRefCounted<URLMatcherConditionSet>(kConditionSetId,
985 conditions));
[email protected]2280dc82013-04-11 20:04:01986 matcher.AddConditionSets(insert);
987 EXPECT_EQ(0u, matcher.MatchURL(url).size());
988}
989
[email protected]716c0162013-12-13 20:36:53990} // namespace url_matcher