OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include "chrome/installer/util/update_active_setup_version_work_item.h" | |
6 | |
7 #include <windows.h> | |
8 | |
9 #include "base/macros.h" | |
10 #include "base/strings/string16.h" | |
11 #include "base/test/test_reg_util_win.h" | |
12 #include "base/win/registry.h" | |
13 #include "testing/gtest/include/gtest/gtest.h" | |
14 | |
15 using testing::ValuesIn; | |
16 | |
17 namespace { | |
18 | |
19 const HKEY kActiveSetupRoot = HKEY_LOCAL_MACHINE; | |
20 const base::char16 kActiveSetupPath[] = L"Active Setup\\test"; | |
21 | |
22 struct UpdateActiveSetupVersionWorkItemTestCase { | |
23 // The initial value to be set in the registry prior to executing the | |
24 // UpdateActiveSetupVersionWorkItem. No value will be set if this null. | |
25 const wchar_t* initial_value; | |
26 | |
27 // Whether to ask the UpdateActiveSetupVersionWorkItem to bump the OS_UPGRADES | |
28 // component of the Active Setup version. | |
29 bool bump_os_upgrades_component; | |
30 | |
31 // The expected value after executing the UpdateActiveSetupVersionWorkItem. | |
32 const wchar_t* expected_result; | |
33 }; | |
34 | |
35 // Implements PrintTo in order for gtest to be able to print the problematic | |
36 // UpdateActiveSetupVersionWorkItemTestCase on failure. | |
37 void PrintTo(const UpdateActiveSetupVersionWorkItemTestCase& test_case, | |
38 ::std::ostream* os) { | |
39 *os << "Initial value: " | |
grt (UTC plus 2)
2015/07/09 14:23:54
#include <ostream>
gab
2015/07/09 20:36:38
Done.
| |
40 << (test_case.initial_value ? test_case.initial_value : L"(empty)") | |
41 << ", bump_os_upgrades_component: " | |
42 << test_case.bump_os_upgrades_component | |
43 << ", expected result: " << test_case.expected_result; | |
44 } | |
45 | |
46 class UpdateActiveSetupVersionWorkItemTest | |
grt (UTC plus 2)
2015/07/09 14:23:54
i don't see an advantage to having this in an unna
gab
2015/07/09 20:36:38
Thanks for the reference, makes sense, I had alway
| |
47 : public testing::TestWithParam<UpdateActiveSetupVersionWorkItemTestCase> { | |
48 public: | |
49 UpdateActiveSetupVersionWorkItemTest() {} | |
50 | |
51 void SetUp() override { | |
52 registry_override_manager_.OverrideRegistry(kActiveSetupRoot); | |
53 } | |
54 | |
55 private: | |
56 registry_util::RegistryOverrideManager registry_override_manager_; | |
57 | |
58 DISALLOW_COPY_AND_ASSIGN(UpdateActiveSetupVersionWorkItemTest); | |
59 }; | |
60 | |
61 } // namespace | |
62 | |
63 TEST_P(UpdateActiveSetupVersionWorkItemTest, Execute) { | |
grt (UTC plus 2)
2015/07/09 14:23:54
this test is beautiful!
gab
2015/07/09 20:36:38
:-)
| |
64 // Get the parametrized |test_case| which defines 5 steps: | |
65 // 1) Maybe set an initial Active Setup version in the registry according to | |
66 // |test_case.initial_value|. | |
67 // 2) Declare the work to be done by the UpdateActiveSetupVersionWorkItem | |
68 // based on |test_case.bump_os_upgrades_component|. | |
69 // 3) Unconditionally execute the Active Setup work items. | |
70 // 4) Verify that the updated Active Setup version is as expected by | |
71 // |test_case.expected_result|. | |
72 // 5) Rollback and verify that |test_case.initial_value| is back. | |
73 const UpdateActiveSetupVersionWorkItemTestCase& test_case = GetParam(); | |
74 | |
75 base::win::RegKey test_key; | |
76 | |
77 EXPECT_EQ(ERROR_FILE_NOT_FOUND, | |
grt (UTC plus 2)
2015/07/09 14:23:54
should this be ASSERT_EQ? is there a reason to con
gab
2015/07/09 20:36:38
Agreed, anything after a failure here is meaningle
| |
78 test_key.Open(kActiveSetupRoot, kActiveSetupPath, KEY_READ)); | |
79 | |
80 UpdateActiveSetupVersionWorkItem active_setup_work_item( | |
81 kActiveSetupPath, test_case.bump_os_upgrades_component | |
82 ? UpdateActiveSetupVersionWorkItem:: | |
83 UPDATE_AND_BUMP_OS_UPGRADES_COMPONENT | |
84 : UpdateActiveSetupVersionWorkItem::UPDATE); | |
85 | |
86 // Create the key and set the |initial_value| *after* the WorkItem to confirm | |
87 // that all of the work is done when executing the item, not when creating it. | |
88 EXPECT_EQ(ERROR_SUCCESS, | |
grt (UTC plus 2)
2015/07/09 14:23:53
ASSERT_EQ?
gab
2015/07/09 20:36:38
Agreed, anything after a failure here is meaningle
| |
89 test_key.Create(kActiveSetupRoot, kActiveSetupPath, KEY_SET_VALUE)); | |
90 if (test_case.initial_value) { | |
91 EXPECT_EQ(ERROR_SUCCESS, | |
grt (UTC plus 2)
2015/07/09 14:23:54
ASSERT_EQ
gab
2015/07/09 20:36:38
Agreed, anything after a failure here is meaningle
| |
92 test_key.WriteValue(L"Version", test_case.initial_value)); | |
93 } | |
94 | |
95 active_setup_work_item.Do(); | |
grt (UTC plus 2)
2015/07/09 14:23:54
EXPECT_TRUE?
gab
2015/07/09 20:36:38
Done.
| |
96 | |
97 { | |
98 base::string16 version_out; | |
99 EXPECT_EQ(ERROR_SUCCESS, test_key.Open(kActiveSetupRoot, kActiveSetupPath, | |
100 KEY_QUERY_VALUE)); | |
101 EXPECT_EQ(ERROR_SUCCESS, test_key.ReadValue(L"Version", &version_out)); | |
102 EXPECT_EQ(test_case.expected_result, version_out); | |
103 } | |
104 | |
105 active_setup_work_item.Rollback(); | |
106 | |
107 { | |
108 EXPECT_EQ(ERROR_SUCCESS, test_key.Open(kActiveSetupRoot, kActiveSetupPath, | |
109 KEY_QUERY_VALUE)); | |
110 | |
111 base::string16 version_out; | |
112 LONG read_result = test_key.ReadValue(L"Version", &version_out); | |
113 if (test_case.initial_value) { | |
114 EXPECT_EQ(ERROR_SUCCESS, read_result); | |
115 EXPECT_EQ(test_case.initial_value, version_out); | |
116 } else { | |
117 EXPECT_EQ(ERROR_FILE_NOT_FOUND, read_result); | |
118 } | |
119 } | |
120 } | |
121 | |
122 const UpdateActiveSetupVersionWorkItemTestCase | |
grt (UTC plus 2)
2015/07/09 14:23:54
can you put this up with the struct definition? it
gab
2015/07/09 20:36:38
Done.
| |
123 kUpdateActiveSetupVersionWorkItemTestCases[] = { | |
124 // Initial install. | |
125 {nullptr, false, L"43,0,0,0"}, | |
126 // No-op update. | |
127 {L"43.0.0.0", false, L"43,0,0,0"}, | |
128 // Update only major component. | |
129 {L"24,1,2,3", false, L"43,1,2,3"}, | |
130 // Reset from bogus value. | |
131 {L"zzz", false, L"43,0,0,0"}, | |
132 // Reset from invalid version (too few components). | |
133 {L"1,2", false, L"43,0,0,0"}, | |
134 // Reset from invalid version (too many components). | |
135 {L"43,1,2,3,10", false, L"43,0,0,0"}, | |
136 // Reset from empty string. | |
137 {L"", false, L"43,0,0,0"}, | |
138 | |
139 // Same tests with an OS_UPGRADES component bump. | |
140 {nullptr, true, L"43,0,1,0"}, | |
141 {L"43.0.0.0", true, L"43,0,1,0"}, | |
142 {L"24,1,2,3", true, L"43,1,3,3"}, | |
143 {L"zzz", true, L"43,0,1,0"}, | |
144 {L"1,2", true, L"43,0,1,0"}, | |
145 {L"43,1,2,3,10", true, L"43,0,1,0"}, | |
146 {L"", true, L"43,0,1,0"}, | |
147 // Bumping a negative OS upgrade component should result in it being | |
148 // reset and subsequently bumped to 1 as usual. | |
149 {L"43,11,-123,33", true, L"43,11,1,33"}, | |
150 }; | |
151 | |
152 INSTANTIATE_TEST_CASE_P(UpdateActiveSetupVersionWorkItemTestInstance, | |
153 UpdateActiveSetupVersionWorkItemTest, | |
154 ValuesIn(kUpdateActiveSetupVersionWorkItemTestCases)); | |
OLD | NEW |