| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include <gtest/gtest.h> | |
| 6 #include <windows.h> | |
| 7 | |
| 8 #include "base/json/json_writer.h" | |
| 9 #include "base/string16.h" | |
| 10 #include "base/string_number_conversions.h" | |
| 11 #include "base/utf_string_conversions.h" | |
| 12 #include "base/win/registry.h" | |
| 13 #include "chrome/browser/policy/asynchronous_policy_test_base.h" | |
| 14 #include "chrome/browser/policy/configuration_policy_provider_test.h" | |
| 15 #include "chrome/browser/policy/configuration_policy_provider_win.h" | |
| 16 #include "chrome/browser/policy/policy_bundle.h" | |
| 17 #include "chrome/browser/policy/policy_map.h" | |
| 18 #include "policy/policy_constants.h" | |
| 19 #include "testing/gtest/include/gtest/gtest.h" | |
| 20 | |
| 21 using base::win::RegKey; | |
| 22 | |
| 23 namespace policy { | |
| 24 | |
| 25 namespace { | |
| 26 | |
| 27 const wchar_t kUnitTestRegistrySubKey[] = L"SOFTWARE\\Chromium Unit Tests"; | |
| 28 const wchar_t kUnitTestMachineOverrideSubKey[] = | |
| 29 L"SOFTWARE\\Chromium Unit Tests\\HKLM Override"; | |
| 30 const wchar_t kUnitTestUserOverrideSubKey[] = | |
| 31 L"SOFTWARE\\Chromium Unit Tests\\HKCU Override"; | |
| 32 | |
| 33 // This class provides sandboxing and mocking for the parts of the Windows | |
| 34 // Registry implementing Group Policy. It prepares two temporary sandbox keys | |
| 35 // in |kUnitTestRegistrySubKey|, one for HKLM and one for HKCU. A test's calls | |
| 36 // to the registry are redirected by Windows to these sandboxes, allowing the | |
| 37 // tests to manipulate and access policy as if it were active, but without | |
| 38 // actually changing the parts of the Registry that are managed by Group | |
| 39 // Policy. | |
| 40 class ScopedGroupPolicyRegistrySandbox { | |
| 41 public: | |
| 42 ScopedGroupPolicyRegistrySandbox(); | |
| 43 ~ScopedGroupPolicyRegistrySandbox(); | |
| 44 | |
| 45 private: | |
| 46 void ActivateOverrides(); | |
| 47 void RemoveOverrides(); | |
| 48 | |
| 49 // Deletes the sandbox keys. | |
| 50 void DeleteKeys(); | |
| 51 | |
| 52 // Keys are created for the lifetime of a test to contain | |
| 53 // the sandboxed HKCU and HKLM hives, respectively. | |
| 54 RegKey temp_hkcu_hive_key_; | |
| 55 RegKey temp_hklm_hive_key_; | |
| 56 | |
| 57 DISALLOW_COPY_AND_ASSIGN(ScopedGroupPolicyRegistrySandbox); | |
| 58 }; | |
| 59 | |
| 60 class TestHarness : public PolicyProviderTestHarness { | |
| 61 public: | |
| 62 explicit TestHarness(HKEY hive, PolicyScope scope); | |
| 63 virtual ~TestHarness(); | |
| 64 | |
| 65 virtual void SetUp() OVERRIDE; | |
| 66 | |
| 67 virtual ConfigurationPolicyProvider* CreateProvider( | |
| 68 const PolicyDefinitionList* policy_definition_list) OVERRIDE; | |
| 69 | |
| 70 virtual void InstallEmptyPolicy() OVERRIDE; | |
| 71 virtual void InstallStringPolicy(const std::string& policy_name, | |
| 72 const std::string& policy_value) OVERRIDE; | |
| 73 virtual void InstallIntegerPolicy(const std::string& policy_name, | |
| 74 int policy_value) OVERRIDE; | |
| 75 virtual void InstallBooleanPolicy(const std::string& policy_name, | |
| 76 bool policy_value) OVERRIDE; | |
| 77 virtual void InstallStringListPolicy( | |
| 78 const std::string& policy_name, | |
| 79 const base::ListValue* policy_value) OVERRIDE; | |
| 80 virtual void InstallDictionaryPolicy( | |
| 81 const std::string& policy_name, | |
| 82 const base::DictionaryValue* policy_value) OVERRIDE; | |
| 83 | |
| 84 // Creates a harness instance that will install policy in HKCU or HKLM, | |
| 85 // respectively. | |
| 86 static PolicyProviderTestHarness* CreateHKCU(); | |
| 87 static PolicyProviderTestHarness* CreateHKLM(); | |
| 88 | |
| 89 private: | |
| 90 HKEY hive_; | |
| 91 | |
| 92 ScopedGroupPolicyRegistrySandbox registry_sandbox_; | |
| 93 | |
| 94 DISALLOW_COPY_AND_ASSIGN(TestHarness); | |
| 95 }; | |
| 96 | |
| 97 ScopedGroupPolicyRegistrySandbox::ScopedGroupPolicyRegistrySandbox() { | |
| 98 // Cleanup any remnants of previous tests. | |
| 99 DeleteKeys(); | |
| 100 | |
| 101 // Create the subkeys to hold the overridden HKLM and HKCU | |
| 102 // policy settings. | |
| 103 temp_hklm_hive_key_.Create(HKEY_CURRENT_USER, | |
| 104 kUnitTestMachineOverrideSubKey, | |
| 105 KEY_ALL_ACCESS); | |
| 106 temp_hkcu_hive_key_.Create(HKEY_CURRENT_USER, | |
| 107 kUnitTestUserOverrideSubKey, | |
| 108 KEY_ALL_ACCESS); | |
| 109 | |
| 110 ActivateOverrides(); | |
| 111 } | |
| 112 | |
| 113 ScopedGroupPolicyRegistrySandbox::~ScopedGroupPolicyRegistrySandbox() { | |
| 114 RemoveOverrides(); | |
| 115 DeleteKeys(); | |
| 116 } | |
| 117 | |
| 118 void ScopedGroupPolicyRegistrySandbox::ActivateOverrides() { | |
| 119 ASSERT_HRESULT_SUCCEEDED(RegOverridePredefKey(HKEY_LOCAL_MACHINE, | |
| 120 temp_hklm_hive_key_.Handle())); | |
| 121 ASSERT_HRESULT_SUCCEEDED(RegOverridePredefKey(HKEY_CURRENT_USER, | |
| 122 temp_hkcu_hive_key_.Handle())); | |
| 123 } | |
| 124 | |
| 125 void ScopedGroupPolicyRegistrySandbox::RemoveOverrides() { | |
| 126 ASSERT_HRESULT_SUCCEEDED(RegOverridePredefKey(HKEY_LOCAL_MACHINE, 0)); | |
| 127 ASSERT_HRESULT_SUCCEEDED(RegOverridePredefKey(HKEY_CURRENT_USER, 0)); | |
| 128 } | |
| 129 | |
| 130 void ScopedGroupPolicyRegistrySandbox::DeleteKeys() { | |
| 131 RegKey key(HKEY_CURRENT_USER, kUnitTestRegistrySubKey, KEY_ALL_ACCESS); | |
| 132 key.DeleteKey(L""); | |
| 133 } | |
| 134 | |
| 135 TestHarness::TestHarness(HKEY hive, PolicyScope scope) | |
| 136 : PolicyProviderTestHarness(POLICY_LEVEL_MANDATORY, scope), hive_(hive) {} | |
| 137 | |
| 138 TestHarness::~TestHarness() {} | |
| 139 | |
| 140 void TestHarness::SetUp() {} | |
| 141 | |
| 142 ConfigurationPolicyProvider* TestHarness::CreateProvider( | |
| 143 const PolicyDefinitionList* policy_definition_list) { | |
| 144 return new ConfigurationPolicyProviderWin(policy_definition_list); | |
| 145 } | |
| 146 | |
| 147 void TestHarness::InstallEmptyPolicy() {} | |
| 148 | |
| 149 void TestHarness::InstallStringPolicy(const std::string& policy_name, | |
| 150 const std::string& policy_value) { | |
| 151 RegKey key(hive_, policy::kRegistryMandatorySubKey, KEY_ALL_ACCESS); | |
| 152 key.WriteValue(UTF8ToUTF16(policy_name).c_str(), | |
| 153 UTF8ToUTF16(policy_value).c_str()); | |
| 154 } | |
| 155 | |
| 156 void TestHarness::InstallIntegerPolicy(const std::string& policy_name, | |
| 157 int policy_value) { | |
| 158 RegKey key(hive_, policy::kRegistryMandatorySubKey, KEY_ALL_ACCESS); | |
| 159 key.WriteValue(UTF8ToUTF16(policy_name).c_str(), | |
| 160 static_cast<DWORD>(policy_value)); | |
| 161 } | |
| 162 | |
| 163 void TestHarness::InstallBooleanPolicy(const std::string& policy_name, | |
| 164 bool policy_value) { | |
| 165 RegKey key(hive_, policy::kRegistryMandatorySubKey, KEY_ALL_ACCESS); | |
| 166 key.WriteValue(UTF8ToUTF16(policy_name).c_str(), | |
| 167 static_cast<DWORD>(policy_value)); | |
| 168 } | |
| 169 | |
| 170 void TestHarness::InstallStringListPolicy(const std::string& policy_name, | |
| 171 const base::ListValue* policy_value) { | |
| 172 RegKey key(hive_, | |
| 173 (string16(policy::kRegistryMandatorySubKey) + ASCIIToUTF16("\\") + | |
| 174 UTF8ToUTF16(policy_name)).c_str(), | |
| 175 KEY_ALL_ACCESS); | |
| 176 int index = 1; | |
| 177 for (base::ListValue::const_iterator element(policy_value->begin()); | |
| 178 element != policy_value->end(); | |
| 179 ++element) { | |
| 180 std::string element_value; | |
| 181 if (!(*element)->GetAsString(&element_value)) | |
| 182 continue; | |
| 183 std::string name(base::IntToString(index++)); | |
| 184 key.WriteValue(UTF8ToUTF16(name).c_str(), | |
| 185 UTF8ToUTF16(element_value).c_str()); | |
| 186 } | |
| 187 } | |
| 188 | |
| 189 void TestHarness::InstallDictionaryPolicy( | |
| 190 const std::string& policy_name, | |
| 191 const base::DictionaryValue* policy_value) { | |
| 192 std::string json; | |
| 193 base::JSONWriter::Write(policy_value, &json); | |
| 194 RegKey key(hive_, policy::kRegistryMandatorySubKey, KEY_ALL_ACCESS); | |
| 195 key.WriteValue(UTF8ToUTF16(policy_name).c_str(), | |
| 196 UTF8ToUTF16(json).c_str()); | |
| 197 } | |
| 198 | |
| 199 // static | |
| 200 PolicyProviderTestHarness* TestHarness::CreateHKCU() { | |
| 201 return new TestHarness(HKEY_CURRENT_USER, POLICY_SCOPE_USER); | |
| 202 } | |
| 203 | |
| 204 // static | |
| 205 PolicyProviderTestHarness* TestHarness::CreateHKLM() { | |
| 206 return new TestHarness(HKEY_LOCAL_MACHINE, POLICY_SCOPE_MACHINE); | |
| 207 } | |
| 208 | |
| 209 } // namespace | |
| 210 | |
| 211 // Instantiate abstract test case for basic policy reading tests. | |
| 212 INSTANTIATE_TEST_CASE_P( | |
| 213 ConfigurationPolicyProviderWinTest, | |
| 214 ConfigurationPolicyProviderTest, | |
| 215 testing::Values(TestHarness::CreateHKCU, TestHarness::CreateHKLM)); | |
| 216 | |
| 217 // Test cases for windows policy provider specific functionality. | |
| 218 class ConfigurationPolicyProviderWinTest : public AsynchronousPolicyTestBase { | |
| 219 protected: | |
| 220 ConfigurationPolicyProviderWinTest() | |
| 221 : provider_(&test_policy_definitions::kList) {} | |
| 222 virtual ~ConfigurationPolicyProviderWinTest() {} | |
| 223 | |
| 224 ScopedGroupPolicyRegistrySandbox registry_sandbox_; | |
| 225 ConfigurationPolicyProviderWin provider_; | |
| 226 }; | |
| 227 | |
| 228 TEST_F(ConfigurationPolicyProviderWinTest, HKLMOverHKCU) { | |
| 229 RegKey hklm_key(HKEY_LOCAL_MACHINE, | |
| 230 policy::kRegistryMandatorySubKey, | |
| 231 KEY_ALL_ACCESS); | |
| 232 hklm_key.WriteValue(UTF8ToUTF16(test_policy_definitions::kKeyString).c_str(), | |
| 233 UTF8ToUTF16("hklm").c_str()); | |
| 234 RegKey hkcu_key(HKEY_CURRENT_USER, | |
| 235 policy::kRegistryMandatorySubKey, | |
| 236 KEY_ALL_ACCESS); | |
| 237 hkcu_key.WriteValue(UTF8ToUTF16(test_policy_definitions::kKeyString).c_str(), | |
| 238 UTF8ToUTF16("hkcu").c_str()); | |
| 239 | |
| 240 provider_.RefreshPolicies(); | |
| 241 loop_.RunAllPending(); | |
| 242 | |
| 243 PolicyBundle expected_bundle; | |
| 244 expected_bundle.Get(POLICY_DOMAIN_CHROME, "") | |
| 245 .Set(test_policy_definitions::kKeyString, | |
| 246 POLICY_LEVEL_MANDATORY, | |
| 247 POLICY_SCOPE_MACHINE, | |
| 248 base::Value::CreateStringValue("hklm")); | |
| 249 EXPECT_TRUE(provider_.policies().Equals(expected_bundle)); | |
| 250 } | |
| 251 | |
| 252 } // namespace policy | |
| OLD | NEW |