Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 | |
|
erikwright (departed)
2014/08/02 07:16:52
MANY -> ANY?
gab
2014/08/04 18:53:10
Done.
| |
| 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 // Represents the strongest level (i.e. always equivalent to the last one in | |
| 142 // terms of protection), leave this one last when adding new levels. | |
| 143 PROTECTION_ENABLED_ALL | |
| 144 }; | |
| 145 | |
| 146 PrefHashBrowserTestBase() | |
| 147 : protection_level_(GetProtectionLevelFromTrialGroup(GetParam())) { | |
| 148 } | |
| 149 | |
| 150 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { | |
| 151 ExtensionBrowserTest::SetUpCommandLine(command_line); | |
| 152 EXPECT_FALSE(command_line->HasSwitch(switches::kForceFieldTrials)); | |
| 153 command_line->AppendSwitchASCII( | |
| 154 switches::kForceFieldTrials, | |
| 155 std::string(chrome_prefs::internals::kSettingsEnforcementTrialName) + | |
| 156 "/" + GetParam() + "/"); | |
| 157 #if defined(OS_CHROMEOS) | |
| 158 command_line->AppendSwitch( | |
| 159 chromeos::switches::kIgnoreUserProfileMappingForTests); | |
| 160 #endif | |
| 161 } | |
| 162 | |
| 163 virtual bool SetUpUserDataDirectory() OVERRIDE { | |
| 164 // Do the normal setup in the PRE test and attack preferences in the main | |
| 165 // test. | |
| 166 if (IsPRETest()) | |
| 167 return ExtensionBrowserTest::SetUpUserDataDirectory(); | |
| 168 | |
| 169 base::FilePath profile_dir; | |
| 170 EXPECT_TRUE(PathService::Get(chrome::DIR_USER_DATA, &profile_dir)); | |
| 171 profile_dir = profile_dir.AppendASCII(TestingProfile::kTestUserProfileDir); | |
| 172 | |
| 173 // Sanity check that old protected pref file is never present in modern | |
| 174 // Chromes. | |
| 175 EXPECT_FALSE(base::PathExists( | |
| 176 profile_dir.Append(chrome::kProtectedPreferencesFilenameDeprecated))); | |
| 177 | |
| 178 // Read the preferences from disk. | |
| 179 | |
| 180 const base::FilePath unprotected_pref_file = | |
| 181 profile_dir.Append(chrome::kPreferencesFilename); | |
| 182 EXPECT_TRUE(base::PathExists(unprotected_pref_file)); | |
| 183 | |
| 184 const base::FilePath protected_pref_file = | |
| 185 profile_dir.Append(chrome::kSecurePreferencesFilename); | |
| 186 EXPECT_EQ(protection_level_ > PROTECTION_DISABLED_ON_PLATFORM, | |
| 187 base::PathExists(protected_pref_file)); | |
| 188 | |
| 189 scoped_ptr<base::DictionaryValue> unprotected_preferences( | |
| 190 ReadPrefsDictionary(unprotected_pref_file)); | |
| 191 if (!unprotected_preferences) | |
| 192 return false; | |
| 193 | |
| 194 scoped_ptr<base::DictionaryValue> protected_preferences; | |
| 195 if (protection_level_ > PROTECTION_DISABLED_ON_PLATFORM) { | |
| 196 protected_preferences = ReadPrefsDictionary(protected_pref_file); | |
| 197 if (!protected_preferences) | |
| 198 return false; | |
| 199 } | |
| 200 | |
| 201 // Let the underlying test modify the preferences. | |
| 202 AttackPreferencesOnDisk(unprotected_preferences.get(), | |
| 203 protected_preferences.get()); | |
| 204 | |
| 205 // Write the modified preferences back to disk. | |
| 206 | |
| 207 JSONFileValueSerializer unprotected_prefs_serializer(unprotected_pref_file); | |
| 208 EXPECT_TRUE( | |
| 209 unprotected_prefs_serializer.Serialize(*unprotected_preferences)); | |
| 210 | |
| 211 if (protected_preferences) { | |
| 212 JSONFileValueSerializer protected_prefs_serializer(protected_pref_file); | |
| 213 EXPECT_TRUE(protected_prefs_serializer.Serialize(*protected_preferences)); | |
| 214 } | |
| 215 | |
| 216 return true; | |
| 217 } | |
| 218 | |
| 219 // In the PRE_ test, find the number of tracked preferences that were | |
| 220 // initialized and save it to a file to be read back in the main test and used | |
| 221 // as the total number of tracked preferences. | |
| 222 virtual void SetUpOnMainThread() OVERRIDE { | |
| 223 ExtensionBrowserTest::SetUpOnMainThread(); | |
| 224 | |
| 225 // File in which the PRE_ test will save the number of tracked preferences | |
| 226 // on this platform. | |
| 227 const char kNumTrackedPrefFilename[] = "NumTrackedPrefs"; | |
| 228 | |
| 229 base::FilePath num_tracked_prefs_file; | |
| 230 ASSERT_TRUE( | |
| 231 PathService::Get(chrome::DIR_USER_DATA, &num_tracked_prefs_file)); | |
| 232 num_tracked_prefs_file = | |
| 233 num_tracked_prefs_file.AppendASCII(kNumTrackedPrefFilename); | |
| 234 | |
| 235 if (IsPRETest()) { | |
| 236 num_tracked_prefs_ = GetTrackedPrefHistogramCount( | |
| 237 "Settings.TrackedPreferenceTrustedInitialized", ALLOW_MANY); | |
| 238 EXPECT_EQ(protection_level_ > PROTECTION_DISABLED_ON_PLATFORM, | |
| 239 num_tracked_prefs_ > 0); | |
| 240 | |
| 241 // Split tracked prefs are reported as Unchanged not as TrustedInitialized | |
| 242 // when an empty dictionary is encountered on first run (this should only | |
| 243 // hit for pref #5 in the current design). | |
| 244 int num_split_tracked_prefs = GetTrackedPrefHistogramCount( | |
| 245 "Settings.TrackedPreferenceUnchanged", BEGIN_ALLOW_SINGLE_BUCKET + 5); | |
| 246 EXPECT_EQ(protection_level_ > PROTECTION_DISABLED_ON_PLATFORM ? 1 : 0, | |
| 247 num_split_tracked_prefs); | |
| 248 | |
| 249 num_tracked_prefs_ += num_split_tracked_prefs; | |
| 250 | |
| 251 std::string num_tracked_prefs_str = base::IntToString(num_tracked_prefs_); | |
| 252 EXPECT_EQ(static_cast<int>(num_tracked_prefs_str.size()), | |
| 253 base::WriteFile(num_tracked_prefs_file, | |
| 254 num_tracked_prefs_str.c_str(), | |
| 255 num_tracked_prefs_str.size())); | |
| 256 } else { | |
| 257 std::string num_tracked_prefs_str; | |
| 258 EXPECT_TRUE(base::ReadFileToString(num_tracked_prefs_file, | |
| 259 &num_tracked_prefs_str)); | |
| 260 EXPECT_TRUE( | |
| 261 base::StringToInt(num_tracked_prefs_str, &num_tracked_prefs_)); | |
| 262 } | |
| 263 } | |
| 264 | |
| 265 protected: | |
| 266 // Called from the PRE_ test's body. Overrides should use it to setup | |
| 267 // preferences through Chrome. | |
| 268 virtual void SetupPreferences() = 0; | |
| 269 | |
| 270 // Called prior to the main test launching its browser. Overrides should use | |
| 271 // it to attack preferences. |(un)protected_preferences| represent the state | |
| 272 // on disk prior to launching the main test, they can be modified by this | |
| 273 // method and modifications will be flushed back to disk before launching the | |
| 274 // main test. |unprotected_preferences| is never NULL, |protected_preferences| | |
| 275 // may be NULL if in PROTECTION_DISABLED_ON_PLATFORM mode. | |
| 276 virtual void AttackPreferencesOnDisk( | |
| 277 base::DictionaryValue* unprotected_preferences, | |
| 278 base::DictionaryValue* protected_preferences) = 0; | |
| 279 | |
| 280 // Called from the body of the main test. Overrides should use it to verify | |
| 281 // that the browser had the desired reaction when faced when the attack | |
| 282 // orchestrated in AttackPreferencesOnDisk(). | |
| 283 virtual void VerifyReactionToPrefAttack() = 0; | |
| 284 | |
| 285 int num_tracked_prefs_; | |
|
erikwright (departed)
2014/08/02 07:16:52
this should be private with an accessor. It doesn'
gab
2014/08/04 18:53:10
Done.
| |
| 286 | |
| 287 SettingsProtectionLevel protection_level_; | |
|
erikwright (departed)
2014/08/02 07:16:52
At a minimum, const. But an accessor is probably b
gab
2014/08/04 18:53:10
I like const, but I won't add an accessor because
| |
| 288 | |
| 289 private: | |
| 290 // Returns true if this is the PRE_ phase of the test. | |
| 291 bool IsPRETest() { | |
| 292 return StartsWithASCII( | |
| 293 testing::UnitTest::GetInstance()->current_test_info()->name(), | |
| 294 "PRE_", | |
| 295 true /* case_sensitive */); | |
| 296 } | |
| 297 | |
| 298 SettingsProtectionLevel GetProtectionLevelFromTrialGroup( | |
| 299 const std::string& trial_group) { | |
| 300 if (!ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking) | |
| 301 return PROTECTION_DISABLED_ON_PLATFORM; | |
| 302 | |
| 303 // Protection levels can't be adjusted via --force-fieldtrials in official | |
| 304 // builds. | |
| 305 #if defined(OFFICIAL_BUILD) | |
| 306 #if defined(OS_WIN) | |
| 307 // The strongest mode is enforced on Windows in the absence of a field | |
|
erikwright (departed)
2014/08/02 07:16:52
Instead of this, how about ifdef'ing out the insta
gab
2014/08/04 18:53:10
No, I explicitly want to test that --force-fieldtr
| |
| 308 // trial. | |
| 309 return PROTECTION_ENABLED_ALL; | |
| 310 #else | |
| 311 return PROTECTION_DISABLED_FOR_GROUP; | |
| 312 #endif | |
| 313 #endif | |
| 314 | |
| 315 using namespace chrome_prefs::internals; | |
| 316 if (trial_group == kSettingsEnforcementGroupNoEnforcement) { | |
| 317 return PROTECTION_DISABLED_FOR_GROUP; | |
| 318 } else if (trial_group == kSettingsEnforcementGroupEnforceAlways) { | |
| 319 return PROTECTION_ENABLED_BASIC; | |
| 320 } else if (trial_group == kSettingsEnforcementGroupEnforceAlwaysWithDSE) { | |
| 321 return PROTECTION_ENABLED_DSE; | |
| 322 } else if (trial_group == | |
| 323 kSettingsEnforcementGroupEnforceAlwaysWithExtensionsAndDSE) { | |
| 324 return PROTECTION_ENABLED_EXTENSIONS; | |
| 325 } else { | |
| 326 ADD_FAILURE(); | |
| 327 return static_cast<SettingsProtectionLevel>(-1); | |
| 328 } | |
| 329 } | |
| 330 }; | |
| 331 | |
| 332 } // namespace | |
| 333 | |
| 334 // Verifies that nothing is reset when nothing is tampered with. | |
| 335 // Also sanity checks that the expected preferences files are in place. | |
| 336 class PrefHashBrowserTestUnchangedDefault : public PrefHashBrowserTestBase { | |
| 337 public: | |
| 338 virtual void SetupPreferences() OVERRIDE { | |
| 339 // Default Chrome setup. | |
| 340 } | |
| 341 | |
| 342 virtual void AttackPreferencesOnDisk( | |
| 343 base::DictionaryValue* unprotected_preferences, | |
| 344 base::DictionaryValue* protected_preferences) OVERRIDE { | |
| 345 // No attack. | |
| 346 } | |
| 347 | |
| 348 virtual void VerifyReactionToPrefAttack() OVERRIDE { | |
| 349 // Expect all prefs to be reported as Unchanged with no resets. | |
| 350 EXPECT_EQ(protection_level_ > PROTECTION_DISABLED_ON_PLATFORM | |
| 351 ? num_tracked_prefs_ : 0, | |
| 352 GetTrackedPrefHistogramCount( | |
| 353 "Settings.TrackedPreferenceUnchanged", ALLOW_MANY)); | |
| 354 EXPECT_EQ(0, | |
| 355 GetTrackedPrefHistogramCount( | |
| 356 "Settings.TrackedPreferenceWantedReset", ALLOW_NONE)); | |
| 357 EXPECT_EQ(0, | |
| 358 GetTrackedPrefHistogramCount("Settings.TrackedPreferenceReset", | |
| 359 ALLOW_NONE)); | |
| 360 | |
| 361 // Nothing else should have triggered. | |
| 362 EXPECT_EQ(0, | |
| 363 GetTrackedPrefHistogramCount("Settings.TrackedPreferenceChanged", | |
| 364 ALLOW_NONE)); | |
| 365 EXPECT_EQ(0, | |
| 366 GetTrackedPrefHistogramCount("Settings.TrackedPreferenceCleared", | |
| 367 ALLOW_NONE)); | |
| 368 EXPECT_EQ(0, | |
| 369 GetTrackedPrefHistogramCount( | |
| 370 "Settings.TrackedPreferenceInitialized", ALLOW_NONE)); | |
| 371 EXPECT_EQ(0, | |
| 372 GetTrackedPrefHistogramCount( | |
| 373 "Settings.TrackedPreferenceTrustedInitialized", ALLOW_NONE)); | |
| 374 EXPECT_EQ( | |
| 375 0, | |
| 376 GetTrackedPrefHistogramCount( | |
| 377 "Settings.TrackedPreferenceMigratedLegacyDeviceId", ALLOW_NONE)); | |
| 378 } | |
| 379 }; | |
| 380 | |
| 381 PREF_HASH_BROWSER_TEST(PrefHashBrowserTestUnchangedDefault, UnchangedDefault); | |
| 382 | |
| 383 // Augments PrefHashBrowserTestUnchangedDefault to confirm that nothing is reset | |
| 384 // when nothing is tampered with, even if Chrome itself wrote custom prefs in | |
| 385 // its last run. | |
| 386 class PrefHashBrowserTestUnchangedCustom | |
| 387 : public PrefHashBrowserTestUnchangedDefault { | |
| 388 public: | |
| 389 virtual void SetupPreferences() OVERRIDE { | |
| 390 profile()->GetPrefs()->SetString(prefs::kHomePage, "http://example.com"); | |
| 391 | |
| 392 InstallExtensionWithUIAutoConfirm( | |
| 393 test_data_dir_.AppendASCII("good.crx"), 1, browser()); | |
| 394 } | |
| 395 | |
| 396 virtual void VerifyReactionToPrefAttack() OVERRIDE { | |
| 397 // Make sure the settings written in the last run stuck. | |
| 398 EXPECT_EQ("http://example.com", | |
| 399 profile()->GetPrefs()->GetString(prefs::kHomePage)); | |
| 400 | |
| 401 EXPECT_TRUE(extension_service()->GetExtensionById(kGoodCrxId, false)); | |
| 402 | |
| 403 // Reaction should be identical to unattacked default prefs. | |
| 404 PrefHashBrowserTestUnchangedDefault::VerifyReactionToPrefAttack(); | |
| 405 } | |
| 406 }; | |
| 407 | |
| 408 PREF_HASH_BROWSER_TEST(PrefHashBrowserTestUnchangedCustom, UnchangedCustom); | |
| 409 | |
| 410 // Verifies that cleared prefs are reported. | |
| 411 class PrefHashBrowserTestClearedAtomic : public PrefHashBrowserTestBase { | |
| 412 public: | |
| 413 virtual void SetupPreferences() OVERRIDE { | |
| 414 profile()->GetPrefs()->SetString(prefs::kHomePage, "http://example.com"); | |
| 415 } | |
| 416 | |
| 417 virtual void AttackPreferencesOnDisk( | |
| 418 base::DictionaryValue* unprotected_preferences, | |
| 419 base::DictionaryValue* protected_preferences) OVERRIDE { | |
| 420 base::DictionaryValue* selected_prefs = | |
| 421 protection_level_ >= PROTECTION_ENABLED_BASIC ? protected_preferences | |
| 422 : unprotected_preferences; | |
| 423 // |selected_prefs| should never be NULL under the protection level picking | |
| 424 // it. | |
| 425 EXPECT_TRUE(selected_prefs); | |
| 426 EXPECT_TRUE(selected_prefs->Remove(prefs::kHomePage, NULL)); | |
| 427 } | |
| 428 | |
| 429 virtual void VerifyReactionToPrefAttack() OVERRIDE { | |
| 430 // The clearance of homepage should have been noticed (as pref #2 being | |
| 431 // cleared), but shouldn't have triggered a reset (as there is nothing we | |
| 432 // can do when the pref is already gone). | |
| 433 EXPECT_EQ(protection_level_ > PROTECTION_DISABLED_ON_PLATFORM ? 1 : 0, | |
| 434 GetTrackedPrefHistogramCount("Settings.TrackedPreferenceCleared", | |
| 435 BEGIN_ALLOW_SINGLE_BUCKET + 2)); | |
| 436 EXPECT_EQ(protection_level_ > PROTECTION_DISABLED_ON_PLATFORM | |
| 437 ? num_tracked_prefs_ - 1 : 0, | |
| 438 GetTrackedPrefHistogramCount( | |
| 439 "Settings.TrackedPreferenceUnchanged", ALLOW_MANY)); | |
| 440 EXPECT_EQ(0, | |
| 441 GetTrackedPrefHistogramCount( | |
| 442 "Settings.TrackedPreferenceWantedReset", ALLOW_NONE)); | |
| 443 EXPECT_EQ(0, | |
| 444 GetTrackedPrefHistogramCount("Settings.TrackedPreferenceReset", | |
| 445 ALLOW_NONE)); | |
| 446 | |
| 447 // Nothing else should have triggered. | |
| 448 EXPECT_EQ(0, | |
| 449 GetTrackedPrefHistogramCount("Settings.TrackedPreferenceChanged", | |
| 450 ALLOW_NONE)); | |
| 451 EXPECT_EQ(0, | |
| 452 GetTrackedPrefHistogramCount( | |
| 453 "Settings.TrackedPreferenceInitialized", ALLOW_NONE)); | |
| 454 EXPECT_EQ(0, | |
| 455 GetTrackedPrefHistogramCount( | |
| 456 "Settings.TrackedPreferenceTrustedInitialized", ALLOW_NONE)); | |
| 457 EXPECT_EQ( | |
| 458 0, | |
| 459 GetTrackedPrefHistogramCount( | |
| 460 "Settings.TrackedPreferenceMigratedLegacyDeviceId", ALLOW_NONE)); | |
| 461 } | |
| 462 }; | |
| 463 | |
| 464 PREF_HASH_BROWSER_TEST(PrefHashBrowserTestClearedAtomic, ClearedAtomic); | |
| 465 | |
| 466 // Verifies that clearing the MACs results in untrusted Initialized pings for | |
| 467 // non-null protected prefs. | |
| 468 class PrefHashBrowserTestUntrustedInitialized : public PrefHashBrowserTestBase { | |
| 469 public: | |
| 470 virtual void SetupPreferences() OVERRIDE { | |
| 471 // Explicitly set the DSE (it's otherwise NULL by default, preventing | |
| 472 // thorough testing of the PROTECTION_ENABLED_DSE level). | |
| 473 DefaultSearchManager default_search_manager( | |
| 474 profile()->GetPrefs(), DefaultSearchManager::ObserverCallback()); | |
| 475 DefaultSearchManager::Source dse_source = | |
| 476 static_cast<DefaultSearchManager::Source>(-1); | |
| 477 | |
| 478 const TemplateURLData* default_template_url_data = | |
| 479 default_search_manager.GetDefaultSearchEngine(&dse_source); | |
| 480 EXPECT_EQ(DefaultSearchManager::FROM_FALLBACK, dse_source); | |
| 481 | |
| 482 default_search_manager.SetUserSelectedDefaultSearchEngine( | |
| 483 *default_template_url_data); | |
| 484 | |
| 485 default_search_manager.GetDefaultSearchEngine(&dse_source); | |
| 486 EXPECT_EQ(DefaultSearchManager::FROM_USER, dse_source); | |
| 487 | |
| 488 // Also explicitly set an atomic pref that falls under | |
| 489 // PROTECTION_ENABLED_BASIC. | |
| 490 profile()->GetPrefs()->SetInteger(prefs::kRestoreOnStartup, | |
| 491 SessionStartupPref::URLS); | |
| 492 } | |
| 493 | |
| 494 virtual void AttackPreferencesOnDisk( | |
| 495 base::DictionaryValue* unprotected_preferences, | |
| 496 base::DictionaryValue* protected_preferences) OVERRIDE { | |
| 497 EXPECT_TRUE(unprotected_preferences->Remove("protection.macs", NULL)); | |
| 498 if (protected_preferences) | |
| 499 EXPECT_TRUE(protected_preferences->Remove("protection.macs", NULL)); | |
| 500 } | |
| 501 | |
| 502 virtual void VerifyReactionToPrefAttack() OVERRIDE { | |
| 503 // Preferences that are NULL by default will be TrustedInitialized. | |
| 504 int num_null_values = GetTrackedPrefHistogramCount( | |
| 505 "Settings.TrackedPreferenceTrustedInitialized", ALLOW_MANY); | |
| 506 EXPECT_EQ(protection_level_ > PROTECTION_DISABLED_ON_PLATFORM, | |
| 507 num_null_values > 0); | |
| 508 if (num_null_values > 0) { | |
| 509 // This test requires that at least 3 prefs be non-null (extensions, DSE, | |
| 510 // and 1 atomic pref explictly set for this test above). | |
| 511 EXPECT_LT(num_null_values, num_tracked_prefs_ - 3); | |
| 512 } | |
| 513 | |
| 514 // Expect all non-null prefs to be reported as Initialized (with | |
| 515 // accompanying resets or wanted resets based on the current protection | |
| 516 // level). | |
| 517 EXPECT_EQ(num_tracked_prefs_ - num_null_values, | |
| 518 GetTrackedPrefHistogramCount( | |
| 519 "Settings.TrackedPreferenceInitialized", ALLOW_MANY)); | |
| 520 | |
| 521 int num_protected_prefs = 0; | |
| 522 // A switch statement falling through each protection level in decreasing | |
| 523 // levels of protection to add expectations for each level which augments | |
| 524 // the previous one. | |
| 525 switch (protection_level_) { | |
| 526 case PROTECTION_ENABLED_ALL: | |
| 527 // Falls through. | |
| 528 case PROTECTION_ENABLED_EXTENSIONS: | |
| 529 ++num_protected_prefs; | |
| 530 // Falls through. | |
| 531 case PROTECTION_ENABLED_DSE: | |
| 532 ++num_protected_prefs; | |
| 533 // Falls through. | |
| 534 case PROTECTION_ENABLED_BASIC: | |
| 535 num_protected_prefs += num_tracked_prefs_ - num_null_values - 2; | |
| 536 // Falls through. | |
| 537 case PROTECTION_DISABLED_FOR_GROUP: | |
| 538 // No protection. Falls through. | |
| 539 case PROTECTION_DISABLED_ON_PLATFORM: | |
| 540 // No protection. | |
| 541 break; | |
| 542 } | |
| 543 | |
| 544 EXPECT_EQ(num_tracked_prefs_ - num_null_values - num_protected_prefs, | |
| 545 GetTrackedPrefHistogramCount( | |
| 546 "Settings.TrackedPreferenceWantedReset", ALLOW_MANY)); | |
| 547 EXPECT_EQ(num_protected_prefs, | |
| 548 GetTrackedPrefHistogramCount("Settings.TrackedPreferenceReset", | |
| 549 ALLOW_MANY)); | |
| 550 | |
| 551 // Explicitly verify the result of reported resets. | |
| 552 | |
| 553 DefaultSearchManager default_search_manager( | |
| 554 profile()->GetPrefs(), DefaultSearchManager::ObserverCallback()); | |
| 555 DefaultSearchManager::Source dse_source = | |
| 556 static_cast<DefaultSearchManager::Source>(-1); | |
| 557 default_search_manager.GetDefaultSearchEngine(&dse_source); | |
| 558 EXPECT_EQ(protection_level_ < PROTECTION_ENABLED_DSE | |
| 559 ? DefaultSearchManager::FROM_USER | |
| 560 : DefaultSearchManager::FROM_FALLBACK, | |
| 561 dse_source); | |
| 562 | |
| 563 EXPECT_EQ(protection_level_ < PROTECTION_ENABLED_BASIC, | |
| 564 profile()->GetPrefs()->GetInteger(prefs::kRestoreOnStartup) == | |
| 565 SessionStartupPref::URLS); | |
| 566 | |
| 567 // Nothing else should have triggered. | |
| 568 EXPECT_EQ(0, | |
| 569 GetTrackedPrefHistogramCount( | |
| 570 "Settings.TrackedPreferenceUnchanged", ALLOW_NONE)); | |
| 571 EXPECT_EQ(0, | |
| 572 GetTrackedPrefHistogramCount("Settings.TrackedPreferenceChanged", | |
| 573 ALLOW_NONE)); | |
| 574 EXPECT_EQ(0, | |
| 575 GetTrackedPrefHistogramCount("Settings.TrackedPreferenceCleared", | |
| 576 ALLOW_NONE)); | |
| 577 EXPECT_EQ( | |
| 578 0, | |
| 579 GetTrackedPrefHistogramCount( | |
| 580 "Settings.TrackedPreferenceMigratedLegacyDeviceId", ALLOW_NONE)); | |
| 581 } | |
| 582 }; | |
| 583 | |
| 584 PREF_HASH_BROWSER_TEST(PrefHashBrowserTestUntrustedInitialized, | |
| 585 UntrustedInitialized); | |
| 586 | |
| 587 // Verifies that changing an atomic pref results in it being reported (and reset | |
| 588 // if the protection level allows it). | |
| 589 class PrefHashBrowserTestChangedAtomic : public PrefHashBrowserTestBase { | |
| 590 public: | |
| 591 virtual void SetupPreferences() OVERRIDE { | |
| 592 profile()->GetPrefs()->SetInteger(prefs::kRestoreOnStartup, | |
| 593 SessionStartupPref::URLS); | |
| 594 | |
| 595 ListPrefUpdate update(profile()->GetPrefs(), | |
| 596 prefs::kURLsToRestoreOnStartup); | |
| 597 update->AppendString("http://example.com"); | |
| 598 } | |
| 599 | |
| 600 virtual void AttackPreferencesOnDisk( | |
| 601 base::DictionaryValue* unprotected_preferences, | |
| 602 base::DictionaryValue* protected_preferences) OVERRIDE { | |
| 603 base::DictionaryValue* selected_prefs = | |
| 604 protection_level_ >= PROTECTION_ENABLED_BASIC ? protected_preferences | |
| 605 : unprotected_preferences; | |
| 606 // |selected_prefs| should never be NULL under the protection level picking | |
| 607 // it. | |
| 608 EXPECT_TRUE(selected_prefs); | |
| 609 base::ListValue* startup_urls; | |
| 610 EXPECT_TRUE( | |
| 611 selected_prefs->GetList(prefs::kURLsToRestoreOnStartup, &startup_urls)); | |
| 612 EXPECT_TRUE(startup_urls); | |
| 613 EXPECT_EQ(1U, startup_urls->GetSize()); | |
| 614 startup_urls->AppendString("http://example.org"); | |
| 615 } | |
| 616 | |
| 617 virtual void VerifyReactionToPrefAttack() OVERRIDE { | |
| 618 // Expect a single Changed event for tracked pref #4 (startup URLs). | |
| 619 EXPECT_EQ(protection_level_ > PROTECTION_DISABLED_ON_PLATFORM ? 1 : 0, | |
| 620 GetTrackedPrefHistogramCount("Settings.TrackedPreferenceChanged", | |
| 621 BEGIN_ALLOW_SINGLE_BUCKET + 4)); | |
| 622 EXPECT_EQ(protection_level_ > PROTECTION_DISABLED_ON_PLATFORM | |
| 623 ? num_tracked_prefs_ - 1 : 0, | |
| 624 GetTrackedPrefHistogramCount( | |
| 625 "Settings.TrackedPreferenceUnchanged", ALLOW_MANY)); | |
| 626 | |
| 627 EXPECT_EQ( | |
| 628 (protection_level_ > PROTECTION_DISABLED_ON_PLATFORM && | |
| 629 protection_level_ < PROTECTION_ENABLED_BASIC) ? 1 : 0, | |
| 630 GetTrackedPrefHistogramCount("Settings.TrackedPreferenceWantedReset", | |
| 631 BEGIN_ALLOW_SINGLE_BUCKET + 4)); | |
| 632 EXPECT_EQ(protection_level_ >= PROTECTION_ENABLED_BASIC ? 1 : 0, | |
| 633 GetTrackedPrefHistogramCount("Settings.TrackedPreferenceReset", | |
| 634 BEGIN_ALLOW_SINGLE_BUCKET + 4)); | |
| 635 | |
| 636 // Explicitly verify the result of reported resets. | |
| 637 EXPECT_EQ(protection_level_ >= PROTECTION_ENABLED_BASIC ? 0U : 2U, | |
| 638 profile() | |
| 639 ->GetPrefs() | |
| 640 ->GetList(prefs::kURLsToRestoreOnStartup) | |
| 641 ->GetSize()); | |
| 642 | |
| 643 // Nothing else should have triggered. | |
| 644 EXPECT_EQ(0, | |
| 645 GetTrackedPrefHistogramCount("Settings.TrackedPreferenceCleared", | |
| 646 ALLOW_NONE)); | |
| 647 EXPECT_EQ(0, | |
| 648 GetTrackedPrefHistogramCount( | |
| 649 "Settings.TrackedPreferenceInitialized", ALLOW_NONE)); | |
| 650 EXPECT_EQ(0, | |
| 651 GetTrackedPrefHistogramCount( | |
| 652 "Settings.TrackedPreferenceTrustedInitialized", ALLOW_NONE)); | |
| 653 EXPECT_EQ( | |
| 654 0, | |
| 655 GetTrackedPrefHistogramCount( | |
| 656 "Settings.TrackedPreferenceMigratedLegacyDeviceId", ALLOW_NONE)); | |
| 657 } | |
| 658 }; | |
| 659 | |
| 660 PREF_HASH_BROWSER_TEST(PrefHashBrowserTestChangedAtomic, ChangedAtomic); | |
| 661 | |
| 662 // Verifies that changing or adding an entry in a split pref results in both | |
| 663 // items being reported (and remove if the protection level allows it). | |
| 664 class PrefHashBrowserTestChangedSplitPref : public PrefHashBrowserTestBase { | |
| 665 public: | |
| 666 virtual void SetupPreferences() OVERRIDE { | |
| 667 InstallExtensionWithUIAutoConfirm( | |
| 668 test_data_dir_.AppendASCII("good.crx"), 1, browser()); | |
| 669 } | |
| 670 | |
| 671 virtual void AttackPreferencesOnDisk( | |
| 672 base::DictionaryValue* unprotected_preferences, | |
| 673 base::DictionaryValue* protected_preferences) OVERRIDE { | |
| 674 base::DictionaryValue* selected_prefs = | |
| 675 protection_level_ >= PROTECTION_ENABLED_EXTENSIONS | |
| 676 ? protected_preferences | |
| 677 : unprotected_preferences; | |
| 678 // |selected_prefs| should never be NULL under the protection level picking | |
| 679 // it. | |
| 680 EXPECT_TRUE(selected_prefs); | |
| 681 base::DictionaryValue* extensions_dict; | |
| 682 EXPECT_TRUE(selected_prefs->GetDictionary( | |
| 683 extensions::pref_names::kExtensions, &extensions_dict)); | |
| 684 EXPECT_TRUE(extensions_dict); | |
| 685 | |
| 686 // Tamper with any installed setting for good.crx | |
| 687 base::DictionaryValue* good_crx_dict; | |
| 688 EXPECT_TRUE(extensions_dict->GetDictionary(kGoodCrxId, &good_crx_dict)); | |
| 689 int good_crx_state; | |
| 690 EXPECT_TRUE(good_crx_dict->GetInteger("state", &good_crx_state)); | |
| 691 EXPECT_EQ(extensions::Extension::ENABLED, good_crx_state); | |
| 692 good_crx_dict->SetInteger("state", extensions::Extension::DISABLED); | |
| 693 | |
| 694 // Drop a fake extension (for the purpose of this test, dropped settings | |
| 695 // don't need to be valid extension settings). | |
| 696 base::DictionaryValue* fake_extension = new base::DictionaryValue; | |
| 697 fake_extension->SetString("name", "foo"); | |
| 698 extensions_dict->Set(std::string(32, 'a'), fake_extension); | |
| 699 } | |
| 700 | |
| 701 virtual void VerifyReactionToPrefAttack() OVERRIDE { | |
| 702 // Expect a single split pref changed report with a count of 2 for tracked | |
| 703 // pref #5 (extensions). | |
| 704 EXPECT_EQ(protection_level_ > PROTECTION_DISABLED_ON_PLATFORM ? 1 : 0, | |
| 705 GetTrackedPrefHistogramCount("Settings.TrackedPreferenceChanged", | |
| 706 BEGIN_ALLOW_SINGLE_BUCKET + 5)); | |
| 707 EXPECT_EQ(protection_level_ > PROTECTION_DISABLED_ON_PLATFORM ? 1 : 0, | |
| 708 GetTrackedPrefHistogramCount( | |
| 709 "Settings.TrackedSplitPreferenceChanged.extensions.settings", | |
| 710 BEGIN_ALLOW_SINGLE_BUCKET + 2)); | |
| 711 | |
| 712 // Everything else should have remained unchanged. | |
| 713 EXPECT_EQ(protection_level_ > PROTECTION_DISABLED_ON_PLATFORM | |
| 714 ? num_tracked_prefs_ - 1 : 0, | |
| 715 GetTrackedPrefHistogramCount( | |
| 716 "Settings.TrackedPreferenceUnchanged", ALLOW_MANY)); | |
| 717 | |
| 718 EXPECT_EQ( | |
| 719 (protection_level_ > PROTECTION_DISABLED_ON_PLATFORM && | |
| 720 protection_level_ < PROTECTION_ENABLED_EXTENSIONS) ? 1 : 0, | |
| 721 GetTrackedPrefHistogramCount("Settings.TrackedPreferenceWantedReset", | |
| 722 BEGIN_ALLOW_SINGLE_BUCKET + 5)); | |
| 723 EXPECT_EQ(protection_level_ >= PROTECTION_ENABLED_EXTENSIONS ? 1 : 0, | |
| 724 GetTrackedPrefHistogramCount("Settings.TrackedPreferenceReset", | |
| 725 BEGIN_ALLOW_SINGLE_BUCKET + 5)); | |
| 726 | |
| 727 EXPECT_EQ(protection_level_ < PROTECTION_ENABLED_EXTENSIONS, | |
| 728 extension_service()->GetExtensionById(kGoodCrxId, true) != NULL); | |
| 729 | |
| 730 // Nothing else should have triggered. | |
| 731 EXPECT_EQ(0, | |
| 732 GetTrackedPrefHistogramCount("Settings.TrackedPreferenceCleared", | |
| 733 ALLOW_NONE)); | |
| 734 EXPECT_EQ(0, | |
| 735 GetTrackedPrefHistogramCount( | |
| 736 "Settings.TrackedPreferenceInitialized", ALLOW_NONE)); | |
| 737 EXPECT_EQ(0, | |
| 738 GetTrackedPrefHistogramCount( | |
| 739 "Settings.TrackedPreferenceTrustedInitialized", ALLOW_NONE)); | |
| 740 EXPECT_EQ( | |
| 741 0, | |
| 742 GetTrackedPrefHistogramCount( | |
| 743 "Settings.TrackedPreferenceMigratedLegacyDeviceId", ALLOW_NONE)); | |
| 744 } | |
| 745 }; | |
| 746 | |
| 747 PREF_HASH_BROWSER_TEST(PrefHashBrowserTestChangedSplitPref, ChangedSplitPref); | |
| OLD | NEW |