OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 | |
7 #include <windows.h> | 6 #include <windows.h> |
8 | 7 |
| 8 #include "base/message_loop.h" |
9 #include "base/scoped_ptr.h" | 9 #include "base/scoped_ptr.h" |
10 #include "base/stl_util-inl.h" | 10 #include "base/stl_util-inl.h" |
11 #include "base/string_number_conversions.h" | 11 #include "base/string_number_conversions.h" |
12 #include "base/string_piece.h" | 12 #include "base/string_piece.h" |
13 #include "base/utf_string_conversions.h" | 13 #include "base/utf_string_conversions.h" |
14 #include "base/win/registry.h" | 14 #include "base/win/registry.h" |
| 15 #include "chrome/browser/browser_thread.h" |
| 16 #include "chrome/browser/policy/asynchronous_policy_loader.h" |
15 #include "chrome/browser/policy/configuration_policy_pref_store.h" | 17 #include "chrome/browser/policy/configuration_policy_pref_store.h" |
16 #include "chrome/browser/policy/configuration_policy_provider_win.h" | 18 #include "chrome/browser/policy/configuration_policy_provider_win.h" |
17 #include "chrome/browser/policy/mock_configuration_policy_store.h" | 19 #include "chrome/browser/policy/mock_configuration_policy_store.h" |
18 #include "chrome/common/policy_constants.h" | 20 #include "chrome/common/policy_constants.h" |
19 #include "chrome/common/pref_names.h" | 21 #include "chrome/common/pref_names.h" |
20 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
21 | 23 |
22 using base::win::RegKey; | 24 using base::win::RegKey; |
23 | 25 |
24 namespace policy { | 26 namespace policy { |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
144 | 146 |
145 // Write the given value to the registry. | 147 // Write the given value to the registry. |
146 void WriteValue(HKEY hive, const char* name, const Value* value); | 148 void WriteValue(HKEY hive, const char* name, const Value* value); |
147 // Write a value that is not compatible with the given |value|. | 149 // Write a value that is not compatible with the given |value|. |
148 void WriteInvalidValue(HKEY hive, const char* name, const Value* value); | 150 void WriteInvalidValue(HKEY hive, const char* name, const Value* value); |
149 | 151 |
150 protected: | 152 protected: |
151 scoped_ptr<MockConfigurationPolicyStore> store_; | 153 scoped_ptr<MockConfigurationPolicyStore> store_; |
152 scoped_ptr<ConfigurationPolicyProviderWin> provider_; | 154 scoped_ptr<ConfigurationPolicyProviderWin> provider_; |
153 | 155 |
154 private: | |
155 // A message loop must be declared and instantiated for these tests, | 156 // A message loop must be declared and instantiated for these tests, |
156 // because Windows policy provider create WaitableEvents and | 157 // because Windows policy provider create WaitableEvents and |
157 // ObjectWatchers that require the tests to have a MessageLoop associated | 158 // ObjectWatchers that require the tests to have a MessageLoop associated |
158 // with the thread executing the tests. | 159 // with the thread executing the tests. |
159 MessageLoop loop_; | 160 MessageLoop loop_; |
160 | 161 |
| 162 private: |
| 163 BrowserThread ui_thread_; |
| 164 BrowserThread file_thread_; |
| 165 |
161 // Keys are created for the lifetime of a test to contain | 166 // Keys are created for the lifetime of a test to contain |
162 // the sandboxed HKCU and HKLM hives, respectively. | 167 // the sandboxed HKCU and HKLM hives, respectively. |
163 RegKey temp_hkcu_hive_key_; | 168 RegKey temp_hkcu_hive_key_; |
164 RegKey temp_hklm_hive_key_; | 169 RegKey temp_hklm_hive_key_; |
165 }; | 170 }; |
166 | 171 |
167 ConfigurationPolicyProviderWinTest::ConfigurationPolicyProviderWinTest() | 172 ConfigurationPolicyProviderWinTest::ConfigurationPolicyProviderWinTest() |
168 : temp_hklm_hive_key_(HKEY_CURRENT_USER, kUnitTestMachineOverrideSubKey, | 173 : ui_thread_(BrowserThread::UI, &loop_), |
| 174 file_thread_(BrowserThread::FILE, &loop_), |
| 175 temp_hklm_hive_key_(HKEY_CURRENT_USER, kUnitTestMachineOverrideSubKey, |
169 KEY_READ), | 176 KEY_READ), |
170 temp_hkcu_hive_key_(HKEY_CURRENT_USER, kUnitTestUserOverrideSubKey, | 177 temp_hkcu_hive_key_(HKEY_CURRENT_USER, kUnitTestUserOverrideSubKey, |
171 KEY_READ) { | 178 KEY_READ) { |
172 } | 179 } |
173 | 180 |
174 void ConfigurationPolicyProviderWinTest::SetUp() { | 181 void ConfigurationPolicyProviderWinTest::SetUp() { |
175 // Cleanup any remnants of previous tests. | 182 // Cleanup any remnants of previous tests. |
176 DeleteRegistrySandbox(); | 183 DeleteRegistrySandbox(); |
177 | 184 |
178 // Create the subkeys to hold the overridden HKLM and HKCU | 185 // Create the subkeys to hold the overridden HKLM and HKCU |
179 // policy settings. | 186 // policy settings. |
180 temp_hklm_hive_key_.Create(HKEY_CURRENT_USER, | 187 temp_hklm_hive_key_.Create(HKEY_CURRENT_USER, |
181 kUnitTestMachineOverrideSubKey, | 188 kUnitTestMachineOverrideSubKey, |
182 KEY_ALL_ACCESS); | 189 KEY_ALL_ACCESS); |
183 temp_hkcu_hive_key_.Create(HKEY_CURRENT_USER, | 190 temp_hkcu_hive_key_.Create(HKEY_CURRENT_USER, |
184 kUnitTestUserOverrideSubKey, | 191 kUnitTestUserOverrideSubKey, |
185 KEY_ALL_ACCESS); | 192 KEY_ALL_ACCESS); |
186 | 193 |
187 ActivateOverrides(); | 194 ActivateOverrides(); |
188 | 195 |
189 store_.reset(new MockConfigurationPolicyStore); | 196 store_.reset(new MockConfigurationPolicyStore); |
190 provider_.reset(new ConfigurationPolicyProviderWin( | 197 provider_.reset(new ConfigurationPolicyProviderWin( |
191 ConfigurationPolicyPrefStore::GetChromePolicyDefinitionList())); | 198 ConfigurationPolicyPrefStore::GetChromePolicyDefinitionList())); |
192 } | 199 } |
193 | 200 |
194 void ConfigurationPolicyProviderWinTest::TearDown() { | 201 void ConfigurationPolicyProviderWinTest::TearDown() { |
195 DeactivateOverrides(); | 202 DeactivateOverrides(); |
196 DeleteRegistrySandbox(); | 203 DeleteRegistrySandbox(); |
| 204 loop_.RunAllPending(); |
197 } | 205 } |
198 | 206 |
199 void ConfigurationPolicyProviderWinTest::ActivateOverrides() { | 207 void ConfigurationPolicyProviderWinTest::ActivateOverrides() { |
200 HRESULT result = RegOverridePredefKey(HKEY_LOCAL_MACHINE, | 208 HRESULT result = RegOverridePredefKey(HKEY_LOCAL_MACHINE, |
201 temp_hklm_hive_key_.Handle()); | 209 temp_hklm_hive_key_.Handle()); |
202 EXPECT_EQ(ERROR_SUCCESS, result); | 210 EXPECT_EQ(ERROR_SUCCESS, result); |
203 result = RegOverridePredefKey(HKEY_CURRENT_USER, | 211 result = RegOverridePredefKey(HKEY_CURRENT_USER, |
204 temp_hkcu_hive_key_.Handle()); | 212 temp_hkcu_hive_key_.Handle()); |
205 EXPECT_EQ(ERROR_SUCCESS, result); | 213 EXPECT_EQ(ERROR_SUCCESS, result); |
206 } | 214 } |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
292 EXPECT_TRUE(store_->policy_map().empty()); | 300 EXPECT_TRUE(store_->policy_map().empty()); |
293 } | 301 } |
294 | 302 |
295 TEST_P(ConfigurationPolicyProviderWinTest, InvalidValue) { | 303 TEST_P(ConfigurationPolicyProviderWinTest, InvalidValue) { |
296 WriteInvalidValue(HKEY_LOCAL_MACHINE, | 304 WriteInvalidValue(HKEY_LOCAL_MACHINE, |
297 GetParam().policy_name(), | 305 GetParam().policy_name(), |
298 GetParam().hklm_value()); | 306 GetParam().hklm_value()); |
299 WriteInvalidValue(HKEY_CURRENT_USER, | 307 WriteInvalidValue(HKEY_CURRENT_USER, |
300 GetParam().policy_name(), | 308 GetParam().policy_name(), |
301 GetParam().hkcu_value()); | 309 GetParam().hkcu_value()); |
| 310 provider_->loader()->Reload(); |
| 311 loop_.RunAllPending(); |
302 provider_->Provide(store_.get()); | 312 provider_->Provide(store_.get()); |
303 EXPECT_TRUE(store_->policy_map().empty()); | 313 EXPECT_TRUE(store_->policy_map().empty()); |
304 } | 314 } |
305 | 315 |
306 TEST_P(ConfigurationPolicyProviderWinTest, HKLM) { | 316 TEST_P(ConfigurationPolicyProviderWinTest, HKLM) { |
307 WriteValue(HKEY_LOCAL_MACHINE, | 317 WriteValue(HKEY_LOCAL_MACHINE, |
308 GetParam().policy_name(), | 318 GetParam().policy_name(), |
309 GetParam().hklm_value()); | 319 GetParam().hklm_value()); |
| 320 provider_->loader()->Reload(); |
| 321 loop_.RunAllPending(); |
310 provider_->Provide(store_.get()); | 322 provider_->Provide(store_.get()); |
311 const Value* value = store_->Get(GetParam().type()); | 323 const Value* value = store_->Get(GetParam().type()); |
312 ASSERT_TRUE(value); | 324 ASSERT_TRUE(value); |
313 EXPECT_TRUE(value->Equals(GetParam().hklm_value())); | 325 EXPECT_TRUE(value->Equals(GetParam().hklm_value())); |
314 } | 326 } |
315 | 327 |
316 TEST_P(ConfigurationPolicyProviderWinTest, HKCU) { | 328 TEST_P(ConfigurationPolicyProviderWinTest, HKCU) { |
317 WriteValue(HKEY_CURRENT_USER, | 329 WriteValue(HKEY_CURRENT_USER, |
318 GetParam().policy_name(), | 330 GetParam().policy_name(), |
319 GetParam().hkcu_value()); | 331 GetParam().hkcu_value()); |
| 332 provider_->loader()->Reload(); |
| 333 loop_.RunAllPending(); |
320 provider_->Provide(store_.get()); | 334 provider_->Provide(store_.get()); |
321 const Value* value = store_->Get(GetParam().type()); | 335 const Value* value = store_->Get(GetParam().type()); |
322 ASSERT_TRUE(value); | 336 ASSERT_TRUE(value); |
323 EXPECT_TRUE(value->Equals(GetParam().hkcu_value())); | 337 EXPECT_TRUE(value->Equals(GetParam().hkcu_value())); |
324 } | 338 } |
325 | 339 |
326 TEST_P(ConfigurationPolicyProviderWinTest, HKLMOverHKCU) { | 340 TEST_P(ConfigurationPolicyProviderWinTest, HKLMOverHKCU) { |
327 WriteValue(HKEY_LOCAL_MACHINE, | 341 WriteValue(HKEY_LOCAL_MACHINE, |
328 GetParam().policy_name(), | 342 GetParam().policy_name(), |
329 GetParam().hklm_value()); | 343 GetParam().hklm_value()); |
330 WriteValue(HKEY_CURRENT_USER, | 344 WriteValue(HKEY_CURRENT_USER, |
331 GetParam().policy_name(), | 345 GetParam().policy_name(), |
332 GetParam().hkcu_value()); | 346 GetParam().hkcu_value()); |
| 347 provider_->loader()->Reload(); |
| 348 loop_.RunAllPending(); |
333 provider_->Provide(store_.get()); | 349 provider_->Provide(store_.get()); |
334 const Value* value = store_->Get(GetParam().type()); | 350 const Value* value = store_->Get(GetParam().type()); |
335 ASSERT_TRUE(value); | 351 ASSERT_TRUE(value); |
336 EXPECT_TRUE(value->Equals(GetParam().hklm_value())); | 352 EXPECT_TRUE(value->Equals(GetParam().hklm_value())); |
337 } | 353 } |
338 | 354 |
339 // Instantiate the test case for all supported policies. | 355 // Instantiate the test case for all supported policies. |
340 INSTANTIATE_TEST_CASE_P( | 356 INSTANTIATE_TEST_CASE_P( |
341 ConfigurationPolicyProviderWinTestInstance, | 357 ConfigurationPolicyProviderWinTestInstance, |
342 ConfigurationPolicyProviderWinTest, | 358 ConfigurationPolicyProviderWinTest, |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
423 kPolicyExtensionInstallDenyList, | 439 kPolicyExtensionInstallDenyList, |
424 key::kExtensionInstallDenyList), | 440 key::kExtensionInstallDenyList), |
425 PolicyTestParams::ForBooleanPolicy( | 441 PolicyTestParams::ForBooleanPolicy( |
426 kPolicyShowHomeButton, | 442 kPolicyShowHomeButton, |
427 key::kShowHomeButton), | 443 key::kShowHomeButton), |
428 PolicyTestParams::ForBooleanPolicy( | 444 PolicyTestParams::ForBooleanPolicy( |
429 kPolicyPrintingEnabled, | 445 kPolicyPrintingEnabled, |
430 key::kPrintingEnabled))); | 446 key::kPrintingEnabled))); |
431 | 447 |
432 } // namespace policy | 448 } // namespace policy |
OLD | NEW |