Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1463)

Side by Side Diff: chrome/browser/extensions/extension_special_storage_policy_unittest.cc

Issue 10709006: Show apps protecting local data in chrome://settings/cookies. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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 void ExpectProtectedBy(const ExtensionSet& expected_extensions,
marja 2012/07/09 08:11:43 Nit: There's no test which would assert that somet
Bernhard Bauer 2012/07/09 09:50:48 I pass in an ExtensionSet and then expect that thi
marja 2012/07/09 09:55:02 Ahh, yes, I read this sloppily, it's not enough th
126 const GURL& url) {
127 const ExtensionSet* extensions = policy_->ExtensionsProtectingOrigin(url);
128 EXPECT_EQ(expected_extensions.size(), extensions->size());
129 for (ExtensionSet::const_iterator it = expected_extensions.begin();
130 it != expected_extensions.end(); ++it) {
131 EXPECT_TRUE(extensions->Contains((*it)->id()))
132 << "Origin " << url << "not protected by extension ID "
133 << (*it)->id();
134 }
135 }
136
137 scoped_refptr<ExtensionSpecialStoragePolicy> policy_;
119 }; 138 };
120 139
121 TEST_F(ExtensionSpecialStoragePolicyTest, EmptyPolicy) { 140 TEST_F(ExtensionSpecialStoragePolicyTest, EmptyPolicy) {
122 const GURL kHttpUrl("http://foo"); 141 const GURL kHttpUrl("http://foo");
123 const GURL kExtensionUrl("chrome-extension://bar"); 142 const GURL kExtensionUrl("chrome-extension://bar");
124 143
125 scoped_refptr<ExtensionSpecialStoragePolicy> policy( 144 EXPECT_FALSE(policy_->IsStorageUnlimited(kHttpUrl));
126 new ExtensionSpecialStoragePolicy(NULL)); 145 EXPECT_FALSE(policy_->IsStorageUnlimited(kHttpUrl)); // test cached result
127 146 EXPECT_FALSE(policy_->IsStorageUnlimited(kExtensionUrl));
128 ASSERT_FALSE(policy->IsStorageUnlimited(kHttpUrl)); 147 ExtensionSet empty_set;
129 ASSERT_FALSE(policy->IsStorageUnlimited(kHttpUrl)); // test cached result 148 ExpectProtectedBy(empty_set, kHttpUrl);
130 ASSERT_FALSE(policy->IsStorageUnlimited(kExtensionUrl));
131 ASSERT_FALSE(policy->IsStorageProtected(kHttpUrl));
132 149
133 // This one is just based on the scheme. 150 // This one is just based on the scheme.
134 ASSERT_TRUE(policy->IsStorageProtected(kExtensionUrl)); 151 EXPECT_TRUE(policy_->IsStorageProtected(kExtensionUrl));
135 } 152 }
136 153
137 154
138 TEST_F(ExtensionSpecialStoragePolicyTest, AppWithProtectedStorage) { 155 TEST_F(ExtensionSpecialStoragePolicyTest, AppWithProtectedStorage) {
139 scoped_refptr<Extension> extension(CreateProtectedApp()); 156 scoped_refptr<Extension> extension(CreateProtectedApp());
140 scoped_refptr<ExtensionSpecialStoragePolicy> policy( 157 policy_->GrantRightsForExtension(extension);
141 new ExtensionSpecialStoragePolicy(NULL)); 158 ExtensionSet protecting_extensions;
142 policy->GrantRightsForExtension(extension); 159 protecting_extensions.Insert(extension);
143 EXPECT_FALSE(policy->IsStorageUnlimited(extension->url())); 160 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 161
151 policy->RevokeRightsForExtension(extension); 162 EXPECT_FALSE(policy_->IsStorageUnlimited(extension->url()));
152 EXPECT_FALSE(policy->IsStorageProtected(GURL("http://explicit/"))); 163 EXPECT_FALSE(policy_->IsStorageUnlimited(GURL("http://explicit/")));
153 EXPECT_FALSE(policy->IsStorageProtected(GURL("http://foo.wildcards/"))); 164 ExpectProtectedBy(protecting_extensions, GURL("http://explicit/"));
154 EXPECT_FALSE(policy->IsStorageProtected(GURL("https://bar.wildcards/"))); 165 ExpectProtectedBy(protecting_extensions, GURL("http://explicit:6000/"));
166 ExpectProtectedBy(protecting_extensions, GURL("http://foo.wildcards/"));
167 ExpectProtectedBy(protecting_extensions, GURL("https://bar.wildcards/"));
168 ExpectProtectedBy(empty_set, GURL("http://not_listed/"));
169
170 policy_->RevokeRightsForExtension(extension);
171 ExpectProtectedBy(empty_set, GURL("http://explicit/"));
172 ExpectProtectedBy(empty_set, GURL("http://foo.wildcards/"));
173 ExpectProtectedBy(empty_set, GURL("https://bar.wildcards/"));
155 } 174 }
156 175
157 TEST_F(ExtensionSpecialStoragePolicyTest, AppWithUnlimitedStorage) { 176 TEST_F(ExtensionSpecialStoragePolicyTest, AppWithUnlimitedStorage) {
158 scoped_refptr<Extension> extension(CreateUnlimitedApp()); 177 scoped_refptr<Extension> extension(CreateUnlimitedApp());
159 scoped_refptr<ExtensionSpecialStoragePolicy> policy( 178 policy_->GrantRightsForExtension(extension);
160 new ExtensionSpecialStoragePolicy(NULL)); 179 ExtensionSet protecting_extensions;
161 policy->GrantRightsForExtension(extension); 180 protecting_extensions.Insert(extension);
162 EXPECT_TRUE(policy->IsStorageProtected(GURL("http://explicit/"))); 181 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 182
175 policy->RevokeRightsForExtension(extension); 183 ExpectProtectedBy(protecting_extensions, GURL("http://explicit/"));
176 EXPECT_FALSE(policy->IsStorageProtected(GURL("http://explicit/"))); 184 ExpectProtectedBy(protecting_extensions, GURL("http://explicit:6000/"));
177 EXPECT_FALSE(policy->IsStorageProtected(GURL("https://foo.wildcards/"))); 185 ExpectProtectedBy(protecting_extensions, GURL("https://foo.wildcards/"));
178 EXPECT_FALSE(policy->IsStorageProtected(GURL("https://foo.wildcards/"))); 186 ExpectProtectedBy(protecting_extensions, GURL("https://foo.wildcards/"));
179 EXPECT_FALSE(policy->IsStorageProtected(GURL("http://bar.wildcards/"))); 187 ExpectProtectedBy(protecting_extensions, GURL("http://bar.wildcards/"));
180 EXPECT_FALSE(policy->IsStorageUnlimited(GURL("http://explicit/"))); 188 ExpectProtectedBy(empty_set, GURL("http://not_listed/"));
181 EXPECT_FALSE(policy->IsStorageUnlimited(GURL("https://foo.wildcards/"))); 189 EXPECT_TRUE(policy_->IsStorageUnlimited(extension->url()));
182 EXPECT_FALSE(policy->IsStorageUnlimited(GURL("https://bar.wildcards/"))); 190 EXPECT_TRUE(policy_->IsStorageUnlimited(GURL("http://explicit/")));
191 EXPECT_TRUE(policy_->IsStorageUnlimited(GURL("http://explicit:6000/")));
192 EXPECT_TRUE(policy_->IsStorageUnlimited(GURL("https://foo.wildcards/")));
193 EXPECT_TRUE(policy_->IsStorageUnlimited(GURL("https://bar.wildcards/")));
194 EXPECT_FALSE(policy_->IsStorageUnlimited(GURL("http://not_listed/")));
195
196 policy_->RevokeRightsForExtension(extension);
197 ExpectProtectedBy(empty_set, GURL("http://explicit/"));
198 ExpectProtectedBy(empty_set, GURL("https://foo.wildcards/"));
199 ExpectProtectedBy(empty_set, GURL("https://foo.wildcards/"));
200 ExpectProtectedBy(empty_set, GURL("http://bar.wildcards/"));
201 EXPECT_FALSE(policy_->IsStorageUnlimited(GURL("http://explicit/")));
202 EXPECT_FALSE(policy_->IsStorageUnlimited(GURL("https://foo.wildcards/")));
203 EXPECT_FALSE(policy_->IsStorageUnlimited(GURL("https://bar.wildcards/")));
183 } 204 }
184 205
185 TEST_F(ExtensionSpecialStoragePolicyTest, OverlappingApps) { 206 TEST_F(ExtensionSpecialStoragePolicyTest, OverlappingApps) {
186 scoped_refptr<Extension> protected_app(CreateProtectedApp()); 207 scoped_refptr<Extension> protected_app(CreateProtectedApp());
187 scoped_refptr<Extension> unlimited_app(CreateUnlimitedApp()); 208 scoped_refptr<Extension> unlimited_app(CreateUnlimitedApp());
188 scoped_refptr<ExtensionSpecialStoragePolicy> policy( 209 policy_->GrantRightsForExtension(protected_app);
189 new ExtensionSpecialStoragePolicy(NULL)); 210 policy_->GrantRightsForExtension(unlimited_app);
190 policy->GrantRightsForExtension(protected_app); 211 ExtensionSet protecting_extensions;
191 policy->GrantRightsForExtension(unlimited_app); 212 ExtensionSet empty_set;
213 protecting_extensions.Insert(protected_app);
214 protecting_extensions.Insert(unlimited_app);
192 215
193 EXPECT_TRUE(policy->IsStorageProtected(GURL("http://explicit/"))); 216 ExpectProtectedBy(protecting_extensions, GURL("http://explicit/"));
194 EXPECT_TRUE(policy->IsStorageProtected(GURL("http://explicit:6000/"))); 217 ExpectProtectedBy(protecting_extensions, GURL("http://explicit:6000/"));
195 EXPECT_TRUE(policy->IsStorageProtected(GURL("https://foo.wildcards/"))); 218 ExpectProtectedBy(protecting_extensions, GURL("https://foo.wildcards/"));
196 EXPECT_TRUE(policy->IsStorageProtected(GURL("https://foo.wildcards/"))); 219 ExpectProtectedBy(protecting_extensions, GURL("https://foo.wildcards/"));
197 EXPECT_TRUE(policy->IsStorageProtected(GURL("http://bar.wildcards/"))); 220 ExpectProtectedBy(protecting_extensions, GURL("http://bar.wildcards/"));
198 EXPECT_FALSE(policy->IsStorageProtected(GURL("http://not_listed/"))); 221 ExpectProtectedBy(empty_set, GURL("http://not_listed/"));
199 EXPECT_TRUE(policy->IsStorageUnlimited(GURL("http://explicit/"))); 222 EXPECT_TRUE(policy_->IsStorageUnlimited(GURL("http://explicit/")));
200 EXPECT_TRUE(policy->IsStorageUnlimited(GURL("http://explicit:6000/"))); 223 EXPECT_TRUE(policy_->IsStorageUnlimited(GURL("http://explicit:6000/")));
201 EXPECT_TRUE(policy->IsStorageUnlimited(GURL("https://foo.wildcards/"))); 224 EXPECT_TRUE(policy_->IsStorageUnlimited(GURL("https://foo.wildcards/")));
202 EXPECT_TRUE(policy->IsStorageUnlimited(GURL("https://bar.wildcards/"))); 225 EXPECT_TRUE(policy_->IsStorageUnlimited(GURL("https://bar.wildcards/")));
203 EXPECT_FALSE(policy->IsStorageUnlimited(GURL("http://not_listed/"))); 226 EXPECT_FALSE(policy_->IsStorageUnlimited(GURL("http://not_listed/")));
204 227
205 policy->RevokeRightsForExtension(unlimited_app); 228 policy_->RevokeRightsForExtension(unlimited_app);
206 EXPECT_FALSE(policy->IsStorageUnlimited(GURL("http://explicit/"))); 229 protecting_extensions.Remove(unlimited_app->id());
207 EXPECT_FALSE(policy->IsStorageUnlimited(GURL("https://foo.wildcards/"))); 230 EXPECT_FALSE(policy_->IsStorageUnlimited(GURL("http://explicit/")));
208 EXPECT_FALSE(policy->IsStorageUnlimited(GURL("https://bar.wildcards/"))); 231 EXPECT_FALSE(policy_->IsStorageUnlimited(GURL("https://foo.wildcards/")));
209 EXPECT_TRUE(policy->IsStorageProtected(GURL("http://explicit/"))); 232 EXPECT_FALSE(policy_->IsStorageUnlimited(GURL("https://bar.wildcards/")));
210 EXPECT_TRUE(policy->IsStorageProtected(GURL("http://foo.wildcards/"))); 233 ExpectProtectedBy(protecting_extensions, GURL("http://explicit/"));
211 EXPECT_TRUE(policy->IsStorageProtected(GURL("https://bar.wildcards/"))); 234 ExpectProtectedBy(protecting_extensions, GURL("http://foo.wildcards/"));
235 ExpectProtectedBy(protecting_extensions, GURL("https://bar.wildcards/"));
212 236
213 policy->RevokeRightsForExtension(protected_app); 237 policy_->RevokeRightsForExtension(protected_app);
214 EXPECT_FALSE(policy->IsStorageProtected(GURL("http://explicit/"))); 238 ExpectProtectedBy(empty_set, GURL("http://explicit/"));
215 EXPECT_FALSE(policy->IsStorageProtected(GURL("http://foo.wildcards/"))); 239 ExpectProtectedBy(empty_set, GURL("http://foo.wildcards/"));
216 EXPECT_FALSE(policy->IsStorageProtected(GURL("https://bar.wildcards/"))); 240 ExpectProtectedBy(empty_set, GURL("https://bar.wildcards/"));
217 } 241 }
218 242
219 TEST_F(ExtensionSpecialStoragePolicyTest, HasSessionOnlyOrigins) { 243 TEST_F(ExtensionSpecialStoragePolicyTest, HasSessionOnlyOrigins) {
220 MessageLoop message_loop; 244 MessageLoop message_loop;
221 content::TestBrowserThread ui_thread(BrowserThread::UI, &message_loop); 245 content::TestBrowserThread ui_thread(BrowserThread::UI, &message_loop);
222 246
223 TestingProfile profile; 247 TestingProfile profile;
224 CookieSettings* cookie_settings = 248 CookieSettings* cookie_settings =
225 CookieSettings::Factory::GetForProfile(&profile); 249 CookieSettings::Factory::GetForProfile(&profile);
226 scoped_refptr<ExtensionSpecialStoragePolicy> policy( 250 policy_ = new ExtensionSpecialStoragePolicy(cookie_settings);
227 new ExtensionSpecialStoragePolicy(cookie_settings));
228 251
229 EXPECT_FALSE(policy->HasSessionOnlyOrigins()); 252 EXPECT_FALSE(policy_->HasSessionOnlyOrigins());
230 253
231 // The default setting can be session-only. 254 // The default setting can be session-only.
232 cookie_settings->SetDefaultCookieSetting(CONTENT_SETTING_SESSION_ONLY); 255 cookie_settings->SetDefaultCookieSetting(CONTENT_SETTING_SESSION_ONLY);
233 EXPECT_TRUE(policy->HasSessionOnlyOrigins()); 256 EXPECT_TRUE(policy_->HasSessionOnlyOrigins());
234 257
235 cookie_settings->SetDefaultCookieSetting(CONTENT_SETTING_ALLOW); 258 cookie_settings->SetDefaultCookieSetting(CONTENT_SETTING_ALLOW);
236 EXPECT_FALSE(policy->HasSessionOnlyOrigins()); 259 EXPECT_FALSE(policy_->HasSessionOnlyOrigins());
237 260
238 // Or the session-onlyness can affect individual origins. 261 // Or the session-onlyness can affect individual origins.
239 ContentSettingsPattern pattern = 262 ContentSettingsPattern pattern =
240 ContentSettingsPattern::FromString("pattern.com"); 263 ContentSettingsPattern::FromString("pattern.com");
241 264
242 cookie_settings->SetCookieSetting(pattern, 265 cookie_settings->SetCookieSetting(pattern,
243 ContentSettingsPattern::Wildcard(), 266 ContentSettingsPattern::Wildcard(),
244 CONTENT_SETTING_SESSION_ONLY); 267 CONTENT_SETTING_SESSION_ONLY);
245 268
246 EXPECT_TRUE(policy->HasSessionOnlyOrigins()); 269 EXPECT_TRUE(policy_->HasSessionOnlyOrigins());
247 270
248 // Clearing an origin-specific rule. 271 // Clearing an origin-specific rule.
249 cookie_settings->ResetCookieSetting(pattern, 272 cookie_settings->ResetCookieSetting(pattern,
250 ContentSettingsPattern::Wildcard()); 273 ContentSettingsPattern::Wildcard());
251 274
252 EXPECT_FALSE(policy->HasSessionOnlyOrigins()); 275 EXPECT_FALSE(policy_->HasSessionOnlyOrigins());
253 } 276 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698