| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/setup/setup_util_unittest.h" | 5 #include "chrome/installer/setup/setup_util_unittest.h" |
| 6 | 6 |
| 7 #include <windows.h> | 7 #include <windows.h> |
| 8 #include <shlobj.h> | 8 #include <shlobj.h> |
| 9 | 9 |
| 10 #include <memory> |
| 10 #include <string> | 11 #include <string> |
| 11 | 12 |
| 12 #include "base/command_line.h" | 13 #include "base/command_line.h" |
| 13 #include "base/files/file_util.h" | 14 #include "base/files/file_util.h" |
| 14 #include "base/files/scoped_temp_dir.h" | 15 #include "base/files/scoped_temp_dir.h" |
| 15 #include "base/macros.h" | 16 #include "base/macros.h" |
| 16 #include "base/memory/scoped_ptr.h" | |
| 17 #include "base/process/kill.h" | 17 #include "base/process/kill.h" |
| 18 #include "base/process/launch.h" | 18 #include "base/process/launch.h" |
| 19 #include "base/process/process_handle.h" | 19 #include "base/process/process_handle.h" |
| 20 #include "base/strings/string_util.h" | 20 #include "base/strings/string_util.h" |
| 21 #include "base/test/test_reg_util_win.h" | 21 #include "base/test/test_reg_util_win.h" |
| 22 #include "base/test/test_timeouts.h" | 22 #include "base/test/test_timeouts.h" |
| 23 #include "base/threading/platform_thread.h" | 23 #include "base/threading/platform_thread.h" |
| 24 #include "base/version.h" | 24 #include "base/version.h" |
| 25 #include "base/win/registry.h" | 25 #include "base/win/registry.h" |
| 26 #include "base/win/scoped_handle.h" | 26 #include "base/win/scoped_handle.h" |
| (...skipping 25 matching lines...) Expand all Loading... |
| 52 return false; | 52 return false; |
| 53 } | 53 } |
| 54 | 54 |
| 55 base::win::ScopedHandle token(temp_handle); | 55 base::win::ScopedHandle token(temp_handle); |
| 56 | 56 |
| 57 // First get the size of the buffer needed for |privileges| below. | 57 // First get the size of the buffer needed for |privileges| below. |
| 58 DWORD size; | 58 DWORD size; |
| 59 EXPECT_FALSE(::GetTokenInformation(token.Get(), TokenPrivileges, NULL, 0, | 59 EXPECT_FALSE(::GetTokenInformation(token.Get(), TokenPrivileges, NULL, 0, |
| 60 &size)); | 60 &size)); |
| 61 | 61 |
| 62 scoped_ptr<BYTE[]> privileges_bytes(new BYTE[size]); | 62 std::unique_ptr<BYTE[]> privileges_bytes(new BYTE[size]); |
| 63 TOKEN_PRIVILEGES* privileges = | 63 TOKEN_PRIVILEGES* privileges = |
| 64 reinterpret_cast<TOKEN_PRIVILEGES*>(privileges_bytes.get()); | 64 reinterpret_cast<TOKEN_PRIVILEGES*>(privileges_bytes.get()); |
| 65 | 65 |
| 66 if (!::GetTokenInformation(token.Get(), TokenPrivileges, privileges, size, | 66 if (!::GetTokenInformation(token.Get(), TokenPrivileges, privileges, size, |
| 67 &size)) { | 67 &size)) { |
| 68 ADD_FAILURE(); | 68 ADD_FAILURE(); |
| 69 return false; | 69 return false; |
| 70 } | 70 } |
| 71 | 71 |
| 72 // There is no point getting a buffer to store more than |privilege_name|\0 as | 72 // There is no point getting a buffer to store more than |privilege_name|\0 as |
| 73 // anything longer will obviously not be equal to |privilege_name|. | 73 // anything longer will obviously not be equal to |privilege_name|. |
| 74 const DWORD desired_size = static_cast<DWORD>(wcslen(privilege_name)); | 74 const DWORD desired_size = static_cast<DWORD>(wcslen(privilege_name)); |
| 75 const DWORD buffer_size = desired_size + 1; | 75 const DWORD buffer_size = desired_size + 1; |
| 76 scoped_ptr<wchar_t[]> name_buffer(new wchar_t[buffer_size]); | 76 std::unique_ptr<wchar_t[]> name_buffer(new wchar_t[buffer_size]); |
| 77 for (int i = privileges->PrivilegeCount - 1; i >= 0 ; --i) { | 77 for (int i = privileges->PrivilegeCount - 1; i >= 0 ; --i) { |
| 78 LUID_AND_ATTRIBUTES& luid_and_att = privileges->Privileges[i]; | 78 LUID_AND_ATTRIBUTES& luid_and_att = privileges->Privileges[i]; |
| 79 DWORD size = buffer_size; | 79 DWORD size = buffer_size; |
| 80 ::LookupPrivilegeName(NULL, &luid_and_att.Luid, name_buffer.get(), &size); | 80 ::LookupPrivilegeName(NULL, &luid_and_att.Luid, name_buffer.get(), &size); |
| 81 if (size == desired_size && | 81 if (size == desired_size && |
| 82 wcscmp(name_buffer.get(), privilege_name) == 0) { | 82 wcscmp(name_buffer.get(), privilege_name) == 0) { |
| 83 return luid_and_att.Attributes == SE_PRIVILEGE_ENABLED; | 83 return luid_and_att.Attributes == SE_PRIVILEGE_ENABLED; |
| 84 } | 84 } |
| 85 } | 85 } |
| 86 return false; | 86 return false; |
| 87 } | 87 } |
| 88 | 88 |
| 89 } // namespace | 89 } // namespace |
| 90 | 90 |
| 91 // Test that we are parsing Chrome version correctly. | 91 // Test that we are parsing Chrome version correctly. |
| 92 TEST(SetupUtilTest, GetMaxVersionFromArchiveDirTest) { | 92 TEST(SetupUtilTest, GetMaxVersionFromArchiveDirTest) { |
| 93 // Create a version dir | 93 // Create a version dir |
| 94 base::ScopedTempDir test_dir; | 94 base::ScopedTempDir test_dir; |
| 95 ASSERT_TRUE(test_dir.CreateUniqueTempDir()); | 95 ASSERT_TRUE(test_dir.CreateUniqueTempDir()); |
| 96 base::FilePath chrome_dir = test_dir.path().AppendASCII("1.0.0.0"); | 96 base::FilePath chrome_dir = test_dir.path().AppendASCII("1.0.0.0"); |
| 97 base::CreateDirectory(chrome_dir); | 97 base::CreateDirectory(chrome_dir); |
| 98 ASSERT_TRUE(base::PathExists(chrome_dir)); | 98 ASSERT_TRUE(base::PathExists(chrome_dir)); |
| 99 scoped_ptr<Version> version( | 99 std::unique_ptr<Version> version( |
| 100 installer::GetMaxVersionFromArchiveDir(test_dir.path())); | 100 installer::GetMaxVersionFromArchiveDir(test_dir.path())); |
| 101 ASSERT_EQ(version->GetString(), "1.0.0.0"); | 101 ASSERT_EQ(version->GetString(), "1.0.0.0"); |
| 102 | 102 |
| 103 base::DeleteFile(chrome_dir, true); | 103 base::DeleteFile(chrome_dir, true); |
| 104 ASSERT_FALSE(base::PathExists(chrome_dir)) << chrome_dir.value(); | 104 ASSERT_FALSE(base::PathExists(chrome_dir)) << chrome_dir.value(); |
| 105 ASSERT_TRUE(installer::GetMaxVersionFromArchiveDir(test_dir.path()) == NULL); | 105 ASSERT_TRUE(installer::GetMaxVersionFromArchiveDir(test_dir.path()) == NULL); |
| 106 | 106 |
| 107 chrome_dir = test_dir.path().AppendASCII("ABC"); | 107 chrome_dir = test_dir.path().AppendASCII("ABC"); |
| 108 base::CreateDirectory(chrome_dir); | 108 base::CreateDirectory(chrome_dir); |
| 109 ASSERT_TRUE(base::PathExists(chrome_dir)); | 109 ASSERT_TRUE(base::PathExists(chrome_dir)); |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 196 return installer::AdjustProcessPriority() ? PCCR_CHANGED : PCCR_UNCHANGED; | 196 return installer::AdjustProcessPriority() ? PCCR_CHANGED : PCCR_UNCHANGED; |
| 197 } | 197 } |
| 198 | 198 |
| 199 namespace { | 199 namespace { |
| 200 | 200 |
| 201 // A scoper that sets/resets the current process's priority class. | 201 // A scoper that sets/resets the current process's priority class. |
| 202 class ScopedPriorityClass { | 202 class ScopedPriorityClass { |
| 203 public: | 203 public: |
| 204 // Applies |priority_class|, returning an instance if a change was made. | 204 // Applies |priority_class|, returning an instance if a change was made. |
| 205 // Otherwise, returns an empty scoped_ptr. | 205 // Otherwise, returns an empty scoped_ptr. |
| 206 static scoped_ptr<ScopedPriorityClass> Create(DWORD priority_class); | 206 static std::unique_ptr<ScopedPriorityClass> Create(DWORD priority_class); |
| 207 ~ScopedPriorityClass(); | 207 ~ScopedPriorityClass(); |
| 208 | 208 |
| 209 private: | 209 private: |
| 210 explicit ScopedPriorityClass(DWORD original_priority_class); | 210 explicit ScopedPriorityClass(DWORD original_priority_class); |
| 211 DWORD original_priority_class_; | 211 DWORD original_priority_class_; |
| 212 DISALLOW_COPY_AND_ASSIGN(ScopedPriorityClass); | 212 DISALLOW_COPY_AND_ASSIGN(ScopedPriorityClass); |
| 213 }; | 213 }; |
| 214 | 214 |
| 215 scoped_ptr<ScopedPriorityClass> ScopedPriorityClass::Create( | 215 std::unique_ptr<ScopedPriorityClass> ScopedPriorityClass::Create( |
| 216 DWORD priority_class) { | 216 DWORD priority_class) { |
| 217 HANDLE this_process = ::GetCurrentProcess(); | 217 HANDLE this_process = ::GetCurrentProcess(); |
| 218 DWORD original_priority_class = ::GetPriorityClass(this_process); | 218 DWORD original_priority_class = ::GetPriorityClass(this_process); |
| 219 EXPECT_NE(0U, original_priority_class); | 219 EXPECT_NE(0U, original_priority_class); |
| 220 if (original_priority_class && original_priority_class != priority_class) { | 220 if (original_priority_class && original_priority_class != priority_class) { |
| 221 BOOL result = ::SetPriorityClass(this_process, priority_class); | 221 BOOL result = ::SetPriorityClass(this_process, priority_class); |
| 222 EXPECT_NE(FALSE, result); | 222 EXPECT_NE(FALSE, result); |
| 223 if (result) { | 223 if (result) { |
| 224 return scoped_ptr<ScopedPriorityClass>( | 224 return std::unique_ptr<ScopedPriorityClass>( |
| 225 new ScopedPriorityClass(original_priority_class)); | 225 new ScopedPriorityClass(original_priority_class)); |
| 226 } | 226 } |
| 227 } | 227 } |
| 228 return scoped_ptr<ScopedPriorityClass>(); | 228 return std::unique_ptr<ScopedPriorityClass>(); |
| 229 } | 229 } |
| 230 | 230 |
| 231 ScopedPriorityClass::ScopedPriorityClass(DWORD original_priority_class) | 231 ScopedPriorityClass::ScopedPriorityClass(DWORD original_priority_class) |
| 232 : original_priority_class_(original_priority_class) {} | 232 : original_priority_class_(original_priority_class) {} |
| 233 | 233 |
| 234 ScopedPriorityClass::~ScopedPriorityClass() { | 234 ScopedPriorityClass::~ScopedPriorityClass() { |
| 235 BOOL result = ::SetPriorityClass(::GetCurrentProcess(), | 235 BOOL result = ::SetPriorityClass(::GetCurrentProcess(), |
| 236 original_priority_class_); | 236 original_priority_class_); |
| 237 EXPECT_NE(FALSE, result); | 237 EXPECT_NE(FALSE, result); |
| 238 } | 238 } |
| (...skipping 18 matching lines...) Expand all Loading... |
| 257 // Launching a subprocess at normal priority class is a noop. | 257 // Launching a subprocess at normal priority class is a noop. |
| 258 TEST(SetupUtilTest, AdjustFromNormalPriority) { | 258 TEST(SetupUtilTest, AdjustFromNormalPriority) { |
| 259 ASSERT_EQ(static_cast<DWORD>(NORMAL_PRIORITY_CLASS), | 259 ASSERT_EQ(static_cast<DWORD>(NORMAL_PRIORITY_CLASS), |
| 260 ::GetPriorityClass(::GetCurrentProcess())); | 260 ::GetPriorityClass(::GetCurrentProcess())); |
| 261 EXPECT_EQ(PCCR_UNCHANGED, RelaunchAndDoProcessPriorityAdjustment()); | 261 EXPECT_EQ(PCCR_UNCHANGED, RelaunchAndDoProcessPriorityAdjustment()); |
| 262 } | 262 } |
| 263 | 263 |
| 264 // Launching a subprocess below normal priority class drops it to bg mode for | 264 // Launching a subprocess below normal priority class drops it to bg mode for |
| 265 // sufficiently recent operating systems. | 265 // sufficiently recent operating systems. |
| 266 TEST(SetupUtilTest, AdjustFromBelowNormalPriority) { | 266 TEST(SetupUtilTest, AdjustFromBelowNormalPriority) { |
| 267 scoped_ptr<ScopedPriorityClass> below_normal = | 267 std::unique_ptr<ScopedPriorityClass> below_normal = |
| 268 ScopedPriorityClass::Create(BELOW_NORMAL_PRIORITY_CLASS); | 268 ScopedPriorityClass::Create(BELOW_NORMAL_PRIORITY_CLASS); |
| 269 ASSERT_TRUE(below_normal); | 269 ASSERT_TRUE(below_normal); |
| 270 if (base::win::GetVersion() > base::win::VERSION_SERVER_2003) | 270 if (base::win::GetVersion() > base::win::VERSION_SERVER_2003) |
| 271 EXPECT_EQ(PCCR_CHANGED, RelaunchAndDoProcessPriorityAdjustment()); | 271 EXPECT_EQ(PCCR_CHANGED, RelaunchAndDoProcessPriorityAdjustment()); |
| 272 else | 272 else |
| 273 EXPECT_EQ(PCCR_UNCHANGED, RelaunchAndDoProcessPriorityAdjustment()); | 273 EXPECT_EQ(PCCR_UNCHANGED, RelaunchAndDoProcessPriorityAdjustment()); |
| 274 } | 274 } |
| 275 | 275 |
| 276 namespace { | 276 namespace { |
| 277 | 277 |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 369 original_state_->GetNonVersionedProductState(kSystemInstall_, | 369 original_state_->GetNonVersionedProductState(kSystemInstall_, |
| 370 kProductType_)) | 370 kProductType_)) |
| 371 ->set_version(Version()); | 371 ->set_version(Version()); |
| 372 } | 372 } |
| 373 | 373 |
| 374 static const bool kSystemInstall_; | 374 static const bool kSystemInstall_; |
| 375 static const BrowserDistribution::Type kProductType_; | 375 static const BrowserDistribution::Type kProductType_; |
| 376 base::ScopedTempDir test_dir_; | 376 base::ScopedTempDir test_dir_; |
| 377 Version product_version_; | 377 Version product_version_; |
| 378 Version max_version_; | 378 Version max_version_; |
| 379 scoped_ptr<FakeInstallationState> original_state_; | 379 std::unique_ptr<FakeInstallationState> original_state_; |
| 380 scoped_ptr<installer::InstallerState> installer_state_; | 380 std::unique_ptr<installer::InstallerState> installer_state_; |
| 381 | 381 |
| 382 private: | 382 private: |
| 383 registry_util::RegistryOverrideManager registry_override_manager_; | 383 registry_util::RegistryOverrideManager registry_override_manager_; |
| 384 | 384 |
| 385 DISALLOW_COPY_AND_ASSIGN(FindArchiveToPatchTest); | 385 DISALLOW_COPY_AND_ASSIGN(FindArchiveToPatchTest); |
| 386 }; | 386 }; |
| 387 | 387 |
| 388 const bool FindArchiveToPatchTest::kSystemInstall_ = false; | 388 const bool FindArchiveToPatchTest::kSystemInstall_ = false; |
| 389 const BrowserDistribution::Type FindArchiveToPatchTest::kProductType_ = | 389 const BrowserDistribution::Type FindArchiveToPatchTest::kProductType_ = |
| 390 BrowserDistribution::CHROME_BROWSER; | 390 BrowserDistribution::CHROME_BROWSER; |
| (...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 643 } | 643 } |
| 644 | 644 |
| 645 // Ensure that all values are absent. | 645 // Ensure that all values are absent. |
| 646 { | 646 { |
| 647 base::win::RegistryValueIterator it(root_, path_.c_str()); | 647 base::win::RegistryValueIterator it(root_, path_.c_str()); |
| 648 ASSERT_EQ(0u, it.ValueCount()); | 648 ASSERT_EQ(0u, it.ValueCount()); |
| 649 } | 649 } |
| 650 } | 650 } |
| 651 | 651 |
| 652 } // namespace installer | 652 } // namespace installer |
| OLD | NEW |