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() { |
pastarmovj
2017/05/19 11:14:17
move constructor code over and
auto result1 =
pmarko
2017/05/19 11:27:27
Done.
| |
299 // Generate a unique registry key for the override for each test. This | 303 // 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 | 304 // makes sure that tests executing in parallel won't delete each other's |
301 // key, at DeleteKeys(). | 305 // key, at DeleteKeys(). |
302 key_name_ = base::ASCIIToUTF16(base::StringPrintf( | 306 key_name_ = base::ASCIIToUTF16(base::StringPrintf( |
303 "SOFTWARE\\chromium unittest %d", base::GetCurrentProcId())); | 307 "SOFTWARE\\chromium unittest %d", base::GetCurrentProcId())); |
304 std::wstring hklm_key_name = key_name_ + L"\\HKLM"; | 308 std::wstring hklm_key_name = key_name_ + L"\\HKLM"; |
305 std::wstring hkcu_key_name = key_name_ + L"\\HKCU"; | 309 std::wstring hkcu_key_name = key_name_ + L"\\HKCU"; |
306 | 310 |
311 // Delete the registry test key if it already exists (this could happen if | |
312 // the process id got recycled and the last test running under the same | |
313 // process id crashed and could not clean up its test registry keys). | |
314 RegKey parent_key; | |
315 if (parent_key.Open(HKEY_CURRENT_USER, key_name_, KEY_ALL_ACESS) == | |
pastarmovj
2017/05/19 11:14:17
c_Str, ACCESS
pmarko
2017/05/19 11:27:27
Done. (removed, replaced by DeleteKeys() for simpl
| |
316 ERROR_SUCCESS) { | |
317 parent_key.DeleteKey(L""); | |
318 parent_key.Close(); | |
319 } | |
320 | |
307 // Create the subkeys to hold the overridden HKLM and HKCU | 321 // Create the subkeys to hold the overridden HKLM and HKCU |
308 // policy settings. | 322 // policy settings. |
309 temp_hklm_hive_key_.Create(HKEY_CURRENT_USER, | 323 temp_hklm_hive_key_.Create(HKEY_CURRENT_USER, |
310 hklm_key_name.c_str(), | 324 hklm_key_name.c_str(), |
311 KEY_ALL_ACCESS); | 325 KEY_ALL_ACCESS); |
312 temp_hkcu_hive_key_.Create(HKEY_CURRENT_USER, | 326 temp_hkcu_hive_key_.Create(HKEY_CURRENT_USER, |
313 hkcu_key_name.c_str(), | 327 hkcu_key_name.c_str(), |
314 KEY_ALL_ACCESS); | 328 KEY_ALL_ACCESS); |
315 | 329 |
316 ActivateOverrides(); | 330 ActivateOverrides(); |
pastarmovj
2017/05/19 11:14:17
Remove from here.
pmarko
2017/05/19 11:27:27
Done.
| |
317 } | 331 } |
318 | 332 |
319 ScopedGroupPolicyRegistrySandbox::~ScopedGroupPolicyRegistrySandbox() { | 333 ScopedGroupPolicyRegistrySandbox::~ScopedGroupPolicyRegistrySandbox() { |
320 RemoveOverrides(); | 334 RemoveOverrides(); |
321 DeleteKeys(); | 335 DeleteKeys(); |
322 } | 336 } |
323 | 337 |
324 void ScopedGroupPolicyRegistrySandbox::ActivateOverrides() { | 338 void ScopedGroupPolicyRegistrySandbox::ActivateOverrides() { |
325 ASSERT_HRESULT_SUCCEEDED(RegOverridePredefKey(HKEY_LOCAL_MACHINE, | 339 ASSERT_HRESULT_SUCCEEDED(RegOverridePredefKey(HKEY_LOCAL_MACHINE, |
326 temp_hklm_hive_key_.Handle())); | 340 temp_hklm_hive_key_.Handle())); |
(...skipping 13 matching lines...) Expand all Loading... | |
340 } | 354 } |
341 | 355 |
342 RegistryTestHarness::RegistryTestHarness(HKEY hive, PolicyScope scope) | 356 RegistryTestHarness::RegistryTestHarness(HKEY hive, PolicyScope scope) |
343 : PolicyProviderTestHarness(POLICY_LEVEL_MANDATORY, scope, | 357 : PolicyProviderTestHarness(POLICY_LEVEL_MANDATORY, scope, |
344 POLICY_SOURCE_PLATFORM), | 358 POLICY_SOURCE_PLATFORM), |
345 hive_(hive) { | 359 hive_(hive) { |
346 } | 360 } |
347 | 361 |
348 RegistryTestHarness::~RegistryTestHarness() {} | 362 RegistryTestHarness::~RegistryTestHarness() {} |
349 | 363 |
350 void RegistryTestHarness::SetUp() {} | 364 void RegistryTestHarness::SetUp() { |
365 // SetUp is called at gtest SetUp time, and gtest documentation guarantees | |
366 // that the test will not be executed if SetUp has a fatal failure. This is | |
367 // important, see crbug.com/721691. | |
368 ASSERT_NO_FATAL_FAILURE(registry_sandbox_.ActivateOverrides()); | |
369 } | |
351 | 370 |
352 ConfigurationPolicyProvider* RegistryTestHarness::CreateProvider( | 371 ConfigurationPolicyProvider* RegistryTestHarness::CreateProvider( |
353 SchemaRegistry* registry, | 372 SchemaRegistry* registry, |
354 scoped_refptr<base::SequencedTaskRunner> task_runner) { | 373 scoped_refptr<base::SequencedTaskRunner> task_runner) { |
355 base::win::SetDomainStateForTesting(true); | 374 base::win::SetDomainStateForTesting(true); |
356 std::unique_ptr<AsyncPolicyLoader> loader( | 375 std::unique_ptr<AsyncPolicyLoader> loader( |
357 new PolicyLoaderWin(task_runner, kTestPolicyKey, this)); | 376 new PolicyLoaderWin(task_runner, kTestPolicyKey, this)); |
358 return new AsyncPolicyProvider(registry, std::move(loader)); | 377 return new AsyncPolicyProvider(registry, std::move(loader)); |
359 } | 378 } |
360 | 379 |
(...skipping 346 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
707 | 726 |
708 PolicyLoaderWinTest() | 727 PolicyLoaderWinTest() |
709 : gpo_list_(NULL), | 728 : gpo_list_(NULL), |
710 gpo_list_status_(ERROR_ACCESS_DENIED) {} | 729 gpo_list_status_(ERROR_ACCESS_DENIED) {} |
711 ~PolicyLoaderWinTest() override {} | 730 ~PolicyLoaderWinTest() override {} |
712 | 731 |
713 void SetUp() override { | 732 void SetUp() override { |
714 base::win::SetDomainStateForTesting(false); | 733 base::win::SetDomainStateForTesting(false); |
715 PolicyTestBase::SetUp(); | 734 PolicyTestBase::SetUp(); |
716 | 735 |
736 // Activate overrides of registry keys. gtest documentation guarantees | |
737 // that the test will not be executed if SetUp has a fatal failure. This is | |
738 // important, see crbug.com/721691. | |
739 ASSERT_NO_FATAL_FAILURE(registry_sandbox_.ActivateOverrides()); | |
740 | |
717 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &test_data_dir_)); | 741 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &test_data_dir_)); |
718 test_data_dir_ = test_data_dir_.AppendASCII("chrome") | 742 test_data_dir_ = test_data_dir_.AppendASCII("chrome") |
719 .AppendASCII("test") | 743 .AppendASCII("test") |
720 .AppendASCII("data") | 744 .AppendASCII("data") |
721 .AppendASCII("policy") | 745 .AppendASCII("policy") |
722 .AppendASCII("gpo"); | 746 .AppendASCII("gpo"); |
723 | 747 |
724 gpo_list_provider_ = this; | 748 gpo_list_provider_ = this; |
725 } | 749 } |
726 | 750 |
(...skipping 461 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1188 expected.Get(ns_a).LoadFrom(&expected_a, POLICY_LEVEL_MANDATORY, | 1212 expected.Get(ns_a).LoadFrom(&expected_a, POLICY_LEVEL_MANDATORY, |
1189 POLICY_SCOPE_MACHINE, POLICY_SOURCE_PLATFORM); | 1213 POLICY_SCOPE_MACHINE, POLICY_SOURCE_PLATFORM); |
1190 base::DictionaryValue expected_b; | 1214 base::DictionaryValue expected_b; |
1191 expected_b.SetInteger("policy 1", 2); | 1215 expected_b.SetInteger("policy 1", 2); |
1192 expected.Get(ns_b).LoadFrom(&expected_b, POLICY_LEVEL_MANDATORY, | 1216 expected.Get(ns_b).LoadFrom(&expected_b, POLICY_LEVEL_MANDATORY, |
1193 POLICY_SCOPE_MACHINE, POLICY_SOURCE_PLATFORM); | 1217 POLICY_SCOPE_MACHINE, POLICY_SOURCE_PLATFORM); |
1194 EXPECT_TRUE(Matches(expected)); | 1218 EXPECT_TRUE(Matches(expected)); |
1195 } | 1219 } |
1196 | 1220 |
1197 } // namespace policy | 1221 } // namespace policy |
OLD | NEW |