Remove obsolete for_web_contents parameter in FontRenderParamsQuery.
[chromium-blink-merge.git] / chrome / installer / util / google_update_settings_unittest.cc
blob024cf5bf685676a31f1b74c66342a8aeda75598f
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"
7 #include <windows.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;
32 namespace {
34 const wchar_t kTestProductGuid[] = L"{89F1B351-B15D-48D4-8F10-1298721CF13D}";
35 const wchar_t kTestExperimentLabel[] = L"test_label_value";
37 // This test fixture redirects the HKLM and HKCU registry hives for
38 // the duration of the test to make it independent of the machine
39 // and user settings.
40 class GoogleUpdateSettingsTest : public testing::Test {
41 protected:
42 enum SystemUserInstall {
43 SYSTEM_INSTALL,
44 USER_INSTALL,
47 GoogleUpdateSettingsTest()
48 : program_files_override_(base::DIR_PROGRAM_FILES),
49 program_files_x86_override_(base::DIR_PROGRAM_FILESX86) {
50 registry_overrides_.OverrideRegistry(HKEY_LOCAL_MACHINE);
51 registry_overrides_.OverrideRegistry(HKEY_CURRENT_USER);
54 void SetApField(SystemUserInstall is_system, const wchar_t* value) {
55 HKEY root = is_system == SYSTEM_INSTALL ?
56 HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER;
58 RegKey update_key;
59 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
60 base::string16 path = dist->GetStateKey();
61 ASSERT_EQ(ERROR_SUCCESS, update_key.Create(root, path.c_str(), KEY_WRITE));
62 ASSERT_EQ(ERROR_SUCCESS, update_key.WriteValue(L"ap", value));
65 // Sets the "ap" field for a multi-install product (both the product and
66 // the binaries).
67 void SetMultiApField(SystemUserInstall is_system, const wchar_t* value) {
68 // Caller must specify a multi-install ap value.
69 ASSERT_NE(base::string16::npos, base::string16(value).find(L"-multi"));
70 HKEY root = is_system == SYSTEM_INSTALL ?
71 HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER;
72 RegKey update_key;
74 // Write the ap value for both the product and the binaries.
75 BrowserDistribution* const kDists[] = {
76 BrowserDistribution::GetDistribution(),
77 BrowserDistribution::GetSpecificDistribution(
78 BrowserDistribution::CHROME_BINARIES)
80 for (size_t i = 0; i < arraysize(kDists); ++i) {
81 base::string16 path = kDists[i]->GetStateKey();
82 ASSERT_EQ(ERROR_SUCCESS, update_key.Create(root, path.c_str(),
83 KEY_WRITE));
84 ASSERT_EQ(ERROR_SUCCESS, update_key.WriteValue(L"ap", value));
87 // Make the product technically multi-install.
88 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
89 ASSERT_EQ(ERROR_SUCCESS,
90 update_key.Create(root, dist->GetStateKey().c_str(), KEY_WRITE));
91 ASSERT_EQ(ERROR_SUCCESS,
92 update_key.WriteValue(installer::kUninstallArgumentsField,
93 L"--multi-install"));
96 // Tests setting the ap= value to various combinations of values with
97 // prefixes and suffixes, while asserting on the correct channel value.
98 // Note that any non-empty ap= value that doesn't match ".*-{dev|beta}.*"
99 // will return the "unknown" channel.
100 void TestCurrentChromeChannelWithVariousApValues(SystemUserInstall install) {
101 static struct Expectations {
102 const wchar_t* ap_value;
103 const wchar_t* channel;
104 } expectations[] = {
105 { L"dev", installer::kChromeChannelDev },
106 { L"-dev", installer::kChromeChannelDev },
107 { L"-developer", installer::kChromeChannelDev },
108 { L"beta", installer::kChromeChannelBeta },
109 { L"-beta", installer::kChromeChannelBeta },
110 { L"-betamax", installer::kChromeChannelBeta },
112 bool is_system = install == SYSTEM_INSTALL;
113 const wchar_t* prefixes[] = {
114 L"",
115 L"prefix",
116 L"prefix-with-dash",
118 const wchar_t* suffixes[] = {
119 L"",
120 L"suffix",
121 L"suffix-with-dash",
124 for (size_t i = 0; i < arraysize(prefixes); ++i) {
125 for (size_t j = 0; j < arraysize(expectations); ++j) {
126 for (size_t k = 0; k < arraysize(suffixes); ++k) {
127 base::string16 ap = prefixes[i];
128 ap += expectations[j].ap_value;
129 ap += suffixes[k];
130 const wchar_t* channel = expectations[j].channel;
132 SetApField(install, ap.c_str());
133 base::string16 ret_channel;
135 EXPECT_TRUE(GoogleUpdateSettings::GetChromeChannelAndModifiers(
136 is_system, &ret_channel));
137 EXPECT_STREQ(channel, ret_channel.c_str())
138 << "Expecting channel \"" << channel
139 << "\" for ap=\"" << ap << "\"";
145 // Test the writing and deleting functionality of the experiments label
146 // helper.
147 void TestExperimentsLabelHelper(SystemUserInstall install) {
148 BrowserDistribution* chrome =
149 BrowserDistribution::GetSpecificDistribution(
150 BrowserDistribution::CHROME_BROWSER);
151 base::string16 value;
152 #if defined(GOOGLE_CHROME_BUILD)
153 EXPECT_TRUE(chrome->ShouldSetExperimentLabels());
155 // Before anything is set, ReadExperimentLabels should succeed but return
156 // an empty string.
157 EXPECT_TRUE(GoogleUpdateSettings::ReadExperimentLabels(
158 install == SYSTEM_INSTALL, &value));
159 EXPECT_EQ(base::string16(), value);
161 EXPECT_TRUE(GoogleUpdateSettings::SetExperimentLabels(
162 install == SYSTEM_INSTALL, kTestExperimentLabel));
164 // Validate that something is written. Only worry about the label itself.
165 RegKey key;
166 HKEY root = install == SYSTEM_INSTALL ?
167 HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER;
168 base::string16 state_key = install == SYSTEM_INSTALL ?
169 chrome->GetStateMediumKey() : chrome->GetStateKey();
171 EXPECT_EQ(ERROR_SUCCESS,
172 key.Open(root, state_key.c_str(), KEY_QUERY_VALUE));
173 EXPECT_EQ(ERROR_SUCCESS,
174 key.ReadValue(google_update::kExperimentLabels, &value));
175 EXPECT_EQ(kTestExperimentLabel, value);
176 EXPECT_TRUE(GoogleUpdateSettings::ReadExperimentLabels(
177 install == SYSTEM_INSTALL, &value));
178 EXPECT_EQ(kTestExperimentLabel, value);
179 key.Close();
181 // Now that the label is set, test the delete functionality. An empty label
182 // should result in deleting the value.
183 EXPECT_TRUE(GoogleUpdateSettings::SetExperimentLabels(
184 install == SYSTEM_INSTALL, base::string16()));
185 EXPECT_EQ(ERROR_SUCCESS,
186 key.Open(root, state_key.c_str(), KEY_QUERY_VALUE));
187 EXPECT_EQ(ERROR_FILE_NOT_FOUND,
188 key.ReadValue(google_update::kExperimentLabels, &value));
189 EXPECT_TRUE(GoogleUpdateSettings::ReadExperimentLabels(
190 install == SYSTEM_INSTALL, &value));
191 EXPECT_EQ(base::string16(), value);
192 key.Close();
193 #else
194 EXPECT_FALSE(chrome->ShouldSetExperimentLabels());
195 EXPECT_FALSE(GoogleUpdateSettings::ReadExperimentLabels(
196 install == SYSTEM_INSTALL, &value));
197 #endif // GOOGLE_CHROME_BUILD
200 // Creates "ap" key with the value given as parameter. Also adds work
201 // items to work_item_list given so that they can be rolled back later.
202 bool CreateApKey(WorkItemList* work_item_list, const base::string16& value) {
203 HKEY reg_root = HKEY_CURRENT_USER;
204 base::string16 reg_key = GetApKeyPath();
205 work_item_list->AddCreateRegKeyWorkItem(
206 reg_root, reg_key, WorkItem::kWow64Default);
207 work_item_list->AddSetRegValueWorkItem(reg_root,
208 reg_key,
209 WorkItem::kWow64Default,
210 google_update::kRegApField,
211 value.c_str(),
212 true);
213 if (!work_item_list->Do()) {
214 work_item_list->Rollback();
215 return false;
217 return true;
220 // Returns the key path of "ap" key, e.g.:
221 // Google\Update\ClientState\<kTestProductGuid>
222 base::string16 GetApKeyPath() {
223 base::string16 reg_key(google_update::kRegPathClientState);
224 reg_key.append(L"\\");
225 reg_key.append(kTestProductGuid);
226 return reg_key;
229 // Utility method to read "ap" key value
230 base::string16 ReadApKeyValue() {
231 RegKey key;
232 base::string16 ap_key_value;
233 base::string16 reg_key = GetApKeyPath();
234 if (key.Open(HKEY_CURRENT_USER, reg_key.c_str(), KEY_ALL_ACCESS) ==
235 ERROR_SUCCESS) {
236 key.ReadValue(google_update::kRegApField, &ap_key_value);
239 return ap_key_value;
242 bool SetUpdatePolicyForAppGuid(const base::string16& app_guid,
243 GoogleUpdateSettings::UpdatePolicy policy) {
244 RegKey policy_key;
245 if (policy_key.Create(HKEY_LOCAL_MACHINE,
246 GoogleUpdateSettings::kPoliciesKey,
247 KEY_SET_VALUE) == ERROR_SUCCESS) {
248 base::string16 app_update_override(
249 GoogleUpdateSettings::kUpdateOverrideValuePrefix);
250 app_update_override.append(app_guid);
251 return policy_key.WriteValue(app_update_override.c_str(),
252 static_cast<DWORD>(policy)) == ERROR_SUCCESS;
254 return false;
257 GoogleUpdateSettings::UpdatePolicy GetUpdatePolicyForAppGuid(
258 const base::string16& app_guid) {
259 RegKey policy_key;
260 if (policy_key.Create(HKEY_LOCAL_MACHINE,
261 GoogleUpdateSettings::kPoliciesKey,
262 KEY_QUERY_VALUE) == ERROR_SUCCESS) {
263 base::string16 app_update_override(
264 GoogleUpdateSettings::kUpdateOverrideValuePrefix);
265 app_update_override.append(app_guid);
267 DWORD value;
268 if (policy_key.ReadValueDW(app_update_override.c_str(),
269 &value) == ERROR_SUCCESS) {
270 return static_cast<GoogleUpdateSettings::UpdatePolicy>(value);
273 return GoogleUpdateSettings::UPDATE_POLICIES_COUNT;
276 bool SetGlobalUpdatePolicy(GoogleUpdateSettings::UpdatePolicy policy) {
277 RegKey policy_key;
278 return policy_key.Create(HKEY_LOCAL_MACHINE,
279 GoogleUpdateSettings::kPoliciesKey,
280 KEY_SET_VALUE) == ERROR_SUCCESS &&
281 policy_key.WriteValue(GoogleUpdateSettings::kUpdatePolicyValue,
282 static_cast<DWORD>(policy)) == ERROR_SUCCESS;
285 GoogleUpdateSettings::UpdatePolicy GetGlobalUpdatePolicy() {
286 RegKey policy_key;
287 DWORD value;
288 return (policy_key.Create(HKEY_LOCAL_MACHINE,
289 GoogleUpdateSettings::kPoliciesKey,
290 KEY_QUERY_VALUE) == ERROR_SUCCESS &&
291 policy_key.ReadValueDW(GoogleUpdateSettings::kUpdatePolicyValue,
292 &value) == ERROR_SUCCESS) ?
293 static_cast<GoogleUpdateSettings::UpdatePolicy>(value) :
294 GoogleUpdateSettings::UPDATE_POLICIES_COUNT;
297 bool SetUpdateTimeoutOverride(DWORD time_in_minutes) {
298 RegKey policy_key;
299 return policy_key.Create(HKEY_LOCAL_MACHINE,
300 GoogleUpdateSettings::kPoliciesKey,
301 KEY_SET_VALUE) == ERROR_SUCCESS &&
302 policy_key.WriteValue(
303 GoogleUpdateSettings::kCheckPeriodOverrideMinutes,
304 time_in_minutes) == ERROR_SUCCESS;
307 // Path overrides so that SHGetFolderPath isn't needed after the registry
308 // is overridden.
309 base::ScopedPathOverride program_files_override_;
310 base::ScopedPathOverride program_files_x86_override_;
311 registry_util::RegistryOverrideManager registry_overrides_;
314 } // namespace
316 // Verify that we return success on no registration (which means stable),
317 // whether per-system or per-user install.
318 TEST_F(GoogleUpdateSettingsTest, CurrentChromeChannelAbsent) {
319 // Per-system first.
320 base::string16 channel;
321 EXPECT_TRUE(GoogleUpdateSettings::GetChromeChannelAndModifiers(true,
322 &channel));
323 EXPECT_STREQ(L"", channel.c_str());
325 // Then per-user.
326 EXPECT_TRUE(GoogleUpdateSettings::GetChromeChannelAndModifiers(false,
327 &channel));
328 EXPECT_STREQ(L"", channel.c_str());
331 // Test an empty Ap key for system and user.
332 TEST_F(GoogleUpdateSettingsTest, CurrentChromeChannelEmptySystem) {
333 SetApField(SYSTEM_INSTALL, L"");
334 base::string16 channel;
335 EXPECT_TRUE(GoogleUpdateSettings::GetChromeChannelAndModifiers(true,
336 &channel));
337 EXPECT_STREQ(L"", channel.c_str());
339 // Per-user lookups still succeed and return empty string.
340 EXPECT_TRUE(GoogleUpdateSettings::GetChromeChannelAndModifiers(false,
341 &channel));
342 EXPECT_STREQ(L"", channel.c_str());
345 TEST_F(GoogleUpdateSettingsTest, CurrentChromeChannelEmptyUser) {
346 SetApField(USER_INSTALL, L"");
347 // Per-system lookups still succeed and return empty string.
348 base::string16 channel;
349 EXPECT_TRUE(GoogleUpdateSettings::GetChromeChannelAndModifiers(true,
350 &channel));
351 EXPECT_STREQ(L"", channel.c_str());
353 // Per-user lookup should succeed.
354 EXPECT_TRUE(GoogleUpdateSettings::GetChromeChannelAndModifiers(false,
355 &channel));
356 EXPECT_STREQ(L"", channel.c_str());
359 // Test that the channel is pulled from the binaries for multi-install products.
360 TEST_F(GoogleUpdateSettingsTest, MultiInstallChannelFromBinaries) {
361 SetMultiApField(USER_INSTALL, L"2.0-dev-multi-chrome");
362 base::string16 channel;
364 EXPECT_TRUE(GoogleUpdateSettings::GetChromeChannelAndModifiers(false,
365 &channel));
366 EXPECT_STREQ(L"dev-m", channel.c_str());
368 // See if the same happens if the product's ap is cleared.
369 SetApField(USER_INSTALL, L"");
370 EXPECT_TRUE(GoogleUpdateSettings::GetChromeChannelAndModifiers(false,
371 &channel));
372 EXPECT_STREQ(L"dev-m", channel.c_str());
374 // Test the converse (binaries are stable, Chrome is other).
375 SetMultiApField(USER_INSTALL, L"-multi-chrome");
376 SetApField(USER_INSTALL, L"2.0-dev-multi-chrome");
377 EXPECT_TRUE(GoogleUpdateSettings::GetChromeChannelAndModifiers(false,
378 &channel));
379 EXPECT_STREQ(L"m", channel.c_str());
382 TEST_F(GoogleUpdateSettingsTest, CurrentChromeChannelVariousApValuesSystem) {
383 TestCurrentChromeChannelWithVariousApValues(SYSTEM_INSTALL);
386 TEST_F(GoogleUpdateSettingsTest, CurrentChromeChannelVariousApValuesUser) {
387 TestCurrentChromeChannelWithVariousApValues(USER_INSTALL);
390 // Run through all combinations of diff vs. full install, single vs. multi
391 // install, success and failure results, and a fistful of initial "ap" values
392 // checking that the expected final "ap" value is generated by
393 // GoogleUpdateSettings::UpdateGoogleUpdateApKey.
394 TEST_F(GoogleUpdateSettingsTest, UpdateGoogleUpdateApKey) {
395 const installer::ArchiveType archive_types[] = {
396 installer::UNKNOWN_ARCHIVE_TYPE,
397 installer::FULL_ARCHIVE_TYPE,
398 installer::INCREMENTAL_ARCHIVE_TYPE
400 const int results[] = {
401 installer::FIRST_INSTALL_SUCCESS,
402 installer::INSTALL_FAILED
404 const wchar_t* const plain[] = {
405 L"",
406 L"1.1",
407 L"1.1-dev"
409 const wchar_t* const full[] = {
410 L"-full",
411 L"1.1-full",
412 L"1.1-dev-full"
414 COMPILE_ASSERT(arraysize(full) == arraysize(plain), bad_full_array_size);
415 const wchar_t* const multifail[] = {
416 L"-multifail",
417 L"1.1-multifail",
418 L"1.1-dev-multifail"
420 COMPILE_ASSERT(arraysize(multifail) == arraysize(plain),
421 bad_multifail_array_size);
422 const wchar_t* const multifail_full[] = {
423 L"-multifail-full",
424 L"1.1-multifail-full",
425 L"1.1-dev-multifail-full"
427 COMPILE_ASSERT(arraysize(multifail_full) == arraysize(plain),
428 bad_multifail_full_array_size);
429 const wchar_t* const* input_arrays[] = {
430 plain,
431 full,
432 multifail,
433 multifail_full
435 ChannelInfo v;
436 for (int type_idx = 0; type_idx < arraysize(archive_types); ++type_idx) {
437 const installer::ArchiveType archive_type = archive_types[type_idx];
438 for (int result_idx = 0; result_idx < arraysize(results); ++result_idx) {
439 const int result = results[result_idx];
440 // The archive type will/must always be known on install success.
441 if (archive_type == installer::UNKNOWN_ARCHIVE_TYPE &&
442 result == installer::FIRST_INSTALL_SUCCESS) {
443 continue;
445 const wchar_t* const* outputs = NULL;
446 if (result == installer::FIRST_INSTALL_SUCCESS ||
447 archive_type == installer::FULL_ARCHIVE_TYPE) {
448 outputs = plain;
449 } else if (archive_type == installer::INCREMENTAL_ARCHIVE_TYPE) {
450 outputs = full;
451 } // else if (archive_type == UNKNOWN) see below
453 for (int inputs_idx = 0; inputs_idx < arraysize(input_arrays);
454 ++inputs_idx) {
455 const wchar_t* const* inputs = input_arrays[inputs_idx];
456 if (archive_type == installer::UNKNOWN_ARCHIVE_TYPE) {
457 // "-full" is untouched if the archive type is unknown.
458 // "-multifail" is unconditionally removed.
459 if (inputs == full || inputs == multifail_full)
460 outputs = full;
461 else
462 outputs = plain;
464 for (int input_idx = 0; input_idx < arraysize(plain); ++input_idx) {
465 const wchar_t* input = inputs[input_idx];
466 const wchar_t* output = outputs[input_idx];
468 v.set_value(input);
469 if (output == v.value()) {
470 EXPECT_FALSE(GoogleUpdateSettings::UpdateGoogleUpdateApKey(
471 archive_type, result, &v))
472 << "archive_type: " << archive_type
473 << ", result: " << result
474 << ", input ap value: " << input;
475 } else {
476 EXPECT_TRUE(GoogleUpdateSettings::UpdateGoogleUpdateApKey(
477 archive_type, result, &v))
478 << "archive_type: " << archive_type
479 << ", result: " << result
480 << ", input ap value: " << input;
482 EXPECT_EQ(output, v.value())
483 << "archive_type: " << archive_type
484 << ", result: " << result
485 << ", input ap value: " << input;
492 TEST_F(GoogleUpdateSettingsTest, UpdateInstallStatusTest) {
493 scoped_ptr<WorkItemList> work_item_list(WorkItem::CreateWorkItemList());
494 // Test incremental install failure
495 ASSERT_TRUE(CreateApKey(work_item_list.get(), L""))
496 << "Failed to create ap key.";
497 GoogleUpdateSettings::UpdateInstallStatus(false,
498 installer::INCREMENTAL_ARCHIVE_TYPE,
499 installer::INSTALL_FAILED,
500 kTestProductGuid);
501 EXPECT_STREQ(ReadApKeyValue().c_str(), L"-full");
502 work_item_list->Rollback();
504 work_item_list.reset(WorkItem::CreateWorkItemList());
505 // Test incremental install success
506 ASSERT_TRUE(CreateApKey(work_item_list.get(), L""))
507 << "Failed to create ap key.";
508 GoogleUpdateSettings::UpdateInstallStatus(false,
509 installer::INCREMENTAL_ARCHIVE_TYPE,
510 installer::FIRST_INSTALL_SUCCESS,
511 kTestProductGuid);
512 EXPECT_STREQ(ReadApKeyValue().c_str(), L"");
513 work_item_list->Rollback();
515 work_item_list.reset(WorkItem::CreateWorkItemList());
516 // Test full install failure
517 ASSERT_TRUE(CreateApKey(work_item_list.get(), L"-full"))
518 << "Failed to create ap key.";
519 GoogleUpdateSettings::UpdateInstallStatus(false, installer::FULL_ARCHIVE_TYPE,
520 installer::INSTALL_FAILED,
521 kTestProductGuid);
522 EXPECT_STREQ(ReadApKeyValue().c_str(), L"");
523 work_item_list->Rollback();
525 work_item_list.reset(WorkItem::CreateWorkItemList());
526 // Test full install success
527 ASSERT_TRUE(CreateApKey(work_item_list.get(), L"-full"))
528 << "Failed to create ap key.";
529 GoogleUpdateSettings::UpdateInstallStatus(false, installer::FULL_ARCHIVE_TYPE,
530 installer::FIRST_INSTALL_SUCCESS,
531 kTestProductGuid);
532 EXPECT_STREQ(ReadApKeyValue().c_str(), L"");
533 work_item_list->Rollback();
535 work_item_list.reset(WorkItem::CreateWorkItemList());
536 // Test the case of when "ap" key doesnt exist at all
537 base::string16 ap_key_value = ReadApKeyValue();
538 base::string16 reg_key = GetApKeyPath();
539 HKEY reg_root = HKEY_CURRENT_USER;
540 bool ap_key_deleted = false;
541 RegKey key;
542 if (key.Open(HKEY_CURRENT_USER, reg_key.c_str(), KEY_ALL_ACCESS) !=
543 ERROR_SUCCESS) {
544 work_item_list->AddCreateRegKeyWorkItem(
545 reg_root, reg_key, WorkItem::kWow64Default);
546 ASSERT_TRUE(work_item_list->Do()) << "Failed to create ClientState key.";
547 } else if (key.DeleteValue(google_update::kRegApField) == ERROR_SUCCESS) {
548 ap_key_deleted = true;
550 // try differential installer
551 GoogleUpdateSettings::UpdateInstallStatus(false,
552 installer::INCREMENTAL_ARCHIVE_TYPE,
553 installer::INSTALL_FAILED,
554 kTestProductGuid);
555 EXPECT_STREQ(ReadApKeyValue().c_str(), L"-full");
556 // try full installer now
557 GoogleUpdateSettings::UpdateInstallStatus(false, installer::FULL_ARCHIVE_TYPE,
558 installer::INSTALL_FAILED,
559 kTestProductGuid);
560 EXPECT_STREQ(ReadApKeyValue().c_str(), L"");
561 // Now cleanup to leave the system in unchanged state.
562 // - Diff installer creates an ap key if it didnt exist, so delete this ap key
563 // - If we created any reg key path for ap, roll it back
564 // - Finally restore the original value of ap key.
565 key.Open(HKEY_CURRENT_USER, reg_key.c_str(), KEY_ALL_ACCESS);
566 key.DeleteValue(google_update::kRegApField);
567 work_item_list->Rollback();
568 if (ap_key_deleted) {
569 work_item_list.reset(WorkItem::CreateWorkItemList());
570 ASSERT_TRUE(CreateApKey(work_item_list.get(), ap_key_value))
571 << "Failed to restore ap key.";
575 TEST_F(GoogleUpdateSettingsTest, SetEULAConsent) {
576 using installer::FakeInstallationState;
578 const bool multi_install = true;
579 const bool system_level = true;
580 FakeInstallationState machine_state;
582 // Chrome is installed.
583 machine_state.AddChrome(system_level, multi_install,
584 new Version(chrome::kChromeVersion));
586 RegKey key;
587 DWORD value;
588 BrowserDistribution* binaries =
589 BrowserDistribution::GetSpecificDistribution(
590 BrowserDistribution::CHROME_BINARIES);
591 BrowserDistribution* chrome =
592 BrowserDistribution::GetSpecificDistribution(
593 BrowserDistribution::CHROME_BROWSER);
595 // eulaconsent is set on both the product and the binaries.
596 EXPECT_TRUE(GoogleUpdateSettings::SetEULAConsent(machine_state, chrome,
597 true));
598 EXPECT_EQ(ERROR_SUCCESS,
599 key.Open(HKEY_LOCAL_MACHINE, binaries->GetStateMediumKey().c_str(),
600 KEY_QUERY_VALUE));
601 EXPECT_EQ(ERROR_SUCCESS,
602 key.ReadValueDW(google_update::kRegEULAAceptedField, &value));
603 EXPECT_EQ(1U, value);
604 EXPECT_EQ(ERROR_SUCCESS,
605 key.Open(HKEY_LOCAL_MACHINE, chrome->GetStateMediumKey().c_str(),
606 KEY_QUERY_VALUE));
607 EXPECT_EQ(ERROR_SUCCESS,
608 key.ReadValueDW(google_update::kRegEULAAceptedField, &value));
609 EXPECT_EQ(1U, value);
612 // Test that the appropriate default is returned if no update override is
613 // present.
614 TEST_F(GoogleUpdateSettingsTest, GetAppUpdatePolicyNoOverride) {
615 // There are no policies at all.
616 EXPECT_EQ(ERROR_FILE_NOT_FOUND,
617 RegKey().Open(HKEY_LOCAL_MACHINE,
618 GoogleUpdateSettings::kPoliciesKey,
619 KEY_QUERY_VALUE));
620 bool is_overridden = true;
621 EXPECT_EQ(GoogleUpdateSettings::kDefaultUpdatePolicy,
622 GoogleUpdateSettings::GetAppUpdatePolicy(kTestProductGuid,
623 &is_overridden));
624 EXPECT_FALSE(is_overridden);
626 // The policy key exists, but there are no values of interest present.
627 EXPECT_EQ(ERROR_SUCCESS,
628 RegKey().Create(HKEY_LOCAL_MACHINE,
629 GoogleUpdateSettings::kPoliciesKey,
630 KEY_SET_VALUE));
631 EXPECT_EQ(ERROR_SUCCESS,
632 RegKey().Open(HKEY_LOCAL_MACHINE,
633 GoogleUpdateSettings::kPoliciesKey,
634 KEY_QUERY_VALUE));
635 is_overridden = true;
636 EXPECT_EQ(GoogleUpdateSettings::kDefaultUpdatePolicy,
637 GoogleUpdateSettings::GetAppUpdatePolicy(kTestProductGuid,
638 &is_overridden));
639 EXPECT_FALSE(is_overridden);
642 TEST_F(GoogleUpdateSettingsTest, UpdateProfileCountsSystemInstall) {
643 // Override FILE_MODULE and FILE_EXE with a path somewhere in the default
644 // system-level install location so that
645 // GoogleUpdateSettings::IsSystemInstall() returns true.
646 base::FilePath file_exe;
647 ASSERT_TRUE(PathService::Get(base::FILE_EXE, &file_exe));
648 base::FilePath install_dir(installer::GetChromeInstallPath(
649 true /* system_install */, BrowserDistribution::GetDistribution()));
650 file_exe = install_dir.Append(file_exe.BaseName());
651 base::ScopedPathOverride file_module_override(
652 base::FILE_MODULE, file_exe, true /* is_absolute */, false /* create */);
653 base::ScopedPathOverride file_exe_override(
654 base::FILE_EXE, file_exe, true /* is_absolute */, false /* create */);
656 // No profile count keys present yet.
657 const base::string16& state_key = BrowserDistribution::GetDistribution()->
658 GetAppRegistrationData().GetStateMediumKey();
659 base::string16 num_profiles_path(state_key);
660 num_profiles_path.append(L"\\");
661 num_profiles_path.append(google_update::kRegProfilesActive);
662 base::string16 num_signed_in_path(state_key);
663 num_signed_in_path.append(L"\\");
664 num_signed_in_path.append(google_update::kRegProfilesSignedIn);
666 EXPECT_EQ(ERROR_FILE_NOT_FOUND,
667 RegKey().Open(HKEY_LOCAL_MACHINE,
668 num_profiles_path.c_str(),
669 KEY_QUERY_VALUE));
670 EXPECT_EQ(ERROR_FILE_NOT_FOUND,
671 RegKey().Open(HKEY_LOCAL_MACHINE,
672 num_signed_in_path.c_str(),
673 KEY_QUERY_VALUE));
675 // Show time! Write the values.
676 GoogleUpdateSettings::UpdateProfileCounts(3, 2);
678 // Verify the keys were created.
679 EXPECT_EQ(ERROR_SUCCESS,
680 RegKey().Open(HKEY_LOCAL_MACHINE,
681 num_profiles_path.c_str(),
682 KEY_QUERY_VALUE));
683 EXPECT_EQ(ERROR_SUCCESS,
684 RegKey().Open(HKEY_LOCAL_MACHINE,
685 num_signed_in_path.c_str(),
686 KEY_QUERY_VALUE));
688 base::string16 uniquename;
689 EXPECT_TRUE(base::win::GetUserSidString(&uniquename));
691 // Verify the values are accessible.
692 DWORD num_profiles = 0;
693 DWORD num_signed_in = 0;
694 base::string16 aggregate;
695 EXPECT_EQ(
696 ERROR_SUCCESS,
697 RegKey(HKEY_LOCAL_MACHINE, num_profiles_path.c_str(),
698 KEY_QUERY_VALUE).ReadValueDW(uniquename.c_str(),
699 &num_profiles));
700 EXPECT_EQ(
701 ERROR_SUCCESS,
702 RegKey(HKEY_LOCAL_MACHINE, num_signed_in_path.c_str(),
703 KEY_QUERY_VALUE).ReadValueDW(uniquename.c_str(),
704 &num_signed_in));
705 EXPECT_EQ(
706 ERROR_SUCCESS,
707 RegKey(HKEY_LOCAL_MACHINE, num_signed_in_path.c_str(),
708 KEY_QUERY_VALUE).ReadValue(google_update::kRegAggregateMethod,
709 &aggregate));
711 // Verify the correct values were written.
712 EXPECT_EQ(3, num_profiles);
713 EXPECT_EQ(2, num_signed_in);
714 EXPECT_EQ(L"sum()", aggregate);
717 TEST_F(GoogleUpdateSettingsTest, UpdateProfileCountsUserInstall) {
718 // Unit tests never operate as an installed application, so will never
719 // be a system install.
721 // No profile count values present yet.
722 const base::string16& state_key = BrowserDistribution::GetDistribution()->
723 GetAppRegistrationData().GetStateKey();
725 EXPECT_EQ(ERROR_FILE_NOT_FOUND,
726 RegKey().Open(HKEY_CURRENT_USER,
727 state_key.c_str(),
728 KEY_QUERY_VALUE));
730 // Show time! Write the values.
731 GoogleUpdateSettings::UpdateProfileCounts(4, 1);
733 // Verify the key was created.
734 EXPECT_EQ(ERROR_SUCCESS,
735 RegKey().Open(HKEY_CURRENT_USER,
736 state_key.c_str(),
737 KEY_QUERY_VALUE));
739 // Verify the values are accessible.
740 base::string16 num_profiles;
741 base::string16 num_signed_in;
742 EXPECT_EQ(
743 ERROR_SUCCESS,
744 RegKey(HKEY_CURRENT_USER, state_key.c_str(), KEY_QUERY_VALUE).
745 ReadValue(google_update::kRegProfilesActive, &num_profiles));
746 EXPECT_EQ(
747 ERROR_SUCCESS,
748 RegKey(HKEY_CURRENT_USER, state_key.c_str(), KEY_QUERY_VALUE).
749 ReadValue(google_update::kRegProfilesSignedIn, &num_signed_in));
751 // Verify the correct values were written.
752 EXPECT_EQ(L"4", num_profiles);
753 EXPECT_EQ(L"1", num_signed_in);
756 #if defined(GOOGLE_CHROME_BUILD)
758 // Test that the default override is returned if no app-specific override is
759 // present.
760 TEST_F(GoogleUpdateSettingsTest, GetAppUpdatePolicyDefaultOverride) {
761 EXPECT_EQ(ERROR_SUCCESS,
762 RegKey(HKEY_LOCAL_MACHINE, GoogleUpdateSettings::kPoliciesKey,
763 KEY_SET_VALUE).WriteValue(
764 GoogleUpdateSettings::kUpdatePolicyValue,
765 static_cast<DWORD>(0)));
766 bool is_overridden = true;
767 EXPECT_EQ(GoogleUpdateSettings::UPDATES_DISABLED,
768 GoogleUpdateSettings::GetAppUpdatePolicy(kTestProductGuid,
769 &is_overridden));
770 EXPECT_FALSE(is_overridden);
772 EXPECT_EQ(ERROR_SUCCESS,
773 RegKey(HKEY_LOCAL_MACHINE, GoogleUpdateSettings::kPoliciesKey,
774 KEY_SET_VALUE).WriteValue(
775 GoogleUpdateSettings::kUpdatePolicyValue,
776 static_cast<DWORD>(1)));
777 is_overridden = true;
778 EXPECT_EQ(GoogleUpdateSettings::AUTOMATIC_UPDATES,
779 GoogleUpdateSettings::GetAppUpdatePolicy(kTestProductGuid,
780 &is_overridden));
781 EXPECT_FALSE(is_overridden);
783 EXPECT_EQ(ERROR_SUCCESS,
784 RegKey(HKEY_LOCAL_MACHINE, GoogleUpdateSettings::kPoliciesKey,
785 KEY_SET_VALUE).WriteValue(
786 GoogleUpdateSettings::kUpdatePolicyValue,
787 static_cast<DWORD>(2)));
788 is_overridden = true;
789 EXPECT_EQ(GoogleUpdateSettings::MANUAL_UPDATES_ONLY,
790 GoogleUpdateSettings::GetAppUpdatePolicy(kTestProductGuid,
791 &is_overridden));
792 EXPECT_FALSE(is_overridden);
794 EXPECT_EQ(ERROR_SUCCESS,
795 RegKey(HKEY_LOCAL_MACHINE, GoogleUpdateSettings::kPoliciesKey,
796 KEY_SET_VALUE).WriteValue(
797 GoogleUpdateSettings::kUpdatePolicyValue,
798 static_cast<DWORD>(3)));
799 is_overridden = true;
800 EXPECT_EQ(GoogleUpdateSettings::AUTO_UPDATES_ONLY,
801 GoogleUpdateSettings::GetAppUpdatePolicy(kTestProductGuid,
802 &is_overridden));
803 EXPECT_FALSE(is_overridden);
805 // The default policy should be in force for bogus values.
806 EXPECT_EQ(ERROR_SUCCESS,
807 RegKey(HKEY_LOCAL_MACHINE, GoogleUpdateSettings::kPoliciesKey,
808 KEY_SET_VALUE).WriteValue(
809 GoogleUpdateSettings::kUpdatePolicyValue,
810 static_cast<DWORD>(4)));
811 is_overridden = true;
812 EXPECT_EQ(GoogleUpdateSettings::kDefaultUpdatePolicy,
813 GoogleUpdateSettings::GetAppUpdatePolicy(kTestProductGuid,
814 &is_overridden));
815 EXPECT_FALSE(is_overridden);
818 // Test that an app-specific override is used if present.
819 TEST_F(GoogleUpdateSettingsTest, GetAppUpdatePolicyAppOverride) {
820 base::string16 app_policy_value(
821 GoogleUpdateSettings::kUpdateOverrideValuePrefix);
822 app_policy_value.append(kTestProductGuid);
824 EXPECT_EQ(ERROR_SUCCESS,
825 RegKey(HKEY_LOCAL_MACHINE, GoogleUpdateSettings::kPoliciesKey,
826 KEY_SET_VALUE).WriteValue(
827 GoogleUpdateSettings::kUpdatePolicyValue,
828 static_cast<DWORD>(1)));
829 EXPECT_EQ(ERROR_SUCCESS,
830 RegKey(HKEY_LOCAL_MACHINE, GoogleUpdateSettings::kPoliciesKey,
831 KEY_SET_VALUE).WriteValue(app_policy_value.c_str(),
832 static_cast<DWORD>(0)));
833 bool is_overridden = false;
834 EXPECT_EQ(GoogleUpdateSettings::UPDATES_DISABLED,
835 GoogleUpdateSettings::GetAppUpdatePolicy(kTestProductGuid,
836 &is_overridden));
837 EXPECT_TRUE(is_overridden);
839 EXPECT_EQ(ERROR_SUCCESS,
840 RegKey(HKEY_LOCAL_MACHINE, GoogleUpdateSettings::kPoliciesKey,
841 KEY_SET_VALUE).WriteValue(
842 GoogleUpdateSettings::kUpdatePolicyValue,
843 static_cast<DWORD>(0)));
844 EXPECT_EQ(ERROR_SUCCESS,
845 RegKey(HKEY_LOCAL_MACHINE, GoogleUpdateSettings::kPoliciesKey,
846 KEY_SET_VALUE).WriteValue(app_policy_value.c_str(),
847 static_cast<DWORD>(1)));
848 is_overridden = false;
849 EXPECT_EQ(GoogleUpdateSettings::AUTOMATIC_UPDATES,
850 GoogleUpdateSettings::GetAppUpdatePolicy(kTestProductGuid,
851 &is_overridden));
852 EXPECT_TRUE(is_overridden);
854 EXPECT_EQ(ERROR_SUCCESS,
855 RegKey(HKEY_LOCAL_MACHINE, GoogleUpdateSettings::kPoliciesKey,
856 KEY_SET_VALUE).WriteValue(app_policy_value.c_str(),
857 static_cast<DWORD>(2)));
858 is_overridden = false;
859 EXPECT_EQ(GoogleUpdateSettings::MANUAL_UPDATES_ONLY,
860 GoogleUpdateSettings::GetAppUpdatePolicy(kTestProductGuid,
861 &is_overridden));
862 EXPECT_TRUE(is_overridden);
864 EXPECT_EQ(ERROR_SUCCESS,
865 RegKey(HKEY_LOCAL_MACHINE, GoogleUpdateSettings::kPoliciesKey,
866 KEY_SET_VALUE).WriteValue(app_policy_value.c_str(),
867 static_cast<DWORD>(3)));
868 is_overridden = false;
869 EXPECT_EQ(GoogleUpdateSettings::AUTO_UPDATES_ONLY,
870 GoogleUpdateSettings::GetAppUpdatePolicy(kTestProductGuid,
871 &is_overridden));
872 EXPECT_TRUE(is_overridden);
874 // The default policy should be in force for bogus values.
875 EXPECT_EQ(ERROR_SUCCESS,
876 RegKey(HKEY_LOCAL_MACHINE, GoogleUpdateSettings::kPoliciesKey,
877 KEY_SET_VALUE).WriteValue(app_policy_value.c_str(),
878 static_cast<DWORD>(4)));
879 is_overridden = true;
880 EXPECT_EQ(GoogleUpdateSettings::UPDATES_DISABLED,
881 GoogleUpdateSettings::GetAppUpdatePolicy(kTestProductGuid,
882 &is_overridden));
883 EXPECT_FALSE(is_overridden);
886 TEST_F(GoogleUpdateSettingsTest, PerAppUpdatesDisabledByPolicy) {
887 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
888 EXPECT_TRUE(
889 SetUpdatePolicyForAppGuid(dist->GetAppGuid(),
890 GoogleUpdateSettings::UPDATES_DISABLED));
891 bool is_overridden = false;
892 GoogleUpdateSettings::UpdatePolicy update_policy =
893 GoogleUpdateSettings::GetAppUpdatePolicy(dist->GetAppGuid(),
894 &is_overridden);
895 EXPECT_TRUE(is_overridden);
896 EXPECT_EQ(GoogleUpdateSettings::UPDATES_DISABLED, update_policy);
897 EXPECT_FALSE(GoogleUpdateSettings::AreAutoupdatesEnabled());
899 EXPECT_TRUE(GoogleUpdateSettings::ReenableAutoupdates());
900 update_policy = GoogleUpdateSettings::GetAppUpdatePolicy(dist->GetAppGuid(),
901 &is_overridden);
902 // Should still have a policy but now that policy should explicitly enable
903 // updates.
904 EXPECT_TRUE(is_overridden);
905 EXPECT_EQ(GoogleUpdateSettings::AUTOMATIC_UPDATES, update_policy);
906 EXPECT_TRUE(GoogleUpdateSettings::AreAutoupdatesEnabled());
909 TEST_F(GoogleUpdateSettingsTest, PerAppUpdatesEnabledWithGlobalDisabled) {
910 // Disable updates globally but enable them for Chrome (the app-specific
911 // setting should take precedence).
912 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
913 BrowserDistribution* binaries = BrowserDistribution::GetSpecificDistribution(
914 BrowserDistribution::CHROME_BINARIES);
915 EXPECT_TRUE(
916 SetUpdatePolicyForAppGuid(dist->GetAppGuid(),
917 GoogleUpdateSettings::AUTOMATIC_UPDATES));
918 EXPECT_TRUE(
919 SetUpdatePolicyForAppGuid(binaries->GetAppGuid(),
920 GoogleUpdateSettings::AUTOMATIC_UPDATES));
921 EXPECT_TRUE(SetGlobalUpdatePolicy(GoogleUpdateSettings::UPDATES_DISABLED));
923 // Make sure we read this as still having updates enabled.
924 EXPECT_TRUE(GoogleUpdateSettings::AreAutoupdatesEnabled());
926 // Make sure that the reset action returns true and is a no-op.
927 EXPECT_TRUE(GoogleUpdateSettings::ReenableAutoupdates());
928 EXPECT_EQ(GoogleUpdateSettings::AUTOMATIC_UPDATES,
929 GetUpdatePolicyForAppGuid(dist->GetAppGuid()));
930 EXPECT_EQ(GoogleUpdateSettings::AUTOMATIC_UPDATES,
931 GetUpdatePolicyForAppGuid(binaries->GetAppGuid()));
932 EXPECT_EQ(GoogleUpdateSettings::UPDATES_DISABLED, GetGlobalUpdatePolicy());
935 TEST_F(GoogleUpdateSettingsTest, GlobalUpdatesDisabledByPolicy) {
936 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
937 EXPECT_TRUE(SetGlobalUpdatePolicy(GoogleUpdateSettings::UPDATES_DISABLED));
938 bool is_overridden = false;
940 // The contract for GetAppUpdatePolicy states that |is_overridden| should be
941 // set to false when updates are disabled on a non-app-specific basis.
942 GoogleUpdateSettings::UpdatePolicy update_policy =
943 GoogleUpdateSettings::GetAppUpdatePolicy(dist->GetAppGuid(),
944 &is_overridden);
945 EXPECT_FALSE(is_overridden);
946 EXPECT_EQ(GoogleUpdateSettings::UPDATES_DISABLED, update_policy);
947 EXPECT_FALSE(GoogleUpdateSettings::AreAutoupdatesEnabled());
949 EXPECT_TRUE(GoogleUpdateSettings::ReenableAutoupdates());
950 update_policy = GoogleUpdateSettings::GetAppUpdatePolicy(dist->GetAppGuid(),
951 &is_overridden);
952 // Policy should now be to enable updates, |is_overridden| should still be
953 // false.
954 EXPECT_FALSE(is_overridden);
955 EXPECT_EQ(GoogleUpdateSettings::AUTOMATIC_UPDATES, update_policy);
956 EXPECT_TRUE(GoogleUpdateSettings::AreAutoupdatesEnabled());
959 TEST_F(GoogleUpdateSettingsTest, UpdatesDisabledByTimeout) {
960 // Disable updates altogether.
961 EXPECT_TRUE(SetUpdateTimeoutOverride(0));
962 EXPECT_FALSE(GoogleUpdateSettings::AreAutoupdatesEnabled());
963 EXPECT_TRUE(GoogleUpdateSettings::ReenableAutoupdates());
964 EXPECT_TRUE(GoogleUpdateSettings::AreAutoupdatesEnabled());
966 // Set the update period to something unreasonable.
967 EXPECT_TRUE(SetUpdateTimeoutOverride(
968 GoogleUpdateSettings::kCheckPeriodOverrideMinutesMax + 1));
969 EXPECT_FALSE(GoogleUpdateSettings::AreAutoupdatesEnabled());
970 EXPECT_TRUE(GoogleUpdateSettings::ReenableAutoupdates());
971 EXPECT_TRUE(GoogleUpdateSettings::AreAutoupdatesEnabled());
974 TEST_F(GoogleUpdateSettingsTest, ExperimentsLabelHelperSystem) {
975 TestExperimentsLabelHelper(SYSTEM_INSTALL);
978 TEST_F(GoogleUpdateSettingsTest, ExperimentsLabelHelperUser) {
979 TestExperimentsLabelHelper(USER_INSTALL);
982 #endif // defined(GOOGLE_CHROME_BUILD)
984 // Test GoogleUpdateSettings::GetUninstallCommandLine at system- or user-level,
985 // according to the param.
986 class GetUninstallCommandLine : public GoogleUpdateSettingsTest,
987 public testing::WithParamInterface<bool> {
988 protected:
989 static const wchar_t kDummyCommand[];
991 virtual void SetUp() override {
992 GoogleUpdateSettingsTest::SetUp();
993 system_install_ = GetParam();
994 root_key_ = system_install_ ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER;
997 HKEY root_key_;
998 bool system_install_;
1001 const wchar_t GetUninstallCommandLine::kDummyCommand[] =
1002 L"\"goopdate.exe\" /spam";
1004 // Tests that GetUninstallCommandLine returns an empty string if there's no
1005 // Software\Google\Update key.
1006 TEST_P(GetUninstallCommandLine, TestNoKey) {
1007 EXPECT_EQ(base::string16(),
1008 GoogleUpdateSettings::GetUninstallCommandLine(system_install_));
1011 // Tests that GetUninstallCommandLine returns an empty string if there's no
1012 // UninstallCmdLine value in the Software\Google\Update key.
1013 TEST_P(GetUninstallCommandLine, TestNoValue) {
1014 RegKey(root_key_, google_update::kRegPathGoogleUpdate, KEY_SET_VALUE);
1015 EXPECT_EQ(base::string16(),
1016 GoogleUpdateSettings::GetUninstallCommandLine(system_install_));
1019 // Tests that GetUninstallCommandLine returns an empty string if there's an
1020 // empty UninstallCmdLine value in the Software\Google\Update key.
1021 TEST_P(GetUninstallCommandLine, TestEmptyValue) {
1022 RegKey(root_key_, google_update::kRegPathGoogleUpdate, KEY_SET_VALUE)
1023 .WriteValue(google_update::kRegUninstallCmdLine, L"");
1024 EXPECT_EQ(base::string16(),
1025 GoogleUpdateSettings::GetUninstallCommandLine(system_install_));
1028 // Tests that GetUninstallCommandLine returns the correct string if there's an
1029 // UninstallCmdLine value in the Software\Google\Update key.
1030 TEST_P(GetUninstallCommandLine, TestRealValue) {
1031 RegKey(root_key_, google_update::kRegPathGoogleUpdate, KEY_SET_VALUE)
1032 .WriteValue(google_update::kRegUninstallCmdLine, kDummyCommand);
1033 EXPECT_EQ(base::string16(kDummyCommand),
1034 GoogleUpdateSettings::GetUninstallCommandLine(system_install_));
1035 // Make sure that there's no value in the other level (user or system).
1036 EXPECT_EQ(base::string16(),
1037 GoogleUpdateSettings::GetUninstallCommandLine(!system_install_));
1040 INSTANTIATE_TEST_CASE_P(GetUninstallCommandLineAtLevel, GetUninstallCommandLine,
1041 testing::Bool());
1043 // Test GoogleUpdateSettings::GetGoogleUpdateVersion at system- or user-level,
1044 // according to the param.
1045 class GetGoogleUpdateVersion : public GoogleUpdateSettingsTest,
1046 public testing::WithParamInterface<bool> {
1047 protected:
1048 static const wchar_t kDummyVersion[];
1050 virtual void SetUp() override {
1051 GoogleUpdateSettingsTest::SetUp();
1052 system_install_ = GetParam();
1053 root_key_ = system_install_ ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER;
1056 HKEY root_key_;
1057 bool system_install_;
1060 const wchar_t GetGoogleUpdateVersion::kDummyVersion[] = L"1.2.3.4";
1062 // Tests that GetGoogleUpdateVersion returns an empty string if there's no
1063 // Software\Google\Update key.
1064 TEST_P(GetGoogleUpdateVersion, TestNoKey) {
1065 EXPECT_FALSE(
1066 GoogleUpdateSettings::GetGoogleUpdateVersion(system_install_).IsValid());
1069 // Tests that GetGoogleUpdateVersion returns an empty string if there's no
1070 // version value in the Software\Google\Update key.
1071 TEST_P(GetGoogleUpdateVersion, TestNoValue) {
1072 RegKey(root_key_, google_update::kRegPathGoogleUpdate, KEY_SET_VALUE);
1073 EXPECT_FALSE(
1074 GoogleUpdateSettings::GetGoogleUpdateVersion(system_install_).IsValid());
1077 // Tests that GetGoogleUpdateVersion returns an empty string if there's an
1078 // empty version value in the Software\Google\Update key.
1079 TEST_P(GetGoogleUpdateVersion, TestEmptyValue) {
1080 RegKey(root_key_, google_update::kRegPathGoogleUpdate, KEY_SET_VALUE)
1081 .WriteValue(google_update::kRegGoogleUpdateVersion, L"");
1082 EXPECT_FALSE(
1083 GoogleUpdateSettings::GetGoogleUpdateVersion(system_install_).IsValid());
1086 // Tests that GetGoogleUpdateVersion returns the correct string if there's a
1087 // version value in the Software\Google\Update key.
1088 TEST_P(GetGoogleUpdateVersion, TestRealValue) {
1089 RegKey(root_key_, google_update::kRegPathGoogleUpdate, KEY_SET_VALUE)
1090 .WriteValue(google_update::kRegGoogleUpdateVersion, kDummyVersion);
1091 Version expected(base::UTF16ToUTF8(kDummyVersion));
1092 EXPECT_TRUE(expected.Equals(
1093 GoogleUpdateSettings::GetGoogleUpdateVersion(system_install_)));
1094 // Make sure that there's no value in the other level (user or system).
1095 EXPECT_FALSE(
1096 GoogleUpdateSettings::GetGoogleUpdateVersion(!system_install_)
1097 .IsValid());
1100 INSTANTIATE_TEST_CASE_P(GetGoogleUpdateVersionAtLevel, GetGoogleUpdateVersion,
1101 testing::Bool());
1103 // Test values for use by the CollectStatsConsent test fixture.
1104 class StatsState {
1105 public:
1106 enum InstallType {
1107 SINGLE_INSTALL,
1108 MULTI_INSTALL,
1110 enum StateSetting {
1111 NO_SETTING,
1112 FALSE_SETTING,
1113 TRUE_SETTING,
1115 struct UserLevelState {};
1116 struct SystemLevelState {};
1117 static const UserLevelState kUserLevel;
1118 static const SystemLevelState kSystemLevel;
1120 StatsState(const UserLevelState&,
1121 InstallType install_type,
1122 StateSetting state_value)
1123 : system_level_(false),
1124 multi_install_(install_type == MULTI_INSTALL),
1125 state_value_(state_value),
1126 state_medium_value_(NO_SETTING) {
1128 StatsState(const SystemLevelState&,
1129 InstallType install_type,
1130 StateSetting state_value,
1131 StateSetting state_medium_value)
1132 : system_level_(true),
1133 multi_install_(install_type == MULTI_INSTALL),
1134 state_value_(state_value),
1135 state_medium_value_(state_medium_value) {
1137 bool system_level() const { return system_level_; }
1138 bool multi_install() const { return multi_install_; }
1139 HKEY root_key() const {
1140 return system_level_ ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER;
1142 StateSetting state_value() const { return state_value_; }
1143 StateSetting state_medium_value() const {
1144 return state_medium_value_;
1146 bool is_consent_granted() const {
1147 return (system_level_ && state_medium_value_ != NO_SETTING) ?
1148 (state_medium_value_ == TRUE_SETTING) :
1149 (state_value_ == TRUE_SETTING);
1152 private:
1153 bool system_level_;
1154 bool multi_install_;
1155 StateSetting state_value_;
1156 StateSetting state_medium_value_;
1159 const StatsState::UserLevelState StatsState::kUserLevel = {};
1160 const StatsState::SystemLevelState StatsState::kSystemLevel = {};
1162 // A value parameterized test for testing the stats collection consent setting.
1163 class CollectStatsConsent : public ::testing::TestWithParam<StatsState> {
1164 public:
1165 static void SetUpTestCase();
1166 static void TearDownTestCase();
1167 protected:
1168 virtual void SetUp() override;
1169 static void MakeChromeMultiInstall(HKEY root_key);
1170 static void ApplySetting(StatsState::StateSetting setting,
1171 HKEY root_key,
1172 const base::string16& reg_key);
1174 static base::string16* chrome_version_key_;
1175 static base::string16* chrome_state_key_;
1176 static base::string16* chrome_state_medium_key_;
1177 static base::string16* binaries_state_key_;
1178 static base::string16* binaries_state_medium_key_;
1179 registry_util::RegistryOverrideManager override_manager_;
1182 base::string16* CollectStatsConsent::chrome_version_key_;
1183 base::string16* CollectStatsConsent::chrome_state_key_;
1184 base::string16* CollectStatsConsent::chrome_state_medium_key_;
1185 base::string16* CollectStatsConsent::binaries_state_key_;
1186 base::string16* CollectStatsConsent::binaries_state_medium_key_;
1188 void CollectStatsConsent::SetUpTestCase() {
1189 BrowserDistribution* dist =
1190 BrowserDistribution::GetSpecificDistribution(
1191 BrowserDistribution::CHROME_BROWSER);
1192 chrome_version_key_ = new base::string16(dist->GetVersionKey());
1193 chrome_state_key_ = new base::string16(dist->GetStateKey());
1194 chrome_state_medium_key_ = new base::string16(dist->GetStateMediumKey());
1196 dist = BrowserDistribution::GetSpecificDistribution(
1197 BrowserDistribution::CHROME_BINARIES);
1198 binaries_state_key_ = new base::string16(dist->GetStateKey());
1199 binaries_state_medium_key_ = new base::string16(dist->GetStateMediumKey());
1202 void CollectStatsConsent::TearDownTestCase() {
1203 delete chrome_version_key_;
1204 delete chrome_state_key_;
1205 delete chrome_state_medium_key_;
1206 delete binaries_state_key_;
1207 delete binaries_state_medium_key_;
1210 // Install the registry override and apply the settings to the registry.
1211 void CollectStatsConsent::SetUp() {
1212 const StatsState& stats_state = GetParam();
1213 const HKEY root_key = stats_state.root_key();
1214 base::string16 reg_temp_name(
1215 stats_state.system_level() ? L"HKLM_" : L"HKCU_");
1216 reg_temp_name += L"CollectStatsConsent";
1217 override_manager_.OverrideRegistry(root_key);
1219 if (stats_state.multi_install()) {
1220 MakeChromeMultiInstall(root_key);
1221 ApplySetting(stats_state.state_value(), root_key, *binaries_state_key_);
1222 ApplySetting(stats_state.state_medium_value(), root_key,
1223 *binaries_state_medium_key_);
1224 } else {
1225 ApplySetting(stats_state.state_value(), root_key, *chrome_state_key_);
1226 ApplySetting(stats_state.state_medium_value(), root_key,
1227 *chrome_state_medium_key_);
1231 // Write values into the registry so that Chrome is considered to be installed
1232 // as multi-install.
1233 void CollectStatsConsent::MakeChromeMultiInstall(HKEY root_key) {
1234 ASSERT_EQ(
1235 ERROR_SUCCESS,
1236 RegKey(root_key, chrome_version_key_->c_str(),
1237 KEY_SET_VALUE).WriteValue(google_update::kRegVersionField,
1238 L"1.2.3.4"));
1239 ASSERT_EQ(
1240 ERROR_SUCCESS,
1241 RegKey(root_key, chrome_state_key_->c_str(),
1242 KEY_SET_VALUE).WriteValue(installer::kUninstallArgumentsField,
1243 L"--multi-install"));
1246 // Write the correct value to represent |setting| in the registry.
1247 void CollectStatsConsent::ApplySetting(StatsState::StateSetting setting,
1248 HKEY root_key,
1249 const base::string16& reg_key) {
1250 if (setting != StatsState::NO_SETTING) {
1251 DWORD value = setting != StatsState::FALSE_SETTING ? 1 : 0;
1252 ASSERT_EQ(
1253 ERROR_SUCCESS,
1254 RegKey(root_key, reg_key.c_str(),
1255 KEY_SET_VALUE).WriteValue(google_update::kRegUsageStatsField,
1256 value));
1260 // Test that stats consent can be read.
1261 TEST_P(CollectStatsConsent, GetCollectStatsConsentAtLevel) {
1262 if (GetParam().is_consent_granted()) {
1263 EXPECT_TRUE(GoogleUpdateSettings::GetCollectStatsConsentAtLevel(
1264 GetParam().system_level()));
1265 } else {
1266 EXPECT_FALSE(GoogleUpdateSettings::GetCollectStatsConsentAtLevel(
1267 GetParam().system_level()));
1271 // Test that stats consent can be flipped to the opposite setting, that the new
1272 // setting takes affect, and that the correct registry location is modified.
1273 TEST_P(CollectStatsConsent, SetCollectStatsConsentAtLevel) {
1274 EXPECT_TRUE(GoogleUpdateSettings::SetCollectStatsConsentAtLevel(
1275 GetParam().system_level(),
1276 !GetParam().is_consent_granted()));
1277 const base::string16* const reg_keys[] = {
1278 chrome_state_key_,
1279 chrome_state_medium_key_,
1280 binaries_state_key_,
1281 binaries_state_medium_key_,
1283 int key_index = ((GetParam().system_level() ? 1 : 0) +
1284 (GetParam().multi_install() ? 2 : 0));
1285 const base::string16& reg_key = *reg_keys[key_index];
1286 DWORD value = 0;
1287 EXPECT_EQ(
1288 ERROR_SUCCESS,
1289 RegKey(GetParam().root_key(), reg_key.c_str(),
1290 KEY_QUERY_VALUE).ReadValueDW(google_update::kRegUsageStatsField,
1291 &value));
1292 if (GetParam().is_consent_granted()) {
1293 EXPECT_FALSE(GoogleUpdateSettings::GetCollectStatsConsentAtLevel(
1294 GetParam().system_level()));
1295 EXPECT_EQ(0UL, value);
1296 } else {
1297 EXPECT_TRUE(GoogleUpdateSettings::GetCollectStatsConsentAtLevel(
1298 GetParam().system_level()));
1299 EXPECT_EQ(1UL, value);
1303 INSTANTIATE_TEST_CASE_P(
1304 UserLevelSingleInstall,
1305 CollectStatsConsent,
1306 ::testing::Values(
1307 StatsState(StatsState::kUserLevel, StatsState::SINGLE_INSTALL,
1308 StatsState::NO_SETTING),
1309 StatsState(StatsState::kUserLevel, StatsState::SINGLE_INSTALL,
1310 StatsState::FALSE_SETTING),
1311 StatsState(StatsState::kUserLevel, StatsState::SINGLE_INSTALL,
1312 StatsState::TRUE_SETTING)));
1313 INSTANTIATE_TEST_CASE_P(
1314 UserLevelMultiInstall,
1315 CollectStatsConsent,
1316 ::testing::Values(
1317 StatsState(StatsState::kUserLevel, StatsState::MULTI_INSTALL,
1318 StatsState::NO_SETTING),
1319 StatsState(StatsState::kUserLevel, StatsState::MULTI_INSTALL,
1320 StatsState::FALSE_SETTING),
1321 StatsState(StatsState::kUserLevel, StatsState::MULTI_INSTALL,
1322 StatsState::TRUE_SETTING)));
1323 INSTANTIATE_TEST_CASE_P(
1324 SystemLevelSingleInstall,
1325 CollectStatsConsent,
1326 ::testing::Values(
1327 StatsState(StatsState::kSystemLevel, StatsState::SINGLE_INSTALL,
1328 StatsState::NO_SETTING, StatsState::NO_SETTING),
1329 StatsState(StatsState::kSystemLevel, StatsState::SINGLE_INSTALL,
1330 StatsState::NO_SETTING, StatsState::FALSE_SETTING),
1331 StatsState(StatsState::kSystemLevel, StatsState::SINGLE_INSTALL,
1332 StatsState::NO_SETTING, StatsState::TRUE_SETTING),
1333 StatsState(StatsState::kSystemLevel, StatsState::SINGLE_INSTALL,
1334 StatsState::FALSE_SETTING, StatsState::NO_SETTING),
1335 StatsState(StatsState::kSystemLevel, StatsState::SINGLE_INSTALL,
1336 StatsState::FALSE_SETTING, StatsState::FALSE_SETTING),
1337 StatsState(StatsState::kSystemLevel, StatsState::SINGLE_INSTALL,
1338 StatsState::FALSE_SETTING, StatsState::TRUE_SETTING),
1339 StatsState(StatsState::kSystemLevel, StatsState::SINGLE_INSTALL,
1340 StatsState::TRUE_SETTING, StatsState::NO_SETTING),
1341 StatsState(StatsState::kSystemLevel, StatsState::SINGLE_INSTALL,
1342 StatsState::TRUE_SETTING, StatsState::FALSE_SETTING),
1343 StatsState(StatsState::kSystemLevel, StatsState::SINGLE_INSTALL,
1344 StatsState::TRUE_SETTING, StatsState::TRUE_SETTING)));
1345 INSTANTIATE_TEST_CASE_P(
1346 SystemLevelMultiInstall,
1347 CollectStatsConsent,
1348 ::testing::Values(
1349 StatsState(StatsState::kSystemLevel, StatsState::MULTI_INSTALL,
1350 StatsState::NO_SETTING, StatsState::NO_SETTING),
1351 StatsState(StatsState::kSystemLevel, StatsState::MULTI_INSTALL,
1352 StatsState::NO_SETTING, StatsState::FALSE_SETTING),
1353 StatsState(StatsState::kSystemLevel, StatsState::MULTI_INSTALL,
1354 StatsState::NO_SETTING, StatsState::TRUE_SETTING),
1355 StatsState(StatsState::kSystemLevel, StatsState::MULTI_INSTALL,
1356 StatsState::FALSE_SETTING, StatsState::NO_SETTING),
1357 StatsState(StatsState::kSystemLevel, StatsState::MULTI_INSTALL,
1358 StatsState::FALSE_SETTING, StatsState::FALSE_SETTING),
1359 StatsState(StatsState::kSystemLevel, StatsState::MULTI_INSTALL,
1360 StatsState::FALSE_SETTING, StatsState::TRUE_SETTING),
1361 StatsState(StatsState::kSystemLevel, StatsState::MULTI_INSTALL,
1362 StatsState::TRUE_SETTING, StatsState::NO_SETTING),
1363 StatsState(StatsState::kSystemLevel, StatsState::MULTI_INSTALL,
1364 StatsState::TRUE_SETTING, StatsState::FALSE_SETTING),
1365 StatsState(StatsState::kSystemLevel, StatsState::MULTI_INSTALL,
1366 StatsState::TRUE_SETTING, StatsState::TRUE_SETTING)));