Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 <gtest/gtest.h> | 5 #include <gtest/gtest.h> |
| 6 #include <windows.h> | 6 #include <windows.h> |
| 7 | 7 |
| 8 #include "base/memory/scoped_ptr.h" | 8 #include "base/string16.h" |
| 9 #include "base/message_loop.h" | |
| 10 #include "base/stl_util.h" | |
| 11 #include "base/string_number_conversions.h" | 9 #include "base/string_number_conversions.h" |
| 12 #include "base/string_piece.h" | |
| 13 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
| 14 #include "base/win/registry.h" | 11 #include "base/win/registry.h" |
| 15 #include "chrome/browser/policy/asynchronous_policy_loader.h" | 12 #include "chrome/browser/policy/asynchronous_policy_test_base.h" |
| 16 #include "chrome/browser/policy/configuration_policy_pref_store.h" | 13 #include "chrome/browser/policy/configuration_policy_provider_test.h" |
| 17 #include "chrome/browser/policy/configuration_policy_provider_win.h" | 14 #include "chrome/browser/policy/configuration_policy_provider_win.h" |
| 18 #include "chrome/browser/policy/policy_map.h" | 15 #include "chrome/browser/policy/policy_map.h" |
| 19 #include "chrome/common/pref_names.h" | |
| 20 #include "content/test/test_browser_thread.h" | |
| 21 #include "policy/policy_constants.h" | 16 #include "policy/policy_constants.h" |
| 22 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 23 | 18 |
| 24 using base::win::RegKey; | 19 using base::win::RegKey; |
| 25 using content::BrowserThread; | |
| 26 | 20 |
| 27 namespace policy { | 21 namespace policy { |
| 28 | 22 |
| 29 namespace { | 23 namespace { |
| 30 | 24 |
| 31 const wchar_t kUnitTestRegistrySubKey[] = L"SOFTWARE\\Chromium Unit Tests"; | 25 const wchar_t kUnitTestRegistrySubKey[] = L"SOFTWARE\\Chromium Unit Tests"; |
| 32 const wchar_t kUnitTestMachineOverrideSubKey[] = | 26 const wchar_t kUnitTestMachineOverrideSubKey[] = |
| 33 L"SOFTWARE\\Chromium Unit Tests\\HKLM Override"; | 27 L"SOFTWARE\\Chromium Unit Tests\\HKLM Override"; |
| 34 const wchar_t kUnitTestUserOverrideSubKey[] = | 28 const wchar_t kUnitTestUserOverrideSubKey[] = |
| 35 L"SOFTWARE\\Chromium Unit Tests\\HKCU Override"; | 29 L"SOFTWARE\\Chromium Unit Tests\\HKCU Override"; |
| 36 | 30 |
| 37 // Holds policy type, corresponding policy name string and a valid value for use | 31 // This class provides sandboxing and mocking for the parts of the Windows |
| 38 // in parametrized value tests. | 32 // Registry implementing Group Policy. It prepares two temporary sandbox keys |
| 39 class PolicyTestParams { | 33 // in |kUnitTestRegistrySubKey|, one for HKLM and one for HKCU. A test's calls |
| 34 // to the registry are redirected by Windows to these sandboxes, allowing the | |
| 35 // tests to manipulate and access policy as if it were active, but without | |
| 36 // actually changing the parts of the Registry that are managed by Group | |
| 37 // Policy. | |
| 38 class ScopedGroupPolicyRegistrySandbox { | |
| 40 public: | 39 public: |
| 41 // Assumes ownership of |hklm_value| and |hkcu_value|. | 40 ScopedGroupPolicyRegistrySandbox(); |
| 42 PolicyTestParams(ConfigurationPolicyType type, | 41 ~ScopedGroupPolicyRegistrySandbox(); |
| 43 const char* policy_name, | |
| 44 Value* hklm_value, | |
| 45 Value* hkcu_value) | |
| 46 : type_(type), | |
| 47 policy_name_(policy_name), | |
| 48 hklm_value_(hklm_value), | |
| 49 hkcu_value_(hkcu_value) {} | |
| 50 | |
| 51 // testing::TestWithParam does copy the parameters, so provide copy | |
| 52 // constructor and assignment operator. | |
| 53 PolicyTestParams(const PolicyTestParams& other) | |
| 54 : type_(other.type_), | |
| 55 policy_name_(other.policy_name_), | |
| 56 hklm_value_(other.hklm_value_->DeepCopy()), | |
| 57 hkcu_value_(other.hkcu_value_->DeepCopy()) {} | |
| 58 | |
| 59 const PolicyTestParams& operator=(PolicyTestParams other) { | |
| 60 swap(other); | |
| 61 return *this; | |
| 62 } | |
| 63 | |
| 64 void swap(PolicyTestParams& other) { | |
| 65 std::swap(type_, other.type_); | |
| 66 std::swap(policy_name_, other.policy_name_); | |
| 67 hklm_value_.swap(other.hklm_value_); | |
| 68 hkcu_value_.swap(other.hkcu_value_); | |
| 69 } | |
| 70 | |
| 71 ConfigurationPolicyType type() const { return type_; } | |
| 72 const char* policy_name() const { return policy_name_; } | |
| 73 const Value* hklm_value() const { return hklm_value_.get(); } | |
| 74 const Value* hkcu_value() const { return hkcu_value_.get(); } | |
| 75 | |
| 76 // Factory methods for different value types. | |
| 77 static PolicyTestParams ForStringPolicy( | |
| 78 ConfigurationPolicyType type, | |
| 79 const char* policy_name) { | |
| 80 return PolicyTestParams(type, | |
| 81 policy_name, | |
| 82 Value::CreateStringValue("string_a"), | |
| 83 Value::CreateStringValue("string_b")); | |
| 84 } | |
| 85 static PolicyTestParams ForBooleanPolicy( | |
| 86 ConfigurationPolicyType type, | |
| 87 const char* policy_name) { | |
| 88 return PolicyTestParams(type, | |
| 89 policy_name, | |
| 90 Value::CreateBooleanValue(true), | |
| 91 Value::CreateBooleanValue(false)); | |
| 92 } | |
| 93 static PolicyTestParams ForIntegerPolicy( | |
| 94 ConfigurationPolicyType type, | |
| 95 const char* policy_name) { | |
| 96 return PolicyTestParams(type, | |
| 97 policy_name, | |
| 98 Value::CreateIntegerValue(42), | |
| 99 Value::CreateIntegerValue(17)); | |
| 100 } | |
| 101 static PolicyTestParams ForListPolicy( | |
| 102 ConfigurationPolicyType type, | |
| 103 const char* policy_name) { | |
| 104 ListValue* hklm_value = new ListValue; | |
| 105 hklm_value->Set(0U, Value::CreateStringValue("It's a plane!")); | |
| 106 ListValue* hkcu_value = new ListValue; | |
| 107 hkcu_value->Set(0U, Value::CreateStringValue("It's a bird!")); | |
| 108 hkcu_value->Set(0U, Value::CreateStringValue("It's a flying carpet!")); | |
| 109 return PolicyTestParams(type, policy_name, hklm_value, hkcu_value); | |
| 110 } | |
| 111 | 42 |
| 112 private: | 43 private: |
| 113 ConfigurationPolicyType type_; | 44 void ActivateOverrides(); |
| 114 const char* policy_name_; | 45 void RemoveOverrides(); |
| 115 scoped_ptr<Value> hklm_value_; | 46 |
| 116 scoped_ptr<Value> hkcu_value_; | 47 // Deletes the sandbox keys. |
| 48 void DeleteKeys(); | |
| 49 | |
| 50 // Keys are created for the lifetime of a test to contain | |
| 51 // the sandboxed HKCU and HKLM hives, respectively. | |
| 52 RegKey temp_hkcu_hive_key_; | |
| 53 RegKey temp_hklm_hive_key_; | |
| 54 | |
| 55 DISALLOW_COPY_AND_ASSIGN(ScopedGroupPolicyRegistrySandbox); | |
| 117 }; | 56 }; |
| 118 | 57 |
| 119 } // namespace | 58 class TestHarness : public PolicyProviderTestHarness { |
| 59 public: | |
| 60 explicit TestHarness(HKEY hive); | |
| 61 virtual ~TestHarness(); | |
| 120 | 62 |
| 121 // This test class provides sandboxing and mocking for the parts of the | 63 virtual void SetUp() OVERRIDE; |
| 122 // Windows Registry implementing Group Policy. The |SetUp| method prepares | |
| 123 // two temporary sandbox keys in |kUnitTestRegistrySubKey|, one for HKLM and one | |
| 124 // for HKCU. A test's calls to the registry are redirected by Windows to these | |
| 125 // sandboxes, allowing the tests to manipulate and access policy as if it | |
| 126 // were active, but without actually changing the parts of the Registry that | |
| 127 // are managed by Group Policy. | |
| 128 class ConfigurationPolicyProviderWinTest | |
| 129 : public testing::TestWithParam<PolicyTestParams> { | |
| 130 public: | |
| 131 ConfigurationPolicyProviderWinTest(); | |
| 132 | 64 |
| 133 // testing::Test method overrides: | 65 virtual AsynchronousPolicyProvider* CreateProvider( |
| 134 virtual void SetUp(); | 66 const PolicyDefinitionList* policy_definition_list) OVERRIDE; |
| 135 virtual void TearDown(); | |
| 136 | 67 |
| 68 virtual void InstallEmptyPolicy() OVERRIDE; | |
| 69 virtual void InstallStringPolicy(const std::string& policy_name, | |
| 70 const std::string& policy_value) OVERRIDE; | |
| 71 virtual void InstallIntegerPolicy(const std::string& policy_name, | |
| 72 int policy_value) OVERRIDE; | |
| 73 virtual void InstallBooleanPolicy(const std::string& policy_name, | |
| 74 bool policy_value) OVERRIDE; | |
| 75 virtual void InstallStringListPolicy(const std::string& policy_name, | |
| 76 const ListValue* policy_value) OVERRIDE; | |
| 77 | |
| 78 // Creates a harness instance that will install policy in HKCU or HKLM, | |
| 79 // respectively. | |
| 80 static PolicyProviderTestHarness* CreateHKCU(); | |
| 81 static PolicyProviderTestHarness* CreateHKLM(); | |
| 82 | |
| 83 private: | |
| 84 // Activates/Deactivates registry key redirection. | |
| 137 void ActivateOverrides(); | 85 void ActivateOverrides(); |
| 138 void DeactivateOverrides(); | 86 void DeactivateOverrides(); |
| 139 | 87 |
| 140 // Deletes the registry key created during the tests. | 88 // Deletes the registry key created during the tests. |
| 141 void DeleteRegistrySandbox(); | 89 void DeleteRegistrySandbox(); |
|
Joao da Silva
2011/11/09 18:02:40
Nit: remove this 3 dead declarations
Mattias Nissler (ping if slow)
2011/11/09 18:06:05
Done.
| |
| 142 | 90 |
| 143 // Write a string value to the registry. | 91 HKEY hive_; |
| 144 void WriteString(HKEY hive, const char* name, const wchar_t* value); | |
| 145 // Write a DWORD value to the registry. | |
| 146 void WriteDWORD(HKEY hive, const char* name, DWORD value); | |
| 147 | 92 |
| 148 // Write the given value to the registry. | 93 ScopedGroupPolicyRegistrySandbox registry_sandbox_; |
| 149 void WriteValue(HKEY hive, const char* name, const Value* value); | |
| 150 // Write a value that is not compatible with the given |value|. | |
| 151 void WriteInvalidValue(HKEY hive, const char* name, const Value* value); | |
| 152 | 94 |
| 153 protected: | 95 DISALLOW_COPY_AND_ASSIGN(TestHarness); |
| 154 scoped_ptr<ConfigurationPolicyProviderWin> provider_; | |
| 155 | |
| 156 // A message loop must be declared and instantiated for these tests, | |
| 157 // because Windows policy provider create WaitableEvents and | |
| 158 // ObjectWatchers that require the tests to have a MessageLoop associated | |
| 159 // with the thread executing the tests. | |
| 160 MessageLoop loop_; | |
| 161 | |
| 162 private: | |
| 163 content::TestBrowserThread ui_thread_; | |
| 164 content::TestBrowserThread file_thread_; | |
| 165 | |
| 166 // Keys are created for the lifetime of a test to contain | |
| 167 // the sandboxed HKCU and HKLM hives, respectively. | |
| 168 RegKey temp_hkcu_hive_key_; | |
| 169 RegKey temp_hklm_hive_key_; | |
| 170 }; | 96 }; |
| 171 | 97 |
| 172 ConfigurationPolicyProviderWinTest::ConfigurationPolicyProviderWinTest() | 98 ScopedGroupPolicyRegistrySandbox::ScopedGroupPolicyRegistrySandbox() { |
| 173 : ui_thread_(BrowserThread::UI, &loop_), | |
| 174 file_thread_(BrowserThread::FILE, &loop_), | |
| 175 temp_hklm_hive_key_(HKEY_CURRENT_USER, kUnitTestMachineOverrideSubKey, | |
| 176 KEY_READ), | |
| 177 temp_hkcu_hive_key_(HKEY_CURRENT_USER, kUnitTestUserOverrideSubKey, | |
| 178 KEY_READ) { | |
| 179 } | |
| 180 | |
| 181 void ConfigurationPolicyProviderWinTest::SetUp() { | |
| 182 // Cleanup any remnants of previous tests. | 99 // Cleanup any remnants of previous tests. |
| 183 DeleteRegistrySandbox(); | 100 DeleteKeys(); |
| 184 | 101 |
| 185 // Create the subkeys to hold the overridden HKLM and HKCU | 102 // Create the subkeys to hold the overridden HKLM and HKCU |
| 186 // policy settings. | 103 // policy settings. |
| 187 temp_hklm_hive_key_.Create(HKEY_CURRENT_USER, | 104 temp_hklm_hive_key_.Create(HKEY_CURRENT_USER, |
| 188 kUnitTestMachineOverrideSubKey, | 105 kUnitTestMachineOverrideSubKey, |
| 189 KEY_ALL_ACCESS); | 106 KEY_ALL_ACCESS); |
| 190 temp_hkcu_hive_key_.Create(HKEY_CURRENT_USER, | 107 temp_hkcu_hive_key_.Create(HKEY_CURRENT_USER, |
| 191 kUnitTestUserOverrideSubKey, | 108 kUnitTestUserOverrideSubKey, |
| 192 KEY_ALL_ACCESS); | 109 KEY_ALL_ACCESS); |
| 193 | 110 |
| 194 ActivateOverrides(); | 111 ActivateOverrides(); |
| 195 | |
| 196 provider_.reset(new ConfigurationPolicyProviderWin( | |
| 197 GetChromePolicyDefinitionList())); | |
| 198 } | 112 } |
| 199 | 113 |
| 200 void ConfigurationPolicyProviderWinTest::TearDown() { | 114 ScopedGroupPolicyRegistrySandbox::~ScopedGroupPolicyRegistrySandbox() { |
| 201 DeactivateOverrides(); | 115 RemoveOverrides(); |
| 202 DeleteRegistrySandbox(); | 116 DeleteKeys(); |
| 203 loop_.RunAllPending(); | |
| 204 } | 117 } |
| 205 | 118 |
| 206 void ConfigurationPolicyProviderWinTest::ActivateOverrides() { | 119 void ScopedGroupPolicyRegistrySandbox::ActivateOverrides() { |
| 207 HRESULT result = RegOverridePredefKey(HKEY_LOCAL_MACHINE, | 120 ASSERT_HRESULT_SUCCEEDED(RegOverridePredefKey(HKEY_LOCAL_MACHINE, |
| 208 temp_hklm_hive_key_.Handle()); | 121 temp_hklm_hive_key_.Handle())); |
| 209 EXPECT_EQ(ERROR_SUCCESS, result); | 122 ASSERT_HRESULT_SUCCEEDED(RegOverridePredefKey(HKEY_CURRENT_USER, |
| 210 result = RegOverridePredefKey(HKEY_CURRENT_USER, | 123 temp_hkcu_hive_key_.Handle())); |
| 211 temp_hkcu_hive_key_.Handle()); | |
| 212 EXPECT_EQ(ERROR_SUCCESS, result); | |
| 213 } | 124 } |
| 214 | 125 |
| 215 void ConfigurationPolicyProviderWinTest::DeactivateOverrides() { | 126 void ScopedGroupPolicyRegistrySandbox::RemoveOverrides() { |
| 216 uint32 result = RegOverridePredefKey(HKEY_LOCAL_MACHINE, 0); | 127 ASSERT_HRESULT_SUCCEEDED(RegOverridePredefKey(HKEY_LOCAL_MACHINE, 0)); |
| 217 EXPECT_EQ(ERROR_SUCCESS, result); | 128 ASSERT_HRESULT_SUCCEEDED(RegOverridePredefKey(HKEY_CURRENT_USER, 0)); |
| 218 result = RegOverridePredefKey(HKEY_CURRENT_USER, 0); | |
| 219 EXPECT_EQ(ERROR_SUCCESS, result); | |
| 220 } | 129 } |
| 221 | 130 |
| 222 void ConfigurationPolicyProviderWinTest::DeleteRegistrySandbox() { | 131 void ScopedGroupPolicyRegistrySandbox::DeleteKeys() { |
| 223 temp_hklm_hive_key_.Close(); | |
| 224 temp_hkcu_hive_key_.Close(); | |
| 225 RegKey key(HKEY_CURRENT_USER, kUnitTestRegistrySubKey, KEY_ALL_ACCESS); | 132 RegKey key(HKEY_CURRENT_USER, kUnitTestRegistrySubKey, KEY_ALL_ACCESS); |
| 226 key.DeleteKey(L""); | 133 key.DeleteKey(L""); |
| 227 } | 134 } |
| 228 | 135 |
| 229 void ConfigurationPolicyProviderWinTest::WriteString(HKEY hive, | 136 TestHarness::TestHarness(HKEY hive) |
| 230 const char* name, | 137 : hive_(hive) {} |
| 231 const wchar_t* value) { | 138 |
| 232 RegKey key(hive, policy::kRegistrySubKey, KEY_ALL_ACCESS); | 139 TestHarness::~TestHarness() {} |
| 233 key.WriteValue(UTF8ToUTF16(name).c_str(), value); | 140 |
| 141 void TestHarness::SetUp() {} | |
| 142 | |
| 143 AsynchronousPolicyProvider* TestHarness::CreateProvider( | |
| 144 const PolicyDefinitionList* policy_definition_list) { | |
| 145 return new ConfigurationPolicyProviderWin(policy_definition_list); | |
| 234 } | 146 } |
| 235 | 147 |
| 236 void ConfigurationPolicyProviderWinTest::WriteDWORD(HKEY hive, | 148 void TestHarness::InstallEmptyPolicy() {} |
| 237 const char* name, | 149 |
| 238 DWORD value) { | 150 void TestHarness::InstallStringPolicy(const std::string& policy_name, |
| 239 RegKey key(hive, policy::kRegistrySubKey, KEY_ALL_ACCESS); | 151 const std::string& policy_value) { |
| 240 key.WriteValue(UTF8ToUTF16(name).c_str(), value); | 152 RegKey key(hive_, policy::kRegistrySubKey, KEY_ALL_ACCESS); |
| 153 key.WriteValue(UTF8ToUTF16(policy_name).c_str(), | |
| 154 UTF8ToUTF16(policy_value).c_str()); | |
| 241 } | 155 } |
| 242 | 156 |
| 243 void ConfigurationPolicyProviderWinTest::WriteValue(HKEY hive, | 157 void TestHarness::InstallIntegerPolicy(const std::string& policy_name, |
| 244 const char* name, | 158 int policy_value) { |
| 245 const Value* value) { | 159 RegKey key(hive_, policy::kRegistrySubKey, KEY_ALL_ACCESS); |
| 246 switch (value->GetType()) { | 160 key.WriteValue(UTF8ToUTF16(policy_name).c_str(), |
| 247 case Value::TYPE_BOOLEAN: { | 161 static_cast<DWORD>(policy_value)); |
| 248 bool v; | 162 } |
| 249 ASSERT_TRUE(value->GetAsBoolean(&v)); | 163 |
| 250 WriteDWORD(hive, name, v); | 164 void TestHarness::InstallBooleanPolicy(const std::string& policy_name, |
| 251 break; | 165 bool policy_value) { |
| 252 } | 166 RegKey key(hive_, policy::kRegistrySubKey, KEY_ALL_ACCESS); |
| 253 case Value::TYPE_INTEGER: { | 167 key.WriteValue(UTF8ToUTF16(policy_name).c_str(), |
| 254 int v; | 168 static_cast<DWORD>(policy_value)); |
| 255 ASSERT_TRUE(value->GetAsInteger(&v)); | 169 } |
| 256 WriteDWORD(hive, name, v); | 170 |
| 257 break; | 171 void TestHarness::InstallStringListPolicy(const std::string& policy_name, |
| 258 } | 172 const ListValue* policy_value) { |
| 259 case Value::TYPE_STRING: { | 173 RegKey key(hive_, |
| 260 std::string v; | 174 (string16(policy::kRegistrySubKey) + ASCIIToUTF16("\\") + |
| 261 ASSERT_TRUE(value->GetAsString(&v)); | 175 UTF8ToUTF16(policy_name)).c_str(), |
| 262 WriteString(hive, name, UTF8ToUTF16(v).c_str()); | 176 KEY_ALL_ACCESS); |
| 263 break; | 177 int index = 1; |
| 264 } | 178 for (ListValue::const_iterator element(policy_value->begin()); |
| 265 case Value::TYPE_LIST: { | 179 element != policy_value->end(); |
| 266 const ListValue* list = static_cast<const ListValue*>(value); | 180 ++element) { |
| 267 RegKey key(hive, | 181 std::string element_value; |
| 268 (string16(policy::kRegistrySubKey) + ASCIIToUTF16("\\") + | 182 if (!(*element)->GetAsString(&element_value)) |
| 269 UTF8ToUTF16(name)).c_str(), | 183 continue; |
| 270 KEY_ALL_ACCESS); | 184 std::string name(base::IntToString(index++)); |
| 271 int index = 1; | 185 key.WriteValue(UTF8ToUTF16(name).c_str(), |
| 272 for (ListValue::const_iterator element(list->begin()); | 186 UTF8ToUTF16(element_value).c_str()); |
| 273 element != list->end(); ++element) { | |
| 274 ASSERT_TRUE((*element)->IsType(Value::TYPE_STRING)); | |
| 275 std::string element_value; | |
| 276 ASSERT_TRUE((*element)->GetAsString(&element_value)); | |
| 277 key.WriteValue(base::IntToString16(index++).c_str(), | |
| 278 UTF8ToUTF16(element_value).c_str()); | |
| 279 } | |
| 280 break; | |
| 281 } | |
| 282 default: | |
| 283 FAIL() << "Unsupported value type " << value->GetType(); | |
| 284 break; | |
| 285 } | 187 } |
| 286 } | 188 } |
| 287 | 189 |
| 288 void ConfigurationPolicyProviderWinTest::WriteInvalidValue(HKEY hive, | 190 // static |
| 289 const char* name, | 191 PolicyProviderTestHarness* TestHarness::CreateHKCU() { |
| 290 const Value* value) { | 192 return new TestHarness(HKEY_CURRENT_USER); |
| 291 if (value->IsType(Value::TYPE_STRING)) | |
| 292 WriteDWORD(hive, name, -1); | |
| 293 else | |
| 294 WriteString(hive, name, L"bad value"); | |
| 295 } | 193 } |
| 296 | 194 |
| 297 TEST_P(ConfigurationPolicyProviderWinTest, Default) { | 195 // static |
| 298 PolicyMap policy_map; | 196 PolicyProviderTestHarness* TestHarness::CreateHKLM() { |
| 299 provider_->Provide(&policy_map); | 197 return new TestHarness(HKEY_LOCAL_MACHINE); |
| 300 EXPECT_TRUE(policy_map.empty()); | |
| 301 } | 198 } |
| 302 | 199 |
| 303 TEST_P(ConfigurationPolicyProviderWinTest, InvalidValue) { | 200 } // namespace |
| 304 WriteInvalidValue(HKEY_LOCAL_MACHINE, | 201 |
| 305 GetParam().policy_name(), | 202 // Instantiate abstract test case for basic policy reading tests. |
| 306 GetParam().hklm_value()); | 203 INSTANTIATE_TEST_CASE_P( |
| 307 WriteInvalidValue(HKEY_CURRENT_USER, | 204 ConfigurationPolicyProviderWinTest, |
| 308 GetParam().policy_name(), | 205 ConfigurationPolicyProviderTest, |
| 309 GetParam().hkcu_value()); | 206 testing::Values(TestHarness::CreateHKCU, TestHarness::CreateHKLM)); |
| 310 provider_->loader()->Reload(); | 207 |
| 208 // Test cases for windows policy provider specific functionality. | |
| 209 class ConfigurationPolicyProviderWinTest : public AsynchronousPolicyTestBase { | |
| 210 protected: | |
| 211 ConfigurationPolicyProviderWinTest() | |
| 212 : provider_(&test_policy_definitions::kList) {} | |
| 213 virtual ~ConfigurationPolicyProviderWinTest() {} | |
| 214 | |
| 215 ScopedGroupPolicyRegistrySandbox registry_sandbox_; | |
| 216 ConfigurationPolicyProviderWin provider_; | |
| 217 }; | |
| 218 | |
| 219 TEST_F(ConfigurationPolicyProviderWinTest, HKLMOverHKCU) { | |
| 220 RegKey hklm_key(HKEY_LOCAL_MACHINE, policy::kRegistrySubKey, KEY_ALL_ACCESS); | |
| 221 hklm_key.WriteValue(UTF8ToUTF16(test_policy_definitions::kKeyString).c_str(), | |
| 222 UTF8ToUTF16("hklm").c_str()); | |
| 223 RegKey hkcu_key(HKEY_CURRENT_USER, policy::kRegistrySubKey, KEY_ALL_ACCESS); | |
| 224 hkcu_key.WriteValue(UTF8ToUTF16(test_policy_definitions::kKeyString).c_str(), | |
| 225 UTF8ToUTF16("hkcu").c_str()); | |
| 226 | |
| 227 provider_.ForceReload(); | |
| 311 loop_.RunAllPending(); | 228 loop_.RunAllPending(); |
| 229 | |
| 312 PolicyMap policy_map; | 230 PolicyMap policy_map; |
| 313 provider_->Provide(&policy_map); | 231 provider_.Provide(&policy_map); |
| 314 EXPECT_TRUE(policy_map.empty()); | 232 const Value* value = policy_map.Get(test_policy_definitions::kPolicyString); |
| 233 EXPECT_TRUE(StringValue("hklm").Equals(value)); | |
| 315 } | 234 } |
| 316 | 235 |
| 317 TEST_P(ConfigurationPolicyProviderWinTest, HKLM) { | |
| 318 WriteValue(HKEY_LOCAL_MACHINE, | |
| 319 GetParam().policy_name(), | |
| 320 GetParam().hklm_value()); | |
| 321 provider_->loader()->Reload(); | |
| 322 loop_.RunAllPending(); | |
| 323 PolicyMap policy_map; | |
| 324 provider_->Provide(&policy_map); | |
| 325 const Value* value = policy_map.Get(GetParam().type()); | |
| 326 ASSERT_TRUE(value); | |
| 327 EXPECT_TRUE(value->Equals(GetParam().hklm_value())); | |
| 328 } | |
| 329 | |
| 330 TEST_P(ConfigurationPolicyProviderWinTest, HKCU) { | |
| 331 WriteValue(HKEY_CURRENT_USER, | |
| 332 GetParam().policy_name(), | |
| 333 GetParam().hkcu_value()); | |
| 334 provider_->loader()->Reload(); | |
| 335 loop_.RunAllPending(); | |
| 336 PolicyMap policy_map; | |
| 337 provider_->Provide(&policy_map); | |
| 338 const Value* value = policy_map.Get(GetParam().type()); | |
| 339 ASSERT_TRUE(value); | |
| 340 EXPECT_TRUE(value->Equals(GetParam().hkcu_value())); | |
| 341 } | |
| 342 | |
| 343 TEST_P(ConfigurationPolicyProviderWinTest, HKLMOverHKCU) { | |
| 344 WriteValue(HKEY_LOCAL_MACHINE, | |
| 345 GetParam().policy_name(), | |
| 346 GetParam().hklm_value()); | |
| 347 WriteValue(HKEY_CURRENT_USER, | |
| 348 GetParam().policy_name(), | |
| 349 GetParam().hkcu_value()); | |
| 350 provider_->loader()->Reload(); | |
| 351 loop_.RunAllPending(); | |
| 352 PolicyMap policy_map; | |
| 353 provider_->Provide(&policy_map); | |
| 354 const Value* value = policy_map.Get(GetParam().type()); | |
| 355 ASSERT_TRUE(value); | |
| 356 EXPECT_TRUE(value->Equals(GetParam().hklm_value())); | |
| 357 } | |
| 358 | |
| 359 // Test parameters for all supported policies. testing::Values() has a limit of | |
| 360 // 50 parameters which is reached in this instantiation; new policies should go | |
| 361 // in the next instantiation after this one. | |
| 362 INSTANTIATE_TEST_CASE_P( | |
| 363 ConfigurationPolicyProviderWinTestInstance, | |
| 364 ConfigurationPolicyProviderWinTest, | |
| 365 testing::Values( | |
| 366 PolicyTestParams::ForStringPolicy( | |
| 367 kPolicyHomepageLocation, | |
| 368 key::kHomepageLocation), | |
| 369 PolicyTestParams::ForBooleanPolicy( | |
| 370 kPolicyHomepageIsNewTabPage, | |
| 371 key::kHomepageIsNewTabPage), | |
| 372 PolicyTestParams::ForIntegerPolicy( | |
| 373 kPolicyRestoreOnStartup, | |
| 374 key::kRestoreOnStartup), | |
| 375 PolicyTestParams::ForListPolicy( | |
| 376 kPolicyRestoreOnStartupURLs, | |
| 377 key::kRestoreOnStartupURLs), | |
| 378 PolicyTestParams::ForBooleanPolicy( | |
| 379 kPolicyDefaultSearchProviderEnabled, | |
| 380 key::kDefaultSearchProviderEnabled), | |
| 381 PolicyTestParams::ForStringPolicy( | |
| 382 kPolicyDefaultSearchProviderName, | |
| 383 key::kDefaultSearchProviderName), | |
| 384 PolicyTestParams::ForStringPolicy( | |
| 385 kPolicyDefaultSearchProviderKeyword, | |
| 386 key::kDefaultSearchProviderKeyword), | |
| 387 PolicyTestParams::ForStringPolicy( | |
| 388 kPolicyDefaultSearchProviderSearchURL, | |
| 389 key::kDefaultSearchProviderSearchURL), | |
| 390 PolicyTestParams::ForStringPolicy( | |
| 391 kPolicyDefaultSearchProviderSuggestURL, | |
| 392 key::kDefaultSearchProviderSuggestURL), | |
| 393 PolicyTestParams::ForStringPolicy( | |
| 394 kPolicyDefaultSearchProviderInstantURL, | |
| 395 key::kDefaultSearchProviderInstantURL), | |
| 396 PolicyTestParams::ForStringPolicy( | |
| 397 kPolicyDefaultSearchProviderIconURL, | |
| 398 key::kDefaultSearchProviderIconURL), | |
| 399 PolicyTestParams::ForListPolicy( | |
| 400 kPolicyDefaultSearchProviderEncodings, | |
| 401 key::kDefaultSearchProviderEncodings), | |
| 402 PolicyTestParams::ForStringPolicy( | |
| 403 kPolicyProxyMode, | |
| 404 key::kProxyMode), | |
| 405 PolicyTestParams::ForIntegerPolicy( | |
| 406 kPolicyProxyServerMode, | |
| 407 key::kProxyServerMode), | |
| 408 PolicyTestParams::ForStringPolicy( | |
| 409 kPolicyProxyServer, | |
| 410 key::kProxyServer), | |
| 411 PolicyTestParams::ForStringPolicy( | |
| 412 kPolicyProxyPacUrl, | |
| 413 key::kProxyPacUrl), | |
| 414 PolicyTestParams::ForStringPolicy( | |
| 415 kPolicyProxyBypassList, | |
| 416 key::kProxyBypassList), | |
| 417 PolicyTestParams::ForBooleanPolicy( | |
| 418 kPolicyAlternateErrorPagesEnabled, | |
| 419 key::kAlternateErrorPagesEnabled), | |
| 420 PolicyTestParams::ForBooleanPolicy( | |
| 421 kPolicySearchSuggestEnabled, | |
| 422 key::kSearchSuggestEnabled), | |
| 423 PolicyTestParams::ForBooleanPolicy( | |
| 424 kPolicyDnsPrefetchingEnabled, | |
| 425 key::kDnsPrefetchingEnabled), | |
| 426 PolicyTestParams::ForBooleanPolicy( | |
| 427 kPolicySafeBrowsingEnabled, | |
| 428 key::kSafeBrowsingEnabled), | |
| 429 PolicyTestParams::ForBooleanPolicy( | |
| 430 kPolicyMetricsReportingEnabled, | |
| 431 key::kMetricsReportingEnabled), | |
| 432 PolicyTestParams::ForBooleanPolicy( | |
| 433 kPolicyPasswordManagerEnabled, | |
| 434 key::kPasswordManagerEnabled), | |
| 435 PolicyTestParams::ForListPolicy( | |
| 436 kPolicyDisabledPlugins, | |
| 437 key::kDisabledPlugins), | |
| 438 PolicyTestParams::ForListPolicy( | |
| 439 kPolicyDisabledPluginsExceptions, | |
| 440 key::kDisabledPluginsExceptions), | |
| 441 PolicyTestParams::ForListPolicy( | |
| 442 kPolicyEnabledPlugins, | |
| 443 key::kEnabledPlugins), | |
| 444 PolicyTestParams::ForBooleanPolicy( | |
| 445 kPolicyAutoFillEnabled, | |
| 446 key::kAutoFillEnabled), | |
| 447 PolicyTestParams::ForBooleanPolicy( | |
| 448 kPolicySyncDisabled, | |
| 449 key::kSyncDisabled), | |
| 450 PolicyTestParams::ForStringPolicy( | |
| 451 kPolicyApplicationLocaleValue, | |
| 452 key::kApplicationLocaleValue), | |
| 453 PolicyTestParams::ForListPolicy( | |
| 454 kPolicyExtensionInstallWhitelist, | |
| 455 key::kExtensionInstallWhitelist), | |
| 456 PolicyTestParams::ForListPolicy( | |
| 457 kPolicyExtensionInstallBlacklist, | |
| 458 key::kExtensionInstallBlacklist), | |
| 459 PolicyTestParams::ForBooleanPolicy( | |
| 460 kPolicyShowHomeButton, | |
| 461 key::kShowHomeButton), | |
| 462 PolicyTestParams::ForBooleanPolicy( | |
| 463 kPolicyPrintingEnabled, | |
| 464 key::kPrintingEnabled), | |
| 465 PolicyTestParams::ForBooleanPolicy( | |
| 466 kPolicyInstantEnabled, | |
| 467 key::kInstantEnabled), | |
| 468 PolicyTestParams::ForIntegerPolicy( | |
| 469 kPolicyIncognitoModeAvailability, | |
| 470 key::kIncognitoModeAvailability), | |
| 471 PolicyTestParams::ForBooleanPolicy( | |
| 472 kPolicyDisablePluginFinder, | |
| 473 key::kDisablePluginFinder), | |
| 474 PolicyTestParams::ForBooleanPolicy( | |
| 475 kPolicyClearSiteDataOnExit, | |
| 476 key::kClearSiteDataOnExit), | |
| 477 PolicyTestParams::ForStringPolicy( | |
| 478 kPolicyDownloadDirectory, | |
| 479 key::kDownloadDirectory), | |
| 480 PolicyTestParams::ForBooleanPolicy( | |
| 481 kPolicyDefaultBrowserSettingEnabled, | |
| 482 key::kDefaultBrowserSettingEnabled), | |
| 483 PolicyTestParams::ForBooleanPolicy( | |
| 484 kPolicyCloudPrintProxyEnabled, | |
| 485 key::kCloudPrintProxyEnabled), | |
| 486 PolicyTestParams::ForBooleanPolicy( | |
| 487 kPolicyTranslateEnabled, | |
| 488 key::kTranslateEnabled), | |
| 489 PolicyTestParams::ForBooleanPolicy( | |
| 490 kPolicyAllowOutdatedPlugins, | |
| 491 key::kAllowOutdatedPlugins), | |
| 492 PolicyTestParams::ForBooleanPolicy( | |
| 493 kPolicyAlwaysAuthorizePlugins, | |
| 494 key::kAlwaysAuthorizePlugins), | |
| 495 PolicyTestParams::ForBooleanPolicy( | |
| 496 kPolicyBookmarkBarEnabled, | |
| 497 key::kBookmarkBarEnabled), | |
| 498 PolicyTestParams::ForBooleanPolicy( | |
| 499 kPolicyEditBookmarksEnabled, | |
| 500 key::kEditBookmarksEnabled), | |
| 501 PolicyTestParams::ForBooleanPolicy( | |
| 502 kPolicyAllowFileSelectionDialogs, | |
| 503 key::kAllowFileSelectionDialogs), | |
| 504 PolicyTestParams::ForListPolicy( | |
| 505 kPolicyDisabledSchemes, | |
| 506 key::kDisabledSchemes), | |
| 507 PolicyTestParams::ForStringPolicy( | |
| 508 kPolicyDiskCacheDir, | |
| 509 key::kDiskCacheDir), | |
| 510 PolicyTestParams::ForIntegerPolicy( | |
| 511 kPolicyMaxConnectionsPerProxy, | |
| 512 key::kMaxConnectionsPerProxy), | |
| 513 PolicyTestParams::ForListPolicy( | |
| 514 kPolicyURLBlacklist, | |
| 515 key::kURLBlacklist))); | |
| 516 | |
| 517 // testing::Values has a limit of 50 test templates, which is reached by the | |
| 518 // instantiations above. Add tests for new policies here: | |
| 519 INSTANTIATE_TEST_CASE_P( | |
| 520 ConfigurationPolicyProviderWinTestInstance2, | |
| 521 ConfigurationPolicyProviderWinTest, | |
| 522 testing::Values( | |
| 523 PolicyTestParams::ForListPolicy( | |
| 524 kPolicyURLWhitelist, | |
| 525 key::kURLWhitelist), | |
| 526 PolicyTestParams::ForBooleanPolicy( | |
| 527 kPolicyCloudPrintSubmitEnabled, | |
| 528 key::kCloudPrintSubmitEnabled))); | |
| 529 | |
| 530 } // namespace policy | 236 } // namespace policy |
| OLD | NEW |