Chromium Code Reviews| 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 | 11 |
| 12 #include "base/environment.h" | 12 #include "base/environment.h" |
| 13 #include "base/test/test_reg_util_win.h" | |
|
huangs
2017/01/31 17:40:04
#include "base/macros.h" for DISALLOW_COPY_AND_ASS
grt (UTC plus 2)
2017/02/02 08:17:16
Done.
| |
| 14 #include "base/win/registry.h" | |
| 13 #include "chrome/installer/mini_installer/appid.h" | 15 #include "chrome/installer/mini_installer/appid.h" |
| 16 #include "chrome/installer/mini_installer/mini_installer_constants.h" | |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 18 |
| 16 using mini_installer::Configuration; | 19 namespace mini_installer { |
| 17 | 20 |
| 18 namespace { | 21 namespace { |
| 19 | 22 |
| 20 // A helper class to set the "GoogleUpdateIsMachine" environment variable. | 23 // A helper class to set the "GoogleUpdateIsMachine" environment variable. |
| 21 class ScopedGoogleUpdateIsMachine { | 24 class ScopedGoogleUpdateIsMachine { |
| 22 public: | 25 public: |
| 23 explicit ScopedGoogleUpdateIsMachine(bool value) | 26 explicit ScopedGoogleUpdateIsMachine(bool value) |
| 24 : env_(base::Environment::Create()) { | 27 : env_(base::Environment::Create()) { |
| 25 env_->SetVar("GoogleUpdateIsMachine", value ? "1" : "0"); | 28 env_->SetVar("GoogleUpdateIsMachine", value ? "1" : "0"); |
| 26 } | 29 } |
| 27 | 30 |
| 28 ~ScopedGoogleUpdateIsMachine() { | 31 ~ScopedGoogleUpdateIsMachine() { |
| 29 env_->UnSetVar("GoogleUpdateIsMachine"); | 32 env_->UnSetVar("GoogleUpdateIsMachine"); |
| 30 } | 33 } |
| 31 | 34 |
| 32 private: | 35 private: |
| 33 std::unique_ptr<base::Environment> env_; | 36 std::unique_ptr<base::Environment> env_; |
| 34 }; | 37 }; |
| 35 | 38 |
| 39 class TestConfiguration : public Configuration { | |
| 40 public: | |
| 41 explicit TestConfiguration(const wchar_t* command_line) { | |
| 42 EXPECT_TRUE(ParseCommandLine(command_line)); | |
| 43 } | |
| 44 | |
| 45 private: | |
| 46 DISALLOW_COPY_AND_ASSIGN(TestConfiguration); | |
| 47 }; | |
| 48 | |
| 36 } // namespace | 49 } // namespace |
| 37 | 50 |
| 38 class TestConfiguration : public Configuration { | 51 class MiniInstallerConfigurationTest : public ::testing::Test { |
| 39 public: | 52 protected: |
| 40 explicit TestConfiguration(const wchar_t* command_line) | 53 MiniInstallerConfigurationTest() { |
| 41 : Configuration(), | 54 registry_overrides_.OverrideRegistry(HKEY_CURRENT_USER); |
| 42 open_registry_key_result_(false), | 55 registry_overrides_.OverrideRegistry(HKEY_LOCAL_MACHINE); |
| 43 read_registry_value_result_(0), | |
| 44 read_registry_value_(L"") { | |
| 45 Initialize(command_line); | |
| 46 } | 56 } |
| 47 explicit TestConfiguration(const wchar_t* command_line, | 57 |
| 48 LONG ret, const wchar_t* value) | 58 // Adds sufficient state in the registry for Configuration to think that |
| 49 : Configuration(), | 59 // Chrome is already installed at |system_level| as per |multi_install|. |
| 50 open_registry_key_result_(true), | 60 void AddChromeRegistryState(bool system_level, bool multi_install) { |
| 51 read_registry_value_result_(ret), | 61 #if defined(GOOGLE_CHROME_BUILD) |
| 52 read_registry_value_(value) { | 62 static constexpr wchar_t kClientsPath[] = |
| 53 Initialize(command_line); | 63 L"SOFTWARE\\Google\\Update\\Clients\\" |
| 64 L"{8A69D345-D564-463c-AFF1-A69D9E530F96}"; | |
| 65 static constexpr wchar_t kClientStatePath[] = | |
| 66 L"SOFTWARE\\Google\\Update\\ClientState\\" | |
| 67 L"{8A69D345-D564-463c-AFF1-A69D9E530F96}"; | |
| 68 #else | |
| 69 static constexpr wchar_t kClientsPath[] = L"SOFTWARE\\Chromium"; | |
| 70 static constexpr wchar_t kClientStatePath[] = L"SOFTWARE\\Chromium"; | |
| 71 #endif | |
|
huangs
2017/01/31 17:40:04
#endif // defined(GOOGLE_CHROME_BUILD)
or
#endi
grt (UTC plus 2)
2017/02/02 08:17:15
Done here. For the ones below that are pretty much
| |
| 72 static constexpr const wchar_t* kUninstallArguments[] = { | |
|
huangs
2017/01/31 17:40:04
Would it be clearer to render |kUninstallArguments
grt (UTC plus 2)
2017/02/02 08:17:15
Done.
| |
| 73 L"--uninstall", L"--uninstall --multi-install --chrome", | |
| 74 L"--uninstall --system-level", | |
| 75 L"--uninstall --system-level --multi-install --chrome", | |
| 76 }; | |
| 77 const int uninstall_index = | |
| 78 ((system_level ? 0x02 : 0) | (multi_install ? 0x01 : 0)); | |
|
huangs
2017/01/31 17:40:04
NIT: Redundant outer ()?
grt (UTC plus 2)
2017/02/02 08:17:15
code removed
| |
| 79 const HKEY root = system_level ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER; | |
| 80 base::win::RegKey key; | |
|
huangs
2017/01/31 17:40:04
Should this be base::win::RegKey or mini_installer
grt (UTC plus 2)
2017/02/02 08:17:16
Done
| |
| 81 ASSERT_EQ(ERROR_SUCCESS, | |
| 82 key.Create(root, kClientsPath, KEY_WOW64_32KEY | KEY_SET_VALUE)); | |
| 83 ASSERT_EQ(ERROR_SUCCESS, key.WriteValue(L"pv", L"4.3.2.1")); | |
| 84 ASSERT_EQ(ERROR_SUCCESS, key.Create(root, kClientStatePath, | |
| 85 KEY_WOW64_32KEY | KEY_SET_VALUE)); | |
| 86 ASSERT_EQ(ERROR_SUCCESS, | |
| 87 key.WriteValue(L"UninstallArguments", | |
| 88 kUninstallArguments[uninstall_index])); | |
| 54 } | 89 } |
| 55 void SetRegistryResults(bool openkey, LONG ret, const wchar_t* value) { | 90 |
| 56 } | |
| 57 private: | 91 private: |
| 58 bool open_registry_key_result_; | 92 registry_util::RegistryOverrideManager registry_overrides_; |
| 59 LONG read_registry_value_result_; | |
| 60 const wchar_t* read_registry_value_ = L""; | |
| 61 | 93 |
| 62 void Initialize(const wchar_t* command_line) { | 94 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 }; | 95 }; |
| 74 | 96 |
| 75 // Test that the operation type is CLEANUP iff --cleanup is on the cmdline. | 97 // Test that the operation type is CLEANUP iff --cleanup is on the cmdline. |
| 76 TEST(MiniInstallerConfigurationTest, Operation) { | 98 TEST_F(MiniInstallerConfigurationTest, Operation) { |
| 77 EXPECT_EQ(Configuration::INSTALL_PRODUCT, | 99 EXPECT_EQ(Configuration::INSTALL_PRODUCT, |
| 78 TestConfiguration(L"spam.exe").operation()); | 100 TestConfiguration(L"spam.exe").operation()); |
| 79 EXPECT_EQ(Configuration::INSTALL_PRODUCT, | 101 EXPECT_EQ(Configuration::INSTALL_PRODUCT, |
| 80 TestConfiguration(L"spam.exe --clean").operation()); | 102 TestConfiguration(L"spam.exe --clean").operation()); |
| 81 EXPECT_EQ(Configuration::INSTALL_PRODUCT, | 103 EXPECT_EQ(Configuration::INSTALL_PRODUCT, |
| 82 TestConfiguration(L"spam.exe --cleanupthis").operation()); | 104 TestConfiguration(L"spam.exe --cleanupthis").operation()); |
| 83 | 105 |
| 84 EXPECT_EQ(Configuration::CLEANUP, | 106 EXPECT_EQ(Configuration::CLEANUP, |
| 85 TestConfiguration(L"spam.exe --cleanup").operation()); | 107 TestConfiguration(L"spam.exe --cleanup").operation()); |
| 86 EXPECT_EQ(Configuration::CLEANUP, | 108 EXPECT_EQ(Configuration::CLEANUP, |
| 87 TestConfiguration(L"spam.exe --cleanup now").operation()); | 109 TestConfiguration(L"spam.exe --cleanup now").operation()); |
| 88 } | 110 } |
| 89 | 111 |
| 90 TEST(MiniInstallerConfigurationTest, Program) { | 112 TEST_F(MiniInstallerConfigurationTest, Program) { |
| 91 EXPECT_TRUE(NULL == mini_installer::Configuration().program()); | 113 EXPECT_TRUE(NULL == mini_installer::Configuration().program()); |
| 92 EXPECT_TRUE(std::wstring(L"spam.exe") == | 114 EXPECT_TRUE(std::wstring(L"spam.exe") == |
| 93 TestConfiguration(L"spam.exe").program()); | 115 TestConfiguration(L"spam.exe").program()); |
| 94 EXPECT_TRUE(std::wstring(L"spam.exe") == | 116 EXPECT_TRUE(std::wstring(L"spam.exe") == |
| 95 TestConfiguration(L"spam.exe --with args").program()); | 117 TestConfiguration(L"spam.exe --with args").program()); |
| 96 EXPECT_TRUE(std::wstring(L"c:\\blaz\\spam.exe") == | 118 EXPECT_TRUE(std::wstring(L"c:\\blaz\\spam.exe") == |
| 97 TestConfiguration(L"c:\\blaz\\spam.exe --with args").program()); | 119 TestConfiguration(L"c:\\blaz\\spam.exe --with args").program()); |
| 98 } | 120 } |
| 99 | 121 |
| 100 TEST(MiniInstallerConfigurationTest, ArgumentCount) { | 122 TEST_F(MiniInstallerConfigurationTest, ArgumentCount) { |
| 101 EXPECT_EQ(1, TestConfiguration(L"spam.exe").argument_count()); | 123 EXPECT_EQ(1, TestConfiguration(L"spam.exe").argument_count()); |
| 102 EXPECT_EQ(2, TestConfiguration(L"spam.exe --foo").argument_count()); | 124 EXPECT_EQ(2, TestConfiguration(L"spam.exe --foo").argument_count()); |
| 103 EXPECT_EQ(3, TestConfiguration(L"spam.exe --foo --bar").argument_count()); | 125 EXPECT_EQ(3, TestConfiguration(L"spam.exe --foo --bar").argument_count()); |
| 104 } | 126 } |
| 105 | 127 |
| 106 TEST(MiniInstallerConfigurationTest, CommandLine) { | 128 TEST_F(MiniInstallerConfigurationTest, CommandLine) { |
| 107 static const wchar_t* const kCommandLines[] = { | 129 static const wchar_t* const kCommandLines[] = { |
| 108 L"", | 130 L"", |
| 109 L"spam.exe", | 131 L"spam.exe", |
| 110 L"spam.exe --foo", | 132 L"spam.exe --foo", |
| 111 }; | 133 }; |
| 112 for (size_t i = 0; i < _countof(kCommandLines); ++i) { | 134 for (size_t i = 0; i < _countof(kCommandLines); ++i) { |
| 113 EXPECT_TRUE(std::wstring(kCommandLines[i]) == | 135 EXPECT_TRUE(std::wstring(kCommandLines[i]) == |
| 114 TestConfiguration(kCommandLines[i]).command_line()); | 136 TestConfiguration(kCommandLines[i]).command_line()); |
| 115 } | 137 } |
| 116 } | 138 } |
| 117 | 139 |
| 118 TEST(MiniInstallerConfigurationTest, ChromeAppGuid) { | 140 TEST_F(MiniInstallerConfigurationTest, IsUpdatingUserSingle) { |
| 119 EXPECT_TRUE(std::wstring(google_update::kAppGuid) == | 141 AddChromeRegistryState(false /* !system_level */, false /* !multi_install */); |
| 120 TestConfiguration(L"spam.exe").chrome_app_guid()); | 142 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::kChromeFrameAppGuid) == | |
| 124 TestConfiguration(L"spam.exe --chrome-frame").chrome_app_guid()); | |
| 125 EXPECT_TRUE(std::wstring(google_update::kSxSAppGuid) == | |
| 126 TestConfiguration(L"spam.exe --chrome-sxs").chrome_app_guid()); | |
| 127 EXPECT_TRUE(std::wstring(google_update::kMultiInstallAppGuid) == | |
| 128 TestConfiguration(L"spam.exe --multi-install --chrome") | |
| 129 .chrome_app_guid()); | |
| 130 EXPECT_TRUE(std::wstring(google_update::kMultiInstallAppGuid) == | |
| 131 TestConfiguration(L"spam.exe --multi-install --chrome", | |
| 132 ERROR_INVALID_FUNCTION, L"") | |
| 133 .chrome_app_guid()); | |
| 134 EXPECT_TRUE(std::wstring(google_update::kAppGuid) == | |
| 135 TestConfiguration(L"spam.exe --multi-install --chrome", | |
| 136 ERROR_FILE_NOT_FOUND, L"") | |
| 137 .chrome_app_guid()); | |
| 138 EXPECT_TRUE(std::wstring(google_update::kAppGuid) == | |
| 139 TestConfiguration(L"spam.exe --multi-install --chrome", | |
| 140 ERROR_SUCCESS, L"foo-bar") | |
| 141 .chrome_app_guid()); | |
| 142 EXPECT_TRUE(std::wstring(google_update::kMultiInstallAppGuid) == | |
| 143 TestConfiguration(L"spam.exe --multi-install --chrome", | |
| 144 ERROR_SUCCESS, L"foo-multi") | |
| 145 .chrome_app_guid()); | |
| 146 } | 143 } |
| 147 | 144 |
| 148 TEST(MiniInstallerConfigurationTest, HasChrome) { | 145 TEST_F(MiniInstallerConfigurationTest, IsUpdatingSystemSingle) { |
| 149 EXPECT_TRUE(TestConfiguration(L"spam.exe").has_chrome()); | 146 AddChromeRegistryState(true /* system_level */, false /* !multi_install */); |
| 150 EXPECT_TRUE(TestConfiguration(L"spam.exe --chrome").has_chrome()); | 147 EXPECT_FALSE( |
| 151 EXPECT_TRUE(TestConfiguration(L"spam.exe --multi-install --chrome") | 148 TestConfiguration(L"spam.exe --system-level").is_updating_multi_chrome()); |
| 152 .has_chrome()); | |
| 153 EXPECT_FALSE(TestConfiguration(L"spam.exe --chrome-frame").has_chrome()); | |
| 154 EXPECT_FALSE(TestConfiguration(L"spam.exe --multi-install").has_chrome()); | |
| 155 } | 149 } |
| 156 | 150 |
| 157 TEST(MiniInstallerConfigurationTest, HasChromeFrame) { | 151 TEST_F(MiniInstallerConfigurationTest, IsUpdatingUserMulti) { |
| 158 EXPECT_FALSE(TestConfiguration(L"spam.exe").has_chrome_frame()); | 152 AddChromeRegistryState(false /* !system_level */, true /* multi_install */); |
| 159 EXPECT_FALSE(TestConfiguration(L"spam.exe --chrome").has_chrome_frame()); | 153 #if defined(GOOGLE_CHROME_BUILD) |
| 160 EXPECT_FALSE(TestConfiguration(L"spam.exe --multi-install --chrome") | 154 EXPECT_TRUE(TestConfiguration(L"spam.exe").is_updating_multi_chrome()); |
| 161 .has_chrome_frame()); | 155 #else |
| 162 EXPECT_TRUE(TestConfiguration(L"spam.exe --chrome-frame").has_chrome_frame()); | 156 EXPECT_FALSE(TestConfiguration(L"spam.exe").is_updating_multi_chrome()); |
| 163 EXPECT_TRUE(TestConfiguration(L"spam.exe --multi-install --chrome-frame") | 157 #endif |
| 164 .has_chrome_frame()); | |
| 165 EXPECT_FALSE(TestConfiguration(L"spam.exe --multi-install") | |
| 166 .has_chrome_frame()); | |
| 167 } | 158 } |
| 168 | 159 |
| 169 TEST(MiniInstallerConfigurationTest, IsMultiInstall) { | 160 TEST_F(MiniInstallerConfigurationTest, IsUpdatingSystemMulti) { |
| 170 EXPECT_FALSE(TestConfiguration(L"spam.exe").is_multi_install()); | 161 AddChromeRegistryState(true /* system_level */, true /* multi_install */); |
| 171 EXPECT_FALSE(TestConfiguration(L"spam.exe --chrome").is_multi_install()); | 162 #if defined(GOOGLE_CHROME_BUILD) |
| 172 EXPECT_TRUE(TestConfiguration(L"spam.exe --multi-install --chrome") | 163 EXPECT_TRUE( |
| 173 .is_multi_install()); | 164 TestConfiguration(L"spam.exe --system-level").is_updating_multi_chrome()); |
| 174 EXPECT_FALSE(TestConfiguration(L"spam.exe --chrome-frame") | 165 #else |
| 175 .is_multi_install()); | 166 EXPECT_FALSE( |
| 176 EXPECT_TRUE(TestConfiguration(L"spam.exe --multi-install --chrome-frame") | 167 TestConfiguration(L"spam.exe --system-level").is_updating_multi_chrome()); |
| 177 .is_multi_install()); | 168 #endif |
| 178 EXPECT_TRUE(TestConfiguration(L"spam.exe --multi-install") | |
| 179 .is_multi_install()); | |
| 180 } | 169 } |
| 181 | 170 |
| 182 TEST(MiniInstallerConfigurationTest, IsSystemLevel) { | 171 TEST_F(MiniInstallerConfigurationTest, ChromeAppGuid) { |
| 172 #if defined(GOOGLE_CHROME_BUILD) | |
|
huangs
2017/01/31 17:40:04
Apply #if to the entire test, rather than leave em
grt (UTC plus 2)
2017/02/02 08:17:15
Done.
| |
| 173 EXPECT_STREQ(google_update::kAppGuid, | |
| 174 TestConfiguration(L"spam.exe").chrome_app_guid()); | |
| 175 EXPECT_STREQ(google_update::kSxSAppGuid, | |
| 176 TestConfiguration(L"spam.exe --chrome-sxs").chrome_app_guid()); | |
| 177 #endif | |
|
huangs
2017/01/31 17:40:04
Is it worth testing interaction with AddChromeRegi
grt (UTC plus 2)
2017/02/02 08:17:15
I'm not sure there is any interaction. chrome_app_
| |
| 178 } | |
| 179 | |
| 180 TEST_F(MiniInstallerConfigurationTest, IsSystemLevel) { | |
| 183 EXPECT_FALSE(TestConfiguration(L"spam.exe").is_system_level()); | 181 EXPECT_FALSE(TestConfiguration(L"spam.exe").is_system_level()); |
| 184 EXPECT_FALSE(TestConfiguration(L"spam.exe --chrome").is_system_level()); | 182 EXPECT_FALSE(TestConfiguration(L"spam.exe --chrome").is_system_level()); |
| 185 EXPECT_TRUE(TestConfiguration(L"spam.exe --system-level").is_system_level()); | 183 EXPECT_TRUE(TestConfiguration(L"spam.exe --system-level").is_system_level()); |
| 186 | 184 |
| 187 { | 185 { |
| 188 ScopedGoogleUpdateIsMachine env_setter(false); | 186 ScopedGoogleUpdateIsMachine env_setter(false); |
| 189 EXPECT_FALSE(TestConfiguration(L"spam.exe").is_system_level()); | 187 EXPECT_FALSE(TestConfiguration(L"spam.exe").is_system_level()); |
| 190 } | 188 } |
| 191 | 189 |
| 192 { | 190 { |
| 193 ScopedGoogleUpdateIsMachine env_setter(true); | 191 ScopedGoogleUpdateIsMachine env_setter(true); |
| 194 EXPECT_TRUE(TestConfiguration(L"spam.exe").is_system_level()); | 192 EXPECT_TRUE(TestConfiguration(L"spam.exe").is_system_level()); |
| 195 } | 193 } |
| 196 } | 194 } |
| 195 | |
| 196 TEST_F(MiniInstallerConfigurationTest, IsSideBySide) { | |
| 197 EXPECT_FALSE(TestConfiguration(L"spam.exe").is_side_by_side()); | |
| 198 #if defined(GOOGLE_CHROME_BUILD) | |
| 199 EXPECT_TRUE(TestConfiguration(L"spam.exe --chrome-sxs").is_side_by_side()); | |
| 200 #else | |
| 201 EXPECT_FALSE(TestConfiguration(L"spam.exe --chrome-sxs").is_side_by_side()); | |
| 202 #endif | |
|
huangs
2017/01/31 17:40:04
Is it worth testing interaction with AddChromeRegi
grt (UTC plus 2)
2017/02/02 08:17:16
is_side_by_side() was bad, so I've removed it.
| |
| 203 } | |
| 204 | |
| 205 } // namespace mini_installer | |
| OLD | NEW |