Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(277)

Side by Side Diff: chrome/installer/util/google_update_settings_unittest.cc

Issue 2476573004: Use InstallDetails in installer_util. (Closed)
Patch Set: sync to position 450321 Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/installer/util/google_update_settings.h" 5 #include "chrome/installer/util/google_update_settings.h"
6 6
7 #include <windows.h> 7 #include <windows.h>
8 #include <shlwapi.h> // For SHDeleteKey. 8 #include <shlwapi.h> // For SHDeleteKey.
9 #include <stddef.h> 9 #include <stddef.h>
10 10
11 #include <memory> 11 #include <memory>
12 12
13 #include "base/base_paths.h" 13 #include "base/base_paths.h"
14 #include "base/macros.h" 14 #include "base/macros.h"
15 #include "base/memory/ptr_util.h"
15 #include "base/path_service.h" 16 #include "base/path_service.h"
16 #include "base/strings/utf_string_conversions.h" 17 #include "base/strings/utf_string_conversions.h"
17 #include "base/test/scoped_path_override.h" 18 #include "base/test/scoped_path_override.h"
18 #include "base/test/test_reg_util_win.h" 19 #include "base/test/test_reg_util_win.h"
19 #include "base/win/registry.h" 20 #include "base/win/registry.h"
20 #include "base/win/win_util.h" 21 #include "base/win/win_util.h"
21 #include "chrome/common/chrome_constants.h" 22 #include "chrome/common/chrome_constants.h"
23 #include "chrome/install_static/test/scoped_install_details.h"
22 #include "chrome/installer/util/app_registration_data.h" 24 #include "chrome/installer/util/app_registration_data.h"
23 #include "chrome/installer/util/browser_distribution.h" 25 #include "chrome/installer/util/browser_distribution.h"
24 #include "chrome/installer/util/channel_info.h" 26 #include "chrome/installer/util/channel_info.h"
25 #include "chrome/installer/util/fake_installation_state.h" 27 #include "chrome/installer/util/fake_installation_state.h"
26 #include "chrome/installer/util/google_update_constants.h" 28 #include "chrome/installer/util/google_update_constants.h"
27 #include "chrome/installer/util/helper.h" 29 #include "chrome/installer/util/helper.h"
28 #include "chrome/installer/util/util_constants.h" 30 #include "chrome/installer/util/util_constants.h"
29 #include "chrome/installer/util/work_item_list.h" 31 #include "chrome/installer/util/work_item_list.h"
30 #include "testing/gtest/include/gtest/gtest.h" 32 #include "testing/gtest/include/gtest/gtest.h"
31 33
(...skipping 22 matching lines...) Expand all
54 : program_files_override_(base::DIR_PROGRAM_FILES), 56 : program_files_override_(base::DIR_PROGRAM_FILES),
55 program_files_x86_override_(base::DIR_PROGRAM_FILESX86) {} 57 program_files_x86_override_(base::DIR_PROGRAM_FILESX86) {}
56 58
57 void SetUp() override { 59 void SetUp() override {
58 ASSERT_NO_FATAL_FAILURE( 60 ASSERT_NO_FATAL_FAILURE(
59 registry_overrides_.OverrideRegistry(HKEY_LOCAL_MACHINE)); 61 registry_overrides_.OverrideRegistry(HKEY_LOCAL_MACHINE));
60 ASSERT_NO_FATAL_FAILURE( 62 ASSERT_NO_FATAL_FAILURE(
61 registry_overrides_.OverrideRegistry(HKEY_CURRENT_USER)); 63 registry_overrides_.OverrideRegistry(HKEY_CURRENT_USER));
62 } 64 }
63 65
64 void SetApField(SystemUserInstall is_system, const wchar_t* value) {
65 HKEY root = is_system == SYSTEM_INSTALL ?
66 HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER;
67
68 RegKey update_key;
69 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
70 base::string16 path = dist->GetStateKey();
71 ASSERT_EQ(ERROR_SUCCESS, update_key.Create(root, path.c_str(), KEY_WRITE));
72 ASSERT_EQ(ERROR_SUCCESS, update_key.WriteValue(L"ap", value));
73 }
74
75 // Tests setting the ap= value to various combinations of values with
76 // suffixes, while asserting on the correct channel value.
77 // Note that ap= value has to match "^2.0-d.*" or ".*x64-dev.*" and "^1.1-.*"
78 // or ".*x64-beta.*" for dev and beta channels respectively.
79 void TestCurrentChromeChannelWithVariousApValues(SystemUserInstall install) {
80 static struct Expectation {
81 const wchar_t* ap_value;
82 const wchar_t* channel;
83 bool supports_prefixes;
84 } expectations[] = {
85 { L"2.0-dev", installer::kChromeChannelDev, false},
86 { L"1.1-beta", installer::kChromeChannelBeta, false},
87 { L"x64-dev", installer::kChromeChannelDev, true},
88 { L"x64-beta", installer::kChromeChannelBeta, true},
89 { L"x64-stable", installer::kChromeChannelStable, true},
90 };
91 bool is_system = install == SYSTEM_INSTALL;
92 const wchar_t* prefixes[] = {
93 L"",
94 L"prefix",
95 L"prefix-with-dash",
96 };
97 const wchar_t* suffixes[] = {
98 L"",
99 L"suffix",
100 L"suffix-with-dash",
101 };
102
103 for (const wchar_t* prefix : prefixes) {
104 for (const Expectation& expectation : expectations) {
105 for (const wchar_t* suffix : suffixes) {
106 base::string16 ap = prefix;
107 ap += expectation.ap_value;
108 ap += suffix;
109 const wchar_t* channel = expectation.channel;
110
111 SetApField(install, ap.c_str());
112 const base::string16 ret_channel =
113 GoogleUpdateSettings::GetChromeChannel(is_system);
114
115 // If prefixes are not supported for a channel, we expect the channel
116 // to be "unknown" if a non-empty prefix is present in ap_value.
117 if (!expectation.supports_prefixes && wcslen(prefix) > 0) {
118 EXPECT_STREQ(installer::kChromeChannelUnknown, ret_channel.c_str())
119 << "Expecting channel \"" << installer::kChromeChannelUnknown
120 << "\" for ap=\"" << ap << "\"";
121 } else {
122 EXPECT_STREQ(channel, ret_channel.c_str())
123 << "Expecting channel \"" << channel
124 << "\" for ap=\"" << ap << "\"";
125 }
126 }
127 }
128 }
129 }
130
131 // Test the writing and deleting functionality of the experiments label 66 // Test the writing and deleting functionality of the experiments label
132 // helper. 67 // helper.
133 void TestExperimentsLabelHelper(SystemUserInstall install) { 68 void TestExperimentsLabelHelper(SystemUserInstall install) {
69 // Install a basic InstallDetails instance.
70 install_static::ScopedInstallDetails details(install == SYSTEM_INSTALL);
gab 2017/02/14 16:57:56 This is unfortunate, would be much easier to read
grt (UTC plus 2) 2017/02/14 21:04:24 TODO added to scoped_install_details.h (I hate to
71
134 BrowserDistribution* chrome = BrowserDistribution::GetDistribution(); 72 BrowserDistribution* chrome = BrowserDistribution::GetDistribution();
135 base::string16 value; 73 base::string16 value;
136 #if defined(GOOGLE_CHROME_BUILD) 74 #if defined(GOOGLE_CHROME_BUILD)
137 EXPECT_TRUE(chrome->ShouldSetExperimentLabels()); 75 EXPECT_TRUE(chrome->ShouldSetExperimentLabels());
138 76
139 // Before anything is set, ReadExperimentLabels should succeed but return 77 // Before anything is set, ReadExperimentLabels should succeed but return
140 // an empty string. 78 // an empty string.
141 EXPECT_TRUE(GoogleUpdateSettings::ReadExperimentLabels( 79 EXPECT_TRUE(GoogleUpdateSettings::ReadExperimentLabels(
142 install == SYSTEM_INSTALL, &value)); 80 install == SYSTEM_INSTALL, &value));
143 EXPECT_EQ(base::string16(), value); 81 EXPECT_EQ(base::string16(), value);
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after
290 228
291 // Path overrides so that SHGetFolderPath isn't needed after the registry 229 // Path overrides so that SHGetFolderPath isn't needed after the registry
292 // is overridden. 230 // is overridden.
293 base::ScopedPathOverride program_files_override_; 231 base::ScopedPathOverride program_files_override_;
294 base::ScopedPathOverride program_files_x86_override_; 232 base::ScopedPathOverride program_files_x86_override_;
295 registry_util::RegistryOverrideManager registry_overrides_; 233 registry_util::RegistryOverrideManager registry_overrides_;
296 }; 234 };
297 235
298 } // namespace 236 } // namespace
299 237
300 // Verify that we return success on no registration (which means stable),
301 // whether per-system or per-user install.
302 TEST_F(GoogleUpdateSettingsTest, CurrentChromeChannelAbsent) {
303 // Per-system first.
304 base::string16 channel;
305 channel = GoogleUpdateSettings::GetChromeChannel(true);
306 EXPECT_STREQ(L"", channel.c_str());
307
308 // Then per-user.
309 channel = GoogleUpdateSettings::GetChromeChannel(false);
310 EXPECT_STREQ(L"", channel.c_str());
311 }
312
313 // Test an empty Ap key for system and user.
314 TEST_F(GoogleUpdateSettingsTest, CurrentChromeChannelEmptySystem) {
315 SetApField(SYSTEM_INSTALL, L"");
316 base::string16 channel;
317 channel = GoogleUpdateSettings::GetChromeChannel(true);
318 EXPECT_STREQ(L"", channel.c_str());
319
320 // Per-user lookups still succeed and return empty string.
321 channel = GoogleUpdateSettings::GetChromeChannel(false);
322 EXPECT_STREQ(L"", channel.c_str());
323 }
324
325 TEST_F(GoogleUpdateSettingsTest, CurrentChromeChannelEmptyUser) {
326 SetApField(USER_INSTALL, L"");
327 // Per-system lookups still succeed and return empty string.
328 base::string16 channel;
329 channel = GoogleUpdateSettings::GetChromeChannel(true);
330 EXPECT_STREQ(L"", channel.c_str());
331
332 // Per-user lookup should succeed.
333 channel = GoogleUpdateSettings::GetChromeChannel(false);
334 EXPECT_STREQ(L"", channel.c_str());
335 }
336
337 TEST_F(GoogleUpdateSettingsTest, CurrentChromeChannelVariousApValuesSystem) {
338 TestCurrentChromeChannelWithVariousApValues(SYSTEM_INSTALL);
339 }
340
341 TEST_F(GoogleUpdateSettingsTest, CurrentChromeChannelVariousApValuesUser) {
342 TestCurrentChromeChannelWithVariousApValues(USER_INSTALL);
343 }
344
345 // Run through all combinations of diff vs. full install, success and failure 238 // Run through all combinations of diff vs. full install, success and failure
346 // results, and a fistful of initial "ap" values checking that the expected 239 // results, and a fistful of initial "ap" values checking that the expected
347 // final "ap" value is generated by 240 // final "ap" value is generated by
348 // GoogleUpdateSettings::UpdateGoogleUpdateApKey. 241 // GoogleUpdateSettings::UpdateGoogleUpdateApKey.
349 TEST_F(GoogleUpdateSettingsTest, UpdateGoogleUpdateApKey) { 242 TEST_F(GoogleUpdateSettingsTest, UpdateGoogleUpdateApKey) {
350 const installer::ArchiveType archive_types[] = { 243 const installer::ArchiveType archive_types[] = {
351 installer::UNKNOWN_ARCHIVE_TYPE, 244 installer::UNKNOWN_ARCHIVE_TYPE,
352 installer::FULL_ARCHIVE_TYPE, 245 installer::FULL_ARCHIVE_TYPE,
353 installer::INCREMENTAL_ARCHIVE_TYPE 246 installer::INCREMENTAL_ARCHIVE_TYPE
354 }; 247 };
(...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after
572 GoogleUpdateSettings::kPoliciesKey, 465 GoogleUpdateSettings::kPoliciesKey,
573 KEY_QUERY_VALUE)); 466 KEY_QUERY_VALUE));
574 is_overridden = true; 467 is_overridden = true;
575 EXPECT_EQ(GoogleUpdateSettings::kDefaultUpdatePolicy, 468 EXPECT_EQ(GoogleUpdateSettings::kDefaultUpdatePolicy,
576 GoogleUpdateSettings::GetAppUpdatePolicy(kTestProductGuid, 469 GoogleUpdateSettings::GetAppUpdatePolicy(kTestProductGuid,
577 &is_overridden)); 470 &is_overridden));
578 EXPECT_FALSE(is_overridden); 471 EXPECT_FALSE(is_overridden);
579 } 472 }
580 473
581 TEST_F(GoogleUpdateSettingsTest, UpdateProfileCountsSystemInstall) { 474 TEST_F(GoogleUpdateSettingsTest, UpdateProfileCountsSystemInstall) {
582 // Override FILE_MODULE and FILE_EXE with a path somewhere in the default 475 // Set up a basic system-level InstallDetails.
583 // system-level install location so that 476 install_static::ScopedInstallDetails details(true /* system_level */);
584 // GoogleUpdateSettings::IsSystemInstall() returns true.
585 base::FilePath file_exe;
586 ASSERT_TRUE(PathService::Get(base::FILE_EXE, &file_exe));
587 base::FilePath install_dir(installer::GetChromeInstallPath(
588 true /* system_install */, BrowserDistribution::GetDistribution()));
589 file_exe = install_dir.Append(file_exe.BaseName());
590 base::ScopedPathOverride file_module_override(
591 base::FILE_MODULE, file_exe, true /* is_absolute */, false /* create */);
592 base::ScopedPathOverride file_exe_override(
593 base::FILE_EXE, file_exe, true /* is_absolute */, false /* create */);
594 477
595 // No profile count keys present yet. 478 // No profile count keys present yet.
596 const base::string16& state_key = BrowserDistribution::GetDistribution()-> 479 const base::string16& state_key = BrowserDistribution::GetDistribution()->
597 GetAppRegistrationData().GetStateMediumKey(); 480 GetAppRegistrationData().GetStateMediumKey();
598 base::string16 num_profiles_path(state_key); 481 base::string16 num_profiles_path(state_key);
599 num_profiles_path.append(L"\\"); 482 num_profiles_path.append(L"\\");
600 num_profiles_path.append(google_update::kRegProfilesActive); 483 num_profiles_path.append(google_update::kRegProfilesActive);
601 base::string16 num_signed_in_path(state_key); 484 base::string16 num_signed_in_path(state_key);
602 num_signed_in_path.append(L"\\"); 485 num_signed_in_path.append(L"\\");
603 num_signed_in_path.append(google_update::kRegProfilesSignedIn); 486 num_signed_in_path.append(google_update::kRegProfilesSignedIn);
(...skipping 560 matching lines...) Expand 10 before | Expand all | Expand 10 after
1164 bool system_level_; 1047 bool system_level_;
1165 StateSetting state_value_; 1048 StateSetting state_value_;
1166 StateSetting state_medium_value_; 1049 StateSetting state_medium_value_;
1167 }; 1050 };
1168 1051
1169 const StatsState::UserLevelState StatsState::kUserLevel = {}; 1052 const StatsState::UserLevelState StatsState::kUserLevel = {};
1170 const StatsState::SystemLevelState StatsState::kSystemLevel = {}; 1053 const StatsState::SystemLevelState StatsState::kSystemLevel = {};
1171 1054
1172 // A value parameterized test for testing the stats collection consent setting. 1055 // A value parameterized test for testing the stats collection consent setting.
1173 class CollectStatsConsent : public ::testing::TestWithParam<StatsState> { 1056 class CollectStatsConsent : public ::testing::TestWithParam<StatsState> {
1174 public:
1175 static void SetUpTestCase();
1176 static void TearDownTestCase();
1177 protected: 1057 protected:
1178 void SetUp() override; 1058 CollectStatsConsent();
1179 static void ApplySetting(StatsState::StateSetting setting, 1059 void ApplySetting(StatsState::StateSetting setting,
1180 HKEY root_key, 1060 HKEY root_key,
1181 const base::string16& reg_key); 1061 const base::string16& reg_key);
1182 1062
1183 // TODO(grt): Get rid of these statics and SetUpTestCase. 1063 BrowserDistribution* const dist_;
1184 static base::string16* chrome_version_key_;
1185 static base::string16* chrome_state_key_;
1186 static base::string16* chrome_state_medium_key_;
1187 registry_util::RegistryOverrideManager override_manager_; 1064 registry_util::RegistryOverrideManager override_manager_;
1065 std::unique_ptr<install_static::ScopedInstallDetails> scoped_install_details_;
1188 }; 1066 };
1189 1067
1190 base::string16* CollectStatsConsent::chrome_version_key_;
1191 base::string16* CollectStatsConsent::chrome_state_key_;
1192 base::string16* CollectStatsConsent::chrome_state_medium_key_;
1193
1194 void CollectStatsConsent::SetUpTestCase() {
1195 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
1196 chrome_version_key_ = new base::string16(dist->GetVersionKey());
1197 chrome_state_key_ = new base::string16(dist->GetStateKey());
1198 chrome_state_medium_key_ = new base::string16(dist->GetStateMediumKey());
1199 }
1200
1201 void CollectStatsConsent::TearDownTestCase() {
1202 delete chrome_version_key_;
1203 delete chrome_state_key_;
1204 delete chrome_state_medium_key_;
1205 }
1206
1207 // Install the registry override and apply the settings to the registry. 1068 // Install the registry override and apply the settings to the registry.
1208 void CollectStatsConsent::SetUp() { 1069 CollectStatsConsent::CollectStatsConsent()
1070 : dist_(BrowserDistribution::GetDistribution()) {
1209 // Override both HKLM and HKCU as tests may touch either/both. 1071 // Override both HKLM and HKCU as tests may touch either/both.
1210 ASSERT_NO_FATAL_FAILURE( 1072 ASSERT_NO_FATAL_FAILURE(
1211 override_manager_.OverrideRegistry(HKEY_LOCAL_MACHINE)); 1073 override_manager_.OverrideRegistry(HKEY_LOCAL_MACHINE));
1212 ASSERT_NO_FATAL_FAILURE( 1074 ASSERT_NO_FATAL_FAILURE(
1213 override_manager_.OverrideRegistry(HKEY_CURRENT_USER)); 1075 override_manager_.OverrideRegistry(HKEY_CURRENT_USER));
1214 1076
1215 const StatsState& stats_state = GetParam(); 1077 const StatsState& stats_state = GetParam();
1078 scoped_install_details_ =
1079 base::MakeUnique<install_static::ScopedInstallDetails>(
1080 stats_state.system_level(), 0 /* install_mode_index */);
1216 const HKEY root_key = stats_state.root_key(); 1081 const HKEY root_key = stats_state.root_key();
1217 ApplySetting(stats_state.state_value(), root_key, *chrome_state_key_); 1082 ApplySetting(stats_state.state_value(), root_key, dist_->GetStateKey());
1218 ApplySetting(stats_state.state_medium_value(), root_key, 1083 ApplySetting(stats_state.state_medium_value(), root_key,
1219 *chrome_state_medium_key_); 1084 dist_->GetStateMediumKey());
1220 } 1085 }
1221 1086
1222 // Write the correct value to represent |setting| in the registry. 1087 // Write the correct value to represent |setting| in the registry.
1223 void CollectStatsConsent::ApplySetting(StatsState::StateSetting setting, 1088 void CollectStatsConsent::ApplySetting(StatsState::StateSetting setting,
1224 HKEY root_key, 1089 HKEY root_key,
1225 const base::string16& reg_key) { 1090 const base::string16& reg_key) {
1226 if (setting != StatsState::NO_SETTING) { 1091 if (setting != StatsState::NO_SETTING) {
1227 DWORD value = setting != StatsState::FALSE_SETTING ? 1 : 0; 1092 DWORD value = setting != StatsState::FALSE_SETTING ? 1 : 0;
1228 ASSERT_EQ( 1093 ASSERT_EQ(
1229 ERROR_SUCCESS, 1094 ERROR_SUCCESS,
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
1313 StatsState::TRUE_SETTING), 1178 StatsState::TRUE_SETTING),
1314 StatsState(StatsState::kSystemLevel, 1179 StatsState(StatsState::kSystemLevel,
1315 StatsState::TRUE_SETTING, 1180 StatsState::TRUE_SETTING,
1316 StatsState::NO_SETTING), 1181 StatsState::NO_SETTING),
1317 StatsState(StatsState::kSystemLevel, 1182 StatsState(StatsState::kSystemLevel,
1318 StatsState::TRUE_SETTING, 1183 StatsState::TRUE_SETTING,
1319 StatsState::FALSE_SETTING), 1184 StatsState::FALSE_SETTING),
1320 StatsState(StatsState::kSystemLevel, 1185 StatsState(StatsState::kSystemLevel,
1321 StatsState::TRUE_SETTING, 1186 StatsState::TRUE_SETTING,
1322 StatsState::TRUE_SETTING))); 1187 StatsState::TRUE_SETTING)));
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698