diff options
author | jochen@chromium.org <jochen@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-04-08 09:18:29 +0000 |
---|---|---|
committer | jochen@chromium.org <jochen@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-04-08 09:18:29 +0000 |
commit | 0314ae017cc069f72240401b52f6e37403c581ca (patch) | |
tree | 55f61e3ca4f6670648038cae338f82cddc2625bb /chrome/browser/host_content_settings_map_unittest.cc | |
parent | 3184770afa9cbcf49680bab5046c57a7ca035388 (diff) | |
download | chromium_src-0314ae017cc069f72240401b52f6e37403c581ca.zip chromium_src-0314ae017cc069f72240401b52f6e37403c581ca.tar.gz chromium_src-0314ae017cc069f72240401b52f6e37403c581ca.tar.bz2 |
Implement patterns for content setting exceptions.
BUG=37394
TEST=unit_tests + manual
Review URL: http://codereview.chromium.org/1567010
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@43936 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/host_content_settings_map_unittest.cc')
-rw-r--r-- | chrome/browser/host_content_settings_map_unittest.cc | 129 |
1 files changed, 94 insertions, 35 deletions
diff --git a/chrome/browser/host_content_settings_map_unittest.cc b/chrome/browser/host_content_settings_map_unittest.cc index 164e7b9..363e9b5 100644 --- a/chrome/browser/host_content_settings_map_unittest.cc +++ b/chrome/browser/host_content_settings_map_unittest.cc @@ -8,6 +8,7 @@ #include "chrome/common/notification_service.h" #include "chrome/common/url_constants.h" #include "chrome/test/testing_profile.h" +#include "googleurl/src/gurl.h" #include "testing/gtest/include/gtest/gtest.h" @@ -37,14 +38,16 @@ class StubSettingsObserver : public NotificationObserver { Details<HostContentSettingsMap::ContentSettingsDetails> settings_details(details); last_notifier = content_settings.ptr(); - last_host = settings_details.ptr()->host(); + last_pattern = settings_details.ptr()->pattern(); + last_update_all = settings_details.ptr()->update_all(); // This checks that calling a Get function from an observer doesn't // deadlock. - last_notifier->GetContentSettings("random-hostname"); + last_notifier->GetContentSettings(GURL("http://random-hostname.com/")); } HostContentSettingsMap* last_notifier; - std::string last_host; + HostContentSettingsMap::Pattern last_pattern; + bool last_update_all; int counter; private: @@ -93,16 +96,17 @@ TEST_F(HostContentSettingsMapTest, DefaultValues) { CONTENT_SETTINGS_TYPE_PLUGINS)); // Check returning individual settings. - std::string host("example.com"); + GURL host("http://example.com/"); + HostContentSettingsMap::Pattern pattern("[*.]example.com"); EXPECT_EQ(CONTENT_SETTING_ALLOW, host_content_settings_map->GetContentSetting( host, CONTENT_SETTINGS_TYPE_IMAGES)); - host_content_settings_map->SetContentSetting(host, + host_content_settings_map->SetContentSetting(pattern, CONTENT_SETTINGS_TYPE_IMAGES, CONTENT_SETTING_DEFAULT); EXPECT_EQ(CONTENT_SETTING_ALLOW, host_content_settings_map->GetContentSetting( host, CONTENT_SETTINGS_TYPE_IMAGES)); - host_content_settings_map->SetContentSetting(host, + host_content_settings_map->SetContentSetting(pattern, CONTENT_SETTINGS_TYPE_IMAGES, CONTENT_SETTING_BLOCK); EXPECT_EQ(CONTENT_SETTING_BLOCK, host_content_settings_map->GetContentSetting( @@ -115,15 +119,15 @@ TEST_F(HostContentSettingsMapTest, DefaultValues) { ContentSettings desired_settings; desired_settings.settings[CONTENT_SETTINGS_TYPE_COOKIES] = CONTENT_SETTING_ALLOW; - host_content_settings_map->SetContentSetting(host, + host_content_settings_map->SetContentSetting(pattern, CONTENT_SETTINGS_TYPE_IMAGES, CONTENT_SETTING_DEFAULT); desired_settings.settings[CONTENT_SETTINGS_TYPE_IMAGES] = CONTENT_SETTING_ALLOW; - host_content_settings_map->SetContentSetting(host, + host_content_settings_map->SetContentSetting(pattern, CONTENT_SETTINGS_TYPE_JAVASCRIPT, CONTENT_SETTING_BLOCK); desired_settings.settings[CONTENT_SETTINGS_TYPE_JAVASCRIPT] = CONTENT_SETTING_BLOCK; - host_content_settings_map->SetContentSetting(host, + host_content_settings_map->SetContentSetting(pattern, CONTENT_SETTINGS_TYPE_PLUGINS, CONTENT_SETTING_ALLOW); desired_settings.settings[CONTENT_SETTINGS_TYPE_PLUGINS] = CONTENT_SETTING_ALLOW; @@ -136,10 +140,10 @@ TEST_F(HostContentSettingsMapTest, DefaultValues) { EXPECT_TRUE(SettingsEqual(desired_settings, settings)); // Check returning all hosts for a setting. - std::string host2("example.org"); - host_content_settings_map->SetContentSetting(host2, + HostContentSettingsMap::Pattern pattern2("[*.]example.org"); + host_content_settings_map->SetContentSetting(pattern2, CONTENT_SETTINGS_TYPE_IMAGES, CONTENT_SETTING_BLOCK); - host_content_settings_map->SetContentSetting(host2, + host_content_settings_map->SetContentSetting(pattern2, CONTENT_SETTINGS_TYPE_PLUGINS, CONTENT_SETTING_BLOCK); HostContentSettingsMap::SettingsForOneType host_settings; host_content_settings_map->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_IMAGES, @@ -157,14 +161,14 @@ TEST_F(HostContentSettingsMapTest, DefaultValues) { EXPECT_EQ(0U, host_settings.size()); // Check clearing one type. - std::string host3("example.net"); - host_content_settings_map->SetContentSetting(host, + HostContentSettingsMap::Pattern pattern3("[*.]example.net"); + host_content_settings_map->SetContentSetting(pattern, CONTENT_SETTINGS_TYPE_IMAGES, CONTENT_SETTING_BLOCK); - host_content_settings_map->SetContentSetting(host2, + host_content_settings_map->SetContentSetting(pattern2, CONTENT_SETTINGS_TYPE_IMAGES, CONTENT_SETTING_BLOCK); - host_content_settings_map->SetContentSetting(host2, + host_content_settings_map->SetContentSetting(pattern2, CONTENT_SETTINGS_TYPE_PLUGINS, CONTENT_SETTING_BLOCK); - host_content_settings_map->SetContentSetting(host3, + host_content_settings_map->SetContentSetting(pattern3, CONTENT_SETTINGS_TYPE_IMAGES, CONTENT_SETTING_BLOCK); host_content_settings_map->ClearSettingsForOneType( CONTENT_SETTINGS_TYPE_IMAGES); @@ -176,34 +180,89 @@ TEST_F(HostContentSettingsMapTest, DefaultValues) { EXPECT_EQ(1U, host_settings.size()); } +TEST_F(HostContentSettingsMapTest, Patterns) { + TestingProfile profile; + HostContentSettingsMap* host_content_settings_map = + profile.GetHostContentSettingsMap(); + + GURL host1("http://example.com/"); + GURL host2("http://www.example.com/"); + GURL host3("http://example.org/"); + HostContentSettingsMap::Pattern pattern1("[*.]example.com"); + HostContentSettingsMap::Pattern pattern2("example.org"); + EXPECT_EQ(CONTENT_SETTING_ALLOW, + host_content_settings_map->GetContentSetting( + host1, CONTENT_SETTINGS_TYPE_IMAGES)); + host_content_settings_map->SetContentSetting(pattern1, + CONTENT_SETTINGS_TYPE_IMAGES, CONTENT_SETTING_BLOCK); + EXPECT_EQ(CONTENT_SETTING_BLOCK, + host_content_settings_map->GetContentSetting( + host1, CONTENT_SETTINGS_TYPE_IMAGES)); + EXPECT_EQ(CONTENT_SETTING_BLOCK, + host_content_settings_map->GetContentSetting( + host2, CONTENT_SETTINGS_TYPE_IMAGES)); + EXPECT_EQ(CONTENT_SETTING_ALLOW, + host_content_settings_map->GetContentSetting( + host3, CONTENT_SETTINGS_TYPE_IMAGES)); + host_content_settings_map->SetContentSetting(pattern2, + CONTENT_SETTINGS_TYPE_IMAGES, CONTENT_SETTING_BLOCK); + EXPECT_EQ(CONTENT_SETTING_BLOCK, + host_content_settings_map->GetContentSetting( + host3, CONTENT_SETTINGS_TYPE_IMAGES)); +} + +TEST_F(HostContentSettingsMapTest, PatternSupport) { + EXPECT_TRUE(HostContentSettingsMap::Pattern("[*.]example.com").IsValid()); + EXPECT_TRUE(HostContentSettingsMap::Pattern("example.com").IsValid()); + EXPECT_TRUE(HostContentSettingsMap::Pattern("192.168.0.1").IsValid()); + EXPECT_TRUE(HostContentSettingsMap::Pattern("[::1]").IsValid()); + EXPECT_FALSE(HostContentSettingsMap::Pattern("*example.com").IsValid()); + EXPECT_FALSE(HostContentSettingsMap::Pattern("example.*").IsValid()); + EXPECT_FALSE(HostContentSettingsMap::Pattern("http://example.com").IsValid()); + + EXPECT_TRUE(HostContentSettingsMap::Pattern("[*.]example.com").Matches( + GURL("http://example.com/"))); + EXPECT_TRUE(HostContentSettingsMap::Pattern("[*.]example.com").Matches( + GURL("http://www.example.com/"))); + EXPECT_TRUE(HostContentSettingsMap::Pattern("www.example.com").Matches( + GURL("http://www.example.com/"))); + EXPECT_FALSE(HostContentSettingsMap::Pattern("").Matches( + GURL("http://www.example.com/"))); + EXPECT_FALSE(HostContentSettingsMap::Pattern("[*.]example.com").Matches( + GURL("http://example.org/"))); + EXPECT_FALSE(HostContentSettingsMap::Pattern("example.com").Matches( + GURL("http://example.org/"))); +} + TEST_F(HostContentSettingsMapTest, Observer) { TestingProfile profile; HostContentSettingsMap* host_content_settings_map = profile.GetHostContentSettingsMap(); StubSettingsObserver observer; - std::string host("example.com"); - host_content_settings_map->SetContentSetting(host, + HostContentSettingsMap::Pattern pattern("[*.]example.com"); + host_content_settings_map->SetContentSetting(pattern, CONTENT_SETTINGS_TYPE_IMAGES, CONTENT_SETTING_ALLOW); EXPECT_EQ(host_content_settings_map, observer.last_notifier); - EXPECT_EQ(host, observer.last_host); + EXPECT_EQ(pattern, observer.last_pattern); + EXPECT_FALSE(observer.last_update_all); EXPECT_EQ(1, observer.counter); host_content_settings_map->ClearSettingsForOneType( CONTENT_SETTINGS_TYPE_IMAGES); EXPECT_EQ(host_content_settings_map, observer.last_notifier); - EXPECT_EQ(std::string(), observer.last_host); + EXPECT_TRUE(observer.last_update_all); EXPECT_EQ(2, observer.counter); host_content_settings_map->ResetToDefaults(); EXPECT_EQ(host_content_settings_map, observer.last_notifier); - EXPECT_EQ(std::string(), observer.last_host); + EXPECT_TRUE(observer.last_update_all); EXPECT_EQ(3, observer.counter); host_content_settings_map->SetDefaultContentSetting( CONTENT_SETTINGS_TYPE_IMAGES, CONTENT_SETTING_BLOCK); EXPECT_EQ(host_content_settings_map, observer.last_notifier); - EXPECT_EQ(std::string(), observer.last_host); + EXPECT_TRUE(observer.last_update_all); EXPECT_EQ(4, observer.counter); } @@ -212,18 +271,18 @@ TEST_F(HostContentSettingsMapTest, HostTrimEndingDotCheck) { HostContentSettingsMap* host_content_settings_map = profile.GetHostContentSettingsMap(); - std::string host("example.com"); - std::string host_ending_with_dot("example.com."); + HostContentSettingsMap::Pattern pattern("[*.]example.com"); + GURL host_ending_with_dot("http://example.com./"); EXPECT_EQ(CONTENT_SETTING_ALLOW, host_content_settings_map->GetContentSetting( host_ending_with_dot, CONTENT_SETTINGS_TYPE_IMAGES)); - host_content_settings_map->SetContentSetting(host, + host_content_settings_map->SetContentSetting(pattern, CONTENT_SETTINGS_TYPE_IMAGES, CONTENT_SETTING_DEFAULT); EXPECT_EQ(CONTENT_SETTING_ALLOW, host_content_settings_map->GetContentSetting( host_ending_with_dot, CONTENT_SETTINGS_TYPE_IMAGES)); - host_content_settings_map->SetContentSetting(host, + host_content_settings_map->SetContentSetting(pattern, CONTENT_SETTINGS_TYPE_IMAGES, CONTENT_SETTING_BLOCK); EXPECT_EQ(CONTENT_SETTING_BLOCK, host_content_settings_map->GetContentSetting( @@ -232,12 +291,12 @@ TEST_F(HostContentSettingsMapTest, HostTrimEndingDotCheck) { EXPECT_EQ(CONTENT_SETTING_ALLOW, host_content_settings_map->GetContentSetting( host_ending_with_dot, CONTENT_SETTINGS_TYPE_COOKIES)); - host_content_settings_map->SetContentSetting(host, + host_content_settings_map->SetContentSetting(pattern, CONTENT_SETTINGS_TYPE_COOKIES, CONTENT_SETTING_DEFAULT); EXPECT_EQ(CONTENT_SETTING_ALLOW, host_content_settings_map->GetContentSetting( host_ending_with_dot, CONTENT_SETTINGS_TYPE_COOKIES)); - host_content_settings_map->SetContentSetting(host, + host_content_settings_map->SetContentSetting(pattern, CONTENT_SETTINGS_TYPE_COOKIES, CONTENT_SETTING_BLOCK); EXPECT_EQ(CONTENT_SETTING_BLOCK, host_content_settings_map->GetContentSetting( @@ -246,12 +305,12 @@ TEST_F(HostContentSettingsMapTest, HostTrimEndingDotCheck) { EXPECT_EQ(CONTENT_SETTING_ALLOW, host_content_settings_map->GetContentSetting( host_ending_with_dot, CONTENT_SETTINGS_TYPE_JAVASCRIPT)); - host_content_settings_map->SetContentSetting(host, + host_content_settings_map->SetContentSetting(pattern, CONTENT_SETTINGS_TYPE_JAVASCRIPT, CONTENT_SETTING_DEFAULT); EXPECT_EQ(CONTENT_SETTING_ALLOW, host_content_settings_map->GetContentSetting( host_ending_with_dot, CONTENT_SETTINGS_TYPE_JAVASCRIPT)); - host_content_settings_map->SetContentSetting(host, + host_content_settings_map->SetContentSetting(pattern, CONTENT_SETTINGS_TYPE_JAVASCRIPT, CONTENT_SETTING_BLOCK); EXPECT_EQ(CONTENT_SETTING_BLOCK, host_content_settings_map->GetContentSetting( @@ -260,12 +319,12 @@ TEST_F(HostContentSettingsMapTest, HostTrimEndingDotCheck) { EXPECT_EQ(CONTENT_SETTING_ALLOW, host_content_settings_map->GetContentSetting( host_ending_with_dot, CONTENT_SETTINGS_TYPE_PLUGINS)); - host_content_settings_map->SetContentSetting(host, + host_content_settings_map->SetContentSetting(pattern, CONTENT_SETTINGS_TYPE_PLUGINS, CONTENT_SETTING_DEFAULT); EXPECT_EQ(CONTENT_SETTING_ALLOW, host_content_settings_map->GetContentSetting( host_ending_with_dot, CONTENT_SETTINGS_TYPE_PLUGINS)); - host_content_settings_map->SetContentSetting(host, + host_content_settings_map->SetContentSetting(pattern, CONTENT_SETTINGS_TYPE_PLUGINS, CONTENT_SETTING_BLOCK); EXPECT_EQ(CONTENT_SETTING_BLOCK, host_content_settings_map->GetContentSetting( @@ -274,12 +333,12 @@ TEST_F(HostContentSettingsMapTest, HostTrimEndingDotCheck) { EXPECT_EQ(CONTENT_SETTING_BLOCK, host_content_settings_map->GetContentSetting( host_ending_with_dot, CONTENT_SETTINGS_TYPE_POPUPS)); - host_content_settings_map->SetContentSetting(host, + host_content_settings_map->SetContentSetting(pattern, CONTENT_SETTINGS_TYPE_POPUPS, CONTENT_SETTING_DEFAULT); EXPECT_EQ(CONTENT_SETTING_BLOCK, host_content_settings_map->GetContentSetting( host_ending_with_dot, CONTENT_SETTINGS_TYPE_POPUPS)); - host_content_settings_map->SetContentSetting(host, + host_content_settings_map->SetContentSetting(pattern, CONTENT_SETTINGS_TYPE_POPUPS, CONTENT_SETTING_ALLOW); EXPECT_EQ(CONTENT_SETTING_ALLOW, host_content_settings_map->GetContentSetting( |