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" |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
122 list->Append(Value::CreateStringValue("fileSystem")); | 122 list->Append(Value::CreateStringValue("fileSystem")); |
123 manifest.Set(keys::kPermissions, list); | 123 manifest.Set(keys::kPermissions, list); |
124 std::string error; | 124 std::string error; |
125 scoped_refptr<Extension> handler_app = Extension::Create( | 125 scoped_refptr<Extension> handler_app = Extension::Create( |
126 path, Manifest::INVALID_LOCATION, manifest, | 126 path, Manifest::INVALID_LOCATION, manifest, |
127 Extension::NO_FLAGS, &error); | 127 Extension::NO_FLAGS, &error); |
128 EXPECT_TRUE(handler_app.get()) << error; | 128 EXPECT_TRUE(handler_app.get()) << error; |
129 return handler_app; | 129 return handler_app; |
130 } | 130 } |
131 | 131 |
| 132 scoped_refptr<Extension> CreateRegularApp() { |
| 133 #if defined(OS_WIN) |
| 134 base::FilePath path(FILE_PATH_LITERAL("c:\\app")); |
| 135 #elif defined(OS_POSIX) |
| 136 base::FilePath path(FILE_PATH_LITERAL("/app")); |
| 137 #endif |
| 138 DictionaryValue manifest; |
| 139 manifest.SetString(keys::kName, "App"); |
| 140 manifest.SetString(keys::kVersion, "1"); |
| 141 manifest.SetString(keys::kPlatformAppBackgroundPage, "background.html"); |
| 142 std::string error; |
| 143 scoped_refptr<Extension> app = Extension::Create( |
| 144 path, Manifest::INVALID_LOCATION, manifest, |
| 145 Extension::NO_FLAGS, &error); |
| 146 EXPECT_TRUE(app.get()) << error; |
| 147 return app; |
| 148 } |
| 149 |
132 // Verifies that the set of extensions protecting |url| is *exactly* equal to | 150 // Verifies that the set of extensions protecting |url| is *exactly* equal to |
133 // |expected_extensions|. Pass in an empty set to verify that an origin is not | 151 // |expected_extensions|. Pass in an empty set to verify that an origin is not |
134 // protected. | 152 // protected. |
135 void ExpectProtectedBy(const ExtensionSet& expected_extensions, | 153 void ExpectProtectedBy(const ExtensionSet& expected_extensions, |
136 const GURL& url) { | 154 const GURL& url) { |
137 const ExtensionSet* extensions = policy_->ExtensionsProtectingOrigin(url); | 155 const ExtensionSet* extensions = policy_->ExtensionsProtectingOrigin(url); |
138 EXPECT_EQ(expected_extensions.size(), extensions->size()); | 156 EXPECT_EQ(expected_extensions.size(), extensions->size()); |
139 for (ExtensionSet::const_iterator it = expected_extensions.begin(); | 157 for (ExtensionSet::const_iterator it = expected_extensions.begin(); |
140 it != expected_extensions.end(); ++it) { | 158 it != expected_extensions.end(); ++it) { |
141 EXPECT_TRUE(extensions->Contains((*it)->id())) | 159 EXPECT_TRUE(extensions->Contains((*it)->id())) |
142 << "Origin " << url << "not protected by extension ID " | 160 << "Origin " << url << "not protected by extension ID " |
143 << (*it)->id(); | 161 << (*it)->id(); |
144 } | 162 } |
145 } | 163 } |
146 | 164 |
147 scoped_refptr<ExtensionSpecialStoragePolicy> policy_; | 165 scoped_refptr<ExtensionSpecialStoragePolicy> policy_; |
148 }; | 166 }; |
149 | 167 |
150 TEST_F(ExtensionSpecialStoragePolicyTest, EmptyPolicy) { | 168 TEST_F(ExtensionSpecialStoragePolicyTest, EmptyPolicy) { |
151 const GURL kHttpUrl("http://foo"); | 169 const GURL kHttpUrl("http://foo"); |
152 const GURL kExtensionUrl("chrome-extension://bar"); | 170 const GURL kExtensionUrl("chrome-extension://bar"); |
| 171 scoped_refptr<Extension> app(CreateRegularApp()); |
153 | 172 |
154 EXPECT_FALSE(policy_->IsStorageUnlimited(kHttpUrl)); | 173 EXPECT_FALSE(policy_->IsStorageUnlimited(kHttpUrl)); |
155 EXPECT_FALSE(policy_->IsStorageUnlimited(kHttpUrl)); // test cached result | 174 EXPECT_FALSE(policy_->IsStorageUnlimited(kHttpUrl)); // test cached result |
156 EXPECT_FALSE(policy_->IsStorageUnlimited(kExtensionUrl)); | 175 EXPECT_FALSE(policy_->IsStorageUnlimited(kExtensionUrl)); |
| 176 EXPECT_FALSE(policy_->IsStorageUnlimited(app->url())); |
157 ExtensionSet empty_set; | 177 ExtensionSet empty_set; |
158 ExpectProtectedBy(empty_set, kHttpUrl); | 178 ExpectProtectedBy(empty_set, kHttpUrl); |
159 | 179 |
160 // This one is just based on the scheme. | 180 // This one is just based on the scheme. |
161 EXPECT_TRUE(policy_->IsStorageProtected(kExtensionUrl)); | 181 EXPECT_TRUE(policy_->IsStorageProtected(kExtensionUrl)); |
| 182 EXPECT_TRUE(policy_->IsStorageProtected(app->url())); |
162 } | 183 } |
163 | 184 |
164 | |
165 TEST_F(ExtensionSpecialStoragePolicyTest, AppWithProtectedStorage) { | 185 TEST_F(ExtensionSpecialStoragePolicyTest, AppWithProtectedStorage) { |
166 scoped_refptr<Extension> extension(CreateProtectedApp()); | 186 scoped_refptr<Extension> extension(CreateProtectedApp()); |
167 policy_->GrantRightsForExtension(extension); | 187 policy_->GrantRightsForExtension(extension); |
168 ExtensionSet protecting_extensions; | 188 ExtensionSet protecting_extensions; |
169 protecting_extensions.Insert(extension); | 189 protecting_extensions.Insert(extension); |
170 ExtensionSet empty_set; | 190 ExtensionSet empty_set; |
171 | 191 |
172 EXPECT_FALSE(policy_->IsStorageUnlimited(extension->url())); | 192 EXPECT_FALSE(policy_->IsStorageUnlimited(extension->url())); |
173 EXPECT_FALSE(policy_->IsStorageUnlimited(GURL("http://explicit/"))); | 193 EXPECT_FALSE(policy_->IsStorageUnlimited(GURL("http://explicit/"))); |
174 ExpectProtectedBy(protecting_extensions, GURL("http://explicit/")); | 194 ExpectProtectedBy(protecting_extensions, GURL("http://explicit/")); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
206 policy_->RevokeRightsForExtension(extension); | 226 policy_->RevokeRightsForExtension(extension); |
207 ExpectProtectedBy(empty_set, GURL("http://explicit/")); | 227 ExpectProtectedBy(empty_set, GURL("http://explicit/")); |
208 ExpectProtectedBy(empty_set, GURL("https://foo.wildcards/")); | 228 ExpectProtectedBy(empty_set, GURL("https://foo.wildcards/")); |
209 ExpectProtectedBy(empty_set, GURL("https://foo.wildcards/")); | 229 ExpectProtectedBy(empty_set, GURL("https://foo.wildcards/")); |
210 ExpectProtectedBy(empty_set, GURL("http://bar.wildcards/")); | 230 ExpectProtectedBy(empty_set, GURL("http://bar.wildcards/")); |
211 EXPECT_FALSE(policy_->IsStorageUnlimited(GURL("http://explicit/"))); | 231 EXPECT_FALSE(policy_->IsStorageUnlimited(GURL("http://explicit/"))); |
212 EXPECT_FALSE(policy_->IsStorageUnlimited(GURL("https://foo.wildcards/"))); | 232 EXPECT_FALSE(policy_->IsStorageUnlimited(GURL("https://foo.wildcards/"))); |
213 EXPECT_FALSE(policy_->IsStorageUnlimited(GURL("https://bar.wildcards/"))); | 233 EXPECT_FALSE(policy_->IsStorageUnlimited(GURL("https://bar.wildcards/"))); |
214 } | 234 } |
215 | 235 |
| 236 TEST_F(ExtensionSpecialStoragePolicyTest, IsInstalled) { |
| 237 const GURL kHttpUrl("http://foo"); |
| 238 const GURL kExtensionUrl("chrome-extension://bar"); |
| 239 scoped_refptr<Extension> regular_app(CreateRegularApp()); |
| 240 scoped_refptr<Extension> protected_app(CreateProtectedApp()); |
| 241 scoped_refptr<Extension> unlimited_app(CreateUnlimitedApp()); |
| 242 policy_->GrantRightsForExtension(regular_app); |
| 243 policy_->GrantRightsForExtension(protected_app); |
| 244 policy_->GrantRightsForExtension(unlimited_app); |
| 245 |
| 246 EXPECT_FALSE(policy_->IsInstalledApp(kHttpUrl)); |
| 247 EXPECT_FALSE(policy_->IsInstalledApp(kExtensionUrl)); |
| 248 EXPECT_TRUE(policy_->IsInstalledApp(regular_app->url())); |
| 249 EXPECT_TRUE(policy_->IsInstalledApp(protected_app->url())); |
| 250 EXPECT_TRUE(policy_->IsInstalledApp(unlimited_app->url())); |
| 251 } |
| 252 |
216 TEST_F(ExtensionSpecialStoragePolicyTest, OverlappingApps) { | 253 TEST_F(ExtensionSpecialStoragePolicyTest, OverlappingApps) { |
217 scoped_refptr<Extension> protected_app(CreateProtectedApp()); | 254 scoped_refptr<Extension> protected_app(CreateProtectedApp()); |
218 scoped_refptr<Extension> unlimited_app(CreateUnlimitedApp()); | 255 scoped_refptr<Extension> unlimited_app(CreateUnlimitedApp()); |
219 policy_->GrantRightsForExtension(protected_app); | 256 policy_->GrantRightsForExtension(protected_app); |
220 policy_->GrantRightsForExtension(unlimited_app); | 257 policy_->GrantRightsForExtension(unlimited_app); |
221 ExtensionSet protecting_extensions; | 258 ExtensionSet protecting_extensions; |
222 ExtensionSet empty_set; | 259 ExtensionSet empty_set; |
223 protecting_extensions.Insert(protected_app); | 260 protecting_extensions.Insert(protected_app); |
224 protecting_extensions.Insert(unlimited_app); | 261 protecting_extensions.Insert(unlimited_app); |
225 | 262 |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
277 CONTENT_SETTING_SESSION_ONLY); | 314 CONTENT_SETTING_SESSION_ONLY); |
278 | 315 |
279 EXPECT_TRUE(policy_->HasSessionOnlyOrigins()); | 316 EXPECT_TRUE(policy_->HasSessionOnlyOrigins()); |
280 | 317 |
281 // Clearing an origin-specific rule. | 318 // Clearing an origin-specific rule. |
282 cookie_settings->ResetCookieSetting(pattern, | 319 cookie_settings->ResetCookieSetting(pattern, |
283 ContentSettingsPattern::Wildcard()); | 320 ContentSettingsPattern::Wildcard()); |
284 | 321 |
285 EXPECT_FALSE(policy_->HasSessionOnlyOrigins()); | 322 EXPECT_FALSE(policy_->HasSessionOnlyOrigins()); |
286 } | 323 } |
OLD | NEW |