| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/message_loop.h" | 5 #include "base/message_loop.h" |
| 6 #include "base/values.h" | 6 #include "base/values.h" |
| 7 #include "chrome/browser/content_settings/cookie_settings.h" | 7 #include "chrome/browser/content_settings/cookie_settings.h" |
| 8 #include "chrome/browser/extensions/extension_special_storage_policy.h" | 8 #include "chrome/browser/extensions/extension_special_storage_policy.h" |
| 9 #include "chrome/common/content_settings.h" | 9 #include "chrome/common/content_settings.h" |
| 10 #include "chrome/common/content_settings_types.h" | 10 #include "chrome/common/content_settings_types.h" |
| 11 #include "chrome/common/extensions/extension.h" | 11 #include "chrome/common/extensions/extension.h" |
| 12 #include "chrome/common/extensions/extension_manifest_constants.h" | 12 #include "chrome/common/extensions/extension_manifest_constants.h" |
| 13 #include "chrome/test/base/testing_profile.h" | 13 #include "chrome/test/base/testing_profile.h" |
| 14 #include "content/public/test/test_browser_thread.h" | 14 #include "content/public/test/test_browser_thread.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 | 16 |
| 17 using content::BrowserThread; | 17 using content::BrowserThread; |
| 18 using extensions::Extension; | 18 using extensions::Extension; |
| 19 | 19 |
| 20 namespace keys = extension_manifest_keys; | 20 namespace keys = extension_manifest_keys; |
| 21 | 21 |
| 22 class ExtensionSpecialStoragePolicyTest : public testing::Test { | 22 class ExtensionSpecialStoragePolicyTest : public testing::Test { |
| 23 public: |
| 24 virtual void SetUp() { |
| 25 policy_ = new ExtensionSpecialStoragePolicy(NULL); |
| 26 } |
| 27 |
| 23 protected: | 28 protected: |
| 24 scoped_refptr<Extension> CreateProtectedApp() { | 29 scoped_refptr<Extension> CreateProtectedApp() { |
| 25 #if defined(OS_WIN) | 30 #if defined(OS_WIN) |
| 26 FilePath path(FILE_PATH_LITERAL("c:\\foo")); | 31 FilePath path(FILE_PATH_LITERAL("c:\\foo")); |
| 27 #elif defined(OS_POSIX) | 32 #elif defined(OS_POSIX) |
| 28 FilePath path(FILE_PATH_LITERAL("/foo")); | 33 FilePath path(FILE_PATH_LITERAL("/foo")); |
| 29 #endif | 34 #endif |
| 30 DictionaryValue manifest; | 35 DictionaryValue manifest; |
| 31 manifest.SetString(keys::kName, "Protected"); | 36 manifest.SetString(keys::kName, "Protected"); |
| 32 manifest.SetString(keys::kVersion, "1"); | 37 manifest.SetString(keys::kVersion, "1"); |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 109 ListValue* list = new ListValue(); | 114 ListValue* list = new ListValue(); |
| 110 list->Append(Value::CreateStringValue("unlimitedStorage")); | 115 list->Append(Value::CreateStringValue("unlimitedStorage")); |
| 111 list->Append(Value::CreateStringValue("fileSystem")); | 116 list->Append(Value::CreateStringValue("fileSystem")); |
| 112 manifest.Set(keys::kPermissions, list); | 117 manifest.Set(keys::kPermissions, list); |
| 113 std::string error; | 118 std::string error; |
| 114 scoped_refptr<Extension> handler_app = Extension::Create( | 119 scoped_refptr<Extension> handler_app = Extension::Create( |
| 115 path, Extension::INVALID, manifest, Extension::NO_FLAGS, &error); | 120 path, Extension::INVALID, manifest, Extension::NO_FLAGS, &error); |
| 116 EXPECT_TRUE(handler_app.get()) << error; | 121 EXPECT_TRUE(handler_app.get()) << error; |
| 117 return handler_app; | 122 return handler_app; |
| 118 } | 123 } |
| 124 |
| 125 // Verifies that the set of extensions protecting |url| is *exactly* equal to |
| 126 // |expected_extensions|. Pass in an empty set to verify that an origin is not |
| 127 // protected. |
| 128 void ExpectProtectedBy(const ExtensionSet& expected_extensions, |
| 129 const GURL& url) { |
| 130 const ExtensionSet* extensions = policy_->ExtensionsProtectingOrigin(url); |
| 131 EXPECT_EQ(expected_extensions.size(), extensions->size()); |
| 132 for (ExtensionSet::const_iterator it = expected_extensions.begin(); |
| 133 it != expected_extensions.end(); ++it) { |
| 134 EXPECT_TRUE(extensions->Contains((*it)->id())) |
| 135 << "Origin " << url << "not protected by extension ID " |
| 136 << (*it)->id(); |
| 137 } |
| 138 } |
| 139 |
| 140 scoped_refptr<ExtensionSpecialStoragePolicy> policy_; |
| 119 }; | 141 }; |
| 120 | 142 |
| 121 TEST_F(ExtensionSpecialStoragePolicyTest, EmptyPolicy) { | 143 TEST_F(ExtensionSpecialStoragePolicyTest, EmptyPolicy) { |
| 122 const GURL kHttpUrl("http://foo"); | 144 const GURL kHttpUrl("http://foo"); |
| 123 const GURL kExtensionUrl("chrome-extension://bar"); | 145 const GURL kExtensionUrl("chrome-extension://bar"); |
| 124 | 146 |
| 125 scoped_refptr<ExtensionSpecialStoragePolicy> policy( | 147 EXPECT_FALSE(policy_->IsStorageUnlimited(kHttpUrl)); |
| 126 new ExtensionSpecialStoragePolicy(NULL)); | 148 EXPECT_FALSE(policy_->IsStorageUnlimited(kHttpUrl)); // test cached result |
| 127 | 149 EXPECT_FALSE(policy_->IsStorageUnlimited(kExtensionUrl)); |
| 128 ASSERT_FALSE(policy->IsStorageUnlimited(kHttpUrl)); | 150 ExtensionSet empty_set; |
| 129 ASSERT_FALSE(policy->IsStorageUnlimited(kHttpUrl)); // test cached result | 151 ExpectProtectedBy(empty_set, kHttpUrl); |
| 130 ASSERT_FALSE(policy->IsStorageUnlimited(kExtensionUrl)); | |
| 131 ASSERT_FALSE(policy->IsStorageProtected(kHttpUrl)); | |
| 132 | 152 |
| 133 // This one is just based on the scheme. | 153 // This one is just based on the scheme. |
| 134 ASSERT_TRUE(policy->IsStorageProtected(kExtensionUrl)); | 154 EXPECT_TRUE(policy_->IsStorageProtected(kExtensionUrl)); |
| 135 } | 155 } |
| 136 | 156 |
| 137 | 157 |
| 138 TEST_F(ExtensionSpecialStoragePolicyTest, AppWithProtectedStorage) { | 158 TEST_F(ExtensionSpecialStoragePolicyTest, AppWithProtectedStorage) { |
| 139 scoped_refptr<Extension> extension(CreateProtectedApp()); | 159 scoped_refptr<Extension> extension(CreateProtectedApp()); |
| 140 scoped_refptr<ExtensionSpecialStoragePolicy> policy( | 160 policy_->GrantRightsForExtension(extension); |
| 141 new ExtensionSpecialStoragePolicy(NULL)); | 161 ExtensionSet protecting_extensions; |
| 142 policy->GrantRightsForExtension(extension); | 162 protecting_extensions.Insert(extension); |
| 143 EXPECT_FALSE(policy->IsStorageUnlimited(extension->url())); | 163 ExtensionSet empty_set; |
| 144 EXPECT_FALSE(policy->IsStorageUnlimited(GURL("http://explicit/"))); | |
| 145 EXPECT_TRUE(policy->IsStorageProtected(GURL("http://explicit/"))); | |
| 146 EXPECT_TRUE(policy->IsStorageProtected(GURL("http://explicit:6000/"))); | |
| 147 EXPECT_TRUE(policy->IsStorageProtected(GURL("http://foo.wildcards/"))); | |
| 148 EXPECT_TRUE(policy->IsStorageProtected(GURL("https://bar.wildcards/"))); | |
| 149 EXPECT_FALSE(policy->IsStorageProtected(GURL("http://not_listed/"))); | |
| 150 | 164 |
| 151 policy->RevokeRightsForExtension(extension); | 165 EXPECT_FALSE(policy_->IsStorageUnlimited(extension->url())); |
| 152 EXPECT_FALSE(policy->IsStorageProtected(GURL("http://explicit/"))); | 166 EXPECT_FALSE(policy_->IsStorageUnlimited(GURL("http://explicit/"))); |
| 153 EXPECT_FALSE(policy->IsStorageProtected(GURL("http://foo.wildcards/"))); | 167 ExpectProtectedBy(protecting_extensions, GURL("http://explicit/")); |
| 154 EXPECT_FALSE(policy->IsStorageProtected(GURL("https://bar.wildcards/"))); | 168 ExpectProtectedBy(protecting_extensions, GURL("http://explicit:6000/")); |
| 169 ExpectProtectedBy(protecting_extensions, GURL("http://foo.wildcards/")); |
| 170 ExpectProtectedBy(protecting_extensions, GURL("https://bar.wildcards/")); |
| 171 ExpectProtectedBy(empty_set, GURL("http://not_listed/")); |
| 172 |
| 173 policy_->RevokeRightsForExtension(extension); |
| 174 ExpectProtectedBy(empty_set, GURL("http://explicit/")); |
| 175 ExpectProtectedBy(empty_set, GURL("http://foo.wildcards/")); |
| 176 ExpectProtectedBy(empty_set, GURL("https://bar.wildcards/")); |
| 155 } | 177 } |
| 156 | 178 |
| 157 TEST_F(ExtensionSpecialStoragePolicyTest, AppWithUnlimitedStorage) { | 179 TEST_F(ExtensionSpecialStoragePolicyTest, AppWithUnlimitedStorage) { |
| 158 scoped_refptr<Extension> extension(CreateUnlimitedApp()); | 180 scoped_refptr<Extension> extension(CreateUnlimitedApp()); |
| 159 scoped_refptr<ExtensionSpecialStoragePolicy> policy( | 181 policy_->GrantRightsForExtension(extension); |
| 160 new ExtensionSpecialStoragePolicy(NULL)); | 182 ExtensionSet protecting_extensions; |
| 161 policy->GrantRightsForExtension(extension); | 183 protecting_extensions.Insert(extension); |
| 162 EXPECT_TRUE(policy->IsStorageProtected(GURL("http://explicit/"))); | 184 ExtensionSet empty_set; |
| 163 EXPECT_TRUE(policy->IsStorageProtected(GURL("http://explicit:6000/"))); | |
| 164 EXPECT_TRUE(policy->IsStorageProtected(GURL("https://foo.wildcards/"))); | |
| 165 EXPECT_TRUE(policy->IsStorageProtected(GURL("https://foo.wildcards/"))); | |
| 166 EXPECT_TRUE(policy->IsStorageProtected(GURL("http://bar.wildcards/"))); | |
| 167 EXPECT_FALSE(policy->IsStorageProtected(GURL("http://not_listed/"))); | |
| 168 EXPECT_TRUE(policy->IsStorageUnlimited(extension->url())); | |
| 169 EXPECT_TRUE(policy->IsStorageUnlimited(GURL("http://explicit/"))); | |
| 170 EXPECT_TRUE(policy->IsStorageUnlimited(GURL("http://explicit:6000/"))); | |
| 171 EXPECT_TRUE(policy->IsStorageUnlimited(GURL("https://foo.wildcards/"))); | |
| 172 EXPECT_TRUE(policy->IsStorageUnlimited(GURL("https://bar.wildcards/"))); | |
| 173 EXPECT_FALSE(policy->IsStorageUnlimited(GURL("http://not_listed/"))); | |
| 174 | 185 |
| 175 policy->RevokeRightsForExtension(extension); | 186 ExpectProtectedBy(protecting_extensions, GURL("http://explicit/")); |
| 176 EXPECT_FALSE(policy->IsStorageProtected(GURL("http://explicit/"))); | 187 ExpectProtectedBy(protecting_extensions, GURL("http://explicit:6000/")); |
| 177 EXPECT_FALSE(policy->IsStorageProtected(GURL("https://foo.wildcards/"))); | 188 ExpectProtectedBy(protecting_extensions, GURL("https://foo.wildcards/")); |
| 178 EXPECT_FALSE(policy->IsStorageProtected(GURL("https://foo.wildcards/"))); | 189 ExpectProtectedBy(protecting_extensions, GURL("https://foo.wildcards/")); |
| 179 EXPECT_FALSE(policy->IsStorageProtected(GURL("http://bar.wildcards/"))); | 190 ExpectProtectedBy(protecting_extensions, GURL("http://bar.wildcards/")); |
| 180 EXPECT_FALSE(policy->IsStorageUnlimited(GURL("http://explicit/"))); | 191 ExpectProtectedBy(empty_set, GURL("http://not_listed/")); |
| 181 EXPECT_FALSE(policy->IsStorageUnlimited(GURL("https://foo.wildcards/"))); | 192 EXPECT_TRUE(policy_->IsStorageUnlimited(extension->url())); |
| 182 EXPECT_FALSE(policy->IsStorageUnlimited(GURL("https://bar.wildcards/"))); | 193 EXPECT_TRUE(policy_->IsStorageUnlimited(GURL("http://explicit/"))); |
| 194 EXPECT_TRUE(policy_->IsStorageUnlimited(GURL("http://explicit:6000/"))); |
| 195 EXPECT_TRUE(policy_->IsStorageUnlimited(GURL("https://foo.wildcards/"))); |
| 196 EXPECT_TRUE(policy_->IsStorageUnlimited(GURL("https://bar.wildcards/"))); |
| 197 EXPECT_FALSE(policy_->IsStorageUnlimited(GURL("http://not_listed/"))); |
| 198 |
| 199 policy_->RevokeRightsForExtension(extension); |
| 200 ExpectProtectedBy(empty_set, GURL("http://explicit/")); |
| 201 ExpectProtectedBy(empty_set, GURL("https://foo.wildcards/")); |
| 202 ExpectProtectedBy(empty_set, GURL("https://foo.wildcards/")); |
| 203 ExpectProtectedBy(empty_set, GURL("http://bar.wildcards/")); |
| 204 EXPECT_FALSE(policy_->IsStorageUnlimited(GURL("http://explicit/"))); |
| 205 EXPECT_FALSE(policy_->IsStorageUnlimited(GURL("https://foo.wildcards/"))); |
| 206 EXPECT_FALSE(policy_->IsStorageUnlimited(GURL("https://bar.wildcards/"))); |
| 183 } | 207 } |
| 184 | 208 |
| 185 TEST_F(ExtensionSpecialStoragePolicyTest, OverlappingApps) { | 209 TEST_F(ExtensionSpecialStoragePolicyTest, OverlappingApps) { |
| 186 scoped_refptr<Extension> protected_app(CreateProtectedApp()); | 210 scoped_refptr<Extension> protected_app(CreateProtectedApp()); |
| 187 scoped_refptr<Extension> unlimited_app(CreateUnlimitedApp()); | 211 scoped_refptr<Extension> unlimited_app(CreateUnlimitedApp()); |
| 188 scoped_refptr<ExtensionSpecialStoragePolicy> policy( | 212 policy_->GrantRightsForExtension(protected_app); |
| 189 new ExtensionSpecialStoragePolicy(NULL)); | 213 policy_->GrantRightsForExtension(unlimited_app); |
| 190 policy->GrantRightsForExtension(protected_app); | 214 ExtensionSet protecting_extensions; |
| 191 policy->GrantRightsForExtension(unlimited_app); | 215 ExtensionSet empty_set; |
| 216 protecting_extensions.Insert(protected_app); |
| 217 protecting_extensions.Insert(unlimited_app); |
| 192 | 218 |
| 193 EXPECT_TRUE(policy->IsStorageProtected(GURL("http://explicit/"))); | 219 ExpectProtectedBy(protecting_extensions, GURL("http://explicit/")); |
| 194 EXPECT_TRUE(policy->IsStorageProtected(GURL("http://explicit:6000/"))); | 220 ExpectProtectedBy(protecting_extensions, GURL("http://explicit:6000/")); |
| 195 EXPECT_TRUE(policy->IsStorageProtected(GURL("https://foo.wildcards/"))); | 221 ExpectProtectedBy(protecting_extensions, GURL("https://foo.wildcards/")); |
| 196 EXPECT_TRUE(policy->IsStorageProtected(GURL("https://foo.wildcards/"))); | 222 ExpectProtectedBy(protecting_extensions, GURL("https://foo.wildcards/")); |
| 197 EXPECT_TRUE(policy->IsStorageProtected(GURL("http://bar.wildcards/"))); | 223 ExpectProtectedBy(protecting_extensions, GURL("http://bar.wildcards/")); |
| 198 EXPECT_FALSE(policy->IsStorageProtected(GURL("http://not_listed/"))); | 224 ExpectProtectedBy(empty_set, GURL("http://not_listed/")); |
| 199 EXPECT_TRUE(policy->IsStorageUnlimited(GURL("http://explicit/"))); | 225 EXPECT_TRUE(policy_->IsStorageUnlimited(GURL("http://explicit/"))); |
| 200 EXPECT_TRUE(policy->IsStorageUnlimited(GURL("http://explicit:6000/"))); | 226 EXPECT_TRUE(policy_->IsStorageUnlimited(GURL("http://explicit:6000/"))); |
| 201 EXPECT_TRUE(policy->IsStorageUnlimited(GURL("https://foo.wildcards/"))); | 227 EXPECT_TRUE(policy_->IsStorageUnlimited(GURL("https://foo.wildcards/"))); |
| 202 EXPECT_TRUE(policy->IsStorageUnlimited(GURL("https://bar.wildcards/"))); | 228 EXPECT_TRUE(policy_->IsStorageUnlimited(GURL("https://bar.wildcards/"))); |
| 203 EXPECT_FALSE(policy->IsStorageUnlimited(GURL("http://not_listed/"))); | 229 EXPECT_FALSE(policy_->IsStorageUnlimited(GURL("http://not_listed/"))); |
| 204 | 230 |
| 205 policy->RevokeRightsForExtension(unlimited_app); | 231 policy_->RevokeRightsForExtension(unlimited_app); |
| 206 EXPECT_FALSE(policy->IsStorageUnlimited(GURL("http://explicit/"))); | 232 protecting_extensions.Remove(unlimited_app->id()); |
| 207 EXPECT_FALSE(policy->IsStorageUnlimited(GURL("https://foo.wildcards/"))); | 233 EXPECT_FALSE(policy_->IsStorageUnlimited(GURL("http://explicit/"))); |
| 208 EXPECT_FALSE(policy->IsStorageUnlimited(GURL("https://bar.wildcards/"))); | 234 EXPECT_FALSE(policy_->IsStorageUnlimited(GURL("https://foo.wildcards/"))); |
| 209 EXPECT_TRUE(policy->IsStorageProtected(GURL("http://explicit/"))); | 235 EXPECT_FALSE(policy_->IsStorageUnlimited(GURL("https://bar.wildcards/"))); |
| 210 EXPECT_TRUE(policy->IsStorageProtected(GURL("http://foo.wildcards/"))); | 236 ExpectProtectedBy(protecting_extensions, GURL("http://explicit/")); |
| 211 EXPECT_TRUE(policy->IsStorageProtected(GURL("https://bar.wildcards/"))); | 237 ExpectProtectedBy(protecting_extensions, GURL("http://foo.wildcards/")); |
| 238 ExpectProtectedBy(protecting_extensions, GURL("https://bar.wildcards/")); |
| 212 | 239 |
| 213 policy->RevokeRightsForExtension(protected_app); | 240 policy_->RevokeRightsForExtension(protected_app); |
| 214 EXPECT_FALSE(policy->IsStorageProtected(GURL("http://explicit/"))); | 241 ExpectProtectedBy(empty_set, GURL("http://explicit/")); |
| 215 EXPECT_FALSE(policy->IsStorageProtected(GURL("http://foo.wildcards/"))); | 242 ExpectProtectedBy(empty_set, GURL("http://foo.wildcards/")); |
| 216 EXPECT_FALSE(policy->IsStorageProtected(GURL("https://bar.wildcards/"))); | 243 ExpectProtectedBy(empty_set, GURL("https://bar.wildcards/")); |
| 217 } | 244 } |
| 218 | 245 |
| 219 TEST_F(ExtensionSpecialStoragePolicyTest, HasSessionOnlyOrigins) { | 246 TEST_F(ExtensionSpecialStoragePolicyTest, HasSessionOnlyOrigins) { |
| 220 MessageLoop message_loop; | 247 MessageLoop message_loop; |
| 221 content::TestBrowserThread ui_thread(BrowserThread::UI, &message_loop); | 248 content::TestBrowserThread ui_thread(BrowserThread::UI, &message_loop); |
| 222 | 249 |
| 223 TestingProfile profile; | 250 TestingProfile profile; |
| 224 CookieSettings* cookie_settings = | 251 CookieSettings* cookie_settings = |
| 225 CookieSettings::Factory::GetForProfile(&profile); | 252 CookieSettings::Factory::GetForProfile(&profile); |
| 226 scoped_refptr<ExtensionSpecialStoragePolicy> policy( | 253 policy_ = new ExtensionSpecialStoragePolicy(cookie_settings); |
| 227 new ExtensionSpecialStoragePolicy(cookie_settings)); | |
| 228 | 254 |
| 229 EXPECT_FALSE(policy->HasSessionOnlyOrigins()); | 255 EXPECT_FALSE(policy_->HasSessionOnlyOrigins()); |
| 230 | 256 |
| 231 // The default setting can be session-only. | 257 // The default setting can be session-only. |
| 232 cookie_settings->SetDefaultCookieSetting(CONTENT_SETTING_SESSION_ONLY); | 258 cookie_settings->SetDefaultCookieSetting(CONTENT_SETTING_SESSION_ONLY); |
| 233 EXPECT_TRUE(policy->HasSessionOnlyOrigins()); | 259 EXPECT_TRUE(policy_->HasSessionOnlyOrigins()); |
| 234 | 260 |
| 235 cookie_settings->SetDefaultCookieSetting(CONTENT_SETTING_ALLOW); | 261 cookie_settings->SetDefaultCookieSetting(CONTENT_SETTING_ALLOW); |
| 236 EXPECT_FALSE(policy->HasSessionOnlyOrigins()); | 262 EXPECT_FALSE(policy_->HasSessionOnlyOrigins()); |
| 237 | 263 |
| 238 // Or the session-onlyness can affect individual origins. | 264 // Or the session-onlyness can affect individual origins. |
| 239 ContentSettingsPattern pattern = | 265 ContentSettingsPattern pattern = |
| 240 ContentSettingsPattern::FromString("pattern.com"); | 266 ContentSettingsPattern::FromString("pattern.com"); |
| 241 | 267 |
| 242 cookie_settings->SetCookieSetting(pattern, | 268 cookie_settings->SetCookieSetting(pattern, |
| 243 ContentSettingsPattern::Wildcard(), | 269 ContentSettingsPattern::Wildcard(), |
| 244 CONTENT_SETTING_SESSION_ONLY); | 270 CONTENT_SETTING_SESSION_ONLY); |
| 245 | 271 |
| 246 EXPECT_TRUE(policy->HasSessionOnlyOrigins()); | 272 EXPECT_TRUE(policy_->HasSessionOnlyOrigins()); |
| 247 | 273 |
| 248 // Clearing an origin-specific rule. | 274 // Clearing an origin-specific rule. |
| 249 cookie_settings->ResetCookieSetting(pattern, | 275 cookie_settings->ResetCookieSetting(pattern, |
| 250 ContentSettingsPattern::Wildcard()); | 276 ContentSettingsPattern::Wildcard()); |
| 251 | 277 |
| 252 EXPECT_FALSE(policy->HasSessionOnlyOrigins()); | 278 EXPECT_FALSE(policy_->HasSessionOnlyOrigins()); |
| 253 } | 279 } |
| OLD | NEW |