[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 5 | #include <memory> |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 6 | #include <string> |
vabr | 9984ea6 | 2017-04-10 11:33:49 | [diff] [blame] | 7 | #include <utility> |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 8 | |
blundell | 6e85b7c | 2015-09-29 12:33:35 | [diff] [blame] | 9 | #include "base/base_switches.h" |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 10 | #include "base/command_line.h" |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 11 | #include "base/files/file_path.h" |
thestig | 18dfb7a5 | 2014-08-26 10:44:04 | [diff] [blame] | 12 | #include "base/files/file_util.h" |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 13 | #include "base/json/json_file_value_serializer.h" |
a-v-y | 3841686 | 2016-12-08 08:40:13 | [diff] [blame] | 14 | #include "base/json/json_reader.h" |
vabr | 9984ea6 | 2017-04-10 11:33:49 | [diff] [blame] | 15 | #include "base/memory/ptr_util.h" |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 16 | #include "base/metrics/histogram_base.h" |
| 17 | #include "base/metrics/histogram_samples.h" |
| 18 | #include "base/metrics/statistics_recorder.h" |
| 19 | #include "base/path_service.h" |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 20 | #include "base/strings/string_number_conversions.h" |
| 21 | #include "base/strings/string_util.h" |
a-v-y | 3841686 | 2016-12-08 08:40:13 | [diff] [blame] | 22 | #include "base/strings/utf_string_conversions.h" |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 23 | #include "base/values.h" |
| 24 | #include "build/build_config.h" |
| 25 | #include "chrome/browser/extensions/extension_browsertest.h" |
| 26 | #include "chrome/browser/extensions/extension_service.h" |
| 27 | #include "chrome/browser/prefs/chrome_pref_service_factory.h" |
| 28 | #include "chrome/browser/prefs/profile_pref_store_manager.h" |
| 29 | #include "chrome/browser/prefs/session_startup_pref.h" |
| 30 | #include "chrome/browser/profiles/profile.h" |
| 31 | #include "chrome/browser/ui/browser.h" |
| 32 | #include "chrome/common/chrome_constants.h" |
| 33 | #include "chrome/common/chrome_paths.h" |
| 34 | #include "chrome/common/pref_names.h" |
| 35 | #include "chrome/test/base/testing_profile.h" |
| 36 | #include "components/search_engines/default_search_manager.h" |
a-v-y | 3841686 | 2016-12-08 08:40:13 | [diff] [blame] | 37 | #include "components/search_engines/template_url_data.h" |
Lei Zhang | fd5e6f0a | 2017-11-28 18:24:29 | [diff] [blame] | 38 | #include "content/public/test/test_launcher.h" |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 39 | #include "extensions/browser/pref_names.h" |
| 40 | #include "extensions/common/extension.h" |
sammc | fb473a87 | 2017-04-03 01:56:59 | [diff] [blame] | 41 | #include "services/preferences/public/cpp/tracked/tracked_preference_histogram_names.h" |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 42 | |
| 43 | #if defined(OS_CHROMEOS) |
| 44 | #include "chromeos/chromeos_switches.h" |
| 45 | #endif |
| 46 | |
proberge | 269fd09 | 2016-10-04 22:13:41 | [diff] [blame] | 47 | #if defined(OS_WIN) |
gab | 36b8552 | 2017-05-24 19:30:45 | [diff] [blame] | 48 | #include "base/win/registry.h" |
| 49 | #include "chrome/install_static/install_util.h" |
proberge | 269fd09 | 2016-10-04 22:13:41 | [diff] [blame] | 50 | #endif |
| 51 | |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 52 | namespace { |
| 53 | |
| 54 | // Extension ID of chrome/test/data/extensions/good.crx |
| 55 | const char kGoodCrxId[] = "ldnnhddmnhbkjipkidpdiheffobcpfmf"; |
| 56 | |
| 57 | // Explicit expectations from the caller of GetTrackedPrefHistogramCount(). This |
| 58 | // enables detailed reporting of the culprit on failure. |
| 59 | enum AllowedBuckets { |
| 60 | // Allow no samples in any buckets. |
| 61 | ALLOW_NONE = -1, |
| 62 | // Any integer between BEGIN_ALLOW_SINGLE_BUCKET and END_ALLOW_SINGLE_BUCKET |
| 63 | // indicates that only this specific bucket is allowed to have a sample. |
| 64 | BEGIN_ALLOW_SINGLE_BUCKET = 0, |
| 65 | END_ALLOW_SINGLE_BUCKET = 100, |
| 66 | // Allow any buckets (no extra verifications performed). |
| 67 | ALLOW_ANY |
| 68 | }; |
| 69 | |
proberge | 269fd09 | 2016-10-04 22:13:41 | [diff] [blame] | 70 | #if defined(OS_WIN) |
| 71 | base::string16 GetRegistryPathForTestProfile() { |
gab | 36b8552 | 2017-05-24 19:30:45 | [diff] [blame] | 72 | // Cleanup follow-up to http://crbug.com/721245 for the previous location of |
| 73 | // this test key which had similar problems (to a lesser extent). It's |
| 74 | // redundant but harmless to have multiple callers hit this on the same |
| 75 | // machine. TODO(gab): remove this mid-june 2017. |
| 76 | base::win::RegKey key; |
| 77 | if (key.Open(HKEY_CURRENT_USER, L"SOFTWARE\\Chromium\\PrefHashBrowserTest", |
| 78 | KEY_SET_VALUE | KEY_WOW64_32KEY) == ERROR_SUCCESS) { |
| 79 | LONG result = key.DeleteKey(L""); |
| 80 | EXPECT_TRUE(result == ERROR_SUCCESS || result == ERROR_FILE_NOT_FOUND); |
| 81 | } |
| 82 | |
proberge | 269fd09 | 2016-10-04 22:13:41 | [diff] [blame] | 83 | base::FilePath profile_dir; |
| 84 | EXPECT_TRUE(PathService::Get(chrome::DIR_USER_DATA, &profile_dir)); |
gab | 36b8552 | 2017-05-24 19:30:45 | [diff] [blame] | 85 | |
| 86 | // Use a location under the real PreferenceMACs path so that the backup |
| 87 | // cleanup logic in ChromeTestLauncherDelegate::PreSharding() for interrupted |
| 88 | // tests covers this test key as well. |
| 89 | return install_static::GetRegistryPath() + |
| 90 | L"\\PreferenceMACs\\PrefHashBrowserTest\\" + |
proberge | 269fd09 | 2016-10-04 22:13:41 | [diff] [blame] | 91 | profile_dir.BaseName().value(); |
| 92 | } |
| 93 | #endif |
| 94 | |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 95 | // Returns the number of times |histogram_name| was reported so far; adding the |
| 96 | // results of the first 100 buckets (there are only ~19 reporting IDs as of this |
| 97 | // writing; varies depending on the platform). |allowed_buckets| hints at extra |
| 98 | // requirements verified in this method (see AllowedBuckets for details). |
| 99 | int GetTrackedPrefHistogramCount(const char* histogram_name, |
proberge | 269fd09 | 2016-10-04 22:13:41 | [diff] [blame] | 100 | const char* histogram_suffix, |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 101 | int allowed_buckets) { |
proberge | 269fd09 | 2016-10-04 22:13:41 | [diff] [blame] | 102 | std::string full_histogram_name(histogram_name); |
| 103 | if (*histogram_suffix) |
| 104 | full_histogram_name.append(".").append(histogram_suffix); |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 105 | const base::HistogramBase* histogram = |
proberge | 269fd09 | 2016-10-04 22:13:41 | [diff] [blame] | 106 | base::StatisticsRecorder::FindHistogram(full_histogram_name); |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 107 | if (!histogram) |
| 108 | return 0; |
| 109 | |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 110 | std::unique_ptr<base::HistogramSamples> samples(histogram->SnapshotSamples()); |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 111 | int sum = 0; |
| 112 | for (int i = 0; i < 100; ++i) { |
| 113 | int count_for_id = samples->GetCount(i); |
| 114 | EXPECT_GE(count_for_id, 0); |
| 115 | sum += count_for_id; |
| 116 | |
| 117 | if (allowed_buckets == ALLOW_NONE || |
| 118 | (allowed_buckets != ALLOW_ANY && i != allowed_buckets)) { |
| 119 | EXPECT_EQ(0, count_for_id) << "Unexpected reporting_id: " << i; |
| 120 | } |
| 121 | } |
| 122 | return sum; |
| 123 | } |
| 124 | |
proberge | 269fd09 | 2016-10-04 22:13:41 | [diff] [blame] | 125 | // Helper function to call GetTrackedPrefHistogramCount with no external |
| 126 | // validation suffix. |
| 127 | int GetTrackedPrefHistogramCount(const char* histogram_name, |
| 128 | int allowed_buckets) { |
| 129 | return GetTrackedPrefHistogramCount(histogram_name, "", allowed_buckets); |
| 130 | } |
| 131 | |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 132 | std::unique_ptr<base::DictionaryValue> ReadPrefsDictionary( |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 133 | const base::FilePath& pref_file) { |
prashhir | 54a99450 | 2015-03-05 09:30:57 | [diff] [blame] | 134 | JSONFileValueDeserializer deserializer(pref_file); |
| 135 | int error_code = JSONFileValueDeserializer::JSON_NO_ERROR; |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 136 | std::string error_str; |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 137 | std::unique_ptr<base::Value> prefs = |
olli.raula | ba04525 | 2015-10-16 06:16:40 | [diff] [blame] | 138 | deserializer.Deserialize(&error_code, &error_str); |
prashhir | 54a99450 | 2015-03-05 09:30:57 | [diff] [blame] | 139 | if (!prefs || error_code != JSONFileValueDeserializer::JSON_NO_ERROR) { |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 140 | ADD_FAILURE() << "Error #" << error_code << ": " << error_str; |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 141 | return std::unique_ptr<base::DictionaryValue>(); |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 142 | } |
jdoerrie | 1f536b2 | 2017-10-23 17:15:11 | [diff] [blame] | 143 | if (!prefs->is_dict()) { |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 144 | ADD_FAILURE(); |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 145 | return std::unique_ptr<base::DictionaryValue>(); |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 146 | } |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 147 | return std::unique_ptr<base::DictionaryValue>( |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 148 | static_cast<base::DictionaryValue*>(prefs.release())); |
| 149 | } |
| 150 | |
proberge | 269fd09 | 2016-10-04 22:13:41 | [diff] [blame] | 151 | // Returns whether external validation is supported on the platform through |
| 152 | // storing MACs in the registry. |
| 153 | bool SupportsRegistryValidation() { |
| 154 | #if defined(OS_WIN) |
| 155 | return true; |
| 156 | #else |
| 157 | return false; |
| 158 | #endif |
| 159 | } |
| 160 | |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 161 | #define PREF_HASH_BROWSER_TEST(fixture, test_name) \ |
| 162 | IN_PROC_BROWSER_TEST_P(fixture, PRE_##test_name) { \ |
| 163 | SetupPreferences(); \ |
| 164 | } \ |
| 165 | IN_PROC_BROWSER_TEST_P(fixture, test_name) { \ |
| 166 | VerifyReactionToPrefAttack(); \ |
| 167 | } \ |
| 168 | INSTANTIATE_TEST_CASE_P( \ |
| 169 | fixture##Instance, \ |
| 170 | fixture, \ |
| 171 | testing::Values( \ |
| 172 | chrome_prefs::internals::kSettingsEnforcementGroupNoEnforcement, \ |
| 173 | chrome_prefs::internals::kSettingsEnforcementGroupEnforceAlways, \ |
| 174 | chrome_prefs::internals:: \ |
| 175 | kSettingsEnforcementGroupEnforceAlwaysWithDSE, \ |
| 176 | chrome_prefs::internals:: \ |
| 177 | kSettingsEnforcementGroupEnforceAlwaysWithExtensionsAndDSE)); |
| 178 | |
| 179 | // A base fixture designed such that implementations do two things: |
| 180 | // 1) Override all three pure-virtual methods below to setup, attack, and |
| 181 | // verify preferenes throughout the tests provided by this fixture. |
| 182 | // 2) Instantiate their test via the PREF_HASH_BROWSER_TEST macro above. |
| 183 | // Based on top of ExtensionBrowserTest to allow easy interaction with the |
| 184 | // ExtensionService. |
| 185 | class PrefHashBrowserTestBase |
| 186 | : public ExtensionBrowserTest, |
| 187 | public testing::WithParamInterface<std::string> { |
| 188 | public: |
| 189 | // List of potential protection levels for this test in strict increasing |
| 190 | // order of protection levels. |
| 191 | enum SettingsProtectionLevel { |
| 192 | PROTECTION_DISABLED_ON_PLATFORM, |
| 193 | PROTECTION_DISABLED_FOR_GROUP, |
| 194 | PROTECTION_ENABLED_BASIC, |
| 195 | PROTECTION_ENABLED_DSE, |
| 196 | PROTECTION_ENABLED_EXTENSIONS, |
| 197 | // Represents the strongest level (i.e. always equivalent to the last one in |
| 198 | // terms of protection), leave this one last when adding new levels. |
| 199 | PROTECTION_ENABLED_ALL |
| 200 | }; |
| 201 | |
| 202 | PrefHashBrowserTestBase() |
| 203 | : protection_level_(GetProtectionLevelFromTrialGroup(GetParam())) { |
| 204 | } |
| 205 | |
avi | 556c0502 | 2014-12-22 23:31:43 | [diff] [blame] | 206 | void SetUpCommandLine(base::CommandLine* command_line) override { |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 207 | ExtensionBrowserTest::SetUpCommandLine(command_line); |
| 208 | EXPECT_FALSE(command_line->HasSwitch(switches::kForceFieldTrials)); |
| 209 | command_line->AppendSwitchASCII( |
| 210 | switches::kForceFieldTrials, |
| 211 | std::string(chrome_prefs::internals::kSettingsEnforcementTrialName) + |
| 212 | "/" + GetParam() + "/"); |
| 213 | #if defined(OS_CHROMEOS) |
| 214 | command_line->AppendSwitch( |
| 215 | chromeos::switches::kIgnoreUserProfileMappingForTests); |
| 216 | #endif |
| 217 | } |
| 218 | |
dcheng | 8f4b862 | 2014-10-23 16:37:48 | [diff] [blame] | 219 | bool SetUpUserDataDirectory() override { |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 220 | // Do the normal setup in the PRE test and attack preferences in the main |
| 221 | // test. |
Lei Zhang | fd5e6f0a | 2017-11-28 18:24:29 | [diff] [blame] | 222 | if (content::IsPreTest()) |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 223 | return ExtensionBrowserTest::SetUpUserDataDirectory(); |
| 224 | |
| 225 | #if defined(OS_CHROMEOS) |
| 226 | // For some reason, the Preferences file does not exist in the location |
| 227 | // below on Chrome OS. Since protection is disabled on Chrome OS, it's okay |
| 228 | // to simply not attack preferences at all (and still assert that no |
| 229 | // hardening related histogram kicked in in VerifyReactionToPrefAttack()). |
| 230 | // TODO(gab): Figure out why there is no Preferences file in this location |
| 231 | // on Chrome OS (and re-enable the section disabled for OS_CHROMEOS further |
| 232 | // below). |
| 233 | EXPECT_EQ(PROTECTION_DISABLED_ON_PLATFORM, protection_level_); |
| 234 | return true; |
| 235 | #endif |
| 236 | |
| 237 | base::FilePath profile_dir; |
| 238 | EXPECT_TRUE(PathService::Get(chrome::DIR_USER_DATA, &profile_dir)); |
| 239 | profile_dir = profile_dir.AppendASCII(TestingProfile::kTestUserProfileDir); |
| 240 | |
| 241 | // Sanity check that old protected pref file is never present in modern |
| 242 | // Chromes. |
| 243 | EXPECT_FALSE(base::PathExists( |
sammc | f420c16e | 2017-03-16 02:11:13 | [diff] [blame] | 244 | profile_dir.Append(FILE_PATH_LITERAL("Protected Preferences")))); |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 245 | |
| 246 | // Read the preferences from disk. |
| 247 | |
| 248 | const base::FilePath unprotected_pref_file = |
| 249 | profile_dir.Append(chrome::kPreferencesFilename); |
| 250 | EXPECT_TRUE(base::PathExists(unprotected_pref_file)); |
| 251 | |
| 252 | const base::FilePath protected_pref_file = |
| 253 | profile_dir.Append(chrome::kSecurePreferencesFilename); |
| 254 | EXPECT_EQ(protection_level_ > PROTECTION_DISABLED_ON_PLATFORM, |
| 255 | base::PathExists(protected_pref_file)); |
| 256 | |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 257 | std::unique_ptr<base::DictionaryValue> unprotected_preferences( |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 258 | ReadPrefsDictionary(unprotected_pref_file)); |
| 259 | if (!unprotected_preferences) |
| 260 | return false; |
| 261 | |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 262 | std::unique_ptr<base::DictionaryValue> protected_preferences; |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 263 | if (protection_level_ > PROTECTION_DISABLED_ON_PLATFORM) { |
| 264 | protected_preferences = ReadPrefsDictionary(protected_pref_file); |
| 265 | if (!protected_preferences) |
| 266 | return false; |
| 267 | } |
| 268 | |
| 269 | // Let the underlying test modify the preferences. |
| 270 | AttackPreferencesOnDisk(unprotected_preferences.get(), |
| 271 | protected_preferences.get()); |
| 272 | |
| 273 | // Write the modified preferences back to disk. |
| 274 | |
| 275 | JSONFileValueSerializer unprotected_prefs_serializer(unprotected_pref_file); |
| 276 | EXPECT_TRUE( |
| 277 | unprotected_prefs_serializer.Serialize(*unprotected_preferences)); |
| 278 | |
| 279 | if (protected_preferences) { |
| 280 | JSONFileValueSerializer protected_prefs_serializer(protected_pref_file); |
| 281 | EXPECT_TRUE(protected_prefs_serializer.Serialize(*protected_preferences)); |
| 282 | } |
| 283 | |
| 284 | return true; |
| 285 | } |
| 286 | |
gab | bcdefd0cc | 2015-02-17 18:12:40 | [diff] [blame] | 287 | void SetUpInProcessBrowserTestFixture() override { |
| 288 | ExtensionBrowserTest::SetUpInProcessBrowserTestFixture(); |
| 289 | |
| 290 | // Bots are on a domain, turn off the domain check for settings hardening in |
| 291 | // order to be able to test all SettingsEnforcement groups. |
| 292 | chrome_prefs::DisableDomainCheckForTesting(); |
proberge | 269fd09 | 2016-10-04 22:13:41 | [diff] [blame] | 293 | |
| 294 | #if defined(OS_WIN) |
| 295 | // Avoid polluting prefs for the user and the bots by writing to a specific |
| 296 | // testing registry path. |
| 297 | registry_key_for_external_validation_ = GetRegistryPathForTestProfile(); |
| 298 | ProfilePrefStoreManager::SetPreferenceValidationRegistryPathForTesting( |
| 299 | ®istry_key_for_external_validation_); |
| 300 | |
| 301 | // Keys should be unique, but to avoid flakes in the long run make sure an |
| 302 | // identical test key wasn't left behind by a previous test. |
Lei Zhang | fd5e6f0a | 2017-11-28 18:24:29 | [diff] [blame] | 303 | if (content::IsPreTest()) { |
proberge | 269fd09 | 2016-10-04 22:13:41 | [diff] [blame] | 304 | base::win::RegKey key; |
| 305 | if (key.Open(HKEY_CURRENT_USER, |
| 306 | registry_key_for_external_validation_.c_str(), |
| 307 | KEY_SET_VALUE | KEY_WOW64_32KEY) == ERROR_SUCCESS) { |
| 308 | LONG result = key.DeleteKey(L""); |
| 309 | ASSERT_TRUE(result == ERROR_SUCCESS || result == ERROR_FILE_NOT_FOUND); |
| 310 | } |
| 311 | } |
| 312 | #endif |
| 313 | } |
| 314 | |
| 315 | void TearDown() override { |
| 316 | #if defined(OS_WIN) |
| 317 | // When done, delete the Registry key to avoid polluting the registry. |
Lei Zhang | fd5e6f0a | 2017-11-28 18:24:29 | [diff] [blame] | 318 | if (!content::IsPreTest()) { |
proberge | 269fd09 | 2016-10-04 22:13:41 | [diff] [blame] | 319 | base::string16 registry_key = GetRegistryPathForTestProfile(); |
| 320 | base::win::RegKey key; |
| 321 | if (key.Open(HKEY_CURRENT_USER, registry_key.c_str(), |
| 322 | KEY_SET_VALUE | KEY_WOW64_32KEY) == ERROR_SUCCESS) { |
| 323 | LONG result = key.DeleteKey(L""); |
| 324 | ASSERT_TRUE(result == ERROR_SUCCESS || result == ERROR_FILE_NOT_FOUND); |
| 325 | } |
| 326 | } |
| 327 | #endif |
| 328 | ExtensionBrowserTest::TearDown(); |
gab | bcdefd0cc | 2015-02-17 18:12:40 | [diff] [blame] | 329 | } |
| 330 | |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 331 | // In the PRE_ test, find the number of tracked preferences that were |
| 332 | // initialized and save it to a file to be read back in the main test and used |
| 333 | // as the total number of tracked preferences. |
dcheng | 8f4b862 | 2014-10-23 16:37:48 | [diff] [blame] | 334 | void SetUpOnMainThread() override { |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 335 | ExtensionBrowserTest::SetUpOnMainThread(); |
| 336 | |
| 337 | // File in which the PRE_ test will save the number of tracked preferences |
| 338 | // on this platform. |
| 339 | const char kNumTrackedPrefFilename[] = "NumTrackedPrefs"; |
| 340 | |
| 341 | base::FilePath num_tracked_prefs_file; |
| 342 | ASSERT_TRUE( |
| 343 | PathService::Get(chrome::DIR_USER_DATA, &num_tracked_prefs_file)); |
| 344 | num_tracked_prefs_file = |
| 345 | num_tracked_prefs_file.AppendASCII(kNumTrackedPrefFilename); |
| 346 | |
Lei Zhang | fd5e6f0a | 2017-11-28 18:24:29 | [diff] [blame] | 347 | if (content::IsPreTest()) { |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 348 | num_tracked_prefs_ = GetTrackedPrefHistogramCount( |
deepak.m1 | 13481c8 | 2015-12-16 05:21:43 | [diff] [blame] | 349 | user_prefs::tracked::kTrackedPrefHistogramNullInitialized, ALLOW_ANY); |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 350 | EXPECT_EQ(protection_level_ > PROTECTION_DISABLED_ON_PLATFORM, |
| 351 | num_tracked_prefs_ > 0); |
| 352 | |
gab | 342aa618 | 2014-10-02 21:59:00 | [diff] [blame] | 353 | // Split tracked prefs are reported as Unchanged not as NullInitialized |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 354 | // when an empty dictionary is encountered on first run (this should only |
| 355 | // hit for pref #5 in the current design). |
| 356 | int num_split_tracked_prefs = GetTrackedPrefHistogramCount( |
deepak.m1 | 13481c8 | 2015-12-16 05:21:43 | [diff] [blame] | 357 | user_prefs::tracked::kTrackedPrefHistogramUnchanged, |
| 358 | BEGIN_ALLOW_SINGLE_BUCKET + 5); |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 359 | EXPECT_EQ(protection_level_ > PROTECTION_DISABLED_ON_PLATFORM ? 1 : 0, |
| 360 | num_split_tracked_prefs); |
| 361 | |
proberge | 269fd09 | 2016-10-04 22:13:41 | [diff] [blame] | 362 | if (SupportsRegistryValidation()) { |
| 363 | // Same checks as above, but for the registry. |
| 364 | num_tracked_prefs_ = GetTrackedPrefHistogramCount( |
| 365 | user_prefs::tracked::kTrackedPrefHistogramNullInitialized, |
| 366 | user_prefs::tracked::kTrackedPrefRegistryValidationSuffix, |
| 367 | ALLOW_ANY); |
| 368 | EXPECT_EQ(protection_level_ > PROTECTION_DISABLED_ON_PLATFORM, |
| 369 | num_tracked_prefs_ > 0); |
| 370 | |
| 371 | int num_split_tracked_prefs = GetTrackedPrefHistogramCount( |
| 372 | user_prefs::tracked::kTrackedPrefHistogramUnchanged, |
| 373 | user_prefs::tracked::kTrackedPrefRegistryValidationSuffix, |
| 374 | BEGIN_ALLOW_SINGLE_BUCKET + 5); |
| 375 | EXPECT_EQ(protection_level_ > PROTECTION_DISABLED_ON_PLATFORM ? 1 : 0, |
| 376 | num_split_tracked_prefs); |
| 377 | } |
| 378 | |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 379 | num_tracked_prefs_ += num_split_tracked_prefs; |
| 380 | |
| 381 | std::string num_tracked_prefs_str = base::IntToString(num_tracked_prefs_); |
| 382 | EXPECT_EQ(static_cast<int>(num_tracked_prefs_str.size()), |
| 383 | base::WriteFile(num_tracked_prefs_file, |
| 384 | num_tracked_prefs_str.c_str(), |
| 385 | num_tracked_prefs_str.size())); |
| 386 | } else { |
| 387 | std::string num_tracked_prefs_str; |
| 388 | EXPECT_TRUE(base::ReadFileToString(num_tracked_prefs_file, |
| 389 | &num_tracked_prefs_str)); |
| 390 | EXPECT_TRUE( |
| 391 | base::StringToInt(num_tracked_prefs_str, &num_tracked_prefs_)); |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | protected: |
| 396 | // Called from the PRE_ test's body. Overrides should use it to setup |
| 397 | // preferences through Chrome. |
| 398 | virtual void SetupPreferences() = 0; |
| 399 | |
| 400 | // Called prior to the main test launching its browser. Overrides should use |
| 401 | // it to attack preferences. |(un)protected_preferences| represent the state |
| 402 | // on disk prior to launching the main test, they can be modified by this |
| 403 | // method and modifications will be flushed back to disk before launching the |
| 404 | // main test. |unprotected_preferences| is never NULL, |protected_preferences| |
| 405 | // may be NULL if in PROTECTION_DISABLED_ON_PLATFORM mode. |
| 406 | virtual void AttackPreferencesOnDisk( |
| 407 | base::DictionaryValue* unprotected_preferences, |
| 408 | base::DictionaryValue* protected_preferences) = 0; |
| 409 | |
| 410 | // Called from the body of the main test. Overrides should use it to verify |
| 411 | // that the browser had the desired reaction when faced when the attack |
| 412 | // orchestrated in AttackPreferencesOnDisk(). |
| 413 | virtual void VerifyReactionToPrefAttack() = 0; |
| 414 | |
| 415 | int num_tracked_prefs() const { return num_tracked_prefs_; } |
| 416 | |
| 417 | const SettingsProtectionLevel protection_level_; |
| 418 | |
| 419 | private: |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 420 | SettingsProtectionLevel GetProtectionLevelFromTrialGroup( |
| 421 | const std::string& trial_group) { |
| 422 | if (!ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking) |
| 423 | return PROTECTION_DISABLED_ON_PLATFORM; |
| 424 | |
| 425 | // Protection levels can't be adjusted via --force-fieldtrials in official |
| 426 | // builds. |
| 427 | #if defined(OFFICIAL_BUILD) |
| 428 | |
proberge | 2562e6f | 2017-04-10 17:46:57 | [diff] [blame] | 429 | #if defined(OS_WIN) || defined(OS_MACOSX) |
| 430 | // The strongest mode is enforced on Windows and MacOS in the absence of a |
| 431 | // field trial. |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 432 | return PROTECTION_ENABLED_ALL; |
| 433 | #else |
| 434 | return PROTECTION_DISABLED_FOR_GROUP; |
Lei Zhang | fd5e6f0a | 2017-11-28 18:24:29 | [diff] [blame] | 435 | #endif // defined(OS_WIN) || defined(OS_MACOSX) |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 436 | |
| 437 | #else // defined(OFFICIAL_BUILD) |
| 438 | |
| 439 | using namespace chrome_prefs::internals; |
Lei Zhang | fd5e6f0a | 2017-11-28 18:24:29 | [diff] [blame] | 440 | if (trial_group == kSettingsEnforcementGroupNoEnforcement) |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 441 | return PROTECTION_DISABLED_FOR_GROUP; |
Lei Zhang | fd5e6f0a | 2017-11-28 18:24:29 | [diff] [blame] | 442 | if (trial_group == kSettingsEnforcementGroupEnforceAlways) |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 443 | return PROTECTION_ENABLED_BASIC; |
Lei Zhang | fd5e6f0a | 2017-11-28 18:24:29 | [diff] [blame] | 444 | if (trial_group == kSettingsEnforcementGroupEnforceAlwaysWithDSE) |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 445 | return PROTECTION_ENABLED_DSE; |
Lei Zhang | fd5e6f0a | 2017-11-28 18:24:29 | [diff] [blame] | 446 | if (trial_group == |
| 447 | kSettingsEnforcementGroupEnforceAlwaysWithExtensionsAndDSE) { |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 448 | return PROTECTION_ENABLED_EXTENSIONS; |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 449 | } |
Lei Zhang | fd5e6f0a | 2017-11-28 18:24:29 | [diff] [blame] | 450 | ADD_FAILURE(); |
| 451 | return static_cast<SettingsProtectionLevel>(-1); |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 452 | #endif // defined(OFFICIAL_BUILD) |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | int num_tracked_prefs_; |
proberge | 269fd09 | 2016-10-04 22:13:41 | [diff] [blame] | 456 | |
| 457 | #if defined(OS_WIN) |
| 458 | base::string16 registry_key_for_external_validation_; |
| 459 | #endif |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 460 | }; |
| 461 | |
| 462 | } // namespace |
| 463 | |
| 464 | // Verifies that nothing is reset when nothing is tampered with. |
| 465 | // Also sanity checks that the expected preferences files are in place. |
| 466 | class PrefHashBrowserTestUnchangedDefault : public PrefHashBrowserTestBase { |
| 467 | public: |
dcheng | 8f4b862 | 2014-10-23 16:37:48 | [diff] [blame] | 468 | void SetupPreferences() override { |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 469 | // Default Chrome setup. |
| 470 | } |
| 471 | |
dcheng | 8f4b862 | 2014-10-23 16:37:48 | [diff] [blame] | 472 | void AttackPreferencesOnDisk( |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 473 | base::DictionaryValue* unprotected_preferences, |
mostynb | 2b52d1db | 2014-10-07 02:47:17 | [diff] [blame] | 474 | base::DictionaryValue* protected_preferences) override { |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 475 | // No attack. |
| 476 | } |
| 477 | |
dcheng | 8f4b862 | 2014-10-23 16:37:48 | [diff] [blame] | 478 | void VerifyReactionToPrefAttack() override { |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 479 | // Expect all prefs to be reported as Unchanged with no resets. |
deepak.m1 | 13481c8 | 2015-12-16 05:21:43 | [diff] [blame] | 480 | EXPECT_EQ( |
| 481 | protection_level_ > PROTECTION_DISABLED_ON_PLATFORM |
| 482 | ? num_tracked_prefs() |
| 483 | : 0, |
| 484 | GetTrackedPrefHistogramCount( |
| 485 | user_prefs::tracked::kTrackedPrefHistogramUnchanged, ALLOW_ANY)); |
| 486 | EXPECT_EQ(0, GetTrackedPrefHistogramCount( |
| 487 | user_prefs::tracked::kTrackedPrefHistogramWantedReset, |
| 488 | ALLOW_NONE)); |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 489 | EXPECT_EQ(0, |
| 490 | GetTrackedPrefHistogramCount( |
deepak.m1 | 13481c8 | 2015-12-16 05:21:43 | [diff] [blame] | 491 | user_prefs::tracked::kTrackedPrefHistogramReset, ALLOW_NONE)); |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 492 | |
| 493 | // Nothing else should have triggered. |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 494 | EXPECT_EQ( |
deepak.m1 | 13481c8 | 2015-12-16 05:21:43 | [diff] [blame] | 495 | 0, GetTrackedPrefHistogramCount( |
| 496 | user_prefs::tracked::kTrackedPrefHistogramChanged, ALLOW_NONE)); |
| 497 | EXPECT_EQ( |
| 498 | 0, GetTrackedPrefHistogramCount( |
| 499 | user_prefs::tracked::kTrackedPrefHistogramCleared, ALLOW_NONE)); |
| 500 | EXPECT_EQ(0, GetTrackedPrefHistogramCount( |
| 501 | user_prefs::tracked::kTrackedPrefHistogramInitialized, |
| 502 | ALLOW_NONE)); |
| 503 | EXPECT_EQ(0, |
| 504 | GetTrackedPrefHistogramCount( |
| 505 | user_prefs::tracked::kTrackedPrefHistogramTrustedInitialized, |
| 506 | ALLOW_NONE)); |
| 507 | EXPECT_EQ(0, GetTrackedPrefHistogramCount( |
| 508 | user_prefs::tracked::kTrackedPrefHistogramNullInitialized, |
| 509 | ALLOW_NONE)); |
| 510 | EXPECT_EQ( |
| 511 | 0, GetTrackedPrefHistogramCount( |
| 512 | user_prefs::tracked::kTrackedPrefHistogramMigratedLegacyDeviceId, |
| 513 | ALLOW_NONE)); |
proberge | 269fd09 | 2016-10-04 22:13:41 | [diff] [blame] | 514 | |
| 515 | if (SupportsRegistryValidation()) { |
| 516 | // Expect all prefs to be reported as Unchanged. |
| 517 | EXPECT_EQ(protection_level_ > PROTECTION_DISABLED_ON_PLATFORM |
| 518 | ? num_tracked_prefs() |
| 519 | : 0, |
| 520 | GetTrackedPrefHistogramCount( |
| 521 | user_prefs::tracked::kTrackedPrefHistogramUnchanged, |
| 522 | user_prefs::tracked::kTrackedPrefRegistryValidationSuffix, |
| 523 | ALLOW_ANY)); |
| 524 | } |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 525 | } |
| 526 | }; |
| 527 | |
gab | 36b8552 | 2017-05-24 19:30:45 | [diff] [blame] | 528 | PREF_HASH_BROWSER_TEST(PrefHashBrowserTestUnchangedDefault, UnchangedDefault); |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 529 | |
| 530 | // Augments PrefHashBrowserTestUnchangedDefault to confirm that nothing is reset |
| 531 | // when nothing is tampered with, even if Chrome itself wrote custom prefs in |
| 532 | // its last run. |
| 533 | class PrefHashBrowserTestUnchangedCustom |
| 534 | : public PrefHashBrowserTestUnchangedDefault { |
| 535 | public: |
dcheng | 8f4b862 | 2014-10-23 16:37:48 | [diff] [blame] | 536 | void SetupPreferences() override { |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 537 | profile()->GetPrefs()->SetString(prefs::kHomePage, "http://example.com"); |
| 538 | |
| 539 | InstallExtensionWithUIAutoConfirm( |
| 540 | test_data_dir_.AppendASCII("good.crx"), 1, browser()); |
| 541 | } |
| 542 | |
dcheng | 8f4b862 | 2014-10-23 16:37:48 | [diff] [blame] | 543 | void VerifyReactionToPrefAttack() override { |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 544 | // Make sure the settings written in the last run stuck. |
| 545 | EXPECT_EQ("http://example.com", |
| 546 | profile()->GetPrefs()->GetString(prefs::kHomePage)); |
| 547 | |
| 548 | EXPECT_TRUE(extension_service()->GetExtensionById(kGoodCrxId, false)); |
| 549 | |
| 550 | // Reaction should be identical to unattacked default prefs. |
| 551 | PrefHashBrowserTestUnchangedDefault::VerifyReactionToPrefAttack(); |
| 552 | } |
| 553 | }; |
| 554 | |
gab | 36b8552 | 2017-05-24 19:30:45 | [diff] [blame] | 555 | PREF_HASH_BROWSER_TEST(PrefHashBrowserTestUnchangedCustom, UnchangedCustom); |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 556 | |
| 557 | // Verifies that cleared prefs are reported. |
| 558 | class PrefHashBrowserTestClearedAtomic : public PrefHashBrowserTestBase { |
| 559 | public: |
dcheng | 8f4b862 | 2014-10-23 16:37:48 | [diff] [blame] | 560 | void SetupPreferences() override { |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 561 | profile()->GetPrefs()->SetString(prefs::kHomePage, "http://example.com"); |
| 562 | } |
| 563 | |
dcheng | 8f4b862 | 2014-10-23 16:37:48 | [diff] [blame] | 564 | void AttackPreferencesOnDisk( |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 565 | base::DictionaryValue* unprotected_preferences, |
mostynb | 2b52d1db | 2014-10-07 02:47:17 | [diff] [blame] | 566 | base::DictionaryValue* protected_preferences) override { |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 567 | base::DictionaryValue* selected_prefs = |
| 568 | protection_level_ >= PROTECTION_ENABLED_BASIC ? protected_preferences |
| 569 | : unprotected_preferences; |
| 570 | // |selected_prefs| should never be NULL under the protection level picking |
| 571 | // it. |
| 572 | EXPECT_TRUE(selected_prefs); |
| 573 | EXPECT_TRUE(selected_prefs->Remove(prefs::kHomePage, NULL)); |
| 574 | } |
| 575 | |
dcheng | 8f4b862 | 2014-10-23 16:37:48 | [diff] [blame] | 576 | void VerifyReactionToPrefAttack() override { |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 577 | // The clearance of homepage should have been noticed (as pref #2 being |
| 578 | // cleared), but shouldn't have triggered a reset (as there is nothing we |
| 579 | // can do when the pref is already gone). |
| 580 | EXPECT_EQ(protection_level_ > PROTECTION_DISABLED_ON_PLATFORM ? 1 : 0, |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 581 | GetTrackedPrefHistogramCount( |
deepak.m1 | 13481c8 | 2015-12-16 05:21:43 | [diff] [blame] | 582 | user_prefs::tracked::kTrackedPrefHistogramCleared, |
| 583 | BEGIN_ALLOW_SINGLE_BUCKET + 2)); |
| 584 | EXPECT_EQ( |
| 585 | protection_level_ > PROTECTION_DISABLED_ON_PLATFORM |
| 586 | ? num_tracked_prefs() - 1 |
| 587 | : 0, |
| 588 | GetTrackedPrefHistogramCount( |
| 589 | user_prefs::tracked::kTrackedPrefHistogramUnchanged, ALLOW_ANY)); |
| 590 | EXPECT_EQ(0, GetTrackedPrefHistogramCount( |
| 591 | user_prefs::tracked::kTrackedPrefHistogramWantedReset, |
| 592 | ALLOW_NONE)); |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 593 | EXPECT_EQ(0, |
| 594 | GetTrackedPrefHistogramCount( |
deepak.m1 | 13481c8 | 2015-12-16 05:21:43 | [diff] [blame] | 595 | user_prefs::tracked::kTrackedPrefHistogramReset, ALLOW_NONE)); |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 596 | |
| 597 | // Nothing else should have triggered. |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 598 | EXPECT_EQ( |
deepak.m1 | 13481c8 | 2015-12-16 05:21:43 | [diff] [blame] | 599 | 0, GetTrackedPrefHistogramCount( |
| 600 | user_prefs::tracked::kTrackedPrefHistogramChanged, ALLOW_NONE)); |
| 601 | EXPECT_EQ(0, GetTrackedPrefHistogramCount( |
| 602 | user_prefs::tracked::kTrackedPrefHistogramInitialized, |
| 603 | ALLOW_NONE)); |
| 604 | EXPECT_EQ(0, |
| 605 | GetTrackedPrefHistogramCount( |
| 606 | user_prefs::tracked::kTrackedPrefHistogramTrustedInitialized, |
| 607 | ALLOW_NONE)); |
| 608 | EXPECT_EQ(0, GetTrackedPrefHistogramCount( |
| 609 | user_prefs::tracked::kTrackedPrefHistogramNullInitialized, |
| 610 | ALLOW_NONE)); |
| 611 | EXPECT_EQ( |
| 612 | 0, GetTrackedPrefHistogramCount( |
| 613 | user_prefs::tracked::kTrackedPrefHistogramMigratedLegacyDeviceId, |
| 614 | ALLOW_NONE)); |
proberge | 269fd09 | 2016-10-04 22:13:41 | [diff] [blame] | 615 | |
| 616 | if (SupportsRegistryValidation()) { |
| 617 | // Expect homepage clearance to have been noticed by registry validation. |
| 618 | EXPECT_EQ(protection_level_ > PROTECTION_DISABLED_ON_PLATFORM ? 1 : 0, |
| 619 | GetTrackedPrefHistogramCount( |
| 620 | user_prefs::tracked::kTrackedPrefHistogramCleared, |
| 621 | user_prefs::tracked::kTrackedPrefRegistryValidationSuffix, |
| 622 | BEGIN_ALLOW_SINGLE_BUCKET + 2)); |
| 623 | } |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 624 | } |
| 625 | }; |
| 626 | |
gab | 36b8552 | 2017-05-24 19:30:45 | [diff] [blame] | 627 | PREF_HASH_BROWSER_TEST(PrefHashBrowserTestClearedAtomic, ClearedAtomic); |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 628 | |
| 629 | // Verifies that clearing the MACs results in untrusted Initialized pings for |
| 630 | // non-null protected prefs. |
| 631 | class PrefHashBrowserTestUntrustedInitialized : public PrefHashBrowserTestBase { |
| 632 | public: |
dcheng | 8f4b862 | 2014-10-23 16:37:48 | [diff] [blame] | 633 | void SetupPreferences() override { |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 634 | // Explicitly set the DSE (it's otherwise NULL by default, preventing |
| 635 | // thorough testing of the PROTECTION_ENABLED_DSE level). |
| 636 | DefaultSearchManager default_search_manager( |
| 637 | profile()->GetPrefs(), DefaultSearchManager::ObserverCallback()); |
| 638 | DefaultSearchManager::Source dse_source = |
| 639 | static_cast<DefaultSearchManager::Source>(-1); |
| 640 | |
| 641 | const TemplateURLData* default_template_url_data = |
| 642 | default_search_manager.GetDefaultSearchEngine(&dse_source); |
| 643 | EXPECT_EQ(DefaultSearchManager::FROM_FALLBACK, dse_source); |
| 644 | |
| 645 | default_search_manager.SetUserSelectedDefaultSearchEngine( |
| 646 | *default_template_url_data); |
| 647 | |
| 648 | default_search_manager.GetDefaultSearchEngine(&dse_source); |
| 649 | EXPECT_EQ(DefaultSearchManager::FROM_USER, dse_source); |
| 650 | |
| 651 | // Also explicitly set an atomic pref that falls under |
| 652 | // PROTECTION_ENABLED_BASIC. |
| 653 | profile()->GetPrefs()->SetInteger(prefs::kRestoreOnStartup, |
| 654 | SessionStartupPref::URLS); |
| 655 | } |
| 656 | |
dcheng | 8f4b862 | 2014-10-23 16:37:48 | [diff] [blame] | 657 | void AttackPreferencesOnDisk( |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 658 | base::DictionaryValue* unprotected_preferences, |
mostynb | 2b52d1db | 2014-10-07 02:47:17 | [diff] [blame] | 659 | base::DictionaryValue* protected_preferences) override { |
proberge | 574d7d9 | 2016-08-01 20:14:11 | [diff] [blame] | 660 | unprotected_preferences->Remove("protection.macs", NULL); |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 661 | if (protected_preferences) |
proberge | 574d7d9 | 2016-08-01 20:14:11 | [diff] [blame] | 662 | protected_preferences->Remove("protection.macs", NULL); |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 663 | } |
| 664 | |
dcheng | 8f4b862 | 2014-10-23 16:37:48 | [diff] [blame] | 665 | void VerifyReactionToPrefAttack() override { |
gab | 342aa618 | 2014-10-02 21:59:00 | [diff] [blame] | 666 | // Preferences that are NULL by default will be NullInitialized. |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 667 | int num_null_values = GetTrackedPrefHistogramCount( |
deepak.m1 | 13481c8 | 2015-12-16 05:21:43 | [diff] [blame] | 668 | user_prefs::tracked::kTrackedPrefHistogramNullInitialized, ALLOW_ANY); |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 669 | EXPECT_EQ(protection_level_ > PROTECTION_DISABLED_ON_PLATFORM, |
| 670 | num_null_values > 0); |
| 671 | if (num_null_values > 0) { |
| 672 | // This test requires that at least 3 prefs be non-null (extensions, DSE, |
| 673 | // and 1 atomic pref explictly set for this test above). |
gab | 342aa618 | 2014-10-02 21:59:00 | [diff] [blame] | 674 | EXPECT_GE(num_tracked_prefs() - num_null_values, 3); |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 675 | } |
| 676 | |
| 677 | // Expect all non-null prefs to be reported as Initialized (with |
| 678 | // accompanying resets or wanted resets based on the current protection |
| 679 | // level). |
deepak.m1 | 13481c8 | 2015-12-16 05:21:43 | [diff] [blame] | 680 | EXPECT_EQ( |
| 681 | num_tracked_prefs() - num_null_values, |
| 682 | GetTrackedPrefHistogramCount( |
| 683 | user_prefs::tracked::kTrackedPrefHistogramInitialized, ALLOW_ANY)); |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 684 | |
| 685 | int num_protected_prefs = 0; |
| 686 | // A switch statement falling through each protection level in decreasing |
| 687 | // levels of protection to add expectations for each level which augments |
| 688 | // the previous one. |
| 689 | switch (protection_level_) { |
| 690 | case PROTECTION_ENABLED_ALL: |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 691 | case PROTECTION_ENABLED_EXTENSIONS: |
| 692 | ++num_protected_prefs; |
Nico Weber | 1c64f9a | 2018-01-30 12:00:48 | [diff] [blame^] | 693 | FALLTHROUGH; |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 694 | case PROTECTION_ENABLED_DSE: |
| 695 | ++num_protected_prefs; |
Nico Weber | 1c64f9a | 2018-01-30 12:00:48 | [diff] [blame^] | 696 | FALLTHROUGH; |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 697 | case PROTECTION_ENABLED_BASIC: |
| 698 | num_protected_prefs += num_tracked_prefs() - num_null_values - 2; |
Nico Weber | 1c64f9a | 2018-01-30 12:00:48 | [diff] [blame^] | 699 | FALLTHROUGH; |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 700 | case PROTECTION_DISABLED_FOR_GROUP: |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 701 | case PROTECTION_DISABLED_ON_PLATFORM: |
| 702 | // No protection. |
| 703 | break; |
| 704 | } |
| 705 | |
deepak.m1 | 13481c8 | 2015-12-16 05:21:43 | [diff] [blame] | 706 | EXPECT_EQ( |
| 707 | num_tracked_prefs() - num_null_values - num_protected_prefs, |
| 708 | GetTrackedPrefHistogramCount( |
| 709 | user_prefs::tracked::kTrackedPrefHistogramWantedReset, ALLOW_ANY)); |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 710 | EXPECT_EQ(num_protected_prefs, |
deepak.m1 | 13481c8 | 2015-12-16 05:21:43 | [diff] [blame] | 711 | GetTrackedPrefHistogramCount( |
| 712 | user_prefs::tracked::kTrackedPrefHistogramReset, ALLOW_ANY)); |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 713 | |
| 714 | // Explicitly verify the result of reported resets. |
| 715 | |
| 716 | DefaultSearchManager default_search_manager( |
| 717 | profile()->GetPrefs(), DefaultSearchManager::ObserverCallback()); |
| 718 | DefaultSearchManager::Source dse_source = |
| 719 | static_cast<DefaultSearchManager::Source>(-1); |
| 720 | default_search_manager.GetDefaultSearchEngine(&dse_source); |
| 721 | EXPECT_EQ(protection_level_ < PROTECTION_ENABLED_DSE |
| 722 | ? DefaultSearchManager::FROM_USER |
| 723 | : DefaultSearchManager::FROM_FALLBACK, |
| 724 | dse_source); |
| 725 | |
| 726 | EXPECT_EQ(protection_level_ < PROTECTION_ENABLED_BASIC, |
| 727 | profile()->GetPrefs()->GetInteger(prefs::kRestoreOnStartup) == |
| 728 | SessionStartupPref::URLS); |
| 729 | |
| 730 | // Nothing else should have triggered. |
deepak.m1 | 13481c8 | 2015-12-16 05:21:43 | [diff] [blame] | 731 | EXPECT_EQ(0, GetTrackedPrefHistogramCount( |
| 732 | user_prefs::tracked::kTrackedPrefHistogramUnchanged, |
| 733 | ALLOW_NONE)); |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 734 | EXPECT_EQ( |
deepak.m1 | 13481c8 | 2015-12-16 05:21:43 | [diff] [blame] | 735 | 0, GetTrackedPrefHistogramCount( |
| 736 | user_prefs::tracked::kTrackedPrefHistogramChanged, ALLOW_NONE)); |
| 737 | EXPECT_EQ( |
| 738 | 0, GetTrackedPrefHistogramCount( |
| 739 | user_prefs::tracked::kTrackedPrefHistogramCleared, ALLOW_NONE)); |
| 740 | EXPECT_EQ( |
| 741 | 0, GetTrackedPrefHistogramCount( |
| 742 | user_prefs::tracked::kTrackedPrefHistogramMigratedLegacyDeviceId, |
| 743 | ALLOW_NONE)); |
proberge | 269fd09 | 2016-10-04 22:13:41 | [diff] [blame] | 744 | |
| 745 | if (SupportsRegistryValidation()) { |
| 746 | // The MACs have been cleared but the preferences have not been tampered. |
| 747 | // The registry should report all prefs as unchanged. |
| 748 | EXPECT_EQ(protection_level_ > PROTECTION_DISABLED_ON_PLATFORM |
| 749 | ? num_tracked_prefs() |
| 750 | : 0, |
| 751 | GetTrackedPrefHistogramCount( |
| 752 | user_prefs::tracked::kTrackedPrefHistogramUnchanged, |
| 753 | user_prefs::tracked::kTrackedPrefRegistryValidationSuffix, |
| 754 | ALLOW_ANY)); |
| 755 | } |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 756 | } |
| 757 | }; |
| 758 | |
| 759 | PREF_HASH_BROWSER_TEST(PrefHashBrowserTestUntrustedInitialized, |
gab | 36b8552 | 2017-05-24 19:30:45 | [diff] [blame] | 760 | UntrustedInitialized); |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 761 | |
| 762 | // Verifies that changing an atomic pref results in it being reported (and reset |
| 763 | // if the protection level allows it). |
| 764 | class PrefHashBrowserTestChangedAtomic : public PrefHashBrowserTestBase { |
| 765 | public: |
dcheng | 8f4b862 | 2014-10-23 16:37:48 | [diff] [blame] | 766 | void SetupPreferences() override { |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 767 | profile()->GetPrefs()->SetInteger(prefs::kRestoreOnStartup, |
| 768 | SessionStartupPref::URLS); |
| 769 | |
| 770 | ListPrefUpdate update(profile()->GetPrefs(), |
| 771 | prefs::kURLsToRestoreOnStartup); |
| 772 | update->AppendString("http://example.com"); |
| 773 | } |
| 774 | |
dcheng | 8f4b862 | 2014-10-23 16:37:48 | [diff] [blame] | 775 | void AttackPreferencesOnDisk( |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 776 | base::DictionaryValue* unprotected_preferences, |
mostynb | 2b52d1db | 2014-10-07 02:47:17 | [diff] [blame] | 777 | base::DictionaryValue* protected_preferences) override { |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 778 | base::DictionaryValue* selected_prefs = |
| 779 | protection_level_ >= PROTECTION_ENABLED_BASIC ? protected_preferences |
| 780 | : unprotected_preferences; |
| 781 | // |selected_prefs| should never be NULL under the protection level picking |
| 782 | // it. |
| 783 | EXPECT_TRUE(selected_prefs); |
| 784 | base::ListValue* startup_urls; |
| 785 | EXPECT_TRUE( |
| 786 | selected_prefs->GetList(prefs::kURLsToRestoreOnStartup, &startup_urls)); |
| 787 | EXPECT_TRUE(startup_urls); |
| 788 | EXPECT_EQ(1U, startup_urls->GetSize()); |
| 789 | startup_urls->AppendString("http://example.org"); |
| 790 | } |
| 791 | |
dcheng | 8f4b862 | 2014-10-23 16:37:48 | [diff] [blame] | 792 | void VerifyReactionToPrefAttack() override { |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 793 | // Expect a single Changed event for tracked pref #4 (startup URLs). |
| 794 | EXPECT_EQ(protection_level_ > PROTECTION_DISABLED_ON_PLATFORM ? 1 : 0, |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 795 | GetTrackedPrefHistogramCount( |
deepak.m1 | 13481c8 | 2015-12-16 05:21:43 | [diff] [blame] | 796 | user_prefs::tracked::kTrackedPrefHistogramChanged, |
| 797 | BEGIN_ALLOW_SINGLE_BUCKET + 4)); |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 798 | EXPECT_EQ( |
deepak.m1 | 13481c8 | 2015-12-16 05:21:43 | [diff] [blame] | 799 | protection_level_ > PROTECTION_DISABLED_ON_PLATFORM |
| 800 | ? num_tracked_prefs() - 1 |
| 801 | : 0, |
| 802 | GetTrackedPrefHistogramCount( |
| 803 | user_prefs::tracked::kTrackedPrefHistogramUnchanged, ALLOW_ANY)); |
| 804 | |
| 805 | EXPECT_EQ((protection_level_ > PROTECTION_DISABLED_ON_PLATFORM && |
| 806 | protection_level_ < PROTECTION_ENABLED_BASIC) |
| 807 | ? 1 |
| 808 | : 0, |
| 809 | GetTrackedPrefHistogramCount( |
| 810 | user_prefs::tracked::kTrackedPrefHistogramWantedReset, |
| 811 | BEGIN_ALLOW_SINGLE_BUCKET + 4)); |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 812 | EXPECT_EQ(protection_level_ >= PROTECTION_ENABLED_BASIC ? 1 : 0, |
deepak.m1 | 13481c8 | 2015-12-16 05:21:43 | [diff] [blame] | 813 | GetTrackedPrefHistogramCount( |
| 814 | user_prefs::tracked::kTrackedPrefHistogramReset, |
| 815 | BEGIN_ALLOW_SINGLE_BUCKET + 4)); |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 816 | |
| 817 | // TODO(gab): This doesn't work on OS_CHROMEOS because we fail to attack |
| 818 | // Preferences. |
| 819 | #if !defined(OS_CHROMEOS) |
| 820 | // Explicitly verify the result of reported resets. |
| 821 | EXPECT_EQ(protection_level_ >= PROTECTION_ENABLED_BASIC ? 0U : 2U, |
| 822 | profile() |
| 823 | ->GetPrefs() |
| 824 | ->GetList(prefs::kURLsToRestoreOnStartup) |
| 825 | ->GetSize()); |
| 826 | #endif |
| 827 | |
| 828 | // Nothing else should have triggered. |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 829 | EXPECT_EQ( |
deepak.m1 | 13481c8 | 2015-12-16 05:21:43 | [diff] [blame] | 830 | 0, GetTrackedPrefHistogramCount( |
| 831 | user_prefs::tracked::kTrackedPrefHistogramCleared, ALLOW_NONE)); |
| 832 | EXPECT_EQ(0, GetTrackedPrefHistogramCount( |
| 833 | user_prefs::tracked::kTrackedPrefHistogramInitialized, |
| 834 | ALLOW_NONE)); |
| 835 | EXPECT_EQ(0, |
| 836 | GetTrackedPrefHistogramCount( |
| 837 | user_prefs::tracked::kTrackedPrefHistogramTrustedInitialized, |
| 838 | ALLOW_NONE)); |
| 839 | EXPECT_EQ(0, GetTrackedPrefHistogramCount( |
| 840 | user_prefs::tracked::kTrackedPrefHistogramNullInitialized, |
| 841 | ALLOW_NONE)); |
| 842 | EXPECT_EQ( |
| 843 | 0, GetTrackedPrefHistogramCount( |
| 844 | user_prefs::tracked::kTrackedPrefHistogramMigratedLegacyDeviceId, |
| 845 | ALLOW_NONE)); |
proberge | 269fd09 | 2016-10-04 22:13:41 | [diff] [blame] | 846 | |
| 847 | if (SupportsRegistryValidation()) { |
| 848 | // Expect a single Changed event for tracked pref #4 (startup URLs). |
| 849 | EXPECT_EQ(protection_level_ > PROTECTION_DISABLED_ON_PLATFORM ? 1 : 0, |
| 850 | GetTrackedPrefHistogramCount( |
| 851 | user_prefs::tracked::kTrackedPrefHistogramChanged, |
| 852 | user_prefs::tracked::kTrackedPrefRegistryValidationSuffix, |
| 853 | BEGIN_ALLOW_SINGLE_BUCKET + 4)); |
| 854 | } |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 855 | } |
| 856 | }; |
| 857 | |
gab | 36b8552 | 2017-05-24 19:30:45 | [diff] [blame] | 858 | PREF_HASH_BROWSER_TEST(PrefHashBrowserTestChangedAtomic, ChangedAtomic); |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 859 | |
| 860 | // Verifies that changing or adding an entry in a split pref results in both |
| 861 | // items being reported (and remove if the protection level allows it). |
| 862 | class PrefHashBrowserTestChangedSplitPref : public PrefHashBrowserTestBase { |
| 863 | public: |
dcheng | 8f4b862 | 2014-10-23 16:37:48 | [diff] [blame] | 864 | void SetupPreferences() override { |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 865 | InstallExtensionWithUIAutoConfirm( |
| 866 | test_data_dir_.AppendASCII("good.crx"), 1, browser()); |
| 867 | } |
| 868 | |
dcheng | 8f4b862 | 2014-10-23 16:37:48 | [diff] [blame] | 869 | void AttackPreferencesOnDisk( |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 870 | base::DictionaryValue* unprotected_preferences, |
mostynb | 2b52d1db | 2014-10-07 02:47:17 | [diff] [blame] | 871 | base::DictionaryValue* protected_preferences) override { |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 872 | base::DictionaryValue* selected_prefs = |
| 873 | protection_level_ >= PROTECTION_ENABLED_EXTENSIONS |
| 874 | ? protected_preferences |
| 875 | : unprotected_preferences; |
| 876 | // |selected_prefs| should never be NULL under the protection level picking |
| 877 | // it. |
| 878 | EXPECT_TRUE(selected_prefs); |
| 879 | base::DictionaryValue* extensions_dict; |
| 880 | EXPECT_TRUE(selected_prefs->GetDictionary( |
| 881 | extensions::pref_names::kExtensions, &extensions_dict)); |
| 882 | EXPECT_TRUE(extensions_dict); |
| 883 | |
| 884 | // Tamper with any installed setting for good.crx |
| 885 | base::DictionaryValue* good_crx_dict; |
| 886 | EXPECT_TRUE(extensions_dict->GetDictionary(kGoodCrxId, &good_crx_dict)); |
| 887 | int good_crx_state; |
| 888 | EXPECT_TRUE(good_crx_dict->GetInteger("state", &good_crx_state)); |
| 889 | EXPECT_EQ(extensions::Extension::ENABLED, good_crx_state); |
| 890 | good_crx_dict->SetInteger("state", extensions::Extension::DISABLED); |
| 891 | |
| 892 | // Drop a fake extension (for the purpose of this test, dropped settings |
| 893 | // don't need to be valid extension settings). |
vabr | 9984ea6 | 2017-04-10 11:33:49 | [diff] [blame] | 894 | auto fake_extension = base::MakeUnique<base::DictionaryValue>(); |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 895 | fake_extension->SetString("name", "foo"); |
vabr | 9984ea6 | 2017-04-10 11:33:49 | [diff] [blame] | 896 | extensions_dict->Set(std::string(32, 'a'), std::move(fake_extension)); |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 897 | } |
| 898 | |
dcheng | 8f4b862 | 2014-10-23 16:37:48 | [diff] [blame] | 899 | void VerifyReactionToPrefAttack() override { |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 900 | // Expect a single split pref changed report with a count of 2 for tracked |
| 901 | // pref #5 (extensions). |
| 902 | EXPECT_EQ(protection_level_ > PROTECTION_DISABLED_ON_PLATFORM ? 1 : 0, |
deepak.m1 | 13481c8 | 2015-12-16 05:21:43 | [diff] [blame] | 903 | GetTrackedPrefHistogramCount( |
| 904 | user_prefs::tracked::kTrackedPrefHistogramChanged, |
| 905 | BEGIN_ALLOW_SINGLE_BUCKET + 5)); |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 906 | EXPECT_EQ(protection_level_ > PROTECTION_DISABLED_ON_PLATFORM ? 1 : 0, |
| 907 | GetTrackedPrefHistogramCount( |
| 908 | "Settings.TrackedSplitPreferenceChanged.extensions.settings", |
| 909 | BEGIN_ALLOW_SINGLE_BUCKET + 2)); |
| 910 | |
| 911 | // Everything else should have remained unchanged. |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 912 | EXPECT_EQ( |
deepak.m1 | 13481c8 | 2015-12-16 05:21:43 | [diff] [blame] | 913 | protection_level_ > PROTECTION_DISABLED_ON_PLATFORM |
| 914 | ? num_tracked_prefs() - 1 |
| 915 | : 0, |
| 916 | GetTrackedPrefHistogramCount( |
| 917 | user_prefs::tracked::kTrackedPrefHistogramUnchanged, ALLOW_ANY)); |
| 918 | |
| 919 | EXPECT_EQ((protection_level_ > PROTECTION_DISABLED_ON_PLATFORM && |
| 920 | protection_level_ < PROTECTION_ENABLED_EXTENSIONS) |
| 921 | ? 1 |
| 922 | : 0, |
| 923 | GetTrackedPrefHistogramCount( |
| 924 | user_prefs::tracked::kTrackedPrefHistogramWantedReset, |
| 925 | BEGIN_ALLOW_SINGLE_BUCKET + 5)); |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 926 | EXPECT_EQ(protection_level_ >= PROTECTION_ENABLED_EXTENSIONS ? 1 : 0, |
deepak.m1 | 13481c8 | 2015-12-16 05:21:43 | [diff] [blame] | 927 | GetTrackedPrefHistogramCount( |
| 928 | user_prefs::tracked::kTrackedPrefHistogramReset, |
| 929 | BEGIN_ALLOW_SINGLE_BUCKET + 5)); |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 930 | |
| 931 | EXPECT_EQ(protection_level_ < PROTECTION_ENABLED_EXTENSIONS, |
| 932 | extension_service()->GetExtensionById(kGoodCrxId, true) != NULL); |
| 933 | |
| 934 | // Nothing else should have triggered. |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 935 | EXPECT_EQ( |
deepak.m1 | 13481c8 | 2015-12-16 05:21:43 | [diff] [blame] | 936 | 0, GetTrackedPrefHistogramCount( |
| 937 | user_prefs::tracked::kTrackedPrefHistogramCleared, ALLOW_NONE)); |
| 938 | EXPECT_EQ(0, GetTrackedPrefHistogramCount( |
| 939 | user_prefs::tracked::kTrackedPrefHistogramInitialized, |
| 940 | ALLOW_NONE)); |
| 941 | EXPECT_EQ(0, |
| 942 | GetTrackedPrefHistogramCount( |
| 943 | user_prefs::tracked::kTrackedPrefHistogramTrustedInitialized, |
| 944 | ALLOW_NONE)); |
| 945 | EXPECT_EQ(0, GetTrackedPrefHistogramCount( |
| 946 | user_prefs::tracked::kTrackedPrefHistogramNullInitialized, |
| 947 | ALLOW_NONE)); |
| 948 | EXPECT_EQ( |
| 949 | 0, GetTrackedPrefHistogramCount( |
| 950 | user_prefs::tracked::kTrackedPrefHistogramMigratedLegacyDeviceId, |
| 951 | ALLOW_NONE)); |
proberge | 269fd09 | 2016-10-04 22:13:41 | [diff] [blame] | 952 | |
| 953 | if (SupportsRegistryValidation()) { |
| 954 | // Expect that the registry validation caught the invalid MAC in split |
| 955 | // pref #5 (extensions). |
| 956 | EXPECT_EQ(protection_level_ > PROTECTION_DISABLED_ON_PLATFORM ? 1 : 0, |
| 957 | GetTrackedPrefHistogramCount( |
| 958 | user_prefs::tracked::kTrackedPrefHistogramChanged, |
| 959 | user_prefs::tracked::kTrackedPrefRegistryValidationSuffix, |
| 960 | BEGIN_ALLOW_SINGLE_BUCKET + 5)); |
| 961 | } |
[email protected] | ae7bf34 | 2014-08-06 18:03:47 | [diff] [blame] | 962 | } |
| 963 | }; |
| 964 | |
gab | 36b8552 | 2017-05-24 19:30:45 | [diff] [blame] | 965 | PREF_HASH_BROWSER_TEST(PrefHashBrowserTestChangedSplitPref, ChangedSplitPref); |
gab | b004a56 | 2014-09-16 12:45:22 | [diff] [blame] | 966 | |
| 967 | // Verifies that adding a value to unprotected preferences for a key which is |
| 968 | // still using the default (i.e. has no value stored in protected preferences) |
| 969 | // doesn't allow that value to slip in with no valid MAC (regression test for |
| 970 | // http://crbug.com/414554) |
| 971 | class PrefHashBrowserTestUntrustedAdditionToPrefs |
| 972 | : public PrefHashBrowserTestBase { |
| 973 | public: |
dcheng | 8f4b862 | 2014-10-23 16:37:48 | [diff] [blame] | 974 | void SetupPreferences() override { |
gab | b004a56 | 2014-09-16 12:45:22 | [diff] [blame] | 975 | // Ensure there is no user-selected value for kRestoreOnStartup. |
| 976 | EXPECT_FALSE( |
| 977 | profile()->GetPrefs()->GetUserPrefValue(prefs::kRestoreOnStartup)); |
| 978 | } |
| 979 | |
dcheng | 8f4b862 | 2014-10-23 16:37:48 | [diff] [blame] | 980 | void AttackPreferencesOnDisk( |
gab | b004a56 | 2014-09-16 12:45:22 | [diff] [blame] | 981 | base::DictionaryValue* unprotected_preferences, |
mostynb | 2b52d1db | 2014-10-07 02:47:17 | [diff] [blame] | 982 | base::DictionaryValue* protected_preferences) override { |
gab | b004a56 | 2014-09-16 12:45:22 | [diff] [blame] | 983 | unprotected_preferences->SetInteger(prefs::kRestoreOnStartup, |
| 984 | SessionStartupPref::LAST); |
| 985 | } |
| 986 | |
dcheng | 8f4b862 | 2014-10-23 16:37:48 | [diff] [blame] | 987 | void VerifyReactionToPrefAttack() override { |
gab | b004a56 | 2014-09-16 12:45:22 | [diff] [blame] | 988 | // Expect a single Changed event for tracked pref #3 (kRestoreOnStartup) if |
| 989 | // not protecting; if protection is enabled the change should be a no-op. |
| 990 | int changed_expected = |
| 991 | protection_level_ == PROTECTION_DISABLED_FOR_GROUP ? 1 : 0; |
deepak.m1 | 13481c8 | 2015-12-16 05:21:43 | [diff] [blame] | 992 | EXPECT_EQ((protection_level_ > PROTECTION_DISABLED_ON_PLATFORM && |
| 993 | protection_level_ < PROTECTION_ENABLED_BASIC) |
| 994 | ? changed_expected |
| 995 | : 0, |
gab | b004a56 | 2014-09-16 12:45:22 | [diff] [blame] | 996 | GetTrackedPrefHistogramCount( |
deepak.m1 | 13481c8 | 2015-12-16 05:21:43 | [diff] [blame] | 997 | user_prefs::tracked::kTrackedPrefHistogramChanged, |
| 998 | BEGIN_ALLOW_SINGLE_BUCKET + 3)); |
gab | b004a56 | 2014-09-16 12:45:22 | [diff] [blame] | 999 | EXPECT_EQ( |
deepak.m1 | 13481c8 | 2015-12-16 05:21:43 | [diff] [blame] | 1000 | protection_level_ > PROTECTION_DISABLED_ON_PLATFORM |
| 1001 | ? num_tracked_prefs() - changed_expected |
| 1002 | : 0, |
| 1003 | GetTrackedPrefHistogramCount( |
| 1004 | user_prefs::tracked::kTrackedPrefHistogramUnchanged, ALLOW_ANY)); |
| 1005 | |
| 1006 | EXPECT_EQ((protection_level_ > PROTECTION_DISABLED_ON_PLATFORM && |
| 1007 | protection_level_ < PROTECTION_ENABLED_BASIC) |
| 1008 | ? 1 |
| 1009 | : 0, |
| 1010 | GetTrackedPrefHistogramCount( |
| 1011 | user_prefs::tracked::kTrackedPrefHistogramWantedReset, |
| 1012 | BEGIN_ALLOW_SINGLE_BUCKET + 3)); |
gab | b004a56 | 2014-09-16 12:45:22 | [diff] [blame] | 1013 | EXPECT_EQ(0, |
deepak.m1 | 13481c8 | 2015-12-16 05:21:43 | [diff] [blame] | 1014 | GetTrackedPrefHistogramCount( |
| 1015 | user_prefs::tracked::kTrackedPrefHistogramReset, ALLOW_NONE)); |
gab | b004a56 | 2014-09-16 12:45:22 | [diff] [blame] | 1016 | |
| 1017 | // Nothing else should have triggered. |
gab | b004a56 | 2014-09-16 12:45:22 | [diff] [blame] | 1018 | EXPECT_EQ( |
deepak.m1 | 13481c8 | 2015-12-16 05:21:43 | [diff] [blame] | 1019 | 0, GetTrackedPrefHistogramCount( |
| 1020 | user_prefs::tracked::kTrackedPrefHistogramCleared, ALLOW_NONE)); |
| 1021 | EXPECT_EQ(0, GetTrackedPrefHistogramCount( |
| 1022 | user_prefs::tracked::kTrackedPrefHistogramInitialized, |
| 1023 | ALLOW_NONE)); |
| 1024 | EXPECT_EQ(0, |
| 1025 | GetTrackedPrefHistogramCount( |
| 1026 | user_prefs::tracked::kTrackedPrefHistogramTrustedInitialized, |
| 1027 | ALLOW_NONE)); |
| 1028 | EXPECT_EQ(0, GetTrackedPrefHistogramCount( |
| 1029 | user_prefs::tracked::kTrackedPrefHistogramNullInitialized, |
| 1030 | ALLOW_NONE)); |
| 1031 | EXPECT_EQ( |
| 1032 | 0, GetTrackedPrefHistogramCount( |
| 1033 | user_prefs::tracked::kTrackedPrefHistogramMigratedLegacyDeviceId, |
| 1034 | ALLOW_NONE)); |
proberge | 269fd09 | 2016-10-04 22:13:41 | [diff] [blame] | 1035 | |
| 1036 | if (SupportsRegistryValidation()) { |
| 1037 | EXPECT_EQ((protection_level_ > PROTECTION_DISABLED_ON_PLATFORM && |
| 1038 | protection_level_ < PROTECTION_ENABLED_BASIC) |
| 1039 | ? changed_expected |
| 1040 | : 0, |
| 1041 | GetTrackedPrefHistogramCount( |
| 1042 | user_prefs::tracked::kTrackedPrefHistogramChanged, |
| 1043 | user_prefs::tracked::kTrackedPrefRegistryValidationSuffix, |
| 1044 | BEGIN_ALLOW_SINGLE_BUCKET + 3)); |
| 1045 | } |
gab | b004a56 | 2014-09-16 12:45:22 | [diff] [blame] | 1046 | } |
| 1047 | }; |
| 1048 | |
| 1049 | PREF_HASH_BROWSER_TEST(PrefHashBrowserTestUntrustedAdditionToPrefs, |
gab | 36b8552 | 2017-05-24 19:30:45 | [diff] [blame] | 1050 | UntrustedAdditionToPrefs); |
erikwright | f4e02b7 | 2014-09-17 20:25:45 | [diff] [blame] | 1051 | |
| 1052 | // Verifies that adding a value to unprotected preferences while wiping a |
| 1053 | // user-selected value from protected preferences doesn't allow that value to |
| 1054 | // slip in with no valid MAC (regression test for http://crbug.com/414554). |
| 1055 | class PrefHashBrowserTestUntrustedAdditionToPrefsAfterWipe |
| 1056 | : public PrefHashBrowserTestBase { |
| 1057 | public: |
dcheng | 8f4b862 | 2014-10-23 16:37:48 | [diff] [blame] | 1058 | void SetupPreferences() override { |
erikwright | f4e02b7 | 2014-09-17 20:25:45 | [diff] [blame] | 1059 | profile()->GetPrefs()->SetString(prefs::kHomePage, "http://example.com"); |
| 1060 | } |
| 1061 | |
dcheng | 8f4b862 | 2014-10-23 16:37:48 | [diff] [blame] | 1062 | void AttackPreferencesOnDisk( |
erikwright | f4e02b7 | 2014-09-17 20:25:45 | [diff] [blame] | 1063 | base::DictionaryValue* unprotected_preferences, |
mostynb | 2b52d1db | 2014-10-07 02:47:17 | [diff] [blame] | 1064 | base::DictionaryValue* protected_preferences) override { |
erikwright | f4e02b7 | 2014-09-17 20:25:45 | [diff] [blame] | 1065 | // Set or change the value in Preferences to the attacker's choice. |
| 1066 | unprotected_preferences->SetString(prefs::kHomePage, "http://example.net"); |
| 1067 | // Clear the value in Secure Preferences, if any. |
| 1068 | if (protected_preferences) |
| 1069 | protected_preferences->Remove(prefs::kHomePage, NULL); |
| 1070 | } |
| 1071 | |
dcheng | 8f4b862 | 2014-10-23 16:37:48 | [diff] [blame] | 1072 | void VerifyReactionToPrefAttack() override { |
erikwright | f4e02b7 | 2014-09-17 20:25:45 | [diff] [blame] | 1073 | // Expect a single Changed event for tracked pref #2 (kHomePage) if |
| 1074 | // not protecting; if protection is enabled the change should be a Cleared. |
| 1075 | int changed_expected = |
| 1076 | protection_level_ > PROTECTION_DISABLED_ON_PLATFORM && |
| 1077 | protection_level_ < PROTECTION_ENABLED_BASIC |
| 1078 | ? 1 : 0; |
| 1079 | int cleared_expected = |
| 1080 | protection_level_ >= PROTECTION_ENABLED_BASIC |
| 1081 | ? 1 : 0; |
| 1082 | EXPECT_EQ(changed_expected, |
erikwright | f4e02b7 | 2014-09-17 20:25:45 | [diff] [blame] | 1083 | GetTrackedPrefHistogramCount( |
deepak.m1 | 13481c8 | 2015-12-16 05:21:43 | [diff] [blame] | 1084 | user_prefs::tracked::kTrackedPrefHistogramChanged, |
| 1085 | BEGIN_ALLOW_SINGLE_BUCKET + 2)); |
| 1086 | EXPECT_EQ(cleared_expected, |
| 1087 | GetTrackedPrefHistogramCount( |
| 1088 | user_prefs::tracked::kTrackedPrefHistogramCleared, |
| 1089 | BEGIN_ALLOW_SINGLE_BUCKET + 2)); |
erikwright | f4e02b7 | 2014-09-17 20:25:45 | [diff] [blame] | 1090 | EXPECT_EQ( |
deepak.m1 | 13481c8 | 2015-12-16 05:21:43 | [diff] [blame] | 1091 | protection_level_ > PROTECTION_DISABLED_ON_PLATFORM |
| 1092 | ? num_tracked_prefs() - changed_expected - cleared_expected |
| 1093 | : 0, |
| 1094 | GetTrackedPrefHistogramCount( |
| 1095 | user_prefs::tracked::kTrackedPrefHistogramUnchanged, ALLOW_ANY)); |
| 1096 | |
| 1097 | EXPECT_EQ(changed_expected, |
| 1098 | GetTrackedPrefHistogramCount( |
| 1099 | user_prefs::tracked::kTrackedPrefHistogramWantedReset, |
| 1100 | BEGIN_ALLOW_SINGLE_BUCKET + 2)); |
erikwright | f4e02b7 | 2014-09-17 20:25:45 | [diff] [blame] | 1101 | EXPECT_EQ(0, |
deepak.m1 | 13481c8 | 2015-12-16 05:21:43 | [diff] [blame] | 1102 | GetTrackedPrefHistogramCount( |
| 1103 | user_prefs::tracked::kTrackedPrefHistogramReset, ALLOW_NONE)); |
erikwright | f4e02b7 | 2014-09-17 20:25:45 | [diff] [blame] | 1104 | |
| 1105 | // Nothing else should have triggered. |
deepak.m1 | 13481c8 | 2015-12-16 05:21:43 | [diff] [blame] | 1106 | EXPECT_EQ(0, GetTrackedPrefHistogramCount( |
| 1107 | user_prefs::tracked::kTrackedPrefHistogramInitialized, |
| 1108 | ALLOW_NONE)); |
erikwright | f4e02b7 | 2014-09-17 20:25:45 | [diff] [blame] | 1109 | EXPECT_EQ(0, |
| 1110 | GetTrackedPrefHistogramCount( |
deepak.m1 | 13481c8 | 2015-12-16 05:21:43 | [diff] [blame] | 1111 | user_prefs::tracked::kTrackedPrefHistogramTrustedInitialized, |
| 1112 | ALLOW_NONE)); |
| 1113 | EXPECT_EQ(0, GetTrackedPrefHistogramCount( |
| 1114 | user_prefs::tracked::kTrackedPrefHistogramNullInitialized, |
| 1115 | ALLOW_NONE)); |
erikwright | f4e02b7 | 2014-09-17 20:25:45 | [diff] [blame] | 1116 | EXPECT_EQ( |
deepak.m1 | 13481c8 | 2015-12-16 05:21:43 | [diff] [blame] | 1117 | 0, GetTrackedPrefHistogramCount( |
| 1118 | user_prefs::tracked::kTrackedPrefHistogramMigratedLegacyDeviceId, |
| 1119 | ALLOW_NONE)); |
proberge | 269fd09 | 2016-10-04 22:13:41 | [diff] [blame] | 1120 | |
| 1121 | if (SupportsRegistryValidation()) { |
| 1122 | EXPECT_EQ(changed_expected, |
| 1123 | GetTrackedPrefHistogramCount( |
| 1124 | user_prefs::tracked::kTrackedPrefHistogramChanged, |
| 1125 | user_prefs::tracked::kTrackedPrefRegistryValidationSuffix, |
| 1126 | BEGIN_ALLOW_SINGLE_BUCKET + 2)); |
| 1127 | EXPECT_EQ(cleared_expected, |
| 1128 | GetTrackedPrefHistogramCount( |
| 1129 | user_prefs::tracked::kTrackedPrefHistogramCleared, |
| 1130 | user_prefs::tracked::kTrackedPrefRegistryValidationSuffix, |
| 1131 | BEGIN_ALLOW_SINGLE_BUCKET + 2)); |
| 1132 | } |
erikwright | f4e02b7 | 2014-09-17 20:25:45 | [diff] [blame] | 1133 | } |
| 1134 | }; |
| 1135 | |
| 1136 | PREF_HASH_BROWSER_TEST(PrefHashBrowserTestUntrustedAdditionToPrefsAfterWipe, |
gab | 36b8552 | 2017-05-24 19:30:45 | [diff] [blame] | 1137 | UntrustedAdditionToPrefsAfterWipe); |
proberge | 269fd09 | 2016-10-04 22:13:41 | [diff] [blame] | 1138 | |
| 1139 | #if defined(OS_WIN) |
| 1140 | class PrefHashBrowserTestRegistryValidationFailure |
| 1141 | : public PrefHashBrowserTestBase { |
| 1142 | public: |
| 1143 | void SetupPreferences() override { |
| 1144 | profile()->GetPrefs()->SetString(prefs::kHomePage, "http://example.com"); |
| 1145 | } |
| 1146 | |
| 1147 | void AttackPreferencesOnDisk( |
| 1148 | base::DictionaryValue* unprotected_preferences, |
| 1149 | base::DictionaryValue* protected_preferences) override { |
| 1150 | base::string16 registry_key = |
| 1151 | GetRegistryPathForTestProfile() + L"\\PreferenceMACs\\Default"; |
| 1152 | base::win::RegKey key; |
| 1153 | ASSERT_EQ(ERROR_SUCCESS, key.Open(HKEY_CURRENT_USER, registry_key.c_str(), |
| 1154 | KEY_SET_VALUE | KEY_WOW64_32KEY)); |
| 1155 | // An incorrect hash should still have the correct size. |
| 1156 | ASSERT_EQ(ERROR_SUCCESS, |
| 1157 | key.WriteValue(L"homepage", base::string16(64, 'A').c_str())); |
| 1158 | } |
| 1159 | |
| 1160 | void VerifyReactionToPrefAttack() override { |
| 1161 | EXPECT_EQ( |
| 1162 | protection_level_ > PROTECTION_DISABLED_ON_PLATFORM |
| 1163 | ? num_tracked_prefs() |
| 1164 | : 0, |
| 1165 | GetTrackedPrefHistogramCount( |
| 1166 | user_prefs::tracked::kTrackedPrefHistogramUnchanged, ALLOW_ANY)); |
| 1167 | |
| 1168 | if (SupportsRegistryValidation()) { |
| 1169 | // Expect that the registry validation caught the invalid MAC for pref #2 |
| 1170 | // (homepage). |
| 1171 | EXPECT_EQ(protection_level_ > PROTECTION_DISABLED_ON_PLATFORM ? 1 : 0, |
| 1172 | GetTrackedPrefHistogramCount( |
| 1173 | user_prefs::tracked::kTrackedPrefHistogramChanged, |
| 1174 | user_prefs::tracked::kTrackedPrefRegistryValidationSuffix, |
| 1175 | BEGIN_ALLOW_SINGLE_BUCKET + 2)); |
| 1176 | } |
| 1177 | } |
| 1178 | }; |
| 1179 | |
| 1180 | PREF_HASH_BROWSER_TEST(PrefHashBrowserTestRegistryValidationFailure, |
gab | 36b8552 | 2017-05-24 19:30:45 | [diff] [blame] | 1181 | RegistryValidationFailure); |
proberge | 269fd09 | 2016-10-04 22:13:41 | [diff] [blame] | 1182 | #endif |
a-v-y | 3841686 | 2016-12-08 08:40:13 | [diff] [blame] | 1183 | |
| 1184 | // Verifies that all preferences related to choice of default search engine are |
| 1185 | // protected. |
| 1186 | class PrefHashBrowserTestDefaultSearch : public PrefHashBrowserTestBase { |
| 1187 | public: |
| 1188 | void SetupPreferences() override { |
| 1189 | // Set user selected default search engine. |
| 1190 | DefaultSearchManager default_search_manager( |
| 1191 | profile()->GetPrefs(), DefaultSearchManager::ObserverCallback()); |
| 1192 | DefaultSearchManager::Source dse_source = |
| 1193 | static_cast<DefaultSearchManager::Source>(-1); |
| 1194 | |
| 1195 | TemplateURLData user_dse; |
| 1196 | user_dse.SetKeyword(base::UTF8ToUTF16("userkeyword")); |
| 1197 | user_dse.SetShortName(base::UTF8ToUTF16("username")); |
| 1198 | user_dse.SetURL("http://user_default_engine/search?q=good_user_query"); |
| 1199 | default_search_manager.SetUserSelectedDefaultSearchEngine(user_dse); |
| 1200 | |
| 1201 | const TemplateURLData* current_dse = |
| 1202 | default_search_manager.GetDefaultSearchEngine(&dse_source); |
| 1203 | EXPECT_EQ(DefaultSearchManager::FROM_USER, dse_source); |
| 1204 | EXPECT_EQ(current_dse->keyword(), base::UTF8ToUTF16("userkeyword")); |
| 1205 | EXPECT_EQ(current_dse->short_name(), base::UTF8ToUTF16("username")); |
| 1206 | EXPECT_EQ(current_dse->url(), |
| 1207 | "http://user_default_engine/search?q=good_user_query"); |
| 1208 | } |
| 1209 | |
| 1210 | void AttackPreferencesOnDisk( |
| 1211 | base::DictionaryValue* unprotected_preferences, |
| 1212 | base::DictionaryValue* protected_preferences) override { |
| 1213 | static constexpr char default_search_provider_data[] = R"( |
| 1214 | { |
| 1215 | "default_search_provider_data" : { |
| 1216 | "template_url_data" : { |
| 1217 | "keyword" : "badkeyword", |
| 1218 | "short_name" : "badname", |
| 1219 | "url" : "http://bad_default_engine/search?q=dirty_user_query" |
| 1220 | } |
| 1221 | } |
| 1222 | })"; |
| 1223 | static constexpr char search_provider_overrides[] = R"( |
| 1224 | { |
| 1225 | "search_provider_overrides" : [ |
| 1226 | { |
| 1227 | "keyword" : "badkeyword", |
| 1228 | "name" : "badname", |
| 1229 | "search_url" : "http://bad_default_engine/search?q=dirty_user_query", |
| 1230 | "encoding" : "utf-8", |
| 1231 | "id" : 1 |
| 1232 | }, { |
| 1233 | "keyword" : "badkeyword2", |
| 1234 | "name" : "badname2", |
| 1235 | "search_url" : "http://bad_default_engine2/search?q=dirty_user_query", |
| 1236 | "encoding" : "utf-8", |
| 1237 | "id" : 2 |
| 1238 | } |
| 1239 | ] |
| 1240 | })"; |
a-v-y | 3841686 | 2016-12-08 08:40:13 | [diff] [blame] | 1241 | |
| 1242 | // Try to override default search in all three of available preferences. |
| 1243 | auto attack1 = base::DictionaryValue::From( |
| 1244 | base::JSONReader::Read(default_search_provider_data)); |
| 1245 | auto attack2 = base::DictionaryValue::From( |
| 1246 | base::JSONReader::Read(search_provider_overrides)); |
a-v-y | 3841686 | 2016-12-08 08:40:13 | [diff] [blame] | 1247 | unprotected_preferences->MergeDictionary(attack1.get()); |
| 1248 | unprotected_preferences->MergeDictionary(attack2.get()); |
a-v-y | 3841686 | 2016-12-08 08:40:13 | [diff] [blame] | 1249 | if (protected_preferences) { |
| 1250 | // Override here, too. |
| 1251 | protected_preferences->MergeDictionary(attack1.get()); |
| 1252 | protected_preferences->MergeDictionary(attack2.get()); |
a-v-y | 3841686 | 2016-12-08 08:40:13 | [diff] [blame] | 1253 | } |
| 1254 | } |
| 1255 | |
| 1256 | void VerifyReactionToPrefAttack() override { |
| 1257 | DefaultSearchManager default_search_manager( |
| 1258 | profile()->GetPrefs(), DefaultSearchManager::ObserverCallback()); |
| 1259 | DefaultSearchManager::Source dse_source = |
| 1260 | static_cast<DefaultSearchManager::Source>(-1); |
| 1261 | |
| 1262 | const TemplateURLData* current_dse = |
| 1263 | default_search_manager.GetDefaultSearchEngine(&dse_source); |
| 1264 | |
| 1265 | if (protection_level_ < PROTECTION_ENABLED_DSE) { |
| 1266 | // This doesn't work on OS_CHROMEOS because we fail to attack Preferences. |
| 1267 | #if !defined(OS_CHROMEOS) |
| 1268 | // Attack is successful. |
| 1269 | EXPECT_EQ(DefaultSearchManager::FROM_USER, dse_source); |
| 1270 | EXPECT_EQ(current_dse->keyword(), base::UTF8ToUTF16("badkeyword")); |
| 1271 | EXPECT_EQ(current_dse->short_name(), base::UTF8ToUTF16("badname")); |
| 1272 | EXPECT_EQ(current_dse->url(), |
| 1273 | "http://bad_default_engine/search?q=dirty_user_query"); |
| 1274 | #endif |
| 1275 | } else { |
| 1276 | // Attack fails. |
| 1277 | EXPECT_EQ(DefaultSearchManager::FROM_FALLBACK, dse_source); |
| 1278 | EXPECT_NE(current_dse->keyword(), base::UTF8ToUTF16("badkeyword")); |
| 1279 | EXPECT_NE(current_dse->short_name(), base::UTF8ToUTF16("badname")); |
| 1280 | EXPECT_NE(current_dse->url(), |
| 1281 | "http://bad_default_engine/search?q=dirty_user_query"); |
| 1282 | } |
| 1283 | } |
| 1284 | }; |
| 1285 | |
gab | 36b8552 | 2017-05-24 19:30:45 | [diff] [blame] | 1286 | PREF_HASH_BROWSER_TEST(PrefHashBrowserTestDefaultSearch, SearchProtected); |