| 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 // Saves |value| in the registry at the value name corresponding to the cleanup |
| 66 // completed state. |
| 67 void SetCompletedState(DWORD value) { |
| 68 base::string16 cleaner_key_path( |
| 69 chrome_cleaner::kSoftwareRemovalToolRegistryKey); |
| 70 cleaner_key_path.append(L"\\").append(chrome_cleaner::kCleanerSubKey); |
| 71 |
| 72 LONG result = |
| 73 base::win::RegKey(HKEY_CURRENT_USER, cleaner_key_path.c_str(), |
| 74 KEY_SET_VALUE) |
| 75 .WriteValue(chrome_cleaner::kCleanupCompletedValueName, value); |
| 76 ASSERT_EQ(ERROR_SUCCESS, result); |
| 77 } |
| 78 |
| 79 class ChromeCleanerTagForResettingTest : public InProcessBrowserTest { |
| 80 public: |
| 81 void SetUpInProcessBrowserTestFixture() override { |
| 82 InProcessBrowserTest::SetUpInProcessBrowserTestFixture(); |
| 83 |
| 84 scoped_feature_list_.InitAndEnableFeature(kInBrowserCleanerUIFeature); |
| 85 } |
| 86 |
| 87 protected: |
| 88 base::test::ScopedFeatureList scoped_feature_list_; |
| 89 }; |
| 90 |
| 91 IN_PROC_BROWSER_TEST_F(ChromeCleanerTagForResettingTest, Run) { |
| 92 Browser* browser = chrome::FindLastActive(); |
| 93 ASSERT_TRUE(browser); |
| 94 Profile* profile = browser->profile(); |
| 95 ASSERT_TRUE(profile); |
| 96 |
| 97 PostCleanupSettingsResetter resetter; |
| 98 resetter.TagForResetting(profile); |
| 99 EXPECT_TRUE(ProfileIsTagged(profile)); |
| 100 } |
| 101 |
| 102 class SettingsResetterTestDelegate |
| 103 : public PostCleanupSettingsResetter::Delegate { |
| 104 public: |
| 105 explicit SettingsResetterTestDelegate(int* num_resets) |
| 106 : num_resets_(num_resets) {} |
| 107 ~SettingsResetterTestDelegate() override = default; |
| 108 |
| 109 void FetchDefaultSettings( |
| 110 DefaultSettingsFetcher::SettingsCallback callback) override { |
| 111 callback.Run(base::MakeUnique<BrandcodedDefaultSettings>()); |
| 112 } |
| 113 |
| 114 // Returns a MockProfileResetter that requires Reset() be called. |
| 115 std::unique_ptr<ProfileResetter> GetProfileResetter( |
| 116 Profile* profile) override { |
| 117 ++(*num_resets_); |
| 118 auto mock_profile_resetter = |
| 119 base::MakeUnique<StrictMock<MockProfileResetter>>(profile); |
| 120 EXPECT_CALL(*mock_profile_resetter, MockReset(_, _, _)); |
| 121 return std::move(mock_profile_resetter); |
| 122 } |
| 123 |
| 124 private: |
| 125 int* num_resets_; |
| 126 |
| 127 DISALLOW_COPY_AND_ASSIGN(SettingsResetterTestDelegate); |
| 128 }; |
| 129 |
| 130 // Indicates the possible values to be written to the registry for cleanup |
| 131 // completion. |
| 132 enum class CleanupCompletionState { |
| 133 // No value will be written to the registry; cleanup should be considered |
| 134 // as not completed. |
| 135 kNotAvailable, |
| 136 // Value 0 will be written to the registry; cleanup should be considered as |
| 137 // not completed. |
| 138 kNotCompleted, |
| 139 // Value 1 will be written to the registry; cleanup should be considered |
| 140 // as completed. |
| 141 kCompleted, |
| 142 // A non-zero value different than 1 will be written to the registry; cleanup |
| 143 // should be considered as not completed. |
| 144 kInvalidValue, |
| 145 }; |
| 146 |
| 147 // Param for this test: |
| 148 // - CleanupCompletionState completion_state: indicates the value to be |
| 149 // written to the registry for cleanup completion. |
| 150 class ChromeCleanerResetTaggedProfilesTest |
| 151 : public InProcessBrowserTest, |
| 152 public ::testing::WithParamInterface<CleanupCompletionState> { |
| 153 public: |
| 154 void SetUpInProcessBrowserTestFixture() override { |
| 155 InProcessBrowserTest::SetUpInProcessBrowserTestFixture(); |
| 156 |
| 157 completion_state_ = GetParam(); |
| 158 ASSERT_TRUE(completion_state_ >= CleanupCompletionState::kNotAvailable && |
| 159 completion_state_ <= CleanupCompletionState::kInvalidValue); |
| 160 scoped_feature_list_.InitAndEnableFeature(kInBrowserCleanerUIFeature); |
| 161 } |
| 162 |
| 163 protected: |
| 164 CleanupCompletionState completion_state_; |
| 165 |
| 166 base::test::ScopedFeatureList scoped_feature_list_; |
| 167 registry_util::RegistryOverrideManager registry_override_manager_; |
| 168 }; |
| 169 |
| 170 IN_PROC_BROWSER_TEST_P(ChromeCleanerResetTaggedProfilesTest, Run) { |
| 171 ASSERT_NO_FATAL_FAILURE( |
| 172 registry_override_manager_.OverrideRegistry(HKEY_CURRENT_USER)); |
| 173 switch (completion_state_) { |
| 174 case CleanupCompletionState::kNotAvailable: |
| 175 // No value written to the registry. |
| 176 break; |
| 177 |
| 178 case CleanupCompletionState::kNotCompleted: |
| 179 SetCompletedState(0); |
| 180 break; |
| 181 |
| 182 case CleanupCompletionState::kCompleted: |
| 183 SetCompletedState(1); |
| 184 break; |
| 185 |
| 186 case CleanupCompletionState::kInvalidValue: |
| 187 SetCompletedState(42); |
| 188 } |
| 189 |
| 190 // Profile objects are owned by ProfileManager. |
| 191 Profile* profile1 = CreateProfile(); |
| 192 ASSERT_TRUE(profile1); |
| 193 Profile* profile2 = CreateProfile(); |
| 194 ASSERT_TRUE(profile2); |
| 195 Profile* profile3 = CreateProfile(); |
| 196 ASSERT_TRUE(profile3); |
| 197 |
| 198 profile1->GetPrefs()->SetBoolean(prefs::kChromeCleanerResetPending, true); |
| 199 profile3->GetPrefs()->SetBoolean(prefs::kChromeCleanerResetPending, true); |
| 200 |
| 201 int num_resets = 0; |
| 202 auto delegate = base::MakeUnique<SettingsResetterTestDelegate>(&num_resets); |
| 203 |
| 204 PostCleanupSettingsResetter resetter; |
| 205 base::RunLoop run_loop_for_reset; |
| 206 resetter.ResetTaggedProfiles({profile1, profile2, profile3}, |
| 207 run_loop_for_reset.QuitClosure(), |
| 208 std::move(delegate)); |
| 209 run_loop_for_reset.Run(); |
| 210 |
| 211 // Profiles 1 and 3 should be reset only if completion_state_ is kCompleted. |
| 212 // Profile 2 should remain not-tagged by the operation. |
| 213 bool reset_expected = completion_state_ == CleanupCompletionState::kCompleted; |
| 214 EXPECT_EQ(reset_expected ? 2 : 0, num_resets); |
| 215 EXPECT_EQ(!reset_expected, ProfileIsTagged(profile1)); |
| 216 EXPECT_EQ(false, ProfileIsTagged(profile2)); |
| 217 EXPECT_EQ(!reset_expected, ProfileIsTagged(profile3)); |
| 218 } |
| 219 |
| 220 INSTANTIATE_TEST_CASE_P(Default, |
| 221 ChromeCleanerResetTaggedProfilesTest, |
| 222 testing::Values(CleanupCompletionState::kNotAvailable, |
| 223 CleanupCompletionState::kNotCompleted, |
| 224 CleanupCompletionState::kCompleted, |
| 225 CleanupCompletionState::kInvalidValue)); |
| 226 |
| 227 } // namespace |
| 228 } // namespace safe_browsing |
| OLD | NEW |