| 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 <userenv.h> | 8 #include <userenv.h> |
| 9 | 9 |
| 10 #include <algorithm> | 10 #include <algorithm> |
| (...skipping 19 matching lines...) Expand all Loading... |
| 30 #include "base/win/registry.h" | 30 #include "base/win/registry.h" |
| 31 #include "components/policy/core/common/async_policy_provider.h" | 31 #include "components/policy/core/common/async_policy_provider.h" |
| 32 #include "components/policy/core/common/configuration_policy_provider_test.h" | 32 #include "components/policy/core/common/configuration_policy_provider_test.h" |
| 33 #include "components/policy/core/common/external_data_fetcher.h" | 33 #include "components/policy/core/common/external_data_fetcher.h" |
| 34 #include "components/policy/core/common/policy_bundle.h" | 34 #include "components/policy/core/common/policy_bundle.h" |
| 35 #include "components/policy/core/common/policy_map.h" | 35 #include "components/policy/core/common/policy_map.h" |
| 36 #include "components/policy/core/common/preg_parser_win.h" | 36 #include "components/policy/core/common/preg_parser_win.h" |
| 37 #include "components/policy/core/common/schema_map.h" | 37 #include "components/policy/core/common/schema_map.h" |
| 38 #include "testing/gtest/include/gtest/gtest.h" | 38 #include "testing/gtest/include/gtest/gtest.h" |
| 39 | 39 |
| 40 using base::UTF8ToUTF16; |
| 40 using base::win::RegKey; | 41 using base::win::RegKey; |
| 41 | 42 |
| 42 namespace policy { | 43 namespace policy { |
| 43 | 44 |
| 44 namespace { | 45 namespace { |
| 45 | 46 |
| 46 // Constants for registry key names. | 47 // Constants for registry key names. |
| 47 const wchar_t kPathSep[] = L"\\"; | 48 const wchar_t kPathSep[] = L"\\"; |
| 48 const wchar_t kThirdParty[] = L"3rdparty"; | 49 const wchar_t kThirdParty[] = L"3rdparty"; |
| 49 const wchar_t kMandatory[] = L"policy"; | 50 const wchar_t kMandatory[] = L"policy"; |
| (...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 286 base::FilePath preg_file_path_; | 287 base::FilePath preg_file_path_; |
| 287 GROUP_POLICY_OBJECT gpo_; | 288 GROUP_POLICY_OBJECT gpo_; |
| 288 | 289 |
| 289 DISALLOW_COPY_AND_ASSIGN(PRegTestHarness); | 290 DISALLOW_COPY_AND_ASSIGN(PRegTestHarness); |
| 290 }; | 291 }; |
| 291 | 292 |
| 292 ScopedGroupPolicyRegistrySandbox::ScopedGroupPolicyRegistrySandbox() { | 293 ScopedGroupPolicyRegistrySandbox::ScopedGroupPolicyRegistrySandbox() { |
| 293 // Generate a unique registry key for the override for each test. This | 294 // Generate a unique registry key for the override for each test. This |
| 294 // makes sure that tests executing in parallel won't delete each other's | 295 // makes sure that tests executing in parallel won't delete each other's |
| 295 // key, at DeleteKeys(). | 296 // key, at DeleteKeys(). |
| 296 key_name_ = ASCIIToWide(base::StringPrintf( | 297 key_name_ = base::ASCIIToWide(base::StringPrintf( |
| 297 "SOFTWARE\\chromium unittest %d", | 298 "SOFTWARE\\chromium unittest %d", |
| 298 base::Process::Current().pid())); | 299 base::Process::Current().pid())); |
| 299 std::wstring hklm_key_name = key_name_ + L"\\HKLM"; | 300 std::wstring hklm_key_name = key_name_ + L"\\HKLM"; |
| 300 std::wstring hkcu_key_name = key_name_ + L"\\HKCU"; | 301 std::wstring hkcu_key_name = key_name_ + L"\\HKCU"; |
| 301 | 302 |
| 302 // Create the subkeys to hold the overridden HKLM and HKCU | 303 // Create the subkeys to hold the overridden HKLM and HKCU |
| 303 // policy settings. | 304 // policy settings. |
| 304 temp_hklm_hive_key_.Create(HKEY_CURRENT_USER, | 305 temp_hklm_hive_key_.Create(HKEY_CURRENT_USER, |
| 305 hklm_key_name.c_str(), | 306 hklm_key_name.c_str(), |
| 306 KEY_ALL_ACCESS); | 307 KEY_ALL_ACCESS); |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 375 RegKey key(hive_, kTestPolicyKey, KEY_ALL_ACCESS); | 376 RegKey key(hive_, kTestPolicyKey, KEY_ALL_ACCESS); |
| 376 ASSERT_TRUE(key.Valid()); | 377 ASSERT_TRUE(key.Valid()); |
| 377 key.WriteValue(UTF8ToUTF16(policy_name).c_str(), | 378 key.WriteValue(UTF8ToUTF16(policy_name).c_str(), |
| 378 static_cast<DWORD>(policy_value)); | 379 static_cast<DWORD>(policy_value)); |
| 379 } | 380 } |
| 380 | 381 |
| 381 void RegistryTestHarness::InstallStringListPolicy( | 382 void RegistryTestHarness::InstallStringListPolicy( |
| 382 const std::string& policy_name, | 383 const std::string& policy_name, |
| 383 const base::ListValue* policy_value) { | 384 const base::ListValue* policy_value) { |
| 384 RegKey key(hive_, | 385 RegKey key(hive_, |
| 385 (base::string16(kTestPolicyKey) + ASCIIToUTF16("\\") + | 386 (base::string16(kTestPolicyKey) + base::ASCIIToUTF16("\\") + |
| 386 UTF8ToUTF16(policy_name)).c_str(), | 387 UTF8ToUTF16(policy_name)).c_str(), |
| 387 KEY_ALL_ACCESS); | 388 KEY_ALL_ACCESS); |
| 388 ASSERT_TRUE(key.Valid()); | 389 ASSERT_TRUE(key.Valid()); |
| 389 int index = 1; | 390 int index = 1; |
| 390 for (base::ListValue::const_iterator element(policy_value->begin()); | 391 for (base::ListValue::const_iterator element(policy_value->begin()); |
| 391 element != policy_value->end(); | 392 element != policy_value->end(); |
| 392 ++element) { | 393 ++element) { |
| 393 std::string element_value; | 394 std::string element_value; |
| 394 if (!(*element)->GetAsString(&element_value)) | 395 if (!(*element)->GetAsString(&element_value)) |
| 395 continue; | 396 continue; |
| (...skipping 417 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 813 " \"type\": \"object\"," | 814 " \"type\": \"object\"," |
| 814 " \"properties\": {" | 815 " \"properties\": {" |
| 815 " \"a\": { \"type\": \"string\" }," | 816 " \"a\": { \"type\": \"string\" }," |
| 816 " \"b\": { \"type\": \"string\" }," | 817 " \"b\": { \"type\": \"string\" }," |
| 817 " \"c\": { \"type\": \"string\" }," | 818 " \"c\": { \"type\": \"string\" }," |
| 818 " \"d\": { \"type\": \"string\" }" | 819 " \"d\": { \"type\": \"string\" }" |
| 819 " }" | 820 " }" |
| 820 "}")); | 821 "}")); |
| 821 | 822 |
| 822 const base::string16 kPathSuffix = | 823 const base::string16 kPathSuffix = |
| 823 kTestPolicyKey + ASCIIToUTF16("\\3rdparty\\extensions\\merge"); | 824 kTestPolicyKey + base::ASCIIToUTF16("\\3rdparty\\extensions\\merge"); |
| 824 | 825 |
| 825 const char kUserMandatory[] = "user-mandatory"; | 826 const char kUserMandatory[] = "user-mandatory"; |
| 826 const char kUserRecommended[] = "user-recommended"; | 827 const char kUserRecommended[] = "user-recommended"; |
| 827 const char kMachineMandatory[] = "machine-mandatory"; | 828 const char kMachineMandatory[] = "machine-mandatory"; |
| 828 const char kMachineRecommended[] = "machine-recommended"; | 829 const char kMachineRecommended[] = "machine-recommended"; |
| 829 | 830 |
| 830 base::DictionaryValue policy; | 831 base::DictionaryValue policy; |
| 831 policy.SetString("a", kMachineMandatory); | 832 policy.SetString("a", kMachineMandatory); |
| 832 EXPECT_TRUE(InstallValue(policy, HKEY_LOCAL_MACHINE, | 833 EXPECT_TRUE(InstallValue(policy, HKEY_LOCAL_MACHINE, |
| 833 kPathSuffix, kMandatory)); | 834 kPathSuffix, kMandatory)); |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 902 ASSERT_FALSE(encoded_list.empty()); | 903 ASSERT_FALSE(encoded_list.empty()); |
| 903 base::DictionaryValue encoded_policy; | 904 base::DictionaryValue encoded_policy; |
| 904 encoded_policy.SetString("null", ""); | 905 encoded_policy.SetString("null", ""); |
| 905 encoded_policy.SetString("bool", "1"); | 906 encoded_policy.SetString("bool", "1"); |
| 906 encoded_policy.SetString("int", "-123"); | 907 encoded_policy.SetString("int", "-123"); |
| 907 encoded_policy.SetString("double", "456.78e9"); | 908 encoded_policy.SetString("double", "456.78e9"); |
| 908 encoded_policy.SetString("list", encoded_list); | 909 encoded_policy.SetString("list", encoded_list); |
| 909 encoded_policy.SetString("dict", encoded_dict); | 910 encoded_policy.SetString("dict", encoded_dict); |
| 910 | 911 |
| 911 const base::string16 kPathSuffix = | 912 const base::string16 kPathSuffix = |
| 912 kTestPolicyKey + ASCIIToUTF16("\\3rdparty\\extensions\\string"); | 913 kTestPolicyKey + base::ASCIIToUTF16("\\3rdparty\\extensions\\string"); |
| 913 EXPECT_TRUE( | 914 EXPECT_TRUE( |
| 914 InstallValue(encoded_policy, HKEY_CURRENT_USER, kPathSuffix, kMandatory)); | 915 InstallValue(encoded_policy, HKEY_CURRENT_USER, kPathSuffix, kMandatory)); |
| 915 | 916 |
| 916 PolicyBundle expected; | 917 PolicyBundle expected; |
| 917 expected.Get(ns).LoadFrom(&policy, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER); | 918 expected.Get(ns).LoadFrom(&policy, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER); |
| 918 EXPECT_TRUE(Matches(expected)); | 919 EXPECT_TRUE(Matches(expected)); |
| 919 } | 920 } |
| 920 | 921 |
| 921 TEST_F(PolicyLoaderWinTest, LoadIntegerEncodedValues) { | 922 TEST_F(PolicyLoaderWinTest, LoadIntegerEncodedValues) { |
| 922 const PolicyNamespace ns(POLICY_DOMAIN_EXTENSIONS, "int"); | 923 const PolicyNamespace ns(POLICY_DOMAIN_EXTENSIONS, "int"); |
| 923 ASSERT_TRUE(RegisterSchema( | 924 ASSERT_TRUE(RegisterSchema( |
| 924 ns, | 925 ns, |
| 925 "{" | 926 "{" |
| 926 " \"type\": \"object\"," | 927 " \"type\": \"object\"," |
| 927 " \"properties\": {" | 928 " \"properties\": {" |
| 928 " \"bool\": { \"type\": \"boolean\" }," | 929 " \"bool\": { \"type\": \"boolean\" }," |
| 929 " \"int\": { \"type\": \"integer\" }," | 930 " \"int\": { \"type\": \"integer\" }," |
| 930 " \"double\": { \"type\": \"number\" }" | 931 " \"double\": { \"type\": \"number\" }" |
| 931 " }" | 932 " }" |
| 932 "}")); | 933 "}")); |
| 933 | 934 |
| 934 base::DictionaryValue encoded_policy; | 935 base::DictionaryValue encoded_policy; |
| 935 encoded_policy.SetInteger("bool", 1); | 936 encoded_policy.SetInteger("bool", 1); |
| 936 encoded_policy.SetInteger("int", 123); | 937 encoded_policy.SetInteger("int", 123); |
| 937 encoded_policy.SetInteger("double", 456); | 938 encoded_policy.SetInteger("double", 456); |
| 938 | 939 |
| 939 const base::string16 kPathSuffix = | 940 const base::string16 kPathSuffix = |
| 940 kTestPolicyKey + ASCIIToUTF16("\\3rdparty\\extensions\\int"); | 941 kTestPolicyKey + base::ASCIIToUTF16("\\3rdparty\\extensions\\int"); |
| 941 EXPECT_TRUE( | 942 EXPECT_TRUE( |
| 942 InstallValue(encoded_policy, HKEY_CURRENT_USER, kPathSuffix, kMandatory)); | 943 InstallValue(encoded_policy, HKEY_CURRENT_USER, kPathSuffix, kMandatory)); |
| 943 | 944 |
| 944 base::DictionaryValue policy; | 945 base::DictionaryValue policy; |
| 945 policy.SetBoolean("bool", true); | 946 policy.SetBoolean("bool", true); |
| 946 policy.SetInteger("int", 123); | 947 policy.SetInteger("int", 123); |
| 947 policy.SetDouble("double", 456.0); | 948 policy.SetDouble("double", 456.0); |
| 948 PolicyBundle expected; | 949 PolicyBundle expected; |
| 949 expected.Get(ns).LoadFrom(&policy, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER); | 950 expected.Get(ns).LoadFrom(&policy, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER); |
| 950 EXPECT_TRUE(Matches(expected)); | 951 EXPECT_TRUE(Matches(expected)); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 977 policy.SetInteger("special-int1", 123); | 978 policy.SetInteger("special-int1", 123); |
| 978 policy.SetString("special-int2", "-456"); | 979 policy.SetString("special-int2", "-456"); |
| 979 // Other values default to be loaded as doubles. | 980 // Other values default to be loaded as doubles. |
| 980 policy.SetInteger("double1", 789.0); | 981 policy.SetInteger("double1", 789.0); |
| 981 policy.SetString("double2", "123.456e7"); | 982 policy.SetString("double2", "123.456e7"); |
| 982 policy.SetString("invalid", "omg"); | 983 policy.SetString("invalid", "omg"); |
| 983 base::DictionaryValue all_policies; | 984 base::DictionaryValue all_policies; |
| 984 all_policies.Set("policy", policy.DeepCopy()); | 985 all_policies.Set("policy", policy.DeepCopy()); |
| 985 | 986 |
| 986 const base::string16 kPathSuffix = | 987 const base::string16 kPathSuffix = |
| 987 kTestPolicyKey + ASCIIToUTF16("\\3rdparty\\extensions\\test"); | 988 kTestPolicyKey + base::ASCIIToUTF16("\\3rdparty\\extensions\\test"); |
| 988 EXPECT_TRUE( | 989 EXPECT_TRUE( |
| 989 InstallValue(all_policies, HKEY_CURRENT_USER, kPathSuffix, kMandatory)); | 990 InstallValue(all_policies, HKEY_CURRENT_USER, kPathSuffix, kMandatory)); |
| 990 | 991 |
| 991 base::DictionaryValue expected_policy; | 992 base::DictionaryValue expected_policy; |
| 992 expected_policy.SetInteger("special-int1", 123); | 993 expected_policy.SetInteger("special-int1", 123); |
| 993 expected_policy.SetInteger("special-int2", -456); | 994 expected_policy.SetInteger("special-int2", -456); |
| 994 expected_policy.SetDouble("double1", 789.0); | 995 expected_policy.SetDouble("double1", 789.0); |
| 995 expected_policy.SetDouble("double2", 123.456e7); | 996 expected_policy.SetDouble("double2", 123.456e7); |
| 996 base::DictionaryValue expected_policies; | 997 base::DictionaryValue expected_policies; |
| 997 expected_policies.Set("policy", expected_policy.DeepCopy()); | 998 expected_policies.Set("policy", expected_policy.DeepCopy()); |
| (...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1154 const char kIncompleteSchema[] = | 1155 const char kIncompleteSchema[] = |
| 1155 "{" | 1156 "{" |
| 1156 " \"type\": \"object\"," | 1157 " \"type\": \"object\"," |
| 1157 " \"properties\": {" | 1158 " \"properties\": {" |
| 1158 " \"url_list\": { \"type\": \"array\" }," | 1159 " \"url_list\": { \"type\": \"array\" }," |
| 1159 " \"url_greylist\": { \"type\": \"array\" }" | 1160 " \"url_greylist\": { \"type\": \"array\" }" |
| 1160 " }" | 1161 " }" |
| 1161 "}"; | 1162 "}"; |
| 1162 | 1163 |
| 1163 const base::string16 kPathSuffix = | 1164 const base::string16 kPathSuffix = |
| 1164 kTestPolicyKey + ASCIIToUTF16("\\3rdparty\\extensions"); | 1165 kTestPolicyKey + base::ASCIIToUTF16("\\3rdparty\\extensions"); |
| 1165 | 1166 |
| 1166 base::ListValue list; | 1167 base::ListValue list; |
| 1167 list.AppendString("youtube.com"); | 1168 list.AppendString("youtube.com"); |
| 1168 base::DictionaryValue policy; | 1169 base::DictionaryValue policy; |
| 1169 policy.Set("url_list", list.DeepCopy()); | 1170 policy.Set("url_list", list.DeepCopy()); |
| 1170 policy.SetString("alternative_browser_path", "c:\\legacy\\browser.exe"); | 1171 policy.SetString("alternative_browser_path", "c:\\legacy\\browser.exe"); |
| 1171 base::DictionaryValue root; | 1172 base::DictionaryValue root; |
| 1172 root.Set(UTF16ToUTF8(kMandatory), policy.DeepCopy()); | 1173 root.Set(base::UTF16ToUTF8(kMandatory), policy.DeepCopy()); |
| 1173 root.SetString(kSchema, kIncompleteSchema); | 1174 root.SetString(kSchema, kIncompleteSchema); |
| 1174 EXPECT_TRUE(InstallValue(root, HKEY_LOCAL_MACHINE, | 1175 EXPECT_TRUE(InstallValue(root, HKEY_LOCAL_MACHINE, |
| 1175 kPathSuffix, ASCIIToUTF16(ns.component_id))); | 1176 kPathSuffix, base::ASCIIToUTF16(ns.component_id))); |
| 1176 | 1177 |
| 1177 PolicyBundle expected; | 1178 PolicyBundle expected; |
| 1178 PolicyMap& expected_policy = expected.Get(ns); | 1179 PolicyMap& expected_policy = expected.Get(ns); |
| 1179 expected_policy.Set("alternative_browser_path", | 1180 expected_policy.Set("alternative_browser_path", |
| 1180 POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE, | 1181 POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE, |
| 1181 new base::StringValue("c:\\legacy\\browser.exe"), NULL); | 1182 new base::StringValue("c:\\legacy\\browser.exe"), NULL); |
| 1182 expected_policy.Set("url_list", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE, | 1183 expected_policy.Set("url_list", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE, |
| 1183 list.DeepCopy(), NULL); | 1184 list.DeepCopy(), NULL); |
| 1184 EXPECT_TRUE(Matches(expected)); | 1185 EXPECT_TRUE(Matches(expected)); |
| 1185 } | 1186 } |
| 1186 | 1187 |
| 1187 } // namespace policy | 1188 } // namespace policy |
| OLD | NEW |