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