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

Side by Side Diff: chrome/browser/content_settings/host_content_settings_map_unittest.cc

Issue 2075103002: Change ContentSettingsType's scoping type and hookup migration code (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@do_migration_after_sync
Patch Set: address comments Created 4 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
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 <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
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
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
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
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.
raymes 2016/07/25 02:18:27 nit: say why we are doing this. // Set the pref to
lshang 2016/07/25 04:56:43 Done.
1360 int default_value;
1361 prefs->GetDefaultPrefValue(prefs::kDomainToOriginMigrationStatus)
1362 ->GetAsInteger(&default_value);
1363 prefs->SetInteger(prefs::kDomainToOriginMigrationStatus, default_value);
1358 1364
1359 // Set old formatted http settings. 1365 // Set old formatted http settings.
1360 GURL http_host("http://example.com/"); 1366 GURL http_host("http://example.com/");
1361 GURL http_host_narrower("http://a.example.com/"); 1367 GURL http_host_narrower("http://a.example.com/");
1362 1368
1363 // Change default setting to BLOCK. 1369 // Change default setting to BLOCK.
1364 host_content_settings_map->SetDefaultContentSetting( 1370 host_content_settings_map->SetDefaultContentSetting(
1365 CONTENT_SETTINGS_TYPE_COOKIES, CONTENT_SETTING_BLOCK); 1371 CONTENT_SETTINGS_TYPE_COOKIES, CONTENT_SETTING_BLOCK);
1366 EXPECT_EQ( 1372 EXPECT_EQ(
1367 CONTENT_SETTING_BLOCK, 1373 CONTENT_SETTING_BLOCK,
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
1459 // |host_content_settings_map| contains default setting and a origin scoped 1465 // |host_content_settings_map| contains default setting and a origin scoped
1460 // setting. 1466 // setting.
1461 EXPECT_EQ(2U, settings.size()); 1467 EXPECT_EQ(2U, settings.size());
1462 EXPECT_TRUE(settings[0].primary_pattern.ToString() == 1468 EXPECT_TRUE(settings[0].primary_pattern.ToString() ==
1463 "https://example.com:443"); 1469 "https://example.com:443");
1464 EXPECT_TRUE(settings[1].primary_pattern.ToString() == "*"); 1470 EXPECT_TRUE(settings[1].primary_pattern.ToString() == "*");
1465 } 1471 }
1466 1472
1467 TEST_F(HostContentSettingsMapTest, DomainToOriginMigrationStatus) { 1473 TEST_F(HostContentSettingsMapTest, DomainToOriginMigrationStatus) {
1468 TestingProfile profile; 1474 TestingProfile profile;
1469 1475 // Migration is done on construction of HostContentSettingsMap.
1470 HostContentSettingsMap* host_content_settings_map = 1476 HostContentSettingsMap* host_content_settings_map =
1471 HostContentSettingsMapFactory::GetForProfile(&profile); 1477 HostContentSettingsMapFactory::GetForProfile(&profile);
1472 1478
1473 GURL http_host("http://example.com/"); 1479 GURL http_host("http://example.com/");
1474 GURL http_host_narrower("http://a.example.com/"); 1480 GURL http_host_narrower("http://a.example.com/");
1475 1481
1476 // Change default setting to BLOCK. 1482 // Change default setting to BLOCK.
1477 host_content_settings_map->SetDefaultContentSetting( 1483 host_content_settings_map->SetDefaultContentSetting(
1478 CONTENT_SETTINGS_TYPE_COOKIES, CONTENT_SETTING_BLOCK); 1484 CONTENT_SETTINGS_TYPE_COOKIES, CONTENT_SETTING_BLOCK);
1479 EXPECT_EQ( 1485 EXPECT_EQ(
1480 CONTENT_SETTING_BLOCK, 1486 CONTENT_SETTING_BLOCK,
1481 host_content_settings_map->GetContentSetting( 1487 host_content_settings_map->GetContentSetting(
1482 http_host, http_host, CONTENT_SETTINGS_TYPE_COOKIES, std::string())); 1488 http_host, http_host, CONTENT_SETTINGS_TYPE_COOKIES, std::string()));
1483 // Set domain scoped settings. 1489 // Set domain scoped settings.
1484 host_content_settings_map->SetContentSettingCustomScope( 1490 host_content_settings_map->SetContentSettingCustomScope(
1485 ContentSettingsPattern::FromURL(http_host), 1491 ContentSettingsPattern::FromURL(http_host),
1486 ContentSettingsPattern::Wildcard(), CONTENT_SETTINGS_TYPE_COOKIES, 1492 ContentSettingsPattern::Wildcard(), CONTENT_SETTINGS_TYPE_COOKIES,
1487 std::string(), CONTENT_SETTING_ALLOW); 1493 std::string(), CONTENT_SETTING_ALLOW);
1488 EXPECT_EQ( 1494 EXPECT_EQ(
1489 CONTENT_SETTING_ALLOW, 1495 CONTENT_SETTING_ALLOW,
1490 host_content_settings_map->GetContentSetting( 1496 host_content_settings_map->GetContentSetting(
1491 http_host, http_host, CONTENT_SETTINGS_TYPE_COOKIES, std::string())); 1497 http_host, http_host, CONTENT_SETTINGS_TYPE_COOKIES, std::string()));
1492 // Settings apply to subdomains. 1498 // Settings apply to subdomains.
1493 EXPECT_EQ(CONTENT_SETTING_ALLOW, 1499 EXPECT_EQ(CONTENT_SETTING_ALLOW,
1494 host_content_settings_map->GetContentSetting( 1500 host_content_settings_map->GetContentSetting(
1495 http_host_narrower, http_host_narrower, 1501 http_host_narrower, http_host_narrower,
1496 CONTENT_SETTINGS_TYPE_COOKIES, std::string())); 1502 CONTENT_SETTINGS_TYPE_COOKIES, std::string()));
1497 1503
1498 // Do migration before sync. 1504 // Do migration again before sync.
1499 host_content_settings_map->MigrateDomainScopedSettings(false);
1500
1501 // Settings only apply to origins. Migration got executed.
1502 EXPECT_EQ(CONTENT_SETTING_BLOCK,
1503 host_content_settings_map->GetContentSetting(
1504 http_host_narrower, http_host_narrower,
1505 CONTENT_SETTINGS_TYPE_COOKIES, std::string()));
1506
1507 GURL https_host("https://example.com/");
1508 GURL https_host_narrower("https://a.example.com/");
1509
1510 host_content_settings_map->SetContentSettingCustomScope(
1511 ContentSettingsPattern::FromURL(https_host),
1512 ContentSettingsPattern::Wildcard(), CONTENT_SETTINGS_TYPE_COOKIES,
1513 std::string(), CONTENT_SETTING_ALLOW);
1514 EXPECT_EQ(CONTENT_SETTING_ALLOW,
1515 host_content_settings_map->GetContentSetting(
1516 https_host, https_host, CONTENT_SETTINGS_TYPE_COOKIES,
1517 std::string()));
1518 // Settings apply to subdomains.
1519 EXPECT_EQ(CONTENT_SETTING_ALLOW,
1520 host_content_settings_map->GetContentSetting(
1521 https_host_narrower, https_host_narrower,
1522 CONTENT_SETTINGS_TYPE_COOKIES, std::string()));
1523
1524 // Try to do migration again before sync.
1525 host_content_settings_map->MigrateDomainScopedSettings(false); 1505 host_content_settings_map->MigrateDomainScopedSettings(false);
1526 1506
1527 // Settings still apply to subdomains. Migration didn't get executed. 1507 // Settings still apply to subdomains. Migration didn't get executed.
1528 EXPECT_EQ(CONTENT_SETTING_ALLOW, 1508 EXPECT_EQ(CONTENT_SETTING_ALLOW,
1529 host_content_settings_map->GetContentSetting( 1509 host_content_settings_map->GetContentSetting(
1530 https_host_narrower, https_host_narrower, 1510 http_host_narrower, http_host_narrower,
1531 CONTENT_SETTINGS_TYPE_COOKIES, std::string())); 1511 CONTENT_SETTINGS_TYPE_COOKIES, std::string()));
1532 1512
1533 // Do migration after sync. 1513 // Do migration after sync.
1534 host_content_settings_map->MigrateDomainScopedSettings(true); 1514 host_content_settings_map->MigrateDomainScopedSettings(true);
1535 1515
1536 // Settings only apply to origins. Migration got executed. 1516 // Settings only apply to origins. Migration got executed.
1537 EXPECT_EQ(CONTENT_SETTING_BLOCK, 1517 EXPECT_EQ(CONTENT_SETTING_BLOCK,
1538 host_content_settings_map->GetContentSetting( 1518 host_content_settings_map->GetContentSetting(
1539 https_host_narrower, https_host_narrower, 1519 http_host_narrower, http_host_narrower,
1540 CONTENT_SETTINGS_TYPE_COOKIES, std::string())); 1520 CONTENT_SETTINGS_TYPE_COOKIES, std::string()));
1541 1521
1542 GURL http1_host("http://google.com/"); 1522 GURL http1_host("http://google.com/");
1543 GURL http1_host_narrower("http://a.google.com/"); 1523 GURL http1_host_narrower("http://a.google.com/");
1544 1524
1545 host_content_settings_map->SetContentSettingCustomScope( 1525 host_content_settings_map->SetContentSettingCustomScope(
1546 ContentSettingsPattern::FromURL(http1_host), 1526 ContentSettingsPattern::FromURL(http1_host),
1547 ContentSettingsPattern::Wildcard(), CONTENT_SETTINGS_TYPE_COOKIES, 1527 ContentSettingsPattern::Wildcard(), CONTENT_SETTINGS_TYPE_COOKIES,
1548 std::string(), CONTENT_SETTING_ALLOW); 1528 std::string(), CONTENT_SETTING_ALLOW);
1549 EXPECT_EQ(CONTENT_SETTING_ALLOW, 1529 EXPECT_EQ(CONTENT_SETTING_ALLOW,
(...skipping 27 matching lines...) Expand all
1577 base::DictionaryValue test_value; 1557 base::DictionaryValue test_value;
1578 test_value.SetString("test", "value"); 1558 test_value.SetString("test", "value");
1579 host_content_settings_map->SetWebsiteSettingDefaultScope( 1559 host_content_settings_map->SetWebsiteSettingDefaultScope(
1580 unsupported_url, unsupported_url, CONTENT_SETTINGS_TYPE_APP_BANNER, 1560 unsupported_url, unsupported_url, CONTENT_SETTINGS_TYPE_APP_BANNER,
1581 std::string(), base::WrapUnique(test_value.DeepCopy())); 1561 std::string(), base::WrapUnique(test_value.DeepCopy()));
1582 EXPECT_EQ(nullptr, 1562 EXPECT_EQ(nullptr,
1583 host_content_settings_map->GetWebsiteSetting( 1563 host_content_settings_map->GetWebsiteSetting(
1584 unsupported_url, unsupported_url, 1564 unsupported_url, unsupported_url,
1585 CONTENT_SETTINGS_TYPE_APP_BANNER, std::string(), nullptr)); 1565 CONTENT_SETTINGS_TYPE_APP_BANNER, std::string(), nullptr));
1586 } 1566 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698