1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/memory/scoped_ptr.h"
6 #include "extensions/common/url_pattern.h"
7 #include "testing/gtest/include/gtest/gtest.h"
8 #include "googleurl/src/gurl.h"
10 // See url_pattern.h for examples of valid and invalid patterns.
12 static const int kAllSchemes
=
13 URLPattern::SCHEME_HTTP
|
14 URLPattern::SCHEME_HTTPS
|
15 URLPattern::SCHEME_FILE
|
16 URLPattern::SCHEME_FTP
|
17 URLPattern::SCHEME_CHROMEUI
|
18 URLPattern::SCHEME_EXTENSION
|
19 URLPattern::SCHEME_FILESYSTEM
;
21 TEST(ExtensionURLPatternTest
, ParseInvalid
) {
24 URLPattern::ParseResult expected_result
;
25 } kInvalidPatterns
[] = {
26 { "http", URLPattern::PARSE_ERROR_MISSING_SCHEME_SEPARATOR
},
27 { "http:", URLPattern::PARSE_ERROR_WRONG_SCHEME_SEPARATOR
},
28 { "http:/", URLPattern::PARSE_ERROR_WRONG_SCHEME_SEPARATOR
},
29 { "about://", URLPattern::PARSE_ERROR_WRONG_SCHEME_SEPARATOR
},
30 { "http://", URLPattern::PARSE_ERROR_EMPTY_HOST
},
31 { "http:///", URLPattern::PARSE_ERROR_EMPTY_HOST
},
32 { "http://*foo/bar", URLPattern::PARSE_ERROR_INVALID_HOST_WILDCARD
},
33 { "http://foo.*.bar/baz", URLPattern::PARSE_ERROR_INVALID_HOST_WILDCARD
},
34 { "http://fo.*.ba:123/baz", URLPattern::PARSE_ERROR_INVALID_HOST_WILDCARD
},
35 { "http:/bar", URLPattern::PARSE_ERROR_WRONG_SCHEME_SEPARATOR
},
36 { "http://bar", URLPattern::PARSE_ERROR_EMPTY_PATH
},
39 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(kInvalidPatterns
); ++i
) {
40 URLPattern
pattern(URLPattern::SCHEME_ALL
);
41 EXPECT_EQ(kInvalidPatterns
[i
].expected_result
,
42 pattern
.Parse(kInvalidPatterns
[i
].pattern
))
43 << kInvalidPatterns
[i
].pattern
;
47 TEST(ExtensionURLPatternTest
, Ports
) {
50 URLPattern::ParseResult expected_result
;
51 const char* expected_port
;
53 { "http://foo:1234/", URLPattern::PARSE_SUCCESS
, "1234" },
54 { "http://foo:1234/bar", URLPattern::PARSE_SUCCESS
, "1234" },
55 { "http://*.foo:1234/", URLPattern::PARSE_SUCCESS
, "1234" },
56 { "http://*.foo:1234/bar", URLPattern::PARSE_SUCCESS
,"1234" },
57 { "http://:1234/", URLPattern::PARSE_SUCCESS
, "1234" },
58 { "http://foo:/", URLPattern::PARSE_ERROR_INVALID_PORT
, "*" },
59 { "http://foo:*/", URLPattern::PARSE_SUCCESS
, "*" },
60 { "http://*.foo:/", URLPattern::PARSE_ERROR_INVALID_PORT
, "*" },
61 { "http://foo:com/", URLPattern::PARSE_ERROR_INVALID_PORT
, "*" },
62 { "http://foo:123456/", URLPattern::PARSE_ERROR_INVALID_PORT
, "*" },
63 { "http://foo:80:80/monkey", URLPattern::PARSE_ERROR_INVALID_PORT
, "*" },
64 { "file://foo:1234/bar", URLPattern::PARSE_SUCCESS
, "*" },
65 { "chrome://foo:1234/bar", URLPattern::PARSE_ERROR_INVALID_PORT
, "*" },
67 // Port-like strings in the path should not trigger a warning.
68 { "http://*/:1234", URLPattern::PARSE_SUCCESS
, "*" },
69 { "http://*.foo/bar:1234", URLPattern::PARSE_SUCCESS
, "*" },
70 { "http://foo/bar:1234/path", URLPattern::PARSE_SUCCESS
,"*" },
73 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(kTestPatterns
); ++i
) {
74 URLPattern
pattern(URLPattern::SCHEME_ALL
);
75 EXPECT_EQ(kTestPatterns
[i
].expected_result
,
76 pattern
.Parse(kTestPatterns
[i
].pattern
))
77 << "Got unexpected result for URL pattern: "
78 << kTestPatterns
[i
].pattern
;
79 EXPECT_EQ(kTestPatterns
[i
].expected_port
, pattern
.port())
80 << "Got unexpected port for URL pattern: " << kTestPatterns
[i
].pattern
;
84 // all pages for a given scheme
85 TEST(ExtensionURLPatternTest
, Match1
) {
86 URLPattern
pattern(kAllSchemes
);
87 EXPECT_EQ(URLPattern::PARSE_SUCCESS
, pattern
.Parse("http://*/*"));
88 EXPECT_EQ("http", pattern
.scheme());
89 EXPECT_EQ("", pattern
.host());
90 EXPECT_TRUE(pattern
.match_subdomains());
91 EXPECT_FALSE(pattern
.match_all_urls());
92 EXPECT_EQ("/*", pattern
.path());
93 EXPECT_TRUE(pattern
.MatchesURL(GURL("http://google.com")));
94 EXPECT_TRUE(pattern
.MatchesURL(GURL("http://yahoo.com")));
95 EXPECT_TRUE(pattern
.MatchesURL(GURL("http://google.com/foo")));
96 EXPECT_FALSE(pattern
.MatchesURL(GURL("https://google.com")));
97 EXPECT_TRUE(pattern
.MatchesURL(GURL("http://74.125.127.100/search")));
101 TEST(ExtensionURLPatternTest
, Match2
) {
102 URLPattern
pattern(kAllSchemes
);
103 EXPECT_EQ(URLPattern::PARSE_SUCCESS
, pattern
.Parse("https://*/foo*"));
104 EXPECT_EQ("https", pattern
.scheme());
105 EXPECT_EQ("", pattern
.host());
106 EXPECT_TRUE(pattern
.match_subdomains());
107 EXPECT_FALSE(pattern
.match_all_urls());
108 EXPECT_EQ("/foo*", pattern
.path());
109 EXPECT_TRUE(pattern
.MatchesURL(GURL("https://www.google.com/foo")));
110 EXPECT_TRUE(pattern
.MatchesURL(GURL("https://www.google.com/foobar")));
111 EXPECT_FALSE(pattern
.MatchesURL(GURL("http://www.google.com/foo")));
112 EXPECT_FALSE(pattern
.MatchesURL(GURL("https://www.google.com/")));
113 EXPECT_TRUE(pattern
.MatchesURL(
114 GURL("filesystem:https://www.google.com/foobar/")));
118 TEST(URLPatternTest
, Match3
) {
119 URLPattern
pattern(kAllSchemes
);
120 EXPECT_EQ(URLPattern::PARSE_SUCCESS
,
121 pattern
.Parse("http://*.google.com/foo*bar"));
122 EXPECT_EQ("http", pattern
.scheme());
123 EXPECT_EQ("google.com", pattern
.host());
124 EXPECT_TRUE(pattern
.match_subdomains());
125 EXPECT_FALSE(pattern
.match_all_urls());
126 EXPECT_EQ("/foo*bar", pattern
.path());
127 EXPECT_TRUE(pattern
.MatchesURL(GURL("http://google.com/foobar")));
128 EXPECT_TRUE(pattern
.MatchesURL(GURL("http://www.google.com/foo?bar")));
129 EXPECT_TRUE(pattern
.MatchesURL(
130 GURL("http://monkey.images.google.com/foooobar")));
131 EXPECT_FALSE(pattern
.MatchesURL(GURL("http://yahoo.com/foobar")));
132 EXPECT_TRUE(pattern
.MatchesURL(
133 GURL("filesystem:http://google.com/foo/bar")));
134 EXPECT_FALSE(pattern
.MatchesURL(
135 GURL("filesystem:http://google.com/temporary/foobar")));
139 TEST(ExtensionURLPatternTest
, Match5
) {
140 URLPattern
pattern(kAllSchemes
);
141 EXPECT_EQ(URLPattern::PARSE_SUCCESS
, pattern
.Parse("file:///foo?bar\\*baz"));
142 EXPECT_EQ("file", pattern
.scheme());
143 EXPECT_EQ("", pattern
.host());
144 EXPECT_FALSE(pattern
.match_subdomains());
145 EXPECT_FALSE(pattern
.match_all_urls());
146 EXPECT_EQ("/foo?bar\\*baz", pattern
.path());
147 EXPECT_TRUE(pattern
.MatchesURL(GURL("file:///foo?bar\\hellobaz")));
148 EXPECT_FALSE(pattern
.MatchesURL(GURL("file:///fooXbar\\hellobaz")));
152 TEST(ExtensionURLPatternTest
, Match6
) {
153 URLPattern
pattern(kAllSchemes
);
154 EXPECT_EQ(URLPattern::PARSE_SUCCESS
, pattern
.Parse("http://127.0.0.1/*"));
155 EXPECT_EQ("http", pattern
.scheme());
156 EXPECT_EQ("127.0.0.1", pattern
.host());
157 EXPECT_FALSE(pattern
.match_subdomains());
158 EXPECT_FALSE(pattern
.match_all_urls());
159 EXPECT_EQ("/*", pattern
.path());
160 EXPECT_TRUE(pattern
.MatchesURL(GURL("http://127.0.0.1")));
163 // subdomain matching with ip addresses
164 TEST(ExtensionURLPatternTest
, Match7
) {
165 URLPattern
pattern(kAllSchemes
);
166 // allowed, but useless
167 EXPECT_EQ(URLPattern::PARSE_SUCCESS
, pattern
.Parse("http://*.0.0.1/*"));
168 EXPECT_EQ("http", pattern
.scheme());
169 EXPECT_EQ("0.0.1", pattern
.host());
170 EXPECT_TRUE(pattern
.match_subdomains());
171 EXPECT_FALSE(pattern
.match_all_urls());
172 EXPECT_EQ("/*", pattern
.path());
173 // Subdomain matching is never done if the argument has an IP address host.
174 EXPECT_FALSE(pattern
.MatchesURL(GURL("http://127.0.0.1")));
178 TEST(ExtensionURLPatternTest
, Match8
) {
179 URLPattern
pattern(kAllSchemes
);
180 // The below is the ASCII encoding of the following URL:
181 // http://*.\xe1\x80\xbf/a\xc2\x81\xe1*
182 EXPECT_EQ(URLPattern::PARSE_SUCCESS
,
183 pattern
.Parse("http://*.xn--gkd/a%C2%81%E1*"));
184 EXPECT_EQ("http", pattern
.scheme());
185 EXPECT_EQ("xn--gkd", pattern
.host());
186 EXPECT_TRUE(pattern
.match_subdomains());
187 EXPECT_FALSE(pattern
.match_all_urls());
188 EXPECT_EQ("/a%C2%81%E1*", pattern
.path());
189 EXPECT_TRUE(pattern
.MatchesURL(
190 GURL("http://abc.\xe1\x80\xbf/a\xc2\x81\xe1xyz")));
191 EXPECT_TRUE(pattern
.MatchesURL(
192 GURL("http://\xe1\x80\xbf/a\xc2\x81\xe1\xe1")));
196 TEST(ExtensionURLPatternTest
, Match9
) {
197 URLPattern
pattern(kAllSchemes
);
198 EXPECT_EQ(URLPattern::PARSE_SUCCESS
, pattern
.Parse("chrome://favicon/*"));
199 EXPECT_EQ("chrome", pattern
.scheme());
200 EXPECT_EQ("favicon", pattern
.host());
201 EXPECT_FALSE(pattern
.match_subdomains());
202 EXPECT_FALSE(pattern
.match_all_urls());
203 EXPECT_EQ("/*", pattern
.path());
204 EXPECT_TRUE(pattern
.MatchesURL(GURL("chrome://favicon/http://google.com")));
205 EXPECT_TRUE(pattern
.MatchesURL(GURL("chrome://favicon/https://google.com")));
206 EXPECT_FALSE(pattern
.MatchesURL(GURL("chrome://history")));
210 TEST(ExtensionURLPatternTest
, Match10
) {
211 URLPattern
pattern(kAllSchemes
);
212 EXPECT_EQ(URLPattern::PARSE_SUCCESS
, pattern
.Parse("*://*/*"));
213 EXPECT_TRUE(pattern
.MatchesScheme("http"));
214 EXPECT_TRUE(pattern
.MatchesScheme("https"));
215 EXPECT_FALSE(pattern
.MatchesScheme("chrome"));
216 EXPECT_FALSE(pattern
.MatchesScheme("file"));
217 EXPECT_FALSE(pattern
.MatchesScheme("ftp"));
218 EXPECT_TRUE(pattern
.match_subdomains());
219 EXPECT_FALSE(pattern
.match_all_urls());
220 EXPECT_EQ("/*", pattern
.path());
221 EXPECT_TRUE(pattern
.MatchesURL(GURL("http://127.0.0.1")));
222 EXPECT_FALSE(pattern
.MatchesURL(GURL("chrome://favicon/http://google.com")));
223 EXPECT_FALSE(pattern
.MatchesURL(GURL("file:///foo/bar")));
224 EXPECT_FALSE(pattern
.MatchesURL(GURL("file://localhost/foo/bar")));
228 TEST(ExtensionURLPatternTest
, Match11
) {
229 URLPattern
pattern(kAllSchemes
);
230 EXPECT_EQ(URLPattern::PARSE_SUCCESS
, pattern
.Parse("<all_urls>"));
231 EXPECT_TRUE(pattern
.MatchesScheme("chrome"));
232 EXPECT_TRUE(pattern
.MatchesScheme("http"));
233 EXPECT_TRUE(pattern
.MatchesScheme("https"));
234 EXPECT_TRUE(pattern
.MatchesScheme("file"));
235 EXPECT_TRUE(pattern
.MatchesScheme("filesystem"));
236 EXPECT_TRUE(pattern
.MatchesScheme("chrome-extension"));
237 EXPECT_TRUE(pattern
.match_subdomains());
238 EXPECT_TRUE(pattern
.match_all_urls());
239 EXPECT_EQ("/*", pattern
.path());
240 EXPECT_TRUE(pattern
.MatchesURL(GURL("chrome://favicon/http://google.com")));
241 EXPECT_TRUE(pattern
.MatchesURL(GURL("http://127.0.0.1")));
242 EXPECT_TRUE(pattern
.MatchesURL(GURL("file:///foo/bar")));
243 EXPECT_TRUE(pattern
.MatchesURL(GURL("file://localhost/foo/bar")));
245 // Make sure the properties are the same when creating an <all_urls> pattern
246 // via SetMatchAllURLs and by parsing <all_urls>.
247 URLPattern
pattern2(kAllSchemes
);
248 pattern2
.SetMatchAllURLs(true);
250 EXPECT_EQ(pattern
.valid_schemes(), pattern2
.valid_schemes());
251 EXPECT_EQ(pattern
.match_subdomains(), pattern2
.match_subdomains());
252 EXPECT_EQ(pattern
.path(), pattern2
.path());
253 EXPECT_EQ(pattern
.match_all_urls(), pattern2
.match_all_urls());
254 EXPECT_EQ(pattern
.scheme(), pattern2
.scheme());
255 EXPECT_EQ(pattern
.port(), pattern2
.port());
256 EXPECT_EQ(pattern
.GetAsString(), pattern2
.GetAsString());
259 // SCHEME_ALL matches all schemes.
260 TEST(ExtensionURLPatternTest
, Match12
) {
261 URLPattern
pattern(URLPattern::SCHEME_ALL
);
262 EXPECT_EQ(URLPattern::PARSE_SUCCESS
, pattern
.Parse("<all_urls>"));
263 EXPECT_TRUE(pattern
.MatchesScheme("chrome"));
264 EXPECT_TRUE(pattern
.MatchesScheme("http"));
265 EXPECT_TRUE(pattern
.MatchesScheme("https"));
266 EXPECT_TRUE(pattern
.MatchesScheme("file"));
267 EXPECT_TRUE(pattern
.MatchesScheme("filesystem"));
268 EXPECT_TRUE(pattern
.MatchesScheme("javascript"));
269 EXPECT_TRUE(pattern
.MatchesScheme("data"));
270 EXPECT_TRUE(pattern
.MatchesScheme("about"));
271 EXPECT_TRUE(pattern
.MatchesScheme("chrome-extension"));
272 EXPECT_TRUE(pattern
.match_subdomains());
273 EXPECT_TRUE(pattern
.match_all_urls());
274 EXPECT_EQ("/*", pattern
.path());
275 EXPECT_TRUE(pattern
.MatchesURL(GURL("chrome://favicon/http://google.com")));
276 EXPECT_TRUE(pattern
.MatchesURL(GURL("http://127.0.0.1")));
277 EXPECT_TRUE(pattern
.MatchesURL(GURL("file:///foo/bar")));
278 EXPECT_TRUE(pattern
.MatchesURL(GURL("file://localhost/foo/bar")));
279 EXPECT_TRUE(pattern
.MatchesURL(GURL("chrome://newtab")));
280 EXPECT_TRUE(pattern
.MatchesURL(GURL("about:blank")));
281 EXPECT_TRUE(pattern
.MatchesURL(GURL("about:version")));
282 EXPECT_TRUE(pattern
.MatchesURL(
283 GURL("data:text/html;charset=utf-8,<html>asdf</html>")));
286 static const struct MatchPatterns
{
289 } kMatch13UrlPatternTestCases
[] = {
290 {"about:*", "about:blank"},
291 {"about:blank", "about:blank"},
292 {"about:*", "about:version"},
293 {"chrome-extension://*/*", "chrome-extension://FTW"},
294 {"data:*", "data:monkey"},
295 {"javascript:*", "javascript:atemyhomework"},
298 // SCHEME_ALL and specific schemes.
299 TEST(ExtensionURLPatternTest
, Match13
) {
300 for (size_t i
= 0; i
< arraysize(kMatch13UrlPatternTestCases
); ++i
) {
301 URLPattern
pattern(URLPattern::SCHEME_ALL
);
302 EXPECT_EQ(URLPattern::PARSE_SUCCESS
,
303 pattern
.Parse(kMatch13UrlPatternTestCases
[i
].pattern
))
304 << " while parsing " << kMatch13UrlPatternTestCases
[i
].pattern
;
305 EXPECT_TRUE(pattern
.MatchesURL(
306 GURL(kMatch13UrlPatternTestCases
[i
].matches
)))
307 << " while matching " << kMatch13UrlPatternTestCases
[i
].matches
;
311 URLPattern
pattern(URLPattern::SCHEME_ALL
);
312 EXPECT_EQ(URLPattern::PARSE_SUCCESS
, pattern
.Parse("data:*"));
313 EXPECT_FALSE(pattern
.MatchesURL(GURL("about:blank")));
316 // file scheme with empty hostname
317 TEST(ExtensionURLPatternTest
, Match14
) {
318 URLPattern
pattern(kAllSchemes
);
319 EXPECT_EQ(URLPattern::PARSE_SUCCESS
, pattern
.Parse("file:///foo*"));
320 EXPECT_EQ("file", pattern
.scheme());
321 EXPECT_EQ("", pattern
.host());
322 EXPECT_FALSE(pattern
.match_subdomains());
323 EXPECT_FALSE(pattern
.match_all_urls());
324 EXPECT_EQ("/foo*", pattern
.path());
325 EXPECT_FALSE(pattern
.MatchesURL(GURL("file://foo")));
326 EXPECT_FALSE(pattern
.MatchesURL(GURL("file://foobar")));
327 EXPECT_TRUE(pattern
.MatchesURL(GURL("file:///foo")));
328 EXPECT_TRUE(pattern
.MatchesURL(GURL("file:///foobar")));
329 EXPECT_TRUE(pattern
.MatchesURL(GURL("file://localhost/foo")));
332 // file scheme without hostname part
333 TEST(ExtensionURLPatternTest
, Match15
) {
334 URLPattern
pattern(kAllSchemes
);
335 EXPECT_EQ(URLPattern::PARSE_SUCCESS
, pattern
.Parse("file://foo*"));
336 EXPECT_EQ("file", pattern
.scheme());
337 EXPECT_EQ("", pattern
.host());
338 EXPECT_FALSE(pattern
.match_subdomains());
339 EXPECT_FALSE(pattern
.match_all_urls());
340 EXPECT_EQ("/foo*", pattern
.path());
341 EXPECT_FALSE(pattern
.MatchesURL(GURL("file://foo")));
342 EXPECT_FALSE(pattern
.MatchesURL(GURL("file://foobar")));
343 EXPECT_TRUE(pattern
.MatchesURL(GURL("file:///foo")));
344 EXPECT_TRUE(pattern
.MatchesURL(GURL("file:///foobar")));
345 EXPECT_TRUE(pattern
.MatchesURL(GURL("file://localhost/foo")));
348 // file scheme with hostname
349 TEST(ExtensionURLPatternTest
, Match16
) {
350 URLPattern
pattern(kAllSchemes
);
351 EXPECT_EQ(URLPattern::PARSE_SUCCESS
, pattern
.Parse("file://localhost/foo*"));
352 EXPECT_EQ("file", pattern
.scheme());
353 // Since hostname is ignored for file://.
354 EXPECT_EQ("", pattern
.host());
355 EXPECT_FALSE(pattern
.match_subdomains());
356 EXPECT_FALSE(pattern
.match_all_urls());
357 EXPECT_EQ("/foo*", pattern
.path());
358 EXPECT_FALSE(pattern
.MatchesURL(GURL("file://foo")));
359 EXPECT_FALSE(pattern
.MatchesURL(GURL("file://foobar")));
360 EXPECT_TRUE(pattern
.MatchesURL(GURL("file:///foo")));
361 EXPECT_TRUE(pattern
.MatchesURL(GURL("file:///foobar")));
362 EXPECT_TRUE(pattern
.MatchesURL(GURL("file://localhost/foo")));
366 TEST(ExtensionURLPatternTest
, Match17
) {
367 URLPattern
pattern(kAllSchemes
);
368 EXPECT_EQ(URLPattern::PARSE_SUCCESS
,
369 pattern
.Parse("http://www.example.com:80/foo"));
370 EXPECT_EQ("http", pattern
.scheme());
371 EXPECT_EQ("www.example.com", pattern
.host());
372 EXPECT_FALSE(pattern
.match_subdomains());
373 EXPECT_FALSE(pattern
.match_all_urls());
374 EXPECT_EQ("/foo", pattern
.path());
375 EXPECT_EQ("80", pattern
.port());
376 EXPECT_TRUE(pattern
.MatchesURL(GURL("http://www.example.com:80/foo")));
377 EXPECT_TRUE(pattern
.MatchesURL(GURL("http://www.example.com/foo")));
378 EXPECT_FALSE(pattern
.MatchesURL(GURL("http://www.example.com:8080/foo")));
379 EXPECT_FALSE(pattern
.MatchesURL(
380 GURL("filesystem:http://www.example.com:8080/foo/")));
381 EXPECT_FALSE(pattern
.MatchesURL(GURL("filesystem:http://www.example.com/f/foo")));
384 // Explicit port wildcard
385 TEST(ExtensionURLPatternTest
, Match18
) {
386 URLPattern
pattern(kAllSchemes
);
387 EXPECT_EQ(URLPattern::PARSE_SUCCESS
,
388 pattern
.Parse("http://www.example.com:*/foo"));
389 EXPECT_EQ("http", pattern
.scheme());
390 EXPECT_EQ("www.example.com", pattern
.host());
391 EXPECT_FALSE(pattern
.match_subdomains());
392 EXPECT_FALSE(pattern
.match_all_urls());
393 EXPECT_EQ("/foo", pattern
.path());
394 EXPECT_EQ("*", pattern
.port());
395 EXPECT_TRUE(pattern
.MatchesURL(GURL("http://www.example.com:80/foo")));
396 EXPECT_TRUE(pattern
.MatchesURL(GURL("http://www.example.com/foo")));
397 EXPECT_TRUE(pattern
.MatchesURL(GURL("http://www.example.com:8080/foo")));
398 EXPECT_FALSE(pattern
.MatchesURL(
399 GURL("filesystem:http://www.example.com:8080/foo/")));
402 // chrome-extension://
403 TEST(ExtensionURLPatternTest
, Match19
) {
404 URLPattern
pattern(URLPattern::SCHEME_EXTENSION
);
405 EXPECT_EQ(URLPattern::PARSE_SUCCESS
,
406 pattern
.Parse("chrome-extension://ftw/*"));
407 EXPECT_EQ("chrome-extension", pattern
.scheme());
408 EXPECT_EQ("ftw", pattern
.host());
409 EXPECT_FALSE(pattern
.match_subdomains());
410 EXPECT_FALSE(pattern
.match_all_urls());
411 EXPECT_EQ("/*", pattern
.path());
412 EXPECT_TRUE(pattern
.MatchesURL(GURL("chrome-extension://ftw")));
413 EXPECT_TRUE(pattern
.MatchesURL(
414 GURL("chrome-extension://ftw/http://google.com")));
415 EXPECT_TRUE(pattern
.MatchesURL(
416 GURL("chrome-extension://ftw/https://google.com")));
417 EXPECT_FALSE(pattern
.MatchesURL(GURL("chrome-extension://foobar")));
418 EXPECT_TRUE(pattern
.MatchesURL(
419 GURL("filesystem:chrome-extension://ftw/t/file.txt")));
422 static const struct GetAsStringPatterns
{
424 } kGetAsStringTestCases
[] = {
428 { "chrome://newtab/" },
431 { "chrome-extension://*/*" },
432 { "chrome-extension://FTW/" },
436 { "javascript:atemyhomework" },
437 { "http://www.example.com:8080/foo" },
440 TEST(ExtensionURLPatternTest
, GetAsString
) {
441 for (size_t i
= 0; i
< arraysize(kGetAsStringTestCases
); ++i
) {
442 URLPattern
pattern(URLPattern::SCHEME_ALL
);
443 EXPECT_EQ(URLPattern::PARSE_SUCCESS
,
444 pattern
.Parse(kGetAsStringTestCases
[i
].pattern
))
445 << "Error parsing " << kGetAsStringTestCases
[i
].pattern
;
446 EXPECT_EQ(kGetAsStringTestCases
[i
].pattern
,
447 pattern
.GetAsString());
451 void TestPatternOverlap(const URLPattern
& pattern1
, const URLPattern
& pattern2
,
452 bool expect_overlap
) {
453 EXPECT_EQ(expect_overlap
, pattern1
.OverlapsWith(pattern2
))
454 << pattern1
.GetAsString() << ", " << pattern2
.GetAsString();
455 EXPECT_EQ(expect_overlap
, pattern2
.OverlapsWith(pattern1
))
456 << pattern2
.GetAsString() << ", " << pattern1
.GetAsString();
459 TEST(ExtensionURLPatternTest
, OverlapsWith
) {
460 URLPattern
pattern1(kAllSchemes
, "http://www.google.com/foo/*");
461 URLPattern
pattern2(kAllSchemes
, "https://www.google.com/foo/*");
462 URLPattern
pattern3(kAllSchemes
, "http://*.google.com/foo/*");
463 URLPattern
pattern4(kAllSchemes
, "http://*.yahooo.com/foo/*");
464 URLPattern
pattern5(kAllSchemes
, "http://www.yahooo.com/bar/*");
465 URLPattern
pattern6(kAllSchemes
,
466 "http://www.yahooo.com/bar/baz/*");
467 URLPattern
pattern7(kAllSchemes
, "file:///*");
468 URLPattern
pattern8(kAllSchemes
, "*://*/*");
469 URLPattern
pattern9(URLPattern::SCHEME_HTTPS
, "*://*/*");
470 URLPattern
pattern10(kAllSchemes
, "<all_urls>");
472 TestPatternOverlap(pattern1
, pattern1
, true);
473 TestPatternOverlap(pattern1
, pattern2
, false);
474 TestPatternOverlap(pattern1
, pattern3
, true);
475 TestPatternOverlap(pattern1
, pattern4
, false);
476 TestPatternOverlap(pattern3
, pattern4
, false);
477 TestPatternOverlap(pattern4
, pattern5
, false);
478 TestPatternOverlap(pattern5
, pattern6
, true);
480 // Test that scheme restrictions work.
481 TestPatternOverlap(pattern1
, pattern8
, true);
482 TestPatternOverlap(pattern1
, pattern9
, false);
483 TestPatternOverlap(pattern1
, pattern10
, true);
485 // Test that '<all_urls>' includes file URLs, while scheme '*' does not.
486 TestPatternOverlap(pattern7
, pattern8
, false);
487 TestPatternOverlap(pattern7
, pattern10
, true);
489 // Test that wildcard schemes are handled correctly, especially when compared
491 URLPattern
pattern11(kAllSchemes
, "http://example.com/*");
492 URLPattern
pattern12(kAllSchemes
, "*://example.com/*");
493 URLPattern
pattern13(kAllSchemes
, "*://example.com/foo/*");
494 URLPattern
pattern14(kAllSchemes
, "*://google.com/*");
495 TestPatternOverlap(pattern8
, pattern12
, true);
496 TestPatternOverlap(pattern9
, pattern12
, true);
497 TestPatternOverlap(pattern10
, pattern12
, true);
498 TestPatternOverlap(pattern11
, pattern12
, true);
499 TestPatternOverlap(pattern12
, pattern13
, true);
500 TestPatternOverlap(pattern11
, pattern13
, true);
501 TestPatternOverlap(pattern14
, pattern12
, false);
502 TestPatternOverlap(pattern14
, pattern13
, false);
505 TEST(ExtensionURLPatternTest
, ConvertToExplicitSchemes
) {
506 URLPatternList
all_urls(URLPattern(
508 "<all_urls>").ConvertToExplicitSchemes());
510 URLPatternList
all_schemes(URLPattern(
512 "*://google.com/foo").ConvertToExplicitSchemes());
514 URLPatternList
monkey(URLPattern(
515 URLPattern::SCHEME_HTTP
| URLPattern::SCHEME_HTTPS
|
516 URLPattern::SCHEME_FTP
,
517 "http://google.com/monkey").ConvertToExplicitSchemes());
519 ASSERT_EQ(7u, all_urls
.size());
520 ASSERT_EQ(2u, all_schemes
.size());
521 ASSERT_EQ(1u, monkey
.size());
523 EXPECT_EQ("http://*/*", all_urls
[0].GetAsString());
524 EXPECT_EQ("https://*/*", all_urls
[1].GetAsString());
525 EXPECT_EQ("file:///*", all_urls
[2].GetAsString());
526 EXPECT_EQ("ftp://*/*", all_urls
[3].GetAsString());
527 EXPECT_EQ("chrome://*/*", all_urls
[4].GetAsString());
529 EXPECT_EQ("http://google.com/foo", all_schemes
[0].GetAsString());
530 EXPECT_EQ("https://google.com/foo", all_schemes
[1].GetAsString());
532 EXPECT_EQ("http://google.com/monkey", monkey
[0].GetAsString());
535 TEST(ExtensionURLPatternTest
, IgnorePorts
) {
536 std::string pattern_str
= "http://www.example.com:8080/foo";
537 GURL
url("http://www.example.com:1234/foo");
539 URLPattern
pattern(kAllSchemes
);
540 EXPECT_EQ(URLPattern::PARSE_SUCCESS
, pattern
.Parse(pattern_str
));
542 EXPECT_EQ(pattern_str
, pattern
.GetAsString());
543 EXPECT_FALSE(pattern
.MatchesURL(url
));
546 TEST(ExtensionURLPatternTest
, Equals
) {
548 const char* pattern1
;
549 const char* pattern2
;
551 } kEqualsTestCases
[] = {
553 { "http://en.google.com/blah/*/foo",
554 "https://en.google.com/blah/*/foo",
557 { "https://en.google.com/blah/*/foo",
558 "https://en.google.com/blah/*/foo",
561 { "https://en.google.com/blah/*/foo",
562 "ftp://en.google.com/blah/*/foo",
567 { "https://en.google.com/blah/*/foo",
568 "https://fr.google.com/blah/*/foo",
571 { "https://www.google.com/blah/*/foo",
572 "https://*.google.com/blah/*/foo",
575 { "https://*.google.com/blah/*/foo",
576 "https://*.google.com/blah/*/foo",
581 { "http://en.example.com/blah/*/foo",
582 "http://en.google.com/blah/*/foo",
587 { "http://en.google.com:8000/blah/*/foo",
588 "http://en.google.com/blah/*/foo",
591 { "http://fr.google.com:8000/blah/*/foo",
592 "http://fr.google.com:8000/blah/*/foo",
595 { "http://en.google.com:8000/blah/*/foo",
596 "http://en.google.com:8080/blah/*/foo",
601 { "http://en.google.com/blah/*/foo",
602 "http://en.google.com/blah/*",
605 { "http://en.google.com/*",
606 "http://en.google.com/",
609 { "http://en.google.com/*",
610 "http://en.google.com/*",
625 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(kEqualsTestCases
); ++i
) {
626 std::string message
= kEqualsTestCases
[i
].pattern1
;
628 message
+= kEqualsTestCases
[i
].pattern2
;
630 URLPattern
pattern1(URLPattern::SCHEME_ALL
);
631 URLPattern
pattern2(URLPattern::SCHEME_ALL
);
633 pattern1
.Parse(kEqualsTestCases
[i
].pattern1
);
634 pattern2
.Parse(kEqualsTestCases
[i
].pattern2
);
635 EXPECT_EQ(kEqualsTestCases
[i
].expected_equal
, pattern1
== pattern2
)
640 TEST(ExtensionURLPatternTest
, CanReusePatternWithParse
) {
641 URLPattern
pattern1(URLPattern::SCHEME_ALL
);
642 EXPECT_EQ(URLPattern::PARSE_SUCCESS
, pattern1
.Parse("http://aa.com/*"));
643 EXPECT_EQ(URLPattern::PARSE_SUCCESS
, pattern1
.Parse("http://bb.com/*"));
645 EXPECT_TRUE(pattern1
.MatchesURL(GURL("http://bb.com/path")));
646 EXPECT_FALSE(pattern1
.MatchesURL(GURL("http://aa.com/path")));
648 URLPattern
pattern2(URLPattern::SCHEME_ALL
, URLPattern::kAllUrlsPattern
);
649 EXPECT_EQ(URLPattern::PARSE_SUCCESS
, pattern2
.Parse("http://aa.com/*"));
651 EXPECT_FALSE(pattern2
.MatchesURL(GURL("http://bb.com/path")));
652 EXPECT_TRUE(pattern2
.MatchesURL(GURL("http://aa.com/path")));
653 EXPECT_FALSE(pattern2
.MatchesURL(GURL("http://sub.aa.com/path")));
655 URLPattern
pattern3(URLPattern::SCHEME_ALL
, "http://aa.com/*");
656 EXPECT_EQ(URLPattern::PARSE_SUCCESS
, pattern3
.Parse("http://aa.com:88/*"));
657 EXPECT_FALSE(pattern3
.MatchesURL(GURL("http://aa.com/path")));
658 EXPECT_TRUE(pattern3
.MatchesURL(GURL("http://aa.com:88/path")));