| 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 <windows.h> | 5 #include <windows.h> |
| 6 | 6 |
| 7 #include <string> |
| 8 |
| 7 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 8 #include "base/path_service.h" | 10 #include "base/path_service.h" |
| 9 #include "base/scoped_temp_dir.h" | 11 #include "base/scoped_temp_dir.h" |
| 10 #include "base/time.h" | 12 #include "base/time.h" |
| 13 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/threading/platform_thread.h" | 14 #include "base/threading/platform_thread.h" |
| 15 #include "base/win/scoped_handle.h" |
| 12 #include "chrome/common/chrome_paths.h" | 16 #include "chrome/common/chrome_paths.h" |
| 13 #include "chrome/installer/setup/setup_util.h" | 17 #include "chrome/installer/setup/setup_util.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 19 |
| 16 namespace { | 20 namespace { |
| 17 class SetupUtilTest : public testing::Test { | 21 |
| 22 class SetupUtilTestWithDir : public testing::Test { |
| 18 protected: | 23 protected: |
| 19 virtual void SetUp() { | 24 virtual void SetUp() { |
| 20 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &data_dir_)); | 25 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &data_dir_)); |
| 21 data_dir_ = data_dir_.AppendASCII("installer"); | 26 data_dir_ = data_dir_.AppendASCII("installer"); |
| 22 ASSERT_TRUE(file_util::PathExists(data_dir_)); | 27 ASSERT_TRUE(file_util::PathExists(data_dir_)); |
| 23 | 28 |
| 24 // Create a temp directory for testing. | 29 // Create a temp directory for testing. |
| 25 ASSERT_TRUE(test_dir_.CreateUniqueTempDir()); | 30 ASSERT_TRUE(test_dir_.CreateUniqueTempDir()); |
| 26 } | 31 } |
| 27 | 32 |
| 28 virtual void TearDown() { | 33 virtual void TearDown() { |
| 29 // Clean up test directory manually so we can fail if it leaks. | 34 // Clean up test directory manually so we can fail if it leaks. |
| 30 ASSERT_TRUE(test_dir_.Delete()); | 35 ASSERT_TRUE(test_dir_.Delete()); |
| 31 } | 36 } |
| 32 | 37 |
| 33 // The temporary directory used to contain the test operations. | 38 // The temporary directory used to contain the test operations. |
| 34 ScopedTempDir test_dir_; | 39 ScopedTempDir test_dir_; |
| 35 | 40 |
| 36 // The path to input data used in tests. | 41 // The path to input data used in tests. |
| 37 FilePath data_dir_; | 42 FilePath data_dir_; |
| 38 }; | 43 }; |
| 44 |
| 45 // The privilege tested in ScopeTokenPrivilege tests below. |
| 46 // Use SE_RESTORE_NAME as it is one of the many privileges that is available, |
| 47 // but not enabled by default on processes running at high integrity. |
| 48 static const wchar_t kTestedPrivilege[] = SE_RESTORE_NAME; |
| 49 |
| 50 // Returns true if the current process' token has privilege |privilege_name| |
| 51 // enabled. |
| 52 bool CurrentProcessHasPrivilege(const wchar_t* privilege_name) { |
| 53 base::win::ScopedHandle token; |
| 54 if (!::OpenProcessToken(::GetCurrentProcess(), TOKEN_QUERY, |
| 55 token.Receive())) { |
| 56 ADD_FAILURE(); |
| 57 return false; |
| 58 } |
| 59 |
| 60 // First get the size of the buffer needed for |privileges| below. |
| 61 DWORD size; |
| 62 EXPECT_FALSE(::GetTokenInformation(token, TokenPrivileges, NULL, 0, &size)); |
| 63 |
| 64 scoped_array<BYTE> privileges_bytes(new BYTE[size]); |
| 65 TOKEN_PRIVILEGES* privileges = |
| 66 reinterpret_cast<TOKEN_PRIVILEGES*>(privileges_bytes.get()); |
| 67 |
| 68 if (!::GetTokenInformation(token, TokenPrivileges, privileges, size, &size)) { |
| 69 ADD_FAILURE(); |
| 70 return false; |
| 71 } |
| 72 |
| 73 // There is no point getting a buffer to store more than |privilege_name|\0 as |
| 74 // anything longer will obviously not be equal to |privilege_name|. |
| 75 const DWORD desired_size = wcslen(privilege_name); |
| 76 const DWORD buffer_size = desired_size + 1; |
| 77 scoped_array<wchar_t> name_buffer(new wchar_t[buffer_size]); |
| 78 for (int i = privileges->PrivilegeCount - 1; i >= 0 ; --i) { |
| 79 LUID_AND_ATTRIBUTES& luid_and_att = privileges->Privileges[i]; |
| 80 DWORD size = buffer_size; |
| 81 ::LookupPrivilegeName(NULL, &luid_and_att.Luid, name_buffer.get(), &size); |
| 82 if (size == desired_size && |
| 83 wcscmp(name_buffer.get(), privilege_name) == 0) { |
| 84 return luid_and_att.Attributes == SE_PRIVILEGE_ENABLED; |
| 85 } |
| 86 } |
| 87 return false; |
| 39 } | 88 } |
| 40 | 89 |
| 90 } // namespace |
| 91 |
| 41 // Test that we are parsing Chrome version correctly. | 92 // Test that we are parsing Chrome version correctly. |
| 42 TEST_F(SetupUtilTest, ApplyDiffPatchTest) { | 93 TEST_F(SetupUtilTestWithDir, ApplyDiffPatchTest) { |
| 43 FilePath work_dir(test_dir_.path()); | 94 FilePath work_dir(test_dir_.path()); |
| 44 work_dir = work_dir.AppendASCII("ApplyDiffPatchTest"); | 95 work_dir = work_dir.AppendASCII("ApplyDiffPatchTest"); |
| 45 ASSERT_FALSE(file_util::PathExists(work_dir)); | 96 ASSERT_FALSE(file_util::PathExists(work_dir)); |
| 46 EXPECT_TRUE(file_util::CreateDirectory(work_dir)); | 97 EXPECT_TRUE(file_util::CreateDirectory(work_dir)); |
| 47 ASSERT_TRUE(file_util::PathExists(work_dir)); | 98 ASSERT_TRUE(file_util::PathExists(work_dir)); |
| 48 | 99 |
| 49 FilePath src = data_dir_.AppendASCII("archive1.7z"); | 100 FilePath src = data_dir_.AppendASCII("archive1.7z"); |
| 50 FilePath patch = data_dir_.AppendASCII("archive.diff"); | 101 FilePath patch = data_dir_.AppendASCII("archive.diff"); |
| 51 FilePath dest = work_dir.AppendASCII("archive2.7z"); | 102 FilePath dest = work_dir.AppendASCII("archive2.7z"); |
| 52 EXPECT_EQ(installer::ApplyDiffPatch(src, patch, dest, NULL), 0); | 103 EXPECT_EQ(installer::ApplyDiffPatch(src, patch, dest, NULL), 0); |
| 53 FilePath base = data_dir_.AppendASCII("archive2.7z"); | 104 FilePath base = data_dir_.AppendASCII("archive2.7z"); |
| 54 EXPECT_TRUE(file_util::ContentsEqual(dest, base)); | 105 EXPECT_TRUE(file_util::ContentsEqual(dest, base)); |
| 55 | 106 |
| 56 EXPECT_EQ(installer::ApplyDiffPatch(FilePath(), FilePath(), FilePath(), NULL), | 107 EXPECT_EQ(installer::ApplyDiffPatch(FilePath(), FilePath(), FilePath(), NULL), |
| 57 6); | 108 6); |
| 58 } | 109 } |
| 59 | 110 |
| 60 // Test that we are parsing Chrome version correctly. | 111 // Test that we are parsing Chrome version correctly. |
| 61 TEST_F(SetupUtilTest, GetMaxVersionFromArchiveDirTest) { | 112 TEST_F(SetupUtilTestWithDir, GetMaxVersionFromArchiveDirTest) { |
| 62 // Create a version dir | 113 // Create a version dir |
| 63 FilePath chrome_dir = test_dir_.path().AppendASCII("1.0.0.0"); | 114 FilePath chrome_dir = test_dir_.path().AppendASCII("1.0.0.0"); |
| 64 file_util::CreateDirectory(chrome_dir); | 115 file_util::CreateDirectory(chrome_dir); |
| 65 ASSERT_TRUE(file_util::PathExists(chrome_dir)); | 116 ASSERT_TRUE(file_util::PathExists(chrome_dir)); |
| 66 scoped_ptr<Version> version( | 117 scoped_ptr<Version> version( |
| 67 installer::GetMaxVersionFromArchiveDir(test_dir_.path())); | 118 installer::GetMaxVersionFromArchiveDir(test_dir_.path())); |
| 68 ASSERT_EQ(version->GetString(), "1.0.0.0"); | 119 ASSERT_EQ(version->GetString(), "1.0.0.0"); |
| 69 | 120 |
| 70 file_util::Delete(chrome_dir, true); | 121 file_util::Delete(chrome_dir, true); |
| 71 ASSERT_FALSE(file_util::PathExists(chrome_dir)); | 122 ASSERT_FALSE(file_util::PathExists(chrome_dir)); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 87 file_util::CreateDirectory(chrome_dir); | 138 file_util::CreateDirectory(chrome_dir); |
| 88 ASSERT_TRUE(file_util::PathExists(chrome_dir)); | 139 ASSERT_TRUE(file_util::PathExists(chrome_dir)); |
| 89 chrome_dir = test_dir_.path().AppendASCII("1.1.1.1"); | 140 chrome_dir = test_dir_.path().AppendASCII("1.1.1.1"); |
| 90 file_util::CreateDirectory(chrome_dir); | 141 file_util::CreateDirectory(chrome_dir); |
| 91 ASSERT_TRUE(file_util::PathExists(chrome_dir)); | 142 ASSERT_TRUE(file_util::PathExists(chrome_dir)); |
| 92 | 143 |
| 93 version.reset(installer::GetMaxVersionFromArchiveDir(test_dir_.path())); | 144 version.reset(installer::GetMaxVersionFromArchiveDir(test_dir_.path())); |
| 94 ASSERT_EQ(version->GetString(), "9.9.9.9"); | 145 ASSERT_EQ(version->GetString(), "9.9.9.9"); |
| 95 } | 146 } |
| 96 | 147 |
| 97 TEST_F(SetupUtilTest, DeleteFileFromTempProcess) { | 148 TEST_F(SetupUtilTestWithDir, DeleteFileFromTempProcess) { |
| 98 FilePath test_file; | 149 FilePath test_file; |
| 99 file_util::CreateTemporaryFileInDir(test_dir_.path(), &test_file); | 150 file_util::CreateTemporaryFileInDir(test_dir_.path(), &test_file); |
| 100 ASSERT_TRUE(file_util::PathExists(test_file)); | 151 ASSERT_TRUE(file_util::PathExists(test_file)); |
| 101 file_util::WriteFile(test_file, "foo", 3); | 152 file_util::WriteFile(test_file, "foo", 3); |
| 102 EXPECT_TRUE(installer::DeleteFileFromTempProcess(test_file, 0)); | 153 EXPECT_TRUE(installer::DeleteFileFromTempProcess(test_file, 0)); |
| 103 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(200)); | 154 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(200)); |
| 104 EXPECT_FALSE(file_util::PathExists(test_file)); | 155 EXPECT_FALSE(file_util::PathExists(test_file)); |
| 105 } | 156 } |
| 157 |
| 158 // Note: This test is only valid when run at high integrity (i.e. it will fail |
| 159 // at medium integrity). |
| 160 TEST(SetupUtilTest, ScopedTokenPrivilegeBasic) { |
| 161 ASSERT_FALSE(CurrentProcessHasPrivilege(kTestedPrivilege)); |
| 162 |
| 163 { |
| 164 installer::ScopedTokenPrivilege test_scoped_privilege(kTestedPrivilege); |
| 165 ASSERT_TRUE(test_scoped_privilege.is_enabled()); |
| 166 ASSERT_TRUE(CurrentProcessHasPrivilege(kTestedPrivilege)); |
| 167 } |
| 168 |
| 169 ASSERT_FALSE(CurrentProcessHasPrivilege(kTestedPrivilege)); |
| 170 } |
| 171 |
| 172 // Note: This test is only valid when run at high integrity (i.e. it will fail |
| 173 // at medium integrity). |
| 174 TEST(SetupUtilTest, ScopedTokenPrivilegeAlreadyEnabled) { |
| 175 ASSERT_FALSE(CurrentProcessHasPrivilege(kTestedPrivilege)); |
| 176 |
| 177 { |
| 178 installer::ScopedTokenPrivilege test_scoped_privilege(kTestedPrivilege); |
| 179 ASSERT_TRUE(test_scoped_privilege.is_enabled()); |
| 180 ASSERT_TRUE(CurrentProcessHasPrivilege(kTestedPrivilege)); |
| 181 { |
| 182 installer::ScopedTokenPrivilege dup_scoped_privilege(kTestedPrivilege); |
| 183 ASSERT_TRUE(dup_scoped_privilege.is_enabled()); |
| 184 ASSERT_TRUE(CurrentProcessHasPrivilege(kTestedPrivilege)); |
| 185 } |
| 186 ASSERT_TRUE(CurrentProcessHasPrivilege(kTestedPrivilege)); |
| 187 } |
| 188 |
| 189 ASSERT_FALSE(CurrentProcessHasPrivilege(kTestedPrivilege)); |
| 190 } |
| OLD | NEW |