OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/memory/scoped_ptr.h" | 5 #include "base/memory/scoped_ptr.h" |
6 #include "chrome/common/extensions/url_pattern.h" | 6 #include "chrome/common/extensions/url_pattern.h" |
7 #include "testing/gtest/include/gtest/gtest.h" | 7 #include "testing/gtest/include/gtest/gtest.h" |
8 #include "googleurl/src/gurl.h" | 8 #include "googleurl/src/gurl.h" |
9 | 9 |
10 // See url_pattern.h for examples of valid and invalid patterns. | 10 // See url_pattern.h for examples of valid and invalid patterns. |
11 | 11 |
12 static const int kAllSchemes = | 12 static const int kAllSchemes = |
13 URLPattern::SCHEME_HTTP | | 13 URLPattern::SCHEME_HTTP | |
14 URLPattern::SCHEME_HTTPS | | 14 URLPattern::SCHEME_HTTPS | |
15 URLPattern::SCHEME_FILE | | 15 URLPattern::SCHEME_FILE | |
16 URLPattern::SCHEME_FTP | | 16 URLPattern::SCHEME_FTP | |
17 URLPattern::SCHEME_CHROMEUI; | 17 URLPattern::SCHEME_CHROMEUI | |
| 18 URLPattern::SCHEME_EXTENSION | |
| 19 URLPattern::SCHEME_FILESYSTEM; |
18 | 20 |
19 TEST(ExtensionURLPatternTest, ParseInvalid) { | 21 TEST(ExtensionURLPatternTest, ParseInvalid) { |
20 const struct { | 22 const struct { |
21 const char* pattern; | 23 const char* pattern; |
22 URLPattern::ParseResult expected_result; | 24 URLPattern::ParseResult expected_result; |
23 } kInvalidPatterns[] = { | 25 } kInvalidPatterns[] = { |
24 { "http", URLPattern::PARSE_ERROR_MISSING_SCHEME_SEPARATOR }, | 26 { "http", URLPattern::PARSE_ERROR_MISSING_SCHEME_SEPARATOR }, |
25 { "http:", URLPattern::PARSE_ERROR_WRONG_SCHEME_SEPARATOR }, | 27 { "http:", URLPattern::PARSE_ERROR_WRONG_SCHEME_SEPARATOR }, |
26 { "http:/", URLPattern::PARSE_ERROR_WRONG_SCHEME_SEPARATOR }, | 28 { "http:/", URLPattern::PARSE_ERROR_WRONG_SCHEME_SEPARATOR }, |
27 { "about://", URLPattern::PARSE_ERROR_WRONG_SCHEME_SEPARATOR }, | 29 { "about://", URLPattern::PARSE_ERROR_WRONG_SCHEME_SEPARATOR }, |
(...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
269 }; | 271 }; |
270 | 272 |
271 // <all_urls> | 273 // <all_urls> |
272 TEST(ExtensionURLPatternTest, Match11) { | 274 TEST(ExtensionURLPatternTest, Match11) { |
273 URLPattern pattern(URLPattern::ERROR_ON_PORTS, kAllSchemes); | 275 URLPattern pattern(URLPattern::ERROR_ON_PORTS, kAllSchemes); |
274 EXPECT_EQ(URLPattern::PARSE_SUCCESS, pattern.Parse("<all_urls>")); | 276 EXPECT_EQ(URLPattern::PARSE_SUCCESS, pattern.Parse("<all_urls>")); |
275 EXPECT_TRUE(pattern.MatchesScheme("chrome")); | 277 EXPECT_TRUE(pattern.MatchesScheme("chrome")); |
276 EXPECT_TRUE(pattern.MatchesScheme("http")); | 278 EXPECT_TRUE(pattern.MatchesScheme("http")); |
277 EXPECT_TRUE(pattern.MatchesScheme("https")); | 279 EXPECT_TRUE(pattern.MatchesScheme("https")); |
278 EXPECT_TRUE(pattern.MatchesScheme("file")); | 280 EXPECT_TRUE(pattern.MatchesScheme("file")); |
| 281 EXPECT_TRUE(pattern.MatchesScheme("chrome-extension")); |
279 EXPECT_TRUE(pattern.match_subdomains()); | 282 EXPECT_TRUE(pattern.match_subdomains()); |
280 EXPECT_TRUE(pattern.match_all_urls()); | 283 EXPECT_TRUE(pattern.match_all_urls()); |
281 EXPECT_EQ("/*", pattern.path()); | 284 EXPECT_EQ("/*", pattern.path()); |
282 EXPECT_TRUE(pattern.MatchesURL(GURL("chrome://favicon/http://google.com"))); | 285 EXPECT_TRUE(pattern.MatchesURL(GURL("chrome://favicon/http://google.com"))); |
283 EXPECT_TRUE(pattern.MatchesURL(GURL("http://127.0.0.1"))); | 286 EXPECT_TRUE(pattern.MatchesURL(GURL("http://127.0.0.1"))); |
284 EXPECT_TRUE(pattern.MatchesURL(GURL("file:///foo/bar"))); | 287 EXPECT_TRUE(pattern.MatchesURL(GURL("file:///foo/bar"))); |
285 EXPECT_TRUE(pattern.MatchesURL(GURL("file://localhost/foo/bar"))); | 288 EXPECT_TRUE(pattern.MatchesURL(GURL("file://localhost/foo/bar"))); |
286 | 289 |
287 // Make sure the properties are the same when creating an <all_urls> pattern | 290 // Make sure the properties are the same when creating an <all_urls> pattern |
288 // via SetMatchAllURLs and by parsing <all_urls>. | 291 // via SetMatchAllURLs and by parsing <all_urls>. |
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
428 EXPECT_EQ("www.example.com", pattern.host()); | 431 EXPECT_EQ("www.example.com", pattern.host()); |
429 EXPECT_FALSE(pattern.match_subdomains()); | 432 EXPECT_FALSE(pattern.match_subdomains()); |
430 EXPECT_FALSE(pattern.match_all_urls()); | 433 EXPECT_FALSE(pattern.match_all_urls()); |
431 EXPECT_EQ("/foo", pattern.path()); | 434 EXPECT_EQ("/foo", pattern.path()); |
432 EXPECT_EQ("*", pattern.port()); | 435 EXPECT_EQ("*", pattern.port()); |
433 EXPECT_TRUE(pattern.MatchesURL(GURL("http://www.example.com:80/foo"))); | 436 EXPECT_TRUE(pattern.MatchesURL(GURL("http://www.example.com:80/foo"))); |
434 EXPECT_TRUE(pattern.MatchesURL(GURL("http://www.example.com/foo"))); | 437 EXPECT_TRUE(pattern.MatchesURL(GURL("http://www.example.com/foo"))); |
435 EXPECT_TRUE(pattern.MatchesURL(GURL("http://www.example.com:8080/foo"))); | 438 EXPECT_TRUE(pattern.MatchesURL(GURL("http://www.example.com:8080/foo"))); |
436 } | 439 } |
437 | 440 |
| 441 // chrome-extension:// |
| 442 TEST(ExtensionURLPatternTest, Match19) { |
| 443 URLPattern pattern(URLPattern::ERROR_ON_PORTS, URLPattern::SCHEME_EXTENSION); |
| 444 EXPECT_EQ(URLPattern::PARSE_SUCCESS, |
| 445 pattern.Parse("chrome-extension://ftw/*")); |
| 446 EXPECT_EQ("chrome-extension", pattern.scheme()); |
| 447 EXPECT_EQ("ftw", pattern.host()); |
| 448 EXPECT_FALSE(pattern.match_subdomains()); |
| 449 EXPECT_FALSE(pattern.match_all_urls()); |
| 450 EXPECT_EQ("/*", pattern.path()); |
| 451 EXPECT_TRUE(pattern.MatchesURL(GURL("chrome-extension://ftw"))); |
| 452 EXPECT_TRUE(pattern.MatchesURL( |
| 453 GURL("chrome-extension://ftw/http://google.com"))); |
| 454 EXPECT_TRUE(pattern.MatchesURL( |
| 455 GURL("chrome-extension://ftw/https://google.com"))); |
| 456 EXPECT_FALSE(pattern.MatchesURL(GURL("chrome-extension://foobar"))); |
| 457 }; |
| 458 |
438 static const struct GetAsStringPatterns { | 459 static const struct GetAsStringPatterns { |
439 const char* pattern; | 460 const char* pattern; |
440 } kGetAsStringTestCases[] = { | 461 } kGetAsStringTestCases[] = { |
441 { "http://www/" }, | 462 { "http://www/" }, |
442 { "http://*/*" }, | 463 { "http://*/*" }, |
443 { "chrome://*/*" }, | 464 { "chrome://*/*" }, |
444 { "chrome://newtab/" }, | 465 { "chrome://newtab/" }, |
445 { "about:*" }, | 466 { "about:*" }, |
446 { "about:blank" }, | 467 { "about:blank" }, |
447 { "chrome-extension://*/*" }, | 468 { "chrome-extension://*/*" }, |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
532 | 553 |
533 URLPatternList all_schemes(URLPattern( | 554 URLPatternList all_schemes(URLPattern( |
534 kAllSchemes, | 555 kAllSchemes, |
535 "*://google.com/foo").ConvertToExplicitSchemes()); | 556 "*://google.com/foo").ConvertToExplicitSchemes()); |
536 | 557 |
537 URLPatternList monkey(URLPattern( | 558 URLPatternList monkey(URLPattern( |
538 URLPattern::SCHEME_HTTP | URLPattern::SCHEME_HTTPS | | 559 URLPattern::SCHEME_HTTP | URLPattern::SCHEME_HTTPS | |
539 URLPattern::SCHEME_FTP, | 560 URLPattern::SCHEME_FTP, |
540 "http://google.com/monkey").ConvertToExplicitSchemes()); | 561 "http://google.com/monkey").ConvertToExplicitSchemes()); |
541 | 562 |
542 ASSERT_EQ(5u, all_urls.size()); | 563 ASSERT_EQ(7u, all_urls.size()); |
543 ASSERT_EQ(2u, all_schemes.size()); | 564 ASSERT_EQ(2u, all_schemes.size()); |
544 ASSERT_EQ(1u, monkey.size()); | 565 ASSERT_EQ(1u, monkey.size()); |
545 | 566 |
546 EXPECT_EQ("http://*/*", all_urls[0].GetAsString()); | 567 EXPECT_EQ("http://*/*", all_urls[0].GetAsString()); |
547 EXPECT_EQ("https://*/*", all_urls[1].GetAsString()); | 568 EXPECT_EQ("https://*/*", all_urls[1].GetAsString()); |
548 EXPECT_EQ("file:///*", all_urls[2].GetAsString()); | 569 EXPECT_EQ("file:///*", all_urls[2].GetAsString()); |
549 EXPECT_EQ("ftp://*/*", all_urls[3].GetAsString()); | 570 EXPECT_EQ("ftp://*/*", all_urls[3].GetAsString()); |
550 EXPECT_EQ("chrome://*/*", all_urls[4].GetAsString()); | 571 EXPECT_EQ("chrome://*/*", all_urls[4].GetAsString()); |
551 | 572 |
552 EXPECT_EQ("http://google.com/foo", all_schemes[0].GetAsString()); | 573 EXPECT_EQ("http://google.com/foo", all_schemes[0].GetAsString()); |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
657 | 678 |
658 URLPattern pattern1(URLPattern::USE_PORTS, URLPattern::SCHEME_ALL); | 679 URLPattern pattern1(URLPattern::USE_PORTS, URLPattern::SCHEME_ALL); |
659 URLPattern pattern2(URLPattern::USE_PORTS, URLPattern::SCHEME_ALL); | 680 URLPattern pattern2(URLPattern::USE_PORTS, URLPattern::SCHEME_ALL); |
660 | 681 |
661 pattern1.Parse(kEqualsTestCases[i].pattern1); | 682 pattern1.Parse(kEqualsTestCases[i].pattern1); |
662 pattern2.Parse(kEqualsTestCases[i].pattern2); | 683 pattern2.Parse(kEqualsTestCases[i].pattern2); |
663 EXPECT_EQ(kEqualsTestCases[i].expected_equal, pattern1 == pattern2) | 684 EXPECT_EQ(kEqualsTestCases[i].expected_equal, pattern1 == pattern2) |
664 << message; | 685 << message; |
665 } | 686 } |
666 } | 687 } |
OLD | NEW |