OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/installer/mini_installer/configuration.h" | 5 #include "chrome/installer/mini_installer/configuration.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdlib.h> | 8 #include <stdlib.h> |
9 | 9 |
10 #include <memory> | 10 #include <memory> |
11 #include <vector> | |
11 | 12 |
12 #include "base/environment.h" | 13 #include "base/environment.h" |
14 #include "base/test/test_reg_util_win.h" | |
15 #include "base/win/registry.h" | |
13 #include "chrome/installer/mini_installer/appid.h" | 16 #include "chrome/installer/mini_installer/appid.h" |
17 #include "chrome/installer/mini_installer/mini_installer_constants.h" | |
14 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
15 | 19 |
16 using mini_installer::Configuration; | 20 namespace mini_installer { |
17 | 21 |
18 namespace { | 22 namespace { |
19 | 23 |
20 // A helper class to set the "GoogleUpdateIsMachine" environment variable. | 24 // A helper class to set the "GoogleUpdateIsMachine" environment variable. |
21 class ScopedGoogleUpdateIsMachine { | 25 class ScopedGoogleUpdateIsMachine { |
22 public: | 26 public: |
23 explicit ScopedGoogleUpdateIsMachine(bool value) | 27 explicit ScopedGoogleUpdateIsMachine(bool value) |
24 : env_(base::Environment::Create()) { | 28 : env_(base::Environment::Create()) { |
25 env_->SetVar("GoogleUpdateIsMachine", value ? "1" : "0"); | 29 env_->SetVar("GoogleUpdateIsMachine", value ? "1" : "0"); |
26 } | 30 } |
27 | 31 |
28 ~ScopedGoogleUpdateIsMachine() { | 32 ~ScopedGoogleUpdateIsMachine() { |
29 env_->UnSetVar("GoogleUpdateIsMachine"); | 33 env_->UnSetVar("GoogleUpdateIsMachine"); |
30 } | 34 } |
31 | 35 |
32 private: | 36 private: |
33 std::unique_ptr<base::Environment> env_; | 37 std::unique_ptr<base::Environment> env_; |
34 }; | 38 }; |
35 | 39 |
40 class TestConfiguration : public Configuration { | |
41 public: | |
42 explicit TestConfiguration(const wchar_t* command_line) { | |
43 Initialize(command_line); | |
44 } | |
45 void Initialize(const wchar_t* command_line) { | |
gab
2016/12/14 16:33:38
inline in constructor?
grt (UTC plus 2)
2016/12/14 20:38:45
Done.
| |
46 Clear(); | |
gab
2016/12/14 16:33:38
Probably don't need to Clear() a brand new Configu
grt (UTC plus 2)
2016/12/14 20:38:45
Done.
| |
47 ASSERT_TRUE(ParseCommandLine(command_line)); | |
48 } | |
49 | |
50 private: | |
51 DISALLOW_COPY_AND_ASSIGN(TestConfiguration); | |
52 }; | |
53 | |
36 } // namespace | 54 } // namespace |
37 | 55 |
38 class TestConfiguration : public Configuration { | 56 class MiniInstallerConfigurationTest : public ::testing::Test { |
39 public: | 57 protected: |
40 explicit TestConfiguration(const wchar_t* command_line) | 58 MiniInstallerConfigurationTest() { |
41 : Configuration(), | 59 registry_overrides_.OverrideRegistry(HKEY_CURRENT_USER); |
42 open_registry_key_result_(false), | 60 registry_overrides_.OverrideRegistry(HKEY_LOCAL_MACHINE); |
43 read_registry_value_result_(0), | |
44 read_registry_value_(L"") { | |
45 Initialize(command_line); | |
46 } | 61 } |
47 explicit TestConfiguration(const wchar_t* command_line, | 62 |
48 LONG ret, const wchar_t* value) | 63 // Adds sufficient state in the registry for Configuration to think that |
49 : Configuration(), | 64 // Chrome is already installed at |system_level| as per |multi_install|. |
50 open_registry_key_result_(true), | 65 void AddChromeRegistryState(bool system_level, bool multi_install) { |
51 read_registry_value_result_(ret), | 66 #if defined(GOOGLE_CHROME_BUILD) |
52 read_registry_value_(value) { | 67 static constexpr wchar_t kClientsPath[] = |
53 Initialize(command_line); | 68 L"SOFTWARE\\Google\\Update\\Clients\\" |
69 L"{8A69D345-D564-463c-AFF1-A69D9E530F96}"; | |
70 static constexpr wchar_t kClientStatePath[] = | |
71 L"SOFTWARE\\Google\\Update\\ClientState\\" | |
72 L"{8A69D345-D564-463c-AFF1-A69D9E530F96}"; | |
73 #else | |
74 static constexpr wchar_t kClientsPath[] = L"SOFTWARE\\Chromium"; | |
75 static constexpr wchar_t kClientStatePath[] = L"SOFTWARE\\Chromium"; | |
76 #endif | |
77 static constexpr const wchar_t* kUninstallArguments[] = { | |
78 L"--uninstall", L"--uninstall --multi-install --chrome", | |
79 L"--uninstall --system-level", | |
80 L"--uninstall --system-level --multi-install --chrome", | |
81 }; | |
82 const int uninstall_index = | |
83 ((system_level ? 0x02 : 0) | (multi_install ? 0x01 : 0)); | |
84 const HKEY root = system_level ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER; | |
85 base::win::RegKey key; | |
86 ASSERT_EQ(ERROR_SUCCESS, | |
87 key.Create(root, kClientsPath, KEY_WOW64_32KEY | KEY_SET_VALUE)); | |
88 ASSERT_EQ(ERROR_SUCCESS, key.WriteValue(L"pv", L"4.3.2.1")); | |
89 ASSERT_EQ(ERROR_SUCCESS, key.Create(root, kClientStatePath, | |
90 KEY_WOW64_32KEY | KEY_SET_VALUE)); | |
91 ASSERT_EQ(ERROR_SUCCESS, | |
92 key.WriteValue(L"UninstallArguments", | |
93 kUninstallArguments[uninstall_index])); | |
54 } | 94 } |
55 void SetRegistryResults(bool openkey, LONG ret, const wchar_t* value) { | 95 |
56 } | |
57 private: | 96 private: |
58 bool open_registry_key_result_; | 97 registry_util::RegistryOverrideManager registry_overrides_; |
59 LONG read_registry_value_result_; | |
60 const wchar_t* read_registry_value_ = L""; | |
61 | 98 |
62 void Initialize(const wchar_t* command_line) { | 99 DISALLOW_COPY_AND_ASSIGN(MiniInstallerConfigurationTest); |
63 Clear(); | |
64 ASSERT_TRUE(ParseCommandLine(command_line)); | |
65 } | |
66 bool ReadClientStateRegistryValue( | |
67 const HKEY root_key, const wchar_t* app_guid, | |
68 LONG* retval, ValueString& value) override { | |
69 *retval = read_registry_value_result_; | |
70 value.assign(read_registry_value_); | |
71 return open_registry_key_result_; | |
72 } | |
73 }; | 100 }; |
74 | 101 |
75 // Test that the operation type is CLEANUP iff --cleanup is on the cmdline. | 102 // Test that the operation type is CLEANUP iff --cleanup is on the cmdline. |
76 TEST(MiniInstallerConfigurationTest, Operation) { | 103 TEST_F(MiniInstallerConfigurationTest, Operation) { |
77 EXPECT_EQ(Configuration::INSTALL_PRODUCT, | 104 EXPECT_EQ(Configuration::INSTALL_PRODUCT, |
78 TestConfiguration(L"spam.exe").operation()); | 105 TestConfiguration(L"spam.exe").operation()); |
79 EXPECT_EQ(Configuration::INSTALL_PRODUCT, | 106 EXPECT_EQ(Configuration::INSTALL_PRODUCT, |
80 TestConfiguration(L"spam.exe --clean").operation()); | 107 TestConfiguration(L"spam.exe --clean").operation()); |
81 EXPECT_EQ(Configuration::INSTALL_PRODUCT, | 108 EXPECT_EQ(Configuration::INSTALL_PRODUCT, |
82 TestConfiguration(L"spam.exe --cleanupthis").operation()); | 109 TestConfiguration(L"spam.exe --cleanupthis").operation()); |
83 | 110 |
84 EXPECT_EQ(Configuration::CLEANUP, | 111 EXPECT_EQ(Configuration::CLEANUP, |
85 TestConfiguration(L"spam.exe --cleanup").operation()); | 112 TestConfiguration(L"spam.exe --cleanup").operation()); |
86 EXPECT_EQ(Configuration::CLEANUP, | 113 EXPECT_EQ(Configuration::CLEANUP, |
87 TestConfiguration(L"spam.exe --cleanup now").operation()); | 114 TestConfiguration(L"spam.exe --cleanup now").operation()); |
88 } | 115 } |
89 | 116 |
90 TEST(MiniInstallerConfigurationTest, Program) { | 117 TEST_F(MiniInstallerConfigurationTest, Program) { |
91 EXPECT_TRUE(NULL == mini_installer::Configuration().program()); | 118 EXPECT_TRUE(NULL == mini_installer::Configuration().program()); |
92 EXPECT_TRUE(std::wstring(L"spam.exe") == | 119 EXPECT_TRUE(std::wstring(L"spam.exe") == |
93 TestConfiguration(L"spam.exe").program()); | 120 TestConfiguration(L"spam.exe").program()); |
94 EXPECT_TRUE(std::wstring(L"spam.exe") == | 121 EXPECT_TRUE(std::wstring(L"spam.exe") == |
95 TestConfiguration(L"spam.exe --with args").program()); | 122 TestConfiguration(L"spam.exe --with args").program()); |
96 EXPECT_TRUE(std::wstring(L"c:\\blaz\\spam.exe") == | 123 EXPECT_TRUE(std::wstring(L"c:\\blaz\\spam.exe") == |
97 TestConfiguration(L"c:\\blaz\\spam.exe --with args").program()); | 124 TestConfiguration(L"c:\\blaz\\spam.exe --with args").program()); |
98 } | 125 } |
99 | 126 |
100 TEST(MiniInstallerConfigurationTest, ArgumentCount) { | 127 TEST_F(MiniInstallerConfigurationTest, ArgumentCount) { |
101 EXPECT_EQ(1, TestConfiguration(L"spam.exe").argument_count()); | 128 EXPECT_EQ(1, TestConfiguration(L"spam.exe").argument_count()); |
102 EXPECT_EQ(2, TestConfiguration(L"spam.exe --foo").argument_count()); | 129 EXPECT_EQ(2, TestConfiguration(L"spam.exe --foo").argument_count()); |
103 EXPECT_EQ(3, TestConfiguration(L"spam.exe --foo --bar").argument_count()); | 130 EXPECT_EQ(3, TestConfiguration(L"spam.exe --foo --bar").argument_count()); |
104 } | 131 } |
105 | 132 |
106 TEST(MiniInstallerConfigurationTest, CommandLine) { | 133 TEST_F(MiniInstallerConfigurationTest, CommandLine) { |
107 static const wchar_t* const kCommandLines[] = { | 134 static const wchar_t* const kCommandLines[] = { |
108 L"", | 135 L"", |
109 L"spam.exe", | 136 L"spam.exe", |
110 L"spam.exe --foo", | 137 L"spam.exe --foo", |
111 }; | 138 }; |
112 for (size_t i = 0; i < _countof(kCommandLines); ++i) { | 139 for (size_t i = 0; i < _countof(kCommandLines); ++i) { |
113 EXPECT_TRUE(std::wstring(kCommandLines[i]) == | 140 EXPECT_TRUE(std::wstring(kCommandLines[i]) == |
114 TestConfiguration(kCommandLines[i]).command_line()); | 141 TestConfiguration(kCommandLines[i]).command_line()); |
115 } | 142 } |
116 } | 143 } |
117 | 144 |
118 TEST(MiniInstallerConfigurationTest, ChromeAppGuid) { | 145 TEST_F(MiniInstallerConfigurationTest, IsUpdatingUserSingle) { |
119 EXPECT_TRUE(std::wstring(google_update::kAppGuid) == | 146 AddChromeRegistryState(false /* !system_level */, false /* !multi_install */); |
120 TestConfiguration(L"spam.exe").chrome_app_guid()); | 147 EXPECT_FALSE(TestConfiguration(L"spam.exe").is_updating_multi_chrome()); |
121 EXPECT_TRUE(std::wstring(google_update::kAppGuid) == | |
122 TestConfiguration(L"spam.exe --chrome").chrome_app_guid()); | |
123 EXPECT_TRUE(std::wstring(google_update::kSxSAppGuid) == | |
124 TestConfiguration(L"spam.exe --chrome-sxs").chrome_app_guid()); | |
125 EXPECT_TRUE(std::wstring(google_update::kMultiInstallAppGuid) == | |
126 TestConfiguration(L"spam.exe --multi-install --chrome") | |
127 .chrome_app_guid()); | |
128 EXPECT_TRUE(std::wstring(google_update::kMultiInstallAppGuid) == | |
129 TestConfiguration(L"spam.exe --multi-install --chrome", | |
130 ERROR_INVALID_FUNCTION, L"") | |
131 .chrome_app_guid()); | |
132 EXPECT_TRUE(std::wstring(google_update::kAppGuid) == | |
133 TestConfiguration(L"spam.exe --multi-install --chrome", | |
134 ERROR_FILE_NOT_FOUND, L"") | |
135 .chrome_app_guid()); | |
136 EXPECT_TRUE(std::wstring(google_update::kAppGuid) == | |
137 TestConfiguration(L"spam.exe --multi-install --chrome", | |
138 ERROR_SUCCESS, L"foo-bar") | |
139 .chrome_app_guid()); | |
140 EXPECT_TRUE(std::wstring(google_update::kMultiInstallAppGuid) == | |
141 TestConfiguration(L"spam.exe --multi-install --chrome", | |
142 ERROR_SUCCESS, L"foo-multi") | |
143 .chrome_app_guid()); | |
144 } | 148 } |
145 | 149 |
146 TEST(MiniInstallerConfigurationTest, HasChrome) { | 150 TEST_F(MiniInstallerConfigurationTest, IsUpdatingSystemSingle) { |
147 EXPECT_TRUE(TestConfiguration(L"spam.exe").has_chrome()); | 151 AddChromeRegistryState(true /* system_level */, false /* !multi_install */); |
148 EXPECT_TRUE(TestConfiguration(L"spam.exe --chrome").has_chrome()); | 152 EXPECT_FALSE( |
149 EXPECT_TRUE(TestConfiguration(L"spam.exe --multi-install --chrome") | 153 TestConfiguration(L"spam.exe --system-level").is_updating_multi_chrome()); |
150 .has_chrome()); | |
151 EXPECT_FALSE(TestConfiguration(L"spam.exe --multi-install").has_chrome()); | |
152 } | 154 } |
153 | 155 |
154 TEST(MiniInstallerConfigurationTest, IsMultiInstall) { | 156 TEST_F(MiniInstallerConfigurationTest, IsUpdatingUserMulti) { |
155 EXPECT_FALSE(TestConfiguration(L"spam.exe").is_multi_install()); | 157 AddChromeRegistryState(false /* !system_level */, true /* multi_install */); |
156 EXPECT_FALSE(TestConfiguration(L"spam.exe --chrome").is_multi_install()); | 158 #if defined(GOOGLE_CHROME_BUILD) |
157 EXPECT_TRUE(TestConfiguration(L"spam.exe --multi-install --chrome") | 159 EXPECT_TRUE(TestConfiguration(L"spam.exe").is_updating_multi_chrome()); |
158 .is_multi_install()); | 160 #else |
159 EXPECT_TRUE(TestConfiguration(L"spam.exe --multi-install") | 161 EXPECT_FALSE(TestConfiguration(L"spam.exe").is_updating_multi_chrome()); |
160 .is_multi_install()); | 162 #endif |
161 } | 163 } |
162 | 164 |
163 TEST(MiniInstallerConfigurationTest, IsSystemLevel) { | 165 TEST_F(MiniInstallerConfigurationTest, IsUpdatingSystemMulti) { |
166 AddChromeRegistryState(true /* system_level */, true /* multi_install */); | |
167 #if defined(GOOGLE_CHROME_BUILD) | |
168 EXPECT_TRUE( | |
169 TestConfiguration(L"spam.exe --system-level").is_updating_multi_chrome()); | |
170 #else | |
171 EXPECT_FALSE( | |
172 TestConfiguration(L"spam.exe --system-level").is_updating_multi_chrome()); | |
173 #endif | |
174 } | |
175 | |
176 TEST_F(MiniInstallerConfigurationTest, ChromeAppGuid) { | |
177 #if defined(GOOGLE_CHROME_BUILD) | |
178 EXPECT_STREQ(google_update::kAppGuid, | |
179 TestConfiguration(L"spam.exe").chrome_app_guid()); | |
180 EXPECT_STREQ(google_update::kSxSAppGuid, | |
181 TestConfiguration(L"spam.exe --chrome-sxs").chrome_app_guid()); | |
182 #endif | |
183 } | |
184 | |
185 TEST_F(MiniInstallerConfigurationTest, IsSystemLevel) { | |
164 EXPECT_FALSE(TestConfiguration(L"spam.exe").is_system_level()); | 186 EXPECT_FALSE(TestConfiguration(L"spam.exe").is_system_level()); |
165 EXPECT_FALSE(TestConfiguration(L"spam.exe --chrome").is_system_level()); | 187 EXPECT_FALSE(TestConfiguration(L"spam.exe --chrome").is_system_level()); |
166 EXPECT_TRUE(TestConfiguration(L"spam.exe --system-level").is_system_level()); | 188 EXPECT_TRUE(TestConfiguration(L"spam.exe --system-level").is_system_level()); |
167 | 189 |
168 { | 190 { |
169 ScopedGoogleUpdateIsMachine env_setter(false); | 191 ScopedGoogleUpdateIsMachine env_setter(false); |
170 EXPECT_FALSE(TestConfiguration(L"spam.exe").is_system_level()); | 192 EXPECT_FALSE(TestConfiguration(L"spam.exe").is_system_level()); |
171 } | 193 } |
172 | 194 |
173 { | 195 { |
174 ScopedGoogleUpdateIsMachine env_setter(true); | 196 ScopedGoogleUpdateIsMachine env_setter(true); |
175 EXPECT_TRUE(TestConfiguration(L"spam.exe").is_system_level()); | 197 EXPECT_TRUE(TestConfiguration(L"spam.exe").is_system_level()); |
176 } | 198 } |
177 } | 199 } |
178 | 200 |
179 TEST(MiniInstallerConfigurationTest, HasInvalidSwitch) { | 201 TEST_F(MiniInstallerConfigurationTest, IsSideBySide) { |
202 EXPECT_FALSE(TestConfiguration(L"spam.exe").is_side_by_side()); | |
203 #if defined(GOOGLE_CHROME_BUILD) | |
204 EXPECT_TRUE(TestConfiguration(L"spam.exe --chrome-sxs").is_side_by_side()); | |
205 #else | |
206 EXPECT_FALSE(TestConfiguration(L"spam.exe --chrome-sxs").is_side_by_side()); | |
207 #endif | |
208 } | |
209 | |
210 TEST_F(MiniInstallerConfigurationTest, HasInvalidSwitch) { | |
180 EXPECT_FALSE(TestConfiguration(L"spam.exe").has_invalid_switch()); | 211 EXPECT_FALSE(TestConfiguration(L"spam.exe").has_invalid_switch()); |
181 EXPECT_TRUE(TestConfiguration(L"spam.exe --chrome-frame") | 212 EXPECT_TRUE(TestConfiguration(L"spam.exe --chrome-frame") |
182 .has_invalid_switch()); | 213 .has_invalid_switch()); |
183 } | 214 } |
215 | |
216 } // namespace mini_installer | |
OLD | NEW |