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_client.h" | |
6 | |
7 #include <map> | |
8 #include <set> | |
9 | |
10 #include "base/compiler_specific.h" | |
11 #include "base/memory/scoped_ptr.h" | |
12 #include "chrome/browser/policy/mock_device_management_service.h" | |
13 #include "chrome/browser/policy/proto/device_management_backend.pb.h" | |
14 #include "testing/gmock/include/gmock/gmock.h" | |
15 #include "testing/gtest/include/gtest/gtest.h" | |
16 | |
17 using testing::Return; | |
18 using testing::SaveArg; | |
19 using testing::StrictMock; | |
20 using testing::_; | |
21 | |
22 namespace em = enterprise_management; | |
23 | |
24 namespace policy { | |
25 | |
26 namespace { | |
27 | |
28 const char kClientID[] = "fake-client-id"; | |
29 const char kMachineID[] = "fake-machine-id"; | |
30 const char kMachineModel[] = "fake-machine-model"; | |
31 const char kOAuthToken[] = "fake-oauth-token"; | |
32 const char kDMToken[] = "fake-dm-token"; | |
33 | |
34 class MockObserver : public CloudPolicyClient::Observer { | |
35 public: | |
36 MockObserver() {} | |
37 virtual ~MockObserver() {} | |
38 | |
39 MOCK_METHOD1(OnPolicyFetched, void(CloudPolicyClient*)); | |
40 MOCK_METHOD1(OnRegistrationStateChanged, void(CloudPolicyClient*)); | |
41 MOCK_METHOD1(OnClientError, void(CloudPolicyClient*)); | |
42 | |
43 private: | |
44 DISALLOW_COPY_AND_ASSIGN(MockObserver); | |
45 }; | |
46 | |
47 class MockStatusProvider : public CloudPolicyClient::StatusProvider { | |
48 public: | |
49 MockStatusProvider() {} | |
50 virtual ~MockStatusProvider() {} | |
51 | |
52 MOCK_METHOD1(GetDeviceStatus, bool(em::DeviceStatusReportRequest* status)); | |
53 MOCK_METHOD1(GetSessionStatus, bool(em::SessionStatusReportRequest* status)); | |
54 MOCK_METHOD0(OnSubmittedSuccessfully, void(void)); | |
55 | |
56 private: | |
57 DISALLOW_COPY_AND_ASSIGN(MockStatusProvider); | |
58 }; | |
59 | |
60 MATCHER_P(MatchProto, expected, "matches protobuf") { | |
61 return arg.SerializePartialAsString() == expected.SerializePartialAsString(); | |
62 } | |
63 | |
64 } // namespace | |
65 | |
66 class CloudPolicyClientTest : public testing::Test { | |
67 protected: | |
68 CloudPolicyClientTest() | |
69 : client_id_(kClientID), | |
70 policy_ns_key_(dm_protocol::kChromeUserPolicyType, std::string()) { | |
71 em::DeviceRegisterRequest* register_request = | |
72 registration_request_.mutable_register_request(); | |
73 register_request->set_type(em::DeviceRegisterRequest::USER); | |
74 register_request->set_machine_id(kMachineID); | |
75 register_request->set_machine_model(kMachineModel); | |
76 registration_response_.mutable_register_response()-> | |
77 set_device_management_token(kDMToken); | |
78 | |
79 em::PolicyFetchRequest* policy_fetch_request = | |
80 policy_request_.mutable_policy_request()->add_request(); | |
81 policy_fetch_request->set_policy_type(dm_protocol::kChromeUserPolicyType); | |
82 policy_fetch_request->set_signature_type(em::PolicyFetchRequest::SHA1_RSA); | |
83 policy_response_.mutable_policy_response()->add_response()->set_policy_data( | |
84 CreatePolicyData("fake-policy-data")); | |
85 | |
86 unregistration_request_.mutable_unregister_request(); | |
87 unregistration_response_.mutable_unregister_response(); | |
88 } | |
89 | |
90 virtual void SetUp() OVERRIDE { | |
91 EXPECT_CALL(status_provider_, GetDeviceStatus(_)) | |
92 .WillRepeatedly(Return(false)); | |
93 EXPECT_CALL(status_provider_, GetSessionStatus(_)) | |
94 .WillRepeatedly(Return(false)); | |
95 CreateClient(USER_AFFILIATION_NONE); | |
96 } | |
97 | |
98 virtual void TearDown() OVERRIDE { | |
99 client_->RemoveObserver(&observer_); | |
100 } | |
101 | |
102 void Register() { | |
103 EXPECT_CALL(observer_, OnRegistrationStateChanged(_)); | |
104 client_->SetupRegistration(kDMToken, client_id_); | |
105 } | |
106 | |
107 void CreateClient(UserAffiliation user_affiliation) { | |
108 if (client_.get()) | |
109 client_->RemoveObserver(&observer_); | |
110 | |
111 client_.reset(new CloudPolicyClient(kMachineID, kMachineModel, | |
112 user_affiliation, &status_provider_, | |
113 &service_)); | |
114 client_->AddNamespaceToFetch(policy_ns_key_); | |
115 client_->AddObserver(&observer_); | |
116 } | |
117 | |
118 void ExpectRegistration(const std::string& oauth_token) { | |
119 EXPECT_CALL(service_, | |
120 CreateJob(DeviceManagementRequestJob::TYPE_REGISTRATION)) | |
121 .WillOnce(service_.SucceedJob(registration_response_)); | |
122 EXPECT_CALL(service_, StartJob(dm_protocol::kValueRequestRegister, | |
123 "", oauth_token, "", "", _, | |
124 MatchProto(registration_request_))) | |
125 .WillOnce(SaveArg<5>(&client_id_)); | |
126 } | |
127 | |
128 void ExpectPolicyFetch(const std::string& dm_token, | |
129 const std::string& user_affiliation) { | |
130 EXPECT_CALL(service_, | |
131 CreateJob(DeviceManagementRequestJob::TYPE_POLICY_FETCH)) | |
132 .WillOnce(service_.SucceedJob(policy_response_)); | |
133 EXPECT_CALL(service_, StartJob(dm_protocol::kValueRequestPolicy, | |
134 "", "", dm_token, user_affiliation, | |
135 client_id_, | |
136 MatchProto(policy_request_))); | |
137 } | |
138 | |
139 void ExpectUnregistration(const std::string& dm_token) { | |
140 EXPECT_CALL(service_, | |
141 CreateJob(DeviceManagementRequestJob::TYPE_UNREGISTRATION)) | |
142 .WillOnce(service_.SucceedJob(unregistration_response_)); | |
143 EXPECT_CALL(service_, StartJob(dm_protocol::kValueRequestUnregister, | |
144 "", "", dm_token, "", client_id_, | |
145 MatchProto(unregistration_request_))); | |
146 } | |
147 | |
148 void CheckPolicyResponse() { | |
149 ASSERT_TRUE(client_->GetPolicyFor(policy_ns_key_)); | |
150 EXPECT_THAT(*client_->GetPolicyFor(policy_ns_key_), | |
151 MatchProto(policy_response_.policy_response().response(0))); | |
152 } | |
153 | |
154 std::string CreatePolicyData(const std::string& policy_value) { | |
155 em::PolicyData policy_data; | |
156 policy_data.set_policy_type(dm_protocol::kChromeUserPolicyType); | |
157 policy_data.set_policy_value(policy_value); | |
158 return policy_data.SerializeAsString(); | |
159 } | |
160 | |
161 // Request protobufs used as expectations for the client requests. | |
162 em::DeviceManagementRequest registration_request_; | |
163 em::DeviceManagementRequest policy_request_; | |
164 em::DeviceManagementRequest unregistration_request_; | |
165 | |
166 // Protobufs used in successful responses. | |
167 em::DeviceManagementResponse registration_response_; | |
168 em::DeviceManagementResponse policy_response_; | |
169 em::DeviceManagementResponse unregistration_response_; | |
170 | |
171 std::string client_id_; | |
172 PolicyNamespaceKey policy_ns_key_; | |
173 MockDeviceManagementService service_; | |
174 StrictMock<MockStatusProvider> status_provider_; | |
175 StrictMock<MockObserver> observer_; | |
176 scoped_ptr<CloudPolicyClient> client_; | |
177 }; | |
178 | |
179 TEST_F(CloudPolicyClientTest, Init) { | |
180 EXPECT_CALL(service_, CreateJob(_)).Times(0); | |
181 EXPECT_FALSE(client_->is_registered()); | |
182 EXPECT_FALSE(client_->GetPolicyFor(policy_ns_key_)); | |
183 } | |
184 | |
185 TEST_F(CloudPolicyClientTest, SetupRegistrationAndPolicyFetch) { | |
186 EXPECT_CALL(service_, CreateJob(_)).Times(0); | |
187 EXPECT_CALL(observer_, OnRegistrationStateChanged(_)); | |
188 client_->SetupRegistration(kDMToken, client_id_); | |
189 EXPECT_TRUE(client_->is_registered()); | |
190 EXPECT_FALSE(client_->GetPolicyFor(policy_ns_key_)); | |
191 | |
192 ExpectPolicyFetch(kDMToken, dm_protocol::kValueUserAffiliationNone); | |
193 EXPECT_CALL(observer_, OnPolicyFetched(_)); | |
194 EXPECT_CALL(status_provider_, OnSubmittedSuccessfully()); | |
195 client_->FetchPolicy(); | |
196 EXPECT_EQ(DM_STATUS_SUCCESS, client_->status()); | |
197 CheckPolicyResponse(); | |
198 } | |
199 | |
200 TEST_F(CloudPolicyClientTest, RegistrationAndPolicyFetch) { | |
201 ExpectRegistration(kOAuthToken); | |
202 EXPECT_CALL(observer_, OnRegistrationStateChanged(_)); | |
203 client_->Register(em::DeviceRegisterRequest::USER, | |
204 kOAuthToken, std::string(), false); | |
205 EXPECT_TRUE(client_->is_registered()); | |
206 EXPECT_FALSE(client_->GetPolicyFor(policy_ns_key_)); | |
207 EXPECT_EQ(DM_STATUS_SUCCESS, client_->status()); | |
208 | |
209 ExpectPolicyFetch(kDMToken, dm_protocol::kValueUserAffiliationNone); | |
210 EXPECT_CALL(observer_, OnPolicyFetched(_)); | |
211 EXPECT_CALL(status_provider_, OnSubmittedSuccessfully()); | |
212 client_->FetchPolicy(); | |
213 EXPECT_EQ(DM_STATUS_SUCCESS, client_->status()); | |
214 CheckPolicyResponse(); | |
215 } | |
216 | |
217 TEST_F(CloudPolicyClientTest, RegistrationParameters) { | |
218 registration_request_.mutable_register_request()->set_reregister(true); | |
219 registration_request_.mutable_register_request()->set_auto_enrolled(true); | |
220 ExpectRegistration(kOAuthToken); | |
221 EXPECT_CALL(observer_, OnRegistrationStateChanged(_)); | |
222 client_->Register(em::DeviceRegisterRequest::USER, | |
223 kOAuthToken, kClientID, true); | |
224 EXPECT_EQ(kClientID, client_id_); | |
225 } | |
226 | |
227 TEST_F(CloudPolicyClientTest, RegistrationNoToken) { | |
228 registration_response_.mutable_register_response()-> | |
229 clear_device_management_token(); | |
230 ExpectRegistration(kOAuthToken); | |
231 EXPECT_CALL(observer_, OnClientError(_)); | |
232 client_->Register(em::DeviceRegisterRequest::USER, | |
233 kOAuthToken, std::string(), false); | |
234 EXPECT_FALSE(client_->is_registered()); | |
235 EXPECT_FALSE(client_->GetPolicyFor(policy_ns_key_)); | |
236 EXPECT_EQ(DM_STATUS_RESPONSE_DECODING_ERROR, client_->status()); | |
237 } | |
238 | |
239 TEST_F(CloudPolicyClientTest, RegistrationFailure) { | |
240 EXPECT_CALL(service_, | |
241 CreateJob(DeviceManagementRequestJob::TYPE_REGISTRATION)) | |
242 .WillOnce(service_.FailJob(DM_STATUS_REQUEST_FAILED)); | |
243 EXPECT_CALL(service_, StartJob(_, _, _, _, _, _, _)); | |
244 EXPECT_CALL(observer_, OnClientError(_)); | |
245 client_->Register(em::DeviceRegisterRequest::USER, | |
246 kOAuthToken, std::string(), false); | |
247 EXPECT_FALSE(client_->is_registered()); | |
248 EXPECT_FALSE(client_->GetPolicyFor(policy_ns_key_)); | |
249 EXPECT_EQ(DM_STATUS_REQUEST_FAILED, client_->status()); | |
250 } | |
251 | |
252 TEST_F(CloudPolicyClientTest, PolicyUpdate) { | |
253 Register(); | |
254 | |
255 ExpectPolicyFetch(kDMToken, dm_protocol::kValueUserAffiliationNone); | |
256 EXPECT_CALL(observer_, OnPolicyFetched(_)); | |
257 EXPECT_CALL(status_provider_, OnSubmittedSuccessfully()); | |
258 client_->FetchPolicy(); | |
259 CheckPolicyResponse(); | |
260 | |
261 policy_response_.mutable_policy_response()->clear_response(); | |
262 policy_response_.mutable_policy_response()->add_response()->set_policy_data( | |
263 CreatePolicyData("updated-fake-policy-data")); | |
264 ExpectPolicyFetch(kDMToken, dm_protocol::kValueUserAffiliationNone); | |
265 EXPECT_CALL(observer_, OnPolicyFetched(_)); | |
266 EXPECT_CALL(status_provider_, OnSubmittedSuccessfully()); | |
267 client_->FetchPolicy(); | |
268 EXPECT_EQ(DM_STATUS_SUCCESS, client_->status()); | |
269 CheckPolicyResponse(); | |
270 } | |
271 | |
272 TEST_F(CloudPolicyClientTest, PolicyFetchWithMetaData) { | |
273 Register(); | |
274 | |
275 const base::Time timestamp( | |
276 base::Time::UnixEpoch() + base::TimeDelta::FromDays(20)); | |
277 client_->set_submit_machine_id(true); | |
278 client_->set_last_policy_timestamp(timestamp); | |
279 client_->set_public_key_version(42); | |
280 em::PolicyFetchRequest* policy_fetch_request = | |
281 policy_request_.mutable_policy_request()->mutable_request(0); | |
282 policy_fetch_request->set_machine_id(kMachineID); | |
283 policy_fetch_request->set_timestamp( | |
284 (timestamp - base::Time::UnixEpoch()).InMilliseconds()); | |
285 policy_fetch_request->set_public_key_version(42); | |
286 | |
287 ExpectPolicyFetch(kDMToken, dm_protocol::kValueUserAffiliationNone); | |
288 EXPECT_CALL(observer_, OnPolicyFetched(_)); | |
289 EXPECT_CALL(status_provider_, OnSubmittedSuccessfully()); | |
290 client_->FetchPolicy(); | |
291 CheckPolicyResponse(); | |
292 } | |
293 | |
294 TEST_F(CloudPolicyClientTest, BadPolicyResponse) { | |
295 Register(); | |
296 | |
297 policy_response_.clear_policy_response(); | |
298 ExpectPolicyFetch(kDMToken, dm_protocol::kValueUserAffiliationNone); | |
299 EXPECT_CALL(observer_, OnClientError(_)); | |
300 client_->FetchPolicy(); | |
301 EXPECT_FALSE(client_->GetPolicyFor(policy_ns_key_)); | |
302 EXPECT_EQ(DM_STATUS_RESPONSE_DECODING_ERROR, client_->status()); | |
303 | |
304 policy_response_.mutable_policy_response()->add_response()->set_policy_data( | |
305 CreatePolicyData("fake-policy-data")); | |
306 policy_response_.mutable_policy_response()->add_response()->set_policy_data( | |
307 CreatePolicyData("excess-fake-policy-data")); | |
308 ExpectPolicyFetch(kDMToken, dm_protocol::kValueUserAffiliationNone); | |
309 EXPECT_CALL(observer_, OnPolicyFetched(_)); | |
310 EXPECT_CALL(status_provider_, OnSubmittedSuccessfully()); | |
311 client_->FetchPolicy(); | |
312 EXPECT_EQ(DM_STATUS_SUCCESS, client_->status()); | |
313 CheckPolicyResponse(); | |
314 } | |
315 | |
316 TEST_F(CloudPolicyClientTest, PolicyRequestFailure) { | |
317 Register(); | |
318 | |
319 EXPECT_CALL(service_, | |
320 CreateJob(DeviceManagementRequestJob::TYPE_POLICY_FETCH)) | |
321 .WillOnce(service_.FailJob(DM_STATUS_REQUEST_FAILED)); | |
322 EXPECT_CALL(service_, StartJob(_, _, _, _, _, _, _)); | |
323 EXPECT_CALL(observer_, OnClientError(_)); | |
324 EXPECT_CALL(status_provider_, OnSubmittedSuccessfully()).Times(0); | |
325 client_->FetchPolicy(); | |
326 EXPECT_EQ(DM_STATUS_REQUEST_FAILED, client_->status()); | |
327 EXPECT_FALSE(client_->GetPolicyFor(policy_ns_key_)); | |
328 } | |
329 | |
330 TEST_F(CloudPolicyClientTest, Unregister) { | |
331 Register(); | |
332 | |
333 ExpectUnregistration(kDMToken); | |
334 EXPECT_CALL(observer_, OnRegistrationStateChanged(_)); | |
335 client_->Unregister(); | |
336 EXPECT_FALSE(client_->is_registered()); | |
337 EXPECT_EQ(DM_STATUS_SUCCESS, client_->status()); | |
338 } | |
339 | |
340 TEST_F(CloudPolicyClientTest, UnregisterEmpty) { | |
341 Register(); | |
342 | |
343 unregistration_response_.clear_unregister_response(); | |
344 EXPECT_CALL(service_, | |
345 CreateJob(DeviceManagementRequestJob::TYPE_UNREGISTRATION)) | |
346 .WillOnce(service_.SucceedJob(unregistration_response_)); | |
347 EXPECT_CALL(service_, StartJob(_, _, _, _, _, _, _)); | |
348 EXPECT_CALL(observer_, OnRegistrationStateChanged(_)); | |
349 client_->Unregister(); | |
350 EXPECT_FALSE(client_->is_registered()); | |
351 EXPECT_EQ(DM_STATUS_SUCCESS, client_->status()); | |
352 } | |
353 | |
354 TEST_F(CloudPolicyClientTest, UnregisterFailure) { | |
355 Register(); | |
356 | |
357 EXPECT_CALL(service_, | |
358 CreateJob(DeviceManagementRequestJob::TYPE_UNREGISTRATION)) | |
359 .WillOnce(service_.FailJob(DM_STATUS_REQUEST_FAILED)); | |
360 EXPECT_CALL(service_, StartJob(_, _, _, _, _, _, _)); | |
361 EXPECT_CALL(observer_, OnClientError(_)); | |
362 client_->Unregister(); | |
363 EXPECT_TRUE(client_->is_registered()); | |
364 EXPECT_EQ(DM_STATUS_REQUEST_FAILED, client_->status()); | |
365 } | |
366 | |
367 TEST_F(CloudPolicyClientTest, PolicyFetchWithExtensionPolicy) { | |
368 Register(); | |
369 | |
370 // Setup the |expected_responses| and |policy_response_|. | |
371 static const char* kExtensions[] = { | |
372 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", | |
373 "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", | |
374 "cccccccccccccccccccccccccccccccc", | |
375 }; | |
376 typedef std::map<PolicyNamespaceKey, em::PolicyFetchResponse> ResponseMap; | |
377 ResponseMap expected_responses; | |
378 std::set<PolicyNamespaceKey> expected_namespaces; | |
379 PolicyNamespaceKey key(dm_protocol::kChromeUserPolicyType, std::string()); | |
380 // Copy the user policy fetch request. | |
381 expected_responses[key].CopyFrom( | |
382 policy_response_.policy_response().response(0)); | |
383 expected_namespaces.insert(key); | |
384 key.first = dm_protocol::kChromeExtensionPolicyType; | |
385 for (size_t i = 0; i < arraysize(kExtensions); ++i) { | |
386 key.second = kExtensions[i]; | |
387 em::PolicyData policy_data; | |
388 policy_data.set_policy_type(key.first); | |
389 policy_data.set_settings_entity_id(key.second); | |
390 expected_responses[key].set_policy_data(policy_data.SerializeAsString()); | |
391 policy_response_.mutable_policy_response()->add_response()->CopyFrom( | |
392 expected_responses[key]); | |
393 expected_namespaces.insert(key); | |
394 } | |
395 | |
396 // Make a policy fetch. | |
397 EXPECT_CALL(service_, | |
398 CreateJob(DeviceManagementRequestJob::TYPE_POLICY_FETCH)) | |
399 .WillOnce(service_.SucceedJob(policy_response_)); | |
400 EXPECT_CALL(service_, StartJob(dm_protocol::kValueRequestPolicy, "", "", | |
401 kDMToken, | |
402 dm_protocol::kValueUserAffiliationNone, | |
403 client_id_, _)) | |
404 .WillOnce(SaveArg<6>(&policy_request_)); | |
405 EXPECT_CALL(observer_, OnPolicyFetched(_)); | |
406 EXPECT_CALL(status_provider_, OnSubmittedSuccessfully()); | |
407 for (size_t i = 0; i < arraysize(kExtensions); ++i) { | |
408 client_->AddNamespaceToFetch(PolicyNamespaceKey( | |
409 dm_protocol::kChromeExtensionPolicyType, kExtensions[i])); | |
410 } | |
411 client_->FetchPolicy(); | |
412 | |
413 // Verify that the request includes the expected namespaces. | |
414 ASSERT_TRUE(policy_request_.has_policy_request()); | |
415 const em::DevicePolicyRequest& policy_request = | |
416 policy_request_.policy_request(); | |
417 ASSERT_EQ(static_cast<int>(1 + arraysize(kExtensions)), | |
418 policy_request.request_size()); | |
419 for (int i = 0; i < policy_request.request_size(); ++i) { | |
420 const em::PolicyFetchRequest& fetch_request = policy_request.request(i); | |
421 ASSERT_TRUE(fetch_request.has_policy_type()); | |
422 std::string entity_id; | |
423 if (fetch_request.has_settings_entity_id()) | |
424 entity_id = fetch_request.settings_entity_id(); | |
425 PolicyNamespaceKey key(fetch_request.policy_type(), entity_id); | |
426 EXPECT_EQ(1u, expected_namespaces.erase(key)); | |
427 } | |
428 EXPECT_TRUE(expected_namespaces.empty()); | |
429 | |
430 // Verify that the client got all the responses mapped to their namespaces. | |
431 for (ResponseMap::iterator it = expected_responses.begin(); | |
432 it != expected_responses.end(); ++it) { | |
433 const em::PolicyFetchResponse* response = client_->GetPolicyFor(it->first); | |
434 ASSERT_TRUE(response); | |
435 EXPECT_EQ(it->second.SerializeAsString(), response->SerializeAsString()); | |
436 } | |
437 } | |
438 | |
439 } // namespace policy | |
OLD | NEW |