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
3 // found in the LICENSE file.
5 #include "chrome/installer/util/google_update_settings.h"
8 #include <shlwapi.h> // For SHDeleteKey.
10 #include "base/base_paths.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/path_service.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "base/test/scoped_path_override.h"
15 #include "base/test/test_reg_util_win.h"
16 #include "base/win/registry.h"
17 #include "base/win/win_util.h"
18 #include "chrome/common/chrome_constants.h"
19 #include "chrome/installer/util/app_registration_data.h"
20 #include "chrome/installer/util/browser_distribution.h"
21 #include "chrome/installer/util/channel_info.h"
22 #include "chrome/installer/util/fake_installation_state.h"
23 #include "chrome/installer/util/google_update_constants.h"
24 #include "chrome/installer/util/google_update_experiment_util.h"
25 #include "chrome/installer/util/util_constants.h"
26 #include "chrome/installer/util/work_item_list.h"
27 #include "testing/gtest/include/gtest/gtest.h"
29 using base::win::RegKey
;
30 using installer::ChannelInfo
;
34 const wchar_t kTestProductGuid
[] = L
"{89F1B351-B15D-48D4-8F10-1298721CF13D}";
36 #if defined(GOOGLE_CHROME_BUILD)
37 const wchar_t kTestExperimentLabel
[] = L
"test_label_value";
40 // This test fixture redirects the HKLM and HKCU registry hives for
41 // the duration of the test to make it independent of the machine
43 class GoogleUpdateSettingsTest
: public testing::Test
{
45 enum SystemUserInstall
{
50 GoogleUpdateSettingsTest()
51 : program_files_override_(base::DIR_PROGRAM_FILES
),
52 program_files_x86_override_(base::DIR_PROGRAM_FILESX86
) {
53 registry_overrides_
.OverrideRegistry(HKEY_LOCAL_MACHINE
);
54 registry_overrides_
.OverrideRegistry(HKEY_CURRENT_USER
);
57 void SetApField(SystemUserInstall is_system
, const wchar_t* value
) {
58 HKEY root
= is_system
== SYSTEM_INSTALL
?
59 HKEY_LOCAL_MACHINE
: HKEY_CURRENT_USER
;
62 BrowserDistribution
* dist
= BrowserDistribution::GetDistribution();
63 base::string16 path
= dist
->GetStateKey();
64 ASSERT_EQ(ERROR_SUCCESS
, update_key
.Create(root
, path
.c_str(), KEY_WRITE
));
65 ASSERT_EQ(ERROR_SUCCESS
, update_key
.WriteValue(L
"ap", value
));
68 // Sets the "ap" field for a multi-install product (both the product and
70 void SetMultiApField(SystemUserInstall is_system
, const wchar_t* value
) {
71 // Caller must specify a multi-install ap value.
72 ASSERT_NE(base::string16::npos
, base::string16(value
).find(L
"-multi"));
73 HKEY root
= is_system
== SYSTEM_INSTALL
?
74 HKEY_LOCAL_MACHINE
: HKEY_CURRENT_USER
;
77 // Write the ap value for both the product and the binaries.
78 BrowserDistribution
* const kDists
[] = {
79 BrowserDistribution::GetDistribution(),
80 BrowserDistribution::GetSpecificDistribution(
81 BrowserDistribution::CHROME_BINARIES
)
83 for (size_t i
= 0; i
< arraysize(kDists
); ++i
) {
84 base::string16 path
= kDists
[i
]->GetStateKey();
85 ASSERT_EQ(ERROR_SUCCESS
, update_key
.Create(root
, path
.c_str(),
87 ASSERT_EQ(ERROR_SUCCESS
, update_key
.WriteValue(L
"ap", value
));
90 // Make the product technically multi-install.
91 BrowserDistribution
* dist
= BrowserDistribution::GetDistribution();
92 ASSERT_EQ(ERROR_SUCCESS
,
93 update_key
.Create(root
, dist
->GetStateKey().c_str(), KEY_WRITE
));
94 ASSERT_EQ(ERROR_SUCCESS
,
95 update_key
.WriteValue(installer::kUninstallArgumentsField
,
99 // Tests setting the ap= value to various combinations of values with
100 // prefixes and suffixes, while asserting on the correct channel value.
101 // Note that any non-empty ap= value that doesn't match ".*-{dev|beta}.*"
102 // will return the "unknown" channel.
103 void TestCurrentChromeChannelWithVariousApValues(SystemUserInstall install
) {
104 static struct Expectations
{
105 const wchar_t* ap_value
;
106 const wchar_t* channel
;
108 { L
"dev", installer::kChromeChannelDev
},
109 { L
"-dev", installer::kChromeChannelDev
},
110 { L
"-developer", installer::kChromeChannelDev
},
111 { L
"beta", installer::kChromeChannelBeta
},
112 { L
"-beta", installer::kChromeChannelBeta
},
113 { L
"-betamax", installer::kChromeChannelBeta
},
115 bool is_system
= install
== SYSTEM_INSTALL
;
116 const wchar_t* prefixes
[] = {
121 const wchar_t* suffixes
[] = {
127 for (size_t i
= 0; i
< arraysize(prefixes
); ++i
) {
128 for (size_t j
= 0; j
< arraysize(expectations
); ++j
) {
129 for (size_t k
= 0; k
< arraysize(suffixes
); ++k
) {
130 base::string16 ap
= prefixes
[i
];
131 ap
+= expectations
[j
].ap_value
;
133 const wchar_t* channel
= expectations
[j
].channel
;
135 SetApField(install
, ap
.c_str());
136 base::string16 ret_channel
;
138 EXPECT_TRUE(GoogleUpdateSettings::GetChromeChannelAndModifiers(
139 is_system
, &ret_channel
));
140 EXPECT_STREQ(channel
, ret_channel
.c_str())
141 << "Expecting channel \"" << channel
142 << "\" for ap=\"" << ap
<< "\"";
148 // Test the writing and deleting functionality of the experiments label
150 void TestExperimentsLabelHelper(SystemUserInstall install
) {
151 BrowserDistribution
* chrome
=
152 BrowserDistribution::GetSpecificDistribution(
153 BrowserDistribution::CHROME_BROWSER
);
154 base::string16 value
;
155 #if defined(GOOGLE_CHROME_BUILD)
156 EXPECT_TRUE(chrome
->ShouldSetExperimentLabels());
158 // Before anything is set, ReadExperimentLabels should succeed but return
160 EXPECT_TRUE(GoogleUpdateSettings::ReadExperimentLabels(
161 install
== SYSTEM_INSTALL
, &value
));
162 EXPECT_EQ(base::string16(), value
);
164 EXPECT_TRUE(GoogleUpdateSettings::SetExperimentLabels(
165 install
== SYSTEM_INSTALL
, kTestExperimentLabel
));
167 // Validate that something is written. Only worry about the label itself.
169 HKEY root
= install
== SYSTEM_INSTALL
?
170 HKEY_LOCAL_MACHINE
: HKEY_CURRENT_USER
;
171 base::string16 state_key
= install
== SYSTEM_INSTALL
?
172 chrome
->GetStateMediumKey() : chrome
->GetStateKey();
174 EXPECT_EQ(ERROR_SUCCESS
,
175 key
.Open(root
, state_key
.c_str(), KEY_QUERY_VALUE
));
176 EXPECT_EQ(ERROR_SUCCESS
,
177 key
.ReadValue(google_update::kExperimentLabels
, &value
));
178 EXPECT_EQ(kTestExperimentLabel
, value
);
179 EXPECT_TRUE(GoogleUpdateSettings::ReadExperimentLabels(
180 install
== SYSTEM_INSTALL
, &value
));
181 EXPECT_EQ(kTestExperimentLabel
, value
);
184 // Now that the label is set, test the delete functionality. An empty label
185 // should result in deleting the value.
186 EXPECT_TRUE(GoogleUpdateSettings::SetExperimentLabels(
187 install
== SYSTEM_INSTALL
, base::string16()));
188 EXPECT_EQ(ERROR_SUCCESS
,
189 key
.Open(root
, state_key
.c_str(), KEY_QUERY_VALUE
));
190 EXPECT_EQ(ERROR_FILE_NOT_FOUND
,
191 key
.ReadValue(google_update::kExperimentLabels
, &value
));
192 EXPECT_TRUE(GoogleUpdateSettings::ReadExperimentLabels(
193 install
== SYSTEM_INSTALL
, &value
));
194 EXPECT_EQ(base::string16(), value
);
197 EXPECT_FALSE(chrome
->ShouldSetExperimentLabels());
198 EXPECT_FALSE(GoogleUpdateSettings::ReadExperimentLabels(
199 install
== SYSTEM_INSTALL
, &value
));
200 #endif // GOOGLE_CHROME_BUILD
203 // Creates "ap" key with the value given as parameter. Also adds work
204 // items to work_item_list given so that they can be rolled back later.
205 bool CreateApKey(WorkItemList
* work_item_list
, const base::string16
& value
) {
206 HKEY reg_root
= HKEY_CURRENT_USER
;
207 base::string16 reg_key
= GetApKeyPath();
208 work_item_list
->AddCreateRegKeyWorkItem(
209 reg_root
, reg_key
, WorkItem::kWow64Default
);
210 work_item_list
->AddSetRegValueWorkItem(reg_root
,
212 WorkItem::kWow64Default
,
213 google_update::kRegApField
,
216 if (!work_item_list
->Do()) {
217 work_item_list
->Rollback();
223 // Returns the key path of "ap" key, e.g.:
224 // Google\Update\ClientState\<kTestProductGuid>
225 base::string16
GetApKeyPath() {
226 base::string16
reg_key(google_update::kRegPathClientState
);
227 reg_key
.append(L
"\\");
228 reg_key
.append(kTestProductGuid
);
232 // Utility method to read "ap" key value
233 base::string16
ReadApKeyValue() {
235 base::string16 ap_key_value
;
236 base::string16 reg_key
= GetApKeyPath();
237 if (key
.Open(HKEY_CURRENT_USER
, reg_key
.c_str(), KEY_ALL_ACCESS
) ==
239 key
.ReadValue(google_update::kRegApField
, &ap_key_value
);
245 bool SetUpdatePolicyForAppGuid(const base::string16
& app_guid
,
246 GoogleUpdateSettings::UpdatePolicy policy
) {
248 if (policy_key
.Create(HKEY_LOCAL_MACHINE
,
249 GoogleUpdateSettings::kPoliciesKey
,
250 KEY_SET_VALUE
) == ERROR_SUCCESS
) {
251 base::string16
app_update_override(
252 GoogleUpdateSettings::kUpdateOverrideValuePrefix
);
253 app_update_override
.append(app_guid
);
254 return policy_key
.WriteValue(app_update_override
.c_str(),
255 static_cast<DWORD
>(policy
)) == ERROR_SUCCESS
;
260 GoogleUpdateSettings::UpdatePolicy
GetUpdatePolicyForAppGuid(
261 const base::string16
& app_guid
) {
263 if (policy_key
.Create(HKEY_LOCAL_MACHINE
,
264 GoogleUpdateSettings::kPoliciesKey
,
265 KEY_QUERY_VALUE
) == ERROR_SUCCESS
) {
266 base::string16
app_update_override(
267 GoogleUpdateSettings::kUpdateOverrideValuePrefix
);
268 app_update_override
.append(app_guid
);
271 if (policy_key
.ReadValueDW(app_update_override
.c_str(),
272 &value
) == ERROR_SUCCESS
) {
273 return static_cast<GoogleUpdateSettings::UpdatePolicy
>(value
);
276 return GoogleUpdateSettings::UPDATE_POLICIES_COUNT
;
279 bool SetGlobalUpdatePolicy(GoogleUpdateSettings::UpdatePolicy policy
) {
281 return policy_key
.Create(HKEY_LOCAL_MACHINE
,
282 GoogleUpdateSettings::kPoliciesKey
,
283 KEY_SET_VALUE
) == ERROR_SUCCESS
&&
284 policy_key
.WriteValue(GoogleUpdateSettings::kUpdatePolicyValue
,
285 static_cast<DWORD
>(policy
)) == ERROR_SUCCESS
;
288 GoogleUpdateSettings::UpdatePolicy
GetGlobalUpdatePolicy() {
291 return (policy_key
.Create(HKEY_LOCAL_MACHINE
,
292 GoogleUpdateSettings::kPoliciesKey
,
293 KEY_QUERY_VALUE
) == ERROR_SUCCESS
&&
294 policy_key
.ReadValueDW(GoogleUpdateSettings::kUpdatePolicyValue
,
295 &value
) == ERROR_SUCCESS
) ?
296 static_cast<GoogleUpdateSettings::UpdatePolicy
>(value
) :
297 GoogleUpdateSettings::UPDATE_POLICIES_COUNT
;
300 bool SetUpdateTimeoutOverride(DWORD time_in_minutes
) {
302 return policy_key
.Create(HKEY_LOCAL_MACHINE
,
303 GoogleUpdateSettings::kPoliciesKey
,
304 KEY_SET_VALUE
) == ERROR_SUCCESS
&&
305 policy_key
.WriteValue(
306 GoogleUpdateSettings::kCheckPeriodOverrideMinutes
,
307 time_in_minutes
) == ERROR_SUCCESS
;
310 // Path overrides so that SHGetFolderPath isn't needed after the registry
312 base::ScopedPathOverride program_files_override_
;
313 base::ScopedPathOverride program_files_x86_override_
;
314 registry_util::RegistryOverrideManager registry_overrides_
;
319 // Verify that we return success on no registration (which means stable),
320 // whether per-system or per-user install.
321 TEST_F(GoogleUpdateSettingsTest
, CurrentChromeChannelAbsent
) {
323 base::string16 channel
;
324 EXPECT_TRUE(GoogleUpdateSettings::GetChromeChannelAndModifiers(true,
326 EXPECT_STREQ(L
"", channel
.c_str());
329 EXPECT_TRUE(GoogleUpdateSettings::GetChromeChannelAndModifiers(false,
331 EXPECT_STREQ(L
"", channel
.c_str());
334 // Test an empty Ap key for system and user.
335 TEST_F(GoogleUpdateSettingsTest
, CurrentChromeChannelEmptySystem
) {
336 SetApField(SYSTEM_INSTALL
, L
"");
337 base::string16 channel
;
338 EXPECT_TRUE(GoogleUpdateSettings::GetChromeChannelAndModifiers(true,
340 EXPECT_STREQ(L
"", channel
.c_str());
342 // Per-user lookups still succeed and return empty string.
343 EXPECT_TRUE(GoogleUpdateSettings::GetChromeChannelAndModifiers(false,
345 EXPECT_STREQ(L
"", channel
.c_str());
348 TEST_F(GoogleUpdateSettingsTest
, CurrentChromeChannelEmptyUser
) {
349 SetApField(USER_INSTALL
, L
"");
350 // Per-system lookups still succeed and return empty string.
351 base::string16 channel
;
352 EXPECT_TRUE(GoogleUpdateSettings::GetChromeChannelAndModifiers(true,
354 EXPECT_STREQ(L
"", channel
.c_str());
356 // Per-user lookup should succeed.
357 EXPECT_TRUE(GoogleUpdateSettings::GetChromeChannelAndModifiers(false,
359 EXPECT_STREQ(L
"", channel
.c_str());
362 // Test that the channel is pulled from the binaries for multi-install products.
363 TEST_F(GoogleUpdateSettingsTest
, MultiInstallChannelFromBinaries
) {
364 SetMultiApField(USER_INSTALL
, L
"2.0-dev-multi-chrome");
365 base::string16 channel
;
367 EXPECT_TRUE(GoogleUpdateSettings::GetChromeChannelAndModifiers(false,
369 EXPECT_STREQ(L
"dev-m", channel
.c_str());
371 // See if the same happens if the product's ap is cleared.
372 SetApField(USER_INSTALL
, L
"");
373 EXPECT_TRUE(GoogleUpdateSettings::GetChromeChannelAndModifiers(false,
375 EXPECT_STREQ(L
"dev-m", channel
.c_str());
377 // Test the converse (binaries are stable, Chrome is other).
378 SetMultiApField(USER_INSTALL
, L
"-multi-chrome");
379 SetApField(USER_INSTALL
, L
"2.0-dev-multi-chrome");
380 EXPECT_TRUE(GoogleUpdateSettings::GetChromeChannelAndModifiers(false,
382 EXPECT_STREQ(L
"m", channel
.c_str());
385 TEST_F(GoogleUpdateSettingsTest
, CurrentChromeChannelVariousApValuesSystem
) {
386 TestCurrentChromeChannelWithVariousApValues(SYSTEM_INSTALL
);
389 TEST_F(GoogleUpdateSettingsTest
, CurrentChromeChannelVariousApValuesUser
) {
390 TestCurrentChromeChannelWithVariousApValues(USER_INSTALL
);
393 // Run through all combinations of diff vs. full install, single vs. multi
394 // install, success and failure results, and a fistful of initial "ap" values
395 // checking that the expected final "ap" value is generated by
396 // GoogleUpdateSettings::UpdateGoogleUpdateApKey.
397 TEST_F(GoogleUpdateSettingsTest
, UpdateGoogleUpdateApKey
) {
398 const installer::ArchiveType archive_types
[] = {
399 installer::UNKNOWN_ARCHIVE_TYPE
,
400 installer::FULL_ARCHIVE_TYPE
,
401 installer::INCREMENTAL_ARCHIVE_TYPE
403 const int results
[] = {
404 installer::FIRST_INSTALL_SUCCESS
,
405 installer::INSTALL_FAILED
407 const wchar_t* const plain
[] = {
412 const wchar_t* const full
[] = {
417 COMPILE_ASSERT(arraysize(full
) == arraysize(plain
), bad_full_array_size
);
418 const wchar_t* const multifail
[] = {
423 COMPILE_ASSERT(arraysize(multifail
) == arraysize(plain
),
424 bad_multifail_array_size
);
425 const wchar_t* const multifail_full
[] = {
427 L
"1.1-multifail-full",
428 L
"1.1-dev-multifail-full"
430 COMPILE_ASSERT(arraysize(multifail_full
) == arraysize(plain
),
431 bad_multifail_full_array_size
);
432 const wchar_t* const* input_arrays
[] = {
439 for (int type_idx
= 0; type_idx
< arraysize(archive_types
); ++type_idx
) {
440 const installer::ArchiveType archive_type
= archive_types
[type_idx
];
441 for (int result_idx
= 0; result_idx
< arraysize(results
); ++result_idx
) {
442 const int result
= results
[result_idx
];
443 // The archive type will/must always be known on install success.
444 if (archive_type
== installer::UNKNOWN_ARCHIVE_TYPE
&&
445 result
== installer::FIRST_INSTALL_SUCCESS
) {
448 const wchar_t* const* outputs
= NULL
;
449 if (result
== installer::FIRST_INSTALL_SUCCESS
||
450 archive_type
== installer::FULL_ARCHIVE_TYPE
) {
452 } else if (archive_type
== installer::INCREMENTAL_ARCHIVE_TYPE
) {
454 } // else if (archive_type == UNKNOWN) see below
456 for (int inputs_idx
= 0; inputs_idx
< arraysize(input_arrays
);
458 const wchar_t* const* inputs
= input_arrays
[inputs_idx
];
459 if (archive_type
== installer::UNKNOWN_ARCHIVE_TYPE
) {
460 // "-full" is untouched if the archive type is unknown.
461 // "-multifail" is unconditionally removed.
462 if (inputs
== full
|| inputs
== multifail_full
)
467 for (int input_idx
= 0; input_idx
< arraysize(plain
); ++input_idx
) {
468 const wchar_t* input
= inputs
[input_idx
];
469 const wchar_t* output
= outputs
[input_idx
];
472 if (output
== v
.value()) {
473 EXPECT_FALSE(GoogleUpdateSettings::UpdateGoogleUpdateApKey(
474 archive_type
, result
, &v
))
475 << "archive_type: " << archive_type
476 << ", result: " << result
477 << ", input ap value: " << input
;
479 EXPECT_TRUE(GoogleUpdateSettings::UpdateGoogleUpdateApKey(
480 archive_type
, result
, &v
))
481 << "archive_type: " << archive_type
482 << ", result: " << result
483 << ", input ap value: " << input
;
485 EXPECT_EQ(output
, v
.value())
486 << "archive_type: " << archive_type
487 << ", result: " << result
488 << ", input ap value: " << input
;
495 TEST_F(GoogleUpdateSettingsTest
, UpdateInstallStatusTest
) {
496 scoped_ptr
<WorkItemList
> work_item_list(WorkItem::CreateWorkItemList());
497 // Test incremental install failure
498 ASSERT_TRUE(CreateApKey(work_item_list
.get(), L
""))
499 << "Failed to create ap key.";
500 GoogleUpdateSettings::UpdateInstallStatus(false,
501 installer::INCREMENTAL_ARCHIVE_TYPE
,
502 installer::INSTALL_FAILED
,
504 EXPECT_STREQ(ReadApKeyValue().c_str(), L
"-full");
505 work_item_list
->Rollback();
507 work_item_list
.reset(WorkItem::CreateWorkItemList());
508 // Test incremental install success
509 ASSERT_TRUE(CreateApKey(work_item_list
.get(), L
""))
510 << "Failed to create ap key.";
511 GoogleUpdateSettings::UpdateInstallStatus(false,
512 installer::INCREMENTAL_ARCHIVE_TYPE
,
513 installer::FIRST_INSTALL_SUCCESS
,
515 EXPECT_STREQ(ReadApKeyValue().c_str(), L
"");
516 work_item_list
->Rollback();
518 work_item_list
.reset(WorkItem::CreateWorkItemList());
519 // Test full install failure
520 ASSERT_TRUE(CreateApKey(work_item_list
.get(), L
"-full"))
521 << "Failed to create ap key.";
522 GoogleUpdateSettings::UpdateInstallStatus(false, installer::FULL_ARCHIVE_TYPE
,
523 installer::INSTALL_FAILED
,
525 EXPECT_STREQ(ReadApKeyValue().c_str(), L
"");
526 work_item_list
->Rollback();
528 work_item_list
.reset(WorkItem::CreateWorkItemList());
529 // Test full install success
530 ASSERT_TRUE(CreateApKey(work_item_list
.get(), L
"-full"))
531 << "Failed to create ap key.";
532 GoogleUpdateSettings::UpdateInstallStatus(false, installer::FULL_ARCHIVE_TYPE
,
533 installer::FIRST_INSTALL_SUCCESS
,
535 EXPECT_STREQ(ReadApKeyValue().c_str(), L
"");
536 work_item_list
->Rollback();
538 work_item_list
.reset(WorkItem::CreateWorkItemList());
539 // Test the case of when "ap" key doesnt exist at all
540 base::string16 ap_key_value
= ReadApKeyValue();
541 base::string16 reg_key
= GetApKeyPath();
542 HKEY reg_root
= HKEY_CURRENT_USER
;
543 bool ap_key_deleted
= false;
545 if (key
.Open(HKEY_CURRENT_USER
, reg_key
.c_str(), KEY_ALL_ACCESS
) !=
547 work_item_list
->AddCreateRegKeyWorkItem(
548 reg_root
, reg_key
, WorkItem::kWow64Default
);
549 ASSERT_TRUE(work_item_list
->Do()) << "Failed to create ClientState key.";
550 } else if (key
.DeleteValue(google_update::kRegApField
) == ERROR_SUCCESS
) {
551 ap_key_deleted
= true;
553 // try differential installer
554 GoogleUpdateSettings::UpdateInstallStatus(false,
555 installer::INCREMENTAL_ARCHIVE_TYPE
,
556 installer::INSTALL_FAILED
,
558 EXPECT_STREQ(ReadApKeyValue().c_str(), L
"-full");
559 // try full installer now
560 GoogleUpdateSettings::UpdateInstallStatus(false, installer::FULL_ARCHIVE_TYPE
,
561 installer::INSTALL_FAILED
,
563 EXPECT_STREQ(ReadApKeyValue().c_str(), L
"");
564 // Now cleanup to leave the system in unchanged state.
565 // - Diff installer creates an ap key if it didnt exist, so delete this ap key
566 // - If we created any reg key path for ap, roll it back
567 // - Finally restore the original value of ap key.
568 key
.Open(HKEY_CURRENT_USER
, reg_key
.c_str(), KEY_ALL_ACCESS
);
569 key
.DeleteValue(google_update::kRegApField
);
570 work_item_list
->Rollback();
571 if (ap_key_deleted
) {
572 work_item_list
.reset(WorkItem::CreateWorkItemList());
573 ASSERT_TRUE(CreateApKey(work_item_list
.get(), ap_key_value
))
574 << "Failed to restore ap key.";
578 TEST_F(GoogleUpdateSettingsTest
, SetEULAConsent
) {
579 using installer::FakeInstallationState
;
581 const bool multi_install
= true;
582 const bool system_level
= true;
583 FakeInstallationState machine_state
;
585 // Chrome is installed.
586 machine_state
.AddChrome(system_level
, multi_install
,
587 new Version(chrome::kChromeVersion
));
591 BrowserDistribution
* binaries
=
592 BrowserDistribution::GetSpecificDistribution(
593 BrowserDistribution::CHROME_BINARIES
);
594 BrowserDistribution
* chrome
=
595 BrowserDistribution::GetSpecificDistribution(
596 BrowserDistribution::CHROME_BROWSER
);
598 // eulaconsent is set on both the product and the binaries.
599 EXPECT_TRUE(GoogleUpdateSettings::SetEULAConsent(machine_state
, chrome
,
601 EXPECT_EQ(ERROR_SUCCESS
,
602 key
.Open(HKEY_LOCAL_MACHINE
, binaries
->GetStateMediumKey().c_str(),
604 EXPECT_EQ(ERROR_SUCCESS
,
605 key
.ReadValueDW(google_update::kRegEULAAceptedField
, &value
));
606 EXPECT_EQ(1U, value
);
607 EXPECT_EQ(ERROR_SUCCESS
,
608 key
.Open(HKEY_LOCAL_MACHINE
, chrome
->GetStateMediumKey().c_str(),
610 EXPECT_EQ(ERROR_SUCCESS
,
611 key
.ReadValueDW(google_update::kRegEULAAceptedField
, &value
));
612 EXPECT_EQ(1U, value
);
615 // Test that the appropriate default is returned if no update override is
617 TEST_F(GoogleUpdateSettingsTest
, GetAppUpdatePolicyNoOverride
) {
618 // There are no policies at all.
619 EXPECT_EQ(ERROR_FILE_NOT_FOUND
,
620 RegKey().Open(HKEY_LOCAL_MACHINE
,
621 GoogleUpdateSettings::kPoliciesKey
,
623 bool is_overridden
= true;
624 EXPECT_EQ(GoogleUpdateSettings::kDefaultUpdatePolicy
,
625 GoogleUpdateSettings::GetAppUpdatePolicy(kTestProductGuid
,
627 EXPECT_FALSE(is_overridden
);
629 // The policy key exists, but there are no values of interest present.
630 EXPECT_EQ(ERROR_SUCCESS
,
631 RegKey().Create(HKEY_LOCAL_MACHINE
,
632 GoogleUpdateSettings::kPoliciesKey
,
634 EXPECT_EQ(ERROR_SUCCESS
,
635 RegKey().Open(HKEY_LOCAL_MACHINE
,
636 GoogleUpdateSettings::kPoliciesKey
,
638 is_overridden
= true;
639 EXPECT_EQ(GoogleUpdateSettings::kDefaultUpdatePolicy
,
640 GoogleUpdateSettings::GetAppUpdatePolicy(kTestProductGuid
,
642 EXPECT_FALSE(is_overridden
);
645 TEST_F(GoogleUpdateSettingsTest
, UpdateProfileCountsSystemInstall
) {
646 // Override FILE_MODULE and FILE_EXE with a path somewhere in the default
647 // system-level install location so that
648 // GoogleUpdateSettings::IsSystemInstall() returns true.
649 base::FilePath file_exe
;
650 ASSERT_TRUE(PathService::Get(base::FILE_EXE
, &file_exe
));
651 base::FilePath
install_dir(installer::GetChromeInstallPath(
652 true /* system_install */, BrowserDistribution::GetDistribution()));
653 file_exe
= install_dir
.Append(file_exe
.BaseName());
654 base::ScopedPathOverride
file_module_override(
655 base::FILE_MODULE
, file_exe
, true /* is_absolute */, false /* create */);
656 base::ScopedPathOverride
file_exe_override(
657 base::FILE_EXE
, file_exe
, true /* is_absolute */, false /* create */);
659 // No profile count keys present yet.
660 const base::string16
& state_key
= BrowserDistribution::GetDistribution()->
661 GetAppRegistrationData().GetStateMediumKey();
662 base::string16
num_profiles_path(state_key
);
663 num_profiles_path
.append(L
"\\");
664 num_profiles_path
.append(google_update::kRegProfilesActive
);
665 base::string16
num_signed_in_path(state_key
);
666 num_signed_in_path
.append(L
"\\");
667 num_signed_in_path
.append(google_update::kRegProfilesSignedIn
);
669 EXPECT_EQ(ERROR_FILE_NOT_FOUND
,
670 RegKey().Open(HKEY_LOCAL_MACHINE
,
671 num_profiles_path
.c_str(),
673 EXPECT_EQ(ERROR_FILE_NOT_FOUND
,
674 RegKey().Open(HKEY_LOCAL_MACHINE
,
675 num_signed_in_path
.c_str(),
678 // Show time! Write the values.
679 GoogleUpdateSettings::UpdateProfileCounts(3, 2);
681 // Verify the keys were created.
682 EXPECT_EQ(ERROR_SUCCESS
,
683 RegKey().Open(HKEY_LOCAL_MACHINE
,
684 num_profiles_path
.c_str(),
686 EXPECT_EQ(ERROR_SUCCESS
,
687 RegKey().Open(HKEY_LOCAL_MACHINE
,
688 num_signed_in_path
.c_str(),
691 base::string16 uniquename
;
692 EXPECT_TRUE(base::win::GetUserSidString(&uniquename
));
694 // Verify the values are accessible.
695 DWORD num_profiles
= 0;
696 DWORD num_signed_in
= 0;
697 base::string16 aggregate
;
700 RegKey(HKEY_LOCAL_MACHINE
, num_profiles_path
.c_str(),
701 KEY_QUERY_VALUE
).ReadValueDW(uniquename
.c_str(),
705 RegKey(HKEY_LOCAL_MACHINE
, num_signed_in_path
.c_str(),
706 KEY_QUERY_VALUE
).ReadValueDW(uniquename
.c_str(),
710 RegKey(HKEY_LOCAL_MACHINE
, num_signed_in_path
.c_str(),
711 KEY_QUERY_VALUE
).ReadValue(google_update::kRegAggregateMethod
,
714 // Verify the correct values were written.
715 EXPECT_EQ(3, num_profiles
);
716 EXPECT_EQ(2, num_signed_in
);
717 EXPECT_EQ(L
"sum()", aggregate
);
720 TEST_F(GoogleUpdateSettingsTest
, UpdateProfileCountsUserInstall
) {
721 // Unit tests never operate as an installed application, so will never
722 // be a system install.
724 // No profile count values present yet.
725 const base::string16
& state_key
= BrowserDistribution::GetDistribution()->
726 GetAppRegistrationData().GetStateKey();
728 EXPECT_EQ(ERROR_FILE_NOT_FOUND
,
729 RegKey().Open(HKEY_CURRENT_USER
,
733 // Show time! Write the values.
734 GoogleUpdateSettings::UpdateProfileCounts(4, 1);
736 // Verify the key was created.
737 EXPECT_EQ(ERROR_SUCCESS
,
738 RegKey().Open(HKEY_CURRENT_USER
,
742 // Verify the values are accessible.
743 base::string16 num_profiles
;
744 base::string16 num_signed_in
;
747 RegKey(HKEY_CURRENT_USER
, state_key
.c_str(), KEY_QUERY_VALUE
).
748 ReadValue(google_update::kRegProfilesActive
, &num_profiles
));
751 RegKey(HKEY_CURRENT_USER
, state_key
.c_str(), KEY_QUERY_VALUE
).
752 ReadValue(google_update::kRegProfilesSignedIn
, &num_signed_in
));
754 // Verify the correct values were written.
755 EXPECT_EQ(L
"4", num_profiles
);
756 EXPECT_EQ(L
"1", num_signed_in
);
759 #if defined(GOOGLE_CHROME_BUILD)
761 // Test that the default override is returned if no app-specific override is
763 TEST_F(GoogleUpdateSettingsTest
, GetAppUpdatePolicyDefaultOverride
) {
764 EXPECT_EQ(ERROR_SUCCESS
,
765 RegKey(HKEY_LOCAL_MACHINE
, GoogleUpdateSettings::kPoliciesKey
,
766 KEY_SET_VALUE
).WriteValue(
767 GoogleUpdateSettings::kUpdatePolicyValue
,
768 static_cast<DWORD
>(0)));
769 bool is_overridden
= true;
770 EXPECT_EQ(GoogleUpdateSettings::UPDATES_DISABLED
,
771 GoogleUpdateSettings::GetAppUpdatePolicy(kTestProductGuid
,
773 EXPECT_FALSE(is_overridden
);
775 EXPECT_EQ(ERROR_SUCCESS
,
776 RegKey(HKEY_LOCAL_MACHINE
, GoogleUpdateSettings::kPoliciesKey
,
777 KEY_SET_VALUE
).WriteValue(
778 GoogleUpdateSettings::kUpdatePolicyValue
,
779 static_cast<DWORD
>(1)));
780 is_overridden
= true;
781 EXPECT_EQ(GoogleUpdateSettings::AUTOMATIC_UPDATES
,
782 GoogleUpdateSettings::GetAppUpdatePolicy(kTestProductGuid
,
784 EXPECT_FALSE(is_overridden
);
786 EXPECT_EQ(ERROR_SUCCESS
,
787 RegKey(HKEY_LOCAL_MACHINE
, GoogleUpdateSettings::kPoliciesKey
,
788 KEY_SET_VALUE
).WriteValue(
789 GoogleUpdateSettings::kUpdatePolicyValue
,
790 static_cast<DWORD
>(2)));
791 is_overridden
= true;
792 EXPECT_EQ(GoogleUpdateSettings::MANUAL_UPDATES_ONLY
,
793 GoogleUpdateSettings::GetAppUpdatePolicy(kTestProductGuid
,
795 EXPECT_FALSE(is_overridden
);
797 EXPECT_EQ(ERROR_SUCCESS
,
798 RegKey(HKEY_LOCAL_MACHINE
, GoogleUpdateSettings::kPoliciesKey
,
799 KEY_SET_VALUE
).WriteValue(
800 GoogleUpdateSettings::kUpdatePolicyValue
,
801 static_cast<DWORD
>(3)));
802 is_overridden
= true;
803 EXPECT_EQ(GoogleUpdateSettings::AUTO_UPDATES_ONLY
,
804 GoogleUpdateSettings::GetAppUpdatePolicy(kTestProductGuid
,
806 EXPECT_FALSE(is_overridden
);
808 // The default policy should be in force for bogus values.
809 EXPECT_EQ(ERROR_SUCCESS
,
810 RegKey(HKEY_LOCAL_MACHINE
, GoogleUpdateSettings::kPoliciesKey
,
811 KEY_SET_VALUE
).WriteValue(
812 GoogleUpdateSettings::kUpdatePolicyValue
,
813 static_cast<DWORD
>(4)));
814 is_overridden
= true;
815 EXPECT_EQ(GoogleUpdateSettings::kDefaultUpdatePolicy
,
816 GoogleUpdateSettings::GetAppUpdatePolicy(kTestProductGuid
,
818 EXPECT_FALSE(is_overridden
);
821 // Test that an app-specific override is used if present.
822 TEST_F(GoogleUpdateSettingsTest
, GetAppUpdatePolicyAppOverride
) {
823 base::string16
app_policy_value(
824 GoogleUpdateSettings::kUpdateOverrideValuePrefix
);
825 app_policy_value
.append(kTestProductGuid
);
827 EXPECT_EQ(ERROR_SUCCESS
,
828 RegKey(HKEY_LOCAL_MACHINE
, GoogleUpdateSettings::kPoliciesKey
,
829 KEY_SET_VALUE
).WriteValue(
830 GoogleUpdateSettings::kUpdatePolicyValue
,
831 static_cast<DWORD
>(1)));
832 EXPECT_EQ(ERROR_SUCCESS
,
833 RegKey(HKEY_LOCAL_MACHINE
, GoogleUpdateSettings::kPoliciesKey
,
834 KEY_SET_VALUE
).WriteValue(app_policy_value
.c_str(),
835 static_cast<DWORD
>(0)));
836 bool is_overridden
= false;
837 EXPECT_EQ(GoogleUpdateSettings::UPDATES_DISABLED
,
838 GoogleUpdateSettings::GetAppUpdatePolicy(kTestProductGuid
,
840 EXPECT_TRUE(is_overridden
);
842 EXPECT_EQ(ERROR_SUCCESS
,
843 RegKey(HKEY_LOCAL_MACHINE
, GoogleUpdateSettings::kPoliciesKey
,
844 KEY_SET_VALUE
).WriteValue(
845 GoogleUpdateSettings::kUpdatePolicyValue
,
846 static_cast<DWORD
>(0)));
847 EXPECT_EQ(ERROR_SUCCESS
,
848 RegKey(HKEY_LOCAL_MACHINE
, GoogleUpdateSettings::kPoliciesKey
,
849 KEY_SET_VALUE
).WriteValue(app_policy_value
.c_str(),
850 static_cast<DWORD
>(1)));
851 is_overridden
= false;
852 EXPECT_EQ(GoogleUpdateSettings::AUTOMATIC_UPDATES
,
853 GoogleUpdateSettings::GetAppUpdatePolicy(kTestProductGuid
,
855 EXPECT_TRUE(is_overridden
);
857 EXPECT_EQ(ERROR_SUCCESS
,
858 RegKey(HKEY_LOCAL_MACHINE
, GoogleUpdateSettings::kPoliciesKey
,
859 KEY_SET_VALUE
).WriteValue(app_policy_value
.c_str(),
860 static_cast<DWORD
>(2)));
861 is_overridden
= false;
862 EXPECT_EQ(GoogleUpdateSettings::MANUAL_UPDATES_ONLY
,
863 GoogleUpdateSettings::GetAppUpdatePolicy(kTestProductGuid
,
865 EXPECT_TRUE(is_overridden
);
867 EXPECT_EQ(ERROR_SUCCESS
,
868 RegKey(HKEY_LOCAL_MACHINE
, GoogleUpdateSettings::kPoliciesKey
,
869 KEY_SET_VALUE
).WriteValue(app_policy_value
.c_str(),
870 static_cast<DWORD
>(3)));
871 is_overridden
= false;
872 EXPECT_EQ(GoogleUpdateSettings::AUTO_UPDATES_ONLY
,
873 GoogleUpdateSettings::GetAppUpdatePolicy(kTestProductGuid
,
875 EXPECT_TRUE(is_overridden
);
877 // The default policy should be in force for bogus values.
878 EXPECT_EQ(ERROR_SUCCESS
,
879 RegKey(HKEY_LOCAL_MACHINE
, GoogleUpdateSettings::kPoliciesKey
,
880 KEY_SET_VALUE
).WriteValue(app_policy_value
.c_str(),
881 static_cast<DWORD
>(4)));
882 is_overridden
= true;
883 EXPECT_EQ(GoogleUpdateSettings::UPDATES_DISABLED
,
884 GoogleUpdateSettings::GetAppUpdatePolicy(kTestProductGuid
,
886 EXPECT_FALSE(is_overridden
);
889 TEST_F(GoogleUpdateSettingsTest
, PerAppUpdatesDisabledByPolicy
) {
890 BrowserDistribution
* dist
= BrowserDistribution::GetDistribution();
892 SetUpdatePolicyForAppGuid(dist
->GetAppGuid(),
893 GoogleUpdateSettings::UPDATES_DISABLED
));
894 bool is_overridden
= false;
895 GoogleUpdateSettings::UpdatePolicy update_policy
=
896 GoogleUpdateSettings::GetAppUpdatePolicy(dist
->GetAppGuid(),
898 EXPECT_TRUE(is_overridden
);
899 EXPECT_EQ(GoogleUpdateSettings::UPDATES_DISABLED
, update_policy
);
900 EXPECT_FALSE(GoogleUpdateSettings::AreAutoupdatesEnabled());
902 EXPECT_TRUE(GoogleUpdateSettings::ReenableAutoupdates());
903 update_policy
= GoogleUpdateSettings::GetAppUpdatePolicy(dist
->GetAppGuid(),
905 // Should still have a policy but now that policy should explicitly enable
907 EXPECT_TRUE(is_overridden
);
908 EXPECT_EQ(GoogleUpdateSettings::AUTOMATIC_UPDATES
, update_policy
);
909 EXPECT_TRUE(GoogleUpdateSettings::AreAutoupdatesEnabled());
912 TEST_F(GoogleUpdateSettingsTest
, PerAppUpdatesEnabledWithGlobalDisabled
) {
913 // Disable updates globally but enable them for Chrome (the app-specific
914 // setting should take precedence).
915 BrowserDistribution
* dist
= BrowserDistribution::GetDistribution();
916 BrowserDistribution
* binaries
= BrowserDistribution::GetSpecificDistribution(
917 BrowserDistribution::CHROME_BINARIES
);
919 SetUpdatePolicyForAppGuid(dist
->GetAppGuid(),
920 GoogleUpdateSettings::AUTOMATIC_UPDATES
));
922 SetUpdatePolicyForAppGuid(binaries
->GetAppGuid(),
923 GoogleUpdateSettings::AUTOMATIC_UPDATES
));
924 EXPECT_TRUE(SetGlobalUpdatePolicy(GoogleUpdateSettings::UPDATES_DISABLED
));
926 // Make sure we read this as still having updates enabled.
927 EXPECT_TRUE(GoogleUpdateSettings::AreAutoupdatesEnabled());
929 // Make sure that the reset action returns true and is a no-op.
930 EXPECT_TRUE(GoogleUpdateSettings::ReenableAutoupdates());
931 EXPECT_EQ(GoogleUpdateSettings::AUTOMATIC_UPDATES
,
932 GetUpdatePolicyForAppGuid(dist
->GetAppGuid()));
933 EXPECT_EQ(GoogleUpdateSettings::AUTOMATIC_UPDATES
,
934 GetUpdatePolicyForAppGuid(binaries
->GetAppGuid()));
935 EXPECT_EQ(GoogleUpdateSettings::UPDATES_DISABLED
, GetGlobalUpdatePolicy());
938 TEST_F(GoogleUpdateSettingsTest
, GlobalUpdatesDisabledByPolicy
) {
939 BrowserDistribution
* dist
= BrowserDistribution::GetDistribution();
940 EXPECT_TRUE(SetGlobalUpdatePolicy(GoogleUpdateSettings::UPDATES_DISABLED
));
941 bool is_overridden
= false;
943 // The contract for GetAppUpdatePolicy states that |is_overridden| should be
944 // set to false when updates are disabled on a non-app-specific basis.
945 GoogleUpdateSettings::UpdatePolicy update_policy
=
946 GoogleUpdateSettings::GetAppUpdatePolicy(dist
->GetAppGuid(),
948 EXPECT_FALSE(is_overridden
);
949 EXPECT_EQ(GoogleUpdateSettings::UPDATES_DISABLED
, update_policy
);
950 EXPECT_FALSE(GoogleUpdateSettings::AreAutoupdatesEnabled());
952 EXPECT_TRUE(GoogleUpdateSettings::ReenableAutoupdates());
953 update_policy
= GoogleUpdateSettings::GetAppUpdatePolicy(dist
->GetAppGuid(),
955 // Policy should now be to enable updates, |is_overridden| should still be
957 EXPECT_FALSE(is_overridden
);
958 EXPECT_EQ(GoogleUpdateSettings::AUTOMATIC_UPDATES
, update_policy
);
959 EXPECT_TRUE(GoogleUpdateSettings::AreAutoupdatesEnabled());
962 TEST_F(GoogleUpdateSettingsTest
, UpdatesDisabledByTimeout
) {
963 // Disable updates altogether.
964 EXPECT_TRUE(SetUpdateTimeoutOverride(0));
965 EXPECT_FALSE(GoogleUpdateSettings::AreAutoupdatesEnabled());
966 EXPECT_TRUE(GoogleUpdateSettings::ReenableAutoupdates());
967 EXPECT_TRUE(GoogleUpdateSettings::AreAutoupdatesEnabled());
969 // Set the update period to something unreasonable.
970 EXPECT_TRUE(SetUpdateTimeoutOverride(
971 GoogleUpdateSettings::kCheckPeriodOverrideMinutesMax
+ 1));
972 EXPECT_FALSE(GoogleUpdateSettings::AreAutoupdatesEnabled());
973 EXPECT_TRUE(GoogleUpdateSettings::ReenableAutoupdates());
974 EXPECT_TRUE(GoogleUpdateSettings::AreAutoupdatesEnabled());
977 TEST_F(GoogleUpdateSettingsTest
, ExperimentsLabelHelperSystem
) {
978 TestExperimentsLabelHelper(SYSTEM_INSTALL
);
981 TEST_F(GoogleUpdateSettingsTest
, ExperimentsLabelHelperUser
) {
982 TestExperimentsLabelHelper(USER_INSTALL
);
985 #endif // defined(GOOGLE_CHROME_BUILD)
987 // Test GoogleUpdateSettings::GetUninstallCommandLine at system- or user-level,
988 // according to the param.
989 class GetUninstallCommandLine
: public GoogleUpdateSettingsTest
,
990 public testing::WithParamInterface
<bool> {
992 static const wchar_t kDummyCommand
[];
994 void SetUp() override
{
995 GoogleUpdateSettingsTest::SetUp();
996 system_install_
= GetParam();
997 root_key_
= system_install_
? HKEY_LOCAL_MACHINE
: HKEY_CURRENT_USER
;
1001 bool system_install_
;
1004 const wchar_t GetUninstallCommandLine::kDummyCommand
[] =
1005 L
"\"goopdate.exe\" /spam";
1007 // Tests that GetUninstallCommandLine returns an empty string if there's no
1008 // Software\Google\Update key.
1009 TEST_P(GetUninstallCommandLine
, TestNoKey
) {
1010 EXPECT_EQ(base::string16(),
1011 GoogleUpdateSettings::GetUninstallCommandLine(system_install_
));
1014 // Tests that GetUninstallCommandLine returns an empty string if there's no
1015 // UninstallCmdLine value in the Software\Google\Update key.
1016 TEST_P(GetUninstallCommandLine
, TestNoValue
) {
1017 RegKey(root_key_
, google_update::kRegPathGoogleUpdate
, KEY_SET_VALUE
);
1018 EXPECT_EQ(base::string16(),
1019 GoogleUpdateSettings::GetUninstallCommandLine(system_install_
));
1022 // Tests that GetUninstallCommandLine returns an empty string if there's an
1023 // empty UninstallCmdLine value in the Software\Google\Update key.
1024 TEST_P(GetUninstallCommandLine
, TestEmptyValue
) {
1025 RegKey(root_key_
, google_update::kRegPathGoogleUpdate
, KEY_SET_VALUE
)
1026 .WriteValue(google_update::kRegUninstallCmdLine
, L
"");
1027 EXPECT_EQ(base::string16(),
1028 GoogleUpdateSettings::GetUninstallCommandLine(system_install_
));
1031 // Tests that GetUninstallCommandLine returns the correct string if there's an
1032 // UninstallCmdLine value in the Software\Google\Update key.
1033 TEST_P(GetUninstallCommandLine
, TestRealValue
) {
1034 RegKey(root_key_
, google_update::kRegPathGoogleUpdate
, KEY_SET_VALUE
)
1035 .WriteValue(google_update::kRegUninstallCmdLine
, kDummyCommand
);
1036 EXPECT_EQ(base::string16(kDummyCommand
),
1037 GoogleUpdateSettings::GetUninstallCommandLine(system_install_
));
1038 // Make sure that there's no value in the other level (user or system).
1039 EXPECT_EQ(base::string16(),
1040 GoogleUpdateSettings::GetUninstallCommandLine(!system_install_
));
1043 INSTANTIATE_TEST_CASE_P(GetUninstallCommandLineAtLevel
, GetUninstallCommandLine
,
1046 // Test GoogleUpdateSettings::GetGoogleUpdateVersion at system- or user-level,
1047 // according to the param.
1048 class GetGoogleUpdateVersion
: public GoogleUpdateSettingsTest
,
1049 public testing::WithParamInterface
<bool> {
1051 static const wchar_t kDummyVersion
[];
1053 void SetUp() override
{
1054 GoogleUpdateSettingsTest::SetUp();
1055 system_install_
= GetParam();
1056 root_key_
= system_install_
? HKEY_LOCAL_MACHINE
: HKEY_CURRENT_USER
;
1060 bool system_install_
;
1063 const wchar_t GetGoogleUpdateVersion::kDummyVersion
[] = L
"1.2.3.4";
1065 // Tests that GetGoogleUpdateVersion returns an empty string if there's no
1066 // Software\Google\Update key.
1067 TEST_P(GetGoogleUpdateVersion
, TestNoKey
) {
1069 GoogleUpdateSettings::GetGoogleUpdateVersion(system_install_
).IsValid());
1072 // Tests that GetGoogleUpdateVersion returns an empty string if there's no
1073 // version value in the Software\Google\Update key.
1074 TEST_P(GetGoogleUpdateVersion
, TestNoValue
) {
1075 RegKey(root_key_
, google_update::kRegPathGoogleUpdate
, KEY_SET_VALUE
);
1077 GoogleUpdateSettings::GetGoogleUpdateVersion(system_install_
).IsValid());
1080 // Tests that GetGoogleUpdateVersion returns an empty string if there's an
1081 // empty version value in the Software\Google\Update key.
1082 TEST_P(GetGoogleUpdateVersion
, TestEmptyValue
) {
1083 RegKey(root_key_
, google_update::kRegPathGoogleUpdate
, KEY_SET_VALUE
)
1084 .WriteValue(google_update::kRegGoogleUpdateVersion
, L
"");
1086 GoogleUpdateSettings::GetGoogleUpdateVersion(system_install_
).IsValid());
1089 // Tests that GetGoogleUpdateVersion returns the correct string if there's a
1090 // version value in the Software\Google\Update key.
1091 TEST_P(GetGoogleUpdateVersion
, TestRealValue
) {
1092 RegKey(root_key_
, google_update::kRegPathGoogleUpdate
, KEY_SET_VALUE
)
1093 .WriteValue(google_update::kRegGoogleUpdateVersion
, kDummyVersion
);
1094 Version
expected(base::UTF16ToUTF8(kDummyVersion
));
1095 EXPECT_TRUE(expected
.Equals(
1096 GoogleUpdateSettings::GetGoogleUpdateVersion(system_install_
)));
1097 // Make sure that there's no value in the other level (user or system).
1099 GoogleUpdateSettings::GetGoogleUpdateVersion(!system_install_
)
1103 INSTANTIATE_TEST_CASE_P(GetGoogleUpdateVersionAtLevel
, GetGoogleUpdateVersion
,
1106 // Test values for use by the CollectStatsConsent test fixture.
1118 struct UserLevelState
{};
1119 struct SystemLevelState
{};
1120 static const UserLevelState kUserLevel
;
1121 static const SystemLevelState kSystemLevel
;
1123 StatsState(const UserLevelState
&,
1124 InstallType install_type
,
1125 StateSetting state_value
)
1126 : system_level_(false),
1127 multi_install_(install_type
== MULTI_INSTALL
),
1128 state_value_(state_value
),
1129 state_medium_value_(NO_SETTING
) {
1131 StatsState(const SystemLevelState
&,
1132 InstallType install_type
,
1133 StateSetting state_value
,
1134 StateSetting state_medium_value
)
1135 : system_level_(true),
1136 multi_install_(install_type
== MULTI_INSTALL
),
1137 state_value_(state_value
),
1138 state_medium_value_(state_medium_value
) {
1140 bool system_level() const { return system_level_
; }
1141 bool multi_install() const { return multi_install_
; }
1142 HKEY
root_key() const {
1143 return system_level_
? HKEY_LOCAL_MACHINE
: HKEY_CURRENT_USER
;
1145 StateSetting
state_value() const { return state_value_
; }
1146 StateSetting
state_medium_value() const {
1147 return state_medium_value_
;
1149 bool is_consent_granted() const {
1150 return (system_level_
&& state_medium_value_
!= NO_SETTING
) ?
1151 (state_medium_value_
== TRUE_SETTING
) :
1152 (state_value_
== TRUE_SETTING
);
1157 bool multi_install_
;
1158 StateSetting state_value_
;
1159 StateSetting state_medium_value_
;
1162 const StatsState::UserLevelState
StatsState::kUserLevel
= {};
1163 const StatsState::SystemLevelState
StatsState::kSystemLevel
= {};
1165 // A value parameterized test for testing the stats collection consent setting.
1166 class CollectStatsConsent
: public ::testing::TestWithParam
<StatsState
> {
1168 static void SetUpTestCase();
1169 static void TearDownTestCase();
1171 void SetUp() override
;
1172 static void MakeChromeMultiInstall(HKEY root_key
);
1173 static void ApplySetting(StatsState::StateSetting setting
,
1175 const base::string16
& reg_key
);
1177 static base::string16
* chrome_version_key_
;
1178 static base::string16
* chrome_state_key_
;
1179 static base::string16
* chrome_state_medium_key_
;
1180 static base::string16
* binaries_state_key_
;
1181 static base::string16
* binaries_state_medium_key_
;
1182 registry_util::RegistryOverrideManager override_manager_
;
1185 base::string16
* CollectStatsConsent::chrome_version_key_
;
1186 base::string16
* CollectStatsConsent::chrome_state_key_
;
1187 base::string16
* CollectStatsConsent::chrome_state_medium_key_
;
1188 base::string16
* CollectStatsConsent::binaries_state_key_
;
1189 base::string16
* CollectStatsConsent::binaries_state_medium_key_
;
1191 void CollectStatsConsent::SetUpTestCase() {
1192 BrowserDistribution
* dist
=
1193 BrowserDistribution::GetSpecificDistribution(
1194 BrowserDistribution::CHROME_BROWSER
);
1195 chrome_version_key_
= new base::string16(dist
->GetVersionKey());
1196 chrome_state_key_
= new base::string16(dist
->GetStateKey());
1197 chrome_state_medium_key_
= new base::string16(dist
->GetStateMediumKey());
1199 dist
= BrowserDistribution::GetSpecificDistribution(
1200 BrowserDistribution::CHROME_BINARIES
);
1201 binaries_state_key_
= new base::string16(dist
->GetStateKey());
1202 binaries_state_medium_key_
= new base::string16(dist
->GetStateMediumKey());
1205 void CollectStatsConsent::TearDownTestCase() {
1206 delete chrome_version_key_
;
1207 delete chrome_state_key_
;
1208 delete chrome_state_medium_key_
;
1209 delete binaries_state_key_
;
1210 delete binaries_state_medium_key_
;
1213 // Install the registry override and apply the settings to the registry.
1214 void CollectStatsConsent::SetUp() {
1215 const StatsState
& stats_state
= GetParam();
1216 const HKEY root_key
= stats_state
.root_key();
1217 base::string16
reg_temp_name(
1218 stats_state
.system_level() ? L
"HKLM_" : L
"HKCU_");
1219 reg_temp_name
+= L
"CollectStatsConsent";
1220 override_manager_
.OverrideRegistry(root_key
);
1222 if (stats_state
.multi_install()) {
1223 MakeChromeMultiInstall(root_key
);
1224 ApplySetting(stats_state
.state_value(), root_key
, *binaries_state_key_
);
1225 ApplySetting(stats_state
.state_medium_value(), root_key
,
1226 *binaries_state_medium_key_
);
1228 ApplySetting(stats_state
.state_value(), root_key
, *chrome_state_key_
);
1229 ApplySetting(stats_state
.state_medium_value(), root_key
,
1230 *chrome_state_medium_key_
);
1234 // Write values into the registry so that Chrome is considered to be installed
1235 // as multi-install.
1236 void CollectStatsConsent::MakeChromeMultiInstall(HKEY root_key
) {
1239 RegKey(root_key
, chrome_version_key_
->c_str(),
1240 KEY_SET_VALUE
).WriteValue(google_update::kRegVersionField
,
1244 RegKey(root_key
, chrome_state_key_
->c_str(),
1245 KEY_SET_VALUE
).WriteValue(installer::kUninstallArgumentsField
,
1246 L
"--multi-install"));
1249 // Write the correct value to represent |setting| in the registry.
1250 void CollectStatsConsent::ApplySetting(StatsState::StateSetting setting
,
1252 const base::string16
& reg_key
) {
1253 if (setting
!= StatsState::NO_SETTING
) {
1254 DWORD value
= setting
!= StatsState::FALSE_SETTING
? 1 : 0;
1257 RegKey(root_key
, reg_key
.c_str(),
1258 KEY_SET_VALUE
).WriteValue(google_update::kRegUsageStatsField
,
1263 // Test that stats consent can be read.
1264 TEST_P(CollectStatsConsent
, GetCollectStatsConsentAtLevel
) {
1265 if (GetParam().is_consent_granted()) {
1266 EXPECT_TRUE(GoogleUpdateSettings::GetCollectStatsConsentAtLevel(
1267 GetParam().system_level()));
1269 EXPECT_FALSE(GoogleUpdateSettings::GetCollectStatsConsentAtLevel(
1270 GetParam().system_level()));
1274 // Test that stats consent can be flipped to the opposite setting, that the new
1275 // setting takes affect, and that the correct registry location is modified.
1276 TEST_P(CollectStatsConsent
, SetCollectStatsConsentAtLevel
) {
1277 EXPECT_TRUE(GoogleUpdateSettings::SetCollectStatsConsentAtLevel(
1278 GetParam().system_level(),
1279 !GetParam().is_consent_granted()));
1280 const base::string16
* const reg_keys
[] = {
1282 chrome_state_medium_key_
,
1283 binaries_state_key_
,
1284 binaries_state_medium_key_
,
1286 int key_index
= ((GetParam().system_level() ? 1 : 0) +
1287 (GetParam().multi_install() ? 2 : 0));
1288 const base::string16
& reg_key
= *reg_keys
[key_index
];
1292 RegKey(GetParam().root_key(), reg_key
.c_str(),
1293 KEY_QUERY_VALUE
).ReadValueDW(google_update::kRegUsageStatsField
,
1295 if (GetParam().is_consent_granted()) {
1296 EXPECT_FALSE(GoogleUpdateSettings::GetCollectStatsConsentAtLevel(
1297 GetParam().system_level()));
1298 EXPECT_EQ(0UL, value
);
1300 EXPECT_TRUE(GoogleUpdateSettings::GetCollectStatsConsentAtLevel(
1301 GetParam().system_level()));
1302 EXPECT_EQ(1UL, value
);
1306 INSTANTIATE_TEST_CASE_P(
1307 UserLevelSingleInstall
,
1308 CollectStatsConsent
,
1310 StatsState(StatsState::kUserLevel
, StatsState::SINGLE_INSTALL
,
1311 StatsState::NO_SETTING
),
1312 StatsState(StatsState::kUserLevel
, StatsState::SINGLE_INSTALL
,
1313 StatsState::FALSE_SETTING
),
1314 StatsState(StatsState::kUserLevel
, StatsState::SINGLE_INSTALL
,
1315 StatsState::TRUE_SETTING
)));
1316 INSTANTIATE_TEST_CASE_P(
1317 UserLevelMultiInstall
,
1318 CollectStatsConsent
,
1320 StatsState(StatsState::kUserLevel
, StatsState::MULTI_INSTALL
,
1321 StatsState::NO_SETTING
),
1322 StatsState(StatsState::kUserLevel
, StatsState::MULTI_INSTALL
,
1323 StatsState::FALSE_SETTING
),
1324 StatsState(StatsState::kUserLevel
, StatsState::MULTI_INSTALL
,
1325 StatsState::TRUE_SETTING
)));
1326 INSTANTIATE_TEST_CASE_P(
1327 SystemLevelSingleInstall
,
1328 CollectStatsConsent
,
1330 StatsState(StatsState::kSystemLevel
, StatsState::SINGLE_INSTALL
,
1331 StatsState::NO_SETTING
, StatsState::NO_SETTING
),
1332 StatsState(StatsState::kSystemLevel
, StatsState::SINGLE_INSTALL
,
1333 StatsState::NO_SETTING
, StatsState::FALSE_SETTING
),
1334 StatsState(StatsState::kSystemLevel
, StatsState::SINGLE_INSTALL
,
1335 StatsState::NO_SETTING
, StatsState::TRUE_SETTING
),
1336 StatsState(StatsState::kSystemLevel
, StatsState::SINGLE_INSTALL
,
1337 StatsState::FALSE_SETTING
, StatsState::NO_SETTING
),
1338 StatsState(StatsState::kSystemLevel
, StatsState::SINGLE_INSTALL
,
1339 StatsState::FALSE_SETTING
, StatsState::FALSE_SETTING
),
1340 StatsState(StatsState::kSystemLevel
, StatsState::SINGLE_INSTALL
,
1341 StatsState::FALSE_SETTING
, StatsState::TRUE_SETTING
),
1342 StatsState(StatsState::kSystemLevel
, StatsState::SINGLE_INSTALL
,
1343 StatsState::TRUE_SETTING
, StatsState::NO_SETTING
),
1344 StatsState(StatsState::kSystemLevel
, StatsState::SINGLE_INSTALL
,
1345 StatsState::TRUE_SETTING
, StatsState::FALSE_SETTING
),
1346 StatsState(StatsState::kSystemLevel
, StatsState::SINGLE_INSTALL
,
1347 StatsState::TRUE_SETTING
, StatsState::TRUE_SETTING
)));
1348 INSTANTIATE_TEST_CASE_P(
1349 SystemLevelMultiInstall
,
1350 CollectStatsConsent
,
1352 StatsState(StatsState::kSystemLevel
, StatsState::MULTI_INSTALL
,
1353 StatsState::NO_SETTING
, StatsState::NO_SETTING
),
1354 StatsState(StatsState::kSystemLevel
, StatsState::MULTI_INSTALL
,
1355 StatsState::NO_SETTING
, StatsState::FALSE_SETTING
),
1356 StatsState(StatsState::kSystemLevel
, StatsState::MULTI_INSTALL
,
1357 StatsState::NO_SETTING
, StatsState::TRUE_SETTING
),
1358 StatsState(StatsState::kSystemLevel
, StatsState::MULTI_INSTALL
,
1359 StatsState::FALSE_SETTING
, StatsState::NO_SETTING
),
1360 StatsState(StatsState::kSystemLevel
, StatsState::MULTI_INSTALL
,
1361 StatsState::FALSE_SETTING
, StatsState::FALSE_SETTING
),
1362 StatsState(StatsState::kSystemLevel
, StatsState::MULTI_INSTALL
,
1363 StatsState::FALSE_SETTING
, StatsState::TRUE_SETTING
),
1364 StatsState(StatsState::kSystemLevel
, StatsState::MULTI_INSTALL
,
1365 StatsState::TRUE_SETTING
, StatsState::NO_SETTING
),
1366 StatsState(StatsState::kSystemLevel
, StatsState::MULTI_INSTALL
,
1367 StatsState::TRUE_SETTING
, StatsState::FALSE_SETTING
),
1368 StatsState(StatsState::kSystemLevel
, StatsState::MULTI_INSTALL
,
1369 StatsState::TRUE_SETTING
, StatsState::TRUE_SETTING
)));