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_service_impl.h" | 5 #include "chrome/browser/policy/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/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
| 11 #include "base/values.h" | 11 #include "base/values.h" |
| 12 #include "chrome/browser/policy/mock_configuration_policy_provider.h" | 12 #include "chrome/browser/policy/mock_configuration_policy_provider.h" |
| 13 #include "content/public/browser/browser_thread.h" | 13 #include "content/public/browser/browser_thread.h" |
| 14 #include "content/test/test_browser_thread.h" | 14 #include "content/test/test_browser_thread.h" |
| 15 #include "testing/gmock/include/gmock/gmock.h" | 15 #include "testing/gmock/include/gmock/gmock.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 | 17 |
| 18 using ::testing::AnyNumber; | 18 using ::testing::AnyNumber; |
| 19 using ::testing::Mock; | 19 using ::testing::Mock; |
| 20 using ::testing::Return; | 20 using ::testing::Return; |
| 21 using ::testing::_; | 21 using ::testing::_; |
| 22 | 22 |
| 23 namespace policy { | 23 namespace policy { |
| 24 | 24 |
| 25 namespace { | 25 namespace { |
| 26 | 26 |
| 27 const char kExtension[] = "extension-id"; | |
| 28 const char kSameLevelPolicy[] = "policy-same-level-and-scope"; | |
| 29 const char kDiffLevelPolicy[] = "chrome-diff-level-and-scope"; | |
| 30 | |
| 27 class MockPolicyServiceObserver : public PolicyService::Observer { | 31 class MockPolicyServiceObserver : public PolicyService::Observer { |
| 28 public: | 32 public: |
| 29 virtual ~MockPolicyServiceObserver() {} | 33 virtual ~MockPolicyServiceObserver() {} |
| 30 MOCK_METHOD4(OnPolicyUpdated, void(PolicyDomain, | 34 MOCK_METHOD4(OnPolicyUpdated, void(PolicyDomain, |
| 31 const std::string&, | 35 const std::string&, |
| 32 const PolicyMap& previous, | 36 const PolicyMap& previous, |
| 33 const PolicyMap& current)); | 37 const PolicyMap& current)); |
| 34 }; | 38 }; |
| 35 | 39 |
| 36 // Helper to compare the arguments to an EXPECT_CALL of OnPolicyUpdated() with | 40 // Helper to compare the arguments to an EXPECT_CALL of OnPolicyUpdated() with |
| 37 // their expected values. | 41 // their expected values. |
| 38 MATCHER_P(PolicyEquals, expected, "") { | 42 MATCHER_P(PolicyEquals, expected, "") { |
| 39 return arg.Equals(*expected); | 43 return arg.Equals(*expected); |
| 40 } | 44 } |
| 41 | 45 |
| 42 // Helper to compare the arguments to an EXPECT_CALL of OnPolicyValueUpdated() | 46 // Helper to compare the arguments to an EXPECT_CALL of OnPolicyValueUpdated() |
| 43 // with their expected values. | 47 // with their expected values. |
| 44 MATCHER_P(ValueEquals, expected, "") { | 48 MATCHER_P(ValueEquals, expected, "") { |
| 45 return base::Value::Equals(arg, expected); | 49 return base::Value::Equals(arg, expected); |
| 46 } | 50 } |
| 47 | 51 |
| 52 // Helper that fills |bundle| with test policies. | |
| 53 void AddTestPolicies(PolicyBundle* bundle, const char* value, | |
| 54 PolicyLevel level, PolicyScope scope) { | |
|
Mattias Nissler (ping if slow)
2012/05/14 13:59:37
Individual lines for parameter declarations. bundl
Joao da Silva
2012/05/14 15:26:51
Done.
| |
| 55 PolicyMap* policy_map = &bundle->Get(POLICY_DOMAIN_CHROME, ""); | |
| 56 policy_map->Set(kSameLevelPolicy, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | |
| 57 base::Value::CreateStringValue(value)); | |
| 58 policy_map->Set(kDiffLevelPolicy, level, scope, | |
| 59 base::Value::CreateStringValue(value)); | |
| 60 policy_map = &bundle->Get(POLICY_DOMAIN_EXTENSIONS, kExtension); | |
| 61 policy_map->Set(kSameLevelPolicy, POLICY_LEVEL_MANDATORY, | |
| 62 POLICY_SCOPE_USER, base::Value::CreateStringValue(value)); | |
| 63 policy_map->Set(kDiffLevelPolicy, level, scope, | |
| 64 base::Value::CreateStringValue(value)); | |
| 65 } | |
| 66 | |
| 48 } // namespace | 67 } // namespace |
| 49 | 68 |
| 50 class PolicyServiceTest : public testing::Test { | 69 class PolicyServiceTest : public testing::Test { |
| 51 public: | 70 public: |
| 52 PolicyServiceTest() {} | 71 PolicyServiceTest() {} |
| 53 | 72 |
| 54 void SetUp() OVERRIDE { | 73 void SetUp() OVERRIDE { |
| 55 EXPECT_CALL(provider0_, ProvideInternal(_)) | |
| 56 .WillRepeatedly(CopyPolicyMap(&policy0_)); | |
| 57 EXPECT_CALL(provider1_, ProvideInternal(_)) | |
| 58 .WillRepeatedly(CopyPolicyMap(&policy1_)); | |
| 59 EXPECT_CALL(provider2_, ProvideInternal(_)) | |
| 60 .WillRepeatedly(CopyPolicyMap(&policy2_)); | |
| 61 | |
| 62 EXPECT_CALL(provider0_, IsInitializationComplete()) | 74 EXPECT_CALL(provider0_, IsInitializationComplete()) |
| 63 .WillRepeatedly(Return(true)); | 75 .WillRepeatedly(Return(true)); |
| 64 EXPECT_CALL(provider1_, IsInitializationComplete()) | 76 EXPECT_CALL(provider1_, IsInitializationComplete()) |
| 65 .WillRepeatedly(Return(true)); | 77 .WillRepeatedly(Return(true)); |
| 66 EXPECT_CALL(provider2_, IsInitializationComplete()) | 78 EXPECT_CALL(provider2_, IsInitializationComplete()) |
| 67 .WillRepeatedly(Return(true)); | 79 .WillRepeatedly(Return(true)); |
| 68 | 80 |
| 69 policy0_.Set("pre", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 81 policy0_.Set("pre", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 70 base::Value::CreateIntegerValue(13)); | 82 base::Value::CreateIntegerValue(13)); |
| 83 provider0_.UpdateChromePolicy(policy0_); | |
| 71 | 84 |
| 72 PolicyServiceImpl::Providers providers; | 85 PolicyServiceImpl::Providers providers; |
| 73 providers.push_back(&provider0_); | 86 providers.push_back(&provider0_); |
| 74 providers.push_back(&provider1_); | 87 providers.push_back(&provider1_); |
| 75 providers.push_back(&provider2_); | 88 providers.push_back(&provider2_); |
| 76 policy_service_.reset(new PolicyServiceImpl(providers)); | 89 policy_service_.reset(new PolicyServiceImpl(providers)); |
| 77 } | 90 } |
| 78 | 91 |
| 79 MOCK_METHOD2(OnPolicyValueUpdated, void(const base::Value*, | 92 MOCK_METHOD2(OnPolicyValueUpdated, void(const base::Value*, |
| 80 const base::Value*)); | 93 const base::Value*)); |
| 81 | 94 |
| 82 MOCK_METHOD0(OnPolicyRefresh, void()); | 95 MOCK_METHOD0(OnPolicyRefresh, void()); |
| 83 | 96 |
| 84 // Returns true if the policies for |domain|, |component_id| match |expected|. | 97 // Returns true if the policies for |domain|, |component_id| match |expected|. |
| 85 bool VerifyPolicies(PolicyDomain domain, | 98 bool VerifyPolicies(PolicyDomain domain, |
| 86 const std::string& component_id, | 99 const std::string& component_id, |
| 87 const PolicyMap& expected) { | 100 const PolicyMap& expected) { |
| 88 const PolicyMap* policies = | 101 return policy_service_->GetPolicies(domain, component_id).Equals(expected); |
| 89 policy_service_->GetPolicies(domain, component_id); | |
| 90 return policies && policies->Equals(expected); | |
| 91 } | 102 } |
| 92 | 103 |
| 93 protected: | 104 protected: |
| 94 MockConfigurationPolicyProvider provider0_; | 105 MockConfigurationPolicyProvider provider0_; |
| 95 MockConfigurationPolicyProvider provider1_; | 106 MockConfigurationPolicyProvider provider1_; |
| 96 MockConfigurationPolicyProvider provider2_; | 107 MockConfigurationPolicyProvider provider2_; |
| 97 PolicyMap policy0_; | 108 PolicyMap policy0_; |
| 98 PolicyMap policy1_; | 109 PolicyMap policy1_; |
| 99 PolicyMap policy2_; | 110 PolicyMap policy2_; |
| 100 scoped_ptr<PolicyServiceImpl> policy_service_; | 111 scoped_ptr<PolicyServiceImpl> policy_service_; |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 120 | 131 |
| 121 PolicyMap expectedCurrent; | 132 PolicyMap expectedCurrent; |
| 122 expectedCurrent.CopyFrom(expectedPrevious); | 133 expectedCurrent.CopyFrom(expectedPrevious); |
| 123 expectedCurrent.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 134 expectedCurrent.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 124 base::Value::CreateIntegerValue(123)); | 135 base::Value::CreateIntegerValue(123)); |
| 125 policy0_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 136 policy0_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 126 base::Value::CreateIntegerValue(123)); | 137 base::Value::CreateIntegerValue(123)); |
| 127 EXPECT_CALL(observer, OnPolicyUpdated(POLICY_DOMAIN_CHROME, "", | 138 EXPECT_CALL(observer, OnPolicyUpdated(POLICY_DOMAIN_CHROME, "", |
| 128 PolicyEquals(&expectedPrevious), | 139 PolicyEquals(&expectedPrevious), |
| 129 PolicyEquals(&expectedCurrent))); | 140 PolicyEquals(&expectedCurrent))); |
| 130 provider0_.NotifyPolicyUpdated(); | 141 provider0_.UpdateChromePolicy(policy0_); |
| 131 Mock::VerifyAndClearExpectations(&observer); | 142 Mock::VerifyAndClearExpectations(&observer); |
| 132 | 143 |
| 133 // No changes. | 144 // No changes. |
| 134 EXPECT_CALL(observer, OnPolicyUpdated(_, _, _, _)).Times(0); | 145 EXPECT_CALL(observer, OnPolicyUpdated(_, _, _, _)).Times(0); |
| 135 provider0_.NotifyPolicyUpdated(); | 146 provider0_.UpdateChromePolicy(policy0_); |
| 136 Mock::VerifyAndClearExpectations(&observer); | 147 Mock::VerifyAndClearExpectations(&observer); |
| 137 EXPECT_TRUE(VerifyPolicies(POLICY_DOMAIN_CHROME, "", expectedCurrent)); | 148 EXPECT_TRUE(VerifyPolicies(POLICY_DOMAIN_CHROME, "", expectedCurrent)); |
| 138 | 149 |
| 139 // New policy. | 150 // New policy. |
| 140 expectedPrevious.CopyFrom(expectedCurrent); | 151 expectedPrevious.CopyFrom(expectedCurrent); |
| 141 expectedCurrent.Set("bbb", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 152 expectedCurrent.Set("bbb", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 142 base::Value::CreateIntegerValue(456)); | 153 base::Value::CreateIntegerValue(456)); |
| 143 policy0_.Set("bbb", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 154 policy0_.Set("bbb", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 144 base::Value::CreateIntegerValue(456)); | 155 base::Value::CreateIntegerValue(456)); |
| 145 EXPECT_CALL(observer, OnPolicyUpdated(POLICY_DOMAIN_CHROME, "", | 156 EXPECT_CALL(observer, OnPolicyUpdated(POLICY_DOMAIN_CHROME, "", |
| 146 PolicyEquals(&expectedPrevious), | 157 PolicyEquals(&expectedPrevious), |
| 147 PolicyEquals(&expectedCurrent))); | 158 PolicyEquals(&expectedCurrent))); |
| 148 provider0_.NotifyPolicyUpdated(); | 159 provider0_.UpdateChromePolicy(policy0_); |
| 149 Mock::VerifyAndClearExpectations(&observer); | 160 Mock::VerifyAndClearExpectations(&observer); |
| 150 | 161 |
| 151 // Removed policy. | 162 // Removed policy. |
| 152 expectedPrevious.CopyFrom(expectedCurrent); | 163 expectedPrevious.CopyFrom(expectedCurrent); |
| 153 expectedCurrent.Erase("bbb"); | 164 expectedCurrent.Erase("bbb"); |
| 154 policy0_.Erase("bbb"); | 165 policy0_.Erase("bbb"); |
| 155 EXPECT_CALL(observer, OnPolicyUpdated(POLICY_DOMAIN_CHROME, "", | 166 EXPECT_CALL(observer, OnPolicyUpdated(POLICY_DOMAIN_CHROME, "", |
| 156 PolicyEquals(&expectedPrevious), | 167 PolicyEquals(&expectedPrevious), |
| 157 PolicyEquals(&expectedCurrent))); | 168 PolicyEquals(&expectedCurrent))); |
| 158 provider0_.NotifyPolicyUpdated(); | 169 provider0_.UpdateChromePolicy(policy0_); |
| 159 Mock::VerifyAndClearExpectations(&observer); | 170 Mock::VerifyAndClearExpectations(&observer); |
| 160 | 171 |
| 161 // Changed policy. | 172 // Changed policy. |
| 162 expectedPrevious.CopyFrom(expectedCurrent); | 173 expectedPrevious.CopyFrom(expectedCurrent); |
| 163 expectedCurrent.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 174 expectedCurrent.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 164 base::Value::CreateIntegerValue(789)); | 175 base::Value::CreateIntegerValue(789)); |
| 165 policy0_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 176 policy0_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 166 base::Value::CreateIntegerValue(789)); | 177 base::Value::CreateIntegerValue(789)); |
| 167 | 178 |
| 168 EXPECT_CALL(observer, OnPolicyUpdated(POLICY_DOMAIN_CHROME, "", | 179 EXPECT_CALL(observer, OnPolicyUpdated(POLICY_DOMAIN_CHROME, "", |
| 169 PolicyEquals(&expectedPrevious), | 180 PolicyEquals(&expectedPrevious), |
| 170 PolicyEquals(&expectedCurrent))); | 181 PolicyEquals(&expectedCurrent))); |
| 171 provider0_.NotifyPolicyUpdated(); | 182 provider0_.UpdateChromePolicy(policy0_); |
| 172 Mock::VerifyAndClearExpectations(&observer); | 183 Mock::VerifyAndClearExpectations(&observer); |
| 173 | 184 |
| 174 // No changes again. | 185 // No changes again. |
| 175 EXPECT_CALL(observer, OnPolicyUpdated(_, _, _, _)).Times(0); | 186 EXPECT_CALL(observer, OnPolicyUpdated(_, _, _, _)).Times(0); |
| 176 provider0_.NotifyPolicyUpdated(); | 187 provider0_.UpdateChromePolicy(policy0_); |
| 177 Mock::VerifyAndClearExpectations(&observer); | 188 Mock::VerifyAndClearExpectations(&observer); |
| 178 EXPECT_TRUE(VerifyPolicies(POLICY_DOMAIN_CHROME, "", expectedCurrent)); | 189 EXPECT_TRUE(VerifyPolicies(POLICY_DOMAIN_CHROME, "", expectedCurrent)); |
| 179 | 190 |
| 180 policy_service_->RemoveObserver(POLICY_DOMAIN_CHROME, "", &observer); | 191 policy_service_->RemoveObserver(POLICY_DOMAIN_CHROME, "", &observer); |
| 181 } | 192 } |
| 182 | 193 |
| 183 TEST_F(PolicyServiceTest, Priorities) { | 194 TEST_F(PolicyServiceTest, Priorities) { |
| 184 PolicyMap expected; | 195 PolicyMap expected; |
| 185 expected.Set("pre", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 196 expected.Set("pre", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 186 base::Value::CreateIntegerValue(13)); | 197 base::Value::CreateIntegerValue(13)); |
| 187 expected.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 198 expected.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 188 base::Value::CreateIntegerValue(0)); | 199 base::Value::CreateIntegerValue(0)); |
| 189 policy0_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 200 policy0_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 190 base::Value::CreateIntegerValue(0)); | 201 base::Value::CreateIntegerValue(0)); |
| 191 policy1_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 202 policy1_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 192 base::Value::CreateIntegerValue(1)); | 203 base::Value::CreateIntegerValue(1)); |
| 193 policy2_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 204 policy2_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 194 base::Value::CreateIntegerValue(2)); | 205 base::Value::CreateIntegerValue(2)); |
| 195 provider0_.NotifyPolicyUpdated(); | 206 provider0_.UpdateChromePolicy(policy0_); |
| 196 provider1_.NotifyPolicyUpdated(); | 207 provider1_.UpdateChromePolicy(policy1_); |
| 197 provider2_.NotifyPolicyUpdated(); | 208 provider2_.UpdateChromePolicy(policy2_); |
| 198 EXPECT_TRUE(VerifyPolicies(POLICY_DOMAIN_CHROME, "", expected)); | 209 EXPECT_TRUE(VerifyPolicies(POLICY_DOMAIN_CHROME, "", expected)); |
| 199 | 210 |
| 200 expected.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 211 expected.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 201 base::Value::CreateIntegerValue(1)); | 212 base::Value::CreateIntegerValue(1)); |
| 202 policy0_.Erase("aaa"); | 213 policy0_.Erase("aaa"); |
| 203 provider0_.NotifyPolicyUpdated(); | 214 provider0_.UpdateChromePolicy(policy0_); |
| 204 EXPECT_TRUE(VerifyPolicies(POLICY_DOMAIN_CHROME, "", expected)); | 215 EXPECT_TRUE(VerifyPolicies(POLICY_DOMAIN_CHROME, "", expected)); |
| 205 | 216 |
| 206 expected.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 217 expected.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 207 base::Value::CreateIntegerValue(2)); | 218 base::Value::CreateIntegerValue(2)); |
| 208 policy1_.Set("aaa", POLICY_LEVEL_RECOMMENDED, POLICY_SCOPE_USER, | 219 policy1_.Set("aaa", POLICY_LEVEL_RECOMMENDED, POLICY_SCOPE_USER, |
| 209 base::Value::CreateIntegerValue(1)); | 220 base::Value::CreateIntegerValue(1)); |
| 210 provider1_.NotifyPolicyUpdated(); | 221 provider1_.UpdateChromePolicy(policy1_); |
| 211 EXPECT_TRUE(VerifyPolicies(POLICY_DOMAIN_CHROME, "", expected)); | 222 EXPECT_TRUE(VerifyPolicies(POLICY_DOMAIN_CHROME, "", expected)); |
| 212 } | 223 } |
| 213 | 224 |
| 214 TEST_F(PolicyServiceTest, PolicyChangeRegistrar) { | 225 TEST_F(PolicyServiceTest, PolicyChangeRegistrar) { |
| 215 scoped_ptr<PolicyChangeRegistrar> registrar( | 226 scoped_ptr<PolicyChangeRegistrar> registrar( |
| 216 new PolicyChangeRegistrar( | 227 new PolicyChangeRegistrar( |
| 217 policy_service_.get(), POLICY_DOMAIN_CHROME, "")); | 228 policy_service_.get(), POLICY_DOMAIN_CHROME, "")); |
| 218 | 229 |
| 219 // Starting to observe existing policies doesn't trigger a notification. | 230 // Starting to observe existing policies doesn't trigger a notification. |
| 220 EXPECT_CALL(*this, OnPolicyValueUpdated(_, _)).Times(0); | 231 EXPECT_CALL(*this, OnPolicyValueUpdated(_, _)).Times(0); |
| 221 registrar->Observe("pre", base::Bind( | 232 registrar->Observe("pre", base::Bind( |
| 222 &PolicyServiceTest::OnPolicyValueUpdated, | 233 &PolicyServiceTest::OnPolicyValueUpdated, |
| 223 base::Unretained(this))); | 234 base::Unretained(this))); |
| 224 registrar->Observe("aaa", base::Bind( | 235 registrar->Observe("aaa", base::Bind( |
| 225 &PolicyServiceTest::OnPolicyValueUpdated, | 236 &PolicyServiceTest::OnPolicyValueUpdated, |
| 226 base::Unretained(this))); | 237 base::Unretained(this))); |
| 227 Mock::VerifyAndClearExpectations(this); | 238 Mock::VerifyAndClearExpectations(this); |
| 228 | 239 |
| 229 // Changing it now triggers a notification. | 240 // Changing it now triggers a notification. |
| 230 base::FundamentalValue kValue0(0); | 241 base::FundamentalValue kValue0(0); |
| 231 EXPECT_CALL(*this, OnPolicyValueUpdated(NULL, ValueEquals(&kValue0))); | 242 EXPECT_CALL(*this, OnPolicyValueUpdated(NULL, ValueEquals(&kValue0))); |
| 232 policy0_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 243 policy0_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 233 kValue0.DeepCopy()); | 244 kValue0.DeepCopy()); |
| 234 provider0_.NotifyPolicyUpdated(); | 245 provider0_.UpdateChromePolicy(policy0_); |
| 235 Mock::VerifyAndClearExpectations(this); | 246 Mock::VerifyAndClearExpectations(this); |
| 236 | 247 |
| 237 // Changing other values doesn't trigger a notification. | 248 // Changing other values doesn't trigger a notification. |
| 238 EXPECT_CALL(*this, OnPolicyValueUpdated(_, _)).Times(0); | 249 EXPECT_CALL(*this, OnPolicyValueUpdated(_, _)).Times(0); |
| 239 policy0_.Set("bbb", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 250 policy0_.Set("bbb", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 240 kValue0.DeepCopy()); | 251 kValue0.DeepCopy()); |
| 241 provider0_.NotifyPolicyUpdated(); | 252 provider0_.UpdateChromePolicy(policy0_); |
| 242 Mock::VerifyAndClearExpectations(this); | 253 Mock::VerifyAndClearExpectations(this); |
| 243 | 254 |
| 244 // Modifying the value triggers a notification. | 255 // Modifying the value triggers a notification. |
| 245 base::FundamentalValue kValue1(1); | 256 base::FundamentalValue kValue1(1); |
| 246 EXPECT_CALL(*this, OnPolicyValueUpdated(ValueEquals(&kValue0), | 257 EXPECT_CALL(*this, OnPolicyValueUpdated(ValueEquals(&kValue0), |
| 247 ValueEquals(&kValue1))); | 258 ValueEquals(&kValue1))); |
| 248 policy0_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 259 policy0_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 249 kValue1.DeepCopy()); | 260 kValue1.DeepCopy()); |
| 250 provider0_.NotifyPolicyUpdated(); | 261 provider0_.UpdateChromePolicy(policy0_); |
| 251 Mock::VerifyAndClearExpectations(this); | 262 Mock::VerifyAndClearExpectations(this); |
| 252 | 263 |
| 253 // Removing the value triggers a notification. | 264 // Removing the value triggers a notification. |
| 254 EXPECT_CALL(*this, OnPolicyValueUpdated(ValueEquals(&kValue1), NULL)); | 265 EXPECT_CALL(*this, OnPolicyValueUpdated(ValueEquals(&kValue1), NULL)); |
| 255 policy0_.Erase("aaa"); | 266 policy0_.Erase("aaa"); |
| 256 provider0_.NotifyPolicyUpdated(); | 267 provider0_.UpdateChromePolicy(policy0_); |
| 257 Mock::VerifyAndClearExpectations(this); | 268 Mock::VerifyAndClearExpectations(this); |
| 258 | 269 |
| 259 // No more notifications after destroying the registrar. | 270 // No more notifications after destroying the registrar. |
| 260 EXPECT_CALL(*this, OnPolicyValueUpdated(_, _)).Times(0); | 271 EXPECT_CALL(*this, OnPolicyValueUpdated(_, _)).Times(0); |
| 261 registrar.reset(); | 272 registrar.reset(); |
| 262 policy0_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 273 policy0_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 263 kValue1.DeepCopy()); | 274 kValue1.DeepCopy()); |
| 264 policy0_.Set("pre", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 275 policy0_.Set("pre", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 265 kValue1.DeepCopy()); | 276 kValue1.DeepCopy()); |
| 266 provider0_.NotifyPolicyUpdated(); | 277 provider0_.UpdateChromePolicy(policy0_); |
| 267 Mock::VerifyAndClearExpectations(this); | 278 Mock::VerifyAndClearExpectations(this); |
| 268 } | 279 } |
| 269 | 280 |
| 270 TEST_F(PolicyServiceTest, RefreshPolicies) { | 281 TEST_F(PolicyServiceTest, RefreshPolicies) { |
| 271 MessageLoop loop; | 282 MessageLoop loop; |
| 272 content::TestBrowserThread ui_thread(content::BrowserThread::UI, &loop); | 283 content::TestBrowserThread ui_thread(content::BrowserThread::UI, &loop); |
| 273 content::TestBrowserThread file_thread(content::BrowserThread::FILE, &loop); | 284 content::TestBrowserThread file_thread(content::BrowserThread::FILE, &loop); |
| 274 content::TestBrowserThread io_thread(content::BrowserThread::IO, &loop); | 285 content::TestBrowserThread io_thread(content::BrowserThread::IO, &loop); |
| 275 | 286 |
| 276 EXPECT_CALL(provider0_, RefreshPolicies()).Times(AnyNumber()); | 287 EXPECT_CALL(provider0_, RefreshPolicies()).Times(AnyNumber()); |
| 277 EXPECT_CALL(provider1_, RefreshPolicies()).Times(AnyNumber()); | 288 EXPECT_CALL(provider1_, RefreshPolicies()).Times(AnyNumber()); |
| 278 EXPECT_CALL(provider2_, RefreshPolicies()).Times(AnyNumber()); | 289 EXPECT_CALL(provider2_, RefreshPolicies()).Times(AnyNumber()); |
| 279 | 290 |
| 280 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0); | 291 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0); |
| 281 policy_service_->RefreshPolicies(base::Bind( | 292 policy_service_->RefreshPolicies(base::Bind( |
| 282 &PolicyServiceTest::OnPolicyRefresh, | 293 &PolicyServiceTest::OnPolicyRefresh, |
| 283 base::Unretained(this))); | 294 base::Unretained(this))); |
| 284 loop.RunAllPending(); | 295 loop.RunAllPending(); |
| 285 Mock::VerifyAndClearExpectations(this); | 296 Mock::VerifyAndClearExpectations(this); |
| 286 | 297 |
| 287 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0); | 298 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0); |
| 288 base::FundamentalValue kValue0(0); | 299 base::FundamentalValue kValue0(0); |
| 289 policy0_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 300 policy0_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 290 kValue0.DeepCopy()); | 301 kValue0.DeepCopy()); |
| 291 provider0_.NotifyPolicyUpdated(); | 302 provider0_.UpdateChromePolicy(policy0_); |
| 292 loop.RunAllPending(); | 303 loop.RunAllPending(); |
| 293 Mock::VerifyAndClearExpectations(this); | 304 Mock::VerifyAndClearExpectations(this); |
| 294 | 305 |
| 295 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0); | 306 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0); |
| 296 base::FundamentalValue kValue1(1); | 307 base::FundamentalValue kValue1(1); |
| 297 policy1_.Set("aaa", POLICY_LEVEL_RECOMMENDED, POLICY_SCOPE_USER, | 308 policy1_.Set("aaa", POLICY_LEVEL_RECOMMENDED, POLICY_SCOPE_USER, |
| 298 kValue1.DeepCopy()); | 309 kValue1.DeepCopy()); |
| 299 provider1_.NotifyPolicyUpdated(); | 310 provider1_.UpdateChromePolicy(policy1_); |
| 300 loop.RunAllPending(); | 311 loop.RunAllPending(); |
| 301 Mock::VerifyAndClearExpectations(this); | 312 Mock::VerifyAndClearExpectations(this); |
| 302 | 313 |
| 303 // A provider can refresh more than once after a RefreshPolicies call, but | 314 // A provider can refresh more than once after a RefreshPolicies call, but |
| 304 // OnPolicyRefresh should be triggered only after all providers are | 315 // OnPolicyRefresh should be triggered only after all providers are |
| 305 // refreshed. | 316 // refreshed. |
| 306 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0); | 317 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0); |
| 307 policy1_.Set("bbb", POLICY_LEVEL_RECOMMENDED, POLICY_SCOPE_USER, | 318 policy1_.Set("bbb", POLICY_LEVEL_RECOMMENDED, POLICY_SCOPE_USER, |
| 308 kValue1.DeepCopy()); | 319 kValue1.DeepCopy()); |
| 309 provider1_.NotifyPolicyUpdated(); | 320 provider1_.UpdateChromePolicy(policy1_); |
| 310 loop.RunAllPending(); | 321 loop.RunAllPending(); |
| 311 Mock::VerifyAndClearExpectations(this); | 322 Mock::VerifyAndClearExpectations(this); |
| 312 | 323 |
| 313 // If another RefreshPolicies() call happens while waiting for a previous | 324 // If another RefreshPolicies() call happens while waiting for a previous |
| 314 // one to complete, then all providers must refresh again. | 325 // one to complete, then all providers must refresh again. |
| 315 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0); | 326 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0); |
| 316 policy_service_->RefreshPolicies(base::Bind( | 327 policy_service_->RefreshPolicies(base::Bind( |
| 317 &PolicyServiceTest::OnPolicyRefresh, | 328 &PolicyServiceTest::OnPolicyRefresh, |
| 318 base::Unretained(this))); | 329 base::Unretained(this))); |
| 319 loop.RunAllPending(); | 330 loop.RunAllPending(); |
| 320 Mock::VerifyAndClearExpectations(this); | 331 Mock::VerifyAndClearExpectations(this); |
| 321 | 332 |
| 322 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0); | 333 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0); |
| 323 policy2_.Set("bbb", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 334 policy2_.Set("bbb", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 324 kValue0.DeepCopy()); | 335 kValue0.DeepCopy()); |
| 325 provider2_.NotifyPolicyUpdated(); | 336 provider2_.UpdateChromePolicy(policy2_); |
| 326 loop.RunAllPending(); | 337 loop.RunAllPending(); |
| 327 Mock::VerifyAndClearExpectations(this); | 338 Mock::VerifyAndClearExpectations(this); |
| 328 | 339 |
| 329 // Providers 0 and 1 must reload again. | 340 // Providers 0 and 1 must reload again. |
| 330 EXPECT_CALL(*this, OnPolicyRefresh()).Times(2); | 341 EXPECT_CALL(*this, OnPolicyRefresh()).Times(2); |
| 331 base::FundamentalValue kValue2(2); | 342 base::FundamentalValue kValue2(2); |
| 332 policy0_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 343 policy0_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 333 kValue2.DeepCopy()); | 344 kValue2.DeepCopy()); |
| 334 provider0_.NotifyPolicyUpdated(); | 345 provider0_.UpdateChromePolicy(policy0_); |
| 335 provider1_.NotifyPolicyUpdated(); | 346 provider1_.UpdateChromePolicy(policy1_); |
| 336 loop.RunAllPending(); | 347 loop.RunAllPending(); |
| 337 Mock::VerifyAndClearExpectations(this); | 348 Mock::VerifyAndClearExpectations(this); |
| 338 | 349 |
| 339 const PolicyMap* policies = policy_service_->GetPolicies( | 350 const PolicyMap& policies = policy_service_->GetPolicies( |
| 340 POLICY_DOMAIN_CHROME, ""); | 351 POLICY_DOMAIN_CHROME, ""); |
| 341 ASSERT_TRUE(policies); | 352 EXPECT_TRUE(base::Value::Equals(&kValue2, policies.GetValue("aaa"))); |
| 342 EXPECT_TRUE(base::Value::Equals(&kValue2, policies->GetValue("aaa"))); | 353 EXPECT_TRUE(base::Value::Equals(&kValue0, policies.GetValue("bbb"))); |
| 343 EXPECT_TRUE(base::Value::Equals(&kValue0, policies->GetValue("bbb"))); | 354 } |
| 355 | |
| 356 TEST_F(PolicyServiceTest, NamespaceMerge) { | |
| 357 scoped_ptr<PolicyBundle> bundle0(new PolicyBundle()); | |
| 358 scoped_ptr<PolicyBundle> bundle1(new PolicyBundle()); | |
| 359 scoped_ptr<PolicyBundle> bundle2(new PolicyBundle()); | |
| 360 | |
| 361 AddTestPolicies( | |
| 362 bundle0.get(), "bundle0", POLICY_LEVEL_RECOMMENDED, POLICY_SCOPE_USER); | |
| 363 AddTestPolicies( | |
| 364 bundle1.get(), "bundle1", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER); | |
| 365 AddTestPolicies( | |
| 366 bundle2.get(), "bundle2", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE); | |
| 367 | |
| 368 provider0_.UpdatePolicy(bundle0.Pass()); | |
| 369 provider1_.UpdatePolicy(bundle1.Pass()); | |
| 370 provider2_.UpdatePolicy(bundle2.Pass()); | |
| 371 | |
| 372 PolicyMap expected; | |
| 373 // For policies of the same level and scope, the first provider takes | |
| 374 // precedence, on every namespace. | |
| 375 expected.Set(kSameLevelPolicy, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | |
| 376 base::Value::CreateStringValue("bundle0")); | |
| 377 // For policies with different levels and scopes, the highest priority | |
| 378 // level/scope combination takes precedence, on every namespace. | |
| 379 expected.Set(kDiffLevelPolicy, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE, | |
| 380 base::Value::CreateStringValue("bundle2")); | |
| 381 EXPECT_TRUE(policy_service_->GetPolicies(POLICY_DOMAIN_CHROME, "") | |
| 382 .Equals(expected)); | |
| 383 EXPECT_TRUE(policy_service_->GetPolicies(POLICY_DOMAIN_EXTENSIONS, kExtension) | |
| 384 .Equals(expected)); | |
| 344 } | 385 } |
| 345 | 386 |
| 346 } // namespace policy | 387 } // namespace policy |
| OLD | NEW |