| 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/browser/policy/policy_loader_win.h" | 5 #include "chrome/browser/policy/policy_loader_win.h" |
| 6 | 6 |
| 7 #include <userenv.h> | 7 #include <userenv.h> |
| 8 #include <windows.h> | 8 #include <windows.h> |
| 9 | 9 |
| 10 #include <algorithm> | 10 #include <algorithm> |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 #include "base/strings/utf_string_conversions.h" | 28 #include "base/strings/utf_string_conversions.h" |
| 29 #include "base/sys_byteorder.h" | 29 #include "base/sys_byteorder.h" |
| 30 #include "base/win/registry.h" | 30 #include "base/win/registry.h" |
| 31 #include "chrome/browser/policy/async_policy_provider.h" | 31 #include "chrome/browser/policy/async_policy_provider.h" |
| 32 #include "chrome/browser/policy/configuration_policy_provider_test.h" | 32 #include "chrome/browser/policy/configuration_policy_provider_test.h" |
| 33 #include "chrome/browser/policy/external_data_fetcher.h" | 33 #include "chrome/browser/policy/external_data_fetcher.h" |
| 34 #include "chrome/browser/policy/policy_bundle.h" | 34 #include "chrome/browser/policy/policy_bundle.h" |
| 35 #include "chrome/browser/policy/policy_map.h" | 35 #include "chrome/browser/policy/policy_map.h" |
| 36 #include "chrome/browser/policy/preg_parser_win.h" | 36 #include "chrome/browser/policy/preg_parser_win.h" |
| 37 #include "components/json_schema/json_schema_constants.h" | 37 #include "components/json_schema/json_schema_constants.h" |
| 38 #include "policy/policy_constants.h" | |
| 39 #include "testing/gtest/include/gtest/gtest.h" | 38 #include "testing/gtest/include/gtest/gtest.h" |
| 40 | 39 |
| 41 namespace schema = json_schema_constants; | 40 namespace schema = json_schema_constants; |
| 42 | 41 |
| 43 using base::win::RegKey; | 42 using base::win::RegKey; |
| 44 | 43 |
| 45 namespace policy { | 44 namespace policy { |
| 46 | 45 |
| 47 namespace { | 46 namespace { |
| 48 | 47 |
| (...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 246 public AppliedGPOListProvider { | 245 public AppliedGPOListProvider { |
| 247 public: | 246 public: |
| 248 RegistryTestHarness(HKEY hive, PolicyScope scope); | 247 RegistryTestHarness(HKEY hive, PolicyScope scope); |
| 249 virtual ~RegistryTestHarness(); | 248 virtual ~RegistryTestHarness(); |
| 250 | 249 |
| 251 // PolicyProviderTestHarness: | 250 // PolicyProviderTestHarness: |
| 252 virtual void SetUp() OVERRIDE; | 251 virtual void SetUp() OVERRIDE; |
| 253 | 252 |
| 254 virtual ConfigurationPolicyProvider* CreateProvider( | 253 virtual ConfigurationPolicyProvider* CreateProvider( |
| 255 SchemaRegistry* registry, | 254 SchemaRegistry* registry, |
| 256 scoped_refptr<base::SequencedTaskRunner> task_runner, | 255 scoped_refptr<base::SequencedTaskRunner> task_runner) OVERRIDE; |
| 257 const PolicyDefinitionList* policy_list) OVERRIDE; | |
| 258 | 256 |
| 259 virtual void InstallEmptyPolicy() OVERRIDE; | 257 virtual void InstallEmptyPolicy() OVERRIDE; |
| 260 virtual void InstallStringPolicy(const std::string& policy_name, | 258 virtual void InstallStringPolicy(const std::string& policy_name, |
| 261 const std::string& policy_value) OVERRIDE; | 259 const std::string& policy_value) OVERRIDE; |
| 262 virtual void InstallIntegerPolicy(const std::string& policy_name, | 260 virtual void InstallIntegerPolicy(const std::string& policy_name, |
| 263 int policy_value) OVERRIDE; | 261 int policy_value) OVERRIDE; |
| 264 virtual void InstallBooleanPolicy(const std::string& policy_name, | 262 virtual void InstallBooleanPolicy(const std::string& policy_name, |
| 265 bool policy_value) OVERRIDE; | 263 bool policy_value) OVERRIDE; |
| 266 virtual void InstallStringListPolicy( | 264 virtual void InstallStringListPolicy( |
| 267 const std::string& policy_name, | 265 const std::string& policy_name, |
| (...skipping 30 matching lines...) Expand all Loading... |
| 298 public AppliedGPOListProvider { | 296 public AppliedGPOListProvider { |
| 299 public: | 297 public: |
| 300 PRegTestHarness(); | 298 PRegTestHarness(); |
| 301 virtual ~PRegTestHarness(); | 299 virtual ~PRegTestHarness(); |
| 302 | 300 |
| 303 // PolicyProviderTestHarness: | 301 // PolicyProviderTestHarness: |
| 304 virtual void SetUp() OVERRIDE; | 302 virtual void SetUp() OVERRIDE; |
| 305 | 303 |
| 306 virtual ConfigurationPolicyProvider* CreateProvider( | 304 virtual ConfigurationPolicyProvider* CreateProvider( |
| 307 SchemaRegistry* registry, | 305 SchemaRegistry* registry, |
| 308 scoped_refptr<base::SequencedTaskRunner> task_runner, | 306 scoped_refptr<base::SequencedTaskRunner> task_runner) OVERRIDE; |
| 309 const PolicyDefinitionList* policy_list) OVERRIDE; | |
| 310 | 307 |
| 311 virtual void InstallEmptyPolicy() OVERRIDE; | 308 virtual void InstallEmptyPolicy() OVERRIDE; |
| 312 virtual void InstallStringPolicy(const std::string& policy_name, | 309 virtual void InstallStringPolicy(const std::string& policy_name, |
| 313 const std::string& policy_value) OVERRIDE; | 310 const std::string& policy_value) OVERRIDE; |
| 314 virtual void InstallIntegerPolicy(const std::string& policy_name, | 311 virtual void InstallIntegerPolicy(const std::string& policy_name, |
| 315 int policy_value) OVERRIDE; | 312 int policy_value) OVERRIDE; |
| 316 virtual void InstallBooleanPolicy(const std::string& policy_name, | 313 virtual void InstallBooleanPolicy(const std::string& policy_name, |
| 317 bool policy_value) OVERRIDE; | 314 bool policy_value) OVERRIDE; |
| 318 virtual void InstallStringListPolicy( | 315 virtual void InstallStringListPolicy( |
| 319 const std::string& policy_name, | 316 const std::string& policy_name, |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 416 | 413 |
| 417 RegistryTestHarness::RegistryTestHarness(HKEY hive, PolicyScope scope) | 414 RegistryTestHarness::RegistryTestHarness(HKEY hive, PolicyScope scope) |
| 418 : PolicyProviderTestHarness(POLICY_LEVEL_MANDATORY, scope), hive_(hive) {} | 415 : PolicyProviderTestHarness(POLICY_LEVEL_MANDATORY, scope), hive_(hive) {} |
| 419 | 416 |
| 420 RegistryTestHarness::~RegistryTestHarness() {} | 417 RegistryTestHarness::~RegistryTestHarness() {} |
| 421 | 418 |
| 422 void RegistryTestHarness::SetUp() {} | 419 void RegistryTestHarness::SetUp() {} |
| 423 | 420 |
| 424 ConfigurationPolicyProvider* RegistryTestHarness::CreateProvider( | 421 ConfigurationPolicyProvider* RegistryTestHarness::CreateProvider( |
| 425 SchemaRegistry* registry, | 422 SchemaRegistry* registry, |
| 426 scoped_refptr<base::SequencedTaskRunner> task_runner, | 423 scoped_refptr<base::SequencedTaskRunner> task_runner) { |
| 427 const PolicyDefinitionList* policy_list) { | 424 scoped_ptr<AsyncPolicyLoader> loader( |
| 428 scoped_ptr<AsyncPolicyLoader> loader(new PolicyLoaderWin( | 425 new PolicyLoaderWin(task_runner, kRegistryChromePolicyKey, this)); |
| 429 task_runner, policy_list, kRegistryChromePolicyKey, this)); | |
| 430 return new AsyncPolicyProvider(registry, loader.Pass()); | 426 return new AsyncPolicyProvider(registry, loader.Pass()); |
| 431 } | 427 } |
| 432 | 428 |
| 433 void RegistryTestHarness::InstallEmptyPolicy() {} | 429 void RegistryTestHarness::InstallEmptyPolicy() {} |
| 434 | 430 |
| 435 void RegistryTestHarness::InstallStringPolicy( | 431 void RegistryTestHarness::InstallStringPolicy( |
| 436 const std::string& policy_name, | 432 const std::string& policy_name, |
| 437 const std::string& policy_value) { | 433 const std::string& policy_value) { |
| 438 RegKey key(hive_, kRegistryChromePolicyKey, KEY_ALL_ACCESS); | 434 RegKey key(hive_, kRegistryChromePolicyKey, KEY_ALL_ACCESS); |
| 439 ASSERT_TRUE(key.Valid()); | 435 ASSERT_TRUE(key.Valid()); |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 549 ASSERT_TRUE(file_util::WriteFile(preg_file_path_, | 545 ASSERT_TRUE(file_util::WriteFile(preg_file_path_, |
| 550 preg_parser::kPRegFileHeader, | 546 preg_parser::kPRegFileHeader, |
| 551 arraysize(preg_parser::kPRegFileHeader))); | 547 arraysize(preg_parser::kPRegFileHeader))); |
| 552 | 548 |
| 553 memset(&gpo_, 0, sizeof(GROUP_POLICY_OBJECT)); | 549 memset(&gpo_, 0, sizeof(GROUP_POLICY_OBJECT)); |
| 554 gpo_.lpFileSysPath = const_cast<wchar_t*>(temp_dir_.path().value().c_str()); | 550 gpo_.lpFileSysPath = const_cast<wchar_t*>(temp_dir_.path().value().c_str()); |
| 555 } | 551 } |
| 556 | 552 |
| 557 ConfigurationPolicyProvider* PRegTestHarness::CreateProvider( | 553 ConfigurationPolicyProvider* PRegTestHarness::CreateProvider( |
| 558 SchemaRegistry* registry, | 554 SchemaRegistry* registry, |
| 559 scoped_refptr<base::SequencedTaskRunner> task_runner, | 555 scoped_refptr<base::SequencedTaskRunner> task_runner) { |
| 560 const PolicyDefinitionList* policy_list) { | 556 scoped_ptr<AsyncPolicyLoader> loader( |
| 561 scoped_ptr<AsyncPolicyLoader> loader(new PolicyLoaderWin( | 557 new PolicyLoaderWin(task_runner, kRegistryChromePolicyKey, this)); |
| 562 task_runner, policy_list, kRegistryChromePolicyKey, this)); | |
| 563 return new AsyncPolicyProvider(registry, loader.Pass()); | 558 return new AsyncPolicyProvider(registry, loader.Pass()); |
| 564 } | 559 } |
| 565 | 560 |
| 566 void PRegTestHarness::InstallEmptyPolicy() {} | 561 void PRegTestHarness::InstallEmptyPolicy() {} |
| 567 | 562 |
| 568 void PRegTestHarness::InstallStringPolicy(const std::string& policy_name, | 563 void PRegTestHarness::InstallStringPolicy(const std::string& policy_name, |
| 569 const std::string& policy_value) { | 564 const std::string& policy_value) { |
| 570 AppendStringToPRegFile(kRegistryChromePolicyKey, policy_name, policy_value); | 565 AppendStringToPRegFile(kRegistryChromePolicyKey, policy_name, policy_value); |
| 571 } | 566 } |
| 572 | 567 |
| (...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 822 GROUP_POLICY_OBJECT* next, | 817 GROUP_POLICY_OBJECT* next, |
| 823 GROUP_POLICY_OBJECT* prev) { | 818 GROUP_POLICY_OBJECT* prev) { |
| 824 memset(gpo, 0, sizeof(GROUP_POLICY_OBJECT)); | 819 memset(gpo, 0, sizeof(GROUP_POLICY_OBJECT)); |
| 825 gpo->dwOptions = options; | 820 gpo->dwOptions = options; |
| 826 gpo->lpFileSysPath = const_cast<wchar_t*>(path.value().c_str()); | 821 gpo->lpFileSysPath = const_cast<wchar_t*>(path.value().c_str()); |
| 827 gpo->pNext = next; | 822 gpo->pNext = next; |
| 828 gpo->pPrev = prev; | 823 gpo->pPrev = prev; |
| 829 } | 824 } |
| 830 | 825 |
| 831 bool Matches(const PolicyBundle& expected) { | 826 bool Matches(const PolicyBundle& expected) { |
| 832 PolicyLoaderWin loader(loop_.message_loop_proxy(), | 827 PolicyLoaderWin loader(loop_.message_loop_proxy(), kTestPolicyKey, this); |
| 833 &test_policy_definitions::kList, kTestPolicyKey, | |
| 834 this); | |
| 835 scoped_ptr<PolicyBundle> loaded( | 828 scoped_ptr<PolicyBundle> loaded( |
| 836 loader.InitialLoad(schema_registry_.schema_map())); | 829 loader.InitialLoad(schema_registry_.schema_map())); |
| 837 return loaded->Equals(expected); | 830 return loaded->Equals(expected); |
| 838 } | 831 } |
| 839 | 832 |
| 840 void InstallRegistrySentinel() { | 833 void InstallRegistrySentinel() { |
| 841 RegKey hklm_key(HKEY_CURRENT_USER, kTestPolicyKey, KEY_ALL_ACCESS); | 834 RegKey hklm_key(HKEY_CURRENT_USER, kTestPolicyKey, KEY_ALL_ACCESS); |
| 842 ASSERT_TRUE(hklm_key.Valid()); | 835 ASSERT_TRUE(hklm_key.Valid()); |
| 843 hklm_key.WriteValue( | 836 hklm_key.WriteValue( |
| 844 UTF8ToUTF16(test_policy_definitions::kKeyString).c_str(), | 837 UTF8ToUTF16(test_keys::kKeyString).c_str(), |
| 845 UTF8ToUTF16("registry").c_str()); | 838 UTF8ToUTF16("registry").c_str()); |
| 846 } | 839 } |
| 847 | 840 |
| 848 bool MatchesRegistrySentinel() { | 841 bool MatchesRegistrySentinel() { |
| 849 base::DictionaryValue expected_policy; | 842 base::DictionaryValue expected_policy; |
| 850 expected_policy.SetString(test_policy_definitions::kKeyString, "registry"); | 843 expected_policy.SetString(test_keys::kKeyString, "registry"); |
| 851 PolicyBundle expected; | 844 PolicyBundle expected; |
| 852 expected.Get(PolicyNamespace(POLICY_DOMAIN_CHROME, std::string())) | 845 expected.Get(PolicyNamespace(POLICY_DOMAIN_CHROME, std::string())) |
| 853 .LoadFrom(&expected_policy, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER); | 846 .LoadFrom(&expected_policy, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER); |
| 854 return Matches(expected); | 847 return Matches(expected); |
| 855 } | 848 } |
| 856 | 849 |
| 857 bool MatchesTestBundle() { | 850 bool MatchesTestBundle() { |
| 858 base::DictionaryValue expected_policy; | 851 base::DictionaryValue expected_policy; |
| 859 expected_policy.SetBoolean(test_policy_definitions::kKeyBoolean, true); | 852 expected_policy.SetBoolean(test_keys::kKeyBoolean, true); |
| 860 expected_policy.SetString(test_policy_definitions::kKeyString, "GPO"); | 853 expected_policy.SetString(test_keys::kKeyString, "GPO"); |
| 861 expected_policy.SetInteger(test_policy_definitions::kKeyInteger, 42); | 854 expected_policy.SetInteger(test_keys::kKeyInteger, 42); |
| 862 scoped_ptr<base::ListValue> list(new base::ListValue()); | 855 scoped_ptr<base::ListValue> list(new base::ListValue()); |
| 863 list->AppendString("GPO 1"); | 856 list->AppendString("GPO 1"); |
| 864 list->AppendString("GPO 2"); | 857 list->AppendString("GPO 2"); |
| 865 expected_policy.Set(test_policy_definitions::kKeyStringList, | 858 expected_policy.Set(test_keys::kKeyStringList, list.release()); |
| 866 list.release()); | |
| 867 PolicyBundle expected; | 859 PolicyBundle expected; |
| 868 expected.Get(PolicyNamespace(POLICY_DOMAIN_CHROME, std::string())) | 860 expected.Get(PolicyNamespace(POLICY_DOMAIN_CHROME, std::string())) |
| 869 .LoadFrom(&expected_policy, POLICY_LEVEL_MANDATORY, | 861 .LoadFrom(&expected_policy, POLICY_LEVEL_MANDATORY, |
| 870 POLICY_SCOPE_MACHINE); | 862 POLICY_SCOPE_MACHINE); |
| 871 return Matches(expected); | 863 return Matches(expected); |
| 872 } | 864 } |
| 873 | 865 |
| 874 ScopedGroupPolicyRegistrySandbox registry_sandbox_; | 866 ScopedGroupPolicyRegistrySandbox registry_sandbox_; |
| 875 PGROUP_POLICY_OBJECT gpo_list_; | 867 PGROUP_POLICY_OBJECT gpo_list_; |
| 876 DWORD gpo_list_status_; | 868 DWORD gpo_list_status_; |
| 877 base::FilePath test_data_dir_; | 869 base::FilePath test_data_dir_; |
| 878 }; | 870 }; |
| 879 | 871 |
| 880 const char16 PolicyLoaderWinTest::kTestPolicyKey[] = | 872 const char16 PolicyLoaderWinTest::kTestPolicyKey[] = |
| 881 L"SOFTWARE\\Policies\\Chromium"; | 873 L"SOFTWARE\\Policies\\Chromium"; |
| 882 | 874 |
| 883 TEST_F(PolicyLoaderWinTest, HKLMOverHKCU) { | 875 TEST_F(PolicyLoaderWinTest, HKLMOverHKCU) { |
| 884 RegKey hklm_key(HKEY_LOCAL_MACHINE, kTestPolicyKey, KEY_ALL_ACCESS); | 876 RegKey hklm_key(HKEY_LOCAL_MACHINE, kTestPolicyKey, KEY_ALL_ACCESS); |
| 885 ASSERT_TRUE(hklm_key.Valid()); | 877 ASSERT_TRUE(hklm_key.Valid()); |
| 886 hklm_key.WriteValue(UTF8ToUTF16(test_policy_definitions::kKeyString).c_str(), | 878 hklm_key.WriteValue(UTF8ToUTF16(test_keys::kKeyString).c_str(), |
| 887 UTF8ToUTF16("hklm").c_str()); | 879 UTF8ToUTF16("hklm").c_str()); |
| 888 RegKey hkcu_key(HKEY_CURRENT_USER, kTestPolicyKey, KEY_ALL_ACCESS); | 880 RegKey hkcu_key(HKEY_CURRENT_USER, kTestPolicyKey, KEY_ALL_ACCESS); |
| 889 ASSERT_TRUE(hkcu_key.Valid()); | 881 ASSERT_TRUE(hkcu_key.Valid()); |
| 890 hkcu_key.WriteValue(UTF8ToUTF16(test_policy_definitions::kKeyString).c_str(), | 882 hkcu_key.WriteValue(UTF8ToUTF16(test_keys::kKeyString).c_str(), |
| 891 UTF8ToUTF16("hkcu").c_str()); | 883 UTF8ToUTF16("hkcu").c_str()); |
| 892 | 884 |
| 893 PolicyBundle expected; | 885 PolicyBundle expected; |
| 894 expected.Get(PolicyNamespace(POLICY_DOMAIN_CHROME, std::string())) | 886 expected.Get(PolicyNamespace(POLICY_DOMAIN_CHROME, std::string())) |
| 895 .Set(test_policy_definitions::kKeyString, | 887 .Set(test_keys::kKeyString, |
| 896 POLICY_LEVEL_MANDATORY, | 888 POLICY_LEVEL_MANDATORY, |
| 897 POLICY_SCOPE_MACHINE, | 889 POLICY_SCOPE_MACHINE, |
| 898 base::Value::CreateStringValue("hklm"), NULL); | 890 base::Value::CreateStringValue("hklm"), NULL); |
| 899 EXPECT_TRUE(Matches(expected)); | 891 EXPECT_TRUE(Matches(expected)); |
| 900 } | 892 } |
| 901 | 893 |
| 902 TEST_F(PolicyLoaderWinTest, Load3rdPartyWithoutSchema) { | 894 TEST_F(PolicyLoaderWinTest, Load3rdPartyWithoutSchema) { |
| 903 base::DictionaryValue dict; | 895 base::DictionaryValue dict; |
| 904 dict.SetString("str", "string value"); | 896 dict.SetString("str", "string value"); |
| 905 dict.SetInteger("int", 123); | 897 dict.SetInteger("int", 123); |
| (...skipping 298 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1204 .LoadFrom(&expected_a, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE); | 1196 .LoadFrom(&expected_a, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE); |
| 1205 base::DictionaryValue expected_b; | 1197 base::DictionaryValue expected_b; |
| 1206 expected_b.SetInteger("policy 1", 2); | 1198 expected_b.SetInteger("policy 1", 2); |
| 1207 expected.Get(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, | 1199 expected.Get(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, |
| 1208 "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb")) | 1200 "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb")) |
| 1209 .LoadFrom(&expected_b, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE); | 1201 .LoadFrom(&expected_b, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE); |
| 1210 EXPECT_TRUE(Matches(expected)); | 1202 EXPECT_TRUE(Matches(expected)); |
| 1211 } | 1203 } |
| 1212 | 1204 |
| 1213 } // namespace policy | 1205 } // namespace policy |
| OLD | NEW |