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

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

Issue 2612993002: Make geolocation API and X-Geo header access consistent (Closed)
Patch Set: Remove stuff which shouldn't be there Created 3 years, 11 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"
11 #include "base/json/json_writer.h" 11 #include "base/json/json_writer.h"
12 #include "base/memory/ptr_util.h" 12 #include "base/memory/ptr_util.h"
13 #include "base/message_loop/message_loop.h" 13 #include "base/message_loop/message_loop.h"
14 #include "chrome/browser/content_settings/content_settings_mock_observer.h" 14 #include "chrome/browser/content_settings/content_settings_mock_observer.h"
15 #include "chrome/browser/content_settings/cookie_settings_factory.h" 15 #include "chrome/browser/content_settings/cookie_settings_factory.h"
16 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" 16 #include "chrome/browser/content_settings/host_content_settings_map_factory.h"
17 #include "chrome/browser/content_settings/mock_settings_observer.h" 17 #include "chrome/browser/content_settings/mock_settings_observer.h"
18 #include "chrome/common/url_constants.h" 18 #include "chrome/common/url_constants.h"
19 #include "chrome/test/base/testing_profile.h" 19 #include "chrome/test/base/testing_profile.h"
20 #include "components/content_settings/core/browser/content_settings_details.h" 20 #include "components/content_settings/core/browser/content_settings_details.h"
21 #include "components/content_settings/core/browser/cookie_settings.h" 21 #include "components/content_settings/core/browser/cookie_settings.h"
22 #include "components/content_settings/core/browser/host_content_settings_map.h" 22 #include "components/content_settings/core/browser/host_content_settings_map.h"
23 #include "components/content_settings/core/browser/website_settings_info.h" 23 #include "components/content_settings/core/browser/website_settings_info.h"
24 #include "components/content_settings/core/browser/website_settings_registry.h" 24 #include "components/content_settings/core/browser/website_settings_registry.h"
25 #include "components/content_settings/core/common/pref_names.h" 25 #include "components/content_settings/core/common/pref_names.h"
26 #include "components/policy/core/common/policy_service.h"
26 #include "components/prefs/pref_service.h" 27 #include "components/prefs/pref_service.h"
27 #include "components/prefs/scoped_user_pref_update.h" 28 #include "components/prefs/scoped_user_pref_update.h"
28 #include "components/sync_preferences/testing_pref_service_syncable.h" 29 #include "components/sync_preferences/testing_pref_service_syncable.h"
29 #include "content/public/test/test_browser_thread.h" 30 #include "content/public/test/test_browser_thread.h"
30 #include "net/base/static_cookie_policy.h" 31 #include "net/base/static_cookie_policy.h"
31 #include "ppapi/features/features.h" 32 #include "ppapi/features/features.h"
32 #include "testing/gtest/include/gtest/gtest.h" 33 #include "testing/gtest/include/gtest/gtest.h"
33 #include "url/gurl.h" 34 #include "url/gurl.h"
34 35
36 #if BUILDFLAG(ENABLE_EXTENSIONS)
37 #include "chrome/browser/extensions/extension_special_storage_policy.h"
38 #endif
raymes 2017/01/11 08:30:12 Are these 2 includes above needed?
benwells 2017/01/11 09:26:11 Yes, they are needed because two of the nullptrs p
39
35 using content::BrowserThread; 40 using content::BrowserThread;
36 41
37 using ::testing::_; 42 using ::testing::_;
38 43
39 namespace { 44 namespace {
40 45
41 bool MatchPrimaryPattern(const ContentSettingsPattern& expected_primary, 46 bool MatchPrimaryPattern(const ContentSettingsPattern& expected_primary,
42 const ContentSettingsPattern& primary_pattern, 47 const ContentSettingsPattern& primary_pattern,
43 const ContentSettingsPattern& secondary_pattern) { 48 const ContentSettingsPattern& secondary_pattern) {
44 return expected_primary == primary_pattern; 49 return expected_primary == primary_pattern;
(...skipping 1198 matching lines...) Expand 10 before | Expand all | Expand 10 after
1243 host_content_settings_map->AddObserver(&mock_observer); 1248 host_content_settings_map->AddObserver(&mock_observer);
1244 1249
1245 EXPECT_EQ(CONTENT_SETTING_ALLOW, 1250 EXPECT_EQ(CONTENT_SETTING_ALLOW,
1246 host_content_settings_map->GetContentSetting( 1251 host_content_settings_map->GetContentSetting(
1247 host, host, CONTENT_SETTINGS_TYPE_COOKIES, std::string())); 1252 host, host, CONTENT_SETTINGS_TYPE_COOKIES, std::string()));
1248 host_content_settings_map->SetContentSettingDefaultScope( 1253 host_content_settings_map->SetContentSettingDefaultScope(
1249 host, GURL(), CONTENT_SETTINGS_TYPE_COOKIES, std::string(), 1254 host, GURL(), CONTENT_SETTINGS_TYPE_COOKIES, std::string(),
1250 CONTENT_SETTING_DEFAULT); 1255 CONTENT_SETTING_DEFAULT);
1251 } 1256 }
1252 1257
1253 TEST_F(HostContentSettingsMapTest, GuestProfile) { 1258 TEST_F(HostContentSettingsMapTest, GuestProfile) {
msramek 2017/01/11 09:51:10 The guest profile is Desktop only (unless I'm real
1254 TestingProfile profile; 1259 TestingProfile profile(base::FilePath(),
1255 profile.SetGuestSession(true); 1260 #if BUILDFLAG(ENABLE_EXTENSIONS)
1261 nullptr,
1262 #endif
1263 nullptr, nullptr, nullptr, true /* guest_profile */,
1264 "", nullptr, TestingProfile::TestingFactories(),
raymes 2017/01/11 08:30:12 nit: std::string() for empty strings
1265 "testing_profile");
1256 HostContentSettingsMap* host_content_settings_map = 1266 HostContentSettingsMap* host_content_settings_map =
1257 HostContentSettingsMapFactory::GetForProfile(&profile); 1267 HostContentSettingsMapFactory::GetForProfile(&profile);
1258 1268
1259 GURL host("http://example.com/"); 1269 GURL host("http://example.com/");
1260 ContentSettingsPattern pattern = 1270 ContentSettingsPattern pattern =
1261 ContentSettingsPattern::FromString("[*.]example.com"); 1271 ContentSettingsPattern::FromString("[*.]example.com");
1262 1272
1263 EXPECT_EQ(CONTENT_SETTING_ALLOW, 1273 EXPECT_EQ(CONTENT_SETTING_ALLOW,
1264 host_content_settings_map->GetContentSetting( 1274 host_content_settings_map->GetContentSetting(
1265 host, host, CONTENT_SETTINGS_TYPE_COOKIES, std::string())); 1275 host, host, CONTENT_SETTINGS_TYPE_COOKIES, std::string()));
1266 1276
1267 // Changing content settings should not result in any prefs being stored 1277 // Changing content settings should not result in any prefs being stored
1268 // however the value should be set in memory. 1278 // however the value should be set in memory.
1269 host_content_settings_map->SetContentSettingDefaultScope( 1279 host_content_settings_map->SetContentSettingDefaultScope(
1270 host, GURL(), CONTENT_SETTINGS_TYPE_COOKIES, std::string(), 1280 host, GURL(), CONTENT_SETTINGS_TYPE_COOKIES, std::string(),
1271 CONTENT_SETTING_BLOCK); 1281 CONTENT_SETTING_BLOCK);
1272 EXPECT_EQ(CONTENT_SETTING_BLOCK, 1282 EXPECT_EQ(CONTENT_SETTING_BLOCK,
1273 host_content_settings_map->GetContentSetting( 1283 host_content_settings_map->GetContentSetting(
1274 host, host, CONTENT_SETTINGS_TYPE_COOKIES, std::string())); 1284 host, host, CONTENT_SETTINGS_TYPE_COOKIES, std::string()));
1275 1285
1276 const base::DictionaryValue* all_settings_dictionary = 1286 const base::DictionaryValue* all_settings_dictionary =
1277 profile.GetPrefs()->GetDictionary( 1287 profile.GetPrefs()->GetDictionary(
1278 GetPrefName(CONTENT_SETTINGS_TYPE_COOKIES)); 1288 GetPrefName(CONTENT_SETTINGS_TYPE_COOKIES));
1279 EXPECT_TRUE(all_settings_dictionary->empty()); 1289 EXPECT_TRUE(all_settings_dictionary->empty());
1280 } 1290 }
1281 1291
1282 // Default settings should not be modifiable for the guest profile (there is no 1292 // Default settings should not be modifiable for the guest profile (there is no
1283 // UI to do this). 1293 // UI to do this).
1284 TEST_F(HostContentSettingsMapTest, GuestProfileDefaultSetting) { 1294 TEST_F(HostContentSettingsMapTest, GuestProfileDefaultSetting) {
1285 TestingProfile profile; 1295 TestingProfile profile(base::FilePath(),
1286 profile.SetGuestSession(true); 1296 #if BUILDFLAG(ENABLE_EXTENSIONS)
1297 nullptr,
1298 #endif
1299 nullptr, nullptr, nullptr, true /* guest_profile */,
1300 "", nullptr, TestingProfile::TestingFactories(),
1301 "testing_profile");
1287 HostContentSettingsMap* host_content_settings_map = 1302 HostContentSettingsMap* host_content_settings_map =
1288 HostContentSettingsMapFactory::GetForProfile(&profile); 1303 HostContentSettingsMapFactory::GetForProfile(&profile);
1289 1304
1290 GURL host("http://example.com/"); 1305 GURL host("http://example.com/");
1291 1306
1292 // There are no custom rules, so this should be the default. 1307 // There are no custom rules, so this should be the default.
1293 EXPECT_EQ(CONTENT_SETTING_ALLOW, 1308 EXPECT_EQ(CONTENT_SETTING_ALLOW,
1294 host_content_settings_map->GetContentSetting( 1309 host_content_settings_map->GetContentSetting(
1295 host, host, CONTENT_SETTINGS_TYPE_COOKIES, std::string())); 1310 host, host, CONTENT_SETTINGS_TYPE_COOKIES, std::string()));
1296 1311
1297 host_content_settings_map->SetDefaultContentSetting( 1312 host_content_settings_map->SetDefaultContentSetting(
1298 CONTENT_SETTINGS_TYPE_COOKIES, CONTENT_SETTING_BLOCK); 1313 CONTENT_SETTINGS_TYPE_COOKIES, CONTENT_SETTING_BLOCK);
1299 1314
1300 EXPECT_EQ(CONTENT_SETTING_ALLOW, 1315 EXPECT_EQ(CONTENT_SETTING_ALLOW,
1301 host_content_settings_map->GetContentSetting( 1316 host_content_settings_map->GetContentSetting(
1302 host, host, CONTENT_SETTINGS_TYPE_COOKIES, std::string())); 1317 host, host, CONTENT_SETTINGS_TYPE_COOKIES, std::string()));
1303 } 1318 }
1304 1319
1305 // We used to incorrectly store content settings in prefs for the guest profile. 1320 // We used to incorrectly store content settings in prefs for the guest profile.
1306 // We need to ensure these get deleted appropriately. 1321 // We need to ensure these get deleted appropriately.
1307 TEST_F(HostContentSettingsMapTest, GuestProfileMigration) { 1322 TEST_F(HostContentSettingsMapTest, GuestProfileMigration) {
1308 TestingProfile profile; 1323 TestingProfile profile(base::FilePath(),
1309 profile.SetGuestSession(true); 1324 #if BUILDFLAG(ENABLE_EXTENSIONS)
1325 nullptr,
1326 #endif
1327 nullptr, nullptr, nullptr, true /* guest_profile */,
1328 "", nullptr, TestingProfile::TestingFactories(),
1329 "testing_profile");
1310 1330
1311 // Set a pref manually in the guest profile. 1331 // Set a pref manually in the guest profile.
1312 std::unique_ptr<base::Value> value = 1332 std::unique_ptr<base::Value> value =
1313 base::JSONReader::Read("{\"[*.]\\xC4\\x87ira.com,*\":{\"setting\":1}}"); 1333 base::JSONReader::Read("{\"[*.]\\xC4\\x87ira.com,*\":{\"setting\":1}}");
1314 profile.GetPrefs()->Set(GetPrefName(CONTENT_SETTINGS_TYPE_COOKIES), *value); 1334 profile.GetPrefs()->Set(GetPrefName(CONTENT_SETTINGS_TYPE_COOKIES), *value);
1315 1335
1316 // Test that during construction all the prefs get cleared. 1336 // Test that during construction all the prefs get cleared.
1317 HostContentSettingsMapFactory::GetForProfile(&profile); 1337 HostContentSettingsMapFactory::GetForProfile(&profile);
1318 1338
1319 const base::DictionaryValue* all_settings_dictionary = 1339 const base::DictionaryValue* all_settings_dictionary =
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after
1490 EXPECT_EQ(CONTENT_SETTING_ALLOW, 1510 EXPECT_EQ(CONTENT_SETTING_ALLOW,
1491 host_content_settings_map->GetContentSetting( 1511 host_content_settings_map->GetContentSetting(
1492 https_host_narrower, https_host_narrower, 1512 https_host_narrower, https_host_narrower,
1493 CONTENT_SETTINGS_TYPE_COOKIES, std::string())); 1513 CONTENT_SETTINGS_TYPE_COOKIES, std::string()));
1494 } 1514 }
1495 1515
1496 // Ensure that migration only happens once upon construction of the HCSM and 1516 // Ensure that migration only happens once upon construction of the HCSM and
1497 // once after syncing (even when these events occur multiple times). 1517 // once after syncing (even when these events occur multiple times).
1498 TEST_F(HostContentSettingsMapTest, DomainToOriginMigrationStatus) { 1518 TEST_F(HostContentSettingsMapTest, DomainToOriginMigrationStatus) {
1499 TestingProfile profile; 1519 TestingProfile profile;
1520
1521 // Construct the map now to make the various platforms equivalent. On Android
1522 // the map is created with the profile (due to dependencies), while on other
1523 // platform it is created lazily. Note, migration should be run here.
raymes 2017/01/11 08:30:12 nit: platforms
benwells 2017/01/12 03:10:43 Done.
1524 HostContentSettingsMap* host_content_settings_map =
1525 HostContentSettingsMapFactory::GetForProfile(&profile);
1526
1500 PrefService* prefs = profile.GetPrefs(); 1527 PrefService* prefs = profile.GetPrefs();
1501 1528
1502 GURL http_host("http://example.com/"); 1529 GURL http_host("http://example.com/");
1503 GURL http_host_narrower("http://a.example.com/"); 1530 GURL http_host_narrower("http://a.example.com/");
1504 std::string host_pattern_string = 1531 std::string host_pattern_string =
1505 ContentSettingsPattern::FromURL(http_host).ToString(); 1532 ContentSettingsPattern::FromURL(http_host).ToString();
1506 1533
1507 { 1534 {
1508 DictionaryPrefUpdate update(prefs, 1535 DictionaryPrefUpdate update(prefs,
1509 GetPrefName(CONTENT_SETTINGS_TYPE_POPUPS)); 1536 GetPrefName(CONTENT_SETTINGS_TYPE_POPUPS));
1510 base::DictionaryValue* all_settings_dictionary = update.Get(); 1537 base::DictionaryValue* all_settings_dictionary = update.Get();
1511 ASSERT_TRUE(NULL != all_settings_dictionary); 1538 ASSERT_TRUE(NULL != all_settings_dictionary);
1512 1539
1513 base::DictionaryValue* domain_setting = new base::DictionaryValue; 1540 base::DictionaryValue* domain_setting = new base::DictionaryValue;
1514 domain_setting->SetInteger("setting", CONTENT_SETTING_ALLOW); 1541 domain_setting->SetInteger("setting", CONTENT_SETTING_ALLOW);
1515 all_settings_dictionary->SetWithoutPathExpansion(host_pattern_string + ",*", 1542 all_settings_dictionary->SetWithoutPathExpansion(host_pattern_string + ",*",
1516 domain_setting); 1543 domain_setting);
1517 } 1544 }
1518 1545
1519 const base::DictionaryValue* all_settings_dictionary = 1546 const base::DictionaryValue* all_settings_dictionary =
1520 prefs->GetDictionary(GetPrefName(CONTENT_SETTINGS_TYPE_POPUPS)); 1547 prefs->GetDictionary(GetPrefName(CONTENT_SETTINGS_TYPE_POPUPS));
1521 const base::DictionaryValue* result = NULL; 1548 const base::DictionaryValue* result = NULL;
1522 EXPECT_TRUE(all_settings_dictionary->GetDictionaryWithoutPathExpansion( 1549 EXPECT_TRUE(all_settings_dictionary->GetDictionaryWithoutPathExpansion(
1523 "[*.]example.com,*", &result)); 1550 "[*.]example.com,*", &result));
1524 1551
1525 // Migration is done on construction of HostContentSettingsMap. 1552 // Migration is done on construction of HostContentSettingsMap. True to run it
raymes 2017/01/11 08:30:12 nit: True->Try
benwells 2017/01/12 03:10:43 Done.
1526 HostContentSettingsMap* host_content_settings_map = 1553 // again. This should not do anything as it has already ran.
1527 HostContentSettingsMapFactory::GetForProfile(&profile); 1554 host_content_settings_map->MigrateDomainScopedSettings(
1555 false /* after_sync */);
1528 1556
1529 // Change default setting to BLOCK. 1557 // Change default setting to BLOCK.
1530 host_content_settings_map->SetDefaultContentSetting( 1558 host_content_settings_map->SetDefaultContentSetting(
1531 CONTENT_SETTINGS_TYPE_POPUPS, CONTENT_SETTING_BLOCK); 1559 CONTENT_SETTINGS_TYPE_POPUPS, CONTENT_SETTING_BLOCK);
1560
1532 EXPECT_EQ( 1561 EXPECT_EQ(
1533 CONTENT_SETTING_ALLOW, 1562 CONTENT_SETTING_ALLOW,
1534 host_content_settings_map->GetContentSetting( 1563 host_content_settings_map->GetContentSetting(
1564 http_host, http_host, CONTENT_SETTINGS_TYPE_POPUPS, std::string()));
1565
1566 // The setting should still be allow as it hasn't been migrated.
1567 EXPECT_EQ(CONTENT_SETTING_ALLOW,
1568 host_content_settings_map->GetContentSetting(
1569 http_host_narrower, http_host_narrower,
1570 CONTENT_SETTINGS_TYPE_POPUPS, std::string()));
1571
1572 // Set the pref to its initial state so that migration can be triggered again,
1573 // just for the sake of testing.
1574 int default_value;
1575 prefs->GetDefaultPrefValue(prefs::kDomainToOriginMigrationStatus)
1576 ->GetAsInteger(&default_value);
1577 prefs->SetInteger(prefs::kDomainToOriginMigrationStatus, default_value);
1578
1579 // Now, do the migration. This should work as we've cleared the pref back to
1580 // its default value.
1581 host_content_settings_map->MigrateDomainScopedSettings(false);
1582
1583 // Now the settings should be migrated.
1584 EXPECT_EQ(
1585 CONTENT_SETTING_ALLOW,
1586 host_content_settings_map->GetContentSetting(
1535 http_host, http_host, CONTENT_SETTINGS_TYPE_POPUPS, std::string())); 1587 http_host, http_host, CONTENT_SETTINGS_TYPE_POPUPS, std::string()));
1536 // Settings only apply to origins. Migration got executed. 1588 // Settings only apply to origins. Migration got executed.
1537 EXPECT_EQ(CONTENT_SETTING_BLOCK, 1589 EXPECT_EQ(CONTENT_SETTING_BLOCK,
1538 host_content_settings_map->GetContentSetting( 1590 host_content_settings_map->GetContentSetting(
1539 http_host_narrower, http_host_narrower, 1591 http_host_narrower, http_host_narrower,
1540 CONTENT_SETTINGS_TYPE_POPUPS, std::string())); 1592 CONTENT_SETTINGS_TYPE_POPUPS, std::string()));
1541 1593
1542 GURL https_host("https://example.com/"); 1594 GURL https_host("https://example.com/");
1543 GURL https_host_narrower("https://a.example.com/"); 1595 GURL https_host_narrower("https://a.example.com/");
1544 1596
1545 host_content_settings_map->SetContentSettingCustomScope( 1597 host_content_settings_map->SetContentSettingCustomScope(
1546 ContentSettingsPattern::FromURL(https_host), 1598 ContentSettingsPattern::FromURL(https_host),
1547 ContentSettingsPattern::Wildcard(), CONTENT_SETTINGS_TYPE_POPUPS, 1599 ContentSettingsPattern::Wildcard(), CONTENT_SETTINGS_TYPE_POPUPS,
1548 std::string(), CONTENT_SETTING_ALLOW); 1600 std::string(), CONTENT_SETTING_ALLOW);
1601
1549 EXPECT_EQ( 1602 EXPECT_EQ(
1550 CONTENT_SETTING_ALLOW, 1603 CONTENT_SETTING_ALLOW,
1551 host_content_settings_map->GetContentSetting( 1604 host_content_settings_map->GetContentSetting(
1552 https_host, https_host, CONTENT_SETTINGS_TYPE_POPUPS, std::string())); 1605 https_host, https_host, CONTENT_SETTINGS_TYPE_POPUPS, std::string()));
1553 // Settings apply to subdomains. 1606 // Settings apply to subdomains.
1554 EXPECT_EQ(CONTENT_SETTING_ALLOW, 1607 EXPECT_EQ(CONTENT_SETTING_ALLOW,
1555 host_content_settings_map->GetContentSetting( 1608 host_content_settings_map->GetContentSetting(
1556 https_host_narrower, https_host_narrower, 1609 https_host_narrower, https_host_narrower,
1557 CONTENT_SETTINGS_TYPE_POPUPS, std::string())); 1610 CONTENT_SETTINGS_TYPE_POPUPS, std::string()));
1558 1611
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after
1714 EXPECT_TRUE(map->CanSetNarrowestContentSetting( 1767 EXPECT_TRUE(map->CanSetNarrowestContentSetting(
1715 valid_url, valid_url, 1768 valid_url, valid_url,
1716 CONTENT_SETTINGS_TYPE_POPUPS)); 1769 CONTENT_SETTINGS_TYPE_POPUPS));
1717 1770
1718 GURL invalid_url("about:blank"); 1771 GURL invalid_url("about:blank");
1719 EXPECT_FALSE(map->CanSetNarrowestContentSetting( 1772 EXPECT_FALSE(map->CanSetNarrowestContentSetting(
1720 invalid_url, invalid_url, 1773 invalid_url, invalid_url,
1721 CONTENT_SETTINGS_TYPE_POPUPS)); 1774 CONTENT_SETTINGS_TYPE_POPUPS));
1722 } 1775 }
1723 1776
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698