Chromium Code Reviews| 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 <windows.h> | 8 #include <windows.h> |
| 8 | 9 |
| 10 #include <cstring> | |
| 11 | |
| 12 #include "base/file_util.h" | |
| 13 #include "base/files/file_path.h" | |
| 9 #include "base/json/json_writer.h" | 14 #include "base/json/json_writer.h" |
| 15 #include "base/path_service.h" | |
| 10 #include "base/process.h" | 16 #include "base/process.h" |
| 11 #include "base/string16.h" | 17 #include "base/string16.h" |
| 12 #include "base/string_util.h" | 18 #include "base/string_util.h" |
| 13 #include "base/stringprintf.h" | 19 #include "base/stringprintf.h" |
| 14 #include "base/strings/string_number_conversions.h" | 20 #include "base/strings/string_number_conversions.h" |
| 15 #include "base/utf_string_conversions.h" | 21 #include "base/utf_string_conversions.h" |
| 16 #include "base/win/registry.h" | 22 #include "base/win/registry.h" |
| 17 #include "chrome/browser/policy/async_policy_provider.h" | 23 #include "chrome/browser/policy/async_policy_provider.h" |
| 18 #include "chrome/browser/policy/configuration_policy_provider_test.h" | 24 #include "chrome/browser/policy/configuration_policy_provider_test.h" |
| 19 #include "chrome/browser/policy/policy_bundle.h" | 25 #include "chrome/browser/policy/policy_bundle.h" |
| 20 #include "chrome/browser/policy/policy_map.h" | 26 #include "chrome/browser/policy/policy_map.h" |
| 27 #include "chrome/common/chrome_paths.h" | |
| 21 #include "chrome/common/json_schema/json_schema_constants.h" | 28 #include "chrome/common/json_schema/json_schema_constants.h" |
| 22 #include "policy/policy_constants.h" | 29 #include "policy/policy_constants.h" |
| 23 #include "testing/gtest/include/gtest/gtest.h" | 30 #include "testing/gtest/include/gtest/gtest.h" |
| 24 | 31 |
| 25 namespace schema = json_schema_constants; | 32 namespace schema = json_schema_constants; |
| 26 | 33 |
| 27 using base::win::RegKey; | 34 using base::win::RegKey; |
| 28 using namespace policy::registry_constants; | |
| 29 | 35 |
| 30 namespace policy { | 36 namespace policy { |
| 31 | 37 |
| 32 namespace { | 38 namespace { |
| 33 | 39 |
| 40 // Constants for registry key names. | |
| 41 const wchar_t kPathSep[] = L"\\"; | |
| 42 const wchar_t kThirdParty[] = L"3rdparty"; | |
| 43 const wchar_t kMandatory[] = L"policy"; | |
| 44 const wchar_t kRecommended[] = L"recommended"; | |
| 45 const wchar_t kSchema[] = L"schema"; | |
| 46 | |
| 34 // Installs |value| in the given registry |path| and |hive|, under the key | 47 // Installs |value| in the given registry |path| and |hive|, under the key |
| 35 // |name|. Returns false on errors. | 48 // |name|. Returns false on errors. |
| 36 // Some of the possible Value types are stored after a conversion (e.g. doubles | 49 // Some of the possible Value types are stored after a conversion (e.g. doubles |
| 37 // are stored as strings), and can only be retrieved if a corresponding schema | 50 // are stored as strings), and can only be retrieved if a corresponding schema |
| 38 // is written. | 51 // is written. |
| 39 bool InstallValue(const base::Value& value, | 52 bool InstallValue(const base::Value& value, |
| 40 HKEY hive, | 53 HKEY hive, |
| 41 const string16& path, | 54 const string16& path, |
| 42 const string16& name) { | 55 const string16& name) { |
| 43 // KEY_ALL_ACCESS causes the ctor to create the key if it does not exist yet. | 56 // KEY_ALL_ACCESS causes the ctor to create the key if it does not exist yet. |
| (...skipping 261 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 305 | 318 |
| 306 TestHarness::TestHarness(HKEY hive, PolicyScope scope) | 319 TestHarness::TestHarness(HKEY hive, PolicyScope scope) |
| 307 : PolicyProviderTestHarness(POLICY_LEVEL_MANDATORY, scope), hive_(hive) {} | 320 : PolicyProviderTestHarness(POLICY_LEVEL_MANDATORY, scope), hive_(hive) {} |
| 308 | 321 |
| 309 TestHarness::~TestHarness() {} | 322 TestHarness::~TestHarness() {} |
| 310 | 323 |
| 311 void TestHarness::SetUp() {} | 324 void TestHarness::SetUp() {} |
| 312 | 325 |
| 313 ConfigurationPolicyProvider* TestHarness::CreateProvider( | 326 ConfigurationPolicyProvider* TestHarness::CreateProvider( |
| 314 const PolicyDefinitionList* policy_list) { | 327 const PolicyDefinitionList* policy_list) { |
| 315 scoped_ptr<AsyncPolicyLoader> loader(new PolicyLoaderWin(policy_list)); | 328 scoped_ptr<AsyncPolicyLoader> loader(PolicyLoaderWin::Create(policy_list)); |
| 316 return new AsyncPolicyProvider(loader.Pass()); | 329 return new AsyncPolicyProvider(loader.Pass()); |
| 317 } | 330 } |
| 318 | 331 |
| 319 void TestHarness::InstallEmptyPolicy() {} | 332 void TestHarness::InstallEmptyPolicy() {} |
| 320 | 333 |
| 321 void TestHarness::InstallStringPolicy(const std::string& policy_name, | 334 void TestHarness::InstallStringPolicy(const std::string& policy_name, |
| 322 const std::string& policy_value) { | 335 const std::string& policy_value) { |
| 323 RegKey key(hive_, kRegistryMandatorySubKey, KEY_ALL_ACCESS); | 336 RegKey key(hive_, kRegistryChromePolicyKey, KEY_ALL_ACCESS); |
| 324 ASSERT_TRUE(key.Valid()); | 337 ASSERT_TRUE(key.Valid()); |
| 325 ASSERT_HRESULT_SUCCEEDED(key.WriteValue(UTF8ToUTF16(policy_name).c_str(), | 338 ASSERT_HRESULT_SUCCEEDED(key.WriteValue(UTF8ToUTF16(policy_name).c_str(), |
| 326 UTF8ToUTF16(policy_value).c_str())); | 339 UTF8ToUTF16(policy_value).c_str())); |
| 327 } | 340 } |
| 328 | 341 |
| 329 void TestHarness::InstallIntegerPolicy(const std::string& policy_name, | 342 void TestHarness::InstallIntegerPolicy(const std::string& policy_name, |
| 330 int policy_value) { | 343 int policy_value) { |
| 331 RegKey key(hive_, kRegistryMandatorySubKey, KEY_ALL_ACCESS); | 344 RegKey key(hive_, kRegistryChromePolicyKey, KEY_ALL_ACCESS); |
| 332 ASSERT_TRUE(key.Valid()); | 345 ASSERT_TRUE(key.Valid()); |
| 333 key.WriteValue(UTF8ToUTF16(policy_name).c_str(), | 346 key.WriteValue(UTF8ToUTF16(policy_name).c_str(), |
| 334 static_cast<DWORD>(policy_value)); | 347 static_cast<DWORD>(policy_value)); |
| 335 } | 348 } |
| 336 | 349 |
| 337 void TestHarness::InstallBooleanPolicy(const std::string& policy_name, | 350 void TestHarness::InstallBooleanPolicy(const std::string& policy_name, |
| 338 bool policy_value) { | 351 bool policy_value) { |
| 339 RegKey key(hive_, kRegistryMandatorySubKey, KEY_ALL_ACCESS); | 352 RegKey key(hive_, kRegistryChromePolicyKey, KEY_ALL_ACCESS); |
| 340 ASSERT_TRUE(key.Valid()); | 353 ASSERT_TRUE(key.Valid()); |
| 341 key.WriteValue(UTF8ToUTF16(policy_name).c_str(), | 354 key.WriteValue(UTF8ToUTF16(policy_name).c_str(), |
| 342 static_cast<DWORD>(policy_value)); | 355 static_cast<DWORD>(policy_value)); |
| 343 } | 356 } |
| 344 | 357 |
| 345 void TestHarness::InstallStringListPolicy(const std::string& policy_name, | 358 void TestHarness::InstallStringListPolicy(const std::string& policy_name, |
| 346 const base::ListValue* policy_value) { | 359 const base::ListValue* policy_value) { |
| 347 RegKey key(hive_, | 360 RegKey key(hive_, |
| 348 (string16(kRegistryMandatorySubKey) + ASCIIToUTF16("\\") + | 361 (string16(kRegistryChromePolicyKey) + ASCIIToUTF16("\\") + |
| 349 UTF8ToUTF16(policy_name)).c_str(), | 362 UTF8ToUTF16(policy_name)).c_str(), |
| 350 KEY_ALL_ACCESS); | 363 KEY_ALL_ACCESS); |
| 351 ASSERT_TRUE(key.Valid()); | 364 ASSERT_TRUE(key.Valid()); |
| 352 int index = 1; | 365 int index = 1; |
| 353 for (base::ListValue::const_iterator element(policy_value->begin()); | 366 for (base::ListValue::const_iterator element(policy_value->begin()); |
| 354 element != policy_value->end(); | 367 element != policy_value->end(); |
| 355 ++element) { | 368 ++element) { |
| 356 std::string element_value; | 369 std::string element_value; |
| 357 if (!(*element)->GetAsString(&element_value)) | 370 if (!(*element)->GetAsString(&element_value)) |
| 358 continue; | 371 continue; |
| 359 std::string name(base::IntToString(index++)); | 372 std::string name(base::IntToString(index++)); |
| 360 key.WriteValue(UTF8ToUTF16(name).c_str(), | 373 key.WriteValue(UTF8ToUTF16(name).c_str(), |
| 361 UTF8ToUTF16(element_value).c_str()); | 374 UTF8ToUTF16(element_value).c_str()); |
| 362 } | 375 } |
| 363 } | 376 } |
| 364 | 377 |
| 365 void TestHarness::InstallDictionaryPolicy( | 378 void TestHarness::InstallDictionaryPolicy( |
| 366 const std::string& policy_name, | 379 const std::string& policy_name, |
| 367 const base::DictionaryValue* policy_value) { | 380 const base::DictionaryValue* policy_value) { |
| 368 std::string json; | 381 std::string json; |
| 369 base::JSONWriter::Write(policy_value, &json); | 382 base::JSONWriter::Write(policy_value, &json); |
| 370 RegKey key(hive_, kRegistryMandatorySubKey, KEY_ALL_ACCESS); | 383 RegKey key(hive_, kRegistryChromePolicyKey, KEY_ALL_ACCESS); |
| 371 ASSERT_TRUE(key.Valid()); | 384 ASSERT_TRUE(key.Valid()); |
| 372 key.WriteValue(UTF8ToUTF16(policy_name).c_str(), | 385 key.WriteValue(UTF8ToUTF16(policy_name).c_str(), |
| 373 UTF8ToUTF16(json).c_str()); | 386 UTF8ToUTF16(json).c_str()); |
| 374 } | 387 } |
| 375 | 388 |
| 376 void TestHarness::Install3rdPartyPolicy(const base::DictionaryValue* policies) { | 389 void TestHarness::Install3rdPartyPolicy(const base::DictionaryValue* policies) { |
| 377 // The first level entries are domains, and the second level entries map | 390 // The first level entries are domains, and the second level entries map |
| 378 // components to their policy. | 391 // components to their policy. |
| 379 const string16 kPathPrefix = string16(kRegistryMandatorySubKey) + kPathSep + | 392 const string16 kPathPrefix = string16(kRegistryChromePolicyKey) + kPathSep + |
| 380 kThirdParty + kPathSep; | 393 kThirdParty + kPathSep; |
| 381 for (base::DictionaryValue::Iterator domain(*policies); | 394 for (base::DictionaryValue::Iterator domain(*policies); |
| 382 domain.HasNext(); domain.Advance()) { | 395 domain.HasNext(); domain.Advance()) { |
| 383 const base::DictionaryValue* components = NULL; | 396 const base::DictionaryValue* components = NULL; |
| 384 if (!domain.value().GetAsDictionary(&components)) { | 397 if (!domain.value().GetAsDictionary(&components)) { |
| 385 ADD_FAILURE(); | 398 ADD_FAILURE(); |
| 386 continue; | 399 continue; |
| 387 } | 400 } |
| 388 for (base::DictionaryValue::Iterator component(*components); | 401 for (base::DictionaryValue::Iterator component(*components); |
| 389 component.HasNext(); component.Advance()) { | 402 component.HasNext(); component.Advance()) { |
| 390 const string16 path = string16(kRegistryMandatorySubKey) + kPathSep + | 403 const string16 path = string16(kRegistryChromePolicyKey) + kPathSep + |
| 391 kThirdParty + kPathSep + | 404 kThirdParty + kPathSep + |
| 392 UTF8ToUTF16(domain.key()) + kPathSep + | 405 UTF8ToUTF16(domain.key()) + kPathSep + |
| 393 UTF8ToUTF16(component.key()); | 406 UTF8ToUTF16(component.key()); |
| 394 InstallValue(component.value(), hive_, path, kMandatory); | 407 InstallValue(component.value(), hive_, path, kMandatory); |
| 395 EXPECT_TRUE(InstallSchema(component.value(), hive_, path, kSchema)); | 408 EXPECT_TRUE(InstallSchema(component.value(), hive_, path, kSchema)); |
| 396 } | 409 } |
| 397 } | 410 } |
| 398 } | 411 } |
| 399 | 412 |
| 400 // static | 413 // static |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 415 ConfigurationPolicyProviderTest, | 428 ConfigurationPolicyProviderTest, |
| 416 testing::Values(TestHarness::CreateHKCU, TestHarness::CreateHKLM)); | 429 testing::Values(TestHarness::CreateHKCU, TestHarness::CreateHKLM)); |
| 417 | 430 |
| 418 // Instantiate abstract test case for 3rd party policy reading tests. | 431 // Instantiate abstract test case for 3rd party policy reading tests. |
| 419 INSTANTIATE_TEST_CASE_P( | 432 INSTANTIATE_TEST_CASE_P( |
| 420 ThirdPartyPolicyProviderWinTest, | 433 ThirdPartyPolicyProviderWinTest, |
| 421 Configuration3rdPartyPolicyProviderTest, | 434 Configuration3rdPartyPolicyProviderTest, |
| 422 testing::Values(TestHarness::CreateHKCU, TestHarness::CreateHKLM)); | 435 testing::Values(TestHarness::CreateHKCU, TestHarness::CreateHKLM)); |
| 423 | 436 |
| 424 // Test cases for windows policy provider specific functionality. | 437 // Test cases for windows policy provider specific functionality. |
| 425 class PolicyLoaderWinTest : public PolicyTestBase { | 438 class PolicyLoaderWinTest : public PolicyTestBase, |
| 439 public AppliedGPOListProvider { | |
| 426 protected: | 440 protected: |
| 427 PolicyLoaderWinTest() {} | 441 // The policy key this tests places data under. This must match the data |
| 442 // files in chrome/test/data/policy/gpo. | |
| 443 static const char16 kTestPolicyKey[]; | |
| 444 | |
| 445 PolicyLoaderWinTest() | |
| 446 : gpo_list_(NULL), | |
| 447 gpo_list_status_(ERROR_ACCESS_DENIED) {} | |
| 428 virtual ~PolicyLoaderWinTest() {} | 448 virtual ~PolicyLoaderWinTest() {} |
| 429 | 449 |
| 450 virtual void SetUp() OVERRIDE { | |
| 451 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir_)); | |
| 452 test_data_dir_ = test_data_dir_.AppendASCII("policy").AppendASCII("gpo"); | |
| 453 } | |
| 454 | |
| 455 // AppliedGPOListProvider: | |
| 456 virtual DWORD GetAppliedGPOList(DWORD flags, | |
| 457 LPCTSTR machine_name, | |
| 458 PSID sid_user, | |
| 459 GUID* extension_guid, | |
| 460 PGROUP_POLICY_OBJECT* gpo_list) OVERRIDE { | |
| 461 *gpo_list = gpo_list_; | |
| 462 return gpo_list_status_; | |
| 463 } | |
|
Joao da Silva
2013/04/10 12:32:12
nit: newline
Mattias Nissler (ping if slow)
2013/04/10 13:20:25
We usually don't do that for OVERRIDE blocks belon
Joao da Silva
2013/04/10 13:32:17
I haven't seen this before. The style guides don't
| |
| 464 virtual BOOL FreeGPOList(PGROUP_POLICY_OBJECT gpo_list) OVERRIDE { | |
| 465 return TRUE; | |
| 466 } | |
| 467 | |
| 468 void InitGPO(GROUP_POLICY_OBJECT* gpo, | |
| 469 DWORD options, | |
| 470 const base::FilePath& path, | |
| 471 GROUP_POLICY_OBJECT* next, | |
| 472 GROUP_POLICY_OBJECT* prev) { | |
| 473 memset(gpo, 0, sizeof(GROUP_POLICY_OBJECT)); | |
| 474 gpo->dwOptions = options; | |
| 475 gpo->lpFileSysPath = const_cast<wchar_t*>(path.value().c_str()); | |
| 476 gpo->pNext = next; | |
| 477 gpo->pPrev = prev; | |
| 478 } | |
| 479 | |
| 430 bool Matches(const PolicyBundle& expected) { | 480 bool Matches(const PolicyBundle& expected) { |
| 431 PolicyLoaderWin loader(&test_policy_definitions::kList); | 481 PolicyLoaderWin loader(&test_policy_definitions::kList, kTestPolicyKey, |
| 482 this); | |
| 432 scoped_ptr<PolicyBundle> loaded(loader.Load()); | 483 scoped_ptr<PolicyBundle> loaded(loader.Load()); |
| 433 return loaded->Equals(expected); | 484 return loaded->Equals(expected); |
| 434 } | 485 } |
| 435 | 486 |
| 487 void InstallRegistrySentinel() { | |
| 488 RegKey hklm_key(HKEY_CURRENT_USER, kTestPolicyKey, KEY_ALL_ACCESS); | |
| 489 ASSERT_TRUE(hklm_key.Valid()); | |
| 490 hklm_key.WriteValue( | |
| 491 UTF8ToUTF16(test_policy_definitions::kKeyString).c_str(), | |
| 492 UTF8ToUTF16("registry").c_str()); | |
| 493 } | |
| 494 | |
| 495 bool MatchesRegistrySentinel() { | |
| 496 base::DictionaryValue expected_policy; | |
| 497 expected_policy.SetString(test_policy_definitions::kKeyString, "registry"); | |
| 498 PolicyBundle expected; | |
| 499 expected.Get(PolicyNamespace(POLICY_DOMAIN_CHROME, std::string())) | |
| 500 .LoadFrom(&expected_policy, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER); | |
| 501 return Matches(expected); | |
| 502 } | |
| 503 | |
| 504 bool MatchesTestBundle() { | |
| 505 base::DictionaryValue expected_policy; | |
| 506 expected_policy.SetBoolean(test_policy_definitions::kKeyBoolean, true); | |
| 507 expected_policy.SetString(test_policy_definitions::kKeyString, "GPO"); | |
| 508 expected_policy.SetInteger(test_policy_definitions::kKeyInteger, 42); | |
| 509 scoped_ptr<base::ListValue> list(new base::ListValue()); | |
| 510 list->AppendString("GPO 1"); | |
| 511 list->AppendString("GPO 2"); | |
| 512 expected_policy.Set(test_policy_definitions::kKeyStringList, | |
| 513 list.release()); | |
| 514 PolicyBundle expected; | |
| 515 expected.Get(PolicyNamespace(POLICY_DOMAIN_CHROME, std::string())) | |
| 516 .LoadFrom(&expected_policy, POLICY_LEVEL_MANDATORY, | |
| 517 POLICY_SCOPE_MACHINE); | |
| 518 return Matches(expected); | |
| 519 } | |
| 520 | |
| 436 ScopedGroupPolicyRegistrySandbox registry_sandbox_; | 521 ScopedGroupPolicyRegistrySandbox registry_sandbox_; |
| 522 PGROUP_POLICY_OBJECT gpo_list_; | |
| 523 DWORD gpo_list_status_; | |
| 524 base::FilePath test_data_dir_; | |
| 437 }; | 525 }; |
| 438 | 526 |
| 527 const char16 PolicyLoaderWinTest::kTestPolicyKey[] = | |
| 528 L"SOFTWARE\\Policies\\Chromium"; | |
| 529 | |
| 439 TEST_F(PolicyLoaderWinTest, HKLMOverHKCU) { | 530 TEST_F(PolicyLoaderWinTest, HKLMOverHKCU) { |
| 440 RegKey hklm_key(HKEY_LOCAL_MACHINE, kRegistryMandatorySubKey, KEY_ALL_ACCESS); | 531 RegKey hklm_key(HKEY_LOCAL_MACHINE, kTestPolicyKey, KEY_ALL_ACCESS); |
| 441 ASSERT_TRUE(hklm_key.Valid()); | 532 ASSERT_TRUE(hklm_key.Valid()); |
| 442 hklm_key.WriteValue(UTF8ToUTF16(test_policy_definitions::kKeyString).c_str(), | 533 hklm_key.WriteValue(UTF8ToUTF16(test_policy_definitions::kKeyString).c_str(), |
| 443 UTF8ToUTF16("hklm").c_str()); | 534 UTF8ToUTF16("hklm").c_str()); |
| 444 RegKey hkcu_key(HKEY_CURRENT_USER, kRegistryMandatorySubKey, KEY_ALL_ACCESS); | 535 RegKey hkcu_key(HKEY_CURRENT_USER, kTestPolicyKey, KEY_ALL_ACCESS); |
| 445 ASSERT_TRUE(hkcu_key.Valid()); | 536 ASSERT_TRUE(hkcu_key.Valid()); |
| 446 hkcu_key.WriteValue(UTF8ToUTF16(test_policy_definitions::kKeyString).c_str(), | 537 hkcu_key.WriteValue(UTF8ToUTF16(test_policy_definitions::kKeyString).c_str(), |
| 447 UTF8ToUTF16("hkcu").c_str()); | 538 UTF8ToUTF16("hkcu").c_str()); |
| 448 | 539 |
| 449 PolicyBundle expected; | 540 PolicyBundle expected; |
| 450 expected.Get(PolicyNamespace(POLICY_DOMAIN_CHROME, std::string())) | 541 expected.Get(PolicyNamespace(POLICY_DOMAIN_CHROME, std::string())) |
| 451 .Set(test_policy_definitions::kKeyString, | 542 .Set(test_policy_definitions::kKeyString, |
| 452 POLICY_LEVEL_MANDATORY, | 543 POLICY_LEVEL_MANDATORY, |
| 453 POLICY_SCOPE_MACHINE, | 544 POLICY_SCOPE_MACHINE, |
| 454 base::Value::CreateStringValue("hklm")); | 545 base::Value::CreateStringValue("hklm")); |
| 455 EXPECT_TRUE(Matches(expected)); | 546 EXPECT_TRUE(Matches(expected)); |
| 456 } | 547 } |
| 457 | 548 |
| 458 TEST_F(PolicyLoaderWinTest, Load3rdPartyWithoutSchema) { | 549 TEST_F(PolicyLoaderWinTest, Load3rdPartyWithoutSchema) { |
| 459 base::DictionaryValue dict; | 550 base::DictionaryValue dict; |
| 460 dict.SetString("str", "string value"); | 551 dict.SetString("str", "string value"); |
| 461 dict.SetInteger("int", 123); | 552 dict.SetInteger("int", 123); |
| 462 dict.Set("subdict", dict.DeepCopy()); | 553 dict.Set("subdict", dict.DeepCopy()); |
| 463 dict.Set("subsubdict", dict.DeepCopy()); | 554 dict.Set("subsubdict", dict.DeepCopy()); |
| 464 dict.Set("subsubsubdict", dict.DeepCopy()); | 555 dict.Set("subsubsubdict", dict.DeepCopy()); |
| 465 | 556 |
| 466 base::DictionaryValue policy_dict; | 557 base::DictionaryValue policy_dict; |
| 467 policy_dict.Set("extensions.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.policy", | 558 policy_dict.Set("extensions.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.policy", |
| 468 dict.DeepCopy()); | 559 dict.DeepCopy()); |
| 469 policy_dict.Set("extensions.bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb.policy", | 560 policy_dict.Set("extensions.bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb.policy", |
| 470 dict.DeepCopy()); | 561 dict.DeepCopy()); |
| 471 EXPECT_TRUE(InstallValue(policy_dict, HKEY_LOCAL_MACHINE, | 562 EXPECT_TRUE(InstallValue(policy_dict, HKEY_LOCAL_MACHINE, |
| 472 kRegistryMandatorySubKey, kThirdParty)); | 563 kTestPolicyKey, kThirdParty)); |
| 473 | 564 |
| 474 PolicyBundle expected; | 565 PolicyBundle expected; |
| 475 expected.Get(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, | 566 expected.Get(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, |
| 476 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")) | 567 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")) |
| 477 .LoadFrom(&dict, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE); | 568 .LoadFrom(&dict, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE); |
| 478 expected.Get(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, | 569 expected.Get(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, |
| 479 "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb")) | 570 "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb")) |
| 480 .LoadFrom(&dict, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE); | 571 .LoadFrom(&dict, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE); |
| 481 EXPECT_TRUE(Matches(expected)); | 572 EXPECT_TRUE(Matches(expected)); |
| 482 } | 573 } |
| 483 | 574 |
| 484 TEST_F(PolicyLoaderWinTest, Merge3rdPartyPolicies) { | 575 TEST_F(PolicyLoaderWinTest, Merge3rdPartyPolicies) { |
| 485 // Policy for the same extension will be provided at the 4 level/scope | 576 // Policy for the same extension will be provided at the 4 level/scope |
| 486 // combinations, to verify that they overlap as expected. | 577 // combinations, to verify that they overlap as expected. |
| 487 | 578 |
| 488 const string16 kPathSuffix = | 579 const string16 kPathSuffix = |
| 489 kRegistryMandatorySubKey + ASCIIToUTF16("\\3rdparty\\extensions\\merge"); | 580 kTestPolicyKey + ASCIIToUTF16("\\3rdparty\\extensions\\merge"); |
| 490 | 581 |
| 491 const char kUserMandatory[] = "user-mandatory"; | 582 const char kUserMandatory[] = "user-mandatory"; |
| 492 const char kUserRecommended[] = "user-recommended"; | 583 const char kUserRecommended[] = "user-recommended"; |
| 493 const char kMachineMandatory[] = "machine-mandatory"; | 584 const char kMachineMandatory[] = "machine-mandatory"; |
| 494 const char kMachineRecommended[] = "machine-recommended"; | 585 const char kMachineRecommended[] = "machine-recommended"; |
| 495 | 586 |
| 496 base::DictionaryValue policy; | 587 base::DictionaryValue policy; |
| 497 policy.SetString("a", kMachineMandatory); | 588 policy.SetString("a", kMachineMandatory); |
| 498 EXPECT_TRUE(InstallValue(policy, HKEY_LOCAL_MACHINE, | 589 EXPECT_TRUE(InstallValue(policy, HKEY_LOCAL_MACHINE, |
| 499 kPathSuffix, kMandatory)); | 590 kPathSuffix, kMandatory)); |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 551 ASSERT_FALSE(encoded_list.empty()); | 642 ASSERT_FALSE(encoded_list.empty()); |
| 552 base::DictionaryValue encoded_policy; | 643 base::DictionaryValue encoded_policy; |
| 553 encoded_policy.SetString("null", ""); | 644 encoded_policy.SetString("null", ""); |
| 554 encoded_policy.SetString("bool", "1"); | 645 encoded_policy.SetString("bool", "1"); |
| 555 encoded_policy.SetString("int", "-123"); | 646 encoded_policy.SetString("int", "-123"); |
| 556 encoded_policy.SetString("double", "456.78e9"); | 647 encoded_policy.SetString("double", "456.78e9"); |
| 557 encoded_policy.SetString("list", encoded_list); | 648 encoded_policy.SetString("list", encoded_list); |
| 558 encoded_policy.SetString("dict", encoded_dict); | 649 encoded_policy.SetString("dict", encoded_dict); |
| 559 | 650 |
| 560 const string16 kPathSuffix = | 651 const string16 kPathSuffix = |
| 561 kRegistryMandatorySubKey + ASCIIToUTF16("\\3rdparty\\extensions\\string"); | 652 kTestPolicyKey + ASCIIToUTF16("\\3rdparty\\extensions\\string"); |
| 562 EXPECT_TRUE(InstallSchema(policy, HKEY_CURRENT_USER, kPathSuffix, kSchema)); | 653 EXPECT_TRUE(InstallSchema(policy, HKEY_CURRENT_USER, kPathSuffix, kSchema)); |
| 563 EXPECT_TRUE( | 654 EXPECT_TRUE( |
| 564 InstallValue(encoded_policy, HKEY_CURRENT_USER, kPathSuffix, kMandatory)); | 655 InstallValue(encoded_policy, HKEY_CURRENT_USER, kPathSuffix, kMandatory)); |
| 565 | 656 |
| 566 PolicyBundle expected; | 657 PolicyBundle expected; |
| 567 expected.Get(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "string")) | 658 expected.Get(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "string")) |
| 568 .LoadFrom(&policy, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER); | 659 .LoadFrom(&policy, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER); |
| 569 EXPECT_TRUE(Matches(expected)); | 660 EXPECT_TRUE(Matches(expected)); |
| 570 } | 661 } |
| 571 | 662 |
| 572 TEST_F(PolicyLoaderWinTest, LoadIntegerEncodedValues) { | 663 TEST_F(PolicyLoaderWinTest, LoadIntegerEncodedValues) { |
| 573 base::DictionaryValue policy; | 664 base::DictionaryValue policy; |
| 574 policy.SetBoolean("bool", true); | 665 policy.SetBoolean("bool", true); |
| 575 policy.SetInteger("int", 123); | 666 policy.SetInteger("int", 123); |
| 576 policy.SetDouble("double", 456.0); | 667 policy.SetDouble("double", 456.0); |
| 577 | 668 |
| 578 base::DictionaryValue encoded_policy; | 669 base::DictionaryValue encoded_policy; |
| 579 encoded_policy.SetInteger("bool", 1); | 670 encoded_policy.SetInteger("bool", 1); |
| 580 encoded_policy.SetInteger("int", 123); | 671 encoded_policy.SetInteger("int", 123); |
| 581 encoded_policy.SetInteger("double", 456); | 672 encoded_policy.SetInteger("double", 456); |
| 582 | 673 |
| 583 const string16 kPathSuffix = | 674 const string16 kPathSuffix = |
| 584 kRegistryMandatorySubKey + ASCIIToUTF16("\\3rdparty\\extensions\\int"); | 675 kTestPolicyKey + ASCIIToUTF16("\\3rdparty\\extensions\\int"); |
| 585 EXPECT_TRUE(InstallSchema(policy, HKEY_CURRENT_USER, kPathSuffix, kSchema)); | 676 EXPECT_TRUE(InstallSchema(policy, HKEY_CURRENT_USER, kPathSuffix, kSchema)); |
| 586 EXPECT_TRUE( | 677 EXPECT_TRUE( |
| 587 InstallValue(encoded_policy, HKEY_CURRENT_USER, kPathSuffix, kMandatory)); | 678 InstallValue(encoded_policy, HKEY_CURRENT_USER, kPathSuffix, kMandatory)); |
| 588 | 679 |
| 589 PolicyBundle expected; | 680 PolicyBundle expected; |
| 590 expected.Get(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "int")) | 681 expected.Get(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "int")) |
| 591 .LoadFrom(&policy, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER); | 682 .LoadFrom(&policy, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER); |
| 592 EXPECT_TRUE(Matches(expected)); | 683 EXPECT_TRUE(Matches(expected)); |
| 593 } | 684 } |
| 594 | 685 |
| 595 TEST_F(PolicyLoaderWinTest, DefaultPropertySchemaType) { | 686 TEST_F(PolicyLoaderWinTest, DefaultPropertySchemaType) { |
| 596 // Build a schema for an "object" with a default schema for its properties. | 687 // Build a schema for an "object" with a default schema for its properties. |
| 597 base::DictionaryValue default_schema; | 688 base::DictionaryValue default_schema; |
| 598 default_schema.SetString(schema::kType, "number"); | 689 default_schema.SetString(schema::kType, "number"); |
| 599 base::DictionaryValue integer_schema; | 690 base::DictionaryValue integer_schema; |
| 600 integer_schema.SetString(schema::kType, "integer"); | 691 integer_schema.SetString(schema::kType, "integer"); |
| 601 base::DictionaryValue properties; | 692 base::DictionaryValue properties; |
| 602 properties.Set("special-int1", integer_schema.DeepCopy()); | 693 properties.Set("special-int1", integer_schema.DeepCopy()); |
| 603 properties.Set("special-int2", integer_schema.DeepCopy()); | 694 properties.Set("special-int2", integer_schema.DeepCopy()); |
| 604 base::DictionaryValue schema; | 695 base::DictionaryValue schema; |
| 605 schema.SetString(schema::kType, "object"); | 696 schema.SetString(schema::kType, "object"); |
| 606 schema.Set(schema::kProperties, properties.DeepCopy()); | 697 schema.Set(schema::kProperties, properties.DeepCopy()); |
| 607 schema.Set(schema::kAdditionalProperties, default_schema.DeepCopy()); | 698 schema.Set(schema::kAdditionalProperties, default_schema.DeepCopy()); |
| 608 | 699 |
| 609 const string16 kPathSuffix = | 700 const string16 kPathSuffix = |
| 610 kRegistryMandatorySubKey + ASCIIToUTF16("\\3rdparty\\extensions\\test"); | 701 kTestPolicyKey + ASCIIToUTF16("\\3rdparty\\extensions\\test"); |
| 611 EXPECT_TRUE(WriteSchema(schema, HKEY_CURRENT_USER, kPathSuffix, kSchema)); | 702 EXPECT_TRUE(WriteSchema(schema, HKEY_CURRENT_USER, kPathSuffix, kSchema)); |
| 612 | 703 |
| 613 // Write some test values. | 704 // Write some test values. |
| 614 base::DictionaryValue policy; | 705 base::DictionaryValue policy; |
| 615 // These special values have a specific schema for them. | 706 // These special values have a specific schema for them. |
| 616 policy.SetInteger("special-int1", 123); | 707 policy.SetInteger("special-int1", 123); |
| 617 policy.SetString("special-int2", "-456"); | 708 policy.SetString("special-int2", "-456"); |
| 618 // Other values default to be loaded as doubles. | 709 // Other values default to be loaded as doubles. |
| 619 policy.SetInteger("double1", 789.0); | 710 policy.SetInteger("double1", 789.0); |
| 620 policy.SetString("double2", "123.456e7"); | 711 policy.SetString("double2", "123.456e7"); |
| 621 policy.SetString("invalid", "omg"); | 712 policy.SetString("invalid", "omg"); |
| 622 EXPECT_TRUE(InstallValue(policy, HKEY_CURRENT_USER, kPathSuffix, kMandatory)); | 713 EXPECT_TRUE(InstallValue(policy, HKEY_CURRENT_USER, kPathSuffix, kMandatory)); |
| 623 | 714 |
| 624 base::DictionaryValue expected_policy; | 715 base::DictionaryValue expected_policy; |
| 625 expected_policy.SetInteger("special-int1", 123); | 716 expected_policy.SetInteger("special-int1", 123); |
| 626 expected_policy.SetInteger("special-int2", -456); | 717 expected_policy.SetInteger("special-int2", -456); |
| 627 expected_policy.SetDouble("double1", 789.0); | 718 expected_policy.SetDouble("double1", 789.0); |
| 628 expected_policy.SetDouble("double2", 123.456e7); | 719 expected_policy.SetDouble("double2", 123.456e7); |
| 720 expected_policy.Set("invalid", base::Value::CreateNullValue()); | |
| 629 PolicyBundle expected; | 721 PolicyBundle expected; |
| 630 expected.Get(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "test")) | 722 expected.Get(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "test")) |
| 631 .LoadFrom(&expected_policy, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER); | 723 .LoadFrom(&expected_policy, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER); |
| 632 EXPECT_TRUE(Matches(expected)); | 724 EXPECT_TRUE(Matches(expected)); |
| 633 } | 725 } |
| 634 | 726 |
| 727 TEST_F(PolicyLoaderWinTest, AppliedPolicyNonExistingFile) { | |
| 728 GROUP_POLICY_OBJECT gpo; | |
| 729 InitGPO(&gpo, 0, test_data_dir_, NULL, NULL); | |
| 730 gpo_list_ = &gpo; | |
| 731 gpo_list_status_ = ERROR_SUCCESS; | |
| 732 | |
| 733 InstallRegistrySentinel(); | |
| 734 EXPECT_TRUE(MatchesRegistrySentinel()); | |
| 735 } | |
| 736 | |
| 737 TEST_F(PolicyLoaderWinTest, AppliedPolicyBadPath) { | |
| 738 base::FilePath gpo_dir(test_data_dir_.AppendASCII("bad")); | |
| 739 GROUP_POLICY_OBJECT gpo; | |
| 740 InitGPO(&gpo, 0, gpo_dir, NULL, NULL); | |
| 741 gpo_list_ = &gpo; | |
| 742 gpo_list_status_ = ERROR_SUCCESS; | |
| 743 | |
| 744 InstallRegistrySentinel(); | |
| 745 EXPECT_TRUE(MatchesRegistrySentinel()); | |
| 746 } | |
| 747 | |
| 748 TEST_F(PolicyLoaderWinTest, AppliedPolicyPresent) { | |
| 749 base::FilePath gpo_dir(test_data_dir_.AppendASCII("test1")); | |
| 750 GROUP_POLICY_OBJECT gpo; | |
| 751 InitGPO(&gpo, 0, gpo_dir, NULL, NULL); | |
| 752 gpo_list_ = &gpo; | |
| 753 gpo_list_status_ = ERROR_SUCCESS; | |
| 754 | |
| 755 EXPECT_TRUE(MatchesTestBundle()); | |
| 756 } | |
| 757 | |
| 758 TEST_F(PolicyLoaderWinTest, AppliedPolicyMerged) { | |
| 759 base::FilePath gpo1_dir(test_data_dir_.AppendASCII("test2")); | |
| 760 base::FilePath gpo2_dir(test_data_dir_.AppendASCII("test1")); | |
| 761 GROUP_POLICY_OBJECT gpo1; | |
| 762 GROUP_POLICY_OBJECT gpo2; | |
| 763 InitGPO(&gpo1, 0, gpo1_dir, &gpo2, NULL); | |
| 764 InitGPO(&gpo2, 0, gpo2_dir, NULL, &gpo1); | |
| 765 gpo_list_ = &gpo1; | |
| 766 gpo_list_status_ = ERROR_SUCCESS; | |
| 767 | |
| 768 EXPECT_TRUE(MatchesTestBundle()); | |
| 769 } | |
| 770 | |
| 771 TEST_F(PolicyLoaderWinTest, AppliedPolicyDisabled) { | |
| 772 base::FilePath gpo1_dir(test_data_dir_.AppendASCII("test1")); | |
| 773 base::FilePath gpo2_dir(test_data_dir_.AppendASCII("test2")); | |
| 774 GROUP_POLICY_OBJECT gpo1; | |
| 775 GROUP_POLICY_OBJECT gpo2; | |
| 776 InitGPO(&gpo1, 0, gpo1_dir, &gpo2, NULL); | |
| 777 InitGPO(&gpo2, GPO_FLAG_DISABLE, gpo2_dir, NULL, &gpo1); | |
| 778 gpo_list_ = &gpo1; | |
| 779 gpo_list_status_ = ERROR_SUCCESS; | |
| 780 | |
| 781 EXPECT_TRUE(MatchesTestBundle()); | |
| 782 } | |
| 783 | |
| 784 TEST_F(PolicyLoaderWinTest, AppliedPolicyForcedPolicy) { | |
| 785 base::FilePath gpo1_dir(test_data_dir_.AppendASCII("test1")); | |
| 786 base::FilePath gpo2_dir(test_data_dir_.AppendASCII("test2")); | |
| 787 GROUP_POLICY_OBJECT gpo1; | |
| 788 GROUP_POLICY_OBJECT gpo2; | |
| 789 InitGPO(&gpo1, GPO_FLAG_FORCE, gpo1_dir, &gpo2, NULL); | |
| 790 InitGPO(&gpo2, 0, gpo2_dir, NULL, &gpo1); | |
| 791 gpo_list_ = &gpo1; | |
| 792 gpo_list_status_ = ERROR_SUCCESS; | |
| 793 | |
| 794 EXPECT_TRUE(MatchesTestBundle()); | |
| 795 } | |
| 796 | |
| 797 TEST_F(PolicyLoaderWinTest, AppliedPolicyUNCPath) { | |
| 798 base::FilePath gpo_dir(test_data_dir_.AppendASCII("test1")); | |
| 799 base::FilePath unc_path(L"\\\\some_share\\GPO"); | |
| 800 GROUP_POLICY_OBJECT gpo1; | |
| 801 GROUP_POLICY_OBJECT gpo2; | |
| 802 InitGPO(&gpo1, 0, gpo_dir, &gpo2, NULL); | |
| 803 InitGPO(&gpo2, 0, unc_path, NULL, &gpo1); | |
| 804 gpo_list_ = &gpo1; | |
| 805 gpo_list_status_ = ERROR_SUCCESS; | |
| 806 | |
| 807 InstallRegistrySentinel(); | |
| 808 EXPECT_TRUE(MatchesRegistrySentinel()); | |
| 809 } | |
| 810 | |
|
Joao da Silva
2013/04/10 12:32:12
Can you add a test where there is no GPO data, the
Mattias Nissler (ping if slow)
2013/04/10 13:20:25
Done.
| |
| 635 } // namespace policy | 811 } // namespace policy |
| OLD | NEW |