blob: 7219d60f43f1c8bf6fd0288becf8983ff01fc237 [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>
8
9#include "base/macros.h"
[email protected]f4390962013-06-11 07:29:2210#include "base/strings/string_util.h"
[email protected]fb5bcc02012-02-17 14:05:4211#include "testing/gtest/include/gtest/gtest.h"
[email protected]79fe2272013-07-13 20:01:4012#include "url/gurl.h"
[email protected]fb5bcc02012-02-17 14:05:4213
[email protected]716c0162013-12-13 20:36:5314namespace url_matcher {
[email protected]fb5bcc02012-02-17 14:05:4215
16//
17// URLMatcherCondition
18//
19
20TEST(URLMatcherConditionTest, Constructors) {
[email protected]5bcf3b72012-09-14 00:20:2821 StringPattern pattern("example.com", 1);
[email protected]fb5bcc02012-02-17 14:05:4222 URLMatcherCondition m1(URLMatcherCondition::HOST_SUFFIX, &pattern);
23 EXPECT_EQ(URLMatcherCondition::HOST_SUFFIX, m1.criterion());
[email protected]5bcf3b72012-09-14 00:20:2824 EXPECT_EQ(&pattern, m1.string_pattern());
[email protected]fb5bcc02012-02-17 14:05:4225
26 URLMatcherCondition m2;
27 m2 = m1;
28 EXPECT_EQ(URLMatcherCondition::HOST_SUFFIX, m2.criterion());
[email protected]5bcf3b72012-09-14 00:20:2829 EXPECT_EQ(&pattern, m2.string_pattern());
[email protected]fb5bcc02012-02-17 14:05:4230
31 URLMatcherCondition m3(m1);
32 EXPECT_EQ(URLMatcherCondition::HOST_SUFFIX, m3.criterion());
[email protected]5bcf3b72012-09-14 00:20:2833 EXPECT_EQ(&pattern, m3.string_pattern());
[email protected]fb5bcc02012-02-17 14:05:4234}
35
[email protected]faceb0f2012-04-12 17:07:1936TEST(URLMatcherSchemeFilter, TestMatching) {
37 URLMatcherSchemeFilter filter1("https");
38 std::vector<std::string> filter2_content;
39 filter2_content.push_back("http");
40 filter2_content.push_back("https");
41 URLMatcherSchemeFilter filter2(filter2_content);
42
43 GURL matching_url("https://www.foobar.com");
44 GURL non_matching_url("http://www.foobar.com");
45 EXPECT_TRUE(filter1.IsMatch(matching_url));
46 EXPECT_FALSE(filter1.IsMatch(non_matching_url));
47 EXPECT_TRUE(filter2.IsMatch(matching_url));
48 EXPECT_TRUE(filter2.IsMatch(non_matching_url));
49}
50
[email protected]00520a52012-04-12 18:30:4751TEST(URLMatcherPortFilter, TestMatching) {
52 std::vector<URLMatcherPortFilter::Range> ranges;
53 ranges.push_back(URLMatcherPortFilter::CreateRange(80, 90));
54 ranges.push_back(URLMatcherPortFilter::CreateRange(8080));
55 URLMatcherPortFilter filter(ranges);
56 EXPECT_TRUE(filter.IsMatch(GURL("http://www.example.com")));
57 EXPECT_TRUE(filter.IsMatch(GURL("http://www.example.com:80")));
58 EXPECT_TRUE(filter.IsMatch(GURL("http://www.example.com:81")));
59 EXPECT_TRUE(filter.IsMatch(GURL("http://www.example.com:90")));
60 EXPECT_TRUE(filter.IsMatch(GURL("http://www.example.com:8080")));
61 EXPECT_FALSE(filter.IsMatch(GURL("http://www.example.com:79")));
62 EXPECT_FALSE(filter.IsMatch(GURL("http://www.example.com:91")));
63 EXPECT_FALSE(filter.IsMatch(GURL("https://www.example.com")));
64}
65
[email protected]fb5bcc02012-02-17 14:05:4266TEST(URLMatcherConditionTest, IsFullURLCondition) {
[email protected]5bcf3b72012-09-14 00:20:2867 StringPattern pattern("example.com", 1);
[email protected]fb5bcc02012-02-17 14:05:4268 EXPECT_FALSE(URLMatcherCondition(URLMatcherCondition::HOST_SUFFIX,
69 &pattern).IsFullURLCondition());
70
71 EXPECT_TRUE(URLMatcherCondition(URLMatcherCondition::HOST_CONTAINS,
72 &pattern).IsFullURLCondition());
73 EXPECT_TRUE(URLMatcherCondition(URLMatcherCondition::PATH_CONTAINS,
74 &pattern).IsFullURLCondition());
75 EXPECT_TRUE(URLMatcherCondition(URLMatcherCondition::QUERY_CONTAINS,
76 &pattern).IsFullURLCondition());
77
78 EXPECT_TRUE(URLMatcherCondition(URLMatcherCondition::URL_PREFIX,
79 &pattern).IsFullURLCondition());
80 EXPECT_TRUE(URLMatcherCondition(URLMatcherCondition::URL_SUFFIX,
81 &pattern).IsFullURLCondition());
82 EXPECT_TRUE(URLMatcherCondition(URLMatcherCondition::URL_CONTAINS,
83 &pattern).IsFullURLCondition());
84 EXPECT_TRUE(URLMatcherCondition(URLMatcherCondition::URL_EQUALS,
85 &pattern).IsFullURLCondition());
86}
87
88TEST(URLMatcherConditionTest, IsMatch) {
89 GURL url1("http://www.example.com/www.foobar.com/index.html");
90 GURL url2("http://www.foobar.com/example.com/index.html");
91
[email protected]5bcf3b72012-09-14 00:20:2892 StringPattern pattern("example.com", 1);
[email protected]fb5bcc02012-02-17 14:05:4293 URLMatcherCondition m1(URLMatcherCondition::HOST_SUFFIX, &pattern);
94
[email protected]5bcf3b72012-09-14 00:20:2895 std::set<StringPattern::ID> matching_patterns;
[email protected]fb5bcc02012-02-17 14:05:4296
97 // matches = {0} --> matcher did not indicate that m1 was a match.
[email protected]5bcf3b72012-09-14 00:20:2898 matching_patterns.insert(0);
99 EXPECT_FALSE(m1.IsMatch(matching_patterns, url1));
[email protected]fb5bcc02012-02-17 14:05:42100
101 // matches = {0, 1} --> matcher did indicate that m1 was a match.
[email protected]5bcf3b72012-09-14 00:20:28102 matching_patterns.insert(1);
103 EXPECT_TRUE(m1.IsMatch(matching_patterns, url1));
[email protected]fb5bcc02012-02-17 14:05:42104
105 // For m2 we use a HOST_CONTAINS test, which requires a post-validation
106 // whether the match reported by the SubstringSetMatcher occurs really
107 // in the correct url component.
108 URLMatcherCondition m2(URLMatcherCondition::HOST_CONTAINS, &pattern);
[email protected]5bcf3b72012-09-14 00:20:28109 EXPECT_TRUE(m2.IsMatch(matching_patterns, url1));
110 EXPECT_FALSE(m2.IsMatch(matching_patterns, url2));
[email protected]fb5bcc02012-02-17 14:05:42111}
112
113TEST(URLMatcherConditionTest, Comparison) {
[email protected]5bcf3b72012-09-14 00:20:28114 StringPattern p1("foobar.com", 1);
115 StringPattern p2("foobar.com", 2);
[email protected]fb5bcc02012-02-17 14:05:42116 // The first component of each test is expected to be < than the second.
117 URLMatcherCondition test_smaller[][2] = {
118 {URLMatcherCondition(URLMatcherCondition::HOST_PREFIX, &p1),
119 URLMatcherCondition(URLMatcherCondition::HOST_SUFFIX, &p1)},
120 {URLMatcherCondition(URLMatcherCondition::HOST_PREFIX, &p1),
121 URLMatcherCondition(URLMatcherCondition::HOST_PREFIX, &p2)},
122 {URLMatcherCondition(URLMatcherCondition::HOST_PREFIX, NULL),
123 URLMatcherCondition(URLMatcherCondition::HOST_PREFIX, &p2)},
124 {URLMatcherCondition(URLMatcherCondition::HOST_PREFIX, &p1),
125 URLMatcherCondition(URLMatcherCondition::HOST_SUFFIX, NULL)},
126 };
127 for (size_t i = 0; i < arraysize(test_smaller); ++i) {
128 EXPECT_TRUE(test_smaller[i][0] < test_smaller[i][1])
129 << "Test " << i << " of test_smaller failed";
130 EXPECT_FALSE(test_smaller[i][1] < test_smaller[i][0])
131 << "Test " << i << " of test_smaller failed";
132 }
133 URLMatcherCondition test_equal[][2] = {
134 {URLMatcherCondition(URLMatcherCondition::HOST_PREFIX, &p1),
135 URLMatcherCondition(URLMatcherCondition::HOST_PREFIX, &p1)},
136 {URLMatcherCondition(URLMatcherCondition::HOST_PREFIX, NULL),
137 URLMatcherCondition(URLMatcherCondition::HOST_PREFIX, NULL)},
138 };
139 for (size_t i = 0; i < arraysize(test_equal); ++i) {
140 EXPECT_FALSE(test_equal[i][0] < test_equal[i][1])
141 << "Test " << i << " of test_equal failed";
142 EXPECT_FALSE(test_equal[i][1] < test_equal[i][0])
143 << "Test " << i << " of test_equal failed";
144 }
145}
146
147//
148// URLMatcherConditionFactory
149//
150
151namespace {
152
ki.stfu939799a42015-09-28 04:41:20153bool Matches(const URLMatcherCondition& condition, const std::string& text) {
[email protected]5bcf3b72012-09-14 00:20:28154 return text.find(condition.string_pattern()->pattern()) !=
[email protected]fb5bcc02012-02-17 14:05:42155 std::string::npos;
156}
157
158} // namespace
159
160TEST(URLMatcherConditionFactoryTest, GURLCharacterSet) {
161 // GURL guarantees that neither domain, nor path, nor query may contain
162 // non ASCII-7 characters. We test this here, because a change to this
163 // guarantee breaks this implementation horribly.
164 GURL url("http://www.föö.com/föö?föö#föö");
[email protected]527965412014-05-07 14:38:26165 EXPECT_TRUE(base::IsStringASCII(url.host()));
166 EXPECT_TRUE(base::IsStringASCII(url.path()));
167 EXPECT_TRUE(base::IsStringASCII(url.query()));
168 EXPECT_FALSE(base::IsStringASCII(url.ref()));
[email protected]fb5bcc02012-02-17 14:05:42169}
170
[email protected]6d8e5e42012-07-17 15:54:45171TEST(URLMatcherConditionFactoryTest, Criteria) {
172 URLMatcherConditionFactory factory;
173 EXPECT_EQ(URLMatcherCondition::HOST_PREFIX,
174 factory.CreateHostPrefixCondition("foo").criterion());
175 EXPECT_EQ(URLMatcherCondition::HOST_SUFFIX,
176 factory.CreateHostSuffixCondition("foo").criterion());
177 EXPECT_EQ(URLMatcherCondition::HOST_CONTAINS,
178 factory.CreateHostContainsCondition("foo").criterion());
179 EXPECT_EQ(URLMatcherCondition::HOST_EQUALS,
180 factory.CreateHostEqualsCondition("foo").criterion());
181 EXPECT_EQ(URLMatcherCondition::PATH_PREFIX,
182 factory.CreatePathPrefixCondition("foo").criterion());
183 EXPECT_EQ(URLMatcherCondition::PATH_SUFFIX,
184 factory.CreatePathSuffixCondition("foo").criterion());
185 EXPECT_EQ(URLMatcherCondition::PATH_CONTAINS,
186 factory.CreatePathContainsCondition("foo").criterion());
187 EXPECT_EQ(URLMatcherCondition::PATH_EQUALS,
188 factory.CreatePathEqualsCondition("foo").criterion());
189 EXPECT_EQ(URLMatcherCondition::QUERY_PREFIX,
190 factory.CreateQueryPrefixCondition("foo").criterion());
191 EXPECT_EQ(URLMatcherCondition::QUERY_SUFFIX,
192 factory.CreateQuerySuffixCondition("foo").criterion());
193 EXPECT_EQ(URLMatcherCondition::QUERY_CONTAINS,
194 factory.CreateQueryContainsCondition("foo").criterion());
195 EXPECT_EQ(URLMatcherCondition::QUERY_EQUALS,
196 factory.CreateQueryEqualsCondition("foo").criterion());
197 EXPECT_EQ(URLMatcherCondition::HOST_SUFFIX_PATH_PREFIX,
198 factory.CreateHostSuffixPathPrefixCondition("foo",
199 "bar").criterion());
200 EXPECT_EQ(URLMatcherCondition::HOST_EQUALS_PATH_PREFIX,
201 factory.CreateHostEqualsPathPrefixCondition("foo",
202 "bar").criterion());
203 EXPECT_EQ(URLMatcherCondition::URL_PREFIX,
204 factory.CreateURLPrefixCondition("foo").criterion());
205 EXPECT_EQ(URLMatcherCondition::URL_SUFFIX,
206 factory.CreateURLSuffixCondition("foo").criterion());
207 EXPECT_EQ(URLMatcherCondition::URL_CONTAINS,
208 factory.CreateURLContainsCondition("foo").criterion());
209 EXPECT_EQ(URLMatcherCondition::URL_EQUALS,
210 factory.CreateURLEqualsCondition("foo").criterion());
[email protected]5bcf3b72012-09-14 00:20:28211 EXPECT_EQ(URLMatcherCondition::URL_MATCHES,
212 factory.CreateURLMatchesCondition("foo").criterion());
[email protected]6d8e5e42012-07-17 15:54:45213}
214
[email protected]fb5bcc02012-02-17 14:05:42215TEST(URLMatcherConditionFactoryTest, TestSingletonProperty) {
216 URLMatcherConditionFactory factory;
217 URLMatcherCondition c1 = factory.CreateHostEqualsCondition("www.google.com");
218 URLMatcherCondition c2 = factory.CreateHostEqualsCondition("www.google.com");
219 EXPECT_EQ(c1.criterion(), c2.criterion());
[email protected]5bcf3b72012-09-14 00:20:28220 EXPECT_EQ(c1.string_pattern(), c2.string_pattern());
[email protected]fb5bcc02012-02-17 14:05:42221 URLMatcherCondition c3 = factory.CreateHostEqualsCondition("www.google.de");
222 EXPECT_EQ(c2.criterion(), c3.criterion());
[email protected]5bcf3b72012-09-14 00:20:28223 EXPECT_NE(c2.string_pattern(), c3.string_pattern());
224 EXPECT_NE(c2.string_pattern()->id(), c3.string_pattern()->id());
225 EXPECT_NE(c2.string_pattern()->pattern(),
226 c3.string_pattern()->pattern());
227 URLMatcherCondition c4 = factory.CreateURLMatchesCondition("www.google.com");
228 URLMatcherCondition c5 = factory.CreateURLContainsCondition("www.google.com");
229 // Regex patterns and substring patterns do not share IDs.
230 EXPECT_EQ(c5.string_pattern()->pattern(), c4.string_pattern()->pattern());
231 EXPECT_NE(c5.string_pattern(), c4.string_pattern());
232 EXPECT_NE(c5.string_pattern()->id(), c4.string_pattern()->id());
[email protected]fb5bcc02012-02-17 14:05:42233
[email protected]5bcf3b72012-09-14 00:20:28234 // Check that all StringPattern singletons are freed if we call
[email protected]fb5bcc02012-02-17 14:05:42235 // ForgetUnusedPatterns.
[email protected]5bcf3b72012-09-14 00:20:28236 StringPattern::ID old_id_1 = c1.string_pattern()->id();
237 StringPattern::ID old_id_4 = c4.string_pattern()->id();
238 factory.ForgetUnusedPatterns(std::set<StringPattern::ID>());
[email protected]357c4db2012-03-29 07:51:57239 EXPECT_TRUE(factory.IsEmpty());
[email protected]5bcf3b72012-09-14 00:20:28240 URLMatcherCondition c6 = factory.CreateHostEqualsCondition("www.google.com");
241 EXPECT_NE(old_id_1, c6.string_pattern()->id());
242 URLMatcherCondition c7 = factory.CreateURLMatchesCondition("www.google.com");
243 EXPECT_NE(old_id_4, c7.string_pattern()->id());
[email protected]fb5bcc02012-02-17 14:05:42244}
245
246TEST(URLMatcherConditionFactoryTest, TestComponentSearches) {
mnisslerd9cdcd872015-05-29 13:57:41247 URLMatcherConditionFactory factory;
[email protected]c640fd7c2012-08-17 08:19:25248 GURL gurl("https://www.google.com:1234/webhp?sourceid=chrome-instant&ie=UTF-8"
[email protected]fb5bcc02012-02-17 14:05:42249 "&ion=1#hl=en&output=search&sclient=psy-ab&q=chrome%20is%20awesome");
[email protected]fb5bcc02012-02-17 14:05:42250 std::string url = factory.CanonicalizeURLForComponentSearches(gurl);
mnisslerd9cdcd872015-05-29 13:57:41251 GURL gurl2("https://www.google.com.:1234/webhp?sourceid=chrome-instant"
252 "&ie=UTF-8&ion=1#hl=en&output=search&sclient=psy-ab"
253 "&q=chrome%20is%20awesome");
254 std::string url2 = factory.CanonicalizeURLForComponentSearches(gurl2);
[email protected]fb5bcc02012-02-17 14:05:42255
256 // Test host component.
[email protected]007b3f82013-04-09 08:46:45257 EXPECT_TRUE(Matches(factory.CreateHostPrefixCondition(std::string()), url));
[email protected]fb5bcc02012-02-17 14:05:42258 EXPECT_TRUE(Matches(factory.CreateHostPrefixCondition("www.goog"), url));
259 EXPECT_TRUE(
260 Matches(factory.CreateHostPrefixCondition("www.google.com"), url));
261 EXPECT_TRUE(
262 Matches(factory.CreateHostPrefixCondition(".www.google.com"), url));
263 EXPECT_FALSE(Matches(factory.CreateHostPrefixCondition("google.com"), url));
264 EXPECT_FALSE(
265 Matches(factory.CreateHostPrefixCondition("www.google.com/"), url));
266 EXPECT_FALSE(Matches(factory.CreateHostPrefixCondition("webhp"), url));
267
[email protected]007b3f82013-04-09 08:46:45268 EXPECT_TRUE(Matches(factory.CreateHostSuffixCondition(std::string()), url));
mnisslerd9cdcd872015-05-29 13:57:41269 EXPECT_TRUE(Matches(factory.CreateHostSuffixCondition(std::string()), url2));
[email protected]fb5bcc02012-02-17 14:05:42270 EXPECT_TRUE(Matches(factory.CreateHostSuffixCondition("com"), url));
mnisslerd9cdcd872015-05-29 13:57:41271 EXPECT_TRUE(Matches(factory.CreateHostSuffixCondition("com"), url2));
[email protected]fb5bcc02012-02-17 14:05:42272 EXPECT_TRUE(Matches(factory.CreateHostSuffixCondition(".com"), url));
273 EXPECT_TRUE(
274 Matches(factory.CreateHostSuffixCondition("www.google.com"), url));
275 EXPECT_TRUE(
276 Matches(factory.CreateHostSuffixCondition(".www.google.com"), url));
mnisslerd9cdcd872015-05-29 13:57:41277 EXPECT_TRUE(
278 Matches(factory.CreateHostSuffixCondition(".www.google.com"), url2));
279 EXPECT_TRUE(
280 Matches(factory.CreateHostSuffixCondition(".www.google.com."), url));
[email protected]fb5bcc02012-02-17 14:05:42281 EXPECT_FALSE(Matches(factory.CreateHostSuffixCondition("www"), url));
282 EXPECT_FALSE(
283 Matches(factory.CreateHostSuffixCondition("www.google.com/"), url));
284 EXPECT_FALSE(Matches(factory.CreateHostSuffixCondition("webhp"), url));
285
[email protected]007b3f82013-04-09 08:46:45286 EXPECT_FALSE(Matches(factory.CreateHostEqualsCondition(std::string()), url));
[email protected]fb5bcc02012-02-17 14:05:42287 EXPECT_FALSE(Matches(factory.CreateHostEqualsCondition("www"), url));
288 EXPECT_TRUE(
289 Matches(factory.CreateHostEqualsCondition("www.google.com"), url));
mnisslerd9cdcd872015-05-29 13:57:41290 EXPECT_TRUE(
291 Matches(factory.CreateHostEqualsCondition("www.google.com"), url2));
[email protected]fb5bcc02012-02-17 14:05:42292 EXPECT_FALSE(
293 Matches(factory.CreateHostEqualsCondition("www.google.com/"), url));
mnisslerd9cdcd872015-05-29 13:57:41294 EXPECT_TRUE(
295 Matches(factory.CreateHostEqualsCondition(".www.google.com."), url));
296 EXPECT_TRUE(
297 Matches(factory.CreateHostEqualsCondition(".www.google.com."), url2));
[email protected]fb5bcc02012-02-17 14:05:42298
299 // Test path component.
[email protected]007b3f82013-04-09 08:46:45300 EXPECT_TRUE(Matches(factory.CreatePathPrefixCondition(std::string()), url));
[email protected]fb5bcc02012-02-17 14:05:42301 EXPECT_TRUE(Matches(factory.CreatePathPrefixCondition("/web"), url));
302 EXPECT_TRUE(Matches(factory.CreatePathPrefixCondition("/webhp"), url));
303 EXPECT_FALSE(Matches(factory.CreatePathPrefixCondition("webhp"), url));
304 EXPECT_FALSE(Matches(factory.CreatePathPrefixCondition("/webhp?"), url));
[email protected]ea6249b2012-12-06 18:45:20305 EXPECT_FALSE(Matches(factory.CreatePathPrefixCondition("?sourceid"), url));
[email protected]fb5bcc02012-02-17 14:05:42306
[email protected]007b3f82013-04-09 08:46:45307 EXPECT_TRUE(Matches(factory.CreatePathSuffixCondition(std::string()), url));
[email protected]fb5bcc02012-02-17 14:05:42308 EXPECT_TRUE(Matches(factory.CreatePathSuffixCondition("webhp"), url));
309 EXPECT_TRUE(Matches(factory.CreatePathSuffixCondition("/webhp"), url));
310 EXPECT_FALSE(Matches(factory.CreatePathSuffixCondition("/web"), url));
311 EXPECT_FALSE(Matches(factory.CreatePathSuffixCondition("/webhp?"), url));
312
313 EXPECT_TRUE(Matches(factory.CreatePathEqualsCondition("/webhp"), url));
314 EXPECT_FALSE(Matches(factory.CreatePathEqualsCondition("webhp"), url));
315 EXPECT_FALSE(Matches(factory.CreatePathEqualsCondition("/webhp?"), url));
316 EXPECT_FALSE(
317 Matches(factory.CreatePathEqualsCondition("www.google.com"), url));
318
319
320 // Test query component.
[email protected]007b3f82013-04-09 08:46:45321 EXPECT_TRUE(Matches(factory.CreateQueryPrefixCondition(std::string()), url));
[email protected]ea6249b2012-12-06 18:45:20322 EXPECT_TRUE(Matches(factory.CreateQueryPrefixCondition("sourceid"), url));
323 // The '?' at the beginning is just ignored.
[email protected]fb5bcc02012-02-17 14:05:42324 EXPECT_TRUE(Matches(factory.CreateQueryPrefixCondition("?sourceid"), url));
[email protected]fb5bcc02012-02-17 14:05:42325
[email protected]007b3f82013-04-09 08:46:45326 EXPECT_TRUE(Matches(factory.CreateQuerySuffixCondition(std::string()), url));
[email protected]fb5bcc02012-02-17 14:05:42327 EXPECT_TRUE(Matches(factory.CreateQuerySuffixCondition("ion=1"), url));
[email protected]fb5bcc02012-02-17 14:05:42328 EXPECT_FALSE(Matches(factory.CreateQuerySuffixCondition("www"), url));
[email protected]ea6249b2012-12-06 18:45:20329 // "Suffix" condition + pattern starting with '?' = "equals" condition.
330 EXPECT_FALSE(Matches(factory.CreateQuerySuffixCondition(
331 "?sourceid=chrome-instant&ie=UTF-8&ion="), url));
332 EXPECT_TRUE(Matches(factory.CreateQuerySuffixCondition(
333 "?sourceid=chrome-instant&ie=UTF-8&ion=1"), url));
[email protected]fb5bcc02012-02-17 14:05:42334
[email protected]ea6249b2012-12-06 18:45:20335 EXPECT_FALSE(Matches(factory.CreateQueryEqualsCondition(
336 "?sourceid=chrome-instant&ie=UTF-8&ion="), url));
337 EXPECT_FALSE(Matches(factory.CreateQueryEqualsCondition(
338 "sourceid=chrome-instant&ie=UTF-8&ion="), url));
339 EXPECT_TRUE(Matches(factory.CreateQueryEqualsCondition(
340 "sourceid=chrome-instant&ie=UTF-8&ion=1"), url));
341 // The '?' at the beginning is just ignored.
[email protected]fb5bcc02012-02-17 14:05:42342 EXPECT_TRUE(Matches(factory.CreateQueryEqualsCondition(
343 "?sourceid=chrome-instant&ie=UTF-8&ion=1"), url));
[email protected]fb5bcc02012-02-17 14:05:42344 EXPECT_FALSE(
345 Matches(factory.CreateQueryEqualsCondition("www.google.com"), url));
346
347
348 // Test adjacent components
349 EXPECT_TRUE(Matches(factory.CreateHostSuffixPathPrefixCondition(
350 "google.com", "/webhp"), url));
mnisslerd9cdcd872015-05-29 13:57:41351 EXPECT_TRUE(Matches(factory.CreateHostSuffixPathPrefixCondition(
352 "google.com", "/webhp"), url2));
353 EXPECT_TRUE(Matches(factory.CreateHostSuffixPathPrefixCondition(
354 "google.com.", "/webhp"), url));
355 EXPECT_TRUE(Matches(factory.CreateHostSuffixPathPrefixCondition(
356 "google.com.", "/webhp"), url2));
[email protected]007b3f82013-04-09 08:46:45357 EXPECT_TRUE(Matches(
358 factory.CreateHostSuffixPathPrefixCondition(std::string(), "/webhp"),
359 url));
360 EXPECT_TRUE(Matches(
361 factory.CreateHostSuffixPathPrefixCondition("google.com", std::string()),
362 url));
363 EXPECT_FALSE(Matches(
364 factory.CreateHostSuffixPathPrefixCondition("www", std::string()), url));
[email protected]6d8e5e42012-07-17 15:54:45365
366 EXPECT_TRUE(Matches(factory.CreateHostEqualsPathPrefixCondition(
367 "www.google.com", "/webhp"), url));
mnisslerd9cdcd872015-05-29 13:57:41368 EXPECT_TRUE(Matches(factory.CreateHostEqualsPathPrefixCondition(
369 "www.google.com", "/webhp"), url2));
370 EXPECT_TRUE(Matches(factory.CreateHostEqualsPathPrefixCondition(
371 ".www.google.com.", "/webhp"), url));
372 EXPECT_TRUE(Matches(factory.CreateHostEqualsPathPrefixCondition(
373 ".www.google.com.", "/webhp"), url2));
[email protected]007b3f82013-04-09 08:46:45374 EXPECT_FALSE(Matches(
375 factory.CreateHostEqualsPathPrefixCondition(std::string(), "/webhp"),
376 url));
[email protected]6d8e5e42012-07-17 15:54:45377 EXPECT_TRUE(Matches(factory.CreateHostEqualsPathPrefixCondition(
[email protected]007b3f82013-04-09 08:46:45378 "www.google.com", std::string()),
379 url));
380 EXPECT_FALSE(Matches(
381 factory.CreateHostEqualsPathPrefixCondition("google.com", std::string()),
382 url));
[email protected]fb5bcc02012-02-17 14:05:42383}
384
385TEST(URLMatcherConditionFactoryTest, TestFullSearches) {
[email protected]c640fd7c2012-08-17 08:19:25386 // The Port 443 is stripped because it is the default port for https.
387 GURL gurl("https://www.google.com:443/webhp?sourceid=chrome-instant&ie=UTF-8"
[email protected]fb5bcc02012-02-17 14:05:42388 "&ion=1#hl=en&output=search&sclient=psy-ab&q=chrome%20is%20awesome");
389 URLMatcherConditionFactory factory;
390 std::string url = factory.CanonicalizeURLForFullSearches(gurl);
391
[email protected]007b3f82013-04-09 08:46:45392 EXPECT_TRUE(Matches(factory.CreateURLPrefixCondition(std::string()), url));
393 EXPECT_TRUE(
394 Matches(factory.CreateURLPrefixCondition("https://www.goog"), url));
[email protected]c640fd7c2012-08-17 08:19:25395 EXPECT_TRUE(Matches(factory.CreateURLPrefixCondition(
396 "https://www.google.com"), url));
397 EXPECT_TRUE(Matches(factory.CreateURLPrefixCondition(
398 "https://www.google.com/webhp?"), url));
399 EXPECT_FALSE(Matches(factory.CreateURLPrefixCondition(
400 "http://www.google.com"), url));
[email protected]fb5bcc02012-02-17 14:05:42401 EXPECT_FALSE(Matches(factory.CreateURLPrefixCondition("webhp"), url));
402
[email protected]007b3f82013-04-09 08:46:45403 EXPECT_TRUE(Matches(factory.CreateURLSuffixCondition(std::string()), url));
[email protected]fb5bcc02012-02-17 14:05:42404 EXPECT_TRUE(Matches(factory.CreateURLSuffixCondition("ion=1"), url));
405 EXPECT_FALSE(Matches(factory.CreateURLSuffixCondition("www"), url));
406
[email protected]007b3f82013-04-09 08:46:45407 EXPECT_TRUE(Matches(factory.CreateURLContainsCondition(std::string()), url));
[email protected]fb5bcc02012-02-17 14:05:42408 EXPECT_TRUE(Matches(factory.CreateURLContainsCondition("www.goog"), url));
[email protected]fb5bcc02012-02-17 14:05:42409 EXPECT_TRUE(Matches(factory.CreateURLContainsCondition("webhp"), url));
410 EXPECT_TRUE(Matches(factory.CreateURLContainsCondition("?"), url));
411 EXPECT_TRUE(Matches(factory.CreateURLContainsCondition("sourceid"), url));
412 EXPECT_TRUE(Matches(factory.CreateURLContainsCondition("ion=1"), url));
[email protected]c640fd7c2012-08-17 08:19:25413 EXPECT_FALSE(Matches(factory.CreateURLContainsCondition(".www.goog"), url));
[email protected]fb5bcc02012-02-17 14:05:42414 EXPECT_FALSE(Matches(factory.CreateURLContainsCondition("foobar"), url));
415 EXPECT_FALSE(Matches(factory.CreateURLContainsCondition("search"), url));
[email protected]c640fd7c2012-08-17 08:19:25416 EXPECT_FALSE(Matches(factory.CreateURLContainsCondition(":443"), url));
[email protected]fb5bcc02012-02-17 14:05:42417
418 EXPECT_TRUE(Matches(factory.CreateURLEqualsCondition(
[email protected]c640fd7c2012-08-17 08:19:25419 "https://www.google.com/webhp?sourceid=chrome-instant&ie=UTF-8&ion=1"),
420 url));
[email protected]fb5bcc02012-02-17 14:05:42421 EXPECT_FALSE(
[email protected]c640fd7c2012-08-17 08:19:25422 Matches(factory.CreateURLEqualsCondition("https://www.google.com"), url));
423
424 // Same as above but this time with a non-standard port.
425 gurl = GURL("https://www.google.com:1234/webhp?sourceid=chrome-instant&"
426 "ie=UTF-8&ion=1#hl=en&output=search&sclient=psy-ab&q=chrome%20is%20"
427 "awesome");
428 url = factory.CanonicalizeURLForFullSearches(gurl);
429 EXPECT_TRUE(Matches(factory.CreateURLPrefixCondition(
430 "https://www.google.com:1234/webhp?"), url));
431 EXPECT_TRUE(Matches(factory.CreateURLContainsCondition(":1234"), url));
[email protected]fb5bcc02012-02-17 14:05:42432}
433
[email protected]fb5bcc02012-02-17 14:05:42434//
435// URLMatcherConditionSet
436//
437
[email protected]3b001a02012-04-05 10:38:06438TEST(URLMatcherConditionSetTest, Constructor) {
[email protected]fb5bcc02012-02-17 14:05:42439 URLMatcherConditionFactory factory;
440 URLMatcherCondition m1 = factory.CreateHostSuffixCondition("example.com");
441 URLMatcherCondition m2 = factory.CreatePathContainsCondition("foo");
442
443 std::set<URLMatcherCondition> conditions;
444 conditions.insert(m1);
445 conditions.insert(m2);
446
[email protected]3b001a02012-04-05 10:38:06447 scoped_refptr<URLMatcherConditionSet> condition_set(
448 new URLMatcherConditionSet(1, conditions));
449 EXPECT_EQ(1, condition_set->id());
450 EXPECT_EQ(2u, condition_set->conditions().size());
[email protected]fb5bcc02012-02-17 14:05:42451}
452
453TEST(URLMatcherConditionSetTest, Matching) {
454 GURL url1("http://www.example.com/foo?bar=1");
455 GURL url2("http://foo.example.com/index.html");
[email protected]00520a52012-04-12 18:30:47456 GURL url3("http://www.example.com:80/foo?bar=1");
457 GURL url4("http://www.example.com:8080/foo?bar=1");
[email protected]fb5bcc02012-02-17 14:05:42458
459 URLMatcherConditionFactory factory;
460 URLMatcherCondition m1 = factory.CreateHostSuffixCondition("example.com");
461 URLMatcherCondition m2 = factory.CreatePathContainsCondition("foo");
462
463 std::set<URLMatcherCondition> conditions;
464 conditions.insert(m1);
465 conditions.insert(m2);
466
[email protected]3b001a02012-04-05 10:38:06467 scoped_refptr<URLMatcherConditionSet> condition_set(
468 new URLMatcherConditionSet(1, conditions));
469 EXPECT_EQ(1, condition_set->id());
470 EXPECT_EQ(2u, condition_set->conditions().size());
[email protected]fb5bcc02012-02-17 14:05:42471
[email protected]5bcf3b72012-09-14 00:20:28472 std::set<StringPattern::ID> matching_patterns;
473 matching_patterns.insert(m1.string_pattern()->id());
474 EXPECT_FALSE(condition_set->IsMatch(matching_patterns, url1));
[email protected]fb5bcc02012-02-17 14:05:42475
[email protected]5bcf3b72012-09-14 00:20:28476 matching_patterns.insert(m2.string_pattern()->id());
477 EXPECT_TRUE(condition_set->IsMatch(matching_patterns, url1));
478 EXPECT_FALSE(condition_set->IsMatch(matching_patterns, url2));
[email protected]faceb0f2012-04-12 17:07:19479
[email protected]faceb0f2012-04-12 17:07:19480 // Test scheme filters.
481 scoped_refptr<URLMatcherConditionSet> condition_set2(
[email protected]7e6d3f62013-06-13 17:32:14482 new URLMatcherConditionSet(1,
483 conditions,
484 scoped_ptr<URLMatcherSchemeFilter>(
485 new URLMatcherSchemeFilter("https")),
486 scoped_ptr<URLMatcherPortFilter>()));
[email protected]5bcf3b72012-09-14 00:20:28487 EXPECT_FALSE(condition_set2->IsMatch(matching_patterns, url1));
[email protected]faceb0f2012-04-12 17:07:19488 scoped_refptr<URLMatcherConditionSet> condition_set3(
[email protected]7e6d3f62013-06-13 17:32:14489 new URLMatcherConditionSet(1,
490 conditions,
491 scoped_ptr<URLMatcherSchemeFilter>(
492 new URLMatcherSchemeFilter("http")),
493 scoped_ptr<URLMatcherPortFilter>()));
[email protected]5bcf3b72012-09-14 00:20:28494 EXPECT_TRUE(condition_set3->IsMatch(matching_patterns, url1));
[email protected]00520a52012-04-12 18:30:47495
496 // Test port filters.
497 std::vector<URLMatcherPortFilter::Range> ranges;
498 ranges.push_back(URLMatcherPortFilter::CreateRange(80));
499 scoped_ptr<URLMatcherPortFilter> filter(new URLMatcherPortFilter(ranges));
500 scoped_refptr<URLMatcherConditionSet> condition_set4(
[email protected]7e6d3f62013-06-13 17:32:14501 new URLMatcherConditionSet(
502 1, conditions, scoped_ptr<URLMatcherSchemeFilter>(), filter.Pass()));
[email protected]5bcf3b72012-09-14 00:20:28503 EXPECT_TRUE(condition_set4->IsMatch(matching_patterns, url1));
504 EXPECT_TRUE(condition_set4->IsMatch(matching_patterns, url3));
505 EXPECT_FALSE(condition_set4->IsMatch(matching_patterns, url4));
506
507 // Test regex patterns.
508 matching_patterns.clear();
509 URLMatcherCondition r1 = factory.CreateURLMatchesCondition("/fo?oo");
510 std::set<URLMatcherCondition> regex_conditions;
511 regex_conditions.insert(r1);
512 scoped_refptr<URLMatcherConditionSet> condition_set5(
513 new URLMatcherConditionSet(1, regex_conditions));
514 EXPECT_FALSE(condition_set5->IsMatch(matching_patterns, url1));
515 matching_patterns.insert(r1.string_pattern()->id());
516 EXPECT_TRUE(condition_set5->IsMatch(matching_patterns, url1));
517
518 regex_conditions.insert(m1);
519 scoped_refptr<URLMatcherConditionSet> condition_set6(
520 new URLMatcherConditionSet(1, regex_conditions));
521 EXPECT_FALSE(condition_set6->IsMatch(matching_patterns, url1));
522 matching_patterns.insert(m1.string_pattern()->id());
523 EXPECT_TRUE(condition_set6->IsMatch(matching_patterns, url1));
[email protected]2280dc82013-04-11 20:04:01524
525 matching_patterns.clear();
526 regex_conditions.clear();
527 URLMatcherCondition r2 = factory.CreateOriginAndPathMatchesCondition("b[a]r");
528 regex_conditions.insert(r2);
529 scoped_refptr<URLMatcherConditionSet> condition_set7(
530 new URLMatcherConditionSet(1, regex_conditions));
531 EXPECT_FALSE(condition_set7->IsMatch(matching_patterns, url1));
532 matching_patterns.insert(r2.string_pattern()->id());
533 EXPECT_TRUE(condition_set7->IsMatch(matching_patterns, url1));
[email protected]fb5bcc02012-02-17 14:05:42534}
535
[email protected]c967c002014-04-11 13:45:02536namespace {
537
538bool IsQueryMatch(
539 const std::string& url_query,
540 const std::string& key,
541 URLQueryElementMatcherCondition::QueryElementType query_element_type,
542 const std::string& value,
543 URLQueryElementMatcherCondition::QueryValueMatchType query_value_match_type,
544 URLQueryElementMatcherCondition::Type match_type) {
545 URLMatcherConditionFactory factory;
546
547 URLMatcherCondition m1 = factory.CreateHostSuffixCondition("example.com");
548 URLMatcherCondition m2 = factory.CreatePathContainsCondition("foo");
549 URLMatcherConditionSet::Conditions conditions;
550 conditions.insert(m1);
551 conditions.insert(m2);
552
553 URLQueryElementMatcherCondition q1(key,
554 value,
555 query_value_match_type,
556 query_element_type,
557 match_type,
558 &factory);
559 URLMatcherConditionSet::QueryConditions query_conditions;
560 query_conditions.insert(q1);
561
562 scoped_ptr<URLMatcherSchemeFilter> scheme_filter;
563 scoped_ptr<URLMatcherPortFilter> port_filter;
564
565 scoped_refptr<URLMatcherConditionSet> condition_set(
566 new URLMatcherConditionSet(1,
567 conditions,
568 query_conditions,
569 scheme_filter.Pass(),
570 port_filter.Pass()));
571
572 GURL url("http://www.example.com/foo?" + url_query);
573
574 URLMatcher matcher;
575 URLMatcherConditionSet::Vector vector;
576 vector.push_back(condition_set);
577 matcher.AddConditionSets(vector);
578
579 return matcher.MatchURL(url).size() == 1;
580}
581
582} // namespace
583
584TEST(URLMatcherConditionSetTest, QueryMatching) {
585 EXPECT_TRUE(
586 IsQueryMatch("a=foo&b=foo&a=barr",
587 "a",
588 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY_VALUE,
589 "bar",
590 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_PREFIX,
591 URLQueryElementMatcherCondition::MATCH_ANY));
592 EXPECT_FALSE(
593 IsQueryMatch("a=foo&b=foo&a=barr",
594 "a",
595 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY_VALUE,
596 "bar",
597 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_EXACT,
598 URLQueryElementMatcherCondition::MATCH_ANY));
599 EXPECT_TRUE(
600 IsQueryMatch("a=foo&b=foo&a=barr",
601 "a",
602 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY,
603 "bar",
604 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_PREFIX,
605 URLQueryElementMatcherCondition::MATCH_ANY));
606 EXPECT_FALSE(
607 IsQueryMatch("a=foo&b=foo&a=barr",
608 "a",
609 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY,
610 "bar",
611 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_EXACT,
612 URLQueryElementMatcherCondition::MATCH_ANY));
613 EXPECT_TRUE(
614 IsQueryMatch("a&b=foo&a=barr",
615 "a",
616 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY,
617 "bar",
618 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_EXACT,
619 URLQueryElementMatcherCondition::MATCH_ANY));
620 EXPECT_FALSE(
621 IsQueryMatch("a=foo&b=foo&a=barr",
622 "a",
623 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY,
624 "bar",
625 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_EXACT,
626 URLQueryElementMatcherCondition::MATCH_ANY));
627
628 EXPECT_FALSE(
629 IsQueryMatch("a=foo&b=foo&a=bar",
630 "a",
631 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY_VALUE,
632 "bar",
633 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_EXACT,
634 URLQueryElementMatcherCondition::MATCH_ALL));
635 EXPECT_TRUE(
636 IsQueryMatch("a=bar&b=foo&a=bar",
637 "a",
638 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY_VALUE,
639 "bar",
640 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_EXACT,
641 URLQueryElementMatcherCondition::MATCH_ALL));
642 EXPECT_TRUE(
643 IsQueryMatch("a=bar&b=foo&a=bar",
644 "b",
645 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY_VALUE,
646 "foo",
647 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_EXACT,
648 URLQueryElementMatcherCondition::MATCH_ALL));
649 EXPECT_FALSE(
650 IsQueryMatch("a=bar&b=foo&a=bar",
651 "b",
652 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY_VALUE,
653 "goo",
654 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_EXACT,
655 URLQueryElementMatcherCondition::MATCH_ALL));
656 EXPECT_FALSE(
657 IsQueryMatch("a=bar&b=foo&a=bar",
658 "c",
659 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY_VALUE,
660 "goo",
661 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_EXACT,
662 URLQueryElementMatcherCondition::MATCH_ALL));
663 EXPECT_TRUE(
664 IsQueryMatch("a=foo1&b=foo&a=foo2",
665 "a",
666 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY_VALUE,
667 "foo",
668 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_PREFIX,
669 URLQueryElementMatcherCondition::MATCH_ALL));
670 EXPECT_FALSE(
671 IsQueryMatch("a=foo1&b=foo&a=fo02",
672 "a",
673 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY_VALUE,
674 "foo",
675 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_PREFIX,
676 URLQueryElementMatcherCondition::MATCH_ALL));
677 EXPECT_TRUE(
678 IsQueryMatch("a&b=foo&a",
679 "a",
680 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY,
681 "foo",
682 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_PREFIX,
683 URLQueryElementMatcherCondition::MATCH_ALL));
684 EXPECT_TRUE(
685 IsQueryMatch("alt&b=foo",
686 "a",
687 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY,
688 "foo",
689 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_PREFIX,
690 URLQueryElementMatcherCondition::MATCH_ALL));
691 EXPECT_TRUE(
692 IsQueryMatch("b=foo&a",
693 "a",
694 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY,
695 "foo",
696 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_PREFIX,
697 URLQueryElementMatcherCondition::MATCH_ALL));
698 EXPECT_FALSE(
699 IsQueryMatch("b=foo",
700 "a",
701 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY,
702 "foo",
703 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_PREFIX,
704 URLQueryElementMatcherCondition::MATCH_ALL));
705 EXPECT_TRUE(
706 IsQueryMatch("b=foo&a",
707 "a",
708 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY,
709 "foo",
710 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_EXACT,
711 URLQueryElementMatcherCondition::MATCH_ALL));
712
713 EXPECT_TRUE(
714 IsQueryMatch("a=foo&b=foo&a=bar",
715 "a",
716 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY_VALUE,
717 "foo",
718 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_EXACT,
719 URLQueryElementMatcherCondition::MATCH_FIRST));
720 EXPECT_FALSE(
721 IsQueryMatch("a=foo&b=foo&a=bar",
722 "a",
723 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY_VALUE,
724 "bar",
725 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_EXACT,
726 URLQueryElementMatcherCondition::MATCH_FIRST));
727 EXPECT_TRUE(
728 IsQueryMatch("a=foo1&b=foo&a=bar",
729 "a",
730 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY_VALUE,
731 "foo",
732 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_PREFIX,
733 URLQueryElementMatcherCondition::MATCH_FIRST));
734 EXPECT_FALSE(
735 IsQueryMatch("a=foo1&b=foo&a=bar",
736 "a",
737 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY_VALUE,
738 "foo",
739 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_EXACT,
740 URLQueryElementMatcherCondition::MATCH_FIRST));
741 EXPECT_TRUE(
742 IsQueryMatch("a&b=foo&a=bar",
743 "a",
744 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY,
745 "foo",
746 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_EXACT,
747 URLQueryElementMatcherCondition::MATCH_FIRST));
748 EXPECT_TRUE(
749 IsQueryMatch("alt&b=foo&a=bar",
750 "a",
751 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY,
752 "foo",
753 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_PREFIX,
754 URLQueryElementMatcherCondition::MATCH_FIRST));
755 EXPECT_FALSE(
756 IsQueryMatch("alt&b=foo&a=bar",
757 "a",
758 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY,
759 "foo",
760 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_EXACT,
761 URLQueryElementMatcherCondition::MATCH_FIRST));
762
763 EXPECT_FALSE(
764 IsQueryMatch("a=foo&b=foo&a=bar",
765 "a",
766 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY_VALUE,
767 "foo",
768 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_EXACT,
769 URLQueryElementMatcherCondition::MATCH_LAST));
770 EXPECT_TRUE(
771 IsQueryMatch("a=foo&b=foo&a=bar",
772 "a",
773 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY_VALUE,
774 "bar",
775 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_EXACT,
776 URLQueryElementMatcherCondition::MATCH_LAST));
777 EXPECT_FALSE(
778 IsQueryMatch("a=foo1&b=foo&a=bar",
779 "a",
780 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY_VALUE,
781 "foo",
782 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_PREFIX,
783 URLQueryElementMatcherCondition::MATCH_LAST));
784 EXPECT_TRUE(
785 IsQueryMatch("a=foo1&b=foo&a=bar1",
786 "a",
787 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY_VALUE,
788 "bar",
789 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_PREFIX,
790 URLQueryElementMatcherCondition::MATCH_LAST));
791 EXPECT_FALSE(
792 IsQueryMatch("a&b=foo&a=bar",
793 "a",
794 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY,
795 "foo",
796 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_EXACT,
797 URLQueryElementMatcherCondition::MATCH_LAST));
798 EXPECT_TRUE(
799 IsQueryMatch("b=foo&alt",
800 "a",
801 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY,
802 "foo",
803 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_PREFIX,
804 URLQueryElementMatcherCondition::MATCH_LAST));
805 EXPECT_FALSE(
806 IsQueryMatch("b=foo&alt",
807 "a",
808 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY,
809 "foo",
810 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_EXACT,
811 URLQueryElementMatcherCondition::MATCH_LAST));
812}
[email protected]fb5bcc02012-02-17 14:05:42813
814//
815// URLMatcher
816//
817
818TEST(URLMatcherTest, FullTest) {
819 GURL url1("http://www.example.com/foo?bar=1");
820 GURL url2("http://foo.example.com/index.html");
821
822 URLMatcher matcher;
823 URLMatcherConditionFactory* factory = matcher.condition_factory();
824
825 // First insert.
826 URLMatcherConditionSet::Conditions conditions1;
827 conditions1.insert(factory->CreateHostSuffixCondition("example.com"));
828 conditions1.insert(factory->CreatePathContainsCondition("foo"));
829
830 const int kConditionSetId1 = 1;
[email protected]3b001a02012-04-05 10:38:06831 URLMatcherConditionSet::Vector insert1;
832 insert1.push_back(make_scoped_refptr(
833 new URLMatcherConditionSet(kConditionSetId1, conditions1)));
[email protected]fb5bcc02012-02-17 14:05:42834 matcher.AddConditionSets(insert1);
835 EXPECT_EQ(1u, matcher.MatchURL(url1).size());
836 EXPECT_EQ(0u, matcher.MatchURL(url2).size());
837
838 // Second insert.
839 URLMatcherConditionSet::Conditions conditions2;
840 conditions2.insert(factory->CreateHostSuffixCondition("example.com"));
841
842 const int kConditionSetId2 = 2;
[email protected]3b001a02012-04-05 10:38:06843 URLMatcherConditionSet::Vector insert2;
844 insert2.push_back(make_scoped_refptr(
845 new URLMatcherConditionSet(kConditionSetId2, conditions2)));
[email protected]fb5bcc02012-02-17 14:05:42846 matcher.AddConditionSets(insert2);
847 EXPECT_EQ(2u, matcher.MatchURL(url1).size());
848 EXPECT_EQ(1u, matcher.MatchURL(url2).size());
849
850 // This should be the cached singleton.
851 int patternId1 = factory->CreateHostSuffixCondition(
[email protected]5bcf3b72012-09-14 00:20:28852 "example.com").string_pattern()->id();
[email protected]fb5bcc02012-02-17 14:05:42853
[email protected]5bcf3b72012-09-14 00:20:28854 // Third insert.
855 URLMatcherConditionSet::Conditions conditions3;
856 conditions3.insert(factory->CreateHostSuffixCondition("example.com"));
857 conditions3.insert(factory->CreateURLMatchesCondition("x.*[0-9]"));
858
859 const int kConditionSetId3 = 3;
860 URLMatcherConditionSet::Vector insert3;
861 insert3.push_back(make_scoped_refptr(
862 new URLMatcherConditionSet(kConditionSetId3, conditions3)));
863 matcher.AddConditionSets(insert3);
864 EXPECT_EQ(3u, matcher.MatchURL(url1).size());
865 EXPECT_EQ(1u, matcher.MatchURL(url2).size());
866
867 // Removal of third insert.
868 std::vector<URLMatcherConditionSet::ID> remove3;
869 remove3.push_back(kConditionSetId3);
870 matcher.RemoveConditionSets(remove3);
871 EXPECT_EQ(2u, matcher.MatchURL(url1).size());
872 EXPECT_EQ(1u, matcher.MatchURL(url2).size());
873
874 // Removal of second insert.
[email protected]fb5bcc02012-02-17 14:05:42875 std::vector<URLMatcherConditionSet::ID> remove2;
876 remove2.push_back(kConditionSetId2);
877 matcher.RemoveConditionSets(remove2);
878 EXPECT_EQ(1u, matcher.MatchURL(url1).size());
879 EXPECT_EQ(0u, matcher.MatchURL(url2).size());
880
881 // Removal of first insert.
882 std::vector<URLMatcherConditionSet::ID> remove1;
883 remove1.push_back(kConditionSetId1);
884 matcher.RemoveConditionSets(remove1);
885 EXPECT_EQ(0u, matcher.MatchURL(url1).size());
886 EXPECT_EQ(0u, matcher.MatchURL(url2).size());
887
[email protected]357c4db2012-03-29 07:51:57888 EXPECT_TRUE(matcher.IsEmpty());
889
[email protected]fb5bcc02012-02-17 14:05:42890 // The cached singleton in matcher.condition_factory_ should be destroyed to
891 // free memory.
892 int patternId2 = factory->CreateHostSuffixCondition(
[email protected]5bcf3b72012-09-14 00:20:28893 "example.com").string_pattern()->id();
[email protected]fb5bcc02012-02-17 14:05:42894 // If patternId1 and patternId2 are different that indicates that
[email protected]5bcf3b72012-09-14 00:20:28895 // matcher.condition_factory_ does not leak memory by holding onto
896 // unused patterns.
[email protected]fb5bcc02012-02-17 14:05:42897 EXPECT_NE(patternId1, patternId2);
898}
899
[email protected]ea6249b2012-12-06 18:45:20900TEST(URLMatcherTest, TestComponentsImplyContains) {
901 // Due to a different implementation of component (prefix, suffix and equals)
902 // and *Contains conditions we need to check that when a pattern matches a
903 // given part of a URL as equal, prefix or suffix, it also matches it in the
904 // "contains" test.
905 GURL url("https://www.google.com:1234/webhp?test=val&a=b");
906
907 URLMatcher matcher;
908 URLMatcherConditionFactory* factory = matcher.condition_factory();
909
910 URLMatcherConditionSet::Conditions conditions;
911
912 // First insert all the matching equals => contains pairs.
913 conditions.insert(factory->CreateHostEqualsCondition("www.google.com"));
914 conditions.insert(factory->CreateHostContainsCondition("www.google.com"));
915
916 conditions.insert(factory->CreateHostPrefixCondition("www."));
917 conditions.insert(factory->CreateHostContainsCondition("www."));
918
919 conditions.insert(factory->CreateHostSuffixCondition("com"));
920 conditions.insert(factory->CreateHostContainsCondition("com"));
921
922 conditions.insert(factory->CreatePathEqualsCondition("/webhp"));
923 conditions.insert(factory->CreatePathContainsCondition("/webhp"));
924
925 conditions.insert(factory->CreatePathPrefixCondition("/we"));
926 conditions.insert(factory->CreatePathContainsCondition("/we"));
927
928 conditions.insert(factory->CreatePathSuffixCondition("hp"));
929 conditions.insert(factory->CreatePathContainsCondition("hp"));
930
931 conditions.insert(factory->CreateQueryEqualsCondition("test=val&a=b"));
932 conditions.insert(factory->CreateQueryContainsCondition("test=val&a=b"));
933
934 conditions.insert(factory->CreateQueryPrefixCondition("test=v"));
935 conditions.insert(factory->CreateQueryContainsCondition("test=v"));
936
937 conditions.insert(factory->CreateQuerySuffixCondition("l&a=b"));
938 conditions.insert(factory->CreateQueryContainsCondition("l&a=b"));
939
940 // The '?' for equality is just ignored.
941 conditions.insert(factory->CreateQueryEqualsCondition("?test=val&a=b"));
942 // Due to '?' the condition created here is a prefix-testing condition.
943 conditions.insert(factory->CreateQueryContainsCondition("?test=val&a=b"));
944
945 const int kConditionSetId = 1;
946 URLMatcherConditionSet::Vector insert;
947 insert.push_back(make_scoped_refptr(
948 new URLMatcherConditionSet(kConditionSetId, conditions)));
949 matcher.AddConditionSets(insert);
950 EXPECT_EQ(1u, matcher.MatchURL(url).size());
951}
952
[email protected]2280dc82013-04-11 20:04:01953// Check that matches in everything but the query are found.
954TEST(URLMatcherTest, TestOriginAndPathRegExPositive) {
955 GURL url("https://www.google.com:1234/webhp?test=val&a=b");
956
957 URLMatcher matcher;
958 URLMatcherConditionFactory* factory = matcher.condition_factory();
959
960 URLMatcherConditionSet::Conditions conditions;
961
962 conditions.insert(factory->CreateOriginAndPathMatchesCondition("w..hp"));
963 const int kConditionSetId = 1;
964 URLMatcherConditionSet::Vector insert;
965 insert.push_back(make_scoped_refptr(
966 new URLMatcherConditionSet(kConditionSetId, conditions)));
967 matcher.AddConditionSets(insert);
968 EXPECT_EQ(1u, matcher.MatchURL(url).size());
969}
970
971// Check that matches in the query are ignored.
972TEST(URLMatcherTest, TestOriginAndPathRegExNegative) {
973 GURL url("https://www.google.com:1234/webhp?test=val&a=b");
974
975 URLMatcher matcher;
976 URLMatcherConditionFactory* factory = matcher.condition_factory();
977
978 URLMatcherConditionSet::Conditions conditions;
979
980 conditions.insert(factory->CreateOriginAndPathMatchesCondition("val"));
981 const int kConditionSetId = 1;
982 URLMatcherConditionSet::Vector insert;
983 insert.push_back(make_scoped_refptr(
984 new URLMatcherConditionSet(kConditionSetId, conditions)));
985 matcher.AddConditionSets(insert);
986 EXPECT_EQ(0u, matcher.MatchURL(url).size());
987}
988
[email protected]716c0162013-12-13 20:36:53989} // namespace url_matcher