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/message_loop.h" | |
6 #include "base/values.h" | 5 #include "base/values.h" |
7 #include "chrome/browser/content_settings/cookie_settings.h" | 6 #include "chrome/browser/content_settings/cookie_settings.h" |
8 #include "chrome/browser/extensions/extension_special_storage_policy.h" | 7 #include "chrome/browser/extensions/extension_special_storage_policy.h" |
9 #include "chrome/test/base/testing_profile.h" | 8 #include "chrome/test/base/testing_profile.h" |
10 #include "components/content_settings/core/common/content_settings.h" | 9 #include "components/content_settings/core/common/content_settings.h" |
11 #include "components/content_settings/core/common/content_settings_types.h" | 10 #include "components/content_settings/core/common/content_settings_types.h" |
12 #include "content/public/test/test_browser_thread.h" | 11 #include "content/public/test/test_browser_thread.h" |
| 12 #include "content/public/test/test_browser_thread_bundle.h" |
13 #include "extensions/common/extension.h" | 13 #include "extensions/common/extension.h" |
14 #include "extensions/common/extension_set.h" | 14 #include "extensions/common/extension_set.h" |
15 #include "extensions/common/manifest.h" | 15 #include "extensions/common/manifest.h" |
16 #include "extensions/common/manifest_constants.h" | 16 #include "extensions/common/manifest_constants.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
18 | 18 |
19 using content::BrowserThread; | 19 using content::BrowserThread; |
20 using extensions::Extension; | 20 using extensions::Extension; |
21 using extensions::ExtensionSet; | 21 using extensions::ExtensionSet; |
22 using extensions::Manifest; | 22 using extensions::Manifest; |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
165 const ExtensionSet* extensions = policy_->ExtensionsProtectingOrigin(url); | 165 const ExtensionSet* extensions = policy_->ExtensionsProtectingOrigin(url); |
166 EXPECT_EQ(expected_extensions.size(), extensions->size()); | 166 EXPECT_EQ(expected_extensions.size(), extensions->size()); |
167 for (ExtensionSet::const_iterator it = expected_extensions.begin(); | 167 for (ExtensionSet::const_iterator it = expected_extensions.begin(); |
168 it != expected_extensions.end(); ++it) { | 168 it != expected_extensions.end(); ++it) { |
169 EXPECT_TRUE(extensions->Contains((*it)->id())) | 169 EXPECT_TRUE(extensions->Contains((*it)->id())) |
170 << "Origin " << url << "not protected by extension ID " | 170 << "Origin " << url << "not protected by extension ID " |
171 << (*it)->id(); | 171 << (*it)->id(); |
172 } | 172 } |
173 } | 173 } |
174 | 174 |
| 175 content::TestBrowserThreadBundle thread_bundle_; |
175 scoped_refptr<ExtensionSpecialStoragePolicy> policy_; | 176 scoped_refptr<ExtensionSpecialStoragePolicy> policy_; |
176 }; | 177 }; |
177 | 178 |
178 TEST_F(ExtensionSpecialStoragePolicyTest, EmptyPolicy) { | 179 TEST_F(ExtensionSpecialStoragePolicyTest, EmptyPolicy) { |
179 const GURL kHttpUrl("http://foo"); | 180 const GURL kHttpUrl("http://foo"); |
180 const GURL kExtensionUrl("chrome-extension://bar"); | 181 const GURL kExtensionUrl("chrome-extension://bar"); |
181 scoped_refptr<Extension> app(CreateRegularApp()); | 182 scoped_refptr<Extension> app(CreateRegularApp()); |
182 | 183 |
183 EXPECT_FALSE(policy_->IsStorageUnlimited(kHttpUrl)); | 184 EXPECT_FALSE(policy_->IsStorageUnlimited(kHttpUrl)); |
184 EXPECT_FALSE(policy_->IsStorageUnlimited(kHttpUrl)); // test cached result | 185 EXPECT_FALSE(policy_->IsStorageUnlimited(kHttpUrl)); // test cached result |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
302 ExpectProtectedBy(protecting_extensions, GURL("http://foo.wildcards/")); | 303 ExpectProtectedBy(protecting_extensions, GURL("http://foo.wildcards/")); |
303 ExpectProtectedBy(protecting_extensions, GURL("https://bar.wildcards/")); | 304 ExpectProtectedBy(protecting_extensions, GURL("https://bar.wildcards/")); |
304 | 305 |
305 policy_->RevokeRightsForExtension(protected_app.get()); | 306 policy_->RevokeRightsForExtension(protected_app.get()); |
306 ExpectProtectedBy(empty_set, GURL("http://explicit/")); | 307 ExpectProtectedBy(empty_set, GURL("http://explicit/")); |
307 ExpectProtectedBy(empty_set, GURL("http://foo.wildcards/")); | 308 ExpectProtectedBy(empty_set, GURL("http://foo.wildcards/")); |
308 ExpectProtectedBy(empty_set, GURL("https://bar.wildcards/")); | 309 ExpectProtectedBy(empty_set, GURL("https://bar.wildcards/")); |
309 } | 310 } |
310 | 311 |
311 TEST_F(ExtensionSpecialStoragePolicyTest, HasSessionOnlyOrigins) { | 312 TEST_F(ExtensionSpecialStoragePolicyTest, HasSessionOnlyOrigins) { |
312 base::MessageLoop message_loop; | |
313 content::TestBrowserThread ui_thread(BrowserThread::UI, &message_loop); | |
314 | |
315 TestingProfile profile; | 313 TestingProfile profile; |
316 CookieSettings* cookie_settings = | 314 CookieSettings* cookie_settings = |
317 CookieSettings::Factory::GetForProfile(&profile).get(); | 315 CookieSettings::Factory::GetForProfile(&profile).get(); |
318 policy_ = new ExtensionSpecialStoragePolicy(cookie_settings); | 316 policy_ = new ExtensionSpecialStoragePolicy(cookie_settings); |
319 | 317 |
320 EXPECT_FALSE(policy_->HasSessionOnlyOrigins()); | 318 EXPECT_FALSE(policy_->HasSessionOnlyOrigins()); |
321 | 319 |
322 // The default setting can be session-only. | 320 // The default setting can be session-only. |
323 cookie_settings->SetDefaultCookieSetting(CONTENT_SETTING_SESSION_ONLY); | 321 cookie_settings->SetDefaultCookieSetting(CONTENT_SETTING_SESSION_ONLY); |
324 EXPECT_TRUE(policy_->HasSessionOnlyOrigins()); | 322 EXPECT_TRUE(policy_->HasSessionOnlyOrigins()); |
(...skipping 12 matching lines...) Expand all Loading... |
337 EXPECT_TRUE(policy_->HasSessionOnlyOrigins()); | 335 EXPECT_TRUE(policy_->HasSessionOnlyOrigins()); |
338 | 336 |
339 // Clearing an origin-specific rule. | 337 // Clearing an origin-specific rule. |
340 cookie_settings->ResetCookieSetting(pattern, | 338 cookie_settings->ResetCookieSetting(pattern, |
341 ContentSettingsPattern::Wildcard()); | 339 ContentSettingsPattern::Wildcard()); |
342 | 340 |
343 EXPECT_FALSE(policy_->HasSessionOnlyOrigins()); | 341 EXPECT_FALSE(policy_->HasSessionOnlyOrigins()); |
344 } | 342 } |
345 | 343 |
346 TEST_F(ExtensionSpecialStoragePolicyTest, NotificationTest) { | 344 TEST_F(ExtensionSpecialStoragePolicyTest, NotificationTest) { |
347 base::MessageLoop message_loop; | |
348 content::TestBrowserThread ui_thread(BrowserThread::UI, &message_loop); | |
349 content::TestBrowserThread io_thread(BrowserThread::IO, &message_loop); | |
350 | |
351 PolicyChangeObserver observer; | 345 PolicyChangeObserver observer; |
352 policy_->AddObserver(&observer); | 346 policy_->AddObserver(&observer); |
353 | 347 |
354 scoped_refptr<Extension> apps[] = { | 348 scoped_refptr<Extension> apps[] = { |
355 CreateProtectedApp(), | 349 CreateProtectedApp(), |
356 CreateUnlimitedApp(), | 350 CreateUnlimitedApp(), |
357 }; | 351 }; |
358 | 352 |
359 int change_flags[] = { | 353 int change_flags[] = { |
360 SpecialStoragePolicy::STORAGE_PROTECTED, | 354 SpecialStoragePolicy::STORAGE_PROTECTED, |
361 | 355 |
362 SpecialStoragePolicy::STORAGE_PROTECTED | | 356 SpecialStoragePolicy::STORAGE_PROTECTED | |
363 SpecialStoragePolicy::STORAGE_UNLIMITED, | 357 SpecialStoragePolicy::STORAGE_UNLIMITED, |
364 }; | 358 }; |
365 | 359 |
366 ASSERT_EQ(arraysize(apps), arraysize(change_flags)); | 360 ASSERT_EQ(arraysize(apps), arraysize(change_flags)); |
367 for (size_t i = 0; i < arraysize(apps); ++i) { | 361 for (size_t i = 0; i < arraysize(apps); ++i) { |
368 SCOPED_TRACE(testing::Message() << "i: " << i); | 362 SCOPED_TRACE(testing::Message() << "i: " << i); |
369 observer.ExpectGrant(apps[i]->id(), change_flags[i]); | 363 observer.ExpectGrant(apps[i]->id(), change_flags[i]); |
370 policy_->GrantRightsForExtension(apps[i].get(), NULL); | 364 policy_->GrantRightsForExtension(apps[i].get(), NULL); |
371 message_loop.RunUntilIdle(); | 365 base::MessageLoop::current()->RunUntilIdle(); |
372 EXPECT_TRUE(observer.IsCompleted()); | 366 EXPECT_TRUE(observer.IsCompleted()); |
373 } | 367 } |
374 | 368 |
375 for (size_t i = 0; i < arraysize(apps); ++i) { | 369 for (size_t i = 0; i < arraysize(apps); ++i) { |
376 SCOPED_TRACE(testing::Message() << "i: " << i); | 370 SCOPED_TRACE(testing::Message() << "i: " << i); |
377 policy_->GrantRightsForExtension(apps[i].get(), NULL); | 371 policy_->GrantRightsForExtension(apps[i].get(), NULL); |
378 message_loop.RunUntilIdle(); | 372 base::MessageLoop::current()->RunUntilIdle(); |
379 EXPECT_TRUE(observer.IsCompleted()); | 373 EXPECT_TRUE(observer.IsCompleted()); |
380 } | 374 } |
381 | 375 |
382 for (size_t i = 0; i < arraysize(apps); ++i) { | 376 for (size_t i = 0; i < arraysize(apps); ++i) { |
383 SCOPED_TRACE(testing::Message() << "i: " << i); | 377 SCOPED_TRACE(testing::Message() << "i: " << i); |
384 observer.ExpectRevoke(apps[i]->id(), change_flags[i]); | 378 observer.ExpectRevoke(apps[i]->id(), change_flags[i]); |
385 policy_->RevokeRightsForExtension(apps[i].get()); | 379 policy_->RevokeRightsForExtension(apps[i].get()); |
386 message_loop.RunUntilIdle(); | 380 base::MessageLoop::current()->RunUntilIdle(); |
387 EXPECT_TRUE(observer.IsCompleted()); | 381 EXPECT_TRUE(observer.IsCompleted()); |
388 } | 382 } |
389 | 383 |
390 for (size_t i = 0; i < arraysize(apps); ++i) { | 384 for (size_t i = 0; i < arraysize(apps); ++i) { |
391 SCOPED_TRACE(testing::Message() << "i: " << i); | 385 SCOPED_TRACE(testing::Message() << "i: " << i); |
392 policy_->RevokeRightsForExtension(apps[i].get()); | 386 policy_->RevokeRightsForExtension(apps[i].get()); |
393 message_loop.RunUntilIdle(); | 387 base::MessageLoop::current()->RunUntilIdle(); |
394 EXPECT_TRUE(observer.IsCompleted()); | 388 EXPECT_TRUE(observer.IsCompleted()); |
395 } | 389 } |
396 | 390 |
397 observer.ExpectClear(); | 391 observer.ExpectClear(); |
398 policy_->RevokeRightsForAllExtensions(); | 392 policy_->RevokeRightsForAllExtensions(); |
399 message_loop.RunUntilIdle(); | 393 base::MessageLoop::current()->RunUntilIdle(); |
400 EXPECT_TRUE(observer.IsCompleted()); | 394 EXPECT_TRUE(observer.IsCompleted()); |
401 | 395 |
402 policy_->RemoveObserver(&observer); | 396 policy_->RemoveObserver(&observer); |
403 } | 397 } |
OLD | NEW |