Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(854)

Side by Side Diff: chrome/browser/policy/policy_loader_win_unittest.cc

Issue 13619014: Change PolicyLoaderWin to load PReg files if possible. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 168 matching lines...) Expand 10 before | Expand all | Expand 10 after
212 std::wstring key_name_; 225 std::wstring key_name_;
213 226
214 // Keys are created for the lifetime of a test to contain 227 // Keys are created for the lifetime of a test to contain
215 // the sandboxed HKCU and HKLM hives, respectively. 228 // the sandboxed HKCU and HKLM hives, respectively.
216 RegKey temp_hkcu_hive_key_; 229 RegKey temp_hkcu_hive_key_;
217 RegKey temp_hklm_hive_key_; 230 RegKey temp_hklm_hive_key_;
218 231
219 DISALLOW_COPY_AND_ASSIGN(ScopedGroupPolicyRegistrySandbox); 232 DISALLOW_COPY_AND_ASSIGN(ScopedGroupPolicyRegistrySandbox);
220 }; 233 };
221 234
222 class TestHarness : public PolicyProviderTestHarness { 235 class TestHarness : public PolicyProviderTestHarness,
236 public AppliedGPOListProvider {
223 public: 237 public:
224 explicit TestHarness(HKEY hive, PolicyScope scope); 238 explicit TestHarness(HKEY hive, PolicyScope scope);
225 virtual ~TestHarness(); 239 virtual ~TestHarness();
226 240
227 virtual void SetUp() OVERRIDE; 241 virtual void SetUp() OVERRIDE;
228 242
243 // PolicyProviderTestHarness:
229 virtual ConfigurationPolicyProvider* CreateProvider( 244 virtual ConfigurationPolicyProvider* CreateProvider(
230 const PolicyDefinitionList* policy_definition_list) OVERRIDE; 245 const PolicyDefinitionList* policy_definition_list) OVERRIDE;
231 246
232 virtual void InstallEmptyPolicy() OVERRIDE; 247 virtual void InstallEmptyPolicy() OVERRIDE;
233 virtual void InstallStringPolicy(const std::string& policy_name, 248 virtual void InstallStringPolicy(const std::string& policy_name,
234 const std::string& policy_value) OVERRIDE; 249 const std::string& policy_value) OVERRIDE;
235 virtual void InstallIntegerPolicy(const std::string& policy_name, 250 virtual void InstallIntegerPolicy(const std::string& policy_name,
236 int policy_value) OVERRIDE; 251 int policy_value) OVERRIDE;
237 virtual void InstallBooleanPolicy(const std::string& policy_name, 252 virtual void InstallBooleanPolicy(const std::string& policy_name,
238 bool policy_value) OVERRIDE; 253 bool policy_value) OVERRIDE;
239 virtual void InstallStringListPolicy( 254 virtual void InstallStringListPolicy(
240 const std::string& policy_name, 255 const std::string& policy_name,
241 const base::ListValue* policy_value) OVERRIDE; 256 const base::ListValue* policy_value) OVERRIDE;
242 virtual void InstallDictionaryPolicy( 257 virtual void InstallDictionaryPolicy(
243 const std::string& policy_name, 258 const std::string& policy_name,
244 const base::DictionaryValue* policy_value) OVERRIDE; 259 const base::DictionaryValue* policy_value) OVERRIDE;
245 virtual void Install3rdPartyPolicy( 260 virtual void Install3rdPartyPolicy(
246 const base::DictionaryValue* policies) OVERRIDE; 261 const base::DictionaryValue* policies) OVERRIDE;
247 262
263 // AppliedGPOListProvider:
264 virtual DWORD GetAppliedGPOList(DWORD flags,
265 LPCTSTR machine_name,
266 PSID sid_user,
267 GUID* extension_guid,
268 PGROUP_POLICY_OBJECT* gpo_list) OVERRIDE;
269 virtual BOOL FreeGPOList(PGROUP_POLICY_OBJECT gpo_list) OVERRIDE;
270
248 // Creates a harness instance that will install policy in HKCU or HKLM, 271 // Creates a harness instance that will install policy in HKCU or HKLM,
249 // respectively. 272 // respectively.
250 static PolicyProviderTestHarness* CreateHKCU(); 273 static PolicyProviderTestHarness* CreateHKCU();
251 static PolicyProviderTestHarness* CreateHKLM(); 274 static PolicyProviderTestHarness* CreateHKLM();
252 275
253 private: 276 private:
254 HKEY hive_; 277 HKEY hive_;
255 278
256 ScopedGroupPolicyRegistrySandbox registry_sandbox_; 279 ScopedGroupPolicyRegistrySandbox registry_sandbox_;
257 280
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
303 key.DeleteKey(L""); 326 key.DeleteKey(L"");
304 } 327 }
305 328
306 TestHarness::TestHarness(HKEY hive, PolicyScope scope) 329 TestHarness::TestHarness(HKEY hive, PolicyScope scope)
307 : PolicyProviderTestHarness(POLICY_LEVEL_MANDATORY, scope), hive_(hive) {} 330 : PolicyProviderTestHarness(POLICY_LEVEL_MANDATORY, scope), hive_(hive) {}
308 331
309 TestHarness::~TestHarness() {} 332 TestHarness::~TestHarness() {}
310 333
311 void TestHarness::SetUp() {} 334 void TestHarness::SetUp() {}
312 335
336 DWORD TestHarness::GetAppliedGPOList(DWORD flags,
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
313 ConfigurationPolicyProvider* TestHarness::CreateProvider( 349 ConfigurationPolicyProvider* TestHarness::CreateProvider(
314 const PolicyDefinitionList* policy_list) { 350 const PolicyDefinitionList* policy_list) {
315 scoped_ptr<AsyncPolicyLoader> loader(new PolicyLoaderWin(policy_list)); 351 scoped_ptr<AsyncPolicyLoader> loader(
352 new PolicyLoaderWin(policy_list, kRegistryChromePolicyKey, this));
316 return new AsyncPolicyProvider(loader.Pass()); 353 return new AsyncPolicyProvider(loader.Pass());
317 } 354 }
318 355
319 void TestHarness::InstallEmptyPolicy() {} 356 void TestHarness::InstallEmptyPolicy() {}
320 357
321 void TestHarness::InstallStringPolicy(const std::string& policy_name, 358 void TestHarness::InstallStringPolicy(const std::string& policy_name,
322 const std::string& policy_value) { 359 const std::string& policy_value) {
323 RegKey key(hive_, kRegistryMandatorySubKey, KEY_ALL_ACCESS); 360 RegKey key(hive_, kRegistryChromePolicyKey, KEY_ALL_ACCESS);
324 ASSERT_TRUE(key.Valid()); 361 ASSERT_TRUE(key.Valid());
325 ASSERT_HRESULT_SUCCEEDED(key.WriteValue(UTF8ToUTF16(policy_name).c_str(), 362 ASSERT_HRESULT_SUCCEEDED(key.WriteValue(UTF8ToUTF16(policy_name).c_str(),
326 UTF8ToUTF16(policy_value).c_str())); 363 UTF8ToUTF16(policy_value).c_str()));
327 } 364 }
328 365
329 void TestHarness::InstallIntegerPolicy(const std::string& policy_name, 366 void TestHarness::InstallIntegerPolicy(const std::string& policy_name,
330 int policy_value) { 367 int policy_value) {
331 RegKey key(hive_, kRegistryMandatorySubKey, KEY_ALL_ACCESS); 368 RegKey key(hive_, kRegistryChromePolicyKey, KEY_ALL_ACCESS);
332 ASSERT_TRUE(key.Valid()); 369 ASSERT_TRUE(key.Valid());
333 key.WriteValue(UTF8ToUTF16(policy_name).c_str(), 370 key.WriteValue(UTF8ToUTF16(policy_name).c_str(),
334 static_cast<DWORD>(policy_value)); 371 static_cast<DWORD>(policy_value));
335 } 372 }
336 373
337 void TestHarness::InstallBooleanPolicy(const std::string& policy_name, 374 void TestHarness::InstallBooleanPolicy(const std::string& policy_name,
338 bool policy_value) { 375 bool policy_value) {
339 RegKey key(hive_, kRegistryMandatorySubKey, KEY_ALL_ACCESS); 376 RegKey key(hive_, kRegistryChromePolicyKey, KEY_ALL_ACCESS);
340 ASSERT_TRUE(key.Valid()); 377 ASSERT_TRUE(key.Valid());
341 key.WriteValue(UTF8ToUTF16(policy_name).c_str(), 378 key.WriteValue(UTF8ToUTF16(policy_name).c_str(),
342 static_cast<DWORD>(policy_value)); 379 static_cast<DWORD>(policy_value));
343 } 380 }
344 381
345 void TestHarness::InstallStringListPolicy(const std::string& policy_name, 382 void TestHarness::InstallStringListPolicy(const std::string& policy_name,
346 const base::ListValue* policy_value) { 383 const base::ListValue* policy_value) {
347 RegKey key(hive_, 384 RegKey key(hive_,
348 (string16(kRegistryMandatorySubKey) + ASCIIToUTF16("\\") + 385 (string16(kRegistryChromePolicyKey) + ASCIIToUTF16("\\") +
349 UTF8ToUTF16(policy_name)).c_str(), 386 UTF8ToUTF16(policy_name)).c_str(),
350 KEY_ALL_ACCESS); 387 KEY_ALL_ACCESS);
351 ASSERT_TRUE(key.Valid()); 388 ASSERT_TRUE(key.Valid());
352 int index = 1; 389 int index = 1;
353 for (base::ListValue::const_iterator element(policy_value->begin()); 390 for (base::ListValue::const_iterator element(policy_value->begin());
354 element != policy_value->end(); 391 element != policy_value->end();
355 ++element) { 392 ++element) {
356 std::string element_value; 393 std::string element_value;
357 if (!(*element)->GetAsString(&element_value)) 394 if (!(*element)->GetAsString(&element_value))
358 continue; 395 continue;
359 std::string name(base::IntToString(index++)); 396 std::string name(base::IntToString(index++));
360 key.WriteValue(UTF8ToUTF16(name).c_str(), 397 key.WriteValue(UTF8ToUTF16(name).c_str(),
361 UTF8ToUTF16(element_value).c_str()); 398 UTF8ToUTF16(element_value).c_str());
362 } 399 }
363 } 400 }
364 401
365 void TestHarness::InstallDictionaryPolicy( 402 void TestHarness::InstallDictionaryPolicy(
366 const std::string& policy_name, 403 const std::string& policy_name,
367 const base::DictionaryValue* policy_value) { 404 const base::DictionaryValue* policy_value) {
368 std::string json; 405 std::string json;
369 base::JSONWriter::Write(policy_value, &json); 406 base::JSONWriter::Write(policy_value, &json);
370 RegKey key(hive_, kRegistryMandatorySubKey, KEY_ALL_ACCESS); 407 RegKey key(hive_, kRegistryChromePolicyKey, KEY_ALL_ACCESS);
371 ASSERT_TRUE(key.Valid()); 408 ASSERT_TRUE(key.Valid());
372 key.WriteValue(UTF8ToUTF16(policy_name).c_str(), 409 key.WriteValue(UTF8ToUTF16(policy_name).c_str(),
373 UTF8ToUTF16(json).c_str()); 410 UTF8ToUTF16(json).c_str());
374 } 411 }
375 412
376 void TestHarness::Install3rdPartyPolicy(const base::DictionaryValue* policies) { 413 void TestHarness::Install3rdPartyPolicy(const base::DictionaryValue* policies) {
377 // The first level entries are domains, and the second level entries map 414 // The first level entries are domains, and the second level entries map
378 // components to their policy. 415 // components to their policy.
379 const string16 kPathPrefix = string16(kRegistryMandatorySubKey) + kPathSep + 416 const string16 kPathPrefix = string16(kRegistryChromePolicyKey) + kPathSep +
380 kThirdParty + kPathSep; 417 kThirdParty + kPathSep;
381 for (base::DictionaryValue::Iterator domain(*policies); 418 for (base::DictionaryValue::Iterator domain(*policies);
382 !domain.IsAtEnd(); domain.Advance()) { 419 !domain.IsAtEnd(); domain.Advance()) {
383 const base::DictionaryValue* components = NULL; 420 const base::DictionaryValue* components = NULL;
384 if (!domain.value().GetAsDictionary(&components)) { 421 if (!domain.value().GetAsDictionary(&components)) {
385 ADD_FAILURE(); 422 ADD_FAILURE();
386 continue; 423 continue;
387 } 424 }
388 for (base::DictionaryValue::Iterator component(*components); 425 for (base::DictionaryValue::Iterator component(*components);
389 !component.IsAtEnd(); component.Advance()) { 426 !component.IsAtEnd(); component.Advance()) {
390 const string16 path = string16(kRegistryMandatorySubKey) + kPathSep + 427 const string16 path = string16(kRegistryChromePolicyKey) + kPathSep +
391 kThirdParty + kPathSep + 428 kThirdParty + kPathSep +
392 UTF8ToUTF16(domain.key()) + kPathSep + 429 UTF8ToUTF16(domain.key()) + kPathSep +
393 UTF8ToUTF16(component.key()); 430 UTF8ToUTF16(component.key());
394 InstallValue(component.value(), hive_, path, kMandatory); 431 InstallValue(component.value(), hive_, path, kMandatory);
395 EXPECT_TRUE(InstallSchema(component.value(), hive_, path, kSchema)); 432 EXPECT_TRUE(InstallSchema(component.value(), hive_, path, kSchema));
396 } 433 }
397 } 434 }
398 } 435 }
399 436
400 // static 437 // static
(...skipping 14 matching lines...) Expand all
415 ConfigurationPolicyProviderTest, 452 ConfigurationPolicyProviderTest,
416 testing::Values(TestHarness::CreateHKCU, TestHarness::CreateHKLM)); 453 testing::Values(TestHarness::CreateHKCU, TestHarness::CreateHKLM));
417 454
418 // Instantiate abstract test case for 3rd party policy reading tests. 455 // Instantiate abstract test case for 3rd party policy reading tests.
419 INSTANTIATE_TEST_CASE_P( 456 INSTANTIATE_TEST_CASE_P(
420 ThirdPartyPolicyProviderWinTest, 457 ThirdPartyPolicyProviderWinTest,
421 Configuration3rdPartyPolicyProviderTest, 458 Configuration3rdPartyPolicyProviderTest,
422 testing::Values(TestHarness::CreateHKCU, TestHarness::CreateHKLM)); 459 testing::Values(TestHarness::CreateHKCU, TestHarness::CreateHKLM));
423 460
424 // Test cases for windows policy provider specific functionality. 461 // Test cases for windows policy provider specific functionality.
425 class PolicyLoaderWinTest : public PolicyTestBase { 462 class PolicyLoaderWinTest : public PolicyTestBase,
463 public AppliedGPOListProvider {
426 protected: 464 protected:
427 PolicyLoaderWinTest() {} 465 // The policy key this tests places data under. This must match the data
466 // files in chrome/test/data/policy/gpo.
467 static const char16 kTestPolicyKey[];
468
469 PolicyLoaderWinTest()
470 : gpo_list_(NULL),
471 gpo_list_status_(ERROR_ACCESS_DENIED) {}
428 virtual ~PolicyLoaderWinTest() {} 472 virtual ~PolicyLoaderWinTest() {}
429 473
474 virtual void SetUp() OVERRIDE {
475 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir_));
476 test_data_dir_ = test_data_dir_.AppendASCII("policy").AppendASCII("gpo");
477 }
478
479 // AppliedGPOListProvider:
480 virtual DWORD GetAppliedGPOList(DWORD flags,
481 LPCTSTR machine_name,
482 PSID sid_user,
483 GUID* extension_guid,
484 PGROUP_POLICY_OBJECT* gpo_list) OVERRIDE {
485 *gpo_list = gpo_list_;
486 return gpo_list_status_;
487 }
488 virtual BOOL FreeGPOList(PGROUP_POLICY_OBJECT gpo_list) OVERRIDE {
489 return TRUE;
490 }
491
492 void InitGPO(GROUP_POLICY_OBJECT* gpo,
493 DWORD options,
494 const base::FilePath& path,
495 GROUP_POLICY_OBJECT* next,
496 GROUP_POLICY_OBJECT* prev) {
497 memset(gpo, 0, sizeof(GROUP_POLICY_OBJECT));
498 gpo->dwOptions = options;
499 gpo->lpFileSysPath = const_cast<wchar_t*>(path.value().c_str());
500 gpo->pNext = next;
501 gpo->pPrev = prev;
502 }
503
430 bool Matches(const PolicyBundle& expected) { 504 bool Matches(const PolicyBundle& expected) {
431 PolicyLoaderWin loader(&test_policy_definitions::kList); 505 PolicyLoaderWin loader(&test_policy_definitions::kList, kTestPolicyKey,
506 this);
432 scoped_ptr<PolicyBundle> loaded(loader.Load()); 507 scoped_ptr<PolicyBundle> loaded(loader.Load());
433 return loaded->Equals(expected); 508 return loaded->Equals(expected);
434 } 509 }
435 510
511 void InstallRegistrySentinel() {
512 RegKey hklm_key(HKEY_CURRENT_USER, kTestPolicyKey, KEY_ALL_ACCESS);
513 ASSERT_TRUE(hklm_key.Valid());
514 hklm_key.WriteValue(
515 UTF8ToUTF16(test_policy_definitions::kKeyString).c_str(),
516 UTF8ToUTF16("registry").c_str());
517 }
518
519 bool MatchesRegistrySentinel() {
520 base::DictionaryValue expected_policy;
521 expected_policy.SetString(test_policy_definitions::kKeyString, "registry");
522 PolicyBundle expected;
523 expected.Get(PolicyNamespace(POLICY_DOMAIN_CHROME, std::string()))
524 .LoadFrom(&expected_policy, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER);
525 return Matches(expected);
526 }
527
528 bool MatchesTestBundle() {
529 base::DictionaryValue expected_policy;
530 expected_policy.SetBoolean(test_policy_definitions::kKeyBoolean, true);
531 expected_policy.SetString(test_policy_definitions::kKeyString, "GPO");
532 expected_policy.SetInteger(test_policy_definitions::kKeyInteger, 42);
533 scoped_ptr<base::ListValue> list(new base::ListValue());
534 list->AppendString("GPO 1");
535 list->AppendString("GPO 2");
536 expected_policy.Set(test_policy_definitions::kKeyStringList,
537 list.release());
538 PolicyBundle expected;
539 expected.Get(PolicyNamespace(POLICY_DOMAIN_CHROME, std::string()))
540 .LoadFrom(&expected_policy, POLICY_LEVEL_MANDATORY,
541 POLICY_SCOPE_MACHINE);
542 return Matches(expected);
543 }
544
436 ScopedGroupPolicyRegistrySandbox registry_sandbox_; 545 ScopedGroupPolicyRegistrySandbox registry_sandbox_;
546 PGROUP_POLICY_OBJECT gpo_list_;
547 DWORD gpo_list_status_;
548 base::FilePath test_data_dir_;
437 }; 549 };
438 550
551 const char16 PolicyLoaderWinTest::kTestPolicyKey[] =
552 L"SOFTWARE\\Policies\\Chromium";
553
439 TEST_F(PolicyLoaderWinTest, HKLMOverHKCU) { 554 TEST_F(PolicyLoaderWinTest, HKLMOverHKCU) {
440 RegKey hklm_key(HKEY_LOCAL_MACHINE, kRegistryMandatorySubKey, KEY_ALL_ACCESS); 555 RegKey hklm_key(HKEY_LOCAL_MACHINE, kTestPolicyKey, KEY_ALL_ACCESS);
441 ASSERT_TRUE(hklm_key.Valid()); 556 ASSERT_TRUE(hklm_key.Valid());
442 hklm_key.WriteValue(UTF8ToUTF16(test_policy_definitions::kKeyString).c_str(), 557 hklm_key.WriteValue(UTF8ToUTF16(test_policy_definitions::kKeyString).c_str(),
443 UTF8ToUTF16("hklm").c_str()); 558 UTF8ToUTF16("hklm").c_str());
444 RegKey hkcu_key(HKEY_CURRENT_USER, kRegistryMandatorySubKey, KEY_ALL_ACCESS); 559 RegKey hkcu_key(HKEY_CURRENT_USER, kTestPolicyKey, KEY_ALL_ACCESS);
445 ASSERT_TRUE(hkcu_key.Valid()); 560 ASSERT_TRUE(hkcu_key.Valid());
446 hkcu_key.WriteValue(UTF8ToUTF16(test_policy_definitions::kKeyString).c_str(), 561 hkcu_key.WriteValue(UTF8ToUTF16(test_policy_definitions::kKeyString).c_str(),
447 UTF8ToUTF16("hkcu").c_str()); 562 UTF8ToUTF16("hkcu").c_str());
448 563
449 PolicyBundle expected; 564 PolicyBundle expected;
450 expected.Get(PolicyNamespace(POLICY_DOMAIN_CHROME, std::string())) 565 expected.Get(PolicyNamespace(POLICY_DOMAIN_CHROME, std::string()))
451 .Set(test_policy_definitions::kKeyString, 566 .Set(test_policy_definitions::kKeyString,
452 POLICY_LEVEL_MANDATORY, 567 POLICY_LEVEL_MANDATORY,
453 POLICY_SCOPE_MACHINE, 568 POLICY_SCOPE_MACHINE,
454 base::Value::CreateStringValue("hklm")); 569 base::Value::CreateStringValue("hklm"));
455 EXPECT_TRUE(Matches(expected)); 570 EXPECT_TRUE(Matches(expected));
456 } 571 }
457 572
458 TEST_F(PolicyLoaderWinTest, Load3rdPartyWithoutSchema) { 573 TEST_F(PolicyLoaderWinTest, Load3rdPartyWithoutSchema) {
459 base::DictionaryValue dict; 574 base::DictionaryValue dict;
460 dict.SetString("str", "string value"); 575 dict.SetString("str", "string value");
461 dict.SetInteger("int", 123); 576 dict.SetInteger("int", 123);
462 dict.Set("subdict", dict.DeepCopy()); 577 dict.Set("subdict", dict.DeepCopy());
463 dict.Set("subsubdict", dict.DeepCopy()); 578 dict.Set("subsubdict", dict.DeepCopy());
464 dict.Set("subsubsubdict", dict.DeepCopy()); 579 dict.Set("subsubsubdict", dict.DeepCopy());
465 580
466 base::DictionaryValue policy_dict; 581 base::DictionaryValue policy_dict;
467 policy_dict.Set("extensions.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.policy", 582 policy_dict.Set("extensions.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.policy",
468 dict.DeepCopy()); 583 dict.DeepCopy());
469 policy_dict.Set("extensions.bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb.policy", 584 policy_dict.Set("extensions.bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb.policy",
470 dict.DeepCopy()); 585 dict.DeepCopy());
471 EXPECT_TRUE(InstallValue(policy_dict, HKEY_LOCAL_MACHINE, 586 EXPECT_TRUE(InstallValue(policy_dict, HKEY_LOCAL_MACHINE,
472 kRegistryMandatorySubKey, kThirdParty)); 587 kTestPolicyKey, kThirdParty));
473 588
474 PolicyBundle expected; 589 PolicyBundle expected;
475 expected.Get(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, 590 expected.Get(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS,
476 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")) 591 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"))
477 .LoadFrom(&dict, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE); 592 .LoadFrom(&dict, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE);
478 expected.Get(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, 593 expected.Get(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS,
479 "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb")) 594 "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"))
480 .LoadFrom(&dict, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE); 595 .LoadFrom(&dict, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE);
481 EXPECT_TRUE(Matches(expected)); 596 EXPECT_TRUE(Matches(expected));
482 } 597 }
483 598
484 TEST_F(PolicyLoaderWinTest, Merge3rdPartyPolicies) { 599 TEST_F(PolicyLoaderWinTest, Merge3rdPartyPolicies) {
485 // Policy for the same extension will be provided at the 4 level/scope 600 // Policy for the same extension will be provided at the 4 level/scope
486 // combinations, to verify that they overlap as expected. 601 // combinations, to verify that they overlap as expected.
487 602
488 const string16 kPathSuffix = 603 const string16 kPathSuffix =
489 kRegistryMandatorySubKey + ASCIIToUTF16("\\3rdparty\\extensions\\merge"); 604 kTestPolicyKey + ASCIIToUTF16("\\3rdparty\\extensions\\merge");
490 605
491 const char kUserMandatory[] = "user-mandatory"; 606 const char kUserMandatory[] = "user-mandatory";
492 const char kUserRecommended[] = "user-recommended"; 607 const char kUserRecommended[] = "user-recommended";
493 const char kMachineMandatory[] = "machine-mandatory"; 608 const char kMachineMandatory[] = "machine-mandatory";
494 const char kMachineRecommended[] = "machine-recommended"; 609 const char kMachineRecommended[] = "machine-recommended";
495 610
496 base::DictionaryValue policy; 611 base::DictionaryValue policy;
497 policy.SetString("a", kMachineMandatory); 612 policy.SetString("a", kMachineMandatory);
498 EXPECT_TRUE(InstallValue(policy, HKEY_LOCAL_MACHINE, 613 EXPECT_TRUE(InstallValue(policy, HKEY_LOCAL_MACHINE,
499 kPathSuffix, kMandatory)); 614 kPathSuffix, kMandatory));
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
551 ASSERT_FALSE(encoded_list.empty()); 666 ASSERT_FALSE(encoded_list.empty());
552 base::DictionaryValue encoded_policy; 667 base::DictionaryValue encoded_policy;
553 encoded_policy.SetString("null", ""); 668 encoded_policy.SetString("null", "");
554 encoded_policy.SetString("bool", "1"); 669 encoded_policy.SetString("bool", "1");
555 encoded_policy.SetString("int", "-123"); 670 encoded_policy.SetString("int", "-123");
556 encoded_policy.SetString("double", "456.78e9"); 671 encoded_policy.SetString("double", "456.78e9");
557 encoded_policy.SetString("list", encoded_list); 672 encoded_policy.SetString("list", encoded_list);
558 encoded_policy.SetString("dict", encoded_dict); 673 encoded_policy.SetString("dict", encoded_dict);
559 674
560 const string16 kPathSuffix = 675 const string16 kPathSuffix =
561 kRegistryMandatorySubKey + ASCIIToUTF16("\\3rdparty\\extensions\\string"); 676 kTestPolicyKey + ASCIIToUTF16("\\3rdparty\\extensions\\string");
562 EXPECT_TRUE(InstallSchema(policy, HKEY_CURRENT_USER, kPathSuffix, kSchema)); 677 EXPECT_TRUE(InstallSchema(policy, HKEY_CURRENT_USER, kPathSuffix, kSchema));
563 EXPECT_TRUE( 678 EXPECT_TRUE(
564 InstallValue(encoded_policy, HKEY_CURRENT_USER, kPathSuffix, kMandatory)); 679 InstallValue(encoded_policy, HKEY_CURRENT_USER, kPathSuffix, kMandatory));
565 680
566 PolicyBundle expected; 681 PolicyBundle expected;
567 expected.Get(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "string")) 682 expected.Get(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "string"))
568 .LoadFrom(&policy, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER); 683 .LoadFrom(&policy, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER);
569 EXPECT_TRUE(Matches(expected)); 684 EXPECT_TRUE(Matches(expected));
570 } 685 }
571 686
572 TEST_F(PolicyLoaderWinTest, LoadIntegerEncodedValues) { 687 TEST_F(PolicyLoaderWinTest, LoadIntegerEncodedValues) {
573 base::DictionaryValue policy; 688 base::DictionaryValue policy;
574 policy.SetBoolean("bool", true); 689 policy.SetBoolean("bool", true);
575 policy.SetInteger("int", 123); 690 policy.SetInteger("int", 123);
576 policy.SetDouble("double", 456.0); 691 policy.SetDouble("double", 456.0);
577 692
578 base::DictionaryValue encoded_policy; 693 base::DictionaryValue encoded_policy;
579 encoded_policy.SetInteger("bool", 1); 694 encoded_policy.SetInteger("bool", 1);
580 encoded_policy.SetInteger("int", 123); 695 encoded_policy.SetInteger("int", 123);
581 encoded_policy.SetInteger("double", 456); 696 encoded_policy.SetInteger("double", 456);
582 697
583 const string16 kPathSuffix = 698 const string16 kPathSuffix =
584 kRegistryMandatorySubKey + ASCIIToUTF16("\\3rdparty\\extensions\\int"); 699 kTestPolicyKey + ASCIIToUTF16("\\3rdparty\\extensions\\int");
585 EXPECT_TRUE(InstallSchema(policy, HKEY_CURRENT_USER, kPathSuffix, kSchema)); 700 EXPECT_TRUE(InstallSchema(policy, HKEY_CURRENT_USER, kPathSuffix, kSchema));
586 EXPECT_TRUE( 701 EXPECT_TRUE(
587 InstallValue(encoded_policy, HKEY_CURRENT_USER, kPathSuffix, kMandatory)); 702 InstallValue(encoded_policy, HKEY_CURRENT_USER, kPathSuffix, kMandatory));
588 703
589 PolicyBundle expected; 704 PolicyBundle expected;
590 expected.Get(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "int")) 705 expected.Get(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "int"))
591 .LoadFrom(&policy, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER); 706 .LoadFrom(&policy, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER);
592 EXPECT_TRUE(Matches(expected)); 707 EXPECT_TRUE(Matches(expected));
593 } 708 }
594 709
595 TEST_F(PolicyLoaderWinTest, DefaultPropertySchemaType) { 710 TEST_F(PolicyLoaderWinTest, DefaultPropertySchemaType) {
596 // Build a schema for an "object" with a default schema for its properties. 711 // Build a schema for an "object" with a default schema for its properties.
597 base::DictionaryValue default_schema; 712 base::DictionaryValue default_schema;
598 default_schema.SetString(schema::kType, "number"); 713 default_schema.SetString(schema::kType, "number");
599 base::DictionaryValue integer_schema; 714 base::DictionaryValue integer_schema;
600 integer_schema.SetString(schema::kType, "integer"); 715 integer_schema.SetString(schema::kType, "integer");
601 base::DictionaryValue properties; 716 base::DictionaryValue properties;
602 properties.Set("special-int1", integer_schema.DeepCopy()); 717 properties.Set("special-int1", integer_schema.DeepCopy());
603 properties.Set("special-int2", integer_schema.DeepCopy()); 718 properties.Set("special-int2", integer_schema.DeepCopy());
604 base::DictionaryValue schema; 719 base::DictionaryValue schema;
605 schema.SetString(schema::kType, "object"); 720 schema.SetString(schema::kType, "object");
606 schema.Set(schema::kProperties, properties.DeepCopy()); 721 schema.Set(schema::kProperties, properties.DeepCopy());
607 schema.Set(schema::kAdditionalProperties, default_schema.DeepCopy()); 722 schema.Set(schema::kAdditionalProperties, default_schema.DeepCopy());
608 723
609 const string16 kPathSuffix = 724 const string16 kPathSuffix =
610 kRegistryMandatorySubKey + ASCIIToUTF16("\\3rdparty\\extensions\\test"); 725 kTestPolicyKey + ASCIIToUTF16("\\3rdparty\\extensions\\test");
611 EXPECT_TRUE(WriteSchema(schema, HKEY_CURRENT_USER, kPathSuffix, kSchema)); 726 EXPECT_TRUE(WriteSchema(schema, HKEY_CURRENT_USER, kPathSuffix, kSchema));
612 727
613 // Write some test values. 728 // Write some test values.
614 base::DictionaryValue policy; 729 base::DictionaryValue policy;
615 // These special values have a specific schema for them. 730 // These special values have a specific schema for them.
616 policy.SetInteger("special-int1", 123); 731 policy.SetInteger("special-int1", 123);
617 policy.SetString("special-int2", "-456"); 732 policy.SetString("special-int2", "-456");
618 // Other values default to be loaded as doubles. 733 // Other values default to be loaded as doubles.
619 policy.SetInteger("double1", 789.0); 734 policy.SetInteger("double1", 789.0);
620 policy.SetString("double2", "123.456e7"); 735 policy.SetString("double2", "123.456e7");
621 policy.SetString("invalid", "omg"); 736 policy.SetString("invalid", "omg");
622 EXPECT_TRUE(InstallValue(policy, HKEY_CURRENT_USER, kPathSuffix, kMandatory)); 737 EXPECT_TRUE(InstallValue(policy, HKEY_CURRENT_USER, kPathSuffix, kMandatory));
623 738
624 base::DictionaryValue expected_policy; 739 base::DictionaryValue expected_policy;
625 expected_policy.SetInteger("special-int1", 123); 740 expected_policy.SetInteger("special-int1", 123);
626 expected_policy.SetInteger("special-int2", -456); 741 expected_policy.SetInteger("special-int2", -456);
627 expected_policy.SetDouble("double1", 789.0); 742 expected_policy.SetDouble("double1", 789.0);
628 expected_policy.SetDouble("double2", 123.456e7); 743 expected_policy.SetDouble("double2", 123.456e7);
744 expected_policy.Set("invalid", base::Value::CreateNullValue());
629 PolicyBundle expected; 745 PolicyBundle expected;
630 expected.Get(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "test")) 746 expected.Get(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "test"))
631 .LoadFrom(&expected_policy, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER); 747 .LoadFrom(&expected_policy, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER);
632 EXPECT_TRUE(Matches(expected)); 748 EXPECT_TRUE(Matches(expected));
633 } 749 }
634 750
751 TEST_F(PolicyLoaderWinTest, AppliedPolicyNotPresent) {
752 InstallRegistrySentinel();
753 gpo_list_ = NULL;
754 gpo_list_status_ = ERROR_SUCCESS;
755
756 PolicyBundle empty;
757 EXPECT_TRUE(Matches(empty));
758 }
759
760 TEST_F(PolicyLoaderWinTest, AppliedPolicyEmpty) {
761 InstallRegistrySentinel();
762 base::FilePath gpo_dir(test_data_dir_.AppendASCII("empty"));
763 GROUP_POLICY_OBJECT gpo;
764 InitGPO(&gpo, 0, gpo_dir, NULL, NULL);
765 gpo_list_ = &gpo;
766 gpo_list_status_ = ERROR_SUCCESS;
767
768 PolicyBundle empty;
769 EXPECT_TRUE(Matches(empty));
770 }
771
772 TEST_F(PolicyLoaderWinTest, AppliedPolicyNonExistingFile) {
773 InstallRegistrySentinel();
774 GROUP_POLICY_OBJECT gpo;
775 InitGPO(&gpo, 0, test_data_dir_, NULL, NULL);
776 gpo_list_ = &gpo;
777 gpo_list_status_ = ERROR_SUCCESS;
778
779 EXPECT_TRUE(MatchesRegistrySentinel());
780 }
781
782 TEST_F(PolicyLoaderWinTest, AppliedPolicyBadPath) {
783 InstallRegistrySentinel();
784 base::FilePath gpo_dir(test_data_dir_.AppendASCII("bad"));
785 GROUP_POLICY_OBJECT gpo;
786 InitGPO(&gpo, 0, gpo_dir, NULL, NULL);
787 gpo_list_ = &gpo;
788 gpo_list_status_ = ERROR_SUCCESS;
789
790 EXPECT_TRUE(MatchesRegistrySentinel());
791 }
792
793 TEST_F(PolicyLoaderWinTest, AppliedPolicyPresent) {
794 InstallRegistrySentinel();
795 base::FilePath gpo_dir(test_data_dir_.AppendASCII("test1"));
796 GROUP_POLICY_OBJECT gpo;
797 InitGPO(&gpo, 0, gpo_dir, NULL, NULL);
798 gpo_list_ = &gpo;
799 gpo_list_status_ = ERROR_SUCCESS;
800
801 EXPECT_TRUE(MatchesTestBundle());
802 }
803
804 TEST_F(PolicyLoaderWinTest, AppliedPolicyMerged) {
805 InstallRegistrySentinel();
806 base::FilePath gpo1_dir(test_data_dir_.AppendASCII("test2"));
807 base::FilePath gpo2_dir(test_data_dir_.AppendASCII("test1"));
808 GROUP_POLICY_OBJECT gpo1;
809 GROUP_POLICY_OBJECT gpo2;
810 InitGPO(&gpo1, 0, gpo1_dir, &gpo2, NULL);
811 InitGPO(&gpo2, 0, gpo2_dir, NULL, &gpo1);
812 gpo_list_ = &gpo1;
813 gpo_list_status_ = ERROR_SUCCESS;
814
815 EXPECT_TRUE(MatchesTestBundle());
816 }
817
818 TEST_F(PolicyLoaderWinTest, AppliedPolicyDisabled) {
819 InstallRegistrySentinel();
820 base::FilePath gpo1_dir(test_data_dir_.AppendASCII("test1"));
821 base::FilePath gpo2_dir(test_data_dir_.AppendASCII("test2"));
822 GROUP_POLICY_OBJECT gpo1;
823 GROUP_POLICY_OBJECT gpo2;
824 InitGPO(&gpo1, 0, gpo1_dir, &gpo2, NULL);
825 InitGPO(&gpo2, GPO_FLAG_DISABLE, gpo2_dir, NULL, &gpo1);
826 gpo_list_ = &gpo1;
827 gpo_list_status_ = ERROR_SUCCESS;
828
829 EXPECT_TRUE(MatchesTestBundle());
830 }
831
832 TEST_F(PolicyLoaderWinTest, AppliedPolicyForcedPolicy) {
833 InstallRegistrySentinel();
834 base::FilePath gpo1_dir(test_data_dir_.AppendASCII("test1"));
835 base::FilePath gpo2_dir(test_data_dir_.AppendASCII("test2"));
836 GROUP_POLICY_OBJECT gpo1;
837 GROUP_POLICY_OBJECT gpo2;
838 InitGPO(&gpo1, GPO_FLAG_FORCE, gpo1_dir, &gpo2, NULL);
839 InitGPO(&gpo2, 0, gpo2_dir, NULL, &gpo1);
840 gpo_list_ = &gpo1;
841 gpo_list_status_ = ERROR_SUCCESS;
842
843 EXPECT_TRUE(MatchesTestBundle());
844 }
845
846 TEST_F(PolicyLoaderWinTest, AppliedPolicyUNCPath) {
847 InstallRegistrySentinel();
848 base::FilePath gpo_dir(test_data_dir_.AppendASCII("test1"));
849 base::FilePath unc_path(L"\\\\some_share\\GPO");
850 GROUP_POLICY_OBJECT gpo1;
851 GROUP_POLICY_OBJECT gpo2;
852 InitGPO(&gpo1, 0, gpo_dir, &gpo2, NULL);
853 InitGPO(&gpo2, 0, unc_path, NULL, &gpo1);
854 gpo_list_ = &gpo1;
855 gpo_list_status_ = ERROR_SUCCESS;
856
857 EXPECT_TRUE(MatchesRegistrySentinel());
858 }
859
635 } // namespace policy 860 } // namespace policy
OLDNEW
« no previous file with comments | « chrome/browser/policy/policy_loader_win.cc ('k') | chrome/browser/policy/policy_path_parser_win.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698