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

Side by Side Diff: chrome/browser/prefs/tracked/pref_hash_browsertest.cc

Issue 431973002: Add integration browser tests for settings hardening. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: tweak for OFFICIAL_BUILDs Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/browser/prefs/chrome_pref_service_factory.cc ('k') | chrome/chrome_tests.gypi » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <string>
6
7 #include "base/command_line.h"
8 #include "base/file_util.h"
9 #include "base/files/file_path.h"
10 #include "base/json/json_file_value_serializer.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/metrics/histogram_base.h"
13 #include "base/metrics/histogram_samples.h"
14 #include "base/metrics/statistics_recorder.h"
15 #include "base/path_service.h"
16 #include "base/prefs/pref_service.h"
17 #include "base/prefs/scoped_user_pref_update.h"
18 #include "base/strings/string_number_conversions.h"
19 #include "base/strings/string_util.h"
20 #include "base/values.h"
21 #include "build/build_config.h"
22 #include "chrome/browser/extensions/extension_browsertest.h"
23 #include "chrome/browser/extensions/extension_service.h"
24 #include "chrome/browser/prefs/chrome_pref_service_factory.h"
25 #include "chrome/browser/prefs/profile_pref_store_manager.h"
26 #include "chrome/browser/prefs/session_startup_pref.h"
27 #include "chrome/browser/profiles/profile.h"
28 #include "chrome/browser/ui/browser.h"
29 #include "chrome/common/chrome_constants.h"
30 #include "chrome/common/chrome_paths.h"
31 #include "chrome/common/pref_names.h"
32 #include "chrome/test/base/testing_profile.h"
33 #include "components/search_engines/default_search_manager.h"
34 #include "content/public/common/content_switches.h"
35 #include "extensions/browser/pref_names.h"
36 #include "extensions/common/extension.h"
37
38 #if defined(OS_CHROMEOS)
39 #include "chromeos/chromeos_switches.h"
40 #endif
41
42 namespace {
43
44 // Extension ID of chrome/test/data/extensions/good.crx
45 const char kGoodCrxId[] = "ldnnhddmnhbkjipkidpdiheffobcpfmf";
46
47 // Explicit expectations from the caller of GetTrackedPrefHistogramCount(). This
48 // enables detailed reporting of the culprit on failure.
49 enum AllowedBuckets {
50 // Allow no samples in any buckets.
51 ALLOW_NONE = -1,
52 // Any integer between BEGIN_ALLOW_SINGLE_BUCKET and END_ALLOW_SINGLE_BUCKET
53 // indicates that only this specific bucket is allowed to have a sample.
54 BEGIN_ALLOW_SINGLE_BUCKET = 0,
55 END_ALLOW_SINGLE_BUCKET = 100,
56 // Allow many buckets (no extra verifications performed).
57 ALLOW_MANY
58 };
59
60 // Returns the number of times |histogram_name| was reported so far; adding the
61 // results of the first 100 buckets (there are only ~19 reporting IDs as of this
62 // writing; varies depending on the platform). |allowed_buckets| hints at extra
63 // requirements verified in this method (see AllowedBuckets for details).
64 int GetTrackedPrefHistogramCount(const char* histogram_name,
65 int allowed_buckets) {
66 const base::HistogramBase* histogram =
67 base::StatisticsRecorder::FindHistogram(histogram_name);
68 if (!histogram)
69 return 0;
70
71 scoped_ptr<base::HistogramSamples> samples(histogram->SnapshotSamples());
72 int sum = 0;
73 for (int i = 0; i < 100; ++i) {
74 int count_for_id = samples->GetCount(i);
75 EXPECT_GE(count_for_id, 0);
76 sum += count_for_id;
77
78 if (allowed_buckets == ALLOW_NONE ||
79 (allowed_buckets != ALLOW_MANY && i != allowed_buckets)) {
80 EXPECT_EQ(0, count_for_id) << "Unexpected reporting_id: " << i;
81 }
82 }
83 return sum;
84 }
85
86 scoped_ptr<base::DictionaryValue> ReadPrefsDictionary(
87 const base::FilePath& pref_file) {
88 JSONFileValueSerializer serializer(pref_file);
89 int error_code = JSONFileValueSerializer::JSON_NO_ERROR;
90 std::string error_str;
91 scoped_ptr<base::Value> prefs(
92 serializer.Deserialize(&error_code, &error_str));
93 if (!prefs || error_code != JSONFileValueSerializer::JSON_NO_ERROR) {
94 ADD_FAILURE() << "Error #" << error_code << ": " << error_str;
95 return scoped_ptr<base::DictionaryValue>();
96 }
97 if (!prefs->IsType(base::Value::TYPE_DICTIONARY)) {
98 ADD_FAILURE();
99 return scoped_ptr<base::DictionaryValue>();
100 }
101 return scoped_ptr<base::DictionaryValue>(
102 static_cast<base::DictionaryValue*>(prefs.release()));
103 }
104
105 #define PREF_HASH_BROWSER_TEST(fixture, test_name) \
106 IN_PROC_BROWSER_TEST_P(fixture, PRE_##test_name) { \
107 SetupPreferences(); \
108 } \
109 IN_PROC_BROWSER_TEST_P(fixture, test_name) { \
110 VerifyReactionToPrefAttack(); \
111 } \
112 INSTANTIATE_TEST_CASE_P( \
113 fixture##Instance, \
114 fixture, \
115 testing::Values( \
116 chrome_prefs::internals::kSettingsEnforcementGroupNoEnforcement, \
117 chrome_prefs::internals::kSettingsEnforcementGroupEnforceAlways, \
118 chrome_prefs::internals:: \
119 kSettingsEnforcementGroupEnforceAlwaysWithDSE, \
120 chrome_prefs::internals:: \
121 kSettingsEnforcementGroupEnforceAlwaysWithExtensionsAndDSE));
122
123 // A base fixture designed such that implementations do two things:
124 // 1) Override all three pure-virtual methods below to setup, attack, and
125 // verify preferenes throughout the tests provided by this fixture.
126 // 2) Instantiate their test via the PREF_HASH_BROWSER_TEST macro above.
127 // Based on top of ExtensionBrowserTest to allow easy interaction with the
128 // ExtensionService.
129 class PrefHashBrowserTestBase
130 : public ExtensionBrowserTest,
131 public testing::WithParamInterface<std::string> {
132 public:
133 // List of potential protection levels for this test in strict increasing
134 // order of protection levels.
135 enum SettingsProtectionLevel {
136 PROTECTION_DISABLED_ON_PLATFORM,
137 PROTECTION_DISABLED_FOR_GROUP,
138 PROTECTION_ENABLED_BASIC,
139 PROTECTION_ENABLED_DSE,
140 PROTECTION_ENABLED_EXTENSIONS,
141 PROTECTION_ENABLED_ALL,
142 };
143
144 PrefHashBrowserTestBase()
145 : protection_level_(PROTECTION_DISABLED_ON_PLATFORM) {
146 if (ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking) {
147 const std::string& experiment_group = GetParam();
148 if (experiment_group ==
149 chrome_prefs::internals::kSettingsEnforcementGroupNoEnforcement) {
150 protection_level_ = PROTECTION_DISABLED_FOR_GROUP;
151 } else if (experiment_group ==
152 chrome_prefs::internals::
153 kSettingsEnforcementGroupEnforceAlways) {
154 protection_level_ = PROTECTION_ENABLED_BASIC;
155 } else if (experiment_group ==
156 chrome_prefs::internals::
157 kSettingsEnforcementGroupEnforceAlwaysWithDSE) {
158 protection_level_ = PROTECTION_ENABLED_DSE;
159 } else if (experiment_group ==
160 chrome_prefs::internals::
161 kSettingsEnforcementGroupEnforceAlwaysWithExtensionsAndDSE) {
Bernhard Bauer 2014/08/01 14:57:46 This doesn't fit into 80 columns anymore... Some t
gab 2014/08/01 17:52:21 Done.
162 protection_level_ = PROTECTION_ENABLED_EXTENSIONS;
163 } else {
164 ADD_FAILURE();
165 }
166
167 // Protection levels can't be adjusted via --force-fieldtrials in official
Bernhard Bauer 2014/08/01 14:57:47 Indent is off
gab 2014/08/01 17:52:21 I think comments connected to ifdef's go at the sa
168 // builds.
169 #if defined(OFFICIAL_BUILD)
170 #if defined(OS_WIN)
171 // The strongest mode is enforced on Windows in the absence of a field
172 // trial.
173 protection_level_ = PROTECTION_ENABLED_ALL;
174 #else
175 protection_level_ = PROTECTION_DISABLED_FOR_GROUP;
176 #endif
177 #endif
178 }
179 }
180
181 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
182 ExtensionBrowserTest::SetUpCommandLine(command_line);
183 EXPECT_FALSE(command_line->HasSwitch(switches::kForceFieldTrials));
184 command_line->AppendSwitchASCII(
185 switches::kForceFieldTrials,
186 std::string(chrome_prefs::internals::kSettingsEnforcementTrialName) +
187 "/" + GetParam() + "/");
188 #if defined(OS_CHROMEOS)
189 command_line->AppendSwitch(
190 chromeos::switches::kIgnoreUserProfileMappingForTests);
191 #endif
192 }
193
194 virtual bool SetUpUserDataDirectory() OVERRIDE {
195 // Do the normal setup in the PRE test and attack preferences in the main
196 // test.
197 if (IsPRETest())
198 return ExtensionBrowserTest::SetUpUserDataDirectory();
199
200 base::FilePath profile_dir;
201 EXPECT_TRUE(PathService::Get(chrome::DIR_USER_DATA, &profile_dir));
202 profile_dir = profile_dir.AppendASCII(TestingProfile::kTestUserProfileDir);
203
204 // Sanity check that old protected pref file is never present in modern
205 // Chromes.
206 EXPECT_FALSE(base::PathExists(
207 profile_dir.Append(chrome::kProtectedPreferencesFilenameDeprecated)));
208
209 // Read the preferences from disk.
210
211 const base::FilePath unprotected_pref_file =
212 profile_dir.Append(chrome::kPreferencesFilename);
213 EXPECT_TRUE(base::PathExists(unprotected_pref_file));
214
215 const base::FilePath protected_pref_file =
216 profile_dir.Append(chrome::kSecurePreferencesFilename);
217 EXPECT_EQ(protection_level_ > PROTECTION_DISABLED_ON_PLATFORM,
218 base::PathExists(protected_pref_file));
219
220 scoped_ptr<base::DictionaryValue> unprotected_preferences(
221 ReadPrefsDictionary(unprotected_pref_file));
222 if (!unprotected_preferences)
223 return false;
224
225 scoped_ptr<base::DictionaryValue> protected_preferences;
226 if (protection_level_ > PROTECTION_DISABLED_ON_PLATFORM) {
227 protected_preferences = ReadPrefsDictionary(protected_pref_file);
228 if (!protected_preferences)
229 return false;
230 }
231
232 // Let the underlying test modify the preferences.
233 AttackPreferencesOnDisk(unprotected_preferences.get(),
234 protected_preferences.get());
235
236 // Write the modified preferences back to disk.
237
238 JSONFileValueSerializer unprotected_prefs_serializer(unprotected_pref_file);
239 EXPECT_TRUE(
240 unprotected_prefs_serializer.Serialize(*unprotected_preferences));
241
242 if (protected_preferences) {
243 JSONFileValueSerializer protected_prefs_serializer(protected_pref_file);
244 EXPECT_TRUE(protected_prefs_serializer.Serialize(*protected_preferences));
245 }
246
247 return true;
248 }
249
250 // In the PRE_ test, find the number of tracked preferences that were
251 // initialized and save it to a file to be read back in the main test and used
252 // as the total number of tracked preferences.
253 virtual void SetUpOnMainThread() OVERRIDE {
254 ExtensionBrowserTest::SetUpOnMainThread();
255
256 // File in which the PRE_ test will save the number of tracked preferences
257 // on this platform.
258 const char kNumTrackedPrefFilename[] = "NumTrackedPrefs";
259
260 base::FilePath num_tracked_prefs_file;
261 ASSERT_TRUE(
262 PathService::Get(chrome::DIR_USER_DATA, &num_tracked_prefs_file));
263 num_tracked_prefs_file =
264 num_tracked_prefs_file.AppendASCII(kNumTrackedPrefFilename);
265
266 if (IsPRETest()) {
267 num_tracked_prefs_ = GetTrackedPrefHistogramCount(
268 "Settings.TrackedPreferenceTrustedInitialized", ALLOW_MANY);
269 EXPECT_EQ(protection_level_ > PROTECTION_DISABLED_ON_PLATFORM,
270 num_tracked_prefs_ > 0);
271
272 // Split tracked prefs are reported as Unchanged not as TrustedInitialized
273 // when an empty dictionary is encountered on first run (this should only
274 // hit for pref #5 in the current design).
275 int num_split_tracked_prefs = GetTrackedPrefHistogramCount(
276 "Settings.TrackedPreferenceUnchanged", BEGIN_ALLOW_SINGLE_BUCKET + 5);
277 EXPECT_EQ(protection_level_ > PROTECTION_DISABLED_ON_PLATFORM ? 1 : 0,
278 num_split_tracked_prefs);
279
280 num_tracked_prefs_ += num_split_tracked_prefs;
281
282 std::string num_tracked_prefs_str = base::IntToString(num_tracked_prefs_);
283 EXPECT_EQ(static_cast<int>(num_tracked_prefs_str.size()),
284 base::WriteFile(num_tracked_prefs_file,
285 num_tracked_prefs_str.c_str(),
286 num_tracked_prefs_str.size()));
287 } else {
288 std::string num_tracked_prefs_str;
289 EXPECT_TRUE(base::ReadFileToString(num_tracked_prefs_file,
290 &num_tracked_prefs_str));
291 EXPECT_TRUE(
292 base::StringToInt(num_tracked_prefs_str, &num_tracked_prefs_));
293 }
294 }
295
296 protected:
297 // Called from the PRE_ test's body. Overrides should use it to setup
298 // preferences through Chrome.
299 virtual void SetupPreferences() = 0;
300
301 // Called prior to the main test launching its browser. Overrides should use
302 // it to attack preferences. |(un)protected_preferences| represent the state
303 // on disk prior to launching the main test, they can be modified by this
304 // method and modifications will be flushed back to disk before launching the
305 // main test. |unprotected_preferences| is never NULL, |protected_preferences|
306 // may be NULL if in PROTECTION_DISABLED_ON_PLATFORM mode.
307 virtual void AttackPreferencesOnDisk(
308 base::DictionaryValue* unprotected_preferences,
309 base::DictionaryValue* protected_preferences) = 0;
310
311 // Called from the body of the main test. Overrides should use it to verify
312 // that the browser had the desired reaction when faced when the attack
313 // orchestrated in AttackPreferencesOnDisk().
314 virtual void VerifyReactionToPrefAttack() = 0;
315
316 int num_tracked_prefs_;
317
318 SettingsProtectionLevel protection_level_;
319
320 private:
321 // Returns true if this is the PRE_ phase of the test.
322 bool IsPRETest() {
323 return StartsWithASCII(
324 testing::UnitTest::GetInstance()->current_test_info()->name(),
325 "PRE_",
326 true /* case_sensitive */);
327 }
328 };
329
330 } // namespace
331
332 // Verifies that nothing is reset when nothing is tampered with.
333 // Also sanity checks that the expected preferences files are in place.
334 class PrefHashBrowserTestUnchangedDefault : public PrefHashBrowserTestBase {
335 public:
336 virtual void SetupPreferences() OVERRIDE {
337 // Default Chrome setup.
338 }
339
340 virtual void AttackPreferencesOnDisk(
341 base::DictionaryValue* unprotected_preferences,
342 base::DictionaryValue* protected_preferences) OVERRIDE {
343 // No attack.
344 }
345
346 virtual void VerifyReactionToPrefAttack() OVERRIDE {
347 // Expect all prefs to be reported as Unchanged with no resets.
348 EXPECT_EQ(protection_level_ > PROTECTION_DISABLED_ON_PLATFORM
349 ? num_tracked_prefs_ : 0,
350 GetTrackedPrefHistogramCount(
351 "Settings.TrackedPreferenceUnchanged", ALLOW_MANY));
352 EXPECT_EQ(0,
353 GetTrackedPrefHistogramCount(
354 "Settings.TrackedPreferenceWantedReset", ALLOW_NONE));
355 EXPECT_EQ(0,
356 GetTrackedPrefHistogramCount("Settings.TrackedPreferenceReset",
357 ALLOW_NONE));
358
359 // Nothing else should have triggered.
360 EXPECT_EQ(0,
361 GetTrackedPrefHistogramCount("Settings.TrackedPreferenceChanged",
362 ALLOW_NONE));
363 EXPECT_EQ(0,
364 GetTrackedPrefHistogramCount("Settings.TrackedPreferenceCleared",
365 ALLOW_NONE));
366 EXPECT_EQ(0,
367 GetTrackedPrefHistogramCount(
368 "Settings.TrackedPreferenceInitialized", ALLOW_NONE));
369 EXPECT_EQ(0,
370 GetTrackedPrefHistogramCount(
371 "Settings.TrackedPreferenceTrustedInitialized", ALLOW_NONE));
372 EXPECT_EQ(
373 0,
374 GetTrackedPrefHistogramCount(
375 "Settings.TrackedPreferenceMigratedLegacyDeviceId", ALLOW_NONE));
376 }
377 };
378
379 PREF_HASH_BROWSER_TEST(PrefHashBrowserTestUnchangedDefault, UnchangedDefault);
380
381 // Augments PrefHashBrowserTestUnchangedDefault to confirm that nothing is reset
382 // when nothing is tampered with, even if Chrome itself wrote custom prefs in
383 // its last run.
384 class PrefHashBrowserTestUnchangedCustom
385 : public PrefHashBrowserTestUnchangedDefault {
386 public:
387 virtual void SetupPreferences() OVERRIDE {
388 profile()->GetPrefs()->SetString(prefs::kHomePage, "http://example.com");
389
390 InstallExtensionWithUIAutoConfirm(
391 test_data_dir_.AppendASCII("good.crx"), 1, browser());
392 }
393
394 virtual void VerifyReactionToPrefAttack() OVERRIDE {
395 // Make sure the settings written in the last run stuck.
396 EXPECT_EQ("http://example.com",
397 profile()->GetPrefs()->GetString(prefs::kHomePage));
398
399 EXPECT_TRUE(extension_service()->GetExtensionById(kGoodCrxId, false));
400
401 // Reaction should be identical to unattacked default prefs.
402 PrefHashBrowserTestUnchangedDefault::VerifyReactionToPrefAttack();
403 }
404 };
405
406 PREF_HASH_BROWSER_TEST(PrefHashBrowserTestUnchangedCustom, UnchangedCustom);
407
408 // Verifies that cleared prefs are reported.
409 class PrefHashBrowserTestClearedAtomic : public PrefHashBrowserTestBase {
410 public:
411 virtual void SetupPreferences() OVERRIDE {
412 profile()->GetPrefs()->SetString(prefs::kHomePage, "http://example.com");
413 }
414
415 virtual void AttackPreferencesOnDisk(
416 base::DictionaryValue* unprotected_preferences,
417 base::DictionaryValue* protected_preferences) OVERRIDE {
418 base::DictionaryValue* selected_prefs =
419 protection_level_ >= PROTECTION_ENABLED_BASIC ? protected_preferences
420 : unprotected_preferences;
421 // |selected_prefs| should never be NULL under the protection level picking
422 // it.
423 EXPECT_TRUE(selected_prefs);
424 EXPECT_TRUE(selected_prefs->Remove(prefs::kHomePage, NULL));
425 }
426
427 virtual void VerifyReactionToPrefAttack() OVERRIDE {
428 // The clearance of homepage should have been noticed (as pref #2 being
429 // cleared), but shouldn't have triggered a reset (as there is nothing we
430 // can do when the pref is already gone).
431 EXPECT_EQ(protection_level_ > PROTECTION_DISABLED_ON_PLATFORM ? 1 : 0,
432 GetTrackedPrefHistogramCount("Settings.TrackedPreferenceCleared",
433 BEGIN_ALLOW_SINGLE_BUCKET + 2));
434 EXPECT_EQ(protection_level_ > PROTECTION_DISABLED_ON_PLATFORM
435 ? num_tracked_prefs_ - 1 : 0,
436 GetTrackedPrefHistogramCount(
437 "Settings.TrackedPreferenceUnchanged", ALLOW_MANY));
438 EXPECT_EQ(0,
439 GetTrackedPrefHistogramCount(
440 "Settings.TrackedPreferenceWantedReset", ALLOW_NONE));
441 EXPECT_EQ(0,
442 GetTrackedPrefHistogramCount("Settings.TrackedPreferenceReset",
443 ALLOW_NONE));
444
445 // Nothing else should have triggered.
446 EXPECT_EQ(0,
447 GetTrackedPrefHistogramCount("Settings.TrackedPreferenceChanged",
448 ALLOW_NONE));
449 EXPECT_EQ(0,
450 GetTrackedPrefHistogramCount(
451 "Settings.TrackedPreferenceInitialized", ALLOW_NONE));
452 EXPECT_EQ(0,
453 GetTrackedPrefHistogramCount(
454 "Settings.TrackedPreferenceTrustedInitialized", ALLOW_NONE));
455 EXPECT_EQ(
456 0,
457 GetTrackedPrefHistogramCount(
458 "Settings.TrackedPreferenceMigratedLegacyDeviceId", ALLOW_NONE));
459 }
460 };
461
462 PREF_HASH_BROWSER_TEST(PrefHashBrowserTestClearedAtomic, ClearedAtomic);
463
464 // Verifies that clearing the MACs results in untrusted Initialized pings for
465 // non-null protected prefs.
466 class PrefHashBrowserTestUntrustedInitialized : public PrefHashBrowserTestBase {
467 public:
468 virtual void SetupPreferences() OVERRIDE {
469 // Explicitly set the DSE (it's otherwise NULL by default, preventing
470 // thorough testing of the PROTECTION_ENABLED_DSE level).
471 DefaultSearchManager default_search_manager(
472 profile()->GetPrefs(), DefaultSearchManager::ObserverCallback());
473 DefaultSearchManager::Source dse_source =
474 static_cast<DefaultSearchManager::Source>(-1);
475
476 const TemplateURLData* default_template_url_data =
477 default_search_manager.GetDefaultSearchEngine(&dse_source);
478 EXPECT_EQ(DefaultSearchManager::FROM_FALLBACK, dse_source);
479
480 default_search_manager.SetUserSelectedDefaultSearchEngine(
481 *default_template_url_data);
482
483 default_search_manager.GetDefaultSearchEngine(&dse_source);
484 EXPECT_EQ(DefaultSearchManager::FROM_USER, dse_source);
485
486 // Also explicitly set an atomic pref that falls under
487 // PROTECTION_ENABLED_BASIC.
488 profile()->GetPrefs()->SetInteger(prefs::kRestoreOnStartup,
489 SessionStartupPref::URLS);
490 }
491
492 virtual void AttackPreferencesOnDisk(
493 base::DictionaryValue* unprotected_preferences,
494 base::DictionaryValue* protected_preferences) OVERRIDE {
495 EXPECT_TRUE(unprotected_preferences->Remove("protection.macs", NULL));
496 if (protected_preferences)
497 EXPECT_TRUE(protected_preferences->Remove("protection.macs", NULL));
498 }
499
500 virtual void VerifyReactionToPrefAttack() OVERRIDE {
501 // Preferences that are NULL by default will be TrustedInitialized.
502 int num_null_values = GetTrackedPrefHistogramCount(
503 "Settings.TrackedPreferenceTrustedInitialized", ALLOW_MANY);
504 EXPECT_EQ(protection_level_ > PROTECTION_DISABLED_ON_PLATFORM,
505 num_null_values > 0);
506 if (num_null_values > 0) {
507 // This test requires that at least 3 prefs be non-null (extensions, DSE,
508 // and 1 atomic pref explictly set for this test above).
509 EXPECT_LT(num_null_values, num_tracked_prefs_ - 3);
510 }
511
512 // Expect all non-null prefs to be reported as Initialized (with
513 // accompanying resets or wanted resets based on the current protection
514 // level).
515 EXPECT_EQ(num_tracked_prefs_ - num_null_values,
516 GetTrackedPrefHistogramCount(
517 "Settings.TrackedPreferenceInitialized", ALLOW_MANY));
518
519 int num_protected_prefs = 0;
520 // A switch statement falling through each protection level in decreasing
521 // levels of protection to add expectations for each level which augments
522 // the previous one.
523 switch (protection_level_) {
524 case PROTECTION_ENABLED_EXTENSIONS:
525 ++num_protected_prefs;
526 // Falls through.
527 case PROTECTION_ENABLED_DSE:
528 ++num_protected_prefs;
529 // Falls through.
530 case PROTECTION_ENABLED_BASIC:
531 num_protected_prefs += num_tracked_prefs_ - num_null_values - 2;
532 // Falls through.
533 case PROTECTION_DISABLED_FOR_GROUP:
534 // No protection. Falls through.
535 case PROTECTION_DISABLED_ON_PLATFORM:
536 // No protection.
537 break;
538 }
539
540 EXPECT_EQ(num_tracked_prefs_ - num_null_values - num_protected_prefs,
541 GetTrackedPrefHistogramCount(
542 "Settings.TrackedPreferenceWantedReset", ALLOW_MANY));
543 EXPECT_EQ(num_protected_prefs,
544 GetTrackedPrefHistogramCount("Settings.TrackedPreferenceReset",
545 ALLOW_MANY));
546
547 // Explicitly verify the result of reported resets.
548
549 DefaultSearchManager default_search_manager(
550 profile()->GetPrefs(), DefaultSearchManager::ObserverCallback());
551 DefaultSearchManager::Source dse_source =
552 static_cast<DefaultSearchManager::Source>(-1);
553 default_search_manager.GetDefaultSearchEngine(&dse_source);
554 EXPECT_EQ(protection_level_ < PROTECTION_ENABLED_DSE
555 ? DefaultSearchManager::FROM_USER
556 : DefaultSearchManager::FROM_FALLBACK,
557 dse_source);
558
559 EXPECT_EQ(protection_level_ < PROTECTION_ENABLED_BASIC,
560 profile()->GetPrefs()->GetInteger(prefs::kRestoreOnStartup) ==
561 SessionStartupPref::URLS);
562
563 // Nothing else should have triggered.
564 EXPECT_EQ(0,
565 GetTrackedPrefHistogramCount(
566 "Settings.TrackedPreferenceUnchanged", ALLOW_NONE));
567 EXPECT_EQ(0,
568 GetTrackedPrefHistogramCount("Settings.TrackedPreferenceChanged",
569 ALLOW_NONE));
570 EXPECT_EQ(0,
571 GetTrackedPrefHistogramCount("Settings.TrackedPreferenceCleared",
572 ALLOW_NONE));
573 EXPECT_EQ(
574 0,
575 GetTrackedPrefHistogramCount(
576 "Settings.TrackedPreferenceMigratedLegacyDeviceId", ALLOW_NONE));
577 }
578 };
579
580 PREF_HASH_BROWSER_TEST(PrefHashBrowserTestUntrustedInitialized,
581 UntrustedInitialized);
582
583 // Verifies that changing an atomic pref results in it being reported (and reset
584 // if the protection level allows it).
585 class PrefHashBrowserTestChangedAtomic : public PrefHashBrowserTestBase {
586 public:
587 virtual void SetupPreferences() OVERRIDE {
588 profile()->GetPrefs()->SetInteger(prefs::kRestoreOnStartup,
589 SessionStartupPref::URLS);
590
591 ListPrefUpdate update(profile()->GetPrefs(),
592 prefs::kURLsToRestoreOnStartup);
593 update->AppendString("http://example.com");
594 }
595
596 virtual void AttackPreferencesOnDisk(
597 base::DictionaryValue* unprotected_preferences,
598 base::DictionaryValue* protected_preferences) OVERRIDE {
599 base::DictionaryValue* selected_prefs =
600 protection_level_ >= PROTECTION_ENABLED_BASIC ? protected_preferences
601 : unprotected_preferences;
602 // |selected_prefs| should never be NULL under the protection level picking
603 // it.
604 EXPECT_TRUE(selected_prefs);
605 base::ListValue* startup_urls;
606 EXPECT_TRUE(
607 selected_prefs->GetList(prefs::kURLsToRestoreOnStartup, &startup_urls));
608 EXPECT_TRUE(startup_urls);
609 EXPECT_EQ(1U, startup_urls->GetSize());
610 startup_urls->AppendString("http://example.org");
611 }
612
613 virtual void VerifyReactionToPrefAttack() OVERRIDE {
614 // Expect a single Changed event for tracked pref #4 (startup URLs).
615 EXPECT_EQ(protection_level_ > PROTECTION_DISABLED_ON_PLATFORM ? 1 : 0,
616 GetTrackedPrefHistogramCount("Settings.TrackedPreferenceChanged",
617 BEGIN_ALLOW_SINGLE_BUCKET + 4));
618 EXPECT_EQ(protection_level_ > PROTECTION_DISABLED_ON_PLATFORM
619 ? num_tracked_prefs_ - 1 : 0,
620 GetTrackedPrefHistogramCount(
621 "Settings.TrackedPreferenceUnchanged", ALLOW_MANY));
622
623 EXPECT_EQ(
624 (protection_level_ > PROTECTION_DISABLED_ON_PLATFORM &&
625 protection_level_ < PROTECTION_ENABLED_BASIC) ? 1 : 0,
626 GetTrackedPrefHistogramCount("Settings.TrackedPreferenceWantedReset",
627 BEGIN_ALLOW_SINGLE_BUCKET + 4));
628 EXPECT_EQ(protection_level_ >= PROTECTION_ENABLED_BASIC ? 1 : 0,
629 GetTrackedPrefHistogramCount("Settings.TrackedPreferenceReset",
630 BEGIN_ALLOW_SINGLE_BUCKET + 4));
631
632 // Explicitly verify the result of reported resets.
633 EXPECT_EQ(protection_level_ >= PROTECTION_ENABLED_BASIC ? 0U : 2U,
634 profile()
635 ->GetPrefs()
636 ->GetList(prefs::kURLsToRestoreOnStartup)
637 ->GetSize());
638
639 // Nothing else should have triggered.
640 EXPECT_EQ(0,
641 GetTrackedPrefHistogramCount("Settings.TrackedPreferenceCleared",
642 ALLOW_NONE));
643 EXPECT_EQ(0,
644 GetTrackedPrefHistogramCount(
645 "Settings.TrackedPreferenceInitialized", ALLOW_NONE));
646 EXPECT_EQ(0,
647 GetTrackedPrefHistogramCount(
648 "Settings.TrackedPreferenceTrustedInitialized", ALLOW_NONE));
649 EXPECT_EQ(
650 0,
651 GetTrackedPrefHistogramCount(
652 "Settings.TrackedPreferenceMigratedLegacyDeviceId", ALLOW_NONE));
653 }
654 };
655
656 PREF_HASH_BROWSER_TEST(PrefHashBrowserTestChangedAtomic, ChangedAtomic);
657
658 // Verifies that changing or adding an entry in a split pref results in both
659 // items being reported (and remove if the protection level allows it).
660 class PrefHashBrowserTestChangedSplitPref : public PrefHashBrowserTestBase {
661 public:
662 virtual void SetupPreferences() OVERRIDE {
663 InstallExtensionWithUIAutoConfirm(
664 test_data_dir_.AppendASCII("good.crx"), 1, browser());
665 }
666
667 virtual void AttackPreferencesOnDisk(
668 base::DictionaryValue* unprotected_preferences,
669 base::DictionaryValue* protected_preferences) OVERRIDE {
670 base::DictionaryValue* selected_prefs =
671 protection_level_ >= PROTECTION_ENABLED_EXTENSIONS
672 ? protected_preferences
673 : unprotected_preferences;
674 // |selected_prefs| should never be NULL under the protection level picking
675 // it.
676 EXPECT_TRUE(selected_prefs);
677 base::DictionaryValue* extensions_dict;
678 EXPECT_TRUE(selected_prefs->GetDictionary(
679 extensions::pref_names::kExtensions, &extensions_dict));
680 EXPECT_TRUE(extensions_dict);
681
682 // Tamper with any installed setting for good.crx
683 base::DictionaryValue* good_crx_dict;
684 EXPECT_TRUE(extensions_dict->GetDictionary(kGoodCrxId, &good_crx_dict));
685 int good_crx_state;
686 EXPECT_TRUE(good_crx_dict->GetInteger("state", &good_crx_state));
687 EXPECT_EQ(extensions::Extension::ENABLED, good_crx_state);
688 good_crx_dict->SetInteger("state", extensions::Extension::DISABLED);
689
690 // Drop a fake extension (for the purpose of this test, dropped settings
691 // don't need to be valid extension settings).
692 base::DictionaryValue* fake_extension = new base::DictionaryValue;
693 fake_extension->SetString("name", "foo");
694 extensions_dict->Set(std::string(32, 'a'), fake_extension);
695 }
696
697 virtual void VerifyReactionToPrefAttack() OVERRIDE {
698 // Expect a single split pref changed report with a count of 2 for tracked
699 // pref #5 (extensions).
700 EXPECT_EQ(protection_level_ > PROTECTION_DISABLED_ON_PLATFORM ? 1 : 0,
701 GetTrackedPrefHistogramCount("Settings.TrackedPreferenceChanged",
702 BEGIN_ALLOW_SINGLE_BUCKET + 5));
703 EXPECT_EQ(protection_level_ > PROTECTION_DISABLED_ON_PLATFORM ? 1 : 0,
704 GetTrackedPrefHistogramCount(
705 "Settings.TrackedSplitPreferenceChanged.extensions.settings",
706 BEGIN_ALLOW_SINGLE_BUCKET + 2));
707
708 // Everything else should have remained unchanged.
709 EXPECT_EQ(protection_level_ > PROTECTION_DISABLED_ON_PLATFORM
710 ? num_tracked_prefs_ - 1 : 0,
711 GetTrackedPrefHistogramCount(
712 "Settings.TrackedPreferenceUnchanged", ALLOW_MANY));
713
714 EXPECT_EQ(
715 (protection_level_ > PROTECTION_DISABLED_ON_PLATFORM &&
716 protection_level_ < PROTECTION_ENABLED_EXTENSIONS) ? 1 : 0,
717 GetTrackedPrefHistogramCount("Settings.TrackedPreferenceWantedReset",
718 BEGIN_ALLOW_SINGLE_BUCKET + 5));
719 EXPECT_EQ(protection_level_ >= PROTECTION_ENABLED_EXTENSIONS ? 1 : 0,
720 GetTrackedPrefHistogramCount("Settings.TrackedPreferenceReset",
721 BEGIN_ALLOW_SINGLE_BUCKET + 5));
722
723 EXPECT_EQ(protection_level_ < PROTECTION_ENABLED_EXTENSIONS,
724 extension_service()->GetExtensionById(kGoodCrxId, true) != NULL);
725
726 // Nothing else should have triggered.
727 EXPECT_EQ(0,
728 GetTrackedPrefHistogramCount("Settings.TrackedPreferenceCleared",
729 ALLOW_NONE));
730 EXPECT_EQ(0,
731 GetTrackedPrefHistogramCount(
732 "Settings.TrackedPreferenceInitialized", ALLOW_NONE));
733 EXPECT_EQ(0,
734 GetTrackedPrefHistogramCount(
735 "Settings.TrackedPreferenceTrustedInitialized", ALLOW_NONE));
736 EXPECT_EQ(
737 0,
738 GetTrackedPrefHistogramCount(
739 "Settings.TrackedPreferenceMigratedLegacyDeviceId", ALLOW_NONE));
740 }
741 };
742
743 PREF_HASH_BROWSER_TEST(PrefHashBrowserTestChangedSplitPref, ChangedSplitPref);
OLDNEW
« no previous file with comments | « chrome/browser/prefs/chrome_pref_service_factory.cc ('k') | chrome/chrome_tests.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698