| 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_service_impl.h" | 5 #include "chrome/browser/policy/policy_service_impl.h" |
| 6 | 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" |
| 7 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 8 #include "base/values.h" | 10 #include "base/values.h" |
| 9 #include "chrome/browser/policy/mock_configuration_policy_provider.h" | 11 #include "chrome/browser/policy/mock_configuration_policy_provider.h" |
| 10 #include "testing/gmock/include/gmock/gmock.h" | 12 #include "testing/gmock/include/gmock/gmock.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 12 | 14 |
| 13 using ::testing::AnyNumber; | 15 using ::testing::AnyNumber; |
| 14 using ::testing::Mock; | 16 using ::testing::Mock; |
| 15 using ::testing::_; | 17 using ::testing::_; |
| 16 | 18 |
| 17 namespace policy { | 19 namespace policy { |
| 18 | 20 |
| 19 namespace { | 21 namespace { |
| 20 | 22 |
| 21 class MockPolicyServiceObserver : public PolicyService::Observer { | 23 class MockPolicyServiceObserver : public PolicyService::Observer { |
| 22 public: | 24 public: |
| 23 virtual ~MockPolicyServiceObserver() {} | 25 virtual ~MockPolicyServiceObserver() {} |
| 24 MOCK_METHOD4(OnPolicyUpdated, void(PolicyDomain, | 26 MOCK_METHOD4(OnPolicyUpdated, void(PolicyDomain, |
| 25 const std::string&, | 27 const std::string&, |
| 26 const PolicyMap& previous, | 28 const PolicyMap& previous, |
| 27 const PolicyMap& current)); | 29 const PolicyMap& current)); |
| 28 }; | 30 }; |
| 29 | 31 |
| 30 // Helper to compare the arguments to an EXPECT_CALL of OnPolicyUpdated() with | 32 // Helper to compare the arguments to an EXPECT_CALL of OnPolicyUpdated() with |
| 31 // their expected values. | 33 // their expected values. |
| 32 MATCHER_P(PolicyEquals, expected, "") { | 34 MATCHER_P(PolicyEquals, expected, "") { |
| 33 return arg.Equals(*expected); | 35 return arg.Equals(*expected); |
| 34 } | 36 } |
| 35 | 37 |
| 38 // Helper to compare the arguments to an EXPECT_CALL of OnPolicyValueUpdated() |
| 39 // with their expected values. |
| 40 MATCHER_P(ValueEquals, expected, "") { |
| 41 return base::Value::Equals(arg, expected); |
| 42 } |
| 43 |
| 36 } // namespace | 44 } // namespace |
| 37 | 45 |
| 38 class PolicyServiceTest : public testing::Test { | 46 class PolicyServiceTest : public testing::Test { |
| 39 public: | 47 public: |
| 40 PolicyServiceTest() {} | 48 PolicyServiceTest() {} |
| 41 | 49 |
| 42 void SetUp() OVERRIDE { | 50 void SetUp() OVERRIDE { |
| 43 provider0_.AddMandatoryPolicy("pre", base::Value::CreateIntegerValue(13)); | 51 provider0_.AddMandatoryPolicy("pre", base::Value::CreateIntegerValue(13)); |
| 44 provider0_.SetInitializationComplete(true); | 52 provider0_.SetInitializationComplete(true); |
| 45 provider1_.SetInitializationComplete(true); | 53 provider1_.SetInitializationComplete(true); |
| 46 provider2_.SetInitializationComplete(true); | 54 provider2_.SetInitializationComplete(true); |
| 47 PolicyServiceImpl::Providers providers; | 55 PolicyServiceImpl::Providers providers; |
| 48 providers.push_back(&provider0_); | 56 providers.push_back(&provider0_); |
| 49 providers.push_back(&provider1_); | 57 providers.push_back(&provider1_); |
| 50 providers.push_back(&provider2_); | 58 providers.push_back(&provider2_); |
| 51 policy_service_.reset(new PolicyServiceImpl(providers)); | 59 policy_service_.reset(new PolicyServiceImpl(providers)); |
| 52 policy_service_->AddObserver(POLICY_DOMAIN_CHROME, "", &observer_); | |
| 53 } | 60 } |
| 54 | 61 |
| 55 void TearDown() OVERRIDE { | 62 MOCK_METHOD2(OnPolicyValueUpdated, void(const base::Value*, |
| 56 policy_service_->RemoveObserver(POLICY_DOMAIN_CHROME, "", &observer_); | 63 const base::Value*)); |
| 57 } | |
| 58 | 64 |
| 59 // Returns true if the policies for |domain|, |component_id| match |expected|. | 65 // Returns true if the policies for |domain|, |component_id| match |expected|. |
| 60 bool VerifyPolicies(PolicyDomain domain, | 66 bool VerifyPolicies(PolicyDomain domain, |
| 61 const std::string& component_id, | 67 const std::string& component_id, |
| 62 const PolicyMap& expected) { | 68 const PolicyMap& expected) { |
| 63 const PolicyMap* policies = | 69 const PolicyMap* policies = |
| 64 policy_service_->GetPolicies(domain, component_id); | 70 policy_service_->GetPolicies(domain, component_id); |
| 65 return policies && policies->Equals(expected); | 71 return policies && policies->Equals(expected); |
| 66 } | 72 } |
| 67 | 73 |
| 68 protected: | 74 protected: |
| 69 MockConfigurationPolicyProvider provider0_; | 75 MockConfigurationPolicyProvider provider0_; |
| 70 MockConfigurationPolicyProvider provider1_; | 76 MockConfigurationPolicyProvider provider1_; |
| 71 MockConfigurationPolicyProvider provider2_; | 77 MockConfigurationPolicyProvider provider2_; |
| 72 scoped_ptr<PolicyServiceImpl> policy_service_; | 78 scoped_ptr<PolicyServiceImpl> policy_service_; |
| 73 MockPolicyServiceObserver observer_; | |
| 74 | 79 |
| 75 private: | 80 private: |
| 76 DISALLOW_COPY_AND_ASSIGN(PolicyServiceTest); | 81 DISALLOW_COPY_AND_ASSIGN(PolicyServiceTest); |
| 77 }; | 82 }; |
| 78 | 83 |
| 79 TEST_F(PolicyServiceTest, LoadsPoliciesBeforeProvidersRefresh) { | 84 TEST_F(PolicyServiceTest, LoadsPoliciesBeforeProvidersRefresh) { |
| 80 PolicyMap expected; | 85 PolicyMap expected; |
| 81 expected.Set("pre", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 86 expected.Set("pre", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 82 base::Value::CreateIntegerValue(13)); | 87 base::Value::CreateIntegerValue(13)); |
| 83 EXPECT_TRUE(VerifyPolicies(POLICY_DOMAIN_CHROME, "", expected)); | 88 EXPECT_TRUE(VerifyPolicies(POLICY_DOMAIN_CHROME, "", expected)); |
| 84 } | 89 } |
| 85 | 90 |
| 86 TEST_F(PolicyServiceTest, NotifyObservers) { | 91 TEST_F(PolicyServiceTest, NotifyObservers) { |
| 92 MockPolicyServiceObserver observer; |
| 93 policy_service_->AddObserver(POLICY_DOMAIN_CHROME, "", &observer); |
| 94 |
| 87 PolicyMap expectedPrevious; | 95 PolicyMap expectedPrevious; |
| 88 expectedPrevious.Set("pre", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 96 expectedPrevious.Set("pre", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 89 base::Value::CreateIntegerValue(13)); | 97 base::Value::CreateIntegerValue(13)); |
| 90 | 98 |
| 91 PolicyMap expectedCurrent; | 99 PolicyMap expectedCurrent; |
| 92 expectedCurrent.CopyFrom(expectedPrevious); | 100 expectedCurrent.CopyFrom(expectedPrevious); |
| 93 expectedCurrent.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 101 expectedCurrent.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 94 base::Value::CreateIntegerValue(123)); | 102 base::Value::CreateIntegerValue(123)); |
| 95 provider0_.AddMandatoryPolicy("aaa", base::Value::CreateIntegerValue(123)); | 103 provider0_.AddMandatoryPolicy("aaa", base::Value::CreateIntegerValue(123)); |
| 96 EXPECT_CALL(observer_, OnPolicyUpdated(POLICY_DOMAIN_CHROME, "", | 104 EXPECT_CALL(observer, OnPolicyUpdated(POLICY_DOMAIN_CHROME, "", |
| 97 PolicyEquals(&expectedPrevious), | 105 PolicyEquals(&expectedPrevious), |
| 98 PolicyEquals(&expectedCurrent))); | 106 PolicyEquals(&expectedCurrent))); |
| 99 provider0_.RefreshPolicies(); | 107 provider0_.RefreshPolicies(); |
| 100 Mock::VerifyAndClearExpectations(&observer_); | 108 Mock::VerifyAndClearExpectations(&observer); |
| 101 | 109 |
| 102 // No changes. | 110 // No changes. |
| 103 EXPECT_CALL(observer_, OnPolicyUpdated(_, _, _, _)).Times(0); | 111 EXPECT_CALL(observer, OnPolicyUpdated(_, _, _, _)).Times(0); |
| 104 provider0_.RefreshPolicies(); | 112 provider0_.RefreshPolicies(); |
| 105 Mock::VerifyAndClearExpectations(&observer_); | 113 Mock::VerifyAndClearExpectations(&observer); |
| 106 EXPECT_TRUE(VerifyPolicies(POLICY_DOMAIN_CHROME, "", expectedCurrent)); | 114 EXPECT_TRUE(VerifyPolicies(POLICY_DOMAIN_CHROME, "", expectedCurrent)); |
| 107 | 115 |
| 108 // New policy. | 116 // New policy. |
| 109 expectedPrevious.CopyFrom(expectedCurrent); | 117 expectedPrevious.CopyFrom(expectedCurrent); |
| 110 expectedCurrent.Set("bbb", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 118 expectedCurrent.Set("bbb", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 111 base::Value::CreateIntegerValue(456)); | 119 base::Value::CreateIntegerValue(456)); |
| 112 provider0_.AddMandatoryPolicy("bbb", base::Value::CreateIntegerValue(456)); | 120 provider0_.AddMandatoryPolicy("bbb", base::Value::CreateIntegerValue(456)); |
| 113 EXPECT_CALL(observer_, OnPolicyUpdated(POLICY_DOMAIN_CHROME, "", | 121 EXPECT_CALL(observer, OnPolicyUpdated(POLICY_DOMAIN_CHROME, "", |
| 114 PolicyEquals(&expectedPrevious), | 122 PolicyEquals(&expectedPrevious), |
| 115 PolicyEquals(&expectedCurrent))); | 123 PolicyEquals(&expectedCurrent))); |
| 116 provider0_.RefreshPolicies(); | 124 provider0_.RefreshPolicies(); |
| 117 Mock::VerifyAndClearExpectations(&observer_); | 125 Mock::VerifyAndClearExpectations(&observer); |
| 118 | 126 |
| 119 // Removed policy. | 127 // Removed policy. |
| 120 expectedPrevious.CopyFrom(expectedCurrent); | 128 expectedPrevious.CopyFrom(expectedCurrent); |
| 121 expectedCurrent.Erase("bbb"); | 129 expectedCurrent.Erase("bbb"); |
| 122 provider0_.RemovePolicy("bbb"); | 130 provider0_.RemovePolicy("bbb"); |
| 123 EXPECT_CALL(observer_, OnPolicyUpdated(POLICY_DOMAIN_CHROME, "", | 131 EXPECT_CALL(observer, OnPolicyUpdated(POLICY_DOMAIN_CHROME, "", |
| 124 PolicyEquals(&expectedPrevious), | 132 PolicyEquals(&expectedPrevious), |
| 125 PolicyEquals(&expectedCurrent))); | 133 PolicyEquals(&expectedCurrent))); |
| 126 provider0_.RefreshPolicies(); | 134 provider0_.RefreshPolicies(); |
| 127 Mock::VerifyAndClearExpectations(&observer_); | 135 Mock::VerifyAndClearExpectations(&observer); |
| 128 | 136 |
| 129 // Changed policy. | 137 // Changed policy. |
| 130 expectedPrevious.CopyFrom(expectedCurrent); | 138 expectedPrevious.CopyFrom(expectedCurrent); |
| 131 expectedCurrent.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 139 expectedCurrent.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 132 base::Value::CreateIntegerValue(789)); | 140 base::Value::CreateIntegerValue(789)); |
| 133 provider0_.AddMandatoryPolicy("aaa", base::Value::CreateIntegerValue(789)); | 141 provider0_.AddMandatoryPolicy("aaa", base::Value::CreateIntegerValue(789)); |
| 134 EXPECT_CALL(observer_, OnPolicyUpdated(POLICY_DOMAIN_CHROME, "", | 142 EXPECT_CALL(observer, OnPolicyUpdated(POLICY_DOMAIN_CHROME, "", |
| 135 PolicyEquals(&expectedPrevious), | 143 PolicyEquals(&expectedPrevious), |
| 136 PolicyEquals(&expectedCurrent))); | 144 PolicyEquals(&expectedCurrent))); |
| 137 provider0_.RefreshPolicies(); | 145 provider0_.RefreshPolicies(); |
| 138 Mock::VerifyAndClearExpectations(&observer_); | 146 Mock::VerifyAndClearExpectations(&observer); |
| 139 | 147 |
| 140 // No changes again. | 148 // No changes again. |
| 141 EXPECT_CALL(observer_, OnPolicyUpdated(_, _, _, _)).Times(0); | 149 EXPECT_CALL(observer, OnPolicyUpdated(_, _, _, _)).Times(0); |
| 142 provider0_.RefreshPolicies(); | 150 provider0_.RefreshPolicies(); |
| 143 Mock::VerifyAndClearExpectations(&observer_); | 151 Mock::VerifyAndClearExpectations(&observer); |
| 144 EXPECT_TRUE(VerifyPolicies(POLICY_DOMAIN_CHROME, "", expectedCurrent)); | 152 EXPECT_TRUE(VerifyPolicies(POLICY_DOMAIN_CHROME, "", expectedCurrent)); |
| 153 |
| 154 policy_service_->RemoveObserver(POLICY_DOMAIN_CHROME, "", &observer); |
| 145 } | 155 } |
| 146 | 156 |
| 147 TEST_F(PolicyServiceTest, Priorities) { | 157 TEST_F(PolicyServiceTest, Priorities) { |
| 148 PolicyMap expected; | 158 PolicyMap expected; |
| 149 expected.Set("pre", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 159 expected.Set("pre", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 150 base::Value::CreateIntegerValue(13)); | 160 base::Value::CreateIntegerValue(13)); |
| 151 EXPECT_CALL(observer_, OnPolicyUpdated(_, _, _, _)).Times(AnyNumber()); | |
| 152 | |
| 153 expected.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 161 expected.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 154 base::Value::CreateIntegerValue(0)); | 162 base::Value::CreateIntegerValue(0)); |
| 155 provider0_.AddMandatoryPolicy("aaa", base::Value::CreateIntegerValue(0)); | 163 provider0_.AddMandatoryPolicy("aaa", base::Value::CreateIntegerValue(0)); |
| 156 provider1_.AddMandatoryPolicy("aaa", base::Value::CreateIntegerValue(1)); | 164 provider1_.AddMandatoryPolicy("aaa", base::Value::CreateIntegerValue(1)); |
| 157 provider2_.AddMandatoryPolicy("aaa", base::Value::CreateIntegerValue(2)); | 165 provider2_.AddMandatoryPolicy("aaa", base::Value::CreateIntegerValue(2)); |
| 158 provider0_.RefreshPolicies(); | 166 provider0_.RefreshPolicies(); |
| 159 provider1_.RefreshPolicies(); | 167 provider1_.RefreshPolicies(); |
| 160 provider2_.RefreshPolicies(); | 168 provider2_.RefreshPolicies(); |
| 161 EXPECT_TRUE(VerifyPolicies(POLICY_DOMAIN_CHROME, "", expected)); | 169 EXPECT_TRUE(VerifyPolicies(POLICY_DOMAIN_CHROME, "", expected)); |
| 162 | 170 |
| 163 expected.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 171 expected.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 164 base::Value::CreateIntegerValue(1)); | 172 base::Value::CreateIntegerValue(1)); |
| 165 provider0_.RemovePolicy("aaa"); | 173 provider0_.RemovePolicy("aaa"); |
| 166 provider0_.RefreshPolicies(); | 174 provider0_.RefreshPolicies(); |
| 167 EXPECT_TRUE(VerifyPolicies(POLICY_DOMAIN_CHROME, "", expected)); | 175 EXPECT_TRUE(VerifyPolicies(POLICY_DOMAIN_CHROME, "", expected)); |
| 168 | 176 |
| 169 expected.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 177 expected.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 170 base::Value::CreateIntegerValue(2)); | 178 base::Value::CreateIntegerValue(2)); |
| 171 provider1_.AddRecommendedPolicy("aaa", base::Value::CreateIntegerValue(1)); | 179 provider1_.AddRecommendedPolicy("aaa", base::Value::CreateIntegerValue(1)); |
| 172 provider1_.RefreshPolicies(); | 180 provider1_.RefreshPolicies(); |
| 173 EXPECT_TRUE(VerifyPolicies(POLICY_DOMAIN_CHROME, "", expected)); | 181 EXPECT_TRUE(VerifyPolicies(POLICY_DOMAIN_CHROME, "", expected)); |
| 174 } | 182 } |
| 175 | 183 |
| 184 TEST_F(PolicyServiceTest, PolicyChangeRegistrar) { |
| 185 scoped_ptr<PolicyChangeRegistrar> registrar( |
| 186 new PolicyChangeRegistrar( |
| 187 policy_service_.get(), POLICY_DOMAIN_CHROME, "")); |
| 188 |
| 189 // Starting to observe existing policies doesn't trigger a notification. |
| 190 EXPECT_CALL(*this, OnPolicyValueUpdated(_, _)).Times(0); |
| 191 registrar->Observe("pre", base::Bind( |
| 192 &PolicyServiceTest::OnPolicyValueUpdated, |
| 193 base::Unretained(this))); |
| 194 registrar->Observe("aaa", base::Bind( |
| 195 &PolicyServiceTest::OnPolicyValueUpdated, |
| 196 base::Unretained(this))); |
| 197 Mock::VerifyAndClearExpectations(this); |
| 198 |
| 199 // Changing it now triggers a notification. |
| 200 base::FundamentalValue kValue0(0); |
| 201 EXPECT_CALL(*this, OnPolicyValueUpdated(NULL, ValueEquals(&kValue0))); |
| 202 provider0_.AddMandatoryPolicy("aaa", kValue0.DeepCopy()); |
| 203 provider0_.RefreshPolicies(); |
| 204 Mock::VerifyAndClearExpectations(this); |
| 205 |
| 206 // Changing other values doesn't trigger a notification. |
| 207 EXPECT_CALL(*this, OnPolicyValueUpdated(_, _)).Times(0); |
| 208 provider0_.AddMandatoryPolicy("bbb", kValue0.DeepCopy()); |
| 209 provider0_.RefreshPolicies(); |
| 210 Mock::VerifyAndClearExpectations(this); |
| 211 |
| 212 // Modifying the value triggers a notification. |
| 213 base::FundamentalValue kValue1(1); |
| 214 EXPECT_CALL(*this, OnPolicyValueUpdated(ValueEquals(&kValue0), |
| 215 ValueEquals(&kValue1))); |
| 216 provider0_.AddMandatoryPolicy("aaa", kValue1.DeepCopy()); |
| 217 provider0_.RefreshPolicies(); |
| 218 Mock::VerifyAndClearExpectations(this); |
| 219 |
| 220 // Removing the value triggers a notification. |
| 221 EXPECT_CALL(*this, OnPolicyValueUpdated(ValueEquals(&kValue1), NULL)); |
| 222 provider0_.RemovePolicy("aaa"); |
| 223 provider0_.RefreshPolicies(); |
| 224 Mock::VerifyAndClearExpectations(this); |
| 225 |
| 226 // No more notifications after destroying the registrar. |
| 227 EXPECT_CALL(*this, OnPolicyValueUpdated(_, _)).Times(0); |
| 228 registrar.reset(); |
| 229 provider0_.AddMandatoryPolicy("aaa", kValue1.DeepCopy()); |
| 230 provider0_.AddMandatoryPolicy("pre", kValue1.DeepCopy()); |
| 231 provider0_.RefreshPolicies(); |
| 232 Mock::VerifyAndClearExpectations(this); |
| 233 } |
| 234 |
| 176 } // namespace policy | 235 } // namespace policy |
| OLD | NEW |