Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 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 "chrome/browser/safe_browsing/chrome_cleaner/settings_resetter_win.h" | |
| 6 | |
| 7 #include <memory> | |
| 8 | |
| 9 #include "base/run_loop.h" | |
| 10 #include "base/test/scoped_feature_list.h" | |
| 11 #include "base/test/test_reg_util_win.h" | |
| 12 #include "base/win/registry.h" | |
| 13 #include "chrome/browser/browser_process.h" | |
| 14 #include "chrome/browser/profile_resetter/brandcoded_default_settings.h" | |
| 15 #include "chrome/browser/profile_resetter/profile_resetter.h" | |
| 16 #include "chrome/browser/profiles/profile.h" | |
| 17 #include "chrome/browser/profiles/profile_manager.h" | |
| 18 #include "chrome/browser/safe_browsing/chrome_cleaner/srt_field_trial_win.h" | |
| 19 #include "chrome/browser/safe_browsing/settings_reset_prompt/settings_reset_prom pt_test_utils.h" | |
| 20 #include "chrome/browser/ui/browser.h" | |
| 21 #include "chrome/browser/ui/browser_finder.h" | |
| 22 #include "chrome/common/pref_names.h" | |
| 23 #include "chrome/test/base/in_process_browser_test.h" | |
| 24 #include "components/chrome_cleaner/public/constants/constants.h" | |
| 25 #include "components/prefs/pref_service.h" | |
| 26 #include "testing/gmock/include/gmock/gmock.h" | |
| 27 #include "testing/gtest/include/gtest/gtest.h" | |
| 28 | |
| 29 namespace safe_browsing { | |
| 30 namespace { | |
| 31 | |
| 32 using ::testing::_; | |
| 33 using ::testing::StrictMock; | |
| 34 | |
| 35 // Callback for CreateProfile() that assigns |profile| to |*out_profile| | |
| 36 // if the profile creation is successful. | |
| 37 void CreateProfileCallback(Profile** out_profile, | |
| 38 const base::Closure& closure, | |
| 39 Profile* profile, | |
| 40 Profile::CreateStatus status) { | |
| 41 DCHECK(out_profile); | |
| 42 if (status == Profile::CREATE_STATUS_INITIALIZED) | |
| 43 *out_profile = profile; | |
| 44 closure.Run(); | |
| 45 } | |
| 46 | |
| 47 // Creates a new profile from the UI thread. | |
| 48 Profile* CreateProfile() { | |
| 49 ProfileManager* profile_manager = g_browser_process->profile_manager(); | |
| 50 Profile* profile = nullptr; | |
| 51 base::RunLoop run_loop; | |
| 52 profile_manager->CreateProfileAsync( | |
| 53 profile_manager->GenerateNextProfileDirectoryPath(), | |
| 54 base::Bind(&CreateProfileCallback, &profile, run_loop.QuitClosure()), | |
| 55 base::string16(), std::string(), std::string()); | |
| 56 run_loop.Run(); | |
| 57 return profile; | |
| 58 } | |
| 59 | |
| 60 // Returns true if |profile| is tagged for settings reset. | |
| 61 bool ProfileIsTagged(Profile* profile) { | |
| 62 return profile->GetPrefs()->GetBoolean(prefs::kChromeCleanerResetPending); | |
| 63 } | |
| 64 | |
| 65 // If |value| is positive, saves it in the registry at the Chrome Cleaner | |
| 66 // registry key under |value_name|. | |
| 67 void SetValueIfPositive(const wchar_t* value_name, int64_t value) { | |
| 68 if (value <= 0) | |
| 69 return; | |
| 70 | |
| 71 base::string16 cleaner_key_path( | |
| 72 chrome_cleaner::kSoftwareRemovalToolRegistryKey); | |
| 73 cleaner_key_path.append(L"\\").append(chrome_cleaner::kCleanerSubKey); | |
| 74 | |
| 75 LONG result = base::win::RegKey(HKEY_CURRENT_USER, cleaner_key_path.c_str(), | |
| 76 KEY_SET_VALUE) | |
| 77 .WriteValue(value_name, &value, sizeof(value), REG_QWORD); | |
| 78 ASSERT_EQ(ERROR_SUCCESS, result); | |
| 79 } | |
| 80 | |
| 81 // Params: | |
| 82 // - bool in_browser_cleaner_ui: if true, consider that kInBrowserCleanerUI | |
|
alito
2017/06/09 01:52:07
If you create a single test for the Create() funct
ftirelo
2017/06/09 21:24:55
Excellent idea. Done.
| |
| 83 // feature is enabled. | |
| 84 class TagProfileForResettingTest : public InProcessBrowserTest, | |
| 85 public ::testing::WithParamInterface<bool> { | |
| 86 public: | |
| 87 void SetUpInProcessBrowserTestFixture() override { | |
| 88 InProcessBrowserTest::SetUpInProcessBrowserTestFixture(); | |
| 89 | |
| 90 in_browser_cleaner_ui_ = GetParam(); | |
| 91 if (in_browser_cleaner_ui_) | |
| 92 scoped_feature_list_.InitAndEnableFeature(kInBrowserCleanerUIFeature); | |
| 93 else | |
| 94 scoped_feature_list_.InitAndDisableFeature(kInBrowserCleanerUIFeature); | |
| 95 } | |
| 96 | |
| 97 protected: | |
| 98 base::test::ScopedFeatureList scoped_feature_list_; | |
| 99 bool in_browser_cleaner_ui_; | |
| 100 }; | |
| 101 | |
| 102 IN_PROC_BROWSER_TEST_P(TagProfileForResettingTest, Run) { | |
| 103 Browser* browser = chrome::FindLastActive(); | |
| 104 ASSERT_TRUE(browser); | |
| 105 Profile* profile = browser->profile(); | |
| 106 ASSERT_TRUE(profile); | |
| 107 | |
| 108 std::unique_ptr<PostCleanupSettingsResetter> resetter( | |
| 109 PostCleanupSettingsResetter::Create()); | |
| 110 if (!in_browser_cleaner_ui_) { | |
| 111 ASSERT_FALSE(resetter); | |
| 112 return; | |
| 113 } | |
| 114 | |
| 115 ASSERT_TRUE(resetter); | |
| 116 resetter->TagForResetting(profile); | |
| 117 EXPECT_EQ(in_browser_cleaner_ui_, ProfileIsTagged(profile)); | |
| 118 } | |
| 119 | |
| 120 INSTANTIATE_TEST_CASE_P(Default, TagProfileForResettingTest, testing::Bool()); | |
| 121 | |
| 122 class SettingsResetterTestDelegate | |
| 123 : public PostCleanupSettingsResetter::Delegate { | |
| 124 public: | |
| 125 explicit SettingsResetterTestDelegate(int* num_resets) | |
| 126 : num_resets_(num_resets) {} | |
| 127 ~SettingsResetterTestDelegate() override = default; | |
| 128 | |
| 129 void FetchDefaultSettings( | |
| 130 DefaultSettingsFetcher::SettingsCallback callback) override { | |
| 131 callback.Run(base::MakeUnique<BrandcodedDefaultSettings>()); | |
| 132 } | |
| 133 | |
| 134 // Returns a MockProfileResetter that requires Reset() be called. | |
| 135 std::unique_ptr<ProfileResetter> GetProfileResetter( | |
| 136 Profile* profile) override { | |
| 137 ++(*num_resets_); | |
| 138 auto mock_profile_resetter = | |
| 139 base::MakeUnique<StrictMock<MockProfileResetter>>(profile); | |
| 140 EXPECT_CALL(*mock_profile_resetter, MockReset(_, _, _)); | |
| 141 return std::move(mock_profile_resetter); | |
| 142 } | |
| 143 | |
| 144 private: | |
| 145 int* num_resets_; | |
| 146 | |
| 147 DISALLOW_COPY_AND_ASSIGN(SettingsResetterTestDelegate); | |
| 148 }; | |
| 149 | |
| 150 const struct SettingsResetTestParams { | |
| 151 int64_t ts_cleanup_started; | |
| 152 int64_t ts_cleanup_completed; | |
| 153 bool settings_reset; | |
| 154 } kSettingsResetTestParams[] = {{0, 0, false}, | |
| 155 {10000, 0, false}, | |
| 156 {0, 20000, false}, | |
| 157 {20000, 10000, false}, | |
| 158 {10000, 20000, true}}; | |
| 159 | |
| 160 // Params: | |
| 161 // - SettingsResetTestParams params: values to write to the registry and | |
| 162 // whether settings should be reset. | |
| 163 // - bool in_browser_cleaner_ui: if true, consider that kInBrowserCleanerUI | |
| 164 // feature is enabled. | |
| 165 class ResetPostCleanupSettingsIfTaggedTest | |
| 166 : public InProcessBrowserTest, | |
| 167 public ::testing::WithParamInterface< | |
| 168 std::tuple<bool, SettingsResetTestParams>> { | |
| 169 public: | |
| 170 void SetUpInProcessBrowserTestFixture() override { | |
| 171 InProcessBrowserTest::SetUpInProcessBrowserTestFixture(); | |
| 172 | |
| 173 std::tie(in_browser_cleaner_ui_, params_) = GetParam(); | |
| 174 if (in_browser_cleaner_ui_) | |
| 175 scoped_feature_list_.InitAndEnableFeature(kInBrowserCleanerUIFeature); | |
| 176 else | |
| 177 scoped_feature_list_.InitAndDisableFeature(kInBrowserCleanerUIFeature); | |
| 178 } | |
| 179 | |
| 180 protected: | |
| 181 // Test params. | |
| 182 SettingsResetTestParams params_; | |
| 183 bool in_browser_cleaner_ui_; | |
| 184 | |
| 185 base::test::ScopedFeatureList scoped_feature_list_; | |
| 186 registry_util::RegistryOverrideManager registry_override_manager_; | |
| 187 }; | |
| 188 | |
| 189 IN_PROC_BROWSER_TEST_P(ResetPostCleanupSettingsIfTaggedTest, Run) { | |
| 190 ASSERT_NO_FATAL_FAILURE( | |
| 191 registry_override_manager_.OverrideRegistry(HKEY_CURRENT_USER)); | |
| 192 SetValueIfPositive(chrome_cleaner::kCleanupStartedTimestampValueName, | |
| 193 params_.ts_cleanup_started); | |
| 194 SetValueIfPositive(chrome_cleaner::kCleanupConfirmedTimestampValueName, | |
| 195 params_.ts_cleanup_completed); | |
| 196 | |
| 197 // Profile objects are owned by ProfileManager. | |
| 198 Profile* profile1 = CreateProfile(); | |
| 199 ASSERT_TRUE(profile1); | |
| 200 Profile* profile2 = CreateProfile(); | |
| 201 ASSERT_TRUE(profile2); | |
| 202 Profile* profile3 = CreateProfile(); | |
| 203 ASSERT_TRUE(profile3); | |
| 204 | |
| 205 profile1->GetPrefs()->SetBoolean(prefs::kChromeCleanerResetPending, true); | |
| 206 profile3->GetPrefs()->SetBoolean(prefs::kChromeCleanerResetPending, true); | |
| 207 | |
| 208 int num_resets = 0; | |
| 209 auto delegate = base::MakeUnique<SettingsResetterTestDelegate>(&num_resets); | |
| 210 | |
| 211 std::unique_ptr<PostCleanupSettingsResetter> resetter( | |
| 212 PostCleanupSettingsResetter::Create()); | |
| 213 if (!in_browser_cleaner_ui_) { | |
| 214 ASSERT_FALSE(resetter); | |
| 215 return; | |
| 216 } | |
| 217 | |
| 218 ASSERT_TRUE(resetter); | |
| 219 base::RunLoop run_loop_for_reset; | |
| 220 resetter->ResetTaggedProfiles({profile1, profile2, profile3}, | |
| 221 run_loop_for_reset.QuitClosure(), | |
| 222 std::move(delegate)); | |
| 223 run_loop_for_reset.Run(); | |
| 224 | |
| 225 // If settings reset is on, profiles 1 and 3 should be reset. | |
|
alito
2017/06/09 01:52:07
Now that the Create() function returns a null ptr
ftirelo
2017/06/09 21:24:55
In fact, it depends on the registry entries above
alito
2017/06/09 23:24:08
Acknowledged.
| |
| 226 EXPECT_EQ(params_.settings_reset ? 2 : 0, num_resets); | |
| 227 EXPECT_EQ(!params_.settings_reset, ProfileIsTagged(profile1)); | |
| 228 EXPECT_EQ(false, ProfileIsTagged(profile2)); | |
| 229 EXPECT_EQ(!params_.settings_reset, ProfileIsTagged(profile3)); | |
| 230 } | |
| 231 | |
| 232 INSTANTIATE_TEST_CASE_P( | |
| 233 Default, | |
| 234 ResetPostCleanupSettingsIfTaggedTest, | |
| 235 testing::Combine(testing::Bool(), | |
| 236 testing::ValuesIn(kSettingsResetTestParams))); | |
| 237 | |
| 238 } // namespace | |
| 239 } // namespace safe_browsing | |
| OLD | NEW |