diff options
Diffstat (limited to 'components')
-rw-r--r-- | components/BUILD.gn | 1 | ||||
-rw-r--r-- | components/OWNERS | 4 | ||||
-rw-r--r-- | components/components_tests.gyp | 6 | ||||
-rw-r--r-- | components/content_settings.gypi | 2 | ||||
-rw-r--r-- | components/content_settings/core/common/BUILD.gn | 24 | ||||
-rw-r--r-- | components/content_settings/core/common/DEPS | 2 | ||||
-rw-r--r-- | components/content_settings/core/common/content_settings.cc | 31 | ||||
-rw-r--r-- | components/content_settings/core/common/content_settings.h | 76 | ||||
-rw-r--r-- | components/content_settings/core/common/content_settings_pattern_parser_unittest.cc | 230 | ||||
-rw-r--r-- | components/content_settings/core/common/content_settings_pattern_unittest.cc | 672 | ||||
-rw-r--r-- | components/omnibox/autocomplete_input_unittest.cc | 6 | ||||
-rw-r--r-- | components/test/DEPS | 2 | ||||
-rw-r--r-- | components/test/run_all_unittests.cc | 13 |
13 files changed, 1062 insertions, 7 deletions
diff --git a/components/BUILD.gn b/components/BUILD.gn index 05f7e97..6383b23 100644 --- a/components/BUILD.gn +++ b/components/BUILD.gn @@ -207,6 +207,7 @@ test("components_unittests") { "//components/captive_portal:unit_tests", "//components/cloud_devices/common:unit_tests", "//components/component_updater:unit_tests", + "//components/content_settings/core/common:unit_tests", "//components/crx_file:unit_tests", "//components/data_reduction_proxy/browser:unit_tests", "//components/data_reduction_proxy/common:unit_tests", diff --git a/components/OWNERS b/components/OWNERS index f375616..c9663cd 100644 --- a/components/OWNERS +++ b/components/OWNERS @@ -13,6 +13,10 @@ per-file bookmarks.gypi=sky@chromium.org per-file cloud_devices*=gene@chromium.org per-file cloud_devices*=vitalybuka@chromium.org +per-file content_settings*=markusheintz@chromium.org +per-file content_settings*=bauerb@chromium.org +per-file content_settings*=jochen@chromium.org + per-file copresence.gypi=rkc@chromium.org per-file copresence.gypi=ckehoe@chromium.org per-file copresence.gypi=xiyuan@chromium.org diff --git a/components/components_tests.gyp b/components/components_tests.gyp index b8af781..e7f99ef 100644 --- a/components/components_tests.gyp +++ b/components/components_tests.gyp @@ -81,6 +81,8 @@ 'component_updater/test/crx_downloader_unittest.cc', 'component_updater/test/update_checker_unittest.cc', 'component_updater/test/update_response_unittest.cc', + 'content_settings/core/common/content_settings_pattern_parser_unittest.cc', + 'content_settings/core/common/content_settings_pattern_unittest.cc', 'crx_file/id_util_unittest.cc', 'data_reduction_proxy/browser/data_reduction_proxy_auth_request_handler_unittest.cc', 'data_reduction_proxy/browser/data_reduction_proxy_config_service_unittest.cc', @@ -297,6 +299,9 @@ 'components.gyp:component_updater_test_support', '../third_party/libxml/libxml.gyp:libxml', + # Dependencies of content_settings + 'components.gyp:content_settings_core_common', + # Dependencies of crash 'components.gyp:crash_test_support', @@ -498,6 +503,7 @@ ['include', '^bookmarks/'], ['include', '^component_updater/'], ['include', '^crash/'], + ['include', '^content_settings/'], ['include', '^data_reduction_proxy/'], ['include', '^dom_distiller/'], ['include', '^enhanced_bookmarks/'], diff --git a/components/content_settings.gypi b/components/content_settings.gypi index b24ee34..94eb5f3a 100644 --- a/components/content_settings.gypi +++ b/components/content_settings.gypi @@ -19,6 +19,8 @@ ], 'sources': [ # Note: sources list duplicated in GN build. + 'content_settings/core/common/content_settings.cc', + 'content_settings/core/common/content_settings.h', 'content_settings/core/common/content_settings_pattern.cc', 'content_settings/core/common/content_settings_pattern.h', 'content_settings/core/common/content_settings_pattern_parser.cc', diff --git a/components/content_settings/core/common/BUILD.gn b/components/content_settings/core/common/BUILD.gn index 6dfa0bb..6d3d02c 100644 --- a/components/content_settings/core/common/BUILD.gn +++ b/components/content_settings/core/common/BUILD.gn @@ -2,12 +2,34 @@ # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. -source_set("common") { +static_library("common") { sources = [ + "content_settings.cc", + "content_settings.h", "content_settings_pattern.cc", "content_settings_pattern.h", "content_settings_pattern_parser.cc", "content_settings_pattern_parser.h", "content_settings_types.h", ] + + deps = [ + "//base", + "//net", + "//url", + ] +} + +source_set("unit_tests") { + testonly = true + sources = [ + "content_settings_pattern_unittest.cc", + "content_settings_pattern_parser_unittest.cc", + ] + + deps = [ + ":common", + "//testing/gmock", + "//testing/gtest", + ] } diff --git a/components/content_settings/core/common/DEPS b/components/content_settings/core/common/DEPS index 6a2f02e..201d8c8 100644 --- a/components/content_settings/core/common/DEPS +++ b/components/content_settings/core/common/DEPS @@ -1,3 +1,5 @@ include_rules = [ "+net/base", + "+testing", + "+url", ] diff --git a/components/content_settings/core/common/content_settings.cc b/components/content_settings/core/common/content_settings.cc new file mode 100644 index 0000000..6bfbadb --- /dev/null +++ b/components/content_settings/core/common/content_settings.cc @@ -0,0 +1,31 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "components/content_settings/core/common/content_settings.h" + +ContentSetting IntToContentSetting(int content_setting) { + return ((content_setting < 0) || + (content_setting >= CONTENT_SETTING_NUM_SETTINGS)) ? + CONTENT_SETTING_DEFAULT : static_cast<ContentSetting>(content_setting); +} + +ContentSettingPatternSource::ContentSettingPatternSource( + const ContentSettingsPattern& primary_pattern, + const ContentSettingsPattern& secondary_pattern, + ContentSetting setting, + const std::string& source, + bool incognito) + : primary_pattern(primary_pattern), + secondary_pattern(secondary_pattern), + setting(setting), + source(source), + incognito(incognito) {} + +ContentSettingPatternSource::ContentSettingPatternSource() + : setting(CONTENT_SETTING_DEFAULT), incognito(false) { +} + +RendererContentSettingRules::RendererContentSettingRules() {} + +RendererContentSettingRules::~RendererContentSettingRules() {} diff --git a/components/content_settings/core/common/content_settings.h b/components/content_settings/core/common/content_settings.h new file mode 100644 index 0000000..f35e9e1 --- /dev/null +++ b/components/content_settings/core/common/content_settings.h @@ -0,0 +1,76 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef COMPONENTS_CONTENT_SETTINGS_CORE_COMMON_CONTENT_SETTINGS_H_ +#define COMPONENTS_CONTENT_SETTINGS_CORE_COMMON_CONTENT_SETTINGS_H_ + +#include <string> +#include <vector> + +#include "components/content_settings/core/common/content_settings_pattern.h" + +// Different settings that can be assigned for a particular content type. We +// give the user the ability to set these on a global and per-origin basis. +enum ContentSetting { + CONTENT_SETTING_DEFAULT = 0, + CONTENT_SETTING_ALLOW, + CONTENT_SETTING_BLOCK, + CONTENT_SETTING_ASK, + CONTENT_SETTING_SESSION_ONLY, + CONTENT_SETTING_NUM_SETTINGS +}; + +// Range-checked conversion of an int to a ContentSetting, for use when reading +// prefs off disk. +ContentSetting IntToContentSetting(int content_setting); + +struct ContentSettingPatternSource { + ContentSettingPatternSource(const ContentSettingsPattern& primary_pattern, + const ContentSettingsPattern& secondary_patttern, + ContentSetting setting, + const std::string& source, + bool incognito); + ContentSettingPatternSource(); + ContentSettingsPattern primary_pattern; + ContentSettingsPattern secondary_pattern; + ContentSetting setting; + std::string source; + bool incognito; +}; + +typedef std::vector<ContentSettingPatternSource> ContentSettingsForOneType; + +struct RendererContentSettingRules { + RendererContentSettingRules(); + ~RendererContentSettingRules(); + ContentSettingsForOneType image_rules; + ContentSettingsForOneType script_rules; +}; + +namespace content_settings { + +// Enum containing the various source for content settings. Settings can be +// set by policy, extension or the user. Certain (internal) schemes are +// whilelisted. For whilelisted schemes the source is +// |SETTING_SOURCE_WHITELIST|. +enum SettingSource { + SETTING_SOURCE_NONE, + SETTING_SOURCE_POLICY, + SETTING_SOURCE_EXTENSION, + SETTING_SOURCE_USER, + SETTING_SOURCE_WHITELIST, +}; + +// |SettingInfo| provides meta data for content setting values. |source| +// contains the source of a value. |primary_pattern| and |secondary_pattern| +// contains the patterns of the appling rule. +struct SettingInfo { + SettingSource source; + ContentSettingsPattern primary_pattern; + ContentSettingsPattern secondary_pattern; +}; + +} // namespace content_settings + +#endif // COMPONENTS_CONTENT_SETTINGS_CORE_COMMON_CONTENT_SETTINGS_H_ diff --git a/components/content_settings/core/common/content_settings_pattern_parser_unittest.cc b/components/content_settings/core/common/content_settings_pattern_parser_unittest.cc new file mode 100644 index 0000000..b35b700 --- /dev/null +++ b/components/content_settings/core/common/content_settings_pattern_parser_unittest.cc @@ -0,0 +1,230 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "components/content_settings/core/common/content_settings_pattern.h" +#include "components/content_settings/core/common/content_settings_pattern_parser.h" + +#include "testing/gmock/include/gmock/gmock.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace { +typedef ContentSettingsPattern::BuilderInterface BuilderInterface; +} // namespace + +class MockBuilder : public ContentSettingsPattern::BuilderInterface { + public: + MOCK_METHOD0(WithSchemeWildcard, BuilderInterface*()); + MOCK_METHOD0(WithDomainWildcard, BuilderInterface*()); + MOCK_METHOD0(WithPortWildcard, BuilderInterface*()); + MOCK_METHOD1(WithScheme, BuilderInterface*(const std::string& scheme)); + MOCK_METHOD1(WithHost, BuilderInterface*(const std::string& host)); + MOCK_METHOD1(WithPort, BuilderInterface*(const std::string& port)); + MOCK_METHOD1(WithPath, BuilderInterface*(const std::string& path)); + MOCK_METHOD0(WithPathWildcard, BuilderInterface*()); + MOCK_METHOD0(Invalid, BuilderInterface*()); + MOCK_METHOD0(Build, ContentSettingsPattern()); +}; + +TEST(ContentSettingsPatternParserTest, ParsePatterns) { + // Test valid patterns + ::testing::StrictMock<MockBuilder> builder; + + // WithPathWildcard() is not called for "*". (Need a strict Mock for this + // case.) + EXPECT_CALL(builder, WithSchemeWildcard()).Times(1).WillOnce( + ::testing::Return(&builder)); + EXPECT_CALL(builder, WithDomainWildcard()).Times(1).WillOnce( + ::testing::Return(&builder)); + EXPECT_CALL(builder, WithPortWildcard()).Times(1).WillOnce( + ::testing::Return(&builder)); + content_settings::PatternParser::Parse("*", &builder); + ::testing::Mock::VerifyAndClear(&builder); + + EXPECT_CALL(builder, WithScheme("http")).Times(1).WillOnce( + ::testing::Return(&builder)); + EXPECT_CALL(builder, WithHost("www.youtube.com")).Times(1).WillOnce( + ::testing::Return(&builder)); + EXPECT_CALL(builder, WithPort("8080")).Times(1).WillOnce( + ::testing::Return(&builder)); + content_settings::PatternParser::Parse( + "http://www.youtube.com:8080", &builder); + ::testing::Mock::VerifyAndClear(&builder); + + EXPECT_CALL(builder, WithSchemeWildcard()).Times(1).WillOnce( + ::testing::Return(&builder)); + EXPECT_CALL(builder, WithHost("www.gmail.com")).Times(1).WillOnce( + ::testing::Return(&builder)); + EXPECT_CALL(builder, WithPort("80")).Times(1).WillOnce( + ::testing::Return(&builder)); + content_settings::PatternParser::Parse("*://www.gmail.com:80", &builder); + ::testing::Mock::VerifyAndClear(&builder); + + EXPECT_CALL(builder, WithScheme("http")).Times(1).WillOnce( + ::testing::Return(&builder)); + EXPECT_CALL(builder, WithHost("www.gmail.com")).Times(1).WillOnce( + ::testing::Return(&builder)); + EXPECT_CALL(builder, WithPortWildcard()).Times(1).WillOnce( + ::testing::Return(&builder)); + content_settings::PatternParser::Parse("http://www.gmail.com:*", &builder); + ::testing::Mock::VerifyAndClear(&builder); + + EXPECT_CALL(builder, WithScheme("http")).Times(1).WillOnce( + ::testing::Return(&builder)); + EXPECT_CALL(builder, WithDomainWildcard()).WillOnce( + ::testing::Return(&builder)); + EXPECT_CALL(builder, WithHost("google.com")).Times(1).WillOnce( + ::testing::Return(&builder)); + EXPECT_CALL(builder, WithPort("80")).Times(1).WillOnce( + ::testing::Return(&builder)); + content_settings::PatternParser::Parse("http://[*.]google.com:80", &builder); + ::testing::Mock::VerifyAndClear(&builder); + + EXPECT_CALL(builder, WithScheme("https")).Times(1).WillOnce( + ::testing::Return(&builder)); + EXPECT_CALL(builder, WithHost("[::1]")).Times(1).WillOnce( + ::testing::Return(&builder)); + EXPECT_CALL(builder, WithPort("8080")).Times(1).WillOnce( + ::testing::Return(&builder)); + content_settings::PatternParser::Parse("https://[::1]:8080", &builder); + ::testing::Mock::VerifyAndClear(&builder); + + EXPECT_CALL(builder, WithScheme("http")).Times(1).WillOnce( + ::testing::Return(&builder)); + EXPECT_CALL(builder, WithHost("127.0.0.1")).Times(1).WillOnce( + ::testing::Return(&builder)); + EXPECT_CALL(builder, WithPort("8080")).Times(1).WillOnce( + ::testing::Return(&builder)); + content_settings::PatternParser::Parse("http://127.0.0.1:8080", &builder); + ::testing::Mock::VerifyAndClear(&builder); + + // Test valid pattern short forms + EXPECT_CALL(builder, WithSchemeWildcard()).Times(1).WillOnce( + ::testing::Return(&builder)); + EXPECT_CALL(builder, WithHost("www.youtube.com")).Times(1).WillOnce( + ::testing::Return(&builder)); + EXPECT_CALL(builder, WithPort("8080")).Times(1).WillOnce( + ::testing::Return(&builder)); + content_settings::PatternParser::Parse("www.youtube.com:8080", &builder); + ::testing::Mock::VerifyAndClear(&builder); + + EXPECT_CALL(builder, WithSchemeWildcard()).Times(1).WillOnce( + ::testing::Return(&builder)); + EXPECT_CALL(builder, WithHost("www.youtube.com")).Times(1).WillOnce( + ::testing::Return(&builder)); + EXPECT_CALL(builder, WithPortWildcard()).Times(1).WillOnce( + ::testing::Return(&builder)); + content_settings::PatternParser::Parse("www.youtube.com", &builder); + ::testing::Mock::VerifyAndClear(&builder); + + EXPECT_CALL(builder, WithSchemeWildcard()).Times(1).WillOnce( + ::testing::Return(&builder)); + EXPECT_CALL(builder, WithDomainWildcard()).Times(1).WillOnce( + ::testing::Return(&builder)); + EXPECT_CALL(builder, WithHost("youtube.com")).Times(1).WillOnce( + ::testing::Return(&builder)); + EXPECT_CALL(builder, WithPortWildcard()).Times(1).WillOnce( + ::testing::Return(&builder)); + content_settings::PatternParser::Parse("[*.]youtube.com", &builder); + ::testing::Mock::VerifyAndClear(&builder); + + // Test invalid patterns + EXPECT_CALL(builder, WithSchemeWildcard()).Times(1).WillOnce( + ::testing::Return(&builder)); + EXPECT_CALL(builder, Invalid()).Times(1).WillOnce( + ::testing::Return(&builder)); + content_settings::PatternParser::Parse("*youtube.com", &builder); + ::testing::Mock::VerifyAndClear(&builder); + + EXPECT_CALL(builder, WithSchemeWildcard()).Times(1).WillOnce( + ::testing::Return(&builder)); + EXPECT_CALL(builder, Invalid()).Times(1).WillOnce( + ::testing::Return(&builder)); + content_settings::PatternParser::Parse("*.youtube.com", &builder); + ::testing::Mock::VerifyAndClear(&builder); + + EXPECT_CALL(builder, WithSchemeWildcard()).Times(1).WillOnce( + ::testing::Return(&builder)); + EXPECT_CALL(builder, Invalid()).Times(1).WillOnce( + ::testing::Return(&builder)); + content_settings::PatternParser::Parse("www.youtube.com*", &builder); + ::testing::Mock::VerifyAndClear(&builder); +} + +TEST(ContentSettingsPatternParserTest, ParseFilePatterns) { + ::testing::StrictMock<MockBuilder> builder; + + EXPECT_CALL(builder, WithScheme("file")).Times(1).WillOnce( + ::testing::Return(&builder)); + EXPECT_CALL(builder, WithPath("/foo/bar/test.html")).Times(1).WillOnce( + ::testing::Return(&builder)); + content_settings::PatternParser::Parse( + "file:///foo/bar/test.html", &builder); + ::testing::Mock::VerifyAndClear(&builder); + + EXPECT_CALL(builder, WithScheme("file")).Times(1).WillOnce( + ::testing::Return(&builder)); + EXPECT_CALL(builder, WithDomainWildcard()).Times(1).WillOnce( + ::testing::Return(&builder)); + content_settings::PatternParser::Parse( + "file://*", &builder); + ::testing::Mock::VerifyAndClear(&builder); + + EXPECT_CALL(builder, WithScheme("file")).Times(1).WillOnce( + ::testing::Return(&builder)); + EXPECT_CALL(builder, WithDomainWildcard()).Times(1).WillOnce( + ::testing::Return(&builder)); + EXPECT_CALL(builder, WithPath("/")).Times(1).WillOnce( + ::testing::Return(&builder)); + content_settings::PatternParser::Parse( + "file://*/", &builder); + ::testing::Mock::VerifyAndClear(&builder); + + EXPECT_CALL(builder, WithScheme("file")).Times(1).WillOnce( + ::testing::Return(&builder)); + EXPECT_CALL(builder, WithDomainWildcard()).Times(1).WillOnce( + ::testing::Return(&builder)); + EXPECT_CALL(builder, WithPathWildcard()).Times(1).WillOnce( + ::testing::Return(&builder)); + content_settings::PatternParser::Parse( + "file://*/*", &builder); + ::testing::Mock::VerifyAndClear(&builder); + + EXPECT_CALL(builder, WithScheme("file")).Times(1).WillOnce( + ::testing::Return(&builder)); + EXPECT_CALL(builder, WithPathWildcard()).Times(1).WillOnce( + ::testing::Return(&builder)); + content_settings::PatternParser::Parse( + "file:///*", &builder); + ::testing::Mock::VerifyAndClear(&builder); + + // Invalid file patterns. + EXPECT_CALL(builder, WithScheme("file")).Times(1).WillOnce( + ::testing::Return(&builder)); + EXPECT_CALL(builder, Invalid()).Times(1).WillOnce( + ::testing::Return(&builder)); + content_settings::PatternParser::Parse( + "file://**", &builder); + ::testing::Mock::VerifyAndClear(&builder); +} + +TEST(ContentSettingsPatternParserTest, SerializePatterns) { + ContentSettingsPattern::PatternParts parts; + parts.scheme = "http"; + parts.host = "www.youtube.com"; + parts.port = "8080"; + EXPECT_STREQ("http://www.youtube.com:8080", + content_settings::PatternParser::ToString(parts).c_str()); + + parts = ContentSettingsPattern::PatternParts(); + parts.scheme = "file"; + parts.path = "/foo/bar/test.html"; + EXPECT_STREQ("file:///foo/bar/test.html", + content_settings::PatternParser::ToString(parts).c_str()); + + parts = ContentSettingsPattern::PatternParts(); + parts.scheme = "file"; + parts.path = ""; + parts.is_path_wildcard = true; + EXPECT_EQ("file:///*", content_settings::PatternParser::ToString(parts)); +} diff --git a/components/content_settings/core/common/content_settings_pattern_unittest.cc b/components/content_settings/core/common/content_settings_pattern_unittest.cc new file mode 100644 index 0000000..3688d84 --- /dev/null +++ b/components/content_settings/core/common/content_settings_pattern_unittest.cc @@ -0,0 +1,672 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "components/content_settings/core/common/content_settings_pattern.h" + +#include "testing/gtest/include/gtest/gtest.h" +#include "url/gurl.h" + +namespace { + +ContentSettingsPattern Pattern(const std::string& str) { + return ContentSettingsPattern::FromString(str); +} + +} // namespace + +TEST(ContentSettingsPatternTest, RealWorldPatterns) { + // This is the place for real world patterns that unveiled bugs. + EXPECT_STREQ("[*.]ikea.com", + Pattern("[*.]ikea.com").ToString().c_str()); +} + +TEST(ContentSettingsPatternTest, GURL) { + // Document and verify GURL behavior. + GURL url("http://mail.google.com:80"); + EXPECT_EQ(-1, url.IntPort()); + EXPECT_EQ("", url.port()); + + url = GURL("http://mail.google.com"); + EXPECT_EQ(-1, url.IntPort()); + EXPECT_EQ("", url.port()); + + url = GURL("https://mail.google.com:443"); + EXPECT_EQ(-1, url.IntPort()); + EXPECT_EQ("", url.port()); + + url = GURL("https://mail.google.com"); + EXPECT_EQ(-1, url.IntPort()); + EXPECT_EQ("", url.port()); + + url = GURL("http://mail.google.com"); + EXPECT_EQ(-1, url.IntPort()); + EXPECT_EQ("", url.port()); +} + +TEST(ContentSettingsPatternTest, FromURL) { + // NOTICE: When content settings pattern are created from a GURL the following + // happens: + // - If the GURL scheme is "http" the scheme wildcard is used. Otherwise the + // GURL scheme is used. + // - A domain wildcard is added to the GURL host. + // - A port wildcard is used instead of the schemes default port. + // In case of non-default ports the specific GURL port is used. + ContentSettingsPattern pattern = ContentSettingsPattern::FromURL( + GURL("http://www.youtube.com")); + EXPECT_TRUE(pattern.IsValid()); + EXPECT_STREQ("[*.]www.youtube.com", pattern.ToString().c_str()); + + // Patterns created from a URL. + pattern = ContentSettingsPattern::FromURL(GURL("http://www.google.com")); + EXPECT_TRUE(pattern.Matches(GURL("http://www.google.com"))); + EXPECT_TRUE(pattern.Matches(GURL("http://foo.www.google.com"))); + EXPECT_TRUE(pattern.Matches(GURL("http://www.google.com:80"))); + EXPECT_TRUE(pattern.Matches(GURL("http://www.google.com:81"))); + EXPECT_FALSE(pattern.Matches(GURL("https://mail.google.com"))); + EXPECT_TRUE(pattern.Matches(GURL("https://www.google.com"))); + + pattern = ContentSettingsPattern::FromURL(GURL("http://www.google.com:80")); + EXPECT_TRUE(pattern.Matches(GURL("http://www.google.com"))); + EXPECT_TRUE(pattern.Matches(GURL("http://www.google.com:80"))); + EXPECT_TRUE(pattern.Matches(GURL("http://www.google.com:81"))); + + pattern = ContentSettingsPattern::FromURL(GURL("https://www.google.com:443")); + EXPECT_TRUE(pattern.Matches(GURL("https://www.google.com"))); + EXPECT_TRUE(pattern.Matches(GURL("https://foo.www.google.com"))); + EXPECT_TRUE(pattern.Matches(GURL("https://www.google.com:443"))); + EXPECT_FALSE(pattern.Matches(GURL("https://www.google.com:444"))); + EXPECT_FALSE(pattern.Matches(GURL("http://www.google.com:443"))); + + pattern = ContentSettingsPattern::FromURL(GURL("https://127.0.0.1")); + EXPECT_TRUE(pattern.IsValid()); + EXPECT_STREQ("https://127.0.0.1:443", pattern.ToString().c_str()); + + pattern = ContentSettingsPattern::FromURL(GURL("http://[::1]")); + EXPECT_TRUE(pattern.IsValid()); + + pattern = ContentSettingsPattern::FromURL(GURL("file:///foo/bar.html")); + EXPECT_TRUE(pattern.IsValid()); + EXPECT_EQ("file:///foo/bar.html", pattern.ToString()); +} + +TEST(ContentSettingsPatternTest, FilesystemUrls) { + ContentSettingsPattern pattern = + ContentSettingsPattern::FromURL(GURL("http://www.google.com")); + EXPECT_TRUE(pattern.Matches( + GURL("filesystem:http://www.google.com/temporary/"))); + EXPECT_TRUE(pattern.Matches( + GURL("filesystem:http://foo.www.google.com/temporary/"))); + EXPECT_TRUE(pattern.Matches( + GURL("filesystem:http://www.google.com:80/temporary/"))); + EXPECT_TRUE(pattern.Matches( + GURL("filesystem:http://www.google.com:81/temporary/"))); + + pattern = ContentSettingsPattern::FromURL(GURL("https://www.google.com")); + EXPECT_TRUE(pattern.Matches( + GURL("filesystem:https://www.google.com/temporary/"))); + EXPECT_TRUE(pattern.Matches( + GURL("filesystem:https://www.google.com:443/temporary/"))); + EXPECT_TRUE(pattern.Matches( + GURL("filesystem:https://foo.www.google.com/temporary/"))); + EXPECT_FALSE(pattern.Matches( + GURL("filesystem:https://www.google.com:81/temporary/"))); + + // A pattern from a filesystem URLs is equivalent to a pattern from the inner + // URL of the filesystem URL. + ContentSettingsPattern pattern2 = ContentSettingsPattern::FromURL( + GURL("filesystem:https://www.google.com/temporary/")); + EXPECT_EQ(ContentSettingsPattern::IDENTITY, pattern.Compare(pattern2)); + + EXPECT_STREQ("https://[*.]www.google.com:443", pattern2.ToString().c_str()); + + pattern = + ContentSettingsPattern::FromURL( + GURL("filesystem:file:///temporary/foo/bar")); + EXPECT_TRUE(pattern.Matches(GURL("filesystem:file:///temporary/"))); + EXPECT_TRUE(pattern.Matches(GURL("filesystem:file:///temporary/test.txt"))); + EXPECT_TRUE(pattern.Matches(GURL("file:///temporary"))); + EXPECT_FALSE(pattern.Matches(GURL("file://foo/bar"))); + pattern2 = + ContentSettingsPattern::FromURL( + GURL("filesystem:file:///persistent/foo2/bar2")); + EXPECT_EQ(ContentSettingsPattern::IDENTITY, pattern.Compare(pattern2)); +} + +TEST(ContentSettingsPatternTest, FromURLNoWildcard) { + // If no port is specifed GURLs always use the default port for the schemes + // HTTP and HTTPS. Hence a GURL always carries a port specification either + // explicitly or implicitly. Therefore if a content settings pattern is + // created from a GURL with no wildcard, specific values are used for the + // scheme, host and port part of the pattern. + // Creating content settings patterns from strings behaves different. Pattern + // parts that are omitted in pattern specifications (strings), are completed + // with a wildcard. + ContentSettingsPattern pattern = ContentSettingsPattern::FromURLNoWildcard( + GURL("http://www.example.com")); + EXPECT_TRUE(pattern.IsValid()); + EXPECT_STREQ("http://www.example.com:80", pattern.ToString().c_str()); + EXPECT_TRUE(pattern.Matches(GURL("http://www.example.com"))); + EXPECT_FALSE(pattern.Matches(GURL("https://www.example.com"))); + EXPECT_FALSE(pattern.Matches(GURL("http://foo.www.example.com"))); + + pattern = ContentSettingsPattern::FromURLNoWildcard( + GURL("https://www.example.com")); + EXPECT_TRUE(pattern.IsValid()); + EXPECT_STREQ("https://www.example.com:443", pattern.ToString().c_str()); + EXPECT_FALSE(pattern.Matches(GURL("http://www.example.com"))); + EXPECT_TRUE(pattern.Matches(GURL("https://www.example.com"))); + EXPECT_FALSE(pattern.Matches(GURL("http://foo.www.example.com"))); + + // Pattern for filesystem URLs + pattern = + ContentSettingsPattern::FromURLNoWildcard( + GURL("filesystem:http://www.google.com/temporary/")); + EXPECT_TRUE(pattern.IsValid()); + EXPECT_TRUE(pattern.Matches(GURL("http://www.google.com"))); + EXPECT_FALSE(pattern.Matches(GURL("http://foo.www.google.com"))); + EXPECT_TRUE(pattern.Matches( + GURL("filesystem:http://www.google.com/persistent/"))); + EXPECT_FALSE(pattern.Matches( + GURL("filesystem:https://www.google.com/persistent/"))); + EXPECT_FALSE(pattern.Matches( + GURL("filesystem:https://www.google.com:81/temporary/"))); + EXPECT_FALSE(pattern.Matches( + GURL("filesystem:https://foo.www.google.com/temporary/"))); +} + +TEST(ContentSettingsPatternTest, Wildcard) { + EXPECT_TRUE(ContentSettingsPattern::Wildcard().IsValid()); + + EXPECT_TRUE(ContentSettingsPattern::Wildcard().Matches( + GURL("http://www.google.com"))); + EXPECT_TRUE(ContentSettingsPattern::Wildcard().Matches( + GURL("https://www.google.com"))); + EXPECT_TRUE(ContentSettingsPattern::Wildcard().Matches( + GURL("https://myhost:8080"))); + EXPECT_TRUE(ContentSettingsPattern::Wildcard().Matches( + GURL("file:///foo/bar.txt"))); + + EXPECT_STREQ("*", ContentSettingsPattern::Wildcard().ToString().c_str()); + + EXPECT_EQ(ContentSettingsPattern::IDENTITY, + ContentSettingsPattern::Wildcard().Compare( + ContentSettingsPattern::Wildcard())); +} + +TEST(ContentSettingsPatternTest, TrimEndingDotFromHost) { + EXPECT_TRUE(Pattern("www.example.com").IsValid()); + EXPECT_TRUE(Pattern("www.example.com").Matches( + GURL("http://www.example.com"))); + EXPECT_TRUE(Pattern("www.example.com").Matches( + GURL("http://www.example.com."))); + + EXPECT_TRUE(Pattern("www.example.com.").IsValid()); + EXPECT_STREQ("www.example.com", + Pattern("www.example.com.").ToString().c_str()); + + EXPECT_TRUE(Pattern("www.example.com.") == Pattern("www.example.com")); + + EXPECT_TRUE(Pattern(".").IsValid()); + EXPECT_STREQ(".", Pattern(".").ToString().c_str()); +} + +TEST(ContentSettingsPatternTest, FromString_WithNoWildcards) { + // HTTP patterns with default port. + EXPECT_TRUE(Pattern("http://www.example.com:80").IsValid()); + EXPECT_STREQ("http://www.example.com:80", + Pattern("http://www.example.com:80").ToString().c_str()); + // HTTP patterns with none default port. + EXPECT_TRUE(Pattern("http://www.example.com:81").IsValid()); + EXPECT_STREQ("http://www.example.com:81", + Pattern("http://www.example.com:81").ToString().c_str()); + + // HTTPS patterns with default port. + EXPECT_TRUE(Pattern("https://www.example.com:443").IsValid()); + EXPECT_STREQ("https://www.example.com:443", + Pattern("https://www.example.com:443").ToString().c_str()); + // HTTPS patterns with none default port. + EXPECT_TRUE(Pattern("https://www.example.com:8080").IsValid()); + EXPECT_STREQ("https://www.example.com:8080", + Pattern("https://www.example.com:8080").ToString().c_str()); +} + +TEST(ContentSettingsPatternTest, FromString_FilePatterns) { + // "/" is an invalid file path. + EXPECT_FALSE(Pattern("file:///").IsValid()); + + // Non-empty domains aren't allowed in file patterns. + EXPECT_FALSE(Pattern("file://foo/").IsValid()); + EXPECT_FALSE(Pattern("file://localhost/foo/bar/test.html").IsValid()); + EXPECT_FALSE(Pattern("file://*").IsValid()); + EXPECT_FALSE(Pattern("file://*/").IsValid()); + EXPECT_FALSE(Pattern("file://*/*").IsValid()); + EXPECT_FALSE(Pattern("file://*/foo/bar/test.html").IsValid()); + EXPECT_FALSE(Pattern("file://[*.]/").IsValid()); + + // This is the only valid file path wildcard format. + EXPECT_TRUE(Pattern("file:///*").IsValid()); + EXPECT_EQ("file:///*", Pattern("file:///*").ToString()); + + // Wildcards are not allowed anywhere in the file path. + EXPECT_FALSE(Pattern("file:///f*o/bar/file.html").IsValid()); + EXPECT_FALSE(Pattern("file:///*/bar/file.html").IsValid()); + EXPECT_FALSE(Pattern("file:///foo/*").IsValid()); + EXPECT_FALSE(Pattern("file:///foo/bar/*").IsValid()); + EXPECT_FALSE(Pattern("file:///foo/*/file.html").IsValid()); + EXPECT_FALSE(Pattern("file:///foo/bar/*.html").IsValid()); + EXPECT_FALSE(Pattern("file:///foo/bar/file.*").IsValid()); + + EXPECT_TRUE(Pattern("file:///tmp/test.html").IsValid()); + EXPECT_EQ("file:///tmp/file.html", + Pattern("file:///tmp/file.html").ToString()); + EXPECT_TRUE(Pattern("file:///tmp/test.html").Matches( + GURL("file:///tmp/test.html"))); + EXPECT_FALSE(Pattern("file:///tmp/test.html").Matches( + GURL("file:///tmp/other.html"))); + EXPECT_FALSE(Pattern("file:///tmp/test.html").Matches( + GURL("http://example.org/"))); + + EXPECT_TRUE(Pattern("file:///*").Matches(GURL("file:///tmp/test.html"))); + EXPECT_TRUE(Pattern("file:///*").Matches( + GURL("file://localhost/tmp/test.html"))); +} + +TEST(ContentSettingsPatternTest, FromString_ExtensionPatterns) { + EXPECT_TRUE(Pattern("chrome-extension://peoadpeiejnhkmpaakpnompolbglelel/") + .IsValid()); + EXPECT_EQ("chrome-extension://peoadpeiejnhkmpaakpnompolbglelel/", + Pattern("chrome-extension://peoadpeiejnhkmpaakpnompolbglelel/") + .ToString()); + EXPECT_TRUE(Pattern("chrome-extension://peoadpeiejnhkmpaakpnompolbglelel/") + .Matches(GURL("chrome-extension://peoadpeiejnhkmpaakpnompolbglelel/"))); +} + +TEST(ContentSettingsPatternTest, FromString_WithIPAdresses) { + // IPv4 + EXPECT_TRUE(Pattern("192.168.0.1").IsValid()); + EXPECT_STREQ("192.168.1.1", Pattern("192.168.1.1").ToString().c_str()); + EXPECT_TRUE(Pattern("https://192.168.0.1:8080").IsValid()); + EXPECT_STREQ("https://192.168.0.1:8080", + Pattern("https://192.168.0.1:8080").ToString().c_str()); + + // Subdomain wildcards should be only valid for hosts, not for IP addresses. + EXPECT_FALSE(Pattern("[*.]127.0.0.1").IsValid()); + + // IPv6 + EXPECT_TRUE(Pattern("[::1]").IsValid()); + EXPECT_STREQ("[::1]", Pattern("[::1]").ToString().c_str()); + EXPECT_TRUE(Pattern("https://[::1]:8080").IsValid()); + EXPECT_STREQ("https://[::1]:8080", + Pattern("https://[::1]:8080").ToString().c_str()); +} + +TEST(ContentSettingsPatternTest, FromString_WithWildcards) { + // Creating content settings patterns from strings completes pattern parts + // that are omitted in pattern specifications (strings) with a wildcard. + + // The wildcard pattern. + EXPECT_TRUE(Pattern("*").IsValid()); + EXPECT_STREQ("*", Pattern("*").ToString().c_str()); + EXPECT_EQ(ContentSettingsPattern::IDENTITY, + Pattern("*").Compare(ContentSettingsPattern::Wildcard())); + + // Patterns with port wildcard. + EXPECT_TRUE(Pattern("http://example.com:*").IsValid()); + EXPECT_STREQ("http://example.com", + Pattern("http://example.com:*").ToString().c_str()); + + EXPECT_TRUE(Pattern("https://example.com").IsValid()); + EXPECT_STREQ("https://example.com", + Pattern("https://example.com").ToString().c_str()); + + EXPECT_TRUE(Pattern("*://www.google.com.com:8080").IsValid()); + EXPECT_STREQ("www.google.com:8080", + Pattern("*://www.google.com:8080").ToString().c_str()); + EXPECT_TRUE(Pattern("*://www.google.com:8080").Matches( + GURL("http://www.google.com:8080"))); + EXPECT_TRUE(Pattern("*://www.google.com:8080").Matches( + GURL("https://www.google.com:8080"))); + EXPECT_FALSE( + Pattern("*://www.google.com").Matches(GURL("file:///foo/bar.html"))); + + EXPECT_TRUE(Pattern("www.example.com:8080").IsValid()); + + // Patterns with port and scheme wildcard. + EXPECT_TRUE(Pattern("*://www.example.com:*").IsValid()); + EXPECT_STREQ("www.example.com", + Pattern("*://www.example.com:*").ToString().c_str()); + + EXPECT_TRUE(Pattern("*://www.example.com").IsValid()); + EXPECT_STREQ("www.example.com", + Pattern("*://www.example.com").ToString().c_str()); + + EXPECT_TRUE(Pattern("www.example.com:*").IsValid()); + EXPECT_STREQ("www.example.com", + Pattern("www.example.com:*").ToString().c_str()); + + EXPECT_TRUE(Pattern("www.example.com").IsValid()); + EXPECT_STREQ("www.example.com", + Pattern("www.example.com").ToString().c_str()); + EXPECT_TRUE(Pattern("www.example.com").Matches( + GURL("http://www.example.com/"))); + EXPECT_FALSE(Pattern("example.com").Matches( + GURL("http://example.org/"))); + + // Patterns with domain wildcard. + EXPECT_TRUE(Pattern("[*.]example.com").IsValid()); + EXPECT_STREQ("[*.]example.com", + Pattern("[*.]example.com").ToString().c_str()); + EXPECT_TRUE(Pattern("[*.]example.com").Matches( + GURL("http://example.com/"))); + EXPECT_TRUE(Pattern("[*.]example.com").Matches( + GURL("http://foo.example.com/"))); + EXPECT_FALSE(Pattern("[*.]example.com").Matches( + GURL("http://example.org/"))); + + EXPECT_TRUE(Pattern("[*.]google.com:80").Matches( + GURL("http://mail.google.com:80"))); + EXPECT_FALSE(Pattern("[*.]google.com:80").Matches( + GURL("http://mail.google.com:81"))); + EXPECT_TRUE(Pattern("[*.]google.com:80").Matches( + GURL("http://www.google.com"))); + + EXPECT_TRUE(Pattern("[*.]google.com:8080").Matches( + GURL("http://mail.google.com:8080"))); + + EXPECT_TRUE(Pattern("[*.]google.com:443").Matches( + GURL("https://mail.google.com:443"))); + EXPECT_TRUE(Pattern("[*.]google.com:443").Matches( + GURL("https://www.google.com"))); + + EXPECT_TRUE(Pattern("[*.]google.com:4321").Matches( + GURL("https://mail.google.com:4321"))); + EXPECT_TRUE(Pattern("[*.]example.com").Matches( + GURL("http://example.com/"))); + EXPECT_TRUE(Pattern("[*.]example.com").Matches( + GURL("http://www.example.com/"))); + + // Patterns with host wildcard + EXPECT_TRUE(Pattern("[*.]").IsValid()); + EXPECT_TRUE(Pattern("http://*").IsValid()); + EXPECT_TRUE(Pattern("http://[*.]").IsValid()); + EXPECT_EQ(std::string("http://*"), Pattern("http://[*.]").ToString()); + EXPECT_TRUE(Pattern("http://*:8080").IsValid()); + EXPECT_TRUE(Pattern("*://*").IsValid()); + EXPECT_STREQ("*", Pattern("*://*").ToString().c_str()); +} + +TEST(ContentSettingsPatternTest, FromString_Canonicalized) { + // UTF-8 patterns. + EXPECT_TRUE(Pattern("[*.]\xC4\x87ira.com").IsValid()); + EXPECT_STREQ("[*.]xn--ira-ppa.com", + Pattern("[*.]\xC4\x87ira.com").ToString().c_str()); + EXPECT_TRUE(Pattern("\xC4\x87ira.com").IsValid()); + EXPECT_STREQ("xn--ira-ppa.com", + Pattern("\xC4\x87ira.com").ToString().c_str()); + EXPECT_TRUE(Pattern("file:///\xC4\x87ira.html").IsValid()); + EXPECT_STREQ("file:///%C4%87ira.html", + Pattern("file:///\xC4\x87ira.html").ToString().c_str()); + + // File path normalization. + EXPECT_TRUE(Pattern("file:///tmp/bar/../test.html").IsValid()); + EXPECT_STREQ("file:///tmp/test.html", + Pattern("file:///tmp/bar/../test.html").ToString().c_str()); +} + +TEST(ContentSettingsPatternTest, InvalidPatterns) { + // StubObserver expects an empty pattern top be returned as empty string. + EXPECT_FALSE(ContentSettingsPattern().IsValid()); + EXPECT_STREQ("", ContentSettingsPattern().ToString().c_str()); + + // Empty pattern string + EXPECT_FALSE(Pattern(std::string()).IsValid()); + EXPECT_STREQ("", Pattern(std::string()).ToString().c_str()); + + // Pattern strings with invalid scheme part. + EXPECT_FALSE(Pattern("ftp://myhost.org").IsValid()); + EXPECT_STREQ("", Pattern("ftp://myhost.org").ToString().c_str()); + + // Pattern strings with invalid host part. + EXPECT_FALSE(Pattern("*example.com").IsValid()); + EXPECT_STREQ("", Pattern("*example.com").ToString().c_str()); + EXPECT_FALSE(Pattern("example.*").IsValid()); + EXPECT_STREQ("", Pattern("example.*").ToString().c_str()); + EXPECT_FALSE(Pattern("*\xC4\x87ira.com").IsValid()); + EXPECT_STREQ("", Pattern("*\xC4\x87ira.com").ToString().c_str()); + EXPECT_FALSE(Pattern("\xC4\x87ira.*").IsValid()); + EXPECT_STREQ("", Pattern("\xC4\x87ira.*").ToString().c_str()); + + // Pattern strings with invalid port parts. + EXPECT_FALSE(Pattern("example.com:abc").IsValid()); + EXPECT_STREQ("", Pattern("example.com:abc").ToString().c_str()); + + // Invalid file pattern strings. + EXPECT_FALSE(Pattern("file://").IsValid()); + EXPECT_STREQ("", Pattern("file://").ToString().c_str()); + EXPECT_FALSE(Pattern("file:///foo/bar.html:8080").IsValid()); + EXPECT_STREQ("", Pattern("file:///foo/bar.html:8080").ToString().c_str()); + + // Host having multiple ending dots. + EXPECT_FALSE(Pattern("www.example.com..").IsValid()); + EXPECT_STREQ("", Pattern("www.example.com..").ToString().c_str()); +} + +TEST(ContentSettingsPatternTest, UnequalOperator) { + EXPECT_TRUE(Pattern("http://www.foo.com") != Pattern("http://www.foo.com*")); + EXPECT_TRUE(Pattern("http://www.foo.com*") != + ContentSettingsPattern::Wildcard()); + + EXPECT_TRUE(Pattern("http://www.foo.com") != + ContentSettingsPattern::Wildcard()); + + EXPECT_TRUE(Pattern("http://www.foo.com") != Pattern("www.foo.com")); + EXPECT_TRUE(Pattern("http://www.foo.com") != + Pattern("http://www.foo.com:80")); + + EXPECT_FALSE(Pattern("http://www.foo.com") != Pattern("http://www.foo.com")); + EXPECT_TRUE(Pattern("http://www.foo.com") == Pattern("http://www.foo.com")); +} + +TEST(ContentSettingsPatternTest, Compare) { + // Test identical patterns patterns. + ContentSettingsPattern pattern1 = + Pattern("http://www.google.com"); + EXPECT_EQ(ContentSettingsPattern::IDENTITY, pattern1.Compare(pattern1)); + EXPECT_EQ(ContentSettingsPattern::IDENTITY, + Pattern("http://www.google.com:80").Compare( + Pattern("http://www.google.com:80"))); + EXPECT_EQ(ContentSettingsPattern::IDENTITY, + Pattern("*://[*.]google.com:*").Compare( + Pattern("*://[*.]google.com:*"))); + + ContentSettingsPattern invalid_pattern1; + ContentSettingsPattern invalid_pattern2 = + ContentSettingsPattern::FromString("google.com*"); + + // Compare invalid patterns. + EXPECT_TRUE(!invalid_pattern1.IsValid()); + EXPECT_TRUE(!invalid_pattern2.IsValid()); + EXPECT_EQ(ContentSettingsPattern::IDENTITY, + invalid_pattern1.Compare(invalid_pattern2)); + EXPECT_TRUE(invalid_pattern1 == invalid_pattern2); + + // Compare a pattern with an IPv4 addresse to a pattern with a domain name. + EXPECT_EQ(ContentSettingsPattern::DISJOINT_ORDER_POST, + Pattern("http://www.google.com").Compare( + Pattern("127.0.0.1"))); + EXPECT_EQ(ContentSettingsPattern::DISJOINT_ORDER_PRE, + Pattern("127.0.0.1").Compare( + Pattern("http://www.google.com"))); + EXPECT_TRUE(Pattern("127.0.0.1") > Pattern("http://www.google.com")); + EXPECT_TRUE(Pattern("http://www.google.com") < Pattern("127.0.0.1")); + + // Compare a pattern with an IPv6 address to a patterns with a domain name. + EXPECT_EQ(ContentSettingsPattern::DISJOINT_ORDER_POST, + Pattern("http://www.google.com").Compare( + Pattern("[::1]"))); + EXPECT_EQ(ContentSettingsPattern::DISJOINT_ORDER_PRE, + Pattern("[::1]").Compare( + Pattern("http://www.google.com"))); + EXPECT_TRUE(Pattern("[::1]") > Pattern("http://www.google.com")); + EXPECT_TRUE(Pattern("http://www.google.com") < Pattern("[::1]")); + + // Compare a pattern with an IPv6 addresse to a pattern with an IPv4 addresse. + EXPECT_EQ(ContentSettingsPattern::DISJOINT_ORDER_PRE, + Pattern("127.0.0.1").Compare( + Pattern("[::1]"))); + EXPECT_EQ(ContentSettingsPattern::DISJOINT_ORDER_POST, + Pattern("[::1]").Compare( + Pattern("127.0.0.1"))); + EXPECT_TRUE(Pattern("[::1]") < Pattern("127.0.0.1")); + EXPECT_TRUE(Pattern("127.0.0.1") > Pattern("[::1]")); + + EXPECT_EQ(ContentSettingsPattern::DISJOINT_ORDER_PRE, + Pattern("http://www.google.com").Compare( + Pattern("http://www.youtube.com"))); + EXPECT_EQ(ContentSettingsPattern::DISJOINT_ORDER_PRE, + Pattern("http://[*.]google.com").Compare( + Pattern("http://[*.]youtube.com"))); + + EXPECT_EQ(ContentSettingsPattern::DISJOINT_ORDER_POST, + Pattern("http://[*.]host.com").Compare( + Pattern("http://[*.]evilhost.com"))); + EXPECT_EQ(ContentSettingsPattern::DISJOINT_ORDER_POST, + Pattern("*://www.google.com:80").Compare( + Pattern("*://www.google.com:8080"))); + EXPECT_EQ(ContentSettingsPattern::DISJOINT_ORDER_PRE, + Pattern("https://www.google.com:80").Compare( + Pattern("http://www.google.com:80"))); + + EXPECT_EQ(ContentSettingsPattern::DISJOINT_ORDER_PRE, + Pattern("http://[*.]google.com:90").Compare( + Pattern("http://mail.google.com:80"))); + EXPECT_EQ(ContentSettingsPattern::DISJOINT_ORDER_PRE, + Pattern("https://[*.]google.com:80").Compare( + Pattern("http://mail.google.com:80"))); + EXPECT_EQ(ContentSettingsPattern::DISJOINT_ORDER_PRE, + Pattern("https://mail.google.com:*").Compare( + Pattern("http://mail.google.com:80"))); + + // Test patterns with different precedences. + EXPECT_EQ(ContentSettingsPattern::PREDECESSOR, + Pattern("mail.google.com").Compare( + Pattern("[*.]google.com"))); + EXPECT_EQ(ContentSettingsPattern::SUCCESSOR, + Pattern("[*.]google.com").Compare( + Pattern("mail.google.com"))); + + EXPECT_EQ(ContentSettingsPattern::PREDECESSOR, + Pattern("[*.]mail.google.com").Compare( + Pattern("[*.]google.com"))); + EXPECT_EQ(ContentSettingsPattern::SUCCESSOR, + Pattern("[*.]google.com").Compare( + Pattern("[*.]mail.google.com"))); + + EXPECT_EQ(ContentSettingsPattern::PREDECESSOR, + Pattern("mail.google.com:80").Compare( + Pattern("mail.google.com:*"))); + EXPECT_EQ(ContentSettingsPattern::SUCCESSOR, + Pattern("mail.google.com:*").Compare( + Pattern("mail.google.com:80"))); + + EXPECT_EQ(ContentSettingsPattern::PREDECESSOR, + Pattern("https://mail.google.com:*").Compare( + Pattern("*://mail.google.com:*"))); + EXPECT_EQ(ContentSettingsPattern::SUCCESSOR, + Pattern("*://mail.google.com:*").Compare( + Pattern("https://mail.google.com:*"))); + + EXPECT_EQ(ContentSettingsPattern::PREDECESSOR, + Pattern("*://mail.google.com:80").Compare( + Pattern("https://mail.google.com:*"))); + EXPECT_EQ(ContentSettingsPattern::SUCCESSOR, + Pattern("https://mail.google.com:*").Compare( + Pattern("*://mail.google.com:80"))); + + // Test the wildcard pattern. + EXPECT_EQ(ContentSettingsPattern::IDENTITY, + ContentSettingsPattern::Wildcard().Compare( + ContentSettingsPattern::Wildcard())); + + EXPECT_EQ(ContentSettingsPattern::PREDECESSOR, + Pattern("[*.]google.com").Compare( + ContentSettingsPattern::Wildcard())); + EXPECT_EQ(ContentSettingsPattern::SUCCESSOR, + ContentSettingsPattern::Wildcard().Compare( + Pattern("[*.]google.com"))); + + EXPECT_EQ(ContentSettingsPattern::PREDECESSOR, + Pattern("mail.google.com").Compare( + ContentSettingsPattern::Wildcard())); + EXPECT_EQ(ContentSettingsPattern::SUCCESSOR, + ContentSettingsPattern::Wildcard().Compare( + Pattern("mail.google.com"))); +} + +// Legacy tests to ensure backwards compatibility. + +TEST(ContentSettingsPatternTest, PatternSupport_Legacy) { + EXPECT_TRUE(Pattern("[*.]example.com").IsValid()); + EXPECT_TRUE(Pattern("example.com").IsValid()); + EXPECT_TRUE(Pattern("192.168.0.1").IsValid()); + EXPECT_TRUE(Pattern("[::1]").IsValid()); + EXPECT_TRUE( + Pattern("file:///tmp/test.html").IsValid()); + EXPECT_FALSE(Pattern("*example.com").IsValid()); + EXPECT_FALSE(Pattern("example.*").IsValid()); + + EXPECT_TRUE( + Pattern("http://example.com").IsValid()); + EXPECT_TRUE( + Pattern("https://example.com").IsValid()); + + EXPECT_TRUE(Pattern("[*.]example.com").Matches( + GURL("http://example.com/"))); + EXPECT_TRUE(Pattern("[*.]example.com").Matches( + GURL("http://www.example.com/"))); + EXPECT_TRUE(Pattern("www.example.com").Matches( + GURL("http://www.example.com/"))); + EXPECT_TRUE( + Pattern("file:///tmp/test.html").Matches( + GURL("file:///tmp/test.html"))); + EXPECT_FALSE(Pattern(std::string()).Matches(GURL("http://www.example.com/"))); + EXPECT_FALSE(Pattern("[*.]example.com").Matches( + GURL("http://example.org/"))); + EXPECT_FALSE(Pattern("example.com").Matches( + GURL("http://example.org/"))); + EXPECT_FALSE( + Pattern("file:///tmp/test.html").Matches( + GURL("file:///tmp/other.html"))); + EXPECT_FALSE( + Pattern("file:///tmp/test.html").Matches( + GURL("http://example.org/"))); +} + +TEST(ContentSettingsPatternTest, CanonicalizePattern_Legacy) { + // Basic patterns. + EXPECT_STREQ("[*.]ikea.com", Pattern("[*.]ikea.com").ToString().c_str()); + EXPECT_STREQ("example.com", Pattern("example.com").ToString().c_str()); + EXPECT_STREQ("192.168.1.1", Pattern("192.168.1.1").ToString().c_str()); + EXPECT_STREQ("[::1]", Pattern("[::1]").ToString().c_str()); + EXPECT_STREQ("file:///tmp/file.html", + Pattern("file:///tmp/file.html").ToString().c_str()); + + // UTF-8 patterns. + EXPECT_STREQ("[*.]xn--ira-ppa.com", + Pattern("[*.]\xC4\x87ira.com").ToString().c_str()); + EXPECT_STREQ("xn--ira-ppa.com", + Pattern("\xC4\x87ira.com").ToString().c_str()); + EXPECT_STREQ("file:///%C4%87ira.html", + Pattern("file:///\xC4\x87ira.html").ToString().c_str()); + + // file:/// normalization. + EXPECT_STREQ("file:///tmp/test.html", + Pattern("file:///tmp/bar/../test.html").ToString().c_str()); + + // Invalid patterns. + EXPECT_STREQ("", Pattern("*example.com").ToString().c_str()); + EXPECT_STREQ("", Pattern("example.*").ToString().c_str()); + EXPECT_STREQ("", Pattern("*\xC4\x87ira.com").ToString().c_str()); + EXPECT_STREQ("", Pattern("\xC4\x87ira.*").ToString().c_str()); +} diff --git a/components/omnibox/autocomplete_input_unittest.cc b/components/omnibox/autocomplete_input_unittest.cc index aaa4cd2..939aea8 100644 --- a/components/omnibox/autocomplete_input_unittest.cc +++ b/components/omnibox/autocomplete_input_unittest.cc @@ -13,17 +13,11 @@ #include "components/omnibox/test_scheme_classifier.h" #include "testing/gtest/include/gtest/gtest.h" #include "url/url_parse.h" -#include "url/url_util.h" using base::ASCIIToUTF16; using metrics::OmniboxEventProto; TEST(AutocompleteInputTest, InputType) { - // TODO(mukai): Fix this scheme setup. - url::AddStandardScheme("chrome"); - url::AddStandardScheme("chrome-devtools"); - url::AddStandardScheme("chrome-search"); - struct test_data { const base::string16 input; const metrics::OmniboxInputType::Type type; diff --git a/components/test/DEPS b/components/test/DEPS index 25be319..57345fc 100644 --- a/components/test/DEPS +++ b/components/test/DEPS @@ -1,4 +1,6 @@ include_rules = [ + # To initialize the global data of content_settings. + "+components/content_settings/core/common", "+content/public/test", "+ui/base/android/ui_base_jni_registrar.h", "+ui/base/resource/resource_bundle.h", diff --git a/components/test/run_all_unittests.cc b/components/test/run_all_unittests.cc index 1cee176..9664cce 100644 --- a/components/test/run_all_unittests.cc +++ b/components/test/run_all_unittests.cc @@ -8,10 +8,12 @@ #include "base/path_service.h" #include "base/test/launcher/unit_test_launcher.h" #include "base/test/test_suite.h" +#include "components/content_settings/core/common/content_settings_pattern.h" #include "content/public/test/test_content_client_initializer.h" #include "testing/gtest/include/gtest/gtest.h" #include "ui/base/resource/resource_bundle.h" #include "ui/base/ui_base_paths.h" +#include "url/url_util.h" #if defined(OS_MACOSX) #include "base/mac/bundle_locations.h" @@ -83,6 +85,17 @@ class ComponentsTestSuite : public base::TestSuite { ui::ResourceBundle::GetSharedInstance().AddDataPackFromPath( resources_pack_path.AppendASCII("resources.pak"), ui::SCALE_FACTOR_NONE); + + // These schemes need to be added globally to pass tests of + // autocomplete_input_unittest.cc and content_settings_pattern* + url::AddStandardScheme("chrome"); + url::AddStandardScheme("chrome-extension"); + url::AddStandardScheme("chrome-devtools"); + url::AddStandardScheme("chrome-search"); + + // Not using kExtensionScheme to avoid the dependency to extensions. + ContentSettingsPattern::SetNonWildcardDomainNonPortScheme( + "chrome-extension"); } virtual void Shutdown() OVERRIDE { |