| 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 <cstring> | 11 #include <cstring> |
| 12 #include <functional> |
| 13 #include <iterator> |
| 14 #include <vector> |
| 11 | 15 |
| 12 #include "base/file_util.h" | 16 #include "base/file_util.h" |
| 13 #include "base/files/file_path.h" | 17 #include "base/files/file_path.h" |
| 18 #include "base/files/scoped_temp_dir.h" |
| 14 #include "base/json/json_writer.h" | 19 #include "base/json/json_writer.h" |
| 15 #include "base/path_service.h" | 20 #include "base/path_service.h" |
| 16 #include "base/process.h" | 21 #include "base/process.h" |
| 17 #include "base/string16.h" | 22 #include "base/string16.h" |
| 18 #include "base/string_util.h" | 23 #include "base/string_util.h" |
| 19 #include "base/stringprintf.h" | 24 #include "base/stringprintf.h" |
| 20 #include "base/strings/string_number_conversions.h" | 25 #include "base/strings/string_number_conversions.h" |
| 26 #include "base/sys_byteorder.h" |
| 21 #include "base/utf_string_conversions.h" | 27 #include "base/utf_string_conversions.h" |
| 22 #include "base/win/registry.h" | 28 #include "base/win/registry.h" |
| 23 #include "chrome/browser/policy/async_policy_provider.h" | 29 #include "chrome/browser/policy/async_policy_provider.h" |
| 24 #include "chrome/browser/policy/configuration_policy_provider_test.h" | 30 #include "chrome/browser/policy/configuration_policy_provider_test.h" |
| 25 #include "chrome/browser/policy/policy_bundle.h" | 31 #include "chrome/browser/policy/policy_bundle.h" |
| 26 #include "chrome/browser/policy/policy_map.h" | 32 #include "chrome/browser/policy/policy_map.h" |
| 33 #include "chrome/browser/policy/preg_parser_win.h" |
| 27 #include "chrome/common/chrome_paths.h" | 34 #include "chrome/common/chrome_paths.h" |
| 28 #include "chrome/common/json_schema/json_schema_constants.h" | 35 #include "chrome/common/json_schema/json_schema_constants.h" |
| 29 #include "policy/policy_constants.h" | 36 #include "policy/policy_constants.h" |
| 30 #include "testing/gtest/include/gtest/gtest.h" | 37 #include "testing/gtest/include/gtest/gtest.h" |
| 31 | 38 |
| 32 namespace schema = json_schema_constants; | 39 namespace schema = json_schema_constants; |
| 33 | 40 |
| 34 using base::win::RegKey; | 41 using base::win::RegKey; |
| 35 | 42 |
| 36 namespace policy { | 43 namespace policy { |
| (...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 225 std::wstring key_name_; | 232 std::wstring key_name_; |
| 226 | 233 |
| 227 // Keys are created for the lifetime of a test to contain | 234 // Keys are created for the lifetime of a test to contain |
| 228 // the sandboxed HKCU and HKLM hives, respectively. | 235 // the sandboxed HKCU and HKLM hives, respectively. |
| 229 RegKey temp_hkcu_hive_key_; | 236 RegKey temp_hkcu_hive_key_; |
| 230 RegKey temp_hklm_hive_key_; | 237 RegKey temp_hklm_hive_key_; |
| 231 | 238 |
| 232 DISALLOW_COPY_AND_ASSIGN(ScopedGroupPolicyRegistrySandbox); | 239 DISALLOW_COPY_AND_ASSIGN(ScopedGroupPolicyRegistrySandbox); |
| 233 }; | 240 }; |
| 234 | 241 |
| 235 class TestHarness : public PolicyProviderTestHarness, | 242 // A test harness that feeds policy via the Chrome GPO registry subtree. |
| 236 public AppliedGPOListProvider { | 243 class RegistryTestHarness : public PolicyProviderTestHarness, |
| 244 public AppliedGPOListProvider { |
| 237 public: | 245 public: |
| 238 explicit TestHarness(HKEY hive, PolicyScope scope); | 246 RegistryTestHarness(HKEY hive, PolicyScope scope); |
| 239 virtual ~TestHarness(); | 247 virtual ~RegistryTestHarness(); |
| 240 | 248 |
| 249 // PolicyProviderTestHarness: |
| 241 virtual void SetUp() OVERRIDE; | 250 virtual void SetUp() OVERRIDE; |
| 242 | 251 |
| 243 // PolicyProviderTestHarness: | |
| 244 virtual ConfigurationPolicyProvider* CreateProvider( | 252 virtual ConfigurationPolicyProvider* CreateProvider( |
| 245 const PolicyDefinitionList* policy_definition_list) OVERRIDE; | 253 const PolicyDefinitionList* policy_list) OVERRIDE; |
| 246 | 254 |
| 247 virtual void InstallEmptyPolicy() OVERRIDE; | 255 virtual void InstallEmptyPolicy() OVERRIDE; |
| 248 virtual void InstallStringPolicy(const std::string& policy_name, | 256 virtual void InstallStringPolicy(const std::string& policy_name, |
| 249 const std::string& policy_value) OVERRIDE; | 257 const std::string& policy_value) OVERRIDE; |
| 250 virtual void InstallIntegerPolicy(const std::string& policy_name, | 258 virtual void InstallIntegerPolicy(const std::string& policy_name, |
| 251 int policy_value) OVERRIDE; | 259 int policy_value) OVERRIDE; |
| 252 virtual void InstallBooleanPolicy(const std::string& policy_name, | 260 virtual void InstallBooleanPolicy(const std::string& policy_name, |
| 253 bool policy_value) OVERRIDE; | 261 bool policy_value) OVERRIDE; |
| 254 virtual void InstallStringListPolicy( | 262 virtual void InstallStringListPolicy( |
| 255 const std::string& policy_name, | 263 const std::string& policy_name, |
| (...skipping 15 matching lines...) Expand all Loading... |
| 271 // Creates a harness instance that will install policy in HKCU or HKLM, | 279 // Creates a harness instance that will install policy in HKCU or HKLM, |
| 272 // respectively. | 280 // respectively. |
| 273 static PolicyProviderTestHarness* CreateHKCU(); | 281 static PolicyProviderTestHarness* CreateHKCU(); |
| 274 static PolicyProviderTestHarness* CreateHKLM(); | 282 static PolicyProviderTestHarness* CreateHKLM(); |
| 275 | 283 |
| 276 private: | 284 private: |
| 277 HKEY hive_; | 285 HKEY hive_; |
| 278 | 286 |
| 279 ScopedGroupPolicyRegistrySandbox registry_sandbox_; | 287 ScopedGroupPolicyRegistrySandbox registry_sandbox_; |
| 280 | 288 |
| 281 DISALLOW_COPY_AND_ASSIGN(TestHarness); | 289 DISALLOW_COPY_AND_ASSIGN(RegistryTestHarness); |
| 290 }; |
| 291 |
| 292 // A test harness that generates PReg files for the provider to read. |
| 293 class PRegTestHarness : public PolicyProviderTestHarness, |
| 294 public AppliedGPOListProvider { |
| 295 public: |
| 296 PRegTestHarness(); |
| 297 virtual ~PRegTestHarness(); |
| 298 |
| 299 // PolicyProviderTestHarness: |
| 300 virtual void SetUp() OVERRIDE; |
| 301 |
| 302 virtual ConfigurationPolicyProvider* CreateProvider( |
| 303 const PolicyDefinitionList* policy_list) OVERRIDE; |
| 304 |
| 305 virtual void InstallEmptyPolicy() OVERRIDE; |
| 306 virtual void InstallStringPolicy(const std::string& policy_name, |
| 307 const std::string& policy_value) OVERRIDE; |
| 308 virtual void InstallIntegerPolicy(const std::string& policy_name, |
| 309 int policy_value) OVERRIDE; |
| 310 virtual void InstallBooleanPolicy(const std::string& policy_name, |
| 311 bool policy_value) OVERRIDE; |
| 312 virtual void InstallStringListPolicy( |
| 313 const std::string& policy_name, |
| 314 const base::ListValue* policy_value) OVERRIDE; |
| 315 virtual void InstallDictionaryPolicy( |
| 316 const std::string& policy_name, |
| 317 const base::DictionaryValue* policy_value) OVERRIDE; |
| 318 virtual void Install3rdPartyPolicy( |
| 319 const base::DictionaryValue* policies) OVERRIDE; |
| 320 |
| 321 // AppliedGPOListProvider: |
| 322 virtual DWORD GetAppliedGPOList(DWORD flags, |
| 323 LPCTSTR machine_name, |
| 324 PSID sid_user, |
| 325 GUID* extension_guid, |
| 326 PGROUP_POLICY_OBJECT* gpo_list) OVERRIDE; |
| 327 virtual BOOL FreeGPOList(PGROUP_POLICY_OBJECT gpo_list) OVERRIDE; |
| 328 |
| 329 // Creates a harness instance. |
| 330 static PolicyProviderTestHarness* Create(); |
| 331 |
| 332 private: |
| 333 // Helper to append a string16 to an uint8 buffer. |
| 334 static void AppendChars(std::vector<uint8>* buffer, const string16& chars); |
| 335 |
| 336 // Appends a record with the given fields to the PReg file. |
| 337 void AppendRecordToPRegFile(const string16& path, |
| 338 const std::string& key, |
| 339 DWORD type, |
| 340 DWORD size, |
| 341 uint8* data); |
| 342 |
| 343 // Appends the given DWORD |value| for |path| + |key| to the PReg file. |
| 344 void AppendDWORDToPRegFile(const string16& path, |
| 345 const std::string& key, |
| 346 DWORD value); |
| 347 |
| 348 // Appends the given string |value| for |path| + |key| to the PReg file. |
| 349 void AppendStringToPRegFile(const string16& path, |
| 350 const std::string& key, |
| 351 const std::string& value); |
| 352 |
| 353 // Appends the given policy |value| for |path| + |key| to the PReg file, |
| 354 // converting and recursing as necessary. |
| 355 void AppendPolicyToPRegFile(const string16& path, |
| 356 const std::string& key, |
| 357 const base::Value* value); |
| 358 |
| 359 base::ScopedTempDir temp_dir_; |
| 360 base::FilePath preg_file_path_; |
| 361 GROUP_POLICY_OBJECT gpo_; |
| 362 |
| 363 DISALLOW_COPY_AND_ASSIGN(PRegTestHarness); |
| 282 }; | 364 }; |
| 283 | 365 |
| 284 ScopedGroupPolicyRegistrySandbox::ScopedGroupPolicyRegistrySandbox() { | 366 ScopedGroupPolicyRegistrySandbox::ScopedGroupPolicyRegistrySandbox() { |
| 285 // Generate a unique registry key for the override for each test. This | 367 // Generate a unique registry key for the override for each test. This |
| 286 // makes sure that tests executing in parallel won't delete each other's | 368 // makes sure that tests executing in parallel won't delete each other's |
| 287 // key, at DeleteKeys(). | 369 // key, at DeleteKeys(). |
| 288 key_name_ = ASCIIToWide(base::StringPrintf( | 370 key_name_ = ASCIIToWide(base::StringPrintf( |
| 289 "SOFTWARE\\chromium unittest %d", | 371 "SOFTWARE\\chromium unittest %d", |
| 290 base::Process::Current().pid())); | 372 base::Process::Current().pid())); |
| 291 std::wstring hklm_key_name = key_name_ + L"\\HKLM"; | 373 std::wstring hklm_key_name = key_name_ + L"\\HKLM"; |
| (...skipping 27 matching lines...) Expand all Loading... |
| 319 ASSERT_HRESULT_SUCCEEDED(RegOverridePredefKey(HKEY_LOCAL_MACHINE, 0)); | 401 ASSERT_HRESULT_SUCCEEDED(RegOverridePredefKey(HKEY_LOCAL_MACHINE, 0)); |
| 320 ASSERT_HRESULT_SUCCEEDED(RegOverridePredefKey(HKEY_CURRENT_USER, 0)); | 402 ASSERT_HRESULT_SUCCEEDED(RegOverridePredefKey(HKEY_CURRENT_USER, 0)); |
| 321 } | 403 } |
| 322 | 404 |
| 323 void ScopedGroupPolicyRegistrySandbox::DeleteKeys() { | 405 void ScopedGroupPolicyRegistrySandbox::DeleteKeys() { |
| 324 RegKey key(HKEY_CURRENT_USER, key_name_.c_str(), KEY_ALL_ACCESS); | 406 RegKey key(HKEY_CURRENT_USER, key_name_.c_str(), KEY_ALL_ACCESS); |
| 325 ASSERT_TRUE(key.Valid()); | 407 ASSERT_TRUE(key.Valid()); |
| 326 key.DeleteKey(L""); | 408 key.DeleteKey(L""); |
| 327 } | 409 } |
| 328 | 410 |
| 329 TestHarness::TestHarness(HKEY hive, PolicyScope scope) | 411 RegistryTestHarness::RegistryTestHarness(HKEY hive, PolicyScope scope) |
| 330 : PolicyProviderTestHarness(POLICY_LEVEL_MANDATORY, scope), hive_(hive) {} | 412 : PolicyProviderTestHarness(POLICY_LEVEL_MANDATORY, scope), hive_(hive) {} |
| 331 | 413 |
| 332 TestHarness::~TestHarness() {} | 414 RegistryTestHarness::~RegistryTestHarness() {} |
| 333 | 415 |
| 334 void TestHarness::SetUp() {} | 416 void RegistryTestHarness::SetUp() {} |
| 335 | 417 |
| 336 DWORD TestHarness::GetAppliedGPOList(DWORD flags, | 418 ConfigurationPolicyProvider* RegistryTestHarness::CreateProvider( |
| 337 LPCTSTR machine_name, | |
| 338 PSID sid_user, | |
| 339 GUID* extension_guid, | |
| 340 PGROUP_POLICY_OBJECT* gpo_list) { | |
| 341 *gpo_list = NULL; | |
| 342 return ERROR_ACCESS_DENIED; | |
| 343 } | |
| 344 | |
| 345 BOOL TestHarness::FreeGPOList(PGROUP_POLICY_OBJECT gpo_list) { | |
| 346 return TRUE; | |
| 347 } | |
| 348 | |
| 349 ConfigurationPolicyProvider* TestHarness::CreateProvider( | |
| 350 const PolicyDefinitionList* policy_list) { | 419 const PolicyDefinitionList* policy_list) { |
| 351 scoped_ptr<AsyncPolicyLoader> loader( | 420 scoped_ptr<AsyncPolicyLoader> loader( |
| 352 new PolicyLoaderWin(policy_list, kRegistryChromePolicyKey, this)); | 421 new PolicyLoaderWin(policy_list, kRegistryChromePolicyKey, this)); |
| 353 return new AsyncPolicyProvider(loader.Pass()); | 422 return new AsyncPolicyProvider(loader.Pass()); |
| 354 } | 423 } |
| 355 | 424 |
| 356 void TestHarness::InstallEmptyPolicy() {} | 425 void RegistryTestHarness::InstallEmptyPolicy() {} |
| 357 | 426 |
| 358 void TestHarness::InstallStringPolicy(const std::string& policy_name, | 427 void RegistryTestHarness::InstallStringPolicy( |
| 359 const std::string& policy_value) { | 428 const std::string& policy_name, |
| 429 const std::string& policy_value) { |
| 360 RegKey key(hive_, kRegistryChromePolicyKey, KEY_ALL_ACCESS); | 430 RegKey key(hive_, kRegistryChromePolicyKey, KEY_ALL_ACCESS); |
| 361 ASSERT_TRUE(key.Valid()); | 431 ASSERT_TRUE(key.Valid()); |
| 362 ASSERT_HRESULT_SUCCEEDED(key.WriteValue(UTF8ToUTF16(policy_name).c_str(), | 432 ASSERT_HRESULT_SUCCEEDED(key.WriteValue(UTF8ToUTF16(policy_name).c_str(), |
| 363 UTF8ToUTF16(policy_value).c_str())); | 433 UTF8ToUTF16(policy_value).c_str())); |
| 364 } | 434 } |
| 365 | 435 |
| 366 void TestHarness::InstallIntegerPolicy(const std::string& policy_name, | 436 void RegistryTestHarness::InstallIntegerPolicy( |
| 367 int policy_value) { | 437 const std::string& policy_name, |
| 438 int policy_value) { |
| 368 RegKey key(hive_, kRegistryChromePolicyKey, KEY_ALL_ACCESS); | 439 RegKey key(hive_, kRegistryChromePolicyKey, KEY_ALL_ACCESS); |
| 369 ASSERT_TRUE(key.Valid()); | 440 ASSERT_TRUE(key.Valid()); |
| 370 key.WriteValue(UTF8ToUTF16(policy_name).c_str(), | 441 key.WriteValue(UTF8ToUTF16(policy_name).c_str(), |
| 371 static_cast<DWORD>(policy_value)); | 442 static_cast<DWORD>(policy_value)); |
| 372 } | 443 } |
| 373 | 444 |
| 374 void TestHarness::InstallBooleanPolicy(const std::string& policy_name, | 445 void RegistryTestHarness::InstallBooleanPolicy( |
| 375 bool policy_value) { | 446 const std::string& policy_name, |
| 447 bool policy_value) { |
| 376 RegKey key(hive_, kRegistryChromePolicyKey, KEY_ALL_ACCESS); | 448 RegKey key(hive_, kRegistryChromePolicyKey, KEY_ALL_ACCESS); |
| 377 ASSERT_TRUE(key.Valid()); | 449 ASSERT_TRUE(key.Valid()); |
| 378 key.WriteValue(UTF8ToUTF16(policy_name).c_str(), | 450 key.WriteValue(UTF8ToUTF16(policy_name).c_str(), |
| 379 static_cast<DWORD>(policy_value)); | 451 static_cast<DWORD>(policy_value)); |
| 380 } | 452 } |
| 381 | 453 |
| 382 void TestHarness::InstallStringListPolicy(const std::string& policy_name, | 454 void RegistryTestHarness::InstallStringListPolicy( |
| 383 const base::ListValue* policy_value) { | 455 const std::string& policy_name, |
| 456 const base::ListValue* policy_value) { |
| 384 RegKey key(hive_, | 457 RegKey key(hive_, |
| 385 (string16(kRegistryChromePolicyKey) + ASCIIToUTF16("\\") + | 458 (string16(kRegistryChromePolicyKey) + ASCIIToUTF16("\\") + |
| 386 UTF8ToUTF16(policy_name)).c_str(), | 459 UTF8ToUTF16(policy_name)).c_str(), |
| 387 KEY_ALL_ACCESS); | 460 KEY_ALL_ACCESS); |
| 388 ASSERT_TRUE(key.Valid()); | 461 ASSERT_TRUE(key.Valid()); |
| 389 int index = 1; | 462 int index = 1; |
| 390 for (base::ListValue::const_iterator element(policy_value->begin()); | 463 for (base::ListValue::const_iterator element(policy_value->begin()); |
| 391 element != policy_value->end(); | 464 element != policy_value->end(); |
| 392 ++element) { | 465 ++element) { |
| 393 std::string element_value; | 466 std::string element_value; |
| 394 if (!(*element)->GetAsString(&element_value)) | 467 if (!(*element)->GetAsString(&element_value)) |
| 395 continue; | 468 continue; |
| 396 std::string name(base::IntToString(index++)); | 469 std::string name(base::IntToString(index++)); |
| 397 key.WriteValue(UTF8ToUTF16(name).c_str(), | 470 key.WriteValue(UTF8ToUTF16(name).c_str(), |
| 398 UTF8ToUTF16(element_value).c_str()); | 471 UTF8ToUTF16(element_value).c_str()); |
| 399 } | 472 } |
| 400 } | 473 } |
| 401 | 474 |
| 402 void TestHarness::InstallDictionaryPolicy( | 475 void RegistryTestHarness::InstallDictionaryPolicy( |
| 403 const std::string& policy_name, | 476 const std::string& policy_name, |
| 404 const base::DictionaryValue* policy_value) { | 477 const base::DictionaryValue* policy_value) { |
| 405 std::string json; | 478 std::string json; |
| 406 base::JSONWriter::Write(policy_value, &json); | 479 base::JSONWriter::Write(policy_value, &json); |
| 407 RegKey key(hive_, kRegistryChromePolicyKey, KEY_ALL_ACCESS); | 480 RegKey key(hive_, kRegistryChromePolicyKey, KEY_ALL_ACCESS); |
| 408 ASSERT_TRUE(key.Valid()); | 481 ASSERT_TRUE(key.Valid()); |
| 409 key.WriteValue(UTF8ToUTF16(policy_name).c_str(), | 482 key.WriteValue(UTF8ToUTF16(policy_name).c_str(), |
| 410 UTF8ToUTF16(json).c_str()); | 483 UTF8ToUTF16(json).c_str()); |
| 411 } | 484 } |
| 412 | 485 |
| 413 void TestHarness::Install3rdPartyPolicy(const base::DictionaryValue* policies) { | 486 void RegistryTestHarness::Install3rdPartyPolicy( |
| 487 const base::DictionaryValue* policies) { |
| 414 // The first level entries are domains, and the second level entries map | 488 // The first level entries are domains, and the second level entries map |
| 415 // components to their policy. | 489 // components to their policy. |
| 416 const string16 kPathPrefix = string16(kRegistryChromePolicyKey) + kPathSep + | 490 const string16 kPathPrefix = string16(kRegistryChromePolicyKey) + kPathSep + |
| 417 kThirdParty + kPathSep; | 491 kThirdParty + kPathSep; |
| 418 for (base::DictionaryValue::Iterator domain(*policies); | 492 for (base::DictionaryValue::Iterator domain(*policies); |
| 419 !domain.IsAtEnd(); domain.Advance()) { | 493 !domain.IsAtEnd(); domain.Advance()) { |
| 420 const base::DictionaryValue* components = NULL; | 494 const base::DictionaryValue* components = NULL; |
| 421 if (!domain.value().GetAsDictionary(&components)) { | 495 if (!domain.value().GetAsDictionary(&components)) { |
| 422 ADD_FAILURE(); | 496 ADD_FAILURE(); |
| 423 continue; | 497 continue; |
| 424 } | 498 } |
| 425 for (base::DictionaryValue::Iterator component(*components); | 499 for (base::DictionaryValue::Iterator component(*components); |
| 426 !component.IsAtEnd(); component.Advance()) { | 500 !component.IsAtEnd(); component.Advance()) { |
| 427 const string16 path = string16(kRegistryChromePolicyKey) + kPathSep + | 501 const string16 path = kPathPrefix + |
| 428 kThirdParty + kPathSep + | |
| 429 UTF8ToUTF16(domain.key()) + kPathSep + | 502 UTF8ToUTF16(domain.key()) + kPathSep + |
| 430 UTF8ToUTF16(component.key()); | 503 UTF8ToUTF16(component.key()); |
| 431 InstallValue(component.value(), hive_, path, kMandatory); | 504 InstallValue(component.value(), hive_, path, kMandatory); |
| 432 EXPECT_TRUE(InstallSchema(component.value(), hive_, path, kSchema)); | 505 EXPECT_TRUE(InstallSchema(component.value(), hive_, path, kSchema)); |
| 433 } | 506 } |
| 434 } | 507 } |
| 435 } | 508 } |
| 436 | 509 |
| 437 // static | 510 DWORD RegistryTestHarness::GetAppliedGPOList(DWORD flags, |
| 438 PolicyProviderTestHarness* TestHarness::CreateHKCU() { | 511 LPCTSTR machine_name, |
| 439 return new TestHarness(HKEY_CURRENT_USER, POLICY_SCOPE_USER); | 512 PSID sid_user, |
| 440 } | 513 GUID* extension_guid, |
| 441 | 514 PGROUP_POLICY_OBJECT* gpo_list) { |
| 442 // static | 515 *gpo_list = NULL; |
| 443 PolicyProviderTestHarness* TestHarness::CreateHKLM() { | 516 return ERROR_ACCESS_DENIED; |
| 444 return new TestHarness(HKEY_LOCAL_MACHINE, POLICY_SCOPE_MACHINE); | 517 } |
| 518 |
| 519 BOOL RegistryTestHarness::FreeGPOList(PGROUP_POLICY_OBJECT gpo_list) { |
| 520 return TRUE; |
| 521 } |
| 522 |
| 523 // static |
| 524 PolicyProviderTestHarness* RegistryTestHarness::CreateHKCU() { |
| 525 return new RegistryTestHarness(HKEY_CURRENT_USER, POLICY_SCOPE_USER); |
| 526 } |
| 527 |
| 528 // static |
| 529 PolicyProviderTestHarness* RegistryTestHarness::CreateHKLM() { |
| 530 return new RegistryTestHarness(HKEY_LOCAL_MACHINE, POLICY_SCOPE_MACHINE); |
| 531 } |
| 532 |
| 533 PRegTestHarness::PRegTestHarness() |
| 534 : PolicyProviderTestHarness(POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE) {} |
| 535 |
| 536 PRegTestHarness::~PRegTestHarness() {} |
| 537 |
| 538 void PRegTestHarness::SetUp() { |
| 539 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 540 preg_file_path_ = temp_dir_.path().Append(PolicyLoaderWin::kPRegFileName); |
| 541 ASSERT_TRUE(file_util::WriteFile(preg_file_path_, |
| 542 preg_parser::kPRegFileHeader, |
| 543 arraysize(preg_parser::kPRegFileHeader))); |
| 544 |
| 545 memset(&gpo_, 0, sizeof(GROUP_POLICY_OBJECT)); |
| 546 gpo_.lpFileSysPath = const_cast<wchar_t*>(temp_dir_.path().value().c_str()); |
| 547 } |
| 548 |
| 549 ConfigurationPolicyProvider* PRegTestHarness::CreateProvider( |
| 550 const PolicyDefinitionList* policy_list) { |
| 551 scoped_ptr<AsyncPolicyLoader> loader( |
| 552 new PolicyLoaderWin(policy_list, kRegistryChromePolicyKey, this)); |
| 553 return new AsyncPolicyProvider(loader.Pass()); |
| 554 } |
| 555 |
| 556 void PRegTestHarness::InstallEmptyPolicy() {} |
| 557 |
| 558 void PRegTestHarness::InstallStringPolicy(const std::string& policy_name, |
| 559 const std::string& policy_value) { |
| 560 AppendStringToPRegFile(kRegistryChromePolicyKey, policy_name, policy_value); |
| 561 } |
| 562 |
| 563 void PRegTestHarness::InstallIntegerPolicy(const std::string& policy_name, |
| 564 int policy_value) { |
| 565 AppendDWORDToPRegFile(kRegistryChromePolicyKey, policy_name, policy_value); |
| 566 } |
| 567 |
| 568 void PRegTestHarness::InstallBooleanPolicy(const std::string& policy_name, |
| 569 bool policy_value) { |
| 570 AppendDWORDToPRegFile(kRegistryChromePolicyKey, policy_name, policy_value); |
| 571 } |
| 572 |
| 573 void PRegTestHarness::InstallStringListPolicy( |
| 574 const std::string& policy_name, |
| 575 const base::ListValue* policy_value) { |
| 576 AppendPolicyToPRegFile(kRegistryChromePolicyKey, policy_name, policy_value); |
| 577 } |
| 578 |
| 579 void PRegTestHarness::InstallDictionaryPolicy( |
| 580 const std::string& policy_name, |
| 581 const base::DictionaryValue* policy_value) { |
| 582 std::string json; |
| 583 base::JSONWriter::Write(policy_value, &json); |
| 584 AppendStringToPRegFile(kRegistryChromePolicyKey, policy_name, json); |
| 585 } |
| 586 |
| 587 void PRegTestHarness::Install3rdPartyPolicy( |
| 588 const base::DictionaryValue* policies) { |
| 589 // The first level entries are domains, and the second level entries map |
| 590 // components to their policy. |
| 591 const string16 kPathPrefix = string16(kRegistryChromePolicyKey) + kPathSep + |
| 592 kThirdParty + kPathSep; |
| 593 for (base::DictionaryValue::Iterator domain(*policies); |
| 594 !domain.IsAtEnd(); domain.Advance()) { |
| 595 const base::DictionaryValue* components = NULL; |
| 596 if (!domain.value().GetAsDictionary(&components)) { |
| 597 ADD_FAILURE(); |
| 598 continue; |
| 599 } |
| 600 const string16 domain_path = kPathPrefix + UTF8ToUTF16(domain.key()); |
| 601 for (base::DictionaryValue::Iterator component(*components); |
| 602 !component.IsAtEnd(); component.Advance()) { |
| 603 const string16 component_path = |
| 604 domain_path + kPathSep + UTF8ToUTF16(component.key()); |
| 605 AppendPolicyToPRegFile(component_path, UTF16ToUTF8(kMandatory), |
| 606 &component.value()); |
| 607 |
| 608 scoped_ptr<base::DictionaryValue> schema_dict( |
| 609 BuildSchema(component.value())); |
| 610 std::string schema_json; |
| 611 base::JSONWriter::Write(schema_dict.get(), &schema_json); |
| 612 if (!schema_json.empty()) { |
| 613 AppendStringToPRegFile(component_path, UTF16ToUTF8(kSchema), |
| 614 schema_json); |
| 615 } |
| 616 } |
| 617 } |
| 618 } |
| 619 |
| 620 DWORD PRegTestHarness::GetAppliedGPOList(DWORD flags, |
| 621 LPCTSTR machine_name, |
| 622 PSID sid_user, |
| 623 GUID* extension_guid, |
| 624 PGROUP_POLICY_OBJECT* gpo_list) { |
| 625 *gpo_list = flags & GPO_LIST_FLAG_MACHINE ? &gpo_ : NULL; |
| 626 return ERROR_SUCCESS; |
| 627 } |
| 628 |
| 629 BOOL PRegTestHarness::FreeGPOList(PGROUP_POLICY_OBJECT gpo_list) { |
| 630 return TRUE; |
| 631 } |
| 632 |
| 633 // static |
| 634 PolicyProviderTestHarness* PRegTestHarness::Create() { |
| 635 return new PRegTestHarness(); |
| 636 } |
| 637 |
| 638 // static |
| 639 void PRegTestHarness::AppendChars(std::vector<uint8>* buffer, |
| 640 const string16& chars) { |
| 641 for (string16::const_iterator c(chars.begin()); c != chars.end(); ++c) { |
| 642 buffer->push_back(*c & 0xff); |
| 643 buffer->push_back((*c >> 8) & 0xff); |
| 644 } |
| 645 } |
| 646 |
| 647 void PRegTestHarness::AppendRecordToPRegFile(const string16& path, |
| 648 const std::string& key, |
| 649 DWORD type, |
| 650 DWORD size, |
| 651 uint8* data) { |
| 652 std::vector<uint8> buffer; |
| 653 AppendChars(&buffer, L"["); |
| 654 AppendChars(&buffer, path); |
| 655 AppendChars(&buffer, string16(L"\0;", 2)); |
| 656 AppendChars(&buffer, UTF8ToUTF16(key)); |
| 657 AppendChars(&buffer, string16(L"\0;", 2)); |
| 658 type = base::ByteSwapToLE32(type); |
| 659 uint8* type_data = reinterpret_cast<uint8*>(&type); |
| 660 buffer.insert(buffer.end(), type_data, type_data + sizeof(DWORD)); |
| 661 AppendChars(&buffer, L";"); |
| 662 size = base::ByteSwapToLE32(size); |
| 663 uint8* size_data = reinterpret_cast<uint8*>(&size); |
| 664 buffer.insert(buffer.end(), size_data, size_data + sizeof(DWORD)); |
| 665 AppendChars(&buffer, L";"); |
| 666 buffer.insert(buffer.end(), data, data + size); |
| 667 AppendChars(&buffer, L"]"); |
| 668 |
| 669 ASSERT_EQ(buffer.size(), |
| 670 file_util::AppendToFile( |
| 671 preg_file_path_, |
| 672 reinterpret_cast<const char*>(vector_as_array(&buffer)), |
| 673 buffer.size())); |
| 674 } |
| 675 |
| 676 void PRegTestHarness::AppendDWORDToPRegFile(const string16& path, |
| 677 const std::string& key, |
| 678 DWORD value) { |
| 679 value = base::ByteSwapToLE32(value); |
| 680 AppendRecordToPRegFile(path, key, REG_DWORD, sizeof(DWORD), |
| 681 reinterpret_cast<uint8*>(&value)); |
| 682 } |
| 683 |
| 684 void PRegTestHarness::AppendStringToPRegFile(const string16& path, |
| 685 const std::string& key, |
| 686 const std::string& value) { |
| 687 string16 string16_value(UTF8ToUTF16(value)); |
| 688 std::vector<char16> data; |
| 689 std::transform(string16_value.begin(), string16_value.end(), |
| 690 std::back_inserter(data), std::ptr_fun(base::ByteSwapToLE16)); |
| 691 data.push_back(base::ByteSwapToLE16(L'\0')); |
| 692 |
| 693 AppendRecordToPRegFile(path, key, REG_SZ, data.size() * sizeof(char16), |
| 694 reinterpret_cast<uint8*>(vector_as_array(&data))); |
| 695 } |
| 696 |
| 697 void PRegTestHarness::AppendPolicyToPRegFile(const string16& path, |
| 698 const std::string& key, |
| 699 const base::Value* value) { |
| 700 switch (value->GetType()) { |
| 701 case base::Value::TYPE_BOOLEAN: { |
| 702 bool boolean_value = false; |
| 703 ASSERT_TRUE(value->GetAsBoolean(&boolean_value)); |
| 704 AppendDWORDToPRegFile(path, key, boolean_value); |
| 705 break; |
| 706 } |
| 707 case base::Value::TYPE_INTEGER: { |
| 708 int int_value = 0; |
| 709 ASSERT_TRUE(value->GetAsInteger(&int_value)); |
| 710 AppendDWORDToPRegFile(path, key, int_value); |
| 711 break; |
| 712 } |
| 713 case base::Value::TYPE_DOUBLE: { |
| 714 double double_value = 0; |
| 715 std::string string_value; |
| 716 ASSERT_TRUE(value->GetAsDouble(&double_value)); |
| 717 AppendStringToPRegFile(path, key, base::DoubleToString(double_value)); |
| 718 break; |
| 719 } |
| 720 case base::Value::TYPE_STRING: { |
| 721 std::string string_value; |
| 722 ASSERT_TRUE(value->GetAsString(&string_value)); |
| 723 AppendStringToPRegFile(path, key, string_value); |
| 724 break; |
| 725 } |
| 726 case base::Value::TYPE_DICTIONARY: { |
| 727 string16 subpath = path + kPathSep + UTF8ToUTF16(key); |
| 728 const base::DictionaryValue* dict = NULL; |
| 729 ASSERT_TRUE(value->GetAsDictionary(&dict)); |
| 730 for (base::DictionaryValue::Iterator entry(*dict); !entry.IsAtEnd(); |
| 731 entry.Advance()) { |
| 732 AppendPolicyToPRegFile(subpath, entry.key(), &entry.value()); |
| 733 } |
| 734 break; |
| 735 } |
| 736 case base::Value::TYPE_LIST: { |
| 737 string16 subpath = path + kPathSep + UTF8ToUTF16(key); |
| 738 const base::ListValue* list = NULL; |
| 739 ASSERT_TRUE(value->GetAsList(&list)); |
| 740 for (size_t i = 0; i < list->GetSize(); ++i) { |
| 741 const base::Value* entry = NULL; |
| 742 ASSERT_TRUE(list->Get(i, &entry)); |
| 743 AppendPolicyToPRegFile(subpath, base::IntToString(i + 1), entry); |
| 744 } |
| 745 break; |
| 746 } |
| 747 case base::Value::TYPE_BINARY: |
| 748 case base::Value::TYPE_NULL: { |
| 749 ADD_FAILURE(); |
| 750 break; |
| 751 } |
| 752 } |
| 445 } | 753 } |
| 446 | 754 |
| 447 } // namespace | 755 } // namespace |
| 448 | 756 |
| 449 // Instantiate abstract test case for basic policy reading tests. | 757 // Instantiate abstract test case for basic policy reading tests. |
| 450 INSTANTIATE_TEST_CASE_P( | 758 INSTANTIATE_TEST_CASE_P( |
| 451 PolicyProviderWinTest, | 759 PolicyProviderWinTest, |
| 452 ConfigurationPolicyProviderTest, | 760 ConfigurationPolicyProviderTest, |
| 453 testing::Values(TestHarness::CreateHKCU, TestHarness::CreateHKLM)); | 761 testing::Values(RegistryTestHarness::CreateHKCU, |
| 762 RegistryTestHarness::CreateHKLM, |
| 763 PRegTestHarness::Create)); |
| 454 | 764 |
| 455 // Instantiate abstract test case for 3rd party policy reading tests. | 765 // Instantiate abstract test case for 3rd party policy reading tests. |
| 456 INSTANTIATE_TEST_CASE_P( | 766 INSTANTIATE_TEST_CASE_P( |
| 457 ThirdPartyPolicyProviderWinTest, | 767 ThirdPartyPolicyProviderWinTest, |
| 458 Configuration3rdPartyPolicyProviderTest, | 768 Configuration3rdPartyPolicyProviderTest, |
| 459 testing::Values(TestHarness::CreateHKCU, TestHarness::CreateHKLM)); | 769 testing::Values(RegistryTestHarness::CreateHKCU, |
| 770 RegistryTestHarness::CreateHKLM, |
| 771 PRegTestHarness::Create)); |
| 460 | 772 |
| 461 // Test cases for windows policy provider specific functionality. | 773 // Test cases for windows policy provider specific functionality. |
| 462 class PolicyLoaderWinTest : public PolicyTestBase, | 774 class PolicyLoaderWinTest : public PolicyTestBase, |
| 463 public AppliedGPOListProvider { | 775 public AppliedGPOListProvider { |
| 464 protected: | 776 protected: |
| 465 // The policy key this tests places data under. This must match the data | 777 // The policy key this tests places data under. This must match the data |
| 466 // files in chrome/test/data/policy/gpo. | 778 // files in chrome/test/data/policy/gpo. |
| 467 static const char16 kTestPolicyKey[]; | 779 static const char16 kTestPolicyKey[]; |
| 468 | 780 |
| 469 PolicyLoaderWinTest() | 781 PolicyLoaderWinTest() |
| (...skipping 381 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 851 GROUP_POLICY_OBJECT gpo2; | 1163 GROUP_POLICY_OBJECT gpo2; |
| 852 InitGPO(&gpo1, 0, gpo_dir, &gpo2, NULL); | 1164 InitGPO(&gpo1, 0, gpo_dir, &gpo2, NULL); |
| 853 InitGPO(&gpo2, 0, unc_path, NULL, &gpo1); | 1165 InitGPO(&gpo2, 0, unc_path, NULL, &gpo1); |
| 854 gpo_list_ = &gpo1; | 1166 gpo_list_ = &gpo1; |
| 855 gpo_list_status_ = ERROR_SUCCESS; | 1167 gpo_list_status_ = ERROR_SUCCESS; |
| 856 | 1168 |
| 857 EXPECT_TRUE(MatchesRegistrySentinel()); | 1169 EXPECT_TRUE(MatchesRegistrySentinel()); |
| 858 } | 1170 } |
| 859 | 1171 |
| 860 } // namespace policy | 1172 } // namespace policy |
| OLD | NEW |