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