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 350 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
361 } | 361 } |
362 | 362 |
363 TEST_F(HostContentSettingsMapTest, Observer) { | 363 TEST_F(HostContentSettingsMapTest, Observer) { |
364 TestingProfile profile; | 364 TestingProfile profile; |
365 HostContentSettingsMap* host_content_settings_map = | 365 HostContentSettingsMap* host_content_settings_map = |
366 HostContentSettingsMapFactory::GetForProfile(&profile); | 366 HostContentSettingsMapFactory::GetForProfile(&profile); |
367 MockSettingsObserver observer(host_content_settings_map); | 367 MockSettingsObserver observer(host_content_settings_map); |
368 | 368 |
369 GURL host("http://example.com/"); | 369 GURL host("http://example.com/"); |
370 ContentSettingsPattern primary_pattern = | 370 ContentSettingsPattern primary_pattern = |
371 ContentSettingsPattern::FromString("[*.]example.com"); | 371 ContentSettingsPattern::FromString("http://example.com:80"); |
372 ContentSettingsPattern secondary_pattern = | 372 ContentSettingsPattern secondary_pattern = |
373 ContentSettingsPattern::Wildcard(); | 373 ContentSettingsPattern::Wildcard(); |
374 EXPECT_CALL(observer, OnContentSettingsChanged(host_content_settings_map, | 374 EXPECT_CALL(observer, OnContentSettingsChanged(host_content_settings_map, |
375 CONTENT_SETTINGS_TYPE_COOKIES, | 375 CONTENT_SETTINGS_TYPE_COOKIES, |
376 false, primary_pattern, | 376 false, primary_pattern, |
377 secondary_pattern, false)); | 377 secondary_pattern, false)); |
378 host_content_settings_map->SetContentSettingDefaultScope( | 378 host_content_settings_map->SetContentSettingDefaultScope( |
379 host, GURL(), CONTENT_SETTINGS_TYPE_COOKIES, std::string(), | 379 host, GURL(), CONTENT_SETTINGS_TYPE_COOKIES, std::string(), |
380 CONTENT_SETTING_ALLOW); | 380 CONTENT_SETTING_ALLOW); |
381 ::testing::Mock::VerifyAndClearExpectations(&observer); | 381 ::testing::Mock::VerifyAndClearExpectations(&observer); |
(...skipping 560 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
942 | 942 |
943 // Set utf-8 data. | 943 // Set utf-8 data. |
944 { | 944 { |
945 DictionaryPrefUpdate update(prefs, | 945 DictionaryPrefUpdate update(prefs, |
946 GetPrefName(CONTENT_SETTINGS_TYPE_COOKIES)); | 946 GetPrefName(CONTENT_SETTINGS_TYPE_COOKIES)); |
947 base::DictionaryValue* all_settings_dictionary = update.Get(); | 947 base::DictionaryValue* all_settings_dictionary = update.Get(); |
948 ASSERT_TRUE(NULL != all_settings_dictionary); | 948 ASSERT_TRUE(NULL != all_settings_dictionary); |
949 | 949 |
950 base::DictionaryValue* dummy_payload = new base::DictionaryValue; | 950 base::DictionaryValue* dummy_payload = new base::DictionaryValue; |
951 dummy_payload->SetInteger("setting", CONTENT_SETTING_ALLOW); | 951 dummy_payload->SetInteger("setting", CONTENT_SETTING_ALLOW); |
952 all_settings_dictionary->SetWithoutPathExpansion("[*.]\xC4\x87ira.com,*", | 952 all_settings_dictionary->SetWithoutPathExpansion("www.\xC4\x87ira.com,*", |
953 dummy_payload); | 953 dummy_payload); |
954 } | 954 } |
955 | 955 |
956 HostContentSettingsMapFactory::GetForProfile(&profile); | 956 HostContentSettingsMapFactory::GetForProfile(&profile); |
957 | 957 |
958 const base::DictionaryValue* all_settings_dictionary = | 958 const base::DictionaryValue* all_settings_dictionary = |
959 prefs->GetDictionary(GetPrefName(CONTENT_SETTINGS_TYPE_COOKIES)); | 959 prefs->GetDictionary(GetPrefName(CONTENT_SETTINGS_TYPE_COOKIES)); |
960 const base::DictionaryValue* result = NULL; | 960 const base::DictionaryValue* result = NULL; |
961 EXPECT_FALSE(all_settings_dictionary->GetDictionaryWithoutPathExpansion( | 961 EXPECT_FALSE(all_settings_dictionary->GetDictionaryWithoutPathExpansion( |
962 "[*.]\xC4\x87ira.com,*", &result)); | 962 "www.\xC4\x87ira.com,*", &result)); |
963 EXPECT_TRUE(all_settings_dictionary->GetDictionaryWithoutPathExpansion( | 963 EXPECT_TRUE(all_settings_dictionary->GetDictionaryWithoutPathExpansion( |
964 "[*.]xn--ira-ppa.com,*", &result)); | 964 "www.xn--ira-ppa.com,*", &result)); |
965 } | 965 } |
966 | 966 |
967 // If both Unicode and its punycode pattern exist, make sure we don't touch the | 967 // If both Unicode and its punycode pattern exist, make sure we don't touch the |
968 // settings for the punycode, and that Unicode pattern gets deleted. | 968 // settings for the punycode, and that Unicode pattern gets deleted. |
969 TEST_F(HostContentSettingsMapTest, CanonicalizeExceptionsUnicodeAndPunycode) { | 969 TEST_F(HostContentSettingsMapTest, CanonicalizeExceptionsUnicodeAndPunycode) { |
970 TestingProfile profile; | 970 TestingProfile profile; |
971 | 971 |
972 std::unique_ptr<base::Value> value = | 972 std::unique_ptr<base::Value> value = |
973 base::JSONReader::Read("{\"[*.]\\xC4\\x87ira.com,*\":{\"setting\":1}}"); | 973 base::JSONReader::Read("{\"[*.]\\xC4\\x87ira.com,*\":{\"setting\":1}}"); |
974 profile.GetPrefs()->Set(GetPrefName(CONTENT_SETTINGS_TYPE_COOKIES), *value); | 974 profile.GetPrefs()->Set(GetPrefName(CONTENT_SETTINGS_TYPE_COOKIES), *value); |
(...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1207 } | 1207 } |
1208 | 1208 |
1209 TEST_F(HostContentSettingsMapTest, AddContentSettingsObserver) { | 1209 TEST_F(HostContentSettingsMapTest, AddContentSettingsObserver) { |
1210 TestingProfile profile; | 1210 TestingProfile profile; |
1211 HostContentSettingsMap* host_content_settings_map = | 1211 HostContentSettingsMap* host_content_settings_map = |
1212 HostContentSettingsMapFactory::GetForProfile(&profile); | 1212 HostContentSettingsMapFactory::GetForProfile(&profile); |
1213 content_settings::MockObserver mock_observer; | 1213 content_settings::MockObserver mock_observer; |
1214 | 1214 |
1215 GURL host("http://example.com/"); | 1215 GURL host("http://example.com/"); |
1216 ContentSettingsPattern pattern = | 1216 ContentSettingsPattern pattern = |
1217 ContentSettingsPattern::FromString("[*.]example.com"); | 1217 ContentSettingsPattern::FromString("http://example.com:80"); |
1218 EXPECT_CALL(mock_observer, OnContentSettingChanged( | 1218 EXPECT_CALL(mock_observer, OnContentSettingChanged( |
1219 pattern, ContentSettingsPattern::Wildcard(), | 1219 pattern, ContentSettingsPattern::Wildcard(), |
1220 CONTENT_SETTINGS_TYPE_COOKIES, "")); | 1220 CONTENT_SETTINGS_TYPE_COOKIES, "")); |
1221 | 1221 |
1222 host_content_settings_map->AddObserver(&mock_observer); | 1222 host_content_settings_map->AddObserver(&mock_observer); |
1223 | 1223 |
1224 EXPECT_EQ(CONTENT_SETTING_ALLOW, | 1224 EXPECT_EQ(CONTENT_SETTING_ALLOW, |
1225 host_content_settings_map->GetContentSetting( | 1225 host_content_settings_map->GetContentSetting( |
1226 host, host, CONTENT_SETTINGS_TYPE_COOKIES, std::string())); | 1226 host, host, CONTENT_SETTINGS_TYPE_COOKIES, std::string())); |
1227 host_content_settings_map->SetContentSettingDefaultScope( | 1227 host_content_settings_map->SetContentSettingDefaultScope( |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1348 CONTENT_SETTING_BLOCK); | 1348 CONTENT_SETTING_BLOCK); |
1349 EXPECT_EQ(CONTENT_SETTING_BLOCK, | 1349 EXPECT_EQ(CONTENT_SETTING_BLOCK, |
1350 host_content_settings_map->GetContentSetting( | 1350 host_content_settings_map->GetContentSetting( |
1351 host, host, CONTENT_SETTINGS_TYPE_KEYGEN, std::string())); | 1351 host, host, CONTENT_SETTINGS_TYPE_KEYGEN, std::string())); |
1352 } | 1352 } |
1353 | 1353 |
1354 TEST_F(HostContentSettingsMapTest, MigrateDomainScopedSettings) { | 1354 TEST_F(HostContentSettingsMapTest, MigrateDomainScopedSettings) { |
1355 TestingProfile profile; | 1355 TestingProfile profile; |
1356 HostContentSettingsMap* host_content_settings_map = | 1356 HostContentSettingsMap* host_content_settings_map = |
1357 HostContentSettingsMapFactory::GetForProfile(&profile); | 1357 HostContentSettingsMapFactory::GetForProfile(&profile); |
| 1358 PrefService* prefs = profile.GetPrefs(); |
| 1359 // Set the pref to its initial state so that migration can be done later in |
| 1360 // the test (normally it is done on construction of HostContentSettingsMap). |
| 1361 int default_value; |
| 1362 prefs->GetDefaultPrefValue(prefs::kDomainToOriginMigrationStatus) |
| 1363 ->GetAsInteger(&default_value); |
| 1364 prefs->SetInteger(prefs::kDomainToOriginMigrationStatus, default_value); |
1358 | 1365 |
1359 // Set old formatted http settings. | 1366 // Set old formatted http settings. |
1360 GURL http_host("http://example.com/"); | 1367 GURL http_host("http://example.com/"); |
1361 GURL http_host_narrower("http://a.example.com/"); | 1368 GURL http_host_narrower("http://a.example.com/"); |
1362 | 1369 |
1363 // Change default setting to BLOCK. | 1370 // Change default setting to BLOCK. |
1364 host_content_settings_map->SetDefaultContentSetting( | 1371 host_content_settings_map->SetDefaultContentSetting( |
1365 CONTENT_SETTINGS_TYPE_COOKIES, CONTENT_SETTING_BLOCK); | 1372 CONTENT_SETTINGS_TYPE_COOKIES, CONTENT_SETTING_BLOCK); |
1366 EXPECT_EQ( | 1373 EXPECT_EQ( |
1367 CONTENT_SETTING_BLOCK, | 1374 CONTENT_SETTING_BLOCK, |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1461 EXPECT_EQ(2U, settings.size()); | 1468 EXPECT_EQ(2U, settings.size()); |
1462 EXPECT_TRUE(settings[0].primary_pattern.ToString() == | 1469 EXPECT_TRUE(settings[0].primary_pattern.ToString() == |
1463 "https://example.com:443"); | 1470 "https://example.com:443"); |
1464 EXPECT_TRUE(settings[1].primary_pattern.ToString() == "*"); | 1471 EXPECT_TRUE(settings[1].primary_pattern.ToString() == "*"); |
1465 } | 1472 } |
1466 | 1473 |
1467 // Ensure that migration only happens once upon construction of the HCSM and | 1474 // Ensure that migration only happens once upon construction of the HCSM and |
1468 // once after syncing (even when these events occur multiple times). | 1475 // once after syncing (even when these events occur multiple times). |
1469 TEST_F(HostContentSettingsMapTest, DomainToOriginMigrationStatus) { | 1476 TEST_F(HostContentSettingsMapTest, DomainToOriginMigrationStatus) { |
1470 TestingProfile profile; | 1477 TestingProfile profile; |
1471 | 1478 PrefService* prefs = profile.GetPrefs(); |
1472 HostContentSettingsMap* host_content_settings_map = | |
1473 HostContentSettingsMapFactory::GetForProfile(&profile); | |
1474 | 1479 |
1475 GURL http_host("http://example.com/"); | 1480 GURL http_host("http://example.com/"); |
1476 GURL http_host_narrower("http://a.example.com/"); | 1481 GURL http_host_narrower("http://a.example.com/"); |
| 1482 std::string host_pattern_string = |
| 1483 ContentSettingsPattern::FromURL(http_host).ToString(); |
| 1484 |
| 1485 { |
| 1486 DictionaryPrefUpdate update(prefs, |
| 1487 GetPrefName(CONTENT_SETTINGS_TYPE_COOKIES)); |
| 1488 base::DictionaryValue* all_settings_dictionary = update.Get(); |
| 1489 ASSERT_TRUE(NULL != all_settings_dictionary); |
| 1490 |
| 1491 base::DictionaryValue* domain_setting = new base::DictionaryValue; |
| 1492 domain_setting->SetInteger("setting", CONTENT_SETTING_ALLOW); |
| 1493 all_settings_dictionary->SetWithoutPathExpansion(host_pattern_string + ",*", |
| 1494 domain_setting); |
| 1495 } |
| 1496 |
| 1497 const base::DictionaryValue* all_settings_dictionary = |
| 1498 prefs->GetDictionary(GetPrefName(CONTENT_SETTINGS_TYPE_COOKIES)); |
| 1499 const base::DictionaryValue* result = NULL; |
| 1500 EXPECT_TRUE(all_settings_dictionary->GetDictionaryWithoutPathExpansion( |
| 1501 "[*.]example.com,*", &result)); |
| 1502 |
| 1503 // Migration is done on construction of HostContentSettingsMap. |
| 1504 HostContentSettingsMap* host_content_settings_map = |
| 1505 HostContentSettingsMapFactory::GetForProfile(&profile); |
1477 | 1506 |
1478 // Change default setting to BLOCK. | 1507 // Change default setting to BLOCK. |
1479 host_content_settings_map->SetDefaultContentSetting( | 1508 host_content_settings_map->SetDefaultContentSetting( |
1480 CONTENT_SETTINGS_TYPE_COOKIES, CONTENT_SETTING_BLOCK); | 1509 CONTENT_SETTINGS_TYPE_COOKIES, CONTENT_SETTING_BLOCK); |
1481 // Set domain scoped settings. | |
1482 host_content_settings_map->SetContentSettingCustomScope( | |
1483 ContentSettingsPattern::FromURL(http_host), | |
1484 ContentSettingsPattern::Wildcard(), CONTENT_SETTINGS_TYPE_COOKIES, | |
1485 std::string(), CONTENT_SETTING_ALLOW); | |
1486 EXPECT_EQ( | 1510 EXPECT_EQ( |
1487 CONTENT_SETTING_ALLOW, | 1511 CONTENT_SETTING_ALLOW, |
1488 host_content_settings_map->GetContentSetting( | 1512 host_content_settings_map->GetContentSetting( |
1489 http_host, http_host, CONTENT_SETTINGS_TYPE_COOKIES, std::string())); | 1513 http_host, http_host, CONTENT_SETTINGS_TYPE_COOKIES, std::string())); |
1490 // Settings apply to subdomains. | |
1491 EXPECT_EQ(CONTENT_SETTING_ALLOW, | |
1492 host_content_settings_map->GetContentSetting( | |
1493 http_host_narrower, http_host_narrower, | |
1494 CONTENT_SETTINGS_TYPE_COOKIES, std::string())); | |
1495 | |
1496 // Do migration before sync. | |
1497 host_content_settings_map->MigrateDomainScopedSettings(false); | |
1498 | |
1499 // Settings only apply to origins. Migration got executed. | 1514 // Settings only apply to origins. Migration got executed. |
1500 EXPECT_EQ(CONTENT_SETTING_BLOCK, | 1515 EXPECT_EQ(CONTENT_SETTING_BLOCK, |
1501 host_content_settings_map->GetContentSetting( | 1516 host_content_settings_map->GetContentSetting( |
1502 http_host_narrower, http_host_narrower, | 1517 http_host_narrower, http_host_narrower, |
1503 CONTENT_SETTINGS_TYPE_COOKIES, std::string())); | 1518 CONTENT_SETTINGS_TYPE_COOKIES, std::string())); |
1504 | 1519 |
1505 GURL https_host("https://example.com/"); | 1520 GURL https_host("https://example.com/"); |
1506 GURL https_host_narrower("https://a.example.com/"); | 1521 GURL https_host_narrower("https://a.example.com/"); |
1507 | 1522 |
1508 host_content_settings_map->SetContentSettingCustomScope( | 1523 host_content_settings_map->SetContentSettingCustomScope( |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1575 base::DictionaryValue test_value; | 1590 base::DictionaryValue test_value; |
1576 test_value.SetString("test", "value"); | 1591 test_value.SetString("test", "value"); |
1577 host_content_settings_map->SetWebsiteSettingDefaultScope( | 1592 host_content_settings_map->SetWebsiteSettingDefaultScope( |
1578 unsupported_url, unsupported_url, CONTENT_SETTINGS_TYPE_APP_BANNER, | 1593 unsupported_url, unsupported_url, CONTENT_SETTINGS_TYPE_APP_BANNER, |
1579 std::string(), base::WrapUnique(test_value.DeepCopy())); | 1594 std::string(), base::WrapUnique(test_value.DeepCopy())); |
1580 EXPECT_EQ(nullptr, | 1595 EXPECT_EQ(nullptr, |
1581 host_content_settings_map->GetWebsiteSetting( | 1596 host_content_settings_map->GetWebsiteSetting( |
1582 unsupported_url, unsupported_url, | 1597 unsupported_url, unsupported_url, |
1583 CONTENT_SETTINGS_TYPE_APP_BANNER, std::string(), nullptr)); | 1598 CONTENT_SETTINGS_TYPE_APP_BANNER, std::string(), nullptr)); |
1584 } | 1599 } |
OLD | NEW |