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

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: revise test 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.
1360 prefs->SetInteger(prefs::kDomainToOriginMigrationStatus, 0);
raymes 2016/07/21 01:13:57 Maybe we can set it to the default value: prefs->S
lshang 2016/07/22 02:58:38 Done.
1358 1361
1359 // Set old formatted http settings. 1362 // Set old formatted http settings.
1360 GURL http_host("http://example.com/"); 1363 GURL http_host("http://example.com/");
1361 GURL http_host_narrower("http://a.example.com/"); 1364 GURL http_host_narrower("http://a.example.com/");
1362 1365
1363 // Change default setting to BLOCK. 1366 // Change default setting to BLOCK.
1364 host_content_settings_map->SetDefaultContentSetting( 1367 host_content_settings_map->SetDefaultContentSetting(
1365 CONTENT_SETTINGS_TYPE_COOKIES, CONTENT_SETTING_BLOCK); 1368 CONTENT_SETTINGS_TYPE_COOKIES, CONTENT_SETTING_BLOCK);
1366 EXPECT_EQ( 1369 EXPECT_EQ(
1367 CONTENT_SETTING_BLOCK, 1370 CONTENT_SETTING_BLOCK,
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
1455 https_host_narrower, https_host_narrower, 1458 https_host_narrower, https_host_narrower,
1456 CONTENT_SETTINGS_TYPE_POPUPS, std::string())); 1459 CONTENT_SETTINGS_TYPE_POPUPS, std::string()));
1457 host_content_settings_map->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_POPUPS, 1460 host_content_settings_map->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_POPUPS,
1458 std::string(), &settings); 1461 std::string(), &settings);
1459 // |host_content_settings_map| contains default setting and a origin scoped 1462 // |host_content_settings_map| contains default setting and a origin scoped
1460 // setting. 1463 // setting.
1461 EXPECT_EQ(2U, settings.size()); 1464 EXPECT_EQ(2U, settings.size());
1462 EXPECT_TRUE(settings[0].primary_pattern.ToString() == 1465 EXPECT_TRUE(settings[0].primary_pattern.ToString() ==
1463 "https://example.com:443"); 1466 "https://example.com:443");
1464 EXPECT_TRUE(settings[1].primary_pattern.ToString() == "*"); 1467 EXPECT_TRUE(settings[1].primary_pattern.ToString() == "*");
1468
1469 prefs->SetInteger(prefs::kDomainToOriginMigrationStatus, 0);
raymes 2016/07/21 01:13:57 nit: this isn't needed. The test state should get
lshang 2016/07/22 02:58:38 Done.
1465 } 1470 }
1466 1471
1467 TEST_F(HostContentSettingsMapTest, DomainToOriginMigrationStatus) { 1472 TEST_F(HostContentSettingsMapTest, DomainToOriginMigrationStatus) {
1468 TestingProfile profile; 1473 TestingProfile profile;
1469 PrefService* prefs = profile.GetPrefs(); 1474 PrefService* prefs = profile.GetPrefs();
1470
1471 HostContentSettingsMap* host_content_settings_map =
1472 HostContentSettingsMapFactory::GetForProfile(&profile);
1473
1474 // Initial state is NOT_MIGRATED. 1475 // Initial state is NOT_MIGRATED.
1475 EXPECT_EQ(0, prefs->GetInteger(prefs::kDomainToOriginMigrationStatus)); 1476 EXPECT_EQ(0, prefs->GetInteger(prefs::kDomainToOriginMigrationStatus));
1476 1477
1477 // Do migration on HostContentSettingsMap construction will change the state 1478 // Do migration on HostContentSettingsMap construction will change the state
1478 // to MIGRATED_BEFORE_SYNC. 1479 // to MIGRATED_BEFORE_SYNC.
1479 host_content_settings_map->MigrateDomainScopedSettings(false); 1480 HostContentSettingsMap* host_content_settings_map =
1481 HostContentSettingsMapFactory::GetForProfile(&profile);
1480 EXPECT_EQ(1, prefs->GetInteger(prefs::kDomainToOriginMigrationStatus)); 1482 EXPECT_EQ(1, prefs->GetInteger(prefs::kDomainToOriginMigrationStatus));
1481 1483
1482 // Another call on HostContentSettingsMap construction to do migration will 1484 // Another call on HostContentSettingsMap construction to do migration will
1483 // not change the state(i.e. migration code don't acturally get executed). 1485 // not change the state(i.e. migration code don't acturally get executed).
1484 host_content_settings_map->MigrateDomainScopedSettings(false); 1486 host_content_settings_map->MigrateDomainScopedSettings(false);
1485 EXPECT_EQ(1, prefs->GetInteger(prefs::kDomainToOriginMigrationStatus)); 1487 EXPECT_EQ(1, prefs->GetInteger(prefs::kDomainToOriginMigrationStatus));
1486 1488
1487 // Do migration after sync will change the state to MIGRATED_AFTER_SYNC. 1489 // Do migration after sync will change the state to MIGRATED_AFTER_SYNC.
1488 host_content_settings_map->MigrateDomainScopedSettings(true); 1490 host_content_settings_map->MigrateDomainScopedSettings(true);
1489 EXPECT_EQ(2, prefs->GetInteger(prefs::kDomainToOriginMigrationStatus)); 1491 EXPECT_EQ(2, prefs->GetInteger(prefs::kDomainToOriginMigrationStatus));
1490 1492
1491 // Another call after sync to do migration will not change the state 1493 // Another call after sync to do migration will not change the state
1492 // (i.e. migration has finished, won't get executed anymore). 1494 // (i.e. migration has finished, won't get executed anymore).
1493 host_content_settings_map->MigrateDomainScopedSettings(true); 1495 host_content_settings_map->MigrateDomainScopedSettings(true);
1494 EXPECT_EQ(2, prefs->GetInteger(prefs::kDomainToOriginMigrationStatus)); 1496 EXPECT_EQ(2, prefs->GetInteger(prefs::kDomainToOriginMigrationStatus));
1497
1498 prefs->SetInteger(prefs::kDomainToOriginMigrationStatus, 0);
raymes 2016/07/21 01:13:57 nit: this isn't needed
lshang 2016/07/22 02:58:38 Done.
1495 } 1499 }
1496 1500
1497 TEST_F(HostContentSettingsMapTest, InvalidPattern) { 1501 TEST_F(HostContentSettingsMapTest, InvalidPattern) {
1498 // This is a regression test for crbug.com/618529, which fixed a memory leak 1502 // This is a regression test for crbug.com/618529, which fixed a memory leak
1499 // when a website setting was set under a URL that mapped to an invalid 1503 // when a website setting was set under a URL that mapped to an invalid
1500 // pattern. 1504 // pattern.
1501 TestingProfile profile; 1505 TestingProfile profile;
1502 HostContentSettingsMap* host_content_settings_map = 1506 HostContentSettingsMap* host_content_settings_map =
1503 HostContentSettingsMapFactory::GetForProfile(&profile); 1507 HostContentSettingsMapFactory::GetForProfile(&profile);
1504 GURL unsupported_url = GURL("view-source:http://www.google.com"); 1508 GURL unsupported_url = GURL("view-source:http://www.google.com");
1505 base::DictionaryValue test_value; 1509 base::DictionaryValue test_value;
1506 test_value.SetString("test", "value"); 1510 test_value.SetString("test", "value");
1507 host_content_settings_map->SetWebsiteSettingDefaultScope( 1511 host_content_settings_map->SetWebsiteSettingDefaultScope(
1508 unsupported_url, unsupported_url, CONTENT_SETTINGS_TYPE_APP_BANNER, 1512 unsupported_url, unsupported_url, CONTENT_SETTINGS_TYPE_APP_BANNER,
1509 std::string(), base::WrapUnique(test_value.DeepCopy())); 1513 std::string(), base::WrapUnique(test_value.DeepCopy()));
1510 EXPECT_EQ(nullptr, 1514 EXPECT_EQ(nullptr,
1511 host_content_settings_map->GetWebsiteSetting( 1515 host_content_settings_map->GetWebsiteSetting(
1512 unsupported_url, unsupported_url, 1516 unsupported_url, unsupported_url,
1513 CONTENT_SETTINGS_TYPE_APP_BANNER, std::string(), nullptr)); 1517 CONTENT_SETTINGS_TYPE_APP_BANNER, std::string(), nullptr));
1514 } 1518 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698