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/util_constants.h"
25 #include "chrome/installer/util/work_item_list.h"
26 #include "testing/gtest/include/gtest/gtest.h"
28 using base::win::RegKey
;
29 using installer::ChannelInfo
;
33 const wchar_t kTestProductGuid
[] = L
"{89F1B351-B15D-48D4-8F10-1298721CF13D}";
35 #if defined(GOOGLE_CHROME_BUILD)
36 const wchar_t kTestExperimentLabel
[] = L
"test_label_value";
39 // This test fixture redirects the HKLM and HKCU registry hives for
40 // the duration of the test to make it independent of the machine
42 class GoogleUpdateSettingsTest
: public testing::Test
{
44 enum SystemUserInstall
{
49 GoogleUpdateSettingsTest()
50 : program_files_override_(base::DIR_PROGRAM_FILES
),
51 program_files_x86_override_(base::DIR_PROGRAM_FILESX86
) {
52 registry_overrides_
.OverrideRegistry(HKEY_LOCAL_MACHINE
);
53 registry_overrides_
.OverrideRegistry(HKEY_CURRENT_USER
);
56 void SetApField(SystemUserInstall is_system
, const wchar_t* value
) {
57 HKEY root
= is_system
== SYSTEM_INSTALL
?
58 HKEY_LOCAL_MACHINE
: HKEY_CURRENT_USER
;
61 BrowserDistribution
* dist
= BrowserDistribution::GetDistribution();
62 base::string16 path
= dist
->GetStateKey();
63 ASSERT_EQ(ERROR_SUCCESS
, update_key
.Create(root
, path
.c_str(), KEY_WRITE
));
64 ASSERT_EQ(ERROR_SUCCESS
, update_key
.WriteValue(L
"ap", value
));
67 // Sets the "ap" field for a multi-install product (both the product and
69 void SetMultiApField(SystemUserInstall is_system
, const wchar_t* value
) {
70 // Caller must specify a multi-install ap value.
71 ASSERT_NE(base::string16::npos
, base::string16(value
).find(L
"-multi"));
72 HKEY root
= is_system
== SYSTEM_INSTALL
?
73 HKEY_LOCAL_MACHINE
: HKEY_CURRENT_USER
;
76 // Write the ap value for both the product and the binaries.
77 BrowserDistribution
* const kDists
[] = {
78 BrowserDistribution::GetDistribution(),
79 BrowserDistribution::GetSpecificDistribution(
80 BrowserDistribution::CHROME_BINARIES
)
82 for (size_t i
= 0; i
< arraysize(kDists
); ++i
) {
83 base::string16 path
= kDists
[i
]->GetStateKey();
84 ASSERT_EQ(ERROR_SUCCESS
, update_key
.Create(root
, path
.c_str(),
86 ASSERT_EQ(ERROR_SUCCESS
, update_key
.WriteValue(L
"ap", value
));
89 // Make the product technically multi-install.
90 BrowserDistribution
* dist
= BrowserDistribution::GetDistribution();
91 ASSERT_EQ(ERROR_SUCCESS
,
92 update_key
.Create(root
, dist
->GetStateKey().c_str(), KEY_WRITE
));
93 ASSERT_EQ(ERROR_SUCCESS
,
94 update_key
.WriteValue(installer::kUninstallArgumentsField
,
98 // Tests setting the ap= value to various combinations of values with
99 // prefixes and suffixes, while asserting on the correct channel value.
100 // Note that any non-empty ap= value that doesn't match ".*-{dev|beta}.*"
101 // will return the "unknown" channel.
102 void TestCurrentChromeChannelWithVariousApValues(SystemUserInstall install
) {
103 static struct Expectations
{
104 const wchar_t* ap_value
;
105 const wchar_t* channel
;
107 { L
"dev", installer::kChromeChannelDev
},
108 { L
"-dev", installer::kChromeChannelDev
},
109 { L
"-developer", installer::kChromeChannelDev
},
110 { L
"beta", installer::kChromeChannelBeta
},
111 { L
"-beta", installer::kChromeChannelBeta
},
112 { L
"-betamax", installer::kChromeChannelBeta
},
114 bool is_system
= install
== SYSTEM_INSTALL
;
115 const wchar_t* prefixes
[] = {
120 const wchar_t* suffixes
[] = {
126 for (size_t i
= 0; i
< arraysize(prefixes
); ++i
) {
127 for (size_t j
= 0; j
< arraysize(expectations
); ++j
) {
128 for (size_t k
= 0; k
< arraysize(suffixes
); ++k
) {
129 base::string16 ap
= prefixes
[i
];
130 ap
+= expectations
[j
].ap_value
;
132 const wchar_t* channel
= expectations
[j
].channel
;
134 SetApField(install
, ap
.c_str());
135 base::string16 ret_channel
;
137 EXPECT_TRUE(GoogleUpdateSettings::GetChromeChannelAndModifiers(
138 is_system
, &ret_channel
));
139 EXPECT_STREQ(channel
, ret_channel
.c_str())
140 << "Expecting channel \"" << channel
141 << "\" for ap=\"" << ap
<< "\"";
147 // Test the writing and deleting functionality of the experiments label
149 void TestExperimentsLabelHelper(SystemUserInstall install
) {
150 BrowserDistribution
* chrome
=
151 BrowserDistribution::GetSpecificDistribution(
152 BrowserDistribution::CHROME_BROWSER
);
153 base::string16 value
;
154 #if defined(GOOGLE_CHROME_BUILD)
155 EXPECT_TRUE(chrome
->ShouldSetExperimentLabels());
157 // Before anything is set, ReadExperimentLabels should succeed but return
159 EXPECT_TRUE(GoogleUpdateSettings::ReadExperimentLabels(
160 install
== SYSTEM_INSTALL
, &value
));
161 EXPECT_EQ(base::string16(), value
);
163 EXPECT_TRUE(GoogleUpdateSettings::SetExperimentLabels(
164 install
== SYSTEM_INSTALL
, kTestExperimentLabel
));
166 // Validate that something is written. Only worry about the label itself.
168 HKEY root
= install
== SYSTEM_INSTALL
?
169 HKEY_LOCAL_MACHINE
: HKEY_CURRENT_USER
;
170 base::string16 state_key
= install
== SYSTEM_INSTALL
?
171 chrome
->GetStateMediumKey() : chrome
->GetStateKey();
173 EXPECT_EQ(ERROR_SUCCESS
,
174 key
.Open(root
, state_key
.c_str(), KEY_QUERY_VALUE
));
175 EXPECT_EQ(ERROR_SUCCESS
,
176 key
.ReadValue(google_update::kExperimentLabels
, &value
));
177 EXPECT_EQ(kTestExperimentLabel
, value
);
178 EXPECT_TRUE(GoogleUpdateSettings::ReadExperimentLabels(
179 install
== SYSTEM_INSTALL
, &value
));
180 EXPECT_EQ(kTestExperimentLabel
, value
);
183 // Now that the label is set, test the delete functionality. An empty label
184 // should result in deleting the value.
185 EXPECT_TRUE(GoogleUpdateSettings::SetExperimentLabels(
186 install
== SYSTEM_INSTALL
, base::string16()));
187 EXPECT_EQ(ERROR_SUCCESS
,
188 key
.Open(root
, state_key
.c_str(), KEY_QUERY_VALUE
));
189 EXPECT_EQ(ERROR_FILE_NOT_FOUND
,
190 key
.ReadValue(google_update::kExperimentLabels
, &value
));
191 EXPECT_TRUE(GoogleUpdateSettings::ReadExperimentLabels(
192 install
== SYSTEM_INSTALL
, &value
));
193 EXPECT_EQ(base::string16(), value
);
196 EXPECT_FALSE(chrome
->ShouldSetExperimentLabels());
197 EXPECT_FALSE(GoogleUpdateSettings::ReadExperimentLabels(
198 install
== SYSTEM_INSTALL
, &value
));
199 #endif // GOOGLE_CHROME_BUILD
202 // Creates "ap" key with the value given as parameter. Also adds work
203 // items to work_item_list given so that they can be rolled back later.
204 bool CreateApKey(WorkItemList
* work_item_list
, const base::string16
& value
) {
205 HKEY reg_root
= HKEY_CURRENT_USER
;
206 base::string16 reg_key
= GetApKeyPath();
207 work_item_list
->AddCreateRegKeyWorkItem(
208 reg_root
, reg_key
, WorkItem::kWow64Default
);
209 work_item_list
->AddSetRegValueWorkItem(reg_root
,
211 WorkItem::kWow64Default
,
212 google_update::kRegApField
,
215 if (!work_item_list
->Do()) {
216 work_item_list
->Rollback();
222 // Returns the key path of "ap" key, e.g.:
223 // Google\Update\ClientState\<kTestProductGuid>
224 base::string16
GetApKeyPath() {
225 base::string16
reg_key(google_update::kRegPathClientState
);
226 reg_key
.append(L
"\\");
227 reg_key
.append(kTestProductGuid
);
231 // Utility method to read "ap" key value
232 base::string16
ReadApKeyValue() {
234 base::string16 ap_key_value
;
235 base::string16 reg_key
= GetApKeyPath();
236 if (key
.Open(HKEY_CURRENT_USER
, reg_key
.c_str(), KEY_ALL_ACCESS
) ==
238 key
.ReadValue(google_update::kRegApField
, &ap_key_value
);
244 bool SetUpdatePolicyForAppGuid(const base::string16
& app_guid
,
245 GoogleUpdateSettings::UpdatePolicy policy
) {
247 if (policy_key
.Create(HKEY_LOCAL_MACHINE
,
248 GoogleUpdateSettings::kPoliciesKey
,
249 KEY_SET_VALUE
) == ERROR_SUCCESS
) {
250 base::string16
app_update_override(
251 GoogleUpdateSettings::kUpdateOverrideValuePrefix
);
252 app_update_override
.append(app_guid
);
253 return policy_key
.WriteValue(app_update_override
.c_str(),
254 static_cast<DWORD
>(policy
)) == ERROR_SUCCESS
;
259 GoogleUpdateSettings::UpdatePolicy
GetUpdatePolicyForAppGuid(
260 const base::string16
& app_guid
) {
262 if (policy_key
.Create(HKEY_LOCAL_MACHINE
,
263 GoogleUpdateSettings::kPoliciesKey
,
264 KEY_QUERY_VALUE
) == ERROR_SUCCESS
) {
265 base::string16
app_update_override(
266 GoogleUpdateSettings::kUpdateOverrideValuePrefix
);
267 app_update_override
.append(app_guid
);
270 if (policy_key
.ReadValueDW(app_update_override
.c_str(),
271 &value
) == ERROR_SUCCESS
) {
272 return static_cast<GoogleUpdateSettings::UpdatePolicy
>(value
);
275 return GoogleUpdateSettings::UPDATE_POLICIES_COUNT
;
278 bool SetGlobalUpdatePolicy(GoogleUpdateSettings::UpdatePolicy policy
) {
280 return policy_key
.Create(HKEY_LOCAL_MACHINE
,
281 GoogleUpdateSettings::kPoliciesKey
,
282 KEY_SET_VALUE
) == ERROR_SUCCESS
&&
283 policy_key
.WriteValue(GoogleUpdateSettings::kUpdatePolicyValue
,
284 static_cast<DWORD
>(policy
)) == ERROR_SUCCESS
;
287 GoogleUpdateSettings::UpdatePolicy
GetGlobalUpdatePolicy() {
290 return (policy_key
.Create(HKEY_LOCAL_MACHINE
,
291 GoogleUpdateSettings::kPoliciesKey
,
292 KEY_QUERY_VALUE
) == ERROR_SUCCESS
&&
293 policy_key
.ReadValueDW(GoogleUpdateSettings::kUpdatePolicyValue
,
294 &value
) == ERROR_SUCCESS
) ?
295 static_cast<GoogleUpdateSettings::UpdatePolicy
>(value
) :
296 GoogleUpdateSettings::UPDATE_POLICIES_COUNT
;
299 bool SetUpdateTimeoutOverride(DWORD time_in_minutes
) {
301 return policy_key
.Create(HKEY_LOCAL_MACHINE
,
302 GoogleUpdateSettings::kPoliciesKey
,
303 KEY_SET_VALUE
) == ERROR_SUCCESS
&&
304 policy_key
.WriteValue(
305 GoogleUpdateSettings::kCheckPeriodOverrideMinutes
,
306 time_in_minutes
) == ERROR_SUCCESS
;
309 // Path overrides so that SHGetFolderPath isn't needed after the registry
311 base::ScopedPathOverride program_files_override_
;
312 base::ScopedPathOverride program_files_x86_override_
;
313 registry_util::RegistryOverrideManager registry_overrides_
;
318 // Verify that we return success on no registration (which means stable),
319 // whether per-system or per-user install.
320 TEST_F(GoogleUpdateSettingsTest
, CurrentChromeChannelAbsent
) {
322 base::string16 channel
;
323 EXPECT_TRUE(GoogleUpdateSettings::GetChromeChannelAndModifiers(true,
325 EXPECT_STREQ(L
"", channel
.c_str());
328 EXPECT_TRUE(GoogleUpdateSettings::GetChromeChannelAndModifiers(false,
330 EXPECT_STREQ(L
"", channel
.c_str());
333 // Test an empty Ap key for system and user.
334 TEST_F(GoogleUpdateSettingsTest
, CurrentChromeChannelEmptySystem
) {
335 SetApField(SYSTEM_INSTALL
, L
"");
336 base::string16 channel
;
337 EXPECT_TRUE(GoogleUpdateSettings::GetChromeChannelAndModifiers(true,
339 EXPECT_STREQ(L
"", channel
.c_str());
341 // Per-user lookups still succeed and return empty string.
342 EXPECT_TRUE(GoogleUpdateSettings::GetChromeChannelAndModifiers(false,
344 EXPECT_STREQ(L
"", channel
.c_str());
347 TEST_F(GoogleUpdateSettingsTest
, CurrentChromeChannelEmptyUser
) {
348 SetApField(USER_INSTALL
, L
"");
349 // Per-system lookups still succeed and return empty string.
350 base::string16 channel
;
351 EXPECT_TRUE(GoogleUpdateSettings::GetChromeChannelAndModifiers(true,
353 EXPECT_STREQ(L
"", channel
.c_str());
355 // Per-user lookup should succeed.
356 EXPECT_TRUE(GoogleUpdateSettings::GetChromeChannelAndModifiers(false,
358 EXPECT_STREQ(L
"", channel
.c_str());
361 // Test that the channel is pulled from the binaries for multi-install products.
362 TEST_F(GoogleUpdateSettingsTest
, MultiInstallChannelFromBinaries
) {
363 SetMultiApField(USER_INSTALL
, L
"2.0-dev-multi-chrome");
364 base::string16 channel
;
366 EXPECT_TRUE(GoogleUpdateSettings::GetChromeChannelAndModifiers(false,
368 EXPECT_STREQ(L
"dev-m", channel
.c_str());
370 // See if the same happens if the product's ap is cleared.
371 SetApField(USER_INSTALL
, L
"");
372 EXPECT_TRUE(GoogleUpdateSettings::GetChromeChannelAndModifiers(false,
374 EXPECT_STREQ(L
"dev-m", channel
.c_str());
376 // Test the converse (binaries are stable, Chrome is other).
377 SetMultiApField(USER_INSTALL
, L
"-multi-chrome");
378 SetApField(USER_INSTALL
, L
"2.0-dev-multi-chrome");
379 EXPECT_TRUE(GoogleUpdateSettings::GetChromeChannelAndModifiers(false,
381 EXPECT_STREQ(L
"m", channel
.c_str());
384 TEST_F(GoogleUpdateSettingsTest
, CurrentChromeChannelVariousApValuesSystem
) {
385 TestCurrentChromeChannelWithVariousApValues(SYSTEM_INSTALL
);
388 TEST_F(GoogleUpdateSettingsTest
, CurrentChromeChannelVariousApValuesUser
) {
389 TestCurrentChromeChannelWithVariousApValues(USER_INSTALL
);
392 // Run through all combinations of diff vs. full install, single vs. multi
393 // install, success and failure results, and a fistful of initial "ap" values
394 // checking that the expected final "ap" value is generated by
395 // GoogleUpdateSettings::UpdateGoogleUpdateApKey.
396 TEST_F(GoogleUpdateSettingsTest
, UpdateGoogleUpdateApKey
) {
397 const installer::ArchiveType archive_types
[] = {
398 installer::UNKNOWN_ARCHIVE_TYPE
,
399 installer::FULL_ARCHIVE_TYPE
,
400 installer::INCREMENTAL_ARCHIVE_TYPE
402 const int results
[] = {
403 installer::FIRST_INSTALL_SUCCESS
,
404 installer::INSTALL_FAILED
406 const wchar_t* const plain
[] = {
411 const wchar_t* const full
[] = {
416 COMPILE_ASSERT(arraysize(full
) == arraysize(plain
), bad_full_array_size
);
417 const wchar_t* const multifail
[] = {
422 COMPILE_ASSERT(arraysize(multifail
) == arraysize(plain
),
423 bad_multifail_array_size
);
424 const wchar_t* const multifail_full
[] = {
426 L
"1.1-multifail-full",
427 L
"1.1-dev-multifail-full"
429 COMPILE_ASSERT(arraysize(multifail_full
) == arraysize(plain
),
430 bad_multifail_full_array_size
);
431 const wchar_t* const* input_arrays
[] = {
438 for (int type_idx
= 0; type_idx
< arraysize(archive_types
); ++type_idx
) {
439 const installer::ArchiveType archive_type
= archive_types
[type_idx
];
440 for (int result_idx
= 0; result_idx
< arraysize(results
); ++result_idx
) {
441 const int result
= results
[result_idx
];
442 // The archive type will/must always be known on install success.
443 if (archive_type
== installer::UNKNOWN_ARCHIVE_TYPE
&&
444 result
== installer::FIRST_INSTALL_SUCCESS
) {
447 const wchar_t* const* outputs
= NULL
;
448 if (result
== installer::FIRST_INSTALL_SUCCESS
||
449 archive_type
== installer::FULL_ARCHIVE_TYPE
) {
451 } else if (archive_type
== installer::INCREMENTAL_ARCHIVE_TYPE
) {
453 } // else if (archive_type == UNKNOWN) see below
455 for (int inputs_idx
= 0; inputs_idx
< arraysize(input_arrays
);
457 const wchar_t* const* inputs
= input_arrays
[inputs_idx
];
458 if (archive_type
== installer::UNKNOWN_ARCHIVE_TYPE
) {
459 // "-full" is untouched if the archive type is unknown.
460 // "-multifail" is unconditionally removed.
461 if (inputs
== full
|| inputs
== multifail_full
)
466 for (int input_idx
= 0; input_idx
< arraysize(plain
); ++input_idx
) {
467 const wchar_t* input
= inputs
[input_idx
];
468 const wchar_t* output
= outputs
[input_idx
];
471 if (output
== v
.value()) {
472 EXPECT_FALSE(GoogleUpdateSettings::UpdateGoogleUpdateApKey(
473 archive_type
, result
, &v
))
474 << "archive_type: " << archive_type
475 << ", result: " << result
476 << ", input ap value: " << input
;
478 EXPECT_TRUE(GoogleUpdateSettings::UpdateGoogleUpdateApKey(
479 archive_type
, result
, &v
))
480 << "archive_type: " << archive_type
481 << ", result: " << result
482 << ", input ap value: " << input
;
484 EXPECT_EQ(output
, v
.value())
485 << "archive_type: " << archive_type
486 << ", result: " << result
487 << ", input ap value: " << input
;
494 TEST_F(GoogleUpdateSettingsTest
, UpdateInstallStatusTest
) {
495 scoped_ptr
<WorkItemList
> work_item_list(WorkItem::CreateWorkItemList());
496 // Test incremental install failure
497 ASSERT_TRUE(CreateApKey(work_item_list
.get(), L
""))
498 << "Failed to create ap key.";
499 GoogleUpdateSettings::UpdateInstallStatus(false,
500 installer::INCREMENTAL_ARCHIVE_TYPE
,
501 installer::INSTALL_FAILED
,
503 EXPECT_STREQ(ReadApKeyValue().c_str(), L
"-full");
504 work_item_list
->Rollback();
506 work_item_list
.reset(WorkItem::CreateWorkItemList());
507 // Test incremental install success
508 ASSERT_TRUE(CreateApKey(work_item_list
.get(), L
""))
509 << "Failed to create ap key.";
510 GoogleUpdateSettings::UpdateInstallStatus(false,
511 installer::INCREMENTAL_ARCHIVE_TYPE
,
512 installer::FIRST_INSTALL_SUCCESS
,
514 EXPECT_STREQ(ReadApKeyValue().c_str(), L
"");
515 work_item_list
->Rollback();
517 work_item_list
.reset(WorkItem::CreateWorkItemList());
518 // Test full install failure
519 ASSERT_TRUE(CreateApKey(work_item_list
.get(), L
"-full"))
520 << "Failed to create ap key.";
521 GoogleUpdateSettings::UpdateInstallStatus(false, installer::FULL_ARCHIVE_TYPE
,
522 installer::INSTALL_FAILED
,
524 EXPECT_STREQ(ReadApKeyValue().c_str(), L
"");
525 work_item_list
->Rollback();
527 work_item_list
.reset(WorkItem::CreateWorkItemList());
528 // Test full install success
529 ASSERT_TRUE(CreateApKey(work_item_list
.get(), L
"-full"))
530 << "Failed to create ap key.";
531 GoogleUpdateSettings::UpdateInstallStatus(false, installer::FULL_ARCHIVE_TYPE
,
532 installer::FIRST_INSTALL_SUCCESS
,
534 EXPECT_STREQ(ReadApKeyValue().c_str(), L
"");
535 work_item_list
->Rollback();
537 work_item_list
.reset(WorkItem::CreateWorkItemList());
538 // Test the case of when "ap" key doesnt exist at all
539 base::string16 ap_key_value
= ReadApKeyValue();
540 base::string16 reg_key
= GetApKeyPath();
541 HKEY reg_root
= HKEY_CURRENT_USER
;
542 bool ap_key_deleted
= false;
544 if (key
.Open(HKEY_CURRENT_USER
, reg_key
.c_str(), KEY_ALL_ACCESS
) !=
546 work_item_list
->AddCreateRegKeyWorkItem(
547 reg_root
, reg_key
, WorkItem::kWow64Default
);
548 ASSERT_TRUE(work_item_list
->Do()) << "Failed to create ClientState key.";
549 } else if (key
.DeleteValue(google_update::kRegApField
) == ERROR_SUCCESS
) {
550 ap_key_deleted
= true;
552 // try differential installer
553 GoogleUpdateSettings::UpdateInstallStatus(false,
554 installer::INCREMENTAL_ARCHIVE_TYPE
,
555 installer::INSTALL_FAILED
,
557 EXPECT_STREQ(ReadApKeyValue().c_str(), L
"-full");
558 // try full installer now
559 GoogleUpdateSettings::UpdateInstallStatus(false, installer::FULL_ARCHIVE_TYPE
,
560 installer::INSTALL_FAILED
,
562 EXPECT_STREQ(ReadApKeyValue().c_str(), L
"");
563 // Now cleanup to leave the system in unchanged state.
564 // - Diff installer creates an ap key if it didnt exist, so delete this ap key
565 // - If we created any reg key path for ap, roll it back
566 // - Finally restore the original value of ap key.
567 key
.Open(HKEY_CURRENT_USER
, reg_key
.c_str(), KEY_ALL_ACCESS
);
568 key
.DeleteValue(google_update::kRegApField
);
569 work_item_list
->Rollback();
570 if (ap_key_deleted
) {
571 work_item_list
.reset(WorkItem::CreateWorkItemList());
572 ASSERT_TRUE(CreateApKey(work_item_list
.get(), ap_key_value
))
573 << "Failed to restore ap key.";
577 TEST_F(GoogleUpdateSettingsTest
, SetEULAConsent
) {
578 using installer::FakeInstallationState
;
580 const bool multi_install
= true;
581 const bool system_level
= true;
582 FakeInstallationState machine_state
;
584 // Chrome is installed.
585 machine_state
.AddChrome(system_level
, multi_install
,
586 new Version(chrome::kChromeVersion
));
590 BrowserDistribution
* binaries
=
591 BrowserDistribution::GetSpecificDistribution(
592 BrowserDistribution::CHROME_BINARIES
);
593 BrowserDistribution
* chrome
=
594 BrowserDistribution::GetSpecificDistribution(
595 BrowserDistribution::CHROME_BROWSER
);
597 // eulaconsent is set on both the product and the binaries.
598 EXPECT_TRUE(GoogleUpdateSettings::SetEULAConsent(machine_state
, chrome
,
600 EXPECT_EQ(ERROR_SUCCESS
,
601 key
.Open(HKEY_LOCAL_MACHINE
, binaries
->GetStateMediumKey().c_str(),
603 EXPECT_EQ(ERROR_SUCCESS
,
604 key
.ReadValueDW(google_update::kRegEULAAceptedField
, &value
));
605 EXPECT_EQ(1U, value
);
606 EXPECT_EQ(ERROR_SUCCESS
,
607 key
.Open(HKEY_LOCAL_MACHINE
, chrome
->GetStateMediumKey().c_str(),
609 EXPECT_EQ(ERROR_SUCCESS
,
610 key
.ReadValueDW(google_update::kRegEULAAceptedField
, &value
));
611 EXPECT_EQ(1U, value
);
614 // Test that the appropriate default is returned if no update override is
616 TEST_F(GoogleUpdateSettingsTest
, GetAppUpdatePolicyNoOverride
) {
617 // There are no policies at all.
618 EXPECT_EQ(ERROR_FILE_NOT_FOUND
,
619 RegKey().Open(HKEY_LOCAL_MACHINE
,
620 GoogleUpdateSettings::kPoliciesKey
,
622 bool is_overridden
= true;
623 EXPECT_EQ(GoogleUpdateSettings::kDefaultUpdatePolicy
,
624 GoogleUpdateSettings::GetAppUpdatePolicy(kTestProductGuid
,
626 EXPECT_FALSE(is_overridden
);
628 // The policy key exists, but there are no values of interest present.
629 EXPECT_EQ(ERROR_SUCCESS
,
630 RegKey().Create(HKEY_LOCAL_MACHINE
,
631 GoogleUpdateSettings::kPoliciesKey
,
633 EXPECT_EQ(ERROR_SUCCESS
,
634 RegKey().Open(HKEY_LOCAL_MACHINE
,
635 GoogleUpdateSettings::kPoliciesKey
,
637 is_overridden
= true;
638 EXPECT_EQ(GoogleUpdateSettings::kDefaultUpdatePolicy
,
639 GoogleUpdateSettings::GetAppUpdatePolicy(kTestProductGuid
,
641 EXPECT_FALSE(is_overridden
);
644 TEST_F(GoogleUpdateSettingsTest
, UpdateProfileCountsSystemInstall
) {
645 // Override FILE_MODULE and FILE_EXE with a path somewhere in the default
646 // system-level install location so that
647 // GoogleUpdateSettings::IsSystemInstall() returns true.
648 base::FilePath file_exe
;
649 ASSERT_TRUE(PathService::Get(base::FILE_EXE
, &file_exe
));
650 base::FilePath
install_dir(installer::GetChromeInstallPath(
651 true /* system_install */, BrowserDistribution::GetDistribution()));
652 file_exe
= install_dir
.Append(file_exe
.BaseName());
653 base::ScopedPathOverride
file_module_override(
654 base::FILE_MODULE
, file_exe
, true /* is_absolute */, false /* create */);
655 base::ScopedPathOverride
file_exe_override(
656 base::FILE_EXE
, file_exe
, true /* is_absolute */, false /* create */);
658 // No profile count keys present yet.
659 const base::string16
& state_key
= BrowserDistribution::GetDistribution()->
660 GetAppRegistrationData().GetStateMediumKey();
661 base::string16
num_profiles_path(state_key
);
662 num_profiles_path
.append(L
"\\");
663 num_profiles_path
.append(google_update::kRegProfilesActive
);
664 base::string16
num_signed_in_path(state_key
);
665 num_signed_in_path
.append(L
"\\");
666 num_signed_in_path
.append(google_update::kRegProfilesSignedIn
);
668 EXPECT_EQ(ERROR_FILE_NOT_FOUND
,
669 RegKey().Open(HKEY_LOCAL_MACHINE
,
670 num_profiles_path
.c_str(),
672 EXPECT_EQ(ERROR_FILE_NOT_FOUND
,
673 RegKey().Open(HKEY_LOCAL_MACHINE
,
674 num_signed_in_path
.c_str(),
677 // Show time! Write the values.
678 GoogleUpdateSettings::UpdateProfileCounts(3, 2);
680 // Verify the keys were created.
681 EXPECT_EQ(ERROR_SUCCESS
,
682 RegKey().Open(HKEY_LOCAL_MACHINE
,
683 num_profiles_path
.c_str(),
685 EXPECT_EQ(ERROR_SUCCESS
,
686 RegKey().Open(HKEY_LOCAL_MACHINE
,
687 num_signed_in_path
.c_str(),
690 base::string16 uniquename
;
691 EXPECT_TRUE(base::win::GetUserSidString(&uniquename
));
693 // Verify the values are accessible.
694 DWORD num_profiles
= 0;
695 DWORD num_signed_in
= 0;
696 base::string16 aggregate
;
699 RegKey(HKEY_LOCAL_MACHINE
, num_profiles_path
.c_str(),
700 KEY_QUERY_VALUE
).ReadValueDW(uniquename
.c_str(),
704 RegKey(HKEY_LOCAL_MACHINE
, num_signed_in_path
.c_str(),
705 KEY_QUERY_VALUE
).ReadValueDW(uniquename
.c_str(),
709 RegKey(HKEY_LOCAL_MACHINE
, num_signed_in_path
.c_str(),
710 KEY_QUERY_VALUE
).ReadValue(google_update::kRegAggregateMethod
,
713 // Verify the correct values were written.
714 EXPECT_EQ(3, num_profiles
);
715 EXPECT_EQ(2, num_signed_in
);
716 EXPECT_EQ(L
"sum()", aggregate
);
719 TEST_F(GoogleUpdateSettingsTest
, UpdateProfileCountsUserInstall
) {
720 // Unit tests never operate as an installed application, so will never
721 // be a system install.
723 // No profile count values present yet.
724 const base::string16
& state_key
= BrowserDistribution::GetDistribution()->
725 GetAppRegistrationData().GetStateKey();
727 EXPECT_EQ(ERROR_FILE_NOT_FOUND
,
728 RegKey().Open(HKEY_CURRENT_USER
,
732 // Show time! Write the values.
733 GoogleUpdateSettings::UpdateProfileCounts(4, 1);
735 // Verify the key was created.
736 EXPECT_EQ(ERROR_SUCCESS
,
737 RegKey().Open(HKEY_CURRENT_USER
,
741 // Verify the values are accessible.
742 base::string16 num_profiles
;
743 base::string16 num_signed_in
;
746 RegKey(HKEY_CURRENT_USER
, state_key
.c_str(), KEY_QUERY_VALUE
).
747 ReadValue(google_update::kRegProfilesActive
, &num_profiles
));
750 RegKey(HKEY_CURRENT_USER
, state_key
.c_str(), KEY_QUERY_VALUE
).
751 ReadValue(google_update::kRegProfilesSignedIn
, &num_signed_in
));
753 // Verify the correct values were written.
754 EXPECT_EQ(L
"4", num_profiles
);
755 EXPECT_EQ(L
"1", num_signed_in
);
758 #if defined(GOOGLE_CHROME_BUILD)
760 // Test that the default override is returned if no app-specific override is
762 TEST_F(GoogleUpdateSettingsTest
, GetAppUpdatePolicyDefaultOverride
) {
763 EXPECT_EQ(ERROR_SUCCESS
,
764 RegKey(HKEY_LOCAL_MACHINE
, GoogleUpdateSettings::kPoliciesKey
,
765 KEY_SET_VALUE
).WriteValue(
766 GoogleUpdateSettings::kUpdatePolicyValue
,
767 static_cast<DWORD
>(0)));
768 bool is_overridden
= true;
769 EXPECT_EQ(GoogleUpdateSettings::UPDATES_DISABLED
,
770 GoogleUpdateSettings::GetAppUpdatePolicy(kTestProductGuid
,
772 EXPECT_FALSE(is_overridden
);
774 EXPECT_EQ(ERROR_SUCCESS
,
775 RegKey(HKEY_LOCAL_MACHINE
, GoogleUpdateSettings::kPoliciesKey
,
776 KEY_SET_VALUE
).WriteValue(
777 GoogleUpdateSettings::kUpdatePolicyValue
,
778 static_cast<DWORD
>(1)));
779 is_overridden
= true;
780 EXPECT_EQ(GoogleUpdateSettings::AUTOMATIC_UPDATES
,
781 GoogleUpdateSettings::GetAppUpdatePolicy(kTestProductGuid
,
783 EXPECT_FALSE(is_overridden
);
785 EXPECT_EQ(ERROR_SUCCESS
,
786 RegKey(HKEY_LOCAL_MACHINE
, GoogleUpdateSettings::kPoliciesKey
,
787 KEY_SET_VALUE
).WriteValue(
788 GoogleUpdateSettings::kUpdatePolicyValue
,
789 static_cast<DWORD
>(2)));
790 is_overridden
= true;
791 EXPECT_EQ(GoogleUpdateSettings::MANUAL_UPDATES_ONLY
,
792 GoogleUpdateSettings::GetAppUpdatePolicy(kTestProductGuid
,
794 EXPECT_FALSE(is_overridden
);
796 EXPECT_EQ(ERROR_SUCCESS
,
797 RegKey(HKEY_LOCAL_MACHINE
, GoogleUpdateSettings::kPoliciesKey
,
798 KEY_SET_VALUE
).WriteValue(
799 GoogleUpdateSettings::kUpdatePolicyValue
,
800 static_cast<DWORD
>(3)));
801 is_overridden
= true;
802 EXPECT_EQ(GoogleUpdateSettings::AUTO_UPDATES_ONLY
,
803 GoogleUpdateSettings::GetAppUpdatePolicy(kTestProductGuid
,
805 EXPECT_FALSE(is_overridden
);
807 // The default policy should be in force for bogus values.
808 EXPECT_EQ(ERROR_SUCCESS
,
809 RegKey(HKEY_LOCAL_MACHINE
, GoogleUpdateSettings::kPoliciesKey
,
810 KEY_SET_VALUE
).WriteValue(
811 GoogleUpdateSettings::kUpdatePolicyValue
,
812 static_cast<DWORD
>(4)));
813 is_overridden
= true;
814 EXPECT_EQ(GoogleUpdateSettings::kDefaultUpdatePolicy
,
815 GoogleUpdateSettings::GetAppUpdatePolicy(kTestProductGuid
,
817 EXPECT_FALSE(is_overridden
);
820 // Test that an app-specific override is used if present.
821 TEST_F(GoogleUpdateSettingsTest
, GetAppUpdatePolicyAppOverride
) {
822 base::string16
app_policy_value(
823 GoogleUpdateSettings::kUpdateOverrideValuePrefix
);
824 app_policy_value
.append(kTestProductGuid
);
826 EXPECT_EQ(ERROR_SUCCESS
,
827 RegKey(HKEY_LOCAL_MACHINE
, GoogleUpdateSettings::kPoliciesKey
,
828 KEY_SET_VALUE
).WriteValue(
829 GoogleUpdateSettings::kUpdatePolicyValue
,
830 static_cast<DWORD
>(1)));
831 EXPECT_EQ(ERROR_SUCCESS
,
832 RegKey(HKEY_LOCAL_MACHINE
, GoogleUpdateSettings::kPoliciesKey
,
833 KEY_SET_VALUE
).WriteValue(app_policy_value
.c_str(),
834 static_cast<DWORD
>(0)));
835 bool is_overridden
= false;
836 EXPECT_EQ(GoogleUpdateSettings::UPDATES_DISABLED
,
837 GoogleUpdateSettings::GetAppUpdatePolicy(kTestProductGuid
,
839 EXPECT_TRUE(is_overridden
);
841 EXPECT_EQ(ERROR_SUCCESS
,
842 RegKey(HKEY_LOCAL_MACHINE
, GoogleUpdateSettings::kPoliciesKey
,
843 KEY_SET_VALUE
).WriteValue(
844 GoogleUpdateSettings::kUpdatePolicyValue
,
845 static_cast<DWORD
>(0)));
846 EXPECT_EQ(ERROR_SUCCESS
,
847 RegKey(HKEY_LOCAL_MACHINE
, GoogleUpdateSettings::kPoliciesKey
,
848 KEY_SET_VALUE
).WriteValue(app_policy_value
.c_str(),
849 static_cast<DWORD
>(1)));
850 is_overridden
= false;
851 EXPECT_EQ(GoogleUpdateSettings::AUTOMATIC_UPDATES
,
852 GoogleUpdateSettings::GetAppUpdatePolicy(kTestProductGuid
,
854 EXPECT_TRUE(is_overridden
);
856 EXPECT_EQ(ERROR_SUCCESS
,
857 RegKey(HKEY_LOCAL_MACHINE
, GoogleUpdateSettings::kPoliciesKey
,
858 KEY_SET_VALUE
).WriteValue(app_policy_value
.c_str(),
859 static_cast<DWORD
>(2)));
860 is_overridden
= false;
861 EXPECT_EQ(GoogleUpdateSettings::MANUAL_UPDATES_ONLY
,
862 GoogleUpdateSettings::GetAppUpdatePolicy(kTestProductGuid
,
864 EXPECT_TRUE(is_overridden
);
866 EXPECT_EQ(ERROR_SUCCESS
,
867 RegKey(HKEY_LOCAL_MACHINE
, GoogleUpdateSettings::kPoliciesKey
,
868 KEY_SET_VALUE
).WriteValue(app_policy_value
.c_str(),
869 static_cast<DWORD
>(3)));
870 is_overridden
= false;
871 EXPECT_EQ(GoogleUpdateSettings::AUTO_UPDATES_ONLY
,
872 GoogleUpdateSettings::GetAppUpdatePolicy(kTestProductGuid
,
874 EXPECT_TRUE(is_overridden
);
876 // The default policy should be in force for bogus values.
877 EXPECT_EQ(ERROR_SUCCESS
,
878 RegKey(HKEY_LOCAL_MACHINE
, GoogleUpdateSettings::kPoliciesKey
,
879 KEY_SET_VALUE
).WriteValue(app_policy_value
.c_str(),
880 static_cast<DWORD
>(4)));
881 is_overridden
= true;
882 EXPECT_EQ(GoogleUpdateSettings::UPDATES_DISABLED
,
883 GoogleUpdateSettings::GetAppUpdatePolicy(kTestProductGuid
,
885 EXPECT_FALSE(is_overridden
);
888 TEST_F(GoogleUpdateSettingsTest
, PerAppUpdatesDisabledByPolicy
) {
889 BrowserDistribution
* dist
= BrowserDistribution::GetDistribution();
891 SetUpdatePolicyForAppGuid(dist
->GetAppGuid(),
892 GoogleUpdateSettings::UPDATES_DISABLED
));
893 bool is_overridden
= false;
894 GoogleUpdateSettings::UpdatePolicy update_policy
=
895 GoogleUpdateSettings::GetAppUpdatePolicy(dist
->GetAppGuid(),
897 EXPECT_TRUE(is_overridden
);
898 EXPECT_EQ(GoogleUpdateSettings::UPDATES_DISABLED
, update_policy
);
899 EXPECT_FALSE(GoogleUpdateSettings::AreAutoupdatesEnabled());
901 EXPECT_TRUE(GoogleUpdateSettings::ReenableAutoupdates());
902 update_policy
= GoogleUpdateSettings::GetAppUpdatePolicy(dist
->GetAppGuid(),
904 // Should still have a policy but now that policy should explicitly enable
906 EXPECT_TRUE(is_overridden
);
907 EXPECT_EQ(GoogleUpdateSettings::AUTOMATIC_UPDATES
, update_policy
);
908 EXPECT_TRUE(GoogleUpdateSettings::AreAutoupdatesEnabled());
911 TEST_F(GoogleUpdateSettingsTest
, PerAppUpdatesEnabledWithGlobalDisabled
) {
912 // Disable updates globally but enable them for Chrome (the app-specific
913 // setting should take precedence).
914 BrowserDistribution
* dist
= BrowserDistribution::GetDistribution();
915 BrowserDistribution
* binaries
= BrowserDistribution::GetSpecificDistribution(
916 BrowserDistribution::CHROME_BINARIES
);
918 SetUpdatePolicyForAppGuid(dist
->GetAppGuid(),
919 GoogleUpdateSettings::AUTOMATIC_UPDATES
));
921 SetUpdatePolicyForAppGuid(binaries
->GetAppGuid(),
922 GoogleUpdateSettings::AUTOMATIC_UPDATES
));
923 EXPECT_TRUE(SetGlobalUpdatePolicy(GoogleUpdateSettings::UPDATES_DISABLED
));
925 // Make sure we read this as still having updates enabled.
926 EXPECT_TRUE(GoogleUpdateSettings::AreAutoupdatesEnabled());
928 // Make sure that the reset action returns true and is a no-op.
929 EXPECT_TRUE(GoogleUpdateSettings::ReenableAutoupdates());
930 EXPECT_EQ(GoogleUpdateSettings::AUTOMATIC_UPDATES
,
931 GetUpdatePolicyForAppGuid(dist
->GetAppGuid()));
932 EXPECT_EQ(GoogleUpdateSettings::AUTOMATIC_UPDATES
,
933 GetUpdatePolicyForAppGuid(binaries
->GetAppGuid()));
934 EXPECT_EQ(GoogleUpdateSettings::UPDATES_DISABLED
, GetGlobalUpdatePolicy());
937 TEST_F(GoogleUpdateSettingsTest
, GlobalUpdatesDisabledByPolicy
) {
938 BrowserDistribution
* dist
= BrowserDistribution::GetDistribution();
939 EXPECT_TRUE(SetGlobalUpdatePolicy(GoogleUpdateSettings::UPDATES_DISABLED
));
940 bool is_overridden
= false;
942 // The contract for GetAppUpdatePolicy states that |is_overridden| should be
943 // set to false when updates are disabled on a non-app-specific basis.
944 GoogleUpdateSettings::UpdatePolicy update_policy
=
945 GoogleUpdateSettings::GetAppUpdatePolicy(dist
->GetAppGuid(),
947 EXPECT_FALSE(is_overridden
);
948 EXPECT_EQ(GoogleUpdateSettings::UPDATES_DISABLED
, update_policy
);
949 EXPECT_FALSE(GoogleUpdateSettings::AreAutoupdatesEnabled());
951 EXPECT_TRUE(GoogleUpdateSettings::ReenableAutoupdates());
952 update_policy
= GoogleUpdateSettings::GetAppUpdatePolicy(dist
->GetAppGuid(),
954 // Policy should now be to enable updates, |is_overridden| should still be
956 EXPECT_FALSE(is_overridden
);
957 EXPECT_EQ(GoogleUpdateSettings::AUTOMATIC_UPDATES
, update_policy
);
958 EXPECT_TRUE(GoogleUpdateSettings::AreAutoupdatesEnabled());
961 TEST_F(GoogleUpdateSettingsTest
, UpdatesDisabledByTimeout
) {
962 // Disable updates altogether.
963 EXPECT_TRUE(SetUpdateTimeoutOverride(0));
964 EXPECT_FALSE(GoogleUpdateSettings::AreAutoupdatesEnabled());
965 EXPECT_TRUE(GoogleUpdateSettings::ReenableAutoupdates());
966 EXPECT_TRUE(GoogleUpdateSettings::AreAutoupdatesEnabled());
968 // Set the update period to something unreasonable.
969 EXPECT_TRUE(SetUpdateTimeoutOverride(
970 GoogleUpdateSettings::kCheckPeriodOverrideMinutesMax
+ 1));
971 EXPECT_FALSE(GoogleUpdateSettings::AreAutoupdatesEnabled());
972 EXPECT_TRUE(GoogleUpdateSettings::ReenableAutoupdates());
973 EXPECT_TRUE(GoogleUpdateSettings::AreAutoupdatesEnabled());
976 TEST_F(GoogleUpdateSettingsTest
, ExperimentsLabelHelperSystem
) {
977 TestExperimentsLabelHelper(SYSTEM_INSTALL
);
980 TEST_F(GoogleUpdateSettingsTest
, ExperimentsLabelHelperUser
) {
981 TestExperimentsLabelHelper(USER_INSTALL
);
984 #endif // defined(GOOGLE_CHROME_BUILD)
986 // Test GoogleUpdateSettings::GetUninstallCommandLine at system- or user-level,
987 // according to the param.
988 class GetUninstallCommandLine
: public GoogleUpdateSettingsTest
,
989 public testing::WithParamInterface
<bool> {
991 static const wchar_t kDummyCommand
[];
993 void SetUp() override
{
994 GoogleUpdateSettingsTest::SetUp();
995 system_install_
= GetParam();
996 root_key_
= system_install_
? HKEY_LOCAL_MACHINE
: HKEY_CURRENT_USER
;
1000 bool system_install_
;
1003 const wchar_t GetUninstallCommandLine::kDummyCommand
[] =
1004 L
"\"goopdate.exe\" /spam";
1006 // Tests that GetUninstallCommandLine returns an empty string if there's no
1007 // Software\Google\Update key.
1008 TEST_P(GetUninstallCommandLine
, TestNoKey
) {
1009 EXPECT_EQ(base::string16(),
1010 GoogleUpdateSettings::GetUninstallCommandLine(system_install_
));
1013 // Tests that GetUninstallCommandLine returns an empty string if there's no
1014 // UninstallCmdLine value in the Software\Google\Update key.
1015 TEST_P(GetUninstallCommandLine
, TestNoValue
) {
1016 RegKey(root_key_
, google_update::kRegPathGoogleUpdate
, KEY_SET_VALUE
);
1017 EXPECT_EQ(base::string16(),
1018 GoogleUpdateSettings::GetUninstallCommandLine(system_install_
));
1021 // Tests that GetUninstallCommandLine returns an empty string if there's an
1022 // empty UninstallCmdLine value in the Software\Google\Update key.
1023 TEST_P(GetUninstallCommandLine
, TestEmptyValue
) {
1024 RegKey(root_key_
, google_update::kRegPathGoogleUpdate
, KEY_SET_VALUE
)
1025 .WriteValue(google_update::kRegUninstallCmdLine
, L
"");
1026 EXPECT_EQ(base::string16(),
1027 GoogleUpdateSettings::GetUninstallCommandLine(system_install_
));
1030 // Tests that GetUninstallCommandLine returns the correct string if there's an
1031 // UninstallCmdLine value in the Software\Google\Update key.
1032 TEST_P(GetUninstallCommandLine
, TestRealValue
) {
1033 RegKey(root_key_
, google_update::kRegPathGoogleUpdate
, KEY_SET_VALUE
)
1034 .WriteValue(google_update::kRegUninstallCmdLine
, kDummyCommand
);
1035 EXPECT_EQ(base::string16(kDummyCommand
),
1036 GoogleUpdateSettings::GetUninstallCommandLine(system_install_
));
1037 // Make sure that there's no value in the other level (user or system).
1038 EXPECT_EQ(base::string16(),
1039 GoogleUpdateSettings::GetUninstallCommandLine(!system_install_
));
1042 INSTANTIATE_TEST_CASE_P(GetUninstallCommandLineAtLevel
, GetUninstallCommandLine
,
1045 // Test GoogleUpdateSettings::GetGoogleUpdateVersion at system- or user-level,
1046 // according to the param.
1047 class GetGoogleUpdateVersion
: public GoogleUpdateSettingsTest
,
1048 public testing::WithParamInterface
<bool> {
1050 static const wchar_t kDummyVersion
[];
1052 void SetUp() override
{
1053 GoogleUpdateSettingsTest::SetUp();
1054 system_install_
= GetParam();
1055 root_key_
= system_install_
? HKEY_LOCAL_MACHINE
: HKEY_CURRENT_USER
;
1059 bool system_install_
;
1062 const wchar_t GetGoogleUpdateVersion::kDummyVersion
[] = L
"1.2.3.4";
1064 // Tests that GetGoogleUpdateVersion returns an empty string if there's no
1065 // Software\Google\Update key.
1066 TEST_P(GetGoogleUpdateVersion
, TestNoKey
) {
1068 GoogleUpdateSettings::GetGoogleUpdateVersion(system_install_
).IsValid());
1071 // Tests that GetGoogleUpdateVersion returns an empty string if there's no
1072 // version value in the Software\Google\Update key.
1073 TEST_P(GetGoogleUpdateVersion
, TestNoValue
) {
1074 RegKey(root_key_
, google_update::kRegPathGoogleUpdate
, KEY_SET_VALUE
);
1076 GoogleUpdateSettings::GetGoogleUpdateVersion(system_install_
).IsValid());
1079 // Tests that GetGoogleUpdateVersion returns an empty string if there's an
1080 // empty version value in the Software\Google\Update key.
1081 TEST_P(GetGoogleUpdateVersion
, TestEmptyValue
) {
1082 RegKey(root_key_
, google_update::kRegPathGoogleUpdate
, KEY_SET_VALUE
)
1083 .WriteValue(google_update::kRegGoogleUpdateVersion
, L
"");
1085 GoogleUpdateSettings::GetGoogleUpdateVersion(system_install_
).IsValid());
1088 // Tests that GetGoogleUpdateVersion returns the correct string if there's a
1089 // version value in the Software\Google\Update key.
1090 TEST_P(GetGoogleUpdateVersion
, TestRealValue
) {
1091 RegKey(root_key_
, google_update::kRegPathGoogleUpdate
, KEY_SET_VALUE
)
1092 .WriteValue(google_update::kRegGoogleUpdateVersion
, kDummyVersion
);
1093 Version
expected(base::UTF16ToUTF8(kDummyVersion
));
1094 EXPECT_TRUE(expected
.Equals(
1095 GoogleUpdateSettings::GetGoogleUpdateVersion(system_install_
)));
1096 // Make sure that there's no value in the other level (user or system).
1098 GoogleUpdateSettings::GetGoogleUpdateVersion(!system_install_
)
1102 INSTANTIATE_TEST_CASE_P(GetGoogleUpdateVersionAtLevel
, GetGoogleUpdateVersion
,
1105 // Test values for use by the CollectStatsConsent test fixture.
1117 struct UserLevelState
{};
1118 struct SystemLevelState
{};
1119 static const UserLevelState kUserLevel
;
1120 static const SystemLevelState kSystemLevel
;
1122 StatsState(const UserLevelState
&,
1123 InstallType install_type
,
1124 StateSetting state_value
)
1125 : system_level_(false),
1126 multi_install_(install_type
== MULTI_INSTALL
),
1127 state_value_(state_value
),
1128 state_medium_value_(NO_SETTING
) {
1130 StatsState(const SystemLevelState
&,
1131 InstallType install_type
,
1132 StateSetting state_value
,
1133 StateSetting state_medium_value
)
1134 : system_level_(true),
1135 multi_install_(install_type
== MULTI_INSTALL
),
1136 state_value_(state_value
),
1137 state_medium_value_(state_medium_value
) {
1139 bool system_level() const { return system_level_
; }
1140 bool multi_install() const { return multi_install_
; }
1141 HKEY
root_key() const {
1142 return system_level_
? HKEY_LOCAL_MACHINE
: HKEY_CURRENT_USER
;
1144 StateSetting
state_value() const { return state_value_
; }
1145 StateSetting
state_medium_value() const {
1146 return state_medium_value_
;
1148 bool is_consent_granted() const {
1149 return (system_level_
&& state_medium_value_
!= NO_SETTING
) ?
1150 (state_medium_value_
== TRUE_SETTING
) :
1151 (state_value_
== TRUE_SETTING
);
1156 bool multi_install_
;
1157 StateSetting state_value_
;
1158 StateSetting state_medium_value_
;
1161 const StatsState::UserLevelState
StatsState::kUserLevel
= {};
1162 const StatsState::SystemLevelState
StatsState::kSystemLevel
= {};
1164 // A value parameterized test for testing the stats collection consent setting.
1165 class CollectStatsConsent
: public ::testing::TestWithParam
<StatsState
> {
1167 static void SetUpTestCase();
1168 static void TearDownTestCase();
1170 void SetUp() override
;
1171 static void MakeChromeMultiInstall(HKEY root_key
);
1172 static void ApplySetting(StatsState::StateSetting setting
,
1174 const base::string16
& reg_key
);
1176 static base::string16
* chrome_version_key_
;
1177 static base::string16
* chrome_state_key_
;
1178 static base::string16
* chrome_state_medium_key_
;
1179 static base::string16
* binaries_state_key_
;
1180 static base::string16
* binaries_state_medium_key_
;
1181 registry_util::RegistryOverrideManager override_manager_
;
1184 base::string16
* CollectStatsConsent::chrome_version_key_
;
1185 base::string16
* CollectStatsConsent::chrome_state_key_
;
1186 base::string16
* CollectStatsConsent::chrome_state_medium_key_
;
1187 base::string16
* CollectStatsConsent::binaries_state_key_
;
1188 base::string16
* CollectStatsConsent::binaries_state_medium_key_
;
1190 void CollectStatsConsent::SetUpTestCase() {
1191 BrowserDistribution
* dist
=
1192 BrowserDistribution::GetSpecificDistribution(
1193 BrowserDistribution::CHROME_BROWSER
);
1194 chrome_version_key_
= new base::string16(dist
->GetVersionKey());
1195 chrome_state_key_
= new base::string16(dist
->GetStateKey());
1196 chrome_state_medium_key_
= new base::string16(dist
->GetStateMediumKey());
1198 dist
= BrowserDistribution::GetSpecificDistribution(
1199 BrowserDistribution::CHROME_BINARIES
);
1200 binaries_state_key_
= new base::string16(dist
->GetStateKey());
1201 binaries_state_medium_key_
= new base::string16(dist
->GetStateMediumKey());
1204 void CollectStatsConsent::TearDownTestCase() {
1205 delete chrome_version_key_
;
1206 delete chrome_state_key_
;
1207 delete chrome_state_medium_key_
;
1208 delete binaries_state_key_
;
1209 delete binaries_state_medium_key_
;
1212 // Install the registry override and apply the settings to the registry.
1213 void CollectStatsConsent::SetUp() {
1214 const StatsState
& stats_state
= GetParam();
1215 const HKEY root_key
= stats_state
.root_key();
1216 base::string16
reg_temp_name(
1217 stats_state
.system_level() ? L
"HKLM_" : L
"HKCU_");
1218 reg_temp_name
+= L
"CollectStatsConsent";
1219 override_manager_
.OverrideRegistry(root_key
);
1221 if (stats_state
.multi_install()) {
1222 MakeChromeMultiInstall(root_key
);
1223 ApplySetting(stats_state
.state_value(), root_key
, *binaries_state_key_
);
1224 ApplySetting(stats_state
.state_medium_value(), root_key
,
1225 *binaries_state_medium_key_
);
1227 ApplySetting(stats_state
.state_value(), root_key
, *chrome_state_key_
);
1228 ApplySetting(stats_state
.state_medium_value(), root_key
,
1229 *chrome_state_medium_key_
);
1233 // Write values into the registry so that Chrome is considered to be installed
1234 // as multi-install.
1235 void CollectStatsConsent::MakeChromeMultiInstall(HKEY root_key
) {
1238 RegKey(root_key
, chrome_version_key_
->c_str(),
1239 KEY_SET_VALUE
).WriteValue(google_update::kRegVersionField
,
1243 RegKey(root_key
, chrome_state_key_
->c_str(),
1244 KEY_SET_VALUE
).WriteValue(installer::kUninstallArgumentsField
,
1245 L
"--multi-install"));
1248 // Write the correct value to represent |setting| in the registry.
1249 void CollectStatsConsent::ApplySetting(StatsState::StateSetting setting
,
1251 const base::string16
& reg_key
) {
1252 if (setting
!= StatsState::NO_SETTING
) {
1253 DWORD value
= setting
!= StatsState::FALSE_SETTING
? 1 : 0;
1256 RegKey(root_key
, reg_key
.c_str(),
1257 KEY_SET_VALUE
).WriteValue(google_update::kRegUsageStatsField
,
1262 // Test that stats consent can be read.
1263 TEST_P(CollectStatsConsent
, GetCollectStatsConsentAtLevel
) {
1264 if (GetParam().is_consent_granted()) {
1265 EXPECT_TRUE(GoogleUpdateSettings::GetCollectStatsConsentAtLevel(
1266 GetParam().system_level()));
1268 EXPECT_FALSE(GoogleUpdateSettings::GetCollectStatsConsentAtLevel(
1269 GetParam().system_level()));
1273 // Test that stats consent can be flipped to the opposite setting, that the new
1274 // setting takes affect, and that the correct registry location is modified.
1275 TEST_P(CollectStatsConsent
, SetCollectStatsConsentAtLevel
) {
1276 EXPECT_TRUE(GoogleUpdateSettings::SetCollectStatsConsentAtLevel(
1277 GetParam().system_level(),
1278 !GetParam().is_consent_granted()));
1279 const base::string16
* const reg_keys
[] = {
1281 chrome_state_medium_key_
,
1282 binaries_state_key_
,
1283 binaries_state_medium_key_
,
1285 int key_index
= ((GetParam().system_level() ? 1 : 0) +
1286 (GetParam().multi_install() ? 2 : 0));
1287 const base::string16
& reg_key
= *reg_keys
[key_index
];
1291 RegKey(GetParam().root_key(), reg_key
.c_str(),
1292 KEY_QUERY_VALUE
).ReadValueDW(google_update::kRegUsageStatsField
,
1294 if (GetParam().is_consent_granted()) {
1295 EXPECT_FALSE(GoogleUpdateSettings::GetCollectStatsConsentAtLevel(
1296 GetParam().system_level()));
1297 EXPECT_EQ(0UL, value
);
1299 EXPECT_TRUE(GoogleUpdateSettings::GetCollectStatsConsentAtLevel(
1300 GetParam().system_level()));
1301 EXPECT_EQ(1UL, value
);
1305 INSTANTIATE_TEST_CASE_P(
1306 UserLevelSingleInstall
,
1307 CollectStatsConsent
,
1309 StatsState(StatsState::kUserLevel
, StatsState::SINGLE_INSTALL
,
1310 StatsState::NO_SETTING
),
1311 StatsState(StatsState::kUserLevel
, StatsState::SINGLE_INSTALL
,
1312 StatsState::FALSE_SETTING
),
1313 StatsState(StatsState::kUserLevel
, StatsState::SINGLE_INSTALL
,
1314 StatsState::TRUE_SETTING
)));
1315 INSTANTIATE_TEST_CASE_P(
1316 UserLevelMultiInstall
,
1317 CollectStatsConsent
,
1319 StatsState(StatsState::kUserLevel
, StatsState::MULTI_INSTALL
,
1320 StatsState::NO_SETTING
),
1321 StatsState(StatsState::kUserLevel
, StatsState::MULTI_INSTALL
,
1322 StatsState::FALSE_SETTING
),
1323 StatsState(StatsState::kUserLevel
, StatsState::MULTI_INSTALL
,
1324 StatsState::TRUE_SETTING
)));
1325 INSTANTIATE_TEST_CASE_P(
1326 SystemLevelSingleInstall
,
1327 CollectStatsConsent
,
1329 StatsState(StatsState::kSystemLevel
, StatsState::SINGLE_INSTALL
,
1330 StatsState::NO_SETTING
, StatsState::NO_SETTING
),
1331 StatsState(StatsState::kSystemLevel
, StatsState::SINGLE_INSTALL
,
1332 StatsState::NO_SETTING
, StatsState::FALSE_SETTING
),
1333 StatsState(StatsState::kSystemLevel
, StatsState::SINGLE_INSTALL
,
1334 StatsState::NO_SETTING
, StatsState::TRUE_SETTING
),
1335 StatsState(StatsState::kSystemLevel
, StatsState::SINGLE_INSTALL
,
1336 StatsState::FALSE_SETTING
, StatsState::NO_SETTING
),
1337 StatsState(StatsState::kSystemLevel
, StatsState::SINGLE_INSTALL
,
1338 StatsState::FALSE_SETTING
, StatsState::FALSE_SETTING
),
1339 StatsState(StatsState::kSystemLevel
, StatsState::SINGLE_INSTALL
,
1340 StatsState::FALSE_SETTING
, StatsState::TRUE_SETTING
),
1341 StatsState(StatsState::kSystemLevel
, StatsState::SINGLE_INSTALL
,
1342 StatsState::TRUE_SETTING
, StatsState::NO_SETTING
),
1343 StatsState(StatsState::kSystemLevel
, StatsState::SINGLE_INSTALL
,
1344 StatsState::TRUE_SETTING
, StatsState::FALSE_SETTING
),
1345 StatsState(StatsState::kSystemLevel
, StatsState::SINGLE_INSTALL
,
1346 StatsState::TRUE_SETTING
, StatsState::TRUE_SETTING
)));
1347 INSTANTIATE_TEST_CASE_P(
1348 SystemLevelMultiInstall
,
1349 CollectStatsConsent
,
1351 StatsState(StatsState::kSystemLevel
, StatsState::MULTI_INSTALL
,
1352 StatsState::NO_SETTING
, StatsState::NO_SETTING
),
1353 StatsState(StatsState::kSystemLevel
, StatsState::MULTI_INSTALL
,
1354 StatsState::NO_SETTING
, StatsState::FALSE_SETTING
),
1355 StatsState(StatsState::kSystemLevel
, StatsState::MULTI_INSTALL
,
1356 StatsState::NO_SETTING
, StatsState::TRUE_SETTING
),
1357 StatsState(StatsState::kSystemLevel
, StatsState::MULTI_INSTALL
,
1358 StatsState::FALSE_SETTING
, StatsState::NO_SETTING
),
1359 StatsState(StatsState::kSystemLevel
, StatsState::MULTI_INSTALL
,
1360 StatsState::FALSE_SETTING
, StatsState::FALSE_SETTING
),
1361 StatsState(StatsState::kSystemLevel
, StatsState::MULTI_INSTALL
,
1362 StatsState::FALSE_SETTING
, StatsState::TRUE_SETTING
),
1363 StatsState(StatsState::kSystemLevel
, StatsState::MULTI_INSTALL
,
1364 StatsState::TRUE_SETTING
, StatsState::NO_SETTING
),
1365 StatsState(StatsState::kSystemLevel
, StatsState::MULTI_INSTALL
,
1366 StatsState::TRUE_SETTING
, StatsState::FALSE_SETTING
),
1367 StatsState(StatsState::kSystemLevel
, StatsState::MULTI_INSTALL
,
1368 StatsState::TRUE_SETTING
, StatsState::TRUE_SETTING
)));