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 <memory> |
(...skipping 20 matching lines...) Expand all Loading... |
31 #include "chrome/installer/setup/installer_state.h" | 31 #include "chrome/installer/setup/installer_state.h" |
32 #include "chrome/installer/setup/setup_constants.h" | 32 #include "chrome/installer/setup/setup_constants.h" |
33 #include "chrome/installer/setup/setup_util.h" | 33 #include "chrome/installer/setup/setup_util.h" |
34 #include "chrome/installer/util/browser_distribution.h" | 34 #include "chrome/installer/util/browser_distribution.h" |
35 #include "chrome/installer/util/google_update_constants.h" | 35 #include "chrome/installer/util/google_update_constants.h" |
36 #include "chrome/installer/util/installation_state.h" | 36 #include "chrome/installer/util/installation_state.h" |
37 #include "chrome/installer/util/updating_app_registration_data.h" | 37 #include "chrome/installer/util/updating_app_registration_data.h" |
38 #include "chrome/installer/util/util_constants.h" | 38 #include "chrome/installer/util/util_constants.h" |
39 #include "testing/gtest/include/gtest/gtest.h" | 39 #include "testing/gtest/include/gtest/gtest.h" |
40 | 40 |
41 namespace { | |
42 | |
43 // The privilege tested in ScopeTokenPrivilege tests below. | |
44 // Use SE_RESTORE_NAME as it is one of the many privileges that is available, | |
45 // but not enabled by default on processes running at high integrity. | |
46 static const wchar_t kTestedPrivilege[] = SE_RESTORE_NAME; | |
47 | |
48 // Returns true if the current process' token has privilege |privilege_name| | |
49 // enabled. | |
50 bool CurrentProcessHasPrivilege(const wchar_t* privilege_name) { | |
51 HANDLE temp_handle; | |
52 if (!::OpenProcessToken(::GetCurrentProcess(), TOKEN_QUERY, | |
53 &temp_handle)) { | |
54 ADD_FAILURE(); | |
55 return false; | |
56 } | |
57 | |
58 base::win::ScopedHandle token(temp_handle); | |
59 | |
60 // First get the size of the buffer needed for |privileges| below. | |
61 DWORD size; | |
62 EXPECT_FALSE(::GetTokenInformation(token.Get(), TokenPrivileges, NULL, 0, | |
63 &size)); | |
64 | |
65 std::unique_ptr<BYTE[]> privileges_bytes(new BYTE[size]); | |
66 TOKEN_PRIVILEGES* privileges = | |
67 reinterpret_cast<TOKEN_PRIVILEGES*>(privileges_bytes.get()); | |
68 | |
69 if (!::GetTokenInformation(token.Get(), TokenPrivileges, privileges, size, | |
70 &size)) { | |
71 ADD_FAILURE(); | |
72 return false; | |
73 } | |
74 | |
75 // There is no point getting a buffer to store more than |privilege_name|\0 as | |
76 // anything longer will obviously not be equal to |privilege_name|. | |
77 const DWORD desired_size = static_cast<DWORD>(wcslen(privilege_name)); | |
78 const DWORD buffer_size = desired_size + 1; | |
79 std::unique_ptr<wchar_t[]> name_buffer(new wchar_t[buffer_size]); | |
80 for (int i = privileges->PrivilegeCount - 1; i >= 0 ; --i) { | |
81 LUID_AND_ATTRIBUTES& luid_and_att = privileges->Privileges[i]; | |
82 DWORD size = buffer_size; | |
83 ::LookupPrivilegeName(NULL, &luid_and_att.Luid, name_buffer.get(), &size); | |
84 if (size == desired_size && | |
85 wcscmp(name_buffer.get(), privilege_name) == 0) { | |
86 return luid_and_att.Attributes == SE_PRIVILEGE_ENABLED; | |
87 } | |
88 } | |
89 return false; | |
90 } | |
91 | |
92 } // namespace | |
93 | |
94 // Test that we are parsing Chrome version correctly. | 41 // Test that we are parsing Chrome version correctly. |
95 TEST(SetupUtilTest, GetMaxVersionFromArchiveDirTest) { | 42 TEST(SetupUtilTest, GetMaxVersionFromArchiveDirTest) { |
96 // Create a version dir | 43 // Create a version dir |
97 base::ScopedTempDir test_dir; | 44 base::ScopedTempDir test_dir; |
98 ASSERT_TRUE(test_dir.CreateUniqueTempDir()); | 45 ASSERT_TRUE(test_dir.CreateUniqueTempDir()); |
99 base::FilePath chrome_dir = test_dir.GetPath().AppendASCII("1.0.0.0"); | 46 base::FilePath chrome_dir = test_dir.GetPath().AppendASCII("1.0.0.0"); |
100 base::CreateDirectory(chrome_dir); | 47 base::CreateDirectory(chrome_dir); |
101 ASSERT_TRUE(base::PathExists(chrome_dir)); | 48 ASSERT_TRUE(base::PathExists(chrome_dir)); |
102 std::unique_ptr<base::Version> version( | 49 std::unique_ptr<base::Version> version( |
103 installer::GetMaxVersionFromArchiveDir(test_dir.GetPath())); | 50 installer::GetMaxVersionFromArchiveDir(test_dir.GetPath())); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
137 ASSERT_TRUE(test_dir.CreateUniqueTempDir()); | 84 ASSERT_TRUE(test_dir.CreateUniqueTempDir()); |
138 base::FilePath test_file; | 85 base::FilePath test_file; |
139 base::CreateTemporaryFileInDir(test_dir.GetPath(), &test_file); | 86 base::CreateTemporaryFileInDir(test_dir.GetPath(), &test_file); |
140 ASSERT_TRUE(base::PathExists(test_file)); | 87 ASSERT_TRUE(base::PathExists(test_file)); |
141 base::WriteFile(test_file, "foo", 3); | 88 base::WriteFile(test_file, "foo", 3); |
142 EXPECT_TRUE(installer::DeleteFileFromTempProcess(test_file, 0)); | 89 EXPECT_TRUE(installer::DeleteFileFromTempProcess(test_file, 0)); |
143 base::PlatformThread::Sleep(TestTimeouts::tiny_timeout() * 3); | 90 base::PlatformThread::Sleep(TestTimeouts::tiny_timeout() * 3); |
144 EXPECT_FALSE(base::PathExists(test_file)) << test_file.value(); | 91 EXPECT_FALSE(base::PathExists(test_file)) << test_file.value(); |
145 } | 92 } |
146 | 93 |
147 // Note: This test is only valid when run at high integrity (i.e. it will fail | |
148 // at medium integrity). | |
149 TEST(SetupUtilTest, ScopedTokenPrivilegeBasic) { | |
150 ASSERT_FALSE(CurrentProcessHasPrivilege(kTestedPrivilege)); | |
151 | |
152 if (!::IsUserAnAdmin()) { | |
153 LOG(WARNING) << "Skipping SetupUtilTest.ScopedTokenPrivilegeBasic due to " | |
154 "not running as admin."; | |
155 return; | |
156 } | |
157 | |
158 { | |
159 installer::ScopedTokenPrivilege test_scoped_privilege(kTestedPrivilege); | |
160 ASSERT_TRUE(test_scoped_privilege.is_enabled()); | |
161 ASSERT_TRUE(CurrentProcessHasPrivilege(kTestedPrivilege)); | |
162 } | |
163 | |
164 ASSERT_FALSE(CurrentProcessHasPrivilege(kTestedPrivilege)); | |
165 } | |
166 | |
167 // Note: This test is only valid when run at high integrity (i.e. it will fail | |
168 // at medium integrity). | |
169 TEST(SetupUtilTest, ScopedTokenPrivilegeAlreadyEnabled) { | |
170 ASSERT_FALSE(CurrentProcessHasPrivilege(kTestedPrivilege)); | |
171 | |
172 if (!::IsUserAnAdmin()) { | |
173 LOG(WARNING) << "Skipping SetupUtilTest.ScopedTokenPrivilegeAlreadyEnabled " | |
174 "due to not running as admin."; | |
175 return; | |
176 } | |
177 | |
178 { | |
179 installer::ScopedTokenPrivilege test_scoped_privilege(kTestedPrivilege); | |
180 ASSERT_TRUE(test_scoped_privilege.is_enabled()); | |
181 ASSERT_TRUE(CurrentProcessHasPrivilege(kTestedPrivilege)); | |
182 { | |
183 installer::ScopedTokenPrivilege dup_scoped_privilege(kTestedPrivilege); | |
184 ASSERT_TRUE(dup_scoped_privilege.is_enabled()); | |
185 ASSERT_TRUE(CurrentProcessHasPrivilege(kTestedPrivilege)); | |
186 } | |
187 ASSERT_TRUE(CurrentProcessHasPrivilege(kTestedPrivilege)); | |
188 } | |
189 | |
190 ASSERT_FALSE(CurrentProcessHasPrivilege(kTestedPrivilege)); | |
191 } | |
192 | |
193 TEST(SetupUtilTest, GuidToSquid) { | 94 TEST(SetupUtilTest, GuidToSquid) { |
194 ASSERT_EQ(installer::GuidToSquid(L"EDA620E3-AA98-3846-B81E-3493CB2E0E02"), | 95 ASSERT_EQ(installer::GuidToSquid(L"EDA620E3-AA98-3846-B81E-3493CB2E0E02"), |
195 L"3E026ADE89AA64838BE14339BCE2E020"); | 96 L"3E026ADE89AA64838BE14339BCE2E020"); |
196 } | 97 } |
197 | 98 |
198 TEST(SetupUtilTest, RegisterEventLogProvider) { | 99 TEST(SetupUtilTest, RegisterEventLogProvider) { |
199 registry_util::RegistryOverrideManager registry_override_manager; | 100 registry_util::RegistryOverrideManager registry_override_manager; |
200 ASSERT_NO_FATAL_FAILURE( | 101 ASSERT_NO_FATAL_FAILURE( |
201 registry_override_manager.OverrideRegistry(HKEY_LOCAL_MACHINE)); | 102 registry_override_manager.OverrideRegistry(HKEY_LOCAL_MACHINE)); |
202 | 103 |
(...skipping 615 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
818 EXPECT_FALSE(HasCommandExecuteImplClassKey()); | 719 EXPECT_FALSE(HasCommandExecuteImplClassKey()); |
819 #if defined(GOOGLE_CHROME_BUILD) | 720 #if defined(GOOGLE_CHROME_BUILD) |
820 EXPECT_FALSE(HasMultiGCFVersionKey()); | 721 EXPECT_FALSE(HasMultiGCFVersionKey()); |
821 EXPECT_FALSE(HasAppLauncherVersionKey()); | 722 EXPECT_FALSE(HasAppLauncherVersionKey()); |
822 EXPECT_FALSE(HasAppHostExe()); | 723 EXPECT_FALSE(HasAppHostExe()); |
823 EXPECT_FALSE(HasInstallExtensionCommand()); | 724 EXPECT_FALSE(HasInstallExtensionCommand()); |
824 #endif // GOOGLE_CHROME_BUILD | 725 #endif // GOOGLE_CHROME_BUILD |
825 } | 726 } |
826 | 727 |
827 } // namespace installer | 728 } // namespace installer |
OLD | NEW |