| 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 EXPECT_TRUE(ParseCommandLine(command_line)); |
| 44 } |
| 45 |
| 46 private: |
| 47 DISALLOW_COPY_AND_ASSIGN(TestConfiguration); |
| 48 }; |
| 49 |
| 36 } // namespace | 50 } // namespace |
| 37 | 51 |
| 38 class TestConfiguration : public Configuration { | 52 class MiniInstallerConfigurationTest : public ::testing::Test { |
| 39 public: | 53 protected: |
| 40 explicit TestConfiguration(const wchar_t* command_line) | 54 MiniInstallerConfigurationTest() { |
| 41 : Configuration(), | 55 registry_overrides_.OverrideRegistry(HKEY_CURRENT_USER); |
| 42 open_registry_key_result_(false), | 56 registry_overrides_.OverrideRegistry(HKEY_LOCAL_MACHINE); |
| 43 read_registry_value_result_(0), | |
| 44 read_registry_value_(L"") { | |
| 45 Initialize(command_line); | |
| 46 } | 57 } |
| 47 explicit TestConfiguration(const wchar_t* command_line, | 58 |
| 48 LONG ret, const wchar_t* value) | 59 // Adds sufficient state in the registry for Configuration to think that |
| 49 : Configuration(), | 60 // Chrome is already installed at |system_level| as per |multi_install|. |
| 50 open_registry_key_result_(true), | 61 void AddChromeRegistryState(bool system_level, bool multi_install) { |
| 51 read_registry_value_result_(ret), | 62 #if defined(GOOGLE_CHROME_BUILD) |
| 52 read_registry_value_(value) { | 63 static constexpr wchar_t kClientsPath[] = |
| 53 Initialize(command_line); | 64 L"SOFTWARE\\Google\\Update\\Clients\\" |
| 65 L"{8A69D345-D564-463c-AFF1-A69D9E530F96}"; |
| 66 static constexpr wchar_t kClientStatePath[] = |
| 67 L"SOFTWARE\\Google\\Update\\ClientState\\" |
| 68 L"{8A69D345-D564-463c-AFF1-A69D9E530F96}"; |
| 69 #else |
| 70 static constexpr wchar_t kClientsPath[] = L"SOFTWARE\\Chromium"; |
| 71 static constexpr wchar_t kClientStatePath[] = L"SOFTWARE\\Chromium"; |
| 72 #endif |
| 73 static constexpr const wchar_t* kUninstallArguments[] = { |
| 74 L"--uninstall", L"--uninstall --multi-install --chrome", |
| 75 L"--uninstall --system-level", |
| 76 L"--uninstall --system-level --multi-install --chrome", |
| 77 }; |
| 78 const int uninstall_index = |
| 79 ((system_level ? 0x02 : 0) | (multi_install ? 0x01 : 0)); |
| 80 const HKEY root = system_level ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER; |
| 81 base::win::RegKey key; |
| 82 ASSERT_EQ(ERROR_SUCCESS, |
| 83 key.Create(root, kClientsPath, KEY_WOW64_32KEY | KEY_SET_VALUE)); |
| 84 ASSERT_EQ(ERROR_SUCCESS, key.WriteValue(L"pv", L"4.3.2.1")); |
| 85 ASSERT_EQ(ERROR_SUCCESS, key.Create(root, kClientStatePath, |
| 86 KEY_WOW64_32KEY | KEY_SET_VALUE)); |
| 87 ASSERT_EQ(ERROR_SUCCESS, |
| 88 key.WriteValue(L"UninstallArguments", |
| 89 kUninstallArguments[uninstall_index])); |
| 54 } | 90 } |
| 55 void SetRegistryResults(bool openkey, LONG ret, const wchar_t* value) { | 91 |
| 56 } | |
| 57 private: | 92 private: |
| 58 bool open_registry_key_result_; | 93 registry_util::RegistryOverrideManager registry_overrides_; |
| 59 LONG read_registry_value_result_; | |
| 60 const wchar_t* read_registry_value_ = L""; | |
| 61 | 94 |
| 62 void Initialize(const wchar_t* command_line) { | 95 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 }; | 96 }; |
| 74 | 97 |
| 75 // Test that the operation type is CLEANUP iff --cleanup is on the cmdline. | 98 // Test that the operation type is CLEANUP iff --cleanup is on the cmdline. |
| 76 TEST(MiniInstallerConfigurationTest, Operation) { | 99 TEST_F(MiniInstallerConfigurationTest, Operation) { |
| 77 EXPECT_EQ(Configuration::INSTALL_PRODUCT, | 100 EXPECT_EQ(Configuration::INSTALL_PRODUCT, |
| 78 TestConfiguration(L"spam.exe").operation()); | 101 TestConfiguration(L"spam.exe").operation()); |
| 79 EXPECT_EQ(Configuration::INSTALL_PRODUCT, | 102 EXPECT_EQ(Configuration::INSTALL_PRODUCT, |
| 80 TestConfiguration(L"spam.exe --clean").operation()); | 103 TestConfiguration(L"spam.exe --clean").operation()); |
| 81 EXPECT_EQ(Configuration::INSTALL_PRODUCT, | 104 EXPECT_EQ(Configuration::INSTALL_PRODUCT, |
| 82 TestConfiguration(L"spam.exe --cleanupthis").operation()); | 105 TestConfiguration(L"spam.exe --cleanupthis").operation()); |
| 83 | 106 |
| 84 EXPECT_EQ(Configuration::CLEANUP, | 107 EXPECT_EQ(Configuration::CLEANUP, |
| 85 TestConfiguration(L"spam.exe --cleanup").operation()); | 108 TestConfiguration(L"spam.exe --cleanup").operation()); |
| 86 EXPECT_EQ(Configuration::CLEANUP, | 109 EXPECT_EQ(Configuration::CLEANUP, |
| 87 TestConfiguration(L"spam.exe --cleanup now").operation()); | 110 TestConfiguration(L"spam.exe --cleanup now").operation()); |
| 88 } | 111 } |
| 89 | 112 |
| 90 TEST(MiniInstallerConfigurationTest, Program) { | 113 TEST_F(MiniInstallerConfigurationTest, Program) { |
| 91 EXPECT_TRUE(NULL == mini_installer::Configuration().program()); | 114 EXPECT_TRUE(NULL == mini_installer::Configuration().program()); |
| 92 EXPECT_TRUE(std::wstring(L"spam.exe") == | 115 EXPECT_TRUE(std::wstring(L"spam.exe") == |
| 93 TestConfiguration(L"spam.exe").program()); | 116 TestConfiguration(L"spam.exe").program()); |
| 94 EXPECT_TRUE(std::wstring(L"spam.exe") == | 117 EXPECT_TRUE(std::wstring(L"spam.exe") == |
| 95 TestConfiguration(L"spam.exe --with args").program()); | 118 TestConfiguration(L"spam.exe --with args").program()); |
| 96 EXPECT_TRUE(std::wstring(L"c:\\blaz\\spam.exe") == | 119 EXPECT_TRUE(std::wstring(L"c:\\blaz\\spam.exe") == |
| 97 TestConfiguration(L"c:\\blaz\\spam.exe --with args").program()); | 120 TestConfiguration(L"c:\\blaz\\spam.exe --with args").program()); |
| 98 } | 121 } |
| 99 | 122 |
| 100 TEST(MiniInstallerConfigurationTest, ArgumentCount) { | 123 TEST_F(MiniInstallerConfigurationTest, ArgumentCount) { |
| 101 EXPECT_EQ(1, TestConfiguration(L"spam.exe").argument_count()); | 124 EXPECT_EQ(1, TestConfiguration(L"spam.exe").argument_count()); |
| 102 EXPECT_EQ(2, TestConfiguration(L"spam.exe --foo").argument_count()); | 125 EXPECT_EQ(2, TestConfiguration(L"spam.exe --foo").argument_count()); |
| 103 EXPECT_EQ(3, TestConfiguration(L"spam.exe --foo --bar").argument_count()); | 126 EXPECT_EQ(3, TestConfiguration(L"spam.exe --foo --bar").argument_count()); |
| 104 } | 127 } |
| 105 | 128 |
| 106 TEST(MiniInstallerConfigurationTest, CommandLine) { | 129 TEST_F(MiniInstallerConfigurationTest, CommandLine) { |
| 107 static const wchar_t* const kCommandLines[] = { | 130 static const wchar_t* const kCommandLines[] = { |
| 108 L"", | 131 L"", |
| 109 L"spam.exe", | 132 L"spam.exe", |
| 110 L"spam.exe --foo", | 133 L"spam.exe --foo", |
| 111 }; | 134 }; |
| 112 for (size_t i = 0; i < _countof(kCommandLines); ++i) { | 135 for (size_t i = 0; i < _countof(kCommandLines); ++i) { |
| 113 EXPECT_TRUE(std::wstring(kCommandLines[i]) == | 136 EXPECT_TRUE(std::wstring(kCommandLines[i]) == |
| 114 TestConfiguration(kCommandLines[i]).command_line()); | 137 TestConfiguration(kCommandLines[i]).command_line()); |
| 115 } | 138 } |
| 116 } | 139 } |
| 117 | 140 |
| 118 TEST(MiniInstallerConfigurationTest, ChromeAppGuid) { | 141 TEST_F(MiniInstallerConfigurationTest, IsUpdatingUserSingle) { |
| 119 EXPECT_TRUE(std::wstring(google_update::kAppGuid) == | 142 AddChromeRegistryState(false /* !system_level */, false /* !multi_install */); |
| 120 TestConfiguration(L"spam.exe").chrome_app_guid()); | 143 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 } | 144 } |
| 145 | 145 |
| 146 TEST(MiniInstallerConfigurationTest, HasChrome) { | 146 TEST_F(MiniInstallerConfigurationTest, IsUpdatingSystemSingle) { |
| 147 EXPECT_TRUE(TestConfiguration(L"spam.exe").has_chrome()); | 147 AddChromeRegistryState(true /* system_level */, false /* !multi_install */); |
| 148 EXPECT_TRUE(TestConfiguration(L"spam.exe --chrome").has_chrome()); | 148 EXPECT_FALSE( |
| 149 EXPECT_TRUE(TestConfiguration(L"spam.exe --multi-install --chrome") | 149 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 } | 150 } |
| 153 | 151 |
| 154 TEST(MiniInstallerConfigurationTest, IsMultiInstall) { | 152 TEST_F(MiniInstallerConfigurationTest, IsUpdatingUserMulti) { |
| 155 EXPECT_FALSE(TestConfiguration(L"spam.exe").is_multi_install()); | 153 AddChromeRegistryState(false /* !system_level */, true /* multi_install */); |
| 156 EXPECT_FALSE(TestConfiguration(L"spam.exe --chrome").is_multi_install()); | 154 #if defined(GOOGLE_CHROME_BUILD) |
| 157 EXPECT_TRUE(TestConfiguration(L"spam.exe --multi-install --chrome") | 155 EXPECT_TRUE(TestConfiguration(L"spam.exe").is_updating_multi_chrome()); |
| 158 .is_multi_install()); | 156 #else |
| 159 EXPECT_TRUE(TestConfiguration(L"spam.exe --multi-install") | 157 EXPECT_FALSE(TestConfiguration(L"spam.exe").is_updating_multi_chrome()); |
| 160 .is_multi_install()); | 158 #endif |
| 161 } | 159 } |
| 162 | 160 |
| 163 TEST(MiniInstallerConfigurationTest, IsSystemLevel) { | 161 TEST_F(MiniInstallerConfigurationTest, IsUpdatingSystemMulti) { |
| 162 AddChromeRegistryState(true /* system_level */, true /* multi_install */); |
| 163 #if defined(GOOGLE_CHROME_BUILD) |
| 164 EXPECT_TRUE( |
| 165 TestConfiguration(L"spam.exe --system-level").is_updating_multi_chrome()); |
| 166 #else |
| 167 EXPECT_FALSE( |
| 168 TestConfiguration(L"spam.exe --system-level").is_updating_multi_chrome()); |
| 169 #endif |
| 170 } |
| 171 |
| 172 TEST_F(MiniInstallerConfigurationTest, ChromeAppGuid) { |
| 173 #if defined(GOOGLE_CHROME_BUILD) |
| 174 EXPECT_STREQ(google_update::kAppGuid, |
| 175 TestConfiguration(L"spam.exe").chrome_app_guid()); |
| 176 EXPECT_STREQ(google_update::kSxSAppGuid, |
| 177 TestConfiguration(L"spam.exe --chrome-sxs").chrome_app_guid()); |
| 178 #endif |
| 179 } |
| 180 |
| 181 TEST_F(MiniInstallerConfigurationTest, IsSystemLevel) { |
| 164 EXPECT_FALSE(TestConfiguration(L"spam.exe").is_system_level()); | 182 EXPECT_FALSE(TestConfiguration(L"spam.exe").is_system_level()); |
| 165 EXPECT_FALSE(TestConfiguration(L"spam.exe --chrome").is_system_level()); | 183 EXPECT_FALSE(TestConfiguration(L"spam.exe --chrome").is_system_level()); |
| 166 EXPECT_TRUE(TestConfiguration(L"spam.exe --system-level").is_system_level()); | 184 EXPECT_TRUE(TestConfiguration(L"spam.exe --system-level").is_system_level()); |
| 167 | 185 |
| 168 { | 186 { |
| 169 ScopedGoogleUpdateIsMachine env_setter(false); | 187 ScopedGoogleUpdateIsMachine env_setter(false); |
| 170 EXPECT_FALSE(TestConfiguration(L"spam.exe").is_system_level()); | 188 EXPECT_FALSE(TestConfiguration(L"spam.exe").is_system_level()); |
| 171 } | 189 } |
| 172 | 190 |
| 173 { | 191 { |
| 174 ScopedGoogleUpdateIsMachine env_setter(true); | 192 ScopedGoogleUpdateIsMachine env_setter(true); |
| 175 EXPECT_TRUE(TestConfiguration(L"spam.exe").is_system_level()); | 193 EXPECT_TRUE(TestConfiguration(L"spam.exe").is_system_level()); |
| 176 } | 194 } |
| 177 } | 195 } |
| 178 | 196 |
| 179 TEST(MiniInstallerConfigurationTest, HasInvalidSwitch) { | 197 TEST_F(MiniInstallerConfigurationTest, IsSideBySide) { |
| 198 EXPECT_FALSE(TestConfiguration(L"spam.exe").is_side_by_side()); |
| 199 #if defined(GOOGLE_CHROME_BUILD) |
| 200 EXPECT_TRUE(TestConfiguration(L"spam.exe --chrome-sxs").is_side_by_side()); |
| 201 #else |
| 202 EXPECT_FALSE(TestConfiguration(L"spam.exe --chrome-sxs").is_side_by_side()); |
| 203 #endif |
| 204 } |
| 205 |
| 206 TEST_F(MiniInstallerConfigurationTest, HasInvalidSwitch) { |
| 180 EXPECT_FALSE(TestConfiguration(L"spam.exe").has_invalid_switch()); | 207 EXPECT_FALSE(TestConfiguration(L"spam.exe").has_invalid_switch()); |
| 181 EXPECT_TRUE(TestConfiguration(L"spam.exe --chrome-frame") | 208 EXPECT_TRUE(TestConfiguration(L"spam.exe --chrome-frame") |
| 182 .has_invalid_switch()); | 209 .has_invalid_switch()); |
| 183 } | 210 } |
| 211 |
| 212 } // namespace mini_installer |
| OLD | NEW |