OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 <tuple> | 5 #include <tuple> |
6 #include <windows.h> | 6 #include <windows.h> |
7 #include <versionhelpers.h> // windows.h must be before. | 7 #include <versionhelpers.h> // windows.h must be before. |
8 | 8 |
9 #include "base/test/test_reg_util_win.h" | 9 #include "base/test/test_reg_util_win.h" |
10 #include "base/win/registry.h" | 10 #include "base/win/registry.h" |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
65 &policy, sizeof(policy))) | 65 &policy, sizeof(policy))) |
66 return false; | 66 return false; |
67 | 67 |
68 return policy.DisableExtensionPoints; | 68 return policy.DisableExtensionPoints; |
69 } | 69 } |
70 | 70 |
71 return true; | 71 return true; |
72 } | 72 } |
73 | 73 |
74 void RegRedirect(nt::ROOT_KEY key, | 74 void RegRedirect(nt::ROOT_KEY key, |
75 registry_util::RegistryOverrideManager& rom) { | 75 registry_util::RegistryOverrideManager* rom) { |
| 76 ASSERT_NE(key, nt::AUTO); |
76 base::string16 temp; | 77 base::string16 temp; |
77 | 78 |
78 if (key == nt::HKCU) { | 79 if (key == nt::HKCU) { |
79 rom.OverrideRegistry(HKEY_CURRENT_USER, &temp); | 80 rom->OverrideRegistry(HKEY_CURRENT_USER, &temp); |
80 ::wcsncpy(nt::HKCU_override, temp.c_str(), nt::g_kRegMaxPathLen - 1); | 81 ASSERT_TRUE(nt::SetTestingOverride(nt::HKCU, temp)); |
81 } else if (key == nt::HKLM) { | 82 } else { |
82 rom.OverrideRegistry(HKEY_LOCAL_MACHINE, &temp); | 83 rom->OverrideRegistry(HKEY_LOCAL_MACHINE, &temp); |
83 ::wcsncpy(nt::HKLM_override, temp.c_str(), nt::g_kRegMaxPathLen - 1); | 84 ASSERT_TRUE(nt::SetTestingOverride(nt::HKLM, temp)); |
84 } | 85 } |
85 // nt::AUTO should not be passed into this function. | 86 } |
| 87 |
| 88 void CancelRegRedirect(nt::ROOT_KEY key) { |
| 89 ASSERT_NE(key, nt::AUTO); |
| 90 if (key == nt::HKCU) |
| 91 ASSERT_TRUE(nt::SetTestingOverride(nt::HKCU, base::string16())); |
| 92 else |
| 93 ASSERT_TRUE(nt::SetTestingOverride(nt::HKLM, base::string16())); |
86 } | 94 } |
87 | 95 |
88 TEST(ChromeElfUtilTest, CanaryTest) { | 96 TEST(ChromeElfUtilTest, CanaryTest) { |
89 EXPECT_TRUE(IsSxSChrome(kCanaryExePath)); | 97 EXPECT_TRUE(IsSxSChrome(kCanaryExePath)); |
90 EXPECT_FALSE(IsSxSChrome(kChromeUserExePath)); | 98 EXPECT_FALSE(IsSxSChrome(kChromeUserExePath)); |
91 EXPECT_FALSE(IsSxSChrome(kChromiumExePath)); | 99 EXPECT_FALSE(IsSxSChrome(kChromiumExePath)); |
92 } | 100 } |
93 | 101 |
94 TEST(ChromeElfUtilTest, SystemInstallTest) { | 102 TEST(ChromeElfUtilTest, SystemInstallTest) { |
95 EXPECT_TRUE(IsSystemInstall(kChromeSystemExePath)); | 103 EXPECT_TRUE(IsSystemInstall(kChromeSystemExePath)); |
96 EXPECT_FALSE(IsSystemInstall(kChromeUserExePath)); | 104 EXPECT_FALSE(IsSystemInstall(kChromeUserExePath)); |
97 } | 105 } |
98 | 106 |
99 TEST(ChromeElfUtilTest, BrowserProcessTest) { | 107 TEST(ChromeElfUtilTest, BrowserProcessTest) { |
100 EXPECT_EQ(ProcessType::UNINITIALIZED, g_process_type); | 108 EXPECT_EQ(ProcessType::UNINITIALIZED, g_process_type); |
101 InitializeProcessType(); | 109 InitializeProcessType(); |
102 EXPECT_FALSE(IsNonBrowserProcess()); | 110 EXPECT_FALSE(IsNonBrowserProcess()); |
103 } | 111 } |
104 | 112 |
105 TEST(ChromeElfUtilTest, BrowserProcessSecurityTest) { | 113 TEST(ChromeElfUtilTest, BrowserProcessSecurityTest) { |
106 if (!::IsWindows8OrGreater()) | 114 if (!::IsWindows8OrGreater()) |
107 return; | 115 return; |
108 | 116 |
109 // Set up registry override for this test. | 117 // Set up registry override for this test. |
110 registry_util::RegistryOverrideManager override_manager; | 118 registry_util::RegistryOverrideManager override_manager; |
111 RegRedirect(nt::HKCU, override_manager); | 119 RegRedirect(nt::HKCU, &override_manager); |
112 | 120 |
113 // First, ensure that the emergency-off finch signal works. | 121 // First, ensure that the emergency-off finch signal works. |
114 EXPECT_TRUE(SetSecurityFinchFlag(true)); | 122 EXPECT_TRUE(SetSecurityFinchFlag(true)); |
115 EarlyBrowserSecurity(); | 123 EarlyBrowserSecurity(); |
116 EXPECT_FALSE(IsSecuritySet()); | 124 EXPECT_FALSE(IsSecuritySet()); |
117 EXPECT_TRUE(SetSecurityFinchFlag(false)); | 125 EXPECT_TRUE(SetSecurityFinchFlag(false)); |
118 | 126 |
119 // Second, test that the process mitigation is set when no finch signal. | 127 // Second, test that the process mitigation is set when no finch signal. |
120 EarlyBrowserSecurity(); | 128 EarlyBrowserSecurity(); |
121 EXPECT_TRUE(IsSecuritySet()); | 129 EXPECT_TRUE(IsSecuritySet()); |
122 } | |
123 | 130 |
124 //------------------------------------------------------------------------------ | 131 CancelRegRedirect(nt::HKCU); |
125 // NT registry API tests (chrome_elf_reg) | |
126 //------------------------------------------------------------------------------ | |
127 | |
128 TEST(ChromeElfUtilTest, NTRegistry) { | |
129 HANDLE key_handle; | |
130 const wchar_t* dword_val_name = L"DwordTestValue"; | |
131 DWORD dword_val = 1234; | |
132 const wchar_t* sz_val_name = L"SzTestValue"; | |
133 base::string16 sz_val = L"blah de blah de blahhhhh."; | |
134 const wchar_t* sz_val_name2 = L"SzTestValueEmpty"; | |
135 base::string16 sz_val2 = L""; | |
136 const wchar_t* multisz_val_name = L"SzmultiTestValue"; | |
137 std::vector<base::string16> multisz_val; | |
138 base::string16 multi1 = L"one"; | |
139 base::string16 multi2 = L"two"; | |
140 base::string16 multi3 = L"three"; | |
141 const wchar_t* multisz_val_name2 = L"SzmultiTestValueBad"; | |
142 base::string16 multi_empty = L""; | |
143 const wchar_t* sz_new_key_1 = L"test\\new\\subkey"; | |
144 const wchar_t* sz_new_key_2 = L"test\\new\\subkey\\blah\\"; | |
145 const wchar_t* sz_new_key_3 = L"\\test\\new\\subkey\\\\blah2"; | |
146 | |
147 // Set up registry override for this test. | |
148 registry_util::RegistryOverrideManager override_manager; | |
149 RegRedirect(nt::HKCU, override_manager); | |
150 | |
151 // Create a temp key to play under. | |
152 ASSERT_TRUE(nt::CreateRegKey(nt::HKCU, elf_sec::kRegSecurityPath, | |
153 KEY_ALL_ACCESS, &key_handle)); | |
154 | |
155 // Exercise the supported getter & setter functions. | |
156 EXPECT_TRUE(nt::SetRegValueDWORD(key_handle, dword_val_name, dword_val)); | |
157 EXPECT_TRUE(nt::SetRegValueSZ(key_handle, sz_val_name, sz_val)); | |
158 EXPECT_TRUE(nt::SetRegValueSZ(key_handle, sz_val_name2, sz_val2)); | |
159 | |
160 DWORD get_dword = 0; | |
161 base::string16 get_sz; | |
162 EXPECT_TRUE(nt::QueryRegValueDWORD(key_handle, dword_val_name, &get_dword) && | |
163 get_dword == dword_val); | |
164 EXPECT_TRUE(nt::QueryRegValueSZ(key_handle, sz_val_name, &get_sz) && | |
165 get_sz.compare(sz_val) == 0); | |
166 EXPECT_TRUE(nt::QueryRegValueSZ(key_handle, sz_val_name2, &get_sz) && | |
167 get_sz.compare(sz_val2) == 0); | |
168 | |
169 multisz_val.push_back(multi1); | |
170 multisz_val.push_back(multi2); | |
171 multisz_val.push_back(multi3); | |
172 EXPECT_TRUE( | |
173 nt::SetRegValueMULTISZ(key_handle, multisz_val_name, multisz_val)); | |
174 multisz_val.clear(); | |
175 multisz_val.push_back(multi_empty); | |
176 EXPECT_TRUE( | |
177 nt::SetRegValueMULTISZ(key_handle, multisz_val_name2, multisz_val)); | |
178 multisz_val.clear(); | |
179 | |
180 EXPECT_TRUE( | |
181 nt::QueryRegValueMULTISZ(key_handle, multisz_val_name, &multisz_val)); | |
182 if (multisz_val.size() == 3) { | |
183 EXPECT_TRUE(multi1.compare(multisz_val.at(0)) == 0); | |
184 EXPECT_TRUE(multi2.compare(multisz_val.at(1)) == 0); | |
185 EXPECT_TRUE(multi3.compare(multisz_val.at(2)) == 0); | |
186 } else { | |
187 EXPECT_TRUE(false); | |
188 } | |
189 multisz_val.clear(); | |
190 | |
191 EXPECT_TRUE( | |
192 nt::QueryRegValueMULTISZ(key_handle, multisz_val_name2, &multisz_val)); | |
193 if (multisz_val.size() == 1) { | |
194 EXPECT_TRUE(multi_empty.compare(multisz_val.at(0)) == 0); | |
195 } else { | |
196 EXPECT_TRUE(false); | |
197 } | |
198 multisz_val.clear(); | |
199 | |
200 // Clean up | |
201 EXPECT_TRUE(nt::DeleteRegKey(key_handle)); | |
202 nt::CloseRegKey(key_handle); | |
203 | |
204 // More tests for CreateRegKey recursion. | |
205 ASSERT_TRUE( | |
206 nt::CreateRegKey(nt::HKCU, sz_new_key_1, KEY_ALL_ACCESS, nullptr)); | |
207 EXPECT_TRUE(nt::OpenRegKey(nt::HKCU, sz_new_key_1, KEY_ALL_ACCESS, | |
208 &key_handle, nullptr)); | |
209 EXPECT_TRUE(nt::DeleteRegKey(key_handle)); | |
210 nt::CloseRegKey(key_handle); | |
211 | |
212 ASSERT_TRUE( | |
213 nt::CreateRegKey(nt::HKCU, sz_new_key_2, KEY_ALL_ACCESS, nullptr)); | |
214 EXPECT_TRUE(nt::OpenRegKey(nt::HKCU, sz_new_key_2, KEY_ALL_ACCESS, | |
215 &key_handle, nullptr)); | |
216 EXPECT_TRUE(nt::DeleteRegKey(key_handle)); | |
217 nt::CloseRegKey(key_handle); | |
218 | |
219 ASSERT_TRUE( | |
220 nt::CreateRegKey(nt::HKCU, sz_new_key_3, KEY_ALL_ACCESS, nullptr)); | |
221 EXPECT_TRUE(nt::OpenRegKey(nt::HKCU, L"test\\new\\subkey\\blah2", | |
222 KEY_ALL_ACCESS, &key_handle, nullptr)); | |
223 EXPECT_TRUE(nt::DeleteRegKey(key_handle)); | |
224 nt::CloseRegKey(key_handle); | |
225 | |
226 ASSERT_TRUE(nt::CreateRegKey(nt::HKCU, nullptr, KEY_ALL_ACCESS, &key_handle)); | |
227 nt::CloseRegKey(key_handle); | |
228 } | 132 } |
229 | 133 |
230 // Parameterized test with paramters: | 134 // Parameterized test with paramters: |
231 // 1: product: "canary" or "google" | 135 // 1: product: "canary" or "google" |
232 // 2: install level: "user" or "system" | 136 // 2: install level: "user" or "system" |
233 // 3: install mode: "single" or "multi" | 137 // 3: install mode: "single" or "multi" |
234 class ChromeElfUtilTest | 138 class ChromeElfUtilTest |
235 : public testing::TestWithParam< | 139 : public testing::TestWithParam< |
236 std::tuple<const char*, const char*, const char*>> { | 140 std::tuple<const char*, const char*, const char*>> { |
237 protected: | 141 protected: |
238 void SetUp() override { | 142 void SetUp() override { |
239 // Set up registry override for these tests. | 143 // Set up registry override for these tests. |
240 RegRedirect(nt::HKLM, override_manager_); | 144 RegRedirect(nt::HKLM, &override_manager_); |
241 RegRedirect(nt::HKCU, override_manager_); | 145 RegRedirect(nt::HKCU, &override_manager_); |
242 | 146 |
243 const char* app; | 147 const char* app; |
244 const char* level; | 148 const char* level; |
245 const char* mode; | 149 const char* mode; |
246 std::tie(app, level, mode) = GetParam(); | 150 std::tie(app, level, mode) = GetParam(); |
247 is_canary_ = (std::string(app) == "canary"); | 151 is_canary_ = (std::string(app) == "canary"); |
248 system_level_ = (std::string(level) != "user"); | 152 system_level_ = (std::string(level) != "user"); |
249 multi_install_ = (std::string(mode) != "single"); | 153 multi_install_ = (std::string(mode) != "single"); |
250 if (is_canary_) { | 154 if (is_canary_) { |
251 ASSERT_FALSE(system_level_); | 155 ASSERT_FALSE(system_level_); |
252 ASSERT_FALSE(multi_install_); | 156 ASSERT_FALSE(multi_install_); |
253 app_guid_ = kAppGuidCanary; | 157 app_guid_ = kAppGuidCanary; |
254 chrome_path_ = kCanaryExePath; | 158 chrome_path_ = kCanaryExePath; |
255 } else { | 159 } else { |
256 app_guid_ = kAppGuidGoogleChrome; | 160 app_guid_ = kAppGuidGoogleChrome; |
257 chrome_path_ = | 161 chrome_path_ = |
258 (system_level_ ? kChromeSystemExePath : kChromeUserExePath); | 162 (system_level_ ? kChromeSystemExePath : kChromeUserExePath); |
259 } | 163 } |
260 if (multi_install_) { | 164 if (multi_install_) { |
261 SetMultiInstallStateInRegistry(system_level_, true); | 165 SetMultiInstallStateInRegistry(system_level_, true); |
262 app_guid_ = kAppGuidGoogleBinaries; | 166 app_guid_ = kAppGuidGoogleBinaries; |
263 } | 167 } |
264 } | 168 } |
265 | 169 |
| 170 void TearDown() override { |
| 171 CancelRegRedirect(nt::HKCU); |
| 172 CancelRegRedirect(nt::HKLM); |
| 173 } |
| 174 |
266 base::string16 BuildKey(const wchar_t* path, const wchar_t* guid) { | 175 base::string16 BuildKey(const wchar_t* path, const wchar_t* guid) { |
267 base::string16 full_key_path(path); | 176 base::string16 full_key_path(path); |
268 full_key_path.append(1, L'\\'); | 177 full_key_path.append(1, L'\\'); |
269 full_key_path.append(guid); | 178 full_key_path.append(guid); |
270 return full_key_path; | 179 return full_key_path; |
271 } | 180 } |
272 | 181 |
273 void SetUsageStat(DWORD value, bool state_medium) { | 182 void SetUsageStat(DWORD value, bool state_medium) { |
274 LONG result = base::win::RegKey( | 183 LONG result = base::win::RegKey( |
275 system_level_ ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER, | 184 system_level_ ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER, |
(...skipping 310 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
586 testing::Combine(testing::Values("canary"), | 495 testing::Combine(testing::Values("canary"), |
587 testing::Values("user"), | 496 testing::Values("user"), |
588 testing::Values("single"))); | 497 testing::Values("single"))); |
589 INSTANTIATE_TEST_CASE_P(GoogleChrome, | 498 INSTANTIATE_TEST_CASE_P(GoogleChrome, |
590 ChromeElfUtilTest, | 499 ChromeElfUtilTest, |
591 testing::Combine(testing::Values("google"), | 500 testing::Combine(testing::Values("google"), |
592 testing::Values("user", "system"), | 501 testing::Values("user", "system"), |
593 testing::Values("single", "multi"))); | 502 testing::Values("single", "multi"))); |
594 | 503 |
595 } // namespace | 504 } // namespace |
OLD | NEW |