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 "components/policy/core/common/policy_loader_win.h" | 5 #include "components/policy/core/common/policy_loader_win.h" |
6 | 6 |
7 #include <windows.h> | 7 #include <windows.h> |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 #include <stdint.h> | 9 #include <stdint.h> |
10 #include <userenv.h> | 10 #include <userenv.h> |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
148 // Registry implementing Group Policy. It prepares two temporary sandbox keys, | 148 // Registry implementing Group Policy. It prepares two temporary sandbox keys, |
149 // one for HKLM and one for HKCU. A test's calls to the registry are redirected | 149 // one for HKLM and one for HKCU. A test's calls to the registry are redirected |
150 // by Windows to these sandboxes, allowing the tests to manipulate and access | 150 // by Windows to these sandboxes, allowing the tests to manipulate and access |
151 // policy as if it were active, but without actually changing the parts of the | 151 // policy as if it were active, but without actually changing the parts of the |
152 // Registry that are managed by Group Policy. | 152 // Registry that are managed by Group Policy. |
153 class ScopedGroupPolicyRegistrySandbox { | 153 class ScopedGroupPolicyRegistrySandbox { |
154 public: | 154 public: |
155 ScopedGroupPolicyRegistrySandbox(); | 155 ScopedGroupPolicyRegistrySandbox(); |
156 ~ScopedGroupPolicyRegistrySandbox(); | 156 ~ScopedGroupPolicyRegistrySandbox(); |
157 | 157 |
| 158 // Activates the registry keys overrides. This must be called before doing any |
| 159 // writes to registry and the call should be wrapped in |
| 160 // ASSERT_NO_FATAL_FAILURE. |
| 161 void ActivateOverrides(); |
| 162 |
158 private: | 163 private: |
159 void ActivateOverrides(); | |
160 void RemoveOverrides(); | 164 void RemoveOverrides(); |
161 | 165 |
162 // Deletes the sandbox keys. | 166 // Deletes the sandbox keys. |
163 void DeleteKeys(); | 167 void DeleteKeys(); |
164 | 168 |
165 base::string16 key_name_; | 169 base::string16 key_name_; |
166 | 170 |
167 // Keys are created for the lifetime of a test to contain | 171 // Keys are created for the lifetime of a test to contain |
168 // the sandboxed HKCU and HKLM hives, respectively. | 172 // the sandboxed HKCU and HKLM hives, respectively. |
169 RegKey temp_hkcu_hive_key_; | 173 RegKey temp_hkcu_hive_key_; |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
288 const std::string& key, | 292 const std::string& key, |
289 const base::Value* value); | 293 const base::Value* value); |
290 | 294 |
291 base::ScopedTempDir temp_dir_; | 295 base::ScopedTempDir temp_dir_; |
292 base::FilePath preg_file_path_; | 296 base::FilePath preg_file_path_; |
293 GROUP_POLICY_OBJECT gpo_; | 297 GROUP_POLICY_OBJECT gpo_; |
294 | 298 |
295 DISALLOW_COPY_AND_ASSIGN(PRegTestHarness); | 299 DISALLOW_COPY_AND_ASSIGN(PRegTestHarness); |
296 }; | 300 }; |
297 | 301 |
298 ScopedGroupPolicyRegistrySandbox::ScopedGroupPolicyRegistrySandbox() { | 302 ScopedGroupPolicyRegistrySandbox::ScopedGroupPolicyRegistrySandbox() {} |
| 303 |
| 304 ScopedGroupPolicyRegistrySandbox::~ScopedGroupPolicyRegistrySandbox() { |
| 305 RemoveOverrides(); |
| 306 DeleteKeys(); |
| 307 } |
| 308 |
| 309 void ScopedGroupPolicyRegistrySandbox::ActivateOverrides() { |
299 // Generate a unique registry key for the override for each test. This | 310 // Generate a unique registry key for the override for each test. This |
300 // makes sure that tests executing in parallel won't delete each other's | 311 // makes sure that tests executing in parallel won't delete each other's |
301 // key, at DeleteKeys(). | 312 // key, at DeleteKeys(). |
302 key_name_ = base::ASCIIToUTF16(base::StringPrintf( | 313 key_name_ = base::ASCIIToUTF16(base::StringPrintf( |
303 "SOFTWARE\\chromium unittest %d", base::GetCurrentProcId())); | 314 "SOFTWARE\\chromium unittest %d", base::GetCurrentProcId())); |
304 std::wstring hklm_key_name = key_name_ + L"\\HKLM"; | 315 std::wstring hklm_key_name = key_name_ + L"\\HKLM"; |
305 std::wstring hkcu_key_name = key_name_ + L"\\HKCU"; | 316 std::wstring hkcu_key_name = key_name_ + L"\\HKCU"; |
306 | 317 |
| 318 // Delete the registry test keys if they already exist (this could happen if |
| 319 // the process id got recycled and the last test running under the same |
| 320 // process id crashed ). |
| 321 DeleteKeys(); |
| 322 |
307 // Create the subkeys to hold the overridden HKLM and HKCU | 323 // Create the subkeys to hold the overridden HKLM and HKCU |
308 // policy settings. | 324 // policy settings. |
309 temp_hklm_hive_key_.Create(HKEY_CURRENT_USER, | 325 temp_hklm_hive_key_.Create(HKEY_CURRENT_USER, |
310 hklm_key_name.c_str(), | 326 hklm_key_name.c_str(), |
311 KEY_ALL_ACCESS); | 327 KEY_ALL_ACCESS); |
312 temp_hkcu_hive_key_.Create(HKEY_CURRENT_USER, | 328 temp_hkcu_hive_key_.Create(HKEY_CURRENT_USER, |
313 hkcu_key_name.c_str(), | 329 hkcu_key_name.c_str(), |
314 KEY_ALL_ACCESS); | 330 KEY_ALL_ACCESS); |
315 | 331 |
316 ActivateOverrides(); | 332 auto result_override_hklm = |
317 } | 333 RegOverridePredefKey(HKEY_LOCAL_MACHINE, temp_hklm_hive_key_.Handle()); |
| 334 auto result_override_hkcu = |
| 335 RegOverridePredefKey(HKEY_CURRENT_USER, temp_hkcu_hive_key_.Handle()); |
318 | 336 |
319 ScopedGroupPolicyRegistrySandbox::~ScopedGroupPolicyRegistrySandbox() { | 337 if (result_override_hklm != ERROR_SUCCESS || |
320 RemoveOverrides(); | 338 result_override_hkcu != ERROR_SUCCESS) { |
321 DeleteKeys(); | 339 // We need to remove the overrides first in case one succeeded and one |
322 } | 340 // failed, otherwise deleting the keys fails. |
| 341 RemoveOverrides(); |
| 342 DeleteKeys(); |
323 | 343 |
324 void ScopedGroupPolicyRegistrySandbox::ActivateOverrides() { | 344 // Assert on the actual results to print the error code in failure case. |
325 ASSERT_HRESULT_SUCCEEDED(RegOverridePredefKey(HKEY_LOCAL_MACHINE, | 345 ASSERT_HRESULT_SUCCEEDED(result_override_hklm); |
326 temp_hklm_hive_key_.Handle())); | 346 ASSERT_HRESULT_SUCCEEDED(result_override_hkcu); |
327 ASSERT_HRESULT_SUCCEEDED(RegOverridePredefKey(HKEY_CURRENT_USER, | 347 } |
328 temp_hkcu_hive_key_.Handle())); | |
329 } | 348 } |
330 | 349 |
331 void ScopedGroupPolicyRegistrySandbox::RemoveOverrides() { | 350 void ScopedGroupPolicyRegistrySandbox::RemoveOverrides() { |
332 ASSERT_HRESULT_SUCCEEDED(RegOverridePredefKey(HKEY_LOCAL_MACHINE, 0)); | 351 ASSERT_HRESULT_SUCCEEDED(RegOverridePredefKey(HKEY_LOCAL_MACHINE, 0)); |
333 ASSERT_HRESULT_SUCCEEDED(RegOverridePredefKey(HKEY_CURRENT_USER, 0)); | 352 ASSERT_HRESULT_SUCCEEDED(RegOverridePredefKey(HKEY_CURRENT_USER, 0)); |
334 } | 353 } |
335 | 354 |
336 void ScopedGroupPolicyRegistrySandbox::DeleteKeys() { | 355 void ScopedGroupPolicyRegistrySandbox::DeleteKeys() { |
337 RegKey key(HKEY_CURRENT_USER, key_name_.c_str(), KEY_ALL_ACCESS); | 356 RegKey key(HKEY_CURRENT_USER, key_name_.c_str(), KEY_ALL_ACCESS); |
338 ASSERT_TRUE(key.Valid()); | 357 ASSERT_TRUE(key.Valid()); |
339 key.DeleteKey(L""); | 358 key.DeleteKey(L""); |
340 } | 359 } |
341 | 360 |
342 RegistryTestHarness::RegistryTestHarness(HKEY hive, PolicyScope scope) | 361 RegistryTestHarness::RegistryTestHarness(HKEY hive, PolicyScope scope) |
343 : PolicyProviderTestHarness(POLICY_LEVEL_MANDATORY, scope, | 362 : PolicyProviderTestHarness(POLICY_LEVEL_MANDATORY, scope, |
344 POLICY_SOURCE_PLATFORM), | 363 POLICY_SOURCE_PLATFORM), |
345 hive_(hive) { | 364 hive_(hive) { |
346 } | 365 } |
347 | 366 |
348 RegistryTestHarness::~RegistryTestHarness() {} | 367 RegistryTestHarness::~RegistryTestHarness() {} |
349 | 368 |
350 void RegistryTestHarness::SetUp() {} | 369 void RegistryTestHarness::SetUp() { |
| 370 // SetUp is called at gtest SetUp time, and gtest documentation guarantees |
| 371 // that the test will not be executed if SetUp has a fatal failure. This is |
| 372 // important, see crbug.com/721691. |
| 373 ASSERT_NO_FATAL_FAILURE(registry_sandbox_.ActivateOverrides()); |
| 374 } |
351 | 375 |
352 ConfigurationPolicyProvider* RegistryTestHarness::CreateProvider( | 376 ConfigurationPolicyProvider* RegistryTestHarness::CreateProvider( |
353 SchemaRegistry* registry, | 377 SchemaRegistry* registry, |
354 scoped_refptr<base::SequencedTaskRunner> task_runner) { | 378 scoped_refptr<base::SequencedTaskRunner> task_runner) { |
355 base::win::SetDomainStateForTesting(true); | 379 base::win::SetDomainStateForTesting(true); |
356 std::unique_ptr<AsyncPolicyLoader> loader( | 380 std::unique_ptr<AsyncPolicyLoader> loader( |
357 new PolicyLoaderWin(task_runner, kTestPolicyKey, this)); | 381 new PolicyLoaderWin(task_runner, kTestPolicyKey, this)); |
358 return new AsyncPolicyProvider(registry, std::move(loader)); | 382 return new AsyncPolicyProvider(registry, std::move(loader)); |
359 } | 383 } |
360 | 384 |
(...skipping 346 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
707 | 731 |
708 PolicyLoaderWinTest() | 732 PolicyLoaderWinTest() |
709 : gpo_list_(NULL), | 733 : gpo_list_(NULL), |
710 gpo_list_status_(ERROR_ACCESS_DENIED) {} | 734 gpo_list_status_(ERROR_ACCESS_DENIED) {} |
711 ~PolicyLoaderWinTest() override {} | 735 ~PolicyLoaderWinTest() override {} |
712 | 736 |
713 void SetUp() override { | 737 void SetUp() override { |
714 base::win::SetDomainStateForTesting(false); | 738 base::win::SetDomainStateForTesting(false); |
715 PolicyTestBase::SetUp(); | 739 PolicyTestBase::SetUp(); |
716 | 740 |
| 741 // Activate overrides of registry keys. gtest documentation guarantees |
| 742 // that the test will not be executed if SetUp has a fatal failure. This is |
| 743 // important, see crbug.com/721691. |
| 744 ASSERT_NO_FATAL_FAILURE(registry_sandbox_.ActivateOverrides()); |
| 745 |
717 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &test_data_dir_)); | 746 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &test_data_dir_)); |
718 test_data_dir_ = test_data_dir_.AppendASCII("chrome") | 747 test_data_dir_ = test_data_dir_.AppendASCII("chrome") |
719 .AppendASCII("test") | 748 .AppendASCII("test") |
720 .AppendASCII("data") | 749 .AppendASCII("data") |
721 .AppendASCII("policy") | 750 .AppendASCII("policy") |
722 .AppendASCII("gpo"); | 751 .AppendASCII("gpo"); |
723 | 752 |
724 gpo_list_provider_ = this; | 753 gpo_list_provider_ = this; |
725 } | 754 } |
726 | 755 |
(...skipping 461 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1188 expected.Get(ns_a).LoadFrom(&expected_a, POLICY_LEVEL_MANDATORY, | 1217 expected.Get(ns_a).LoadFrom(&expected_a, POLICY_LEVEL_MANDATORY, |
1189 POLICY_SCOPE_MACHINE, POLICY_SOURCE_PLATFORM); | 1218 POLICY_SCOPE_MACHINE, POLICY_SOURCE_PLATFORM); |
1190 base::DictionaryValue expected_b; | 1219 base::DictionaryValue expected_b; |
1191 expected_b.SetInteger("policy 1", 2); | 1220 expected_b.SetInteger("policy 1", 2); |
1192 expected.Get(ns_b).LoadFrom(&expected_b, POLICY_LEVEL_MANDATORY, | 1221 expected.Get(ns_b).LoadFrom(&expected_b, POLICY_LEVEL_MANDATORY, |
1193 POLICY_SCOPE_MACHINE, POLICY_SOURCE_PLATFORM); | 1222 POLICY_SCOPE_MACHINE, POLICY_SOURCE_PLATFORM); |
1194 EXPECT_TRUE(Matches(expected)); | 1223 EXPECT_TRUE(Matches(expected)); |
1195 } | 1224 } |
1196 | 1225 |
1197 } // namespace policy | 1226 } // namespace policy |
OLD | NEW |