blob: 5fcf5e777857ea4adc3e0ae527df1eecc63a0d78 [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
5#include <string>
6
blundell6e85b7c2015-09-29 12:33:357#include "base/base_switches.h"
[email protected]ae7bf342014-08-06 18:03:478#include "base/command_line.h"
[email protected]ae7bf342014-08-06 18:03:479#include "base/files/file_path.h"
thestig18dfb7a52014-08-26 10:44:0410#include "base/files/file_util.h"
[email protected]ae7bf342014-08-06 18:03:4711#include "base/json/json_file_value_serializer.h"
12#include "base/memory/scoped_ptr.h"
13#include "base/metrics/histogram_base.h"
14#include "base/metrics/histogram_samples.h"
15#include "base/metrics/statistics_recorder.h"
16#include "base/path_service.h"
[email protected]ae7bf342014-08-06 18:03:4717#include "base/strings/string_number_conversions.h"
18#include "base/strings/string_util.h"
19#include "base/values.h"
20#include "build/build_config.h"
21#include "chrome/browser/extensions/extension_browsertest.h"
22#include "chrome/browser/extensions/extension_service.h"
23#include "chrome/browser/prefs/chrome_pref_service_factory.h"
24#include "chrome/browser/prefs/profile_pref_store_manager.h"
25#include "chrome/browser/prefs/session_startup_pref.h"
26#include "chrome/browser/profiles/profile.h"
27#include "chrome/browser/ui/browser.h"
28#include "chrome/common/chrome_constants.h"
29#include "chrome/common/chrome_paths.h"
30#include "chrome/common/pref_names.h"
31#include "chrome/test/base/testing_profile.h"
brettwb1fc1b82016-02-02 00:19:0832#include "components/prefs/pref_service.h"
33#include "components/prefs/scoped_user_pref_update.h"
[email protected]ae7bf342014-08-06 18:03:4734#include "components/search_engines/default_search_manager.h"
deepak.m113481c82015-12-16 05:21:4335#include "components/user_prefs/tracked/tracked_preference_histogram_names.h"
[email protected]ae7bf342014-08-06 18:03:4736#include "extensions/browser/pref_names.h"
37#include "extensions/common/extension.h"
38
39#if defined(OS_CHROMEOS)
40#include "chromeos/chromeos_switches.h"
41#endif
42
43namespace {
44
45// Extension ID of chrome/test/data/extensions/good.crx
46const char kGoodCrxId[] = "ldnnhddmnhbkjipkidpdiheffobcpfmf";
47
48// Explicit expectations from the caller of GetTrackedPrefHistogramCount(). This
49// enables detailed reporting of the culprit on failure.
50enum AllowedBuckets {
51 // Allow no samples in any buckets.
52 ALLOW_NONE = -1,
53 // Any integer between BEGIN_ALLOW_SINGLE_BUCKET and END_ALLOW_SINGLE_BUCKET
54 // indicates that only this specific bucket is allowed to have a sample.
55 BEGIN_ALLOW_SINGLE_BUCKET = 0,
56 END_ALLOW_SINGLE_BUCKET = 100,
57 // Allow any buckets (no extra verifications performed).
58 ALLOW_ANY
59};
60
61// Returns the number of times |histogram_name| was reported so far; adding the
62// results of the first 100 buckets (there are only ~19 reporting IDs as of this
63// writing; varies depending on the platform). |allowed_buckets| hints at extra
64// requirements verified in this method (see AllowedBuckets for details).
65int GetTrackedPrefHistogramCount(const char* histogram_name,
66 int allowed_buckets) {
67 const base::HistogramBase* histogram =
68 base::StatisticsRecorder::FindHistogram(histogram_name);
69 if (!histogram)
70 return 0;
71
72 scoped_ptr<base::HistogramSamples> samples(histogram->SnapshotSamples());
73 int sum = 0;
74 for (int i = 0; i < 100; ++i) {
75 int count_for_id = samples->GetCount(i);
76 EXPECT_GE(count_for_id, 0);
77 sum += count_for_id;
78
79 if (allowed_buckets == ALLOW_NONE ||
80 (allowed_buckets != ALLOW_ANY && i != allowed_buckets)) {
81 EXPECT_EQ(0, count_for_id) << "Unexpected reporting_id: " << i;
82 }
83 }
84 return sum;
85}
86
87scoped_ptr<base::DictionaryValue> ReadPrefsDictionary(
88 const base::FilePath& pref_file) {
prashhir54a994502015-03-05 09:30:5789 JSONFileValueDeserializer deserializer(pref_file);
90 int error_code = JSONFileValueDeserializer::JSON_NO_ERROR;
[email protected]ae7bf342014-08-06 18:03:4791 std::string error_str;
olli.raulaba045252015-10-16 06:16:4092 scoped_ptr<base::Value> prefs =
93 deserializer.Deserialize(&error_code, &error_str);
prashhir54a994502015-03-05 09:30:5794 if (!prefs || error_code != JSONFileValueDeserializer::JSON_NO_ERROR) {
[email protected]ae7bf342014-08-06 18:03:4795 ADD_FAILURE() << "Error #" << error_code << ": " << error_str;
96 return scoped_ptr<base::DictionaryValue>();
97 }
98 if (!prefs->IsType(base::Value::TYPE_DICTIONARY)) {
99 ADD_FAILURE();
100 return scoped_ptr<base::DictionaryValue>();
101 }
102 return scoped_ptr<base::DictionaryValue>(
103 static_cast<base::DictionaryValue*>(prefs.release()));
104}
105
106#define PREF_HASH_BROWSER_TEST(fixture, test_name) \
107 IN_PROC_BROWSER_TEST_P(fixture, PRE_##test_name) { \
108 SetupPreferences(); \
109 } \
110 IN_PROC_BROWSER_TEST_P(fixture, test_name) { \
111 VerifyReactionToPrefAttack(); \
112 } \
113 INSTANTIATE_TEST_CASE_P( \
114 fixture##Instance, \
115 fixture, \
116 testing::Values( \
117 chrome_prefs::internals::kSettingsEnforcementGroupNoEnforcement, \
118 chrome_prefs::internals::kSettingsEnforcementGroupEnforceAlways, \
119 chrome_prefs::internals:: \
120 kSettingsEnforcementGroupEnforceAlwaysWithDSE, \
121 chrome_prefs::internals:: \
122 kSettingsEnforcementGroupEnforceAlwaysWithExtensionsAndDSE));
123
124// A base fixture designed such that implementations do two things:
125// 1) Override all three pure-virtual methods below to setup, attack, and
126// verify preferenes throughout the tests provided by this fixture.
127// 2) Instantiate their test via the PREF_HASH_BROWSER_TEST macro above.
128// Based on top of ExtensionBrowserTest to allow easy interaction with the
129// ExtensionService.
130class PrefHashBrowserTestBase
131 : public ExtensionBrowserTest,
132 public testing::WithParamInterface<std::string> {
133 public:
134 // List of potential protection levels for this test in strict increasing
135 // order of protection levels.
136 enum SettingsProtectionLevel {
137 PROTECTION_DISABLED_ON_PLATFORM,
138 PROTECTION_DISABLED_FOR_GROUP,
139 PROTECTION_ENABLED_BASIC,
140 PROTECTION_ENABLED_DSE,
141 PROTECTION_ENABLED_EXTENSIONS,
142 // Represents the strongest level (i.e. always equivalent to the last one in
143 // terms of protection), leave this one last when adding new levels.
144 PROTECTION_ENABLED_ALL
145 };
146
147 PrefHashBrowserTestBase()
148 : protection_level_(GetProtectionLevelFromTrialGroup(GetParam())) {
149 }
150
avi556c05022014-12-22 23:31:43151 void SetUpCommandLine(base::CommandLine* command_line) override {
[email protected]ae7bf342014-08-06 18:03:47152 ExtensionBrowserTest::SetUpCommandLine(command_line);
153 EXPECT_FALSE(command_line->HasSwitch(switches::kForceFieldTrials));
154 command_line->AppendSwitchASCII(
155 switches::kForceFieldTrials,
156 std::string(chrome_prefs::internals::kSettingsEnforcementTrialName) +
157 "/" + GetParam() + "/");
158#if defined(OS_CHROMEOS)
159 command_line->AppendSwitch(
160 chromeos::switches::kIgnoreUserProfileMappingForTests);
161#endif
162 }
163
dcheng8f4b8622014-10-23 16:37:48164 bool SetUpUserDataDirectory() override {
[email protected]ae7bf342014-08-06 18:03:47165 // Do the normal setup in the PRE test and attack preferences in the main
166 // test.
167 if (IsPRETest())
168 return ExtensionBrowserTest::SetUpUserDataDirectory();
169
170#if defined(OS_CHROMEOS)
171 // For some reason, the Preferences file does not exist in the location
172 // below on Chrome OS. Since protection is disabled on Chrome OS, it's okay
173 // to simply not attack preferences at all (and still assert that no
174 // hardening related histogram kicked in in VerifyReactionToPrefAttack()).
175 // TODO(gab): Figure out why there is no Preferences file in this location
176 // on Chrome OS (and re-enable the section disabled for OS_CHROMEOS further
177 // below).
178 EXPECT_EQ(PROTECTION_DISABLED_ON_PLATFORM, protection_level_);
179 return true;
180#endif
181
182 base::FilePath profile_dir;
183 EXPECT_TRUE(PathService::Get(chrome::DIR_USER_DATA, &profile_dir));
184 profile_dir = profile_dir.AppendASCII(TestingProfile::kTestUserProfileDir);
185
186 // Sanity check that old protected pref file is never present in modern
187 // Chromes.
188 EXPECT_FALSE(base::PathExists(
189 profile_dir.Append(chrome::kProtectedPreferencesFilenameDeprecated)));
190
191 // Read the preferences from disk.
192
193 const base::FilePath unprotected_pref_file =
194 profile_dir.Append(chrome::kPreferencesFilename);
195 EXPECT_TRUE(base::PathExists(unprotected_pref_file));
196
197 const base::FilePath protected_pref_file =
198 profile_dir.Append(chrome::kSecurePreferencesFilename);
199 EXPECT_EQ(protection_level_ > PROTECTION_DISABLED_ON_PLATFORM,
200 base::PathExists(protected_pref_file));
201
202 scoped_ptr<base::DictionaryValue> unprotected_preferences(
203 ReadPrefsDictionary(unprotected_pref_file));
204 if (!unprotected_preferences)
205 return false;
206
207 scoped_ptr<base::DictionaryValue> protected_preferences;
208 if (protection_level_ > PROTECTION_DISABLED_ON_PLATFORM) {
209 protected_preferences = ReadPrefsDictionary(protected_pref_file);
210 if (!protected_preferences)
211 return false;
212 }
213
214 // Let the underlying test modify the preferences.
215 AttackPreferencesOnDisk(unprotected_preferences.get(),
216 protected_preferences.get());
217
218 // Write the modified preferences back to disk.
219
220 JSONFileValueSerializer unprotected_prefs_serializer(unprotected_pref_file);
221 EXPECT_TRUE(
222 unprotected_prefs_serializer.Serialize(*unprotected_preferences));
223
224 if (protected_preferences) {
225 JSONFileValueSerializer protected_prefs_serializer(protected_pref_file);
226 EXPECT_TRUE(protected_prefs_serializer.Serialize(*protected_preferences));
227 }
228
229 return true;
230 }
231
gabbcdefd0cc2015-02-17 18:12:40232 void SetUpInProcessBrowserTestFixture() override {
233 ExtensionBrowserTest::SetUpInProcessBrowserTestFixture();
234
235 // Bots are on a domain, turn off the domain check for settings hardening in
236 // order to be able to test all SettingsEnforcement groups.
237 chrome_prefs::DisableDomainCheckForTesting();
238 }
239
[email protected]ae7bf342014-08-06 18:03:47240 // In the PRE_ test, find the number of tracked preferences that were
241 // initialized and save it to a file to be read back in the main test and used
242 // as the total number of tracked preferences.
dcheng8f4b8622014-10-23 16:37:48243 void SetUpOnMainThread() override {
[email protected]ae7bf342014-08-06 18:03:47244 ExtensionBrowserTest::SetUpOnMainThread();
245
246 // File in which the PRE_ test will save the number of tracked preferences
247 // on this platform.
248 const char kNumTrackedPrefFilename[] = "NumTrackedPrefs";
249
250 base::FilePath num_tracked_prefs_file;
251 ASSERT_TRUE(
252 PathService::Get(chrome::DIR_USER_DATA, &num_tracked_prefs_file));
253 num_tracked_prefs_file =
254 num_tracked_prefs_file.AppendASCII(kNumTrackedPrefFilename);
255
256 if (IsPRETest()) {
257 num_tracked_prefs_ = GetTrackedPrefHistogramCount(
deepak.m113481c82015-12-16 05:21:43258 user_prefs::tracked::kTrackedPrefHistogramNullInitialized, ALLOW_ANY);
[email protected]ae7bf342014-08-06 18:03:47259 EXPECT_EQ(protection_level_ > PROTECTION_DISABLED_ON_PLATFORM,
260 num_tracked_prefs_ > 0);
261
gab342aa6182014-10-02 21:59:00262 // Split tracked prefs are reported as Unchanged not as NullInitialized
[email protected]ae7bf342014-08-06 18:03:47263 // when an empty dictionary is encountered on first run (this should only
264 // hit for pref #5 in the current design).
265 int num_split_tracked_prefs = GetTrackedPrefHistogramCount(
deepak.m113481c82015-12-16 05:21:43266 user_prefs::tracked::kTrackedPrefHistogramUnchanged,
267 BEGIN_ALLOW_SINGLE_BUCKET + 5);
[email protected]ae7bf342014-08-06 18:03:47268 EXPECT_EQ(protection_level_ > PROTECTION_DISABLED_ON_PLATFORM ? 1 : 0,
269 num_split_tracked_prefs);
270
271 num_tracked_prefs_ += num_split_tracked_prefs;
272
273 std::string num_tracked_prefs_str = base::IntToString(num_tracked_prefs_);
274 EXPECT_EQ(static_cast<int>(num_tracked_prefs_str.size()),
275 base::WriteFile(num_tracked_prefs_file,
276 num_tracked_prefs_str.c_str(),
277 num_tracked_prefs_str.size()));
278 } else {
279 std::string num_tracked_prefs_str;
280 EXPECT_TRUE(base::ReadFileToString(num_tracked_prefs_file,
281 &num_tracked_prefs_str));
282 EXPECT_TRUE(
283 base::StringToInt(num_tracked_prefs_str, &num_tracked_prefs_));
284 }
285 }
286
287 protected:
288 // Called from the PRE_ test's body. Overrides should use it to setup
289 // preferences through Chrome.
290 virtual void SetupPreferences() = 0;
291
292 // Called prior to the main test launching its browser. Overrides should use
293 // it to attack preferences. |(un)protected_preferences| represent the state
294 // on disk prior to launching the main test, they can be modified by this
295 // method and modifications will be flushed back to disk before launching the
296 // main test. |unprotected_preferences| is never NULL, |protected_preferences|
297 // may be NULL if in PROTECTION_DISABLED_ON_PLATFORM mode.
298 virtual void AttackPreferencesOnDisk(
299 base::DictionaryValue* unprotected_preferences,
300 base::DictionaryValue* protected_preferences) = 0;
301
302 // Called from the body of the main test. Overrides should use it to verify
303 // that the browser had the desired reaction when faced when the attack
304 // orchestrated in AttackPreferencesOnDisk().
305 virtual void VerifyReactionToPrefAttack() = 0;
306
307 int num_tracked_prefs() const { return num_tracked_prefs_; }
308
309 const SettingsProtectionLevel protection_level_;
310
311 private:
312 // Returns true if this is the PRE_ phase of the test.
313 bool IsPRETest() {
brettw66d1b81b2015-07-06 19:29:40314 return base::StartsWith(
brettw44ce0ec52015-06-12 01:57:57315 testing::UnitTest::GetInstance()->current_test_info()->name(), "PRE_",
brettw66d1b81b2015-07-06 19:29:40316 base::CompareCase::SENSITIVE);
[email protected]ae7bf342014-08-06 18:03:47317 }
318
319 SettingsProtectionLevel GetProtectionLevelFromTrialGroup(
320 const std::string& trial_group) {
321 if (!ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking)
322 return PROTECTION_DISABLED_ON_PLATFORM;
323
324// Protection levels can't be adjusted via --force-fieldtrials in official
325// builds.
326#if defined(OFFICIAL_BUILD)
327
328#if defined(OS_WIN)
329 // The strongest mode is enforced on Windows in the absence of a field
330 // trial.
331 return PROTECTION_ENABLED_ALL;
332#else
333 return PROTECTION_DISABLED_FOR_GROUP;
334#endif
335
336#else // defined(OFFICIAL_BUILD)
337
338 using namespace chrome_prefs::internals;
339 if (trial_group == kSettingsEnforcementGroupNoEnforcement) {
340 return PROTECTION_DISABLED_FOR_GROUP;
341 } else if (trial_group == kSettingsEnforcementGroupEnforceAlways) {
342 return PROTECTION_ENABLED_BASIC;
343 } else if (trial_group == kSettingsEnforcementGroupEnforceAlwaysWithDSE) {
344 return PROTECTION_ENABLED_DSE;
345 } else if (trial_group ==
346 kSettingsEnforcementGroupEnforceAlwaysWithExtensionsAndDSE) {
347 return PROTECTION_ENABLED_EXTENSIONS;
348 } else {
349 ADD_FAILURE();
350 return static_cast<SettingsProtectionLevel>(-1);
351 }
352
353#endif // defined(OFFICIAL_BUILD)
354
355 }
356
357 int num_tracked_prefs_;
358};
359
360} // namespace
361
362// Verifies that nothing is reset when nothing is tampered with.
363// Also sanity checks that the expected preferences files are in place.
364class PrefHashBrowserTestUnchangedDefault : public PrefHashBrowserTestBase {
365 public:
dcheng8f4b8622014-10-23 16:37:48366 void SetupPreferences() override {
[email protected]ae7bf342014-08-06 18:03:47367 // Default Chrome setup.
368 }
369
dcheng8f4b8622014-10-23 16:37:48370 void AttackPreferencesOnDisk(
[email protected]ae7bf342014-08-06 18:03:47371 base::DictionaryValue* unprotected_preferences,
mostynb2b52d1db2014-10-07 02:47:17372 base::DictionaryValue* protected_preferences) override {
[email protected]ae7bf342014-08-06 18:03:47373 // No attack.
374 }
375
dcheng8f4b8622014-10-23 16:37:48376 void VerifyReactionToPrefAttack() override {
[email protected]ae7bf342014-08-06 18:03:47377 // Expect all prefs to be reported as Unchanged with no resets.
deepak.m113481c82015-12-16 05:21:43378 EXPECT_EQ(
379 protection_level_ > PROTECTION_DISABLED_ON_PLATFORM
380 ? num_tracked_prefs()
381 : 0,
382 GetTrackedPrefHistogramCount(
383 user_prefs::tracked::kTrackedPrefHistogramUnchanged, ALLOW_ANY));
384 EXPECT_EQ(0, GetTrackedPrefHistogramCount(
385 user_prefs::tracked::kTrackedPrefHistogramWantedReset,
386 ALLOW_NONE));
[email protected]ae7bf342014-08-06 18:03:47387 EXPECT_EQ(0,
388 GetTrackedPrefHistogramCount(
deepak.m113481c82015-12-16 05:21:43389 user_prefs::tracked::kTrackedPrefHistogramReset, ALLOW_NONE));
[email protected]ae7bf342014-08-06 18:03:47390
391 // Nothing else should have triggered.
[email protected]ae7bf342014-08-06 18:03:47392 EXPECT_EQ(
deepak.m113481c82015-12-16 05:21:43393 0, GetTrackedPrefHistogramCount(
394 user_prefs::tracked::kTrackedPrefHistogramChanged, ALLOW_NONE));
395 EXPECT_EQ(
396 0, GetTrackedPrefHistogramCount(
397 user_prefs::tracked::kTrackedPrefHistogramCleared, ALLOW_NONE));
398 EXPECT_EQ(0, GetTrackedPrefHistogramCount(
399 user_prefs::tracked::kTrackedPrefHistogramInitialized,
400 ALLOW_NONE));
401 EXPECT_EQ(0,
402 GetTrackedPrefHistogramCount(
403 user_prefs::tracked::kTrackedPrefHistogramTrustedInitialized,
404 ALLOW_NONE));
405 EXPECT_EQ(0, GetTrackedPrefHistogramCount(
406 user_prefs::tracked::kTrackedPrefHistogramNullInitialized,
407 ALLOW_NONE));
408 EXPECT_EQ(
409 0, GetTrackedPrefHistogramCount(
410 user_prefs::tracked::kTrackedPrefHistogramMigratedLegacyDeviceId,
411 ALLOW_NONE));
[email protected]ae7bf342014-08-06 18:03:47412 }
413};
414
415PREF_HASH_BROWSER_TEST(PrefHashBrowserTestUnchangedDefault, UnchangedDefault);
416
417// Augments PrefHashBrowserTestUnchangedDefault to confirm that nothing is reset
418// when nothing is tampered with, even if Chrome itself wrote custom prefs in
419// its last run.
420class PrefHashBrowserTestUnchangedCustom
421 : public PrefHashBrowserTestUnchangedDefault {
422 public:
dcheng8f4b8622014-10-23 16:37:48423 void SetupPreferences() override {
[email protected]ae7bf342014-08-06 18:03:47424 profile()->GetPrefs()->SetString(prefs::kHomePage, "http://example.com");
425
426 InstallExtensionWithUIAutoConfirm(
427 test_data_dir_.AppendASCII("good.crx"), 1, browser());
428 }
429
dcheng8f4b8622014-10-23 16:37:48430 void VerifyReactionToPrefAttack() override {
[email protected]ae7bf342014-08-06 18:03:47431 // Make sure the settings written in the last run stuck.
432 EXPECT_EQ("http://example.com",
433 profile()->GetPrefs()->GetString(prefs::kHomePage));
434
435 EXPECT_TRUE(extension_service()->GetExtensionById(kGoodCrxId, false));
436
437 // Reaction should be identical to unattacked default prefs.
438 PrefHashBrowserTestUnchangedDefault::VerifyReactionToPrefAttack();
439 }
440};
441
442PREF_HASH_BROWSER_TEST(PrefHashBrowserTestUnchangedCustom, UnchangedCustom);
443
444// Verifies that cleared prefs are reported.
445class PrefHashBrowserTestClearedAtomic : public PrefHashBrowserTestBase {
446 public:
dcheng8f4b8622014-10-23 16:37:48447 void SetupPreferences() override {
[email protected]ae7bf342014-08-06 18:03:47448 profile()->GetPrefs()->SetString(prefs::kHomePage, "http://example.com");
449 }
450
dcheng8f4b8622014-10-23 16:37:48451 void AttackPreferencesOnDisk(
[email protected]ae7bf342014-08-06 18:03:47452 base::DictionaryValue* unprotected_preferences,
mostynb2b52d1db2014-10-07 02:47:17453 base::DictionaryValue* protected_preferences) override {
[email protected]ae7bf342014-08-06 18:03:47454 base::DictionaryValue* selected_prefs =
455 protection_level_ >= PROTECTION_ENABLED_BASIC ? protected_preferences
456 : unprotected_preferences;
457 // |selected_prefs| should never be NULL under the protection level picking
458 // it.
459 EXPECT_TRUE(selected_prefs);
460 EXPECT_TRUE(selected_prefs->Remove(prefs::kHomePage, NULL));
461 }
462
dcheng8f4b8622014-10-23 16:37:48463 void VerifyReactionToPrefAttack() override {
[email protected]ae7bf342014-08-06 18:03:47464 // The clearance of homepage should have been noticed (as pref #2 being
465 // cleared), but shouldn't have triggered a reset (as there is nothing we
466 // can do when the pref is already gone).
467 EXPECT_EQ(protection_level_ > PROTECTION_DISABLED_ON_PLATFORM ? 1 : 0,
[email protected]ae7bf342014-08-06 18:03:47468 GetTrackedPrefHistogramCount(
deepak.m113481c82015-12-16 05:21:43469 user_prefs::tracked::kTrackedPrefHistogramCleared,
470 BEGIN_ALLOW_SINGLE_BUCKET + 2));
471 EXPECT_EQ(
472 protection_level_ > PROTECTION_DISABLED_ON_PLATFORM
473 ? num_tracked_prefs() - 1
474 : 0,
475 GetTrackedPrefHistogramCount(
476 user_prefs::tracked::kTrackedPrefHistogramUnchanged, ALLOW_ANY));
477 EXPECT_EQ(0, GetTrackedPrefHistogramCount(
478 user_prefs::tracked::kTrackedPrefHistogramWantedReset,
479 ALLOW_NONE));
[email protected]ae7bf342014-08-06 18:03:47480 EXPECT_EQ(0,
481 GetTrackedPrefHistogramCount(
deepak.m113481c82015-12-16 05:21:43482 user_prefs::tracked::kTrackedPrefHistogramReset, ALLOW_NONE));
[email protected]ae7bf342014-08-06 18:03:47483
484 // Nothing else should have triggered.
[email protected]ae7bf342014-08-06 18:03:47485 EXPECT_EQ(
deepak.m113481c82015-12-16 05:21:43486 0, GetTrackedPrefHistogramCount(
487 user_prefs::tracked::kTrackedPrefHistogramChanged, ALLOW_NONE));
488 EXPECT_EQ(0, GetTrackedPrefHistogramCount(
489 user_prefs::tracked::kTrackedPrefHistogramInitialized,
490 ALLOW_NONE));
491 EXPECT_EQ(0,
492 GetTrackedPrefHistogramCount(
493 user_prefs::tracked::kTrackedPrefHistogramTrustedInitialized,
494 ALLOW_NONE));
495 EXPECT_EQ(0, GetTrackedPrefHistogramCount(
496 user_prefs::tracked::kTrackedPrefHistogramNullInitialized,
497 ALLOW_NONE));
498 EXPECT_EQ(
499 0, GetTrackedPrefHistogramCount(
500 user_prefs::tracked::kTrackedPrefHistogramMigratedLegacyDeviceId,
501 ALLOW_NONE));
[email protected]ae7bf342014-08-06 18:03:47502 }
503};
504
505PREF_HASH_BROWSER_TEST(PrefHashBrowserTestClearedAtomic, ClearedAtomic);
506
507// Verifies that clearing the MACs results in untrusted Initialized pings for
508// non-null protected prefs.
509class PrefHashBrowserTestUntrustedInitialized : public PrefHashBrowserTestBase {
510 public:
dcheng8f4b8622014-10-23 16:37:48511 void SetupPreferences() override {
[email protected]ae7bf342014-08-06 18:03:47512 // Explicitly set the DSE (it's otherwise NULL by default, preventing
513 // thorough testing of the PROTECTION_ENABLED_DSE level).
514 DefaultSearchManager default_search_manager(
515 profile()->GetPrefs(), DefaultSearchManager::ObserverCallback());
516 DefaultSearchManager::Source dse_source =
517 static_cast<DefaultSearchManager::Source>(-1);
518
519 const TemplateURLData* default_template_url_data =
520 default_search_manager.GetDefaultSearchEngine(&dse_source);
521 EXPECT_EQ(DefaultSearchManager::FROM_FALLBACK, dse_source);
522
523 default_search_manager.SetUserSelectedDefaultSearchEngine(
524 *default_template_url_data);
525
526 default_search_manager.GetDefaultSearchEngine(&dse_source);
527 EXPECT_EQ(DefaultSearchManager::FROM_USER, dse_source);
528
529 // Also explicitly set an atomic pref that falls under
530 // PROTECTION_ENABLED_BASIC.
531 profile()->GetPrefs()->SetInteger(prefs::kRestoreOnStartup,
532 SessionStartupPref::URLS);
533 }
534
dcheng8f4b8622014-10-23 16:37:48535 void AttackPreferencesOnDisk(
[email protected]ae7bf342014-08-06 18:03:47536 base::DictionaryValue* unprotected_preferences,
mostynb2b52d1db2014-10-07 02:47:17537 base::DictionaryValue* protected_preferences) override {
[email protected]ae7bf342014-08-06 18:03:47538 EXPECT_TRUE(unprotected_preferences->Remove("protection.macs", NULL));
539 if (protected_preferences)
540 EXPECT_TRUE(protected_preferences->Remove("protection.macs", NULL));
541 }
542
dcheng8f4b8622014-10-23 16:37:48543 void VerifyReactionToPrefAttack() override {
gab342aa6182014-10-02 21:59:00544 // Preferences that are NULL by default will be NullInitialized.
[email protected]ae7bf342014-08-06 18:03:47545 int num_null_values = GetTrackedPrefHistogramCount(
deepak.m113481c82015-12-16 05:21:43546 user_prefs::tracked::kTrackedPrefHistogramNullInitialized, ALLOW_ANY);
[email protected]ae7bf342014-08-06 18:03:47547 EXPECT_EQ(protection_level_ > PROTECTION_DISABLED_ON_PLATFORM,
548 num_null_values > 0);
549 if (num_null_values > 0) {
550 // This test requires that at least 3 prefs be non-null (extensions, DSE,
551 // and 1 atomic pref explictly set for this test above).
gab342aa6182014-10-02 21:59:00552 EXPECT_GE(num_tracked_prefs() - num_null_values, 3);
[email protected]ae7bf342014-08-06 18:03:47553 }
554
555 // Expect all non-null prefs to be reported as Initialized (with
556 // accompanying resets or wanted resets based on the current protection
557 // level).
deepak.m113481c82015-12-16 05:21:43558 EXPECT_EQ(
559 num_tracked_prefs() - num_null_values,
560 GetTrackedPrefHistogramCount(
561 user_prefs::tracked::kTrackedPrefHistogramInitialized, ALLOW_ANY));
[email protected]ae7bf342014-08-06 18:03:47562
563 int num_protected_prefs = 0;
564 // A switch statement falling through each protection level in decreasing
565 // levels of protection to add expectations for each level which augments
566 // the previous one.
567 switch (protection_level_) {
568 case PROTECTION_ENABLED_ALL:
569 // Falls through.
570 case PROTECTION_ENABLED_EXTENSIONS:
571 ++num_protected_prefs;
572 // Falls through.
573 case PROTECTION_ENABLED_DSE:
574 ++num_protected_prefs;
575 // Falls through.
576 case PROTECTION_ENABLED_BASIC:
577 num_protected_prefs += num_tracked_prefs() - num_null_values - 2;
578 // Falls through.
579 case PROTECTION_DISABLED_FOR_GROUP:
580 // No protection. Falls through.
581 case PROTECTION_DISABLED_ON_PLATFORM:
582 // No protection.
583 break;
584 }
585
deepak.m113481c82015-12-16 05:21:43586 EXPECT_EQ(
587 num_tracked_prefs() - num_null_values - num_protected_prefs,
588 GetTrackedPrefHistogramCount(
589 user_prefs::tracked::kTrackedPrefHistogramWantedReset, ALLOW_ANY));
[email protected]ae7bf342014-08-06 18:03:47590 EXPECT_EQ(num_protected_prefs,
deepak.m113481c82015-12-16 05:21:43591 GetTrackedPrefHistogramCount(
592 user_prefs::tracked::kTrackedPrefHistogramReset, ALLOW_ANY));
[email protected]ae7bf342014-08-06 18:03:47593
594 // Explicitly verify the result of reported resets.
595
596 DefaultSearchManager default_search_manager(
597 profile()->GetPrefs(), DefaultSearchManager::ObserverCallback());
598 DefaultSearchManager::Source dse_source =
599 static_cast<DefaultSearchManager::Source>(-1);
600 default_search_manager.GetDefaultSearchEngine(&dse_source);
601 EXPECT_EQ(protection_level_ < PROTECTION_ENABLED_DSE
602 ? DefaultSearchManager::FROM_USER
603 : DefaultSearchManager::FROM_FALLBACK,
604 dse_source);
605
606 EXPECT_EQ(protection_level_ < PROTECTION_ENABLED_BASIC,
607 profile()->GetPrefs()->GetInteger(prefs::kRestoreOnStartup) ==
608 SessionStartupPref::URLS);
609
610 // Nothing else should have triggered.
deepak.m113481c82015-12-16 05:21:43611 EXPECT_EQ(0, GetTrackedPrefHistogramCount(
612 user_prefs::tracked::kTrackedPrefHistogramUnchanged,
613 ALLOW_NONE));
[email protected]ae7bf342014-08-06 18:03:47614 EXPECT_EQ(
deepak.m113481c82015-12-16 05:21:43615 0, GetTrackedPrefHistogramCount(
616 user_prefs::tracked::kTrackedPrefHistogramChanged, ALLOW_NONE));
617 EXPECT_EQ(
618 0, GetTrackedPrefHistogramCount(
619 user_prefs::tracked::kTrackedPrefHistogramCleared, ALLOW_NONE));
620 EXPECT_EQ(
621 0, GetTrackedPrefHistogramCount(
622 user_prefs::tracked::kTrackedPrefHistogramMigratedLegacyDeviceId,
623 ALLOW_NONE));
[email protected]ae7bf342014-08-06 18:03:47624 }
625};
626
627PREF_HASH_BROWSER_TEST(PrefHashBrowserTestUntrustedInitialized,
628 UntrustedInitialized);
629
630// Verifies that changing an atomic pref results in it being reported (and reset
631// if the protection level allows it).
632class PrefHashBrowserTestChangedAtomic : public PrefHashBrowserTestBase {
633 public:
dcheng8f4b8622014-10-23 16:37:48634 void SetupPreferences() override {
[email protected]ae7bf342014-08-06 18:03:47635 profile()->GetPrefs()->SetInteger(prefs::kRestoreOnStartup,
636 SessionStartupPref::URLS);
637
638 ListPrefUpdate update(profile()->GetPrefs(),
639 prefs::kURLsToRestoreOnStartup);
640 update->AppendString("http://example.com");
641 }
642
dcheng8f4b8622014-10-23 16:37:48643 void AttackPreferencesOnDisk(
[email protected]ae7bf342014-08-06 18:03:47644 base::DictionaryValue* unprotected_preferences,
mostynb2b52d1db2014-10-07 02:47:17645 base::DictionaryValue* protected_preferences) override {
[email protected]ae7bf342014-08-06 18:03:47646 base::DictionaryValue* selected_prefs =
647 protection_level_ >= PROTECTION_ENABLED_BASIC ? protected_preferences
648 : unprotected_preferences;
649 // |selected_prefs| should never be NULL under the protection level picking
650 // it.
651 EXPECT_TRUE(selected_prefs);
652 base::ListValue* startup_urls;
653 EXPECT_TRUE(
654 selected_prefs->GetList(prefs::kURLsToRestoreOnStartup, &startup_urls));
655 EXPECT_TRUE(startup_urls);
656 EXPECT_EQ(1U, startup_urls->GetSize());
657 startup_urls->AppendString("http://example.org");
658 }
659
dcheng8f4b8622014-10-23 16:37:48660 void VerifyReactionToPrefAttack() override {
[email protected]ae7bf342014-08-06 18:03:47661 // Expect a single Changed event for tracked pref #4 (startup URLs).
662 EXPECT_EQ(protection_level_ > PROTECTION_DISABLED_ON_PLATFORM ? 1 : 0,
[email protected]ae7bf342014-08-06 18:03:47663 GetTrackedPrefHistogramCount(
deepak.m113481c82015-12-16 05:21:43664 user_prefs::tracked::kTrackedPrefHistogramChanged,
665 BEGIN_ALLOW_SINGLE_BUCKET + 4));
[email protected]ae7bf342014-08-06 18:03:47666 EXPECT_EQ(
deepak.m113481c82015-12-16 05:21:43667 protection_level_ > PROTECTION_DISABLED_ON_PLATFORM
668 ? num_tracked_prefs() - 1
669 : 0,
670 GetTrackedPrefHistogramCount(
671 user_prefs::tracked::kTrackedPrefHistogramUnchanged, ALLOW_ANY));
672
673 EXPECT_EQ((protection_level_ > PROTECTION_DISABLED_ON_PLATFORM &&
674 protection_level_ < PROTECTION_ENABLED_BASIC)
675 ? 1
676 : 0,
677 GetTrackedPrefHistogramCount(
678 user_prefs::tracked::kTrackedPrefHistogramWantedReset,
679 BEGIN_ALLOW_SINGLE_BUCKET + 4));
[email protected]ae7bf342014-08-06 18:03:47680 EXPECT_EQ(protection_level_ >= PROTECTION_ENABLED_BASIC ? 1 : 0,
deepak.m113481c82015-12-16 05:21:43681 GetTrackedPrefHistogramCount(
682 user_prefs::tracked::kTrackedPrefHistogramReset,
683 BEGIN_ALLOW_SINGLE_BUCKET + 4));
[email protected]ae7bf342014-08-06 18:03:47684
685// TODO(gab): This doesn't work on OS_CHROMEOS because we fail to attack
686// Preferences.
687#if !defined(OS_CHROMEOS)
688 // Explicitly verify the result of reported resets.
689 EXPECT_EQ(protection_level_ >= PROTECTION_ENABLED_BASIC ? 0U : 2U,
690 profile()
691 ->GetPrefs()
692 ->GetList(prefs::kURLsToRestoreOnStartup)
693 ->GetSize());
694#endif
695
696 // Nothing else should have triggered.
[email protected]ae7bf342014-08-06 18:03:47697 EXPECT_EQ(
deepak.m113481c82015-12-16 05:21:43698 0, GetTrackedPrefHistogramCount(
699 user_prefs::tracked::kTrackedPrefHistogramCleared, ALLOW_NONE));
700 EXPECT_EQ(0, GetTrackedPrefHistogramCount(
701 user_prefs::tracked::kTrackedPrefHistogramInitialized,
702 ALLOW_NONE));
703 EXPECT_EQ(0,
704 GetTrackedPrefHistogramCount(
705 user_prefs::tracked::kTrackedPrefHistogramTrustedInitialized,
706 ALLOW_NONE));
707 EXPECT_EQ(0, GetTrackedPrefHistogramCount(
708 user_prefs::tracked::kTrackedPrefHistogramNullInitialized,
709 ALLOW_NONE));
710 EXPECT_EQ(
711 0, GetTrackedPrefHistogramCount(
712 user_prefs::tracked::kTrackedPrefHistogramMigratedLegacyDeviceId,
713 ALLOW_NONE));
[email protected]ae7bf342014-08-06 18:03:47714 }
715};
716
717PREF_HASH_BROWSER_TEST(PrefHashBrowserTestChangedAtomic, ChangedAtomic);
718
719// Verifies that changing or adding an entry in a split pref results in both
720// items being reported (and remove if the protection level allows it).
721class PrefHashBrowserTestChangedSplitPref : public PrefHashBrowserTestBase {
722 public:
dcheng8f4b8622014-10-23 16:37:48723 void SetupPreferences() override {
[email protected]ae7bf342014-08-06 18:03:47724 InstallExtensionWithUIAutoConfirm(
725 test_data_dir_.AppendASCII("good.crx"), 1, browser());
726 }
727
dcheng8f4b8622014-10-23 16:37:48728 void AttackPreferencesOnDisk(
[email protected]ae7bf342014-08-06 18:03:47729 base::DictionaryValue* unprotected_preferences,
mostynb2b52d1db2014-10-07 02:47:17730 base::DictionaryValue* protected_preferences) override {
[email protected]ae7bf342014-08-06 18:03:47731 base::DictionaryValue* selected_prefs =
732 protection_level_ >= PROTECTION_ENABLED_EXTENSIONS
733 ? protected_preferences
734 : unprotected_preferences;
735 // |selected_prefs| should never be NULL under the protection level picking
736 // it.
737 EXPECT_TRUE(selected_prefs);
738 base::DictionaryValue* extensions_dict;
739 EXPECT_TRUE(selected_prefs->GetDictionary(
740 extensions::pref_names::kExtensions, &extensions_dict));
741 EXPECT_TRUE(extensions_dict);
742
743 // Tamper with any installed setting for good.crx
744 base::DictionaryValue* good_crx_dict;
745 EXPECT_TRUE(extensions_dict->GetDictionary(kGoodCrxId, &good_crx_dict));
746 int good_crx_state;
747 EXPECT_TRUE(good_crx_dict->GetInteger("state", &good_crx_state));
748 EXPECT_EQ(extensions::Extension::ENABLED, good_crx_state);
749 good_crx_dict->SetInteger("state", extensions::Extension::DISABLED);
750
751 // Drop a fake extension (for the purpose of this test, dropped settings
752 // don't need to be valid extension settings).
753 base::DictionaryValue* fake_extension = new base::DictionaryValue;
754 fake_extension->SetString("name", "foo");
755 extensions_dict->Set(std::string(32, 'a'), fake_extension);
756 }
757
dcheng8f4b8622014-10-23 16:37:48758 void VerifyReactionToPrefAttack() override {
[email protected]ae7bf342014-08-06 18:03:47759 // Expect a single split pref changed report with a count of 2 for tracked
760 // pref #5 (extensions).
761 EXPECT_EQ(protection_level_ > PROTECTION_DISABLED_ON_PLATFORM ? 1 : 0,
deepak.m113481c82015-12-16 05:21:43762 GetTrackedPrefHistogramCount(
763 user_prefs::tracked::kTrackedPrefHistogramChanged,
764 BEGIN_ALLOW_SINGLE_BUCKET + 5));
[email protected]ae7bf342014-08-06 18:03:47765 EXPECT_EQ(protection_level_ > PROTECTION_DISABLED_ON_PLATFORM ? 1 : 0,
766 GetTrackedPrefHistogramCount(
767 "Settings.TrackedSplitPreferenceChanged.extensions.settings",
768 BEGIN_ALLOW_SINGLE_BUCKET + 2));
769
770 // Everything else should have remained unchanged.
[email protected]ae7bf342014-08-06 18:03:47771 EXPECT_EQ(
deepak.m113481c82015-12-16 05:21:43772 protection_level_ > PROTECTION_DISABLED_ON_PLATFORM
773 ? num_tracked_prefs() - 1
774 : 0,
775 GetTrackedPrefHistogramCount(
776 user_prefs::tracked::kTrackedPrefHistogramUnchanged, ALLOW_ANY));
777
778 EXPECT_EQ((protection_level_ > PROTECTION_DISABLED_ON_PLATFORM &&
779 protection_level_ < PROTECTION_ENABLED_EXTENSIONS)
780 ? 1
781 : 0,
782 GetTrackedPrefHistogramCount(
783 user_prefs::tracked::kTrackedPrefHistogramWantedReset,
784 BEGIN_ALLOW_SINGLE_BUCKET + 5));
[email protected]ae7bf342014-08-06 18:03:47785 EXPECT_EQ(protection_level_ >= PROTECTION_ENABLED_EXTENSIONS ? 1 : 0,
deepak.m113481c82015-12-16 05:21:43786 GetTrackedPrefHistogramCount(
787 user_prefs::tracked::kTrackedPrefHistogramReset,
788 BEGIN_ALLOW_SINGLE_BUCKET + 5));
[email protected]ae7bf342014-08-06 18:03:47789
790 EXPECT_EQ(protection_level_ < PROTECTION_ENABLED_EXTENSIONS,
791 extension_service()->GetExtensionById(kGoodCrxId, true) != NULL);
792
793 // Nothing else should have triggered.
[email protected]ae7bf342014-08-06 18:03:47794 EXPECT_EQ(
deepak.m113481c82015-12-16 05:21:43795 0, GetTrackedPrefHistogramCount(
796 user_prefs::tracked::kTrackedPrefHistogramCleared, ALLOW_NONE));
797 EXPECT_EQ(0, GetTrackedPrefHistogramCount(
798 user_prefs::tracked::kTrackedPrefHistogramInitialized,
799 ALLOW_NONE));
800 EXPECT_EQ(0,
801 GetTrackedPrefHistogramCount(
802 user_prefs::tracked::kTrackedPrefHistogramTrustedInitialized,
803 ALLOW_NONE));
804 EXPECT_EQ(0, GetTrackedPrefHistogramCount(
805 user_prefs::tracked::kTrackedPrefHistogramNullInitialized,
806 ALLOW_NONE));
807 EXPECT_EQ(
808 0, GetTrackedPrefHistogramCount(
809 user_prefs::tracked::kTrackedPrefHistogramMigratedLegacyDeviceId,
810 ALLOW_NONE));
[email protected]ae7bf342014-08-06 18:03:47811 }
812};
813
814PREF_HASH_BROWSER_TEST(PrefHashBrowserTestChangedSplitPref, ChangedSplitPref);
gabb004a562014-09-16 12:45:22815
816// Verifies that adding a value to unprotected preferences for a key which is
817// still using the default (i.e. has no value stored in protected preferences)
818// doesn't allow that value to slip in with no valid MAC (regression test for
819// http://crbug.com/414554)
820class PrefHashBrowserTestUntrustedAdditionToPrefs
821 : public PrefHashBrowserTestBase {
822 public:
dcheng8f4b8622014-10-23 16:37:48823 void SetupPreferences() override {
gabb004a562014-09-16 12:45:22824 // Ensure there is no user-selected value for kRestoreOnStartup.
825 EXPECT_FALSE(
826 profile()->GetPrefs()->GetUserPrefValue(prefs::kRestoreOnStartup));
827 }
828
dcheng8f4b8622014-10-23 16:37:48829 void AttackPreferencesOnDisk(
gabb004a562014-09-16 12:45:22830 base::DictionaryValue* unprotected_preferences,
mostynb2b52d1db2014-10-07 02:47:17831 base::DictionaryValue* protected_preferences) override {
gabb004a562014-09-16 12:45:22832 unprotected_preferences->SetInteger(prefs::kRestoreOnStartup,
833 SessionStartupPref::LAST);
834 }
835
dcheng8f4b8622014-10-23 16:37:48836 void VerifyReactionToPrefAttack() override {
gabb004a562014-09-16 12:45:22837 // Expect a single Changed event for tracked pref #3 (kRestoreOnStartup) if
838 // not protecting; if protection is enabled the change should be a no-op.
839 int changed_expected =
840 protection_level_ == PROTECTION_DISABLED_FOR_GROUP ? 1 : 0;
deepak.m113481c82015-12-16 05:21:43841 EXPECT_EQ((protection_level_ > PROTECTION_DISABLED_ON_PLATFORM &&
842 protection_level_ < PROTECTION_ENABLED_BASIC)
843 ? changed_expected
844 : 0,
gabb004a562014-09-16 12:45:22845 GetTrackedPrefHistogramCount(
deepak.m113481c82015-12-16 05:21:43846 user_prefs::tracked::kTrackedPrefHistogramChanged,
847 BEGIN_ALLOW_SINGLE_BUCKET + 3));
gabb004a562014-09-16 12:45:22848 EXPECT_EQ(
deepak.m113481c82015-12-16 05:21:43849 protection_level_ > PROTECTION_DISABLED_ON_PLATFORM
850 ? num_tracked_prefs() - changed_expected
851 : 0,
852 GetTrackedPrefHistogramCount(
853 user_prefs::tracked::kTrackedPrefHistogramUnchanged, ALLOW_ANY));
854
855 EXPECT_EQ((protection_level_ > PROTECTION_DISABLED_ON_PLATFORM &&
856 protection_level_ < PROTECTION_ENABLED_BASIC)
857 ? 1
858 : 0,
859 GetTrackedPrefHistogramCount(
860 user_prefs::tracked::kTrackedPrefHistogramWantedReset,
861 BEGIN_ALLOW_SINGLE_BUCKET + 3));
gabb004a562014-09-16 12:45:22862 EXPECT_EQ(0,
deepak.m113481c82015-12-16 05:21:43863 GetTrackedPrefHistogramCount(
864 user_prefs::tracked::kTrackedPrefHistogramReset, ALLOW_NONE));
gabb004a562014-09-16 12:45:22865
866 // Nothing else should have triggered.
gabb004a562014-09-16 12:45:22867 EXPECT_EQ(
deepak.m113481c82015-12-16 05:21:43868 0, GetTrackedPrefHistogramCount(
869 user_prefs::tracked::kTrackedPrefHistogramCleared, ALLOW_NONE));
870 EXPECT_EQ(0, GetTrackedPrefHistogramCount(
871 user_prefs::tracked::kTrackedPrefHistogramInitialized,
872 ALLOW_NONE));
873 EXPECT_EQ(0,
874 GetTrackedPrefHistogramCount(
875 user_prefs::tracked::kTrackedPrefHistogramTrustedInitialized,
876 ALLOW_NONE));
877 EXPECT_EQ(0, GetTrackedPrefHistogramCount(
878 user_prefs::tracked::kTrackedPrefHistogramNullInitialized,
879 ALLOW_NONE));
880 EXPECT_EQ(
881 0, GetTrackedPrefHistogramCount(
882 user_prefs::tracked::kTrackedPrefHistogramMigratedLegacyDeviceId,
883 ALLOW_NONE));
gabb004a562014-09-16 12:45:22884 }
885};
886
887PREF_HASH_BROWSER_TEST(PrefHashBrowserTestUntrustedAdditionToPrefs,
888 UntrustedAdditionToPrefs);
erikwrightf4e02b72014-09-17 20:25:45889
890// Verifies that adding a value to unprotected preferences while wiping a
891// user-selected value from protected preferences doesn't allow that value to
892// slip in with no valid MAC (regression test for http://crbug.com/414554).
893class PrefHashBrowserTestUntrustedAdditionToPrefsAfterWipe
894 : public PrefHashBrowserTestBase {
895 public:
dcheng8f4b8622014-10-23 16:37:48896 void SetupPreferences() override {
erikwrightf4e02b72014-09-17 20:25:45897 profile()->GetPrefs()->SetString(prefs::kHomePage, "http://example.com");
898 }
899
dcheng8f4b8622014-10-23 16:37:48900 void AttackPreferencesOnDisk(
erikwrightf4e02b72014-09-17 20:25:45901 base::DictionaryValue* unprotected_preferences,
mostynb2b52d1db2014-10-07 02:47:17902 base::DictionaryValue* protected_preferences) override {
erikwrightf4e02b72014-09-17 20:25:45903 // Set or change the value in Preferences to the attacker's choice.
904 unprotected_preferences->SetString(prefs::kHomePage, "http://example.net");
905 // Clear the value in Secure Preferences, if any.
906 if (protected_preferences)
907 protected_preferences->Remove(prefs::kHomePage, NULL);
908 }
909
dcheng8f4b8622014-10-23 16:37:48910 void VerifyReactionToPrefAttack() override {
erikwrightf4e02b72014-09-17 20:25:45911 // Expect a single Changed event for tracked pref #2 (kHomePage) if
912 // not protecting; if protection is enabled the change should be a Cleared.
913 int changed_expected =
914 protection_level_ > PROTECTION_DISABLED_ON_PLATFORM &&
915 protection_level_ < PROTECTION_ENABLED_BASIC
916 ? 1 : 0;
917 int cleared_expected =
918 protection_level_ >= PROTECTION_ENABLED_BASIC
919 ? 1 : 0;
920 EXPECT_EQ(changed_expected,
erikwrightf4e02b72014-09-17 20:25:45921 GetTrackedPrefHistogramCount(
deepak.m113481c82015-12-16 05:21:43922 user_prefs::tracked::kTrackedPrefHistogramChanged,
923 BEGIN_ALLOW_SINGLE_BUCKET + 2));
924 EXPECT_EQ(cleared_expected,
925 GetTrackedPrefHistogramCount(
926 user_prefs::tracked::kTrackedPrefHistogramCleared,
927 BEGIN_ALLOW_SINGLE_BUCKET + 2));
erikwrightf4e02b72014-09-17 20:25:45928 EXPECT_EQ(
deepak.m113481c82015-12-16 05:21:43929 protection_level_ > PROTECTION_DISABLED_ON_PLATFORM
930 ? num_tracked_prefs() - changed_expected - cleared_expected
931 : 0,
932 GetTrackedPrefHistogramCount(
933 user_prefs::tracked::kTrackedPrefHistogramUnchanged, ALLOW_ANY));
934
935 EXPECT_EQ(changed_expected,
936 GetTrackedPrefHistogramCount(
937 user_prefs::tracked::kTrackedPrefHistogramWantedReset,
938 BEGIN_ALLOW_SINGLE_BUCKET + 2));
erikwrightf4e02b72014-09-17 20:25:45939 EXPECT_EQ(0,
deepak.m113481c82015-12-16 05:21:43940 GetTrackedPrefHistogramCount(
941 user_prefs::tracked::kTrackedPrefHistogramReset, ALLOW_NONE));
erikwrightf4e02b72014-09-17 20:25:45942
943 // Nothing else should have triggered.
deepak.m113481c82015-12-16 05:21:43944 EXPECT_EQ(0, GetTrackedPrefHistogramCount(
945 user_prefs::tracked::kTrackedPrefHistogramInitialized,
946 ALLOW_NONE));
erikwrightf4e02b72014-09-17 20:25:45947 EXPECT_EQ(0,
948 GetTrackedPrefHistogramCount(
deepak.m113481c82015-12-16 05:21:43949 user_prefs::tracked::kTrackedPrefHistogramTrustedInitialized,
950 ALLOW_NONE));
951 EXPECT_EQ(0, GetTrackedPrefHistogramCount(
952 user_prefs::tracked::kTrackedPrefHistogramNullInitialized,
953 ALLOW_NONE));
erikwrightf4e02b72014-09-17 20:25:45954 EXPECT_EQ(
deepak.m113481c82015-12-16 05:21:43955 0, GetTrackedPrefHistogramCount(
956 user_prefs::tracked::kTrackedPrefHistogramMigratedLegacyDeviceId,
957 ALLOW_NONE));
erikwrightf4e02b72014-09-17 20:25:45958 }
959};
960
961PREF_HASH_BROWSER_TEST(PrefHashBrowserTestUntrustedAdditionToPrefsAfterWipe,
962 UntrustedAdditionToPrefsAfterWipe);