Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / installer / setup / update_active_setup_version_work_item_unittest.cc
blobbf3ca46e6ce7cc6ac6b028931e3e99dbb9542644
1 // Copyright 2015 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/setup/update_active_setup_version_work_item.h"
7 #include <windows.h>
9 #include <ostream>
11 #include "base/macros.h"
12 #include "base/strings/string16.h"
13 #include "base/test/test_reg_util_win.h"
14 #include "base/win/registry.h"
15 #include "testing/gtest/include/gtest/gtest.h"
17 using testing::ValuesIn;
19 namespace {
21 const HKEY kActiveSetupRoot = HKEY_LOCAL_MACHINE;
22 const base::char16 kActiveSetupPath[] = L"Active Setup\\test";
24 struct UpdateActiveSetupVersionWorkItemTestCase {
25 // The initial value to be set in the registry prior to executing the
26 // UpdateActiveSetupVersionWorkItem. No value will be set if this null.
27 const wchar_t* initial_value;
29 // Whether to ask the UpdateActiveSetupVersionWorkItem to bump the OS_UPGRADES
30 // component of the Active Setup version.
31 bool bump_os_upgrades_component;
33 // The expected value after executing the UpdateActiveSetupVersionWorkItem.
34 const wchar_t* expected_result;
35 } const kUpdateActiveSetupVersionWorkItemTestCases[] = {
36 // Initial install.
37 {nullptr, false, L"43,0,0,0"},
38 // No-op update.
39 {L"43.0.0.0", false, L"43,0,0,0"},
40 // Update only major component.
41 {L"24,1,2,3", false, L"43,1,2,3"},
42 // Reset from bogus value.
43 {L"zzz", false, L"43,0,0,0"},
44 // Reset from invalid version (too few components).
45 {L"1,2", false, L"43,0,0,0"},
46 // Reset from invalid version (too many components).
47 {L"43,1,2,3,10", false, L"43,0,0,0"},
48 // Reset from empty string.
49 {L"", false, L"43,0,0,0"},
51 // Same tests with an OS_UPGRADES component bump.
52 {nullptr, true, L"43,0,1,0"},
53 {L"43.0.0.0", true, L"43,0,1,0"},
54 {L"24,1,2,3", true, L"43,1,3,3"},
55 {L"zzz", true, L"43,0,1,0"},
56 {L"1,2", true, L"43,0,1,0"},
57 {L"43,1,2,3,10", true, L"43,0,1,0"},
58 {L"", true, L"43,0,1,0"},
59 // Bumping a negative OS upgrade component should result in it being
60 // reset and subsequently bumped to 1 as usual.
61 {L"43,11,-123,33", true, L"43,11,1,33"},
64 // Implements PrintTo in order for gtest to be able to print the problematic
65 // UpdateActiveSetupVersionWorkItemTestCase on failure.
66 void PrintTo(const UpdateActiveSetupVersionWorkItemTestCase& test_case,
67 ::std::ostream* os) {
68 *os << "Initial value: "
69 << (test_case.initial_value ? test_case.initial_value : L"(empty)")
70 << ", bump_os_upgrades_component: "
71 << test_case.bump_os_upgrades_component
72 << ", expected result: " << test_case.expected_result;
75 } // namespace
77 class UpdateActiveSetupVersionWorkItemTest
78 : public testing::TestWithParam<UpdateActiveSetupVersionWorkItemTestCase> {
79 public:
80 UpdateActiveSetupVersionWorkItemTest() {}
82 void SetUp() override {
83 registry_override_manager_.OverrideRegistry(kActiveSetupRoot);
86 private:
87 registry_util::RegistryOverrideManager registry_override_manager_;
89 DISALLOW_COPY_AND_ASSIGN(UpdateActiveSetupVersionWorkItemTest);
92 TEST_P(UpdateActiveSetupVersionWorkItemTest, Execute) {
93 // Get the parametrized |test_case| which defines 5 steps:
94 // 1) Maybe set an initial Active Setup version in the registry according to
95 // |test_case.initial_value|.
96 // 2) Declare the work to be done by the UpdateActiveSetupVersionWorkItem
97 // based on |test_case.bump_os_upgrades_component|.
98 // 3) Unconditionally execute the Active Setup work items.
99 // 4) Verify that the updated Active Setup version is as expected by
100 // |test_case.expected_result|.
101 // 5) Rollback and verify that |test_case.initial_value| is back.
102 const UpdateActiveSetupVersionWorkItemTestCase& test_case = GetParam();
104 base::win::RegKey test_key;
106 ASSERT_EQ(ERROR_FILE_NOT_FOUND,
107 test_key.Open(kActiveSetupRoot, kActiveSetupPath, KEY_READ));
109 UpdateActiveSetupVersionWorkItem active_setup_work_item(
110 kActiveSetupPath, test_case.bump_os_upgrades_component
111 ? UpdateActiveSetupVersionWorkItem::
112 UPDATE_AND_BUMP_OS_UPGRADES_COMPONENT
113 : UpdateActiveSetupVersionWorkItem::UPDATE);
115 // Create the key and set the |initial_value| *after* the WorkItem to confirm
116 // that all of the work is done when executing the item, not when creating it.
117 ASSERT_EQ(ERROR_SUCCESS,
118 test_key.Create(kActiveSetupRoot, kActiveSetupPath, KEY_SET_VALUE));
119 if (test_case.initial_value) {
120 ASSERT_EQ(ERROR_SUCCESS,
121 test_key.WriteValue(L"Version", test_case.initial_value));
124 EXPECT_TRUE(active_setup_work_item.Do());
127 base::string16 version_out;
128 EXPECT_EQ(ERROR_SUCCESS, test_key.Open(kActiveSetupRoot, kActiveSetupPath,
129 KEY_QUERY_VALUE));
130 EXPECT_EQ(ERROR_SUCCESS, test_key.ReadValue(L"Version", &version_out));
131 EXPECT_EQ(test_case.expected_result, version_out);
134 active_setup_work_item.Rollback();
137 EXPECT_EQ(ERROR_SUCCESS, test_key.Open(kActiveSetupRoot, kActiveSetupPath,
138 KEY_QUERY_VALUE));
140 base::string16 version_out;
141 LONG read_result = test_key.ReadValue(L"Version", &version_out);
142 if (test_case.initial_value) {
143 EXPECT_EQ(ERROR_SUCCESS, read_result);
144 EXPECT_EQ(test_case.initial_value, version_out);
145 } else {
146 EXPECT_EQ(ERROR_FILE_NOT_FOUND, read_result);
151 INSTANTIATE_TEST_CASE_P(UpdateActiveSetupVersionWorkItemTestInstance,
152 UpdateActiveSetupVersionWorkItemTest,
153 ValuesIn(kUpdateActiveSetupVersionWorkItemTestCases));