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/auto_reset.h" | 5 #include "base/auto_reset.h" |
6 #include "base/command_line.h" | 6 #include "base/command_line.h" |
7 #include "base/json/json_reader.h" | 7 #include "base/json/json_reader.h" |
8 #include "base/json/json_writer.h" | 8 #include "base/json/json_writer.h" |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 #include "base/prefs/pref_service.h" | 10 #include "base/prefs/pref_service.h" |
11 #include "base/prefs/scoped_user_pref_update.h" | 11 #include "base/prefs/scoped_user_pref_update.h" |
12 #include "chrome/browser/content_settings/content_settings_mock_observer.h" | 12 #include "chrome/browser/content_settings/content_settings_mock_observer.h" |
13 #include "chrome/browser/content_settings/cookie_settings_factory.h" | 13 #include "chrome/browser/content_settings/cookie_settings_factory.h" |
14 #include "chrome/browser/content_settings/mock_settings_observer.h" | 14 #include "chrome/browser/content_settings/mock_settings_observer.h" |
15 #include "chrome/common/pref_names.h" | 15 #include "chrome/common/pref_names.h" |
16 #include "chrome/common/url_constants.h" | 16 #include "chrome/common/url_constants.h" |
17 #include "chrome/test/base/testing_pref_service_syncable.h" | 17 #include "chrome/test/base/testing_pref_service_syncable.h" |
18 #include "chrome/test/base/testing_profile.h" | 18 #include "chrome/test/base/testing_profile.h" |
19 #include "components/content_settings/core/browser/content_settings_details.h" | 19 #include "components/content_settings/core/browser/content_settings_details.h" |
20 #include "components/content_settings/core/browser/cookie_settings.h" | 20 #include "components/content_settings/core/browser/cookie_settings.h" |
21 #include "components/content_settings/core/browser/host_content_settings_map.h" | 21 #include "components/content_settings/core/browser/host_content_settings_map.h" |
| 22 #include "components/content_settings/core/browser/website_settings_info.h" |
| 23 #include "components/content_settings/core/browser/website_settings_registry.h" |
22 #include "content/public/test/test_browser_thread.h" | 24 #include "content/public/test/test_browser_thread.h" |
23 #include "net/base/static_cookie_policy.h" | 25 #include "net/base/static_cookie_policy.h" |
24 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
25 #include "url/gurl.h" | 27 #include "url/gurl.h" |
26 | 28 |
27 using content::BrowserThread; | 29 using content::BrowserThread; |
28 | 30 |
29 using ::testing::_; | 31 using ::testing::_; |
30 | 32 |
31 class HostContentSettingsMapTest : public testing::Test { | 33 class HostContentSettingsMapTest : public testing::Test { |
32 public: | 34 public: |
33 HostContentSettingsMapTest() : ui_thread_(BrowserThread::UI, &message_loop_) { | 35 HostContentSettingsMapTest() : ui_thread_(BrowserThread::UI, &message_loop_) { |
34 } | 36 } |
35 | 37 |
36 protected: | 38 protected: |
| 39 const std::string& GetPrefName(ContentSettingsType type) { |
| 40 return content_settings::WebsiteSettingsRegistry::GetInstance() |
| 41 ->Get(type) |
| 42 ->pref_name(); |
| 43 } |
| 44 |
37 base::MessageLoop message_loop_; | 45 base::MessageLoop message_loop_; |
38 content::TestBrowserThread ui_thread_; | 46 content::TestBrowserThread ui_thread_; |
39 }; | 47 }; |
40 | 48 |
41 TEST_F(HostContentSettingsMapTest, DefaultValues) { | 49 TEST_F(HostContentSettingsMapTest, DefaultValues) { |
42 TestingProfile profile; | 50 TestingProfile profile; |
43 HostContentSettingsMap* host_content_settings_map = | 51 HostContentSettingsMap* host_content_settings_map = |
44 profile.GetHostContentSettingsMap(); | 52 profile.GetHostContentSettingsMap(); |
45 | 53 |
46 // Check setting defaults. | 54 // Check setting defaults. |
(...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
326 | 334 |
327 PrefService* prefs = profile.GetPrefs(); | 335 PrefService* prefs = profile.GetPrefs(); |
328 GURL host("http://example.com"); | 336 GURL host("http://example.com"); |
329 | 337 |
330 host_content_settings_map->SetDefaultContentSetting( | 338 host_content_settings_map->SetDefaultContentSetting( |
331 CONTENT_SETTINGS_TYPE_IMAGES, CONTENT_SETTING_BLOCK); | 339 CONTENT_SETTINGS_TYPE_IMAGES, CONTENT_SETTING_BLOCK); |
332 EXPECT_EQ(CONTENT_SETTING_BLOCK, | 340 EXPECT_EQ(CONTENT_SETTING_BLOCK, |
333 host_content_settings_map->GetContentSetting( | 341 host_content_settings_map->GetContentSetting( |
334 host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string())); | 342 host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string())); |
335 | 343 |
| 344 const content_settings::WebsiteSettingsInfo* info = |
| 345 content_settings::WebsiteSettingsRegistry::GetInstance()->Get( |
| 346 CONTENT_SETTINGS_TYPE_IMAGES); |
336 // Clearing the backing pref should also clear the internal cache. | 347 // Clearing the backing pref should also clear the internal cache. |
337 prefs->ClearPref(prefs::kDefaultImagesSetting); | 348 prefs->ClearPref(info->default_value_pref_name()); |
338 EXPECT_EQ(CONTENT_SETTING_ALLOW, | 349 EXPECT_EQ(CONTENT_SETTING_ALLOW, |
339 host_content_settings_map->GetContentSetting( | 350 host_content_settings_map->GetContentSetting( |
340 host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string())); | 351 host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string())); |
341 | 352 |
342 // Reseting the pref to its previous value should update the cache. | 353 // Reseting the pref to its previous value should update the cache. |
343 prefs->SetInteger(prefs::kDefaultImagesSetting, CONTENT_SETTING_BLOCK); | 354 prefs->SetInteger(info->default_value_pref_name(), CONTENT_SETTING_BLOCK); |
344 EXPECT_EQ(CONTENT_SETTING_BLOCK, | 355 EXPECT_EQ(CONTENT_SETTING_BLOCK, |
345 host_content_settings_map->GetContentSetting( | 356 host_content_settings_map->GetContentSetting( |
346 host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string())); | 357 host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string())); |
347 } | 358 } |
348 | 359 |
349 TEST_F(HostContentSettingsMapTest, ObserveExceptionPref) { | 360 TEST_F(HostContentSettingsMapTest, ObserveExceptionPref) { |
350 TestingProfile profile; | 361 TestingProfile profile; |
351 HostContentSettingsMap* host_content_settings_map = | 362 HostContentSettingsMap* host_content_settings_map = |
352 profile.GetHostContentSettingsMap(); | 363 profile.GetHostContentSettingsMap(); |
353 | 364 |
354 PrefService* prefs = profile.GetPrefs(); | 365 PrefService* prefs = profile.GetPrefs(); |
355 | 366 |
356 // Make a copy of the default pref value so we can reset it later. | 367 // Make a copy of the default pref value so we can reset it later. |
357 scoped_ptr<base::Value> default_value(prefs->FindPreference( | 368 scoped_ptr<base::Value> default_value( |
358 prefs::kContentSettingsImagesPatternPairs)->GetValue()->DeepCopy()); | 369 prefs->FindPreference(GetPrefName(CONTENT_SETTINGS_TYPE_IMAGES)) |
| 370 ->GetValue() |
| 371 ->DeepCopy()); |
359 | 372 |
360 ContentSettingsPattern pattern = | 373 ContentSettingsPattern pattern = |
361 ContentSettingsPattern::FromString("[*.]example.com"); | 374 ContentSettingsPattern::FromString("[*.]example.com"); |
362 GURL host("http://example.com"); | 375 GURL host("http://example.com"); |
363 | 376 |
364 EXPECT_EQ(CONTENT_SETTING_ALLOW, | 377 EXPECT_EQ(CONTENT_SETTING_ALLOW, |
365 host_content_settings_map->GetContentSetting( | 378 host_content_settings_map->GetContentSetting( |
366 host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string())); | 379 host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string())); |
367 | 380 |
368 host_content_settings_map->SetContentSetting( | 381 host_content_settings_map->SetContentSetting( |
369 pattern, | 382 pattern, |
370 ContentSettingsPattern::Wildcard(), | 383 ContentSettingsPattern::Wildcard(), |
371 CONTENT_SETTINGS_TYPE_IMAGES, | 384 CONTENT_SETTINGS_TYPE_IMAGES, |
372 std::string(), | 385 std::string(), |
373 CONTENT_SETTING_BLOCK); | 386 CONTENT_SETTING_BLOCK); |
374 EXPECT_EQ(CONTENT_SETTING_BLOCK, | 387 EXPECT_EQ(CONTENT_SETTING_BLOCK, |
375 host_content_settings_map->GetContentSetting( | 388 host_content_settings_map->GetContentSetting( |
376 host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string())); | 389 host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string())); |
377 | 390 |
378 // Make a copy of the pref's new value so we can reset it later. | 391 // Make a copy of the pref's new value so we can reset it later. |
379 scoped_ptr<base::Value> new_value(prefs->FindPreference( | 392 scoped_ptr<base::Value> new_value( |
380 prefs::kContentSettingsImagesPatternPairs)->GetValue()->DeepCopy()); | 393 prefs->FindPreference(GetPrefName(CONTENT_SETTINGS_TYPE_IMAGES)) |
| 394 ->GetValue() |
| 395 ->DeepCopy()); |
381 | 396 |
382 // Clearing the backing pref should also clear the internal cache. | 397 // Clearing the backing pref should also clear the internal cache. |
383 prefs->Set(prefs::kContentSettingsImagesPatternPairs, *default_value); | 398 prefs->Set(GetPrefName(CONTENT_SETTINGS_TYPE_IMAGES), *default_value); |
384 EXPECT_EQ(CONTENT_SETTING_ALLOW, | 399 EXPECT_EQ(CONTENT_SETTING_ALLOW, |
385 host_content_settings_map->GetContentSetting( | 400 host_content_settings_map->GetContentSetting( |
386 host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string())); | 401 host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string())); |
387 | 402 |
388 // Reseting the pref to its previous value should update the cache. | 403 // Reseting the pref to its previous value should update the cache. |
389 prefs->Set(prefs::kContentSettingsImagesPatternPairs, *new_value); | 404 prefs->Set(GetPrefName(CONTENT_SETTINGS_TYPE_IMAGES), *new_value); |
390 EXPECT_EQ(CONTENT_SETTING_BLOCK, | 405 EXPECT_EQ(CONTENT_SETTING_BLOCK, |
391 host_content_settings_map->GetContentSetting( | 406 host_content_settings_map->GetContentSetting( |
392 host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string())); | 407 host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string())); |
393 } | 408 } |
394 | 409 |
395 TEST_F(HostContentSettingsMapTest, HostTrimEndingDotCheck) { | 410 TEST_F(HostContentSettingsMapTest, HostTrimEndingDotCheck) { |
396 TestingProfile profile; | 411 TestingProfile profile; |
397 HostContentSettingsMap* host_content_settings_map = | 412 HostContentSettingsMap* host_content_settings_map = |
398 profile.GetHostContentSettingsMap(); | 413 profile.GetHostContentSettingsMap(); |
399 content_settings::CookieSettings* cookie_settings = | 414 content_settings::CookieSettings* cookie_settings = |
(...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
665 } | 680 } |
666 | 681 |
667 // For a single Unicode encoded pattern, check if it gets converted to punycode | 682 // For a single Unicode encoded pattern, check if it gets converted to punycode |
668 // and old pattern gets deleted. | 683 // and old pattern gets deleted. |
669 TEST_F(HostContentSettingsMapTest, CanonicalizeExceptionsUnicodeOnly) { | 684 TEST_F(HostContentSettingsMapTest, CanonicalizeExceptionsUnicodeOnly) { |
670 TestingProfile profile; | 685 TestingProfile profile; |
671 PrefService* prefs = profile.GetPrefs(); | 686 PrefService* prefs = profile.GetPrefs(); |
672 | 687 |
673 // Set utf-8 data. | 688 // Set utf-8 data. |
674 { | 689 { |
675 DictionaryPrefUpdate update( | 690 DictionaryPrefUpdate update(prefs, |
676 prefs, prefs::kContentSettingsPluginsPatternPairs); | 691 GetPrefName(CONTENT_SETTINGS_TYPE_PLUGINS)); |
677 base::DictionaryValue* all_settings_dictionary = update.Get(); | 692 base::DictionaryValue* all_settings_dictionary = update.Get(); |
678 ASSERT_TRUE(NULL != all_settings_dictionary); | 693 ASSERT_TRUE(NULL != all_settings_dictionary); |
679 | 694 |
680 base::DictionaryValue* dummy_payload = new base::DictionaryValue; | 695 base::DictionaryValue* dummy_payload = new base::DictionaryValue; |
681 dummy_payload->SetInteger("setting", CONTENT_SETTING_ALLOW); | 696 dummy_payload->SetInteger("setting", CONTENT_SETTING_ALLOW); |
682 all_settings_dictionary->SetWithoutPathExpansion("[*.]\xC4\x87ira.com,*", | 697 all_settings_dictionary->SetWithoutPathExpansion("[*.]\xC4\x87ira.com,*", |
683 dummy_payload); | 698 dummy_payload); |
684 } | 699 } |
685 | 700 |
686 profile.GetHostContentSettingsMap(); | 701 profile.GetHostContentSettingsMap(); |
687 | 702 |
688 const base::DictionaryValue* all_settings_dictionary = | 703 const base::DictionaryValue* all_settings_dictionary = |
689 prefs->GetDictionary(prefs::kContentSettingsPluginsPatternPairs); | 704 prefs->GetDictionary(GetPrefName(CONTENT_SETTINGS_TYPE_PLUGINS)); |
690 const base::DictionaryValue* result = NULL; | 705 const base::DictionaryValue* result = NULL; |
691 EXPECT_FALSE(all_settings_dictionary->GetDictionaryWithoutPathExpansion( | 706 EXPECT_FALSE(all_settings_dictionary->GetDictionaryWithoutPathExpansion( |
692 "[*.]\xC4\x87ira.com,*", &result)); | 707 "[*.]\xC4\x87ira.com,*", &result)); |
693 EXPECT_TRUE(all_settings_dictionary->GetDictionaryWithoutPathExpansion( | 708 EXPECT_TRUE(all_settings_dictionary->GetDictionaryWithoutPathExpansion( |
694 "[*.]xn--ira-ppa.com,*", &result)); | 709 "[*.]xn--ira-ppa.com,*", &result)); |
695 } | 710 } |
696 | 711 |
697 // If both Unicode and its punycode pattern exist, make sure we don't touch the | 712 // If both Unicode and its punycode pattern exist, make sure we don't touch the |
698 // settings for the punycode, and that Unicode pattern gets deleted. | 713 // settings for the punycode, and that Unicode pattern gets deleted. |
699 TEST_F(HostContentSettingsMapTest, CanonicalizeExceptionsUnicodeAndPunycode) { | 714 TEST_F(HostContentSettingsMapTest, CanonicalizeExceptionsUnicodeAndPunycode) { |
700 TestingProfile profile; | 715 TestingProfile profile; |
701 | 716 |
702 scoped_ptr<base::Value> value(base::JSONReader::DeprecatedRead( | 717 scoped_ptr<base::Value> value(base::JSONReader::DeprecatedRead( |
703 "{\"[*.]\\xC4\\x87ira.com,*\":{\"setting\":1}}")); | 718 "{\"[*.]\\xC4\\x87ira.com,*\":{\"setting\":1}}")); |
704 profile.GetPrefs()->Set(prefs::kContentSettingsCookiesPatternPairs, *value); | 719 profile.GetPrefs()->Set(GetPrefName(CONTENT_SETTINGS_TYPE_COOKIES), *value); |
705 | 720 |
706 // Set punycode equivalent, with different setting. | 721 // Set punycode equivalent, with different setting. |
707 scoped_ptr<base::Value> puny_value(base::JSONReader::DeprecatedRead( | 722 scoped_ptr<base::Value> puny_value(base::JSONReader::DeprecatedRead( |
708 "{\"[*.]xn--ira-ppa.com,*\":{\"setting\":2}}")); | 723 "{\"[*.]xn--ira-ppa.com,*\":{\"setting\":2}}")); |
709 profile.GetPrefs()->Set( | 724 profile.GetPrefs()->Set(GetPrefName(CONTENT_SETTINGS_TYPE_COOKIES), |
710 prefs::kContentSettingsCookiesPatternPairs, *puny_value); | 725 *puny_value); |
711 | 726 |
712 // Initialize the content map. | 727 // Initialize the content map. |
713 profile.GetHostContentSettingsMap(); | 728 profile.GetHostContentSettingsMap(); |
714 | 729 |
715 const base::DictionaryValue& content_setting_prefs = | 730 const base::DictionaryValue& content_setting_prefs = |
716 *profile.GetPrefs()->GetDictionary( | 731 *profile.GetPrefs()->GetDictionary( |
717 prefs::kContentSettingsCookiesPatternPairs); | 732 GetPrefName(CONTENT_SETTINGS_TYPE_COOKIES)); |
718 std::string prefs_as_json; | 733 std::string prefs_as_json; |
719 base::JSONWriter::Write(content_setting_prefs, &prefs_as_json); | 734 base::JSONWriter::Write(content_setting_prefs, &prefs_as_json); |
720 EXPECT_STREQ("{\"[*.]xn--ira-ppa.com,*\":{\"setting\":2}}", | 735 EXPECT_STREQ("{\"[*.]xn--ira-ppa.com,*\":{\"setting\":2}}", |
721 prefs_as_json.c_str()); | 736 prefs_as_json.c_str()); |
722 } | 737 } |
723 | 738 |
724 // If a default-content-setting is managed, the managed value should be used | 739 // If a default-content-setting is managed, the managed value should be used |
725 // instead of the default value. | 740 // instead of the default value. |
726 TEST_F(HostContentSettingsMapTest, ManagedDefaultContentSetting) { | 741 TEST_F(HostContentSettingsMapTest, ManagedDefaultContentSetting) { |
727 TestingProfile profile; | 742 TestingProfile profile; |
(...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1007 EXPECT_EQ(CONTENT_SETTING_ALLOW, | 1022 EXPECT_EQ(CONTENT_SETTING_ALLOW, |
1008 host_content_settings_map->GetContentSetting( | 1023 host_content_settings_map->GetContentSetting( |
1009 host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string())); | 1024 host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string())); |
1010 host_content_settings_map->SetContentSetting( | 1025 host_content_settings_map->SetContentSetting( |
1011 pattern, | 1026 pattern, |
1012 ContentSettingsPattern::Wildcard(), | 1027 ContentSettingsPattern::Wildcard(), |
1013 CONTENT_SETTINGS_TYPE_IMAGES, | 1028 CONTENT_SETTINGS_TYPE_IMAGES, |
1014 std::string(), | 1029 std::string(), |
1015 CONTENT_SETTING_DEFAULT); | 1030 CONTENT_SETTING_DEFAULT); |
1016 } | 1031 } |
OLD | NEW |