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 "components/policy/core/common/policy_service_impl.h" | 5 #include "components/policy/core/common/policy_service_impl.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/callback.h" | 9 #include "base/callback.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
11 #include "base/run_loop.h" | 11 #include "base/run_loop.h" |
12 #include "base/values.h" | 12 #include "base/values.h" |
13 #include "components/policy/core/common/external_data_fetcher.h" | 13 #include "components/policy/core/common/external_data_fetcher.h" |
14 #include "components/policy/core/common/mock_configuration_policy_provider.h" | 14 #include "components/policy/core/common/mock_configuration_policy_provider.h" |
15 #include "components/policy/core/common/mock_policy_service.h" | 15 #include "components/policy/core/common/mock_policy_service.h" |
| 16 #include "policy/policy_constants.h" |
16 #include "testing/gmock/include/gmock/gmock.h" | 17 #include "testing/gmock/include/gmock/gmock.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
18 | 19 |
19 using ::testing::AnyNumber; | 20 using ::testing::AnyNumber; |
20 using ::testing::Mock; | 21 using ::testing::Mock; |
21 using ::testing::Return; | 22 using ::testing::Return; |
22 using ::testing::_; | 23 using ::testing::_; |
23 | 24 |
24 namespace policy { | 25 namespace policy { |
25 | 26 |
26 namespace { | 27 namespace { |
27 | 28 |
28 const char kExtension[] = "extension-id"; | 29 const char kExtension[] = "extension-id"; |
29 const char kSameLevelPolicy[] = "policy-same-level-and-scope"; | 30 const char kSameLevelPolicy[] = "policy-same-level-and-scope"; |
30 const char kDiffLevelPolicy[] = "chrome-diff-level-and-scope"; | 31 const char kDiffLevelPolicy[] = "chrome-diff-level-and-scope"; |
31 | 32 |
32 void SetPolicyMapValue(const std::string& key, | |
33 const std::string& value, | |
34 PolicyBundle* bundle) { | |
35 bundle->Get(PolicyNamespace(POLICY_DOMAIN_CHROME, std::string())) | |
36 .Set(key, | |
37 POLICY_LEVEL_MANDATORY, | |
38 POLICY_SCOPE_USER, | |
39 new base::StringValue(value), | |
40 NULL); | |
41 } | |
42 | |
43 // Helper to compare the arguments to an EXPECT_CALL of OnPolicyUpdated() with | 33 // Helper to compare the arguments to an EXPECT_CALL of OnPolicyUpdated() with |
44 // their expected values. | 34 // their expected values. |
45 MATCHER_P(PolicyEquals, expected, "") { | 35 MATCHER_P(PolicyEquals, expected, "") { |
46 return arg.Equals(*expected); | 36 return arg.Equals(*expected); |
47 } | 37 } |
48 | 38 |
49 // Helper to compare the arguments to an EXPECT_CALL of OnPolicyValueUpdated() | 39 // Helper to compare the arguments to an EXPECT_CALL of OnPolicyValueUpdated() |
50 // with their expected values. | 40 // with their expected values. |
51 MATCHER_P(ValueEquals, expected, "") { | 41 MATCHER_P(ValueEquals, expected, "") { |
52 return base::Value::Equals(arg, expected); | 42 return base::Value::Equals(arg, expected); |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
115 provider2_.Init(); | 105 provider2_.Init(); |
116 | 106 |
117 policy0_.Set("pre", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 107 policy0_.Set("pre", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
118 base::Value::CreateIntegerValue(13), NULL); | 108 base::Value::CreateIntegerValue(13), NULL); |
119 provider0_.UpdateChromePolicy(policy0_); | 109 provider0_.UpdateChromePolicy(policy0_); |
120 | 110 |
121 PolicyServiceImpl::Providers providers; | 111 PolicyServiceImpl::Providers providers; |
122 providers.push_back(&provider0_); | 112 providers.push_back(&provider0_); |
123 providers.push_back(&provider1_); | 113 providers.push_back(&provider1_); |
124 providers.push_back(&provider2_); | 114 providers.push_back(&provider2_); |
125 policy_service_.reset(new PolicyServiceImpl( | 115 policy_service_.reset(new PolicyServiceImpl(providers)); |
126 providers, PolicyServiceImpl::PreprocessCallback())); | |
127 } | 116 } |
128 | 117 |
129 virtual void TearDown() OVERRIDE { | 118 virtual void TearDown() OVERRIDE { |
130 provider0_.Shutdown(); | 119 provider0_.Shutdown(); |
131 provider1_.Shutdown(); | 120 provider1_.Shutdown(); |
132 provider2_.Shutdown(); | 121 provider2_.Shutdown(); |
133 } | 122 } |
134 | 123 |
135 MOCK_METHOD2(OnPolicyValueUpdated, void(const base::Value*, | 124 MOCK_METHOD2(OnPolicyValueUpdated, void(const base::Value*, |
136 const base::Value*)); | 125 const base::Value*)); |
(...skipping 390 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
527 // For policies with different levels and scopes, the highest priority | 516 // For policies with different levels and scopes, the highest priority |
528 // level/scope combination takes precedence, on every namespace. | 517 // level/scope combination takes precedence, on every namespace. |
529 expected.Set(kDiffLevelPolicy, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE, | 518 expected.Set(kDiffLevelPolicy, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE, |
530 base::Value::CreateStringValue("bundle2"), NULL); | 519 base::Value::CreateStringValue("bundle2"), NULL); |
531 EXPECT_TRUE(policy_service_->GetPolicies( | 520 EXPECT_TRUE(policy_service_->GetPolicies( |
532 PolicyNamespace(POLICY_DOMAIN_CHROME, std::string())).Equals(expected)); | 521 PolicyNamespace(POLICY_DOMAIN_CHROME, std::string())).Equals(expected)); |
533 EXPECT_TRUE(policy_service_->GetPolicies( | 522 EXPECT_TRUE(policy_service_->GetPolicies( |
534 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, kExtension)).Equals(expected)); | 523 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, kExtension)).Equals(expected)); |
535 } | 524 } |
536 | 525 |
537 TEST_F(PolicyServiceTest, PolicyPreprocessing) { | |
538 // Reset the PolicyServiceImpl to one that has the preprocessor. | |
539 PolicyServiceImpl::Providers providers; | |
540 providers.push_back(&provider0_); | |
541 policy_service_.reset(new PolicyServiceImpl( | |
542 providers, base::Bind(&SetPolicyMapValue, kSameLevelPolicy, "bar"))); | |
543 | |
544 // Set the policy value to "foo". | |
545 scoped_ptr<PolicyBundle> bundle(new PolicyBundle()); | |
546 PolicyMap& map = | |
547 bundle->Get(PolicyNamespace(POLICY_DOMAIN_CHROME, std::string())); | |
548 map.Set(kSameLevelPolicy, | |
549 POLICY_LEVEL_MANDATORY, | |
550 POLICY_SCOPE_USER, | |
551 base::Value::CreateStringValue("foo"), | |
552 NULL); | |
553 | |
554 // Push the update through the provider. | |
555 provider0_.UpdatePolicy(bundle.Pass()); | |
556 RunUntilIdle(); | |
557 | |
558 // The value should have been changed from "foo" to "bar". | |
559 const PolicyMap& actual = policy_service_->GetPolicies( | |
560 PolicyNamespace(POLICY_DOMAIN_CHROME, std::string())); | |
561 PolicyMap expected; | |
562 expected.Set(kSameLevelPolicy, | |
563 POLICY_LEVEL_MANDATORY, | |
564 POLICY_SCOPE_USER, | |
565 base::Value::CreateStringValue("bar"), | |
566 NULL); | |
567 EXPECT_TRUE(actual.Equals(expected)); | |
568 } | |
569 | |
570 TEST_F(PolicyServiceTest, IsInitializationComplete) { | 526 TEST_F(PolicyServiceTest, IsInitializationComplete) { |
571 // |provider0| has all domains initialized. | 527 // |provider0| has all domains initialized. |
572 Mock::VerifyAndClearExpectations(&provider1_); | 528 Mock::VerifyAndClearExpectations(&provider1_); |
573 Mock::VerifyAndClearExpectations(&provider2_); | 529 Mock::VerifyAndClearExpectations(&provider2_); |
574 EXPECT_CALL(provider1_, IsInitializationComplete(_)) | 530 EXPECT_CALL(provider1_, IsInitializationComplete(_)) |
575 .WillRepeatedly(Return(false)); | 531 .WillRepeatedly(Return(false)); |
576 EXPECT_CALL(provider2_, IsInitializationComplete(_)) | 532 EXPECT_CALL(provider2_, IsInitializationComplete(_)) |
577 .WillRepeatedly(Return(false)); | 533 .WillRepeatedly(Return(false)); |
578 PolicyServiceImpl::Providers providers; | 534 PolicyServiceImpl::Providers providers; |
579 providers.push_back(&provider0_); | 535 providers.push_back(&provider0_); |
580 providers.push_back(&provider1_); | 536 providers.push_back(&provider1_); |
581 providers.push_back(&provider2_); | 537 providers.push_back(&provider2_); |
582 policy_service_.reset(new PolicyServiceImpl( | 538 policy_service_.reset(new PolicyServiceImpl(providers)); |
583 providers, PolicyServiceImpl::PreprocessCallback())); | |
584 EXPECT_FALSE(policy_service_->IsInitializationComplete(POLICY_DOMAIN_CHROME)); | 539 EXPECT_FALSE(policy_service_->IsInitializationComplete(POLICY_DOMAIN_CHROME)); |
585 EXPECT_FALSE( | 540 EXPECT_FALSE( |
586 policy_service_->IsInitializationComplete(POLICY_DOMAIN_EXTENSIONS)); | 541 policy_service_->IsInitializationComplete(POLICY_DOMAIN_EXTENSIONS)); |
587 | 542 |
588 // |provider2_| still doesn't have POLICY_DOMAIN_CHROME initialized, so | 543 // |provider2_| still doesn't have POLICY_DOMAIN_CHROME initialized, so |
589 // the initialization status of that domain won't change. | 544 // the initialization status of that domain won't change. |
590 MockPolicyServiceObserver observer; | 545 MockPolicyServiceObserver observer; |
591 policy_service_->AddObserver(POLICY_DOMAIN_CHROME, &observer); | 546 policy_service_->AddObserver(POLICY_DOMAIN_CHROME, &observer); |
592 policy_service_->AddObserver(POLICY_DOMAIN_EXTENSIONS, &observer); | 547 policy_service_->AddObserver(POLICY_DOMAIN_EXTENSIONS, &observer); |
593 EXPECT_CALL(observer, OnPolicyServiceInitialized(_)).Times(0); | 548 EXPECT_CALL(observer, OnPolicyServiceInitialized(_)).Times(0); |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
641 Mock::VerifyAndClearExpectations(&observer); | 596 Mock::VerifyAndClearExpectations(&observer); |
642 EXPECT_TRUE(policy_service_->IsInitializationComplete(POLICY_DOMAIN_CHROME)); | 597 EXPECT_TRUE(policy_service_->IsInitializationComplete(POLICY_DOMAIN_CHROME)); |
643 EXPECT_TRUE( | 598 EXPECT_TRUE( |
644 policy_service_->IsInitializationComplete(POLICY_DOMAIN_EXTENSIONS)); | 599 policy_service_->IsInitializationComplete(POLICY_DOMAIN_EXTENSIONS)); |
645 | 600 |
646 // Cleanup. | 601 // Cleanup. |
647 policy_service_->RemoveObserver(POLICY_DOMAIN_CHROME, &observer); | 602 policy_service_->RemoveObserver(POLICY_DOMAIN_CHROME, &observer); |
648 policy_service_->RemoveObserver(POLICY_DOMAIN_EXTENSIONS, &observer); | 603 policy_service_->RemoveObserver(POLICY_DOMAIN_EXTENSIONS, &observer); |
649 } | 604 } |
650 | 605 |
| 606 TEST_F(PolicyServiceTest, FixDeprecatedPolicies) { |
| 607 const PolicyNamespace chrome_namespace(POLICY_DOMAIN_CHROME, std::string()); |
| 608 const PolicyNamespace extension_namespace(POLICY_DOMAIN_EXTENSIONS, "xyz"); |
| 609 |
| 610 scoped_ptr<PolicyBundle> policy_bundle(new PolicyBundle()); |
| 611 PolicyMap& policy_map = policy_bundle->Get(chrome_namespace); |
| 612 // Individual proxy policy values in the Chrome namespace should be collected |
| 613 // into a dictionary. |
| 614 policy_map.Set(key::kProxyServerMode, POLICY_LEVEL_MANDATORY, |
| 615 POLICY_SCOPE_USER, base::Value::CreateIntegerValue(3), NULL); |
| 616 |
| 617 // Both these policies should be ignored, since there's a higher priority |
| 618 // policy available. |
| 619 policy_map.Set(key::kProxyMode, POLICY_LEVEL_RECOMMENDED, POLICY_SCOPE_USER, |
| 620 base::Value::CreateStringValue("pac_script"), NULL); |
| 621 policy_map.Set(key::kProxyPacUrl, POLICY_LEVEL_RECOMMENDED, POLICY_SCOPE_USER, |
| 622 base::Value::CreateStringValue("http://example.com/wpad.dat"), |
| 623 NULL); |
| 624 |
| 625 // Add a value to a non-Chrome namespace. |
| 626 policy_bundle->Get(extension_namespace) |
| 627 .Set(key::kProxyServerMode, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 628 base::Value::CreateIntegerValue(3), NULL); |
| 629 |
| 630 // The resulting Chrome namespace map should have the collected policy. |
| 631 PolicyMap expected_chrome; |
| 632 scoped_ptr<base::DictionaryValue> expected_value(new base::DictionaryValue); |
| 633 expected_value->SetInteger(key::kProxyServerMode, 3); |
| 634 expected_chrome.Set(key::kProxySettings, POLICY_LEVEL_MANDATORY, |
| 635 POLICY_SCOPE_USER, expected_value.release(), NULL); |
| 636 |
| 637 // The resulting Extensions namespace map shouldn't have been modified. |
| 638 PolicyMap expected_extension; |
| 639 expected_extension.Set(key::kProxyServerMode, POLICY_LEVEL_MANDATORY, |
| 640 POLICY_SCOPE_USER, base::Value::CreateIntegerValue(3), |
| 641 NULL); |
| 642 |
| 643 provider0_.UpdatePolicy(policy_bundle.Pass()); |
| 644 RunUntilIdle(); |
| 645 |
| 646 EXPECT_TRUE(VerifyPolicies(chrome_namespace, expected_chrome)); |
| 647 EXPECT_TRUE(VerifyPolicies(extension_namespace, expected_extension)); |
| 648 } |
| 649 |
651 } // namespace policy | 650 } // namespace policy |
OLD | NEW |