OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/policy/cloud_policy_manager.h" | |
6 | |
7 #include "base/basictypes.h" | |
8 #include "base/compiler_specific.h" | |
9 #include "base/memory/scoped_ptr.h" | |
10 #include "base/message_loop.h" | |
11 #include "chrome/browser/policy/cloud_policy_constants.h" | |
12 #include "chrome/browser/policy/configuration_policy_provider_test.h" | |
13 #include "chrome/browser/policy/mock_cloud_policy_client.h" | |
14 #include "chrome/browser/policy/mock_cloud_policy_store.h" | |
15 #include "chrome/browser/policy/mock_configuration_policy_provider.h" | |
16 #include "chrome/browser/policy/policy_builder.h" | |
17 #include "testing/gmock/include/gmock/gmock.h" | |
18 #include "testing/gtest/include/gtest/gtest.h" | |
19 | |
20 using testing::Mock; | |
21 using testing::_; | |
22 | |
23 namespace em = enterprise_management; | |
24 | |
25 namespace policy { | |
26 namespace { | |
27 | |
28 class TestHarness : public PolicyProviderTestHarness { | |
29 public: | |
30 explicit TestHarness(PolicyLevel level); | |
31 virtual ~TestHarness(); | |
32 | |
33 virtual void SetUp() OVERRIDE; | |
34 | |
35 virtual ConfigurationPolicyProvider* CreateProvider( | |
36 const PolicyDefinitionList* policy_definition_list) OVERRIDE; | |
37 | |
38 virtual void InstallEmptyPolicy() OVERRIDE; | |
39 virtual void InstallStringPolicy(const std::string& policy_name, | |
40 const std::string& policy_value) OVERRIDE; | |
41 virtual void InstallIntegerPolicy(const std::string& policy_name, | |
42 int policy_value) OVERRIDE; | |
43 virtual void InstallBooleanPolicy(const std::string& policy_name, | |
44 bool policy_value) OVERRIDE; | |
45 virtual void InstallStringListPolicy( | |
46 const std::string& policy_name, | |
47 const base::ListValue* policy_value) OVERRIDE; | |
48 virtual void InstallDictionaryPolicy( | |
49 const std::string& policy_name, | |
50 const base::DictionaryValue* policy_value) OVERRIDE; | |
51 | |
52 // Creates harnesses for mandatory and recommended levels, respectively. | |
53 static PolicyProviderTestHarness* CreateMandatory(); | |
54 static PolicyProviderTestHarness* CreateRecommended(); | |
55 | |
56 private: | |
57 MockCloudPolicyStore store_; | |
58 | |
59 DISALLOW_COPY_AND_ASSIGN(TestHarness); | |
60 }; | |
61 | |
62 TestHarness::TestHarness(PolicyLevel level) | |
63 : PolicyProviderTestHarness(level, POLICY_SCOPE_USER) {} | |
64 | |
65 TestHarness::~TestHarness() {} | |
66 | |
67 void TestHarness::SetUp() {} | |
68 | |
69 ConfigurationPolicyProvider* TestHarness::CreateProvider( | |
70 const PolicyDefinitionList* policy_definition_list) { | |
71 // Create and initialize the store. | |
72 store_.NotifyStoreLoaded(); | |
73 ConfigurationPolicyProvider* provider = new CloudPolicyManager( | |
74 PolicyNamespaceKey(dm_protocol::kChromeUserPolicyType, std::string()), | |
75 &store_); | |
76 Mock::VerifyAndClearExpectations(&store_); | |
77 return provider; | |
78 } | |
79 | |
80 void TestHarness::InstallEmptyPolicy() {} | |
81 | |
82 void TestHarness::InstallStringPolicy(const std::string& policy_name, | |
83 const std::string& policy_value) { | |
84 store_.policy_map_.Set(policy_name, policy_level(), policy_scope(), | |
85 base::Value::CreateStringValue(policy_value)); | |
86 } | |
87 | |
88 void TestHarness::InstallIntegerPolicy(const std::string& policy_name, | |
89 int policy_value) { | |
90 store_.policy_map_.Set(policy_name, policy_level(), policy_scope(), | |
91 base::Value::CreateIntegerValue(policy_value)); | |
92 } | |
93 | |
94 void TestHarness::InstallBooleanPolicy(const std::string& policy_name, | |
95 bool policy_value) { | |
96 store_.policy_map_.Set(policy_name, policy_level(), policy_scope(), | |
97 base::Value::CreateBooleanValue(policy_value)); | |
98 } | |
99 | |
100 void TestHarness::InstallStringListPolicy(const std::string& policy_name, | |
101 const base::ListValue* policy_value) { | |
102 store_.policy_map_.Set(policy_name, policy_level(), policy_scope(), | |
103 policy_value->DeepCopy()); | |
104 } | |
105 | |
106 void TestHarness::InstallDictionaryPolicy( | |
107 const std::string& policy_name, | |
108 const base::DictionaryValue* policy_value) { | |
109 store_.policy_map_.Set(policy_name, policy_level(), policy_scope(), | |
110 policy_value->DeepCopy()); | |
111 } | |
112 | |
113 // static | |
114 PolicyProviderTestHarness* TestHarness::CreateMandatory() { | |
115 return new TestHarness(POLICY_LEVEL_MANDATORY); | |
116 } | |
117 | |
118 // static | |
119 PolicyProviderTestHarness* TestHarness::CreateRecommended() { | |
120 return new TestHarness(POLICY_LEVEL_RECOMMENDED); | |
121 } | |
122 | |
123 // Instantiate abstract test case for basic policy reading tests. | |
124 INSTANTIATE_TEST_CASE_P( | |
125 UserCloudPolicyManagerProviderTest, | |
126 ConfigurationPolicyProviderTest, | |
127 testing::Values(TestHarness::CreateMandatory, | |
128 TestHarness::CreateRecommended)); | |
129 | |
130 class TestCloudPolicyManager : public CloudPolicyManager { | |
131 public: | |
132 explicit TestCloudPolicyManager(CloudPolicyStore* store) | |
133 : CloudPolicyManager(PolicyNamespaceKey( | |
134 dm_protocol::kChromeUserPolicyType, | |
135 std::string()), | |
136 store) {} | |
137 virtual ~TestCloudPolicyManager() {} | |
138 | |
139 // Publish the protected members for testing. | |
140 using CloudPolicyManager::client; | |
141 using CloudPolicyManager::store; | |
142 using CloudPolicyManager::service; | |
143 using CloudPolicyManager::CheckAndPublishPolicy; | |
144 | |
145 private: | |
146 DISALLOW_COPY_AND_ASSIGN(TestCloudPolicyManager); | |
147 }; | |
148 | |
149 MATCHER_P(ProtoMatches, proto, "") { | |
150 return arg.SerializePartialAsString() == proto.SerializePartialAsString(); | |
151 } | |
152 | |
153 class CloudPolicyManagerTest : public testing::Test { | |
154 protected: | |
155 CloudPolicyManagerTest() | |
156 : policy_ns_key_(dm_protocol::kChromeUserPolicyType, std::string()) {} | |
157 | |
158 virtual void SetUp() OVERRIDE { | |
159 // Set up a policy map for testing. | |
160 policy_map_.Set("key", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | |
161 base::Value::CreateStringValue("value")); | |
162 expected_bundle_.Get(PolicyNamespace(POLICY_DOMAIN_CHROME, std::string())) | |
163 .CopyFrom(policy_map_); | |
164 | |
165 policy_.payload().mutable_homepagelocation()->set_value( | |
166 "http://www.example.com"); | |
167 policy_.Build(); | |
168 | |
169 EXPECT_CALL(store_, Load()); | |
170 manager_.reset(new TestCloudPolicyManager(&store_)); | |
171 manager_->Init(); | |
172 Mock::VerifyAndClearExpectations(&store_); | |
173 manager_->AddObserver(&observer_); | |
174 } | |
175 | |
176 virtual void TearDown() OVERRIDE { | |
177 manager_->RemoveObserver(&observer_); | |
178 manager_->Shutdown(); | |
179 } | |
180 | |
181 // Required by the refresh scheduler that's created by the manager. | |
182 MessageLoop loop_; | |
183 | |
184 // Testing policy. | |
185 const PolicyNamespaceKey policy_ns_key_; | |
186 UserPolicyBuilder policy_; | |
187 PolicyMap policy_map_; | |
188 PolicyBundle expected_bundle_; | |
189 | |
190 // Policy infrastructure. | |
191 MockConfigurationPolicyObserver observer_; | |
192 MockCloudPolicyStore store_; | |
193 scoped_ptr<TestCloudPolicyManager> manager_; | |
194 | |
195 private: | |
196 DISALLOW_COPY_AND_ASSIGN(CloudPolicyManagerTest); | |
197 }; | |
198 | |
199 TEST_F(CloudPolicyManagerTest, InitAndShutdown) { | |
200 PolicyBundle empty_bundle; | |
201 EXPECT_TRUE(empty_bundle.Equals(manager_->policies())); | |
202 EXPECT_FALSE(manager_->IsInitializationComplete(POLICY_DOMAIN_CHROME)); | |
203 | |
204 EXPECT_CALL(observer_, OnUpdatePolicy(_)).Times(0); | |
205 manager_->CheckAndPublishPolicy(); | |
206 Mock::VerifyAndClearExpectations(&observer_); | |
207 | |
208 store_.policy_map_.CopyFrom(policy_map_); | |
209 store_.policy_.reset(new em::PolicyData(policy_.policy_data())); | |
210 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())); | |
211 store_.NotifyStoreLoaded(); | |
212 Mock::VerifyAndClearExpectations(&observer_); | |
213 EXPECT_TRUE(expected_bundle_.Equals(manager_->policies())); | |
214 EXPECT_TRUE(manager_->IsInitializationComplete(POLICY_DOMAIN_CHROME)); | |
215 | |
216 MockCloudPolicyClient* client = new MockCloudPolicyClient(); | |
217 EXPECT_CALL(*client, SetupRegistration(_, _)); | |
218 manager_->core()->Connect(scoped_ptr<CloudPolicyClient>(client)); | |
219 Mock::VerifyAndClearExpectations(client); | |
220 EXPECT_TRUE(manager_->client()); | |
221 EXPECT_TRUE(manager_->service()); | |
222 | |
223 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())); | |
224 manager_->CheckAndPublishPolicy(); | |
225 Mock::VerifyAndClearExpectations(&observer_); | |
226 | |
227 manager_->core()->Disconnect(); | |
228 EXPECT_FALSE(manager_->client()); | |
229 EXPECT_FALSE(manager_->service()); | |
230 } | |
231 | |
232 TEST_F(CloudPolicyManagerTest, RegistrationAndFetch) { | |
233 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())); | |
234 store_.NotifyStoreLoaded(); | |
235 Mock::VerifyAndClearExpectations(&observer_); | |
236 EXPECT_TRUE(manager_->IsInitializationComplete(POLICY_DOMAIN_CHROME)); | |
237 | |
238 MockCloudPolicyClient* client = new MockCloudPolicyClient(); | |
239 manager_->core()->Connect(scoped_ptr<CloudPolicyClient>(client)); | |
240 | |
241 client->SetDMToken(policy_.policy_data().request_token()); | |
242 client->NotifyRegistrationStateChanged(); | |
243 | |
244 client->SetPolicy(policy_ns_key_, policy_.policy()); | |
245 EXPECT_CALL(store_, Store(ProtoMatches(policy_.policy()))); | |
246 client->NotifyPolicyFetched(); | |
247 Mock::VerifyAndClearExpectations(&store_); | |
248 | |
249 store_.policy_map_.CopyFrom(policy_map_); | |
250 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())); | |
251 store_.NotifyStoreLoaded(); | |
252 Mock::VerifyAndClearExpectations(&observer_); | |
253 EXPECT_TRUE(expected_bundle_.Equals(manager_->policies())); | |
254 } | |
255 | |
256 TEST_F(CloudPolicyManagerTest, Update) { | |
257 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())); | |
258 store_.NotifyStoreLoaded(); | |
259 Mock::VerifyAndClearExpectations(&observer_); | |
260 EXPECT_TRUE(manager_->IsInitializationComplete(POLICY_DOMAIN_CHROME)); | |
261 PolicyBundle empty_bundle; | |
262 EXPECT_TRUE(empty_bundle.Equals(manager_->policies())); | |
263 | |
264 store_.policy_map_.CopyFrom(policy_map_); | |
265 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())); | |
266 store_.NotifyStoreLoaded(); | |
267 Mock::VerifyAndClearExpectations(&observer_); | |
268 EXPECT_TRUE(expected_bundle_.Equals(manager_->policies())); | |
269 EXPECT_TRUE(manager_->IsInitializationComplete(POLICY_DOMAIN_CHROME)); | |
270 } | |
271 | |
272 TEST_F(CloudPolicyManagerTest, RefreshNotRegistered) { | |
273 MockCloudPolicyClient* client = new MockCloudPolicyClient(); | |
274 manager_->core()->Connect(scoped_ptr<CloudPolicyClient>(client)); | |
275 | |
276 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())); | |
277 store_.NotifyStoreLoaded(); | |
278 Mock::VerifyAndClearExpectations(&observer_); | |
279 | |
280 // A refresh on a non-registered store should not block. | |
281 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())); | |
282 manager_->RefreshPolicies(); | |
283 Mock::VerifyAndClearExpectations(&observer_); | |
284 } | |
285 | |
286 TEST_F(CloudPolicyManagerTest, RefreshSuccessful) { | |
287 MockCloudPolicyClient* client = new MockCloudPolicyClient(); | |
288 manager_->core()->Connect(scoped_ptr<CloudPolicyClient>(client)); | |
289 | |
290 // Simulate a store load. | |
291 store_.policy_.reset(new em::PolicyData(policy_.policy_data())); | |
292 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())); | |
293 EXPECT_CALL(*client, SetupRegistration(_, _)); | |
294 store_.NotifyStoreLoaded(); | |
295 Mock::VerifyAndClearExpectations(client); | |
296 Mock::VerifyAndClearExpectations(&observer_); | |
297 | |
298 // Acknowledge registration. | |
299 client->SetDMToken(policy_.policy_data().request_token()); | |
300 | |
301 // Start a refresh. | |
302 EXPECT_CALL(observer_, OnUpdatePolicy(_)).Times(0); | |
303 EXPECT_CALL(*client, FetchPolicy()); | |
304 manager_->RefreshPolicies(); | |
305 Mock::VerifyAndClearExpectations(client); | |
306 Mock::VerifyAndClearExpectations(&observer_); | |
307 store_.policy_map_.CopyFrom(policy_map_); | |
308 | |
309 // A stray reload should be suppressed until the refresh completes. | |
310 EXPECT_CALL(observer_, OnUpdatePolicy(_)).Times(0); | |
311 store_.NotifyStoreLoaded(); | |
312 Mock::VerifyAndClearExpectations(&observer_); | |
313 | |
314 // Respond to the policy fetch, which should trigger a write to |store_|. | |
315 EXPECT_CALL(observer_, OnUpdatePolicy(_)).Times(0); | |
316 EXPECT_CALL(store_, Store(_)); | |
317 client->SetPolicy(policy_ns_key_, policy_.policy()); | |
318 client->NotifyPolicyFetched(); | |
319 Mock::VerifyAndClearExpectations(&observer_); | |
320 Mock::VerifyAndClearExpectations(&store_); | |
321 | |
322 // The load notification from |store_| should trigger the policy update. | |
323 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())); | |
324 store_.NotifyStoreLoaded(); | |
325 EXPECT_TRUE(expected_bundle_.Equals(manager_->policies())); | |
326 Mock::VerifyAndClearExpectations(&observer_); | |
327 } | |
328 | |
329 TEST_F(CloudPolicyManagerTest, SignalOnError) { | |
330 // Simulate a failed load and verify that it triggers OnUpdatePolicy(). | |
331 store_.policy_.reset(new em::PolicyData(policy_.policy_data())); | |
332 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())); | |
333 store_.NotifyStoreError(); | |
334 Mock::VerifyAndClearExpectations(&observer_); | |
335 | |
336 EXPECT_TRUE(manager_->IsInitializationComplete(POLICY_DOMAIN_CHROME)); | |
337 } | |
338 | |
339 } // namespace | |
340 } // namespace policy | |
OLD | NEW |