Fix for uninitialized access.
[chromium-blink-merge.git] / extensions / common / url_pattern_unittest.cc
blob8f353cca480c2893f876bea5367ec8f3976a4192
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 "url/gurl.h"
10 namespace {
12 // See url_pattern.h for examples of valid and invalid patterns.
14 static const int kAllSchemes =
15 URLPattern::SCHEME_HTTP |
16 URLPattern::SCHEME_HTTPS |
17 URLPattern::SCHEME_FILE |
18 URLPattern::SCHEME_FTP |
19 URLPattern::SCHEME_CHROMEUI |
20 URLPattern::SCHEME_EXTENSION |
21 URLPattern::SCHEME_FILESYSTEM;
23 TEST(ExtensionURLPatternTest, ParseInvalid) {
24 const struct {
25 const char* pattern;
26 URLPattern::ParseResult expected_result;
27 } kInvalidPatterns[] = {
28 { "http", URLPattern::PARSE_ERROR_MISSING_SCHEME_SEPARATOR },
29 { "http:", URLPattern::PARSE_ERROR_WRONG_SCHEME_SEPARATOR },
30 { "http:/", URLPattern::PARSE_ERROR_WRONG_SCHEME_SEPARATOR },
31 { "about://", URLPattern::PARSE_ERROR_WRONG_SCHEME_SEPARATOR },
32 { "http://", URLPattern::PARSE_ERROR_EMPTY_HOST },
33 { "http:///", URLPattern::PARSE_ERROR_EMPTY_HOST },
34 { "http:// /", URLPattern::PARSE_ERROR_EMPTY_HOST },
35 { "http://*foo/bar", URLPattern::PARSE_ERROR_INVALID_HOST_WILDCARD },
36 { "http://foo.*.bar/baz", URLPattern::PARSE_ERROR_INVALID_HOST_WILDCARD },
37 { "http://fo.*.ba:123/baz", URLPattern::PARSE_ERROR_INVALID_HOST_WILDCARD },
38 { "http:/bar", URLPattern::PARSE_ERROR_WRONG_SCHEME_SEPARATOR },
39 { "http://bar", URLPattern::PARSE_ERROR_EMPTY_PATH },
42 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kInvalidPatterns); ++i) {
43 URLPattern pattern(URLPattern::SCHEME_ALL);
44 EXPECT_EQ(kInvalidPatterns[i].expected_result,
45 pattern.Parse(kInvalidPatterns[i].pattern))
46 << kInvalidPatterns[i].pattern;
50 TEST(ExtensionURLPatternTest, Ports) {
51 const struct {
52 const char* pattern;
53 URLPattern::ParseResult expected_result;
54 const char* expected_port;
55 } kTestPatterns[] = {
56 { "http://foo:1234/", URLPattern::PARSE_SUCCESS, "1234" },
57 { "http://foo:1234/bar", URLPattern::PARSE_SUCCESS, "1234" },
58 { "http://*.foo:1234/", URLPattern::PARSE_SUCCESS, "1234" },
59 { "http://*.foo:1234/bar", URLPattern::PARSE_SUCCESS, "1234" },
60 { "http://:1234/", URLPattern::PARSE_SUCCESS, "1234" },
61 { "http://foo:/", URLPattern::PARSE_ERROR_INVALID_PORT, "*" },
62 { "http://foo:*/", URLPattern::PARSE_SUCCESS, "*" },
63 { "http://*.foo:/", URLPattern::PARSE_ERROR_INVALID_PORT, "*" },
64 { "http://foo:com/", URLPattern::PARSE_ERROR_INVALID_PORT, "*" },
65 { "http://foo:123456/", URLPattern::PARSE_ERROR_INVALID_PORT, "*" },
66 { "http://foo:80:80/monkey", URLPattern::PARSE_ERROR_INVALID_PORT, "*" },
67 { "file://foo:1234/bar", URLPattern::PARSE_SUCCESS, "*" },
68 { "chrome://foo:1234/bar", URLPattern::PARSE_ERROR_INVALID_PORT, "*" },
70 // Port-like strings in the path should not trigger a warning.
71 { "http://*/:1234", URLPattern::PARSE_SUCCESS, "*" },
72 { "http://*.foo/bar:1234", URLPattern::PARSE_SUCCESS, "*" },
73 { "http://foo/bar:1234/path", URLPattern::PARSE_SUCCESS, "*" },
76 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestPatterns); ++i) {
77 URLPattern pattern(URLPattern::SCHEME_ALL);
78 EXPECT_EQ(kTestPatterns[i].expected_result,
79 pattern.Parse(kTestPatterns[i].pattern))
80 << "Got unexpected result for URL pattern: "
81 << kTestPatterns[i].pattern;
82 EXPECT_EQ(kTestPatterns[i].expected_port, pattern.port())
83 << "Got unexpected port for URL pattern: " << kTestPatterns[i].pattern;
87 // all pages for a given scheme
88 TEST(ExtensionURLPatternTest, Match1) {
89 URLPattern pattern(kAllSchemes);
90 EXPECT_EQ(URLPattern::PARSE_SUCCESS, pattern.Parse("http://*/*"));
91 EXPECT_EQ("http", pattern.scheme());
92 EXPECT_EQ("", pattern.host());
93 EXPECT_TRUE(pattern.match_subdomains());
94 EXPECT_FALSE(pattern.match_all_urls());
95 EXPECT_EQ("/*", pattern.path());
96 EXPECT_TRUE(pattern.MatchesURL(GURL("http://google.com")));
97 EXPECT_TRUE(pattern.MatchesURL(GURL("http://yahoo.com")));
98 EXPECT_TRUE(pattern.MatchesURL(GURL("http://google.com/foo")));
99 EXPECT_FALSE(pattern.MatchesURL(GURL("https://google.com")));
100 EXPECT_TRUE(pattern.MatchesURL(GURL("http://74.125.127.100/search")));
103 // all domains
104 TEST(ExtensionURLPatternTest, Match2) {
105 URLPattern pattern(kAllSchemes);
106 EXPECT_EQ(URLPattern::PARSE_SUCCESS, pattern.Parse("https://*/foo*"));
107 EXPECT_EQ("https", pattern.scheme());
108 EXPECT_EQ("", pattern.host());
109 EXPECT_TRUE(pattern.match_subdomains());
110 EXPECT_FALSE(pattern.match_all_urls());
111 EXPECT_EQ("/foo*", pattern.path());
112 EXPECT_TRUE(pattern.MatchesURL(GURL("https://www.google.com/foo")));
113 EXPECT_TRUE(pattern.MatchesURL(GURL("https://www.google.com/foobar")));
114 EXPECT_FALSE(pattern.MatchesURL(GURL("http://www.google.com/foo")));
115 EXPECT_FALSE(pattern.MatchesURL(GURL("https://www.google.com/")));
116 EXPECT_TRUE(pattern.MatchesURL(
117 GURL("filesystem:https://www.google.com/foobar/")));
120 // subdomains
121 TEST(URLPatternTest, Match3) {
122 URLPattern pattern(kAllSchemes);
123 EXPECT_EQ(URLPattern::PARSE_SUCCESS,
124 pattern.Parse("http://*.google.com/foo*bar"));
125 EXPECT_EQ("http", pattern.scheme());
126 EXPECT_EQ("google.com", pattern.host());
127 EXPECT_TRUE(pattern.match_subdomains());
128 EXPECT_FALSE(pattern.match_all_urls());
129 EXPECT_EQ("/foo*bar", pattern.path());
130 EXPECT_TRUE(pattern.MatchesURL(GURL("http://google.com/foobar")));
131 EXPECT_TRUE(pattern.MatchesURL(GURL("http://www.google.com/foo?bar")));
132 EXPECT_TRUE(pattern.MatchesURL(
133 GURL("http://monkey.images.google.com/foooobar")));
134 EXPECT_FALSE(pattern.MatchesURL(GURL("http://yahoo.com/foobar")));
135 EXPECT_TRUE(pattern.MatchesURL(
136 GURL("filesystem:http://google.com/foo/bar")));
137 EXPECT_FALSE(pattern.MatchesURL(
138 GURL("filesystem:http://google.com/temporary/foobar")));
141 // glob escaping
142 TEST(ExtensionURLPatternTest, Match5) {
143 URLPattern pattern(kAllSchemes);
144 EXPECT_EQ(URLPattern::PARSE_SUCCESS, pattern.Parse("file:///foo?bar\\*baz"));
145 EXPECT_EQ("file", pattern.scheme());
146 EXPECT_EQ("", pattern.host());
147 EXPECT_FALSE(pattern.match_subdomains());
148 EXPECT_FALSE(pattern.match_all_urls());
149 EXPECT_EQ("/foo?bar\\*baz", pattern.path());
150 EXPECT_TRUE(pattern.MatchesURL(GURL("file:///foo?bar\\hellobaz")));
151 EXPECT_FALSE(pattern.MatchesURL(GURL("file:///fooXbar\\hellobaz")));
154 // ip addresses
155 TEST(ExtensionURLPatternTest, Match6) {
156 URLPattern pattern(kAllSchemes);
157 EXPECT_EQ(URLPattern::PARSE_SUCCESS, pattern.Parse("http://127.0.0.1/*"));
158 EXPECT_EQ("http", pattern.scheme());
159 EXPECT_EQ("127.0.0.1", pattern.host());
160 EXPECT_FALSE(pattern.match_subdomains());
161 EXPECT_FALSE(pattern.match_all_urls());
162 EXPECT_EQ("/*", pattern.path());
163 EXPECT_TRUE(pattern.MatchesURL(GURL("http://127.0.0.1")));
166 // subdomain matching with ip addresses
167 TEST(ExtensionURLPatternTest, Match7) {
168 URLPattern pattern(kAllSchemes);
169 // allowed, but useless
170 EXPECT_EQ(URLPattern::PARSE_SUCCESS, pattern.Parse("http://*.0.0.1/*"));
171 EXPECT_EQ("http", pattern.scheme());
172 EXPECT_EQ("0.0.1", pattern.host());
173 EXPECT_TRUE(pattern.match_subdomains());
174 EXPECT_FALSE(pattern.match_all_urls());
175 EXPECT_EQ("/*", pattern.path());
176 // Subdomain matching is never done if the argument has an IP address host.
177 EXPECT_FALSE(pattern.MatchesURL(GURL("http://127.0.0.1")));
180 // unicode
181 TEST(ExtensionURLPatternTest, Match8) {
182 URLPattern pattern(kAllSchemes);
183 // The below is the ASCII encoding of the following URL:
184 // http://*.\xe1\x80\xbf/a\xc2\x81\xe1*
185 EXPECT_EQ(URLPattern::PARSE_SUCCESS,
186 pattern.Parse("http://*.xn--gkd/a%C2%81%E1*"));
187 EXPECT_EQ("http", pattern.scheme());
188 EXPECT_EQ("xn--gkd", pattern.host());
189 EXPECT_TRUE(pattern.match_subdomains());
190 EXPECT_FALSE(pattern.match_all_urls());
191 EXPECT_EQ("/a%C2%81%E1*", pattern.path());
192 EXPECT_TRUE(pattern.MatchesURL(
193 GURL("http://abc.\xe1\x80\xbf/a\xc2\x81\xe1xyz")));
194 EXPECT_TRUE(pattern.MatchesURL(
195 GURL("http://\xe1\x80\xbf/a\xc2\x81\xe1\xe1")));
198 // chrome://
199 TEST(ExtensionURLPatternTest, Match9) {
200 URLPattern pattern(kAllSchemes);
201 EXPECT_EQ(URLPattern::PARSE_SUCCESS, pattern.Parse("chrome://favicon/*"));
202 EXPECT_EQ("chrome", pattern.scheme());
203 EXPECT_EQ("favicon", pattern.host());
204 EXPECT_FALSE(pattern.match_subdomains());
205 EXPECT_FALSE(pattern.match_all_urls());
206 EXPECT_EQ("/*", pattern.path());
207 EXPECT_TRUE(pattern.MatchesURL(GURL("chrome://favicon/http://google.com")));
208 EXPECT_TRUE(pattern.MatchesURL(GURL("chrome://favicon/https://google.com")));
209 EXPECT_FALSE(pattern.MatchesURL(GURL("chrome://history")));
212 // *://
213 TEST(ExtensionURLPatternTest, Match10) {
214 URLPattern pattern(kAllSchemes);
215 EXPECT_EQ(URLPattern::PARSE_SUCCESS, pattern.Parse("*://*/*"));
216 EXPECT_TRUE(pattern.MatchesScheme("http"));
217 EXPECT_TRUE(pattern.MatchesScheme("https"));
218 EXPECT_FALSE(pattern.MatchesScheme("chrome"));
219 EXPECT_FALSE(pattern.MatchesScheme("file"));
220 EXPECT_FALSE(pattern.MatchesScheme("ftp"));
221 EXPECT_TRUE(pattern.match_subdomains());
222 EXPECT_FALSE(pattern.match_all_urls());
223 EXPECT_EQ("/*", pattern.path());
224 EXPECT_TRUE(pattern.MatchesURL(GURL("http://127.0.0.1")));
225 EXPECT_FALSE(pattern.MatchesURL(GURL("chrome://favicon/http://google.com")));
226 EXPECT_FALSE(pattern.MatchesURL(GURL("file:///foo/bar")));
227 EXPECT_FALSE(pattern.MatchesURL(GURL("file://localhost/foo/bar")));
230 // <all_urls>
231 TEST(ExtensionURLPatternTest, Match11) {
232 URLPattern pattern(kAllSchemes);
233 EXPECT_EQ(URLPattern::PARSE_SUCCESS, pattern.Parse("<all_urls>"));
234 EXPECT_TRUE(pattern.MatchesScheme("chrome"));
235 EXPECT_TRUE(pattern.MatchesScheme("http"));
236 EXPECT_TRUE(pattern.MatchesScheme("https"));
237 EXPECT_TRUE(pattern.MatchesScheme("file"));
238 EXPECT_TRUE(pattern.MatchesScheme("filesystem"));
239 EXPECT_TRUE(pattern.MatchesScheme("chrome-extension"));
240 EXPECT_TRUE(pattern.match_subdomains());
241 EXPECT_TRUE(pattern.match_all_urls());
242 EXPECT_EQ("/*", pattern.path());
243 EXPECT_TRUE(pattern.MatchesURL(GURL("chrome://favicon/http://google.com")));
244 EXPECT_TRUE(pattern.MatchesURL(GURL("http://127.0.0.1")));
245 EXPECT_TRUE(pattern.MatchesURL(GURL("file:///foo/bar")));
246 EXPECT_TRUE(pattern.MatchesURL(GURL("file://localhost/foo/bar")));
248 // Make sure the properties are the same when creating an <all_urls> pattern
249 // via SetMatchAllURLs and by parsing <all_urls>.
250 URLPattern pattern2(kAllSchemes);
251 pattern2.SetMatchAllURLs(true);
253 EXPECT_EQ(pattern.valid_schemes(), pattern2.valid_schemes());
254 EXPECT_EQ(pattern.match_subdomains(), pattern2.match_subdomains());
255 EXPECT_EQ(pattern.path(), pattern2.path());
256 EXPECT_EQ(pattern.match_all_urls(), pattern2.match_all_urls());
257 EXPECT_EQ(pattern.scheme(), pattern2.scheme());
258 EXPECT_EQ(pattern.port(), pattern2.port());
259 EXPECT_EQ(pattern.GetAsString(), pattern2.GetAsString());
262 // SCHEME_ALL matches all schemes.
263 TEST(ExtensionURLPatternTest, Match12) {
264 URLPattern pattern(URLPattern::SCHEME_ALL);
265 EXPECT_EQ(URLPattern::PARSE_SUCCESS, pattern.Parse("<all_urls>"));
266 EXPECT_TRUE(pattern.MatchesScheme("chrome"));
267 EXPECT_TRUE(pattern.MatchesScheme("http"));
268 EXPECT_TRUE(pattern.MatchesScheme("https"));
269 EXPECT_TRUE(pattern.MatchesScheme("file"));
270 EXPECT_TRUE(pattern.MatchesScheme("filesystem"));
271 EXPECT_TRUE(pattern.MatchesScheme("javascript"));
272 EXPECT_TRUE(pattern.MatchesScheme("data"));
273 EXPECT_TRUE(pattern.MatchesScheme("about"));
274 EXPECT_TRUE(pattern.MatchesScheme("chrome-extension"));
275 EXPECT_TRUE(pattern.match_subdomains());
276 EXPECT_TRUE(pattern.match_all_urls());
277 EXPECT_EQ("/*", pattern.path());
278 EXPECT_TRUE(pattern.MatchesURL(GURL("chrome://favicon/http://google.com")));
279 EXPECT_TRUE(pattern.MatchesURL(GURL("http://127.0.0.1")));
280 EXPECT_TRUE(pattern.MatchesURL(GURL("file:///foo/bar")));
281 EXPECT_TRUE(pattern.MatchesURL(GURL("file://localhost/foo/bar")));
282 EXPECT_TRUE(pattern.MatchesURL(GURL("chrome://newtab")));
283 EXPECT_TRUE(pattern.MatchesURL(GURL("about:blank")));
284 EXPECT_TRUE(pattern.MatchesURL(GURL("about:version")));
285 EXPECT_TRUE(pattern.MatchesURL(
286 GURL("data:text/html;charset=utf-8,<html>asdf</html>")));
289 static const struct MatchPatterns {
290 const char* pattern;
291 const char* matches;
292 } kMatch13UrlPatternTestCases[] = {
293 {"about:*", "about:blank"},
294 {"about:blank", "about:blank"},
295 {"about:*", "about:version"},
296 {"chrome-extension://*/*", "chrome-extension://FTW"},
297 {"data:*", "data:monkey"},
298 {"javascript:*", "javascript:atemyhomework"},
301 // SCHEME_ALL and specific schemes.
302 TEST(ExtensionURLPatternTest, Match13) {
303 for (size_t i = 0; i < arraysize(kMatch13UrlPatternTestCases); ++i) {
304 URLPattern pattern(URLPattern::SCHEME_ALL);
305 EXPECT_EQ(URLPattern::PARSE_SUCCESS,
306 pattern.Parse(kMatch13UrlPatternTestCases[i].pattern))
307 << " while parsing " << kMatch13UrlPatternTestCases[i].pattern;
308 EXPECT_TRUE(pattern.MatchesURL(
309 GURL(kMatch13UrlPatternTestCases[i].matches)))
310 << " while matching " << kMatch13UrlPatternTestCases[i].matches;
313 // Negative test.
314 URLPattern pattern(URLPattern::SCHEME_ALL);
315 EXPECT_EQ(URLPattern::PARSE_SUCCESS, pattern.Parse("data:*"));
316 EXPECT_FALSE(pattern.MatchesURL(GURL("about:blank")));
319 // file scheme with empty hostname
320 TEST(ExtensionURLPatternTest, Match14) {
321 URLPattern pattern(kAllSchemes);
322 EXPECT_EQ(URLPattern::PARSE_SUCCESS, pattern.Parse("file:///foo*"));
323 EXPECT_EQ("file", pattern.scheme());
324 EXPECT_EQ("", pattern.host());
325 EXPECT_FALSE(pattern.match_subdomains());
326 EXPECT_FALSE(pattern.match_all_urls());
327 EXPECT_EQ("/foo*", pattern.path());
328 EXPECT_FALSE(pattern.MatchesURL(GURL("file://foo")));
329 EXPECT_FALSE(pattern.MatchesURL(GURL("file://foobar")));
330 EXPECT_TRUE(pattern.MatchesURL(GURL("file:///foo")));
331 EXPECT_TRUE(pattern.MatchesURL(GURL("file:///foobar")));
332 EXPECT_TRUE(pattern.MatchesURL(GURL("file://localhost/foo")));
335 // file scheme without hostname part
336 TEST(ExtensionURLPatternTest, Match15) {
337 URLPattern pattern(kAllSchemes);
338 EXPECT_EQ(URLPattern::PARSE_SUCCESS, pattern.Parse("file://foo*"));
339 EXPECT_EQ("file", pattern.scheme());
340 EXPECT_EQ("", pattern.host());
341 EXPECT_FALSE(pattern.match_subdomains());
342 EXPECT_FALSE(pattern.match_all_urls());
343 EXPECT_EQ("/foo*", pattern.path());
344 EXPECT_FALSE(pattern.MatchesURL(GURL("file://foo")));
345 EXPECT_FALSE(pattern.MatchesURL(GURL("file://foobar")));
346 EXPECT_TRUE(pattern.MatchesURL(GURL("file:///foo")));
347 EXPECT_TRUE(pattern.MatchesURL(GURL("file:///foobar")));
348 EXPECT_TRUE(pattern.MatchesURL(GURL("file://localhost/foo")));
351 // file scheme with hostname
352 TEST(ExtensionURLPatternTest, Match16) {
353 URLPattern pattern(kAllSchemes);
354 EXPECT_EQ(URLPattern::PARSE_SUCCESS, pattern.Parse("file://localhost/foo*"));
355 EXPECT_EQ("file", pattern.scheme());
356 // Since hostname is ignored for file://.
357 EXPECT_EQ("", pattern.host());
358 EXPECT_FALSE(pattern.match_subdomains());
359 EXPECT_FALSE(pattern.match_all_urls());
360 EXPECT_EQ("/foo*", pattern.path());
361 EXPECT_FALSE(pattern.MatchesURL(GURL("file://foo")));
362 EXPECT_FALSE(pattern.MatchesURL(GURL("file://foobar")));
363 EXPECT_TRUE(pattern.MatchesURL(GURL("file:///foo")));
364 EXPECT_TRUE(pattern.MatchesURL(GURL("file:///foobar")));
365 EXPECT_TRUE(pattern.MatchesURL(GURL("file://localhost/foo")));
368 // Specific port
369 TEST(ExtensionURLPatternTest, Match17) {
370 URLPattern pattern(kAllSchemes);
371 EXPECT_EQ(URLPattern::PARSE_SUCCESS,
372 pattern.Parse("http://www.example.com:80/foo"));
373 EXPECT_EQ("http", pattern.scheme());
374 EXPECT_EQ("www.example.com", pattern.host());
375 EXPECT_FALSE(pattern.match_subdomains());
376 EXPECT_FALSE(pattern.match_all_urls());
377 EXPECT_EQ("/foo", pattern.path());
378 EXPECT_EQ("80", pattern.port());
379 EXPECT_TRUE(pattern.MatchesURL(GURL("http://www.example.com:80/foo")));
380 EXPECT_TRUE(pattern.MatchesURL(GURL("http://www.example.com/foo")));
381 EXPECT_FALSE(pattern.MatchesURL(GURL("http://www.example.com:8080/foo")));
382 EXPECT_FALSE(pattern.MatchesURL(
383 GURL("filesystem:http://www.example.com:8080/foo/")));
384 EXPECT_FALSE(pattern.MatchesURL(
385 GURL("filesystem:http://www.example.com/f/foo")));
388 // Explicit port wildcard
389 TEST(ExtensionURLPatternTest, Match18) {
390 URLPattern pattern(kAllSchemes);
391 EXPECT_EQ(URLPattern::PARSE_SUCCESS,
392 pattern.Parse("http://www.example.com:*/foo"));
393 EXPECT_EQ("http", pattern.scheme());
394 EXPECT_EQ("www.example.com", pattern.host());
395 EXPECT_FALSE(pattern.match_subdomains());
396 EXPECT_FALSE(pattern.match_all_urls());
397 EXPECT_EQ("/foo", pattern.path());
398 EXPECT_EQ("*", pattern.port());
399 EXPECT_TRUE(pattern.MatchesURL(GURL("http://www.example.com:80/foo")));
400 EXPECT_TRUE(pattern.MatchesURL(GURL("http://www.example.com/foo")));
401 EXPECT_TRUE(pattern.MatchesURL(GURL("http://www.example.com:8080/foo")));
402 EXPECT_FALSE(pattern.MatchesURL(
403 GURL("filesystem:http://www.example.com:8080/foo/")));
406 // chrome-extension://
407 TEST(ExtensionURLPatternTest, Match19) {
408 URLPattern pattern(URLPattern::SCHEME_EXTENSION);
409 EXPECT_EQ(URLPattern::PARSE_SUCCESS,
410 pattern.Parse("chrome-extension://ftw/*"));
411 EXPECT_EQ("chrome-extension", pattern.scheme());
412 EXPECT_EQ("ftw", pattern.host());
413 EXPECT_FALSE(pattern.match_subdomains());
414 EXPECT_FALSE(pattern.match_all_urls());
415 EXPECT_EQ("/*", pattern.path());
416 EXPECT_TRUE(pattern.MatchesURL(GURL("chrome-extension://ftw")));
417 EXPECT_TRUE(pattern.MatchesURL(
418 GURL("chrome-extension://ftw/http://google.com")));
419 EXPECT_TRUE(pattern.MatchesURL(
420 GURL("chrome-extension://ftw/https://google.com")));
421 EXPECT_FALSE(pattern.MatchesURL(GURL("chrome-extension://foobar")));
422 EXPECT_TRUE(pattern.MatchesURL(
423 GURL("filesystem:chrome-extension://ftw/t/file.txt")));
426 static const struct GetAsStringPatterns {
427 const char* pattern;
428 } kGetAsStringTestCases[] = {
429 { "http://www/" },
430 { "http://*/*" },
431 { "chrome://*/*" },
432 { "chrome://newtab/" },
433 { "about:*" },
434 { "about:blank" },
435 { "chrome-extension://*/*" },
436 { "chrome-extension://FTW/" },
437 { "data:*" },
438 { "data:monkey" },
439 { "javascript:*" },
440 { "javascript:atemyhomework" },
441 { "http://www.example.com:8080/foo" },
444 TEST(ExtensionURLPatternTest, GetAsString) {
445 for (size_t i = 0; i < arraysize(kGetAsStringTestCases); ++i) {
446 URLPattern pattern(URLPattern::SCHEME_ALL);
447 EXPECT_EQ(URLPattern::PARSE_SUCCESS,
448 pattern.Parse(kGetAsStringTestCases[i].pattern))
449 << "Error parsing " << kGetAsStringTestCases[i].pattern;
450 EXPECT_EQ(kGetAsStringTestCases[i].pattern,
451 pattern.GetAsString());
455 testing::AssertionResult Overlaps(const URLPattern& pattern1,
456 const URLPattern& pattern2) {
457 if (!pattern1.OverlapsWith(pattern2)) {
458 return testing::AssertionFailure()
459 << pattern1.GetAsString() << " does not overlap " <<
460 pattern2.GetAsString();
462 if (!pattern2.OverlapsWith(pattern1)) {
463 return testing::AssertionFailure()
464 << pattern2.GetAsString() << " does not overlap " <<
465 pattern1.GetAsString();
467 return testing::AssertionSuccess()
468 << pattern1.GetAsString() << " overlaps with " << pattern2.GetAsString();
471 TEST(ExtensionURLPatternTest, Overlaps) {
472 URLPattern pattern1(kAllSchemes, "http://www.google.com/foo/*");
473 URLPattern pattern2(kAllSchemes, "https://www.google.com/foo/*");
474 URLPattern pattern3(kAllSchemes, "http://*.google.com/foo/*");
475 URLPattern pattern4(kAllSchemes, "http://*.yahooo.com/foo/*");
476 URLPattern pattern5(kAllSchemes, "http://www.yahooo.com/bar/*");
477 URLPattern pattern6(kAllSchemes,
478 "http://www.yahooo.com/bar/baz/*");
479 URLPattern pattern7(kAllSchemes, "file:///*");
480 URLPattern pattern8(kAllSchemes, "*://*/*");
481 URLPattern pattern9(URLPattern::SCHEME_HTTPS, "*://*/*");
482 URLPattern pattern10(kAllSchemes, "<all_urls>");
484 EXPECT_TRUE(Overlaps(pattern1, pattern1));
485 EXPECT_FALSE(Overlaps(pattern1, pattern2));
486 EXPECT_TRUE(Overlaps(pattern1, pattern3));
487 EXPECT_FALSE(Overlaps(pattern1, pattern4));
488 EXPECT_FALSE(Overlaps(pattern3, pattern4));
489 EXPECT_FALSE(Overlaps(pattern4, pattern5));
490 EXPECT_TRUE(Overlaps(pattern5, pattern6));
492 // Test that scheme restrictions work.
493 EXPECT_TRUE(Overlaps(pattern1, pattern8));
494 EXPECT_FALSE(Overlaps(pattern1, pattern9));
495 EXPECT_TRUE(Overlaps(pattern1, pattern10));
497 // Test that '<all_urls>' includes file URLs, while scheme '*' does not.
498 EXPECT_FALSE(Overlaps(pattern7, pattern8));
499 EXPECT_TRUE(Overlaps(pattern7, pattern10));
501 // Test that wildcard schemes are handled correctly, especially when compared
502 // to each-other.
503 URLPattern pattern11(kAllSchemes, "http://example.com/*");
504 URLPattern pattern12(kAllSchemes, "*://example.com/*");
505 URLPattern pattern13(kAllSchemes, "*://example.com/foo/*");
506 URLPattern pattern14(kAllSchemes, "*://google.com/*");
507 EXPECT_TRUE(Overlaps(pattern8, pattern12));
508 EXPECT_TRUE(Overlaps(pattern9, pattern12));
509 EXPECT_TRUE(Overlaps(pattern10, pattern12));
510 EXPECT_TRUE(Overlaps(pattern11, pattern12));
511 EXPECT_TRUE(Overlaps(pattern12, pattern13));
512 EXPECT_TRUE(Overlaps(pattern11, pattern13));
513 EXPECT_FALSE(Overlaps(pattern14, pattern12));
514 EXPECT_FALSE(Overlaps(pattern14, pattern13));
517 TEST(ExtensionURLPatternTest, ConvertToExplicitSchemes) {
518 URLPatternList all_urls(URLPattern(
519 kAllSchemes,
520 "<all_urls>").ConvertToExplicitSchemes());
522 URLPatternList all_schemes(URLPattern(
523 kAllSchemes,
524 "*://google.com/foo").ConvertToExplicitSchemes());
526 URLPatternList monkey(URLPattern(
527 URLPattern::SCHEME_HTTP | URLPattern::SCHEME_HTTPS |
528 URLPattern::SCHEME_FTP,
529 "http://google.com/monkey").ConvertToExplicitSchemes());
531 ASSERT_EQ(7u, all_urls.size());
532 ASSERT_EQ(2u, all_schemes.size());
533 ASSERT_EQ(1u, monkey.size());
535 EXPECT_EQ("http://*/*", all_urls[0].GetAsString());
536 EXPECT_EQ("https://*/*", all_urls[1].GetAsString());
537 EXPECT_EQ("file:///*", all_urls[2].GetAsString());
538 EXPECT_EQ("ftp://*/*", all_urls[3].GetAsString());
539 EXPECT_EQ("chrome://*/*", all_urls[4].GetAsString());
541 EXPECT_EQ("http://google.com/foo", all_schemes[0].GetAsString());
542 EXPECT_EQ("https://google.com/foo", all_schemes[1].GetAsString());
544 EXPECT_EQ("http://google.com/monkey", monkey[0].GetAsString());
547 TEST(ExtensionURLPatternTest, IgnorePorts) {
548 std::string pattern_str = "http://www.example.com:8080/foo";
549 GURL url("http://www.example.com:1234/foo");
551 URLPattern pattern(kAllSchemes);
552 EXPECT_EQ(URLPattern::PARSE_SUCCESS, pattern.Parse(pattern_str));
554 EXPECT_EQ(pattern_str, pattern.GetAsString());
555 EXPECT_FALSE(pattern.MatchesURL(url));
558 TEST(ExtensionURLPatternTest, IgnoreMissingBackslashes) {
559 std::string pattern_str1 = "http://www.example.com/example";
560 std::string pattern_str2 = "http://www.example.com/example/*";
561 GURL url1("http://www.example.com/example");
562 GURL url2("http://www.example.com/example/");
564 URLPattern pattern1(kAllSchemes);
565 EXPECT_EQ(URLPattern::PARSE_SUCCESS, pattern1.Parse(pattern_str1));
566 URLPattern pattern2(kAllSchemes);
567 EXPECT_EQ(URLPattern::PARSE_SUCCESS, pattern2.Parse(pattern_str2));
569 // Same patterns should match same urls.
570 EXPECT_TRUE(pattern1.MatchesURL(url1));
571 EXPECT_TRUE(pattern2.MatchesURL(url2));
572 // The not terminated path should match the terminated pattern.
573 EXPECT_TRUE(pattern2.MatchesURL(url1));
574 // The terminated path however should not match the unterminated pattern.
575 EXPECT_FALSE(pattern1.MatchesURL(url2));
578 TEST(ExtensionURLPatternTest, Equals) {
579 const struct {
580 const char* pattern1;
581 const char* pattern2;
582 bool expected_equal;
583 } kEqualsTestCases[] = {
584 // schemes
585 { "http://en.google.com/blah/*/foo",
586 "https://en.google.com/blah/*/foo",
587 false
589 { "https://en.google.com/blah/*/foo",
590 "https://en.google.com/blah/*/foo",
591 true
593 { "https://en.google.com/blah/*/foo",
594 "ftp://en.google.com/blah/*/foo",
595 false
598 // subdomains
599 { "https://en.google.com/blah/*/foo",
600 "https://fr.google.com/blah/*/foo",
601 false
603 { "https://www.google.com/blah/*/foo",
604 "https://*.google.com/blah/*/foo",
605 false
607 { "https://*.google.com/blah/*/foo",
608 "https://*.google.com/blah/*/foo",
609 true
612 // domains
613 { "http://en.example.com/blah/*/foo",
614 "http://en.google.com/blah/*/foo",
615 false
618 // ports
619 { "http://en.google.com:8000/blah/*/foo",
620 "http://en.google.com/blah/*/foo",
621 false
623 { "http://fr.google.com:8000/blah/*/foo",
624 "http://fr.google.com:8000/blah/*/foo",
625 true
627 { "http://en.google.com:8000/blah/*/foo",
628 "http://en.google.com:8080/blah/*/foo",
629 false
632 // paths
633 { "http://en.google.com/blah/*/foo",
634 "http://en.google.com/blah/*",
635 false
637 { "http://en.google.com/*",
638 "http://en.google.com/",
639 false
641 { "http://en.google.com/*",
642 "http://en.google.com/*",
643 true
646 // all_urls
647 { "<all_urls>",
648 "<all_urls>",
649 true
651 { "<all_urls>",
652 "http://*/*",
653 false
657 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kEqualsTestCases); ++i) {
658 std::string message = kEqualsTestCases[i].pattern1;
659 message += " ";
660 message += kEqualsTestCases[i].pattern2;
662 URLPattern pattern1(URLPattern::SCHEME_ALL);
663 URLPattern pattern2(URLPattern::SCHEME_ALL);
665 pattern1.Parse(kEqualsTestCases[i].pattern1);
666 pattern2.Parse(kEqualsTestCases[i].pattern2);
667 EXPECT_EQ(kEqualsTestCases[i].expected_equal, pattern1 == pattern2)
668 << message;
672 TEST(ExtensionURLPatternTest, CanReusePatternWithParse) {
673 URLPattern pattern1(URLPattern::SCHEME_ALL);
674 EXPECT_EQ(URLPattern::PARSE_SUCCESS, pattern1.Parse("http://aa.com/*"));
675 EXPECT_EQ(URLPattern::PARSE_SUCCESS, pattern1.Parse("http://bb.com/*"));
677 EXPECT_TRUE(pattern1.MatchesURL(GURL("http://bb.com/path")));
678 EXPECT_FALSE(pattern1.MatchesURL(GURL("http://aa.com/path")));
680 URLPattern pattern2(URLPattern::SCHEME_ALL, URLPattern::kAllUrlsPattern);
681 EXPECT_EQ(URLPattern::PARSE_SUCCESS, pattern2.Parse("http://aa.com/*"));
683 EXPECT_FALSE(pattern2.MatchesURL(GURL("http://bb.com/path")));
684 EXPECT_TRUE(pattern2.MatchesURL(GURL("http://aa.com/path")));
685 EXPECT_FALSE(pattern2.MatchesURL(GURL("http://sub.aa.com/path")));
687 URLPattern pattern3(URLPattern::SCHEME_ALL, "http://aa.com/*");
688 EXPECT_EQ(URLPattern::PARSE_SUCCESS, pattern3.Parse("http://aa.com:88/*"));
689 EXPECT_FALSE(pattern3.MatchesURL(GURL("http://aa.com/path")));
690 EXPECT_TRUE(pattern3.MatchesURL(GURL("http://aa.com:88/path")));
693 // Returns success if neither |a| nor |b| encompasses the other.
694 testing::AssertionResult NeitherContains(const URLPattern& a,
695 const URLPattern& b) {
696 if (a.Contains(b))
697 return testing::AssertionFailure() << a.GetAsString() << " encompasses " <<
698 b.GetAsString();
699 if (b.Contains(a))
700 return testing::AssertionFailure() << b.GetAsString() << " encompasses " <<
701 a.GetAsString();
702 return testing::AssertionSuccess() <<
703 "Neither " << a.GetAsString() << " nor " << b.GetAsString() <<
704 " encompass the other";
707 // Returns success if |a| encompasses |b| but not the other way around.
708 testing::AssertionResult StrictlyContains(const URLPattern& a,
709 const URLPattern& b) {
710 if (!a.Contains(b))
711 return testing::AssertionFailure() << a.GetAsString() <<
712 " does not encompass " <<
713 b.GetAsString();
714 if (b.Contains(a))
715 return testing::AssertionFailure() << b.GetAsString() << " encompasses " <<
716 a.GetAsString();
717 return testing::AssertionSuccess() << a.GetAsString() <<
718 " strictly encompasses " <<
719 b.GetAsString();
722 TEST(ExtensionURLPatternTest, Subset) {
723 URLPattern pattern1(kAllSchemes, "http://www.google.com/foo/*");
724 URLPattern pattern2(kAllSchemes, "https://www.google.com/foo/*");
725 URLPattern pattern3(kAllSchemes, "http://*.google.com/foo/*");
726 URLPattern pattern4(kAllSchemes, "http://*.yahooo.com/foo/*");
727 URLPattern pattern5(kAllSchemes, "http://www.yahooo.com/bar/*");
728 URLPattern pattern6(kAllSchemes, "http://www.yahooo.com/bar/baz/*");
729 URLPattern pattern7(kAllSchemes, "file:///*");
730 URLPattern pattern8(kAllSchemes, "*://*/*");
731 URLPattern pattern9(URLPattern::SCHEME_HTTPS, "*://*/*");
732 URLPattern pattern10(kAllSchemes, "<all_urls>");
733 URLPattern pattern11(kAllSchemes, "http://example.com/*");
734 URLPattern pattern12(kAllSchemes, "*://example.com/*");
735 URLPattern pattern13(kAllSchemes, "*://example.com/foo/*");
737 // All patterns should encompass themselves.
738 EXPECT_TRUE(pattern1.Contains(pattern1));
739 EXPECT_TRUE(pattern2.Contains(pattern2));
740 EXPECT_TRUE(pattern3.Contains(pattern3));
741 EXPECT_TRUE(pattern4.Contains(pattern4));
742 EXPECT_TRUE(pattern5.Contains(pattern5));
743 EXPECT_TRUE(pattern6.Contains(pattern6));
744 EXPECT_TRUE(pattern7.Contains(pattern7));
745 EXPECT_TRUE(pattern8.Contains(pattern8));
746 EXPECT_TRUE(pattern9.Contains(pattern9));
747 EXPECT_TRUE(pattern10.Contains(pattern10));
748 EXPECT_TRUE(pattern11.Contains(pattern11));
749 EXPECT_TRUE(pattern12.Contains(pattern12));
750 EXPECT_TRUE(pattern13.Contains(pattern13));
752 // pattern1's relationship to the other patterns.
753 EXPECT_TRUE(NeitherContains(pattern1, pattern2));
754 EXPECT_TRUE(StrictlyContains(pattern3, pattern1));
755 EXPECT_TRUE(NeitherContains(pattern1, pattern4));
756 EXPECT_TRUE(NeitherContains(pattern1, pattern5));
757 EXPECT_TRUE(NeitherContains(pattern1, pattern6));
758 EXPECT_TRUE(NeitherContains(pattern1, pattern7));
759 EXPECT_TRUE(StrictlyContains(pattern8, pattern1));
760 EXPECT_TRUE(NeitherContains(pattern1, pattern9));
761 EXPECT_TRUE(StrictlyContains(pattern10, pattern1));
762 EXPECT_TRUE(NeitherContains(pattern1, pattern11));
763 EXPECT_TRUE(NeitherContains(pattern1, pattern12));
764 EXPECT_TRUE(NeitherContains(pattern1, pattern13));
766 // pattern2's relationship to the other patterns.
767 EXPECT_TRUE(NeitherContains(pattern2, pattern3));
768 EXPECT_TRUE(NeitherContains(pattern2, pattern4));
769 EXPECT_TRUE(NeitherContains(pattern2, pattern5));
770 EXPECT_TRUE(NeitherContains(pattern2, pattern6));
771 EXPECT_TRUE(NeitherContains(pattern2, pattern7));
772 EXPECT_TRUE(StrictlyContains(pattern8, pattern2));
773 EXPECT_TRUE(StrictlyContains(pattern9, pattern2));
774 EXPECT_TRUE(StrictlyContains(pattern10, pattern2));
775 EXPECT_TRUE(NeitherContains(pattern2, pattern11));
776 EXPECT_TRUE(NeitherContains(pattern2, pattern12));
777 EXPECT_TRUE(NeitherContains(pattern2, pattern13));
779 // Specifically test file:// URLs.
780 EXPECT_TRUE(NeitherContains(pattern7, pattern8));
781 EXPECT_TRUE(NeitherContains(pattern7, pattern9));
782 EXPECT_TRUE(StrictlyContains(pattern10, pattern7));
784 // <all_urls> encompasses everything.
785 EXPECT_TRUE(StrictlyContains(pattern10, pattern1));
786 EXPECT_TRUE(StrictlyContains(pattern10, pattern2));
787 EXPECT_TRUE(StrictlyContains(pattern10, pattern3));
788 EXPECT_TRUE(StrictlyContains(pattern10, pattern4));
789 EXPECT_TRUE(StrictlyContains(pattern10, pattern5));
790 EXPECT_TRUE(StrictlyContains(pattern10, pattern6));
791 EXPECT_TRUE(StrictlyContains(pattern10, pattern7));
792 EXPECT_TRUE(StrictlyContains(pattern10, pattern8));
793 EXPECT_TRUE(StrictlyContains(pattern10, pattern9));
794 EXPECT_TRUE(StrictlyContains(pattern10, pattern11));
795 EXPECT_TRUE(StrictlyContains(pattern10, pattern12));
796 EXPECT_TRUE(StrictlyContains(pattern10, pattern13));
798 // More...
799 EXPECT_TRUE(StrictlyContains(pattern12, pattern11));
800 EXPECT_TRUE(NeitherContains(pattern11, pattern13));
801 EXPECT_TRUE(StrictlyContains(pattern12, pattern13));
804 } // namespace