| 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 <string> | 5 #include <string> |
| 6 #include <utility> | 6 #include <utility> |
| 7 | 7 |
| 8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
| 9 #include "base/test/test_reg_util_win.h" |
| 9 #include "base/win/registry.h" | 10 #include "base/win/registry.h" |
| 10 #include "chrome/installer/util/google_update_constants.h" | 11 #include "chrome/installer/util/google_update_constants.h" |
| 11 #include "chrome/installer/util/install_util.h" | 12 #include "chrome/installer/util/install_util.h" |
| 12 #include "chrome/installer/util/product_unittest.h" | 13 #include "chrome/installer/util/product_unittest.h" |
| 13 #include "testing/gmock/include/gmock/gmock.h" | 14 #include "testing/gmock/include/gmock/gmock.h" |
| 14 | 15 |
| 15 using base::win::RegKey; | 16 using base::win::RegKey; |
| 17 using registry_util::ScopedRegistryKeyOverride; |
| 18 using registry_util::ScopedRegistryTempPathCleanup; |
| 16 using ::testing::_; | 19 using ::testing::_; |
| 17 using ::testing::Return; | 20 using ::testing::Return; |
| 18 using ::testing::StrEq; | 21 using ::testing::StrEq; |
| 19 | 22 |
| 20 class MockRegistryValuePredicate : public InstallUtil::RegistryValuePredicate { | 23 class MockRegistryValuePredicate : public InstallUtil::RegistryValuePredicate { |
| 21 public: | 24 public: |
| 22 MOCK_CONST_METHOD1(Evaluate, bool(const std::wstring&)); | 25 MOCK_CONST_METHOD1(Evaluate, bool(const std::wstring&)); |
| 23 }; | 26 }; |
| 24 | 27 |
| 25 class InstallUtilTest : public TestWithTempDirAndDeleteTempOverrideKeys { | 28 class InstallUtilTest : public TestWithTempDirAndDeleteTempOverrideKeys { |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 69 } | 72 } |
| 70 } | 73 } |
| 71 | 74 |
| 72 TEST_F(InstallUtilTest, UpdateInstallerStageAP) { | 75 TEST_F(InstallUtilTest, UpdateInstallerStageAP) { |
| 73 const bool system_level = false; | 76 const bool system_level = false; |
| 74 const HKEY root = system_level ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER; | 77 const HKEY root = system_level ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER; |
| 75 std::wstring state_key_path(L"PhonyClientState"); | 78 std::wstring state_key_path(L"PhonyClientState"); |
| 76 | 79 |
| 77 // Update the stage when there's no "ap" value. | 80 // Update the stage when there's no "ap" value. |
| 78 { | 81 { |
| 79 TempRegKeyOverride override(root, L"root_inst_res"); | 82 ScopedRegistryTempPathCleanup cleanup; |
| 83 ScopedRegistryKeyOverride override(root, L"root_inst_res"); |
| 80 RegKey(root, state_key_path.c_str(), KEY_SET_VALUE); | 84 RegKey(root, state_key_path.c_str(), KEY_SET_VALUE); |
| 81 InstallUtil::UpdateInstallerStage(system_level, state_key_path, | 85 InstallUtil::UpdateInstallerStage(system_level, state_key_path, |
| 82 installer::BUILDING); | 86 installer::BUILDING); |
| 83 std::wstring value; | 87 std::wstring value; |
| 84 EXPECT_EQ(ERROR_SUCCESS, | 88 EXPECT_EQ(ERROR_SUCCESS, |
| 85 RegKey(root, state_key_path.c_str(), KEY_QUERY_VALUE) | 89 RegKey(root, state_key_path.c_str(), KEY_QUERY_VALUE) |
| 86 .ReadValue(google_update::kRegApField, &value)); | 90 .ReadValue(google_update::kRegApField, &value)); |
| 87 EXPECT_EQ(L"-stage:building", value); | 91 EXPECT_EQ(L"-stage:building", value); |
| 88 } | 92 } |
| 89 TempRegKeyOverride::DeleteAllTempKeys(); | |
| 90 | 93 |
| 91 // Update the stage when there is an "ap" value. | 94 // Update the stage when there is an "ap" value. |
| 92 { | 95 { |
| 93 TempRegKeyOverride override(root, L"root_inst_res"); | 96 ScopedRegistryTempPathCleanup cleanup; |
| 97 ScopedRegistryKeyOverride override(root, L"root_inst_res"); |
| 94 RegKey(root, state_key_path.c_str(), KEY_SET_VALUE) | 98 RegKey(root, state_key_path.c_str(), KEY_SET_VALUE) |
| 95 .WriteValue(google_update::kRegApField, L"2.0-dev"); | 99 .WriteValue(google_update::kRegApField, L"2.0-dev"); |
| 96 InstallUtil::UpdateInstallerStage(system_level, state_key_path, | 100 InstallUtil::UpdateInstallerStage(system_level, state_key_path, |
| 97 installer::BUILDING); | 101 installer::BUILDING); |
| 98 std::wstring value; | 102 std::wstring value; |
| 99 EXPECT_EQ(ERROR_SUCCESS, | 103 EXPECT_EQ(ERROR_SUCCESS, |
| 100 RegKey(root, state_key_path.c_str(), KEY_QUERY_VALUE) | 104 RegKey(root, state_key_path.c_str(), KEY_QUERY_VALUE) |
| 101 .ReadValue(google_update::kRegApField, &value)); | 105 .ReadValue(google_update::kRegApField, &value)); |
| 102 EXPECT_EQ(L"2.0-dev-stage:building", value); | 106 EXPECT_EQ(L"2.0-dev-stage:building", value); |
| 103 } | 107 } |
| 104 TempRegKeyOverride::DeleteAllTempKeys(); | |
| 105 | 108 |
| 106 // Clear the stage. | 109 // Clear the stage. |
| 107 { | 110 { |
| 108 TempRegKeyOverride override(root, L"root_inst_res"); | 111 ScopedRegistryTempPathCleanup cleanup; |
| 112 ScopedRegistryKeyOverride override(root, L"root_inst_res"); |
| 109 RegKey(root, state_key_path.c_str(), KEY_SET_VALUE) | 113 RegKey(root, state_key_path.c_str(), KEY_SET_VALUE) |
| 110 .WriteValue(google_update::kRegApField, L"2.0-dev-stage:building"); | 114 .WriteValue(google_update::kRegApField, L"2.0-dev-stage:building"); |
| 111 InstallUtil::UpdateInstallerStage(system_level, state_key_path, | 115 InstallUtil::UpdateInstallerStage(system_level, state_key_path, |
| 112 installer::NO_STAGE); | 116 installer::NO_STAGE); |
| 113 std::wstring value; | 117 std::wstring value; |
| 114 EXPECT_EQ(ERROR_SUCCESS, | 118 EXPECT_EQ(ERROR_SUCCESS, |
| 115 RegKey(root, state_key_path.c_str(), KEY_QUERY_VALUE) | 119 RegKey(root, state_key_path.c_str(), KEY_QUERY_VALUE) |
| 116 .ReadValue(google_update::kRegApField, &value)); | 120 .ReadValue(google_update::kRegApField, &value)); |
| 117 EXPECT_EQ(L"2.0-dev", value); | 121 EXPECT_EQ(L"2.0-dev", value); |
| 118 } | 122 } |
| 119 TempRegKeyOverride::DeleteAllTempKeys(); | |
| 120 } | 123 } |
| 121 | 124 |
| 122 TEST_F(InstallUtilTest, UpdateInstallerStage) { | 125 TEST_F(InstallUtilTest, UpdateInstallerStage) { |
| 123 const bool system_level = false; | 126 const bool system_level = false; |
| 124 const HKEY root = system_level ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER; | 127 const HKEY root = system_level ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER; |
| 125 std::wstring state_key_path(L"PhonyClientState"); | 128 std::wstring state_key_path(L"PhonyClientState"); |
| 126 | 129 |
| 127 // Update the stage when there's no "InstallerExtraCode1" value. | 130 // Update the stage when there's no "InstallerExtraCode1" value. |
| 128 { | 131 { |
| 129 TempRegKeyOverride override(root, L"root_inst_res"); | 132 ScopedRegistryTempPathCleanup cleanup; |
| 133 ScopedRegistryKeyOverride override(root, L"root_inst_res"); |
| 130 RegKey(root, state_key_path.c_str(), KEY_SET_VALUE) | 134 RegKey(root, state_key_path.c_str(), KEY_SET_VALUE) |
| 131 .DeleteValue(installer::kInstallerExtraCode1); | 135 .DeleteValue(installer::kInstallerExtraCode1); |
| 132 InstallUtil::UpdateInstallerStage(system_level, state_key_path, | 136 InstallUtil::UpdateInstallerStage(system_level, state_key_path, |
| 133 installer::BUILDING); | 137 installer::BUILDING); |
| 134 DWORD value; | 138 DWORD value; |
| 135 EXPECT_EQ(ERROR_SUCCESS, | 139 EXPECT_EQ(ERROR_SUCCESS, |
| 136 RegKey(root, state_key_path.c_str(), KEY_QUERY_VALUE) | 140 RegKey(root, state_key_path.c_str(), KEY_QUERY_VALUE) |
| 137 .ReadValueDW(installer::kInstallerExtraCode1, &value)); | 141 .ReadValueDW(installer::kInstallerExtraCode1, &value)); |
| 138 EXPECT_EQ(static_cast<DWORD>(installer::BUILDING), value); | 142 EXPECT_EQ(static_cast<DWORD>(installer::BUILDING), value); |
| 139 } | 143 } |
| 140 TempRegKeyOverride::DeleteAllTempKeys(); | |
| 141 | 144 |
| 142 // Update the stage when there is an "InstallerExtraCode1" value. | 145 // Update the stage when there is an "InstallerExtraCode1" value. |
| 143 { | 146 { |
| 144 TempRegKeyOverride override(root, L"root_inst_res"); | 147 ScopedRegistryTempPathCleanup cleanup; |
| 148 ScopedRegistryKeyOverride override(root, L"root_inst_res"); |
| 145 RegKey(root, state_key_path.c_str(), KEY_SET_VALUE) | 149 RegKey(root, state_key_path.c_str(), KEY_SET_VALUE) |
| 146 .WriteValue(installer::kInstallerExtraCode1, | 150 .WriteValue(installer::kInstallerExtraCode1, |
| 147 static_cast<DWORD>(installer::UNPACKING)); | 151 static_cast<DWORD>(installer::UNPACKING)); |
| 148 InstallUtil::UpdateInstallerStage(system_level, state_key_path, | 152 InstallUtil::UpdateInstallerStage(system_level, state_key_path, |
| 149 installer::BUILDING); | 153 installer::BUILDING); |
| 150 DWORD value; | 154 DWORD value; |
| 151 EXPECT_EQ(ERROR_SUCCESS, | 155 EXPECT_EQ(ERROR_SUCCESS, |
| 152 RegKey(root, state_key_path.c_str(), KEY_QUERY_VALUE) | 156 RegKey(root, state_key_path.c_str(), KEY_QUERY_VALUE) |
| 153 .ReadValueDW(installer::kInstallerExtraCode1, &value)); | 157 .ReadValueDW(installer::kInstallerExtraCode1, &value)); |
| 154 EXPECT_EQ(static_cast<DWORD>(installer::BUILDING), value); | 158 EXPECT_EQ(static_cast<DWORD>(installer::BUILDING), value); |
| 155 } | 159 } |
| 156 TempRegKeyOverride::DeleteAllTempKeys(); | |
| 157 | 160 |
| 158 // Clear the stage. | 161 // Clear the stage. |
| 159 { | 162 { |
| 160 TempRegKeyOverride override(root, L"root_inst_res"); | 163 ScopedRegistryTempPathCleanup cleanup; |
| 164 ScopedRegistryKeyOverride override(root, L"root_inst_res"); |
| 161 RegKey(root, state_key_path.c_str(), KEY_SET_VALUE) | 165 RegKey(root, state_key_path.c_str(), KEY_SET_VALUE) |
| 162 .WriteValue(installer::kInstallerExtraCode1, static_cast<DWORD>(5)); | 166 .WriteValue(installer::kInstallerExtraCode1, static_cast<DWORD>(5)); |
| 163 InstallUtil::UpdateInstallerStage(system_level, state_key_path, | 167 InstallUtil::UpdateInstallerStage(system_level, state_key_path, |
| 164 installer::NO_STAGE); | 168 installer::NO_STAGE); |
| 165 DWORD value; | 169 DWORD value; |
| 166 EXPECT_EQ(ERROR_FILE_NOT_FOUND, | 170 EXPECT_EQ(ERROR_FILE_NOT_FOUND, |
| 167 RegKey(root, state_key_path.c_str(), KEY_QUERY_VALUE) | 171 RegKey(root, state_key_path.c_str(), KEY_QUERY_VALUE) |
| 168 .ReadValueDW(installer::kInstallerExtraCode1, &value)); | 172 .ReadValueDW(installer::kInstallerExtraCode1, &value)); |
| 169 } | 173 } |
| 170 TempRegKeyOverride::DeleteAllTempKeys(); | |
| 171 } | 174 } |
| 172 | 175 |
| 173 TEST_F(InstallUtilTest, DeleteRegistryKeyIf) { | 176 TEST_F(InstallUtilTest, DeleteRegistryKeyIf) { |
| 174 const HKEY root = HKEY_CURRENT_USER; | 177 const HKEY root = HKEY_CURRENT_USER; |
| 175 std::wstring parent_key_path(L"SomeKey\\ToDelete"); | 178 std::wstring parent_key_path(L"SomeKey\\ToDelete"); |
| 176 std::wstring child_key_path(parent_key_path); | 179 std::wstring child_key_path(parent_key_path); |
| 177 child_key_path.append(L"\\ChildKey\\WithAValue"); | 180 child_key_path.append(L"\\ChildKey\\WithAValue"); |
| 178 const wchar_t value_name[] = L"some_value_name"; | 181 const wchar_t value_name[] = L"some_value_name"; |
| 179 const wchar_t value[] = L"hi mom"; | 182 const wchar_t value[] = L"hi mom"; |
| 180 | 183 |
| 181 { | 184 { |
| 182 TempRegKeyOverride override(root, L"root_key"); | 185 ScopedRegistryTempPathCleanup cleanup; |
| 186 ScopedRegistryKeyOverride override(root, L"root_key"); |
| 183 // Nothing to delete if the keys aren't even there. | 187 // Nothing to delete if the keys aren't even there. |
| 184 { | 188 { |
| 185 MockRegistryValuePredicate pred; | 189 MockRegistryValuePredicate pred; |
| 186 | 190 |
| 187 EXPECT_CALL(pred, Evaluate(_)).Times(0); | 191 EXPECT_CALL(pred, Evaluate(_)).Times(0); |
| 188 ASSERT_FALSE(RegKey(root, parent_key_path.c_str(), | 192 ASSERT_FALSE(RegKey(root, parent_key_path.c_str(), |
| 189 KEY_QUERY_VALUE).Valid()); | 193 KEY_QUERY_VALUE).Valid()); |
| 190 EXPECT_TRUE(InstallUtil::DeleteRegistryKeyIf( | 194 EXPECT_TRUE(InstallUtil::DeleteRegistryKeyIf( |
| 191 root, parent_key_path, child_key_path, value_name, pred)); | 195 root, parent_key_path, child_key_path, value_name, pred)); |
| 192 EXPECT_FALSE(RegKey(root, parent_key_path.c_str(), | 196 EXPECT_FALSE(RegKey(root, parent_key_path.c_str(), |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 238 EXPECT_CALL(pred, Evaluate(StrEq(value))).WillOnce(Return(true)); | 242 EXPECT_CALL(pred, Evaluate(StrEq(value))).WillOnce(Return(true)); |
| 239 ASSERT_EQ(ERROR_SUCCESS, | 243 ASSERT_EQ(ERROR_SUCCESS, |
| 240 RegKey(root, child_key_path.c_str(), | 244 RegKey(root, child_key_path.c_str(), |
| 241 KEY_SET_VALUE).WriteValue(value_name, value)); | 245 KEY_SET_VALUE).WriteValue(value_name, value)); |
| 242 EXPECT_TRUE(InstallUtil::DeleteRegistryKeyIf( | 246 EXPECT_TRUE(InstallUtil::DeleteRegistryKeyIf( |
| 243 root, parent_key_path, child_key_path, value_name, pred)); | 247 root, parent_key_path, child_key_path, value_name, pred)); |
| 244 EXPECT_FALSE(RegKey(root, parent_key_path.c_str(), | 248 EXPECT_FALSE(RegKey(root, parent_key_path.c_str(), |
| 245 KEY_QUERY_VALUE).Valid()); | 249 KEY_QUERY_VALUE).Valid()); |
| 246 } | 250 } |
| 247 } | 251 } |
| 248 TempRegKeyOverride::DeleteAllTempKeys(); | |
| 249 } | 252 } |
| 250 | 253 |
| 251 TEST_F(InstallUtilTest, DeleteRegistryValueIf) { | 254 TEST_F(InstallUtilTest, DeleteRegistryValueIf) { |
| 252 const HKEY root = HKEY_CURRENT_USER; | 255 const HKEY root = HKEY_CURRENT_USER; |
| 253 std::wstring key_path(L"SomeKey\\ToDelete"); | 256 std::wstring key_path(L"SomeKey\\ToDelete"); |
| 254 const wchar_t value_name[] = L"some_value_name"; | 257 const wchar_t value_name[] = L"some_value_name"; |
| 255 const wchar_t value[] = L"hi mom"; | 258 const wchar_t value[] = L"hi mom"; |
| 256 | 259 |
| 257 { | 260 { |
| 258 TempRegKeyOverride override(root, L"root_key"); | 261 ScopedRegistryTempPathCleanup cleanup; |
| 262 ScopedRegistryKeyOverride override(root, L"root_key"); |
| 259 // Nothing to delete if the key isn't even there. | 263 // Nothing to delete if the key isn't even there. |
| 260 { | 264 { |
| 261 MockRegistryValuePredicate pred; | 265 MockRegistryValuePredicate pred; |
| 262 | 266 |
| 263 EXPECT_CALL(pred, Evaluate(_)).Times(0); | 267 EXPECT_CALL(pred, Evaluate(_)).Times(0); |
| 264 ASSERT_FALSE(RegKey(root, key_path.c_str(), KEY_QUERY_VALUE).Valid()); | 268 ASSERT_FALSE(RegKey(root, key_path.c_str(), KEY_QUERY_VALUE).Valid()); |
| 265 EXPECT_TRUE(InstallUtil::DeleteRegistryValueIf( | 269 EXPECT_TRUE(InstallUtil::DeleteRegistryValueIf( |
| 266 root, key_path.c_str(), value_name, pred)); | 270 root, key_path.c_str(), value_name, pred)); |
| 267 EXPECT_FALSE(RegKey(root, key_path.c_str(), KEY_QUERY_VALUE).Valid()); | 271 EXPECT_FALSE(RegKey(root, key_path.c_str(), KEY_QUERY_VALUE).Valid()); |
| 268 } | 272 } |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 301 ASSERT_EQ(ERROR_SUCCESS, | 305 ASSERT_EQ(ERROR_SUCCESS, |
| 302 RegKey(root, key_path.c_str(), | 306 RegKey(root, key_path.c_str(), |
| 303 KEY_SET_VALUE).WriteValue(value_name, value)); | 307 KEY_SET_VALUE).WriteValue(value_name, value)); |
| 304 EXPECT_TRUE(InstallUtil::DeleteRegistryValueIf( | 308 EXPECT_TRUE(InstallUtil::DeleteRegistryValueIf( |
| 305 root, key_path.c_str(), value_name, pred)); | 309 root, key_path.c_str(), value_name, pred)); |
| 306 EXPECT_TRUE(RegKey(root, key_path.c_str(), KEY_QUERY_VALUE).Valid()); | 310 EXPECT_TRUE(RegKey(root, key_path.c_str(), KEY_QUERY_VALUE).Valid()); |
| 307 EXPECT_FALSE(RegKey(root, key_path.c_str(), | 311 EXPECT_FALSE(RegKey(root, key_path.c_str(), |
| 308 KEY_QUERY_VALUE).ValueExists(value_name)); | 312 KEY_QUERY_VALUE).ValueExists(value_name)); |
| 309 } | 313 } |
| 310 } | 314 } |
| 311 TempRegKeyOverride::DeleteAllTempKeys(); | |
| 312 | 315 |
| 313 { | 316 { |
| 314 TempRegKeyOverride override(root, L"root_key"); | 317 ScopedRegistryTempPathCleanup cleanup; |
| 318 ScopedRegistryKeyOverride override(root, L"root_key"); |
| 315 // Default value matches: delete. | 319 // Default value matches: delete. |
| 316 { | 320 { |
| 317 MockRegistryValuePredicate pred; | 321 MockRegistryValuePredicate pred; |
| 318 | 322 |
| 319 EXPECT_CALL(pred, Evaluate(StrEq(value))).WillOnce(Return(true)); | 323 EXPECT_CALL(pred, Evaluate(StrEq(value))).WillOnce(Return(true)); |
| 320 ASSERT_EQ(ERROR_SUCCESS, | 324 ASSERT_EQ(ERROR_SUCCESS, |
| 321 RegKey(root, key_path.c_str(), | 325 RegKey(root, key_path.c_str(), |
| 322 KEY_SET_VALUE).WriteValue(L"", value)); | 326 KEY_SET_VALUE).WriteValue(L"", value)); |
| 323 EXPECT_TRUE(InstallUtil::DeleteRegistryValueIf( | 327 EXPECT_TRUE(InstallUtil::DeleteRegistryValueIf( |
| 324 root, key_path.c_str(), L"", pred)); | 328 root, key_path.c_str(), L"", pred)); |
| 325 EXPECT_TRUE(RegKey(root, key_path.c_str(), KEY_QUERY_VALUE).Valid()); | 329 EXPECT_TRUE(RegKey(root, key_path.c_str(), KEY_QUERY_VALUE).Valid()); |
| 326 EXPECT_FALSE(RegKey(root, key_path.c_str(), | 330 EXPECT_FALSE(RegKey(root, key_path.c_str(), |
| 327 KEY_QUERY_VALUE).ValueExists(L"")); | 331 KEY_QUERY_VALUE).ValueExists(L"")); |
| 328 } | 332 } |
| 329 } | 333 } |
| 330 TempRegKeyOverride::DeleteAllTempKeys(); | |
| 331 } | 334 } |
| 332 | 335 |
| 333 TEST_F(InstallUtilTest, ValueEquals) { | 336 TEST_F(InstallUtilTest, ValueEquals) { |
| 334 InstallUtil::ValueEquals pred(L"howdy"); | 337 InstallUtil::ValueEquals pred(L"howdy"); |
| 335 | 338 |
| 336 EXPECT_FALSE(pred.Evaluate(L"")); | 339 EXPECT_FALSE(pred.Evaluate(L"")); |
| 337 EXPECT_FALSE(pred.Evaluate(L"Howdy")); | 340 EXPECT_FALSE(pred.Evaluate(L"Howdy")); |
| 338 EXPECT_FALSE(pred.Evaluate(L"howdy!")); | 341 EXPECT_FALSE(pred.Evaluate(L"howdy!")); |
| 339 EXPECT_FALSE(pred.Evaluate(L"!howdy")); | 342 EXPECT_FALSE(pred.Evaluate(L"!howdy")); |
| 340 EXPECT_TRUE(pred.Evaluate(L"howdy")); | 343 EXPECT_TRUE(pred.Evaluate(L"howdy")); |
| 341 } | 344 } |
| OLD | NEW |