| 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. |
| (...skipping 25 matching lines...) Expand all Loading... |
| 36 | 36 |
| 37 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kInvalidPatterns); ++i) { | 37 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kInvalidPatterns); ++i) { |
| 38 URLPattern pattern(URLPattern::SCHEME_ALL); | 38 URLPattern pattern(URLPattern::SCHEME_ALL); |
| 39 EXPECT_EQ(kInvalidPatterns[i].expected_result, | 39 EXPECT_EQ(kInvalidPatterns[i].expected_result, |
| 40 pattern.Parse(kInvalidPatterns[i].pattern, | 40 pattern.Parse(kInvalidPatterns[i].pattern, |
| 41 URLPattern::PARSE_LENIENT)) | 41 URLPattern::PARSE_LENIENT)) |
| 42 << kInvalidPatterns[i].pattern; | 42 << kInvalidPatterns[i].pattern; |
| 43 } | 43 } |
| 44 }; | 44 }; |
| 45 | 45 |
| 46 TEST(ExtensionURLPatternTest, Colons) { | 46 TEST(ExtensionURLPatternTest, Ports) { |
| 47 const struct { | 47 const struct { |
| 48 const char* pattern; | 48 const char* pattern; |
| 49 URLPattern::ParseResult expected_result; | 49 URLPattern::ParseResult expected_result_strict; |
| 50 URLPattern::ParseResult expected_result_lenient; |
| 51 const char* expected_port; |
| 50 } kTestPatterns[] = { | 52 } kTestPatterns[] = { |
| 51 { "http://foo:1234/", URLPattern::PARSE_ERROR_HAS_COLON }, | 53 { "http://foo:1234/", URLPattern::PARSE_ERROR_HAS_COLON, |
| 52 { "http://foo:1234/bar", URLPattern::PARSE_ERROR_HAS_COLON }, | 54 URLPattern::PARSE_SUCCESS, "1234" }, |
| 53 { "http://*.foo:1234/", URLPattern::PARSE_ERROR_HAS_COLON }, | 55 { "http://foo:1234/bar", URLPattern::PARSE_ERROR_HAS_COLON, |
| 54 { "http://*.foo:1234/bar", URLPattern::PARSE_ERROR_HAS_COLON }, | 56 URLPattern::PARSE_SUCCESS, "1234" }, |
| 55 { "http://:1234/", URLPattern::PARSE_ERROR_HAS_COLON }, | 57 { "http://*.foo:1234/", URLPattern::PARSE_ERROR_HAS_COLON, |
| 56 { "http://foo:/", URLPattern::PARSE_ERROR_HAS_COLON }, | 58 URLPattern::PARSE_SUCCESS, "1234" }, |
| 57 { "http://*.foo:/", URLPattern::PARSE_ERROR_HAS_COLON }, | 59 { "http://*.foo:1234/bar", URLPattern::PARSE_ERROR_HAS_COLON, |
| 58 { "http://foo:com/", URLPattern::PARSE_ERROR_HAS_COLON }, | 60 URLPattern::PARSE_SUCCESS, "1234" }, |
| 61 { "http://:1234/", URLPattern::PARSE_ERROR_HAS_COLON, |
| 62 URLPattern::PARSE_SUCCESS, "1234" }, |
| 63 { "http://foo:/", URLPattern::PARSE_ERROR_HAS_COLON, |
| 64 URLPattern::PARSE_SUCCESS, "" }, |
| 65 { "http://foo:*/", URLPattern::PARSE_ERROR_HAS_COLON, |
| 66 URLPattern::PARSE_SUCCESS, "*" }, |
| 67 { "http://*.foo:/", URLPattern::PARSE_ERROR_HAS_COLON, |
| 68 URLPattern::PARSE_SUCCESS, "" }, |
| 69 { "http://foo:com/", URLPattern::PARSE_ERROR_HAS_COLON, |
| 70 URLPattern::PARSE_ERROR_INVALID_PORT, "" }, |
| 71 { "http://foo:123456/", URLPattern::PARSE_ERROR_HAS_COLON, |
| 72 URLPattern::PARSE_ERROR_INVALID_PORT, "" }, |
| 59 | 73 |
| 60 // Port-like strings in the path should not trigger a warning. | 74 // Port-like strings in the path should not trigger a warning. |
| 61 { "http://*/:1234", URLPattern::PARSE_SUCCESS }, | 75 { "http://*/:1234", URLPattern::PARSE_SUCCESS, |
| 62 { "http://*.foo/bar:1234", URLPattern::PARSE_SUCCESS }, | 76 URLPattern::PARSE_SUCCESS, "" }, |
| 63 { "http://foo/bar:1234/path", URLPattern::PARSE_SUCCESS }, | 77 { "http://*.foo/bar:1234", URLPattern::PARSE_SUCCESS, |
| 78 URLPattern::PARSE_SUCCESS, "" }, |
| 79 { "http://foo/bar:1234/path", URLPattern::PARSE_SUCCESS, |
| 80 URLPattern::PARSE_SUCCESS, "" }, |
| 64 }; | 81 }; |
| 65 | 82 |
| 66 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestPatterns); ++i) { | 83 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestPatterns); ++i) { |
| 67 URLPattern pattern(URLPattern::SCHEME_ALL); | 84 URLPattern pattern(URLPattern::SCHEME_ALL); |
| 68 | 85 |
| 69 // Without |strict_error_checks|, expect success. | 86 // Check results without |strict_error_checks|. |
| 70 EXPECT_EQ(URLPattern::PARSE_SUCCESS, | 87 EXPECT_EQ(kTestPatterns[i].expected_result_lenient, |
| 71 pattern.Parse(kTestPatterns[i].pattern, | 88 pattern.Parse(kTestPatterns[i].pattern, |
| 72 URLPattern::PARSE_LENIENT)) | 89 URLPattern::PARSE_LENIENT)) |
| 73 << "Got unexpected error for URL pattern: " | 90 << "Got unexpected result for URL pattern: " |
| 91 << kTestPatterns[i].pattern; |
| 92 EXPECT_EQ(kTestPatterns[i].expected_port, pattern.port()) |
| 93 << "Got unexpected port for URL pattern: " |
| 74 << kTestPatterns[i].pattern; | 94 << kTestPatterns[i].pattern; |
| 75 | 95 |
| 76 EXPECT_EQ(kTestPatterns[i].expected_result, | 96 // Check results with |strict_error_checks|. |
| 97 EXPECT_EQ(kTestPatterns[i].expected_result_strict, |
| 77 pattern.Parse(kTestPatterns[i].pattern, | 98 pattern.Parse(kTestPatterns[i].pattern, |
| 78 URLPattern::PARSE_STRICT)) | 99 URLPattern::PARSE_STRICT)) |
| 79 << "Got unexpected result for URL pattern: " | 100 << "Got unexpected result for URL pattern: " |
| 80 << kTestPatterns[i].pattern; | 101 << kTestPatterns[i].pattern; |
| 81 } | 102 } |
| 82 }; | 103 }; |
| 83 | 104 |
| 84 // all pages for a given scheme | 105 // all pages for a given scheme |
| 85 TEST(ExtensionURLPatternTest, Match1) { | 106 TEST(ExtensionURLPatternTest, Match1) { |
| 86 URLPattern pattern(kAllSchemes); | 107 URLPattern pattern(kAllSchemes); |
| (...skipping 277 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 364 { "chrome://*/*" }, | 385 { "chrome://*/*" }, |
| 365 { "chrome://newtab/" }, | 386 { "chrome://newtab/" }, |
| 366 { "about:*" }, | 387 { "about:*" }, |
| 367 { "about:blank" }, | 388 { "about:blank" }, |
| 368 { "chrome-extension://*/*" }, | 389 { "chrome-extension://*/*" }, |
| 369 { "chrome-extension://FTW/" }, | 390 { "chrome-extension://FTW/" }, |
| 370 { "data:*" }, | 391 { "data:*" }, |
| 371 { "data:monkey" }, | 392 { "data:monkey" }, |
| 372 { "javascript:*" }, | 393 { "javascript:*" }, |
| 373 { "javascript:atemyhomework" }, | 394 { "javascript:atemyhomework" }, |
| 395 { "http://www.example.com:8080/foo" }, |
| 374 }; | 396 }; |
| 375 | 397 |
| 376 TEST(ExtensionURLPatternTest, GetAsString) { | 398 TEST(ExtensionURLPatternTest, GetAsString) { |
| 377 for (size_t i = 0; i < arraysize(kGetAsStringTestCases); ++i) { | 399 for (size_t i = 0; i < arraysize(kGetAsStringTestCases); ++i) { |
| 378 URLPattern pattern(URLPattern::SCHEME_ALL); | 400 URLPattern pattern(URLPattern::SCHEME_ALL); |
| 379 EXPECT_EQ(URLPattern::PARSE_SUCCESS, | 401 EXPECT_EQ(URLPattern::PARSE_SUCCESS, |
| 380 pattern.Parse(kGetAsStringTestCases[i].pattern, | 402 pattern.Parse(kGetAsStringTestCases[i].pattern, |
| 381 URLPattern::PARSE_STRICT)); | 403 URLPattern::PARSE_LENIENT)); |
| 382 EXPECT_STREQ(kGetAsStringTestCases[i].pattern, | 404 EXPECT_STREQ(kGetAsStringTestCases[i].pattern, |
| 383 pattern.GetAsString().c_str()); | 405 pattern.GetAsString().c_str()); |
| 384 } | 406 } |
| 385 } | 407 } |
| 386 | 408 |
| 387 void TestPatternOverlap(const URLPattern& pattern1, const URLPattern& pattern2, | 409 void TestPatternOverlap(const URLPattern& pattern1, const URLPattern& pattern2, |
| 388 bool expect_overlap) { | 410 bool expect_overlap) { |
| 389 EXPECT_EQ(expect_overlap, pattern1.OverlapsWith(pattern2)) | 411 EXPECT_EQ(expect_overlap, pattern1.OverlapsWith(pattern2)) |
| 390 << pattern1.GetAsString() << ", " << pattern2.GetAsString(); | 412 << pattern1.GetAsString() << ", " << pattern2.GetAsString(); |
| 391 EXPECT_EQ(expect_overlap, pattern2.OverlapsWith(pattern1)) | 413 EXPECT_EQ(expect_overlap, pattern2.OverlapsWith(pattern1)) |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 460 EXPECT_EQ("https://*/*", all_urls[1].GetAsString()); | 482 EXPECT_EQ("https://*/*", all_urls[1].GetAsString()); |
| 461 EXPECT_EQ("file:///*", all_urls[2].GetAsString()); | 483 EXPECT_EQ("file:///*", all_urls[2].GetAsString()); |
| 462 EXPECT_EQ("ftp://*/*", all_urls[3].GetAsString()); | 484 EXPECT_EQ("ftp://*/*", all_urls[3].GetAsString()); |
| 463 EXPECT_EQ("chrome://*/*", all_urls[4].GetAsString()); | 485 EXPECT_EQ("chrome://*/*", all_urls[4].GetAsString()); |
| 464 | 486 |
| 465 EXPECT_EQ("http://google.com/foo", all_schemes[0].GetAsString()); | 487 EXPECT_EQ("http://google.com/foo", all_schemes[0].GetAsString()); |
| 466 EXPECT_EQ("https://google.com/foo", all_schemes[1].GetAsString()); | 488 EXPECT_EQ("https://google.com/foo", all_schemes[1].GetAsString()); |
| 467 | 489 |
| 468 EXPECT_EQ("http://google.com/monkey", monkey[0].GetAsString()); | 490 EXPECT_EQ("http://google.com/monkey", monkey[0].GetAsString()); |
| 469 } | 491 } |
| 492 |
| 493 TEST(ExtensionURLPatternTest, IgnorePorts) { |
| 494 URLPattern pattern1(kAllSchemes); |
| 495 EXPECT_EQ(URLPattern::PARSE_SUCCESS, |
| 496 pattern1.Parse("http://www.example.com:8080/foo", |
| 497 URLPattern::PARSE_LENIENT)); |
| 498 |
| 499 EXPECT_EQ("http://www.example.com/foo", pattern1.GetAsString()); |
| 500 EXPECT_TRUE(pattern1.MatchesURL(GURL("http://www.example.com:1234/foo"))); |
| 501 |
| 502 URLPattern pattern2(kAllSchemes); |
| 503 pattern2.set_ignores_ports(false); |
| 504 EXPECT_EQ(URLPattern::PARSE_SUCCESS, |
| 505 pattern2.Parse("http://www.example.com:8080/foo", |
| 506 URLPattern::PARSE_LENIENT)); |
| 507 |
| 508 EXPECT_EQ("http://www.example.com:8080/foo", pattern2.GetAsString()); |
| 509 EXPECT_FALSE(pattern2.MatchesURL(GURL("http://www.example.com:1234/foo"))); |
| 510 } |
| OLD | NEW |