blob: a2c642a4b4241fe154690ebff14e1b88f91acf54 [file] [log] [blame]
pkalinnikov15cf7242016-07-13 08:57:341// Copyright 2016 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Pavel Kalinnikovd7970632017-06-20 09:07:345#include "components/url_pattern_index/url_pattern.h"
pkalinnikov35d18812017-04-05 17:28:186
pkalinnikov15cf7242016-07-13 08:57:347#include "testing/gtest/include/gtest/gtest.h"
8#include "url/gurl.h"
9
Pavel Kalinnikovd7970632017-06-20 09:07:3410namespace url_pattern_index {
pkalinnikov15cf7242016-07-13 08:57:3411
12namespace {
13
14constexpr proto::AnchorType kAnchorNone = proto::ANCHOR_TYPE_NONE;
15constexpr proto::AnchorType kBoundary = proto::ANCHOR_TYPE_BOUNDARY;
16constexpr proto::AnchorType kSubdomain = proto::ANCHOR_TYPE_SUBDOMAIN;
Karan Bhatiaa9c4e1d2018-09-10 23:37:4717constexpr UrlPattern::MatchCase kMatchCase = UrlPattern::MatchCase::kTrue;
18constexpr UrlPattern::MatchCase kDonotMatchCase = UrlPattern::MatchCase::kFalse;
pkalinnikov15cf7242016-07-13 08:57:3419
20} // namespace
21
Karan Bhatiaa9c4e1d2018-09-10 23:37:4722TEST(UrlPatternTest, MatchesUrl) {
pkalinnikov15cf7242016-07-13 08:57:3423 const struct {
24 UrlPattern url_pattern;
25 const char* url;
26 bool expect_match;
27 } kTestCases[] = {
pkalinnikov35d18812017-04-05 17:28:1828 {{"", proto::URL_PATTERN_TYPE_SUBSTRING}, "http://ex.com/", true},
29 {{"", proto::URL_PATTERN_TYPE_WILDCARDED}, "http://ex.com/", true},
30 {{"", kBoundary, kAnchorNone}, "http://ex.com/", true},
31 {{"", kSubdomain, kAnchorNone}, "http://ex.com/", true},
32 {{"", kSubdomain, kAnchorNone}, "http://ex.com/", true},
33 {{"^", kSubdomain, kAnchorNone}, "http://ex.com/", false},
34 {{".", kSubdomain, kAnchorNone}, "http://ex.com/", false},
35 {{"", kAnchorNone, kBoundary}, "http://ex.com/", true},
36 {{"^", kAnchorNone, kBoundary}, "http://ex.com/", true},
37 {{".", kAnchorNone, kBoundary}, "http://ex.com/", false},
38 {{"", kBoundary, kBoundary}, "http://ex.com/", false},
39 {{"", kSubdomain, kBoundary}, "http://ex.com/", false},
40 {{"com/", kSubdomain, kBoundary}, "http://ex.com/", true},
41
pkalinnikov15cf7242016-07-13 08:57:3442 {{"xampl", proto::URL_PATTERN_TYPE_SUBSTRING},
43 "http://example.com",
44 true},
45 {{"example", proto::URL_PATTERN_TYPE_SUBSTRING},
46 "http://example.com",
47 true},
48 {{"/a?a"}, "http://ex.com/a?a", true},
49 {{"^abc"}, "http://ex.com/abc?a", true},
50 {{"^abc"}, "http://ex.com/a?abc", true},
51 {{"^abc"}, "http://ex.com/abc?abc", true},
pkalinnikov35d18812017-04-05 17:28:1852 {{"^abc^abc"}, "http://ex.com/abc?abc", true},
53 {{"^com^abc^abc"}, "http://ex.com/abc?abc", false},
pkalinnikov15cf7242016-07-13 08:57:3454
55 {{"http://ex", kBoundary, kAnchorNone}, "http://example.com", true},
pkalinnikov35d18812017-04-05 17:28:1856 {{"http://ex", kAnchorNone, kAnchorNone}, "http://example.com", true},
pkalinnikov15cf7242016-07-13 08:57:3457 {{"mple.com/", kAnchorNone, kBoundary}, "http://example.com", true},
pkalinnikov35d18812017-04-05 17:28:1858 {{"mple.com/", kAnchorNone, kAnchorNone}, "http://example.com", true},
59 {{"mple.com/", kSubdomain, kAnchorNone}, "http://example.com", false},
60 {{"ex.com", kSubdomain, kAnchorNone}, "http://hex.com", false},
61 {{"ex.com", kSubdomain, kAnchorNone}, "http://ex.com", true},
62 {{"ex.com", kSubdomain, kAnchorNone}, "http://hex.ex.com", true},
63 {{"ex.com", kSubdomain, kAnchorNone}, "http://hex.hex.com", false},
pkalinnikov15cf7242016-07-13 08:57:3464
65 // Note: "example.com" will be normalized into "example.com/".
Charlie Harrisone88a48532018-12-04 20:31:5866 {{"example.com^", kSubdomain, kAnchorNone},
67 "http://www.example.com",
68 true},
pkalinnikov15cf7242016-07-13 08:57:3469 {{"http://*mpl", kBoundary, kAnchorNone}, "http://example.com", true},
70 {{"mpl*com/", kAnchorNone, kBoundary}, "http://example.com", true},
71 {{"example^com"}, "http://example.com", false},
72 {{"example^com"}, "http://example/com", true},
73 {{"example.com^"}, "http://example.com:8080", true},
74 {{"http*.com/", kBoundary, kBoundary}, "http://example.com", true},
75 {{"http*.org/", kBoundary, kBoundary}, "http://example.com", false},
76
77 {{"/path?*&p1=*&p2="}, "http://ex.com/aaa/path/bbb?k=v&p1=0&p2=1", false},
78 {{"/path?*&p1=*&p2="}, "http://ex.com/aaa/path?k=v&p1=0&p2=1", true},
79 {{"/path?*&p1=*&p2="}, "http://ex.com/aaa/path?k=v&k=v&p1=0&p2=1", true},
80 {{"/path?*&p1=*&p2="},
81 "http://ex.com/aaa/path?k=v&p1=0&p3=10&p2=1",
82 true},
83 {{"/path?*&p1=*&p2="}, "http://ex.com/aaa/path&p1=0&p2=1", false},
84 {{"/path?*&p1=*&p2="}, "http://ex.com/aaa/path?k=v&p2=0&p1=1", false},
85
86 {{"abc*def*ghijk*xyz"},
87 "http://example.com/abcdeffffghijkmmmxyzzz",
88 true},
89 {{"abc*cdef"}, "http://example.com/abcdef", false},
90
91 {{"^^a^^"}, "http://ex.com/?a=/", true},
92 {{"^^a^^"}, "http://ex.com/?a=/&b=0", true},
Karan Bhatia1d1eaed22019-02-20 21:07:1793 {{"^^a^^"}, "http://ex.com/?a=x", false},
94 // The last ^ matches the end of the url.
95 {{"^^a^^"}, "http://ex.com/?a=", true},
pkalinnikov15cf7242016-07-13 08:57:3496
97 {{"ex.com^path^*k=v^"}, "http://ex.com/path/?k1=v1&ak=v&kk=vv", true},
98 {{"ex.com^path^*k=v^"}, "http://ex.com/p/path/?k1=v1&ak=v&kk=vv", false},
99 {{"a^a&a^a&"}, "http://ex.com/a/a/a/a/?a&a&a&a&a", true},
100
101 {{"abc*def^"}, "http://ex.com/abc/a/ddef/", true},
pkalinnikov854818d62016-07-22 11:55:10102
103 {{"https://example.com/"}, "http://example.com/", false},
104 {{"example.com/", kSubdomain, kAnchorNone}, "http://example.com/", true},
105 {{"examp", kSubdomain, kAnchorNone}, "http://example.com/", true},
106 {{"xamp", kSubdomain, kAnchorNone}, "http://example.com/", false},
107 {{"examp", kSubdomain, kAnchorNone}, "http://test.example.com/", true},
108 {{"t.examp", kSubdomain, kAnchorNone}, "http://test.example.com/", false},
109 {{"com^", kSubdomain, kAnchorNone}, "http://test.example.com/", true},
Charles Harrison78ff41bf2018-02-08 20:21:11110 {{"com^x", kSubdomain, kBoundary}, "http://a.com/x", true},
pkalinnikov854818d62016-07-22 11:55:10111 {{"x.com", kSubdomain, kAnchorNone}, "http://ex.com/?url=x.com", false},
112 {{"ex.com/", kSubdomain, kBoundary}, "http://ex.com/", true},
113 {{"ex.com^", kSubdomain, kBoundary}, "http://ex.com/", true},
114 {{"ex.co", kSubdomain, kBoundary}, "http://ex.com/", false},
115 {{"ex.com", kSubdomain, kBoundary}, "http://rex.com.ex.com/", false},
116 {{"ex.com/", kSubdomain, kBoundary}, "http://rex.com.ex.com/", true},
117 {{"http", kSubdomain, kBoundary}, "http://http.com/", false},
118 {{"http", kSubdomain, kAnchorNone}, "http://http.com/", true},
119 {{"/example.com", kSubdomain, kBoundary}, "http://example.com/", false},
120 {{"/example.com/", kSubdomain, kBoundary}, "http://example.com/", false},
Charles Harrison78ff41bf2018-02-08 20:21:11121 {{".", kSubdomain, kAnchorNone}, "http://a..com/", true},
122 {{"^", kSubdomain, kAnchorNone}, "http://a..com/", false},
123 {{".", kSubdomain, kAnchorNone}, "http://a.com./", false},
124 {{"^", kSubdomain, kAnchorNone}, "http://a.com./", true},
125 {{".", kSubdomain, kAnchorNone}, "http://a.com../", true},
126 {{"^", kSubdomain, kAnchorNone}, "http://a.com../", true},
127 {{"/path", kSubdomain, kAnchorNone}, "http://a.com./path/to/x", true},
128 {{"^path", kSubdomain, kAnchorNone}, "http://a.com./path/to/x", true},
129 {{"/path", kSubdomain, kBoundary}, "http://a.com./path", true},
130 {{"^path", kSubdomain, kBoundary}, "http://a.com./path", true},
131 {{"path", kSubdomain, kBoundary}, "http://a.com./path", false},
Karan Bhatiaa9c4e1d2018-09-10 23:37:47132 // Case-sensitivity tests.
133 {{"path", proto::URL_PATTERN_TYPE_SUBSTRING, kDonotMatchCase},
134 "http://a.com/PaTh",
135 true},
136 {{"path", proto::URL_PATTERN_TYPE_SUBSTRING, kMatchCase},
137 "http://a.com/PaTh",
138 false},
139 {{"path", proto::URL_PATTERN_TYPE_SUBSTRING, kDonotMatchCase},
140 "http://a.com/path",
141 true},
142 {{"path", proto::URL_PATTERN_TYPE_SUBSTRING, kMatchCase},
143 "http://a.com/path",
144 true},
145 {{"abc*def^", proto::URL_PATTERN_TYPE_WILDCARDED, kMatchCase},
146 "http://a.com/abcxAdef/vo",
147 true},
148 {{"abc*def^", proto::URL_PATTERN_TYPE_WILDCARDED, kMatchCase},
149 "http://a.com/aBcxAdeF/vo",
150 false},
151 {{"abc*def^", proto::URL_PATTERN_TYPE_WILDCARDED, kDonotMatchCase},
152 "http://a.com/aBcxAdeF/vo",
153 true},
154 {{"abc*def^", proto::URL_PATTERN_TYPE_WILDCARDED, kDonotMatchCase},
155 "http://a.com/abcxAdef/vo",
156 true},
Karan Bhatia1d1eaed22019-02-20 21:07:17157 {{"abc^", kAnchorNone, kAnchorNone}, "https://xyz.com/abc/123", true},
158 {{"abc^", kAnchorNone, kAnchorNone}, "https://xyz.com/abc", true},
159 {{"abc^", kAnchorNone, kAnchorNone}, "https://abc.com", false},
160 {{"abc^", kAnchorNone, kBoundary}, "https://xyz.com/abc/", true},
161 {{"abc^", kAnchorNone, kBoundary}, "https://xyz.com/abc", true},
162 {{"abc^", kAnchorNone, kBoundary}, "https://xyz.com/abc/123", false},
163 {{"http://abc.com/x^", kBoundary, kAnchorNone}, "http://abc.com/x", true},
164 {{"http://abc.com/x^", kBoundary, kAnchorNone},
165 "http://abc.com/x/",
166 true},
167 {{"http://abc.com/x^", kBoundary, kAnchorNone},
168 "http://abc.com/x/123",
169 true},
170 {{"http://abc.com/x^", kBoundary, kBoundary}, "http://abc.com/x", true},
171 {{"http://abc.com/x^", kBoundary, kBoundary}, "http://abc.com/x/", true},
172 {{"http://abc.com/x^", kBoundary, kBoundary},
173 "http://abc.com/x/123",
174 false},
175 {{"abc.com^", kSubdomain, kAnchorNone}, "http://xyz.abc.com/123", true},
176 {{"abc.com^", kSubdomain, kAnchorNone}, "http://xyz.abc.com", true},
177 {{"abc.com^", kSubdomain, kAnchorNone},
178 "http://abc.com.xyz.com?q=abc.com",
179 false},
180 {{"abc.com^", kSubdomain, kBoundary}, "http://xyz.abc.com/123", false},
181 {{"abc.com^", kSubdomain, kBoundary}, "http://xyz.abc.com", true},
182 {{"abc.com^", kSubdomain, kBoundary},
183 "http://abc.com.xyz.com?q=abc.com/",
184 false},
185 {{"abc*^", kAnchorNone, kAnchorNone}, "https://abc.com", true},
186 {{"abc*^", kAnchorNone, kAnchorNone}, "https://abc.com?q=123", true},
187 {{"abc*^", kAnchorNone, kBoundary}, "https://abc.com", true},
188 {{"abc*^", kAnchorNone, kBoundary}, "https://abc.com?q=123", true},
Karandeep Bhatiacf2b1a02019-02-25 23:09:31189 {{"abc*", kAnchorNone, kBoundary}, "https://a.com/abcxyz", true},
190 {{"*google.com", kBoundary, kAnchorNone}, "https://www.google.com", true},
191 {{"*", kBoundary, kBoundary}, "https://example.com", true},
192 {{"", kBoundary, kBoundary}, "https://example.com", false},
pkalinnikov15cf7242016-07-13 08:57:34193 };
194
195 for (const auto& test_case : kTestCases) {
pkalinnikov35d18812017-04-05 17:28:18196 SCOPED_TRACE(testing::Message() << "Rule: " << test_case.url_pattern
197 << "; URL: " << GURL(test_case.url));
pkalinnikov15cf7242016-07-13 08:57:34198
Karan Bhatiae0aeb0e2018-09-12 18:57:21199 GURL url(test_case.url);
200 const bool is_match =
201 test_case.url_pattern.MatchesUrl(UrlPattern::UrlInfo(url));
pkalinnikov15cf7242016-07-13 08:57:34202 EXPECT_EQ(test_case.expect_match, is_match);
203 }
204}
205
Pavel Kalinnikovd7970632017-06-20 09:07:34206} // namespace url_pattern_index