| 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 <memory> | 5 #include <memory> |
| 6 #include <string> | 6 #include <string> |
| 7 | 7 |
| 8 #include "base/auto_reset.h" | 8 #include "base/auto_reset.h" |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/json/json_reader.h" | 10 #include "base/json/json_reader.h" |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 #include "components/syncable_prefs/testing_pref_service_syncable.h" | 28 #include "components/syncable_prefs/testing_pref_service_syncable.h" |
| 29 #include "content/public/test/test_browser_thread.h" | 29 #include "content/public/test/test_browser_thread.h" |
| 30 #include "net/base/static_cookie_policy.h" | 30 #include "net/base/static_cookie_policy.h" |
| 31 #include "testing/gtest/include/gtest/gtest.h" | 31 #include "testing/gtest/include/gtest/gtest.h" |
| 32 #include "url/gurl.h" | 32 #include "url/gurl.h" |
| 33 | 33 |
| 34 using content::BrowserThread; | 34 using content::BrowserThread; |
| 35 | 35 |
| 36 using ::testing::_; | 36 using ::testing::_; |
| 37 | 37 |
| 38 namespace { |
| 39 |
| 40 bool MatchPrimaryPattern(const ContentSettingsPattern& expected_primary, |
| 41 const ContentSettingsPattern& primary_pattern, |
| 42 const ContentSettingsPattern& secondary_pattern) { |
| 43 return expected_primary == primary_pattern; |
| 44 } |
| 45 |
| 46 } // namespace |
| 47 |
| 38 class HostContentSettingsMapTest : public testing::Test { | 48 class HostContentSettingsMapTest : public testing::Test { |
| 39 public: | 49 public: |
| 40 HostContentSettingsMapTest() : ui_thread_(BrowserThread::UI, &message_loop_) { | 50 HostContentSettingsMapTest() : ui_thread_(BrowserThread::UI, &message_loop_) { |
| 41 } | 51 } |
| 42 | 52 |
| 43 protected: | 53 protected: |
| 44 const std::string& GetPrefName(ContentSettingsType type) { | 54 const std::string& GetPrefName(ContentSettingsType type) { |
| 45 return content_settings::WebsiteSettingsRegistry::GetInstance() | 55 return content_settings::WebsiteSettingsRegistry::GetInstance() |
| 46 ->Get(type) | 56 ->Get(type) |
| 47 ->pref_name(); | 57 ->pref_name(); |
| (...skipping 1593 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1641 base::DictionaryValue test_value; | 1651 base::DictionaryValue test_value; |
| 1642 test_value.SetString("test", "value"); | 1652 test_value.SetString("test", "value"); |
| 1643 host_content_settings_map->SetWebsiteSettingDefaultScope( | 1653 host_content_settings_map->SetWebsiteSettingDefaultScope( |
| 1644 unsupported_url, unsupported_url, CONTENT_SETTINGS_TYPE_APP_BANNER, | 1654 unsupported_url, unsupported_url, CONTENT_SETTINGS_TYPE_APP_BANNER, |
| 1645 std::string(), base::WrapUnique(test_value.DeepCopy())); | 1655 std::string(), base::WrapUnique(test_value.DeepCopy())); |
| 1646 EXPECT_EQ(nullptr, | 1656 EXPECT_EQ(nullptr, |
| 1647 host_content_settings_map->GetWebsiteSetting( | 1657 host_content_settings_map->GetWebsiteSetting( |
| 1648 unsupported_url, unsupported_url, | 1658 unsupported_url, unsupported_url, |
| 1649 CONTENT_SETTINGS_TYPE_APP_BANNER, std::string(), nullptr)); | 1659 CONTENT_SETTINGS_TYPE_APP_BANNER, std::string(), nullptr)); |
| 1650 } | 1660 } |
| 1661 |
| 1662 TEST_F(HostContentSettingsMapTest, ClearSettingsForOneTypeWithPredicate) { |
| 1663 TestingProfile profile; |
| 1664 HostContentSettingsMap* host_content_settings_map = |
| 1665 HostContentSettingsMapFactory::GetForProfile(&profile); |
| 1666 ContentSettingsForOneType host_settings; |
| 1667 |
| 1668 // Patterns with wildcards. |
| 1669 ContentSettingsPattern pattern = |
| 1670 ContentSettingsPattern::FromString("[*.]example.org"); |
| 1671 ContentSettingsPattern pattern2 = |
| 1672 ContentSettingsPattern::FromString("[*.]example.net"); |
| 1673 |
| 1674 // Patterns without wildcards. |
| 1675 GURL url1("https://www.google.com/"); |
| 1676 GURL url2("https://www.google.com/maps"); |
| 1677 GURL url3("http://www.google.com/maps"); |
| 1678 GURL url3_origin_only("http://www.google.com/"); |
| 1679 |
| 1680 host_content_settings_map->SetContentSettingCustomScope( |
| 1681 pattern2, ContentSettingsPattern::Wildcard(), |
| 1682 CONTENT_SETTINGS_TYPE_COOKIES, std::string(), CONTENT_SETTING_BLOCK); |
| 1683 host_content_settings_map->SetContentSettingCustomScope( |
| 1684 pattern, ContentSettingsPattern::Wildcard(), |
| 1685 CONTENT_SETTINGS_TYPE_COOKIES, std::string(), CONTENT_SETTING_BLOCK); |
| 1686 host_content_settings_map->SetWebsiteSettingCustomScope( |
| 1687 pattern2, ContentSettingsPattern::Wildcard(), |
| 1688 CONTENT_SETTINGS_TYPE_APP_BANNER, std::string(), |
| 1689 base::WrapUnique(new base::DictionaryValue())); |
| 1690 |
| 1691 // First, test that we clear only COOKIES (not APP_BANNER), and pattern2. |
| 1692 host_content_settings_map->ClearSettingsForOneTypeWithPredicate( |
| 1693 CONTENT_SETTINGS_TYPE_COOKIES, |
| 1694 base::Bind(&MatchPrimaryPattern, pattern2)); |
| 1695 host_content_settings_map->GetSettingsForOneType( |
| 1696 CONTENT_SETTINGS_TYPE_COOKIES, std::string(), &host_settings); |
| 1697 // |host_settings| contains default & block. |
| 1698 EXPECT_EQ(2U, host_settings.size()); |
| 1699 EXPECT_EQ(pattern, host_settings[0].primary_pattern); |
| 1700 EXPECT_EQ("*", host_settings[0].secondary_pattern.ToString()); |
| 1701 EXPECT_EQ("*", host_settings[1].primary_pattern.ToString()); |
| 1702 EXPECT_EQ("*", host_settings[1].secondary_pattern.ToString()); |
| 1703 |
| 1704 host_content_settings_map->GetSettingsForOneType( |
| 1705 CONTENT_SETTINGS_TYPE_APP_BANNER, std::string(), &host_settings); |
| 1706 // |host_settings| still contains the value for APP_BANNER. |
| 1707 EXPECT_EQ(1U, host_settings.size()); |
| 1708 EXPECT_EQ(pattern2, host_settings[0].primary_pattern); |
| 1709 EXPECT_EQ("*", host_settings[0].secondary_pattern.ToString()); |
| 1710 |
| 1711 // Next, test that we do correct pattern matching w/ an origin policy item. |
| 1712 // We verify that we have no settings stored. |
| 1713 host_content_settings_map->GetSettingsForOneType( |
| 1714 CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, std::string(), &host_settings); |
| 1715 EXPECT_EQ(0u, host_settings.size()); |
| 1716 // Add settings. |
| 1717 host_content_settings_map->SetWebsiteSettingDefaultScope( |
| 1718 url1, GURL(), CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, std::string(), |
| 1719 base::WrapUnique(new base::DictionaryValue())); |
| 1720 // This setting should override the one above, as it's the same origin. |
| 1721 host_content_settings_map->SetWebsiteSettingDefaultScope( |
| 1722 url2, GURL(), CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, std::string(), |
| 1723 base::WrapUnique(new base::DictionaryValue())); |
| 1724 host_content_settings_map->SetWebsiteSettingDefaultScope( |
| 1725 url3, GURL(), CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, std::string(), |
| 1726 base::WrapUnique(new base::DictionaryValue())); |
| 1727 // Verify we only have two. |
| 1728 host_content_settings_map->GetSettingsForOneType( |
| 1729 CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, std::string(), &host_settings); |
| 1730 EXPECT_EQ(2u, host_settings.size()); |
| 1731 |
| 1732 // Clear the http one, which we should be able to do w/ the origin only, as |
| 1733 // the scope of CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT is |
| 1734 // REQUESTING_ORIGIN_ONLY_SCOPE. |
| 1735 ContentSettingsPattern http_pattern = |
| 1736 ContentSettingsPattern::FromURLNoWildcard(url3_origin_only); |
| 1737 host_content_settings_map->ClearSettingsForOneTypeWithPredicate( |
| 1738 CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, |
| 1739 base::Bind(&MatchPrimaryPattern, http_pattern)); |
| 1740 // Verify we only have one, and it's url1. |
| 1741 host_content_settings_map->GetSettingsForOneType( |
| 1742 CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, std::string(), &host_settings); |
| 1743 EXPECT_EQ(1u, host_settings.size()); |
| 1744 EXPECT_EQ(ContentSettingsPattern::FromURLNoWildcard(url1), |
| 1745 host_settings[0].primary_pattern); |
| 1746 } |
| OLD | NEW |