blob: cf50a5514c04d29f16ba9f4bfc19cc92dec6e587 [file] [log] [blame]
[email protected]ae7bf342014-08-06 18:03:471// 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
dcheng4af48582016-04-19 00:29:355#include <memory>
[email protected]ae7bf342014-08-06 18:03:476#include <string>
vabr9984ea62017-04-10 11:33:497#include <utility>
[email protected]ae7bf342014-08-06 18:03:478
blundell6e85b7c2015-09-29 12:33:359#include "base/base_switches.h"
[email protected]ae7bf342014-08-06 18:03:4710#include "base/command_line.h"
[email protected]ae7bf342014-08-06 18:03:4711#include "base/files/file_path.h"
thestig18dfb7a52014-08-26 10:44:0412#include "base/files/file_util.h"
[email protected]ae7bf342014-08-06 18:03:4713#include "base/json/json_file_value_serializer.h"
a-v-y38416862016-12-08 08:40:1314#include "base/json/json_reader.h"
vabr9984ea62017-04-10 11:33:4915#include "base/memory/ptr_util.h"
[email protected]ae7bf342014-08-06 18:03:4716#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]ae7bf342014-08-06 18:03:4720#include "base/strings/string_number_conversions.h"
21#include "base/strings/string_util.h"
a-v-y38416862016-12-08 08:40:1322#include "base/strings/utf_string_conversions.h"
[email protected]ae7bf342014-08-06 18:03:4723#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-y38416862016-12-08 08:40:1337#include "components/search_engines/template_url_data.h"
Lei Zhangfd5e6f0a2017-11-28 18:24:2938#include "content/public/test/test_launcher.h"
[email protected]ae7bf342014-08-06 18:03:4739#include "extensions/browser/pref_names.h"
40#include "extensions/common/extension.h"
sammcfb473a872017-04-03 01:56:5941#include "services/preferences/public/cpp/tracked/tracked_preference_histogram_names.h"
[email protected]ae7bf342014-08-06 18:03:4742
43#if defined(OS_CHROMEOS)
44#include "chromeos/chromeos_switches.h"
45#endif
46
proberge269fd092016-10-04 22:13:4147#if defined(OS_WIN)
gab36b85522017-05-24 19:30:4548#include "base/win/registry.h"
49#include "chrome/install_static/install_util.h"
proberge269fd092016-10-04 22:13:4150#endif
51
[email protected]ae7bf342014-08-06 18:03:4752namespace {
53
54// Extension ID of chrome/test/data/extensions/good.crx
55const char kGoodCrxId[] = "ldnnhddmnhbkjipkidpdiheffobcpfmf";
56
57// Explicit expectations from the caller of GetTrackedPrefHistogramCount(). This
58// enables detailed reporting of the culprit on failure.
59enum 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
proberge269fd092016-10-04 22:13:4170#if defined(OS_WIN)
71base::string16 GetRegistryPathForTestProfile() {
gab36b85522017-05-24 19:30:4572 // 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
proberge269fd092016-10-04 22:13:4183 base::FilePath profile_dir;
84 EXPECT_TRUE(PathService::Get(chrome::DIR_USER_DATA, &profile_dir));
gab36b85522017-05-24 19:30:4585
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\\" +
proberge269fd092016-10-04 22:13:4191 profile_dir.BaseName().value();
92}
93#endif
94
[email protected]ae7bf342014-08-06 18:03:4795// 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).
99int GetTrackedPrefHistogramCount(const char* histogram_name,
proberge269fd092016-10-04 22:13:41100 const char* histogram_suffix,
[email protected]ae7bf342014-08-06 18:03:47101 int allowed_buckets) {
proberge269fd092016-10-04 22:13:41102 std::string full_histogram_name(histogram_name);
103 if (*histogram_suffix)
104 full_histogram_name.append(".").append(histogram_suffix);
[email protected]ae7bf342014-08-06 18:03:47105 const base::HistogramBase* histogram =
proberge269fd092016-10-04 22:13:41106 base::StatisticsRecorder::FindHistogram(full_histogram_name);
[email protected]ae7bf342014-08-06 18:03:47107 if (!histogram)
108 return 0;
109
dcheng4af48582016-04-19 00:29:35110 std::unique_ptr<base::HistogramSamples> samples(histogram->SnapshotSamples());
[email protected]ae7bf342014-08-06 18:03:47111 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
proberge269fd092016-10-04 22:13:41125// Helper function to call GetTrackedPrefHistogramCount with no external
126// validation suffix.
127int GetTrackedPrefHistogramCount(const char* histogram_name,
128 int allowed_buckets) {
129 return GetTrackedPrefHistogramCount(histogram_name, "", allowed_buckets);
130}
131
dcheng4af48582016-04-19 00:29:35132std::unique_ptr<base::DictionaryValue> ReadPrefsDictionary(
[email protected]ae7bf342014-08-06 18:03:47133 const base::FilePath& pref_file) {
prashhir54a994502015-03-05 09:30:57134 JSONFileValueDeserializer deserializer(pref_file);
135 int error_code = JSONFileValueDeserializer::JSON_NO_ERROR;
[email protected]ae7bf342014-08-06 18:03:47136 std::string error_str;
dcheng4af48582016-04-19 00:29:35137 std::unique_ptr<base::Value> prefs =
olli.raulaba045252015-10-16 06:16:40138 deserializer.Deserialize(&error_code, &error_str);
prashhir54a994502015-03-05 09:30:57139 if (!prefs || error_code != JSONFileValueDeserializer::JSON_NO_ERROR) {
[email protected]ae7bf342014-08-06 18:03:47140 ADD_FAILURE() << "Error #" << error_code << ": " << error_str;
dcheng4af48582016-04-19 00:29:35141 return std::unique_ptr<base::DictionaryValue>();
[email protected]ae7bf342014-08-06 18:03:47142 }
jdoerrie1f536b22017-10-23 17:15:11143 if (!prefs->is_dict()) {
[email protected]ae7bf342014-08-06 18:03:47144 ADD_FAILURE();
dcheng4af48582016-04-19 00:29:35145 return std::unique_ptr<base::DictionaryValue>();
[email protected]ae7bf342014-08-06 18:03:47146 }
dcheng4af48582016-04-19 00:29:35147 return std::unique_ptr<base::DictionaryValue>(
[email protected]ae7bf342014-08-06 18:03:47148 static_cast<base::DictionaryValue*>(prefs.release()));
149}
150
proberge269fd092016-10-04 22:13:41151// Returns whether external validation is supported on the platform through
152// storing MACs in the registry.
153bool SupportsRegistryValidation() {
154#if defined(OS_WIN)
155 return true;
156#else
157 return false;
158#endif
159}
160
[email protected]ae7bf342014-08-06 18:03:47161#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.
185class 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
avi556c05022014-12-22 23:31:43206 void SetUpCommandLine(base::CommandLine* command_line) override {
[email protected]ae7bf342014-08-06 18:03:47207 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
dcheng8f4b8622014-10-23 16:37:48219 bool SetUpUserDataDirectory() override {
[email protected]ae7bf342014-08-06 18:03:47220 // Do the normal setup in the PRE test and attack preferences in the main
221 // test.
Lei Zhangfd5e6f0a2017-11-28 18:24:29222 if (content::IsPreTest())
[email protected]ae7bf342014-08-06 18:03:47223 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(
sammcf420c16e2017-03-16 02:11:13244 profile_dir.Append(FILE_PATH_LITERAL("Protected Preferences"))));
[email protected]ae7bf342014-08-06 18:03:47245
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
dcheng4af48582016-04-19 00:29:35257 std::unique_ptr<base::DictionaryValue> unprotected_preferences(
[email protected]ae7bf342014-08-06 18:03:47258 ReadPrefsDictionary(unprotected_pref_file));
259 if (!unprotected_preferences)
260 return false;
261
dcheng4af48582016-04-19 00:29:35262 std::unique_ptr<base::DictionaryValue> protected_preferences;
[email protected]ae7bf342014-08-06 18:03:47263 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
gabbcdefd0cc2015-02-17 18:12:40287 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();
proberge269fd092016-10-04 22:13:41293
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 &registry_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 Zhangfd5e6f0a2017-11-28 18:24:29303 if (content::IsPreTest()) {
proberge269fd092016-10-04 22:13:41304 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 Zhangfd5e6f0a2017-11-28 18:24:29318 if (!content::IsPreTest()) {
proberge269fd092016-10-04 22:13:41319 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();
gabbcdefd0cc2015-02-17 18:12:40329 }
330
[email protected]ae7bf342014-08-06 18:03:47331 // 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.
dcheng8f4b8622014-10-23 16:37:48334 void SetUpOnMainThread() override {
[email protected]ae7bf342014-08-06 18:03:47335 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 Zhangfd5e6f0a2017-11-28 18:24:29347 if (content::IsPreTest()) {
[email protected]ae7bf342014-08-06 18:03:47348 num_tracked_prefs_ = GetTrackedPrefHistogramCount(
deepak.m113481c82015-12-16 05:21:43349 user_prefs::tracked::kTrackedPrefHistogramNullInitialized, ALLOW_ANY);
[email protected]ae7bf342014-08-06 18:03:47350 EXPECT_EQ(protection_level_ > PROTECTION_DISABLED_ON_PLATFORM,
351 num_tracked_prefs_ > 0);
352
gab342aa6182014-10-02 21:59:00353 // Split tracked prefs are reported as Unchanged not as NullInitialized
[email protected]ae7bf342014-08-06 18:03:47354 // 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.m113481c82015-12-16 05:21:43357 user_prefs::tracked::kTrackedPrefHistogramUnchanged,
358 BEGIN_ALLOW_SINGLE_BUCKET + 5);
[email protected]ae7bf342014-08-06 18:03:47359 EXPECT_EQ(protection_level_ > PROTECTION_DISABLED_ON_PLATFORM ? 1 : 0,
360 num_split_tracked_prefs);
361
proberge269fd092016-10-04 22:13:41362 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]ae7bf342014-08-06 18:03:47379 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]ae7bf342014-08-06 18:03:47420 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
proberge2562e6f2017-04-10 17:46:57429#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]ae7bf342014-08-06 18:03:47432 return PROTECTION_ENABLED_ALL;
433#else
434 return PROTECTION_DISABLED_FOR_GROUP;
Lei Zhangfd5e6f0a2017-11-28 18:24:29435#endif // defined(OS_WIN) || defined(OS_MACOSX)
[email protected]ae7bf342014-08-06 18:03:47436
437#else // defined(OFFICIAL_BUILD)
438
439 using namespace chrome_prefs::internals;
Lei Zhangfd5e6f0a2017-11-28 18:24:29440 if (trial_group == kSettingsEnforcementGroupNoEnforcement)
[email protected]ae7bf342014-08-06 18:03:47441 return PROTECTION_DISABLED_FOR_GROUP;
Lei Zhangfd5e6f0a2017-11-28 18:24:29442 if (trial_group == kSettingsEnforcementGroupEnforceAlways)
[email protected]ae7bf342014-08-06 18:03:47443 return PROTECTION_ENABLED_BASIC;
Lei Zhangfd5e6f0a2017-11-28 18:24:29444 if (trial_group == kSettingsEnforcementGroupEnforceAlwaysWithDSE)
[email protected]ae7bf342014-08-06 18:03:47445 return PROTECTION_ENABLED_DSE;
Lei Zhangfd5e6f0a2017-11-28 18:24:29446 if (trial_group ==
447 kSettingsEnforcementGroupEnforceAlwaysWithExtensionsAndDSE) {
[email protected]ae7bf342014-08-06 18:03:47448 return PROTECTION_ENABLED_EXTENSIONS;
[email protected]ae7bf342014-08-06 18:03:47449 }
Lei Zhangfd5e6f0a2017-11-28 18:24:29450 ADD_FAILURE();
451 return static_cast<SettingsProtectionLevel>(-1);
[email protected]ae7bf342014-08-06 18:03:47452#endif // defined(OFFICIAL_BUILD)
[email protected]ae7bf342014-08-06 18:03:47453 }
454
455 int num_tracked_prefs_;
proberge269fd092016-10-04 22:13:41456
457#if defined(OS_WIN)
458 base::string16 registry_key_for_external_validation_;
459#endif
[email protected]ae7bf342014-08-06 18:03:47460};
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.
466class PrefHashBrowserTestUnchangedDefault : public PrefHashBrowserTestBase {
467 public:
dcheng8f4b8622014-10-23 16:37:48468 void SetupPreferences() override {
[email protected]ae7bf342014-08-06 18:03:47469 // Default Chrome setup.
470 }
471
dcheng8f4b8622014-10-23 16:37:48472 void AttackPreferencesOnDisk(
[email protected]ae7bf342014-08-06 18:03:47473 base::DictionaryValue* unprotected_preferences,
mostynb2b52d1db2014-10-07 02:47:17474 base::DictionaryValue* protected_preferences) override {
[email protected]ae7bf342014-08-06 18:03:47475 // No attack.
476 }
477
dcheng8f4b8622014-10-23 16:37:48478 void VerifyReactionToPrefAttack() override {
[email protected]ae7bf342014-08-06 18:03:47479 // Expect all prefs to be reported as Unchanged with no resets.
deepak.m113481c82015-12-16 05:21:43480 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]ae7bf342014-08-06 18:03:47489 EXPECT_EQ(0,
490 GetTrackedPrefHistogramCount(
deepak.m113481c82015-12-16 05:21:43491 user_prefs::tracked::kTrackedPrefHistogramReset, ALLOW_NONE));
[email protected]ae7bf342014-08-06 18:03:47492
493 // Nothing else should have triggered.
[email protected]ae7bf342014-08-06 18:03:47494 EXPECT_EQ(
deepak.m113481c82015-12-16 05:21:43495 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));
proberge269fd092016-10-04 22:13:41514
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]ae7bf342014-08-06 18:03:47525 }
526};
527
gab36b85522017-05-24 19:30:45528PREF_HASH_BROWSER_TEST(PrefHashBrowserTestUnchangedDefault, UnchangedDefault);
[email protected]ae7bf342014-08-06 18:03:47529
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.
533class PrefHashBrowserTestUnchangedCustom
534 : public PrefHashBrowserTestUnchangedDefault {
535 public:
dcheng8f4b8622014-10-23 16:37:48536 void SetupPreferences() override {
[email protected]ae7bf342014-08-06 18:03:47537 profile()->GetPrefs()->SetString(prefs::kHomePage, "http://example.com");
538
539 InstallExtensionWithUIAutoConfirm(
540 test_data_dir_.AppendASCII("good.crx"), 1, browser());
541 }
542
dcheng8f4b8622014-10-23 16:37:48543 void VerifyReactionToPrefAttack() override {
[email protected]ae7bf342014-08-06 18:03:47544 // 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
gab36b85522017-05-24 19:30:45555PREF_HASH_BROWSER_TEST(PrefHashBrowserTestUnchangedCustom, UnchangedCustom);
[email protected]ae7bf342014-08-06 18:03:47556
557// Verifies that cleared prefs are reported.
558class PrefHashBrowserTestClearedAtomic : public PrefHashBrowserTestBase {
559 public:
dcheng8f4b8622014-10-23 16:37:48560 void SetupPreferences() override {
[email protected]ae7bf342014-08-06 18:03:47561 profile()->GetPrefs()->SetString(prefs::kHomePage, "http://example.com");
562 }
563
dcheng8f4b8622014-10-23 16:37:48564 void AttackPreferencesOnDisk(
[email protected]ae7bf342014-08-06 18:03:47565 base::DictionaryValue* unprotected_preferences,
mostynb2b52d1db2014-10-07 02:47:17566 base::DictionaryValue* protected_preferences) override {
[email protected]ae7bf342014-08-06 18:03:47567 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
dcheng8f4b8622014-10-23 16:37:48576 void VerifyReactionToPrefAttack() override {
[email protected]ae7bf342014-08-06 18:03:47577 // 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]ae7bf342014-08-06 18:03:47581 GetTrackedPrefHistogramCount(
deepak.m113481c82015-12-16 05:21:43582 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]ae7bf342014-08-06 18:03:47593 EXPECT_EQ(0,
594 GetTrackedPrefHistogramCount(
deepak.m113481c82015-12-16 05:21:43595 user_prefs::tracked::kTrackedPrefHistogramReset, ALLOW_NONE));
[email protected]ae7bf342014-08-06 18:03:47596
597 // Nothing else should have triggered.
[email protected]ae7bf342014-08-06 18:03:47598 EXPECT_EQ(
deepak.m113481c82015-12-16 05:21:43599 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));
proberge269fd092016-10-04 22:13:41615
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]ae7bf342014-08-06 18:03:47624 }
625};
626
gab36b85522017-05-24 19:30:45627PREF_HASH_BROWSER_TEST(PrefHashBrowserTestClearedAtomic, ClearedAtomic);
[email protected]ae7bf342014-08-06 18:03:47628
629// Verifies that clearing the MACs results in untrusted Initialized pings for
630// non-null protected prefs.
631class PrefHashBrowserTestUntrustedInitialized : public PrefHashBrowserTestBase {
632 public:
dcheng8f4b8622014-10-23 16:37:48633 void SetupPreferences() override {
[email protected]ae7bf342014-08-06 18:03:47634 // 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
dcheng8f4b8622014-10-23 16:37:48657 void AttackPreferencesOnDisk(
[email protected]ae7bf342014-08-06 18:03:47658 base::DictionaryValue* unprotected_preferences,
mostynb2b52d1db2014-10-07 02:47:17659 base::DictionaryValue* protected_preferences) override {
proberge574d7d92016-08-01 20:14:11660 unprotected_preferences->Remove("protection.macs", NULL);
[email protected]ae7bf342014-08-06 18:03:47661 if (protected_preferences)
proberge574d7d92016-08-01 20:14:11662 protected_preferences->Remove("protection.macs", NULL);
[email protected]ae7bf342014-08-06 18:03:47663 }
664
dcheng8f4b8622014-10-23 16:37:48665 void VerifyReactionToPrefAttack() override {
gab342aa6182014-10-02 21:59:00666 // Preferences that are NULL by default will be NullInitialized.
[email protected]ae7bf342014-08-06 18:03:47667 int num_null_values = GetTrackedPrefHistogramCount(
deepak.m113481c82015-12-16 05:21:43668 user_prefs::tracked::kTrackedPrefHistogramNullInitialized, ALLOW_ANY);
[email protected]ae7bf342014-08-06 18:03:47669 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).
gab342aa6182014-10-02 21:59:00674 EXPECT_GE(num_tracked_prefs() - num_null_values, 3);
[email protected]ae7bf342014-08-06 18:03:47675 }
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.m113481c82015-12-16 05:21:43680 EXPECT_EQ(
681 num_tracked_prefs() - num_null_values,
682 GetTrackedPrefHistogramCount(
683 user_prefs::tracked::kTrackedPrefHistogramInitialized, ALLOW_ANY));
[email protected]ae7bf342014-08-06 18:03:47684
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]ae7bf342014-08-06 18:03:47691 case PROTECTION_ENABLED_EXTENSIONS:
692 ++num_protected_prefs;
Nico Weber1c64f9a2018-01-30 12:00:48693 FALLTHROUGH;
[email protected]ae7bf342014-08-06 18:03:47694 case PROTECTION_ENABLED_DSE:
695 ++num_protected_prefs;
Nico Weber1c64f9a2018-01-30 12:00:48696 FALLTHROUGH;
[email protected]ae7bf342014-08-06 18:03:47697 case PROTECTION_ENABLED_BASIC:
698 num_protected_prefs += num_tracked_prefs() - num_null_values - 2;
Nico Weber1c64f9a2018-01-30 12:00:48699 FALLTHROUGH;
[email protected]ae7bf342014-08-06 18:03:47700 case PROTECTION_DISABLED_FOR_GROUP:
[email protected]ae7bf342014-08-06 18:03:47701 case PROTECTION_DISABLED_ON_PLATFORM:
702 // No protection.
703 break;
704 }
705
deepak.m113481c82015-12-16 05:21:43706 EXPECT_EQ(
707 num_tracked_prefs() - num_null_values - num_protected_prefs,
708 GetTrackedPrefHistogramCount(
709 user_prefs::tracked::kTrackedPrefHistogramWantedReset, ALLOW_ANY));
[email protected]ae7bf342014-08-06 18:03:47710 EXPECT_EQ(num_protected_prefs,
deepak.m113481c82015-12-16 05:21:43711 GetTrackedPrefHistogramCount(
712 user_prefs::tracked::kTrackedPrefHistogramReset, ALLOW_ANY));
[email protected]ae7bf342014-08-06 18:03:47713
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.m113481c82015-12-16 05:21:43731 EXPECT_EQ(0, GetTrackedPrefHistogramCount(
732 user_prefs::tracked::kTrackedPrefHistogramUnchanged,
733 ALLOW_NONE));
[email protected]ae7bf342014-08-06 18:03:47734 EXPECT_EQ(
deepak.m113481c82015-12-16 05:21:43735 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));
proberge269fd092016-10-04 22:13:41744
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]ae7bf342014-08-06 18:03:47756 }
757};
758
759PREF_HASH_BROWSER_TEST(PrefHashBrowserTestUntrustedInitialized,
gab36b85522017-05-24 19:30:45760 UntrustedInitialized);
[email protected]ae7bf342014-08-06 18:03:47761
762// Verifies that changing an atomic pref results in it being reported (and reset
763// if the protection level allows it).
764class PrefHashBrowserTestChangedAtomic : public PrefHashBrowserTestBase {
765 public:
dcheng8f4b8622014-10-23 16:37:48766 void SetupPreferences() override {
[email protected]ae7bf342014-08-06 18:03:47767 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
dcheng8f4b8622014-10-23 16:37:48775 void AttackPreferencesOnDisk(
[email protected]ae7bf342014-08-06 18:03:47776 base::DictionaryValue* unprotected_preferences,
mostynb2b52d1db2014-10-07 02:47:17777 base::DictionaryValue* protected_preferences) override {
[email protected]ae7bf342014-08-06 18:03:47778 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
dcheng8f4b8622014-10-23 16:37:48792 void VerifyReactionToPrefAttack() override {
[email protected]ae7bf342014-08-06 18:03:47793 // Expect a single Changed event for tracked pref #4 (startup URLs).
794 EXPECT_EQ(protection_level_ > PROTECTION_DISABLED_ON_PLATFORM ? 1 : 0,
[email protected]ae7bf342014-08-06 18:03:47795 GetTrackedPrefHistogramCount(
deepak.m113481c82015-12-16 05:21:43796 user_prefs::tracked::kTrackedPrefHistogramChanged,
797 BEGIN_ALLOW_SINGLE_BUCKET + 4));
[email protected]ae7bf342014-08-06 18:03:47798 EXPECT_EQ(
deepak.m113481c82015-12-16 05:21:43799 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]ae7bf342014-08-06 18:03:47812 EXPECT_EQ(protection_level_ >= PROTECTION_ENABLED_BASIC ? 1 : 0,
deepak.m113481c82015-12-16 05:21:43813 GetTrackedPrefHistogramCount(
814 user_prefs::tracked::kTrackedPrefHistogramReset,
815 BEGIN_ALLOW_SINGLE_BUCKET + 4));
[email protected]ae7bf342014-08-06 18:03:47816
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]ae7bf342014-08-06 18:03:47829 EXPECT_EQ(
deepak.m113481c82015-12-16 05:21:43830 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));
proberge269fd092016-10-04 22:13:41846
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]ae7bf342014-08-06 18:03:47855 }
856};
857
gab36b85522017-05-24 19:30:45858PREF_HASH_BROWSER_TEST(PrefHashBrowserTestChangedAtomic, ChangedAtomic);
[email protected]ae7bf342014-08-06 18:03:47859
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).
862class PrefHashBrowserTestChangedSplitPref : public PrefHashBrowserTestBase {
863 public:
dcheng8f4b8622014-10-23 16:37:48864 void SetupPreferences() override {
[email protected]ae7bf342014-08-06 18:03:47865 InstallExtensionWithUIAutoConfirm(
866 test_data_dir_.AppendASCII("good.crx"), 1, browser());
867 }
868
dcheng8f4b8622014-10-23 16:37:48869 void AttackPreferencesOnDisk(
[email protected]ae7bf342014-08-06 18:03:47870 base::DictionaryValue* unprotected_preferences,
mostynb2b52d1db2014-10-07 02:47:17871 base::DictionaryValue* protected_preferences) override {
[email protected]ae7bf342014-08-06 18:03:47872 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).
vabr9984ea62017-04-10 11:33:49894 auto fake_extension = base::MakeUnique<base::DictionaryValue>();
[email protected]ae7bf342014-08-06 18:03:47895 fake_extension->SetString("name", "foo");
vabr9984ea62017-04-10 11:33:49896 extensions_dict->Set(std::string(32, 'a'), std::move(fake_extension));
[email protected]ae7bf342014-08-06 18:03:47897 }
898
dcheng8f4b8622014-10-23 16:37:48899 void VerifyReactionToPrefAttack() override {
[email protected]ae7bf342014-08-06 18:03:47900 // 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.m113481c82015-12-16 05:21:43903 GetTrackedPrefHistogramCount(
904 user_prefs::tracked::kTrackedPrefHistogramChanged,
905 BEGIN_ALLOW_SINGLE_BUCKET + 5));
[email protected]ae7bf342014-08-06 18:03:47906 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]ae7bf342014-08-06 18:03:47912 EXPECT_EQ(
deepak.m113481c82015-12-16 05:21:43913 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]ae7bf342014-08-06 18:03:47926 EXPECT_EQ(protection_level_ >= PROTECTION_ENABLED_EXTENSIONS ? 1 : 0,
deepak.m113481c82015-12-16 05:21:43927 GetTrackedPrefHistogramCount(
928 user_prefs::tracked::kTrackedPrefHistogramReset,
929 BEGIN_ALLOW_SINGLE_BUCKET + 5));
[email protected]ae7bf342014-08-06 18:03:47930
931 EXPECT_EQ(protection_level_ < PROTECTION_ENABLED_EXTENSIONS,
932 extension_service()->GetExtensionById(kGoodCrxId, true) != NULL);
933
934 // Nothing else should have triggered.
[email protected]ae7bf342014-08-06 18:03:47935 EXPECT_EQ(
deepak.m113481c82015-12-16 05:21:43936 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));
proberge269fd092016-10-04 22:13:41952
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]ae7bf342014-08-06 18:03:47962 }
963};
964
gab36b85522017-05-24 19:30:45965PREF_HASH_BROWSER_TEST(PrefHashBrowserTestChangedSplitPref, ChangedSplitPref);
gabb004a562014-09-16 12:45:22966
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)
971class PrefHashBrowserTestUntrustedAdditionToPrefs
972 : public PrefHashBrowserTestBase {
973 public:
dcheng8f4b8622014-10-23 16:37:48974 void SetupPreferences() override {
gabb004a562014-09-16 12:45:22975 // Ensure there is no user-selected value for kRestoreOnStartup.
976 EXPECT_FALSE(
977 profile()->GetPrefs()->GetUserPrefValue(prefs::kRestoreOnStartup));
978 }
979
dcheng8f4b8622014-10-23 16:37:48980 void AttackPreferencesOnDisk(
gabb004a562014-09-16 12:45:22981 base::DictionaryValue* unprotected_preferences,
mostynb2b52d1db2014-10-07 02:47:17982 base::DictionaryValue* protected_preferences) override {
gabb004a562014-09-16 12:45:22983 unprotected_preferences->SetInteger(prefs::kRestoreOnStartup,
984 SessionStartupPref::LAST);
985 }
986
dcheng8f4b8622014-10-23 16:37:48987 void VerifyReactionToPrefAttack() override {
gabb004a562014-09-16 12:45:22988 // 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.m113481c82015-12-16 05:21:43992 EXPECT_EQ((protection_level_ > PROTECTION_DISABLED_ON_PLATFORM &&
993 protection_level_ < PROTECTION_ENABLED_BASIC)
994 ? changed_expected
995 : 0,
gabb004a562014-09-16 12:45:22996 GetTrackedPrefHistogramCount(
deepak.m113481c82015-12-16 05:21:43997 user_prefs::tracked::kTrackedPrefHistogramChanged,
998 BEGIN_ALLOW_SINGLE_BUCKET + 3));
gabb004a562014-09-16 12:45:22999 EXPECT_EQ(
deepak.m113481c82015-12-16 05:21:431000 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));
gabb004a562014-09-16 12:45:221013 EXPECT_EQ(0,
deepak.m113481c82015-12-16 05:21:431014 GetTrackedPrefHistogramCount(
1015 user_prefs::tracked::kTrackedPrefHistogramReset, ALLOW_NONE));
gabb004a562014-09-16 12:45:221016
1017 // Nothing else should have triggered.
gabb004a562014-09-16 12:45:221018 EXPECT_EQ(
deepak.m113481c82015-12-16 05:21:431019 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));
proberge269fd092016-10-04 22:13:411035
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 }
gabb004a562014-09-16 12:45:221046 }
1047};
1048
1049PREF_HASH_BROWSER_TEST(PrefHashBrowserTestUntrustedAdditionToPrefs,
gab36b85522017-05-24 19:30:451050 UntrustedAdditionToPrefs);
erikwrightf4e02b72014-09-17 20:25:451051
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).
1055class PrefHashBrowserTestUntrustedAdditionToPrefsAfterWipe
1056 : public PrefHashBrowserTestBase {
1057 public:
dcheng8f4b8622014-10-23 16:37:481058 void SetupPreferences() override {
erikwrightf4e02b72014-09-17 20:25:451059 profile()->GetPrefs()->SetString(prefs::kHomePage, "http://example.com");
1060 }
1061
dcheng8f4b8622014-10-23 16:37:481062 void AttackPreferencesOnDisk(
erikwrightf4e02b72014-09-17 20:25:451063 base::DictionaryValue* unprotected_preferences,
mostynb2b52d1db2014-10-07 02:47:171064 base::DictionaryValue* protected_preferences) override {
erikwrightf4e02b72014-09-17 20:25:451065 // 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
dcheng8f4b8622014-10-23 16:37:481072 void VerifyReactionToPrefAttack() override {
erikwrightf4e02b72014-09-17 20:25:451073 // 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,
erikwrightf4e02b72014-09-17 20:25:451083 GetTrackedPrefHistogramCount(
deepak.m113481c82015-12-16 05:21:431084 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));
erikwrightf4e02b72014-09-17 20:25:451090 EXPECT_EQ(
deepak.m113481c82015-12-16 05:21:431091 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));
erikwrightf4e02b72014-09-17 20:25:451101 EXPECT_EQ(0,
deepak.m113481c82015-12-16 05:21:431102 GetTrackedPrefHistogramCount(
1103 user_prefs::tracked::kTrackedPrefHistogramReset, ALLOW_NONE));
erikwrightf4e02b72014-09-17 20:25:451104
1105 // Nothing else should have triggered.
deepak.m113481c82015-12-16 05:21:431106 EXPECT_EQ(0, GetTrackedPrefHistogramCount(
1107 user_prefs::tracked::kTrackedPrefHistogramInitialized,
1108 ALLOW_NONE));
erikwrightf4e02b72014-09-17 20:25:451109 EXPECT_EQ(0,
1110 GetTrackedPrefHistogramCount(
deepak.m113481c82015-12-16 05:21:431111 user_prefs::tracked::kTrackedPrefHistogramTrustedInitialized,
1112 ALLOW_NONE));
1113 EXPECT_EQ(0, GetTrackedPrefHistogramCount(
1114 user_prefs::tracked::kTrackedPrefHistogramNullInitialized,
1115 ALLOW_NONE));
erikwrightf4e02b72014-09-17 20:25:451116 EXPECT_EQ(
deepak.m113481c82015-12-16 05:21:431117 0, GetTrackedPrefHistogramCount(
1118 user_prefs::tracked::kTrackedPrefHistogramMigratedLegacyDeviceId,
1119 ALLOW_NONE));
proberge269fd092016-10-04 22:13:411120
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 }
erikwrightf4e02b72014-09-17 20:25:451133 }
1134};
1135
1136PREF_HASH_BROWSER_TEST(PrefHashBrowserTestUntrustedAdditionToPrefsAfterWipe,
gab36b85522017-05-24 19:30:451137 UntrustedAdditionToPrefsAfterWipe);
proberge269fd092016-10-04 22:13:411138
1139#if defined(OS_WIN)
1140class 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
1180PREF_HASH_BROWSER_TEST(PrefHashBrowserTestRegistryValidationFailure,
gab36b85522017-05-24 19:30:451181 RegistryValidationFailure);
proberge269fd092016-10-04 22:13:411182#endif
a-v-y38416862016-12-08 08:40:131183
1184// Verifies that all preferences related to choice of default search engine are
1185// protected.
1186class 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-y38416862016-12-08 08:40:131241
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-y38416862016-12-08 08:40:131247 unprotected_preferences->MergeDictionary(attack1.get());
1248 unprotected_preferences->MergeDictionary(attack2.get());
a-v-y38416862016-12-08 08:40:131249 if (protected_preferences) {
1250 // Override here, too.
1251 protected_preferences->MergeDictionary(attack1.get());
1252 protected_preferences->MergeDictionary(attack2.get());
a-v-y38416862016-12-08 08:40:131253 }
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
gab36b85522017-05-24 19:30:451286PREF_HASH_BROWSER_TEST(PrefHashBrowserTestDefaultSearch, SearchProtected);