| 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 // Ensures the settings resetter class can only be instantiated if |
| 36 // kInBrowserCleanerUIFeature is enabled. |
| 37 // Test param: |
| 38 // - in_browser_cleaner_ui: if the feature is enabled. |
| 39 class PostCleanupSettingsResetEnabledTest |
| 40 : public InProcessBrowserTest, |
| 41 public ::testing::WithParamInterface<bool> { |
| 42 public: |
| 43 void SetUpInProcessBrowserTestFixture() override { |
| 44 InProcessBrowserTest::SetUpInProcessBrowserTestFixture(); |
| 45 |
| 46 in_browser_cleaner_ui_ = GetParam(); |
| 47 if (in_browser_cleaner_ui_) |
| 48 scoped_feature_list_.InitAndEnableFeature(kInBrowserCleanerUIFeature); |
| 49 else |
| 50 scoped_feature_list_.InitAndDisableFeature(kInBrowserCleanerUIFeature); |
| 51 } |
| 52 |
| 53 protected: |
| 54 base::test::ScopedFeatureList scoped_feature_list_; |
| 55 bool in_browser_cleaner_ui_; |
| 56 }; |
| 57 |
| 58 IN_PROC_BROWSER_TEST_P(PostCleanupSettingsResetEnabledTest, Run) { |
| 59 ASSERT_EQ(in_browser_cleaner_ui_, |
| 60 PostCleanupSettingsResetter::Create() != nullptr); |
| 61 } |
| 62 |
| 63 INSTANTIATE_TEST_CASE_P(Default, |
| 64 PostCleanupSettingsResetEnabledTest, |
| 65 testing::Bool()); |
| 66 |
| 67 // Callback for CreateProfile() that assigns |profile| to |*out_profile| |
| 68 // if the profile creation is successful. |
| 69 void CreateProfileCallback(Profile** out_profile, |
| 70 const base::Closure& closure, |
| 71 Profile* profile, |
| 72 Profile::CreateStatus status) { |
| 73 DCHECK(out_profile); |
| 74 if (status == Profile::CREATE_STATUS_INITIALIZED) |
| 75 *out_profile = profile; |
| 76 closure.Run(); |
| 77 } |
| 78 |
| 79 // Creates a new profile from the UI thread. |
| 80 Profile* CreateProfile() { |
| 81 ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 82 Profile* profile = nullptr; |
| 83 base::RunLoop run_loop; |
| 84 profile_manager->CreateProfileAsync( |
| 85 profile_manager->GenerateNextProfileDirectoryPath(), |
| 86 base::Bind(&CreateProfileCallback, &profile, run_loop.QuitClosure()), |
| 87 base::string16(), std::string(), std::string()); |
| 88 run_loop.Run(); |
| 89 return profile; |
| 90 } |
| 91 |
| 92 // Returns true if |profile| is tagged for settings reset. |
| 93 bool ProfileIsTagged(Profile* profile) { |
| 94 return profile->GetPrefs()->GetBoolean(prefs::kChromeCleanerResetPending); |
| 95 } |
| 96 |
| 97 // If |value| is positive, saves it in the registry at the Chrome Cleaner |
| 98 // registry key under |value_name|. |
| 99 void SetValueIfPositive(const wchar_t* value_name, int64_t value) { |
| 100 if (value <= 0) |
| 101 return; |
| 102 |
| 103 base::string16 cleaner_key_path( |
| 104 chrome_cleaner::kSoftwareRemovalToolRegistryKey); |
| 105 cleaner_key_path.append(L"\\").append(chrome_cleaner::kCleanerSubKey); |
| 106 |
| 107 LONG result = base::win::RegKey(HKEY_CURRENT_USER, cleaner_key_path.c_str(), |
| 108 KEY_SET_VALUE) |
| 109 .WriteValue(value_name, &value, sizeof(value), REG_QWORD); |
| 110 ASSERT_EQ(ERROR_SUCCESS, result); |
| 111 } |
| 112 |
| 113 class ChromeCleanerTagForResettingTest : public InProcessBrowserTest { |
| 114 public: |
| 115 void SetUpInProcessBrowserTestFixture() override { |
| 116 InProcessBrowserTest::SetUpInProcessBrowserTestFixture(); |
| 117 |
| 118 scoped_feature_list_.InitAndEnableFeature(kInBrowserCleanerUIFeature); |
| 119 } |
| 120 |
| 121 protected: |
| 122 base::test::ScopedFeatureList scoped_feature_list_; |
| 123 }; |
| 124 |
| 125 IN_PROC_BROWSER_TEST_F(ChromeCleanerTagForResettingTest, Run) { |
| 126 Browser* browser = chrome::FindLastActive(); |
| 127 ASSERT_TRUE(browser); |
| 128 Profile* profile = browser->profile(); |
| 129 ASSERT_TRUE(profile); |
| 130 |
| 131 std::unique_ptr<PostCleanupSettingsResetter> resetter = |
| 132 PostCleanupSettingsResetter::Create(); |
| 133 ASSERT_TRUE(resetter); |
| 134 resetter->TagForResetting(profile); |
| 135 EXPECT_TRUE(ProfileIsTagged(profile)); |
| 136 } |
| 137 |
| 138 class SettingsResetterTestDelegate |
| 139 : public PostCleanupSettingsResetter::Delegate { |
| 140 public: |
| 141 explicit SettingsResetterTestDelegate(int* num_resets) |
| 142 : num_resets_(num_resets) {} |
| 143 ~SettingsResetterTestDelegate() override = default; |
| 144 |
| 145 void FetchDefaultSettings( |
| 146 DefaultSettingsFetcher::SettingsCallback callback) override { |
| 147 callback.Run(base::MakeUnique<BrandcodedDefaultSettings>()); |
| 148 } |
| 149 |
| 150 // Returns a MockProfileResetter that requires Reset() be called. |
| 151 std::unique_ptr<ProfileResetter> GetProfileResetter( |
| 152 Profile* profile) override { |
| 153 ++(*num_resets_); |
| 154 auto mock_profile_resetter = |
| 155 base::MakeUnique<StrictMock<MockProfileResetter>>(profile); |
| 156 EXPECT_CALL(*mock_profile_resetter, MockReset(_, _, _)); |
| 157 return std::move(mock_profile_resetter); |
| 158 } |
| 159 |
| 160 private: |
| 161 int* num_resets_; |
| 162 |
| 163 DISALLOW_COPY_AND_ASSIGN(SettingsResetterTestDelegate); |
| 164 }; |
| 165 |
| 166 const struct SettingsResetTestParams { |
| 167 int64_t ts_cleanup_started; |
| 168 int64_t ts_cleanup_completed; |
| 169 bool settings_reset; |
| 170 } kSettingsResetTestParams[] = {{0, 0, false}, |
| 171 {10000, 0, false}, |
| 172 {0, 20000, false}, |
| 173 {20000, 10000, false}, |
| 174 {10000, 20000, true}}; |
| 175 |
| 176 // Param for this test: |
| 177 // - SettingsResetTestParams params: values to write to the registry and |
| 178 // whether settings should be reset. |
| 179 class ChromeCleanerResetTaggedProfilesTest |
| 180 : public InProcessBrowserTest, |
| 181 public ::testing::WithParamInterface<SettingsResetTestParams> { |
| 182 public: |
| 183 void SetUpInProcessBrowserTestFixture() override { |
| 184 InProcessBrowserTest::SetUpInProcessBrowserTestFixture(); |
| 185 |
| 186 params_ = GetParam(); |
| 187 scoped_feature_list_.InitAndEnableFeature(kInBrowserCleanerUIFeature); |
| 188 } |
| 189 |
| 190 protected: |
| 191 // Test params. |
| 192 SettingsResetTestParams params_; |
| 193 |
| 194 base::test::ScopedFeatureList scoped_feature_list_; |
| 195 registry_util::RegistryOverrideManager registry_override_manager_; |
| 196 }; |
| 197 |
| 198 IN_PROC_BROWSER_TEST_P(ChromeCleanerResetTaggedProfilesTest, Run) { |
| 199 ASSERT_NO_FATAL_FAILURE( |
| 200 registry_override_manager_.OverrideRegistry(HKEY_CURRENT_USER)); |
| 201 SetValueIfPositive(chrome_cleaner::kCleanupStartedTimestampValueName, |
| 202 params_.ts_cleanup_started); |
| 203 SetValueIfPositive(chrome_cleaner::kCleanupConfirmedTimestampValueName, |
| 204 params_.ts_cleanup_completed); |
| 205 |
| 206 // Profile objects are owned by ProfileManager. |
| 207 Profile* profile1 = CreateProfile(); |
| 208 ASSERT_TRUE(profile1); |
| 209 Profile* profile2 = CreateProfile(); |
| 210 ASSERT_TRUE(profile2); |
| 211 Profile* profile3 = CreateProfile(); |
| 212 ASSERT_TRUE(profile3); |
| 213 |
| 214 profile1->GetPrefs()->SetBoolean(prefs::kChromeCleanerResetPending, true); |
| 215 profile3->GetPrefs()->SetBoolean(prefs::kChromeCleanerResetPending, true); |
| 216 |
| 217 int num_resets = 0; |
| 218 auto delegate = base::MakeUnique<SettingsResetterTestDelegate>(&num_resets); |
| 219 |
| 220 std::unique_ptr<PostCleanupSettingsResetter> resetter = |
| 221 PostCleanupSettingsResetter::Create(); |
| 222 ASSERT_TRUE(resetter); |
| 223 |
| 224 base::RunLoop run_loop_for_reset; |
| 225 resetter->ResetTaggedProfiles({profile1, profile2, profile3}, |
| 226 run_loop_for_reset.QuitClosure(), |
| 227 std::move(delegate)); |
| 228 run_loop_for_reset.Run(); |
| 229 |
| 230 // Profiles 1 and 3 should be reset only if a cleanup has completed according |
| 231 // to the registry entries written by the cleaner and indicated by |
| 232 // params_.settings_reset. Profile 2 should remain not-tagged by the |
| 233 // operation. |
| 234 EXPECT_EQ(params_.settings_reset ? 2 : 0, num_resets); |
| 235 EXPECT_EQ(!params_.settings_reset, ProfileIsTagged(profile1)); |
| 236 EXPECT_EQ(false, ProfileIsTagged(profile2)); |
| 237 EXPECT_EQ(!params_.settings_reset, ProfileIsTagged(profile3)); |
| 238 } |
| 239 |
| 240 INSTANTIATE_TEST_CASE_P(Default, |
| 241 ChromeCleanerResetTaggedProfilesTest, |
| 242 testing::ValuesIn(kSettingsResetTestParams)); |
| 243 |
| 244 } // namespace |
| 245 } // namespace safe_browsing |
| OLD | NEW |